spec: bit-exact PRNG, canonical run record, global quiescence

Owner review of the RFC 0007 implementation:
- spec 11 §8.1: xoshiro256** with SplitMix64 seeding specified bit for
  bit, fill consumes ceil(n/8) outputs LSB first; golden vector; own
  implementation replaces std.Random (whose seeding differs)
- spec 07a: canonical run record; stdout/stderr as UTF-8 strings or
  base64 objects, files always base64 objects
- spec 11 §8: blocked only at global quiescence (no runnable unit, no
  known future event, unfinished execution)
- reserved feature resource_joins; test names are permanent and
  rule-descriptive (renamed resource_*/borrow_* tests)
- plan: remaining Phase 2 order heap/regions -> unsafe -> hosted ->
  reports -> aji.toml
This commit is contained in:
dev committed 2026-10-04 14:07:54 +03:00
1 parent d405d4ea50
commit 7a74be3cf7
18 files changed
+203 -38

No files matched your search

+41 -21
View File
@@ -830,18 +830,32 @@ pub fn writeValue(module: air.Module, v: Value, s: *std.json.Stringify) std.Io.W
}
}
/// Writes bytes as a JSON string: printable ASCII as is, every other byte
/// as `\u00XX`, so arbitrary output is valid, deterministic JSON.
fn writeBytes(w: *std.Io.Writer, bytes: []const u8) std.Io.Writer.Error!void {
try w.writeByte('"');
for (bytes) |b| switch (b) {
'"' => try w.writeAll("\\\""),
'\\' => try w.writeAll("\\\\"),
'\n' => try w.writeAll("\\n"),
0x20...0x21, 0x23...0x5b, 0x5d...0x7e => try w.writeByte(b),
else => try w.print("\\u{x:0>4}", .{b}),
};
try w.writeByte('"');
/// Byte strings in the run record (spec 07a "Run result"): a JSON string
/// when the bytes are valid UTF-8, otherwise {"encoding":"base64","data":…}.
fn writeText(s: *std.json.Stringify, bytes: []const u8) std.Io.Writer.Error!void {
if (std.unicode.utf8ValidateSlice(bytes)) return s.write(bytes);
try writeBase64(s, bytes);
}
/// Binary data: always {"encoding":"base64","data":…} (RFC 4648, padded).
fn writeBase64(s: *std.json.Stringify, bytes: []const u8) std.Io.Writer.Error!void {
try s.beginObject();
try s.objectField("encoding");
try s.write("base64");
try s.objectField("data");
var buf: [4096]u8 = undefined;
try s.beginWriteRaw();
try s.writer.writeByte('"');
var i: usize = 0;
// Encode in chunks of 3-byte groups so no padding appears mid-string.
while (i < bytes.len) {
const n = @min(bytes.len - i, 3 * 1024);
try s.writer.writeAll(std.base64.standard.Encoder.encode(&buf, bytes[i..][0..n]));
i += n;
}
try s.writer.writeByte('"');
s.endWriteRaw();
try s.endObject();
}
/// Writes the run record for `outcome` as one JSON object (no newline).
@@ -911,20 +925,14 @@ pub fn writeOutcome(module: air.Module, outcome: Outcome, out: ?Output, w: *std.
}
if (out) |o| if (module.declares("capabilities") and outcome != .not_instantiated) {
try s.objectField("stdout");
try s.beginWriteRaw();
try writeBytes(w, o.stdout);
s.endWriteRaw();
try writeText(&s, o.stdout);
try s.objectField("stderr");
try s.beginWriteRaw();
try writeBytes(w, o.stderr);
s.endWriteRaw();
try writeText(&s, o.stderr);
try s.objectField("files");
try s.beginObject();
for (o.files) |file| {
try s.objectField(file[0]);
try s.beginWriteRaw();
try writeBytes(w, file[1]);
s.endWriteRaw();
try writeBase64(&s, file[1]);
}
try s.endObject();
};
@@ -1088,3 +1096,15 @@ test "a trap at the last unit of fuel is still a trap" {
// const, const, add (traps) = the add begins with the third unit.
try expectRunFuel(comptime main1("u8", " %a = const.u8 255\n %b = const.u8 1\n %c = add.checked.u8 %a, %b\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0003\",\"kind\":\"trap.overflow\",\"function\":\"main\",\"block\":\"entry\",\"inst\":2}", 3);
}
test "canonical byte strings in the run record" {
var buf: [256]u8 = undefined;
var w: std.Io.Writer = .fixed(&buf);
var s: std.json.Stringify = .{ .writer = &w };
try s.beginArray();
try writeText(&s, "h\u{e9}\n");
try writeText(&s, "\xff");
try writeBase64(&s, "Aji");
try s.endArray();
try std.testing.expectEqualStrings("[\"h\u{e9}\\n\",{\"encoding\":\"base64\",\"data\":\"/w==\"},{\"encoding\":\"base64\",\"data\":\"QWpp\"}]", w.buffered());
}