Skip to content

Commit b022f0d

Browse files
fix(server): failed wake continuation dispatch no longer drops later Claude wakes
When the continuation service failed to dispatch a plain (non-delegated) wake, for example on a full disk, it logged and dropped the request without calling clearIfCurrent. Claude's wake offer had no clearIfCurrent anyway, so its per-thread latch stayed set and every later wake on that CLI process was silently ignored. Muse had the same leak. The dispatch-failed handler now calls clearIfCurrent for non-delegated requests. Claude's offer supplies one that releases its latch only while that request still owns it, so a stale failure after a process reset cannot release a newer wake's latch and enqueue a duplicate run. ACP and Pi already self-clear on dispatch failure, so the extra call does nothing for them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3449f71 commit b022f0d

3 files changed

Lines changed: 103 additions & 6 deletions

File tree

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10310,6 +10310,89 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1031010310
),
1031110311
);
1031210312

10313+
it.effect("keeps a newer wake latched when a stale continuation request is cleared", () =>
10314+
Effect.scoped(
10315+
Effect.gen(function* () {
10316+
const SECOND_TASK_ID = "b2ndwake";
10317+
const SECOND_TOOL_USE_ID = "toolu_second_wake";
10318+
const harness = yield* makeWakeHarness;
10319+
const now = yield* DateTime.now;
10320+
10321+
yield* harness.runtime.startTurn(
10322+
makeClaudeTestTurnInput({
10323+
threadId: harness.threadId,
10324+
providerThread: harness.providerThread,
10325+
now,
10326+
attemptId: RunAttemptId.make("attempt-claude-wake-stale-clear-1"),
10327+
text: "Run two builds in the background.",
10328+
attachments: [],
10329+
}),
10330+
);
10331+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
10332+
yield* Queue.offer(
10333+
harness.sdkMessages,
10334+
claudeSdkFrame({
10335+
...wakeTaskStarted,
10336+
task_id: SECOND_TASK_ID,
10337+
tool_use_id: SECOND_TOOL_USE_ID,
10338+
description: "Second sleep",
10339+
uuid: "00000000-0000-4000-8000-000000000131",
10340+
}),
10341+
);
10342+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
10343+
yield* harness.awaitTerminal(1);
10344+
10345+
yield* harness.offerAndWait(wakeNotification);
10346+
yield* harness.offerAndWait(wakeTurnInit);
10347+
assert.lengthOf(harness.continuationRequests, 1);
10348+
yield* harness.runtime.startTurn(
10349+
makeClaudeTestTurnInput({
10350+
threadId: harness.threadId,
10351+
providerThread: harness.providerThread,
10352+
now,
10353+
attemptId: RunAttemptId.make("attempt-claude-wake-stale-clear-2"),
10354+
text: "Background task completed.",
10355+
attachments: [],
10356+
providerTurnOrdinal: 2,
10357+
messageCreatedBy: "agent",
10358+
messageCreationSource: "provider",
10359+
}),
10360+
);
10361+
yield* harness.offerAndWait(wakeAssistant);
10362+
yield* harness.offerAndWait(wakeResult);
10363+
yield* harness.awaitTerminal(2);
10364+
10365+
yield* harness.offerAndWait(
10366+
claudeSdkFrame({
10367+
...wakeNotification,
10368+
task_id: SECOND_TASK_ID,
10369+
tool_use_id: SECOND_TOOL_USE_ID,
10370+
output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
10371+
uuid: "00000000-0000-4000-8000-000000000133",
10372+
}),
10373+
);
10374+
yield* harness.offerAndWait(
10375+
claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000134" }),
10376+
);
10377+
assert.lengthOf(harness.continuationRequests, 2);
10378+
10379+
yield* harness.continuationRequests[0]!.clearIfCurrent!();
10380+
yield* harness.offerAndWait(
10381+
claudeSdkFrame({ ...wakeAssistant, uuid: "00000000-0000-4000-8000-000000000135" }),
10382+
);
10383+
assert.lengthOf(
10384+
harness.continuationRequests,
10385+
2,
10386+
"clearing the first wake's request must not release the second wake's latch",
10387+
);
10388+
}).pipe(
10389+
Effect.provide(
10390+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
10391+
),
10392+
),
10393+
),
10394+
);
10395+
1031310396
it.effect.each([true, false])(
1031410397
"carries a rejected wake rate limit into the continuation failure (event=%s)",
1031510398
(standaloneEvent) =>

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,7 +4969,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
49694969
return { kind: "command", label, outcome };
49704970
}
49714971
});
4972-
const requestedContinuations = yield* Ref.make(new Set<string>());
4972+
const requestedContinuations = yield* Ref.make(new Map<string, symbol>());
49734973
const heldProposedPlansByToolUseId = new Map<string, string>();
49744974
const runtimeContext = yield* Effect.context<never>();
49754975
const runPromise = Effect.runPromiseWith(runtimeContext);
@@ -5421,7 +5421,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
54215421
if (!current.has(nativeThreadId)) {
54225422
return current;
54235423
}
5424-
const updated = new Set(current);
5424+
const updated = new Map(current);
54255425
updated.delete(nativeThreadId);
54265426
return updated;
54275427
});
@@ -7964,12 +7964,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
79647964
});
79657965
return;
79667966
}
7967+
const nativeThreadId = wakeInput.nativeThreadId;
7968+
const latch = Symbol(nativeThreadId);
79677969
const shouldOffer = yield* Ref.modify(requestedContinuations, (current) => {
7968-
if (current.has(wakeInput.nativeThreadId)) {
7970+
if (current.has(nativeThreadId)) {
79697971
return [false, current] as const;
79707972
}
7971-
const updated = new Set(current);
7972-
updated.add(wakeInput.nativeThreadId);
7973+
const updated = new Map(current);
7974+
updated.set(nativeThreadId, latch);
79737975
return [true, updated] as const;
79747976
});
79757977
if (!shouldOffer) {
@@ -7992,6 +7994,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
79927994
driver: CLAUDE_PROVIDER,
79937995
detail,
79947996
...(notification === null ? {} : { notification }),
7997+
// A process reset can clear and re-latch this thread; a stale failure must not release the newer wake.
7998+
clearIfCurrent: () =>
7999+
Ref.update(requestedContinuations, (current) => {
8000+
if (current.get(nativeThreadId) !== latch) {
8001+
return current;
8002+
}
8003+
const updated = new Map(current);
8004+
updated.delete(nativeThreadId);
8005+
return updated;
8006+
}),
79958007
});
79968008
});
79978009

@@ -10575,7 +10587,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1057510587
yield* attachTurn;
1057610588
const drained = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
1057710589
yield* Ref.update(requestedContinuations, (current) => {
10578-
const updated = new Set(current);
10590+
const updated = new Map(current);
1057910591
updated.delete(nativeThreadId);
1058010592
return updated;
1058110593
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ export const layer = Layer.effectDiscard(
275275
),
276276
Effect.forkScoped,
277277
);
278+
} else if (request.clearIfCurrent !== undefined) {
279+
yield* request.clearIfCurrent();
278280
}
279281
}),
280282
),

0 commit comments

Comments
 (0)