Skip to content

fix(plugins): Gateway memory grows on every cold tool inventory in multi-agent setups - #178596

Merged
steipete merged 7 commits into
openclaw:mainfrom
bguliano:fix/main-thread-inspection-native-esm
Oct 21, 2026
Merged

steipete merged 7 commits into
openclaw:mainfrom
bguliano:fix/main-thread-inspection-native-esm

Conversation

@bguliano

@bguliano bguliano commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #178548
Related: #173021

What Problem This Solves

Fixes growing Gateway memory when cold tools.effective inspections repeatedly load an installed native ESM plugin in an agent workspace that differs from the Gateway workspace.

User Impact

Repeated cold inspections reuse unchanged installed plugin code within their Gateway inventory. Workspace isolation and registration authority remain intact. The first inspection still pays source preparation cost; no configuration changes or migration are required. Node retains evaluated ESM modules until process exit, including older graphs after an explicit plugin replacement.

Why This Change Was Made

Workspace mismatch correctly prevents borrowing the Gateway registry, but the fallback creates an uncached inspection and evaluates a fresh captured native ESM graph on each request. Catalog workers already retain those graphs within their worker lifetime.

The maintainer rewrite retains the contributor's immutable npm install identity and preparation reuse, but moves main-thread retention out of the process-global map. The existing native admission owner now holds the entries, bounded by the published inventory's installed execution paths, configured workspaces, SDK selection, and prepared read/write storage views. Inventory replacement and final borrower release dispose source captures, compiled helpers, and preparation workers. Editable installations and explicit recovery preserve their independent source custody.

The real-loader regressions are consolidated into one suite. Thanks to @bguliano for the reproduction, original fix, measurements, and regression coverage; contributor commits remain in the branch history.

Evidence

  • Source and history confirm the defect on main: the workspace check rejects lending and the native ESM retention path excludes the main thread.
  • Regression suite: src/plugins/loader.main-inspection-authority.test.ts, including reuses only published workspace code while preserving inspection authority and capture lifetime and loads edited sources for the next inspection of $name (archive and npm replacement). It covers evaluation/preparation counts, concurrent capture disposal, lazy imports, workspace/SDK separation, stale authority, and final borrower cleanup.
  • Changed-file oxlint, oxfmt, and git diff --check: passed.
  • Codex autoreview: scoped-clean through P2 on the complete maintainer candidate. The original review's P2 about unbounded artifact/worker retention is addressed by the inventory-owned lifecycle.
  • The first rewrite CI exposed two PR-caused integration defects: the helper was missing from restricted wrapper extraction, and concrete loader types created an import cycle. The follow-up keeps identity in the existing install owner and shared lifetime types in the leaf artifact contract; no baseline or inventory exception was added. Both ClawSweeper P2 code findings are addressed. Its requests for additional local/live proof are overridden by the maintainer policy below.
  • PR CI is the test gate for the regression suite, affected loader/runtime consumers, and repository checks. Exact-head CI completed for b77dbec960d194ff09e6933105edd9a17b532978: 61 successful jobs, 29 skipped, one inherited failing test shard and its failing aggregate gate. All three new regression cases passed; architecture, wrapper extraction, lint, types, and all UI E2E shards passed.
  • Inherited CI failure: checks-node-changed-compact-large-14-3 has four 120-second timeouts in memory-background-reload.test.ts. The same four cases fail in the latest scheduled Full Release Validation Nightly, downstream checks-node-agentic-plugins, on main a984f7723914e75c5a5a9a23c802aa356faedb95. The detached cases also share the exact session-startup-catchup: sync completion stall. Main commit fix(memory): resume background reindex after foreground turns #178979 routed background work through syncBackground, while three failing cases still wait on sync spies; the fourth timeout is matched to main but its exact stalled wait is not localized. The fixture and memory implementation are unchanged by this PR, and bundled loads bypass the new retention path. No retries or weakened assertions were used.
  • Final-head ClawSweeper review: no code findings; both previous P2 findings are resolved. Its remaining request is live behavior proof, intentionally overridden by the maintainer's CI-only policy in this direction.
  • Local tests, typechecks, builds, and live Gateway proof were intentionally skipped per maintainer policy. Contributor code was never executed locally; no remote leases were started.
  • LOC against merged main: production +308/-50 (net +258), tests +332/-0, docs +2/-0. Production growth supplies the bounded eligibility and retirement path missing from the original retention mechanism.

The contributor's prior-head measurements reported main heap ending at 341 MiB instead of 546 MiB over 24 cold calls, with warm inspection latency falling to 0–1 seconds. These are historical contributor measurements, not a measurement of the maintainer rewrite.

@clawsweeper

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

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 20, 2026
@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR reuses immutable installed native ESM plugin code within a Gateway inventory and releases retained captures after its final inspection borrower retires.

Example: Two cold tools.effective inspections use the same npm-installed plugin in the research agent workspace.

  • Before: Each inspection prepares and evaluates another native ESM graph that Node retains.
  • After: Both inspections reuse one evaluated graph while keeping separate registration authority.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The implementation is coherent and both prior code findings are fixed, but historical live measurements and current harness coverage do not clear the required proof gate for the rewrite.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Opening cold tool inventories repeatedly increases Gateway memory and delays Control UI requests in multi-agent installations.
