fix(frontend): tolerate project attachment storage failures (#8347)

* fix(frontend): tolerate project attachment storage failures

* fix(frontend): tolerate project attachment storage failures

* test(frontend): cover project attachment storage failures

* test(frontend): keep composer state when storage fails

Added tests to verify behavior when storage operations fail.

* test(frontend): isolate storage spies across attachment tests

Refactored sessionStorage mock implementations for better readability and consistency across tests.

* test(frontend): restore attachment hook spies in cleanup

* test(frontend): cover project attachment storage degradation

* test(frontend): expand project attachment storage coverage

* fix(frontend): preserve pending attachments when storage reads fail

* fix(frontend): keep composer edits local after an unreadable attachment list

---------

Co-authored-by: KaimLee <KaimLee@users.noreply.github.com>
This commit is contained in:
KaimLee 2026-11-19 07:27:04 +08:00 committed by GitHub
parent 778dbe456d
commit ccc1d1ea19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 301 additions and 26 deletions

View File

@ -80,6 +80,12 @@ from the project page's Documents section:
a Save to project action.
- **Attach to thread**: copy a shelf file into a thread's uploads through the
normal ingestion pipeline, so the conversation can work with it directly.
The successful server response closes the picker and navigates to the target
conversation even when the browser blocks or fills `sessionStorage`. The
staged attachment handoff is best-effort in that case, so a blocked storage
area can prevent the attachment chip from surviving the route change or a
reload. If the browser temporarily blocks reading the pending list, adding
a document or clearing sent attachments leaves the saved list intact.
New shelf names, including explicit upload `name` and promotion `shelf_name`,
follow ordinary upload filename validation. Names containing NUL, Windows

View File

@ -43,6 +43,22 @@ function safeSessionStorage(): Storage | null {
}
}
function safeSetItem(storage: Storage, key: string, value: string): void {
try {
storage.setItem(key, value);
} catch {
// Storage can be unavailable or full. The caller keeps its in-memory state.
}
}
function safeRemoveItem(storage: Storage, key: string): void {
try {
storage.removeItem(key);
} catch {
// Storage can be unavailable. The caller keeps its in-memory state.
}
}
function isAttachment(value: unknown): value is AttachProjectDocumentResult {
return (
value !== null &&
@ -67,13 +83,15 @@ export function stageProjectAttachment(
if (!storage) {
return;
}
const pending = readProjectAttachments(threadId).filter(
const key = storageKey(threadId);
const pending = readStoredAttachments(storage, key);
if (pending === null) {
return;
}
const remaining = pending.filter(
(candidate) => candidate.virtual_path !== attachment.virtual_path,
);
storage.setItem(
storageKey(threadId),
JSON.stringify([...pending, attachment]),
);
safeSetItem(storage, key, JSON.stringify([...remaining, attachment]));
}
/**
@ -84,11 +102,25 @@ export function stageProjectAttachment(
export function readProjectAttachments(
threadId: string,
): AttachProjectDocumentResult[] {
const storage = safeSessionStorage();
return (
readStoredAttachments(safeSessionStorage(), storageKey(threadId)) ?? []
);
}
function readStoredAttachments(
storage: Storage | null,
key: string,
): AttachProjectDocumentResult[] | null {
if (!storage) {
return [];
return null;
}
let raw: string | null;
try {
raw = storage.getItem(key);
} catch {
// The list is unknown. Mutating callers must leave the stored value intact.
return null;
}
const raw = storage.getItem(storageKey(threadId));
if (raw === null) {
return [];
}
@ -103,7 +135,7 @@ export function readProjectAttachments(
} catch {
// Corrupt payload: drop it.
}
storage.removeItem(storageKey(threadId));
safeRemoveItem(storage, key);
return [];
}
@ -121,14 +153,19 @@ export function retireProjectAttachments(
if (!storage) {
return;
}
const key = storageKey(threadId);
const pending = readStoredAttachments(storage, key);
if (pending === null) {
return;
}
const sentPaths = new Set(sent.map((attachment) => attachment.virtual_path));
const remaining = readProjectAttachments(threadId).filter(
const remaining = pending.filter(
(attachment) => !sentPaths.has(attachment.virtual_path),
);
if (remaining.length === 0) {
storage.removeItem(storageKey(threadId));
safeRemoveItem(storage, key);
} else {
storage.setItem(storageKey(threadId), JSON.stringify(remaining));
safeSetItem(storage, key, JSON.stringify(remaining));
}
}
@ -149,11 +186,16 @@ export function useStagedProjectAttachments(threadId: string) {
AttachProjectDocumentResult[]
>([]);
const pendingRef = useRef<AttachProjectDocumentResult[]>([]);
const loadedThreadRef = useRef<string | null>(null);
useEffect(() => {
const pending = readProjectAttachments(threadId);
pendingRef.current = pending;
setAttachmentsState(pending);
const pending = readStoredAttachments(
safeSessionStorage(),
storageKey(threadId),
);
loadedThreadRef.current = pending === null ? null : threadId;
pendingRef.current = pending ?? [];
setAttachmentsState(pendingRef.current);
}, [threadId]);
const setAttachments = useCallback(
@ -161,12 +203,13 @@ export function useStagedProjectAttachments(threadId: string) {
const next =
typeof value === "function" ? value(pendingRef.current) : value;
pendingRef.current = next;
const storage = safeSessionStorage();
const storage =
loadedThreadRef.current === threadId ? safeSessionStorage() : null;
if (storage) {
if (next.length === 0) {
storage.removeItem(storageKey(threadId));
safeRemoveItem(storage, storageKey(threadId));
} else {
storage.setItem(storageKey(threadId), JSON.stringify(next));
safeSetItem(storage, storageKey(threadId), JSON.stringify(next));
}
}
setAttachmentsState(next);

View File

@ -39,6 +39,7 @@ const mocks = rs.hoisted(() => {
}),
deleteMutate: rs.fn(),
attachMutate: rs.fn(),
routerPush: rs.fn(),
promoteMutate: rs.fn(),
fetchPreview:
rs.fn<
@ -158,7 +159,7 @@ rs.mock("@/env", () => ({
}));
rs.mock("next/navigation", () => ({
useRouter: () => ({ push: rs.fn() }),
useRouter: () => ({ push: mocks.routerPush }),
}));
// The real preview pulls Streamdown/KaTeX into the worker bundle (several GB
@ -268,10 +269,51 @@ beforeEach(() => {
afterEach(() => {
cleanup();
rs.restoreAllMocks();
rs.clearAllMocks();
window.sessionStorage.clear();
});
describe("ProjectDocumentsSection", () => {
it.each(["getItem", "setItem"] as const)(
"closes the attach picker and navigates after ingestion succeeds even if %s fails",
async (method) => {
mocks.documents = [makeDocument()];
mocks.threadsPages = [
[makeAgentThread({ values: { title: "Target chat" } })],
];
mocks.attachMutate.mockImplementationOnce((_variables, options) => {
options.onSuccess?.({
filename: "roadmap.md",
size_bytes: 2048,
virtual_path: "/mnt/user-data/uploads/roadmap.md",
artifact_url:
"/api/threads/thread-a/artifacts/mnt/user-data/uploads/roadmap.md",
});
});
render(<ProjectDocumentsSection project={makeProject()} threads={[]} />, {
wrapper: Wrapper,
});
fireEvent.click(screen.getByRole("button", { name: /Attach to chat/ }));
const dialog = await screen.findByRole("dialog");
rs.spyOn(window.sessionStorage, method).mockImplementation(() => {
throw new DOMException(
"Storage unavailable",
method === "setItem" ? "QuotaExceededError" : "SecurityError",
);
});
fireEvent.click(
within(dialog).getByRole("button", { name: "Target chat" }),
);
await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
expect(mocks.routerPush).toHaveBeenCalledWith(
"/workspace/chats/thread-a",
);
},
);
it("renders shelf rows with the provenance badge for thread-sourced documents", () => {
mocks.documents = [
makeDocument({

View File

@ -1,6 +1,6 @@
import { afterEach, expect, test } from "@rstest/core";
import { cleanup, render } from "@testing-library/react";
import { StrictMode } from "react";
import { afterEach, expect, rs, test } from "@rstest/core";
import { act, cleanup, render, screen } from "@testing-library/react";
import { StrictMode, type Dispatch, type SetStateAction } from "react";
import {
stageProjectAttachment,
@ -24,12 +24,12 @@ const OTHER: AttachProjectDocumentResult = {
"/api/threads/thread-1/artifacts/mnt/user-data/uploads/notes.txt",
};
// Latest hook state, exposed to assertions without a DOM surface. The probe
// Latest hook state, exposed to assertions alongside a DOM surface. The probe
// keeps the setter reachable so tests can drive removal/clear like the
// composer's chip button and onSent callback do.
const observed: {
current: AttachProjectDocumentResult[];
set: (value: AttachProjectDocumentResult[]) => void;
set: Dispatch<SetStateAction<AttachProjectDocumentResult[]>>;
} = {
current: [],
set: () => {
@ -41,11 +41,16 @@ function ComposerProbe({ threadId }: { threadId: string }) {
const [attachments, setAttachments] = useStagedProjectAttachments(threadId);
observed.current = attachments;
observed.set = setAttachments;
return null;
return (
<output aria-label="Pending attachments">
{attachments.map((item) => item.filename).join(", ")}
</output>
);
}
afterEach(() => {
cleanup();
rs.restoreAllMocks();
observed.current = [];
observed.set = () => {
throw new Error("composer probe not mounted");
@ -165,3 +170,69 @@ test("clearing after a successful send drops the pending list for good", () => {
);
expect(observed.current).toEqual([]);
});
test("a composer that could not read pending attachments keeps its changes local", () => {
stageProjectAttachment("thread-1", OTHER);
const denied = rs
.spyOn(window.sessionStorage, "getItem")
.mockImplementation(() => {
throw new DOMException("Read denied", "SecurityError");
});
const first = render(
<StrictMode>
<ComposerProbe threadId="thread-1" />
</StrictMode>,
);
expect(screen.getByLabelText("Pending attachments").textContent).toBe("");
denied.mockRestore();
act(() => observed.set([ATTACHMENT]));
expect(screen.getByLabelText("Pending attachments").textContent).toBe(
"roadmap.md",
);
act(() => observed.set([]));
expect(screen.getByLabelText("Pending attachments").textContent).toBe("");
first.unmount();
render(<ComposerProbe threadId="thread-1" />);
expect(observed.current).toEqual([OTHER]);
});
test("quota failures still update visible attachments and the next functional updater", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
render(
<StrictMode>
<ComposerProbe threadId="thread-1" />
</StrictMode>,
);
rs.spyOn(window.sessionStorage, "setItem").mockImplementation(() => {
throw new DOMException("Storage full", "QuotaExceededError");
});
act(() => observed.set([OTHER]));
expect(screen.getByLabelText("Pending attachments").textContent).toBe(
"notes.txt",
);
act(() => observed.set((previous) => [...previous, ATTACHMENT]));
expect(screen.getByLabelText("Pending attachments").textContent).toBe(
"notes.txt, roadmap.md",
);
});
test("denied removal still clears visible attachments after a send", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
render(
<StrictMode>
<ComposerProbe threadId="thread-1" />
</StrictMode>,
);
rs.spyOn(window.sessionStorage, "removeItem").mockImplementation(() => {
throw new DOMException("Removal denied", "SecurityError");
});
act(() => observed.set([]));
expect(screen.getByLabelText("Pending attachments").textContent).toBe("");
expect(observed.current).toEqual([]);
});

View File

@ -1,7 +1,8 @@
import { afterEach, expect, test } from "@rstest/core";
import { afterEach, expect, rs, test } from "@rstest/core";
import {
readProjectAttachments,
retireProjectAttachments,
stageProjectAttachment,
} from "@/core/projects/composer-attach";
import type { AttachProjectDocumentResult } from "@/core/projects/types";
@ -23,6 +24,7 @@ const OTHER: AttachProjectDocumentResult = {
};
afterEach(() => {
rs.restoreAllMocks();
window.sessionStorage.clear();
});
@ -60,6 +62,117 @@ test("a corrupt staged payload is dropped, not thrown", () => {
).toBeNull();
});
test("unavailable storage access leaves staging, reading and retirement usable", () => {
rs.spyOn(window, "sessionStorage", "get").mockImplementation(() => {
throw new DOMException("Storage denied", "SecurityError");
});
expect(() => stageProjectAttachment("thread-1", ATTACHMENT)).not.toThrow();
expect(readProjectAttachments("thread-1")).toEqual([]);
expect(() =>
retireProjectAttachments("thread-1", [ATTACHMENT]),
).not.toThrow();
});
test("denied reads return no pending attachments without interrupting staging or retirement", () => {
rs.spyOn(window.sessionStorage, "getItem").mockImplementation(() => {
throw new DOMException("Read denied", "SecurityError");
});
expect(readProjectAttachments("thread-1")).toEqual([]);
expect(() => stageProjectAttachment("thread-1", ATTACHMENT)).not.toThrow();
expect(() =>
retireProjectAttachments("thread-1", [ATTACHMENT]),
).not.toThrow();
});
test("staging preserves pending attachments when their stored list cannot be read", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
rs.spyOn(window.sessionStorage, "getItem").mockImplementationOnce(() => {
throw new DOMException("Read denied", "SecurityError");
});
stageProjectAttachment("thread-1", OTHER);
expect(readProjectAttachments("thread-1")).toEqual([ATTACHMENT]);
});
test("retiring sent attachments preserves the list when it cannot be read", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
stageProjectAttachment("thread-1", OTHER);
rs.spyOn(window.sessionStorage, "getItem").mockImplementationOnce(() => {
throw new DOMException("Read denied", "SecurityError");
});
retireProjectAttachments("thread-1", [ATTACHMENT]);
expect(readProjectAttachments("thread-1")).toEqual([ATTACHMENT, OTHER]);
});
test("quota failures do not interrupt staging or retirement of a partial list", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
stageProjectAttachment("thread-1", OTHER);
rs.spyOn(window.sessionStorage, "setItem").mockImplementation(() => {
throw new DOMException("Storage full", "QuotaExceededError");
});
expect(() => stageProjectAttachment("thread-1", OTHER)).not.toThrow();
expect(() =>
retireProjectAttachments("thread-1", [ATTACHMENT]),
).not.toThrow();
// A failed persistence write leaves the old stored list untouched.
expect(readProjectAttachments("thread-1")).toEqual([ATTACHMENT, OTHER]);
});
test("a failed cleanup does not interrupt reading a corrupt payload", () => {
window.sessionStorage.setItem(
"deerflow.project-attachment.thread-1",
"{not json",
);
rs.spyOn(window.sessionStorage, "removeItem").mockImplementation(() => {
throw new DOMException("blocked", "SecurityError");
});
expect(() => readProjectAttachments("thread-1")).not.toThrow();
expect(readProjectAttachments("thread-1")).toEqual([]);
});
test("a failed storage removal does not interrupt retiring sent attachments", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
rs.spyOn(window.sessionStorage, "removeItem").mockImplementation(() => {
throw new DOMException("blocked", "SecurityError");
});
expect(() =>
retireProjectAttachments("thread-1", [ATTACHMENT]),
).not.toThrow();
});
test("a failed storage write does not interrupt retiring remaining attachments", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
stageProjectAttachment("thread-1", OTHER);
rs.spyOn(window.sessionStorage, "setItem").mockImplementation(() => {
throw new DOMException("full", "QuotaExceededError");
});
expect(() =>
retireProjectAttachments("thread-1", [ATTACHMENT]),
).not.toThrow();
});
test("retirement keeps later attachments and other threads' pending lists", () => {
stageProjectAttachment("thread-1", ATTACHMENT);
stageProjectAttachment("thread-1", OTHER);
stageProjectAttachment("thread-2", ATTACHMENT);
retireProjectAttachments("thread-1", [ATTACHMENT]);
expect(readProjectAttachments("thread-1")).toEqual([OTHER]);
expect(readProjectAttachments("thread-2")).toEqual([ATTACHMENT]);
retireProjectAttachments("thread-1", [OTHER]);
expect(readProjectAttachments("thread-1")).toEqual([]);
});
test("the pre-list single-object payload shape reads as a one-element list", () => {
window.sessionStorage.setItem(
"deerflow.project-attachment.thread-1",