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

+32 -3
View File
@@ -1,8 +1,8 @@
# Standard library API
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve.
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve and std.io.reader/std.io.writer new.
Except std.io (§2.17) and std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
Except std.io/std.io.reader/std.io.writer (§2.17–§2.18) and std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
| Module | Function signature | Result/failure |
|---|---|---|
@@ -389,10 +389,39 @@ Import `std.io`. Six concrete functions compose existing primitives with caller-
ReadExactResult is ok / eof(completed:usize) / err(ReadError{completed:usize,error:io_error}). Exact reads fill the destination, looping over short reads. Empty destination is ok without I/O. EOF leaves the remaining suffix unchanged. A read error guarantees only the successful prefix; the attempted suffix follows the primitive's failure contract. Bytes outside the supplied destination remain unchanged.
ReadToEndResult is eof(completed:usize) / full(completed:usize) / err(ReadError). Full means the supplied buffer was filled; it does not establish whether EOF was reached or unread data remains. Empty destination is full(0), and no extra byte is read/discarded to probe EOF. Growing/owning read-to-end and buffered adapters are separate work.
ReadToEndResult is eof(completed:usize) / full(completed:usize) / err(ReadError). Full means the supplied buffer was filled; it does not establish whether EOF was reached or unread data remains. Empty destination is full(0), and no extra byte is read/discarded to probe EOF. Growing/owning read-to-end is separate work; fixed-capacity adapters are below.
CopyResult is eof(completed:usize) / limit(completed:usize) / err(CopyError{read:usize,confirmed_written:usize,error:io_error}). Copies bound every scratch chunk by remaining limit and stop without probing past it. Exact EOF at the limit reports limit. A read error has both counts equal to earlier completed chunks. A write error includes its consumed input chunk in read; confirmed_written includes only prior successful writes. The failed write may have externally written a prefix: there is no rollback promise or exact failed-output count. Resources remain borrowed for explicit caller cleanup.
Zero limit returns limit(0) without I/O, even with empty scratch. Empty scratch with positive limit returns invalid_input with both error counts zero without I/O. No counter exceeds the limit or overflows. Scratch contents after a call are unspecified; bytes outside its slice remain unchanged. The [copy example](../../examples/std/io.aji) uses only fs.read/fs.write entry grants and a fixed frame scratch array, handling results and closing both resources explicitly. Existing fs.write_all/net.send_all/console writes already write all bytes on success, so this increment adds no duplicate write alias.
Ordinary alias/task-loan, type/mode, capability and effect checks apply; imports grant nothing. Blocking/fuel/profile errors remain spec 11 behavior, with no hidden timeout, task or unsafe boundary. std.io's concrete contracts are spec 16 §2.17; owning buffered adapters and lexical paths remain forthcoming.
## Owning buffered files
Import `std.io.reader` for Reader/file<read> and `std.io.writer` for Writer/file<write>. Import `std.io` directly when spelling read_exact's result, and `std.option` when matching the writer failure state; dependency imports do not expose names to callers. Each adapter owns a fixed initialized buffer and its file. Only new allocates, with an explicit mem.alloc capability, charging N bytes. Zero capacity delegates directly to primitive I/O. Files are never implicitly closed or synced. All file/adapter-bearing results are linear: consume every normal branch explicitly.
| Function arguments | Result | Effects |
|---|---|---|
| std.io.reader.new(input: file<read>, capacity: usize, heap: cap<mem.alloc>) | NewResult: ok(Reader), err(NewError{file:file<read>,error:alloc_error}) | mem.alloc |
| std.io.reader.read(input: mut<Reader>, destination: slice_mut<u8>) | ReadResult: ok(usize), err(io_error) | fs.read |
| std.io.reader.read_exact(input: mut<Reader>, destination: slice_mut<u8>) | std.io.ReadExactResult | fs.read |
| std.io.reader.finish(input: Reader) | FinishResult: ok(file<read>), unread(Reader) | none |
| std.io.reader.discard(input: Reader) | Discarded{file:file<read>,unread:usize} | none |
| std.io.writer.new(output: file<write>, capacity: usize, heap: cap<mem.alloc>) | NewResult: ok(Writer), err(NewError{file:file<write>,error:alloc_error}) | mem.alloc |
| std.io.writer.write(output: mut<Writer>, bytes: slice<u8>) | WriteResult: ok, err(WriteError{accepted:usize,error:io_error}) | fs.write |
| std.io.writer.flush(output: mut<Writer>) | FlushResult: ok, err(io_error) | fs.write |
| std.io.writer.finish(output: Writer) | FinishResult: ok(file<write>), err(FinishError{writer:Writer,error:io_error}) | fs.write |
| std.io.writer.discard(output: Writer) | Discarded{file:file<write>,pending:usize,failed:std.option.Option<io_error>} | none |
Move named files into new and named adapters into finish/discard. Constructor failure returns the exact original file before I/O; it can be closed or reused. Successful finish/discard drains/frees storage and refunds its recorded authority, returning the file for explicit use/close. No implicit growth, cleanup, retained caller borrow or extra authority is introduced.
Reader.read copies one cached prefix without refilling; when empty it refills the storage once, then returns a prefix. Thus read can return short even with more data available. Empty destination returns ok(0) without consuming/refilling and does not establish EOF. read_exact loops to fill the destination, using the std.io prefix/EOF/error contract. Reader.finish returns the complete unchanged Reader if cached unread bytes remain. Empty cache permits finish without probing EOF. Discard explicitly abandons unread prefetched bytes and returns their count: the file cursor remains advanced through prefetch, with no rewind. A read error retains ownership/offsets but promises no external cursor rollback or untouched attempted destination suffix.
Writer.write fills available storage and flushes a full buffer only when more input needs room. Ending with a full buffer keeps it pending. Empty input on a healthy writer does nothing. A write error reports bytes accepted from that call; accepted includes buffered data and is not an externally written/durable count. Pending data may include earlier calls. At capacity zero a direct failed write reports accepted=0 even if an external prefix was written. Healthy flush of empty storage performs no I/O; successful flush resets used to zero.
Any failed external write preserves pending bytes/used and records its first io_error. Every subsequent write, including empty, returns that error with accepted=0; every flush returns it, with no I/O or mutation. Finish returns the complete failed Writer plus error. There is no retry/reset because the failed operation may already have externally written an unknown prefix. Discard returns the file, pending count and failure state; pending can overlap that prefix and is not an exact loss count. Callers can explicitly handle the returned file according to their application protocol.
The visible representations are Reader{file,storage,begin,end}, 0≤begin≤end≤N, and Writer{file,storage,used,failed}, 0≤used≤N. Both buffers have initialized length=capacity=N. Caller field access/construction must preserve spec 16 §2.18's complete data/cursor/first-error invariant and must not independently consume/use the internal file/storage. Existing ownership, alias/task-loan, effects and Send/Share checks apply. Adapters can transfer to workers but cannot be shared immutably across tasks because files are non-Share. No unsafe/trusted boundary or new runtime/verifier state is needed.
The [copy example](../../examples/std/buffered_io.aji) uses two three-byte buffers (heap=mem.alloc:6), explicitly handles constructor/finish failure and returns the copied byte count, or zero after normal error cleanup. Conformance fixtures include capacities zero/full, unread finish/discard, exact EOF, allocation failure/refunds, worker transfer and rejected ownership/effect/alias cases. Hosted tests exercise read errors and real partial external writes; Linux raises the child file quota after failure to detect forbidden retry.