Files
Library/tests/standalone/Fsm/Hierarchy.cpp
T
faumarayandClaude Fable 5 ec9e466d7a FSM (4): one signature-inferred State (drops Reactive/Continue bases)
There is now a single state base, `State`. The engine infers each state's
role purely from member signatures via P2996 — no `override`, no
framework-mandated names:

  struct Work : State {                          // entry
    Coro::Task<Step> run() { ... }
    Coro::Task<Step> run(Cfg c) { ... }          // startup-entry (launch data)
  };
  struct Idle : State {                          // reactive
    void transitions(TransitionBuilder& on) { on.event<Go>().to<Run>(); }
  };
  struct After : State {                         // continuation
    Coro::Task<Step> ok(ChildSuccess auto s) { use s.value; }
    Coro::Task<Step> no(ChildFailure auto f) { use f.value; }
  };

- Detail/Roles.hpp: ChildSuccess/ChildFailure concepts over boxes, plus the
  reflection selectors (invoke_entry / invoke_startup / invoke_handler /
  dispatch_result / has_transitions / make_driver). At add<S>() a per-state
  driver thunk is stored (reactive table vs entry, by whether S has a
  TransitionBuilder& member); drive() calls it. startMachine<Sub, Next>(data)
  builds a Launch step carrying a launcher (injects data into Sub's initial
  startup-entry) + a dispatcher (dispatch_result<Next, Sub>) that builds the
  ChildSuccess/Failure box and calls Next's matching handler.
- Removed Reactive/Continue<R> bases, StateKind/kind(), StateBase::run/resume
  (only on_enter/on_exit remain virtual). Reactive states keep the same DSL.
- Toolchain note: template-for + member splicing fails inside a coroutine
  body in this GCC build, so the selectors are non-coroutines returning a
  Task<Step> (plain Step wrapped via ready_task). Discovery is
  invocability-based — no parameter-type reflection (P3096). Probe-verified.

All FSM tests migrated to the nameless style (SubMachine: Continue ->
ChildSuccess/Failure handlers + startup-entry seeding; reactive states drop
the Reactive base/override). 995 tests (992 green, 3 skipped), ASan clean,
-Werror clean; lone intermittent failure remains the pre-existing racy
plugin hot-reload-under-load test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-15 01:20:56 +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()
{
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()
{
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()
{
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});
}