Skip to content

Commit 3fe0756

Browse files
committed
fix: recover stuck control-ui chat runs
1 parent 6b87489 commit 3fe0756

6 files changed

Lines changed: 307 additions & 2 deletions

File tree

‎ui/src/ui/app-chat.ts‎

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { resetToolStream } from "./app-tool-stream.ts";
55
import type { OpenClawApp } from "./app.ts";
66
import { abortChatRun, loadChatHistory, sendChatMessage } from "./controllers/chat.ts";
77
import { loadSessions } from "./controllers/sessions.ts";
8+
import type { GatewayBrowserClient } from "./gateway.ts";
89
import type { GatewayHelloOk } from "./gateway.ts";
910
import { normalizeBasePath } from "./navigation.ts";
1011
import type { ChatAttachment, ChatQueueItem } from "./ui-types.ts";
@@ -25,11 +26,111 @@ export type ChatHost = {
2526
};
2627

2728
export const CHAT_SESSIONS_ACTIVE_MINUTES = 120;
29+
const CHAT_RUN_WATCHDOG_IDLE_MS = 15_000;
30+
const CHAT_RUN_WATCHDOG_RETRY_MS = 5_000;
31+
const CHAT_RUN_WATCHDOG_WAIT_TIMEOUT_MS = 50;
2832

2933
export function isChatBusy(host: ChatHost) {
3034
return host.chatSending || Boolean(host.chatRunId);
3135
}
3236

