The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(gateway): the delivery ledger prunes inside the recording transaction
kshitijk4poor4c41d9804c1d
diff --git a/gateway/delivery_ledger.py b/gateway/delivery_ledger.pyindex afe180c46d0..d0a84fedfcd 100644--- a/gateway/delivery_ledger.py+++ b/gateway/delivery_ledger.py@@ -276,7 +276,12 @@ def record_obligation(*, obligation_id: str, session_key: str, platform: str, chVALUES (?, ?, ?, ?, ?, ?, 'pending', 0, ?, ?, ?, ?, ?)""",(obligation_id, session_key, platform, str(chat_id), str(thread_id) if thread_id else None,content, now, now, pid, started, str(adapter_profile).strip() if adapter_profile else "default"))- _prune()+ # Same transaction, same connection: the cron ledgers prune this way too+ # (cron/delivery_queue._prune_terminal_unlocked, cron/executions._prune_unlocked).+ try:+ _prune_unlocked(conn, now)+ except Exception: # a prune failure must never cost the recorded obligation+ logger.debug("delivery ledger prune failed", exc_info=True)def mark_attempting(obligation_id: str) -> None:@@ -503,24 +508,29 @@ def pending_retries(now: Optional[float] = None) -> List[Dict[str, Any]]:for (platform, profile), due in sorted(earliest.items())]+def _prune_unlocked(conn, now: float) -> None:+ """Retention DELETEs on the caller's open connection — must run inside the caller's transaction."""+ conn.execute(+ """DELETE FROM delivery_obligations+ WHERE state IN ('delivered', 'abandoned') AND updated_at < ?""", (now - _RETENTION_SECONDS,))+ total = conn.execute("SELECT COUNT(*) FROM delivery_obligations").fetchone()[0]+ if total > _MAX_ROWS:+ conn.execute(+ """DELETE FROM delivery_obligations WHERE obligation_id IN (+ SELECT obligation_id FROM delivery_obligations+ ORDER BY CASE state+ WHEN 'delivered' THEN 0+ WHEN 'abandoned' THEN 1+ ELSE 2+ END, updated_at ASC+ LIMIT ?)""", (total - _MAX_ROWS,))++def _prune(now: Optional[float] = None) -> None:now = now if now is not None else time.time()try:with _transaction() as conn:- conn.execute(- """DELETE FROM delivery_obligations- WHERE state IN ('delivered', 'abandoned') AND updated_at < ?""", (now - _RETENTION_SECONDS,))- total = conn.execute("SELECT COUNT(*) FROM delivery_obligations").fetchone()[0]- if total > _MAX_ROWS:- conn.execute(- """DELETE FROM delivery_obligations WHERE obligation_id IN (- SELECT obligation_id FROM delivery_obligations- ORDER BY CASE state- WHEN 'delivered' THEN 0- WHEN 'abandoned' THEN 1- ELSE 2- END, updated_at ASC- LIMIT ?)""", (total - _MAX_ROWS,))+ _prune_unlocked(conn, now)except Exception:logger.debug("delivery ledger prune failed", exc_info=True)