diff --git a/docs/rfcs/0007-io-and-linear-resources.md b/docs/rfcs/0007-io-and-linear-resources.md index 832ee18..c889327 100644 --- a/docs/rfcs/0007-io-and-linear-resources.md +++ b/docs/rfcs/0007-io-and-linear-resources.md @@ -1,6 +1,6 @@ # RFC 0007: I/O Intrinsics, I/O Errors and Linear Resources -Status: Proposed +Status: Proposed (revision 2: owner review applied) Scope: AIR format 0, feature `resources`; intrinsic and narrowing registries of `capabilities` Language version: 0.1 Supersedes: — @@ -17,7 +17,7 @@ Phase 2 must let a verified program talk to the outside world — console, files 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. +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. ## 1. Feature `resources` @@ -27,10 +27,14 @@ Depends on `capabilities`, `effects`, `ownership`. | Type | Meaning | Accounted size | |---|---|---| -| `file` | open file | 8 | +| `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** (RFC 0006 §2.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. @@ -39,7 +43,7 @@ In format 0, resource types MUST NOT appear as struct fields, enum payloads or a A resource value is **consumed** by exactly one of: -- a closing intrinsic (`fs.close`, `net.close_stream`, `net.close_listener`); +- 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`. @@ -77,10 +81,12 @@ Resource parameters of the current function are owned by it (they were moved in) | `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` | the operation cannot complete (see §5 for the deterministic profile) | +| `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. +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. ## 3. Intrinsic forms @@ -113,15 +119,15 @@ Paths are `slice` holding an absolute path whose components are not empty, ` | 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`, `bytes: slice` | — | `fs.write` | -| `fs.sync` | `b: mut` | — | `fs.write` | -| `fs.close` | `f: file` (consumed) | — | — | +| `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) | — | — | -Reading a file opened for writing, or writing one opened for reading, is `permission_denied`. `fs.close` consumes the file on both edges; an error reports a failed final flush. +An error from `fs.close` reports a failed final flush; the file is closed either way. ### TCP (`net.*`) @@ -158,13 +164,28 @@ The run record gains fields for observable output. `aji run` uses the **determin | Aspect | `deterministic` | `hosted` | |---|---|---| -| stdout / stderr | captured; reported as `"stdout"` / `"stderr"` strings in the run record | the process's real streams; the run record goes to stderr as the last line | +| 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; a `recv` that could only block forever yields `timed_out` | real sockets | +| 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. + ## Valid example ```text @@ -234,8 +255,10 @@ On acceptance: spec 06 (linear resources, §1 and §3 realized), spec 05 §10– - **A generic `result` type**: more general, but needs generics in AIR. The terminator form delivers results the same way `enum.switch` and `cap.narrow` do and cannot be misused. - **Traps on I/O failure**: rejected; external failure is expected and must be handled (spec 06 §5). - **Hosted profile by default**: makes the default run non-reproducible and mixes program output with the machine-readable record. +- **Synthetic `timed_out` for a `recv` that can never complete** (revision 1): gives one program different logical results in different profiles. Rejected for the `blocked` environment outcome. +- **Run record on stderr in the hosted profile** (revision 1): cannot be parsed unambiguously next to program stderr. Rejected for a separate result channel. +- **Untyped `file` with runtime mode checks** (revision 1): wrong-mode use found at runtime. Rejected for `file`. ## Unresolved questions -- Whether format 0 should include UDP and IPv6 (proposed: later). -- Whether `fs.read`/`net.recv` should also have proven-length variants avoiding the copy into a separate buffer (proposed: not needed). +None. Decided: UDP and IPv6 are deferred; no proven variants of `fs.read`/`net.recv` (a `.proven` form exists only where the verifier removes a specific safety check, as for `borrow.index.proven`).