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// --- .agentmapignore + .d.ts default-exclude (config-file / flag scoping) ------
42// The map-cache path used when --include-dts is set. Kept SEPARATE from map.json
43// so the two modes never collide: the default (`.d.ts` excluded) map.json — the
44// one the post-commit hook writes and every normal query reads — stays untouched
45// and byte-identical, while --include-dts builds/reads its own cache.
46const MAP_DTS = ".claude/agentmap/map.dts.json"; // --include-dts full-build cache
47const AGENTMAPIGNORE = ".agentmapignore"; // repo-root ignore file (gitignore-ish)