Skip to content

Key a budget-declined pattern to its own schema - #2969

Merged
cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2934-pattern-budget-cache-key
Nov 12, 2026
Merged

cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2934-pattern-budget-cache-key

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2934

Problem

Ajv's usePattern registers each regex in the engine-wide ValueScope under rx.toString(), and ValueScope.value() returns the first entry registered for a key. LinearPattern.toString() returned /${source}/${flags}, so a LinearPattern declined with COMPILE_BUDGET_EXHAUSTED while compiling one over-budget schema became the entry every later schema on that engine tested against — the later schema's own compile ran against its fresh budget, and its result was discarded. A heavy outputSchema on one tool made other tools' structured output (and app elicitation results) read as "could not be verified" for the rest of the session.

Fix

core/mcp/linearPatternValidator.ts: LinearPattern computes its scope key once, in its constructor.

  • A pattern that compiled, or was declined for a reason that depends only on the pattern (unsupported construct, too long, too complex), keeps the shared /${source}/${flags} key, so it is still compiled once per engine.
  • A pattern declined because the schema's compile budget ran out gets that key plus a #<n> suffix from a module-level counter, so it stays with the schema whose budget it reflects.

The doc comment on the new key field replaces the old "per-schema pattern cache" comment, which was inaccurate (the cache is engine-wide). tracked(), the budgets and getValidator are unchanged.

Tests

New case in the "bounded pattern compilation" block of clients/web/src/test/core/mcp/linearPatternValidator.test.ts: an over-budget schema declines ^[a-z]+$; a later { type: "string", pattern: "^[a-z]+$" } on the same provider then validates "abc" and rejects "ABC" with the ordinary must match pattern error; the heavy schema still reports its own decline. Fails on v2/main, passes with the change. npm run local:gate is green.

Approach follows the prototype prompt shared on the issue.

🤖 Generated with Claude Code

Ajv registers each pattern in the engine-wide scope under rx.toString()
and hands back the first entry for a key, so a LinearPattern declined
because one schema's compile budget ran out was reused by every later
schema on the engine. Give such a decline a unique key; every other
outcome depends only on the pattern and keeps the shared key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no outstanding review issues requiring changes.

No open issues 🎉

What changed in this PR

Fixes #2934 in the shared schema validator so a pattern declined because one schema exhausted its compile budget does not affect later schemas on the same validator.

Changes:

  • Assigns unique cache keys to budget-declined patterns while preserving shared keys for pattern-only outcomes.
  • Corrects the cache documentation to describe Ajv’s engine-wide scope.
  • Adds regression coverage for later-schema validation and continued reporting of the original schema’s decline.
File Description
core/​mcp/​linearPatternValidator.ts Isolates budget-declined pattern cache entries and clarifies scope-key behavior.
clients/​web/​src/​test/​core/​mcp/​linearPatternValidator.test.ts Tests that an over-budget schema does not poison a later schema’s pattern validation.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review closed: round 1 was clean (no inline comments, no suppressed findings), so the loop stopped on the first clean round.

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

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema validation: a pattern declined for one schema's compile budget stays declined for every later schema in the session

2 participants