diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index 5c9ad9c..d886ae4 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -4,7 +4,7 @@ import type { On } from 'claude-code' import { cap, tail, redact, normalize, failureLine, sessionOf, addFailure, entriesOf, parseIndex, isLive, lessonOf, assembleEntries, grams, - buildIndex, matchOf, + buildIndex, matchOf, deErrno, segments, suiteKeyOf, commitLabel, finishLabel, } from './register' @@ -27,6 +27,7 @@ const INDEX_OUT = [ type Proc = { stdout: string } | 'throw' | 'exit2' | 'hang' type World = { + home: string | undefined // what env.get answers for HOME switchText: string | undefined // ~/.claude/memory.json (undefined: absent) rootFails: boolean // session.root rejects id: string // what session.id answers now @@ -93,7 +94,7 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise { unreadable: new Set(o.unreadable ?? []), statFails: new Set(o.statFails ?? []), existsFails: new Set(o.existsFails ?? []), - proc: { stdout: INDEX_OUT }, runs: [], + proc: { stdout: INDEX_OUT }, runs: [], home: HOME, gets: 0, getsFail: false, } handStore(on, w, o.storeFails ?? false) @@ -101,7 +102,7 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise { w.tools.push(e.name) return { value: undefined } as never }) - on('env.get', () => ({ value: HOME })) + on('env.get', () => ({ value: w.home })) on('fs.exists', (_a, e) => { if (w.existsFails.has(e.path)) throw new Error('EIO') return { value: w.files.has(e.path) || @@ -1651,8 +1652,8 @@ for (const [name, proc] of [ w.proc = proc await failing($, w, EXIT1 + SR_LINE) const text = await pending($) - expect(text).toContain( - 'index: read blockers.md, learnings.md; status reader unavailable') + expect(text.split('\n').find(l => l.startsWith('index:'))) + .toBe('index: read blockers.md, learnings.md; status reader unavailable') expect(text).toContain('- known BLK-001 ×1 ' + SR_LINE) }) } @@ -1664,10 +1665,10 @@ test('SR5: process.run never settles, the answer comes at 2500 ms', async ( await failing($, w, EXIT1 + SR_LINE) w.proc = 'hang' const answer = pending($) - await new Promise(r => setTimeout(r, 20)) // let the call reach its wait await w.clock.advance(2500) const text = await answer - expect(text).toContain('read blockers.md, learnings.md; status reader unav') + expect(text.split('\n').find(l => l.startsWith('index:'))) + .toBe('index: read blockers.md, learnings.md; status reader unavailable') expect(text).toContain('- known BLK-001 ×1 ' + SR_LINE) }) @@ -1681,6 +1682,35 @@ test('SR7: no registry read, no process run, no unavailable note', async ( expect(text).not.toContain('status reader') }) +test('SR8: HOME unset, status unavailable, fails open', async ($, on) => { + const w = await boot($, on, { files: REGS }) + await failing($, w, EXIT1 + SR_LINE) + w.home = undefined + const text = await pending($) + expect(w.runs).toEqual([]) + expect(text.split('\n').find(l => l.startsWith('index:'))) + .toBe('index: read blockers.md, learnings.md; status reader unavailable') + expect(text).toContain('- known BLK-001 ×1 ' + SR_LINE) +}) + +test('SR9: the status word is its leading letters only', () => { + const row = '| BDR-031 | 2026-06-25 | t | superseded-by BDR-040 |' + expect(parseIndex(row).get('BDR-031')?.status).toBe('superseded') +}) + +test('TIMEindex: parseIndex on 64 KiB stays under 50 ms', () => { + const rows = [ + '| LRN-001 | 2026-01-01 | ' + 'a | '.repeat(16000) + ' | open |', + '| BLK-001 | ' + '|'.repeat(65000), + ('| LRN-002 | 2026-01-01 | t | open |\n').repeat(2000), + ] + for (const text of rows) { + const t0 = now() + parseIndex(text.slice(0, 65536)) + expect(now() - t0).toBeLessThan(50) + } +}) + test('LS1: a BDR Decision reads its first sentence', () => { expect(lessonOf('BDR', '## BDR-1 t\n- **Decision**: Use X. Then Y.\n')) .toBe('Use X.') @@ -1753,3 +1783,52 @@ test('TIMEentries: entriesOf and lessonOf on 64 KiB fields, under 50 ms', () => expect(now() - t0).toBeLessThan(50) } }) + +// ---- Task 2 (phase 4c): errno phrases out of the error index ---- + +const ER_ENTRY = '## LRN-009 e\n- **Status**: open\n' + +test('ER1: an ENOENT line never makes another ENOENT known', async ( + $, on, +) => { + const text = ER_ENTRY + + 'seen: `cat: x: No such file or directory (os error 2)`\n' + const w = await boot($, on, { files: { [LRN]: text } }) + w.proc = { stdout: '| LRN-009 | 2026-01-01 | e | open |' } + await failing($, w, + EXIT1 + "ls: cannot access '/tmp/zz': No such file or directory") + expect(await pending($)).toContain('- NEW ×1 ls: cannot access') +}) + +test('ER2: content grams still match around an errno-free line', async ( + $, on, +) => { + const text = ER_ENTRY + 'seen: `fatal: unable to access remote host thing`\n' + const w = await boot($, on, { files: { [LRN]: text } }) + w.proc = { stdout: '| LRN-009 | 2026-01-01 | e | open |' } + await failing($, w, EXIT1 + 'fatal: unable to access remote host thing') + expect(await pending($)).toContain('- known LRN-009 ×1 fatal:') +}) + +test('ER3: deErrno replaces a phrase on a normalized line', () => { + expect(deErrno('cat: : no such file or directory (os error #)')) + .toBe('cat: : (os error #)') + expect(deErrno('xpermission denied here')).toBe('xpermission denied here') +}) + +test('ER4: a gram of placeholders only is skipped', () => { + expect(grams(' # ')).toEqual([]) +}) + +test('ER5: input/output error becomes despite the path rule', () => { + expect(normalize('cp: Input/output error (os error 5)')) + .toBe('cp: (os error #)') + expect(normalize('read /dev/sda1: input/output error')) + .toBe('read : ') +}) + +test('TIMEerrno: deErrno on 64 KiB of a phrase prefix, under 50 ms', () => { + const t0 = now() + deErrno('no such file or '.repeat(4096)) + expect(now() - t0).toBeLessThan(50) +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index d9a4aa9..dd8cf42 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -112,6 +112,7 @@ const CURL_SEGMENT = new RegExp( const COOKIE_FLAG = new RegExp(`(?<=^|\\s)-b[ \\t]{1,5}${VALUE}`, 'g') const HEX_RUN = /\b[0-9a-f]{7,}\b/g +const IO_ERROR = /input\/output error/g const PATH = /(?:\.{0,2}\/)?[\w.-]{1,200}(?:\/[\w.-]{1,200}){1,30}/g /** The first `n` code units of `text` (input guard for regex work). */ @@ -156,6 +157,7 @@ export function redact(text: string): string { export function normalize(line: string): string { return line .toLowerCase() + .replace(IO_ERROR, '') // before PATH reads its slash .replace(HEX_RUN, '') .replace(PATH, '') .replace(/\d+/g, '#') @@ -465,7 +467,7 @@ export function parseIndex(stdout: string): StatusMap { for (const line of stdout.split('\n')) { const m = INDEX_ROW.exec(line.slice(0, MAX_INDEX_ROW)) if (m === null) continue - const word = (m[3] ?? '').trim().match(/^[A-Za-z-]+/)?.[0] ?? '' + const word = (m[3] ?? '').trim().match(/^[A-Za-z]+/)?.[0] ?? '' const title = (m[2] ?? '').trim() rows.set(m[1] ?? '', { status: word.toLowerCase(), title }) } @@ -514,14 +516,33 @@ export function assembleEntries(texts: string[], index: StatusMap): Entry[] { }) } -const PLACEHOLDERS = new Set(['#', '', '']) +export const ERRNO_PHRASES: readonly string[] = [ + 'no such file or directory', 'permission denied', 'command not found', + 'is a directory', 'not a directory', 'file exists', + 'directory not empty', 'operation not permitted', + 'no space left on device', 'connection refused', 'connection timed out', + 'broken pipe', 'resource temporarily unavailable', 'invalid argument', + 'too many open files', 'read-only file system', + 'device or resource busy', 'input/output error', +] + +// Word-bounded literals; `input/output error` is already `` here. +const ERRNO = new RegExp( + `(?`. */ +export function deErrno(normLine: string): string { + return normLine.replace(ERRNO, '') +} + +const PLACEHOLDERS = new Set(['#', '', '', '']) /** * Every window of 4 words, except one made only of placeholders (it * carries no content); a key of fewer words has none (always NEW). */ export function grams(normLine: string): string[] { - const words = normLine.split(/[^a-z0-9<>#]+/).filter(Boolean) + const words = deErrno(normLine).split(/[^a-z0-9<>#]+/).filter(Boolean) if (words.length < GRAM_WORDS) return [] return words .slice(0, words.length - GRAM_WORDS + 1)