Files
aji/docs/rfcs/0014-islands-frozen-arc.md
T

19 KiB
Raw Blame History

RFC 0014: Islands, Frozen Graphs and Arc (Phase 4)

Status: Proposed Target: AIR format 0 extension (publishes islands, adds arc); .aji 0.1 layer 9 Builds on: RFC 0001 (Proposed; its island sections are made concrete here), RFC 0011, RFC 0012, RFC 0013

Summary

Three verifier-visible ownership forms for object graphs, delivered through every layer at once (spec, AIR, verifier, interpreter, .aji, Quick Reference, conformance; owner decision after P6A-9):

Form What it is Class
island<T> a uniquely owned, closed, mutable graph whose root has type T linear; Send, Share
iref<T> a handle to an object of type T inside some island copyable plain data
frozen<T> an island made deeply immutable by island.freeze linear; Send, Share
arc<T> explicitly counted shared ownership of a Share value linear handle; Send, Share

A cyclic graph is built in an island with iref edges, moved to another task in O(1) through the existing channel and spawn rules, frozen without copying, and shared by arc with explicit clone and release. There is no garbage collection, no implicit reference count, no hidden destructor and no shared mutable memory outside synchronization.

Motivation

Per-object ownership (own<T>) makes cyclic and arena-shaped data awkward, and regions give bulk lifetime but cannot move between tasks or be shared. Phase 3 can move values and lend borrows within a scope, but there is no way to:

  • build a graph with cycles and move it to another task without copying;
  • turn a graph built by one task into immutable data many tasks read for longer than one scope;
  • share one immutable value among tasks that are not in the same scope.

These are the island-pipeline workloads of the Phase 4 exit criterion: parsers, message graphs, request contexts and actor-like state.

Invariants

  1. Closure. An island holds only island-storable values: plain data and iref handles. It never points into ordinary mutable memory outside itself (RFC 0001 invariant).
  2. No outside mutable alias. Every access to an island's objects goes through a borrow of the island. While a mutable access is live, no other access to the island exists.
  3. Transfer invalidates the sender. After an island, a frozen graph or an arc is moved, the sender holds nothing that reaches it. This is the ordinary linear-move rule, so a use after the move is E2001.
  4. Freeze is irreversible. No safe operation yields a mutable borrow of a frozen graph.
  5. Explicit counting. An arc's count changes only at arc.clone and arc.release. Memory is freed only at an explicit island.destroy, frozen.destroy or the last arc.release.
  6. Handles cannot be forged into memory errors. An iref is checked against the island it is used with. Using it with another island traps (T0010). Island identities are never reused within an execution, so a stale handle can never reach a later island's objects.
  7. Determinism. Island identities, slots and arc counts are deterministic. Transfer and freeze cost O(1) in the graph size.

Decisions for the owner

D1. iref provenance: checked at run time

Recommended. An iref<T> is plain data: the island identity and a slot. It is dereferenced only together with a borrow of an island. The interpreter checks that the handle belongs to that island, and a mismatch traps T0010 (trap.foreign_iref).

Alternative. Track island identity in the verifier. Static provenance does not survive function boundaries, aggregates or channels without island-indexed types (dependent on values), which grows the verifier state and the agent's reasoning a great deal. The runtime check costs one comparison, keeps every check intraprocedural, and is memory-safe: an island is never freed while a borrow of it is live, and identities are never reused.

D2. What an island may contain

Recommended. Island-storable values are:

  • integers, bool and built-in enums;
  • iref<U>;
  • structs, enums and arrays of island-storable values.

Everything else is E2111 (island.foreign_mutable_edge, field found):

  • borrows;
  • own, own_array, region;
  • resources and endpoints;
  • tasks, scopes, shields, guards and mutexes;
  • cap<K>;
  • islands, frozen graphs and arcs.

This keeps the closure invariant a property of types, with no ownership analysis. Allowing capabilities or nested islands is left open (Q1).

D3. Island memory

Recommended. island.create takes a cap<mem.alloc>. The island charges that authority for every object, as own does: live bytes, and freeing returns the budget. island.alloc is fallible (alloc_error). Objects are not freed one by one; island.destroy frees them all, and freezing keeps them. This is bulk lifetime, as for regions.

D4. Access through a borrow of the island

