Skip to main content
A guard refuses acts that positive authority would otherwise allow. It never grants anything: a yes is a live grant of a role in the function’s allow. Declare a guard with forbid(scope, { message, where }) and place it anywhere in the app tree. The host enforces it at the act, whichever function does it.
bijection/invoices.ts
A refused act fails the transaction with Forbidden. The refusal’s message is the guard’s reason, and its guard is the guard’s tree path (invoices.fixedCurrency): the path is the guard’s identity.

Scopes

A scope names its declaration by value. Use act.grant to guard ctx.grant, and act.write to guard changes to a table’s facts. Pass an array to cover several scopes with one guard, forbid([act.grant(hr), act.disclose(payroll)], …). It sees only the roots the scopes have in common. Write guards share one commit-time act per changed fact. Its before is the committed image at the transaction’s basis, and its after is the final staged image. Use act.write(table, { transition: "create" }) to guard an absent fact becoming present, "update" for a present fact changing, and "remove" for a present fact becoming absent. A write followed by a patch of its new fact is still a creation. A keyed put can create or update, and a retraction of a present keyed fact removes it. Equal images and an insert followed by a deletion of the same fact produce no write act. A fields filter compares only the two final decision images, as does a guard on a machine’s edge. The staged verb still matters to verb-specific scopes. The creation followed by a patch also matches act.patch. The SDK stages keyed patches and replacements as puts, and keyed deletes as retractions. The last staged write supplies writer (the function’s tree path) and the grants offered for all matching scopes, including when that write came from a nested call.

Roots

Every guard reads one record of the act: the same values as ctx.frame, the grant being decided, and the roots of its act. The where: tier describes comparisons over those values. The host computes a.now, a.mfaAge, a.holdings and a.readSet; the caller cannot supply them. a.args and a.after are chosen by the caller, so a guard may read them only to refuse. Field types follow the declared value types: t.boolean is a boolean, t.int an integer, t.string, t.key(table) and t.principal strings, t.instant an instant, t.one(...) of literals an enum, and t.maybe(x) nullable. A field of any other type is out of the where: tier; read it in when:.

Purpose of use

A sensitive function can declare why as an argument using purposeArgs from @bijectionhq/library-sensitive-data. Apply the library’s ordinary where: guard to the function’s entry.
Supply { why: { purpose: "case-review", reference: "CASE-42" } } to the call. A null or mismatched purpose returns the guard’s Forbidden prompt. Inside the guarded handler, call recordPurpose(ctx, why) before the sensitive operation to record the evidence in Deployment History under the run’s label.

A guard judges each offered grant

An act is offered through each live grant of each party: the caller, its code and its credential. Every guard in scope judges each grant on its own, with a.offer bound to that grant and its whole role chain. A grant some guard refuses is removed; the act is admitted if the grants that survive still cover it together (how an act is decided).
  • A guard only removes grants. Adding a guard never admits an act, and a guard about one role refuses only the grants of that role: it never removes a right another grant holds.
  • a.offer.role.equals(manager) and a.offer.role.oneOf([clerk, manager]) take role values and refuse exactly the grants of those roles. a.offer.chain includes the roles of the grants it was issued from, so a delegated or invited grant still answers for its origin.
  • Open entries (allow: [role.anyone], allow: [role.signedIn]) are class grants, judged like any other grant.
  • A rule about the actor uses a.as, a.holdings or a fact read. An actor-wide rule refuses the act through every grant.
  • Session-bound roles (break-glass, fresh sign-in) are short grants plus guards on act.grant and on the entry (example).
  • When guards remove every grant that would cover the act, the refusal is the first grant’s (by grant order) that reached a guard refusal: Forbidden, with that guard. With no covering grant at all it is Disallowed.
A treasurer who also holds clerk pays through the treasury grant; the same person acting only as a clerk is refused.

Two tiers

Give a guard exactly one of them. Prefer where:; use when: for a condition the builder cannot state.

The where: builder

