Repository navigation
perf(agents): avoid copying coalesced stream events - #184880
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review in progressClawSweeper is reviewing this revision. This supersedes any previous blocked status. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
e584191 to
03c348d
Compare
|
Admin-merged at the pinned head |
* perf(agents): avoid copying coalesced stream events * test(agents): narrow benchmark event properties
What Problem This Solves
Buffered tool-call repair copied every incoming stream delta before discovering that it could merge the delta into an already-owned event and discard the copy.
User Impact
Small allocation reduction during buffered text/reasoning streams, with unchanged stream content, ordering, timeout behavior, and source-event isolation. No configuration or migration is needed.
Why This Change Was Made
Copy an incoming event only when retaining a new buffer entry. Coalescing still mutates the buffer's owned snapshot and retains the latest partial; byte/event caps and replay are unchanged. Ordinary pass-through events already preserve identity.
Runtime LOC delta: 0 (+8/-8); the change removes an allocation without adding state, policy, or a second implementation. The regression adds 22 lines; the standalone owner benchmark adds 127. The non-test diff is +128 lines, entirely the benchmark and its required one-line Knip entry. The benchmark documents its usage in its header and has one exact Knip entry.
Evidence
Predeclared rule: lower allocation per event with stable CPU and identical output, measured separately with and without allocation sampling. Five alternating base/candidate pairs per workload and mode on macOS; each arm runs 128 turns × 4,096 deltas (524,288 deltas), with a 40-name tool matcher. Base:
e923309c2978268898f7b57a11879fdfa174ca38. Warmup: two turns. Sample interval: 4 KiB, including collected objects. One-minute host load ranged 49.7–69.6.These are arithmetic means across five pairs. Every buffered allocation pair improved; CPU pairs had mixed signs on the busy host, so the CPU means are context, not a demonstrated speedup. The reasoning path saves approximately one 48-byte event object per merged delta. This is a small owner-level win, not a whole-Gateway throughput or latency claim. No Gateway load test was run for this bounded redundant-copy deletion.
Reproduce with
node --expose-gc --import ./scripts/tsx.mjs scripts/perf/tool-call-stream-buffer-measure.ts --turns 128 --kind thinking --mode timing; repeat with--mode allocation,--kind text, and--kind passthrough. Use--ownerfor an unchanged source snapshot with intact relative imports. The script asserts exact output and unchanged source deltas, plus identity/order for pass-through events.The added regression fails on base with 2 input snapshots instead of 1, and passes with this change. It also checks coalesced content, latest partial, and unmodified input events. Focused proof: 109 tests passed across the normalizer, scrub-cache, attempt text-promotion buffering/sanitization, and idle-timeout files. Total wrapper wall time 113.57 s (primarily cold transforms); the changed normalizer and scrub-cache shard took 2.17 s.
Formatting, focused lint, export-name collision checks, and both requested import/PR-wrapper closure files passed (26 additional tests). Independent subagent review (autoreview route unavailable) found no actionable P0–P2 findings. The autoreview helper refused the requested provider because it did not match the operator configuration; no configuration was changed. The aggregate unused-file scan was not run under the lane's local-compute restriction; the standalone command is registered in Knip. The initial CI script-typecheck found a missing
typeproperty narrowing in the benchmark; that assertion is fixed and the targeted script typecheck passes. GitHub Codex review completed without findings on the exact current head,03c348dcab91156e5ede93a887fb4fd739795a00.CI blocker: 50 checks passed on this head. The only underlying failure is check-test-types-core-2: TS2345 in
src/auto-reply/reply/reply-agent-turn-preparation.test.ts:646, where the fixture supplies a partial mock instead ofReplyOperation. Aggregate CI gates fail because of that job. The failing test,reply-agent-turn-preparation.ts, andsrc/sessions/session-controller.tshave identical Git blobs on this head and its main base5c7ff7a8281b539ff198209e9c5fb80dba908360; none are changed by this PR. The same diagnostic occurred on the initial PR head. This attribution is based on source identity and CI diagnostics, not a separate baseline typecheck execution. CI is not green; the PR remains open and unmerged for the lead handoff.