Skip to content

Commit d06173b

Browse files
jieyuexingcodex
andcommitted
fix(server): serialize dropped Claude wakes with SDK frame handling
Serialize dropped wake outcomes with SDK frame handling and ignore notifications superseded by a newer task lifecycle. Preserve a restarted subagent until its new notification arrives, including when its start races with buffer removal. Co-Authored-By: Codex (gpt-6.1-sol) <[email protected]>
1 parent 710f1ea commit d06173b

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9450,6 +9450,104 @@ describe("ClaudeAdapterV2 background wake turns", () => {
94509450
),
94519451
);
94529452

9453+
it.effect.each(["buffered", "during drop"] as const)(
9454+
"a dropped wake does not end a subagent that has started again (%s)",
9455+
(startTiming) =>
9456+
Effect.scoped(
9457+
Effect.gen(function* () {
9458+
const harness = yield* makeWakeHarness;
9459+
const taskId = "task-drop-resumed";
9460+
const started = (uuid: string) =>
9461+
makeSubagentTaskStartedFrame({ taskId, toolUseId: uuid, uuid });
9462+
const notification = (uuid: string) =>
9463+
makeSubagentNotificationFrame({ taskId, toolUseId: uuid, uuid, summary: uuid });
9464+
yield* harness.runtime.startTurn(
9465+
makeClaudeTestTurnInput({
9466+
threadId: harness.threadId,
9467+
providerThread: harness.providerThread,
9468+
now: yield* DateTime.now,
9469+
attemptId: RunAttemptId.make("attempt-drop-resumed"),
9470+
text: "Start a background subagent.",
9471+
attachments: [],
9472+
}),
9473+
);
9474+
yield* harness.offerAndWait(started("drop-resumed-start"));
9475+
yield* Queue.take(harness.subagentReceipts);
9476+
yield* harness.offerAndWait(
9477+
makeResultFrame({ uuid: "drop-resumed-root-result", result: "Working in background." }),
9478+
);
9479+
yield* Queue.take(harness.terminalReceipts);
9480+
yield* harness.offerAndWait(notification("drop-resumed-old-end"));
9481+
const oldWake = yield* Queue.take(harness.continuationReceipts);
9482+
assert.isDefined(oldWake.clearIfCurrent);
9483+
9484+
// SendMessage resumes the same task while its earlier outcome still
9485+
// waits for the worker to drop the wake.
9486+
if (startTiming === "buffered") {
9487+
yield* harness.offerAndWait(started("drop-resumed-new-start"));
9488+
yield* oldWake.clearIfCurrent!();
9489+
} else {
9490+
const clock = yield* Clock.Clock;
9491+
const paused = yield* Deferred.make<void>();
9492+
const resume = yield* Deferred.make<void>();
9493+
// The first timestamp read in drop occurs after its atomic buffer
9494+
// removal, before it publishes the subagent's terminal state.
9495+
const dropping = yield* oldWake.clearIfCurrent!().pipe(
9496+
Effect.provideService(Clock.Clock, {
9497+
...clock,
9498+
currentTimeMillis: Deferred.succeed(paused, undefined).pipe(
9499+
Effect.andThen(Deferred.await(resume)),
9500+
Effect.andThen(clock.currentTimeMillis),
9501+
),
9502+
}),
9503+
Effect.forkScoped,
9504+
);
9505+
yield* Deferred.await(paused);
9506+
const offered = yield* Deferred.make<void>();
9507+
const restarting = yield* harness
9508+
.offerAndWait(started("drop-resumed-new-start"), offered)
9509+
.pipe(Effect.forkScoped);
9510+
yield* Deferred.await(offered);
9511+
// Let the SDK handler attempt the start while the drop is paused.
9512+
// The shared lock makes it wait until the outcome has been applied.
9513+
yield* Effect.yieldNow;
9514+
yield* Deferred.succeed(resume, undefined);
9515+
yield* Fiber.join(dropping);
9516+
yield* Fiber.join(restarting);
9517+
}
9518+
assert.isTrue(
9519+
yield* harness.hasPendingBackgroundWork,
9520+
"the newer start remains running after the older wake is dropped",
9521+
);
9522+
if (startTiming === "buffered") {
9523+
assert.isFalse(
9524+
harness.events.some(
9525+
(event) =>
9526+
event.type === "subagent.updated" &&
9527+
event.subagent.result === "drop-resumed-old-end",
9528+
),
9529+
"the superseded notification is not projected",
9530+
);
9531+
} else {
9532+
yield* Queue.take(harness.subagentReceipts);
9533+
}
9534+
9535+
yield* harness.offerAndWait(notification("drop-resumed-new-end"));
9536+
const newWake = yield* Queue.take(harness.continuationReceipts);
9537+
assert.isDefined(newWake.clearIfCurrent);
9538+
yield* newWake.clearIfCurrent!();
9539+
const ended = yield* Queue.take(harness.subagentReceipts);
9540+
assert.equal(ended.subagent.result, "drop-resumed-new-end");
9541+
assert.equal(ended.subagent.status, "completed");
9542+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
9543+
}).pipe(
9544+
Effect.provide(
9545+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9546+
),
9547+
),
9548+
),
9549+
);
9550+
94539551
it.effect("ends buffered subagents when the worker drops their wake continuation", () =>
94549552
Effect.scoped(
94559553
Effect.gen(function* () {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6146,6 +6146,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
61466146
// Claude turn will drain the buffer, so the subagents it reports end
61476147
// here, through the turn that settled last, and the next wake can
61486148
// request a continuation again. Claude's own session keeps the wake.
6149+
// Use the SDK frame lock through outcome application: a new start
6150+
// must not slip between buffer removal and the registry update.
61496151
const dropBufferedWake = Effect.fnUntraced(function* (
61506152
nativeThreadId: string,
61516153
generation: number,
@@ -6164,11 +6166,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
61646166
const settled = settledTurnByNativeThread.get(nativeThreadId);
61656167
if (settled === undefined) return;
61666168
const subagents = yield* Ref.get(sessionSubagentsByTaskId);
6169+
const latestLifecycle = new Map<string, SDKMessage>();
6170+
for (const message of buffered) {
6171+
if (
6172+
message.type === "system" &&
6173+
(message.subtype === "task_started" || message.subtype === "task_notification")
6174+
) {
6175+
latestLifecycle.set(message.task_id, message);
6176+
}
6177+
}
61676178
for (const message of buffered) {
61686179
if (
61696180
message.type !== "system" ||
61706181
message.subtype !== "task_notification" ||
6171-
!subagents.has(message.task_id)
6182+
!subagents.has(message.task_id) ||
6183+
latestLifecycle.get(message.task_id) !== message
61726184
) {
61736185
continue;
61746186
}
@@ -6181,7 +6193,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
61816193
status: claudeTaskOutcome(message.status),
61826194
});
61836195
}
6184-
});
6196+
}, sdkMessageLock.withPermits(1));
61856197

61866198
const bufferWakeMessage = Effect.fnUntraced(function* (wakeInput: {
61876199
readonly nativeThreadId: string;

0 commit comments

Comments
 (0)