rfc: 0006 effects and capabilities (Proposed)
Resolves Q-011 (flat effects, no discharge in format 0) and Q-012 (opaque copyable capabilities, origin only as @main parameters granted by the host, explicit narrowing without widening, not_instantiated environment outcome, intrinsic call mechanism).
This commit is contained in:
1 parent
2ddf424c14
commit
56728a534c
1 file changed
+198
@@ -0,0 +1,198 @@
|
||||
# RFC 0006: Effects and Capabilities in AIR
|
||||
|
||||
Status: Proposed
|
||||
Scope: AIR format 0, features `effects` and `capabilities`
|
||||
Language version: 0.1
|
||||
Supersedes: —
|
||||
|
||||
Resolves: Q-011 (effect hierarchy and discharge), Q-012 (capability representation and host instantiation).
|
||||
|
||||
Not in scope: the catalogue of I/O intrinsics and the representation of I/O errors (RFC 0007, resources and I/O), heap allocation and regions (RFC 0008), which build on this RFC.
|
||||
|
||||
## Motivation
|
||||
|
||||
Spec 05 separates *what code does* (effects) from *what an invocation may do* (capabilities) but leaves both unrealized in AIR. Before Phase 2 can add allocation, files or sockets, AIR needs a precise, verifier-checkable model of both, and a precise boundary between an Aji program and its host.
|
||||
|
||||
## Invariants
|
||||
|
||||
1. **No ambient authority.** An operation that acts outside the program's own memory requires a capability value as an operand. There is no global, implicit or default authority.
|
||||
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.
|
||||
|
||||
## 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:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
### 1.2 Effects are flat (Q-011)
|
||||
|
||||
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.
|
||||
|
||||
### 1.3 Checking rule
|
||||
|
||||
In a module that declares `effects`:
|
||||
|
||||
- For every `call @g(…)` in function `f`: `effects(g) ⊆ effects(f)`, otherwise `E3101` for each missing effect.
|
||||
- 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.
|
||||
|
||||
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`.
|
||||
|
||||
## 2. Capabilities (feature `capabilities`)
|
||||
|
||||
### 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`.
|
||||
|
||||
A capability is an **opaque value**:
|
||||
|
||||
- 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.
|
||||
|
||||
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.
|
||||
|
||||
### 2.2 Origin: the entry function (Q-012)
|
||||
|
||||
Capabilities enter a program only as parameters of the entry function `@main`:
|
||||
|
||||
```text
|
||||
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.
|
||||
- Compiling or verifying a module never grants authority (spec 05 §6).
|
||||
|
||||
### 2.3 Narrowing
|
||||
|
||||
`%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):
|
||||
|
||||
| Kind | Form | Meaning |
|
||||
|---|---|---|
|
||||
| `fs.read`, `fs.write` | `prefix` | restrict to a path below the current scope (`slice<u8>` operand) |
|
||||
| `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.
|
||||
|
||||
### 2.4 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.
|
||||
|
||||
## 3. Intrinsic calls (feature `capabilities`)
|
||||
|
||||
```text
|
||||
[%r =] intrinsic <name>(%a1, …, %an)
|
||||
```
|
||||
|
||||
- `<name>` is a dotted name from the intrinsic registry (`reference/intrinsics.md`); an unknown name is `E0004`.
|
||||
- Each registry entry specifies, as for any instruction (spec 07 §4): operand types, which operand is the capability and its kind, result type, required effect, ownership and borrow effects on operands, memory read/write set, trap cases, failure model, blocking behavior, and behavior in the `deterministic` profile.
|
||||
- Verification: operand types (`E1001`), capability kind (`E3201`), effect (`E3101`), borrows as for `call` (spec 08 §11.2).
|
||||
- Execution: the interpreter forwards to the host through the capability; the capability's scope is enforced by the host, never by trusting the program.
|
||||
|
||||
This RFC registers no I/O intrinsic yet. RFC 0007 adds the catalogue (console, files, sockets, clock, random) together with the error representation.
|
||||
|
||||
## 4. Runtime and interpreter
|
||||
|
||||
- A capability is represented as an index into a host-owned table of (kind, scope) entries. The index is not observable by the program.
|
||||
- Narrowing appends a new table entry. Entries are never removed during a run.
|
||||
- In the `deterministic` profile, grants refer to virtual resources (an in-memory file system, a captured stdout, a seeded clock and random source), so runs are reproducible.
|
||||
|
||||
## Valid example
|
||||
|
||||
```text
|
||||
module example.hello
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, capabilities, effects, ownership]
|
||||
|
||||
fn @main(%out: cap<io.write>) -> void
|
||||
effects [io.write]
|
||||
entry:
|
||||
call @greet(%out)
|
||||
ret
|
||||
|
||||
fn @greet(%out: cap<io.write>) -> void
|
||||
effects [io.write]
|
||||
entry:
|
||||
... ; intrinsic io.stdout_write (RFC 0007)
|
||||
ret
|
||||
```
|
||||
|
||||
## Rejected examples
|
||||
|
||||
```text
|
||||
fn @greet(%out: cap<io.write>) -> void
|
||||
effects [] ; E3101: callee or intrinsic needs io.write
|
||||
```
|
||||
|
||||
```text
|
||||
%fake = const.cap 7 ; E0004: capabilities have no constant form,
|
||||
; and no other instruction produces cap<…> from data
|
||||
```
|
||||
|
||||
```text
|
||||
%w = cap.narrow.prefix %fs_read, %p ; result is still cap<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
|
||||
```
|
||||
|
||||
## Diagnostics
|
||||
|
||||
| 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) |
|
||||
| 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.
|
||||
|
||||
## Compatibility
|
||||
|
||||
Additive under spec 07 §8.7: new type, instructions and registries inside the features `effects` and `capabilities`, which are reserved today. The `@main` rule extends spec 07a "Run result": `@main` may take capability parameters.
|
||||
|
||||
## Spec integration
|
||||
|
||||
On acceptance: spec 05 (§2 flat effects and checking rule, §3–§5 capability model, §6 instantiation), spec 07 §8.8 (types), spec 07a (feature `capabilities` and `effects` instructions, run result), spec 08 (stage order: effects and capabilities after types), spec 11 (instantiation outcome), new `reference/effects.md` and `reference/capabilities.md`, `reference/intrinsics.md` format, `reference/air-features.md` (publish both features).
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
- **Effect implication (`atomic` ⊇ `atomic.weak`)**: shorter lists, but a second semantics to learn and specify. Rejected for format 0 (§1.2).
|
||||
- **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.
|
||||
- **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).
|
||||
Reference in new issue
Block a user