Lines 1-47javascript
2// SPDX-License-Identifier: MIT
3// ============================================================================
4// agentmap — the repo map your coding agent is *forced* to use.
6// A ts-morph code-relationship map for TypeScript/JavaScript repos. Unlike
7// one-shot "pack the repo into a prompt" tools, this is a QUERYABLE, RANKED
8// map: PageRank importance (approach from Aider's repo map), Aider-style
9// symbol ranking, a token-budgeted `--map` digest, and a single `--any`
10// router (file → symbol → feature → live git-grep) — wired into the agent
11// loop via a post-commit auto-refresh + a PreToolUse hook.
13// Near-zero deps (ts-morph only). Runs in the target repo's cwd.
14// Algorithm credit: Aider's repo map (Apache-2.0) — github.com/Aider-AI/aider
15// ============================================================================
16import { writeFileSync, readFileSync, existsSync, mkdirSync, renameSync, readdirSync, lstatSync, chmodSync } from "node:fs";
17import { execSync, execFileSync } from "node:child_process";
18import { createHash } from "node:crypto";
LowWeak Crypto
Package source references weak cryptographic algorithms.
agentmap.mjsView on unpkg · L16 19import { createRequire } from "node:module";
20import { homedir } from "node:os";
21import { fileURLToPath } from "node:url";
22import { join, dirname } from "node:path";
24// Lazy ts-morph: its ~105ms module init only fires on a COLD rebuild. Warm cache
25// queries (the common case) never construct a Project, so they skip the load
26// entirely (~2x faster warm). createRequire keeps it synchronous — no async to
27// thread through build()/makeProject().
28const _require = createRequire(import.meta.url);
29let _tsm = null;
MediumDynamic Require
Package source references dynamic require/import behavior.
agentmap.mjsView on unpkg · L27 30const tsMorph = () => (_tsm ??= _require("ts-morph"));
32const MAP = ".claude/agentmap/map.json";
33const MAP_LEGACY = ".claude/agentmap.json"; // pre-namespacing path; read for migration
34const MAP_DIRTY = ".claude/agentmap/map.dirty.json"; // dirty-tree build cache, keyed by dirtyFingerprint (Batch 3 Tier 1)
35const FACTS = ".claude/agentmap/facts.json"; // raw per-file facts snapshot for incremental rebuild (Batch 3 Tier 2)
36// Bumped 2 → 3: Vue SFC support. `.vue` files now appear in the map and the
37// source-discovery / freshness checks treat them as first-class source files.
38// Old caches (schema 2) are ignored so the first run after upgrade rebuilds.
39const SCHEMA_VERSION = 3;
41// ---------------------------------------------------------------------------
42// Tuning constants — KEEP THESE VALUES IDENTICAL (output + marketing must not
43// shift). Hoisted out of inline literals so the algorithm is self-documenting.
44// ---------------------------------------------------------------------------
45const DAMPING = 0.85; // PageRank damping (Aider parity)
46const TOL = 1e-6; // power-iteration convergence tolerance
47const MAX_ITER = 100; // power-iteration iteration cap