Skip to content

perf(agents): carry prepared plugin load context into per-attempt tool construction - #118398

Merged
steipete merged 6 commits into
mainfrom
perf/prepared-loadcontext-118027
Aug 3, 2026
Merged

steipete merged 6 commits into
mainfrom
perf/prepared-loadcontext-118027

Conversation

@steipete

@steipete steipete commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

The follow-up named in #118334: per-attempt plugin tool construction still re-ran the plugin load-context resolver + auto-enable orchestration once per tool-constructing attempt (call chain run-orchestrator → attempt-tool-base-prepare → agent-tools → openclaw-plugin-tools → plugins/tools → runtime/load-context). Measured at 14.0-15.6 ms per attempt, 117 ms aggregate in an N=8 bench (~1.8% of turn time), repeated again on retries.

Why This Change Was Made

Prepared facts now travel with the lifecycle owner instead of being rediscovered (#117768's seam, extended): prepared-runtime acquisition resolves the plugin load context once after registry construction and attaches it to the lifecycle-owned plugin registry via a module-private symbol (prepared-model-runtime.plugin-context.ts) — no Plugin SDK type changes; all 149 SDK subpaths unchanged. Tool construction uses the prepared context only under strict reference-identity compatibility (raw/resolved config, env, workspaceDir all identical), falling back to the cold resolver otherwise. Incompatible, standalone, CLI, status, and sandbox paths keep the original cold path; one resolution implementation remains.

User Impact

No behavior change (same plugins, same tools, same registries — six ownership/forwarding/metadata-reuse regressions incl. zero per-attempt resolution under a prepared owner). Configured turns stop paying per-attempt load-context resolution: bench A/B turns 6391 → 6219 ms (−2.7%), in-load readyz/sessions p50 38.7/38.5 → 7.6/7.4 ms.

Evidence

  • Focused Vitest: 8 files, 315 tests passed. Targeted lint/type boundaries clean; full build clean.
  • Autoreview (Codex gpt-5.6-sol, high): clean, 0.98.
  • Bench A/B (N=8, same machine): above. Production delta +84 net (lifecycle ownership boundary), tests +131.

Completes the follow-up track from #118027 (#118192, #118207, #118193, #118334, #118349, #118364).

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels Aug 3, 2026
@steipete steipete self-assigned this Aug 3, 2026
@steipete
steipete marked this pull request as ready for review August 3, 2026 02:55
@clawsweeper clawsweeper Bot added 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. P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. labels Aug 3, 2026
@clawsweeper

clawsweeper Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed August 2, 2026, 11:47 PM ET / August 3, 2026, 03:47 UTC.

ClawSweeper review

What this changes

Carries lifecycle-prepared plugin load context and registry facts into per-attempt agent tool construction to avoid repeatedly resolving plugin metadata and auto-enable state.

Merge readiness

⚠️ Needs maintainer review before merge - 3 items remain

Needs one correction before merge: the prepared path can select tools using metadata captured before auto-enable, while the cold path selects from metadata compatible with the auto-enabled configuration. That can silently omit an auto-enabled plugin tool on prepared configured turns, so this PR remains necessary but is not ready to merge.

Priority: P1
Reviewed head: ead3f7d9fca4925f96b4343fe52a9352bf8451b5

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The performance proof is useful and the ownership direction is sound, but a concrete compatibility regression blocks merge.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR body supplies after-fix same-machine A/B benchmark output for configured turns and readyz/session latency; it is relevant real runtime evidence, although it does not cover the newly identified auto-enable compatibility case.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body supplies after-fix same-machine A/B benchmark output for configured turns and readyz/session latency; it is relevant real runtime evidence, although it does not cover the newly identified auto-enable compatibility case.
Evidence reviewed 6 items Prepared-path metadata overwrite: The new prepared-context helper spreads the resolved load context, then overwrites its metadata snapshot with the snapshot resolved before auto-enable. The resolver can otherwise create a final snapshot compatible with the auto-enabled config.
Auto-enable resolver contract: The runtime load-context resolver applies auto-enable and, when the initial snapshot is incompatible with the resulting config, resolves a final metadata snapshot. Passing a manifest registry suppresses that final snapshot path, making the helper's raw-snapshot override consequential.
Prepared tools consume the stale snapshot: On a compatible prepared context, tool selection uses preparedRuntime.metadataSnapshot rather than loading a manifest snapshot from the final resolved configuration; an auto-enabled tool plugin can therefore be absent only on the prepared path.
Findings 1 actionable finding [P1] Retain the final auto-enabled metadata snapshot
Security None None.

How this fits together

Prepared model runtimes build plugin registry and metadata facts once for configured agent turns. Per-attempt tool construction consumes those facts to choose and instantiate enabled plugin tools, with a cold resolver retained for incompatible contexts.

flowchart LR
  A[Configured agent turn] --> B[Prepared runtime owner]
  B --> C[Plugin registry and metadata]
  C --> D[Prepared load context]
  D --> E[Per-attempt tool construction]
  E --> F{Compatible context?}
  F -->|Yes| G[Reuse prepared facts]
  F -->|No| H[Cold plugin resolution]
  G --> I[Selected plugin tools]
  H --> I
Loading

Before merge

  • Retain the final auto-enabled metadata snapshot (P1) - resolvePluginRuntimeLoadContext can replace the initial snapshot after auto-enable changes the config, but this overwrite reattaches the pre-auto-enable metadataSnapshot. The compatible prepared path then passes that stale snapshot to resolvePluginToolRuntimePluginIds, unlike the cold path, so an auto-enabled tool plugin can be omitted. Carry the resolver's final snapshot and add a prepared-versus-cold auto-enable regression.
  • Resolve merge risk (P1) - Merging as written can make an auto-enabled plugin tool disappear only on compatible prepared configured turns, creating a prepared-versus-cold behavior split that ordinary cold-path coverage does not reveal.
  • Complete next step (P2) - A narrow mechanical repair and focused regression can restore the cold-path plugin-selection contract before merge.

Findings

  • [P1] Retain the final auto-enabled metadata snapshot — src/agents/prepared-model-runtime.plugin-context.ts:46
Agent review details

Security

None.

PR surface

Source +114, Tests +160. Total +274 across 16 files.

View PR surface stats
Area Files Added Removed Net
Source 9 142 28 +114
Tests 7 219 59 +160
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 16 361 87 +274

Review metrics

Metric Value Why it matters
Production vs test delta production +142/-28; tests +219/-59 The production growth is an ownership-boundary optimization, but its added snapshot carrier must preserve the cold resolver's final auto-enable contract.

Stored data model

Persistent data-model change detected: vector/embedding metadata: src/agents/openclaw-tools.browser-plugin.integration.test.ts, vector/embedding metadata: src/agents/prepared-model-runtime.facts.ts, vector/embedding metadata: src/agents/prepared-model-runtime.plugin-context.ts, vector/embedding metadata: src/plugins/runtime/load-context.test.ts, vector/embedding metadata: src/plugins/runtime/load-context.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Preserve final auto-enable metadata before merge (recommended)
    Carry the resolver's final config-compatible metadata snapshot through the prepared owner and add a prepared-versus-cold auto-enabled-tool regression.
  2. Pause the optimization
    Do not merge the prepared-context reuse until its plugin-selection behavior is proven equivalent to the existing cold path for auto-enabled configurations.

Technical review

Best possible solution:

Keep one canonical resolved metadata snapshot after auto-enable, carry that final snapshot with the prepared load context, and add a regression that proves prepared and cold tool selection agree when auto-enable adds a tool plugin.

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

Yes, source-reproducible: configure metadata so auto-enable adds a plugin tool, then compare a compatible prepared runtime with the cold resolver. The branch routes the prepared selection through the pre-auto-enable snapshot while the cold path resolves final-compatible metadata.

Is this the best way to solve the issue?

No. Reusing lifecycle-owned facts is the right owner boundary, but the carrier must retain the final metadata snapshot produced after auto-enable rather than reattach the initial snapshot.

Full review comments:

  • [P1] Retain the final auto-enabled metadata snapshot — src/agents/prepared-model-runtime.plugin-context.ts:46
    resolvePluginRuntimeLoadContext can replace the initial snapshot after auto-enable changes the config, but this overwrite reattaches the pre-auto-enable metadataSnapshot. The compatible prepared path then passes that stale snapshot to resolvePluginToolRuntimePluginIds, unlike the cold path, so an auto-enabled tool plugin can be omitted. Carry the resolver's final snapshot and add a prepared-versus-cold auto-enable regression.
    Confidence: 0.98

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against c89e57dd4517.

Labels

Label changes:

  • add P1: The patch can silently suppress auto-enabled plugin tools on configured prepared turns, affecting an agent's available tool workflow after merge.
  • remove P3: Current review triage priority is P1, so this older priority label is no longer current.

Label justifications:

  • P1: The patch can silently suppress auto-enabled plugin tools on configured prepared turns, affecting an agent's available tool workflow after merge.
  • merge-risk: 🚨 compatibility: Prepared and cold paths can observe different plugin metadata after auto-enable, changing which existing configured tools are available.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): The PR body supplies after-fix same-machine A/B benchmark output for configured turns and readyz/session latency; it is relevant real runtime evidence, although it does not cover the newly identified auto-enable compatibility case.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies after-fix same-machine A/B benchmark output for configured turns and readyz/session latency; it is relevant real runtime evidence, although it does not cover the newly identified auto-enable compatibility case.

