diff --git a/hooks/router.ts b/hooks/router.ts index b67f4c3..86b6855 100644 --- a/hooks/router.ts +++ b/hooks/router.ts @@ -114,16 +114,45 @@ const HEAVY = [ export type Route = { tier: Tier; reason: string } +// Standing text that tools add to every brief (pipeline-guard's constraints block): +// it says how to work, not what the task is, so it never picks a model (#9). +const BOILERPLATE_HEADING = /^(standing constraints?|constraints|house rules|rules|pipeline[- ]guard\b.*)\b/i +const HEADING = /^(#{1,6})[ \t]+(.*?)[ \t#]*$/ + +/** + * The brief without boilerplate sections: a heading named Standing constraints + * (or Constraints, Rules, House rules, Pipeline guard ...) and everything under + * it, up to the next heading of the same or a higher level. + */ +export const taskText = (prompt: string): string => { + const kept: string[] = [] + let skipLevel = 0 + for (const line of prompt.split(/\r?\n/)) { + const h = line.match(HEADING) + if (h) { + const level = h[1]!.length + if (skipLevel && level <= skipLevel) skipLevel = 0 + if (!skipLevel && BOILERPLATE_HEADING.test(h[2]!.replace(/[*_`]/g, '').trim())) { + skipLevel = level + continue + } + } + if (!skipLevel) kept.push(line) + } + return kept.join('\n') +} + /** A clear signal from the text, or undefined (ask the classifier). */ export const heuristic = (subagentType: string | undefined, prompt: string, role?: Role): Route | undefined => { if (!role && subagentType && READ_ONLY_TYPES.test(subagentType)) return { tier: 'haiku', reason: `read-only agent type ${subagentType}` } - const lower = prompt.toLowerCase() + const task = taskText(prompt) + const lower = task.toLowerCase() const heavy = HEAVY.find(r => r.test(lower)) if (heavy) return { tier: 'opus', reason: 'approach still open' } if (role) return undefined // dev briefs never take the light path const light = LIGHT.find(r => r.test(lower)) - if (light && prompt.length < 1500) return { tier: 'haiku', reason: 'light task' } + if (light && task.length < 1500) return { tier: 'haiku', reason: 'light task' } return undefined } diff --git a/tests/router.test.ts b/tests/router.test.ts index 8b43a7b..968ba80 100644 --- a/tests/router.test.ts +++ b/tests/router.test.ts @@ -4,6 +4,7 @@ import { expect, mock, test, type Engine } from 'claude-code/testing' import { decide, heuristic, + taskText, onFail, parseBrief, parseVerdict, @@ -1264,3 +1265,43 @@ test('#7: a test designer is no saving and gets an A number; the log shows its r expect(log).toContain('test-design') expect(log).not.toContain('T1') }) + +// ---------- #9: boilerplate never picks the model ---------- + +const GUARD_BLOCK = + '\n\n## Standing constraints\n' + + '- A rerun only for a named, reproduced flaky test, never a blanket retry.\n' + + '- Do not redesign shared fixtures; propose an approach in the PR if one is needed.\n' +const SONNET_BRIEF = `Pipeline-Role: dev\nClone: ${DEV} (Role: dev)\nBase branch: release/1.0.\nEV-19: clamp the export setpoint to the grid limit as specified. Expected: test_export_clamp passes.` + +test('#9: the guard block appended to a Sonnet-shaped dev brief goes to the classifier, not Opus', async () => { + expect(heuristic(undefined, SONNET_BRIEF + GUARD_BLOCK, 'dev')).toBe(undefined) + expect(decide({ role: 'dev', prompt: SONNET_BRIEF + GUARD_BLOCK, respectExplicit: false }).kind).toBe('classify') + // the same words in the task itself still mean the approach is open + expect(heuristic(undefined, SONNET_BRIEF + '\nFind out why test_export_clamp is flaky.' + GUARD_BLOCK, 'dev')?.tier).toBe('opus') +}) + +test('#9: a skipped section ends at the next heading of the same or a higher level', async () => { + const t = taskText('# EV-19\nclamp it\n## Standing constraints\n- flaky\n### sub\n- design\n## Task\nredesign the loop\n') + expect(t).not.toContain('flaky') + expect(t).not.toContain('- design') + expect(t).toContain('redesign the loop') + expect(t).toContain('clamp it') + // headings in other styles and levels + expect(taskText('x\n### **Constraints**\n- intermittent\n')).not.toContain('intermittent') + expect(taskText('x\n## Pipeline guard (v3)\n- trade-offs\n')).not.toContain('trade-offs') + expect(taskText('x\n## Constraints on the design\n- keep it\n')).not.toContain('keep it') + // the heading line goes too: its words are no task either + expect(taskText('x\n## Constraints on the design\n- keep it\n')).not.toContain('design') + // a heading that only starts with the word is no boilerplate + expect(taskText('## Rulesets to design\nnew ruleset')).toContain('new ruleset') +}) + +test('#9 end to end: a guarded dev brief is classified, and the light path stays closed', async ($, on) => { + const w = fresh() + world(on, w) + await $.agent.spawn({ prompt: SONNET_BRIEF + GUARD_BLOCK, description: 'ev19' } as never) + expect(w.spawned[0]?.model).toBe('sonnet') + // boilerplate is not counted toward the light path's length either + expect(heuristic(undefined, 'Find where the MQTT discovery payload is built' + GUARD_BLOCK + 'x'.repeat(2000))?.tier).toBe('haiku') +})