Skip to content

fix(tools): cache fs.existsSync calls in plugin scan path - #94170

Closed
dwc1997 wants to merge 1 commit into
openclaw:mainfrom
dwc1997:fix/76209-cache-exists-sync
Closed

dwc1997 wants to merge 1 commit into
openclaw:mainfrom
dwc1997:fix/76209-cache-exists-sync

Conversation

@dwc1997

@dwc1997 dwc1997 commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Plugin manifest discovery calls fs.existsSync dozens of times across hot paths during cold start. CPU profiling shows significant self-time in these syscalls.

This PR introduces a process-scoped cache in src/shared/cached-fs.ts that maps absolute paths to their fs.existsSync result. The cache lives for the lifetime of the Node process and can be invalidated per-path or fully via invalidateExistsSyncCache().

Changes

  • New file: src/shared/cached-fs.ts - exports existsSyncCached(p) and invalidateExistsSyncCache(p?)
  • New file: src/shared/cached-fs.test.ts - tests for the cache utility
  • Modified: src/plugins/bundle-manifest.ts - replaced 18 fs.existsSync calls
  • Modified: src/plugins/bundled-dir.ts - replaced 14 fs.existsSync calls
  • Modified: src/plugins/sdk-alias.ts - replaced 9 fs.existsSync calls

Testing

Tests verify:

  • Cache returns same results as fs.existsSync
  • Repeated lookups use cached values (no redundant syscalls)
  • Per-path and full cache invalidation works correctly

Fixes #76209

Real behavior proof (required for external PRs)

  • Behavior or issue addressed: Plugin manifest discovery calls fs.existsSync dozens of times across hot paths during cold start, causing significant syscall overhead.
  • Real environment tested: Windows 10, Node.js v22.22.3, OpenClaw latest main, local checkout with the patch applied.
  • Exact steps or command run after this patch: Ran node scripts/run-vitest.mjs run src/shared/cached-fs.test.ts with tests for cache hit, invalidation, and parity with fs.existsSync.
  • Evidence after fix:
$ node scripts/run-vitest.mjs run src/shared/cached-fs.test.ts
Test Files  1 passed (1)
     Tests  6 passed (6)
  • Observed result after fix: existsSyncCached() returns the same results as fs.existsSync and caches lookups so repeated calls for the same path do not trigger additional syscalls. invalidateExistsSyncCache() correctly clears individual paths or the full cache.
  • What was not tested: Full cold-start timing benchmark (requires profiling setup). The test verifies correctness and caching behavior, not wall-clock performance gains.

Current review state

  • Pre-existing CI failure (not from this PR): check-test-types fails on extensions/msteams/src/send.test.ts lines 663/713 with TS2493 tuple type error -- this file is not in the PR diff.
  • AI-assisted (built with Claude Code)

Plugin manifest discovery calls fs.existsSync dozens of times across
hot paths during cold start. CPU profiling shows significant self-time
in these syscalls.

Introduce a process-scoped cache in src/shared/cached-fs.ts that maps
absolute paths to their fs.existsSync result. The cache lives for the
lifetime of the Node process and can be invalidated per-path or fully
via invalidateExistsSyncCache().

Replace fs.existsSync calls in:
- src/plugins/bundle-manifest.ts (18 calls)
- src/plugins/bundled-dir.ts (14 calls)
- src/plugins/sdk-alias.ts (9 calls)

Fixes openclaw#76209
@openclaw-barnacle openclaw-barnacle Bot added size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 17, 2026
@clawsweeper

clawsweeper Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close: the useful plugin-scan fs.existsSync performance fix is superseded by the merged scan-scoped cache in #93919, while this PR’s process-lifetime cache is broader and can stale install or diagnostic filesystem probes.

Root-cause cluster
Relationship: superseded
Canonical: #93919
Summary: This PR and the merged scan-scoped cache target the same repeated plugin-scan existence probes; the merged PR is the canonical implemented path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Keep the merged scan-scoped cache and handle any remaining bundled-dir or sdk-alias startup hot spots as a fresh, profiled, lifecycle-owned follow-up.

So I’m closing this here and keeping the remaining discussion on #93919.

Review details

Best possible solution:

Keep the merged scan-scoped cache and handle any remaining bundled-dir or sdk-alias startup hot spots as a fresh, profiled, lifecycle-owned follow-up.

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

Yes for the patch defect: source inspection shows this branch would cache existence results process-wide while current main tests require later scans to re-read filesystem state. I did not rerun the original Windows cold-start profile in this read-only review.

Is this the best way to solve the issue?

No. A cache is the right direction, but the merged scan-scoped implementation is the narrower maintainable fix; this branch’s process-lifetime cache is too broad for install and diagnostic freshness.

Security review:

Security review cleared: No concrete security or supply-chain concern was found; the diff adds no dependencies, workflow changes, secrets handling, network access, or code-execution surface.

AGENTS.md: found and applied where relevant.

What I checked:

  • Root policy read: Root AGENTS.md was read fully; its plugin hot-path guidance requires prepared facts and bounded lifecycle-owned caches rather than scattered request-time discovery caches. (AGENTS.md:145, d1b917120a47)
  • Scoped plugin policy read: The scoped plugin guide allows bounded lifecycle-owned process caches, but no broad persistent caches; this made cache lifetime central to the review. (src/plugins/AGENTS.md:21, d1b917120a47)
  • Current main cache boundary: Current main memoizes only inside withPluginScanExistenceCache; outside an active scan, pluginScanExistsSync calls plain fs.existsSync, avoiding process-global staleness. (src/plugins/plugin-scan-existence-cache.ts:39, d1b917120a47)
  • Current main wraps the target scan path: discoverBundleInRoot wraps the bundle-format detect/load pair in the scan-scoped cache, covering the repeated bundle manifest probe path this PR targets. (src/plugins/discovery.ts:813, d1b917120a47)
  • Freshness regression coverage: Current tests verify filesystem state is re-read across separate scans and that the detect/load pair makes fewer fs.existsSync calls under the scan cache. (src/plugins/plugin-scan-existence-cache.test.ts:89, d1b917120a47)
  • Existing diagnostic must stay live: Current main leaves the source-checkout node_modules/.pnpm diagnostic as a direct fs.existsSync probe, and adjacent tests cover the missing-dependency diagnostic path. (src/plugins/bundled-dir.ts:119, d1b917120a47)

Likely related people:

  • ml12580: Authored the merged scan-scoped cache PR for the same linked issue and central plugin discovery files. (role: canonical fix author; confidence: high; commits: 1585ec54f1a6; files: src/plugins/bundle-manifest.ts, src/plugins/discovery.ts, src/plugins/plugin-scan-existence-cache.ts)
  • ottodeng: Opened an earlier unmerged attempt for the same fs.existsSync plugin-scan issue and explicitly preserved the bundled-dir dependency diagnostic as a live probe. (role: prior attempt author; confidence: medium; commits: 2953b3c707bc; files: src/plugins/bundle-manifest.ts, src/plugins/bundled-dir.ts, src/plugins/sdk-alias.ts)

Codex review notes: model internal, reasoning high; reviewed against d1b917120a47; fix evidence: commit 1585ec54f1a6, main fix timestamp 2026-06-22T18:27:36Z.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 17, 2026
@clawsweeper clawsweeper Bot mentioned this pull request Jun 17, 2026
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 18, 2026
@dwc1997 dwc1997 closed this Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:

1 participant