Skip to content

fix(pi): stop pi-subagents workflows from pinning a pane on working - #22533

Merged
nwparker merged 1 commit into
stablyai:mainfrom
mmarabel:mmarabel/piworking-when-stopped
Sep 25, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
mmarabel:mmarabel/piworking-when-stopped

Conversation

@mmarabel

Copy link
Copy Markdown
Contributor

ELI5

When Pi hands work to a pi-subagents workflow, Orca keeps the pane "working" until every helper it saw start has finished. Helpers inside a workflow never say they are finished; only the workflow does. So Orca waited for them forever, and the pane stayed "working" even after later prompts. Now Orca also counts a helper as finished when its process exits.

What Changed

Before: once Pi ran a subagent workflow script, the pane stayed "working" for as long as that Pi process lived. New prompts ran and finished, but the pane never went back to done, and /reload didn't help. #22527 describes a remote host where one pane stayed stuck for more than two hours.

After: the pane goes to done when the turn after the workflow finishes, the same as it does for a single background subagent. A child started with async: true that keeps running after its workflow ends still holds the pane on "working" until it finishes.

How it works: the change is in the generated Pi status extension. The roster code moved out of agent-status-handler-source.ts into agent-status-subagent-roster-source.ts to keep that file under the size limit.

  • fix(pi): keep panes working while async subagents run #21882 keeps a list of running subagents. subagent:async-started adds an id, subagent:async-complete removes it, and agent_end is held back while the list has anything in it.
  • If a workflow child doesn't set async, pi-subagents runs it as an "awaited" background run. It sends subagent:async-started with parentWorkflowRunId, but its result goes straight to the workflow. No subagent:async-complete is ever sent for it; only the workflow's own id completes. Foreground (async: false) workflows have the same gap.
  • The extension now also listens for subagent:process-terminal, which pi-subagents sends when a run's process exits. Before deciding whether children still hold the pane, it removes any tracked run whose process has exited.
  • A run that reports its own completion does so about 150 ms after its process exits, and pi-subagents starts the wake-up turn at that moment. So a process exit on its own only settles the pane after a 2 s grace, and only if nothing else settled it first. In the paths that already worked, the pane settles on the same event as before.
  • Completion events that carry only runId are now accepted too. pi-subagents' own status bridge reads runId ?? id.
  • The new listener has its own reload guard, so a roster created by an older build in the same Pi process still gets it.

Why

  • My first idea was to ignore start events that carry parentWorkflowRunId. The traces show that children launched with async: true carry that field too and can outlive their workflow, so ignoring them would bring Pi appears finished while async pi-subagents are still running #18031 back for that case.
  • Dropping a workflow's children when the workflow completes doesn't cover foreground workflows, which never send a workflow-level event.
  • Polling process ids would work, but it needs a repeating timer and can be fooled by a reused process id. The process-exit event is pi-subagents' own signal, it is sent for every run process it starts, and it needs only the one-shot grace timer.
  • Clearing the list at Pi session boundaries isn't needed with this change. It would also drop children that survive a /reload, which is why fix(pi): keep panes working while async subagents run #21882 left plain Pi's list alone at those boundaries.

Linked Issue

Fixes #22527

Follow-up to #21882 (#18031).

Visual Proof

N/A: this changes when the status extension reports completion. Nothing in the interface changes. Recorded before and after results from real runs are under Testing.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

agent-status-extension-async-subagents.test.ts replays event orders recorded from real pi-subagents 0.71.0 runs:

  • a background workflow with awaited children, with and without the wake-up turn in between
  • a foreground workflow
  • a child whose process exits after its workflow completed
  • an async: true child that outlives its workflow (must stay working, with no early done)
  • process exits for runs it isn't tracking
  • completions that carry only runId
  • reload

Six of the eight fail on main. The two regression guards pass on both.

End to end on Linux, I ran Pi 0.87.1 in RPC mode with pi-subagents 0.71.0 and the generated extension posting to a local test listener. The workflows had two delegate children each. Last status posted:

Scenario main this branch
Background workflow, awaited children agent_start (stuck) agent_end
Foreground workflow, awaited children agent_start (stuck) agent_end
Background workflow, async: true child running 45 s after the workflow ended not run working for the whole 45 s, then agent_end

pnpm tc, pnpm test src/main/pi/ (265 passed) and pnpm run check:code-quality:changed pass. wsl-hook-relay-manager.test.ts has one failure on this host, and it fails the same way on unmodified main.

AI Disclosure

Written with Claude Opus 5.5 in the pi coding agent, reviewed by me.

