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.
Auto-Update Knowledge Graph (Internal — Hook-Triggered)
Incrementally update the knowledge graph after a commit. Do not ask the user for confirmation.
Cost rule: deletion-only, ignored-only, generated-artifact-only, and cosmetic-only updates dispatch no LLM agent. Local structural updates dispatch file-analyzer only for the current files that actually changed structurally. Whole-graph architecture/tour agents run only when the prepared plan requests them.
Phase 0 — Pre-flight and deterministic preparation
Set
PROJECT_ROOTto the current working directory. Resolve the data directory once:UA_DIR="$PROJECT_ROOT/$([ -d "$PROJECT_ROOT/.understand-anything" ] && echo .understand-anything || echo .ua)"Require
$UA_DIR/knowledge-graph.jsonand$UA_DIR/meta.json. If either is missing, report that/understandmust create a baseline and STOP.Read
gitCommitHashfrom meta as$LAST_COMMIT_HASH; get$HEAD_COMMITwithgit rev-parse HEAD. If they match, report that the graph is current and STOP.Resolve
$PLUGIN_ROOTfrom$CLAUDE_PLUGIN_ROOT, then$DEVIN_PLUGIN_ROOT, then the parent of thehooks/directory containing this prompt file (the path the hook told you to read — this covers Devin plugin installs, whose agent shell has no plugin-root variable), then$HOME/.understand-anything-plugin, validating that it containsskills/understand/prepare-incremental.mjs. If it cannot be found, report the error and STOP without changing metadata.Ensure core is built, then run the bundled helper with parameterized arguments:
node "$PLUGIN_ROOT/skills/understand/prepare-incremental.mjs" \ "$PROJECT_ROOT" \ "$LAST_COMMIT_HASH"The helper is authoritative for
git diff --name-status -z, renames, spaces, POSIX normalization, the current scan/ignore rules, structural fingerprints, generated artifacts, and selective import-map refresh. Do not recreate or post-filter its decisions.Read
$UA_DIR/intermediate/incremental-plan.json.Action Required behavior SKIPRun the finalizer below. It advances scan/fingerprint/meta for safe cosmetic or irrelevant changes and intentionally advances nothing for generated-only commits. Report zero tokens spent and STOP. PARTIAL_UPDATEContinue with targeted analysis. ARCHITECTURE_UPDATEContinue with targeted analysis, then rerun architecture and tour. FULL_UPDATEImmediately invoke /understand --full; do not patch the incremental baseline.Resolve the stored output language. Read
$UA_DIR/config.json; if it has anoutputLanguagefield, construct$LANGUAGE_DIRECTIVEfrom it using the exact template in/understand's language configuration step (the "Language directive template"). This hook has no user interaction, so the stored preference is the only source — never re-detect. Ifconfig.jsonis missing or has nooutputLanguage, leave$LANGUAGE_DIRECTIVEunset and omit it from the lesson dispatch in Phase 3 (the writer treats the directive as optional).SKIP finalizer:
node "$PLUGIN_ROOT/skills/understand/finalize-incremental.mjs" "$PROJECT_ROOT"
Phase 1 — Targeted file analysis
Read filesToReanalyze from the plan. It never contains deleted, ignored, cosmetic, or generated-artifact paths.
If the list is empty, dispatch no agent and create no new batch file. Continue directly to merge;
batch-existing.jsonalready contains the pruned graph. This covers deletion-only updates at zero token cost.Otherwise compute only changed batches:
node "$PLUGIN_ROOT/skills/understand/compute-batches.mjs" "$PROJECT_ROOT" \ --changed-files="$UA_DIR/intermediate/changed-files.json"Dispatch file-analyzer for those batches only, using
agents/file-analyzer.mdand the batch prompt contract from the/understandskill. IncludepreviousSymbolsfor each batch's files fromincremental-symbol-baseline.json: old symbol IDs, names, types, paths, line ranges, and class containment. Existing symbols that still exist must survive significance filtering. Preserve each original batch index in its output filename. Retry a failed dispatch once; if it still fails, STOP without running the finalizer or advancing the baseline.
Phase 2 — Merge
Run the deterministic merge in both empty and non-empty analyzer cases:
python "$PLUGIN_ROOT/skills/understand/merge-batch-graphs.py" "$PROJECT_ROOT"
It combines batch-existing.json with fresh batches and recovers imports from the refreshed scan-result.json, including config:, schema:, service:, and other valid whole-file nodes. Require both a successful exit and $UA_DIR/intermediate/assembled-graph.json; failed candidates remain on disk for diagnosis.
Merge runs validate-incremental-symbols.mjs, comparing the preserved old symbols with the candidate and base/current source. Read incremental-symbol-report.json for per-file counts, missing IDs/names, and still-present/deleted/unknown classifications. A same-size node set can still have missing symbols. Confirmed deletions are allowed; still-present or unknown omissions block publication.
Merge also preserves normalized dangling edge candidates from fresh batches in incremental-edge-candidates.json before discarding unresolved endpoints. Every successful validation reconciles both endpoint IDs, including first-pass updates without a retry and repairs where omitted symbols return. This evidence excludes batch-existing.json and is cleared by fresh preparation.
Current endpoint descriptors take precedence over baseline aliases when IDs are reused. Repair defers incoming edges outside the retained batch until their original HEAD identities are matched, preventing a reused ID from attaching an old current-analysis reference to a different symbol.
If the report has unresolvedFiles, prepare one targeted symbol retry:
node "$PLUGIN_ROOT/skills/understand/prepare-symbol-retry.mjs" "$PROJECT_ROOT"
Dispatch only batches[] from incremental-symbol-retry.json, with the usual file-analyzer prompt and its supplied files, batchIndex, batchImportData, neighborMap, previousSymbols, and missingSymbols. Reanalyze those files completely. The helper preserves other merged results in batch-0.json, removes affected nodes and outgoing edges, and clears old numeric shards before repair. Current inbound edges from other files remain candidates until merge reconciles their replacement targets and drops dangling references. Rerun merge after this one repair. Never paste old nodes or semantic edges back into the candidate. The attempt is recorded for the base/head commits, even across repeated prepare calls.
If the helper, repair dispatch, or second merge fails, STOP with diagnostics and leave knowledge-graph.json, fingerprints.json, and meta.json unchanged. Other merge failures without eligible unresolved files also stop immediately.
Languages without a deterministic structural parser (including .sh, .ps1, and .bat) cannot have missing symbols automatically confirmed as deleted. Callables without explicit class containment also require source verification when their IDs/names are unchanged, regardless of whether class nodes were emitted; unverified identities block publication. These cases remain unknown and require manual investigation or parser support. Do not use supplemental LLM inspection or regex guesses to waive the gate.
Do not dispatch assemble-reviewer or graph-reviewer for automatic updates.
Phase 3 — Conditional architecture and tour
- For
PARTIAL_UPDATE, dispatch neither agent. The finalizer preserves existing layer assignments and tour prose, removes dangling references/empty layers, and assigns new nodes deterministically by directory depth, graph connectivity, then prior layer order. - For
ARCHITECTURE_UPDATE, dispatch architecture-analyzer on the full merged node/edge set, including previous layers for naming stability, and write$UA_DIR/intermediate/layers.json. Then dispatch tour-builder and write$UA_DIR/intermediate/tour.json. Then normalize the tour deterministically — runnode "$PLUGIN_ROOT/skills/understand/normalize-tour.mjs" "$PROJECT_ROOT"and use the resulting$UA_DIR/intermediate/tour.normalized.jsonfor the lesson dispatch (the finalizer applies the same normalization at save time; hand-written conversion disagrees with it). Before dispatching the lesson writer, delete any stale lesson scratch files left by an interrupted earlier run —$UA_DIR/intermediate/code-lessons.json,code-lessons.detailed.<N>.json, andcode-lessons.normalized.json(never touch the served$UA_DIR/code-lessons.json). Then dispatchcode-lesson-writerfollowing the SKILL.md §Code Lessons contract (rebuilt tour, detailed files in bounded batches, language directive) and write the batch outputs, normalized into$UA_DIR/intermediate/code-lessons.normalized.jsonper the SKILL.md Phase 5 rules. Append$LANGUAGE_DIRECTIVEto each dispatch prompt when Phase 0 resolved one — without it, a project stored with a non-EnglishoutputLanguagewould get its lessons regenerated in English beside the non-English graph and tour. Lessons are best-effort here: retry a failed batch once, and if any batch still fails leave no staged document, record the warning, and continue — publication (Phase 4) then removes the stale served sidecar instead of leaving lessons for the previous tour.
Retry either required agent once. If it still fails, STOP without advancing the baseline.
Phase 4 — Atomic save and baseline patch
Run:
node "$PLUGIN_ROOT/skills/understand/finalize-incremental.mjs" "$PROJECT_ROOT"
The finalizer performs deterministic graph/layer/tour validation and independently reruns the shared symbol check on the exact graph before writing. It atomically saves knowledge-graph.json, patches changed fingerprints while preserving untouched entries, removes deleted fingerprints, and only then writes meta.json. A graph-save failure must never advance the fingerprint or commit baseline. If symbol loss is first detected here, follow the same one-retry procedure, rerun merge and any required architecture/tour phases, then finalize; an already-used or unsuccessful repair must stop with the old graph and baselines intact.
After the finalizer exits successfully, publish lessons with the same rules as Lesson publication in SKILL.md's incremental save gate — only lessons staged by the current invocation: atomically move $UA_DIR/intermediate/code-lessons.normalized.json over $UA_DIR/code-lessons.json when this run staged it; when lessons were attempted but skipped or failed, delete a served $UA_DIR/code-lessons.json instead; when the finalizer failed, touch nothing so the previous graph and lessons stay consistent. PARTIAL_UPDATE never attempts lessons, but incremental updates can still delete or newly ignore files from the graph while their source-derived lessons survive — so for every action except SKIP (and PARTIAL_UPDATE in particular), after the finalizer succeeds and the publication rules above have been applied, run:
node "$PLUGIN_ROOT/skills/understand/prune-code-lessons.mjs" "$PROJECT_ROOT"
The helper removes only entries whose file path has no node in the saved graph and leaves every surviving lesson's content untouched (the file is rewritten as normalized 2-space JSON). If the prune itself fails, report the warning and continue — the saved graph is already final and the idempotent helper can prune the orphans again on the next run; never roll the graph back for it.
Report:
- action taken;
- files structurally analyzed;
- deleted/ignored/cosmetic/generated counts from the plan;
- whether architecture/tour ran;
- output path.
Error handling
- Never fall back to a hand-written regex fingerprint script or an extension-only changed-file filter.
- Never pass a deleted or ignored path to file-analyzer.
- Never update
meta.jsonafter a failed graph/agent phase. - Never update
meta.jsonfor a generated-artifact-only commit. - Surface every warning or failure; a subsequent run can safely retry because the helper reconstructs the previous inventory from scan results, graph node
filePathvalues, and fingerprints.