Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): a run no longer stays working after its idle session is released #14507

Closedsaphid(GitHub profile, opens in a new tab)wants to merge 5 commits intot3code/codex-turn-mapping fromrefs/pull/14507/headopened View on GitHub

What Changed

When an idle provider session is released, any run it still owned is now settled. The run, its open tool calls and subagents become cancelled, and a queued run behind it starts normally. Nothing changes for threads whose session is still live.

ProviderSessionManager publishes idleReleases after an idle release has persisted the session as stopped. serverRuntimeStartup subscribes and calls the new ProviderRuntimeRecoveryService.reconcileReleasedSession, which reuses the restart reconciliation with a new session-released trigger. It acts only when every unfinished run on the thread is running and bound to the released session and no other session of the thread is live. It rechecks for a live session with the released id right before committing.

Differences from restart reconciliation: the queue is not held, since the user queued those runs to follow the stranded one, and the command id is not command:runtime-reconcile:*, which terminal-run queue promotion ignores.

Why

Seen on a V2 Claude thread that showed "Working 13h 48m" with the error "Provider session ... is not active". Its Claude process had finished its turn at 11:35Z but the run stayed running with five open tool calls and two subagents that never reported back. The session manager pins an idle session for up to 4 h while background work is pending, then releases it (the session persisted as stopped at 16:11:07Z, the same millisecond as the logged query.close). Nothing settled the run at that point, because only server restart recovery does. From then on steer and stop failed with "session is not active" and the queued run behind it never started.

The subagents never reporting back is not addressed here. This makes the outcome recoverable instead of permanent.

Verification

  • ProviderRuntimeRecoveryService.test.ts: 19 tests passed. New cases settle a stranded run, its nodes and its provider turn while leaving the queued run untouched and using a command id that queue promotion does not skip; and skip a thread when an unfinished run is not owned by the released session, when a run is waiting, when a live session holds the released id again, or when another of its sessions is live.
  • ProviderSessionManager.test.ts: 41 tests passed, including a new case that an idle release announces its session id and attached threads.
  • EffectWorker, ProviderTurnControlService, ProviderRuntimeRecoveryService.regression and serverRuntimeStartup tests also pass. apps/server typechecks clean.
  • Not run against a real provider or client; no UI code changed.

Known limits

  • A waiting run is deliberately left alone: its checkpoint capture can complete concurrently with the reconciliation read.
  • The live-session recheck is not atomic with the commit; the window is the commit itself, and a replacement session moves its run out of running first.
  • If another thread reopens a shared session under the same id before the release is handled, the release is skipped and a stranded thread there is only settled at the next restart.
  • Threads already stuck before this ships still need Stop once the fix is installed, or a server restart.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (no UI code changed)
  • I included a video for animation/interaction changes (n/a)

