Skip to content

fix(cron): park a cancelled delete_after_run one-shot - #25130

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
ykharkov:fix/cron-at-cancel-parks
Oct 29, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
ykharkov:fix/cron-at-cancel-parks

Conversation

@ykharkov

@ykharkov ykharkov commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A one-shot cron job the user cancels before its payload starts never runs, including the delete_after_run one-shots the dashboard, the CLI and MCP create.

A one-shot (at) job with delete_after_run runs anyway after a successful Cancel, when the cancel lands before its payload starts: while the run waits in the fire-time gate, or between the tick that dispatched it and the run task's first step. The job stays enabled with its due time passed, so the next tick dispatches it again and the payload runs. A plain one-shot cancelled at the same moment is parked and stays stopped.

How a user runs into it:

  1. The user asks the agent in chat to do something once at a set time, for example "at 3:00 PM today, post the release notes to the team channel". The agent creates a one-shot job with its cron_add tool (kirocrew cron add --at makes the same kind). Every one-shot made this way is delete_after_run: it is meant to remove itself after it runs.
  2. At 3:00 PM the scheduler picks the job up and starts a run. Before the run does the actual work, it waits in the fire-time gate.
  3. The user changes their mind and clicks Cancel run on the Schedule page while the run is still waiting. Cancel stops the run and says it worked.
  4. Cancel does not switch the job off. It leaves that to the automatic pause after a failed run, but that pause only happens once the work has started, and here it never started.
  5. The job is still switched on and 3:00 PM is now in the past, so on its next tick, seconds later, the scheduler picks the job up again and the release notes get posted after all.

Why it matters

Cancel is how a user stops a one-shot they no longer want: a message to a channel, an agent turn with side effects. Here Cancel reports success and the action happens anyway, seconds later. Every one-shot the dashboard, the CLI and MCP create is delete_after_run, so this is the common shape, not an edge setting.

Not a goal

  • The reaper path (_force_reap): a reaped delete_after_run one-shot whose run had already finished can still run twice. Open PR fix(cron): fence scheduled terminal settlement #9038 changes that settlement; this PR leaves it alone.
  • How a system refusal (fire-time denial, overlap, queue starvation) leaves a never-started one-shot scheduled: only a user cancel changes.
  • user_paused versus auto_paused semantics, and the cancel notification text.

What changed (motivation → approach → change)

A cancelled one-shot is now parked whatever its delete_after_run setting, so the next tick does not run it.

Symptom → cause. cancel() in cron_service/service/run_teardown.py parked a one-shot only when it was not delete_after_run, and left the delete_after_run ones to _execute_with_timeout's failed-run auto-pause. That pause runs only for a run whose payload started and failed. A cancel that lands earlier leaves the job enabled, and is_due treats an at job whose time has passed as due whatever last_run_ts says.

Change. cancel() parks every at job. _merge_terminal_state_locked already parks any at job it is asked to, so the one condition is the whole change: the job ends enabled false and user_paused true, the same park a cancelled plain one-shot gets today.

The cron spec (learn-cron-dashboard.md) states the same rule: a user cancel parks every one-shot, whatever its delete_after_run setting.

Backwards compatibility

Compatible: a cancelled delete_after_run one-shot is kept and parked instead of left due; nothing that works today stops working.

Removes nothing: the cancelled job is kept, parked the way a cancelled plain one-shot already is; no record, key, field or path is removed.

Tests

  • test_one_shot_cancelled_before_its_run_starts_does_not_run[False] and [True] (test/test_cron_cancel.py): a cancel between the tick and the run task's first step. The next tick does not dispatch the job, its payload never runs, and a reloaded service finds it disabled and user_paused.
  • The parametrization comment on test_cancelled_one_shot_does_not_run_again says why a mid-run cancel of a delete_after_run one-shot stays out of that test: the failed-run auto-pause races the cancel's store write, so its flags are not fixed.

Proof: PROVEN test/test_cron_cancel.py::TestCancelledOneShotIsParked::test_one_shot_cancelled_before_its_run_starts_does_not_run[True] (head 8786eb3)

Determinism, on the two new node ids and the file they live in:

  • 20 runs of the two new ids at -n0: 20/20 green.
  • 10 runs of test/test_cron_cancel.py (57 tests) at -n 4 --dist loadgroup --max-worker-restart=2: 10/10 green.
  • Shuffled order (the file's 57 ids passed shuffled, per testing-conventions), seeds 31435, 7211, 90817, 4402, 61263: all green.

scripts/local-gate.py: the related backend tests (477) and the related frontend set (410 files) pass.

Manual verification

  • Found with a Lean verification pipeline: a Lean 4 model of the one-shot lifecycle (dispatch, fire-time gate, run, cancel, reap, store merge) disproved "a cancelled one-shot never runs again" for delete_after_run jobs and produced the event sequences that break it. The two deterministic ones were then reproduced on the real CronService, as below. The model is not part of this PR.
  • Reproduced before the fix on the real CronService with a delete_after_run job: a cancel while the run waits in the fire-time gate, and a cancel before the run task's first step, both left the job enabled, and the next tick dispatched it and ran its payload once more. Plain one-shots in the same scenarios stayed parked. The fire-time gate case takes the same cancel() branch the new test covers.

Related Issues

Closes #25128

Sibling sweep

Swept: git grep -n "park_one_shot = " -- src/
Sibling: src/kiro_crew/cron_service/service/run_teardown.py:293 -- tracked in #9038

Pattern harvest

Not generalizable: one exclusion in one predicate, where the cancel path handed a required park to a pause whose precondition (the payload started) it cannot assume.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

A user cancel parked a plain one-shot "at" job but left a
delete_after_run one, which is every one-shot the dashboard, the CLI
and MCP create, to the failed-run auto-pause in _execute_with_timeout.
That pause only reaches a run whose payload started. A cancel that
lands earlier, in the fire-time gate or before the run task's first
step, left the job enabled with its due time passed, so the next tick
ran the job the user had just cancelled.

Cancel now parks every one-shot. The store merge already parks any
"at" job it is asked to, so nothing else changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Oct 28, 2026
@ykharkov
ykharkov marked this pull request as ready for review October 28, 2026 03:38
@ykharkov
ykharkov requested review from a team and KiroCrewCaptain as code owners October 28, 2026 03:38
@ykharkov
ykharkov requested a review from cruisercohen October 28, 2026 03:38
@ykharkov

Copy link
Copy Markdown
Contributor Author

Problem / Motivation edited at the author's request: it now walks through the user flow that leads to the bug (schedule a one-shot on the dashboard, Cancel it while its run waits in the fire-time gate, and the next tick runs it anyway). The Goal line, Why it matters and Not a goal are unchanged.

@ykharkov

Copy link
Copy Markdown
Contributor Author

Problem / Motivation corrected at the author's request: the user flow now starts where a one-shot is actually created (the agent's cron_add tool from a chat request, or kirocrew cron add --at), not the dashboard's job form, which offers only recurring schedules, and names the Schedule page's Cancel run button. The Goal line, Why it matters and Not a goal are unchanged.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision 8786eb35b2813fbf35a4ed5aa8bd17750ff74104 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 8786eb35b2813fbf35a4ed5aa8bd17750ff74104 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

The one-condition change fixes the root cause: the cancel path stopped leaving the park to an auto-pause that only runs after the payload has started.

Notes

  • After a mid-run cancel, a delete_after_run one-shot can still end with both user_paused and auto_paused set, because the two writes race. The PR leaves this case out of the test, so those final flags are not pinned. This can be reverted cleanly and does not cause a re-run, but it is worth a follow-up issue so the flag pair gets a single owner.

[DESIGN-REVIEWED] 8786eb3

Description read

[DESCRIPTION-READ] c40fd9b83df520b0698401bb34e9fd7375e98063cd7be5087300a70ce13b996a

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed 8786eb35b2813fbf35a4ed5aa8bd17750ff74104 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 8786eb3

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 8786eb35b2813fbf35a4ed5aa8bd17750ff74104 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 8786eb3

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of 8786eb35b2813fbf35a4ed5aa8bd17750ff74104 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

The fix is one condition removed at the point that causes the bug. It has a linked issue (#25128) and a test that fails on base. The one other site with this root cause, the reaper at run_teardown.py:293, is declared out of goal and tracked in #9038.

What this change ships

Inventory (4 items) — 4 justified

Intent: a one-shot job the user cancels before its payload starts must never run. This is a FIX.

  1. When a user cancels a delete_after_run one-shot, the job is now parked (disabled and user_paused, so the user can re-enable it) instead of staying due and running on the next tick — justified
  2. The rule in the cron spec now reads that a cancel parks every one-shot, whatever its delete_after_run setting — justified
  3. The comment that excluded delete_after_run is replaced with the reason the failed-run auto-pause cannot cover a cancel that lands before the payload starts. The PR's new test is the evidence that the old exclusion was wrong — justified
  4. A new test covers a cancel that lands before the run starts, for both delete_after_run values, and the comment on the existing mid-run test is reworded — justified

[FIRST-PRINCIPLES-REVIEWED] 8786eb3

Description read

[DESCRIPTION-READ] d40f4c998366d0a7d4d01ae2aac9bd796905e6a4223955548ac75c4103e6772b

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 28, 2026
@bolichen97
bolichen97 enabled auto-merge (squash) October 28, 2026 05:04
@ykharkov

Copy link
Copy Markdown
Contributor Author

How this bug was found: a Lean counterexample

The promise under test: once a user cancels a one-shot job, no scheduler tick dispatches it again. A Lean 4 model of the one-shot lifecycle (dispatch, fire-time gate, run, cancel, reap, store merge, each event one atomic step) states it as a theorem. For the current code it is false for delete_after_run jobs, and Lean checks a concrete counterexample:

/-- What a trace ends with: payloads started, dispatches, and whether the user cancelled. -/
def outcome (fixed : Bool) (dar : Bool) (es : List Ev) : Option (Nat × Nat × Bool) :=
  (run fixed (init dar) es).map (fun t => (t.payloads, t.dispatches, t.userCancelled))

theorem bug_cancel_in_gate_runs_anyway : outcome false true cancelInGate = some (1, 2, true) := by
  decide

outcome false true runs the trace on the current code (fixed = false) for a delete_after_run job (dar = true). The result (1, 2, true) means the payload ran once, the job was dispatched twice, and the user had cancelled it. decide has Lean evaluate the model on the trace, so the counterexample is machine-checked; it uses only the propext axiom.

The counterexample:

/-- A user cancel that lands while the fire-time gate is awaited: `run_never_started` is set,
    so the finally does not auto-pause, and cancel() does not park a delete_after_run job. The
    next tick dispatches it again and its payload runs, after the user cancelled it. -/
def cancelInGate : List Ev :=
  [tick, firstStep, enterExecute, cancelTake, cancelFinish, runFinally,
   tick, firstStep, enterExecute, gatePass]
# Event What happens
1 tick The scheduler dispatches the due one-shot.
2 firstStep The run task starts.
3 enterExecute The run enters _execute and waits in the fire-time gate.
4 cancelTake The user clicks Cancel run; cancel() takes the run's claim.
5 cancelFinish cancel() records "Cancelled by user" but does not park the job: it parks only a one-shot that is not delete_after_run.
6 runFinally The run's finally: the payload never started, so no failed-run auto-pause.
7 tick The job is still enabled and its time has passed, so it is dispatched again.
8-10 firstStep, enterExecute, gatePass The new run passes the gate and the payload runs.

In the model, cancel's park decision is cancelParks fixed s := fixed || !s.dar, the counterpart of park_one_shot = job.schedule.kind == "at" and not job.delete_after_run in run_teardown.py. This PR removes and not job.delete_after_run. In the model's fixed version, Lean proves the promise for every interleaving (fixed_cancel_is_final: after a user cancel, no tick dispatches the job again, in memory or after a restart) and that this trace stops at its second tick. That fixed version also parks a reaped delete_after_run one-shot, which this PR leaves to #9038. The trace was then reproduced on the real CronService (see Manual verification). The model is not part of this PR.

@ykharkov

Copy link
Copy Markdown
Contributor Author

@bolichen97 thanks for the review. The only red check left is Internal Content Scan, and it has no verdict: its run was cancelled before the scan finished (https://github.com/kirodotdev/KiroCrew/actions/runs/40630087615). I have read-only access, so I can't re-run it. Could you re-run that job? PR Readiness should pick up the result on its next sweep.

@iamwhatever
iamwhatever disabled auto-merge October 29, 2026 15:52
@iamwhatever
iamwhatever merged commit 7939d9c into kirodotdev:main Oct 29, 2026
116 of 133 checks passed
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cron: a cancelled delete_after_run one-shot runs again when the cancel lands before its payload starts

3 participants