From 48e4b81c11bfd2f3da750381753748f7a22ae57f Mon Sep 17 00:00:00 2001 From: Obvious Date: Thu, 17 Sep 2026 20:23:19 +0000 Subject: [PATCH] fix(capture-recovery): type attempt counters as AttemptNumber, brand captureId at ingress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #15's recovery schemas referenced domain's `ExtractionAttempt` (the attempt RECORD, per contract v0.3 adaptation A5) where the numeric counter was intended — the record type that v0.3 renamed AWAY from that name. The fixtures, reducer arithmetic (attempt + 1), comparisons, and expectations all treat attempt as a number, so the wrong field type failed three gates on master: typecheck (TS2322/TS2365 across state/replay/tests), runtime test (fail-closed decodeStoredState rejected numeric attempts — "Expected object"), and build. - state.ts: CaptureRecoveryState.attempt and DiscardReceipt.attempt → AttemptNumber - events.ts: SubmitRejected.attempt and ExtractionResultArrived.attempt → AttemptNumber - reducer.ts: createCapture brands captureId at ingress via Schema.decodeSync(CaptureId) (canonical identifier space, fail-closed on empty) so the branded state field is honestly constructed - test: brand the two captureId expectations the same way No behavior change: every fixture, reducer branch, and expectation already used numeric semantics; this aligns the schemas with the code's actual contract and unblocks the shared typecheck/test/build gates for all PRs. Co-authored-by: Gilbert Polanco --- packages/capture-recovery/src/events.ts | 6 +++--- packages/capture-recovery/src/reducer.ts | 6 +++++- packages/capture-recovery/src/state.ts | 8 ++++---- packages/capture-recovery/test/recovery.test.ts | 8 ++++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/capture-recovery/src/events.ts b/packages/capture-recovery/src/events.ts index ceb161a..ee6b8ac 100644 --- a/packages/capture-recovery/src/events.ts +++ b/packages/capture-recovery/src/events.ts @@ -1,5 +1,5 @@ import { Schema } from "effect" -import { ExtractionAttempt } from "@journal/domain" +import { AttemptNumber } from "@journal/domain" import { FailureReason } from "./state.js" @@ -47,14 +47,14 @@ export const SubmitAccepted = Schema.TaggedStruct("SubmitAccepted", { export const SubmitRejected = Schema.TaggedStruct("SubmitRejected", { at: Schema.Number, - attempt: ExtractionAttempt, + attempt: AttemptNumber, reason: FailureReason, }) /** Decoded extraction result for (captureId, attempt) — latest-attempt-only. */ export const ExtractionResultArrived = Schema.TaggedStruct("ExtractionResultArrived", { at: Schema.Number, - attempt: ExtractionAttempt, + attempt: AttemptNumber, outcome: ExtractionOutcome, }) diff --git a/packages/capture-recovery/src/reducer.ts b/packages/capture-recovery/src/reducer.ts index 5fc9117..995c40c 100644 --- a/packages/capture-recovery/src/reducer.ts +++ b/packages/capture-recovery/src/reducer.ts @@ -1,3 +1,6 @@ +import { Schema } from "effect" +import { CaptureId } from "@journal/domain" + import type { CaptureEvent } from "./events.js" import type { CaptureRecovery, DiscardReceipt } from "./state.js" @@ -31,7 +34,8 @@ export const createCapture = (input: { authorId: string at: number }): CaptureRecovery => ({ - captureId: input.captureId, + // Canonical identifier space: brand at ingress (fail-closed on empty). + captureId: Schema.decodeSync(CaptureId)(input.captureId), authorId: input.authorId, rawTranscript: "", phase: "drafting", diff --git a/packages/capture-recovery/src/state.ts b/packages/capture-recovery/src/state.ts index 773be71..164cca4 100644 --- a/packages/capture-recovery/src/state.ts +++ b/packages/capture-recovery/src/state.ts @@ -1,5 +1,5 @@ import { Schema } from "effect" -import { CaptureId, ExtractionAttempt, ExtractionStatus } from "@journal/domain" +import { AttemptNumber, CaptureId, ExtractionStatus } from "@journal/domain" /** * Interrupted-capture recovery state (proposal toward contract v0.3 — bounded @@ -31,7 +31,7 @@ export type SubmissionId = typeof SubmissionId["Type"] export const DiscardReceipt = Schema.Struct({ at: Schema.Number, transcriptLength: Schema.Int, - attempt: ExtractionAttempt, + attempt: AttemptNumber, submissionId: Schema.optionalKey(SubmissionId), }) export type DiscardReceipt = typeof DiscardReceipt["Type"] @@ -52,8 +52,8 @@ export const CaptureRecoveryState = Schema.Struct({ phase: CapturePhase, /** Stable idempotency key: assigned at first submit, reused on retries. */ submissionId: Schema.optionalKey(SubmissionId), - /** Extraction attempt counter (canonical envelope), monotonic per capture. */ - attempt: ExtractionAttempt, + /** Extraction attempt counter (canonical `AttemptNumber`), monotonic per capture. */ + attempt: AttemptNumber, /** Mirror of Entry.extractionStatus for pending captures. */ extractionStatus: Schema.optionalKey(ExtractionStatus), lastFailure: Schema.optionalKey(LastFailure), diff --git a/packages/capture-recovery/test/recovery.test.ts b/packages/capture-recovery/test/recovery.test.ts index fd8eeea..1136529 100644 --- a/packages/capture-recovery/test/recovery.test.ts +++ b/packages/capture-recovery/test/recovery.test.ts @@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test" import { readdirSync, readFileSync } from "node:fs" import { join } from "node:path" import { Schema } from "effect" +import { CaptureId } from "@journal/domain" import { CaptureEvent, @@ -19,6 +20,9 @@ import { type FixtureRunResult, } from "../src/index.js" +/** Canonical identifier space: brand fixture/test literals the same way createCapture does. */ +const asCaptureId = Schema.decodeSync(CaptureId) + const fixturesDir = join(import.meta.dir, "..", "fixtures") const loadRuns = (): FixtureRunResult[] => @@ -267,8 +271,8 @@ describe("storage contract", () => { dead = reduce(dead, { _tag: "DiscardRequested", at: 4_000 }) storage.save(live) storage.save(dead) - expect(storage.listUnresolved().map((s) => s.captureId)).toEqual(["cap-live"]) - expect(storage.listReceipts().map((s) => s.captureId)).toEqual(["cap-dead"]) + expect(storage.listUnresolved().map((s) => s.captureId)).toEqual([asCaptureId("cap-live")]) + expect(storage.listReceipts().map((s) => s.captureId)).toEqual([asCaptureId("cap-dead")]) }) test("fail-closed read: a corrupt stored record cannot enter the machine", () => {