Skip to content

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

feat(claude): active retrieval — inject graft ask matches on prompt

Shrish Dwivedib94b3d158e71
diff --git a/.claude/settings.json b/.claude/settings.json
index f5a74e26..74be35e7 100644
--- a/.claude/settings.json
+++ b/.claude/settings.json
@@ -18,6 +18,13 @@
{ "type": "command", "command": "node \"${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/graft-hooks.cjs\" stop", "timeout": 8000 }
]
}
+ ],
+ "UserPromptSubmit": [
+ {
+ "hooks": [
+ { "type": "command", "command": "node \"${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/graft-hooks.cjs\" prompt", "timeout": 8000 }
+ ]
+ }
]
}
}
diff --git a/src/claude/format.ts b/src/claude/format.ts
index 98abfba1..12f09e0b 100644
--- a/src/claude/format.ts
+++ b/src/claude/format.ts
@@ -71,3 +71,19 @@ export function formatBlastRadius(w: GraphV1, filePath: string, cap = 8): string
const more = edges.length > cap ? `\n • +${edges.length - cap} more` : '';
return `[graft] blast radius for ${basename(filePath)} — who depends on it:\n${items.join('\n')}${more}`;
}
+
+export interface AskJson {
+ query: string; mode: string;
+ hits: { kind: string; title: string; pointer: string; snippet: string; score: number }[];
+}
+
+export function formatRetrieval(ask: AskJson, cap = 5): string | null {
+ const hits = (ask.hits ?? []).slice(0, cap);
+ if (!hits.length) return null;
+ const lines = hits.map((h) => {
+ const ptr = (h.pointer ?? '').split(',')[0].trim();
+ const snip = (h.snippet ?? '').replace(/\s+/g, ' ').trim().slice(0, 140);
+ return ` • ${h.title} — ${ptr} — ${snip}`;
+ });
+ return `[graft] relevant context for this prompt:\n${lines.join('\n')}`;
+}
diff --git a/src/claude/hooks.ts b/src/claude/hooks.ts
index 5cb0a793..4fd30f2c 100644
--- a/src/claude/hooks.ts
+++ b/src/claude/hooks.ts
@@ -2,8 +2,8 @@ import { readFileSync } from 'node:fs';
import { execFileSync, spawn } from 'node:child_process';
import { join, basename } from 'node:path';
import { readWiring } from './stats.js';
-import { formatBlastRadius } from './format.js';
-import { patchStats, readStats, acquireLock } from './state.js';
+import { formatBlastRadius, formatRetrieval } from './format.js';
+import { patchStats, readStats, acquireLock, readSession, writeSession } from './state.js';
function readStdin(): any {
const seam = process.env.GRAFT_TEST_STDIN;
@@ -58,4 +58,20 @@ export async function main(event: string): Promise<void> {
}
return;
}
+
+ if (event === 'prompt') {
+ const prompt = String(input?.prompt ?? '').trim();
+ if (prompt.length < 8) return;
+ const ask = graftJson(dir, ['ask', prompt, '.', '--json', '-n', '5']);
+ if (!ask) return;
+ const txt = formatRetrieval(ask);
+ if (!txt) return;
+ emit('UserPromptSubmit', txt);
+ const id = input.session_id || 'default';
+ const s = readSession(dir, id);
+ s.lastQuery = prompt;
+ const agent = input?.agent?.name;
+ if (agent) s.perAgentQuery[agent] = prompt;
+ writeSession(dir, id, s);
+ }
}
diff --git a/test/claude-format.test.ts b/test/claude-format.test.ts
index f5374238..59e70ae6 100644
--- a/test/claude-format.test.ts
+++ b/test/claude-format.test.ts
@@ -1,6 +1,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { renderStatusline, enrichedSegment, incomingEdges, formatBlastRadius } from '../src/claude/format.js';
+import { renderStatusline, enrichedSegment, incomingEdges, formatBlastRadius, formatRetrieval } from '../src/claude/format.js';
import { emptyStats } from '../src/claude/state.js';
const strip = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, '');
@@ -62,3 +62,17 @@ test('formatBlastRadius renders callers or null', () => {
assert.match(strip(txt!), /exchange \(client\.ts\)/);
assert.equal(formatBlastRadius(wiring2, '/abs/repo/src/unknown.ts'), null);
});
+
+test('formatRetrieval renders top hits, trims snippet, first pointer only', () => {
+ const ask = { query: 'pkce', mode: 'lexical', hits: [
+ { kind: 'concept', title: 'PKCE', pointer: 'src/pkce.ts, src/client.ts', snippet: 'Validates the challenge.', score: 1 },
+ ] } as any;
+ const txt = strip(formatRetrieval(ask)!);
+ assert.match(txt, /relevant context/);
+ assert.match(txt, /PKCE — src\/pkce\.ts — Validates the challenge\./);
+ assert.doesNotMatch(txt, /client\.ts/); // only the first pointer segment
+});
+
+test('formatRetrieval returns null for no hits', () => {
+ assert.equal(formatRetrieval({ query: 'x', mode: 'empty', hits: [] } as any), null);
+});