refactor(sessions): persist archive state in SQLite (#6536)

* feat(webui): add archived chat management

* fix(webui): keep archived chats in the sidebar

* feat(webui): scope archived topics to sidebar groups

* fix(webui): reuse group dissolve label

* fix(webui): fail fast when adding panes

* refactor(sessions): persist archive state in SQLite

* refactor: limit archive changes to SQLite storage

* perf(sessions): avoid redundant archive I/O and read barriers

* fix(sessions): archive only persisted conversations
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-03 15:58:41 +08:00
1 parent 58a05ea3e0
commit 80ac41b1f4
16 files changed
+751 -107

No files matched your search

+7 -2
View File
@@ -2072,7 +2072,11 @@ function Shell({
const onArchiveMany = useCallback(
(keys: string[], archive: boolean) => {
const selected = new Set(keys);
const persistedKeys = new Set(sessions
.filter((session) => session.projectLocked !== false)
.map((session) => session.key));
const selected = new Set(keys.filter((key) => persistedKeys.has(key)));
if (selected.size === 0) return;
void updateSidebarState((current) => {
const archived = new Set(current.archived_keys);
for (const key of selected) {
@@ -2095,7 +2099,7 @@ function Shell({
});
}
},
[activeKey, navigate, sidebarState.archived_keys, topicSessions, updateSidebarState],
[activeKey, navigate, sessions, sidebarState.archived_keys, topicSessions, updateSidebarState],
);
const onToggleArchive = useCallback((key: string) => {
@@ -2652,6 +2656,7 @@ function Shell({
chatId: session.chatId,
title: titleForSession(session),
handle: session.handle,
projectLocked: session.projectLocked !== false,
}));
return [presentation.rowKey, {
tabKey: orderedTab.tabKey,
+38 -20
View File
@@ -182,6 +182,7 @@ export interface SidebarPaneGroup {
chatId: string;
title: string;
handle?: ChatSummary["handle"];
projectLocked?: ChatSummary["projectLocked"];
}>;
}
@@ -407,6 +408,18 @@ export const ChatList = memo(function ChatList({
}
return items;
}, [paneGroups, sessions, t, titleOverrides]);
const archivableKeys = useMemo(() => {
const keys = new Set(sessions
.filter((session) => session.projectLocked !== false)
.map((session) => session.key));
for (const group of Object.values(paneGroups)) {
for (const pane of group.panes) {
if (pane.projectLocked === true) keys.add(pane.key);
else keys.delete(pane.key);
}
}
return keys;
}, [paneGroups, sessions]);
const paneGroupTargets = useMemo(() => Array.from(new Map(
Object.values(paneGroups)
.filter((group) => group.visible ?? group.panes.length > 1)
@@ -699,8 +712,9 @@ export const ChatList = memo(function ChatList({
if (confirmed) closeDeleteSelection();
});
};
const allSelectedArchived = selectedDeleteKeys.size > 0
&& Array.from(selectedDeleteKeys).every((key) => archivedPanes.has(key) || archived.has(key));
const selectedArchiveKeys = Array.from(selectedDeleteKeys).filter((key) => archivableKeys.has(key));
const allSelectedArchived = selectedArchiveKeys.length > 0
&& selectedArchiveKeys.every((key) => archivedPanes.has(key) || archived.has(key));
return (
<div ref={scrollViewportRef} className="sidebar-scroll-fade sidebar-scrollbar h-full min-h-0 min-w-0 overflow-x-hidden overflow-y-auto overscroll-contain pb-2">
<SidebarSelectionHighlight
@@ -1116,14 +1130,16 @@ export const ChatList = memo(function ChatList({
<Pencil className="h-4 w-4 shrink-0" />
{t("chat.rename")}
</DropdownMenuItem>
<DropdownMenuItem onSelect={() => onToggleArchive(s.key)}>
{isArchived ? (
<ArchiveRestore className="h-4 w-4 shrink-0" />
) : (
<Archive className="h-4 w-4 shrink-0" />
)}
{isArchived ? t("chat.unarchive") : t("chat.archive")}
</DropdownMenuItem>
{s.projectLocked !== false ? (
<DropdownMenuItem onSelect={() => onToggleArchive(s.key)}>
{isArchived ? (
<ArchiveRestore className="h-4 w-4 shrink-0" />
) : (
<Archive className="h-4 w-4 shrink-0" />
)}
{isArchived ? t("chat.unarchive") : t("chat.archive")}
</DropdownMenuItem>
) : null}
{paneGroup && onCreateTab ? (
<DropdownMenuItem onSelect={() => onCreateTab(s.key)}>
<PanelsTopLeft className="h-4 w-4 shrink-0" aria-hidden />
@@ -1202,14 +1218,14 @@ export const ChatList = memo(function ChatList({
count: selectedDeleteKeys.size,
})}
</span>
{onArchiveMany ? (
{onArchiveMany && (selectedDeleteKeys.size === 0 || selectedArchiveKeys.length > 0) ? (
<HoverHint content={t(allSelectedArchived ? "chat.unarchive" : "chat.archive")}>
<button
type="button"
aria-label={t(allSelectedArchived ? "chat.unarchive" : "chat.archive")}
disabled={selectedDeleteKeys.size === 0 || deleteConfirmationPending}
onClick={() => setPendingArchive({
keys: Array.from(selectedDeleteKeys),
keys: selectedArchiveKeys,
archive: !allSelectedArchived,
})}
className="touch-target inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-foreground transition-colors media-hover:hover:bg-sidebar-accent disabled:pointer-events-none disabled:opacity-40"
@@ -1589,14 +1605,16 @@ function ActivePaneRows({
<Pencil className="h-4 w-4 shrink-0" />
{t("chat.rename")}
</DropdownMenuItem>
<DropdownMenuItem onSelect={() => onToggleArchive(pane.key)}>
{isArchived ? (
<ArchiveRestore className="h-4 w-4 shrink-0" />
) : (
<Archive className="h-4 w-4 shrink-0" />
)}
{isArchived ? t("chat.unarchive") : t("chat.archive")}
</DropdownMenuItem>
{pane.projectLocked === true ? (
<DropdownMenuItem onSelect={() => onToggleArchive(pane.key)}>
{isArchived ? (
<ArchiveRestore className="h-4 w-4 shrink-0" />
) : (
<Archive className="h-4 w-4 shrink-0" />
)}
{isArchived ? t("chat.unarchive") : t("chat.archive")}
</DropdownMenuItem>
) : null}
{onDetachPane ? (
<DropdownMenuItem onSelect={() => onDetachPane(group.tabKey, pane.key)}>
<Unplug className="h-4 w-4 shrink-0" />
+11 -6
View File
@@ -2374,11 +2374,12 @@ describe("App layout", () => {
expect(document.body.style.pointerEvents).not.toBe("none");
}, 15_000);
it("archives selected topics in one sidebar update and navigates outside the full selection", async () => {
it.each([false, true])("archives only persisted selected topics in one sidebar update (draft=%s)", async (includeDraft) => {
mockSessions = ["a", "b", "c"].map((id) => ({
key: `websocket:chat-${id}`, channel: "websocket", chatId: `chat-${id}`,
createdAt: "2026-04-16T10:00:00Z", updatedAt: "2026-04-16T10:00:00Z",
title: `Topic ${id}`, preview: `Message ${id}`,
title: `Topic ${id}`, preview: "",
projectLocked: includeDraft && id === "a" ? false : undefined,
}));
mockFetchRoutes({
"/api/webui/sidebar-state": { pinned_keys: ["websocket:chat-a", "websocket:chat-b"] },
@@ -2393,17 +2394,21 @@ describe("App layout", () => {
fireEvent.click(await screen.findByRole("menuitem", { name: "Select" }));
fireEvent.click(within(sidebar).getByRole("button", { name: "Topic b" }));
fireEvent.click(within(sidebar).getByRole("button", { name: "Archive" }));
const dialog = await screen.findByRole("alertdialog", { name: "Archive 2 topics?" });
const dialog = await screen.findByRole("alertdialog", {
name: includeDraft ? "Archive this topic?" : "Archive 2 topics?",
});
expect(setSidebarStateSpy).not.toHaveBeenCalled();
fireEvent.click(within(dialog).getByRole("button", { name: "Archive" }));
await waitFor(() => expect(setSidebarStateSpy).toHaveBeenCalledTimes(1));
expect(setSidebarStateSpy).toHaveBeenCalledWith(expect.objectContaining({
archived_keys: ["websocket:chat-a", "websocket:chat-b"], pinned_keys: [],
archived_keys: includeDraft ? ["websocket:chat-b"] : ["websocket:chat-a", "websocket:chat-b"],
pinned_keys: includeDraft ? ["websocket:chat-a"] : [],
}));
expect(window.location.hash).toBe("#/chat/websocket%3Achat-c");
expect(window.location.hash).toBe(includeDraft ? "#/chat/websocket%3Achat-a" : "#/chat/websocket%3Achat-c");
expect(within(sidebar).getByRole("button", { name: "Topic c" })).toBeInTheDocument();
expect(within(sidebar).queryByRole("button", { name: "Topic a" })).not.toBeInTheDocument();
if (includeDraft) expect(within(sidebar).getByRole("button", { name: "Topic a" })).toBeInTheDocument();
else expect(within(sidebar).queryByRole("button", { name: "Topic a" })).not.toBeInTheDocument();
expect(within(sidebar).queryByRole("button", { name: "Topic b" })).not.toBeInTheDocument();
expect(deleteChatSpy).not.toHaveBeenCalled();
});
+71 -2
View File
@@ -67,6 +67,75 @@ describe("ChatList", () => {
expect(onTogglePin).toHaveBeenCalledWith("websocket:review");
});
it.each([false, true, undefined])("uses persistence, not preview, for topic archive actions (%s)", async (projectLocked) => {
const onToggleArchive = vi.fn();
render(<ChatList
sessions={[session({ chatId: "empty", title: "Empty topic", projectLocked })]}
activeKey={null}
onSelect={vi.fn()}
onRequestDelete={vi.fn()}
onTogglePin={vi.fn()}
onRequestRename={vi.fn()}
onToggleArchive={onToggleArchive}
/>);
fireEvent.contextMenu(screen.getByRole("button", { name: "Empty topic" }).closest("[data-chat-row]")!);
await screen.findByRole("menuitem", { name: "Delete" });
if (projectLocked === false) {
expect(screen.queryByRole("menuitem", { name: "Archive" })).not.toBeInTheDocument();
} else {
fireEvent.click(screen.getByRole("menuitem", { name: "Archive" }));
expect(onToggleArchive).toHaveBeenCalledWith("websocket:empty");
}
});
it.each([
{ archivedPaneKeys: [], projectLocked: false },
{ archivedPaneKeys: ["websocket:saved"], projectLocked: false },
{ archivedPaneKeys: [], projectLocked: undefined },
])("archives only persisted panes in a mixed selection ($archivedPaneKeys, draft=$projectLocked)", async ({ archivedPaneKeys, projectLocked }) => {
const onArchiveMany = vi.fn();
const onRequestDeleteMany = vi.fn();
render(<ChatList
sessions={[session({ chatId: "draft", title: "Draft", projectLocked: false })]}
paneGroups={{
"websocket:draft": {
tabKey: "tab", title: "Mixed group", activePaneKey: "websocket:draft",
panes: [
{ key: "websocket:draft", chatId: "draft", title: "Draft", projectLocked },
{ key: "websocket:saved", chatId: "saved", title: "Empty fork", projectLocked: true },
],
},
}}
archivedPaneKeys={archivedPaneKeys}
activeKey="websocket:draft"
onSelect={vi.fn()}
onRequestDelete={vi.fn()}
onRequestDeleteMany={onRequestDeleteMany}
onTogglePin={vi.fn()}
onRequestRename={vi.fn()}
onToggleArchive={vi.fn()}
onArchiveMany={onArchiveMany}
/>);
fireEvent.pointerDown(screen.getByRole("button", { name: "Draft pane actions" }), { button: 0 });
await screen.findByRole("menuitem", { name: "Delete" });
expect(screen.queryByRole("menuitem", { name: "Archive" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("menuitem", { name: "Select" }));
const bar = screen.getByTestId("delete-selection-bar");
expect(within(bar).queryByRole("button", { name: "Archive" })).not.toBeInTheDocument();
fireEvent.click(within(bar).getByRole("button", { name: "Delete" }));
expect(onRequestDeleteMany).toHaveBeenCalledWith([
{ key: "websocket:draft", label: "Draft" },
], expect.any(Function));
act(() => onRequestDeleteMany.mock.calls[0][1](false));
fireEvent.click(screen.getByRole("button", { name: "Empty fork" }));
const action = archivedPaneKeys.length ? "Unarchive" : "Archive";
fireEvent.click(within(bar).getByRole("button", { name: action }));
const dialog = screen.getByRole("alertdialog");
expect(dialog).toHaveAccessibleName(`${action} this topic?`);
fireEvent.click(within(dialog).getByRole("button", { name: action }));
expect(onArchiveMany).toHaveBeenCalledWith(["websocket:saved"], !archivedPaneKeys.length);
});
it("restores the colored handle underline and animated active track", () => {
render(
<ChatList
@@ -827,8 +896,8 @@ describe("ChatList", () => {
title: "Root topic",
activePaneKey: "websocket:root",
panes: [
{ key: "websocket:root", chatId: "root", title: "Root topic" },
{ key: "websocket:child", chatId: "child", title: "Research pane" },
{ key: "websocket:root", chatId: "root", title: "Root topic", projectLocked: true },
{ key: "websocket:child", chatId: "child", title: "Research pane", projectLocked: true },
],
},
}}