fix(tui): stop stale instances from erasing pinned sessions - #45117
Open
rareboe wants to merge 1 commit into
Open
fix(tui): stop stale instances from erasing pinned sessions#45117rareboe wants to merge 1 commit into
rareboe wants to merge 1 commit into
Conversation
Every TUI instance read the global session.json pin list once at startup and kept a private copy. Pinning, unpinning and session.deleted all rewrote the whole file from that copy with an unserialized, fire-and-forget write, so an instance holding a stale snapshot replaced pins added by another instance. Two writers starting from an empty list, one pinning "a" and one pinning "b", end up with just ["b"]. Apply each change as an intent against the file's current contents inside Flock.withLock, the same lock context/kv.tsx already uses, and chain writes off the initial read so they cannot run before it completes. The in-memory update still happens immediately so the UI does not wait on the write, and the store is reconciled with the merged result once it lands.
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.
Issue for this PR
Closes #44736
Type of change
What does this PR do?
createSession()inpackages/tui/src/context/local.tsxread the globalsession.jsonpin list once at startup and kept a private in-memory copy.togglePin,pruneand thesession.deletedhandler all rewrote the whole file from that copy through a fire-and-forgetwriteJsonAtomic()— no reread, no lock, no ordering. An instance holding a stale snapshot therefore replaced pins another instance had added.writeJsonAtomicprevents torn files, not stale writers.Reproduced against the old semantics: two writers starting from an empty list, one pinning
aand one pinningb, leave the file as["b"].The fix changes what gets persisted. Instead of writing a snapshot, each change is now an intent — a session ID plus pin/unpin — applied to the file's current contents inside
Flock.withLock, the same lockcontext/kv.tsxalready uses forkv.json. Writes chain off the initial read, so they can never run before the list has loaded and thependingflag is no longer needed. The in-memory store is still updated immediately so the UI doesn't wait on the write, then reconciled with whatever the merged file actually ended up containing.parsePinned,setPinnedandpersistPinnedare exported so the behaviour is testable directly, matching howparseModel/recentModelsare already handled in this module.Worth flagging:
kv.tsxtakes the lock but still writes its own snapshot, sokv.jsonhas the same stale-writer shape. I left it alone to keep this focused — happy to follow up if you want it covered.How did you verify your code works?
Added tests to
packages/tui/test/context/local.test.ts. They use a temp directory for both the file and the lock root, so nothing touches real state:persistPinnedcalls all survive — this is the regression; under the old snapshot write the same shape collapses to a single pinparsePinneddrops non-string entries and bad shapes;setPinneddoesn't duplicateI also verified the old behaviour actually fails, by reimplementing the previous snapshot write in a scratch test:
Screenshots / recordings
No visible UI change — the pin list renders the same, it just stops losing entries.
Checklist