Repository navigation
fix(resume): refuse to publish a slot while its session's delete is in flight - #16435
Conversation
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Check that a hook-less resume of a closed session still broadcasts its rows when it publishes, now that [DESIGN-REVIEWED] c9f19c5 Description read[DESCRIPTION-READ] c2c52acf68cc1578caa666c3beaacd876ac16ea2662c8de0479f421af0b491ea 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 Check before merge: a failed Not justified as shipped
What this change shipsInventory (8 items) — 7 justifiedIntent: when someone deletes a history session while a resume of it is in progress, the deleted conversation is never left open as a tab. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] c9f19c5 Description read[DESCRIPTION-READ] ef3b46dbf9a3e87a237de07edf608ef10a2e48abc8e7885727348a0e28f6d96a That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
2537794 to
cbbde04
Compare
|
|
|
|
cbbde04 to
8b7d28c
Compare
Local on 8b7d28c: |
|
Replies to the two advisory notes on cbbde04 (both lanes were PASS):
|
8b7d28c to
1e5211d
Compare
|
|
Update to the GPT disposition above for the rollback finding, part 2: on 1e5211d the publish-time guard no longer refuses on any reappearing |
1e5211d to
c528448
Compare
|
All three findings on 1e5211d sit in the rollback counter, and c528448 removes the rollback they guard rather than patching it a third time.
|
30114ad to
60779c3
Compare
…n flight
Resume re-checks existence and created_at identity with a lock-free metadata
read, but delete_session holds the transcript lock until it unlinks, so a
re-check inside the delete's transaction sees the file and publishes. The
session-delete handlers remove only the slot they captured before their first
await, so that slot survives as a tab of a deleted conversation.
Add a process-local in-flight marker on ConversationLog, keyed by store
directory and transcript lock stem. delete_session holds it for its
transaction, and _claim_history_delete opens it in the same step as the slot
claim for both DELETE /api/sessions/{key} and the bulk clear. Both resume
barriers refuse with a retryable resume_conflict while it is held.
A resume of a closed session, with or without a containment hook, now builds
its slot retracted under the construction mark and clears the closed marker
there: the write is awaited and verified before the publish, a failed clear
refuses with reopen_failed, and a refusal after it puts the marker back. Every
refusal before construction therefore leaves the marker as it was found and
has nothing to roll back.
Closes kirodotdev#16433
60779c3 to
c9f19c5
Compare
|
GPT F1 on 60779c3 (persist-before-you-publish: the hook-less A resume of a closed session now takes the same reserved window the containment-hook path already used, with or without a hook. The slot is built retracted under its construction mark, the compare-and-clear runs there and is awaited, its verification read goes through the identity barrier (which includes the delete-in-flight check), and the slot is published only after that. A clear that raises refuses with A second resume during the clear now finds the key under construction and gets Tests: The same head also fixes the four |
|
Replies to the advisory notes on 60779c3 (both lanes PASS):
|
|
Replies to the advisory notes on c9f19c5 (both lanes PASS):
|
iamwhatever
left a comment
There was a problem hiding this comment.
The race fix is sound. The in-process marker is counted, released in finally, and opened at the same instant the delete handler claims its slot. Resume checks it at every last synchronous step before publishing, so the window the issue describes is closed. A failed or cancelled delete releases the marker, so resume isn't refused forever. Two Low notes: the _claim_history_delete docstring names resume_session_deleted but the code returns resume_conflict, and moving the hook-less closed clear grows the PR beyond its goal (it's covered by tests). I read the tests but didn't run them against base myself. Approving.
Things I couldn't verify:
- I did not check out either PR or run any tests; there was no local clone. For 16435, the new tests either call
delete_in_flight_window, which doesn't exist on base, or expect a refusal where base returns 200, so they should fail without the fix. That conclusion comes from reading the tests, not a run. CI is green: 90 pass, 10 skipped,PR Readinesspassed. - #16433 is
tier:T1with notriagedlabel, which T1 doesn't need. - Nothing was posted or modified on either PR. The
@kirocrew-computerMCP server was not configured in this session; this review didn't need it.
Backend Tests (Windows) (2) fails on unrelated PRs with 409
resume_conflict ("this session changed while resuming"). The cause is
not an unreadable identity re-read. A resume clears `closed` only when
closed_at is strictly earlier than the resume's start, and a tie keeps
the marker on purpose (#15444). Both stamps come from time.time(),
which moves every ~15.6 ms on Windows CPython 3.12, so a test that
closes a session and resumes it at once ties there, the clear is
skipped, and the post-clear verification refuses. Since #16435 every
closed resume verifies that clear.
Add close_before_resume() to chat_test_helpers: it states the
precondition "closed BEFORE the resume begins" by moving closed_at
60 s back, as test_session_control_revive._archive already does. Call
it from test_slot_ownership_checkpoint's _persist_and_close and from
the two close-then-resume tests in test_dashboard_chat.
test_resume_close_resume_no_duplicate_history also asserts that both
of its resumes return 200; on a tie its second resume was refused
unasserted and the test passed without running the cycle it covers.
Fixes #17117
…#17145) Backend Tests (Windows) (2) fails on unrelated PRs with 409 resume_conflict ("this session changed while resuming"). The cause is not an unreadable identity re-read. A resume clears `closed` only when closed_at is strictly earlier than the resume's start, and a tie keeps the marker on purpose (#15444). Both stamps come from time.time(), which moves every ~15.6 ms on Windows CPython 3.12, so a test that closes a session and resumes it at once ties there, the clear is skipped, and the post-clear verification refuses. Since #16435 every closed resume verifies that clear. Add close_before_resume() to chat_test_helpers: it states the precondition "closed BEFORE the resume begins" by moving closed_at 60 s back, as test_session_control_revive._archive already does. Call it from test_slot_ownership_checkpoint's _persist_and_close and from the two close-then-resume tests in test_dashboard_chat. test_resume_close_resume_no_duplicate_history also asserts that both of its resumes return 200; on a tie its second resume was refused unasserted and the test passed without running the cycle it covers. Fixes #17117
Problem / Motivation
Goal: Resuming a history session while that session is being permanently deleted is refused, so the deleted conversation never stays open as a tab.
History resume re-checks that the session still exists, and is still the same session, after its threaded transcript read. That re-check is a lock-free
get_metadata_statusread.delete_sessionholds the transcript lock across its whole transaction (search-index drop, attachment and reply-thread staging, the unlink), so a re-check that lands inside the transaction still sees the file with its originalcreated_atand resume publishes the slot (200).DELETE /api/sessions/{key}captures which slot to remove before its first await and removes only that slot afterwards, so the slot the resume published survives the delete.The same window shows up in CI.
test_a_delete_during_the_read_does_not_republish_the_transcriptandtest_a_delete_then_recreate_during_the_read_does_not_overwrite_the_replacementfailed on Backend Tests (Windows) (4) for #15596 and #15592 with "resume published a slot ... (status 200)". Their parked transcript read has a 10 s fallback, and on a slow runner the racer'sdelete_sessionwas still running when it fired (both tests took about 11.5 s), so resume re-checked mid-delete.Why it matters
A user who deletes a session while a resume of it is in progress (a History click, a session-control revive) is left with an open tab holding the deleted conversation. The slot save's delete-won guard keeps that tab from writing the transcript back, so what they type there is silently dropped instead of saved. It also makes two resume tests fail at random on Windows CI for unrelated PRs.
Not a goal
delete_session.test/test_resume_publishes_hydrated_slot.py. With the fix they pass in either interleaving.session_was_deletedbefore republishing.What changed (motivation → approach → change)
Symptom: resume answers 200 for a session whose delete is in progress. Root cause: the existence and identity re-check cannot see a delete that holds the transcript lock but has not unlinked yet, and the delete handler only removes slots it captured before it started. Taking the transcript lock in the re-check is not an option, because the re-check is synchronous on the event loop just before the publish and must add no suspension point. So the delete publishes an in-process marker for its whole span, and resume, on seeing it at its last synchronous step, refuses with a retryable
resume_conflictand leaves the session exactly as it found it. A retry after the delete ends finds the session gone or opens it.ConversationLog.delete_in_flight_window(key)/delete_in_flight(key)(src/kiro_crew/history.py): a process-wide, counted marker keyed by store directory and transcript lock stem, so every spelling of one session shares it and nested windows release cleanly. It is dict bookkeeping under athreading.Lock, with no I/O, so it is safe on the loop.delete_sessionholds it for its whole transaction.src/kiro_crew/dashboard/handlers/sessions.py:_claim_history_deleteis now the one way a delete handler takes a slot claim. It opens the window on anExitStackand captures the claim in the same synchronous step, and the caller closes the stack after its slot cleanup. A resume that published before that point is in the claim and is removed; one that re-checks after it is refused.api_session_deleteand the bulkapi_sessions_clearboth use it (each row's window stays open until the batch cleanup). Their bodies moved unchanged into_delete_claimed_sessionand_clear_history_rows. The single delete's app check on the live slot runs on the claim inside the window, before the first await.src/kiro_crew/dashboard/chat_api/resume.py(the resume core's home since refactor(dashboard): compose the chat handlers from chat_api owners #16427): when the resume core sees the marker held for a session that existed, at the last synchronous step before construction (after the member-binding read, so a delete whose window opens during any earlier await is seen), it publishes nothing and refuses with the existingresume_conflict(409). The containment hook's path makes the same check in_identity_refusalafter its own awaits and refuses the same way through its existing_discard, which restores the marker.closedclear runs there (compare-and-clear against the resume's start), the write is awaited and its result verified, the identity barrier re-runs on that read, and only then is the slot published. A clear that raises refuses with the existingreopen_failed(503) and publishes nothing; a refusal after the clear puts the marker back through the existing_discard. Every refusal before construction, the in-flight one included, leaves the marker as it was found and has nothing to roll back, so the lost-race arm's marker restore, the app-ownership late barrier's marker restore and the identity-baseline adjustment for the old eager clear are removed; both arms still answer an app with the uniform 404. A second resume that arrives during the clear finds the key under construction and getsresume_in_progress. The construction set keeps its meaning (only a slot build marks a key).test/test_session_control_revive.py: the lost-race test now checks that a click refusedresume_in_progressleavesclosedset, and a new test checks that a click refused by an in-flight delete makes no durable write at all.docs/system-specs/modules/history.mddocuments the marker, the refusal, and that the refusal leaves the session as it was found.Backwards compatibility
Compatible: a resume that races an in-process delete of its session now answers 409
resume_conflict, an existing retryable code. A resume of a closed session clearsclosedafter its slot is built and before it is published, instead of before its transcript read; if that write fails it answers the existingreopen_failed(503) instead of opening a tab that would not restore. No API, field or stored format changes.Removes the lost-race arm's and the app late barrier's marker restores in the resume core, which nothing reaches once the clear follows construction. No path, record, key or public function is removed or renamed.
Tests
test/test_resume_refuses_an_in_flight_delete.py(new):test_a_resume_that_rechecks_while_the_delete_holds_its_lock_is_refused: parksdelete_sessioninside its locked section and releases the resume's read there. Expects 409resume_conflictwhile the delete is still parked, no slot published. Fails on origin/main.test_a_resume_racing_the_delete_handler_before_its_unlink_is_refused: the same againstDELETE /api/sessions/{key}, parked at its first await after the slot claim. Fails on origin/main.test_a_bulk_clear_racing_a_resume_before_its_unlink_is_refused: the same againstDELETE /api/sessions, parked inside its worker after the row's claim and beforedelete_session.test_a_resume_after_the_delete_finished_still_refuses_and_one_before_it_publishes: a skipped delete of a pinned session leaves no marker behind, so a later resume still publishes (control).test_a_refused_resume_leaves_a_closed_session_closed_and_a_retry_opens_it: a closed session's resume inside a window that does not delete answersresume_conflictwithclosedandclosed_atunchanged and no hold left behind; a retry after the window opens it withclosedcleared.test_the_reopen_write_lands_before_the_publish: the resume of a closed session is parked inside its marker clear, which finds the slot not yet published; a second resume getsresume_in_progress, and the first publishes once the clear lands withclosedgone. Fails on 60779c3 (the clear ran after the publish).test_a_reopen_write_that_fails_publishes_nothing: a raising clear answers 503reopen_failed, publishes no slot, releases the construction mark and leavesclosedset. Fails on 60779c3 (answered 200).test_a_delete_starting_during_the_member_binding_read_is_seen: opens the delete window while a member resume is suspended in its late binding read. Expects 409 and no slot.test/test_session_control_revive.py:test_a_history_click_that_loses_the_race_leaves_the_marker_untouchedandtest_a_history_click_refused_by_an_in_flight_delete_writes_nothing: the second fails on the previous head (it cleared and restored the marker).test/test_slot_ownership_checkpoint.py(app-ownership tests that pinned the old eager clear): the destination-interleaving test now expects the source still closed during the reads;test_a_late_refusal_leaves_the_closed_marker_untouchedandtest_construction_starting_during_the_reads_leaves_the_markercheck that both refusals answer the app's 404 with no clear attempted andclosedstill set.test/test_remove_slot_for_history_key.py: its fake transcript store gains a no-opdelete_in_flight_window, the one method the handler now calls on it.test_app_*,test_history*,test_*resume*,test_dashboard_session*andtest_session_control*file, plus the delete, transfer and chat-handler composition suites: 5063 passed, 17 skipped.clear_closedor the resume route (test_slot_ownership_checkpoint.py,test_channel_slots.py,test_members_dm_thread.py,test_session_end_interaction.pyand others): 3228 passed, 8 skipped.test_app_script_cron_sibling_import.pyerrors at collection on this machine because its platform profile resolves to a non-default context; it does not import the changed modules.--check-only, flake8 on the changed files, mypy onchat_api/resume.py,history.pyandhandlers/sessions.py, the black gate, comment-history gate against origin/main, docs lint: clean.test_session_control_durable_sid_across_processes.py::test_the_store_names_another_slots_log_in_a_process_that_mapped_nothingfails locally on origin/main as well, unchanged by this PR.Manual verification
N/A: the race is reproduced with forced interleavings in the tests above, which also cover the HTTP delete route end to end.
Related Issues
Closes #16433
Pattern harvest
Rule candidate: review-prompt
Pattern: a lock-free existence probe used as a barrier against a writer that holds a lock across a multi-step transaction cannot see the writer until it commits; pair it with an in-flight marker the writer sets before its transaction (and before any claim it takes), or take the same lock.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
N/A: no CLA text has been published for this repository yet.