Skip to content

feat(runtime): /v0/estop software safe-state with actuation latch - #229

Merged
CameronBrooks11 merged 5 commits into
mainfrom
feat/estop-safe-state
Jul 28, 2026
Merged

CameronBrooks11 merged 5 commits into
mainfrom
feat/estop-safe-state

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

Closes #219. Builds on #218 (registry::is_actuating).

Problem

A pure-manual deployment had no software safe-state command — /v0/mode returns UNAVAILABLE when automation is off, and a naïve "run hooks else zero" is unreachable on exactly that machine (mode-transition hooks register only onto the automation-gated mode manager). And a one-shot stop is defeated by the next /v0/call.

Change

A first-class software safe-state that works regardless of automation.enabled, with its own config-driven execution path independent of the mode manager.

  • New top-level safety.safe_state config (hooks, setpoints, zero_is_safe), parsed and validated independently of automation. Schema + valid/invalid contract fixtures included. Hook call-list parsing is factored into a shared parse_call_list (reused by mode-transition hooks — behavior/error strings preserved).
  • SafeStateController owns a thread-safe actuation latch and runs the locked D4 ladder: declared hooks → per-actuator setpoints (only with full coverage of every actuating output, fail-closed) → zero all actuating outputs (only if zero_is_safe) → else honest software_safe_state: "none" and latch anyway (never a false zero).
  • Latch checkpoint in CallRouter::execute_call — the single choke point for HTTP /v0/call, autonomous BT actuation, and hook calls. While latched, actuating calls are refused with 409 (fail-closed via is_actuating); read-only calls pass. A safe_state bypass (never settable from HTTP) lets the ladder itself actuate.
  • POST /v0/estop + POST /v0/estop/clear, and an estop{} block on GET /v0/runtime/status (latched, software_safe_state, latched_at_epoch_ms, uncovered_actuating_functions).
  • On an automation machine, an e-stop also drives FAULT (latch → ladder → FAULT); clear releases the latch but does not exit FAULT. Uniform auth. A software stop is not a substitute for the hardware backplane cut.

Locked design decisions (maintainer)

Full-coverage-or-fail-closed setpoints; latch-anyway with honest "none"; uniform auth; FAULT coupling on automation machines. Rung-3 zeroing drives numeric required args to 0 / bool to false and refuses non-numeric (and — post-review — no-arg / non-ACTUATE functions).

Tests & verification (local)

  • C++ unit: 376/376. CallRouter latch/bypass (incl. the post-lock re-check), and the full SafeStateController ladder (none / hooks / setpoint full-vs-partial coverage / zero / zero-refuses-non-numeric / zero-skips-CONFIG / zero-refuses-no-arg / clear).
  • End-to-end integration: a live runtime+sim suite drives engage → status latched → actuating /v0/call 409 → clear → call succeeds.
  • Contracts: OpenAPI paths + estop status block, examples, and runtime-config safety fixtures; live conformance 19/19.
  • clang-format + clang-tidy + ruff + mypy clean.

Adversarial branch review

An independent reviewer's REVISE findings are all addressed in fix(estop): …: the TOCTOU re-check under the provider lock (F1), narrowing rung-3 to declared actuators + refusing bare-invoke (F2), consistent latch snapshot (F4), setpoint both-selector agreement (F5), optional estop in the status schema (F6), and documenting the trigger-holds-mutex trade-off (F3).

Parse a new top-level `safety:` section, independent of `automation`, so a
pure-manual machine can declare a software safe-state. Carries the estop
ladder inputs: declared safe-state hooks, per-actuator safe setpoints, and a
`zero_is_safe` assertion. Validated unconditionally (device_handle + a
function selector required). Factor the hook call-list parsing into a shared
parse_call_list helper reused by mode-transition hooks and safety.

Part of #219.
Add a first-class software safe-state that works regardless of
automation.enabled. SafeStateController owns a thread-safe actuation latch
and runs the declared safe-state ladder independent of the mode manager:
run declared hooks; else drive per-actuator setpoints (only if they cover
every actuating output); else zero all actuating outputs, but only if the
profile asserts zero_is_safe; else honestly report no software safe-state.

The latch checkpoint lives in CallRouter::execute_call, the single choke
point covering HTTP /v0/call, autonomous BT actuation, and hook calls: while
latched, actuating calls (fail-closed on unclassified functions, via #218's
is_actuating) are refused with 409, except safe-state actions which carry a
bypass flag. On an automation machine an e-stop also drives FAULT.

Expose POST /v0/estop and POST /v0/estop/clear, and an estop{} block on
GET /v0/runtime/status (latched, software_safe_state, latched_at, uncovered
actuating functions). Auth is uniform with the rest of /v0.

Part of #219.
- CallRouter latch tests: actuating call refused while latched, read-only
  allowed, safe_state bypasses the latch and IDLE gating.
- SafeStateController ladder tests: none/hooks/setpoints-full-coverage/
  partial-fail-closed/zero/zero-refuses-non-numeric/clear.
- End-to-end estop integration suite: engage -> status latched -> actuating
  /v0/call 409 -> clear -> call succeeds, against a live runtime + sim.
- OpenAPI: /v0/estop, /v0/estop/clear, and the estop block on
  /v0/runtime/status, with examples and manifest entries.
- runtime-config contract fixtures for the safety section (valid + invalid).
- Docs: http-api, safety, configuration-schema; CHANGELOG.

Part of #219.
- Re-check the actuation latch after acquiring the per-provider lock, closing
  a TOCTOU window where an in-flight actuating call could land after the
  safe-state ladder had run (F1).
- Narrow the ladder's execution set to explicitly-declared CATEGORY_ACTUATE
  functions and refuse to "zero" a function with no numeric required argument,
  so an e-stop never bare-invokes a no-arg command or wipes a CONFIG setting.
  The CallRouter latch keeps its broader fail-closed is_actuating scope (F2).
- Read the latch flag and timestamp together under the mutex so the status
  snapshot is never torn against a concurrent clear (F4).
- Require both selectors to agree in setpoint coverage matching, mirroring
  CallRouter resolution (F5).
- Make the runtime-status estop block optional in the OpenAPI schema, matching
  its conditional emission (F6).
- Document that trigger holds the mutex across the ladder (F3).

Part of #219.
runtime.hpp only declares mode_manager_ when automation is enabled, so the
set_mode_manager() call in init_http broke the no-automation build. Move it
inside the automation guard: without automation there is no mode manager and
the e-stop is latch + ladder only. The safe_state HTTP dependency stays wired
unconditionally.

Part of #219.
@CameronBrooks11
CameronBrooks11 merged commit e35d132 into main Jul 28, 2026
25 checks passed
@CameronBrooks11
CameronBrooks11 deleted the feat/estop-safe-state branch July 28, 2026 13:46
CameronBrooks11 added a commit that referenced this pull request Jul 30, 2026
Ships the safety spine + health-staleness work that has accumulated on main
since v0.1.38:

- #228 surface FunctionPolicy category over /v0 capabilities (is_actuating)
- #229 POST /v0/estop software safe-state + actuation latch
- #230 install.sh --variant selection + verify-inert boot-inert
- #231 refuse-hookless: veto MANUAL->AUTO without safe-state hooks
- #234 cadence-derived device-liveness staleness (D7 phase 1)
- #235 per-signal quality/freshness into health, new FAULT state (D7 phase 2)

Bumps CMakeLists project VERSION + vcpkg.json, rolls CHANGELOG [Unreleased]
into [0.1.39] (adds the missing phase-2 entry), and updates the runtime_version
in the runtime-http contract examples.
CameronBrooks11 added a commit that referenced this pull request Oct 15, 2026
The zero rung of the safe-state ladder (`zero_is_safe: true`) built a
call for every required argument of every ACTUATE function, and
`zero_value(BOOL)` returned `false`. A boolean has no safe pole the
runtime can know: `false` disables an enable flag but releases a brake
flag. So rung 3 could release a brake as part of a stop.

`zero_value(BOOL)` now returns `nullopt`, like string and bytes. Rung 3
skips any function it could only call by guessing a boolean, and reports
it in the ladder's actions as not driven (`success: false`, error
"cannot zero required non-numeric argument '<name>'") instead of sending
it. Numeric arguments are zeroed as before. The `zero_is_safe` entry in
docs/configuration-schema.md now says so.

A skipped call never reaches the CallRouter, and `trigger()` logged only
an INFO summary, so the skip was silent. `trigger()` now logs one WARN
per failed or skipped action, as the re-assert path does:

    [SafeState] E-stop did not drive <device>/<function>: <reason>

A correction to the issue description: rung 3 has run only
`CATEGORY_ACTUATE` functions since #229 (`actuating_functions_for` in
core/control/safe_state.cpp), so it does not zero PID gains or write
any CONFIG function. `ZeroRungSkipsNonActuateFunctions` already pins
that. The boolean pole was the remaining defect.

Tests (tests/unit/safe_state_test.cpp):

- `ZeroRungNeverSendsBoolArgument` registers an ACTUATE function whose
  only required argument is a bool, triggers the e-stop with
  `zero_is_safe`, and expects no provider call and one action reported
  as not driven. On the unfixed code it fails: the StrictMock records
  `call("heater", 1, "set_output", {pwm: bool_value: false})`.
- `TriggerWarnsForEachActionNotDriven` captures the log for the same
  e-stop and expects the WARN line above. Before the logging change it
  fails: the log holds only "E-stop engaged" and the INFO summary.

All 14 `SafeStateTest` cases pass.

Closes #255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add /v0/estop runtime software safe-state command (with actuation latch)

1 participant