1
0

offload-eval-hook.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. // UserPromptSubmit hook — APPROACH 1: additive context-injection.
  3. // Front-loads codegraph's structural answer for flow/impact/"how/where" prompts so the
  4. // agent's reflex grep/read has nothing left to find. Strictly additive (never blocks),
  5. // gated to structural prompts (no cost otherwise), and uses RAW explore (offload disabled)
  6. // so the injected context is accurate — never the (currently low-fidelity) synthesis.
  7. //
  8. // Reads {prompt, cwd} as JSON on stdin; prints the explore result to stdout (which Claude
  9. // Code injects into the agent's context). Any failure -> silent exit 0 (degradable).
  10. import { pathToFileURL, fileURLToPath } from 'node:url';
  11. import { resolve, join, dirname } from 'node:path';
  12. import { existsSync, readFileSync, appendFileSync } from 'node:fs';
  13. // Resolve the engine repo from this script's own location (scripts/agent-eval/ -> ../..),
  14. // overridable with CG_ENGINE. The hook ships inside the repo, so it finds its own dist.
  15. const HERE = dirname(fileURLToPath(import.meta.url));
  16. const ENGINE = process.env.CG_ENGINE || resolve(HERE, '..', '..');
  17. const BUDGET = Number(process.env.CG_FRONTLOAD_BUDGET || 16000);
  18. // Debug log only when CG_FRONTLOAD_DEBUG is set to a file path (the harness points it at a
  19. // log to count injections); off by default so the shipped hook writes nothing extra.
  20. const DBG = process.env.CG_FRONTLOAD_DEBUG;
  21. const dbg = (m) => { if (!DBG) return; try { appendFileSync(DBG, `[${new Date().toISOString()}] ${m}\n`); } catch { /* ignore */ } };
  22. let input = {};
  23. try { input = JSON.parse(readFileSync(0, 'utf8')); } catch (e) { dbg('stdin parse fail: ' + e.message); }
  24. const prompt = String(input.prompt || '');
  25. const cwd = String(input.cwd || process.cwd());
  26. dbg(`invoked: promptLen=${prompt.length} cwd=${cwd}`);
  27. // Gate: only structural / flow / impact / where-how questions. Cheap regex; silent no-op
  28. // otherwise so non-structural prompts ("fix this typo") cost nothing.
  29. const STRUCTURAL = /\b(how|where|trace|flow|path|reach(es|ed)?|call(s|ed|er|ers|ee)?|depend|impact|affect|wire[ds]?|connect|implement|architect|structure|breaks?|what calls|why does)\b/i;
  30. if (!prompt || !STRUCTURAL.test(prompt)) { dbg('gate: non-structural, no-op'); process.exit(0); }
  31. dbg('gate: structural PASS');
  32. // Find the index: cwd, then walk up a few levels.
  33. let root = cwd, found = null;
  34. for (let i = 0; i < 6 && root; i++) {
  35. if (existsSync(join(root, '.codegraph'))) { found = root; break; }
  36. const parent = resolve(root, '..'); if (parent === root) break; root = parent;
  37. }
  38. if (!found) { dbg(`no .codegraph found from cwd=${cwd}`); process.exit(0); }
  39. dbg(`found index at ${found}`);
  40. try {
  41. process.env.CODEGRAPH_OFFLOAD_DISABLE = '1'; // raw, accurate — never the unfixed offload
  42. process.env.CODEGRAPH_TELEMETRY = '0'; process.env.DO_NOT_TRACK = '1';
  43. const load = async (rel) => import(pathToFileURL(resolve(ENGINE, rel)).href);
  44. const idx = await load('dist/index.js');
  45. const tools = await load('dist/mcp/tools.js');
  46. const CodeGraph = idx.default?.default ?? idx.default ?? idx.CodeGraph;
  47. const ToolHandler = tools.ToolHandler ?? tools.default?.ToolHandler;
  48. if (typeof CodeGraph?.openSync !== 'function' || typeof ToolHandler !== 'function') process.exit(0);
  49. // Retry once on a transient busy/locked index (the hook's openSync can race a
  50. // freshly-warming daemon on the first prompt of a session).
  51. let text = '';
  52. for (let attempt = 1; attempt <= 2; attempt++) {
  53. try {
  54. const cg = CodeGraph.openSync(found);
  55. const h = new ToolHandler(cg);
  56. const res = await h.execute('codegraph_explore', { query: prompt });
  57. text = res?.content?.[0]?.text ?? '';
  58. try { cg.close?.(); } catch { /* ignore */ }
  59. dbg(`explore attempt ${attempt} returned ${text.length} chars`);
  60. break;
  61. } catch (e) {
  62. dbg(`explore attempt ${attempt} failed: ${e?.message || e}`);
  63. if (attempt === 2) throw e;
  64. await new Promise((r) => setTimeout(r, 800));
  65. }
  66. }
  67. if (!text.trim()) { dbg('empty explore result, no-op'); process.exit(0); }
  68. if (text.length > BUDGET) text = text.slice(0, BUDGET) + '\n…[front-load truncated to budget]';
  69. process.stdout.write(
  70. `## CodeGraph structural context (auto-retrieved for this question)\n` +
  71. `The code graph was queried for your question; the relevant symbols, source, and call flow are below. ` +
  72. `Treat the quoted source as already read. If you need more, call codegraph_explore with specific symbol names rather than grepping or reading files.\n\n` +
  73. text + '\n'
  74. );
  75. dbg(`INJECTED ${text.length} chars`);
  76. } catch (e) { dbg('ERROR: ' + (e?.stack || e?.message || e)); process.exit(0); } // degradable