37+
type ChatRunWatchdogHost = ChatHost & {
38+
client: GatewayBrowserClient | null;
39+
chatLoading: boolean;
40+
chatMessages: unknown[];
41+
chatThinkingLevel: string | null;
42+
chatRunLastActivityAt: number | null;
43+
chatRunWatchdogTimer: number | null;
44+
chatRunWatchdogProbeInFlight: boolean;
45+
chatStream: string | null;
46+
chatStreamStartedAt: number | null;
47+
lastError: string | null;
48+
};
49+
50+
function clearChatRunWatchdogTimer(host: ChatRunWatchdogHost) {
51+
if (host.chatRunWatchdogTimer !== null) {
52+
globalThis.clearTimeout(host.chatRunWatchdogTimer);
53+
host.chatRunWatchdogTimer = null;
54+
}
55+
}
56+
57+
export function resetChatRunWatchdog(host: ChatRunWatchdogHost) {
58+
clearChatRunWatchdogTimer(host);
59+
host.chatRunWatchdogProbeInFlight = false;
60+
if (!host.chatRunId) {
61+
host.chatRunLastActivityAt = null;
62+
}
63+
}
64+
65+
async function runChatRunWatchdog(host: ChatRunWatchdogHost, runId: string) {
66+
if (!host.connected || !host.client || !host.chatRunId || host.chatRunId !== runId) {
67+
resetChatRunWatchdog(host);
68+
return;
69+
}
70+
if (host.chatRunWatchdogProbeInFlight) {
71+
scheduleChatRunWatchdog(host, CHAT_RUN_WATCHDOG_RETRY_MS);
72+
return;
73+
}
74+
75+
host.chatRunWatchdogProbeInFlight = true;
76+
try {
77+
const result = await host.client.request<{ status?: string }>("agent.wait", {
78+
runId,
79+
timeoutMs: CHAT_RUN_WATCHDOG_WAIT_TIMEOUT_MS,
80+
});
81+
if (host.chatRunId !== runId) {
82+
return;
83+
}
84+
if (result?.status === "ok" || result?.status === "error") {
85+
host.chatRunId = null;
86+
host.chatStream = null;
87+
host.chatStreamStartedAt = null;
88+
host.chatRunLastActivityAt = null;
89+
resetToolStream(host as unknown as Parameters<typeof resetToolStream>[0]);
90+
try {
91+
await loadChatHistory(host as unknown as OpenClawApp);
92+
} finally {
93+
await flushChatQueue(host);
94+
}
95+
return;
96+
}
97+
} catch (err) {
98+
host.lastError = String(err);
99+
} finally {
100+
host.chatRunWatchdogProbeInFlight = false;
101+
}
102+
103+
if (host.chatRunId === runId) {
104+
scheduleChatRunWatchdog(host, CHAT_RUN_WATCHDOG_RETRY_MS);
105+
}
106+
}
107+
108+
export function scheduleChatRunWatchdog(host: ChatRunWatchdogHost, delayMs?: number) {
109+
clearChatRunWatchdogTimer(host);
110+
if (!host.connected || !host.client || !host.chatRunId) {
111+
resetChatRunWatchdog(host);
112+
return;
113+
}
114+
115+
const now = Date.now();
116+
const lastActivityAt = host.chatRunLastActivityAt ?? now;
117+
const delay =
118+
delayMs ?? Math.max(0, CHAT_RUN_WATCHDOG_IDLE_MS - Math.max(0, now - lastActivityAt));
119+
const runId = host.chatRunId;
120+
host.chatRunWatchdogTimer = globalThis.setTimeout(() => {
121+
host.chatRunWatchdogTimer = null;
122+
void runChatRunWatchdog(host, runId);
123+
}, delay);
124+
}
125+
126+
export function noteChatRunActivity(host: ChatRunWatchdogHost, now = Date.now()) {
127+
if (!host.chatRunId) {
128+
return;
129+
}
130+
host.chatRunLastActivityAt = now;
131+
scheduleChatRunWatchdog(host);
132+
}
133+
33134
export function isChatStopCommand(text: string) {
34135
const trimmed = text.trim();
35136
if (!trimmed) {
@@ -65,7 +166,10 @@ export async function handleAbortChat(host: ChatHost) {
65166
return;
66167
}
67168
host.chatMessage = "";
68-
await abortChatRun(host as unknown as OpenClawApp);
169+
const ok = await abortChatRun(host as unknown as OpenClawApp);
170+
if (ok && host.chatRunId) {
171+
noteChatRunActivity(host as unknown as ChatRunWatchdogHost);
172+
}
69173
}
70174

71175
function enqueueChatMessage(

‎ui/src/ui/app-gateway.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import {
33
type GatewayUpdateAvailableEventPayload,
44
} from "../../../src/gateway/events.js";
55
import { ConnectErrorDetailCodes } from "../../../src/gateway/protocol/connect-error-details.js";
6-
import { CHAT_SESSIONS_ACTIVE_MINUTES, flushChatQueueForEvent } from "./app-chat.ts";
6+
import {
7+
CHAT_SESSIONS_ACTIVE_MINUTES,
8+
flushChatQueueForEvent,
9+
noteChatRunActivity,
10+
resetChatRunWatchdog,
11+
scheduleChatRunWatchdog,
12+
} from "./app-chat.ts";
713
import type { EventLogEntry } from "./app-events.ts";
814
import {
915
applySettings,
@@ -91,6 +97,11 @@ type GatewayHost = {
9197
serverVersion: string | null;
9298
sessionKey: string;
9399
chatRunId: string | null;
100+
chatRunLastActivityAt: number | null;
101+
chatRunWatchdogTimer: number | null;
102+
chatRunWatchdogProbeInFlight: boolean;
103+
chatStreamStartedAt: number | null;
104+
chatStream: string | null;
94105
refreshSessionsAfterChat: Set<string>;
95106
execApprovalQueue: ExecApprovalRequest[];
96107
execApprovalError: string | null;
@@ -218,6 +229,8 @@ export function connectGateway(host: GatewayHost) {
218229
host.chatRunId = null;
219230
(host as unknown as { chatStream: string | null }).chatStream = null;
220231
(host as unknown as { chatStreamStartedAt: number | null }).chatStreamStartedAt = null;
232+
host.chatRunLastActivityAt = null;
233+
resetChatRunWatchdog(host);
221234
resetToolStream(host as unknown as Parameters<typeof resetToolStream>[0]);
222235
void loadAssistantIdentity(host as unknown as OpenClawApp);
223236
void loadAgents(host as unknown as OpenClawApp);
@@ -248,6 +261,7 @@ export function connectGateway(host: GatewayHost) {
248261
host.lastError = null;
249262
host.lastErrorCode = null;
250263
}
264+
resetChatRunWatchdog(host);
251265
},
252266
onEvent: (evt) => {
253267
if (host.client !== client) {
@@ -315,6 +329,13 @@ function handleChatGatewayEvent(host: GatewayHost, payload: ChatEventPayload | u
315329
);
316330
}
317331
const state = handleChatEvent(host as unknown as OpenClawApp, payload);
332+
if (state === "delta") {
333+
noteChatRunActivity(host);
334+
} else if (host.chatRunId) {
335+
scheduleChatRunWatchdog(host);
336+
} else {
337+
resetChatRunWatchdog(host);
338+
}
318339
const historyReloaded = handleTerminalChatEvent(host, payload, state);
319340
if (state === "final" && !historyReloaded && shouldReloadHistoryForFinalEvent(payload)) {
320341
void loadChatHistory(host as unknown as OpenClawApp);

‎ui/src/ui/app-lifecycle.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resetChatRunWatchdog, scheduleChatRunWatchdog } from "./app-chat.ts";
12
import { connectGateway } from "./app-gateway.ts";
23
import {
34
startLogsPolling,
@@ -35,6 +36,10 @@ type LifecycleHost = {
3536
chatMessages: unknown[];
3637
chatToolMessages: unknown[];
3738
chatStream: string;
39+
chatRunId: string | null;
40+
chatRunLastActivityAt: number | null;
41+
chatRunWatchdogTimer: number | null;
42+
chatRunWatchdogProbeInFlight: boolean;
3843
logsAutoFollow: boolean;
3944
logsAtBottom: boolean;
4045
logsEntries: unknown[];
@@ -76,6 +81,7 @@ export function handleDisconnected(host: LifecycleHost) {
7681
stopNodesPolling(host as unknown as Parameters<typeof stopNodesPolling>[0]);
7782
stopLogsPolling(host as unknown as Parameters<typeof stopLogsPolling>[0]);
7883
stopDebugPolling(host as unknown as Parameters<typeof stopDebugPolling>[0]);
84+
resetChatRunWatchdog(host as unknown as Parameters<typeof resetChatRunWatchdog>[0]);
7985
host.client?.stop();
8086
host.client = null;
8187
host.connected = false;
@@ -85,6 +91,13 @@ export function handleDisconnected(host: LifecycleHost) {
8591
}
8692

8793
export function handleUpdated(host: LifecycleHost, changed: Map<PropertyKey, unknown>) {
94+
if (changed.has("chatRunId")) {
95+
if (host.chatRunId) {
96+
scheduleChatRunWatchdog(host as unknown as Parameters<typeof scheduleChatRunWatchdog>[0]);
97+
} else {
98+
resetChatRunWatchdog(host as unknown as Parameters<typeof resetChatRunWatchdog>[0]);
99+
}
100+
}
88101
if (host.tab === "chat" && host.chatManualRefreshInFlight) {
89102
return;
90103
}

‎ui/src/ui/app.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ export class OpenClawApp extends LitElement {
383383
private chatScrollTimeout: number | null = null;
384384
private chatHasAutoScrolled = false;
385385
private chatUserNearBottom = true;
386+
chatRunLastActivityAt: number | null = null;
387+
chatRunWatchdogTimer: number | null = null;
388+
chatRunWatchdogProbeInFlight = false;
386389
@state() chatNewMessagesBelow = false;
387390
private nodesPollInterval: number | null = null;
388391
private logsPollInterval: number | null = null;

0 commit comments

Comments
 (0)