Skip to content

fix(pi): scope status completion to the run that ended - #20840

Merged
nwparker merged 1 commit into
stablyai:mainfrom
mmarabel:fix/pi-status-run-generation
Sep 19, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
mmarabel:fix/pi-status-run-generation

Conversation

@mmarabel

Copy link
Copy Markdown
Contributor

ELI5

When an agent finishes a turn, Orca's Pi integration tells the sidebar "this pane is done". Another Pi extension can immediately start one more run from the settlement event (a memory reminder that asks the agent to continue does exactly that). In that shape the "done" message for the finished run was eaten and the follow-up run's own "done" was never sent, so the sidebar kept showing "working" even though the agent sat idle. This PR makes each run report its own end.

What Changed

src/main/pi/agent-status-handler-source.ts now dedupes the completion post by the generation of the run that ended (runGeneration / endedRunGeneration / completionPostedGeneration) instead of a single agentEndReported boolean that agent_start reset.

  • agent_start bumps runGeneration.
  • agent_end (terminal) records endedRunGeneration.
  • postAgentEndOnce() posts while completionPostedGeneration !== endedRunGeneration, so the idle-recheck path and the settlement path still cannot double-post for one run, while a later run is no longer suppressed.
  • checkPendingAgentEnd() dedupes against the same generation pair.

src/main/pi/agent-status-extension-nested-run-settlement.test.ts reproduces the event order Pi produces in this shape and asserts the follow-up run's completion is published. It fails on the previous code (agent_start → agent_end → agent_start → message_end → agent_end → message_end, so the host's last word is working) and passes now.

Deliberate call: the older run's settlement still posts its own completion. That run did finish; the bug was the follow-up run's completion being swallowed, not the earlier post.

Why

Pi dispatches extension events in registration order, and a sibling extension's agent_settled handler can start the next run synchronously (pi.sendMessage(..., { deliverAs: 'followUp', triggerTurn: true }) → session.sendCustomMessage → _runAgentPrompt). The status extension therefore sees the follow-up's agent_start before its own agent_settled for the run that just ended. Since agent_start reset the shared latch, the older settlement consumed it and the follow-up's own settlement posted nothing.

The host keeps the last accepted event per pane, so its last word stayed the follow-up's final message_end (working) with no later event able to correct it; the row only decayed when the 30-minute freshness window expired. Observed on a remote Orca host with a Pi pane whose session transcript showed the follow-up turn completing normally while the process sat idle.

Linked Issue

Fixes #20838

Visual Proof

N/A — no visual change: no rendered UI or interaction code is touched. The fix changes which hook events are published for an unchanged status glyph, and the regression is covered by the unit test above.

Testing

  • pnpm test src/main/pi → 167 passed (9 files), including the new regression test.
  • RED/GREEN verified: reverting only the handler change makes the new test fail with the sequence above; the fix makes it pass.
  • pnpm tc:node, pnpm run check:code-quality:changed (scoped to this diff), and oxfmt --check clean.
  • Manually reproduced end to end on a Linux remote host with a macOS client before fixing: host agent-hooks/last-status.json held the pane at message_end / working for 40+ minutes while the Pi process was idle.
  • Platforms: the change is in the shared host-side Pi extension generator, with no platform-specific code paths. Verified on Linux (remote host); macOS and Windows load the same generated source.

AI Disclosure

Implemented with the Pi coding agent (deepseek-flash via opencode-go, max reasoning). The root cause was reproduced from live host state and with the existing extension test harness before the fix; the human reviewed the diff.

Review

Agent review summary:

  • Cross-platform: no platform-specific code; the generated extension is identical on macOS, Linux, and Windows. No path, shortcut, or shell behavior touched.
  • Local / SSH / remote: src/main/pi/agent-status-handler-source.ts feeds the host-side hook publisher, so local, SSH, and remote-runtime panes all take the same path. The bug was observed on a remote host and the fix is host-side, so no client-side assumption is added.
  • Agent compatibility: the handler is shared by pi, omp, and prime-agent. The OMP willContinue contract, the legacy idle-recheck path, and the Prime daemon-worker guard are unchanged; all existing Pi extension tests, including the OMP lifecycle and UI-prompt suites, pass.
  • Performance: replaces one boolean with three integer comparisons and adds no extra hook posts — the dedupe still allows exactly one completion per ended run.
  • UI quality: no styling, layout, tokens, or components touched; only the status value the sidebar renders for a pane.
  • Security: no new inputs, paths, credentials, or network targets; the change only affects when an existing loopback hook POST is sent.

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

