Skip to content

fix(pi): report completion on agent_settled so intermediate agent_end stops firing false notifications - #8826

Merged
Jinwoo-H merged 1 commit into
stablyai:mainfrom
syabro:syabro/pi-agent-settled-notifications
Jul 16, 2026
Merged

Jinwoo-H merged 1 commit into
stablyai:mainfrom
syabro:syabro/pi-agent-settled-notifications

Conversation

@syabro

@syabro syabro commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #8035.

ELI5: Pi's agent_end means "this attempt ended," not necessarily "the whole task is finished." Pi may still retry, compact context, or run a queued follow-up. Orca now waits for Pi's real agent_settled signal before reporting completion.

Modern Pi reports completion exactly once from agent_settled. On the first run, ambiguous agent_end events are held while the extension context remains active. Older Pi/OMP runtimes that never emit agent_settled retain completion reporting through an idle-state fallback. The settled handler still posts the existing agent_end hook payload, so no receiver contract changes.

Root cause and reproduction

Orca's completion quiet window is 1,500 ms, while Pi's default automatic retry delay is 2,000 ms. Posting the first agent_end immediately therefore produced this deterministic sequence:

  1. Pi emitted an intermediate agent_end.
  2. Orca armed its 1.5-second completion timer.
  3. Orca showed "Task complete."
  4. Pi resumed its retry at 2 seconds.

The regression harness keeps Pi non-idle across multiple intermediate agent_end events and a 2-second retry-shaped gap. With the previous implementation it records three premature completion posts; with this implementation it records none before agent_settled and exactly one afterward.

Implementation

  • Defer ambiguous first-run agent_end completion while ctx.isIdle() is false.
  • Cancel pending fallback checks when work resumes or agent_settled arrives.
  • Use a per-run guard so an idle transition and agent_settled cannot race duplicate posts.
  • Back off idle checks from 25 ms to a 250 ms cap and unref every timer.
  • Drop stale extension contexts safely across in-process reloads.
  • Preserve current behavior when a legacy runtime supplies no idle context.
  • Preserve PID ownership so nested Pi/OMP agents remain suppressed.

Testing

  • A/B regression: previous handler fails 4 focused tests, including three premature first-run posts
  • Focused generated-extension suite: 21/21
  • Seven affected Pi, installer, SSH, relay, and completion-coordinator suites: 181/181
  • pnpm typecheck
  • Scoped oxlint and oxfmt --check
  • pnpm run check:max-lines-ratchet
  • git diff --check

Coverage includes retries longer than the notification quiet window, multiple intermediate ends, resumed work, duplicate settlement races, legacy Pi/OMP, missing and stale contexts, reloads, nested processes, WSL transport, and SSH/relay installation.

Trade-offs and regression risk

  • Legacy Pi/OMP completion can be delayed by up to roughly 250 ms while idle state is confirmed.
  • Standard modern Pi is expected to emit agent_settled for every completed run; nonstandard loaders without hook context use the immediate compatibility fallback.
  • Polling is limited to initial capability discovery, backs off to at most four checks per second, and is cancelled or unreferenced so it cannot hold the process open.

No visual, receiver-protocol, auth, secret, path, command-execution, or generic completion-coordinator behavior changes.

Screenshots

No visual change.

Security audit

The change adds process-local event state and timers inside the generated Pi/OMP extension. It does not add input parsing, filesystem writes, commands, credentials, or new IPC/network endpoints.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Pi status handler now detects agent_settled, reports completion from that event, and suppresses duplicate agent_end reports. When settled events are unavailable, it checks idle state with deferred retries and retains fail-open behavior without context. Tests cover event ordering, stale contexts, legacy Pi and OMP behavior, and single-delivery guarantees.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed, but it is missing the required AI Review Report section and the template's Notes section. Add an AI Review Report covering risks and cross-platform checks, and include a Notes section; keep the existing summary, testing, screenshots, and security details.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The change implements #8035 by using agent_settled to suppress intermediate agent_end notifications while keeping legacy fallback behavior.
Out of Scope Changes check ✅ Passed The code changes stay focused on Pi completion signaling and its tests, with no clear unrelated or extraneous additions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly states the main change: using agent_settled to report Pi completion and avoid false intermediate agent_end notifications.

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.