Recommended.

  • island.borrow %ib, %r gives ref<U> from ref<island<T>> and mut<U> from mut<island<T>>.
  • The result is a child borrow of %ib, with the usual rules: it ends before %ib (E2102); a mutable child is exclusive (E2101 for any other access through %ib while it is live).
  • Two irefs may name the same object, so two simultaneous mut accesses into one island are never accepted. Read accesses may coexist.

This reuses the borrow machinery of Phases 1–3 unchanged.

D5. Freeze

Recommended. island.freeze %i consumes the island and returns frozen<T> over the same storage, in O(1). frozen.borrow %fb, %r gives ref<U> from ref<frozen<T>> and nothing else. frozen<T> is linear, with one owner, and is shared by lending ref<frozen<T>> to tasks (it is Share) or by putting it in an arc (D6). frozen.destroy frees it.

D6. arc<T> (new feature arc)

Recommended.

  • arc.new %v, %mem (fallible) requires T: Share, otherwise E4002.
  • In format 0, T contains no linear value other than frozen<U>, otherwise E1001. Releasing the last arc then frees only memory, never anything with an observable effect; that is what keeps the release from being a hidden destructor.
  • arc.clone %ar takes ref<arc<T>> and returns a new arc<T> (count + 1).
  • arc.release %a consumes the arc (count − 1). At zero, T's memory is freed, together with a contained frozen graph.
  • arc.borrow %ar gives ref<T>, a child borrow.

The count is synchronized inside the runtime. It is not a user atomic, so it introduces no memory-ordering choice. arc<mutex<T>> (shared mutable state beyond one scope) is deferred (Q2).

D7. Transfer

Recommended.

  • island<T>, frozen<T> and arc<T> move through channel.send, task.spawn arguments and returns under the existing rules.
  • They are Send.
  • They are allowed as channel elements and mutex contents: they contain no borrow, task or guard.
  • Moving the value moves a handle (O(1)).
  • ref<island<T>> and ref<frozen<T>> may be lent to tasks: both are Share, since only reads go through a shared borrow.

D8. .aji syntax: an explicit container borrow, as for regions

Recommended. Every operation that yields a borrow into a container takes an explicit borrow binding of that container, which outlives the borrows derived from it. This is the rule spec 15 §8 already applies to regions, stated generally:

let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap;
{
    mut g = isl;                                   // the island borrow, to the end of this block
    let root = island.root(g);
    let n = island.alloc(g, Node { value: 2, next: Link::To(root) }) or trap;
    {
        mut node = island.at(g, root);             // mut<Node>, a child of g
        node.next = Link::To(n);                   // a cycle
    }
}
let f = island.freeze(move isl);
Source AIR
island<T>, iref<T>, frozen<T>, arc<T> the same types
island.create(v, mem) (outcome match / or trap) island.create
island.root(g), frozen.root(fr) island.root, frozen.root
island.alloc(g, v) (outcome match / or trap) island.alloc
ref x = island.at(g, r); / mut x = island.at(g, r); island.borrow on the binding g
ref x = frozen.at(fr, r); frozen.borrow
island.freeze(move i), island.destroy(move i), frozen.destroy(move f) the same
arc.new(v, mem) (outcome match / or trap), arc.clone(a), arc.release(move a) the same; clone borrows a for the operation
ref x = arc.at(ar); arc.borrow on the binding ar

Container operands.

  • Bare name (borrowed for the one operation, as for resources): island.root, frozen.root and arc.clone. Their results are plain values (iref, arc), not borrows.
  • Explicit borrow binding of the container: island.at, frozen.at, arc.at and island.alloc. Passing the container itself is E7005 with a hint, as for regions.

Accesses produce borrows. island.at, frozen.at and arc.at appear only as the place of a ref/mut binding. Elsewhere they are E7006.

D9. Diagnostics

Code Use
E2111 (island.foreign_mutable_edge, registered) island content that is not island-storable; field found names the type
E2110 (island.escape, registered) stays reserved: a borrow derived from an island can no longer escape any other way than E2102, and with D1 an iref is plain data. It is kept for a later static-provenance mode (Q3)
T0010 trap.foreign_iref (new) an iref used with an island it does not belong to
E4002 arc of a non-Share value
E1001 an arc holding a linear value other than frozen<…>

E2001/E2002/E3001/E3002 apply to islands, frozen graphs and arcs as linear values. E2101/E2102/E4101/E4302 apply to their borrows and loans.

D10. Features

Feature Status Depends on Contents
islands published (reserved until now) capabilities, effects, heap, ownership, resources island<T>, iref<T>, frozen<T>, island.*, frozen.*
arc new, published capabilities, effects, heap, ownership, resources arc<T>, arc.*

