Skip to content

Ariadne async execution semantics: app-wide scheduler wiring + owner scopes - #437

Merged
transfix merged 8 commits into
masterfrom
design/ariadne-async-integration
Sep 28, 2026
Merged

transfix merged 8 commits into
masterfrom
design/ariadne-async-integration

Conversation

@transfix

Copy link
Copy Markdown
Owner

Ariadne async execution semantics — the runtime integration

Builds on #435 (the state_exec scheduler foundation, now merged). This PR wires the
single app-wide scheduler the §4.7 decision calls for and the isolation primitive
that makes sharing it safe, then (incoming commits) rewires Ariadne's action lane onto
it so (await …)/(sleep …)/(msg-recv …) suspend across frames instead of blocking
the UI thread.

Landed so far

  • cvc::app::exec_scheduler() — one lazily-built, per-app async_scheduler (the
    single cooperative timeline every .ari document the app hosts shares), wired to the
    app's state root. Per-app-instance, like computePool()/world_clock().
  • Owner-scope + kill_owner — execute_options/process carry an owner tag; both
    schedulers copy it; async_scheduler::kill_owner(owner) reaps a document's whole
    process group (parked processes included) so nothing lingers after a Runtime tears down.

Incoming (the ariadne.cpp rewiring)

  • Runtime borrows app.exec_scheduler() (owner = the document) instead of a throwaway
    local scheduler; action on: programs submit-and-return with a persistent
    ActionContext (intrinsics_context+env kept alive until the process terminates via
    on_complete), chrooted to the doc prefix.
  • Per-frame pump at the app/frame level (bounded step budget; parked processes persist).
  • Runtime teardown → kill_owner.
  • (await expr) on the scheduled path; on:tick/on:key residents (§7.1).

Testing

520 state_exec + ariadne tests pass locally (incl. AppExecScheduler app-wide-singleton
identity + KillOwnerReapsProcessGroup). The read lane stays a separate synchronous
stackless_evaluator — never on the shared scheduler.

Commit 4 (Ariadne integration, part 1). Stands up the single app-wide scheduler
the §4.7 decision calls for: cvc::app now owns one lazily-built async_scheduler,
exposed as exec_scheduler() (per-app-instance, like computePool()/world_clock()),
wired to the app's state root for state-watches. This is the one cooperative
timeline every Ariadne document the app hosts will schedule on; per-document
isolation (owner scope + per-process chroot + child env) layers on top in the
Ariadne runtime in the following commits.

Forward-declared in app.h (unique_ptr + mutex, no state_exec coroutine headers
leak into app.h's includers); constructed + wired in app.cpp.

Verified: 519 state_exec + ariadne tests PASS, incl. AppExecScheduler
(app-wide singleton identity + runs a program end-to-end).
…oup reaping)

Commit 5 (Ariadne integration, part 2): the isolation primitive for the app-wide
scheduler. execute_options + process carry an `owner` tag; both schedulers' execute()
copy it. async_scheduler::kill_owner(owner) kills every live process with that tag —
including parked (sleep/msg-recv/await) ones — so an Ariadne Runtime can reap its
whole process group on teardown and nothing lingers after its document closes (the
one hazard a shared, long-lived scheduler adds, per §4.7).

Not a scheduler_base/intrinsic method — it's a host/teardown operation the Runtime
calls on the concrete scheduler. Verified: +1 test (KillOwnerReapsProcessGroup —
reaps one owner's group, leaves another's untouched, idempotent). 520 tests PASS.
…n + per-frame pump)

Commit 6 (the core rewiring). A program `on:` no longer builds a throwaway scheduler
and runs to completion inside drain() on the UI thread. Instead each Runtime submits
the action as an owner-tagged, chrooted process on cvc::app::exec_scheduler() (the
single app-wide scheduler) and returns; drain() then pumps that scheduler a bounded
slice and sweeps finished actions.

