Skip to content

perf(models): reduce catalog worker standing heap - #164428

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-model-catalog-worker-heap-20260919
Oct 3, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-model-catalog-worker-heap-20260919

Conversation

@steipete

@steipete steipete commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Reduces standing model-catalog worker heap retained by recursively serialized plugin metadata and redundant raw static-catalog results.

User Impact

Catalog preparation retains less memory and reuses the prepared static inventory during discovery fallback. Provider selection, authored model overrides, and metadata-generation invalidation stay with their existing owners. No configuration or migration is required.

Why This Change Was Made

Worker heap snapshots traced serialized manifest subtrees to auto-enable's process-lifetime stableFingerprintMemo. Its recursive WeakMap already trusted immutable object identity; compare the prepared candidate/plugin array identities directly and keep the existing same-turn and metadata-lifecycle invalidation.

Static preparation now owns only normalized provider configurations. Discovery consumes them directly, including fallback after an absent live result, instead of retaining and recopying raw hook results or rerunning an already prepared static hook. The duplicate provider remapping map is removed.

Production TypeScript: −36 LOC. The requested non-test diff is 23 insertions / 60 deletions (−37 lines), including one generated, shrink-only assertion-baseline row.

Evidence

  • Linux Blacksmith Testbox tbx_01m41a0ebcsxp8r4n5vdvmggy3, profile openclaw-check, Node 24.19.0.
  • Baseline 8e4f1533a300 and the reviewed patch, synthetic built Gateway: two agents, twelve configured providers, 302 returned models, 85,048-byte models.list payload; repeated reads and explicit refreshes.
  • Baseline worker forced-GC heap: 156,539,864 → 156,889,384 bytes over the two captures. Worker snapshots are captured through Node's private inspector channel, separately from the main isolate.
Measurement Before After
Worker heap after GC, first preparation 149.29 MiB 141.36 MiB
Worker heap after GC, repeated refreshes 149.62 MiB 141.69 MiB
Memo-retained serialization strings 7,669,416 B / 41,158 nodes 0 B / 0 nodes
Model rows / response bytes 302 / 85,048 302 / 85,048

