Skip to content

fix(mcp): re-emit advertised patterns in a portable regex grammar - #10582

Merged
morluto merged 26 commits into
mainfrom
fix/portable-advertised-patterns
Nov 15, 2026
Merged

morluto merged 26 commits into
mainfrom
fix/portable-advertised-patterns

Conversation

@morluto

@morluto morluto commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

The presentation owner now re-emits every advertised pattern, in input and output schemas, into the portable regex grammar from #10567. When a pattern has no exact portable form it is widened, or omitted when it has no form at all. Server-side validation is unchanged: the Zod contracts still validate the real rule. The pattern entries leave the dialect baseline, so every advertised pattern is now enforced at zero in every profile, on the Windows lane and in the packaged catalog check.

Stacked on #10567 (base branch test/advertised-dialect-checker).

Refs #10389 #10436 #7151

Problem and expected behavior

#10567 measured the patterns advertised today that fall outside the grammar that compiles in V8 (""/u/v), Python re, Go regexp (RE2), Rust regex and fancy-regex:

  • Default compact input: 30 patterns (\s, unanchored ^\/ prefixes).
  • Full input: 135 patterns. These include \d, ., an unescaped ^ in a class, and \x80-\xff.
  • Full output: 67 patterns (\d, ., unanchored base64 probes).

Each past fix escaped one construct at one source site (\x2d, \x2f, \x00, look-around rewritten as not). Nothing stopped the next site from reintroducing a construct.

Expected: whatever a contract author writes, the advertised pattern is portable. The advertisement may widen a constraint, but it never narrows one.

Change and scope

  • src/contracts/portablePattern.ts parses an ECMAScript (u) pattern and re-emits it. It returns:

    • exact, for the same strings;
    • wider, for a superset;
    • unexpressible, for backreferences, inline flags and empty classes.

    How it re-emits:

    • Classes become an exact ASCII member set computed by V8 itself, emitted as merged ranges with \-, \/, \^ and \xHH escapes. A class's non-ASCII members become an exact or widening [^\x00-\x7f] alternative, or are removed from a negated class.
    • Shorthands: \d becomes [0-9] and \w becomes [A-Za-z0-9_], both exact. \s and \S become ASCII-whitespace forms (wider). . becomes [^\n\r] (wider).
    • Look-around and \b are removed (wider).
    • Unanchored alternatives are completed exactly with [^\x00]*(?:\x00[^\x00]*)*. This is a single-class loop, so V8 does not keep one backtracking frame per character.
    • Repeats above 1,000 are opened (wider).
    • Every result is checked against the grammar again before it is used.
  • inputSchemaPresentation.ts applies the projection inside the existing normalization:

    • A widened or omitted pattern marks its site as changed. The existing dependency analysis then removes an enclosing not or if/then/else, turns oneOf into anyOf, and handles $ref dependents. The advertisement therefore only widens. Dropping a pattern under not would instead narrow the schema.
    • An exact rewrite changes no dependent.
    • Literal rewriting is now an option, so the same pass serves outputs without touching their literals.
  • presentOutputJsonSchema is applied in toolOutputSchemaWithMetadata. Output schemas whose patterns are all exact take a structural copy instead of the dependency analysis, because output schemas reach several megabytes.

  • advertisedDialectBaseline.ts loses every pattern key and the output baseline, which is now empty. The full input oneOf count drops from 223 to 219, because four unions whose branches widened become anyOf. The test and the packaged verifier now enforce output patterns at zero.

  • Tests that matched advertised pattern spellings (canonical base64, JavaScript route paths) now compare against the projected form. Each still checks exact Ajv semantics on line terminators and padding.

  • Docs (testing.md): source regexes no longer need client-oriented escaping.

Intentionally not included:

  • Rewriting the 39 source files that still spell \x2d/\x2f for clients. They are harmless and the projection now covers them. Changing canonical server regexes is churn without client benefit, so I propose it as an optional follow-up.
  • rejectUnpairedSurrogates, which stays as it is: a \uD800 class would project to "any character", the same as advertising nothing.
  • The broader projector (W3 step 3), changes to default profiles or output schemas, and removal of the full profile.