- Runtime::Impl gains a unique owner_ tag, a live_actions_ registry of persistent
  ActionContext{tracker, proc, env, ictx} (the intrinsics capture &ictx by pointer, so a
  suspended action's context must outlive the run across frames), and used_scheduler_.
- submit_action(): apply_chroot(prefix) + register_intrinsics + host providers, submit
  with opts.owner=owner_ and the §7.4 per-activation caps; keep the context keyed by pid.
- pump_and_sweep_actions(): sync_run a bounded slice (generous enough a quick action still
  finishes THIS drain — preserving fire-then-check semantics — while an await/sleep/msg-recv
  action parks and resumes next frame), then drop terminated actions, warn+drop killed ones,
  keep still-parked ones.
- ~Impl reaps the whole process group via kill_owner(owner_) so nothing lingers after the
  document closes. init: stays synchronous via run_scoped_program (load-time, no frames).

The read lane is untouched — still a separate per-Runtime synchronous stackless_evaluator.
Verified: 660 state_exec + ariadne tests PASS (ariadne_runtime_test's AriadneAction/
HostIntrinsic/ProgramOn suites — fire-then-check — all green through the new async path).
…message ingress

Commit 7. Unlocks the marquee async scenario: an action parked on `(msg-recv "path")`
woken by a worker thread (e.g. a nav step finishing on app.computePool()). Delivery
paths (deliver_to_receivers etc.) mutate process state and are NOT thread-safe, and the
pump runs on the UI thread — so a worker calling them directly would race.

post_message(path, msg) is the one thread-safe entry point: it enqueues onto a
mutex-guarded ingress from ANY thread. step() calls drain_ingress() first, which swaps
the ingress out under the lock and applies each delivery via deliver_to_receivers ON THE
SCHEDULER THREAD. So all scheduler-state mutation stays single-threaded; only the small
ingress queue is locked (the intent-queue pattern, mirroring the roadmap's UI-intent seam).

Verified: +1 test (PostMessageFromAnotherThreadWakesReceiver — park a receiver, post from
a std::thread, next step drains the ingress and wakes it). 661 tests PASS. Full concurrent
TSan stress is a follow-up, but cross-thread access is confined to the locked ingress by
construction.
Follow-up: the DSL `(await expr)` form. Evaluates expr, yields the process until the next
frame boundary, and resumes with expr's value — so a long .ari action can spread its work
across frames instead of consuming one drain's budget.

Implemented as an intrinsic (no evaluator special-form change): intrinsic_await evaluates
its arg eagerly, calls sched->yield_frame(current_pid), and returns the value. yield_frame
is a new scheduler_base method — async_scheduler parks the process (waiting + a new
process.awaiting_frame flag), re-readied by wake_awaiting() which the Ariadne pump calls
ONCE per drain (not inside step()/sync_run), so an await crosses exactly one frame. On the
sync scheduler (CLI/pycvc, no frame concept) yield_frame is a no-op and await degrades to
identity. NB an await only parks when work follows it in the program; if it is the last
expression the program finishes in that step (done=true) and terminates normally.

Verified: +3 tests (YieldFrameParksUntilWakeAwaiting — a step does NOT wake, only
wake_awaiting does; AwaitIntrinsicParksThenResumesWithValue — `(begin (set r (await 42)) r)`
parks then resumes to 42). 663 state_exec + ariadne tests PASS.
… async action test

Follow-up + a real fix. A parked-only scheduler (every process waiting on msg-recv/await)
has has_runnable()==false, so sync_run/run skip stepping entirely — and step() is where the
ingress is drained. A cross-thread post_message() delivery that would READY a parked receiver
was therefore never applied by the pump: the receiver stayed parked forever. Fix: the Ariadne
pump calls exec_scheduler().drain_ingress() (now public) BEFORE sync_run, so a delivery readies
its receiver before the has_runnable() gate. step() still drains the ingress too (for messages
that arrive mid-run).

Adds the marquee end-to-end test through the real Runtime
(ProgramActionParksOnMsgRecvAndResumesWhenWorkerDelivers): a program `on:` parks on
(msg-recv "async.done") across a drain without blocking it, a WORKER THREAD delivers via the
thread-safe post_message ingress, and the next drain wakes + resumes the action to completion.

Verified: 664 state_exec + ariadne tests PASS. NB threading the DELIVERED VALUE into a nested
expression (e.g. `(state-set k (msg-recv p))`) is a separate PRE-EXISTING issue —
deliver_to_receivers's placeholder patch (stack.back().results.back(), ported verbatim from the
sync scheduler) mis-targets a nested msg-recv; the wake/resume path itself is correct.
…pression

Follow-up + correction. Adds ProgramActionCapturesDeliveredValueFromMsgRecv:
`(state-set "async.value" (msg-recv "vchan"))` — a worker's post_message("vchan","delivered")
wakes the parked action and the delivered value flows into the state-set (async.value ==
"delivered"). This confirms value-threading WORKS: pop_frame pushes msg-recv's nil placeholder
onto the parent frame's results, and deliver_to_receivers patches exactly that slot.

Corrects the caveat in the prior commit (3c5fc9a) and the sibling sentinel test's comment:
there is NO pre-existing value-threading bug — the earlier symptom was solely the ingress-drain
gap (parked-only scheduler never drained the ingress), fixed in 3c5fc9a. 73 ariadne_runtime
tests pass.
…rocess

Follow-up: the §7.1 resident-await handler for on:tick. A document's `on_tick:` block is a
per-frame state_exec program that runs as ONE long-lived process (submitted once, NOT
re-submitted each frame) parking between frames — the resident-await pattern.

- Loader: captures document-level `on_tick:`/`on_key:` scalar scripts into
  LoadResult.on_tick_script/on_key_script (mirroring init:), added to is_builtin_block; warns
  on a non-scalar node.
- Runtime::set_tick_program(script): stores the body; ensure_tick_resident() submits ONE
  owner-tagged, chrooted process wrapping the body in
  `(while t (begin (msg-recv "<owner>#tick") <body>))` (max_steps=0 — a resident loops forever,
  parking each frame; the per-frame pump budget bounds each activation). drain() calls
  ensure_tick_resident() + post_tick() (coalesced: post only if the channel has no pending tick)
  before the pump, so the resident runs its body once per drain. Reaped by ~Impl's kill_owner;
  set_tick_program("") kills it.
- ariadne_hello wires lr.on_tick_script -> rt.set_tick_program.

on:key is captured by the loader but NOT wired: the Backend exposes only a static
Capabilities::keyboard flag, no per-frame key events — on:key awaits a Backend key-event seam.

Verified: +5 tests (AriadneResident.TickProgramRunsOncePerDrain/ClearingTickProgramStopsIt;
AriadneInit.CapturesOnTickAndOnKeyScripts/NonScalarOnTickWarns). 542 ariadne + state_exec
tests pass; ariadne_hello builds.
@transfix
transfix merged commit 958dbeb into master Sep 28, 2026
13 checks passed
@transfix
transfix deleted the design/ariadne-async-integration branch October 29, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant