@devries-cameron20 / t3code Mirror · read-only / commits / f2417cde4d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

fix(mobile): await branch refs before choosing an automatic feature branch (#22098)

Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Victor Vogel committed Oct 30, 2026 at 15:36 UTC f2417cde4d6c4ce9490aefafac35b0a11c88a278
3 files changed +91 -4
apps/mobile/src/features/threads/git/GitConfirmSheet.tsx
+1
@@ -94,6 +94,7 @@ export function GitConfirmSheet(props: GitConfirmSheetProps) {
94 gitState.selectedThreadBranches.length > 0
95 ? gitState.selectedThreadBranches
96 : await gitActions.refreshSelectedThreadBranches();
97 + if (branches === null) return;
98 const newBranchName = resolveAutoFeatureBranchName(
99 Arr.filterMap(branches, (branch) =>
100 branch.isRemote ? Result.failVoid : Result.succeed(branch.name),
apps/mobile/src/state/use-selected-thread-git-actions.test.ts
+74 -1
@@ -1,4 +1,6 @@
1 import { AuthOrchestrationOperateScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
2 +import type { VcsListRefsResult } from "@t3tools/contracts";
3 +import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
4 import * as Cause from "effect/Cause";
5 import { AsyncResult } from "effect/reactivity";
6 import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -20,6 +22,8 @@ const state = vi.hoisted(() => ({
22 results: [] as { type: string; description?: string }[],
23 afterGitAction: undefined as (() => void) | undefined,
24 statusRefreshes: 0,
25 + readRefs: vi.fn(),
26 + cachedRefs: [] as VcsListRefsResult["refs"],
27 }));
28
29 vi.mock("react", () => ({
@@ -46,7 +50,7 @@ vi.mock("./use-selected-thread-worktree", () => ({
50 }),
51 }));
52 vi.mock("./queries", () => ({
49 - useBranches: () => ({ data: { refs: [] }, refresh: () => {} }),
53 + useBranches: () => ({ data: { refs: state.cachedRefs }, refresh: () => {} }),
54 }));
55 vi.mock("./use-atom-command", () => ({ useAtomCommand: (command: unknown) => command }));
56 vi.mock("./atom-registry", () => ({ appAtomRegistry: {} }));
@@ -79,6 +83,7 @@ vi.mock("./threads", () => ({
83 }));
84 vi.mock("./vcs", () => ({
85 vcsEnvironment: {
86 + readRefs: state.readRefs,
87 refreshStatus: async () => {
88 state.statusRefreshes += 1;
89 return AsyncResult.success({ refName: state.branch });
@@ -231,3 +236,71 @@ describe("thread Git mutation permissions", () => {
236 },
237 );
238 });
239 +
240 +describe("automatic feature branch refs", () => {
241 + const empty: VcsListRefsResult = {
242 + refs: [],
243 + isRepo: true,
244 + hasPrimaryRemote: true,
245 + nextCursor: null,
246 + totalCount: 0,
247 + };
248 +
249 + it.each([false, true])("awaits fresh refs before naming, cached: %s", async (cached) => {
250 + const request = Promise.withResolvers<VcsListRefsResult>();
251 + state.cachedRefs = cached
252 + ? [
253 + {
254 + name: "old-branch",
255 + isRemote: false,
256 + current: false,
257 + isDefault: false,
258 + worktreePath: null,
259 + },
260 + ]
261 + : [];
262 + state.readRefs.mockReturnValue(request.promise.then(AsyncResult.success));
263 + let settled = false;
264 + const refresh = useSelectedThreadGitActions()
265 + .refreshSelectedThreadBranches()
266 + .then((refs) => {
267 + settled = true;
268 + return refs;
269 + });
270 + await Promise.resolve();
271 + expect(settled).toBe(false);
272 + const local = {
273 + name: "feature/update",
274 + isRemote: false,
275 + current: false,
276 + isDefault: false,
277 + worktreePath: null,
278 + };
279 + request.resolve({
280 + ...empty,
281 + refs: [
282 + local,
283 + { ...local, name: "origin/feature/update", isRemote: true, remoteName: "origin" },
284 + ],
285 + totalCount: 2,
286 + });
287 + const refs = await refresh;
288 + expect(refs).toEqual([local]);
289 + expect(resolveAutoFeatureBranchName(refs!.map((ref) => ref.name))).toBe("feature/update-2");
290 + expect(state.readRefs).toHaveBeenCalledWith({
291 + environmentId: "environment",
292 + input: { cwd: "/repo", limit: 100, refresh: true },
293 + });
294 + });
295 +
296 + it("reports lookup failure instead of returning empty refs", async () => {
297 + state.results = [];
298 + state.readRefs.mockResolvedValue(
299 + AsyncResult.failure(Cause.fail(new Error("Cannot list refs"))),
300 + );
301 + expect(await useSelectedThreadGitActions().refreshSelectedThreadBranches()).toBeNull();
302 + expect(state.results).toEqual([
303 + { type: "error", title: "Git action failed", description: "Cannot list refs" },
304 + ]);
305 + });
306 +});
apps/mobile/src/state/use-selected-thread-git-actions.ts
+16 -3
@@ -56,6 +56,7 @@ export function useSelectedThreadGitActions() {
56 const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
57 reportFailure: false,
58 });
59 + const readRefs = useAtomCommand(vcsEnvironment.readRefs, { reportFailure: false });
60 const refreshStatus = useAtomCommand(vcsEnvironment.refreshStatus, { reportFailure: false });
61 const switchRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
62 const createRef = useAtomCommand(vcsEnvironment.createRef, { reportFailure: false });
@@ -206,9 +207,21 @@ export function useSelectedThreadGitActions() {
207 return result.value;
208 };
209
209 - const refreshSelectedThreadBranches = async (): Promise<ReadonlyArray<VcsRef>> => {
210 - branchState.refresh();
211 - return dedupeRemoteBranchesWithLocalMatches(branchState.data?.refs ?? []).filter(
210 + const refreshSelectedThreadBranches = async (): Promise<ReadonlyArray<VcsRef> | null> => {
211 + if (branchTarget.environmentId === null || branchTarget.cwd === null) return null;
212 + const result = await readRefs({
213 + environmentId: branchTarget.environmentId,
214 + input: { cwd: branchTarget.cwd, limit: 100, refresh: true },
215 + });
216 + if (AsyncResult.isFailure(result)) {
217 + const error = Cause.squash(result.cause);
218 + const message = error instanceof Error ? error.message : "Failed to refresh branches.";
219 + setPendingConnectionError(message);
220 + showGitActionResult({ type: "error", title: "Git action failed", description: message });
221 + return null;
222 + }
223 + setPendingConnectionError(null);
224 + return dedupeRemoteBranchesWithLocalMatches(result.value.refs).filter(
225 (branch) => !branch.isRemote,
226 );
227 };