rfc: 0011 revision 2 per owner review

function-bound task handles and scopes (task_transfer reserved); loan
state machine; task results Send and borrow-free (task_borrow_returns
reserved); spawn argument rules (E4001/E4002); cancel is a request only;
child trap terminates the execution; deterministic task limit
(limit_exceeded kind tasks); separate schedule seed; one instruction =
one fuel unit = one quantum step; narrowed native equivalence
This commit is contained in:
dev committed 2026-10-04 15:53:11 +03:00
1 parent a4ba255a1f
commit 1eb446eedb
1 file changed
+56 -16
+56 -16
View File
@@ -1,6 +1,6 @@
# RFC 0011: Structured Tasks and the Deterministic Scheduler
Status: Proposed
Status: Proposed (revision 2: owner review items 1–10 applied)
Scope: AIR format 0, feature `tasks`
Language version: 0.1
Supersedes: —
@@ -18,6 +18,8 @@ Phase 3 is the first test of Aji's concurrency thesis (spec 14, evaluation stage
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.
6. **Local verification.** Task handles, scopes and loans never leave the function that created them, so all concurrency checks stay intraprocedural.
7. **Bounded concurrency.** The deterministic profile bounds the number of live tasks independently of host memory.
## 1. Feature `tasks`
@@ -27,21 +29,27 @@ Depends on `ownership`.
| 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` |
| `scope` | a structured task scope | linear, function-bound |
| `task<T>` | handle of a task that returns `T` | linear, function-bound; consumed by `task.join` |
**Function-bound.** A `scope` or `task<T>` may be passed between blocks of the function that created it (as a block argument) and used by `task.*`. It may not be a function parameter, a return type, an aggregate member, or stored in heap or region memory; those uses belong to the reserved feature `task_transfer` (`E0006` reason `feature_not_declared`). Keeping handles in their function keeps loans intraprocedural (invariant 6).
**Result type.** `task<T>` requires `T: Send`, and `T` must not transitively contain a borrow, a `scope` or a `task<…>`. A spawned function returning a borrow (for example one derived from a lent argument) belongs to the reserved feature `task_borrow_returns` (`E0006`): after `join`, whether the loan ends or continues as the returned borrow is deliberately left undefined in format 0.
### 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 |
| `%t = task.spawn %s, @f(%a…)` | `scope`, arguments of `@f` → `task<T>` (`T` = return type of `@f`) | `task.spawn` | arguments checked like a `call`; argument rules of §3; borrow arguments are lent until `task.join` (§2); `effects(@f) ⊆ effects(caller)` (`E3101`); result rules of §1.1 | takes a task slot (§4.3), 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, frees its slot; a scheduling point |
| `task.cancel %t` | `task<T>` (not consumed) | — | — | sets the task's cancellation-request 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.
**Cancellation is a request only** (spec 04 §15). `task.cancel` does not consume the handle, does not stop the task, does not release its resources and does not return its loans. Only `task.join` consumes the handle and returns the loans, and `task.scope.end` requires every child to be *joined*, not merely cancelled. `task.cancelled` reads the current task's flag; RFC 0012 adds cancellation points to waiting operations.
**A trap in any task terminates the whole execution** with that trap record (spec 01 §6). There is no recoverable task failure in format 0: `task.join` never reports a trapped child, so no unwinding or cleanup semantics for the child's resources are needed. Expected failure is a value the task returns.
### 1.3 Linearity and structure
@@ -50,7 +58,15 @@ Cancellation is cooperative (spec 04 §15): a task observes it only by `task.can
## 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:
A borrow argument of `task.spawn` is **lent to the task** from the spawn until the `task.join` that consumes the handle. Its state machine is:
```text
LIVE --spawn--> LOANED(task) --join--> LIVE --borrow.end--> ENDED
```
The state is static: a borrow stays `LOANED` until the `join`, even if the child has already finished at runtime, so the model is independent of the scheduler.
While `LOANED`:
- 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`);
@@ -76,9 +92,17 @@ Example: two workers square the two halves of one buffer in parallel; both loans
| `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.
So an aggregate holding capabilities is `Send` and `Share` exactly when its other members are (Q-014).
`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`.
Argument rules at `task.spawn`:
```text
moved T → T: Send otherwise E4001 (concurrency.not_send)
ref<T> → T: Share otherwise E4002 (concurrency.not_share)
mut<T> → T: Send otherwise E4001
```
(`slice<T>` and `slice_mut<T>` follow `ref` and `mut`.) Handing a borrow to another task is sending or sharing what it points to.
## 4. The deterministic scheduler
@@ -86,25 +110,39 @@ So an aggregate holding capabilities is `Send` and `Share` exactly when its othe
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.
Native backends (Phase 7) may run tasks in parallel; by invariant 5 they may only produce outcomes that some interleaving of this model produces. This guarantee applies to safe programs that use neither the future weak-memory atomic facilities nor unsafe code; atomic memory ordering is governed by the Aji memory model (spec 04 §20–§31).
The `hosted` profile uses the same cooperative scheduler on one host thread; in format 0 a blocking host I/O call blocks every task until it returns (task-aware I/O is later work).
### 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.
Runnable tasks are kept in increasing order of creation. At each scheduling point the scheduler draws `x = next()` from the **schedule generator** and resumes runnable task number `x mod n` (the current task counts if it is still runnable).
The schedule generator is the generator of spec 11 §8.1, seeded with the **schedule seed**. It is a stream independent of the program's `random.fill` stream (seeded with `--seed`):
```text
--seed N program randomness (random.fill)
--schedule-seed M scheduler choices; default: seed XOR 0x5343484544554c45 ("SCHEDULE")
```
Varying the schedule seed while keeping `--seed` explores interleavings without changing the program's inputs.
### 4.3 Quantum and fuel
- One executed AIR instruction is one fuel unit and one quantum step; there is a single definition of "instruction executed".
- `--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.
- Order at every instruction: the instruction begins → its fuel unit is consumed → it completes or blocks → its effects become visible → if it is a scheduling point (or ends the quantum), the scheduler decides. A blocked instruction consumes its fuel unit once, when it first begins; when its task resumes, it completes without consuming fuel again. While a task waits, its quantum is irrelevant.
- Fuel counts the instructions of all tasks together (spec 11 §7.2).
- **Task limit.** The deterministic profile has `max_task_slots`, default 1 024, set with `--task-limit N`. A slot is taken by a successful `task.spawn` and freed by its `task.join`; a finished but unjoined task still holds its slot. A spawn that would exceed the limit ends the run with the environment outcome `{"status":"limit_exceeded","kind":"tasks","limit":N}` (exit code 5, like fuel); it is not a trap.
- 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.
The record of a run that spawned tasks adds `"schedule":{"seed":S,"quantum":Q,"choices":C}`, where `S` is the schedule seed and `C` 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.
The conformance runner gains `schedules: N`: the test runs with schedule seeds `0 … N−1` and an unchanged program seed, and every run must satisfy the expectation. Systematic exploration (DPOR) is Phase 5 work.
## Valid example
@@ -152,7 +190,7 @@ entry:
## Diagnostics
Existing codes, now specified: `E4001`, `E4002`, `E4101`, `E4301`, `E4302`; `E3101` for `task.spawn` and child effects. No new codes, no new traps.
Existing codes, now specified: `E4001`, `E4002`, `E4101`, `E4301`, `E4302`; `E3101` for `task.spawn` and child effects; `E0006` for the reserved features `task_transfer` and `task_borrow_returns`; `E1001` for a task result type containing a borrow. No new codes, no new traps. New environment outcome kind: `limit_exceeded` with `kind: "tasks"`.
## Agent complexity impact
@@ -179,7 +217,9 @@ On acceptance: spec 04 §2–§6, §15, §17 (AIR realization), spec 02 §6 (`Se
- **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.
- **Recoverable child traps** (`task.join → TaskError::Trapped`): would need unwinding and cleanup of the child's resources. Rejected for format 0.
- **One seed for program randomness and scheduling**: exploring schedules would also change program inputs, hiding whether a failure came from the interleaving. Rejected for separate streams.
## Unresolved questions
- Whether `@main` should implicitly run in a scope (proposed: no; `@main` is task 0 and structure is enforced by the scope rules).
None. Decided: `@main` is task 0 and has no implicit scope; structured concurrency starts only with an explicit `task.scope.begin`.