FSM (3): direct control — fluent events, free jumps, lifecycle, mutation

Adds imperative ergonomics for driving and reshaping a machine. The engine
isn't preemptible mid-state (coroutines aren't interruptible), so external
controls apply at transition boundaries and mutation is Idle-only — both
documented.

- Fluent events: send(e1, e2, …) variadic post.
- Free runtime jumps: in-state goto_state<S>() reaches any registered state
  with no declared transition ("each state can be moved to another state");
  external jump<S>() requests a redirect applied by drive() at the next
  boundary (race-safe via an atomic, non-preemptive).
- Lifecycle: reset() returns a finished/idle machine to Idle (drops the
  owned ctx, clears outcome/current/jump); restart() / restart_at<S>()
  re-run; pause() / resume() (ExecutionContext gains a paused flag — the
  worker stops draining and pump() no-ops while paused; posted events
  buffer until resume()).
- Runtime mutation (Idle only): remove<S>() (re-packs indices,
  fixes the initial), set_initial<S>(), replace<S, T>().

8 new tests (Direct): send batch, goto_state skip, boundary jump (embedder
mode), restart / restart_at, remove + set_initial, replace, pause holds
events until resume. 995 green, ASan+UBSan clean, 0 flaky in 15 runs,
-Werror clean. Completes the FSM refactor plan (1-3).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 15:44:19 +03:00
1 parent b34226369a
commit 7c7c590d79
5 files changed
+458 -4

No files matched your search

+9
View File
@@ -132,8 +132,16 @@ namespace Void::Fsm::Detail
/// Embedder-driven step: deliver queued events + run ready
/// coroutines. Returns work done count. Single consumer only.
/// A no-op (returns 0) while paused.
std::size_t pump();
/// Pause / resume event processing. While paused the worker stops
/// draining (and pump() is a no-op); posted events buffer.
void set_paused(bool paused);
/// Wake the consumer (e.g. to re-check an external jump request).
void nudge() { wake(); }
private:
void deliver(EventEnvelope& env);
@@ -180,6 +188,7 @@ namespace Void::Fsm::Detail
std::counting_semaphore<> m_wakeup{0};
std::atomic<bool> m_sleeping{false};
std::atomic<bool> m_done{false};
std::atomic<bool> m_paused{false};
std::atomic<bool> m_worker_started{false};
std::once_flag m_worker_once;
std::thread m_worker;