feat(lifecycle): add stage 4 authoritative unbind and durable task routing
Unify broker-mode binding changes in database transactions: advance device generation, invalidate pending authorizations/tasks and prior pairing-code epochs, and persist audit plus invalidation outbox. Add authenticated device unbind alongside robot/admin paths. Pairing codes are hashed, generation- fenced and consumed atomically with binding. Replay Redis invalidation effects without undoing committed unbind. Guard online cleanup and delayed kill events by generation. Introduce a separate router admission credential with no platform-event injection authority. Router verifies signed nonce-bound internal admission on handshake, task traffic and periodic checks; reject stale sessions and control-plane outages. Persist task owner/device/generation/session/worker and original message mapping. Sign routing metadata, verify late results, claim replies once and mark ambiguous/crashed tasks unknown instead of replaying writes. Fence notifications across same-user rebind. Release in-memory slots/waiters even when database or Redis cleanup fails. Validation: 213 passed, 3 skipped (real PostgreSQL not configured), 30 existing uvicorn/websockets deprecation warnings. New lifecycle coverage: 28 tests, including real loopback WebSocket isolation, reverse completion, missed kill, periodic unbind checks, outbox recovery, transaction rollback and cleanup failures. Syntax, docs and staged diff checks pass. Default legacy mode remains unchanged. Broker mode now requires coordinated router admission configuration and a v2 client handshake. No production ECS configuration, real credentials or client installer was changed. PostgreSQL, real Redis multi-node, ARM64 and actual Work/client rollout remain stage 5.
This commit is contained in:
@@ -32,6 +32,7 @@ class Postgres:
|
||||
|
||||
class Store:
|
||||
def __init__(self, path, database_url=""):
|
||||
self.lifecycle = None
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
schema = """
|
||||
CREATE TABLE IF NOT EXISTS devices(
|
||||
@@ -71,6 +72,8 @@ class Store:
|
||||
self.db.execute("INSERT INTO devices(host_id,cert,fingerprint) VALUES(?,?,?)", (host, cert, fp))
|
||||
|
||||
def bind(self, host, owner):
|
||||
if self.lifecycle:
|
||||
return self.lifecycle.bind(host, owner)
|
||||
with self.db:
|
||||
# Conditional update also protects against two connectors racing for the same device.
|
||||
count = self.db.execute("UPDATE devices SET owner=?,binding_generation=binding_generation+1 WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount
|
||||
@@ -78,10 +81,14 @@ class Store:
|
||||
raise ValueError("device unavailable or already bound")
|
||||
|
||||
def unbind(self, owner):
|
||||
if self.lifecycle:
|
||||
return self.lifecycle.unbind(owner)
|
||||
with self.db:
|
||||
self.db.execute("UPDATE devices SET owner=NULL,binding_generation=binding_generation+1 WHERE owner=?", (owner,))
|
||||
|
||||
def revoke(self, host):
|
||||
if self.lifecycle:
|
||||
return self.lifecycle.invalidate_host(host, "revoke")
|
||||
with self.db:
|
||||
self.db.execute("UPDATE devices SET revoked=1,binding_generation=binding_generation+1 WHERE host_id=? AND revoked=0", (host,))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user