Phase 3: reference programs and schedule stress mode

- program_parallel_map: four exclusive loans of disjoint quarters
- program_channel_transfer: heap-owned messages moved through a bounded
  channel; the 64-byte budget fits exactly the bounded worst case
- program_tcp_echo_tasks: server task, one echo task per connection,
  two client tasks sharing address and message by shared loans
- runner --stress N: seeds 0..N-1 under quanta 1, 2, 7 and the test's
  own; failures print the replay command; CI runs --stress 200
- plan: P3-12, P3-13 done
This commit is contained in:
dev committed 2026-10-04 17:01:05 +03:00
1 parent 929132c81d
commit 9f322cfe31
7 files changed
+453 -7

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ jobs:
- run: zig fmt --check build.zig build.zig.zon src tools
- run: zig build test --summary all
- run: zig build conformance
- run: zig build conformance -- --stress 200
- run: zig build && python3 tests/hosted/smoke.py
- run: zig-out/bin/aji run --grant out=io.write --grant heap=mem.alloc:64 docs/agent/syntax-example.air
- run: zig build agent-reference
+1 -1
View File
@@ -10,7 +10,7 @@ Non-normative. How the reference implementation is tested. Policy (what must be
| Conformance | `tests/conformance/` | `zig build conformance` | spec behavior: accept/reject, later run output and traps |
| Reference programs | `tests/programs/` (from P2) | `zig build conformance` | the six programs in `plan/conformance-suite.md` |
| Litmus | `tests/litmus/` (from P5) | `zig build litmus` | memory-model outcomes allowed/forbidden |
| Schedule stress | via runner flags (from P3) | `zig build conformance -- --schedules=N` | randomized and systematic interleavings |
| Schedule stress | runner flag (P3-12) | `zig build conformance -- --stress N` | every test with `schedules:` runs schedule seeds 0…N-1 under quanta 1, 2, 7 and its own; a failure prints the replay command. Systematic exploration (DPOR) is Phase 5 |
| Native parity | (from P7) | `zig build parity` | interpreter and native backend agree on the whole corpus |
Unit, conformance and hosted smoke tests exist today; conformance covers accept, reject and run tests in the deterministic profile, and `tests/hosted/smoke.py` checks observable effects (files, streams, sockets) of hosted runs.
+2 -2
View File
@@ -35,8 +35,8 @@ Non-normative. Part of the [implementation plan](README.md).
| P3-9 | done | Mutex with linear guards and the critical-section rule (E4304); RwLock deferred | P3-7 | interpreter + verifier | guard not `Send`; unlock consumes guard; E3002 on leaked guard |
| P3-10 | moved | Async I/O buffer claims: moved to P7-10 (owner decision; needs task-aware I/O) | P3-7 | verifier + runtime | E4303 accept/reject |
| P3-11 | todo | Blocking effect and blocking pool policy | P3-7 | spec 04 §18, runtime | `thread.block` from ordinary task handled per spec |
| P3-12 | doing | Runner: `seed:`/`schedules:`/`quantum:` headers (schedule budget kept separate from fuel); stress modes (1/2/N workers, random, forced yield, slow consumer, cancellation during waits, I/O reordering) | P3-3 | `tools/conformance.zig` | stress modes selectable; failing seed printed for replay |
| P3-13 | todo | Reference programs: parallel-map, channel transfer, tcp-echo with tasks | P3-8, P3-9 | `tests/programs/` | pass under all stress modes |
| P3-12 | done | Runner: `seed:`/`schedules:`/`quantum:` headers (schedule budget kept separate from fuel); `--stress N`: seeds 0…N-1 × quanta 1 (forced yield), 2, 7 and the test's own, replay command on failure; slow consumers and cancellation during waits are covered by the run corpus; worker-count modes belong to native runtimes (P7) | P3-3 | `tools/conformance.zig` | stress mode selectable; failing seed printed for replay |
| P3-13 | done | Reference programs: parallel-map, channel transfer, tcp-echo with tasks | P3-8, P3-9 | `tests/conformance/phase3/run/program_*.air` | pass under `--stress` |
| P3-14 | todo | Phase exit review | all | plan status | exit criterion passes |
## Conformance tests to add
@@ -0,0 +1,81 @@
; Phase 3 exit program: ownership transfer through a bounded channel. The
; producer allocates each message on the heap and moves the owner into the
; channel; the consumer takes it. The budget (64 bytes) only fits the channel
; (2 × 8) plus three live messages (3 × 16): bounded capacity bounds memory.
; Result: 10 + 20 + 30 = 60.
; expect: run
; grant: heap=mem.alloc:64
; schedules: 30
; quantum: 2
; result: 60
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.program_channel_transfer
air 0
lang 0.1
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
type @Message = struct { id: u64, size: u64 }
fn @main(%heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
entry:
%cap = const.usize 2
channel.make own<@Message>, %cap, %heap, ok -> @made, err -> @oom
made(%tx: sender<own<@Message>>, %rx: receiver<own<@Message>>):
%sc = task.scope.begin
%p = task.spawn %sc, @producer(%tx, %heap)
%z = const.u64 0
br @recv_loop(%rx, %z)
recv_loop(%r: receiver<own<@Message>>, %acc: u64):
%rb = borrow.mut %r
channel.recv %rb, ok -> @got, closed -> @drained, cancelled -> @drained
got(%o: own<@Message>):
borrow.end %rb
%msg = own.take %o
%id = struct.get @Message.id, %msg
%acc2 = add.checked.u64 %acc, %id
br @recv_loop(%r, %acc2)
drained:
borrow.end %rb
channel.close_receiver %r, ok -> @closed, pending -> @pending
pending(%r2: receiver<own<@Message>>):
br @recv_loop(%r2, %acc)
closed:
%x = task.join %p
task.scope.end %sc
ret %acc
oom(%e: alloc_error):
trap
fn @producer(%tx: sender<own<@Message>>, %heap: cap<mem.alloc>) -> u8
effects [mem.alloc, task.wait]
entry:
%txb = borrow.mut %tx
%ten = const.u64 10
br @loop(%ten)
loop(%id: u64):
%end = const.u64 40
%done = eq.u64 %id, %end
cbr %done, @finish(), @build(%id)
build(%i: u64):
%size = const.u64 16
%msg = struct.make @Message(%i, %size)
own.new %msg, %heap, ok -> @owned, err -> @oom
owned(%o: own<@Message>):
channel.send %txb, %o, ok -> @sent, closed -> @refused, cancelled -> @refused
sent:
; %o is gone: the receiver owns the message now
%step = const.u64 10
%next = add.checked.u64 %i, %step
br @loop(%next)
refused(%back: own<@Message>):
own.free %back
br @finish()
finish:
borrow.end %txb
channel.close_sender %tx
%r = const.u8 0
ret %r
oom(%e: alloc_error):
trap
@@ -0,0 +1,99 @@
; Phase 3 exit program: parallel map. One buffer is split into four disjoint
; quarters; four tasks square their quarter in place through exclusive loans;
; the parent sums the result after joining. Result: 1² + … + 8² = 204.
; expect: run
; schedules: 30
; quantum: 3
; result: 204
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.program_parallel_map
air 0
lang 0.1
features [arrays, effects, ownership, tasks]
fn @main() -> u64
effects [task.spawn]
entry:
%buf = local.alloc array<u64, 8>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%i0 = const.usize 0
%v0 = const.u64 1
br @fill(%i0, %v0)
fill(%i: usize, %v: u64):
%n = slice.len %s
%full = eq.usize %i, %n
cbr %full, @split(), @store(%i, %v)
store(%j: usize, %w: u64):
%e = borrow.index %s, %j
store %e, %w
borrow.end %e
%one = const.usize 1
%j2 = add.checked.usize %j, %one
%uno = const.u64 1
%w2 = add.checked.u64 %w, %uno
br @fill(%j2, %w2)
split:
%four = const.usize 4
%l, %r = slice.split %s, %four
%two = const.usize 2
%a, %b = slice.split %l, %two
%c, %d = slice.split %r, %two
%sc = task.scope.begin
%ta = task.spawn %sc, @square(%a)
%tb = task.spawn %sc, @square(%b)
%tc = task.spawn %sc, @square(%c)
%td = task.spawn %sc, @square(%d)
%xa = task.join %ta
%xb = task.join %tb
%xc = task.join %tc
%xd = task.join %td
task.scope.end %sc
borrow.end %a
borrow.end %b
borrow.end %c
borrow.end %d
borrow.end %l
borrow.end %r
%z = const.u64 0
br @sum(%i0, %z)
sum(%k: usize, %acc: u64):
%len = slice.len %s
%done = eq.usize %k, %len
cbr %done, @finish(), @add(%k, %acc)
add(%k2: usize, %acc2: u64):
%e2 = borrow.index %s, %k2
%x = load %e2
borrow.end %e2
%acc3 = add.checked.u64 %acc2, %x
%step = const.usize 1
%k3 = add.checked.usize %k2, %step
br @sum(%k3, %acc3)
finish:
borrow.end %s
borrow.end %m
ret %acc
fn @square(%q: slice_mut<u64>) -> u8
effects []
entry:
%z = const.usize 0
br @loop(%z)
loop(%i: usize):
%n = slice.len %q
%done = eq.usize %i, %n
cbr %done, @fin(), @body(%i)
body(%j: usize):
%e = borrow.index %q, %j
%v = load %e
%sq = mul.checked.u64 %v, %v
store %e, %sq
borrow.end %e
%one = const.usize 1
%k = add.checked.usize %j, %one
br @loop(%k)
fin:
%r = const.u8 0
ret %r
@@ -0,0 +1,233 @@
; Phase 3 exit program: TCP echo with tasks over the deterministic loopback.
; A server task accepts two connections and spawns one echo task per
; connection; two client tasks share the address and the message through
; shared loans. Result: the bytes echoed to both clients, 2 × (112+105+110+103).
; expect: run
; grant: listen=net.listen
; grant: connect=net.connect
; schedules: 30
; quantum: 5
; result: 860
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.program_tcp_echo_tasks
air 0
lang 0.1
features [arrays, capabilities, effects, ownership, resources, tasks]
fn @main(%listen: cap<net.listen>, %connect: cap<net.connect>) -> u64
effects [net.listen, net.connect, net.read, net.write, task.spawn]
entry:
%addr = local.alloc array<u8, 11>
local.zero %addr
%addr_m = borrow.mut %addr
%addr_i0 = const.usize 0
%addr_c0 = const.u8 49
%addr_e0 = borrow.index %addr_m, %addr_i0
store %addr_e0, %addr_c0
borrow.end %addr_e0
%addr_i1 = const.usize 1
%addr_c1 = const.u8 50
%addr_e1 = borrow.index %addr_m, %addr_i1
store %addr_e1, %addr_c1
borrow.end %addr_e1
%addr_i2 = const.usize 2
%addr_c2 = const.u8 55
%addr_e2 = borrow.index %addr_m, %addr_i2
store %addr_e2, %addr_c2
borrow.end %addr_e2
%addr_i3 = const.usize 3
%addr_c3 = const.u8 46
%addr_e3 = borrow.index %addr_m, %addr_i3
store %addr_e3, %addr_c3
borrow.end %addr_e3
%addr_i4 = const.usize 4
%addr_c4 = const.u8 48
%addr_e4 = borrow.index %addr_m, %addr_i4
store %addr_e4, %addr_c4
borrow.end %addr_e4
%addr_i5 = const.usize 5
%addr_c5 = const.u8 46
%addr_e5 = borrow.index %addr_m, %addr_i5
store %addr_e5, %addr_c5
borrow.end %addr_e5
%addr_i6 = const.usize 6
%addr_c6 = const.u8 48
%addr_e6 = borrow.index %addr_m, %addr_i6
store %addr_e6, %addr_c6
borrow.end %addr_e6
%addr_i7 = const.usize 7
%addr_c7 = const.u8 46
%addr_e7 = borrow.index %addr_m, %addr_i7
store %addr_e7, %addr_c7
borrow.end %addr_e7
%addr_i8 = const.usize 8
%addr_c8 = const.u8 49
%addr_e8 = borrow.index %addr_m, %addr_i8
store %addr_e8, %addr_c8
borrow.end %addr_e8
%addr_i9 = const.usize 9
%addr_c9 = const.u8 58
%addr_e9 = borrow.index %addr_m, %addr_i9
store %addr_e9, %addr_c9
borrow.end %addr_e9
%addr_i10 = const.usize 10
%addr_c10 = const.u8 55
%addr_e10 = borrow.index %addr_m, %addr_i10
store %addr_e10, %addr_c10
borrow.end %addr_e10
borrow.end %addr_m
%addr_r = borrow.ref %addr
%addr_s = slice.make %addr_r
%ping = local.alloc array<u8, 4>
local.zero %ping
%ping_m = borrow.mut %ping
%ping_i0 = const.usize 0
%ping_c0 = const.u8 112
%ping_e0 = borrow.index %ping_m, %ping_i0
store %ping_e0, %ping_c0
borrow.end %ping_e0
%ping_i1 = const.usize 1
%ping_c1 = const.u8 105
%ping_e1 = borrow.index %ping_m, %ping_i1
store %ping_e1, %ping_c1
borrow.end %ping_e1
%ping_i2 = const.usize 2
%ping_c2 = const.u8 110
%ping_e2 = borrow.index %ping_m, %ping_i2
store %ping_e2, %ping_c2
borrow.end %ping_e2
%ping_i3 = const.usize 3
%ping_c3 = const.u8 103
%ping_e3 = borrow.index %ping_m, %ping_i3
store %ping_e3, %ping_c3
borrow.end %ping_e3
borrow.end %ping_m
%ping_r = borrow.ref %ping
%ping_s = slice.make %ping_r
intrinsic net.listen(%listen, %addr_s), ok -> @listening, err -> @f0
listening(%l: listener):
%sc = task.scope.begin
%srv = task.spawn %sc, @serve(%l)
%c1 = task.spawn %sc, @client(%connect, %addr_s, %ping_s)
%c2 = task.spawn %sc, @client(%connect, %addr_s, %ping_s)
%x = task.join %srv
%a = task.join %c1
%b = task.join %c2
task.scope.end %sc
borrow.end %ping_s
borrow.end %ping_r
borrow.end %addr_s
borrow.end %addr_r
%sum = add.checked.u64 %a, %b
ret %sum
f0(%fe0: io_error):
trap
; Accepts two connections; each is moved into its own echo task.
fn @serve(%l: listener) -> u8
effects [net.listen, net.read, net.write, task.spawn]
entry:
%sc = task.scope.begin
%lm = borrow.mut %l
intrinsic net.accept(%lm), ok -> @first, err -> @g0
first(%s1: stream):
%t1 = task.spawn %sc, @echo(%s1)
intrinsic net.accept(%lm), ok -> @second, err -> @g1
second(%s2: stream):
%t2 = task.spawn %sc, @echo(%s2)
borrow.end %lm
%x1 = task.join %t1
%x2 = task.join %t2
task.scope.end %sc
intrinsic net.close_listener(%l), ok -> @done, err -> @g2
done:
%r = const.u8 0
ret %r
g0(%ge0: io_error):
trap
g1(%ge1: io_error):
trap
g2(%ge2: io_error):
trap
fn @echo(%s: stream) -> u8
effects [net.read, net.write]
entry:
%buf = local.alloc array<u8, 4>
local.zero %buf
%sm = borrow.mut %s
%bm = borrow.mut %buf
%bs = slice.make %bm
intrinsic net.recv(%sm, %bs), ok -> @received, err -> @h0
received(%n: usize):
%z = const.usize 0
%got = slice.sub %bs, %z, %n
%gr = borrow.ref %got
intrinsic net.send_all(%sm, %gr), ok -> @echoed, err -> @h1
echoed:
borrow.end %gr
borrow.end %got
borrow.end %bs
borrow.end %bm
borrow.end %sm
intrinsic net.close_stream(%s), ok -> @done, err -> @h2
done:
%r = const.u8 0
ret %r
h0(%he0: io_error):
trap
h1(%he1: io_error):
trap
h2(%he2: io_error):
trap
; Connects, sends the message, and returns the sum of the bytes echoed back.
fn @client(%connect: cap<net.connect>, %addr: slice<u8>, %msg: slice<u8>) -> u64
effects [net.connect, net.read, net.write]
entry:
%back = local.alloc array<u8, 4>
local.zero %back
intrinsic net.connect(%connect, %addr), ok -> @connected, err -> @k0
connected(%c: stream):
%cm = borrow.mut %c
intrinsic net.send_all(%cm, %msg), ok -> @sent, err -> @k1
sent:
%km = borrow.mut %back
%ks = slice.make %km
intrinsic net.recv(%cm, %ks), ok -> @answer, err -> @k2
answer(%n: usize):
borrow.end %ks
borrow.end %km
borrow.end %cm
intrinsic net.close_stream(%c), ok -> @closed, err -> @k3
closed:
%br = borrow.ref %back
%bs = slice.make %br
%z = const.usize 0
%acc0 = const.u64 0
br @sum(%z, %acc0)
sum(%i: usize, %acc: u64):
%done = eq.usize %i, %n
cbr %done, @fin(), @add(%i, %acc)
add(%j: usize, %acc2: u64):
%e = borrow.index %bs, %j
%b = load %e
borrow.end %e
%w = cast.widen.u64 %b
%acc3 = add.checked.u64 %acc2, %w
%one = const.usize 1
%j2 = add.checked.usize %j, %one
br @sum(%j2, %acc3)
fin:
borrow.end %bs
borrow.end %br
ret %acc
k0(%ke0: io_error):
trap
k1(%ke1: io_error):
trap
k2(%ke2: io_error):
trap
k3(%ke3: io_error):
trap
+36 -4
View File
@@ -1,6 +1,10 @@
//! Conformance suite runner. Format: tests/conformance/README.md.
//!
//! usage: aji-conformance <dir> [--json] [--filter <substring>]
//! usage: aji-conformance <dir> [--json] [--filter <substring>] [--stress <seeds>]
//!
//! `--stress N` (P3-12): every test with a `schedules:` header runs schedule
//! seeds 0 … N-1 under each quantum of `stress_quanta` and its own quantum,
//! instead of its `schedules:` count. A failure prints the replay command.
//!
//! Exit status is 1 if any test FAILs or has a malformed header. PENDING
//! (pipeline stage not implemented yet) does not fail the run.
@@ -58,7 +62,7 @@ pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(arena);
if (args.len < 2) {
std.debug.print("usage: aji-conformance <dir> [--json] [--filter <substring>]\n", .{});
std.debug.print("usage: aji-conformance <dir> [--json] [--filter <substring>] [--stress <seeds>]\n", .{});
std.process.exit(2);
}
const root = args[1];
@@ -71,6 +75,12 @@ pub fn main(init: std.process.Init) !void {
} else if (std.mem.eql(u8, args[i], "--filter") and i + 1 < args.len) {
i += 1;
filter = args[i];
} else if (std.mem.eql(u8, args[i], "--stress") and i + 1 < args.len) {
i += 1;
stress_seeds = std.fmt.parseInt(u64, args[i], 10) catch {
std.debug.print("aji-conformance: --stress needs a seed count\n", .{});
std.process.exit(2);
};
} else {
std.debug.print("aji-conformance: unknown argument {s}\n", .{args[i]});
std.process.exit(2);
@@ -119,6 +129,13 @@ pub fn main(init: std.process.Init) !void {
if (counts.get(.fail) != 0) std.process.exit(1);
}
/// Seeds per quantum under `--stress`; null runs each test's `schedules:`.
var stress_seeds: ?u64 = null;
/// Quanta of the stress mode: forced yield after every instruction, short
/// and odd slices (spec 04 §36.3); the test's own quantum is added.
const stress_quanta = [_]u64{ 1, 2, 7 };
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
return std.mem.lessThan(u8, a, b);
}
@@ -165,7 +182,7 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
switch (header.expect) {
.accept => if (report.status == .accepted) return .{ .outcome = .pass },
.reject => |want| if (report.status == .rejected and eqlCodes(want, got.items)) return .{ .outcome = .pass },
.run => |want| if (report.status == .accepted) return runModule(arena, report.module.?, want, header),
.run => |want| if (report.status == .accepted) return runModule(arena, path, report.module.?, want, header),
}
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "expected {f}, got {t} {s}", .{
fmtExpect(header.expect), report.status, try std.mem.join(arena, " ", got.items),
@@ -176,7 +193,22 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
/// before comparing against the expectation.
/// Runs the module once per schedule seed; every seed must satisfy the
/// expectation (spec 04 §36.5). The program seed stays unchanged.
fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run, header: Header) !Result {
fn runModule(arena: std.mem.Allocator, path: []const u8, module: aji.air.Module, want: Expect.Run, header: Header) !Result {
if (stress_seeds) |n| if (header.schedules > 1) {
for (stress_quanta ++ [_]u64{header.quantum}) |q| {
var h = header;
h.quantum = q;
var seed: u64 = 0;
while (seed < n) : (seed += 1) {
const r = try runSeed(arena, module, want, h, seed);
if (r.outcome == .pass) continue;
var grants: std.Io.Writer.Allocating = .init(arena);
for (header.grants) |g| try grants.writer.print(" --grant {s}={s}{s}{s}", .{ g.parameter, g.kind, if (g.scope != null) ":" else "", g.scope orelse "" });
return .{ .outcome = r.outcome, .detail = try std.fmt.allocPrint(arena, "quantum {d}, schedule seed {d}: {s}; replay: aji run --quantum {d} --schedule-seed {d}{s} {s}", .{ q, seed, r.detail, q, seed, grants.written(), path }) };
}
}
return .{ .outcome = .pass };
};
var seed: u64 = 0;
while (seed < header.schedules) : (seed += 1) {
const r = try runSeed(arena, module, want, header, if (header.schedules > 1) seed else null);