all-for-claudecode 2.9.1 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +2 -2
  3. package/MIGRATION.md +1 -1
  4. package/README.md +4 -0
  5. package/package.json +2 -2
  6. package/schemas/plugin.schema.json +2 -2
  7. package/scripts/afc-consistency-check.sh +25 -23
  8. package/scripts/afc-doctor.sh +10 -10
  9. package/scripts/afc-qa-audit.sh +1 -1
  10. package/scripts/afc-sync-cache.sh +1 -1
  11. package/{commands/analyze.md → skills/analyze/SKILL.md} +10 -8
  12. package/{commands/architect.md → skills/architect/SKILL.md} +3 -3
  13. package/skills/auto/SKILL.md +1156 -0
  14. package/{commands/clarify.md → skills/clarify/SKILL.md} +4 -3
  15. package/{commands/clean.md → skills/clean/SKILL.md} +14 -13
  16. package/{commands/consult.md → skills/consult/SKILL.md} +19 -18
  17. package/{commands/implement.md → skills/implement/SKILL.md} +28 -15
  18. package/{commands/init.md → skills/init/SKILL.md} +5 -1
  19. package/{commands/learner.md → skills/learner/SKILL.md} +4 -4
  20. package/{commands/plan.md → skills/plan/SKILL.md} +1 -122
  21. package/skills/plan/plan-template.md +118 -0
  22. package/{commands/pr-comment.md → skills/pr-comment/SKILL.md} +4 -4
  23. package/{commands/principles.md → skills/principles/SKILL.md} +1 -1
  24. package/{commands/qa.md → skills/qa/SKILL.md} +2 -2
  25. package/{commands/release-notes.md → skills/release-notes/SKILL.md} +8 -4
  26. package/{commands/review.md → skills/review/SKILL.md} +11 -11
  27. package/{commands/security.md → skills/security/SKILL.md} +19 -4
  28. package/{commands/spec.md → skills/spec/SKILL.md} +2 -77
  29. package/skills/spec/spec-template.md +72 -0
  30. package/{commands/triage.md → skills/triage/SKILL.md} +6 -7
  31. package/commands/auto.md +0 -585
  32. /package/{commands/checkpoint.md → skills/checkpoint/SKILL.md} +0 -0
  33. /package/{commands/debug.md → skills/debug/SKILL.md} +0 -0
  34. /package/{commands/doctor.md → skills/doctor/SKILL.md} +0 -0
  35. /package/{commands/ideate.md → skills/ideate/SKILL.md} +0 -0
  36. /package/{commands/launch.md → skills/launch/SKILL.md} +0 -0
  37. /package/{commands/research.md → skills/research/SKILL.md} +0 -0
  38. /package/{commands/resume.md → skills/resume/SKILL.md} +0 -0
  39. /package/{docs → skills/spec}/nfr-templates.md +0 -0
  40. /package/{commands/tasks.md → skills/tasks/SKILL.md} +0 -0
  41. /package/{commands/test.md → skills/test/SKILL.md} +0 -0
  42. /package/{commands/validate.md → skills/validate/SKILL.md} +0 -0
@@ -0,0 +1,1156 @@
1
+ ---
2
+ name: afc:auto
3
+ description: "Full auto pipeline — use when the user asks to run the full afc pipeline automatically or automate the spec-to-clean cycle"
4
+ argument-hint: "[feature description in natural language]"
5
+ ---
6
+
7
+ # /afc:auto — Full Auto Pipeline
8
+
9
+ > Runs clarify? → spec → plan → implement → review → clean fully automatically from a single feature description.
10
+ > Tasks are generated automatically at implement start (no separate tasks phase).
11
+ > Critic Loop runs at each phase with unified safety cap (5). Convergence terminates early when quality is sufficient.
12
+ > Pre-implementation gates (clarify, TDD pre-gen, blast-radius) run conditionally within the implement phase.
13
+ > **Skill Advisor**: 5 checkpoints (A–E) at phase boundaries dynamically invoke auxiliary skills (ideate, consult, architect, security, analyze, test, qa, learner) based on signal detection. Budget-controlled (max 5 per pipeline).
14
+
15
+ ## Arguments
16
+
17
+ - `$ARGUMENTS` — (required) Feature description in natural language
18
+
19
+ ## Project Config (auto-loaded)
20
+
21
+ !`cat .claude/afc.config.md 2>/dev/null || echo "[CONFIG NOT FOUND] .claude/afc.config.md not found. Create it with /afc:init."`
22
+
23
+ ## Config Load
24
+
25
+ **Always** read `.claude/afc.config.md` first (read manually if not auto-loaded above). Values defined in this file are referenced below as `{config.*}`:
26
+ - `{config.ci}` — full CI command (from `## CI Commands` YAML)
27
+ - `{config.gate}` — phase gate command (from `## CI Commands` YAML)
28
+ - `{config.test}` — test command (from `## CI Commands` YAML)
29
+ - `{config.architecture}` — architecture style and rules (from `## Architecture` section)
30
+ - `{config.code_style}` — code style rules (from `## Code Style` section)
31
+
32
+ If config file is missing:
33
+ 1. Ask the user: "`.claude/afc.config.md` not found. Run `/afc:init` to set up the project?"
34
+ 2. If user accepts → run `/afc:init`, then **restart this command** with the original `$ARGUMENTS`
35
+ 3. If user declines → **abort**
36
+
37
+ ---
38
+
39
+ ## Skill Advisor System
40
+
41
+ > Auxiliary skills (ideate, consult, architect, security, analyze, test, qa, learner) are dynamically invoked at phase boundaries based on **intent-based evaluation**. Each checkpoint uses LLM semantic judgment — not keyword counting — to determine whether auxiliary skills would add value.
42
+
43
+ ### Core Principle: Intent-Based Evaluation
44
+
45
+ Each checkpoint contains a **structured evaluation prompt** that the orchestrator answers by reading the actual artifact content (not scanning for keywords). The evaluation produces a 1–5 score per signal. Score >= 3 triggers the corresponding skill.
46
+
47
+ **Why not keywords**: Keyword matching produces false positives (e.g., "token" in CSS vs auth context) and misses implicit intent (e.g., "user upload feature" implies security concerns without mentioning "XSS"). The orchestrator is an LLM — it should use semantic understanding.
48
+
49
+ ### Execution Modes
50
+
51
+ | Mode | Description | Context cost | Example |
52
+ |------|-------------|-------------|---------|
53
+ | **Transform** | Skill output **replaces or restructures** the next phase's input | High (blocking) | ideate → $ARGUMENTS restructured |
54
+ | **Enrich** | Skill output **appends context** to the next phase's input | Low (fork/Task) | consult → domain constraints section added |
55
+ | **Observe** | Skill output is **metadata only** (logged, flags set) | Low (fork) | qa → quality score recorded |
56
+
57
+ ### Budget Control
58
+
59
+ | Constraint | Limit | Rationale |
60
+ |-----------|-------|-----------|
61
+ | Per checkpoint | max 2 skills | Phase transition delay cap |
62
+ | Pipeline total | max 5 auxiliary invocations | Total execution time cap |
63
+ | Transform mode | max 1 per pipeline | Main context pollution prevention |
64
+ | Concurrent fork | max 3 per checkpoint | Agent resource limit |
65
+
66
+ Track auxiliary invocations in `ADVISOR_COUNT` (starts at 0, increments per invocation). If `ADVISOR_COUNT >= 5`, skip remaining checkpoints. Transform invocations tracked in `ADVISOR_TRANSFORM_USED` (boolean). **Every increment** must persist to pipeline state: `afc_state_write "advisorCount" "$ADVISOR_COUNT"`. On context recovery, restore from state: `ADVISOR_COUNT = afc_state_read "advisorCount"`.
67
+
68
+ ### Expert Agent Routing
69
+
70
+ When a checkpoint determines that domain expertise is needed, route to the appropriate expert agent:
71
+
72
+ | Domain | Agent ID | When to route |
73
+ |--------|----------|---------------|
74
+ | backend | `afc-backend-expert` | API design, database schema, server architecture, auth flows |
75
+ | infra | `afc-infra-expert` | Deployment, CI/CD, cloud infrastructure, containerization, scaling |
76
+ | pm | `afc-pm-expert` | Product decisions, user stories, prioritization, metrics |
77
+ | design | `afc-design-expert` | UI/UX, accessibility, component design, visual hierarchy |
78
+ | marketing | `afc-marketing-expert` | SEO, analytics, growth, conversion optimization |
79
+ | legal | `afc-legal-expert` | Privacy regulations, licensing, compliance, data protection |
80
+ | security | `afc-appsec-expert` | Application security, vulnerability patterns, secure coding |
81
+ | advisor | `afc-tech-advisor` | Technology selection, library comparison, stack decisions |
82
+
83
+ Route based on **what expertise the feature actually needs**, not keyword presence. Consider the project's `{config.architecture}` and tech stack — skip domains irrelevant to the project.
84
+
85
+ **Agent ID lookup**: Use the Agent ID column directly as the `subagent_type` value (e.g., `subagent_type: "afc:afc-backend-expert"`). Do NOT construct agent names from the domain name — `security` maps to `afc-appsec-expert` (not `afc-security-expert`), and `advisor` maps to `afc-tech-advisor` (not `afc-advisor-expert`).
86
+
87
+ ---
88
+
89
+ ## Critic Loop Rules (common to all phases)
90
+
91
+ > **Always** read `${CLAUDE_PLUGIN_ROOT}/docs/critic-loop-rules.md` first and follow it.
92
+ > Core: minimum 1 concern per criterion + mandatory Adversarial failure scenario each pass + quantitative evidence required. "PASS" as a single word is prohibited. Uses convergence-based termination with 4 verdicts (PASS/FAIL/ESCALATE/DEFER). On ESCALATE: pause and present options to user even in auto mode.
93
+
94
+ ---
95
+
96
+ ## Execution Steps
97
+
98
+ ### Phase 0: Preparation
99
+
100
+ 1. If `$ARGUMENTS` is empty → print "Please enter a feature description." and abort
101
+ 2. Check current branch → `BRANCH_NAME`
102
+ 3. Determine feature name (2-3 keywords → kebab-case)
103
+ 3.5. **Preflight Check**:
104
+ ```bash
105
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-preflight-check.sh"
106
+ ```
107
+ - If exit 1 (hard failure) → print error and **abort**
108
+ - If warnings only (exit 0) → print warnings and continue
109
+ 4. **Activate Pipeline Flag** (hook integration):
110
+ ```bash
111
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" start {feature}
112
+ ```
113
+ - Safety Snapshot created automatically (`afc/pre-auto` git tag)
114
+ - Stop Gate Hook activated (blocks response termination on CI failure)
115
+ - File change tracking started
116
+ - Timeline log: `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" log pipeline-start "Auto pipeline: {feature}"`
117
+ 5. Create `.claude/afc/specs/{feature}/` directory → **record path as `PIPELINE_ARTIFACT_DIR`** (for Clean scope)
118
+ 6. **Initialize Skill Advisor**: `ADVISOR_COUNT = 0`, `ADVISOR_TRANSFORM_USED = false`. Persist to pipeline state for context-loss resilience:
119
+ ```bash
120
+ afc_state_write "advisorCount" "0"
121
+ afc_state_write "advisorTransformUsed" "false"
122
+ ```
123
+ 7. Start notification:
124
+ ```
125
+ Auto pipeline started: {feature}
126
+ ├─ Clarify? → 1/5 Spec → 2/5 Plan → 3/5 Implement → 4/5 Review → 5/5 Clean
127
+ └─ Running fully automatically (tasks auto-generated, pre-implementation gates conditional)
128
+ ```
129
+
130
+ ### Phase 0.3: Request Triage
131
+
132
+ Before investing pipeline resources, evaluate whether the request warrants execution:
133
+
134
+ 1. **Necessity check**: Explore codebase for existing implementations related to `$ARGUMENTS`.
135
+ - If the feature substantially exists → ask user via AskUserQuestion:
136
+ - "This feature appears to already exist at {path}. (1) Enhance existing (2) Replace entirely (3) Abort"
137
+ - If user chooses abort → release pipeline flag (`"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" end`), end with: `"Pipeline aborted — feature already exists."`
138
+
139
+ 2. **Scope check**: Estimate the scope of `$ARGUMENTS`:
140
+ - If description implies 10+ files or multiple unrelated concerns → warn:
141
+ - "This request spans multiple concerns: {list}. Recommended: split into {N} separate pipeline runs."
142
+ - Ask: "(1) Proceed as single pipeline (2) Reduce scope to {suggestion} (3) Abort"
143
+
144
+ 3. **Proportionality check**: If the request is trivially small (single file, single-line change, config edit):
145
+ - Suggest: "This change is small enough to implement directly. Skip full pipeline?"
146
+ - If user agrees → execute fast-path directly, skip spec/plan/tasks
147
+
148
+ If all checks pass, proceed to Phase 0.8.
149
+
150
+ ### Phase 0.8: Size-Based Fast-Path Detection (conditional)
151
+
152
+ **Trigger condition**: Evaluate `$ARGUMENTS` against ALL 3 criteria. Fast-path activates only when ALL are met:
153
+
154
+ | Criterion | Check | Example |
155
+ |-----------|-------|---------|
156
+ | Trivial scope | Description explicitly mentions 1-2 specific files or a single-line fix | "fix typo in README", "update version in package.json" |
157
+ | No script impact | Description does not reference `.sh` scripts, hooks, or pipeline logic | NOT: "fix the hook script" |
158
+ | Low ambiguity | Clarify gate score < 2 (very clear, specific request) | "change 'foo' to 'bar' in config.md" |
159
+
160
+ **If ALL 3 criteria met** (fast-path):
161
+ 1. Print: `⚡ Fast path detected — skipping spec/plan phases`
162
+ 2. Jump directly to **Fast-Path Execution** (see below)
163
+ 3. Skip Phases 0.5 through 3.3 entirely
164
+
165
+ **If ANY criterion fails**: proceed to Phase 0.5 (full pipeline).
166
+
167
+ **Fast-Path Execution** (implement → review → clean):
168
+ 1. Implement the change directly (no tasks.md, no plan.md)
169
+ 2. Run `{config.ci}` verification
170
+ - On fail: **rollback fast-path changes** (`git reset --hard afc/pre-auto`), then restart with full pipeline: `⚠ Fast-path aborted — change is more complex than expected. Rolling back and running full pipeline.`
171
+ 3. If change touches > 2 files OR modifies any `.sh` script: **rollback fast-path changes** (`git reset --hard afc/pre-auto`), then restart with full pipeline
172
+ 4. **Checkpoint**:
173
+ ```bash
174
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase fast-path
175
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" ci-pass
176
+ ```
177
+ 5. Run `/afc:review` logic inline (mini-review only — single Critic pass)
178
+ 6. Run Phase 5 Clean logic (artifact cleanup, CI gate, pipeline flag release)
179
+ 7. Final output:
180
+ ```
181
+ Fast path complete: {feature}
182
+ ├─ Mode: ⚡ Fast path (spec/plan skipped)
183
+ ├─ Changed files: {N}
184
+ ├─ CI: ✓
185
+ └─ Review: mini-review passed
186
+ ```
187
+
188
+ ### Phase 0.5: Auto-Clarify Gate (conditional)
189
+
190
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase clarify`
191
+
192
+ **Trigger condition**: Score `$ARGUMENTS` on 5 ambiguity signals. If score >= 3, trigger clarification.
193
+
194
+ | Signal | Detection | Example |
195
+ |--------|-----------|---------|
196
+ | Vague scope | No specific file, component, or module mentioned | "add caching" |
197
+ | Missing quantifiers | No numbers, sizes, limits, or thresholds | "improve performance" |
198
+ | Undefined entities | References to concepts not in the codebase | "integrate the new service" |
199
+ | Unclear boundaries | No start/end conditions or scope limits | "refactor the system" |
200
+ | Multiple interpretations | Ambiguous verbs or overloaded terms | "fix the pipeline" (which one?) |
201
+
202
+ **If score >= 3** (ambiguous):
203
+ 1. Generate at most 3 clarification questions targeting the highest-signal areas
204
+ 2. Present via AskUserQuestion with multiple-choice options
205
+ 3. Apply answers to refine `$ARGUMENTS` before proceeding to Spec
206
+ 4. If in full-auto mode and user prefers no interruption: auto-resolve with best-guess, tag with `[AUTO-RESOLVED: clarify]`, emit warning
207
+ 5. Progress: `✓ Clarify gate triggered ({N} questions, {M} auto-resolved)`
208
+
209
+ **If score < 3** (clear): skip silently, proceed to Phase 1.
210
+
211
+ ### Skill Advisor Checkpoint A (Pre-Spec)
212
+
213
+ > Evaluate auxiliary skill triggers BEFORE entering Phase 1. Budget: max 2 skills, max 1 Transform. Skip all if `ADVISOR_COUNT >= 5`.
214
+
215
+ **Intent evaluation** — Read `$ARGUMENTS` and answer these questions semantically (not by keyword scanning):
216
+
217
+ | # | Question | Score 1–5 | If >= 3 | Skill | Mode |
218
+ |---|----------|-----------|---------|-------|------|
219
+ | A1 | Is this request at the **idea/vision level** rather than a concrete feature? (e.g., "make onboarding better" vs "add email verification to signup flow") Does it lack specific technical scope — no file paths, no API endpoints, no component names? | 1=concrete spec-ready, 5=pure vision | `ideate` | Transform |
220
+ | A2 | Does implementing this feature require **specialized domain knowledge** that a generalist developer wouldn't have? Consider: regulatory requirements, industry-specific patterns, domain-specific anti-patterns, compliance rules. Which domain from the Expert Agent Routing table would add the most value? | 1=general programming, 5=deep domain expertise essential | `consult({domain})` | Enrich (fork) |
221
+
222
+ **If A1 >= 3** (Transform — skip if `ADVISOR_TRANSFORM_USED`):
223
+ 1. Execute `/afc:ideate` inline with `$ARGUMENTS`
224
+ 2. If ideate fails or produces no output:
225
+ - Do NOT set `ADVISOR_TRANSFORM_USED = true`
226
+ - Proceed with original `$ARGUMENTS`
227
+ - Log: `"Skill Advisor [A]: ideate failed, proceeding with original input"`
228
+ 3. On success: read generated `ideate.md` → extract `## Problem Statement` + `## Value Proposition` + `## Core Features (MoSCoW)` sections
229
+ 4. Construct enriched spec input:
230
+ ```
231
+ SPEC_INPUT = "$ARGUMENTS
232
+
233
+ ## Ideation Context (auto-generated)
234
+ {extracted Problem Statement section}
235
+ {extracted Value Proposition section}
236
+ {extracted Core Features (MoSCoW) — Must Have items only}"
237
+ ```
238
+ 5. Replace `$ARGUMENTS` with `SPEC_INPUT` for Phase 1
239
+ 6. Set `ADVISOR_TRANSFORM_USED = true`, increment `ADVISOR_COUNT`, persist: `afc_state_write "advisorCount" "$ADVISOR_COUNT"` and `afc_state_write "advisorTransformUsed" "true"`
240
+ 7. Progress: ` ├─ Skill Advisor [A]: ideate (score: {N}/5, input restructured from idea to structured brief)`
241
+
242
+ **If A2 >= 3** (Enrich):
243
+ 1. Determine which domain from Expert Agent Routing table best matches the **actual expertise gap** (not keyword presence)
244
+ 2. Verify domain relevance: does this project's `{config.architecture}` and tech stack make this domain applicable? (e.g., skip `design` for a CLI tool, skip `infra` if the project has no deployment config)
245
+ 3. Invoke expert agent (look up the agent-id from the Expert Agent Routing table — do NOT construct from domain name):
246
+ ```
247
+ Task("Domain pre-consultation: {domain}", subagent_type: "afc:{agent-id-from-routing-table}",
248
+ prompt: "You are being consulted automatically during pipeline spec preparation.
249
+
250
+ ## Feature Context
251
+ {$ARGUMENTS}
252
+
253
+ ## Why You Were Consulted
254
+ {1-sentence explanation of what domain expertise gap was identified}
255
+
256
+ ## Instructions
257
+ 1. Read your MEMORY.md for prior project context
258
+ 2. Read .claude/afc/project-profile.md if it exists
259
+ 3. Provide domain-specific constraints, regulations, and anti-patterns that MUST be reflected in the spec
260
+ 4. Format your response EXACTLY as:
261
+ ## Domain Constraints ({domain})
262
+ - [MUST] {constraint}: {rationale}
263
+ - [MUST NOT] {anti-pattern}: {risk}
264
+ - [CONSIDER] {best practice}: {benefit}
265
+ 5. Keep to max 10 items. Prioritize by risk severity.
266
+ 6. Update your MEMORY.md with the consultation context")
267
+ ```
268
+ 4. Store output as `DOMAIN_CONSTRAINTS` → injected into Phase 1 spec context
269
+ 5. Spec phase MUST include a `## Domain Constraints` section reflecting these items
270
+ 6. Increment `ADVISOR_COUNT`
271
+ 7. Progress: ` ├─ Skill Advisor [A]: consult({domain}) (score: {N}/5, {M} constraints injected)`
272
+
273
+ **If all scores < 3**: proceed silently to Phase 1.
274
+
275
+ ### Phase 1: Spec (1/5)
276
+
277
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase spec`
278
+
279
+ Execute `/afc:spec` logic inline:
280
+
281
+ 1. Explore codebase for related code (Glob, Grep) — explore by `{config.architecture}` layer
282
+ 2. **Research Gate** (conditional):
283
+ - Scan `$ARGUMENTS` for external library/API/technology references not present in the codebase
284
+ - If external references found: run focused WebSearch for each (latest stable version, key constraints, compatibility)
285
+ - Optionally use Context7 for library-specific documentation
286
+ - Use research findings to inform spec writing (accurate requirements instead of guesses)
287
+ - Tag researched items with `[RESEARCHED]` in spec
288
+ - If no external references: skip (all internal → no research needed)
289
+ 3. Create `.claude/afc/specs/{feature}/spec.md`
290
+ 4. `[NEEDS CLARIFICATION]` items: **research first, then auto-resolve remaining** (clarify skipped if Phase 0.5 already ran)
291
+ - Items answerable via research → resolve with researched facts, tag `[RESEARCHED]`
292
+ - Items requiring user judgment → auto-resolve with best-guess, tag `[AUTO-RESOLVED]`
293
+ 5. **Retrospective check**: if `.claude/afc/memory/retrospectives/` exists, load the **most recent 10 files** (sorted by filename descending) and check:
294
+ - Were there previous `[AUTO-RESOLVED]` items that turned out wrong? Flag similar patterns.
295
+ - Were there scope-related issues in past specs? Warn about similar ambiguities.
296
+ 6. **Critic Loop until convergence** (safety cap: 5, follow Critic Loop rules):
297
+ - COMPLETENESS: does every User Story have acceptance scenarios? Any missing requirements?
298
+ - MEASURABILITY: are success criteria measurable, not subjective? **Is quantitative evidence provided for numerical targets?**
299
+ - INDEPENDENCE: are implementation details (code, library names) absent from the spec?
300
+ - EDGE_CASES: are at least 2 identified? Any missing boundary conditions?
301
+ - TESTABILITY: Does every System Requirement follow one of the 5 EARS patterns (WHEN/WHILE/IF/WHERE/SHALL)? Does each EARS requirement have a mapped TC (`→ TC: should_...`)? If not → FAIL and auto-fix: rewrite to EARS + generate TC mapping.
302
+ - FAIL → auto-fix and continue. ESCALATE → pause, present options, resume after response. DEFER → record reason, mark clean.
303
+ 7. **Checkpoint**: phase transition already recorded by `afc-pipeline-manage.sh phase spec` at phase start
304
+ 8. Progress: `✓ 1/5 Spec complete (US: {N}, FR: {N}, researched: {N}, Critic: converged ({N} passes, {M} fixes, {E} escalations))`
305
+
306
+ ### Skill Advisor Checkpoint B (Post-Spec)
307
+
308
+ > Evaluate auxiliary skill triggers AFTER spec completion, BEFORE plan creation. Budget: max 2 skills. Skip all if `ADVISOR_COUNT >= 5`.
309
+
310
+ **Intent evaluation** — Read the completed spec.md and answer these questions:
311
+
312
+ | # | Question | Score 1–5 | If >= 3 | Skill | Mode |
313
+ |---|----------|-----------|---------|-------|------|
314
+ | B1 | Does this feature **handle, store, or transmit sensitive data or trust boundaries**? Consider: user authentication/authorization, cryptographic operations, PII/financial data processing, external input that reaches internal systems, session/token lifecycle. Judge by the feature's actual behavior, not by whether security-related words appear. | 1=no trust boundary touched, 5=core security feature | `security` | Enrich (fork) |
315
+ | B2 | Does this feature **cross multiple architectural boundaries** or introduce a new structural pattern? Consider: does it touch 3+ layers (e.g., API + service + data + external), create a new component type not seen in the codebase, or require coordination between independently-deployable units? | 1=single-layer change, 5=cross-cutting architectural change | `architect` | Enrich (fork) |
316
+
317
+ **If B1 >= 3** (Enrich):
318
+ 1. Invoke security agent for pre-plan threat modeling:
319
+ ```
320
+ Task("Threat Model: {feature}", subagent_type: "afc:afc-security",
321
+ prompt: "Generate a threat model BEFORE implementation planning begins.
322
+
323
+ ## Spec Summary
324
+ {spec.md FR/NFR/Key Entities — security-relevant items only}
325
+
326
+ ## Why This Was Triggered
327
+ {1-sentence explanation of which trust boundary or sensitive data flow was identified}
328
+
329
+ ## Instructions
330
+ 1. Read your MEMORY.md for known vulnerability patterns in this project
331
+ 2. Identify attack surfaces from the spec requirements
332
+ 3. For each threat, specify the mitigation that MUST appear in the plan
333
+ 4. Format your response EXACTLY as:
334
+ ## Threat Model (pre-scan)
335
+ | Threat | Attack Surface | Mitigation Required | Priority |
336
+ |--------|---------------|-------------------|----------|
337
+ 5. Max 8 threats. Prioritize by exploitability and impact.")
338
+ ```
339
+ 2. Store output as `THREAT_MODEL` → injected into Phase 2 plan context
340
+ 3. Plan phase MUST address each mitigation in its Risk & Mitigation section
341
+ 4. Plan Critic RISK criterion MUST verify: `{M}/{N} threat mitigations addressed`
342
+ 5. Increment `ADVISOR_COUNT`
343
+ 6. Progress: ` ├─ Skill Advisor [B]: security (score: {N}/5, threat model: {M} threats identified)`
344
+
345
+ **If B2 >= 3** (Enrich):
346
+ 1. Invoke architect agent for pre-plan guidance:
347
+ ```
348
+ Task("Architecture Advisory: {feature}", subagent_type: "afc:afc-architect",
349
+ prompt: "Provide architecture guidance BEFORE plan creation.
350
+
351
+ ## Spec Summary
352
+ {spec.md Key Entities + layer analysis from {config.architecture}}
353
+
354
+ ## Why This Was Triggered
355
+ {1-sentence explanation of which architectural boundary crossing was identified}
356
+
357
+ ## Instructions
358
+ 1. Read your MEMORY.md for prior ADRs and architecture patterns
359
+ 2. Recommend: component placement, layer boundaries, interface contracts
360
+ 3. Flag conflicts with existing architecture patterns
361
+ 4. Format your response EXACTLY as:
362
+ ## Architecture Advisory (pre-plan)
363
+ - [PLACE] {component} → {layer/module}: {rationale}
364
+ - [BOUNDARY] {interface}: {contract description}
365
+ - [CONFLICT] {existing} ↔ {new}: {resolution recommendation}
366
+ - [PATTERN] {recommended pattern}: {why it fits}
367
+ 5. Max 10 items.
368
+ 6. Update your MEMORY.md if new patterns are identified")
369
+ ```
370
+ 2. Store output as `ARCH_ADVISORY` → injected into Phase 2 plan context
371
+ 3. Plan Critic ARCHITECTURE criterion MUST validate against this advisory
372
+ 4. Increment `ADVISOR_COUNT`
373
+ 5. Progress: ` ├─ Skill Advisor [B]: architect (score: {N}/5, advisory: {M} recommendations, {K} conflicts)`
374
+
375
+ **If both B1 and B2 >= 3**: launch both agents in a **single message** (parallel fork). Both count toward budget. After both return:
376
+ 1. Apply `THREAT_MODEL` to plan Risk & Mitigation section
377
+ 2. Apply `ARCH_ADVISORY` to plan Architecture Decision section
378
+ 3. If security mitigations conflict with architecture proposals (e.g., "encrypt at rest" vs "use in-memory cache") → **ESCALATE** to user with conflict details
379
+
380
+ **If all scores < 3**: proceed silently to Phase 2.
381
+
382
+ ### Phase 2: Plan (2/5)
383
+
384
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase plan`
385
+
386
+ Execute `/afc:plan` logic inline:
387
+
388
+ 1. Load spec.md
389
+ 2. **Research (ReWOO pattern, if needed)**:
390
+ Extract technical uncertainties from spec.md (libraries/APIs not yet used, unverified performance requirements, unclear integration approach). If no uncertain items: skip.
391
+ If there are uncertain items, follow the 3-step ReWOO flow:
392
+ - **Step 1 — Plan**: List all research topics as a numbered list (NO execution yet): `1. {topic} — {what we need to know}`
393
+ - **Step 2 — Execute**: If topics are independent → launch parallel Task() calls in a **single message**: `Task("Research: {topic1}", subagent_type: "general-purpose")`. If a topic depends on another's result → execute sequentially. For 1-2 topics → resolve directly via WebSearch/codebase exploration (no delegation).
394
+ - **Step 3 — Solve**: Collect all results and record in `.claude/afc/specs/{feature}/research.md` with: Decision, Rationale, Alternatives, Source per topic.
395
+ 3. **Memory loading** (skip gracefully if directories are empty or absent):
396
+ - **Quality history**: if `.claude/afc/memory/quality-history/*.json` exists, load the **most recent 10 files** (sorted by filename descending) and display trend summary: "Last {N} pipelines: avg critic_fixes {X}, avg ci_failures {Y}, avg escalations {Z}". Use trends to inform plan risk assessment.
397
+ - **Decisions**: if `.claude/afc/memory/decisions/` exists, load the **most recent 30 files** (sorted by filename descending) and check for conflicts with the current feature's design direction. Flag any contradictions.
398
+ - **Reviews**: if `.claude/afc/memory/reviews/` exists, load the **most recent 15 files** (sorted by filename descending) and scan for recurring finding patterns (same file/category appearing in 2+ reviews). Flag as known risk areas.
399
+ 4. Create `.claude/afc/specs/{feature}/plan.md`
400
+ - **If setting numerical targets (line counts etc.), include structure-analysis-based estimates** (e.g., "function A ~50 lines, component B ~80 lines → total ~130 lines")
401
+ 5. **Critic Loop until convergence** (safety cap: 5, follow Critic Loop rules):
402
+ - Criteria: COMPLETENESS, FEASIBILITY, ARCHITECTURE, **CROSS_CONSISTENCY**, RISK, PRINCIPLES
403
+ - **CROSS_CONSISTENCY criterion** (spec↔plan cross-validation, check all 5):
404
+ 1. Entity coverage: every spec Key Entity → File Change Map row. `{M}/{N} entities covered`
405
+ 2. NFR traceability: every NFR-* → Architecture Decision or Risk mitigation. `{M}/{N} NFRs traced`
406
+ 3. Terminology consistency: same concept = same name across spec and plan
407
+ 4. Constraint propagation: every spec Constraint → Risk & Mitigation or Implementation Context Must NOT. `{M}/{N} constraints propagated`
408
+ 5. Acceptance anchor alignment: Implementation Context Acceptance Anchors faithfully reflect spec acceptance scenarios
409
+ - **RISK criterion mandatory checks**:
410
+ - Enumerate **at least 3** `{config.ci}` failure scenarios and describe mitigation
411
+ - Check each risk pattern described in config's Project Context section one by one
412
+ - Consider framework characteristics from config's Project Context (server/client boundary etc.)
413
+ - **ARCHITECTURE criterion**: explicitly describe import paths for moved/created files and pre-validate against `{config.architecture}` rules
414
+ - Each pass must **explicitly explore what was missed in the previous pass** ("Pass 2: {X} was missed in pass 1. Further review: ...")
415
+ - FAIL → auto-fix and continue. ESCALATE → pause, present options, resume after response. DEFER → record reason, mark clean.
416
+ 6. **Research persistence**: If research.md was created in step 2, persist a copy to long-term memory:
417
+ - Copy research findings to `.claude/afc/memory/research/{feature}.md`
418
+ - This enables future pipelines to reference prior research decisions
419
+ 7. **ADR recording via architect agent**: After plan.md is written, invoke the architect agent to record architectural decisions:
420
+ ```
421
+ Task("ADR: Record architecture decisions for {feature}", subagent_type: "afc:afc-architect",
422
+ prompt: "Review the following plan and record key architecture decisions to your persistent memory.
423
+
424
+ ## Plan Summary
425
+ {paste Architecture Decision + File Change Map sections from plan.md}
426
+
427
+ ## Instructions
428
+ 1. Read your MEMORY.md for prior ADR history
429
+ 2. Check for conflicts between new decisions and existing ADRs
430
+ 3. If conflicts found: return CONFLICT with details (orchestrator will ESCALATE)
431
+ 4. If no conflicts: record new ADRs to your MEMORY.md
432
+ 5. Return: { decisions_recorded: N, conflicts: [] }")
433
+ ```
434
+ - If architect returns conflicts → **ESCALATE** to user with conflict details
435
+ - If no conflicts → proceed (ADR recorded for future reference)
436
+ 8. **Session context preservation**: Write key decisions to `.claude/afc/specs/{feature}/context.md` for compaction resilience:
437
+ ```markdown
438
+ # Session Context: {feature}
439
+ ## Goal
440
+ - Original request: $ARGUMENTS
441
+ - Current objective: Implement {feature}
442
+ ## Acceptance Criteria (from spec.md)
443
+ {copy ALL FR-*, NFR-*, SC-* items and GWT acceptance scenarios from spec.md verbatim}
444
+ ## Key Decisions
445
+ - {what}: {rationale}
446
+ ## Discoveries
447
+ - {file path}: {finding}
448
+ ```
449
+ This file is read at Implement start to restore context after compaction. The full AC section ensures Review phase (Phase 4) can verify spec compliance even after spec.md is compacted.
450
+ 9. **Checkpoint**: phase transition already recorded by `afc-pipeline-manage.sh phase plan` at phase start
451
+ 10. Progress: `✓ 2/5 Plan complete (Critic: converged ({N} passes, {M} fixes, {E} escalations), files: {N}, ADR: {N} recorded, Implementation Context: {W} words)`
452
+
453
+ ### Skill Advisor Checkpoint C (Post-Plan)
454
+
455
+ > Evaluate auxiliary skill triggers AFTER plan completion, BEFORE implementation. Budget: max 2 skills. Skip all if `ADVISOR_COUNT >= 5`.
456
+
457
+ **Intent evaluation** — Read the completed plan.md and answer these questions:
458
+
459
+ | # | Question | Score 1–5 | If >= 3 | Skill | Mode |
460
+ |---|----------|-----------|---------|-------|------|
461
+ | C1 | Is the **implementation risk high enough** that a dependency pre-analysis would catch problems the plan missed? Consider: are there files in the File Change Map that import each other (potential circular dependency)? Are there shared utility files that many other files depend on (high fan-out risk)? Are the declared `Depends On` relationships complete, or could there be hidden coupling? | 1=isolated changes, 5=deeply interconnected change set | dependency analysis (general-purpose fork) | Observe |
462
+ | C2 | Does the plan contain **unresolved domain uncertainties** — items tagged `[UNCERTAIN]`, open questions in Implementation Context, or design decisions that assume domain knowledge the team may not have? | 1=all decisions are well-grounded, 5=critical domain questions remain open | `consult({domain})` expert agent | Enrich (fork) |
463
+
464
+ **If C1 >= 3** (Observe):
465
+ 1. Invoke analysis in fork context:
466
+ ```
467
+ Task("Complexity Analysis: {feature}", subagent_type: "general-purpose",
468
+ prompt: "Analyze the dependency graph of files listed in the plan's File Change Map.
469
+
470
+ ## File Change Map
471
+ {paste File Change Map table from plan.md}
472
+
473
+ ## Instructions
474
+ 1. For each file in the map, check its imports/dependencies in the codebase (Grep for import/require/source patterns)
475
+ 2. Identify:
476
+ - Circular dependencies between planned files
477
+ - High fan-out files (>5 dependents outside the change set)
478
+ - Hidden coupling not captured in the Depends On column
479
+ - Files that are imported by many other files (risk of breakage)
480
+ 3. Format your response EXACTLY as:
481
+ ## Complexity Analysis
482
+ - [CIRCULAR] {file A} ↔ {file B}: {description}
483
+ - [FAN-OUT] {file} → {N} dependents: {list top 5}
484
+ - [COUPLING] {file A} → {file B}: {not in Depends On column}
485
+ - [HIGH-RISK] {file}: {reason — most impactful if broken}
486
+ ## Risk Summary
487
+ Circular: {N}, High fan-out: {N}, Hidden coupling: {N}
488
+ 4. If no issues found, return: '## Complexity Analysis\nNo significant risks detected.'")
489
+ ```
490
+ 2. Store output to `.claude/afc/specs/{feature}/complexity-analysis.md`
491
+ 3. Implement phase reads this file → high-risk files get extra verification after modification
492
+ 4. If circular dependencies found → **ESCALATE** to user (circular deps in implementation plan are a design flaw)
493
+ 5. Increment `ADVISOR_COUNT`
494
+ 6. Progress: ` ├─ Skill Advisor [C]: analyze (score: {N}/5, circular: {C}, fan-out: {F}, coupling: {H})`
495
+
496
+ **If C2 >= 3** (Enrich):
497
+ 1. Determine which domain expert can best resolve the uncertainties (based on the nature of the open questions, not keywords)
498
+ 2. Invoke expert agent (look up the agent-id from the Expert Agent Routing table — do NOT construct from domain name):
499
+ ```
500
+ Task("Domain gap resolution: {domain}", subagent_type: "afc:{agent-id-from-routing-table}",
501
+ prompt: "Resolve domain uncertainties found during planning.
502
+
503
+ ## Uncertain Items
504
+ {extract all [UNCERTAIN] tagged items and open questions from plan.md}
505
+
506
+ ## Plan Context
507
+ {Implementation Context section from plan.md}
508
+
509
+ ## Instructions
510
+ 1. For each uncertain item, provide a definitive answer with rationale
511
+ 2. Format your response EXACTLY as:
512
+ ## Domain Resolutions
513
+ - [RESOLVED] {item}: {answer} — {rationale}
514
+ - [NEEDS-USER] {item}: {why this requires human judgment}
515
+ 3. Update your MEMORY.md with the resolution context")
516
+ ```
517
+ 3. Apply resolutions to plan.md Implementation Context (replace `[UNCERTAIN]` with `[RESOLVED: {answer}]`)
518
+ 4. `[NEEDS-USER]` items → **ESCALATE** to user via AskUserQuestion
519
+ 5. Increment `ADVISOR_COUNT`
520
+ 6. Progress: ` ├─ Skill Advisor [C]: consult({domain}) (score: {N}/5, {M} resolved, {K} needs-user)`
521
+
522
+ **If all scores < 3**: proceed silently to Phase 3.
523
+
524
+ ### Phase 3: Implement (3/5)
525
+
526
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase implement`
527
+
528
+ **Session context reload**: At implement start, read `.claude/afc/specs/{feature}/context.md` if it exists. This restores key decisions and constraints from Plan phase (resilient to context compaction).
529
+
530
+ **Advisor context reload**: If Checkpoint C produced `.claude/afc/specs/{feature}/complexity-analysis.md`, read it and flag high-risk files (circular dependencies, high fan-out) for extra verification after modification during task execution.
531
+
532
+ Execute `/afc:implement` logic inline — **follow all orchestration rules defined in `skills/implement/SKILL.md`** (task generation, mode selection, batch/swarm execution, failure recovery, task execution pattern). The implement skill is the single source of truth for orchestration details.
533
+
534
+ **Auto-specific additions** (beyond implement.md):
535
+
536
+ #### Step 3.1: Task Generation + Validation
537
+
538
+ 1. Generate tasks.md from plan.md File Change Map using the following format and principles:
539
+
540
+ **Task Format** (required):
541
+ ```markdown
542
+ - [ ] T{NNN} {[P]} {[US*]} {description} `{file path}` {depends: [TXXX, TXXX]}
543
+ ```
544
+ | Component | Required | Description |
545
+ |-----------|----------|-------------|
546
+ | `T{NNN}` | Yes | 3-digit sequential ID (T001, T002, ...) |
547
+ | `[P]` | No | **Mandatory parallel execution** — task MUST run in parallel with other [P] tasks in the same phase. Requires no file overlap. |
548
+ | `[US*]` | No | User Story label from spec.md |
549
+ | description | Yes | Clear task description (start with a verb) |
550
+ | file path | Yes | Primary target file (wrapped in backticks) |
551
+ | `depends:` | No | Explicit dependency list — task cannot start until all listed complete |
552
+
553
+ **Decomposition Principles**:
554
+ - **1 task = 1 file** principle (where possible)
555
+ - **Same file = sequential**, **different files = [P] candidate**
556
+ - **Explicit dependencies**: Use `depends: [T001, T002]` for blocking dependencies
557
+ - **Test tasks**: Include a verification task for each testable unit
558
+ - **Phase gate**: Add a `{config.gate}` validation task at the end of each Phase
559
+
560
+ **Phase Structure**: Group tasks by Phase (Setup → Core → UI → Integration & Polish)
561
+
562
+ **Coverage Mapping** (append after tasks):
563
+ ```markdown
564
+ ## Coverage Mapping
565
+ | Requirement | Tasks |
566
+ |-------------|-------|
567
+ | FR-001 | T003, T007 |
568
+ ```
569
+ Every FR-*/NFR-* must be mapped to at least one task.
570
+
571
+ 2. **Retrospective check**: if `.claude/afc/memory/retrospectives/` exists, load the **most recent 10 files** (sorted by filename descending) and check:
572
+ - Were there previous parallel conflict issues ([P] file overlaps)? Flag similar file patterns.
573
+ - Were there tasks that were over-decomposed or under-decomposed? Adjust granularity.
574
+ 3. **Script validation**: Run DAG validation (`afc-dag-validate.sh`) and parallel overlap validation (`afc-parallel-validate.sh`) — no critic loop, script-based only. Fix any conflicts before proceeding.
575
+ 4. Progress: ` ├─ Tasks generated: {N} ({P} parallelizable), Coverage: FR {M}%, NFR {K}%`
576
+
577
+ #### Step 3.2: TDD Pre-Generation (conditional)
578
+
579
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase test-pre-gen`
580
+
581
+ **Trigger condition**: tasks.md contains at least 1 task targeting a `.sh` file in `scripts/`.
582
+
583
+ **If triggered**:
584
+ 1. Run the test pre-generation script:
585
+ ```bash
586
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-test-pre-gen.sh" ".claude/afc/specs/{feature}/tasks.md" "spec/"
587
+ ```
588
+ 2. Review generated skeleton files — verify they are parseable:
589
+ ```bash
590
+ {config.test} # should show Pending examples, not errors
591
+ ```
592
+ 3. Create `.claude/afc/specs/{feature}/tests-pre.md` listing generated test expectations per task
593
+ 4. Progress: ` ├─ TDD pre-gen: {N} skeletons generated`
594
+
595
+ **If not triggered** (no `.sh` tasks): skip silently.
596
+
597
+ **Note**: Generated tests contain `Pending` examples — implementation agents replace these with real assertions during implementation.
598
+
599
+ #### Step 3.3: Blast Radius Analysis (conditional)
600
+
601
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase blast-radius`
602
+
603
+ **Trigger condition**: plan.md File Change Map lists >= 3 files to change.
604
+
605
+ **If triggered**:
606
+ 1. Run the blast radius analysis:
607
+ ```bash
608
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-blast-radius.sh" ".claude/afc/specs/{feature}/plan.md" "${CLAUDE_PROJECT_DIR}"
609
+ ```
610
+ 2. If exit 1 (cycle detected): **ESCALATE** — present the cycle to user with options:
611
+ - Option 1: Refactor plan to break the cycle
612
+ - Option 2: Acknowledge the cycle and proceed (mark as [DEFERRED])
613
+ 3. If high fan-out files detected (>5 dependents): emit warning, add as RISK note in plan.md
614
+ 4. Save output to `.claude/afc/specs/{feature}/impact.md`
615
+ 5. Progress: ` ├─ Blast radius: {N} planned, {M} dependents`
616
+
617
+ **If not triggered** (< 3 files): skip silently (small changes have bounded blast radius).
618
+
619
+ #### Step 3.4: Execution
620
+
621
+ 0. **Baseline test** (follows implement.md Step 1, item 5): if `{config.test}` is non-empty, run `{config.test}` before starting task execution. On failure, report pre-existing test failures to user and ask: "(1) Proceed anyway (2) Fix first (3) Abort". On pass or empty config, continue.
622
+ 1. Execute tasks phase by phase using implement.md orchestration rules (sequential/batch/swarm based on [P] count)
623
+ 2. **Implementation Context injection**: Every sub-agent prompt includes the `## Implementation Context` section from plan.md **and relevant FR/AC items from spec.md** (ensures spec intent propagates to workers)
624
+ 3. Perform **3-4 step gate** on each Implementation Phase completion — **always** read `${CLAUDE_PLUGIN_ROOT}/docs/phase-gate-protocol.md` first. Cannot advance to next phase without passing the gate.
625
+ - On gate pass: create phase rollback point `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase-tag {phase_number}`
626
+ 4. Real-time `[x]` updates in tasks.md
627
+ 5. After full completion, run `{config.ci}` final verification
628
+ - On pass: `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" ci-pass` (releases Stop Gate)
629
+ - **On fail: Debug-based RCA** (replaces blind retry):
630
+ 1. Execute `/afc:debug` logic inline with the CI error output as input
631
+ 2. Debug performs RCA: error trace → data flow → hypothesis → targeted fix
632
+ 3. Re-run `{config.ci}` after fix
633
+ 4. If debug-fix cycle fails 3 times → **abort** (not a simple fix — requires user intervention)
634
+ 5. This replaces the previous "retry max 3 attempts" pattern with intelligent diagnosis
635
+
636
+ #### Step 3.5: Acceptance Test Generation (conditional)
637
+
638
+ **Trigger condition**: spec.md contains acceptance scenarios (Given/When/Then blocks) AND `{config.test}` is configured (non-empty).
639
+
640
+ **If triggered**:
641
+ 1. Extract all GWT (Given/When/Then) acceptance scenarios from spec.md
642
+ 2. Execute `/afc:test` logic inline — generate test cases from acceptance scenarios:
643
+ ```
644
+ For each acceptance scenario in spec.md:
645
+ - Map GWT to a test case: Given → Arrange, When → Act, Then → Assert
646
+ - Target file: determined by the component/module referenced in the scenario
647
+ - Test file location: follows project convention (test framework from Project Context)
648
+ ```
649
+ 3. Run `{config.test}` to verify tests pass against the implementation
650
+ - If tests fail → this reveals a gap between spec and implementation:
651
+ - Fixable implementation issue → apply targeted fix
652
+ - Spec-implementation mismatch → record as SC shortfall for Review phase
653
+ 4. Progress: ` ├─ Acceptance tests: {N} generated, {M} passing`
654
+
655
+ **If not triggered** (no GWT scenarios or no test framework configured): skip silently.
656
+
657
+ #### Step 3.6: Implement Critic Loop
658
+
659
+ > **Always** read `${CLAUDE_PLUGIN_ROOT}/docs/critic-loop-rules.md` first and follow it.
660
+
661
+ **Critic Loop until convergence** (safety cap: 5, follow Critic Loop rules):
662
+ - **SCOPE_ADHERENCE**: Compare `git diff` changed files against plan.md File Change Map. Flag any file modified that is NOT in the plan. Flag any planned file NOT modified. Provide "M of N files match" count.
663
+ - **ARCHITECTURE**: Validate changed files against `{config.architecture}` rules (layer boundaries, naming conventions, import paths). Provide "N of M rules checked" count.
664
+ - **CORRECTNESS**: Cross-check implemented changes against spec.md acceptance criteria (AC). Verify each AC has corresponding code. Provide "N of M AC verified" count.
665
+ - **SIDE_EFFECT_SAFETY**: For tasks that changed call order, error handling, or state flow: verify that callee behavior is compatible with the new usage. Read callee implementations when uncertain (do not rely on function names alone).
666
+ - **Adversarial 3-perspective** (mandatory each pass):
667
+ - Skeptic: "Which implementation assumption is most likely wrong?"
668
+ - Devil's Advocate: "How could this implementation be misused or fail unexpectedly?"
669
+ - Edge-case Hunter: "What input would cause this implementation to fail silently?"
670
+ - State one failure scenario per perspective. If realistic → FAIL + fix. If unrealistic → state quantitative rationale.
671
+ - FAIL → auto-fix, re-run `{config.ci}`, and continue. ESCALATE → pause, present options, resume after response. DEFER → record reason, mark clean.
672
+
673
+ 6. **Implement retrospective**: if unexpected problems arose that weren't predicted in Plan, record in `.claude/afc/specs/{feature}/retrospective.md` (for memory update in Clean)
674
+ 7. **Checkpoint**: phase transition already recorded by `afc-pipeline-manage.sh phase implement` at phase start
675
+ 8. Progress: `✓ 3/5 Implement complete ({completed}/{total} tasks, CI: ✓, Critic: converged ({N} passes, {M} fixes, {E} escalations))`
676
+
677
+ ### Skill Advisor Checkpoint D (Post-Implement)
678
+
679
+ > Evaluate auxiliary skill triggers AFTER implementation, BEFORE review. Budget: max 2 skills. Skip all if `ADVISOR_COUNT >= 5`.
680
+
681
+ **Intent evaluation** — Examine the implementation results and answer these questions:
682
+
683
+ | # | Question | Score 1–5 | If >= 3 | Skill | Mode |
684
+ |---|----------|-----------|---------|-------|------|
685
+ | D1 | Were **testable source files changed without corresponding test coverage**? Look at `git diff --name-only` — for each changed source file, does a test file covering its behavior also appear in the diff? Consider the project's test convention and whether the changed files contain logic that should be tested (skip config files, types-only files, static assets). Only evaluate if `{config.test}` is non-empty. | 1=all changes have test coverage, 5=critical logic changed with zero tests | test generation (general-purpose fork) | Enrich |
686
+ | D2 | Based on **past pipeline quality data**, is there reason to believe this implementation has hidden quality issues? Check `.claude/afc/memory/quality-history/*.json` (if exists) — have recent pipelines shown elevated critical findings? Are there recurring problem categories that this feature's changed files might be susceptible to? | 1=clean history or no history, 5=strong pattern of recurring issues in similar areas | pre-review QA (general-purpose fork) | Observe |
687
+
688
+ **If D1 >= 3 AND `{config.test}` is non-empty** (Enrich — skip if no test framework configured):
689
+ 1. Identify which changed source files lack test coverage — focus on files with meaningful logic (not config, not types, not assets):
690
+ ```
691
+ For each changed source file:
692
+ - Does the project have a test file for it? (check test directory patterns)
693
+ - Was that test file also modified in this diff?
694
+ - Does the source file contain testable exports? (functions, classes, handlers)
695
+ → List files that have testable logic but no test coverage in this diff
696
+ ```
697
+ 2. Invoke test generation (fork):
698
+ ```
699
+ Task("Coverage boost: {feature}", subagent_type: "general-purpose",
700
+ prompt: "Generate missing tests for recently implemented files.
701
+
702
+ ## Uncovered Files (testable logic, no test changes in this diff)
703
+ {list of uncovered source files with their full paths}
704
+
705
+ ## Instructions
706
+ 1. Read each uncovered file to understand its exports and behavior
707
+ 2. Read existing test files in the project for pattern reference
708
+ 3. Generate unit tests targeting:
709
+ - Exported functions/classes
710
+ - Edge cases and error paths
711
+ - Integration points (if the file calls other changed files)
712
+ 4. Follow the project's test framework: {config.test framework}
713
+ 5. Place test files following project convention
714
+ 6. Run {config.test} to verify tests pass
715
+ 7. Return: files created, test count, pass/fail status")
716
+ ```
717
+ 3. New test files automatically enter review scope (Phase 4)
718
+ 4. Increment `ADVISOR_COUNT`
719
+ 5. Progress: ` ├─ Skill Advisor [D]: test (score: {N}/5, {M} uncovered files → {K} test files generated)`
720
+
721
+ **If D2 >= 3** (Observe):
722
+ 1. Load `.claude/afc/memory/quality-history/*.json` (most recent 3 files, sorted by filename descending)
723
+ 2. Identify recurring problem categories and which changed files are most at risk:
724
+ ```
725
+ Task("Pre-review QA: {feature}", subagent_type: "general-purpose",
726
+ prompt: "Perform a pre-review quality audit focused on historically problematic areas.
727
+
728
+ ## Changed Files
729
+ {git diff --name-only}
730
+
731
+ ## Quality History Context
732
+ {summary of patterns from recent quality-history reports — categories, frequencies, affected file types}
733
+
734
+ ## Instructions
735
+ 1. Focus on the recurring problem categories identified above
736
+ 2. Check: error handling completeness, input validation, resource cleanup
737
+ 3. Format your response EXACTLY as:
738
+ ## Pre-Review QA Findings
739
+ - [{severity}] {file}:{line} — {issue}: {suggested fix}
740
+ ## Priority Hints for Review
741
+ - {file}: focus on {area} (historically problematic)
742
+ 4. Read-only — do NOT modify any files")
743
+ ```
744
+ 3. Store output as `QA_FINDINGS` → injected into Phase 4 review context
745
+ 4. Review phase uses "Priority Hints" to focus attention
746
+ 5. Increment `ADVISOR_COUNT`
747
+ 6. Progress: ` ├─ Skill Advisor [D]: qa (score: {N}/5, {M} priority hints for review)`
748
+
749
+ **If all scores < 3**: proceed silently to Phase 4.
750
+
751
+ ### Phase 4: Review (4/5)
752
+
753
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase review`
754
+
755
+ Execute `/afc:review` logic inline — **follow all review perspectives defined in `skills/review/SKILL.md`** (A through H). The review skill is the single source of truth for review criteria.
756
+
757
+ **Context reload**: Re-read `.claude/afc/specs/{feature}/context.md` (contains full AC) and `.claude/afc/specs/{feature}/spec.md` to ensure spec context is available for SPEC_ALIGNMENT validation (these may have been compacted since Phase 1).
758
+
759
+ #### Step 4.1: Collect Review Targets
760
+
761
+ 1. Collect changed files via `git diff HEAD`
762
+ 2. Read **full content** of each changed file (not just the diff — full context needed for review)
763
+
764
+ #### Step 4.2: Reverse Impact Analysis
765
+
766
+ Before reviewing, identify **files affected by the changes** (not just the changed files themselves):
767
+
768
+ 1. **For each changed file**, find files that depend on it:
769
+ - **LSP (preferred)**: `LSP(findReferences)` on exported symbols — tracks type references, function calls, re-exports
770
+ - **Grep (fallback)**: `Grep` for `import.*{filename}`, `require.*{filename}`, `source.*{filename}` patterns across the codebase
771
+ - LSP and Grep are complementary — use both when LSP is available
772
+
773
+ 2. **Build impact map**:
774
+ ```
775
+ Impact Map:
776
+ ├─ src/auth/login.ts (changed)
777
+ │ └─ affected: src/pages/LoginPage.tsx, src/middleware/auth.ts
778
+ └─ Total: {N} changed files → {M} affected files
779
+ ```
780
+
781
+ 3. **Scope decision**: Affected files are NOT full review targets. Include them as **cross-reference context** in review and cross-boundary verification. If an affected file has >3 references to a changed symbol → flag for closer inspection.
782
+
783
+ 4. **Limitations** (include in review output):
784
+ > ⚠ Dynamic dependencies not covered: runtime dispatch, reflection, cross-language calls, config/env-driven branching.
785
+
786
+ #### Step 4.3: Scaled Review Orchestration
787
+
788
+ Choose review orchestration based on the number of changed files:
789
+
790
+ **Pre-scan: Call Chain Context** (for Parallel Batch and Review Swarm modes only):
791
+ Before distributing files to review agents, collect cross-boundary context:
792
+ 1. For each changed file, identify **outbound calls** to other changed files (imports + function calls)
793
+ 2. For each outbound call target, extract: function signature + 1-line side-effect summary
794
+ 3. Include the **Impact Map** from Step 4.2 — each agent receives the list of affected files
795
+ 4. Include this context in each review agent's prompt as `## Cross-File Context`
796
+
797
+ For Direct review mode (≤5 files): skip pre-scan — orchestrator already has full context.
798
+
799
+ **5 or fewer files**: Direct review — review all files directly in the current context (no delegation).
800
+
801
+ **6–10 files**: Parallel Batch — distribute to parallel review agents (2–3 files per agent) in a **single message**:
802
+ ```
803
+ Task("Review: {file1, file2}", subagent_type: "general-purpose")
804
+ Task("Review: {file3, file4}", subagent_type: "general-purpose")
805
+ ```
806
+
807
+ **11+ files**: Review Swarm — group files into batches (2-3 per worker), spawn N review workers in a **single message** (N = min(5, file count / 2)). Review is read-only — no write race conditions.
808
+
809
+ #### Step 4.4: Specialist Agent Delegation (parallel, perspectives B and C)
810
+
811
+ Launch architect and security agents in a **single message** to leverage their persistent memory:
812
+ ```
813
+ Task("Architecture Review: {feature}", subagent_type: "afc:afc-architect",
814
+ prompt: "Review the following changed files for architecture compliance.
815
+
816
+ ## Changed Files
817
+ {list of changed files from git diff}
818
+
819
+ ## Architecture Rules
820
+ {config.architecture}
821
+
822
+ ## Instructions
823
+ 1. Read your MEMORY.md for prior architecture patterns and ADRs
824
+ 2. Check each file against architecture rules (layer boundaries, naming, placement)
825
+ 3. Cross-reference with ADRs recorded during Plan phase — any violations?
826
+ 4. Return findings as: severity (Critical/Warning/Info), file:line, issue, suggested fix
827
+ 5. Update your MEMORY.md with any new architecture patterns discovered")
828
+
829
+ Task("Security Review: {feature}", subagent_type: "afc:afc-security",
830
+ prompt: "Scan the following changed files for security vulnerabilities.
831
+
832
+ ## Changed Files
833
+ {list of changed files from git diff}
834
+
835
+ ## Instructions
836
+ 1. Read your MEMORY.md for known vulnerability patterns and false positives
837
+ 2. Check for: command injection, path traversal, unvalidated input, sensitive data exposure
838
+ 3. Skip patterns recorded as false positives in your memory
839
+ 4. Return findings as: severity (Critical/Warning/Info), file:line, issue, suggested fix
840
+ 5. Include any new vulnerability patterns or confirmed false positives in your response (orchestrator will record them)")
841
+ ```
842
+ - Collect agent outputs and merge into the consolidated review
843
+ - Agent findings inherit their severity classification directly
844
+
845
+ #### Step 4.5: Perform Review (8 perspectives)
846
+
847
+ Check across **8 perspectives** (A-H as defined in `skills/review/SKILL.md`):
848
+ - A. Code Quality — `{config.code_style}` compliance (direct review)
849
+ - B. Architecture — **delegated to afc-architect agent** (persistent memory, ADR-aware)
850
+ - C. Security — **delegated to afc-security agent** (persistent memory, false-positive-aware)
851
+ - D. Performance — framework-specific patterns from Project Context (direct review)
852
+ - E. Project Pattern Compliance — conventions and idioms (direct review)
853
+ - **F. Reusability** — DRY, shared utilities, abstraction level (direct review)
854
+ - **G. Maintainability** — AI/human comprehension, naming clarity, self-contained files (direct review)
855
+ - **H. Extensibility** — extension points, OCP, future modification cost (direct review)
856
+
857
+ #### Step 4.6: Cross-Boundary Verification (MANDATORY)
858
+
859
+ After individual/parallel reviews and specialist agents complete, the **orchestrator** MUST perform a cross-boundary check. This is a required step, not optional — skipping it is a review defect.
860
+
861
+ **For High complexity (Review Swarm) reviews**: This is especially critical because individual review agents cannot see cross-file interactions. The orchestrator MUST read callee implementations directly.
862
+
863
+ 0. **Impact Map integration**: Use the Impact Map from Step 4.2 to prioritize verification. Affected files with significant coupling to changed symbols (behavioral call references, not just type imports, especially in critical code paths) should be read and checked for breakage — even if no finding was raised against them.
864
+
865
+ 1. **Filter**: From all collected findings, select those involving:
866
+ - Call order changes (function A now calls B before C)
867
+ - Error handling modifications (try/catch scope changes, error propagation changes)
868
+ - State mutation changes (new writes to shared state, removed cleanup)
869
+
870
+ 2. **Verify**: For each behavioral finding rated Critical or Warning:
871
+ - **Read the callee's implementation** (the function/method being called) — this read is mandatory, not optional
872
+ - **Skip external dependencies**: If the callee is in `node_modules/`, `vendor/`, or other third-party directories, verify against type definitions or documented API contract instead. Note: "verified against types/docs, not source"
873
+ - Check: does the callee's internal behavior (side effects, state changes, return values) actually conflict with the change?
874
+ - If no conflict → downgrade: Critical → Info, Warning → Info (append "verified: no cross-boundary impact")
875
+ - If confirmed conflict → keep severity, enrich description with callee behavior details
876
+
877
+ 3. **False positive reference** (security-related findings only): Check `afc-security` agent's MEMORY.md `## False Positives` section if it exists. Known false positive patterns should be noted in findings.
878
+
879
+ 4. **Output**: Append verification summary before Review Output:
880
+ ```
881
+ Cross-Boundary Check: {N} behavioral findings verified
882
+ ├─ Confirmed: {M} (severity kept)
883
+ ├─ Downgraded: {K} (false positive — callee compatible)
884
+ └─ Skipped: {J} (no behavioral change)
885
+ ```
886
+
887
+ This step runs in the orchestrator context (not delegated), as it requires reading code across file boundaries.
888
+
889
+ #### Step 4.7: Inject Advisor Context
890
+
891
+ If Checkpoint D produced outputs, inject them into the review context:
892
+ - **`QA_FINDINGS`** (from D2): read the stored findings and include as "Priority Hints for Review" — focus review attention on historically problematic areas and pre-identified quality concerns.
893
+ - **New test files** (from D1): include in the review scope alongside implementation changes.
894
+
895
+ #### Step 4.8: Auto-specific Validations
896
+
897
+ 1. **Auto-resolved validation**: Check all `[AUTO-RESOLVED]` items from spec phase — does the implementation match the guess? Flag mismatches as Critical.
898
+ 2. **Past reviews check**: if `.claude/afc/memory/reviews/` exists, load the **most recent 15 files** (sorted by filename descending) and scan for recurring finding patterns across past review reports. Prioritize those areas.
899
+ 3. **Retrospective check**: if `.claude/afc/memory/retrospectives/` exists, load the **most recent 10 files** (sorted by filename descending) and check:
900
+ - Were there recurring Critical finding categories in past reviews? Prioritize those perspectives.
901
+ - Were there false positives that wasted effort? Reduce sensitivity for those patterns.
902
+
903
+ #### Step 4.9: Critic Loop
904
+
905
+ > **Always** read `${CLAUDE_PLUGIN_ROOT}/docs/critic-loop-rules.md` first and follow it.
906
+
907
+ **Critic Loop until convergence** (safety cap: 5, follow Critic Loop rules):
908
+ - COMPLETENESS: were all changed files reviewed across all 8 perspectives (A-H)?
909
+ - SPEC_ALIGNMENT: cross-check implementation against spec.md — (1) every SC verified with `{M}/{N}` count, (2) every acceptance scenario (GWT) has corresponding code path, (3) no spec constraint is violated
910
+ - SIDE_EFFECT_AWARENESS: For findings involving call order changes, error handling modifications, or state mutation changes: did the reviewer verify the callee's internal behavior? If a Critical finding assumes a side effect without reading the target implementation → auto-downgrade to Info with note "cross-boundary unverified". Provide "{M} of {N} behavioral findings verified" count.
911
+ - PRECISION: are there unnecessary changes? Are there out-of-scope modifications? Are findings actual issues, not false positives?
912
+ - FAIL → auto-fix and continue. ESCALATE → pause, present options, resume after response. DEFER → record reason, mark clean.
913
+
914
+ #### Step 4.10: Handling SC shortfalls
915
+
916
+ - Fixable → attempt auto-fix → re-run `{config.ci}` verification
917
+ - Not fixable → state in final report with reason (no post-hoc rationalization; record as Plan-phase target-setting error)
918
+
919
+ #### Step 4.11: Retrospective Entry (if new pattern found)
920
+
921
+ If this review reveals a recurring pattern not previously documented in `.claude/afc/memory/retrospectives/`:
922
+
923
+ Append to `.claude/afc/memory/retrospectives/{YYYY-MM-DD}.md`:
924
+ ```markdown
925
+ ## Pattern: {category}
926
+ **What happened**: {concrete description}
927
+ **Root cause**: {why this keeps occurring}
928
+ **Prevention rule**: {actionable rule — usable in future plan/implement phases}
929
+ **Severity**: Critical | Warning
930
+ ```
931
+
932
+ Only write if the pattern is new and actionable. Generic observations are prohibited.
933
+
934
+ #### Step 4.12: Archive Review Report
935
+
936
+ Persist the review results for memory:
937
+
938
+ 1. Write full review output (Summary table + Impact Analysis + Detailed Findings + Positives + Cross-Boundary Check) to `.claude/afc/specs/{feature}/review-report.md`
939
+ 2. Include metadata header:
940
+ ```markdown
941
+ # Review Report: {feature name}
942
+ > Date: {YYYY-MM-DD}
943
+ > Files reviewed: {count}
944
+ > Findings: Critical {N} / Warning {N} / Info {N}
945
+ ```
946
+ 3. This file is copied to `.claude/afc/memory/reviews/{feature}-{date}.md` during Clean phase before .claude/afc/specs/ deletion.
947
+
948
+ #### Step 4.12: Checkpoint & Progress
949
+
950
+ - **Checkpoint**: phase transition already recorded by `afc-pipeline-manage.sh phase review` at phase start
951
+ - Progress: `✓ 4/5 Review complete (Critical:{N} Warning:{N} Info:{N}, Cross-boundary: {M} verified, SC shortfalls: {N})`
952
+
953
+ ### Skill Advisor Checkpoint E (Post-Review)
954
+
955
+ > Evaluate auxiliary skill triggers AFTER review, BEFORE clean. Budget: max 1 skill. Skip all if `ADVISOR_COUNT >= 5`.
956
+
957
+ **Intent evaluation** — Examine review findings and retrospective history:
958
+
959
+ | # | Question | Score 1–5 | If >= 3 | Skill | Mode |
960
+ |---|----------|-----------|---------|-------|------|
961
+ | E1 | Are there **recurring problem patterns** across this and past pipelines that should be codified as project rules? Check `.claude/afc/memory/retrospectives/` — do the same types of issues (e.g., "missing error handling in hooks", "forgotten spec file updates") keep appearing? Also consider: did this pipeline's review reveal issues that match past retrospective patterns? | 1=no retrospective history or no patterns, 5=same issue type recurred 3+ times and is not yet a project rule | pattern promotion (general-purpose fork, with learner guardrails) | Observe |
962
+
963
+ **If E1 >= 3** (Observe):
964
+ 1. Read retrospective files and identify recurring pattern categories:
965
+ - What types of issues keep recurring?
966
+ - Are they already covered by existing rules in `.claude/rules/afc-learned.md`?
967
+ - Would a project rule have prevented the recurrence?
968
+ 2. Invoke learner:
969
+ ```
970
+ Task("Pattern promotion: {feature}", subagent_type: "general-purpose",
971
+ prompt: "Review recurring patterns for potential promotion to project rules.
972
+
973
+ ## Recurring Patterns
974
+ {list each pattern with: category, occurrence count, concrete examples from retrospective entries}
975
+
976
+ ## Current Review Findings
977
+ {summary of this pipeline's review findings that match retrospective patterns}
978
+
979
+ ## Current Rules
980
+ {read .claude/rules/afc-learned.md if it exists, else 'No learned rules yet'}
981
+
982
+ ## Instructions
983
+ 1. For each recurring pattern, evaluate:
984
+ - Is it actionable? (specific enough to enforce)
985
+ - Is it already covered by existing rules?
986
+ - Would enforcing it have prevented the recurrence?
987
+ 2. For patterns worth promoting, write a rule in this format:
988
+ ### {Category}
989
+ - **Rule**: {concise, enforceable statement}
990
+ - **Rationale**: {why — based on {N} occurrences across pipelines}
991
+ - **Enforcement**: {how to check — linter, review criterion, or convention}
992
+ 3. **Safety guardrails** (mandatory):
993
+ - Do NOT create rules about: permissions, security policies, hook behavior, tool access
994
+ - Do NOT create rules that contradict existing CLAUDE.md or .claude/rules/ content
995
+ - Each rule must be scoped to code conventions only (naming, style, workflow, testing, architecture)
996
+ - Verify no duplicate or contradictory rule exists before appending
997
+ 4. Append new rules to .claude/rules/afc-learned.md (create if absent)
998
+ 5. Do NOT duplicate existing rules
999
+ 6. Return: {N} patterns evaluated, {M} promoted, {K} already covered")
1000
+ ```
1001
+ 3. Increment `ADVISOR_COUNT`
1002
+ 4. Progress: ` ├─ Skill Advisor [E]: learner (score: {N}/5, {M} patterns evaluated, {K} promoted to rules)`
1003
+
1004
+ **If score < 3**: proceed silently to Phase 5.
1005
+
1006
+ ### Phase 5: Clean (5/5)
1007
+
1008
+ `"${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" phase clean`
1009
+
1010
+ Artifact cleanup and codebase hygiene check after implementation and review:
1011
+
1012
+ 1. **Artifact cleanup** (scope-limited):
1013
+ - **Delete only the `.claude/afc/specs/{feature}/` directory created by the current pipeline**
1014
+ - If other `.claude/afc/specs/` subdirectories exist, **do not delete them** (only inform the user of their existence)
1015
+ - Do not leave pipeline intermediate artifacts in the codebase
1016
+ 2. **Dead code scan** (prefer external tooling over LLM judgment):
1017
+ - Run `{config.gate}` / `{config.ci}` — most linters detect unused imports/variables automatically
1018
+ - If the project has dedicated dead code tools (e.g., `eslint --rule 'no-unused-vars'`, `ts-prune`, `knip`), use them first
1019
+ - Only fall back to LLM-based scan for detection that static tools cannot cover
1020
+ - Remove empty directories from moved/deleted files
1021
+ - Detect unused exports (re-exports of moved code from original locations etc.)
1022
+ 3. **Final CI gate**:
1023
+ - Run `{config.ci}` final execution
1024
+ - Auto-fix on failure (max 2 attempts)
1025
+ 4. **Memory update** (if applicable):
1026
+ - Reusable patterns found during pipeline → record in `.claude/afc/memory/`
1027
+ - If there were `[AUTO-RESOLVED]` items → record decisions in `.claude/afc/memory/decisions/`
1028
+ - **If retrospective.md exists** → record as patterns missed by the Plan phase Critic Loop in `.claude/afc/memory/retrospectives/` (reuse as RISK checklist items in future runs)
1029
+ - **If review-report.md exists** → copy to `.claude/afc/memory/reviews/{feature}-{date}.md` before .claude/afc/specs/ deletion
1030
+ - **If research.md exists** and was not already persisted in Plan phase → copy to `.claude/afc/memory/research/{feature}.md`
1031
+ - **Agent memory consolidation**: Check each agent's MEMORY.md for bloat — if it contains redundant, obsolete, or superseded entries that reduce signal-to-noise ratio, invoke the agent to self-prune:
1032
+ ```
1033
+ Task("Memory cleanup: afc-architect", subagent_type: "afc:afc-architect",
1034
+ prompt: "Review your MEMORY.md. Read it, identify and prune old/redundant/obsolete entries, and rewrite it keeping only entries that are still relevant and non-overlapping.")
1035
+ ```
1036
+ Use semantic assessment (are entries still relevant? do entries overlap?) rather than a line-count threshold. (Same pattern for afc-security if needed.)
1037
+ - **Memory rotation**: For each memory subdirectory, assess whether the oldest files still provide value. Prune files that are superseded by newer entries, reference features/code that no longer exists, or overlap with other files. As a practical guideline, keep the most recent and relevant entries — if a directory has grown large enough that scanning it would be slow (roughly 30+ files), prioritize pruning the least relevant entries:
1038
+ | Directory | Pruning Intent | Soft Guideline |
1039
+ |-----------|---------------|----------------|
1040
+ | `quality-history/` | Remove superseded or redundant quality records | ~30 files |
1041
+ | `reviews/` | Remove reviews for features no longer in the codebase | ~40 files |
1042
+ | `retrospectives/` | Remove retrospectives whose learnings are already captured elsewhere | ~30 files |
1043
+ | `research/` | Remove research for libraries/patterns no longer used | ~50 files |
1044
+ | `decisions/` | Remove decisions that have been reversed or are no longer relevant | ~60 files |
1045
+ - These numbers are soft guidelines, not hard cutoffs — use judgment based on relevance
1046
+ - Sort by filename ascending (oldest first) when pruning by recency
1047
+ - Log: `"Memory rotation: {dir} pruned {N} files"`
1048
+ - Skip directories that do not exist or clearly do not need pruning
1049
+ 5. **Quality report** (structured pipeline metrics):
1050
+ - Generate `.claude/afc/memory/quality-history/{feature}-{date}.json` with the following structure:
1051
+ ```json
1052
+ {
1053
+ "feature": "{feature}",
1054
+ "date": "{YYYY-MM-DD}",
1055
+ "phases": {
1056
+ "clarify": { "triggered": true/false, "questions": N, "auto_resolved": N },
1057
+ "spec": { "user_stories": N, "requirements": { "FR": N, "NFR": N }, "researched": N, "auto_resolved": N, "critic_passes": N, "critic_fixes": N, "escalations": N },
1058
+ "plan": { "files_planned": N, "implementation_context_words": N, "adr_recorded": N, "adr_conflicts": N, "research_persisted": true/false, "critic_passes": N, "critic_fixes": N, "escalations": N },
1059
+ "implement": {
1060
+ "tasks": { "total": N, "parallel": N, "phases": N },
1061
+ "test_pre_gen": { "triggered": true/false, "skeletons": N },
1062
+ "blast_radius": { "triggered": true/false, "dependents": N, "high_fan_out": N },
1063
+ "completed": N, "total": N, "ci_passes": N, "ci_failures": N,
1064
+ "acceptance_tests": { "triggered": true/false, "generated": N, "passing": N },
1065
+ "debug_rca": { "triggered": true/false, "cycles": N },
1066
+ "critic_passes": N, "critic_fixes": N, "escalations": N
1067
+ },
1068
+ "review": { "critical": N, "warning": N, "info": N, "sc_shortfalls": N, "auto_resolved_mismatches": N,
1069
+ "architect_agent": { "invoked": true/false, "findings": N },
1070
+ "security_agent": { "invoked": true/false, "findings": N },
1071
+ "critic_passes": N, "critic_fixes": N, "escalations": N }
1072
+ },
1073
+ "totals": { "changed_files": N, "auto_resolved": N, "escalations": N, "totalPromptCount": N }
1074
+ }
1075
+ ```
1076
+ - Create `.claude/afc/memory/quality-history/` directory if it does not exist
1077
+ 6. **Checkpoint reset**:
1078
+ - Clear `.claude/afc/memory/checkpoint.md` **and** `~/.claude/projects/{ENCODED_PATH}/memory/checkpoint.md` (pipeline complete = session goal achieved, dual-delete prevents stale checkpoint in either location; `ENCODED_PATH` = project path with `/` replaced by `-`)
1079
+ 7. **Timeline finalize**:
1080
+ ```bash
1081
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" log pipeline-end "Pipeline complete: {feature}"
1082
+ ```
1083
+ 8. **Release Pipeline Flag** (hook integration):
1084
+ ```bash
1085
+ "${CLAUDE_PLUGIN_ROOT}/scripts/afc-pipeline-manage.sh" end
1086
+ ```
1087
+ - Stop Gate Hook deactivated
1088
+ - Change tracking log deleted
1089
+ - Safety tag removed (successful completion)
1090
+ - Phase rollback tags removed (handled automatically by pipeline end)
1091
+ 9. **Checkpoint**: phase transition already recorded by `afc-pipeline-manage.sh phase clean` at phase start
1092
+ 10. Progress: `✓ 5/5 Clean complete (deleted: {N}, dead code: {N}, CI: ✓)`
1093
+
1094
+ ### Final Output
1095
+
1096
+ ```
1097
+ Auto pipeline complete: {feature}
1098
+ ├─ 1/5 Spec: US {N}, FR {N}, researched {N}
1099
+ ├─ 2/5 Plan: Critic converged ({N} passes), ADR {N} recorded, Implementation Context {W} words
1100
+ ├─ 3/5 Implement: {completed}/{total} tasks ({P} parallel), CI ✓
1101
+ │ ├─ TDD: {triggered/skipped}, Blast Radius: {triggered/skipped}
1102
+ │ ├─ Acceptance Tests: {N} generated ({M} passing) / skipped
1103
+ │ └─ Critic: converged ({N} passes, {M} fixes, {E} escalations)
1104
+ ├─ 4/5 Review: Critical:{N} Warning:{N} Info:{N}
1105
+ │ ├─ Perspectives: Quality, Architecture*, Security*, Performance, Patterns, Reusability, Maintainability, Extensibility
1106
+ │ └─ (* = delegated to persistent-memory agent)
1107
+ ├─ 5/5 Clean: {N} artifacts deleted, {N} dead code removed
1108
+ ├─ Skill Advisor: {ADVISOR_COUNT} auxiliary skills invoked
1109
+ │ {for each invoked: ├─ [{checkpoint}] {skill}: {summary}}
1110
+ ├─ Changed files: {N}
1111
+ ├─ Auto-resolved: {N} ({M} validated in review)
1112
+ ├─ Agent memory: architect {updated/skipped}, security {updated/skipped}
1113
+ ├─ Retrospective: {present/absent}
1114
+ └─ .claude/afc/specs/{feature}/ cleaned up
1115
+ ```
1116
+
1117
+ ## Abort Conditions
1118
+
1119
+ **Abort** the pipeline and report to user in these situations:
1120
+
1121
+ 1. `{config.ci}` fails 3 consecutive times
1122
+ 2. File conflict during implementation (overlaps with changes from another branch)
1123
+ 3. Critical security issue found (cannot auto-fix)
1124
+
1125
+ On abort:
1126
+ ```
1127
+ Pipeline aborted (Phase {N}/5)
1128
+ ├─ Reason: {abort cause}
1129
+ ├─ Completed phases: {completed list}
1130
+ ├─ Rollback: git reset --hard afc/pre-auto (restores state before implementation)
1131
+ ├─ Checkpoint: .claude/afc/memory/checkpoint.md (last phase gate passed)
1132
+ ├─ Artifacts: .claude/afc/specs/{feature}/ (partial completion, manual deletion needed if Clean did not run)
1133
+ └─ Resume: /afc:resume → /afc:implement (checkpoint-based)
1134
+ ```
1135
+
1136
+ ## Notes
1137
+
1138
+ - **Full auto does not mean uncritical**: Phase 0.3 Request Triage may reject, reduce, or redirect requests before the pipeline invests resources. "Auto" automates execution, not judgment.
1139
+ - **Full auto**: runs to completion without intermediate confirmation. Fast but direction cannot be changed mid-run.
1140
+ - **Review auto-resolved items**: items tagged `[AUTO-RESOLVED]` are estimates; review after the fact is recommended.
1141
+ - **Large feature warning**: warn before starting if more than 5 User Stories are expected.
1142
+ - **Read existing code first**: always read existing files before modifying. Do not blindly generate code.
1143
+ - **Follow project rules**: project rules in `afc.config.md` and `CLAUDE.md` take priority.
1144
+ - **Critic Loop is not a ritual**: a single "PASS" line is equivalent to not running Critic at all. Always follow the format in the Critic Loop rules section. Critic uses convergence-based termination — it may finish in 1 pass or take several, depending on the output quality.
1145
+ - **ESCALATE pauses auto mode**: when a Critic finds an ambiguous issue requiring user judgment, the pipeline pauses and presents options via AskUserQuestion. Auto mode automates clear decisions but escalates ambiguous ones.
1146
+ - **Tasks phase is absorbed**: tasks.md is generated automatically at implement start from plan.md's File Change Map. No separate tasks phase or tasks critic loop. Validation is script-based (DAG + parallel overlap checks).
1147
+ - **[P] parallel is mandatory**: if a [P] marker is assigned in tasks.md, it must be executed in parallel. Orchestration mode (batch vs swarm) is selected automatically based on task count. Sequential substitution is prohibited.
1148
+ - **Swarm mode is automatic**: when a phase has 6+ [P] tasks, the orchestrator pre-assigns tasks to swarm workers. Do not manually batch.
1149
+ - **Implementation Context travels with workers**: every sub-agent prompt includes the Implementation Context section from plan.md, ensuring spec intent propagates to parallel workers.
1150
+ - **Session context resilience**: key decisions are written to `.claude/afc/specs/{feature}/context.md` at Plan completion and read at Implement start, surviving context compaction.
1151
+ - **Specialist agents enhance review**: afc-architect and afc-security agents are invoked during Review to provide persistent-memory-aware analysis. Their findings are merged into the consolidated review. Agent memory updates happen automatically during the agent call.
1152
+ - **Debug-based RCA replaces blind retry**: CI failures trigger `/afc:debug` logic (hypothesis → targeted fix) instead of generic "retry 3 times". This produces better fixes and records patterns via retrospective.
1153
+ - **Acceptance tests close the spec-to-code gap**: When spec contains GWT scenarios and a test framework is configured, acceptance tests are auto-generated after implementation, verifying spec intent is met.
1154
+ - **Research and ADR persist across sessions**: Research findings are saved to `.claude/afc/memory/research/`, ADRs to architect agent memory. Future pipelines can reference these to avoid re-research and detect conflicts.
1155
+ - **No out-of-scope deletion**: do not delete files/directories in Clean that were not created by the current pipeline.
1156
+ - **NEVER use `run_in_background: true` on Task calls**: agents must run in foreground so results are returned before the next step.