Repository navigation
perf(gateway): keep long chat streams responsive - #158489
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 25, 2026, 11:40 PM ET / September 26, 2026, 03:40 UTC (Revision 4). ClawSweeper reviewWhat this changesThe branch incrementally filters Gateway chat streams, carries prepared text deltas into broadcasts, shares directive parsing with the agent stream, and adds projection lifecycle tests. Merge readiness✅ Ready for maintainer review Keep open. Current main still normalizes the full growing chat buffer, so this performance change remains useful. The latest branch addresses the prior activity-clock fixture concern, and this review found no definite introduced defect. Priority: P2 Review scores
Verification
How this fits togetherAgent text updates enter Gateway chat-run state, where internal directives are filtered before chat events reach Control UI clients. The embedded agent stream shares the directive parser used to distinguish literal Markdown examples from reply controls. flowchart LR
A[Agent text updates] --> B[Gateway chat run]
B --> C[Display projection]
C --> D{Append or correction?}
D --> E[Chat deltas and snapshots]
E --> F[Control UI clients]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +244, Tests +110. Total +354 across 15 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Retain one run-owned display projection while preserving the documented append and replacement event contract and the existing run cleanup lifecycle. Do we have a high-confidence way to reproduce the issue? Yes in source: current main re-normalizes a changed growing buffer, and the PR body reports a measured long-stream workload. This read-only review did not execute that workload. Is this the best way to solve the issue? Yes. The branch changes the existing chat-run owner, resets projection on corrections and changed media facts, and retains the canonical transforms for activated syntax. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 11ce05bac7c6. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (3 earlier review cycles) |
Preserve main’s bounded reply-tag parser and lastActivityAt maintenance ownership. Move projector cleanup fixture coverage into the dedicated chat-state maintenance suite. Keep display bytes and wire payload semantics unchanged.
|
Merged via squash.
|
* perf(gateway): project live chat displays incrementally * test(gateway): seed abort buffers through the run owner * perf(gateway): reuse prepared stream snapshots
What Problem This Solves
Long streaming answers can stall other chat sessions because display filtering repeatedly scans and allocates the entire growing reply, especially when the answer contains literal reply-marker examples.
User Impact
Concurrent Control UI replies use substantially less projection work while preserving displayed text, suppression, media handling, recovery snapshots, and event ordering. The Gateway wire payload, configuration, and database schemas are unchanged.
Why This Change Was Made
The existing chat-run owner now retains one incremental display chain and carries prepared append deltas through to delivery. Every stage reuses its canonical transform when syntax requires it; ordinary appends use incremental activation checks. Literal directive ownership is shared with the embedded stream's existing Markdown logic. Corrections, cap rollover, and changed managed-media facts reset the chain, and final reads cannot release unfinished tails into live state.
This replaces the full-source projection cache and separate broadcast prefix comparison. Production grows by 244 lines (+419/-175); tests grow by 110 lines (+139/-29). The additional stage and lifecycle state preserves the existing normalization contracts; the shared parser logic and superseded comparison paths were consolidated rather than retained in parallel.
Evidence
Isolated measurements used Node 26.9.0 on an Apple M3 Ultra under shared-host load; these are owner/handler benchmarks, not live Gateway latency claims.
Allocation sampling includes objects collected by minor and major GC. The append workload now grows approximately linearly. Observed process RSS across these paired measurements was 183–194 MB; that is not a peak or retained-memory comparison.
The real event-handler comparison used 50 synthetic runs with cumulative provider snapshots and unchanged JSON encoding. The 20k marker case fell from 82.28 s to 0.47 s; ordinary 40k snapshots took 17.47 s before and 15.62 s after, with unchanged serialization dominating. Host timing variance was substantial, so allocation and exact byte comparisons are the stronger evidence. Both comparisons produced identical event counts, serialized byte counts, and SHA-256 hashes. Separate differential checks against the original built normalization matched 7,054 boundaries, including 20k/40k/80k replies, split directives, runtime context, media, control tokens, and changing Markdown references.
Already-flat cumulative snapshots reuse their validated raw bytes: 40k/50-run projection took 292 ms versus 2,074 ms before. This path allocates 46.3 MB versus 29.3 MB before because of linear state bookkeeping.
Validation:
node scripts/run-vitest.mjs src/gateway/server-chat-state.projection.test.ts --maxWorkers=1— 9 passed, 23.85 s wall; 1.98 s test execution.node scripts/check-changed.mjspassed, including all 25 affected test typecheck graphs and policy guards; 4,544.37 s wall on the shared host.Validation used isolated owner and handler fixtures; no live Gateway was started or modified.
Merge validation
Merged main
3ed4948024e3a9df038db0f67b36f3491a604f07into the branch, preserving the bounded reply-tag reader and the lastActivityAt-based maintenance owner. The moved maintenance fixture now seeds and clears the run-owned display projector. Candidate:a3b3f0510608664148d51387747feee4f7552865. Display and wire contracts are unchanged.The focused projection, state, stream-merge, agent-event, control-reply, directive-parser, and moved maintenance suites passed: 388 tests, 464.29 s local wall time including cold worker compilation. A disposable differential probe matched canonical display bytes, suppression, reconstructed deltas, replacements, and final reads at 4,184 delta/snapshot boundaries. Its initial replacement fixture mixed item identities; correcting that fixture required no product change.
node scripts/check-changed.mjs --base origin/pr-158489-proof-basepassed on Blacksmith Testboxtbx_01m3dtgce2zrgykx1agv0snnrd, including all 25 affected test typecheck graphs, lint, formatting, and policy guards. Testbox run; delegated command 28m38s, exit 0, box stopped and verified completed. The backing Actions job can show cancelled because the one-shot lease owns shutdown. An external runner-portal sync timeout was a post-success warning.Fresh Codex autoreview raised one performance concern about full parsing after an active stripped directive. Rejected as a regression finding: main already reparses that path, the approved design retains the canonical transform when syntax is active, and later Markdown can change earlier directive ownership. No accepted actionable findings remain. The conflicts and retired activity-field fixture identified by ClawSweeper are resolved; no live Gateway was started or changed.