Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(pi): a Pi session is not released as idle while an async pi-subagents run is working #41649

Mergedchenxin-yan(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/41649/headmerged by juliusmarminge View on GitHub

Problem

A Pi session with an async pi-subagents run still working is released 30 minutes after its last turn. The release terminates the Pi process, and the run dies with it. The next message opens a new session and pi-subagents restarts the workflow from the beginning.

The runtime already tracks open async runs (piAsyncRuns) and sets backgroundTasksEndWithProcess: true, but its probes report pending work only for a queued wake, so ProviderSessionManager.releaseIfStillIdle sees nothing to wait for.

Change

packages/provider-pi/src/server/adapter.ts, the two probes of the runtime:

  • hasPendingBackgroundWork is also true while any tracked async run is open. This is the probe the idle release reads, so the release is deferred until the run settles, within the existing maxIdlePinMs budget.
  • hasPendingBackgroundWorkForThread counts those runs only when the caller passes includeSubagents: true, the way the Claude adapter does. The default call stays the root-subscription probe, so RunExecutionService behaves as before; the replacement check in ProviderTurnStartService, which opts in, no longer treats a session with a working run as free to replace.

Nothing else changes. A run is dropped from piAsyncRuns when it settles, which is when the probes go back to false.

Scope

Bug fix: work in progress is killed. Fixes #41648.

The review of #36599 raised these probes and left them out of that PR as a session-lifetime change. This PR is that change on its own, limited to the two probes.

Verification

How I found it: three async pi-subagents runs were killed about 30 minutes after the turn that started them. The server trace shows ProviderSessionManager.releaseIfStillIdle → PiRpc.terminatePiProcess → AgentScope.stop for the thread 30 minutes after PiAdapterV2.openSession, on 0.1.6-nightly.20261031.3956 (details in the issue).

On main @ 0fd87d3c:

  • I added assertions to the existing test "shows the members of an async pi-subagents workflow as running subagents". Before the fix it fails at the first one: with the run open, runtime.hasPendingBackgroundWork is false (AssertionError: expected false to be true, adapter.test.ts:3869).
  • With the fix: that test passes; the probes are true while the run is open (for its own thread, not for another one), the default thread probe stays false, and both go false once the run settles.
  • vp test run in packages/provider-pi: 7 files, 249 tests pass. effect-tsgo tsrs --noEmit in the package exits 0. The pre-commit hook (vp fmt, vp lint, knip:check) passes.
  • apps/server ProviderSessionManager.test.ts: 79 tests pass (no server file changes).

Not checked: an end-to-end run on a build of main (a real Pi session left idle for 30 minutes with a run working), and the rest of the apps/server suites.

Model and harness: anthropic/claude-fable-5-2 in pi 1.6.0.

    1. Pushed3d7e688

    2. Evidence

      Real Linux web/server comparison: Pi 1.6.0, pi-subagents 0.90.0, openai/gpt-6-luna, fresh databases, 1280×800 CSS viewport, light theme. Both sides used the same launch prompt and a real 40-minute async worker (sleep 2400); clocks and idle timeouts were unchanged. Leave the thread untouched through the automatic four-minute notice and the following 30-minute idle interval, then ask for status without restarting work.

      BeforeAfter
      b5e1f91cba3d7e688d8d
      Before: async runner disappeared before writing a resultAfter: the 40-minute async run completed successfully
      61-second recording, 40× playback · Continuous original61-second recording, 40× playback · Continuous original

      Base released the session at 22:10:40 UTC and interrupted the worker; its native runner was dead. Head deferred idle release, completed at 22:16:36 UTC, and returned IDLE_SURVIVAL_COMPLETE. After settlement, head released normally at 22:47:10 UTC, 30 minutes after its final turn.

      Launch driver · Reproduction and environment · Lifecycle observer · Lifecycle capture · Native process observer · Native process capture · Post-settlement sessions

      Not shown:

      • The four-hour maximum idle-pin expiration was not exercised because it exceeds the 100-minute budget.
      • Automatic workflow relaunch was not exercised; the follow-up explicitly prohibited restarting work. A replacement Pi runtime and the failed prior run were verified.
    3. juliusmarminge added labelstakeover:ready

    4. juliusmarminge removed labelstakeover:in-progress

    5. juliusmarminge merged this67c442a

    6. juliusmarminge closed this

    7. juliusmarminge removed labelstakeover:ready

Sign in to join this conversation.