cli: add explicit bounded standard I/O composition

Add std.io exact stdin/file/TCP reads, bounded file read-to-end and
limited file write/append copies. Use caller slices and resource loans,
exact effects, explicit EOF/capacity/limit and confirmed partial progress.
No implicit heap, open/close, timeout or new AIR/runtime semantics.

Document failure limits and check hosted read errors and a partial
external file write under a child-only file-size quota. Integrate
normative contracts, RFC, source reference budgets, CLI/profile/package
checks and independent byte/count/cursor vectors.

Validation: 1027 conformance cases, 20 I/O GC cases, scheduled stress,
unit/Quick Reference, formatting, hosted smoke, 55 CLI checks, 111 source
coverage constructs and unchanged AIR reference bundle.
This commit is contained in:
dev committed 2026-10-07 12:14:11 +03:00
1 parent cfece6080c
commit 3e1c0f34e8
37 files changed
+1181 -6

No files matched your search

+26 -1
View File
@@ -10,7 +10,7 @@ APIs SHOULD offer one canonical operation for a common need, small documentation
## 2. Bootstrap module registry
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.hashmap`, `std.hashset`, `std.heap`, `std.map`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/hash.aji`, `std/hashmap.aji`, `std/hashset.aji`, `std/heap.aji`, `std/map.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile and have no concurrency effects. Modules other than std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap declare effects [] and allocate no heap memory. std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13–§2.16); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.hashmap`, `std.hashset`, `std.heap`, `std.io`, `std.map`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/hash.aji`, `std/hashmap.aji`, `std/hashset.aji`, `std/heap.aji`, `std/io.aji`, `std/map.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. Modules other than std.io implement deterministic algorithms in every profile and have no concurrency effects; std.io follows the profile I/O/blocking rules in §2.17 and spec 11. Modules other than std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap and std.io declare effects [] and allocate no heap memory. std.io declares only the concrete I/O effects in §2.17 and allocates no heap storage. std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13–§2.16); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
### 2.1 `std.math`
@@ -316,3 +316,28 @@ New allocates empty entries then empty indices; a returned second-allocation fai
Each capacity slot charges usize + complete T for Entry and usize for the index. On air64, T=u64 or own<u64> reserves 24N bytes, with separately owned payloads charged independently. Peak growth includes old/new entries, old/new indices and all user-owned allocations. Refunds follow each buffer's recorded authority, including growth through a different capability. Arithmetic/allocation-size overflow returns alloc_error.budget_exceeded.
Existing buffer/aggregate transfer/exchange, static contracts, borrow/task-loan exclusion, bounds, effects, Copy/Send/Share and resource verification suffice. Valid representations/successful callbacks do not trap during bookkeeping. Existing source/AIR meanings are unchanged; no new syntax, diagnostic, AIR feature/instruction, verifier/runtime state or trusted boundary is added. Deterministic/hosted behavior agrees. Abstract push/pop perform O(log L) comparisons and index swaps, peek is O(1), reserve copies O(L); interpreter prefix-copy/GC costs may add host work. No native-throughput or stable tie-order promise is made.
### 2.17 `std.io`: bounded I/O foundation
The registry MUST include std.io at std/io.aji, ordinary safe source. Six concrete functions compose existing borrowed-resource/capability I/O with caller-supplied slices. They MUST NOT allocate heap storage, open/close resources, retain borrows, infer effects or gain authority through import. Existing primitive writes already report all bytes written on success; no redundant write alias is added.
ReadError has completed: usize / error: io_error. ReadExactResult has ok, eof(usize), err(ReadError). ReadToEndResult has eof(usize), full(usize), err(ReadError). CopyError has read: usize / confirmed_written: usize / error: io_error; CopyResult has eof(usize), limit(usize), err(CopyError). All are ordinary Copy data, never authority.
| Function arguments | Result | Effects |
|---|---|---|
| read_exact_stdin(input: cap<io.read>, destination: slice_mut<u8>) | ReadExactResult | io.read |
| read_exact_file(input: mut<file<read>>, destination: slice_mut<u8>) | ReadExactResult | fs.read |
| read_exact_stream(input: mut<stream>, destination: slice_mut<u8>) | ReadExactResult | net.read |
| read_to_end_file(input: mut<file<read>>, destination: slice_mut<u8>) | ReadToEndResult | fs.read |
| copy_file(input: mut<file<read>>, output: mut<file<write>>, scratch: slice_mut<u8>, limit: usize) | CopyResult | fs.read, fs.write |
| copy_file_append(input: mut<file<read>>, output: mut<file<append>>, scratch: slice_mut<u8>, limit: usize) | CopyResult | fs.read, fs.write |
Exact reads loop over the unfilled suffix until destination.len successful bytes have been read (ok), an intrinsic reports zero (eof(completed)), or a read errors (err with the previous successful byte count and unchanged error). Empty exact destination returns ok without an intrinsic. EOF leaves the remaining destination unchanged. On a returned read error, only the completed successful prefix is guaranteed; contents of the attempted suffix follow the primitive and are not additionally promised by this wrapper. Bytes outside the destination MUST remain unchanged. Every count increment MUST be bounded by the remaining capacity.
read_to_end_file loops until zero (eof(completed)), destination capacity is reached (full(destination.len)), or a read errors (same prefix/error contract). Empty destination returns full(0) without I/O. Full does not establish that unread bytes remain or that EOF was reached: the function MUST NOT read/discard an extra byte to distinguish those states. Neither reader changes or retains caller resource ownership.
Copies use scratch chunks bounded by min(scratch.len, limit-completed). Successful reads are followed by fs.write_all; reaching zero returns eof(completed), reaching limit returns limit(completed) without probing EOF. Thus exact EOF at limit returns limit, not eof. A read error returns both counts equal to the previously confirmed completed chunks. A write error returns read including the just-read chunk and confirmed_written counting only earlier successful writes. The failed write MAY have externally written a prefix; confirmed_written is a lower bound, not an exact failed-output count or rollback promise. The input has advanced through confirmed successful reads. Counts MUST NOT exceed limit or overflow. EOF/error retain both borrowed resources for explicit caller cleanup.
limit=0 MUST return limit(0) without I/O, including with empty scratch. Empty scratch with positive limit MUST return err(CopyError{read:0,confirmed_written:0,error:io_error.invalid_input}) without I/O. Scratch contents after the call are unspecified; bytes outside its slice MUST remain unchanged. Functions do not allocate, grow, sync or close; the caller explicitly supplies all grants, slices, limits and final resource consumption. Effects/mut/task-loan requirements apply even to no-op paths. Append copies use the existing append resource semantics.
Existing type, effect/capability, borrow/task-loan, resource availability, bounds and outcome checks apply without new diagnostics or verifier/runtime state. Operations use ordinary AIR and spec 11 profile behavior; no unsafe/trusted boundary, syntax/opcode/feature, implicit cleanup, timeout or existing semantic change is introduced. Blocking/fuel remain environment outcomes. Returned failures are explicit ordinary enums, never exceptions. Successful work is linear in read bytes plus intrinsic calls; caller capacities/limits and fuel bound work. Buffered adapters and lexical paths remain subsequent STD-7 increments.