Files
Library/docs/agent-workflow/tasks/W11-CONTRACT-R2.md

96 lines
6.6 KiB
Markdown

# Wave 11 contract amendment R2 (root resolutions of PREFLIGHT-2)
Source findings: [W11-PREFLIGHT-2-OUTPUT](W11-PREFLIGHT-2-OUTPUT.md). Base contracts:
[W11-PLAN-OUTPUT](W11-PLAN-OUTPUT.md) amended by [W11-PLAN-REV1-OUTPUT](W11-PLAN-REV1-OUTPUT.md).
These rules are MANDATORY text for the B2, B3, B4, A2, A3, A4 specs (decision D-040). Where this
file and earlier documents disagree, this file wins.
## N1 — Host destruction and shutdown budgets (B3/B4)
- `~HostCore` blocks until shutdown is complete: it loops an internal no-deadline shutdown
(equivalent to repeated `shutdown_blocking({})` ignoring the total-timeout cap) until
`complete == true`. Documented: "destruction blocks until complete".
- `poll_shutdown(Async::OperationOptions = {})` takes options; every shutdown entry point
(`shutdown_blocking`, `poll_shutdown`, `shutdown_pumping`) uses the same per-call budget
`min(options.deadline, deadline_after(clock, total_timeout))`.
- The budget test uses an already-expired caller deadline (or manual-clock deadline) with
`shutdown_blocking`/`poll_shutdown`, so expiry is observable.
- Waiting phases that return `complete=false` on call-deadline expiry: 2, 4, 5, 6, 8, 9 (P3-1).
## N2 — Embedded mode (B3/B4)
- `shutdown_blocking` returns `RuntimeErrorCode::invalid_argument` when the Host's executor has
no background workers (`!has_background_workers()`); `shutdown_pumping` in managed (owned
executor) mode returns `invalid_argument` (the reactor owns polling).
- `~HostCore` in embedded mode: if the borrowed executor is a `ManualExecutor`
(`dynamic_cast`), it loops `shutdown_pumping(manual)` until complete; otherwise, if shutdown is
not already complete, it prints a diagnostic and calls `std::terminate` (documented
programmer error: embedded Host destroyed before shutdown completed).
- The "destroyed on a Runtime worker" terminate check applies only to OWNED executors/reactor/
IO workers; never to a borrowed executor's `is_executor_thread()`.
## N3 — Who polls during shutdown (B2, A4, B3/B4)
- B2: every poll turn drains cancels, control messages and file completions BEFORE any
stop-flag early return. `begin_shutdown` sets the existing stopping flag (admission closed).
While stopping (but not yet shut down), a turn with no work waits in `epoll_wait` up to
`max_wait` (wake fd stays registered) instead of returning immediately; it must not spin.
- B2: after shutdown completes, `poll_once` returns 0 without touching any fd (F2), and
`shutdown_complete()` is true; library loops must exit on it (below).
- B2: `poll_once` checks the thread-local polling marker before `try_lock(poll_mu_)`; a
reentrant call from inside a turn returns `busy` (no recursive try_lock UB).
- B2: `advance_shutdown` releases `poll_mu_` between its ≤1 ms slices; other pollers may run
turns in between (they only drain/dispatch). If its stop token fires it returns the current
report with `complete=false` (not an error).
- B2: when `advance_shutdown`/teardown dispatches resume tickets, it does so after releasing
poll ownership (P3-9). Document: an owned executor must not be joined before the Driver is
torn down.
- A4: `Driver::run` and `pump_until` on `busy` wait up to `max_wait` (stop-aware, at most 1 ms
slices) and retry; they exit when `shutdown_complete()` is true.
- B3/B4: Runtime phase 8 first requests reactor stop and JOINS the reactor thread, then drives
`driver->shutdown(remaining)` from the shutdown thread. The reactor loop also exits on
`shutdown_complete()`; on `busy` it waits as A4's loops do.
## N4 — File descriptor state machine (B2)
- `detail::FileState` has `enum { open, closing, closed }` guarded by the pool mutex; the
handle-drop / in-flight count / `close_pending` transitions happen under that mutex, so
exactly one close is enqueued and whoever closes the fd sets `closed`.
- Driver teardown closes any still-open file fds after joining workers and marks them
`closed`. Afterwards File reads/writes/syncs return `IoErrorCode::closed` without a syscall,
`~File` does nothing, and `close_and_join` returns success immediately (idempotent).
- Files count against `IoLimits::max_descriptors`; that bounds the reserved close capacity.
The close request node is preallocated inside `FileState`, so `~File` (noexcept) never
allocates.
- `~File` wakes the poller only through a weak→strong backend lock or the worker condition
variable, never through the raw waker pointer.
## N5 — Channel timer callbacks (A2; B1 documents)
- B1 documents: `TimerHandle::cancel() == false` means the callback fired or is firing, not
that it finished.
- A2: an AsyncChannel timer callback captures shared waiter state (`shared_ptr` control block
for the waiter) or a `weak_ptr` to the channel plus a waiter id looked up under the channel
lock. It never holds a raw pointer into a coroutine frame.
## N6 — EventBus destruction (A3)
- `~EventBus` gates every admitted-but-not-started delivery on every thread before returning
(shared core `closed` flag checked under the core lock before each delivery starts).
- It waits for callbacks running on OTHER threads to finish, excluding the current thread's own
in-progress callback (thread-local dispatch depth). Residual documented: destroying a bus from
inside a callback that another thread's callback waits on can deadlock (caller error).
- A throwing `error_sink` is caught and counted (D-W11-06 condition, P3-8).
## P3 lines
- P3-2 (B3/B4): owned `BudgetResource`, executor and driver objects live until `~HostCore`;
NEW `[[nodiscard]] std::size_t Host::cpu_admitted() const noexcept` (0 when not started or
embedded) for the budget test; a `BufferLease` must not outlive its resource.
- P3-3 (B3/B4): the borrowed `SignalGate` must outlive the Host; Host tracks `gate_installed_`
so `uninstall` runs at most once (failed-start cleanup and teardown cannot double-uninstall);
the off-owner signal test restores the owner mask and drains SIGUSR2 before finishing.
- P3-4 (root): MIGRATION records "concurrent `poll_once` returns `busy`" and "`~Driver` on a
file worker or inside a poll turn terminates"; `Runtime.md` shows the `TimerQueue*` parameter
on `shutdown_pumping`.
- P3-5 (B2): the D1 seam clock is armed only after submission (`submit_io` reads the clock at
`EpollDriver.cpp:503`).
- P3-6 (A1): `seeded_engine` constructs with parentheses `G(std::random_device{}())` (sent).
- P3-7 (B3): moved-from Host reference accessors (`options`, `lifecycle_plan`,
`embedded_resources`) have precondition `valid()` (documented, asserted in debug); `HostCore`
holds no pointers into `Host` members.
- P3-8 (B3): stop-and-report if an IO edit is needed (IO files are owned elsewhere).