Diagnosed, implemented and tested by Claude Sonnet 5.5 in Claude Code, running inside T3 Code; reviewed by GPT-6 Astra (its findings on queue promotion and the waiting-run race were fixed in the second commit); Macroscope findings on replacement races and other live sessions were fixed in the third.

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. @@ -526,3 +528,11 @@
      526528 ).pipe(Effect.map((targets): AutoBootstrapWelcomeTargets => targets)),
      527529 });
      528530 yield* Effect.logInfo("V2 orchestration recovery completed", recovery);
      531+ // An idle-released session's process is gone, so a run it still owned
      532+ // (for example one pinned open by background work that never reported
      533+ // back) can never finish. Settle it instead of leaving it working.
      534+ yield* providerSessions.idleReleases.pipe(
      535+ Stream.runForEach(({ providerSessionId, threadIds }) =>
      536+ Effect.gen(function* () {
      537+ // A replacement may have reused the id; its runs are live.
      538+ if (Option.isSome(yield* providerSessions.get(providerSessionId))) return;
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High src/serverRuntimeStartup.ts:538

      reconcileReleasedSession can cancel runs belonging to a live replacement process, causing active work to be terminated. The providerSessions.get(providerSessionId) check at line 538 is separate from reconciliation, so a replacement can reuse the ID after get() returns None but before reconcileReleasedSession reads and commits its projection; keep the incarnation check atomic with reconciliation or recheck immediately before cancellation.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/serverRuntimeStartup.ts around line 538:
      
      `reconcileReleasedSession` can cancel runs belonging to a live replacement process, causing active work to be terminated. The `providerSessions.get(providerSessionId)` check at line 538 is separate from reconciliation, so a replacement can reuse the ID after `get()` returns `None` but before `reconcileReleasedSession` reads and commits its projection; keep the incarnation check atomic with reconciliation or recheck immediately before cancellation.
    3. @@ -527,2 +529,21 @@
      527529 });
      528530 yield* Effect.logInfo("V2 orchestration recovery completed", recovery);
      531+ // An idle-released session's process is gone, so a run it still owned
      532+ // (for example one pinned open by background work that never reported
      533+ // back) can never finish. Settle it instead of leaving it working.
      534+ yield* providerSessions.idleReleases.pipe(
      535+ Stream.runForEach(({ providerSessionId, threadIds }) =>
      536+ Effect.gen(function* () {
      537+ // A replacement may have reused the id; its runs are live.
      538+ if (Option.isSome(yield* providerSessions.get(providerSessionId))) return;
      539+ yield* Effect.forEach(
      540+ threadIds,
      541+ (threadId) =>
      542+ providerRuntimeRecovery.reconcileReleasedSession({ threadId, providerSessionId }),
      543+ { discard: true },
      544+ );
      545+ }).pipe(
      546+ Effect.catchCause((cause) =>
      547+ Effect.logWarning("V2 orchestration idle-release reconciliation failed", {
      548+ providerSessionId,
      549+ cause: Cause.pretty(cause),
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Cause.pretty(cause) can put arbitrary defect text (including sensitive payloads) into a log annotation. Suggest logging a bounded category instead of rendering the cause.

      Suggested change
      549- cause: Cause.pretty(cause),
      549+ failureCategory: Cause.hasInterruptsOnly(cause) ? "interrupted" : "failed",
      Posted via Macroscope — Effect Service Conventions
    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This PR adds event-driven production recovery that cancels stranded runs and changes queue/reconciliation behavior across the session manager, startup lifecycle, and recovery service. Non-atomic session validation and a non-replaying startup subscription leave unresolved race and event-loss risks that require human review.

      Not approved because:

      • 2 blocking correctness issues found at or above your repo's Minimum Blocking Severity

      No code changes detected at 3086779. Prior analysis still applies.

      Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. @@ -526,3 +528,7 @@
      526528 ).pipe(Effect.map((targets): AutoBootstrapWelcomeTargets => targets)),
      527529 });
      528530 yield* Effect.logInfo("V2 orchestration recovery completed", recovery);
      531+ // An idle-released session's process is gone, so a run it still owned
      532+ // (for example one pinned open by background work that never reported
      533+ // back) can never finish. Settle it instead of leaving it working.
      534+ yield* providerSessions.idleReleases.pipe(
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High src/serverRuntimeStartup.ts:534

      A run can remain working until the next server restart when its idle-release event occurs during startEffectWorker or autoBootstrap. Because idleReleases is a non-replaying PubSub and this subscriber is started only afterward, that event is discarded and the run is never reconciled; subscribe before those phases or use a replayable/state-based reconciliation mechanism.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/serverRuntimeStartup.ts around line 534:
      
      A run can remain `working` until the next server restart when its idle-release event occurs during `startEffectWorker` or `autoBootstrap`. Because `idleReleases` is a non-replaying `PubSub` and this subscriber is started only afterward, that event is discarded and the run is never reconciled; subscribe before those phases or use a replayable/state-based reconciliation mechanism.
    6. @@ -528,0 +531,21 @@
      531+ // An idle-released session's process is gone, so a run it still owned
      532+ // (for example one pinned open by background work that never reported
      533+ // back) can never finish. Settle it instead of leaving it working.
      534+ yield* providerSessions.idleReleases.pipe(
      535+ Stream.runForEach(({ providerSessionId, threadIds }) =>
      536+ Effect.forEach(
      537+ threadIds,
      538+ (threadId) =>
      539+ providerRuntimeRecovery
      540+ .reconcileReleasedSession({
      541+ threadId,
      542+ providerSessionId,
      543+ isSessionLive: providerSessions.get(providerSessionId).pipe(
      544+ Effect.map(Option.isSome),
      545+ // When unsure, treat it as live and leave the thread alone.
      546+ Effect.orElseSucceed(() => true),
      547+ ),
      548+ })
      549+ .pipe(
      550+ // One thread failing must not skip the rest of the batch.
      551+ Effect.catchCause((cause) =>
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      The new per-thread failure handling changes backend behavior, but no focused test exercises an idle-release batch where one reconciliation fails. Suggest adding a startup test using service test layers that emits two thread IDs, makes the first reconciliation fail, and verifies the second still runs.

      Posted via Macroscope — Effect Service Conventions
    7. juliusmarminge added labelsmacroscope-review

    8. juliusmarminge closed this

Sign in to join this conversation.