Nothing here changes the wire format or the hook payloads; only the dedupe key for the terminal agent_end post. Security, cross-platform, SSH/remote, mobile, backwards-compatibility, and performance were considered as above.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • 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)

A sibling extension can queue a follow-up run from inside its own
`agent_settled` handler (a memory reminder does exactly that). Pi
dispatches extension handlers in registration order and starts that run
synchronously, so the status extension sees the follow-up's `agent_start`
before its own `agent_settled` for the run that just ended.

The completion post was deduped by one `agentEndReported` boolean that
`agent_start` reset, so the older settlement consumed the latch and the
follow-up run's own settlement was swallowed. The host kept the
follow-up's last `message_end` (working) as its last word about the pane,
with nothing left to correct it until the freshness window expired.

Key the dedupe on the generation of the run that ENDED instead: the older
settlement still reports its own completion (that run did finish), and
the follow-up run now reports its own end exactly once.

Fixes stablyai#20838
@coderabbitai

coderabbitai Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

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: a0d29104-7232-4c09-8141-348d098f6336

📥 Commits

Reviewing files that changed from the base of the PR and between caa465d and a58ad4c.

📒 Files selected for processing (2)
  • src/main/pi/agent-status-extension-nested-run-settlement.test.ts
  • src/main/pi/agent-status-handler-source.ts

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


📝 Walkthrough

Walkthrough

The status handler now tracks agent completion by run generation instead of a shared boolean latch. It records the generation that ended and prevents duplicate completion posts for that generation. A regression test covers a follow-up run started during settlement and verifies that its agent_end event is posted last.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to a58ad

The follow-up run completion path is covered without an identified remaining regression, so the change is ready to merge with normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 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: scoping Pi status completion to the run that ended.
Description check ✅ Passed The description is complete and aligned with the repository template. It explains the problem, implementation, rationale, linked issue, testing, platform impact, review considerations, and checklist s…
Linked Issues check ✅ Passed The changes satisfy issue #20838. agent-status-handler-source.ts replaces the shared boolean latch with run-generation tracking. agent_start records a new generation, agent_end records the gener…
Out of Scope Changes check ✅ Passed The listed changes are limited to the Pi status handler completion-deduplication fix and its regression test. The test directly supports issue #20838. No unrelated source changes, public API changes, …
  • 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

  • Per-run completion dedupe — src/main/pi/agent-status-handler-source.ts replaces the pane-lifetime agentEndReported latch with runGeneration / endedRunGeneration / completionPostedGeneration. agent_start bumps the run, a terminal agent_end records the ended run, and postAgentEndOnce() / checkPendingAgentEnd() post while completionPostedGeneration !== endedRunGeneration, so a late agent_settled for a finished run can no longer swallow a follow-up run's own completion.
  • Regression coverage — new src/main/pi/agent-status-extension-nested-run-settlement.test.ts reproduces the sibling-extension ordering (agent_end → follow-up agent_start → prior agent_settled) and asserts the host's final word is agent_end; it fails on the old boolean latch (last post message_end) and passes now.

I traced the willContinue, legacy-pending-recheck, OMP-immediate, and first-agent_settled-without-agent_end paths; each still publishes exactly one completion per ended run, and pnpm test src/main/pi passes 167/167 with a clean tree. The deliberate decision to let the older run's settlement still post its completion is load-bearing: I tried gating postAgentEndOnce() on endedRunGeneration === runGeneration, and it breaks the agent-status-ui-prompt cases where agent_settled is the completion boundary with no preceding agent_end (there endedRunGeneration stays 0 while runGeneration is 1). Since agent_settled carries no generation, the stale post cannot be distinguished from that legitimate case — so the current shape is the right call.

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

@nwparker
nwparker merged commit f960bc7 into stablyai:main Sep 19, 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" when a follow-up run is queued from another extension's agent_settled handler

2 participants