FSM (2): declarative sub-machine continuations (startMachine + Continue<R>)

A state can now launch a full sub-FSM and have the engine resume into a
named state that handles the result, instead of awaiting+branching by hand:

    co_return startMachine<Login, AfterLogin>(user, tries);

    struct AfterLogin : Continue<bool> {        // Login = Machine<Login,bool>
      Task<Step> on_success(bool ok) override {
        co_return ok ? next<Home>() : next<Locked>(); }
      Task<Step> on_failure(FsmError e) override { co_return fail(e.message); }
    };

- Sub runs on the parent's shared executor/event-mailbox/services (reuses
  run_child); seed data is forwarded to Sub's constructor.
- The engine dispatches on its own: on entry with a pending result it calls
  the state's resume(), which Continue<R> overrides to pick
  on_success(R)/on_failure(FsmError) by the outcome. Continue<void> takes a
  nullary on_success(). A Continue handler may itself return another
  startMachine (chaining) or any Step.
- Type-safe: startMachine<Sub, Next> static_asserts Next derives
  Continue<Sub::result_type> and is in States<>.

Mechanics: Step gains Kind::Launch carrying the target + a type-erased
launcher (a coroutine that runs the sub and maps its Outcome to a
LaunchResult); drive() awaits it, stashes the result, and the entered
state's resume() consumes it. StateBase::resume defaults to run().

5 new tests (SubMachine): success/failure dispatch, void-result nullary
on_success, chained startMachine through Continue states, and that the sub
shares the parent's event mailbox. 987 green, ASan+UBSan clean, stable
across 10 runs, -Werror clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 15:37:40 +03:00
1 parent fedc14b0e3
commit b34226369a
7 files changed
+447 -3

No files matched your search

+7
View File
@@ -13,6 +13,13 @@ namespace Void::Fsm::Detail
co_return Step::failure(FsmError{"state defines no run()"});
}
Coro::Task<Step> StateBase::resume(LaunchResult /*result*/)
{
// A plain state entered with a pending sub-machine result just runs;
// Continue<R> overrides this to dispatch on_success / on_failure.
co_return co_await run();
}
ExecutionContext::~ExecutionContext() { stop(); }
void ExecutionContext::post(EventEnvelope env)