On this page:
commerce-storage?
storage-project-next!
projection-result
issuance-outbox-item
storage-order-ref
storage-outbox-ref
storage-close
5.1 SQLite backend
open-sqlite-commerce-storage
storage-readiness
5.2 Issuance lease state machine
storage-claim-issuance-work!
storage-complete-issuance-work!
storage-release-issuance-work!
storage-entitlement-ref
storage-lineage-ref
storage-entitlement-revision-ref
storage-latest-entitlement-revision
storage-latest-entitlement-revision-and-unresolved-refund
entitlement-lineage
entitlement-revision
issuance-completion
issued-credential-record
claim-fulfillment
5.3 One-time claim delivery
storage-claim-credential
storage-mark-claim-notified
storage-retire-expired-claims!

5 Durable projection and issuance outbox🔗ℹ

 (require commerce/storage) package: commerce

Server processes explicitly require commerce/storage. It is not re-exported from the default facade. Journal acknowledgement and projection are separate durability phases. Projection runs outside the provider request:

procedure

(commerce-storage? value) → boolean?

  value : any/c
Recognizes one opened durable Commerce storage. All journal, projection, outbox, lineage, and claim operations accept only this type.

procedure

(storage-project-next! storage 
  #:processed-at processed-at) 
 → (or/c projection-result? #f)
  storage : commerce-storage?
  processed-at : exact-nonnegative-integer?
Selects the lowest unprocessed journal sequence and atomically writes its order projection, any newly emitted issuance-outbox row, and its processing marker. It returns #f when no sequence is pending. A successful result has disposition 'projected; an order identity conflict has disposition 'blocked, failure code 'order-conflict, no new outbox items, and leaves the previous order unchanged.

struct

(struct projection-result (disposition
    journal-sequence
    order
    outbox
    failure-code))
  disposition : (or/c 'projected 'blocked)
  journal-sequence : exact-nonnegative-integer?
  order : (or/c order-projection? #f)
  outbox : (listof issuance-outbox-item?)
  failure-code : (or/c symbol? #f)
Exposes the projection disposition, journal sequence, resulting order, new outbox items, and optional failure code.

struct

(struct issuance-outbox-item (id
    idempotency-key
    source-sequence
    action
    provider
    order-id
    offer-id
    subject-id
    occurred-at
    status
    attempts
    available-at
    lease-owner
    lease-expires-at
    last-error-code
    created-at
    updated-at
    completed-at))
  id : exact-nonnegative-integer?
  idempotency-key : string?
  source-sequence : exact-nonnegative-integer?
  action : symbol?
  provider : string?
  order-id : string?
  offer-id : string?
  subject-id : string?
  occurred-at : exact-nonnegative-integer?
  status : symbol?
  attempts : exact-nonnegative-integer?
  available-at : exact-nonnegative-integer?
  lease-owner : (or/c string? #f)
  lease-expires-at : (or/c exact-nonnegative-integer? #f)
  last-error-code : (or/c symbol? #f)
  created-at : exact-nonnegative-integer?
  updated-at : exact-nonnegative-integer?
  completed-at : (or/c exact-nonnegative-integer? #f)
One durable issuance work row. New rows are 'pending with zero attempts and no lease.

procedure

(storage-order-ref storage    
  provider    
  order-id) → (or/c order-projection? #f)
  storage : commerce-storage?
  provider : string?
  order-id : string?
Returns the durable order projection.

procedure

(storage-outbox-ref storage 
  idempotency-key) 
 → (or/c issuance-outbox-item? #f)
  storage : commerce-storage?
  idempotency-key : string?
Returns one outbox row by its stable idempotency key.

procedure

(storage-close storage) → void?

  storage : commerce-storage?
Closes the storage during service shutdown.

5.1 SQLite backend🔗ℹ

 (require commerce/storage/sqlite) package: commerce

procedure

(open-sqlite-commerce-storage database-path)

 → commerce-storage?
  database-path : path-string?
Opens or creates a Commerce-owned SQLite database. Schema v9 uses WAL mode, full commit synchronization, foreign keys, and database triggers that reject updates or deletes of accepted event and entitlement-lineage evidence. Earlier schema databases migrate in place without rewriting existing rows. The lineage tables retain stable entitlement roots and contiguous revisions with exact unsigned Permit payload bytes; no signer or signed-credential storage is exposed.

procedure

(storage-readiness storage #:now now) → health-status?

  storage : commerce-storage?
  now : exact-nonnegative-integer?
The readiness snapshot behind commerce/health; also re-exported there.

5.2 Issuance lease state machine🔗ℹ

The issuance worker protocol is owner-safe: a row is claimed, signed work happens outside the claim transaction, and completion or release re-checks the lease owner against the database.

procedure

(storage-claim-issuance-work! storage 
  #:owner owner 
  #:lease-seconds lease-seconds 
  #:now now) 
 → (or/c issuance-outbox-item? #f)
  storage : commerce-storage?
  owner : string?
  lease-seconds : exact-positive-integer?
  now : exact-nonnegative-integer?
Claims the lowest due outbox row in one immediate transaction: status 'processing, attempts incremented by one, and an opaque exclusive lease bound to the owner. A row is due when it is 'pending and available_at <= now, or when its lease has expired. Returns #f when nothing is due.

procedure

(storage-complete-issuance-work! 
  storage 
  item 
  #:issuer issuer 
  #:subject-type subject-type 
  #:entitlement-id entitlement-id 
  #:credential-id credential-id 
  #:policy-id policy-id 
  #:policy-version policy-version 
  #:payload-bytes payload-bytes 
  #:issued-at issued-at 
  #:recorded-at recorded-at 
  [#:signed-credential-bytes envelope-bytes 
  #:key-id key-id 
  #:claim-token claim-token 
  #:claim-expires-at claim-expires-at]) 
 → issuance-completion?
  storage : commerce-storage?
  item : issuance-outbox-item?
  issuer : string?
  subject-type : string?
  entitlement-id : string?
  credential-id : string?
  policy-id : string?
  policy-version : exact-positive-integer?
  payload-bytes : bytes?
  issued-at : exact-nonnegative-integer?
  recorded-at : exact-nonnegative-integer?
  envelope-bytes : (or/c bytes? #f) = #f
  key-id : (or/c string? #f) = #f
  claim-token : (or/c string? #f) = #f
  claim-expires-at : (or/c exact-nonnegative-integer? #f) = #f
Appends the lineage root together with revision zero, or exactly one successor revision, and completes the outbox row inside the same short transaction. The writer enforces the ADR-0008 identity rules as typed failures before any insert: a second grant over an existing lineage, a commercial identity bound to a different entitlement, a withdrawal without a lineage, and a credential id already bound to another revision raise exn:fail:commerce codes 'entitlement-lineage-conflict or 'entitlement-lineage-required and write nothing. The payload digest is computed from the given bytes, never trusted from the caller. Completion without the active lease raises 'lease-not-held. Repeating a completion that already landed reconciles to the recorded revision; the same outbox id with different payload bytes is a conflict. When the exact signed envelope bytes and key id are supplied, the completion transaction also records the issued credential; a claim token and expiry add its one-time fulfillment claim. Omitting them records issuance evidence without delivery.

procedure

(storage-release-issuance-work! storage 
  item 
  #:error-code error-code 
  #:retry-at retry-at) 
 → issuance-outbox-item?
  storage : commerce-storage?
  item : issuance-outbox-item?
  error-code : symbol?
  retry-at : exact-nonnegative-integer?
Returns transient work to 'pending with the typed error code recorded and a future retry time. Only the lease owner may release; attempts stay exactly as the claim incremented them.

procedure

(storage-entitlement-ref storage 
  entitlement-id) 
 → (or/c entitlement-lineage? #f)
  storage : commerce-storage?
  entitlement-id : string?
Returns the durable lineage for one entitlement id.

procedure

(storage-lineage-ref storage 
  #:issuer issuer 
  #:subject-type subject-type 
  #:subject-id subject-id 
  #:offer-id offer-id) 
 → (or/c entitlement-lineage? #f)
  storage : commerce-storage?
  issuer : string?
  subject-type : string?
  subject-id : string?
  offer-id : string?
Resolves the lineage bound to one commercial identity.

procedure

(storage-entitlement-revision-ref storage 
  entitlement-id 
  revision) 
 → (or/c entitlement-revision? #f)
  storage : commerce-storage?
  entitlement-id : string?
  revision : exact-nonnegative-integer?
Returns one recorded revision. Revision payload bytes are the exact unsigned snapshot bytes, never signed credential bytes.

procedure

(storage-latest-entitlement-revision storage 
  entitlement-id) 
 → (or/c entitlement-revision? #f)
  storage : commerce-storage?
  entitlement-id : string?
Returns the newest recorded revision of one lineage.

procedure

(storage-latest-entitlement-revision-and-unresolved-refund 
  storage 
  entitlement-id 
  subject-id 
  offer-id) 
 → 
(or/c entitlement-revision? #f) boolean?
  storage : commerce-storage?
  entitlement-id : string?
  subject-id : string?
  offer-id : string?
Returns the newest recorded revision and the unresolved-refund state as two facts observed at one instant: one read transaction backs both values. Hosted proof verification denies on freshness and refund state together, so reading them separately could straddle the transaction that records a withdrawal and serve a withdrawn grant.

struct

(struct entitlement-lineage (entitlement-id
    issuer
    subject-type
    subject-id
    offer-id
    created-at))
  entitlement-id : string?
  issuer : string?
  subject-type : string?
  subject-id : string?
  offer-id : string?
  created-at : exact-nonnegative-integer?
The stable entitlement root bound to one commercial identity.

struct

(struct entitlement-revision (entitlement-id
    revision
    previous-revision
    credential-id
    issuance-outbox-id
    action
    policy-id
    policy-version
    payload-bytes
    payload-digest
    issued-at
    recorded-at))
  entitlement-id : string?
  revision : exact-nonnegative-integer?
  previous-revision : (or/c exact-nonnegative-integer? #f)
  credential-id : string?
  issuance-outbox-id : exact-nonnegative-integer?
  action : symbol?
  policy-id : string?
  policy-version : exact-positive-integer?
  payload-bytes : bytes?
  payload-digest : string?
  issued-at : exact-nonnegative-integer?
  recorded-at : exact-nonnegative-integer?
One recorded lineage revision. payload-bytes is the exact unsigned snapshot bytes, never signed credential bytes.

struct

(struct issuance-completion (outbox revision))

  outbox : issuance-outbox-item?
  revision : exact-nonnegative-integer?
The durable result of one completed issuance work item.

struct

(struct issued-credential-record (id
    issuance-outbox-id
    entitlement-id
    revision
    credential-id
    key-id
    envelope-bytes
    created-at))
  id : exact-nonnegative-integer?
  issuance-outbox-id : exact-nonnegative-integer?
  entitlement-id : string?
  revision : exact-nonnegative-integer?
  credential-id : string?
  key-id : string?
  envelope-bytes : bytes?
  created-at : exact-nonnegative-integer?
The durable signed-credential audit record. envelope-bytes is the exact serialized Permit envelope (key id, payload, signature) as delivered.

struct

(struct claim-fulfillment (claim-id credential claimed-at))

  claim-id : string?
  credential : issued-credential-record?
  claimed-at : exact-nonnegative-integer?
The result of fulfilling one claim token.

5.3 One-time claim delivery🔗ℹ

The issued credential record retains the exact signed envelope bytes; the claim stores only the token’s SHA-256 digest. No transport or HTTP listener is included in this boundary.

procedure

(storage-claim-credential storage    
  #:token token    
  #:now now) → claim-fulfillment?
  storage : commerce-storage?
  token : string?
  now : exact-nonnegative-integer?
Verifies the token, its state, and its fulfillment window, marks the claim claimed, and returns the claim id, the fulfillment time, and the issued-credential-record with the exact signed envelope bytes, which verify as a Permit envelope for the recorded key id. Wrong, reused, and expired tokens raise typed exn:fail:commerce values ('claim-not-found, 'claim-already-claimed, 'claim-expired) and mutate nothing. Fulfillment never creates or advances a lineage revision.

procedure

(storage-mark-claim-notified storage    
  #:token token    
  #:now now) → void?
  storage : commerce-storage?
  token : string?
  now : exact-nonnegative-integer?
Records that a delivery transport handed the token out. The first notification timestamp wins; later retries are idempotent no-ops.

procedure

(storage-retire-expired-claims! storage 
  #:now now) 
 → exact-nonnegative-integer?
  storage : commerce-storage?
  now : exact-nonnegative-integer?
Marks expired unfulfilled claims retired; returns the number retired. Exposed to operators through commerce/retention.