Phase 2: hosted profile (P2-8a)

- src/interp/host.zig: real console, files (scope checked before any
  host call, positional reads/writes, append from file size) and TCP
  through std.Io; monotonic clock and host CSPRNG; host errors mapped
  to the closed io_error set; open handles released at the end of the
  instance
- interpreter dispatches intrinsics to the host or the deterministic
  world; never reports blocked in the hosted profile
- CLI: --profile hosted; the program owns stdout/stderr, the run record
  goes to --result-file, otherwise a human summary on stderr
- spec 11 §8.0: host error mapping table
- tests/hosted/smoke.py (file copy, scope denial, stdin->stdout, TCP
  echo on a free port) runs in CI
- works around Zig 0.17 net.Stream.read not compiling by using
  readWithControl
This commit is contained in:
dev committed 2026-10-04 14:46:40 +03:00
1 parent 008654999e
commit 36e91be84c
10 files changed
+530 -51

No files matched your search

+19 -10
View File
@@ -25,8 +25,8 @@ const usage =
\\ --fuel stops after N executed instructions;
\\ --grant binds capability parameter P of @main;
\\ deterministic world: --stdin TEXT, --vfs PATH=TEXT,
\\ --seed N; --result-file PATH also writes the record;
\\ --profile hosted is not implemented yet
\\ --seed N; --profile hosted uses the real OS and
\\ writes the record only to --result-file PATH
\\ codes list diagnostic codes as JSON Lines
\\ features print supported AIR format, language version and features
\\ explain <code> show one diagnostic code
@@ -104,10 +104,8 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
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")) {
std.debug.print("aji: not implemented yet: hosted profile\n", .{});
return .unimplemented;
}
if (!std.mem.eql(u8, value, "deterministic")) return usageError("--profile takes deterministic or 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")) {
@@ -137,17 +135,28 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
if (report.status == .accepted and std.mem.eql(u8, cmd, "run")) {
const module = report.module.?;
var output: aji.interp.Output = undefined;
options.output = &output;
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,
};
try aji.interp.writeOutcome(module, outcome, output, out);
try out.writeByte('\n');
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, output, &rec.writer);
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 });