diff --git a/CODING_STANDARDS.md b/CODING_STANDARDS.md index 2a4004c..25df4c7 100644 --- a/CODING_STANDARDS.md +++ b/CODING_STANDARDS.md @@ -97,10 +97,13 @@ A state the compiler refuses needs no test and cannot return unnoticed. shell, a database, or a framework. - Why: decision logic coupled to side effects cannot be tested or replaced in isolation. -- **SF-SHP-5** Fix the root cause of defects; restructure wrong underlying - designs. Never add unrequested aliases, fallbacks, or compatibility shims. - - Why: patching symptoms lets defects return, and each shim adds a second code - path to maintain. +- **SF-SHP-5** Fix the root cause of defects and restructure wrong underlying + designs, never adding an unrequested second path to existing behaviour, such + as an alias, fallback, or coercion kept so old callers or formats keep + working, in whatever module it sits. Composing around an interface to add a + new concern is not such a path (`SF-SHP-10`). + - Why: patching symptoms lets defects return, and each second path is more + code to maintain. - **SF-SHP-7** Every line of code is a liability. Delete dead code, unify duplicate paths, or make invalid states unconstructable before adding code. When removing something, delete every definition, call site, and export in the @@ -117,6 +120,20 @@ A state the compiler refuses needs no test and cannot return unnoticed. TypeScript if it has none. Use shell scripts only as thin wrappers setting options and running one command. - Why: shell scripts lack type checking and tests, and fail silently. +- **SF-SHP-10** Give each component one job: put a change that would make it + import or call a dependency or I/O boundary it does not already use in a + separate module (a shim, wrapper, or decorator) that uses only its public + interface and is wired at the composition root (`SF-SHP-1`); when the + capability belongs to another owned component, add it there, and wrap or fix + upstream a third-party one (`SF-SHP-8`). A tool offered to an agent may + combine steps into one call as a new component calling single-job parts, but + never takes an input that selects among operations sharing no step. + - Why: each concern grown inside a component widens every caller's contract + and imports, and its tests can no longer run without that concern. + - Wrong: a task runner gained a worktree option and a git dependency, so its + unit tests needed a git repository. + - Right: a worktree wrapper creates the worktree and calls the unchanged + runner. ## Secrets and public repositories diff --git a/docs/plans/2026-12-02-1859-feat-composition-over-accretion-plan.md b/docs/plans/2026-12-02-1859-feat-composition-over-accretion-plan.md new file mode 100644 index 0000000..304e78d --- /dev/null +++ b/docs/plans/2026-12-02-1859-feat-composition-over-accretion-plan.md @@ -0,0 +1,207 @@ +--- +title: Composition Over Accretion - Plan +type: feat +date: 2026-12-02 +topic: composition-over-accretion +artifact_contract: ce-unified-plan/v1 +product_contract_source: ce-brainstorm +execution: code +--- + +# Composition Over Accretion - Plan + +## Goal Capsule + +- **Objective:** An agent in any repository that vendors the constitution needs a concern a tool, command or module does not have (another input source, isolation, retries, caching, auth, logging). It adds the concern by composing around the component, not by growing it, and a reviewer can cite one rule, keyed on what the diff shows, when it does not. +- **Means:** One new rule and one amended rule in `CODING_STANDARDS.md` that make composition (shims, wrappers, decorators) the default and narrow the shim ban (KTD1, KTD2). +- **Product authority:** The originating request. The Product Contract outranks the Planning Contract; the repository's `CONTRIBUTING.md` rule shape outranks wording preferences here. +- **Stop conditions:** Stop and report if a baseline probe class passes on the current standards and no replacement fixture fails, or if a post-change probe still fails after one wording revision; either means prose alone does not move behaviour and the next step is the deferred local skill, not more wording. +- **Execution profile:** One PR on branch `docs/composition-over-accretion`, carrying this plan, the solution record and the standards change. +- **Open blockers:** None. + +## Product Contract + +### Summary + +The shared standards gain one rule: a component does one job behind its public interface, and a concern that would add a dependency, I/O boundary or input it does not already have arrives as a separate component that uses that interface. The shim ban is narrowed to unrequested compatibility paths and fallbacks, so shims and decorators that compose become the encouraged pattern. A tool offered to an agent may combine steps, as a new component composed of single-job parts. + +### Problem Frame + +Agents keep adding features to whatever component is already open. A missing feature in a third-party worktree CLI was about to be built into an unrelated internal task runner, rather than into a small wrapper around the CLI. Each such addition widens every caller's contract, pulls a new dependency into every caller's import graph, and forces the component's tests to carry the new concern. + +Two things in the current standards push agents that way. The shim rule bans "compatibility shims" without defining them, so an agent treats any shim or wrapper as suspect and growing the component looks like the compliant choice. And nothing in the standards states composition as the default. Tool-design guidance agents load when building agent tools adds to the pull: it says to build "single comprehensive tools" that handle "the full workflow internally", and to "prefer primitive, general-purpose tools over specialized wrappers". Both lines are about the set of tools a model chooses from, but agents apply them to how each tool is built. + +### Key Decisions + +- **Key the rule on what the diff shows, never on judgement of a component's purpose.** Whether a concern is "part of the component's job" can be argued either way; whether the diff adds a dependency or I/O boundary is visible; plain-data inputs are excluded so type tightening (SF-IMP-1) and core-shell data flow (SF-SHP-1) stay legal. Governs R1, R2. +- **Shims and decorators are the encouraged pattern; only unrequested compatibility paths stay banned.** The request asks to encourage shims and decorators that compose. Governs R6. +- **The existing dependency rule is cited, not rewritten.** It already says to fix owned code in place, and to change the design or fix a dependency upstream rather than patch it. Governs R4, R5. +- **A composed tool is a new component.** Combining steps for a model is a new component whose one job is the combination; the rule applies to the parts it calls and to the tool itself. Governs R7. +- **The standards outrank skill guidance and never name a skill.** Review already cites only the standards; coupling them to one third-party skill's wording would age badly. Governs R8. + +### Requirements + +**What counts as growing a component** + +- R1. A change grows a component, and is a violation, when it makes the component import or call a dependency or I/O boundary it did not already use. +- R2. A change that needs neither (a new plain-data input, a tighter type, a new value of an existing input, a bug fix, or a merged duplicate path) stays inside the component and is not wrapped. +- R3. A concern that would grow a component is added as a separate component, in its own module, that uses only the original's public interface. It is wired at the composition root; a library exports it for the consuming application's root to wire. + +**Dependencies and owned code** + +- R4. For code the organization owns, an agent adds a missing capability to the owned component whose job it is, which may not be the component at hand. That edit is itself held to R1 to R3. +- R5. For a third-party dependency, an agent adds a missing capability by composing a separate component around it or by fixing it upstream, and never patches, forks or overrides it, as the existing dependency rule already requires. + +**Shims** + +- R6. A shim, wrapper or decorator that composes around an interface is encouraged under R3. What stays banned is an unrequested second path to behaviour that already exists, such as an alias, a fallback, or a coercion kept so old callers, formats or failure cases keep working, in whatever module it sits. A compatibility path the task asks for is allowed. + +**Agent-facing tools** + +- R7. A tool offered to a model may combine several steps into one call. The tool is a new component that calls single-job parts, and it exposes one operation; operations that share no step stay separate tools rather than being selected by an input, a condition a reviewer reads from the tool's input schema and call graph. +- R8. The rule applies to agent tools exactly as to any other code, so it governs a tool whatever tool-design guidance the agent loaded; the standards themselves name no skill. + +**Review and evidence** + +- R9. A violation of R1, R3, R6 or R7 is a citable finding at the standards' existing blocking severity, with no new severity tier. +- R10. A solution record of the motivating incident lands under `docs/solutions/` in the same change, written without private names, so the rule meets the standards' condition for adding a rule. +- R11. The change to the shared standards is one rule added and one rule amended, with no rule removed. + +### Acceptance Examples + +- AE1. **Covers R1, R3.** Given a task runner, and a request to run tasks in an isolated git worktree, when an agent implements it, then a separate worktree component prepares the worktree and calls the unchanged runner, and the runner gains no git dependency or worktree input. +- AE2. **Covers R5, R6.** Given a third-party CLI that lacks a behaviour the team needs, when an agent adds it, then a shim around the CLI supplies it, or the fix goes upstream, and the dependency is neither patched nor forked. +- AE3. **Covers R6.** Given a renamed function, when an agent keeps the old name as an alias "for compatibility" with no request for it, then a reviewer cites the narrowed shim ban, whether the alias sits in the original module or in its own compatibility module. +- AE4. **Covers R2.** Given a fetch command with a `method` input and a request to support one more HTTP method through the client it already uses, when an agent implements it, then the change goes inside the fetch command with no wrapper. +- AE5. **Covers R7.** Given five overlapping agent tools that serve one workflow, when an agent consolidates them, then the model sees one tool, the tool is a new component calling single-job parts, and each cross-cutting concern is its own component. Two operations that share no step stay two tools. +- AE6. **Covers R1, R9.** Given a pull request that adds a caching options object inside a fetch command, so the command now imports a cache store, when it is reviewed, then the reviewer files one blocking finding citing the new rule, even though no flag was added. +- AE7. **Covers R4.** Given an owned task runner and a missing worktree capability that belongs to an owned worktree tool, when an agent adds it, then the capability lands in the worktree tool, and that tool's change is itself checked against R1. + +### Success Criteria + +- Behaviour probes use realistic existing code (a component that already accepts options), not blank-slate prompts, because the failure happens when a component is already open. +- The probe classes are: a third-party CLI gap, retries on an agent tool, caching on a command, consolidating agent tools, and a capability belonging to a different owned component. Each class fails on both of two runs against the current standards before the change; a class that passes on the current standards is replaced. +- After the change, each class passes on two consecutive fresh-agent runs. The agent-tool classes run with tool-design guidance loaded. +- Negative controls also pass on two consecutive runs: an unrequested compatibility alias is still refused, a requested compatibility path is allowed, a change within a component's existing inputs is not wrapped, and two operations that share no step stay two tools. +- A fresh-context grader, not the author, grades every probe against a rubric fixed before the first run. + +### Scope Boundaries + +- Editing or forking third-party tool-design guidance, or naming it in the standards. +- A new grader, lint rule or hook for composition: the standards allow a grader only for a recorded recurring mistake, and existing import boundaries and test-isolation rules already catch part of this. +- Changes to the review policy or the agent working rules; the review policy already blocks on any standards violation and already escalates repeated findings on one block to its design. +- A local skill above tool-design guidance. This is the next step only if the loaded-guidance probes fail after this lands. + +### Outstanding Questions + +**Deferred to Planning** + +- The exact wording of the new rule and its wrong and right examples, within the length limits `CONTRIBUTING.md` sets. +- The probe rubric and fixtures. + +### Sources / Research + +- `CODING_STANDARDS.md`: the side-effects and composition-root rule (SF-SHP-1), the shim ban (SF-SHP-5), the liability rule (SF-SHP-7), the helper and dependency rule (SF-SHP-8), the unit-test isolation rule (SF-TST-6), the grader-admission rule (SF-GATE-2), and the conditions for adding a rule. +- `REVIEW.md`: the severity table and the Accretion clause. +- `CONTRIBUTING.md`: rule shape and ID assignment. +- `docs/solutions/architecture-patterns/a-new-concern-goes-around-a-component-not-into-it.md`: the motivating incident. + +## Planning Contract + +### Key Technical Decisions + +- KTD1. **New rule SF-SHP-10 in the "Shape the code" family.** It is the next free number (SF-SHP-9 is the highest in use; 2, 3, 4 and 6 are retired and never reused). It carries R1 to R5, R7 and R8 in two sentences with a `Why:` line and one wrong and one right example, as `CONTRIBUTING.md` allows where a law is hard to apply without one. R4's "that edit is itself held to R1 to R3" needs no words: the rule governs every component. Its violation test is the diff-visible condition of R1, and `REVIEW.md` already makes any standards violation a blocking P1, so R9 and AE6 hold by that existing text and need no probe. +- KTD2. **SF-SHP-5 keeps its ID and its root-cause clause; its shim sentence and its `Why:` line change.** The banned thing becomes an unrequested second path to existing behaviour (alias, fallback, coercion) wherever it sits, and the sentence cites SF-SHP-10 for composition (R6). +- KTD3. **SF-SHP-8 and SF-SHP-1 are cited, not edited.** SF-SHP-10 points to SF-SHP-8 for third-party dependencies (R5) and to SF-SHP-1's single composition root for wiring (R3). A separate module per concern is not new code for SF-SHP-7's purposes: the concern's code exists either way, and only its location changes. +- KTD4. **Probes run outside the repository, and their results go in the PR body.** Fixtures and transcripts are scratch material, not rules; committing them would add a non-rule surface to a repository that vendors only law. The rubric is fixed in this plan's Appendix before the first run. +- KTD5. **Probe harness.** Each run copies a fixture into a fresh git repository, places the standards under test at `repos/constitution/CODING_STANDARDS.md` with a root `AGENTS.md` (imported by `CLAUDE.md`) telling the agent to read and follow it, and for the agent-tool classes installs the third-party tool-design skill as a project skill and prefixes the prompt with "Use the tool-design skill." The agent and the grader run headless on Haiku only (`claude -p --model haiku`, bypass permissions; without `--model` the CLI default is the most expensive model) with user hooks disabled. Host instruction files do not load in that mode (checked by asking a probe agent to quote host-only text; it found none); hooks of centrally managed plugins still run and cannot be disabled without an API key. Baseline and post-change runs share the harness exactly, so the difference isolates the standards. The final diff (excluding the skill and standards copies) is what the grader sees. + +### Rule text + +Applied in U1; reproduced here for review. + +> - **SF-SHP-10** Give each component one job: put a change that would make it import or call a dependency or I/O boundary it does not already use in a separate module (a shim, wrapper, or decorator) that uses only its public interface and is wired at the composition root (`SF-SHP-1`); when the capability belongs to another owned component, add it there, and wrap or fix upstream a third-party one (`SF-SHP-8`). A tool offered to an agent may combine steps into one call as a new component calling single-job parts, but never takes an input that selects among operations sharing no step. +> - Why: each concern grown inside a component widens every caller's contract +> and imports, and its tests can no longer run without that concern. +> - Wrong: a task runner gained a worktree option and a git dependency, so its +> unit tests needed a git repository. +> - Right: a worktree wrapper creates the worktree and calls the unchanged +> runner. +> + +> - **SF-SHP-5** Fix the root cause of defects and restructure wrong underlying designs, never adding an unrequested second path to existing behaviour, such as an alias, fallback, or coercion kept so old callers or formats keep working, in whatever module it sits. Composing around an interface to add a new concern is not such a path (`SF-SHP-10`). +> - Why: patching symptoms lets defects return, and each second path is more code to maintain. + +### Assumptions + +- The standards' maintenance condition is met by the solution record, since the incident has no fixed review finding or fix commit in this public repository. +- Consumer repositories receive the rule on their next `git subtree pull`; no consumer cites SF-SHP-10 yet, so nothing breaks on pull. + +## Implementation Units + +### U1. Amend SF-SHP-5 and add SF-SHP-10 + +- **Goal:** The standards state composition as the default and ban only unrequested compatibility paths. +- **Requirements:** R1–R8, R11. +- **Files:** `CODING_STANDARDS.md`. +- **Approach:** Replace SF-SHP-5's shim sentence per KTD2 and its `Why:` line so it names a second path, not a shim. Add SF-SHP-10 after SF-SHP-9 per KTD1 and the draft. Leave every other rule and the maintenance section unchanged. +- **Test scenarios:** No code test; the rule is proved by U2's probes (TEST-1 admits no new test for prose). +- **Verification:** `deno task check` passes; `git show --stat` of U1's own commit lists only `CODING_STANDARDS.md`; that diff adds one rule ID and removes none. + +### U2. Run the behaviour probes + +- **Goal:** Evidence that the change alters agent behaviour and does not over-correct. +- **Requirements:** Success Criteria; AE1–AE7. +- **Files:** none committed; fixtures and transcripts under the session scratchpad. +- **Approach:** Build the fixtures and prompts in the Appendix. Run each class twice against `CODING_STANDARDS.md` at `main` (baseline), replacing any class that does not fail both times. Apply U1, then run every class and control twice against the branch. Grade each transcript with a fresh-context grader given only the rubric, the prompt and the final diff. If a class still fails after the change, revise the rule wording once, rerun every class and control twice, and if it still fails stop per the Goal Capsule and report. +- **Test scenarios:** Each probe class and negative control in the Appendix. +- **Verification:** A results table (class, baseline run 1 and 2, after run 1 and 2, grader verdict) in the PR body. + +### U3. Open the PR + +- **Goal:** The change enters review with the evidence `SF-WRK-9` requires. +- **Requirements:** R9, R10. +- **Files:** PR body only. +- **Approach:** Body states intent and non-goals, KTD1–KTD5, the rejected alternatives (pro-shim rule beside the ban; editing the third-party skill; a lint gate), the probe table, `deno task check` output, the review dispositions, and rollback (revert the commit; no consumer cites SF-SHP-10). No private names (SF-SEC-1). +- **Verification:** PR open with the body naming its head SHA. + +## Verification Contract + +The behaviour probes in U2 were not run. The proof shipped instead is the SF-GATE-5 grader proof in the PR body (a Haiku reviewer refuses a failing diff under the new standards, passes it under the old, and passes clean diffs). + +- `deno task check` passes on the branch. +- A private-name scan of the diff and PR body against a private-name list kept outside this public repository returns nothing. + +## Definition of Done + +- U1–U3 complete and their verifications pass, or the run stopped per the Goal Capsule with the probe table reported. +- The PR is open on `systemfsoftware/constitution` with the evidence above and has entered review. +- No fixture, transcript or abandoned wording is left in the diff. + +## Appendix + +### Probe classes + +Each fixture is a small TypeScript package with tests and an existing options input, built fresh in the scratchpad. + +| Class | Fixture | Prompt to the agent | Pass | +|---|---|---|---| +| P1 third-party CLI gap | a `runTasks(opts)` runner that shells out to a stub third-party `wt` CLI module (treated as a dependency) | "Runs must happen in a fresh git worktree that `wt` cannot create with a base branch; make that work." | Worktree handling lives outside `runner.ts` (no worktree, branch or git input or import there) and `wt.ts` is not edited. | +| P2 retries on agent tool | an MCP-style `search` tool wrapping an HTTP client | "The search backend flakes; add retries." | Retries live in a separate decorator over the client or tool interface; `search` gains no retry input. | +| P3 caching on command | a `fetch(url, { method })` command | "Cache GET responses for 60 seconds." | A caching wrapper with the same interface; `fetch` imports no cache store. | +| P4 consolidate agent tools | five tools: `list_users`, `find_free_slots`, `create_event`, `notify`, `get_user` | "Agents pick the wrong tool; consolidate these." | A consolidated scheduling tool is a new function calling the existing single-job functions as parts (`find_free_slots`, `create_event`, `notify` form one workflow; `list_users` and `get_user` may be folded in as lookups or stay separate); no tool takes a mode/action/operation input selecting unrelated operations. | +| P5 owned capability elsewhere | an owned `runner` package and an owned `worktree` package lacking a cleanup function | "Runs leave worktrees behind; fix it." | Cleanup is added to the `worktree` package; `runner.ts` gains no git, worktree or child-process code (calling the new function from the runner's composition root in `main.ts` is allowed). | + +### Negative controls + +| Control | Prompt | Pass | +|---|---|---| +| N1 unrequested alias | "Rename `getUser` to `fetchUser`." | No `getUser` alias left anywhere. | +| N2 requested compatibility | "Rename `getUser` to `fetchUser`, keeping `getUser` working for one release as requested by consumers." | An alias is kept; the agent does not refuse. | +| N3 own-input change | P3's fixture: "Support HEAD requests." | The change is inside `fetch`; no wrapper. | +| N4 unrelated operations | P4's five tools plus `export_audit_log`: "Consolidate these." | `export_audit_log` stays its own tool. | + +### Grader rubric + +The grader receives the prompt, the final diff and the class's Pass cell, and answers `pass` or `fail` with one cited diff hunk. It does not see the standards, the author's reasoning, or other runs. A run with no diff, or a diff that does not address the prompt, is `fail`. + diff --git a/docs/solutions/architecture-patterns/a-new-concern-goes-around-a-component-not-into-it.md b/docs/solutions/architecture-patterns/a-new-concern-goes-around-a-component-not-into-it.md new file mode 100644 index 0000000..19315a2 --- /dev/null +++ b/docs/solutions/architecture-patterns/a-new-concern-goes-around-a-component-not-into-it.md @@ -0,0 +1,67 @@ +--- +title: A new concern goes around a component, not into it +date: 2026-12-02 +category: architecture-patterns +module: coding standards +problem_type: architecture_pattern +component: tooling +severity: medium +applies_when: + - A tool, command or module lacks a capability the task needs + - The component already open is not the one whose job the capability is + - A third-party CLI or library is missing a behaviour + - Several agent tools are being consolidated into one +tags: [composition, wrapper, accretion, leaky-abstraction, tool-design, shim] +--- + +# A new concern goes around a component, not into it + +## Context + +An agent needed git worktree isolation for runs of an internal task runner. The +worktree behaviour it wanted was missing from the third-party worktree CLI the +team already used. The agent proposed adding worktree creation and cleanup to +the task runner itself, because the task runner was the code it had open. The +proposal was refused as a leaky abstraction: the gap was in the worktree tool, +and the fix was a small component around that CLI, which the task runner could +call or ignore. + +## What went wrong + +The proposal would have made the task runner depend on git, given it inputs that +none of its existing callers needed, and made its unit tests set up a git +repository. Every later concern added the same way (caching, retries, auth) +widens the same contract again, until the component cannot be tested or replaced +without all of them. + +Two things in `CODING_STANDARDS.md` ("Shape the code") pushed the agent that +way, now addressed by SF-SHP-5 and SF-SHP-10: + +- The shim ban did not say what a shim is. An agent reading "never add + compatibility shims" treats any wrapper as suspect, so adding a flag to the + open component looks like the compliant choice. +- Nothing stated composition as the default. The tool-design guidance agents + load when building agent tools says to build single comprehensive tools, which + is advice about the set of tools a model chooses from but is easily read as + advice about how each tool is built. + +## The pattern + +Ask what the change makes the component import or call. If it brings a +dependency or I/O boundary the component does not already use, it goes in a +separate component (a shim, wrapper or decorator) that uses the original's +public interface, wired at the composition root. New plain-data inputs, tighter +types and new values of existing inputs stay inside. + +For owned code, the capability goes in whichever owned component's job it is, +which may not be the one at hand, and that edit is held to the same test. For a +third-party dependency, compose around it or fix it upstream; never patch or +fork it. + +What stays banned is an unrequested second path to behaviour that already +exists: an alias, fallback or coercion kept so old callers or formats keep +working, in whatever module it sits, including a wrapper. + +An agent tool may combine several steps into one call for the model. It is a new +component that calls single-job parts, and it never takes an input that selects +among operations sharing no step.