|
| 1 | +--- |
| 2 | +alwaysApply: true |
| 3 | +--- |
| 4 | + |
| 5 | +# Codemap (structural codebase index) |
| 6 | + |
| 7 | +> **STOP.** Before you call Grep, Glob, SemanticSearch, or Read to answer a **structural** question about this repository — query the Codemap SQLite index first. This is not optional when the question matches a trigger pattern below. |
| 8 | +
|
| 9 | +A local database (default **`.codemap.db`**) indexes structure: symbols, imports, exports, components, dependencies, markers, CSS variables, CSS classes, CSS keyframes. |
| 10 | + |
| 11 | +**This file** is for **developing Codemap** in this clone. **End users** of the published package get the agent rule from **`templates/agents/`** (via **`codemap agents init`**). **Generic defaults:** SQL and triggers stay project-agnostic — **edit** this rule for repo-specific paths and queries. |
| 12 | + |
| 13 | +## CLI (this repository) |
| 14 | + |
| 15 | +| Context | Incremental index | Query | |
| 16 | +| ------------------------------ | ------------------ | ------------------------------------------------------------------------------------------ | |
| 17 | +| **Default** — from this clone | `bun src/index.ts` | `bun src/index.ts query --json "<SQL>"` | |
| 18 | +| Same entry | `bun run dev` | (same as first row) | |
| 19 | +| Query (ASCII table — optional) | — | `bun src/index.ts query "<SQL>"` | |
| 20 | +| Recipe | — | `bun src/index.ts query --json --recipe fan-out` (see **`bun src/index.ts query --help`**) | |
| 21 | +| Recipe catalog / SQL | — | `bun src/index.ts query --recipes-json` · `bun src/index.ts query --print-sql fan-out` | |
| 22 | + |
| 23 | +After **`bun run build`**, **`node dist/index.mjs`** matches the published **`codemap`** binary (same flags). **`bun link`** / global **`codemap`** also work when testing the packaged CLI. |
| 24 | + |
| 25 | +Index another project: **`--root /path/to/repo`**, or set **`CODEMAP_ROOT`** or **`CODEMAP_TEST_BENCH`** (e.g. in **`.env`** — see [docs/benchmark.md § Indexing another project](../../docs/benchmark.md#indexing-another-project)). Full rebuild: **`--full`**. Targeted re-index: **`--files path/to/a.ts path/to/b.tsx`**. |
| 26 | + |
| 27 | +## Session start (do this ONCE per conversation) |
| 28 | + |
| 29 | +Run incremental indexing to catch changes made outside this session: |
| 30 | + |
| 31 | +```bash |
| 32 | +bun src/index.ts |
| 33 | +``` |
| 34 | + |
| 35 | +## Pre-flight check (do this EVERY time before searching) |
| 36 | + |
| 37 | +1. **Does the question match a trigger pattern below?** |
| 38 | +2. **If yes → query the index.** Do NOT use Grep/Glob/SemanticSearch/Read. |
| 39 | +3. **If the index result is incomplete → THEN fall back** to other tools, but always try the index first. |
| 40 | + |
| 41 | +Violating this order is wrong even if you get the right answer — it wastes time and ignores the tool purpose-built for this. |
| 42 | + |
| 43 | +## Trigger patterns |
| 44 | + |
| 45 | +If the question looks like any of these → use the index: |
| 46 | + |
| 47 | +| Question shape | Table(s) | |
| 48 | +| ------------------------------------------------------------ | -------------------------------------------------------- | |
| 49 | +| "What/which files import X?" | `imports` (by `source`) or `dependencies` (by `to_path`) | |
| 50 | +| "Where is X defined?" | `symbols` | |
| 51 | +| "What does file X export?" | `exports` | |
| 52 | +| "What hooks does component X use?" / "List React components" | `components` | |
| 53 | +| "What are the CSS variables/tokens for X?" | `css_variables` | |
| 54 | +| "Find all TODOs/FIXMEs" | `markers` | |
| 55 | +| "Who depends on file X?" / "What does file X depend on?" | `dependencies` | |
| 56 | +| "How many files/symbols/components are there?" | any table with `COUNT(*)` | |
| 57 | +| "What are the CSS classes in X?" | `css_classes` | |
| 58 | +| "What keyframe animations exist?" | `css_keyframes` | |
| 59 | +| "What fields does interface/type X have?" | `type_members` | |
| 60 | +| "Is symbol X deprecated?" / "What does X do?" | `symbols` (`doc_comment`) | |
| 61 | +| "Who calls X?" / "What does X call?" | `calls` | |
| 62 | + |
| 63 | +## When Grep / Read IS appropriate |
| 64 | + |
| 65 | +- Reading implementation details you need to edit |
| 66 | +- Reviewing logic, control flow, or business rules |
| 67 | +- Searching for patterns the index doesn't capture (string literals, inline logic, etc.) |
| 68 | + |
| 69 | +## How to query |
| 70 | + |
| 71 | +```bash |
| 72 | +bun src/index.ts query --json "<SQL>" |
| 73 | +``` |
| 74 | + |
| 75 | +**Human-readable:** Omit **`--json`** to print **`console.table`** to the terminal (optional; wide results use more lines and bytes than JSON). |
| 76 | + |
| 77 | +**Row count:** The CLI does **not** impose a maximum number of rows. Add **`LIMIT`** (and **`ORDER BY`**) in SQL when you need a bounded list. With **`--json`**, stdout is a JSON array on success; **on failure**, stdout is **`{"error":"<message>"}`** and the process exits **1** (invalid SQL, database open errors, or **`query` bootstrap** failures such as config/resolver — not only SQL runtime errors). The CLI sets **`process.exitCode`** instead of **`process.exit`** so piped stdout is not truncated. |
| 78 | + |
| 79 | +**Verbatim answers:** When the user asks for lists, counts, or enumerated structural data from the index, **paste or summarize from the query output without inventing rows** — do not substitute a prose “summary” that omits rows the user asked to see. Use **`--json`** so the full result set is unambiguous. |
| 80 | + |
| 81 | +## Quick reference queries |
| 82 | + |
| 83 | +| I need to... | Query | |
| 84 | +| ------------------------- | ------------------------------------------------------------------------------------------------------------ | |
| 85 | +| Find a symbol | `SELECT name, kind, file_path, line_start, line_end, signature FROM symbols WHERE name = '...'` | |
| 86 | +| Find a symbol (fuzzy) | `SELECT name, kind, file_path, line_start FROM symbols WHERE name LIKE '%...%'` | |
| 87 | +| See file exports | `SELECT name, kind, is_default FROM exports WHERE file_path LIKE '%...'` | |
| 88 | +| See file imports | `SELECT source, specifiers, is_type_only FROM imports WHERE file_path LIKE '%...'` | |
| 89 | +| Who imports this module? | `SELECT DISTINCT file_path FROM imports WHERE source LIKE '~/some/module%'` | |
| 90 | +| Who imports this file? | `SELECT DISTINCT from_path FROM dependencies WHERE to_path LIKE '%...'` | |
| 91 | +| What does this depend on? | `SELECT DISTINCT to_path FROM dependencies WHERE from_path LIKE '%...'` | |
| 92 | +| Component info | `SELECT name, props_type, hooks_used FROM components WHERE name = '...'` | |
| 93 | +| All components | `SELECT name, file_path, props_type, hooks_used FROM components ORDER BY name` | |
| 94 | +| TODOs in a file | `SELECT line_number, content FROM markers WHERE file_path LIKE '%...' AND kind = 'TODO'` | |
| 95 | +| Most complex files | `SELECT from_path, COUNT(*) as deps FROM dependencies GROUP BY from_path ORDER BY deps DESC LIMIT 10` | |
| 96 | +| CSS design tokens | `SELECT name, value, scope FROM css_variables WHERE name LIKE '--%...'` | |
| 97 | +| CSS module classes | `SELECT name, file_path FROM css_classes WHERE is_module = 1` | |
| 98 | +| CSS keyframes | `SELECT name, file_path FROM css_keyframes` | |
| 99 | +| Type/interface shape | `SELECT name, type, is_optional, is_readonly FROM type_members WHERE symbol_name = '...'` | |
| 100 | +| Deprecated symbols | `SELECT name, kind, file_path, doc_comment FROM symbols WHERE doc_comment LIKE '%@deprecated%'` | |
| 101 | +| Symbol docs | `SELECT name, signature, doc_comment FROM symbols WHERE name = '...' AND doc_comment IS NOT NULL` | |
| 102 | +| Const values | `SELECT name, value, file_path FROM symbols WHERE kind = 'const' AND value IS NOT NULL AND name LIKE '%...'` | |
| 103 | +| Class members | `SELECT name, kind, signature FROM symbols WHERE parent_name = '...'` | |
| 104 | +| Top-level only | `SELECT name, kind, signature FROM symbols WHERE parent_name IS NULL AND file_path LIKE '%...'` | |
| 105 | +| Who calls X? | `SELECT DISTINCT caller_name, file_path FROM calls WHERE callee_name = '...'` | |
| 106 | +| What does X call? | `SELECT DISTINCT callee_name FROM calls WHERE caller_name = '...'` | |
| 107 | +| Call hotspots | `SELECT callee_name, COUNT(*) as fan_in FROM calls GROUP BY callee_name ORDER BY fan_in DESC LIMIT 10` | |
| 108 | + |
| 109 | +**Use `DISTINCT`** on dependency and import queries — a file importing multiple specifiers from the same module produces duplicate rows. |
| 110 | + |
| 111 | +For the full schema, advanced query patterns, and troubleshooting, read the skill at `.agents/skills/codemap/SKILL.md`. |
| 112 | + |
| 113 | +## Keeping it fresh |
| 114 | + |
| 115 | +**After completing a step that modified source files, re-index before making any further queries.** The index only reflects the state at the time it was last built — edits you just made won't appear until you re-index. |
| 116 | + |
| 117 | +```bash |
| 118 | +# Targeted — re-index only the files you just touched |
| 119 | +bun src/index.ts --files path/to/file1.tsx path/to/file2.ts |
| 120 | + |
| 121 | +# Incremental — auto-detects changed files via git |
| 122 | +bun src/index.ts |
| 123 | + |
| 124 | +# Full rebuild — after rebase, branch switch, or stale index |
| 125 | +bun src/index.ts --full |
| 126 | +``` |
| 127 | + |
| 128 | +### When to re-index |
| 129 | + |
| 130 | +- **After editing files** — use `--files` with the paths you modified (fastest). Deleted files are auto-detected and removed from the index |
| 131 | +- **After switching branches or rebasing** — run `--full` |
| 132 | +- **When unsure which files changed** — run without flags to auto-detect via git |
0 commit comments