From 144445bd7c710f8cc1b05349947954654efbbac1 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 14:12:55 -0700 Subject: [PATCH 1/9] feat: useRenderDebug hook for debugging react rerenders --- gui/src/hooks/useRenderDebug.tsx | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 gui/src/hooks/useRenderDebug.tsx diff --git a/gui/src/hooks/useRenderDebug.tsx b/gui/src/hooks/useRenderDebug.tsx new file mode 100644 index 000000000..6aa3e936c --- /dev/null +++ b/gui/src/hooks/useRenderDebug.tsx @@ -0,0 +1,66 @@ +import { CSSProperties, useEffect, useRef } from "react"; + +/** + * useRenderDebug + * + * Developer utility to visualize component re-renders. + * + * - Returns a `ref` to attach to a container element and a `count` that + * increments on every render of the component using this hook. + * - On each render, it briefly adds a CSS class (default: `toolbar-rerender-flash`) + * to the element for a quick visual flash. + * - Use together with to show the render count inline. + * + * Options: + * - enabled: boolean to toggle the effect (default true) + * - flashClassName: class added on each render (default `toolbar-rerender-flash`) + * - flashDurationMs: duration of the flash animation in ms (default 180) + */ + +export function useRenderDebug(options?: { + enabled?: boolean; + flashClassName?: string; + flashDurationMs?: number; +}) { + const { + enabled = true, + flashClassName = "toolbar-rerender-flash", + flashDurationMs = 180, + } = options || {}; + + const ref = useRef(null); + const countRef = useRef(0); + countRef.current += 1; + + useEffect(() => { + if (!enabled || !ref.current) return; + const el = ref.current; + el.classList.add(flashClassName); + const t = setTimeout( + () => el.classList.remove(flashClassName), + flashDurationMs, + ); + return () => clearTimeout(t); + }); + + return { ref, count: countRef.current, countRef } as const; +} + +export function RenderCountBadge(props: { + count: number; + title?: string; + style?: CSSProperties; + className?: string; +}) { + // Simple badge for use with useRenderDebug; keep lightweight. + const { count, title = `Renders: ${count}`, style, className } = props; + return ( + + R{count} + + ); +} From d9db117d85ea0ccacfb6a514e7bae0d65fec9c92 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 14:17:53 -0700 Subject: [PATCH 2/9] perf: memoize input box components --- .../components/mainInput/ContinueInputBox.tsx | 16 +++-- gui/src/components/mainInput/InputToolbar.tsx | 24 +++++++- .../mainInput/TipTapEditor/TipTapEditor.tsx | 58 ++++++++++++++++--- 3 files changed, 82 insertions(+), 16 deletions(-) diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index c731b761b..3b2ed52ea 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -1,6 +1,6 @@ import { Editor, JSONContent } from "@tiptap/react"; import { ContextItemWithId, InputModifiers, RuleWithSource } from "core"; -import { useMemo } from "react"; +import { memo, useMemo } from "react"; import { defaultBorderRadius, vscBackground } from ".."; import { useAppSelector } from "../../redux/hooks"; import { selectSlashCommandComboBoxInputs } from "../../redux/selectors"; @@ -69,16 +69,20 @@ function ContinueInputBox(props: ContinueInputBoxProps) { const historyKey = isInEdit ? "edit" : "chat"; const placeholder = isInEdit ? "Edit selected code" : undefined; - const toolbarOptions: ToolbarOptions = isInEdit - ? { + const toolbarOptions: ToolbarOptions = useMemo(() => { + if (isInEdit) { + return { hideAddContext: false, hideImageUpload: false, hideUseCodebase: true, hideSelectModel: false, enterText: editModeState.applyState.status === "done" ? "Retry" : "Edit", - } - : {}; + } as ToolbarOptions; + } + // Stable empty object to avoid re-renders from identity changes + return {} as ToolbarOptions; + }, [isInEdit, editModeState.applyState.status]); const { appliedRules = [], contextItems = [] } = props; @@ -123,4 +127,4 @@ function ContinueInputBox(props: ContinueInputBoxProps) { ); } -export default ContinueInputBox; +export default memo(ContinueInputBox); diff --git a/gui/src/components/mainInput/InputToolbar.tsx b/gui/src/components/mainInput/InputToolbar.tsx index bc5bc6a4d..25355a7bd 100644 --- a/gui/src/components/mainInput/InputToolbar.tsx +++ b/gui/src/components/mainInput/InputToolbar.tsx @@ -9,7 +9,7 @@ import { modelSupportsImages, modelSupportsReasoning, } from "core/llm/autodetect"; -import { useContext, useRef } from "react"; +import { memo, useContext, useRef } from "react"; import { IdeMessengerContext } from "../../context/IdeMessenger"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import { selectUseActiveFile } from "../../redux/selectors"; @@ -242,4 +242,24 @@ function InputToolbar(props: InputToolbarProps) { ); } -export default InputToolbar; +function shallowToolbarOptionsEqual(a?: ToolbarOptions, b?: ToolbarOptions) { + if (a === b) return true; + if (!a || !b) return false; + return ( + a.hideAddContext === b.hideAddContext && + a.hideImageUpload === b.hideImageUpload && + a.hideUseCodebase === b.hideUseCodebase && + a.hideSelectModel === b.hideSelectModel && + a.enterText === b.enterText + ); +} + +export default memo( + InputToolbar, + (prev, next) => + prev.hidden === next.hidden && + prev.disabled === next.disabled && + prev.isMainInput === next.isMainInput && + prev.activeKey === next.activeKey && + shallowToolbarOptionsEqual(prev.toolbarOptions, next.toolbarOptions), +); diff --git a/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx index 82a934f41..09aee4d4e 100644 --- a/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx @@ -1,7 +1,14 @@ import { Editor, EditorContent, JSONContent } from "@tiptap/react"; import { ContextProviderDescription, InputModifiers } from "core"; import { modelSupportsImages } from "core/llm/autodetect"; -import { useCallback, useContext, useEffect, useRef, useState } from "react"; +import { + memo, + useCallback, + useContext, + useEffect, + useRef, + useState, +} from "react"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import useIsOSREnabled from "../../../hooks/useIsOSREnabled"; import useUpdatingRef from "../../../hooks/useUpdatingRef"; @@ -38,7 +45,7 @@ export interface TipTapEditorProps { export const TIPPY_DIV_ID = "tippy-js-div"; -export function TipTapEditor(props: TipTapEditorProps) { +function TipTapEditorInner(props: TipTapEditorProps) { const dispatch = useAppDispatch(); const mainEditorContext = useMainEditor(); @@ -72,12 +79,13 @@ export function TipTapEditor(props: TipTapEditorProps) { return; } const placeholder = getPlaceholderText(props.placeholder, historyLength); - - editor.extensionManager.extensions.filter( - (extension) => extension.name === "placeholder", - )[0].options["placeholder"] = placeholder; - - editor.view.dispatch(editor.state.tr); + const placeholderExt = editor.extensionManager.extensions.find( + (e) => e.name === "placeholder", + ) as any; + if (placeholderExt) { + placeholderExt.options["placeholder"] = placeholder; + editor.view.dispatch(editor.state.tr); + } }, [editor, props.placeholder, historyLength]); useEffect(() => { @@ -281,3 +289,37 @@ export function TipTapEditor(props: TipTapEditorProps) { ); } + +function toolbarOptionsEqual(a?: ToolbarOptions, b?: ToolbarOptions) { + if (a === b) return true; + if (!a || !b) return false; + return ( + a.hideAddContext === b.hideAddContext && + a.hideImageUpload === b.hideImageUpload && + a.hideUseCodebase === b.hideUseCodebase && + a.hideSelectModel === b.hideSelectModel && + a.enterText === b.enterText + ); +} + +const MemoInner = memo( + TipTapEditorInner, + (prev, next) => + prev.isMainInput === next.isMainInput && + prev.placeholder === next.placeholder && + prev.historyKey === next.historyKey && + prev.inputId === next.inputId && + toolbarOptionsEqual(prev.toolbarOptions, next.toolbarOptions) && + (prev.availableContextProviders?.length || 0) === + (next.availableContextProviders?.length || 0) && + (prev.availableSlashCommands?.length || 0) === + (next.availableSlashCommands?.length || 0), +); + +export function TipTapEditor(props: TipTapEditorProps) { + return ( +
+ +
+ ); +} From 364489c57384f11c77c284323fdcc170be9af96e Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 14:19:28 -0700 Subject: [PATCH 3/9] perf: only set activeKey for special keys that we care about to avoid rerenders --- .../mainInput/TipTapEditor/utils/keyHandlers.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts b/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts index a102ba2f0..cec988450 100644 --- a/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts +++ b/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts @@ -13,6 +13,7 @@ export function useEditorEventHandlers(options: { setActiveKey: (key: string | null) => void; }) { const { editor, isOSREnabled, editorFocusedRef, setActiveKey } = options; + const metaActiveRef = { current: false } as React.MutableRefObject; /** * This handles various issues with meta key actions @@ -31,9 +32,13 @@ export function useEditorEventHandlers(options: { return; } - setActiveKey(e.key); + if (!editorFocusedRef?.current || isMetaEquivalentKeyPressed(e)) return; - if (!editorFocusedRef?.current || !isMetaEquivalentKeyPressed(e)) return; + // Only set activeKey for Meta/Control/Alt to drive toolbar highlighting + if (e.key === "Meta" || e.key === "Control" || e.key === "Alt") { + setActiveKey(e.key); + metaActiveRef.current = true; + } if (isOSREnabled) { handleJetBrainsOSRMetaKeyIssues(e, editor); @@ -42,7 +47,12 @@ export function useEditorEventHandlers(options: { } }; - const handleKeyUp = () => setActiveKey(null); + const handleKeyUp = () => { + if (metaActiveRef.current) { + setActiveKey(null); + metaActiveRef.current = false; + } + }; return { handleKeyDown, handleKeyUp }; } From 931afc63aa5abc117689246b58f6e69784d8a3b3 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 14:22:29 -0700 Subject: [PATCH 4/9] follow up to userenderdebug --- gui/src/hooks/useRenderDebug.tsx | 8 ++++---- gui/src/index.css | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/gui/src/hooks/useRenderDebug.tsx b/gui/src/hooks/useRenderDebug.tsx index 6aa3e936c..bc5fb8308 100644 --- a/gui/src/hooks/useRenderDebug.tsx +++ b/gui/src/hooks/useRenderDebug.tsx @@ -7,13 +7,13 @@ import { CSSProperties, useEffect, useRef } from "react"; * * - Returns a `ref` to attach to a container element and a `count` that * increments on every render of the component using this hook. - * - On each render, it briefly adds a CSS class (default: `toolbar-rerender-flash`) + * - On each render, it briefly adds a CSS class (default: `rerender-flash`) * to the element for a quick visual flash. * - Use together with to show the render count inline. * * Options: * - enabled: boolean to toggle the effect (default true) - * - flashClassName: class added on each render (default `toolbar-rerender-flash`) + * - flashClassName: class added on each render (default `rerender-flash`) * - flashDurationMs: duration of the flash animation in ms (default 180) */ @@ -24,7 +24,7 @@ export function useRenderDebug(options?: { }) { const { enabled = true, - flashClassName = "toolbar-rerender-flash", + flashClassName = "rerender-flash", flashDurationMs = 180, } = options || {}; @@ -56,7 +56,7 @@ export function RenderCountBadge(props: { const { count, title = `Renders: ${count}`, style, className } = props; return ( diff --git a/gui/src/index.css b/gui/src/index.css index 6ef5e902e..fce97497e 100644 --- a/gui/src/index.css +++ b/gui/src/index.css @@ -119,3 +119,28 @@ body { .scroll-container:is(:hover, :focus)::-webkit-scrollbar { display: block; /* Show scrollbar only on hover or focus */ } + +/* Debug: visible flash when component re-renders */ +@keyframes rerenderFlash { + 0% { + box-shadow: 0 0 0 2px rgba(255, 0, 0, 0.9) inset; + } + 100% { + box-shadow: 0 0 0 0 rgba(255, 0, 0, 0) inset; + } +} + +.rerender-flash { + animation: rerenderFlash 180ms ease-out; +} + +.render-count { + display: inline-block; + margin-right: 6px; + padding: 0 4px; + border-radius: 3px; + background: rgba(255, 0, 0, 0.15); + color: rgba(255, 0, 0, 0.9); + font-weight: 600; + letter-spacing: 0.02em; +} From d6e6f1e9b0e7079e6fcab2e6beafda40c79e94d7 Mon Sep 17 00:00:00 2001 From: Nate Sesti <33237525+sestinj@users.noreply.github.com> Date: Mon, 15 Sep 2025 20:10:45 -0700 Subject: [PATCH 5/9] Apply suggestion from @cubic-dev-ai[bot] Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts b/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts index cec988450..c652857df 100644 --- a/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts +++ b/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts @@ -32,7 +32,7 @@ export function useEditorEventHandlers(options: { return; } - if (!editorFocusedRef?.current || isMetaEquivalentKeyPressed(e)) return; + if (!editorFocusedRef?.current || !isMetaEquivalentKeyPressed(e)) return; // Only set activeKey for Meta/Control/Alt to drive toolbar highlighting if (e.key === "Meta" || e.key === "Control" || e.key === "Alt") { From da2e31694a49db91e7269ddb9b08cef565e3a199 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 20:11:48 -0700 Subject: [PATCH 6/9] fix: cleanup of render debug --- gui/src/hooks/useRenderDebug.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gui/src/hooks/useRenderDebug.tsx b/gui/src/hooks/useRenderDebug.tsx index bc5fb8308..2a86247eb 100644 --- a/gui/src/hooks/useRenderDebug.tsx +++ b/gui/src/hooks/useRenderDebug.tsx @@ -40,7 +40,11 @@ export function useRenderDebug(options?: { () => el.classList.remove(flashClassName), flashDurationMs, ); - return () => clearTimeout(t); + return () => { + clearTimeout(t); + // Ensure we don't leave the flash class applied on cleanup + el.classList.remove(flashClassName); + }; }); return { ref, count: countRef.current, countRef } as const; From 62120df413665a28d3b79a4705c9f4a5f5d19b50 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 20:15:31 -0700 Subject: [PATCH 7/9] fix: address feedback --- .../components/mainInput/TipTapEditor/utils/keyHandlers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts b/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts index c652857df..bb7fd593a 100644 --- a/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts +++ b/gui/src/components/mainInput/TipTapEditor/utils/keyHandlers.ts @@ -1,5 +1,5 @@ import { Editor } from "@tiptap/react"; -import { KeyboardEvent } from "react"; +import { KeyboardEvent, useRef } from "react"; import { isJetBrains, isMetaEquivalentKeyPressed } from "../../../../util"; import { handleJetBrainsOSRMetaKeyIssues, @@ -13,7 +13,7 @@ export function useEditorEventHandlers(options: { setActiveKey: (key: string | null) => void; }) { const { editor, isOSREnabled, editorFocusedRef, setActiveKey } = options; - const metaActiveRef = { current: false } as React.MutableRefObject; + const metaActiveRef = useRef(false); /** * This handles various issues with meta key actions From 867720804457ddbe283c7b8b610cff48a543a31e Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 20:52:01 -0700 Subject: [PATCH 8/9] fix: tests --- gui/src/pages/gui/Chat.tsx | 45 ++++++++++------------ packages/openai-adapters/package-lock.json | 1 - 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 4be689dae..fe9a3285a 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -49,6 +49,8 @@ import { cancelStream } from "../../redux/thunks/cancelStream"; import { EmptyChatBody } from "./EmptyChatBody"; import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; import { useAutoScroll } from "./useAutoScroll"; +import { useStore } from "react-redux"; +import { RootState } from "../../redux/store"; // Helper function to find the index of the latest conversation summary function findLatestSummaryIndex(history: ChatHistoryItem[]): number { @@ -98,13 +100,11 @@ function fallbackRender({ error, resetErrorBoundary }: any) { export function Chat() { const dispatch = useAppDispatch(); const ideMessenger = useContext(IdeMessengerContext); + const reduxStore = useStore(); const onboardingCard = useOnboardingCard(); const showSessionTabs = useAppSelector( (store) => store.config.config.ui?.showSessionTabs, ); - const selectedModels = useAppSelector( - (store) => store.config?.config.selectedModelByRole, - ); const isStreaming = useAppSelector((state) => state.session.isStreaming); const [stepsOpen] = useState<(boolean | undefined)[]>([]); const mainTextInputRef = useRef(null); @@ -115,7 +115,6 @@ export function Chat() { (state) => state.config.config.ui?.showChatScrollbar, ); const codeToEdit = useAppSelector((state) => state.editModeState.codeToEdit); - const mode = useAppSelector((store) => store.session.mode); const isInEdit = useAppSelector((store) => store.session.isInEdit); const lastSessionId = useAppSelector((state) => state.session.lastSessionId); @@ -152,9 +151,6 @@ export function Chat() { isStreaming, ); - const pendingToolCalls = useAppSelector(selectPendingToolCalls); - const pendingApplyStates = useAppSelector(selectDoneApplyStates); - const sendInput = useCallback( ( editorState: JSONContent, @@ -162,8 +158,16 @@ export function Chat() { index?: number, editorToClearOnSend?: Editor, ) => { + const stateSnapshot = reduxStore.getState(); + const latestPendingToolCalls = selectPendingToolCalls(stateSnapshot); + const latestPendingApplyStates = selectDoneApplyStates(stateSnapshot); + const isCurrentlyInEdit = stateSnapshot.session.isInEdit; + const codeToEditSnapshot = stateSnapshot.editModeState.codeToEdit; + const selectedModelByRole = + stateSnapshot.config.config.selectedModelByRole; + // Cancel all pending tool calls - pendingToolCalls.forEach((toolCallState) => { + latestPendingToolCalls.forEach((toolCallState) => { dispatch( cancelToolCall({ toolCallId: toolCallState.toolCallId, @@ -172,19 +176,20 @@ export function Chat() { }); // Reject all pending apply states - pendingApplyStates.forEach((applyState) => { + latestPendingApplyStates.forEach((applyState) => { if (applyState.status !== "closed") { ideMessenger.post("rejectDiff", applyState); } }); - const model = isInEdit - ? (selectedModels?.edit ?? selectedModels?.chat) - : selectedModels?.chat; + const model = isCurrentlyInEdit + ? selectedModelByRole.edit ?? selectedModelByRole.chat + : selectedModelByRole.chat; + if (!model) { return; } - if (isInEdit && codeToEdit.length === 0) { + if (isCurrentlyInEdit && codeToEditSnapshot.length === 0) { return; } @@ -212,11 +217,11 @@ export function Chat() { // } // } - if (isInEdit) { + if (isCurrentlyInEdit) { void dispatch( streamEditThunk({ editorState, - codeToEdit, + codeToEdit: codeToEditSnapshot, }), ); } else { @@ -227,15 +232,7 @@ export function Chat() { } } }, - [ - history, - selectedModels, - mode, - isInEdit, - codeToEdit, - pendingToolCalls, - pendingApplyStates, - ], + [dispatch, ideMessenger, reduxStore], ); useWebviewListener( diff --git a/packages/openai-adapters/package-lock.json b/packages/openai-adapters/package-lock.json index a9144e2a3..dc0994761 100644 --- a/packages/openai-adapters/package-lock.json +++ b/packages/openai-adapters/package-lock.json @@ -12531,7 +12531,6 @@ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", "integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==", "dev": true, - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" From c87793aaa449d07e96e500e5d05bf2616f5cf8f6 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 15 Sep 2025 20:56:00 -0700 Subject: [PATCH 9/9] chore: format --- gui/src/pages/gui/Chat.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index fe9a3285a..cd39c5185 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -182,7 +182,7 @@ export function Chat() { } }); const model = isCurrentlyInEdit - ? selectedModelByRole.edit ?? selectedModelByRole.chat + ? (selectedModelByRole.edit ?? selectedModelByRole.chat) : selectedModelByRole.chat; if (!model) {