Files
aji/docs/rfcs/0034-owning-buffered-files.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

7.6 KiB
Raw Blame History

RFC 0034: Owning bounded file reader and writer

Status: Implemented Scope: STD-7b, safe std.io.reader / std.io.writer after RFCs 0024/0033 Adoption: owner-requested intermediate std merge, 2026-10-07; normative rules and reference implementation are integrated together.

Motivation and normative integration

Spec 16 §2.18 adds concrete file adapters with fixed-capacity initialized byte buffers. They reduce repeated refill/flush code while keeping file ownership, allocation, effects, failure and cleanup explicit. Existing linear aggregates/buffers suffice without a reader trait, hidden destructor or new instruction.

Preserved invariant

The adapter owns exactly one file and one initialized buffer. Reader offsets delimit its unread prefix; writer used count delimits pending bytes. A failed writer retains its first I/O error and prevents further writes/flush retries because failed external writes may have written an unknown prefix. All normal outcomes transfer complete resources or retain them in the adapter; no resource is implicitly closed.

Verifier rule

No new abstract state/transfer function. Ordinary whole-value struct/enum moves, resource borrowing, buffer views, disjoint slices, effects/capabilities, alias/task loans, Copy/Send/Share and consumption checks apply. Fields remain visible with a documented caller invariant, as for existing safe collections. Invalid representation may trap through checked bounds/arithmetic but cannot bypass memory safety.

Runtime / deterministic behavior

Only new allocates, with explicit mem.alloc authority, charging capacity bytes. A failed new returns the original unchanged file with alloc_error. Existing spec 11 I/O/blocking/fuel/profile behavior applies. Reader refill/cached copies and writer room-making flushes are ordinary safe source. Finish frees Copy storage and returns the file; unread/failed finish returns the complete adapter. Discard explicitly abandons buffered bytes, frees storage and returns the file plus counts/error state. No sync/close is hidden.

Valid and rejected examples

The caller moves a file into new, explicitly consumes constructor failure, lends the adapter for reads/writes, handles finish returning the file or complete adapter, and closes the returned file. Discard reports unread/pending byte counts. Ignoring owning new/finish/discard outcomes, missing moves, invalid modes/effects, alias/task loans and sharing file-bearing adapters reject through existing diagnostics. The copy example consumes every constructor/finish result and closes returned files. Unread finish returns/reuses the complete reader; explicit discard reports abandoned pending bytes and refunds storage. Ignoring a constructor failure and ignoring failed finish reject E3002. Aliasing the cached bytes rejects E2101; sharing a file-bearing adapter rejects E4002.

Diagnostics / compatibility / AIR

No new diagnostic, syntax, AIR opcode/feature or verifier/runtime rule. Existing E7003/E7005/E3002/E2101/E4101/E3101/E4002 and resource availability checks apply. Two pinned modules are additive; old std.io APIs and AIR meanings retain their contracts. No implicit allocation, cleanup, authority, timeout, task or unsafe/trusted boundary.

Agent complexity impact

Five operations per concrete adapter: reader new/read/read_exact/finish/discard; writer new/write/flush/finish/discard. Known file modes keep exact fs.read/fs.write effects. Writer failure state distinguishes acceptance into an owned buffer from confirmation of external output and prohibits an unsound automatic retry. No language/verifier state is added. Full representation/failure contracts stay on demand; measured Quick Reference/source budgets follow. Existing primitives suffice, and no empirical MCC improvement is claimed.

Validation gate

Independent read/segmented-write byte oracles, capacities including zero, refill/flush/exact EOF, preserved guards, empty/full pending storage, constructor failure ownership/refunds, unread/failed finish, explicit discard, files transferred to tasks and restored loans, rejected ownership/type/effect/alias/Share cases. Hosted read error, externally partial write, no retry after error, source/AIR/package/import-order/profile/provenance checks, GC/scheduler stress and required repository/bundle checks. Paths and other system modules remain subsequent work.

Measured cl100k growth from RFC 0033: Quick Reference 8,350 → 8,445 (+1.1%); other source documentation 58,473 → 61,223 (+4.7%); full source excluding AIR 66,823 → 69,668 (+4.3%). library/std.md is 24,364 tokens. Corresponding o200k groups are 8,478 / 61,408 / 69,886; qwen2.5 groups are 8,695 / 62,738 / 71,433. 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 +105.0%, justified for owner review by visible prefetch/cursor, acceptance/partial-write, first-error and explicit resource cleanup contracts. Quick Reference remains beyond the 4–6k target; front-door consolidation/selective reading remains separate work. This increment adds no language syntax, diagnostic or verifier/runtime state and claims no empirical MCC improvement.

Validation result

All 34 buffered adapter conformance cases pass, including collection before every instruction. Owning reader/writer transfers and restored reader loans run 12 schedule seeds under quanta 1, 2, 7 and their own quantum. Independent Python byte oracles cover 48 complete read streams, 64 exact prefixes and 96 segmented writes over empty/exact/short inputs, zero and nonzero storage, multiple refills/flushes and preserved destination guards. Fixtures cover unread finish/discard cursor behavior, no EOF probe at finish, lazy full-cache flush, explicit discard, maximum capacity, constructor failure ownership and allocation refunds to narrowed authorities. Existing diagnostics reject missing moves, resource/result leaks, wrong mode/kind/effects, aliases, active task loans and non-Share references.

On Linux, 89 CLI checks pass: source/AIR/package/profile parity, reordered imports, pure audit and caller diagnostic provenance, a real file read fault (/proc/self/mem at unreadable offset zero), and four partial external write traces. A child-only three-byte file quota causes room-making, previous-call pending, explicit flush and capacity-zero direct write errors. The parent then raises the child's quota before the program attempts another write/empty write/flush/finish; the file remains exactly abc and the first error/pending bytes are preserved. This detects forbidden retry even when a later external write would succeed. Constructor/finish/discard failures retain complete owners, and the program explicitly closes returned files and verifies quota refunds. Tests use fuel and pipe handshakes, no wall-clock timeout or global host configuration change. macOS runs the portable checks/partial quota cases; read-fault and raised-quota retry probes are Linux-specific.

Unit/Quick Reference, formatting, hosted smoke, 115/115 source coverage constructs and reference generation pass. Full repository conformance passes with 1061 cases, zero failures and zero pending. The buffered file adapter gate is complete on this branch. Lexical paths and Std 0.1 remain required subsequent work.