Files
aji/docs/rfcs/0006-effects-and-capabilities.md
dev fb4338cea6 spec: accept RFC 0006 and integrate effects and capabilities
- spec 05 §9-§12: flat effects, checking rule, capability model,
  instantiation by parameter name, lifetime, fallible narrowing,
  intrinsic calls, runtime
- spec 07 §8.9, 07a (cap.narrow, intrinsic, run result), 08 stage
  order, 11 not_instantiated outcome
- new reference/effects.md and reference/capabilities.md (kinds,
  scopes, built-in cap_error)
- features effects and capabilities published
- Q-011, Q-012 resolved
2026-10-04 13:36:46 +03:00

16 KiB
Raw Permalink Blame History

RFC 0006: Effects and Capabilities in AIR

Status: Accepted (revision 2, after owner review items 1–8)
Scope: AIR format 0, features effects and capabilities
Language version: 0.1
Integrated into: spec 05 §2, §9–§12; spec 07 §8.9; spec 07a; spec 08 §14; spec 11 §7; reference/effects.md, reference/capabilities.md, reference/air-features.md
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, 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. The registry records for each effect whether exercising it requires authority, and which capability kind provides it:

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.

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

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.

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.

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, which lists for each kind the effects it authorizes (see §1.1) and its narrowing forms. An unregistered kind is E0004.

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

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.

2.2 Origin: the entry function (Q-012)

Capabilities enter a program only as parameters of the entry function @main:

fn @main(%out: cap<io.write>, %src: cap<fs.read>) -> void
effects [fs.read, io.write]
  • @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:

    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:

    {"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).

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.

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:

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

3. Intrinsic calls (feature capabilities)

[%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

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

fn @greet(%out: cap<io.write>) -> void
effects []                               ; E3101: callee or intrinsic needs io.write
    %fake = const.cap 7                  ; E0004: capabilities have no constant form,
                                         ; and no other instruction produces cap<…> from data
    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>
    cap.narrow.budget %fs_read, %n, ok -> @a, err -> @b   ; 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), 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 with a reason, 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.
  • 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

None. Startup data (§2.2) is deferred to a later feature by decision, not left open.