Contract and boundary impact

  • Semantic owner and earliest changed stage: the presentation (inputSchemaPresentation.ts, using portablePattern.ts) owns how a pattern is advertised. The canonical Zod contracts and server-side validation are unchanged.

  • Contribution scope fit and linked maintainer decision: not applicable (advertisement-only fix within existing boundaries).

  • CLI and MCP/tool-catalog contract: the pattern values in tools/list change in every profile. All changes are exact or widening; widened sites drop enclosing not/if or relax oneOf to anyOf. Catalog digests change accordingly and are regenerated at build. Examples from the default catalog:

    • ^\/ becomes ^\/[^\x00]*(?:\x00[^\x00]*)*$ (exact).
    • ^[^\x00\s"']+$ becomes ^[^\x00\x09-\x0d "']+$ (wider: it now admits non-ASCII spaces).
    • ^-?\d+$ becomes ^-?[0-9]+$ (exact).
  • Provider, bridge, target-format, or platform compatibility: none.

  • Evidence, artifact, provenance, or reconstruction contract: none.

  • Target selection, authority, upstream behavior, and cleanup details: not applicable.

  • Generated metadata, package, or installation impact: the recorded catalogs change with the advertised patterns.

  • Documentation or usability only; no target interaction.

Evidence and regression coverage

  • Tests added or updated:
  • Base reproduction: on test(mcp): check advertised schemas against a portable dialect in every profile #10567 alone, the catalog test records the 30, 135 and 67 non-grammar patterns above. With this change, observed counts for every pattern key are zero.
  • Cross-engine check (run locally, not in CI): every distinct pattern advertised across the compact and full input × full and compact output profiles (183 patterns) compiles in:
    • Go 1.26 regexp;
    • Rust regex and fancy-regex, using the audit harness;
    • Python 3.14 re.
  • Remaining proof gaps:
    • No live provider was probed.
    • CI runs V8 only. The other engines were checked locally with the audit's harness.

Validation performed

  • npm run check:fast: passed.

  • npm run docs:check: passed.

  • npm run test:local -- src/contracts/portablePattern.test.ts: 46 tests passed.

  • npm run test:focused -- with the 25 schema-related files: 354 tests passed. The files are:

    • src/contracts: portablePattern, compactOutputSchema, inputObservationPresentation, toolInputReferences, toolOutputReferences, canonicalBase64Metadata;
    • src/server: registerGuidedWorkflowTool, JavaScriptRouteAdvertisedSchema;
    • tests/boundary/mcp: advertisedDialect, advertisedPatternsVMode, contractPresentation, deferredRuntime, mcpDoctor, serverIdentity, stdioFrameDiagnostics, processCaptureCleanupSchema, compactInputSchemaMcp, toolSchemaValidity, packageCatalogVerification, browserScenarioMcp, officialTools, absoluteLocalPathSchema, applicationEvidenceMcp;
    • tests/conformance/contracts/mcpOutputSchemaProfile;
    • tests/conformance/verifiers/clientSchemaPatterns.
  • Local cross-engine compile of the 183 advertised patterns (Go, Rust, fancy-regex, Python): all compiled.

  • Performance: output presentation over all 255 output schemas (4.26 MB) took about 100 to 175 ms of CPU in one process. This is an estimate on a machine shared with other jobs. It runs once per contract per process, because the projection is memoized.

    • First in-process tools/list at default profiles: about 0.8 to 1.0 s wall time, against about 0.6 to 1.0 s before. Runs were noisy and the difference is within run-to-run variance.
    • Peak memory was not measured.
  • Review follow-up (4d49945, 95eda9b): npx vitest run src/contracts/portablePattern.test.ts tests/boundary/mcp/advertisedDialect.test.ts tests/boundary/mcp/advertisedPatternsVMode.test.ts src/contracts/toolInputReferences.test.ts src/contracts/inputObservationPresentation.test.ts: 165 passed. The new regressions (references into removed not/if/then/else, $dynamicRef, astral literal in V8 plain mode) each failed before their fix.

  • Second follow-up (95d1fcb): astral literals are now omitted (unexpressible) instead of projected, which removes the backtracking form 95eda9b introduced. Unresolved $ref sites, such as an embedded resource reached by $id, are relaxed when anything widens. Same vitest set plus toolSchemaMetadata.test.ts: 168 passed. The embedded-resource regression failed before the fix.

  • Third follow-up (d7aa9c8, b71c9c1): union collapse and pointer protection both decide from canonical branches (collapsesToBoolean); the four new relaxed-reference union regressions failed on 897cd48. Non-u astral matching in single wildcard/shorthand/negated-class positions is documented as an accepted limitation, and the test pins equal BMP acceptance in V8 plain, u and v modes. npm run test:local -- src/contracts/portablePattern.test.ts: 78 passed. npm run test:focused -- tests/boundary/mcp/advertisedPatternsVMode.test.ts: passed. npm run check:fast: passed. Offline, projected patterns over 1922 BMP samples: Go regexp matched V8 u exactly; Python re differed only on strings ending in \n (its $ matches before a final newline, wider only). Rust was not run. Also restored the cancellation paragraph in testing.md that an earlier commit dropped by mistake.

  • Fourth follow-up (b44abc9, 8d6721c): canonicalBase64Metadata.test.ts now expects the advertisement to admit AB== (the unbounded pad-bit probe is omitted on the wire; the server still refuses it); references into unevaluatedProperties/unevaluatedItems removed by lost evaluation are protected. New regressions failed before the fix. npm run test:focused over 22 schema-related files: 378 passed (before 8d6721c); after it, portablePattern, canonicalBase64Metadata, advertisedDialect, advertisedPatternsVMode, toolInputReferences, toolOutputReferences, toolSchemaValidity: all passed. Merged current main.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. Advertised constraints only widen, and canonical validation is unchanged.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; Windows comes from the curated lane added in test(mcp): check advertised schemas against a portable dialect in every profile #10567.
  • Package or release metadata impact: the recorded MCP catalogs and digests change with the advertised patterns.
  • Security, privacy, process, or containment review: none.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T08:48:50.407130Z 211eb08 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 810a8e25f2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/contracts/portablePattern.ts Resolved
@greptile-apps

greptile-apps Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5 Tier: plus

[Medium impact] The changes affect regex patterns shown across MCP tool catalogs, but not server-side validation.

Fix repeated alternatives that overlap after widening before merging.

Fix All in CodexFindings

  1. P1 Repeated alternatives freeze clients ▶
Fix with agent prompt
### Issue 1
src/contracts/portablePattern.ts:187-190
Widening separate characters can make repeated alternatives overlap. `^(?:é|è)+$` becomes `^(?:[^\x00-\x7f]|[^\x00-\x7f])+$`, which both presenters advertise. On Node 24.22.3, rejecting `"é".repeat(28) + "A"` took about 1.9 seconds with the projected pattern, versus 0.03 ms with the source. Longer mismatches can freeze a client that checks requests or responses.

Combine overlapping alternatives before repeating them, or omit patterns whose widened form is unsafe. Add a rejected-string regression.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Re-emits advertised input and output patterns in a portable grammar while keeping canonical validation unchanged.

  • Pattern projection now emits regexes in a grammar shared by clients.
  • MCP tool schemas now show portable patterns for inputs and outputs.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Canonical contracts] --> B[JSON Schema conversion]
  A --> V[Canonical server validation]
  B --> C[Portable pattern projection]
  C --> D{Projection result}
  D -->|Exact| E[Replace pattern]
  D -->|Wider or omitted| F[Relax dependent constraints and references]
  E --> G[Advertised input and output schemas]
  F --> G
  G --> H[Catalog and package grammar checks]
