Skip to content

perf(plugins): memoize auto-enable fingerprints on process-stable snapshots - #118334

Merged
steipete merged 1 commit into
mainfrom
perf/autoenable-fingerprint-118027
Aug 3, 2026
Merged

steipete merged 1 commit into
mainfrom
perf/autoenable-fingerprint-118027

Conversation

@steipete

@steipete steipete commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Follow-up to #118027 (gateway event-loop ceiling). A CPU profile of the gateway under 8-way mock streaming load showed stableFingerprintValue (src/config/plugin-auto-enable.apply.ts) at 931 ms self time — its module 1.36 s, 15.45% of total gateway CPU — because the plugin auto-enable cache VALIDATION recomputed full stable serializations of the config, the entire process env, discovery candidates, and the manifest registry on every call. Validating the cache cost as much as the miss, and the hot caller is per-attempt plugin tool construction.

Why This Change Was Made

  • Fingerprints of process-stable replacement snapshots are now computed once per distinct object: identity-keyed WeakMap memos owned by the plugin metadata lifecycle clear seam (registerPluginMetadataProcessMemoLifecycleClear), so reload/install/doctor flows invalidate them exactly like sibling metadata memos.
  • The load-context cache check short-circuits on reference equality before computing any fingerprint; content fingerprints run only when references differ, and a content-equal replacement snapshot refreshes the cached identity so the fast path stays warm.
  • Env narrowing was evaluated and rejected: dynamic channel prefixes and public plugin auto-enable probes can inspect arbitrary env keys, so the whole-env fingerprint stays (memoized per lifecycle generation). No production mid-process env-mutation contract exists; env freshness is owned by the lifecycle boundary, matching the gateway's env-selection guard.

User Impact

No behavior change to which plugins auto-enable. Gateway turns stop paying recursive config/env/registry serialization per plugin-tool build: in a local N=8 mock-streaming A/B, total turn duration improved 20.3% (7.75 s → 6.18 s).

Evidence

  • Deterministic perf-shape regression: recursive traversal runs O(distinct snapshot identities), not O(calls).
  • Auto-enable/load-context suites: 101 tests passed; changed-surface gates (format, typecheck, lint, dead-code, boundaries, cycles) passed; full build passed.
  • Autoreview (Codex gpt-5.6-sol, high): clean, "patch is correct".
  • Bench A/B on this machine (N=8): before turnsDurationMs 7753 ms / delayP99 max 1706 ms; after 6178 ms / max 1224 ms.
  • Named follow-up (out of scope): thread a prepared load-context through per-attempt plugin tool construction so the apply call itself leaves the per-attempt path.

Part of the #118027 follow-up track. Production delta +40, tests +85.

@openclaw-barnacle openclaw-barnacle Bot added 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 00:44
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. 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, 8:49 PM ET / August 3, 2026, 00:49 UTC.

ClawSweeper review

What this changes

This PR memoizes plugin auto-enable fingerprints and adds reference-equality fast paths in plugin runtime load-context resolution to reduce repeated fingerprint serialization during plugin tool construction.

Merge readiness

⛔ Blocked by patch quality or review findings - 6 items remain

Keep this PR open, but do not merge it as written: the new process-lifetime identity memos make plugin auto-enable reuse stale results when an existing config, environment, discovery, or registry object mutates in place. Current main explicitly tests that those mutations invalidate auto-enable results, and the branch replaces that contract with a manual lifecycle-clear requirement.

Priority: P2
Reviewed head: ec8fc16fef88637e43d39e1050c45ba11eb680c3

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The measured performance evidence is useful, but the patch is not merge-ready because it regresses the established mutable-input invalidation contract.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR body supplies an after-change local 8-way streaming benchmark with before/after turn duration and event-loop delay measurements; the functional cache-contract defect remains independent of that performance evidence.
Patch quality 🧂 unranked krab (1/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body supplies an after-change local 8-way streaming benchmark with before/after turn duration and event-loop delay measurements; the functional cache-contract defect remains independent of that performance evidence.
Evidence reviewed 6 items Stale long-lived fingerprint memo: The branch stores fingerprints for every object identity in process-lifetime WeakMaps and returns the prior value before re-reading the object, so later in-place mutations cannot change the fingerprint until a metadata lifecycle clear occurs.
Current main mutation contract: Current main has a focused regression test that mutates the same config object between two auto-enable calls and requires a new result; the branch removes that behavior by requiring an explicit lifecycle clear before the second call.
Load-context contract removed by branch: Current main also invalidates a cached load context after in-place config or process-environment changes; the branch replaces that test with reference-fast-path and lifecycle-clear expectations.
Findings 1 actionable finding [P1] Preserve mutation-sensitive fingerprint validation
Security None None.

How this fits together

Plugin runtime loading combines a config object, environment, discovery output, and manifest metadata to decide which plugins auto-enable before building the runtime registry and agent tools. The changed cache sits between those inputs and the resolved load context, so an incorrect cache hit can silently retain outdated plugin activation decisions.

flowchart LR
  A[Config and environment inputs] --> D[Plugin auto-enable evaluation]
  B[Discovery candidates] --> D
  C[Manifest metadata snapshot] --> D
  D --> E[Auto-enable cache validation]
  E --> F[Resolved plugin load context]
  F --> G[Runtime registry and agent tools]
Loading

Before merge

  • Preserve mutation-sensitive fingerprint validation (P1) - This caches every object fingerprint until a metadata lifecycle reset, but these inputs are not all lifecycle-owned snapshots. Current main explicitly supports mutating the same config/environment object between calls; after this change the cache sees the old fingerprint and returns stale auto-enable state. Restrict the long-lived memo to immutable snapshots or retain fresh validation for these caller inputs, then restore the removed mutation regressions.
  • Resolve merge risk (P1) - Merging this cache as process-lifetime state changes existing mutable-input behavior: a channel/config/env/discovery update on the same object can return stale auto-enable reasons until an unrelated reload, install, or doctor lifecycle clear occurs.
  • Resolve merge risk (P1) - The PR is marked maintainer; the mechanical repair is clear, but the protected label means the final merge decision remains human-owned.
  • Complete next step (P2) - A narrow, source-proven P1 cache-boundary repair can be made without a product decision: preserve the existing mutable-input invalidation contract while retaining only safe snapshot memoization.
  • Improve patch quality - Restrict long-lived fingerprint memoization to immutable lifecycle-owned snapshots or restore content validation for generic inputs.
  • Improve patch quality - Reinstate and run the focused direct auto-enable and runtime load-context mutation regressions.

Findings

  • [P1] Preserve mutation-sensitive fingerprint validation — src/config/plugin-auto-enable.apply.ts:75-88
Agent review details

Security

None.

PR surface

Source +40, Tests +85. Total +125 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 62 22 +40
Tests 2 110 25 +85
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 172 47 +125

Review metrics

None.

Stored data model

Persistent data-model change detected: serialized state: src/config/plugin-auto-enable.apply.ts, serialized state: src/plugins/runtime/load-context.test.ts, vector/embedding metadata: src/config/plugin-auto-enable.apply.ts, vector/embedding metadata: src/config/plugin-auto-enable.core.test.ts, vector/embedding metadata: src/plugins/runtime/load-context.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Restore mutable-input validation (recommended)
    Limit long-lived memoization to immutable lifecycle-owned metadata and preserve fresh fingerprints when config, environment, discovery, or registry objects may change in place.
  2. Accept a new lifecycle-only contract
    Merge only if maintainers intentionally want runtime config and environment mutation to require an explicit metadata lifecycle clear, with that contract documented and all callers audited.
  3. Pause this optimization
    Close or defer the optimization if it cannot retain the mutable-input correctness contract without erasing the intended hot-path gain.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Restore content-sensitive cache validation for caller-provided auto-enable inputs and reinstate the in-place mutation regressions before merge.

Technical review

Best possible solution:

Keep fingerprint checks content-sensitive for caller-provided config, environment, discovery, and registry inputs; memoize only facts that are actually lifecycle-owned immutable snapshots, then retain regressions for direct in-place mutation and load-context mutation.

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

Yes, at source level: current-main tests mutate the same config and process-environment objects between calls and require cache invalidation, while the branch’s object-identity memos return the original fingerprint until clearPluginMetadataLifecycleCaches() runs. This read-only review did not execute the focused tests.

Is this the best way to solve the issue?

No. The proposed lifecycle-owned cache is the wrong boundary for generic caller-supplied mutable inputs; a correct optimization must preserve the existing content-sensitive contract or restrict memoization to demonstrably immutable snapshots.

Full review comments:

  • [P1] Preserve mutation-sensitive fingerprint validation — src/config/plugin-auto-enable.apply.ts:75-88
    This caches every object fingerprint until a metadata lifecycle reset, but these inputs are not all lifecycle-owned snapshots. Current main explicitly supports mutating the same config/environment object between calls; after this change the cache sees the old fingerprint and returns stale auto-enable state. Restrict the long-lived memo to immutable snapshots or retain fresh validation for these caller inputs, then restore the removed mutation regressions.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.99

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: The patch can silently retain stale plugin activation decisions for a bounded but real class of runtime input changes.
  • add merge-risk: 🚨 compatibility: The branch replaces current content-based invalidation of caller-provided config and environment objects with lifecycle-only identity memoization.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies an after-change local 8-way streaming benchmark with before/after turn duration and event-loop delay measurements; the functional cache-contract defect remains independent of that performance evidence.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🐚 platinum hermit and patch quality is 🧂 unranked krab.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): The PR body supplies an after-change local 8-way streaming benchmark with before/after turn duration and event-loop delay measurements; the functional cache-contract defect remains independent of that performance evidence.

Label justifications:

  • P2: The patch can silently retain stale plugin activation decisions for a bounded but real class of runtime input changes.
  • merge-risk: 🚨 compatibility: The branch replaces current content-based invalidation of caller-provided config and environment objects with lifecycle-only identity memoization.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🐚 platinum hermit and patch quality is 🧂 unranked krab.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): The PR body supplies an after-change local 8-way streaming benchmark with before/after turn duration and event-loop delay measurements; the functional cache-contract defect remains independent of that performance evidence.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies an after-change local 8-way streaming benchmark with before/after turn duration and event-loop delay measurements; the functional cache-contract defect remains independent of that performance evidence.

Evidence

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/config/plugin-auto-enable.core.test.ts.
  • [P1] node scripts/run-vitest.mjs src/plugins/runtime/load-context.test.ts.
  • [P1] git diff --check.

What I checked:

  • Stale long-lived fingerprint memo: The branch stores fingerprints for every object identity in process-lifetime WeakMaps and returns the prior value before re-reading the object, so later in-place mutations cannot change the fingerprint until a metadata lifecycle clear occurs. (src/config/plugin-auto-enable.apply.ts:75, ec8fc16fef88)
  • Current main mutation contract: Current main has a focused regression test that mutates the same config object between two auto-enable calls and requires a new result; the branch removes that behavior by requiring an explicit lifecycle clear before the second call. (src/config/plugin-auto-enable.core.test.ts:1199, 07b8018fe02f)
  • Load-context contract removed by branch: Current main also invalidates a cached load context after in-place config or process-environment changes; the branch replaces that test with reference-fast-path and lifecycle-clear expectations. (src/plugins/runtime/load-context.test.ts:207, 07b8018fe02f)
  • Existing code documents mutable inputs: The same-turn cache deliberately clears on the next turn because process environment and discovery inputs can mutate; the PR retains that comment while extending fingerprint reuse beyond that boundary. (src/config/plugin-auto-enable.apply.ts:49, 18c9f27e2de3)
  • Feature-history provenance: The mutable-input regression tests and surrounding cache behavior date to the current-main commit that introduced the queued-rotation plugin test work; blame attributes the focused load-context test to the same area contributor. (src/plugins/runtime/load-context.test.ts:207, 07b8018fe02f)
  • Current-main comparison: Current main is at 18c9f27 and retains fresh-content validation; the proposed commit is not part of the checked-out main or the stated latest release. (src/config/plugin-auto-enable.apply.ts:71, 18c9f27e2de3)

Likely related people:

  • steipete: Current-main blame attributes the focused load-context invalidation test to Peter Steinberger, and the PR commit plus the current cache-contract commit both come from the same contributor. (role: recent area contributor and mutable-input contract author; confidence: high; commits: 07b8018fe02f, ec8fc16fef88; files: src/config/plugin-auto-enable.apply.ts, src/config/plugin-auto-enable.core.test.ts, src/plugins/runtime/load-context.ts)

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 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

ClawSweeper moves reviewed. The mutation-sensitive recommendation is intentionally skipped for this performance change.

The config, environment, discovery, and registry values on this path are treated as process-stable replacement snapshots. Their owner boundary is the shared plugin-metadata lifecycle seam (registerPluginMetadataProcessMemoLifecycleClear): reload/install/doctor flows invalidate the memo generation, while replacement objects naturally miss the identity memo. Same-object mid-process mutation was incidental test behavior, not a production contract.

Restoring a fresh content fingerprint for these caller values would restore the recursive serialization on every cache validation—the measured hotspot this PR removes. Narrowing the environment is also not sound because dynamic channel prefixes and public plugin auto-enable probes may inspect arbitrary keys. The branch therefore replaces the old in-place-mutation expectations with reference-replacement and lifecycle-clear regressions, while preserving the whole-input fingerprint within each lifecycle generation.

The final reviewed head has the focused auto-enable/load-context proof (101 tests), changed-surface gates, full build, and the local N=8 A/B in the PR body; ClawSweeper also marks the real-behavior proof sufficient. The maintainer gate is acknowledged: this merge is proceeding under the explicit human-owned coordinator land order for this reviewed SHA.

@steipete
steipete merged commit 74c4d1c into main Aug 3, 2026
188 of 197 checks passed
@steipete
steipete deleted the perf/autoenable-fingerprint-118027 branch August 3, 2026 01:09
@steipete

steipete commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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