Skip to content

fix: escape * and ? in glob-to-regex and add runtime type check for candidatesTokenCount#1851

Open
fix2015 wants to merge 1 commit into
MoonshotAI:mainfrom
fix2015:fix/glob-regex-escape-and-type-safety
Open

fix: escape * and ? in glob-to-regex and add runtime type check for candidatesTokenCount#1851
fix2015 wants to merge 1 commit into
MoonshotAI:mainfrom
fix2015:fix/glob-regex-escape-and-type-safety

Conversation

@fix2015

@fix2015 fix2015 commented Jul 17, 2026

Copy link
Copy Markdown

Problem

Two independent bugs:

1. globPatternToRegex missing * and ? in regex escape character class

In the backslash-escape branch of globPatternToRegex, the regex metacharacter set [{}()+.\\[\]^$|] does not include * and ?. When a glob pattern contains \* or \? (backslash-escaped literals), the resulting regex leaves * or ? unescaped, turning them into quantifiers instead of literal matches.

Affected files:

  • packages/kaos/src/internal.ts (line 223)
  • packages/agent-core-v2/src/_base/execEnv/globPattern.ts (line 46)

2. Unsafe type assertion for candidatesTokenCount

(usageMetadata['candidatesTokenCount'] as number) ?? 0 — the as number cast silences TypeScript but does not protect at runtime. If the field is undefined, the cast produces undefined (not number), and ?? correctly falls back to 0 only by accident of JS semantics. If the field is a non-number truthy value, it passes through unchecked. The adjacent promptTokenCount and cachedContentTokenCount fields already use a proper typeof guard; this field should match.

Affected files:

  • packages/kosong/src/providers/google-genai.ts (line 635)
  • packages/agent-core-v2/src/app/llmProtocol/providers/google-genai.ts (line 546)

Fix

  1. Added *? to the regex character class: [{}()+.\\[\]^$|*?]
  2. Replaced (usageMetadata['candidatesTokenCount'] as number) ?? 0 with typeof usageMetadata['candidatesTokenCount'] === 'number' ? usageMetadata['candidatesTokenCount'] : 0

@changeset-bot

changeset-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a389868

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

1 participant