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:
ex_tianzy
2026-11-04 11:43:53 +08:00
parent 93bfbca83a
commit d1ab834ecb
13 changed files with 1128 additions and 29 deletions
+12
View File
@@ -44,11 +44,17 @@ class Settings(BaseSettings):
credential_mode: str = "legacy"
credential_keyring_file: Path | None = None
credential_permission_profiles: dict[str, list[str]] = {}
connector_internal_url: str = "http://connector:8000"
router_admission_secret: str = ""
@model_validator(mode="after")
def validate_runtime(self):
if self.credential_mode not in {"legacy", "broker"}:
raise ValueError("invalid credential mode")
if self.credential_mode == "broker" and self.role in {"connector","router"}:
if (len(self.router_admission_secret) < 32 or self.router_admission_secret in {
self.internal_secret,self.admin_secret,self.feishu_app_secret}):
raise ValueError("broker mode requires an independent router admission credential")
for profile, scopes in self.credential_permission_profiles.items():
if (not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", profile) or not scopes or len(scopes) > 128
or any(not re.fullmatch(r"[A-Za-z0-9_:.-]{1,256}", scope) for scope in scopes)):
@@ -63,6 +69,12 @@ class Settings(BaseSettings):
if (authorize.scheme != "https" or not authorize.hostname or authorize.username or authorize.password
or authorize.query or authorize.fragment):
raise ValueError("broker mode requires a fixed HTTPS authorization endpoint")
if self.credential_mode == "broker" and self.role == "router":
target = urlsplit(self.connector_internal_url)
if (not self.feishu_app_id or target.scheme not in {"http","https"}
or not target.hostname or target.username or target.password or target.query or target.fragment
or target.path not in {"","/"}):
raise ValueError("broker router requires authenticated fixed internal connector")
if self.environment not in {"production", "test", "development"}:
raise ValueError("invalid environment")
if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: