feat(gem): multi-observer state-change handlers via HandlerSlot

The single-slot set_*_handler pattern was a structural blocker, hit twice:
the daemon could not observe control-state changes because
register_default_handlers owns the slot, forcing GetControlState to read the
FSM cross-thread (a data race), and blocking WatchHealth and the Subscribe
stream's ControlStateChange variant.

HandlerSlot<Args...> keeps a primary slot with exact legacy semantics
(set_ replaces — one existing test depends on replacement) plus an
append-only observer list (add_) that survives set_ calls. Fire sites are
textually unchanged (operator bool / operator() / assign-from-function).

Applied to ControlStateMachine + ProcessJobStore + ControlJobStore (the
roadmap-critical three; the remaining single-slot classes follow the same
3-line pattern as needed). EquipmentRuntime gains an atomic control-state
mirror registered as an observer — control_state() is now safe from any
thread, retiring the GetControlState race — plus add_control_state_observer
and add_link_observer (selected/closed fan-out), the hooks WatchHealth and
Subscribe need.

Tests: observer ordering, set-replaces-primary-but-observers-survive,
observers-without-primary, PJ-store coexistence, and the runtime scenario
that was previously impossible (mirror + observer + default-handlers set_).
Core 464/464 (2816 assertions), daemon 16/16, live GEM300 demo passes with
single-fire control-state transitions.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-10 18:57:53 +02:00
parent b067a76b80
commit 8a48ffeed4
10 changed files with 192 additions and 7 deletions
+16
View File
@@ -221,3 +221,19 @@ TEST_CASE("Store: set_alert toggles per-PJ alert flag") {
CHECK(store.get("PJ-1")->alert_enabled == false);
CHECK_FALSE(store.set_alert("ghost", false));
}
TEST_CASE("ProcessJobStore: add_state_change_handler observer coexists with primary") {
ProcessJobStore store;
int primary_calls = 0, observer_calls = 0;
store.set_state_change_handler(
[&](const std::string&, ProcessJobState, ProcessJobState,
ProcessJobEvent) { ++primary_calls; });
store.add_state_change_handler(
[&](const std::string&, ProcessJobState, ProcessJobState,
ProcessJobEvent) { ++observer_calls; });
CHECK(store.create("PJ-OBS", "RECIPE-A", {"lot1"}) ==
ProcessJobStore::CreateResult::Created); // NoState -> Queued fires both
CHECK(primary_calls == 1);
CHECK(observer_calls == 1);
}