|
| 1 | +--- |
| 2 | +name: design-iteration |
| 3 | +description: "Design-iteration passes that an agent runs over its own code before it reports a structural change as done. Use after adding or splitting a component, changing a contract or public API, rewriting a module, or moving responsibility between modules, and before listing a design smell as an accepted cost. Covers the pass loop, the question bank, and when to apply versus propose." |
| 4 | +--- |
| 5 | + |
| 6 | +# Design iteration |
| 7 | + |
| 8 | +A design is not done when it works and its tests pass. It is done when a pass |
| 9 | +over it finds nothing simpler. |
| 10 | + |
| 11 | +The failure this skill fixes: an agent ships a working design, then the user |
| 12 | +asks "why does X need Y?" and the agent answers with the better design in one |
| 13 | +turn. The agent had the knowledge. It did not ask the question. In one session |
| 14 | +of this package's v4 work, the user started nearly all of several dozen |
| 15 | +design rounds. Most rounds began with one of these questions: |
| 16 | + |
| 17 | +- "Do we still need X?" |
| 18 | +- "Why does Y care about Z?" |
| 19 | +- "Is X derived from Y?" |
| 20 | +- "Can't A just call B directly?" |
| 21 | +- "Should this exist at all?" |
| 22 | + |
| 23 | +Ask them yourself, before the user has to. |
| 24 | + |
| 25 | +## When to run a pass |
| 26 | + |
| 27 | +Run a pass before you report done, after any of these: |
| 28 | + |
| 29 | +- You add a component, or split or merge components. |
| 30 | +- You change a contract, a public API, or a type that other modules read. |
| 31 | +- You rewrite a function or module. |
| 32 | +- You move responsibility from one module to another. |
| 33 | +- A pass applied a change. Run another pass, because one change often makes |
| 34 | + another piece obsolete. |
| 35 | + |
| 36 | +Also run a pass when you are about to write "accepted cost", "acceptable |
| 37 | +because it is private", "mostly theoretical", or "behavior change" about your |
| 38 | +own design. |
| 39 | + |
| 40 | +## The loop |
| 41 | + |
| 42 | +1. Run the question bank below over every field, parameter, option, type, |
| 43 | + helper, and module boundary the change adds or touches. |
| 44 | +2. Apply each fix that stays inside the task's scope. |
| 45 | +3. Propose each fix that crosses scope, and let the user decide whether to |
| 46 | + widen it. Give the fix. Do not only name the smell. |
| 47 | +4. Repeat from step 1 until a pass finds nothing, or after three passes. |
| 48 | +5. Run `/simplify` on the result. Its Structure angle reviews the touched |
| 49 | + modules as a whole, and its report lists what a reviewer must see. |
| 50 | + |
| 51 | +## Rules |
| 52 | + |
| 53 | +- **If you could answer it when asked, it is yours to fix now.** When you can |
| 54 | + name the better shape, propose or apply it. Do not wait for the question. |
| 55 | +- **A smell you list must come with a fix.** "This is worse than before", |
| 56 | + "no requirement forces this order", and "the names still differ" are the |
| 57 | + start of a change, not its report. (Real case: the agent listed four |
| 58 | + regressions against an earlier version, including an `any[]` result and |
| 59 | + mutable state that user code could reach. The user then had to request each |
| 60 | + fix one at a time.) |
| 61 | +- **An API you own is not fixed.** Change the lower layer when that is the |
| 62 | + simpler design. Do not add a side channel in the consumer to avoid it. (Real |
| 63 | + case: the agent saved and restored a mutable variable around every call to |
| 64 | + its own `TaskSharer`, because adding a context argument "would change the |
| 65 | + sharer's API". The context argument deleted the save-and-restore in two |
| 66 | + places.) |
| 67 | +- **Ask whether it should exist before you rename it.** A rename keeps the |
| 68 | + thing. (Real case: the agent renamed `Work.accumulated` to `autoBatch`. A |
| 69 | + later pass deleted the whole `Work` type, because the batcher already |
| 70 | + computed the only value that depended on it.) |
| 71 | +- **Extraction does not remove state.** A helper extraction lowers nesting but |
| 72 | + keeps every variable. Check that your simplification deleted something. |
| 73 | +- **Do not keep an order because the old code had it.** When the old shape |
| 74 | + forced a guard, a deferred, or a failure path, ask what requirement forced |
| 75 | + that shape. Remove the shape when no requirement does. |
| 76 | +- **Read the prior art before you design its replacement.** When you replace a |
| 77 | + well-known library, read its source to learn its semantics, and run its own |
| 78 | + tests against your replacement. Write down each difference. |
| 79 | + |
| 80 | +## Question bank |
| 81 | + |
| 82 | +### Existence |
| 83 | + |
| 84 | +- Does each field, parameter, option, and type have a reader? Delete it when |
| 85 | + nothing reads it, along with the type parameters it carries. |
| 86 | +- Can a value be computed from other state, or from another value you also |
| 87 | + return? Derive it on demand, or state why it is stored. |
| 88 | +- Did this change make an earlier workaround unnecessary? Look for guards, |
| 89 | + boxes, deferrals, and dedup steps that existed only for the removed problem. |
| 90 | +- Is a helper or named type used in one place only? Inline it when it hides |
| 91 | + the flow or the contract. |
| 92 | +- Does a public export have a real consumer? If it cannot honor a guarantee |
| 93 | + that every other entry point honors, remove it instead of documenting the |
| 94 | + gap or adding a hook. |
| 95 | + |
| 96 | +### Responsibility |
| 97 | + |
| 98 | +- Does this module import or name a type from a module that sits above it? |
| 99 | + The composer imports the parts. A part never imports its composer. |
| 100 | +- Does a flag or parameter repeat a decision that the caller already expresses |
| 101 | + through another input? (Real case: a `background` flag only dropped the |
| 102 | + signal, and the caller already passed no signal.) |
| 103 | +- Does a generic component encode one caller's modes, such as an |
| 104 | + `"automatic" | "direct"` parameter? Move that policy to the caller. |
| 105 | +- Does a concern live on an object with a different lifetime? A lease that |
| 106 | + must last for a task's whole life belongs on the task, not on the queue. |
| 107 | +- Does a method mostly touch another object's fields? Move it to that object. |
| 108 | +- Does every call to X sit next to a call to Y? Fold Y into X. |
| 109 | + |
| 110 | +### Composition |
| 111 | + |
| 112 | +- Make a table of callers against features. If the table is sparse, split the |
| 113 | + component into blocks, and let each caller compose the blocks it needs. |
| 114 | + (Real case: one scheduler did batching and sharing. Store reads used only |
| 115 | + batching, single-request producers used only sharing, and only bulk |
| 116 | + producers used both.) |
| 117 | +- Does any caller pay for a part that it switches off? It should not use that |
| 118 | + part. |
| 119 | +- After a split, re-justify each option that came from the old component. Some |
| 120 | + options exist only because one object did two jobs. |
| 121 | +- Is a special case a general mechanism in disguise? A task "without |
| 122 | + cancellation" is a task with no signal. |
| 123 | + |
| 124 | +### API shape |
| 125 | + |
| 126 | +- Does any caller work around the API? Signs are a pre-filter, `[0]!`, |
| 127 | + inferring a value the callee knows, or two settings to express one intent. |
| 128 | + A workaround is evidence of a callee defect. |
| 129 | +- Does a callback receive information that its caller already has? Return |
| 130 | + data and let the caller act after the call. |
| 131 | +- Is each fact at the level where it varies? A per-call fact belongs on the |
| 132 | + call, not repeated on each item. |
| 133 | +- In a batch API, does one bad item affect the other items? It must not. |
| 134 | +- Is each fixed collaborator a constructor option, and each per-call choice an |
| 135 | + argument? |
| 136 | +- Does a flag keep the promise that its name makes? Fix the flag. Do not add a |
| 137 | + second option beside it. |
| 138 | + |
| 139 | +### Invariants |
| 140 | + |
| 141 | +- Does correctness depend on call order that only a comment states? Make one |
| 142 | + operation do both steps, or use a hold, lock, or refcount that makes the |
| 143 | + wrong order impossible. |
| 144 | +- Did you make a field optional and then add an "impossible" throw? Model the |
| 145 | + new case as a union where the paths meet, and leave the old type unchanged. |
| 146 | +- Can the API express an illegal combination of options? Make it untypeable. |
| 147 | +- Does a doc comment describe a known gap? Look for the primitive that closes |
| 148 | + it before you accept it. |
| 149 | +- Do you represent absence with a sentinel? Use `undefined` and an optional |
| 150 | + field. Do not export an internal constant so that another module can compare |
| 151 | + against it. |
| 152 | +- Did you write `T | undefined` at the use site of a type parameter? Write |
| 153 | + `T`, and let a caller that needs `undefined` put it in the type argument. For |
| 154 | + an omittable trailing argument, default the type parameter to `void`: |
| 155 | + TypeScript lets a caller omit a trailing argument only when its type accepts |
| 156 | + `void`. |
| 157 | + |
| 158 | +### Names |
| 159 | + |
| 160 | +- Does each concept have one name across every layer, including the wrapper |
| 161 | + around a component? Trace a value from its entry point to its last use. |
| 162 | +- Does each name refer to exactly one concept? |
| 163 | +- Does a name describe what the thing is, or only its mechanism or first use? |
| 164 | +- Does a function's name match what it takes and every effect it has? A name |
| 165 | + like `flush` must cover a permanent mode change, if the method makes one. |
| 166 | + |
| 167 | +### Baseline |
| 168 | + |
| 169 | +- Diff the rewrite against the base branch. For each lost name, log, comment, |
| 170 | + helper, `async`, or assertion, confirm that the change needs it gone. |
| 171 | +- Did a refactor onto a new abstraction drop an output that callers had, such |
| 172 | + as a log field or a metric? Extend the abstraction to expose the fact. Do not |
| 173 | + report the loss as a behavior change. |
| 174 | + |
| 175 | +### Verifiability |
| 176 | + |
| 177 | +- Before you propose a design, write the call trace for two or three |
| 178 | + interleaved calls, including the edge modes. Two design errors in the cache |
| 179 | + work appeared only when the user asked for pseudo-code. |
| 180 | +- Are `map`, `filter`, and `flatMap` callbacks free of side effects? Decide |
| 181 | + first, then act. |
| 182 | +- Is each acquire paired with its release on every path, close enough that a |
| 183 | + reader can check it line by line? |
| 184 | + |
| 185 | +## Apply versus propose |
| 186 | + |
| 187 | +Apply a fix in the same task when it stays inside the files and contracts the |
| 188 | +task already changes. Propose it with a concrete fix when it edits a module |
| 189 | +the task never named, turns a test-only change into a production change, or |
| 190 | +changes a public contract the user has not asked to change. In both |
| 191 | +cases, say what the pass found. |
0 commit comments