Skip to content

perf(plugins): cache fs.existsSync probes during plugin scan - #76350

Closed
ottodeng wants to merge 11 commits into
openclaw:mainfrom
ottodeng:fix/issue-76209-cache-exists-sync
Closed

ottodeng wants to merge 11 commits into
openclaw:mainfrom
ottodeng:fix/issue-76209-cache-exists-sync

Conversation

@ottodeng

@ottodeng ottodeng commented May 3, 2026

Copy link
Copy Markdown
Contributor

Plugin manifest discovery and bundled directory scanning call fs.existsSync 41 times across hot paths during cold start. CPU profiling on Windows reported fs.existsSync self-time of ~25 s (≈50% of profile) for a single cold start (#76209).

This PR adds a process-level memoization cache (src/shared/cached-fs.ts) and routes those 41 probes through it across:

  • src/plugins/bundle-manifest.ts
  • src/plugins/bundled-dir.ts
  • src/plugins/sdk-alias.ts

The pnpm-workspace diagnostic in bundled-dir keeps a direct fs.existsSync so it re-evaluates after the user runs pnpm install (preserves the existing test contract).

Why caching is safe here

Paths probed during the plugin scan are stable for the lifetime of the process — no scan-time path creation. The cache exposes invalidateExistsSyncCache for tests / future callers that need to force a fresh probe.

Tests

  • New unit tests in src/shared/cached-fs.test.ts cover hits, misses, single-key invalidation, and full clear.
  • pnpm test:changed green (plugins shard: 83 tests pass).

Fixes #76209

Plugin manifest discovery and bundled directory scanning probe the same
read-only paths repeatedly during cold start (`<rootDir>/skills`,
`<rootDir>/.cursor/rules`, etc.). CPU profiles on Windows show
fs.existsSync as the single largest self-time contributor (~25s of
~50s cold start).

Add a process-level memoization cache and route the 41 fs.existsSync
call sites in bundle-manifest, bundled-dir, and sdk-alias through it.
The pnpm-workspace diagnostic in bundled-dir keeps the uncached
fs.existsSync call so it re-evaluates after `pnpm install`.

Fixes openclaw#76209
@clawsweeper

clawsweeper Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Summary
The PR adds a per-scan fs.existsSync cache across plugin bundle manifest, bundled directory, and SDK alias scanning, plus cache tests and changelog entries.

Reproducibility: yes. Current main still contains the direct fs.existsSync probes the PR targets, and the branch-local regression is reproduced by current-head CI annotations in src/plugins/sdk-alias.ts.

Real behavior proof
Needs stronger real behavior proof before merge: Older profiling/live-output evidence predates the current per-scan rewrite; the latest head only shows unit-test proof.

Next step before merge
Contributor action is needed for current-head real behavior proof, plus the PR still has branch-local SDK alias and changelog blockers.

Security
Cleared: No concrete security or supply-chain regression found in the TypeScript filesystem probing, tests, or changelog diff.

Review findings

  • [P1] Thread resolveExists into public-surface aliases — src/plugins/sdk-alias.ts:442
  • [P2] Remove the unrelated changelog entries — CHANGELOG.md:73-74
Review details

Best possible solution:

Land the per-scan cache only after public-surface alias lookup shares the scan resolver, unrelated changelog lines are removed, CI is green, and current-head runtime or profiling proof shows the startup improvement.

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

Yes. Current main still contains the direct fs.existsSync probes the PR targets, and the branch-local regression is reproduced by current-head CI annotations in src/plugins/sdk-alias.ts.

Is this the best way to solve the issue?

No, not as submitted. A per-scan cache is the right boundary, but the public-surface alias helper must receive the same resolver and the changelog/proof blockers need closure first.

Full review comments:

  • [P1] Thread resolveExists into public-surface aliases — src/plugins/sdk-alias.ts:442
    resolveLoaderPluginSdkPackageRoot now requires resolveExists, but resolveBundledPluginPackagePublicSurfaceAliasMap still calls it without that field. Current-head CI reports this exact type error and plugin contract tests hit params.resolveExists is not a function, so pass the scan resolver through this helper and its target lookup.
    Confidence: 0.99
  • [P2] Remove the unrelated changelog entries — CHANGELOG.md:73-74
    This PR adds release notes for ACPX/Google Chat/LINE beta package specs and CLI session cleanup, but the diff is about plugin discovery filesystem caching. Drop those entries or move them to the PRs that own those changes before merging.
    Confidence: 0.96

Overall correctness: patch is incorrect
Overall confidence: 0.96

Acceptance criteria:

  • pnpm test src/shared/cached-fs.test.ts
  • pnpm test src/plugins/bundle-manifest.test.ts
  • pnpm test src/plugins/bundled-dir.test.ts
  • pnpm check:changed

What I checked:

  • Current main has the targeted probes: Current main still calls fs.existsSync directly in plugin manifest and bundled plugin scan paths, including default skill and bundled tree probes. (src/plugins/bundle-manifest.ts:145, 9fa685e3b3e4)
  • Plugin cache boundary: The plugin boundary guide permits caller-owned current-flow caches while warning against persistent discovery metadata caches, which supports the per-scan direction. (src/plugins/AGENTS.md:30, 678323d01390)
  • PR head still omits resolver threading: At PR head, resolveBundledPluginPackagePublicSurfaceAliasMap calls resolveLoaderPluginSdkPackageRoot without passing the now-required resolveExists callback. (src/plugins/sdk-alias.ts:442, 2953b3c707bc)
  • CI confirms branch-local failure: The current-head check-prod-types annotation reports the missing resolveExists property at src/plugins/sdk-alias.ts:442, and plugin contract annotations show runtime params.resolveExists is not a function failures. (src/plugins/sdk-alias.ts:442, 2953b3c707bc)
  • Unrelated changelog entries remain: The PR head adds ACPX/Google Chat/LINE beta package and CLI session cleanup release notes that are unrelated to filesystem probe caching. (CHANGELOG.md:73, 2953b3c707bc)
  • Real behavior proof gap: The PR still has triage: needs-real-behavior-proof; the latest contributor comment lists unit tests for the per-scan rewrite, while the older profiling/live-output evidence predates that rewrite. (2953b3c707bc)

Likely related people:

  • Peter Steinberger: Local history for the central plugin scan and SDK alias files is dominated by Peter-authored commits, including plugin SDK alias wrapper work and bundled plugin resolution changes. (role: recent maintainer / primary history owner; confidence: high; commits: 4862d349257a, 324cddee4c18, 1c924c3c126d; files: src/plugins/sdk-alias.ts, src/plugins/bundle-manifest.ts, src/plugins/bundled-dir.ts)
  • vincentkoc: The PR timeline explicitly mentions/subscribes vincentkoc on this plugin-scan work, and local history shows Vincent Koc in adjacent plugin install and integrity handling changes. (role: adjacent plugin maintainer / review-route candidate; confidence: medium; commits: f392b81e9502, 428d1761b43e, cf311978ea61; files: src/plugins/bundled-dir.ts, src/plugins/sdk-alias.ts)

Remaining risk / open question:

  • Current-head performance impact is not yet proven after the per-scan rewrite.
  • The PR head has SDK alias type and runtime failures tied to the unresolved resolver threading gap.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 9fa685e3b3e4.

Address clawsweeper [P2]: the previous module-level Map<string, boolean> in
src/shared/cached-fs.ts persisted for the lifetime of the Node process.
A false miss recorded during one plugin scan could be re-served on later
scans after marker files (skills/, .cursor/rules, SDK artifacts) appeared
in the same process — violating the plugin-discovery freshness boundary.

Refactor to a closure-owned cache:

- Replace existsSyncCached / invalidateExistsSyncCache with a
  createExistsSyncCache() factory returning an ExistsSyncCache instance.
- Thread an optional resolveExists callback (or ExistsSyncCache instance)
  through bundle-manifest, bundled-dir, and sdk-alias entry points. Each
  top-level scan creates ONE cache and passes it down to its private
  helpers; without one the helpers fall back to plain fs.existsSync (no
  cache) so one-off callers stay correct.
- Tests now cover the per-instance shape and add a regression for cross-
  instance isolation (caching a false in instance A does not leak into
  instance B), plus an explicit stale-false invalidate-and-reprobe path.

Verified locally:
  pnpm test src/shared/cached-fs.test.ts            (6 passed)
  pnpm test src/plugins/bundle-manifest.test.ts     (15 passed)
  pnpm test src/plugins/bundled-dir.test.ts         (19 passed)
@ottodeng

ottodeng commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

Addressed clawsweeper [P2] + [P3] in aa560046e7:

Bug: the previous existsSyncCached lived in a module-level Map<string, boolean> that persisted for the entire Node process. A false miss recorded while scanning one plugin root could be re-served by later plugin discovery passes once skills/, .cursor/rules, or SDK artifact marker files appeared in the same process — violating the plugin-discovery freshness boundary the reviewer flagged.

Surface: plugin scan/install paths in src/plugins/{bundle-manifest,bundled-dir,sdk-alias}.ts (called from cold-start plugin discovery and openclaw plugins … install/repair flows).

Fix: replaced the bare exports with a createExistsSyncCache() factory that returns an ExistsSyncCache { existsSync, invalidate } whose underlying Map lives in the closure. Threaded a resolveExists callback through every private helper in the three caller files; each top-level scan creates ONE cache and passes it down. Helpers fall back to plain fs.existsSync (no cache) when no cache is supplied, so one-off callers remain correct and the process-global state is fully removed.

[P3] CHANGELOG: added the required ## Unreleased → ### Fixes entry attributing me.

Why best: keeps the cold-start CPU win (fs.existsSync was the largest self-time on the original profile in #76209) without retaining the scope problem. The factory shape is the minimal seam — callers that already own a scan boundary already pass the cache; future callers that don't own one pay the original fs.existsSync cost rather than corrupting a shared cache. Regression coverage added: cached-fs.test.ts now asserts two instances do not share state, and that a stale false can be invalidated and re-probed after the file is created.

Verified locally:

  • pnpm test src/shared/cached-fs.test.ts → 6 passed
  • pnpm test src/plugins/bundle-manifest.test.ts → 15 passed
  • pnpm test src/plugins/bundled-dir.test.ts → 19 passed

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling commands Command implementations labels May 3, 2026
@ottodeng
ottodeng force-pushed the fix/issue-76209-cache-exists-sync branch from 7771c0d to 60ebf0d Compare May 4, 2026 06:14
@openclaw-barnacle openclaw-barnacle Bot removed commands Command implementations agents Agent runtime and tooling labels May 4, 2026
@ottodeng
ottodeng force-pushed the fix/issue-76209-cache-exists-sync branch 5 times, most recently from 909b9a3 to 0c047ea Compare May 4, 2026 22:41
@ottodeng
ottodeng force-pushed the fix/issue-76209-cache-exists-sync branch from 0c047ea to 8f4ae59 Compare May 5, 2026 00:45
@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. label May 5, 2026
@ottodeng

ottodeng commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

Closing — CI shows 15 failures across the matrix and the change has grown beyond what a perf-only PR should be (size: L). Will revisit as a smaller, scoped patch with a real benchmark proof if the perf gain still holds on current main. Thanks.

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

Labels

size: L triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:

1 participant