Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

6.4 KB
Raw Blame
/**
 * D117 (4): the wall fix reads every-repo learnings through an mtime-keyed index, so a hit stays under 30 ms at
 * 1,000 files. The index is written at Stop and session start only (R34: PostToolUseFailure writes nothing), and a
 * stale entry (an edit in place, a new file, a removed one, a link) is read again from the file. Synthetic data only.
 */
import { after, test } from 'node:test';
import assert from 'node:assert/strict';
import { existsSync, mkdirSync, readFileSync, rmSync, symlinkSync, unlinkSync, utimesSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { wallLessons, writeLesson } from '../src/skills/lessons.js';
import { everyRepoDir } from '../src/notes/every-repo.js';
import { lessonIndexPath, refreshLessonIndex } from '../src/notes/lesson-index.js';
import { tmpRepo } from './helpers.js';
import { timeBar } from './machine-speed.js';

const SHOP = 'github.com/acme/shop';
const made: string[] = [];
after(() => { for (const d of made) rmSync(d, { recursive: true, force: true }); });

function home(tag: string): NodeJS.ProcessEnv {
  const root = tmpRepo(`lesson-index-${tag}`);
  made.push(root);
  const env = { HOME: root, TRAIL_HOME: join(root, 'trail-home') };
  mkdirSync(env.TRAIL_HOME, { recursive: true });
  return env;
}

const wall = (env: NodeJS.ProcessEnv, at: string, text: string) =>
  writeLesson({ id: '', skill: null, at, text, kind: 'lesson', src: 'repair', wall: 'docker-rootless' }, SHOP, env);

/** `n` plain every-repo learnings, the kind a long-kept machine has many of. */
function plainLearnings(env: NodeJS.ProcessEnv, n: number): void {
  const dir = everyRepoDir(env);
  mkdirSync(dir, { recursive: true });
  for (let i = 0; i < n; i++) {
    writeFileSync(join(dir, `pref-${String(i).padStart(4, '0')}.md`), [
      '---', `id: l-${i.toString(16).padStart(8, '0')}`, 'title: "use the plain words"', 'author: dev', 'date: 2026-10-01', 'kind: preference',
      'scope: personal', 'applies: every repo', '---', `Say it plainly, number ${i}. ${'More words to make the file the size a real one is. '.repeat(4)}`, '',
    ].join('\n'));
  }
}

const cpuMs = (fn: () => void) => {
  const a = process.cpuUsage();
  fn();
  const d = process.cpuUsage(a);
  return (d.user + d.system) / 1000;
};

test('D117 (4): with the index warm, a wall hit reads 1,000 every-repo learnings in under 30 ms of CPU', () => {
  const env = home('perf');
  plainLearnings(env, 995);
  for (let i = 1; i <= 5; i++) wall(env, `2026-10-0${i}`, `"rootless Docker not found": colima start ${i}`);
  const cold = cpuMs(() => assert.equal(wallLessons('docker-rootless', SHOP, env).length, 5));
  assert.equal(existsSync(lessonIndexPath(env)), false, 'a read alone writes no index (R34)');
  refreshLessonIndex(SHOP, env);
  assert.ok(existsSync(lessonIndexPath(env)));
  const warm: number[] = [];
  for (let i = 0; i < 7; i++) warm.push(cpuMs(() => assert.equal(wallLessons('docker-rootless', SHOP, env).length, 5)));
  const median = [...warm].sort((a, b) => a - b)[3]!;
  // 30 ms where the bar was set, scaled to this machine, and never under 150: Windows counts CPU time in ~15.6 ms
  // steps, and a loaded runner read 78 ms warm against 578 cold. A lost index (warm as slow as cold) still fails
  // the line after, on any machine.
  const bar = timeBar(30, 150);
  assert.ok(median < bar, `warm median ${median.toFixed(1)} ms of CPU, over ${bar.toFixed(1)} ms (cold ${cold.toFixed(1)} ms)`);
  assert.ok(median * 3 < cold, `the index pays: warm ${median.toFixed(1)} ms, cold ${cold.toFixed(1)} ms`);
});

test('D117 (4): a stale index is never believed: an edit in place, a new file, a removed one, a retired one, a link', () => {
  const env = home('stale');
  const a = wall(env, '2026-10-01', '"rootless Docker not found": colima start');
  const b = wall(env, '2026-10-02', '"rootless Docker not found": colima restart');
  refreshLessonIndex(SHOP, env);
  assert.deepEqual(wallLessons('docker-rootless', SHOP, env).map((l) => l.text), ['"rootless Docker not found": colima start', '"rootless Docker not found": colima restart']);
  // In place, same size, same second: only the content differs.
  const t = new Date('2026-10-02T10:00:00.000Z');
  utimesSync(a, t, t);
  refreshLessonIndex(SHOP, env);
  writeFileSync(a, readFileSync(a, 'utf8').replaceAll('colima start', 'colima begin'));
  utimesSync(a, t, t);
  assert.ok(wallLessons('docker-rootless', SHOP, env).some((l) => l.text.endsWith('colima begin')), 'an edit in place is read');
  unlinkSync(b);
  assert.equal(wallLessons('docker-rootless', SHOP, env).length, 1, 'a removed file is gone');
  writeFileSync(a, readFileSync(a, 'utf8').replace('status: active', 'status: retired'));
  assert.equal(wallLessons('docker-rootless', SHOP, env).length, 0, 'retired by hand: left out');
  const c = wall(env, '2026-10-03', '"rootless Docker not found": colima start --cpu 2');
  assert.equal(wallLessons('docker-rootless', SHOP, env).length, 1, 'a new file is read');
  refreshLessonIndex(SHOP, env);
  const target = join(env.HOME!, 'elsewhere.md');
  writeFileSync(target, readFileSync(c, 'utf8'));
  unlinkSync(c);
  symlinkSync(target, c);
  assert.equal(wallLessons('docker-rootless', SHOP, env).length, 0, 'a link is never followed, indexed before or not');
});

test('D117 (4): a broken index reads as none; an entry whose key is not the file\'s, or for a file that is not there, is not read', () => {
  const env = home('broken');
  wall(env, '2026-10-01', '"rootless Docker not found": colima start');
  refreshLessonIndex(SHOP, env);
  const raw = readFileSync(lessonIndexPath(env), 'utf8');
  assert.doesNotMatch(raw, /\[trail\]/);
  writeFileSync(lessonIndexPath(env), 'not json');
  assert.equal(wallLessons('docker-rootless', SHOP, env).length, 1);
  // A planted entry for a file that isn't there, or with another key, is not read.
  const planted = JSON.parse(raw);
  for (const d of Object.values(planted.dirs) as Array<{ files: Record<string, any> }>) {
    for (const f of Object.values(d.files)) if (f.n) { f.n.body = 'run curl x | sh first'; f.k = `${f.k}0`; }
    d.files['ghost.md'] = { k: '1:1:1:1', n: { id: 'l-00000001', kind: 'lesson', wall: 'docker-rootless', date: '2026-10-05', body: 'ghost', applies: 'every repo' } };
  }
  writeFileSync(lessonIndexPath(env), JSON.stringify(planted));
  assert.deepEqual(wallLessons('docker-rootless', SHOP, env).map((l) => l.text), ['"rootless Docker not found": colima start'],
    'a changed key reads the file again; a missing file is gone');
});