carlos / platform Public

plan: Go code health, fix, hold and improve

The plan behind the 2026-11-16 audit (#1123): batch 2 and 3 fixes,
a code-health ratchet in CI that only goes down, Go rules in AGENTS.md,
a monthly health pass and a quarterly one-slice audit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev b2bf354b52353c4621208fe43a4cc4f4916197bf parent c4b44c3
1 file changed, +192 −0
  • docs/superpowers/plans/2026-11-16-go-code-health.md +192 −0

CI cancelled — superseded by a newer push — run details

diff --git a/docs/superpowers/plans/2026-11-16-go-code-health.md b/docs/superpowers/plans/2026-11-16-go-code-health.md
new file mode 100644
index 000000000..ea3e327e3
--- /dev/null
+++ b/docs/superpowers/plans/2026-11-16-go-code-health.md
@@ -0,0 +1,192 @@
+# Go code health: fix, hold, improve
+
+Status: draft for Paul, 2026-11-16. Backlog and audit reports: amadan
+discussion #1123. First batch merged the same day (958ff2e, b064b14,
+0374ac2).
+
+## What this plan is for
+
+The 2026-11-16 audit found the code careful but with three kinds of gap:
+network calls with no deadline, holes in the CI gate, and shapes that keep
+growing (a 162k-line console package, 1,200-line functions, 999
+`log.Fatal` calls in the CLI, 1,019 sleeps in tests). The first batch
+closed the worst deadlines and two gate holes.
+
+Fixing the current list once is not enough. Between the audit (5b6d5374)
+and the recheck (c4b44c34), eight days apart, the CLI gained 45
+`log.Fatal` calls, tests gained 147 sleeps, and the three longest
+functions each grew by 100 to 200 lines. So the plan has three parts:
+
+1. **Fix** what is known, in small reviewed branches.
+2. **Hold** every gain with a check in CI, so nothing that was fixed comes
+ back and nothing measured gets worse.
+3. **Improve** steadily: convert code as it is touched, and look again on
+ a fixed rhythm.
+
+## Principles
+
+- **Ratchets, not sweeps.** A count that can only go down beats a
+ one-time cleanup. Sweeps across hundreds of files cost review rounds and
+ census cards; ratchets cost nothing until someone touches the code.
+- **Convert on touch.** A branch that changes a function brings that
+ function up to the rules below. No branch exists only to restyle.
+- **Guards over rules.** A rule in AGENTS.md is read sometimes; a test
+ that fails is read every time. Where a rule can be a guard test, it is.
+- **Small branches, the stopping rules unchanged.** One concern per
+ branch, Opus builds, Astra reviews (Fable for whole-branch reviews
+ where the tier matters), three rounds at most.
+- **Recheck before building.** Every backlog item is checked against
+ current main first; the audit's own baseline was 159 commits stale.
+
+## Part 1: fix
+
+### Batch 2 (started 2026-11-16)
+
+| Branch | What | Census |
+|---|---|---|
+| `audit-bugs-2` | ledger verify timeout and `{}` HEAD panic (cli.go); system update decode errors (system.go); "ops" substring match (console/ownership.go); #1141 subdomain lost race; silent capacity page (host/haplacement.go); escrow alerts matched by string prefix (activator/escrow.go) | yes |
+| `audit-console-shutdown` | console drains HTTP and its workers on SIGTERM before releasing the policy lease, so rolls stop dropping requests | no (console) |
+| `audit-govulncheck` | `50-govulncheck` CI step with an explicit, dated exception file; GO-2026-6859 listed under the 1.26.10 pin (#897, #904) | no |
+| `audit-edge-test-sleeps` | about 13 s of fixed sleeps in edge tests replaced through the seams that exist (`Edge.watchTick`, a seam for `holderHeaderSuspectAfter`) | no (tests) |
+
+Plus the one-line AGENTS.md fix: its sandbox note recommends a GOCACHE
+path the machine policy bans (rides `audit-bugs-2`).
+
+### Batch 3: close the follow-ups
+
+- #1143: credential clients get the body bound; an explicit buildable
+ client stays the caller's; the AWS client guard catches field
+ assignment and option callbacks.
+- The edge prober's unbounded dial (`internal/edge/upstreamdial.go`
+ comment).
+- Host loops that wait on `systemctl`/`journalctl` with no timeout
+ (adoptLoop, fastConvergeLoop, statusLoop, configSync/configGen): one
+ bounded `systemctl` helper, used everywhere.
+- staticcheck's excluded families, one branch each, then switched on:
+ SA4006 (70, includes #1141's shape), SA1019 (70), SA1012 (177, mostly
+ nil contexts in internal/registry).
+- Racy test helpers (host/caps_pass_test.go captureLog,
+ activator/wakecheck_test.go), then a targeted `-race` CI step on edge,
+ activator, lease and host.
+- gitleaks as its own CI step.
+
+### Batch 4 onward: structure, each with its own short spec
+
+In this order, because each makes the next cheaper:
+
+1. **Census `moved` class** in `census-propose`: a pure file move carries
+ its disposition instead of reading as a removal plus a new unit. Every
+ split below in the root binary's graph depends on it.
+2. **Leaf package for the S3 `Store` interface** out of
+ `internal/release` (67 importers, all linking the registry and SQLite
+ today).
+3. **Leaf package for the names console takes from activator and edge**,
+ so the console stops linking 78k lines it barely uses.
+4. **Console request contexts**: request-path queries first (459
+ context-free calls on one SQLite connection; an abandoned request
+ keeps its place in the queue).
+5. **Console split, staged**: widen the 15 package-directory source guards
+ first, then `consoledb` (the Store), then billing, alerts, operator
+ pages, architecture view and PDF, one at a time.
+6. **CLI commands return errors**: convert on touch to
+ `runXWithSession(ctx, sess, args, out) error`; the ratchet below holds
+ the count.
+
+## Part 2: hold
+
+### A code-health ratchet in CI
+
+A test-only package (`internal/codehealth`, outside the census because
+nothing links it) measures the tree with `go/parser` and compares it with
+a checked-in record, `.amadan/code-health.tsv`, one line per metric and
+package. The same shape as Rastrillo's `.rastrillo/code.txt`: the records
+start at today's numbers and only go down.
+
+- A count above its record fails the gate and names the new offences.
+- A count below its record also fails, and prints the lower number to
+ commit, so a gain is locked in the branch that made it.
+ `make code-health-record` rewrites the file.
+- A metric's record reaching zero turns it into a plain guard.
+
+Metrics at the start (numbers from c4b44c34, to be re-measured):
+
+| Metric | Today |
+|---|---|
+| `log.Fatal` and `os.Exit` in root-package production files | 999, 120 |
+| `time.Sleep` in tests, per package | 1,019 total |
+| context-free `db.Query`/`QueryRow`/`Exec` in internal/console | 459 |
+| functions over 300 lines, by name, with their length (no new ones; listed ones may only shrink) | list |
+| files over 3,000 lines, same rule | list |
+| packages importing internal/release | 67 |
+| `context.Background()`/`TODO()` in internal/host production code | to measure |
+
+### Guards that already hold a line
+
+- Every edge `http.Server` sets its timeouts (958ff2e).
+- Every AWS client in the root binary goes through the bounded transport
+ (0374ac2, `TestAWSClientsAreBounded`).
+- staticcheck SA checks clean (b064b14), rootrunner tests vetted.
+
+Each later batch adds its own guard where the rule is mechanical (the
+bounded `systemctl` helper, for one).
+
+### Go rules in AGENTS.md
+
+A short "Writing Go here" section, each rule pointing at the guard or
+ratchet that enforces it:
+
+- Network calls carry a deadline; AWS clients come from internal/rootaws.
+- Functions that have a `ctx` pass it on; a detached
+ `context.Background()` carries a comment saying why.
+- New CLI commands return errors; they do not call `log.Fatal`.
+- New tests wait on a signal or a seam, not `time.Sleep`.
+- Errors are compared with `errors.Is`/`errors.As` and wrapped with `%w`
+ when a caller might branch on them; never match `err.Error()` text.
+- A function past 300 lines, or a file past 3,000, is split by concern in
+ the branch that would grow it.
+
+## Part 3: improve
+
+### Monthly health pass (first Monday of the month)
+
+One session, read-only, under an hour of agent time:
+
+1. Re-measure the ratchet and staticcheck with every check on (S, ST,
+ QF, U1000 included) and govulncheck; post the numbers on a standing
+ discussion, with the change since last month.
+2. Lower any records that fell and were not locked in.
+3. Pick the next two or three items from the backlog for the month and
+ post them for Paul's go.
+
+### Quarterly audit, one slice at a time
+
+Each quarter, one read-only Fable auditor takes one slice (console; host
+family; activator and edge; root CLI and the rest; in rotation) with the
+2026-11-16 brief, against current main. Astra verifies the findings
+before they reach the backlog. New findings become backlog items or new
+ratchet metrics. Every slice is seen once a year.
+
+### Tightening over time
+
+- staticcheck: SA families back on (batch 3), then U1000 (dead code),
+ then the S and ST checks worth having, one at a time, each with its
+ findings fixed or recorded first.
+- Ratchet thresholds come down once the lists are short: functions from
+ 300 lines towards 150, files from 3,000 towards 1,500.
+- Test speed: the slowest-test list from `test-weights.tsv` is reviewed
+ each health pass; sleeps over one second are the first targets.
+
+## What is not in this plan
+
+Settled by the audit as not worth doing: a `slog` migration, a repo-wide
+`%w` or context sweep, moving the CLI to `internal/cli`, subpackages
+inside activator, mass-unexporting in host.
+
+## Done means
+
+- Batches 2 and 3 merged; staticcheck runs every SA check; govulncheck,
+ gitleaks and a targeted `-race` run in CI.
+- The ratchet in CI, its records never raised.
+- Each structural item merged or explicitly dropped with a reason.
+- After that the monthly pass and quarterly audit continue; this plan is
+ then a rhythm, not a project.