Reason: The defect is documented on official releases and current source, and the maintainer rewrite explicitly approves the bounded repair direction without adding configuration or public APIs.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep open: current main still permits repeated native ESM evaluation during cold workspace-mismatched inspections, and this maintainer-engaged repair addresses that defect. Both previous P2 findings are resolved; no new actionable code findings were identified.

Priority: P1
Reviewed head: b77dbec960d194ff09e6933105edd9a17b532978

Before merge

  • Add real behavior proof - Historical built-Gateway measurements exercise the contributor's earlier retention mechanism, while exact-head CI supports the rewritten production-loader harness. No shipped-entry-point evidence exercises inventory replacement and final-borrower capture/worker cleanup on the rewrite. steipete's CI-only direction is recorded, but this external PR has no proof: override label under the requested review gate. Supply redacted Gateway logs or terminal output covering repeated cold inspections and that retirement lifecycle, or apply the explicit proof override; redact IPs, keys, phone numbers and non-public endpoints. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.

Findings

None.

Tests

  • Missing end-to-end proof: The rewritten inventory lifecycle lacks shipped-Gateway evidence for repeated workspace-mismatched cold inspections and final-borrower cleanup; red-on-base evidence for the consolidated final suite is also not supplied.
Agent review details

How this fits together

The plugin loader turns installed entries and workspace metadata into executable inspection registries; native admission owns retained source custody, while plugin instances own registration and callback authority.

flowchart TD
A[Cold tool inventory] --> B[Workspace inspection registry]
C[Published Gateway inventory] --> D[Native admission owner]
D --> E[Retained plugin code and captures]
B --> E
E --> F[Instance scoped registration]
B --> G[Borrower release]
G --> D
Loading

Technical review

Best possible solution:

Retain this inventory-owned repair while preserving independent registration authority, editable-install freshness and cleanup after the final borrower.

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

The linked report supplies release-specific measurements and a repeatable multi-agent scenario; current-main source independently confirms workspace-mismatched lending falls back to inspections excluded from native ESM reuse. No target code was executed during this review.

Is this the best way to solve the issue?

The repair uses the existing admission lifecycle to bound code custody without lending another workspace's registry, and all substantive additions support reuse, invalidation or cleanup; no smaller complete correction was established.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Native ESM retention and generation authority changes intended behavior with a stated reason (a81bb54: Retain unchanged native graphs within catalog workers while keeping workspace and registration authority separate.)
  • Admission view partitioning and borrower custody keeps the original intent (9faa93d: Share admitted native payload custody across inspections while retaining resources until all owners release them; later Doctor fixes partition private storage views.)
  • Asynchronous external source preparation keeps the original intent (a99f797: Keep the Gateway responsive during external source capture, with retained workers and independent recovery custody.)
  • Published workspace metadata compatibility keeps the original intent (dde140c: Reuse Gateway metadata only for compatible canonical workspace inventories while preserving discovery order.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 9faa93d adds src/plugins/plugin-native-admission-state.ts:89 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9faa93d44c85; files: src/plugins/plugin-native-admission-state.ts)
  • Ayaan Zaidi: Raw commit a81bb54 adds src/plugins/plugin-instance-module-loader.ts:118 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: a81bb54f094f; files: src/plugins/plugin-instance-module-loader.ts)
  • Vincent Koc: Raw commit 99f5d0f adds src/plugins/plugin-native-admission-state.ts:188 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 99f5d0f8e4e0; files: src/plugins/plugin-native-admission-state.ts)

PR surface

Source +258, Tests +332, Docs +2. Total +592 across 12 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 10 308 50 +258
Tests 1 332 0 +332
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 12 642 50 +592

Review metrics

Metric Value Why it matters
Production versus test growth Production +308/-50; tests +332/-0 Growth supplies finite reuse eligibility and admission-owned cleanup, with distinct loader, authority, concurrency and freshness regressions.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: Repeated cold inventories cause observed Gateway heap growth and Control UI stalls in affected multi-agent installations.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

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.

History

Review history (6 earlier review cycles)
  • reviewed 2026-10-20T17:42:04.591Z sha 4a65ac920827b071ba9f03765f73c685a674dc92 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-20T20:30:33.829Z sha 925ed6674bcf144f88dc2a870338c498915cd6b5 :: needs changes before merge. :: [P1] Keep mutable global plugins fresh across inspections
  • reviewed 2026-10-20T20:45:23.183Z sha 1ee1e29 :: needs changes before merge. :: [P1] Keep mutable global plugins fresh across inspections
  • reviewed 2026-10-20T21:03:06.042Z sha 33e9ac6 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-21T11:39:35.717Z sha a5054c4 :: needs real behavior proof before merge. :: [P2] Include the new helper in the extracted PR wrapper | [P2] Keep shared contracts independent of loader implementations
  • reviewed 2026-10-21T11:55:29.647Z sha b77dbec :: needs real behavior proof before merge. :: none

