- interpreter core rewritten around explicit per-task frame stacks so a task can be suspended at any instruction; behavior of all existing tests unchanged - tasks: scope and task<T> (function-bound; task_transfer and task_borrow_returns reserved), spawn/join/cancel/cancelled/scope.end; cooperative scheduler with seeded choice rule, separate schedule seed, quantum = fuel unit, task limit (limit_exceeded kind tasks), global quiescence over waiting tasks; schedule field in the run record - verifier: Send/Share table (E4001, E4002), loans as task-owned borrows (E4302 on early end, E4101 on conflicts), E4301 at scope end, task effects (E3101) - CLI --quantum, --task-limit, --schedule-seed; runner schedules:, quantum:, task_limit:, limit_exceeded: - fixed: schedule seed default used orelse/^ precedence wrongly - 15 phase-3 conformance tests; 4000 mutants without a crash - Agent Reference Bundle +10.9 % tokens (recorded)
281 lines
14 KiB
Zig
281 lines
14 KiB
Zig
//! `aji` command-line driver.
|
|
//!
|
|
//! Output contract (stable, consumed by agents):
|
|
//! - diagnostics go to stdout as JSON Lines, one object per line;
|
|
//! - human-oriented notes go to stderr;
|
|
//! - `aji run` writes one run record (spec 07a "Run result") to stdout;
|
|
//! - exit codes: 0 accepted / returned, 1 rejected, 2 usage or I/O error,
|
|
//! 3 input not decidable yet (pipeline stage unimplemented), 4 trapped,
|
|
//! 5 execution limit exceeded (`--fuel`, spec 11 §7),
|
|
//! 6 not instantiated (missing or invalid `--grant`, spec 05 §10.2),
|
|
//! 7 blocked: no future event can complete a wait (spec 11 §8).
|
|
|
|
const std = @import("std");
|
|
const aji = @import("aji");
|
|
|
|
const usage =
|
|
\\usage: aji <command> [args]
|
|
\\
|
|
\\commands:
|
|
\\ check [--report unsafe] <file.air>
|
|
\\ parse and verify a text AIR module; the unsafe report
|
|
\\ lists trusted boundaries and unsafe operations
|
|
\\ run [--fuel N] [--grant P=KIND[:SCOPE]]... <file.air | --manifest aji.toml>
|
|
\\ verify, then run @main and print the run record;
|
|
\\ --fuel stops after N executed instructions;
|
|
\\ --grant binds capability parameter P of @main
|
|
\\ (and replaces the manifest's grant for P);
|
|
\\ deterministic world: --stdin TEXT, --vfs PATH=TEXT,
|
|
\\ --seed N; scheduler: --quantum N, --task-limit N,
|
|
\\ --schedule-seed N; --profile hosted uses the real OS and
|
|
\\ writes the record only to --result-file PATH
|
|
\\ report <file.air> verify, then print the audit report (spec 10 §6.1)
|
|
\\ codes list diagnostic codes as JSON Lines
|
|
\\ features print supported AIR format, language version and features
|
|
\\ explain <code> show one diagnostic code
|
|
\\ version print toolchain version
|
|
\\
|
|
;
|
|
|
|
const ExitCode = enum(u8) { accepted = 0, rejected = 1, usage = 2, unimplemented = 3, trapped = 4, limit_exceeded = 5, not_instantiated = 6, blocked = 7 };
|
|
|
|
pub fn main(init: std.process.Init) !void {
|
|
const arena = init.arena.allocator();
|
|
const io = init.io;
|
|
const args = try init.minimal.args.toSlice(arena);
|
|
|
|
var stdout_buf: [4096]u8 = undefined;
|
|
var stdout_fw: std.Io.File.Writer = .init(.stdout(), io, &stdout_buf);
|
|
const out = &stdout_fw.interface;
|
|
|
|
const code = run(io, arena, args, out) catch |err| switch (err) {
|
|
error.WriteFailed => return stdout_fw.err.?,
|
|
else => |e| return e,
|
|
};
|
|
try out.flush();
|
|
std.process.exit(@backingInt(code));
|
|
}
|
|
|
|
fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *std.Io.Writer) !ExitCode {
|
|
if (args.len < 2) {
|
|
std.debug.print("{s}", .{usage});
|
|
return .usage;
|
|
}
|
|
const cmd = args[1];
|
|
if (std.mem.eql(u8, cmd, "version")) {
|
|
try out.print("aji {s} (air format {d})\n", .{ aji.version, aji.air.format_version });
|
|
return .accepted;
|
|
}
|
|
if (std.mem.eql(u8, cmd, "report")) {
|
|
if (args.len != 3) return usageError("report takes exactly one file");
|
|
const path = args[2];
|
|
const source = std.Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 << 20)) catch |err| {
|
|
std.debug.print("aji: cannot read {s}: {t}\n", .{ path, err });
|
|
return .usage;
|
|
};
|
|
const result = try aji.check.checkAir(arena, path, source);
|
|
for (result.diagnostics) |d| {
|
|
try d.writeJson(out);
|
|
try out.writeByte('\n');
|
|
}
|
|
const module = result.module orelse return .rejected;
|
|
try aji.report.write(arena, module, out);
|
|
try out.writeByte('\n');
|
|
return .accepted;
|
|
}
|
|
if (std.mem.eql(u8, cmd, "features")) {
|
|
var s: std.json.Stringify = .{ .writer = out };
|
|
try s.write(.{
|
|
.air = aji.air.format_version,
|
|
.lang = std.fmt.comptimePrint("{d}.{d}", .{ aji.air.lang_version[0], aji.air.lang_version[1] }),
|
|
.features = &aji.air.features.supported,
|
|
});
|
|
try out.writeByte('\n');
|
|
return .accepted;
|
|
}
|
|
if (std.mem.eql(u8, cmd, "codes")) {
|
|
for (aji.diag.registry) |c| try writeCode(out, c);
|
|
return .accepted;
|
|
}
|
|
if (std.mem.eql(u8, cmd, "explain")) {
|
|
if (args.len != 3) return usageError("explain takes exactly one code");
|
|
const c = aji.diag.lookup(args[2]) orelse return usageError("unknown diagnostic code");
|
|
try writeCode(out, c.*);
|
|
return .accepted;
|
|
}
|
|
if (std.mem.eql(u8, cmd, "check") or std.mem.eql(u8, cmd, "run")) {
|
|
var options: aji.interp.Options = .{};
|
|
var grants: std.ArrayList(aji.interp.Grant) = .empty;
|
|
var files: std.ArrayList(struct { []const u8, []const u8 }) = .empty;
|
|
var result_file: ?[]const u8 = null;
|
|
var rest = args[2..];
|
|
var unsafe_report = false;
|
|
if (std.mem.eql(u8, cmd, "check") and rest.len == 3 and std.mem.eql(u8, rest[0], "--report")) {
|
|
if (!std.mem.eql(u8, rest[1], "unsafe")) return usageError("--report takes unsafe");
|
|
unsafe_report = true;
|
|
rest = rest[2..];
|
|
}
|
|
var manifest_path: ?[]const u8 = null;
|
|
while (std.mem.eql(u8, cmd, "run") and rest.len >= 2) {
|
|
const flag = rest[0];
|
|
const value = rest[1];
|
|
if (std.mem.eql(u8, flag, "--fuel")) {
|
|
options.fuel = std.fmt.parseInt(u64, value, 10) catch return usageError("--fuel takes a non-negative integer");
|
|
} else if (std.mem.eql(u8, flag, "--grant")) {
|
|
try grants.append(arena, parseGrant(value) orelse return usageError("--grant takes <parameter>=<kind>[:<scope>]"));
|
|
} else if (std.mem.eql(u8, flag, "--profile")) {
|
|
if (std.mem.eql(u8, value, "hosted")) {
|
|
options.hosted = io;
|
|
} else if (!std.mem.eql(u8, value, "deterministic")) return usageError("--profile takes deterministic or hosted");
|
|
} else if (std.mem.eql(u8, flag, "--stdin")) {
|
|
options.world.stdin = value;
|
|
} else if (std.mem.eql(u8, flag, "--vfs")) {
|
|
const eq = std.mem.indexOfScalar(u8, value, '=') orelse return usageError("--vfs takes <path>=<text>");
|
|
try files.append(arena, .{ value[0..eq], value[eq + 1 ..] });
|
|
} else if (std.mem.eql(u8, flag, "--seed")) {
|
|
options.world.seed = std.fmt.parseInt(u64, value, 10) catch return usageError("--seed takes a non-negative integer");
|
|
} else if (std.mem.eql(u8, flag, "--quantum")) {
|
|
options.quantum = std.fmt.parseInt(u64, value, 10) catch return usageError("--quantum takes a positive integer");
|
|
if (options.quantum == 0) return usageError("--quantum takes a positive integer");
|
|
} else if (std.mem.eql(u8, flag, "--task-limit")) {
|
|
options.task_limit = std.fmt.parseInt(u64, value, 10) catch return usageError("--task-limit takes a non-negative integer");
|
|
} else if (std.mem.eql(u8, flag, "--schedule-seed")) {
|
|
options.schedule_seed = std.fmt.parseInt(u64, value, 10) catch return usageError("--schedule-seed takes a non-negative integer");
|
|
} else if (std.mem.eql(u8, flag, "--result-file")) {
|
|
result_file = value;
|
|
} else if (std.mem.eql(u8, flag, "--manifest")) {
|
|
manifest_path = value;
|
|
} else break;
|
|
rest = rest[2..];
|
|
}
|
|
options.world.files = files.items;
|
|
var path: []const u8 = undefined;
|
|
if (manifest_path) |mp| {
|
|
// spec 12 §1.2: the module is the manifest's entry; its grants
|
|
// are applied, each replaced by a --grant for the same parameter.
|
|
if (rest.len != 0) return usageError("a file argument cannot be combined with --manifest");
|
|
const text = std.Io.Dir.cwd().readFileAlloc(io, mp, arena, .limited(1 << 20)) catch |err| {
|
|
std.debug.print("aji: cannot read {s}: {t}\n", .{ mp, err });
|
|
return .usage;
|
|
};
|
|
const m = switch (try aji.manifest.parse(arena, text)) {
|
|
.ok => |m| m,
|
|
.err => |e| {
|
|
std.debug.print("aji: manifest {s}:{d}: {s}\n", .{ mp, e.line, e.message });
|
|
return .usage;
|
|
},
|
|
};
|
|
const entry = m.entry orelse {
|
|
std.debug.print("aji: manifest {s}: [package] entry is required for aji run\n", .{mp});
|
|
return .usage;
|
|
};
|
|
path = try std.fs.path.join(arena, &.{ std.fs.path.dirname(mp) orelse ".", entry });
|
|
var merged: std.ArrayList(aji.interp.Grant) = .empty;
|
|
for (m.grants) |g| {
|
|
const overridden = for (grants.items) |cg| {
|
|
if (std.mem.eql(u8, cg.parameter, g.parameter)) break true;
|
|
} else false;
|
|
if (!overridden) try merged.append(arena, .{ .parameter = g.parameter, .kind = g.kind, .scope = g.scope });
|
|
}
|
|
try merged.appendSlice(arena, grants.items);
|
|
options.grants = merged.items;
|
|
} else {
|
|
if (rest.len != 1) return usageError("check and run take exactly one file");
|
|
path = rest[0];
|
|
options.grants = grants.items;
|
|
}
|
|
const source = std.Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 << 20)) catch |err| {
|
|
std.debug.print("aji: cannot read {s}: {t}\n", .{ path, err });
|
|
return .usage;
|
|
};
|
|
const report = try aji.check.checkAir(arena, path, source);
|
|
for (report.diagnostics) |d| {
|
|
try d.writeJson(out);
|
|
try out.writeByte('\n');
|
|
}
|
|
if (unsafe_report) if (report.module) |module| try aji.verifier.unsafe_.writeReport(module, out);
|
|
if (report.status == .accepted and std.mem.eql(u8, cmd, "run")) {
|
|
const module = report.module.?;
|
|
var output: aji.interp.Output = undefined;
|
|
const hosted = options.hosted != null;
|
|
// In the hosted profile the program owns stdout and stderr: flush
|
|
// what the toolchain wrote before the program starts.
|
|
if (hosted) try out.flush() else options.output = &output;
|
|
const outcome = aji.interp.run(arena, module, "main", options) catch |err| switch (err) {
|
|
error.EntryNotFound => return usageError("module has no @main whose parameters are all capabilities"),
|
|
error.DuplicateGrant => return usageError("a parameter is granted more than once"),
|
|
else => |e| return e,
|
|
};
|
|
const captured: ?aji.interp.Output = if (hosted) null else output;
|
|
if (!hosted) {
|
|
try aji.interp.writeOutcome(module, outcome, captured, out);
|
|
try out.writeByte('\n');
|
|
} else if (result_file == null) {
|
|
// Human summary only; the machine interface is --result-file (spec 11 §8).
|
|
var rec: std.Io.Writer.Allocating = .init(arena);
|
|
try aji.interp.writeOutcome(module, outcome, null, &rec.writer);
|
|
std.debug.print("aji: {s}\n", .{rec.written()});
|
|
}
|
|
if (result_file) |rpath| {
|
|
var rec: std.Io.Writer.Allocating = .init(arena);
|
|
try aji.interp.writeOutcome(module, outcome, captured, &rec.writer);
|
|
try rec.writer.writeByte('\n');
|
|
std.Io.Dir.cwd().writeFile(io, .{ .sub_path = rpath, .data = rec.written() }) catch |err| {
|
|
std.debug.print("aji: cannot write {s}: {t}\n", .{ rpath, err });
|
|
return .usage;
|
|
};
|
|
}
|
|
return switch (outcome) {
|
|
.returned => .accepted,
|
|
.trapped => .trapped,
|
|
.limit_exceeded => .limit_exceeded,
|
|
.not_instantiated => .not_instantiated,
|
|
.blocked => .blocked,
|
|
};
|
|
}
|
|
return switch (report.status) {
|
|
.accepted => .accepted,
|
|
.rejected => .rejected,
|
|
.unimplemented => {
|
|
std.debug.print("aji: not implemented yet: {s}\n", .{report.missing_stage});
|
|
return .unimplemented;
|
|
},
|
|
};
|
|
}
|
|
return usageError("unknown command");
|
|
}
|
|
|
|
fn writeCode(out: *std.Io.Writer, c: aji.diag.Code) !void {
|
|
var s: std.json.Stringify = .{ .writer = out };
|
|
try s.write(.{ .code = c.id, .kind = c.kind, .meaning = c.meaning });
|
|
try out.writeByte('\n');
|
|
}
|
|
|
|
fn usageError(msg: []const u8) ExitCode {
|
|
std.debug.print("aji: {s}\n\n{s}", .{ msg, usage });
|
|
return .usage;
|
|
}
|
|
|
|
/// `<parameter>=<kind>[:<scope>]` (reference/capabilities.md).
|
|
pub fn parseGrant(text: []const u8) ?aji.interp.Grant {
|
|
const eq = std.mem.indexOfScalar(u8, text, '=') orelse return null;
|
|
const rest = text[eq + 1 ..];
|
|
const colon = std.mem.indexOfScalar(u8, rest, ':');
|
|
const g: aji.interp.Grant = .{
|
|
.parameter = text[0..eq],
|
|
.kind = if (colon) |c| rest[0..c] else rest,
|
|
.scope = if (colon) |c| rest[c + 1 ..] else null,
|
|
};
|
|
if (g.parameter.len == 0 or g.kind.len == 0) return null;
|
|
return g;
|
|
}
|
|
|
|
test parseGrant {
|
|
const g = parseGrant("data=fs.read:/var/data").?;
|
|
try std.testing.expectEqualStrings("data", g.parameter);
|
|
try std.testing.expectEqualStrings("fs.read", g.kind);
|
|
try std.testing.expectEqualStrings("/var/data", g.scope.?);
|
|
try std.testing.expect(parseGrant("out=io.write").?.scope == null);
|
|
try std.testing.expect(parseGrant("io.write") == null);
|
|
}
|