From 929c5dc50c77b715da958756393fa5f2658a77a6 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 12:20:03 +0100 Subject: [PATCH] fix(memory): fail-open exists probe, drop dead readRegistry --- mods/memory/hooks/register.test.ts | 36 +++++++++++++++++++++++++++--- mods/memory/hooks/register.ts | 17 ++++++++------ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index be4a8b3..f0d4a90 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -25,6 +25,8 @@ type World = { tools: string[] // every tool name registered files: Map // other files (null: a directory) unreadable: Set // paths whose fs.read throws + statFails: Set // paths whose fs.stat throws though they exist + existsFails: Set // paths whose fs.exists throws } type Opts = { @@ -34,6 +36,8 @@ type Opts = { storeFails?: boolean // store.set rejects (a hand-made store) files?: Record // registries under the root unreadable?: string[] // paths whose fs.read throws + statFails?: string[] // paths whose fs.stat throws though they exist + existsFails?: string[] // paths whose fs.exists throws } /** @@ -64,6 +68,8 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise { sets: [], logs: [], tools: [], files: new Map(Object.entries(o.files ?? {})), unreadable: new Set(o.unreadable ?? []), + statFails: new Set(o.statFails ?? []), + existsFails: new Set(o.existsFails ?? []), } handStore(on, w, o.storeFails ?? false) on('tool.register', (_a, e) => { @@ -71,10 +77,12 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise { return { value: undefined } as never }) on('env.get', () => ({ value: HOME })) - on('fs.exists', (_a, e) => ({ - value: w.files.has(e.path) || e.path === SWITCH, - })) + on('fs.exists', (_a, e) => { + if (w.existsFails.has(e.path)) throw new Error('EIO') + return { value: w.files.has(e.path) || e.path === SWITCH } + }) on('fs.stat', (_a, e) => { + if (w.statFails.has(e.path)) throw new Error('EIO') if (w.files.has(e.path)) { const text = w.files.get(e.path) return { value: { @@ -809,3 +817,25 @@ test('a span of 12 chars is indexed, one of 11 is not', () => { expect(at12.size).toBe(1) expect(at11.size).toBe(0) }) + +test('a rejected stat on an existing registry is unreadable', async ( + $, on, +) => { + const w = await boot($, on, { files: REGS, statFails: [BLK] }) + await failing($, w, EXIT1 + 'error: pathspec did not match anything') + expect(await pending($)).toContain( + 'index: read learnings.md; skipped blockers.md (unreadable)') +}) + +test('a throwing exists probe names the registry, keeps the other', async ( + $, on, +) => { + const files = { [BLK]: BLOCKERS, [LRN]: LEARNINGS.replace( + 'superseded', 'open') } + const w = await boot($, on, { files, statFails: [BLK], existsFails: [BLK] }) + await failing($, w, EXIT1 + 'error: pathspec did not match anything') + const text = await pending($) + expect(text).toContain( + 'index: read learnings.md; skipped blockers.md (unreadable)') + expect(text).toContain('- known LRN-002 \u00d71') +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index 4bfc3a5..2e96fb0 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -294,6 +294,15 @@ type Entry = { id: string; body: string } type Read = { text: string } | { why: string } +/** True only when exists says no; a throwing probe cannot tell. */ +async function isGone($: Api, path: string): Promise { + try { + return !(await $.fs.exists(path)) + } catch { + return false + } +} + /** One stat, one read: the text, or why the registry is skipped. */ // a rejected stat means missing only when exists says so async function tryRead($: Api, path: string): Promise { @@ -302,7 +311,7 @@ async function tryRead($: Api, path: string): Promise { if (info.kind !== 'file') return { why: 'not a file' } if (info.size > MAX_REGISTRY) return { why: 'over 4 MiB' } } catch { - return { why: (await $.fs.exists(path)) ? 'unreadable' : 'missing' } + return { why: (await isGone($, path)) ? 'missing' : 'unreadable' } } try { return { text: await $.fs.read(path) } @@ -311,12 +320,6 @@ async function tryRead($: Api, path: string): Promise { } } -/** A registry's text; `''` when skipped (missing, not a file, too big). */ -export async function readRegistry($: Api, path: string): Promise { - const r = await tryRead($, path) - return 'text' in r ? r.text : '' -} - /** The BLK/LRN entries; a body runs from its heading to the next one. */ export function entriesOf(text: string): Entry[] { const heads = [...text.matchAll(HEADING)]