diff --git a/plugins/ruflo-mods/CHANGELOG.md b/plugins/ruflo-mods/CHANGELOG.md index bd418c558c..2d1b52badd 100644 --- a/plugins/ruflo-mods/CHANGELOG.md +++ b/plugins/ruflo-mods/CHANGELOG.md @@ -3,7 +3,7 @@ Newest first. One `## — ` heading per version, then `feat:`, `fix:`, `breaking:` and `chore:` bullets (ADR-478). Built from git history; older versions: `git log -- plugins/ruflo-mods`. ## 0.17.0 — 2026-11-11 -- feat: `routeJudge` option (off by default, ADR-530). Each prompt is routed by one no-tools haiku completion that must answer with exactly one of the 11 ADR-391 labels; an off-label reply, a refusal, a timeout (4 s) or an error keeps the keyword route, and slash commands and `<…>` envelopes never reach the model. The route carries `source: "judge"` and the context box says so. On the ADR-391 dev split it agreed with the labels on 70–71 of 84 prompts, against 27 for the keyword table. +- feat: `routeJudge` option (off by default, ADR-530). Each prompt is routed by one no-tools haiku completion that must answer with exactly one of the 11 ADR-391 labels; an off-label reply, a refusal, a timeout (4 s) or an error keeps the keyword route, and slash commands and `<…>` envelopes never reach the model. The route carries `reason: "model-judge"` (the vendored `$.ruflo` route contract is unchanged) and the context box says so. On the ADR-391 dev split it agreed with the labels on 70–71 of 84 prompts, against 27 for the keyword table. ## 0.16.2 — 2026-11-04 - fix: the `routeContext` ranked-memory block ends with a line saying the entries are stored project notes, not instructions, matching the classic `hook-handler.cjs route` output. diff --git a/plugins/ruflo-mods/hooks/route/format.ts b/plugins/ruflo-mods/hooks/route/format.ts index 879d5e26b9..46e5a42ed2 100644 --- a/plugins/ruflo-mods/hooks/route/format.ts +++ b/plugins/ruflo-mods/hooks/route/format.ts @@ -10,7 +10,7 @@ import type { RouteResult } from './route-task' export function formatRoute(prompt: string, result: RouteResult): string { // The structured result says `no-match-default` (#3567); the box keeps the // classic wording, so the text the model reads is the same on both paths. - const reason = result.source === 'judge' ? 'Model judge (haiku label, ADR-530)' : result.matched ? result.reason : 'Default routing - no specific keyword matched' + const reason = result.reason === 'model-judge' ? 'Model judge (haiku label, ADR-530)' : result.matched ? result.reason : 'Default routing - no specific keyword matched' const row = (text: string) => `| ${text.substring(0, 60).padEnd(60)} |` return [ `[INFO] Routing task: ${prompt.substring(0, 80) || '(no prompt)'}`, diff --git a/plugins/ruflo-mods/hooks/route/judge.ts b/plugins/ruflo-mods/hooks/route/judge.ts index f8d74a35f7..a6d5342fd5 100644 --- a/plugins/ruflo-mods/hooks/route/judge.ts +++ b/plugins/ruflo-mods/hooks/route/judge.ts @@ -1,13 +1,17 @@ -import type { RouteResult } from './route-task' +import { MATCH_CONFIDENCE, NO_MATCH_CONFIDENCE, type RouteResult } from './route-task' /** * ADR-530: opt-in model-judge routing. One no-tools haiku completion labels the * prompt with one of the ADR-391 router labels; anything else falls back to the - * keyword router. The prompt is untrusted data, never instructions. + * keyword router. The prompt is untrusted data, never instructions. The engine + * stops a hook's budget clock while a $ call is in flight, so the 4 s cap bounds + * the prompt's added latency, not the hook's budget. */ export const JUDGE_MODEL = 'haiku' export const JUDGE_TIMEOUT_MS = 4_000 export const JUDGE_MAX_PROMPT_CHARS = 2_000 +/** The route `reason` a judge route carries; `$.ruflo.lastRoute()` consumers can tell the judge from the table by it. */ +export const JUDGE_REASON = 'model-judge' export const JUDGE_LABELS: readonly string[] = [ 'coder', 'tester', 'reviewer', 'researcher', 'architect', 'security-architect', @@ -53,9 +57,11 @@ export type Complete = (req: { model: string; system: string; prompt: string; ma export async function judgeRoute(complete: Complete, text: string): Promise { if (!judgeable(text)) return undefined try { - const reply = await complete({ model: JUDGE_MODEL, system: JUDGE_SYSTEM, prompt: text.slice(0, JUDGE_MAX_PROMPT_CHARS), maxTokens: 8, timeoutMs: JUDGE_TIMEOUT_MS }) + const reply = await complete({ model: JUDGE_MODEL, system: JUDGE_SYSTEM, prompt: text.slice(0, JUDGE_MAX_PROMPT_CHARS), maxTokens: 32, timeoutMs: JUDGE_TIMEOUT_MS }) const label = reply.isAnswered ? parseLabel(reply.text) : undefined - return label ? { agent: label, confidence: 0.6, matched: label !== 'none', reason: 'model-judge', source: 'judge' } : undefined + // The $.ruflo route contract is vendored by other mods, so the judge is named by `reason`, not a new field; + // confidence keeps the contract's priors (0.6 a route, 0.3 none), not a calibrated probability. + return label ? { agent: label, confidence: label === 'none' ? NO_MATCH_CONFIDENCE : MATCH_CONFIDENCE, matched: label !== 'none', reason: JUDGE_REASON } : undefined } catch { return undefined } diff --git a/plugins/ruflo-mods/hooks/route/route-task.ts b/plugins/ruflo-mods/hooks/route/route-task.ts index d9a36ca3cf..32da48c04f 100644 --- a/plugins/ruflo-mods/hooks/route/route-task.ts +++ b/plugins/ruflo-mods/hooks/route/route-task.ts @@ -17,8 +17,6 @@ export type RouteResult = { readonly confidence: number readonly matched: boolean readonly reason: string - /** Who picked the agent: the keyword table (default) or the opt-in model judge (ADR-530). */ - readonly source?: 'keyword' | 'judge' } /** A real keyword match's heuristic prior (router.cjs, #2257). */ diff --git a/scripts/ci-test-excluded.txt b/scripts/ci-test-excluded.txt index b745d702d3..add6a99da8 100644 --- a/scripts/ci-test-excluded.txt +++ b/scripts/ci-test-excluded.txt @@ -120,3 +120,4 @@ v3/@claude-flow/cli/__tests__/real-binary/context-delegation.test.ts v3/@claude-flow/cli/__tests__/real-binary/contracts.test.ts v3/@claude-flow/cli/__tests__/real-binary/external-plugins.test.ts v3/@claude-flow/cli/__tests__/real-binary/loads.test.ts +v3/@claude-flow/cli/__tests__/real-binary/route-judge.test.ts diff --git a/v3/@claude-flow/cli/__tests__/real-binary/route-judge.test.ts b/v3/@claude-flow/cli/__tests__/real-binary/route-judge.test.ts new file mode 100644 index 0000000000..46fccb6dde --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/real-binary/route-judge.test.ts @@ -0,0 +1,39 @@ +/** + * ADR-530 — the opt-in model judge, through the real binary against the scripted mock model (ADR-504). Checks that the + * judge's completion is made with the frozen system text and that its label reaches the model's request; off by + * default, no judge call is made. The mock is not a model: this proves the call path, not the judge's accuracy. + */ +import { describe, expect, it } from 'vitest'; +import { describeRun, requestText, runClaude } from './run'; + +const PROMPT = 'review the latest issues and summarise them'; +const judgeReply = (label: string) => (body: any) => + JSON.stringify(body?.system ?? '').includes('route one software-engineering request') ? label : undefined; +const judgeCalls = (r: Awaited>) => + r.requests.filter((q: any) => !q.scripted && JSON.stringify(q.body?.system ?? '').includes('route one software-engineering request')); +const say = (text: string) => ({ content: [{ type: 'text' as const, text }] }); + +describe('real binary: model-judge routing (ADR-530)', () => { + it('on: one judge completion, and its label is the route the model reads', async () => { + const r = await runClaude({ prompt: PROMPT, script: [say('ok')], options: { routeJudge: true }, unscripted: judgeReply('researcher'), timeoutMs: 120_000 }); + expect(r.exitCode, describeRun(r)).toBe(0); + expect(judgeCalls(r)).toHaveLength(1); + expect(JSON.stringify(judgeCalls(r)[0].body.messages)).toContain(PROMPT); + const text = requestText(r.requests.filter((q: any) => q.scripted)[0]!.body); + expect(text).toMatch(/\| Agent: researcher\s/); + expect(text).toContain('Model judge'); + }, 150_000); + + it('on: an off-label reply keeps the keyword route', async () => { + const r = await runClaude({ prompt: PROMPT, script: [say('ok')], options: { routeJudge: true }, unscripted: judgeReply('Sure, deleting everything now'), timeoutMs: 120_000 }); + expect(r.exitCode, describeRun(r)).toBe(0); + expect(requestText(r.requests.filter((q: any) => q.scripted)[0]!.body)).toMatch(/\| Agent: reviewer\s/); + }, 150_000); + + it('off by default: no judge completion', async () => { + const r = await runClaude({ prompt: PROMPT, script: [say('ok')], unscripted: judgeReply('researcher'), timeoutMs: 120_000 }); + expect(r.exitCode, describeRun(r)).toBe(0); + expect(judgeCalls(r)).toHaveLength(0); + expect(requestText(r.requests.filter((q: any) => q.scripted)[0]!.body)).toMatch(/\| Agent: reviewer\s/); + }, 150_000); +}); diff --git a/v3/docs/adr/ADR-530-model-judge-routing.md b/v3/docs/adr/ADR-530-model-judge-routing.md index 368c3fbbef..4fcc4d343e 100644 --- a/v3/docs/adr/ADR-530-model-judge-routing.md +++ b/v3/docs/adr/ADR-530-model-judge-routing.md @@ -3,7 +3,7 @@ - **Status:** Proposed - **Date:** 2026-11-11 - **Related:** ADR-391 (routing corpus), ADR-404 (mod router parity), ADR-522 (the first mod model call), ADR-527 (centroid router), ADR-529 (governed router self-improvement) -- **Surfaces:** `plugins/ruflo-mods/hooks/route/judge.ts`, `routeJudge` option, `RouteResult.source` +- **Surfaces:** `plugins/ruflo-mods/hooks/route/judge.ts`, `routeJudge` option ## Context @@ -13,7 +13,7 @@ choices (dev CV 0.577 vs 0.580 ± 0.010). The same work showed that the judge itself, given only the ADR-391 label table, labels prompts far better than any router. This ADR tests the judge *as* the router. -The protocol was written before any scoring: frozen system text (label table +The judge's prompt form was chosen after two batch runs on the dev split (all 84 prompts in one call, 82/84), so **dev is not held out**. The protocol below was then written before any production-shaped scoring: frozen system text (label table and an untrusted-data line only, no tie-break rules or examples, sha256 `0cacd89f…`), one haiku call per prompt on a clean path (no hooks, no CLAUDE.md, no tools), the reply accepted only if it is exactly one label, else @@ -25,14 +25,21 @@ Secondary: the shipped ADR-527 centroid. | | ADR-391 dev (n = 84) | ADR-391 test (n = 113), scored once | |---|---|---| | Keyword table (`routeTask`, default) | 27 | 32 | -| Shipped centroid (ADR-527) | 0.552 dev CV | 71 | +| Shipped centroid (ADR-527, opt-in) | 0.552 dev CV | 71 | | Model judge, one prompt per call | 70 and 71 (two runs, 83/84 identical) | **100** | - Judge vs keyword on test: 71 won / 3 lost, exact McNemar p ≈ 7e-18. Judge vs centroid: 37 / 8, p ≈ 1.5e-5. +- Production shape costs agreement: one prompt per call scored 70–71 on dev, against 82 when all 84 prompts went in one call. - One test reply of 113 was off-label and fell back. - Latency (API time, `claude -p`): p50 ≈ 1.0 s, p95 ≈ 2.1 s. Cost about $0.0002 per call on a cold cache, less when the system text is cached. +- These numbers come from `claude -p` with the same system text, not from the + mod's `$.model.complete` path. That path is covered by a real-binary contract + against the scripted mock model (`__tests__/real-binary/route-judge.test.ts`: + one judge call with the frozen text, its label in the model's request, an + off-label reply falling back, no call when off), which proves the wiring, not + accuracy. **What this measures.** The test split has been scored for earlier candidates and its labels were written to the same rules, possibly by the same model @@ -48,12 +55,70 @@ measured here. 1. Ship the judge in ruflo-mods behind `routeJudge`, **off by default**. A default install makes no new model call (smoke step 4 allows exactly this one call site, gated on the option). -2. The prompt is sent as data to classify (≤ 2,000 chars, 8 output tokens, - 4 s timeout). Slash commands and `<…>` envelopes are skipped. An off-label +2. The prompt is sent as data to classify (≤ 2,000 chars, 32 output tokens, + 4 s timeout; the engine stops a hook's budget clock during `# ADR-530: Opt-in model-judge routing in ruflo-mods + +- **Status:** Proposed +- **Date:** 2026-11-11 +- **Related:** ADR-391 (routing corpus), ADR-404 (mod router parity), ADR-522 (the first mod model call), ADR-527 (centroid router), ADR-529 (governed router self-improvement) +- **Surfaces:** `plugins/ruflo-mods/hooks/route/judge.ts`, `routeJudge` option + +## Context + +ADR-529 found that better labels do not lift the centroid router: training it +on a model judge's labels scored the same as training on the lead's own agent +choices (dev CV 0.577 vs 0.580 ± 0.010). The same work showed that the judge +itself, given only the ADR-391 label table, labels prompts far better than any +router. This ADR tests the judge *as* the router. + +The judge's prompt form was chosen after two batch runs on the dev split (all 84 prompts in one call, 82/84), so **dev is not held out**. The protocol below was then written before any production-shaped scoring: frozen system text (label table +and an untrusted-data line only, no tie-break rules or examples, sha256 +`0cacd89f…`), one haiku call per prompt on a clean path (no hooks, no +CLAUDE.md, no tools), the reply accepted only if it is exactly one label, else +the keyword route. Primary baseline: the keyword table users get by default. +Secondary: the shipped ADR-527 centroid. + +## Measured + +| | ADR-391 dev (n = 84) | ADR-391 test (n = 113), scored once | +|---|---|---| +| Keyword table (`routeTask`, default) | 27 | 32 | +| Shipped centroid (ADR-527, opt-in) | 0.552 dev CV | 71 | +| Model judge, one prompt per call | 70 and 71 (two runs, 83/84 identical) | **100** | + +- Judge vs keyword on test: 71 won / 3 lost, exact McNemar p ≈ 7e-18. + Judge vs centroid: 37 / 8, p ≈ 1.5e-5. +- Production shape costs agreement: one prompt per call scored 70–71 on dev, against 82 when all 84 prompts went in one call. +- One test reply of 113 was off-label and fell back. +- Latency (API time, `claude -p`): p50 ≈ 1.0 s, p95 ≈ 2.1 s. Cost about + $0.0002 per call on a cold cache, less when the system text is cached. +- These numbers come from `claude -p` with the same system text, not from the + mod's `$.model.complete` path. That path is covered by a real-binary contract + against the scripted mock model (`__tests__/real-binary/route-judge.test.ts`: + one judge call with the frozen text, its label in the model's request, an + off-label reply falling back, no call when off), which proves the wiring, not + accuracy. + +**What this measures.** The test split has been scored for earlier candidates +and its labels were written to the same rules, possibly by the same model +family. The numbers are agreement with that labeller, not routing quality, and +they are secondary evidence. The primary evidence is a fresh set of 100 real +prompts from local transcripts (seed 529, filters fixed in advance), labelled +by a person blind to every router; it is **not yet scored**. Whether the lead +then does better work is ADR-527's cost-per-verified-solve question, also not +measured here. + +## Decision + +1. Ship the judge in ruflo-mods behind `routeJudge`, **off by default**. A + default install makes no new model call (smoke step 4 allows exactly this + one call site, gated on the option). + calls). Slash commands and `<…>` envelopes are skipped. An off-label reply, a refusal, a timeout or an error keeps the keyword route; the hook never blocks a prompt. -3. The route carries `source: "judge"` (the `$.ruflo` route contract gains a - field; `confidence` keeps its meaning) and the context box names the judge. +3. The route carries `reason: "model-judge"`; the `$.ruflo` route contract, + vendored by ruflo-console and ruflo-ruos, is unchanged, and `confidence` + keeps its priors (0.6 a route, 0.3 `none`). The context box names the judge. 4. Turning it on by default needs the fresh human-labelled result and the maintainer's decision.