The best way to control Claude Code, Codex and more from any computer or phone
fix(cli): restore Windows desktop launcher console input (#24870)
Co-authored-by: Nathan Shan <nathanielcrush51@gmail.com> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: Utkarsh Patil <73941998+UtkarshUsername@users.noreply.github.com>
t3-code-bot[bot] committed
Oct 29, 2026 at 16:01 UTC
960d5761c13f68a2f10635bcd26489a5fa404ec1
2 files changed
+148
-10
apps/server/src/cli/consoleStdin.test.ts
new
+127
@@ -0,0 +1,127 @@
1
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
+
3
+const io = vi.hoisted(() => ({
4
+ platform: vi.fn(() => "win32"),
5
+ stat: vi.fn(() => ({ isCharacterDevice: (): boolean => true })),
6
+ open: vi.fn(() => 42),
7
+ close: vi.fn(),
8
+ isatty: vi.fn((fd: number) => fd !== 0),
9
+}));
10
+
11
+vi.mock("node:fs", () => ({ fstatSync: io.stat, openSync: io.open, closeSync: io.close }));
12
+vi.mock("node:tty", async () => {
13
+ const { EventEmitter } = await import("node:events");
14
+ return {
15
+ isatty: io.isatty,
16
+ ReadStream: class extends EventEmitter {
17
+ readonly isTTY = true;
18
+ readonly _readableState = { reading: true };
19
+ _handle: { reading: boolean; readStop: () => void } | undefined = {
20
+ reading: true,
21
+ readStop: vi.fn(),
22
+ };
23
+ },
24
+ };
25
+});
26
+vi.mock("@t3tools/shared/HostProcess", () => ({ Platform: { defaultValue: io.platform } }));
27
+
28
+const originalStdin = Object.getOwnPropertyDescriptor(process, "stdin")!;
29
+
30
+beforeEach(() => {
31
+ vi.resetModules();
32
+ vi.resetAllMocks();
33
+ vi.stubEnv("T3CODE_DESKTOP_CLI_LAUNCHER", "1");
34
+});
35
+
36
+afterEach(() => {
37
+ Object.defineProperty(process, "stdin", originalStdin);
38
+ vi.unstubAllEnvs();
39
+});
40
+
41
+describe("attachConsoleStdin", () => {
42
+ it("reopens the console when Node replaces missing Windows stdin with NUL", async () => {
43
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
44
+
45
+ expect(attachConsoleStdin()).toBe(42);
46
+ expect(process.stdin.isTTY).toBe(true);
47
+ expect(interactiveStdin()).toBe(42);
48
+ expect(process.env.T3CODE_DESKTOP_CLI_LAUNCHER).toBeUndefined();
49
+ });
50
+
51
+ it("releases console reads when the picker pauses stdin before launching an agent", async () => {
52
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
53
+ attachConsoleStdin();
54
+ const stdin = process.stdin as unknown as {
55
+ emit(event: string): boolean;
56
+ _readableState: { reading: boolean };
57
+ _handle: { reading: boolean; readStop: () => void };
58
+ };
59
+ stdin.emit("pause");
60
+ expect(stdin._readableState.reading).toBe(false);
61
+ expect(stdin._handle.reading).toBe(false);
62
+ expect(stdin._handle.readStop).toHaveBeenCalledOnce();
63
+ expect(interactiveStdin()).toBe(42);
64
+ });
65
+
66
+ it("can pause after the reopened stream's handle has been closed", async () => {
67
+ const { attachConsoleStdin } = await import("./consoleStdin.ts");
68
+ attachConsoleStdin();
69
+ const stdin = process.stdin as unknown as {
70
+ emit(event: string): boolean;
71
+ _handle: undefined;
72
+ };
73
+ stdin._handle = undefined;
74
+ expect(() => stdin.emit("pause")).not.toThrow();
75
+ });
76
+
77
+ it("still reopens a closed stdin", async () => {
78
+ io.stat.mockImplementation(() => {
79
+ throw new Error("EBADF");
80
+ });
81
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
82
+ expect(attachConsoleStdin()).toBe(42);
83
+ expect(interactiveStdin()).toBe(42);
84
+ });
85
+
86
+ it.each(["pipe", "file"])("preserves redirected %s input", async () => {
87
+ io.stat.mockReturnValue({ isCharacterDevice: () => false });
88
+ const stdin = process.stdin;
89
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
90
+ expect(attachConsoleStdin()).toBeUndefined();
91
+ expect(process.stdin).toBe(stdin);
92
+ expect(interactiveStdin()).toBe("inherit");
93
+ expect(io.open).not.toHaveBeenCalled();
94
+ });
95
+
96
+ it.each(["console input", "redirected output", "unmarked process", "non-Windows"])(
97
+ "leaves %s alone",
98
+ async (scenario) => {
99
+ if (scenario === "console input") io.isatty.mockReturnValue(true);
100
+ if (scenario === "redirected output") io.isatty.mockReturnValue(false);
101
+ if (scenario === "unmarked process") delete process.env.T3CODE_DESKTOP_CLI_LAUNCHER;
102
+ if (scenario === "non-Windows") io.platform.mockReturnValue("darwin");
103
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
104
+ expect(attachConsoleStdin()).toBeUndefined();
105
+ expect(interactiveStdin()).toBe("inherit");
106
+ expect(io.open).not.toHaveBeenCalled();
107
+ expect(process.env.T3CODE_DESKTOP_CLI_LAUNCHER).toBeUndefined();
108
+ },
109
+ );
110
+
111
+ it("keeps inherited input when CONIN$ cannot be opened", async () => {
112
+ io.open.mockImplementation(() => {
113
+ throw new Error("ENOENT");
114
+ });
115
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
116
+ expect(attachConsoleStdin()).toBeUndefined();
117
+ expect(interactiveStdin()).toBe("inherit");
118
+ });
119
+
120
+ it("closes a reopened descriptor that is not a console", async () => {
121
+ io.isatty.mockImplementation((fd) => fd === 1);
122
+ const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
123
+ expect(attachConsoleStdin()).toBeUndefined();
124
+ expect(interactiveStdin()).toBe("inherit");
125
+ expect(io.close).toHaveBeenCalledWith(42);
126
+ });
127
+});
apps/server/src/cli/consoleStdin.ts
+21
-10
@@ -5,31 +5,30 @@ import * as NodeTty from "node:tty";
5
import * as HostProcess from "@t3tools/shared/HostProcess";
6
7
/**
8
- * Electron's Windows executable is a GUI program, so `t3.cmd` under
9
- * `ELECTRON_RUN_AS_NODE` gets the console for stdout and stderr but no stdin.
10
- * Piped or redirected input and runs whose output is not a console are left alone.
8
+ * Electron's Windows launcher has no stdin; Node substitutes a non-console NUL device.
9
+ * Preserve pipes, files, console input, and runs without console output.
10
+ * Explicit `< NUL` is indistinguishable from missing launcher stdin and also reattaches.
11
*/
12
function needsConsoleStdin(input: {
13
readonly platform: NodeJS.Platform;
14
readonly env: NodeJS.ProcessEnv;
15
- readonly stdinOpen: boolean;
15
+ readonly stdinMissing: boolean;
16
readonly stdoutIsConsole: boolean;
17
}): boolean {
18
return (
19
input.platform === "win32" &&
20
// Set only by the launcher (DesktopCliShim), never for the desktop backend.
21
input.env.T3CODE_DESKTOP_CLI_LAUNCHER === "1" &&
22
- !input.stdinOpen &&
22
+ input.stdinMissing &&
23
input.stdoutIsConsole
24
);
25
}
26
27
-const isOpen = (fd: number) => {
27
+const isMissingInput = (fd: number) => {
28
try {
29
- NodeFS.fstatSync(fd);
30
- return true;
29
+ return NodeFS.fstatSync(fd).isCharacterDevice() && !NodeTty.isatty(fd);
30
} catch {
32
- return false;
31
+ return true;
32
}
33
};
34
@@ -38,7 +37,7 @@ export function attachConsoleStdin(): number | undefined {
37
const needed = needsConsoleStdin({
38
platform: HostProcess.Platform.defaultValue(),
39
env: process.env,
41
- stdinOpen: isOpen(0),
40
+ stdinMissing: isMissingInput(0),
41
stdoutIsConsole: NodeTty.isatty(1),
42
});
43
// The launcher marker must not propagate to child shells or t3 processes.
@@ -55,6 +54,18 @@ export function attachConsoleStdin(): number | undefined {
54
return undefined;
55
}
56
const stdin = new NodeTty.ReadStream(fd);
57
+ // Like Node's built-in stdin, release pending console reads before a child takes over.
58
+ stdin.on("pause", () => {
59
+ const internal = stdin as unknown as {
60
+ _handle?: { reading: boolean; readStop(): void };
61
+ _readableState: { reading: boolean };
62
+ };
63
+ const handle = internal._handle;
64
+ if (!handle) return;
65
+ internal._readableState.reading = false;
66
+ handle.reading = false;
67
+ handle.readStop();
68
+ });
69
Object.defineProperty(process, "stdin", { value: stdin, configurable: true, enumerable: true });
70
consoleStdinFd = fd;
71
return fd;