Skip to content

fix(plugins): share native module resolution hooks - #185033

Merged
steipete merged 2 commits into
mainfrom
steipete/fix-plugin-module-resolve-hook-chain-20261026
Oct 26, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/fix-plugin-module-resolve-hook-chain-20261026

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Native module resolution becomes more expensive as a Gateway retains more plugin instances: every import walks a process-wide hook for each live source capture, and the SDK hook scans all prepared alias contexts.

User Impact

Reduces import CPU and allocation without changing plugin module identity, reload behavior, SDK aliases, or capture custody. No configuration or migration is required.

Why This Change Was Made

Use one process-level resolve hook to select the capture boundary and its registration. Entry loads still resolve natively before the selected owner admits and prepares their target. Instance disposal removes the registration, and the hook deregisters when the last boundary retires. Retained native ESM keeps its current-instance selection and rejects failed/retired acquisition owners before resolution, including imports outside the capture.

SDK alias preparation now records root membership in its existing cache generation. The global resolver consumes that index instead of allocating an array and repeatedly deriving the same parent directory. Per-context roots remain intact for native-loader ownership checks.

This fixes residual active-instance cost: the base already deregisters hooks on disposal, so this is not a claim of an unbounded retirement leak. The production delta is +89 LOC (source only, excluding tests and benchmarks). That increase is required for shared dispatch, lifecycle cleanup, and the existing retained-owner authority contract; the per-instance Node hook registration path is removed.

Evidence

Acceptance: preserve resolution/identity/custody and reload tests, keep resolve-hook count independent of live plugin count, reduce warmed import CPU/allocation, and check real Gateway reload behavior. Timing and allocation passes are separate; no traced timing is used as a latency gate.

Owner workload Before After Change
Resolve hooks, 40 live plugins 41 2 -39
Resolve hooks after replacing all 40 41 2 -39
Hooks after disposing all instances 1 1 SDK hook remains
Hook calls, 5,000 warmed host imports 205,000 10,000 -95.1%
Maximum resolve-hook depth 41 2 -95.1%
Process CPU, 5,000 host imports 105.41 ms 80.31 ms -23.8%
Sampled allocation, 5,000 host imports 132.92 MB 50.98 MB -61.6%
SDK main-thread CPU, 5,000 imports / 40 contexts 78.31 ms 68.22 ms -12.9%
SDK sampled allocation, same workload 64.28 MB 55.53 MB -13.6%
SDK context scans / 10 probe imports 10 0 removed

Dispatcher numbers: five counterbalanced pairs, 5,000 imports per arm per timing/allocation pass. The original dispatcher source uses the same SDK implementation as the candidate to isolate dispatch cost. All five pairs improved CPU and allocation. Host 1-minute load was 25.9–37.8; timing mean was 83.71→61.31 ms. SDK numbers: two counterbalanced pairs (BAAB), 5,000 imports per arm/pass, host load 18.09. Reported numbers are owner measurements, not whole-Gateway performance claims. Reproduction commands are in each benchmark script's header; the shared perf README is untouched.

Regressions fail on unchanged main: four plugin instances install five resolve hooks (expected at most two); the native eager/lazy SDK fixture performs seven context scans (expected zero). The candidate passed 229 tests across six focused Node files, including module generations, interop, demanded work, failed acquisitions, SDK aliases, and native SDK resolution. Node was selected explicitly because the local runner defaults to Bun, which does not exercise Node hooks.

Independent subagent review (autoreview route unavailable): no actionable P0/P1/P2 findings. The configured autoreview provider failed its route-match preflight before inference; no operator configuration was changed.

Built-Gateway proof: the supported qaRuntime build passed in 114.9 seconds. An isolated dist/entry.js Gateway loaded 40 synthetic external plugins against 10,800 sessions and served 65 WebSocket clients. Seven source-edit/reload operations each passed a revision RPC assertion. Boot and all completed reloads retained exactly two active resolve hooks and 40 capture owners; all 76 periodic samples with 40 live owners had two hooks. A transient native loader hook reached peak/depth three. Shutdown released all capture owners and left only the SDK hook. The build preceded the mechanical SDK publication helper extraction; the resolver behavior is identical. No production Gateway was accessed.

After the resolver-module extraction, another 73 plugin tests passed, including retained native ESM inspection/recovery authority. Both closure guard files passed after adding the new internal module to the tooling source inventory (23 + 3 tests). The initial missing-inventory failure was repaired, not waived. Targeted lint and oxfmt passed, the core tsrs typecheck passed, and the export-name collision guard passed. The line-cap guard passed without a baseline exception. Broad proof is PR CI. The scoped fast-path instruction uses owner measurements and focused proof rather than a multi-pair ten-minute stress campaign; no whole-Gateway CPU/GC/tail improvement is claimed.

