Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
test: scale the timing bars to the machine, with a floor, in the tests part 4 adds and the ones that failed on Windows
Shrish Dwivedi7b379d387470
diff --git a/test/claude-guard-review.test.ts b/test/claude-guard-review.test.tsindex 915e603f..83ddd6e4 100644--- a/test/claude-guard-review.test.ts+++ b/test/claude-guard-review.test.ts@@ -14,6 +14,7 @@ import { spawnSync } from 'node:child_process';import { agentGuard } from '../src/skills/risk.js';import { skillsPrefilter } from '../src/claude/pre-bash.js';import { preBashSkills } from '../src/claude/pre-bash-skills.js';+import { timeBar } from './machine-speed.js';const ENV = { HOME: '/tmp/g7-guard-home', TRAIL_HOME: '/tmp/g7-guard-home/.trail' };const input = (command: string) => ({ session_id: 'sess-guard', tool_name: 'Bash', tool_input: { command } });@@ -92,9 +93,10 @@ test('a command built to make the guard backtrack is still read well inside theconst r = spawnSync(process.execPath, ['--import', 'tsx', '--input-type=module', '-e', probe], { encoding: 'utf8', timeout: 30_000 });assert.equal(r.signal, null, 'the guard ran past 30 s');const rows = JSON.parse(r.stdout) as Array<{ hit: string | null; pre: boolean; ms: number }>;+ const bar = timeBar(1500, 4500);for (const row of rows) {assert.ok(row.hit && row.pre, JSON.stringify(row));- assert.ok(row.ms < 1500, JSON.stringify(row));+ assert.ok(row.ms < bar, `${JSON.stringify(row)} over ${bar} ms`);}});@@ -128,5 +130,6 @@ test('the gated patterns read a long command in linear time: no span inside a caprocess.stdout.write(JSON.stringify(out));`;const r = spawnSync(process.execPath, ['--import', 'tsx', '--input-type=module', '-e', probe], { encoding: 'utf8', timeout: 60_000 });assert.equal(r.signal, null, 'the gated patterns ran past 60 s');- for (const row of JSON.parse(r.stdout) as Array<{ u: string; ms: number }>) assert.ok(row.ms < 1500, JSON.stringify(row));+ const bar = timeBar(1500, 4500);+ for (const row of JSON.parse(r.stdout) as Array<{ u: string; ms: number }>) assert.ok(row.ms < bar, `${JSON.stringify(row)} over ${bar} ms`);});diff --git a/test/init-record.test.ts b/test/init-record.test.tsindex 578bb619..6cd95cab 100644--- a/test/init-record.test.ts+++ b/test/init-record.test.ts@@ -10,6 +10,7 @@ import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync }import { tmpdir } from "node:os";import { dirname, join } from "node:path";import { emptyRecord, initRecordPath, MAX_RUNS, newRunId, pendingItems, readInitRecord, updateInitRecord, writeInitRecord, type InitRecord } from "../src/init/init-record.js";+import { timeBar } from "./machine-speed.js";const KEY = "github.com/acme/shop";const sha = "a".repeat(64);@@ -149,7 +150,7 @@ test("a FIFO planted at the record reads as no record, at once", { skip: processexecFileSync("mkfifo", [path]);const t0 = Date.now();assert.deepEqual(readInitRecord(KEY, h.env), emptyRecord(KEY));- assert.ok(Date.now() - t0 < 2000);+ assert.ok(Date.now() - t0 < timeBar(2000, 6000));} finally {h.done();}diff --git a/test/lesson-index-1b.test.ts b/test/lesson-index-1b.test.tsindex b58b52e8..9851da50 100644--- a/test/lesson-index-1b.test.ts+++ b/test/lesson-index-1b.test.ts@@ -11,7 +11,7 @@ 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 { slowness } from './machine-speed.js';+import { timeBar } from './machine-speed.js';const SHOP = 'github.com/acme/shop';const made: string[] = [];@@ -58,9 +58,10 @@ test('D117 (4): with the index warm, a wall hit reads 1,000 every-repo learningsconst 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 50: Windows counts CPU time in ~15.6 ms- // steps, so a 5 ms read can show as 31. A lost index (warm as slow as cold) still fails the line after.- const bar = Math.max(30 * slowness(), 50);+ // 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`);});diff --git a/test/machine-speed.ts b/test/machine-speed.tsindex b7aeef01..b7d29a70 100644--- a/test/machine-speed.ts+++ b/test/machine-speed.ts@@ -50,3 +50,14 @@ export function slowness(): number {cached ??= Math.min(10, Math.max(1, workloadMs() / REFERENCE_MS));return cached;}++/**+ * A timing bar for this machine: `ms` where it was set, times slowness(), and never under `floor`. The timing+ * tests catch the catastrophic (a backtracking regex, a lost index, O(n^2): ten times over or more), not a few+ * ms over on a busy shared runner, so a floor of 3 to 5 times the bar costs them nothing. Exactly `ms` under+ * TRAIL_PERF=1, the strict run.+ */+export function timeBar(ms: number, floor: number): number {+ if (process.env.TRAIL_PERF === "1") return ms;+ return Math.max(ms * slowness(), floor);+}diff --git a/test/skills-restore-version-guard.test.ts b/test/skills-restore-version-guard.test.tsindex 31b2b951..fecdc823 100644--- a/test/skills-restore-version-guard.test.ts+++ b/test/skills-restore-version-guard.test.ts@@ -10,6 +10,7 @@ import assert from "node:assert/strict";import { agentGuard } from "../src/skills/risk.js";import { skillsPrefilter } from "../src/claude/pre-bash.js";import { preBashSkills } from "../src/claude/pre-bash-skills.js";+import { timeBar } from "./machine-speed.js";const ENV = { HOME: "/tmp/c2-guard-home", TRAIL_HOME: "/tmp/c2-guard-home/.trail" };const input = (command: string) => ({ session_id: "sess-c2-guard", tool_name: "Bash", tool_input: { command } });@@ -61,9 +62,10 @@ test("1a's plain restore, show, and commands that only mention the words stay optest("a 2 MB command of restore words is read in well under the hook's 5 s", () => {const big = 1_900_000;+ const bar = timeBar(1500, 4500);for (const c of ["x; " + "trail skills restore a ".repeat(big / 23), "trail x; " + "skills restore $".repeat(big / 16), "s=1; trail $s " + "restore a --version ".repeat(big / 20)]) {const t = Date.now();agentGuard(c);- assert.ok(Date.now() - t < 1500, `${Date.now() - t} ms`);+ assert.ok(Date.now() - t < bar, `${Date.now() - t} ms, over ${bar} ms`);}});diff --git a/test/skills-update-parse.test.ts b/test/skills-update-parse.test.tsindex 2bd5b761..5769c5d4 100644--- a/test/skills-update-parse.test.ts+++ b/test/skills-update-parse.test.ts@@ -9,6 +9,7 @@ import { test } from "node:test";import assert from "node:assert/strict";import { parseSkillsUpdate, SAVE_GATE_RE, SKILLS_UPDATE_RE } from "../src/skills/save-parse.js";import { SAVE_GATE } from "../src/claude/pre-bash-skills.js";+import { timeBar } from "./machine-speed.js";const OK: Array<[string, string, string]> = [["trail skills update release-npm --offer o-1a2b3c", "release-npm", "o-1a2b3c"],@@ -61,5 +62,5 @@ test("the parser is linear: a 60 KB command of update words is read in millisecoconst c = `trail skills update ${"a ".repeat(30_000)}`;const t = Date.now();assert.equal(parseSkillsUpdate(c), null);- assert.ok(Date.now() - t < 500);+ assert.ok(Date.now() - t < timeBar(500, 2000));});diff --git a/test/skills-value-line.test.ts b/test/skills-value-line.test.tsindex 41c4fe04..be2a1ed7 100644--- a/test/skills-value-line.test.ts+++ b/test/skills-value-line.test.ts@@ -22,6 +22,7 @@ import {VALUE_COPY, guardrailStopsIn, markValueSaid, noteValue, startValue, valueContext, valueCounts, valueDue, valueLine, valuePath,valueWeek, valueWords,} from "../src/skills/value-line.js";+import { timeBar } from "./machine-speed.js";const SHOP = "github.com/acme/shop";const OTHER = "github.com/acme/storefront";@@ -232,13 +233,20 @@ test("the context has the D107 shape: Trail's opening, why, for the person, one});test("a busy value.json lock drops the count and never waits over 50 ms", () => {+ // The same call with the lock free, for what the work itself takes on this machine.+ const free = box("busy-free");+ noteValue(SHOP, { guardrailStops: 1 }, free.env, NOW);+ const f0 = Date.now();+ noteValue(SHOP, { guardrailStops: 1 }, free.env, NOW);+ const work = Date.now() - f0;const b = box("busy");noteValue(SHOP, { guardrailStops: 1 }, b.env, NOW);writeFileSync(`${valuePath(b.env)}.lock`, "4242");const t0 = Date.now();noteValue(SHOP, { guardrailStops: 5, wallFixes: 5 }, b.env, NOW);const waited = Date.now() - t0;- assert.ok(waited < 190, `waited ${waited} ms: the hook lock wait is 50 ms, not 1a's 200`);+ // Over the work itself, so a slow machine's work doesn't count as waiting; a 200 ms wait still shows.+ assert.ok(waited - work < 190, `waited ${waited} ms (the work alone ${work} ms): the hook lock wait is 50 ms, not 1a's 200`);assert.deepEqual(valueFile(b).repos[SHOP].days, { "2026-10-22": { g: 1, w: 0 } }, "the busy count is dropped");assert.equal(markValueSaid(SHOP, b.env, NOW), false, "a busy lock says so");assert.equal(startValue(start(), b.env, NOW), null, "and SessionStart says nothing then");@@ -285,7 +293,7 @@ test("never through a link: a linked state folder is not written; a FIFO at valuconst t0 = Date.now();assert.equal(valueDue(SHOP, c.env, NOW), true);assert.equal(valueLine([SHOP], c.env, NOW), null);- assert.ok(Date.now() - t0 < 2000, "the read did not wait on the FIFO");+ assert.ok(Date.now() - t0 < timeBar(2000, 6000), "the read did not wait on the FIFO");});test("guardrailStopsIn counts the guardrail's sure-fail denies a Stop collected, nothing else", () => {diff --git a/test/trace-guard-fast.test.ts b/test/trace-guard-fast.test.tsindex 98efa090..9d1146b8 100644--- a/test/trace-guard-fast.test.ts+++ b/test/trace-guard-fast.test.ts@@ -16,6 +16,7 @@ import { FIXED_DIRS, SURE_FAIL_REASON, markDenied, sureFail, timeoutOnPath } froimport { writeEveryRepo } from "../src/notes/every-repo.js";import { guardrailLearning } from "../src/notes/guardrails.js";import { sessionPath } from "../src/trace/gate-file.js";+import { timeBar } from "./machine-speed.js";const SRC = join(dirname(fileURLToPath(import.meta.url)), "..", "src");/** A machine with a real `timeout` in a system folder of 4.11 can't show a deny: those tests say so and skip. */@@ -194,5 +195,7 @@ test("a guard call takes under 2 ms (best of 200, file kept, no timeout anywheretimes.push(Number(process.hrtime.bigint() - t0) / 1e6);}times.sort((a, z) => a - z);- assert.ok(times[Math.floor(times.length / 2)]! < 2, `median ${times[100]} ms`);+ // 2 ms where the bar was set, scaled, never under 8: a guard that read a big file or spawned a child takes tens.+ const bar = timeBar(2, 8);+ assert.ok(times[Math.floor(times.length / 2)]! < bar, `median ${times[100]} ms, over ${bar.toFixed(1)} ms`);});diff --git a/test/trace-walls-review.test.ts b/test/trace-walls-review.test.tsindex 161588af..c9243509 100644--- a/test/trace-walls-review.test.ts+++ b/test/trace-walls-review.test.ts@@ -9,6 +9,7 @@ import { spawnSync } from "node:child_process";import { dirname, join } from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";import { lastTimeText, matchWall, sshRemoteFor, wallFixText } from "../src/trace/walls.js";+import { timeBar } from "./machine-speed.js";const SRC = join(dirname(fileURLToPath(import.meta.url)), "..", "src");@@ -87,15 +88,19 @@ test("a command over 64 KB matches nothing, and long commands stay fast (cappedconst n = 2 * 1024 * 1024;const cmds = ['('.repeat(n / 2) + ')'.repeat(n / 2), '{ '.repeat(n / 2), 'if true; then '.repeat(n / 14), "sh -c 'npm x' ; ".repeat(n / 16)];const outs = ['x'.repeat(n), ': '.repeat(n / 2), 'zsh:1:'.repeat(n / 6), 'a\\n'.repeat(n / 2)];+ // CPU time, best of 3: the work matchWall does, not how busy the machine is (the suite runs files side by side).+ const cpu = (fn) => { let best = Infinity; for (let k = 0; k < 3; k++) { const t = process.cpuUsage(); fn(); const d = process.cpuUsage(t); best = Math.min(best, (d.user + d.system) / 1000); } return best; };const rows = [];- for (const c of cmds) { const t = performance.now(); matchWall('npm ERR! code E401\\n', c, true); rows.push(performance.now() - t); }- for (const o of outs) { const t = performance.now(); matchWall(o, 'npm test', true); matchWall(o, 'npm test', false); rows.push(performance.now() - t); }+ for (const c of cmds) rows.push(cpu(() => matchWall('npm ERR! code E401\\n', c, true)));+ for (const o of outs) rows.push(cpu(() => { matchWall(o, 'npm test', true); matchWall(o, 'npm test', false); }));process.stdout.write(JSON.stringify(rows));`;const r = spawnSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", probe], { encoding: "utf8", timeout: 30_000 });assert.equal(r.signal, null, "matchWall ran past 30 s");assert.equal(r.status, 0, r.stderr);const rows = JSON.parse(r.stdout) as number[];- for (const ms of rows) assert.ok(ms < 20, `2 MB took ${ms.toFixed(1)} ms`);+ // 20 ms of CPU where the bar was set, scaled, never under 80: a backtracking pattern takes seconds on 2 MB.+ const bar = timeBar(20, 80);+ for (const ms of rows) assert.ok(ms < bar, `2 MB took ${ms.toFixed(1)} ms, over ${bar.toFixed(1)} ms`);});test("lastTimeText: a lesson's $ signs reach the line as written; junk dates don't make 'twice'", () => {