Files
Library/tests/standalone/Fsm/Hierarchy.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

130 lines
3.4 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <variant>
#include <vector>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::Step;
// Composite sub-states: a parent state runs a child region (run_region) that
// shares the parent's executor, event mailbox and services. The child's
// Outcome drives the parent's next transition; a parent-level await_any lets
// the region react to several event types (cooperative cancel).
namespace
{
struct Tick {
};
struct Cancel {
};
// 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>
{
public:
using Success = std::string;
using Failure = Void::Fsm::FsmError;
struct Waiting : State {
Task<Step> run() override
{
auto ev = co_await await_any<Tick, Cancel>();
if (std::holds_alternative<Cancel>(ev)) {
co_return fail("cancelled");
}
co_return succeed(std::string{"ticked"});
}
};
using States = Void::Fsm::States<Waiting>;
explicit Work(MachineOptions 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>
{
public:
using Success = std::string;
struct Running : State {
Task<Step> run() override
{
auto r = co_await run_region<Work>();
if (r.has_value()) {
co_return succeed(*r);
}
co_return next<Aborted>();
}
};
struct Aborted : State {
Task<Step> run() override
{
co_return succeed(std::string{"aborted"});
}
};
using States = Void::Fsm::States<Running, Aborted>;
explicit Job(MachineOptions opts = {})
: Void::Fsm::Machine<Job>(opts)
{
}
};
} // namespace
// @tag integration
Void::Testing::Test composite_region_result_drives_parent()
{
Job fsm;
fsm.start();
fsm.post(Tick{}); // region succeeds -> parent passes "ticked" through
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"ticked"});
}
// @tag integration
Void::Testing::Test parent_cancel_routes_to_sibling()
{
Job fsm;
fsm.start();
fsm.post(Cancel{}); // region fails -> parent transitions to Aborted
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"aborted"});
}
// state_path() reports the parent→child chain while a region is active.
// Embedder mode keeps the single consumer on this thread, so the read is
// deterministic.
Void::Testing::Test state_path_reports_parent_child()
{
Job fsm{MachineOptions{.own_worker = false}};
fsm.start();
// Drive until the region parks on await_any: parent in Running, child in
// Waiting.
while (fsm.pump() > 0) {
}
const std::vector<std::string> expected{"Running", "Waiting"};
Void::Testing::expect(fsm.state_path() == expected);
// Release the region and run to completion.
fsm.post(Tick{});
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"ticked"});
// Region gone → path collapses to the (final) parent state only.
Void::Testing::expect_eq(fsm.state_path().size(), std::size_t{1});
}