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
15 changes: 15 additions & 0 deletions tools/bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Nothing here is ever built by CI, released, or flashed to a customer unit.
| `tp1_sim.py` | A simulated unit for the script's dry run (`--sim`). |
| `find_unit.py` | Finds the bench unit's current address by its MAC, confirmed by the unit itself. |
| `open_guard.py` | Runs in the background until the stroke is learned: stops any open that passes 97 mm (100.5 mm with `--trimmed-carriage`) without S4, or that starts with no S5 reference. |
| `bench_session.py` | Starts and ends a sitting: records the carriage and the window, turns Child Lock on, restores it at the end. |
| `bench_common.py`, `bench_prompt.py` | The guard heartbeat and move gate, and the prompt window with speech, used by the scripts above. |

The design and the expected results are in
[`../regression-gate/predictions/2026-11-tp1-automatic.md`](../regression-gate/predictions/2026-11-tp1-automatic.md).
Expand Down Expand Up @@ -50,6 +52,19 @@ Dry run without a unit: `python tools/bench/tp1_run.py --sim`

## Every sitting

1. Confirm with Daniel which carriage is fitted, then start the sitting:
`python tools/bench/bench_session.py start --host <unit address> --mac <MAC> --window-end HH:MM --carriage old|trimmed`.
It records the sitting and turns **Child Lock** on (Button 1 can't start a move by accident);
`tp1_run.py` turns it off just before its Button 1 step and on again right after.
2. Start the open guard and leave it running for the whole sitting (below). Every script that
moves the unit checks the guard's heartbeat first: no move is sent unless the guard is
connected and watching, and if it drops mid-move the script sends STOP. If the unit reboots
(a flash, a power cut) the guard reconnects by itself and checks the MAC again.
3. Prompts that ask Daniel to act, and the guard's alerts, open in a window on top of the screen
and are spoken (Windows' built-in voice); the power-cut countdown is spoken.
4. At the end: `python tools/bench/bench_session.py end --host <unit address> --mac <MAC>`
restores Daniel's Child Lock setting.

- At the start, confirm with Daniel which carriage is fitted. Pass `--trimmed-carriage` to
`open_guard.py` only when the trimmed carriage is in (its guard limit is 100.5 mm; the default,
97 mm, is for the old carriage). The guard's record names the carriage in its first row.
Expand Down
160 changes: 160 additions & 0 deletions tools/bench/bench_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
"""Shared bench pieces — BENCH ONLY (tools/regression-gate/predictions/2026-11-bench-guard-prompts.md).

- The open guard's heartbeat: open_guard.py writes out/guard-heartbeat.json about twice a second
(time, connected, watching). guard_ok() reads it.
- MoveGate: no move (Open, Close, Set position, Auto-Calibrate, a bench press that starts motion)
is sent unless the heartbeat is under 2 s old and says connected and watching; while a move is
under way every wait checks it, and if it goes stale or says disconnected the script sends STOP
itself.
- Prompter: prompts that ask Daniel to act, and alerts, open in a window that stays on top of his
screen (bench_prompt.py) and are spoken by Windows' built-in voice. Dry runs record them instead.
- The sitting record (out/sitting.json), written by bench_session.py.
"""
import asyncio, json, os, pathlib, subprocess, sys, time

OUT = pathlib.Path(__file__).parent / "out"
HEARTBEAT = OUT / "guard-heartbeat.json"
SITTING = OUT / "sitting.json"
MAX_AGE_S = 2.0
MOVING = ("OPENING", "CLOSING")
MOVE_PRESSES = ("Auto-Calibrate", "BENCH: fire scheduled open", "BENCH: fire scheduled close",
"BENCH: simulate stall")


# ----------------------------------------------------------------------------- heartbeat
def write_heartbeat(connected, watching, carriage, path=HEARTBEAT):
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(".tmp")
tmp.write_text(json.dumps({"t": time.time(), "connected": bool(connected), "watching": bool(watching),
"carriage": carriage, "pid": os.getpid()}), encoding="utf-8")
os.replace(tmp, path)


def guard_ok(path=HEARTBEAT, max_age=MAX_AGE_S, now=time.time):
"""(ok, reason) from the open guard's heartbeat file."""
try:
hb = json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError):
return False, "no heartbeat from the open guard (is open_guard.py running?)"
age = now() - hb.get("t", 0)
if age > max_age:
return False, f"the open guard's heartbeat is {age:.1f} s old"
if not hb.get("connected"):
return False, "the open guard has lost its connection to the unit"
if not hb.get("watching"):
return False, "the open guard isn't watching"
return True, "open guard connected and watching"


class GuardMissing(Exception):
"""A move was not sent: the guard isn't connected and watching."""


class GuardLost(Exception):
"""The guard dropped mid-move: STOP was sent."""


class MoveGate:
"""Wraps a device (tp1_run.RealDevice signature: cover(path, cmd, pos)). check() returns
(ok, reason); the dry runs pass their own."""

def __init__(self, dev, check=guard_ok, on_event=None):
self._dev, self._check, self._on_event = dev, check, on_event or (lambda *a: None)

def __getattr__(self, name):
return getattr(self._dev, name)

def _require(self, what):
ok, why = self._check()
if not ok:
self._on_event("refused", f"{what} not sent: {why}")
raise GuardMissing(f"{what} not sent: {why}")

async def cover(self, path, cmd, pos=None):
if cmd in ("open", "close", "set"):
self._require(f"cover {cmd}")
return await self._dev.cover(path, cmd, pos)

async def press(self, name):
if name in MOVE_PRESSES:
self._require(f"press '{name}'")
return await self._dev.press(name)

async def sleep(self, s):
end = self._dev.now() + s
while True:
if self._dev.get("State") in MOVING:
ok, why = self._check()
if not ok:
await self._dev.cover("api", "stop")
self._on_event("stop", f"STOP sent mid-move: {why}")
raise GuardLost(f"STOP sent mid-move: {why}")
rest = end - self._dev.now()
if rest <= 0:
return
await self._dev.sleep(min(rest, 0.5))


# ----------------------------------------------------------------------------- prompts
class Prompter:
"""act(): a prompt asking Daniel to do something; alert(): something he must notice;
info(): a status line. Shown on top of his screen and spoken, or only recorded (dry runs)."""

def __init__(self, record_only=False, title="Glasscalibur bench"):
self.record_only, self.title, self.sent, self._proc = record_only, title, [], None

def _ui(self):
if self.record_only:
return None
if self._proc is None or self._proc.poll() is not None:
helper = pathlib.Path(__file__).with_name("bench_prompt.py")
try:
self._proc = subprocess.Popen([sys.executable, str(helper), self.title], stdin=subprocess.PIPE,
text=True, encoding="utf-8")
except OSError:
self._proc = None
return self._proc

def _send(self, kind, text, say):
self.sent.append((kind, text, say))
print(f">>> [{kind}] {text}", flush=True)
p = self._ui()
if p and p.stdin:
try:
p.stdin.write(json.dumps({"kind": kind, "text": text, "say": say}) + "\n")
p.stdin.flush()
except OSError:
pass

def act(self, text, say=None):
self._send("act", text, say if say is not None else text)

def alert(self, text, say=None):
self._send("alert", text, say if say is not None else text)

def info(self, text):
self._send("info", text, None)

async def countdown(self, sleep, call, n=3):
"""Spoken countdown, about a second apart, ending with the call (returns at the call)."""
words = {3: "three", 2: "two", 1: "one"}
for k in range(n, 0, -1):
self._send("act", f"{k}...", words.get(k, str(k)))
await sleep(1)
self._send("act", call, call)

def close(self):
if self._proc and self._proc.poll() is None:
try:
self._proc.stdin.close()
except OSError:
pass


# ----------------------------------------------------------------------------- the sitting
def read_sitting(path=SITTING):
try:
s = json.loads(path.read_text(encoding="utf-8"))
return s if not s.get("ended") else None
except (OSError, ValueError):
return None
68 changes: 68 additions & 0 deletions tools/bench/bench_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Bench prompt window — BENCH ONLY. Started by bench_common.Prompter; reads JSON lines on stdin
({"kind": "act"|"alert"|"info", "text": ..., "say": ...}), shows the latest in a window that stays
on top of the screen, and speaks "say" with Windows' built-in voice (System.Speech via PowerShell).
Closes when stdin closes."""
import json, queue, subprocess, sys, threading

COLOURS = {"act": ("#b00020", "white"), "alert": ("#e65100", "white"), "info": ("#1b5e20", "white")}
SPEAK = ("Add-Type -AssemblyName System.Speech; $s = New-Object System.Speech.Synthesis.SpeechSynthesizer; "
"$s.Rate = 1; while (($l = [Console]::In.ReadLine()) -ne $null) { if ($l) { $s.Speak($l) } }")


def main():
import tkinter as tk
q = queue.Queue()
voice = None
try:
voice = subprocess.Popen(["powershell", "-NoProfile", "-Command", SPEAK], stdin=subprocess.PIPE,
text=True, encoding="utf-8")
except OSError:
pass

def reader():
for line in sys.stdin:
try:
q.put(json.loads(line))
except ValueError:
pass
q.put(None)

threading.Thread(target=reader, daemon=True).start()
root = tk.Tk()
root.title(sys.argv[1] if len(sys.argv) > 1 else "Bench")
root.attributes("-topmost", True)
root.geometry("900x260+40+40")
label = tk.Label(root, text="Bench prompts appear here", font=("Segoe UI", 28, "bold"),
wraplength=860, justify="center", bg="#263238", fg="white")
label.pack(fill="both", expand=True)

def poll():
try:
while True:
m = q.get_nowait()
if m is None:
root.destroy()
return
bg, fg = COLOURS.get(m.get("kind"), ("#263238", "white"))
label.config(text=m.get("text", ""), bg=bg, fg=fg)
root.deiconify()
root.lift()
root.attributes("-topmost", True)
if m.get("say") and voice and voice.stdin:
try:
voice.stdin.write(m["say"].replace("\n", " ") + "\n")
voice.stdin.flush()
except OSError:
pass
except queue.Empty:
pass
root.after(100, poll)

root.after(100, poll)
root.mainloop()
if voice and voice.stdin:
voice.stdin.close()


if __name__ == "__main__":
main()
79 changes: 79 additions & 0 deletions tools/bench/bench_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
"""A bench sitting — BENCH ONLY (tools/regression-gate/predictions/2026-11-bench-guard-prompts.md).

python tools/bench/bench_session.py start --host <unit address> --mac <MAC> --window-end HH:MM --carriage old|trimmed
python tools/bench/bench_session.py end --host <unit address> --mac <MAC>

start: records the sitting in tools/bench/out/sitting.json (the carriage, as confirmed with Daniel at
the start; the window end; Child Lock's current setting) and turns Child Lock on, so Button 1 can't
start a move by accident. Scripts that need Button 1 (TP-1's lockout press) turn it off just before
that step and on again after. end: restores Daniel's original Child Lock setting and marks the
sitting ended (the record is kept). Like the other scripts it checks the unit's MAC first and keeps
no address or MAC in the record.
"""
import argparse, asyncio, datetime, json, os, pathlib, sys

sys.path.insert(0, str(pathlib.Path(__file__).parent))
from bench_common import SITTING # noqa: E402

E_CHILD = "Child Lock"


async def set_button_lock(dev, on, settle=2.0):
await dev.switch(E_CHILD, on)
await dev.sleep(settle)
return dev.get(E_CHILD) == on


async def start_sitting(dev, carriage, window_end, path=SITTING):
original = bool(dev.get(E_CHILD))
rec = {"started": datetime.datetime.now().isoformat(timespec="seconds"), "carriage": carriage,
"window_end": window_end, "button_lock_original": original, "ended": None}
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(rec, indent=1), encoding="utf-8")
ok = await set_button_lock(dev, True)
print(f"Sitting started: carriage {carriage}; window ends {window_end}; Child Lock was "
f"{'on' if original else 'off'}, now {'on' if ok else 'NOT on'}.", flush=True)
return ok


async def end_sitting(dev, path=SITTING):
rec = json.loads(path.read_text(encoding="utf-8"))
ok = await set_button_lock(dev, bool(rec["button_lock_original"]))
rec["ended"] = datetime.datetime.now().isoformat(timespec="seconds")
rec["button_lock_restored"] = ok
path.write_text(json.dumps(rec, indent=1), encoding="utf-8")
kept = path.with_name(f"sitting-{rec['started'].replace(':', '')}.json")
os.replace(path, kept)
print(f"Sitting ended: Child Lock {'restored' if ok else 'NOT restored'} to "
f"{'on' if rec['button_lock_original'] else 'off'}; record kept as {kept.name}.", flush=True)
return ok


async def main():
from tp1_run import RealDevice, norm_mac
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("action", choices=["start", "end"])
ap.add_argument("--host", required=True)
ap.add_argument("--mac", required=True)
ap.add_argument("--window-end")
ap.add_argument("--carriage", choices=["old", "trimmed"])
a = ap.parse_args()
if a.action == "start" and not (a.window_end and a.carriage):
ap.error("start needs --window-end and --carriage (confirm the carriage with Daniel first)")
dev = RealDevice(a.host)
await dev.start()
if len(norm_mac(a.mac)) != 12 or norm_mac(dev.mac) != norm_mac(a.mac):
print("STOP: the unit at this address does not report the requested MAC - nothing changed.")
await dev.close()
return 2
await dev.sleep(3)
try:
ok = await (start_sitting(dev, a.carriage, a.window_end) if a.action == "start" else end_sitting(dev))
finally:
await dev.close()
return 0 if ok else 1


if __name__ == "__main__":
sys.exit(asyncio.run(main()))
Loading
Loading