Skip to content

Commit b8a95e0

Browse files
authored
fix(ui): make terminal open failures retryable (#124890)
1 parent 0099050 commit b8a95e0

10 files changed

Lines changed: 349 additions & 26 deletions

‎ui/src/components/terminal/terminal-connection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class TerminalOpenTimeoutError extends Error {
101101
* tab label, uploads, or a replay. Fail here so the panel reports an unusable
102102
* gateway instead of surfacing a downstream TypeError as its only content. */
103103
export class TerminalOpenUnusableSessionError extends Error {
104-
constructor(field: string) {
104+
constructor(readonly field: string) {
105105
super(`terminal session response is missing ${field}`);
106106
this.name = "TerminalOpenUnusableSessionError";
107107
}

‎ui/src/components/terminal/terminal-panel-chrome.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import {
1717
} from "./terminal-panel-upload.ts";
1818

1919
type TerminalDock = Exclude<DockPanelPlacement, "left">;
20+
type TerminalPanelViewportParams = {
21+
activeId: string | null;
22+
connecting: boolean;
23+
error: { text: string; retry?: () => void } | null;
24+
uploadController: TerminalPanelUploadController;
25+
};
2026

2127
export function renderTerminalPanelToolbar(
2228
fullscreen: boolean,
@@ -62,14 +68,23 @@ export function renderTerminalPanelHeader(
6268
</header>`;
6369
}
6470

65-
export function renderTerminalPanelViewport(
66-
activeId: string | null,
67-
connecting: boolean,
68-
errorText: string | null,
69-
uploadController: TerminalPanelUploadController,
70-
): TemplateResult {
71+
export function renderTerminalPanelViewport({
72+
activeId,
73+
connecting,
74+
error,
75+
uploadController,
76+
}: TerminalPanelViewportParams): TemplateResult {
7177
return html`
72-
${errorText ? html`<div class="tp-error" role="alert">${errorText}</div>` : nothing}
78+
${error
79+
? html`<div class="tp-error" role="alert">
80+
<span>${error.text}</span>
81+
${error.retry
82+
? html`<button class="btn btn--sm" type="button" @click=${error.retry}>
83+
${t("common.retry")}
84+
</button>`
85+
: nothing}
86+
</div>`
87+
: nothing}
7388
<wa-tab-panel
7489
id="terminal-tab-panel"
7590
class="tp-viewport"
@@ -87,7 +102,7 @@ export function renderTerminalPanelViewport(
87102
<span>${t("terminal.connecting")}</span>
88103
</div>`
89104
: nothing}
90-
${!activeId && !connecting && !errorText
105+
${!activeId && !connecting && !error
91106
? renderPanelEmptyState({
92107
icon: icons.terminal,
93108
heading: t("chat.sidePanel.terminal"),
@@ -105,7 +120,7 @@ export function terminalOpenErrorText(error: unknown): string {
105120
return t("terminal.connectionTimedOut");
106121
}
107122
if (error instanceof TerminalOpenUnusableSessionError) {
108-
return t("terminal.unavailable");
123+
return t("terminal.unusableSession", { field: error.field });
109124
}
110125
return formatUiError(error);
111126
}

‎ui/src/components/terminal/terminal-panel-readiness.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,11 @@ describe("terminal panel readiness", () => {
281281
panel.available = true;
282282
(panel as unknown as { catalogReadyTimeoutMs: number }).catalogReadyTimeoutMs = 5;
283283
document.body.append(panel);
284+
const catalog = { catalogId: "anthropic", hostId: "node:mac", threadId: "thread" };
284285

285286
panel.handleToggleRequest(
286287
new CustomEvent("openclaw:terminal-toggle", {
287-
detail: { catalog: { catalogId: "anthropic", hostId: "node:mac", threadId: "thread" } },
288+
detail: { catalog },
288289
}),
289290
);
290291

@@ -298,5 +299,15 @@ describe("terminal panel readiness", () => {
298299
params: { sessionId: "catalog-terminal-1" },
299300
});
300301
expect(panel.renderRoot.querySelector(".tabstrip-tab")).toBeNull();
302+
const retry = panel.renderRoot.querySelector<HTMLButtonElement>(".tp-error button");
303+
expect(retry?.textContent?.trim()).toBe("Retry");
304+
305+
retry?.click();
306+
await waitForFast(() => {
307+
expect(requests.filter((request) => request.method === "terminal.open")).toHaveLength(2);
308+
});
309+
expect(requests.findLast((request) => request.method === "terminal.open")?.params).toEqual(
310+
expect.objectContaining({ catalog }),
311+
);
301312
});
302313
});

‎ui/src/components/terminal/terminal-panel-session-controller.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
type TerminalPanelSessionControllerState,
2424
type TerminalPanelSessionTab,
2525
} from "./terminal-panel-session-types.ts";
26-
import { terminalIntentQueue, type TerminalIntentHost } from "./terminal-pending-actions.ts";
26+
import { TerminalOpenRetry, terminalIntentQueue } from "./terminal-pending-actions.ts";
27+
import type { TerminalIntentHost } from "./terminal-pending-actions.ts";
2728
import {
2829
loadPersistedTerminalSessionIds,
2930
persistLiveTerminalSessions,
@@ -33,8 +34,6 @@ import { TerminalTabReadinessController } from "./terminal-tab-readiness.ts";
3334
import { TerminalTaskQueue } from "./terminal-task-queue.ts";
3435
import { terminalDynamicColors, terminalTheme } from "./terminal-theme.ts";
3536

36-
type TerminalSessionSink = Parameters<TerminalConnection["open"]>[1];
37-
3837
/** Owns gateway PTY sessions and the Ghostty controllers bound to them. */
3938
export class TerminalPanelSessionController
4039
implements ReactiveController, TerminalPanelSessionControllerState
@@ -52,6 +51,7 @@ export class TerminalPanelSessionController
5251
private lifecycleAbortController = new AbortController();
5352
private lifecycleSyncToken = 0;
5453
private tabSequence = 0;
54+
readonly openRetry = new TerminalOpenRetry();
5555
private readonly bootQueue = new TerminalTaskQueue();
5656
private readonly intentHost: TerminalIntentHost;
5757
private readonly readiness: TerminalTabReadinessController<TerminalPanelSessionTab>;
@@ -70,17 +70,14 @@ export class TerminalPanelSessionController
7070
requestUpdate: () => this.host.requestUpdate(),
7171
setBooting: (booting) => this.updateControllerState("booting", booting),
7272
timeoutMs: () => this.host.catalogReadyTimeoutMs,
73-
showTimeout: () => {
74-
this.host.terminalPanelErrorText = t("terminal.refreshRequired");
75-
},
76-
clearTimeout: () => {
77-
this.host.terminalPanelErrorText = null;
78-
},
73+
showTimeout: () => (this.host.terminalPanelErrorText = t("terminal.refreshRequired")),
74+
clearTimeout: () => (this.host.terminalPanelErrorText = null),
7975
};
8076
this.readiness = new TerminalTabReadinessController<TerminalPanelSessionTab>({
8177
timeoutMs: () => this.host.catalogReadyTimeoutMs,
8278
isCurrent: (tab) => this.tabs.includes(tab),
8379
onReady: () => {
80+
this.openRetry.clear();
8481
this.updateControllerState("tabs", [...this.tabs]);
8582
persistLiveTerminalSessions(this.tabs);
8683
},
@@ -318,6 +315,7 @@ export class TerminalPanelSessionController
318315
return false;
319316
}
320317
this.updateControllerState("booting", true);
318+
this.openRetry.clear();
321319
this.host.terminalPanelErrorText = null;
322320
try {
323321
const attached = await this.attachSession(sessionId, operation, agentOwned);
@@ -413,7 +411,7 @@ export class TerminalPanelSessionController
413411
readyTimer: null,
414412
};
415413
tabReference.current = tab;
416-
const sink: TerminalSessionSink = {
414+
const sink: Parameters<TerminalConnection["open"]>[1] = {
417415
// The cancelled guard also protects the buffered-event replay inside
418416
// connection.open/attach from writing to an already-disposed terminal.
419417
onData: (data: string) => {
@@ -520,6 +518,7 @@ export class TerminalPanelSessionController
520518
return false;
521519
}
522520
this.updateControllerState("booting", true);
521+
this.openRetry.remember(catalog, agentId);
523522
this.host.terminalPanelErrorText = null;
524523
// Freeze the selection for this tab; later agent changes affect only new tabs.
525524
const ownerAgentId = agentId ?? undefined;
@@ -561,6 +560,7 @@ export class TerminalPanelSessionController
561560
if (!this.isTerminalOperationCurrent(operation)) {
562561
return false;
563562
}
563+
this.openRetry.clearUnlessRetryable(error);
564564
this.host.terminalPanelErrorText = terminalOpenErrorText(error);
565565
return true;
566566
} finally {
@@ -766,6 +766,7 @@ export class TerminalPanelSessionController
766766
this.lifecycleAbortController.abort();
767767
this.lifecycleAbortController = new AbortController();
768768
this.bootQueue.reset();
769+
this.openRetry.clear();
769770
this.updateControllerState("booting", false);
770771
this.host.terminalPanelUploadController.dispose();
771772
this.host.clearTerminalPanelResizeListeners();

‎ui/src/components/terminal/terminal-panel-styles.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,23 @@ export const terminalPanelStyles = css`
182182
animation: tp-spin 0.8s linear infinite;
183183
}
184184
.tp-error {
185+
display: flex;
186+
align-items: center;
187+
justify-content: space-between;
188+
gap: 12px;
185189
padding: 10px 12px;
186190
font-size: 12px;
187191
color: var(--danger, #ff6b6b);
188192
}
193+
.tp-error .btn {
194+
flex: 0 0 auto;
195+
border: 1px solid var(--border);
196+
border-radius: var(--radius-md);
197+
background: var(--bg-elevated);
198+
color: var(--text);
199+
padding: 6px 10px;
200+
font: inherit;
201+
}
189202
@keyframes tp-spin {
190203
to {
191204
transform: rotate(360deg);

‎ui/src/components/terminal/terminal-panel.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ export class OpenClawTerminalPanel extends OpenClawLitElement {
378378
return this.renderRoot.querySelector(".tp-viewport");
379379
}
380380

381+
private retryTerminalOpen(): void {
382+
this.terminalPanelErrorText = null;
383+
this.terminalSessions.openRetry.run();
384+
}
385+
381386
override render() {
382387
if (
383388
!this.available ||
@@ -400,6 +405,14 @@ export class OpenClawTerminalPanel extends OpenClawLitElement {
400405
this.terminalSessions.waitingForRefresh ||
401406
(this.terminalSessions.booting && this.terminalSessions.tabs.length === 0) ||
402407
activeTab?.status === "connecting";
408+
const terminalError = this.terminalPanelErrorText
409+
? {
410+
text: this.terminalPanelErrorText,
411+
retry: this.terminalSessions.openRetry.available
412+
? () => this.retryTerminalOpen()
413+
: undefined,
414+
}
415+
: null;
403416
const sessionPicker = renderTerminalSessionPicker({
404417
open: this.sessionPickerOpen,
405418
loading: this.sessionPickerTask.status === TaskStatus.PENDING,
@@ -443,12 +456,12 @@ export class OpenClawTerminalPanel extends OpenClawLitElement {
443456
},
444457
() => void this.terminalSessions.openSession(),
445458
)}
446-
${renderTerminalPanelViewport(
447-
this.terminalSessions.activeId,
459+
${renderTerminalPanelViewport({
460+
activeId: this.terminalSessions.activeId,
448461
connecting,
449-
this.terminalPanelErrorText,
450-
this.terminalPanelUploadController,
451-
)}
462+
error: terminalError,
463+
uploadController: this.terminalPanelUploadController,
464+
})}
452465
</section>
453466
`;
454467
}

‎ui/src/components/terminal/terminal-pending-actions.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { TerminalPanelToggleDetail } from "../panel-toggle-contract.ts";
2+
import {
3+
TerminalOpenTimeoutError,
4+
TerminalOpenUnusableSessionError,
5+
} from "./terminal-connection.ts";
26
import type {
37
TerminalPanelAction,
48
TerminalPanelCatalogReference,
@@ -9,6 +13,44 @@ import {
913
} from "./terminal-session-storage.ts";
1014
import type { TerminalTaskQueue } from "./terminal-task-queue.ts";
1115

16+
type RetryOpenAction = Extract<TerminalPanelAction, { kind: "catalog" | "open" }>;
17+
18+
/** Retains the exact failed open intent until the operator retries or the tab becomes ready. */
19+
export class TerminalOpenRetry {
20+
private action: RetryOpenAction | null = null;
21+
22+
remember(catalog: TerminalPanelCatalogReference | undefined, agentId: string | null): void {
23+
this.action = catalog ? { kind: "catalog", agentId, catalog } : { kind: "open", agentId };
24+
}
25+
26+
clearUnlessRetryable(error: unknown): void {
27+
if (
28+
!(
29+
error instanceof TerminalOpenTimeoutError ||
30+
error instanceof TerminalOpenUnusableSessionError
31+
)
32+
) {
33+
this.clear();
34+
}
35+
}
36+
37+
clear(): void {
38+
this.action = null;
39+
}
40+
41+
get available(): boolean {
42+
return this.action !== null;
43+
}
44+
45+
run(): void {
46+
const action = this.action;
47+
this.clear();
48+
if (action) {
49+
void terminalIntentQueue.queue(action);
50+
}
51+
}
52+
}
53+
1254
export type TerminalIntentHost = {
1355
bootQueue: Pick<TerminalTaskQueue, "enqueue">;
1456
currentGeneration: () => number;

0 commit comments

Comments
 (0)