feat(sfm): device_commands audit table -- one row per change attempt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-23 16:22:28 -04:00
1 parent 7ff527df07
commit fb9b48f259
4 files changed
+160

No files matched your search

+92
View File
@@ -34,6 +34,7 @@ Usage
from __future__ import annotations
import datetime
import json
import logging
import sqlite3
import uuid
@@ -126,6 +127,24 @@ CREATE TABLE IF NOT EXISTS monitor_log (
CREATE INDEX IF NOT EXISTS idx_monitor_log_serial ON monitor_log(serial);
CREATE INDEX IF NOT EXISTS idx_monitor_log_start ON monitor_log(start_time);
CREATE INDEX IF NOT EXISTS idx_monitor_log_session ON monitor_log(session_id);
CREATE TABLE IF NOT EXISTS device_commands (
id TEXT PRIMARY KEY,
ts TEXT NOT NULL, -- when the attempt began (UTC ISO)
finished_ts TEXT,
serial TEXT NOT NULL,
series INTEGER NOT NULL,
address TEXT NOT NULL,
action TEXT NOT NULL, -- e.g. "start monitoring"
tier TEXT NOT NULL, -- "change" | "destructive"
request TEXT, -- JSON: parameters + the planned frames (hex)
ack TEXT, -- JSON: ack summary
verified TEXT, -- "ok" | "mismatch" | "not_checked"
outcome TEXT NOT NULL, -- "pending" | "sent" | "skipped" | "failed"
error TEXT,
caller TEXT -- "webapp" | "terra-view" | "script" | ...
);
CREATE INDEX IF NOT EXISTS idx_device_commands_serial ON device_commands(serial, ts);
"""
@@ -372,6 +391,79 @@ class SeismoDb:
).fetchall()
return [dict(r) for r in rows]
# ── Device command audit ──────────────────────────────────────────────────
def begin_device_command(
self,
*,
serial: str,
series: int,
address: str,
action: str,
tier: str,
request: dict,
caller: str,
) -> str:
"""Write a ``pending`` audit row BEFORE anything is sent. Returns its id."""
cid = self._new_id()
ts = datetime.datetime.now(datetime.timezone.utc).isoformat()
with self._connect() as conn:
conn.execute(
"""
INSERT INTO device_commands
(id, ts, serial, series, address, action, tier,
request, outcome, caller)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?)
""",
(cid, ts, serial, series, address, action, tier,
json.dumps(request), caller),
)
return cid
def finish_device_command(
self,
command_id: str,
*,
outcome: str,
verified: Optional[str],
ack: Optional[dict],
error: Optional[str],
) -> None:
"""Record the result of a command begun with begin_device_command()."""
ts = datetime.datetime.now(datetime.timezone.utc).isoformat()
with self._connect() as conn:
conn.execute(
"UPDATE device_commands SET finished_ts=?, outcome=?, verified=?, "
"ack=?, error=? WHERE id=?",
(ts, outcome, verified,
json.dumps(ack) if ack is not None else None,
error, command_id),
)
def query_device_commands(
self,
serial: Optional[str] = None,
limit: int = 100,
) -> list[dict]:
"""Return audit rows, newest first, with request/ack parsed from JSON."""
with self._connect() as conn:
if serial:
rows = conn.execute(
"SELECT * FROM device_commands WHERE serial=? "
"ORDER BY ts DESC LIMIT ?", (serial, limit)).fetchall()
else:
rows = conn.execute(
"SELECT * FROM device_commands ORDER BY ts DESC LIMIT ?",
(limit,)).fetchall()
out = []
for r in rows:
d = dict(r)
for k in ("request", "ack"):
if d[k] is not None:
d[k] = json.loads(d[k])
out.append(d)
return out
# ── Events ────────────────────────────────────────────────────────────────
def insert_events(