Sync / foldkit-durable

JournalShape

interface · packages/durable/src/journal.ts:392

interface Journal<Operation, Snapshot, Principal, OperationEncoded = unknown> {
  readonly store: JournalStore
  readonly load: (key: DocumentId) => Effect.Effect<
    {
      readonly snapshot: Snapshot
      readonly cursor: Cursor
    },
    JournalError
  >
  readonly cursor: (key: DocumentId) => Effect.Effect<Cursor, JournalError>
  readonly epoch: (key: DocumentId) => Effect.Effect<string, JournalError>
  readonly floor: (key: DocumentId) => Effect.Effect<Sequence, JournalError>
  readonly read: (
    key: DocumentId,
    after: Cursor,
    options?: {
      readonly limit?: number
    },
  ) => Effect.Effect<
    ReadonlyArray<Committed<Operation>>,
    InvalidCursorError | CompactedCursorError | JournalError
  >
  readonly replay: (
    key: DocumentId,
    options: {
      readonly after: Cursor
      readonly epoch?: string | undefined
      readonly limit?: number | undefined
    },
  ) => Effect.Effect<ReplayResult<Operation, Snapshot>, JournalError>
  readonly append: (
    key: DocumentId,
    input: OperationEncoded,
    principal: Principal,
  ) => Effect.Effect<AppendResult<Operation>, AppendError>
  readonly appendAll: (
    key: DocumentId,
    inputs: ReadonlyArray<OperationEncoded>,
    principal: Principal,
  ) => Effect.Effect<ReadonlyArray<AppendResult<Operation>>, AppendError>
  readonly compact: (
    key: DocumentId,
    through: Sequence,
  ) => Effect.Effect<void, InvalidCompactionError | JournalError>
  readonly vacuum: () => Effect.Effect<void, JournalError>
  readonly keys: () => Effect.Effect<ReadonlyArray<DocumentId>, JournalError>
  readonly unfinished: () => Effect.Effect<
    ReadonlyArray<EffectRecord>,
    JournalError
  >
  readonly effect: (
    key: string,
  ) => Effect.Effect<Option.Option<EffectRecord>, JournalError>
  readonly runEffect: <Result, E>(
    key: string,
    run: Effect.Effect<Result, E>,
    options?: {
      readonly retryFailed?: boolean | ((record: EffectRecord) => boolean)
    },
  ) => Effect.Effect<Result, E | JournalError | EffectFailedError>
  readonly clearEffect: (key: string) => Effect.Effect<void, JournalError>
  readonly recover: (
    options: RecoveryOptions<Operation>,
  ) => Effect.Effect<
    Cursor,
    InvalidCursorError | CompactedCursorError | JournalError
  >
  readonly reset: (key: DocumentId) => Effect.Effect<void, JournalError>
  readonly subscribe: Stream.Stream<string>
  readonly cursors: Cursors
}

Members

JournalShape.store

property · packages/durable/src/journal.ts:400

readonly store: JournalStore

What this journal's store guarantees of concurrent appends: serializable over SQLite, isolating a transaction (one per append); serializable over D1 isolating one statement, since a read and the append that follows are two round trips (the uniqueness constraints keep the order). A journal over another store says its own.

JournalShape.load

property · packages/durable/src/journal.ts:401

readonly load: (key: DocumentId) => Effect.Effect<
  {
    readonly snapshot: Snapshot
    readonly cursor: Cursor
  },
  JournalError
>

JournalShape.cursor

property · packages/durable/src/journal.ts:409

readonly cursor: (key: DocumentId) => Effect.Effect<Cursor, JournalError>

The sequence of the document's last commit; 0 if none. Unlike load, it decodes nothing, so an exchange can check a client's cursor before it appends anything.

JournalShape.epoch

property · packages/durable/src/journal.ts:415

readonly epoch: (key: DocumentId) => Effect.Effect<string, JournalError>

The identity of the document's history: the same while its operations are kept, new after reset or on a new database file. A client that saw another epoch holds a cursor into history this journal does not have, and must start again from 0.

JournalShape.floor

property · packages/durable/src/journal.ts:417

readonly floor: (key: DocumentId) => Effect.Effect<Sequence, JournalError>

The highest sequence whose payload has been compacted away; 0 if none.

JournalShape.read

property · packages/durable/src/journal.ts:422

readonly read: (
  key: DocumentId,
  after: Cursor,
  options?: {
    readonly limit?: number
  },
) => Effect.Effect<
  ReadonlyArray<Committed<Operation>>,
  InvalidCursorError | CompactedCursorError | JournalError
>

The committed operations after after, in order: at most limit of them when given, so a caller far behind can catch up in bounded steps.

JournalShape.replay

property · packages/durable/src/journal.ts:436

