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 +}