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