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:
@@ -21,6 +21,7 @@ class Notification(BaseModel):
|
||||
proof: dict
|
||||
request_id: str = Field(pattern=r'^[A-Za-z0-9_-]{8,100}$')
|
||||
binding_owner: str = Field(min_length=1, max_length=128)
|
||||
binding_generation: int | None = Field(default=None,strict=True,ge=0)
|
||||
kind: str = Field(pattern=r'^(text|notice|authorization)$')
|
||||
title: str = Field(default='', max_length=80)
|
||||
text: str = Field(default='', max_length=2000)
|
||||
@@ -41,10 +42,16 @@ def router(settings, redis, store, platform, oauth):
|
||||
if not settings.feishu_enabled:
|
||||
raise HTTPException(503, 'platform disabled')
|
||||
payload = body.model_dump(exclude={'host_id', 'proof'})
|
||||
if body.binding_generation is None:
|
||||
payload.pop('binding_generation') # Preserve exact legacy proof contract.
|
||||
owner = await device_owner(settings, redis, store, body.host_id, body.proof,
|
||||
'work-notify-v1', payload)
|
||||
if body.binding_owner != owner:
|
||||
raise HTTPException(403, 'local binding is stale; pair the client again')
|
||||
if settings.credential_mode == 'broker':
|
||||
current = store.device(body.host_id)
|
||||
if body.binding_generation is None or current['binding_generation'] != body.binding_generation:
|
||||
raise HTTPException(403,'stale_binding')
|
||||
digest = hashlib.sha256(canonical(payload)).hexdigest()
|
||||
previous = store.db.execute('SELECT * FROM notifications WHERE host_id=? AND request_id=?',
|
||||
(body.host_id, body.request_id)).fetchone()
|
||||
@@ -106,7 +113,8 @@ def router(settings, redis, store, platform, oauth):
|
||||
# Recheck the transaction and generation after waiting on app credentials.
|
||||
oauth.authorization_url_for_notification(owner, body.host_id, body.oauth_state)
|
||||
device = store.device(body.host_id)
|
||||
if not device or device['revoked'] or device['owner'] != owner:
|
||||
if (not device or device['revoked'] or device['owner'] != owner
|
||||
or (settings.credential_mode == 'broker' and device['binding_generation'] != body.binding_generation)):
|
||||
status = 'binding_changed'
|
||||
else:
|
||||
delivery_id = uuid.uuid5(uuid.NAMESPACE_URL, body.host_id + ':' + body.request_id).hex
|
||||
|
||||
Reference in New Issue
Block a user