TypeScript
fix(terminal): keep project script commands out of shell history (#13903)
Otavio Salvadorb92ebce0ad8e
diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsxindex 2b109ca9e28..de3c5999410 100644--- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx@@ -25,6 +25,7 @@ import {import {projectScriptCwd,projectScriptRuntimeEnv,+ projectScriptTerminalInput,resolveProjectScripts,} from "@t3tools/shared/projectScripts";import { Platform, ScrollView, View } from "react-native";@@ -667,7 +668,7 @@ function ThreadRouteContent(cwd,worktreePath: preferredWorktreePath,env,- initialInput: `${script.command}\r`,+ initialInput: projectScriptTerminalInput(script.command),},});terminalDebugLog("project-script:staged", {diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.tsindex 8516c279417..dcba984ffe9 100644--- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts+++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts@@ -106,7 +106,7 @@ it.effect("resolves setup scripts through the standalone project service", () =>NO_COLOR: "1",FORCE_COLOR: "0",});- assert.equal(write.mock.calls[0]?.[0].data, "vp install\r");+ assert.equal(write.mock.calls[0]?.[0].data, " vp install\r");const lines: string[] = [];const observed = yield* runner.runForThread({threadId: "thread-1",@@ -120,6 +120,7 @@ it.effect("resolves setup scripts through the standalone project service", () =>},});assert.equal(observed.status, "started");+ assert.match(write.mock.calls[1]?.[0].data ?? "", /^ \S/);const listener = listeners[0]!;yield* listener({type: "output",@@ -138,7 +139,7 @@ it.effect("resolves setup scripts through the standalone project service", () =>});const settleTerminalId = settle.status === "started" ? settle.terminalId : "";assert.match(settleTerminalId, /^settle-clean-/);- assert.equal(write.mock.calls.at(-1)?.[0].data, "cargo clean\r");+ assert.equal(write.mock.calls.at(-1)?.[0].data, " cargo clean\r");const observedSettle = yield* runner.runForThread({threadId: "thread-1",@@ -149,6 +150,7 @@ it.effect("resolves setup scripts through the standalone project service", () =>});const observedTerminalId = observedSettle.status === "started" ? observedSettle.terminalId : "";assert.notEqual(observedTerminalId, settleTerminalId);+ assert.match(write.mock.calls.at(-1)?.[0].data ?? "", /^ \S/);const token = /__T3_SETUP_DONE___(\w+):/.exec(write.mock.calls.at(-1)?.[0].data ?? "")?.[1];const settleListener = listeners.at(-1)!;const completion = yield* Effect.forkChild(diff --git a/apps/server/src/project/ProjectSetupScriptRunner.ts b/apps/server/src/project/ProjectSetupScriptRunner.tsindex dfa850b2fdb..67977c5c7f2 100644--- a/apps/server/src/project/ProjectSetupScriptRunner.ts+++ b/apps/server/src/project/ProjectSetupScriptRunner.ts@@ -2,6 +2,7 @@ import { ProjectId, type ProjectScript } from "@t3tools/contracts";import * as HostProcess from "@t3tools/shared/HostProcess";import {projectScriptRuntimeEnv,+ projectScriptTerminalInput,resolveProjectScripts,settleProjectScript,setupProjectScript,@@ -410,7 +411,7 @@ export const make = Effect.gen(function* () {.write({threadId: input.threadId,terminalId,- data: `${commandLine}\r`,+ data: projectScriptTerminalInput(commandLine),}).pipe(Effect.mapError(diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 9885e70275a..98b3172d0c6 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -129,7 +129,11 @@ import {resolvePromptInjectedEffort,resolveSelectableModel,} from "@t3tools/shared/model";-import { projectScriptCwd, resolveProjectScripts } from "@t3tools/shared/projectScripts";+import {+ projectScriptCwd,+ projectScriptTerminalInput,+ resolveProjectScripts,+} from "@t3tools/shared/projectScripts";import { CHAT_LIST_ANCHOR_OFFSET } from "@t3tools/shared/chatList";import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";import {@@ -3436,6 +3440,7 @@ export default function ChatView(props: ChatViewProps) {worktreePath?: string | null;preferNewTerminal?: boolean;rememberAsLastInvoked?: boolean;+ shellHistory?: "preserve" | "suppress";},) => {if (!hasTerminalWriteAccess() || !activeThreadId || !activeProject || !activeThread) return;@@ -3502,7 +3507,9 @@ export default function ChatView(props: ChatViewProps) {const writeResult = await terminalActions.writeToTerminal(targetTerminalId,- `${script.command}\r`,+ options?.shellHistory === "preserve"+ ? `${script.command}\r`+ : projectScriptTerminalInput(script.command),);if (!writeResult) return;if (writeResult._tag === "Failure") {@@ -3548,7 +3555,7 @@ export default function ChatView(props: ChatViewProps) {icon: "play",runOnWorktreeCreate: false,},- { rememberAsLastInvoked: false },+ { rememberAsLastInvoked: false, shellHistory: "preserve" },);};const runShellCommandStable = useStableCallback(runShellCommand);diff --git a/packages/shared/src/projectScripts.ts b/packages/shared/src/projectScripts.tsindex 0312252d355..af43f78c934 100644--- a/packages/shared/src/projectScripts.ts+++ b/packages/shared/src/projectScripts.ts@@ -79,3 +79,12 @@ export function projectScriptMenuLabel(script: ProjectScript): string {export function settleProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null {return scripts.find((script) => script.runOnSettle === true) ?? null;}++/**+ * Leading spaces omit scripts from Bash history with ignorespace/ignoreboth,+ * zsh with HIST_IGNORE_SPACE, and fish. Only the first line is prefixed, so later+ * entries from multiline input may still enter history. PowerShell/PSReadLine history is not suppressed.+ */+export function projectScriptTerminalInput(command: string): string {+ return ` ${command}\r`;+}