diff --git a/apps/server/src/project/ProjectSetupScriptRunner.ts b/apps/server/src/project/ProjectSetupScriptRunner.ts index a049bc89b01a1..19452ac310d0c 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.ts @@ -19,6 +19,7 @@ import * as Schema from "effect/Schema"; import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as TerminalManager from "../terminal/Manager.ts"; +import { watchShellReadiness as watchTerminalShellReadiness } from "../terminal/shellReadiness.ts"; import * as ProjectService from "./ProjectService.ts"; export interface ProjectSetupScriptRunnerResultNoScript { @@ -154,18 +155,6 @@ function stripTerminalControl(text: string): string { .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ""); } -// Line editors (zsh, fish, nu, bash 5.1+) enable bracketed paste when they start reading a command. -const BRACKETED_PASTE_ON = "\x1b[?2004h"; -const BRACKETED_PASTE_OFF = "\x1b[?2004l"; -// Longer than one subprocess poll plus its ps timeout, so a startup prompt is seen as busy first. -const SHELL_QUIET_FALLBACK = "3 seconds"; - -function lineEditorReading(text: string, previous: boolean): boolean { - const on = text.lastIndexOf(BRACKETED_PASTE_ON); - const off = text.lastIndexOf(BRACKETED_PASTE_OFF); - return on === off ? previous : on > off; -} - type CompletionShell = "posix" | "fish" | "nushell" | "powershell"; function resolveCompletionShell(shell: string): CompletionShell | null { @@ -312,54 +301,16 @@ export const make = Effect.gen(function* () { }, ); - // Input typed during shell startup can be read by an rc-file prompt (fnm, nvm) instead of the - // shell, which loses the command. Subscribe before the terminal opens so no activity is missed. const watchShellReadiness = Effect.fn("ProjectSetupScriptRunner.watchShellReadiness")( function* (input: { readonly threadId: string; readonly terminalId: string; readonly onStartupPrompt: Effect.Effect; }) { - const outcome = yield* Deferred.make<"ready" | "exited">(); - let reading = false; - let carry = ""; - let busy = false; - let activity = 0; - const unsubscribe = yield* terminalManager.subscribe((event) => { - if (event.threadId !== input.threadId || event.terminalId !== input.terminalId) { - return Effect.void; - } - activity += 1; - // A new shell process starts idle; the manager resets its subprocess state without an event. - if (event.type === "started" || event.type === "restarted") { - busy = false; - reading = false; - carry = ""; - } - if (event.type === "activity") busy = event.hasRunningSubprocess; - if (event.type === "output") { - const text = carry + event.data; - carry = text.slice(-(BRACKETED_PASTE_ON.length - 1)); - reading = lineEditorReading(text, reading); - } - if (event.type === "exited" || event.type === "closed") { - return Deferred.succeed(outcome, "exited").pipe(Effect.asVoid); - } - return reading ? Deferred.succeed(outcome, "ready").pipe(Effect.asVoid) : Effect.void; - }); - // A startup prompt runs as a child process, so a busy shell keeps waiting for the user. - const quiet = Effect.gen(function* () { - while (true) { - const seen = activity; - yield* Effect.sleep(SHELL_QUIET_FALLBACK); - if (activity !== seen) continue; - if (!busy) return "ready" as const; - yield* input.onStartupPrompt; - } + return yield* watchTerminalShellReadiness({ + ...input, + subscribe: terminalManager.subscribe, }); - // Restored history can hold a previous shell's line editor state, so only live output counts. - const wait = Deferred.await(outcome).pipe(Effect.raceFirst(quiet)); - return { wait, unsubscribe }; }, ); diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index eb27174d5ed6d..01ffd1b4cf277 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -73,6 +73,7 @@ class FakePtyProcess implements PtyAdapter.PtyProcess { killed = false; exitOnSubscribe: PtyAdapter.PtyExitEvent | undefined; onKill: ((signal?: string) => void) | undefined; + onWrite: ((data: string) => void) | undefined; constructor(pid: number) { this.pid = pid; @@ -83,6 +84,7 @@ class FakePtyProcess implements PtyAdapter.PtyProcess { throw this.writeFailure; } this.writes.push(data); + this.onWrite?.(data); } resize(cols: number, rows: number): void { @@ -1111,11 +1113,17 @@ it.layer( yield* Effect.addFinalizer(() => Effect.sync(unsubscribeOutput)); yield* manager.open(openInput({ terminalId: "typed" })); yield* manager.open(openInput({ terminalId: "echoed" })); - const [typed, echoed] = ptyAdapter.processes; + yield* manager.open(openInput({ terminalId: "held" })); + const [typed, echoed, held] = ptyAdapter.processes; duringCheck = (pid) => - pid === typed!.pid + pid === typed!.pid || pid === held!.pid ? manager - .write({ threadId: "thread-1", terminalId: "typed", data: "make build\r" }) + .write({ + threadId: "thread-1", + terminalId: pid === typed!.pid ? "typed" : "held", + data: "make build\r", + afterShellStartup: pid === held!.pid, + }) .pipe(Effect.orDie) : Effect.gen(function* () { echoed!.emitData("make build\r\n"); @@ -1124,7 +1132,8 @@ it.layer( yield* manager.closeIdle({ threadId: "thread-1" }); - expect(ptyAdapter.processes.map((process) => process.killed)).toEqual([false, false]); + expect(held!.writes).toEqual([]); + expect(ptyAdapter.processes.map((process) => process.killed)).toEqual([false, false, false]); }), ); @@ -2358,6 +2367,90 @@ it.layer( ), ); + it.effect("holds startup writes, in order, until a fresh shell reads input", () => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(); + const afterStartup = (data: string) => + manager.write({ + threadId: "thread-1", + terminalId: DEFAULT_TERMINAL_ID, + data, + afterShellStartup: true, + }); + yield* manager.open(openInput()); + const shell = ptyAdapter.processes[0]!; + const secondWritten = yield* Deferred.make(); + shell.onWrite = (data) => { + if (data === "SECOND\r") Deferred.doneUnsafe(secondWritten, Effect.void); + }; + + yield* afterStartup("FIRST\r"); + // A second action reopens the running terminal before typing. + yield* manager.open(openInput()); + yield* afterStartup("SECOND\r"); + expect(shell.writes).toEqual([]); + // Answering an rc-file prompt must not wait behind the held input. + yield* manager.write({ threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID, data: "y\r" }); + expect(shell.writes).toEqual(["y\r"]); + + shell.emitData("\x1b[?2004h$ "); + yield* Deferred.await(secondWritten); + expect(shell.writes).toEqual(["y\r", "FIRST\r", "SECOND\r"]); + + yield* afterStartup("THIRD\r"); + expect(shell.writes).toEqual(["y\r", "FIRST\r", "SECOND\r", "THIRD\r"]); + expect(ptyAdapter.spawnInputs).toHaveLength(1); + }), + ); + + it.effect("drops startup writes held for a shell that open replaced", () => + Effect.gen(function* () { + const { manager, ptyAdapter, baseDir } = yield* createManager(5, { processKillGraceMs: 0 }); + const path = yield* Path.Path; + const fs = yield* FileSystem.FileSystem; + const replacementCwd = path.join(baseDir, "replacement"); + yield* fs.makeDirectory(replacementCwd, { recursive: true }); + const afterStartup = (data: string) => + manager.write({ + threadId: "thread-1", + terminalId: DEFAULT_TERMINAL_ID, + data, + afterShellStartup: true, + }); + + yield* manager.open(openInput()); + yield* afterStartup("OLD\r"); + yield* manager.open(openInput({ cwd: replacementCwd })); + yield* afterStartup("NEW\r"); + const replacement = ptyAdapter.processes[1]!; + const written = yield* Deferred.make(); + replacement.onWrite = () => Deferred.doneUnsafe(written, Effect.void); + replacement.emitData("\x1b[?2004h$ "); + yield* Deferred.await(written); + + expect(ptyAdapter.processes.map((process) => process.writes)).toEqual([[], ["NEW\r"]]); + }), + ); + + it.effect("types a startup write into PowerShell without waiting for a line editor", () => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(5, { + env: { PATH: "C:\\Windows\\System32", SystemRoot: "C:\\Windows" }, + }).pipe(Effect.provide(layerWithHostPlatform("win32"))); + + yield* manager.open(openInput()); + yield* manager.write({ + threadId: "thread-1", + terminalId: DEFAULT_TERMINAL_ID, + data: "npm run dev\r", + afterShellStartup: true, + }); + + expect(ptyAdapter.spawnInputs[0]?.shell).toBe("pwsh.exe"); + expect(ptyAdapter.processes[0]?.writes).toEqual(["npm run dev\r"]); + }), + ); + it.effect("restarts a running terminal when the resolved provider environment changes", () => Effect.gen(function* () { const providerInstanceId = ProviderInstanceId.make("codex_work"); diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index dd06f5f516959..62a7c22ec12de 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -76,6 +76,7 @@ import * as ProcessRunner from "../processRunner.ts"; import * as PortScanner from "../preview/PortScanner.ts"; import * as NativeTelemetryClient from "../resourceTelemetry/NativeTelemetryClient.ts"; import { terminalOutputStream } from "./OutputProtocol.ts"; +import { watchShellReadiness } from "./shellReadiness.ts"; import * as PtyAdapter from "@t3tools/shared/PtyAdapter"; export { @@ -111,6 +112,12 @@ const CLOSE_IDLE_QUERY_TIMEOUT = { posix: "1 second", win32: "1500 millis" } as const QUIET_SUBPROCESS_SNAPSHOT_EVERY_TICKS = 30; const DEFAULT_PROCESS_KILL_GRACE_MS = 1_000; const DEFAULT_MAX_RETAINED_INACTIVE_SESSIONS = 128; +// PowerShell and cmd have no line-editor signal, so their input is written right away. +function shellSignalsLineEditor(shell: string): boolean { + const name = (shell.split(/[/\\]/).at(-1) ?? shell).toLowerCase().replace(/\.exe$/, ""); + return name !== "cmd" && name !== "pwsh" && name !== "powershell"; +} + const DEFAULT_OPEN_COLS = 120; const DEFAULT_OPEN_ROWS = 30; // The tmux/screen/Zellij keys name the multiplexer pane that launched the server, @@ -313,6 +320,8 @@ interface TerminalSessionState { childCommandLabel: string | null; runtimeEnv: Record | null; stopOnSettle: boolean; + /** Writes waiting for the current shell to read input; null once it takes them directly. */ + heldWrites: Array | null; } interface TerminalGrid { @@ -2437,6 +2446,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func session.hasRunningSubprocess = false; session.childCommandLabel = null; session.status = "exited"; + session.heldWrites = null; session.pendingHistoryControlSequence = ""; session.pendingProcessEvents = []; session.pendingProcessEventIndex = 0; @@ -2534,6 +2544,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* modifyManagerState((state) => { session.status = "starting"; session.shell = null; + session.heldWrites = null; session.cwd = input.cwd; session.worktreePath = input.worktreePath ?? null; session.cols = input.cols; @@ -2942,6 +2953,52 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func }).pipe(Effect.ignoreCause({ log: true })), ); + // Input typed while a shell starts can be read by an rc-file prompt (fnm, nvm) instead of the + // shell. Writes that ask to wait are held, in order, until the line editor reads input. + const startOpenedSession = Effect.fn("terminal.startOpenedSession")(function* ( + session: TerminalSessionState, + startInput: TerminalStartInput, + ) { + const target = { threadId: session.threadId, terminalId: session.terminalId }; + // Subscribed before the spawn so no startup output is missed. + const readiness = yield* watchShellReadiness({ + ...target, + subscribe, + onStartupPrompt: Effect.void, + }); + yield* startSession(session, startInput, "started").pipe( + Effect.onError(() => Effect.sync(readiness.unsubscribe)), + ); + const spawned = session.process; + if ( + session.status !== "running" || + spawned === null || + session.shell === null || + !shellSignalsLineEditor(session.shell) + ) { + readiness.unsubscribe(); + return snapshot(session); + } + const held: Array = []; + session.heldWrites = held; + yield* readiness.wait.pipe( + Effect.flatMap((outcome) => { + // A restart or exit already dropped the queue: the input belonged to the shell it was typed for. + if (session.heldWrites !== held) return Effect.void; + session.heldWrites = null; + return outcome === "exited" || session.process !== spawned + ? Effect.void + : Effect.forEach(held, (data) => write({ ...target, data }), { discard: true }); + }), + Effect.catchCause((cause) => + Effect.logWarning("terminal held input was not written", { ...target, cause }), + ), + Effect.ensuring(Effect.sync(readiness.unsubscribe)), + Effect.forkIn(workerScope), + ); + return snapshot(session); + }); + const openWithWorkspaceLease = Effect.fn("terminal.openLocked")(function* ( requestedInput: TerminalOpenInput, ) { @@ -2992,6 +3049,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func childCommandLabel: null, runtimeEnv: normalizedRuntimeEnv(input.env), stopOnSettle: input.stopOnSettle ?? false, + heldWrites: null, }; const createdSession = session; @@ -3003,17 +3061,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func }); yield* evictInactiveSessionsIfNeeded(); - yield* startSession( - session, - { - cwd: input.cwd, - ...(input.worktreePath !== undefined ? { worktreePath: input.worktreePath } : {}), - cols, - rows, - }, - "started", - ); - return snapshot(session); + return yield* startOpenedSession(session, { + cwd: input.cwd, + ...(input.worktreePath !== undefined ? { worktreePath: input.worktreePath } : {}), + cols, + rows, + }); } const liveSession = existing.value; @@ -3053,17 +3106,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func } if (!liveSession.process) { - yield* startSession( - liveSession, - { - cwd: input.cwd, - worktreePath: liveSession.worktreePath, - cols: targetCols, - rows: targetRows, - }, - "started", - ); - return snapshot(liveSession); + return yield* startOpenedSession(liveSession, { + cwd: input.cwd, + worktreePath: liveSession.worktreePath, + cols: targetCols, + rows: targetRows, + }); } if (liveSession.cols !== targetCols || liveSession.rows !== targetRows) { @@ -3467,6 +3515,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func }); } session.inputCount += 1; + if (input.afterShellStartup === true && session.heldWrites !== null) { + session.heldWrites.push(input.data); + return; + } yield* Effect.try({ try: () => process.write(input.data), catch: (cause) => diff --git a/apps/server/src/terminal/shellReadiness.ts b/apps/server/src/terminal/shellReadiness.ts new file mode 100644 index 0000000000000..88380c46bd9b6 --- /dev/null +++ b/apps/server/src/terminal/shellReadiness.ts @@ -0,0 +1,69 @@ +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; + +import type { TerminalEvent } from "@t3tools/contracts"; + +// Line editors (zsh, fish, nu, bash 5.1+) enable bracketed paste when they start reading a command. +const BRACKETED_PASTE_ON = "\x1b[?2004h"; +const BRACKETED_PASTE_OFF = "\x1b[?2004l"; +// Longer than one subprocess poll plus its ps timeout, so a startup prompt is seen as busy first. +const SHELL_QUIET_FALLBACK = "3 seconds"; + +function lineEditorReading(text: string, previous: boolean): boolean { + const on = text.lastIndexOf(BRACKETED_PASTE_ON); + const off = text.lastIndexOf(BRACKETED_PASTE_OFF); + return on === off ? previous : on > off; +} + +// Input typed during shell startup can be read by an rc-file prompt (fnm, nvm) instead of the +// shell, which loses the command. Subscribe before the terminal opens so no activity is missed. +// Untraced: each caller owns the span around the wait. +export const watchShellReadiness = Effect.fnUntraced(function* (input: { + readonly subscribe: ( + listener: (event: TerminalEvent) => Effect.Effect, + ) => Effect.Effect<() => void>; + readonly threadId: string; + readonly terminalId: string; + readonly onStartupPrompt: Effect.Effect; +}) { + const outcome = yield* Deferred.make<"ready" | "exited">(); + let reading = false; + let carry = ""; + let busy = false; + let activity = 0; + const unsubscribe = yield* input.subscribe((event) => { + if (event.threadId !== input.threadId || event.terminalId !== input.terminalId) { + return Effect.void; + } + activity += 1; + // A new shell process starts idle; the manager resets its subprocess state without an event. + if (event.type === "started" || event.type === "restarted") { + busy = false; + reading = false; + carry = ""; + } + if (event.type === "activity") busy = event.hasRunningSubprocess; + if (event.type === "output") { + const text = carry + event.data; + carry = text.slice(-(BRACKETED_PASTE_ON.length - 1)); + reading = lineEditorReading(text, reading); + } + if (event.type === "exited" || event.type === "closed") { + return Deferred.succeed(outcome, "exited").pipe(Effect.asVoid); + } + return reading ? Deferred.succeed(outcome, "ready").pipe(Effect.asVoid) : Effect.void; + }); + // A startup prompt runs as a child process, so a busy shell keeps waiting for the user. + const quiet = Effect.gen(function* () { + while (true) { + const seen = activity; + yield* Effect.sleep(SHELL_QUIET_FALLBACK); + if (activity !== seen) continue; + if (!busy) return "ready" as const; + yield* input.onStartupPrompt; + } + }); + // Restored history can hold a previous shell's line editor state, so only live output counts. + const wait = Deferred.await(outcome).pipe(Effect.raceFirst(quiet)); + return { wait, unsubscribe }; +}); diff --git a/apps/web/src/components/chat/useChatTerminalActions.ts b/apps/web/src/components/chat/useChatTerminalActions.ts index a64c748e81432..07afc84fb7466 100644 --- a/apps/web/src/components/chat/useChatTerminalActions.ts +++ b/apps/web/src/components/chat/useChatTerminalActions.ts @@ -278,6 +278,7 @@ export function useChatTerminalActions(input: ChatTerminalActionsInput) { options?.shellHistory === "preserve" ? `${script.command}\r` : projectScriptTerminalInput(script.command), + { afterShellStartup: true }, ); if (!writeResult) return; if (writeResult._tag === "Failure") { diff --git a/apps/web/src/components/terminal/useThreadTerminalActions.ts b/apps/web/src/components/terminal/useThreadTerminalActions.ts index 9112f41519754..f2052be1e566d 100644 --- a/apps/web/src/components/terminal/useThreadTerminalActions.ts +++ b/apps/web/src/components/terminal/useThreadTerminalActions.ts @@ -134,11 +134,16 @@ export function useThreadTerminalActions(threadRef: ScopedThreadRef | null) { })(); }; - const writeToTerminal = (terminalId: string, data: string) => { + /** afterShellStartup has the server hold the input until a starting shell reads it. */ + const writeToTerminal = ( + terminalId: string, + data: string, + options?: { readonly afterShellStartup: boolean }, + ) => { if (!threadRef || !canWrite()) return null; return writeTerminal({ environmentId: threadRef.environmentId, - input: { threadId: threadRef.threadId, terminalId, data }, + input: { threadId: threadRef.threadId, terminalId, data, ...options }, }); }; diff --git a/packages/client-runtime/src/state/terminal.test.ts b/packages/client-runtime/src/state/terminal.test.ts index 6fb2d21b19ea1..0acfb7e6fbbb4 100644 --- a/packages/client-runtime/src/state/terminal.test.ts +++ b/packages/client-runtime/src/state/terminal.test.ts @@ -154,13 +154,15 @@ it("replays the measured grid on every new attachment generation", () => { ]); }); -it("writes the launch's initial input once the first snapshot lands", () => { +it("sends the launch's initial input once, for the server to type after shell startup", () => { const { attached, emit, mount, calls, of } = setup({ initialInput: "pnpm dev\r" }); mount(attached); expect(of("write")).toEqual([]); emit({ status: "running", version: 1 }); emit({ status: "running", version: 2 }); - expect(of("write")).toEqual([{ threadId, terminalId: "term-1", data: "pnpm dev\r" }]); + expect(of("write")).toEqual([ + { threadId, terminalId: "term-1", data: "pnpm dev\r", afterShellStartup: true }, + ]); // The shell gets the measured grid before the launched command runs. expect(calls.map((call) => call.command)).toEqual(["resize", "write"]); }); diff --git a/packages/client-runtime/src/state/terminal.ts b/packages/client-runtime/src/state/terminal.ts index f62c2549daf1d..e364f63ff703d 100644 --- a/packages/client-runtime/src/state/terminal.ts +++ b/packages/client-runtime/src/state/terminal.ts @@ -226,7 +226,7 @@ export interface TerminalSessionOptions { readonly terminalId: string; }; readonly gridSize: TerminalGridSize; - /** Written once, as soon as the first attachment snapshot lands. */ + /** Sent once when the first attachment snapshot lands; the server types it after shell startup. */ readonly initialInput?: string | undefined; /** Read right before every command, so revoking operate stops them at once. */ readonly canOperate: () => boolean; @@ -364,7 +364,8 @@ export function createTerminalSession( initialInputPending = false; void runAtomCommand(registry, atoms.write, { environmentId, - input: { threadId, terminalId, data: options.initialInput }, + // The first snapshot can land while the shell still starts; the server holds the input. + input: { threadId, terminalId, data: options.initialInput, afterShellStartup: true }, }); } diff --git a/packages/contracts/src/terminal.ts b/packages/contracts/src/terminal.ts index 7dcd75cb9d94e..48ba0d643c822 100644 --- a/packages/contracts/src/terminal.ts +++ b/packages/contracts/src/terminal.ts @@ -95,6 +95,8 @@ export type TerminalObserveInput = Schema.Codec.Encoded;