Repository navigation
fix(sessions): keep a deleted session from being written back by a write in progress - #254
Merged
Merged
Conversation
…ite in progress Deleting a session unlinked its file without going through the per-session write queue that every other write to the file uses. A write is "find the file, read it, append", and the append creates a missing file, so a delete that landed in between left a file with one or two metadata lines behind. It showed up in the sidebar as an empty session with no parent. The usual trigger was closing a side chat: stopping its process makes the server write the custom title again, and the delete follows at once. It was not limited to side chats or to the title: renaming a session or recording its permission mode at that moment brought the file back the same way. Both delete entry points (deleteSession and deleteSessionFile) now run in the same queue, so a write that came first finishes before the file is removed, and a write that comes after finds no file and does nothing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
SessionService.deleteSessionanddeleteSessionFilenow remove the session file inside the per-session write queue (enqueueSessionWrite), through one privateremoveSessionFile.Why
A deleted session could come back as an empty session in the sidebar. It was seen after closing side chat tabs: each closed side chat left a session titled "btw" in the list.
Every write to a session file goes through the per-session queue; the two deletes did not. A write is "find the file, read it, append", and the append uses
appendFile, which creates a missing file. A delete that landed between "find the file" and "append" was undone by the append: the file came back with one or two metadata lines and no parent, so the list showed it as an ordinary session.The usual trigger is the title check after the CLI exits (
restoreCustomTitle, called fromsrc/server/session/persistence.ts): deleting a session stops its CLI first, the exit starts the title check, and the delete follows immediately. A side chat always has a custom title and a running CLI, so it hit this every time. Renaming a session and recording its permission mode at the same moment bring the file back the same way.With the delete in the queue the two cannot interleave: a write that is ahead finishes and the file is then removed; a delete that is ahead leaves the write with no file to find, and it does nothing.
Not covered
Two paths that can still recreate a deleted session's file. Neither was reproduced, and neither is handled here:
clearSessionTranscriptwith afallbackWorkDircreates a file for a session that has none (used for placeholder sessions). Starting a deleted session is blocked, but a/clearsent by another client at the moment of the delete is not.Files that were already written back before this fix stay on disk; they can be deleted from the sidebar.
How it was tested
src/server/__tests__/sessions.test.ts: 8 new tests, 4 kinds of write (title check with an unchanged title, title written again, rename, permission mode) times the 2 delete entry points. Each holds the write at its read or append step, deletes the session, releases the write, and expects the session file and the list entry to be gone. All 8 fail on unchangedmainand pass with the fix.bun test: 2661 passed, 0 failed.bun test --randomize --seed=1on the touched test file: 255 passed.The session state and side chat design docs describe the delete being in the queue.