Skip to content

Commit f8e648f

Browse files
committed
Refactor architecture decision processes across templates
- Updated `pb-build` templates to enforce architecture decision adherence, ensuring that all tasks and subagents follow the approved design patterns and principles. - Enhanced `pb-init` templates to capture architecture decision snapshots, including established patterns and dependency injection boundaries. - Revised `pb-plan` templates to require explicit architecture decisions before implementation, emphasizing the importance of SRP, DIP, and other architectural principles. - Improved documentation in `design_template.md` and `tasks_template.md` to clarify architecture decision requirements and project identity alignment. - Added tests to verify that templates enforce architecture decision rules and capture necessary context for implementers. - Bumped version to 0.8.0 to reflect these changes.
1 parent 17ed67f commit f8e648f

File tree

13 files changed

+243
-25
lines changed

13 files changed

+243
-25
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pb-spec follows a **harness-first** philosophy: reliability comes from process d
3434
- **Behavior Before Code:** `/pb-plan` turns user-visible requirements into Gherkin `.feature` scenarios before implementation begins.
3535
- **Verification by Design:** Planning requires explicit verification commands so completion is measurable.
3636
- **Observability as Context:** Service-facing tasks must capture runtime evidence (log tails and/or health probes), not only test output.
37+
- **Architecture Before Implementation:** `/pb-init` snapshots established architecture decisions, `/pb-plan` records explicit `Architecture Decisions`, and `/pb-build` executes against that contract instead of inventing a new one.
3738
- **Double-Loop Execution:** `/pb-build` enforces a BDD outer loop plus a TDD inner loop with per-task status tracking.
3839
- **Escalation Over Thrashing:** Three consecutive failures suspend the current task and route a standardized DCR packet to `/pb-refine`.
3940
- **Safe Failure Recovery:** Failed attempts use scoped recovery guidance to avoid polluting unrelated workspace state.
@@ -131,6 +132,8 @@ Merge behavior is non-destructive:
131132

132133
This design avoids relying on any fixed `AGENTS.md` section layout and protects user-maintained constraints across re-runs.
133134

135+
The managed snapshot now also includes an **Architecture Decision Snapshot** so later agents inherit repo-level conventions instead of re-deciding them every run. Typical entries include established patterns, dependency-injection boundaries, error-handling conventions, and workflow/state-modeling rules.
136+
134137
### 2. `/pb-plan <requirement>` — Design & Task Planning
135138

136139
Takes a natural-language requirement and produces a complete feature spec:
@@ -153,6 +156,8 @@ It also performs two additional planning audits before implementation starts:
153156
- Template identity alignment: if the repo still contains generic crate/package/module names from a scaffold, `pb-plan` must front-load renaming those identifiers to project-matching names.
154157
- Risk-based advanced testing: property testing is planned by default for broad input-domain logic, while fuzzing and benchmarks are added only when the feature profile justifies them. Tool selection follows repo language conventions: `Hypothesis` / `fast-check` / `proptest`, `Atheris` / `jazzer.js` / `cargo-fuzz`, and `pytest-benchmark` / `Vitest Bench` / `criterion`.
155158

159+
It also adds an explicit **Architecture Decisions** section to `design.md`. For work that introduces a new boundary or is likely to exceed 200 lines, planning must evaluate **SRP**, **DIP**, and the classic patterns **Factory**, **Strategy**, **Observer**, **Adapter**, and **Decorator**. The chosen pattern must be justified against alternatives and checked against the code-simplification lens so the design stays simpler, not just more abstract.
160+
156161
### 3. `/pb-refine <feature-name>` — Design Iteration (Optional)
157162

