Analyze the current codebase and produce a knowledge-graph.json file in .understand-anything/. This file powers the interactive dashboard for exploring the project's architecture.
$ARGUMENTS may contain:--full — Force a full rebuild, ignoring any existing graphDetermine whether to run a full analysis or incremental update.
PROJECT_ROOT to the current working directory.```bash
git rev-parse HEAD
```
```bash
mkdir -p $PROJECT_ROOT/.understand-anything/intermediate
```
$PROJECT_ROOT/.understand-anything/knowledge-graph.json exists. If it does, read it.$PROJECT_ROOT/.understand-anything/meta.json exists. If it does, read it to get gitCommitHash.| Condition | Action |
|---|---|
| --full flag in $ARGUMENTS | Full analysis (all phases) |
| No existing graph or meta | Full analysis (all phases) |
| Existing graph + unchanged commit hash | Report "Graph is up to date" and STOP |
| Existing graph + changed files | Incremental update (re-analyze changed files only) |
For incremental updates, get the changed file list:
```bash
git diff
```
If this returns no files, report "Graph is up to date" and STOP.
README.md (or README.rst, readme.md) from $PROJECT_ROOT if it exists. Store as $README_CONTENT (first 3000 characters).package.json, pyproject.toml, Cargo.toml, go.mod, pom.xml) if it exists. Store as $MANIFEST_CONTENT.```bash
find $PROJECT_ROOT -maxdepth 2 -type f -not -path '/node_modules/' -not -path '/.git/' -not -path '/dist/' | head -100
```
Store as $DIR_TREE.
src/index.ts, src/main.ts, src/App.tsx, main.py, main.go, src/main.rs, index.js. Store first match as $ENTRY_POINT.Dispatch a subagent using the prompt template at ./project-scanner-prompt.md. Read the template file and pass the full content as the subagent's prompt, appending the following additional context:
> Additional context from main session:
>
> Project README (first 3000 chars):
> ```
> $README_CONTENT
> ```
>
> Package manifest:
> ```
> $MANIFEST_CONTENT
> ```
>
> Use this context to produce more accurate project name, description, and framework detection. The README and manifest are authoritative — prefer their information over heuristics.
Pass these parameters in the dispatch prompt:
> Scan this project directory to discover all source files, detect languages and frameworks.
> Project root: $PROJECT_ROOT
> Write output to: $PROJECT_ROOT/.understand-anything/intermediate/scan-result.json
After the subagent completes, read $PROJECT_ROOT/.understand-anything/intermediate/scan-result.json to get:
Gate check: If >200 files, inform the user and suggest scoping with a subdirectory argument. Proceed only if user confirms or add guidance that this may take a while.
Batch the file list from Phase 1 into groups of 5-10 files each (aim for balanced batch sizes).
For each batch, dispatch a subagent using the prompt template at ./file-analyzer-prompt.md. Run up to 3 subagents concurrently using parallel dispatch. Read the template once, then for each batch pass the full template content as the subagent's prompt, appending the following additional context:
> Additional context from main session:
>
> Project: —
> Frameworks detected:
> Languages:
>
> Framework-specific guidance:
> - If React/Next.js: files in app/ or pages/ are routes, components/ are UI, lib/ or utils/ are utilities
> - If Express/Fastify: files in routes/ are API endpoints, middleware/ is middleware, models/ or db/ is data
> - If Python Django: views.py are controllers, models.py is data, urls.py is routing, templates/ is UI
> - If Go: cmd/ is entry points, internal/ is private packages, pkg/ is public packages
>
> Use this context to produce more accurate summaries and better classify file roles.
Fill in batch-specific parameters below and dispatch:
> Analyze these source files and produce GraphNode and GraphEdge objects.
> Project root: $PROJECT_ROOT
> Project:
> Languages:
> Batch index:
> Write output to: $PROJECT_ROOT/.understand-anything/intermediate/batch-
>
> All project files (for import resolution):
>
>
> Files to analyze in this batch:
> 1. (
> 2. (
> ...
After ALL batches complete, read each batch- file and merge:
nodes arrays. If duplicate node IDs exist, keep the later occurrence.edges arrays. Deduplicate by the composite key source + target + type.Use the changed files list from Phase 0. Batch and dispatch file-analyzer subagents using the same process as above, but only for changed files.
After batches complete, merge with the existing graph:
filePath matches any changed filesource or target references a removed nodeMerge all file-analyzer results into a single set of nodes and edges. Then perform basic integrity cleanup:
source or target references a node ID that does not exist in the merged node setDispatch a subagent using the prompt template at ./architecture-analyzer-prompt.md. Read the template file and pass the full content as the subagent's prompt, appending the following additional context:
> Additional context from main session:
>
> Frameworks detected:
>
> Directory tree (top 2 levels):
> ```
> $DIR_TREE
> ```
>
> Framework-specific layer hints:
> - If React/Next.js: app/ or pages/ → UI Layer, api/ → API Layer, lib/ → Service Layer, components/ → UI Layer
> - If Express: routes/ → API Layer, controllers/ → Service Layer, models/ → Data Layer, middleware/ → Middleware Layer
> - If Python Django: views/ → API Layer, models/ → Data Layer, templates/ → UI Layer, management/ → CLI Layer
> - If Go: cmd/ → Entry Points, internal/ → Service Layer, pkg/ → Shared Library, api/ → API Layer
>
> Use the directory tree and framework hints to inform layer assignments. Directory structure is strong evidence for layer boundaries.
Pass these parameters in the dispatch prompt:
> Analyze this codebase's structure to identify architectural layers.
> Project root: $PROJECT_ROOT
> Write output to: $PROJECT_ROOT/.understand-anything/intermediate/layers.json
> Project: —
>
> File nodes:
> ```json
> [list of {id, name, filePath, summary, tags} for all file-type nodes]
> ```
>
> Import edges:
> ```json
> [list of edges with type "imports"]
> ```
After the subagent completes, read $PROJECT_ROOT/.understand-anything/intermediate/layers.json to get the layer assignments.
layers.json may be either:
{ "layers": [...] } from the current prompt/template outputNormalize either form into a final top-level layers array before assembling the graph. Each final saved layer object MUST match this exact shape:
[
{
"id": "layer:<kebab-case-name>",
"name": "<layer name>",
"description": "<what belongs in this layer>",
"nodeIds": ["file:src/App.tsx", "file:src/main.tsx"]
}
]
Rules:
id is required and must be uniquenodeIds is required and must contain graph node IDs, not raw file pathslayers array before any other normalizationnodeIds that do not exist in the merged node setnodes field in the final saved layer objectsFor incremental updates: Always re-run architecture analysis on the full merged node set, since layer assignments may shift when files change.
Context for incremental updates: When re-running architecture analysis, also inject the previous layer definitions:
> Previous layer definitions (for naming consistency):
> ```json
> [previous layers from existing graph]
> ```
>
> Maintain the same layer names and IDs where possible. Only add/remove layers if the file structure has materially changed.
Dispatch a subagent using the prompt template at ./tour-builder-prompt.md. Read the template file and pass the full content as the subagent's prompt, appending the following additional context:
> Additional context from main session:
>
> Project README (first 3000 chars):
> ```
> $README_CONTENT
> ```
>
> Project entry point: $ENTRY_POINT
>
> Use the README to align the tour narrative with the project's own documentation. Start the tour from the entry point if one was detected. The tour should tell the same story the README tells, but through the lens of actual code structure.
Pass these parameters in the dispatch prompt:
> Create a guided learning tour for this codebase.
> Project root: $PROJECT_ROOT
> Write output to: $PROJECT_ROOT/.understand-anything/intermediate/tour.json
> Project: —
> Languages:
>
> Nodes (summarized):
> ```json
> [list of {id, name, filePath, summary, type} for key nodes]
> ```
>
> Layers:
> ```json
> [layers from Phase 4]
> ```
>
> Key edges:
> ```json
> [imports and calls edges]
> ```
After the subagent completes, read $PROJECT_ROOT/.understand-anything/intermediate/tour.json to get the tour steps.
tour.json may be either:
{ "steps": [...] } from the current prompt/template outputNormalize either form into a final top-level tour array before assembling the graph. Each final saved tour step object MUST match this exact shape:
[
{
"order": 1,
"title": "Start at the app entry",
"description": "This step explains how the frontend boots and mounts.",
"nodeIds": ["file:src/main.tsx", "file:src/App.tsx"]
}
]
Rules:
steps array before any other normalizationdescription is required; do not use whyItMatters in the final saved tour stepsnodeIds is required; do not use nodesToInspect in the final saved tour stepsnodeIds must reference existing graph node IDslanguageLesson when presentorder before savingBefore assembling the final graph:
{ "layers": [...] } -> use the contained array as the working layers value{ "steps": [...] } -> use the contained array as the working tour valuenodes field to nodeIdsnodesToInspect field to nodeIdswhyItMatters field to descriptionfile: conventionlayer:layers value is an array of { id, name, description, nodeIds }tour value is an array of { order, title, description, nodeIds }, preserving optional languageLessonAssemble the full KnowledgeGraph JSON object:
{
"version": "1.0.0",
"project": {
"name": "<projectName>",
"languages": ["<languages>"],
"frameworks": ["<frameworks>"],
"description": "<projectDescription>",
"analyzedAt": "<ISO 8601 timestamp>",
"gitCommitHash": "<commit hash from Phase 0>"
},
"nodes": [<all merged nodes from Phase 3>],
"edges": [<all merged edges from Phase 3>],
"layers": [<layers from Phase 4>],
"tour": [<steps from Phase 5>]
}
layers is an array of objects with these required fields: id, name, description, nodeIdstour is an array of objects with these required fields: order, title, description, nodeIdstour[*].languageLesson is allowed as an optional string fieldlayers[*].nodeIds entry exists in the merged node settour[*].nodeIds entry exists in the merged node setIf validation fails, automatically normalize and rewrite the graph into this shape before saving. If the graph still fails final validation after the normalization pass, save it with warnings but mark dashboard auto-launch as skipped.
$PROJECT_ROOT/.understand-anything/intermediate/assembled-graph.json../graph-reviewer-prompt.md. Read the template file and pass the full content as the subagent's prompt, appending the following additional context:> Additional context from main session:
>
> Phase 1 scan results (file inventory):
> ```json
> [list of {path, sizeLines} from scan-result.json]
> ```
>
> Phase warnings/errors accumulated during analysis:
> - [list any batch failures, skipped files, or warnings from Phases 2-5]
>
> Cross-validate: every file in the scan inventory should have a corresponding file: node in the graph. Flag any missing files. Also flag any graph nodes whose filePath doesn't appear in the scan inventory.
Pass these parameters in the dispatch prompt:
> Validate the knowledge graph at $PROJECT_ROOT/.understand-anything/intermediate/assembled-graph.json.
> Project root: $PROJECT_ROOT
> Read the file and validate it for completeness and correctness.
> Write output to: $PROJECT_ROOT/.understand-anything/intermediate/review.json
$PROJECT_ROOT/.understand-anything/intermediate/review.json.approved: false:issues listtags -> ["untagged"], empty summary -> "No summary available")approved: true: Proceed to Phase 7.$PROJECT_ROOT/.understand-anything/knowledge-graph.json.$PROJECT_ROOT/.understand-anything/meta.json:```json
{
"lastAnalyzedAt": "
"gitCommitHash": "
"version": "1.0.0",
"analyzedFiles":
}
```
```bash
rm -rf $PROJECT_ROOT/.understand-anything/intermediate
```
$PROJECT_ROOT/.understand-anything/knowledge-graph.json/understand-dashboard skill if final graph validation passed after normalization/review fixes.If final validation did not pass, report that the graph was saved with warnings and dashboard launch was skipped.
$PHASE_WARNINGS list. Pass this list to the graph-reviewer in Phase 6 for comprehensive validation.| Type | Description | ID Convention |
|---|---|---|
| --- | --- | --- |
file | Source file | file: |
function | Function or method | func: |
class | Class, interface, or type | class: |
module | Logical module or package | module: |
concept | Abstract concept or pattern | concept: |
| Category | Types |
|---|---|
| --- | --- |
| Structural | imports, exports, contains, inherits, implements |
| Behavioral | calls, subscribes, publishes, middleware |
| Data flow | reads_from, writes_to, transforms, validates |
| Dependencies | depends_on, tested_by, configures |
| Semantic | related, similar_to |
| Edge Type | Weight |
|---|---|
| --- | --- |
contains | 1.0 |
inherits, implements | 0.9 |
calls, exports | 0.8 |
imports | 0.7 |
depends_on | 0.6 |
tested_by | 0.5 |
| All others | 0.5 (default) |
共 1 个版本