Evidence

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/plugins/tools.optional.test.ts src/plugins/runtime/load-context.test.ts src/agents/openclaw-tools.browser-plugin.integration.test.ts.
  • [P1] pnpm plugin-sdk:surface:check.
  • [P1] pnpm build.

What I checked:

  • Prepared-path metadata overwrite: The new prepared-context helper spreads the resolved load context, then overwrites its metadata snapshot with the snapshot resolved before auto-enable. The resolver can otherwise create a final snapshot compatible with the auto-enabled config. (src/agents/prepared-model-runtime.plugin-context.ts:46, ead3f7d9fca4)
  • Auto-enable resolver contract: The runtime load-context resolver applies auto-enable and, when the initial snapshot is incompatible with the resulting config, resolves a final metadata snapshot. Passing a manifest registry suppresses that final snapshot path, making the helper's raw-snapshot override consequential. (src/plugins/runtime/load-context.ts:200, ead3f7d9fca4)
  • Prepared tools consume the stale snapshot: On a compatible prepared context, tool selection uses preparedRuntime.metadataSnapshot rather than loading a manifest snapshot from the final resolved configuration; an auto-enabled tool plugin can therefore be absent only on the prepared path. (src/plugins/tools.ts:1159, ead3f7d9fca4)
  • Regression coverage gap: The added prepared-runtime test proves resolver avoidance with a metadata snapshot whose config already has plugins enabled; it does not exercise an auto-enable transition and compare the prepared selection with the cold selection. (src/plugins/tools.optional.test.ts:995, ead3f7d9fca4)
  • Current-main owner boundary: Current main still resolves metadata after acquiring the lifecycle-owned runtime registry; this branch moves that work into the prepared owner and is not already implemented on main. (src/agents/prepared-model-runtime.facts.ts:226, c89e57dd4517)
  • Feature-history routing: The current prepared-runtime and plugin-tool paths blame to the recent agent/plugin performance work by Peter Steinberger; the PR commits continue that ownership thread. (src/plugins/tools.ts:1112, ffe2268eec)

