From feb12b15863c9be3bf77a51d3abeb57d493901ef Mon Sep 17 00:00:00 2001 From: Lukasz Boryn Date: Fri, 19 Jun 2026 00:37:37 +0200 Subject: [PATCH] feat(skills): port codebase-design vocabulary skill from upstream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds engineering/codebase-design/ — shared vocabulary for designing deep modules (module, interface, seam, adapter, depth, leverage, locality). Three files: SKILL.md (glossary + principles + R+Py testability examples + rejected framings); DEEPENING.md (4 dependency categories, seam discipline, replace-don't-layer); DESIGN-IT-TWICE.md (parallel sub-agent pattern for exploring 3+ radically different interface designs). Retargets existing bdd siblings as language-flavored leaves: - bdd/SKILL.md gains a Vocabulary anchor pointing at codebase-design - interface-design-{r,py}.md and deep-modules-r.md get "vocab lives upstream" headers; "deep modules" mention dropped from the py interface-design description (codebase-design owns it now) - mocking-{r,py}.md swap "boundary" -> "seam" throughout — upstream rejects "boundary" as overloaded with DDD's bounded context - refactoring-r.md cross-links Shallow-modules to DEEPENING.md Cross-references added in sieve/SKILL.md (Minimal invokes depth), winnow/SKILL.md (deletion test formalised there), diagnose/SKILL.md (seam definition for Phase 5's "no correct seam" finding), domain-modeling/SKILL.md (sibling — vocabulary for solution structure vs problem domain). README gains a codebase-design bullet. Source: mattpocock/skills@main engineering/codebase-design/. R+Py testability examples replace upstream TS originals. --- README.md | 4 + skills/engineering/bdd/SKILL.md | 7 +- skills/engineering/bdd/deep-modules-r.md | 8 +- skills/engineering/bdd/interface-design-py.md | 8 +- skills/engineering/bdd/interface-design-r.md | 3 + skills/engineering/bdd/mocking-py.md | 8 +- skills/engineering/bdd/mocking-r.md | 12 +-- skills/engineering/bdd/refactoring-r.md | 7 +- .../engineering/codebase-design/DEEPENING.md | 61 +++++++++++ .../codebase-design/DESIGN-IT-TWICE.md | 66 ++++++++++++ skills/engineering/codebase-design/SKILL.md | 100 ++++++++++++++++++ skills/engineering/diagnose/SKILL.md | 1 + skills/engineering/domain-modeling/SKILL.md | 4 + skills/engineering/sieve/SKILL.md | 3 + skills/engineering/winnow/SKILL.md | 4 + 15 files changed, 275 insertions(+), 21 deletions(-) create mode 100644 skills/engineering/codebase-design/DEEPENING.md create mode 100644 skills/engineering/codebase-design/DESIGN-IT-TWICE.md create mode 100644 skills/engineering/codebase-design/SKILL.md diff --git a/README.md b/README.md index 8520ea0..7a94812 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ skill list. - **domain-modeling** — actively build / sharpen a project's domain model — challenge terms, update `CONTEXT.md` inline, offer ADRs only on the three-of-three rule. +- **codebase-design** — shared vocabulary for designing deep modules + (module, interface, seam, adapter, depth, leverage, locality); + `DEEPENING.md` for dependency-aware seam strategy, `DESIGN-IT-TWICE.md` + for parallel-sub-agent interface exploration. - **r-style** — Tidyverse + `box::use` + `dtplyr` standards with paired good/bad sibling docs (`box.md`, `tidyverse.md`, `dtplyr.md`). - **dev-python** — orchestrate the full Python workflow (implement → diff --git a/skills/engineering/bdd/SKILL.md b/skills/engineering/bdd/SKILL.md index c78d501..80a6d08 100644 --- a/skills/engineering/bdd/SKILL.md +++ b/skills/engineering/bdd/SKILL.md @@ -12,10 +12,15 @@ description: Drive implementation with a strict red-green-refactor loop where ev - Behavior is observable from outside the unit (input → output, state change, side effect) — pure refactors are out of scope. +## Vocabulary +Before step 1, anchor on [../codebase-design/SKILL.md](../codebase-design/SKILL.md) — +module, interface, seam, adapter, depth, leverage, locality. Language siblings +below restate the vocabulary in R / Python idiom. + ## Language references — Python Pytest only — `@pytest.fixture`, `parametrize`, scenario as docstring. - [tests-py.md](tests-py.md) — paired good/bad examples. -- [interface-design-py.md](interface-design-py.md) — DI, pure functions, typing, deep modules. *Before step 1.* +- [interface-design-py.md](interface-design-py.md) — DI, pure functions, typing. *Before step 1.* - [mocking-py.md](mocking-py.md) — boundaries only, façade + DI over patching. *Before `patch` / `monkeypatch`.* - [docstrings-py.md](docstrings-py.md) — Google style, types in annotations not prose. diff --git a/skills/engineering/bdd/deep-modules-r.md b/skills/engineering/bdd/deep-modules-r.md index 8f6bd08..cc39b3e 100644 --- a/skills/engineering/bdd/deep-modules-r.md +++ b/skills/engineering/bdd/deep-modules-r.md @@ -1,10 +1,8 @@ # Deep modules in R -Adapted from John Ousterhout, *A Philosophy of Software Design*. A **deep -module** has a small interface and a large implementation. A **shallow -module** has a large interface and almost no implementation — every helper -becomes part of the public contract, which makes the code hard to reason -about *and* hard to test. +Vocabulary and principles in [`../codebase-design/SKILL.md`](../codebase-design/SKILL.md). +This file shows what deep / shallow look like with `box::use` — the R lever +for controlling the interface. In R you control the interface with `box::use` (or NAMESPACE/`@export` in a package). The interface is whatever a caller writes after `box::use(...)`. diff --git a/skills/engineering/bdd/interface-design-py.md b/skills/engineering/bdd/interface-design-py.md index 667efd6..6625b09 100644 --- a/skills/engineering/bdd/interface-design-py.md +++ b/skills/engineering/bdd/interface-design-py.md @@ -1,8 +1,10 @@ # Interface design for testability (Python) -Rules to apply before writing the test, not after. A deep module (small -interface, large implementation — Ousterhout) is the goal; a testable -interface is the side effect. +Vocabulary lives in [`../codebase-design/SKILL.md`](../codebase-design/SKILL.md) — +module, interface, seam, adapter, depth. This file is the Python-flavored leaf. + +Rules to apply before writing the test, not after. A deep module is the goal; +a testable interface is the side effect. ## 1. Dependency injection — pass collaborators, don't construct them diff --git a/skills/engineering/bdd/interface-design-r.md b/skills/engineering/bdd/interface-design-r.md index d55ebc0..243c8e4 100644 --- a/skills/engineering/bdd/interface-design-r.md +++ b/skills/engineering/bdd/interface-design-r.md @@ -1,5 +1,8 @@ # Interface design for testability (R) +Vocabulary lives in [`../codebase-design/SKILL.md`](../codebase-design/SKILL.md) — +module, interface, seam, adapter. This file is the R-flavored leaf. + Three rules. Apply them before writing the test, not after. > Snippets below show only the function bodies; the `box::use(...)` diff --git a/skills/engineering/bdd/mocking-py.md b/skills/engineering/bdd/mocking-py.md index 37f2212..8ccf524 100644 --- a/skills/engineering/bdd/mocking-py.md +++ b/skills/engineering/bdd/mocking-py.md @@ -1,16 +1,16 @@ # Mocking in Python -**Mock at system boundaries. Never mock your own internals. Don't patch +**Mock at system seams. Never mock your own internals. Don't patch what you don't own — wrap it.** -## Boundaries you may mock +## Seams you may mock - HTTP / REST APIs — but through a façade you own (below), or replay fixtures with `respx` / `responses`. - Databases — prefer a real in-memory DB (`sqlite3 :memory:`) or a fake repository over patching cursors. - Time — inject `now: Callable[[], datetime]`, or `monkeypatch` a clock - boundary; never patch `datetime.datetime` globally. + seam; never patch `datetime.datetime` globally. - Randomness — pass an `rng: np.random.Generator` parameter; seed it in the test. - File system — `tmp_path`. Real temp files are cheaper than mocks. @@ -59,7 +59,7 @@ with patch("httpx.Client.get") as m: ## Dependency injection over patching ```python -# Easy to test — boundary passed in +# Easy to test — seam passed in def annotate(gene_ids: list[str], entrez: EntrezClient) -> list[Annotation]: ... # Hard to test — constructs its client internally; forces patching diff --git a/skills/engineering/bdd/mocking-r.md b/skills/engineering/bdd/mocking-r.md index 7daa7cd..0ddd879 100644 --- a/skills/engineering/bdd/mocking-r.md +++ b/skills/engineering/bdd/mocking-r.md @@ -1,12 +1,12 @@ # Mocking in R -**Mock at system boundaries. Never mock your own code.** +**Mock at system seams. Never mock your own code.** > Snippets below show only the function bodies; the `box::use(...)` > prologue is omitted for clarity. Real files start with one — see > [`../r-style/box.md`](../r-style/box.md). -## Boundaries you may mock +## Seams you may mock - HTTP / REST APIs — `httptest2`, `webmockr`, or `httr2::with_mocked_responses()`. - Databases — prefer a real test DB (`RSQLite::SQLite()` in-memory) over a @@ -27,7 +27,7 @@ If a test feels like it needs to stub an internal helper, that is a signal your function under test is doing two things. Split it. -## Design for mockability at the boundary +## Design for mockability at the seam ### Dependency injection over hardcoded constructors @@ -78,10 +78,10 @@ expectations bleeding into another. - `testthat::local_mocked_bindings(fn = function(...) ...)` — replaces a binding inside the package under test for the scope of one `test_that`. - Use only at the *boundary*: stub `httr2::req_perform`, not your own + Use only at the *seam*: stub `httr2::req_perform`, not your own `parse_response`. -- `mockery::stub(target, "boundary_fn", value)` — same idea, finer-grained. - Same rule: only stub at the boundary. +- `mockery::stub(target, "seam_fn", value)` — same idea, finer-grained. + Same rule: only stub at the seam. - `httptest2::with_mock_dir("fixtures/")` — replay recorded HTTP fixtures. Best option for integration-style tests against an HTTP service. diff --git a/skills/engineering/bdd/refactoring-r.md b/skills/engineering/bdd/refactoring-r.md index 62c2e03..adc41a4 100644 --- a/skills/engineering/bdd/refactoring-r.md +++ b/skills/engineering/bdd/refactoring-r.md @@ -44,8 +44,11 @@ parse_response <- function(raw) { ... } Two modules that always travel together, each exporting one thin function? Merge them. A module that exports eight helpers users always call in the -same order? Deepen — collapse them behind one entry point. (See -`deep-modules-r.md`.) +same order? Deepen — collapse them behind one entry point. See +[`deep-modules-r.md`](deep-modules-r.md) for the R shape, and +[`../codebase-design/DEEPENING.md`](../codebase-design/DEEPENING.md) for the +dependency-aware strategy (replace, don't layer — old tests on the shallow +modules become waste once the deepened interface has its own tests). ## Feature envy → move logic to where the data lives diff --git a/skills/engineering/codebase-design/DEEPENING.md b/skills/engineering/codebase-design/DEEPENING.md new file mode 100644 index 0000000..e70ccaf --- /dev/null +++ b/skills/engineering/codebase-design/DEEPENING.md @@ -0,0 +1,61 @@ +# Deepening + +How to deepen a cluster of shallow modules safely, given its dependencies. +Assumes the vocabulary in [SKILL.md](SKILL.md) — **module**, **interface**, +**seam**, **adapter**. + +## Dependency categories + +When assessing a candidate for deepening, classify its dependencies. The +category determines how the deepened module is tested across its seam. + +### 1. In-process + +Pure computation, in-memory state, no I/O. Always deepenable — merge the +modules and test through the new interface directly. No adapter needed. + +### 2. Local-substitutable + +Dependencies that have local test stand-ins (PGLite for Postgres, in-memory +filesystem, `RSQLite::SQLite()` for Postgres). Deepenable if the stand-in +exists. The deepened module is tested with the stand-in running in the test +suite. The seam is internal; no port at the module's external interface. + +### 3. Remote but owned (Ports & Adapters) + +Your own services across a network boundary (microservices, internal APIs). +Define a **port** (interface) at the seam. The deep module owns the logic; +the transport is injected as an **adapter**. Tests use an in-memory adapter. +Production uses an HTTP/gRPC/queue adapter. + +Recommendation shape: *"Define a port at the seam, implement an HTTP adapter +for production and an in-memory adapter for testing, so the logic sits in +one deep module even though it's deployed across a network."* + +### 4. True external (Mock) + +Third-party services (Stripe, NCBI, UniProt) you don't control. The deepened +module takes the external dependency as an injected port; tests provide a +mock adapter. + +## Seam discipline + +- **One adapter means a hypothetical seam. Two adapters means a real one.** + Don't introduce a port unless at least two adapters are justified + (typically production + test). A single-adapter seam is just indirection. +- **Internal seams vs external seams.** A deep module can have internal seams + (private to its implementation, used by its own tests) as well as the + external seam at its interface. Don't expose internal seams through the + interface just because tests use them. + +## Testing strategy: replace, don't layer + +- Old unit tests on shallow modules become waste once tests at the deepened + module's interface exist — delete them. +- Write new tests at the deepened module's interface. The **interface is the + test surface**. +- Tests assert on observable outcomes through the interface, not internal + state. +- Tests should survive internal refactors — they describe behaviour, not + implementation. If a test has to change when the implementation changes, + it's testing past the interface. diff --git a/skills/engineering/codebase-design/DESIGN-IT-TWICE.md b/skills/engineering/codebase-design/DESIGN-IT-TWICE.md new file mode 100644 index 0000000..ff0acfe --- /dev/null +++ b/skills/engineering/codebase-design/DESIGN-IT-TWICE.md @@ -0,0 +1,66 @@ +# Design It Twice + +When the user wants to explore alternative interfaces for a chosen deepening +candidate, use this parallel sub-agent pattern. Based on "Design It Twice" +(Ousterhout) — your first idea is unlikely to be the best. + +Uses the vocabulary in [SKILL.md](SKILL.md) — **module**, **interface**, +**seam**, **adapter**, **leverage**. + +## Process + +### 1. Frame the problem space + +Before spawning sub-agents, write a user-facing explanation of the problem +space for the chosen candidate: + +- The constraints any new interface would need to satisfy. +- The dependencies it would rely on, and which category they fall into + (see [DEEPENING.md](DEEPENING.md)). +- A rough illustrative code sketch to ground the constraints — not a + proposal, just a way to make the constraints concrete. + +Show this to the user, then immediately proceed to Step 2. The user reads and +thinks while the sub-agents work in parallel. + +### 2. Spawn sub-agents + +Spawn 3+ sub-agents in parallel using the Agent tool — **one message with +multiple Agent tool uses** so they run concurrently. Each must produce a +**radically different** interface for the deepened module. Use +`subagent_type: claude` (general-purpose) so each has full read + search. + +Prompt each sub-agent with a separate technical brief (file paths, coupling +details, dependency category from [DEEPENING.md](DEEPENING.md), what sits +behind the seam). The brief is independent of the user-facing problem-space +explanation in Step 1. Give each agent a different design constraint: + +- Agent 1: "Minimize the interface — aim for 1–3 entry points max. Maximise + leverage per entry point." +- Agent 2: "Maximise flexibility — support many use cases and extension." +- Agent 3: "Optimise for the most common caller — make the default case + trivial." +- Agent 4 (if applicable): "Design around ports & adapters for cross-seam + dependencies." + +Include both [SKILL.md](SKILL.md) vocabulary and `CONTEXT.md` vocabulary in +the brief so each sub-agent names things consistently with the architecture +language and the project's domain language. + +Each sub-agent outputs: + +1. Interface (types, methods, params — plus invariants, ordering, error modes). +2. Usage example showing how callers use it. +3. What the implementation hides behind the seam. +4. Dependency strategy and adapters (see [DEEPENING.md](DEEPENING.md)). +5. Trade-offs — where leverage is high, where it's thin. + +### 3. Present and compare + +Present designs sequentially so the user can absorb each one, then compare +them in prose. Contrast by **depth** (leverage at the interface), +**locality** (where change concentrates), and **seam placement**. + +After comparing, give your own recommendation: which design you think is +strongest and why. If elements from different designs would combine well, +propose a hybrid. Be opinionated — the user wants a strong read, not a menu. diff --git a/skills/engineering/codebase-design/SKILL.md b/skills/engineering/codebase-design/SKILL.md new file mode 100644 index 0000000..4ef8e82 --- /dev/null +++ b/skills/engineering/codebase-design/SKILL.md @@ -0,0 +1,100 @@ +--- +name: codebase-design +description: Shared vocabulary for designing deep modules. Use when the user wants to design or improve a module's interface, find deepening opportunities, decide where a seam goes, make code more testable or AI-navigable, or when another skill needs the deep-module vocabulary. +--- + +# codebase-design + +Design **deep modules**: a lot of behaviour behind a small interface, placed +at a clean seam, testable through that interface. The aim is leverage for +callers, locality for maintainers, and testability for everyone. + +## Glossary + +Use these exactly; don't substitute "component", "service", "API", "boundary". + +**Module** — anything with an interface and an implementation. Scale-agnostic: +function, class, package, tier-spanning slice. _Avoid_: unit, component, service. + +**Interface** — everything a caller must know to use the module correctly: +type signature, invariants, ordering, error modes, configuration, performance. +_Avoid_: API, signature (type-level only). + +**Implementation** — what's inside a module. Distinct from **Adapter**: a small +adapter can wrap a large implementation (a Postgres repo), or vice versa. + +**Depth** — leverage at the interface: behaviour a caller exercises per unit +of interface learned. **Deep** = much behaviour behind a small interface; +**shallow** = interface nearly as complex as impl. + +**Seam** _(Michael Feathers)_ — a place where you can alter behaviour without +editing in that place; the *location* at which a module's interface lives. +_Avoid_: boundary (overloaded with DDD's bounded context). + +**Adapter** — a concrete thing that satisfies an interface at a seam. *Role*, +not substance. + +**Leverage** — what callers get from depth: more capability per unit of +interface learned. One impl pays back across N call sites, M tests. + +**Locality** — what maintainers get from depth: change, bugs, knowledge, +verification concentrate in one place. Fix once, fixed everywhere. + +## Deep vs shallow + +A **deep** module = small interface (few methods, simple params) over large +implementation (complex logic hidden). **Shallow** = the inverse, mostly +pass-through. Avoid shallow: reduce methods, simplify params, hide more inside. + +## Principles + +- **Depth is a property of the interface, not the implementation.** A deep + module can be composed of small mockable parts internally; **internal + seams** (private, used by its own tests) coexist with the **external seam**. +- **The deletion test.** Imagine deleting the module. If complexity vanishes, + it was a pass-through. If complexity reappears across N callers, it was + earning its keep. +- **The interface is the test surface.** Callers and tests cross the same + seam. To test *past* the interface is to find the module is the wrong shape. +- **One adapter = hypothetical seam; two = real one.** Don't introduce a seam + unless something actually varies across it. + +## Designing for testability + +**Accept dependencies, don't create them.** + +```r +# R: pass the client; default keeps callers ergonomic +fetch_orders <- function(order_id, client = modb_client()) client$get(order_id) +``` + +```python +# Python: optional parameter, same shape +def fetch_orders(order_id: str, client: ModbClient | None = None) -> Order: + return (client or ModbClient()).get(order_id) +``` + +Constructing the client inside buries the dependency past the interface. + +**Return results, don't produce side effects.** + +```r +# R: return the new value +calculate_discount <- function(cart) cart$total - discount(cart) +``` + +```python +# Python: return the new value +def calculate_discount(cart: Cart) -> Decimal: + return cart.total - discount(cart) +``` + +Mutating in place hides the effect and couples every test to caller state. + +## Rejected framings + +- **Depth as impl-lines ÷ interface-lines** (Ousterhout): rewards padding. Use depth-as-leverage instead. +- **"Interface" = the TypeScript `interface` keyword or a class's public methods**: too narrow — interface here is every fact a caller must know. +- **"Boundary"**: overloaded with DDD's bounded context. Say **seam** or **interface**. + +[DEEPENING.md](DEEPENING.md) — dependency categories, seam discipline, replace-don't-layer testing. [DESIGN-IT-TWICE.md](DESIGN-IT-TWICE.md) — parallel sub-agents to design the interface several radically different ways; compare on depth, locality, seam placement. diff --git a/skills/engineering/diagnose/SKILL.md b/skills/engineering/diagnose/SKILL.md index 601c01c..b14ab0c 100644 --- a/skills/engineering/diagnose/SKILL.md +++ b/skills/engineering/diagnose/SKILL.md @@ -95,5 +95,6 @@ Then ask: **what would have prevented this bug?** - [../bdd/SKILL.md](../bdd/SKILL.md) — phase 5 is a bdd cycle. - [../bdd/refactoring-r.md](../bdd/refactoring-r.md) — file architectural follow-ups here. +- [../codebase-design/SKILL.md](../codebase-design/SKILL.md) — definition of **seam** (Phase 5's "no correct seam" finding). - [../domain-modeling/SKILL.md](../domain-modeling/SKILL.md) — phase 3 hypothesis names should use canonical terms from `CONTEXT.md`. diff --git a/skills/engineering/domain-modeling/SKILL.md b/skills/engineering/domain-modeling/SKILL.md index 5ebdae6..61a28ba 100644 --- a/skills/engineering/domain-modeling/SKILL.md +++ b/skills/engineering/domain-modeling/SKILL.md @@ -82,6 +82,10 @@ per-context `CONTEXT.md` under each sub-root. confirming the first such write. ## See also +- [../codebase-design/SKILL.md](../codebase-design/SKILL.md) — sibling + vocabulary skill. **domain-modeling** pins terms for the *problem domain* + (CONTEXT.md, glossary); **codebase-design** pins terms for the *solution + structure* (module, interface, seam, adapter). - [../grill-with-docs/SKILL.md](../grill-with-docs/SKILL.md) — the user-invoked router that pairs this skill with a Socratic interview. - [../bdd/SKILL.md](../bdd/SKILL.md) — once vocabulary is pinned, the diff --git a/skills/engineering/sieve/SKILL.md b/skills/engineering/sieve/SKILL.md index d6917c6..febdecb 100644 --- a/skills/engineering/sieve/SKILL.md +++ b/skills/engineering/sieve/SKILL.md @@ -79,6 +79,9 @@ describe`), not per keystroke — otherwise it fights iteration. ## See also - [`../winnow/SKILL.md`](../winnow/SKILL.md) — the mechanical after-the-fact audit. The sieve judges entry; winnow clears the field. +- [`../codebase-design/SKILL.md`](../codebase-design/SKILL.md) — *Minimal* + invokes **depth** intuition. The leanest form that works tends to be the + deepest: small interface, large implementation hidden behind it. - [`../../productivity/jj-atomic/SKILL.md`](../../productivity/jj-atomic/SKILL.md) — the seal where the sieve fires. - [`../grill-with-docs/SKILL.md`](../grill-with-docs/SKILL.md) diff --git a/skills/engineering/winnow/SKILL.md b/skills/engineering/winnow/SKILL.md index b2418d8..08da188 100644 --- a/skills/engineering/winnow/SKILL.md +++ b/skills/engineering/winnow/SKILL.md @@ -76,6 +76,10 @@ it sharp. ## See also - [`../sieve/SKILL.md`](../sieve/SKILL.md) — the judgment gate. Sieve decides what earns its place; winnow mechanically clears what doesn't. +- [`../codebase-design/SKILL.md`](../codebase-design/SKILL.md) — the + **deletion test** formalised: if removing a module makes complexity + vanish, it was a pass-through; if it reappears across N callers, it earned + its keep. The `delete` and `yagni` tags are this test mechanised. - [`../bdd/refactoring-r.md`](../bdd/refactoring-r.md) — file the architectural follow-ups a winnow surfaces; don't auto-cut them mid-pass. - [`../review-angles/SKILL.md`](../review-angles/SKILL.md) — the correctness