diff --git a/docs/reference/air-features.md b/docs/reference/air-features.md index d74813e..3d40c58 100644 --- a/docs/reference/air-features.md +++ b/docs/reference/air-features.md @@ -38,8 +38,10 @@ Declaring `features [capabilities, effects, ownership]` grants no authority. It | `effects` | published | 2 | — | flat effect checking of `effects [...]` for `call` and `intrinsic` (E3101, spec 05 §9) | | `heap` | reserved | 2 | `capabilities`, `effects`, `ownership` | `Own`, `own.alloc`, `own.free`, move-only values | | `islands` | reserved | 4 | `capabilities`, `effects`, `ownership`, `regions` | `Island`, `iref`, transfer, freeze, `Frozen` | +| `linear_aggregates` | reserved | — | `capabilities`, `effects`, `ownership`, `resources` | resource types as struct fields, enum payloads and array elements; unscheduled | | `ownership` | published | 1 | — | frame storage `local`, second-class borrows `ref`/`mut`, `from` returns, initialization (spec 07 §8.8, spec 07a, spec 08 §11, §13) | | `regions` | reserved | 2 | `capabilities`, `effects`, `ownership` | `Region`, static generations, region allocation, reset and destroy | +| `resources` | published | 2 | `capabilities`, `effects`, `ownership` | linear resources `file`, `file`, `file`, `listener`, `stream`; built-in `io_error`; I/O intrinsics (spec 06 §6–§7, `reference/intrinsics.md`) | | `sync` | reserved | 3 | `ownership` | `Mutex`, `RwLock`, linear guards | | `tasks` | reserved | 3 | `ownership` | structured task scopes, spawn/join/cancel, `Send`/`Share` | diff --git a/docs/reference/capabilities.md b/docs/reference/capabilities.md index ecd35fb..c74435c 100644 --- a/docs/reference/capabilities.md +++ b/docs/reference/capabilities.md @@ -5,8 +5,8 @@ Normative registry of capability kinds for AIR format 0 (spec 05 §10). A kind n | Kind | Authorizes effects | Scope at instantiation | Narrowing forms | |---|---|---|---| | `clock.read` | `clock.read` | none | — | -| `fs.read` | `fs.read` | absolute path prefix | `prefix` (RFC 0007) | -| `fs.write` | `fs.write` | absolute path prefix | `prefix` (RFC 0007) | +| `fs.read` | `fs.read` | absolute path prefix | `prefix` | +| `fs.write` | `fs.write` | absolute path prefix | `prefix` | | `io.read` | `io.read` | none | — | | `io.write` | `io.write` | none | — | | `mem.alloc` | `mem.alloc` | byte budget | `budget` (RFC 0008) | @@ -26,6 +26,15 @@ Narrowing forms marked with an RFC are registered when that RFC is accepted; unt | Enum | Variants | |---|---| | `cap_error` | `invalid_restriction`, `not_subset`, `unsupported_restriction` | +| `io_error` | `not_found`, `permission_denied`, `already_exists`, `invalid_input`, `no_space`, `broken_pipe`, `connection_refused`, `connection_reset`, `address_in_use`, `address_unavailable`, `timed_out`, `other` | + +Variant sets of published built-in enums are frozen (spec 07 §8.7). + +## Narrowing forms + +| Kind | Form | Argument | `ok` | `err` (`cap_error`) | +|---|---|---|---|---| +| `fs.read`, `fs.write` | `prefix` | `path: slice`, absolute, no empty/`.`/`..` components | same kind scoped to `path` | `invalid_restriction` if malformed; `not_subset` if not inside the current scope | ## Instantiation diff --git a/docs/reference/intrinsics.md b/docs/reference/intrinsics.md index 9a0c9cd..67e7bb5 100644 --- a/docs/reference/intrinsics.md +++ b/docs/reference/intrinsics.md @@ -1,93 +1,68 @@ -# Intrinsic Registry (Draft) +# Intrinsic Registry -Intrinsics are verifier-known platform/runtime operations. Each intrinsic requires a formal signature, effects, capabilities, ownership behavior and failure model. +Normative catalogue of intrinsics for AIR format 0 (spec 05 §11). Each entry fixes operand types, results, effect, capability, consumption and failure model; behavior per profile is in spec 11 §8. Using an intrinsic needs the feature `capabilities`; intrinsics that mention resource types also need `resources`. -## Filesystem +Notation: `c` capability operand, `b` borrowed resource operand. Fallible intrinsics are terminators: results are the parameters of `@ok`, and `@err` receives one `io_error` (spec 06 §7). -```text -fs.open_read -fs.open_write -fs.read -fs.write -fs.write_all -fs.sync -fs.close -``` +`c` = capability operand, `b` = borrowed resource operand, results are `@ok` parameters. -## Console / generic I/O +### Console (`io.*`) -```text -io.stdout_write -io.stderr_write -``` +| Intrinsic | Operands | `@ok` | Effect | +|---|---|---|---| +| `io.stdout_write` | `c: cap`, `bytes: slice` | — | `io.write` | +| `io.stderr_write` | `c: cap`, `bytes: slice` | — | `io.write` | +| `io.stdin_read` | `c: cap`, `buf: slice_mut` | `n: usize` (0 = end of input) | `io.read` | -## Network +Writes are all-or-error: on `@ok` every byte was written. -```text -net.socket -net.bind -net.listen -net.accept -net.connect -net.recv -net.send -net.close -``` +### Files (`fs.*`) -## Time/random +Paths are `slice` holding an absolute path whose components are not empty, `.` or `..` (otherwise `invalid_input`). A path outside the capability's scope is `permission_denied`. -```text -clock.monotonic -clock.wall -random.fill -``` +| Intrinsic | Operands | `@ok` | Effect | +|---|---|---|---| +| `fs.open_read` | `c: cap`, `path` | `f: file` | `fs.read` | +| `fs.create` | `c: cap`, `path` | `f: file` (truncated or new) | `fs.write` | +| `fs.open_append` | `c: cap`, `path` | `f: file` (created if missing) | `fs.write` | +| `fs.read` | `b: mut>`, `buf: slice_mut` | `n: usize` (0 = end of file) | `fs.read` | +| `fs.write_all` | `b: mut>` or `mut>`, `bytes: slice` | — | `fs.write` | +| `fs.sync` | `b: mut>` or `mut>` | — | `fs.write` | +| `fs.close` | `f: file` for any mode (consumed on both edges) | — | — | -## Task/thread +An error from `fs.close` reports a failed final flush; the file is closed either way. -```text -task.spawn -task.join -task.cancel -thread.spawn -thread.join -``` +### TCP (`net.*`) -## Synchronization +Addresses are `slice` text `host:port` with a numeric IPv4 host (format 0). -```text -mutex.create -mutex.lock -mutex.unlock -rwlock.create -rwlock.read -rwlock.write -channel.create -channel.send -channel.recv -``` +| Intrinsic | Operands | `@ok` | Effect | +|---|---|---|---| +| `net.listen` | `c: cap`, `addr` | `l: listener` | `net.listen` | +| `net.accept` | `b: mut` | `s: stream` | `net.listen` | +| `net.connect` | `c: cap`, `addr` | `s: stream` | `net.connect` | +| `net.recv` | `b: mut`, `buf: slice_mut` | `n: usize` (0 = peer closed) | `net.read` | +| `net.send_all` | `b: mut`, `bytes: slice` | — | `net.write` | +| `net.close_stream` | `s: stream` (consumed) | — | — | +| `net.close_listener` | `l: listener` (consumed) | — | — | -## Atomics +All network operations block the calling execution unit (Phase 3 adds `thread.block` handling and task-aware I/O). -```text -atomic.load -atomic.store -atomic.exchange -atomic.fetch_add -atomic.fetch_sub -atomic.compare_exchange -atomic.fence -``` +### Clock and random (infallible) -## Memory +| Intrinsic | Operands | Result | Effect | +|---|---|---|---| +| `clock.monotonic` | `c: cap` | `u64` nanoseconds | `clock.read` | +| `random.fill` | `c: cap`, `buf: slice_mut` | — | `random.read` | -```text -heap.alloc -heap.free -region.create -region.alloc -region.reset -region.destroy -island.create -island.alloc -island.freeze -``` +### Narrowing forms + +| Kind | Form | Argument | Result | +|---|---|---|---| +| `fs.read`, `fs.write` | `prefix` | `path: slice`, absolute | `ok`: same kind scoped to `path`; `err`: `invalid_restriction` if `path` is malformed, `not_subset` if it is not inside the current scope | + +## Registry rules + +- An unknown name is `E0004`; operands of the wrong type `E1001`; a capability of the wrong kind `E3201`; an effect missing from the caller `E3101`. +- Using the fallible form for an infallible intrinsic, or the reverse, is `E1001`; wrong `@ok`/`@err` parameter counts are `E5002`. +- Resource operands marked "consumed" are consumed on both edges. diff --git a/docs/rfcs/0007-io-and-linear-resources.md b/docs/rfcs/0007-io-and-linear-resources.md index ed27f0e..dce4501 100644 --- a/docs/rfcs/0007-io-and-linear-resources.md +++ b/docs/rfcs/0007-io-and-linear-resources.md @@ -1,8 +1,9 @@ # RFC 0007: I/O Intrinsics, I/O Errors and Linear Resources -Status: Proposed (revision 2: owner review applied) +Status: Accepted (revision 2, after owner review) Scope: AIR format 0, feature `resources`; intrinsic and narrowing registries of `capabilities` Language version: 0.1 +Integrated into: spec 05 §11.1; spec 06 §6–§7; spec 07 §8.7, §8.9; spec 07a; spec 11 §7–§8; `reference/intrinsics.md`, `reference/capabilities.md`, `reference/air-features.md` Supersedes: — Builds on RFC 0005 (borrows) and RFC 0006 (effects, capabilities). Heap allocation and regions follow in RFC 0008 and reuse the linearity rules defined here. diff --git a/docs/spec/05-effects-and-capabilities.md b/docs/spec/05-effects-and-capabilities.md index bd2088f..1fb3eb5 100644 --- a/docs/spec/05-effects-and-capabilities.md +++ b/docs/spec/05-effects-and-capabilities.md @@ -250,6 +250,19 @@ An effectful operation takes its capability as an explicit operand (§11). If th This RFC registers no I/O intrinsic yet. RFC 0007 adds the catalogue (console, files, sockets, clock, random) together with the error representation. +### 11.1 Fallible intrinsics + +Two forms extend RFC 0006 §3: + +```text +[%r =] intrinsic (%a…) ; infallible +intrinsic (%a…), ok -> @ok, err -> @err ; fallible: a terminator +``` + +A fallible intrinsic delivers its results as the parameters of `@ok` (count and types per registry; `E5002`/`E1001`) and a single `io_error` to `@err`. The registry says which form each intrinsic has; using the other form is `E1001`. Resource operands consumed by an intrinsic are consumed on both edges. + +The catalogue of intrinsics is [`../reference/intrinsics.md`](../reference/intrinsics.md). + ## 12. Runtime - A capability is represented as an index into a host-owned table of (kind, scope) entries. The index is not observable by the program. diff --git a/docs/spec/06-resources-and-errors.md b/docs/spec/06-resources-and-errors.md index 501954c..979a8ce 100644 --- a/docs/spec/06-resources-and-errors.md +++ b/docs/spec/06-resources-and-errors.md @@ -58,3 +58,82 @@ Propagation syntax may exist only if its lowering is explicit and does not intro Use `Result` for failures callers may reasonably handle. Use trap for invariant violations, failed assertions and explicitly trapping primitives. + +## 6. Resources in AIR (feature `resources`) + +Decision record: RFC 0007. + +Invariants: + +1. **Linearity.** A resource value (`file`, `listener`, `stream`) is consumed exactly once on every normal path: by a closing intrinsic, by passing it to a by-value resource parameter or block parameter, or by returning it. It is never used after consumption and never silently dropped. +2. **No implicit release.** No resource is closed by scope exit, by a trap path being taken, or by any hidden action. Paths ending in `trap` end the program; the runtime then releases host resources as part of terminating the execution instance, which is not observable by the program. +3. **Explicit failure.** Every operation that can fail for external reasons delivers either its results or an `io_error` to block parameters. Failure is never a trap and never an exception. +4. **Object capabilities.** An open resource carries the authority it was opened with; operations on it need its effect but no further capability operand. Opening a resource requires a capability. +5. **Determinism.** In the `deterministic` profile every intrinsic has a fully specified result; the same module, grants, inputs and fuel produce the same run record. Profiles never invent results the program did not ask for: a run that can make no further progress ends with the environment outcome `blocked`, not with an I/O error. + +Depends on `capabilities`, `effects`, `ownership`. + +### 6.1 Types + +| Type | Meaning | Accounted size | +|---|---|---| +| `file` | file opened for reading | 8 | +| `file` | file opened for writing (created or truncated) | 8 | +| `file` | file opened for appending | 8 | +| `listener` | listening TCP socket | 8 | +| `stream` | connected TCP stream | 8 | + +The mode of a file is part of its type, so using a file in the wrong mode is a verifier error, not a runtime failure: `fs.read` takes `mut>`; `fs.write_all` and `fs.sync` take `mut>` or `mut>`. + +Resource types are **linear** and **authority-bearing** (spec 05 §10.1): not copyable (`copy` is `E1001`), not zeroable, not comparable, not serializable. + +In format 0, resource types MUST NOT appear as struct fields, enum payloads or array elements; that requires the reserved feature `linear_aggregates` (`E0006` reason `feature_not_declared` without it). They may appear as function parameters, return types, block parameters and as operands of borrow instructions. + +### 6.2 Consumption + +A resource value is **consumed** by exactly one of: + +- a closing intrinsic (`fs.close`, `net.close_stream`, `net.close_listener`). A closing intrinsic consumes the resource **regardless of its result**: on both the `ok` and the `err` edge the resource is dead, and closing it again is `E2002`. After a failed close the state of the underlying host handle is unspecified, so no retry is possible; +- being a `call` argument for a parameter of resource type (ownership moves to the callee); +- being a branch argument (`br`, `cbr`, `switch`, default targets) for a block parameter of resource type; +- `ret`. + +Every other operand use of a resource is a **non-consuming use**, which is only permitted through borrows: `borrow.ref %f` / `borrow.mut %f` take a resource SSA value as their root, exactly as for a `local` (RFC 0005), and produce `ref` / `mut`. + +### 6.3 Verifier rules + +The resource state of each resource value — `owned`, `consumed by close`, `consumed by transfer` — is tracked on every reachable path with the same discipline as borrows (spec 08 §11.4): + +| Rule | Code | +|---|---| +| owned resource live at `ret` (other than the returned one) | `E3002` (`resource.leak`) | +| resource state differs between edges into a block | `E3002` | +| resource consumed again after any consumption | `E2002` (`ownership.double_consume`) | +| non-consuming use after consumption by a closing intrinsic | `E3001` (`resource.use_after_consume`) | +| non-consuming use after consumption by transfer | `E2001` (`ownership.use_after_move`) | +| resource consumed while a borrow of it is live | `E2101` | + +Resource parameters of the current function are owned by it (they were moved in). Resource block parameters own their argument. + +## 7. I/O errors + +`io_error` is a built-in enum (spelled without `@`, used with `enum.switch io_error %e, …`): + +| Variant | Meaning | +|---|---| +| `not_found` | path or address does not exist | +| `permission_denied` | the host refused, or the path is outside the capability's scope | +| `already_exists` | exclusive creation of an existing path | +| `invalid_input` | malformed path or address (relative path, `.`/`..` component, unparsable address) | +| `no_space` | storage exhausted | +| `broken_pipe` | the peer closed while writing | +| `connection_refused` | nobody listens at the address | +| `connection_reset` | the peer reset the connection | +| `address_in_use` | listen address taken | +| `address_unavailable` | listen or connect address not available to this host | +| `timed_out` | an explicit timeout expired (reserved for timers in Phase 3; no Phase 2 operation produces it) | +| `other` | any host error not listed above | + +The mapping from host errors to variants is part of each profile's specification; `other` keeps the set closed. Host error numbers are not part of AIR semantics: a runtime may attach them as diagnostic metadata outside the run record's values, never in a way the program can observe. + +**Frozen variant sets.** The variants of a published built-in enum never change: adding one would make an existing exhaustive `enum.switch` without `default` non-exhaustive, turning an accepted module into a rejected one (spec 07 §8.7). This is why `timed_out` is reserved now although nothing produces it yet. diff --git a/docs/spec/07-air.md b/docs/spec/07-air.md index 2413927..7a336ae 100644 --- a/docs/spec/07-air.md +++ b/docs/spec/07-air.md @@ -250,13 +250,15 @@ A change is permitted under `air 0` only if all of the following hold: 3. the change adds instructions, types, diagnostic codes or trap codes; every added construct belongs to a feature registered in [`../reference/air-features.md`](../reference/air-features.md), and the feature's semantics are specified (spec 07a and the relevant spec files) before the feature is published and implemented; 4. the canonical form (§8.5) of existing constructs is unchanged. -A published feature's semantics are immutable. Extending a feature with new semantics is not permitted; register a new feature instead. +A published feature's semantics are immutable. In particular the variant set of a published built-in enum never changes, since a new variant would make existing exhaustive `enum.switch` instructions non-exhaustive. Extending a feature with new semantics is not permitted; register a new feature instead. Any other change to format 0 — a different meaning, typing rule, trap behavior, diagnostic code or canonical spelling for an existing construct — requires a new format version (`air 1`) introduced by an RFC. An implementation that supports several format versions selects the semantics by the module's `air` line; it never mixes them. ### 8.9 Types of the features `effects` and `capabilities` -`cap` (feature `capabilities`) is an opaque, copyable, authority-bearing value type; `K` is a registered capability kind ([`../reference/capabilities.md`](../reference/capabilities.md)). Built-in enums such as `cap_error` are spelled without `@`, have registered variants and are used with `enum.switch %v, …` like declared enums. Rules: spec 05 §9–§12. +`cap` (feature `capabilities`) is an opaque, copyable, authority-bearing value type; `K` is a registered capability kind ([`../reference/capabilities.md`](../reference/capabilities.md)). Built-in enums such as `cap_error` and `io_error` are spelled without `@`, have registered variants and are used with `enum.switch %v, …` like declared enums. Rules: spec 05 §9–§12. + +Feature `resources`: `file`, `file`, `file`, `listener`, `stream` are linear, authority-bearing resource types (spec 06 §6). ### 8.8 Types and syntax of the features `ownership` and `arrays` diff --git a/docs/spec/07a-air-instructions.md b/docs/spec/07a-air-instructions.md index 1b248b4..d1beb05 100644 --- a/docs/spec/07a-air-instructions.md +++ b/docs/spec/07a-air-instructions.md @@ -222,7 +222,11 @@ Rules: spec 05 §9–§12. Decision record: RFC 0006. | `cap.narrow.
%c, %arg…, ok -> @ok, err -> @err` (terminator) | `cap`, form arguments | form registered for `K` (`E3202`); `@ok` has one parameter `cap`, `@err` one parameter `cap_error` (`E5002` count, `E1001` type) | continues at `@ok` with a narrower capability sharing nothing wider than `%c`, or at `@err` with the reason; never traps | | `[%r =] intrinsic (%a…)` | per registry entry | operand types (`E1001`), capability kind (`E3201`), effect (`E3101`), borrows as for `call`; unknown name `E0004` | forwards to the host through the capability | -No intrinsic is registered yet (RFC 0007). Effect checking applies to `call` and `intrinsic` in modules that declare `effects` (spec 05 §9.3). +Intrinsics have an infallible form (`[%r =] intrinsic …`) and a fallible terminator form (`intrinsic …, ok -> @ok, err -> @err`) (spec 05 §11.1). The catalogue is `reference/intrinsics.md`. Effect checking applies to `call` and `intrinsic` in modules that declare `effects` (spec 05 §9.3). + +## Feature `resources` + +Resource types and their consumption rules: spec 06 §6. Resources are used through `borrow.ref`/`borrow.mut` (which accept a resource value as root) and consumed by closing intrinsics, by-value resource parameters, resource block parameters and `ret`. ## Run result @@ -238,4 +242,4 @@ No intrinsic is registered yet (RFC 0007). Effect checking applies to `call` and On a trap it reports `{"status":"trapped", "code":…, "kind":…, "function":…, "block":…, "inst":…}` (spec 01 §6). -When instantiation fails (spec 05 §10.2) the program does not start and the runner reports `{"status":"not_instantiated","reason":…,…}`. When the runner stops execution because an execution limit is exhausted (spec 11 §7), it reports `{"status":"limit_exceeded","kind":"fuel","limit":N,"steps":N}`. This is an outcome of the execution environment, not of the program, and is not a trap. +Programs that declare `capabilities` also get `"stdout"` and `"stderr"` (captured output) and `"files"` (files written in the virtual file system) in the record of the `deterministic` profile (spec 11 §8). When the deterministic runtime detects quiescence it reports `{"status":"blocked","kind":"no_future_event",…}` (spec 11 §8). When instantiation fails (spec 05 §10.2) the program does not start and the runner reports `{"status":"not_instantiated","reason":…,…}`. When the runner stops execution because an execution limit is exhausted (spec 11 §7), it reports `{"status":"limit_exceeded","kind":"fuel","limit":N,"steps":N}`. This is an outcome of the execution environment, not of the program, and is not a trap. diff --git a/docs/spec/11-determinism-and-replay.md b/docs/spec/11-determinism-and-replay.md index 8bfd8c4..91fa1f3 100644 --- a/docs/spec/11-determinism-and-replay.md +++ b/docs/spec/11-determinism-and-replay.md @@ -87,6 +87,7 @@ not_instantiated the environment did not start the program (spec 05 §10.2) returned the program finished trapped the program failed (T0xxx) limit_exceeded the environment stopped the program +blocked the program can make no further progress (§8) ``` Running without a fuel limit is permitted (for example `aji run program.air`). Conformance, fuzz and stress runs MUST use a fuel limit. @@ -100,3 +101,33 @@ With several tasks, fuel is global: the sum of instructions executed by all task | fuel | how long the program may execute | | schedule budget | how many interleavings schedule exploration examines | | quantum | how many instructions a task executes before a forced scheduling point | + +## 8. Execution profiles + +Decision record: RFC 0007. + +The run record gains fields for observable output. `aji run` uses the **deterministic** profile by default; `--profile hosted` uses the real host. + +| Aspect | `deterministic` | `hosted` | +|---|---|---| +| stdout / stderr | captured; reported as `"stdout"` / `"stderr"` strings in the run record, which is printed on stdout | the process's real streams; the run record is written to a separate result channel (below) | +| stdin | from `--stdin TEXT` (empty by default) | real stdin | +| files | in-memory file system seeded by `--vfs PATH=TEXT`; files written during the run are reported as `"files"` in the run record | real file system, limited by capability scopes | +| network | in-process loopback: `net.connect` to a listening address of the same run queues a connection that `net.accept` returns; sends are buffered; an operation that would wait for an event that can never occur ends the run with the environment outcome `blocked` (below) | real sockets | +| clock | the number of instructions executed so far (1 ns per instruction) | real monotonic clock | +| random | PRNG seeded by `--seed N` (default 0), algorithm fixed by the spec | host CSPRNG | + +**Quiescence.** In the `deterministic` profile, when an operation would wait, no other execution unit can run, and no future deterministic event can complete the wait, the run ends with the environment outcome + +```json +{"status":"blocked","kind":"no_future_event","function":"…","block":"…","inst":0} +``` + +and `aji run` exits with code 7. This is not an `io_error` and not a trap: the program did not ask for a timeout, so none is invented. In the `hosted` profile the same operation simply blocks. A program that needs a timeout states it explicitly (timers and `select`, Phase 3). The outcomes are now: + +```text +program outcome: returned, trapped +environment outcome: not_instantiated, limit_exceeded, blocked +``` + +**Result channel.** In the `hosted` profile the program owns stdout and stderr, so the run record cannot share them unambiguously. The machine-readable record goes to a separate channel: `aji run --profile hosted --result-file PATH` (a `--result-fd N` form may follow; `ajid` has its own control channel). Without `--result-file` the CLI may print a human-readable summary on stderr after the program ends; that summary is not a machine interface.