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 part 6's timing bars to the machine, with a floor
Shrish Dwivediad59e56a5c0d
diff --git a/test/claude-hook-timeouts.test.ts b/test/claude-hook-timeouts.test.tsindex b2e12fda..c7b6ac91 100644--- a/test/claude-hook-timeouts.test.ts+++ b/test/claude-hook-timeouts.test.ts@@ -18,6 +18,7 @@ import { installCodexHooks } from '../src/hosts/codex-hooks.js';import { handlePrompt, PROMPT_BUDGET_MS, promptAskTimeout } from '../src/claude/prompt-hook.js';import { writeNote } from '../src/notes/notes.js';import { tmpRepo } from './helpers.js';+import { timeBar } from './machine-speed.js';type Hooks = Record<string, Array<{ hooks: Array<{ command: string; timeout: number }> }>>;@@ -126,7 +127,9 @@ test('the prompt hook ends inside its total budget, with the learnings, when theconst started = Date.now();const out = await captured(() => handlePrompt({ session_id: 's1', prompt: 'how do we promote this build to prod safely' }, dir, { budgetMs: 1500 }));const took = Date.now() - started;- assert.ok(took < 1500 + 700, `took ${took} ms on a 1500 ms budget`);+ // The budget, and what a slow machine adds around it; under the default 4 s budget, which would show if this one were ignored.+ const bar = Math.min(1500 + timeBar(700, 2100), 3900);+ assert.ok(took < bar, `took ${took} ms on a 1500 ms budget (bar ${bar} ms)`);assert.equal(readFileSync(called, 'utf8'), '1', 'the ask was started inside the budget');assert.match(JSON.parse(out).hookSpecificOutput.additionalContext, /Promote to prod only after beta is green/);} finally {@@ -155,7 +158,8 @@ test('the default budget holds a never-answering ask to a few seconds, whateverconst started = Date.now();const out = await captured(() => handlePrompt({ session_id: 's1', prompt: 'how do we promote this build to prod safely' }, dir));const took = Date.now() - started;- assert.ok(took < PROMPT_BUDGET_MS + 700, `took ${took} ms`);+ // Far under the installed 15 s however slow the machine: 700 ms scaled is at most 7 s.+ assert.ok(took < PROMPT_BUDGET_MS + timeBar(700, 2100), `took ${took} ms`);assert.match(JSON.parse(out).hookSpecificOutput.additionalContext, /Promote to prod/);} finally {delete process.env.GRAFT_TEST_CLI;@@ -186,7 +190,9 @@ test('the budget holds when the ask is slow to die (a refresh under way handlesconst started = Date.now();const out = await captured(() => handlePrompt({ session_id: 's1', prompt: 'how do we promote this build to prod safely' }, dir, { budgetMs: 1500 }));const took = Date.now() - started;- assert.ok(took < 1500 + 700, `took ${took} ms on a 1500 ms budget`);+ // The budget, and what a slow machine adds around it; under the 3 s the stub's parse takes, which a wait for it would show.+ const bar = Math.min(1500 + timeBar(700, 1400), 2900);+ assert.ok(took < bar, `took ${took} ms on a 1500 ms budget (bar ${bar} ms)`);assert.match(JSON.parse(out).hookSpecificOutput.additionalContext, /Promote to prod only after beta is green/);const pid = Number(readFileSync(pidFile, 'utf8'));assert.ok(await goneWithin(pid, 6000), 'the cut ask ends on its own once its work is done');diff --git a/test/claude-hooks-1b-review-trust.test.ts b/test/claude-hooks-1b-review-trust.test.tsindex e9a84543..640bfccb 100644--- a/test/claude-hooks-1b-review-trust.test.ts+++ b/test/claude-hooks-1b-review-trust.test.ts@@ -19,6 +19,7 @@ import { readWalls } from '../src/trace/wall-state.js';import { updateInitRecord } from '../src/init/init-record.js';import { writeEveryRepo } from '../src/notes/every-repo.js';import { guardrailCandidate } from './init-screen-fixtures.js';+import { timeBar } from './machine-speed.js';test('D87 through the hooks: the kept notice the agent is asked to say holds no double quote of its own, and an undo is only ever a name or an id', async () => {const b = box('r1b-d87-quotes');@@ -275,7 +276,7 @@ test('session start keeps its budget while the init record is locked (a keep orconst t0 = Date.now();const out = await startHook(s, at(17, 0));const ms = Date.now() - t0;- assert.ok(ms < 1000, `session start took ${ms} ms with the record locked`);+ assert.ok(ms < timeBar(1000, 3000), `session start took ${ms} ms with the record locked`);oneOrNothing(out, 'start');assert.equal(readInitRecordOf(b).notice?.shownAt, null, 'named next time');} finally {diff --git a/test/claude-hooks-perf.test.ts b/test/claude-hooks-perf.test.tsindex ce336072..c3ede7ce 100644--- a/test/claude-hooks-perf.test.ts+++ b/test/claude-hooks-perf.test.ts@@ -20,6 +20,7 @@ import { readSession } from '../src/claude/state.js';import { writeNote } from '../src/notes/notes.js';import { jsonl, toolResult, typed } from './trace-fixtures.js';import { tmpRepo } from './helpers.js';+import { timeBar } from './machine-speed.js';const SESSION = '0b9c8d7e-6f5a-4b3c-9d2e-1f0a9b8c7d6e';@@ -126,9 +127,9 @@ async function measure(bytes: number, rounds: number, tag: string): Promise<{ fitest('on a growing transcript every hook stays well under 300 ms, and Stop reads only the new turn', async () => {const { first, times } = await measure(4 * 1024 * 1024, 10, 'small');- assert.ok(first < 1000, `first Stop ${first.toFixed(1)} ms`);- // Generous under a loaded full-suite run; the TRAIL_PERF run below holds the 300 ms line.- assert.ok(p95(times.stop) < 300, `Stop p95 ${p95(times.stop).toFixed(1)} ms`);+ assert.ok(first < timeBar(1000, 3000), `first Stop ${first.toFixed(1)} ms`);+ // Scaled to this machine, with a floor, under a loaded full-suite run; the TRAIL_PERF run below holds the 300 ms line.+ assert.ok(p95(times.stop) < timeBar(300, 1000), `Stop p95 ${p95(times.stop).toFixed(1)} ms`);});test('on a 200 MB transcript each hook is under 300 ms at p95 (TRAIL_PERF=1)', { skip: process.env.TRAIL_PERF === '1' ? false : 'set TRAIL_PERF=1 to run' }, async () => {diff --git a/test/claude-hooks-review.test.ts b/test/claude-hooks-review.test.tsindex b092fe19..983d63ec 100644--- a/test/claude-hooks-review.test.ts+++ b/test/claude-hooks-review.test.ts@@ -20,6 +20,7 @@ import { withoutOffer } from '../src/claude/stop-skills.js';import {agentRuns, at, box, cli, done, finish, lastCli, one, promptHook, release, reply, session, skillsKept, stateOf, stopHook,} from './claude-skills-journey-fixtures.js';+import { timeBar } from './machine-speed.js';const OFFER = { lines: ['Published 1.4.3: npm shows it. That\'s the 3rd day I\'ve worked out this release from scratch (4 commands this time).', 'Keep the 2 that worked as a skill, just for you in this repo? (yes / show me / no)'] as [string, string] };@@ -256,11 +257,11 @@ test('a 2 MB command with a step that can\'t be undone in it still asks inside tconst d = await ask(command);const ms = Date.now() - t;assert.equal(d?.permissionDecision, 'ask', tail);- assert.ok(ms < 4000, `${tail}: ${ms} ms (the hook has 5 s)`);+ assert.ok(ms < timeBar(4000, 12_000), `${tail}: ${ms} ms (the hook has 5 s)`);}const t = Date.now();assert.equal(await ask(`echo ${'a|'.repeat(900_000)}`), null, 'nothing gated in it: nothing to ask');- assert.ok(Date.now() - t < 4000);+ assert.ok(Date.now() - t < timeBar(4000, 12_000));} finally {done(b);}diff --git a/test/claude-pre-bash.test.ts b/test/claude-pre-bash.test.tsindex 90f1c590..89f3a14f 100644--- a/test/claude-pre-bash.test.ts+++ b/test/claude-pre-bash.test.ts@@ -18,6 +18,7 @@ import { hooksShim } from '../src/claude/shim-template.js';import { mergeGraftSettings } from '../src/claude/settings-merge.js';import { tmpRepo } from './helpers.js';import { RELAY_LINE } from '../src/init/relay.js';+import { timeBar } from './machine-speed.js';const SRC = resolve('src');/** Loaded by `import()` only for a `skills add` command (pre-bash-skills.ts). */@@ -275,21 +276,24 @@ test('a payload whose stdin never closes ends the hook with exit 0, inside the hconst p = project('pre-bash-open-stdin');fakePackage(p.dir);const child = hostRun(p);- const r = await settle(child, (stdin) => stdin.write('{"tool_name":"Bash","tool_input":{"command":"npm'));+ // 4.5 s where the bar was set, scaled, never under 13.5 s; the child is killed 3.5 s past it, which fails the exit code.+ const bar = timeBar(4500, 13_500);+ const r = await settle(child, (stdin) => stdin.write('{"tool_name":"Bash","tool_input":{"command":"npm'), bar + 3500);child.stdin!.destroy();assert.equal(r.code, 0);assert.equal(r.stdout, '');- assert.ok(r.ms < 4500, `gave up after ${r.ms} ms: the host allows 5000`);+ assert.ok(r.ms < bar, `gave up after ${r.ms} ms: the host allows 5000`);});test('a 5 MB payload is read to its end and lets the command through, quickly and silently', async () => {const p = project('pre-bash-huge-stdin');fakePackage(p.dir);const huge = JSON.stringify({ ...BASH, tool_input: { command: `cat > /tmp/x/big.txt <<'EOF'\n${'a'.repeat(5 * 1024 * 1024)}\nEOF` } });- const r = await settle(hostRun(p), (stdin) => stdin.end(huge));+ const bar = timeBar(4500, 13_500);+ const r = await settle(hostRun(p), (stdin) => stdin.end(huge), bar + 3500);assert.equal(r.code, 0);assert.equal(r.stdout, '');- assert.ok(r.ms < 4500, `took ${r.ms} ms`);+ assert.ok(r.ms < bar, `took ${r.ms} ms`);});/**diff --git a/test/claude-prompt-learnings.test.ts b/test/claude-prompt-learnings.test.tsindex ff0acc78..46b4c423 100644--- a/test/claude-prompt-learnings.test.ts+++ b/test/claude-prompt-learnings.test.ts@@ -14,6 +14,7 @@ import { BRAND_ENV } from '../src/brand.js';import { main } from '../src/claude/hooks.js';import { writeNote } from '../src/notes/notes.js';import { tmpRepo } from './helpers.js';+import { timeBar } from './machine-speed.js';const PROD = { title: 'Promote to prod only after beta is green', body: '## Decided\nPush to beta first, wait for its deploy, then fast-forward prod.\n', author: 'Priya', date: '2026-10-07', touches: [] };const DNS = { title: 'DNS changes go through the infra repo', body: '## Decided\nNever edit records by hand.\n', author: 'Kumar', date: '2026-10-06', touches: [] };@@ -92,7 +93,7 @@ test('learnings still arrive when the graph ask runs out of time', async () => {const { stub, calledFile } = askStub(dir, `setTimeout(() => {}, 60000);`);const started = Date.now();const text = context(await prompt('how do we promote this build to prod safely', stub));- assert.ok(Date.now() - started < 20000, 'the ask was cut off');+ assert.ok(Date.now() - started < timeBar(20_000, 40_000), 'the ask was cut off (the stand-in sleeps 60 s)');assert.equal(existsSync(calledFile), true);assert.match(text, /Promote to prod only after beta is green/);});diff --git a/test/hooks-fifo.test.ts b/test/hooks-fifo.test.tsindex fdb7617a..3b5d223a 100644--- a/test/hooks-fifo.test.ts+++ b/test/hooks-fifo.test.ts@@ -12,6 +12,7 @@ import { execFileSync, spawnSync } from 'node:child_process';import { mkdirSync, mkdtempSync, rmSync, statSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, resolve } from 'node:path';+import { timeBar } from './machine-speed.js';const SRC = resolve(import.meta.dirname, '..', 'src');const FIXTURES = resolve(import.meta.dirname, 'claude-skills-journey-fixtures.ts');@@ -41,7 +42,7 @@ test('a FIFO at the PostToolUse mark file: appendPostMark returns at once and wrfifoAt(path);const ms = inChild(root, env, `const { appendPostMark } = await import(${JSON.stringify(join(SRC, 'trace', 'gate-file.ts'))});appendPostMark('s-fifo', { id: 'toolu_1', at: '2026-10-01T10:00:01.000Z', failed: false }, env);`);- assert.ok(ms < 100, `${ms} ms`);+ assert.ok(ms < timeBar(100, 400), `${ms} ms`);assert.ok(stillFifo(path));} finally {rmSync(root, { recursive: true, force: true });@@ -56,7 +57,7 @@ test('a FIFO at the hook-error log: hookError returns at once and writes nothingfifoAt(path);const ms = inChild(root, env, `const { hookError } = await import(${JSON.stringify(join(SRC, 'claude', 'skills-hook-util.ts'))});hookError('prompt', new TypeError('made up'), env);`);- assert.ok(ms < 100, `${ms} ms`);+ assert.ok(ms < timeBar(100, 400), `${ms} ms`);assert.ok(stillFifo(path));} finally {rmSync(root, { recursive: true, force: true });diff --git a/test/skills-anchors-wide-review.test.ts b/test/skills-anchors-wide-review.test.tsindex bfb66a6c..130d5501 100644--- a/test/skills-anchors-wide-review.test.ts+++ b/test/skills-anchors-wide-review.test.ts@@ -19,6 +19,7 @@ import type { ParsedSkill } from "../src/skills/types.js";import { promptSkills, type PromptDeps } from "../src/claude/prompt-skills.js";import { releaseSkill, WEBSHOP } from "./skills-recall-fixtures.js";import { box as promptBox, CWD, gateFor, payload, save } from "./skills-prompt-fixtures.js";+import { timeBar } from "./machine-speed.js";const require = createRequire(import.meta.url);@@ -153,7 +154,8 @@ test("ci-file: many CI anchors read the folder once, inside the scan's deadline}const took = Date.now() - t;assert.ok(reads <= 64, `each CI file read at most once (${reads} reads)`);- assert.ok(took < 400, `inside the cap (${took} ms)`);+ // The 150 ms deadline, and what a slow machine adds around it.+ assert.ok(took < 150 + timeBar(250, 750), `inside the cap (${took} ms)`);assert.ok(out.length === 0 || out.length === 12, "past the deadline nothing is judged; inside it, all are");});diff --git a/test/skills-anchors-wide.test.ts b/test/skills-anchors-wide.test.tsindex 52d6ec89..b783ce9b 100644--- a/test/skills-anchors-wide.test.ts+++ b/test/skills-anchors-wide.test.ts@@ -17,6 +17,7 @@ import { cloneShape } from "./anchors-clone-fixture.js";import { WIDE_SCAN_MS, wideAnchorsOf, wideGone, type WideAnchor } from "../src/skills/anchors-wide.js";import { goneAnchors } from "../src/skills/anchors.js";import type { ParsedSkill } from "../src/skills/types.js";+import { timeBar } from "./machine-speed.js";const require = createRequire(import.meta.url);@@ -174,7 +175,7 @@ test("service: a FIFO at the compose path doesn't block (D100)", { skip: processexecFileSync("mkfifo", [join(b.checkout, "compose.yaml")]);const t0 = Date.now();assert.deepEqual(gone(b, ["docker compose up worker"]), []);- assert.ok(Date.now() - t0 < 1000);+ assert.ok(Date.now() - t0 < timeBar(1000, 3000));execFileSync("mkfifo", [join(b.checkout, ".env")]);put(b, "docker-compose.yml", COMPOSE);rmSync(join(b.checkout, "compose.yaml"));diff --git a/test/skills-contracts.test.ts b/test/skills-contracts.test.tsindex d76bf637..314f5b9b 100644--- a/test/skills-contracts.test.ts+++ b/test/skills-contracts.test.ts@@ -15,6 +15,7 @@ import {appendPrivateLine, ensurePrivateDir, readJsonSafe, trailPath, withLock, writePrivate, writePrivateJson,} from "../src/util/private-fs.js";import { hookError } from "../src/claude/skills-hook-util.js";+import { timeBar } from "./machine-speed.js";const SRC = join(dirname(fileURLToPath(import.meta.url)), "..", "src");const POSIX = process.platform !== "win32";@@ -212,7 +213,7 @@ test("withLock gives up on a timeout and never runs fn", () => {assert.deepEqual(got, { ok: false });assert.equal(ran, false, "a fold that ran twice would count every call twice");assert.ok(took >= 50, `waited ${took} ms`);- assert.ok(took < 1000, `gave up promptly (${took} ms)`);+ assert.ok(took < timeBar(1000, 3000), `gave up promptly (${took} ms)`);assert.equal(existsSync(`${file}.lock`), true, "someone else's lock is left alone");});diff --git a/test/skills-guard-text.test.ts b/test/skills-guard-text.test.tsindex a8bbc20f..1c078247 100644--- a/test/skills-guard-text.test.ts+++ b/test/skills-guard-text.test.ts@@ -13,7 +13,7 @@ import assert from 'node:assert/strict';import { spawnSync } from 'node:child_process';import { agentGuard } from '../src/skills/risk.js';import { preBashSkills } from '../src/claude/pre-bash-skills.js';-import { slowness } from './machine-speed.js';+import { timeBar } from './machine-speed.js';const ENV = { HOME: '/tmp/guard-text-home', TRAIL_HOME: '/tmp/guard-text-home/.trail' };const input = (command: string) => ({ session_id: 'sess-guard-text', tool_name: 'Bash', tool_input: { command } });@@ -152,7 +152,7 @@ test('the narrowed guard stays linear: 2 MB of each shape reads in under 20 ms oprocess.stdout.write(JSON.stringify(out));`;const 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');- // 20 ms where the bar was set, scaled to this machine (a backtracking regex takes seconds on 2 MB, on any machine).- const bar = 20 * slowness();+ // 20 ms where the bar was set, scaled to this machine, never under 80 (a backtracking regex takes seconds on 2 MB, on any machine).+ const bar = timeBar(20, 80);for (const row of JSON.parse(r.stdout) as Array<{ ms: number }>) assert.ok(row.ms < bar, `${JSON.stringify(row)} over ${bar.toFixed(1)} ms`);});diff --git a/test/skills-risk-login.test.ts b/test/skills-risk-login.test.tsindex ecc8ac02..c61814d3 100644--- a/test/skills-risk-login.test.ts+++ b/test/skills-risk-login.test.ts@@ -17,6 +17,7 @@ import { programOf } from '../src/trace/shell.js';import { gateFilePath, type GateFile } from '../src/trace/gate-file.js';import { at, box, done, session } from './claude-skills-journey-fixtures.js';import { releaseSkill } from './skills-recall-fixtures.js';+import { timeBar } from './machine-speed.js';const LOGINS = ['npm login', 'npm adduser', 'npm login --registry https://registry.npmjs.org', 'npm --registry=https://npm.acme.dev login', 'pnpm login', 'yarn npm login',@@ -125,5 +126,6 @@ test('the login pattern reads a 2 MB command in linear time', () => {process.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/trace-skill-session.test.ts b/test/trace-skill-session.test.tsindex 85267e30..2b733e29 100644--- a/test/trace-skill-session.test.ts+++ b/test/trace-skill-session.test.ts@@ -18,6 +18,7 @@ import { readRunSheet, runSheetPath, writeRunSheet } from "../src/trace/runs.js"import { withLock } from "../src/util/private-fs.js";import { compactBoundary, jsonl, toolUse } from "./trace-fixtures.js";import { ask, at, bash, emptyState, injection, reply, runState } from "./trace-run-fixtures.js";+import { timeBar } from "./machine-speed.js";const SID = "5f0c1a2b-3c4d-4e5f-8a9b-0c1d2e3f4a5b";const PASS = "Test Files 3 passed (3)\n Tests 42 passed (42)";@@ -315,7 +316,7 @@ test("a first catch-up on a long transcript reads only its last 8 MB", () => {withSkillSession(ctx, { catchUp: true }, (st) => {assert.deepEqual(st.runs.map((r) => r.job), ["tests"], "the early publish was never read");});- assert.ok(performance.now() - t0 < 2000);+ assert.ok(performance.now() - t0 < timeBar(2000, 6000));} finally {rmSync(b.homeDir, { recursive: true, force: true });}diff --git a/test/upkeep-hooks.test.ts b/test/upkeep-hooks.test.tsindex 920e2100..3ffa54fe 100644--- a/test/upkeep-hooks.test.ts+++ b/test/upkeep-hooks.test.ts@@ -10,6 +10,7 @@ import { delimiter, dirname, join } from 'node:path';import { main } from '../src/claude/hooks.js';import { readStamp, runningVersion, updateCachePath } from '../src/upkeep.js';import { tmpRepo } from './helpers.js';+import { timeBar } from './machine-speed.js';/** A repo that looks like a previous `graft init` ran here, with no stamp — i.e.* wired by a graft old enough not to have written one. */@@ -106,7 +107,7 @@ test('a stale cache is used as-is: the hook never fetches', async () => {const before = Date.now();const ctx = contextOf(await runHook('session-start', repo, home));assert.match(ctx, /99\.0\.0 available/, 'stale answer still shown rather than refetched');- assert.ok(Date.now() - before < 2000, 'no network round trip');+ assert.ok(Date.now() - before < timeBar(2000, 6000), 'no network round trip');});test('an unwired repo is left completely alone', async () => {