#!/usr/bin/env python3 """Initialized storage, resource vectors, 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/buffers" 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", "200000", *arguments, path)) assert record["status"] == "returned" and record["value"] == 42, record return record assert "linear_buffers" in json.loads(cli("features"))["features"] example = REPO / "examples/std/vector.aji" returned(example, "heap=mem.alloc:256") report = json.loads(cli("report", FIXTURES / "owner.aji")) assert sum(function["allocations"] for function in report["functions"]) == 3, report with tempfile.TemporaryDirectory(prefix="aji-buffers-") as directory: root = Path(directory) air = root / "result.air" for fixture, grants in ( ("owner", ("heap=mem.alloc:128",)), ("push_failure", ("heap=mem.alloc:8",)), ("free_failure", ("heap=mem.alloc:128",)), ("reserve_failure", ("heap=mem.alloc:24",)), ("reserve_authority", ("first=mem.alloc:8", "second=mem.alloc:16")), ("long_reuse", ("heap=mem.alloc:16",)), ("nested", ("heap=mem.alloc:128",)), ("view", ("heap=mem.alloc:16",)), ("vec_copy", ("heap=mem.alloc:128",)), ("vec_replace_owner", ("heap=mem.alloc:128",)), ("vec_file", ("heap=mem.alloc:128", "dst=fs.write:/out")), ("vec_sender", ("heap=mem.alloc:128",)), ): 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) direct = returned(source, *grants) round_trip = returned(air, *grants) assert direct == round_trip, (direct, round_trip) # Compiler-pinned vectors retain nominal ownership across package modules. (root / "lib.aji").write_text( "module app.lib\nimport std.vec;\n" "fn identity(value: std.vec.Vec) -> std.vec.Vec effects [] { return move value; }\n" ) source = example.read_text().replace("module example.vector", "module app.main") source = source.replace("import std.vec;", "import std.vec;\nimport app.lib;") source = source.replace("::ok(value) => {", "::ok(value) => {\n let value = app.lib.identity::>(move value);") (root / "main.aji").write_text(source) manifest = root / "aji.toml" manifest.write_text('[package]\nname = "buffers"\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", "--fuel", "200000", "--manifest", manifest, "--grant", "heap=mem.alloc:128"))["value"] == 42 cli("explore", "--max-runs", "8", "--manifest", manifest, "--grant", "heap=mem.alloc:128") # Same buffer/Vec runtime in hosted mode; the run record is explicitly directed to a file. 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 ( ("borrow_conflict", "E2101"), ("task_loan", "E4101"), ("push_failure_leak", "E3002"), ("free_failure_leak", "E3002"), ("missing_move", "E7003"), ("duplicate_owner", "E2002"), ("recursive", "E1003"), ("vec_get_owner", "E7005"), ("generic_borrow_move", "E7006"), ): 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 ("task_loan_join", "vec_sender"): cli("explore", "--max-runs", "16", "--grant", "heap=mem.alloc:128", FIXTURES / f"{fixture}.aji") print(f"source buffers CLI integration passed ({checks} checks)")