Skip to content

The agent that grows with you

fix(cron): settle the run a dead drain owner's unknown delivery belongs to

Teknium15b4c3467af5
diff --git a/cron/delivery_queue.py b/cron/delivery_queue.py
index 12b220ded0c..5d3ecd9c29e 100644
--- a/cron/delivery_queue.py
+++ b/cron/delivery_queue.py
@@ -280,11 +280,15 @@ def _finish(execution_id: str, *, error: Optional[str], suppressed: bool = False
def recover_abandoned() -> int:
- """Fence dead delivery owners as unknown; never replay uncertain sends."""
- changed = 0
+ """Fence dead delivery owners as unknown; never replay uncertain sends.
+
+ The firer settled long ago against a ``queued`` row, so each fenced row's outcome is written
+ back here (job ``delivery_failed`` + error, ledger ``failed``) instead of leaving the run at
+ ``delivery_queued`` forever."""
+ fenced: list[tuple[Optional[str], str]] = []
with _transaction() as conn:
rows = conn.execute(
- "SELECT execution_id, owner_process_id, owner_pid, owner_started_at "
+ "SELECT execution_id, job_json, owner_process_id, owner_pid, owner_started_at "
"FROM deliveries WHERE status='delivering'"
).fetchall()
for row in rows:
@@ -310,9 +314,15 @@ def recover_abandoned() -> int:
row["execution_id"],
),
)
- changed += cur.rowcount
+ if cur.rowcount:
+ # Read the job id now: pruning below redacts a terminal row's job_json.
+ fenced.append((json.loads(row["job_json"] or "{}").get("id"), row["execution_id"]))
_prune_terminal_unlocked(conn)
- return changed
+ from cron.delivery_outcome import settle_quietly
+
+ for job_id, execution_id in fenced:
+ settle_quietly(job_id, execution_id)
+ return len(fenced)
def drain(
diff --git a/tests/cron/test_queued_delivery_fencing.py b/tests/cron/test_queued_delivery_fencing.py
index 4dabc47de75..506030ab112 100644
--- a/tests/cron/test_queued_delivery_fencing.py
+++ b/tests/cron/test_queued_delivery_fencing.py
@@ -24,3 +24,23 @@ def test_claim_stolen_after_the_ownership_sample_queues_no_ghost_send(tmp_path,
assert samples[:2] == [False, False]
assert delivery_queue.get_status(execution["id"]) is None
assert execution["status"] == "failed"
+
+
+def test_dead_drain_owner_settles_the_run_it_fenced_unknown(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(scheduler, "run_job", lambda job, **kw: (True, "raw", "the result", None))
+ job = jobs.create_job(prompt="p", schedule="every 1h", deliver="telegram:fixture")
+ scheduler.run_one_job(job)
+ execution = executions.latest_execution(job["id"])
+ assert jobs.get_job(job["id"])["last_status"] == "delivery_queued"
+ claimed = delivery_queue.claim_next()
+ delivery_queue._ACTIVE_DELIVERIES.discard(claimed["execution_id"])
+ with delivery_queue._transaction() as conn: # the claiming gateway died mid-send
+ conn.execute("UPDATE deliveries SET owner_process_id='gone', owner_pid=999999 "
+ "WHERE execution_id=?", (execution["id"],))
+ sent = []
+ delivery_queue.drain(lambda *args: sent.append(args))
+ saved = jobs.get_job(job["id"])
+ assert sent == [], "an uncertain send is never retried"
+ assert saved["last_status"] == "delivery_failed" and "unknown" in saved["last_delivery_error"]
+ assert executions.get_execution(execution["id"])["delivery_outcome"] == "failed"