From d9f0f427c6c84a6ec7a3aaa3071c6d430f2e0404 Mon Sep 17 00:00:00 2001 From: bchanot Date: Tue, 24 Nov 2026 13:35:46 +0100 Subject: [PATCH] feat(model-router): advisor note on main-loop plan/reflect skill loads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mod prepends a one-call-per-run advisor rule to the body of main-loop skill loads whose row is plan or reflect; effort-shift § Advisor gains point 4 (Per run). --- lib/effort-shift.md | 12 +++ mods/model-router/hooks/register.test.ts | 107 +++++++++++++++++++++++ mods/model-router/hooks/register.ts | 42 ++++++++- 3 files changed, 159 insertions(+), 2 deletions(-) diff --git a/lib/effort-shift.md b/lib/effort-shift.md index 3fdff95..37203eb 100644 --- a/lib/effort-shift.md +++ b/lib/effort-shift.md @@ -134,6 +134,18 @@ triggers in CLAUDE.global.md § Workflow): reuses the last run's answer, or its skip, and names any code commit made after it (doc and memory commits excepted). Elsewhere the answer is reported only and a fix it calls for is a new run. +4. **Per run.** A main-loop run of a skill whose model-router row is + `plan` or `reflect` (the rows that put main on Fable before BDR-150) + makes one call on its settled plan, diagnosis or fix, before it + executes or reports (at load when it starts from a settled plan). A + call this run made at point 1, 2 or 3 counts as this one. Only the + placed-decision skip (`lib/challenge-plan.md` § SKIP) and point 3's + skips waive it; any other skipped challenge step (hotfix's cosmetic + guard, an audit-only or conservative mode, a clean stop) gets the + call where that step would have run. A skill loaded inside a + plan/reflect skill's run makes none; under another row or none, it + calls. The model-router mod prepends this rule to the skill body on a + main-loop load, never on a sub-agent's. Inside a skill run, the points above also cover the triggers of CLAUDE.global.md § Workflow; no second call for the same recommendation. diff --git a/mods/model-router/hooks/register.test.ts b/mods/model-router/hooks/register.test.ts index b06055d..5d5c7e2 100644 --- a/mods/model-router/hooks/register.test.ts +++ b/mods/model-router/hooks/register.test.ts @@ -7744,3 +7744,110 @@ test('GUARD: an isError Skill answer leaves the latch closed', async ( await $.tool.call({ tool: 'Skill', skill: 'diagram' } as never) expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD) }) + +// ---- advisor note: one main-loop advisor call per plan/reflect run ------ + +// duplicated on purpose: register.ts exports no constant +const NOTE = 'Advisor (`~/.claude/lib/effort-shift.md` § Advisor, **Per run**)' + +const noteWorld = () => newWorld({ + edit: f => { + Object.assign(f.skills as Rec, { + 'writing-plans': 'plan', + 'pack-design:frontend-design': 'reflect', + capitalize: 'apply', + }) + }, +}) + +/** + * A rig whose bottom Skill tool loads the skill body through skill.prompt, + * as the engine does, and answers the loaded text as `commandName`. + */ +async function noteRig($: Engine, on: On, gate?: Promise) { + on('tool.call', { tool: 'Skill' }, async (_api, e) => { + if (e.agentId === undefined) await gate + const out = await $.skill.prompt({ skill: String(e.skill), text: 'BODY' }) + return { result: { success: true, commandName: out.text } } + }) + await bootRig($, on, FABLE, 1000, noteWorld()) +} + +/** The body a Skill call loaded, as the bottom tool answered it. */ +async function bodyOf( + $: Engine, + skill: string, + agentId?: string, +): Promise { + const out = await loadSkill($, skill, agentId) + return (out as unknown as { result: { commandName: string } }) + .result.commandName +} + +test('advisor note: a main feat load carries it', async ($, on) => { + await noteRig($, on) + const body = await bodyOf($, 'feat') + expect(body.startsWith(NOTE)).toBe(true) + expect(body.endsWith('\n\nBODY')).toBe(true) +}) + +test('advisor note: a main plan row carries it', async ($, on) => { + await noteRig($, on) + expect(await bodyOf($, 'writing-plans')).toContain(NOTE) +}) + +test('advisor note: a prefixed reflect row carries it', async ($, on) => { + await noteRig($, on) + expect(await bodyOf($, 'pack-design:frontend-design')).toContain(NOTE) +}) + +test('advisor note: a sub-agent load carries none', async ($, on) => { + await noteRig($, on) + expect(await bodyOf($, 'feat', 'a1')).toBe('BODY') +}) + +test('advisor note: a preload carries none', async ($, on) => { + await noteRig($, on) + await $.agent.spawn(spawnOf('feater')) + expect((await skillPrompt($, 'feat')).text).toBe('x') +}) + +test('advisor note: an apply row carries none', async ($, on) => { + await noteRig($, on) + expect(await bodyOf($, 'capitalize')).toBe('BODY') +}) + +test('advisor note: a mechanical row carries none', async ($, on) => { + await noteRig($, on) + expect(await bodyOf($, 'status')).toBe('BODY') +}) + +test('advisor note: an unrowed skill carries none', async ($, on) => { + await noteRig($, on) + expect(await bodyOf($, 'diagram')).toBe('BODY') +}) + +test('advisor note: /route off carries none', async ($, on) => { + await noteRig($, on) + await route($, 'off') + expect(await bodyOf($, 'feat')).toBe('BODY') +}) + +test('advisor note: a typed /feat carries it', async ($, on) => { + await noteRig($, on) + await typed($, '/feat add a thing') + const out = await skillPrompt($, 'feat') + expect(out.text.startsWith(NOTE)).toBe(true) + expect(out.text.endsWith('\n\nx')).toBe(true) +}) + +test('advisor note: a sub-agent load ending mid-call keeps the main one', async ( + $, on) => { + let release: () => void = () => undefined + const gate = new Promise(resolve => { release = resolve }) + await noteRig($, on, gate) + const main = bodyOf($, 'feat') + expect(await bodyOf($, 'status', 'a1')).toBe('BODY') + release() + expect(await main).toContain(NOTE) +}) diff --git a/mods/model-router/hooks/register.ts b/mods/model-router/hooks/register.ts index 2be5329..247ca83 100644 --- a/mods/model-router/hooks/register.ts +++ b/mods/model-router/hooks/register.ts @@ -95,6 +95,7 @@ type State = { loops: Map // agentId -> that loop's routing explicitEffort: Map // Agent tool_use_id -> effort param skillCalls: number // Skill tool calls in flight + advisorSkill: string | null // main-loop Skill load awaiting its note off: boolean // /route off or config: every hook passes through offConfig: boolean // `off` comes from the config key `enabled` spinner: string // "model/effort" of the last main step of this turn @@ -192,6 +193,18 @@ const INHERITING_AGENTS: ReadonlySet = new Set(['general-purpose']) const REFERENCE_SKILLS: ReadonlySet = new Set([ 'modern-web-guidance', 'find-docs', ]) +// Rows whose skill run makes one main-loop advisor call (effort-shift.md +// § Advisor, point 4); ADVISOR_NOTE is the rule, prepended to the body. +const ADVISOR_PHASES: ReadonlySet = new Set(['plan', 'reflect']) +const ADVISOR_NOTE = + 'Advisor (`~/.claude/lib/effort-shift.md` § Advisor, **Per run**): when ' + + 'the `advisor` tool is listed, this skill run makes one main-loop ' + + 'advisor call on its settled plan, diagnosis or fix, before it executes ' + + 'or reports. A call this run already made at an advisor point counts. ' + + 'Only `~/.claude/lib/challenge-plan.md` § SKIP and the end-of-run skips ' + + 'waive it; any other skipped challenge step still gets the call where ' + + 'it would have run. Loaded inside another plan/reflect skill\'s run: no ' + + 'call of its own.\n\n' // PostModelSwitch sources a person chose (auto and resume are not). const USER_SWITCH: ReadonlySet = new Set([ 'command', 'picker', 'sdk', @@ -1026,6 +1039,7 @@ function newState(cfg: Config, source: string): State { loops: new Map(), explicitEffort: new Map(), skillCalls: 0, + advisorSkill: null, off: false, offConfig: false, spinner: '', @@ -3754,6 +3768,12 @@ function skillRow(st: State, skill: string): Picked | undefined { return phase !== undefined && route ? { phase, route } : undefined } +/** Whether a skill's row phase owes its run one advisor call. */ +function advisorDue(st: State, skill: string): boolean { + const phase = skillRow(st, skill)?.phase + return phase !== undefined && ADVISOR_PHASES.has(phase) +} + /** The run slot's model outranks the route's: a helper moves effort only. */ function belowRun(st: State, route: Route): boolean { const mine = resolveRoute(st, route) @@ -4634,6 +4654,13 @@ function registerSkills(on: On, st: State): void { if (e.agentId === undefined && skillRow(st, skill)) { await tierAsk($, st, st.turnMain, `Skill /${clean(skill, NAME_CAP)}`) } + // shortcut: one marker, so accepted gaps: a same-name sub-agent load + // during a main call takes the note (the main load loses it); two + // concurrent main calls keep the last marker. skill.prompt has no + // agentId. Upgrade path: key the marker by tool_use_id. + if (e.agentId === undefined && advisorDue(st, skill)) { + st.advisorSkill = skill + } const out = await next(e) const refused = typeof out !== 'object' || out === null || 'deny' in out || (out as { isError?: unknown }).isError === true @@ -4642,6 +4669,7 @@ function registerSkills(on: On, st: State): void { return out } finally { st.skillCalls -= 1 + if (e.agentId === undefined) st.advisorSkill = null } }).catch(($, e, next) => { warnOnce(st, $, 'Skill', next.error.kind) @@ -4649,14 +4677,24 @@ function registerSkills(on: On, st: State): void { }) on('skill.prompt', async ($, e, next) => { await ensureConfig($, st) - if (st.off || st.skillCalls > 0) return next(e) + if (st.off) return next(e) + if (st.skillCalls > 0) { + const out = await next(e) + if (st.advisorSkill !== e.skill) return out + st.advisorSkill = null + return { ...out, text: ADVISOR_NOTE + out.text } + } const { row, typed } = armed($, st, e.skill) if (typed) { const size = { points: pointsOf([e.text]), unknown: false } await addEstimate($, st, size, false) } if (row) await confirmSkill($, st, e.skill, row) - return next(e) + const out = await next(e) + // shortcut: a typed slash landing while a Skill call is in flight + // takes the in-call branch above and loses its note. + if (!typed || !advisorDue(st, e.skill)) return out + return { ...out, text: ADVISOR_NOTE + out.text } }).catch(($, e, next) => { warnOnce(st, $, 'skill.prompt', next.error.kind) return next(e)