Phase 3: structured tasks and the deterministic scheduler (RFC 0011)

- interpreter core rewritten around explicit per-task frame stacks so a
  task can be suspended at any instruction; behavior of all existing
  tests unchanged
- tasks: scope and task<T> (function-bound; task_transfer and
  task_borrow_returns reserved), spawn/join/cancel/cancelled/scope.end;
  cooperative scheduler with seeded choice rule, separate schedule seed,
  quantum = fuel unit, task limit (limit_exceeded kind tasks), global
  quiescence over waiting tasks; schedule field in the run record
- verifier: Send/Share table (E4001, E4002), loans as task-owned borrows
  (E4302 on early end, E4101 on conflicts), E4301 at scope end, task
  effects (E3101)
- CLI --quantum, --task-limit, --schedule-seed; runner schedules:,
  quantum:, task_limit:, limit_exceeded:
- fixed: schedule seed default used orelse/^ precedence wrongly
- 15 phase-3 conformance tests; 4000 mutants without a crash
- Agent Reference Bundle +10.9 % tokens (recorded)
This commit is contained in:
dev committed 2026-10-04 16:02:03 +03:00
1 parent 2289917af3
commit ab5814a7bb
31 files changed
+1252 -94

No files matched your search

+52
View File
@@ -29,6 +29,8 @@ const Seg = union(enum) {
const Mode = enum { shared, exclusive };
const Loan = struct { task: ValueId, borrow: ValueId, root: ValueId, path: []const Seg, mode: Mode };
/// Why a resource is no longer live (spec 06 §6.3).
const Dead = enum { none, closed, moved };
@@ -65,6 +67,11 @@ const Walker = struct {
live: []bool,
/// How a resource stopped being live, for diagnostics.
dead: []Dead,
/// Borrows lent to tasks (spec 04 §34). A loan is active while its task
/// handle is live, so it is part of the per-path state through `live`.
loans: std.ArrayList(Loan) = .empty,
/// Scope of each task handle value.
task_scope: []?ValueId,
block: u32 = 0,
index: u32 = 0,
span: air.Span = .{ .line = 0, .col = 0 },
@@ -127,6 +134,14 @@ const Walker = struct {
try w.report(code, message, who, .{ "conflicts_with", @intCast(cv) });
return;
}
// Borrows lent to live tasks are accessed concurrently (spec 04 §34).
for (w.loans.items) |l| {
if (!w.live[l.task] or l.root != root) continue;
if (mode == .shared and l.mode == .shared) continue;
if (!overlap(path, l.path)) continue;
try w.report("E4101", "conflicts with a borrow lent to a live task", who, .{ "task", l.task });
return;
}
}
/// Records a new owned borrow and checks it against live borrows.
@@ -262,7 +277,9 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
.info = try c.arena.alloc(?Info, f.values.len),
.live = try c.arena.alloc(bool, f.values.len),
.dead = try c.arena.alloc(Dead, f.values.len),
.task_scope = try c.arena.alloc(?ValueId, f.values.len),
};
@memset(w.task_scope, null);
@memset(w.info, null);
@memset(w.live, false);
@memset(w.dead, .none);
@@ -377,6 +394,9 @@ fn step(w: *Walker, inst: air.Inst) !void {
if (!w.live[v]) return w.report("E2106", "borrow ended twice", v, null);
// Ended even when rejected, so one mistake yields one diagnostic.
defer w.live[v] = false;
for (w.loans.items) |l| if (w.live[l.task] and (l.borrow == v or w.isAncestor(v, l.borrow))) {
return w.report("E4302", "borrow ended while it is lent to a live task", v, .{ "task", l.task });
};
for (w.live, 0..) |is_live, cv| {
if (is_live and w.info[cv] != null and w.info[cv].?.parent == v) {
return w.report("E2102", "borrow ended while a borrow derived from it is live", v, .{ "child", @intCast(cv) });
@@ -397,6 +417,38 @@ fn step(w: *Walker, inst: air.Inst) !void {
};
},
.own_free, .own_take => |v| try w.consume(v, .closed),
.task_scope_begin => w.live[inst.result.?] = true,
.task_spawn => |x| {
const t = inst.result.?;
w.task_scope[t] = x.scope;
for (x.args) |a| {
if (w.types[a].isBorrow()) {
if (!try w.usable(a)) continue;
const ai = w.info[a].?;
// The loan must not conflict with live borrows or loans.
try w.accessCode(ai.root, ai.path, ai.mode, a, a, "E4101", "lending a borrow that conflicts with a live borrow");
try w.loans.append(w.c.arena, .{ .task = t, .borrow = a, .root = ai.root, .path = ai.path, .mode = ai.mode });
} else if (w.isResource(a)) try w.consume(a, .moved);
}
w.live[t] = true;
},
.task_join => |t| {
// Joining ends the task's loans (they are active only while `t` lives).
try w.consume(t, .closed);
if (inst.result) |r| if (w.isResource(r)) {
w.live[r] = true;
};
},
.task_cancel => |t| _ = try w.resourceUsable(t),
.task_scope_end => |s| {
for (w.live, 0..) |is_live, v| {
if (is_live and w.task_scope[v] != null and w.task_scope[v].? == s) {
try w.report("E4301", "scope ends while a task spawned from it is not joined", @intCast(v), .{ "scope", s });
w.live[v] = false; // reported once, in preference to E3002
}
}
try w.consume(s, .closed);
},
.region_reset => |v| {
if (!try w.resourceUsable(v)) return;
// Reset invalidates the generation: no reference may be live.