Skip to content

perf(sessions): avoid serializing retained tool classifications - #183548

Merged
steipete merged 3 commits into
mainfrom
steipete/perf-mailbox-match-stable-stringify-20261025
Oct 25, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/perf-mailbox-match-stable-stringify-20261025

Conversation

@steipete

@steipete steipete commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Every retained session-entry publication serialized two temporary classification objects per retained tool-authority reader, even when only unrelated metadata changed. This created avoidable allocation during active turns.

User Impact

Reduces publication CPU and allocation without changing tool-authority classification, sandbox policy, or revocation behavior. No new configuration, retained cache, schema, or public API.

Why This Change Was Made

The existing classification predicate owns five facts: session ID, lifecycle revision, sandbox requirement, sandbox mode, and creator actor. Compare the scalar fields directly and compare admitted actor data structurally with prototype checks skipped. Key order remains irrelevant. Missing versus explicit undefined actor properties remain distinct; missing versus undefined top-level optional classification fields remain equivalent, as they were in the previous projection. Live caller authority and retained predicate lifecycle are unchanged.

Runtime production LOC: 8 added / 9 removed (net −1); benchmark +218 lines; tests exercise the retained publication contract. Deleted the classification projection and both serialization calls; no cache or alternate path. The separate lifecycle target collector is outside this change.

Evidence

Acceptance declared before measurements: identical publication outcomes, at least 10% owner allocation or CPU reduction, no material regression under absolute GC/tail thresholds, and net nonpositive production LOC. Five alternating, counterbalanced pairs per instrumentation mode, serial on macOS with Node 26.12.1. Workload: 48 synthetic active sessions, four retained classification readers/session, 10,000 warmup publications then 200,000 measured metadata publications/run (800,000 comparisons). All 192 retained readers remain current after every run. Neither side performs resets; this owner workload does not model reset boundaries.

Timings/GC are untraced. Allocation uses a separate in-process sampling heap profile at 32 KiB with collected objects included. Promotion uses a separate V8 trace pass with its own baseline. No forced collection in measured windows; no pause trimming or post-hoc exclusions. No A/A rejection controls were needed: total GC, promotion, mean latency and CPU all improved. Minor pause changes below 1 ms are context.

Metric (mean of five runs/side) Baseline Candidate Delta
Sampled bytes/publication 49336.266128 3611.761600 -45724.504528 (-92.68%)
CPU µs/publication 16.025726 3.534498 -12.491228 (-77.94%)
Wall µs/publication 16.149919 3.723785 -12.426134 (-76.94%)
GC ms/publication 0.000268 0.000023 -0.000245 (-91.31%)
Promoted bytes/publication (separate trace) 30.856832 29.333312 -1.523520 (-4.94%)
Publication p50 ms (200,000 samples/run) 0.014508 0.002808 -0.011700 (-80.64%)
Publication p95 ms (200,000 samples/run) 0.019500 0.005159 -0.014341 (-73.55%)
Publication p99 ms (200,000 samples/run) 0.032567 0.009550 -0.023017 (-70.68%)
Loop p95 ms (counts below) 1.951129 1.331404 -0.619725 (-31.76%)
Loop p99 ms (counts below) 2.536037 1.647410 -0.888627 (-35.04%)
Pair CPU ms A→B GC ms A→B Minor counts A→B GC max ms A→B Loop n A→B Loop p99 ms A→B
1 3119.64→642.85 50.289→4.511 333→27 0.344→0.326 1999→663 1.909→1.427
2 3179.28→677.75 55.049→4.337 333→26 0.388→0.262 1999→691 2.245→1.499
3 3390.35→716.98 60.524→5.131 333→27 0.711→0.348 1999→774 4.289→1.885
4 3207.55→640.53 50.939→4.346 333→27 0.517→0.244 1999→659 2.382→1.653
5 3128.90→856.38 51.110→4.959 333→27 1.205→0.311 1999→904 1.857→1.773

All untraced collections were minor; major/incremental/weak counts were zero on both sides, so major-GC behavior is unmeasured. Minor p95/p99 and candidate loop p99 are context where counts fall below 200/1,000. Both GC maxima are reported per pair above; none approaches the 50 ms safeguard. Sampled allocation beneath stringifyStableValue fell from approximately 8.76 GB/run to zero. All runs use identical work counts; wall-time tails remain noisy on the shared host.

Reproduce with node --import ./scripts/tsx.mjs scripts/perf/reply-classification-publication-measure.ts --output result.json; the script header documents separate allocation and promotion modes. Compare this script against the base and candidate source with five alternating pairs each.

