Phase 2: I/O intrinsics, linear resources, deterministic profile (RFC 0007)

- air: resource types file<read|write|append>, listener, stream; io_error;
  intrinsic registry (19 intrinsics) synced with reference/intrinsics.md;
  fallible intrinsics as ok/err terminators; fs prefix narrowing;
  linear_aggregates gating
- verifier: intrinsic form, operand, result and capability-kind checks
  (E1001, E3201, E5002); intrinsic effects (E3101); resource linearity
  in the borrow walker with per-edge moves: E3002 leaks and join
  mismatches, E3001, E2001, E2002, E2101 for close while borrowed;
  resources as borrow roots
- interp: deterministic world (captured console, VFS, loopback TCP,
  instruction clock, pinned xoshiro256 PRNG), blocked outcome, run
  record with stdout/stderr/files
- CLI: --stdin, --vfs, --seed, --result-file, --profile (hosted: exit 3),
  exit code 7; runner: stdin/vfs/seed/stdout/stderr/file/blocked headers
- conformance: hello, file copy and TCP echo exit programs; 37 new tests;
  4000 mutants without a crash
This commit is contained in:
dev committed 2026-10-04 14:02:17 +03:00
1 parent 571cc6e362
commit d405d4ea50
49 files changed
+2325 -81

No files matched your search

