cli: add safe owning numeric-key maps

Register std.map with bounded U64Map<V>, exact key equality and explicit
allocation/growth. Preserve returned ownership, old slots on reserve failure
and original-authority refunds; support Copy values, owners and resources.

Cover collision/tombstone/wrap behavior, independent dictionary traces,
quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI.
Publish the API, roadmap and measured documentation cost. General key
hashing/equality remains Q-037; numeric maps do not satisfy that gate.
This commit is contained in:
dev committed 2026-10-07 08:36:23 +03:00
1 parent fc23f1b23a
commit 57603860df
46 files changed
+2430 -15

No files matched your search

+8 -8
View File
@@ -1,16 +1,16 @@
# Owning hash map contract proposal
# Owning hash map design
Status: proposed STD-6d design; no map module is implemented or registered yet. Spec 16 defines the available std.hash API only. This document fixes the next implementation scope and review cases; it does not override the specification.
Status: STD-6d.2 numeric-key branch implementation (RFC 0028 Proposed); general keys remain Q-037. Spec 16 §2.13 is normative for std.map. This document records design rationale and does not override the specification.
## Key policy and scope
The eventual collection gate still requires HashMap<Key, Own<Value>>. Current type parameters are types, not hashing/equality operations (spec 15 §15); Copy supplies neither. Source has no callbacks, methods, user traits or value generic parameters. Treating a digest as key identity is incorrect because different keys can collide. Adding a language feature solely to make this API look generic would add more agent concepts before establishing their general value.
Start with `std.map.U64Map<V>`: exact u64 keys, generic Copy or linear values, existing source primitives only. Users with genuine numeric identifiers can use it directly. Bytes/struct keys cannot be converted to digests and then treated as unique numeric keys; general key hashing/equality is Q-037. Byte-key ownership needs a separate owning representation contract. Do not name this specialization HashMap<K,V> or claim that it satisfies the general-key gate.
Start with `std.map.U64Map<V>`: exact u64 keys, generic Copy or linear values, initialized buffer primitives and explicit error construction (RFC 0027). Users with genuine numeric identifiers can use it directly. Bytes/struct keys cannot be converted to digests and then treated as unique numeric keys; general key hashing/equality is Q-037. Byte-key ownership needs a separate owning representation contract. Do not name this specialization HashMap<K,V> or claim that it satisfies the general-key gate.
## Proposed public API
## Public API
Ten generic functions, each explicitly specialized `::<V>`; only new/reserve declare effects [mem.alloc] and take cap<mem.alloc>. All other functions declare effects []. Names/types below are proposed, unavailable calls today.
Ten generic functions, each explicitly specialized `::<V>`; only new/reserve declare effects [mem.alloc] and take cap<mem.alloc>. All other functions declare effects []. The numeric-key names/types below are implemented on the branch; spec 16 §2.13 is authoritative.
| Function | Result/ownership |
|---|---|
@@ -41,12 +41,12 @@ Expected probing depends on collisions/history; worst-case lookup/insert/remove
## Ownership and implementation gate
Preserved invariant: every live entry holds one complete V, occupied count equals length, keys are unique, and each occupied slot is reachable by its probe sequence. Occupied state corresponds exactly to some(V); vacant/deleted states correspond to none and own no V. Unused keys remain initialized Copy values, never identity sentinels. Existing aggregate/enum availability, exchange, slice bounds, mutable exclusion and task-loan rules verify every operation. Runtime uses ordinary deterministic AIR, initialized buffer accounting and no new instruction. No new diagnostic is expected: bad moves/leaks use E7003/E2002/E3002, alias/task conflicts E2101/E4101, Copy/Send/Share restrictions E7005/E4002. User owners, files and endpoints remain subject to ordinary resource/task rules; no destructor closes them. Existing AIR/APIs retain meaning; map support would be an additive registry entry, with its spec/reference delivered alongside code.
Preserved invariant: every live entry holds one complete V, occupied count equals length, keys are unique, and each occupied slot is reachable by its probe sequence. Occupied state corresponds exactly to some(V); vacant/deleted states correspond to none and own no V. Unused keys remain initialized Copy values, never identity sentinels. Existing aggregate/enum availability, exchange, slice bounds, mutable exclusion and task-loan rules verify every operation. Runtime uses ordinary deterministic AIR, initialized buffer accounting and no new instruction. No new diagnostic is expected: bad moves/leaks use E7003/E2002/E3002, alias/task conflicts E2101/E4101, Copy/Send/Share restrictions E7005/E4002. User owners, files and endpoints remain subject to ordinary resource/task rules; no destructor closes them. Existing AIR/APIs retain meaning; map support is an additive registry entry, with its spec/reference delivered alongside code.
The checked [slot prerequisite](../../tests/conformance/source/hash/map_storage.aji) already demonstrates read-only key/state access without consuming an owning payload, complete extraction and explicit cleanup; it does not implement a map. A valid owning use creates U64Map<own<u64>>, inserts a moved owner under key 7, handles replaced/full payloads, removes or pops and explicitly consumes the returned owner, then handles empty free. Rejected uses omit the move, ignore a linear replaced/full/pop result, copy an owner through get, free without consuming not_empty, mutate while a slice/task loan exists or share a file-valued map with workers. These become executable conformance examples with the implementation; this proposal is not evidence that they run today.
The checked [slot prerequisite](../../tests/conformance/source/hash/map_storage.aji) already demonstrates read-only key/state access without consuming an owning payload, complete extraction and explicit cleanup; it does not implement a map. A valid owning use creates U64Map<own<u64>>, inserts a moved owner under key 7, handles replaced/full payloads, removes or pops and explicitly consumes the returned owner, then handles empty free. Rejected uses omit the move, ignore a linear replaced/full/pop result, copy an owner through get, free without consuming not_empty, mutate while a slice/task loan exists or share a file-valued map with workers. Executable valid/rejected examples are in tests/conformance/source/map; RFC 0028 records validation.
Before marking the specialization done, test exact collisions, tombstones before matching keys, wraparound, a table with no empty slots, full-table replacement, zero capacity and all scalar keys. Compare mixed operations and all associations with an independent Python dict oracle. Include Copy/owner/file/sender cleanup, repeated reuse, peak-charge failure, exact failure-state preservation, cross-authority refunds and invalid-representation safety. Verify canonical AIR/package/hosted behavior, GC after each instruction and schedules for valid workers. Integrate normative rules in spec 16 and the API reference, register the module, measure bundle costs and report the remaining general-key gate explicitly.
## Agent complexity impact
This proposal adds no language concept, verifier state, syntax, diagnostic, callback, trait or runtime feature. The future library adds key equality, probing and returned replacement ownership; the latter reuses existing complete-value patterns. Ten operations avoid independent entry/cursor/borrow lifetimes. Public representation invariants increase documentation needs, so full details belong on demand. This draft stays outside the source/AIR bundles; the runnable hash increment measures its own cost in RFC 0026. No performance or Model Capability Compression result is inferred from the proposal or from benchmark tasks.
The numeric-key library adds no map-specific language concept, verifier state, syntax, diagnostic, callback, trait or runtime feature. Its separately implemented error construction prerequisite is RFC 0027. Key equality, probing and returned replacement ownership reuse existing complete-value patterns. Ten operations avoid independent entry/cursor/borrow lifetimes. Public representation invariants increase documentation needs, so full details belong on demand. This design stays outside the source/AIR bundles; RFC 0028 measures the normative API cost. No performance or Model Capability Compression result is inferred from fixtures or benchmark tasks.