# Boundaries

burying-beetle decides whether an event for an entity is newer than the state
already stored for it, including a delete. Without it, a late event
overwrites newer state, and a late update after a delete brings the entity
back.

## What it owns

- A pure decision over one event, described by four plain values the caller
  reads out of their own payload: the scope, the entity ID, the version, and
  whether the event is a delete. The package never sees the payload, so it
  works the same for webhooks, queue messages or change-data-capture rows.
  The decision also takes the stored record. The result is apply, stale,
  or deleted.
- A store contract with one atomic method, `advance`: read the record, run
  `decide`, and write `recordFor` on `apply`, as one operation. The Postgres
  store in [postgres.md](postgres.md) does it under an advisory lock inside
  the caller's transaction; a Redis store would be a Lua script.
- `MemoryStore`, for one process and for tests.
- A conformance check that runs the contract against a real store, the way
  Argia's `checkVectorStore` does.

## Agreement with Argia

Argia's `VectorStore` contract (`src/conformance.ts` there) already settles
ordering for its stores, and burying-beetle starts from the same rules:

- A write older than the stored version is dropped.
- A write at the stored version replaces it, so a retry of the same event
  applies.
- A delete is a versioned tombstone. A write older than the delete stays
  dropped; a write at or above the delete's version brings the entity back.

The last rule is the one place the two packages differ. burying-beetle keeps
a deleted entity deleted unless the caller passes `unsafeAllowRecreate`,
because a webhook consumer whose IDs are never reused should not see a
deleted entity come back.

## Versions

The comparison has to run inside the caller's store, atomically, so a version
can only be something every store compares the same way as JavaScript does.
A compare function supplied by the caller cannot run inside a Postgres
`WHERE` clause or a Redis script, so there is none.

A version is a non-negative integer (`number` or `bigint`), or a tuple of
them compared left to right:

- A sequence number or row version: `42`.
- A snowflake ID too large for a `number` (Discord): `1790000000000000000n`.
- A timestamp that can tie, with a tiebreaker: `[timestampMs, sequence]`.
  Postgres compares this as a row value, `(a, b) < (c, d)`.

Strings are refused. Their order depends on collation in Postgres and on
code units in JavaScript, and an ISO date converts to a number in one line.
Every event for one entity has to use the same tuple length; a different
length is a refusal, not a guess.

## The hard part

Recording the version and doing the caller's write have to happen together.
If the version is recorded and the write then fails, the retry is refused as
stale and the event is lost. If the write happens first, two concurrent
events can both pass. The store method is designed to run inside the
caller's own transaction, next to their write. The library does not call the
caller's handler for them.

## Decisions the caller makes

- What the version is. Required. Provider timestamps can tie (GitHub's
  `updated_at` is to the second; Discord IDs order creation, not edits).
- Ties follow Argia and apply. Callers whose versions can tie between
  different events add a tiebreaker to the tuple. Some senders have none to
  give (GitHub's `updated_at` is to the second), and then the later arrival
  of two same-second events wins.
- Tombstones are final and kept forever by default. That is the strict
  choice: a deleted entity never comes back. The cost is storage that grows
  with every delete, and the docs say so. Two options loosen it:
  - `unsafeTombstoneExpiry`, in milliseconds: tombstones are dropped after
    this long, and a late update after that brings the entity back.
  - `unsafeAllowRecreate: true`: a write at or above the delete's version
    brings the entity back, as in Argia. For callers whose IDs are reused.

Every option that loosens a check is named `unsafe*`, so one search finds
every loosened choice in a codebase:

```sh
grep -rnE "unsafe[A-Z]" .
```

This is the convention lacewing uses.

- The scope that keeps one caller's entities apart from another's. Required.

## Out of scope

| Excluded | Where it goes |
| --- | --- |
| Buffering events until a gap closes | Needs contiguous sequence numbers, which the providers we use do not send, and durable storage. Use a queue |
| Verifying where an event came from | fishhook-ant |
| Duplicate deliveries of the same event | mycocepurus-smithii |
| Retries | firefly |
| Event sourcing, replaying history | EventStoreDB or your own log |
| Storage drivers | You pass your own; the store contract is the interface |

## Open questions

- Whether Argia should later depend on burying-beetle for this rule, or the
  two keep separate copies of a small contract.
