The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(cron): run-or-book-cancel lives beside the cancel marker, keeping _run_one_job_body's complexity at its baseline
Tekniumb64c3757240c
diff --git a/cron/cancellation.py b/cron/cancellation.pyindex c0eeadf52f4..4c23e644768 100644--- a/cron/cancellation.py+++ b/cron/cancellation.py@@ -146,7 +146,7 @@ def cancel_reason(cancel_event: Any) -> str:return getattr(cancel_event, "reason", None) or "cron fire ownership was lost"-def finish_cancelled_run(job: dict[str, Any], execution_id: str, fire_owner: Optional[str]) -> bool:+def finish_cancelled_run(job: dict[str, Any], execution_id: str, fire_owner: Optional[str]) -> None:"""Book an operator-cancelled run: ledger row ``cancelled`` with the recorded reason, and thejob's ``last_status`` — without a failure alert or a failure-streak bump, since the operatorasked for it. A job removed mid-run has no record left to mark; its ledger row still lands."""@@ -157,7 +157,28 @@ def finish_cancelled_run(job: dict[str, Any], execution_id: str, fire_owner: Optowner = {"expected_fire_owner": fire_owner} if fire_owner is not None else {}scheduler.mark_job_run(job["id"], False, f"Cancelled: {reason}", status="cancelled", **owner)scheduler.finish_execution(execution_id, success=False, error=reason, cancelled=True)- return True+++def run_job_unless_cancelled(run_job, job, run_kwargs, teardown, fence, execution_id, fire_owner):+ """``run_job``'s result, or None once an operator cancel stopped the run and was booked.++ Checked before the caller's claim-loss handling: removing a job mid-run cancels it AND drops+ its claim, and the run must book the cancel, not "fire claim ownership lost" (#123045).+ ``run_job`` hands the agent back even when raising, so ``teardown`` runs on every path+ (BaseException, so KeyboardInterrupt/SystemExit mid-run still tear down)."""+ try:+ result = run_job(job, **run_kwargs)+ except BaseException as exc:+ teardown()+ if not (isinstance(exc, Exception) and fence.operator_cancelled()):+ raise+ finish_cancelled_run(job, execution_id, fire_owner)+ return None+ if not fence.operator_cancelled():+ return result+ teardown()+ finish_cancelled_run(job, execution_id, fire_owner)+ return Nonedef cancel_run(job_id: str, *, reason: Optional[str] = None) -> dict[str, Any]:diff --git a/cron/scheduler.py b/cron/scheduler.pyindex c618230fcfb..4532003fda6 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -532,7 +532,7 @@ from cron.executions import (from cron.scheduler_liveness import ExecutionProgressStamper, _inactivity_watchdog_loopfrom cron.scheduler_bookkeeping import _classify_delivery_outcomefrom cron.cancellation import (CancelRequest, _CancelEventLike, _CombinedCancelEvent, cancel_reason,- clear_cancel, finish_cancelled_run as _finish_cancelled_run)+ clear_cancel, run_job_unless_cancelled as _run_job_unless_cancelled)# Response marker that suppresses delivery (output is still saved locally for audit).SILENT_MARKER = "[SILENT]"@@ -3350,20 +3350,11 @@ def _run_one_job_body("execution_id": execution_id}if fence.cancel_event is not None:_run_kwargs["cancel_event"] = fence.cancel_event- try:- success, output, final_response, error = run_job(job, **_run_kwargs)- except BaseException as exc:- # run_job hands back the agent even when raising; tear down so a failed run never leaks.- # BaseException so KeyboardInterrupt/SystemExit mid-run still trigger teardown.- _teardown_deferred()- if not (isinstance(exc, Exception) and fence.operator_cancelled()):- raise- success = None- # Before claim loss: removing a job mid-run cancels it AND drops its claim (#123045).- if fence.operator_cancelled():- if success is not None: # the except path above already tore down- _teardown_deferred()- return _finish_cancelled_run(job, execution_id, fire_owner)+ outcome = _run_job_unless_cancelled(+ run_job, job, _run_kwargs, _teardown_deferred, fence, execution_id, fire_owner)+ if outcome is None: # an operator cancel stopped it and is already booked+ return True+ success, output, final_response, error = outcomeif _fire_claim_ownership_lost():_teardown_deferred()