@skilldeck/craft 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +17 -0
- package/LICENSE +21 -0
- package/README.md +63 -2
- package/bin/craft.mjs +24 -0
- package/index.mjs +20 -0
- package/package.json +43 -4
- package/skills/deepen-modules/SKILL.md +99 -0
- package/skills/deepen-modules/references/deep-modules.md +81 -0
- package/skills/deepen-modules/scripts/arch-scan.mjs +784 -0
- package/skills/grill/SKILL.md +109 -0
- package/skills/grill/references/question-bank.md +75 -0
- package/skills/grill/scripts/decision-log.mjs +334 -0
- package/skills/tdd-loop/SKILL.md +110 -0
- package/skills/tdd-loop/references/seams.md +112 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "craft",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Design interrogation, hands-on TDD and deep-module refactoring: a grilling interview with a resumable decision log and capped question frontier, a TDD loop that fit-checks, confirms seams and enforces red-first, and a zero-dependency architecture scanner that ranks shallow modules by churn, fan-in, interface depth, pass-throughs and import cycles.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "SkillDeck"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"design-review",
|
|
11
|
+
"decision-log",
|
|
12
|
+
"tdd",
|
|
13
|
+
"architecture",
|
|
14
|
+
"deep-modules",
|
|
15
|
+
"refactoring"
|
|
16
|
+
]
|
|
17
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kitasid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# craft
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Design interrogation, hands-on TDD and deep modules for coding agents.**
|
|
4
|
+
|
|
5
|
+
Three skills for the work around the code. `grill` stress-tests a plan before it becomes tasks. `tdd-loop` drives a change test by test, with proof at each step. `deepen-modules` finds the modules that make every change expensive. Each one leaves an artifact you can check: a decision log, a loop log with pasted red output, or a metrics report.
|
|
6
|
+
|
|
7
|
+
| Skill | Use when | Helper |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| `grill` | You want a plan, spec or idea grilled: one question at a time, each with a recommended answer | `decision-log.mjs` keeps a resumable log in `docs/decisions/<topic>.md` (Resolved / Open frontier / Deferred / Prototype-needed) and caps the open frontier at 7 |
|
|
10
|
+
| `tdd-loop` | You want to drive a change red-green-refactor, or need test seams for awkward code | `references/seams.md`: seams for pure functions, module boundaries, HTTP handlers, CLIs and DB repositories, plus what to do when TDD doesn't fit |
|
|
11
|
+
| `deepen-modules` | You want to know which modules to refactor, with evidence | `arch-scan.mjs` ranks shallow modules by LOC per export, fan-in and fan-out, pass-through functions, import cycles and git churn |
|
|
12
|
+
|
|
13
|
+
They complement the [disciplines](../disciplines) pack rather than replacing it. `disciplines/test-first` is the rule and `tdd-loop` is the hands-on driver. `disciplines/brainstorming` shapes a new design, while `grill` attacks one that already exists and hands its summary to `disciplines/writing-plans`.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npx @skilldeck/craft install # Claude Code, user-wide
|
|
19
|
+
npx @skilldeck/craft install --project # this repo only
|
|
20
|
+
npx @skilldeck/craft install --agent all # also Codex and .agents
|
|
21
|
+
# or, with every SkillDeck pack available:
|
|
22
|
+
npx @skilldeck/cli add craft
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Claude Code marketplace: `/plugin marketplace add kitasid/skilldeck`, then `/plugin install craft@skilldeck`.
|
|
26
|
+
|
|
27
|
+
## Run the helpers yourself
|
|
28
|
+
|
|
29
|
+
They work without an agent, in a script, or in CI:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Architecture scan: writes .craft/arch-report.md (Markdown + Mermaid, fully offline)
|
|
33
|
+
npx @skilldeck/craft scan .
|
|
34
|
+
npx @skilldeck/craft scan . --since "3 months ago" --top 20
|
|
35
|
+
npx @skilldeck/craft scan . --json > arch.json
|
|
36
|
+
|
|
37
|
+
# Decision log for a grilling session
|
|
38
|
+
npx @skilldeck/craft decisions init checkout-v2 --goal "One-page checkout with saved cards"
|
|
39
|
+
npx @skilldeck/craft decisions add checkout-v2 "What if the payment webhook arrives twice?" --recommend "Idempotent on event id"
|
|
40
|
+
npx @skilldeck/craft decisions resolve checkout-v2 Q1 --answer "Idempotent on event id" --why "No double charges"
|
|
41
|
+
npx @skilldeck/craft decisions status checkout-v2
|
|
42
|
+
npx @skilldeck/craft decisions summary checkout-v2
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Both also run directly as Node scripts (Node 20+, no dependencies):
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
node skills/deepen-modules/scripts/arch-scan.mjs <root> [--out file.md] [--json]
|
|
49
|
+
node skills/grill/scripts/decision-log.mjs status <topic> [--json]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## What arch-scan reports
|
|
53
|
+
|
|
54
|
+
- **Per module:** LOC, exported symbols, LOC per export (depth), exports per 100 LOC (interface ratio), fan-in and fan-out over resolved internal imports (relative paths, TS `.js` to `.ts` swaps, and workspace package names), pass-through functions, and git churn over a window.
|
|
55
|
+
- **Import cycles:** strongly connected components found with Tarjan's algorithm.
|
|
56
|
+
- **Ranked candidates:** a documented score, `(5*shallow + 4*pass + 2*cycle) * (1 + 0.5*log2(1+fanIn) + 0.5*log2(1+churn))`, with a plain-language reason on every row.
|
|
57
|
+
- **A Mermaid graph** of the top candidates and their neighbors, inside a fenced code block. Nothing is fetched and no browser is opened.
|
|
58
|
+
- **Languages:** JS/TS by default. Python and Go are detected automatically and parsed by import regex; use `--lang` to choose.
|
|
59
|
+
|
|
60
|
+
If a cartograph `cartograph-out/graph.json` exists, the report points to it so candidates can be cross-checked with cartograph's `query.mjs`.
|
|
61
|
+
|
|
62
|
+
The decision log is plain Markdown that you can edit by hand, and the script parses it back. Exit code 3 means the frontier is full: resolve, defer or prototype a question before adding another.
|
|
63
|
+
|
|
64
|
+
MIT licensed. Node 20+. No dependencies beyond `@skilldeck/core` for the installer.
|
package/bin/craft.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from 'node:child_process'
|
|
3
|
+
import { runPackCli } from '@skilldeck/core'
|
|
4
|
+
import pack, { scripts } from '../index.mjs'
|
|
5
|
+
|
|
6
|
+
// Helpers run the exact scripts the skills ship, so CLI and agent behave identically.
|
|
7
|
+
function run(script, args) {
|
|
8
|
+
const r = spawnSync(process.execPath, [script, ...args], { stdio: 'inherit' })
|
|
9
|
+
process.exitCode = r.status ?? 1
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
await runPackCli(pack, process.argv.slice(2), {
|
|
13
|
+
extraCommands: {
|
|
14
|
+
scan: (args) => run(scripts.scan, args),
|
|
15
|
+
decisions: (args) => run(scripts.decisions, args),
|
|
16
|
+
},
|
|
17
|
+
extraHelp: `
|
|
18
|
+
Helpers (no agent needed):
|
|
19
|
+
craft-skills scan [root] [--out .craft/arch-report.md] [--json] rank shallow modules: churn, fan-in/out,
|
|
20
|
+
LOC per export, pass-throughs, cycles
|
|
21
|
+
craft-skills decisions init|add|resolve|defer|prototype|status|summary <topic> ...
|
|
22
|
+
resumable decision log in docs/decisions/
|
|
23
|
+
`,
|
|
24
|
+
})
|
package/index.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
|
|
5
|
+
const root = path.dirname(fileURLToPath(import.meta.url))
|
|
6
|
+
const { version } = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
|
|
7
|
+
|
|
8
|
+
/** Pack descriptor consumed by @skilldeck/core. */
|
|
9
|
+
export default {
|
|
10
|
+
name: 'craft',
|
|
11
|
+
version,
|
|
12
|
+
root,
|
|
13
|
+
description: 'Grill a plan into decisions, drive TDD loops, and deepen shallow modules with metrics.',
|
|
14
|
+
skills: ['grill', 'tdd-loop', 'deepen-modules'],
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const scripts = {
|
|
18
|
+
scan: path.join(root, 'skills', 'deepen-modules', 'scripts', 'arch-scan.mjs'),
|
|
19
|
+
decisions: path.join(root, 'skills', 'grill', 'scripts', 'decision-log.mjs'),
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skilldeck/craft",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Design craft for coding agents: grill a plan one question at a time with a resumable decision log, drive TDD loops with confirmed seams and pasted red output, and find shallow modules backed by churn, fan-in, interface-depth, pass-through and cycle metrics.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.mjs"
|
|
8
|
+
},
|
|
9
|
+
"main": "./index.mjs",
|
|
10
|
+
"bin": {
|
|
11
|
+
"craft-skills": "bin/craft.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"index.mjs",
|
|
15
|
+
"bin/",
|
|
16
|
+
"skills/",
|
|
17
|
+
".claude-plugin/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@skilldeck/core": "^0.1.1"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"claude-code",
|
|
30
|
+
"agent-skills",
|
|
31
|
+
"skill-md",
|
|
32
|
+
"design-review",
|
|
33
|
+
"decision-log",
|
|
34
|
+
"tdd",
|
|
35
|
+
"test-driven-development",
|
|
36
|
+
"architecture",
|
|
37
|
+
"deep-modules",
|
|
38
|
+
"refactoring",
|
|
39
|
+
"code-churn"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"author": "kitasid"
|
|
45
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: deepen-modules
|
|
3
|
+
description: Use when the user asks where the architecture is weak, which modules to refactor, wants to find shallow or pass-through modules, or says changes keep rippling across many files, and the refactoring targets should be backed by churn, fan-in, interface-depth and import-cycle metrics rather than taste.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Deepen Modules
|
|
7
|
+
|
|
8
|
+
A deep module hides a lot of behavior behind a small interface. A shallow one makes its callers learn nearly as much as it saves them. Shallow modules are cheap to write and expensive to keep, because every caller depends on details that should have stayed inside. This skill finds them with numbers, lets the user pick one, and runs a deletion test and a redesign loop on that one.
|
|
9
|
+
|
|
10
|
+
Announce: "Using deepen-modules to rank shallow-module candidates with metrics."
|
|
11
|
+
|
|
12
|
+
**Script location.** The scanner lives in `${CLAUDE_SKILL_DIR}/scripts/`. If `CLAUDE_SKILL_DIR` is not set, use the absolute path of the directory holding this SKILL.md. Below, `$S` is that folder. Node 20+, no dependencies, offline.
|
|
13
|
+
|
|
14
|
+
## The Rule
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
No candidate without numbers. No redesign without a deletion test.
|
|
18
|
+
One module at a time, chosen by the user, behind green tests.
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Checklist
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
- [ ] 1. Ran arch-scan; report written; numbers read, not skimmed
|
|
25
|
+
- [ ] 2. Presented the top candidates with evidence; user picked one
|
|
26
|
+
- [ ] 3. Ran the deletion test on it and wrote the verdict
|
|
27
|
+
- [ ] 4. Proposed a deeper interface; user agreed
|
|
28
|
+
- [ ] 5. Moved callers behind it test-first; reran the scan to show the delta
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 1. Scan
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
node $S/scripts/arch-scan.mjs <root> --out .craft/arch-report.md # Markdown report + top 5 on stdout
|
|
35
|
+
node $S/scripts/arch-scan.mjs <root> --since "3 months ago" --top 20 # tighter churn window, more rows
|
|
36
|
+
node $S/scripts/arch-scan.mjs <root> --json > .craft/arch.json # everything, machine-readable
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Per module it reports LOC, exported symbols, **LOC per export** (depth), exports per 100 LOC (interface ratio), import **fan-in** and **fan-out**, **cyclic import groups** (Tarjan), **pass-through functions** (the whole body is `return other(sameArgs)`), and **git churn** in the window. Outside a git repo, churn is skipped. It walks JS/TS by default and also Python and Go when it finds them (`--lang js,py,go` to choose). It skips `node_modules`, `dist`, `.git` and test files (`--include-tests` to count them). The score formula is printed in the report and in `references/deep-modules.md`. It is a ranking of where to look, not a verdict.
|
|
40
|
+
|
|
41
|
+
Add `.craft/` to `.gitignore` unless the user wants the report committed.
|
|
42
|
+
|
|
43
|
+
**Cartograph.** If a `cartograph-out/graph.json` exists (from the cartograph pack), the report notes it. Use `query.mjs explain "<module>"` and `query.mjs path "<A>" "<B>"` on it to see who calls a candidate and why before you judge it.
|
|
44
|
+
|
|
45
|
+
## 2. Present candidates
|
|
46
|
+
|
|
47
|
+
Show the top 5 to 8 as a table straight from the report: module, score, LOC/export, fan-in, churn, pass-throughs, cycle, and the "why". For each one, add one line of judgment after reading the code:
|
|
48
|
+
|
|
49
|
+
- **Shallow wrapper.** Many thin exports, low LOC per export, and callers that do the real work.
|
|
50
|
+
- **Pass-through layer.** It forwards calls with the same arguments and adds nothing but a name.
|
|
51
|
+
- **Barrel or re-export.** Often intentional for a package's public surface. Usually leave it alone.
|
|
52
|
+
- **Cycle member.** Two modules that can't be understood apart.
|
|
53
|
+
- **False positive.** A constants table, generated code, or a deliberate facade. Say so and drop it.
|
|
54
|
+
|
|
55
|
+
Then ask the user to pick one. Don't refactor several at once.
|
|
56
|
+
|
|
57
|
+
## 3. Deletion test
|
|
58
|
+
|
|
59
|
+
For the chosen module, imagine deleting it and inlining its body into every caller. Write down the answers:
|
|
60
|
+
|
|
61
|
+
1. **What would callers lose?** List the knowledge it hides: a format, an ordering rule, a retry policy, an invariant. If the list is empty, the module is a pass-through.
|
|
62
|
+
2. **How much would callers grow?** Compare the lines each caller gains with the module's size. If inlining barely adds anything, the module is shallow.
|
|
63
|
+
3. **What would callers have to know?** If they'd need its internals anyway (and they probably already do), the interface is leaking.
|
|
64
|
+
|
|
65
|
+
Verdict, in one of three words:
|
|
66
|
+
- **delete:** inline it and remove the layer.
|
|
67
|
+
- **merge:** fold it into its main collaborator. This is common for cycle pairs and for a wrapper with a single caller.
|
|
68
|
+
- **deepen:** keep it, but pull logic in from the callers so the interface shrinks.
|
|
69
|
+
|
|
70
|
+
`references/deep-modules.md` has the full method and the smells to look for.
|
|
71
|
+
|
|
72
|
+
## 4. Redesign
|
|
73
|
+
|
|
74
|
+
For **deepen**, write the new interface before writing any code. Aim for fewer, more general functions, with defaults for the common case and the special cases handled inside. Show the old and new signatures side by side, plus the caller code before and after for the two busiest callers. Get agreement on the interface.
|
|
75
|
+
|
|
76
|
+
## 5. Move callers, test-first
|
|
77
|
+
|
|
78
|
+
Pin current behavior with tests at the module's public seam ([[tdd-loop]] for the loop, [[test-first]] for the rule). Then move one caller at a time onto the new interface, running the suite after each move. Delete the old exports only when nothing imports them. Rerun the scan and show the delta for this module: LOC/export, export count, fan-in, pass-throughs, cycle. If the refactor touches many files, write a plan with [[writing-plans]] first.
|
|
79
|
+
|
|
80
|
+
## Output
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
Candidate: <module> score <s> (LOC/export <d>, fan-in <n>, churn <c>, pass-through <p>, cycle <yes/no>)
|
|
84
|
+
Deletion test: callers would lose <...>; inlining adds <n> lines; verdict <delete|merge|deepen>
|
|
85
|
+
Interface: <old exports> -> <new exports>
|
|
86
|
+
After: LOC/export <d'>, exports <e'>, fan-in <n'>, pass-throughs <p'>; suite green
|
|
87
|
+
Report: .craft/arch-report.md
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Red flags
|
|
91
|
+
|
|
92
|
+
| Thought | Reality |
|
|
93
|
+
|---|---|
|
|
94
|
+
| "This module feels shallow." | Feelings aren't evidence. Run the scan and cite the numbers. |
|
|
95
|
+
| "Top score, so refactor it." | The score says where to look. The deletion test decides. Barrels and constant tables score high and are often fine. |
|
|
96
|
+
| "Small modules are good modules." | Small interfaces are good. Many small modules with wide interfaces push the complexity onto callers. |
|
|
97
|
+
| "I'll deepen the top five while I'm here." | One module, chosen by the user, behind green tests. |
|
|
98
|
+
| "Split it into more files to make it cleaner." | Splitting usually makes modules shallower. Deepening usually means merging. |
|
|
99
|
+
| "Tests can come after the refactor." | Pin the behavior first, or the refactor is a rewrite with no safety net. |
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Deep modules: the method
|
|
2
|
+
|
|
3
|
+
## Depth
|
|
4
|
+
|
|
5
|
+
A module's **cost** is its interface: everything a caller must know to use it correctly. That includes function names, parameters, return shapes, error cases, ordering rules and side effects. Its **benefit** is the functionality it provides: the behavior callers get without having to know how it works.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
depth = functionality / interface
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
A **deep** module offers a lot of functionality through a small interface. A Unix file descriptor is the classic case: five calls hide disks, caches, permissions and devices. A **shallow** module has an interface nearly as complex as its implementation, so callers save little and still depend on its details.
|
|
12
|
+
|
|
13
|
+
`arch-scan.mjs` approximates depth with what it can count:
|
|
14
|
+
|
|
15
|
+
| Metric | Proxy for | Reading |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| LOC per export | functionality per unit of interface | under ~30 suggests thin; hundreds per export is usually deep |
|
|
18
|
+
| Exports per 100 LOC (interface ratio) | interface weight | high means a wide surface for little code |
|
|
19
|
+
| Pass-through ratio | layers that add no behavior | any is suspicious; 1.0 means the module is pure forwarding |
|
|
20
|
+
| Fan-in | how many modules pay the interface cost | a wide, shallow interface with high fan-in spreads change widely |
|
|
21
|
+
| Churn | how often the interface or body changes | a shallow module with high churn leaks change to its callers |
|
|
22
|
+
| Cycle membership | modules that can't be understood apart | a candidate for **merge** |
|
|
23
|
+
|
|
24
|
+
### The score
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
thin = clamp((30 - LOC/exports) / 30, 0, 1)
|
|
28
|
+
breadth = min(1, log2(1 + exports) / log2(5)) 1 export 0.43, 2 exports 0.68, 4+ exports 1
|
|
29
|
+
shallow = thin * breadth * (0.5 if data-only, else 1) data-only: exports but no functions
|
|
30
|
+
pass = pass-through functions / functions 1 for a re-export-only module
|
|
31
|
+
cycle = 1 if the module is in an import cycle
|
|
32
|
+
base = 5*shallow + 4*pass + 2*cycle 0 .. 11
|
|
33
|
+
score = base * (1 + 0.5*log2(1 + fanIn) + 0.5*log2(1 + churn))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`base` says how shallow the module looks. The multiplier says how much that matters: how many modules depend on it and how often it changes. A module with `base = 0` is never a candidate, however popular it is. Use `--json` to see `scoreParts` for each module.
|
|
37
|
+
|
|
38
|
+
**Known blind spots.** The scan counts lines, not concepts, so a 10-line module that encodes a subtle rule can be deep. Types-only modules, constant tables, generated code and intentional public barrels can score high. Dynamic imports with computed paths and tsconfig path aliases are not resolved. Treat the report as a list of places to look.
|
|
39
|
+
|
|
40
|
+
## The deletion test
|
|
41
|
+
|
|
42
|
+
Imagine deleting the module and inlining its code into each caller.
|
|
43
|
+
|
|
44
|
+
1. **List what callers would lose.** Each item should be a piece of *knowledge*: a wire format, an ordering rule, a retry or backoff policy, an invariant, a cache, a unit conversion. Code that is merely moved doesn't count.
|
|
45
|
+
2. **Count what inlining costs.** Count the lines added to each caller. If the total is close to the module's own size times one caller, the module was mostly interface.
|
|
46
|
+
3. **Ask what callers already know.** If callers already handle the module's special cases, pass its internal flags around, or call its functions in a required order, the abstraction is leaking.
|
|
47
|
+
|
|
48
|
+
| Result | Verdict |
|
|
49
|
+
|---|---|
|
|
50
|
+
| Nothing lost; inlining adds about as many lines as it removes | **delete**: inline it and remove the layer |
|
|
51
|
+
| Only one or two callers, or it sits in a cycle with its main collaborator | **merge**: fold it into the collaborator |
|
|
52
|
+
| Real knowledge is hidden, but callers still juggle details | **deepen**: pull those details inside and shrink the interface |
|
|
53
|
+
| Real knowledge is hidden and callers stay simple | **keep**: a false positive. Note why and move on |
|
|
54
|
+
|
|
55
|
+
## Smells
|
|
56
|
+
|
|
57
|
+
- **Pass-through function.** `getUser(id) { return repo.getUser(id) }`. The name is the only thing added. One is a smell; a module full of them is a pass-through layer.
|
|
58
|
+
- **Pass-through variable.** A parameter threaded through four layers so that the bottom one can use it. Give the bottom layer access to it directly (context, configuration, or a constructor argument).
|
|
59
|
+
- **Shallow wrapper.** `add`, `sub`, `mul` and friends, each one line, re-exported for "consistency". Callers would understand the operator directly.
|
|
60
|
+
- **Leaky ordering.** The interface only works if callers call `init()`, then `load()`, then `start()`. Fold the sequence into one function.
|
|
61
|
+
- **Configuration explosion.** Ten options, nine of which every caller sets the same way. Make those the defaults and drop them from the interface.
|
|
62
|
+
- **Classitis.** Many tiny classes, each a handful of lines, that are always used together. Merge them into one module with one interface.
|
|
63
|
+
- **Temporal decomposition.** Modules split by *when* things happen (read, parse, write) rather than by *what knowledge* they hide, so one format change touches all three.
|
|
64
|
+
- **Cycle.** A imports B and B imports A. Usually this is one concept split in two. Merge them, or extract the shared knowledge into a third module that both depend on.
|
|
65
|
+
|
|
66
|
+
## Redesigning toward depth
|
|
67
|
+
|
|
68
|
+
1. **Write the interface first.** Before writing any code, write down the new exported functions and their types. Aim for fewer, more general operations: `save(entity)` instead of `saveUser`, `saveOrder` and `saveDraft`, when the knowledge they encode is the same.
|
|
69
|
+
2. **Pull complexity down.** If every caller handles a case, move it inside the module. The module author handles it once, instead of every caller handling it repeatedly.
|
|
70
|
+
3. **Define errors out of existence.** Where you can, make an operation always succeed (deleting a missing key is a no-op; a range past the end is clipped) instead of making every caller handle an error.
|
|
71
|
+
4. **Defaults for the common case.** Required parameters should be the ones that actually vary between callers.
|
|
72
|
+
5. **Show it on the busiest callers.** Write the before and after for the two callers with the most usage. If the after isn't clearly simpler, the redesign isn't deeper.
|
|
73
|
+
6. **Migrate behind tests.** Pin the behavior at the public seam, move callers one at a time, and delete the old exports last. Rerun `arch-scan.mjs` and report the change in LOC/export, export count, fan-in and pass-throughs.
|
|
74
|
+
|
|
75
|
+
## A worked example
|
|
76
|
+
|
|
77
|
+
Report row: `src/lib/user-service.ts`, score 9.4, 18 LOC/export, 6 exports, fan-in 11, churn 14, 5/6 functions pass-through.
|
|
78
|
+
|
|
79
|
+
- **Deletion test.** Callers would lose nothing. Five functions forward to `userRepo` with the same arguments, and the sixth adds a `lowercase(email)` that two callers repeat anyway. Inlining adds about one line per call site. Verdict: **deepen**, because there is real knowledge (email normalization, uniqueness on signup) that callers currently duplicate.
|
|
80
|
+
- **New interface.** `register(email, profile)` and `find(by)`, with normalization, the uniqueness check and the duplicate-error mapping moved inside. Six exports become two.
|
|
81
|
+
- **After.** 54 LOC/export, 2 exports, fan-in 11, zero pass-throughs. Callers lost their own normalization code.
|