Codebase Index - 3-Layer Memory Optimizer
Build and maintain a progressive disclosure index that prevents agents from reading entire files when they only need a function signature or a specific line range.
When to Activate
- During
/crew init (full build)
- During
/crew reindex (full rebuild)
- When an agent needs to find code but the index exists (consult mode)
- After file edits (incremental update mode via index-updater skill)
Three-Layer Architecture
Layer 1 - Compact Index (.claude/crew-index.json, ~2-5KB)
The cheapest layer. Costs ~50 tokens to read. Contains just enough to route queries to the right file:
Per-file entry:
hash: SHA-256 of file content (for change detection)
size: file size in bytes
type: language (javascript, python, typescript, etc.)
exports: exported functions/classes/constants
imports: external dependencies
functions: all function/method names
classes: all class names
lines: total line count
category: core | test | config | docs | util
summary: 1-line description of what this file does
Global metadata:
version: schema version
generated: ISO timestamp
contentHash: hash of all file hashes combined
dependencies: package dependencies with versions
architecture.entryPoint: main entry file
architecture.pipeline: data flow description
architecture.patterns: detected patterns (MVC, pipeline, event-driven, etc.)
stats: totalFiles, totalLines, languages, lastIndexed
Layer 2 - Symbol Map (.claude/crew-symbols.json, ~5-15KB)
Mid-detail layer. Costs ~100-200 tokens for relevant section. Contains function signatures and relationships:
Per-symbol entry (keyed as filepath::symbolName):
type: function | class | method | constant | type
signature: full signature string
params: parameter list with types if available
returns: return type if available
calls: what this symbol calls
calledBy: what calls this symbol
lineRange: [startLine, endLine]
description: 1-line description
Relationship maps:
callGraph: which functions call which
fileRelationships: import/export connections between files
Layer 3 - Full Details (on-demand file reads)
Agents read actual file contents ONLY after consulting Layer 1+2 to know exactly which file and line range they need. Use the Read tool with offset and limit parameters to read specific line ranges.
Building the Index (Full Build)
When triggered for full build:
Step 1: Discover source files
Use Glob to find all source files:
- **/*.{js,ts,jsx,tsx,py,go,rs,java,kt,rb,php,c,cpp,h,cs}
- Exclude: node_modules, .git, dist, build, __pycache__, .next, vendor
- Also find: package.json, requirements.txt, go.mod, Cargo.toml, pyproject.toml
Step 2: Read each file and extract metadata
For each source file:
- Read the file content
- Compute SHA-256 hash (use
shasum -a 256 via Bash)
- Extract exports (look for
module.exports, export, def, func, fn, public)
- Extract imports (look for
require, import, from, use)
- Extract function names and signatures with line ranges
- Extract class names
- Count lines
- Categorize: core (src/), test (test/), config (root configs), docs, util (lib/, utils/)
- Generate 1-line summary
Step 3: Build call graph
From the imports/exports and function calls data:
- Map which files import from which
- Map which functions call which (by scanning function bodies for calls to known symbols)
- Identify entry points (files not imported by anything)
Step 4: Write index files
- Write
.claude/crew-index.json (Layer 1)
- Write
.claude/crew-symbols.json (Layer 2)
- Report stats: files indexed, total lines, languages detected
Incremental Update
When a single file changes:
- Recompute its hash
- Compare with stored hash in crew-index.json
- If different: re-read the file, update its Layer 1 + Layer 2 entries
- Update the global contentHash
- Do NOT rebuild entries for unchanged files
How Agents Should Use the Index
INDEX-FIRST PROTOCOL (mandatory):
Before reading ANY source file:
1. Read .claude/crew-index.json → find which file(s) are relevant
- Match by: function names, exports, summary keywords, category
2. Read .claude/crew-symbols.json → find exact symbol and line range
- Match by: symbol name, signature, calledBy/calls relationships
3. Read ONLY the specific line range you need from the actual file
- Use: Read tool with offset=startLine and limit=(endLine-startLine+1)
NEVER:
- Read an entire file when you only need one function
- Grep the whole codebase when the index has the answer
- Skip the index because "it's faster to just read the file"
ALWAYS:
- Consult Layer 1 first (cheapest)
- Consult Layer 2 only if Layer 1 isn't specific enough
- Read Layer 3 (actual file) only for the exact lines you need
- After making changes, note which files were modified for index update
Token Savings
| Approach |
Tokens Used |
When |
| Read all files |
~2000-10000+ |
Without index |
| Layer 1 only |
~50-100 |
Finding which file has a feature |
| Layer 1 + Layer 2 |
~150-300 |
Finding a specific function |
| Layer 1 + 2 + targeted read |
~300-500 |
Reading + editing a function |
| Savings |
5-10x |
Per agent interaction |
Edge Cases
- New file not in index: If an agent creates a new file, add it to the index immediately
- Deleted file still in index: On reindex, remove entries for files that no longer exist
- Binary/large files: Skip files > 100KB or binary files (images, compiled assets)
- Generated files: Skip dist/, build/, .next/ directories
- Config files: Index but categorize as "config" — lower priority for code searches
1---2name: codebase-index3description: 3-layer codebase indexing system for token-efficient code navigation. Builds compact index + symbol map so agents read targeted lines instead of entire files. Achieves ~5-10x token savings.4---56# Codebase Index - 3-Layer Memory Optimizer78Build and maintain a progressive disclosure index that prevents agents from reading entire files when they only need a function signature or a specific line range.910## When to Activate1112- During `/crew init` (full build)13- During `/crew reindex` (full rebuild)14- When an agent needs to find code but the index exists (consult mode)15- After file edits (incremental update mode via index-updater skill)1617## Three-Layer Architecture1819### Layer 1 - Compact Index (`.claude/crew-index.json`, ~2-5KB)2021The cheapest layer. Costs ~50 tokens to read. Contains just enough to route queries to the right file:2223**Per-file entry:**24- `hash`: SHA-256 of file content (for change detection)25- `size`: file size in bytes26- `type`: language (javascript, python, typescript, etc.)27- `exports`: exported functions/classes/constants28- `imports`: external dependencies29- `functions`: all function/method names30- `classes`: all class names31- `lines`: total line count32- `category`: core | test | config | docs | util33- `summary`: 1-line description of what this file does3435**Global metadata:**36- `version`: schema version37- `generated`: ISO timestamp38- `contentHash`: hash of all file hashes combined39- `dependencies`: package dependencies with versions40- `architecture.entryPoint`: main entry file41- `architecture.pipeline`: data flow description42- `architecture.patterns`: detected patterns (MVC, pipeline, event-driven, etc.)43- `stats`: totalFiles, totalLines, languages, lastIndexed4445### Layer 2 - Symbol Map (`.claude/crew-symbols.json`, ~5-15KB)4647Mid-detail layer. Costs ~100-200 tokens for relevant section. Contains function signatures and relationships:4849**Per-symbol entry** (keyed as `filepath::symbolName`):50- `type`: function | class | method | constant | type51- `signature`: full signature string52- `params`: parameter list with types if available53- `returns`: return type if available54- `calls`: what this symbol calls55- `calledBy`: what calls this symbol56- `lineRange`: [startLine, endLine]57- `description`: 1-line description5859**Relationship maps:**60- `callGraph`: which functions call which61- `fileRelationships`: import/export connections between files6263### Layer 3 - Full Details (on-demand file reads)6465Agents read actual file contents ONLY after consulting Layer 1+2 to know exactly which file and line range they need. Use the Read tool with `offset` and `limit` parameters to read specific line ranges.6667## Building the Index (Full Build)6869When triggered for full build:7071### Step 1: Discover source files72```73Use Glob to find all source files:74- **/*.{js,ts,jsx,tsx,py,go,rs,java,kt,rb,php,c,cpp,h,cs}75- Exclude: node_modules, .git, dist, build, __pycache__, .next, vendor76- Also find: package.json, requirements.txt, go.mod, Cargo.toml, pyproject.toml77```7879### Step 2: Read each file and extract metadata80For each source file:811. Read the file content822. Compute SHA-256 hash (use `shasum -a 256` via Bash)833. Extract exports (look for `module.exports`, `export`, `def`, `func`, `fn`, `public`)844. Extract imports (look for `require`, `import`, `from`, `use`)855. Extract function names and signatures with line ranges866. Extract class names877. Count lines888. Categorize: core (src/), test (test/), config (root configs), docs, util (lib/, utils/)899. Generate 1-line summary9091### Step 3: Build call graph92From the imports/exports and function calls data:931. Map which files import from which942. Map which functions call which (by scanning function bodies for calls to known symbols)953. Identify entry points (files not imported by anything)9697### Step 4: Write index files981. Write `.claude/crew-index.json` (Layer 1)992. Write `.claude/crew-symbols.json` (Layer 2)1003. Report stats: files indexed, total lines, languages detected101102## Incremental Update103104When a single file changes:1051. Recompute its hash1062. Compare with stored hash in crew-index.json1073. If different: re-read the file, update its Layer 1 + Layer 2 entries1084. Update the global contentHash1095. Do NOT rebuild entries for unchanged files110111## How Agents Should Use the Index112113```114INDEX-FIRST PROTOCOL (mandatory):115116Before reading ANY source file:1171. Read .claude/crew-index.json → find which file(s) are relevant118 - Match by: function names, exports, summary keywords, category1192. Read .claude/crew-symbols.json → find exact symbol and line range120 - Match by: symbol name, signature, calledBy/calls relationships1213. Read ONLY the specific line range you need from the actual file122 - Use: Read tool with offset=startLine and limit=(endLine-startLine+1)123124NEVER:125- Read an entire file when you only need one function126- Grep the whole codebase when the index has the answer127- Skip the index because "it's faster to just read the file"128129ALWAYS:130- Consult Layer 1 first (cheapest)131- Consult Layer 2 only if Layer 1 isn't specific enough132- Read Layer 3 (actual file) only for the exact lines you need133- After making changes, note which files were modified for index update134```135136## Token Savings137138| Approach | Tokens Used | When |139|----------|-------------|------|140| Read all files | ~2000-10000+ | Without index |141| Layer 1 only | ~50-100 | Finding which file has a feature |142| Layer 1 + Layer 2 | ~150-300 | Finding a specific function |143| Layer 1 + 2 + targeted read | ~300-500 | Reading + editing a function |144| **Savings** | **5-10x** | Per agent interaction |145146## Edge Cases147148- **New file not in index**: If an agent creates a new file, add it to the index immediately149- **Deleted file still in index**: On reindex, remove entries for files that no longer exist150- **Binary/large files**: Skip files > 100KB or binary files (images, compiled assets)151- **Generated files**: Skip dist/, build/, .next/ directories152- **Config files**: Index but categorize as "config" — lower priority for code searches