Files
dev 89c63c306b cli: add standard hashes and propose owning map contract
Embed std.hash with four ordinary safe byte functions: FNV-1a 64 and
Castagnoli CRC-32C, each with fresh and numeric continuation forms.
Specify exact constants, empty/chunk semantics, arbitrary continuation
states, target-independent numeric results and explicit serialization.
No heap allocation, authority, AIR, verifier or runtime change.

Propose the next U64Map<V> contract: exact key equality, bounded insert,
returned replacement/full ownership, tombstones, moving drain and
failure-preserving peak-charged rehash. Keep general HashMap<K,V> key
hash/equality policy open as Q-037. A checked slot prerequisite confirms
read-only Copy key/state fields over Option<V>, with explicit accounting
for both tags and owning payload cleanup; no map module is registered.

Validation: unit tests, 797 full conformance cases, 12 new fixtures;
all 11 hash cases with per-instruction collection and 12 schedule seeds
across four quanta, plus owning slot collection; 84 source/AIR, package
and hosted CLI checks; independent forward-polynomial CRC and arbitrary
precision FNV oracles for 286 inputs / 1144 results; 95/95 source coverage;
Zig formatting, whitespace and reference generation. Published vectors
come from RFC 9923 and RFC 3720, with explicit numeric/wire conventions.

Reference cost: source excluding AIR 53,993 -> 55,281 cl100k (+2.4%);
Quick Reference 7,747 -> 7,806 (+0.8%); AIR remains 175,736 bytes.
Map design remains outside both bundles. No empirical MCC or native
throughput claim; CRC initially uses eight bit steps per byte.
2026-10-07 00:53:50 +03:00

90 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Pure hashes, canonical source/AIR, package/hosted CLI and borrow contracts."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / "zig-out/bin/aji"
FIXTURES = REPO / "tests/conformance/source/hash"
checks = 0
def cli(*args, code=0):
global checks
result = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert result.returncode == code, (args, result.returncode, result.stdout, result.stderr)
checks += 1
return result.stdout
def returned(path, *grants):
arguments = [argument for grant in grants for argument in ("--grant", grant)]
record = json.loads(cli("run", "--fuel", "2000000", *arguments, path))
assert record["status"] == "returned" and record["value"] == 42, record
return record
example = REPO / "examples/std/hash.aji"
returned(example)
with tempfile.TemporaryDirectory(prefix="aji-hash-") as directory:
root = Path(directory)
air = root / "result.air"
for name in ("known", "single_bytes", "patterns", "streaming", "borrow", "shared"):
source = FIXTURES / f"{name}.aji"
cli("check", source)
canonical = cli("lower", source)
assert cli("lower", source) == canonical
assert "effects []" in canonical and "unsafe" not in canonical
air.write_text(canonical)
cli("check", air)
assert returned(source) == returned(air)
report = json.loads(cli("report", source))
assert all(function["allocations"] == 0 for function in report["functions"]), report
assert report["entry"]["capabilities"] == [], report
# Proposed map storage can observe Copy metadata without consuming V.
source = FIXTURES / "map_storage.aji"
cli("check", source)
canonical = cli("lower", source)
assert cli("lower", source) == canonical
air.write_text(canonical)
cli("check", air)
assert returned(source, "heap=mem.alloc:26") == returned(air, "heap=mem.alloc:26")
report = json.loads(cli("report", source))
assert sum(function["allocations"] for function in report["functions"]) == 2, report
canonical = cli("lower", example)
reordered = root / "reordered.aji"
reordered.write_text(example.read_text().replace("import std.hash;\nimport std.bytes;", "import std.bytes;\nimport std.hash;"))
assert cli("lower", reordered) == canonical
# A user's local name does not intercept a qualified std call.
collision = root / "collision.aji"
collision.write_text(example.read_text().replace("fn main()", "fn __aji_std_hash_fnv1a64(data: slice<u8>) -> u64 effects [] { return 0; }\nfn main()"))
returned(collision)
assert "@__aji_std_hash_fnv1a64_1" in cli("lower", collision)
(root / "lib.aji").write_text("module app.lib\nimport std.hash;\nfn checksum(data: slice<u8>) -> u32 effects [] { return std.hash.crc32c(data); }\n")
(root / "main.aji").write_text('module app.main\nimport app.lib;\nfn main() -> u64 effects [] { assert(app.lib.checksum(b"123456789") == 0xe3069283u32); return 42; }\n')
manifest = root / "aji.toml"
manifest.write_text('[package]\nname = "hash"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.lib = "lib.aji"\n')
cli("check", "--manifest", manifest)
cli("report", "--manifest", manifest)
assert json.loads(cli("run", "--manifest", manifest))["value"] == 42
cli("explore", "--max-runs", "8", "--manifest", manifest)
result_path = root / "hosted.json"
assert cli("run", "--profile", "hosted", "--result-file", result_path, example) == ""
assert json.loads(result_path.read_text())["value"] == 42
# No installed compiler needs std files from its working directory.
isolated = subprocess.run([str(AJI), "run", str(example)], cwd=root, capture_output=True, text=True)
assert isolated.returncode == 0 and json.loads(isolated.stdout)["value"] == 42, isolated
checks += 1
for name, code in (("missing_import", "E7002"), ("wrong_slice", "E7005"), ("wrong_state", "E7005"), ("generic", "E7006"), ("alias", "E2101")):
source = FIXTURES / f"{name}.aji"
for command in ("check", "report", "run", "explore"):
records = [json.loads(line) for line in cli(command, source, code=1).splitlines()]
assert [record["code"] for record in records] == ([code, code] if name == "alias" else [code]), records
assert records[0]["file"].endswith(source.name), records
for seed in (0, 7, 19):
assert json.loads(cli("run", "--seed", seed, example))["value"] == 42
cli("explore", "--max-runs", "12", FIXTURES / "shared.aji")
print(f"source hash CLI checks passed ({checks})")