rfc: 0011 structured tasks and the deterministic scheduler (Proposed)
Feature tasks: scope and task<T>, spawn/join/cancel/cancelled, borrows lent to tasks until join (E4302, E4101), structural Send/Share with a base table resolving Q-014 (E4001, E4002), cooperative scheduler with a seeded choice rule, quantum, global fuel, schedule exploration.
This commit is contained in:
1 parent
6eceb4405b
commit
a4ba255a1f
1 file changed
+185
@@ -0,0 +1,185 @@
|
||||
# RFC 0011: Structured Tasks and the Deterministic Scheduler
|
||||
|
||||
Status: Proposed
|
||||
Scope: AIR format 0, feature `tasks`
|
||||
Language version: 0.1
|
||||
Supersedes: —
|
||||
|
||||
Resolves: Q-014 (`Send`/`Share` for aggregates holding capabilities). Realizes spec 04 §2–§6, §15 (in part), §17 and spec 11 §5, §7.2 for tasks. Channels, mutexes, `select` (Q-013), waiting cancellation points and shields follow in RFC 0012.
|
||||
|
||||
## Motivation
|
||||
|
||||
Phase 3 is the first test of Aji's concurrency thesis (spec 14, evaluation stage B). The order of preference in spec 04 §1 starts with ownership transfer and shared immutable data; this RFC delivers exactly that layer: tasks that cannot outlive their scope, values moved into tasks, borrows lent to tasks for their whole lifetime, and a scheduler whose every interleaving is reproducible from a seed. Synchronized shared state comes after.
|
||||
|
||||
## Invariants
|
||||
|
||||
1. **Structure.** A task never outlives the scope that spawned it: a scope cannot end while a task spawned from it is unjoined, and the entry function cannot return while any scope is live. There is no detachment.
|
||||
2. **No data races on ordinary memory.** Memory reachable by two tasks at once is reachable only through shared borrows of `Share` data, or through exclusive borrows of provably disjoint places. A borrow lent to a task stays lent until the task is joined.
|
||||
3. **Send/Share soundness.** A value moved into a task is `Send`; data observed through a shared borrow from several tasks is `Share`.
|
||||
4. **Reproducibility.** For a given module, inputs, grants, seed and quantum, the schedule — and therefore the run record — is identical on every run.
|
||||
5. **Exhaustive interleaving semantics.** A program's meaning does not depend on the scheduler beyond what its synchronization allows; schedule exploration may run any interleaving the scheduler can produce.
|
||||
|
||||
## 1. Feature `tasks`
|
||||
|
||||
Depends on `ownership`.
|
||||
|
||||
### 1.1 Types
|
||||
|
||||
| Type | Meaning | Class |
|
||||
|---|---|---|
|
||||
| `scope` | a structured task scope | storage-like: only an operand of `task.*`, never passed, returned, stored or copied (`E1001`) |
|
||||
| `task<T>` | handle of a running task that returns `T` | linear; consumed by `task.join` |
|
||||
|
||||
### 1.2 Instructions
|
||||
|
||||
| Instruction | Operands → result | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|
|
||||
| `%s = task.scope.begin` | → `scope` | — | — | opens a scope |
|
||||
| `%t = task.spawn %s, @f(%a…)` | `scope`, arguments of `@f` → `task<T>` (`T` = return type of `@f`) | `task.spawn` | arguments checked like a `call`; moved arguments must be `Send` (`E4001`); borrow arguments are lent until `task.join` (§2); `effects(@f) ⊆ effects(caller)` (`E3101`); `@f` must not return a borrow (`E1001`) | creates a runnable task; a scheduling point |
|
||||
| `[%r =] task.join %t` | `task<T>` (consumed) → `T` | — | ends the loans of the task (§2) | waits until the task returned; a scheduling point |
|
||||
| `task.cancel %t` | `task<T>` (not consumed) | — | — | sets the task's cancellation flag; a scheduling point |
|
||||
| `%c = task.cancelled` | → `bool` | — | — | whether the current task's flag is set; a scheduling point |
|
||||
| `task.scope.end %s` | `scope` (consumed) | — | every task spawned from `%s` must be joined on every path (`E4301`) | closes the scope |
|
||||
|
||||
Cancellation is cooperative (spec 04 §15): a task observes it only by `task.cancelled` in this RFC, and by waiting operations in RFC 0012. Cancelling does not consume the handle; the task must still be joined. A trap in any task terminates the execution instance with that trap record (spec 01 §6); supervision policies are library code over `task.join` results.
|
||||
|
||||
### 1.3 Linearity and structure
|
||||
|
||||
- `task<T>` handles and scopes follow the linear rules of spec 06 §6 (`E3002` if never consumed). A live task handle at `task.scope.end` of its scope is `E4301` (`task.live_child_escape`), reported in preference to `E3002`.
|
||||
- A scope is ended in the function that began it; `ret` with a live scope is `E3002`.
|
||||
|
||||
## 2. Lending borrows to tasks
|
||||
|
||||
A borrow argument of `task.spawn` is **lent to the task** from the spawn until the `task.join` that consumes the handle:
|
||||
|
||||
- for the verifier it behaves as a live child borrow of the argument, owned by the task handle, with the argument's mode and path;
|
||||
- ending the argument borrow, or any ancestor of it, while the task is live is `E4302` (`task.borrow_escape`);
|
||||
- an access or a new borrow that conflicts with a loan of a live task is `E4101` (`concurrency.possible_data_race`) rather than `E2101`, since the other party runs concurrently;
|
||||
- lending the same exclusive borrow, or overlapping exclusive borrows, to two live tasks is `E4101`; lending disjoint places (different fields, the halves of a `slice.split`, different region allocations) is accepted.
|
||||
|
||||
Example: two workers square the two halves of one buffer in parallel; both loans end at the joins.
|
||||
|
||||
## 3. Send and Share (Q-014)
|
||||
|
||||
`Send` and `Share` are derived structurally (spec 02 §6), with these base cases in format 0:
|
||||
|
||||
| Type | Send | Share |
|
||||
|---|---|---|
|
||||
| integers, `bool`, built-in enums | yes | yes |
|
||||
| `cap<K>` (every kind) | yes | yes — the authority object is shared and its host side is synchronized |
|
||||
| `file<…>`, `listener`, `stream` | yes | no |
|
||||
| `own<T>`, `own_array<T>` | if `T` is | if `T` is |
|
||||
| `region` | yes | no — allocation through a shared borrow mutates the region |
|
||||
| `ref<T>` | if `T` is `Share` | if `T` is `Share` |
|
||||
| `mut<T>` | if `T` is `Send` | no |
|
||||
| `slice<T>` / `slice_mut<T>` | as `ref<array>` / `mut<array>` | as `ref` / `mut` |
|
||||
| `task<T>`, `scope` | no (`Local`) | no |
|
||||
| struct, enum, array | if every field/payload/element is | if every field/payload/element is |
|
||||
|
||||
So an aggregate holding capabilities is `Send` and `Share` exactly when its other members are (Q-014). At `task.spawn`, a moved argument that is not `Send` is `E4001`; a borrow argument `ref<T>` whose `T` is not `Share`, or `mut<T>` whose `T` is not `Send`, is `E4001` as well — handing a borrow to another task is sending it.
|
||||
|
||||
`E4002` (`concurrency.not_share`) is reported when the same place is lent through shared borrows to two live tasks and the pointee is not `Share`.
|
||||
|
||||
## 4. The deterministic scheduler
|
||||
|
||||
### 4.1 Model
|
||||
|
||||
In both profiles of the reference interpreter, tasks are cooperative execution units on one host thread. A task runs until it reaches a **scheduling point** — `task.spawn`, `task.join`, `task.cancel`, `task.cancelled`, a waiting operation (RFC 0012, I/O), or the end of its **quantum** — and then the scheduler chooses the next task to run among the runnable ones.
|
||||
|
||||
Native backends (Phase 7) may run tasks in parallel; by invariant 5 they may only produce outcomes that some interleaving of this model produces.
|
||||
|
||||
### 4.2 Choice rule (deterministic profile)
|
||||
|
||||
Runnable tasks are kept in increasing order of creation. At each scheduling point the scheduler draws `x = next()` from a **schedule generator** — the generator of spec 11 §8.1 seeded with `seed ^ 0x5343484544554c45` ("SCHEDULE"), separate from the `random.fill` stream — and resumes runnable task number `x mod n`. With `--seed` unchanged, the same choices recur.
|
||||
|
||||
### 4.3 Quantum and fuel
|
||||
|
||||
- `--quantum N` (default 1 000) bounds the instructions a task executes between scheduling points; reaching it is a scheduling point. The quantum is an execution parameter like fuel, recorded in the run record, never a semantic property.
|
||||
- Fuel counts the instructions of all tasks together (spec 11 §7.2).
|
||||
- Quiescence (spec 11 §8): if every unfinished task waits and no future event can wake one, the run ends `blocked` — for example a task joining a task that waits on it.
|
||||
|
||||
### 4.4 Run record
|
||||
|
||||
The record of a run that spawned tasks adds `"schedule":{"seed":S,"quantum":Q,"choices":C}`, where `C` is the number of scheduler choices made; two runs with equal records made the same choices.
|
||||
|
||||
### 4.5 Schedule exploration
|
||||
|
||||
The conformance runner gains `schedules: N`: the test runs with seeds `0 … N−1` and every run must satisfy the expectation. Systematic exploration (DPOR) is Phase 5 work.
|
||||
|
||||
## Valid example
|
||||
|
||||
```text
|
||||
fn @main() -> u64
|
||||
effects [task.spawn]
|
||||
entry:
|
||||
%buf = local.alloc array<u64, 4>
|
||||
... ; fill
|
||||
%m = borrow.mut %buf
|
||||
%s = slice.make %m
|
||||
%two = const.usize 2
|
||||
%l, %r = slice.split %s, %two
|
||||
%sc = task.scope.begin
|
||||
%t1 = task.spawn %sc, @square(%l) ; lends %l
|
||||
%t2 = task.spawn %sc, @square(%r) ; lends %r: disjoint, accepted
|
||||
task.join %t1
|
||||
task.join %t2
|
||||
task.scope.end %sc
|
||||
borrow.end %l
|
||||
...
|
||||
```
|
||||
|
||||
## Rejected examples
|
||||
|
||||
```text
|
||||
%t1 = task.spawn %sc, @square(%s)
|
||||
%t2 = task.spawn %sc, @square(%s) ; E4101: %s lent exclusively to a live task
|
||||
```
|
||||
|
||||
```text
|
||||
%t = task.spawn %sc, @square(%l)
|
||||
borrow.end %l ; E4302: %l is lent to a live task
|
||||
```
|
||||
|
||||
```text
|
||||
%t = task.spawn %sc, @work()
|
||||
task.scope.end %sc ; E4301: %t not joined
|
||||
```
|
||||
|
||||
```text
|
||||
%rb = borrow.ref %reg
|
||||
%t = task.spawn %sc, @alloc_in(%rb) ; E4001: ref<region> is not Send (region is not Share)
|
||||
```
|
||||
|
||||
## Diagnostics
|
||||
|
||||
Existing codes, now specified: `E4001`, `E4002`, `E4101`, `E4301`, `E4302`; `E3101` for `task.spawn` and child effects. No new codes, no new traps.
|
||||
|
||||
## Agent complexity impact
|
||||
|
||||
- New concepts: scope, task handle, lending a borrow to a task, `Send`/`Share` (structural, with a small table), quantum and schedule seed.
|
||||
- New verifier state: per task handle, its scope and its loans; loans reuse the borrow machinery as children of the lent borrows.
|
||||
- New syntax: two types, six instructions.
|
||||
- Interactions: borrows (loans), linearity (handles, scopes), effects (`task.spawn`, child effects).
|
||||
- Repairability: each concurrency error has its own code (`E4001`, `E4002`, `E4101`, `E4301`, `E4302`) and names the task and the borrow.
|
||||
- Existing primitives: none express concurrency; the features are minimal (no detachment, no thread API, no futures).
|
||||
- Agent Reference Bundle: expected growth below 10 % (one new section in spec 04 plus registry rows); measured on integration.
|
||||
|
||||
## Compatibility
|
||||
|
||||
Additive under spec 07 §8.7: feature `tasks` is reserved today.
|
||||
|
||||
## Spec integration
|
||||
|
||||
On acceptance: spec 04 §2–§6, §15, §17 (AIR realization), spec 02 §6 (`Send`/`Share` table), spec 11 §5, §7.2, §8 (scheduler, quantum, choice rule, record), spec 07a, spec 08 (stage order), `reference/air-features.md`, `docs/agent/bundle.txt`.
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
- **Lexical scope blocks in AIR**: AIR has no lexical structure; a linear scope value with an explicit end has the same guarantee and fits the existing linearity checks.
|
||||
- **Detached tasks**: forbidden by AGENTS.md; supervisors can be built from scopes.
|
||||
- **Task handles passable between tasks**: would let a task be joined elsewhere and complicates structure; handles are `Local`.
|
||||
- **Round-robin only**: explores one interleaving; the seeded choice rule explores many while staying reproducible.
|
||||
- **OS threads in the interpreter**: nondeterministic and unnecessary for semantics; native backends may parallelize.
|
||||
|
||||
## Unresolved questions
|
||||
|
||||
- Whether `@main` should implicitly run in a scope (proposed: no; `@main` is task 0 and structure is enforced by the scope rules).
|
||||
Reference in new issue
Block a user