Repository navigation
refactor: express gate outcomes as a discriminated union - #171
Conversation
abogartz
left a comment
There was a problem hiding this comment.
APPROVE — the union is enforced, provably inert at runtime, and survives #158
Reviewed at head a3a17ae (merge-base e32327a / v0.14.17) on macOS + Bun 1.4.3, plus a local merge against current main 20bb57e (v0.14.18, i.e. #158 already in) — which is the one place this PR could have gone wrong quietly.
1. The contract file is not decorative — I made it fail on the old interface
tsconfig.json has include: ["index.ts","src","test","bench","e2e"] and no exclude, so bun run typecheck really does read test/gate-outcome.types.ts. I copied that exact file into a clean e32327a checkout and ran the typecheck gate against the old interface:
test/gate-outcome.types.ts(15,3): error TS2578: Unused '@ts-expect-error' directive. # promoted without ts
test/gate-outcome.types.ts(17,3): error TS2578: Unused '@ts-expect-error' directive. # promoted with ts: undefined
test/gate-outcome.types.ts(19,3): error TS2578: Unused '@ts-expect-error' directive. # blocked without reason
test/gate-outcome.types.ts(21,3): error TS2578: Unused '@ts-expect-error' directive. # blocked with reason: undefined
test/gate-outcome.types.ts(33,20): error TS2345: 'string | undefined' is not assignable to 'string' # outcome.ts after narrowing
test/gate-outcome.types.ts(36,20): error TS2345: 'string | undefined' is not assignable to 'string' # outcome.reason after narrowing
test/gate-outcome.types.ts(43,13): error TS2322: Type 'GateOutcome' is not assignable to type 'never' # no exhaustiveness
The four TS2578s are the important ones: "unused @ts-expect-error" is TypeScript saying the old type happily accepted the illegal state — exactly the claim in #167 and the first line of your body. On head the same file typechecks clean. So all four claimed coverage areas (valid assignments, missing required fields, narrowing, exhaustiveness) are live, and the file's own header is honest about how it is checked: bun test ./test/gate-outcome.types.ts collects 0 tests, and the file never claims runtime coverage.
2. "Byte-for-byte identical after type erasure" — literally true
Transpiled src/gates.ts with Bun.Transpiler({loader:"ts",target:"bun"}) on both sides:
raw_identical=true code_identical=true bytes: 15562 15562
Not just comment-normalised — identical bytes. The refactor cannot change a verdict, a response string, or a persistence path.
3. Nothing escapes the union
GateOutcome is returned from exactly one function (src/gates.ts:236), produced by exactly seven literals (:254 :259 :294 :315 :651 :655 :692), and I found zero as GateOutcome casts in src/, test/ or e2e/. Each producer already supplies what its variant now owes (:692 { action: "promoted", ts, chain }, :655 { action: "collapsed", chain }, every blocked carries a reason), so this is tightening a guarantee the runtime was already keeping rather than coercing callers — which is why no behavior had to change.
4. The #158 interaction is clean (the risk worth naming)
#158 grew the same interface with evidenceError and collapsedWith, so a naive rebase could have orphaned those fields outside the union. I merged 20bb57e + a3a17ae myself: git places both fields — with their #155/#158 rationale comments — inside GateOutcomeFields, so all three variants still carry them, and auditCaveat(outcome: GateOutcome) keeps reading evidenceError before narrowing. Full gate on that merge tree: tsc clean, biome exit 0, unit 644 pass / 12 skip / 0 fail, e2e 11 pass / 0 fail, exit 0. That matches your "temporary merge with #158 at 0a6999e" note, which I can therefore confirm rather than take on faith.
Lint does not regress either: Checked 129 → 130 files, Found 397 warnings / 116 infos on both sides, so the new contract file contributes zero diagnostics.
Non-blocking nits
N1 — the fallbacks #167 pointed at are still there, and they are now load-bearing in a different way. src/index.ts:639 if (outcome.action === "promoted" && outcome.ts && target) and src/init.ts:267 stampOutcome.action === "promoted" && prior && stampOutcome.ts. After narrowing, outcome.ts is string, so && outcome.ts is a truthiness test that TypeScript can neither prove redundant nor remove — because ts: "" is still assignable to ts: string. The guard is therefore still the only thing between an empty row id and a wrong superseded_by. If you ever want to delete those && outcome.ts clauses, the type has to say "non-empty" (a template-literal type or a tiny rowId() assertion at the producer), and that is a separate change with its own proof — your body is right to bundle no runtime cleanup, and #167 explicitly deferred it.
N2 — collapsed still does not distinguish "with a survivor" from "without". collapsedWith sits in the shared field set, so { action: "collapsed" } is valid with or without it, and the memory_update completion path (#175's survivor lookup) is exactly the consumer that has to re-check it at runtime. That is inherited from the shape #158 chose, not something this PR makes worse — flagging it only so the choice is on the record here rather than surprising the next reader of the union.
Approved: five lines of type-level law, one file of compile-time teeth, zero emitted-byte delta.
|
🎉 This PR is included in version 0.14.19 🎉 The release is available on: Your semantic-release bot 📦🚀 |
GateOutcomecurrently accepts a promoted result without a timestamp and a blocked result without a reason. Express its three verdicts as a discriminated union so producers must supply those fields and callers get required strings after narrowing onaction.Shared optional fields remain accessible to existing diagnostic callers. This leaves runtime branches, response wording, and persistence unchanged; the generated gate JavaScript is byte-for-byte identical after type erasure. Adds compile-time contracts covering valid outcomes, missing required fields, narrowing, and exhaustiveness. These contracts fail against the old interface and pass with this change.
Closes #167.
Validation (Windows, Bun 1.4.4):
bun run gate: typecheck and lint pass (existing warnings remain); 620 unit tests pass, 12 skipped; 11 real-host end-to-end tests pass.0a6999e: no conflicts;evidenceErrorandcollapsedWithpreserved; typecheck and 72 update/audit/batch/epoch regressions pass. Generated gate JavaScript also matches fix(update): metadata fidelity and truthful partial results (#154 #155) #158 exactly. The trial merge was aborted; this PR contains only the two-file refactor and is based on main.No runtime cleanup is bundled with the type change. #169 and #170 remain separate.
This is an optional proposal. Please feel free to close it without merging if you prefer to keep the current interface or this refactor does not fit your plans for the project.