Skip to content

Commit 5298bea

Browse files
fix(ui): abort chat-header confirms too and surface a retry notice
The sidebar epoch-abort wiring left the chat-header delete/fork/archive path uncovered: it builds its own SidebarSessionMutationScope from connectionGeneration but never handed it a signal, so a header confirm left open across a reconnect kept the shared confirmation lock forever. Give ChatPaneBase its own AbortController, retire it everywhere connectionGeneration advances (reconnect and pane teardown), and wire the signal into the header's captured scope, matching SessionDataController's controller for the sidebar. Separately, resolving an aborted confirm to `false` looked identical to an ordinary cancel, so the operator's lost intent vanished silently. Reorder every destructive confirm site to check scope staleness ahead of the `confirmed` guard and show the existing "Gateway connection replaced ... Try again." toast on abort, matching the pattern deleteSessionGroup already used for the same race. This also fixes a regression the PR had introduced in a pre-existing main test (app-sidebar.test.ts's "keeps a reconnected group delete retryable after its confirm opened"), which is what failed checks-node-compact-large-22 and checks-ui on the previous head.
1 parent ac82b81 commit 5298bea

9 files changed

Lines changed: 131 additions & 6 deletions

‎ui/src/components/session-organizer-batch-mutations.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import type {
88
import { GatewayRequestError, type GatewayBrowserClient } from "../api/gateway.ts";
99
import type { ApplicationGatewaySnapshot } from "../app/gateway.ts";
1010
import { loadSettings, patchSettings } from "../app/settings.ts";
11+
import { t } from "../i18n/index.ts";
1112
import type { SessionCapability } from "../lib/sessions/index.ts";
13+
import { showToast } from "../lib/toast.ts";
1214
import {
1315
answerConfirmDialog,
1416
installDialogPolyfill,
@@ -27,6 +29,8 @@ import {
2729
stopCloudWorker,
2830
} from "./session-organizer-operations.runtime.ts";
2931

32+
vi.mock("../lib/toast.ts", () => ({ showToast: vi.fn() }));
33+
3034
function sessionRow(index: number): SidebarRecentSession {
3135
return {
3236
key: `agent:main:batch-${index}`,
@@ -428,22 +432,26 @@ const destructiveOperations = [
428432
run: (harness: OperationsHarness) =>
429433
deleteSessionsBatch(harness.host, [sessionRow(0), sessionRow(1)], harness.scope),
430434
mutation: (harness: OperationsHarness) => harness.deleteMany,
435+
staleMessage: t("sessionsView.deleteSessionsStale", { count: "2" }),
431436
},
432437
{
433438
name: "session delete",
434439
run: (harness: OperationsHarness) => deleteSession(harness.host, sessionRow(0), harness.scope),
435440
mutation: (harness: OperationsHarness) => harness.deleteOne,
441+
staleMessage: t("sessionsView.deleteSessionStale", { session: sessionRow(0).label }),
436442
},
437443
{
438444
name: "cloud worker stop",
439445
run: (harness: OperationsHarness) =>
440446
stopCloudWorker(harness.host, cloudWorkerRow(false), harness.scope),
441447
mutation: (harness: OperationsHarness) => harness.request,
448+
staleMessage: t("sessionsView.stopCloudWorkerStale", { session: cloudWorkerRow(false).label }),
442449
},
443450
{
444451
name: "session-group delete",
445452
run: (harness: OperationsHarness) => deleteSessionGroup(harness.host, "Group A", harness.scope),
446453
mutation: (harness: OperationsHarness) => harness.groupsDelete,
454+
staleMessage: t("sessionsView.deleteGroupStale", { group: "Group A" }),
447455
},
448456
] as const;
449457

@@ -452,6 +460,7 @@ describe("session organizer destructive confirmations", () => {
452460

453461
beforeEach(() => {
454462
restoreDialogPolyfill = installDialogPolyfill();
463+
vi.mocked(showToast).mockClear();
455464
});
456465

457466
afterEach(() => {
@@ -487,6 +496,9 @@ describe("session organizer destructive confirmations", () => {
487496

488497
expect(operation.mutation(harness)).not.toHaveBeenCalled();
489498
expect(harness.publishSessionMutationError).not.toHaveBeenCalled();
499+
// An ordinary cancel is expected UX and needs no announcement; only a
500+
// reconnect-driven abort earns the retry notice below.
501+
expect(showToast).not.toHaveBeenCalled();
490502
});
491503

492504
it.each(destructiveOperations)(
@@ -502,6 +514,10 @@ describe("session organizer destructive confirmations", () => {
502514
expect(operation.mutation(harness)).not.toHaveBeenCalled();
503515
// The stale dialog must dismiss itself, not merely stop sending its request.
504516
expect(document.body.querySelector("openclaw-modal-dialog")).toBeNull();
517+
// The abort resolves the dialog to `false`, same as a user cancel, so the
518+
// operator needs a distinct, visible outcome or their lost intent reads
519+
// as a click that simply did nothing.
520+
expect(showToast).toHaveBeenCalledWith({ message: operation.staleMessage });
505521

506522
// A fresh confirmation (the reconnect's own retry) must be able to open
507523
// immediately; a stale dialog holding the shared lock would block it.
@@ -535,6 +551,11 @@ describe("session organizer destructive confirmations", () => {
535551

536552
expect(harness.request).not.toHaveBeenCalled();
537553
expect(harness.replaceCurrentSession).not.toHaveBeenCalled();
554+
// The session delete already landed; the worktree just stays put, same as
555+
// the no-access branch, so the operator learns where it went.
556+
expect(showToast).toHaveBeenCalledWith({
557+
message: t("sessionsView.deletePreservedWorktrees", { count: "1", branches: "feature" }),
558+
});
538559
});
539560

540561
it("skips the delete confirm entirely once the operator opted out", async () => {

‎ui/src/components/session-organizer-catalog.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,16 @@ export async function deleteSessionGroup(
102102
danger: true,
103103
signal: scope.signal,
104104
});
105-
if (!confirmed) {
106-
return false;
107-
}
105+
// Checked ahead of `confirmed`: a retired scope aborts the dialog to `false`
106+
// too, so without this order the operator's lost intent would look like an
107+
// ordinary cancel instead of the reconnect that actually dropped it.
108108
if (!host.sessionData.isSessionMutationScopeCurrent(scope)) {
109109
showToast({ message: t("sessionsView.deleteGroupStale", { group }) });
110110
return false;
111111
}
112+
if (!confirmed) {
113+
return false;
114+
}
112115
try {
113116
const outcome = await scope.sessions.groupsDelete(group);
114117
return outcome === "completed" && host.sessionData.isSessionMutationScopeCurrent(scope);

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,14 @@ export async function deleteSessionsBatch(
280280
});
281281
// A reconnect or a replaced sessions capability can land while the modal is
282282
// open, so the captured scope is revalidated before any delete leaves here.
283-
if (!confirmed || !host.sessionData.isSessionMutationScopeCurrent(scope)) {
283+
// Checked ahead of `confirmed`: a retired scope aborts the dialog to `false`
284+
// too, so without this order the operator's lost intent would look like an
285+
// ordinary cancel instead of the reconnect that actually dropped it.
286+
if (!host.sessionData.isSessionMutationScopeCurrent(scope)) {
287+
showToast({ message: t("sessionsView.deleteSessionsStale", { count: String(rows.length) }) });
288+
return;
289+
}
290+
if (!confirmed) {
284291
return;
285292
}
286293
const requests = rows.map((row) => ({
@@ -506,7 +513,14 @@ export async function stopCloudWorker(
506513
danger: true,
507514
signal: scope.signal,
508515
});
509-
if (!confirmed || !host.sessionData.isSessionMutationScopeCurrent(scope)) {
516+
// Checked ahead of `confirmed`: a retired scope aborts the dialog to `false`
517+
// too, so without this order the operator's lost intent would look like an
518+
// ordinary cancel instead of the reconnect that actually dropped it.
519+
if (!host.sessionData.isSessionMutationScopeCurrent(scope)) {
520+
showToast({ message: t("sessionsView.stopCloudWorkerStale", { session: session.label }) });
521+
return;
522+
}
523+
if (!confirmed) {
510524
return;
511525
}
512526
if (!requireSessionMutationAccess(host, scope, stopAction)) {
@@ -550,7 +564,14 @@ export async function deleteSession(
550564
...(options.offerSkip ? { skipPreference: sessionDeleteSkipPreference(scope) } : {}),
551565
signal: scope.signal,
552566
});
553-
if (!confirmed || !host.sessionData.isSessionMutationScopeCurrent(scope)) {
567+
// Checked ahead of `confirmed`: a retired scope aborts the dialog to `false`
568+
// too, so without this order the operator's lost intent would look like an
569+
// ordinary cancel instead of the reconnect that actually dropped it.
570+
if (!host.sessionData.isSessionMutationScopeCurrent(scope)) {
571+
showToast({ message: t("sessionsView.deleteSessionStale", { session: session.label }) });
572+
return;
573+
}
574+
if (!confirmed) {
554575
return;
555576
}
556577
const agentId = parseAgentSessionKey(session.key)?.agentId ?? scope.selectedAgentId;
@@ -604,7 +625,15 @@ export async function deleteSession(
604625
});
605626
// Cancel needs this guard too: the delete already landed, so a scope
606627
// retired while the modal was open must not drive the navigation below.
628+
// A retired scope leaves the worktree exactly like the no-access branch
629+
// above, so it earns the same visible outcome instead of vanishing quietly.
607630
if (!host.sessionData.isSessionMutationScopeCurrent(scope)) {
631+
showToast({
632+
message: t("sessionsView.deletePreservedWorktrees", {
633+
count: "1",
634+
branches: preserved.branch,
635+
}),
636+
});
608637
return;
609638
}
610639
if (removeWorktree) {

‎ui/src/i18n/locales/en.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,11 +997,16 @@ export const en: TranslationMap = {
997997
stopCloudWorker: "Stop cloud worker…",
998998
stopCloudWorkerConfirm: 'Stop the cloud worker for "{session}"?',
999999
stopCloudWorkerConfirmAction: "Stop worker",
1000+
stopCloudWorkerStale:
1001+
'Gateway connection replaced before the cloud worker for "{session}" was stopped. Try again.',
10001002
cloudWorkerStopResult: 'Cloud worker for "{session}" is {state}.',
10011003
deleteSessionMenu: "Delete…",
10021004
deleteSessionCount: "Delete {count}…",
10031005
deleteSessionConfirm: 'Delete "{session}" and its transcript?',
1006+
deleteSessionStale: 'Gateway connection replaced before "{session}" was deleted. Try again.',
10041007
deleteSessionsConfirm: "Delete {count} sessions and their transcripts?",
1008+
deleteSessionsStale:
1009+
"Gateway connection replaced before {count} sessions were deleted. Try again.",
10051010
deleteSelectedConfirmOne:
10061011
"Delete 1 session?\n\nThis will delete the session entry and archive its transcript.",
10071012
deleteSelectedConfirm:

‎ui/src/pages/chat/chat-pane-base.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ export abstract class ChatPaneBase extends OpenClawLightDomElement {
119119
this.activeChanged(value);
120120
}
121121
protected activeChanged(_active: boolean): void {}
122+
// Call wherever connectionGeneration itself advances (reconnect, capability
123+
// replacement, pane teardown) so a header confirm dialog open across that
124+
// boundary dismisses itself instead of confirming into a retired scope.
125+
protected retireHeaderSessionMutations(): void {
126+
this.headerSessionMutationAbortController.abort();
127+
this.headerSessionMutationAbortController = new AbortController();
128+
}
122129
@property({ attribute: false }) draft?: string;
123130
@property({ attribute: false }) focusComposer = false;
124131
@property({ attribute: false }) routeFace: BoardFace = "chat";
@@ -171,6 +178,11 @@ export abstract class ChatPaneBase extends OpenClawLightDomElement {
171178
protected boardProviderLease: (BoardProviderLease & { sessionKey: string }) | undefined;
172179
protected boardProviderLifecycleConnected = false;
173180
protected connectionGeneration = 0;
181+
// Owns the abort signal handed to header-scoped destructive confirm dialogs.
182+
// Retiring it alongside every connectionGeneration bump lets a dialog open
183+
// across a reconnect or pane teardown dismiss itself, matching
184+
// SessionDataController's own epoch-scoped controller for the sidebar.
185+
protected headerSessionMutationAbortController = new AbortController();
174186
@litState() protected headerEditing = false;
175187
@litState() protected headerRenameValue = "";
176188
@litState() protected headerPlatform: string | null = null;

‎ui/src/pages/chat/chat-pane-context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export abstract class ChatPaneContext extends ChatPaneLifecycle {
224224
// A reconnect can retain the browser client. Keep async ownership tied
225225
// to the logical connection, not only the transport object identity.
226226
this.connectionGeneration += 1;
227+
this.retireHeaderSessionMutations();
227228
invalidateChatAvatarCache(state);
228229
invalidateAssistantIdentityCache(state.client);
229230
state.assistantIdentityRequestVersion += 1;

‎ui/src/pages/chat/chat-pane-lifecycle.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ export abstract class ChatPaneLifecycle extends ChatPaneSessionCreation {
614614
this.paneResizeObserver?.disconnect();
615615
this.paneResizeObserver = null;
616616
this.connectionGeneration += 1;
617+
this.retireHeaderSessionMutations();
617618
this.deferredSessionHydrationRequestVersion += 1;
618619
this.sessionDiscussionPanels.clear();
619620
this.taskSuggestionsRequestVersion += 1;

‎ui/src/pages/chat/chat-pane-session-menu.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export abstract class ChatPaneSessionMenu extends ChatPaneContext {
226226
sessions: this.context.sessions,
227227
client,
228228
selectedAgentId: this.context.agentSelection.state.selectedId ?? "main",
229+
signal: this.headerSessionMutationAbortController.signal,
229230
};
230231
}
231232

‎ui/src/pages/chat/chat-pane.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import type { GatewaySessionRow } from "../../api/types.ts";
77
import { createChatAttachmentHandoff } from "../../app/chat-attachment-handoff.ts";
88
import type { ApplicationContext } from "../../app/context.ts";
99
import { createInitialUserMessageHandoff } from "../../app/initial-user-message-handoff.ts";
10+
import { t } from "../../i18n/index.ts";
1011
import type { SessionCapability } from "../../lib/sessions/index.ts";
12+
import { showToast } from "../../lib/toast.ts";
13+
import {
14+
installDialogPolyfill,
15+
waitForConfirmDialogActions,
16+
} from "../../test-helpers/modal-dialog.ts";
1117
import {
1218
createSessionContext,
1319
createTestChatPane,
@@ -19,6 +25,8 @@ import type { SidebarContent } from "./components/chat-sidebar.ts";
1925
import { cacheChatSessionSnapshot, type ChatMessageCache } from "./session-message-cache.ts";
2026
import { openSlot } from "./sidebar-layout.ts";
2127

28+
vi.mock("../../lib/toast.ts", () => ({ showToast: vi.fn() }));
29+
2230
afterEach(() => {
2331
vi.unstubAllGlobals();
2432
});
@@ -117,6 +125,50 @@ describe("chat pane header state", () => {
117125
expect(onPaneSessionChange).toHaveBeenCalledWith("single", "agent:main:forked");
118126
});
119127

128+
it("aborts a stale header delete confirm and shows a retry notice when the connection is replaced while it is open", async () => {
129+
const restoreDialogPolyfill = installDialogPolyfill();
130+
try {
131+
const deleteOne = vi.fn(async () => ({ deleted: true }));
132+
const sessions = {
133+
delete: deleteOne,
134+
refreshReplacement: vi.fn(async () => undefined),
135+
} as unknown as SessionCapability;
136+
const client = {} as GatewayBrowserClient;
137+
const { pane } = createTestChatPane({ client, sessions });
138+
const session = {
139+
key: "agent:main:current",
140+
kind: "direct",
141+
updatedAt: 0,
142+
label: "Current session",
143+
} satisfies GatewaySessionRow;
144+
145+
const pending = pane.handleHeaderSessionAction({ kind: "delete" }, session);
146+
await waitForConfirmDialogActions();
147+
// Mirrors a reconnect landing while the header's own confirm dialog is
148+
// open: the chat header builds this scope independently of
149+
// SessionDataController, so it needs its own signal retired here too.
150+
pane.applyGatewaySnapshot({
151+
...pane.context.gateway.snapshot,
152+
phase: "reconnecting",
153+
hello: null,
154+
});
155+
await pending;
156+
157+
expect(deleteOne).not.toHaveBeenCalled();
158+
// The stale dialog must dismiss itself, not merely stop sending its request.
159+
expect(document.body.querySelector("openclaw-modal-dialog")).toBeNull();
160+
// The abort resolves the dialog to `false`, same as a user cancel, so the
161+
// operator needs a distinct, visible outcome or their lost intent reads
162+
// as a click that simply did nothing.
163+
expect(showToast).toHaveBeenCalledWith({
164+
message: t("sessionsView.deleteSessionStale", { session: "Current session" }),
165+
});
166+
} finally {
167+
document.body.replaceChildren();
168+
restoreDialogPolyfill();
169+
}
170+
});
171+
120172
it("commits a trimmed label and clears with null", async () => {
121173
const patch = vi.fn(async () => ({}));
122174
const sessions = { patch } as unknown as SessionCapability;

0 commit comments

Comments
 (0)