Phase 2: manifest grants (P2-9) and exit review (P2-12)

- src/manifest.zig: strict aji.toml subset ([package], [capabilities],
  [metadata]); errors with line numbers
- aji run --manifest: entry relative to the manifest, manifest grants
  replaced per parameter by --grant; usage error on manifest problems
- examples/packages/hello as a runnable package, checked in CI
- implemented_phase = 2; Phase 2 exit review; README and AGENTS updated
This commit is contained in:
dev committed 2026-10-04 15:24:04 +03:00
1 parent 0948eb7347
commit 778d563182
10 files changed
+332 -15

No files matched your search

+42 -6
View File
@@ -20,10 +20,11 @@ const usage =
\\ 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>
\\ 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;
\\ --grant binds capability parameter P of @main
\\ (and replaces the manifest's grant for P);
\\ deterministic world: --stdin TEXT, --vfs PATH=TEXT,
\\ --seed N; --profile hosted uses the real OS and
\\ writes the record only to --result-file PATH
@@ -113,7 +114,8 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
unsafe_report = true;
rest = rest[2..];
}
while (std.mem.eql(u8, cmd, "run") and rest.len >= 3) {
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")) {
@@ -133,13 +135,47 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
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, "--result-file")) {
result_file = value;
} else if (std.mem.eql(u8, flag, "--manifest")) {
manifest_path = value;
} else break;
rest = rest[2..];
}
options.grants = grants.items;
options.world.files = files.items;
if (rest.len != 1) return usageError("check and run take exactly one file");
const path = rest[0];
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;
+179
View File
@@ -0,0 +1,179 @@
//! `aji.toml` package manifest (spec 12 §1, RFC 0010).
//!
//! A strict TOML subset: comments with `#`, `[section]` headers and
//! `key = "string"` lines. Unknown sections, unknown keys, duplicate keys
//! and anything outside the subset are errors, so a typo cannot silently
//! drop or misplace authority.
const std = @import("std");
pub const Grant = struct { parameter: []const u8, kind: []const u8, scope: ?[]const u8 };
pub const Manifest = struct {
name: []const u8,
version: []const u8,
entry: ?[]const u8,
/// `[capabilities]` in file order.
grants: []const Grant,
};
pub const Error = struct {
line: u32,
message: []const u8,
};
pub const Result = union(enum) { ok: Manifest, err: Error };
fn fail(arena: std.mem.Allocator, line: u32, comptime fmt: []const u8, args: anytype) !Result {
return .{ .err = .{ .line = line, .message = try std.fmt.allocPrint(arena, fmt, args) } };
}
/// `kind[:scope]`, the same form as `--grant` after the `=`.
pub fn parseGrantValue(parameter: []const u8, value: []const u8) ?Grant {
const colon = std.mem.indexOfScalar(u8, value, ':');
const g: Grant = .{
.parameter = parameter,
.kind = if (colon) |c| value[0..c] else value,
.scope = if (colon) |c| value[c + 1 ..] else null,
};
if (g.kind.len == 0) return null;
return g;
}
fn isKey(s: []const u8) bool {
if (s.len == 0) return false;
for (s) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '_' and ch != '-') return false;
return true;
}
pub fn parse(arena: std.mem.Allocator, text: []const u8) !Result {
const Section = enum { none, package, capabilities, metadata };
var section: Section = .none;
var seen_sections: [3]bool = .{ false, false, false };
var name: ?[]const u8 = null;
var version: ?[]const u8 = null;
var entry: ?[]const u8 = null;
var grants: std.ArrayList(Grant) = .empty;
var keys: std.StringHashMapUnmanaged(void) = .empty;
var lines = std.mem.splitScalar(u8, text, '\n');
var line_no: u32 = 0;
while (lines.next()) |raw| {
line_no += 1;
const line = std.mem.trim(u8, raw, " \t\r");
if (line.len == 0 or line[0] == '#') continue;
if (line[0] == '[') {
if (line[line.len - 1] != ']') return fail(arena, line_no, "malformed section header", .{});
const sname = std.mem.trim(u8, line[1 .. line.len - 1], " ");
section = std.meta.stringToEnum(Section, sname) orelse return fail(arena, line_no, "unknown section [{s}]", .{sname});
if (section == .none) return fail(arena, line_no, "unknown section [{s}]", .{sname});
const idx = @backingInt(section) - 1;
if (seen_sections[idx]) return fail(arena, line_no, "duplicate section [{s}]", .{sname});
seen_sections[idx] = true;
continue;
}
const eq = std.mem.indexOfScalar(u8, line, '=') orelse return fail(arena, line_no, "expected key = \"value\"", .{});
const key = std.mem.trim(u8, line[0..eq], " \t");
if (!isKey(key)) return fail(arena, line_no, "malformed key", .{});
const value = (try parseString(arena, std.mem.trim(u8, line[eq + 1 ..], " \t"))) orelse
return fail(arena, line_no, "value of {s} must be a string \"...\" (escapes \\\", \\\\, \\n), optionally followed by a # comment", .{key});
if (section == .none) return fail(arena, line_no, "key {s} outside a section", .{key});
const full = try std.fmt.allocPrint(arena, "{t}.{s}", .{ section, key });
const gop = try keys.getOrPut(arena, full);
if (gop.found_existing) return fail(arena, line_no, "duplicate key {s}", .{key});
switch (section) {
.package => {
if (std.mem.eql(u8, key, "name")) {
name = value;
} else if (std.mem.eql(u8, key, "version")) {
version = value;
} else if (std.mem.eql(u8, key, "entry")) {
entry = value;
} else return fail(arena, line_no, "unknown key {s} in [package]", .{key});
},
.capabilities => try grants.append(arena, parseGrantValue(key, value) orelse
return fail(arena, line_no, "grant {s} must be \"kind[:scope]\"", .{key})),
.metadata => {},
.none => unreachable,
}
}
return .{ .ok = .{
.name = name orelse return fail(arena, line_no, "[package] name is required", .{}),
.version = version orelse return fail(arena, line_no, "[package] version is required", .{}),
.entry = entry,
.grants = grants.items,
} };
}
/// `"..."` with the escapes `\"`, `\\`, `\n`, optionally followed by a comment.
fn parseString(arena: std.mem.Allocator, s: []const u8) !?[]const u8 {
if (s.len < 2 or s[0] != '"') return null;
var out: std.ArrayList(u8) = .empty;
var i: usize = 1;
while (i < s.len) : (i += 1) {
switch (s[i]) {
'"' => {
const rest = std.mem.trim(u8, s[i + 1 ..], " \t");
if (rest.len != 0 and rest[0] != '#') return null;
return out.items;
},
'\\' => {
i += 1;
if (i >= s.len) return null;
try out.append(arena, switch (s[i]) {
'"' => '"',
'\\' => '\\',
'n' => '\n',
else => return null,
});
},
else => try out.append(arena, s[i]),
}
}
return null;
}
fn expectErr(text: []const u8, line: u32) !void {
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena_state.deinit();
const r = try parse(arena_state.allocator(), text);
try std.testing.expect(r == .err);
try std.testing.expectEqual(line, r.err.line);
}
test "manifest with grants" {
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena_state.deinit();
const r = try parse(arena_state.allocator(),
\\# a package
\\[package]
\\name = "file-copy"
\\version = "0.1.0"
\\entry = "src/main.air" # relative to the manifest
\\
\\[capabilities]
\\src = "fs.read:/var/data"
\\heap = "mem.alloc:67108864"
\\out = "io.write"
\\
\\[metadata]
\\purpose = "example"
\\
);
const m = r.ok;
try std.testing.expectEqualStrings("file-copy", m.name);
try std.testing.expectEqualStrings("src/main.air", m.entry.?);
try std.testing.expectEqual(@as(usize, 3), m.grants.len);
try std.testing.expectEqualStrings("fs.read", m.grants[0].kind);
try std.testing.expectEqualStrings("/var/data", m.grants[0].scope.?);
try std.testing.expect(m.grants[2].scope == null);
}
test "strict parsing" {
try expectErr("[package]\nname = \"a\"\nversion = \"1\"\n[capabilites]\n", 4); // typo in section
try expectErr("[package]\nname = \"a\"\nnmae = \"b\"\n", 3); // unknown key
try expectErr("[package]\nname = \"a\"\nname = \"b\"\n", 3); // duplicate key
try expectErr("[package]\nname = \"a\"\nversion = 1\n", 3); // not a string
try expectErr("[package]\nname = \"a\"\nversion = \"1\"\n[capabilities]\nout = \":x\"\n", 5); // empty kind
try expectErr("[package]\nname = \"a\"\n", 3); // missing version: reported at the end of the file
}
+2 -1
View File
@@ -8,7 +8,7 @@ pub const version = "0.0.0-dev";
/// Highest plan phase (docs/plan/README.md) whose features are implemented.
/// Conformance tests for later phases are reported as PENDING.
pub const implemented_phase: u8 = 1;
pub const implemented_phase: u8 = 2;
pub const diag = @import("diag.zig");
pub const air = @import("air.zig");
@@ -16,6 +16,7 @@ pub const verifier = @import("verifier.zig");
pub const interp = @import("interp.zig");
pub const check = @import("check.zig");
pub const report = @import("report.zig");
pub const manifest = @import("manifest.zig");
test {
@import("std").testing.refAllDecls(@This());