feat(model-router): write guard fails closed, refused Skill no longer latches
When the guard itself fails, a main-loop code write outside a skill run is held (next.called split); tool errors replay as before. A Skill answer carrying isError no longer opens the guard.
This commit is contained in:
1 parent
2a27032ac4
commit
859e5040b6
2 files changed
+133
-18
No files matched your search
@@ -51,6 +51,7 @@ type World = {
|
||||
onAsk: (() => void) | undefined // called when a dialog opens
|
||||
onWrite: (() => void) | undefined // called when an fs.write starts
|
||||
repo: { root: string; remote: string | null } | null | 'fail'
|
||||
root: 'fail' | undefined // session.root throws (the guard's own failure)
|
||||
env: string | undefined // HOME as env.get answers it (undefined = unset)
|
||||
kinds: Map<string, 'file' | 'dir' | 'other'> // per-path stat kind override
|
||||
dirs: Map<string, {
|
||||
@@ -146,6 +147,7 @@ function newWorld(fx: Fixture = {}): World {
|
||||
onAsk: undefined,
|
||||
onWrite: undefined,
|
||||
repo: fx.repo === undefined ? { root: ROOT, remote: REMOTE } : fx.repo,
|
||||
root: undefined,
|
||||
env: fx.noHome ? undefined : HOME,
|
||||
kinds: new Map(),
|
||||
dirs: new Map(),
|
||||
@@ -206,7 +208,10 @@ function installWorld(on: On, w: World): void {
|
||||
return { value: text }
|
||||
})
|
||||
installWrite(on, w)
|
||||
on('session.root', () => ({ value: ROOT }))
|
||||
on('session.root', () => {
|
||||
if (w.root === 'fail') throw new Error('no root')
|
||||
return { value: ROOT }
|
||||
})
|
||||
on('session.surfaces', () => ({ value: w.surfaces as never }))
|
||||
on('session.repo', () => {
|
||||
if (w.repo === 'fail') throw new Error('no repo')
|
||||
@@ -7469,3 +7474,79 @@ test('GUARD: a denied 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)
|
||||
})
|
||||
|
||||
// the guard's own failure (session.root throws) holds a main code write
|
||||
|
||||
const HELD = 'the write guard failed ('
|
||||
|
||||
const rootFails = async ($: Engine, on: On) => {
|
||||
const w = newWorld()
|
||||
await guardRig($, on, w)
|
||||
w.root = 'fail'
|
||||
return w
|
||||
}
|
||||
|
||||
test('GUARD: a failing guard holds Write, Edit and NotebookEdit', async (
|
||||
$, on) => {
|
||||
await rootFails($, 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)
|
||||
for (const out of [await write($, `${ROOT}/src/a.ts`), ed, nb]) {
|
||||
expect(denied(out)).toContain(HELD)
|
||||
expect(denied(out)).toContain('so this write is held')
|
||||
}
|
||||
})
|
||||
|
||||
test('GUARD: a failing guard still passes docs and sub-agents', async (
|
||||
$, on) => {
|
||||
await rootFails($, on)
|
||||
expect(denied(await write($, `${ROOT}/README.md`))).toBe('')
|
||||
expect(denied(await write($, `${ROOT}/src/a.ts`, { agentId: 'a1' })))
|
||||
.toBe('')
|
||||
})
|
||||
|
||||
test('GUARD: a failing guard still passes an in-run write', async (
|
||||
$, on) => {
|
||||
const w = await rootFails($, on)
|
||||
w.root = undefined
|
||||
await typeFeat($)
|
||||
await endTurn($)
|
||||
w.root = 'fail'
|
||||
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||
})
|
||||
|
||||
test('GUARD: a downstream tool failure is never turned into a hold', async (
|
||||
$, on) => {
|
||||
on('tool.call', { tool: 'Write' }, () => {
|
||||
throw new Error('disk full')
|
||||
})
|
||||
await bootRun($, on, OPUS, newWorld())
|
||||
const out = await $.tool.call({ tool: 'Write',
|
||||
file_path: `${ROOT}/README.md`, content: 'x' } as never)
|
||||
.catch((err: unknown) => ({ thrown: String(err) }))
|
||||
expect(denied(out)).not.toContain(HELD)
|
||||
expect(JSON.stringify(out)).not.toContain(HELD)
|
||||
// the kit rejects with HooksError ("no implementation for tool.call")
|
||||
expect((out as { thrown?: string }).thrown).toContain('HooksError')
|
||||
})
|
||||
|
||||
test('GUARD: a model Skill call frees a held write', async ($, on) => {
|
||||
await rootFails($, on)
|
||||
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(HELD)
|
||||
await $.tool.call({ tool: 'Skill', skill: 'diagram' } as never)
|
||||
expect(denied(await write($, `${ROOT}/src/a.ts`))).toBe('')
|
||||
})
|
||||
|
||||
test('GUARD: an isError Skill answer leaves the latch closed', async (
|
||||
$, on) => {
|
||||
on('tool.call', { tool: 'Skill' }, () => ({
|
||||
result: { success: false } as never, isError: true,
|
||||
text: 'unknown skill',
|
||||
}) as never)
|
||||
writeBottoms(on)
|
||||
await bootRig($, on, OPUS, 1000, newWorld())
|
||||
await $.tool.call({ tool: 'Skill', skill: 'diagram' } as never)
|
||||
expect(denied(await write($, `${ROOT}/src/a.ts`))).toContain(GUARD)
|
||||
})
|
||||
@@ -4399,15 +4399,25 @@ async function onModelSwitch($: Api, st: State, e: SwitchIn): Promise<void> {
|
||||
// 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.
|
||||
// sub-agents pass. The guard's own failure holds a main-loop code write
|
||||
// outside a skill run (fail closed via next.called, BDR-125 phase 2 rule,
|
||||
// amends BDR-136 (4)); sub-agents, skill runs and doc files still pass; a
|
||||
// downstream tool error is replayed as before. Residual: if the config load
|
||||
// keeps throwing, nothing latches in-session, so main-loop code writes stay
|
||||
// held while sub-agents (skill executors) still write; no throw source known.
|
||||
// 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.'
|
||||
const GUARD_FAIL = (kind: string): string =>
|
||||
`model-router: the write guard failed (${kind}), so this write is held. ` +
|
||||
'Enter /hotfix, /feat, /bugfix or /ship-feature, or re-enter the skill ' +
|
||||
'you were running, then make this edit.'
|
||||
|
||||
/** The path a write tool targets, from its own argument name. */
|
||||
function writtenPath(e: Rec): string | undefined {
|
||||
@@ -4439,28 +4449,52 @@ async function guardWrite($: Api, st: State, e: Rec): Promise<boolean> {
|
||||
return path !== undefined && !isFreePath(path, await $.session.root())
|
||||
}
|
||||
|
||||
type GuardNext = ((e: never) => unknown) & {
|
||||
readonly called: boolean
|
||||
readonly error: { readonly kind: string }
|
||||
}
|
||||
|
||||
/** Writes the guard never holds: sub-agents, skill runs, doc files. */
|
||||
function guardPasses(st: State, e: Rec): boolean {
|
||||
const path = writtenPath(e)
|
||||
const doc = path === undefined ||
|
||||
(DOC_FILE.test(path) && !path.includes('/../') && !path.endsWith('/..'))
|
||||
return e.agentId !== undefined || inRun(st) || doc
|
||||
}
|
||||
|
||||
/** The guard hook failed: replay a downstream error, else hold the write. */
|
||||
function guardCaught(
|
||||
st: State,
|
||||
$: Api,
|
||||
e: Rec,
|
||||
next: GuardNext,
|
||||
hook: string,
|
||||
) {
|
||||
if (next.called || guardPasses(st, e)) {
|
||||
warnOnce(st, $, hook, next.error.kind)
|
||||
return next(e as never) as never
|
||||
}
|
||||
if (!st.warned.has('write guard held')) {
|
||||
st.warned.add('write guard held')
|
||||
tryLog($, `model-router: write guard failed (${next.error.kind}): ` +
|
||||
'main-loop code writes are held until a skill runs')
|
||||
}
|
||||
return { deny: GUARD_FAIL(next.error.kind) } as never
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}).catch(($, e, next) => guardCaught(st, $, e, next, 'Edit guard'))
|
||||
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)
|
||||
})
|
||||
}).catch(($, e, next) => guardCaught(st, $, e, next, 'Write guard'))
|
||||
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)
|
||||
})
|
||||
}).catch(($, e, next) => guardCaught(st, $, e, next, 'NotebookEdit guard'))
|
||||
}
|
||||
|
||||
// ---- registration ----------------------------------------------------
|
||||
@@ -4574,8 +4608,8 @@ function registerSkills(on: On, st: State): void {
|
||||
await tierAsk($, st, st.turnMain, `Skill /${clean(skill, NAME_CAP)}`)
|
||||
}
|
||||
const out = await next(e)
|
||||
const refused = typeof out === 'object' && out !== null &&
|
||||
'deny' in out
|
||||
const refused = typeof out !== 'object' || out === null ||
|
||||
'deny' in out || (out as { isError?: unknown }).isError === true
|
||||
if (e.agentId === undefined && !refused) st.skillRun = true
|
||||
return out
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user