|
| 1 | +# Semantic Contracts |
| 2 | + |
| 3 | +> 18 composable contracts that define what terms mean in your project — |
| 4 | +> by composing established Semantic Anchors or by providing custom team definitions. |
| 5 | +> Add them to your AGENTS.md or CLAUDE.md. |
| 6 | +> Source: https://github.com/LLM-Coding/Semantic-Anchors |
| 7 | +> Select & copy: https://llm-coding.github.io/Semantic-Anchors/#/contracts |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Specification |
| 12 | + |
| 13 | +_What we mean when we say 'spec'_ |
| 14 | + |
| 15 | +When we talk about a "specification" or "spec", we mean: |
| 16 | +- Persona Use Cases in Cockburn's Fully Dressed format (Primary Actor, Trigger, Main Success Scenario, Extensions, Postconditions) at User Goal level, with Business Rules (BR-IDs) |
| 17 | +- System Use Cases for each technical interface (API endpoint, CLI command, event, file format): input/validation, processing, output/status codes, error responses |
| 18 | +- Activity Diagrams for all flows (not just the happy path) |
| 19 | +- Acceptance criteria in Gherkin format (Given/When/Then) |
| 20 | +- Individual requirements in EARS syntax where applicable (When/While/If/Shall) |
| 21 | +- Supplementary Specifications as needed: Entity Model, State Machines, Interface Contracts, Validation Rules |
| 22 | + |
| 23 | +*Referenced anchors: cockburn-use-cases, gherkin, bdd-given-when-then, ears-requirements* |
| 24 | + |
| 25 | +## Requirements Discovery |
| 26 | + |
| 27 | +_How we clarify requirements before writing specs_ |
| 28 | + |
| 29 | +Clarify requirements using the Socratic Method: |
| 30 | +- Ask at most 3 questions at a time, challenge assumptions |
| 31 | +- Use MECE to ensure questions cover all areas without overlap |
| 32 | +- Keep asking until you fully understand the requirements |
| 33 | + |
| 34 | +Frame the scope before writing it down: |
| 35 | +- Impact Mapping connects deliverables to business goals and actors — so you build what moves a goal, not just what was asked. |
| 36 | +- User Story Mapping lays stories along the user's journey and exposes a coherent first slice. |
| 37 | + |
| 38 | +Document the result as a PRD (problem, goals, personas, success criteria, scope). |
| 39 | + |
| 40 | +*Referenced anchors: socratic-method, mece, prd, impact-mapping, user-story-mapping* |
| 41 | + |
| 42 | +## Architecture Documentation |
| 43 | + |
| 44 | +_How we document architecture with diagrams, ADRs, and decision evaluation_ |
| 45 | + |
| 46 | +Architecture documentation follows arc42. Scaffold the arc42 "with-help" template into the project's `src/docs/` via docToolchain `downloadTemplate` rather than restating chapter structure here — each chapter's help text is its structural spec, which the process fills and then replaces. |
| 47 | + |
| 48 | +Every context, building-block and runtime chapter carries at least one diagram. Diagrams are PlantUML, not Mermaid; building blocks use C4 via PlantUML's bundled C4-PlantUML standard library — the `!include <C4/...>` stdlib form (angle brackets), never the remote `https://` URL and never vendored file copies. Not generic boxes. |
| 49 | + |
| 50 | +Decisions are ADRs (Nygard) with a 3-point Pugh Matrix (-1/0/+1). When the rationale is unconfirmed, ADR Status is "Accepted (inferred)" and Pugh cells needing team judgment are marked `?` rather than guessed. Each ADR's Consequences name the risks the decision creates, referencing the Chapter 11 risk IDs (R-NNN); a decision that creates a risk not yet in Chapter 11 either adds it there or records the consequence as explicitly accepted without a tracked risk. Conversely, Chapter 8 concepts back-reference the ADR that decided them. |
| 51 | + |
| 52 | +Cross-section traceability — arc42 templates do not enforce these, so the contract does: |
| 53 | +- Every Chapter 1.2 quality goal maps to a named approach in Chapter 4. |
| 54 | +- The external systems in Chapter 3 (context) and the Chapter 5 Level-1 building-block view are the same set — one system boundary in both. |
| 55 | +- Every Chapter 5 building block appears in at least one Chapter 6 runtime scenario; Chapter 6 includes at least one error/recovery scenario, not only the happy path. |
| 56 | +- Chapter 9 carries an in-document ADR index (ADR | Title | Status), even when the ADRs live in a separate register. |
| 57 | +- Each Chapter 5 building block states responsibility, interface, and source location. |
| 58 | + |
| 59 | +Chapter 1.2 lists only the top 3-5 quality goals — the ones that drive architecture decisions. Chapter 10 may elaborate further quality characteristics beyond those top goals; that is correct arc42, not a defect. The Chapter 10 quality tree marks each characteristic as either concretising a Chapter 1.2 top goal or as a derived quality requirement, and each Chapter 10 quality scenario cross-links back to the Chapter 1.2 goal it concretises (or is marked "derived"). Each Chapter 10 scenario is written in the six-part quality attribute scenario form (Source, Stimulus, Artifact, Environment, Response, Response Measure); the Response Measure carries a literal figure, so the requirement is testable rather than an adjective. |
| 60 | + |
| 61 | +Chapter 11 separates Risks from Technical Debt into two subsections. Each Risk carries probability, impact, a derived priority, and a mitigation/action cross-referencing an existing mitigation in Chapter 8 or a quality scenario where one exists; risks are ordered by priority. Each Technical Debt item references the specific Chapter 5 building block it burdens. |
| 62 | + |
| 63 | +*Referenced anchors: arc42, c4-diagrams, adr-according-to-nygard, pugh-matrix, quality-attribute-scenario* |
| 64 | + |
| 65 | +## Crosscutting Concepts |
| 66 | + |
| 67 | +_Baseline arc42 Chapter 8 concepts every system documents: threat model, security, test, observability, error handling_ |
| 68 | + |
| 69 | +arc42 leaves Chapter 8 open. We require five baseline crosscutting concepts, in this order: |
| 70 | + |
| 71 | +- 8.1 Threat Model — STRIDE; threats get IDs (T-001…). |
| 72 | +- 8.2 Security — every mitigation references the T-IDs it closes. |
| 73 | +- 8.3 Test — testing pyramid; tests trace to Use Cases and Business Rules. |
| 74 | +- 8.4 Observability — logs, metrics, traces, audit trails. |
| 75 | +- 8.5 Error Handling — retry, circuit breaker, fallback, recovery. |
| 76 | + |
| 77 | +Add further Chapter 8.x concepts (persistence, i18n, accessibility, configuration, performance) only when the system actually has that concern. |
| 78 | + |
| 79 | +*Referenced anchors: arc42, stride, testing-pyramid* |
| 80 | + |
| 81 | +## Layer Boundaries |
| 82 | + |
| 83 | +_How we handle boundaries between layers_ |
| 84 | + |
| 85 | +At every layer boundary: |
| 86 | +- Expose only well-defined DTOs and contracts — never domain entities |
| 87 | +- Use explicit mapping at every seam |
| 88 | +- Apply Anti-Corruption Layers when integrating external systems |
| 89 | +- Dependency direction points inward (DIP) |
| 90 | + |
| 91 | +*Referenced anchors: clean-architecture, hexagonal-architecture, domain-driven-design, solid-dip, solid-isp* |
| 92 | + |
| 93 | +## Backlog Management |
| 94 | + |
| 95 | +_How we create and prioritize implementation tasks_ |
| 96 | + |
| 97 | +Create EPICs and User Stories as GitHub issues from the specification. |
| 98 | +- User Stories follow INVEST criteria (Independent, Negotiable, Valuable, Estimable, Small, Testable) |
| 99 | +- Prioritize with MoSCoW (Must/Should/Could/Won't) |
| 100 | +- Mark dependencies between issues |
| 101 | +- Groom the backlog regularly as the project evolves |
| 102 | + |
| 103 | +*Referenced anchors: invest, moscow* |
| 104 | + |
| 105 | +## Vertical Slicing |
| 106 | + |
| 107 | +_How we build the first increment and grow the system in thin end-to-end slices_ |
| 108 | + |
| 109 | +Build the first increment as a walking skeleton: a deployable end-to-end slice that wires every architectural layer together and does almost nothing else. |
| 110 | + |
| 111 | +Grow the system as thin vertical slices — each slice cuts through all layers and delivers one small piece of user value. Slices are tracer bullets: kept and refined, never thrown away. |
| 112 | + |
| 113 | +When a technical unknown blocks a slice, run a spike solution first — a timeboxed, throwaway experiment that removes the risk. Spike code is discarded; only its lesson carries into the slice. |
| 114 | + |
| 115 | +*Referenced anchors: walking-skeleton, tracer-bullet, thin-vertical-slice, spike-solution* |
| 116 | + |
| 117 | +## Implement Next |
| 118 | + |
| 119 | +_The implementation loop for each issue_ |
| 120 | + |
| 121 | +For each issue: |
| 122 | +- Create a feature branch for the EPIC |
| 123 | +- Select next issue from backlog (respect dependencies) |
| 124 | +- Analyze and document analysis as a comment on the issue |
| 125 | +- Implement using TDD (London or Chicago School as appropriate) |
| 126 | +- Each test references its Use Case ID for traceability |
| 127 | +- Commit with Conventional Commits, reference issue number |
| 128 | +- Check if spec or architecture docs need updating |
| 129 | +- When EPIC is complete, create a Pull Request |
| 130 | + |
| 131 | +*Referenced anchors: tdd-london-school, tdd-chicago-school, definition-of-done, conventional-commits* |
| 132 | + |
| 133 | +## Refactoring |
| 134 | + |
| 135 | +_How we identify and execute refactorings safely_ |
| 136 | + |
| 137 | +Refactoring targets are named code smells, not a vague urge to "clean up". |
| 138 | + |
| 139 | +For any refactoring that does not complete in one step, use the Mikado Method: attempt the change, note what breaks, revert, and do the prerequisites first — never leave the build broken while you dig. |
| 140 | + |
| 141 | +Refactoring commits change structure only. Behaviour changes go in separate commits, and the test suite stays green at every commit. |
| 142 | + |
| 143 | +*Referenced anchors: code-smells, mikado-method* |
| 144 | + |
| 145 | +## Code Quality |
| 146 | + |
| 147 | +_Coding conventions and design principles_ |
| 148 | + |
| 149 | +Our code follows: |
| 150 | +- SOLID principles |
| 151 | +- DRY, KISS |
| 152 | +- Ubiquitous Language from Domain-Driven Design (same terms in code as in the specification) |
| 153 | + |
| 154 | +*Referenced anchors: solid-principles, dry, kiss-principle, domain-driven-design* |
| 155 | + |
| 156 | +## Quality Review |
| 157 | + |
| 158 | +_How we review code and check for security issues_ |
| 159 | + |
| 160 | +Quality assurance follows three layers: |
| 161 | +- Code review using Fagan Inspection (structured, systematic, with defined phases) |
| 162 | +- Security review based on OWASP Top 10 |
| 163 | +- Architecture review using ATAM (scenario-based tradeoff analysis against quality goals) |
| 164 | +- Use a different AI model or fresh session for reviews to avoid blind spots |
| 165 | + |
| 166 | +*Referenced anchors: fagan-inspection, owasp-top-10, atam* |
| 167 | + |
| 168 | +## Docs-as-Code |
| 169 | + |
| 170 | +_How we write and build documentation_ |
| 171 | + |
| 172 | +Documentation follows Docs-as-Code according to Ralf D. Müller: |
| 173 | +- AsciiDoc as format, PlantUML for inline diagrams, built by docToolchain |
| 174 | +- Version-controlled, peer-reviewed, and built automatically |
| 175 | +- Plain English according to Strunk & White (or Gutes Deutsch nach Wolf Schneider) |
| 176 | +- Projects following this contract include the `dtcw` wrapper and `docToolchainConfig.groovy` so PlantUML / AsciiDoc actually render. |
| 177 | + |
| 178 | +*Referenced anchors: docs-as-code, plain-english-strunk-white, gutes-deutsch-wolf-schneider* |
| 179 | + |
| 180 | +## Socratic Code Theory Recovery |
| 181 | + |
| 182 | +_Recover a program's 'theory' from source code through recursive question refinement_ |
| 183 | + |
| 184 | +Recover a program's "theory" (Naur 1985) from source code through recursive question refinement. |
| 185 | + |
| 186 | +- Start with 5 root questions: Q1 Problem/Users, Q2 Specification, Q3 Architecture, Q4 Quality Goals, Q5 Risks. |
| 187 | + |
| 188 | +- The second level of the tree is FIXED, not free. Every run emits exactly these nodes, in this order, even when a node's only leaf is [OPEN] or [ANSWERED: not applicable]: |
| 189 | + - Q1.1-Q1.6: product identity, primary users, channels, why-built, success metrics, segment priority |
| 190 | + - Q2.1-Q2.6: actors, use-case catalog, per-interface system specs, data/entity model, acceptance criteria, cross-cutting business rules |
| 191 | + - Q3.1-Q3.12: the twelve arc42 chapters, in arc42 order |
| 192 | + - Q4.1-Q4.8: the eight ISO/IEC 25010 characteristics; plus Q4.9: which characteristic has priority |
| 193 | + - Q5.1-Q5.5: technical debt, security risks, operational risks, dependency/supply-chain risks, scaling/performance risks |
| 194 | + |
| 195 | +- Below the fixed second level, decompose adaptively and code-driven; a node is a leaf only when it can be answered from one specific file:line evidence (a directory is too coarse — decompose further) or definitively marked [OPEN]. Depth tracks code density: a small bounded context yields a shallow tree, a large one a deep tree, capped at four levels below a fixed node. Depth varies between runs — expected. |
| 196 | + |
| 197 | +- Q-IDs are stable: Q3.7 is always Deployment View, in every run, so trees from different runs can be diffed node-by-node. |
| 198 | + |
| 199 | +- Each leaf is [ANSWERED] (with file:line evidence) or [OPEN] (with Category, Ask role, and why it is unanswerable from code). |
| 200 | + |
| 201 | +- Quality is not wholly team knowledge. Derive quality scenarios for the Q4 branch and arc42 Chapter 10 from measurable code behaviour — literal thresholds, timeouts, budgets, the threat catalogue and test concept from Q3.8 — as [ANSWERED] with file:line; never invent target numbers. Only the quality-goal ranking (Q4.9) is [OPEN]. arc42 Chapter 10 carries the derivable scenarios, never just an [OPEN] pointer. Chapter 1.2 names only the top 3-5 quality goals; Chapter 10 covers all eight characteristics — mark each Chapter 10 entry as concretising a Chapter 1.2 top goal or as derived. |
| 202 | + |
| 203 | +- Open Questions are the handoff document: always emit one section per role (Product Owner, Architect, Developer, Domain Expert, Operations), even when a section is empty ("No open questions for this role"). |
| 204 | + |
| 205 | +- Two-phase workflow: Phase 1 builds the tree; the team answers the Open Questions; Phase 2 synthesizes documentation from the answered tree. |
| 206 | + |
| 207 | +*Referenced anchors: socratic-method, arc42, iso-25010, adr-according-to-nygard, mental-model-according-to-naur* |
| 208 | + |
| 209 | +## Concise Response (TLDR) |
| 210 | + |
| 211 | +_How responses should be structured for brevity_ |
| 212 | + |
| 213 | +Responses lead with the conclusion first (BLUF). Keep to essential points. No filler, no preamble. Use short sentences, active voice, and no unnecessary words (Strunk & White). |
| 214 | + |
| 215 | +*Referenced anchors: bluf, plain-english-strunk-white* |
| 216 | + |
| 217 | +## Simple Explanation (ELI5) |
| 218 | + |
| 219 | +_How to explain complex concepts simply_ |
| 220 | + |
| 221 | +Explain complex concepts using simple language and everyday analogies. When the explanation feels hard to write, that reveals gaps in understanding — study those areas first (Feynman Technique). |
| 222 | + |
| 223 | +*Referenced anchors: feynman-technique* |
| 224 | + |
| 225 | +## Explaining and Teaching |
| 226 | + |
| 227 | +_How to teach a topic until understanding is verified, not just explained_ |
| 228 | + |
| 229 | +When asked to explain or teach something (including "why does X…"), don't just deliver the answer — teach until the learner can use it, and gate on evidence they can. |
| 230 | + |
| 231 | +Ground yourself first: if the topic is unfamiliar, fast-moving, or you're unsure, learn its current state before teaching — go to the source (the actual code, the spec, the docs), don't teach from stale memory. |
| 232 | + |
| 233 | +Frame: write a running checklist of what must be grasped — high level (why it matters, what it impacts) and low level (logic, edge cases, design decisions). That checklist is the Definition of Done for understanding. |
| 234 | + |
| 235 | +Diagnose before you teach (Socratic Method): start from the learner's current understanding, so you teach the gap, not the whole topic. Adjust depth on request (ELI5 / ELI-intern). |
| 236 | + |
| 237 | +Teach one item at a time in 4MAT order — Why → What → How → What-If: motivation before detail. Treat the Why as Naur's program theory — the reasoning, trade-offs, and branches not taken behind the design, not merely what it does; drill recursively into why. |
| 238 | + |
| 239 | +Verify — never "makes sense?". Check by active recall: have the learner explain it back in plain words (Feynman Technique — where they stall is the gap) or apply it to a new case (the What-If). "Understood" means Bloom's Apply/Analyze (uses it on a new case, traces the edge cases), not recall. |
| 240 | + |
| 241 | +Gate: don't advance to the next item until the current one is demonstrated, and don't end until the whole checklist is. |
| 242 | + |
| 243 | +Scale the ceremony to the question — a one-line factual question gets a one-line answer, not a loop. The learner can opt out any time ("just tell me") — comply. |
| 244 | + |
| 245 | +*Referenced anchors: 4mat, mental-model-according-to-naur, socratic-method, feynman-technique, blooms-taxonomy, definition-of-done* |
| 246 | + |
| 247 | +## Writing Style |
| 248 | + |
| 249 | +_How we write technical texts — language, tone, and structure_ |
| 250 | + |
| 251 | +Writing follows Gutes Deutsch nach Wolf Schneider (or Plain English according to Strunk & White). |
| 252 | + |
| 253 | +Additionally: |
| 254 | +- Technical terms stay in English (LLM, Prompt, Token, Spec, etc.) |
| 255 | +- Address the reader directly, use first person sparingly but deliberately |
| 256 | +- Use analogies to human thinking to explain technical concepts |
| 257 | +- One thought per paragraph (5-8 sentences is fine) |
| 258 | +- Section headings are statements, not topic announcements |
| 259 | +- First sentence says what the paragraph is about |
| 260 | +- Show code and prompts, don't just claim things work |
| 261 | +- Conclusions make a clear statement — never end with 'it remains exciting' |
| 262 | + |
| 263 | +*Referenced anchors: gutes-deutsch-wolf-schneider, plain-english-strunk-white* |
| 264 | + |
| 265 | +## TDD, Hamburg Style |
| 266 | + |
| 267 | +_Design-led TDD recipe by Ralf Westphal — close the requirements/logic gap before writing code, then test at service boundaries with minimal mocking_ |
| 268 | + |
| 269 | +Design-led TDD recipe by Ralf Westphal — close the requirements/logic gap before writing code, then test at service boundaries with minimal mocking. Use it when the problem is too complex for pure micro-step Red-Green-Refactor. |
| 270 | + |
| 271 | +- **ACD cycle (Analyze → Design → Code)** precedes the test loop: first model the solution to close the gap between requirements and logic, only then code. |
| 272 | +- **"Right from the start" philosophy** — implement correctly the first time so refactoring is a correction, not routine cleanup. |
| 273 | +- **Service-level testing** — test behind the public API, independent of API technology. |
| 274 | +- **Minimal mocking** — closer to *TDD, Chicago School* than *London School*. |
| 275 | +- **IOSP (Integration Operation Segregation Principle)** — a function is either composition (Integration) or logic (Operation), never both; structural support for simple unit tests. |
| 276 | +- **Deep Work over Small Steps** — accept that some problems can't be sliced into tiny green increments; stay red longer when the design demands it. |
| 277 | + |
| 278 | +Composes: *TDD, London School*, *TDD, Chicago School*, *Red-Green-Refactor*, *IOSP*. |
| 279 | +Sources: https://ralfw.de/hamburg-style-tdd/, https://ralfw.de/tdd-how-it-can-be-done-right/ |
| 280 | + |
| 281 | +*Referenced anchors: red-green-tdd, tdd-london-school, tdd-chicago-school, iosp* |
0 commit comments