Skip to content

perf(agents): avoid copying coalesced stream events - #184880

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-stream-event-pipeline-per-event-20261026
Oct 26, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-stream-event-pipeline-per-event-20261026

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

Owner workload Sampled bytes/delta, base → candidate Allocation delta Main-thread CPU ms/arm, base → candidate
Buffered reasoning 1,238.89 → 1,191.17 −3.85% 270.52 → 262.98 (−2.79%)
Buffered text 4,572.00 → 4,528.09 −0.96% 740.59 → 718.76 (−2.95%)
Unchanged prose control 1,990.60 → 1,989.37 −0.06% 402.80 → 401.77 (−0.26%)

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 --owner for 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 type property 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 of ReplyOperation. Aggregate CI gates fail because of that job. The failing test, reply-agent-turn-preparation.ts, and src/sessions/session-controller.ts have identical Git blobs on this head and its main base 5c7ff7a8281b539ff198209e9c5fb80dba908360; 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.

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review in progress

ClawSweeper is reviewing this revision. This supersedes any previous blocked status.

View the workflow run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T18:33:16.172348Z 03c348d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: S labels Oct 26, 2026
@steipete
steipete force-pushed the steipete/perf-stream-event-pipeline-per-event-20261026 branch from e584191 to 03c348d Compare October 26, 2026 18:30
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@steipete
steipete merged commit b4f7a1d into main Oct 26, 2026
116 of 121 checks passed
@steipete

Copy link
Copy Markdown
Contributor Author

Admin-merged at the pinned head 03c348dcab91. The only failing job is check-test-types-core-2 (plus ci-gate): TS2345 in src/auto-reply/reply/reply-agent-turn-preparation.test.ts:646. It is inherited from main commit bd6807a336d6, reproduces on the unchanged parent, and is outside this PR's files. The test-only fix #184907 is queued with auto-merge. Every other job passed or was skipped; equivalence was reviewed by the TS session before merge.

@steipete
steipete deleted the steipete/perf-stream-event-pipeline-per-event-20261026 branch October 26, 2026 18:50
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
* perf(agents): avoid copying coalesced stream events

* test(agents): narrow benchmark event properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. scripts Repository scripts size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant