Skip to content

Commit 9812f94

Browse files
committed
fix(ui): surface route-switched command failures and agent-scope global toast naming
ClawSweeper review findings on #124473: - A queued local command failing after the operator navigated away hit failCommand(error) with expose=false; the dispatcher's stale-scope guard had already withheld the inline error, so a successful state write recorded the failure invisibly — the silent class this PR removes. Expose it globally when the scope is stale and the owning pane is hidden; a stale scope with the pane still visible keeps the failed queue chip (the new connection owns the inline surface). - Global session rows are agent-scoped behind one shared "global" key, so the toast row lookup could borrow another agent's label. Match the row's agentId to the failed outbox's agent for global keys. Both regression tests fail pre-fix (stash-verified).
1 parent 7dddb11 commit 9812f94

3 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎ui/src/pages/chat/chat-outbox-drain.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,17 @@ async function drainStoredChatOutbox(
470470
return "blocked";
471471
}
472472
if (dispatchResult === "failed") {
473+
// A still-current scope already saw the dispatcher's inline error.
474+
// After a route switch the dispatcher withholds it and the pane is
475+
// gone, so the terminal failure must surface globally. A stale scope
476+
// with the pane still visible (connection replaced) keeps the failed
477+
// queue chip instead: the new connection owns the inline surface.
473478
const commandStillCurrent = commandScopeIsCurrent();
474479
const error =
475480
(commandStillCurrent ? host.lastError : null) ??
476481
`Command /${item.localCommandName} failed.`;
477-
return failCommand(error);
482+
const paneHidden = !visibleSessionMatches(host, outbox.sessionKey, outbox.agentId);
483+
return failCommand(error, !commandStillCurrent && paneHidden);
478484
}
479485
if (dispatchResult === "uncertain") {
480486
const currentOutbox = readStoredChatOutbox(host, outbox);

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9566,6 +9566,81 @@ describe("handleSendChat", () => {
95669566
});
95679567
expect(host.request.mock.calls.filter(([method]) => method === "chat.send")).toHaveLength(0);
95689568
});
9569+
9570+
it("surfaces a failed local command globally after a route switch", async () => {
9571+
const toastHost = document.createElement("openclaw-toast-host");
9572+
document.body.append(toastHost);
9573+
const item = createQueuedLocalCommand("route-switched-command", "/think", {
9574+
sessionKey: "agent:main:first",
9575+
});
9576+
// The dispatcher reports failure after the operator navigated away, so its
9577+
// stale-scope guard withholds the inline error.
9578+
executeSlashCommandMock.mockImplementation(async () => {
9579+
host.sessionKey = "agent:main:second";
9580+
return { failed: true, content: "think mode rejected" };
9581+
});
9582+
const host = makeChatHost({
9583+
requestHandlers: {
9584+
"chat.history": () => idleChatHistory("agent:main:first"),
9585+
},
9586+
chatQueue: [item],
9587+
sessionKey: item.sessionKey,
9588+
});
9589+
admitHostQueueItems(host);
9590+
9591+
await retryReconnectableQueuedChatSends(host);
9592+
9593+
// Pre-fix: the failure was recorded on the queue item with no visible outcome.
9594+
expect(host.lastError).toBeNull();
9595+
await waitForFast(() => expect(document.body.textContent).toContain("Command /think failed."));
9596+
expect(listStoredChatOutboxes(host).flatMap((outbox) => outbox.queue)).toEqual([
9597+
expect.objectContaining({ id: item.id, sendState: "failed" }),
9598+
]);
9599+
document.body.replaceChildren();
9600+
});
9601+
9602+
it("names the failed agent's global session in the toast, not another agent's row", async () => {
9603+
const toastHost = document.createElement("openclaw-toast-host");
9604+
document.body.append(toastHost);
9605+
const host = makeChatHost({
9606+
requestHandlers: {
9607+
"chat.history": idleChatHistory("global"),
9608+
"chat.send": () => {
9609+
throw new GatewayRequestError({
9610+
code: "UNAUTHORIZED",
9611+
message: "gateway auth failed",
9612+
retryable: false,
9613+
});
9614+
},
9615+
},
9616+
chatQueue: [
9617+
{
9618+
id: "global-agent-scoped-failure",
9619+
text: "fails on the second agent's global session",
9620+
createdAt: 1,
9621+
sendAttempts: 0,
9622+
sendRunId: "global-agent-scoped-run",
9623+
sendState: "waiting-idle",
9624+
sessionKey: "global",
9625+
agentId: "writer",
9626+
},
9627+
],
9628+
sessionKey: "agent:main:elsewhere",
9629+
sessionsResult: createSessionsResult([
9630+
row("global", { agentId: "main", label: "Main global chat" }),
9631+
row("global", { agentId: "writer", label: "Writer global chat" }),
9632+
]),
9633+
});
9634+
admitHostQueueItems(host);
9635+
9636+
await retryReconnectableQueuedChatSends(host);
9637+
9638+
await waitForFast(() => expect(document.body.textContent).toContain("gateway auth failed"));
9639+
// Global rows share one key; the toast must borrow the failed agent's label.
9640+
expect(document.body.textContent).toContain("Writer global chat");
9641+
expect(document.body.textContent).not.toContain("Main global chat");
9642+
document.body.replaceChildren();
9643+
});
95699644
});
95709645

95719646
describe("handleAbortChat", () => {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { resolveSessionDisplayName } from "../../lib/session-display.ts";
88
import { visibleSessionMatches } from "../../lib/sessions/index.ts";
99
import {
1010
areUiSessionKeysEquivalent,
11+
isUiGlobalSessionKey,
12+
normalizeAgentId,
1113
uiSessionRowMatchesSelectedChat,
1214
} from "../../lib/sessions/session-key.ts";
1315
import { showToast } from "../../lib/toast.ts";
@@ -342,8 +344,15 @@ export function surfaceChatDeliveryFailure(
342344
host.chatError = error;
343345
return;
344346
}
345-
const row = host.sessionsResult?.sessions.find((session) =>
346-
areUiSessionKeysEquivalent(session.key, sessionKey),
347+
// Global rows are agent-scoped while sharing one "global" key, so an
348+
// agent-less equivalence match could borrow another agent's label.
349+
const scopedAgentId = agentId ? normalizeAgentId(agentId) : undefined;
350+
const row = host.sessionsResult?.sessions.find(
351+
(session) =>
352+
areUiSessionKeysEquivalent(session.key, sessionKey) &&
353+
(!isUiGlobalSessionKey(sessionKey) ||
354+
!scopedAgentId ||
355+
(session.agentId !== undefined && normalizeAgentId(session.agentId) === scopedAgentId)),
347356
);
348357
showToast({ message: `${resolveSessionDisplayName(sessionKey, row)}: ${error}` });
349358
}

0 commit comments

Comments
 (0)