Skip to content

Commit 9458d8b

Browse files
authored
fix(ui): remove agent-archived sessions from the sidebar (#131461)
* fix(ui): remove agent-archived sessions from the sidebar * refactor(ui): own archive confirmation and visibility together
1 parent bc9433b commit 9458d8b

10 files changed

Lines changed: 231 additions & 123 deletions

‎ui/src/components/session-organizer-operations.runtime.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ export async function archiveSessionWithUndo(
162162
session: SessionActionRow,
163163
scope: SidebarSessionMutationScope,
164164
) {
165-
scope.sessions.setArchiveVisibility(session.key, "pending");
165+
scope.sessions.setArchivePending(session.key, true);
166166
const result = await patchSession(host, session, { archived: true }, scope);
167-
scope.sessions.setArchiveVisibility(session.key, result === "completed" ? "archived" : undefined);
167+
scope.sessions.setArchivePending(session.key, false);
168168
if (result !== "completed" || !host.sessionData.isSessionMutationScopeCurrent(scope)) {
169169
return;
170170
}

‎ui/src/e2e/session-management.archive.e2e.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,82 @@ async function confirmDelete(page: import("playwright").Page, proofName?: string
2828
}
2929

3030
suite.define(() => {
31+
it("removes an agent-archived selected session without closing its transcript", async () => {
32+
const context = await suite.browser.newContext({
33+
locale: "en-US",
34+
serviceWorkers: "block",
35+
viewport: { height: 900, width: 1280 },
36+
...(captureUiProofEnabled ? { recordVideo: { dir: uiProofArtifactDir } } : {}),
37+
});
38+
const page = await context.newPage();
39+
const main = sessionRow("agent:main:main", "Main", 1);
40+
const target = sessionRow("agent:main:archive-from-agent", "Archive from agent", 2);
41+
const gateway = await installMockGateway(page, {
42+
historyMessages: [
43+
{ role: "assistant", content: [{ type: "text", text: "Work completed." }] },
44+
],
45+
methodResponses: { "sessions.list": sessionsListResponse([main, target]) },
46+
sessionArchiveFiltering: true,
47+
sessionKey: main.key,
48+
});
49+
50+
try {
51+
await page.goto(controlUiSessionUrl(suite.server.baseUrl, target.key));
52+
const row = page.locator(`.sidebar-recent-session[data-session-key="${target.key}"]`);
53+
const pane = page.locator("openclaw-chat-pane.chat-pane-cache__pane--active");
54+
const notice = pane.locator(".agent-chat__disabled-banner");
55+
await row.waitFor({ state: "visible" });
56+
await pane.getByText("Work completed.", { exact: true }).waitFor();
57+
await captureUiProof(page, "agent-archive-before.png");
58+
59+
// The agent's deferred self-archive reaches clients as a committed patch,
60+
// without running the sidebar menu's optimistic mutation path.
61+
const archived = { ...target, archived: true, archivedAt: 3, updatedAt: 3 };
62+
await gateway.setMethodResponse("sessions.list", sessionsListResponse([main, archived]));
63+
await gateway.emitGatewayEvent("sessions.changed", {
64+
...archived,
65+
agentId: "main",
66+
sessionKey: target.key,
67+
reason: "patch",
68+
});
69+
await notice.waitFor({ state: "visible" });
70+
await captureUiProof(page, "agent-archive-received.png");
71+
await row.waitFor({ state: "detached", timeout: 10_000 });
72+
expect(new URL(page.url()).pathname).toBe(controlUiSessionPath(target.key));
73+
await pane.getByText("Work completed.", { exact: true }).waitFor();
74+
expect(await gateway.getRequests("sessions.patch")).toEqual([]);
75+
await captureUiProof(page, "agent-archive-after.png");
76+
77+
await page.getByRole("button", { name: "Filter & sort" }).click();
78+
await page
79+
.locator(".sidebar-session-sort-menu")
80+
.getByRole("menuitemradio", { name: "Archived" })
81+
.click();
82+
await row.waitFor({ state: "visible" });
83+
await page.getByRole("button", { name: "Filter & sort" }).click();
84+
await page
85+
.locator(".sidebar-session-sort-menu")
86+
.getByRole("menuitemradio", { name: "Active", exact: true })
87+
.click();
88+
await row.waitFor({ state: "detached" });
89+
90+
await gateway.setMethodResponse("sessions.list", sessionsListResponse([main, target]));
91+
await gateway.emitGatewayEvent("sessions.changed", {
92+
...target,
93+
agentId: "main",
94+
sessionKey: target.key,
95+
reason: "patch",
96+
archived: false,
97+
archivedAt: null,
98+
updatedAt: 4,
99+
});
100+
await row.waitFor({ state: "visible" });
101+
await notice.waitFor({ state: "detached" });
102+
} finally {
103+
await context.close();
104+
}
105+
});
106+
31107
it("refreshes the archived sidebar after restoring a session during a stale roster load", async () => {
32108
const context = await suite.browser.newContext({
33109
locale: "en-US",

‎ui/src/lib/sessions/index.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ function sessionChangedEvent(key: string): GatewayEventFrame {
2828
}
2929

3030
describe("createSessionCapability", () => {
31+
it.each(["direct", "subscription"] as const)(
32+
"shares confirmed archive visibility after %s reconciliation",
33+
async (path) => {
34+
const key = "agent:main:archive-from-agent";
35+
const row = { key, kind: "direct" as const, sessionId: "archive-session", updatedAt: 1 };
36+
const request = vi.fn(async () => sessionsResult([row], 1));
37+
const { emitEvent, gateway } = createGatewayHarness({
38+
request,
39+
} as unknown as GatewayBrowserClient);
40+
const sessions = createSessionCapability(gateway);
41+
const reconcile = (archived: boolean, updatedAt: number) => {
42+
const payload = { ...row, sessionKey: key, reason: "patch", archived, updatedAt };
43+
if (path === "direct") {
44+
sessions.reconcileChanged(payload);
45+
} else {
46+
emitEvent({ type: "event", event: "sessions.changed", payload });
47+
}
48+
};
49+
try {
50+
await sessions.refresh({ agentId: "main", force: true });
51+
reconcile(true, 2);
52+
expect(sessions.archiveVisibility(key)).toBe("archived");
53+
await sessions.refresh({ agentId: "main", force: true });
54+
expect(sessions.archiveVisibility(key)).toBe("archived");
55+
reconcile(false, 3);
56+
expect(sessions.archiveVisibility(key)).toBeUndefined();
57+
} finally {
58+
sessions.dispose();
59+
}
60+
},
61+
);
62+
3163
it.each(["direct", "subscription"] as const)(
3264
"ignores stale archive state after a newer unarchive via %s reconciliation",
3365
async (path) => {

‎ui/src/lib/sessions/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ export function createSessionCapability(gateway: SessionGateway): SessionCapabil
606606
recover: operations.recover,
607607
patch: mutations.patch,
608608
archiveVisibility: mutations.archiveVisibility,
609-
setArchiveVisibility: mutations.setArchiveVisibility,
609+
setArchivePending: mutations.setArchivePending,
610610
assignOwner: mutations.assignOwner,
611611
retireModelOverride: mutations.retireModelOverride,
612612
think: (key, agentId) => thinkingLevelClaims.get(thinkingClaimKey(key, agentId))?.[0],

‎ui/src/lib/sessions/list-options.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,14 @@ describe("session list replacement options", () => {
362362
archived: false,
363363
});
364364
expect(sessions.state.result?.sessions[0]?.sessionId).toBeUndefined();
365+
expect(sessions.archiveVisibility(key)).toBeUndefined();
365366

366367
await sessions.refresh({ agentId: "main", force: true });
367368
expect(sessions.state.result?.sessions[0]).toMatchObject({
368369
sessionId: "replacement-session",
369370
archived: false,
370371
});
372+
expect(sessions.archiveVisibility(key)).toBeUndefined();
371373

372374
await sessions.refresh({ agentId: "main", force: true });
373375
expect(sessions.state.result?.sessions[0]?.archived).toBe(false);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import type { GatewaySessionRow, SessionsListResult } from "../../api/types.ts";
2+
import type { SessionArchiveVisibility } from "./session-capability.ts";
3+
4+
type ConfirmedArchiveState = Pick<GatewaySessionRow, "archivedAt" | "archivedBy" | "sessionId">;
5+
6+
export function createSessionArchiveState(
7+
publishedRow: (key: string) => GatewaySessionRow | undefined,
8+
onChange: () => void,
9+
) {
10+
const confirmed = new Map<string, ConfirmedArchiveState>();
11+
const pending = new Set<string>();
12+
const clear = (key: string) => {
13+
confirmed.delete(key.trim());
14+
pending.delete(key.trim());
15+
};
16+
return {
17+
clear,
18+
clearAll: () => {
19+
confirmed.clear();
20+
pending.clear();
21+
},
22+
observe: (key: string, archived: boolean | null, row?: GatewaySessionRow): void => {
23+
const normalizedKey = key.trim();
24+
if (!normalizedKey || archived === null) {
25+
return;
26+
}
27+
if (!archived) {
28+
clear(normalizedKey);
29+
return;
30+
}
31+
const previous = confirmed.get(normalizedKey);
32+
confirmed.set(normalizedKey, {
33+
archivedAt: row?.archivedAt ?? previous?.archivedAt,
34+
archivedBy: row?.archivedBy ?? previous?.archivedBy,
35+
sessionId: row?.sessionId || previous?.sessionId,
36+
});
37+
},
38+
apply: (result: SessionsListResult | null): SessionsListResult | null => {
39+
if (!result || confirmed.size === 0) {
40+
return result;
41+
}
42+
let changed = false;
43+
const sessions = result.sessions.map((row) => {
44+
const archive = confirmed.get(row.key);
45+
if (!archive) {
46+
return row;
47+
}
48+
if (archive.sessionId && archive.sessionId !== row.sessionId) {
49+
// An id-less row may be a same-key replacement whose identity has not arrived.
50+
// Do not transfer archive state; retire it only after a different identity appears.
51+
if (row.sessionId) {
52+
confirmed.delete(row.key);
53+
}
54+
return row;
55+
}
56+
if (row.archived === true) {
57+
return row;
58+
}
59+
changed = true;
60+
return {
61+
...row,
62+
archived: true,
63+
...(archive.archivedAt !== undefined ? { archivedAt: archive.archivedAt } : {}),
64+
...(archive.archivedBy ? { archivedBy: archive.archivedBy } : {}),
65+
};
66+
});
67+
return changed ? { ...result, sessions } : result;
68+
},
69+
visibility: (key: string): SessionArchiveVisibility | undefined => {
70+
const normalizedKey = key.trim();
71+
if (pending.has(normalizedKey)) {
72+
return "pending";
73+
}
74+
const archive = confirmed.get(normalizedKey);
75+
if (!archive) {
76+
return undefined;
77+
}
78+
const row = publishedRow(normalizedKey);
79+
// Share the archive confirmation with event-driven actions, but never
80+
// hide a same-key replacement whose durable identity does not match.
81+
return archive.sessionId && row && archive.sessionId !== row.sessionId
82+
? undefined
83+
: "archived";
84+
},
85+
setPending: (key: string, active: boolean) => {
86+
const normalizedKey = key.trim();
87+
if (!normalizedKey || pending.has(normalizedKey) === active) {
88+
return;
89+
}
90+
if (active) {
91+
pending.add(normalizedKey);
92+
} else {
93+
pending.delete(normalizedKey);
94+
}
95+
onChange();
96+
},
97+
};
98+
}

‎ui/src/lib/sessions/session-archive-visibility.ts‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎ui/src/lib/sessions/session-capability.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export type SessionCapability = {
179179
recover: (params: { key: string; agentId?: string }) => Promise<SessionsRecoverResult | null>;
180180
patch: SessionPatchRoute;
181181
archiveVisibility: (key: string) => SessionArchiveVisibility | undefined;
182-
setArchiveVisibility: (key: string, visibility: SessionArchiveVisibility | undefined) => void;
182+
setArchivePending: (key: string, pending: boolean) => void;
183183
assignOwner: (
184184
key: string,
185185
owner: SessionsAssignOwnerParams["owner"],

0 commit comments

Comments
 (0)