Files
aji/docs/plan/phase-3-structured-concurrency.md
T
dev d2932bd75a Phase 3 done: exit review, implemented_phase = 3
P3-10 and P3-11 moved to P7-10 and P7-11 (executor model for async and
blocking host operations); semantic development paused until the E-S
smoke test is analysed
2026-10-04 17:06:45 +03:00

59 lines
6.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Phase 3 — Structured concurrency
Non-normative. Part of the [implementation plan](README.md).
**Goal:** tasks that cannot outlive their scopes, ownership transfer through bounded channels, synchronized shared state, cooperative cancellation, and a deterministic scheduler.
**Out of scope (owner decision):** async I/O and its buffer claims (`E4303` stays reserved; P7-10), `thread.block` and a blocking pool (P7-11); `RwLock`, several receivers and atomics (later RFCs). No further features are added to Phase 3.
**Exit criterion:** `parallel_map` and `channel_transfer` examples run under deterministic and randomized schedules.
**Spec inputs:** spec 04 §1–19, 11 §3, §5–6. RFC 0002 is proposed for acceptance (owner decision); acceptance integrates it into the spec.
**Blocking questions:** Q-013 deterministic `select` and seeds, Q-014 Send/Share for capability-holding aggregates. (Q-019 is resolved: fuel, schedule budget and quantum are defined in spec 11 §7.)
## Invariants (AGENTS.md change checklist applies to every task here)
- no child task outlives its scope; no silent detachment;
- values moved into migratable tasks are `Send`; concurrently read values are `Share`;
- concurrent mutable borrows are proven disjoint;
- every interpreter run is reproducible from its seed.
## Tasks
| ID | Status | Task | Depends | Output | Done when |
|---|---|---|---|---|---|
| P3-1 | done | Resolve Q-013, Q-014 (RFC 0011, RFC 0012) | — | spec 04, 11 | questions resolved |
| P3-2 | done | Contract tables: `task.*` (RFC 0011), `channel.*`, `mutex.*`, select, cancellation (RFC 0012, spec 04 §37–§41); `rwlock.*` deferred (reserved `rwlock`); `thread.*` deferred to native backends | P3-1 | spec 07 appendix | all spec 07 §4 fields including synchronization effects |
| P3-2a | done | Publish features `tasks`, `cancellation`, `channels`, `sync` | P3-2 | `reference/air-features.md`, `src/air/features.zig` | feature_not_declared / missing_dependency tests |
| P3-3 | done | Deterministic scheduler: seeds, choice points, choice log; global fuel across tasks and a separate `quantum` (spec 11 §7.2) | P3-2 | `src/interp/sched.zig` | same seed ⇒ identical trace; choice log replays a run; fuel is the sum over all tasks |
| P3-4 | done | Send/Share/Local derivation | P3-1 | `src/verifier/send_share.zig` | E4001, E4002 accept/reject |
| P3-5 | done | Task-scope verifier and scoped borrows | P3-2 | `src/verifier/tasks.zig` | E4301, E4302 accept/reject |
| P3-6 | done | Concurrent alias / data-race rejection | P3-5 | `src/verifier/borrow.zig` | E4101 accept/reject; disjoint `split_mut` halves accepted in two tasks |
| P3-7 | done | Interpreter tasks: spawn, join, cancel, cancellation points, shields, task failure | P3-3 | `src/interp/` | cancellation observed only at defined points; shield defers it |
| P3-8 | done | Bounded and rendezvous channels with ownership transfer | P3-7 | interpreter + verifier | sender cannot use moved message (E2001); capacity 0 rendezvous test |
| 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 | moved | Blocking effect and blocking pool policy: moved to P7-11 (owner decision; one problem with task-aware I/O: not blocking the executor on blocking host APIs) |
| 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 | done | Phase exit review | all | plan status | exit criterion passes |
## Conformance tests to add
Negative corpus entries owned by this phase: non-Send transfer, non-Share concurrent read, task escape. Plus E4101, E4302. Scheduler tests for every synchronization primitive (testing policy in AGENTS.md).
## Exit review
- Exit criterion met: `program_parallel_map` and `program_channel_transfer` (plus `program_tcp_echo_tasks`) run in the deterministic profile under randomized schedules: `schedules: 30` in the suite and `--stress 1000` (seeds 0…999 × quanta 1, 2, 7 and the test's own) without a failure.
- Decision records accepted and integrated: RFC 0011 (structured tasks, deterministic scheduler), RFC 0012 (waits and cancellation, channels, select, mutex); owner errata after implementation: `mutex<T>` always linear, `mutex.take` rules, E2002 after `send`.
- Features published and supported: `tasks`, `cancellation`, `channels`, `sync`. Reserved: `task_transfer`, `task_borrow_returns`, `channel_transfer`, `receiver_dup`, `guard_transfer`, `rwlock`.
- Questions resolved: Q-013 (seeded choice among ready `select` arms), Q-014 (Send/Share for capability-holding aggregates).
- Conformance: 207 tests pass (Phase 3: 19 run, 5 accept, 26 reject). Every Phase 3 code has positive (run or accept) and negative tests: E4001, E4002, E4101, E4301, E4302, E4304, E4305; `E3101` for `task.spawn`/`task.wait`; `E0006` for each reserved feature.
- Stress and fuzzing: CI runs `--stress 200`; 6 000 extra schedule runs and 4 350 + 4 000 mutants (task and coordination corpora) found no failure and no crash.
- Agent Reference Bundle: 27 347 → 35 324 tokens (cl100k) over Phase 3 (+29.2 %), recorded per RFC in `docs/agent/history.json`.
- Moved out: P3-10 (async buffer claims, `E4303`) to P7-10. Deferred: `RwLock`, several receivers, atomics (Phase 5).
- Known limitations: the hosted profile runs tasks cooperatively on one thread and a blocking host I/O call blocks every task (spec 04 §36.1), so `program_tcp_echo_tasks` runs only in the deterministic profile; no systematic exploration yet (DPOR, Phase 5).
- Moved out with it: P3-11 (`thread.block`, blocking pool) to P7-11. Phase 3 delivers safe concurrency inside the Aji runtime, independent of an asynchronous host I/O model.
- Bundle growth of +29.2 % is the first substantial growth of semantic complexity; nothing is cut before the E-S smoke test measures whether mid and small models use the 35k-token bundle effectively.