Skip to content

Commit 9750c44

Browse files
committed
fix(server): keep Claude background work alive past idle pin limit
1 parent 22b5252 commit 9750c44

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,48 @@ it.effect(
24902490
}),
24912491
);
24922492

2493+
it.effect.each([true, false])(
2494+
"ProviderSessionManagerV2 only expires background work's idle pin when it can survive process exit (process-bound: %s)",
2495+
(backgroundTasksEndWithProcess) =>
2496+
Effect.gen(function* () {
2497+
const state = yield* Ref.make(emptyState);
2498+
const pending = yield* Ref.make(true);
2499+
const effect = Effect.gen(function* () {
2500+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
2501+
const { providerSessionId, rosters } = yield* openSessionWithBackgroundCommand(
2502+
"provider-session-manager-background-pin",
2503+
);
2504+
2505+
yield* TestClock.adjust("5 hours");
2506+
2507+
assert.equal(yield* manager.isLive(providerSessionId), backgroundTasksEndWithProcess);
2508+
assert.equal((yield* Ref.get(state)).closeCount, backgroundTasksEndWithProcess ? 0 : 1);
2509+
assert.deepStrictEqual(yield* rosters, [["dev-server"], ["dev-server"]]);
2510+
2511+
yield* Ref.set(pending, false);
2512+
yield* TestClock.adjust("30 minutes");
2513+
2514+
assert.isFalse(yield* manager.isLive(providerSessionId));
2515+
assert.equal((yield* Ref.get(state)).closeCount, 1);
2516+
assert.deepStrictEqual(yield* rosters, [
2517+
backgroundTasksEndWithProcess ? [] : ["dev-server"],
2518+
["dev-server"],
2519+
]);
2520+
});
2521+
2522+
yield* effect.pipe(
2523+
Effect.provide(
2524+
layerTest({
2525+
state,
2526+
idleTimeoutMs: 30 * 60_000,
2527+
backgroundTasksEndWithProcess,
2528+
hasPendingBackgroundWork: Ref.get(pending),
2529+
}),
2530+
),
2531+
);
2532+
}),
2533+
);
2534+
24932535
it.effect("ProviderSessionManagerV2 keeps the roster of a runtime whose tasks can outlive it", () =>
24942536
Effect.gen(function* () {
24952537
const state = yield* Ref.make(emptyState);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,11 @@ export const layerWithOptions = (
11371137
if (hasPendingWork) {
11381138
const now = yield* Clock.currentTimeMillis;
11391139
const pinnedSinceMs = entry.pinnedSinceMs ?? now;
1140-
if (now - pinnedSinceMs < maxIdlePinMs) {
1140+
// Process-bound work cannot survive release, even when it has stopped emitting progress.
1141+
if (
1142+
probedRuntime.backgroundTasksEndWithProcess === true ||
1143+
now - pinnedSinceMs < maxIdlePinMs
1144+
) {
11411145
const shouldContinuePin = yield* Ref.modify(sessions, (latest) => {
11421146
const latestEntry = latest.get(key);
11431147
if (
@@ -1200,10 +1204,8 @@ export const layerWithOptions = (
12001204
),
12011205
);
12021206

1203-
// Provider activity restarts the pin budget, so background work that keeps
1204-
// reporting progress (a long Claude workflow) is never cut off; only work
1205-
// that went silent for maxIdlePinMs is released. This runs for every
1206-
// provider event, so keep it untraced.
1207+
// Provider activity restarts the pin budget for work that can outlive the process.
1208+
// This runs for every provider event, so keep it untraced.
12071209
const scheduleIdleReleaseInternal = Effect.fnUntraced(function* (
12081210
providerSessionId: ProviderSessionId,
12091211
options?: { readonly providerActivity?: boolean },

0 commit comments

Comments
 (0)