From 8c97e4269c51c0541154129164c9923d3fa5dce9 Mon Sep 17 00:00:00 2001 From: rockywuest Date: Mon, 26 Oct 2026 13:02:03 +0100 Subject: [PATCH 1/2] fix(behavior): the dog never sets off on patrol by itself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A bored or curious mood started a patrol unattended — in the field with the charging cables still plugged in — and /behavior/stop held only until the next bridge restart. Mood-driven patrol is now opt-in (NOX_AUTO_PATROL=1); POST /behavior/start {"state": "patrol"} still walks. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++++-- body/nox.env.example | 4 ++++ body/nox_behavior_engine.py | 10 ++++++++-- tests/test_behavior_user_wins.py | 33 ++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3a134f5..c68ffe5 100644 --- a/README.md +++ b/README.md @@ -223,6 +223,7 @@ Every key is documented with examples in the file itself. | `NOX_NO_AUTO` | `1` disables autonomous behavior at boot | autonomous on | | `WALK_SPEED` | Gait speed (forward/backward/turns), 1–100 | `95` | | `BEHAVIOR_USER_HOLD_S` | Seconds autonomy pauses after your movement command (voice/MCP/API) | `60` | +| `NOX_AUTO_PATROL` | `1` lets a bored/curious dog start patrolling on its own | off: patrol only via `/behavior/start` | **Brain — `/etc/default/nox-brain`** (restart: `sudo systemctl restart nox-brain`; verify: `journalctl -u nox-brain | grep Language`) @@ -330,8 +331,9 @@ sudo systemctl disable nox-body nox-bridge nox-voice sudo systemctl enable --now nox-body nox-bridge nox-voice ``` -Inside Nox mode, the **autonomous behavior engine** (idle / patrol / play) starts -with the bridge. To keep the dog still unless you command it: +Inside Nox mode, the **autonomous behavior engine** (idle / play / rest) starts +with the bridge. It never walks off on its own: patrol starts only on +`POST /behavior/start {"state": "patrol"}` (or with `NOX_AUTO_PATROL=1`). To keep the dog still unless you command it: | Scope | How | |---|---| diff --git a/body/nox.env.example b/body/nox.env.example index a293444..fb9293f 100644 --- a/body/nox.env.example +++ b/body/nox.env.example @@ -78,6 +78,10 @@ CLAWDBOT_PORT=18789 # behavior (patrol, play, tail wags) pauses this many seconds so it does not # undo your command. 0 = never pause: #BEHAVIOR_USER_HOLD_S=60 +# The dog never sets off on a patrol by itself; only +# POST /behavior/start {"state": "patrol"} walks. 1 = a bored or curious dog +# patrols on its own again (only with the cables off and someone watching): +#NOX_AUTO_PATROL=1 # Seconds the dog keeps listening without the wake word after an exchange # (default 45; 0 = wake word required every time — recommended with # NOX_MODE=commands on the brain): diff --git a/body/nox_behavior_engine.py b/body/nox_behavior_engine.py index 81a646d..ffe2b4d 100644 --- a/body/nox_behavior_engine.py +++ b/body/nox_behavior_engine.py @@ -66,6 +66,11 @@ TOUCH_WAG_COOLDOWN_S = 10.0 # a noisy touch pad must not wag non-stop BLIND_PATROL_TICKS = 2 # ticks without a distance reading → stop patrol BLIND_PATROL_PAUSE_S = 300 # then no mood-driven patrol for this long +# Walking around unattended is opt-in. It used to be on: a bored dog set off +# by itself, cables still plugged in, and /behavior/stop held only until the +# next restart. Now only an explicit /behavior/start {"state": "patrol"} +# walks; NOX_AUTO_PATROL=1 brings back the bored/curious patrol. +AUTO_PATROL = env_int("NOX_AUTO_PATROL", 0) > 0 BATTERY_ABSENT_V = 1.0 # at or below this the rail carries no pack BATTERY_ASSUMED_V = 8.4 # stand-in charge level when the real one is unknown @@ -433,7 +438,7 @@ def _internal_send(cmd): # External control self._forced_state = None # Set by API to force a state - self._patrol_enabled = True + self._patrol_enabled = AUTO_PATROL # mood may start a patrol self._user_hold_until = 0.0 self._last_touch_wag = 0.0 self._blind_ticks = 0 @@ -442,7 +447,8 @@ def _internal_send(cmd): # ─── State Management ───────────────────────────────────────────────── def _request_transition(self, desired): """Request a state transition with guard logic.""" - if desired == BehaviorState.PATROL and time.time() < self._patrol_blocked_until: + if desired == BehaviorState.PATROL and ( + not self._patrol_enabled or time.time() < self._patrol_blocked_until): return if desired == self.state: self._candidate_state = None diff --git a/tests/test_behavior_user_wins.py b/tests/test_behavior_user_wins.py index 7348db2..0a18720 100644 --- a/tests/test_behavior_user_wins.py +++ b/tests/test_behavior_user_wins.py @@ -195,3 +195,36 @@ class Legacy: running = True monkeypatch.setattr(bridge, "_behavior_engine", Legacy()) bridge.note_user_command("/action ['lie']") # must not raise + + +def test_a_bored_dog_does_not_set_off_by_itself(): + """Field report: the dog started patrolling unattended, cables still + plugged in. Mood-driven patrol is opt-in (NOX_AUTO_PATROL).""" + e, rec = engine(forward=120.0) + assert e._patrol_enabled is be.AUTO_PATROL is False + e._state_entered_at = 0 + for _ in range(3): + e._request_transition(be.BehaviorState.PATROL) + e.mood.boredom = 1.0 + e._last_behavior_time = 0 + e._tick_idle(10_000) + assert e.state != be.BehaviorState.PATROL + assert "forward" not in rec.moves() + + +def test_an_explicit_request_still_patrols(): + e, rec = engine(forward=120.0) + assert e.force_state("patrol")["ok"] + assert e.state == be.BehaviorState.PATROL + rec.cmds.clear() + e._tick_patrol() + assert "forward" in rec.moves() + + +def test_auto_patrol_can_be_turned_back_on(monkeypatch): + monkeypatch.setattr(be, "AUTO_PATROL", True) + e, _ = engine(forward=120.0) + e._state_entered_at = 0 + for _ in range(3): + e._request_transition(be.BehaviorState.PATROL) + assert e.state == be.BehaviorState.PATROL From 5e555b980491df65786c322f965fcbead8c76ca6 Mon Sep 17 00:00:00 2001 From: rockywuest Date: Mon, 26 Oct 2026 13:05:15 +0100 Subject: [PATCH 2/2] fix(behavior): a patrol that ended stays ended The main loop restored a forced patrol two seconds after anything took the dog out of it (stuck, touched, a person seen), so one request meant walking indefinitely. Forced rest/idle are still restored. Docs follow (review). Co-Authored-By: Claude Opus 5.5 --- README.md | 5 +++-- body/nox.env.example | 4 ++-- body/nox_behavior_engine.py | 18 ++++++++++++++---- tests/test_behavior_user_wins.py | 21 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c68ffe5..297d90e 100644 --- a/README.md +++ b/README.md @@ -331,9 +331,10 @@ sudo systemctl disable nox-body nox-bridge nox-voice sudo systemctl enable --now nox-body nox-bridge nox-voice ``` -Inside Nox mode, the **autonomous behavior engine** (idle / play / rest) starts +Inside Nox mode, the **autonomous behavior engine** (idle / play / rest / investigate) starts with the bridge. It never walks off on its own: patrol starts only on -`POST /behavior/start {"state": "patrol"}` (or with `NOX_AUTO_PATROL=1`). To keep the dog still unless you command it: +`POST /behavior/start {"state": "patrol"}` (or with `NOX_AUTO_PATROL=1`), and +once it ends (stuck, touched, a person seen, a stop) it stays ended. To keep the dog still unless you command it: | Scope | How | |---|---| diff --git a/body/nox.env.example b/body/nox.env.example index fb9293f..e610c75 100644 --- a/body/nox.env.example +++ b/body/nox.env.example @@ -15,8 +15,8 @@ CLAWDBOT_HOST=192.168.1.100 CLAWDBOT_PORT=18789 # # ─── Autonomous behavior ─── -# The bridge starts Nox's behavior engine at boot: the dog idles, patrols and -# plays on its own. Uncomment to disable it — the dog then only moves on your +# The bridge starts Nox's behavior engine at boot: the dog idles, plays and +# rests on its own (it patrols only on request, see NOX_AUTO_PATROL). Uncomment to disable it — the dog then only moves on your # commands (it can still be started on demand: POST /behavior/start). # The LEDs keep breathing either way: the daemon uses them as a status light. # Apply with: sudo systemctl restart nox-bridge diff --git a/body/nox_behavior_engine.py b/body/nox_behavior_engine.py index ffe2b4d..37e0f0b 100644 --- a/body/nox_behavior_engine.py +++ b/body/nox_behavior_engine.py @@ -725,16 +725,26 @@ def _face_check_loop(self): time.sleep(120) # ─── Main Behavior Loop ─────────────────────────────────────────────── + def _honor_forced_state(self): + """Put back a state the API asked for. Not a patrol, though: once the + dog is out of it — stuck, touched, saw someone — the walk is over, and + restoring it two seconds later meant walking without a new request.""" + forced = self._forced_state + if not forced or self.state == forced: + return + self._forced_state = None + if forced == BehaviorState.PATROL: + print("[behavior] Patrol ended — not resuming without a new request", flush=True) + return + self._do_transition(forced) + def _main_loop(self): """Main FSM tick loop — runs every 2 seconds.""" while self.running: try: now = time.time() - # Honor forced state from API - if self._forced_state and self.state != self._forced_state: - self._do_transition(self._forced_state) - self._forced_state = None + self._honor_forced_state() # Low battery → REST if self.low_battery_mode: diff --git a/tests/test_behavior_user_wins.py b/tests/test_behavior_user_wins.py index 0a18720..345b543 100644 --- a/tests/test_behavior_user_wins.py +++ b/tests/test_behavior_user_wins.py @@ -228,3 +228,24 @@ def test_auto_patrol_can_be_turned_back_on(monkeypatch): for _ in range(3): e._request_transition(be.BehaviorState.PATROL) assert e.state == be.BehaviorState.PATROL + + +@pytest.mark.parametrize("leave_to", ["idle", "investigate", "alert"]) +def test_a_finished_patrol_is_not_resumed(leave_to): + """Stuck, touched or a person seen: the patrol ends. The main loop used to + restore the forced patrol two seconds later — a walk nobody asked for.""" + e, rec = engine(forward=120.0) + e.force_state("patrol") + e._do_transition(be.BehaviorState(leave_to)) + rec.cmds.clear() + e._honor_forced_state() + assert e.state == be.BehaviorState(leave_to) and e._forced_state is None + assert "forward" not in rec.moves() + + +def test_a_forced_rest_is_still_restored(): + e, _ = engine() + e.force_state("rest") + e._do_transition(be.BehaviorState.IDLE) + e._honor_forced_state() + assert e.state == be.BehaviorState.REST