#!/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(value: std.deque.Deque) -> std.deque.Deque 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::>(2, heap);", "let original = opened::>(2, heap);\n let value = app.lib.identity::>(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)")