Files
aji/tests/hosted/source_deque.py
T
dev 93405f69ce cli: add safe owning deque standard module
Embed std.deque with eleven generic operations implemented entirely in
safe Aji over initialized Option<T> buffer slots. Bounded pushes and
moving pops work at both ends; full pushes, out-of-bounds replacements
and nonempty free return ownership to the caller. Explicit reserve
preserves order and all state on allocation failure, charges the old
and new slot capacities at the allocation peak, and rotates whole
values after successful growth. No AIR, verifier or runtime change.

Specify the visible representation invariant, tag-inclusive allocation
accounting and empty-only cleanup. Add RFC 0025, a runnable owning
example, source/CLI conformance and CI coverage. Complete STD-6c;
HashMap, HashSet and BinaryHeap remain STD-6d.

Validation: unit tests and 785 full conformance cases; 28 deque fixtures;
focused collection after every instruction and 12 schedule seeds over
four quanta; 136 source/package/hosted CLI checks; an independent Python
deque oracle for 128 operations and 71 small growth configurations;
93/93 source coverage; Zig formatting, staged whitespace and bundles.

Reference cost: full source excluding AIR grows 52,384 -> 53,993 cl100k
tokens (+3.1%); Quick Reference 7,667 -> 7,747 (+1.0%). AIR stays at
175,736 bytes. Construction/growth still incur interpreter prefix-copy
overhead; no native performance or empirical MCC benefit is claimed.
2026-10-07 00:08:42 +03:00

103 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Safe owning ring order, explicit allocation, canonical AIR and CLI 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/deque"
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/deque.aji"
returned(example, "heap=mem.alloc:1024")
report = json.loads(cli("report", FIXTURES / "wrap_reserve.aji"))
assert sum(function["allocations"] for function in report["functions"]) == 6, report
with tempfile.TemporaryDirectory(prefix="aji-deque-") as directory:
root = Path(directory)
air = root / "result.air"
for fixture, grants in (
("copy", ("heap=mem.alloc:1024",)),
("owner", ("heap=mem.alloc:34",)),
("wrap_reserve", ("heap=mem.alloc:128",)),
("reserve_failure", ("heap=mem.alloc:80",)),
("reserve_authority", ("first=mem.alloc:27", "second=mem.alloc:45", "items=mem.alloc:16")),
("replace_owner", ("heap=mem.alloc:128",)),
("zero", ("heap=mem.alloc:8",)),
("full", ("heap=mem.alloc:25",)),
("allocation_tag", ("heap=mem.alloc:8",)),
("reuse", ("heap=mem.alloc:26",)),
("model", ("heap=mem.alloc:2048",)),
("growth_matrix", ("heap=mem.alloc:256",)),
("file", ("heap=mem.alloc:256", "dst=fs.write:/out")),
("sender", ("heap=mem.alloc:256",)),
):
source = FIXTURES / f"{fixture}.aji"
cli("check", source)
canonical = cli("lower", source)
assert cli("lower", source) == canonical
assert "linear_buffers" in canonical
air.write_text(canonical)
cli("check", air)
assert returned(source, *grants) == returned(air, *grants)
# Import order cannot change canonical specializations or linked declarations.
canonical = cli("lower", example)
reordered = root / "reordered.aji"
reordered.write_text(example.read_text().replace("import std.deque;\nimport std.option;", "import std.option;\nimport std.deque;"))
assert cli("lower", reordered) == canonical
# Ownership survives nominal imported contracts and manifest loading.
(root / "lib.aji").write_text(
"module app.lib\nimport std.deque;\n"
"fn identity<T>(value: std.deque.Deque<T>) -> std.deque.Deque<T> effects [] { return move value; }\n"
)
source = example.read_text().replace("module example.deque", "module app.main")
source = source.replace("import std.deque;", "import std.deque;\nimport app.lib;")
source = source.replace("let value = opened::<own<u64>>(2, heap);", "let original = opened::<own<u64>>(2, heap);\n let value = app.lib.identity::<own<u64>>(move original);")
(root / "main.aji").write_text(source)
manifest = root / "aji.toml"
manifest.write_text('[package]\nname = "deque"\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, "--grant", "heap=mem.alloc:128"))["value"] == 42
cli("explore", "--max-runs", "8", "--manifest", manifest, "--grant", "heap=mem.alloc:128")
result_path = root / "hosted.json"
cli("run", "--profile", "hosted", "--result-file", result_path, "--fuel", "200000", "--grant", "heap=mem.alloc:128", example)
record = json.loads(result_path.read_text())
assert record["status"] == "returned" and record["value"] == 42, record
for fixture, expected in (
("alias", "E2101"),
("slot_view", "E2101"),
("loan", "E4101"),
("not_share", "E4002"),
("missing_move", "E7003"),
("double_move", "E2002"),
("get_owner", "E7005"),
("free_leak", "E3002"),
("full_leak", "E3002"),
("pop_leak", "E3002"),
):
for command in ("check", "report", "run", "explore"):
diagnostics = [json.loads(line) for line in cli(command, FIXTURES / f"{fixture}.aji", code=1).splitlines()]
assert diagnostics[0]["code"] == expected, diagnostics
for fixture in ("loan_join", "sender", "shared"):
cli("explore", "--max-runs", "12", "--grant", "heap=mem.alloc:256", FIXTURES / f"{fixture}.aji")
print(f"source deque CLI integration passed ({checks} checks)")