# RFC 0025: Safe owning deque Status: Proposed Scope: STD-6c deque increment, ordinary safe library source ## Decision and invariant Spec 16 §2.11 defines one deque API using the existing initialized buffer and Option. A ring has capacity initialized option slots, a head and a logical length. Exactly the occupied ring range contains some; all other slots contain none. This represents vacancy with complete values, preserving whole-value ownership without holes or new primitives. Representation fields remain ordinarily visible; manual construction/mutation must preserve the documented invariant. Eleven functions cover construction, metadata, explicit reserve, both ends, moving pop, empty-only free, Copy get and complete replace. Push never grows. Full push/out-of-bounds replace return the input; nonempty free returns the whole queue. Allocation failure preserves physical slots and metadata. Successful growth charges old+new capacity, fills new none slots, rotates old slots using complete exchanges and resets head without changing order. Index calculations avoid head+offset overflow. No destructor closes user resources. ## Verifier and runtime rules Existing complete aggregate/enum moves and value.swap preserve exactly one owner per element. Borrow scopes, task loans, Send/Share and Copy constraints apply without special cases. The library never returns a borrow. Only new/reserve require explicit mem.alloc effect/capability; vacancy slots include the Option tag in their charge. The deterministic interpreter executes ordinary AIR with existing budgets/fuel; no opcode/runtime/feature or synchronization change is needed. Existing E7003/E2002/E3002/E2101/E4101/E7005 diagnostics cover bad moves, leaks, aliases, task loans and constraints. Valid calls are trap-free except execution limits; malformed manually modified representations have no weakened verifier or safety rules. Existing source/AIR/API semantics remain compatible. ## Examples and validation The runnable deque example pushes a front/back owner, pops in FIFO order, explicitly consumes each owner and frees the empty queue. Rejected examples omit a move/cleanup, duplicate ownership, call Copy get with an owner or retain a borrow/task loan across mutation. Fixtures independently model ring order, wrapped growth, full/empty/zero capacity, repeated reuse, success/error budget release, files and channel endpoints. New/reserve charge Option slots; tests must not assume Vec's T-only accounting. Ordinary unit/conformance, collection/schedule stress for deque cases, CLI canonical lowering/manifest/hosted checks and reference bundle checks accompany the implementation. ## Agent complexity impact New language concepts, syntax, instructions, verifier state and diagnostics: none. Library concepts: two ends and the documented ring invariant. The API reuses Vec's returned-ownership naming, effects and explicit specialization; no overload/trait/reflection or implicit allocation/destruction is introduced. Existing primitives suffice. Full API/contracts live in the on-demand library page rather than another long Quick Reference tutorial. Measured Quick/Source/AIR bundle costs and final validation follow; no empirical MCC benefit is claimed. This increment completes STD-6c deque; maps/sets/heaps remain STD-6d and Std 0.1 is not complete. The checked [owning example](../../examples/std/deque.aji) uses only safe ordinary source. This call rejects E7003 because a named owner must move: ```aji fn bad(q: mut>>, item: own) -> void effects [] { let result = std.deque.push_back::>(q, item); trap; } ``` ### Reference bundle measurement Relative to STD-6a/b, with the repository's three tokenizers: | Bundle | cl100k before → after | o200k before → after | Qwen2.5 before → after | |---|---:|---:|---:| | Quick Reference | 7,667 → 7,747 | 7,697 → 7,778 | 7,902 → 7,982 | | Other source docs | 44,717 → 46,246 | 44,850 → 46,376 | 45,932 → 47,467 | | Full source excluding AIR | 52,384 → 53,993 | 52,547 → 54,154 | 53,834 → 55,449 | | AIR reference in source bundle | 45,520 → 45,520 | 45,667 → 45,667 | 46,981 → 46,981 | The library page is 10,961 cl100k tokens; standalone AIR remains 175,736 bytes / 19 files / 50 sections. Full source growth is +3.1% relative to buffer/Vec, +58.9% since the 33,981-token pre-stdlib baseline. Quick Reference adds one short paragraph (+1.0%) rather than a new tutorial; the full ring invariant/API/accounting lives on demand. Cumulative growth and the existing front-door target overrun remain for owner review. This cost is justified by a resource-safe reusable ring using existing verifier-visible operations; documentation growth itself proves no compression benefit. No held-out corpus or task-specific semantic change was used. ### Checked implementation Eleven generic functions and five result enums use only existing safe source/AIR. The interpreter/backend/verifier are unchanged; the compiler embeds one extra source module. Independent Python deque vectors exercise 128 operations over four seeds with every logical element checked; growth covers all 71 small capacity/head/length combinations including empty/full/wrapped queues. Owners, files, senders, shared workers, failure ownership, cross-authority refunds and tag-inclusive quota checks pass. Invalid caller metadata cannot make free normally discard a nested owner. Borrow/task/Copy/move/leak rejections have stable existing codes. Validation passed full unit/conformance, focused per-instruction collection and 12-seed schedule stress (four quanta), canonical source/AIR and package/hosted CLI checks, source coverage, independent oracle checks, Zig formatting and reference bundle generation. Final counts are recorded in the commit. No native performance claim is made; construction/growth retains the interpreter's prefix-copy overhead.