Skip to content

Commit 6a0b6c2

Browse files
committed
fix(ui): surface aborted-run diagnostics
1 parent ba2827f commit 6a0b6c2

4 files changed

Lines changed: 278 additions & 4 deletions

File tree

‎ui/src/e2e/chat-run-lifecycle.e2e.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,44 @@ describeControlUiE2e("Control UI chat run lifecycle", () => {
395395
.toBe("");
396396
await gateway.resolveDeferred("sessions.list");
397397
});
398+
399+
it("renders a safe self-abort diagnostic while preserving interrupted status", async () => {
400+
const artifactDir = path.resolve(".artifacts/control-ui-e2e/chat-abort-diagnostic");
401+
const captureProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1";
402+
if (captureProof) {
403+
await mkdir(artifactDir, { recursive: true });
404+
}
405+
const context = await browser.newContext({ viewport: { height: 800, width: 1200 } });
406+
const currentPage = await context.newPage();
407+
page = currentPage;
408+
const gateway = await installMockGateway(currentPage);
409+
410+
await currentPage.goto(`${server?.baseUrl ?? ""}chat`);
411+
await currentPage.locator(".agent-chat__input textarea").fill("run the edit");
412+
await currentPage.getByRole("button", { name: "Send message" }).click();
413+
const send = await gateway.waitForRequest("chat.send");
414+
const params = send.params as { idempotencyKey?: unknown };
415+
expect(typeof params.idempotencyKey).toBe("string");
416+
const runId = params.idempotencyKey as string;
417+
const diagnostic = "edit tool validation failed: edits: must be an array";
418+
419+
await gateway.emitGatewayEvent("chat", {
420+
errorMessage: diagnostic,
421+
runId,
422+
sessionKey: "main",
423+
state: "aborted",
424+
});
425+
426+
const alert = currentPage.getByRole("alert").filter({ hasText: diagnostic });
427+
await alert.waitFor();
428+
expect((await alert.textContent())?.trim()).toContain(`Error: ${diagnostic}`);
429+
await currentPage.getByLabel("Run status: Interrupted").waitFor();
430+
expect(await currentPage.getByRole("button", { name: "Stop generating" }).count()).toBe(0);
431+
if (captureProof) {
432+
await currentPage.screenshot({
433+
path: path.join(artifactDir, "abort-diagnostic-alert.png"),
434+
fullPage: true,
435+
});
436+
}
437+
});
398438
});
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// @vitest-environment node
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3+
import { handleChatGatewayEvent, type ChatEventPayload } from "./chat-gateway.ts";
4+
import type { ChatState } from "./chat-history.ts";
5+
6+
type AbortDiagnosticState = ChatState & {
7+
chatRunStatus?: { phase: string; runId: string | null; sessionKey: string } | null;
8+
lastLocalTerminalReconcile?: { sessionStatus: string } | null;
9+
sessionsResult?: {
10+
ts: number;
11+
path: string;
12+
count: number;
13+
defaults: Record<string, unknown>;
14+
sessions: Array<Record<string, unknown>>;
15+
};
16+
};
17+
18+
function createAbortDiagnosticState(runId = "run-validation-abort"): AbortDiagnosticState {
19+
return {
20+
chatAttachments: [],
21+
chatLoading: false,
22+
chatMessage: "",
23+
chatMessages: [],
24+
chatQueue: [],
25+
chatRunError: null,
26+
chatRunId: runId,
27+
chatSending: false,
28+
chatStream: "Partial assistant reply",
29+
chatStreamStartedAt: 100,
30+
chatRunStartup: null,
31+
chatThinkingLevel: null,
32+
chatVerboseLevel: null,
33+
client: null,
34+
connected: true,
35+
connectionEpoch: 0,
36+
hello: null,
37+
lastError: null,
38+
sessionKey: "main",
39+
sessionsResult: {
40+
ts: 0,
41+
path: "",
42+
count: 1,
43+
defaults: {},
44+
sessions: [
45+
{
46+
key: "main",
47+
kind: "direct",
48+
updatedAt: 1,
49+
hasActiveRun: true,
50+
activeRunIds: [runId],
51+
status: "running",
52+
startedAt: 100,
53+
},
54+
],
55+
},
56+
};
57+
}
58+
59+
describe("aborted chat diagnostics", () => {
60+
beforeEach(() => vi.useFakeTimers());
61+
afterEach(() => {
62+
vi.clearAllTimers();
63+
vi.useRealTimers();
64+
});
65+
66+
it.each([
67+
{ name: "plain cancellation", errorMessage: undefined, expectedError: null },
68+
{
69+
name: "validation self-abort",
70+
errorMessage: "edit tool validation failed: edits: must be an array",
71+
expectedError: "Error: edit tool validation failed: edits: must be an array",
72+
},
73+
])("keeps first-terminal $name killed and interrupted", ({ errorMessage, expectedError }) => {
74+
const state = createAbortDiagnosticState();
75+
const payload: ChatEventPayload = {
76+
runId: "run-validation-abort",
77+
sessionKey: "main",
78+
state: "aborted",
79+
...(errorMessage ? { errorMessage } : {}),
80+
};
81+
82+
expect(handleChatGatewayEvent(state, payload)).toBe("aborted");
83+
84+
expect(state.chatRunError?.summary ?? null).toBe(expectedError);
85+
expect(state.chatRunStatus).toMatchObject({
86+
phase: "interrupted",
87+
runId: "run-validation-abort",
88+
sessionKey: "main",
89+
});
90+
expect(state.lastLocalTerminalReconcile?.sessionStatus).toBe("killed");
91+
expect(state.sessionsResult?.sessions[0]).toMatchObject({
92+
activeRunIds: [],
93+
hasActiveRun: false,
94+
status: "killed",
95+
});
96+
expect(state.chatRunId).toBeNull();
97+
});
98+
99+
it("surfaces one late aborted diagnostic and ignores its replay", () => {
100+
const state = createAbortDiagnosticState();
101+
const aborted = {
102+
runId: "run-validation-abort",
103+
sessionKey: "main",
104+
state: "aborted" as const,
105+
};
106+
const diagnostic = {
107+
...aborted,
108+
errorMessage: "edit tool validation failed: edits: must be an array",
109+
};
110+
111+
expect(handleChatGatewayEvent(state, aborted)).toBe("aborted");
112+
expect(state.chatRunError).toBeNull();
113+
expect(handleChatGatewayEvent(state, diagnostic)).toBe("aborted");
114+
const displayedDiagnostic = state.chatRunError;
115+
expect(handleChatGatewayEvent(state, diagnostic)).toBe("aborted");
116+
117+
expect(state.chatRunError).toBe(displayedDiagnostic);
118+
expect(state.chatRunError).toEqual({
119+
summary: "Error: edit tool validation failed: edits: must be an array",
120+
});
121+
expect(state.chatRunId).toBeNull();
122+
});
123+
124+
it("does not publish a late aborted diagnostic over a newer active run", () => {
125+
const state = createAbortDiagnosticState("run-old");
126+
expect(
127+
handleChatGatewayEvent(state, {
128+
runId: "run-old",
129+
sessionKey: "main",
130+
state: "aborted",
131+
}),
132+
).toBe("aborted");
133+
state.chatRunId = "run-new";
134+
state.chatStream = "New run output";
135+
136+
expect(
137+
handleChatGatewayEvent(state, {
138+
runId: "run-old",
139+
sessionKey: "main",
140+
state: "aborted",
141+
errorMessage: "edit tool validation failed: invalid arguments",
142+
}),
143+
).toBe("aborted");
144+
145+
expect(state.chatRunError).toBeNull();
146+
expect(state.chatRunId).toBe("run-new");
147+
expect(state.chatStream).toBe("New run output");
148+
});
149+
150+
it("does not restore an old diagnostic while a newer send awaits its ACK", () => {
151+
const state = createAbortDiagnosticState("run-old");
152+
expect(
153+
handleChatGatewayEvent(state, {
154+
runId: "run-old",
155+
sessionKey: "main",
156+
state: "aborted",
157+
}),
158+
).toBe("aborted");
159+
state.chatQueue = [
160+
{
161+
id: "pending-new-send",
162+
text: "New request",
163+
createdAt: 200,
164+
sendRunId: "run-new",
165+
sendState: "sending",
166+
},
167+
];
168+
169+
expect(
170+
handleChatGatewayEvent(state, {
171+
runId: "run-old",
172+
sessionKey: "main",
173+
state: "aborted",
174+
errorMessage: "edit tool validation failed: invalid arguments",
175+
}),
176+
).toBe("aborted");
177+
178+
expect(state.chatRunError).toBeNull();
179+
expect(state.chatRunId).toBeNull();
180+
expect(state.lastLocalTerminalReconcile?.runId).toBe("run-old");
181+
});
182+
183+
it("does not publish an old aborted diagnostic after a newer run completes", () => {
184+
const state = createAbortDiagnosticState("run-old");
185+
expect(
186+
handleChatGatewayEvent(state, {
187+
runId: "run-old",
188+
sessionKey: "main",
189+
state: "aborted",
190+
}),
191+
).toBe("aborted");
192+
state.chatRunId = "run-new";
193+
state.chatStream = "New final answer";
194+
expect(
195+
handleChatGatewayEvent(state, {
196+
runId: "run-new",
197+
sessionKey: "main",
198+
state: "final",
199+
message: {
200+
role: "assistant",
201+
content: [{ type: "text", text: "New final answer" }],
202+
},
203+
}),
204+
).toBe("final");
205+
206+
expect(
207+
handleChatGatewayEvent(state, {
208+
runId: "run-old",
209+
sessionKey: "main",
210+
state: "aborted",
211+
errorMessage: "edit tool validation failed: invalid arguments",
212+
}),
213+
).toBe("aborted");
214+
215+
expect(state.chatRunError).toBeNull();
216+
expect(state.lastLocalTerminalReconcile?.runId).toBe("run-new");
217+
expect(state.chatRunId).toBeNull();
218+
expect(state.chatMessages.at(-1)).toMatchObject({
219+
role: "assistant",
220+
content: [{ type: "text", text: "New final answer" }],
221+
});
222+
});
223+
});