Likely related people:

  • steipete: Current-main blame for the prepared-runtime and plugin-tool owner boundary, plus all six commits in this follow-up performance branch, point to the same contributor. (role: recent area contributor; confidence: high; commits: ffe2268eec, f6057604bc77, 28402dc17045; files: src/agents/prepared-model-runtime.facts.ts, src/plugins/tools.ts, src/plugins/runtime/load-context.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Preserve the final metadata snapshot produced after auto-enable.
  • Add a regression showing prepared and cold paths select the same auto-enabled tool plugin.

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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-08-03T03:18:24.366Z sha 48002915986172349f816dca7bca4a86dad7934e :: needs maintainer review before merge. :: none
  • reviewed 2026-08-03T03:34:50.474Z sha 7f0740b208968fb542c55aaa54f6f9acb394e856 :: needs changes before merge. :: [P1] Forward metadata compatible with the auto-enabled config

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed 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. labels Aug 3, 2026
@steipete
steipete force-pushed the perf/prepared-loadcontext-118027 branch from c73adcd to ead3f7d Compare August 3, 2026 03:41
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. and removed P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. labels Aug 3, 2026
@steipete

steipete commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Maintainer review complete. The latest ClawSweeper metadata move is intentionally skipped: the carried metadata snapshot is the lifecycle owner's complete manifest inventory, while auto-enable state comes from the prepared resolved config; incompatible config/env/workspace identities retain the canonical cold path. Re-resolving metadata would reopen the cold discovery path guarded by the unchanged Gateway startup invariant.

Exact-head CI is green. The original build-artifacts failure was the known TUI PTY flake class documented in #113994; run 30782439847 attempt 2 passed.

@steipete
steipete merged commit a6e0002 into main Aug 3, 2026
192 of 196 checks passed
@steipete
steipete deleted the perf/prepared-loadcontext-118027 branch August 3, 2026 04:07
@steipete

steipete commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 3, 2026
…l construction (openclaw#118398)

* perf(plugins): carry prepared load context into tools

* fix(agents): keep prepared context setter private

* fix(agents): reuse prepared plugin metadata context

* refactor(agents): relocate prepared plugin context wiring

* test(agents): cover prepared plugin context owner path

* refactor(plugins): remove duplicate metadata type exports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: M status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant