> ## 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.

# Labels

> Audiences for facts: labels, families, field labels (sealed fields and presence), label flow and timed lowering.

A label is an audience: who may know a fact. A table's `label` is its facts'
audience. A function's `under` is the label it runs under: a query answers
under it, and a mutation commits under it. A frame may read a fact only if
its clearance covers the fact's label, and its writes carry the data they
read to a label at or above `under`.

## Declare a label

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

export const finance = label();
export const accountant = role({ clear: [finance] });
```

`label()` takes no name: its name is its path in the app tree
(`access.finance`). `label.public` is the label every principal may know; a
table without `label` is public. `a.and(b)` is the audience that clears both
`a` and `b`.

A role's `clear` lists the labels its holders may read and run under. A
grant of the role holds `under` one label, and clears exactly that label for
the frames it admits (see [Roles and grants](https://docs.bijection.com/access/grants.md)). Your code needs
the clearance too: a frame clears a label only when both the caller's grants
and the function's grants clear it.

## One audience per key: families

`label.family(keyType)` declares one label per key. Members are erasable by default.
Use `label.family(keyType, { erasable: false })` for members that should be
retired and have their facts deleted through ordinary writes. The table's
history horizon governs the retention of its deleted facts. Keep any personal
data that must support erasure in an erasable table.

Use `family.of(key)` with
a literal or run-time key.

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

export const tenant = label.family(t.string);
export const clerk = role({ clear: [tenant.all] });
```

```ts bijection/orders.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, query, t, table } from "@bijectionhq/bijection";
import { clerk, tenant } from "./tenancy";

export const orders = table({ number: t.string, total: t.decimal }, { key: ["number"], label: tenant });

export const list = query({ under: tenant, allow: [clerk] }, (ctx) =>
  ctx.list(orders.atMost(50)),
);

export const place = mutation(
  { under: tenant, allow: [clerk], args: { number: t.string, total: t.decimal } },
  (ctx, { number, total }) => ctx.insert(orders(number), { total }),
);
```

