Skip to content

Commit 9892136

Browse files
raifdmuellerclaude
andcommitted
feat: add socratic-code-theory-recovery Claude Code Skill (#473)
Package the brownfield Socratic Code-Theory Recovery workflow as an installable Claude Code Skill. Structure: - SKILL.md — when-to-use, contract overview, two-phase workflow diagram - prompts/phase-1-question-tree.md — copy-paste prompt for Phase 1, with post-prompt sanity-check and team-routing instructions - prompts/phase-2-synthesize.md — Phase 2 prompt that consumes the answered tree and produces PRD + Cockburn use cases + arc42 + Nygard ADRs with full Q-ID traceability - references/arc42.md — 12 sections as Q3 decomposition heuristic - references/cockburn-use-cases.md — fields as Q2 sub-questions, persona vs system use cases - references/iso-25010.md — 8 quality characteristics as Q4 sub-questions, with the mechanism-vs-target split - references/nygard-adrs.md — ADR format as Q3.9 sub-tree, what makes a decision architecturally significant, Pugh matrix guidance - references/output-schema.md — strict format for QUESTION_TREE.adoc and OPEN_QUESTIONS.adoc, including Q-ID scheme, [ANSWERED]/[OPEN] block formats, and Phase 2 traceability rules - references/examples.md — worked [ANSWERED] and [OPEN] leaves for each major branch (Q1-Q5) from a hypothetical Order Management context Reference snippets are embedded (per agreed scope) rather than linked, so the skill is usable offline once installed. End-to-end testing on a real codebase is deferred to a follow-up issue. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent cb9abbe commit 9892136

9 files changed

Lines changed: 815 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
name: socratic-code-theory-recovery
3+
description: Recover the "theory" (Naur 1985) of an existing codebase through recursive question refinement before writing documentation. Use on brownfield projects where the spec is missing — produces a Question Tree separating what is answerable from code (with evidence) from what must be asked of the team (routed by role). Phase 1 builds the tree; team answers the OPEN leaves; Phase 2 synthesizes PRD, Cockburn use cases, arc42 architecture, and Nygard ADRs from the answered tree.
4+
metadata:
5+
author: LLM-Coding
6+
version: "0.1"
7+
source: https://github.com/LLM-Coding/Semantic-Anchors
8+
license: MIT
9+
---
10+
11+
# Socratic Code-Theory Recovery
12+
13+
Reverse-engineer a bounded context into documentation without hallucinating the parts the code cannot tell you.
14+
15+
## When to use this skill
16+
17+
Use this skill on a brownfield codebase when:
18+
19+
- Documentation is missing, outdated, or untrusted.
20+
- A change is about to be made and you need a spec before you can change safely.
21+
- You want documentation that distinguishes code-derived facts from team-supplied context — auditable, not generated prose.
22+
- You want to surface the *open questions* in the system, not just synthesize an answer the team has not seen.
23+
24+
Do **not** use this skill when:
25+
26+
- You are doing greenfield development — use the spec-driven workflow instead.
27+
- The whole system needs to be documented at once — work one bounded context at a time.
28+
- The code is not runnable — fix that first.
29+
30+
## The contract
31+
32+
This skill implements the *Socratic Code-Theory Recovery* contract from the Semantic Anchors project. The methodology rests on Peter Naur's 1985 paper *Programming as Theory Building*: a program's theory lives in the heads of its developers and cannot be fully captured in code alone. A documentation-recovery process that ignores this produces confident-looking prose that fills in the gaps with invention.
33+
34+
The fix: model the gaps explicitly. Every question about the system is either `[ANSWERED]` from code (with file:line evidence) or `[OPEN]` (with a category and the role that must answer it). The OPEN leaves are the handoff to humans.
35+
36+
## Two-phase workflow
37+
38+
```
39+
┌────────────────────────────────┐
40+
Phase 1 │ CODE ──► Question Tree │
41+
│ ├─ [ANSWERED] leaves│
42+
│ └─ [OPEN] leaves │
43+
└────────────────┬───────────────┘
44+
45+
┌────────────────────────────────┐
46+
Between │ OPEN_QUESTIONS.adoc │
47+
│ ──► team (routed by role) │
48+
│ ──► answers fill in OPENs │
49+
└────────────────┬───────────────┘
50+
51+
┌────────────────────────────────┐
52+
Phase 2 │ Answered tree ──► Docs │
53+
│ PRD · Cockburn UCs · arc42 · │
54+
│ Nygard ADRs (every claim Q-ID) │
55+
└────────────────────────────────┘
56+
```
57+
58+
### Phase 1: Build the Question Tree
59+
60+
Use [prompts/phase-1-question-tree.md](prompts/phase-1-question-tree.md). Adapt the bounded-context path and any domain-specific Q1 examples; do not change the leaf classification, Q-ID scheme, or output files.
61+
62+
Outputs:
63+
64+
- `QUESTION_TREE.adoc` — the full hierarchical reasoning trace
65+
- `OPEN_QUESTIONS.adoc` — only the `[OPEN]` leaves, grouped by Ask role
66+
67+
Decomposition heuristics — use these Semantic Anchors as guides, not as rigid templates:
68+
69+
- **arc42** — 12 architecture sub-questions (Q3 branch). See [references/arc42.md](references/arc42.md).
70+
- **Cockburn Use Cases** — specification structure (Q2 branch). See [references/cockburn-use-cases.md](references/cockburn-use-cases.md).
71+
- **ISO/IEC 25010** — 8 quality characteristics (Q4 branch). See [references/iso-25010.md](references/iso-25010.md).
72+
- **Nygard ADRs** — design-rationale capture (Q3.9 branch). See [references/nygard-adrs.md](references/nygard-adrs.md).
73+
74+
Leaf classification rules and Q-ID scheme: [references/output-schema.md](references/output-schema.md).
75+
76+
Worked examples — one `[ANSWERED]` and one `[OPEN]` leaf for each major branch: [references/examples.md](references/examples.md).
77+
78+
### Between Phases: Team answers the OPEN leaves
79+
80+
Route `OPEN_QUESTIONS.adoc` to the people whose role appears in each section: Product Owner, Architect, Developer, Domain Expert, Operations. In one controlled experiment with a 13,000-line Go codebase, 11 targeted OPEN questions were enough to close the gap to the original documentation.
81+
82+
Team answers are written **directly into `OPEN_QUESTIONS.adoc`** under each question, marked clearly. Do not call Phase 2 until every OPEN leaf has either an answer or an explicit `(deferred)` marker.
83+
84+
### Phase 2: Synthesize documentation
85+
86+
Use [prompts/phase-2-synthesize.md](prompts/phase-2-synthesize.md). The Phase 2 LLM reads the answered tree and produces:
87+
88+
- **PRD** from the Q1 branch (problem, users, goals, success criteria)
89+
- **Specification** from the Q2 branch (Cockburn use cases at User Goal level, system use cases for each technical interface, supplementary specifications)
90+
- **arc42** with all 12 chapters from the Q3 branch
91+
- **Nygard ADRs** with Pugh Matrix from the Q3.9 branch
92+
93+
Every claim references a Q-ID. Team-supplied information is marked `(team answer)`. This dual traceability — code evidence plus team input — is the difference from a simple reverse-engineering prompt that fills in gaps silently.
94+
95+
## What the LLM can and cannot recover
96+
97+
A controlled experiment (deleting documentation from a greenfield project and regenerating it from code) showed:
98+
99+
**Derivable from code**: functional requirements, acceptance criteria, building-block views, glossary, security mechanisms, crosscutting concepts.
100+
101+
**NOT derivable from code**: business context, design rationale (the ADR "why"), quality-goal *priorities*, stakeholder concerns, aspirational features, performance budgets, tutorials, review results.
102+
103+
If your synthesized documentation contains a claim from the second list without a `(team answer)` marker, the LLM hallucinated it. Mark it `[OPEN]` and ask the team.
104+
105+
## Spec drift and reconciliation
106+
107+
After this skill produces documentation, the implementation LLM will add security hardening, validation rules, and edge cases that are not in the spec. This is structural, not a discipline problem. Re-run Phase 1 against the current code periodically — before a release, after a security review, before onboarding — and diff against the existing spec. The diff reveals NEW (in code, not in spec), CHANGED (diverged), and DEAD (in spec, not in code).
108+
109+
## Further reading
110+
111+
- Peter Naur, *Programming as Theory Building* (1985). https://pages.cs.wisc.edu/~remzi/Naur.pdf
112+
- Brownfield Workflow (Semantic Anchors). https://llm-coding.github.io/Semantic-Anchors/brownfield
113+
- Brownfield Experiment Report. https://llm-coding.github.io/Semantic-Anchors/brownfield-experiment-report
114+
- Fair Comparison Report (three recovery approaches). https://llm-coding.github.io/Semantic-Anchors/brownfield-fair-comparison
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Phase 1 Prompt: Build the Question Tree
2+
3+
Copy the block below into a session that has read access to the bounded context. Replace `[bounded context path]` with the actual path. Adapt the Q1-Q5 examples if your domain has different starting concerns, but do not change the leaf classification, Q-ID scheme, or output files.
4+
5+
```
6+
You are performing Socratic Code-Theory Recovery on a brownfield bounded
7+
context located at [bounded context path]. Phase 1 of two.
8+
9+
Goal: recover the program's theory (Naur, 1985) from source code through
10+
recursive question refinement, before any documentation is written.
11+
12+
Process:
13+
14+
1. Start with five high-level questions about the bounded context:
15+
Q1 What problem does this bounded context solve, and for whom?
16+
Q2 What is the specification of this bounded context?
17+
Q3 What is the architecture of this bounded context?
18+
Q4 What quality goals drive the design?
19+
Q5 What risks and technical debt exist?
20+
21+
2. Decompose each question recursively. Use these Semantic Anchors as
22+
decomposition guides:
23+
- arc42 — 12 sub-questions for architecture (Q3 branch)
24+
- Cockburn Use Cases — Primary Actor, Trigger, Main Success Scenario,
25+
Extensions, Postconditions for specification (Q2 branch)
26+
- ISO/IEC 25010 — 8 quality characteristics for quality goals (Q4 branch)
27+
- Nygard ADRs — Context, Decision, Status, Consequences for design
28+
rationale (Q3.9 branch)
29+
Stop decomposing when a question is precise enough to be answered with a
30+
single piece of code evidence or a single fact from a stakeholder.
31+
32+
3. Assign a hierarchical Q-ID to every node (Q1, Q1.2, Q1.2.3, ...) so that
33+
later documentation can cite back to it.
34+
35+
4. For each leaf, classify it:
36+
37+
[ANSWERED]
38+
- You found the answer in the code.
39+
- Cite the evidence as <file>:<line> or <file>::<function>.
40+
- Be exact. No "see X for details."
41+
42+
[OPEN]
43+
- The answer is not derivable from code alone.
44+
- Category: business-context | design-rationale | quality-goals |
45+
stakeholder-context | future-direction
46+
- Ask role: Product Owner | Architect | Developer | Domain Expert |
47+
Operations
48+
- State precisely what cannot be answered, and why.
49+
50+
5. Output two files in AsciiDoc:
51+
52+
QUESTION_TREE.adoc
53+
- Full hierarchical tree with all nodes and Q-IDs
54+
- Each leaf marked [ANSWERED] (with evidence) or [OPEN] (with Category
55+
and Ask role)
56+
- Includes all reasoning, not only the leaves
57+
58+
OPEN_QUESTIONS.adoc
59+
- Only the [OPEN] leaves, copied verbatim from QUESTION_TREE.adoc
60+
- Grouped by Ask role (one section per role)
61+
- Each question short enough to be answered in 1-3 sentences
62+
63+
Do not write any other documentation in this phase. Phase 2 will synthesize
64+
the answered tree into PRD, specification, arc42, and ADRs — only after the
65+
team has filled in the [OPEN] leaves.
66+
```
67+
68+
## What to do after the prompt completes
69+
70+
1. **Sanity-check `QUESTION_TREE.adoc`.** Pick three `[ANSWERED]` leaves at random and verify the cited file:line actually contains the claim. If any cite is wrong, the LLM is hallucinating evidence — re-run with a smaller bounded context.
71+
72+
2. **Route `OPEN_QUESTIONS.adoc` to the team.** One section per Ask role. Typically 10-15 questions for a small bounded context; if you see 50+, the bounded context is too large.
73+
74+
3. **Team writes answers directly into `OPEN_QUESTIONS.adoc`** under each question. Mark deferrals explicitly as `(deferred)` so Phase 2 can decide whether to leave them as gaps in the documentation.
75+
76+
4. Only after every leaf has an answer or an explicit deferral, run Phase 2.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Phase 2 Prompt: Synthesize Documentation
2+
3+
Run this prompt only after every `[OPEN]` leaf in `OPEN_QUESTIONS.adoc` has either a team answer or an explicit `(deferred)` marker.
4+
5+
```
6+
You are performing Phase 2 of Socratic Code-Theory Recovery.
7+
8+
Inputs:
9+
- QUESTION_TREE.adoc — the answered Question Tree from Phase 1.
10+
- OPEN_QUESTIONS.adoc — same OPEN leaves, now with team answers (or
11+
(deferred) markers) written under each question.
12+
13+
Goal: synthesize documentation from the answered tree. Every claim must be
14+
traceable to a Q-ID. Team-supplied facts must be marked (team answer).
15+
Anything still marked (deferred) must remain an explicit gap in the output,
16+
not be filled with invention.
17+
18+
Produce four artifacts:
19+
20+
1. docs/specs/prd-[context-name].adoc — Product Requirements Document
21+
- Problem statement, target users, goals, success criteria, scope
22+
boundaries, constraints, open questions
23+
- Source: Q1 branch of QUESTION_TREE.adoc
24+
- Anchor: PRD (Cagan / Pichler)
25+
26+
2. docs/specs/use-cases-[context-name].adoc — Specification
27+
- Persona Use Cases in Cockburn Fully Dressed format at User Goal level:
28+
Primary Actor, Trigger, Stakeholders & Interests, Preconditions,
29+
Main Success Scenario, Extensions, Postconditions, Business Rules.
30+
- System Use Cases for each technical interface (API endpoint, CLI
31+
command, event, file format): input + validation, processing,
32+
output + status codes, error responses.
33+
- Supplementary Specifications: Entity Model, State Machines, Interface
34+
Contracts, Validation Rules.
35+
- Gherkin acceptance criteria where applicable.
36+
- Source: Q2 branch of QUESTION_TREE.adoc
37+
- Anchor: Cockburn Use Cases
38+
39+
3. docs/arc42/arc42-[context-name].adoc — Architecture
40+
- All 12 arc42 chapters. Mark chapters with no content as
41+
"No information from Phase 1" rather than fabricating content.
42+
- Source: Q3 branch of QUESTION_TREE.adoc
43+
- Anchor: arc42 (Starke / Hruschka)
44+
45+
4. docs/specs/adrs/*.adoc — one ADR per significant design decision
46+
- Nygard format: Title, Status, Context, Decision, Consequences.
47+
- Include a Pugh Matrix listing the alternatives considered with a
48+
3-point scale (-1, 0, +1) against the quality goals from Q4.
49+
- Source: Q3.9 branch of QUESTION_TREE.adoc
50+
- Anchor: ADR according to Nygard
51+
52+
Rules for traceability:
53+
- Every paragraph references the Q-IDs that support it, in square brackets:
54+
"The system uses Hexagonal Architecture [Q3.5]."
55+
- Team-supplied facts get an inline marker: "Sessions expire after 24 hours
56+
(team answer, Q3.4.2)."
57+
- Deferred questions stay as explicit gaps: "Quality-goal priorities are
58+
deferred (Q4.1.deferred) and must be resolved before the next release."
59+
- Do not introduce facts that do not appear in QUESTION_TREE.adoc or
60+
OPEN_QUESTIONS.adoc. If a Section feels under-specified, leave it
61+
under-specified — that is signal, not a defect.
62+
```
63+
64+
## After Phase 2
65+
66+
- **Spec drift starts immediately.** Re-run Phase 1 against the current code before each release; diff the new Question Tree against the existing documentation to surface NEW (in code, not in spec), CHANGED (diverged), and DEAD (in spec, not in code) findings.
67+
68+
- **Extend bounded contexts incrementally.** Don't reverse-engineer the whole system in one pass. Pick the next bounded context only when the first one's documentation is being actively used.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# arc42 — Decomposition Guide for Q3 (Architecture)
2+
3+
arc42 is a 12-section template for documenting software architecture (Gernot Starke, Peter Hruschka). In this skill, the 12 sections serve as decomposition heuristics for the Q3 branch of the Question Tree — each section becomes a sub-question.
4+
5+
## The 12 sections as Q3 sub-questions
6+
7+
| Q-ID | Section | Sub-question(s) |
8+
|------|---------|-----------------|
9+
| Q3.1 | Introduction and Goals | What does the system do at the highest level? Which 3-5 quality goals drive design? Who are the most important stakeholders? |
10+
| Q3.2 | Architecture Constraints | Which technical, organizational, conventional constraints restrict design choices? |
11+
| Q3.3 | Context and Scope | What are the system's external interfaces — neighbours, channels, protocols? Business context vs technical context? |
12+
| Q3.4 | Solution Strategy | Which fundamental decisions and patterns shape the architecture? Technology choices, top-level decomposition, quality-goal approaches, organizational decisions? |
13+
| Q3.5 | Building Block View | How is the system decomposed into containers, components, classes? Static structure at multiple levels of zoom. |
14+
| Q3.6 | Runtime View | How do components interact for the most important scenarios — startup, user-visible flows, error handling? |
15+
| Q3.7 | Deployment View | Which hardware/infrastructure runs the system? Deployment topology, environments, mapping building blocks to infrastructure. |
16+
| Q3.8 | Crosscutting Concepts | Domain models, architecture/design patterns used, persistence, UI, communication, plausibility checks, exception/error handling, logging, security, internationalisation, configurability? |
17+
| Q3.9 | Architecture Decisions | Why was each significant decision made? Each becomes a Nygard ADR — see [nygard-adrs.md](nygard-adrs.md). |
18+
| Q3.10 | Quality Requirements | Quality tree, quality scenarios (when/where/who/measurement). Connects to Q4 (ISO 25010). |
19+
| Q3.11 | Risks and Technical Debt | Known technical risks, debt items, and their mitigation status. Overlaps with Q5. |
20+
| Q3.12 | Glossary | Domain terminology — terms the team uses with project-specific meaning. |
21+
22+
## Decomposition hints
23+
24+
- **Q3.1 Quality Goals** is *almost always* `[OPEN]` — priorities live in stakeholder heads, not code. Don't fake a ranking from package structure.
25+
- **Q3.4 Solution Strategy** and **Q3.9 Architecture Decisions** are the *why* of the system. Code shows *what* was decided; the *why* is `[OPEN]` unless ADRs or commit messages explain it.
26+
- **Q3.5 Building Block View** is the most code-derivable section. Walk packages/modules and trace dependencies.
27+
- **Q3.6 Runtime View** is partially derivable — entry points, request flows. Error scenarios are often `[OPEN]` because the team's *intent* differs from what happens to compile.
28+
- **Q3.11 Risks/Tech Debt** is `[OPEN]` unless TODO/FIXME comments are systematically maintained. Recent bug fixes and reverts often hint at debt the team already knows about.
29+
30+
## When to stop decomposing
31+
32+
A Q3 sub-question is fine-grained enough to be a leaf when:
33+
34+
- It can be answered with a single file:line reference, or
35+
- It cannot be answered at all from code (mark `[OPEN]` with category and role).
36+
37+
Avoid making sub-questions like "How does the system handle errors?" — too broad. Prefer "What happens when `OrderService.create()` is called with a duplicate idempotency key?" — answerable.
38+
39+
## Reference
40+
41+
- Project: https://arc42.org/
42+
- Anchor in the catalog: https://llm-coding.github.io/Semantic-Anchors/anchor/arc42

0 commit comments

Comments
 (0)