Skip to content
TypeScript

fix(cli): t3 triage and prompts work from the desktop app's t3 on Windows (#20162)

Julius Marmingec0de6bd1b14b
diff --git a/apps/desktop/src/app/DesktopCliShim.ts b/apps/desktop/src/app/DesktopCliShim.ts
index c90e55a88d9..a9d9f04c4e8 100644
--- a/apps/desktop/src/app/DesktopCliShim.ts
+++ b/apps/desktop/src/app/DesktopCliShim.ts
@@ -60,6 +60,8 @@ export const renderCliShim = (input: {
`set "T3CODE_CLI_PATH=${cmdText(input.shimPath)}"`,
`if not defined T3CODE_HOME set "T3CODE_HOME=${cmdText(input.t3Home)}"`,
'set "ELECTRON_RUN_AS_NODE=1"',
+ // Electron's Windows executable starts without stdin; this tells the CLI to reopen the console.
+ 'set "T3CODE_DESKTOP_CLI_LAUNCHER=1"',
// A goto, not a parenthesized block: "Program Files (x86)" would close the block early.
`if exist ${cmdWord(target.executable)} goto run`,
...restore,
diff --git a/apps/server/src/bin.ts b/apps/server/src/bin.ts
index e7b9b38a956..4e01009c77c 100644
--- a/apps/server/src/bin.ts
+++ b/apps/server/src/bin.ts
@@ -18,6 +18,10 @@ if (
const { runAcpMcpCliFastPath } = await import("./mcp/AcpMcpStdioBridge.ts");
await runAcpMcpCliFastPath(command, process.argv.slice(3));
} else {
+ // Before anything reads stdin: the desktop app's Windows launcher starts
+ // the CLI without one. Never for the fast paths, whose stdin is a pipe.
+ const { attachConsoleStdin } = await import("./cli/consoleStdin.ts");
+ attachConsoleStdin();
const { runCli } = await import("./binCli.ts");
runCli();
}
diff --git a/apps/server/src/cli/consoleStdin.test.ts b/apps/server/src/cli/consoleStdin.test.ts
new file mode 100644
index 00000000000..7770fd67d7b
--- /dev/null
+++ b/apps/server/src/cli/consoleStdin.test.ts
@@ -0,0 +1,33 @@
+import { describe, expect, it } from "vite-plus/test";
+
+import { needsConsoleStdin } from "./consoleStdin.ts";
+
+const launcher = {
+ platform: "win32" as const,
+ env: { T3CODE_DESKTOP_CLI_LAUNCHER: "1" },
+ stdinOpen: false,
+ stdoutIsConsole: true,
+};
+
+describe("needsConsoleStdin", () => {
+ it("reopens the console for the desktop launcher in a terminal without stdin", () => {
+ expect(needsConsoleStdin(launcher)).toBe(true);
+ });
+
+ it("leaves every other way of running the CLI alone", () => {
+ for (const other of [
+ // Input the user piped or redirected.
+ { ...launcher, stdinOpen: true },
+ // Output to a pipe or file: the desktop backend, scripts, CI.
+ { ...launcher, stdoutIsConsole: false },
+ // The desktop backend runs the same executable but is not the launcher.
+ { ...launcher, env: { ELECTRON_RUN_AS_NODE: "1", T3CODE_CLI_PATH: "C:\\t3.cmd" } },
+ // npm, the install scripts, and plain Node runs.
+ { ...launcher, env: {} },
+ { ...launcher, platform: "linux" as const },
+ { ...launcher, platform: "darwin" as const },
+ ]) {
+ expect(needsConsoleStdin(other)).toBe(false);
+ }
+ });
+});
diff --git a/apps/server/src/cli/consoleStdin.ts b/apps/server/src/cli/consoleStdin.ts
new file mode 100644
index 00000000000..240ce8b6567
--- /dev/null
+++ b/apps/server/src/cli/consoleStdin.ts
@@ -0,0 +1,77 @@
+// @effect-diagnostics nodeBuiltinImport:off - Runs before the Effect runtime starts, on raw file descriptors.
+import * as NodeFS from "node:fs";
+import * as NodeTty from "node:tty";
+
+import * as HostProcess from "@t3tools/shared/HostProcess";
+
+/**
+ * Whether this process is the desktop app's `t3.cmd` running the CLI in a
+ * terminal with no stdin. The launcher runs the app's executable with
+ * `ELECTRON_RUN_AS_NODE`, and Electron's Windows executable is a GUI program:
+ * on start it points stdout and stderr at the parent's console but leaves
+ * stdin unset, so prompts see no terminal and children such as the agent
+ * `t3 triage` starts inherit nothing to read from. Input the user piped or
+ * redirected is a valid descriptor and is left alone, as is any run whose
+ * output is not a console (the desktop backend, scripts).
+ */
+export function needsConsoleStdin(input: {
+ readonly platform: NodeJS.Platform;
+ readonly env: NodeJS.ProcessEnv;
+ readonly stdinOpen: boolean;
+ readonly stdoutIsConsole: boolean;
+}): boolean {
+ return (
+ input.platform === "win32" &&
+ // Set only by the launcher (DesktopCliShim), never for the desktop backend.
+ input.env.T3CODE_DESKTOP_CLI_LAUNCHER === "1" &&
+ !input.stdinOpen &&
+ input.stdoutIsConsole
+ );
+}
+
+const isOpen = (fd: number) => {
+ try {
+ NodeFS.fstatSync(fd);
+ return true;
+ } catch {
+ return false;
+ }
+};
+
+/**
+ * Reopens the console's input as `process.stdin` when {@link needsConsoleStdin}
+ * holds, and returns its descriptor so interactive children can be given the
+ * same console. Returns undefined, changing nothing, everywhere else.
+ * Call before anything reads `process.stdin`.
+ */
+export function attachConsoleStdin(): number | undefined {
+ const needed = needsConsoleStdin({
+ platform: HostProcess.Platform.defaultValue(),
+ env: process.env,
+ stdinOpen: isOpen(0),
+ stdoutIsConsole: NodeTty.isatty(1),
+ });
+ // The marker describes this process's own start; a server started here
+ // must not pass it on to the shells and `t3` runs it spawns.
+ delete process.env.T3CODE_DESKTOP_CLI_LAUNCHER;
+ if (!needed) return undefined;
+ let fd: number;
+ try {
+ fd = NodeFS.openSync("\\\\.\\CONIN$", "r+");
+ } catch {
+ return undefined;
+ }
+ if (!NodeTty.isatty(fd)) {
+ NodeFS.closeSync(fd);
+ return undefined;
+ }
+ const stdin = new NodeTty.ReadStream(fd);
+ Object.defineProperty(process, "stdin", { value: stdin, configurable: true, enumerable: true });
+ consoleStdinFd = fd;
+ return fd;
+}
+
+let consoleStdinFd: number | undefined;
+
+/** The stdin to hand an interactive child: the reopened console, or the inherited one. */
+export const interactiveStdin = (): number | "inherit" => consoleStdinFd ?? "inherit";
diff --git a/apps/server/src/cli/triage.ts b/apps/server/src/cli/triage.ts
index 8403f650fc4..086e6cdd9a7 100644
--- a/apps/server/src/cli/triage.ts
+++ b/apps/server/src/cli/triage.ts
@@ -21,6 +21,7 @@ import * as ServerConfig from "../config.ts";
import { resolveBaseDir } from "../os-jank.ts";
import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
import { baseDirFlag } from "./config.ts";
+import { interactiveStdin } from "./consoleStdin.ts";
import { resolveCliCommand } from "./invocation.ts";
import {
buildTriageContext,
@@ -118,7 +119,9 @@ const runInteractiveSession = (input: {
Effect.callback<number, TriageAgentSpawnError>((resume) => {
const child = NodeChildProcess.spawn(input.command, [...input.args], {
cwd: input.cwd,
- stdio: "inherit",
+ // The agent reads the user's terminal; under the desktop app's Windows
+ // launcher that is the reopened console, not the (missing) inherited stdin.
+ stdio: [interactiveStdin(), "inherit", "inherit"],
shell: input.shell,
});
child.once("error", (cause) =>