Skip to content

perf(memory): share published tool checkpoint descriptors - #199609

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-duplicate-string-owners-20261115
Nov 15, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-duplicate-string-owners-20261115

Conversation

@steipete

@steipete steipete commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Each new session tool checkpoint recreated a descriptor for every published tool. Transcript admission then detached identical name/description strings again, even though the parameter schema was already shared. In a synthetic 1,000-session/100-tool composition, this retained 100,002 three-field descriptor objects and 14.23 MiB of duplicate description excess.

User Impact

Lower memory use when many retained sessions share tool definitions, with unchanged checkpoint JSON and provider payloads. The owner benchmark saves about 18.2 MiB of collected heap at 1,000 sessions, 10.7% of allocation and 21.5% of CPU in tool preparation/checkpointing. This is a bounded improvement to one producer, not a claim to fix all duplicate strings or an uptime leak.

Why This Change Was Made

Descriptor creation now has one weak slot per existing immutable schema publication. Matching name/description reuse the admitted descriptor; changed metadata replaces the slot, and a new schema has a separate slot. Unpublished mutable schemas retain ordinary transcript admission. String detachment still occurs once, preserving lone surrogates and preventing a small description from retaining a large source buffer. No execution, policy, or authorization result is cached.

Both callers use the same descriptor owner. The old function is removed. Runtime production delta is +21 lines: the weak lifetime and single admission require growth, and the existing prompt-state module's enforced line-cap ratchet requires the small sibling module. Benchmark usage stays in its header. Tests add 27 lines with an exact allocation. No schema, persisted-shape, configuration, dependency, or public SDK change.

Evidence

Five alternating fresh-process repetitions per side on Node 26.12.1. Each process retains 1,000 sessions, 100 published tools, 10,000 compact subagent records, and performs 3,000 prepare/checkpoint operations: 15,000 iterations per side. Exact V8 allocated-byte counters and main-thread CPU; heap capture/collection outside timed windows. Baseline uses the unchanged owner through the existing sparse benchmark overlay. Provider payload hashes and canonical-schema identities match throughout.

Mean per 3,000 preparations Before After Delta
Owner allocated bytes 1,534,563,152 1,370,082,325 -10.7%
Owner main-thread CPU 758.00 ms 595.30 ms -21.5%
Whole composition allocation 2,808,960,762 B 2,643,127,602 B -5.9%
Whole composition CPU 1,923.21 ms 1,719.29 ms -10.6%
Collected heap 220,980,195 B 201,849,061 B -19,131,134 B
Descriptor-dominated retained union 19,202,776 B 38,776 B -99.8%
Duplicate description excess 14,921,864 B 550,664 B -96.3%

Heap evidence comes from scripts/heap-snapshot-diff.mjs --summary plus a retainer walk and an exact three-field descriptor-node group. The union excludes shared descendants; duplicate excess excludes sliced/concatenated and truncated strings. At 24 sessions, description excess is 867,464 → 550,664 B; at 1,000 sessions, 14,921,864 → 550,664 B. The removed base slope is 14,400 bytes per added retained session for 100 tools. Unchanged extra turns in one already-pinned session do not add another surface. Remaining descriptor objects include source fixture and canonical publication objects.

Reproduce owner proof:

node --expose-gc --import ./scripts/tsx.mjs scripts/perf/duplicate-tool-strings-measure.ts .local/tool-descriptors 1000 0
# Append --no-snapshot for CPU/allocation repetitions.

Regression: shares published descriptors across sessions without sharing changed or mutable definitions fails on base at the cross-session identity assertion (34 existing tests pass), and passes with the change. It covers changed name/description/schema and mutable input isolation; existing checkpoint tests cover serialization and ownership. Focused prompt-state, prompt-prefix stability, transcript normalization, prompt-state consumers, export-name and eager/source-closure guards, and strict root-script/agents-other typechecks are exercised. The initial sibling extraction's incorrect package subpath was caught by compilation and corrected to the existing internal tool-schema contract.

Local lint's only remaining source violation is inherited file size: the original prompt-state module was already 743 counted lines against a 700 limit. This change reduces it; no suppression or threshold change. The commit ratchet enforces that reduction, while CI's existing policy makes size warnings advisory.

Additional local built-Gateway retention diagnostics used 10,000 seeded sessions, 512 shared skills, four agents, 41 configured plugin entries, 65 WebSocket clients, mock-provider/tool/subagent turns, cron, and list/history/search churn. Each measured window lasted about 362 seconds, under the lane's focused fast-path exception. These are diagnostic context, not matched timing gates: the baseline reused state after an earlier RPC-coverage setup failure; candidate ran alongside local compilation/checks at higher load. Retention sampling includes forced collections. No controlled Gateway speedup or regression is claimed, and no A/A rejection decision is made.

Gateway diagnostic Base Candidate
Completed turns 140 132
Main-thread CPU 48.53% 53.32%
GC seconds/min 5.83 6.58
Event-loop p99 53.77 ms (15,650 samples) 80.94 ms (14,572)
Client sessions.list p99 228.28 ms (4,657) 287.18 ms (4,524)
Server sessions.list handler p99 44.29 ms (4,657) 66.56 ms (4,524)
Final collected heap / RSS 496.82 / 3,090.15 MiB 499.40 / 3,127.61 MiB
Final host load, 1/5/15 min 10.94 / 9.77 / 9.07 12.03 / 12.73 / 10.78
Allocation rate unavailable in retention mode unavailable in retention mode

The retention-mode survivor sampler is deliberately not reported as an allocation rate; exact allocation evidence is the owner benchmark above. Warm/end snapshots were captured in both diagnostics. On base, retained graph grew 482,865,662 → 580,225,884 B and duplicate excess 25,181,104 → 32,056,432 B; this includes compiler/schema/skill state beyond this descriptor owner. Candidate retained graph grew 482,442,808 → 583,079,501 B and duplicate excess 25,337,824 → 32,333,288 B. The unexplained production encoded group remains unassigned, and Node ResolveCache strings are untouched. Independent residual skill-source objects and TypeBox literal descriptors are follow-ups, not included fixes.

Final-head built-Gateway MCP proof also passed: 65 clients, 40 plugin entries, two mock MCP servers × 40 tools, both catalogs consumed by four concurrent sessions, and 20 measured turns with 20 tool calls/results across five rounds.

Exact-head CI is green for 929431cebc85855299d57e0ab25e485ab73a3f0b: 61 passed checks, 57 skipped, none failed or pending. CI run. Review/merge remains with the coordinating maintainer as requested; no self-autoreview or merge.

@clawsweeper

clawsweeper Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T12:54:03.666735Z 929431c PR opened
ℹ️ 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 agents Agent runtime and tooling size: S labels Nov 15, 2026
@steipete
steipete merged commit 2e1b619 into main Nov 15, 2026
150 of 152 checks passed
@steipete
steipete deleted the steipete/perf-duplicate-string-owners-20261115 branch November 15, 2026 13:09
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling scripts Repository scripts size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant