From 67a18d32b4ae3412c09c066ad702b0f69069b912 Mon Sep 17 00:00:00 2001 From: Navid Nadali Date: Mon, 23 Nov 2026 13:10:59 +0000 Subject: [PATCH 1/3] test(server): a compaction or manual continuation keeps its session past the idle timeout The session manager marks a session busy only for a run started through startTurn. A /compact starts through compactThread and a manual continuation through resumeTurn; both pass through the session manager unwrapped, so one that goes the idle timeout without a provider event has its session released under it. With a 1 s idle timeout: a compaction and a manual continuation each keep their session past the timeout and let it idle out after turn.terminal (fail on main), and each that fails to start still lets the session idle out. Co-Authored-By: Claude Opus 5.5 --- .../ProviderSessionManager.test.ts | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index f2f992d8f4ee9..d2a4ea162a874 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -611,6 +611,8 @@ const makeProviderAdapter = Effect.fnUntraced(function* ( resumeCount: current.resumeCount + 1, })).pipe(Effect.as(threadInput.providerThread)), startTurn: () => options.startTurn ?? Effect.void, + resumeTurn: () => options.startTurn ?? Effect.void, + compactThread: () => options.startTurn ?? Effect.void, steerTurn: () => Effect.void, interruptTurn: () => Ref.update(state, (current) => ({ @@ -2137,6 +2139,132 @@ it.effect.each([ }), ); +const idleReleaseAroundRun = (start: "compactThread" | "resumeTurn", startFails: boolean) => + Effect.gen(function* () { + const state = yield* Ref.make(emptyState); + yield* Effect.gen(function* () { + const eventSink = yield* EventSink.EventSinkV2; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const manager = yield* ProviderSessionManager.ProviderSessionManagerV2; + const projectionStore = yield* ProjectionStore.ProjectionStoreV2; + const now = yield* DateTime.now; + const threadId = ThreadId.make(`thread-${start}-outlasts-idle-timeout`); + const providerSessionId = yield* idAllocator.allocate.providerSession({ + providerInstanceId: modelSelection.instanceId, + threadId, + }); + yield* eventSink.write({ + events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })], + }); + const runtime = yield* manager.open({ + threadId, + providerSessionId, + modelSelection, + runtimePolicy, + }); + const subscriber = yield* runtime.subscribeEvents!; + const terminal = yield* subscriber.events.pipe( + Stream.filter((event) => event.type === "turn.terminal"), + Stream.runHead, + Effect.forkScoped, + ); + const providerThread = makeProviderThread({ + idAllocator, + threadId, + providerSessionId, + now, + nativeThreadId: `native-${threadId}`, + }); + const runId = idAllocator.derive.run({ threadId, ordinal: 1 }); + const run = { + appThread: (yield* projectionStore.getThreadProjection(threadId)).thread, + threadId, + runId, + runOrdinal: 1, + providerTurnOrdinal: 1, + attemptId: idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 }), + rootNodeId: idAllocator.derive.rootNode({ runId }), + providerThread, + modelSelection, + runtimePolicy, + }; + const messageId = yield* idAllocator.allocate.message({ threadId, ordinal: 1 }); + const started = yield* Effect.exit( + start === "compactThread" + ? runtime.compactThread!({ + ...run, + message: { + createdBy: "user", + creationSource: "web", + messageId, + text: "/compact", + attachments: [], + }, + }) + : runtime.resumeTurn!({ ...run, messageId }), + ); + assert.equal(Exit.isFailure(started), startFails); + + // A running compaction or continuation can go minutes without a provider event. + yield* TestClock.adjust("2 seconds"); + assert.equal(yield* manager.isLive(providerSessionId), !startFails); + if (startFails) return; + + const queue = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId)); + assert.isDefined(queue); + yield* Queue.offer(queue!, { + type: "turn.terminal", + driver: CODEX_DRIVER, + providerThreadId: providerThread.id, + providerTurnId: idAllocator.derive.providerTurn({ + driver: CODEX_DRIVER, + nativeTurnId: `native-turn-${start}`, + }), + runOrdinal: 1, + status: "completed", + failure: null, + threadDisposition: "reusable", + }); + // The event pump marks the run idle before it publishes the event. + yield* Fiber.join(terminal); + yield* TestClock.adjust("2 seconds"); + assert.isFalse(yield* manager.isLive(providerSessionId)); + }).pipe( + Effect.provide( + layerTest({ + state, + idleTimeoutMs: 1_000, + ...(startFails + ? { + startTurn: Effect.fail( + new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CODEX_DRIVER, + detail: "query ended before the run could start", + }), + ), + } + : {}), + }), + ), + ); + }); + +it.effect.each([ + { run: "compaction", start: "compactThread" }, + { run: "manual continuation", start: "resumeTurn" }, +] as const)( + "ProviderSessionManagerV2 keeps a session through a $run that outlasts the idle timeout", + ({ start }) => idleReleaseAroundRun(start, false), +); + +it.effect.each([ + { run: "compaction", start: "compactThread" }, + { run: "manual continuation", start: "resumeTurn" }, +] as const)( + "ProviderSessionManagerV2 still idles out a session after a $run that failed to start", + ({ start }) => idleReleaseAroundRun(start, true), +); + it.effect.each(["session metadata", "session status changes", "provider work"] as const)( "ProviderSessionManagerV2 expires idle sessions according to %s", (activity) => From eedccbba22cdc814e2ada49990057e50e09e5e22 Mon Sep 17 00:00:00 2001 From: Navid Nadali Date: Mon, 23 Nov 2026 13:13:28 +0000 Subject: [PATCH 2/3] fix(server): a compaction or manual continuation keeps its session busy like a turn decorateRuntime wrapped only startTurn, so compactThread and resumeTurn reached the adapter without marking the session busy, and the idle release (2 minutes since #41174) could close the session under a running /compact or manual continuation. One busyTurn helper now wraps all three: it attaches the thread, marks the session busy for the run until its turn.terminal, and on a failed or interrupted start marks it idle again and forgets the thread's native load, as startTurn already did. The resumed and compacting runs each record their own turn metric (resume, compact). Co-Authored-By: Claude Opus 5.5 --- .../ProviderSessionManager.ts | 164 ++++++++++-------- 1 file changed, 96 insertions(+), 68 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.ts index dc1ae318a4c2a..291ac02272a26 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.ts @@ -2532,6 +2532,80 @@ export const layerWithOptions = ( modelFamily: normalizeModelMetricLabel(model), }, }); + // A start, continuation or compaction keeps the session busy until its turn.terminal. + const busyTurn = ( + input: Pick< + ProviderAdapter.ProviderAdapterV2TurnInput, + "threadId" | "providerThread" | "runOrdinal" | "runId" + >, + run: () => Effect.Effect, + ) => + observeActivity( + providerSessionId, + ensureThreadAttached({ + providerSessionId, + threadId: input.threadId, + providerInstanceId: runtime.instanceId, + }), + ).pipe( + // Failed or stopped starts may omit turn.terminal; clear their turns so sessions can idle. + Effect.andThen( + holdThreadLoaded({ + providerSessionId, + threadId: input.threadId, + providerThread: input.providerThread, + }), + ), + Effect.andThen( + Effect.acquireUseRelease( + markBusy( + providerSessionId, + busyTurnKey(input.providerThread.id, input.runOrdinal), + runtime, + ).pipe( + Effect.catchCause((cause) => + Effect.logWarning("orchestration-v2.driver-session.activity-failed", { + providerSessionId, + cause, + }).pipe(Effect.as(true)), + ), + // A release between attach and markBusy leaves nothing to mark: fail the + // start instead of running it on a runtime being torn down. + Effect.flatMap((marked) => + marked + ? Effect.void + : Effect.fail( + new ProviderAdapter.ProviderAdapterTurnStartError({ + driver: runtime.driver, + threadId: input.threadId, + providerThreadId: input.providerThread.id, + runId: input.runId, + cause: "The provider session was released while the turn started.", + }), + ), + ), + ), + run, + (_, exit) => + Exit.isFailure(exit) + ? observeActivity( + providerSessionId, + markIdle(providerSessionId, input.providerThread.id, input.runOrdinal), + ).pipe( + // A failed start can leave its native thread gone (Claude rejecting a + // resume), so the next start must ask the adapter to resume it again. + Effect.andThen( + forgetProviderThreadLoaded({ + providerSessionId, + threadId: input.threadId, + runtime, + }), + ), + ) + : Effect.void, + ), + ), + ); return { ...runtime, // Pi replaces its session snapshot as metadata changes. @@ -2663,78 +2737,32 @@ export const layerWithOptions = ( Effect.map(({ loaded }) => loaded), ), startTurn: (input) => - observeActivity( - providerSessionId, - ensureThreadAttached({ - providerSessionId, - threadId: input.threadId, - providerInstanceId: runtime.instanceId, - }), - ).pipe( - // Failed or stopped starts may omit turn.terminal; clear their turns so sessions can idle. - Effect.andThen( - holdThreadLoaded({ - providerSessionId, - threadId: input.threadId, - providerThread: input.providerThread, - }), + busyTurn(input, () => + agentFeedbackMode.pipe( + Effect.flatMap((agentFeedback) => runtime.startTurn({ ...input, agentFeedback })), + turnMetrics("send", input.modelSelection.model), ), - Effect.andThen( - Effect.acquireUseRelease( - markBusy( - providerSessionId, - busyTurnKey(input.providerThread.id, input.runOrdinal), - runtime, - ).pipe( - Effect.catchCause((cause) => - Effect.logWarning("orchestration-v2.driver-session.activity-failed", { - providerSessionId, - cause, - }).pipe(Effect.as(true)), - ), - // A release between attach and markBusy leaves nothing to mark: fail the - // start instead of running it on a runtime being torn down. - Effect.flatMap((marked) => - marked - ? Effect.void - : Effect.fail( - new ProviderAdapter.ProviderAdapterTurnStartError({ - driver: runtime.driver, - threadId: input.threadId, - providerThreadId: input.providerThread.id, - runId: input.runId, - cause: "The provider session was released while the turn started.", - }), - ), + ), + ...(runtime.resumeTurn === undefined + ? {} + : { + resumeTurn: (input: ProviderAdapter.ProviderAdapterV2ResumeTurnInput) => + busyTurn(input, () => + runtime.resumeTurn!(input).pipe( + turnMetrics("resume", input.modelSelection.model), ), ), - () => - agentFeedbackMode.pipe( - Effect.flatMap((agentFeedback) => - runtime.startTurn({ ...input, agentFeedback }), - ), - turnMetrics("send", input.modelSelection.model), + }), + ...(runtime.compactThread === undefined + ? {} + : { + compactThread: (input: ProviderAdapter.ProviderAdapterV2TurnInput) => + busyTurn(input, () => + runtime.compactThread!(input).pipe( + turnMetrics("compact", input.modelSelection.model), ), - (_, exit) => - Exit.isFailure(exit) - ? observeActivity( - providerSessionId, - markIdle(providerSessionId, input.providerThread.id, input.runOrdinal), - ).pipe( - // A failed start can leave its native thread gone (Claude rejecting a - // resume), so the next start must ask the adapter to resume it again. - Effect.andThen( - forgetProviderThreadLoaded({ - providerSessionId, - threadId: input.threadId, - runtime, - }), - ), - ) - : Effect.void, - ), - ), - ), + ), + }), steerTurn: (input) => observeActivity(providerSessionId, touchActivity(providerSessionId)).pipe( Effect.andThen(runtime.steerTurn(input).pipe(turnMetrics("steer"))), From 2c147a26c1c9a5b0a0f7540d5875c0ab925bf256 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 14:38:04 +0000 Subject: [PATCH 3/3] test(server): keep only the idle-release cases that fail without the fix The failed-start variants pass on main too; startTurn's own test covers the cleanup path busyTurn shares. Co-Authored-By: Claude Opus 5.5 --- .../ProviderSessionManager.test.ts | 61 +++++-------------- 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index d2a4ea162a874..e6652c5963905 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -2139,7 +2139,7 @@ it.effect.each([ }), ); -const idleReleaseAroundRun = (start: "compactThread" | "resumeTurn", startFails: boolean) => +const idleReleaseAroundRun = (start: "compactThread" | "resumeTurn") => Effect.gen(function* () { const state = yield* Ref.make(emptyState); yield* Effect.gen(function* () { @@ -2189,26 +2189,22 @@ const idleReleaseAroundRun = (start: "compactThread" | "resumeTurn", startFails: runtimePolicy, }; const messageId = yield* idAllocator.allocate.message({ threadId, ordinal: 1 }); - const started = yield* Effect.exit( - start === "compactThread" - ? runtime.compactThread!({ - ...run, - message: { - createdBy: "user", - creationSource: "web", - messageId, - text: "/compact", - attachments: [], - }, - }) - : runtime.resumeTurn!({ ...run, messageId }), - ); - assert.equal(Exit.isFailure(started), startFails); + yield* start === "compactThread" + ? runtime.compactThread!({ + ...run, + message: { + createdBy: "user", + creationSource: "web", + messageId, + text: "/compact", + attachments: [], + }, + }) + : runtime.resumeTurn!({ ...run, messageId }); // A running compaction or continuation can go minutes without a provider event. yield* TestClock.adjust("2 seconds"); - assert.equal(yield* manager.isLive(providerSessionId), !startFails); - if (startFails) return; + assert.isTrue(yield* manager.isLive(providerSessionId)); const queue = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId)); assert.isDefined(queue); @@ -2229,24 +2225,7 @@ const idleReleaseAroundRun = (start: "compactThread" | "resumeTurn", startFails: yield* Fiber.join(terminal); yield* TestClock.adjust("2 seconds"); assert.isFalse(yield* manager.isLive(providerSessionId)); - }).pipe( - Effect.provide( - layerTest({ - state, - idleTimeoutMs: 1_000, - ...(startFails - ? { - startTurn: Effect.fail( - new ProviderAdapter.ProviderAdapterProtocolError({ - driver: CODEX_DRIVER, - detail: "query ended before the run could start", - }), - ), - } - : {}), - }), - ), - ); + }).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1_000 }))); }); it.effect.each([ @@ -2254,15 +2233,7 @@ it.effect.each([ { run: "manual continuation", start: "resumeTurn" }, ] as const)( "ProviderSessionManagerV2 keeps a session through a $run that outlasts the idle timeout", - ({ start }) => idleReleaseAroundRun(start, false), -); - -it.effect.each([ - { run: "compaction", start: "compactThread" }, - { run: "manual continuation", start: "resumeTurn" }, -] as const)( - "ProviderSessionManagerV2 still idles out a session after a $run that failed to start", - ({ start }) => idleReleaseAroundRun(start, true), + ({ start }) => idleReleaseAroundRun(start), ); it.effect.each(["session metadata", "session status changes", "provider work"] as const)(