Skip to content

perf(plugins): compose async runtime scopes - #152602

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-async-context-scopes-20260919
Sep 19, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-async-context-scopes-20260919

Conversation

@steipete

@steipete steipete commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Prepared plugin invocations repeatedly enter four nested async scopes to carry the same generation's registry, metadata, and cache.

User Impact

No user-visible behavior change. Plugin generation scope setup uses less CPU while preserving caller identity, generation selection, cache ownership, and independent cleanup. No configuration changes or migration are required.

Why This Change Was Made

Compose the generation's fields before entering the existing plugin invocation frame. Remove the three separate async stores while retaining each scope owner's readers, parent chains, and independent exit behavior.

The runtime frame keeps Gateway and registry types behind the existing typed frame boundary. Core scope updates copy through that owner, preserving runtime fields without introducing type-level import cycles. Scope contracts live in leaf type modules, the compaction harness's explicit metadata mock follows the new frame-builder API, and the trusted wrapper inventory includes the runtime frame module.

This remains a marginal microbenchmark win. The final correction adds 99 net production/tooling lines, primarily type contracts and typed frame copying; it introduces no new lifecycle or cache owner.

Evidence

Fresh local macOS measurements compare main at 90ab8619b3ec616db61c916dd850d5876d9f42c9 with the final candidate using real metadata, cache, registry, and Gateway scope readers. The affected baseline scope modules are unchanged from the PR base. Each runtime/mode uses 10 warmup batches, followed by 31 alternating baseline/candidate sample pairs of 10,000 generation entries. Async mode awaits Promise.resolve() inside each entry. Values are medians; CPU uses process.threadCpuUsage().

Runtime / mode CPU before → after CPU reduction Wall before → after
v26.8.2 / sync 71.878 → 67.424 ms 6.2% 82.223 → 72.512 ms
v26.8.2 / async 74.771 → 66.862 ms 10.6% 82.367 → 71.249 ms
v24.21.0 / sync 80.519 → 72.589 ms 9.8% 84.753 → 77.708 ms
v24.21.0 / async 80.499 → 69.661 ms 13.5% 88.028 → 73.304 ms

Generation scope entries fall from 4 to 1. Measurements came from a shared host; no live Gateway profile or aggregate CPU reduction is claimed.

  • 451 tests passed across 12 files on Node 24.21.0, covering metadata/cache scopes, invocation and Gateway identity, instance retirement, synthetic auth, provider ownership, and all five affected compaction suites.
  • The original architecture failure was reproduced on the PR; pure main reported zero Madge cycles. The repaired frame boundary also reports zero cycles.
  • All 234 tests in the five compaction suites passed on pure main, confirming the original missing-frame mock failures were introduced by this PR. The corrected candidate passes the same suites.
  • Independent Codex autoreview: scoped-clean through P2, with no accepted/actionable findings on the fully staged candidate.
  • node scripts/check-changed.mjs passed, including formatting, production and test typechecks, targeted lint, dead-export scans, and runtime import-cycle checks. git diff --check passed.

The local checks and review preceded the mechanical rebase onto 88a48ac42f4e6400bd3d06d7bdedf8de030c4696. Its only conflict resolution moved main's existing signal and hasCurrentClientAuthority declarations into the extracted scope type without changing them. Exact-head CI passed after the rebased push and wrapper inventory correction.

The wrapper archive correction also passed a fresh P2 autoreview, the extracted dependency-context test, and four native provisioning checks. The corresponding main checks passed too; the Linux-only filesystem case is deferred to hosted CI on this macOS host.

The observe and compareDelete lifecycle-custody cases in src/plugin-state/plugin-state-worker.test.ts failed with StateDatabaseCoordinatorContentionError on both CI and clean main at 88a48ac42f4e6400bd3d06d7bdedf8de030c4696 (2 failed, 10 passed). That baseline failure is outside this PR and remains unchanged. Exact-head CI passed for 7d79019a5dcc7791dd9b20c3475bf4945ec21afd, including openclaw/ci-gate; the previously failing state-worker shard passed without state-code changes. The early security-review job only reported that CI was not yet complete; its current dependency and security-sensitive review statuses are successful.

@clawsweeper

clawsweeper Bot commented Sep 19, 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.

@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Sep 19, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Sep 19, 2026
@clawsweeper

clawsweeper Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 19, 2026, 4:22 AM ET / 08:22 UTC (Revision 4).

ClawSweeper review

What this changes

Combines plugin invocation, metadata, cache, and runtime context into one async frame, reducing scope-entry overhead while preserving independent cleanup.

Merge readiness

✅ Ready for maintainer review

The optimization remains distinct from current main and the latest release. No actionable correctness or security findings remain in the reviewed head.

