Python
Sidebar expand sticks while the artifact viewer is open
Rohit C Prasad4eefc09a843f
diff --git a/surfaces/gui/e2e/artifacts.spec.ts b/surfaces/gui/e2e/artifacts.spec.tsindex a8706a08..b71a8358 100644--- a/surfaces/gui/e2e/artifacts.spec.ts+++ b/surfaces/gui/e2e/artifacts.spec.ts@@ -71,3 +71,19 @@ test("Artifacts section renders for a folder-gated coworker too (universal scratawait page.getByTestId("rail-toggle-artifacts").click();await expect(page.locator(".artifact-row", { hasText: "security-review.html" })).toBeVisible();});++test("Show sidebar sticks while the artifact viewer is open", async ({ page }) => {+ // Owner-hit 2026-08-21: opening the viewer auto-collapses the nav (one-shot+ // courtesy), but clicking "Show sidebar" then instantly re-collapsed it — the+ // notify effect replayed "open" on a callback identity change. The user's+ // explicit toggle must win.+ await openReport(page);+ await expect(page.getByRole("button", { name: "Show sidebar" })).toBeVisible();++ await page.getByRole("button", { name: "Show sidebar" }).click();+ await page.waitForTimeout(400); // give a regression time to re-collapse+ await expect(page.getByRole("button", { name: "Show sidebar" })).toHaveCount(0);+ await expect(page.getByText("New session").first()).toBeVisible();+ // The viewer stays open too — expanding the nav is navigation, not dismissal.+ await expect(page.getByTestId("artifact-frame")).toBeVisible();+});diff --git a/surfaces/gui/src/App.tsx b/surfaces/gui/src/App.tsxindex 3a033d13..b04f353f 100644--- a/surfaces/gui/src/App.tsx+++ b/surfaces/gui/src/App.tsx@@ -304,16 +304,22 @@ export function App() {}, [navCollapsed, setNavCollapsedPersist]);// #3: collapse the nav while a full artifact preview is open, restore it on close (unless the// user manually toggled meanwhile). The collapse is transient — it never overwrites the pref.+ // STABLE identity (no deps): depending on navCollapsed changed this callback's identity on+ // every nav toggle, which re-ran the rail's notify effect with the viewer still open and+ // re-collapsed the nav the instant the user expanded it (owner-hit 2026-08-21). The current+ // collapse state is read through the functional updater instead.const onArtifactPreview = useCallback((open: boolean) => {if (open) {- if (navBeforePreview.current === null) navBeforePreview.current = navCollapsed;setNavPeek(false);- setNavCollapsed(true);+ setNavCollapsed((cur) => {+ if (navBeforePreview.current === null) navBeforePreview.current = cur;+ return true;+ });} else if (navBeforePreview.current !== null) {setNavCollapsed(navBeforePreview.current);navBeforePreview.current = null;}- }, [navCollapsed]);+ }, []);useEffect(() => {const onKey = (e: KeyboardEvent) => {if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "b") {diff --git a/surfaces/gui/src/components/RightRail.preview.test.tsx b/surfaces/gui/src/components/RightRail.preview.test.tsxnew file mode 100644index 00000000..08e49562--- /dev/null+++ b/surfaces/gui/src/components/RightRail.preview.test.tsx@@ -0,0 +1,59 @@+// The rail's preview notification must be edge-triggered: a new onPreviewChange+// identity (App re-renders whenever the nav toggles) must NOT replay "open" while+// the viewer sits open — that re-collapsed a sidebar the user had just expanded+// (owner-hit 2026-08-21).+import { act, render } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import { RightRail } from "./RightRail";+import { OPEN_ARTIFACT_EVENT } from "./Markdown";++vi.mock("../api", async () => {+ const actual: any = await vi.importActual("../api");+ return {+ ...actual,+ getArtifacts: vi.fn().mockResolvedValue([]),+ getRoots: vi.fn().mockResolvedValue([]),+ getJournalCases: vi.fn().mockResolvedValue([]),+ readArtifact: vi.fn().mockResolvedValue({ ok: true, path: "r.md", kind: "markdown", content: "x" }),+ revealArtifact: vi.fn().mockResolvedValue({ ok: true }),+ };+});++function rail(onPreviewChange: (open: boolean) => void) {+ return (+ <RightRail+ active+ sessionId="s1"+ refreshKey={0}+ toolNames={[]}+ todo={[]}+ running={false}+ onPreviewChange={onPreviewChange}+ />+ );+}++describe("RightRail preview notification", () => {+ it("fires only on open/close transitions, not on callback identity changes", async () => {+ const first = vi.fn();+ const { rerender } = render(rail(first));+ await act(async () => {});+ expect(first).not.toHaveBeenCalled(); // closed at mount: no "closed" replay either++ // Open the viewer via a transcript chip event.+ await act(async () => {+ window.dispatchEvent(+ new CustomEvent(OPEN_ARTIFACT_EVENT, { detail: { path: "r.md" } }),+ );+ });+ expect(first).toHaveBeenCalledTimes(1);+ expect(first).toHaveBeenLastCalledWith(true);++ // App re-renders with a NEW callback identity (e.g. the user expanded the nav).+ const second = vi.fn();+ rerender(rail(second));+ await act(async () => {});+ // The viewer never transitioned, so the new callback must not be told "open".+ expect(second).not.toHaveBeenCalled();+ });+});diff --git a/surfaces/gui/src/components/RightRail.tsx b/surfaces/gui/src/components/RightRail.tsxindex 07e5589a..2dd73bab 100644--- a/surfaces/gui/src/components/RightRail.tsx+++ b/surfaces/gui/src/components/RightRail.tsx@@ -176,8 +176,16 @@ export function RightRail({}, [selected?.path, sessionId]);// Notify the app when a preview opens/closes (drives the left-nav auto-collapse).+ // Edge-triggered on the ACTUAL transition — a callback-identity change must never+ // replay "open" while the viewer sits open (that re-collapsed a nav the user had+ // just expanded; owner-hit 2026-08-21).+ const prevPreviewOpen = useRef(false);useEffect(() => {- onPreviewChange?.(!!selected);+ const open = !!selected;+ if (open !== prevPreviewOpen.current) {+ prevPreviewOpen.current = open;+ onPreviewChange?.(open);+ }}, [!!selected, onPreviewChange]);const reloadSelected = () => {