Skip to content

perf(agent-status): coalesce live status bursts into one render pass (STA-3328) - #12377

Merged
brennanb2025 merged 2 commits into
mainfrom
brennan/sta-3328-agent-status-coalescing
Aug 4, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennan/sta-3328-agent-status-coalescing

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Summary

Third installment of the STA-3328 typing-lag work (after #12359 spinner, #12371 tab-group amplifiers). This addresses the burst source: every live agent-status event arrives as its own IPC task, and each one committed the store and paid a full React render pass — profiled live as 200–488ms main-thread tasks (Receive mojo message → applyAgentStatus → setAgentStatus → renderRootSync) under heavy orchestration, exactly matching Neil's "happens when using orchestration heavily" report.

The design-defining fact (verified empirically with a real-store/real-React probe before writing any code): N zustand commits in the same macrotask produce exactly 1 React render pass — React 19 flushes external-store sync work at the microtask boundary. So no setAgentStatus refactor is needed; the fix is dispatch-level in useIpcEvents:

  • The leading event of a burst applies synchronously — single events pay zero added latency.
  • Followers within a 33ms window buffer and apply together in one task — one render pass instead of N. Ordering is unchanged (FIFO; every guard, fold, and side-effect runs per event exactly as before, each seeing the previous event's state).
  • Only an applied event arms the window: dropped/pending events (startup replay, not-yet-mounted panes) commit nothing and cost no render, so their successors stay immediate — this also preserves the existing buffered-until-mount retry semantics byte-for-byte.
  • Both clear paths purge the pane's/connection's queued sets first — without this, a coalesced working flushing 33ms after a clear would resurrect a removed status (pinned by a dedicated test).
  • Effect teardown clears the timer and queue.

Worst-case added status-visibility latency is 33ms — under half the spinner's own 83ms tick.

Screenshots

No visual change.

Testing

  • pnpm lint — oxlint, react-doctor changed-lines gate, oxfmt clean (pre-commit hook re-verified all three)
  • pnpm typecheck — all three configs pass, 0 errors
  • pnpm test — full useIpcEvents.test.ts suite: 98/98 pass (96 existing + 2 new); full suite left to CI
  • pnpm build — left to CI
  • New tests verified non-vacuous (each fails against the pre-fix source): (1) burst of 3 → leading applies synchronously, followers coalesce into one deferred batch in arrival order; (2) a clear purges queued burst events so a coalesced set cannot resurrect the removed status. One pre-existing test ("buffers ready push events until a mounted tab contains the pane leaf") drove the applied-only-arms-the-window refinement and passes unchanged.

AI Review Report

Reviewed for: ordering preservation (FIFO queue; leading precedes queued; per-event guards like the receivedAt out-of-order rejection see the store state left by the previous event, identical to today's sequential arrival); clear-vs-set races (both the transient watermark path and the direct paneKey clear purge the burst queue using the same raw-paneKey/connectionId comparisons as the existing pending-retry purges); re-entrancy (queue is spliced before applying, mirroring the crash-9fc89529 guard pattern; the existing isFlushingAgentStatuses guard is untouched); timer lifecycle (cleared on effect teardown alongside the retry timer; queue emptied); occluded-window behavior (Chromium clamps the 33ms timer to 1s when hidden — status still applies, and a hidden window renders nothing anyway). Cross-platform: renderer-only dispatch change; the SSH/WSL connection-watermark semantics are exercised by the existing suite (98/98). The startup snapshot replay loop was already a single task (batches naturally) and is untouched.

Security Audit

No input handling, command execution, path handling, auth, secrets, or IPC surface changes. Same events, same validation pipeline, same store actions — only dispatch timing is batched, with a bounded in-memory queue (drained every 33ms, purged on clears and teardown).

Notes

…(STA-3328)

Each live agent-status event arrives as its own IPC task, so a multi-agent
burst paid one full React render pass per event (200-488ms main-thread tasks
under heavy orchestration). Same-task store commits batch to a single render
pass, so buffer a burst for one 33ms window and apply it in one task. The
leading event still applies synchronously (zero added latency for single
events), only an applied event arms the window (dropped/pending events stay
immediate), and both clear paths purge queued sets so a coalesced event
cannot resurrect a removed status.
@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c6d6ec64-ecac-4a77-ae40-e7a631ac2c50

📥 Commits

Reviewing files that changed from the base of the PR and between 8d80f48 and c97559a.

📒 Files selected for processing (2)
  • src/renderer/src/hooks/useIpcEvents.test.ts
  • src/renderer/src/hooks/useIpcEvents.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/hooks/useIpcEvents.test.ts
  • src/renderer/src/hooks/useIpcEvents.ts

📝 Walkthrough

Walkthrough

Added a 33 ms burst buffer for live agent-status IPC events. The first eligible event applies immediately, and later events flush together in arrival order. Status-clear handling removes matching queued events. Effect cleanup cancels the timer and clears the queue. Tests cover immediate application, ordered batching, and prevention of status restoration after pane clearing.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes coalescing live agent-status bursts into one render pass.
Description check ✅ Passed The description covers the change, testing, review, security, platform considerations, and notes; the build is explicitly left to CI.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

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

🧹 Nitpick comments (1)
src/renderer/src/hooks/useIpcEvents.test.ts (1)

6239-6321: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a teardown test for the burst timer and queue.

This test covers clear-vs-queued behavior well. No test in this change covers effect cleanup for the new burst state: that unmounting the hook while a flush is scheduled cancels liveAgentStatusBurstTimer and clears liveAgentStatusBurstQueue, so a pending burst does not apply a status after teardown. Add a test that queues a follower event, unmounts (or calls the effect's cleanup), advances the fake timer past the burst window, and asserts setAgentStatus does not fire again.

Do you want me to draft this test?


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 26325905-055f-4e51-aa92-16746e58b523

📥 Commits

Reviewing files that changed from the base of the PR and between 8ab7d8a and 8d80f48.

📒 Files selected for processing (2)
  • src/renderer/src/hooks/useIpcEvents.test.ts
  • src/renderer/src/hooks/useIpcEvents.ts

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Electron QA — agent-status burst + terminal typing

Validated the intended dev instance for brennan/sta-3328-agent-status-coalescing through CDP/playwright. The real local agent-hook HTTP surface delivered 90 status updates while Playwright typed 24 characters into the actual terminal: 90/90 returned HTTP 204, p95/max inter-key gaps were 32.9/35.0 ms (idle: 27.6/27.9 ms), max frame interval was 9.2 ms, and Chromium reported no long animation frame during the burst.

Visible state matched the backing Zustand state: the worktree and terminal showed Working during the burst, then Done with the final completion message in FIFO order. The deterministic 98/98 hook suite separately covers the exact queued done → pane-clear race fixed in c97559a604.

Working during status burst, with terminal input responsive

Working status burst and terminal typing

Completion applied in FIFO order

Done status after FIFO completion

Not exercised live: hidden-window timer clamping and an SSH/relay connection; those paths were reviewed and covered by the focused suite, but this Electron run used a local workspace.

@brennanb2025
brennanb2025 merged commit e39cdb8 into main Aug 4, 2026
42 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…(STA-3328) (stablyai#12377)

* perf(agent-status): coalesce live status bursts into one render pass (STA-3328)

Each live agent-status event arrives as its own IPC task, so a multi-agent
burst paid one full React render pass per event (200-488ms main-thread tasks
under heavy orchestration). Same-task store commits batch to a single render
pass, so buffer a burst for one 33ms window and apply it in one task. The
leading event still applies synchronously (zero added latency for single
events), only an applied event arms the window (dropped/pending events stay
immediate), and both clear paths purge queued sets so a coalesced event
cannot resurrect a removed status.

* fix(agent-status): preserve queued completion on teardown
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.

1 participant