Files
msd-core/.planning/phases/05-subagent-codebase-analysis/05-04-PLAN.md
Lex Christopherson a686d28669 docs(05): create gap closure plans for Steps 2-3 subagent delegation
Phase 05: Subagent Codebase Analysis
- 05-03: gsd-indexer subagent for file reading and export/import extraction
- 05-04: Refactor analyze-codebase Steps 2-3 to use indexer subagent

Gap closure addresses context exhaustion during indexing phase (Steps 2-3),
not just entity generation (Step 9).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-20 21:46:14 -06:00

9.0 KiB

phase, plan, type, wave, depends_on, files_modified, autonomous, gap_closure, must_haves
phase plan type wave depends_on files_modified autonomous gap_closure must_haves
05-subagent-codebase-analysis 04 execute 4
05-03
commands/gsd/analyze-codebase.md
false true
truths artifacts key_links
Orchestrator never reads file contents during Steps 2-3
Indexer subagent is spawned with file paths from Glob
Subagent writes index.json, orchestrator receives statistics only
User can run /gsd:analyze-codebase on 500+ file codebases without context exhaustion
path provides contains
commands/gsd/analyze-codebase.md Refactored command with subagent delegation for indexing gsd-indexer
from to via pattern
commands/gsd/analyze-codebase.md agents/gsd-indexer.md Task tool spawn Task.*gsd-indexer
from to via pattern
Step 2 Glob Step 3 subagent spawn file paths array Glob.*file_paths
Refactor analyze-codebase Steps 2-3 to spawn indexer subagent.

Purpose: Replace the current inline file reading (Step 3: "Read file content using Read tool") with a single subagent spawn. The orchestrator runs Glob to find files (Step 2), spawns gsd-indexer with the file list, and receives index.json back. This prevents context exhaustion on large codebases where 250+ file reads were causing the orchestrator to die before reaching Step 9.

Output: Updated commands/gsd/analyze-codebase.md

<execution_context> @/.claude/get-shit-done/workflows/execute-plan.md @/.claude/get-shit-done/templates/summary.md </execution_context>

@.planning/PROJECT.md @.planning/ROADMAP.md @.planning/STATE.md @.planning/phases/05-subagent-codebase-analysis/05-VERIFICATION.md @.planning/phases/05-subagent-codebase-analysis/05-03-SUMMARY.md @commands/gsd/analyze-codebase.md @agents/gsd-indexer.md Task 1: Refactor Steps 2-3 to use indexer subagent commands/gsd/analyze-codebase.md Modify Steps 2 and 3 of analyze-codebase.md. Step 2 stays mostly the same (Glob). Step 3 is completely replaced with subagent spawn.

Update Step 2: Find all indexable files

Keep the Glob pattern and exclusion list, but change the output description:

## Step 2: Find all indexable files

Use Glob tool with pattern: `**/*.{js,ts,jsx,tsx,mjs,cjs}`

Exclude directories (skip any path containing):
- node_modules
- dist
- build
- .git
- vendor
- coverage
- .next
- __pycache__

Filter results to remove excluded paths. Store as `file_paths` array.

**Output:** List of absolute file paths for indexing. Do NOT read file contents.

Replace Step 3: Process each file

Remove the entire current Step 3 (lines ~67-100) which contains:

  • "Read file content using Read tool"
  • Export regex patterns inline
  • Import regex patterns inline
  • Store in index structure

Replace with:

## Step 3: Spawn indexer subagent

Spawn `gsd-indexer` subagent with the file paths from Step 2.

**Why subagent delegation:**
- Orchestrator would exhaust context reading 500+ files inline
- Subagent gets fresh 200k context for file reading
- Orchestrator only handles file paths (small)
- Subagent writes index.json directly (large)

**Task tool invocation:**

```python
file_list = "\n".join(file_paths)  # From Step 2 Glob results

Task(
  prompt=f"""Index codebase files by extracting exports and imports.

You are a GSD indexer. Read source files and extract exports/imports using regex patterns.

**Parameters:**
- Files to process: {len(file_paths)}
- Output path: .planning/intel/index.json

**Export patterns:**
- Named: export\\s*\\{{([^}}]+)\\}}
- Declaration: export\\s+(?:const|let|var|function\\*?|async\\s+function|class)\\s+(\\w+)
- Default: export\\s+default\\s+(?:function\\s*\\*?\\s*|class\\s+)?(\\w+)?
- CommonJS object: module\\.exports\\s*=\\s*\\{{([^}}]+)\\}}
- CommonJS single: module\\.exports\\s*=\\s*(\\w+)\\s*[;\\n]
- TypeScript: export\\s+(?:type|interface)\\s+(\\w+)

**Import patterns:**
- ES6: import\\s+(?:\\{{[^}}]*\\}}|\\*\\s+as\\s+\\w+|\\w+)\\s+from\\s+['\"]([^'\"]+)['\"]
- Side-effect: import\\s+['\"]([^'\"]+)['\"]
- CommonJS: require\\s*\\(\\s*['\"]([^'\"]+)['\"]\\s*\\)

**Index schema:**
```json
{{
  "version": 1,
  "updated": {timestamp},
  "files": {{
    "/absolute/path/file.js": {{
      "exports": ["name1", "name2"],
      "imports": ["source1", "source2"],
      "indexed": {timestamp}
    }}
  }}
}}

Process: For each file path below:

  1. Read file content using Read tool
  2. Apply export regex patterns, collect export names
  3. Apply import regex patterns, collect import sources
  4. Store in index structure with absolute path as key

Files: {file_list}

Return format: When complete, return ONLY statistics:

INDEXING COMPLETE

Files processed: {{N}} Exports found: {{M}} Imports found: {{K}} Errors: {{E}}

Index written to: .planning/intel/index.json

Do NOT return index contents. """, subagent_type="gsd-indexer" )


**Wait for completion:** Task() blocks until subagent finishes.

**Verify index created:**
```bash
ls -la .planning/intel/index.json

**Update context section** (around lines 21-38):

Add to the context section, before the existing "Execution model (Step 9)" section:

```markdown
**Execution model (Steps 2-3 - Indexing):**
- Orchestrator finds file paths via Glob (Step 2)
- Spawns `gsd-indexer` subagent with file paths only (Step 3)
- Subagent reads files in fresh 200k context, applies regex patterns
- Subagent writes index.json directly to disk
- Subagent returns statistics only (not file contents or index data)
- This prevents context exhaustion on large codebases (500+ files)

Remove inline regex documentation:

The regex patterns are now documented in the subagent prompt. Remove any duplicate documentation that described inline processing. The command should make clear that file reading is DELEGATED, not performed inline.

Verify Step 4 still works:

Step 4 "Detect conventions" reads from index.json that the subagent wrote. This should work unchanged since the index format is the same. Read updated commands/gsd/analyze-codebase.md and confirm:

  • Step 2 mentions storing as "file_paths" array, NOT reading contents
  • Step 3 spawns gsd-indexer via Task tool
  • No "Read file content" instruction in orchestrator steps
  • Context section documents subagent delegation for Steps 2-3
  • Step 4+ remain unchanged (read index.json from disk)
  • Regex patterns appear in subagent prompt (not inline) analyze-codebase.md Steps 2-3 refactored. Orchestrator only Globs for paths, then spawns indexer subagent. File reading is fully delegated to subagent with fresh context.
Subagent delegation for indexing in /gsd:analyze-codebase (Steps 2-3) 1. Navigate to a LARGE test project (100+ files, ideally 250+) 2. Delete existing intel if any: `rm -rf .planning/intel` 3. Run `/gsd:analyze-codebase` 4. Observe execution: - Step 1: Creates directory - Step 2: Runs Glob, collects file paths (does NOT read files) - Step 3: Spawns gsd-indexer subagent with file list - Subagent reads files (should see many Read tool calls in subagent output) - Subagent writes index.json directly - Subagent returns statistics only - Step 4+: Orchestrator reads index.json, continues with conventions - Step 9: Spawns gsd-entity-generator (existing flow) 5. Verify: - Orchestrator context NOT exhausted (no early death) - `.planning/intel/index.json` exists with file entries - `.planning/intel/conventions.json` has detected patterns - `.planning/intel/summary.md` exists - Entity generation works (if Step 9 executed) 6. Compare orchestrator context usage before/after refactor - Before: Orchestrator read all files -> context exhaustion at ~250 files - After: Orchestrator only handles paths -> survives 500+ files Type "approved" if /gsd:analyze-codebase completes on large codebase without context exhaustion, or describe issues - [ ] Step 2 outputs file_paths array (no file reading) - [ ] Step 3 spawns gsd-indexer subagent - [ ] Subagent prompt includes all 6 export patterns - [ ] Subagent prompt includes all 3 import patterns - [ ] Context section documents indexing subagent delegation - [ ] No "Read file content" in orchestrator steps - [ ] Step 4+ reads index.json from disk (unchanged) - [ ] Command runs successfully on 250+ file codebase - [ ] Orchestrator context preserved (no early death) - [ ] User verification checkpoint passed

<success_criteria> analyze-codebase refactored with full subagent delegation. Both indexing (Steps 2-3) and entity generation (Step 9) now use subagents. Command works on 500+ file codebases without context exhaustion. </success_criteria>

After completion, create `.planning/phases/05-subagent-codebase-analysis/05-04-SUMMARY.md`