Review

  • Existing behavior, unchanged: when a single background run finishes, pi-subagents starts the wake-up turn before Orca's handler sees the completion. Orca therefore posts agent_end and then agent_start a few milliseconds apart. I recorded the same thing on main; Keep Pi active while async subagents run #18032 addresses it.
  • Not covered: a foreground child that gets detached into the background never sends subagent:async-started, so it was never tracked, and that hasn't changed. If a run process exits while Pi is in the middle of a /reload, pi-subagents can drop the exit event. That run then waits for its own completion event, as it does today.
  • Older pi-subagents: versions without subagent:process-terminal behave exactly as they do today.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Security: the extension only reads run ids from these events, and nothing new leaves the host. Cross-platform: pi-subagents sends the exit event from the process close handler on every platform, and the grace timer is unref'd. Remote/SSH/WSL: SSH and WSL hosts get the same generated extension, and it posts the same hook events as before, so mixed client and server versions are unaffected. Mobile: no change. Performance: one more listener and at most one pending 2 s timer.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Awaited workflow children announce subagent:async-started but never get a
subagent:async-complete, so the stablyai#21882 roster kept their ids forever and
suppressed every later agent_end. Treat subagent:process-terminal (the
runner exiting) as the end signal for tracked runs, with a short grace so
runs that report their own completion keep settling through that path.
Also accept completion events that carry only runId.

Fixes stablyai#22527
@coderabbitai

coderabbitai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 9e824811-fca3-4969-8ace-25db966b23ff

📥 Commits

Reviewing files that changed from the base of the PR and between 519bde8 and 2c5660f.

📒 Files selected for processing (3)
  • src/main/pi/agent-status-extension-async-subagents.test.ts
  • src/main/pi/agent-status-handler-source.ts
  • src/main/pi/agent-status-subagent-roster-source.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The agent status handler now emits subagent roster setup and event-handling code from dedicated source generators. The roster tracks active and exited child IDs, listens for runner-terminal events, and uses a 2,000 ms grace check before attempting settlement after a runner exit. Session shutdown and switch clear exited IDs. New tests cover completion ordering, untracked runs, runId-only completion events, and reload behavior.

Merge Risk: ⚪ Minimal · up to 2c566

Pi panes now return to done after pi-subagents workflows whose awaited children finish, while explicitly async children that outlive their workflow still keep the pane working. The added tests cover completion ordering and reload behavior. No outstanding merge-blocking risk was identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: preventing pi-subagents workflows from leaving a pane stuck in the working state.
Description check ✅ Passed The description is complete and relevant. It includes the required explanation, user-visible behavior, rationale, linked issue, visual-proof statement, testing details, AI disclosure, review notes, bo…
Linked Issues check ✅ Passed Issue #22527 requires Pi panes to settle after pi-subagents workflows with awaited children and to accept completion events identified by runId. The generated roster now subscribes to subagent:process…
Out of Scope Changes check ✅ Passed The changes stay within Issue #22527. The roster source extraction supports the same lifecycle fix, and the added tests exercise the reported stale-roster and runId handling cases. No unrelated behavi…
  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Pi subagent roster extraction — the pi.events-hosted lifecycleState roster and its onEvent handler moved from agent-status-handler-source.ts into the new agent-status-subagent-roster-source.ts (getPiSubagentRosterSetupSourceLines / getPiSubagentRosterEventSourceLines); the move is behavior-preserving and the setup/event split keeps both files under the size limit.
  • Process-exit liveness signal — the generated extension now listens for subagent:process-terminal and records the affected run in a new lifecycleState.exited set; postAgentEndOnce() prunes exited ids from active before deciding whether children still hold the pane, so awaited workflow children that never emit subagent:async-complete no longer pin the pane.
  • Bounded grace for self-reporting runs — a single shared, unref'd 2s RUNNER_EXIT_GRACE_MS timer is armed (and rescheduled per exit) only while waiting, so a run that reports its own completion ~150ms after exit, plus the wake turn it triggers, still settles first.
  • runId-only completions — onEvent now falls back to runId when id is absent, matching pi-subagents' own id ?? runId resolution (its result-watcher emits {...data, runId}, where data.id can be missing).
  • Reload/reset handling — a separate runnerExitListener guard lets a roster object created by an older in-process build still subscribe; exited is cleared on session_shutdown/session_switch; the field is optional so an older-build roster still works.
  • Tests — new agent-status-extension-async-subagents.test.ts replays recorded event orderings (awaited background/foreground workflows, late child exit, async: true child outliving its workflow, untracked/interleaved exits, runId-only completion, reload).

I verified the load-bearing external contract against the published [email protected] tarball: subagent:process-terminal carries runId, is emitted only from the runner's proc.once('close') handler (so even a proof with state: "unknown" still means the process closed), and foreground workflows route through async-execution.js's executeAsyncSingle/executeAsyncChain, so their children emit both async-started and process-terminal. I also ran the touched suites plus the two sibling extension suites (22 passed), and traced the reported event ordering — the awaited-child case settles whether child exits land before or after the workflow completion.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit 9678dfb into stablyai:main Sep 25, 2026
2 checks passed
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.

[Bug]: Pi pane stays "working" forever after a pi-subagents workflow runs

2 participants