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;
|
||||
};
|
||||
|
||||
/// 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.
|
||||
struct Transition {
|
||||
std::string from;
|
||||
@@ -267,11 +271,11 @@ namespace Void::Fsm
|
||||
/// runs. Map the child's Outcome to a sibling transition:
|
||||
/// auto r = co_await run_region<Work>();
|
||||
/// co_return r ? next<Done>() : next<Recover>();
|
||||
template <typename Sub, typename... Args>
|
||||
[[nodiscard]] auto run_region(Args&&... args) const
|
||||
template <typename Sub>
|
||||
[[nodiscard]] auto
|
||||
run_region(Resume resume = Resume::Initial) const
|
||||
{
|
||||
return m_machine->template run_region<Sub>(
|
||||
std::forward<Args>(args)...);
|
||||
return m_machine->template run_region<Sub>(resume);
|
||||
}
|
||||
|
||||
/// Orthogonal regions: run `Subs...` concurrently on the
|
||||
@@ -776,15 +780,27 @@ namespace Void::Fsm
|
||||
/// (parent→child chain). Returns the child's Outcome, which the
|
||||
/// caller maps to a sibling transition. The calling state's
|
||||
/// on_enter/on_exit naturally wrap the region.
|
||||
template <typename Sub, typename... Args>
|
||||
template <typename 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);
|
||||
m_region_probe = [&child] { return child.state_path(); };
|
||||
co_await child.drive();
|
||||
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>();
|
||||
}
|
||||
|
||||
@@ -1379,6 +1395,10 @@ namespace Void::Fsm
|
||||
// state_path(). Set/cleared on the consumer thread.
|
||||
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
|
||||
// state via the bound dispatcher below.
|
||||
std::optional<LaunchResult> m_pending_result;
|
||||
|
||||
Reference in new issue
Block a user