There is no multiplication, no sum of two terms and no call. A where: that returns a JavaScript boolean is a type error (WhereNotTerms): build it from terms, a.after.state.equals("paid"), not ===. Nulls. A field read through null is null. Every comparison with a null operand is false, except .isNull(), .isNotNull() and .equals(null). Use .isNotNull() to test presence. To make another predicate depend on presence, use a.before.isNotNull().ifElse(whenPresent, a.false).

Read facts

Every scope offers the frame’s roots: a.as, a.offer, a.holdings, a.chain, a.mfaAge, a.readSet, a.on, a.now, a.under (the act’s commit label, as a set of tags), a.in (its world, null at the head) and a.at (its valid instant). A where: guard reads one fact with a.get(point) and tests presence with a.has(point). A point is table(key) or table({ uniqueFields }), and its key may be a term. A rule about the roles a person holds reads a.holdings.has(role), not sys.grants.

Bounded arrays and deliveries

An array whose schema declares a maximum length (t.named(…, { length: { maximum } })) offers xs.every((x) => …) and xs.length.lessOrEqual(n) (and the other comparisons with an integer literal). Both unroll into one conjunct per possible element, which counts against the guard’s budget, and the lock prints the unrolled form. An element’s fields are nullable terms. An array without a declared maximum offers neither; deploy refuses a rule over one. A write a claimed reply lands reads a.delivery: the delivered task record, taken from the stored task, never from args. It holds tid, replyTo (the reply function and the place it writes at), observed, start and the delivered rows, bounded by the receiver’s page size. On any other act, a rule that reads a.delivery refuses.

Holdings rules

To block approval by a requester, read the caller’s holdings when they enter. a.holdings holds the roles of the caller’s live grants and, for an agent, those of the person it acts for, so a delegate cannot approve what its delegator could not. A requester grant committed after an earlier successful entry makes the next entry refuse through every grant, including a still-eligible approver grant.
The library rule separationOfDuties states the same rule per role (library rules). A rule that reads holdings is the one kind of guard that a new grant can make stricter, so the lock marks it [reads holdings]. Every when: guard is marked too, since it may read them. A guard on a fact’s fact reads it the same way:
  • Give every guard that reads facts an explicit under, including under: label.public for public facts.
  • If the table can place facts above that label, the read refuses. The guard cannot mistake an unreadable fact for an absent one. A grant presence check also refuses when its role can place grants outside the lookup scope.
  • The reads join the transaction’s read set: a conflicting change to a fact a guard read invalidates the decision.
  • Read a family table, series, timeline or other sys fact in when:.

The when: tier

A when: function receives the act’s roots as plain values, and the read verbs get, has and list at the guard’s under. It has no other ctx verb. Use a.list(selection.atMost(n)) to require a complete result within the stated bound.
  • It runs inside the transaction it judges: at its version, as its caller, over the facts the transaction has written so far. At commit, that is the state the transaction leaves. Its reads of facts the transaction did not write join that transaction’s read set, so a conflicting change to what it read invalidates the decision, as for where: facts.
  • A write in a branch is judged in the branch: the guard reads the branch at the version the call entered. When the branch lands, its writes are judged again at head, where the land reads; a conflicting change there makes the land run again.
  • A refused read refuses the act, even when the function catches it.
  • A guard whose under is above the caller’s label refuses without running: a guard never reads what its caller cannot.

One transaction, several acts

A guard on a write, or on a grant, revocation, disclosure, submit or erasure, is judged at commit against the state the transaction leaves, with every write and grant of the transaction in it:
  • two inserts that a write guard counts against a bound both count;
  • a transaction that updates two facts one guard compares commits only if the pair it leaves passes the guard. Raising an amount and its limit together commits when the new amount is within the new limit.
