Phase 6A: .aji layer 7 (task scopes, spawn/join, channels)

- scope s { … }: task.scope.begin, task.scope.end at every normal exit
  (scope ends generalized: borrow.end or task.scope.end, innermost first)
- task.spawn(s, f, args): call-like; named linear values moved (E7003);
  lent borrows are bound first (inline borrow E7006); scope operand E7005
- task.join/cancel/cancelled; task<T>, sender<T>, receiver<T>, scope types
- channels: make<T>, dup, send, recv, close_sender, close_receiver; send,
  recv and close_receiver are outcome matches; endpoints by name, borrowed
  for the operation and ended on every edge (written `mut tx`: E7006)
- outcome rebinding: a value coming back under the name of a variable of
  its type becomes the variable's value (pending(r))
- joins carry live variables only (pruned SSA): a linear value consumed
  before a join does not travel to it; ordered after-walk over the
  enclosing statement or tail, sibling arms not counted
- parser: a keyword after `.` is a member name (task.cancelled)
- conformance equivalence ignores schedule.choices (quantum counts AIR
  instructions)
- tests: 10 run tests with AIR equivalence (Phase 3 programs), 12 rejects
  (E4301, E4101 x2, E4002, E2001, E3101, E3002, E7006 x2, E7003, E0006,
  E7005); Quick Reference tasks/channels programs now pass
- spec 15 §4/§6/§7, grammar, quick reference, bundle notes, plan
This commit is contained in:
dev committed 2026-10-05 17:55:30 +03:00
1 parent c2c238d9b1
commit 179dee3c3a
32 files changed
+1315 -41

No files matched your search

+1 -1
View File
@@ -31,7 +31,7 @@ Every test starts with a block of `;` lines. Keys:
| `stdout` / `stderr` | no | expected captured output (same escapes) |
| `file` | no, repeatable | `<path>=<text>`: expected contents of a file written by the run |
| `fuel` | no | instruction limit for `run` tests (spec 11 §7.1); default 1 000 000. Running out is a `FAIL`, never a pass |
| `equivalent` | no, `.aji` `run` tests only | path, relative to `tests/conformance`, of a reference AIR program. Both are run with this test's world and grants; their run records must be byte-identical (for a trap, the trap code must match, since the record carries the AIR location). The AIR texts may differ |
| `equivalent` | no, `.aji` `run` tests only | path, relative to `tests/conformance`, of a reference AIR program. Both are run with this test's world and grants; their run records must be byte-identical (for a trap, the trap code must match, since the record carries the AIR location; `schedule.choices` is not compared, since the scheduler's quantum counts AIR instructions and the lowered program may have a different instruction stream). The AIR texts may differ |
| `phase` | yes | implementation phase number |
| `spec` | yes | spec section that justifies the expectation |
@@ -0,0 +1,22 @@
// The only task waits on a channel whose sender it still holds: no future event can
// wake it, so the run ends blocked (an environment outcome, not an error or a trap).
// expect: run
// blocked: no_future_event
// grant: heap=mem.alloc:64
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.blocked
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
let v = channel.recv(rx) or trap;
channel.close_sender(move tx);
channel.close_receiver(move rx) or trap;
v
}
err(e) => { trap; }
}
}
@@ -0,0 +1,27 @@
// task.cancel only requests: the child sees the flag, returns, and is still joined.
// expect: run
// schedules: 10
// result: 7
// equivalent: phase3/run/tasks_cancel_request.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.cancelrequest
fn worker() -> u8
effects []
{
while not task.cancelled() {}
7
}
fn main() -> u8
effects [task.spawn]
{
var r: u8 = 0;
scope sc {
let t = task.spawn(sc, worker);
task.cancel(t);
r = task.join(move t);
}
r
}
@@ -0,0 +1,51 @@
// Closing a receiver with buffered owned values hands it back on `pending`; a send
// after that ends `closed` and returns the value; the receiver drains everything
// before the close succeeds. No owned value is dropped.
// expect: run
// grant: heap=mem.alloc:64
// result: 13
// equivalent: phase3/run/channels_drain_linear.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.drainlinear
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<own<u64>>(4, heap) {
ok(tx, rx) => {
let a = own.new(1u64, heap) or trap;
channel.send(tx, move a) or trap;
let b = own.new(2u64, heap) or trap;
channel.send(tx, move b) or trap;
var acc: u64 = 0;
match channel.close_receiver(move rx) {
ok => { trap; }
pending(r) => {
let c = own.new(10u64, heap) or trap;
match channel.send(tx, move c) {
ok => { trap; }
closed(back) => { acc = own.take(move back); }
cancelled(back) => { acc = own.take(move back); }
}
var rr: receiver<own<u64>> = move r;
loop {
match channel.recv(rr) {
ok(o) => { acc = acc + own.take(move o); }
closed => {
match channel.close_receiver(move rr) {
ok => { break; }
pending(rr) => {}
}
}
cancelled => {}
}
}
}
}
channel.close_sender(move tx);
acc
}
err(e) => { trap; }
}
}
@@ -0,0 +1,64 @@
// Two producers share one channel through channel.dup; the receiver sees closed
// only after both senders are closed.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// quantum: 2
// result: 20
// equivalent: phase3/run/channels_mpsc_dup.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.mpscdup
fn produce(tx: sender<u64>) -> u8
effects [task.wait]
{
var i: u64 = 1;
while i != 5 {
match channel.send(tx, i) {
ok => {}
closed(w) => { break; }
cancelled(w) => { break; }
}
i = i + 1;
}
channel.close_sender(move tx);
0
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
let tx2 = channel.dup(tx);
var acc: u64 = 0;
scope sc {
let a = task.spawn(sc, produce, move tx);
let b = task.spawn(sc, produce, move tx2);
var r: receiver<u64> = move rx;
loop {
match channel.recv(r) {
ok(v) => { acc = acc + v; }
closed => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
cancelled => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
}
}
let x = task.join(move a);
let y = task.join(move b);
}
acc
}
err(e) => { trap; }
}
}
@@ -0,0 +1,36 @@
// Two tasks square the two halves of one buffer; the halves are lent until the joins.
// expect: run
// result: [1,4,9,16]
// equivalent: phase3/run/tasks_parallel_square.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.parallelsquare
fn square(s: slice_mut<u64>) -> void
effects []
{
for i in 0..s.len {
s[i] = s[i] * s[i];
}
}
fn main() -> [u64; 4]
effects [task.spawn]
{
var buf: [u64; 4] = zero;
buf[0] = 1;
buf[1] = 2;
buf[2] = 3;
buf[3] = 4;
{
mut s = buf;
let (l, r) = s.split_at(2);
scope sc {
let t1 = task.spawn(sc, square, l);
let t2 = task.spawn(sc, square, r);
task.join(move t1);
task.join(move t2);
}
}
buf
}
@@ -0,0 +1,82 @@
module demo.parallelsquare
air 0
lang 0.1
features [arrays, effects, ownership, tasks]
fn @square(%s: slice_mut<u64>) -> void
effects []
entry:
%c_1 = const.usize 0
%len_2 = slice.len %s
br @while_3(%c_1)
while_3(%i_6: usize):
%cmp_8 = lt.usize %i_6, %len_2
cbr %cmp_8, @body_9(), @after_4()
after_4:
ret
step_5(%i_7: usize):
%one_16 = const.usize 1
%i_17 = add.checked.usize %i_7, %one_16
br @while_3(%i_17)
body_9:
%e_10 = borrow.index %s, %i_6
%v_11 = load %e_10
borrow.end %e_10
%e_12 = borrow.index %s, %i_6
%v_13 = load %e_12
borrow.end %e_12
%t_14 = mul.checked.u64 %v_11, %v_13
%e_15 = borrow.index %s, %i_6
store %e_15, %t_14
borrow.end %e_15
br @step_5(%i_6)
fn @main() -> array<u64, 4>
effects [task.spawn]
entry:
%buf_1 = local.alloc array<u64, 4>
local.zero %buf_1
%c_2 = const.u64 1
%buf_3 = borrow.mut %buf_1
%c_4 = const.usize 0
%e_5 = borrow.index %buf_3, %c_4
store %e_5, %c_2
borrow.end %e_5
borrow.end %buf_3
%c_6 = const.u64 2
%buf_7 = borrow.mut %buf_1
%c_8 = const.usize 1
%e_9 = borrow.index %buf_7, %c_8
store %e_9, %c_6
borrow.end %e_9
borrow.end %buf_7
%c_10 = const.u64 3
%buf_11 = borrow.mut %buf_1
%c_12 = const.usize 2
%e_13 = borrow.index %buf_11, %c_12
store %e_13, %c_10
borrow.end %e_13
borrow.end %buf_11
%c_14 = const.u64 4
%buf_15 = borrow.mut %buf_1
%c_16 = const.usize 3
%e_17 = borrow.index %buf_15, %c_16
store %e_17, %c_14
borrow.end %e_17
borrow.end %buf_15
%buf_18 = borrow.mut %buf_1
%s_19 = slice.make %buf_18
%c_20 = const.usize 2
%l_21, %r_22 = slice.split %s_19, %c_20
%sc_23 = task.scope.begin
%t_24 = task.spawn %sc_23, @square(%l_21)
%t_25 = task.spawn %sc_23, @square(%r_22)
task.join %t_24
task.join %t_25
task.scope.end %sc_23
borrow.end %r_22
borrow.end %l_21
borrow.end %s_19
borrow.end %buf_18
%buf_26 = local.load %buf_1
ret %buf_26
@@ -0,0 +1,61 @@
// A producer task sends 0..4; the consumer receives until closed and closes the receiver,
// which is carried through the loop and rebound on `pending`.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// quantum: 2
// result: 10
// equivalent: phase3/run/channels_producer_consumer.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.producerconsumer
fn produce(tx: sender<u64>, n: u64) -> u8
effects [task.wait]
{
var i: u64 = 0;
while i != n {
match channel.send(tx, i) {
ok => {}
closed(w) => { break; }
cancelled(w) => { break; }
}
i = i + 1;
}
channel.close_sender(move tx);
0
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<u64>(2, heap) {
ok(tx, rx) => {
var acc: u64 = 0;
scope sc {
let t = task.spawn(sc, produce, move tx, 5);
var r: receiver<u64> = move rx;
loop {
match channel.recv(r) {
ok(v) => { acc = acc + v; }
closed => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
cancelled => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
}
}
let x = task.join(move t);
}
acc
}
err(e) => { trap; }
}
}
@@ -0,0 +1,80 @@
module demo.producerconsumer
air 0
lang 0.1
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
fn @produce(%tx: sender<u64>, %n: u64) -> u8
effects [task.wait]
entry:
%c_1 = const.u64 0
br @while_2(%c_1)
while_2(%i_4: u64):
%cmp_5 = ne.u64 %i_4, %n
cbr %cmp_5, @body_6(), @after_3()
after_3:
channel.close_sender %tx
%c_17 = const.u8 0
ret %c_17
body_6:
%tx_7 = borrow.mut %tx
channel.send %tx_7, %i_4, ok -> @ok_8, closed -> @closed_9, cancelled -> @cancelled_11
ok_8:
borrow.end %tx_7
br @endmatch_13(%i_4)
closed_9(%closed_10: u64):
borrow.end %tx_7
br @after_3()
cancelled_11(%cancelled_12: u64):
borrow.end %tx_7
br @after_3()
endmatch_13(%i_14: u64):
%c_15 = const.u64 1
%t_16 = add.checked.u64 %i_14, %c_15
br @while_2(%t_16)
fn @main(%heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
entry:
%c_1 = const.usize 2
channel.make u64, %c_1, %heap, ok -> @ok_2, err -> @err_5
ok_2(%ok_3: sender<u64>, %ok_4: receiver<u64>):
%c_7 = const.u64 0
%sc_8 = task.scope.begin
%c_9 = const.u64 5
%t_10 = task.spawn %sc_8, @produce(%ok_3, %c_9)
br @loop_11(%c_7, %ok_4)
err_5(%err_6: alloc_error):
trap
loop_11(%acc_13: u64, %r_14: receiver<u64>):
%r_16 = borrow.mut %r_14
channel.recv %r_16, ok -> @ok_17, closed -> @closed_19, cancelled -> @cancelled_20
after_12(%acc_15: u64):
%r_37 = task.join %t_10
task.scope.end %sc_8
br @endmatch_38(%acc_15)
ok_17(%ok_18: u64):
borrow.end %r_16
%t_21 = add.checked.u64 %acc_13, %ok_18
br @endmatch_34(%t_21, %r_14)
closed_19:
borrow.end %r_16
channel.close_receiver %r_14, ok -> @ok_22, pending -> @pending_23
cancelled_20:
borrow.end %r_16
channel.close_receiver %r_14, ok -> @ok_28, pending -> @pending_29
ok_22:
br @after_12(%acc_13)
pending_23(%pending_24: receiver<u64>):
br @endmatch_25(%acc_13, %pending_24)
endmatch_25(%acc_26: u64, %r_27: receiver<u64>):
br @endmatch_34(%acc_26, %r_27)
ok_28:
br @after_12(%acc_13)
pending_29(%pending_30: receiver<u64>):
br @endmatch_31(%acc_13, %pending_30)
endmatch_31(%acc_32: u64, %r_33: receiver<u64>):
br @endmatch_34(%acc_32, %r_33)
endmatch_34(%acc_35: u64, %r_36: receiver<u64>):
br @loop_11(%acc_35, %r_36)
endmatch_38(%match_39: u64):
ret %match_39
@@ -0,0 +1,28 @@
// The parent reads a buffer half it lent exclusively to a running task (E4101).
// expect: reject E4101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.access_lent
fn bump(s: slice_mut<u64>) -> void
effects []
{
s[0] = s[0] + 1;
}
fn main() -> u64
effects [task.spawn]
{
var buf: [u64; 4] = zero;
var seen: u64 = 0;
{
mut s = buf;
let (l, r) = s.split_at(2);
scope sc {
let a = task.spawn(sc, bump, l);
seen = l[0];
task.join(move a);
}
}
seen
}
@@ -0,0 +1,19 @@
// An endpoint operand is written by name; the operation borrows it (one canonical spelling).
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.endpoint_borrow_written
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
channel.send(mut tx, 1u64) or trap;
channel.close_sender(move tx);
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,17 @@
// Task handles are function-bound: a task<T> parameter needs the reserved feature task_transfer.
// expect: reject E0006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.handle_as_parameter
fn finish(t: task<u64>) -> u64
effects []
{
task.join(move t)
}
fn main() -> u64
effects []
{
0
}
@@ -0,0 +1,27 @@
// A region is not Share: lending a ref<region> to a task is rejected (E4002).
// expect: reject E4002
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.not_share
fn look(r: ref<region>) -> u64
effects []
{
0
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn]
{
let r = region.create(heap) or trap;
var v: u64 = 0;
{
ref arena = r;
scope sc {
let t = task.spawn(sc, look, arena);
v = task.join(move t);
}
}
region.destroy(move r);
v
}
@@ -0,0 +1,17 @@
// A receiver that is never closed: the frontend inserts no close; the verifier reports the leak.
// expect: reject E3002
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.receiver_leak
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
channel.close_sender(move tx);
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,23 @@
// channel.recv may suspend: it requires the effect task.wait (E3101).
// expect: reject E3101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.recv_without_effect
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
channel.close_sender(move tx);
match channel.recv(rx) {
ok(v) => {}
closed => {}
cancelled => {}
}
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,27 @@
// The same exclusive borrow lent to two live tasks: a possible data race (E4101).
// expect: reject E4101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.same_mut_twice
fn bump(s: slice_mut<u64>) -> void
effects []
{
s[0] = s[0] + 1;
}
fn main() -> u64
effects [task.spawn]
{
var buf: [u64; 2] = zero;
{
mut s = buf;
scope sc {
let a = task.spawn(sc, bump, s);
let b = task.spawn(sc, bump, s);
task.join(move a);
task.join(move b);
}
}
buf[0]
}
@@ -0,0 +1,20 @@
// A task not joined before its scope ends: the verifier reports E4301.
// expect: reject E4301
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.scope_unjoined
fn work() -> u64
effects []
{
1
}
fn main() -> u64
effects [task.spawn]
{
scope sc {
let t = task.spawn(sc, work);
}
0
}
@@ -0,0 +1,22 @@
// A borrow lent to a task stays lent until the join, so it is bound first, not written inline.
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.spawn_inline_borrow
fn bump(s: slice_mut<u64>) -> void
effects []
{
s[0] = s[0] + 1;
}
fn main() -> u64
effects [task.spawn]
{
var buf: [u64; 2] = zero;
scope sc {
let a = task.spawn(sc, bump, mut buf);
task.join(move a);
}
buf[0]
}
@@ -0,0 +1,28 @@
// A sender moved into a task is written `move tx`.
// expect: reject E7003
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.spawn_missing_move
fn produce(tx: sender<u64>) -> u8
effects []
{
channel.close_sender(move tx);
0
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
scope sc {
let t = task.spawn(sc, produce, tx);
task.join(move t);
}
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,19 @@
// task.spawn spawns into a scope bound by `scope s { … }`.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.spawn_outside_scope
fn work() -> u64
effects []
{
1
}
fn main() -> u64
effects [task.spawn]
{
let s = 0u64;
let t = task.spawn(s, work);
task.join(move t)
}
@@ -0,0 +1,27 @@
// channel.send consumes the value: using it afterwards is E2001 from the verifier.
// expect: reject E2001
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.use_after_send
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<own<u64>>(1, heap) {
ok(tx, rx) => {
let o = own.new(1u64, heap) or trap;
channel.send(tx, move o) or trap;
var v: u64 = 0;
{
ref q = o;
v = *q;
}
channel.close_sender(move tx);
let w = channel.recv(rx) or trap;
own.free(move w);
channel.close_receiver(move rx) or trap;
v
}
err(e) => { trap; }
}
}
@@ -0,0 +1,60 @@
// Capacity 0: every send completes only when the receiver takes the value.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// quantum: 2
// result: 6
// equivalent: phase3/run/channels_rendezvous.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.rendezvous
fn produce(tx: sender<u64>) -> u8
effects [task.wait]
{
var i: u64 = 1;
while i != 4 {
match channel.send(tx, i) {
ok => {}
closed(w) => { break; }
cancelled(w) => { break; }
}
i = i + 1;
}
channel.close_sender(move tx);
0
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<u64>(0, heap) {
ok(tx, rx) => {
var acc: u64 = 0;
scope sc {
let t = task.spawn(sc, produce, move tx);
var r: receiver<u64> = move rx;
loop {
match channel.recv(r) {
ok(v) => { acc = acc + v; }
closed => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
cancelled => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
}
}
let x = task.join(move t);
}
acc
}
err(e) => { trap; }
}
}
@@ -0,0 +1,30 @@
// The same shared borrow of Share data is lent to two tasks.
// expect: run
// schedules: 10
// result: 42
// equivalent: phase3/run/tasks_shared_read.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.sharedread
fn read(p: ref<u64>) -> u64
effects []
{
*p
}
fn main() -> u64
effects [task.spawn]
{
var x: u64 = 21;
ref r = x;
var sum: u64 = 0;
scope sc {
let t1 = task.spawn(sc, read, r);
let t2 = task.spawn(sc, read, r);
let a = task.join(move t1);
let b = task.join(move t2);
sum = a + b;
}
sum
}
@@ -0,0 +1,26 @@
// Spawning beyond the deterministic task limit is an environment outcome, not a trap.
// expect: run
// task_limit: 2
// limit_exceeded: tasks
// equivalent: phase3/run/tasks_limit.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.tasklimit
fn noop() -> void
effects []
{
}
fn main() -> void
effects [task.spawn]
{
scope sc {
let a = task.spawn(sc, noop);
let b = task.spawn(sc, noop);
let c = task.spawn(sc, noop);
task.join(move a);
task.join(move b);
task.join(move c);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
// Quick Reference §7: a scope with two tasks over disjoint halves (layer 7).
// expect: run
// result: 30
// result: 32
// phase: 6
// spec: docs/agent/quick-reference.md
module demo.qrtasks