From f100fc97a59c702b9e38cbaad63bcc51686f4662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Thu, 18 Jun 2026 22:16:49 +0100 Subject: [PATCH] fix: focus changes panel on git updates (#1436) --- apps/web/components/task/changes-tab.tsx | 46 ++++--- .../tests/layout/changes-panel-focus.spec.ts | 114 +++++++++++++++--- 2 files changed, 129 insertions(+), 31 deletions(-) diff --git a/apps/web/components/task/changes-tab.tsx b/apps/web/components/task/changes-tab.tsx index bf9428f6ff..9671ae1d83 100644 --- a/apps/web/components/task/changes-tab.tsx +++ b/apps/web/components/task/changes-tab.tsx @@ -1,7 +1,11 @@ "use client"; import { useCallback, useEffect, useRef, useState } from "react"; -import { DockviewDefaultTab, type IDockviewPanelHeaderProps } from "dockview-react"; +import { + DockviewDefaultTab, + type DockviewApi, + type IDockviewPanelHeaderProps, +} from "dockview-react"; import { ContextMenu, ContextMenuContent, @@ -15,18 +19,20 @@ import { useDockviewStore } from "@/lib/state/dockview-store"; import { cn } from "@kandev/ui/lib/utils"; import { useTabMaximizeOnDoubleClick } from "./use-tab-maximize"; -/** Auto-activate the changes panel only when it lives in the right sidebar. */ +type DockviewPanel = NonNullable>; + +function groupContainsAgentSessionPanel(panel: DockviewPanel): boolean { + return panel.group.panels.some((p) => p.id === "chat" || p.id.startsWith("session:")); +} + +/** Auto-activate the changes panel unless it shares a group with agent sessions. */ function autoActivateChangesPanel(): void { - const { api, rightTopGroupId } = useDockviewStore.getState(); + const { api } = useDockviewStore.getState(); if (!api) return; const panel = api.getPanel("changes"); - // Only auto-focus when the panel is in the right sidebar. - // When it's in the center group (e.g. plan mode layout), never steal focus - // from the active chat/session panel. - if (panel && panel.group.id === rightTopGroupId) { - panel.api.setActive(); - } + if (!panel || groupContainsAgentSessionPanel(panel)) return; + panel.api.setActive(); } /** @@ -48,6 +54,7 @@ export function ChangesTab(props: IDockviewPanelHeaderProps) { const prevTotalRef = useRef(totalCount); const seenCountRef = useRef(api.isActive ? totalCount : 0); + const activeSessionRef = useRef(activeSessionId); const flashTimerRef = useRef | null>(null); // Armed once we know the initial git data has settled. Until then, any // 0→N transition is treated as an initial load, not a real new change. @@ -69,6 +76,14 @@ export function ChangesTab(props: IDockviewPanelHeaderProps) { // React to totalCount changes: auto-activate, flash, badge useEffect(() => { + if (activeSessionRef.current !== activeSessionId) { + activeSessionRef.current = activeSessionId; + prevTotalRef.current = totalCount; + seenCountRef.current = api.isActive ? totalCount : 0; + initializedRef.current = false; + setBadgeCount(0); + } + if (api.isActive) { seenCountRef.current = totalCount; } @@ -79,13 +94,14 @@ export function ChangesTab(props: IDockviewPanelHeaderProps) { const increased = totalCount > prev && totalCount > 0; const decreased = totalCount < prev; - // Auto-activate when changes appear for the first time (0 → N), but only - // after initial git data has settled. gitStatusLoaded is false until the - // first WS git-status event arrives, guaranteeing data has loaded before we - // arm auto-activate (handles both page-refresh and clean-session cases). + // Auto-activate on real post-load updates, but only after initial git data + // has settled. gitStatusLoaded is false until the first WS git-status event + // arrives, guaranteeing existing changes on page refresh do not steal focus. if (!initializedRef.current) { if (gitStatusLoaded) initializedRef.current = true; - } else if (increased && prev === 0) { + } else if (increased) { + // The product behavior is to surface every new git update unless the + // changes panel shares a group with agent session panels. autoActivateChangesPanel(); } @@ -100,7 +116,7 @@ export function ChangesTab(props: IDockviewPanelHeaderProps) { const unseen = Math.max(0, totalCount - seenCountRef.current); requestAnimationFrame(() => setBadgeCount(unseen)); } - }, [totalCount, api, gitStatusLoaded]); + }, [totalCount, api, gitStatusLoaded, activeSessionId]); // Cleanup flash timer on unmount useEffect(() => { diff --git a/apps/web/e2e/tests/layout/changes-panel-focus.spec.ts b/apps/web/e2e/tests/layout/changes-panel-focus.spec.ts index a1ade322d1..e04563a66e 100644 --- a/apps/web/e2e/tests/layout/changes-panel-focus.spec.ts +++ b/apps/web/e2e/tests/layout/changes-panel-focus.spec.ts @@ -1,4 +1,5 @@ import { test, expect } from "../../fixtures/test-base"; +import type { Page } from "@playwright/test"; import { SessionPage } from "../../pages/session-page"; import { makeGitEnv } from "../../helpers/git-helper"; import fs from "node:fs"; @@ -29,6 +30,53 @@ class GitHelper { } } +function changesTab(testPage: Page) { + return testPage.locator(".dv-tab", { + has: testPage.locator(".dv-default-tab").filter({ hasText: /^Changes/ }), + }); +} + +async function moveChangesToTerminalGroupAndFocusTerminal(testPage: Page) { + await testPage.evaluate(() => { + type Group = { id: string }; + type PanelApi = { + moveTo: (opts: { group: Group }) => void; + setActive: () => void; + }; + type Panel = { id: string; api: PanelApi; group?: Group }; + type Api = { getPanel: (id: string) => Panel | undefined }; + const dockview = (window as unknown as { __dockviewApi__?: Api }).__dockviewApi__; + const changes = dockview?.getPanel("changes"); + const terminal = dockview?.getPanel("terminal-default"); + if (!changes || !terminal?.group) { + throw new Error("Dockview changes or terminal panel was not available"); + } + changes.api.moveTo({ group: terminal.group }); + terminal.api.setActive(); + }); +} + +async function moveChangesToChatGroupAndFocusChat(testPage: Page) { + await testPage.evaluate(() => { + type Group = { id: string }; + type PanelApi = { + moveTo: (opts: { group: Group }) => void; + setActive: () => void; + }; + type Panel = { id: string; api: PanelApi; group?: Group }; + type Api = { panels: Panel[]; getPanel: (id: string) => Panel | undefined }; + const dockview = (window as unknown as { __dockviewApi__?: Api }).__dockviewApi__; + const changes = dockview?.getPanel("changes"); + const chat = + dockview?.getPanel("chat") ?? dockview?.panels.find((p) => p.id.startsWith("session:")); + if (!changes || !chat?.group) { + throw new Error("Dockview changes or chat panel was not available"); + } + changes.api.moveTo({ group: chat.group }); + chat.api.setActive(); + }); +} + test.describe("Changes panel focus behavior", () => { /** * Verifies the changes panel does NOT steal focus from the chat tab @@ -99,12 +147,7 @@ test.describe("Changes panel focus behavior", () => { await expect(session.chat).toBeVisible({ timeout: 5_000 }); }); - /** - * Verifies the changes panel does NOT auto-focus when it is in the center - * group (e.g. plan mode layout). Even when new changes appear, the chat - * panel should stay focused. - */ - test("changes panel does not auto-focus when in center group", async ({ + test("changes panel does not auto-focus when grouped with agent session panels", async ({ testPage, apiClient, seedData, @@ -130,17 +173,13 @@ test.describe("Changes panel focus behavior", () => { const session = new SessionPage(testPage); await session.waitForLoad(); await session.waitForChatIdle({ timeout: 30_000 }); + await session.waitForDockviewReady(); - // Toggle plan mode — this moves changes into the center group - await session.togglePlanMode(); - await expect(session.planPanel).toBeVisible({ timeout: 10_000 }); + await moveChangesToChatGroupAndFocusChat(testPage); await expect(session.chat).toBeVisible(); - // Verify the chat/session tab is active, not changes - const changesTab = testPage.locator(".dv-default-tab:has-text('Changes')"); - await expect(changesTab).not.toHaveClass(/dv-active-tab/, { timeout: 5_000 }); + await expect(changesTab(testPage)).not.toHaveClass(/dv-active-tab/, { timeout: 5_000 }); - // Create new changes — this would previously trigger auto-activate git.createFile("new-file.txt", "new content"); git.stageAll(); git.commit("new commit"); @@ -153,10 +192,53 @@ test.describe("Changes panel focus behavior", () => { // Wait a bit for any async auto-activate to fire await testPage.waitForTimeout(2_000); - // Changes tab should NOT have stolen focus - await expect(changesTab).not.toHaveClass(/dv-active-tab/, { timeout: 5_000 }); + await expect(changesTab(testPage)).not.toHaveClass(/dv-active-tab/, { timeout: 5_000 }); - // Chat should still be visible (active) await expect(session.chat).toBeVisible(); }); + + test("new git updates focus the changes tab in its current non-agent group", async ({ + testPage, + apiClient, + seedData, + backend, + }) => { + test.setTimeout(90_000); + + const repoDir = path.join(backend.tmpDir, "repos", "e2e-repo"); + const gitEnv = makeGitEnv(backend.tmpDir); + const git = new GitHelper(repoDir, gitEnv); + + const task = await apiClient.createTaskWithAgent( + seedData.workspaceId, + "Moved changes panel focus test", + seedData.agentProfileId, + { + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + repository_ids: [seedData.repositoryId], + }, + ); + await testPage.goto(`/t/${task.id}`); + const session = new SessionPage(testPage); + await session.waitForLoad(); + await session.waitForChatIdle({ timeout: 30_000 }); + await session.waitForDockviewReady(); + + git.createFile("first-change.txt", "one"); + await expect( + testPage.locator(".dv-default-tab").filter({ hasText: /^Changes \(1\)$/ }), + ).toBeVisible({ timeout: 15_000 }); + + await moveChangesToTerminalGroupAndFocusTerminal(testPage); + await expect(changesTab(testPage)).not.toHaveClass(/dv-active-tab/, { timeout: 5_000 }); + + git.createFile("second-change.txt", "two"); + await expect( + testPage.locator(".dv-default-tab").filter({ hasText: /^Changes \(2\)$/ }), + ).toBeVisible({ timeout: 15_000 }); + + await expect(changesTab(testPage)).toHaveClass(/dv-active-tab/, { timeout: 5_000 }); + await expect(session.changesFileRow("second-change.txt")).toBeVisible({ timeout: 10_000 }); + }); });