fix(web): re-add handleChangeConversation to handleNewConversation deps (#38403)#38573
Open
Taranum01 wants to merge 1 commit into
Open
fix(web): re-add handleChangeConversation to handleNewConversation deps (#38403)#38573Taranum01 wants to merge 1 commit into
Taranum01 wants to merge 1 commit into
Conversation
Resolves a stale closure in the embedded chatbot reset path. Since PR langgenius#31287 (Try Apps refactor), handleNewConversation in web/app/components/base/chat/embedded-chatbot/hooks.tsx omitted handleChangeConversation from its useCallback deps, so the reset button captured the closure from the previous render. When userId, appId, or conversationIdInfo changed between renders (normal during URL→localStorage resolution), the reset click called handleChangeConversation('') over a stale handleConversationIdInfoChange — clearing the stored id in a discarded snapshot rather than the live conversationIdInfo. Subsequent messages re-used the previous conversation_id. The original deps array (pre-PR langgenius#31287, v1.6.0) listed handleChangeConversation. Restoring it puts the deps array back in sync with handleChangeConversation's own deps and eliminates the stale-closure window. Adds a regression test in web/app/components/base/chat/embedded-chatbot/__tests__/hooks.spec.tsx that exercises the identity propagation through a forced embeddedUserId mutation. Test fails on the broken deps array and passes with the fix. Fixes langgenius#38403
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.
Fixes #38403
Problem
Since PR #31287 ("feat: frontend part of support try apps"), the embedded chatbot's
handleNewConversation(web/app/components/base/chat/embedded-chatbot/hooks.tsx) was missinghandleChangeConversationfrom itsuseCallbackdeps array.After URL→localStorage resolution or any state change that touched
handleConversationIdInfoChange's deps (appId,conversationIdInfo,userId),handleChangeConversationregenerates buthandleNewConversationkeeps the previous render's closure. Clicking reset then callshandleChangeConversation('')against a stalehandleConversationIdInfoChangethat closes over the previousconversationIdInfo. ThesetConversationIdInfo({ ...conversationIdInfo, ... })write hits the discarded snapshot rather than the live state, soconversationIdInfo[appId][userId]is never reset. The next message re-uses the oldconversation_id.Fix
Re-add
handleChangeConversationtohandleNewConversation's deps array. This restores the v1.6.0 deps (pre-#31287) and keeps the two callbacks in lockstep:react/exhaustive-depsis now satisfied on line 371.useCallbackonly re-emits when a dep reference changes; at steady state (no state mutation across renders) both callbacks short-circuit and preserve identity, so the rerender cost is unchanged.Test
Added a regression test in
web/app/components/base/chat/embedded-chatbot/__tests__/hooks.spec.tsxthat:conversation_idin localStorage and disables URL/system-variable resolution so the path under test is the localStorage branch.handleNewConversationandhandleChangeConversationreferences.rerenderpreserves both references (validatesuseCallbackmemoization at steady state).mockStoreState.embeddedUserIdto force the line-75 effect →userIdstate →handleConversationIdInfoChangedeps →handleChangeConversationto regenerate.handleNewConversationregenerates alongsidehandleChangeConversation— without the fix this assertion fails (Object.isequality on the stale closure).Run with:
Verification
pnpm exec vitest runon the touched spec: 36/36 passpnpm exec vitest runonapp/components/base/chat/(1355 tests across 73 files): all passpnpm exec eslint --fix --pass-on-unpruned-suppressionson the two changed files: 0 errors, 4 pre-existingexhaustive-depswarnings on unrelateduseEffectblocks (lines 74, 105, 250, 311) — none introduced by this changepnpm exec tsgo --noEmit: cleanexpected [AsyncFunction] not to be [AsyncFunction]), then restored the fix and re-ran → test passesRisk
Minimal. Pure deps-array change. No public-API change, no schema/migration/controller/frontend logic outside the touched file. The change only affects callers of
handleNewConversation(the embedded chatbot reset button) — and only positively: they now reach the freshesthandleChangeConversationinstead of a stale one.Reverts the same
useCallbackdeps edit as PR #31287 did back when it introduced the Try Apps branch. If PR #31287 needed a smaller-deps form for performance reasons, please call that out in review and we can revisit (the call tohandleChangeConversation('')[']only happens once per reset click so the cost change is negligible).Made with Cursor