Repository navigation
fix(history): sync rotation and compaction replacements, tidy failed archives - #20060
jeeshofone wants to merge 2 commits into
Conversation
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the fix is sound and proportionate. Staging and syncing outside the lock, then publishing only if the bytes match, keeps the old lock-hold pin while adding durability. Notes
[DESIGN-REVIEWED] a831312 Description read[DESCRIPTION-READ] f77b3d3315b610d13bb365560d026ac653d314dc70f40a4ac09f5094a4aeba86 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 |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. One gap: the description claims a Notes
What this change shipsInventory (9 items) — 8 justifiedIntent: a power loss or a lasting disk fault can no longer lose or duplicate a conversation's rows. FIX.
[FIRST-PRINCIPLES-REVIEWED] a831312 Description read[DESCRIPTION-READ] f5c946fa1d7c439f5de29580e46880bc876f8c567f6b904df606d2ccb7018898 That sha256 names the PR description this verdict read. Recompute it with |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
|
Dispositions for head fbbfbef (review of 5b6adeb plus the First Principles note on 5b6adeb). SHOULD-FIX A (metadata-only rewrites replace the whole transcript unsynced): fixed. New SHOULD-FIX B (channel migration deletes the orphan after an unsynced merge): fixed. NIT C ( NIT D (macOS NIT E (best-effort removal): fixed. The history spec says the removal is best effort, a failed removal is logged, and the sync error is still raised. First Principles note (rotation's
Rotation itself already reads and rewrites up to 10 MB under that lock. The alternative is a power loss returning an empty transcript, so the conversation is lost rather than a marker. A slow disk raises both columns, but it does not change which side of the trade is right. A sidecar for the marker would avoid the whole-file rewrite, but it changes the format and every Mutations, each KILLED on fbbfbef:
The 5b6adeb mutations stay killed. First-pass check: R1-R8 OK. |
|
Correction to the first-pass line in the comment above, for head fbbfbef. Two R8 gaps remain:
Everything else in R1-R8 holds:
|
fbbfbef to
fff1062
Compare
|
Dispositions for head fff1062 (lanes on fbbfbef). GPT 6.1 F1 ( Design CONCERNS (fsync under the session lock on every metadata write, reversed
Rotation, compaction, PR Hygiene (take-away Reader lines, body over 8,000 B): fixed. E2E (stub ACP backend, offline) on fbbfbef: not ours. Local on fff1062:
First-pass check: R1-R8 OK. R3.1 (other writers): this round's new state is the staged file and the snapshot. The lock-held byte comparison checks them against every other writer and so plays the compare-and-swap role. The interleaving test pins it. |
fff1062 to
c969570
Compare
|
New head c969570, rebased onto current main (d06b611). Content change is wording only.
First-pass check: R1-R8 OK. The PR body is unchanged, since no claim in it changed. |
c969570 to
67ff65a
Compare
|
Dispositions for head 67ff65a (lanes on c969570). GPT 6.1 F1 ( Pinned by First Principles item 3 ( SAST (Semgrep) on c969570: infra. The job hit its 15-minute execution limit, and this head re-runs it. Locally, Semgrep 1.78.0 with Earlier rulings for this PR are unchanged and still in: the whole-transcript writes go through Local on 67ff65a:
First-pass check: R1-R8 OK. R0.3: both new invariant lines (the in-region close, the guarded cleanup close) are mutation-pinned above. R1: the staging change subtracts a duplicate mechanism ( |
67ff65a to
9502aa6
Compare
|
HOLD at 06789c5. The fix for @bolichen97's blocking finding looks right to me. Once Blockers:
What clears it: @bolichen97 re-reviews the fix and approves or dismisses the request, and a maintainer approves the deletion paths. — crew-pr-reviewer (r123) |
06789c5 to
4f7b520
Compare
4f7b520 to
089a815
Compare
|
|
|
|
HOLD at 089a815. I found no defect on this head. The fix for @bolichen97's finding is still in place: once Blockers:
What clears it: @bolichen97 approves or dismisses the request, and a maintainer approves the deletion paths. — crew-pr-reviewer (r131) |
089a815 to
9531268
Compare
…cements Rotation, compaction, clear_closed, _update_metadata_locked, the selection rollback and the channel transcript migration merge now replace the whole transcript synced (history_rewrite.rewrite_transcript). The three hot metadata writes (mark_consolidated, the sub-offset write, update_metadata) stage and sync the replacement before taking the session lock and rename under it only if the file is unchanged (rewrite_metadata_line). A failed archive write or sync removes its file, and archive_dedup is required. Rebased onto main after kirodotdev#21014 split history.py: the _archive_lines change lands in history_files.py and the marker writes in history_marker.py; the TestReducedLockHold pins move to test_history_locking_remediation_locked_section.py after kirodotdev#21783. Co-authored-by: iamwhatever <iamwhatever@users.noreply.github.com>
…ebuild GPT 6.1 on 185dd9b: when a write lands between staging and the lock, rewrite_metadata_line kept the stale staged file until its finally while rewrite_transcript wrote a second whole copy, so a disk with room for one replacement failed the edit with ENOSPC. Discard it first. Pinned by test_the_fallback_frees_the_stale_staged_copy_before_writing (3 writes; red without the fix). Also keep atomic_write and replace_with_retry on the history_projection facade: test_campaign_facades_base_public_names pins every public name the base facade served.
The next push is a rebase onto main only. Main removed the page-index carry after first-line rewrites (the index is now checked by content), so the carry calls this PR had added in |
|
9531268 to
a831312
Compare
|
Moved to #27514: same commits, now on a branch of this repo so CI uses the CodeBuild fleet. Review history stays here. |
Problem / Motivation
Goal: A power loss during any rewrite that replaces a whole transcript cannot lose the rows that file keeps, the channel transcript migration cannot delete the only other copy of a conversation before its merge is on disk, and a lasting archive fault no longer leaves one archive copy per retry. The hot metadata writes pay for that sync without holding the session lock through it.
From a post-merge review of #19362.
delete_session'd the orphan, which held the only other copy._archive_linesleft its file behind when its sync failed, one copy per retry._frozen_prefix_and_foreign_appendsacceptedarchive_dedup=None, dropping dedup lines unarchived.Why it matters
These run unattended, and
mark_consolidatedruns on every consolidation pass, so its lock hold matters as much as its durability.Not a goal
F_FULLFSYNCon macOS: repo-wideatomic_writepolicy.clear_closedand the_update_metadata_lockedcallers keep the sync under the lock: their output, guard or after-commit step needs it, and all are far rarer than the three hot paths.What changed (motivation → approach → change)
history_rewrite.rewrite_transcript(path, content):atomic_write(..., fsync=True)with nofsyncparameter. Rotation, compaction,clear_closed,_update_metadata_locked, the selection rollback and the migration merge use it.history_rewrite.rewrite_metadata_line(log, key, build), formark_consolidated, the sub-offset write andupdate_metadata:buildruns on an unlocked read; the result is staged and synced (atomic_write.stage_replacement) before the lock. Under the lock it is renamed over only if the path for the key and the file's bytes are exactly whatbuildread (no stat shortcut: these writers restore the old mtime). Any difference discards the staged file and rebuilds under the lock, so nothing that landed in between is lost. On that fallbackbuildruns twice and the sync runs under the lock, andmark_consolidatedand the sub-offset write log their messages twice when an append lands in between (accepted). The directory is synced after release, best effort. An interrupt afterpublishedis set and beforepublish_replacemententers its owntryis caught (except BaseException), which discards the staged copy and re-raises.atomic_write._stage_tempis the one staging sequence foratomic_writeandstage_replacement. Its close runs inside the protected region after the descriptor's ownership is cleared, and a failing cleanup close is swallowed, so a failed write,fsyncor close removes the temp file. The by-name publish (publish_replacement) runs insidetry/except BaseExceptionwith_unlink_temp, so an interrupt before its owntryremoves it too.rewrite_metadata_lineand_update_metadata_lockedcallcarry_index_over_head_rewrite(fix(history): persist the transcript page index and carry it across first-line rewrites #24295), so the next history page does not frame the whole transcript again. The marker and cleared-close writes pass bytes, so a Windows write keeps LF line ends.update_metadata's merge rules move to_metadata_replacement, shared with_update_metadata_locked; it still writes bytes.channel_transcript_migration._migrate_one:fsync_dirruns right before every orphan delete, including an unchanged retry. A failed sync keeps the orphan._archive_lines: a failed write or sync unlinks the file it created before re-raising.transcript_merge.py:archive_deduprequired.atomic_write.py,history_files.py(_archive_lines),history_marker.py(the two marker writes),history_projection.py,session_agent_selection.py,history.md, plus the modules named above.Lock hold on XFS, median of 15,
mark_consolidated/update_metadata: 100 KB 3.3 / 3.4 ms → 0.2 / 0.2 ms, 1 MB 8.7 / 8.5 → 0.4 / 0.4 ms, 10 MB 46.1 / 44.9 → 2.5 / 2.4 ms (sync under the lock, fbbfbef → sync before it, 67ff65a; this locked path is 67ff65a's).Backwards compatibility
Compatible: no file format or return value changes.
Reader: src/kiro_crew/dashboard/slot_persistence/transcript_merge.py:compose_payload -- chat -- the only production caller already passes archive_dedup; test_archive_fails_closed.py::TestForeignDedupFailsClosed::test_the_save_archives_dedup_drops_through_the_same_path
Reader: src/kiro_crew/dashboard/session_control_parts/session_create.py:create_session -- chat -- update_metadata's hand-rolled unsynced temp-and-rename is replaced by rewrite_metadata_line with the same upsert and byte output; test_history_locking_remediation.py::TestReducedLockHold::test_update_metadata_still_upserts_a_missing_transcript
Reader: src/kiro_crew/history_rewrite.py:HistoryRewriteCoordinator -- chat -- rotation and compaction lose the unsynced default, not their output; test_archive_fails_closed.py::TestTheTranscriptReplaceIsSynced
Reader: src/kiro_crew/channel_transcript_migration.py:_migrate_one -- release -- _write_merged no longer syncs the directory itself; _migrate_one syncs it before every delete; TestTheChannelMigrationSyncsBeforeItDeletesTheOrphan::test_an_unchanged_retry_after_a_failed_directory_sync_keeps_the_orphan
Reader: test/test_history_locking_remediation.py:TestReducedLockHold -- chat -- the pin is restored (fbbfbef had replaced it), now asserting no file fsync under the session lock
Tests
TestReducedLockHold::test_no_file_fsync_while_the_session_lock_is_held[3 writes]: one filefsync, at lock depth 0, and one best-effort directory sync after release. Red on fbbfbef.TestReducedLockHold::test_a_write_between_the_sync_and_the_rename_is_kept[3 writes × append|edit]: the injected write survives and the staged file is removed. Red on fbbfbef.test_an_unchanged_retry_after_a_failed_directory_sync_keeps_the_orphan: red on fbbfbef.test_atomic_write_staging_cleanup.py(3 writers × close|fsync|write): no.tmpremains and the target is untouched or edited under the lock. 8 of 9 red on c969570.test_a_failed_pinned_rename_removes_its_temp_and_keeps_the_originalpins the pinned rename cleanup.TestReducedLockHold:test_a_same_length_edit_that_restores_the_mtime_is_kept(only the bytes differ),test_a_key_that_moves_before_the_lock_is_rewritten_at_its_new_path(a hard link),test_a_crlf_transcript_keeps_one_line_ending, each × 3 writes.test_an_interrupt_before_the_publish_starts_removes_the_temp_fileis red on 67ff65a.TestReducedLockHold::test_an_interrupt_as_the_publish_starts_removes_the_staged_copy[3 writes]: aKeyboardInterruptaspublish_replacementis entered leaves no staged copy and the transcript unchanged. Red on 5a8f238.test_an_interrupt_after_the_rename_keeps_the_published_edit[3 writes]: the edit and both rows survive an interrupt after the rename.Mutations KILLED: no byte re-check (10), always the locked path (3), migration dir sync only after a write (1), close after the protected region (3), unguarded cleanup close (6), fd ownership kept across the close (2), no cleanup on a failed pinned rename (1), no path check (3), no CRLF normalisation (3), publish outside the cleanup
try(1), no directory sync after release (3), no discard on an interrupted hand-off topublish_replacement(3).Run locally on this head (rebased onto 3ecd50f): the scoped backend suite for this diff (16,762 passed, 1 failed:
test_snapshot_merge_core_files_pinned.py::test_the_rewrite_keeps_the_live_stores_access_control_acl, where this host'ssetfaclrejectsu:nobody:---on any file); the history page-index, locked-section, archive, atomic-write, migration and persistence files (197 passed; removing the page-index carry turns 3test_history_page_index_persist.pycases red); black gate, isort, flake8,mypy src/kiro_crew/, comment-history, subprocess-encoding, docs lint, feature map, brand name, harness parity.Run locally on an earlier head (base 6362ff1): 68 history, migration, archive, atomic-write and caller files (5174 passed, 1 skipped); 59 ratchet and facade files (5915 passed); flake-pattern ratchet (58 passed); black gate, isort, flake8,
mypy src/kiro_crew/, comment-history, subprocess-encoding, docs lint, feature map; Semgrep 1.78.0 withsemgrep/(0 on our lines). macOS Tests is path-skipped here.Manual verification
N/A — unit coverage sufficient: the fault paths are reachable only by injection.
Related Issues
Follow-up to #19362.
Part of #19361
Pattern harvest
Rule candidate: review-prompt
Pattern: a whole-file replace fsyncs even for a one-line edit, before any shared lock, renaming under it only if the file is unchanged. Deleting the other copy syncs the directory first. A temp file's close stays inside its cleanup region.
Checklist