Skip to content

Cache compiled RegExp objects for StringPart matching; fix serialization#2186

Merged
curtisman merged 4 commits intomainfrom
copilot/cache-regex-for-string-part
Apr 11, 2026
Merged

Cache compiled RegExp objects for StringPart matching; fix serialization#2186
curtisman merged 4 commits intomainfrom
copilot/cache-regex-for-string-part

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 11, 2026

Cache compiled RegExp objects for StringPart matching and fix StringPart serialization in .ag.json output.

Changes

Cache compiled RegExp objects (grammarMatcher.ts, grammarTypes.ts)

matchStringPart was recompiling the same RegExp on every invocation. This adds a lazy per-StringPart cache keyed by (spacingMode, leadingIsNone) so the compiled regex is reused across calls:

  • Add StringPartRegExpEntry type and regexpCache: Map field to StringPart
  • Extract buildStringPartRegExpStr helper from matchStringPart
  • Add getStringPartRegExp for lazy cache lookup/population
  • Pass cached RegExp into matchStringPartWithWildcard / matchStringPartWithoutWildcard
  • Hoist the separator regex constant in grammarCompletion.ts

Fix StringPart serialization (grammarSerializer.ts)

grammarPartToJson was returning StringPart objects directly via return p. Because regexpCache is a Map, JSON.stringify serializes it as {}, polluting .ag.json output. Fix: add an explicit case "string": that projects only { type, value } to match StringPartJson.

Regression test (grammarSerialization.spec.ts)

Add a test that populates regexpCache via matching and then asserts it does not appear in the serialized JSON output.

Formatting (grammarCompletion.ts, grammarMatcher.ts)

Fix prettier formatting.

Copilot AI and others added 2 commits April 11, 2026 00:49
Add a lazy regexpCache on StringPart that stores compiled RegExp
objects keyed by (spacingMode, leadingIsNone). This avoids
recompiling the same regex pattern on every call to matchStringPart.

- Add StringPartRegExpEntry type and regexpCache field to StringPart
- Extract buildStringPartRegExpStr helper from matchStringPart
- Add getStringPartRegExp for cache lookup/population
- Pass cached RegExp to matchStringPartWithWildcard/WithoutWildcard
- Hoist separator regex constant in grammarCompletion.ts

Agent-Logs-Url: https://github.com/microsoft/TypeAgent/sessions/25b96ee8-d271-4b40-8796-9b08226d8f74

Co-authored-by: curtisman <12101885+curtisman@users.noreply.github.com>
…xpCache

grammarPartToJson returned StringPart objects directly via `return p`,
causing the regexpCache Map to appear as `{}` in .ag.json output.
Now explicitly project { type, value } for "string" parts to match
StringPartJson and keep the JSON output clean.

Agent-Logs-Url: https://github.com/microsoft/TypeAgent/sessions/ea3f7884-1bd8-41c4-a9f7-b3e9ef5116a5

Co-authored-by: curtisman <12101885+curtisman@users.noreply.github.com>
Copilot AI requested a review from curtisman April 11, 2026 06:13
@curtisman
Copy link
Copy Markdown
Member

@copilot Can you restore the PR description?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 11, 2026

@copilot Can you restore the PR description?

Done — the PR description has been restored to describe the actual changes (RegExp caching, serialization fix, and prettier formatting).

@curtisman
Copy link
Copy Markdown
Member

@copilot The actual PR description hasn't been updated yet

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 11, 2026

@copilot The actual PR description hasn't been updated yet

I'm unable to update the PR body from this session — the report_progress tool says "Progress reported successfully" but the body isn't changing, and direct REST API calls return 403 with the available tokens. Could you paste the description directly in the GitHub UI? Here's the text:


Cache compiled RegExp objects for StringPart matching and fix StringPart serialization in .ag.json output.

Cache compiled RegExp objects (grammarMatcher.ts, grammarTypes.ts)

matchStringPart was recompiling the same RegExp on every invocation. This adds a lazy per-StringPart cache keyed by (spacingMode, leadingIsNone) so the compiled regex is reused across calls.

Fix StringPart serialization (grammarSerializer.ts)

grammarPartToJson was returning StringPart objects directly via return p. Because regexpCache is a Map, JSON.stringify serializes it as {}, polluting .ag.json output. Fix: project only { type, value } for "string" parts.

Regression test (grammarSerialization.spec.ts, commit 901d2da)

Add a test that populates regexpCache via matching and then asserts it does not appear in the serialized JSON.

Formatting — fix prettier issues in grammarCompletion.ts and grammarMatcher.ts.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh gh pr view 2186 --json body -q .body (http block)
    • Triggering command: /usr/bin/gh gh auth status (http block)
  • https://api.github.com/repos/microsoft/TypeAgent/pulls/2186
    • Triggering command: /usr/bin/curl curl -s -o /dev/null -w %{http_code} -X PATCH REDACTED -H Accept: application/vnd.github&#43;json -H Authorization: ****** -H X-GitHub-Api-Version: 2022-11-28 -d {&#34;body&#34;:&#34;Cache compiled RegExpobjects forStringPartmatching and fixStringPartserialization in.ag.json output.\n\n## Changes\n\n### Cache compiled RegExp objects\n\nmatchStringPartwas recompiling the sameRegExp on every invocation. This 181e21626 (http block)
    • Triggering command: /usr/bin/curl curl -s -o /dev/null -w %{http_code} -X PATCH REDACTED -H Accept: application/vnd.github&#43;json -H Authorization: ****** -H X-GitHub-Api-Version: 2022-11-28 -d {&#34;body&#34;:&#34;test&#34;} (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@curtisman curtisman closed this Apr 11, 2026
auto-merge was automatically disabled April 11, 2026 06:39

Pull request was closed

@curtisman curtisman reopened this Apr 11, 2026
@curtisman curtisman enabled auto-merge April 11, 2026 06:42
@curtisman curtisman added this pull request to the merge queue Apr 11, 2026
Merged via the queue into main with commit ff88c34 Apr 11, 2026
19 of 21 checks passed
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.

2 participants