P6A-8: Source Reference Bundle and harness profile aji_source

- source bundle: a /docs directory with QUICK.md (Quick Reference plus a
  where-to-read table), language/*.md, diagnostics/errors.md and the AIR
  bundle only under air/; built from docs/agent/source-bundle.txt
  (> output files, @manifest includes) by zig build agent-reference
- Quick Reference examples are checked: every aji block must be an
  excerpt of a conformance program in tests/conformance/source/quickref
  (zig build test fails otherwise); QR fixes found by the check
  (mut d = data, region not auto-borrowed, statement/borrow blocks that
  could not verify, io_error match)
- frontend: task.*/channel.*/mutex.* are not-yet-implemented, not E7006
- aji lower --stats: mechanical_air_eliminated_raw and _normalized (each
  byte-string materialization counted as one constant; spec 15 §11)
- harness: profile aji_source (doc_groups), per-file and per-group docs
  metrics on distinctive lines, profile columns (tool calls, lowered AIR
  raw/normalized, docs files read, IR-form and type-mismatch counts),
  dev-echo aji_source variant, scripted run and test, source token report
- plan: P6A-8 done, P6A-9 criteria; Q-021/Q-022 resolved; unsafe block
  deferred
This commit is contained in:
dev committed 2026-10-05 13:16:51 +03:00
1 parent 61be766634
commit 3e2d9b63a7
35 files changed
+1170 -106

No files matched your search

+36 -4
View File
@@ -123,6 +123,13 @@ pub const Output = struct {
/// The same AIR with `; line:col-line:col` comments before each
/// instruction (not canonical; `aji lower --annotate`).
annotated: []const u8,
/// Byte-string materializations, for the normalized size metric
/// (spec 15 §11): `constant_of[i]` is 0, or `k` when AIR line `i + 1`
/// belongs to the materialization of `constants[k - 1]`.
constant_of: []const u32,
constants: []const Constant,
pub const Constant = struct { storage: []const u8, bytes: []const u8 };
};
pub const Result = union(enum) {
@@ -142,10 +149,12 @@ pub fn lower(arena: Allocator, m: ast.Module) Allocator.Error!Result {
error.Unimplemented => return .{ .unimplemented = l.unimpl.? },
error.OutOfMemory => return error.OutOfMemory,
};
return .{ .ok = .{ .air = l.out.items, .lines = l.lines.items, .annotated = l.annotated.items } };
return .{ .ok = .{ .air = l.out.items, .lines = l.lines.items, .annotated = l.annotated.items, .constant_of = l.constant_of.items, .constants = l.constants.items } };
}
const Inst = struct { text: []const u8, span: ?Span };
/// `constant`: 0, or the 1-based index of the byte-string constant whose
/// materialization the instruction belongs to.
const Inst = struct { text: []const u8, span: ?Span, constant: u32 = 0 };
const Block = struct {
name: []const u8,
@@ -213,6 +222,10 @@ const Lowerer = struct {
out: std.ArrayList(u8) = .empty,
annotated: std.ArrayList(u8) = .empty,
lines: std.ArrayList(?Span) = .empty,
constant_of: std.ArrayList(u32) = .empty,
constants: std.ArrayList(Output.Constant) = .empty,
/// Set while a byte string is materialized: the tag for its instructions.
in_constant: u32 = 0,
problem: ?Problem = null,
unimpl: ?@FieldType(Result, "unimplemented") = null,
sigs: std.StringHashMapUnmanaged(FnSig) = .empty,
@@ -302,6 +315,11 @@ const Lowerer = struct {
// ------------------------------------------------------------ output
fn line(l: *Lowerer, text: []const u8, span: ?Span) Error!void {
try l.lineTagged(text, span, 0);
}
fn lineTagged(l: *Lowerer, text: []const u8, span: ?Span, constant: u32) Error!void {
try l.constant_of.append(l.arena, constant);
try l.out.appendSlice(l.arena, text);
try l.out.append(l.arena, '\n');
try l.lines.append(l.arena, span);
@@ -539,7 +557,7 @@ const Lowerer = struct {
}
try head.appendSlice(l.arena, ":");
try l.line(head.items, null);
for (b.insts.items) |inst| try l.line(try l.fmt(" {s}", .{inst.text}), inst.span);
for (b.insts.items) |inst| try l.lineTagged(try l.fmt(" {s}", .{inst.text}), inst.span, inst.constant);
}
}
@@ -564,7 +582,7 @@ const Lowerer = struct {
fn emit(l: *Lowerer, text: []const u8, span: Span) Error!void {
const b = &l.blocks.items[l.cur.?];
try b.insts.append(l.arena, .{ .text = text, .span = span });
try b.insts.append(l.arena, .{ .text = text, .span = span, .constant = l.in_constant });
}
/// Emits `%name = text` and returns the value.
@@ -1574,6 +1592,11 @@ const Lowerer = struct {
if (bytes.len == 0) return l.fail("E7006", span, "an empty byte string has no array type", &.{});
const ty = try l.arrayOf(.{ .int = "u8" }, bytes.len);
const st = try l.storageFor("bytes", ty, span);
// Everything from here to the final `borrow.end` is the constant's
// initialization; the `local.alloc` is real frame storage.
try l.constants.append(l.arena, .{ .storage = st, .bytes = bytes });
l.in_constant = @intCast(l.constants.items.len);
defer l.in_constant = 0;
try l.emit(try l.fmt("local.zero %{s}", .{st}), span);
const b = try l.def("w", try l.wrap(.mut, ty), try l.fmt("borrow.mut %{s}", .{st}), span);
for (bytes, 0..) |byte, i| {
@@ -2020,6 +2043,7 @@ const Lowerer = struct {
fn lowerFallible(l: *Lowerer, e: ast.Expr) Error!Fallible {
if (e.kind != .call) return l.fail("E7006", e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
if (laterLayer(e.kind.call.callee.*)) return l.notYet(e.span, "this operation (layers 7–8)");
const path = pathOf(e.kind.call.callee.*) orelse return l.fail("E7006", e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
const args = e.kind.call.args;
const want = struct {
@@ -2254,6 +2278,14 @@ fn elemOf(t: Ty) ?Ty {
};
}
/// `task.*`, `channel.*`, `mutex.*`: operations of layers 7–8.
fn laterLayer(e: ast.Expr) bool {
var buf: [64]u8 = undefined;
const text = dotted(e, &buf) orelse return false;
for ([_][]const u8{ "task.", "channel.", "mutex." }) |p| if (std.mem.startsWith(u8, text, p)) return true;
return false;
}
/// The built-in operation named by a callee such as `own.new` or
/// `cap.narrow.budget`; null for anything else.
fn pathOf(e: ast.Expr) ?[]const u8 {