Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.
TypeScript antigravity-skillsbusiness-knowledgeclaude-codeclaude-skillscodebase-analysiscodexcodex-skillsdeveloper-tools-ai-agentgemini-cli-skillskarpathy-llm-wikiknowledge-baseknowledge-graphmemoryopencode-skillspi-agentunderstandcodevibe-coding
refactor(hooks): extract post-tool auto-update handler
KumamuKuma20f88686e413
diff --git a/tests/hooks/post-tool-use-auto-update.test.mjs b/tests/hooks/post-tool-use-auto-update.test.mjsnew file mode 100644index 00000000..e49d6ec3--- /dev/null+++ b/tests/hooks/post-tool-use-auto-update.test.mjs@@ -0,0 +1,66 @@+import { spawnSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { dirname, join, resolve } from 'node:path';+import { fileURLToPath } from 'node:url';+import { describe, expect, it } from 'vitest';++const __dirname = dirname(fileURLToPath(import.meta.url));+const repoRoot = resolve(__dirname, '../..');+const hookScript = join(+ repoRoot,+ 'understand-anything-plugin',+ 'hooks',+ 'post-tool-use-auto-update.mjs',+);++function runHook({+ command = 'git commit -m "test"',+ autoUpdate = true,+ createGraph = true,+} = {}) {+ const projectRoot = mkdtempSync(join(tmpdir(), 'ua-post-tool-use-'));+ const dataDir = join(projectRoot, '.understand-anything');+ mkdirSync(dataDir);+ writeFileSync(join(dataDir, 'config.json'), JSON.stringify({ autoUpdate }));+ if (createGraph) writeFileSync(join(dataDir, 'knowledge-graph.json'), '{}');++ const pluginRoot = join(projectRoot, 'plugin root');+ const result = spawnSync(process.execPath, [hookScript], {+ cwd: projectRoot,+ input: JSON.stringify({+ tool_name: 'Bash',+ tool_input: { command },+ }),+ env: { ...process.env, CLAUDE_PLUGIN_ROOT: pluginRoot },+ encoding: 'utf8',+ });++ rmSync(projectRoot, { recursive: true, force: true });+ return { ...result, pluginRoot };+}++describe('PostToolUse auto-update hook', () => {+ it('emits the auto-update instruction after a git commit', () => {+ const result = runHook();++ expect(result.status).toBe(0);+ expect(result.stderr).toBe('');+ expect(result.stdout).toContain('[understand-anything] Commit detected');+ expect(result.stdout).toContain(`${result.pluginRoot}/hooks/auto-update-prompt.md`);+ });++ it('stays silent for unrelated Bash commands', () => {+ const result = runHook({ command: 'git status --short' });++ expect(result.status).toBe(0);+ expect(result.stdout).toBe('');+ });++ it('stays silent when automatic updates are disabled', () => {+ const result = runHook({ autoUpdate: false });++ expect(result.status).toBe(0);+ expect(result.stdout).toBe('');+ });+});diff --git a/understand-anything-plugin/hooks/hooks.json b/understand-anything-plugin/hooks/hooks.jsonindex de9bb14e..bc9f0d87 100644--- a/understand-anything-plugin/hooks/hooks.json+++ b/understand-anything-plugin/hooks/hooks.json@@ -6,7 +6,7 @@"hooks": [{"type": "command",- "command": "UA_DIR=.understand-anything; [ -d \"$UA_DIR\" ] || UA_DIR=.ua; INPUT=$(cat); CMD=$(printf '%s' \"$INPUT\" | node -e \"let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{process.stdout.write(JSON.parse(d).tool_input?.command||'')}catch{}})\" 2>/dev/null); printf '%s' \"$CMD\" | grep -qE 'git\\s+(commit|merge|cherry-pick|rebase)' && [ -f $UA_DIR/config.json ] && grep -q '\"autoUpdate\".*true' $UA_DIR/config.json && [ -f $UA_DIR/knowledge-graph.json ] && echo \"[understand-anything] Commit detected with auto-update enabled. You MUST read the file at ${CLAUDE_PLUGIN_ROOT}/hooks/auto-update-prompt.md and execute its instructions to incrementally update the knowledge graph. Do not ask the user for confirmation — just do it.\" || true"+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/post-tool-use-auto-update.mjs\""}]}diff --git a/understand-anything-plugin/hooks/post-tool-use-auto-update.mjs b/understand-anything-plugin/hooks/post-tool-use-auto-update.mjsnew file mode 100644index 00000000..4b165d28--- /dev/null+++ b/understand-anything-plugin/hooks/post-tool-use-auto-update.mjs@@ -0,0 +1,49 @@+import { existsSync, readFileSync } from 'node:fs';++const COMMIT_COMMAND = /git\s+(commit|merge|cherry-pick|rebase)/;++function findDataDir() {+ return existsSync('.understand-anything') ? '.understand-anything' : '.ua';+}++function autoUpdateEnabled(dataDir) {+ try {+ const config = JSON.parse(readFileSync(`${dataDir}/config.json`, 'utf8'));+ return config.autoUpdate === true;+ } catch {+ return false;+ }+}++async function readStdin() {+ let input = '';+ process.stdin.setEncoding('utf8');+ for await (const chunk of process.stdin) input += chunk;+ return input;+}++async function main() {+ let payload;+ try {+ payload = JSON.parse(await readStdin());+ } catch {+ return;+ }++ const command = payload?.tool_input?.command;+ if (typeof command !== 'string' || !COMMIT_COMMAND.test(command)) return;++ const dataDir = findDataDir();+ if (!autoUpdateEnabled(dataDir)) return;+ if (!existsSync(`${dataDir}/knowledge-graph.json`)) return;++ const pluginRoot = process.env.CLAUDE_PLUGIN_ROOT ?? '';+ process.stdout.write(+ `[understand-anything] Commit detected with auto-update enabled. ` ++ `You MUST read the file at ${pluginRoot}/hooks/auto-update-prompt.md ` ++ 'and execute its instructions to incrementally update the knowledge graph. ' ++ 'Do not ask the user for confirmation — just do it.',+ );+}++await main();