Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderRuntimeRecoveryService.ts

1,259 lines48.6 KB

1import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
2import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
3import {
4 CommandId,
5 isProviderNativeSubagentThread,
6 type OrchestrationV2DomainEvent,
7 type ProviderThreadId,
8 type RunAttemptId,
9 type OrchestrationV2RestartCancelledBackgroundWork,
10 type OrchestrationV2Subagent,
11 type OrchestrationV2ThreadProjection,
12 isOrchestrationV2WorkActive,
13 ThreadId,
14} from "@t3tools/contracts";
15import * as Cause from "effect/Cause";
16import * as Context from "effect/Context";
17import * as DateTime from "effect/DateTime";
18import * as Effect from "effect/Effect";
19import * as Layer from "effect/Layer";
20import * as Option from "effect/Option";
21import * as Schema from "effect/Schema";
22​
23import * as EffectOutbox from "./EffectOutbox.ts";
24import * as EventSink from "./EventSink.ts";
25import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
26import * as ProjectionStore from "./ProjectionStore.ts";
27import * as ServerSettings from "../serverSettings.ts";
28import {
29 restartContinuationBlocked,
30 restartContinuationRun,
31 restartStopRequested,
32} from "./RestartContinuation.ts";
33import { isBufferedProviderNotification } from "./NotificationMailbox.ts";
34import { queuedRunsInDeliveryOrder } from "./QueuedRunOrder.ts";
35import {
36 endOrphanedNativeSubagent,
37 endRunlessRootTurns,
38} from "@t3tools/provider-core/server/subagentProjection";
39import {
40 awaitingProviderThreadId,
41 cancelledRosterTaskWork,
42 cancelledTurnItemWork,
43 mergeRestartCancelledBackgroundWork,
44 neverStarted,
45} from "./RestartBackgroundNote.ts";
46​
47export class ProviderRuntimeRecoveryError extends Schema.TaggedError<ProviderRuntimeRecoveryError>()(
48 "ProviderRuntimeRecoveryError",
49 {
50 operation: Schema.Literals(["read-projections", "reconcile", "drain-outbox"]),
51 threadId: Schema.optional(ThreadId),
52 cause: Schema.Defect(),
53 },
54) {
55 override get message(): string {
56 return `Provider runtime recovery failed during ${this.operation}.`;
57 }
59​
60const isProviderRuntimeRecoveryError = Schema.is(ProviderRuntimeRecoveryError);
61​
62const MAX_RESENDS = 3;
63​
64export interface ProviderRuntimeRecoverySummary {
65 readonly terminalizedRuns: number;
66 readonly stoppedSessions: number;
67 readonly closedRequests: number;
68 readonly retiredEffects: number;
69 readonly requeuedEffects: number;
71​
72export interface ProviderRuntimeReconciliationSummary {
73 readonly terminalizedRuns: number;
74 readonly stoppedSessions: number;
75 readonly closedRequests: number;
76 readonly retiredEffects: number;
77 readonly requeuedEffects: number;
79​
80export class ProviderRuntimeRecoveryService extends Context.Service<
81 ProviderRuntimeRecoveryService,
82 {
83 readonly reconcile: (
84 trigger: "startup" | "shutdown",
85 ) => Effect.Effect<ProviderRuntimeReconciliationSummary, ProviderRuntimeRecoveryError>;
86 readonly prepareForShutdown: Effect.Effect<void, ProviderRuntimeRecoveryError>;
87 readonly recover: Effect.Effect<ProviderRuntimeRecoverySummary, ProviderRuntimeRecoveryError>;
88 }
89>()("t3/orchestration-v2/ProviderRuntimeRecoveryService") {}
90​
91function nonterminalRuns(projection: ProjectionStore.ProjectionRuntimeRecoveryState) {
92 return projection.runs.filter((run) => {
93 const status: string = run.status;
94 return (
95 status === "preparing" ||
96 run.status === "starting" ||
97 run.status === "running" ||
98 run.status === "waiting"
99 );
100 });
102​
103function isBackgroundCapableTurnItemType(type: string): boolean {
104 return type === "command_execution" || type === "dynamic_tool" || type === "subagent";
106​
107function isNonterminalTurnItemStatus(status: string): boolean {
108 return status === "pending" || status === "running" || status === "waiting";
110​
111/**
112 * A delegate_task child. Its own thread is reconciled and continued on its own
113 * (here, or in the linked environment it runs in) and reports back through
114 * the app, so it is not provider background work.
115 */
116function isAppOwnedDelegation(task: {
117 readonly origin: OrchestrationV2Subagent["origin"];
118 readonly childThreadId: ThreadId | null;
119 readonly remoteChild?: OrchestrationV2Subagent["remoteChild"];
120}): boolean {
121 return (
122 task.origin === "app_owned" && (task.childThreadId !== null || task.remoteChild !== undefined)
123 );
125​
126function isAppOwnedDelegationItem(
127 item: OrchestrationV2ThreadProjection["turnItems"][number],
128): boolean {
129 return item.type === "subagent" && isAppOwnedDelegation(item);
131​
132function providerThreadHasPendingBackgroundTasks(
133 providerThread: OrchestrationV2ThreadProjection["providerThreads"][number],
134): boolean {
135 return (providerThread.pendingBackgroundTasks?.length ?? 0) > 0;
137​
138function resolveStaleBackgroundItemProviderInstanceId(
139 item: OrchestrationV2ThreadProjection["turnItems"][number],
140 projection: ProjectionStore.ProjectionRuntimeRecoveryState,
141): OrchestrationV2ThreadProjection["thread"]["providerInstanceId"] {
142 if (item.runId !== null) {
143 const run = projection.runs.find((candidate) => candidate.id === item.runId);
144 if (run !== undefined) {
145 return run.providerInstanceId;
146 }
147 }
148 if (item.type === "subagent") {
149 return item.providerInstanceId;
150 }
151 if (item.providerThreadId !== null && item.providerThreadId !== undefined) {
152 const providerThread = projection.providerThreads.find(
153 (candidate) => candidate.id === item.providerThreadId,
154 );
155 if (providerThread !== undefined) {
156 return providerThread.providerInstanceId;
157 }
158 }
159 return projection.providerThreads[0]?.providerInstanceId ?? projection.thread.providerInstanceId;
161​
162const cancelledItem = (
163 item: OrchestrationV2ThreadProjection["turnItems"][number],
164 now: DateTime.Utc,
165): OrchestrationV2ThreadProjection["turnItems"][number] =>
166 item.type === "secret_request" && item.secretStatus === "pending"
167 ? { ...item, status: "cancelled", secretStatus: "cancelled", completedAt: now, updatedAt: now }
168 : {
169 ...item,
170 ...("streaming" in item ? { streaming: false } : {}),
171 status: "cancelled",
172 completedAt: now,
173 updatedAt: now,
174 };
175​
176function latestStartedRun(
177 projection: ProjectionStore.ProjectionRuntimeRecoveryState,
178 providerThreadId: ProviderThreadId,
179) {
180 return projection.runs.reduce<OrchestrationV2ThreadProjection["runs"][number] | undefined>(
181 (latest, run) =>
182 run.providerThreadId === providerThreadId &&
183 run.status !== "queued" &&
184 run.status !== "rolled_back" &&
185 !neverStarted(run) &&
186 (latest === undefined || runRanAfter(run, latest))
187 ? run
188 : latest,
189 undefined,
190 );
192​
193export const make = Effect.gen(function* () {
194 const settings = yield* ServerSettings.ServerSettingsService;
195 const projections = yield* ProjectionStore.ProjectionStoreV2;
196 const eventSink = yield* EventSink.EventSinkV2;
197 const ids = yield* IdAllocator.IdAllocatorV2;
198 const outbox = yield* EffectOutbox.EffectOutboxV2;
199 type RecoverySession = ProjectionStore.ProjectionRuntimeRecoveryState["providerSessions"][number];
200 /** Shared sessions stop on their first recovered thread; later threads still decide wakes from pre-recovery liveness. */
201 const sessionsLiveAtStart = (
202 projection: ProjectionStore.ProjectionRuntimeRecoveryState,
203 stoppedThisPass: ReadonlyMap<string, RecoverySession>,
204 ) => {
205 const sessions = new Map(projection.providerSessions.map((session) => [session.id, session]));
206 for (const providerThread of projection.providerThreads) {
207 const stopped =
208 providerThread.providerSessionId === null
209 ? undefined
210 : stoppedThisPass.get(providerThread.providerSessionId);
211 if (stopped !== undefined) sessions.set(stopped.id, stopped);
212 }
213 return [...sessions.values()];
214 };
215​
216 /**
217 * A run is marked running before its provider accepts the prompt, so only a provider turn of
218 * one of its attempts proves the prompt arrived. Recovery treats the others as never started.
219 */
220 const rewindUnacceptedRuns = Effect.fn("ProviderRuntimeRecoveryService.rewindUnacceptedRuns")(
221 function* (projection: ProjectionStore.ProjectionRuntimeRecoveryState) {
222 const runIdByAttempt = new Map(
223 projection.runs.flatMap((run) =>
224 (run.status === "running" || (run.status === "starting" && run.startedAt !== null)) &&
225 run.activeAttemptId !== null
226 ? [[run.activeAttemptId, run.id] as const]
227 : [],
228 ),
229 );
230 if (runIdByAttempt.size === 0) return projection;
231 const unaccepted = new Set(runIdByAttempt.values());
232 const accept = (turns: ReadonlyArray<{ readonly runAttemptId: RunAttemptId | null }>) => {
233 for (const turn of turns) {
234 const runId =
235 turn.runAttemptId === null ? undefined : runIdByAttempt.get(turn.runAttemptId);
236 if (runId !== undefined) unaccepted.delete(runId);
237 }
238 };
239 accept(projection.providerTurns);
240 if (unaccepted.size === 0) return projection;
241 for (const attempt of projection.attempts) {
242 if (unaccepted.has(attempt.runId)) runIdByAttempt.set(attempt.id, attempt.runId);
243 }
244 // The recovery projection omits ended provider turns, such as one a steer replaced.
245 accept(
246 (yield* projections
247 .getThreadRecords(projection.thread.id, ["providerTurns"], {
248 providerTurnAttemptIds: [...runIdByAttempt].flatMap(([attemptId, runId]) =>
249 unaccepted.has(runId) ? [attemptId] : [],
250 ),
251 })
252 .pipe(
253 Effect.mapError(
254 (cause) =>
255 new ProviderRuntimeRecoveryError({
256 operation: "read-projections",
257 threadId: projection.thread.id,
258 cause,
259 }),
260 ),
261 )).providerTurns,
262 );
263 return {
264 ...projection,
265 runs: projection.runs.map((run) =>
266 unaccepted.has(run.id) ? { ...run, status: "starting" as const, startedAt: null } : run,
267 ),
268 };
269 },
270 );
271​
272 const reconcileProjection = Effect.fn("ProviderRuntimeRecoveryService.reconcileProjection")(
273 function* (
274 stored: ProjectionStore.ProjectionRuntimeRecoveryState,
275 trigger: "startup" | "shutdown",
276 continueAfterRestart: boolean,
277 stoppedThisPass: ReadonlyMap<string, RecoverySession> = new Map(),
278 ) {
279 const now = yield* DateTime.now;
280 const projection = yield* rewindUnacceptedRuns(stored);
281 let runs = [] as Array<OrchestrationV2ThreadProjection["runs"][number]>;
282 for (const run of nonterminalRuns(projection)) {
283 if (run.status === "waiting") {
284 const checkpointEffects = yield* outbox
285 .listByCommandId(CommandId.make(`command:effect:checkpoint.capture:${run.id}`))
286 .pipe(
287 Effect.mapError(
288 (cause) =>
289 new ProviderRuntimeRecoveryError({
290 operation: "reconcile",
291 threadId: projection.thread.id,
292 cause,
293 }),
294 ),
295 );
296 const hasReplayableCheckpoint = checkpointEffects.some(
297 (effect) =>
298 effect.request.type === "checkpoint.capture" &&
299 effect.request.runId === run.id &&
300 (effect.status === "pending" || effect.status === "running"),
301 );
302 if (hasReplayableCheckpoint) continue;
303 }
304 runs.push(run);
305 }
306 const pendingRequests = projection.runtimeRequests.filter(
307 (request) => request.status === "pending",
308 );
309 // The lost provider callback cannot resume; persisted questions can be
310 // answered by a new user message in threads that accept messages.
311 const questions = pendingRequests.filter(
312 (request) =>
313 request.kind === "user_input" &&
314 request.responseCapability.type === "live" &&
315 projection.turnItems.some(
316 (item) =>
317 item.type === "user_input_request" &&
318 item.requestId === request.id &&
319 item.nodeId === request.nodeId &&
320 item.questions.length > 0 &&
321 item.questions.every((question) => question.question.trim().length > 0),
322 ) &&
323 !isProviderNativeSubagentThread(projection.thread),
324 );
325 const recoveredRequestIds = new Set(questions.map((request) => request.id));
326 const messageRequestNodeIds = new Set(
327 pendingRequests
328 .filter(
329 (request) =>
330 request.responseCapability.type === "message" || recoveredRequestIds.has(request.id),
331 )
332 .map((request) => request.nodeId),
333 );
334 const requests = pendingRequests.filter(
335 (request) =>
336 request.responseCapability.type !== "message" && !recoveredRequestIds.has(request.id),
337 );
338 // Delegated task rows, items and nodes stay open: the child settles them.
339 const delegatedTaskNodeIds = new Set<string>([
340 ...(projection.subagents ?? []).filter(isAppOwnedDelegation).map((subagent) => subagent.id),
341 ...(projection.turnItems ?? []).flatMap((item) =>
342 item.type === "subagent" && isAppOwnedDelegation(item) ? [item.subagentId] : [],
343 ),
344 ]);
345 const detail = `Cancelled because the server ${trigger === "startup" ? "restarted" : "shut down"} before the provider work completed.`;
346 const commandId = CommandId.make(
347 `command:runtime-reconcile:${trigger}:${projection.thread.id}:${DateTime.formatIso(now)}`,
348 );
349 const allocateEventId = () =>
350 ids.allocate.event({ threadId: projection.thread.id, commandId }).pipe(
351 Effect.mapError(
352 (cause) =>
353 new ProviderRuntimeRecoveryError({
354 operation: "reconcile",
355 threadId: projection.thread.id,
356 cause,
357 }),
358 ),
359 );
360 const events: Array<OrchestrationV2DomainEvent> = [];
361 const cancelledBackgroundWork = new Map<
362 ProviderThreadId,
363 Array<OrchestrationV2RestartCancelledBackgroundWork>
364 >();
365 const cancelledBackgroundNativeIds = new Set<string>();
366 const recordCancelledBackgroundWork = (
367 providerThreadId: ProviderThreadId | null | undefined,
368 work: OrchestrationV2RestartCancelledBackgroundWork,
369 ) => {
370 if (providerThreadId == null) return;
371 const existing = cancelledBackgroundWork.get(providerThreadId);
372 if (existing === undefined) cancelledBackgroundWork.set(providerThreadId, [work]);
373 else existing.push(work);
374 };
375 const recordCancelledBackgroundItem = (
376 item: OrchestrationV2ThreadProjection["turnItems"][number],
377 ) => {
378 if (!isBackgroundCapableTurnItemType(item.type)) return;
379 const awaitingId = awaitingProviderThreadId(item, projection.runs);
380 const ownProviderThread =
381 item.providerThreadId === awaitingId
382 ? undefined
383 : (projection.providerThreads ?? []).find(
384 (candidate) => candidate.id === item.providerThreadId,
385 );
386 const work = cancelledTurnItemWork(
387 item,
388 ownProviderThread?.nativeThreadRef?.nativeId ?? undefined,
389 );
390 if (work === undefined) return;
391 recordCancelledBackgroundWork(awaitingId, work);
392 if (item.nativeItemRef?.nativeId != null) {
393 cancelledBackgroundNativeIds.add(item.nativeItemRef.nativeId);
394 }
395 };
396 const unstartedRuns = runs.filter(
397 (run) =>
398 run.status === "starting" &&
399 neverStarted(run) &&
400 !projection.providerTurns.some((turn) => turn.runAttemptId === run.activeAttemptId) &&
401 !projection.runtimeRequests.some(
402 (request) =>
403 request.status === "pending" &&
404 projection.nodes.some((node) => node.id === request.nodeId && node.runId === run.id),
405 ),
406 );
407 const queuedRuns = projection.runs.filter((run) => run.status === "queued");
408 const pendingRuns = [...unstartedRuns, ...queuedRuns];
409 const queuedMessages =
410 pendingRuns.length === 0
411 ? []
412 : [
413 ...(yield* projections
414 .getMessagesById(
415 projection.thread.id,
416 pendingRuns.map((run) => run.userMessageId),
417 )
418 .pipe(
419 Effect.mapError(
420 (cause) =>
421 new ProviderRuntimeRecoveryError({
422 operation: "read-projections",
423 threadId: projection.thread.id,
424 cause,
425 }),
426 ),
427 )).values(),
428 ];
429 const bufferedMessageIds = new Set(
430 queuedMessages.filter(isBufferedProviderNotification).map((message) => message.id),
431 );
432 const retryableMessageIds = new Set(
433 queuedMessages
434 .filter(
435 (message) =>
436 message.createdBy === "user" &&
437 message.scheduledTaskId === undefined &&
438 message.notification === undefined &&
439 message.delegatedCompletion === undefined,
440 )
441 .map((message) => message.id),
442 );
443 // A saved user message the provider never accepted stays available through the held queue.
444 const retryableRuns = unstartedRuns.filter((run) =>
445 retryableMessageIds.has(run.userMessageId),
446 );
447 // Nobody reviews a delegated, agent-sent or scheduled prompt, so it is sent again instead.
448 const unattendedRuns = continueAfterRestart
449 ? unstartedRuns.filter((run) =>
450 queuedMessages.some(
451 (message) =>
452 message.id === run.userMessageId &&
453 !retryableMessageIds.has(message.id) &&
454 message.notification === undefined &&
455 message.delegatedCompletion === undefined &&
456 (message.scheduledTaskId !== undefined || message.creationSource === "mcp"),
457 ),
458 )
459 : [];
460 const resentRuns: Array<OrchestrationV2ThreadProjection["runs"][number]> = [];
461 const resends: Array<EffectOutbox.PendingOrchestrationEffectV2> = [];
462 for (const run of unattendedRuns) {
463 if (
464 restartContinuationBlocked(projection, run, now, false) ||
465 (yield* restartStopRequested(projections.getThreadRecords, projection.thread, run).pipe(
466 Effect.mapError(
467 (cause) =>
468 new ProviderRuntimeRecoveryError({
469 operation: "read-projections",
470 threadId: projection.thread.id,
471 cause,
472 }),
473 ),
474 ))
475 )
476 continue;
477 const resendCommandId = CommandId.make(`command:restart-resend:${run.id}`);
478 const earlierResends = yield* outbox.listByCommandId(resendCommandId).pipe(
479 Effect.mapError(
480 (cause) =>
481 new ProviderRuntimeRecoveryError({
482 operation: "reconcile",
483 threadId: projection.thread.id,
484 cause,
485 }),
486 ),
487 );
488 // A prompt whose start keeps crashing the server would otherwise be sent forever.
489 if (earlierResends.filter((effect) => effect.attemptCount > 0).length >= MAX_RESENDS) {
490 yield* Effect.logWarning("Not resending a prompt that restarts cut too many times", {
491 threadId: projection.thread.id,
492 runId: run.id,
493 });
494 continue;
495 }
496 resentRuns.push(run);
497 resends.push({
498 id: `effect:restart-resend:${run.id}:${commandId}`,
499 commandId: resendCommandId,
500 threadId: projection.thread.id,
501 request: { type: "provider-turn.start", runId: run.id },
502 });
503 }
504 for (const run of resentRuns) {
505 events.push({
506 id: yield* allocateEventId(),
507 type: "run.updated",
508 threadId: projection.thread.id,
509 runId: run.id,
510 providerInstanceId: run.providerInstanceId,
511 occurredAt: now,
512 payload: run,
513 });
514 }
515 const keptRuns = [...retryableRuns, ...resentRuns];
516 for (const run of keptRuns) {
517 for (const attempt of projection.attempts.filter(
518 (candidate) => candidate.id === run.activeAttemptId && candidate.status === "running",
519 )) {
520 events.push({
521 id: yield* allocateEventId(),
522 type: "run-attempt.updated",
523 threadId: projection.thread.id,
524 runId: run.id,
525 nodeId: attempt.rootNodeId,
526 providerInstanceId: run.providerInstanceId,
527 occurredAt: now,
528 payload: { ...attempt, status: "pending", startedAt: null },
529 });
530 }
531 for (const node of projection.nodes.filter(
532 (candidate) => candidate.id === run.rootNodeId && candidate.status === "running",
533 )) {
534 events.push({
535 id: yield* allocateEventId(),
536 type: "node.updated",
537 threadId: projection.thread.id,
538 runId: run.id,
539 nodeId: node.id,
540 providerInstanceId: run.providerInstanceId,
541 occurredAt: now,
542 payload: { ...node, status: "pending", startedAt: null },
543 });
544 }
545 }
546 const retryableItems =
547 retryableRuns.length === 0
548 ? []
549 : (yield* projections
550 .getThreadRecords(projection.thread.id, ["turnItems"], {
551 turnItemTypes: ["user_message"],
552 turnItemRunIds: retryableRuns.map((run) => run.id),
553 })
554 .pipe(
555 Effect.mapError(
556 (cause) =>
557 new ProviderRuntimeRecoveryError({
558 operation: "read-projections",
559 threadId: projection.thread.id,
560 cause,
561 }),
562 ),
563 )).turnItems;
564 for (const item of retryableItems) {
565 if (item.type !== "user_message" || !retryableMessageIds.has(item.messageId)) continue;
566 events.push({
567 id: yield* allocateEventId(),
568 type: "turn-item.updated",
569 threadId: projection.thread.id,
570 ...(item.runId === null ? {} : { runId: item.runId }),
571 occurredAt: now,
572 payload: {
573 ...item,
574 inputIntent: "queued_turn",
575 status: "pending",
576 startedAt: null,
577 completedAt: null,
578 updatedAt: now,
579 },
580 });
581 }
582 runs = runs.filter((run) => !keptRuns.some((kept) => kept.id === run.id));
583 const heldRuns = [
584 ...retryableRuns,
585 ...queuedRunsInDeliveryOrder({ runs: queuedRuns, messages: queuedMessages }),
586 ];
587 for (const [index, run] of heldRuns.entries()) {
588 if (bufferedMessageIds.has(run.userMessageId)) {
589 runs.push(run);
590 continue;
591 }
592 if (run.queueHeld === true && retryableRuns.length === 0) continue;
593 events.push({
594 id: yield* allocateEventId(),
595 type: "run.updated",
596 threadId: projection.thread.id,
597 runId: run.id,
598 providerInstanceId: run.providerInstanceId,
599 occurredAt: now,
600 payload: {
601 ...run,
602 status: "queued",
603 queueHeld: true,
604 ...(retryableRuns.length === 0 ? {} : { queuePosition: index + 1 }),
605 },
606 });
607 }
608 const unsentResumeRunIds = runs.flatMap((run) =>
609 run.startedAt === null && run.usageLimitContinuationOfRunId !== undefined ? [run.id] : [],
610 );
611 const limitRecovery = projection.thread.limitRecovery;
612 // The unsent resume spent the worker's request id, so a fresh one lets it send again. An
613 // untagged resume from an older server stays cancelled: its copy couldn't be hidden.
614 if (limitRecovery?.autoResume === true && unsentResumeRunIds.length > 0) {
615 events.push({
616 id: yield* allocateEventId(),
617 type: "thread.metadata-updated",
618 threadId: projection.thread.id,
619 occurredAt: now,
620 payload: {
621 ...projection.thread,
622 limitRecovery: { ...limitRecovery, requestId: commandId },
623 updatedAt: now,
624 },
625 });
626 }
627 // The worker sends the auto-resume again, so the unsent copy leaves the transcript the way
628 // cancelled queued input does.
629 const unsentResumePrompts =
630 unsentResumeRunIds.length === 0
631 ? []
632 : (yield* projections
633 .getThreadRecords(projection.thread.id, ["turnItems"], {
634 turnItemTypes: ["user_message"],
635 turnItemRunIds: unsentResumeRunIds,
636 })
637 .pipe(
638 Effect.mapError(
639 (cause) =>
640 new ProviderRuntimeRecoveryError({
641 operation: "read-projections",
642 threadId: projection.thread.id,
643 cause,
644 }),
645 ),
646 )).turnItems;
647 const unsentResumePromptIds = new Set(unsentResumePrompts.map((item) => item.id));
648 for (const item of unsentResumePrompts) {
649 if (item.type !== "user_message") continue;
650 const active =
651 item.status === "pending" || item.status === "running" || item.status === "waiting";
652 events.push({
653 id: yield* allocateEventId(),
654 type: "turn-item.updated",
655 threadId: projection.thread.id,
656 ...(item.runId === null ? {} : { runId: item.runId }),
657 occurredAt: now,
658 payload: {
659 ...item,
660 ...(active ? { status: "cancelled", completedAt: now } : {}),
661 inputIntent: "queued_turn",
662 updatedAt: now,
663 },
664 });
665 }
666 for (const question of questions) {
667 events.push({
668 id: yield* allocateEventId(),
669 type: "runtime-request.updated",
670 threadId: projection.thread.id,
671 nodeId: question.nodeId,
672 occurredAt: now,
673 payload: { ...question, responseCapability: { type: "message" } },
674 });
675 }
676 for (const request of requests) {
677 events.push({
678 id: yield* allocateEventId(),
679 type: "runtime-request.updated",
680 threadId: projection.thread.id,
681 nodeId: request.nodeId,
682 occurredAt: now,
683 payload: {
684 ...request,
685 status: trigger === "startup" ? "expired" : "cancelled",
686 responseCapability: {
687 type: "not_resumable",
688 reason: `The server ${trigger === "startup" ? "restarted" : "shut down"} before this runtime request was resolved.`,
689 },
690 resolvedAt: now,
691 },
692 });
693 }
694 for (const run of runs) {
695 events.push({
696 id: yield* allocateEventId(),
697 type: "run.updated",
698 threadId: projection.thread.id,
699 runId: run.id,
700 providerInstanceId: run.providerInstanceId,
701 occurredAt: now,
702 payload: { ...run, status: "cancelled", queuePosition: null, completedAt: now },
703 });
704 for (const attempt of projection.attempts.filter(
705 (candidate) =>
706 candidate.runId === run.id &&
707 (candidate.status === "pending" || candidate.status === "running"),
708 )) {
709 events.push({
710 id: yield* allocateEventId(),
711 type: "run-attempt.updated",
712 threadId: projection.thread.id,
713 runId: run.id,
714 nodeId: attempt.rootNodeId,
715 providerInstanceId: run.providerInstanceId,
716 occurredAt: now,
717 payload: { ...attempt, status: "cancelled", completedAt: now },
718 });
719 }
720 for (const node of projection.nodes.filter(
721 (candidate) =>
722 candidate.runId === run.id &&
723 !messageRequestNodeIds.has(candidate.id) &&
724 !delegatedTaskNodeIds.has(candidate.id) &&
725 (candidate.status === "pending" ||
726 candidate.status === "running" ||
727 candidate.status === "waiting"),
728 )) {
729 events.push({
730 id: yield* allocateEventId(),
731 type: "node.updated",
732 threadId: projection.thread.id,
733 runId: run.id,
734 nodeId: node.id,
735 providerInstanceId: run.providerInstanceId,
736 occurredAt: now,
737 payload: { ...node, status: "cancelled", completedAt: now },
738 });
739 }
740 for (const providerTurn of projection.providerTurns.filter(
741 (candidate) =>
742 candidate.runAttemptId !== null &&
743 projection.attempts.some(
744 (attempt) => attempt.id === candidate.runAttemptId && attempt.runId === run.id,
745 ) &&
746 (candidate.status === "pending" || candidate.status === "running"),
747 )) {
748 events.push({
749 id: yield* allocateEventId(),
750 type: "provider-turn.updated",
751 threadId: projection.thread.id,
752 runId: run.id,
753 nodeId: providerTurn.nodeId,
754 providerInstanceId: run.providerInstanceId,
755 occurredAt: now,
756 payload: { ...providerTurn, status: "cancelled", completedAt: now },
757 });
758 }
759 for (const message of projection.messages.filter(
760 (candidate) => candidate.runId === run.id && candidate.streaming,
761 )) {
762 events.push({
763 id: yield* allocateEventId(),
764 type: "message.updated",
765 threadId: projection.thread.id,
766 runId: run.id,
767 ...(message.nodeId === null ? {} : { nodeId: message.nodeId }),
768 providerInstanceId: run.providerInstanceId,
769 occurredAt: now,
770 payload: { ...message, streaming: false, updatedAt: now },
771 });
772 }
773 for (const item of projection.turnItems.filter(
774 (candidate) =>
775 candidate.runId === run.id &&
776 !unsentResumePromptIds.has(candidate.id) &&
777 (candidate.nodeId === null || !messageRequestNodeIds.has(candidate.nodeId)) &&
778 !isAppOwnedDelegationItem(candidate) &&
779 // A link card waits on the user, not the run, and answers by message.
780 candidate.type !== "link_request" &&
781 (candidate.status === "pending" ||
782 candidate.status === "running" ||
783 candidate.status === "waiting"),
784 )) {
785 // Cut native subagents do not resume; record them for the continuation to restart.
786 if (run.status === "waiting") recordCancelledBackgroundItem(item);
787 else if (item.type === "subagent" && item.origin === "provider_native")
788 recordCancelledBackgroundItem(item);
789 events.push({
790 id: yield* allocateEventId(),
791 type: "turn-item.updated",
792 threadId: projection.thread.id,
793 runId: run.id,
794 ...(item.nodeId === null ? {} : { nodeId: item.nodeId }),
795 providerInstanceId: run.providerInstanceId,
796 occurredAt: now,
797 payload: cancelledItem(item, now),
798 });
799 }
800 // Queued runs and unsent resumes leave no visible turn behind, so there is nothing to explain.
801 if (run.status === "queued" || unsentResumeRunIds.includes(run.id)) continue;
802 events.push({
803 id: yield* allocateEventId(),
804 type: "turn-item.updated",
805 threadId: projection.thread.id,
806 runId: run.id,
807 ...(run.rootNodeId === null ? {} : { nodeId: run.rootNodeId }),
808 providerInstanceId: run.providerInstanceId,
809 occurredAt: now,
810 payload: {
811 id: ids.derive.runSignalTurnItem({ runId: run.id, signal: "server-recovery" }),
812 threadId: projection.thread.id,
813 runId: run.id,
814 nodeId: run.rootNodeId,
815 providerThreadId: run.providerThreadId,
816 providerTurnId: null,
817 nativeItemRef: null,
818 parentItemId: null,
819 // EventSink allocates the real position, after the run's existing items.
820 ordinal: 0,
821 status: "completed",
822 title: trigger === "startup" ? "Server restarted" : "Server shut down",
823 startedAt: now,
824 completedAt: now,
825 updatedAt: now,
826 type: "system_notice",
827 message: detail,
828 },
829 });
830 }
831 // Blocking waits die with their turn; let child results wake the parent continuation as async tasks.
832 const cutRunIds = new Set(runs.map((run) => run.id));
833 for (const task of projection.subagents ?? []) {
834 if (
835 task.origin !== "app_owned" ||
836 task.runId === null ||
837 !cutRunIds.has(task.runId) ||
838 task.completionWake !== "settled_only" ||
839 !isOrchestrationV2WorkActive(task.status)
840 )
841 continue;
842 events.push({
843 id: yield* allocateEventId(),
844 type: "subagent.updated",
845 threadId: projection.thread.id,
846 runId: task.runId,
847 nodeId: task.id,
848 driver: task.driver,
849 providerInstanceId: task.providerInstanceId,
850 occurredAt: now,
851 payload: { ...task, completionWake: "always", updatedAt: now },
852 });
853 }
854 const recoveredNonterminalRunIds = new Set(runs.map((run) => run.id));
855 const cancelledStaleNodeIds = new Set<string>();
856 for (const item of projection.turnItems ?? []) {
857 if (item.runId !== null && recoveredNonterminalRunIds.has(item.runId)) {
858 continue;
859 }
860 if (!isBackgroundCapableTurnItemType(item.type)) {
861 continue;
862 }
863 if (!isNonterminalTurnItemStatus(item.status) || isAppOwnedDelegationItem(item)) {
864 continue;
865 }
866 const providerInstanceId = resolveStaleBackgroundItemProviderInstanceId(item, projection);
867 recordCancelledBackgroundItem(item);
868 events.push({
869 id: yield* allocateEventId(),
870 type: "turn-item.updated",
871 threadId: projection.thread.id,
872 ...(item.runId === null ? {} : { runId: item.runId }),
873 ...(item.nodeId === null || item.nodeId === undefined ? {} : { nodeId: item.nodeId }),
874 providerInstanceId,
875 occurredAt: now,
876 payload: cancelledItem(item, now),
877 });
878 if (item.nodeId !== null && item.nodeId !== undefined) {
879 const staleItemNode = projection.nodes.find(
880 (candidate) =>
881 candidate.id === item.nodeId && isNonterminalTurnItemStatus(candidate.status),
882 );
883 if (staleItemNode !== undefined && !cancelledStaleNodeIds.has(staleItemNode.id)) {
884 cancelledStaleNodeIds.add(staleItemNode.id);
885 events.push({
886 id: yield* allocateEventId(),
887 type: "node.updated",
888 threadId: projection.thread.id,
889 ...(item.runId === null ? {} : { runId: item.runId }),
890 nodeId: staleItemNode.id,
891 providerInstanceId,
892 occurredAt: now,
893 payload: { ...staleItemNode, status: "cancelled", completedAt: now },
894 });
895 }
896 }
897 }
898 // No provider process survives the server; open native subagents can never report completion.
899 for (const subagent of projection.subagents ?? []) {
900 events.push(
901 ...(yield* endOrphanedNativeSubagent({
902 projection,
903 subagentId: subagent.id,
904 status: "cancelled",
905 now,
906 emitted: events,
907 allocateEventId,
908 })),
909 );
910 }
911 // Native subagent threads have no runs to settle their runless root turns.
912 events.push(
913 ...(yield* endRunlessRootTurns({
914 threadId: projection.thread.id,
915 providerInstanceId: projection.thread.providerInstanceId,
916 projection,
917 status: "cancelled",
918 now,
919 emitted: events,
920 allocateEventId,
921 })),
922 );
923 for (const providerThread of projection.providerThreads ?? []) {
924 const needsIdle = providerThread.status === "active";
925 const needsRosterClear = providerThreadHasPendingBackgroundTasks(providerThread);
926 if (!needsIdle && !needsRosterClear) {
927 continue;
928 }
929 if (providerThread.ownerNodeId === null) {
930 for (const task of providerThread.pendingBackgroundTasks ?? []) {
931 // A subagent already recorded covers the shells it started.
932 if (
933 cancelledBackgroundNativeIds.has(task.taskId) ||
934 (task.parentTaskId !== undefined &&
935 cancelledBackgroundNativeIds.has(task.parentTaskId))
936 )
937 continue;
938 cancelledBackgroundNativeIds.add(task.taskId);
939 recordCancelledBackgroundWork(providerThread.id, cancelledRosterTaskWork(task));
940 }
941 }
942 events.push({
943 id: yield* allocateEventId(),
944 type: "provider-thread.updated",
945 threadId: projection.thread.id,
946 driver: providerThread.driver,
947 providerInstanceId: providerThread.providerInstanceId,
948 occurredAt: now,
949 payload: {
950 ...providerThread,
951 status: needsIdle ? "idle" : providerThread.status,
952 pendingBackgroundTasks: [],
953 updatedAt: now,
954 },
955 });
956 }
957 for (const session of projection.providerSessions.filter(
958 (candidate) =>
959 (candidate.status !== "stopped" && candidate.status !== "error") ||
960 (candidate.status === "error" && (candidate.extensionStatuses?.length ?? 0) > 0),
961 )) {
962 events.push({
963 id: yield* allocateEventId(),
964 type: "provider-session.updated",
965 threadId: projection.thread.id,
966 driver: session.driver,
967 providerInstanceId: session.providerInstanceId,
968 occurredAt: now,
969 payload: {
970 ...session,
971 status: session.status === "error" ? "error" : "stopped",
972 ...(session.extensionStatuses === undefined ? {} : { extensionStatuses: [] }),
973 updatedAt: now,
974 lastError: session.status === "error" ? session.lastError : null,
975 },
976 });
977 }
978 for (const [providerThreadId, work] of cancelledBackgroundWork) {
979 const noteRun = latestStartedRun(projection, providerThreadId);
980 if (noteRun === undefined) continue;
981 // Use a separate event; stale run snapshots can overwrite intervening lifecycle changes.
982 events.push({
983 id: yield* allocateEventId(),
984 type: "run.background-work-cancelled",
985 threadId: projection.thread.id,
986 runId: noteRun.id,
987 providerInstanceId: noteRun.providerInstanceId,
988 occurredAt: now,
989 payload: {
990 runId: noteRun.id,
991 restartCancelledBackgroundWork: mergeRestartCancelledBackgroundWork(
992 noteRun.restartCancelledBackgroundWork ?? [],
993 work,
994 ),
995 },
996 });
997 }
998 const continuationRun =
999 continueAfterRestart && trigger === "startup"
1000 ? restartContinuationRun(
1001 { ...projection, providerSessions: sessionsLiveAtStart(projection, stoppedThisPass) },
1002 now,
1004 : undefined;
1005 let processLossCount = 1;
1006 // Normal upgrades must not exhaust an unfinished turn's crash allowance.
1007 if (
1008 continuationRun?.restartContinuationOfRunId !== undefined &&
1009 continuationRun.status !== "completed" &&
1010 continuationRun.status !== "waiting"
1011 ) {
1012 const previous = yield* outbox
1013 .get(`effect:restart-continuation:${continuationRun.restartContinuationOfRunId}`)
1014 .pipe(
1015 Effect.mapError(
1016 (cause) =>
1017 new ProviderRuntimeRecoveryError({
1018 operation: "reconcile",
1019 threadId: projection.thread.id,
1020 cause,
1021 }),
1022 ),
1023 );
1024 if (Option.isSome(previous) && previous.value.request.type === "provider-runtime.continue")
1025 processLossCount += previous.value.request.processLossCount ?? 0;
1027 const effects: Array<EffectOutbox.PendingOrchestrationEffectV2> = continuationRun
1028 ? [
1030 id: `effect:restart-continuation:${continuationRun.id}`,
1031 commandId,
1032 threadId: projection.thread.id,
1033 request: {
1034 type: "provider-runtime.continue",
1035 sourceRunId: continuationRun.id,
1036 processLossCount,
1037 },
1038 },
1040 : [];
1041 const stoppedSessions = projection.providerSessions.filter(
1042 (candidate) => candidate.status !== "stopped" && candidate.status !== "error",
1043 ).length;
1044 let retiredEffects: number;
1045 let cancelledEffectIds: ReadonlyArray<string> = [];
1046 if (events.length === 0) {
1047 const retiredEffectIds = yield* outbox
1048 .cancelUnsettled({
1049 threadId: projection.thread.id,
1050 effectTypes: EffectOutbox.PROCESS_BOUND_EFFECT_TYPES,
1051 reason: detail,
1052 })
1053 .pipe(
1054 Effect.mapError(
1055 (cause) =>
1056 new ProviderRuntimeRecoveryError({
1057 operation: "reconcile",
1058 threadId: projection.thread.id,
1059 cause: { detail, cause },
1060 }),
1061 ),
1062 );
1063 cancelledEffectIds = retiredEffectIds;
1064 retiredEffects = retiredEffectIds.length;
1065 } else {
1066 const result = yield* eventSink
1067 .commitCommand({
1068 commandId,
1069 threadId: projection.thread.id,
1070 commandType: "provider-runtime.reconcile",
1071 acceptedAt: now,
1072 events,
1073 effects,
1074 cancelUnsettledEffects: {
1075 effectTypes: EffectOutbox.PROCESS_BOUND_EFFECT_TYPES,
1076 reason: detail,
1077 },
1078 })
1079 .pipe(
1080 Effect.mapError(
1081 (cause) =>
1082 new ProviderRuntimeRecoveryError({
1083 operation: "reconcile",
1084 threadId: projection.thread.id,
1085 cause,
1086 }),
1087 ),
1088 );
1089 retiredEffects = result.cancelledEffectCount;
1091 return {
1092 terminalizedRuns: runs.length,
1093 stoppedSessions,
1094 closedRequests: requests.length,
1095 retiredEffects,
1096 cancelledEffectIds,
1097 resends,
1098 };
1099 },
1100 );
1101​
1102 const reconcile = Effect.fn("ProviderRuntimeRecoveryService.reconcile")(function* (
1103 trigger: "startup" | "shutdown",
1104 ) {
1105 const continueAfterRestart = yield* settings.getSettings.pipe(Effect.orElseSucceed(() => null));
1106 const threadIds = yield* projections
1107 .getRecoveryThreadIds("runtime")
1108 .pipe(
1109 Effect.mapError(
1110 (cause) => new ProviderRuntimeRecoveryError({ operation: "read-projections", cause }),
1111 ),
1112 );
1113 let terminalizedRuns = 0;
1114 let stoppedSessions = 0;
1115 let closedRequests = 0;
1116 let retiredEffects = 0;
1117 const resends: Array<EffectOutbox.PendingOrchestrationEffectV2> = [];
1118 const stoppedThisPass = new Map<string, RecoverySession>();
1119 const grouped = trigger === "shutdown" && threadIds.length > 1;
1120 const groupSize = grouped ? 32 : 1;
1121 for (let offset = 0; offset < threadIds.length; offset += groupSize) {
1122 const cancelledEffectIds: string[] = [];
1123 // Read and decide on the writer so shared sessions see earlier updates in this group.
1124 const reconcileGroup = Effect.gen(function* () {
1125 for (const threadId of threadIds.slice(offset, offset + groupSize)) {
1126 const projection = yield* projections.getRuntimeRecoveryProjection(threadId).pipe(
1127 Effect.mapError(
1128 (cause) =>
1129 new ProviderRuntimeRecoveryError({
1130 operation: "read-projections",
1131 threadId,
1132 cause,
1133 }),
1134 ),
1135 );
1136 const enabled =
1137 continueAfterRestart !== null &&
1138 resolveProjectSettings(continueAfterRestart, projection.thread.projectId).settings
1139 .continueThreadsAfterServerUpdate;
1140 const result = yield* reconcileProjection(projection, trigger, enabled, stoppedThisPass);
1141 for (const session of projection.providerSessions) {
1142 if (session.status !== "stopped" && session.status !== "error") {
1143 stoppedThisPass.set(session.id, session);
1146 cancelledEffectIds.push(...result.cancelledEffectIds);
1147 resends.push(...result.resends);
1148 terminalizedRuns += result.terminalizedRuns;
1149 stoppedSessions += result.stoppedSessions;
1150 closedRequests += result.closedRequests;
1151 retiredEffects += result.retiredEffects;
1153 });
1154 yield* (grouped ? eventSink.withProviderBatch(reconcileGroup) : reconcileGroup).pipe(
1155 Effect.mapError((cause) =>
1156 isProviderRuntimeRecoveryError(cause)
1157 ? cause
1158 : new ProviderRuntimeRecoveryError({ operation: "reconcile", cause }),
1159 ),
1160 );
1161 // Eventless outbox cancellations also wait for the group's durable commit.
1162 if (cancelledEffectIds.length > 0) yield* outbox.signalCancellations(cancelledEffectIds);
1164 const outboxReconciliation = yield* outbox.reconcileAfterProcessLoss.pipe(
1165 Effect.mapError(
1166 (cause) => new ProviderRuntimeRecoveryError({ operation: "drain-outbox", cause }),
1167 ),
1168 );
1169 // After the drain, which cancels every unsettled provider start. A crash before this write
1170 // leaves the run unstarted, so the next recovery sends it again.
1171 if (resends.length > 0) {
1172 yield* eventSink
1173 .writeWithEffects({ events: [], effects: resends })
1174 .pipe(
1175 Effect.mapError(
1176 (cause) => new ProviderRuntimeRecoveryError({ operation: "reconcile", cause }),
1177 ),
1178 );
1180 return {
1181 terminalizedRuns,
1182 stoppedSessions,
1183 closedRequests,
1184 retiredEffects: retiredEffects + outboxReconciliation.cancelled,
1185 requeuedEffects: outboxReconciliation.requeued,
1186 } satisfies ProviderRuntimeReconciliationSummary;
1187 });
1188​
1189 const prepareForShutdown = Effect.gen(function* () {
1190 const enabled = yield* settings.getSettings.pipe(Effect.orElseSucceed(() => null));
1191 if (!enabled) return;
1192 const enabledProjects = Object.entries(enabled.projectSettingsOverrides)
1193 .filter(([, overrides]) => overrides.continueThreadsAfterServerUpdate === true)
1194 .map(([id]) => id);
1195 if (!enabled.continueThreadsAfterServerUpdate && enabledProjects.length === 0) return;
1196 const disabledProjects = Object.entries(enabled.projectSettingsOverrides)
1197 .filter(([, overrides]) => overrides.continueThreadsAfterServerUpdate === false)
1198 .map(([id]) => id);
1199 const threadIds = yield* projections.getRecoveryThreadIds(
1200 "restart-continuations",
1201 enabled.continueThreadsAfterServerUpdate
1202 ? { exclude: disabledProjects }
1203 : { include: enabledProjects },
1204 );
1205 yield* Effect.forEach(
1206 threadIds,
1207 (threadId) =>
1208 Effect.gen(function* () {
1209 const projection = yield* projections.getRuntimeRecoveryProjection(threadId);
1210 if (
1211 !resolveProjectSettings(enabled, projection.thread.projectId).settings
1212 .continueThreadsAfterServerUpdate
1214 return;
1215 const run = restartContinuationRun(
1216 yield* rewindUnacceptedRuns(projection),
1217 yield* DateTime.now,
1218 );
1219 if (!run) return;
1220 const commandId = CommandId.make(`command:restart-prepare:${run.id}`);
1221 yield* eventSink.writeWithEffects({
1222 commandId,
1223 events: [],
1224 effects: [
1226 id: `effect:restart-continuation:${run.id}`,
1227 commandId,
1228 threadId,
1229 request: {
1230 type: "provider-runtime.continue",
1231 sourceRunId: run.id,
1232 processLossCount: 0,
1233 },
1234 },
1235 ],
1236 });
1237 }).pipe(
1238 Effect.catchCauseIf(
1239 (cause) => !Cause.hasInterruptsOnly(cause),
1240 (cause) =>
1241 Effect.logWarning("Failed to prepare a restart continuation", { threadId, cause }),
1242 ),
1243 ),
1244 // Leave room in the four-reader pool for other shutdown reads.
1245 { concurrency: 2, discard: true },
1246 );
1247 }).pipe(
1248 Effect.withSpan("ProviderRuntimeRecoveryService.prepareForShutdown"),
1249 Effect.mapError((cause) => new ProviderRuntimeRecoveryError({ operation: "reconcile", cause })),
1250 );
1251​
1252 const recover = Effect.gen(function* () {
1253 return (yield* reconcile("startup")) satisfies ProviderRuntimeRecoverySummary;
1254 });
1255​
1256 return ProviderRuntimeRecoveryService.of({ reconcile, prepareForShutdown, recover });
1257});
1258​
1259export const layer = Layer.effect(ProviderRuntimeRecoveryService, make);