feat(model-router): redirect main-loop code writes until a skill ran in the session
This commit is contained in:
1 parent
60ee4a996e
commit
22ff57b57b
2 files changed
+243
-1
No files matched your search
@@ -7275,3 +7275,165 @@ test('TIER: no routing.json means no ask; the route is set', async ($, on) => {
|
|||||||
expect(w.asked).toEqual([])
|
expect(w.asked).toEqual([])
|
||||||
expect(mainLine(await route($, 'show'))).toContain('model plan')
|
expect(mainLine(await route($, 'show'))).toContain('model plan')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// ---- GUARD: main-loop code writes go through a skill --------------------
|
||||||
|
|
||||||
|
const GUARD = 'model-router: code is written inside a skill run.'
|
||||||
|
|
||||||
|
function writeBottoms(on: On): void {
|
||||||
|
on('tool.call', { tool: 'Write' }, () => ({ result: 'w' as never }))
|
||||||
|
on('tool.call', { tool: 'Edit' }, () => ({ result: 'e' as never }))
|
||||||
|
on('tool.call', { tool: 'NotebookEdit' }, () => ({ result: 'n' as never }))
|
||||||
|
}
|
||||||
|
|
||||||
|
const write = ($: Engine, file: string, extra: Rec = {}) =>
|
||||||
|
$.tool.call({ tool: 'Write', file_path: file, content: 'x',
|
||||||
|
...extra } as never)
|
||||||
|
|
||||||
|
const denied = (out: unknown): string =>
|
||||||
|
String((out as { deny?: string }).deny ?? '')
|
||||||
|
|
||||||
|
async function guardRig($: Engine, on: On, world: World = newWorld()) {
|
||||||
|
writeBottoms(on)
|
||||||
|
return bootRun($, on, OPUS, world)
|
||||||
|
}
|
||||||
|
|
||||||
|
test('GUARD: a main code write before any skill is redirected', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const free of ['README.md', 'notes.txt', '.claude/tasks/x.json',
|
||||||
|
'docs/a.ts']) {
|
||||||
|
test(`GUARD: ${free} is free`, async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
expect(denied(await write($, `${ROOT}/${free}`))).toBe('')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
test('GUARD: config is code, a home .claude path is code', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
expect(denied(await write($, `${ROOT}/package.json`))).toContain(GUARD)
|
||||||
|
expect(denied(await write($, '/home/t/.claude/lib/x.sh'))).toContain(GUARD)
|
||||||
|
expect(denied(await write($, `${ROOT}/.claude/worktrees/w/a.ts`)))
|
||||||
|
.toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a dot-dot path never counts as free', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
expect(denied(await write($, `${ROOT}/.claude/../src/a.ts`)))
|
||||||
|
.toContain(GUARD)
|
||||||
|
expect(denied(await write($, `${ROOT}/docs/../src/a.ts`))).toContain(GUARD)
|
||||||
|
expect(denied(await write($, `${ROOT}/docs/..`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a sub-agent writes freely', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`, { agentId: 'a1' })))
|
||||||
|
.toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a typed skill opens it for the session', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await typeFeat($)
|
||||||
|
await endTurn($)
|
||||||
|
await endTurn($)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a typed apply skill after a run keeps it open', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await typeFeat($)
|
||||||
|
await endTurn($)
|
||||||
|
await typed($, '/status')
|
||||||
|
await skillPrompt($, 'status')
|
||||||
|
await endTurn($)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a model-loaded skill, rowed or not, opens it', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await $.tool.call({ tool: 'Skill', skill: 'diagram' } as never)
|
||||||
|
await endTurn($)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a Skill call inside a sub-agent does not open it', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await $.tool.call({ tool: 'Skill', skill: 'diagram',
|
||||||
|
agentId: 'a1' } as never)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a route-tool phase opens it for the turn only', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await $.tool.call({ tool: 'mcp__model-router__route', phase: 'implement' })
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
await endTurn($)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a typed /route opens it, /route clear closes it', async (
|
||||||
|
$, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await route($, 'plan')
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
await route($, 'clear')
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: /route clear drops the skill latch too', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await typeFeat($)
|
||||||
|
await endTurn($)
|
||||||
|
await route($, 'clear')
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: /route off turns it off', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await route($, 'off')
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: /route off then on re-arms it', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
await typeFeat($)
|
||||||
|
await endTurn($)
|
||||||
|
await route($, 'off')
|
||||||
|
await route($, 'on')
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: NotebookEdit and Edit are guarded too', async ($, on) => {
|
||||||
|
await guardRig($, on)
|
||||||
|
const nb = await $.tool.call({ tool: 'NotebookEdit',
|
||||||
|
notebook_path: `${ROOT}/n.ipynb`, new_source: 'x' } as never)
|
||||||
|
const ed = await $.tool.call({ tool: 'Edit', file_path: `${ROOT}/a.py`,
|
||||||
|
old_string: 'a', new_string: 'b' } as never)
|
||||||
|
expect(denied(nb)).toContain(GUARD)
|
||||||
|
expect(denied(ed)).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: a /model hand pick does not open it', async ($, on) => {
|
||||||
|
on('command.run', { command: 'model' }, () => ({ text: 'native' }))
|
||||||
|
await guardRig($, on, newWorld({ answers: ['This conversation'] }))
|
||||||
|
await modelCmd($, 'sonnet')
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GUARD: /clear re-arms it', async ($, on) => {
|
||||||
|
on('session.end', ($, e) => ({ sessionId: e.sessionId }))
|
||||||
|
await guardRig($, on)
|
||||||
|
await typeFeat($)
|
||||||
|
await endTurn($)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||||
|
await clearSession($)
|
||||||
|
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||||
|
})
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
// DEFAULT_CONFIG; ~/.claude/model-router.json overrides both per machine.
|
// DEFAULT_CONFIG; ~/.claude/model-router.json overrides both per machine.
|
||||||
// One writer per concern: the Agent tool's own params are never rewritten,
|
// One writer per concern: the Agent tool's own params are never rewritten,
|
||||||
// the spawn hook sets an agent's model once, turn.step sets efforts.
|
// the spawn hook sets an agent's model once, turn.step sets efforts.
|
||||||
|
// A main-loop code write before any skill ran is redirected to a skill.
|
||||||
import type {
|
import type {
|
||||||
CommandRunInput, CommandRunResult, EngineInterface, Next, On, Register,
|
CommandRunInput, CommandRunResult, EngineInterface, Next, On, Register,
|
||||||
TurnStepInput,
|
TurnStepInput,
|
||||||
@@ -79,6 +80,8 @@ type State = {
|
|||||||
// default rule, derived orchestrate; dropped at turn end
|
// default rule, derived orchestrate; dropped at turn end
|
||||||
runMain: Routed | null // best-tier skill row: spans the turns of a run;
|
runMain: Routed | null // best-tier skill row: spans the turns of a run;
|
||||||
// only a skill, /route clear|off or a user /model write or drop it
|
// only a skill, /route clear|off or a user /model write or drop it
|
||||||
|
skillRun: boolean // a skill ran on main this session: the write guard is
|
||||||
|
// open; dropped by /route clear|off and /clear
|
||||||
turnFloor: Routed | null // user-explicit level for this turn (prompt rule):
|
turnFloor: Routed | null // user-explicit level for this turn (prompt rule):
|
||||||
// a floor, main loop only
|
// a floor, main loop only
|
||||||
pendingPrompt: Routed | null // typed mid-turn: the next turn's floor
|
pendingPrompt: Routed | null // typed mid-turn: the next turn's floor
|
||||||
@@ -1001,6 +1004,7 @@ function newState(cfg: Config, source: string): State {
|
|||||||
userMain: null,
|
userMain: null,
|
||||||
turnMain: null,
|
turnMain: null,
|
||||||
runMain: null,
|
runMain: null,
|
||||||
|
skillRun: false,
|
||||||
turnFloor: null,
|
turnFloor: null,
|
||||||
pendingPrompt: null,
|
pendingPrompt: null,
|
||||||
typedSlash: null,
|
typedSlash: null,
|
||||||
@@ -1871,6 +1875,7 @@ function clearRoutes(st: State): void {
|
|||||||
st.userMain = null
|
st.userMain = null
|
||||||
st.turnMain = null
|
st.turnMain = null
|
||||||
st.runMain = null
|
st.runMain = null
|
||||||
|
st.skillRun = false
|
||||||
st.turnFloor = null
|
st.turnFloor = null
|
||||||
st.pendingPrompt = null
|
st.pendingPrompt = null
|
||||||
}
|
}
|
||||||
@@ -2182,6 +2187,7 @@ async function handleCommand($: Api, st: State, args: string): Promise<string> {
|
|||||||
st.offConfig = false
|
st.offConfig = false
|
||||||
st.lastPlan = null // the next step compares to the engine's input
|
st.lastPlan = null // the next step compares to the engine's input
|
||||||
if (st.off) st.runMain = null
|
if (st.off) st.runMain = null
|
||||||
|
if (st.off) st.skillRun = false
|
||||||
refresh($, st)
|
refresh($, st)
|
||||||
return show($, st)
|
return show($, st)
|
||||||
case 'reload':
|
case 'reload':
|
||||||
@@ -3784,6 +3790,7 @@ type Armed = { row: Picked | undefined; typed: boolean }
|
|||||||
function armTypedSkill($: Api, st: State, skill: string): Armed {
|
function armTypedSkill($: Api, st: State, skill: string): Armed {
|
||||||
const row = skillRow(st, skill)
|
const row = skillRow(st, skill)
|
||||||
const typed = typedPath(st, skill) !== undefined
|
const typed = typedPath(st, skill) !== undefined
|
||||||
|
if (typed) st.skillRun = true // unrowed too: its body is the user's
|
||||||
if (row === undefined || !typed) return { row: undefined, typed }
|
if (row === undefined || !typed) return { row: undefined, typed }
|
||||||
routeMainBySkill(st, skill, row, true)
|
routeMainBySkill(st, skill, row, true)
|
||||||
refresh($, st)
|
refresh($, st)
|
||||||
@@ -4385,6 +4392,74 @@ async function onModelSwitch($: Api, st: State, e: SwitchIn): Promise<void> {
|
|||||||
else clearBreaker(st, canonical(st.cfg, e.to_model))
|
else clearBreaker(st, canonical(st.cfg, e.to_model))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- routing guard: main-loop code goes through a skill (BDR-125 p2) ------
|
||||||
|
// A main-loop Edit/Write/NotebookEdit of a code file is redirected until a
|
||||||
|
// skill ran on main this session, a phase was declared this turn, or a typed
|
||||||
|
// /route holds. Docs, <root>/.claude/ (worktrees excepted), <root>/docs/ and
|
||||||
|
// sub-agents pass. Fail-open like every hook here. Limit: an unrowed typed
|
||||||
|
// skill latches through the typed-fallback path (typedPath) only when no
|
||||||
|
// sub-agent is live; the model's own Skill call latches it anyway.
|
||||||
|
|
||||||
|
const DOC_FILE = /\.(md|txt)$/i
|
||||||
|
const GUARD_REASON = 'model-router: code is written inside a skill run. ' +
|
||||||
|
'Enter /hotfix (≤2 files, obvious fix), /feat, /bugfix or /ship-feature, ' +
|
||||||
|
'or re-enter the skill you were running, then make this edit. This is a ' +
|
||||||
|
'redirect, not a refusal.'
|
||||||
|
|
||||||
|
/** The path a write tool targets, from its own argument name. */
|
||||||
|
function writtenPath(e: Rec): string | undefined {
|
||||||
|
const p = e.file_path ?? e.notebook_path
|
||||||
|
return typeof p === 'string' ? p : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Docs, the project's .claude tree and docs/ are free; the rest is code. */
|
||||||
|
function isFreePath(path: string, root: string): boolean {
|
||||||
|
if (path.includes('/../') || path.endsWith('/..')) return false
|
||||||
|
if (DOC_FILE.test(path)) return true
|
||||||
|
if (!path.startsWith(`${root}/`)) return false
|
||||||
|
const rel = path.slice(root.length + 1)
|
||||||
|
const claude = rel.startsWith('.claude/') &&
|
||||||
|
!rel.startsWith('.claude/worktrees/')
|
||||||
|
return claude || rel.startsWith('docs/')
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A skill ran, a phase was declared this turn, or a typed /route holds. */
|
||||||
|
function inRun(st: State): boolean {
|
||||||
|
const typed = st.userMain !== null && st.userMain.explicitModel !== true
|
||||||
|
return st.skillRun || st.turnMain?.source === 'model' || typed
|
||||||
|
}
|
||||||
|
|
||||||
|
async function guardWrite($: Api, st: State, e: Rec): Promise<boolean> {
|
||||||
|
await ensureConfig($, st)
|
||||||
|
if (st.off || e.agentId !== undefined || inRun(st)) return false
|
||||||
|
const path = writtenPath(e)
|
||||||
|
return path !== undefined && !isFreePath(path, await $.session.root())
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerGuard(on: On, st: State): void {
|
||||||
|
on('tool.call', { tool: 'Edit' }, async ($, e, next) => {
|
||||||
|
if (await guardWrite($, st, e)) return { deny: GUARD_REASON } as never
|
||||||
|
return next(e)
|
||||||
|
}).catch(($, e, next) => {
|
||||||
|
warnOnce(st, $, 'Edit guard', next.error.kind)
|
||||||
|
return next(e)
|
||||||
|
})
|
||||||
|
on('tool.call', { tool: 'Write' }, async ($, e, next) => {
|
||||||
|
if (await guardWrite($, st, e)) return { deny: GUARD_REASON } as never
|
||||||
|
return next(e)
|
||||||
|
}).catch(($, e, next) => {
|
||||||
|
warnOnce(st, $, 'Write guard', next.error.kind)
|
||||||
|
return next(e)
|
||||||
|
})
|
||||||
|
on('tool.call', { tool: 'NotebookEdit' }, async ($, e, next) => {
|
||||||
|
if (await guardWrite($, st, e)) return { deny: GUARD_REASON } as never
|
||||||
|
return next(e)
|
||||||
|
}).catch(($, e, next) => {
|
||||||
|
warnOnce(st, $, 'NotebookEdit guard', next.error.kind)
|
||||||
|
return next(e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// ---- registration ----------------------------------------------------
|
// ---- registration ----------------------------------------------------
|
||||||
|
|
||||||
async function readSessionModel($: Api): Promise<string> {
|
async function readSessionModel($: Api): Promise<string> {
|
||||||
@@ -4495,7 +4570,11 @@ function registerSkills(on: On, st: State): void {
|
|||||||
if (e.agentId === undefined && skillRow(st, skill)) {
|
if (e.agentId === undefined && skillRow(st, skill)) {
|
||||||
await tierAsk($, st, st.turnMain, `Skill /${clean(skill, NAME_CAP)}`)
|
await tierAsk($, st, st.turnMain, `Skill /${clean(skill, NAME_CAP)}`)
|
||||||
}
|
}
|
||||||
return await next(e)
|
const out = await next(e)
|
||||||
|
if (e.agentId === undefined && !('deny' in (out as Rec))) {
|
||||||
|
st.skillRun = true
|
||||||
|
}
|
||||||
|
return out
|
||||||
} finally {
|
} finally {
|
||||||
st.skillCalls -= 1
|
st.skillCalls -= 1
|
||||||
}
|
}
|
||||||
@@ -4845,6 +4924,7 @@ export const register: Register = on => {
|
|||||||
registerRouteTool(on, st)
|
registerRouteTool(on, st)
|
||||||
registerSkills(on, st)
|
registerSkills(on, st)
|
||||||
registerAgents(on, st)
|
registerAgents(on, st)
|
||||||
|
registerGuard(on, st)
|
||||||
registerSpawn(on, st)
|
registerSpawn(on, st)
|
||||||
registerTurns(on, st)
|
registerTurns(on, st)
|
||||||
registerPrompt(on, st)
|
registerPrompt(on, st)
|
||||||
|
|||||||
Reference in new issue
Block a user