Skip to content

Commit b0ec85a

Browse files
sahrizviclaude
andcommitted
fix: bound the rule classifier's slow patterns instead of capping its input
- the clause scan after "remember" and the rule-cue lookahead are bounded, so long texts are classified whole and stay fast; the length cap changed what callers read from a long message - the `tool schema` hint also covers a missing workspace skill name or SQL field Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01XM3J9mB75wZRnzTSGcW2kc
1 parent dce9408 commit b0ec85a

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

‎packages/opencode/src/altimate/rules/classify.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ export type Kind = "rule" | "memory" | "ambiguous"
1717
const ASKED_RULE = /\bas\s+a\s+(?:team\s+)?rule\b/i
1818
// Only the named forms: a bare "for me" opens ordinary requests ("fix this model for me"), and "in memory" describes
1919
// data ("never load the fact table in memory"). "Just for me" always names a personal scope; "remember … for me" only
20-
// within one clause, so a later "for me" does not turn a team instruction into a memory.
21-
const ASKED_MEMORY = /\bas\s+a\s+memory\b|\bin\s+my\s+memory\b|\bjust\s+for\s+me\b|\bremember\b[^.!?\n,;:]*\bfor\s+me\b/i
20+
// within one clause, so a later "for me" does not turn a team instruction into a memory. The clause scan is bounded:
21+
// unbounded, every "remember" rescans to the clause end, and a long text takes seconds.
22+
const ASKED_MEMORY = /\bas\s+a\s+memory\b|\bin\s+my\s+memory\b|\bjust\s+for\s+me\b|\bremember\b[^.!?\n,;:]{0,300}\bfor\s+me\b/i
2223

2324
// "I want …" / "I like …" open many ordinary requests ("I want staging models in models/staging"): on its own
2425
// that is not enough to file a team convention as a personal memory, so it only makes the text ambiguous.
@@ -27,7 +28,7 @@ const WEAK_MEMORY = /\bi\s+(?:like|want)\b/i
2728
const MEMORY_CUES: readonly [RegExp, string][] = [
2829
[/\bi\s+(?:prefer|usually|tend to)\b/i, "a personal preference"],
2930
[/\bmy\s+(?:preference|style|default|setup|machine|laptop)\b/i, "a personal preference"],
30-
[/\bremember\s+(?:that|our|the)\b(?!.*\b(?:always|never|must)\b)/i, "a fact to remember"],
31+
[/\bremember\s+(?:that|our|the)\b(?!.{0,2000}\b(?:always|never|must)\b)/i, "a fact to remember"],
3132
[/\b(?:our|the)\s+(?:warehouse|database|account|cluster|stack|region|team|company|org)\s+(?:is|uses|runs|lives)\b/i, "a fact about the environment"],
3233
[/\b[A-Z]{2,}\s+(?:means|stands for|is short for)\b|\bmeans\b.*\b(?:revenue|users?|customers?|metric)\b/, "a glossary term"],
3334
]
@@ -41,11 +42,7 @@ const RULE_CUES: readonly [RegExp, string][] = [
4142
[/\b(?:name|prefix|suffix|alias)\s+\S+(?:\s+\S+)?\s+(?:as|with|like)\b/i, "a naming convention"],
4243
]
4344

44-
// A request to remember is short, and some of the patterns backtrack: a longer text is left to the person to choose.
45-
const MAX_CLASSIFIED_CHARS = 2000
46-
4745
export function classify(text: string): { kind: Kind; cue?: string } {
48-
if (text.length > MAX_CLASSIFIED_CHARS) return { kind: "ambiguous" }
4946
if (ASKED_RULE.test(text)) return { kind: "rule", cue: "asked for a rule" }
5047
if (ASKED_MEMORY.test(text)) return { kind: "memory", cue: "asked to remember it for themselves" }
5148
const memory = MEMORY_CUES.find(([pattern]) => pattern.test(text))

‎packages/opencode/src/cli/cmd/tool.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ const ToolSchemaCommand = cmd({
186186
})
187187

188188
// The messages an argument problem produces: invalid JSON, a non-object, a schema mismatch (built-in or workspace tool).
189-
const ARGUMENT_ERROR = /^(?:Tool arguments |The \S+ tool was called with invalid arguments|Arguments do not match the workspace operation schema)/
189+
const ARGUMENT_ERROR = /^(?:Tool arguments |The \S+ tool was called with invalid arguments|Arguments do not match the workspace operation schema|A workspace skill name is required|A SQL query in \S+ is required)/
190190

191191
const ToolRunCommand = cmd({
192192
command: "run <name>",

‎packages/opencode/test/skill/release-v0.12.9-adversarial.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,12 @@ describe("v0.12.9 adversarial: rule classify", () => {
482482
expect(classifyRule(text), text).toEqual({ kind: "ambiguous" })
483483
})
484484

485-
test("a text past the length cap is ambiguous at once, even with an explicit choice at its end", () => {
485+
test("long texts are read whole and stay fast", () => {
486486
const start = performance.now()
487-
expect(classifyRule("remember ".repeat(50_000) + "always")).toEqual({ kind: "ambiguous" })
488-
expect(classifyRule("x ".repeat(1_500) + "save this as a team rule")).toEqual({ kind: "ambiguous" })
489-
expect(performance.now() - start).toBeLessThan(15_000)
487+
expect(classifyRule("remember ".repeat(50_000) + "always").kind).toBe("rule")
488+
expect(classifyRule("remember that ".repeat(30_000) + "x").kind).toBe("memory")
489+
expect(classifyRule("x ".repeat(1_500) + "save this as a team rule")).toEqual({ kind: "rule", cue: "asked for a rule" })
490+
expect(performance.now() - start).toBeLessThan(5_000)
490491
})
491492

492493
test("cues across lines are still found", () => {

0 commit comments

Comments
 (0)