From 2903eca3a6a178d62b90f4873b4786871fad93fd Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 15 Jun 2026 02:42:35 +0300 Subject: [PATCH] =?UTF-8?q?FSM=20(7):=20history=20states=20=E2=80=94=20run?= =?UTF-8?q?=5Fregion(Resume::History)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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(); // Initial (default) co_await run_region(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 --- include/Void/Fsm/Machine.hpp | 34 +++++++++--- tests/standalone/Fsm/History.cpp | 90 ++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 tests/standalone/Fsm/History.cpp diff --git a/include/Void/Fsm/Machine.hpp b/include/Void/Fsm/Machine.hpp index ac4140a..99b269c 100644 --- a/include/Void/Fsm/Machine.hpp +++ b/include/Void/Fsm/Machine.hpp @@ -63,6 +63,10 @@ namespace Void::Fsm bool own_worker = true; }; + /// How a run_region() 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(); /// co_return r ? next() : next(); - template - [[nodiscard]] auto run_region(Args&&... args) const + template + [[nodiscard]] auto + run_region(Resume resume = Resume::Initial) const { - return m_machine->template run_region( - std::forward(args)...); + return m_machine->template run_region(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 + template [[nodiscard]] Coro::Task> - run_region(Args&&... args) + run_region(Resume resume = Resume::Initial) { - Sub child{std::forward(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(); } @@ -1379,6 +1395,10 @@ namespace Void::Fsm // state_path(). Set/cleared on the consumer thread. std::function()> m_region_probe; + // Per-region-type snapshot of the leaf last reached, for + // Resume::History re-entry. + std::unordered_map m_region_history; + // Set by a Launch step's sub-machine result; consumed by the next // state via the bound dispatcher below. std::optional m_pending_result; diff --git a/tests/standalone/Fsm/History.cpp b/tests/standalone/Fsm/History.cpp new file mode 100644 index 0000000..ab38188 --- /dev/null +++ b/tests/standalone/Fsm/History.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using Void::Coro::Task; +using Void::Fsm::Resume; +using Void::Fsm::Step; +namespace Fsm = Void::Fsm; + +// run_region(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 + { + public: + + using Success = int; + + struct A : State { + Task run() + { + ++g_a_runs; + co_return next(); + } + }; + struct B : State { + Task run() { co_return succeed(2); } + }; + using States = Fsm::States; + }; + + class Host : public Fsm::Machine + { + public: + + using Success = int; + + struct Run : State { + Task run() + { + co_await run_region(); // Initial: A then B + co_await run_region( + Resume::History); // History: B only + co_return succeed(g_a_runs); + } + }; + using States = Fsm::States; + }; +} // 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

{ + using Success = int; + struct Run : State { + Task run() + { + co_await run_region(); + co_await run_region(); // Initial again + co_return succeed(g_a_runs); + } + }; + using States = Fsm::States; + }; + 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 +}