From rp1-base
Orchestrates parallel knowledge base generation from codebases using spatial analysis, map-reduce architecture, and sub-agents for concepts, architecture, modules, and patterns. Supports feature learning from archived features.
npx claudepluginhub rp1-run/rp1This skill is limited to using the following tools:
Extract these parameters from the user's input:
Orchestrates parallel knowledge base generation using spatial analysis, map-reduce architecture, incremental updates, and feature learning from archived features.
Analyzes source code to discover and extract implicit knowledge—architecture patterns, conventions, API contracts, config structures, codebase rules—into KB articles.
Manages persistent knowledge graph for specs by caching agent discoveries, codebase analysis, patterns, components, and APIs. Use to remember findings across sessions, validate task dependencies, and query prior work.
Share bugs, ideas, or general feedback.
Extract these parameters from the user's input:
| Parameter | Required | Default | Description |
|---|---|---|---|
FEATURE_ID | No | - | Feature ID to incorporate learnings from an archived feature into KB |
Environment values (resolve via shell):
RP1_ROOT: !rp1 agent-tools rp1-root-dir (extract data.root from JSON response)This command orchestrates parallel knowledge base generation using a map-reduce architecture
CRITICAL: This is an ORCHESTRATOR command, not a thin wrapper. This command must handle parallel execution coordination, result aggregation, and state management.
Phase 1 (Sequential): Spatial Analyzer -> Categorized file lists
Phase 2 (Parallel): 4 Analysis Agents -> JSON outputs (concept, arch, module, pattern)
Phase 3 (Sequential): Command -> Merge JSON -> Generate index.md -> Write KB files
Key Design: The main orchestrator generates index.md directly (not via sub-agent) because:
DO NOT ask for user approval. Execute immediately.
If FEATURE_ID is provided, this is a feature learning build that captures knowledge from an archived feature. Skip Phase 0 entirely (no git commit parsing needed).
Locate archived feature:
FEATURE_PATH = {{$RP1_ROOT}}/work/archives/features/{FEATURE_ID}/
If not found, check active features:
FEATURE_PATH = {{$RP1_ROOT}}/work/features/{FEATURE_ID}/
If neither exists, error:
Feature not found: {FEATURE_ID}
Checked: {{$RP1_ROOT}}/work/archives/features/{FEATURE_ID}/
{{$RP1_ROOT}}/work/features/{FEATURE_ID}/
Read feature documentation:
{FEATURE_PATH}/requirements.md - What was built{FEATURE_PATH}/design.md - How it was designed{FEATURE_PATH}/field-notes.md - Learnings and discoveries (if exists){FEATURE_PATH}/tasks.md - Implementation details with files modifiedExtract files modified from tasks.md:
Parse implementation summaries to build FILES_MODIFIED list:
Look for patterns:
- **Files**: `src/file1.ts`, `src/file2.ts`
- **Files Modified**: ...
Extract all file paths into FILES_MODIFIED array.
Extract feature context:
Build a FEATURE_CONTEXT object containing:
files_modified: FILES_MODIFIED arrayJump directly to Phase 1 (Spatial Analysis):
FILES_MODIFIED to spatial analyzer instead of git diffSpatial analyzer prompt (Feature Learning Mode):
FEATURE_LEARNING mode. Categorize these files modified during feature implementation:
FILES: {{stringify(FILES_MODIFIED)}}
Rank each file 0-5, categorize by KB section (index_files, concept_files, arch_files, module_files).
Return JSON with categorized files.
Sub-agent prompts include:
FEATURE_CONTEXT: {{stringify(feature_context)}}
MODE: FEATURE_LEARNING
Incorporate learnings from this completed feature:
- Update patterns.md with implementation patterns discovered
- Update architecture.md if new architectural patterns emerged
- Update modules.md with new components/dependencies
- Update concept_map.md with new domain concepts
Focus on files that were modified: {{stringify(FILES_MODIFIED)}}
NOTE: Skip this phase entirely if FEATURE_ID is provided (Feature Learning Mode).
Check for existing KB state:
{{$RP1_ROOT}}/context/state.json existsgit_commit field from state.jsonCheck current git commit:
git rev-parse HEAD to get current commit hashDetermine build strategy:
CASE A: No changes detected (state.json exists AND git commit unchanged):
CASE A-MONOREPO: No changes in this service (monorepo: git commit changed but no changes in CODEBASE_ROOT):
git_commit field to new commit hashCASE B: First-time build (no state.json):
CASE C: Incremental update (state.json exists AND commit changed AND files changed in CODEBASE_ROOT):
ACTION: Incremental analysis mode - get changed files with diffs
Read monorepo metadata from state.json AND local values from meta.json:
# Read shareable state
repo_type=$(jq -r '.repo_type // "single-project"' {{$RP1_ROOT}}/context/state.json)
# Read local values from meta.json (with fallback to state.json for backward compatibility)
if [ -f "{{$RP1_ROOT}}/context/meta.json" ]; then
repo_root=$(jq -r '.repo_root // "."' {{$RP1_ROOT}}/context/meta.json)
current_project_path=$(jq -r '.current_project_path // "."' {{$RP1_ROOT}}/context/meta.json)
else
# Backward compatibility: read from state.json if meta.json doesn't exist
repo_root=$(jq -r '.repo_root // "."' {{$RP1_ROOT}}/context/state.json)
current_project_path=$(jq -r '.current_project_path // "."' {{$RP1_ROOT}}/context/state.json)
fi
Get changed files list:
# If monorepo, run git diff from repo root and filter to current project
if [ "$repo_type" = "monorepo" ]; then
cd "$repo_root"
# Get all changed files
all_changes=$(git diff --name-only {{old_commit}} {{new_commit}})
# Filter to current project (skip filtering if root project)
if [ "$current_project_path" = "." ] || [ "$current_project_path" = "" ]; then
# Root project - include all files
echo "$all_changes"
else
# Subdirectory project - filter to project path
echo "$all_changes" | grep "^${current_project_path}"
fi
else
# Single-project - get all changes
git diff --name-only {{old_commit}} {{new_commit}}
fi
Check if any files changed in scope:
Check change set size (prevent token limit issues):
changed_file_count=$(echo "$changed_files" | wc -l)
if [ $changed_file_count -gt 50 ]; then
echo "Large change set ($changed_file_count files changed). Using FULL mode for reliability."
# Fall back to FULL mode (skip getting diffs)
MODE="FULL"
else
MODE="INCREMENTAL"
fi
MESSAGE:
Get detailed diffs for each changed file (only if MODE=INCREMENTAL):
# Only if incremental mode (< 50 files)
git diff {{old_commit}} {{new_commit}} -- <filepath>
Store diffs: Create FILE_DIFFS JSON mapping filepath -> diff content (only if MODE=INCREMENTAL)
Filter changed files: Apply EXCLUDE_PATTERNS, filter to relevant extensions
Store changed files list: Will be passed to spatial analyzer
MODE: INCREMENTAL (< 50 files) or FULL (>= 50 files)
Spawn spatial analyzer agent:
For full build (CASE B):
{% dispatch_agent "rp1-base:kb-spatial-analyzer" %} FULL SCAN mode. Scan all files in repository at {{CODEBASE_ROOT}}, rank files 0-5, categorize by KB section. Return JSON with index_files, concept_files, arch_files, module_files arrays. {% enddispatch_agent %}
For incremental build (CASE C):
{% dispatch_agent "rp1-base:kb-spatial-analyzer" %} INCREMENTAL mode. Only categorize these changed files: {{changed_files_list}}. Rank each file 0-5, categorize by KB section (index_files, concept_files, arch_files, module_files). Return JSON with categorized changed files. {% enddispatch_agent %}
Parse spatial analyzer output:
repo_type, monorepo_projects, total_files_scanned, index_files, concept_files, arch_files, module_files, local_metarepo_type, monorepo_projectslocal_meta: repo_root, current_project_path (will be written to meta.json)Handle spatial analyzer failure:
Spawn 4 analysis agents in parallel (CRITICAL: Use a SINGLE message with 4 Task tool calls):
Agent 1 - Concept Extractor:
{% dispatch_agent "rp1-base:kb-concept-extractor" %} MODE={{mode}}. Extract domain concepts for concept_map.md. Repository type: {{repo_type}}. Files to analyze (JSON): {{stringify(concept_files)}}. {{if mode==INCREMENTAL}}File diffs (JSON): {{stringify(file_diffs_for_concept_files)}}{{endif}}. Return JSON with concepts, terminology, relationships. {% enddispatch_agent %}
Agent 2 - Architecture Mapper:
{% dispatch_agent "rp1-base:kb-architecture-mapper" %} MODE={{mode}}. Map system architecture for architecture.md. Repository type: {{repo_type}}. Files to analyze (JSON): {{stringify(arch_files)}}. {{if mode==INCREMENTAL}}File diffs (JSON): {{stringify(file_diffs_for_arch_files)}}{{endif}}. Return JSON with patterns, layers, diagram. {% enddispatch_agent %}
Agent 3 - Module Analyzer:
{% dispatch_agent "rp1-base:kb-module-analyzer" %} MODE={{mode}}. Analyze modules for modules.md. Repository type: {{repo_type}}. Files to analyze (JSON): {{stringify(module_files)}}. {{if mode==INCREMENTAL}}File diffs (JSON): {{stringify(file_diffs_for_module_files)}}{{endif}}. Return JSON with modules, components, dependencies. {% enddispatch_agent %}
Agent 4 - Pattern Extractor:
{% dispatch_agent "rp1-base:kb-pattern-extractor" %} MODE={{mode}}. Extract implementation patterns for patterns.md. Repository type: {{repo_type}}. Files to analyze (JSON): {{stringify(concept_files + module_files)}}. {{if mode==INCREMENTAL}}File diffs (JSON): {{stringify(file_diffs_for_pattern_files)}}{{endif}}. Return JSON with patterns (<=150 lines when rendered). {% enddispatch_agent %}
Collect agent outputs:
Handle partial failures:
If 1 agent fails:
If 2+ agents fail:
Load KB templates:
Use Skill tool with:
skill: rp1-base:knowledge-base-templates
Merge agent data into templates (concept_map, architecture, modules, patterns):
concept_map.md:
architecture.md:
modules.md:
patterns.md:
Validate Mermaid diagrams:
Use Skill tool with:
skill: rp1-base:mermaid
Generate index.md directly (orchestrator-owned, not agent):
The orchestrator generates index.md as the "jump off" entry point by aggregating data from all 4 sub-agents.
Follow the index.md generation instructions in the knowledge-base-templates skill:
Write KB files:
Use Write tool to write:
- {{$RP1_ROOT}}/context/index.md
- {{$RP1_ROOT}}/context/concept_map.md
- {{$RP1_ROOT}}/context/architecture.md
- {{$RP1_ROOT}}/context/modules.md
- {{$RP1_ROOT}}/context/patterns.md
Aggregate metadata:
Generate state.json (shareable metadata - safe to commit/share):
{
"strategy": "parallel-map-reduce",
"repo_type": "{{repo_type}}",
"monorepo_projects": ["{{project1}}", "{{project2}}"],
"generated_at": "{{ISO timestamp}}",
"git_commit": "{{git rev-parse HEAD}}",
"files_analyzed": {{total_files}},
"languages": ["{{lang1}}", "{{lang2}}"],
"metrics": {
"modules": {{module_count}},
"components": {{component_count}},
"concepts": {{concept_count}}
}
}
Generate meta.json (local values - should NOT be committed/shared):
{
"repo_root": "{{repo_root}}",
"current_project_path": "{{current_project_path}}"
}
NOTE: meta.json contains local paths that may differ per team member. This file should be added to .gitignore.
Write state files:
Use Write tool to write:
- {{$RP1_ROOT}}/context/state.json
- {{$RP1_ROOT}}/context/meta.json
Error Conditions:
Error Handling Procedure:
Final Report:
Knowledge Base Generated Successfully
Strategy: Parallel map-reduce
Repository: {{repo_type}}
Files Analyzed: {{total_files}}
KB Files Written:
- {{$RP1_ROOT}}/context/index.md
- {{$RP1_ROOT}}/context/concept_map.md
- {{$RP1_ROOT}}/context/architecture.md
- {{$RP1_ROOT}}/context/modules.md
- {{$RP1_ROOT}}/context/patterns.md
- {{$RP1_ROOT}}/context/state.json (shareable metadata)
- {{$RP1_ROOT}}/context/meta.json (local paths - add to .gitignore)
Next steps:
- KB is automatically loaded by agents when needed (no manual /knowledge-load required)
- Subsequent runs will use same parallel approach (10-15 min)
- Incremental updates (changed files only) are faster (2-5 min)
- Add meta.json to .gitignore to prevent sharing local paths
Final Report (Feature Learning Mode):
Feature Learnings Captured
Feature: {{FEATURE_ID}}
Source: {{FEATURE_PATH}}
Learnings Incorporated:
- patterns.md: {{N}} new patterns from implementation
- architecture.md: {{N}} architectural decisions
- modules.md: {{N}} new components/dependencies
- concept_map.md: {{N}} domain concepts
KB Files Updated:
- {{$RP1_ROOT}}/context/index.md
- {{$RP1_ROOT}}/context/concept_map.md
- {{$RP1_ROOT}}/context/architecture.md
- {{$RP1_ROOT}}/context/modules.md
- {{$RP1_ROOT}}/context/patterns.md
The knowledge from feature "{{FEATURE_ID}}" has been captured into the KB.
Future agents will benefit from these learnings.
| Parameter | Default | Purpose |
|---|---|---|
| RP1_ROOT | .rp1/ | Root directory for KB artifacts |
| CODEBASE_ROOT | . | Repository root to analyze |
| EXCLUDE_PATTERNS | node_modules/,.git/,build/,dist/ | Patterns to exclude from scanning |
CRITICAL - Keep Output Concise:
Example of CORRECT output:
First-time KB generation with parallel analysis (10-15 min)
Analyzing... (Phase 2/5)
Knowledge Base Generated Successfully
[Final Report as shown above]
Example of INCORRECT output (DO NOT DO THIS):
Checking for state.json...
state.json not found, proceeding with first-time build
Running git rev-parse HEAD to get commit...
Commit is 475b03e...
Spawning kb-spatial-analyzer agent...
Parsing spatial analyzer output...
Found 90 files in index_files category...
Now spawning 4 parallel agents...
Spawning kb-concept-extractor...
Spawning kb-architecture-mapper...
Spawning kb-module-analyzer...
etc. (too verbose!)
No changes detected:
First-time build (no state.json - full analysis):
Incremental update (commit changed - changed files only):