A family has no fixed size. Registering a member,
`ctx.register(tenant)`, mints a random, opaque tag
`<family>:<id>` and gives the member capacity of its own (see
[System tables](https://docs.bijection.com/database/system-tables.md)). The app keeps its own table from
business keys to ids, and names the member `tenant.of(id)`.

Founding a member is the `register` right for the family, held by both the
caller and the code ([Found a family member](https://docs.bijection.com/access/grants.md#found-a-family-member)).
A founding runs at the public label and registers the member in its own
step. A family's options set how many and who owns each new member:

* `members: n` caps how many are registered at once, charged at the public
  label. Past it a registration refuses `Limit`.
* `keyed: true` also indexes members by a business key,
  `ctx.register(family, { key })`, at the family's stem. The tag
  stays random, a fresh registration of a taken key refuses `Exists`, and
  erasing the member deletes the index entry. A keyed family declares
  `presence: "public"`: whether a key is taken is a public fact.
* `founder: admin` issues `admin` to the registrant under the new member in
  the same commit as registration
  ([Initial owner](https://docs.bijection.com/access/grants.md#initial-owner)).

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

// Organizations found by their domain.
export const org = label.family(t.string, { keyed: true, presence: "public", members: 1000 });
```

A table labelled with a family keeps each fact at one member. It has no
member column: the member is the frame's. A function `under: tenant` takes
its member from the caller's frame, so a client picks the member it acts in:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const acme = client.under(tenant.of("acme"));
await acme.mutate(acme.app.orders.place, { number: "A-1", total: new Decimal("12.50") });
```

A clerk is admitted in `acme` only through a grant held under
`tenant.of("acme")`: `ctx.grant(clerk, { to, under: tenant.of("acme") })`.
`tenant.and(finance)` joins the member with another label, both as a
function's `under` and as a table's `label`. To read or write another
member's facts explicitly, address them with `orders.of(tenant.of(key))`; the
read needs a frame that clears that member.

## Share with a named person

Every principal clears its own member of the built-in `label.principals` family,
with no role. A fact labelled by a field holding a principal is known to that
principal alone: `label.principals.field(name)` labels each fact at the member its
field names.

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

export const owner = role({});

export const shares = table({ reader: t.principal, document: t.string, note: t.string }, {
  key: ["reader", "document"],
  label: label.principals.field("reader"),
});

export const share = mutation(
  { allow: [owner], args: { reader: t.principal, document: t.string, note: t.string } },
  (ctx, fact) => ctx.put(shares({ reader: fact.reader, document: fact.document }), fact),
);

export const mine = query({ under: label.principals, allow: [role.signedIn] }, (ctx) =>
  ctx.list(shares.where({ reader: ctx.frame.as.id }).atMost(50)),
);
```

`share` runs at the public label and writes up into the named reader's member
with a blind `put`; `mine` runs under the caller's own member, so a reader sees
only what was shared with them. The key fields stay known at the public
label. To share a whole function's data with a person, grant them a role under
their member: `ctx.grant(viewer, { to: person, under: label.principals.of(person) })`.

## Choose the label by a field

`label.field(name, cases, { otherwise, release })` labels each fact by the case
its field holds, and `family.field(name)` by the family member its field
names. Both combine with `.and` and `.or`. The field is a
presence field, and a write that moves a fact to a lower case needs the
release:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const drafts = table({ stage: t.one("draft", "published"), body: t.string }, {
  label: label.field("stage", { draft: editorial, published: label.public }, { release: lift }),
  presence: { label: label.public, field: ["stage"], release: lift },
});
```

## Field labels

A field can carry its own label, above or below its fact's. Every read of
the field is judged at the field's label, like a fact.

### Seal one field

A **sealed field** is a field labelled above its fact. `label.seal(type)`
declares a field whose values carry the label:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const orders = table(
  { number: t.string, total: t.decimal, margin: finance.seal(t.decimal) },
  { key: ["number"], label: tenant },
);
```

A frame that clears the fact but not `finance` reads the fact with `margin` as
a `Sealed<Decimal>`. `ctx.open(sealed)` sees the value inside a frame that
clears its label; it moves nothing. Lowering a sealed value to a wider
audience is a disclosure, through a [release](https://docs.bijection.com/access/disclosure.md).

### Presence below the fact

A field labelled below its fact is known to a wider audience than the fact
itself. `presence` declares that lower label for the fact's existence, its key
and the listed fields:

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

export const roster = release({ from: salary, to: label.public, message: "Publish the staff directory" });

export const salaries = table({ employee: t.string, amount: t.decimal }, {
  key: ["employee"],
  label: salary,
  presence: { label: label.public, field: ["employee"], release: roster },
});
```

`ctx.has(salaries(employee))` answers at the presence label, so any frame
learns whether the employee is listed, and an index over `employee` serves
public selections. `amount` stays at `salary`. The fields listed in
`field` stand under the standing `release`, whose `from` covers the fact
label and whose `to` is within the presence label.

`salaries` is a [protected table](https://docs.bijection.com/database/protected-tables.md): its orders
keep tokens of their fields at the store. Under a public presence label with a
release, a declared index whose fields are all presence or key fields
(`index: [["employee"]]`) is kept clear and in order instead, as if it
declared `release: roster`, so it serves every selection. The key's own order
answers exact lookups, `salaries(employee)`. An index over `amount` would keep
tokens: exact lookups, and `ctx.list(selection.atMost(n))` for any other
shape.

## Label flow

Every read stays at or below the frame's clearance, and every write goes
up: it carries what the transaction read to a label at or above the
function's `under`. These checks live in one place. `bijection check` reports
each one at its `path:line:col` with a sentence that says how to fix it, and
the host enforces every one of them again at the act. Editors see them as
`check` diagnostics; the TypeScript types check shape only (verbs, spec keys,
value types, indexes), never flow.

| Code | What `check` found |
| - | - |
| `ReadAbove` | A read (`get`, `has`, `list`, `page`, `count`, or a strict write's assumption) of data above the function's `under`. |
| `WriteBelow` | A write that would carry the frame's data into a table labelled below it. |
| `OpenAbove` | A plain `ctx.open` of a sealed field or argument whose label the frame does not clear. |
| `CallAbove` | A clear query call or synchronous mutation call whose callee's label does not flow to the caller's. |
| `DiscloseWrongRelease`, `DiscloseAbove` | An `open` whose release does not cover the sealed value's label or whose audience is above the frame. |
| `AllowBelowLabel` | A role in `allow` whose `clear` does not cover the function's `under`. |
| `CoverRequired` | A wide read over a cleared family stem or disjunctive share without a declared `across` cover (fix: add `across: family.all` / `family`, or narrow with `.of(member)`). |
| `ReadPlacesUnbounded` | An analyzed read site or frame has a static bound above 64 places. An `under` member list without a static bound is printed as member-dependent. The runtime place cap applies to its actual admitted places. Narrow with `.of(member)` or a declared cover. |
| `CoverOverlap` | A count, aggregate or ranked read whose roots overlap on a disjunctive clause (fix: `.of(member)`, stem + `across: family.all`, or `list`/`page`). |

Any direct or runtime-assembled frame exceeding 64 root places also refuses
`CoverTooWide` at entry before any store lookup.

For example:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
ReadAbove: ctx.get reads finance data (access.ledger), above this function's under (tenant); add finance to under, or read it in a function whose under clears it
```

A commit selector can declare a maximum arity. Use
`teams.field("teamIds", { atMost: 16 })` for member arguments.
The host checks the length before it joins the labels. Exceeding it refuses
`Invalid` with reason `ArityExceeded`. The lock prints the read-place bound
when the selector's arity bounds it.

## Eventual labels and timed lowering

To publish a fact at an instant, label it by a field and let a scheduled task
change that field through a release. `labelSwitch` from
`@bijectionhq/library-label-switch` builds this from a task and the release
you name:

```ts bijection/press.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, mutation, release, role, t, table } from "@bijectionhq/bijection";
import { labelSwitch } from "@bijectionhq/library-label-switch";

export const editorial = label();
export const lift = release({ from: editorial, to: label.public, message: "Publish an embargoed story at its deadline" });

export const stories = table({ slug: t.string, body: t.string, stage: t.one("embargoed", "published") }, {
  key: ["slug"],
  label: label.field("stage", { embargoed: editorial, published: label.public }, { release: lift }),
  presence: { label: label.public, field: ["stage"], release: lift },
});

export const embargo = labelSwitch({ table: stories, field: "stage", before: "embargoed", after: "published", under: editorial, release: lift });

export const editor = role({ clear: [editorial] });

export const schedule = mutation(
  { under: editorial, allow: [editor], args: { slug: t.string, at: t.instant } },
  (ctx, { slug, at }) => embargo.at(ctx, slug, at),
);
```

* Place `embargo` in the app tree: it holds `embargo.lower`, the mutation
  the task runs. `embargo.at(ctx, key, instant)` submits that task for one
  fact, due at the instant, and answers the task. Withdraw the task to cancel
  the lowering.
* When the task runs, it moves `stage` from `embargoed` to `published`
  through `lift`. That one write is all it does, at the fact's own place,
  and the use of `lift` is recorded as a disclosure. A fact no longer
  `embargoed` is left as it is.
* The task decides through the scheduler's grants that are still live when it
  runs. If the editor's grant was revoked or expired in between, the
  lowering refuses and the story stays embargoed.
* Nothing runs at the instant: the task can first commit at its due instant,
  and its wakeup resumes after a restart. List pending lowerings with
  `sys.tasks.where({ target: embargo.lower })`.
* A task lowers a fact written in a branch within that branch.
* If the lowering refuses, for example because the fact's unique key is
  already taken at the lower label or the destination has no capacity left,
  the task settles with that refusal and the fact stays at its earlier
  label. Resolve the constraint, then schedule it again.

The lowering is an ordinary write, so it needs a commit and may complete
after its scheduled instant. At any place and instant, at most one fact
holds each unique key. Until the task commits, an observer who cannot clear
`editorial` learns nothing of the fact beyond what its presence fields
declare.

## Reads validated by what they revealed

A transaction's reads are validated at commit by the values they revealed.
A change entirely above a reader's label never aborts that reader, so a
writer at a higher label cannot signal through conflicts. See
[How an act is decided](https://docs.bijection.com/access/decisions.md).


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