Skip to content

Commit ab785b4

Browse files
fix(web): terminal shows attach errors with unchanged output; Enter searches themes once
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4fdfc6e commit ab785b4

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎apps/web/src/components/ThreadTerminalDrawer.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
shouldClearTerminalSelectionAction,
1212
shouldHandleTerminalExit,
1313
synchronizeTerminalOutput,
14+
synchronizeTerminalSession,
1415
} from "./ThreadTerminalDrawer";
1516

1617
describe("terminal selection actions", () => {
@@ -87,3 +88,16 @@ it("retains visible output and selection until a replacement subscription receiv
8788
synchronizeTerminalOutput(terminal, cleared, observedCursor);
8889
expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("");
8990
});
91+
92+
it("reports an attach failure that leaves the output unchanged", () => {
93+
const terminal = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() };
94+
const previous = { ...EMPTY_TERMINAL_BUFFER_STATE, error: null };
95+
const failed = { ...previous, error: "Terminal attach failed" };
96+
97+
expect(
98+
synchronizeTerminalSession(terminal, previous, failed, INITIAL_TERMINAL_OUTPUT_CURSOR),
99+
).toBe(INITIAL_TERMINAL_OUTPUT_CURSOR);
100+
expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n");
101+
expect(terminal.resetAndWrite).not.toHaveBeenCalled();
102+
expect(terminal.clearSelection).not.toHaveBeenCalled();
103+
});

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ function readDrawerLayout(column: HTMLElement | null): Omit<DrawerLayout, "compo
146146
};
147147
}
148148

149-
function writeSystemMessage(terminal: GhosttyTerminalSurface, message: string): void {
149+
function writeSystemMessage(
150+
terminal: Pick<GhosttyTerminalSurface, "write">,
151+
message: string,
152+
): void {
150153
terminal.write(`\r\n[terminal] ${message}\r\n`);
151154
}
152155

@@ -173,6 +176,24 @@ export function synchronizeTerminalOutput(
173176
return update.cursor;
174177
}
175178

179+
/** Writes what changed between two session notifications: new output, then a new error. */
180+
export function synchronizeTerminalSession(
181+
terminal: Pick<GhosttyTerminalSurface, "resetAndWrite" | "write" | "clearSelection">,
182+
previous: Pick<TerminalSessionState, "output" | "version" | "error">,
183+
session: Pick<TerminalSessionState, "output" | "version" | "error">,
184+
cursor: TerminalOutputCursor,
185+
): TerminalOutputCursor {
186+
const nextCursor =
187+
session.version === previous.version && session.output === previous.output
188+
? cursor
189+
: synchronizeTerminalOutput(terminal, session, cursor);
190+
// An attach failure can change only the error, keeping the last buffer.
191+
if (session.error !== null && session.error !== previous.error) {
192+
writeSystemMessage(terminal, session.error);
193+
}
194+
return nextCursor;
195+
}
196+
176197
function parseTerminalColor(value: string, fallback: GhosttyColor): GhosttyColor {
177198
if (typeof document === "undefined") return fallback;
178199

@@ -691,19 +712,14 @@ function TerminalSessionViewport({
691712
if (nextResizeGeneration !== null) terminal.resendSize();
692713
}
693714
const previous = previousSession;
715+
previousSession = session;
694716
synchronizeTerminalStatus(terminal, session.status, session.version);
695-
if (session.version === previous.version && session.output === previous.output) {
696-
return;
697-
}
698-
outputCursorRef.current = synchronizeTerminalOutput(
717+
outputCursorRef.current = synchronizeTerminalSession(
699718
terminal,
719+
previous,
700720
session,
701721
outputCursorRef.current,
702722
);
703-
if (session.error !== null && session.error !== previous.error) {
704-
writeSystemMessage(terminal, session.error);
705-
}
706-
previousSession = session;
707723
}),
708724
);
709725
if (

‎apps/web/src/components/settings/ThemeSearchSection.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ describe("createThemeSearch", () => {
128128
expect(env.view.error).toBeNull();
129129
});
130130

131+
it("Enter during the debounce searches once", async () => {
132+
using env = setup();
133+
env.themeSearch.setQuery("nord");
134+
env.themeSearch.searchNow();
135+
await vi.advanceTimersByTimeAsync(350);
136+
expect(env.requests.map((request) => request.query)).toEqual(["nord"]);
137+
expect(env.requests[0]!.signal?.aborted).toBe(false);
138+
});
139+
131140
it("closing the session aborts the search, the debounce, and the install", async () => {
132141
using env = setup();
133142
env.themeSearch.setQuery("nord");

‎apps/web/src/components/settings/ThemeSearchSection.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ export function createThemeSearch(
201201
/** Enter or re-picking the current suggestion searches again even for the shown key. */
202202
searchNow() {
203203
if (install !== null || signal.aborted || query === "") return;
204+
// A pending debounce for the same key would otherwise restart this search.
205+
cancelDebounce();
206+
handledKey = keyOf();
207+
interrupted = false;
204208
view.setError(null);
205209
view.setIsSearching(true);
206210
fetchResults();

0 commit comments

Comments
 (0)