Repository navigation
improve: reduce CPU spent broadcasting session activity - #159182
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 27, 2026, 2:22 AM ET / 06:22 UTC (Revision 12). ClawSweeper reviewWhat this changesThe branch reduces work when the Gateway calculates session activity and makes cron and session-group test fixtures drain their scoped database resources before cleanup. Merge readiness✅ Ready for maintainer review Current main still uses the allocation-heavy activity resolver, and the merged related PRs do not contain this optimization. The pinned patch has no supported blocking finding, so this remains a useful landing candidate. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway combines live agent-run contexts with session rows and sends activity snapshots to subscribed clients. The affected tests create SQLite-backed fixtures and release their resources before removing temporary directories. flowchart LR
A[Agent run contexts] --> B[Run projection index]
B --> C[Progress resolver]
D[Session row] --> E[Session snapshot]
C --> E
E --> F[Session change broadcast]
F --> G[Subscribed clients]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +33, Tests +130. Total +163 across 6 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep run liveness in the registry and calculate presentation status in the existing projection owner, with scoped fixture cleanup that preserves unrelated stores. Do we have a high-confidence way to reproduce the issue? Yes for the performance concern: current main visibly allocates status arrays in the resolver, and the PR body gives a concrete broadcaster benchmark. This read-only review did not rerun it. Is this the best way to solve the issue? Yes: the change uses the existing projection owner and preserves the registry call contract without adding a cache, configuration, or protocol field. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 168ebc5b6708. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (11 earlier review cycles; latest 8 shown)
|
Retire case-owned native resources before removing canonical fixture roots. Preserve sibling stores and accepted queued work. Include the shared state-dir helper repair from upstream 5e0d86f.
f865bd8 to
b8cd89f
Compare
|
Merged via squash.
|
) * perf(gateway): reduce session activity broadcast CPU * refactor: keep projected run state merger private * test: simplify projected run context fixture * test(gateway): await scoped session-group cleanup * test(cron): settle scoped fixture databases before cleanup Retire case-owned native resources before removing canonical fixture roots. Preserve sibling stores and accepted queued work. Include the shared state-dir helper repair from upstream d4970fb.
What Problem This Solves
Broadcasting session activity spends unnecessary Gateway CPU resolving the same agent identity and allocating status arrays for each child and recipient.
User Impact
Session activity broadcasts use less CPU while preserving payloads, visibility, and current run state. No schema, migration, configuration, or operator change is required.
Why This Change Was Made
The run registry owns snapshot acquisition; its existing projection module calculates progress. The broadcaster resolves the first encoded agent once, reuses its normalized identity prefix, merges statuses directly, and stops when a running producer cannot be outranked. Exact session keys, explicit/default agent selection, ownerless adoption, and status precedence remain unchanged. This adds no cache.
The remaining fixture repairs make session-group and cron cleanup await the existing scoped database owner before removing state. Fixture producers return canonical paths so cleanup selects the same resources that callers opened. Sibling stores remain available. The Gateway reseed repair and shared state-directory helper from the earlier candidate are now on main through #159197 and #159281, so those four files are absent from this diff.
Evidence
The real
sessions.changedbroadcaster, with 256 synthetic child sessions and 16 recipients, used 35.7% less main-thread CPU: 102.071 ms to 65.662 ms per 20 broadcasts after warmup, across four interleaved samples. Payloads, recipient frames, and zero-SQL behavior matched. This measures that synthetic flow, not overall production latency; temporary measurement code was removed.Projection and Gateway presentation proof passed 33 cases; 94 task-progress cases passed before the behavior-preserving extraction. The added projection case took 1 ms locally; hosted Bun rounded it to 0 ms. The focused file took 1.478 s locally. Production types, all 25 test type graphs, repository guards, dead-export scans, and independent P0–P2 review passed for the unchanged production change.
Cron cleanup's regression holds a real mutation-worker lifetime and verifies revocation, settlement before removal, closed case-owned handles, and an unaffected sibling store. Both original and async-close-only alias controls fail by removing state before revocation. The final four-case harness passes under an aliased temp root in 28.354 s wrapper wall; the new case took 1.125 s. The original 14-case reaper file passes in 23.256 s. All three session-group cases pass under an aliased temp root in 31.916 s; earlier hosted case times were 449/448/1,809 ms. Owning types, lint, formatting, and diff checks pass.
The six remaining files are byte-identical to the independently reviewed candidate. Integration onto
aad795bfpreserves the four already-landed files exactly. Main's dependency/cache generation changed throughede3447band selected-job packing through24100ea4, and fresh integrated-head CI 36297441897 passed with 146 successful and 21 skipped checks. ClawSweeper reviewed the exact integrated head with no findings. Neither integration nor this passing run is claimed to fix the unresolved Bun crash below.Previous CI failures and bounded investigation
Earlier CI encountered an intermittent private-completion timeout and cron cleanup
ENOTEMPTY. Queued-write and scoped cleanup defects were independently demonstrated and repaired. Passing replays do not establish the exact cause of either intermittent hosted failure.Prior-head CI 36287547770 stopped when the checksum-pinned Bun runtime segfaulted after spawning Vitest workers and before reporting a fast-test case. Earlier groups passed 831 Gateway Node, 178 Gateway Bun, 1,230 security, and 82 Node fast cases. The tested merge was
7f4a797a2e55b5cbafc1237537db5e8cc57c2aae. The triggering import or case remains unproven; downstream jobs were cancelled and that CI gate remains failed.One bounded isolated replay used the exact merge, pinned Bun
1.4.3-canary.1+ddfce5d01executable, and original 704-file envelope with two workers. The crash did not reproduce: 8,577 Bun cases passed and three were skipped. The complete envelope exited 1 because a Node cleanup case failed. The diagnostic subreaper deferred reaping orphaned children, contaminating the process-group cleanup observation; no captured PID-to-group mapping establishes exact attribution. Preceding job groups and the original protected transform-cache seed were omitted. Matching symbols were unavailable in the checked release assets, exact-head workflow artifacts, and local debug locations; all 17 known crash PCs remain unsymbolized. This replay establishes neither a crash fix nor a passing complete gate.