The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): a second cancel request keeps the first reason
kokhlo9e5ff5098bb8
diff --git a/cron/cancellation.py b/cron/cancellation.pyindex d7537e8fce4..c7478299e31 100644--- a/cron/cancellation.py+++ b/cron/cancellation.py@@ -53,12 +53,18 @@ def _prune_stale_markers(now: Optional[float] = None) -> None:def request_cancel(execution_id: str, *, reason: Optional[str] = None) -> Path:- """Record a cancel for one execution. Idempotent: the marker holds the first reason."""+ """Record a cancel for one execution. Idempotent: the marker holds the first reason.++ A second request for the same execution keeps the reason already on disk, because the run+ may have read it into its report before the later request lands. The first ask is the one+ that describes why the operator acted.+ """path = _marker_path(execution_id)path.parent.mkdir(parents=True, exist_ok=True)- tmp = path.with_suffix(".cancel.tmp")- tmp.write_text((reason or _DEFAULT_REASON).strip(), encoding="utf-8")- os.replace(tmp, path)+ if not path.exists():+ tmp = path.with_suffix(".cancel.tmp")+ tmp.write_text((reason or _DEFAULT_REASON).strip(), encoding="utf-8")+ os.replace(tmp, path)_prune_stale_markers()return pathdiff --git a/tests/cron/test_cron_cancel.py b/tests/cron/test_cron_cancel.pyindex a302f84be96..2795cd2794b 100644--- a/tests/cron/test_cron_cancel.py+++ b/tests/cron/test_cron_cancel.py@@ -95,6 +95,8 @@ def test_operator_cancel_is_not_treated_as_claim_loss():@pytest.mark.live_system_guard_bypassdef test_cancelling_a_script_job_kills_it_and_names_the_reason(tmp_path, monkeypatch):import os+ import sys+ from pathlib import Pathimport psutilfrom cron import scheduler, scheduler_script@@ -103,6 +105,21 @@ def test_cancelling_a_script_job_kills_it_and_names_the_reason(tmp_path, monkeyp# Script paths resolve under the firing profile's home; point it at the scratch dir.monkeypatch.setattr(scheduler, "_get_hermes_home", lambda: tmp_path)monkeypatch.setattr(scheduler_script, "_get_script_timeout", lambda: 60)+ # Spawn through a scratch dependency environment: the store interpreter resolves under the+ # real Hermes home, which the home-I/O guard refuses. Pinned the same way+ # tests/cron/test_cron_script.py pins this seam — what is under test is the marker and the+ # kill, not interpreter selection.+ venv = tmp_path / "venv"+ (venv / "bin").mkdir(parents=True)+ (venv / "bin" / "python").symlink_to(sys.executable)+ monkeypatch.setattr("hermes_cli._launchers.resolve_store_python", lambda repo: Path(sys.executable))+ monkeypatch.setattr("pm.environments.selected_venv", lambda repo: venv)+ # Spawning sanitizes PATH, which resolves the real install's bin dir; the guard refuses that+ # read. The bin dir is irrelevant to the kill, and None is the documented "no install" answer.+ from tools.environments import local as local_env++ monkeypatch.setattr(local_env, "_resolve_hermes_bin_dir", lambda: None)+ monkeypatch.delenv("PYTHONPATH", raising=False)scripts = tmp_path / "scripts"scripts.mkdir()ready = tmp_path / "script.pid"@@ -145,6 +162,32 @@ def test_cancelling_a_script_job_kills_it_and_names_the_reason(tmp_path, monkeypthread.join(timeout=20)+def test_a_second_request_keeps_the_reason_the_run_already_reported():+ from cron.cancellation import CancelRequest, request_cancel++ request_cancel("exec-reason", reason="operator asked")++ request_cancel("exec-reason", reason="second ask")+ request_cancel("exec-reason")++ # The docstring promises the first reason wins; the run may already have reported it.+ assert CancelRequest("exec-reason").reason == "operator asked"+++def test_a_second_request_still_prunes_stale_markers(monkeypatch):+ from cron import cancellation+ from cron.cancellation import request_cancel++ pruned: list = []+ monkeypatch.setattr(cancellation, "_prune_stale_markers", lambda now=None: pruned.append(now))++ request_cancel("exec-pruned", reason="first")+ request_cancel("exec-pruned", reason="second")++ # An idempotent write must not skip the sweep that rides along with it.+ assert len(pruned) == 2++def _alive(pid: int) -> bool:import psutil