Skip to content

feat!: catalogue stored event types and wait for the code that reads them - #383

Merged
benatespina merged 12 commits into
mainfrom
feat/stored-event-types
Dec 4, 2026
Merged

benatespina merged 12 commits into
mainfrom
feat/stored-event-types

Conversation

@benatespina

@benatespina benatespina commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

The store keeps a catalogue of the kinds of event it holds, bounda_event_types (aggregate_type, type, schema_version), and every reader refuses an event newer code wrote with the transient SchemaAheadError (SCHEMA_AHEAD) instead of reading it in a shape it was not written in. A deploy that changes an event's schema rolls forward across instances: the older code waits for the newer one.

Rows → tests

Paths under packages/core/src/ unless named. Contracts run on memory, node:sqlite, libSQL, PostgreSQL and the Durable Object.

  • E1 — every append and reseal records its kind. adapter/testing/event-store.contract.ts: empty store; one row per kind; more kinds than a statement carries; a failed append records none; a transaction's kinds on commit, none on rollback; "shows a transaction the kinds its appends and reseals write before it commits, and nobody once it rolls back"; "commits two transactions that first write the same new kinds in opposite orders, recording each once"; two writers of one new kind; a reseal records the version it gives; postgresql/src/postgresql.test.ts "records a transaction's new kinds once its work is done, so a reseal that takes no append lock never deadlocks with it". storage-schema.contract.ts (migration fills the catalogue), earlier-rows.ts (0.2.0 / 0.3.0 databases), restore.contract.ts, adapter/sqlite/restore-copy.test.ts, adapter/sql/event-types.test.ts.
  • E2 — the start refuses a store holding what this code cannot read. kernel/aggregate/stored-types.test.ts "refuses the start, naming in code-point order every kind newer code wrote…", "reads in no version a type of the runtime's aggregate or of a process's lifecycle this core does not know"; kernel/schema-ahead-app.test.ts "refuses to start once the catalogue holds a kind of event newer code wrote".
  • E3 — the runtime's own events. stored-types.test.ts "knows every event a module of the runtime's aggregate names…", "reads a scheduled command's failure, on any stream, in the runtime's version"; kernel/system-events.test.ts; modules/validate.test.ts and cli/src/generate/discover.test.ts refuse an app event named ScheduledCommandFailed.
  • E4 — every read refuses an event newer code wrote. kernel/aggregate/upcasting.test.ts ("rejects every read…", "…even when the start saw its kind", "…a kind this code has no module for, written after the start…", "rejects with a configuration error…"); contracts/errors.test.ts.
  • E5 — subscribers stop before it, each on its own. kernel/dispatch/delivery.test.ts; schema-ahead-app.test.ts "holds every subscriber…".
  • E6 — strict reads, and the dispatcher's read up to it. upcasting.test.ts "rejects every read…", "reads the readable events before the first one it cannot read…".
  • E7 — the in-transaction readers. kernel/events/guarded-storage.test.ts; kernel/personal-data/schema-ahead-readers.test.ts; kernel/recipients/recipients-app.test.ts. Each of the ten readers inside a transaction is pinned at an event newer code wrote meanwhile, with a racedAdapter that appends it between the read and the commit: forget ("refuses, writing nothing, a forget whose intention's id another instance committed meanwhile in a version a newer Bounda wrote"), tenant forget, redaction, reseal, restriction and lift, backfill, presumed recipients (forget, export, notices), lost disclosures and handing data out.
  • E8 — nothing rewrites it in place. kernel/personal-data/backfill-walk.test.ts, incl. "leaves an event newer code rewrote while this pass read it, walking no further than it" (two walkers interleaved: older code reads, newer code upcasts, the older write is left); schema-ahead-readers.test.ts "refuses, rewriting nothing, a reseal, a redaction and an export…". EventStore.reseal writes an event given from only while the store still holds it: event-store.contract.ts "writes an event given what it was read as only while it still holds that, in that schema version…", "leaves, inside a transaction, an event written again since it was read", "leaves an event a transaction holds written again from what both read, once it commits"; adapter/staged-event-store.test.ts; postgresql.test.ts "…once the reseal waiting on its row sees it commit" and "locks the events a reseal checks in position order…"; reseal-batches.test.ts "leaves as it is now an event written again since the batch was read, named or not…".
  • E9 — reactions and scheduled work wait, never given up. kernel/shared/reaction-attempt.test.ts "holds a reaction that met an event newer code wrote with the dispatcher's base delay, counting no attempt…", "gives the first failure of its own after an event newer code wrote every attempt its retry allows"; adapter/testing/inbox-ledger.contract.ts (release); schema-ahead-app.test.ts; kernel/recipients/notices.test.ts; kernel/process/deadlines.test.ts "defer a deadline that meets an event newer code wrote by the dispatcher's base delay, counting no attempt…"; kernel/policy/delayed.test.ts "defer a run whose event newer code wrote, or whose command meets such an event…"; kernel/dead-letters/restricted.test.ts "defers the retry whose letter meets an event newer code wrote…"; kernel/unit-of-work/unit-of-work.test.ts "throws what newer code wrote, met by a write at commit, as the work itself would have…".
  • E10 — a command fails with a 503. schema-ahead-app.test.ts "rejects a command on its stream with SCHEMA_AHEAD…"; react-router/src/failure.test.ts; cloudflare/test/worker.test.ts; cloudflare/test/outcome.test.ts "throws an event newer code wrote again as the SchemaAheadError it was, with every field".
  • E11 — rebuilds and exports stop whole. schema-ahead-app.test.ts (rebuild pauses and resumes; a tenant's export hands out no end); person export in schema-ahead-readers.test.ts; cli/src/forgotten.test.ts.
  • Categories newer code declares. The store records every personal category a key was given in (bounda_personal_categories), and a start reads it: older code never forgets a category only newer code declares. kernel/personal-data/category-ahead.test.ts (forget as a whole, a category the store held at start, a reaction that waits, retention that holds, a forgetting's kept keys, a tenant's forget); personal-key-store.contract.ts "records each category a key is added in once…"; storage-transaction.contract.ts "records the category of the keys it adds with the rest, or not at all", "keeps the category a write outside it recorded meanwhile when it rolls back"; kernel/personal-data/forget.test.ts "refuses a forget by category or as a whole that would take a key of a category only newer code declares…"; retention.test.ts "holds a person whose forget meets newer code…", "never lists a kept key of a category only newer code declares…"; reconciliation.test.ts "waits for newer code it met…", "refuses, writing nothing, to forget again a category only newer code declares"; ledger-prune.test.ts "keeps a forgetting's intention that names a category only newer code declares…"; storage-schema.contract.ts (migration fills it); cloudflare/test/outcome.test.ts "throws keys of a category newer code declares again as the CategoryAheadError it was".
  • Types: core/test-types/schema-evolution.test-d.ts.

Checks

pnpm check, pnpm test:types and pnpm docs:build pass locally with Docker. Stryker on the changed lines of every touched package (core, cli, postgresql, react-router, cloudflare): every mutant killed but six equivalents: stored-types.ts:105 (a versions list no case reads); the two halves of upcasting.ts:93, a type guard only a kind below version 1 or an upcast with no steps reaches; sqlite/event-store.ts:211 and postgresql/src/event-store.ts:252, which would read or lock the row of an event given no from too, as its write does anyway; reseal-batches.ts:318, a schemaVersion: undefined every store reads as none.

Breaking

EventStore gains types(), InboxLedger gains release() and PersonalKeyStore gains recordedCategories(): an adapter of your own implements all three. EventStore.reseal takes from per event and resolves to the positions it left. An event named ScheduledCommandFailed is refused. New public CategoryAheadError (SCHEMA_AHEAD). minor changesets on every package changed.

…them

Every append records the kinds of its events (aggregate type, type, schema
version) in bounda_event_types, in its own transaction; a storage migration
fills it from the events already stored. A start refuses a store holding an
event newer code wrote, and every read refuses one written after the start
with the transient SchemaAheadError (SCHEMA_AHEAD), never reading it in a
shape it was not written in.

Each subscriber reads up to the first event it cannot read and stops there
on its own; reactions, recipient notices and scheduled commands wait with
back-off and are never dead-lettered for it; a rebuild pauses; the hosts
answer 503.
- Reactions and scheduled work that meet newer code wait the dispatcher's
  base delay, spend no attempt and release their claim (EV1-a).
- A transaction records the kinds it writes once, sorted, before it
  commits, so two writers of new kinds never deadlock (EV1-b), and sees
  them in types() before it commits (EV1-e).
- The catalogue tells the runtime's ScheduledCommandFailed apart by
  aggregate type, and an app event of that name is refused (EV1-c).
- The store records every personal category a key was given in; a start
  reads it, and older code never forgets a category only newer code
  declares: forgets refuse with CategoryAheadError (SCHEMA_AHEAD),
  reactions and retention wait (EV1-f).
- A commit whose writes meet newer code throws that as the work would,
  so a reaction's forget at commit waits instead of leaving its claim.
SQLite runs one write transaction at a time, so holding a transaction's
kinds back until its end bought nothing there; PostgreSQL keeps it.
A reseal that takes no append lock deadlocked on PostgreSQL with a
transaction recording its kinds as it appended; the new case fails
without the buffer.
…the forget ledger

The reconciliation refuses to forget such a category again with CategoryAheadError and waits, and the pruning keeps an intention that names one, since only the code that declares it can tell whether the forgetting holds.
@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@bounda-dev/cli

npm i https://pkg.pr.new/@bounda-dev/cli@383

@bounda-dev/cloudflare

npm i https://pkg.pr.new/@bounda-dev/cloudflare@383

@bounda-dev/core

npm i https://pkg.pr.new/@bounda-dev/core@383

create-bounda

npm i https://pkg.pr.new/create-bounda@383

@bounda-dev/postgresql

npm i https://pkg.pr.new/@bounda-dev/postgresql@383

@bounda-dev/react-router

npm i https://pkg.pr.new/@bounda-dev/react-router@383

@bounda-dev/sqlite

npm i https://pkg.pr.new/@bounda-dev/sqlite@383

commit: c26c90c

EventStore.reseal takes, for each event, what the caller read it as, writes it only while the store still holds that, and resolves to the positions it left. The backfill passes it on both its paths, the events naming nobody now in a transaction of their own, and reads an event it left again on its next pass instead of stamping an older shape with a newer version. PostgreSQL locks each row it checks; the memory store's own reseal waits for a transaction's commit.
@benatespina
benatespina enabled auto-merge December 3, 2026 23:21
@benatespina
benatespina added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit 84d5258 Dec 4, 2026
31 checks passed
@benatespina
benatespina deleted the feat/stored-event-types branch December 4, 2026 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant