From 859e5040b6b9fc1e79b93545750b1da4541133e0 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 16 Nov 2026 01:16:56 +0100 Subject: [PATCH] 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. --- mods/model-router/hooks/register.test.ts | 83 +++++++++++++++++++++++- mods/model-router/hooks/register.ts | 68 ++++++++++++++----- 2 files changed, 133 insertions(+), 18 deletions(-) diff --git a/mods/model-router/hooks/register.test.ts b/mods/model-router/hooks/register.test.ts index 2d13ce5..2fd2174 100644 --- a/mods/model-router/hooks/register.test.ts +++ b/mods/model-router/hooks/register.test.ts @@ -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 // per-path stat kind override dirs: Map ({ 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) +}) diff --git a/mods/model-router/hooks/register.ts b/mods/model-router/hooks/register.ts index 7a9624d..112c605 100644 --- a/mods/model-router/hooks/register.ts +++ b/mods/model-router/hooks/register.ts @@ -4399,15 +4399,25 @@ async function onModelSwitch($: Api, st: State, e: SwitchIn): Promise { // 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, /.claude/ (worktrees excepted), /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 { 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 {