fix: persist API delegation units once without waking the model
This commit is contained in:
@@ -307,6 +307,38 @@ class SessionMessagesMixin:
|
||||
# holding the lock for seconds (VACUUM, checkpoint) can't kill it.
|
||||
return self._execute_write(_do, patience_s=self._TRANSCRIPT_WRITE_PATIENCE_S)
|
||||
|
||||
def append_delegation_delivery(self, session_id: str, content: str, metadata: Dict[str, Any]) -> int:
|
||||
"""Record a detached API result once, between client turns, including replay after rotation.
|
||||
|
||||
The event's unit id, not its text or active flag, is the identity. Check and insert
|
||||
share the writer transaction, so independent gateway processes cannot duplicate it.
|
||||
"""
|
||||
delegation_id = metadata.get("delegation_id")
|
||||
if not delegation_id:
|
||||
raise ValueError("Delegation delivery requires a stable delegation_id")
|
||||
msg = {"content": content, "display_kind": "async_delegation_complete", "display_metadata": metadata}
|
||||
params = self._message_row_params(session_id, "user", msg, None, time.time(), keep_reasoning=True)
|
||||
|
||||
def _do(conn):
|
||||
existing = conn.execute(
|
||||
"""WITH RECURSIVE lineage(id) AS (
|
||||
SELECT ? UNION
|
||||
SELECT s.parent_session_id FROM sessions s JOIN lineage l ON s.id = l.id
|
||||
JOIN sessions p ON p.id = s.parent_session_id WHERE p.end_reason = 'compression'
|
||||
) SELECT m.id FROM messages m JOIN lineage l ON m.session_id = l.id
|
||||
WHERE m.display_kind = 'async_delegation_complete'
|
||||
AND json_extract(m.display_metadata, '$.delegation_id') = ?
|
||||
AND coalesce(json_extract(m.display_metadata, '$.delivery_notice'), '') = ? LIMIT 1""",
|
||||
(session_id, delegation_id, metadata.get("delivery_notice", ""))).fetchone()
|
||||
if existing is not None:
|
||||
return existing[0]
|
||||
self._check_transcript_write_guards(conn, session_id, None, reject_active_turn_lease=True)
|
||||
msg_id = conn.execute(_INSERT_MESSAGE_SQL, params).lastrowid
|
||||
self._bump_session_counters(conn, session_id, 1, 0, unit=True)
|
||||
return msg_id
|
||||
|
||||
return self._execute_write(_do, patience_s=self._TRANSCRIPT_WRITE_PATIENCE_S)
|
||||
|
||||
def append_messages_batch(
|
||||
self, session_id: str, messages: List[Dict[str, Any]], compression_lock_holder: Optional[str] = None,
|
||||
turn_lease_holder: Optional[str] = None, chunk_rows: Optional[int] = None,
|
||||
|
||||
Reference in New Issue
Block a user