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

113 lines
2.7 KiB
C++

#include <Void/Data/Reflect.hpp>
#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::Step;
namespace
{
// A machine that parks at a named state and exposes its data for
// serialize/restore.
class Pipeline : public Void::Fsm::Machine<Pipeline>
{
public:
using Success = int;
struct Data {
int processed = 0;
std::string stage = "init";
};
Data data;
struct Ingest : State {
Task<Step> run() override
{
co_await await_event<int>(); // park here
co_return next<Process>();
}
};
struct Process : State {
Task<Step> run() override
{
auto& m = self();
m.data.processed += 1;
co_return succeed(m.data.processed);
}
};
Pipeline()
: Void::Fsm::Machine<Pipeline>(
MachineOptions{.own_worker = false})
{
initial<Ingest>();
add<Process>();
}
protected:
void on_serialize(Void::Json::Value& out) const override
{
out = Void::Json::to_json(data);
}
void on_restore(const Void::Json::Value& in) override
{
if (auto r = Void::Json::from_json<Data>(in); r.has_value()) {
data = std::move(*r);
}
}
};
} // namespace
Void::Testing::Test fsm_serialize_captures_state_and_data()
{
Pipeline fsm;
fsm.data.processed = 5;
fsm.data.stage = "running";
fsm.start();
fsm.pump(); // enters Ingest, parks on await_event<int>
const auto json = fsm.serialize();
Void::Testing::expect_eq(json["state"].as_string(), std::string{"Ingest"});
Void::Testing::expect_eq(json["data"]["processed"].as_int(),
std::int64_t{5});
Void::Testing::expect_eq(json["data"]["stage"].as_string(),
std::string{"running"});
fsm.stop();
}
Void::Testing::Test fsm_restore_resumes_at_named_state()
{
const auto saved = Void::Json::parse(
R"({"state":"Process","data":{"processed":9,"stage":"mid"}})");
Void::Testing::expect(saved.has_value());
Pipeline fsm;
const auto r = fsm.restore(*saved);
Void::Testing::expect(r.has_value());
// Starts directly at Process (skipping Ingest's event wait), with the
// restored data → succeeds with processed+1.
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 10);
}
Void::Testing::Test fsm_restore_rejects_unknown_state_and_running()
{
const auto bogus = Void::Json::parse(R"({"state":"NoSuch","data":{}})");
Void::Testing::expect(bogus.has_value());
Pipeline fsm;
Void::Testing::expect(!fsm.restore(*bogus).has_value());
fsm.start();
Void::Testing::expect(!fsm.restore(*bogus).has_value()); // running
fsm.stop();
}