Files
Library/include/Void/Fsm/PluginBehavior.hpp
T
faumarayandClaude Fable 5 c988f635e9 Integrate FSM with Plugin: states/actions from plugins, hot reload
Fsm/PluginBehavior.hpp: IFsmBehaviorPack::register_into(Builder&) lets a
plugin contribute states/events/guards/actions; plugins export it via
extern "C" void_plugin_fsm_behaviors(). build_machine_def(pack) runs the
pack through a Builder and validates.

PluginManager::raw_symbol(name, symbol) resolves an optional extra entry
point from a plugin's current .so (returns nullptr if absent), so the
host can fetch the behaviour factory without widening the frozen IPlugin
vtable.

Integration flow (tests/internal/FsmPluginHotReload.cpp): build a machine
def from the plugin pack, run it; on reload, rebuild from the reloaded
pack and swap_definition between RTC steps. Same state names across
versions ⇒ the live configuration is adopted seamlessly (machine keeps
running, queue intact); v2's new "skip" transition goes live. Safety:
plugin actions are std::functions whose code lives in the .so, so a
PluginHandle is pinned for the machine's lifetime and dlclose_on_swap=
false keeps the code pages mapped under in-flight frames.

Tests: FsmPluginV1/V2 (same idle/busy/done states; v2 adds skip).
Definition-swap test proves configuration survival + new behaviour; the
stress test runs 4 producer threads posting while 5 v1<->v2 rebuild+swaps
happen concurrently, then asserts exactly one active leaf. 967 tests
green, stable across runs, ASan+UBSan clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-13 18:13:03 +03:00

54 lines
1.8 KiB
C++

#pragma once
#include <Void/Fsm/Definition.hpp>
#include <expected>
#include <memory>
namespace Void::Fsm
{
// ── IFsmBehaviorPack ──────────────────────────────────────────────────
// A plugin exposes FSM states/events/guards/actions by implementing
// this interface and returning it from the optional C entry point
//
// extern "C" Void::Fsm::IFsmBehaviorPack* void_plugin_fsm_behaviors();
//
// The host calls register_into(builder) while assembling a machine
// definition, then build()s it. Because the actions/guards the pack
// installs are std::functions whose code lives in the plugin .so, the
// machine (and its MachineDef) must not outlive the plugin: keep a
// Plugin::PluginHandle pinned for the machine's lifetime, and rely on
// PluginManager's dlclose_on_swap=false so the code pages stay mapped
// across reloads. See tests/internal/FsmPluginHotReload.cpp.
//
// The interface is a same-compiler C++ ABI (like IPlugin): the host and
// plugin must be built with the same toolchain, which they are within
// this project.
class IFsmBehaviorPack
{
public:
virtual ~IFsmBehaviorPack() = default;
/// Declare this pack's states, events, and transitions on the
/// builder. Called once per machine-definition build.
virtual void register_into(Builder& builder) = 0;
};
using fsm_behaviors_fn = IFsmBehaviorPack* (*)();
inline constexpr const char* kFsmBehaviorsSymbol =
"void_plugin_fsm_behaviors";
/// Build an immutable machine definition from a behaviour pack.
[[nodiscard]] inline std::expected<std::shared_ptr<const MachineDef>,
DefError>
build_machine_def(IFsmBehaviorPack& pack)
{
Builder builder;
pack.register_into(builder);
return builder.build();
}
} // namespace Void::Fsm