TypeScript
fix(mobile): use current checkout for non-git projects (#21404)
maria4db2533f5575
diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsxindex 1d4dcc750c5..617c50c973c 100644--- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx+++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx@@ -134,6 +134,7 @@ import { resolveDraftProjectSelection } from "./new-task-project-selection";import {resolveNewTaskBranchLabel,resolveNewTaskWorkspaceLabel,+ resolveNewTaskWorkspaceSelection,} from "./new-task-context-presentation";import { useIncomingShare } from "../sharing/IncomingShareProvider";import { selectIncomingShareAttachmentsForServer } from "../sharing/incoming-share-model";@@ -1190,8 +1191,17 @@ export function NewTaskDraftScreen(props: {draft.modelSelection ?? null,selectedProject.id,) ?? flow.selectedModel;- const workspaceMode = draft.workspaceSelection?.mode ?? flow.workspaceMode;- const selectedBranchName = draft.workspaceSelection?.branch ?? flow.selectedBranchName;+ const workspaceSelection = resolveNewTaskWorkspaceSelection({+ selection: draft.workspaceSelection,+ fallback: {+ mode: flow.workspaceMode,+ branch: flow.selectedBranchName,+ worktreePath: flow.selectedWorktreePath,+ },+ isGitRepo: flow.isGitRepo,+ });+ const workspaceMode = workspaceSelection.mode;+ const selectedBranchName = workspaceSelection.branch;const initialMessageText = draft.text.trim();if (@@ -1366,7 +1376,8 @@ export function NewTaskDraftScreen(props: {!flow.submitting &&pendingPastedTextAttachmentCount === 0 &&!voiceInput.blocksSubmission &&- !(flow.workspaceMode === "worktree" && !flow.selectedBranchName);+ // Until the repository check answers, a saved worktree selection may belong to a non-git folder.+ !(flow.workspaceMode === "worktree" && (!flow.selectedBranchName || !flow.isGitRepoKnown));const openDraftDocument = (attachment: ComposerDocumentAttachment) => {// A draft attachment lives only in the draft. Without its key the screen would fall through// to a remote lookup for bytes the server has never seen.@@ -1553,7 +1564,7 @@ export function NewTaskDraftScreen(props: {<ComposerInlineControlaccessibilityHint={`Switches to ${flow.workspaceMode === "local" ? "a new worktree" : "the current checkout"}`}accessibilityLabel={workspaceLabel}- disabled={isComposerInteractionLocked || voiceInput.isBusy}+ disabled={isComposerInteractionLocked || voiceInput.isBusy || !flow.isGitRepo}renderIcon={(size) => (<NewTaskWorkspaceIconworkspaceMode={flow.workspaceMode}@@ -1570,7 +1581,7 @@ export function NewTaskDraftScreen(props: {<ComposerInlineControlaccessibilityLabel={`${flow.workspaceMode === "worktree" ? "Base branch" : "Branch"}: ${selectedBranchLabel}`}chevronDirection="right"- disabled={isComposerInteractionLocked}+ disabled={isComposerInteractionLocked || !flow.isGitRepo}icon="arrow.triangle.branch"label={showBranchLoading ? "Loading branches…" : selectedBranchLabel}maxWidth={190}diff --git a/apps/mobile/src/features/threads/new-task-context-presentation.test.ts b/apps/mobile/src/features/threads/new-task-context-presentation.test.tsindex d79cc596a7b..a14699345a6 100644--- a/apps/mobile/src/features/threads/new-task-context-presentation.test.ts+++ b/apps/mobile/src/features/threads/new-task-context-presentation.test.ts@@ -5,8 +5,48 @@ import {resolveNewTaskBranchWorktreePath,resolveNewTaskBranchLabel,resolveNewTaskLocalWorkspaceSelection,+ resolveNewTaskWorkspaceSelection,} from "./new-task-context-presentation";+describe("resolveNewTaskWorkspaceSelection", () => {+ const fallback = { mode: "worktree", branch: null, worktreePath: null } as const;++ it("falls back to the current checkout when a non-git project defaults to worktree", () => {+ expect(+ resolveNewTaskWorkspaceSelection({ selection: undefined, fallback, isGitRepo: false }),+ ).toEqual({ mode: "local", branch: null, worktreePath: null });+ });++ it.each(["local", "worktree"] as const)(+ "clears stale %s selections for non-git projects",+ (mode) => {+ expect(+ resolveNewTaskWorkspaceSelection({+ selection: {+ mode,+ branch: "main",+ worktreePath: "/old-worktree",+ startFromOrigin: true,+ pullRequestReference: "https://github.com/acme/repo/pull/123",+ },+ fallback,+ isGitRepo: false,+ }),+ ).toEqual({ mode: "local", branch: null, worktreePath: null });+ },+ );++ it("preserves the selection and defaults for git projects", () => {+ const selection = { mode: "local", branch: "feature", worktreePath: "/worktree" } as const;+ expect(resolveNewTaskWorkspaceSelection({ selection, fallback, isGitRepo: true })).toBe(+ selection,+ );+ expect(+ resolveNewTaskWorkspaceSelection({ selection: undefined, fallback, isGitRepo: true }),+ ).toBe(fallback);+ });+});+describe("resolveNewTaskLocalWorkspaceSelection", () => {it("waits for refs instead of carrying a worktree base into Current checkout", () => {expect(diff --git a/apps/mobile/src/features/threads/new-task-context-presentation.ts b/apps/mobile/src/features/threads/new-task-context-presentation.tsindex dd3b23686ce..bd2add22eb6 100644--- a/apps/mobile/src/features/threads/new-task-context-presentation.ts+++ b/apps/mobile/src/features/threads/new-task-context-presentation.ts@@ -1,8 +1,20 @@import { sanitizeNewRefName } from "@t3tools/shared/git";import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";+import { resolveSendEnvMode } from "@t3tools/shared/projectSettings";+import type { ComposerDraftWorkspaceSelection } from "../../state/use-composer-drafts";type WorkspaceMode = "local" | "worktree";+export function resolveNewTaskWorkspaceSelection(input: {+ readonly selection: ComposerDraftWorkspaceSelection | undefined;+ readonly fallback: ComposerDraftWorkspaceSelection;+ readonly isGitRepo: boolean;+}): ComposerDraftWorkspaceSelection {+ const selection = input.selection ?? input.fallback;+ const mode = resolveSendEnvMode({ requestedEnvMode: selection.mode, isGitRepo: input.isGitRepo });+ return input.isGitRepo ? selection : { mode, branch: null, worktreePath: null };+}+export function resolveNewTaskWorkspaceLabel(input: {readonly workspaceMode: WorkspaceMode;readonly worktreePath: string | null;diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsxindex 065cc73ce17..23618df9c8e 100644--- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx+++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx@@ -79,7 +79,7 @@ import {rememberModelOptions,withRememberedModelOptions,} from "../../state/use-model-option-memory";-import { useDebouncedValue, usePaginatedBranches } from "../../state/queries";+import { useBranches, useDebouncedValue, usePaginatedBranches } from "../../state/queries";import { vcsEnvironment } from "../../state/vcs";import {flattenQueuedThreadMessages,@@ -119,6 +119,7 @@ import {filterNewTaskBranches,resolveNewTaskBranchWorktreePath,resolveNewTaskLocalWorkspaceSelection,+ resolveNewTaskWorkspaceSelection,} from "./new-task-context-presentation";import { resolveEnvironmentProjectMatch } from "./new-task-project-selection";import { resolveProjectThreadCreationBranch } from "./projectThreadCreationValidation";@@ -169,6 +170,8 @@ type NewTaskFlowContextValue = {readonly selectedProjectKey: string | null;readonly selectedModelKey: string | null;readonly workspaceMode: WorkspaceMode;+ readonly isGitRepo: boolean;+ readonly isGitRepoKnown: boolean;readonly canChooseWorkspace: boolean;readonly selectedBranchName: string | null;readonly selectedWorktreePath: string | null;@@ -435,6 +438,22 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {const selectedProjectDraft = useComposerDraft(selectedProjectDraftKey);const prompt = selectedProjectDraft.text;const attachments = selectedProjectDraft.attachments;+ const projectGitStatus = useEnvironmentQuery(+ selectedProject !== null && selectedProject.workspaceRoot !== ""+ ? vcsEnvironment.status({+ environmentId: selectedProject.environmentId,+ input: { cwd: selectedProject.workspaceRoot },+ })+ : null,+ );+ const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;+ // The unfiltered ref list also provides repository capability while offline.+ const projectBranches = useBranches({+ environmentId: selectedProject?.environmentId ?? null,+ cwd: selectedProject?.workspaceRoot || null,+ });+ const knownIsGitRepo = projectGitStatus.data?.isRepo ?? projectBranches.data?.isRepo ?? null;+ const isGitRepo = knownIsGitRepo ?? true;const fileAccessSession = useEnvironmentQuery(selectedProject !== null && selectedProject.workspaceRoot !== ""? environmentSession.sessionStateAtom(selectedProject.environmentId)@@ -497,17 +516,20 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {selectedProjectDraft.workspaceSelection?.mode !== undefined ||projectSettings.sources.defaultThreadEnvMode !== "environment" ||(!t3ProjectFileQuery.isPending && !fileAccessPending);- const workspaceMode = canChooseWorkspace- ? (selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode)- : "local";- const selectedBranchName = selectedProjectDraft.workspaceSelection?.branch ?? null;- const selectedWorktreePath = selectedProjectDraft.workspaceSelection?.worktreePath ?? null;+ const workspaceSelection = resolveNewTaskWorkspaceSelection({+ selection: canChooseWorkspace ? selectedProjectDraft.workspaceSelection : undefined,+ fallback: { mode: defaultWorkspaceMode, branch: null, worktreePath: null },+ isGitRepo,+ });+ const workspaceMode = workspaceSelection.mode;+ const selectedBranchName = workspaceSelection.branch;+ const selectedWorktreePath = workspaceSelection.worktreePath;// Keep the user's explicit choice separate from the resolved display value:// only the explicit flag is ever written back to the draft, so the resolved// value keeps tracking the server setting when the config loads late.const draftStartFromOrigin = selectedProjectDraft.workspaceSelection?.startFromOrigin;const startFromOrigin =- draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin;+ isGitRepo && (draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin);const defaultRuntimeMode = editingPendingTask? (editingPendingTask.runtimeMode ?? DEFAULT_RUNTIME_MODE): projectSettings.settings.defaultRuntimeMode;@@ -662,15 +684,6 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {allBranchRefs,Arr.filter((branch) => !branch.isRemote),);- const projectGitStatus = useEnvironmentQuery(- branchTarget.environmentId !== null && branchTarget.cwd !== null- ? vcsEnvironment.status({- environmentId: branchTarget.environmentId,- input: { cwd: branchTarget.cwd },- })- : null,- );- const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;const filteredBranches = filterNewTaskBranches(allBranchRefs, branchQuery);@@ -971,8 +984,12 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {) {return null;}- const workspaceSelection = canChooseWorkspace ? draft.workspaceSelection : undefined;- const mode = workspaceSelection?.mode ?? workspaceMode;+ const workspaceSelection = resolveNewTaskWorkspaceSelection({+ selection: canChooseWorkspace ? draft.workspaceSelection : undefined,+ fallback: { mode: workspaceMode, branch: null, worktreePath: null },+ isGitRepo,+ });+ const mode = workspaceSelection.mode;// When the selection is the stand-in built from the queued snapshot,// persist the original (possibly absent) snapshot values — the// stand-in's placeholder title/workspaceRoot must never be written back@@ -1131,6 +1148,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {selectedProjectKey,selectedModelKey,workspaceMode,+ isGitRepo,+ isGitRepoKnown: knownIsGitRepo !== null,canChooseWorkspace,selectedBranchName,selectedWorktreePath,diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.tsindex c132c6689ff..03ba62765e3 100644--- a/apps/web/src/components/ChatView.logic.ts+++ b/apps/web/src/components/ChatView.logic.ts@@ -797,12 +797,7 @@ export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[export { readFileAsDataUrl } from "../lib/imageCompression";-export function resolveSendEnvMode(input: {- requestedEnvMode: DraftThreadEnvMode;- isGitRepo: boolean;-}): DraftThreadEnvMode {- return input.isGitRepo ? input.requestedEnvMode : "local";-}+export { resolveSendEnvMode } from "@t3tools/shared/projectSettings";export function resolveBackgroundDraftWorkspaceOptions(input: {envMode: DraftThreadEnvMode;diff --git a/packages/shared/src/projectSettings.test.ts b/packages/shared/src/projectSettings.test.tsindex 3cebbe9f57e..868cf088527 100644--- a/packages/shared/src/projectSettings.test.ts+++ b/packages/shared/src/projectSettings.test.ts@@ -13,6 +13,7 @@ import {resolveNewThreadEnvMode,resolveProjectFileBackedSetting,resolveProjectSettings,+ resolveSendEnvMode,resolveWorktreeCleanup,withProjectSettingsOverrides,} from "./projectSettings.ts";@@ -21,6 +22,19 @@ import { applyServerSettingsPatch } from "./serverSettings.ts";const projectId = ProjectId.make("project-a");const otherProjectId = ProjectId.make("project-b");+describe("resolveSendEnvMode", () => {+ it.each(["local", "worktree"] as const)(+ "uses local for non-git projects requesting %s",+ (mode) => {+ expect(resolveSendEnvMode({ requestedEnvMode: mode, isGitRepo: false })).toBe("local");+ },+ );++ it.each(["local", "worktree"] as const)("preserves %s for git repositories", (mode) => {+ expect(resolveSendEnvMode({ requestedEnvMode: mode, isGitRepo: true })).toBe(mode);+ });+});+describe("resolveProjectSettings", () => {it("inherits every scopable key when the project has no overrides", () => {const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId);diff --git a/packages/shared/src/projectSettings.ts b/packages/shared/src/projectSettings.tsindex 76bc70fcc0a..f44e54b2382 100644--- a/packages/shared/src/projectSettings.ts+++ b/packages/shared/src/projectSettings.ts@@ -205,6 +205,13 @@ export function resolveNewThreadEnvMode(input: {return isNewProjectFolder ? "local" : configured;}+export function resolveSendEnvMode(input: {+ readonly requestedEnvMode: ThreadEnvMode;+ readonly isGitRepo: boolean;+}): ThreadEnvMode {+ return input.isGitRepo ? input.requestedEnvMode : "local";+}+export function withProjectSettingsOverrides(settings: Pick<ServerSettings, "projectSettingsOverrides">,projectId: ProjectId,