FSM (7): history states — run_region<Sub>(Resume::History)
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>
This commit is contained in:
1 parent
9d6be56847
commit
2903eca3a6
2 files changed
+117
-7
No files matched your search
@@ -63,6 +63,10 @@ namespace Void::Fsm
|
|||||||
bool own_worker = true;
|
bool own_worker = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// How a run_region<Sub>() re-enters: from Sub's initial state, or resume
|
||||||
|
/// at the sub-state it last reached (shallow history).
|
||||||
|
enum class Resume { Initial, History };
|
||||||
|
|
||||||
/// A transition record handed to observers.
|
/// A transition record handed to observers.
|
||||||
struct Transition {
|
struct Transition {
|
||||||
std::string from;
|
std::string from;
|
||||||
@@ -267,11 +271,11 @@ namespace Void::Fsm
|
|||||||
/// runs. Map the child's Outcome to a sibling transition:
|
/// runs. Map the child's Outcome to a sibling transition:
|
||||||
/// auto r = co_await run_region<Work>();
|
/// auto r = co_await run_region<Work>();
|
||||||
/// co_return r ? next<Done>() : next<Recover>();
|
/// co_return r ? next<Done>() : next<Recover>();
|
||||||
template <typename Sub, typename... Args>
|
template <typename Sub>
|
||||||
[[nodiscard]] auto run_region(Args&&... args) const
|
[[nodiscard]] auto
|
||||||
|
run_region(Resume resume = Resume::Initial) const
|
||||||
{
|
{
|
||||||
return m_machine->template run_region<Sub>(
|
return m_machine->template run_region<Sub>(resume);
|
||||||
std::forward<Args>(args)...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Orthogonal regions: run `Subs...` concurrently on the
|
/// Orthogonal regions: run `Subs...` concurrently on the
|
||||||
@@ -776,15 +780,27 @@ namespace Void::Fsm
|
|||||||
/// (parent→child chain). Returns the child's Outcome, which the
|
/// (parent→child chain). Returns the child's Outcome, which the
|
||||||
/// caller maps to a sibling transition. The calling state's
|
/// caller maps to a sibling transition. The calling state's
|
||||||
/// on_enter/on_exit naturally wrap the region.
|
/// on_enter/on_exit naturally wrap the region.
|
||||||
template <typename Sub, typename... Args>
|
template <typename Sub>
|
||||||
[[nodiscard]] Coro::Task<Detail::OutcomeOf<Sub>>
|
[[nodiscard]] Coro::Task<Detail::OutcomeOf<Sub>>
|
||||||
run_region(Args&&... args)
|
run_region(Resume resume = Resume::Initial)
|
||||||
{
|
{
|
||||||
Sub child{std::forward<Args>(args)...};
|
Sub child;
|
||||||
|
// History: resume at the sub-state this region last reached.
|
||||||
|
// restore() must run before attach() (which marks Running).
|
||||||
|
if (resume == Resume::History) {
|
||||||
|
const auto it =
|
||||||
|
m_region_history.find(std::type_index{typeid(Sub)});
|
||||||
|
if (it != m_region_history.end()) {
|
||||||
|
(void)child.restore(it->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
child.attach(m_ctx, m_services);
|
child.attach(m_ctx, m_services);
|
||||||
m_region_probe = [&child] { return child.state_path(); };
|
m_region_probe = [&child] { return child.state_path(); };
|
||||||
co_await child.drive();
|
co_await child.drive();
|
||||||
m_region_probe = nullptr;
|
m_region_probe = nullptr;
|
||||||
|
// Snapshot the leaf it reached for a later History re-entry.
|
||||||
|
m_region_history[std::type_index{typeid(Sub)}] =
|
||||||
|
child.serialize();
|
||||||
co_return child.template take_outcome<Sub>();
|
co_return child.template take_outcome<Sub>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1379,6 +1395,10 @@ namespace Void::Fsm
|
|||||||
// state_path(). Set/cleared on the consumer thread.
|
// state_path(). Set/cleared on the consumer thread.
|
||||||
std::function<std::vector<std::string>()> m_region_probe;
|
std::function<std::vector<std::string>()> m_region_probe;
|
||||||
|
|
||||||
|
// Per-region-type snapshot of the leaf last reached, for
|
||||||
|
// Resume::History re-entry.
|
||||||
|
std::unordered_map<std::type_index, Json::Value> m_region_history;
|
||||||
|
|
||||||
// Set by a Launch step's sub-machine result; consumed by the next
|
// Set by a Launch step's sub-machine result; consumed by the next
|
||||||
// state via the bound dispatcher below.
|
// state via the bound dispatcher below.
|
||||||
std::optional<LaunchResult> m_pending_result;
|
std::optional<LaunchResult> m_pending_result;
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#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
|
||||||
|
}
|
||||||
Reference in new issue
Block a user