cli: add explicit owning buffered file adapters

Add pinned std.io.reader and std.io.writer using safe source, explicit fixed
allocation and file transfer. Finish/discard return complete resources;
writer failures preserve pending data and prohibit unsound partial retries.

Specify cursor, acceptance, failure and cleanup contracts in spec 16,
RFC 0034, the API reference and source bundle. Add standalone conformance,
independent byte oracles, task/GC stress, CLI/package/profile checks and
hosted partial-write tests with a raised child quota to detect retries.

Validation: 1061 conformance cases; 34 buffered cases with per-step GC and
12 schedule seeds; unit/Quick Reference, formatting, hosted smoke,
89 CLI checks, 115 source coverage constructs and reference generation.
This commit is contained in:
dev committed 2026-10-07 12:38:55 +03:00
1 parent 3e1c0f34e8
commit 8d8bef8f3e
51 files changed
+1916 -8

No files matched your search

+33 -2
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.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.
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.io.reader`, `std.io.writer`, `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/io/reader.aji`, `std/io/writer.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/std.io.reader/std.io.writer implement deterministic algorithms in every profile and have no concurrency effects; std.io/std.io.reader/std.io.writer follow the profile I/O/blocking rules in §2.17–§2.18 and spec 11. Modules other than std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap and std.io/std.io.reader/std.io.writer 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.io.reader/std.io.writer declare mem.alloc only on new and the concrete file effects in §2.18 on read/write/flush/finish; their other operations allocate no heap. 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`
@@ -340,4 +340,35 @@ Copies use scratch chunks bounded by min(scratch.len, limit-completed). Successf
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.
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 are specified in §2.18; lexical paths remain a subsequent STD-7 increment.
### 2.18 `std.io.reader` / `std.io.writer`: owning bounded file adapters
The registry MUST include std.io.reader at std/io/reader.aji and std.io.writer at std/io/writer.aji, ordinary safe source with concrete file modes. They MUST NOT open/close files, sync, retain caller borrows, grow implicitly, infer effects or acquire authority through import. Each owns one file and one fixed-capacity initialized buffer<u8>. Only new declares mem.alloc and accepts explicit allocation authority; byte storage charges capacity bytes, including unused capacity. Capacity zero is valid and uses direct primitive reads/writes. Constructor failure MUST return the exact original file with alloc_error, without I/O or resource consumption.
Reader is `{ file: file<read>, storage: buffer<u8>, begin: usize, end: usize }`, with fully initialized length=capacity=N and 0≤begin≤end≤N. Bytes begin..end are unread prefetched data; the underlying file cursor has advanced through those bytes. Writer is `{ file: file<write>, storage: buffer<u8>, used: usize, failed: std.option.Option<io_error> }`, with fully initialized length=capacity=N and 0≤used≤N. Bytes 0..used are pending accepted data; failed is none or the first failed external-write error. Neither adapter's file/storage may be independently used, consumed or mutated in a way that violates these logical invariants. Ordinary visible fields grant no representation privilege.
Reader types: NewError{file:file<read>,error:alloc_error}; NewResult{ok(Reader),err(NewError)}; ReadResult{ok(usize),err(io_error)}; FinishResult{ok(file<read>),unread(Reader)}; Discarded{file:file<read>,unread:usize}. Writer types: NewError{file:file<write>,error:alloc_error}; NewResult{ok(Writer),err(NewError)}; WriteError{accepted:usize,error:io_error}; WriteResult{ok,err(WriteError)}; FlushResult{ok,err(io_error)}; FinishError{writer:Writer,error:io_error}; FinishResult{ok(file<write>),err(FinishError)}; Discarded{file:file<write>,pending:usize,failed:std.option.Option<io_error>}. Every file/adapter-bearing result is linear and MUST be explicitly consumed.
| Module operation | Result | Effects |
|---|---|---|
| reader.new(input: file<read>, capacity: usize, heap: cap<mem.alloc>) | NewResult | mem.alloc |
| reader.read(input: mut<Reader>, destination: slice_mut<u8>) | ReadResult | fs.read |
| reader.read_exact(input: mut<Reader>, destination: slice_mut<u8>) | std.io.ReadExactResult | fs.read |
| reader.finish(input: Reader) | FinishResult | none |
| reader.discard(input: Reader) | Discarded | none |
| writer.new(output: file<write>, capacity: usize, heap: cap<mem.alloc>) | NewResult | mem.alloc |
| writer.write(output: mut<Writer>, bytes: slice<u8>) | WriteResult | fs.write |
| writer.flush(output: mut<Writer>) | FlushResult | fs.write |
| writer.finish(output: Writer) | FinishResult | fs.write |
| writer.discard(output: Writer) | Discarded | none |
Reader.read returns at most destination.len bytes. Empty destination returns ok(0) without I/O or consuming cached data; it does not establish EOF. If cached bytes exist, copy min(unread,destination.len) and advance begin, without refilling during that call. Otherwise, for N>0, refill the whole storage using fs.read, then return one cached prefix; a successful zero refill returns ok(0) as EOF. With N=0 delegate one primitive read directly to the destination. Refill errors retain the file/storage and existing offsets, returning the original io_error with no guessed external cursor rollback. Bytes outside the destination remain unchanged. read_exact composes read over destination suffixes and uses §2.17's completed-prefix/EOF/error contract, with no borrow escaping.
Reader.finish MUST return unread(the complete unchanged Reader) when begin<end. Otherwise drain/free Copy storage and return the original file at its current cursor. It does not require a confirmed EOF and does not read or close. Reader.discard explicitly abandons end-begin prefetched bytes, drains/frees storage and returns the file plus unread count. No rewind is promised; the underlying cursor remains advanced through prefetch. Storage refunds follow its recorded allocation authority.
Writer.write accepts the complete input into buffered storage or successful writes, returning ok; accepted is not a durability/external-write count. With N>0, copy as much as fits, flushing a full buffer only to make room for further input. If a call ends with a full buffer, retain it until a later write/flush/finish. On a room-making flush error, return WriteError with the number of this call's input bytes already accepted and the original error; the unaccepted suffix remains caller-owned. Buffered pending bytes remain in Writer, including accepted bytes from earlier calls. With N=0, delegate fs.write_all directly: failure has accepted=0, though an external prefix MAY have been written. No count exceeds input.len or overflows.
Writer.flush on a healthy empty buffer returns ok without I/O. On a successful external write, used becomes zero. On any external write failure, used/data remain unchanged and failed becomes some(the first error); no automatic retry/reset is allowed. Once failed is some(error), every write (including empty) MUST return err with accepted=0 and that error, and every flush MUST return err(error), without I/O or buffer mutation. Unknown partial external writes make retrying pending data unsound. Writer.finish first flushes: success drains/frees storage and returns the file; error returns FinishError with the complete unchanged failed Writer and original error. Finish does not sync or close. Writer.discard explicitly abandons buffered pending bytes, drains/frees storage and returns the file, used count and failure state; pending on failure MAY overlap an already externally written prefix and is not an exact loss count.
Effects, moves and mut/task-loan requirements apply on no-op/cached/failure paths. Existing aggregate/buffer moves, resource borrowing, alias exclusion, bounds/arithmetic, effect/capability and Copy/Send/Share rules verify all safe code; files make adapters non-Share and ordinary Send checks govern task transfer. Deterministic/hosted behavior follows existing profile primitives. No new AIR meaning, instruction/feature, syntax, diagnostic, verifier/runtime state, unsafe boundary, implicit destructor or cancellation policy is introduced. Invalid representation may trap through existing checked operations, preserving memory safety. Valid bookkeeping does not trap for a valid invariant/conforming primitives. Work is linear in initialized/copy/drained bytes plus I/O calls, with existing interpreter prefix-copy/GC costs. Lexical paths and Std 0.1 remain unfinished.