Files
aji/tests/hosted/source_static_functions.py
T
dev f02f4bd55b cli: add explicit static function parameters
Bind typed source functions with exact effect contracts during deterministic
specialization; retain nominal operation identity and ordinary AIR verification.

Add spec/RFC/grammar, checked Quick Reference examples, source/AIR conformance,
package/hosted CLI tests, scheduler/GC coverage and documentation budgets.
2026-10-07 10:23:03 +03:00

105 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""Static operations through source, AIR, packages and hosted execution; fuel bounds runs."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / "zig-out/bin/aji"
checks = 0
def cli(*args, code=0):
global checks
run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr)
checks += 1
return run.stdout
example = REPO / "examples/std/static_functions.aji"
cli("check", example)
assert json.loads(cli("run", "--fuel", "1000000", example))["value"] == 42
cli("report", example)
cli("explore", "--max-runs", "8", example)
canonical = cli("lower", example)
assert canonical == cli("lower", example)
assert "call @identity" in canonical and "fn_ref" not in canonical
with tempfile.TemporaryDirectory(prefix="aji-static-functions-") as directory:
root = Path(directory)
air = root / "result.air"
air.write_text(canonical)
cli("check", air)
assert json.loads(cli("run", "--fuel", "1000000", air))["value"] == 42
result = root / "hosted.json"
assert cli("run", "--profile", "hosted", "--fuel", "1000000", "--result-file", result, example) == ""
assert json.loads(result.read_text())["value"] == 42
entry, algo, policy, manifest = (root / name for name in ("main.aji", "algorithm.aji", "policy.aji", "aji.toml"))
manifest.write_text('[package]\nname = "static"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.algorithm = "algorithm.aji"\napp.policy = "policy.aji"\n')
algo.write_text('''module app.algorithm
struct Policy<T, F: fn(T) -> T effects []> { value: T }
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
fn read<T, F: fn(T) -> T effects []>(value: Policy<T, fn F>) -> T effects [] {
let (item) = struct.unpack(move value); return apply::<T, fn F>(move item);
}
''')
policy_text = '''module app.policy
fn identity<T: Copy>(value: T) -> T effects [] { return value; }
fn other<T: Copy>(value: T) -> T effects [] { return value; }
'''
policy.write_text(policy_text)
imports = "import app.algorithm;\nimport app.policy;\n"
body = '''fn main() -> u64 effects [] {
let value = app.algorithm.Policy::<u64, fn app.policy.identity::<u64>> { value: 42 };
return app.algorithm.read::<u64, fn app.policy.identity::<u64>>(value);
}
'''
entry.write_text("module app.entry\n" + imports + body)
assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest))["value"] == 42
cli("report", "--manifest", manifest)
cli("explore", "--max-runs", "8", "--manifest", manifest)
package_air = cli("lower", "--manifest", manifest)
entry.write_text("module app.entry\nimport app.policy;\nimport app.algorithm;\n" + body)
assert cli("lower", "--manifest", manifest) == package_air
# Equal layouts and signatures do not erase operation identity.
entry.write_text("module app.entry\n" + imports + body.replace("read::<u64, fn app.policy.identity", "read::<u64, fn app.policy.other"))
error = json.loads(cli("check", "--manifest", manifest, code=1))
assert error["code"] == "E7005" and Path(error["file"]).name == "main.aji", error
assert "identity" in error["found"] and "other" in error["expected"]
# Selecting an operation cannot bypass direct-import visibility.
entry.write_text("module app.entry\nimport app.algorithm;\n" + body)
error = json.loads(cli("check", "--manifest", manifest, code=1))
assert error["code"] == "E7002" and Path(error["file"]).name == "main.aji"
entry.write_text("module app.entry\n" + imports + body)
# Template-body failures retain the operation's provider filename.
policy.write_text(policy_text.replace("return value;", "assert(false); return value;"))
record = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, code=4))
assert record["source_location"]["file"] == "policy.aji"
policy.write_text(policy_text.replace("return value;", "return missing(value);"))
error = json.loads(cli("check", "--manifest", manifest, code=1))
assert error["code"] == "E7002" and error["file"] == "policy.aji"
# Contracts reject consistently at all CLI entry points, before execution.
policy.write_text("module app.policy\nfn identity<T>(value: T) -> bool effects [] { return true; }\n")
for command in ("check", "lower", "report", "run", "explore"):
error = json.loads(cli(command, "--manifest", manifest, code=1))
assert error["code"] == "E7005" and Path(error["file"]).name == "main.aji", error
assert error["parameter"] == "F" and error["expected"] == "fn(u64) -> u64 effects []"
assert error["found"] == "fn(u64) -> bool effects []"
# Recursive function-argument resolution is bounded before body expansion.
params = "T, F: fn(T) -> T effects []"
declaration = f"fn apply<{params}>(value: T) -> T effects [] {{ return F(move value); }}\n"
entry.write_text("module app.entry\nfn identity(value: u64) -> u64 effects [] { return value; }\n" + declaration +
"fn main() -> u64 effects [] { return apply::<u64, fn identity>(42u64); }\n")
assert json.loads(cli("run", "--fuel", "1000000", entry))["value"] == 42
argument = "fn identity"
for _ in range(70):
argument = f"fn apply::<u64, {argument}>"
entry.write_text("module app.entry\nfn identity(value: u64) -> u64 effects [] { return value; }\n" + declaration +
f"fn main() -> u64 effects [] {{ return apply::<u64, {argument}>(42u64); }}\n")
error = json.loads(cli("check", entry, code=1))
assert error["code"] == "E7006", error
print(f"static function CLI integration passed ({checks} checks)")