Skip to content

Commit dbb7aed

Browse files
fix(server): a failed Claude turn no longer pins the session on a stale cron count
Scheduled work pins an idle session with no cap, but a turn that fails ends through StopFailure, which reports no crons. The count from the turn before then outlived a CronDelete and kept an empty Claude process alive forever. Each turn now drops the previous count when it starts, so only its own Stop hook can pin the session. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d2a15a8 commit dbb7aed

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12379,7 +12379,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1237912379
),
1238012380
);
1238112381

12382-
it.effect("keeps session crons pinned when the next turn fails without closing the CLI", () =>
12382+
it.effect("drops a session cron count the failed next turn did not confirm", () =>
1238312383
Effect.scoped(
1238412384
Effect.gen(function* () {
1238512385
const harness = yield* makeWakeHarness;
@@ -12401,8 +12401,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1240112401
yield* harness.awaitTerminal(1);
1240212402
assert.isTrue(yield* scheduledWork(harness));
1240312403

12404-
// A usage limit fails the turn through StopFailure, which carries no crons, and the
12405-
// CLI keeps running with the job still scheduled.
12404+
// A usage limit fails the turn through StopFailure, which carries no crons, so a
12405+
// CronDelete earlier in the turn would leave the old count pinning the session forever.
1240612406
yield* harness.runtime.startTurn(turn(2, "Keep going."));
1240712407
yield* harness.offerAndWait(
1240812408
makeResultFrame({
@@ -12415,9 +12415,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1241512415
);
1241612416
yield* harness.awaitTerminal(2);
1241712417
assert.equal(harness.terminalEvents()[1]?.status, "failed");
12418-
assert.isTrue(
12418+
assert.isFalse(
1241912419
yield* scheduledWork(harness),
12420-
"a failed turn must not forget crons the live CLI still holds",
12420+
"a failed turn must not keep a cron count it never reported",
1242112421
);
1242212422
}).pipe(
1242312423
Effect.provide(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6386,8 +6386,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
63866386
yield* endClaudeMonitorTasks((_taskId, task) => task.nativeThreadId === nativeThreadId);
63876387
});
63886388

6389-
// Session crons live in the CLI process: only its exit or replacement drops them. A failed
6390-
// result (usage limit, overload) leaves the process and its crons running.
6389+
// Session crons pin the session with no cap, so never keep a count the latest turn did not
6390+
// report: a failed turn (usage limit, overload) ends through StopFailure, which has none.
63916391
const clearSessionCronCountForNativeThread = (nativeThreadId: string) =>
63926392
Ref.update(pendingSessionCronCountByNativeThread, (current) => {
63936393
if (!current.has(nativeThreadId)) return current;
@@ -12393,6 +12393,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1239312393
reason: "previous_turn_active",
1239412394
});
1239512395
}
12396+
// Only this turn's Stop hook may report crons; a failed turn ends without one.
12397+
yield* clearSessionCronCountForNativeThread(nativeThreadId);
1239612398
yield* Ref.update(lastTurnRouteByNativeThread, (current) => {
1239712399
const updated = new Map(current);
1239812400
updated.set(nativeThreadId, {

0 commit comments

Comments
 (0)