Files
Library/tests/standalone/Fsm/SubMachine.cpp
T
faumarayandClaude Fable 5 ec9e466d7a 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>
2026-06-15 01:20:56 +03:00

226 lines
5.1 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
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>(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 via its startup-entry (run(int)): doubles the seed,
// fails (typed FsmError) if negative.
class Doubler : public Fsm::Machine<Doubler>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
struct Run : State {
Task<Step> run(int seed)
{
if (seed < 0) {
co_return fail("negative");
}
co_return succeed(seed * 2);
}
};
using States = Fsm::States<Run>;
};
class Calc : public Fsm::Machine<Calc>
{
public:
using Success = std::string;
int input = 0;
struct Start : State {
Task<Step> run()
{
co_return startMachine<Doubler, AfterDouble>(
self().input);
}
};
struct AfterDouble : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return succeed("doubled:" + std::to_string(s.value));
}
Task<Step> no(ChildFailure auto f)
{
co_return succeed("failed:" + f.value.message);
}
};
using States = Fsm::States<Start, AfterDouble>;
explicit Calc(int in) : input(in) {}
};
} // namespace
// @tag integration
Void::Testing::Test submachine_success_dispatches_on_success()
{
Calc fsm{5};
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"doubled:10"});
}
// @tag integration
Void::Testing::Test submachine_failure_dispatches_on_failure()
{
Calc fsm{-1};
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"failed:negative"});
}
// ── void-result sub-machine ─────────────────────────────────────────────────
namespace
{
class Pinger : public Fsm::Machine<Pinger>
{
public:
struct Ping : State {
Task<Step> run() { co_return succeed(); }
};
using States = Fsm::States<Ping>;
};
class Host : public Fsm::Machine<Host>
{
public:
using Success = int;
struct Go : State {
Task<Step> run() { co_return startMachine<Pinger, Done>(); }
};
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_success_handler()
{
Host fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 42);
}
// ── chained startMachine (a continuation state launches another) ─────────────
namespace
{
class Chain : public Fsm::Machine<Chain>
{
public:
using Success = int;
struct A : State {
Task<Step> run()
{
co_return startMachine<Doubler, B>(3); // 3 -> 6
}
};
struct B : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return startMachine<Doubler, C>(s.value); // 6 -> 12
}
};
struct C : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return succeed(s.value);
}
};
using States = Fsm::States<A, B, C>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_chains_through_continuation_states()
{
Chain fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 12);
}
// ── the sub-machine shares the parent's event mailbox ────────────────────────
namespace
{
struct Tick {
};
class WaitSub : public Fsm::Machine<WaitSub>
{
public:
using Success = int;
struct W : State {
Task<Step> run()
{
co_await await_event<Tick>();
co_return succeed(7);
}
};
using States = Fsm::States<W>;
};
class Outer : public Fsm::Machine<Outer>
{
public:
using Success = int;
struct Begin : State {
Task<Step> run() { co_return startMachine<WaitSub, Fin>(); }
};
struct Fin : State {
Task<Step> ok(ChildSuccess auto s)
{
co_return succeed(s.value);
}
};
using States = Fsm::States<Begin, Fin>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_shares_parent_event_mailbox()
{
Outer fsm;
fsm.start();
fsm.post(Tick{}); // delivered to the sub-machine awaiting on the shared ctx
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7);
}