Skip to content

The agent that grows with you

test(cron): three invariants for the post-delivery fire-claim exemption

kshitijk4poorb97922706c3d
diff --git a/tests/cron/test_fire_claim_lost_after_delivery.py b/tests/cron/test_fire_claim_lost_after_delivery.py
index 171c354f06f..c50ad5f2627 100644
--- a/tests/cron/test_fire_claim_lost_after_delivery.py
+++ b/tests/cron/test_fire_claim_lost_after_delivery.py
@@ -1,22 +1,16 @@
"""#105861: a fire-claim that only *reads* as lost AFTER a run delivered must not overwrite
the delivered success with an error.
-`_FireOwnership.lost()` samples the claim from the store once. In the reported incidents that
-sample missed (a run's own delivery holds the per-job fence past `_JOBS_LOCK_TIMEOUT_SECONDS`,
-and jobs.json is rewritten by two processes per run), the run wrapper then recorded
-`last_status: error` — and the operator's health watchdog alerted on every tick for a job whose
-output had already reached the channel.
-
-Suggested fix #1 (the smallest one that closes it): when the run's delivery actually completed,
-the sampled claim loss is bookkeeping, not a delivery failure — warn and fall through to the
-owner-fenced terminal write in `_finish_completed_run`, which is the authoritative claim check
-(it records `ok` only while the claim is still held, and records nothing when it really is gone).
+`_FireOwnership.lost()` samples the claim from the store once. When that sample misses after
+the notice already reached the channel, the run must fall through to the owner-fenced terminal
+write in `_finish_completed_run` (the authoritative claim check) instead of recording an error.
These drive the real store (``jobs.json`` under a temp HERMES_HOME) so the assertion is the
actual on-disk ``last_status`` the health watchdog reads, not a mock's call list.
"""
import logging
+import threading
import pytest
@@ -42,13 +36,7 @@ def _clean_running_state():
class _SampledHeartbeat:
- """The real ``heartbeat_fire_claim``, with exactly ONE armed sample missing.
-
- That single miss is the whole bug: the claim is sampled once, a miss flips the run's
- ownership flag permanently, and the *next* heartbeat (inside
- ``_record_fire_ownership_lost``) confirms the claim is still ours — which is how a
- delivered run ended up recorded as ``error``.
- """
+ """The real ``heartbeat_fire_claim``, with exactly ONE armed sample missing."""
def __init__(self, real, skip_samples: int):
self._real = real
@@ -113,11 +101,7 @@ def _drive(monkeypatch, *, final_response, deliver, miss_at_sample):
def test_delivered_run_stays_ok_when_claim_sample_misses_after_delivery(
temp_home, monkeypatch, caplog,
):
- """① Delivery completed, then the claim sample missed → last_status stays ok + warning.
-
- Sample 1 is the pre-delivery check, sample 2 the one guarding the side effect; the third
- (post-delivery) is the one the reported incidents hit.
- """
+ """Delivery completed, then the claim sample missed → last_status stays ok + warning."""
from cron.jobs import get_job
sched, job, hb, delivered = _drive(
@@ -136,64 +120,8 @@ def test_delivered_run_stays_ok_when_claim_sample_misses_after_delivery(
assert "ownership lost" in warnings and "deliver" in warnings
-def test_claim_lost_during_delivery_still_records_error(temp_home, monkeypatch):
- """②a A delivery that FAILED, then a lost claim → error (the protection must not weaken)."""
- from cron.jobs import get_job
-
- sched, job, _hb, delivered = _drive(
- monkeypatch, final_response="the report", deliver="send failed: 502", miss_at_sample=2)
-
- assert sched.run_one_job(job) is True
-
- assert delivered == ["the report"]
- record = get_job(job["id"])
- assert record["last_status"] == "error"
- assert record["last_error"] == sched._OWNERSHIP_LOST_INTERRUPTED
-
-
-def test_claim_lost_with_delivery_never_attempted_still_records_error(temp_home, monkeypatch):
- """②b A SILENT run delivers nothing, then a lost claim → error, not a delivered-ok.
-
- No delivery means no delivery phase to guard: sample 2 here is the post-delivery check.
- """
- import cron.scheduler as sched
- from cron.jobs import get_job
-
- sched, job, _hb, delivered = _drive(
- monkeypatch, final_response=sched.SILENT_MARKER, deliver=None, miss_at_sample=1)
-
- assert sched.run_one_job(job) is True
-
- assert delivered == [], "a SILENT run must not deliver"
- record = get_job(job["id"])
- assert record["last_status"] == "error"
- assert record["last_error"] == sched._OWNERSHIP_LOST_INTERRUPTED
-
-
-def test_claim_lost_before_delivery_still_records_error(temp_home, monkeypatch):
- """③ A claim sampled as lost before the side effect → error, delivery untouched."""
- from cron.jobs import get_job
-
- sched, job, _hb, delivered = _drive(
- monkeypatch, final_response="the report", deliver=None, miss_at_sample=0)
-
- assert sched.run_one_job(job) is True
-
- assert delivered == [], "a run that lost its claim must not deliver"
- record = get_job(job["id"])
- assert record["last_status"] == "error"
- assert record["last_error"] == sched._OWNERSHIP_LOST_INTERRUPTED
-
-
def test_transport_cancel_during_delivery_stays_fail_closed(temp_home, monkeypatch):
- """④ The transport cancel is an explicit stop signal, not a sampled miss → still interrupted.
-
- ``fire_claim_lost`` ORs the sampled claim with the transport-level ``cancel_event``, so the
- delivered-success exemption must not swallow a drain that arrived while the notice was being
- sent: the run's provider relies on that cancellation being fail-closed.
- """
- import threading
-
+ """An explicit transport cancel during delivery is not a sampled miss → still interrupted."""
from cron.jobs import get_job
sched, job, hb, delivered = _drive(
@@ -218,17 +146,8 @@ def test_transport_cancel_during_delivery_stays_fail_closed(temp_home, monkeypat
def test_sampled_miss_leaves_an_idle_transport_event_alone(temp_home, monkeypatch):
- """⑤ A sampled miss with an (unset) transport cancel supplied → the run's own loss latch
- must not read as a transport cancellation, so the delivered run stays ok.
-
- ``_FireOwnership.lost()`` latches a sampled loss by calling ``set()`` on ``fire_claim_lost``.
- With a transport ``cancel_event`` in play that object is ``_CombinedCancelEvent(lost_ownership,
- cancel_event)``, and its ``set()`` sets every source — so the transport event read as cancelled
- and the delivered-run exemption was skipped, recording the same false interrupted error the
- issue reports. The latch has to carry the cancellation source's identity.
- """
- import threading
-
+ """A sampled miss must latch on the raw source, not the combined event: the caller's
+ (unset) transport cancel stays unset and the delivered run stays ok."""
from cron.jobs import get_job
sched, job, hb, delivered = _drive(