Skip to content

fix(core): cache compiled wildcard patterns - #63158

Closed
ZM-BAD wants to merge 1 commit into
anomalyco:devfrom
ZM-BAD:wildcard-cache
Closed

ZM-BAD wants to merge 1 commit into
anomalyco:devfrom
ZM-BAD:wildcard-cache

Conversation

@ZM-BAD

@ZM-BAD ZM-BAD commented Nov 10, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #63099

Type of change

  • Bug fix

What does this PR do?

Tool.snapshot calls Wildcard.match once per tool and permission rule on every provider step. Each call compiled a new RegExp; #63099 profiled this at 24–32% of service CPU. This caches compiled patterns by source string with a bounded FIFO (1024), so a large rule set cannot grow the cache without limit.

AI assistance: used to help draft the patch; I reviewed the diff and ran the verification below.

How did you verify your code works?

  • packages/core/test/wildcard.test.ts: 6 pass, 0 fail; packages/core/test/permission.test.ts: 122 pass, 0 fail.
  • bun run typecheck in packages/core: no type errors (2,013 files / 2 projects).
  • oxlint and prettier --check on both changed files: clean.
  • Bun 1.4.4 microbenchmark (1,200 tools × 60 rules × 20 passes, no matches): ~279.5 ms → ~45.0 ms (~6.2x), same match count.

Visual

N/A — internal performance fix; no user-facing behavior change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Tool.snapshot calls Wildcard.match once per tool and permission rule on
every provider step. Each call compiled a new RegExp; the profiling in
anomalyco#63099 showed this at 24-32% of service CPU.

Cache compiled patterns by source string, with a bounded FIFO so an
unusual rule set cannot grow the cache without limit.

Fixes anomalyco#63099
@opencodejr opencodejr Bot closed this Nov 10, 2026
@opencodejr

opencodejr Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix, and for the benchmark numbers on #63099!

I think this duplicates #55322, which already proposes the same change: it caches compiled regexes in Wildcard.match per pattern, with a size limit and oldest-first eviction, and adds packages/core/test/wildcard.test.ts. Its cache key also includes the regex flags, so win32 case-insensitive matching is kept.

If that pull request is missing something, for example your 1,200 tools × 60 rules benchmark or the link to #63099, a review, test results or a comment there would be very welcome.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core: Tool.snapshot compiles a RegExp per tool per permission rule on every step (24-32% of service CPU)

1 participant