Files
aji/docs/rfcs/0024-linear-buffers-and-vectors.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.0 KiB
Raw Blame History

RFC 0024: Initialized linear buffers and safe vectors

Status: Implemented Scope: STD-6a storage / STD-6b Vec; vertical Phase 6, additive AIR format 0 Adoption: owner-requested intermediate std merge, 2026-10-07; normative rules and reference implementation are integrated together.

Motivation and invariant

Resolve Q-036 before promising Vec or Vec<sender>. A uniquely owned buffer exposes exactly its contiguous initialized prefix. Every nested linear element is owned once; unused capacity holds no observable value. No holes, implicit destructor or hidden growth is introduced. Whole-value aggregates and Copy-only own.array cannot represent dynamic initialized storage and moving extraction.

Decision

Spec 03 §26, spec 07a buffer contracts and source §17 define buffer, exact-capacity allocation, explicit reserve, bounded push, LIFO pop, child prefix view and empty-only recoverable free. Growth charges the new allocation before releasing the old authority; failure preserves state. Full push returns the supplied value, nonempty free returns the original buffer. Ordinary linearity forces cleanup on every normal path without verifier length facts. Safe std.vec wrappers provide new/len/capacity/reserve/push/pop/free/get/replace. More collections remain separate work.

Verifier and runtime rules

Whole availability and existing child-borrow/task-loan provenance suffice. buffer content must be complete, owning type graphs acyclic and Send/Share structural. Metadata-changing operations require exclusive access even on a dynamic no-op. Views expose only initialized values and use existing slice bounds/Copy access/exchange. The deterministic interpreter transfers complete values, preserves identity across tracing collection and accounts old/new allocation peaks. No new effect/capability/trap/diagnostic or synchronization policy is added.

Generic transfer

A complete Copy binding inside a specialized generic body may be spelled move, retaining ordinary Copy availability. Linear specializations transfer once. Ordinary monomorphic move of Copy still rejects. This narrowly specified source rule lets one safe container implementation serve Copy and linear elements without overloads/type-case metaprogramming. It adds no AIR instruction or verifier ownership state.

Valid and rejected examples

A valid program explicitly allocates Vec<own>, pushes with a mutable borrow, pops, own.takes the returned owner and frees the empty vector. Rejected examples leak a returned value, free/discard a nonempty vector, mutate/reserve while a view/task loan is live, duplicate an owner, copy a linear element or store borrowed/task-bound content. Checked AIR/source fixtures and a runnable example accompany implementation.

Compatibility and scope

New type/opcodes belong to linear_buffers; baseline own/own_array/region operations retain semantics. New source cases extend previously rejected forms. Existing generic Copy code without move lowers identically; non-generic invalid Copy moves keep E7006. Q-034 from-returns, channel_transfer, guard/task gates and recursive ownership remain restricted. The owner adopted this implemented contract with the intermediate std merge (2026-10-07). Deque/maps/heaps are not completed by this increment.

Agent complexity impact

New concepts: a contiguous initialized prefix, explicit capacity/peak-growth charge, ownership returned on full/nonempty and generic complete-value transfer. New verifier semantic state: none; primitive prefix state is runtime internal, with existing borrows/availability. New source syntax forms: none; buffer wrapper/typed allocator/builtin borrow binding compose existing forms. New diagnostics/effects/capabilities/traps: none. Eight operation names and eight AIR opcodes (len/capacity share a metadata-read contract), safe Vec wrapper signatures and their failure payloads replace opaque container behavior. Existing Copy storage cannot express resource extraction or cleanup obligations. Measured bundle costs and validation are recorded below; no MCC benefit is claimed without measurement.

The initial interpreter stores only the initialized prefix in its traced host representation; user-visible capacity is charged independently. Its push copies the prefix internally (O(length)); this increment makes no native Vec performance claim. A future backend may reserve physical backing while preserving the exact authority/failure contract.

The checked source example in Quick Reference §14 is a complete valid allocate/push/pop/drain/free program. The following rejects E2101 even when capacity does not grow:

fn bad(b: mut<buffer<u64>>, heap: cap<mem.alloc>) -> void effects [mem.alloc] {
    ref values = buffer.view(b);
    buffer.reserve(b, 0usize, heap) or trap;
}

Missing move rejects E7003; duplicated owners reject E2002; omitted failure payload cleanup rejects E3002; a get specialization with an owner rejects E7005. Every instruction's operand/result/continuation types, ownership and memory transfer, capability/effect and runtime behavior are normative in spec 07a. AIR/source fixtures also cover empty/full, nested buffers, allocation overflow, failure preservation, old/new authority release, file closing and sender cleanup, task loans, borrowed content and recursive owning types.

Measured reference costs

Relative to STD-5/RFC 0023, measured with the repository tool (offline tokenizers):

Bundle cl100k before → after o200k before → after Qwen2.5 before → after
Quick Reference 7,225 → 7,667 7,251 → 7,697 7,455 → 7,902
Other source docs 42,364 → 44,717 42,496 → 44,850 43,548 → 45,932
Full source excluding AIR 49,589 → 52,384 49,747 → 52,547 51,003 → 53,834
AIR reference in source bundle 43,966 → 45,520 44,114 → 45,667 45,403 → 46,981

New on-demand language/buffers.md: 1,175 cl100k tokens. library/std.md: 9,432. AIR standalone: 168,336 → 175,736 bytes (+4.4%), 19 files / 50 sections. Source output has 15 source files plus the AIR file. Full source cl100k growth is +5.6% relative to STD-5 and +54.2% since the 33,981-token pre-stdlib baseline. Quick Reference remains above the 4–6k target; the new prefix/returned-ownership contracts are necessary for one verified generic resource container and cannot be expressed by existing Copy-only dynamic storage. This cumulative cost is explicitly presented for owner review, not claimed as compression. Front-door consolidation and empirical MCC evaluation remain separate work.

Validation

The implementation checks unit and parser/serializer round-trip tests, full AIR/source conformance, forced collection and schedule stress, extended interpreter memory tests, hosted OS smoke, package/generic/linear/buffer CLI integration, formatting, source construct coverage and generated reference bundles. Dedicated buffer fixtures cover 56 cases including the Quick Reference example; exact final check counts are recorded in the commit description. No held-out corpus or former smoke-task semantic tuning is involved.