From e1f06323258d6d66208f69a7afd382995cd7a6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Mon, 20 Apr 2026 18:52:45 +0100 Subject: [PATCH] fix(web): anchor PR detail panel to session group on auto-open (#646) --- .../task/__tests__/use-auto-pr-panel.test.ts | 34 ++++- .../components/task/dockview-session-tabs.ts | 30 +++- apps/web/e2e/pages/session-page.ts | 36 +++++ .../e2e/tests/pr/pr-detail-auto-show.spec.ts | 141 ++++++++++++++++++ apps/web/lib/state/dockview-store.ts | 5 + 5 files changed, 239 insertions(+), 7 deletions(-) diff --git a/apps/web/components/task/__tests__/use-auto-pr-panel.test.ts b/apps/web/components/task/__tests__/use-auto-pr-panel.test.ts index 87ef233bbc..9cf9b323a6 100644 --- a/apps/web/components/task/__tests__/use-auto-pr-panel.test.ts +++ b/apps/web/components/task/__tests__/use-auto-pr-panel.test.ts @@ -1,5 +1,15 @@ import { describe, it, expect } from "vitest"; -import { shouldAutoAddPRPanel } from "../dockview-session-tabs"; +import type { DockviewApi } from "dockview-react"; +import { shouldAutoAddPRPanel, resolvePRPanelTargetGroup } from "../dockview-session-tabs"; + +function makeApi(panels: Array<{ id: string; groupId: string }>): DockviewApi { + return { + getPanel(id: string) { + const p = panels.find((x) => x.id === id); + return p ? { id: p.id, group: { id: p.groupId } } : undefined; + }, + } as unknown as DockviewApi; +} describe("shouldAutoAddPRPanel", () => { const base = { @@ -50,3 +60,25 @@ describe("shouldAutoAddPRPanel", () => { ).toBe("add"); }); }); + +describe("resolvePRPanelTargetGroup", () => { + it("returns the session chat panel's live group when it exists", () => { + // Regression: previously the PR panel was anchored to the store's + // centerGroupId, which could lag behind layout transitions and drop the + // PR panel in a split instead of as a tab next to the session. + const api = makeApi([{ id: "session:abc", groupId: "group-live-center" }]); + expect(resolvePRPanelTargetGroup(api, "abc", "stale-center-id")).toBe("group-live-center"); + }); + + it("falls back to centerGroupId when the session panel is missing", () => { + const api = makeApi([]); + expect(resolvePRPanelTargetGroup(api, "abc", "center-id")).toBe("center-id"); + }); + + it("prefers the session panel even when its group differs from centerGroupId", () => { + // centerGroupId still points at the old session's group during a switch; + // the new session's chat panel is the authoritative anchor. + const api = makeApi([{ id: "session:new", groupId: "group-new" }]); + expect(resolvePRPanelTargetGroup(api, "new", "group-old")).toBe("group-new"); + }); +}); diff --git a/apps/web/components/task/dockview-session-tabs.ts b/apps/web/components/task/dockview-session-tabs.ts index bf1502fad6..47e1f875d3 100644 --- a/apps/web/components/task/dockview-session-tabs.ts +++ b/apps/web/components/task/dockview-session-tabs.ts @@ -1,5 +1,5 @@ import { useEffect, useRef } from "react"; -import type { DockviewReadyEvent, AddPanelOptions } from "dockview-react"; +import type { DockviewApi, DockviewReadyEvent, AddPanelOptions } from "dockview-react"; import type { StoreApi } from "zustand"; import type { AppState } from "@/lib/state/store"; import { useDockviewStore } from "@/lib/state/dockview-store"; @@ -99,6 +99,23 @@ export function shouldAutoAddPRPanel(params: { return "add"; } +/** + * Resolve the group ID to anchor the PR detail panel to. + * + * Preference: the live session chat panel's group. It's the group the user is + * actively looking at, and reading it directly avoids the stale-id window the + * store's centerGroupId has across layout transitions (which caused the PR + * panel to land in a split instead of as a tab next to the session). + */ +export function resolvePRPanelTargetGroup( + api: DockviewApi, + sessionId: string, + centerGroupId: string, +): string { + const sessionPanel = api.getPanel(`session:${sessionId}`); + return sessionPanel?.group?.id ?? centerGroupId; +} + /** * Auto-add the PR detail panel to the center group when the active task * has an associated pull request. The panel is added as a background tab @@ -138,15 +155,16 @@ export function useAutoPRPanel() { } if (decision === "add") { - const { centerGroupId } = useDockviewStore.getState(); - // Route through focusOrAddPanel so a stale centerGroupId falls back - // to another non-sidebar group rather than letting dockview place - // the panel in the active group (which may be the sidebar). + const targetGroupId = resolvePRPanelTargetGroup( + api, + sessionId, + useDockviewStore.getState().centerGroupId, + ); focusOrAddPanel(api, { id: "pr-detail", component: "pr-detail", title: "Pull Request", - position: { referenceGroup: centerGroupId }, + position: { referenceGroup: targetGroupId }, inactive: true, }); markPRPanelOffered(sessionId); diff --git a/apps/web/e2e/pages/session-page.ts b/apps/web/e2e/pages/session-page.ts index ac4519d605..fa44dea28c 100644 --- a/apps/web/e2e/pages/session-page.ts +++ b/apps/web/e2e/pages/session-page.ts @@ -342,6 +342,42 @@ export class SessionPage { return this.page.getByTestId("pr-detail-panel"); } + /** + * Assert the `pr-detail` panel's dockview group contains at least one + * `session:{sessionId}` panel — i.e. the PR opened as a tab next to a + * session chat, not as a split in a separate group. Regression guard for + * the "PR opens in a split instead of the center tab" bug. + * + * Checks group membership of the PR panel directly rather than picking a + * session panel first, so the assertion is deterministic even when outgoing + * and incoming session panels briefly coexist during a task switch. + */ + async expectPrPanelAndSessionShareGroup(): Promise { + const result = await this.page.evaluate(() => { + type Panel = { id: string; group?: { id?: string } }; + type Api = { panels: Panel[]; getPanel: (i: string) => Panel | undefined }; + const api = (window as unknown as { __dockviewApi__?: Api }).__dockviewApi__; + if (!api) return { error: "dockview api not exposed" }; + const pr = api.getPanel("pr-detail"); + if (!pr) return { error: "pr-detail panel missing" }; + const prGroupId = pr.group?.id ?? null; + const sessionPanels = api.panels.filter((p) => p.id.startsWith("session:")); + if (sessionPanels.length === 0) return { error: "no session panel" }; + const sessionInPrGroup = sessionPanels.some((p) => p.group?.id === prGroupId); + return { + sessionInPrGroup, + prGroupId, + sessionLocations: sessionPanels.map((p) => `${p.id}@${p.group?.id ?? "?"}`), + }; + }); + expect(result.error, result.error).toBeUndefined(); + expect( + result.sessionInPrGroup, + `PR panel landed in a dockview group that contains no session chat. ` + + `PR group=${result.prGroupId} sessions=[${result.sessionLocations?.join(", ")}]`, + ).toBe(true); + } + /** Dockview tab for the PR detail panel (title starts as "Pull Request", updated to "PR #N"). */ prDetailTab(): Locator { return this.page.locator(".dv-default-tab").filter({ hasText: /^(Pull Request|PR #\d+)$/ }); diff --git a/apps/web/e2e/tests/pr/pr-detail-auto-show.spec.ts b/apps/web/e2e/tests/pr/pr-detail-auto-show.spec.ts index 49c39a01ef..dcedcd6581 100644 --- a/apps/web/e2e/tests/pr/pr-detail-auto-show.spec.ts +++ b/apps/web/e2e/tests/pr/pr-detail-auto-show.spec.ts @@ -338,4 +338,145 @@ test.describe("PR detail panel", () => { await session.waitForLoad(); await expect(session.prDetailTab()).toBeVisible({ timeout: 15_000 }); }); + + /** + * Regression: the PR detail panel must auto-open as a tab inside the + * session's dockview group, not as a split in a separate group. The bug + * triggered intermittently when opening/switching PR-linked tasks because + * `useAutoPRPanel` used a stale `centerGroupId` from the store and fell + * through dockview's default placement (a new group to the right). + * + * Setup: + * Inbox → Working (auto_start, on_turn_complete → Done) → Done + * Task A (with PR #401), Task B (with PR #402) + */ + test("auto-shows PR panel as a tab in the session group, not a split", async ({ + testPage, + apiClient, + seedData, + }) => { + test.setTimeout(120_000); + + const workflow = await apiClient.createWorkflow(seedData.workspaceId, "PR Placement Workflow"); + + const inboxStep = await apiClient.createWorkflowStep(workflow.id, "Inbox", 0); + const workingStep = await apiClient.createWorkflowStep(workflow.id, "Working", 1); + const doneStep = await apiClient.createWorkflowStep(workflow.id, "Done", 2); + + await apiClient.updateWorkflowStep(workingStep.id, { + prompt: 'e2e:message("done")\n{{task_prompt}}', + events: { + on_enter: [{ type: "auto_start_agent" }], + on_turn_complete: [{ type: "move_to_step", config: { step_id: doneStep.id } }], + }, + }); + + await apiClient.saveUserSettings({ + workspace_id: seedData.workspaceId, + workflow_filter_id: workflow.id, + enable_preview_on_click: false, + }); + + await apiClient.mockGitHubReset(); + await apiClient.mockGitHubSetUser("test-user"); + await apiClient.mockGitHubAddPRs([ + { + number: 401, + title: "First PR", + state: "open", + head_branch: "feat/first", + base_branch: "main", + author_login: "test-user", + repo_owner: "testorg", + repo_name: "testrepo", + additions: 10, + deletions: 2, + }, + { + number: 402, + title: "Second PR", + state: "open", + head_branch: "feat/second", + base_branch: "main", + author_login: "test-user", + repo_owner: "testorg", + repo_name: "testrepo", + additions: 20, + deletions: 4, + }, + ]); + + const taskA = await apiClient.createTask(seedData.workspaceId, "First PR Task", { + workflow_id: workflow.id, + workflow_step_id: inboxStep.id, + agent_profile_id: seedData.agentProfileId, + repository_ids: [seedData.repositoryId], + }); + const taskB = await apiClient.createTask(seedData.workspaceId, "Second PR Task", { + workflow_id: workflow.id, + workflow_step_id: inboxStep.id, + agent_profile_id: seedData.agentProfileId, + repository_ids: [seedData.repositoryId], + }); + + const kanban = new KanbanPage(testPage); + await kanban.goto(); + + await apiClient.moveTask(taskA.id, workflow.id, workingStep.id); + await apiClient.moveTask(taskB.id, workflow.id, workingStep.id); + await apiClient.mockGitHubAssociateTaskPR({ + task_id: taskA.id, + owner: "testorg", + repo: "testrepo", + pr_number: 401, + pr_url: "https://github.com/testorg/testrepo/pull/401", + pr_title: "First PR", + head_branch: "feat/first", + base_branch: "main", + author_login: "test-user", + additions: 10, + deletions: 2, + }); + await apiClient.mockGitHubAssociateTaskPR({ + task_id: taskB.id, + owner: "testorg", + repo: "testrepo", + pr_number: 402, + pr_url: "https://github.com/testorg/testrepo/pull/402", + pr_title: "Second PR", + head_branch: "feat/second", + base_branch: "main", + author_login: "test-user", + additions: 20, + deletions: 4, + }); + + await expect(kanban.taskCardInColumn("First PR Task", doneStep.id)).toBeVisible({ + timeout: 45_000, + }); + await expect(kanban.taskCardInColumn("Second PR Task", doneStep.id)).toBeVisible({ + timeout: 45_000, + }); + + // Open Task A and wait for the PR panel to auto-show. + await kanban.taskCardInColumn("First PR Task", doneStep.id).click(); + await expect(testPage).toHaveURL(/\/t\//, { timeout: 15_000 }); + + const session = new SessionPage(testPage); + await session.waitForLoad(); + await session.idleInput().waitFor({ state: "visible", timeout: 30_000 }); + await expect(session.prDetailTab()).toBeVisible({ timeout: 15_000 }); + + // Invariant: PR panel shares the session's dockview group (is a tab, not a split). + await session.expectPrPanelAndSessionShareGroup(); + + // Switch to Task B — the race path that triggered the original bug. The + // session changes, a new layout is resolved, and the PR auto-add hook runs + // against the new session. Assert the invariant still holds. + await session.clickTaskInSidebar("Second PR Task"); + await session.waitForLoad(); + await session.idleInput().waitFor({ state: "visible", timeout: 30_000 }); + await expect(session.prDetailTab()).toBeVisible({ timeout: 15_000 }); + await session.expectPrPanelAndSessionShareGroup(); + }); }); diff --git a/apps/web/lib/state/dockview-store.ts b/apps/web/lib/state/dockview-store.ts index d9fcc32eb4..78c466b710 100644 --- a/apps/web/lib/state/dockview-store.ts +++ b/apps/web/lib/state/dockview-store.ts @@ -608,6 +608,11 @@ export const useDockviewStore = create((set, get) => ({ activeFilePath: null, setApi: (api) => { set({ api, activeFilePath: null }); + if (typeof window !== "undefined") { + // Exposed for E2E tests to assert on panel/group placement. Harmless in + // prod; the DockviewApi is already reachable via the store in devtools. + (window as unknown as { __dockviewApi__: DockviewApi | null }).__dockviewApi__ = api; + } if (api) { api.onDidActivePanelChange((event) => { const id = event?.id;