# scope-algebra — scopes, grants, seals, policy

## Lesson
Authority as algebra: scopes are sets, grants derive policy, and seals
make rosters/plans tamper-evident. Three hard rules carry the lesson:
(1) `scopeSet` auto-grants OBSERVE and is the only place that does;
(2) NEVER_IMPLICIT — a `reclaim` grant without the scope named in the
grant's sealed `explicit` list is a roster error (enforced by
`validateRoster`, check A-8); (3) the Gate recomputes a plan's required
scopes from its body — a plan's own label is never trusted (A-9).

## Provenance
Carried copy (2026-11-07) of `~/workspace/materials/solo/scope-algebra.cjs`
+ `scope-registry.cjs` (the Gate-side tool table; the algebra exports
none since the T5 fix pass R-3). Source system: the "raijin" broker
v0.2.0 authored by N8toriousAi (N8toriousAi); fixtures are test_broker.py's
roster verbatim. Fix history: `scope-algebra-FIX-RECEIPT.md` in
materials/solo. Originals untouched; `program.cjs` and
`scope-registry.cjs` are verbatim + headers (registry's algebra import
now points at `./program.cjs`).
`check.cjs` preserves the source check's assertions verbatim (module
paths only) and counts its ok/FAIL lines via a child-process run.

## How to run
```
node check.cjs     # PASS <n> FAIL <m>
node example.cjs   # grant/deny evaluation incl. hard-deny rules
const A = require('./program.cjs'); const R = require('./scope-registry.cjs');
```

## Worked example
`example.cjs` derives policy from the fixture GRANTS and evaluates:
optimizer→set_affinity ALLOW, planner→set_affinity DENY,
researcher→observe ALLOW, terminate DENY for every agent,
janitor→suspend REQUIRE_APPROVAL; then shows validateRoster refusing
an unmarked reclaim grant by name (NEVER_IMPLICIT) and requireScope
raising ScopeDenied ("scope 'propose' not granted").

## Numbers reproduced
- Source check suite: **PASS 28 FAIL 0** (`all checks passed`).
- Seal golden vector pinned by the suite (A-7): computeSeal(GRANTS) =
  d8147618b72f872fb409dbe8a34f704aaafc5de9c80a177e83efd6e575afcf86 —
  a canonicalisation change must fail loudly against it.
- Example verdicts as listed above, reproduced live on every run.

## Consumers
Gate / broker code: anything minting facets, dispatching tools by
required scope, sealing rosters or plans, or attenuating grants.

## Honest limits
- The tool table lives in `scope-registry.cjs` on purpose (Gate-side);
  consumers enumerate tools from there, never from the algebra.
- Free-text fields (notes, plan goals) carry no authority (R-7): edits
  to them do not break seals, by design — never file a claim in text
  that a seal is expected to protect.
