Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/capture-recovery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ loss, and duplicate submission** — prototyped one-handed for mom, dad, and
invited caregivers.

Bounded direct-worker extension of the canonical contract
(`art_I2TCG08V` v0.2). It **imports** `CaptureId`, `ExtractionAttempt`, and
(`art_I2TCG08V` v0.2). It **imports** `CaptureId`, `AttemptNumber`, and
`ExtractionStatus` from `@journal/domain` and **modifies nothing** in the
existing domain, entry, event, extraction, ui, security, or evaluation
packages. Proposal target: fold into contract v0.3 if accepted.
Expand Down
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
3 changes: 2 additions & 1 deletion packages/capture-recovery/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CaptureId } from "@journal/domain"
import type { CaptureEvent } from "./events.js"
import type { CaptureRecovery, DiscardReceipt } from "./state.js"

Expand Down Expand Up @@ -27,7 +28,7 @@ import type { CaptureRecovery, DiscardReceipt } from "./state.js"
*/

export const createCapture = (input: {
captureId: string
captureId: CaptureId
authorId: string
at: number
}): CaptureRecovery => ({
Expand Down
6 changes: 5 additions & 1 deletion packages/capture-recovery/src/replay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Schema } from "effect"
import { CaptureId } from "@journal/domain"

import { bannerFor, RecoveryBanner } from "./banner.js"
import { CaptureEvent } from "./events.js"
Expand Down Expand Up @@ -98,7 +99,10 @@ const check = (name: string, expected: unknown, actual: unknown): Check => ({
* persisted); scenario expectations are returned as checks, not thrown.
*/
export const runFixture = (fixture: Fixture): FixtureRunResult => {
let state: CaptureRecovery = createCapture({ ...fixture.capture })
let state: CaptureRecovery = createCapture({
...fixture.capture,
captureId: Schema.decodeUnknownSync(CaptureId)(fixture.capture.captureId),
})
const storage = new InMemoryCaptureStorage()
storage.save(state)

Expand Down
6 changes: 3 additions & 3 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 @@ -53,7 +53,7 @@ export const CaptureRecoveryState = Schema.Struct({
/** Stable idempotency key: assigned at first submit, reused on retries. */
submissionId: Schema.optionalKey(SubmissionId),
/** Extraction attempt counter (canonical envelope), monotonic per capture. */
attempt: ExtractionAttempt,
attempt: AttemptNumber,
/** Mirror of Entry.extractionStatus for pending captures. */
extractionStatus: Schema.optionalKey(ExtractionStatus),
lastFailure: Schema.optionalKey(LastFailure),
Expand Down
15 changes: 9 additions & 6 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,8 @@ import {
type FixtureRunResult,
} from "../src/index.js"

const asCaptureId = Schema.decodeUnknownSync(CaptureId)

const fixturesDir = join(import.meta.dir, "..", "fixtures")

const loadRuns = (): FixtureRunResult[] =>
Expand Down Expand Up @@ -124,7 +127,7 @@ describe("recovery fixture replay", () => {

describe("reducer invariants", () => {
const base: CaptureRecovery = createCapture({
captureId: "cap-invariant",
captureId: asCaptureId("cap-invariant"),
authorId: "caregiver-1",
at: 1_000,
})
Expand Down Expand Up @@ -205,7 +208,7 @@ describe("reducer invariants", () => {
})

describe("truthful UI projection", () => {
const base = createCapture({ captureId: "cap-ui", authorId: "caregiver-1", at: 1_000 })
const base = createCapture({ captureId: asCaptureId("cap-ui"), authorId: "caregiver-1", at: 1_000 })

test("pending never claims saved; failed copy names the problem and keeps the raw safe", () => {
let state = base
Expand Down Expand Up @@ -261,14 +264,14 @@ describe("storage contract", () => {

test("unresolved captures and discard receipts are listed separately", () => {
const storage = new InMemoryCaptureStorage()
const live = createCapture({ captureId: "cap-live", authorId: "caregiver-1", at: 1_000 })
let dead = createCapture({ captureId: "cap-dead", authorId: "caregiver-1", at: 2_000 })
const live = createCapture({ captureId: asCaptureId("cap-live"), authorId: "caregiver-1", at: 1_000 })
let dead = createCapture({ captureId: asCaptureId("cap-dead"), authorId: "caregiver-1", at: 2_000 })
dead = reduce(dead, { _tag: "TranscriptChanged", at: 3_000, text: "private note" })
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