A composite region can resume at the sub-state it last reached instead of
restarting from its initial state (shallow history), reusing the existing
serialize()/restore():
co_await run_region<Work>(); // Initial (default)
co_await run_region<Work>(Resume::History); // resume where it left off
- New Resume{Initial, History} enum. On region exit the engine snapshots the
child's serialize() keyed by region type into the parent
(m_region_history); on a History re-entry it restore()s that snapshot
before driving. restore() runs before attach() (attach marks the child
Running, and restore requires Idle).
- run_region no longer forwards constructor args (children are
default-constructed, like run_parallel); the only prior caller passed
none.
2 new tests (History): a History re-entry resumes at the last leaf (skips
the earlier states); a plain Initial re-entry restarts from the top. 1000
tests (997 green, 3 skipped). History/Hierarchy/Parallel ASan-clean.
Known pre-existing issue (NOT from this change): the Serialize suite leaks
under ASan because two tests start a machine that parks on an event then
stop() without it finishing — the parked coroutine frame is never
destroyed. Confirmed present at the pre-epic baseline (7c7c590); it is an
engine-lifetime issue (machine stopped while parked), orthogonal to the FSM
feature work, and left for a separate fix.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
91 lines
2.2 KiB
C++
91 lines
2.2 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::Resume;
|
|
using Void::Fsm::Step;
|
|
namespace Fsm = Void::Fsm;
|
|
|
|
// run_region<Sub>(Resume::History) re-enters a region at the sub-state it last
|
|
// reached, instead of its initial state (shallow history). The leaf is
|
|
// snapshotted via the existing serialize()/restore().
|
|
|
|
namespace
|
|
{
|
|
int g_a_runs = 0; // counts how often Region's A state runs
|
|
|
|
// A -> B; ends at B. On a History re-entry it should resume at B (skipping
|
|
// A), so g_a_runs increments only on a fresh (Initial) entry.
|
|
class Region : public Fsm::Machine<Region>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct A : State {
|
|
Task<Step> run()
|
|
{
|
|
++g_a_runs;
|
|
co_return next<B>();
|
|
}
|
|
};
|
|
struct B : State {
|
|
Task<Step> run() { co_return succeed(2); }
|
|
};
|
|
using States = Fsm::States<A, B>;
|
|
};
|
|
|
|
class Host : public Fsm::Machine<Host>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct Run : State {
|
|
Task<Step> run()
|
|
{
|
|
co_await run_region<Region>(); // Initial: A then B
|
|
co_await run_region<Region>(
|
|
Resume::History); // History: B only
|
|
co_return succeed(g_a_runs);
|
|
}
|
|
};
|
|
using States = Fsm::States<Run>;
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test history_resumes_at_last_leaf()
|
|
{
|
|
g_a_runs = 0;
|
|
Host fsm;
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
// A ran once (first entry); the History re-entry resumed at B, skipping A.
|
|
Void::Testing::expect_eq(*outcome, 1);
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test initial_reentry_restarts_from_the_top()
|
|
{
|
|
g_a_runs = 0;
|
|
// Two plain (Initial) entries run A both times.
|
|
struct H2 : Fsm::Machine<H2> {
|
|
using Success = int;
|
|
struct Run : State {
|
|
Task<Step> run()
|
|
{
|
|
co_await run_region<Region>();
|
|
co_await run_region<Region>(); // Initial again
|
|
co_return succeed(g_a_runs);
|
|
}
|
|
};
|
|
using States = Fsm::States<Run>;
|
|
};
|
|
H2 fsm;
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 2); // A ran on both Initial entries
|
|
}
|