Skip to content

fix(cron): delete run history on every caller-requested removal - #19010

Merged
javenciu merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/cron-remove-deletes-history
Oct 13, 2026
Merged

javenciu merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/cron-remove-deletes-history

Conversation

@javenciu

@javenciu javenciu commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Removing a cron job anywhere except the dashboard leaves its run history on disk. cron-history/<job id>.jsonl and the job's rows in cron-history/_index.jsonl stay behind when one of these removes a job:

  • the MCP cron_remove tool (src/kiro_crew/mcp_cron.py:3677);
  • the MCP cron_remove_all tool (src/kiro_crew/mcp_cron.py:3693), which removes every job the calling session owns through remove_jobs_sync (:3715);
  • kirocrew cron remove, through remove_job (src/kiro_crew/cli_commands.py:2292, :2295);
  • a chat command, through remove_jobs (src/kiro_crew/messaging/commands.py:961) and remove_job_async (:998);
  • the apps SDK, through remove_job_async (src/kiro_crew/apps/cron_sdk.py:633).

Only the two dashboard delete routes call delete_job_history, after the service has removed the job (src/kiro_crew/dashboard/handlers/cron.py:1025 at 2ada192 and :1091). git grep -n delete_job_history -- src finds no other caller.

Goal: a removal that a caller asks for deletes the job's run history, whichever surface asked, and a dashboard delete still deletes it exactly once.

Why it matters

Each removed job leaves one file that nothing reads again and nothing deletes. rotate_all (src/kiro_crew/cron_history.py:717) trims each file to the per-job cap but never deletes one, so the directory grows by one file per removed job for the life of the install. Agents create and remove jobs through cron_remove routinely, which is the path that leaks.

Not a goal

  • Automated one-shot removals. These are the delete_after_run consume in CronService._merge_job_result, the gateway's Done removal (a remove_job_async call with one_shot_path="cron_gateway", src/kiro_crew/slack/gateway.py:3164) and the deferred drain (_drain_pending_removals_locked). They keep their history, as they do today. A delete there would not hold anyway. The run path consumes the job in _merge_job_result (src/kiro_crew/cron.py:3980). It appends that run's record afterwards (:4011), and the gateway's removal runs inside on_job, before both. That append writes the file again. Changing this needs a product answer first: should a one-shot that removed itself keep its last run record in History?
  • remove_jobs_by_owner. An app's teardown (src/kiro_crew/apps/bridges.py:1908) and the SDK's remove-all (src/kiro_crew/apps/cron_sdk.py:763) remove every job an app owns. Whether an uninstalled app's run history stays is a decision for the app lifecycle, so it is left out of this change. Its synchronous sibling, remove_jobs_by_owner_sync, has no caller.
  • Cleaning up history files that removed jobs have already left behind.

What changed

The fix moves the delete out of the dashboard routes and into the service's removal seam. That is the one place every caller-requested removal passes through, so no surface can skip it.

  • CronHistoryStore.delete_job_history_sync (new, src/kiro_crew/cron_history.py) is the synchronous sibling of delete_job_history. It has the same enabled check, and an OSError degrades the store as before.
  • CronService._delete_history_of (new, src/kiro_crew/cron.py) deletes the history of the removed ids. It is best-effort, the same as the audit: the jobs are already gone from the store, so a failure is logged and never fails the removal.
  • CronService.remove_job and remove_jobs_sync call the helper inline. Both are loop-less only: the store lock's guard refuses them on a running loop. remove_job_async, remove_jobs and remove_job_if_async (the dashboard card Undo's compare-and-delete, which main added in feat: let crewmates propose changes the user confirms as cards #18448) call it through asyncio.to_thread. Each skips the helper when nothing was removed. remove_job and remove_job_async also skip it when one_shot_path is set.
  • api_cron_delete and the batch delete route (src/kiro_crew/dashboard/handlers/cron.py) drop their own delete_job_history call, so the dashboard deletes once.

Backwards compatibility

Compatible: the dashboard's responses and status codes are unchanged. A failing history store logs a warning, which is what the batch route already did, and no longer turns a completed single delete into a 500. One-shot removals behave as before. One behaviour changes outside the dashboard: removing a job from the CLI (kirocrew cron remove), the cron MCP server (cron_remove, cron_remove_all), a chat command or the apps SDK now deletes its run history too, which main kept; the dashboard already deleted it, so every caller-requested removal now does the same.

Removes nothing: no endpoint, config key or public method is removed. The two routes stop calling delete_job_history because the service makes that call.

Tests

test/test_cron_remove_deletes_history.py (new):

  • test_remove_job_deletes_the_history
  • test_remove_job_async_deletes_the_history
  • test_remove_job_if_async_deletes_the_history, the dashboard card Undo's compare-and-delete path
  • test_control_a_refused_compare_and_delete_keeps_the_history, a control: a compare-and-delete with a stale expected revision returns "changed" and leaves the job, its history file and its index rows in place
  • test_cron_remove_tool_deletes_the_history
  • test_remove_jobs_deletes_the_history
  • test_cron_remove_all_tool_deletes_the_history, which drives the real cron_remove_all tool and checks that another session's job keeps its history
  • test_a_failed_history_delete_does_not_fail_the_removal
  • test_an_automated_one_shot_removal_keeps_the_history
  • test_control_the_dashboard_route_deletes_the_history_once, which drives the real api_cron_delete against a real CronService and counts deletes.

The dashboard handler tests in test/test_dashboard_cron_delete_handler.py and test/test_dashboard_cron_batch_delete_handler.py are updated so the routes no longer delete history. Two tests are deleted, each replaced by test_route_does_not_touch_the_history_store in the same file:

  • test_dashboard_cron_delete_handler.py::test_audit_lands_before_history_cleanup expected a history-store failure after the route's own delete to answer 500, with the service's audit already written. The route no longer calls the history store, so its replacement checks that a failing store gives 200 and is never called. On the service side, test_a_failed_history_delete_does_not_fail_the_removal checks that a failing delete does not fail the removal. The audit still runs on that path because _delete_history_of catches every exception before _audit_requested_removal runs, but test/test_cron_remove_deletes_history.py does not assert the audit.
  • test_dashboard_cron_batch_delete_handler.py::test_history_cleanup_failure_still_counts_as_deleted expected a history failure on one id to leave both ids reported as deleted and the refresh pushed. Its replacement checks the same response with a failing store that the route never calls. On the service side, _delete_history_of catches a failure per id, so the remaining ids are still deleted; no test covers a batch in which one id's delete fails.

The two card-Undo tests test/test_change_cards.py::test_schedule_card_undo_through_the_hook_refuses_a_delete_raced_by_another_tab and test/test_change_cards_identity.py::test_crewmate_create_cron_undo_refuses_a_schedule_edited_in_the_window now spy on delete_job_history_sync, the call the service makes, instead of the async delete_job_history, which nothing in src/ calls on this branch, so their refused-Undo history_deletes == 0 asserts still check something.

Red, on origin/main 2e2bd06 with only the new test file copied in, 3 of 3 serial runs, each:

FAILED test_remove_job_deletes_the_history - AssertionError: assert not True
FAILED test_remove_job_async_deletes_the_history - AssertionError: assert not True
FAILED test_remove_job_if_async_deletes_the_history - AssertionError: assert not True
FAILED test_cron_remove_tool_deletes_the_history - AssertionError: assert not True
FAILED test_a_failed_history_delete_does_not_fail_the_removal - AttributeError: <CronHistoryStore ...> (delete_job_history_sync is new)
FAILED test_remove_jobs_deletes_the_history - assert not True
FAILED test_cron_remove_all_tool_deletes_the_history - AssertionError: cron_remove_all removed this session's jobs and left their history: [...]
7 failed, 3 passed

The three that pass on main are the two controls (the refused compare-and-delete and the dashboard route) and the one-shot test. With every change except the one to remove_jobs_sync, the file gives 1 failed, 9 passed, in 3 of 3 runs: only the remove-all test fails, with cron_remove_all removed this session's jobs and left their history: [...].

Green: the three changed test files give 27 passed in each of 3 runs, and with the two card-Undo files they give 263 passed in each of 3 runs. The new file plus every test file that names remove_job, delete_job_history, cron_history or cron_remove_all, with test/test_change_cards.py (the compare-and-delete's own tests), 53 files including test/test_cli.py and test/test_change_cards_identity.py: 6915 passed, 35 skipped. mypy, flake8, isort, black, scripts/check_black_formatting.py, scripts/check_subprocess_encoding.py, scripts/docs-lint.sh and scripts/check_comment_history.py (base origin/main) all pass.

The rebase onto 098df01 moved the three lines the Not a goal section cites. On this branch the gateway's Done removal (remove_job_async) starts at src/kiro_crew/slack/gateway.py:3194, the run path calls _merge_job_result at src/kiro_crew/cron.py:4332, and it appends the run record (self._history.append) at :4368.

Manual verification

None beyond the tests; the change has no UI. The reproducer above was also run three times serially on main, failing each time.

Related Issues

None found. No open issue or PR matches delete_job_history, cron history orphans or remove_job history. Two closed PRs matched the searches and are unrelated: #16979 and #10668 (history readers).

Pattern harvest

Rule candidate: a side effect of removing a record (its history, its files) belongs in the service's removal seam, not in one caller's route; any surface that calls the seam directly otherwise skips it. Check with git grep that the side effect has exactly one caller and that it is the seam.

Checklist

  • Red on main, green with the fix
  • Neighbour test files, including test/test_cli.py
  • mypy, flake8, isort, black, black baseline gate, docs-lint, comment-history
  • No blocking file I/O on the event loop (remove_job and remove_jobs_sync are loop-less only; the async variants use asyncio.to_thread)
  • Dashboard delete deletes the history exactly once

@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 11, 2026 02:12
@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 11, 2026
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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

Revision 83bbe64dc06a0f1cb9612c86044996633697c179 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 83bbe64dc06a0f1cb9612c86044996633697c179 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check: if someone removes a job while it is still running, the run's later record append re-creates the history file the new seam just deleted.

[DESIGN-REVIEWED] 83bbe64

Description read

[DESCRIPTION-READ] 563b7957cbd60d5a47a6222ab07051cbb7387cc1474c9b699261d5e947d15524

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

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of 83bbe64dc06a0f1cb9612c86044996633697c179 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/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The leak is justified on symmetry with the dashboard. Its premise, "nothing reads again", is wrong: the global History view reads the deleted index rows. It also leaves an async twin with zero callers.

Not justified as shipped

  • Item 1 — inherited: "Each removed job leaves one file that nothing reads again" is wrong. get_all_history (cron_history.py:649-675) reads _index.jsonl rows for the global History list, and _max_index_records already caps that file. What's left is the description's own reason: "the dashboard already deleted it, so every caller-requested removal now does the same". That is the symmetry form.
    Clears when: a reported disk-growth defect or a measured per-job file cost is linked, or a maintainer confirms that an agent's cron_remove should erase runs from History.
  • Item 4 — duplicate of CronHistoryStore.delete_job_history (cron_history.py:740): once this lands, grep delete_job_history src counts 0 callers of the async method, down from 2. Both are public spellings with the same enabled/degrade body.
    Subtraction: delete the async delete_job_history and keep the sync one. Or delete the new public method and have _delete_history_of call one existing method.
    Clears when: one spelling remains.

What this change ships

Inventory (5 items) — 3 justified

Intent: removing a cron job from any surface also cleans up its run history. FIX.

  1. A cron removed via CLI, MCP, chat or apps SDK vanishes from History — inherited
  2. A successful dashboard card Undo now erases the job's run history — justified
  3. A dashboard delete with a failing history store answers 200, not 500 — justified
  4. New public sync history-delete, with its async twin left callerless — duplicate of cron_history.py:740
  5. The dashboard routes stop deleting history themselves; the service does it once — justified

[FIRST-PRINCIPLES-REVIEWED] 83bbe64

Description read

[DESCRIPTION-READ] 46dfa0837380e9aa369a045fc0635d8b9a80996416bff32d15ba0ec9869d672c

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 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

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

Review details

No findings that block the merge.

FINDING — src/kiro_crew/cron.py:2365 — the service now runs await asyncio.to_thread(self._delete_history_of, [job_id]) before self._audit_requested_removal(...) (the same order is in remove_job, remove_job_if_async, remove_jobs and remove_jobs_sync). This reverses the order that docs/system-specs/modules/learn-cron-dashboard.md:3223 records ("the audit lands before history cleanup"). The spec line also still says the batch route purges history per job, and this PR leaves the spec untouched → Fix: call _delete_history_of after _audit_requested_removal / _audit_requested_batch_removal in all five methods, and update the spec line to say the service, not the route, deletes the history.

[OPUS-REVIEWED] 83bbe64

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

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

Review details

No findings.
[GPT-REVIEWED] 83bbe64

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 11, 2026
@javenciu
javenciu force-pushed the fix/cron-remove-deletes-history branch from bc72597 to 3d405cf Compare October 11, 2026 07:35
@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 11, 2026
@javenciu
javenciu force-pushed the fix/cron-remove-deletes-history branch from 3d405cf to 1d6c93f Compare October 12, 2026 13:21
@javenciu

Copy link
Copy Markdown
Contributor Author

Rebased onto main 098df01 with no change to the patch (the patch-id is the same). Line references in the description's frozen sections moved with main: mcp_cron.py:3677, :3693 and :3715 are now mcp_cron.py:3682, :3698 and :3720; cli_commands.py:2292 and :2295 are now cli_commands.py:2351 and :2354; apps/cron_sdk.py:633 and :763 are now apps/cron_sdk.py:639 and :769; apps/bridges.py:1908 is now apps/bridges.py:1914; cron_history.py:717 is now cron_history.py:720. The Tests section gives the current lines for the three that the Not a goal section cites.

Removing a cron job through the MCP cron_remove or cron_remove_all
tool, the CLI, a chat command or the apps SDK left
cron-history/<job id>.jsonl and the job's index rows behind. Only the
two dashboard delete routes called delete_job_history, after the
service had removed the job.

CronService.remove_job, remove_job_async, remove_jobs and
remove_jobs_sync delete the removed jobs' history themselves through
one best-effort helper, which runs in a worker thread for the async
variants. The dashboard routes drop their own call, so a dashboard
delete still deletes once. Automated one-shot removals keep their
history: the run path appends that run's record after the removal.
@javenciu
javenciu force-pushed the fix/cron-remove-deletes-history branch from 1d6c93f to 83bbe64 Compare October 12, 2026 20:01
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@javenciu
javenciu enabled auto-merge (squash) October 12, 2026 23:04
@iamwhatever

Copy link
Copy Markdown
Contributor

This PR moves run-history deletion out of the two dashboard delete routes and into CronService's removal seam (remove_job, remove_job_async, remove_job_if_async, remove_jobs, remove_jobs_sync) through a best-effort delete_history_of, so the MCP cron_remove/cron_remove_all tools, kirocrew cron remove, chat commands and the apps SDK delete history too. One-shot removals (one_shot_path set) and remove_jobs_by_owner keep their history. The code is correct as written: I checked every remove* caller in src, and the only automated one (the gateway Done removal at slack/gateway.py:3194) passes one_shot_path and is skipped. History failures are caught, so they never fail a completed removal or lose the audit row, and the dashboard now deletes exactly once. What decides the merge is a product question the First Principles lane raised and nobody answered: the leaked rows are not dead data, because get_all_history reads _index.jsonl for the global History view. So after this change, an agent calling cron_remove (or an app removing its own job) erases that job's past runs from the user's History, where today only a human dashboard delete does. Maintainer: should an agent- or app-initiated removal erase user-visible run history, or should only a human delete do that, with the disk growth handled by a retention sweep instead? If the answer is yes, also update docs/system-specs/modules/learn-cron-dashboard.md:3223, which still says the route purges history and that the audit lands before history cleanup (Opus lane finding). — crew-pr-reviewer (deep re-check)

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tier 1 auto-approve: fix (8 files). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, security checklist all-NO, no reviewer-lane findings, readiness signal fresh for this head SHA, declared category matches the diff. Category: every caller-requested cron removal deletes the job's run history in the store, not only the dashboard routes; automated one-shot removals keep it; tests added.

@javenciu
javenciu merged commit ed556b3 into kirodotdev:main Oct 13, 2026
105 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 13, 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.

3 participants