Reviewed October 21, 2026, 8:09 AM ET / 12:09 UTC (Revision 7).

@bguliano

Copy link
Copy Markdown
Contributor Author

Added the requested proof in 925ed66 and updated the PR body:

  • Generation authority: new real-loader regression (loader.main-inspection-authority.test.ts) shows neighboring and retired main-thread inspection generations rejected through a retained replacement API before any registry effect.
  • Whole-process memory: 8-agent, 24-call comparison against main (RSS, main heap, threads, retained workers).
  • CI: the inspection lookup moved to the module runtime, so the PR-wrapper inventory is unchanged (eager-import-closure passes).

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Oct 20, 2026
…ion generation authority

Move the inspection-registry lookup to the module runtime caller so plugin-instance-module-loader keeps its runtime import closure, and add a real-loader regression for neighboring and retired generations using a retained replacement API.
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. clawsweeper:intake:bug Preliminary submission classification; full source review pending. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 20, 2026
@bguliano
bguliano force-pushed the fix/main-thread-inspection-native-esm branch from 925ed66 to 1ee1e29 Compare October 20, 2026 20:38
@clawsweeper clawsweeper Bot removed the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Oct 20, 2026
…le installs

Global discovery also covers local, archive, and path installs that can be edited in place. Require an npm install record with integrity, key reuse on that install identity, and cover edited sources for archive installs and npm reinstalls.
@bguliano

Copy link
Copy Markdown
Contributor Author

Addressed the P1 finding in 33e9ac6 (and rebased onto current main, keeping PluginInventoryRetiredError):

  • Reuse now requires an installer-proven immutable install: global origin plus an npm install record with integrity. Local, archive, and path installs never share, and the key includes the install identity (owner, path, resolved spec, integrity, installedAt), so reinstalls load fresh.
  • New regression in loader.main-inspection-authority.test.ts: edit an imported helper, then run a second cold inspection, for an editable archive install and for an npm reinstall. Both fail on the previous revision (before) and pass now (after).
  • A built-package recheck with the real registry-installed @openclaw/codex still reuses: first call 31 s, then 0–1 s, main heap 314 → 340 MiB.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. and removed merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 21, 2026
@steipete
steipete merged commit 63f386b into openclaw:main Oct 21, 2026
180 of 186 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

@bguliano
bguliano deleted the fix/main-thread-inspection-native-esm branch October 21, 2026 12:59
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 22, 2026
…lti-agent setups (openclaw#178596)

* fix(plugins): stop cold tool inventories re-importing installed native plugins on the main thread

Related: openclaw#178548

* fix(plugins): keep the PR wrapper closure unchanged and prove inspection generation authority

Move the inspection-registry lookup to the module runtime caller so plugin-instance-module-loader keeps its runtime import closure, and add a real-loader regression for neighboring and retired generations using a retained replacement API.

* fix(plugins): share inspection code only for installer-proven immutable installs

Global discovery also covers local, archive, and path installs that can be edited in place. Require an npm install record with integrity, key reuse on that install identity, and cover edited sources for archive installs and npm reinstalls.

* fix(plugins): preserve inspection loader import boundaries

Keep immutable installation identity with its existing owner so Gateway
metadata publication does not introduce an unlisted runtime dependency into
the restricted PR-wrapper source extraction. Keep admission lifetime types
in the leaf artifact contract and concrete retained modules in the loader,
avoiding a type import cycle through the cache owner.

---------

Co-authored-by: Peter Steinberger <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 26, 2026
…lti-agent setups (#178596)

* fix(plugins): stop cold tool inventories re-importing installed native plugins on the main thread

Related: #178548

* fix(plugins): keep the PR wrapper closure unchanged and prove inspection generation authority

Move the inspection-registry lookup to the module runtime caller so plugin-instance-module-loader keeps its runtime import closure, and add a real-loader regression for neighboring and retired generations using a retained replacement API.

* fix(plugins): share inspection code only for installer-proven immutable installs

Global discovery also covers local, archive, and path installs that can be edited in place. Require an npm install record with integrity, key reuse on that install identity, and cover edited sources for archive installs and npm reinstalls.

* fix(plugins): preserve inspection loader import boundaries

Keep immutable installation identity with its existing owner so Gateway
metadata publication does not introduce an unlisted runtime dependency into
the restricted PR-wrapper source extraction. Keep admission lifetime types
in the leaf artifact contract and concrete retained modules in the loader,
avoiding a type import cycle through the cache owner.

---------

Co-authored-by: Peter Steinberger <[email protected]>

Release adaptation: stable has no main-thread artifact prepare step (plugin-generation-worker) or the admission transfer/adoption helpers; ported only the inspection-retention path: immutable npm install identity, published inspection policy on the admission state with disposal, and inspection-scoped retained native ESM reuse in the module loader. The authority test drops main's prepare-step spies and inlines the gateway fixture helper.

(cherry picked from commit 63f386b)
tideclaw-bot added a commit that referenced this pull request Nov 4, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: L status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Multi-agent Gateways leak ~10 MiB main heap per cold tools.effective: workspace-mismatched lending re-imports npm plugin (codex) graphs

2 participants