The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): review round 3: Stop settles by its own run, missed counts use the pinned zone, distributions keep catch_up
Tekniumd4865574e474
diff --git a/apps/desktop/src/app/cron/run-control.test.ts b/apps/desktop/src/app/cron/run-control.test.tsindex c3301ff8eca..388ef375dfc 100644--- a/apps/desktop/src/app/cron/run-control.test.ts+++ b/apps/desktop/src/app/cron/run-control.test.ts@@ -46,6 +46,14 @@ describe('cron Stop behind a refused Run-now', () => {)expect(stopOutcome(undefined, 'e1')).toBe('cancelled')})++ it('settles a stopped run once a newer run replaces it, so the new run can be stopped', () => {+ const next = { claimed_at: '2026-10-25T21:01:00Z', id: 'e2', status: 'running' }+ const stopped = { claimed_at: '2026-10-25T21:00:00Z', id: 'e1', status: 'cancelled' }++ expect(stopOutcome({ latest_execution: next, live_execution: next }, 'e1')).toBe('cancelled')+ expect(stopOutcome({ latest_execution: stopped, live_execution: null }, 'e1')).toBe('cancelled')+ })})describe('cron Stop during a Run-now', () => {diff --git a/apps/desktop/src/app/cron/run-control.ts b/apps/desktop/src/app/cron/run-control.tsindex 93e65b42f87..ddb64d3c1c2 100644--- a/apps/desktop/src/app/cron/run-control.ts+++ b/apps/desktop/src/app/cron/run-control.ts@@ -25,17 +25,27 @@ export function stopOutcome(job: RunFields | undefined, executionId: string): Streturn 'cancelled'}- if (jobRunInFlight(job)) {- return 'pending'- }+ // Settle by the targeted attempt, never by "some run is live": a newer run started after the+ // stopped one must not keep this Stop pending (and its button disabled).+ if (executionId) {+ const target = [job.live_execution, job.latest_execution].find(execution => execution?.id === executionId)- const latest = job.latest_execution+ if (!target) {+ return 'cancelled'+ }- if (executionId && latest && latest.id !== executionId) {- return 'cancelled'+ if (IN_FLIGHT_STATUSES.has(target.status ?? '')) {+ return 'pending'+ }++ return target.status === 'cancelled' ? 'cancelled' : 'finished'+ }++ if (jobRunInFlight(job)) {+ return 'pending'}- return latest?.status === 'cancelled' ? 'cancelled' : 'finished'+ return job.latest_execution?.status === 'cancelled' ? 'cancelled' : 'finished'}// A run created this long before a Run-now click (renderer/backend clock skew) still counts as its run.diff --git a/cron/job_definition.py b/cron/job_definition.pyindex 862eead7bb9..14bc0c418f9 100644--- a/cron/job_definition.py+++ b/cron/job_definition.py@@ -15,7 +15,7 @@ JOB_DEFINITION_FIELDS = frozenset({"script", "no_agent", "monitor_script", "monitor_url", "context_from","schedule", "schedule_display", "deliver", "origin", "enabled_toolsets","workdir", "attach_to_session", "reasoning_effort", "failure_deliver",- "timezone", "grace_minutes",+ "timezone", "grace_minutes", "catch_up",})diff --git a/cron/missed_runs.py b/cron/missed_runs.pyindex 9cde76420c6..31e666f3958 100644--- a/cron/missed_runs.py+++ b/cron/missed_runs.py@@ -41,8 +41,12 @@ def job_catches_up(job: dict[str, Any]) -> bool:return bool(_cron_config_number("catch_up_missed", True, lambda raw: raw is not False))-def count_missed_slots(schedule: dict[str, Any], first: str, now: datetime) -> int:- """Occurrences from ``first`` up to ``now`` inclusive, capped at ``_MAX_COUNTED_SLOTS``."""+def count_missed_slots(+ schedule: dict[str, Any], first: str, now: datetime, *, timezone: Any = None,+) -> int:+ """Occurrences from ``first`` up to ``now`` inclusive, capped at ``_MAX_COUNTED_SLOTS``.++ *timezone* is the job's pin (job record or zone name), as for ``compute_next_run``."""from cron.jobs import _instant_after, _parse_aware, compute_next_runcount, cursor = 0, first@@ -51,7 +55,7 @@ def count_missed_slots(schedule: dict[str, Any], first: str, now: datetime) -> iif at is None or _instant_after(at, now):breakcount += 1- following = compute_next_run(schedule, cursor)+ following = compute_next_run(schedule, cursor, timezone=timezone)cursor = following if following != cursor else Nonereturn count@@ -87,7 +91,7 @@ def fast_forward_missed_recurring(d: Any, grace: int) -> bool:if not new_next:return Falsed.scan.persist(d.job["id"], next_run_at=new_next)- missed = count_missed_slots(d.schedule, d.next_run, d.scan.now)+ missed = count_missed_slots(d.schedule, d.next_run, d.scan.now, timezone=d.job)capped = missed >= _MAX_COUNTED_SLOTSif (_instant_after(_ensure_aware(datetime.fromisoformat(new_next)), d.scan.now)and not job_catches_up(d.job)):diff --git a/tests/cron/test_cron_stop_and_missed_policy.py b/tests/cron/test_cron_stop_and_missed_policy.pyindex 60b970d7810..47c0562f998 100644--- a/tests/cron/test_cron_stop_and_missed_policy.py+++ b/tests/cron/test_cron_stop_and_missed_policy.py@@ -89,6 +89,32 @@ def test_explicit_grace_skips_past_it_unless_the_job_opts_into_catch_up(home):assert [r["status"] for r in _ledger(capped["id"])] == ["missed"]+def test_missed_slots_count_in_the_jobs_pinned_zone(home):+ """A 09:00 New York weekday job under a Los Angeles profile, overdue Thu 09:00 → Fri 15:00 NY:+ two slots (Thu, Fri), not a phantom Thu 09:00 LA occurrence in between."""+ from datetime import datetime+ from zoneinfo import ZoneInfo++ from cron.missed_runs import count_missed_slots++ (home / "config.yaml").write_text("timezone: America/Los_Angeles\n", encoding="utf-8")+ ny = ZoneInfo("America/New_York")+ schedule = jobs.parse_schedule("0 9 * * 1-5")+ first = datetime(2026, 10, 22, 9, 0, tzinfo=ny).isoformat()+ now = datetime(2026, 10, 23, 15, 0, tzinfo=ny)++ assert count_missed_slots(schedule, first, now, timezone="America/New_York") == 2+++def test_a_profile_distribution_carries_the_jobs_missed_run_policy(home):+ from cron.job_definition import merge_job_definition++ local = jobs.create_job(prompt="brief", schedule="0 7 * * *", deliver="local", catch_up=True)+ merged = merge_job_definition(dict(local), {"prompt": "brief", "schedule": local["schedule"],+ "catch_up": False})+ assert merged["catch_up"] is False++def test_one_shot_past_grace_skips_by_default_and_fires_once_when_opted_in(home):run_at = (jobs._hermes_now() + timedelta(hours=1)).isoformat()skipped = jobs.create_job(prompt="stale reminder", schedule=run_at, deliver="local")