Skip to content

Commit b977ccb

Browse files
fix(ui): clarify permission changes apply to the next run (#126893)
* fix(ui): clarify permission changes apply to next run * fix(ui): show next-run permission toast * fix(ui): polish permission toast motion * fix(ui): match permission toast transitions * fix(ui): preserve global toast dismissal * fix(ui): stabilize widget disclosure geometry * fix(ui): preserve disclosure anchor geometry * fix(ui): preserve toast dismissal reason * fix(ui): defer disclosure reconciliation
1 parent 415e868 commit b977ccb

11 files changed

Lines changed: 318 additions & 20 deletions

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5712,7 +5712,8 @@ export const en: TranslationMap = {
57125712
},
57135713
permissionControls: {
57145714
label: "Permissions",
5715-
help: "Choose how this session handles files, commands, and escalation reviews.",
5715+
help: "Choose permissions for new runs.",
5716+
nextRun: "New permissions apply to the next run.",
57165717
default: "Default",
57175718
defaultDescription: "Follow the agent's configured policy.",
57185719
rootLabel: "Root",

‎ui/src/lib/toast.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,20 @@ describe("shared toast", () => {
7373
it("auto-dismisses after the configured duration", async () => {
7474
vi.useFakeTimers();
7575
const host = await mountHost();
76+
const anchor = document.createElement("div");
77+
document.body.append(anchor);
78+
vi.spyOn(anchor, "getBoundingClientRect").mockReturnValue(new DOMRect(0, 0, 100, 100));
7679

77-
showToast({ message: "Temporary", durationMs: 50 });
80+
showToast({ anchor, message: "Temporary", durationMs: 50 });
7881
await host.updateComplete;
7982
await vi.advanceTimersByTimeAsync(50);
8083
await host.updateComplete;
8184

85+
expect(host.querySelector('.app-toast[data-active="false"]')).not.toBeNull();
86+
87+
await vi.runAllTimersAsync();
88+
await host.updateComplete;
89+
8290
expect(host.querySelector(".app-toast")).toBeNull();
8391
});
8492

@@ -95,6 +103,23 @@ describe("shared toast", () => {
95103
expect(host.querySelector(".app-toast")).toBeNull();
96104
});
97105

106+
it("preserves the dismissal reason when an exiting toast is replaced", async () => {
107+
vi.useFakeTimers();
108+
const host = await mountHost();
109+
const anchor = document.createElement("div");
110+
document.body.append(anchor);
111+
vi.spyOn(anchor, "getBoundingClientRect").mockReturnValue(new DOMRect(0, 0, 100, 100));
112+
const reasons: string[] = [];
113+
114+
showToast({ anchor, message: "First", onDismiss: (reason) => reasons.push(reason) });
115+
await host.updateComplete;
116+
host.querySelector<HTMLButtonElement>(".app-toast__dismiss")?.click();
117+
await host.updateComplete;
118+
showToast({ message: "Second" });
119+
120+
expect(reasons).toEqual(["dismiss"]);
121+
});
122+
98123
it("reports why a toast is replaced, dismissed, acted on, or disconnected", async () => {
99124
const host = await mountHost();
100125
const reasons: string[] = [];
@@ -114,6 +139,7 @@ describe("shared toast", () => {
114139
await host.updateComplete;
115140
host.querySelector<HTMLButtonElement>(".app-toast__dismiss")?.click();
116141
await host.updateComplete;
142+
expect(host.querySelector(".app-toast")).toBeNull();
117143

118144
showToast({ message: "Fourth", onDismiss: (reason) => reasons.push(reason) });
119145
host.remove();

‎ui/src/lib/toast.ts‎

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { html, nothing, type TemplateResult } from "lit";
22
import { state } from "lit/decorators.js";
3+
import { styleMap } from "lit/directives/style-map.js";
4+
import { icons } from "../components/icons.ts";
35
import { t } from "../i18n/index.ts";
46
import { OpenClawLightDomContentsElement } from "../lit/openclaw-element.ts";
57
import { formatUiExternalText } from "./format-error.ts";
@@ -10,13 +12,18 @@ export type ToastOptions = {
1012
/** A template lets a message name a destination the operator can actually open,
1113
* instead of spelling out a settings path the toast then makes them find. */
1214
message: string | TemplateResult;
15+
/** Positions a compact toast at the top center of the owning surface. */
16+
anchor?: Element;
17+
anchorTopOffset?: number;
18+
icon?: TemplateResult;
1319
actionLabel?: string;
1420
onAction?: () => void;
1521
onDismiss?: (reason: ToastDismissReason) => void;
1622
durationMs?: number;
1723
};
1824

1925
const DEFAULT_TOAST_DURATION_MS = 6_000;
26+
const TOAST_EXIT_FALLBACK_MS = 450;
2027

2128
function activeModalToastLayer() {
2229
return [...(document.openClawModalToastLayers ?? [])].findLast(
@@ -31,7 +38,10 @@ let queuedToast: ToastOptions | null = null;
3138

3239
class OpenClawToastHost extends OpenClawLightDomContentsElement {
3340
@state() private toast: ToastOptions | null = null;
41+
@state() private active = false;
3442
private dismissTimer: ReturnType<typeof globalThis.setTimeout> | null = null;
43+
private exitTimer: ReturnType<typeof globalThis.setTimeout> | null = null;
44+
private exitReason: ToastDismissReason | null = null;
3545

3646
override connectedCallback() {
3747
super.connectedCallback();
@@ -56,8 +66,10 @@ class OpenClawToastHost extends OpenClawLightDomContentsElement {
5666
connectedMoveCallback() {}
5767

5868
show(options: ToastOptions) {
59-
this.dismiss("replaced");
69+
this.finishDismiss(this.exitReason ?? "replaced");
6070
this.toast = options;
71+
this.active = true;
72+
this.exitReason = null;
6173
this.dismissTimer = globalThis.setTimeout(
6274
() => this.dismiss("timeout"),
6375
options.durationMs ?? DEFAULT_TOAST_DURATION_MS,
@@ -69,22 +81,85 @@ class OpenClawToastHost extends OpenClawLightDomContentsElement {
6981
globalThis.clearTimeout(this.dismissTimer);
7082
this.dismissTimer = null;
7183
}
84+
if (this.exitTimer !== null) {
85+
globalThis.clearTimeout(this.exitTimer);
86+
this.exitTimer = null;
87+
}
7288
}
7389

74-
private dismiss(reason: ToastDismissReason) {
90+
private finishDismiss(reason: ToastDismissReason) {
7591
const toast = this.toast;
7692
this.clearDismissTimer();
93+
this.active = false;
94+
this.exitReason = null;
7795
this.toast = null;
7896
toast?.onDismiss?.(reason);
7997
}
8098

99+
private dismiss(reason: ToastDismissReason) {
100+
const toast = this.toast;
101+
if (!toast) {
102+
return;
103+
}
104+
this.clearDismissTimer();
105+
const reducedMotion = globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
106+
const anchorRect = toast.anchor?.isConnected ? toast.anchor.getBoundingClientRect() : null;
107+
const anchored = anchorRect !== null && anchorRect.width > 0;
108+
if (
109+
(reason !== "dismiss" && reason !== "timeout") ||
110+
reducedMotion ||
111+
!this.isConnected ||
112+
!anchored
113+
) {
114+
this.finishDismiss(reason);
115+
return;
116+
}
117+
this.active = false;
118+
this.exitReason = reason;
119+
this.exitTimer = globalThis.setTimeout(() => {
120+
if (this.toast === toast) {
121+
this.finishDismiss(reason);
122+
}
123+
}, TOAST_EXIT_FALLBACK_MS);
124+
}
125+
81126
override render() {
82127
const toast = this.toast;
83128
if (!toast) {
84129
return nothing;
85130
}
131+
const anchorRect = toast.anchor?.isConnected ? toast.anchor.getBoundingClientRect() : null;
132+
const anchored = anchorRect !== null && anchorRect.width > 0;
86133
return html`
87-
<div class="app-toast" role="status" aria-live="polite" aria-atomic="true">
134+
<div
135+
class="app-toast ${anchored ? "app-toast--anchored" : ""}"
136+
data-active=${this.active ? "true" : "false"}
137+
style=${styleMap(
138+
anchored
139+
? {
140+
"--app-toast-anchor-center": `${anchorRect.left + anchorRect.width / 2}px`,
141+
"--app-toast-anchor-top": `${anchorRect.top + (toast.anchorTopOffset ?? 0)}px`,
142+
"--app-toast-anchor-width": `${anchorRect.width}px`,
143+
}
144+
: {},
145+
)}
146+
role="status"
147+
aria-live="polite"
148+
aria-atomic="true"
149+
@transitionend=${(event: TransitionEvent) => {
150+
if (
151+
event.target === event.currentTarget &&
152+
event.propertyName === "opacity" &&
153+
!this.active &&
154+
this.exitReason
155+
) {
156+
this.finishDismiss(this.exitReason);
157+
}
158+
}}
159+
>
160+
${toast.icon
161+
? html`<span class="app-toast__icon" aria-hidden="true">${toast.icon}</span>`
162+
: nothing}
88163
<span class="app-toast__message"
89164
>${typeof toast.message === "string"
90165
? formatUiExternalText(toast.message)
@@ -110,7 +185,7 @@ class OpenClawToastHost extends OpenClawLightDomContentsElement {
110185
aria-label=${t("common.dismiss")}
111186
@click=${() => this.dismiss("dismiss")}
112187
>
113-
×
188+
${icons.x}
114189
</button>
115190
</div>
116191
`;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export class ChatPane extends ChatPaneLayoutRender {
280280
effortAccess: mutationAccess.effort,
281281
permissionAccess: mutationAccess.permission,
282282
canSelectFull: hasOperatorAdminAccess(gatewaySnapshot.hello?.auth ?? null),
283+
toastAnchor: this,
283284
onModelSetup: () => this.context.navigate("model-setup"),
284285
});
285286
const props: ChatProps = {

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
import type { ChatPageHost } from "./chat-state-host.ts";
1010
import { renderChatPermissionPicker } from "./components/chat-permission-picker.ts";
1111

12+
const { showToastMock } = vi.hoisted(() => ({ showToastMock: vi.fn() }));
13+
14+
vi.mock("../../lib/toast.ts", () => ({ showToast: showToastMock }));
15+
1216
describe("chat model catalog state", () => {
1317
const cachedCatalog = [
1418
{
@@ -83,6 +87,7 @@ describe("chat pane composer controls", () => {
8387
chatStream: null,
8488
} as unknown as ChatPageHost;
8589
const onModelSetup = vi.fn();
90+
const toastAnchor = document.createElement("div");
8691

8792
const controls = renderChatPaneComposerControls({
8893
state,
@@ -92,6 +97,7 @@ describe("chat pane composer controls", () => {
9297
effortAccess: { allowed: true, requiredScope: "operator.write" },
9398
permissionAccess: { allowed: true, requiredScope: "operator.write" },
9499
canSelectFull: true,
100+
toastAnchor,
95101
onModelSetup,
96102
});
97103
render(controls.composerControls, container);
@@ -141,6 +147,7 @@ describe("chat pane composer controls", () => {
141147
effortAccess: { allowed: true, requiredScope: "operator.write" },
142148
permissionAccess: { allowed: true, requiredScope: "operator.write" },
143149
canSelectFull: false,
150+
toastAnchor: document.createElement("div"),
144151
onModelSetup: vi.fn(),
145152
});
146153
render(renderChatPermissionPicker(controls.permissionPicker), container);
@@ -175,6 +182,66 @@ describe("chat pane composer controls", () => {
175182
);
176183
});
177184

185+
it.each([
186+
{ label: "running", chatRunId: null, hasActiveRun: true, status: "running", toastCount: 1 },
187+
{
188+
label: "locally running with a stale idle session row",
189+
chatRunId: "run-active",
190+
hasActiveRun: false,
191+
status: "done",
192+
toastCount: 1,
193+
},
194+
{ label: "idle", chatRunId: null, hasActiveRun: false, status: "done", toastCount: 0 },
195+
] as const)("shows the next-run notice only for a $label session", async (sessionCase) => {
196+
showToastMock.mockClear();
197+
const patch = vi.fn(async () => ({}));
198+
const toastAnchor = document.createElement("div");
199+
const state = {
200+
chatRunId: sessionCase.chatRunId,
201+
connected: true,
202+
client: {},
203+
chatLoading: false,
204+
chatModelCatalog: [],
205+
sessions: { state: { modelOverrides: {} }, patch },
206+
chatModelSwitchPromises: {},
207+
sessionKey: "agent:main:permission-notice",
208+
chatModelsLoading: false,
209+
chatSending: false,
210+
sessionsResult: null,
211+
chatStream: null,
212+
} as unknown as ChatPageHost;
213+
const controls = renderChatPaneComposerControls({
214+
state,
215+
selectedSession: {
216+
key: state.sessionKey,
217+
kind: "direct",
218+
permissionMode: "read-only",
219+
hasActiveRun: sessionCase.hasActiveRun,
220+
status: sessionCase.status,
221+
},
222+
agentDefaultModel: undefined,
223+
modelAccess: { allowed: true, requiredScope: "operator.write" },
224+
effortAccess: { allowed: true, requiredScope: "operator.write" },
225+
permissionAccess: { allowed: true, requiredScope: "operator.write" },
226+
canSelectFull: true,
227+
toastAnchor,
228+
onModelSetup: vi.fn(),
229+
});
230+
231+
await controls.permissionPicker.onSelect("guarded");
232+
233+
expect(showToastMock).toHaveBeenCalledTimes(sessionCase.toastCount);
234+
if (sessionCase.toastCount === 1) {
235+
expect(showToastMock).toHaveBeenCalledWith(
236+
expect.objectContaining({
237+
anchor: toastAnchor,
238+
durationMs: 5_000,
239+
message: "New permissions apply to the next run.",
240+
}),
241+
);
242+
}
243+
});
244+
178245
it("refreshes the configured model catalog when the picker opens", async () => {
179246
const container = document.createElement("div");
180247
const request = vi.fn(async () => ({ models: [] }));
@@ -203,6 +270,7 @@ describe("chat pane composer controls", () => {
203270
effortAccess: { allowed: true, requiredScope: "operator.write" },
204271
permissionAccess: { allowed: true, requiredScope: "operator.write" },
205272
canSelectFull: true,
273+
toastAnchor: document.createElement("div"),
206274
onModelSetup: vi.fn(),
207275
});
208276
render(controls.composerControls, container);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { html } from "lit";
22
import type { GatewaySessionRow } from "../../api/types.ts";
33
import type { ApplicationGatewaySnapshot } from "../../app/gateway.ts";
4+
import { icons } from "../../components/icons.ts";
45
import { t } from "../../i18n/index.ts";
56
import {
67
readSessionMethodAccess,
78
type SessionMethodAccess,
89
} from "../../lib/session-method-access.ts";
10+
import { isSessionRunActive } from "../../lib/session-run-state.ts";
911
import { scopedAgentParamsForSession } from "../../lib/sessions/index.ts";
12+
import { showToast } from "../../lib/toast.ts";
1013
import { readChatSessionActionAccess } from "./chat-session-action-access.ts";
1114
import {
1215
switchChatContextWindow,
@@ -84,6 +87,7 @@ export function renderChatPaneComposerControls(params: {
8487
effortAccess: SessionMethodAccess;
8588
permissionAccess: SessionMethodAccess;
8689
canSelectFull: boolean;
90+
toastAnchor: Element;
8791
onModelSetup: () => void;
8892
}): {
8993
composerControls: NonNullable<ChatProps["composerControls"]>;
@@ -97,6 +101,7 @@ export function renderChatPaneComposerControls(params: {
97101
effortAccess,
98102
permissionAccess,
99103
canSelectFull,
104+
toastAnchor,
100105
onModelSetup,
101106
} = params;
102107
const modelCatalogState = resolveChatModelCatalogState(state);
@@ -154,13 +159,28 @@ export function renderChatPaneComposerControls(params: {
154159
if (!permissionAccess.allowed) {
155160
return;
156161
}
162+
const runWasActive =
163+
Boolean(state.chatRunId) ||
164+
Boolean(selectedSession && isSessionRunActive(selectedSession));
157165
try {
158166
state.chatError = null;
159167
await state.sessions.patch(
160168
state.sessionKey,
161169
{ permissionMode },
162170
scopedAgentParamsForSession(state, state.sessionKey),
163171
);
172+
if (runWasActive) {
173+
const topbarHeight = toastAnchor
174+
.querySelector(".chat-pane__header")
175+
?.getBoundingClientRect().height;
176+
showToast({
177+
anchor: toastAnchor,
178+
anchorTopOffset: (topbarHeight ?? 0) + 12,
179+
durationMs: 5_000,
180+
icon: icons.shieldCheck,
181+
message: t("chat.permissionControls.nextRun"),
182+
});
183+
}
164184
} catch (error) {
165185
state.chatError = t("chat.permissionControls.updateFailed", {
166186
error: String(error),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ describe("refreshChat", () => {
501501
effortAccess: { allowed: true, requiredScope: "operator.write" },
502502
permissionAccess: { allowed: true, requiredScope: "operator.write" },
503503
canSelectFull: true,
504+
toastAnchor: document.createElement("div"),
504505
onModelSetup: vi.fn(),
505506
});
506507
render(controls.composerControls, container);

0 commit comments

Comments
 (0)