Skip to content

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

fix(claude): statusline falls back to wiring.json when stats.json cache is absent

Shrish Dwivedi191421c62819
diff --git a/src/claude/statusline.ts b/src/claude/statusline.ts
index 0f5bbca7..d9411606 100644
--- a/src/claude/statusline.ts
+++ b/src/claude/statusline.ts
@@ -1,6 +1,23 @@
import { readFileSync } from 'node:fs';
import { renderStatusline, renderSubagent } from './format.js';
-import { readStats, readSession } from './state.js';
+import { readStats, readSession, emptyStats, type Stats } from './state.js';
+import { readWiring, computeStats } from './stats.js';
+
+/**
+ * The statusline's fast path is the hook-maintained cache (graft/.cache/stats.json).
+ * When it's absent — a fresh checkout, or a plain `graft build` that doesn't write the
+ * cache — fall back to reading the graph itself (wiring.json) so the bar reflects reality
+ * immediately instead of showing "not built". The graph carries no drift signal, so it
+ * reads as synced until the next edit repopulates the cache. Still a pure read (no
+ * subprocess): the cache is preferred because it carries live dirty/stale state.
+ */
+export function resolveStats(dir: string): Stats | null {
+ const cached = readStats(dir);
+ if (cached && cached.nodeCount > 0) return cached;
+ const wiring = readWiring(dir);
+ if (wiring) return { ...emptyStats(), ...computeStats(wiring) };
+ return cached;
+}
export function main(): void {
let input: any = {};
@@ -9,7 +26,7 @@ export function main(): void {
const session = readSession(dir, input.session_id || 'default');
const agent = input?.agent?.name;
if (agent) { process.stdout.write(renderSubagent(agent, session)); return; }
- const stats = readStats(dir);
+ const stats = resolveStats(dir);
const raw = input?.context_window?.used_percentage;
const ctxPct = typeof raw === 'number' ? Math.round(raw) : null;
process.stdout.write(renderStatusline(stats, session, { ctxPct }).join('\n'));
diff --git a/test/claude-statusline.test.ts b/test/claude-statusline.test.ts
new file mode 100644
index 00000000..b3c4844f
--- /dev/null
+++ b/test/claude-statusline.test.ts
@@ -0,0 +1,47 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { resolveStats } from '../src/claude/statusline.js';
+import { writeStats, emptyStats } from '../src/claude/state.js';
+
+function repo(): string { return mkdtempSync(join(tmpdir(), 'graft-sl-')); }
+function writeWiring(dir: string, obj: unknown): void {
+ mkdirSync(join(dir, 'graft', '.graph'), { recursive: true });
+ writeFileSync(join(dir, 'graft', '.graph', 'wiring.json'), JSON.stringify(obj));
+}
+
+test('resolveStats returns the hook-maintained cache when present and non-empty', () => {
+ const d = repo();
+ writeStats(d, { ...emptyStats(), nodeCount: 5, edgeCount: 9, dirty: true, staleCount: 2 });
+ const s = resolveStats(d)!;
+ assert.equal(s.nodeCount, 5);
+ assert.equal(s.dirty, true, 'cache is the source of truth when present');
+});
+
+test('resolveStats falls back to wiring.json when no cache (manual graft build / fresh checkout)', () => {
+ const d = repo();
+ writeWiring(d, {
+ meta: { nodeCount: 42, edgeCount: 100, languages: ['typescript'] },
+ nodes: [{ id: 'a', summary_state: 'ready' }, { id: 'b', summary_state: 'pending' }],
+ edges: [],
+ });
+ const s = resolveStats(d)!;
+ assert.equal(s.nodeCount, 42, 'reflects the graph immediately instead of "not built"');
+ assert.equal(s.readyCount, 1);
+ assert.equal(s.dirty, false, 'defaults to synced — no drift signal available from the graph alone');
+});
+
+test('resolveStats prefers the cache over the graph even when both exist', () => {
+ const d = repo();
+ writeWiring(d, { meta: { nodeCount: 42, edgeCount: 100, languages: [] }, nodes: [], edges: [] });
+ writeStats(d, { ...emptyStats(), nodeCount: 7, edgeCount: 3, dirty: true, staleCount: 4 });
+ const s = resolveStats(d)!;
+ assert.equal(s.nodeCount, 7, 'cache wins (it carries live drift state the graph cannot)');
+ assert.equal(s.staleCount, 4);
+});
+
+test('resolveStats returns null when neither cache nor graph exists', () => {
+ assert.equal(resolveStats(repo()), null);
+});