feat(work): persist queued tasks before device acceptance

This commit is contained in:
ex_tianzy
2026-11-23 15:49:50 +08:00
parent 1e7307e45d
commit 93739a6089
10 changed files with 294 additions and 19 deletions
+13 -10
View File
@@ -88,7 +88,7 @@ class BindingState:
(self.environment, self.app_id, device["host_id"], generation))
self.db.execute("""UPDATE work_tasks_v2 SET status=?,updated_at=?
WHERE environment=? AND app_id=? AND host_id=? AND binding_generation<=?
AND status IN ('inflight','completed','replying','delivery_ready')""",
AND status IN ('queued','inflight','completed','replying','delivery_ready')""",
("revoked" if reason == "revoke" else "unbound", time.time(), self.environment,
self.app_id, device["host_id"], generation))
event = uuid.uuid4().hex
@@ -142,17 +142,20 @@ class BindingState:
and task["deadline"] > time.time())
def create_task(self, turn, owner, host, generation, session, worker, deadline, chat_id="", message_id=""):
conversation = hashlib.sha256((self.environment+"\0"+self.app_id+"\0"+owner+"\0"+chat_id+"\0work-dm").encode()).hexdigest()
with self.db:
self._owner_lock(owner)
device = self.store.device(host)
if not device or device["revoked"] or device["owner"] != owner or device["binding_generation"] != generation:
raise ValueError("binding changed")
count = self.db.execute("""INSERT INTO work_tasks_v2 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT DO NOTHING""", (turn,self.environment,self.app_id,owner,host,generation,session,
worker,conversation,chat_id,message_id,"inflight",deadline,time.time(),time.time())).rowcount
if count != 1:
raise ValueError("task already recorded")
return self.create_task_locked(turn,owner,host,generation,session,worker,deadline,chat_id,message_id)
def create_task_locked(self,turn,owner,host,generation,session,worker,deadline,chat_id,message_id,status='inflight'):
conversation = hashlib.sha256((self.environment+"\0"+self.app_id+"\0"+owner+"\0"+chat_id+"\0work-dm").encode()).hexdigest()
device = self.store.device(host)
if not device or device["revoked"] or device["owner"] != owner or device["binding_generation"] != generation:
raise ValueError("binding changed")
count = self.db.execute("""INSERT INTO work_tasks_v2 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT DO NOTHING""", (turn,self.environment,self.app_id,owner,host,generation,session,
worker,conversation,chat_id,message_id,status,deadline,time.time(),time.time())).rowcount
if count != 1:
raise ValueError("task already recorded")
return self.task(turn)
def transition(self, turn, previous, status):