Files
aji/docs/rfcs/0032-owning-binary-heap.md
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

49 lines
6.5 KiB
Markdown

# RFC 0032: Safe owning binary heap
Status: Implemented
Scope: STD-6d.6, safe library source after RFCs 0024/0029
Adoption: owner-requested intermediate std merge, 2026-10-07; normative rules and reference implementation are integrated together.
## Motivation and normative changes
Spec 16 §2.16 supplies a bounded priority queue for arbitrary complete T with one explicit pure ref-value ordering operation. Copy and owning values share the same API and safe implementation. Ownership, allocation/failure and ordering contracts are visible to the verifier and caller.
## Preserved invariant
Dense entries own each live T exactly once. A Copy index permutation and inverse positions describe the heap; no child precedes its parent. Unused capacity owns no T. Callback laws are a stable strict weak order. Full push and every returned allocation failure preserve all state, and returned resources are explicitly consumed.
## Verifier / runtime rules
No new transfer function or runtime rule. Existing complete buffer/aggregate moves, exchange, ref/mut exclusion, task loans, exact static callable contracts, bounds, effects, Copy/Send/Share and consumption apply. Indices sift while owners remain in initialized storage; complete pop/exchange repairs a dense hole. All code is safe Aji lowering to ordinary AIR. Callback/fuel traps are abnormal; normal failures preserve layout/extraction order and refund recorded allocation authorities.
## Valid and rejected examples
An owning record orders by a Copy priority field. Push consumes the record; full returns it; pop moves it out for complete unpack and explicit resource consumption; nonempty free returns the whole heap. A Copy heap may peek; attempting peek of an owning T, ignoring full/pop/nonempty-free owners, missing move, borrowing across mutation/task loans, mismatched ordering policies/effects or sharing non-Share resources MUST reject through existing diagnostics. Executable examples and validation results are recorded below.
The [checked owning example](../../examples/std/heap.aji) moves a priority/owner record into one slot, pops/unpacks/takes the payload and frees the empty heap with a 40-byte grant. [Owning peek](../../tests/conformance/source/heap/peek_owner.aji) rejects E7005; [a leaked full return](../../tests/conformance/source/heap/full_leak.aji) rejects E3002; [a live task loan](../../tests/conformance/source/heap/loan.aji) rejects E4101. [Corrupt indices](../../tests/conformance/source/heap/invalid_indices.aji) trap T0008 through ordinary bounds checks, preserving safe-code memory safety.
## Diagnostics / compatibility / AIR
No new diagnostic, instruction, AIR feature, source syntax or verifier/runtime state. E7003/E7005, E3002, E2101/E4101, E3101 and E4001/E4002 remain ordinary checks. Addition of std.heap is compatible with existing modules and AIR format 0; imports grant no authority. No implicit allocation/destructor or unsafe/trusted boundary is introduced.
## Agent complexity impact
One strict-weak-order precondition, already expressible with static operation selection, plus an explicit priority-queue invariant. Eight operations mirror existing bounded containers; no inferred comparator, convenience auto-growth, borrowed-return facility or stable-sort concept. The Copy-only peek restriction follows existing lookup contracts. Dense/Copy-index storage avoids a new owner-swapping primitive, borrowed enum payloads or holes. Verifier state and syntax are unchanged; Quick Reference and full source documentation growth are measured below. No empirical MCC benefit is claimed.
Measured cl100k growth from RFC 0031: Quick Reference 8,201 → 8,284 (+1.0%); other source documentation 54,798 → 56,770 (+3.6%); full source excluding AIR 62,999 → 65,054 (+3.3%). library/std.md is 19,911 tokens. o200k groups are 8,317 / 56,955 / 65,272; qwen2.5 groups 8,534 / 58,278 / 66,812. AIR stays 179,282 bytes / 19 files / 51 sections; its source-bundle copy stays 46,300 / 46,451 / 47,800 tokens. Cumulative cl100k growth over the 33,981 pre-std baseline is +91.4%, justified by the owning priority queue and explicit ordering/representation/failure/budget contracts. No new language form, verifier state or diagnostic; the Quick Reference has one short paragraph and full contracts stay on demand. Existing primitives suffice without changing AIR.
## Alternatives
Copy-only array heaps fail the owning resource gate. A vacancy/default T loses generality. Direct swapping owners could use extra scratch rotations, but adds transient value movement and proof complexity. Two initialized buffers make permutation swaps and complete extraction explicit with a documented 16-byte per-capacity metadata cost on air64. Opaque runtime collection magic enlarges the trusted boundary unnecessarily.
## Validation gate
Independent Python priority-queue traces; ascending/descending order, duplicate priorities, complete owning/files/endpoint values, dense-hole repair, empty/zero/full, capacity overflow, both allocation failure phases, exact peak/refunds/cross-authority accounting, rejected leaks/aliases/loans/Copy/effect/policies; source/AIR/package/profile consistency; GC and scheduler stress; unit/Quick Reference, formatting, full conformance, source coverage and reference budgets. I/O/path remain subsequent increments.
## Validation result
All 1007 conformance cases pass, including 34 heap fixtures. All runtime heap fixtures pass collection before every instruction; shared read-only lookup, sender heap transfer and task-loan restoration run 12 seeds under quanta 1, 2, 7 and their own quantum. Independent Python heapq oracles cover 160 min/Copy and max/owning operations; sorted oracles check all 120 permutations of five elements. Tests cover duplicate priorities, dense-hole repair, direct own<u64>, owning byte buffers, files/endpoints, returned full/pop/nonempty-free ownership, empty/zero/overflow, both growth failure phases, complete layout preservation, exact peak/refund and cross-authority accounting, absence of full-push callbacks and bounds traps for invalid representation. Existing diagnostics reject leaks/missing move/Copy/aliases/loans/effects/policies/non-Share sharing.
Unit/Quick Reference, formatting, hosted smoke, bundle generation, 109/109 source coverage constructs and 70 CLI checks pass. CLI checks compare source/AIR/profile/package behavior, reordered imports, audit output, selected-comparator provenance and std bounds-trap locations. The separate general-map GC/stress process also completed with 33/33, closing Q-037. Collections STD-6d are implemented and validated on this branch; Std 0.1, I/O/path and system adapters remain unfinished.