FSM (4): one signature-inferred State (drops Reactive/Continue bases)

There is now a single state base, `State`. The engine infers each state's
role purely from member signatures via P2996 — no `override`, no
framework-mandated names:

  struct Work : State {                          // entry
    Coro::Task<Step> run() { ... }
    Coro::Task<Step> run(Cfg c) { ... }          // startup-entry (launch data)
  };
  struct Idle : State {                          // reactive
    void transitions(TransitionBuilder& on) { on.event<Go>().to<Run>(); }
  };
  struct After : State {                         // continuation
    Coro::Task<Step> ok(ChildSuccess auto s) { use s.value; }
    Coro::Task<Step> no(ChildFailure auto f) { use f.value; }
  };

- Detail/Roles.hpp: ChildSuccess/ChildFailure concepts over boxes, plus the
  reflection selectors (invoke_entry / invoke_startup / invoke_handler /
  dispatch_result / has_transitions / make_driver). At add<S>() a per-state
  driver thunk is stored (reactive table vs entry, by whether S has a
  TransitionBuilder& member); drive() calls it. startMachine<Sub, Next>(data)
  builds a Launch step carrying a launcher (injects data into Sub's initial
  startup-entry) + a dispatcher (dispatch_result<Next, Sub>) that builds the
  ChildSuccess/Failure box and calls Next's matching handler.
- Removed Reactive/Continue<R> bases, StateKind/kind(), StateBase::run/resume
  (only on_enter/on_exit remain virtual). Reactive states keep the same DSL.
- Toolchain note: template-for + member splicing fails inside a coroutine
  body in this GCC build, so the selectors are non-coroutines returning a
  Task<Step> (plain Step wrapped via ready_task). Discovery is
  invocability-based — no parameter-type reflection (P3096). Probe-verified.

All FSM tests migrated to the nameless style (SubMachine: Continue ->
ChildSuccess/Failure handlers + startup-entry seeding; reactive states drop
the Reactive base/override). 995 tests (992 green, 3 skipped), ASan clean,
-Werror clean; lone intermittent failure remains the pre-existing racy
plugin hot-reload-under-load test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-15 01:20:56 +03:00
1 parent 9e9eb838ea
commit ec9e466d7a
20 files changed
+536 -358

No files matched your search

+39 -46
View File
@@ -3,20 +3,25 @@
#include <Void/Testing/Test.hpp>
#include <string>
#include <variant>
using Void::Coro::Task;
using Void::Fsm::ChildFailure;
using Void::Fsm::ChildSuccess;
using Void::Fsm::FsmError;
using Void::Fsm::Step;
namespace Fsm = Void::Fsm;
// startMachine<Sub, Next>(args...) launches a full sub-FSM on the parent's
// shared executor/events/services, then enters Next — a Continue<Sub::result>
// state — and the engine dispatches to on_success / on_failure by the outcome.
// startMachine<Sub, Next>(data) launches a full sub-FSM on the parent's shared
// executor/events/services (startup data routed to Sub's initial-state
// startup-entry), then enters Next. The engine dispatches the sub's typed
// outcome to Next's signature-matched handler — a member taking
// `ChildSuccess auto` (success) or `ChildFailure auto` (failure). No override,
// no fixed names.
namespace
{
// Sub-machine seeded by constructor arg: doubles `seed`, fails if negative.
// Sub-machine seeded via its startup-entry (run(int)): doubles the seed,
// fails (typed FsmError) if negative.
class Doubler : public Fsm::Machine<Doubler>
{
public:
@@ -24,20 +29,16 @@ namespace
using Success = int;
using Failure = Void::Fsm::FsmError;
int seed;
struct Run : State {
Task<Step> run() override
Task<Step> run(int seed)
{
if (self().seed < 0) {
if (seed < 0) {
co_return fail("negative");
}
co_return succeed(self().seed * 2);
co_return succeed(seed * 2);
}
};
using States = Fsm::States<Run>;
explicit Doubler(int s = 0) : seed(s) {}
};
class Calc : public Fsm::Machine<Calc>
@@ -49,20 +50,20 @@ namespace
int input = 0;
struct Start : State {
Task<Step> run() override
Task<Step> run()
{
co_return startMachine<Doubler, AfterDouble>(
self().input);
}
};
struct AfterDouble : Continue<Doubler> {
Task<Step> on_success(int v) override
struct AfterDouble : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return succeed("doubled:" + std::to_string(v));
co_return succeed("doubled:" + std::to_string(s.value));
}
Task<Step> on_failure(FsmError e) override
Task<Step> no(ChildFailure auto f)
{
co_return succeed("failed:" + e.message);
co_return succeed("failed:" + f.value.message);
}
};
using States = Fsm::States<Start, AfterDouble>;
@@ -98,7 +99,7 @@ namespace
public:
struct Ping : State {
Task<Step> run() override { co_return succeed(); }
Task<Step> run() { co_return succeed(); }
};
using States = Fsm::States<Ping>;
};
@@ -110,23 +111,18 @@ namespace
using Success = int;
struct Go : State {
Task<Step> run() override
{
co_return startMachine<Pinger, Done>();
}
Task<Step> run() { co_return startMachine<Pinger, Done>(); }
};
struct Done : Continue<Pinger> {
Task<Step> on_success(std::monostate) override
{
co_return succeed(42);
}
struct Done : State {
// void-result sub: ChildSuccess box has no value.
Task<Step> ok(ChildSuccess auto) { co_return succeed(42); }
};
using States = Fsm::States<Go, Done>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_void_result_calls_nullary_on_success()
Void::Testing::Test submachine_void_result_calls_success_handler()
{
Host fsm;
const auto outcome = fsm.run_blocking();
@@ -134,7 +130,7 @@ Void::Testing::Test submachine_void_result_calls_nullary_on_success()
Void::Testing::expect_eq(*outcome, 42);
}
// ── chained startMachine (a Continue state launches another) ─────────────────
// ── chained startMachine (a continuation state launches another) ─────────────
namespace
{
@@ -145,21 +141,21 @@ namespace
using Success = int;
struct A : State {
Task<Step> run() override
Task<Step> run()
{
co_return startMachine<Doubler, B>(3); // 3 -> 6
}
};
struct B : Continue<Doubler> {
Task<Step> on_success(int v) override
struct B : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return startMachine<Doubler, C>(v); // 6 -> 12
co_return startMachine<Doubler, C>(s.value); // 6 -> 12
}
};
struct C : Continue<Doubler> {
Task<Step> on_success(int v) override
struct C : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return succeed(v);
co_return succeed(s.value);
}
};
using States = Fsm::States<A, B, C>;
@@ -167,7 +163,7 @@ namespace
} // namespace
// @tag integration
Void::Testing::Test submachine_chains_through_continue_states()
Void::Testing::Test submachine_chains_through_continuation_states()
{
Chain fsm;
const auto outcome = fsm.run_blocking();
@@ -189,7 +185,7 @@ namespace
using Success = int;
struct W : State {
Task<Step> run() override
Task<Step> run()
{
co_await await_event<Tick>();
co_return succeed(7);
@@ -205,15 +201,12 @@ namespace
using Success = int;
struct Begin : State {
Task<Step> run() override
{
co_return startMachine<WaitSub, Fin>();
}
Task<Step> run() { co_return startMachine<WaitSub, Fin>(); }
};
struct Fin : Continue<WaitSub> {
Task<Step> on_success(int v) override
struct Fin : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return succeed(v);
co_return succeed(s.value);
}
};
using States = Fsm::States<Begin, Fin>;