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>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 21:14:43 +03:00
1 parent 7c7c590d79
commit 9e9eb838ea
20 files changed
+487 -245

No files matched your search

+30 -13
View File
@@ -3,6 +3,7 @@
#include <Void/Testing/Test.hpp>
#include <string>
#include <variant>
using Void::Coro::Task;
using Void::Fsm::FsmError;
@@ -16,10 +17,13 @@ namespace Fsm = Void::Fsm;
namespace
{
// Sub-machine seeded by constructor arg: doubles `seed`, fails if negative.
class Doubler : public Fsm::Machine<Doubler, int>
class Doubler : public Fsm::Machine<Doubler>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
int seed;
struct Run : State {
@@ -36,10 +40,12 @@ namespace
explicit Doubler(int s = 0) : seed(s) {}
};
class Calc : public Fsm::Machine<Calc, std::string>
class Calc : public Fsm::Machine<Calc>
{
public:
using Success = std::string;
int input = 0;
struct Start : State {
@@ -49,7 +55,7 @@ namespace
self().input);
}
};
struct AfterDouble : Continue<int> {
struct AfterDouble : Continue<Doubler> {
Task<Step> on_success(int v) override
{
co_return succeed("doubled:" + std::to_string(v));
@@ -87,7 +93,7 @@ Void::Testing::Test submachine_failure_dispatches_on_failure()
namespace
{
class Pinger : public Fsm::Machine<Pinger, void>
class Pinger : public Fsm::Machine<Pinger>
{
public:
@@ -97,18 +103,23 @@ namespace
using States = Fsm::States<Ping>;
};
class Host : public Fsm::Machine<Host, int>
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<void> {
Task<Step> on_success() override { co_return succeed(42); }
struct Done : Continue<Pinger> {
Task<Step> on_success(std::monostate) override
{
co_return succeed(42);
}
};
using States = Fsm::States<Go, Done>;
};
@@ -127,23 +138,25 @@ Void::Testing::Test submachine_void_result_calls_nullary_on_success()
namespace
{
class Chain : public Fsm::Machine<Chain, int>
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<int> {
struct B : Continue<Doubler> {
Task<Step> on_success(int v) override
{
co_return startMachine<Doubler, C>(v); // 6 -> 12
}
};
struct C : Continue<int> {
struct C : Continue<Doubler> {
Task<Step> on_success(int v) override
{
co_return succeed(v);
@@ -169,10 +182,12 @@ namespace
struct Tick {
};
class WaitSub : public Fsm::Machine<WaitSub, int>
class WaitSub : public Fsm::Machine<WaitSub>
{
public:
using Success = int;
struct W : State {
Task<Step> run() override
{
@@ -183,17 +198,19 @@ namespace
using States = Fsm::States<W>;
};
class Outer : public Fsm::Machine<Outer, int>
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<int> {
struct Fin : Continue<WaitSub> {
Task<Step> on_success(int v) override
{
co_return succeed(v);