Prompt-routing hook ignores words inside backticks (#4564) (#4566)

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:
authored and GitHub committed 2026-11-20 17:04:51 +00:00
1 parent 6121a19591
commit 6287c75e2c
4 files changed
+108 -6

No files matched your search

+66 -6
View File
@@ -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(' ')} `)));