#!/usr/bin/env python3 """Explicit package inputs through the CLI; runs use fuel, never timeouts.""" import json import os 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, cwd=None): global checks result = subprocess.run([str(AJI), *map(str, args)], cwd=cwd, capture_output=True, text=True) assert result.returncode == code, (args, result.returncode, result.stdout, result.stderr) checks += 1 return result def write_manifest(root, mapping='app.model = "src/model.aji"', grants=""): path = root / "aji.toml" path.write_text('[package]\nname = "fixture"\nversion = "1"\nentry = "src/main.aji"\n[modules]\n' + mapping + '\n' + grants) return path with tempfile.TemporaryDirectory(prefix="aji-source-modules-") as directory: root = Path(directory) (root / "src").mkdir() main = root / "src/main.aji" model = root / "src/model.aji" main.write_text((REPO / "examples/modules/main.aji").read_text()) model.write_text((REPO / "examples/modules/model.aji").read_text()) manifest = write_manifest(root) cli("check", "--manifest", manifest) record = cli("run", "--fuel", "1000000", "--manifest", manifest) assert json.loads(record.stdout) == {"status": "returned", "value": 42} canonical = cli("lower", "--manifest", manifest).stdout assert cli("lower", "--manifest", manifest, cwd="/").stdout == canonical annotation = cli("lower", "--annotate", "--manifest", manifest).stdout assert "; src/model.aji:" in annotation cli("lower", "--stats", "--manifest", manifest) cli("report", "--manifest", manifest) cli("check", "--report", "unsafe", "--manifest", manifest) cli("explore", "--max-runs", "8", "--manifest", manifest) # No ambient lookup, even with an aji.toml next to the entry. diagnostic = json.loads(cli("check", main, code=1).stdout) assert diagnostic["code"] == "E7002" cli("run", "--manifest", manifest, main, code=2) cli("lower", "--manifest", manifest, main, code=2) cli("check", "--manifest", manifest, main, code=2) # Mapping/import order cannot change canonical output. other = root / "src/other.aji" other.write_text("module app.other\nstruct Pair { value: u64 }\n") original = main.read_text() main.write_text(original.replace("import app.model;", "import app.model;\nimport app.other;")) write_manifest(root, 'app.other = "src/other.aji"\napp.model = "src/model.aji"') ordered = cli("lower", "--manifest", manifest).stdout write_manifest(root, 'app.model = "src/model.aji"\napp.other = "src/other.aji"') main.write_text(main.read_text().replace("import app.model;\nimport app.other;", "import app.other;\nimport app.model;")) assert cli("lower", "--manifest", manifest).stdout == ordered main.write_text(original) write_manifest(root) # Errors and traps name the actual imported source. saved_model = model.read_text() model.write_text("module app.model\nfn broken(\n") diagnostic = json.loads(cli("check", "--manifest", manifest, code=1).stdout) assert diagnostic["code"] == "E7001" and diagnostic["file"] == "src/model.aji" assert "expected" in diagnostic and "found" in diagnostic model.write_text("module app.model\nfn total(value: u64) -> u64 effects [] { assert(false); return value; }\n") main.write_text("module app.entry\nimport app.model;\nfn main() -> u64 effects [] { return app.model.total(42); }\n") trapped = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, code=4).stdout) assert trapped["source_location"]["file"] == "src/model.aji" main.write_text(original) model.write_text(saved_model) # Imported authority is explicit; checking never instantiates grants. model.write_text('module app.model\nfn emit(out: cap) -> void effects [io.write] { io.stdout_write(out, b"module") or trap; }\n') main.write_text("module app.entry\nimport app.model;\nfn main(out: cap) -> u64 effects [io.write] { app.model.emit(out); return 42; }\n") cli("check", "--manifest", manifest) missing = cli("run", "--fuel", "1000000", "--manifest", manifest, code=6) assert json.loads(missing.stdout)["status"] == "not_instantiated" write_manifest(root, grants='[capabilities]\nout = "io.write"\n') granted = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest).stdout) assert granted["stdout"] == "module" overridden = cli("run", "--fuel", "1000000", "--manifest", manifest, "--grant", "out=io.read", code=6) assert json.loads(overridden.stdout)["status"] == "not_instantiated" cli("explore", "--max-runs", "8", "--manifest", manifest) result_file = root / "result.json" hosted = cli("run", "--profile", "hosted", "--fuel", "1000000", "--result-file", result_file, "--manifest", manifest) assert hosted.stdout == "module" and json.loads(result_file.read_text())["value"] == 42 main.write_text(original) model.write_text(saved_model) write_manifest(root) # Invalid paths/reserved names reject as manifest errors, without source execution. for mapping in ('app.model = "../outside.aji"', 'app.model = "/outside.aji"', 'app.model = "src/./model.aji"', 'std.bytes = "src/model.aji"', 'app.model = "src/model.aji"\napp.model = "src/other.aji"'): write_manifest(root, mapping) assert cli("check", "--manifest", manifest, code=2).stdout == "" write_manifest(root, 'app.model = "src/missing.aji"') cli("check", "--manifest", manifest, code=2) # Reject source symlinks and directory symlinks, including entry links. outside = root / "outside.aji" outside.write_text(saved_model) link = root / "src/link.aji" link.symlink_to(outside) write_manifest(root, 'app.model = "src/link.aji"') cli("check", "--manifest", manifest, code=2) alias = root / "alias" alias.symlink_to(root / "src", target_is_directory=True) write_manifest(root, 'app.model = "alias/model.aji"') cli("check", "--manifest", manifest, code=2) entry_link = root / "src/entry-link.aji" entry_link.symlink_to(main) write_manifest(root) manifest.write_text(manifest.read_text().replace('entry = "src/main.aji"', 'entry = "src/entry-link.aji"')) cli("check", "--manifest", manifest, code=2) # Static pipes/directories are rejected without a blocking read. pipe = root / "src/pipe.aji" os.mkfifo(pipe) write_manifest(root, 'app.model = "src/pipe.aji"') cli("check", "--manifest", manifest, code=2) (root / "src/directory.aji").mkdir() write_manifest(root, 'app.model = "src/directory.aji"') cli("check", "--manifest", manifest, code=2) large = root / "src/large.aji" with large.open("wb") as stream: stream.truncate((64 << 20) + 1) write_manifest(root, 'app.model = "src/large.aji"') cli("check", "--manifest", manifest, code=2) # Aggregate budget is enforced before parsing even if each input fits alone. with main.open("wb") as stream: stream.truncate(63 << 20) with large.open("wb") as stream: stream.truncate(2 << 20) cli("check", "--manifest", manifest, code=2) print(f"source module CLI integration passed ({checks} checks)")