Skip to content

Commit 4db2533

Browse files
authored
fix(mobile): use current checkout for non-git projects (pingdotgg#21404)
1 parent 2f6755b commit 4db2533

7 files changed

Lines changed: 127 additions & 29 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ import { resolveDraftProjectSelection } from "./new-task-project-selection";
134134
import {
135135
resolveNewTaskBranchLabel,
136136
resolveNewTaskWorkspaceLabel,
137+
resolveNewTaskWorkspaceSelection,
137138
} from "./new-task-context-presentation";
138139
import { useIncomingShare } from "../sharing/IncomingShareProvider";
139140
import { selectIncomingShareAttachmentsForServer } from "../sharing/incoming-share-model";
@@ -1190,8 +1191,17 @@ export function NewTaskDraftScreen(props: {
11901191
draft.modelSelection ?? null,
11911192
selectedProject.id,
11921193
) ?? flow.selectedModel;
1193-
const workspaceMode = draft.workspaceSelection?.mode ?? flow.workspaceMode;
1194-
const selectedBranchName = draft.workspaceSelection?.branch ?? flow.selectedBranchName;
1194+
const workspaceSelection = resolveNewTaskWorkspaceSelection({
1195+
selection: draft.workspaceSelection,
1196+
fallback: {
1197+
mode: flow.workspaceMode,
1198+
branch: flow.selectedBranchName,
1199+
worktreePath: flow.selectedWorktreePath,
1200+
},
1201+
isGitRepo: flow.isGitRepo,
1202+
});
1203+
const workspaceMode = workspaceSelection.mode;
1204+
const selectedBranchName = workspaceSelection.branch;
11951205
const initialMessageText = draft.text.trim();
11961206

11971207
if (
@@ -1366,7 +1376,8 @@ export function NewTaskDraftScreen(props: {
13661376
!flow.submitting &&
13671377
pendingPastedTextAttachmentCount === 0 &&
13681378
!voiceInput.blocksSubmission &&
1369-
!(flow.workspaceMode === "worktree" && !flow.selectedBranchName);
1379+
// Until the repository check answers, a saved worktree selection may belong to a non-git folder.
1380+
!(flow.workspaceMode === "worktree" && (!flow.selectedBranchName || !flow.isGitRepoKnown));
13701381
const openDraftDocument = (attachment: ComposerDocumentAttachment) => {
13711382
// A draft attachment lives only in the draft. Without its key the screen would fall through
13721383
// to a remote lookup for bytes the server has never seen.
@@ -1553,7 +1564,7 @@ export function NewTaskDraftScreen(props: {
15531564
<ComposerInlineControl
15541565
accessibilityHint={`Switches to ${flow.workspaceMode === "local" ? "a new worktree" : "the current checkout"}`}
15551566
accessibilityLabel={workspaceLabel}
1556-
disabled={isComposerInteractionLocked || voiceInput.isBusy}
1567+
disabled={isComposerInteractionLocked || voiceInput.isBusy || !flow.isGitRepo}
15571568
renderIcon={(size) => (
15581569
<NewTaskWorkspaceIcon
15591570
workspaceMode={flow.workspaceMode}
@@ -1570,7 +1581,7 @@ export function NewTaskDraftScreen(props: {
15701581
<ComposerInlineControl
15711582
accessibilityLabel={`${flow.workspaceMode === "worktree" ? "Base branch" : "Branch"}: ${selectedBranchLabel}`}
15721583
chevronDirection="right"
1573-
disabled={isComposerInteractionLocked}
1584+
disabled={isComposerInteractionLocked || !flow.isGitRepo}
15741585
icon="arrow.triangle.branch"
15751586
label={showBranchLoading ? "Loading branches…" : selectedBranchLabel}
15761587
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: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import {
7979
rememberModelOptions,
8080
withRememberedModelOptions,
8181
} from "../../state/use-model-option-memory";
82-
import { useDebouncedValue, usePaginatedBranches } from "../../state/queries";
82+
import { useBranches, useDebouncedValue, usePaginatedBranches } from "../../state/queries";
8383
import { vcsEnvironment } from "../../state/vcs";
8484
import {
8585
flattenQueuedThreadMessages,
@@ -119,6 +119,7 @@ import {
119119
filterNewTaskBranches,
120120
resolveNewTaskBranchWorktreePath,
121121
resolveNewTaskLocalWorkspaceSelection,
122+
resolveNewTaskWorkspaceSelection,
122123
} from "./new-task-context-presentation";
123124
import { resolveEnvironmentProjectMatch } from "./new-task-project-selection";
124125
import { resolveProjectThreadCreationBranch } from "./projectThreadCreationValidation";
@@ -169,6 +170,8 @@ type NewTaskFlowContextValue = {
169170
readonly selectedProjectKey: string | null;
170171
readonly selectedModelKey: string | null;
171172
readonly workspaceMode: WorkspaceMode;
173+
readonly isGitRepo: boolean;
174+
readonly isGitRepoKnown: boolean;
172175
readonly canChooseWorkspace: boolean;
173176
readonly selectedBranchName: string | null;
174177
readonly selectedWorktreePath: string | null;
@@ -435,6 +438,22 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
435438
const selectedProjectDraft = useComposerDraft(selectedProjectDraftKey);
436439
const prompt = selectedProjectDraft.text;
437440
const attachments = selectedProjectDraft.attachments;
441+
const projectGitStatus = useEnvironmentQuery(
442+
selectedProject !== null && selectedProject.workspaceRoot !== ""
443+
? vcsEnvironment.status({
444+
environmentId: selectedProject.environmentId,
445+
input: { cwd: selectedProject.workspaceRoot },
446+
})
447+
: null,
448+
);
449+
const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;
450+
// The unfiltered ref list also provides repository capability while offline.
451+
const projectBranches = useBranches({
452+
environmentId: selectedProject?.environmentId ?? null,
453+
cwd: selectedProject?.workspaceRoot || null,
454+
});
455+
const knownIsGitRepo = projectGitStatus.data?.isRepo ?? projectBranches.data?.isRepo ?? null;
456+
const isGitRepo = knownIsGitRepo ?? true;
438457
const fileAccessSession = useEnvironmentQuery(
439458
selectedProject !== null && selectedProject.workspaceRoot !== ""
440459
? environmentSession.sessionStateAtom(selectedProject.environmentId)
@@ -497,17 +516,20 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
497516
selectedProjectDraft.workspaceSelection?.mode !== undefined ||
498517
projectSettings.sources.defaultThreadEnvMode !== "environment" ||
499518
(!t3ProjectFileQuery.isPending && !fileAccessPending);
500-
const workspaceMode = canChooseWorkspace
501-
? (selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode)
502-
: "local";
503-
const selectedBranchName = selectedProjectDraft.workspaceSelection?.branch ?? null;
504-
const selectedWorktreePath = selectedProjectDraft.workspaceSelection?.worktreePath ?? null;
519+
const workspaceSelection = resolveNewTaskWorkspaceSelection({
520+
selection: canChooseWorkspace ? selectedProjectDraft.workspaceSelection : undefined,
521+
fallback: { mode: defaultWorkspaceMode, branch: null, worktreePath: null },
522+
isGitRepo,
523+
});
524+
const workspaceMode = workspaceSelection.mode;
525+
const selectedBranchName = workspaceSelection.branch;
526+
const selectedWorktreePath = workspaceSelection.worktreePath;
505527
// Keep the user's explicit choice separate from the resolved display value:
506528
// only the explicit flag is ever written back to the draft, so the resolved
507529
// value keeps tracking the server setting when the config loads late.
508530
const draftStartFromOrigin = selectedProjectDraft.workspaceSelection?.startFromOrigin;
509531
const startFromOrigin =
510-
draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin;
532+
isGitRepo && (draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin);
511533
const defaultRuntimeMode = editingPendingTask
512534
? (editingPendingTask.runtimeMode ?? DEFAULT_RUNTIME_MODE)
513535
: projectSettings.settings.defaultRuntimeMode;
@@ -662,15 +684,6 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
662684
allBranchRefs,
663685
Arr.filter((branch) => !branch.isRemote),
664686
);
665-
const projectGitStatus = useEnvironmentQuery(
666-
branchTarget.environmentId !== null && branchTarget.cwd !== null
667-
? vcsEnvironment.status({
668-
environmentId: branchTarget.environmentId,
669-
input: { cwd: branchTarget.cwd },
670-
})
671-
: null,
672-
);
673-
const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;
674687

675688
const filteredBranches = filterNewTaskBranches(allBranchRefs, branchQuery);
676689

@@ -971,8 +984,12 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
971984
) {
972985
return null;
973986
}
974-
const workspaceSelection = canChooseWorkspace ? draft.workspaceSelection : undefined;
975-
const mode = workspaceSelection?.mode ?? workspaceMode;
987+
const workspaceSelection = resolveNewTaskWorkspaceSelection({
988+
selection: canChooseWorkspace ? draft.workspaceSelection : undefined,
989+
fallback: { mode: workspaceMode, branch: null, worktreePath: null },
990+
isGitRepo,
991+
});
992+
const mode = workspaceSelection.mode;
976993
// When the selection is the stand-in built from the queued snapshot,
977994
// persist the original (possibly absent) snapshot values — the
978995
// stand-in's placeholder title/workspaceRoot must never be written back
@@ -1131,6 +1148,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
11311148
selectedProjectKey,
11321149
selectedModelKey,
11331150
workspaceMode,
1151+
isGitRepo,
1152+
isGitRepoKnown: knownIsGitRepo !== null,
11341153
canChooseWorkspace,
11351154
selectedBranchName,
11361155
selectedWorktreePath,

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

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

798798
export { readFileAsDataUrl } from "../lib/imageCompression";
799799

800-
export function resolveSendEnvMode(input: {
801-
requestedEnvMode: DraftThreadEnvMode;
802-
isGitRepo: boolean;
803-
}): DraftThreadEnvMode {
804-
return input.isGitRepo ? input.requestedEnvMode : "local";
805-
}
800+
export { resolveSendEnvMode } from "@t3tools/shared/projectSettings";
806801

807802
export function resolveBackgroundDraftWorkspaceOptions(input: {
808803
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)