islands drops its registered dependency on regions, which reserved features may change before publication (DD-014). Islands do not use region instructions; they share the bulk-lifetime idea only.

Types and instructions (AIR)

Instruction Operands → result Ownership and memory Effect Verifier Runtime / trap
island.create %v, %mem, ok -> @ok, err -> @err (terminator) T (consumed), cap<mem.alloc>; @ok(island<T>), @err(alloc_error) %v becomes object 0; charges size(T) plus the island header mem.alloc T island-storable (E2111) new island identity; budget failure → @err
%r = island.root %ib ref<island<T>> or mut<island<T>> → iref<T> none — — object 0
island.alloc %ib, %v, ok -> @ok, err -> @err (terminator) mut<island<T>>, U (consumed); @ok(iref<U>), @err(alloc_error) new object; charges the island's authority mem.alloc U island-storable (E2111) —
%p = island.borrow %ib, %r ref<island<T>> → ref<U>, mut<island<T>> → mut<U>; %r: iref<U> child borrow of %ib — ends before %ib (E2102); a mut child is exclusive (E2101) T0010 if %r is not of this island
%f = island.freeze %i island<T> (consumed) → frozen<T> same storage, now immutable — — O(1)
island.destroy %i island<T> (consumed) frees every object; returns the bytes — no live borrow (E2101) —
%r = frozen.root %fb ref<frozen<T>> → iref<T> none — — —
%p = frozen.borrow %fb, %r ref<frozen<T>>, iref<U> → ref<U> child borrow — E2102 T0010
frozen.destroy %f frozen<T> (consumed) frees the graph — no live borrow —
arc.new %v, %mem, ok -> @ok, err -> @err (terminator) T (consumed), cap<mem.alloc>; @ok(arc<T>), @err(alloc_error) count 1 mem.alloc T: Share (E4002); no linear value but frozen<…> (E1001) —
%a2 = arc.clone %ar ref<arc<T>> → arc<T> count + 1 — — synchronized
arc.release %a arc<T> (consumed) count − 1; at 0 frees T (and a contained frozen graph) — — synchronized; never fails
%p = arc.borrow %ar ref<arc<T>> → ref<T> child borrow — E2102 —

Size accounting (spec 08 §7.1) is unchanged. iref<T> is 16 bytes (identity and slot). island<T>, frozen<T> and arc<T> are handles of 8 bytes.

Verifier rules

  • Classes. island<T>, frozen<T> and arc<T> are linear values: consumed exactly once on every path, never copied. iref<T> is copyable.

  • Storability. The island-storable predicate (D2) is checked at island.create and island.alloc (E2111). The arc content rule (D6) is checked at arc.new (E4002 or E1001).

  • Borrows. Accesses are child borrows of the container borrow (D4, D5, D6). No new verifier state is needed beyond the existing borrow tree.

  • Send/Share (spec 04 §35, new rows):

    Type Send Share
    island<T>, frozen<T>, arc<T> yes yes
    iref<T> yes yes

    Island and frozen contents are plain data, and arc<T> already requires T: Share.

  • Element rules. Channel elements and mutex contents (spec 04 §38.1) accept islands, frozen graphs and arcs.

Runtime rules (interpreter)

  • Islands. An island is a table of object cells with an identity drawn from a per-execution counter that is never reused. An iref is the identity and a slot. island.borrow checks the identity, then makes a place into the cell.
  • Transfer. Moving an island, frozen graph or arc moves the handle: no copy, O(1). The execution heap's collector copies island tables like other live state, preserving identity. Collection is amortized and independent of transfers.
  • Freeze marks the table immutable and returns a frozen handle to it.
  • arc. The count lives with the shared value. arc.clone and arc.release change it in the deterministic order the scheduler runs them. The count after each change appears in the execution trace (spec 11 §2), so P4-6 can test it.
  • Budgets. An island charges its authority at create and alloc and returns the bytes at destroy, or at the last arc.release that frees a frozen graph.
  • Traps. T0010 is deterministic: the identity check fails the same way on every run.

Valid example (.aji)

A ring of three nodes is built in an island and sent to a worker, which walks it. The parent then gets the island back, freezes it, and shares it through an arc with two readers.

struct Node { value: u64, next: Link }
enum Link { End, To(iref<Node>) }

