Files
aji/docs/rfcs/0014-islands-frozen-arc.md
T
dev ca84ab4348 rfc: 0014 accepted (revision 2) and integrated into the spec
Owner review: D1-D4, D7-D9 accepted; D5 frozen.release, no thaw; D6
arc.from_frozen (island -> frozen -> arc, one graph identity), ArcSafe
(not Share alone; no arc<mutex>, no nested arc); D7 island Send, not
Share; D10 islands -> heap, arc -> islands; added: never-reused graph
identity, IslandSafe, the quota lifecycle, iref equality.

Integrated: spec 02 §6 (IslandSafe, ArcSafe), 03 §12-§14 and new
§22-§24 (instruction contracts, quota), 04 §13/§35/§38.1, 07a (features
islands, arc), 15 §8 (layer 9 syntax, explicit container borrows),
error codes (T0010, registered in src/diag.zig), type-properties. Phase 4
plan: P4-0, P4-1 done.
2026-10-05 21:57:08 +03:00

381 lines
20 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# RFC 0014: Islands, Frozen Graphs and Arc (Phase 4)
Status: Accepted (revision 2, after owner review). D1–D4, D7–D9 accepted. D5, D6 and D10 revised: `frozen.release`, no thaw; `arc.from_frozen` instead of `arc<frozen<T>>`; `ArcSafe`; `islands → heap`, `arc → islands`. Added: stable identity, `IslandSafe`, the quota lifecycle and `iref` equality.
Target: AIR format 0 extension (publishes `islands`, adds `arc`); `.aji` 0.1 layer 9
Builds on: RFC 0001 (its island sections are made concrete here), RFC 0011, RFC 0012, RFC 0013
Integrated into:
- spec 02 §6;
- spec 03 §12–§14, §22–§24;
- spec 04 §13, §35, §38.1;
- spec 07a (features `islands`, `arc`);
- spec 15 §5, §8;
- `reference/error-codes.md` (`T0010`);
- `reference/type-properties.md`.
## Summary
One immutable-graph lifecycle in three ownership modes, plus a handle type. It is delivered through every layer at once: spec, AIR, verifier, interpreter, `.aji`, Quick Reference and conformance (owner decision after P6A-9).
```text
island<T> mutable, unique "I want to change it"
│ island.freeze O(1)
▼
frozen<T> immutable, unique owner, Share "I want to read it in parallel"
│ arc.from_frozen O(1)
▼
arc<T> immutable, shared ownership "several independent owners"
```
| Type | What it is | Class |
|---|---|---|
| `island<T>` | a uniquely owned, closed, mutable graph whose root has type `T` | linear; `Send`, not `Share` |
| `frozen<T>` | the same graph, immutable, with one owner | linear; `Send`, `Share` |
| `arc<T>` | the same graph, immutable, with counted shared owners; each handle is linear | linear handle; `Send`, `Share` |
| `iref<T>` | an opaque handle to an object of type `T` inside a graph | copyable; `Send`, `Share` |
A graph keeps its identity through every mode, so an `iref` stays valid from island to frozen to arc. There is:
- no garbage collection;
- no implicit reference count;
- no thaw;
- no hidden destructor (releasing a graph frees memory and nothing else);
- no shared mutable memory outside synchronization.
## Motivation
Per-object ownership (`own<T>`) makes cyclic and arena-shaped data awkward. Regions give bulk lifetime but cannot move between tasks or be shared. Phase 3 moves values and lends borrows within a scope, but cannot:
- build a cyclic graph and move it to another task without copying;
- turn a graph built by one task into immutable data that many tasks read for longer than one scope;
- give one immutable value several independent owners.
These are the island-pipeline workloads of the Phase 4 exit criterion: parsers, message graphs, request contexts and actor-like state.
## Invariants
1. **Closure.** A graph holds only `IslandSafe` values. They have no edge into mutable memory outside the graph and carry no authority.
2. **No outside mutable alias.** Every access to a graph's objects goes through a borrow of the graph's owner. While a mutable access is live, no other access to the island exists.
3. **Transfer invalidates the sender.** After a graph's owner is moved, the sender holds nothing that reaches it. This is the linear-move rule, and a later use is `E2001`.
4. **Freeze is irreversible.** No operation yields a mutable borrow of a frozen or arc graph.
5. **Explicit counting.** An arc's count changes only at `arc.clone` and `arc.release`. A graph's memory is freed only at `island.destroy`, `frozen.release` or the last `arc.release`, and that frees memory only.
6. **Identity.** A graph's identity is unique within an execution and never reused. An `iref` from one graph used with another traps (`T0010`). A stale `iref` (its graph released) is harmless: without an owner of that graph it cannot be used.
7. **Determinism.** Identities, slots and counts are deterministic. Freeze, conversion and transfer cost O(1) in the graph size.
## Decisions (revision 2)
### D1. `iref` is opaque; identity is never reused (accepted)
An `iref<T>` is an opaque pair `{graph identity, slot}`. The identity is unique for the execution: it comes from a counter that is never reset or reused. A handle cannot be:
- constructed from an integer;
- cast to an integer;
- inspected as bytes;
- modified.
It is checked against the graph whenever it is used to access one (`T0010` on a mismatch).
**Equality.** `iref.eq %a, %b` (source `a == b`, `a != b`) compares identity and slot. It is always defined. Handles of different graphs are simply unequal, never a trap. Hashing is deferred.
### D2. `IslandSafe` (accepted, as a structural property)
`IslandSafe` is derived structurally (spec 02 §6).
**IslandSafe:**
- integers, `bool`, built-in enums (and floats, when the language has them);
- `iref<U>`;
- arrays, structs and enums whose elements, fields and payloads are `IslandSafe`.
**Not IslandSafe:**
- `ref`, `mut`, `slice`, `slice_mut`;
- `own`, `own_array`, `region`;
- `island`, `frozen`, `arc`;
- resources and endpoints;
- `task`, `scope`, `shield`, `guard`, `mutex`;
- `cap<K>`.
A graph contains only `IslandSafe` values (`E2111` otherwise, field `found`). This is what makes freeze, conversion and transfer provably O(1): a graph never holds outside authority or a mutable ownership edge.
### D3. Memory and quota (accepted, lifecycle stated)
A graph charges the `cap<mem.alloc>` it was created with: live bytes, as for `own`. The ownership mode does not change the budget.
| Operation | Live bytes |
|---|---|
| `island.create`, `island.alloc` | + the object |
| `island.freeze` | unchanged |
| `arc.new` | + the value and the count |
| `arc.from_frozen` | + the count only |
| `arc.clone` | unchanged |
| `arc.release`, not the last | unchanged |
| `arc.release`, the last | − the graph and the count |
| `frozen.release` | − the graph |
| `island.destroy` | − the graph |
Objects are never freed one by one: bulk lifetime, as for regions. `island.alloc` is fallible (`alloc_error`).
### D4. One mutable access per island (accepted, conservative on purpose)
- A `ref<island<T>>` gives any number of shared children.
- A `mut<island<T>>` gives at most one live mutable child; any other access through it while that child lives is `E2101`.
Two irefs that happen to name different objects are not treated as disjoint: the verifier does not try to prove it. A proved-disjointness mechanism may come later if benchmarks need it.
### D5. Freeze and `frozen.release` (accepted with clarification)
- `island.freeze %i` consumes the island (so no borrow of it is live) and returns `frozen<T>` over the same storage. It is O(1), with no traversal; the identity is kept, irefs stay valid, and the quota is unchanged.
- `frozen.release %f` frees the whole graph and returns its bytes.
- There is no thaw in AIR format 0.
### D6. `arc.from_frozen` and `ArcSafe` (revised)
`arc<T>` is a shared, immutable graph with root type `T`. It is never `arc<frozen<T>>`: one graph, one ownership wrapper.
| Operation | Meaning |
|---|---|
| `arc.from_frozen %f` | consumes `frozen<T>` and gives the first `arc<T>` handle of the same graph; O(1); the identity is kept |
| `arc.new %v, %mem` (fallible) | a one-object shared graph whose root is `%v` |
| `arc.clone %ar` | takes `ref<arc<T>>` and gives a new linear handle (count + 1) |
| `arc.release %a` | consumes one handle (count − 1); the last release frees the graph |
| `arc.root %ar`, `arc.borrow %ar, %r` | read access, as for frozen graphs |
**`ArcSafe`** is what an `arc` may hold. The last `arc.release` must be able to destroy the value by freeing memory alone: no user action, no resource action. In format 0, `ArcSafe` is exactly `IslandSafe`. It is named separately so that the two can diverge later. `T: Share` alone is not enough: a mutex is `Share`, yet `arc<mutex<T>>` is deliberately excluded. So there is no:
- `arc<file<…>>` or `arc<stream>`;
- `arc<cap<…>>`;
- `arc<own<T>>`;
- `arc<mutex<T>>`;
- `arc<sender<T>>`;
- nested `arc<arc<T>>`.
A value that is not `ArcSafe` is `E2111`, the same code as an island's content, since both are graph contents. The count is synchronized inside the runtime. It is not a user atomic and adds no memory-ordering choice.
### D7. Transfer (accepted, with the corrected Share)
| Type | Send | Share |
|---|---|---|
| `island<T>` | yes | **no** |
| `frozen<T>` | yes | yes |
| `arc<T>` | yes | yes |
| `iref<T>` | yes | yes |
An island's owner may mutate it, so a `ref<island<T>>` is not lent to tasks: freeze first. Owners move through `channel.send`, `task.spawn` arguments and returns under the existing rules, in O(1). They are allowed as channel elements and mutex contents (spec 04 §38.1). `ref<frozen<T>>` and `ref<arc<T>>` may be lent to tasks.
### D8. Explicit container borrows in `.aji` (accepted)
An operation that yields a borrow into a graph takes a named borrow binding of the graph's owner, which outlives the borrows derived from it. This is the region rule of spec 15 §8, stated generally. A container borrow written inline (`island.at(mut isl, r)`) is `E7006`. The owner itself (`island.at(isl, r)`) is `E7005`, with a hint.
```aji
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>`, `frozen<T>`, `arc<T>`, `iref<T>` | the same types |
| `island.create(v, mem)` (outcome match / `or trap`) | `island.create` |
| `island.alloc(g, v)` (outcome match / `or trap`) | `island.alloc` |
| `island.root(g)`, `frozen.root(g)`, `arc.root(g)` | `island.root`, `frozen.root`, `arc.root` |
| `ref x = island.at(g, r);` / `mut x = island.at(g, r);` | `island.borrow` |
| `ref x = frozen.at(g, r);`, `ref x = arc.at(g, r);` | `frozen.borrow`, `arc.borrow` |
| `island.freeze(move i)`, `island.destroy(move i)`, `frozen.release(move f)` | the same |
| `arc.from_frozen(move f)`, `arc.new(v, mem)` (outcome match / `or trap`) | the same |
| `arc.clone(a)` (`a` borrowed for the operation), `arc.release(move a)` | the same |
| `a == b`, `a != b` on `iref<T>` | `iref.eq`, then `not` |
**Container operands.**
- **Named borrow binding of the owner:** `.root`, `.at` and `island.alloc`.
- **Owner by name** (a fresh handle, no borrow result): `arc.clone`.
**Where `.at` may appear.** `island.at`, `frozen.at` and `arc.at` appear only as the place of a `ref`/`mut` binding (`E7006` elsewhere). `frozen.at` and `arc.at` bind `ref` only (`E7005` for `mut`).
### D9. Diagnostics (accepted)
| Code | Use |
|---|---|
| `T0010` `trap.foreign_iref` (new) | an `iref` used to access a graph it does not belong to |
| `E2111` (`island.foreign_mutable_edge`) | a graph content that is not `IslandSafe`/`ArcSafe`; field `found` |
| `E2110` (`island.escape`) | stays registered and unused; reserved for a static-provenance mode (Q3) |
| `E4002` | lending `ref<island<T>>` to a task (not `Share`) |
`E2001`/`E2002`/`E3001`/`E3002` apply to graph owners as linear values. `E2101`/`E2102`/`E4101`/`E4302` apply to their borrows and loans.
### D10. Features (accepted with dependencies)
| Feature | Depends on | Contents |
|---|---|---|
| `islands` (published; reserved until now) | `heap` (and its closure) | `island<T>`, `frozen<T>`, `iref<T>`, `island.*`, `frozen.*`, `iref.eq` |
| `arc` (new, published) | `islands` | `arc<T>`, `arc.*` |
`regions` is not a dependency (DD-014 allows changing a reserved feature's dependencies before publication).
## 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` is the root object; charges its size and the graph header | `mem.alloc` | `T: IslandSafe` (`E2111`) | a new identity; over budget → `@err` |
| `island.alloc %ib, %v, ok -> @ok, err -> @err` (terminator) | `mut<island<T>>`, `U` (consumed); `@ok(iref<U>)`, `@err(alloc_error)` | a new object; charges the graph's authority | `mem.alloc` | `U: IslandSafe` (`E2111`) | — |
| `%r = island.root %ib` | `ref`/`mut<island<T>>` → `iref<T>` | — | — | — | the root object |
| `%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` |
| `%f = island.freeze %i` | `island<T>` (consumed) → `frozen<T>` | same storage and identity; quota unchanged | — | — | O(1) |
| `island.destroy %i` | `island<T>` (consumed) | frees the graph | — | — | — |
| `%r = frozen.root %fb` | `ref<frozen<T>>` → `iref<T>` | — | — | — | — |
| `%p = frozen.borrow %fb, %r` | `ref<frozen<T>>`, `iref<U>` → `ref<U>` | child borrow | — | `E2102` | `T0010` |
| `frozen.release %f` | `frozen<T>` (consumed) | frees the graph | — | — | — |
| `%a = arc.from_frozen %f` | `frozen<T>` (consumed) → `arc<T>` | same storage and identity; count 1; + count bytes | — | — | O(1) |
| `arc.new %v, %mem, ok -> @ok, err -> @err` (terminator) | `T` (consumed), `cap<mem.alloc>`; `@ok(arc<T>)`, `@err(alloc_error)` | a one-object graph, count 1 | `mem.alloc` | `T: ArcSafe` (`E2111`) | — |
| `%a2 = arc.clone %ar` | `ref<arc<T>>` → `arc<T>` | count + 1 | — | — | synchronized |
| `arc.release %a` | `arc<T>` (consumed) | count − 1; at 0 frees the graph and the count | — | — | synchronized; never fails |
| `%r = arc.root %ar` | `ref<arc<T>>` → `iref<T>` | — | — | — | — |
| `%p = arc.borrow %ar, %r` | `ref<arc<T>>`, `iref<U>` → `ref<U>` | child borrow | — | `E2102` | `T0010` |
| `%c = iref.eq %a, %b` | `iref<U>`, `iref<U>` → `bool` | — | — | — | identity and slot equal |
**Sizes** (spec 08 §7.1): an `iref<T>` is 16 bytes. `island<T>`, `frozen<T>` and `arc<T>` are 8-byte handles.
## Verifier rules
- **Classes.** `island<T>`, `frozen<T>` and `arc<T>` are linear, function-unbound values: they are parameters, returns, block arguments, channel elements and mutex contents. They are not aggregate members (`linear_aggregates` stays reserved). `iref<T>` is copyable and `IslandSafe`.
- **Properties.** `IslandSafe` is checked at `island.create`/`island.alloc`, and `ArcSafe` at `arc.new` (`E2111`).
- **Borrows.** Accesses are child borrows of the container borrow (D4–D6). The borrow tree of Phases 1–3 is unchanged, and no new verifier state is needed.
- **`Send`/`Share`.** The rows of D7 are added to spec 04 §35.
## Runtime rules (interpreter)
- **Graphs.** A graph is a table of object cells with an identity from a per-execution counter that is never reused. An `iref` is the identity and a slot. Accesses check the identity, then make a place into the cell. Frozen and arc graphs give only `ref` places.
- **Mode changes and transfer.** Moving an owner moves a handle. Freeze marks the table immutable. `arc.from_frozen` adds a count. All are O(1).
- **The execution heap.** Graph tables live in the execution heap. Its collector copies them like other live state, preserving identity. The cost is amortized and independent of transfers.
- **Counts.** An arc's count changes in the deterministic order the scheduler runs `clone` and `release`. Each change appears in the execution trace (spec 11 §2).
- **Budgets.** They follow D3.
- **`T0010`** is deterministic.
## Valid example (`.aji`)
A ring of three nodes is built in an island and walked.
```aji
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(f: ref<frozen<Node>>, steps: u64) -> u64
effects []
{
var sum: u64 = 0;
var at = frozen.root(f);
for i in 0..steps {
ref node = frozen.at(f, at);
sum = sum + node.value;
at = match node.next { Link::End => at, Link::To(n) => n };
}
sum
}
```
The Phase 4 reference program `island-pipeline` covers the rest:
- a producer task builds the island and sends it through a channel;
- the consumer freezes it and lends it to two readers;
- it converts the frozen graph with `arc.from_frozen`, clones the arc for a late reader, and releases every handle.
## Rejected examples
| Program | Code |
|---|---|
| `island.create(own.new(1u64, heap) or trap, heap)` | `E2111` (not `IslandSafe`) |
| `arc.new(mutex.make(0u64), heap)` | `E2111` (not `ArcSafe`) |
| `island.alloc(isl, v)`, the island itself | `E7005` |
| `mut x = island.at(mut isl, r);`, an inline container borrow | `E7006` |
| `mut x = island.at(g, a); mut y = island.at(g, b);`, both live | `E2101` |
| `mut x = frozen.at(g, r);` | `E7005` (frozen graphs give `ref` only) |
| using `isl` after `channel.send(tx, move isl)` | `E2001` |
| `task.spawn(s, read, g)` with `g: ref<island<T>>` | `E4002` |
| an `iref` of graph A used with graph B, at run time | `T0010` |
| a graph owner never destroyed, released, frozen, converted, moved or returned | `E3002` |
## Diagnostics impact
- New trap `T0010` `trap.foreign_iref`.
- `E2111` becomes reachable and also covers `ArcSafe`. `E2110` stays registered and unused.
- No new `E7xxx`: the source checks use `E7005` and `E7006`.
## Deterministic interpreter behavior
Identities, slots and counts are deterministic. Freeze, conversion, transfer and release are not scheduling points; their order is the program's. No outcome depends on addresses. The collect-every-step conformance mode covers graphs.
## Compatibility
- No published module changes: `islands` was reserved and `arc` is new.
- AIR format 0 is extended as spec 07 §8.7 allows: new types, instructions and features.
- Spec 03 §12–§14 become concrete.
## Agent complexity impact
- **New concepts:**
- a closed graph addressed by opaque handles;
- three ownership modes of one graph, chosen by intent (change, parallel read, several owners);
- the container borrow, already known from regions.
- **New verifier state:** none beyond the borrow tree. `IslandSafe`, `ArcSafe` and `Share` are type predicates.
- **New syntax and instructions:**
- 4 types and 16 instructions;
- in source, 15 operations plus `==`/`!=` on `iref`;
- one new place form: `island.at`, `frozen.at` or `arc.at` as the place of a `ref`/`mut` binding.
- **Interactions:** channels and spawn (transfer), loans (lending a frozen graph or an arc), budgets.
- **Repairability:** misuse surfaces as existing borrow and linearity codes, `E2111` with `found`, or `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 be shared.
- **Bundle growth (estimates, measured in P4-9):**
| Bundle | Estimate |
|---|---|
| Quick Reference | +600–800 tokens (one section, one example) |
| Source Reference Bundle | +3–4k |
| AIR bundle | +3–4k (≈ +10 %) |
- **New diagnostics an agent sees in source:** `E2111`, `T0010`.
## Alternatives considered
- **Static graph provenance** (D1): rejected for format 0, because it does not survive function boundaries, aggregates or channels without value-indexed types.
- **`arc<frozen<T>>`** (revision 1): replaced by `arc.from_frozen`, which gives one graph and one wrapper.
- **A per-graph garbage collector:** rejected, since lifetime stays explicit and bulk.
- **Implicit reference counting:** rejected (principle).
- **Owners or resources inside graphs:** deferred. Releasing a graph would then have to close or free them, which is a hidden destructor.
- **Proved disjointness of mutable accesses** (D4): deferred until benchmarks ask for it.
## Unresolved questions
- **Q1.** Capabilities or nested graphs inside graphs.
- **Q2.** `arc<mutex<T>>`, shared mutable state beyond one scope.
- **Q3.** A static-provenance mode reporting `E2110` instead of the `T0010` trap.
- **Q4.** O(1) detach of a sub-island (RFC 0001).
- **Q5.** Memory-mapped graphs (RFC 0001).
- **Q6.** Hashing of `iref` (deferred by the owner).