feat(tasks): persist encrypted result handoff and recover received tasks

Add signed device-scoped task state/result endpoints and AES-GCM result outbox. Atomically claim original-message replies, reject cross-user/stale/expired tasks, preserve unknown platform receipts without blind replay, and retain ambiguous ciphertext for at most seven days. Maintain legacy synchronous compatibility and clarify stale deployment-plan diagnostics. Validation: ECS suite 227 passed/2 skipped, later outbox guards 12 passed, deployment diagnostics 54 passed. Real ECS/PostgreSQL/platform failure acceptance remains pending.
This commit is contained in:
ex_tianzy
2026-11-05 10:24:55 +08:00
parent db116284b1
commit e4c3a8ad4e
6 changed files with 325 additions and 4 deletions
+10
View File
@@ -161,6 +161,8 @@ class Broker:
"text": text, "read_only": True, "channel": "im",
"reply_key": raw_public(private.public_key())}
if task:
if source_message_id:
payload['result_delivery']='durable-http-v1'
payload["task_context"] = {key:task[key] for key in ("environment","app_id","owner","host_id","binding_generation",
"conversation_id","chat_id","source_message_id")}
payload["task_context"].update(task_id=turn,channel="work-dm")
@@ -230,6 +232,8 @@ def create_app(settings=None, redis=None, platform=None):
store.lifecycle = BindingState(store,s.environment,s.feishu_app_id)
broker.lifecycle = store.lifecycle
lifecycle = DeviceLifecycle(store.lifecycle,r,broker)
from .task_delivery import TaskDelivery
deliveries = TaskDelivery(oauth,store.lifecycle,broker,f)
else:
oauth = OAuth(s, r, f)
oauth.store = store
@@ -249,6 +253,7 @@ def create_app(settings=None, redis=None, platform=None):
await r.ping()
if lifecycle:
await lifecycle.maintain()
await deliveries.recover_interrupted_sends()
for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall():
await r.set("hn:device:" + row["host_id"], row["fingerprint"])
if row["revoked"]:
@@ -269,6 +274,8 @@ def create_app(settings=None, redis=None, platform=None):
await asyncio.sleep(5)
try:
await lifecycle.maintain()
await deliveries.recover_interrupted_sends()
await deliveries.flush()
except Exception:
# No credentials or raw exception text in background logs.
pass
@@ -292,6 +299,9 @@ def create_app(settings=None, redis=None, platform=None):
app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None)
app.add_middleware(BodyLimitMiddleware, max_body=s.max_body)
app.include_router(oauth.router)
if lifecycle:
app.include_router(deliveries.router)
app.state.deliveries=deliveries
app.include_router(notification_router(s, r, store, f, oauth))
from .web_session import WebSessions
web_sessions = WebSessions(s, r, store, broker, f)