diff --git a/apps/web/.vitest-attachments/bdb7f38c4e160f5f46dac83c451cb22e9fe0c6bb.png b/apps/web/.vitest-attachments/bdb7f38c4e160f5f46dac83c451cb22e9fe0c6bb.png new file mode 100644 index 0000000000..84a660e1e2 Binary files /dev/null and b/apps/web/.vitest-attachments/bdb7f38c4e160f5f46dac83c451cb22e9fe0c6bb.png differ diff --git a/apps/web/src/components/ChatView.browser.tsx b/apps/web/src/components/ChatView.browser.tsx index 48c627747d..18181b2e1c 100644 --- a/apps/web/src/components/ChatView.browser.tsx +++ b/apps/web/src/components/ChatView.browser.tsx @@ -324,6 +324,18 @@ function createDraftOnlySnapshot(): OrchestrationReadModel { }; } +function withProjectScripts( + snapshot: OrchestrationReadModel, + scripts: OrchestrationReadModel["projects"][number]["scripts"], +): OrchestrationReadModel { + return { + ...snapshot, + projects: snapshot.projects.map((project) => + project.id === PROJECT_ID ? { ...project, scripts: Array.from(scripts) } : project, + ), + }; +} + function createSnapshotWithLongProposedPlan(): OrchestrationReadModel { const snapshot = createSnapshotForTargetUser({ targetMessageId: "msg-user-plan-target" as MessageId, @@ -980,6 +992,145 @@ describe("ChatView timeline estimator parity (full app)", () => { } }); + it("runs project scripts from local draft threads at the project cwd", async () => { + useComposerDraftStore.setState({ + draftThreadsByThreadId: { + [THREAD_ID]: { + projectId: PROJECT_ID, + createdAt: NOW_ISO, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + envMode: "local", + }, + }, + projectDraftThreadIdByProjectId: { + [PROJECT_ID]: THREAD_ID, + }, + }); + + const mounted = await mountChatView({ + viewport: DEFAULT_VIEWPORT, + snapshot: withProjectScripts(createDraftOnlySnapshot(), [ + { + id: "lint", + name: "Lint", + command: "bun run lint", + icon: "lint", + runOnWorktreeCreate: false, + }, + ]), + }); + + try { + const runButton = await waitForElement( + () => + Array.from(document.querySelectorAll("button")).find( + (button) => button.title === "Run Lint", + ) as HTMLButtonElement | null, + "Unable to find Run Lint button.", + ); + runButton.click(); + + await vi.waitFor( + () => { + const openRequest = wsRequests.find( + (request) => request._tag === WS_METHODS.terminalOpen, + ); + expect(openRequest).toMatchObject({ + _tag: WS_METHODS.terminalOpen, + threadId: THREAD_ID, + cwd: "/repo/project", + env: { + T3CODE_PROJECT_ROOT: "/repo/project", + }, + }); + }, + { timeout: 8_000, interval: 16 }, + ); + + await vi.waitFor( + () => { + const writeRequest = wsRequests.find( + (request) => request._tag === WS_METHODS.terminalWrite, + ); + expect(writeRequest).toMatchObject({ + _tag: WS_METHODS.terminalWrite, + threadId: THREAD_ID, + data: "bun run lint\r", + }); + }, + { timeout: 8_000, interval: 16 }, + ); + } finally { + await mounted.cleanup(); + } + }); + + it("runs project scripts from worktree draft threads at the worktree cwd", async () => { + useComposerDraftStore.setState({ + draftThreadsByThreadId: { + [THREAD_ID]: { + projectId: PROJECT_ID, + createdAt: NOW_ISO, + runtimeMode: "full-access", + interactionMode: "default", + branch: "feature/draft", + worktreePath: "/repo/worktrees/feature-draft", + envMode: "worktree", + }, + }, + projectDraftThreadIdByProjectId: { + [PROJECT_ID]: THREAD_ID, + }, + }); + + const mounted = await mountChatView({ + viewport: DEFAULT_VIEWPORT, + snapshot: withProjectScripts(createDraftOnlySnapshot(), [ + { + id: "test", + name: "Test", + command: "bun run test", + icon: "test", + runOnWorktreeCreate: false, + }, + ]), + }); + + try { + const runButton = await waitForElement( + () => + Array.from(document.querySelectorAll("button")).find( + (button) => button.title === "Run Test", + ) as HTMLButtonElement | null, + "Unable to find Run Test button.", + ); + runButton.click(); + + await vi.waitFor( + () => { + const openRequest = wsRequests.find( + (request) => request._tag === WS_METHODS.terminalOpen, + ); + expect(openRequest).toMatchObject({ + _tag: WS_METHODS.terminalOpen, + threadId: THREAD_ID, + cwd: "/repo/worktrees/feature-draft", + env: { + T3CODE_PROJECT_ROOT: "/repo/project", + T3CODE_WORKTREE_PATH: "/repo/worktrees/feature-draft", + }, + }); + }, + { timeout: 8_000, interval: 16 }, + ); + } finally { + await mounted.cleanup(); + } + }); + it("toggles plan mode with Shift+Tab only while the composer is focused", async () => { const mounted = await mountChatView({ viewport: DEFAULT_VIEWPORT, diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 3484375244..720d5d5f68 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -112,6 +112,7 @@ import { type NewProjectScriptInput } from "./ProjectScriptsControl"; import { commandForProjectScript, nextProjectScriptId, + projectScriptCwd, projectScriptRuntimeEnv, projectScriptIdFromCommand, setupProjectScript, @@ -994,7 +995,12 @@ export default function ChatView({ threadId }: ChatViewProps) { latestTurnSettled, timelineEntries, ]); - const gitCwd = activeThread?.worktreePath ?? activeProject?.cwd ?? null; + const gitCwd = activeProject + ? projectScriptCwd({ + project: { cwd: activeProject.cwd }, + worktreePath: activeThread?.worktreePath ?? null, + }) + : null; const composerTriggerKind = composerTrigger?.kind ?? null; const pathTriggerQuery = composerTrigger?.kind === "path" ? composerTrigger.query : ""; const isPathTrigger = composerTriggerKind === "path"; @@ -1303,12 +1309,10 @@ export default function ChatView({ threadId }: ChatViewProps) { worktreePath?: string | null; preferNewTerminal?: boolean; rememberAsLastInvoked?: boolean; - allowLocalDraftThread?: boolean; }, ) => { const api = readNativeApi(); if (!api || !activeThreadId || !activeProject || !activeThread) return; - if (!isServerThread && !options?.allowLocalDraftThread) return; if (options?.rememberAsLastInvoked !== false) { setLastInvokedScriptByProjectId((current) => { if (current[activeProject.id] === script.id) return current; @@ -1377,7 +1381,6 @@ export default function ChatView({ threadId }: ChatViewProps) { activeThread, activeThreadId, gitCwd, - isServerThread, setTerminalOpen, setThreadError, storeNewTerminal, @@ -2566,7 +2569,6 @@ export default function ChatView({ threadId }: ChatViewProps) { const setupScriptOptions: Parameters[1] = { worktreePath: nextThreadWorktreePath, rememberAsLastInvoked: false, - allowLocalDraftThread: createdServerThreadForLocalDraft, }; if (nextThreadWorktreePath) { setupScriptOptions.cwd = nextThreadWorktreePath; @@ -3465,7 +3467,7 @@ export default function ChatView({ threadId }: ChatViewProps) { activeThreadTitle={activeThread.title} activeProjectName={activeProject?.name} isGitRepo={isGitRepo} - openInCwd={activeThread.worktreePath ?? activeProject?.cwd ?? null} + openInCwd={gitCwd} activeProjectScripts={activeProject?.scripts} preferredScriptId={ activeProject ? (lastInvokedScriptByProjectId[activeProject.id] ?? null) : null diff --git a/apps/web/src/projectScripts.test.ts b/apps/web/src/projectScripts.test.ts index dadc22a471..08678f8730 100644 --- a/apps/web/src/projectScripts.test.ts +++ b/apps/web/src/projectScripts.test.ts @@ -4,6 +4,7 @@ import { commandForProjectScript, nextProjectScriptId, primaryProjectScript, + projectScriptCwd, projectScriptRuntimeEnv, projectScriptIdFromCommand, setupProjectScript, @@ -70,4 +71,19 @@ describe("projectScripts helpers", () => { expect(env.CUSTOM_FLAG).toBe("1"); expect(env.T3CODE_WORKTREE_PATH).toBeUndefined(); }); + + it("prefers the worktree path for script cwd resolution", () => { + expect( + projectScriptCwd({ + project: { cwd: "/repo" }, + worktreePath: "/repo/worktree-a", + }), + ).toBe("/repo/worktree-a"); + expect( + projectScriptCwd({ + project: { cwd: "/repo" }, + worktreePath: null, + }), + ).toBe("/repo"); + }); }); diff --git a/apps/web/src/projectScripts.ts b/apps/web/src/projectScripts.ts index e4ff752dbb..c11c3923bc 100644 --- a/apps/web/src/projectScripts.ts +++ b/apps/web/src/projectScripts.ts @@ -63,6 +63,15 @@ interface ProjectScriptRuntimeEnvInput { extraEnv?: Record; } +export function projectScriptCwd(input: { + project: { + cwd: string; + }; + worktreePath?: string | null; +}): string { + return input.worktreePath ?? input.project.cwd; +} + export function projectScriptRuntimeEnv( input: ProjectScriptRuntimeEnvInput, ): Record {