The steady worker reduction is 7.93 MiB (5.3%), consistent across both captures. The catalog itself is small; the remaining footprint includes the loaded runtime/plugin graph (large source strings, compiled code, plugin file/metadata cache, and Node's module loader). No growing history of complete catalogs appeared across this run.

  • Regression failed before the fix with 3,072 raw model-ID reads for 1,024 rows across preparation and two implicit reads; after the fix it reads each raw row once. Four focused files passed, including authored overrides, empty/scoped catalogs, and prepared static fallback.
  • pnpm build: baseline and final passed (8m00s and 7m15s). Final auto-enable and plugin-context validation passed 53 tests across three files. Independent review: scoped-clean, no actionable P0/P1 findings.
  • node scripts/check-changed.mjs --staged: passed, including all 27 core test typecheck graphs, formatting, lint, boundary/dead-export checks, and the assertion ratchet. The final gate plus timing/retention runs completed in 24m22s. The first gate exposed three obsolete fixture fields; the next required the serializer's removed assertion baseline to shrink. Both were corrected, and no gate was waived.
  • Focused behavior coverage: 105 tests across 11 files passed, including the existing repeated-generation and alternating-workspace heap regressions. The two retention tests kept one callback across retired generations and two payloads across alternating workspace preparations.
  • Final review of all 11 changed files: scoped-clean, no actionable P0/P1 findings.
  • Rebased unchanged onto 55e3daa31530 before publication; git range-diff reports an identical patch and all changed code/test files are byte-identical. Hosted CI validates the published head.

Per-file pnpm test <file> --maxWorkers=1 wall time (same Testbox):

Changed test Wall time Peak RSS
plugin-auto-enable.core.test.ts 20.44 s 5,810,812 KiB
provider-discovery.catalog-copy-work.test.ts 7.65 s 685,796 KiB
models-config.providers.endpoint.test.ts 8.26 s 682,348 KiB
models-config.providers.implicit.discovery-scope.test.ts 8.88 s 968,524 KiB

Times include the repository runner/import/compiled-worker setup; test bodies remain below 5 seconds per file. The remaining changed test files only remove obsolete fixture fields. Hosted CI run 37141536635 passed on 6d900200f1e9: 65 successful jobs and 23 skipped jobs, 458 seconds elapsed. The 27 selected Node test jobs totaled 4,395 job-seconds (aggregate job duration, including setup; not per-file or incremental test cost). All PR checks were complete at verification: 90 passing, 47 skipped, none failing or pending.

Limitations: synthetic unauthenticated catalog workload; no live provider requests or production restart. Snapshot memory includes the worker's loaded code/plugin graph, not just catalog JSON. The Testbox uses Node 24, so absolute heap is not directly comparable to a Node 26 deployment.

Use generation-owned metadata array identities for same-turn auto-enable
reuse instead of retaining serialized copies of every manifest subtree.
Keep normalized static provider catalogs as the sole prepared result and
reuse them for discovery fallback.

A synthetic Linux dist Gateway with two agents and 302 model rows reduces
catalog worker post-GC heap from 149.62 MiB to 141.69 MiB (5.3%). The removed
memo retained 7,669,416 bytes across 41,158 serialization string nodes.
@clawsweeper

clawsweeper Bot commented Oct 3, 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.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Oct 3, 2026
@steipete
steipete enabled auto-merge (squash) October 3, 2026 17:45
@clawsweeper

clawsweeper Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 3, 2026, 1:46 PM ET / 17:46 UTC.

ClawSweeper review

What this changes

The PR reduces model-catalog worker memory by comparing metadata snapshot identities and reusing normalized static provider catalogs, with regression coverage for fallback and authored overrides.

Merge readiness

✅ Ready for maintainer review

This PR remains useful: current main still retains the serialization memo and redundant raw catalog results. No introduced correctness defect was found, and the reported Gateway benchmark supports the optimization.

Likely related people: steipete and vincentkoc are medium-confidence routing candidates based on prior merged area work.

Priority: P2
Reviewed head: 6d900200f1e9bb644c6604b308d1ad0d1f79496c

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused simplification with meaningful Gateway heap measurements and targeted regression coverage; no blocking defect was found.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied before/after built-Gateway models.list and refresh workload exercises catalog preparation and worker retention, showing lower forced-GC heap with unchanged inventory output. Synthetic provider data is appropriate for this internal memory change; no persisted-data format changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The supplied before/after built-Gateway models.list and refresh workload exercises catalog preparation and worker retention, showing lower forced-GC heap with unchanged inventory output. Synthetic provider data is appropriate for this internal memory change; no persisted-data format changes.
Evidence reviewed 10 items Pinned patch and merge ownership: Reviewed the merge-base-to-head introduced diff. The raw test-merge commit records current main followed by the exact PR head as its parents; its production delta matches the intended optimization and does not remove base-only update work.
Optimization still absent from main: Current main retains stableFingerprintMemo and recursive metadata serialization. The three changed production modules have no base-branch changes since the pinned merge base.
Latest release retains the memo: The v2026.9.8 source also contains stableFingerprintMemo and the recursive serializer, so the latest supplied release does not already provide this optimization.
Findings None None.
Security None None.

How this fits together

OpenClaw prepares model inventories from plugin metadata, provider catalogs, and user configuration. The catalog worker combines these sources into model listings and agent-specific model selection.

flowchart TD
  A[Plugin metadata and user configuration] --> B[Plugin auto-enable cache]
  B --> C[Static catalog preparation]
  C --> D[Normalized provider inventory]
  E[Live provider discovery] --> F[Scoped discovery and static fallback]
  D --> F
  F --> G[Authored model overrides]
  G --> H[Model listings and agent selection]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -36, Tests +35, Other -1. Total -2 across 11 files.

View PR surface stats
Area Files Added Removed Net
Source 3 23 59 -36
Tests 7 120 85 +35
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 0 1 -1
Total 11 143 145 -2

Review metrics

Metric Value Why it matters
Reported worker heap reduction 7.93 MiB lower after GC (5.3%) The built Gateway benchmark reports unchanged model rows and response size across repeated refreshes.

Technical review

Best possible solution:

Keep one generation-owned normalized static inventory and use metadata identities with existing lifecycle invalidation.

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

Yes: current-main source retains recursive serialization strings and recopying paths, and the supplied built-Gateway workload measures their heap cost. This review did not execute that workload.

Is this the best way to solve the issue?

Yes: the patch removes redundant retained representations while preserving lifecycle clearing, provider filtering, and authored override ownership.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This is a measured, bounded catalog-worker memory improvement without evidence of an urgent user-facing outage.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The supplied before/after built-Gateway models.list and refresh workload exercises catalog preparation and worker retention, showing lower forced-GC heap with unchanged inventory output. Synthetic provider data is appropriate for this internal memory change; no persisted-data format changes.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.

Label justifications:

  • P2: This is a measured, bounded catalog-worker memory improvement without evidence of an urgent user-facing outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • proof: sufficient: Contributor real behavior proof is sufficient. The supplied before/after built-Gateway models.list and refresh workload exercises catalog preparation and worker retention, showing lower forced-GC heap with unchanged inventory output. Synthetic provider data is appropriate for this internal memory change; no persisted-data format changes.

Evidence

What I checked:

  • Pinned patch and merge ownership: Reviewed the merge-base-to-head introduced diff. The raw test-merge commit records current main followed by the exact PR head as its parents; its production delta matches the intended optimization and does not remove base-only update work. (116d708958b2)
  • Optimization still absent from main: Current main retains stableFingerprintMemo and recursive metadata serialization. The three changed production modules have no base-branch changes since the pinned merge base. (src/config/plugin-auto-enable.apply.ts:27, fc7e71bad258)
  • Latest release retains the memo: The v2026.9.8 source also contains stableFingerprintMemo and the recursive serializer, so the latest supplied release does not already provide this optimization. (src/config/plugin-auto-enable.apply.ts:28, fc23bc864e45)
  • Cache lifecycle remains bounded: Identity comparison retains config, environment, registry, discovery, and ambient-policy partitioning. Same-turn clearing and metadata lifecycle clearing remain intact; metadata snapshot finalization recursively freezes prepared facts. (src/config/plugin-auto-enable.apply.ts:118, 6d900200f1e9)
  • Prepared fallback preserves filtering and overrides: Prepared normalized configs replace raw results only at static selection or absent-live-result fallback. Provider scope and endpoint eligibility still filter accepted entries, and the existing merge owner preserves authored fields without mutating the prepared catalog. (src/agents/models-config.providers.implicit.ts:274, 6d900200f1e9)
  • Focused regression coverage: The changed tests exercise prepared versus unprepared static fallback, metadata-array replacement, authored model/header overrides, and a subsequent unchanged agent read. The copy-work regression extends the existing preparation boundary to implicit reads and asserts one raw read per model. (src/plugins/provider-discovery.catalog-copy-work.test.ts:106, 6d900200f1e9)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • vincentkoc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 2c8254d into main Oct 3, 2026
141 checks passed
@steipete
steipete deleted the steipete/perf-micro-model-catalog-worker-heap-20260919 branch October 3, 2026 17:51
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 4, 2026
Use generation-owned metadata array identities for same-turn auto-enable
reuse instead of retaining serialized copies of every manifest subtree.
Keep normalized static provider catalogs as the sole prepared result and
reuse them for discovery fallback.

A synthetic Linux dist Gateway with two agents and 302 model rows reduces
catalog worker post-GC heap from 149.62 MiB to 141.69 MiB (5.3%). The removed
memo retained 7,669,416 bytes across 41,158 serialization string nodes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant