1
---
2
name: scan
3
description: Scans the codebase to generate project-doc.md and AGENTS.md. Use when bootstrapping a new agent-driven repo, refreshing project documentation after architectural changes, or running a delta scan to detect drift. Runs a full scan on first use and a smart delta scan on subsequent runs. Uses understand-anything + context-mode when available, falls back to native tools otherwise. Only updates AGENTS.md on detected architectural changes with human confirmation.
4
---
5
6
# Codebase Scanner
7
8
You are a technical analyst. Your job is to scan the project codebase and produce accurate, project-specific documentation used by all downstream agents.
9
10
## Step 1: Check Optional Plugin Dependencies
11
12
Check whether the two optional enhancement plugins are available:
13
14
```
15
understand-anything → /plugin list | grep understand-anything
16
context-mode → /plugin list | grep context-mode
17
```
18
19
These plugins are **optional**. They improve scan quality but are not required:
20
21
- **understand-anything** (Lum1104/Understand-Anything) — provides deeper semantic code analysis
22
- **context-mode** (mksglu/context-mode) — routes large outputs through a sandbox to protect the context window
23
24
If both are present, use them in Steps 3–4 as described below. If either or both are missing, proceed with the **native fallback** approach: use `find`, `grep`, `cat`, and `git` commands directly, routing large outputs through `ctx_execute` / `ctx_execute_file` if context-mode is available, otherwise summarise inline.
25
26
> **Note:** To install the optional plugins manually:
27
> ```
28
> /plugin marketplace add Lum1104/Understand-Anything && /plugin install understand-anything
29
> /plugin marketplace add mksglu/context-mode && /plugin install context-mode@context-mode
30
> ```
31
32
## Step 2: Determine Scan Mode
33
34
Check if `.claude/pipeline/project-doc.md` exists.
35
36
- **Does not exist** → FULL SCAN (first run)
37
- **Exists** → DELTA SCAN
38
39
## Step 3A: Full Scan
40
41
Use `understand-anything` to analyse the entire codebase. If **context-mode** is available (verified in Step 1), route ALL output through its tools (`ctx_batch_execute` / `ctx_execute_file`) — never dump raw file contents into the main context window. If context-mode is not available, summarise each file's findings inline and avoid printing raw file contents.
42
43
Produce `.claude/pipeline/project-doc.md` using the following structure (based on the architecture-blueprint-generator pattern):
44
45
```md
46
# Project Documentation
47
> Generated: [timestamp] | Mode: FULL
48
49
## Tech Stack
50
- Runtime: [e.g. Node.js 20, Python 3.11]
51
- Language: [e.g. TypeScript, Python]
52
- Framework: [e.g. Next.js 14 App Router, FastAPI]
53
- Database: [e.g. PostgreSQL via Prisma]
54
- Styling: [e.g. Tailwind CSS]
55
- State Management: [e.g. Zustand, Redux]
56
57
## Dependencies
58
[Key libraries with versions, grouped by: core / dev / testing]
59
60
## Architecture Pattern
61
[e.g. Feature-based, Layered MVC, Clean Architecture]
62
[Describe how the project is structured and why]
63
64
## Folder Structure
65
[Top-level directory map with purpose of each folder]
66
67
## Code Style Conventions
68
[Naming patterns, file naming, import ordering, export patterns]
69
[Inferred from actual code — not guessed]
70
71
## Modularity Practices
72
[How concerns are separated, shared module locations, service patterns]
73
74
## Data Architecture
75
[Entity relationships, data access patterns, ORM usage]
76
77
## Cross-Cutting Concerns
78
[Auth/authz approach, error handling patterns, logging, validation]
79
80
## Service Communication
81
[REST / GraphQL / event-driven — document what actually exists]
82
83
## Test Coverage
84
- Overall coverage: [X%]
85
- Testing framework: [e.g. Jest, Vitest, Pytest]
86
- Key untested areas: [list]
87
- Test patterns used: [unit / integration / e2e]
88
89
## Entry Points
90
[Main files, key config files, environment setup]
91
92
## Changed Files
93
[Only present in delta scans — list of files re-scanned]
94
95
## Last Scanned
96
[ISO timestamp]
97
```
98
99
After writing `project-doc.md`, proceed to **Step 4** to generate `AGENTS.md`.
100
101
## Step 3B: Delta Scan
102
103
1. Run `git diff HEAD~1 --name-only` to get changed files
104
2. If no changed files, report "No changes detected — project-doc.md is current" and exit
105
3. Use `understand-anything` to re-analyse only the changed files; route output through `ctx_execute_file` if context-mode is available, otherwise summarise inline
106
4. Patch only the affected sections of `.claude/pipeline/project-doc.md`
107
5. Update the `Last Scanned` and `Changed Files` fields
108
6. Proceed to **Step 4B** (architectural change detection)
109
110
## Step 4A: Generate AGENTS.md (First Run Only)
111
112
Write `AGENTS.md` to the repo root. This is NOT a copy of `project-doc.md` — it is rewritten as agent instructions, tailored to this specific project. Every agent reads this file first.
113
114
Before writing AGENTS.md, read and follow the complete [project-specific instruction template](references/agents-template.md), including its security rules and human-confirmed update requirements.
115
116
If the project is MERN stack (MongoDB + Express + React + Node.js — detected from package.json / requirements), append a `### MERN Stack Notes` section to AGENTS.md covering: use Mongoose middleware over raw queries, handle async errors in Express with a central error handler, avoid storing JWT tokens in localStorage (use httpOnly cookies), and never expose Mongoose error objects directly in API responses.
117
118
## Step 4B: Architectural Change Detection (Delta Runs Only)
119
120
After patching `project-doc.md`, compare the new version against the previous. Check for:
121
- New framework or major library added
122
- New architectural directory pattern created (e.g. new `/lib/hooks/`, `/services/`)
123
- Major dependency swap (e.g. axios → fetch, moment → dayjs)
124
- New auth or session handling pattern
125
126
If any detected, show:
127
128
```
129
⚠️ Architectural change detected in delta scan:
130
[List specific changes found]
131
132
AGENTS.md may need updating. Review and confirm:
133
[y] Update AGENTS.md — patch affected sections only
134
[n] Skip — this is not an architectural change
135
```
136
137
Only on `[y]` confirmation: patch the relevant sections of `AGENTS.md`. Never rewrite the full file.
138
139
## Step 5: Report
140
141
Print a summary:
142
143
```
144
✅ Scan complete ([FULL/DELTA])
145
project-doc.md → updated
146
AGENTS.md → [generated / patched / unchanged]
147
Changed files → [N files re-scanned / N/A for full scan]
148
Coverage → [X%]
149
```
150
151
Update `state.json` field `checkpoints.scan = "completed"`.
152