Skip to content

The agent that grows with you

fix: prevent duplicate cron alerts after durable delivery handoff

Teknium47e3bb25fb5b
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 05105ccb887..88707aaf0a9 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -2701,6 +2701,8 @@ def _save_compose_deliver(
if d.failure_incident_id:
queued_job["_failure_incident_id"] = d.failure_incident_id
queued = enqueue(execution_id, queued_job, deliver_content, for_failure=not d.success)
+ # The queue owns this send even if subsequent bookkeeping fails.
+ d.delivery_attempted = True
job['last_delivery_queued'] = {'canonical': {'status': queued['status'], 'execution_id': execution_id}}
return
d.unresolved_origin = (
diff --git a/tests/cron/test_run_one_job.py b/tests/cron/test_run_one_job.py
index 9b241de1b63..c0bb4e7e71a 100644
--- a/tests/cron/test_run_one_job.py
+++ b/tests/cron/test_run_one_job.py
@@ -1,6 +1,6 @@
"""Characterization + unit tests for the `run_one_job` shared helper (Phase 4A).
-`tick`'s per-job body (`_process_job`) is the execute → save → deliver → mark
+`tick`'s per-job body (`_process_job`) is the execute → save → enqueue → mark
sequence that fires ONE due job. Phase 4A extracts it into a module-level
`run_one_job(job, *, adapters=None, loop=None, verbose=False)` so the external
Chronos provider's `fire_due` can reuse the IDENTICAL body — no duplicated
@@ -29,37 +29,37 @@ def _patch_pipeline(monkeypatch, *, success=True, output="out", final="final res
calls.append(("save", jid))
return f"/tmp/{jid}.txt"
- def fake_deliver(job, content, adapters=None, loop=None, **kwargs):
- calls.append(("deliver", job["id"]))
- return None
+ def fake_deliver(execution_id, job, content, **kwargs):
+ calls.append(("enqueue", job["id"]))
+ return {"status": "pending"}
def fake_mark(jid, ok, err=None, delivery_error=None, **_kw):
calls.append(("mark", jid, ok))
monkeypatch.setattr(s, "run_job", fake_run_job)
monkeypatch.setattr(s, "save_job_output", fake_save)
- monkeypatch.setattr(s, "_deliver_result", fake_deliver)
+ monkeypatch.setattr("cron.delivery_queue.enqueue", fake_deliver)
monkeypatch.setattr(s, "mark_job_run", fake_mark)
return calls
def test_tick_process_job_sequence(monkeypatch):
"""Characterization: a single due job driven through tick() runs the
- sequence run_job → save → deliver → mark, in that order."""
+ sequence run_job → save → enqueue → mark, in that order."""
calls = _patch_pipeline(monkeypatch)
- monkeypatch.setattr(s, "get_due_jobs", lambda: [{"id": "j1", "name": "t"}])
+ monkeypatch.setattr(s, "get_due_jobs", lambda: [{"id": "j1", "name": "t", "deliver": "telegram"}])
monkeypatch.setattr(s, "claim_job_for_fire", lambda _job_id, **_kwargs: True)
s.tick(verbose=False, sync=True)
- assert [c[0] for c in calls] == ["run_job", "save", "deliver", "mark"]
+ assert [c[0] for c in calls] == ["run_job", "save", "enqueue", "mark"]
assert calls[-1] == ("mark", "j1", True)
def test_tick_skips_job_when_durable_fire_claim_is_lost(monkeypatch):
"""A manual/external fire that wins the shared CAS must exclude ticker."""
calls = _patch_pipeline(monkeypatch)
- monkeypatch.setattr(s, "get_due_jobs", lambda: [{"id": "j1", "name": "t"}])
+ monkeypatch.setattr(s, "get_due_jobs", lambda: [{"id": "j1", "name": "t", "deliver": "telegram"}])
monkeypatch.setattr(s, "claim_job_for_fire", lambda _job_id: False)
assert s.tick(verbose=False, sync=True) == 0
@@ -67,14 +67,14 @@ def test_tick_skips_job_when_durable_fire_claim_is_lost(monkeypatch):
def test_run_one_job_success_sequence(monkeypatch):
- """The extracted helper runs the same execute→save→deliver→mark sequence
+ """The extracted helper runs the same execute→save→enqueue→mark sequence
for a successful job."""
calls = _patch_pipeline(monkeypatch)
- ok = s.run_one_job({"id": "j2", "name": "t"})
+ ok = s.run_one_job({"id": "j2", "name": "t", "deliver": "telegram"})
assert ok is True
- assert [c[0] for c in calls] == ["run_job", "save", "deliver", "mark"]
+ assert [c[0] for c in calls] == ["run_job", "save", "enqueue", "mark"]
assert calls[-1] == ("mark", "j2", True)
@@ -270,8 +270,12 @@ def test_run_one_job_exception_after_delivery_does_not_redeliver(monkeypatch):
ok = s.run_one_job({"id": "j5", "name": "once", "deliver": "telegram"})
assert ok is False
+ assert delivered == [], "bookkeeping failure must not send a second alert"
+ assert s.drain_delivery_queue(adapters={}, loop=None) == 1
assert delivered == [("j5", "final response")]
- assert mark_calls[0] == (("j5", True, None), {"delivery_error": None})
+ assert s.drain_delivery_queue(adapters={}, loop=None) == 0
+ assert mark_calls[0] == (("j5", True, None), {
+ "delivery_error": None, "execution_id": "exec-j5", "status": "delivery_queued"})
assert mark_calls[1] == (
("j5", False, "bookkeeping boom"),
{"delivery_error": None},
@@ -329,7 +333,7 @@ def test_run_one_job_keyboard_interrupt_skips_delivery_and_reraises(monkeypatch)
def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_path):
"""Regression: under profile isolation (multiplex active), run_one_job must
- keep one profile secret scope active through execution and delivery so
+ keep one profile secret scope active through execution and delivery handoff so
credential reads do not fail closed or fall through to another profile,
then tear the scope down after the complete job lifecycle.
@@ -355,16 +359,16 @@ def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_path
def fake_deliver(*args, **kwargs):
scope_during_delivery["scope"] = ss.current_secret_scope()
scope_during_delivery["base_url"] = ss.get_secret("OPENROUTER_BASE_URL")
- return None
+ return {"status": "pending"}
monkeypatch.setattr(s, "run_job", fake_run_job)
monkeypatch.setattr(s, "save_job_output", lambda jid, out: f"/tmp/{jid}.txt")
- monkeypatch.setattr(s, "_deliver_result", fake_deliver)
+ monkeypatch.setattr("cron.delivery_queue.enqueue", fake_deliver)
monkeypatch.setattr(s, "mark_job_run", lambda *a, **k: None)
ss.set_multiplex_active(True)
try:
- ok = s.run_one_job({"id": "j7", "name": "t"})
+ ok = s.run_one_job({"id": "j7", "name": "t", "deliver": "telegram"})
finally:
ss.set_multiplex_active(False)
diff --git a/tests/cron/test_shutdown_interrupt.py b/tests/cron/test_shutdown_interrupt.py
index a8bb3bf28f1..9d70a960b1f 100644
--- a/tests/cron/test_shutdown_interrupt.py
+++ b/tests/cron/test_shutdown_interrupt.py
@@ -582,7 +582,7 @@ class TestRunOneJobHonoursInterruptedFlag:
would."""
import cron.scheduler as sched
- job = self._make_job()
+ job = dict(self._make_job(), deliver="telegram")
sched._interrupted_job_ids.add(job["id"])
with patch("cron.scheduler.claim_dispatch", return_value=True), \
@@ -599,7 +599,7 @@ class TestRunOneJobHonoursInterruptedFlag:
return_value="This run was interrupted.",
) as mock_summarize, \
patch("cron.scheduler._is_cron_silence_response", return_value=False), \
- patch("cron.scheduler._deliver_result", return_value=None) as mock_deliver, \
+ patch("cron.delivery_queue.enqueue", return_value={"status": "pending"}) as mock_deliver, \
patch("cron.scheduler.mark_job_run"):
result = sched.run_one_job(job)
@@ -608,7 +608,9 @@ class TestRunOneJobHonoursInterruptedFlag:
# The summarizer's error argument must mention the interruption,
# not be silently None / the agent's own (possibly absent) error.
assert "interrupt" in mock_summarize.call_args.args[1].lower()
- delivered_content = mock_deliver.call_args.args[1]
+ mock_deliver.assert_called_once()
+ assert mock_deliver.call_args.kwargs["for_failure"] is True
+ delivered_content = mock_deliver.call_args.args[2]
assert delivered_content == "This run was interrupted."
assert "plausible final response" not in delivered_content