Repository navigation
fix: reduce Doctor memory growth on large agent fleets - #180792
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex review: needs maintainer review before merge. What this changesThis PR bounds reusable plugin metadata snapshots in Doctor and the shared metadata cache to 64 recently used entries. Example: Doctor traverses 400 agent workspaces in the supplied synthetic workload.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Partial Merge readiness✅ Ready for maintainer review Keep this PR open as a useful, measured repair for fleet-scale memory retention; no actionable introduced code defect was found. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherDoctor resolves plugin metadata for configured agent workspaces; lifecycle-owned caches supply immutable snapshots to checks and retain reusable discovery facts. flowchart TD
A[Doctor CLI] --> B[Configured agent workspaces]
B --> C[Doctor snapshot scope]
C --> D[Shared metadata cache]
D --> E[Plugin registry discovery]
D --> F[Immutable snapshots]
F --> G[Doctor checks]
Technical reviewBest possible solution: Bound reusable views in the existing cache owners while preserving active snapshots, shared registry reuse, and lifecycle invalidation. Do we have a high-confidence way to reproduce the issue? Current-main source retains each workspace snapshot in unbounded maps, and the supplied reproducible workload quantifies that growth; no reproduction was executed during this read-only review. Is this the best way to solve the issue? The patch is a small, coherent repair using existing owners: both retaining maps are bounded, all shared-cache access uses the same recency policy, and no new persistence or configuration is introduced. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 1bd44907beb1. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +24, Tests +43, Docs +3, Other +49. Total +119 across 6 files. Added test files: 0. View PR surface stats
LabelsLabel changes:
Label justifications:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale6/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. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed October 22, 2026, 1:00 PM ET / 17:00 UTC. |
…fleets (openclaw#180792) Doctor retained every workspace's full plugin metadata snapshot in both the shared cache and its workspace lookup for the whole run, so heap grew linearly with agent count (openclaw#180690: OOM at 6 GB on ~810 agents). Both owners now retain at most 64 recent snapshots while preserving active references and reusing the fleet registry; cache misses reload through the existing paths. Measured at 400 agents: retained heap 503 -> 166 MiB, duration flat. Partial fix for the contributions-phase growth; initial discovery peak and config-flow serial DB maintenance remain. Refs openclaw#180690, reported by @609NFT.
…fleets (#180792) Doctor retained every workspace's full plugin metadata snapshot in both the shared cache and its workspace lookup for the whole run, so heap grew linearly with agent count (#180690: OOM at 6 GB on ~810 agents). Both owners now retain at most 64 recent snapshots while preserving active references and reusing the fleet registry; cache misses reload through the existing paths. Measured at 400 agents: retained heap 503 -> 166 MiB, duration flat. Partial fix for the contributions-phase growth; initial discovery peak and config-flow serial DB maintenance remain. Refs #180690, reported by @609NFT. Release adaptation: the bounded snapshot cache accessors live in plugin-cache.ts (the cache owner) because the baseline plugin-metadata-snapshot.ts would exceed its max-lines cap. The baseline's async config-wide resolver has a different install-index preparation shape, so only its cache read was switched to the bounded accessor. Dropped the main-only perf script (it relies on a disposable Doctor scope the baseline lacks), and the fleet test uses the baseline's non-disposable scope. Docs keep only the bounded-retention paragraph. (cherry picked from commit d2c70dd)
Adapt 21 audited fixes to release-native ownership and recovery. Retain strict temporary environment isolation and provider-only cooldowns. Original-source provenance and contributor credit: #179144 5a97c90 — Peter Steinberger <[email protected]> #180280 442c2f7 — Jason (Json) <[email protected]> #178596 63f386b — Braden Guliano <[email protected]> #181804 d75915e — Peter Steinberger <[email protected]> #181140 b4031b2 — Peter Steinberger <[email protected]> #182207 b8fae65 — Peter Steinberger <[email protected]> #180792 d2c70dd — Peter Steinberger <[email protected]> #179205 c08aa6d — Peter Steinberger <[email protected]> #179997 f178dd9 — mali <[email protected]> #181270 e204484 — Peter Steinberger <[email protected]> #179031 855b0aa — Peter Steinberger <[email protected]> #179726 3d51d00 — Peter Steinberger <[email protected]> #179249 b617d97 — Josh Avant <[email protected]> #182385 bb13bcd — Peter Steinberger <[email protected]> #182615 411bac2 — Peter Steinberger <[email protected]> #181300 765c2f1 — Peter Steinberger <[email protected]> #179549 09ba994 — Peter Steinberger <[email protected]> #179445 ac5587b — Peter Steinberger <[email protected]> #181127 25dde25 — Josh Lehman <[email protected]> #173209 ebccf57 — Peter Steinberger <[email protected]> #175289 e579f09 — Peter Steinberger <[email protected]> Proof: targeted owner/regression tests; Qwen15 and catalog pool13 pass. Final integrated lint/type/format pass remains pending under user direction. Co-authored-by: RomneyDa <[email protected]>
Refs #180690. Thanks @609NFT for the fleet-scale report.
What Problem This Solves
Doctor retained a full plugin metadata snapshot for every agent workspace until the run ended. A synthetic 400-agent fleet retained 401 snapshots and about 503 MiB after garbage collection, before other Doctor work was counted.
User Impact
Doctor's reusable plugin metadata now has bounded retention while it still inspects every agent. No schema, dependency, CLI option, config option, or migration semantics change.
Why This Change Was Made
The metadata cache and Doctor's workspace lookup both retained every immutable view. Both now retain at most 64 recently used views. Config-wide reads refresh recency so traversing a fleet does not repeatedly rebuild the shared registry. Eviction removes lookup entries only; active and borrowed snapshots remain valid under their existing lifecycle owner.
This removes one measured source of growth in the contributions phase. It does not claim to resolve every cost in #180690. Config-flow's synthetic timings point to serial state maintenance, not root-config parsing. Its migration prelude walks agent databases for media/session/transcript maintenance; native transcript inspection opens a database separately for schema and cursor checks. Source inspection also found repeated full-roster lookup in workspace preparation. These are separate follow-up owners; the reported 730 seconds cannot be attributed precisely without the reporter's populated v28 databases. #180350's migration-obligation conflict and #179481's legacy-session accumulation are outside this change.
Evidence
Acceptance: bound reusable snapshots, retain active views, avoid repeated fleet-wide discovery, and keep workload duration within 20% of baseline.
Committed workload:
scripts/perf/doctor-metadata-retention.ts, fresh processes with isolatedHOMEandOPENCLAW_STATE_DIR, Node 24.22.1,--expose-gc --max-old-space-size=6144. Five samples per size and revision, interleaved baseline/candidate. Timing excludes module imports; retained heap is measured after collection. The filesystem was warm, other lanes shared the host, and observed one-minute host load ranged from 5.35 to 8.58. Baseline:cf3702eb9a43628bdfe2ac93d8c697f4709812ea.Initial discovery still builds the fleet registry, so its transient peak is not removed. Lightweight workspace/root records also remain proportional to the fleet. This bounds full reusable snapshots, not every byte used by Doctor.
At 50 agents, the between-workspace sampled-peak median moved from 242.9 to 299.2 MiB despite unchanged retained heap and nearly identical peak p95. A separate five-pair interleaved
--trace-gc-nvpinvestigation captured main-thread heap sizes immediately before collection as well as the workspace samples: combined peak median/p95 was 294.4/300.0 MiB before and 286.4/300.6 MiB after (ranges 281.0–300.0 and 285.8–300.6 MiB). The original sampler can miss a peak and collection within one workspace check; the trace comparison does not show a corresponding peak-memory regression.The owner regression fails on the unchanged base at 65 retained snapshots, passes with the fix, preserves a held snapshot after eviction, and verifies only 400 registry reads for 200 workspaces including a revisit. Its two tests take about 0.6 seconds; source/compiler preparation accounts for most of the 30-second command wall time.
The real built CLI completed
doctor --fix --non-interactive --yeson fresh isolated 50-, 200-, and 400-agent states, with free loopback ports and a 6-GiB V8 heap cap:CLI heap samples were taken every 25 ms with a task-local preload; synchronous spikes can be missed. These tiny states do not reproduce the reporter's 939 v28 databases or prove the entire 6-GiB incident resolved. An initial source-mode run hit a transitive package-loading failure; built CLI proof succeeded. One 400-agent attempt overlapped a test artifact rebuild and was discarded; the fresh rerun above succeeded.
Core and script typechecks, touched-file type-aware lint, formatting, import-cycle check (zero cycles), prompt-prefix stability (6 tests), and fresh Codex review pass. SQLite inventory was omitted under the campaign's local gate budget.
The required consumer command selected 289 files in discovery order and completed in 2,030.6 seconds. It reported one failure:
loader.metadata-retention.test.tswrites a CommonJS.jsfixture without a package file, so the required worktree-localTMPDIRmade it inherit the repository's ESM package scope. The same failure reproduces on the untouched base. With an independent CommonJS package scope at the temporary-directory boundary, all three tests pass on both base and candidate; no test or production logic changed. Existing PostgreSQL coverage remained skipped without a test database (21 cases), plus one existing Gateway skip. One existing Discord-capture E2E case took 43.0 seconds, including about 29.7 seconds of cold Gateway startup; no timeouts or assertions changed.Change size: production +32/-8 lines, tests +44/-1, reproducible measurement script +49, documentation +3.
Representative commands, with Node 24.22.1 on
PATH: