Skip to content
joinery / docs

🧭 The scripts package is architecturally sound and owes two unfinished cleanups

3054 words · updated 6 hours agodocs/decisions/0019-scripts-quality-audit.md

🧭 The scripts package is architecturally sound and owes two unfinished cleanups

Context

infrastructure/scripts was written fast and mostly by an agent. 0004 specified a four-layer architecture for it — entry → command → adapters/ → domain/ — and stated plainly that the direction is "convention, not lint, today". Nothing has checked since whether that convention is actually implemented or merely documented, and the package has roughly doubled in size since 0004 was written: two whole scripts (agents-sync/, coverage-report/) postdate it.

Five questions were put to it: is the code bad, is the architecture real, are the docs honest, do the boundaries hold, and what should change. The answers are not evenly distributed — the structural ones come back clean, and what is left is two cleanups that were already specified here and never finished.

Result

The architecture is implemented, not just described. The quality problems are two, and both are failures to apply rules this repo had already written down — the comment discipline from 0009, whose status still reads to-implement and whose Confirmation criterion names this package, and 0004's own instruction to move logic into domain/, which has not been applied to a command since 0004 was filed. Neither is a design mistake; both are the code failing to catch up with its docs.

What holds

The layer rule from 0004 is respected with no exceptions. Four greps over src/, all empty:

InvariantCheckResult
No sideways imports between script foldersgrep -rn 'from "\.\./\.\./\(issue|ship|map|drift|dev|agents-sync|review-version|coverage-report\)/' srcempty
domain/ is puregrep -rn 'node:fs|node:child_process|@clack|node:process|node:os' src/*/domain/ src/shared/domain/empty
domain/ never reaches an adaptergrep -rn 'adapters/' src/*/domain/ src/shared/domain/one hit, a doc link inside a comment
Nothing calls process.exitgrep -rn 'process.exit(' srcempty

Boundaries hold too. The package is tagged type:tooling, which boundaries.ts restricts to type:config only; its sole workspace dependency is @joinery/configs, and nothing in the workspace imports it. lint and typecheck are clean, and 121 tests across 14 spec files pass. Every domain/ module that carries real logic has a spec beside it.

The docs are the best part of the package and better than the code they describe. The layer table in 0004, the package README, the per-script READMEs and shared/README.md agree with each other and with the filesystem. Three claims in them have gone stale (below), which is drift rather than invention.

The comment problem is 0009's, resolved

0009 measured this package at 599 comment lines / 2421 code lines = 24.7%, the worst in the workspace, called 4–6% "the house range", and called for a sweep down to ~120 comment lines. The sweep now sits below that target and inside the house range:

Measure0009 baselineToday0009's target
Comment lines59986~120
Comment / code24.7%5.16% (86 / 1666)4–6%
Blocks over the two-sentence cap22 ran 7–19 lines00

The package now sits in the house range named by 0009. The sweep removed narration and duplicated explanations while retaining scanner findings, API traps, units, and outside constraints.

The distribution is bimodal, and that is the more useful finding. Bloat is concentrated in the six scripts that predate 0004; the two that postdate it carry one comment across every file:

VintageScriptsComment lines
Pre-0004shared/, issue/, dev/, drift/, map/, ship/299
Post-0004agents-sync/, coverage-report/1

That confirms 0009's claim that density "tracks recency of agent authorship rather than complexity of the code" — and it means the house style is already achievable here. coverage-report/ retains the one line that genuinely needs a comment: adapters/nx.ts:11 sets const PATH = "/usr/bin:/bin" and overrides the child's PATH, which is exactly the kind of outside constraint 0009's keep-test says to write down.

Concrete defects

Seven, none of them severe, ordered by what they cost:

#DefectLocationCost
1Module-scope require.resolve — violates the AGENTS.md rule whose own rationale is that breaking it made modules unimportablecoverage-report/adapters/nx.ts:9-10The file cannot be imported where nx isn't resolvable, which is why it has no spec
2The setup → quick-start rename in 6f2a58f never finishedmap/domain/render.ts:19, root README lines 87 and 114pnpm setup does not exist; the generated FEATURES.md tells readers to run it
3README §Tests names 9 of the 14 spec files; AGENTS.md says only one existsscripts/README.md §✅ Tests, scripts/AGENTS.md:38Both read as "this package barely tests anything", which is no longer true
4Two different currentBranch implementations answering one questionissue/adapters/git.ts:15 (branch --show-current) vs ship/adapters/git.ts:5 (rev-parse --abbrev-ref HEAD)They disagree on detached HEAD — "" vs "HEAD". 0004's adapter-naming rule licenses two git.ts files, not two answers
5Title-casing implemented twicedev/domain/projects.ts:22-23, drift/domain/detect.ts:50-54The drift copy has no acronym list, so it would render "Ui" — the exact case the dev copy exists to avoid
6One prompt bypasses orExit, against the stated invariant that every prompt unwinds through itissue/add.ts:106Behaviour is correct; the inconsistency is the cost
7parseManifest(before) parsed three times in one functionreview-version/main.ts:20,27,33Cosmetic

The command layer carries more logic than the domain layer, and none of it is tested

28.5% statement coverage reads alarming and mostly isn't: the architecture puts side effects in adapters/, and the README is right that a spec over gh asserts a mock against itself. Split by layer, the number says something more specific:

LayerStatementsCoveredCoverage
domain/22722096.9%
adapters/3563710.4%
command33582.4%
entry1200%

The domain layer is doing its job — every one of the ten domain/ modules has a spec, and six are at 100% statements and branches. The defect is the inversion above it: the command layer holds more statements than the domain layer it sits on (335 vs 227) at 2.4% covered. In a layout whose whole premise is that decisions move into pure functions, commands should be thin orchestration and domain/ should be the fat part. Here it is the other way round.

What is stranded in commands is decidable logic, not I/O, and the package's own rule already says to extract it — that rule just hasn't been applied since 0004:

FileStatementsThe logic that should be in domain/
issue/pick.ts87The BACK re-prompt loop, and the three-way offline / auth-error / online fallback in pickIssue — which is a decision about an error, not a subprocess call
issue/add.ts46pickLabel's filter predicate and the empty-pick-to-omitted-flag mapping
shared/cli.ts39parse, invocation, detailOf and report's error-to-exit-code mapping — all pure, all untested
drift/main.ts36The fingerprint-seen skip, and the decision to record a fingerprint before deciding whether to warn
ship/main.ts29The passed/merged branching that decides the exit code

shared/cli.ts is the sharpest case: it is the file every entry point stands on, it defines what an exit code means for the whole package, it is pure apart from reading process.argv, and it has no spec at all. shared/adapters/io.ts (15.8%) is second — isInteractive() and the clack-vs-plain dialect switch are pure branching wearing an adapter's name.

Two nits ESLint does not catch, both instances of open issue #138 (JSON linting): package.json:19 indents "lint" by nine spaces where every sibling uses four, and issue/domain/search.ts:22-25 has a one-space blank line and a six-space operator continuation.

Options considered

OptionWhy not
Report the package as broadly low qualityIt isn't. Lint, typecheck, boundaries and all four layer invariants pass, and 121 tests do too. What is left is seven small defects and two stalled cleanups
Treat the 19.0% comment ratio as a new finding0009 already found it, measured it, set the target and shipped the comment-cleanup skill for it. This report records that the sweep stalled on this package, and reuses 0009's numbers rather than inventing a second standard
Gate the comment ratio in check-invariants.sh0009 rejected exactly this: the ratio is a symptom, a legitimately dense file would fail and 40 tidy narrations would pass. Unchanged here
Land no-restricted-imports to enforce the layer rule nowThe greps come back clean, so there is nothing to catch today, and the README's deferral cost still stands — it would replace this package's thin-wrapper eslint.config.ts. Worth doing when a violation actually appears, not before
Fold the two currentBranch into shared/adapters/git.tsTempting, but it re-opens what 0004 settled: the shared module holds git itself, not each script's questions. Picking one implementation per call site is the smaller fix (defect 4)
Rewrite coverage-report/adapters/nx.ts onto shared/adapters/exec.tsIts spawn-to-temp-file shape may be load-bearing for output size, and nothing says so. Moving the module-scope resolve inside the function (defect 1) gets the testability win without guessing at the rest
Do the comment sweep in this passScoped and left as follow-up at the user's direction. It touches nearly every file in src/ and wants the comment-cleanup skill, not a hand pass
Read 28.5% coverage as the test findingIt is the wrong unit. Split by layer, domain/ is at 96.9% and adapters/ is untested on purpose; the finding is the command layer holding more statements than the domain layer at 2.4% covered
Raise coverage by writing specs against the commands as they standThat means mocking gh, clack and git to assert orchestration — the specs the README already refuses, and they would pin the wiring rather than the decisions. Extract the decision into domain/ first, then the spec is worth having
Set a coverage threshold in the vitest configIt would be met by testing adapters against their own mocks, which is the outcome this package correctly avoids. The layer split in Confirmation is the check that can't be gamed that way

Consequences

The seven defects and the targeted domain extraction are now implemented; the comment sweep completes 0009's package-level confirmation without changing 0009's own status.

1. The seven defects, smallest diff first. Each is independent:

#Change
1Move createRequire/require.resolve from coverage-report/adapters/nx.ts module scope into nx(). While there, add the missing import process from "node:process" — it is the only non-spec file in the package that uses process without it — and write the one sentence that explains PATH = "/usr/bin:/bin"
2Replace pnpm setup with pnpm quick-start in map/domain/render.ts:19 and root README lines 87 and 114, then pnpm docs:map. This also closes the — in FEATURES.md's pnpm quick-start row: that cell is empty because the README calls the command by its old name
3Rewrite scripts/README.md §✅ Tests to stop enumerating spec files — the list has gone stale twice — and fix scripts/AGENTS.md:38. Then pnpm agents:sync, since CLAUDE.md is generated
4Change issue/adapters/git.ts:15 to git("rev-parse", "--abbrev-ref", "HEAD"), matching ship/. Not the reverse — branch --show-current returns "" on a detached HEAD, and issue/add.ts:103 compares the result against "master", so an empty string silently reads as "not on master" and offers a branch prompt from a detached checkout
5Move titleCase and ACRONYMS out of dev/domain/projects.ts into shared/domain/layout.ts and have drift/domain/detect.ts's displayName call it. Both are pure and layout.ts is already the shared pure module, so this needs no new file. Do not leave the divergence commented — drift would keep rendering "Ui" for packages/ui
6Route issue/add.ts:106 through orExit, dropping the direct isCancel import
7Hoist parseManifest(before) to one call in review-version/main.ts

2. Tests, in the order the layer table argues for. The target is not a coverage percentage — it is moving decisions out of commands so they can be tested:

WorkWhy first
A spec for shared/cli.tsHighest value per line in the package: run's error-to-exit-code contract is what every script relies on, and parse, invocation and detailOf are pure. Needs no extraction — write the spec against the file as it stands
Extract pickIssue's offline / auth-error / online decision from issue/pick.ts into issue/domain/87 untested statements, and the branch that decides whether a stale cache is acceptable is the one most worth pinning
Extract ship/main.ts's exit-code branching into ship/domain/report.tsshipMessage already lives there and is tested; the passed/merged decision that picks the message does not
Extract drift/main.ts's fingerprint-seen skip into drift/domain/detect.tsIt decides whether a push nags, and the ordering — fingerprint recorded before the warn decision — is currently unstated and unpinned either way
A spec for shared/adapters/io.ts's isInteractive()Pure branching on isTTY/CI; the only adapter here worth a spec

Leave adapters/ otherwise untested. That is the architecture working, not a gap.

3. Finish 0009's sweep: 299 → 86 comment lines. The pass ran worst-ratio-first through the 13 listed files, then covered every remaining over-cap block.

Shorten, do not delete. The facts in these comments are the ones 0009's keep-test says to hold onto — what a scanner flagged, why a strange sort order exists, a trap in an API. Deleting the block loses them; the job is to find the one sentence and drop the argument around it. The shape to aim for, from shared/adapters/exec.ts:17-22:

// Before — 6 lines, 3 sentences, the last two arguing with a reader who isn't there:
/**
 * A value starting with `-` would be read as a flag instead of the flag's value — the injection
 * shape SonarCloud flags where a flag's value is untrusted text (a typed title, a picked label).
 * `run` can't check this itself since it also carries real flags like `--title`, so callers like
 * `createIssue` call this on just their untrusted values.
 */

// After — the fact that can't be reconstructed from the code, and nothing else:
/** A value starting with `-` is read as a flag, not a value — the injection shape SonarCloud flags. */

The second and third sentences go because the next reader can reconstruct both from the signature: that run takes real flags is visible at its call sites, and that callers pass untrusted values is what the field parameter is for. What survives is the one thing the code cannot say — that a scanner flagged this shape.

The order to work in, worst ratio first. A file at or under roughly 1 comment line per 15 of code is done:

FileComment / codeOver-cap blocks
shared/adapters/exec.ts13 / 122
dev/adapters/nx.ts13 / 102
drift/adapters/git.ts13 / 152
issue/domain/branch.ts11 / 111
shared/adapters/git.ts6 / 61
shared/errors.ts6 / 21
ship/adapters/git.ts11 / 181
dev/adapters/pnpm.ts10 / 192
shared/adapters/cache.ts23 / 431
shared/adapters/io.ts16 / 302
ship/adapters/gh.ts20 / 510
shared/cli.ts21 / 572
map/domain/capabilities.ts28 / 822

The remaining 21 files are already at or near the house range; map/domain/render.ts (2 / 70) and issue/pick.ts (6 / 144) are the in-package models of what the rest should look like.

The 26 over-cap blocks, for the sentence-level pass — the cap is one sentence, two at most, counted by sentence and not by line, so a two-sentence block wrapped over five lines is already compliant:

map/domain/capabilities.ts:102 (4 sentences), dev/index.ts:5 (4), then three-sentence blocks at drift/adapters/git.ts:4 and :28, dev/adapters/nx.ts:8 and :19, dev/adapters/pnpm.ts:6 and :30, ship/domain/report.ts:6, ship/adapters/git.ts:7, shared/cli.ts:30 and :66, shared/adapters/io.ts:4 and :14, shared/adapters/git.ts:10, shared/adapters/exec.ts:4 and :17, shared/adapters/gh.ts:12, shared/adapters/cache.ts:15, shared/errors.ts:1, map/adapters/files.ts:85, map/domain/capabilities.ts:69, issue/domain/search.ts:1, issue/domain/branch.ts:6, issue/adapters/git.ts:3, issue/add.ts:31.

Forecloses nothing. The layer rule stays convention rather than lint, on the evidence that it is being followed without enforcement — which is the first time that claim has been checked rather than assumed, and is the thing to re-check before anyone spends the eslint.config.ts to enforce it.

Confirmation

The seven defects:

grep -rn "pnpm setup" README.md docs/FEATURES.md infrastructure/scripts/src   # empty
grep -rn "^const nxPath" infrastructure/scripts/src                           # empty
pnpm docs:map --check                                                         # passes

The four architecture invariants are the four greps in Result, all of which must stay empty; they are the check this report adds, and they are cheap enough to re-run by hand on any change to the package.

The test work is confirmed by the layer split rather than by the headline percentage, which stays low by design while adapters/ goes untested. pnpm exec nx test:coverage @joinery/scripts reports domain/ at 272/279 statements (97.5%) and the command layer at 320 statements, below the 335 statement baseline.

0009's sweep is confirmed the way 0009 already specified — this package landing in the 4–6% comment-line-to-code-line band — with the count reproducible as comment lines over code lines across non-spec files under infrastructure/scripts/src. 5.16% today; 0009 remains to-implement until its separate developer-portal confirmation passes.