Skip to content

Commit f474c7f

Browse files
voltcrashcodex
andcommitted
fix(server): preserve tracked Claude transcript receipts and child outcomes
Only reconcile transcript receipts for untracked, non-opaque tasks. Keep tracked-task and shell receipts as native user frames so stale history cannot cancel a resumed agent or suppress wake evidence. Accept untracked terminal outcomes for child threads owned by the active run, using the existing child-thread ownership guard. Add regressions for stale receipts during active and idle turns, shell wake detection, and root/child/foreign outcome routing. Co-Authored-By: Codex <[email protected]>
1 parent c1cdecd commit f474c7f

4 files changed

Lines changed: 175 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6979,6 +6979,116 @@ describe("ClaudeAdapterV2 background wake turns", () => {
69796979
uuid: input.uuid,
69806980
session_id: WAKE_NATIVE_SESSION,
69816981
});
6982+
it.effect.each(["active", "idle"] as const)(
6983+
"keeps a resumed subagent running after a stale transcript receipt while %s",
6984+
(state) =>
6985+
Effect.scoped(
6986+
Effect.gen(function* () {
6987+
const harness = yield* makeWakeHarness;
6988+
const taskId = "task-stale-transcript";
6989+
yield* harness.runtime.startTurn(
6990+
makeClaudeTestTurnInput({
6991+
threadId: harness.threadId,
6992+
providerThread: harness.providerThread,
6993+
now: yield* DateTime.now,
6994+
attemptId: RunAttemptId.make("attempt-stale-transcript"),
6995+
text: "Resume the audit.",
6996+
attachments: [],
6997+
}),
6998+
);
6999+
yield* harness.offerAndWait(
7000+
makeSubagentTaskStartedFrame({
7001+
taskId,
7002+
toolUseId: "toolu-original-audit",
7003+
uuid: "00000000-0000-4000-8000-000000000901",
7004+
}),
7005+
);
7006+
yield* Queue.take(harness.subagentReceipts);
7007+
yield* harness.offerAndWait(
7008+
makeSubagentNotificationFrame({
7009+
taskId,
7010+
toolUseId: "toolu-original-audit",
7011+
summary: "Original audit completed.",
7012+
uuid: "00000000-0000-4000-8000-000000000902",
7013+
}),
7014+
);
7015+
assert.equal((yield* Queue.take(harness.subagentReceipts)).subagent.status, "completed");
7016+
yield* harness.offerAndWait(
7017+
makeSubagentTaskStartedFrame({
7018+
taskId,
7019+
toolUseId: "toolu-resumed-audit",
7020+
uuid: "00000000-0000-4000-8000-000000000903",
7021+
}),
7022+
);
7023+
assert.equal((yield* Queue.take(harness.subagentReceipts)).subagent.status, "running");
7024+
if (state === "idle") {
7025+
yield* harness.offerAndWait(turnOneResult);
7026+
yield* Queue.take(harness.terminalReceipts);
7027+
}
7028+
yield* harness.offerAndWait(
7029+
claudeSdkFrame({
7030+
type: "user",
7031+
isSynthetic: true,
7032+
parent_tool_use_id: null,
7033+
uuid: "00000000-0000-4000-8000-000000000904",
7034+
session_id: WAKE_NATIVE_SESSION,
7035+
message: {
7036+
role: "user",
7037+
content: `<task-notification><task-id>${taskId}</task-id><status>stopped</status><summary>Old task stopped.</summary></task-notification>`,
7038+
},
7039+
}),
7040+
);
7041+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
7042+
}).pipe(
7043+
Effect.provide(
7044+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7045+
),
7046+
),
7047+
),
7048+
);
7049+
7050+
it.effect("uses a known shell's transcript receipt as native wake evidence", () =>
7051+
Effect.scoped(
7052+
Effect.gen(function* () {
7053+
const harness = yield* makeWakeHarness;
7054+
yield* harness.runtime.startTurn(
7055+
makeClaudeTestTurnInput({
7056+
threadId: harness.threadId,
7057+
providerThread: harness.providerThread,
7058+
now: yield* DateTime.now,
7059+
attemptId: RunAttemptId.make("attempt-shell-transcript-wake"),
7060+
text: "Run the build in the background.",
7061+
attachments: [],
7062+
}),
7063+
);
7064+
yield* harness.offerAndWait(wakeTaskStarted);
7065+
yield* harness.offerAndWait(turnOneResult);
7066+
yield* Queue.take(harness.terminalReceipts);
7067+
yield* harness.offerAndWait(wakeNotification);
7068+
assert.lengthOf(harness.continuationRequests, 0);
7069+
yield* harness.offerAndWait(
7070+
claudeSdkFrame({
7071+
type: "user",
7072+
isSynthetic: true,
7073+
parent_tool_use_id: null,
7074+
uuid: "00000000-0000-4000-8000-000000000905",
7075+
session_id: WAKE_NATIVE_SESSION,
7076+
message: {
7077+
role: "user",
7078+
content: `<task-notification><task-id>${WAKE_TASK_ID}</task-id><status>completed</status><summary>${WAKE_SUMMARY}</summary></task-notification>`,
7079+
},
7080+
}),
7081+
);
7082+
assert.lengthOf(harness.continuationRequests, 1);
7083+
assert.equal(harness.continuationRequests[0]?.threadId, harness.threadId);
7084+
}).pipe(
7085+
Effect.provide(
7086+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7087+
),
7088+
),
7089+
),
7090+
);
7091+
69827092
const subagentRouting = (
69837093
events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>,
69847094
nativeToolIds: ReadonlyArray<string>,

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7814,7 +7814,36 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
78147814
readonly message: SDKMessage;
78157815
}) {
78167816
const notification = claudeTaskNotificationFromTranscript(rawInput.message);
7817-
const input = notification === null ? rawInput : { ...rawInput, message: notification };
7817+
let input = rawInput;
7818+
if (notification !== null) {
7819+
const liveQuery = yield* Ref.get(queryContext);
7820+
if (liveQuery?.query === rawInput.query) {
7821+
const registered = (yield* Ref.get(sessionSubagentsByTaskId)).has(
7822+
notification.task_id,
7823+
);
7824+
const hasBufferedStart =
7825+
(yield* Ref.get(wakeBuffers))
7826+
.get(liveQuery.nativeThreadId)
7827+
?.messages.some(
7828+
(message) =>
7829+
message.type === "system" &&
7830+
message.subtype === "task_started" &&
7831+
message.task_id === notification.task_id,
7832+
) ?? false;
7833+
// Transcript history cannot end a task this process resumed. Keep
7834+
// tracked task and shell receipts as native user wake frames.
7835+
if (
7836+
!registered &&
7837+
!hasBufferedStart &&
7838+
!(yield* isKnownOpaqueBackgroundTaskOnNativeThread(
7839+
liveQuery.nativeThreadId,
7840+
notification.task_id,
7841+
))
7842+
) {
7843+
input = { ...rawInput, message: notification };
7844+
}
7845+
}
7846+
}
78187847
// Progress or a notification can be the first frame naming a known
78197848
// subagent's tool_use_id; its held frames must precede the result.
78207849
if (input.message.type !== "system" || input.message.subtype !== "task_started") {

‎apps/server/src/orchestration-v2/RunExecutionService.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,40 @@ it.effect("routes shared-runtime events only to their owning root run", () =>
165165
}),
166166
);
167167

