@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 533 lines 21.1 KB
Raw
1 import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
2 import {
3 MessageId,
4 type ModelSelection,
5 type NodeId,
6 ProviderSessionId,
7 ProviderThreadId,
8 ProviderTurnId,
9 RunAttemptId,
10 ThreadId,
11 withMessageSenderNote,
12 } from "@t3tools/contracts";
13 import type * as DateTime from "effect/DateTime";
14 import * as Context from "effect/Context";
15 import * as Effect from "effect/Effect";
16 import * as Layer from "effect/Layer";
17 import * as Option from "effect/Option";
18 import * as Schema from "effect/Schema";
19 import * as Stream from "effect/Stream";
20
21 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
22 import * as McpAppModelContext from "../mcpApps/McpAppModelContext.ts";
23 import * as EventSink from "./EventSink.ts";
24 import * as ProjectionStore from "./ProjectionStore.ts";
25 import * as ProviderSessionManager from "./ProviderSessionManager.ts";
26
27 const yieldToRuntime = Effect.yieldNow.pipe(
28 Effect.andThen(
29 Effect.promise(
30 () =>
31 new Promise<void>((resolve) => {
32 setImmediate(resolve);
33 }),
34 ),
35 ),
36 );
37
38 export class ProviderTurnControlError extends Schema.TaggedError<ProviderTurnControlError>()(
39 "ProviderTurnControlError",
40 {
41 threadId: ThreadId,
42 operation: Schema.Literals(["interrupt", "restart", "steer"]),
43 providerTurnId: ProviderTurnId,
44 turnCompleted: Schema.optional(Schema.Boolean),
45 steerDeclined: Schema.optional(Schema.Boolean),
46 /** The provider never confirmed the stop; Force stop is the way out. */
47 unresponsive: Schema.optional(Schema.Boolean),
48 cause: Schema.optional(Schema.Defect()),
49 },
50 ) {}
51
52 const isProviderTurnControlError = Schema.is(ProviderTurnControlError);
53
54 export interface ProviderTurnControlServiceV2Shape {
55 readonly interrupt: (input: {
56 readonly subagent?: {
57 readonly id: NodeId;
58 readonly nativeTaskId: string;
59 readonly requestedAt?: DateTime.Utc;
60 };
61 readonly threadId: ThreadId;
62 readonly providerSessionId: ProviderSessionId;
63 readonly providerThreadId: ProviderThreadId;
64 readonly providerTurnId: ProviderTurnId;
65 }) => Effect.Effect<
66 void | ProviderAdapter.ProviderAdapterV2SubagentOutcome,
67 ProviderTurnControlError
68 >;
69 readonly steer: (input: {
70 readonly threadId: ThreadId;
71 readonly providerSessionId: ProviderSessionId;
72 readonly providerThreadId: ProviderThreadId;
73 readonly providerTurnId: ProviderTurnId;
74 readonly messageId: MessageId;
75 readonly modelSelection?: ModelSelection;
76 }) => Effect.Effect<void, ProviderTurnControlError>;
77 /** Best effort: a turn that ended or moved sessions picks the selection up when it next starts. */
78 readonly applyModelSelection: (input: {
79 readonly threadId: ThreadId;
80 readonly providerSessionId: ProviderSessionId;
81 readonly providerThreadId: ProviderThreadId;
82 readonly modelSelection: ModelSelection;
83 }) => Effect.Effect<
84 void,
85 | ProjectionStore.ProjectionStoreV2Error
86 | ProviderSessionManager.ProviderSessionManagerV2Error
87 | ProviderAdapter.ProviderAdapterV2Error
88 >;
89 /** Best effort: a provider thread that is no longer live picks the title up when its next turn starts. */
90 readonly applyThreadTitle: (input: {
91 readonly threadId: ThreadId;
92 readonly title: string;
93 }) => Effect.Effect<
94 void,
95 | ProjectionStore.ProjectionStoreV2Error
96 | ProviderSessionManager.ProviderSessionManagerV2Error
97 | ProviderAdapter.ProviderAdapterV2Error
98 >;
99 readonly interruptAndAwaitTerminal: (input: {
100 readonly threadId: ThreadId;
101 readonly providerSessionId: ProviderSessionId;
102 readonly replacementProviderSessionId?: ProviderSessionId;
103 readonly providerThreadId: ProviderThreadId;
104 readonly providerTurnId: ProviderTurnId;
105 readonly interruptedAttemptId: RunAttemptId;
106 }) => Effect.Effect<void, ProviderTurnControlError>;
107 }
108
109 export class ProviderTurnControlServiceV2 extends Context.Service<
110 ProviderTurnControlServiceV2,
111 ProviderTurnControlServiceV2Shape
112 >()("t3/orchestration-v2/ProviderTurnControlService/ProviderTurnControlServiceV2") {}
113
114 export const layer: Layer.Layer<
115 ProviderTurnControlServiceV2,
116 never,
117 | EventSink.EventSinkV2
118 | McpAppModelContext.McpAppModelContext
119 | ProjectionStore.ProjectionStoreV2
120 | ProviderSessionManager.ProviderSessionManagerV2
121 > = Layer.effect(
122 ProviderTurnControlServiceV2,
123 Effect.gen(function* () {
124 const eventSink = yield* EventSink.EventSinkV2;
125 const projections = yield* ProjectionStore.ProjectionStoreV2;
126 const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
127 const mcpAppModelContext = yield* McpAppModelContext.McpAppModelContext;
128
129 const load = Effect.fn("ProviderTurnControlService.load")(function* (input: {
130 readonly threadId: ThreadId;
131 readonly providerSessionId: ProviderSessionId;
132 readonly replacementProviderSessionId?: ProviderSessionId;
133 readonly providerThreadId: ProviderThreadId;
134 readonly providerTurnId: ProviderTurnId;
135 readonly operation: "interrupt" | "restart" | "steer";
136 readonly messageId?: MessageId;
137 }) {
138 const context = yield* projections.getProviderControlContext(input.threadId, input);
139 const { providerThread, providerTurn } = context;
140 const targetsRecordedSession = providerThread?.providerSessionId === input.providerSessionId;
141 const targetsCommittedReplacement =
142 input.operation === "restart" &&
143 input.replacementProviderSessionId !== undefined &&
144 providerThread?.providerSessionId === input.replacementProviderSessionId;
145 if (
146 providerThread === undefined ||
147 providerTurn === undefined ||
148 (!targetsRecordedSession && !targetsCommittedReplacement) ||
149 providerTurn.providerThreadId !== providerThread.id
150 ) {
151 return yield* new ProviderTurnControlError({
152 threadId: input.threadId,
153 operation: input.operation,
154 providerTurnId: input.providerTurnId,
155 cause: "The recorded provider execution target is no longer valid.",
156 });
157 }
158 // A restart-session command commits the replacement binding before its
159 // process-bound effect runs. The old live runtime must still receive
160 // the interrupt, but only when the projection matches the exact
161 // replacement captured by that same durable effect.
162 const interruptProviderThread = targetsRecordedSession
163 ? providerThread
164 : { ...providerThread, providerSessionId: input.providerSessionId };
165 const continuingRun =
166 input.operation === "steer" &&
167 context.run?.status === "running" &&
168 context.run.activeAttemptId === providerTurn.runAttemptId;
169 if (providerTurn.status !== "running" && !continuingRun) {
170 if (input.operation === "steer") {
171 return yield* new ProviderTurnControlError({
172 threadId: input.threadId,
173 operation: "steer",
174 providerTurnId: input.providerTurnId,
175 turnCompleted: providerTurn.status === "completed",
176 cause: "The provider turn ended before the steering message was delivered.",
177 });
178 }
179 return {
180 context,
181 providerThread: interruptProviderThread,
182 providerTurn,
183 session: Option.none(),
184 };
185 }
186 const session = yield* sessions.get(input.providerSessionId);
187 if (Option.isNone(session)) {
188 // Interrupt/restart against a already-released session must not fail
189 // the durable effect (and retry 5x). The turn may still look running
190 // in projection until recovery/finalization; there is no live adapter
191 // to interrupt.
192 if (input.operation === "interrupt" || input.operation === "restart") {
193 yield* Effect.logWarning(
194 "Provider interrupt/restart found no live session; treating as already stopped",
195 {
196 threadId: input.threadId,
197 operation: input.operation,
198 providerSessionId: input.providerSessionId,
199 providerTurnId: input.providerTurnId,
200 providerTurnStatus: providerTurn.status,
201 },
202 );
203 return {
204 context,
205 providerThread: interruptProviderThread,
206 providerTurn,
207 session: Option.none(),
208 };
209 }
210 return yield* new ProviderTurnControlError({
211 threadId: input.threadId,
212 operation: input.operation,
213 providerTurnId: input.providerTurnId,
214 cause: `Provider session ${input.providerSessionId} is not active.`,
215 });
216 }
217 return { context, providerThread: interruptProviderThread, providerTurn, session };
218 });
219
220 const awaitSettled = Effect.fn("ProviderTurnControlService.awaitSettled")(function* (
221 threadId: ThreadId,
222 target: ProjectionStore.ProjectionProviderControlTarget,
223 ) {
224 const isSettled = projections
225 .getProviderControlContext(threadId, target)
226 .pipe(
227 Effect.map(
228 ({ providerTurn, attempt }) =>
229 providerTurn?.status !== "running" && attempt?.status !== "running",
230 ),
231 );
232 const afterSequence = yield* eventSink.latestSequence({ threadId });
233 if (yield* isSettled) return;
234 yield* eventSink.stream({ threadId, afterSequence }).pipe(
235 Stream.filter(
236 ({ event }) =>
237 event.type === "provider-turn.updated" || event.type === "run-attempt.updated",
238 ),
239 Stream.mapEffect(() => isSettled),
240 Stream.takeUntil((settled) => settled),
241 Stream.runDrain,
242 );
243 });
244
245 return ProviderTurnControlServiceV2.of({
246 applyModelSelection: Effect.fn("ProviderTurnControlService.applyModelSelection")(
247 function* (input) {
248 const { providerThread, providerTurn } = yield* projections.getRunningTurnContext(
249 input.threadId,
250 );
251 if (
252 providerThread?.id !== input.providerThreadId ||
253 providerThread.providerSessionId !== input.providerSessionId ||
254 providerTurn === undefined
255 ) {
256 return;
257 }
258 const session = yield* sessions.get(input.providerSessionId);
259 if (Option.isNone(session) || session.value.applyModelSelection === undefined) return;
260 yield* session.value.applyModelSelection({
261 providerThread,
262 modelSelection: input.modelSelection,
263 });
264 },
265 ),
266 applyThreadTitle: Effect.fn("ProviderTurnControlService.applyThreadTitle")(function* (input) {
267 const { thread, providerThreads } = yield* projections.getThreadRecords(input.threadId, [
268 "providerThreads",
269 ]);
270 const providerThread = providerThreads.find(
271 (candidate) => candidate.id === thread.activeProviderThreadId,
272 );
273 if (!providerThread?.providerSessionId) return;
274 const session = yield* sessions.get(providerThread.providerSessionId);
275 if (Option.isNone(session) || session.value.applyThreadTitle === undefined) return;
276 yield* session.value.applyThreadTitle({ providerThread, title: input.title });
277 }),
278 interrupt: Effect.fn("ProviderTurnControlService.interrupt")(
279 function* (input) {
280 const loaded = yield* load({ ...input, operation: "interrupt" });
281 const session = Option.isSome(loaded.session)
282 ? loaded.session
283 : yield* sessions.get(input.providerSessionId);
284 if (Option.isNone(session)) {
285 if (input.subagent !== undefined) {
286 return yield* new ProviderTurnControlError({
287 threadId: input.threadId,
288 operation: "interrupt",
289 providerTurnId: input.providerTurnId,
290 cause: "No live provider session can confirm this subagent's outcome.",
291 });
292 }
293 return;
294 }
295 if (input.subagent !== undefined) {
296 if (session.value.stopSubagent === undefined) {
297 return yield* new ProviderTurnControlError({
298 threadId: input.threadId,
299 operation: "interrupt",
300 providerTurnId: input.providerTurnId,
301 cause: "The provider does not support stopping a native subagent.",
302 });
303 }
304 return yield* session.value.stopSubagent({
305 providerThread: loaded.providerThread,
306 nativeTaskId: input.subagent.nativeTaskId,
307 ...(input.subagent.requestedAt === undefined
308 ? {}
309 : { requestedAt: input.subagent.requestedAt }),
310 });
311 }
312 // Settled turns still reach adapters, which alone know whether background work remains.
313 yield* session.value
314 .interruptTurn({
315 providerThread: loaded.providerThread,
316 providerTurnId: loaded.providerTurn.id,
317 requestRuntimeRestart: true,
318 })
319 .pipe(
320 Effect.catchTags({
321 ProviderAdapterInterruptTimeoutError: (cause) =>
322 Effect.fail(
323 new ProviderTurnControlError({
324 threadId: input.threadId,
325 operation: "interrupt",
326 providerTurnId: input.providerTurnId,
327 unresponsive: true,
328 cause,
329 }),
330 ),
331 }),
332 );
333 if (loaded.providerTurn.status === "running") {
334 // Let native terminal ingestion finalize the attempt before the Stop follow-up repairs it.
335 const { runAttemptId } = loaded.providerTurn;
336 yield* awaitSettled(input.threadId, {
337 providerThreadId: input.providerThreadId,
338 providerTurnId: input.providerTurnId,
339 ...(runAttemptId === null ? {} : { attemptId: runAttemptId }),
340 }).pipe(Effect.timeoutOption("2 seconds"));
341 }
342 },
343 (effect, input) =>
344 effect.pipe(
345 Effect.mapError((cause) =>
346 isProviderTurnControlError(cause)
347 ? cause
348 : new ProviderTurnControlError({
349 threadId: input.threadId,
350 operation: "interrupt",
351 providerTurnId: input.providerTurnId,
352 cause,
353 }),
354 ),
355 ),
356 ),
357 interruptAndAwaitTerminal: Effect.fn("ProviderTurnControlService.interruptAndAwaitTerminal")(
358 function* (input) {
359 const loaded = yield* load({ ...input, operation: "restart" });
360 if (Option.isNone(loaded.session)) {
361 // Without a live adapter, no terminal event can arrive; proceed to detach/start without polling.
362 if (loaded.providerTurn.status === "running") {
363 yield* Effect.logWarning(
364 "Provider restart interrupt skipped; no live session for a still-projected running turn",
365 {
366 threadId: input.threadId,
367 providerSessionId: input.providerSessionId,
368 providerTurnId: input.providerTurnId,
369 },
370 );
371 }
372 return;
373 }
374
375 yield* loaded.session.value.interruptTurn({
376 providerThread: loaded.providerThread,
377 providerTurnId: loaded.providerTurn.id,
378 });
379
380 for (let remaining = 1_000; remaining > 0; remaining -= 1) {
381 const { providerTurn, attempt } = yield* projections.getProviderControlContext(
382 input.threadId,
383 {
384 providerThreadId: input.providerThreadId,
385 providerTurnId: input.providerTurnId,
386 attemptId: input.interruptedAttemptId,
387 },
388 );
389 if (
390 providerTurn !== undefined &&
391 providerTurn.status !== "running" &&
392 attempt !== undefined &&
393 attempt.status !== "running"
394 ) {
395 return;
396 }
397 // Yield through Node's event loop; detached ingestion cannot advance a deterministic Effect clock.
398 yield* yieldToRuntime;
399 }
400 return yield* new ProviderTurnControlError({
401 threadId: input.threadId,
402 operation: "restart",
403 providerTurnId: input.providerTurnId,
404 cause: `Provider turn ${input.providerTurnId} did not terminalize before restart.`,
405 });
406 },
407 (effect, input) =>
408 effect.pipe(
409 Effect.mapError((cause) =>
410 isProviderTurnControlError(cause)
411 ? cause
412 : new ProviderTurnControlError({
413 threadId: input.threadId,
414 operation: "restart",
415 providerTurnId: input.providerTurnId,
416 cause,
417 }),
418 ),
419 ),
420 ),
421 steer: Effect.fn("ProviderTurnControlService.steer")(
422 function* (input) {
423 const context = yield* projections.getProviderControlContext(input.threadId, input);
424 const ownership = context.message?.delegatedCompletion;
425 if (ownership !== undefined) {
426 const projection = yield* projections.getThreadRecords(input.threadId, ["runs"], {
427 runIds: [ownership.parentRunId],
428 });
429 const cohort = projection.runs.find(
430 (run) => run.id === ownership.parentRunId,
431 )?.delegatedCompletion;
432 if (
433 cohort?.disposition !== "open" ||
434 cohort.delivery?.messageId !== input.messageId ||
435 cohort.delivery.generation !== ownership.generation ||
436 cohort.delivery.taskIds.length === 0
437 )
438 return;
439 }
440 const loaded = yield* load({ ...input, operation: "steer" });
441 if (Option.isNone(loaded.session)) return;
442 const { message, run } = loaded.context;
443 if (message === undefined || run === undefined) {
444 return yield* new ProviderTurnControlError({
445 threadId: input.threadId,
446 operation: "steer",
447 providerTurnId: input.providerTurnId,
448 cause: "The persisted steering message or target run is missing.",
449 });
450 }
451 const appContext = yield* McpAppModelContext.forProvider(
452 mcpAppModelContext,
453 input.threadId,
454 );
455 yield* loaded.session.value
456 .steerTurn({
457 threadId: input.threadId,
458 runId: run.id,
459 providerThread: loaded.providerThread,
460 providerTurnId: loaded.providerTurn.id,
461 ...(input.modelSelection === undefined
462 ? {}
463 : { modelSelection: input.modelSelection }),
464 ...(appContext.length === 0 ? {} : { appContext }),
465 message: {
466 messageId: message.id,
467 text: withMessageSenderNote(
468 message,
469 projectComposerContextForProvider({
470 text: message.text,
471 records: message.context?.records ?? [],
472 }),
473 ),
474 attachments: message.attachments,
475 createdBy: message.createdBy,
476 creationSource: message.creationSource,
477 ...(message.scheduledTaskId === undefined
478 ? {}
479 : { scheduledTaskId: message.scheduledTaskId }),
480 ...(message.senderThreadId === undefined
481 ? {}
482 : { senderThreadId: message.senderThreadId }),
483 },
484 })
485 .pipe(
486 Effect.catchTags({
487 ProviderAdapterSteerDeclinedError: (cause) =>
488 Effect.fail(
489 new ProviderTurnControlError({
490 threadId: input.threadId,
491 operation: "steer",
492 providerTurnId: input.providerTurnId,
493 steerDeclined: true,
494 cause,
495 }),
496 ),
497 }),
498 Effect.catch((cause) =>
499 Effect.gen(function* () {
500 if (isProviderTurnControlError(cause)) return yield* cause;
501 const current = yield* projections.getProviderControlContext(
502 input.threadId,
503 input,
504 );
505 const status = current.providerTurn?.status;
506 return yield* new ProviderTurnControlError({
507 threadId: input.threadId,
508 operation: "steer",
509 providerTurnId: input.providerTurnId,
510 turnCompleted: status === "completed",
511 cause,
512 });
513 }),
514 ),
515 );
516 },
517 (effect, input) =>
518 effect.pipe(
519 Effect.mapError((cause) =>
520 isProviderTurnControlError(cause)
521 ? cause
522 : new ProviderTurnControlError({
523 threadId: input.threadId,
524 operation: "steer",
525 providerTurnId: input.providerTurnId,
526 cause,
527 }),
528 ),
529 ),
530 ),
531 });
532 }),
533 );