Skip to content

Commit dc75a3e

Browse files
authored
fix(cursor): parse model variants without delimiter backtracking (#7938)
Co-authored-by: JUN <[email protected]>
1 parent da721e3 commit dc75a3e

5 files changed

Lines changed: 141 additions & 24 deletions

File tree

‎src/adapters/cursor/catalog.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,11 @@ export function parseCursorVariantId(rawId: string): ParsedCursorVariantId {
473473
}
474474
let thinking = false;
475475
let level: string | undefined;
476-
const thinkingLevel = /^(.*)-thinking-([a-z-]+)$/.exec(stem);
477-
if (thinkingLevel && CURSOR_CAPABILITIES[thinkingLevel[1]!] && (LEVEL_TOKENS as readonly string[]).includes(thinkingLevel[2]!)) {
478-
return finishParse(thinkingLevel[1]!, true, fast, thinkingLevel[2]!);
476+
const thinkingAt = stem.lastIndexOf("-thinking-");
477+
const thinkingBase = thinkingAt < 0 ? "" : stem.slice(0, thinkingAt);
478+
const thinkingLevel = thinkingAt < 0 ? "" : stem.slice(thinkingAt + "-thinking-".length);
479+
if (/^[a-z-]+$/.test(thinkingLevel) && CURSOR_CAPABILITIES[thinkingBase] && (LEVEL_TOKENS as readonly string[]).includes(thinkingLevel)) {
480+
return finishParse(thinkingBase, true, fast, thinkingLevel);
479481
}
480482
const levelThinking = stem.endsWith("-thinking") ? stripLevelSuffix(stem.slice(0, -"-thinking".length)) : undefined;
481483
if (levelThinking && CURSOR_CAPABILITIES[levelThinking.stem]) {

‎src/adapters/cursor/claude-id.ts‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,37 @@ function parseClaudeBase(raw: string): { canonicalBaseId: string; sourceBaseId:
4242

4343
export function normalizeCursorClaudeId(raw: string): NormalizedCursorClaudeId | undefined {
4444
const id = raw.trim().toLowerCase();
45-
const patterns: ReadonlyArray<readonly [RegExp, (m: RegExpExecArray) => { base: string; thinking: boolean; fast: boolean; level?: string }]> = [
46-
[/^(.*)-thinking-([a-z-]+)-fast$/, m => ({ base: m[1]!, thinking: true, fast: true, level: m[2]! })],
47-
[/^(.*)-([a-z-]+)-thinking-fast$/, m => ({ base: m[1]!, thinking: true, fast: true, level: m[2]! })],
48-
[/^(.*)-thinking-([a-z-]+)$/, m => ({ base: m[1]!, thinking: true, fast: false, level: m[2]! })],
49-
[/^(.*)-([a-z-]+)-thinking$/, m => ({ base: m[1]!, thinking: true, fast: false, level: m[2]! })],
50-
[/^(.*)-([a-z-]+)-fast$/, m => ({ base: m[1]!, thinking: false, fast: true, level: m[2]! })],
51-
[/^(.*)-thinking-fast$/, m => ({ base: m[1]!, thinking: true, fast: true })],
52-
[/^(.*)-thinking$/, m => ({ base: m[1]!, thinking: true, fast: false })],
53-
[/^(.*)-fast$/, m => ({ base: m[1]!, thinking: false, fast: true })],
54-
];
55-
for (const [pattern, dims] of patterns) {
56-
const match = pattern.exec(id);
57-
if (!match) continue;
58-
const parsed = dims(match);
59-
if (parsed.level && !CLAUDE_LEVELS.has(parsed.level)) continue;
60-
const base = parseClaudeBase(parsed.base);
61-
if (base) return { ...base, ...parsed, sourceBaseId: base.sourceBaseId };
45+
// Recognize the numeric base first; suffix parsing never searches backwards
46+
// through arbitrary repetitions of model-name delimiters.
47+
const match = /^(claude-(?:fable|haiku|opus|sonnet)-\d+(?:[.-]\d+)*|claude-\d+(?:\.\d+)*-(?:fable|haiku|opus|sonnet))(?=-|$)/.exec(id);
48+
if (!match) return undefined;
49+
const base = parseClaudeBase(match[1]!);
50+
if (!base) return undefined;
51+
let suffix = id.slice(match[1]!.length);
52+
if (!suffix) return { ...base, thinking: false, fast: false };
53+
const fast = suffix.endsWith("-fast");
54+
if (fast) suffix = suffix.slice(0, -"-fast".length);
55+
let thinking = false;
56+
let level: string | undefined;
57+
if (suffix === "-thinking") {
58+
thinking = true;
59+
} else if (suffix.startsWith("-thinking-")) {
60+
thinking = true;
61+
level = suffix.slice("-thinking-".length);
62+
} else if (suffix.endsWith("-thinking")) {
63+
thinking = true;
64+
level = suffix.slice(1, -"-thinking".length);
65+
// The legacy greedy prefix leaves only the final hyphen-delimited token.
66+
if (level.includes("-")) return undefined;
67+
} else if (fast && suffix) {
68+
level = suffix.slice(1);
69+
if (level.includes("-")) return undefined;
70+
} else if (suffix) {
71+
return undefined;
6272
}
63-
const base = parseClaudeBase(id);
64-
return base ? { ...base, thinking: false, fast: false } : undefined;
73+
if (level !== undefined && !CLAUDE_LEVELS.has(level)) return undefined;
74+
const parsed = { base: base.sourceBaseId, thinking, fast, ...(level !== undefined ? { level } : {}) };
75+
return { ...base, ...parsed };
6576
}
6677

6778
export function composeCursorClaudeWireId(

‎structure/providers/cursor.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ The checkpoint instruction digest includes the format, so a changed schema force
124124

125125
## Cursor parameterized models
126126

127+
`src/adapters/cursor/catalog.ts` splits thinking/effort variants at the last `-thinking-`
128+
delimiter and validates its suffix once. `src/adapters/cursor/claude-id.ts` recognizes the
129+
numeric Claude base before parsing thinking, fast, and effort markers with linear string
130+
operations. Both retain legacy spelling, marker precedence, and unknown-ID behavior;
131+
`tests/providers/cursor/cursor-catalog.test.ts` and
132+
`tests/providers/cursor/cursor-claude-id.test.ts` compare legacy regex results and exercise
133+
long repeated-delimiter inputs without wall-clock assertions.
134+
127135
Cursor Router's parameterized `default` model is represented in Codex by four catalog rows:
128136
`cursor/auto` preserves Cursor's team/account default, while `cursor/auto-cost`,
129137
`cursor/auto-balance`, and `cursor/auto-intelligence` make each optimization level explicit.

‎tests/providers/cursor/cursor-catalog.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test } from "bun:test";
1+
import { describe, expect, spyOn, test } from "bun:test";
22
import {
33
CURSOR_CAPABILITIES,
44
cursorLiveRosterScope,
@@ -139,6 +139,41 @@ describe("cursor umbrella catalog (devlog 260828_cursor_umbrella_catalog)", () =
139139
});
140140

141141
describe("parser precedence", () => {
142+
test("thinking-level parsing agrees with the legacy greedy delimiter regex", () => {
143+
for (const id of [
144+
"grok-4.6-thinking-high", "grok-4.6-thinking-extra-high",
145+
"grok-4.6-thinking-thinking-high", "grok-4.6-thinking-high-thinking-low",
146+
"-thinking-high", "-thinking-", "grok-4.6-thinking-",
147+
"grok-4.6-thinking--", "grok-4.6-thinking-HIGH", "grok-4.6-thinking-highX",
148+
"grok-4.6\n-thinking-high", "grok-4.6-thinking-high\nX", "",
149+
]) {
150+
const match = /^(.*)-thinking-([a-z-]+)$/.exec(id);
151+
const known = match?.[1] === "grok-4.6" && ["high", "extra-high"].includes(match[2]!);
152+
expect(parseCursorVariantId(` ${id} `), id).toEqual(known
153+
? { baseId: match![1], kind: "thinking", level: match![2], ultra: false, known: true }
154+
: { baseId: id, kind: "regular", ultra: false, known: false });
155+
}
156+
});
157+
158+
test("adversarial model ids bypass unbounded wildcard regexes", () => {
159+
const exec = RegExp.prototype.exec;
160+
const guard = spyOn(RegExp.prototype, "exec").mockImplementation(function (this: RegExp, value: string) {
161+
// Fail deterministically before a reintroduced backtracking matcher can stall the test runner.
162+
if (value.length > 1_000 && this.source.includes("(.*)")) {
163+
throw new Error("unbounded wildcard regex on adversarial model id");
164+
}
165+
return exec.call(this, value);
166+
});
167+
try {
168+
for (const prefix of ["", "grok-4.6", "claude-opus-5"]) {
169+
const id = prefix + "-thinking-".repeat(20_000) + "X";
170+
expect(parseCursorVariantId(id)).toEqual({ baseId: id, kind: "regular", ultra: false, known: false });
171+
}
172+
} finally {
173+
guard.mockRestore();
174+
}
175+
});
176+
142177
test("exact base identities never mis-parse as effort suffixes", () => {
143178
expect(parseCursorVariantId("gpt-5.1-codex-max")).toMatchObject({ baseId: "gpt-5.1-codex-max", kind: "regular", known: true });
144179
expect(parseCursorVariantId("gpt-5.5-extra")).toMatchObject({ baseId: "gpt-5.5-extra", kind: "regular", known: true });

‎tests/providers/cursor/cursor-claude-id.test.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
1-
import { describe, expect, test } from "bun:test";
1+
import { describe, expect, spyOn, test } from "bun:test";
22
import {
33
composeCursorClaudeWireId,
44
normalizeCursorClaudeId,
55
} from "../../../src/adapters/cursor/claude-id";
66

77
describe("Cursor Claude id normalization", () => {
8+
test("matches the legacy regex table for both base spellings and suffix edge cases", () => {
9+
const patterns = [
10+
[/^(.*)-thinking-([a-z-]+)-fast$/, true, true],
11+
[/^(.*)-([a-z-]+)-thinking-fast$/, true, true],
12+
[/^(.*)-thinking-([a-z-]+)$/, true, false],
13+
[/^(.*)-([a-z-]+)-thinking$/, true, false],
14+
[/^(.*)-([a-z-]+)-fast$/, false, true],
15+
[/^(.*)-thinking-fast$/, true, true],
16+
[/^(.*)-thinking$/, true, false],
17+
[/^(.*)-fast$/, false, true],
18+
] as const;
19+
const levels = ["none", "minimal", "low", "medium", "high", "xhigh", "max", "extra-high"];
20+
for (const [base, canonicalBaseId, spelling] of [
21+
["claude-opus-5", "claude-opus-5", "anthropic"],
22+
["claude-fable-5.1", "claude-fable-5-1", "anthropic"],
23+
["claude-4.6-opus", "claude-4.6-opus", "version-first"],
24+
] as const) {
25+
for (const suffix of [
26+
"", "-thinking", "-fast", "-thinking-fast", "-high", "-thinking-", "-thinking--",
27+
"-thinking-thinking-high", "-high-thinking-low", "\n-thinking-high", "-thinking-high\nX",
28+
...levels.flatMap(level => [
29+
`-thinking-${level}`, `-${level}-thinking`, `-thinking-${level}-fast`,
30+
`-${level}-thinking-fast`, `-${level}-fast`,
31+
]),
32+
]) {
33+
const id = base + suffix;
34+
let expected;
35+
for (const [pattern, thinking, fast] of patterns) {
36+
const match = pattern.exec(id);
37+
if (!match || match[1] !== base || (match[2] && !levels.includes(match[2]))) continue;
38+
expected = { base: match[1], canonicalBaseId, sourceBaseId: base, spelling, thinking, fast,
39+
...(match[2] ? { level: match[2] } : {}) };
40+
break;
41+
}
42+
if (suffix === "") expected = { canonicalBaseId, sourceBaseId: base, spelling, thinking: false, fast: false };
43+
expect(normalizeCursorClaudeId(` ${id.toUpperCase()} `), id).toEqual(expected);
44+
}
45+
}
46+
for (const id of ["", "-thinking-high", "-thinking-", "claude--thinking-high", "claude-composer-5-high-fast"]) {
47+
expect(normalizeCursorClaudeId(id), id).toBeUndefined();
48+
}
49+
});
50+
51+
test("adversarial Claude ids bypass unbounded wildcard regexes", () => {
52+
const exec = RegExp.prototype.exec;
53+
const guard = spyOn(RegExp.prototype, "exec").mockImplementation(function (this: RegExp, value: string) {
54+
if (value.length > 1_000 && this.source.includes("(.*)")) {
55+
throw new Error("unbounded wildcard regex on adversarial Claude id");
56+
}
57+
return exec.call(this, value);
58+
});
59+
try {
60+
for (const ending of ["X", "high", "high-fast", "high-thinking", "high-thinking-fast"]) {
61+
const id = "claude-opus-5" + "-thinking-".repeat(20_000) + ending;
62+
expect(normalizeCursorClaudeId(id)).toBeUndefined();
63+
}
64+
} finally {
65+
guard.mockRestore();
66+
}
67+
});
68+
869
test("normalizes every observed Fable 5.1 spelling to one capability base", () => {
970
for (const id of ["claude-fable-5-1", "claude-fable-5.1", "claude-5.1-fable"]) {
1071
expect(normalizeCursorClaudeId(id), id).toMatchObject({

0 commit comments

Comments
 (0)