168+
it("routes untracked subagent outcomes for the root and owned children only", () => {
169+
const identity: RunExecutionService.ProviderEventRouteIdentity = {
170+
threadId: ThreadId.make("thread:orphaned-root"),
171+
runId: RunId.make("run:orphaned-root"),
172+
attemptId: RunAttemptId.make("attempt:orphaned-root"),
173+
providerThreadId: ProviderThreadId.make("provider-thread:orphaned-root"),
174+
};
175+
const childThreadId = ThreadId.make("thread:orphaned-child");
176+
const state = RunExecutionService.makeProviderEventRoutingState({
177+
identity,
178+
providerTurnId: null,
179+
relatedThreadIds: [childThreadId],
180+
});
181+
const outcome = (threadId: ThreadId): ProviderAdapter.ProviderAdapterV2Event => ({
182+
type: "subagent.ended",
183+
driver,
184+
threadId,
185+
subagentId: NodeId.make("node:orphaned-task"),
186+
status: "cancelled",
187+
result: "Previous process ended.",
188+
});
189+
assert.isTrue(
190+
RunExecutionService.routeProviderEvent(outcome(identity.threadId), identity, state)[0],
191+
);
192+
assert.isTrue(RunExecutionService.routeProviderEvent(outcome(childThreadId), identity, state)[0]);
193+
assert.isFalse(
194+
RunExecutionService.routeProviderEvent(
195+
outcome(ThreadId.make("thread:unrelated")),
196+
identity,
197+
state,
198+
)[0],
199+
);
200+
});
201+
168202
it("routes only enrolled workflow members after the root turn ends", () => {
169203
const threadId = ThreadId.make("thread:late-child");
170204
const rootProviderTurnId = ProviderTurnId.make("provider-turn:late-child");

‎apps/server/src/orchestration-v2/RunExecutionService.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ export function routeProviderEvent(
443443
return [true, { ...state, workflowMemberParents }];
444444
}
445445
case "subagent.ended":
446-
return [event.threadId === input.threadId, state];
446+
return [event.threadId === input.threadId || ownsChildThread(event.threadId), state];
447447
case "message.updated":
448448
return [ownsRun(event.message.runId) || ownsChildThread(event.message.threadId), state];
449449
case "turn_item.updated": {

0 commit comments

Comments
 (0)