Skip to content

Commit f8c2707

Browse files
fix(server): pending Claude session crons no longer block a provider switch
Session crons are not shown to clients, so counting them in hasPendingBackgroundWork made the Orchestrator's detach guard refuse a provider switch with no Stop control to clear it. Crons now report through a separate hasScheduledWork probe that only idle release reads. AI-assisted; verified with the tests and tools listed above.
1 parent 1804d02 commit f8c2707

5 files changed

Lines changed: 75 additions & 20 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10754,6 +10754,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1075410754
);
1075510755
}
1075610756
});
10757+
const scheduledWork = (harness: Effect.Success<typeof makeWakeHarness>) =>
10758+
harness.runtime.hasScheduledWork ?? Effect.succeed(false);
1075710759
const pendingSessionCron: SessionCronSummary = {
1075810760
id: "4e805017",
1075910761
schedule: "20 12 22 10 *",
@@ -10782,15 +10784,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1078210784
yield* harness.offerAndWait(turnOneResult);
1078310785
yield* harness.awaitTerminal(1);
1078410786
assert.isTrue(
10785-
yield* harness.hasPendingBackgroundWork,
10787+
yield* scheduledWork(harness),
1078610788
"a pending session cron must keep the idle session alive",
1078710789
);
10790+
// Crons are not shown to clients, so they must not block a provider switch.
10791+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
1078810792

1078910793
yield* harness.runtime.startTurn(turn(2, "Cancel that job."));
1079010794
yield* runStopHook(harness, []);
1079110795
yield* harness.offerAndWait(turnOneResult);
1079210796
yield* harness.awaitTerminal(2);
10793-
assert.isFalse(yield* harness.hasPendingBackgroundWork);
10797+
assert.isFalse(yield* scheduledWork(harness));
1079410798
}).pipe(
1079510799
Effect.provide(
1079610800
Layer.mergeAll(
@@ -10827,7 +10831,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1082710831
yield* runStopHook(harness, [pendingSessionCron]);
1082810832
yield* harness.offerAndWait(turnOneResult);
1082910833
yield* harness.awaitTerminal(1);
10830-
assert.isTrue(yield* harness.hasPendingBackgroundWork);
10834+
assert.isTrue(yield* scheduledWork(harness));
1083110835

1083210836
// Stop closes the CLI that owns the crons, and the SDK Stop hook never reports them gone.
1083310837
const second = turn(2, "Keep going.");
@@ -10841,7 +10845,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1084110845
});
1084210846
yield* harness.awaitTerminal(2);
1084310847
assert.equal(harness.terminalEvents()[1]?.status, "interrupted");
10844-
assert.isFalse(yield* harness.hasPendingBackgroundWork);
10848+
assert.isFalse(yield* scheduledWork(harness));
1084510849
}).pipe(
1084610850
Effect.provide(
1084710851
Layer.mergeAll(
@@ -10875,7 +10879,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1087510879
yield* runStopHook(harness, [pendingSessionCron]);
1087610880
yield* harness.offerAndWait(turnOneResult);
1087710881
yield* harness.awaitTerminal(1);
10878-
assert.isTrue(yield* harness.hasPendingBackgroundWork);
10882+
assert.isTrue(yield* scheduledWork(harness));
1087910883

1088010884
// A usage limit fails the turn through StopFailure, which carries no crons, and the
1088110885
// CLI keeps running with the job still scheduled.
@@ -10892,7 +10896,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1089210896
yield* harness.awaitTerminal(2);
1089310897
assert.equal(harness.terminalEvents()[1]?.status, "failed");
1089410898
assert.isTrue(
10895-
yield* harness.hasPendingBackgroundWork,
10899+
yield* scheduledWork(harness),
1089610900
"a failed turn must not forget crons the live CLI still holds",
1089710901
);
1089810902
}).pipe(

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11919,14 +11919,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1191911919
return true;
1192011920
}
1192111921
}
11922-
// A session cron fires only in the live process, so releasing it would drop the job.
11923-
for (const cronCount of (yield* Ref.get(
11924-
pendingSessionCronCountByNativeThread,
11925-
)).values()) {
11926-
if (cronCount > 0) {
11927-
return true;
11928-
}
11929-
}
1193011922
const buffers = yield* Ref.get(wakeBuffers);
1193111923
for (const entry of buffers.values()) {
1193211924
if (
@@ -11942,6 +11934,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1194211934
}
1194311935
return false;
1194411936
}),
11937+
// A session cron fires only in the live process, so releasing it would drop the job.
11938+
hasScheduledWork: Ref.get(pendingSessionCronCountByNativeThread).pipe(
11939+
Effect.map((counts) => [...counts.values()].some((count) => count > 0)),
11940+
),
1194511941
// Untraced: the run's event stream calls this once per provider event.
1194611942
hasPendingBackgroundWorkForThread: Effect.fnUntraced(function* (providerThread, options) {
1194711943
const nativeThreadId = providerThread.nativeThreadRef?.nativeId;

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
434434
}) => Effect.Effect<void>;
435435
readonly hasPendingBackgroundWork?: Effect.Effect<boolean>;
436436
readonly hasPendingBackgroundWorkForThread?: Effect.Effect<boolean>;
437+
readonly hasScheduledWork?: Effect.Effect<boolean>;
437438
readonly backgroundTasksEndWithProcess?: boolean;
438439
readonly hangSessionScopeClose?: boolean;
439440
readonly startTurn?: Effect.Effect<void>;
@@ -533,6 +534,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
533534
...(options.hasPendingBackgroundWork === undefined
534535
? {}
535536
: { hasPendingBackgroundWork: options.hasPendingBackgroundWork }),
537+
...(options.hasScheduledWork === undefined
538+
? {}
539+
: { hasScheduledWork: options.hasScheduledWork }),
536540
...(options.hasPendingBackgroundWorkForThread === undefined
537541
? {}
538542
: {
@@ -596,6 +600,7 @@ function layerTest(input: {
596600
};
597601
readonly hasPendingBackgroundWork?: Effect.Effect<boolean>;
598602
readonly hasPendingBackgroundWorkForThread?: Effect.Effect<boolean>;
603+
readonly hasScheduledWork?: Effect.Effect<boolean>;
599604
readonly backgroundTasksEndWithProcess?: boolean;
600605
readonly hangSessionScopeClose?: boolean;
601606
readonly startTurn?: Effect.Effect<void>;
@@ -628,6 +633,7 @@ function layerTest(input: {
628633
...(input.hasPendingBackgroundWork === undefined
629634
? {}
630635
: { hasPendingBackgroundWork: input.hasPendingBackgroundWork }),
636+
...(input.hasScheduledWork === undefined ? {} : { hasScheduledWork: input.hasScheduledWork }),
631637
...(input.hasPendingBackgroundWorkForThread === undefined
632638
? {}
633639
: { hasPendingBackgroundWorkForThread: input.hasPendingBackgroundWorkForThread }),
@@ -2952,6 +2958,52 @@ it.effect("ProviderSessionManagerV2 keeps a provider update that lands while rel
29522958
}),
29532959
);
29542960

2961+
it.effect("ProviderSessionManagerV2 keeps an idle session alive while it has scheduled work", () =>
2962+
Effect.gen(function* () {
2963+
const state = yield* Ref.make(emptyState);
2964+
const scheduled = yield* Ref.make(true);
2965+
yield* Effect.gen(function* () {
2966+
const eventSink = yield* EventSink.EventSinkV2;
2967+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2968+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
2969+
const projectId = yield* idAllocator.allocate.project({
2970+
fixtureName: "provider-session-manager-scheduled-work",
2971+
});
2972+
const threadId = yield* idAllocator.allocate.thread({
2973+
fixtureName: "provider-session-manager-scheduled-work",
2974+
projectId,
2975+
});
2976+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2977+
providerInstanceId: modelSelection.instanceId,
2978+
threadId,
2979+
});
2980+
yield* eventSink.write({
2981+
events: [
2982+
yield* makeThreadCreatedEvent({ idAllocator, threadId, now: yield* DateTime.now }),
2983+
],
2984+
});
2985+
const runtime = yield* manager.open({
2986+
threadId,
2987+
providerSessionId,
2988+
modelSelection,
2989+
runtimePolicy,
2990+
});
2991+
yield* runtime.events.pipe(Stream.runDrain, Effect.forkScoped);
2992+
2993+
yield* TestClock.adjust("1 second");
2994+
assert.isTrue(yield* manager.isLive(providerSessionId));
2995+
2996+
yield* Ref.set(scheduled, false);
2997+
yield* TestClock.adjust("1 second");
2998+
assert.isFalse(yield* manager.isLive(providerSessionId));
2999+
}).pipe(
3000+
Effect.provide(
3001+
layerTest({ state, idleTimeoutMs: 1000, hasScheduledWork: Ref.get(scheduled) }),
3002+
),
3003+
);
3004+
}),
3005+
);
3006+
29553007
it.effect(
29563008
"ProviderSessionManagerV2 does not idle-release a session that turns busy during the pending-work check",
29573009
() =>

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,12 +1190,13 @@ export const layerWithOptions = (
11901190
}
11911191
// Capture runtime identity before yielding; replacements can reuse the session id.
11921192
const probedRuntime = entry.runtime;
1193-
const hasPendingWork =
1194-
probedRuntime.hasPendingBackgroundWork === undefined
1195-
? false
1196-
: yield* probedRuntime.hasPendingBackgroundWork.pipe(
1197-
Effect.catchCause(() => Effect.succeed(false)),
1198-
);
1193+
const hasPendingWork = yield* Effect.forEach(
1194+
[probedRuntime.hasPendingBackgroundWork, probedRuntime.hasScheduledWork],
1195+
(probe) =>
1196+
probe === undefined
1197+
? Effect.succeed(false)
1198+
: probe.pipe(Effect.catchCause(() => Effect.succeed(false))),
1199+
).pipe(Effect.map((results) => results.includes(true)));
11991200
if (hasPendingWork) {
12001201
const now = yield* Clock.currentTimeMillis;
12011202
const pinnedSinceMs = entry.pinnedSinceMs ?? now;

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ export interface ProviderAdapterV2SessionRuntime {
634634
/** Manager runtimes broadcast independent subscriptions; adapters may expose only a single-consumer stream. */
635635
readonly subscribeEvents?: Effect.Effect<ProviderAdapterV2EventSubscription>;
636636
readonly hasPendingBackgroundWork?: Effect.Effect<boolean>;
637+
/** Scheduled jobs that keep an idle session alive but never block a detach the user asked for. */
638+
readonly hasScheduledWork?: Effect.Effect<boolean>;
637639
/** Root-subscription probe; replacement checks opt in to subagents owned by the live process. */
638640
readonly hasPendingBackgroundWorkForThread?: (
639641
providerThread: OrchestrationV2ProviderThread,

0 commit comments

Comments
 (0)