Repository navigation
fix(apps): write an app's cron manifest atomically - #18850
iamwhatever merged 1 commit into
Conversation
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.
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (3 items) — 3 justifiedIntent: 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.
[FIRST-PRINCIPLES-REVIEWED] 7db498a Description read[DESCRIPTION-READ] 53d956e389288ee25ecadee2473af003d36feb7e5ad7167d9dd44c1446705afd That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of 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 |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
iamwhatever
left a comment
There was a problem hiding this comment.
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
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.jsonin 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 fromsrc/kiro_crew/apps/hooks_integration.py:833) reads a third-party app's crons only from that file, throughload_app_cron_defs(bridges.py:1502; the read is atbridges.py:1617on this branch).The root cause is the writer:
_register_cronssaved the file withpath.write_text(...)(bridges.py:1485on 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_defsthen read a truncated file as "no crons" with no log line (except (json.JSONDecodeError, OSError): return [],bridges.py:1527on main). A file that was not valid UTF-8 raisedUnicodeDecodeError, 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.pyalready usesatomic_write(for examplemanager.py:292andmanager.py:354).Not a goal
config.json(write_config_atomically,src/kiro_crew/config/loader.py:1666) also skips fsync by default, andsrc/kiro_crew/security/redaction_allow.py:156replaces its file without one. Both survive a process crash, and neither is changed here.config.jsonand the webhook stores (write_json_atomic,src/kiro_crew/webhooks.py:197) are already written atomically, and the other writers inbridges.pyalready useatomic_write.load_app_cron_defsdocstring); this change only makes it visible.What changed
_register_cronstruncated and rewroteapp-crons.jsonin place. It now writes throughatomic_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_defsnow catchesJSONDecodeError,UnicodeDecodeErrorandOSErrorand logs a warning that names the file (bridges.py:1533), the way its non-list branch already does. It still returns no crons.kiro_crew.atomic_write.atomic_write, already imported inbridges.py. No new helper.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_manifestinjects a full disk after half the bytes into whichever primitive puts them down (Path.write_textbefore the fix,atomic_write's_write_allafter), so the same fault drives both versions.test_a_missing_cron_manifest_is_no_crons_without_a_warningis 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.jsonnow 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/mainat d32c084 (a detached checkout with onlytest/test_app_bridges.pycopied in, the whole file; 3 serial runs, the same each time):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.py222 passed, 2 skipped, in each of 3 serial runs. The neighbour files, everytest/test_apps*.pyand every test file that nameskiro_crew.apps.bridges(65 files), give 5539 passed and 19 skipped. mypy, flake8, isort, black,scripts/check_black_formatting.py,scripts/docs-lint.shand the comment-history check withCOMMENT_HISTORY_BASE_REF=origin/mainexit 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_defsor the cron manifest.Pattern harvest
Rule candidate: semgrep
Pattern: a
write_textoropen(..., "w")on a file the gateway reads at every start, outsideatomic_write; and a reader of such a file whoseexceptreturns an empty result without logging.Checklist