Loading

Reviews (18) · Last reviewed commit: "Merge branch 'main' into fix/portable-ad..." · Reviewed by Greptile

Comment thread src/contracts/portablePattern.ts Resolved
Comment thread src/contracts/portablePattern.ts Resolved
… into HEAD

# Conflicts:
#	scripts/lib/verify-package-core.mjs
#	src/contracts/advertisedDialectBaseline.ts
#	tests/boundary/mcp/advertisedDialect.test.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dfb8ca0751

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Base automatically changed from test/advertised-dialect-checker to main November 14, 2026 20:20
# Conflicts:
#	docs/development/testing.md
#	scripts/lib/verify-package-core.mjs
#	src/contracts/advertisedDialectBaseline.ts
#	tests/boundary/mcp/advertisedDialect.test.ts
#	tests/boundary/mcp/advertisedPatternsVMode.test.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 01d0b22a59

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d499450af

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 897cd485ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/contracts/portablePattern.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b44abc9c13

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d6721cdab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/contracts/portablePattern.ts Resolved
Comment on lines +187 to +190
#group(): string {
return this.#disjunction()
.map((terms) => terms.join(""))
.join("|");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Repeated alternatives freeze clients

Widening separate characters can make repeated alternatives overlap. ^(?:é|è)+$ becomes ^(?:[^\x00-\x7f]|[^\x00-\x7f])+$, which both presenters advertise. On Node 24.22.3, rejecting "é".repeat(28) + "A" took about 1.9 seconds with the projected pattern, versus 0.03 ms with the source. Longer mismatches can freeze a client that checks requests or responses.

Combine overlapping alternatives before repeating them, or omit patterns whose widened form is unsafe. Add a rejected-string regression.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/contracts/portablePattern.ts
Line: 187-190

Comment:
**Repeated alternatives freeze clients**

Widening separate characters can make repeated alternatives overlap. `^(?:é|è)+$` becomes `^(?:[^\x00-\x7f]|[^\x00-\x7f])+$`, which both presenters advertise. On Node 24.22.3, rejecting `"é".repeat(28) + "A"` took about 1.9 seconds with the projected pattern, versus 0.03 ms with the source. Longer mismatches can freeze a client that checks requests or responses.

Combine overlapping alternatives before repeating them, or omit patterns whose widened form is unsafe. Add a rejected-string regression.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex

@Varshith-Kali Varshith-Kali mentioned this pull request Nov 18, 2026
2 tasks done
@morluto morluto added area: mcp MCP server, transport, and tool registration bug Something isn't working labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: mcp MCP server, transport, and tool registration bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant