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.
46 lines
6.1 KiB
Markdown
46 lines
6.1 KiB
Markdown
# RFC 0033: Explicit bounded I/O composition
|
|
|
|
Status: Proposed
|
|
Scope: STD-7a, safe std.io foundation before buffered adapters and paths
|
|
|
|
## Motivation and normative changes
|
|
|
|
Spec 16 §2.17 adds six concrete functions composing existing console/file/TCP reads and file writes. Runtime writes already have all-or-error success contracts, so this increment adds no write alias or broad protocol hierarchy. Exact reads and bounded copies need reusable loops with explicit partial progress and no implicit allocation/authority.
|
|
|
|
## Preserved invariant
|
|
|
|
Only caller-supplied destination/scratch slices are accessed within their bounds. Mutable resources remain borrowed and owned by their caller on every normal return. No buffer, file or stream is allocated, opened, closed or retained. Confirmed counts never exceed supplied capacities/limits. Errors do not imply rollback of prior reads/writes or atomic failure of external I/O.
|
|
|
|
## Verifier and runtime rules
|
|
|
|
No new transfer function or runtime rule: ordinary ref/mut, disjoint slices, resource borrowing, task loans, exact typed/effect calls and checked arithmetic/bounds suffice. The underlying intrinsic supplies authority through cap<io.read>, mut<file<read/write/append>> or mut<stream>. Spec 11 still defines deterministic/hosted outcomes, blocking, fuel and errors. There is no timer, background task, ambient grant or new error variant.
|
|
|
|
## Valid and rejected examples
|
|
|
|
The caller opens read/write files with explicit capabilities, lends both and a fixed scratch slice to copy_file with a limit, handles eof/limit/err, and explicitly closes both on every path. Exact readers report completed prefix length on EOF/error. Aliased buffers, wrong resource modes/capability kinds, missing public effects, active task loans and closing a lent resource reject through existing diagnostics. Executable fixtures and validation results are recorded below.
|
|
|
|
The [copy example](../../examples/std/io.aji) lends two files and a fixed three-byte frame buffer, handles all results and explicitly closes resources. [Exact file reading](../../tests/conformance/source/io/file.aji) validates prefix/suffix/guard preservation. [Wrong mode](../../tests/conformance/source/io/wrong_mode.aji) rejects E7005; [missing effect](../../tests/conformance/source/io/missing_effect.aji) rejects E3101; [a live task loan](../../tests/conformance/source/io/loan.aji) rejects E4101. No callback or hidden resource cleanup is needed.
|
|
|
|
## Diagnostics / compatibility / AIR
|
|
|
|
No new instruction, AIR feature, syntax, verifier/runtime state or diagnostic. Existing type/capability, effect, alias/task-loan and resource-consumption checks apply. Built-in io_error.invalid_input represents unusable nonempty copy with zero scratch capacity, without performing an intrinsic. Imports grant no authority and all existing APIs/AIR meaning are unchanged.
|
|
|
|
## Agent complexity impact
|
|
|
|
Six concrete signatures carry exact io.read/fs.read/net.read/fs.write effects; no effect inference, unions of unrelated effects, callbacks, type erasure or reader trait is needed. EOF and filled-buffer/limit are distinct because no extra byte is consumed to discover EOF. Read and copy error records distinguish confirmed progress from external I/O that may have partially failed. This is a library contract, not a new language concept. Existing safe primitives suffice. Quick Reference/source budgets are measured below; no empirical MCC benefit is claimed.
|
|
|
|
Measured cl100k growth from RFC 0032: Quick Reference 8,284 → 8,350 (+0.8%); other source documentation 56,770 → 58,473 (+3.0%); full source excluding AIR 65,054 → 66,823 (+2.7%). library/std.md is 21,614 tokens. Corresponding o200k groups are 8,384 / 58,664 / 67,048; qwen2.5 groups are 8,600 / 59,988 / 68,588. AIR remains 179,282 bytes / 19 files / 51 sections; its source-bundle copy remains 46,300 / 46,451 / 47,800 tokens. Cumulative cl100k growth over the pre-std 33,981-token baseline is +96.6%, justified by explicit partial-progress, authority, EOF/limit and cleanup contracts. No new syntax, diagnostic or verifier/runtime state is introduced; detailed contracts stay on demand. No empirical compression benefit is claimed.
|
|
|
|
## Validation gate
|
|
|
|
Empty/exact/short input, preserved guard/suffix bytes, limits at/below/above EOF, small scratch chunks, append mode, zero scratch/limit, supplied destination counts, fragmented TCP and task loan restoration; rejected effects/modes/kinds/aliases/loans/close; independent public byte-vector oracle, source/AIR/package/profile/provenance and hosted read/write errors; GC/schedule stress and required repository/bundle checks. Buffered owning reader/writer and lexical paths remain required by STD-7.
|
|
|
|
|
|
## Validation result
|
|
|
|
All 1027 conformance cases pass, including 20 I/O fixtures. Runtime fixtures pass collection before every instruction; fragmented TCP and restored task loans run 12 seeds under quanta 1, 2, 7 and their own quantum. Independent Python byte-prefix/count oracles cover 162 exact/bounded reads and 250 copy/append combinations with empty/exact/short inputs, guard/suffix bytes, limits, cursor tails and scratch capacities. Tests reject wrong modes/kinds, missing effects, active aliases/task loans and closing a borrowed resource with existing codes. Maximum usize limit and exact EOF at limit are covered without overflow or probing past the bound.
|
|
|
|
Hosted tests exercise an actual stdin read error (directory → invalid_input) and a child-only file-size quota causing a partial second write: read=4, confirmed_written=2, actual output length=3. Source code explicitly consumes both files and checks the input cursor tail; no wall-clock timeout, global host configuration change or result-file write under that quota is used. The result confirms why failed writes cannot promise rollback/exact output counts.
|
|
|
|
Unit/Quick Reference, formatting, hosted smoke, reference generation, 111/111 source coverage constructs and 55 CLI checks pass. CLI checks compare source/AIR/profile/package results, reordered imports, audit output and caller diagnostic provenance. AIR semantics and source syntax remain unchanged. The foundation is complete on this branch; buffered owning reader/writer, lexical paths and Std 0.1 remain required future work.
|