+77 -2
View File
@@ -22,6 +22,8 @@ const Expect = union(enum) {
trap: []const u8,
/// Expected `reason` of a `not_instantiated` record.
not_instantiated: []const u8,
/// Expected `blocked` outcome (spec 11 §8).
blocked,
};
};
@@ -35,6 +37,11 @@ const Header = struct {
expect: Expect,
fuel: u64,
grants: []const aji.interp.Grant,
world: aji.interp.WorldOptions,
/// Expected captured output and written files (all optional).
stdout: ?[]const u8,
stderr: ?[]const u8,
files: []const struct { []const u8, []const u8 },
phase: u8,
spec: []const u8,
};
@@ -164,14 +171,15 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run, header: Header) !Result {
var records: [2][]const u8 = undefined;
var outcome: aji.interp.Outcome = undefined;
var output: aji.interp.Output = undefined;
for (&records) |*rec| {
outcome = aji.interp.run(arena, module, "main", .{ .fuel = header.fuel, .grants = header.grants }) catch |err| switch (err) {
outcome = aji.interp.run(arena, module, "main", .{ .fuel = header.fuel, .grants = header.grants, .world = header.world, .output = &output }) catch |err| switch (err) {
error.EntryNotFound => return .{ .outcome = .fail, .detail = "module has no @main whose parameters are all capabilities" },
error.DuplicateGrant => return .{ .outcome = .fail, .detail = "a parameter is granted more than once" },
else => |e| return e,
};
var w: std.Io.Writer.Allocating = .init(arena);
try aji.interp.writeOutcome(module, outcome, &w.writer);
try aji.interp.writeOutcome(module, outcome, output, &w.writer);
rec.* = w.written();
}
if (!std.mem.eql(u8, records[0], records[1]))
@@ -181,7 +189,21 @@ fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run,
.trap => |code| outcome == .trapped and std.mem.eql(u8, outcome.trapped.code.id, code),
.result => |json| outcome == .returned and std.mem.eql(u8, json, try valueJson(arena, module, outcome.returned)),
.not_instantiated => |reason| outcome == .not_instantiated and std.mem.eql(u8, outcome.not_instantiated.reason, reason),
.blocked => outcome == .blocked,
};
if (ok) {
if (header.stdout) |expected| if (!std.mem.eql(u8, expected, output.stdout))
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "stdout differs: {s}", .{records[0]}) };
if (header.stderr) |expected| if (!std.mem.eql(u8, expected, output.stderr))
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "stderr differs: {s}", .{records[0]}) };
for (header.files) |expected| {
const got = for (output.files) |f| {
if (std.mem.eql(u8, f[0], expected[0])) break f[1];
} else null;
if (got == null or !std.mem.eql(u8, got.?, expected[1]))
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "file {s} differs: {s}", .{ expected[0], records[0] }) };
}
}
if (ok) return .{ .outcome = .pass };
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "got {s}", .{records[0]}) };
}
@@ -211,6 +233,31 @@ fn unsupportedFeature(source: []const u8) ?[]const u8 {
return null;
}
/// Header values are single lines: `\n`, `\t`, `\\` and `\xHH` escapes.
fn unescape(arena: std.mem.Allocator, s: []const u8) ![]const u8 {
var out: std.ArrayList(u8) = .empty;
var i: usize = 0;
while (i < s.len) : (i += 1) {
if (s[i] != '\\' or i + 1 == s.len) {
try out.append(arena, s[i]);
continue;
}
i += 1;
switch (s[i]) {
'n' => try out.append(arena, '\n'),
't' => try out.append(arena, '\t'),
'\\' => try out.append(arena, '\\'),
'x' => {
if (i + 2 >= s.len) return error.BadExpect;
try out.append(arena, std.fmt.parseInt(u8, s[i + 1 .. i + 3], 16) catch return error.BadExpect);
i += 2;
},
else => return error.BadExpect,
}
}
return out.items;
}
fn isKey(s: []const u8) bool {
if (s.len == 0) return false;
for (s) |ch| if (!std.ascii.isLower(ch) and ch != '_') return false;
@@ -238,6 +285,7 @@ fn formatExpect(e: Expect, w: *std.Io.Writer) std.Io.Writer.Error!void {
.result => |j| try w.print("run returning {s}", .{j}),
.trap => |c| try w.print("run trapping {s}", .{c}),
.not_instantiated => |reason| try w.print("not_instantiated ({s})", .{reason}),
.blocked => try w.writeAll("blocked"),
},
}
}
@@ -250,6 +298,11 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
var phase: ?u8 = null;
var fuel: u64 = default_fuel;
var grants: std.ArrayList(aji.interp.Grant) = .empty;
var world: aji.interp.WorldOptions = .{};
var vfs: std.ArrayList(struct { []const u8, []const u8 }) = .empty;
var want_files: std.ArrayList(struct { []const u8, []const u8 }) = .empty;
var want_stdout: ?[]const u8 = null;
var want_stderr: ?[]const u8 = null;
var spec: ?[]const u8 = null;
var lines = std.mem.splitScalar(u8, source, '\n');
@@ -290,6 +343,21 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
} else if (std.mem.eql(u8, key, "not_instantiated")) {
if (run_result != null) return error.BadExpect;
run_result = .{ .not_instantiated = value };
} else if (std.mem.eql(u8, key, "blocked")) {
if (run_result != null) return error.BadExpect;
run_result = .blocked;
} else if (std.mem.eql(u8, key, "stdin")) {
world.stdin = try unescape(arena, value);
} else if (std.mem.eql(u8, key, "seed")) {
world.seed = try std.fmt.parseInt(u64, value, 10);
} else if (std.mem.eql(u8, key, "stdout")) {
want_stdout = try unescape(arena, value);
} else if (std.mem.eql(u8, key, "stderr")) {
want_stderr = try unescape(arena, value);
} else if (std.mem.eql(u8, key, "vfs") or std.mem.eql(u8, key, "file")) {
const eq = std.mem.indexOfScalar(u8, value, '=') orelse return error.BadExpect;
const entry: struct { []const u8, []const u8 } = .{ value[0..eq], try unescape(arena, value[eq + 1 ..]) };
try (if (std.mem.eql(u8, key, "vfs")) &vfs else &want_files).append(arena, entry);
} else if (std.mem.eql(u8, key, "grant")) {
const eq = std.mem.indexOfScalar(u8, value, '=') orelse return error.BadGrant;
const rest = value[eq + 1 ..];
@@ -315,6 +383,13 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
.expect = expect orelse return error.MissingExpect,
.fuel = fuel,
.grants = grants.items,
.world = blk: {
world.files = vfs.items;
break :blk world;
},
.stdout = want_stdout,
.stderr = want_stderr,
.files = want_files.items,
.phase = phase orelse return error.MissingPhase,
.spec = spec orelse return error.MissingSpec,
};