@syabro
syabro force-pushed the syabro/pi-agent-settled-notifications branch from fe12a22 to ec0d6f6 Compare July 15, 2026 06:33
… stays silent (stablyai#8035)

Pi's agent_end fires per low-level run — auto-retry, auto-compaction and
queued follow-ups all emit it while the agent is still working — so Orca
fired premature completion notifications. Pi >= 0.80.4 emits
agent_settled exactly once when nothing is left to run.

The extension now subscribes to both events: the first agent_settled
proves runtime support and mutes agent_end reporting from then on; on
older Pi/OMP runtimes (where unknown event names register silently and
never fire) agent_end keeps reporting done exactly as before. The
settled handler reports the existing agent_end hook event, so the
agent-hooks server contract is unchanged.
@Jinwoo-H
Jinwoo-H force-pushed the syabro/pi-agent-settled-notifications branch from ec0d6f6 to 3e36192 Compare July 16, 2026 20:58
@Jinwoo-H

Copy link
Copy Markdown
Contributor

Maintainer review completed after independently reproducing the first-run failure.

The original implementation still posted intermediate agent_end events until the first agent_settled, so Pi's default 2-second retry could outlast Orca's 1.5-second quiet window and trigger a false completion. The reworked extension defers ambiguous first-run ends while Pi is non-idle, reports exactly once at settlement, and preserves legacy Pi/OMP through an idle fallback.

Evidence:

  • A/B regression: the previous handler fails four focused cases and posts three premature first-run completions; the rework passes.
  • Focused generated-extension suite: 21/21.
  • Seven affected Pi, installer, SSH, relay, and completion-coordinator suites: 181/181.
  • Full typecheck, scoped lint/format, max-lines ratchet, and diff checks pass.

Reviewed for nested-process ownership, reload/stale-context cleanup, WSL/SSH transport, duplicate settlement races, and legacy compatibility. No remaining blocking findings.

@Jinwoo-H Jinwoo-H 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.

Approved after independent reproduction, contributor rework, and clean regression validation.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c495a030-96e3-441f-ae5f-2a7599b7eef8

📥 Commits

Reviewing files that changed from the base of the PR and between ec0d6f6f8c67068221268fc10dcc2bc35f49cf11 and 3e36192.

📒 Files selected for processing (2)
  • src/main/pi/agent-status-extension-source.test.ts
  • src/main/pi/agent-status-handler-source.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/pi/agent-status-handler-source.ts

Comment on lines +494 to +499
await harness.callHook('agent_end', undefined, context)
await vi.advanceTimersByTimeAsync(100)
await harness.callHook('agent_start')
await harness.callHook('agent_end', undefined, context)
await vi.advanceTimersByTimeAsync(2_000)
await harness.callHook('agent_settled')

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Verify cancellation before invoking the second agent_end.

The second agent_end clears the existing timer itself, so this test passes even if agent_start fails to cancel the original pending check. Make the old context become idle and assert no completion is delivered before starting the next cycle.

Proposed test adjustment
       await harness.callHook('agent_end', undefined, context)
       await vi.advanceTimersByTimeAsync(100)
       await harness.callHook('agent_start')
+
+      context.isIdle.mockReturnValue(true)
+      await vi.advanceTimersByTimeAsync(2_000)
+      expect(
+        harness.fetchMock.mock.calls.map(
+          (call) => JSON.parse(String(call[1]?.body)).payload.hook_event_name
+        )
+      ).toEqual(['agent_start'])
+
+      context.isIdle.mockReturnValue(false)
       await harness.callHook('agent_end', undefined, context)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await harness.callHook('agent_end', undefined, context)
await vi.advanceTimersByTimeAsync(100)
await harness.callHook('agent_start')
await harness.callHook('agent_end', undefined, context)
await vi.advanceTimersByTimeAsync(2_000)
await harness.callHook('agent_settled')
await harness.callHook('agent_end', undefined, context)
await vi.advanceTimersByTimeAsync(100)
await harness.callHook('agent_start')
context.isIdle.mockReturnValue(true)
await vi.advanceTimersByTimeAsync(2_000)
expect(
harness.fetchMock.mock.calls.map(
(call) => JSON.parse(String(call[1]?.body)).payload.hook_event_name
)
).toEqual(['agent_start'])
context.isIdle.mockReturnValue(false)
await harness.callHook('agent_end', undefined, context)
await vi.advanceTimersByTimeAsync(2_000)
await harness.callHook('agent_settled')

@Jinwoo-H
Jinwoo-H merged commit fc6570d into stablyai:main Jul 16, 2026
1 check passed
@euclodius

euclodius commented Jul 17, 2026 •

Copy link
Copy Markdown

Follow-up after this landed: with OMP 17.0.2, a child AgentSession's settlement still appears to be observed as pane/root settlement. Orca shows 'OMP finished' with 'Using yield' while the visible parent continues working. The in-process OMP reproduction and likely event sequence are tracked in #9181.

enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
… stays silent (stablyai#8035) (stablyai#8826)

Pi's agent_end fires per low-level run — auto-retry, auto-compaction and
queued follow-ups all emit it while the agent is still working — so Orca
fired premature completion notifications. Pi >= 0.80.4 emits
agent_settled exactly once when nothing is left to run.

The extension now subscribes to both events: the first agent_settled
proves runtime support and mutes agent_end reporting from then on; on
older Pi/OMP runtimes (where unknown event names register silently and
never fire) agent_end keeps reporting done exactly as before. The
settled handler reports the existing agent_end hook event, so the
agent-hooks server contract is unchanged.
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.

[Other]: Pi should use agent_settled to detect if the process needs attention

4 participants