fix(pheby): preserve voice run IDs through Hermes admission
Carry run identity in the native message ID. Preserve typed and voice admission on current Hermes and use explicit reply anchors when a durable drain reuses its task.
This commit is contained in:
+19
-13
@@ -125,7 +125,7 @@ class PhebyAdapter(BasePlatformAdapter):
|
||||
async def _process_message_background(self, event, session_key: str) -> None:
|
||||
# Final delivery and the stream consumer's asyncio task inherit this
|
||||
# turn's ID, even if another run takes over the chat while they unwind.
|
||||
run_id = (event.metadata or {}).get("pheby_run_id")
|
||||
run_id = proto.run_id_from_message_id(event.message_id)
|
||||
cid = self._conv_from_chat_id(event.source.chat_id)
|
||||
token = _processing_run.set((cid, run_id) if run_id else None)
|
||||
try:
|
||||
@@ -133,26 +133,29 @@ class PhebyAdapter(BasePlatformAdapter):
|
||||
finally:
|
||||
_processing_run.reset(token)
|
||||
|
||||
def _delivery_run(self, conversation_id: str) -> tuple[Optional[str], bool]:
|
||||
def _delivery_run(self, conversation_id: str, anchored_run: Optional[str] = None) -> tuple[Optional[str], bool]:
|
||||
active = hermes_bridge.active_run(conversation_id)
|
||||
scoped = _processing_run.get()
|
||||
if scoped and scoped[0] == conversation_id:
|
||||
return scoped[1], bool(active and active["run_id"] == scoped[1]
|
||||
and not active.get("cancelling"))
|
||||
if active and active.get("cancelling"):
|
||||
return None, False
|
||||
return active["run_id"] if active else None, True
|
||||
scoped_run = scoped[1] if scoped and scoped[0] == conversation_id else None
|
||||
if active is None:
|
||||
# A recovered Hermes delivery can outlive the connector's run map.
|
||||
# An in-process callback carrying the old run is still stale.
|
||||
return scoped_run, scoped_run is None
|
||||
# A durable Hermes drain may handle a successor in the same task.
|
||||
# Its explicit reply or draft anchor identifies the actual turn.
|
||||
run_id = anchored_run or scoped_run or active["run_id"]
|
||||
return run_id, run_id == active["run_id"] and not active.get("cancelling")
|
||||
|
||||
async def on_processing_complete(self, event, outcome: ProcessingOutcome) -> None:
|
||||
await super().on_processing_complete(event, outcome)
|
||||
run_id = (event.metadata or {}).get("pheby_run_id")
|
||||
run_id = proto.run_id_from_message_id(event.message_id)
|
||||
if not run_id:
|
||||
return
|
||||
cid = self._conv_from_chat_id(event.source.chat_id)
|
||||
if hermes_bridge.active_run_id(cid) != run_id:
|
||||
return
|
||||
pending = self._pending_messages.get(self._event_session_key(event))
|
||||
if pending is not None and (pending.metadata or {}).get("pheby_run_id") == run_id:
|
||||
if pending is not None and pending.message_id == event.message_id:
|
||||
return
|
||||
# Attachment-only and failed deliveries can finish without send() or a final edit.
|
||||
statuses = {ProcessingOutcome.SUCCESS: "completed", ProcessingOutcome.FAILURE: "failed",
|
||||
@@ -191,7 +194,9 @@ class PhebyAdapter(BasePlatformAdapter):
|
||||
await self._server.broadcast(event)
|
||||
return SendResult(success=True, message_id=event["message_id"])
|
||||
|
||||
run_id, current = self._delivery_run(conversation_id)
|
||||
anchored_run = (proto.run_id_from_message_id(reply_to)
|
||||
or proto.run_id_from_message_id(metadata.get("reply_to_message_id")))
|
||||
run_id, current = self._delivery_run(conversation_id, anchored_run)
|
||||
if not current:
|
||||
return SendResult(success=True, message_id=f"draft-{run_id}")
|
||||
|
||||
@@ -212,7 +217,7 @@ class PhebyAdapter(BasePlatformAdapter):
|
||||
return SendResult(success=True, message_id=message_id)
|
||||
|
||||
active = hermes_bridge.active_run(conversation_id)
|
||||
if (_processing_run.get() != (conversation_id, run_id) and reply_to and active
|
||||
if (anchored_run is None and _processing_run.get() != (conversation_id, run_id) and reply_to and active
|
||||
and active.get("message_id") not in (None, reply_to)):
|
||||
return SendResult(success=True, message_id=f"draft-{run_id}")
|
||||
|
||||
@@ -268,7 +273,8 @@ class PhebyAdapter(BasePlatformAdapter):
|
||||
if self._server is None:
|
||||
return SendResult(success=False, error="server not running")
|
||||
conversation_id = self._conv_from_chat_id(chat_id)
|
||||
run_id, current = self._delivery_run(conversation_id)
|
||||
anchored_run = proto.run_id_from_message_id(message_id, proto.DRAFT_MESSAGE_PREFIX)
|
||||
run_id, current = self._delivery_run(conversation_id, anchored_run)
|
||||
existing = self._drafts.get(conversation_id)
|
||||
if not current or (run_id and existing and message_id != existing["message_id"]):
|
||||
return SendResult(success=True, message_id=message_id)
|
||||
|
||||
Reference in New Issue
Block a user