158163
Reads user feedback or Design Change Requests (from failed builds, including standardized 3-failure build-block packets) and intelligently updates `design.md` and `tasks.md`. It maintains a revision history and cascades design changes to the task list without overwriting completed work. `AGENTS.md` remains read-only in this phase.
@@ -161,6 +166,8 @@ Reads user feedback or Design Change Requests (from failed builds, including sta
161166

162167
Reads `specs/<YYYY-MM-DD-NO-feature-name>/tasks.md` and implements each task sequentially. Every `BDD+TDD` task is executed by a fresh subagent following an outside-in double loop: run the Gherkin scenario first so the BDD outer loop is red, drive the implementation with TDD (Red → Green → Refactor) in the inner loop, then re-run the scenario until it passes. Runtime verification (log/health evidence when applicable) still applies. Supports **Design Change Requests** if the planned design proves infeasible during implementation, and auto-escalates to DCR after three consecutive task failures. Only the `<feature-name>` part is needed when invoking — the agent resolves the full directory automatically. `AGENTS.md` is read-only unless the user explicitly requests an `AGENTS.md` change.
163168

169+
`/pb-build` is now explicitly architecture-bound: it reads the repo's **Architecture Decision Snapshot**, follows the feature's **Architecture Decisions**, re-checks **SRP** and **DIP** during execution, and keeps external dependencies behind interfaces or abstract classes when the design requires that seam. It should not improvise a different Factory, Strategy, Observer, Adapter, or Decorator choice mid-build.
170+
164171
## Skills Overview
165172

166173
| Skill | Trigger | Output | Description |
@@ -179,6 +186,7 @@ pb-spec's prompt design is inspired by Anthropic's research on [Effective Harnes
179186
| Principle | How pb-spec Implements It |
180187
|---|---|
181188
| **State Grounding** | Subagents must verify workspace state (`ls`, `find`, `read_file`) before writing any code — preventing path hallucination |
189+
| **Architecture Continuity** | `pb-init` records an Architecture Decision Snapshot, `pb-plan` makes Architecture Decisions explicit, and `pb-build` verifies implementation still conforms to that contract |
182190
| **Error Quoting** | Subagents must quote specific error messages before attempting fixes — preventing blind debugging |
183191
| **Context Hygiene** | Orchestrator passes only minimal, relevant context to each subagent — preventing context window pollution |
184192
| **Recovery Loop** | Failed tasks use pre-task snapshots + file-scoped recovery (`git restore` + task-local cleanup), and avoid workspace-wide restore in dirty trees |

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "uv_build"
44

55
[project]
66
name = "pb-spec"
7-
version = "0.7.1"
7+
version = "0.8.0"
88
description = "Plan-Build Spec (pb-spec): A CLI tool for managing AI coding assistant skills"
99
readme = "README.md"
1010
license = "Apache-2.0"

src/pb_spec/templates/prompts/pb-build.prompt.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Run this when the user invokes `/pb-build <feature-name>`.
99
- Complete unfinished tasks in `tasks.md` sequentially until done or explicitly blocked.
1010
- Use one fresh subagent per task with minimal, task-relevant context only.
1111
- Mark a task as done only after `BDD Verification` passes for `BDD+TDD` tasks, tests pass, task verification passes, and runtime evidence is captured when applicable.
12+
- Treat `design.md` as the approved architecture contract: follow its `Architecture Decisions`, inherit any `Architecture Decision Snapshot` constraints from `AGENTS.md`, and do not improvise a new pattern mid-build.
1213
- If blocked, fail clearly with exact task ID, failed command, and concrete next options (retry/skip/abort within budget, then DCR escalation).
1314

1415
---
@@ -63,15 +64,18 @@ For each unfinished task, in order:
6364
1. **Extract** the full task block (Context, Scenario Coverage, Loop Type, Steps, BDD Verification, Verification).
6465
2. **Gather context** — read `design.md` and `AGENTS.md` (if it exists). Treat `AGENTS.md` as read-only policy context.
6566
- Read any referenced `.feature` files under `specs/<spec-dir>/features/`.
67+
- Extract the relevant `Architecture Decisions` from `design.md` and any `Architecture Decision Snapshot` constraints from `AGENTS.md` that apply to this task.
6668
- Record a pre-task workspace snapshot (`git status --porcelain` + tracked/untracked file lists) for safe rollback.
6769
3. **Spawn a fresh subagent** with the Implementer Prompt (below), filled in with the task content and project context.
6870
**Context Hygiene:** Do NOT pass the entire chat history. Pass ONLY:
6971
- The specific Task Description from `tasks.md`.
7072
- The `AGENTS.md` (project constraints and hard rules; do not assume any fixed template layout).
7173
- The `design.md` (Feature Spec).
7274
- The relevant `.feature` file content and scenario name when `Loop Type` is `BDD+TDD`.
75+
- The task-relevant `Architecture Decisions` and `Architecture Decision Snapshot` excerpts, including any SRP, DIP, Factory, Strategy, Observer, Adapter, or Decorator choice and any requirement to route external dependencies through interfaces or abstract classes.
7376
- **Summary of previous tasks** — a one-line-per-task summary (e.g., "Task 1.1 created `models.py` with `User` class."). Do NOT pass raw logs or full outputs.
7477
4. **Subagent executes** the BDD + TDD + runtime verification cycle (see Implementer Prompt section).
78+
- The subagent must restate the architecture contract before coding and verify it still conforms after implementation.
7579
5. **Mark completed** — update `- [ ]` to `- [x]` and Status to `🟢 DONE` in `tasks.md`.
7680
- **Use precise editing:** Use `sed`, string-replacement, or line-targeted edits to update the specific Task ID heading and its checkboxes. Do NOT rewrite the entire `tasks.md` file — this risks truncation and content loss in large files.
7781
- **Completion gate:** Mark done only when `BDD Verification` is satisfied for `BDD+TDD` tasks, task Verification is satisfied, tests are green, and runtime checks (when applicable) are evidence-backed.
@@ -177,10 +181,10 @@ Summary must be factual and command-backed: do not claim "passed" or "completed"
177181
## Subagent Rules
178182

179183
1. **One subagent per task.** Never combine tasks.
180-
2. **Fresh context per subagent.** Only: task description, non-obvious constraints (AGENTS.md) + design (design.md), summary of completed tasks, files on disk.
184+
2. **Fresh context per subagent.** Only: task description, non-obvious constraints (AGENTS.md) + design (design.md), relevant `Architecture Decisions`, summary of completed tasks, files on disk.
181185
3. **Sequential execution.** Strict `tasks.md` order. No parallelism.
182186
4. **Independence.** Cross-task state lives in files, not memory.
183-
5. **Grounding first.** Every subagent verifies workspace state before writing code.
187+
5. **Grounding first.** Every subagent verifies workspace state before writing code and restates the architecture contract it must preserve.
184188
6. **Verifiable closure.** A task closes only after explicit verification evidence.
185189

186190
---
@@ -252,6 +256,7 @@ Update `tasks.md` in-place after each task using **precise edits** (target the s
252256
9. **Context hygiene.** Pass minimal, relevant context. Summarize — don't dump.
253257
10. **Evidence over assertion.** Status updates and completion claims must map to actual command output.
254258
11. **Escalate deterministically.** After three consecutive failures, stop thrashing and route to `pb-refine` with a structured DCR.
259+
12. **Architecture decisions are binding.** `pb-build` executes the approved design; it does not invent a different architecture during implementation.
255260

256261
---
257262

@@ -273,7 +278,7 @@ You are implementing **Task {{TASK_NUMBER}}: {{TASK_NAME}}**.
273278

274279
{{PROJECT_CONTEXT}}
275280

276-
> From `AGENTS.md` (project constraints and rules) and `design.md` (feature design decisions).
281+
> From `AGENTS.md` (project constraints and rules), the repo's `Architecture Decision Snapshot`, and `design.md` (feature `Architecture Decisions`).
277282
278283
### Your Job
279284

@@ -285,6 +290,7 @@ Before coding, define a compact task contract from the provided task block:
285290
- What must not change
286291
- How success is verified
287292
- Which `Scenario Coverage` entries and scenario name apply to this task
293+
- Which `Architecture Decisions` are binding for this task, including any SRP, DIP, Factory, Strategy, Observer, Adapter, or Decorator choice
288294

289295
**1. Grounding & State Verification (Mandatory)**
290296

@@ -294,6 +300,8 @@ Before writing any code, verify the current workspace state:
294300
- **Read Context:** Read target files to understand surrounding code and current state.
295301
- **Check Dependencies:** Verify modules you plan to import actually exist.
296302
- **Read `design.md`** for overall design context.
303+
- **Read the `Architecture Decisions` section** and restate the specific architecture contract for this task before coding.
304+
- **Read the `Architecture Decision Snapshot`** from `AGENTS.md` when present and preserve any repo-level constraints it establishes.
297305
- Identify existing patterns to follow.
298306
- Confirm task boundaries to avoid scope bleed.
299307

@@ -309,6 +317,7 @@ Before writing any code, verify the current workspace state:
309317
| **BDD OUTER GREEN** | Re-run the BDD scenario until it passes for `BDD+TDD` tasks. | BDD scenario passes |
310318
| **Runtime Verification (if applicable)** | Run runtime checks from task Verification, capture logs + probe output (or explicit `N/A` reason). | Runtime evidence captured |
311319
| **REFACTOR** | Clean up if needed | ALL tests still pass |
320+
| **ARCHITECTURE CHECK** | Confirm the implementation still follows the selected `Architecture Decisions`, preserves SRP and DIP, and keeps external dependencies behind interfaces or abstract classes when required. | Architecture contract preserved |
312321
| **SCOPE CHECK** | Confirm implemented changes match task contract and nothing extra. | Task scope respected |
313322

314323
**3. Self-Review Checklist**
@@ -319,6 +328,7 @@ Before writing any code, verify the current workspace state:
319328
- [ ] Test coverage — tests meaningfully verify requirements
320329
- [ ] No regressions — all pre-existing tests pass
321330
- [ ] BDD coverage — for `BDD+TDD` tasks, the referenced scenario failed first and then passed
331+
- [ ] Architecture conformance — the change still matches the selected `Architecture Decisions` and does not introduce a conflicting pattern
322332
- [ ] YAGNI — no over-engineering
323333
- [ ] Verification mapping — task's stated Verification is explicitly satisfied
324334

@@ -358,6 +368,8 @@ Fix any "no" answers before submitting.
358368
- Only implement the current task.
359369
- Follow YAGNI — no speculative features.
360370
- Use existing patterns — match project style.
371+
- Follow approved architecture decisions — respect the task's `Architecture Decisions` and the repo's `Architecture Decision Snapshot`; do not improvise a different pattern mid-build.
372+
- do not improvise a new pattern mid-build. If the planned architecture no longer fits, raise a Design Change Request instead of silently switching patterns.
361373
- Do not modify `design.md` or `tasks.md`.
362374
- Do not modify, delete, or reformat `AGENTS.md` unless the user explicitly requests an `AGENTS.md` change.
363375
- Do not modify unrelated code.

src/pb_spec/templates/prompts/pb-init.prompt.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,22 @@ Locate and list:
6262
- **CI**: `.github/workflows/`, `.gitlab-ci.yml`, `Jenkinsfile`
6363
- **Docs**: `README.md`, `docs/`, `CHANGELOG.md`
6464

65-
## Step 4: Detect Active Specs
65+
## Step 4: Capture Architecture Decision Snapshot
66+
67+
Search the project for explicit architecture decisions that later agents must inherit instead of reinventing:
68+
69+
1. Read `AGENTS.md`, `CLAUDE.md`, `README.md`, `docs/`, active `specs/`, and nearby source modules for stated or strongly evidenced conventions.
70+
2. Record only decisions grounded in explicit evidence. **Only record decisions grounded in explicit evidence** — do not speculate from vague naming alone.
71+
3. Capture the current project position for:
72+
- **Established Patterns** — e.g. Strategy, Adapter, typestate, repository, command objects
73+
- **Dependency Injection Boundaries** — how external services, SDKs, clients, or storage are abstracted and injected
74+
- **Error Handling Conventions** — shared error types, exception policies, result objects, recovery boundaries
75+
- **State and Workflow Modeling** — how state transitions, orchestration, and long-running flows are represented
76+
- **External Dependency Access** — whether network, filesystem, DB, or platform calls must go through interfaces, protocols, base classes, or wrapper services
77+
- **Known Exceptions / TODOs** — temporary deviations or pending cleanups already documented in the repo
78+
4. If the repo does not expose explicit architecture decisions yet, write `No explicit architecture decisions detected.` instead of inventing guidance.
79+
80+
## Step 5: Detect Active Specs
6681

6782
Check if a `specs/` directory exists. If so, list each subdirectory as an active feature spec. For each spec, perform **dynamic status detection**:
6883

@@ -80,7 +95,7 @@ Output format per spec:
8095
- `specs/<YYYY-MM-DD-NO-feature-name>/` — <status emoji> <status text> | Design: <design status> | Last modified: YYYY-MM-DD
8196
```
8297

83-
## Step 5: Write AGENTS.md (Managed Block, Non-Destructive)
98+
## Step 6: Write AGENTS.md (Managed Block, Non-Destructive)
8499

85100
Write or update `AGENTS.md` at the project root using a **marker-based managed block**. Do NOT parse or rewrite user sections based on heading names.
86101

@@ -128,6 +143,15 @@ This strategy is format-agnostic and prevents accidental loss of user-maintained
128143
- Config: {{config_path}}
129144
- Tests: {{tests_path}}
130145

146+
### Architecture Decision Snapshot
147+
148+
- Established Patterns: {{established_patterns}}
149+
- Dependency Injection Boundaries: {{dependency_injection_boundaries}}
150+
- Error Handling Conventions: {{error_handling_conventions}}
151+
- State and Workflow Modeling: {{state_and_workflow_modeling}}
152+
- External Dependency Access: {{external_dependency_access}}
153+
- Known Exceptions / TODOs: {{architecture_exceptions}}
154+
131155
### Suggested Conventions (Optional Defaults)
132156

133157
- Commit style: conventional commits

0 commit comments

Comments
 (0)