Skip to content

The agent that grows with you

fix(cron): self-removed runs leave no output directory and skip mark_job_run on crash

teknium1134ef6454d3f
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 8c53704e321..d9c6ae21c6d 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -2686,8 +2686,11 @@ def _save_compose_deliver(
with fence.side_effect_fence() as owns_output:
if not owns_output:
raise _FireClaimLostDuringSideEffect
- output_file = save_job_output(job["id"], output)
- if verbose:
+ # remove_job() already deleted this job's output dir; saving would re-create an orphan.
+ output_file = (
+ None if self_removal_delivery_allowed(job["id"])
+ else save_job_output(job["id"], output))
+ if verbose and output_file is not None:
logger.info("Output saved to: %s", output_file)
# A shutdown-killed tool subprocess can leave a plausible final_response from truncated
@@ -3057,7 +3060,10 @@ def _run_one_job_body(
delivery_error, delivery_outcome = _deliver_crash_failure(
job, _err_text, adapters=adapters, loop=loop)
try:
- if not _consume_interrupted_flag(job["id"], execution_token):
+ if (
+ not _consume_interrupted_flag(job["id"], execution_token)
+ and not self_removal_delivery_allowed(job["id"]) # no record left to mark
+ ):
mark_kwargs = {}
if fire_owner is not None:
mark_kwargs["expected_fire_owner"] = fire_owner
diff --git a/tests/cron/test_script_claim_heartbeat.py b/tests/cron/test_script_claim_heartbeat.py
index fe96c675716..1903b78fff4 100644
--- a/tests/cron/test_script_claim_heartbeat.py
+++ b/tests/cron/test_script_claim_heartbeat.py
@@ -418,15 +418,21 @@ def test_lost_fire_claim_stops_stale_delivery(monkeypatch):
mark_run.assert_not_called()
-def _run_claimed_job_with_mid_run_action(tmp_path, monkeypatch, mid_run, *, execution_id):
+def _run_claimed_job_with_mid_run_action(
+ tmp_path, monkeypatch, mid_run, *, execution_id, stub_output=True, crash=None,
+ expect_result=True,
+):
"""Fire a claimed job through run_one_job with a stubbed agent run that performs ``mid_run``
- on its own record, keeps working past one fire-claim heartbeat tick, then completes."""
+ on its own record, keeps working past one fire-claim heartbeat tick, then completes (or
+ raises ``crash``)."""
import cron.jobs as jobs
import cron.scheduler as scheduler
def _run_job(job, **_kwargs):
mid_run(jobs, job)
time.sleep(0.3)
+ if crash is not None:
+ raise crash
return True, "saved output", "D1 is promoting", None
delivered = MagicMock(return_value=None)
@@ -436,7 +442,8 @@ def _run_claimed_job_with_mid_run_action(tmp_path, monkeypatch, mid_run, *, exec
monkeypatch.setattr(scheduler, "claim_dispatch", lambda *_args: True)
monkeypatch.setattr(scheduler, "mark_execution_running", lambda *_args: {})
monkeypatch.setattr(scheduler, "finish_execution", finished)
- monkeypatch.setattr(scheduler, "save_job_output", lambda *_args: "output.md")
+ if stub_output:
+ monkeypatch.setattr(scheduler, "save_job_output", lambda *_args: "output.md")
monkeypatch.setattr(scheduler, "_deliver_result", delivered)
with jobs.use_cron_store(tmp_path):
@@ -449,7 +456,7 @@ def _run_claimed_job_with_mid_run_action(tmp_path, monkeypatch, mid_run, *, exec
with patch("agent.secret_scope.set_secret_scope", return_value=None), \
patch("agent.secret_scope.build_profile_secret_scope", return_value=None), \
patch("agent.secret_scope.reset_secret_scope"):
- assert scheduler.run_one_job(claimed) is True
+ assert scheduler.run_one_job(claimed) is expect_result
return delivered, finished
@@ -471,6 +478,41 @@ def test_self_removed_job_still_delivers_after_post_removal_heartbeat(tmp_path,
assert jobs.load_jobs() == []
+def test_self_removed_job_leaves_no_output_directory(tmp_path, monkeypatch):
+ """remove_job() deletes <cron>/output/<job_id>/; the finishing run must not re-create it
+ (an orphan directory per self-removing job), so 'only the job record is gone' stays true."""
+ import cron.jobs as jobs
+
+ delivered, _finished = _run_claimed_job_with_mid_run_action(
+ tmp_path, monkeypatch,
+ lambda jobs_mod, job: jobs_mod.remove_job(job["id"]),
+ execution_id="self-removal-output-execution", stub_output=False)
+
+ delivered.assert_called_once()
+ with jobs.use_cron_store(tmp_path):
+ assert jobs.load_jobs() == []
+ assert list((tmp_path / "cron" / "output").glob("*")) == []
+
+
+def test_self_removed_job_crash_skips_mark_job_run(tmp_path, monkeypatch):
+ """A run that crashes after removing its own record has no record to mark: the crash path
+ must skip mark_job_run like the completion path does, not probe a missing record."""
+ import cron.scheduler as scheduler
+
+ marked = MagicMock(return_value=True)
+ monkeypatch.setattr(scheduler, "mark_job_run", marked)
+ _delivered, finished = _run_claimed_job_with_mid_run_action(
+ tmp_path, monkeypatch,
+ lambda jobs_mod, job: jobs_mod.remove_job(job["id"]),
+ execution_id="self-removal-crash-execution",
+ crash=RuntimeError("boom after self-removal"), expect_result=False)
+
+ marked.assert_not_called()
+ finished.assert_called_once_with(
+ "self-removal-crash-execution", success=False,
+ error="boom after self-removal", delivery_outcome="delivered")
+
+
def test_self_removal_followed_by_replacement_record_stays_fail_closed(tmp_path, monkeypatch):
"""Self-removal only excuses a MISSING record: once another owner's record reclaims the id,
the run is stale again and its result must be discarded, never delivered."""
diff --git a/website/docs/user-guide/features/cron.md b/website/docs/user-guide/features/cron.md
index 0cf0e84d140..cffac2942bc 100644
--- a/website/docs/user-guide/features/cron.md
+++ b/website/docs/user-guide/features/cron.md
@@ -317,8 +317,9 @@ cadence, or run a "cron librarian" job that reconciles the whole table
- **A job may remove itself and still report.** The "watch for X, tell me
once, then stop" pattern — a recurring job whose run calls
`cronjob(action="remove", job_id=<its own id>)` and then answers — delivers
- that final response and records the run as `completed`; only the job record
- is gone afterwards. Deleting the record from *outside* the run (another
+ that final response and records the run as `completed`; the job record and
+ its `cron/output/<job_id>/` directory are gone afterwards and the final run
+ is not written there. Deleting the record from *outside* the run (another
process, or a replacement job reusing the id) still discards the stale
run's output, as before.