Skip to content

Commit e8d44d0

Browse files
committed
fix(mobile): use current checkout for non-git projects
1 parent 07d38b8 commit e8d44d0

7 files changed

Lines changed: 122 additions & 28 deletions

File tree

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ import { resolveDraftProjectSelection } from "./new-task-project-selection";
132132
import {
133133
resolveNewTaskBranchLabel,
134134
resolveNewTaskWorkspaceLabel,
135+
resolveNewTaskWorkspaceSelection,
135136
} from "./new-task-context-presentation";
136137
import { useIncomingShare } from "../sharing/IncomingShareProvider";
137138
import { selectIncomingShareAttachmentsForServer } from "../sharing/incoming-share-model";
@@ -1188,8 +1189,17 @@ export function NewTaskDraftScreen(props: {
11881189
draft.modelSelection ?? null,
11891190
selectedProject.id,
11901191
) ?? flow.selectedModel;
1191-
const workspaceMode = draft.workspaceSelection?.mode ?? flow.workspaceMode;
1192-
const selectedBranchName = draft.workspaceSelection?.branch ?? flow.selectedBranchName;
1192+
const workspaceSelection = resolveNewTaskWorkspaceSelection({
1193+
selection: draft.workspaceSelection,
1194+
fallback: {
1195+
mode: flow.workspaceMode,
1196+
branch: flow.selectedBranchName,
1197+
worktreePath: flow.selectedWorktreePath,
1198+
},
1199+
isGitRepo: flow.isGitRepo,
1200+
});
1201+
const workspaceMode = workspaceSelection.mode;
1202+
const selectedBranchName = workspaceSelection.branch;
11931203
const initialMessageText = draft.text.trim();
11941204

11951205
if (
@@ -1535,7 +1545,7 @@ export function NewTaskDraftScreen(props: {
15351545
<ComposerInlineControl
15361546
accessibilityHint={`Switches to ${flow.workspaceMode === "local" ? "a new worktree" : "the current checkout"}`}
15371547
accessibilityLabel={workspaceLabel}
1538-
disabled={isComposerInteractionLocked || voiceInput.isBusy}
1548+
disabled={isComposerInteractionLocked || voiceInput.isBusy || !flow.isGitRepo}
15391549
renderIcon={(size) => (
15401550
<NewTaskWorkspaceIcon
15411551
workspaceMode={flow.workspaceMode}
@@ -1552,7 +1562,7 @@ export function NewTaskDraftScreen(props: {
15521562
<ComposerInlineControl
15531563
accessibilityLabel={`${flow.workspaceMode === "worktree" ? "Base branch" : "Branch"}: ${selectedBranchLabel}`}
15541564
chevronDirection="right"
1555-
disabled={isComposerInteractionLocked}
1565+
disabled={isComposerInteractionLocked || !flow.isGitRepo}
15561566
icon="arrow.triangle.branch"
15571567
label={showBranchLoading ? "Loading branches…" : selectedBranchLabel}
15581568
maxWidth={190}

‎apps/mobile/src/features/threads/new-task-context-presentation.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,48 @@ import {
55
resolveNewTaskBranchWorktreePath,
66
resolveNewTaskBranchLabel,
77
resolveNewTaskLocalWorkspaceSelection,
8+
resolveNewTaskWorkspaceSelection,
89
} from "./new-task-context-presentation";
910

11+
describe("resolveNewTaskWorkspaceSelection", () => {
12+
const fallback = { mode: "worktree", branch: null, worktreePath: null } as const;
13+
14+
it("falls back to the current checkout when a non-git project defaults to worktree", () => {
15+
expect(
16+
resolveNewTaskWorkspaceSelection({ selection: undefined, fallback, isGitRepo: false }),
17+
).toEqual({ mode: "local", branch: null, worktreePath: null });
18+
});
19+
20+
it.each(["local", "worktree"] as const)(
21+
"clears stale %s selections for non-git projects",
22+
(mode) => {
23+
expect(
24+
resolveNewTaskWorkspaceSelection({
25+
selection: {
26+
mode,
27+
branch: "main",
28+
worktreePath: "/old-worktree",
29+
startFromOrigin: true,
30+
pullRequestReference: "https://github.com/acme/repo/pull/123",
31+
},
32+
fallback,
33+
isGitRepo: false,
34+
}),
35+
).toEqual({ mode: "local", branch: null, worktreePath: null });
36+
},
37+
);
38+
39+
it("preserves the selection and defaults for git projects", () => {
40+
const selection = { mode: "local", branch: "feature", worktreePath: "/worktree" } as const;
41+
expect(resolveNewTaskWorkspaceSelection({ selection, fallback, isGitRepo: true })).toBe(
42+
selection,
43+
);
44+
expect(
45+
resolveNewTaskWorkspaceSelection({ selection: undefined, fallback, isGitRepo: true }),
46+
).toBe(fallback);
47+
});
48+
});
49+
1050
describe("resolveNewTaskLocalWorkspaceSelection", () => {
1151
it("waits for refs instead of carrying a worktree base into Current checkout", () => {
1252
expect(

‎apps/mobile/src/features/threads/new-task-context-presentation.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { sanitizeNewRefName } from "@t3tools/shared/git";
22
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
3+
import { resolveSendEnvMode } from "@t3tools/shared/projectSettings";
4+
import type { ComposerDraftWorkspaceSelection } from "../../state/use-composer-drafts";
35

46
type WorkspaceMode = "local" | "worktree";
57

8+
export function resolveNewTaskWorkspaceSelection(input: {
9+
readonly selection: ComposerDraftWorkspaceSelection | undefined;
10+
readonly fallback: ComposerDraftWorkspaceSelection;
11+
readonly isGitRepo: boolean;
12+
}): ComposerDraftWorkspaceSelection {
13+
const selection = input.selection ?? input.fallback;
14+
const mode = resolveSendEnvMode({ requestedEnvMode: selection.mode, isGitRepo: input.isGitRepo });
15+
return input.isGitRepo ? selection : { mode, branch: null, worktreePath: null };
16+
}
17+
618
export function resolveNewTaskWorkspaceLabel(input: {
719
readonly workspaceMode: WorkspaceMode;
820
readonly worktreePath: string | null;

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import {
7878
rememberModelOptions,
7979
withRememberedModelOptions,
8080
} from "../../state/use-model-option-memory";
81-
import { useDebouncedValue, usePaginatedBranches } from "../../state/queries";
81+
import { useBranches, useDebouncedValue, usePaginatedBranches } from "../../state/queries";
8282
import { vcsEnvironment } from "../../state/vcs";
8383
import {
8484
flattenQueuedThreadMessages,
@@ -118,6 +118,7 @@ import {
118118
filterNewTaskBranches,
119119
resolveNewTaskBranchWorktreePath,
120120
resolveNewTaskLocalWorkspaceSelection,
121+
resolveNewTaskWorkspaceSelection,
121122
} from "./new-task-context-presentation";
122123
import { resolveEnvironmentProjectMatch } from "./new-task-project-selection";
123124
import { resolveProjectThreadCreationBranch } from "./projectThreadCreationValidation";
@@ -168,6 +169,7 @@ type NewTaskFlowContextValue = {
168169
readonly selectedProjectKey: string | null;
169170
readonly selectedModelKey: string | null;
170171
readonly workspaceMode: WorkspaceMode;
172+
readonly isGitRepo: boolean;
171173
readonly canChooseWorkspace: boolean;
172174
readonly selectedBranchName: string | null;
173175
readonly selectedWorktreePath: string | null;
@@ -434,6 +436,21 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
434436
const selectedProjectDraft = useComposerDraft(selectedProjectDraftKey);
435437
const prompt = selectedProjectDraft.text;
436438
const attachments = selectedProjectDraft.attachments;
439+
const projectGitStatus = useEnvironmentQuery(
440+
selectedProject !== null && selectedProject.workspaceRoot !== ""
441+
? vcsEnvironment.status({
442+
environmentId: selectedProject.environmentId,
443+
input: { cwd: selectedProject.workspaceRoot },
444+
})
445+
: null,
446+
);
447+
const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;
448+
// The unfiltered ref list also provides repository capability while offline.
449+
const projectBranches = useBranches({
450+
environmentId: selectedProject?.environmentId ?? null,
451+
cwd: selectedProject?.workspaceRoot || null,
452+
});
453+
const isGitRepo = projectGitStatus.data?.isRepo ?? projectBranches.data?.isRepo ?? true;
437454
const fileAccessSession = useEnvironmentQuery(
438455
selectedProject !== null && selectedProject.workspaceRoot !== ""
439456
? environmentSession.sessionStateAtom(selectedProject.environmentId)
@@ -492,17 +509,20 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
492509
selectedProjectDraft.workspaceSelection?.mode !== undefined ||
493510
projectSettings.sources.defaultThreadEnvMode !== "environment" ||
494511
(!t3ProjectFileQuery.isPending && !fileAccessPending);
495-
const workspaceMode = canChooseWorkspace
496-
? (selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode)
497-
: "local";
498-
const selectedBranchName = selectedProjectDraft.workspaceSelection?.branch ?? null;
499-
const selectedWorktreePath = selectedProjectDraft.workspaceSelection?.worktreePath ?? null;
512+
const workspaceSelection = resolveNewTaskWorkspaceSelection({
513+
selection: canChooseWorkspace ? selectedProjectDraft.workspaceSelection : undefined,
514+
fallback: { mode: defaultWorkspaceMode, branch: null, worktreePath: null },
515+
isGitRepo,
516+
});
517+
const workspaceMode = workspaceSelection.mode;
518+
const selectedBranchName = workspaceSelection.branch;
519+
const selectedWorktreePath = workspaceSelection.worktreePath;
500520
// Keep the user's explicit choice separate from the resolved display value:
501521
// only the explicit flag is ever written back to the draft, so the resolved
502522
// value keeps tracking the server setting when the config loads late.
503523
const draftStartFromOrigin = selectedProjectDraft.workspaceSelection?.startFromOrigin;
504524
const startFromOrigin =
505-
draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin;
525+
isGitRepo && (draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin);
506526
const defaultRuntimeMode = editingPendingTask
507527
? (editingPendingTask.runtimeMode ?? DEFAULT_RUNTIME_MODE)
508528
: projectSettings.settings.defaultRuntimeMode;
@@ -657,15 +677,6 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
657677
allBranchRefs,
658678
Arr.filter((branch) => !branch.isRemote),
659679
);
660-
const projectGitStatus = useEnvironmentQuery(
661-
branchTarget.environmentId !== null && branchTarget.cwd !== null
662-
? vcsEnvironment.status({
663-
environmentId: branchTarget.environmentId,
664-
input: { cwd: branchTarget.cwd },
665-
})
666-
: null,
667-
);
668-
const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;
669680

670681
const filteredBranches = filterNewTaskBranches(allBranchRefs, branchQuery);
671682

@@ -957,8 +968,12 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
957968
if ((text.length === 0 && draft.attachments.length === 0) || !draftModelSelection) {
958969
return null;
959970
}
960-
const workspaceSelection = canChooseWorkspace ? draft.workspaceSelection : undefined;
961-
const mode = workspaceSelection?.mode ?? workspaceMode;
971+
const workspaceSelection = resolveNewTaskWorkspaceSelection({
972+
selection: canChooseWorkspace ? draft.workspaceSelection : undefined,
973+
fallback: { mode: workspaceMode, branch: null, worktreePath: null },
974+
isGitRepo,
975+
});
976+
const mode = workspaceSelection.mode;
962977
// When the selection is the stand-in built from the queued snapshot,
963978
// persist the original (possibly absent) snapshot values — the
964979
// stand-in's placeholder title/workspaceRoot must never be written back
@@ -1117,6 +1132,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
11171132
selectedProjectKey,
11181133
selectedModelKey,
11191134
workspaceMode,
1135+
isGitRepo,
11201136
canChooseWorkspace,
11211137
selectedBranchName,
11221138
selectedWorktreePath,

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,12 +802,7 @@ export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[
802802

803803
export { readFileAsDataUrl } from "../lib/imageCompression";
804804

805-
export function resolveSendEnvMode(input: {
806-
requestedEnvMode: DraftThreadEnvMode;
807-
isGitRepo: boolean;
808-
}): DraftThreadEnvMode {
809-
return input.isGitRepo ? input.requestedEnvMode : "local";
810-
}
805+
export { resolveSendEnvMode } from "@t3tools/shared/projectSettings";
811806

812807
export function resolveBackgroundDraftWorkspaceOptions(input: {
813808
envMode: DraftThreadEnvMode;

‎packages/shared/src/projectSettings.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
resolveNewThreadEnvMode,
1414
resolveProjectFileBackedSetting,
1515
resolveProjectSettings,
16+
resolveSendEnvMode,
1617
resolveWorktreeCleanup,
1718
withProjectSettingsOverrides,
1819
} from "./projectSettings.ts";
@@ -21,6 +22,19 @@ import { applyServerSettingsPatch } from "./serverSettings.ts";
2122
const projectId = ProjectId.make("project-a");
2223
const otherProjectId = ProjectId.make("project-b");
2324

25+
describe("resolveSendEnvMode", () => {
26+
it.each(["local", "worktree"] as const)(
27+
"uses local for non-git projects requesting %s",
28+
(mode) => {
29+
expect(resolveSendEnvMode({ requestedEnvMode: mode, isGitRepo: false })).toBe("local");
30+
},
31+
);
32+
33+
it.each(["local", "worktree"] as const)("preserves %s for git repositories", (mode) => {
34+
expect(resolveSendEnvMode({ requestedEnvMode: mode, isGitRepo: true })).toBe(mode);
35+
});
36+
});
37+
2438
describe("resolveProjectSettings", () => {
2539
it("inherits every scopable key when the project has no overrides", () => {
2640
const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId);

‎packages/shared/src/projectSettings.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ export function resolveNewThreadEnvMode(input: {
205205
return isNewProjectFolder ? "local" : configured;
206206
}
207207

208+
export function resolveSendEnvMode(input: {
209+
readonly requestedEnvMode: ThreadEnvMode;
210+
readonly isGitRepo: boolean;
211+
}): ThreadEnvMode {
212+
return input.isGitRepo ? input.requestedEnvMode : "local";
213+
}
214+
208215
export function withProjectSettingsOverrides(
209216
settings: Pick<ServerSettings, "projectSettingsOverrides">,
210217
projectId: ProjectId,

0 commit comments

Comments
 (0)