fn ring(heap: cap<mem.alloc>) -> island<Node>
effects [mem.alloc]
{
    let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap;
    {
        mut g = isl;
        let a = island.root(g);
        let b = island.alloc(g, Node { value: 2, next: Link::To(a) }) or trap;
        let c = island.alloc(g, Node { value: 3, next: Link::To(b) }) or trap;
        mut first = island.at(g, a);
        first.next = Link::To(c);                   // the cycle a → c → b → a
    }
    move isl
}

fn walk(isl: island<Node>, steps: u64) -> island<Node>
effects []
{
    var sum: u64 = 0;
    {
        ref g = isl;
        var at = island.root(g);
        for i in 0..steps {
            ref node = island.at(g, at);
            sum = sum + node.value;
            at = match node.next { Link::End => at, Link::To(n) => n };
        }
    }
    move isl
}

(The full program, with the transfer through a channel and the arc readers, is the Phase 4 reference program island-pipeline.)

Rejected examples

Program Code
island.create(own.new(1u64, heap) or trap, heap) (an owner inside an island) E2111
island.alloc(isl, v) with the island itself instead of a borrow binding E7005
mut x = island.at(g, a); mut y = island.at(g, b);, both live E2101
using isl after channel.send(tx, move isl) E2001
mut x = frozen.at(fr, r); E7005 (frozen graphs give ref only)
arc.new(mutex.make(0u64), heap) E1001
an iref from island A with island B, at run time T0010
an island never destroyed, frozen, moved or returned E3002

Diagnostics impact

  • New trap T0010 trap.foreign_iref.
  • E2111 becomes reachable. E2110 stays registered and unused (D9).
  • No new E7xxx: the source checks reuse E7005, E7006 and E7003.

Deterministic interpreter behavior

Identities, slots and counts are deterministic. Transfer, freeze and release are not scheduling points; their order is the program's. No outcome depends on addresses. The execution-heap collector preserves identity (the --collect-every-step conformance mode covers islands).

Compatibility

  • Existing modules are unaffected: islands was reserved, so no published module uses it, and arc is new.
  • AIR format 0 is extended only as spec 07 §8.7 allows: new types, instructions and a feature.
  • Spec 03 §12–14 becomes concrete. Spec 04 §13 points to D7. The Rust-like Island<T>/Frozen<T>/Arc<T> spellings in prose become the lowercase source types.

Agent complexity impact

  • New concepts:

    • a closed graph addressed by handles;
    • the container borrow (already known from regions);
    • irreversible freeze;
    • explicit shared ownership.
  • New verifier state: none beyond the borrow tree. Storability and Share are type predicates.

  • New syntax and instructions:

    • 4 types and 13 instructions;
    • in source, 13 operations and one new place form: an island.at/frozen.at/arc.at call as the place of a ref/mut binding.
  • Interactions: channels and spawn (transfer), loans (sharing a frozen graph), budgets.

  • Repairability: misuse surfaces as existing borrow and linearity codes, or as T0010 with a source location.

  • Could existing primitives express it? No:

    • cyclic graphs need handles;
    • a region cannot move between tasks;
    • an own tree cannot share.
  • Bundle growth (estimates, measured at implementation):

    Bundle Estimate
    Quick Reference +500–700 tokens (one section, one example)
    Source Reference Bundle +2.5–3.5k (memory.md: spec 03 §12–14; concurrency.md: D7 rows)
    AIR bundle +3–4k (≈ +10 %)
  • New diagnostics an agent sees in source: E2111, T0010.

Alternatives considered

  • Static island provenance (D1): rejected for format 0, see D1.
  • A per-island garbage collector: rejected, since lifetime stays explicit and bulk.
  • Implicit reference counting for frozen data: rejected (principle), hence arc with explicit clone/release.
  • Freezing into arc directly (island.freeze returning an arc): rejected. It would join two decisions (immutability, sharing) that are separately useful: a frozen<T> lent by ref needs no count.
  • Allowing owners and resources inside islands: deferred. Transfer would stay O(1), but freeing an island would then have to close or free them, which is a hidden destructor.

Unresolved questions

  • Q1. Capabilities or nested islands inside islands.
  • Q2. arc<mutex<T>>, shared mutable state beyond one scope (needs the mutex content and destruction rules spelled out).
  • Q3. A later static-provenance mode reporting E2110 instead of the T0010 trap.
  • Q4. O(1) detach of a sub-island (RFC 0001).
  • Q5. Memory-mapped islands (RFC 0001).