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

+15 -9
View File
@@ -12,10 +12,12 @@ using Void::Fsm::Step;
namespace
{
// A linear workflow: Start -> Work (loops 3x) -> success(count).
class Counter : public Void::Fsm::Machine<Counter, int>
class Counter : public Void::Fsm::Machine<Counter>
{
public:
using Success = int;
int count = 0;
std::vector<std::string>* log = nullptr;
@@ -47,16 +49,19 @@ namespace
using States = Void::Fsm::States<Start, Work>;
Counter()
: Void::Fsm::Machine<Counter, int>(
: Void::Fsm::Machine<Counter>(
MachineOptions{.own_worker = false})
{
}
};
class Failing : public Void::Fsm::Machine<Failing, int>
class Failing : public Void::Fsm::Machine<Failing>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
struct Boom : State {
Task<Step> run() override { co_return fail("kaboom"); }
};
@@ -64,7 +69,7 @@ namespace
using States = Void::Fsm::States<Boom>;
Failing()
: Void::Fsm::Machine<Failing, int>(
: Void::Fsm::Machine<Failing>(
MachineOptions{.own_worker = false})
{
}
@@ -107,13 +112,13 @@ Void::Testing::Test fsm_pump_drives_to_completion()
Void::Testing::Test fsm_void_result_succeeds()
{
struct Trivial : Void::Fsm::Machine<Trivial, void> {
struct Trivial : Void::Fsm::Machine<Trivial> {
struct Done : State {
Task<Step> run() override { co_return succeed(); }
};
using States = Void::Fsm::States<Done>;
Trivial()
: Void::Fsm::Machine<Trivial, void>(
: Void::Fsm::Machine<Trivial>(
MachineOptions{.own_worker = false})
{
}
@@ -127,7 +132,9 @@ Void::Testing::Test fsm_dynamic_state_registration()
{
// Dynamic machine: states registered conditionally, addressed with
// next_dynamic<S>() (skips the compile-time States<> check).
struct Flow : Void::Fsm::Machine<Flow, int> {
struct Flow : Void::Fsm::Machine<Flow> {
using Success = int;
bool express;
struct Express : State {
Task<Step> run() override { co_return succeed(99); }
@@ -145,8 +152,7 @@ Void::Testing::Test fsm_dynamic_state_registration()
}
};
explicit Flow(bool e)
: Void::Fsm::Machine<Flow, int>(
MachineOptions{.own_worker = false}),
: Void::Fsm::Machine<Flow>(MachineOptions{.own_worker = false}),
express(e)
{
initial<Start>();
+8 -5
View File
@@ -22,10 +22,12 @@ namespace
// Sums many Tick events delivered from several threads, finishing when
// the running total reaches a target. Single consumer (the worker) so
// the total is race-free without the FSM holding any lock.
class Summer : public Void::Fsm::Machine<Summer, long long>
class Summer : public Void::Fsm::Machine<Summer>
{
public:
using Success = long long;
long long total = 0;
long long target;
@@ -42,7 +44,7 @@ namespace
};
explicit Summer(long long tgt)
: Void::Fsm::Machine<Summer, long long>(), target(tgt)
: Void::Fsm::Machine<Summer>(), target(tgt)
{
initial<Run>();
}
@@ -132,7 +134,9 @@ Void::Testing::Test fsm_current_state_reads_never_tear_under_load()
Void::Testing::Test fsm_resolves_service_through_context()
{
struct Calc : Void::Fsm::Machine<Calc, int> {
struct Calc : Void::Fsm::Machine<Calc> {
using Success = int;
struct Go : State {
Task<Step> run() override
{
@@ -140,8 +144,7 @@ Void::Testing::Test fsm_resolves_service_through_context()
}
};
Calc()
: Void::Fsm::Machine<Calc, int>(
MachineOptions{.own_worker = false})
: Void::Fsm::Machine<Calc>(MachineOptions{.own_worker = false})
{
initial<Go>();
}
+22 -11
View File
@@ -20,10 +20,12 @@ namespace
};
// Sums two Add events, posted in one fluent send() call.
class Sum : public Fsm::Machine<Sum, int>
class Sum : public Fsm::Machine<Sum>
{
public:
using Success = int;
int total = 0;
struct Take : State {
@@ -55,10 +57,12 @@ namespace
{
// A and C are not connected by any declared edge; goto_state<C>() jumps
// from A to C freely.
class Hop : public Fsm::Machine<Hop, int>
class Hop : public Fsm::Machine<Hop>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return goto_state<C>(); }
};
@@ -88,10 +92,12 @@ namespace
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
// next boundary.
class Jumper : public Fsm::Machine<Jumper, int>
class Jumper : public Fsm::Machine<Jumper>
{
public:
using Success = int;
struct Loop : State {
Task<Step> run() override
{
@@ -104,10 +110,7 @@ namespace
};
using States = Fsm::States<Loop, Done>;
explicit Jumper(MachineOptions o = {})
: Fsm::Machine<Jumper, int>(o)
{
}
explicit Jumper(MachineOptions o = {}) : Fsm::Machine<Jumper>(o) {}
};
} // namespace
@@ -136,10 +139,12 @@ Void::Testing::Test jump_redirects_at_next_boundary()
namespace
{
// Increments a machine-owned counter each run (data survives reset()).
class Counter : public Fsm::Machine<Counter, int>
class Counter : public Fsm::Machine<Counter>
{
public:
using Success = int;
int runs = 0;
struct Once : State {
@@ -165,10 +170,12 @@ Void::Testing::Test restart_reruns_the_machine()
namespace
{
class Pick : public Fsm::Machine<Pick, int>
class Pick : public Fsm::Machine<Pick>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return succeed(1); }
};
@@ -194,10 +201,12 @@ Void::Testing::Test restart_at_changes_initial_state()
namespace
{
class Mut : public Fsm::Machine<Mut, int>
class Mut : public Fsm::Machine<Mut>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return succeed(1); }
};
@@ -245,10 +254,12 @@ namespace
struct Go {
};
class Sleeper : public Fsm::Machine<Sleeper, int>
class Sleeper : public Fsm::Machine<Sleeper>
{
public:
using Success = int;
struct W : State {
Task<Step> run() override
{
+4 -2
View File
@@ -20,10 +20,12 @@ namespace
};
// Waits for Input events, accumulating until a Stop event arrives.
class Accumulator : public Void::Fsm::Machine<Accumulator, int>
class Accumulator : public Void::Fsm::Machine<Accumulator>
{
public:
using Success = int;
int total = 0;
std::vector<int>* order = nullptr;
@@ -45,7 +47,7 @@ namespace
};
explicit Accumulator(bool own_worker)
: Void::Fsm::Machine<Accumulator, int>(
: Void::Fsm::Machine<Accumulator>(
MachineOptions{.own_worker = own_worker})
{
initial<Waiting>();
+9 -4
View File
@@ -24,10 +24,13 @@ namespace
// Child region: waits for either Tick (succeed) or Cancel (fail),
// handling both at one level via await_any.
class Work : public Void::Fsm::Machine<Work, std::string>
class Work : public Void::Fsm::Machine<Work>
{
public:
using Success = std::string;
using Failure = Void::Fsm::FsmError;
struct Waiting : State {
Task<Step> run() override
{
@@ -41,17 +44,19 @@ namespace
using States = Void::Fsm::States<Waiting>;
explicit Work(MachineOptions opts = {})
: Void::Fsm::Machine<Work, std::string>(opts)
: Void::Fsm::Machine<Work>(opts)
{
}
};
// Parent: a composite Running state enters the Work region and maps its
// result — success passes the value through, failure routes to a sibling.
class Job : public Void::Fsm::Machine<Job, std::string>
class Job : public Void::Fsm::Machine<Job>
{
public:
using Success = std::string;
struct Running : State {
Task<Step> run() override
{
@@ -71,7 +76,7 @@ namespace
using States = Void::Fsm::States<Running, Aborted>;
explicit Job(MachineOptions opts = {})
: Void::Fsm::Machine<Job, std::string>(opts)
: Void::Fsm::Machine<Job>(opts)
{
}
};
+6 -5
View File
@@ -13,10 +13,12 @@ using Void::Fsm::Transition;
namespace
{
// Records enter/run/exit for each state into a shared log.
class Lifecycle : public Void::Fsm::Machine<Lifecycle, int>
class Lifecycle : public Void::Fsm::Machine<Lifecycle>
{
public:
using Success = int;
std::vector<std::string> log;
struct A : State {
@@ -58,7 +60,7 @@ namespace
using States = Void::Fsm::States<A, B>;
Lifecycle()
: Void::Fsm::Machine<Lifecycle, int>(
: Void::Fsm::Machine<Lifecycle>(
MachineOptions{.own_worker = false})
{
}
@@ -106,15 +108,14 @@ Void::Testing::Test fsm_state_names_and_path()
// run() which fails — confirms the default body exists.
Void::Testing::Test fsm_state_without_run_fails()
{
struct NoRun : Void::Fsm::Machine<NoRun, void> {
struct NoRun : Void::Fsm::Machine<NoRun> {
struct Stuck : State {
Task<void> on_enter() override { co_return; }
// no run() override
};
using States = Void::Fsm::States<Stuck>;
NoRun()
: Void::Fsm::Machine<NoRun, void>(
MachineOptions{.own_worker = false})
: Void::Fsm::Machine<NoRun>(MachineOptions{.own_worker = false})
{
}
} fsm;
+22 -9
View File
@@ -11,10 +11,12 @@ using Void::Fsm::Step;
namespace
{
// Child machine: computes a value and succeeds.
class Subtotal : public Void::Fsm::Machine<Subtotal, int>
class Subtotal : public Void::Fsm::Machine<Subtotal>
{
public:
using Success = int;
int seed;
struct Compute : State {
@@ -26,7 +28,7 @@ namespace
};
explicit Subtotal(int s)
: Void::Fsm::Machine<Subtotal, int>(
: Void::Fsm::Machine<Subtotal>(
MachineOptions{.own_worker = false}),
seed(s)
{
@@ -35,15 +37,18 @@ namespace
};
// Child machine that fails.
class FailingChild : public Void::Fsm::Machine<FailingChild, int>
class FailingChild : public Void::Fsm::Machine<FailingChild>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
struct Boom : State {
Task<Step> run() override { co_return fail("child boom"); }
};
FailingChild()
: Void::Fsm::Machine<FailingChild, int>(
: Void::Fsm::Machine<FailingChild>(
MachineOptions{.own_worker = false})
{
initial<Boom>();
@@ -51,10 +56,13 @@ namespace
};
// Parent: enters a child, uses its result to decide the outcome.
class Parent : public Void::Fsm::Machine<Parent, int>
class Parent : public Void::Fsm::Machine<Parent>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
struct Start : State {
Task<Step> run() override
{
@@ -68,17 +76,20 @@ namespace
};
Parent()
: Void::Fsm::Machine<Parent, int>(
: Void::Fsm::Machine<Parent>(
MachineOptions{.own_worker = false})
{
initial<Start>();
}
};
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure, int>
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
struct Start : State {
Task<Step> run() override
{
@@ -91,7 +102,7 @@ namespace
};
ParentOfFailure()
: Void::Fsm::Machine<ParentOfFailure, int>(
: Void::Fsm::Machine<ParentOfFailure>(
MachineOptions{.own_worker = false})
{
initial<Start>();
@@ -121,7 +132,9 @@ Void::Testing::Test fsm_nesting_with_worker_thread()
{
Parent fsm{}; // embedder by default here
// Re-run as worker-driven to exercise the shared executor + worker.
struct WorkerParent : Void::Fsm::Machine<WorkerParent, int> {
struct WorkerParent : Void::Fsm::Machine<WorkerParent> {
using Success = int;
struct Start : State {
Task<Step> run() override
{
+11 -5
View File
@@ -19,10 +19,13 @@ namespace
{
// Attempt fails the first `fail_n` times, then succeeds. with_retry
// keeps trying; next_or routes exhaustion to a fallback.
class Flaky : public Void::Fsm::Machine<Flaky, std::string>
class Flaky : public Void::Fsm::Machine<Flaky>
{
public:
using Success = std::string;
using Failure = Void::Fsm::FsmError;
int fail_n = 0;
int attempts = 0;
int retries; // how many tries with_retry gets
@@ -82,10 +85,12 @@ Void::Testing::Test retry_exhausts_routes_to_fallback()
namespace
{
class Pipe : public Void::Fsm::Machine<Pipe, int>
class Pipe : public Void::Fsm::Machine<Pipe>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return next<B>(); }
};
@@ -94,8 +99,7 @@ namespace
};
using States = Void::Fsm::States<A, B>;
Pipe()
: Void::Fsm::Machine<Pipe, int>(
MachineOptions{.own_worker = false})
: Void::Fsm::Machine<Pipe>(MachineOptions{.own_worker = false})
{
}
};
@@ -145,10 +149,12 @@ namespace
struct Cancel {
};
class Flow : public Void::Fsm::Machine<Flow, int>
class Flow : public Void::Fsm::Machine<Flow>
{
public:
using Success = int;
struct Idle : Reactive {
void transitions(TransitionBuilder& on) override
{
+10 -3
View File
@@ -22,10 +22,13 @@ namespace
// A declarative machine: Idle reacts to events with guards/actions; on
// timeout it bails out.
class Gate : public Void::Fsm::Machine<Gate, int>
class Gate : public Void::Fsm::Machine<Gate>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
int total = 0;
std::vector<std::string> trace;
@@ -87,10 +90,12 @@ namespace
};
// Imperative state using await_event_for: times out → expired.
class Sleeper : public Void::Fsm::Machine<Sleeper, std::string>
class Sleeper : public Void::Fsm::Machine<Sleeper>
{
public:
using Success = std::string;
struct Waiting : State {
Task<Step> run() override
{
@@ -129,10 +134,12 @@ Void::Testing::Test await_event_for_receives_event()
namespace
{
// Reactive timeout transition (on.after().to<>).
class Deadline : public Void::Fsm::Machine<Deadline, int>
class Deadline : public Void::Fsm::Machine<Deadline>
{
public:
using Success = int;
struct Pending : Reactive {
void transitions(TransitionBuilder& on) override
{
+4 -2
View File
@@ -13,10 +13,12 @@ namespace
{
// A machine that parks at a named state and exposes its data for
// serialize/restore.
class Pipeline : public Void::Fsm::Machine<Pipeline, int>
class Pipeline : public Void::Fsm::Machine<Pipeline>
{
public:
using Success = int;
struct Data {
int processed = 0;
std::string stage = "init";
@@ -40,7 +42,7 @@ namespace
};
Pipeline()
: Void::Fsm::Machine<Pipeline, int>(
: Void::Fsm::Machine<Pipeline>(
MachineOptions{.own_worker = false})
{
initial<Ingest>();
+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);
+7 -3
View File
@@ -66,10 +66,12 @@ namespace
};
// An FSM that bumps the counter service N times then succeeds.
class Worker : public Void::Fsm::Machine<Worker, int>
class Worker : public Void::Fsm::Machine<Worker>
{
public:
using Success = int;
int times;
struct Run : State {
@@ -85,7 +87,7 @@ namespace
};
explicit Worker(int t)
: Void::Fsm::Machine<Worker, int>(
: Void::Fsm::Machine<Worker>(
MachineOptions{.own_worker = false}),
times(t)
{
@@ -110,7 +112,9 @@ Void::Testing::Test orchestrator_launches_and_supervises()
{
// Worker-backed FSM so the orchestrator's start() spins a worker and
// the FSM runs to completion on its own.
struct AsyncWorker : Void::Fsm::Machine<AsyncWorker, int> {
struct AsyncWorker : Void::Fsm::Machine<AsyncWorker> {
using Success = int;
struct Run : State {
Task<Step> run() override
{