Repository navigation
Ariadne async execution semantics: app-wide scheduler wiring + owner scopes - #437
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 blockingthe UI thread.
Landed so far
cvc::app::exec_scheduler()— one lazily-built, per-appasync_scheduler(thesingle cooperative timeline every
.aridocument the app hosts shares), wired to theapp's state root. Per-app-instance, like
computePool()/world_clock().kill_owner—execute_options/processcarry anownertag; bothschedulers copy it;
async_scheduler::kill_owner(owner)reaps a document's wholeprocess group (parked processes included) so nothing lingers after a Runtime tears down.
Incoming (the
ariadne.cpprewiring)app.exec_scheduler()(owner = the document) instead of a throwawaylocal scheduler; action
on:programs submit-and-return with a persistentActionContext(intrinsics_context+env kept alive until the process terminates viaon_complete), chrooted to the doc prefix.kill_owner.(await expr)on the scheduled path;on:tick/on:keyresidents (§7.1).Testing
520 state_exec + ariadne tests pass locally (incl.
AppExecSchedulerapp-wide-singletonidentity +
KillOwnerReapsProcessGroup). The read lane stays a separate synchronousstackless_evaluator— never on the shared scheduler.