Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down Expand Up @@ -330,8 +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 / 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 / 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`), 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 |
|---|---|
Expand Down
8 changes: 6 additions & 2 deletions body/nox.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
28 changes: 22 additions & 6 deletions body/nox_behavior_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -719,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:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_behavior_user_wins.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,57 @@ 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


@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
Loading