Files
aji/tests/hosted/source_buffers.py
T
dev 4138a52af6 cli: add initialized buffers and safe owning vectors
Introduce linear_buffers through the specification, AIR parser/serializer,
verifier, deterministic interpreter and source lowering. A buffer owns an
initialized prefix with explicit reserve, bounded push, moving pop/views and
recoverable empty-only free. Growth charges old+new capacity; failed growth,
full push and nonempty free preserve or return complete ownership.

Implement compiler-pinned safe std.vec with nine operations for Copy values,
owners, files and channel endpoints. Permit complete Copy moves inside
specialized generic bodies without weakening monomorphic or borrow rules.
Update RFC 0024, Q-036, STD-6a/b, reference grammar, Quick Reference, bundles,
source coverage and CI. Remaining collections stay STD-6c.

Validation: unit tests and AIR round-trip; 757 conformance cases; 56 dedicated
buffer cases with per-instruction collection and 32-seed schedule stress;
extended memory test; hosted smoke; 36 module, 20 generic, 49 linear and
118 buffer CLI checks; 91/91 source constructs; Zig fmt and reference bundles.
Full source bundle: 49,589 -> 52,384 cl100k tokens (+5.6%); AIR 175,736 bytes.
The reference interpreter's push copies its initialized prefix; no native
performance or empirical MCC benefit is claimed.
2026-10-06 23:26:41 +03:00

99 lines
4.5 KiB
Python

#!/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<T>(value: std.vec.Vec<T>) -> std.vec.Vec<T> 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::<own<u64>>(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)")