rfc: 0006 revision 2 per owner review
1. capabilities are unforgeable references to one shared authority object; authority-bearing types are transitively non-serializable 2. effect -> authority registry; not every effect needs a capability 3. flat effects kept; effect sets must be part of future fn pointer types 4. grants bound by @main parameter name; missing, mismatched, unknown and invalid grants are not_instantiated reasons (exit 6) 5. cap.narrow is a fallible terminator (ok -> cap<K>, err -> cap_error); no empty capabilities 6. cap.narrow is the only authority transformation; no combine 7. startup data deferred and separate from authority; environment via cap<process.env> 8. capabilities live for one execution instance
This commit is contained in:
1 parent
56728a534c
commit
3fd1513c86
1 file changed
+77
-33
@@ -1,6 +1,6 @@
|
||||
# RFC 0006: Effects and Capabilities in AIR
|
||||
|
||||
Status: Proposed
|
||||
Status: Proposed (revision 2: owner review items 1–8 applied)
|
||||
Scope: AIR format 0, features `effects` and `capabilities`
|
||||
Language version: 0.1
|
||||
Supersedes: —
|
||||
@@ -19,24 +19,29 @@ Spec 05 separates *what code does* (effects) from *what an invocation may do* (c
|
||||
2. **Unforgeability.** A capability value can only originate from the host (as an argument of the entry function) or from an existing capability by an explicit, verifier-known narrowing. No instruction creates one from integers, bytes, constants or other values, and no instruction inspects its representation.
|
||||
3. **No widening.** Narrowing never yields more authority than its input, and never changes the kind of authority.
|
||||
4. **Effect soundness.** If a function can perform effect `E`, directly or through any callee, then `E` is in its declared effect set.
|
||||
5. **Separation.** Features say what the implementation must understand; effects say what code does; capabilities say what an invocation may do. None implies another.
|
||||
5. **Separation.** Features say what the implementation must understand; effects say what code does; capabilities say what an invocation may do. None implies another, and not every effect needs authority.
|
||||
6. **Monotone authority graph.** The only operation that transforms authority is `cap.narrow`. Authority flows from the host down through narrowings; it is never widened, combined, forged or cast.
|
||||
7. **Instance lifetime.** A capability is valid only within the execution instance that received it from the host.
|
||||
|
||||
## 1. Effects (feature `effects`)
|
||||
|
||||
### 1.1 Effect names
|
||||
|
||||
Effect names come from a closed registry, `reference/effects.md`, initially the list of spec 05 §2:
|
||||
Effect names come from a closed registry, `reference/effects.md`, initially the list of spec 05 §2. The registry records for each effect whether exercising it requires authority, and which capability kind provides it:
|
||||
|
||||
```text
|
||||
mem.alloc mem.map
|
||||
fs.read fs.write
|
||||
io.read io.write
|
||||
net.connect net.listen net.read net.write
|
||||
clock.read random.read
|
||||
task.spawn thread.spawn thread.block
|
||||
atomic atomic.weak
|
||||
process.spawn ffi unsafe
|
||||
```
|
||||
| Effect | Authority |
|
||||
|---|---|
|
||||
| `mem.alloc`, `mem.map` | `cap<mem.alloc>`, `cap<mem.map>` |
|
||||
| `fs.read`, `fs.write` | `cap<fs.read>`, `cap<fs.write>` |
|
||||
| `io.read`, `io.write` | `cap<io.read>`, `cap<io.write>` |
|
||||
| `net.connect`, `net.listen`, `net.read`, `net.write` | `cap<net.connect>`, `cap<net.listen>`, `cap<net.read>`, `cap<net.write>` |
|
||||
| `clock.read`, `random.read` | `cap<clock.read>`, `cap<random.read>` |
|
||||
| `process.spawn`, `process.env` | `cap<process.spawn>`, `cap<process.env>` |
|
||||
| `task.spawn`, `thread.spawn`, `thread.block` | none |
|
||||
| `atomic`, `atomic.weak` | none |
|
||||
| `ffi`, `unsafe` | none (governed by unsafe boundaries, spec 13) |
|
||||
|
||||
An effect says what a function does; a capability gives power over an external resource. The relation is not one-to-one: effects such as `atomic` or `thread.block` need no authority.
|
||||
|
||||
In a module that declares `effects`, an unregistered name in an `effects [...]` list is `E0004`. New names are added to the registry by the feature that introduces their operations.
|
||||
|
||||
@@ -44,7 +49,7 @@ In a module that declares `effects`, an unregistered name in an `effects [...]`
|
||||
|
||||
Effects are atoms. The dotted spelling is organizational only: `atomic` does not imply `atomic.weak`, `io.write` does not imply `fs.write`, and no effect implies another. A function that performs two effects lists both.
|
||||
|
||||
Reason: implication rules are a second semantics agents must model; a flat set is checked by set inclusion and diagnosed by naming the missing atom.
|
||||
Reason: implication rules are a second semantics agents must model; a flat set is checked by set inclusion and diagnosed by naming the missing atom. It also keeps weaker mechanisms visible across the call graph: a function declaring `[atomic]` cannot call one declaring `[atomic.weak]` without saying so.
|
||||
|
||||
### 1.3 Checking rule
|
||||
|
||||
@@ -54,6 +59,8 @@ In a module that declares `effects`:
|
||||
- For every `intrinsic` in `f` (§3): the intrinsic's effect is in `effects(f)`, otherwise `E3101`.
|
||||
- Because every function is checked against its direct callees, the rule holds transitively, including for recursion.
|
||||
|
||||
**Indirect calls (normative for future features).** When function values are introduced, the effect set MUST be part of the function type, for example `fnptr<(u8) -> void effects [io.write]>`, and an indirect call is checked against that set exactly like a direct call. Otherwise dynamic calls would break transitivity.
|
||||
|
||||
There is **no effect discharge** in format 0: no construct removes an effect from what a caller must declare. A later feature may introduce a discharging boundary only by specifying it explicitly (Q-011).
|
||||
|
||||
In a module that does not declare `effects`, `effects [...]` lists are recorded and not checked (`reference/air-features.md`). Such a module cannot contain an effectful operation, because every intrinsic belongs to `capabilities`, which depends on `effects`.
|
||||
@@ -62,14 +69,17 @@ In a module that does not declare `effects`, `effects [...]` lists are recorded
|
||||
|
||||
### 2.1 Type
|
||||
|
||||
`cap<K>` is the type of a capability of **kind** `K`. Kinds come from a closed registry, `reference/capabilities.md`; in format 0 each kind is named after the single effect it authorizes (`cap<fs.read>` authorizes operations with effect `fs.read`). An unregistered kind is `E0004`.
|
||||
`cap<K>` is the type of a capability of **kind** `K`. Kinds come from a closed registry, `reference/capabilities.md`, which lists for each kind the effects it authorizes (see §1.1) and its narrowing forms. An unregistered kind is `E0004`.
|
||||
|
||||
A capability is an **opaque value**:
|
||||
A capability value is an **unforgeable reference to one runtime authority object** held by the host. Copying a capability copies the reference, not the authority: all copies denote the same authority object and share its state. For example, two copies of a `cap<mem.alloc>` narrowed to a 64 MiB budget draw on one shared 64 MiB budget, not 64 MiB each (RFC 0008).
|
||||
|
||||
- it is copyable: it may be passed, returned, stored in structs and enums, and copied;
|
||||
- it is not zeroable, not comparable (`eq`/`ne` do not accept it), and has no constant form;
|
||||
- no instruction converts it to or from any other type;
|
||||
- its accounted size (spec 08 §7.1) is 8.
|
||||
A capability is **opaque**:
|
||||
|
||||
- it is copyable: it may be passed, returned, stored in structs, enums and arrays, and copied;
|
||||
- it cannot be cast to or from an integer or any other type, bit-cast, compared (`eq`/`ne` do not accept it), zero-initialized, given as a constant, serialized or inspected as bytes;
|
||||
- its accounted size (spec 08 §7.1) is 8, but those 64 bits are not a number, an address or a serializable representation.
|
||||
|
||||
**Authority-bearing types.** A type is authority-bearing if it is `cap<K>` or an aggregate or array containing an authority-bearing type. Every rule above is transitive: an authority-bearing type is not zeroable, and any future instruction that exposes the bytes of a value (a `bytes`-of-value view, bit-casts, by-value FFI) MUST reject authority-bearing types. A `struct @Logger { out: cap<io.write> }` can therefore never be turned into `slice<u8>`.
|
||||
|
||||
Copyable authority keeps format 0 simple: a capability is shared by copying it, and authority is limited by narrowing, not by tracking copies. Move-only (one-shot) and revocable capabilities are not part of format 0; they can be added later as new kinds or a new feature.
|
||||
|
||||
@@ -82,14 +92,43 @@ fn @main(%out: cap<io.write>, %src: cap<fs.read>) -> void
|
||||
effects [fs.read, io.write]
|
||||
```
|
||||
|
||||
- `@main` parameters MUST all be capability types; the runner rejects other signatures before execution.
|
||||
- Before execution the host **instantiates** the program: for each parameter it must hold a matching grant. Grants come from the runner (for example `aji run --grant io.write --grant fs.read=/data`) or, for packages, from `aji.toml` `[capabilities]` (spec 12).
|
||||
- If a grant is missing, the program does not start. This is an **environment outcome**, not a trap and not a verifier error: the run record is `{"status":"not_instantiated","missing":["fs.read"]}` and `aji run` exits with code 6.
|
||||
- `@main` parameters MUST all be capability types in format 0; the runner rejects other signatures before execution.
|
||||
- Grants are bound **by parameter name**, so two parameters of the same kind can receive different scopes:
|
||||
|
||||
```bash
|
||||
aji run app.air --grant config=fs.read:/etc/app --grant data=fs.read:/var/data
|
||||
```
|
||||
|
||||
For packages the same bindings come from `aji.toml` `[capabilities]` (spec 12).
|
||||
- Before execution the host **instantiates** the program: every capability parameter of `@main` must have exactly one grant whose kind matches, and every grant must name a parameter. Otherwise the program does not start. This is an **environment outcome**, not a trap and not a verifier error; `aji run` exits with code 6 and reports, for the first problem in parameter order:
|
||||
|
||||
```json
|
||||
{"status":"not_instantiated","reason":"missing_capability","parameter":"data","kind":"fs.read"}
|
||||
```
|
||||
|
||||
with `reason` one of `missing_capability`, `kind_mismatch` (fields `parameter`, `kind`, `granted`), `unknown_parameter` (an extra grant, field `parameter`; typos are not ignored) and `invalid_scope` (the host cannot represent the requested scope, fields `parameter`, `scope`).
|
||||
- Compiling or verifying a module never grants authority (spec 05 §6).
|
||||
|
||||
### 2.3 Narrowing
|
||||
**Startup data (future).** RFC 0006 defines capability instantiation only. Future features MAY allow immutable non-authority startup values (for example `port: u16`) as `@main` parameters; such values are input data, are recorded in the execution record for replay, and grant no authority. Command-line arguments are startup data of this kind. The host environment is not: it often holds secrets, so reading it requires `cap<process.env>` and an intrinsic.
|
||||
|
||||
`%n = cap.narrow.<form> %c, %arg…` produces a capability of the same kind with less authority. The capability registry lists, per kind, which narrowing forms exist and what they mean; for example (specified with the kinds in RFC 0007):
|
||||
### 2.3 Lifetime
|
||||
|
||||
A capability obtained at instantiation, and every copy and narrowing of it, is valid from the start of the execution instance until its end. Because capabilities cannot be serialized (§2.1), none can be stored and used by a later run.
|
||||
|
||||
### 2.4 Narrowing
|
||||
|
||||
`cap.narrow` is the only instruction that transforms authority. It is fallible and is a **terminator**, delivering its result to block parameters as `enum.switch` delivers payloads, so a failed narrowing can never be mistaken for a usable capability:
|
||||
|
||||
```text
|
||||
cap.narrow.<form> %c, %arg…, ok -> @ok, err -> @err
|
||||
|
||||
ok(%n: cap<K>): ; same kind K, at most the authority of %c
|
||||
err(%e: cap_error): ; why the restriction was refused
|
||||
```
|
||||
|
||||
`cap_error` is a built-in enum with the variants `invalid_restriction` (the argument is malformed or forbidden, for example a path containing `..`), `not_subset` (the requested scope is not within the current one, for example `/etc` below `/data`) and `unsupported_restriction` (the host cannot enforce this restriction for this capability). It is used with `enum.switch cap_error %e, …`. Built-in enums are spelled without `@` and have registered variants; they are values like any enum.
|
||||
|
||||
The capability registry lists, per kind, which narrowing forms exist and what they mean; for example (specified with the kinds in RFC 0007 and RFC 0008):
|
||||
|
||||
| Kind | Form | Meaning |
|
||||
|---|---|---|
|
||||
@@ -97,9 +136,11 @@ effects [fs.read, io.write]
|
||||
| `mem.alloc` | `budget` | restrict to at most N bytes (`usize` operand) (RFC 0008) |
|
||||
|
||||
- A form not listed for the operand's kind is `E3202` (`capability.widen`): there is no instruction that changes kind or adds authority, so any attempt is reported as widening.
|
||||
- A narrowing that cannot be represented at runtime (for example a path prefix that escapes the current scope with `..`) does not widen: it yields a capability with **no** authority, and every operation through it fails as the intrinsic's failure model specifies (RFC 0007). Narrowing never traps.
|
||||
- Narrowing never traps and never produces a capability with more authority than its input. There is no "empty capability" in format 0: a refused restriction goes to the `err` target.
|
||||
- `ok` and `err` targets follow the block-parameter rules of `enum.switch`: `@ok` takes exactly one parameter of type `cap<K>`, `@err` exactly one of type `cap_error` (`E5002` for the count, `E1001` for the type).
|
||||
- There is no `cap.combine`, union, widening or kind conversion in format 0.
|
||||
|
||||
### 2.4 Using a capability
|
||||
### 2.5 Using a capability
|
||||
|
||||
An effectful operation takes its capability as an explicit operand (§3). If the operand's kind differs from the kind the operation requires, the error is `E3201` (`capability.missing`) rather than a generic type mismatch, so an agent sees that authority, not data, is missing.
|
||||
|
||||
@@ -156,12 +197,13 @@ effects [] ; E3101: callee or intrinsic needs io.w
|
||||
```
|
||||
|
||||
```text
|
||||
%w = cap.narrow.prefix %fs_read, %p ; result is still cap<fs.read>
|
||||
cap.narrow.prefix %fs_read, %p, ok -> @use, err -> @fail
|
||||
use(%w: cap<fs.read>): ; still fs.read
|
||||
intrinsic fs.open_write(%w, …) ; E3201: needs cap<fs.write>
|
||||
```
|
||||
|
||||
```text
|
||||
%x = cap.narrow.budget %fs_read, %n ; E3202: no `budget` form for fs.read
|
||||
cap.narrow.budget %fs_read, %n, ok -> @a, err -> @b ; E3202: no `budget` form for fs.read
|
||||
```
|
||||
|
||||
## Diagnostics
|
||||
@@ -169,12 +211,13 @@ effects [] ; E3101: callee or intrinsic needs io.w
|
||||
| Code | Use |
|
||||
|---|---|
|
||||
| E0004 | unknown effect name, capability kind or intrinsic (existing) |
|
||||
| E1001 | capability in a position that requires a value it cannot be (zeroing, comparison, constant) (existing) |
|
||||
| E1001 | capability in a position that requires a value it cannot be (zeroing, comparison, constant), or wrong `ok`/`err` parameter type (existing) |
|
||||
| E5002 | wrong number of `ok`/`err` block parameters (existing) |
|
||||
| E3101 | effect not declared (existing, now specified) |
|
||||
| E3201 | capability operand of the wrong kind (existing, now specified) |
|
||||
| E3202 | narrowing form not defined for the kind (existing, now specified) |
|
||||
|
||||
New environment outcome (not a code): `not_instantiated`, CLI exit code 6.
|
||||
New environment outcome (not a code): `not_instantiated` with a `reason`, CLI exit code 6.
|
||||
|
||||
## Compatibility
|
||||
|
||||
@@ -190,9 +233,10 @@ On acceptance: spec 05 (§2 flat effects and checking rule, §3–§5 capability
|
||||
- **Second-class capabilities** (like borrows: parameters only): prevents storing authority in data structures such as a logger, which is a common and safe pattern. Rejected.
|
||||
- **Linear capabilities by default**: makes sharing authority awkward and adds a resource discipline to every effectful call. Deferred: one-shot kinds can be added when needed.
|
||||
- **Ambient authority with a sandbox policy file**: authority invisible in the code, which is what Aji is designed to avoid. Rejected.
|
||||
- **Silent empty capabilities on a refused narrowing** (revision 1): the failure would surface far from its cause, as a permission error several calls later. Rejected for a fallible `cap.narrow`.
|
||||
- **Grants by kind** (`--grant fs.read=/data`, revision 1): cannot give two parameters of the same kind different scopes. Rejected for grants by parameter name.
|
||||
- **Missing grants as traps**: would make an environment decision part of program semantics, as fuel exhaustion would have. Rejected; `not_instantiated` mirrors `limit_exceeded`.
|
||||
|
||||
## Unresolved questions
|
||||
|
||||
- Whether `@main` may also receive non-capability configuration (arguments, environment) in format 0, or only through a capability such as `cap<process.args>`.
|
||||
- Whether the host may grant *fewer* rights than a narrowing requested silently (current text: narrowing to an unrepresentable scope yields an empty capability, never an error).
|
||||
None. Startup data (§2.2) is deferred to a later feature by decision, not left open.
|
||||
Reference in new issue
Block a user