Skip to content

Commit 935cd82

Browse files
fix(server): a cancelled Claude wake's lifecycle result no longer re-offers it
offerBufferedWake counted the CLI's empty task-notification success as a new wake, so cancelling a finished wake queued it again. Retirement now also compare-and-sets the wake buffer so a concurrent process reset is not undone. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0e1801f commit 935cd82

2 files changed

Lines changed: 82 additions & 14 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14473,6 +14473,62 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1447314473
),
1447414474
);
1447514475

14476+
it.effect("does not offer a cancelled wake again for its trailing empty lifecycle result", () =>
14477+
Effect.scoped(
14478+
Effect.gen(function* () {
14479+
const harness = yield* makeWakeHarness;
14480+
const now = yield* DateTime.now;
14481+
14482+
yield* harness.runtime.startTurn(
14483+
makeClaudeTestTurnInput({
14484+
threadId: harness.threadId,
14485+
providerThread: harness.providerThread,
14486+
now,
14487+
attemptId: RunAttemptId.make("attempt-claude-wake-trailing-lifecycle"),
14488+
text: "Run a build in the background.",
14489+
attachments: [],
14490+
}),
14491+
);
14492+
yield* harness.offerAndWait(wakeTaskStarted);
14493+
yield* harness.offerAndWait(turnOneResult);
14494+
yield* harness.awaitTerminal(1);
14495+
14496+
yield* harness.offerAndWait(wakeNotification);
14497+
yield* harness.offerAndWait(wakeTurnInit);
14498+
yield* harness.offerAndWait(wakeAssistant);
14499+
yield* harness.offerAndWait(wakeResult);
14500+
yield* harness.offerAndWait(
14501+
claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000176" }),
14502+
);
14503+
yield* harness.offerAndWait(
14504+
makeResultFrame({
14505+
uuid: "00000000-0000-4000-8000-000000000177",
14506+
result: "",
14507+
numTurns: 0,
14508+
origin: { kind: "task-notification" },
14509+
terminalReason: null,
14510+
}),
14511+
);
14512+
assert.lengthOf(harness.continuationRequests, 1);
14513+
yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14514+
assert.lengthOf(
14515+
harness.continuationRequests,
14516+
1,
14517+
"a lifecycle result after the cancelled wake must not open another continuation",
14518+
);
14519+
}).pipe(
14520+
Effect.provide(
14521+
Layer.mergeAll(
14522+
IdAllocator.layer,
14523+
McpProviderSessions.layer,
14524+
NodeServices.layer,
14525+
NodeCrypto.layer,
14526+
),
14527+
),
14528+
),
14529+
),
14530+
);
14531+
1447614532
it.effect.each([
1447714533
"before the cancelled wake's result",
1447814534
"after the cancelled wake's result",

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9210,7 +9210,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92109210
) {
92119211
const latch = (yield* Ref.get(requestedContinuations)).get(nativeThreadId);
92129212
if (latch?.token !== token) return;
9213-
const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
9213+
const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages;
9214+
const messages = buffered ?? [];
92149215
const endIndex =
92159216
latch.end === undefined ? messages.length - 1 : messages.indexOf(latch.end);
92169217
const retired = messages.slice(0, endIndex + 1);
@@ -9233,7 +9234,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92339234
),
92349235
...later,
92359236
];
9236-
yield* replaceWakeBuffer(nativeThreadId, kept, retiredTaskIds);
9237+
if (!(yield* replaceWakeBuffer(nativeThreadId, buffered, kept, retiredTaskIds))) return;
92379238
if (latch.end === undefined) {
92389239
// A lifecycle result closes the root frames before it without opening the wake's turn.
92399240
const sinceLifecycleResult = retired.slice(
@@ -9258,18 +9259,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92589259
: new Map(current).set(nativeThreadId, { ...latch, retired });
92599260
});
92609261

9262+
// Compare-and-set: a process reset clears the buffer without the SDK message lock, and must not be undone.
92619263
const replaceWakeBuffer = Effect.fnUntraced(function* (
92629264
nativeThreadId: string,
9265+
expected: ReadonlyArray<SDKMessage> | undefined,
92639266
messages: ReadonlyArray<SDKMessage>,
92649267
droppedTaskIds: ReadonlySet<string>,
92659268
) {
9266-
yield* Ref.update(wakeBuffers, (current) => {
9269+
const replaced = yield* Ref.modify(wakeBuffers, (current) => {
9270+
if (current.get(nativeThreadId)?.messages !== expected)
9271+
return [false, current] as const;
92679272
const updated = new Map(current);
92689273
if (messages.length === 0) updated.delete(nativeThreadId);
92699274
else
92709275
updated.set(nativeThreadId, { messages, detail: claudeWakeBufferDetail(messages) });
9271-
return updated;
9276+
return [true, updated] as const;
92729277
});
9278+
if (!replaced) return false;
92739279
yield* Ref.update(wakeReportsByNativeThread, ({ userTurns, reports }) => {
92749280
const threadReports = reports.get(nativeThreadId);
92759281
if (threadReports === undefined) return { userTurns, reports };
@@ -9281,6 +9287,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92819287
else updatedReports.set(nativeThreadId, remaining);
92829288
return { userTurns, reports: updatedReports };
92839289
});
9290+
return true;
92849291
});
92859292

92869293
// A retired wake's dropped tool results still settle the subagents it kept for later frames.
@@ -9289,7 +9296,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92899296
settledToolUseIds: ReadonlySet<string>,
92909297
) {
92919298
if (settledToolUseIds.size === 0) return;
9292-
const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
9299+
const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages;
9300+
const messages = buffered ?? [];
92939301
const settledTaskIds = new Set(
92949302
messages.flatMap((entry) =>
92959303
entry.type === "system" &&
@@ -9308,21 +9316,25 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93089316
);
93099317
});
93109318
if (kept.length === messages.length) return;
9311-
yield* replaceWakeBuffer(nativeThreadId, kept, settledTaskIds);
9319+
yield* replaceWakeBuffer(nativeThreadId, buffered, kept, settledTaskIds);
93129320
});
93139321

93149322
// After a retired wake releases its latch, a wake buffered behind it has had no offer of its own.
93159323
const offerBufferedWake = Effect.fnUntraced(function* (nativeThreadId: string) {
93169324
const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
93179325
const reports = (yield* Ref.get(wakeReportsByNativeThread)).reports.get(nativeThreadId);
9318-
const hasWake = messages.some(
9319-
(entry) =>
9320-
isClaudeRootTurnFrame(entry) ||
9321-
entry.type === "result" ||
9322-
(entry.type === "system" &&
9323-
entry.subtype === "task_notification" &&
9324-
reports?.get(entry.task_id)?.report.kind === "subagent"),
9325-
);
9326+
// A lifecycle result ends no turn, and closes the root frames before it.
9327+
const hasWake =
9328+
messages
9329+
.slice(messages.findLastIndex(isClaudeEmptyTaskNotificationSuccess) + 1)
9330+
.some(isClaudeRootTurnFrame) ||
9331+
messages.some(
9332+
(entry) =>
9333+
(entry.type === "result" && !isClaudeEmptyTaskNotificationSuccess(entry)) ||
9334+
(entry.type === "system" &&
9335+
entry.subtype === "task_notification" &&
9336+
reports?.get(entry.task_id)?.report.kind === "subagent"),
9337+
);
93269338
if (hasWake) yield* offerWakeContinuation(nativeThreadId);
93279339
});
93289340

0 commit comments

Comments
 (0)