Files
aji/docs/rfcs/0031-owning-general-key-set.md
T

5.5 KiB

RFC 0031: Safe owning general-key hash set

Status: Proposed Scope: STD-6d.5, safe library source after RFCs 0029/0030

Motivation and normative changes

General maps provide exact explicit hash/equality and complete owning keys. Spec 16 §2.15 adds std.hashset without set-specific language machinery. Duplicate insertion returns the original input and retains the existing representative, exposing resource ownership without hidden destructors.

Preserved invariant

The underlying general map's dense/probe invariant holds with bool values true. Every live K has one owned occurrence; equal keys are unique. Keys/hash/equality remain stable. Duplicate/full/failing-growth paths preserve stored entries/buckets and return input ownership as specified.

Verifier rule

No new transfer function: complete struct/enum/buffer moves, exact static callable contracts, borrow/task-loan exclusion, bounds, effects, Copy/Send/Share and explicit consumption apply. All code is ordinary safe Aji.

Runtime and deterministic behavior

Nine wrappers use existing map AIR calls, without runtime dispatch, allocation beyond explicit new/reserve or cleanup. Normal failure preserves all old state; peak charges/refunds and deterministic pop order follow the map. User callback traps/fuel are abnormal outcomes.

Valid and rejected examples

An owning byte/struct key is moved into insert. Duplicate/full returns the original key for explicit consumption. A successful remove/pop returns the stored key; nonempty free returns the set. Missing moves, ignored owning results, bad aliases/task loans, invalid effect/policy bindings and sharing non-Share keys reject through existing diagnostics.

The owning example allocates one slot and one payload with a 67-byte grant, inserts the complete key, pops/unpacks/takes it and frees the empty set. Owning duplicates return the incoming payload and retain the original. A leaked duplicate rejects with E3002; missing move rejects with E7003; a file key shared with a worker rejects with E4002.

Diagnostics / compatibility / AIR

No new code, opcode, AIR feature, source syntax or verifier/runtime state. Existing source/type/availability/alias/effect/Send/Share diagnostics apply. Registry addition is additive and imports grant no authority; old APIs retain meaning.

Agent complexity impact

No additional language concept beyond explicit static policy arguments. Nine operations omit value lookup and replacement alternatives; duplicate handling clearly distinguishes stored and returned owner. An extra membership check reuses the map and its stable callback laws, avoiding a new primitive or overwrite/destructor rule. Full API/representation/accounting stays on demand; measured bundle growth and executable validation are recorded below. No empirical MCC result is claimed.

No new syntax form, diagnostic or verifier state. Relative to RFC 0030, cl100k Quick Reference grows 8,124 → 8,201 (+0.9%); other source documentation 53,363 → 54,798 (+2.7%); full source excluding AIR 61,487 → 62,999 (+2.5%). The on-demand std reference is 17,939 tokens. Corresponding o200k counts are 8,234 / 54,977 / 63,211; qwen2.5 counts are 8,451 / 56,295 / 64,746. AIR remains 179,282 bytes, 19 files and 51 sections; its source-bundle copy remains 46,300 / 46,451 / 47,800 tokens. Cumulative cl100k source growth from the 33,981-token pre-std baseline is +85.4%. The increase buys explicit duplicate ownership, nine signatures and representation/failure/quota laws; mandatory Quick Reference stays at one short paragraph. No language expansion or measured compression benefit is claimed.

Validation gate

Copy/owning/byte keys, duplicate/full preservation, collisions, pop/remove order, zero capacity, growth failure/refunds/peak, rejected leaks/moves/loans/policy/effects, independent Python set traces, source/AIR/package/profile checks, GC/scheduler stress and documentation budgets are required. BinaryHeap and I/O/path remain following increments.

Validation result

Full conformance passes 973/973, including 26 set fixtures. All runtime set fixtures pass collection before every instruction, including both independent model traces; shared lookup runs 12 schedule seeds under quanta 1, 2, 7 and its own quantum. Existing compilation checks reject owner leaks, missing moves, bad borrows/task loans, wrong nominal policies, missing effects and non-Share file keys. Copy and owning key traces use independent Python set/representative/dense-order oracles for 80 operations, checking every domain key after every operation. Duplicate input/stored representative and complete probe metadata, owning byte keys, files, full/remove/pop/nonempty-free ownership, zero/overflow, both failure phases, rollback, exact peaks and cross-authority refunds are covered.

Unit/Quick Reference checks, formatting, hosted smoke, agent-reference generation, source coverage (106/106 constructs) and 53 CLI checks pass. CLI checks compare source/AIR results, hosted execution, explicit package policies, reordered imports, report/audit and callback provenance. AIR and source syntax remain unchanged. The separate general-map full GC/stress process subsequently completed with all 33 cases passing, without wall-clock termination. BinaryHeap and I/O/path remain subsequent work.