> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# History and lifetime

> How long superseded versions stay readable, how pins keep a version, and how facts are erased by lifetime or by erase.

Three table options decide what stays: `history` (earlier versions),
`lifetime` (when a fact is erased on its own) and `erasable` (each fact is a
unit `ctx.erase` can destroy).

| Option | Takes | Controls |
| - | - | - |
| `history: { days: 365 }` | A duration | How long superseded versions of a fact, and its change log, stay readable |
| `lifetime: { minutes: 30 }` | A fixed duration, in whole microseconds | How long after it is written a fact is erased |
| `erasable: true \| { custody, holdWindow }` | `true` or custody options | Each fact is its own erasure unit; `custody` sets the label of `sys.destructions` and the hold ceiling, `holdWindow` sets the post-closure hold window (up to 30 days) |
| `pin: { field, lifetime, quota }` | A field | A version or branch a fact keeps readable, within retention (see [below](#pins)) |

## History

With `history`, a table's superseded versions stay readable for that long:
`.in(version)` reads them, and `.changes` reads the log of changes over that
time. Each table's history floor advances by its own window alone: nothing
holds it back, so whether or when old history goes reveals nothing about
the branches or pins that exist.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { t, table } from "@bijectionhq/bijection";

export const contracts = table({ customer: t.string, terms: t.string }, { history: { days: 365 } });
```

A deleted fact's history stays readable for the table's `history` too.

A change log page whose requested version is older than the history reports
`gone`: start again from the log without `.since`. See [Reading](https://docs.bijection.com/database/reading.md#page).

## Pins

A pin never keeps a version past history. It is admitted only within
retention, and a branch likewise: its parent version must stay inside every table's
history for the pin's whole life. The installation's retention is the
shortest `history` among the tables a pin or branch reads, system tables
included (they keep 30 days), measured from the pinned version's commit or
the branch's parent version. Asking for exactly the end of retention is admitted, and
every default asks for at most that end; a pin or branch reaching past it is
refused `BeyondHistory`, with the latest end it may take. There are two ways to
pin.

* **A pin field.** `pin: { field, lifetime, quota }` names a `t.version` or
  `t.branch` field. Writing a value into it keeps that version readable, or
  that branch alive, for `lifetime` from the version's commit or the
  branch's parent version (1 to 365 whole days). Without `lifetime`, each pin lasts
  exactly the installation's retention. Deploy refuses a `lifetime` longer
  than retention (`PinBeyondRetention`), naming the table that bounds it;
  nothing is shortened at run time. A fact's own `until` field ends its
  version pin sooner; one past retention is refused `BeyondHistory`. `quota`
  caps the table's live pins (1 to 256, 16 by default): a write past it is
  refused `Limit` (`PinQuota`).
  * A write that leaves the field and the fact's own `until` as they were
    keeps the pin as it stands: it is not admitted again, so a pin that
    has already ended, or a full `quota`, never refuses a write to the
    fact's other fields. Changing the fact's `until` measures its pin from
    the same version again, so it never extends it; one past retention is
    refused `BeyondHistory`.
  * Changing the field ends the old pin and pins the new value;
    clearing it or deleting the fact ends the pin. Erasing the fact, or
    its table's `lifetime` ending it, ends the pin as a delete does,
    unless the pin is recorded below the fact's label (it holds a branch
    with a lower label, or a lower writer pinned it): that pin lasts to its
    own end, since an erasure writes nothing below the fact. `sys.pins`
    lists each fact's pin with its table as `holder`, and `ctx.unpin`
    refuses it.
  * A branch lives to the latest of the end its own `ctx.branch` asked and
    the ends of the facts holding it. Releasing a holder shortens the branch
    back to that, and discards it once that end has passed. Holding a
    branch requires clearing its label, as entering it does.
  * A pin field names a version or a branch only at head: a write in a
    branch that sets one is refused (`PinInBranch`). A write in a branch
    that clears it or deletes its fact ends the pin when the branch
    lands, and a branch it held is shortened or discarded in the same
    landing, the landed branch itself included. A transaction that writes in a branch neither holds nor
    releases it through a pin field (`PinMovesWrittenBranch`).
* **A pin act.** `ctx.pin(version, { until })` keeps one version readable
  until an instant within retention (to its end when `until` is omitted),
  and `ctx.unpin(pin)` stops keeping it. See [Effects](https://docs.bijection.com/functions/effects.md).
  A kept version also keeps its installed Program. See
  [Replay the exact code](https://docs.bijection.com/branches/comparison.md#replay-the-exact-code).

A pin keeps the data cut within retention. Ordinary reads at that cut still
apply current erasure closures, including closures under a legal hold.
[Ranked reads](https://docs.bijection.com/database/reading.md#ranked-reads) also remove the erased fact's
contribution to the remaining scores.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, role, t, table } from "@bijectionhq/bijection";

export const approvals = table(
  { shipment: t.string, version: t.version, approved: t.instant },
  { pin: { field: "version", lifetime: { days: 30 } } },
);

export const approve = mutation({ allow: [role.signedIn], args: { shipment: t.string } }, (ctx, { shipment }) =>
  ctx.insert(approvals, { shipment, version: ctx.frame.version, approved: ctx.frame.now.entry }),
);
```

A read in a version older than a table's history is refused; it never
answers partially. Store what a decision read beside its version, so
the evidence outlives the version.

## Version tokens

`Version` is the type exported from `@bijectionhq/bijection` for an
opaque token (`v1.…`) identifying a committed prefix of history. Read
it from `ctx.frame.version` or a committed mutation result's `version`.
Declare a `t.version` field when you need to store a token, as in the
pin example above. Pass the token back as it is and use
`t.version.equals(a, b)` to compare two tokens. Only the host orders
versions.

## Lifetime

A `lifetime` is scheduled erasure: each fact is erased when its time comes,
however it was written. No function of yours runs the removal.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const sessions = table({ person: t.principal, started: t.instant }, { lifetime: { days: 30 } });
```

## Erasure

An **erasure unit** is what `ctx.erase` destroys, in every version:

* **a fact** of a table declared `erasable: true`; an erasable table mints its
  keys;
* **a family member**: every fact and value under that member's label. See
  [Family tables](https://docs.bijection.com/database/family-tables.md).

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const tickets = table({ subject: t.string, body: t.string }, { erasable: true });

// in a mutation:
const erasure = await ctx.erase(tickets(ticket));
const closure = await ctx.get(erasure);
```

`ctx.erase(unit)` closes the unit in the same commit, returns the closure
`Version`, and has no inverse. There is no erase task; physical destruction
runs asynchronously via the host custody collector once the public post-closure
hold cutoff passes and no standing legal hold remains.

What erasure covers:

* Facts are sealed at rest under their units' keys. Destroying a unit's key
  makes every retained copy that needs it unreadable: history, journals,
  backups, branches and pinned versions.
* A transaction accumulates the units of facts that decide its answers. This
  includes returned facts, counted and totalled contributions, opened ranking
  candidates, the witness of a true presence check, and the next fact when a
  page reports that more results exist. A non-null `.sealed(...fields)`
  projection observes the found fact. Its handles keep their own envelopes
  and units when copied, less what the destination's `survives` declaration covers.
  Empty answers, a zero count, and a total
  declared `erased: true` add no units. Durable outputs also carry dependencies
  received from callees. A statistic survives its contributors through an
  `erased: true` total, or through its destination's `survives` declaration.
  A record carries at most 262,144 distinct leaf units. A derived unit
  has fan-in 512 and depth at most two. It has no key of its own. Hosted
  custody seals one capsule, and opening requires a current share from
  every leaf. A change page can stop early to leave dependency capacity
  for writes.
* A point read of an erased or expired winner returns `null`. Pass
  `{ closed: true }` to `ctx.get` for the `erased` or `expired` closure and
  its erasure fact when the frame admits it. Lists, counts, search and
  ordinary totals use the survivors at every version. A total declared `erased: true`
  retains anonymous contributions. An erased assertion keeps superseding
  earlier assertions. When a closure is finer than the entity's version chain,
  the closed versions' extents appear as gaps in the surviving history. Replaying a call that depended on closed content
  refuses `Erased` or `Expired` at the admitted scope.
* A deleted fact is still its unit while the table's `history` keeps
  versions of it, so `ctx.erase` closes those versions too. It is judged as
  the live fact would be: the frame must clear the last kept version's label.
  A fact the frame cannot see refuses `Absent`, exactly like one that never
  existed.
* A write derived from content produced under a standing hold carries the
  closed unit. Its protected fact is listed through the hold, stays closed to
  ordinary reads, and loses its custody when the last hold lifts and the unit
  is destroyed. Only the destination's `survives` declaration lets such a
  write outlive the unit; no write names survival.
* Erasure, hold placement and hold lift run at Root. Running one in an open
  branch refuses `ErasureInBranch`.
* Copies already disclosed outside Bijection follow their receiver's deletion
  contract.

Closure is recorded as a fact in [`sys.erasures`](https://docs.bijection.com/database/system-tables) at
the unit's label, and verified physical destruction is recorded in
`sys.destructions` at the unit's declared `erasable.custody` label.

## Shared erasure fields

`t.shared()` keeps a string in the app's one shared erasure unit, sealed at
the fact's label: for example an email directory whose entries are erased
together. The field is its table's only field, the table is `erasable`, and
its label is fixed.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, t, table } from "@bijectionhq/bijection";

export const directory = label();

export const emails = table(
  { email: t.shared({ search: true }) },
  { erasable: true, label: directory, unique: [["email"]] },
);

// in a function under directory:
const known = await ctx.has(emails({ email }));
const entry = await ctx.get(emails({ email }));
const hit = await ctx.list(emails.search("textEmail", term).first(1));
```

`search: true` enables the `textEmail` index for single-term searches.

## What a change feed receives on erasure

An ascending change walk returns a cursor scoped to the installation, reader
label, world, table, and walk, including any index range or filter. Pass the
cursor to `.changes.since(cursor)` on that same walk.

When a fact or a dependency closes, a reader who can read the closure gets
`erased` or `expired`, with no before or after image. A reader who cannot read
the closure gets `removed` with the fact ID if that cursor chain delivered
the fact and still holds it. Remove that ID from the local copy. Ordinary
deletions and updates that leave the filter also remove the fact from the
chain. A fresh baseline contains surviving facts and replaces the local copy.

A chain can track up to 65,536 closable IDs. A page that exceeds this limit
still returns its entries and has `cursor: null`. Start a fresh baseline to
resume tracking erasures. Cursors carrying IDs expire after 24 to 48 hours,
at the end of the day following issue. If a page returns `gone: true`, start
a fresh baseline. Custody retires the expired epoch key as part of background
maintenance.

A connector saves a native cursor alongside its page in a checkpoint table
that declares `survives: { reply: true, message }`, so the cursor outlives
the page's transport reply. Name the retained residue in the message: the
cursor positions and its temporary set of up to 65,536 delivered IDs.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.