Files
aji/docs/rfcs/0033-bounded-io-foundation.md
T
dev ad75ffe021
ci / build (ubuntu-latest) (push) Successful in 4m52s
ci / extended (push) Failing after 3h0m2s
ci / build (macos-latest) (push) Canceled after 0s
docs: adopt intermediate standard library foundation
Record the owner-requested STD-0..7 merge boundary and adoption of implemented
RFCs 0016-0035, whose normative rules and vertical implementations land together.
Update repository and std status; keep remaining Std 0.1 and service milestones
in the backlog. Track Q-034/P6-11 as the next source-language task and retain
the existing P7/P8 gate.

Implementation checks are recorded in RFC 0035 and the std plan; this change
updates adoption metadata and planning only.
2026-10-07 13:42:50 +03:00

6.3 KiB

RFC 0033: Explicit bounded I/O composition

Status: Implemented Scope: STD-7a, safe std.io foundation before buffered adapters and paths Adoption: owner-requested intermediate std merge, 2026-10-07; normative rules and reference implementation are integrated together.

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. 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 lends two files and a fixed three-byte frame buffer, handles all results and explicitly closes resources. Exact file reading validates prefix/suffix/guard preservation. Wrong mode rejects E7005; missing effect rejects E3101; a live task loan 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.