Fix coding-agent terminal shortcuts not working on Windows (#1509)

* Fix coding-agent terminal shortcuts not working on Windows

On Windows, opening a Terminal profile (Claude Code / Codex / OpenCode) did
nothing, and afterwards even a plain New Terminal stopped working until restart.

Three Windows-specific problems combined:

- A failed node-pty conpty spawn completes asynchronously on its conout worker
  thread; the uncaught exception escaped the per-request try/catch and crashed
  the whole terminal worker, severing every terminal. Add uncaughtException /
  unhandledRejection guards so a single bad spawn keeps the worker alive.
- Profile commands were passed to conpty unresolved. conpty's CreateProcess
  ignores PATHEXT, so bare codex (npm codex.cmd) wasn't found and .cmd/.bat
  shims can't run directly. Add resolveTerminalSpawnCommand() to resolve the
  real path and route .cmd/.bat through cmd.exe /c on Windows.
- winget-installed CLIs (e.g. Claude Code) live under the winget Packages dir
  and aren't on PATH. findExecutable() now falls back to those known install
  locations, so all providers and terminal profiles benefit.

Also surface a failed terminal create to the user via a toast instead of
silently dropping the error.

* Address review: simplify winget scan, narrow worker exception guard

- Drop the single-element WINGET_PACKAGE_BIN_SUBDIRS loop in executable.ts and
  map package dirs directly to the root <name>.exe candidate.
- Remove the unhandledRejection handler in the terminal worker (only the
  uncaughtException path is exercised by conpty's async spawn failure) and
  expand the comment to explain the keep-alive trade-off and the absence of a
  worker restart path.

* Refactor executable resolution out of utils

* Serialize terminal create requests in worker

---------

Co-authored-by: danniel <liminfhu@gmail.com>
Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
This commit is contained in:
itMrBoy
2026-06-14 19:48:42 +08:00
committed by GitHub
co-authored by danniel Mohamed Boudra
parent bf8f2510c8
commit f352072dac
22 changed files with 385 additions and 55 deletions
@@ -9,11 +9,33 @@ import type {
WorkerTerminalInfo,
} from "./terminal-worker-protocol.js";
type TerminalCreateRequest = Extract<TerminalWorkerRequest, { type: "createTerminal" }>;
const manager = createTerminalManager();
const unsubscribeByTerminalId = new Map<string, Array<() => void>>();
const outputCoalescerByTerminalId = new Map<string, TerminalOutputCoalescer>();
let ipcClosing = false;
interface InFlightTerminalCreateRequest {
requestId: string;
errorReported: boolean;
}
let inFlightTerminalCreateRequest: InFlightTerminalCreateRequest | null = null;
// The conpty failure signal is process-scoped, not request-scoped. Serializing
// creates keeps an async spawn failure attributable to exactly one request.
let createTerminalQueue: Promise<void> = Promise.resolve();
// node-pty completes its Windows conpty spawn asynchronously on a separate
// conout worker thread. When that spawn fails (bad cwd, missing command, etc.)
// it throws an exception there that cannot be caught at the call site and would
// otherwise crash this worker process and sever every existing terminal.
process.on("uncaughtException", (error) => {
console.error("Terminal worker uncaught exception (kept alive):", error);
reportInFlightTerminalCreateFailure(error);
});
function sendToParent(message: TerminalWorkerToParentMessage): void {
if (ipcClosing || !process.connected || !process.send) {
return;
@@ -48,6 +70,23 @@ function toTerminalInfo(session: TerminalSession): WorkerTerminalInfo {
};
}
function terminalWorkerErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : "Terminal worker request failed";
}
function reportInFlightTerminalCreateFailure(error: unknown): void {
if (!inFlightTerminalCreateRequest || inFlightTerminalCreateRequest.errorReported) {
return;
}
inFlightTerminalCreateRequest.errorReported = true;
sendToParent({
type: "response",
requestId: inFlightTerminalCreateRequest.requestId,
ok: false,
error: terminalWorkerErrorMessage(error),
});
}
function clearTerminalSubscriptions(terminalId: string): void {
const subscriptions = unsubscribeByTerminalId.get(terminalId);
if (subscriptions) {
@@ -146,6 +185,49 @@ manager.subscribeTerminalsChanged((event) => {
});
});
function enqueueCreateTerminalRequest(message: TerminalCreateRequest): Promise<void> {
const nextRequest = createTerminalQueue.then(() => handleCreateTerminalRequest(message));
createTerminalQueue = nextRequest.catch(() => {});
return nextRequest;
}
async function handleCreateTerminalRequest(message: TerminalCreateRequest): Promise<void> {
const request: InFlightTerminalCreateRequest = {
requestId: message.requestId,
errorReported: false,
};
inFlightTerminalCreateRequest = request;
try {
const session = await manager.createTerminal(message.options);
if (request.errorReported) {
session.kill();
return;
}
watchTerminal(session);
const initialSnapshot = session.getStateSnapshot();
sendToParent({
type: "terminalCreated",
terminal: toTerminalInfo(session),
state: initialSnapshot.state,
});
sendToParent({
type: "response",
requestId: message.requestId,
ok: true,
result: {
terminal: toTerminalInfo(session),
state: initialSnapshot.state,
},
});
} catch (error) {
reportInFlightTerminalCreateFailure(error);
} finally {
if (inFlightTerminalCreateRequest === request) {
inFlightTerminalCreateRequest = null;
}
}
}
async function handleRequest(message: TerminalWorkerRequest): Promise<void> {
switch (message.type) {
case "getTerminals": {
@@ -160,23 +242,7 @@ async function handleRequest(message: TerminalWorkerRequest): Promise<void> {
}
case "createTerminal": {
const session = await manager.createTerminal(message.options);
watchTerminal(session);
const initialSnapshot = session.getStateSnapshot();
sendToParent({
type: "terminalCreated",
terminal: toTerminalInfo(session),
state: initialSnapshot.state,
});
sendToParent({
type: "response",
requestId: message.requestId,
ok: true,
result: {
terminal: toTerminalInfo(session),
state: initialSnapshot.state,
},
});
await enqueueCreateTerminalRequest(message);
return;
}
@@ -285,7 +351,7 @@ process.on("message", (message: TerminalWorkerRequest) => {
type: "response",
requestId: message.requestId,
ok: false,
error: error instanceof Error ? error.message : "Terminal worker request failed",
error: terminalWorkerErrorMessage(error),
});
});
});