Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/capture-recovery/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema } from "effect"
import { ExtractionAttempt } from "@journal/domain"
import { AttemptNumber } from "@journal/domain"

import { FailureReason } from "./state.js"

Expand Down Expand Up @@ -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,
})

Expand Down
6 changes: 5 additions & 1 deletion packages/capture-recovery/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions packages/capture-recovery/src/state.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]
Expand All @@ -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),
Expand Down
8 changes: 6 additions & 2 deletions packages/capture-recovery/test/recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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[] =>
Expand Down Expand Up @@ -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", () => {
Expand Down
Loading