The guard sees its own act’s fact in that state too: a bound counts the inserted fact with the others. Who may act is still decided at the transaction’s start, so a transaction gains no authority from a grant it issues itself. A value a guard admitted before commit, such as a release opened with ctx.open, is judged again at commit, which can only refuse more. An act.enter guard is judged when the call enters, over what the transaction has written so far, and is not judged again at commit: it decides the entry, and the writes its function then makes are its own. Separation of duties takes two forms. “A holder of requester may not approve” is the entry rule above, over the caller’s holdings. “No one holds requester and approver together” is enforced at issuance: act.grant and act.revoke offer a.grant.holdings, the roles of the recipient’s (a.grant.to) live grants read at the grant’s label, plus the issued grant’s own chain. Guard each role’s issue with the other. Here finance covers both roles’ grants and flows to the label of each guarded act.
Both rules read the recipient’s grants as the transaction leaves them, so issuing both roles to one recipient in one transaction refuses. A failed read refuses. The lock marks both [reads holdings].

Fail closed

A guard refuses when its predicate holds or a required read fails or exceeds its budget. A when: function also refuses if it throws or returns a non-boolean. The Boolean connectives .and(), .or(), and .implies() are strict. condition.ifElse(whenTrue, whenFalse) evaluates the condition first and reads only the selected branch. Deploy checks the condition and both branches. A live evaluation can request up to 16 distinct fact points from the condition and the branch it selects. The condition and branch read from the same held view at the guard’s under label. The commit validates those reads, including an observed absence. If the condition’s read changes before ordering, the attempt must retry and select a branch again. Adding a guard can only narrow admission when the other decision inputs are fixed.

Library rules

@bijectionhq/library-policies states the common policies as guards. Each helper returns one guard (two for breakGlass) to place in the tree, judged per offered grant like any other:
bijection/policies.ts
The purpose of use guard of @bijectionhq/library-sensitive-data also records the stated purpose. A table’s writers: () => [record, pay] is a guard on act.write(table) that refuses every write not made by one of those functions. It has no exception: the host, operators and other functions are refused alike. An operator correction is a mutation of the app that the table’s writers name.

Guards over a mounted app

A mountable app is a function from its capabilities to app(...). Its own guards travel with it, at paths under the mount (billing.ledgerWriters). The installer can add guards over the mounted app’s acts, naming them by value through the mounted subtree:
bijection/app.ts

The lock

bijection check --write-lock writes bijection.lock beside app.ts. Each guarded path lists its guards, and each guard has one line, sorted by path:
A where line carries the guard’s canonical predicate; a when line says tested. [reads holdings] marks a guard that reads the caller’s holdings: every when: guard, and a where: guard whose predicate reads a.holdings. The fact lines are the reads where: guards make. Review the lock diff with the source change: it is the reviewable list of everything an installation exposes and every guard on it.

The analyzer

bijection check lists every guard with its scopes, label and tier, and under “Guard inputs” the tables each guard reads (with GuardReadAbove for a read above the guard’s under). With cvc5 or z3 on PATH it translates each where: predicate and answers: Each question is posed per grant: a witness is a grant of a role and an act. Facts are unknown values, so a witness may name a value no current fact holds. Vacuity and reachability hold even when reads fail; a policy diff compares decisions whose reads succeed. Without a solver, check prints analysis unavailable: no SMT solver (cvc5 or z3) on PATH and checks the rest of the app.

Review a guard change

  1. Change the guard in source and run bijection check.
  2. Read the policy diff: every newly refused and newly allowed act, with a witness. Ask --prove for the acts that must stay refused.
  3. For a when: guard, test it on the real kernel with kernel(app): an act it refuses, one it allows, one outside its scope and a failing read.
  4. Review each reason: it is the sentence the caller reads.
  5. Write the lock with --write-lock, review its diff and commit both.

Trial a call

client.trial(fn, args) runs a call in a discarded world, under the caller’s own authority, and commits nothing:
writes lists the facts the call would write and tasks the tasks it would submit. From the terminal, bijection trial invoices.setAmount number=A-1 amount=-5. A trial’s result is labelled by the join of everything it read, so it never reveals data above the caller’s clearance.