Skip to content

Commit 49a57cd

Browse files
fix(server): clear pending session crons when a Claude turn is interrupted
An interrupted or failed turn never reaches the SDK Stop hook, so the session cron count it last reported stayed set and pinned the idle session after its process (and crons) were gone. Clear the count together with the background-task roster. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9fdecd3 commit 49a57cd

2 files changed

Lines changed: 78 additions & 8 deletions

File tree

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6477,6 +6477,76 @@ describe("ClaudeAdapterV2 background wake turns", () => {
64776477
),
64786478
);
64796479

6480+
it.effect("forgets session crons when the next turn is interrupted before Stop", () =>
6481+
Effect.scoped(
6482+
Effect.gen(function* () {
6483+
const harness = yield* makeWakeHarnessWithOptions({
6484+
close: (sdkMessages) => Queue.shutdown(sdkMessages),
6485+
});
6486+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
6487+
const now = yield* DateTime.now;
6488+
const turn = (ordinal: number, text: string) =>
6489+
makeClaudeTestTurnInput({
6490+
threadId: harness.threadId,
6491+
providerThread: harness.providerThread,
6492+
now,
6493+
attemptId: RunAttemptId.make(`attempt-claude-session-cron-interrupt:${ordinal}`),
6494+
text,
6495+
attachments: [],
6496+
providerTurnOrdinal: ordinal,
6497+
});
6498+
6499+
yield* harness.runtime.startTurn(turn(1, "At 12:20 append the date to a file."));
6500+
yield* Effect.promise(async () => {
6501+
const matchers = harness.getOpenedOptions()?.hooks?.Stop ?? [];
6502+
for (const hook of matchers.flatMap((matcher) => matcher.hooks)) {
6503+
await hook(
6504+
{
6505+
hook_event_name: "Stop",
6506+
session_id: WAKE_NATIVE_SESSION,
6507+
transcript_path: "/tmp/transcript.jsonl",
6508+
cwd: "/tmp",
6509+
stop_hook_active: false,
6510+
background_tasks: [],
6511+
session_crons: [
6512+
{
6513+
id: "4e805017",
6514+
schedule: "20 12 22 10 *",
6515+
recurring: false,
6516+
prompt: "Append `date -u` to /tmp/cron.txt",
6517+
},
6518+
],
6519+
},
6520+
undefined,
6521+
{ signal: new AbortController().signal },
6522+
);
6523+
}
6524+
});
6525+
yield* harness.offerAndWait(turnOneResult);
6526+
yield* harness.awaitTerminal(1);
6527+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
6528+
6529+
// Stop closes the CLI that owns the crons, and the SDK Stop hook never reports them gone.
6530+
const second = turn(2, "Keep going.");
6531+
yield* harness.runtime.startTurn(second);
6532+
yield* harness.runtime.interruptTurn({
6533+
providerThread: harness.providerThread,
6534+
providerTurnId: idAllocator.derive.providerTurn({
6535+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
6536+
nativeTurnId: `turn:${second.attemptId}`,
6537+
}),
6538+
});
6539+
yield* harness.awaitTerminal(2);
6540+
assert.equal(harness.terminalEvents()[1]?.status, "interrupted");
6541+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
6542+
}).pipe(
6543+
Effect.provide(
6544+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
6545+
),
6546+
),
6547+
),
6548+
);
6549+
64806550
it.effect("stops background work after the turn settled", () =>
64816551
Effect.scoped(
64826552
Effect.gen(function* () {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4688,6 +4688,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
46884688
updated.delete(nativeThreadId);
46894689
return updated;
46904690
});
4691+
yield* Ref.update(pendingSessionCronCountByNativeThread, (current) => {
4692+
if (!current.has(nativeThreadId)) {
4693+
return current;
4694+
}
4695+
const updated = new Map(current);
4696+
updated.delete(nativeThreadId);
4697+
return updated;
4698+
});
46914699
yield* endClaudeMonitorTasks((_taskId, task) => task.nativeThreadId === nativeThreadId);
46924700
});
46934701

@@ -4744,14 +4752,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
47444752
const preservedOpaqueTombstones = [...priorOpaqueTombstones].filter((taskId) =>
47454753
bufferedTaskNotificationIds.has(taskId),
47464754
);
4747-
yield* Ref.update(pendingSessionCronCountByNativeThread, (current) => {
4748-
if (!current.has(nativeThreadId)) {
4749-
return current;
4750-
}
4751-
const updated = new Map(current);
4752-
updated.delete(nativeThreadId);
4753-
return updated;
4754-
});
47554755
yield* clearPendingBackgroundTasksForNativeThread(nativeThreadId);
47564756
yield* clearNativeThreadTaskIdSet(
47574757
wakeEligibleBackgroundTasksByNativeThread,

0 commit comments

Comments
 (0)