Skip to content

perf(agents): reuse retained event routing snapshots - #184085

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-agent-event-enrichment-20260919
Oct 26, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-agent-event-enrichment-20260919

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Streaming agent events allocate a fresh 13-field routing snapshot on every delta, even while the run owner is unchanged.

User Impact

A small allocation reduction during streamed replies, with identical events, progress, cancellation routing, and ownership checks. No configuration or migration changes.

Why This Change Was Made

Reuse the existing retained routing object for the same exact run owner, refreshing all its fields as before. A replacement owner still gets a fresh snapshot. This removes one temporary object per event without adding a cache, invalidation mechanism, or alternate dispatch path. The retained snapshot remains necessary for late settlement after the live context is cleared. Production LOC delta: 0 (15 added / 15 removed).

Progress capture already keeps unchanged retained event objects by identity. Its existing trusted-freeze change (#183914) is present in the evidence build, so remaining progress serialization cost is residual; this PR does not claim to eliminate it or change its wire-size and mutation-isolation contract.

Evidence

Acceptance declared before measurement: identical emitted public frames and ownership/cancellation behavior, one fewer routing allocation per streamed event, and a consistent small allocation or CPU win. Benchmark: eight interleaved synthetic runs, 64 subscribers, 98% assistant deltas and 2% tool start/results; 2,000 warmup events then 20,000 measured events. Five counterbalanced base/candidate pairs against 29ed214d9e2b21895a3b3000a6344046cc59acc6. CPU and sampled allocation use separate processes/passes.

Metric Base Candidate Change
Mean sampled allocation, bytes/event 2,967.90 2,890.69 −2.60%
Allocation range, bytes/event 2,951.84–2,996.67 2,885.48–2,896.60 Every candidate lower
Mean untraced CPU, µs/event 3.5039 3.5721 +1.95%, no CPU win claimed
Median untraced wall time, ms/20k events 49.35 51.19 +1.84 ms/batch
Mean end-of-pass heap, MiB 46.56 44.75 −1.81 MiB (context only)
Mean end-of-pass RSS, MiB 168.43 171.34 +2.91 MiB (context only)

CPU pairs (base → candidate, µs/event): 3.4457→3.6063, 3.5411→3.4708, 3.4500→3.5144, 3.4635→3.6352, 3.6193→3.6338. Host load was 56.7–61.3. The baseline had a 159.48 ms wall-time outlier; it is retained in the data, and the lower mean wall time is not claimed as a speedup. All 20 runs produced the same public-event hash and 1,280,000 measured subscriber deliveries.

Run:

node --expose-gc --import ./scripts/tsx.mjs scripts/perf/agent-event-routing-measure.ts --events 20000
node --expose-gc --import ./scripts/tsx.mjs scripts/perf/agent-event-routing-measure.ts --events 20000 --allocation

The identity regression fails on base because the routing snapshot is replaced. Candidate: 50/50 focused tests pass in agent-events.test.ts and agent-run-registry.changes.test.ts (7.33 s wrapper wall time; Vitest 4.93 s). Existing tests cover compaction across nested scopes, cancellation, generation handoff, hidden routing, reused run IDs, and stale-owner rejection.

This is an owner microbenchmark, not a whole-Gateway load test. Under the scoped fast-path instruction for allocation deletion, no ten-minute stress campaign was run: no scheduling, admission, delivery, or progress semantics change. Whole-Gateway CPU, GC s/min, loop/list p99, and server handler latency remain unmeasured. No production Gateway was loaded and no remote test lease was allocated. PR CI owns broad proof.

Review: independent subagent review (autoreview route unavailable), clean through P2. The helper refused the configured provider route; no operator configuration was changed. Formatting, targeted core/script lint, and the export-name collision guard pass. Author simplification pass retained only same-owner snapshot reuse, its regression, and the rerunnable owner benchmark.

Required closure checks: eager-import-closure.test.ts and pr-wrapper-source-closure.test.ts, 22/22 passed (50.07 s wrapper wall time). No test files or production exports were added.

Targeted infra TypeScript check passed: node scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.core.test.infra.json --incremental. Before push, rebased onto 7e15500187cd1430cf954ad43e2fd4fb89ac1dd2; relevant event/registry source and tests were unchanged from the measured base, so the patch and proof inputs are unchanged.

CI and inherited failure

Exact-head CI run 40257428681 passed 54 jobs and failed only checks-node-changed-compact-small-20 plus its aggregate gate. The failing case is server.subagent-settle-replay.harness.test.ts → recovers public work once through a sibling and reconciles its private child final: server.subagent-settle-replay-recovery.test-support.ts:459 expects ok/completed but receives error/cancelled.

Ran the complete failing file, once per revision, through node scripts/run-vitest.mjs src/gateway/server.subagent-settle-replay.harness.test.ts --maxWorkers=1:

Revision Result Wrapper wall time
PR 2470a2102321aedacaccbfbcb76ce89f0490412e 22 pass / 1 fail, exact CI assertion 215.24 s
Unchanged main parent 7e15500187cd1430cf954ad43e2fd4fb89ac1dd2 22 pass / 1 fail, identical assertion and outcome 176.09 s

CI tested merge e5a368be against main ebbb03b14935796e54e4a6410b90fb7b26f4fa2f; the intervening main changes do not touch the recovery implementation, test, or event-routing owner. This is a reproducible inherited failure, not a passing-retry classification. No CI rerun was requested. The separate dependency-review and security-sensitive-review statuses passed; automated code review completed on the exact head with no findings. Landing uses the standing authorization for attributed inherited failures; CI is not described as green.

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

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review October 26, 2026 02:06
@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-26T02:08:14.982330Z 2470a21 Draft marked ready
ℹ️ 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
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR refreshes the existing retained routing snapshot for the same run owner instead of allocating a replacement on every event.

Example: The reported benchmark emits 20,000 events across eight runs to 64 subscribers.

  • Before: Mean sampled allocation is 2,967.90 bytes per event.
  • After: Mean sampled allocation is 2,890.69 bytes per event, with no CPU improvement claimed.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The production change is focused and appears correct; evidence is limited to an owner benchmark and focused tests, with implementation-coupled assertions adding little value.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored allocation optimization is outside the ordinary contributor proof gate; its reported benchmark exercises registration, event enrichment, and runtime subscribers through the production owner, rather than a shipped Gateway entry point.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Streaming replies repeatedly allocate unchanged routing metadata, adding avoidable allocation pressure.
Reason: The feature-history owner proposes a narrow allocation reduction, documents the CPU tradeoff, and retains existing routing and cancellation behavior.

Merge readiness

✅ Ready for maintainer review

Keep open: this focused, maintainer-directed optimization removes a verified per-event allocation without an identified production regression; required CI remains red with unresolved attribution.

Priority: P3
Reviewed head: 2470a2102321aedacaccbfbcb76ce89f0490412e

Before merge

None.

Findings

None.

Tests

  • Low-value test src/infra/agent-events.test.ts: Remove the newly added routingByRun object-identity assertions: they pin a private representation, while the benchmark measures allocation and the existing surrounding test already protects compaction and reused-run isolation.
  • Missing end-to-end proof: The supplied evidence exercises the real event owner in process, but does not demonstrate streamed delivery or cancellation through a running Gateway.
Agent review details

How this fits together

The agent-event owner combines registered run metadata with execution-scoped retained facts, then emits sequenced events to runtime subscribers, including after live context cleanup.

flowchart LR
  A[Run registration] --> B[Live run context]
  B --> C[Retained routing snapshot]
  D[Agent stream events] --> E[Event admission and enrichment]
  B --> E
  C --> E
  E --> F[Runtime subscribers]
Loading

Technical review

Best possible solution:

Reuse the existing routing snapshot only for the exact same registration while preserving all field refreshes and replacement-owner isolation.

Do we have a high-confidence way to reproduce the issue?

Current-main source allocates the 13-field snapshot on every accepted event with a live context; the PR supplies a rerunnable owner benchmark, which this read-only review did not execute.

Is this the best way to solve the issue?

The production change is the smallest complete optimization: it reuses the existing owner and retained storage without adding a cache, alternate dispatcher, or invalidation mechanism.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against c04f19f3befd.

Provenance checked

  • Execution-scoped retained routing and terminal-publication state keeps the original intent (7546356: Retain exact-registration routing after cancellation clears live context; a81b9cd subsequently retained shared event state to prevent duplicate terminal publication.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 7546356 adds src/infra/agent-event-execution-context.ts:38 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 754635680ae6; files: src/infra/agent-event-execution-context.ts)

PR surface

Source 0, Tests +11, Other +124. Total +135 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 15 15 0
Tests 1 11 0 +11
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 124 0 +124
Total 3 150 15 +135

Labels

Label changes:

  • add P3: This is a small allocation optimization with unchanged intended behavior and no demonstrated urgent user-facing failure.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P3: This is a small allocation optimization with unchanged intended behavior and no demonstrated urgent user-facing failure.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 25, 2026, 10:22 PM ET / October 26, 2026, 02:22 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

Baseline attribution for the remaining CI red: the private-child recovery case in src/gateway/server.subagent-settle-replay.harness.test.ts fails identically on PR head 2470a2102321aedacaccbfbcb76ce89f0490412e and unchanged main parent 7e15500187cd1430cf954ad43e2fd4fb89ac1dd2. Both full-file runs have 22 passing tests and the same failure at server.subagent-settle-replay-recovery.test-support.ts:459: expected ok/completed, received error/cancelled (215.24 s head / 176.09 s base).

The PR body records commands and CI run 40257428681. The routing regression, focused owner tests, typecheck, lint, export and closure guards pass; independent and automated reviews have no findings, and security/dependency reviews pass. Proceeding under the maintainer's standing inherited-failure landing instruction, without rerunning CI or claiming the failed gate is green.

@steipete
steipete merged commit d4da279 into main Oct 26, 2026
227 of 231 checks passed
@steipete
steipete deleted the steipete/perf-micro-agent-event-enrichment-20260919 branch October 26, 2026 02:26
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant