mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-23 08:31:44 +01:00
The prompt-routing hook no longer acts on words inside backticks or code blocks, so a prompt that quotes a command (`Run `mycli check` now`) stops getting a wrong "run /verify" or "run /review" line. Why: routing is on by default, so any session that quotes commands got these hints; this fixes the report in #4564 by @vilano93, rebuilt on current main. Nothing to do: plain requests ("run the checks", "review this pr", bug reports in words) route as before. Co-authored-by: vilano93 <244097767+vilano93@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=red-team Review-Finding: p1/specialist-batched#2 reader=testing
This commit is contained in:
4 files changed
+108
-6
No files matched your search
@@ -0,0 +1 @@
|
||||
**The prompt-routing hook no longer acts on words inside backticks.** A prompt that quotes a command, such as "Run `mycli check` now" or the line an orchestration tool types to wake a session, was told to run /verify on the trigger "run the checks", and "Run `gh pr review 123`" was told to run /review. A `--fix` flag or the word broken in a pasted log also made a prompt read as a bug report. Triggers and bug-report signs now read only the prose, leaving out inline code and fenced code blocks, on Claude Code and Codex. "run the checks", "review this pr" and bug reports in words route as before, also with code beside them, and an exception name in backticks such as `KeyError` still marks a bug report. Thanks to @vilano93, who found and first fixed it (#4564).
|
||||
@@ -49,6 +49,7 @@ The hook scores the prompt against the `triggers:` list in each installed `SKILL
|
||||
- A bug report goes to the installed skill that claims bug reports, named alone, whatever the other triggers score (GST-3237). A skill claims bug reports when one of its triggers names the defect and fixing it, and nothing else ("fix this bug", "reproduce the bug in a test"); a trigger that names its own target ("fix lint errors", "fix postgres bugs") claims only that kind of fix. gstack's own routed skills only check work, so they never claim one, and /verify is never named ahead of the bug-fix skill. A prompt that holds the whole phrase of one of /review's or /qa's own triggers ("review this pr for bugs", "find bugs on site") and never asks for a fix keeps that skill, since finding bugs is not fixing one. With none installed, a bug report routes on triggers as before. In B1d's in-sample run (gstack-evals #385), Opus 5.5 loaded prove-it in 29 of 33 sessions when the line named it alone and in none of the 20 where it was not named, and took /verify in 4 of the 7 where /verify came first. On B1's 60 in-sample prompts the line now names prove-it alone on all 46 bug reports; before, it named prove-it on 40 of the 60 and put /verify first on 7 of those (`test/fixtures/skill-route-b1-in-sample.json` labels each prompt).
|
||||
- A prompt reads as a bug report on one strong sign: a word for what went wrong (bug, crash, hang, stuck, leak, wrong, incorrect, unexpected, regression and the like), an exception name such as `ValueError`, the verb fix, an issue template's expected or actual heading ("expected behavior", "steps to reproduce", "correct output"), a failure verb ("fails", "failed to", "doesn't work") or a "should X, not Y" correction. Feature requests use error, exception and failure as nouns ("add error handling", "a custom exception class"), so those words, a negated verb, "should" and "instead of" are weak signs, and it takes two different ones. None of the 60 plain prompts in `test/fixtures/skill-route-plain-prompts.txt` reads as a bug report; 5 of B1's 14 in-sample feature requests do.
|
||||
- Words a bug report uses for the defect (error, exception, traceback, crash, fails, failure, broken, regression, incorrect, wrong, defect, and exception names such as `ValueError`) score as "bug", so "parse_one raises ValueError, please fix" matches a "fix this bug" trigger. Paths and identifiers stay whole (`site-packages`, `src/review.ts`, `max_retries`), so a traceback's paths never match a trigger's words.
|
||||
- Triggers and the bug-report signs read only the prompt's prose. Markdown code, meaning inline code in backticks and fenced code blocks as CommonMark reads them, is left out, so a command quoted in backticks never routes a prompt (#4564): "Run `mycli check` now" no longer matches "run the checks", "Run `gh pr review 123`" no longer matches "review this pr", and a `--fix` flag or the word broken in a pasted log is not a bug report. The prose around the code still routes ("run the checks, then `git push`"). One sign still reads the code: an exception name as code writes it (`KeyError`, `NullPointerException`), since a quoted error or a pasted traceback reports a bug.
|
||||
- When a trigger ties with another of the same skill on score and words, a whole-phrase match wins.
|
||||
- A prompt longer than 5,000 characters is scored on its first 4,000 and last 1,000 characters, where the request usually sits; a log pasted between them is not scored, so its words cannot match everything. Until GST-3110 such prompts (over 8,000 characters) were never routed.
|
||||
- Prompts that start with `/` (you already picked a skill) are never routed.
|
||||
|
||||
+66
-6
@@ -268,6 +268,56 @@ export function contentTokens(text: string): string[] {
|
||||
return [...new Set(words(text).filter(w => !STOPWORDS.has(w)).map(w => concept(stem(w))))];
|
||||
}
|
||||
|
||||
/** A fence line (CommonMark): up to three spaces, then three or more backticks or tildes, then its info string. */
|
||||
const FENCE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
|
||||
/** A blank line: an inline code span never crosses one. */
|
||||
const PARAGRAPH_BREAK = /(\n[ \t]*\n)/;
|
||||
|
||||
/**
|
||||
* A prompt split into its prose and its Markdown code, as CommonMark reads them: fenced code blocks (an unclosed fence
|
||||
* runs to the end) and inline code spans (a backtick run closed by the next run of the same length in its paragraph; an
|
||||
* unmatched run is prose). A command quoted in backticks names the command; it does not ask for anything, so triggers
|
||||
* match the prose only (#4564). Each run looks up its closer once, so a long prompt full of unmatched runs stays linear.
|
||||
*/
|
||||
export function splitCode(prompt: string): { prose: string; code: string } {
|
||||
const prose: string[] = [];
|
||||
const code: string[] = [];
|
||||
let fence = '';
|
||||
for (const line of prompt.split('\n')) {
|
||||
const m = FENCE.exec(line);
|
||||
if (fence) {
|
||||
if (m && m[1][0] === fence[0] && m[1].length >= fence.length && !m[2].trim()) fence = '';
|
||||
else code.push(line);
|
||||
prose.push('');
|
||||
} else if (m && !(m[1][0] === '`' && m[2].includes('`'))) {
|
||||
fence = m[1];
|
||||
prose.push('');
|
||||
} else prose.push(line);
|
||||
}
|
||||
const text = prose.join('\n').split(PARAGRAPH_BREAK).map(part => {
|
||||
const runs = [...part.matchAll(/`+/g)];
|
||||
const closer: (number | undefined)[] = [];
|
||||
const nextOfLength = new Map<number, number>();
|
||||
for (let i = runs.length - 1; i >= 0; i--) {
|
||||
closer[i] = nextOfLength.get(runs[i][0].length);
|
||||
nextOfLength.set(runs[i][0].length, i);
|
||||
}
|
||||
let out = '';
|
||||
let at = 0;
|
||||
for (let i = 0; i < runs.length; i++) {
|
||||
const close = closer[i];
|
||||
if (close === undefined) continue;
|
||||
const open = runs[i];
|
||||
code.push(part.slice(open.index + open[0].length, runs[close].index));
|
||||
out += `${part.slice(at, open.index)} `;
|
||||
at = runs[close].index + runs[close][0].length;
|
||||
i = close;
|
||||
}
|
||||
return out + part.slice(at);
|
||||
}).join('');
|
||||
return { prose: text, code: code.join('\n') };
|
||||
}
|
||||
|
||||
/** The text a prompt is scored on: all of it, or its head and tail windows when it is longer than both. */
|
||||
export function promptWindow(prompt: string): string {
|
||||
if (prompt.length <= PROMPT_HEAD_CHARS + PROMPT_TAIL_CHARS) return prompt;
|
||||
@@ -292,7 +342,7 @@ function within(seq: string[], want: string[], span: number): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Every skill whose best trigger shares a content word with the prompt's window (promptWindow), best first
|
||||
* Every skill whose best trigger shares a content word with the window (promptWindow) of the prompt's prose (splitCode), best first
|
||||
* (score, matched words, name). A trigger's score is 1 when its whole phrase appears in the prompt, else the
|
||||
* share of its content words (stopwords dropped, lightly stemmed, defect words read as "bug") that the prompt
|
||||
* contains. A trigger with one content word scores only on its whole phrase unless that word is specific
|
||||
@@ -302,7 +352,7 @@ function within(seq: string[], want: string[], span: number): boolean {
|
||||
* EVERYDAY_SPAN words of each other.
|
||||
*/
|
||||
export function scorePrompt(prompt: string, skills: InstalledSkill[]): RouteCandidate[] {
|
||||
const scored = promptWindow(prompt);
|
||||
const scored = promptWindow(splitCode(prompt).prose);
|
||||
const seq = tokens(scored);
|
||||
const promptSeq = ` ${seq.join(' ')} `;
|
||||
const promptSet = new Set(contentTokens(scored));
|
||||
@@ -360,9 +410,18 @@ const WEAK_SIGNS = [/\b(?:errors?|exceptions?|failures?|failed)\b/, /\b(?:(?:doe
|
||||
/** The verb fix, as a prompt asks for one. */
|
||||
const FIX_WORD = /^(?:re-)?fix(?:es|ing)?$/;
|
||||
|
||||
/** True when the prompt's window (promptWindow) reads as a bug report: one strong sign, or two different weak ones. */
|
||||
/** An exception name as code writes it (ValueError, KeyError, NullPointerException); a helper such as handleError is not one. */
|
||||
const EXCEPTION_NAME = /\b[A-Z][A-Za-z0-9]*(?:Error|Exception)\b/;
|
||||
|
||||
/**
|
||||
* True when the window (promptWindow) of the prompt's prose (splitCode) reads as a bug report: one strong sign, or two
|
||||
* different weak ones. Its code counts only for an exception name, since a pasted traceback or a quoted `KeyError` reports
|
||||
* one; a flag such as `--fix` or the word broken in a log does not.
|
||||
*/
|
||||
export function bugReport(prompt: string): boolean {
|
||||
const text = promptWindow(prompt).toLowerCase().replace(/[\u2019`]/g, "'").replace(/\s+/g, ' ');
|
||||
const { prose, code } = splitCode(prompt);
|
||||
if (EXCEPTION_NAME.test(promptWindow(code))) return true;
|
||||
const text = promptWindow(prose).toLowerCase().replace(/[\u2019`]/g, "'").replace(/\s+/g, ' ');
|
||||
if (words(text).some(w => SYMPTOM_STEMS.has(stem(w)) || FIX_WORD.test(w) || /^[a-z]{3,}(?:error|exception)$/.test(w))) return true;
|
||||
return STRONG_SIGN.test(text) || WEAK_SIGNS.filter(re => re.test(text)).length >= 2;
|
||||
}
|
||||
@@ -399,7 +458,8 @@ export type RouteDecision =
|
||||
* A prompt that already names a command is never routed: `/` on both hosts, and Codex's `$skill` mention there. A bug
|
||||
* report goes to the installed skill that claims bug reports, alone, whatever the triggers score (bugFixRoute), unless
|
||||
* it holds the whole phrase of one of /review's or /qa's own triggers ("review this pr for bugs") and never asks for a
|
||||
* fix: finding bugs is not fixing one. Any other prompt goes to the skills whose triggers reach the threshold.
|
||||
* fix: finding bugs is not fixing one. Any other prompt goes to the skills whose triggers reach the threshold. All of
|
||||
* this reads the prompt's prose, never its Markdown code (splitCode), except that an exception name in code reports a bug.
|
||||
*/
|
||||
export function routePrompt(prompt: string, skills: InstalledSkill[], threshold = ROUTE_THRESHOLD, host: HookHost = 'claude'): RouteDecision {
|
||||
const trimmed = prompt.trim();
|
||||
@@ -408,7 +468,7 @@ export function routePrompt(prompt: string, skills: InstalledSkill[], threshold
|
||||
if (host === 'codex' && trimmed.startsWith('$')) return { route: null, reason: 'skill-mention', candidates: [] };
|
||||
const routed = skills.filter(isRouted);
|
||||
const candidates = scorePrompt(prompt, routed);
|
||||
const window = promptWindow(prompt);
|
||||
const window = promptWindow(splitCode(prompt).prose);
|
||||
const seq = ` ${tokens(window).join(' ')} `;
|
||||
const findsBugs = !words(window).some(w => FIX_WORD.test(w)) && routed.some(s => isGstackSkill(s)
|
||||
&& ['review', 'qa'].includes(baseName(s.name)) && s.triggers.some(t => seq.includes(` ${tokens(t).join(' ')} `)));
|
||||
|
||||
@@ -284,6 +284,46 @@ describe('installed skills beside gstack (GST-3123)', () => {
|
||||
expect(traceLog()).toMatch(/event=routed skill=prove-it score=1 source=\S+prove-it\/SKILL\.md variant=name bug_report=true\n$/);
|
||||
});
|
||||
|
||||
test('words inside inline code or a fenced code block never match a trigger or make a bug report; the prose around them still routes (#4564)', () => {
|
||||
const proveIt = other('prove-it', ['fix this bug', 'prove the fix', 'reproduce the bug in a test']);
|
||||
const names = (text: string, host?: 'claude' | 'codex') => { const d = routePrompt(text, [...routed, proveIt], undefined, host); return d.route ? d.routes.map(r => r.skill) : []; };
|
||||
for (const text of [
|
||||
'You have 1 orchestration message. Run `orca orchestration check --run run_<run-id> --terminal term_<terminal-id>`.',
|
||||
'Run `gh pr review 123 --approve` once CI is done.',
|
||||
'Run ``tool lint check --fix`` and paste the output.',
|
||||
'Run `mycli check` now',
|
||||
'Pasted from CI:\n```\n$ make check\nall tests pass\n```\nWhat does this output mean?',
|
||||
'What does this print?\n~~~sh\nrun the checks || echo broken\n~~~',
|
||||
'What does this print?\n```\nrun the checks\n````\nmore\n````',
|
||||
'rename `handleError` to `onError`',
|
||||
]) {
|
||||
expect([text, names(text)]).toEqual([text, []]);
|
||||
expect([text, names(text, 'codex')]).toEqual([text, []]);
|
||||
}
|
||||
for (const [text, skill] of [
|
||||
['run the checks', 'verify'], ['verify my change in `src/app.ts`', 'verify'], ['run the checks, then `git push`', 'verify'],
|
||||
['an unclosed ` backtick: run the checks', 'verify'], ['```\nmake check\n```\nrun the checks on that', 'verify'],
|
||||
['review this pr before I merge `feature/login`', 'review'], ['please review my changes to `lib/auth.ts`', 'review'],
|
||||
['Saving a draft raises `KeyError: title` when the title is empty', 'prove-it'],
|
||||
['The upload fails on large files; I ran `make check` and it passed', 'prove-it'],
|
||||
['the export crashes on empty input, see `export.ts`', 'prove-it'],
|
||||
['What happened?\n```\nTraceback (most recent call last):\nValueError: bad input\n```', 'prove-it'],
|
||||
]) {
|
||||
expect([text, names(text)]).toEqual([text, [skill]]);
|
||||
expect([text, names(text, 'codex')]).toEqual([text, [skill]]);
|
||||
}
|
||||
});
|
||||
|
||||
test('a prompt at the hook\'s stdin cap full of unmatched backtick runs is split in one linear pass (#4564)', () => {
|
||||
let text = '';
|
||||
for (let i = 1; i <= 60; i++) text += `${'`'.repeat(i)} x `;
|
||||
text += 'y '.repeat(120000);
|
||||
const t0 = performance.now();
|
||||
expect(routePrompt(text, routed).route).toBeNull();
|
||||
// gstack-timing-ok: the bound is the hook's own 1.5 s DEADLINE_MS; a rescan per unmatched run took about 5 s on this prompt, one pass takes milliseconds
|
||||
expect(performance.now() - t0).toBeLessThan(1500);
|
||||
});
|
||||
|
||||
test('a trigger that names its own target claims no bug report; a full /review or /qa trigger keeps a request to find bugs', () => {
|
||||
const proveIt = other('prove-it', ['fix this bug']);
|
||||
const lint = other('auto-fix-lint', ['fix lint errors', 'fix broken links', 'fix postgres bugs']);
|
||||
|
||||
Reference in new issue
Block a user