|
| 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 | +**Generic defaults:** This rule is **project-agnostic**. After you vendor or symlink it (or use a future `codemap` CLI that writes agent files), **edit your copy** to add app-specific triggers and SQL — upstream text is only a baseline. |
| 12 | + |
| 13 | +## CLI (this repo vs installed package) |
| 14 | + |
| 15 | +| Context | Incremental index | Query | |
| 16 | +| ------- | ----------------- | ----- | |
| 17 | +| Developing in this repo | `bun src/index.ts` | `bun src/index.ts query "<SQL>"` | |
| 18 | +| After `bun link` / global `codemap` | `codemap` | `codemap query "<SQL>"` | |
| 19 | +| Published package (when available) | `bunx @stainless-code/codemap` | `bunx @stainless-code/codemap query "<SQL>"` | |
| 20 | + |
| 21 | +Index another project: **`--root /path/to/repo`**, or set **`CODEMAP_ROOT`** or **`CODEMAP_TEST_BENCH`** (e.g. in **`.env`** in this repo — see [docs/bench-repo.md](../../docs/bench-repo.md)). Full rebuild: **`--full`**. Targeted re-index: **`--files path/to/a.ts path/to/b.tsx`**. |
| 22 | + |
| 23 | +## Session start (do this ONCE per conversation) |
| 24 | + |
| 25 | +Run incremental indexing to catch changes made outside this session: |
| 26 | + |
| 27 | +```bash |
| 28 | +bun src/index.ts |
| 29 | +``` |
| 30 | + |
| 31 | +## Pre-flight check (do this EVERY time before searching) |
| 32 | + |
| 33 | +1. **Does the question match a trigger pattern below?** |
| 34 | +2. **If yes → query the index.** Do NOT use Grep/Glob/SemanticSearch/Read. |
| 35 | +3. **If the index result is incomplete → THEN fall back** to other tools, but always try the index first. |
| 36 | + |
| 37 | +Violating this order is wrong even if you get the right answer — it wastes time and ignores the tool purpose-built for this. |
| 38 | + |
| 39 | +## Trigger patterns |
| 40 | + |
| 41 | +If the question looks like any of these → use the index: |
| 42 | + |
| 43 | +| Question shape | Table(s) | |
| 44 | +| --- | --- | |
| 45 | +| "What/which files import X?" | `imports` (by `source`) or `dependencies` (by `to_path`) | |
| 46 | +| "Where is X defined?" | `symbols` | |
| 47 | +| "What does file X export?" | `exports` | |
| 48 | +| "What hooks does component X use?" / "List React components" | `components` | |
| 49 | +| "What are the CSS variables/tokens for X?" | `css_variables` | |
| 50 | +| "Find all TODOs/FIXMEs" | `markers` | |
| 51 | +| "Who depends on file X?" / "What does file X depend on?" | `dependencies` | |
| 52 | +| "How many files/symbols/components are there?" | any table with `COUNT(*)` | |
| 53 | +| "What are the CSS classes in X?" | `css_classes` | |
| 54 | +| "What keyframe animations exist?" | `css_keyframes` | |
| 55 | + |
| 56 | +## When Grep / Read IS appropriate |
| 57 | + |
| 58 | +- Reading implementation details you need to edit |
| 59 | +- Reviewing logic, control flow, or business rules |
| 60 | +- Searching for patterns the index doesn't capture (string literals, inline logic, etc.) |
| 61 | + |
| 62 | +## How to query |
| 63 | + |
| 64 | +```bash |
| 65 | +bun src/index.ts query "<SQL>" |
| 66 | +``` |
| 67 | + |
| 68 | +## Quick reference queries |
| 69 | + |
| 70 | +| I need to... | Query | |
| 71 | +| --- | --- | |
| 72 | +| Find a symbol | `SELECT name, kind, file_path, line_start, line_end, signature FROM symbols WHERE name = '...'` | |
| 73 | +| Find a symbol (fuzzy) | `SELECT name, kind, file_path, line_start FROM symbols WHERE name LIKE '%...%'` | |
| 74 | +| See file exports | `SELECT name, kind, is_default FROM exports WHERE file_path LIKE '%...'` | |
| 75 | +| See file imports | `SELECT source, specifiers, is_type_only FROM imports WHERE file_path LIKE '%...'` | |
| 76 | +| Who imports this module? | `SELECT DISTINCT file_path FROM imports WHERE source LIKE '~/some/module%'` | |
| 77 | +| Who imports this file? | `SELECT DISTINCT from_path FROM dependencies WHERE to_path LIKE '%...'` | |
| 78 | +| What does this depend on? | `SELECT DISTINCT to_path FROM dependencies WHERE from_path LIKE '%...'` | |
| 79 | +| Component info | `SELECT name, props_type, hooks_used FROM components WHERE name = '...'` | |
| 80 | +| All components | `SELECT name, file_path, props_type, hooks_used FROM components ORDER BY name` | |
| 81 | +| TODOs in a file | `SELECT line_number, content FROM markers WHERE file_path LIKE '%...' AND kind = 'TODO'` | |
| 82 | +| Most complex files | `SELECT from_path, COUNT(*) as deps FROM dependencies GROUP BY from_path ORDER BY deps DESC LIMIT 10` | |
| 83 | +| CSS design tokens | `SELECT name, value, scope FROM css_variables WHERE name LIKE '--%...'` | |
| 84 | +| CSS module classes | `SELECT name, file_path FROM css_classes WHERE is_module = 1` | |
| 85 | +| CSS keyframes | `SELECT name, file_path FROM css_keyframes` | |
| 86 | + |
| 87 | +**Use `DISTINCT`** on dependency and import queries — a file importing multiple specifiers from the same module produces duplicate rows. |
| 88 | + |
| 89 | +For the full schema, advanced query patterns, and troubleshooting, read the skill at `.agents/skills/codemap/SKILL.md`. |
| 90 | + |
| 91 | +## Keeping it fresh |
| 92 | + |
| 93 | +**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. |
| 94 | + |
| 95 | +```bash |
| 96 | +# Targeted — re-index only the files you just touched |
| 97 | +bun src/index.ts --files path/to/file1.tsx path/to/file2.ts |
| 98 | + |
| 99 | +# Incremental — auto-detects changed files via git |
| 100 | +bun src/index.ts |
| 101 | + |
| 102 | +# Full rebuild — after rebase, branch switch, or stale index |
| 103 | +bun src/index.ts --full |
| 104 | +``` |
| 105 | + |
| 106 | +### When to re-index |
| 107 | + |
| 108 | +- **After editing files** — use `--files` with the paths you modified (fastest). Deleted files are auto-detected and removed from the index |
| 109 | +- **After switching branches or rebasing** — run `--full` |
| 110 | +- **When unsure which files changed** — run without flags to auto-detect via git |
0 commit comments