Skip to content
Python

fix(gui): ignore stale artifact responses

jeremy gu37a733b728d8
diff --git a/surfaces/gui/src/components/RightRail.requests.test.tsx b/surfaces/gui/src/components/RightRail.requests.test.tsx
new file mode 100644
index 00000000..3d70fdd6
--- /dev/null
+++ b/surfaces/gui/src/components/RightRail.requests.test.tsx
@@ -0,0 +1,126 @@
+// Async artifact reads are session-owned: late work must never overwrite the active rail.
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import type { ArtifactContent, ArtifactInfo } from "../api";
+import { getArtifacts, readArtifact } from "../api";
+import { RightRail } from "./RightRail";
+
+vi.mock("../api", () => ({
+ getArtifacts: vi.fn(),
+ readArtifact: vi.fn(),
+ revealArtifact: vi.fn(),
+}));
+
+vi.mock("./AccessSection", () => ({ AccessSection: () => null }));
+
+const artifact = (name: string): ArtifactInfo => ({
+ path: name,
+ name,
+ kind: "markdown",
+ size: 10,
+ modified_at: 1,
+});
+
+const content = (path: string, body: string): ArtifactContent => ({
+ ok: true,
+ path,
+ kind: "markdown",
+ content: body,
+});
+
+function deferred<T>() {
+ let resolve!: (value: T) => void;
+ let reject!: (reason?: unknown) => void;
+ const promise = new Promise<T>((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ return { promise, resolve, reject };
+}
+
+const baseProps = {
+ active: true,
+ sessionId: "session-a",
+ refreshKey: 0,
+ toolNames: [],
+ todo: [],
+ running: false,
+};
+
+afterEach(() => {
+ cleanup();
+ vi.clearAllMocks();
+});
+
+describe("RightRail artifact request ownership", () => {
+ it("clears the previous session's artifacts while the current list is loading", async () => {
+ const currentRequest = deferred<ArtifactInfo[]>();
+ vi.mocked(getArtifacts)
+ .mockResolvedValueOnce([artifact("previous.md")])
+ .mockReturnValueOnce(currentRequest.promise);
+
+ const { rerender } = render(<RightRail {...baseProps} />);
+ expect(await screen.findByText("previous.md")).toBeTruthy();
+
+ rerender(<RightRail {...baseProps} sessionId="session-b" />);
+ expect(screen.queryByText("previous.md")).toBeNull();
+
+ await act(async () => currentRequest.resolve([artifact("current.md")]));
+ expect(screen.getByText("current.md")).toBeTruthy();
+ });
+
+ it("ignores an artifact-list response from the previous session", async () => {
+ const oldRequest = deferred<ArtifactInfo[]>();
+ const currentRequest = deferred<ArtifactInfo[]>();
+ vi.mocked(getArtifacts)
+ .mockReturnValueOnce(oldRequest.promise)
+ .mockReturnValueOnce(currentRequest.promise);
+
+ const { rerender } = render(<RightRail {...baseProps} />);
+ rerender(<RightRail {...baseProps} sessionId="session-b" />);
+
+ await act(async () => currentRequest.resolve([artifact("current.md")]));
+ expect(screen.getByText("current.md")).toBeTruthy();
+
+ await act(async () => oldRequest.resolve([artifact("stale.md")]));
+ expect(screen.getByText("current.md")).toBeTruthy();
+ expect(screen.queryByText("stale.md")).toBeNull();
+ });
+
+ it("ignores a rejected artifact-list request after a newer request succeeds", async () => {
+ const oldRequest = deferred<ArtifactInfo[]>();
+ const currentRequest = deferred<ArtifactInfo[]>();
+ vi.mocked(getArtifacts)
+ .mockReturnValueOnce(oldRequest.promise)
+ .mockReturnValueOnce(currentRequest.promise);
+
+ const { rerender } = render(<RightRail {...baseProps} />);
+ rerender(<RightRail {...baseProps} sessionId="session-b" />);
+
+ await act(async () => currentRequest.resolve([artifact("current.md")]));
+ await act(async () => oldRequest.reject(new Error("stale failure")));
+
+ expect(screen.getByText("current.md")).toBeTruthy();
+ });
+
+ it("ignores file content that finishes after the user selects another artifact", async () => {
+ const firstRead = deferred<ArtifactContent>();
+ const secondRead = deferred<ArtifactContent>();
+ vi.mocked(getArtifacts).mockResolvedValue([artifact("first.md"), artifact("second.md")]);
+ vi.mocked(readArtifact).mockImplementation((_sessionId, path) =>
+ path === "first.md" ? firstRead.promise : secondRead.promise,
+ );
+
+ render(<RightRail {...baseProps} />);
+ fireEvent.click(await screen.findByText("first.md"));
+ fireEvent.click(screen.getByLabelText("Back to artifacts"));
+ fireEvent.click(screen.getByText("second.md"));
+
+ await act(async () => secondRead.resolve(content("second.md", "Current content")));
+ expect(screen.getByText("Current content")).toBeTruthy();
+
+ await act(async () => firstRead.resolve(content("first.md", "Stale content")));
+ expect(screen.getByText("Current content")).toBeTruthy();
+ expect(screen.queryByText("Stale content")).toBeNull();
+ });
+});
diff --git a/surfaces/gui/src/components/RightRail.tsx b/surfaces/gui/src/components/RightRail.tsx
index ac3ea669..a7d9f8ed 100644
--- a/surfaces/gui/src/components/RightRail.tsx
+++ b/surfaces/gui/src/components/RightRail.tsx
@@ -83,25 +83,71 @@ export function RightRail({
const [artifacts, setArtifacts] = useState<ArtifactInfo[]>([]);
const [selected, setSelected] = useState<ArtifactInfo | null>(null);
const [content, setContent] = useState<ArtifactContent | null>(null);
+ const artifactListRequest = useRef(0);
+ const artifactContentRequest = useRef(0);
+
+ // Every refresh owns a monotonically increasing request id. Session switches, live refreshes,
+ // and manual refreshes can overlap; only the newest response may update this rail.
+ const refreshArtifacts = (
+ onLoaded?: (list: ArtifactInfo[]) => void,
+ onError?: () => void,
+ ) => {
+ const request = ++artifactListRequest.current;
+ return getArtifacts(sessionId)
+ .then((list) => {
+ if (request !== artifactListRequest.current) return;
+ setArtifacts(list);
+ onLoaded?.(list);
+ })
+ .catch(() => {
+ if (request !== artifactListRequest.current) return;
+ setArtifacts([]);
+ onError?.();
+ });
+ };
- const refreshArtifacts = () => getArtifacts(sessionId).then(setArtifacts).catch(() => setArtifacts([]));
-
- useEffect(() => {
- if (!active) return;
- if (showArtifacts) refreshArtifacts();
- }, [active, sessionId, refreshKey, showArtifacts]);
+ const loadArtifactContent = (artifact: ArtifactInfo) => {
+ const request = ++artifactContentRequest.current;
+ setContent(null);
+ return readArtifact(sessionId, artifact.path)
+ .then((next) => {
+ if (request === artifactContentRequest.current) setContent(next);
+ })
+ .catch(() => {
+ if (request === artifactContentRequest.current) setContent(null);
+ });
+ };
- // Switching conversations closes any open artifact — it belongs to the previous session's
- // workspace, which the new session can't (and shouldn't) read.
+ // Artifacts are session-owned. Drop the previous session's visible state before starting
+ // this session's fetch so filenames never linger while the replacement request is pending.
useEffect(() => {
+ artifactListRequest.current += 1;
+ artifactContentRequest.current += 1;
+ setArtifacts([]);
setSelected(null);
setContent(null);
}, [sessionId]);
useEffect(() => {
- setContent(null);
- if (!selected) return;
- readArtifact(sessionId, selected.path).then(setContent).catch(() => setContent(null));
+ if (!active || !showArtifacts) {
+ artifactListRequest.current += 1;
+ return;
+ }
+ refreshArtifacts();
+ return () => {
+ artifactListRequest.current += 1;
+ };
+ }, [active, sessionId, refreshKey, showArtifacts]);
+
+ useEffect(() => {
+ if (selected) loadArtifactContent(selected);
+ else {
+ artifactContentRequest.current += 1;
+ setContent(null);
+ }
+ return () => {
+ artifactContentRequest.current += 1;
+ };
}, [selected?.path, sessionId]);
// Notify the app when a preview opens/closes (drives the left-nav auto-collapse).
@@ -111,8 +157,7 @@ export function RightRail({
const reloadSelected = () => {
if (!selected) return Promise.resolve();
- setContent(null);
- return readArtifact(sessionId, selected.path).then(setContent).catch(() => setContent(null));
+ return loadArtifactContent(selected);
};
// §34 (UX-016): [Title](artifact:path) chips in the transcript open the viewer directly.
@@ -137,12 +182,10 @@ export function RightRail({
setSelected(found);
return;
}
- getArtifacts(sessionId)
- .then((list) => {
- setArtifacts(list);
- setSelected(match(list, path) ?? minimal(path));
- })
- .catch(() => setSelected(minimal(path)));
+ refreshArtifacts(
+ (list) => setSelected(match(list, path) ?? minimal(path)),
+ () => setSelected(minimal(path)),
+ );
};
window.addEventListener(OPEN_ARTIFACT_EVENT, onOpen);
return () => window.removeEventListener(OPEN_ARTIFACT_EVENT, onOpen);