Priority: P3
Reviewed head: 7d79019a5dcc7791dd9b20c3475bf4945ec21afd

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A bounded optimization supported by focused benchmarks, meaningful scope-isolation coverage, and successful exact-head CI.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: Verified maintainer-authored internal refactor: the external-contributor proof gate does not apply. Reported macOS benchmarks exercise the changed generation-scope owner with production readers; focused tests and exact-head CI supplement that evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Verified maintainer-authored internal refactor: the external-contributor proof gate does not apply. Reported macOS benchmarks exercise the changed generation-scope owner with production readers; focused tests and exact-head CI supplement that evidence.
Evidence reviewed 10 items Introduced change and review continuity: Inspected the pinned merge-base-to-head changes, including all scope owners, extracted types, test changes, and wrapper inventory. The previous completed review examined this same head and retained no findings; the checkout remains clean and diff whitespace validation passed.
Optimization remains absent from main and release: Current main and v2026.9.5 retain the nested metadata, cache, generation-registry, and Gateway scope entry path. The four central scope modules are unchanged between the PR merge base and fetched main.
Release comparison: The latest release also uses nested scope composition; it does not contain the proposed consolidation.
Findings None None.
Security None None.

How this fits together

Prepared plugin generations supply metadata, caches, and registries to agent and plugin execution. Request-local scope carries those facts alongside Gateway caller context through asynchronous work.

flowchart TD
  A[Prepared plugin generation] --> C[Compose execution frame]
  B[Gateway caller context] --> C
  C --> D[One async scope]
  D --> E[Plugin and agent execution]
  E --> F[Independent scope cleanup]
  F --> G[Restore enclosing context]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +92, Tests +63, Other +1. Total +156 across 16 files.

View PR surface stats
Area Files Added Removed Net
Source 12 307 215 +92
Tests 3 95 32 +63
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 1 0 +1
Total 16 403 247 +156

Review metrics

Metric Value Why it matters
Generation scope entries 4 → 1 The prepared-generation path removes three nested async-store entries without changing its callers.
Production versus test growth Production/tooling net +99; tests/support net +57 The stated production growth funds leaf type contracts and typed context copying while preserving the runtime import boundary.

Technical review

Best possible solution:

Use one internal execution frame while retaining the existing scope readers, ownership checks, and independent cleanup contracts.

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

Not applicable: this is a performance refactor, not a defect report; source confirms the repeated scope entries and the body supplies before/after measurements.

Is this the best way to solve the issue?

Yes: composing the existing context owners removes redundant async entries without adding a competing cache, lifecycle owner, or public plugin API.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P3: This is an internal performance optimization with no reported user-visible failure or configuration change.
  • 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. Not applicable: Verified maintainer-authored internal refactor: the external-contributor proof gate does not apply. Reported macOS benchmarks exercise the changed generation-scope owner with production readers; focused tests and exact-head CI supplement that evidence.

Evidence

What I checked:

  • Introduced change and review continuity: Inspected the pinned merge-base-to-head changes, including all scope owners, extracted types, test changes, and wrapper inventory. The previous completed review examined this same head and retained no findings; the checkout remains clean and diff whitespace validation passed. (7d79019a5dcc)
  • Optimization remains absent from main and release: Current main and v2026.9.5 retain the nested metadata, cache, generation-registry, and Gateway scope entry path. The four central scope modules are unchanged between the PR merge base and fetched main. (src/plugins/runtime/generation-scope.ts:28, ab0433dedc8d)
  • Release comparison: The latest release also uses nested scope composition; it does not contain the proposed consolidation. (src/plugins/runtime/generation-scope.ts:28, ec9c1a13db89)
  • Context preservation: The runtime frame copies core scope changes while retaining the same Gateway scope and generation registry. Metadata/cache parent chains and independent exit operations remain intact. Existing authority callbacks are moved unchanged, without new authorization decisions or persistence. (src/plugins/runtime/execution-frame.ts:20, 7d79019a5dcc)
  • Focused behavioral coverage: The extended invocation test checks generation metadata/cache preservation through nested calls, invocation exit, and rejection. Gateway tests check generation removal and restoration during re-admission; existing metadata tests cover concurrent isolation and lifecycle invalidation. These tests were inspected, not executed during this read-only review. (src/plugins/plugin-instance-invocation.test.ts:30, 7d79019a5dcc)
  • Captured performance evidence: The supplied full PR body reports macOS measurements using production generation-scope entry and real metadata/cache/registry/Gateway readers on Node 24.21.0 and 26.8.2. Across 31 alternating sample pairs, reported median CPU reductions are 6.2–13.5%. This supports scope-entry improvement, not aggregate Gateway CPU savings. Captured context sourceRevision: c1f1d51d4b6648188499cf087872360279f46614a643cc5fa268003377c6e9e6. (7d79019a5dcc)

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; confidence: low)

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 (3 earlier review cycles)
  • reviewed 2026-09-19T06:22:45.128Z sha f9b66fee0010338dd376b45284a2ca1d14665918 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-19T07:32:16.555Z sha 24e871a6b5ad5b235153152aca2654f40d132c95 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-19T07:55:41.940Z sha 7d79019 :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the steipete/perf-micro-async-context-scopes-20260919 branch from f9b66fe to 24e871a Compare September 19, 2026 07:22
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: L and removed size: M labels Sep 19, 2026
@steipete
steipete force-pushed the steipete/perf-micro-async-context-scopes-20260919 branch from 24e871a to 7d79019 Compare September 19, 2026 07:47
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Sep 19, 2026
@steipete
steipete merged commit 1418ad7 into main Sep 19, 2026
220 of 222 checks passed
@steipete
steipete deleted the steipete/perf-micro-async-context-scopes-20260919 branch September 19, 2026 08:46
@steipete

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

agents Agent runtime and tooling maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: L 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