Files
Library/tests/standalone/Fsm/SubMachine.cpp
T
faumarayandClaude Fable 5 9e9eb838ea FSM: typed Success/Failure result model (reflected nested types)
A machine is now Machine<Derived> (dropping the ResultT template param) and
declares its result types as nested types reflected *by name* — Success and
Failure (a struct or a using-alias), each defaulting to void:

    class Login : public Machine<Login> {
      using Success = Token;     // or a nested struct; default void
      struct Failure { int code; };
    };

- Outcome = std::expected<Success, Failure> (void failure -> std::monostate
  error arm). succeed(Success{...}) / fail(Failure{...}) carry the typed
  values (nullary when void). Resolved by plain dependent-name lookup
  (Detail/Result.hpp), no P2996.
- Engine/system faults (no initial state, unregistered target, no entry)
  travel a SEPARATE FsmError channel: status()==Faulted + system_error(),
  never the typed Failure. New Step::Kind::Fault carries it.
- CRTP wrinkle: Derived is incomplete at base instantiation, so the engine
  stores the result type-erased (std::any) and rebuilds the typed Outcome in
  wait()/run_blocking()/take_outcome() — function templates defaulted on
  Derived. run_child/run_region/start_machine reconstruct OutcomeOf<Sub> at
  the boundary (Sub is complete).
- Step carries Success/Failure payloads as std::any; the reactive DSL
  (finish/fail) and Continue<Sub> are reparameterised off the nested types.

Interim: Continue<Sub> keeps the named on_success/on_failure form (Phase 2
replaces it with signature-inferred ChildSuccess/ChildFailure). All FSM
tests migrated to the nested-type form. 995 tests (992 green, 3 skipped),
ASan clean, -Werror clean; the lone intermittent failure is the
pre-existing racy plugin hot-reload-under-load test (flaky on HEAD too).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 21:14:43 +03:00

233 lines
5.1 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <variant>
using Void::Coro::Task;
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.
namespace
{
// Sub-machine seeded by constructor arg: doubles `seed`, fails if negative.
class Doubler : public Fsm::Machine<Doubler>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
int seed;
struct Run : State {
Task<Step> run() override
{
if (self().seed < 0) {
co_return fail("negative");
}
co_return succeed(self().seed * 2);
}
};
using States = Fsm::States<Run>;
explicit Doubler(int s = 0) : seed(s) {}
};
class Calc : public Fsm::Machine<Calc>
{
public:
using Success = std::string;
int input = 0;
struct Start : State {
Task<Step> run() override
{
co_return startMachine<Doubler, AfterDouble>(
self().input);
}
};
struct AfterDouble : Continue<Doubler> {
Task<Step> on_success(int v) override
{
co_return succeed("doubled:" + std::to_string(v));
}
Task<Step> on_failure(FsmError e) override
{
co_return succeed("failed:" + e.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() override { co_return succeed(); }
};
using States = Fsm::States<Ping>;
};
class Host : public Fsm::Machine<Host>
{
public:
using Success = int;
struct Go : State {
Task<Step> run() override
{
co_return startMachine<Pinger, Done>();
}
};
struct Done : Continue<Pinger> {
Task<Step> on_success(std::monostate) override
{
co_return succeed(42);
}
};
using States = Fsm::States<Go, Done>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_void_result_calls_nullary_on_success()
{
Host fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 42);
}
// ── chained startMachine (a Continue state launches another) ─────────────────
namespace
{
class Chain : public Fsm::Machine<Chain>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override
{
co_return startMachine<Doubler, B>(3); // 3 -> 6
}
};
struct B : Continue<Doubler> {
Task<Step> on_success(int v) override
{
co_return startMachine<Doubler, C>(v); // 6 -> 12
}
};
struct C : Continue<Doubler> {
Task<Step> on_success(int v) override
{
co_return succeed(v);
}
};
using States = Fsm::States<A, B, C>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_chains_through_continue_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() override
{
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() override
{
co_return startMachine<WaitSub, Fin>();
}
};
struct Fin : Continue<WaitSub> {
Task<Step> on_success(int v) override
{
co_return succeed(v);
}
};
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);
}