readonly replay: (
  key: DocumentId,
  options: {
    readonly after: Cursor
    readonly epoch?: string | undefined
    readonly limit?: number | undefined
  },
) => Effect.Effect<ReplayResult<Operation, Snapshot>, JournalError>

Replays committed operations after after for a reconnecting client. Returns Operations when the client's cursor is within the retained log and its epoch (if given) matches; otherwise returns Snapshot with the authoritative state and the reason it could not replay incrementally.

JournalShape.append

property · packages/durable/src/journal.ts:451

readonly append: (
  key: DocumentId,
  input: OperationEncoded,
  principal: Principal,
) => Effect.Effect<AppendResult<Operation>, AppendError>

Commits an operation. A repeat of a known opId returns Committed with the stored operation while its payload is retained, and AlreadyCommitted without one once compaction has removed it. A reuse with different data or actor is an IdentityConflictError in either case, proven by a retained payload hash.

JournalShape.appendAll

property · packages/durable/src/journal.ts:457

readonly appendAll: (
  key: DocumentId,
  inputs: ReadonlyArray<OperationEncoded>,
  principal: Principal,
) => Effect.Effect<ReadonlyArray<AppendResult<Operation>>, AppendError>

Commits several operations in order, in one transaction.

JournalShape.compact

property · packages/durable/src/journal.ts:462

readonly compact: (
  key: DocumentId,
  through: Sequence,
) => Effect.Effect<void, InvalidCompactionError | JournalError>

JournalShape.vacuum

property · packages/durable/src/journal.ts:472

readonly vacuum: () => Effect.Effect<void, JournalError>

Rebuilds the database file, returning to the file system the space that compact freed: compaction empties payloads but leaves their pages allocated. It rewrites every table and holds the database while it runs, so it is maintenance, not something to run per commit.

JournalShape.keys

property · packages/durable/src/journal.ts:474

readonly keys: () => Effect.Effect<ReadonlyArray<DocumentId>, JournalError>

The document keys that have a snapshot or a committed operation.

JournalShape.unfinished

property · packages/durable/src/journal.ts:476

readonly unfinished: () => Effect.Effect<
  ReadonlyArray<EffectRecord>,
  JournalError
>

Every recorded effect that is not succeeded, for recovery.

JournalShape.effect

property · packages/durable/src/journal.ts:478

readonly effect: (
  key: string,
) => Effect.Effect<Option.Option<EffectRecord>, JournalError>

The recorded effect for a key, if it has ever run.

JournalShape.runEffect

property · packages/durable/src/journal.ts:490

readonly runEffect: <Result, E>(
  key: string,
  run: Effect.Effect<Result, E>,
  options?: {
    readonly retryFailed?: boolean | ((record: EffectRecord) => boolean)
  },
) => Effect.Effect<Result, E | JournalError | EffectFailedError>

Reuses recorded successes and shares concurrent runs within this journal instance. Pending and failed records are retried when called again, unless retryFailed says otherwise — false for every failed record, or a predicate that decides from the record itself. An external action can succeed before its result is recorded; recovery requires provider idempotency or reconciliation. Use a stable key including the document, operation, and semantic effect identity, and pass that same key to the provider. Results must be JSON-compatible. See the README's effect recovery policy before retrying work with uncertain outcomes.

JournalShape.clearEffect

property · packages/durable/src/journal.ts:496

readonly clearEffect: (key: string) => Effect.Effect<void, JournalError>

Removes an effect record so the next runEffect treats it as new work.

JournalShape.recover

property · packages/durable/src/journal.ts:504

readonly recover: (
  options: RecoveryOptions<Operation>,
) => Effect.Effect<
  Cursor,
  InvalidCursorError | CompactedCursorError | JournalError
>

Runs the effect intents of a document's committed operations after from, reusing recorded successes and stopping before any operation whose intent failed or was skipped. Returns the cursor up to which every intent settled, so a caller can persist it and resume. The journal does not schedule this; the application owns discovery and startup.

JournalShape.reset

property · packages/durable/src/journal.ts:513

readonly reset: (key: DocumentId) => Effect.Effect<void, JournalError>

Drops a document's snapshot and operations, and its epoch, so the next epoch differs. Replica bindings stay: they are who a replica is, not history. Effect records are keyed by the application's own effect identity, not by document, so they are not scoped to a key; clearEffect removes one.

JournalShape.subscribe

property · packages/durable/src/journal.ts:519

readonly subscribe: Stream.Stream<string>

The document keys a commit changed. Subscription is a Stream, so a subscriber never fails or slows a commit; a caller that needs a callback adapts it at the edge.

JournalShape.cursors

property · packages/durable/src/journal.ts:521

readonly cursors: Cursors

Named cursors and parked runs: where work after a commit has reached (Sync's reactions).