with one click
spec-discovery
// [General] Use when you need reverse-engineer a complete, tech-agnostic specification bundle from an existing codebase.
// [General] Use when you need reverse-engineer a complete, tech-agnostic specification bundle from an existing codebase.
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | spec-discovery |
| version | 3.3.0 |
| last_reviewed | "2026-04-21T00:00:00.000Z" |
| description | [General] Use when you need reverse-engineer a complete, tech-agnostic specification bundle from an existing codebase. |
[BLOCKING] Execute skill steps in declared order. NEVER skip, reorder, or merge steps without explicit user approval. [BLOCKING] Before each step or sub-skill call, update task tracking: set
in_progresswhen step starts, setcompletedwhen step ends. [BLOCKING] Every completed/skipped step MUST include brief evidence or explicit skip reason. [BLOCKING] If Task tools are unavailable, create and maintain an equivalent step-by-step plan tracker with the same status transitions.
Goal: Reverse-engineer tech-agnostic spec bundle from existing codebase. Stable output at docs/specs/{app-bucket}/{system-name}/ โ change history in SPEC-CHANGELOG.md.
Modes:
| Mode | Trigger | Input | Output |
|---|---|---|---|
init | docs/specs/{app-bucket}/{system-name}/ not found | Full codebase | Complete spec bundle from scratch |
update | docs/specs/{app-bucket}/{system-name}/ exists + git diff provided | Changed modules | Re-extract impacted modules ร phases only |
audit | Explicit request | Source timestamps vs spec last_extracted | SPEC-AUDIT-{date}.md |
Workflow: /scout โ /plan (operation-group ร phase tasks) โ /plan-review โ /why-review โ /plan-validate โ /spec-discovery (+ Phase F) โ /review-changes โ /review-artifact โ /watzup โ /workflow-end
Key Rules:
docs/project-reference/spec-principles.md โ shared spec quality standards, completeness checklists, AI-implementability criteria (read Section 4 completeness checklists before any extraction phase)sum(operation_groups ร phase_count)TaskList FIRST, read completeness tracker, NEVER re-scout/re-plan[Source: file:line] โ mark [UNVERIFIED] not blankdocs/specs/{app-bucket}/{system-name}/) โ version history in SPEC-CHANGELOG.md, NOT in folder nameBefore reading any code, use AskUserQuestion. 3 required (โ
) โ MUST confirm before proceeding. 4 optional โ propose auto-default, user can override:
| Dimension | Question | Auto-Default |
|---|---|---|
| Scan scope โ | Full system (all modules) OR a specific module/service/feature area? | โ must confirm |
| Mode โ | Fresh init (no existing specs) OR update (specs exist, re-extract impacted) OR audit (check staleness)? | โ must confirm |
| System name โ | What is the canonical system name? Choose once โ this becomes the stable docs/specs/{app-bucket}/{system-name}/ path (e.g., myapp/orders, myapp/users). | โ must confirm |
| Output depth | Full spec bundle (all 6 phases) OR targeted (select phases)? | Full bundle (all 6 phases) |
| Focus areas | Which of: domain model / business rules / API contracts / events / UI flows? | All phases |
| Source entry | Where to start? (e.g., main entry files, a named module, a specific directory) | Top-level service directory |
| Scale hint | Approximately how many modules/services does this codebase have? (rough estimate) | Scout will determine from registry |
Stable Path Contract: The folder
docs/specs/{app-bucket}/{system-name}/is the permanent home for this system's spec bundle. NEVER include dates in the path. All change history is captured indocs/specs/{app-bucket}/{system-name}/SPEC-CHANGELOG.md. Existing extractions inspecs/{date}-*folders are legacy โ do NOT migrate or delete them; start fresh at the stable path for new runs.
Scale routing: If scope is 1โ3 modules โ single-session extraction. If scope is 4+ modules โ MUST use sub-agent parallel extraction (see Sub-Agent Pattern). If scope is entire large system โ MUST use incremental coverage: one module per session.
[BLOCKING SCALE GATE] If
module_count โฅ 4at the end of Step 2 (Plan): you MUST use sub-agent parallel extraction. Attempting single-session inline extraction with 4+ modules is a workflow violation. Do NOT proceed past Step 2 without spawning sub-agents.
When mode = update, skip Steps 1-2 (Scout + Plan). Instead:
docs/specs/{app-bucket}/{system-name}/00-module-registry.md โ load module catalogdocs/specs/{app-bucket}/{system-name}/extraction-plan.md โ load phase coverage mapdocs/specs/{app-bucket}/{system-name}/README.md completeness tableMap changed source files โ impacted modules ร phases:
| Changed File Pattern | Impacted Module(s) | Impacted Phase(s) |
|---|---|---|
{Module}/Domain/Entities/** | That module | Phase A (domain model) + Phase A.ERD |
{Module}/Application/UseCaseCommands/** | That module | Phase B (business rules โ validation), Phase C (API contracts) |
{Module}/Application/UseCaseEvents/** | That module | Phase D (events) |
{Module}/Service/Controllers/** | That module | Phase C (API contracts) |
{Module}/Persistence/** | That module | Phase B (data constraints) |
Frontend {feature}/** | Mapped module | Phase E (user journeys) |
| Bus message files | All consuming modules | Phase D (events) |
TaskCreate for each impacted module ร phase combination. These replace the full NรM plan for update mode.
Execute per-task deep investigation for each impacted task (same READ โ TRACE โ EXTRACT โ WRITE โ VERIFY protocol as init mode).
After all update tasks complete:
## [{date}] โ Update
- Scope: {list of re-extracted modules}
- Phases re-extracted: {list of phases}
- Triggered by: git diff {git_ref} OR workflow-spec-driven-dev update mode
- Source files changed: {list of changed files}
- Spec files updated: {list of updated spec files}
When mode = audit:
For each module in docs/specs/{app-bucket}/{system-name}/:
{module}/README.md frontmatter โ last_extracted datelast_extracted dateFor each module:
git log --since="{last_extracted}" --name-only -- {source_path}| Changed Files Found | Classification | Action |
|---|---|---|
| Entity files | Phase A stale | Flag for re-extraction |
| Validator/command files | Phase B stale | Flag for re-extraction |
| Controller files | Phase C stale | Flag for re-extraction |
| Event/bus files | Phase D stale | Flag for re-extraction |
| Frontend files | Phase E stale | Flag for re-extraction |
Output: docs/specs/{app-bucket}/{system-name}/SPEC-AUDIT-{date}.md:
# Spec Audit Report โ {date}
**System:** {system-name}
**Audit Run:** {date}
## Module Staleness Summary
| Module | Last Extracted | Source Changes Since | Stale Phases | Action |
| ------------------- | -------------- | ------------------------------------ | ------------ | ------------------ |
| M01 OrderManagement | 2026-04-19 | ValidateOrderService.cs (2026-04-20) | Phase B | Re-extract Phase B |
| M04 ProductCatalog | 2026-04-19 | None | โ | โ
Current |
## Recommended Update Scope
Estimated update effort: {N} modules ร {M} phases = {X} re-extraction tasks
Run: `workflow-spec-driven-dev update` scoped to: {list of stale modules}
Goal: Build complete codebase picture at highest level BEFORE reading any module โ foundation for plan.
HARD GATE: No extraction until Module Registry produced. Missing registry โ plan misses modules โ incomplete spec.
Create docs/specs/{app-bucket}/{system-name}/00-module-registry.md:
---
system_name: { system-name }
extracted: { date }
last_updated: { date }
---
# Module Registry
Generated: {date} | Scope: {full-system / module-scoped}
## System Summary
[2-3 sentences: what this system does, who uses it, approximate scale]
## Module Catalog
| # | Module Name | Responsibility | Layer Structure | File Count (est.) | Data Store | Integration Points |
| --- | ----------- | -------------- | --------------- | ----------------- | ---------- | ------------------ |
| 1 | ... | ... | ... | ... | ... | ... |
## Cross-Cutting Concerns
| Concern | Implementation Location | Consumed By |
| ------- | ----------------------- | ----------- |
## Integration Boundary Map
[Which modules communicate, via what mechanism, in what direction]
## Data Store Ownership
| Store Type | Owner Module(s) | Access Pattern |
| ---------- | --------------- | -------------- |
Goal: Count ALL operations BEFORE plan. Task count MUST derive from inventory โ not module count alone.
HARD GATE: Do NOT proceed Step 2 until Use Case Inventory written for every module. Plan based only on
module_count ร phase_countsystematically misses operations in large modules.
For each module discovered in Step 1, run the following grep commands โ use ALL patterns that apply to this codebase's architecture:
Write-side entry points (operations that change data state):
# CQRS command handlers
grep -r "ICommandHandler\|CommandHandler\|ICommand\b" {module_path}/ --include="*.cs" -l
# REST/MVC mutating endpoints
grep -r "\[HttpPost\]\|\[HttpPut\]\|\[HttpPatch\]\|\[HttpDelete\]" {module_path}/ --include="*.cs" -l
# GraphQL mutations
grep -r "Mutation\|[Mm]utation[Rr]esolver" {module_path}/ -l
# Event consumers (event-driven operations)
grep -r "IConsumer\|EventHandler\|MessageHandler\|@KafkaListener\|IMessageConsumer" {module_path}/ -l
# Generic fallback (class-based handlers)
grep -r "class.*Handler\|class.*Command\|class.*Processor" {module_path}/ -l
Read-side entry points (operations that query data):
# CQRS query handlers
grep -r "IQueryHandler\|QueryHandler\|IQuery\b" {module_path}/ --include="*.cs" -l
# REST GET endpoints
grep -r "\[HttpGet\]" {module_path}/ --include="*.cs" -l
# GraphQL query resolvers
grep -r "class.*Query\b\|[Qq]uery[Rr]esolver" {module_path}/ -l
# Generic fallback
grep -r "class.*Query\|class.*Reader\|class.*Finder" {module_path}/ -l
Background + scheduled operations:
grep -r "BackgroundJob\|IJob\b\|ScheduledTask\|ICronJob\|BackgroundService\|IHostedService" {module_path}/ -l
Before writing any user stories in Phase E, you MUST know WHO the actors are. Run for each module:
# Authorization attributes and policies
grep -r "\[Authorize\]\|\[Permission\]\|RequirePermission\|HasPermission\|IsInRole\|RequireRole" {module_path}/ -l
# Role/permission enums and constants
grep -r "enum.*Role\|enum.*Permission\|RoleEnum\|PermissionEnum\|.*Roles\s*=" {module_path}/ -l
# Policy definitions
grep -r "AddPolicy\|AddAuthorization\|PolicyBuilder" {module_path}/ -l
Build actor catalog: list each unique role/actor type found, permission scope, operations they can perform.
Append to docs/specs/{app-bucket}/{system-name}/00-module-registry.md (add after the Module Catalog table):
## Use Case Inventory
| Module | Write Ops (N) | Read Ops (M) | Event-Driven (K) | Background (J) | Total (N+M+K+J) | Actor Roles |
| --------- | ------------- | ------------ | ---------------- | -------------- | --------------- | ----------------- |
| Orders | 12 | 8 | 3 | 2 | 25 | Customer, Admin |
| Users | 6 | 4 | 1 | 0 | 11 | Admin, HR Manager |
| **Total** | | | | | **36** | |
## Actor Catalog
| Actor / Role | Permission Scope | Source |
| ------------ | ------------------ | ------------------- |
| {role-name} | {what they can do} | [Source: file:line] |
[GATE โ BLOCKING before Step 2]:
An operation group is a batch of โค20 related operations from the Use Case Inventory for a single module. Group by functional area (e.g., "Order CRUD", "Order Fulfillment", "Order Reporting"). If no clear functional grouping exists, batch alphabetically in groups of โค20.
A module with 80 operations โ โฅ4 operation groups โ โฅ4 Phase C tasks for that module.
Goal: Ordered task list decomposing extraction into smallest independent units. Prevents context overrun.
HARD GATE:
TaskCreateMUST be called for EVERY task BEFORE extraction begins. No extraction without complete task list.
Rule 1: One task per operation group per phase. Operation groups from Use Case Inventory (Step 1.5). Module with 80 ops โ 4 groups ร 4 phases = 16 tasks.
Minimum task count formula: sum(operation_groups_per_module) ร phase_count. NEVER module_count ร phase_count โ undercounts large modules โ #1 cause of <10% coverage.
Task format: "Extract [Phase Name] for [Module Name] โ [Group Name] ([N] ops)".
Rule 2: Scope each task โค50 files / โค20 operations.
20 ops in one group โ split:
"Extract API Contracts for Order Module (Part 1: Fulfillment)","Part 2: Reporting".
Rule 3: Dependency-order tasks. Phase A (Domain Model) MUST complete before Phase B (Business Rules) for same module.
Rule 4: Prioritize by value. Core business domain first โ infrastructure/utility last.
Rule 5: Each task = focused investigation. NEVER extract all phases for module in single context pass.
The plan produces:
TaskCreate) โ one task per operation group ร phase combinationdocs/specs/{app-bucket}/{system-name}/extraction-plan.md file tracking which tasks cover which modules# Extraction Plan
## Task Breakdown
Phase 1 (Domain Model) โ {N} tasks
- [ ] Extract Domain Model: Module A
- [ ] Extract Domain Model: Module B
...
Phase 2 (Business Rules) โ {N} tasks
- [ ] Extract Business Rules: Module A (Part 1: Validation)
- [ ] Extract Business Rules: Module A (Part 2: State Machines)
...
## Dependency Map
[Which tasks must complete before others can start]
## Completion Tracker
| Task | Status | Output File | Evidence Lines |
| ---- | ------ | ----------- | -------------- |
[BLOCKING GATE] Before proceeding to
/plan-review:
- Run
TaskListโ count all created extraction tasks- Compute expected minimum using BOTH checks:
- Check A:
sum(operation_groups_per_module) ร phase_countโ derived from Use Case Inventory (Step 1.5)- Check B: Phase C task count โฅ Grand Total (N+M+K+J) from Use Case Inventory โ every counted operation must map to a Phase C task
- If
TaskListcount < Check A result: plan is INCOMPLETE โ create missing operation-group tasks- If Phase C tasks cover fewer operations than Check B: plan is INCOMPLETE โ create missing tasks
- If both checks pass: proceed to
/plan-reviewNEVER use
module_count ร phase_countas the minimum โ this undercounts large modules. An incomplete task list is not a plan โ it is a guarantee of <10% coverage on large modules.
Goal: Execute each task as focused, depth-first investigation. Read ALL relevant files before writing any spec line.
HARD GATE: Read target files BEFORE writing. NEVER write spec content from memory or assumption.
For each extraction task, follow this sequence:
1. READ โ Read all source files in this task's scope (grep โ read โ understand)
2. TRACE โ Trace code paths: what calls what, what triggers what, what validates what
3. EXTRACT โ Extract the relevant spec content (only what this task covers)
4. WRITE โ Write the extracted content to the spec file with [Source: file:line] on every claim
5. VERIFY โ Re-read the written spec against the source. Any claim without a source โ mark [UNVERIFIED]
6. COMPLETE โ Mark the task completed. Move to next task.
| Task Type | What to Read |
|---|---|
| Domain Model | Entity/model files, value object files, aggregate roots, enum definitions, foreign key / navigation property / association fields (for relationship mapping) |
| Business Rules | Validator files, guard clauses in command handlers, entity invariant methods, policy files |
| API Contracts | All application-layer entry points โ read in this priority order: 1. CQRS (if present): command handler files ( UseCaseCommands/) + query handler files (UseCaseQueries/) + event consumer files (UseCaseEvents/) + background job files2. REST/MVC: controller files + request/response DTO files 3. GraphQL: resolver files (mutations = write operations, queries = read operations) 4. Scheduled: job/scheduler/cron files Every operation in the Use Case Inventory (Step 1.5) MUST produce exactly one Phase C spec entry. Missing entries = incomplete spec. |
| Integration & Events | Message publisher files, message consumer files, scheduler files, external client files |
| User Journeys | Three source types โ all required: 1. Backend operations (from Phase C completed spec): every write operation โ one "I want to perform action" story; every read/GET operation with filter params โ one "I want to search/view/filter" story. 2. UI routes (if frontend exists): route files โ component files โ templates. Each screen/route = one story about what the user can see or do there. 3. Auth/permission attributes (actor catalog from Step 1.5): confirm every actor has โฅ1 story. Format required for every story: "As a [actor from catalog], I want to [action], so that [outcome]." |
Critical: 1000+ files โ strict per-task discipline prevents context overrun.
[BLOCKING โ MANDATORY at every session start or after context compaction]
TaskListBEFORE any other action โ findin_progress/pendingtasks; NEVER create duplicates- Read
docs/specs/{app-bucket}/{system-name}/README.mdcompleteness table โ identify โ modules- Skip re-extraction for โ modules โ append only to incomplete sections
- Continue from first non-completed task
- NEVER re-run Step 1 (Scout) or Step 2 (Plan) in resumed session โ Registry + task list already exist
For each entity/aggregate in scope:
### {EntityName}
- **Purpose:** [one sentence โ what business concept this represents]
- **Identity:** [auto-generated / natural key: field name]
- **Attributes:**
| Name | Type | Required | Constraint | Business Meaning |
|------|------|----------|------------|-----------------|
- **Lifecycle:** [created-modified-deleted / append-only / state machine: list states]
- **Invariants:** [list of rules the entity always enforces โ plain language]
- **Domain Events:** [what significant things happen when this entity changes state]
[Source: path/to/entity-file.ext:line_range]
For each value object:
### {ValueObjectName}
- **Represents:** [what real-world concept]
- **Attributes:** [name | type | constraint]
- **Immutable:** yes/no
- **Validation:** [what makes an instance valid]
[Source: path/to/file.ext:line_range]
For each aggregate:
### {AggregateName} Aggregate
- **Root:** {EntityName}
- **Members:** [list]
- **Consistency Boundary:** [what changes must happen atomically]
- **Invariants Enforced:** [cross-entity rules the aggregate protects]
[Source: path/to/file.ext:line_range]
Why: ERD is foundation of entire spec bundle โ all phases (business rules, API contracts, events) reference entity relationships. Generate once per module group, after Phase A entity extraction, before Phase B.
Output file: docs/specs/{app-bucket}/{system-name}/01-domain-erd.md (append when multiple modules contribute)
Add the following YAML frontmatter at the top of every spec output file (A/B/C/D/E and ERD):
---
module: { module-name }
phase: { A|B|C|D|E }
last_extracted: { date }
extraction_mode: init|update
---
ERD generation protocol:
1. Collect all entities extracted in Phase A for this module
2. For each entity: identify its relationships by reading foreign-key fields,
navigation/association properties, and collection fields
3. Determine cardinality for each relationship (read source โ do NOT assume)
4. Generate Mermaid erDiagram block
5. Append to 01-domain-erd.md with [Source] citations
Mermaid ERD template:
## {ModuleName} Domain ERD
%% [AGGREGATE: {AggregateName}] โ root entity listed first
```mermaid
erDiagram
EntityA {
string id PK
string name
string status
}
EntityB {
string id PK
string entityAId FK
number quantity
}
EntityA ||--o{ EntityB: "contains"
EntityA }o--|| EntityC: "belongs to"
```
[Source: path/to/entity-files.ext:line_range]
ERD rules (tech-agnostic contract):
| โ Forbidden | โ Use Instead |
|---|---|
ORM/language types (Guid, List<T>, int?) | string, number, boolean, date, list, map |
Class name suffixes (OrderEntity, UserModel) | Business name only (Order, User) |
Framework annotations ([Required], @Column) | Omit โ captured in Phase A attribute constraints |
Module name prefixes (App_Orders) | Business name only (Orders) |
Cardinality notation:
| Notation | Meaning |
|---|---|
||--|| | Exactly one to exactly one |
||--o{ | Exactly one to zero-or-many |
}o--o{ | Zero-or-many to zero-or-many |
||--|{ | Exactly one to one-or-many |
Cross-module references: If an entity references an entity from another module (not in current scope), add a stub entity with a comment:
ExternalEntity {
string id PK
}
%% [CROSS-REF: {source-module-name} โ not extracted in this scope]
LocalEntity }o--|| ExternalEntity : "references"
ERD completeness gate (before marking Phase A.ERD task complete):
%% [AGGREGATE: ...] comment[Source: file:line] present for relationship evidenceCircular Dependency Handling (ERD):
When entity A has a FK โ entity B AND entity B has a FK โ entity A (mutual reference), do NOT omit one side. Represent both directions with directionality comments:
```mermaid
erDiagram
EntityA {
string id PK
string entityBId FK
}
EntityB {
string id PK
string entityAId FK
}
EntityA }o--|| EntityB: "primary owner"
EntityB }o--o| EntityA: "back-reference (optional)"
%% [CIRCULAR: EntityA โ EntityB โ primary ownership in EntityB]
```
Identify ownership direction (FK created first / "owns" relationship) vs navigational back-reference. Document with
%% [CIRCULAR: ...]comment.
## {ModuleName} โ Business Rules
### Validation Rules: {OperationName}
| Field | Rule | Error Condition | Error Message |
| ----- | ---- | --------------- | ------------- |
[Source: path/to/validator.ext:line_range]
### Authorization Rules
| Operation | Who Can Perform | Condition |
| --------- | --------------- | --------- |
[Source: path/to/policy.ext:line_range]
### Invariants
| # | Invariant | Always True Because | Enforcement |
| --- | --------- | ------------------- | ----------- |
[Source: path/to/entity.ext:line_range]
### Calculations
| Name | Inputs | Formula / Description | Output |
| ---- | ------ | --------------------- | ------ |
[Source: path/to/file.ext:line_range]
### State Machine: {EntityName} Lifecycle
States: [list with descriptions]
Transitions:
| From State | Event/Trigger | Guard Condition | To State |
|-----------|---------------|-----------------|----------|
[Source: path/to/file.ext:line_range]
## {ModuleName} โ Operations
### {OperationName}
- **Purpose:** [one sentence]
- **Transport:** [HTTP method + path / scheduled / message consumer / CLI]
- **Auth Required:** yes/no | Role: [role] | Permission: [permission]
- **Idempotent:** yes/no โ [why]
- **Input:**
| Field | Type | Required | Constraint | Description |
|-------|------|----------|------------|-------------|
- **Output (success):**
| Field | Type | Description |
|-------|------|-------------|
- **Errors:**
| Code | Condition | Retryable |
|------|-----------|-----------|
[Source: path/to/controller.ext:line_range]
## Published Events
### {EventName}
- **Trigger:** [what causes this to be published]
- **Payload:**
| Field | Type | Description |
|-------|------|-------------|
- **Ordering:** guaranteed / best-effort
[Source: path/to/publisher.ext:line_range]
## Consumed Events
### {EventName}
- **Producer:** [system or module name]
- **Processing:** [what this system does when received]
- **Idempotency:** [how duplicate delivery is handled]
- **Failure handling:** [retry? dead-letter? discard?]
[Source: path/to/consumer.ext:line_range]
## Scheduled Jobs
| Job | Schedule | Purpose | Side Effects | Abort Condition |
| --- | -------- | ------- | ------------ | --------------- |
[Source: path/to/job.ext:line_range]
Phase E MUST produce one entry per discovered operation (from Phase C) plus one entry per UI screen. Minimum output = Phase C operation count for this module.
Use actor catalog from Step 1.5 โ every story MUST reference a real actor from that catalog, not assumed role.
User story template (use for each backend operation and each UI screen):
## Journey: {JourneyName}
- **User Story:** As a {actor from actor catalog}, I want to {action verb + object}, so that {business outcome}.
- **Actor:** {exact role name from actor catalog}
- **Operation Type:** write | read | event-driven | background | ui-screen
- **Trigger:** {what starts this journey โ user click, API call, system event, schedule, or message}
- **Preconditions:** {what must already exist or be true before this journey begins}
- **Happy Path:**
1. {Step 1 โ observable user or system action}
2. {Step 2}
...
- **Alternative Paths:**
- {Condition A} โ {what happens instead of happy path}
- {Error/invalid input} โ {error message or fallback behavior}
- **Outcome:** {what the actor achieves or what system state changes}
- **Acceptance Criteria:**
- GIVEN {precondition} WHEN {action} THEN {observable outcome}
- GIVEN {precondition} WHEN {invalid action} THEN {error outcome}
[Source: path/to/handler-or-component.ext:line_range]
Per-task Phase E completeness checklist (check BEFORE marking task complete):
[Source: file:line] โ no journeys from memory or assumptionAfter all per-module tasks complete:
[BLOCKING] Do NOT write Phase F bundle files until all 4 gates pass. A bundle assembled over coverage gaps is misleading โ it implies completeness that doesn't exist.
| Gate | Check | Pass Condition | Failure Action |
|---|---|---|---|
| C-gate | Phase C total entries across all modules | โฅ Grand Total from Use Case Inventory (Step 1.5) | Create fix task: "Fill Phase C gap: {module} missing {N} operation entries" |
| E-gate | Phase E journey count across all modules | โฅ Phase C total entry count | Create fix task: "Fill Phase E gap: {module} missing {N} user stories" |
| Actor-gate | Every actor in actor catalog | Has โฅ1 Phase E journey | Create fix task: "Add stories for actor: {role} โ currently 0 journeys" |
| Source-gate | Every Phase C + Phase E entry | Has [Source: file:line] | Mark [UNVERIFIED] โ do NOT silently pass |
If any gate fails: create fix tasks, execute them, then re-check ALL 4 gates before proceeding. Re-checking runs in the main session โ spawn fresh sub-agent only if re-check still fails after one fix cycle.
docs/specs/{app-bucket}/{system-name}/01-domain-erd.md exists and covers all modules โ the ERD is the foundational artifact; missing ERD = incomplete spec bundledocs/specs/{app-bucket}/{system-name}/06-reimplementation-guide.md โ system overview, build order, architecture constraints, data migration notesdocs/specs/{app-bucket}/{system-name}/README.md โ index with completeness status table[REQUIRED] Doc Network Links in README.md (index):
Every spec bundle README.md MUST include a
## Related Documentationsection:## Related Documentation **Business Feature Doc:** `docs/business-features/{Module}/README.md` _(primary stakeholder view; Sections 1-14 business context, Section 15 test cases)_ **QA Spec Dashboard:** `docs/specs/{Module}/README.md` _(TC execution status, integration test traceability)_ **Integration Tests:** `src/Services/{ServiceName}/{ServiceName}.IntegrationTests/` _(subcutaneous tests; each test annotated with [Trait("TestSpec", "TC-{MODULE}-{NNN}")])_If the business feature doc or QA dashboard doesn't exist yet, write the path with
(not yet created). The path is the navigational intent โ it guides the next person to create it.
[NOT EXTRACTED โ scope excluded]Create docs/specs/{app-bucket}/{system-name}/SPEC-CHANGELOG.md after first init run:
# Spec Changelog: {system-name}
> Change history for the spec bundle at `docs/specs/{app-bucket}/{system-name}/`.
> Each entry records what was extracted, when, and why.
## [{date}] โ Initial Extraction (init)
- **Scope:** {full system / module-scoped}
- **Modules:** {N} modules
- **Phases:** {list of phases extracted}
- **Triggered by:** Initial spec-discovery init run
- **Spec files created:** {count}
- **Total lines:** {approx}
- **Open questions:** {count} (see README.md)
Append after every subsequent update/audit run per Step 0.5.5 format.
After Phase F bundle assembly, generate a README.md for each module folder:
Output: docs/specs/{app-bucket}/{system-name}/{module-id}-{module-name}/README.md
Purpose: Bridge between engineering spec and stakeholder-facing feature docs. Provides navigable summary in 17-section format, linking to detailed A/B/C/D/E files for depth.
---
module: { module-name }
system: { system-name }
last_extracted: { date }
phases_covered: [A, B, C, D, E] # list only completed phases
---
# {Module Name} โ Spec Summary
> Tech-agnostic spec. All entity/operation names are business names only.
> Full details: [A-domain-model.md](./A-domain-model.md), [B-business-rules.md](./B-business-rules.md), etc.
## Quick Navigation
| Section | Content | Detail File |
| ------------------------- | ------------------------- | -------------------------------------------- |
| 1. Domain Model Summary | Key entities and ERD | [A-domain-model.md](./A-domain-model.md) |
| 2. Business Rules Summary | Top validation rules | [B-business-rules.md](./B-business-rules.md) |
| 3. Operations Summary | Key API operations | [C-api-contracts.md](./C-api-contracts.md) |
| 4. Events Summary | Published/consumed events | [D-events.md](./D-events.md) |
| 5. User Journeys Summary | Key user flows | [E-user-journeys.md](./E-user-journeys.md) |
## Domain Model (Summary)
{Top 3-5 entities with 1-sentence purpose each}
{Mermaid erDiagram โ same as in A-domain-model.md, abbreviated to core entities only}
## Business Rules (Top 5)
{Top 5 most important rules, each 1 line, linking to full rule in B-business-rules.md}
## Key Operations (Top 5)
{Top 5 operations โ purpose, method+path, auth required โ linking to C-api-contracts.md}
## Events (Summary)
{Published events count, consumed events count โ link to D-events.md}
## Open Questions
{Any [UNVERIFIED] items or OQ-XX items relevant to this module}
Scope: Generate README.md only for modules that have at least Phase A or Phase B complete. Skip modules with only Phase E (UI journeys only).
4+ modules โ use sub-agents for parallel module extraction:
[BLOCKING โ PARALLEL SPAWN PROTOCOL] Spawn ALL module sub-agents in a SINGLE response with multiple
Agenttool calls. Never spawn module sub-agents one at a time sequentially โ that eliminates the parallelism benefit and extends wall-clock time by Nร for an N-module system.Agent(module=Orders, ...) โ all in ONE message Agent(module=Users, ...) โ same response Agent(module=Billing, ...) โ same responseEach sub-agent is independent โ no shared mutable state, no ordering dependency between modules.
Sub-Agent Prompt Template:
You are extracting the spec for module: {ModuleName}.
Module Registry: docs/specs/{app-bucket}/{system-name}/00-module-registry.md โ read this FIRST to understand your module's boundaries and the full system context.
Your assigned tasks: {list of TaskUpdate IDs for this module โ call TaskList to confirm}
Output path: docs/specs/{app-bucket}/{system-name}/{module-id}-{module-name}/
---
MANDATORY PROTOCOLS โ apply throughout your entire execution:
> Critical Thinking & Anti-Hallucination โ every claim needs traced proof. Confidence >80% โ write with [Source]. 60-80% โ mark [NEEDS-VERIFY]. <60% โ mark [UNVERIFIED]. Never present a guess as fact. Never invent field names, method signatures, or business rules.
> Evidence-Based Reasoning โ BLOCKED until: source file read AND file:line citation exists. Forbidden words without proof: "obviously", "I think", "should be", "probably". If you cannot find evidence: write "Insufficient evidence. Verified: [...]. Not verified: [...]."
> Incremental Persistence โ MANDATORY: after EACH task completes, append findings to your output file immediately. Never hold content in memory across tasks. Context cutoff loses all in-memory work โ disk writes survive.
> Cross-Scope Boundary โ HARD GATE: Do NOT read files outside your assigned module's scope. If you discover a dependency on an unlisted module, note it as [CROSS-REF: {module-name} โ not in scope] in the spec output and stop โ do not follow the reference into that module.
> Tech-Agnostic Output โ FORBIDDEN in all spec output: framework names (e.g., Entity Framework, Django), ORM types, language generics (List<T>), nullable annotations (string?), file paths or class names from source, architectural pattern names (CQRS, middleware). Use business-meaning descriptions only.
---
**Use Case Inventory for your module** (from Step 1.5 โ read from 00-module-registry.md):
> Your module has: Write Ops (N) = {N}, Read Ops (M) = {M}, Event-Driven (K) = {K}, Background (J) = {J}, **Total = {N+M+K+J}**.
> Phase C MUST produce exactly {N+M+K+J} spec entries for your module.
> Phase E MUST produce โฅ {N+M+K+J} user stories for your module.
> If your assigned tasks cover fewer than {N+M+K+J} operations: your task list is incomplete โ STOP and report the gap to the main agent before proceeding.
Per-task execution protocol โ follow in order for EACH assigned task:
1. READ โ grep to narrow file set to this task's scope; read only matching files
2. TRACE โ trace code paths: what calls what, what validates what, what triggers what
3. EXTRACT โ extract spec content for this phase/module only
4. WRITE โ append to output file immediately with [Source: file:line] on every claim
5. VERIFY โ re-read written spec vs source; mark [UNVERIFIED] for any claim without traceable source
6. COMPLETE โ call TaskUpdate to mark task completed; move to next task
**Phase A.ERD is mandatory after all Phase A entity extraction tasks complete:**
After writing all entity/aggregate descriptions, generate a Mermaid `erDiagram` and append it to `docs/specs/{app-bucket}/{system-name}/01-domain-erd.md`. The ERD captures all entities in your assigned module, their key attributes (using only `string`/`number`/`boolean`/`date`/`list`/`map` types), relationships with cardinality, and aggregate boundaries (`%% [AGGREGATE: ...]`). Mark any cross-module entity references as `%% [CROSS-REF: {module-name} โ not in scope]`. The ERD is the foundational artifact โ without it, Phase B and beyond lack a shared entity reference.
Never skip ahead. Never accumulate across tasks. Each task is a fresh investigation.
Before Phase F bundle assembly, quality-review all generated spec files:
For each spec file:
[Source: file:line] citation[UNVERIFIED] โ not left blankIf any check fails:
"Fix spec quality gap: [specific issue] in [spec file]"[UNVERIFIED][BLOCKING] After any fix loop iteration, the main agent has rationalization bias toward its own output. Do NOT re-review inline.
Spawn a NEW
Agenttool call (subagent_type: "code-reviewer") with:
- Target: all generated spec files in
docs/specs/{app-bucket}/{system-name}/- Protocol: re-read ALL spec files from scratch; check every
[UNVERIFIED]item; flag any tech-specific term; verify every entity/operation has at least one[Source: file:line]- Report path:
plans/reports/spec-discovery-review-round{N}-{date}.mdRules:
- Max 2 fresh sub-agent rounds โ if still failing after round 2, escalate via
AskUserQuestionwith:
- Explicit list of all remaining
[UNVERIFIED]items with the file/section where each occurs- Three options:
(1) Accept with gaps โ mark all remaining [UNVERIFIED] items explicitly and ship; (2) Narrow scope โ re-run extraction only on the 1-2 files still failing (new task); (3) Manual review โ hand off gap list to human reviewer- Do NOT silently loop beyond round 2 โ escalation to user is mandatory
- PASS = fresh sub-agent finds zero
[UNVERIFIED]items without an explicit exclusion reason AND zero tech-specific terms- NEVER reuse a sub-agent across rounds โ every iteration spawns a NEW
Agentcall
Every spec bundle document MUST be free of:
| โ Forbidden | โ Use Instead |
|---|---|
| Framework names (Express, Django, etc.) | "HTTP router", "request handler", "middleware" |
| ORM/database type names | "string", "number", "boolean", "date", "list", "map" |
Language generics (List<T>, Optional<>) | "list of X", "optional X" |
Nullable annotations (string?) | "X (optional)" |
| Architectural pattern names (CQRS handler, middleware) | "command processor", "request interceptor" |
| File paths or class names from source | Business-name descriptions only |
| Stack-specific patterns (IoC container, DI) | "dependency injection", "service registry" |
Every spec claim MUST have source reference:
[Source: path/to/file.ext:line_number]
BLOCKED: Any spec section without source evidence MUST be marked [UNVERIFIED โ needs manual review]. NEVER invent or assume.
When the user requests only specific phases:
| User Goal | Phases to Run |
|---|---|
| "I need the data model only" | Scout + Plan + Phase A + Phase A.ERD (always included with A) |
| "Extract the API contracts" | Scout + Plan + Phase A + Phase A.ERD + Phase C |
| "Document the business rules" | Scout + Plan + Phase A + Phase A.ERD + Phase B |
| "Generate acceptance criteria from code" | Scout + Plan + Phase A + Phase A.ERD + Phase E |
| "Extract the event flow" | Scout + Plan + Phase A + Phase A.ERD + Phase D |
| "Full reimplementation spec" | Scout + Plan + All phases (A.ERD always runs after A) |
Phase A.ERD is always mandatory when Phase A runs โ the ERD is the foundational artifact every downstream phase references. Never skip it, even in selective mode.
Selective Phase exception: Phase A may be omitted ONLY if
A-domain-model.mdalready exists for this module from a prior init run โ in which case Phase A.ERD also already exists; do NOT re-generate it. Reference the existing ERD file instead. If Phase A IS being run now (new or re-extraction), Phase A.ERD MUST run immediately after.
Scout and Plan ALWAYS mandatory โ regardless of phase selection.
For systems too large for single session (>10 modules):
Track completeness in docs/specs/{app-bucket}/{system-name}/README.md. Each session appends โ NEVER overwrites.
[BLOCKING] After completing, use AskUserQuestion โ DO NOT skip:
| Skill | Relationship | When to Call |
|---|---|---|
/feature-docs | Consumer โ ingests spec bundle to produce 17-section business doc | After spec-discovery init/update, to create the stakeholder layer |
/tdd-spec | Indirect consumer โ feature-docs Section 15 feeds tdd-spec | After feature-docs, to generate test cases from the spec |
/integration-test | End consumer โ TCs from tdd-spec become integration tests | After tdd-spec, to generate actual test code |
/docs-update | Orchestrator โ runs spec-discovery update as Phase 2.5 | When code changes need full doc sync (calls spec-discovery automatically) |
/review-changes | Trigger โ detects code changes and surfaces doc staleness | Run after code changes; it will suggest /docs-update if stale |
/tdd-spec [direction=sync] | Dashboard sync โ syncs QA spec dashboard at docs/specs/{Module}/README.md | After tdd-spec update, to keep dashboard current |
Outside workflow: follow chain to keep spec layers in sync. Skip steps already current (check file timestamps vs git diff).
spec-discovery (you are here)
โ
โโ [REQUIRED] โ /feature-docs [update mode]
โ Updates business docs (docs/business-features/) from the new spec bundle.
โ Skip if: feature docs were updated in same session or no business doc exists yet for this module.
โ
โโ [REQUIRED] โ /tdd-spec [update mode]
โ Updates test cases in feature doc Section 15 to reflect changed domain model or rules.
โ Skip if: spec update was structural only (paths, formatting) with no behavior change.
โ
โโ [REQUIRED] โ /tdd-spec [direction=sync]
โ Syncs QA dashboard (docs/specs/{Module}/README.md) with updated Section 15 TCs.
โ
โโ [RECOMMENDED] โ /integration-test [from-changes mode]
โ Generates/updates integration test files for TCs that changed.
โ Skip if: integration tests are maintained separately or not applicable for this service.
โ
โโ [RECOMMENDED] โ /docs-update
Final orchestration pass to catch any remaining staleness.
Calls: /feature-docs (Phase 2) โ /spec-discovery (Phase 2.5 โ already done, fast-exit) โ /tdd-spec (Phase 3) โ /tdd-spec sync (Phase 4).
| Mode | Chain |
|---|---|
| init (first time) | spec-discovery init โ /feature-docs init โ /tdd-spec CREATE โ /tdd-spec sync โ /integration-test |
| update (code changed) | spec-discovery update โ /feature-docs update โ /tdd-spec UPDATE โ /tdd-spec sync โ /integration-test verify-traceability |
| audit (staleness check) | spec-discovery audit โ if stale: run update chain โ else: done |
Every spec bundle MUST include ## Related Documentation in README.md:
## Related Documentation
- **Business Feature Doc:** [docs/business-features/{Module}/README.md](../../../docs/business-features/{Module}/README.md)
- **QA Spec Dashboard:** [docs/specs/{Module}/README.md](../../../docs/specs/{Module}/README.md)
- **Integration Tests:** `src/Services/{Service}/{Service}.IntegrationTests/`
On update: verify links present and correct.
[BLOCKING] Each phase MUST ATTENTION invoke its
Skilltool โ marking a taskcompletedwithout skill invocation is a workflow violation. NEVER batch-complete validation gates.
Reverse of product discovery โ existing codebase โ reimplementation-grade spec bundle. Docs describe what system does and why โ stripped of tech choices, implementation details, language constructs. Any team reads bundle, rebuilds on different stack.
Use Cases: AI replatforming, stack migration, compliance docs, knowledge capture, spec-driven bootstrap.
Scale Reality: Thousands of files, dozens of modules. Scout-first โ plan-decompose โ investigate-deeply prevents context overrun, ensures complete coverage.
Cross-Service Check โ Microservices/event-driven: MANDATORY before concluding investigation, plan, spec, or feature doc. Missing downstream consumer = silent regression.
Boundary Grep terms Event producers Publish,Dispatch,Send,emit,EventBus,outbox,IntegrationEventEvent consumers Consumer,EventHandler,Subscribe,@EventListener,inboxSagas/orchestration Saga,ProcessManager,Choreography,Workflow,OrchestratorSync service calls HTTP/gRPC calls to/from other services Shared contracts OpenAPI spec, proto, shared DTO โ flag breaking changes Data ownership Other service reads/writes same table/collection โ Shared-DB anti-pattern Per touchpoint: owner service ยท message name ยท consumers ยท risk (NONE / ADDITIVE / BREAKING).
BLOCKED until: Producers scanned ยท Consumers scanned ยท Sagas checked ยท Contracts reviewed ยท Breaking-change risk flagged
AI Mistake Prevention โ Failure modes to avoid on every task:
Check downstream references before deleting. Deleting components causes documentation and code staleness cascades. Map all referencing files before removal. Verify AI-generated content against actual code. AI hallucinates APIs, class names, and method signatures. Always grep to confirm existence before documenting or referencing. Trace full dependency chain after edits. Changing a definition misses downstream variables and consumers derived from it. Always trace the full chain. Trace ALL code paths when verifying correctness. Confirming code exists is not confirming it executes. Always trace early exits, error branches, and conditional skips โ not just happy path. When debugging, ask "whose responsibility?" before fixing. Trace whether bug is in caller (wrong data) or callee (wrong handling). Fix at responsible layer โ never patch symptom site. Assume existing values are intentional โ ask WHY before changing. Before changing any constant, limit, flag, or pattern: read comments, check git blame, examine surrounding code. Verify ALL affected outputs, not just the first. Changes touching multiple stacks require verifying EVERY output. One green check is not all green checks. Holistic-first debugging โ resist nearest-attention trap. When investigating any failure, list EVERY precondition first (config, env vars, DB names, endpoints, DI registrations, data preconditions), then verify each against evidence before forming any code-layer hypothesis. Surgical changes โ apply the diff test. Bug fix: every changed line must trace directly to the bug. Don't restyle or improve adjacent code. Enhancement task: implement improvements AND announce them explicitly. Surface ambiguity before coding โ don't pick silently. If request has multiple interpretations, present each with effort estimate and ask. Never assume all-records, file-based, or more complex path.
Critical Thinking Mindset โ Apply critical thinking, sequential thinking. Every claim needs traced proof, confidence >80% to act. Anti-hallucination: Never present guess as fact โ cite sources for every claim, admit uncertainty freely, self-check output for errors, cross-reference independently, stay skeptical of own confidence โ certainty without evidence root of all hallucination.
MUST ATTENTION apply critical thinking โ every claim needs traced proof, confidence >80% to act. Anti-hallucination: never present guess as fact.
MUST ATTENTION apply AI mistake prevention โ holistic-first debugging, fix at responsible layer, surface ambiguity before coding, re-read files after compaction.
IMPORTANT MUST ATTENTION follow declared step order for this skill; NEVER skip, reorder, or merge steps without explicit user approval
IMPORTANT MUST ATTENTION for every step/sub-skill call: set in_progress before execution, set completed after execution
IMPORTANT MUST ATTENTION every skipped step MUST include explicit reason; every completed step MUST include concise evidence
IMPORTANT MUST ATTENTION if Task tools unavailable, maintain an equivalent step-by-step plan tracker with synchronized statuses
TaskCreate one task per operation group per phase (not per module per phase) โ verify TaskList count โฅ sum(operation_groups ร phase_count) before /plan-review01-domain-erd.md after all entities extracted; foundational artifact all other phases referenceAskUserQuestion BEFORE Step 1 โ NEVER auto-startTaskList FIRST, read completeness tracker, NEVER re-run scout or plancode-reviewer sub-agent (max 2 rounds) โ NEVER inline re-review[Source: file:line] โ mark [UNVERIFIED] rather than guessingAnti-Rationalization:
| Evasion | Rebuttal |
|---|---|
| "Only 2โ3 modules, skip sub-agents" | BLOCKING SCALE GATE applies at 4+. Count modules first, then decide. |
| "Already scouted, skip Step 1.5" | Step 1.5 MANDATORY after Step 1. Show Use Case Inventory as proof before proceeding. |
| "module_count ร phase_count is the plan formula" | NEVER โ use sum(operation_groups ร phase_count). Module formula undercounts large modules. |
| "Context compacted, re-run scout to rebuild context" | TaskList FIRST. Registry + task list survive compaction. NEVER re-scout in resumed session. |
| "Wrote spec file, marking complete" | VERIFY โ re-read written spec against source. Mark [UNVERIFIED], not blank. |
| "Fresh sub-agent overkill for small fix" | Mandatory after ANY fix loop. Main agent rationalizes its own output. Max 2 rounds. |
| "Scope is clear, skip Step 0 AskUserQuestion" | BLOCKING โ NEVER auto-start. Scope, mode, and system name MUST be confirmed first. |
[TASK-PLANNING] MUST ATTENTION analyze task scope and break into small todo tasks/sub-tasks via TaskCreate before acting.
[IMPORTANT] Break into many small todo tasks systematically before starting โ this is critical.