Skip to content

Commit 953644f

Browse files
fix(server): a cancelled Claude wake settles its subagents and never re-offers itself
A cancelled wake's continuation could still go wrong in three ways. A root tool result dropped while the wake was retired left the subagent it settled kept as running. An empty lifecycle result before the wake's real turn was taken as the wake's end, so the real turn got a second continuation. And a subagent notification kept behind the released wake never got its own offer. The retired branch now drops the settled subagent's start, child frames and report; empty task-notification results neither end the latch nor release a retired one; and both release paths offer a buffered subagent notification. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 959da91 commit 953644f

2 files changed

Lines changed: 317 additions & 64 deletions

File tree

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

Lines changed: 185 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11485,9 +11485,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1148511485
),
1148611486
);
1148711487

11488-
it.effect.each(["notification", "tool result"] as const)(
11489-
"does not replay a subagent that finished inside a cancelled wake (ended by %s)",
11490-
(ending) =>
11488+
it.effect.each([
11489+
{ ending: "notification", cancelled: "after its result" },
11490+
{ ending: "tool result", cancelled: "after its result" },
11491+
{ ending: "tool result", cancelled: "while the subagent ran" },
11492+
] as const)(
11493+
"does not replay a subagent that finished inside a cancelled wake (ended by $ending, cancelled $cancelled)",
11494+
({ ending, cancelled }) =>
1149111495
Effect.scoped(
1149211496
Effect.gen(function* () {
1149311497
const SECOND_TASK_ID = "b2ndwake";
@@ -11538,6 +11542,18 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1153811542
session_id: WAKE_NATIVE_SESSION,
1153911543
}),
1154011544
);
11545+
yield* harness.offerAndWait(
11546+
claudeSdkFrame({
11547+
...makeAssistantTextFrame({
11548+
uuid: "00000000-0000-4000-8000-000000000168",
11549+
text: "Reading the build log.",
11550+
}),
11551+
parent_tool_use_id: SUBAGENT_TOOL_USE_ID,
11552+
}),
11553+
);
11554+
if (cancelled === "while the subagent ran") {
11555+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11556+
}
1154111557
yield* harness.offerAndWait(
1154211558
ending === "notification"
1154311559
? claudeSdkFrame({
@@ -11569,7 +11585,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1156911585
}),
1157011586
);
1157111587
yield* harness.offerAndWait(wakeResult);
11572-
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11588+
if (cancelled === "after its result") {
11589+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11590+
}
1157311591

1157411592
yield* harness.offerAndWait(
1157511593
claudeSdkFrame({
@@ -11637,6 +11655,169 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1163711655
),
1163811656
);
1163911657

11658+
it.effect.each(["before the real turn", "after its first frame"] as const)(
11659+
"offers a cancelled wake once when an empty lifecycle result precedes its turn (cancelled %s)",
11660+
(cancelled) =>
11661+
Effect.scoped(
11662+
Effect.gen(function* () {
11663+
const SECOND_TASK_ID = "b2ndwake";
11664+
const SECOND_TOOL_USE_ID = "toolu_second_wake";
11665+
const harness = yield* makeWakeHarness;
11666+
const now = yield* DateTime.now;
11667+
11668+
yield* harness.runtime.startTurn(
11669+
makeClaudeTestTurnInput({
11670+
threadId: harness.threadId,
11671+
providerThread: harness.providerThread,
11672+
now,
11673+
attemptId: RunAttemptId.make("attempt-claude-wake-empty-lifecycle"),
11674+
text: "Run two builds in the background.",
11675+
attachments: [],
11676+
}),
11677+
);
11678+
yield* harness.offerAndWait(wakeTaskStarted);
11679+
yield* harness.offerAndWait(
11680+
claudeSdkFrame({
11681+
...wakeTaskStarted,
11682+
task_id: SECOND_TASK_ID,
11683+
tool_use_id: SECOND_TOOL_USE_ID,
11684+
uuid: "00000000-0000-4000-8000-000000000171",
11685+
}),
11686+
);
11687+
yield* harness.offerAndWait(turnOneResult);
11688+
yield* harness.awaitTerminal(1);
11689+
11690+
yield* harness.offerAndWait(wakeNotification);
11691+
yield* harness.offerAndWait(wakeTurnInit);
11692+
assert.lengthOf(harness.continuationRequests, 1);
11693+
yield* harness.offerAndWait(
11694+
makeResultFrame({
11695+
uuid: "00000000-0000-4000-8000-000000000172",
11696+
result: "",
11697+
numTurns: 0,
11698+
origin: { kind: "task-notification" },
11699+
terminalReason: null,
11700+
}),
11701+
);
11702+
if (cancelled === "before the real turn") {
11703+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11704+
}
11705+
yield* harness.offerAndWait(
11706+
claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000173" }),
11707+
);
11708+
yield* harness.offerAndWait(wakeAssistant);
11709+
if (cancelled === "after its first frame") {
11710+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11711+
}
11712+
yield* harness.offerAndWait(wakeResult);
11713+
assert.lengthOf(
11714+
harness.continuationRequests,
11715+
1,
11716+
"the cancelled wake must not be offered again",
11717+
);
11718+
11719+
yield* harness.offerAndWait(
11720+
claudeSdkFrame({
11721+
...wakeNotification,
11722+
task_id: SECOND_TASK_ID,
11723+
tool_use_id: SECOND_TOOL_USE_ID,
11724+
output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
11725+
uuid: "00000000-0000-4000-8000-000000000174",
11726+
}),
11727+
);
11728+
yield* harness.offerAndWait(
11729+
claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000175" }),
11730+
);
11731+
assert.lengthOf(harness.continuationRequests, 2, "the next wake must be offered");
11732+
}).pipe(
11733+
Effect.provide(
11734+
Layer.mergeAll(
11735+
IdAllocator.layer,
11736+
McpProviderSessions.layer,
11737+
NodeServices.layer,
11738+
NodeCrypto.layer,
11739+
),
11740+
),
11741+
),
11742+
),
11743+
);
11744+
11745+
it.effect.each([
11746+
"before the cancelled wake's result",
11747+
"after the cancelled wake's result",
11748+
] as const)(
11749+
"offers a subagent's wake that arrives behind a cancelled wake (notified %s)",
11750+
(notified) =>
11751+
Effect.scoped(
11752+
Effect.gen(function* () {
11753+
const SUBAGENT_TASK_ID = "bg-agent";
11754+
const SUBAGENT_TOOL_USE_ID = "toolu-bg-agent";
11755+
const harness = yield* makeWakeHarness;
11756+
const now = yield* DateTime.now;
11757+
11758+
yield* harness.runtime.startTurn(
11759+
makeClaudeTestTurnInput({
11760+
threadId: harness.threadId,
11761+
providerThread: harness.providerThread,
11762+
now,
11763+
attemptId: RunAttemptId.make("attempt-claude-wake-later-subagent"),
11764+
text: "Run a build and an agent in the background.",
11765+
attachments: [],
11766+
}),
11767+
);
11768+
yield* harness.offerAndWait(wakeTaskStarted);
11769+
yield* harness.offerAndWait(
11770+
claudeSdkFrame({
11771+
...wakeTaskStarted,
11772+
task_id: SUBAGENT_TASK_ID,
11773+
tool_use_id: SUBAGENT_TOOL_USE_ID,
11774+
task_type: "local_agent",
11775+
uuid: "00000000-0000-4000-8000-000000000181",
11776+
}),
11777+
);
11778+
yield* harness.offerAndWait(turnOneResult);
11779+
yield* harness.awaitTerminal(1);
11780+
11781+
const subagentNotification = claudeSdkFrame({
11782+
...wakeNotification,
11783+
task_id: SUBAGENT_TASK_ID,
11784+
tool_use_id: SUBAGENT_TOOL_USE_ID,
11785+
output_file: `/tmp/claude-replay/tasks/${SUBAGENT_TASK_ID}.output`,
11786+
summary: "Agent finished",
11787+
uuid: "00000000-0000-4000-8000-000000000182",
11788+
});
11789+
yield* harness.offerAndWait(wakeNotification);
11790+
yield* harness.offerAndWait(wakeTurnInit);
11791+
yield* harness.offerAndWait(wakeAssistant);
11792+
assert.lengthOf(harness.continuationRequests, 1);
11793+
if (notified === "before the cancelled wake's result") {
11794+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11795+
yield* harness.offerAndWait(subagentNotification);
11796+
yield* harness.offerAndWait(wakeResult);
11797+
} else {
11798+
yield* harness.offerAndWait(wakeResult);
11799+
yield* harness.offerAndWait(subagentNotification);
11800+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
11801+
}
11802+
assert.lengthOf(
11803+
harness.continuationRequests,
11804+
2,
11805+
"the subagent's wake must get its own continuation",
11806+
);
11807+
assert.equal(harness.continuationRequests[1]?.detail, "Agent finished");
11808+
}).pipe(
11809+
Effect.provide(
11810+
Layer.mergeAll(
11811+
IdAllocator.layer,
11812+
McpProviderSessions.layer,
11813+
NodeServices.layer,
11814+
NodeCrypto.layer,
11815+
),
11816+
),
11817+
),
11818+
),
11819+
);
11820+
1164011821
it.effect.each([true, false])(
1164111822
"carries a rejected wake rate limit into the continuation failure (event=%s)",
1164211823
(standaloneEvent) =>

0 commit comments

Comments
 (0)