Validation: baseline publication-equivalence suite passed (17 tests); after correcting the stored-actor fixture, all 17 cases also passed against the original comparator (37.40 s including preparation). Final candidate: node scripts/run-vitest.mjs src/auto-reply/reply/reply-tool-authority.snapshot.test.ts src/auto-reply/reply/reply-tool-authority.worker.test.ts --maxWorkers=1 passed 28 tests, 83.35 s total including compiler preparation; worker file duration 18.50 s. Changed-file oxfmt, oxlint (source and benchmark configs), and git diff --check passed. The export-name collision guard passed; test/scripts/eager-import-closure.test.ts and test/scripts/pr-wrapper-source-closure.test.ts passed all 22 tests (41.30 s including preparation). Initial declaration preparation caught the legacy boolean comparison option; fixed to the typed options object and repeated all measurements. Broad typechecking is deferred to PR CI under local compute limits.

Review: independent subagent review (autoreview route unavailable), scoped clean at P0–P2. The helper refused the requested route because it did not match the operator configuration; no configuration changed. Reviewed the frozen diff plus owner, actor-schema, and serializer context. Author simplification pass removed temporary projection/serialization machinery without adding cache state. This behavior-preserving refactor intentionally keeps equality tests green on the baseline; the owner allocation measurement exposes the old performance defect.

Limits: owner-level synthetic publication proof, not a full streaming Gateway/WebSocket benchmark or production speedup claim. No major collections occurred. Broad validation belongs to PR CI. After deployment, inspect collected-object allocation below retained classification matches during entry publication: its stable-stringify stack should disappear while session-policy changes continue to invalidate retained authority.

Leave ready for maintainer review; do not merge as part of this lane.

CI correction: the first check-test-types-core-2 run caught my fixtures using the projected actor's identity field instead of stored source/delegatedBy, plus an invalid Vitest it.each generic. Corrected both and passed the exact affected messaging test-type lane (node scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.core.test.messaging.json --incremental). Independent follow-up review of the corrected fixture and actual stored-actor type is clean. The corrected snapshot file passed all 17 tests (55.18 s including preparation); the unchanged worker suite had passed all 11 tests. The runtime comparator is unchanged.

All performance tables above were remeasured from scratch with valid stored actors after that correction. Earlier malformed-fixture results are superseded, not selectively trimmed: allocation 47,539→3,477 B/publication; six-pair CPU 15.857→4.158 µs/publication; loop p99 4.872→2.787 ms, including one adverse pair at 3.111→5.497 ms. The replacement five-pair workload has improving loop p99 in every pair, so no mixed-sign extra pair was needed.

Minor GC p95/p99 ms by pair (baseline → candidate; counts above):

Pair p95 p99
1 0.197 → 0.217 0.265 → 0.326
2 0.224 → 0.250 0.297 → 0.262
3 0.257 → 0.294 0.345 → 0.348
4 0.211 → 0.209 0.259 → 0.244
5 0.197 → 0.284 0.220 → 0.311

Allocation and promotion spread (five separate pairs; 200,000 publications/side/pair):

Pair Sampled bytes/publication A → B Promoted bytes/publication A → B Traced GC counts A → B
1 49145.13 → 3620.02 30.856 → 29.333 333 → 27
2 49287.10 → 3623.23 30.861 → 29.332 333 → 26
3 49457.58 → 3598.74 30.856 → 29.334 333 → 27
4 49415.45 → 3603.03 30.856 → 29.334 333 → 27
5 49376.07 → 3613.79 30.856 → 29.334 333 → 27

The next CI run passed 58 checks, including all core test-type shards, production types, Node/UI tests, and guards; its remaining failure was script noUncheckedIndexedAccess on the benchmark's fixed-array modulo lookup. Added the erased non-null assertion after independent review of the bounded index. Lint/format pass; runtime code and benchmark work are unchanged by the erased type annotation, so the measurement tables remain applicable. No CI reruns were requested merely to chase green.

Final CI: run 40189869394 completed successfully for exact head d28099a37730020170c7860f72c94866876bab06. GitHub code review also completed on that head with no findings. This PR is intentionally left ready and unmerged for the lead.

@clawsweeper

clawsweeper Bot commented Oct 25, 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 25, 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-25T11:42:35.651035Z d28099a 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: M labels Oct 25, 2026
@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. labels Oct 25, 2026
@steipete
steipete force-pushed the steipete/perf-mailbox-match-stable-stringify-20261025 branch from 48b3558 to f029328 Compare October 25, 2026 11:29
@steipete
steipete force-pushed the steipete/perf-mailbox-match-stable-stringify-20261025 branch from f029328 to d28099a Compare October 25, 2026 11:40
@steipete
steipete merged commit 27e7e5e into main Oct 25, 2026
143 of 145 checks passed
@steipete
steipete deleted the steipete/perf-mailbox-match-stable-stringify-20261025 branch October 25, 2026 11:56
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 26, 2026
…claw#183548)

* perf(sessions): avoid serializing retained tool classifications

* test(sessions): use stored actors in classification fixtures

* test(perf): type the bounded classification fixture lookup
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. scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant