Skip to content

Commit 0a26c94

Browse files
fix(server): an empty Claude roster level no longer finalizes a delegated child early
The previous guard only kept an exited background command published when its task_notification was buffered. Claude's empty background_tasks_changed level can arrive before or after that notification (the SDK leaves the order unspecified), and it published the empty roster straight away. While idle, a roster update now keeps any published task Claude still owes a wake run (wake-eligible or tombstoned). Only the published roster keeps it, so query replacement still sees the cleared internal roster; the wake run's finalize, an interrupted turn, or a process reset publishes it away. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e362ada commit 0a26c94

2 files changed

Lines changed: 109 additions & 86 deletions

File tree

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

Lines changed: 89 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6255,7 +6255,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
62556255
);
62566256

62576257
it.effect(
6258-
"uses task_started as an incremental roster fallback and clears on empty snapshot",
6258+
"uses task_started as an incremental roster fallback and clears the internal roster on empty snapshot",
62596259
() =>
62606260
Effect.scoped(
62616261
Effect.gen(function* () {
@@ -6317,15 +6317,16 @@ describe("ClaudeAdapterV2 background wake turns", () => {
63176317
),
63186318
);
63196319

6320-
yield* Queue.offer(harness.sdkMessages, emptyRoster);
6321-
yield* harness.until(() =>
6322-
providerThreadRosterEvents(harness.events).some(
6323-
(event) =>
6324-
event.providerThread.status === "idle" &&
6325-
(event.providerThread.pendingBackgroundTasks?.length ?? 0) === 0,
6326-
),
6327-
);
6320+
yield* harness.offerAndWait(emptyRoster);
63286321
assert.isFalse(yield* harness.hasPendingBackgroundWork);
6322+
// The exited command still owes its wake run, so the published roster keeps it.
6323+
assert.include(
6324+
(
6325+
providerThreadRosterEvents(harness.events).at(-1)?.providerThread
6326+
.pendingBackgroundTasks ?? []
6327+
).map((task) => task.taskId),
6328+
WAKE_TASK_ID,
6329+
);
63296330
}).pipe(
63306331
Effect.provide(
63316332
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -7652,75 +7653,89 @@ describe("ClaudeAdapterV2 background wake turns", () => {
76527653

76537654
// #24297: an empty roster lets the orchestrator finalize a delegated child on its pre-wake
76547655
// result, and a finalized task never reopens for the wake run that follows.
7655-
it.effect("keeps an exited background command on the roster until its wake run starts", () =>
7656-
Effect.scoped(
7657-
Effect.gen(function* () {
7658-
const harness = yield* makeWakeHarness;
7659-
const now = yield* DateTime.now;
7660-
const wakeAttemptId = RunAttemptId.make("attempt-claude-wake-roster-2");
7656+
// The SDK leaves the empty level's order against the notification unspecified.
7657+
it.effect.each(["none", "before", "after"] as const)(
7658+
"keeps an exited background command on the roster until its wake run starts (empty level %s notification)",
7659+
(emptyLevel) =>
7660+
Effect.scoped(
7661+
Effect.gen(function* () {
7662+
const harness = yield* makeWakeHarness;
7663+
const now = yield* DateTime.now;
7664+
const wakeAttemptId = RunAttemptId.make("attempt-claude-wake-roster-2");
7665+
const emptyRoster = claudeSdkFrame({
7666+
type: "system",
7667+
subtype: "background_tasks_changed",
7668+
tasks: [],
7669+
uuid: "00000000-0000-4000-8000-000000000204",
7670+
session_id: WAKE_NATIVE_SESSION,
7671+
});
76617672

7662-
yield* harness.runtime.startTurn(
7663-
makeClaudeTestTurnInput({
7664-
threadId: harness.threadId,
7665-
providerThread: harness.providerThread,
7666-
now,
7667-
attemptId: RunAttemptId.make("attempt-claude-wake-roster-1"),
7668-
text: "Run the build in the background.",
7669-
attachments: [],
7670-
}),
7671-
);
7672-
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
7673-
yield* Queue.offer(harness.sdkMessages, turnOneResult);
7674-
yield* harness.awaitTerminal(1);
7675-
yield* harness.offerAndWait(wakeNotification);
7676-
yield* harness.offerAndWait(wakeTurnInit);
7677-
yield* harness.runtime.startTurn(
7678-
makeClaudeTestTurnInput({
7679-
threadId: harness.threadId,
7680-
providerThread: harness.providerThread,
7681-
now,
7682-
attemptId: wakeAttemptId,
7683-
text: "Background task completed.",
7684-
attachments: [],
7685-
providerTurnOrdinal: 2,
7686-
messageCreatedBy: "agent",
7687-
messageCreationSource: "provider",
7688-
}),
7689-
);
7690-
yield* harness.offerAndWait(wakeAssistant);
7691-
yield* harness.offerAndWait(wakeResult);
7692-
yield* harness.awaitTerminal(2);
7673+
yield* harness.runtime.startTurn(
7674+
makeClaudeTestTurnInput({
7675+
threadId: harness.threadId,
7676+
providerThread: harness.providerThread,
7677+
now,
7678+
attemptId: RunAttemptId.make("attempt-claude-wake-roster-1"),
7679+
text: "Run the build in the background.",
7680+
attachments: [],
7681+
}),
7682+
);
7683+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
7684+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
7685+
yield* harness.awaitTerminal(1);
7686+
if (emptyLevel === "before") yield* harness.offerAndWait(emptyRoster);
7687+
yield* harness.offerAndWait(wakeNotification);
7688+
if (emptyLevel === "after") yield* harness.offerAndWait(emptyRoster);
7689+
// Query replacement reads the internal roster, which the exit already cleared.
7690+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
7691+
yield* harness.offerAndWait(wakeTurnInit);
7692+
yield* harness.runtime.startTurn(
7693+
makeClaudeTestTurnInput({
7694+
threadId: harness.threadId,
7695+
providerThread: harness.providerThread,
7696+
now,
7697+
attemptId: wakeAttemptId,
7698+
text: "Background task completed.",
7699+
attachments: [],
7700+
providerTurnOrdinal: 2,
7701+
messageCreatedBy: "agent",
7702+
messageCreationSource: "provider",
7703+
}),
7704+
);
7705+
yield* harness.offerAndWait(wakeAssistant);
7706+
yield* harness.offerAndWait(wakeResult);
7707+
yield* harness.awaitTerminal(2);
76937708

7694-
const wakeStart = harness.events.findIndex(
7695-
(event) =>
7696-
event.type === "provider_turn.updated" &&
7697-
event.providerTurn.runAttemptId === wakeAttemptId,
7698-
);
7699-
assert.isAbove(wakeStart, -1);
7700-
const rosters = harness.events.flatMap((event, index) =>
7701-
event.type === "provider_thread.updated" &&
7702-
event.providerThread.id === harness.providerThread.id
7703-
? [
7704-
{
7705-
beforeWake: index < wakeStart,
7706-
taskIds: (event.providerThread.pendingBackgroundTasks ?? []).map(
7707-
(task) => task.taskId,
7708-
),
7709-
},
7710-
]
7711-
: [],
7712-
);
7713-
assert.deepEqual(
7714-
rosters.filter((roster) => roster.beforeWake).map((roster) => roster.taskIds),
7715-
rosters.filter((roster) => roster.beforeWake).map(() => [WAKE_TASK_ID]),
7716-
);
7717-
assert.deepEqual(rosters.at(-1)?.taskIds, []);
7718-
}).pipe(
7719-
Effect.provide(
7720-
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7709+
const wakeStart = harness.events.findIndex(
7710+
(event) =>
7711+
event.type === "provider_turn.updated" &&
7712+
event.providerTurn.runAttemptId === wakeAttemptId,
7713+
);
7714+
assert.isAbove(wakeStart, -1);
7715+
const rosters = harness.events.flatMap((event, index) =>
7716+
event.type === "provider_thread.updated" &&
7717+
event.providerThread.id === harness.providerThread.id
7718+
? [
7719+
{
7720+
beforeWake: index < wakeStart,
7721+
taskIds: (event.providerThread.pendingBackgroundTasks ?? []).map(
7722+
(task) => task.taskId,
7723+
),
7724+
},
7725+
]
7726+
: [],
7727+
);
7728+
assert.deepEqual(
7729+
rosters.filter((roster) => roster.beforeWake).map((roster) => roster.taskIds),
7730+
rosters.filter((roster) => roster.beforeWake).map(() => [WAKE_TASK_ID]),
7731+
);
7732+
assert.deepEqual(rosters.at(-1)?.taskIds, []);
7733+
}).pipe(
7734+
Effect.provide(
7735+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7736+
),
77217737
),
77227738
),
7723-
),
77247739
);
77257740

77267741
it.effect.each([true, false])(

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4563,6 +4563,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
45634563
readonly nativeThreadId: string;
45644564
readonly providerThread: OrchestrationV2ProviderThread;
45654565
readonly status?: OrchestrationV2ProviderThread["status"];
4566+
readonly owedWakeTasks?: ReadonlyArray<OrchestrationV2PendingBackgroundTask>;
45664567
}) {
45674568
const roster = rosterForNativeThread(
45684569
yield* Ref.get(pendingBackgroundTasksByNativeThread),
@@ -4573,7 +4574,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
45734574
...input.providerThread,
45744575
providerSessionId: session.id,
45754576
...(input.status === undefined ? {} : { status: input.status }),
4576-
pendingBackgroundTasks: claudePendingBackgroundTasksFromRoster(roster),
4577+
pendingBackgroundTasks: [
4578+
...claudePendingBackgroundTasksFromRoster(roster),
4579+
...(input.owedWakeTasks ?? []).filter((task) => !roster.has(task.taskId)),
4580+
],
45774581
...(goalsByNativeThread.has(input.nativeThreadId)
45784582
? { goal: goalsByNativeThread.get(input.nativeThreadId) ?? null }
45794583
: {}),
@@ -7350,24 +7354,27 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
73507354
if (!rosterChanged) {
73517355
return false;
73527356
}
7353-
// An idle notification held for its wake run stays published until that run (or the
7354-
// next turn) ends: an empty roster now lets a delegated child finalize on its pre-wake
7355-
// result. finalizeActiveTurn publishes the cleared roster.
7356-
if (
7357-
input.activeContext === null &&
7358-
message.type === "system" &&
7359-
message.subtype === "task_notification" &&
7360-
(yield* Ref.get(wakeBuffers)).get(input.nativeThreadId)?.messages.includes(message)
7361-
) {
7362-
return true;
7363-
}
73647357

73657358
const baseThread =
73667359
input.activeContext?.input.providerThread ??
73677360
(yield* Ref.get(lastProviderThreadByNativeThread)).get(input.nativeThreadId);
73687361
if (baseThread === undefined) {
73697362
return true;
73707363
}
7364+
// While idle, a published task Claude still owes a wake run stays published after the
7365+
// edge or level drops it (their order is unspecified): an empty roster lets a delegated
7366+
// child finalize on its pre-wake result. Only the published roster keeps it, so it never
7367+
// blocks query replacement; the wake run's finalize or a process reset publishes it away.
7368+
const owedWakeTasks: OrchestrationV2PendingBackgroundTask[] = [];
7369+
if (input.activeContext === null) {
7370+
for (const task of baseThread.pendingBackgroundTasks ?? []) {
7371+
if (
7372+
yield* isKnownOpaqueBackgroundTaskOnNativeThread(input.nativeThreadId, task.taskId)
7373+
) {
7374+
owedWakeTasks.push(task);
7375+
}
7376+
}
7377+
}
73717378

73727379
const status =
73737380
input.activeContext === null
@@ -7379,6 +7386,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
73797386
nativeThreadId: input.nativeThreadId,
73807387
providerThread: baseThread,
73817388
status,
7389+
owedWakeTasks,
73827390
});
73837391
return true;
73847392
});

0 commit comments

Comments
 (0)