The first CI head caught unchecked indexed reads in the two benchmark scripts and the new regression. Their loops now iterate typed entries; production behavior is unchanged. The script and plugin-test typecheck leaves and bounded-hook regression were rerun locally before the repair push. SDK figures above use the final workload.

Exact-head CI and inherited UI failures

Head f7004d97323812a00e2418d59af1e12150bbe766 completed CI run 40404251337. Plugin/Node tests, production and test types, lint, build artifacts, security checks, and the other UI shards passed. Four UI E2E shards and the aggregate gate remain red; this PR is not CI-green. GitHub Codex and ClawSweeper also reviewed this exact head with no findings.

All failing files were then run with the supported headless synthetic UI wrapper and one worker on the unchanged exact base 5df1d936646f8a34d20c46602bae37d4c5002581. Every candidate failure reproduced:

Failing file Base result Matching failure
new-session-page.prompt-attachments.e2e.test.ts 1 failed, 10 passed Line 176: scrollbar width expected thin, received auto; height 222 and line height 21 match CI
chat-composer-pointer-activation.e2e.test.ts 2 failed, 2 passed Mobile/narrow-desktop bottom spacing expected 44, received 40
chat-attachment-menu.e2e.test.ts 11 failed, 2 passed Old capture/media labels versus Camera, Media, File; missing Take photo locators
chat-media-picker.e2e.test.ts 3 failed All three mobile cases wait for the removed Photos & videos menu item at line 49

The first file took 55.70 seconds. The other three files ran together in 230.41 seconds (16 failed, 4 passed); the two filechooser unhandled errors also match CI. Local UI timeout policy uses 10-second locator waits where CI uses 30 seconds; failure names, assertion values, and locator sites match. No CI reruns were requested.

Source attribution: #184759 (b22e014dfa7da4ce3476672afe955cad806c98e9) introduced Chromium scrollbar-width: auto and reduced the mobile composer bottom gap from 10 to 6 pixels. #184780 (9d44439eecaa) renamed the attachment controls. This PR changes no UI files. These inherited failures need separate UI reconciliation; the resolver PR remains open and unmerged for the lead's review.

@clawsweeper

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T21:27:55.946193Z f7004d9 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 26, 2026
Dispatch captured module resolution by boundary and instance instead of
walking every live plugin's hook. Preserve retained ESM authority and
remove the shared hook after the last owner retires. Index prepared SDK
roots within their existing cache generation.

With 40 plugins, owner proof reduces resolve hooks from 41 to 2, process
CPU by 23.8%, and sampled allocation by 61.6% over five paired runs of
5,000 imports. Native module and SDK regressions fail on the base and
pass with the fix. Focused Node tests preserve reload and custody.
@steipete
steipete force-pushed the steipete/fix-plugin-module-resolve-hook-chain-20261026 branch from 7dea6ad to f7004d9 Compare October 26, 2026 21:25
@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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR shares native plugin resolution hooks and indexes prepared SDK roots within their existing cache generation.

Example: A Gateway retains 40 external plugin instances.

  • Before: Ordinary imports traverse 41 resolve hooks.
  • After: Ordinary imports traverse two resolve hooks, with instance disposal removing capture registrations.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, measured optimization with supplied Gateway behavior evidence and no actionable code finding; required CI has not cleared.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied built-Gateway results exercise 40 external plugins through revision-checked reloads and shutdown; reported post-extraction production-owner tests cover inspection/recovery authority, while source inspection confirms failed retained acquisitions are rejected before native resolution.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Gateways with many live plugin captures spend unnecessary CPU and allocation resolving ordinary imports.
Reason: The maintainer-authored direction has measured value and preserves existing plugin behavior without new configuration or public contracts.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR removes measured resolver overhead while preserving capture ownership, with no definite introduced correctness defect found.

Priority: P2
Reviewed head: f7004d97323812a00e2418d59af1e12150bbe766

Before merge

None.

Findings

None.

Agent review details

How this fits together

The plugin loader connects native Node imports to captured plugin sources, live instance ownership, and host SDK aliases, producing prepared module URLs for execution.

flowchart LR
  A[Native import] --> B[Shared resolve hook]
  B --> C[Live owner check]
  C --> D[Capture boundary lookup]
  D --> E[Instance source resolver]
  E --> F[Native resolution and SDK aliases]
  F --> G[Prepared module URL]
Loading

Technical review

Best possible solution:

