Skip to content

fix(sessions): do not treat a session with delegated work as idle - #998

Closed
HoloNova wants to merge 2 commits into
agegr:mainfrom
HoloNova:pr/delegated-busy
Closed

HoloNova wants to merge 2 commits into
agegr:mainfrom
HoloNova:pr/delegated-busy

Conversation

@HoloNova

@HoloNova HoloNova commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

What

isRunning() only describes a wrapper's own turn — a pending prompt, streaming
output, a compaction, a shell command. A delegated child keeps working after the
parent turn has returned, so a session with a live async subagent looked idle:
the idle shutdown could close the wrapper and kill an in-process child with it.
GET /api/agent/running had the same hole, and so anything that waits for a
quiet service — a nightly restart guard, for instance — could skip a busy host.

isBusy() now means "own turn or delegated work", and every path that closes
or reports a session asks that instead of isRunning().

How

Two read-only signals, and no plugin-specific state:

  • Built-in subagent runs are asked directly — a run whose parent is this
    session and whose status is starting, queued or running. Exact, in
    process, no filesystem involved.
  • Any other child run is found by its own session file. Every child writes
    under <session file without .jsonl>/, whichever plugin spawned it, so a
    recent write anywhere in that subtree means a child is alive. Bounded in depth
    and entry count with a 10 s cache, reading nothing but mtimes. The window is
    PI_WEB_DELEGATED_ACTIVITY_WINDOW_MS (default 3 min, clamped to 10 s – 30 min).

The filesystem signal is a heuristic and is documented as one: a child that
writes nothing for longer than the window is invisible to it. The other
direction fails closed — an unreadable directory, or a scan that had to stop
early (entry or depth limit), reports busy, because holding a session a
little longer is recoverable and killing a child mid-run is not.

The idle timer treats delegated work exactly as it treats a running turn: the
window starts over. Nothing closes a session the moment a child exits, so a
result on its way back to its parent — the notification that triggers the
parent's next turn — always lands. Reclaiming an idle session sooner than its
idle window is a memory-policy decision, not the idle timer's.

Behaviour change (normal mode, not Lite-mode code)

  • the idle shutdown waits for delegated children instead of closing them;
  • GET /api/agent/running reports a session with a live child, so a caller that
    waits for a quiet service no longer skips it;
  • hasBusyRpcSessionForCwd() uses the same predicate.

A running turn is still never interrupted, and nothing here kills a process.

Verified

node --experimental-strip-types --test lib/delegated-work.test.mjs lib/rpc-manager-delegated-reclaim.test.mjs lib/rpc-manager.test.mjs

42 tests pass, including the new cases: a child writing under the artifact
directory keeps the session busy, the window restarts while it writes, a session
whose child went quiet is reclaimed on the following window (not before), a
running turn is never interrupted, a truncated scan counts as busy, a busy
answer is reused for its short TTL, and a quiet answer is measured again on
every call. A wider
pass over the agent/subagent/session suites: 94 and 21 tests pass.
tsc --noEmit and eslint clean.

The probe cache is one-sided

Only a busy answer is cached (10 s). A quiet answer is measured again on every
call, and it clears any entry for that key. The asymmetry is the point: caching
a quiet answer would open a window in which a child that started just after the
probe is invisible, and closing the parent then kills a live child. A busy
answer that outlives its cause only keeps a session alive a little longer. The
scan itself stays bounded and still fails closed on an unreadable or truncated
tree.

isRunning() only described a wrapper's own turn, so a session whose async
subagent was still working looked idle: the idle shutdown could close it and
kill an in-process child with it. /api/agent/running had the same hole, so
anything that waits for a quiet service — a nightly restart guard, for
instance — could skip a busy host.

isBusy() now means "own turn or delegated work". Delegated work is read from
two read-only signals: a built-in subagent run whose parent is this session,
and recent writes under <session file without .jsonl>/ — the directory every
child run writes its own session into, whichever plugin spawned it. An
incomplete scan counts as busy.

The idle timer treats delegated work exactly as it treats a running turn: the
window starts over rather than the session being closed the moment a child
exits, so a result on its way back to the parent always lands.
A quiet answer was cached for ten seconds just like a busy one, so a child that
started right after an idle probe stayed invisible until the entry expired. The
asymmetry matters: a busy that outlives its cause only keeps a session alive a
little longer, while a quiet that outlives its cause can have a live child
reclaimed with its parent.

Only the busy answer is reused now; a quiet answer is measured again on every
call, and it clears any stale entry for the same key. The scan stays bounded
(depth and entry caps, unreadable or truncated reads still report busy), and the
built-in subagent state and activity window are unchanged.
@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @HoloNova, the fail-closed reasoning and the one-sided cache are careful work. I'm closing this one, though:

  • Built-in subagents aren't cut off by the idle shutdown. A background run only listens to the parent's abort signal when it runs in the foreground (if (!runInBackground) request.signal?.addEventListener(...)), and the child runs in its own wrapper, which stays busy while it works. When it finishes, notifyParent reopens the parent (dependencies.reopenSession) if the idle timer already closed it. A foreground run keeps the parent isRunning(). So for pi-web's own subagents, closing an idle parent loses nothing.
  • The rest is a heuristic about other plugins. Treating recent mtimes under <session file>/ as a live child is a convention pi-web can't rely on. A quiet child is invisible, and a slow write keeps a session pinned. Around 690 lines of scanning, TTLs and fail-closed rules is a lot to carry for that.
  • It changes the session list. getRunningRpcSessionIds() feeds the sidebar's running indicator, so parents would show as running while their children work. That may be a good idea, but session management is being redesigned as a whole, and this belongs in that design.

If a plugin's children need the parent kept alive, PI_WEB_IDLE_TIMEOUT_MS=0 disables idle shutdown today. For a restart guard, polling the running ids plus the Agents list covers the built-in case.


Generated by Claude Code

@agegr agegr closed this Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants