Skip to content

fix(apps): write an app's cron manifest atomically - #18850

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/app-crons-manifest-is-written-atomically
Oct 10, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/app-crons-manifest-is-written-atomically

Conversation

@javenciu

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: a write of an app's cron definitions that is cut off part way leaves the previous definitions in place, and a file that cannot be read is reported rather than dropped silently.

An app's cron definitions are saved to app-crons.json in its install directory by _register_crons (src/kiro_crew/apps/bridges.py:1470) when the app is installed or updated. At every start, register_app_crons_with_service (called from src/kiro_crew/apps/hooks_integration.py:833) reads a third-party app's crons only from that file, through load_app_cron_defs (bridges.py:1502; the read is at bridges.py:1617 on this branch).

The root cause is the writer: _register_crons saved the file with path.write_text(...) (bridges.py:1485 on main), which truncates the file and then writes it. A crash part way through leaves a truncated file. A disk that fills mid-write leaves one too, while the old definitions are already gone. load_app_cron_defs then read a truncated file as "no crons" with no log line (except (json.JSONDecodeError, OSError): return [], bridges.py:1527 on main). A file that was not valid UTF-8 raised UnicodeDecodeError, which that guard did not catch.

Why it matters

The app's scheduled jobs stop running at that start and every start after it, until the app is reinstalled. Nothing in the log says why. Every other app-state writer in src/kiro_crew/apps/manager.py already uses atomic_write (for example manager.py:292 and manager.py:354).

Not a goal

  • Durability across a power loss. Like the other app-state writers, this write does not fsync. config.json (write_config_atomically, src/kiro_crew/config/loader.py:1666) also skips fsync by default, and src/kiro_crew/security/redaction_allow.py:156 replaces its file without one. Both survive a process crash, and neither is changed here.
  • Other boot-time files. config.json and the webhook stores (write_json_atomic, src/kiro_crew/webhooks.py:197) are already written atomically, and the other writers in bridges.py already use atomic_write.
  • The "no crons" result for an unreadable file. It stays the reader's documented fallback (the load_app_cron_defs docstring); this change only makes it visible.

What changed

  • Root cause: _register_crons truncated and rewrote app-crons.json in place. It now writes through atomic_write (bridges.py:1487), which writes a temp file in the same directory and renames it over the old one. A write cut off part way leaves the previous file untouched.
  • load_app_cron_defs now catches JSONDecodeError, UnicodeDecodeError and OSError and logs a warning that names the file (bridges.py:1533), the way its non-list branch already does. It still returns no crons.
  • A missing file is unchanged: no crons, and no warning.
  • Helper reused: kiro_crew.atomic_write.atomic_write, already imported in bridges.py. No new helper.
  • Tests, in test/test_app_bridges.py (TestCronRegistration):
    • test_an_unreadable_cron_manifest_is_reported_not_dropped_silently[truncated] and [not-utf8] write the file through the real _register_crons, cut it, and read it through the real loader.
    • test_a_rewrite_that_fails_part_way_keeps_the_previous_manifest injects a full disk after half the bytes into whichever primitive puts them down (Path.write_text before the fix, atomic_write's _write_all after), so the same fault drives both versions.
    • test_a_missing_cron_manifest_is_no_crons_without_a_warning is the control for the unchanged missing-file contract.

Backwards compatibility

Compatible: an intact file is written and read exactly as before, and the loader still returns no crons for an unreadable file.
Removes nothing: no setting, field or event is removed.
Reader: an unreadable app-crons.json now produces one warning naming the file.
Security: no redaction, auth, sandbox or permission code changes. The file holds cron definitions, not credentials. It is written with the umask-based default mode, as a newly created file was before; because the rename creates a new file, a mode someone set on the old file by hand is not carried over.

Tests

Red on unchanged origin/main at d32c084 (a detached checkout with only test/test_app_bridges.py copied in, the whole file; 3 serial runs, the same each time):

FAILED test/test_app_bridges.py::TestCronRegistration::test_an_unreadable_cron_manifest_is_reported_not_dropped_silently[truncated] - AssertionError: an unreadable cron manifest drops the app's crons without a...
FAILED test/test_app_bridges.py::TestCronRegistration::test_an_unreadable_cron_manifest_is_reported_not_dropped_silently[not-utf8] - UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: inv...
FAILED test/test_app_bridges.py::TestCronRegistration::test_a_rewrite_that_fails_part_way_keeps_the_previous_manifest - AssertionError: assert [] == [{'agent': 'm...nd': '', ...}]
============= 3 failed, 219 passed, 2 skipped, 2 warnings in 7.61s =============

The controls pass on both sides: an intact file registers its crons (test_register_crons, test_register_crons_persists_enabled_flag, test_register_crons_serializes_all_fields), and a missing file is no crons with no warning.

Green on the branch (rebased onto d32c084): test/test_app_bridges.py 222 passed, 2 skipped, in each of 3 serial runs. The neighbour files, every test/test_apps*.py and every test file that names kiro_crew.apps.bridges (65 files), give 5539 passed and 19 skipped. mypy, flake8, isort, black, scripts/check_black_formatting.py, scripts/docs-lint.sh and the comment-history check with COMMENT_HISTORY_BASE_REF=origin/main exit 0.

Manual verification

Not run against a live gateway.

Related Issues

None. Open PRs that edit src/kiro_crew/apps/bridges.py (#17655, #15394, #6600) do not touch _register_crons, load_app_cron_defs or the cron manifest.

Pattern harvest

Rule candidate: semgrep
Pattern: a write_text or open(..., "w") on a file the gateway reads at every start, outside atomic_write; and a reader of such a file whose except returns an empty result without logging.

Checklist

  • Regression tests fail before the fix and pass after
  • Gates pass locally
  • No unrelated changes

An app's cron definitions are saved to app-crons.json in its install
directory, and the scheduler reads them back at every start. The
writer, _register_crons, used Path.write_text, which truncates the file
and then writes it. A crash or a full disk part way through left a torn
file, and load_app_cron_defs read a torn file as no crons without a
log line, so the app's scheduled jobs were never registered again.

The writer now uses atomic_write, so a write cut off part way leaves
the previous definitions in place. load_app_cron_defs logs a warning
naming the file when it is present but cannot be read, the way it
already does for a file that is not a JSON array, and it also catches
a UnicodeDecodeError, which escaped the guard before. A missing file is
still no crons, without a warning.
@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 10, 2026 12:44
@javenciu
javenciu requested a review from cruisercohen October 10, 2026 12:44
@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 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

Revision 7db498ac56bc923d0ac54c1e399f7fa2537b4f08 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

Copy link
Copy Markdown
Contributor

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

Premise-level review of 7db498ac56bc923d0ac54c1e399f7fa2537b4f08 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: PASS

Nothing to check.

What this change ships

Inventory (3 items) — 3 justified

Intent: A FIX, so that an app's scheduled jobs survive a save that gets cut off part way, and a cron file that can't be read gets logged instead of dropped without a word.

  1. When an app is installed or updated, its app-crons.json is now written atomically, so a write that is cut off keeps the previous definitions — justified
  2. A cron manifest that can't be read now logs a warning naming the file instead of silently registering no crons — justified
  3. A manifest that isn't valid UTF-8 now falls back to no crons, where before it raised an uncaught UnicodeDecodeError — justified

[FIRST-PRINCIPLES-REVIEWED] 7db498a

Description read

[DESCRIPTION-READ] 53d956e389288ee25ecadee2473af003d36feb7e5ad7167d9dd44c1446705afd

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

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 7db498ac56bc923d0ac54c1e399f7fa2537b4f08 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

Nothing to check.

[DESIGN-REVIEWED] 7db498a

Description read

[DESCRIPTION-READ] 750ab0955aa2fcbb8eed61fe8223a327b4f218d503b266debcda2383bd25273e

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

Opus 5.5 Review (fork) — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 7db498a

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

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

Review details

No findings.
[GPT-REVIEWED] 7db498a

@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 10, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This fixes app-crons.json being cut off by a crash or a full disk during the write in _register_crons. When that happened, the app's scheduled jobs silently disappeared at every later start. The root cause is fixed: the writer now goes through the existing atomic_write (temp file plus rename) instead of Path.write_text, so an interrupted rewrite leaves the old file in place. load_app_cron_defs now also catches UnicodeDecodeError and logs a WARNING naming the file instead of returning [] with no message. I read the full diff and checked atomic_write on main. _write_all receives the destination path on the POSIX path, so the test's injected half-write and ENOSPC really drives the new code. Without the fix, the half-write leaves a cut-off file and the test fails. The truncated and non-UTF-8 parametrized tests and the missing-file control cover the reader. File mode is still umask-based, the same as write_text. Both AI lanes PASS, base is main, all checks are green, there are no change requests, and the change is small (+13/-2 prod). — crew-pr-reviewer

@iamwhatever
iamwhatever enabled auto-merge (squash) October 10, 2026 19:06
@iamwhatever
iamwhatever merged commit db47e38 into kirodotdev:main Oct 10, 2026
103 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 10, 2026
@javenciu
javenciu deleted the fix/app-crons-manifest-is-written-atomically branch October 11, 2026 04:44
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.

2 participants