Use one capture dispatcher and the existing generation-owned SDK cache while preserving instance admission, module identity, and disposal.

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

Current-main source establishes per-instance hook traversal and repeated SDK-context scans; the PR reports base-failing regressions and paired owner measurements, which were inspected without executing target code.

Is this the best way to solve the issue?

The patch addresses repeated dispatch at its owner, removes the replaced registration path, and carries prepared SDK membership through the existing cache lifecycle.

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduction and complete cutover: The introduced diff replaces per-instance Node hook registration with one shared dispatcher; disposal removes registrations and deregisters the hook when its final boundary retires. (src/plugins/plugin-instance-module-resolver.ts:39, f7004d973238)
  • Current main still carries the cost: Fetched main retains per-instance hook registration and scans prepared SDK contexts during qualifying native imports; the central optimization remains necessary. (src/plugins/plugin-sdk-native-resolver.ts:384, 9137d88c70f5)
  • Runtime and performance evidence: The PR body reports 41→2 hooks with 40 plugins, seven successful revision-checked Gateway reloads, and shutdown releasing capture owners; committed benchmarks exercise the production loader and SDK resolver. (scripts/perf/plugin-resolve-hooks-measure.ts:1, f7004d973238)
  • Authority remains checked at resolution and registration: The dispatcher rejects failed or retired retained owners before nextResolve; existing production-owner fixtures exercise failed acquisitions, neighboring workspace calls, released generations, and zero forbidden registration effects. (src/plugins/plugin-instance-module-resolver.ts:51, f7004d973238)
  • Generation-owned SDK index: Alias publication records roots beside prepared contexts, and the native resolver consumes root membership while retaining parent-scoped alias authorization. (src/plugins/plugin-cache-sdk.ts:90, f7004d973238)
  • Required CI remains unresolved: The exact-head aggregate gate is failing. Annotations identify attachment-label mismatches, a safe-area expectation of 44 receiving 40, and scrollbarWidth expecting thin receiving auto. The attachment mismatch also appears in ci: pack ordinary UI unit checks into two rows #184986 and its linked run; the other failures remain unattributed. (f7004d973238)

Likely related people:

  • Peter Steinberger: Raw commit 7879bdc adds src/plugins/plugin-instance-module-loader.ts:401 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 7879bdcbfa82; files: src/plugins/plugin-instance-module-loader.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)
  • Patrick Erichsen: Raw commit 6878654 adds src/plugins/plugin-instance-module-loader.ts:188 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 687865486444; files: src/plugins/plugin-instance-module-loader.ts)

PR surface

Source +89, Tests +41, Other +282. Total +412 across 10 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 5 101 12 +89
Tests 2 55 14 +41
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 3 282 0 +282
Total 10 438 26 +412

Review metrics

Metric Value Why it matters
Production versus regression coverage Production +101/-12; tests +55/-14 The net production growth supplies shared dispatch, registration cleanup, and a generation-owned root index; the former per-instance registration path is removed.

Labels

Label changes:

  • add P2: Measured import CPU and allocation improve for Gateways retaining many plugins, without evidence of an urgent user-facing outage.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Measured import CPU and allocation improve for Gateways retaining many plugins, without evidence of an urgent user-facing outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

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.

Reviewed October 26, 2026, 5:41 PM ET / 21:41 UTC.

@steipete
steipete merged commit 33a3024 into main Oct 26, 2026
162 of 168 checks passed
@steipete
steipete deleted the steipete/fix-plugin-module-resolve-hook-chain-20261026 branch October 26, 2026 21:54
@steipete

Copy link
Copy Markdown
Contributor Author

Maintainer landing note: merging at exact head f7004d97323812a00e2418d59af1e12150bbe766 with admin override because the only red checks are inherited UI E2E failures unrelated to this resolver change.

Review: the shared dispatcher keeps per-instance authority. Each registration's resolve body still runs the unchanged ownsNativeEsmResolution gate, owner selection matches the old LIFO hook chain, and entry-load admission reuses the single native resolution. All plugin, Node, type, lint, and build jobs passed at this head.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
* fix(plugins): share native module resolution hooks

Dispatch captured module resolution by boundary and instance instead of
walking every live plugin's hook. Preserve retained ESM authority and
remove the shared hook after the last owner retires. Index prepared SDK
roots within their existing cache generation.

With 40 plugins, owner proof reduces resolve hooks from 41 to 2, process
CPU by 23.8%, and sampled allocation by 61.6% over five paired runs of
5,000 imports. Native module and SDK regressions fail on the base and
pass with the fix. Focused Node tests preserve reload and custody.

* test(plugins): iterate known resolver fixture entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant