fix(server): tell the provider when a restart killed its background work #13895
Mergedjuliusmarmingemerged 8 commits into
t3code/codex-turn-mapping fromrefs/pull/13895/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration-v2/ProviderTurnStartService.ts line 1115Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1087,3 +1110,4 @@10871110 });10881111 if (!(yield* isCurrentAttemptInStatus("running"))) return;10891112 const start = compact ? session.compactThread! : session.startTurn;1113+ const context = [delivery.context, restartNote]apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts line 611OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -533,0 +591,21 @@591+ const current = existing?.type === "run.updated" ? existing.payload : noteRun;592+ const payload = {593+ ...current,594+ restartCancelledBackgroundWork: mergeRestartCancelledBackgroundWork(595+ current.restartCancelledBackgroundWork ?? [],596+ cancelledBackgroundWork,597+ ),598+ };599+ if (existing?.type === "run.updated") {600+ events[index] = { ...existing, payload };601+ } else {602+ // Like other settled-run snapshots, leave the completion cohort to its owner.603+ const { delegatedCompletion: _delegatedCompletion, ...unchanged } = payload;604+ events.push({605+ id: yield* allocateEventId(),606+ type: "run.updated",607+ threadId: projection.thread.id,608+ runId: noteRun.id,609+ providerInstanceId: noteRun.providerInstanceId,610+ occurredAt: now,611+ payload: unchanged,@@ -897,3 +901,5 @@897901 text: message.text,898902 records: message.context?.records ?? [],899903 });904+ // Delivered once: this run's provider turn marks the work as told.905+ const restartCancelledWork = pendingRestartCancelledBackgroundWork({
- apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts lines 47 to 50OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -44,3 +44,7 @@4444 import { layer as providerContinuationRequestsLayer } from "../ProviderContinuationRequests.ts";4545 import { workerLive as providerContinuationWorkerLive } from "../ProviderContinuationService.ts";4646 import { layer as providerEventIngestorLayer } from "../ProviderEventIngestor.ts";47+import {48+ layer as providerRuntimeRecoveryLayer,49+ ProviderRuntimeRecoveryService,50+} from "../ProviderRuntimeRecoveryService.ts";
- apps/server/src/orchestration-v2/RestartBackgroundNote.ts lines 62 to 62OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +42,21 @@42+ }43+ default:44+ return undefined;45+ }46+}47+48+/** Describes a provider-reported background task (the provider-thread roster). */49+export function cancelledRosterTaskWork(task: OrchestrationV2PendingBackgroundTask): Work {50+ const type = task.taskType ?? "";51+ const kind =52+ type === "local_bash" || type === "command_execution"53+ ? "shell"54+ : type === "local_agent" || type === "subagent"55+ ? "subagent"56+ : type.includes("monitor")57+ ? "monitor"58+ : "task";59+ const description = compactLabel(task.description);60+ return {61+ kind,62+ label: description === undefined ? task.taskId : `${description} (id ${task.taskId})`,
- apps/server/src/orchestration-v2/RestartBackgroundNote.ts line 141OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +121,21 @@121+ carriesNote(candidate),122+ );123+ return sameThread124+ .filter(125+ (source) =>126+ source.ordinal < input.run.ordinal &&127+ (source.restartCancelledBackgroundWork?.length ?? 0) > 0 &&128+ !prompted.some((later) => later.ordinal > source.ordinal),129+ )130+ .reduce<ReadonlyArray<Work>>(131+ (work, source) =>132+ mergeRestartCancelledBackgroundWork(work, source.restartCancelledBackgroundWork ?? []),133+ [],134+ );135+}136+137+export function mergeRestartCancelledBackgroundWork(138+ current: ReadonlyArray<Work>,139+ added: ReadonlyArray<Work>,140+): ReadonlyArray<Work> {141+ const seen = new Set(current.map((entry) => `${entry.kind}\u0000${entry.label}`));
- apps/server/src/orchestration-v2/RestartBackgroundNote.ts lines 126 to 129Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +102,21 @@102+ readonly compactionMessageIds: ReadonlySet<string>;103+ readonly run: Pick<104+ OrchestrationV2Run,105+ "id" | "ordinal" | "userMessageId" | "providerThreadId" | "restartContinuationOfRunId"106+ >;107+}): ReadonlyArray<Work> {108+ const carriesNote = (run: typeof input.run) =>109+ run.restartContinuationOfRunId === undefined &&110+ !input.compactionMessageIds.has(run.userMessageId);111+ if (input.run.providerThreadId === null || !carriesNote(input.run)) return [];112+ const providerThreadId = input.run.providerThreadId;113+ const deliveredAttemptIds = new Set(114+ input.providerTurns115+ .filter((turn) => turn.providerThreadId === providerThreadId)116+ .map((turn) => turn.runAttemptId),117+ );118+ const sameThread = input.runs.filter((run) => run.providerThreadId === providerThreadId);119+ const prompted = sameThread.filter(120+ (candidate) =>121+ candidate.id !== input.run.id &&122+ candidate.activeAttemptId !== null &&
juliusmarminge merged this39e6fbb
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed