-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(web): preserve unsent user-input answers across thread switches #4592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ipanasenko
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
ipanasenko:fix/preserve-pending-user-input-draft
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+345
−55
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1bb0c8d
fix(web): preserve unsent user-input answers across thread switches
ipanasenko 510f62d
fix(web): keep a "__proto__" question id as an own draft property
ipanasenko 753fff0
fix(web): keep user-input drafts until the request stops being pending
ipanasenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { beforeEach, describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { setPendingUserInputCustomAnswer } from "./pendingUserInput"; | ||
| import { | ||
| clearPendingUserInputDraft, | ||
| usePendingUserInputDraftStore, | ||
| type PendingUserInputDraftAnswers, | ||
| } from "./pendingUserInputDraftStore"; | ||
|
|
||
| const store = () => usePendingUserInputDraftStore.getState(); | ||
| const answersFor = (requestId: string) => store().draftsByRequestId[requestId]?.answers; | ||
| const questionIndexFor = (requestId: string) => store().draftsByRequestId[requestId]?.questionIndex; | ||
|
|
||
| beforeEach(() => { | ||
| usePendingUserInputDraftStore.setState({ draftsByRequestId: {} }); | ||
| }); | ||
|
|
||
| describe("pendingUserInputDraftStore", () => { | ||
| it("keeps a custom answer available after the owning view unmounts", () => { | ||
| store().updateAnswer("request-1", "question-1", (previous) => | ||
| setPendingUserInputCustomAnswer(previous, "this should survive"), | ||
| ); | ||
|
|
||
| // A thread switch destroys `ChatView`; the store outlives it. | ||
| expect(answersFor("request-1")?.["question-1"]).toEqual({ | ||
| customAnswer: "this should survive", | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps drafts for different requests isolated", () => { | ||
| store().updateAnswer("request-1", "question-1", () => ({ customAnswer: "first" })); | ||
| store().updateAnswer("request-2", "question-1", () => ({ customAnswer: "second" })); | ||
|
|
||
| expect(answersFor("request-1")?.["question-1"]?.customAnswer).toBe("first"); | ||
| expect(answersFor("request-2")?.["question-1"]?.customAnswer).toBe("second"); | ||
| }); | ||
|
|
||
| it("tracks the active question index per request without disturbing answers", () => { | ||
| store().updateAnswer("request-1", "question-1", () => ({ customAnswer: "answer" })); | ||
| const answersBefore = answersFor("request-1"); | ||
|
|
||
| store().setQuestionIndex("request-1", 2); | ||
| store().setQuestionIndex("request-2", 0); | ||
|
|
||
| expect(questionIndexFor("request-1")).toBe(2); | ||
| expect(questionIndexFor("request-2")).toBe(0); | ||
| expect(answersFor("request-1")).toBe(answersBefore); | ||
| }); | ||
|
|
||
| it("normalizes negative and fractional question indexes", () => { | ||
| store().setQuestionIndex("request-1", -3); | ||
| expect(questionIndexFor("request-1")).toBe(0); | ||
|
|
||
| store().setQuestionIndex("request-1", 1.7); | ||
| expect(questionIndexFor("request-1")).toBe(1); | ||
| }); | ||
|
|
||
| it("drops a request's draft once the request is no longer pending", () => { | ||
| store().updateAnswer("request-1", "question-1", () => ({ customAnswer: "answer" })); | ||
| store().setQuestionIndex("request-1", 1); | ||
| store().updateAnswer("request-2", "question-1", () => ({ customAnswer: "other thread" })); | ||
|
|
||
| clearPendingUserInputDraft("request-1"); | ||
|
|
||
| expect(store().draftsByRequestId["request-1"]).toBeUndefined(); | ||
| expect(answersFor("request-2")?.["question-1"]?.customAnswer).toBe("other thread"); | ||
| }); | ||
|
|
||
| it("removes a question entry when the updater returns undefined", () => { | ||
| store().updateAnswer("request-1", "question-1", () => ({ customAnswer: "answer" })); | ||
| store().updateAnswer("request-1", "question-1", () => undefined); | ||
|
|
||
| expect(answersFor("request-1")).toEqual({}); | ||
| }); | ||
|
|
||
| it("stores a draft under a prototype-shadowing question id as an own property", () => { | ||
| store().updateAnswer("request-1", "__proto__", () => ({ customAnswer: "answer" })); | ||
|
|
||
| const requestAnswers = answersFor("request-1") as PendingUserInputDraftAnswers; | ||
| expect(Object.hasOwn(requestAnswers, "__proto__")).toBe(true); | ||
| // Must survive the JSON round-trip the persist middleware performs. | ||
| expect(JSON.parse(JSON.stringify(requestAnswers))["__proto__"]).toEqual({ | ||
| customAnswer: "answer", | ||
| }); | ||
|
|
||
| store().updateAnswer("request-1", "__proto__", () => undefined); | ||
| expect(Object.hasOwn(answersFor("request-1") ?? {}, "__proto__")).toBe(false); | ||
| }); | ||
|
|
||
| it("evicts the oldest drafts past the retention cap but keeps the active one", () => { | ||
| for (let index = 0; index < 60; index += 1) { | ||
| store().updateAnswer(`request-${index}`, "question-1", () => ({ | ||
| customAnswer: `answer-${index}`, | ||
| })); | ||
| } | ||
|
|
||
| const retained = Object.keys(store().draftsByRequestId); | ||
| expect(retained).toHaveLength(50); | ||
| expect(retained).not.toContain("request-0"); | ||
| expect(retained).toContain("request-59"); | ||
| }); | ||
|
|
||
| it("evicts answers and question index together so neither half is orphaned", () => { | ||
| for (let index = 0; index < 60; index += 1) { | ||
| const requestId = `request-${index}`; | ||
| store().updateAnswer(requestId, "question-1", () => ({ customAnswer: `answer-${index}` })); | ||
| store().setQuestionIndex(requestId, 1); | ||
| } | ||
|
|
||
| for (const draft of Object.values(store().draftsByRequestId)) { | ||
| expect(Object.keys(draft.answers)).toHaveLength(1); | ||
| expect(draft.questionIndex).toBe(1); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thread switch clears other drafts
High Severity
The pending-input cleanup effect compares the previous
pendingUserInputssnapshot to the current thread’s list, butseenPendingUserInputRequestIdsRefis not reset whenrouteThreadKey/threadIdchanges. Navigating to another thread (same mountedChatView) treats the prior thread’s pendingrequestIds as removed and callsclearPendingUserInputDraft, wiping persisted drafts while those requests are still open on the original thread.Reviewed by Cursor Bugbot for commit 753fff0. Configure here.