‎ui/src/pages/chat/chat-gateway.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,24 @@ function handleChatEvent(
271271
if (payload.state === "delta") {
272272
return null;
273273
}
274-
if (payload.state === "error") {
274+
if (payload.state === "error" || payload.state === "aborted") {
275+
const pendingRunId = state.chatQueue.find(
276+
(item) => item.sendState === "sending" && item.sendRunId,
277+
)?.sendRunId;
278+
const diagnosticOwnerRunId =
279+
state.chatRunId ?? pendingRunId ?? state.lastLocalTerminalReconcile?.runId;
275280
if (
276-
(!state.chatRunId || state.chatRunId === payload.runId) &&
281+
diagnosticOwnerRunId === payload.runId &&
277282
payload.errorMessage?.trim() &&
278283
projectedRun.currentRun?.errorMessage !== previousTerminalRun.errorMessage
279284
) {
280-
// Completed-run diagnostics belong to an idle composer or that same run;
285+
// Late diagnostics belong to the active, pending, or latest locally terminal run;
281286
// publishing them over a newer response falsely marks the new run failed.
282287
setChatRunError(state, resolveGatewayErrorText(payload, null));
283288
}
284-
return "error";
289+
if (payload.state === "error") {
290+
return "error";
291+
}
285292
}
286293
const incomingFinal = normalizedFinalMessage;
287294
if (
@@ -419,6 +426,9 @@ function handleChatEvent(
419426
} else {
420427
state.chatMessages = materializeVisibleStream();
421428
}
429+
if (payload.errorMessage?.trim()) {
430+
setChatRunError(state, resolveGatewayErrorText(payload, null));
431+
}
422432
reconcileTerminalRun("interrupted", "killed");
423433
} else if (payload.state === "error") {
424434
const payloadMessage = normalizeFinalAssistantMessage(payload.message);

‎ui/src/pages/chat/chat-history.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export type ChatState = {
289289
lastError: string | null;
290290
chatError?: string | null;
291291
chatRunError?: { summary: string } | null;
292+
lastLocalTerminalReconcile?: { runId: string | null } | null;
292293
chatReplyTarget?: unknown;
293294
agentsError?: string | null;
294295
onAgentsList?: (agentsList: AgentsListResult, client: GatewayBrowserClient) => void;

0 commit comments

Comments
 (0)