From 5e9c6956e9b49b8e794a54c2e1e7d0cfc9e5037b Mon Sep 17 00:00:00 2001 From: Merrkry Date: Tue, 10 Nov 2026 11:24:56 +0100 Subject: [PATCH] fix(mobile): a failed branch comparison no longer hides Uncommitted changes Mobile review loaded every git source in one preview request, so a branch comparison that failed, such as a base sharing no history with HEAD, made the whole review unavailable. Mobile now requests the working-tree and branch-range sources separately. Each source keeps its own cached copy and its own loading or failed placeholder, so a failed source stays selectable with its error and refresh while the other keeps working. The server rejects a preview request whose file selector names a different source than its top-level sourceKind instead of guessing which one the caller meant. Co-authored-by: maria-rcks --- .../src/features/review/ReviewSheet.tsx | 3 +- .../src/features/review/reviewModel.test.ts | 39 ++++++++++ .../mobile/src/features/review/reviewModel.ts | 72 ++++++++++--------- .../src/features/review/useReviewDiffData.ts | 2 +- .../src/features/review/useReviewSections.ts | 61 +++++++++++++--- apps/mobile/src/state/review.ts | 17 +++-- apps/server/src/vcs/GitVcsDriverCore.test.ts | 21 ++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 12 ++++ 8 files changed, 177 insertions(+), 50 deletions(-) diff --git a/apps/mobile/src/features/review/ReviewSheet.tsx b/apps/mobile/src/features/review/ReviewSheet.tsx index a953ca80901fa..0db132b865297 100644 --- a/apps/mobile/src/features/review/ReviewSheet.tsx +++ b/apps/mobile/src/features/review/ReviewSheet.tsx @@ -741,7 +741,8 @@ export function ReviewSheet(props: ReviewSheetProps) { ) - ) : selectedSection.isLoading && selectedSection.diff === null ? ( + ) : error && selectedSection.diff === null ? null : selectedSection.isLoading && + selectedSection.diff === null ? ( { }); }); +describe("buildReviewSectionItems", () => { + it("keeps a failed or loading git source selectable beside the other one", () => { + const source = (kind: "working-tree" | "branch-range") => ({ + id: kind, + kind, + title: kind === "working-tree" ? "Uncommitted" : "Changes", + baseRef: "HEAD", + headRef: null, + diff: "diff --git a/a.ts b/a.ts\n", + diffHash: "hash", + truncated: false, + }); + const build = ( + gitSections: ReadonlyArray>, + gitSectionStates: Parameters[0]["gitSectionStates"], + ) => + buildReviewSectionItems({ + checkpoints: [], + gitSections, + turnDiffById: {}, + gitSectionStates, + }).map((section) => [section.id, section.diff === null, section.isLoading]); + + expect( + build([source("working-tree")], { "working-tree": null, "branch-range": "failed" }), + ).toEqual([ + ["git:working-tree", false, false], + ["git:branch-range", true, false], + ]); + expect( + build([source("branch-range")], { "working-tree": "loading", "branch-range": null }), + ).toEqual([ + ["git:working-tree", true, true], + ["git:branch-range", false, false], + ]); + }); +}); + describe("applyReviewDiffMetadata", () => { it("uses complete counts even when the preview contains only part of one file", () => { const parsed = buildReviewParsedDiff( diff --git a/apps/mobile/src/features/review/reviewModel.ts b/apps/mobile/src/features/review/reviewModel.ts index df3c7c51324b4..30f0fd72da535 100644 --- a/apps/mobile/src/features/review/reviewModel.ts +++ b/apps/mobile/src/features/review/reviewModel.ts @@ -11,9 +11,15 @@ import { pipe } from "effect/Function"; import * as Order from "effect/Order"; export type ReviewSectionKind = "turn" | "working-tree" | "branch-range"; +export type ReviewGitSectionKind = Exclude; +type ReviewGitSource = ReviewDiffPreviewSource & { readonly kind: ReviewGitSectionKind }; const CHANGES_SECTION_ID = "git:branch-range"; -const CHANGES_TITLE = "Changes"; +const GIT_SECTION_KINDS = ["working-tree", "branch-range"] as const; +const GIT_PLACEHOLDER_TITLES = { + "working-tree": "Uncommitted", + "branch-range": "Changes", +} as const satisfies Record; const UNCOMMITTED_SUBTITLE = "Staged, unstaged, and untracked files"; export interface ReviewSectionItem { @@ -25,7 +31,7 @@ export interface ReviewSectionItem { readonly isLoading: boolean; readonly files?: ReviewDiffPreviewSource["files"]; readonly truncated?: boolean; - readonly source?: ReviewDiffPreviewSource; + readonly source?: ReviewGitSource; } export interface ReviewRenderableHunkRow { @@ -518,7 +524,7 @@ export function buildReviewSectionItems(input: { readonly checkpoints: ReadonlyArray; readonly gitSections: ReadonlyArray; readonly turnDiffById: Readonly>; - readonly loadingGitSections: boolean; + readonly gitSectionStates: Readonly>; }): ReadonlyArray { const turnItems = getReadyReviewCheckpoints(input.checkpoints).map( (checkpoint) => { @@ -534,39 +540,41 @@ export function buildReviewSectionItems(input: { }, ); - const gitItems = input.gitSections - .filter( - (section): section is typeof section & { kind: ReviewSectionKind } => - section.kind !== "commit", - ) - .map((section) => ({ - id: `git:${section.kind}`, - kind: section.kind, - title: section.title, - subtitle: gitSubtitle(section), - diff: section.diff, - source: section, - ...(section.files ? { files: section.files } : {}), - truncated: section.truncated, - isLoading: false, - })); - const hasChangesItem = gitItems.some((item) => item.id === CHANGES_SECTION_ID); - const visibleGitItems = - input.loadingGitSections && !hasChangesItem - ? [ + const gitItems = GIT_SECTION_KINDS.flatMap((kind) => { + const section = input.gitSections.find( + (candidate): candidate is ReviewGitSource => candidate.kind === kind, + ); + if (section) { + return [ + { + id: `git:${kind}`, + kind, + title: section.title, + subtitle: gitSubtitle(section), + diff: section.diff, + source: section, + ...(section.files ? { files: section.files } : {}), + truncated: section.truncated, + isLoading: false, + }, + ]; + } + const state = input.gitSectionStates[kind]; + return state === null + ? [] + : [ { - id: CHANGES_SECTION_ID, - kind: "branch-range", - title: CHANGES_TITLE, + id: `git:${kind}`, + kind, + title: GIT_PLACEHOLDER_TITLES[kind], subtitle: null, diff: null, - isLoading: true, - } satisfies ReviewSectionItem, - ...gitItems, - ] - : gitItems; + isLoading: state === "loading", + }, + ]; + }); - return [...turnItems, ...visibleGitItems]; + return [...turnItems, ...gitItems]; } export function getDefaultReviewSectionId( diff --git a/apps/mobile/src/features/review/useReviewDiffData.ts b/apps/mobile/src/features/review/useReviewDiffData.ts index d9ec656cbc773..8e32b794931f0 100644 --- a/apps/mobile/src/features/review/useReviewDiffData.ts +++ b/apps/mobile/src/features/review/useReviewDiffData.ts @@ -165,7 +165,7 @@ export function useReviewDiffData(input: { input: { cacheKey: scope, // Newer preview revisions refresh patches read under an older one. - preview: { cwd }, + preview: { cwd, sourceKind: lazySource.kind }, request: { cwd, ...(lazySource.baseRef ? { baseRef: lazySource.baseRef } : {}), diff --git a/apps/mobile/src/features/review/useReviewSections.ts b/apps/mobile/src/features/review/useReviewSections.ts index 4fd506732e5d3..3939b8fbc9a59 100644 --- a/apps/mobile/src/features/review/useReviewSections.ts +++ b/apps/mobile/src/features/review/useReviewSections.ts @@ -19,6 +19,7 @@ import { getReviewSectionIdForCheckpoint, resolveReviewSelectedSectionId, reviewTurnDiffsBySectionId, + type ReviewGitSectionKind, type ReviewSectionItem, } from "./reviewModel"; import { setReviewSelectedSectionId, type ReviewCacheForThread } from "./reviewState"; @@ -53,20 +54,33 @@ export function useReviewSections(input: { const { canReadFiles } = fileAccess; const selectedThread = useSelectedThreadProjection(); const { selectedThreadCwd } = useSelectedThreadWorktree(); - const diffPreview = useEnvironmentQuery( + const diffPreviewQuery = (sourceKind: ReviewGitSectionKind) => canReadFiles && enabled && environmentId !== undefined && selectedThreadCwd !== null ? reviewEnvironment.diffPreview({ environmentId, - input: { cwd: selectedThreadCwd }, + input: { cwd: selectedThreadCwd, sourceKind }, }) - : null, - ); + : null; + const workingTreePreview = useEnvironmentQuery(diffPreviewQuery("working-tree")); + const branchPreview = useEnvironmentQuery(diffPreviewQuery("branch-range")); const lastGitSections = useAtomValue( environmentId !== undefined && selectedThreadCwd !== null ? lastReviewGitSectionsAtom(environmentId, selectedThreadCwd) : NO_GIT_SECTIONS_ATOM, ); + const gitSections = ( + [ + ["working-tree", workingTreePreview], + ["branch-range", branchPreview], + ] as const + ).flatMap(([kind, preview]) => + (preview.data?.incomplete + ? lastGitSections + : (preview.data?.sources ?? lastGitSections) + ).filter((source) => source.kind === kind), + ); + const lastTurnDiffs = useAtomValue( environmentId !== undefined && threadId !== undefined ? lastReviewTurnDiffsAtom(environmentId, threadId) @@ -83,10 +97,12 @@ export function useReviewSections(input: { // Section ids and the selection do not depend on the live query, so they are resolved first. const cachedSections = buildReviewSectionItems({ checkpoints: readyCheckpoints, - gitSections: - canReadFiles || fileAccess.isPending ? (diffPreview.data?.sources ?? lastGitSections) : [], + gitSections: canReadFiles || fileAccess.isPending ? gitSections : [], turnDiffById: reviewTurnDiffsBySectionId(readyCheckpoints, lastTurnDiffs), - loadingGitSections: fileAccess.isPending || diffPreview.isPending, + gitSectionStates: { + "working-tree": gitSectionState(fileAccess.isPending, workingTreePreview), + "branch-range": gitSectionState(fileAccess.isPending, branchPreview), + }, }); const selectedSectionId = resolveReviewSelectedSectionId( cachedSections, @@ -124,6 +140,13 @@ export function useReviewSections(input: { const selectedSection = reviewSections.find((section) => section.id === selectedSectionId) ?? null; + const selectedGitPreview = + selectedSection?.kind === "working-tree" + ? workingTreePreview + : selectedSection?.kind === "branch-range" + ? branchPreview + : null; + const refreshSelectedSection = async () => { if (!enabled) { return; @@ -132,7 +155,12 @@ export function useReviewSections(input: { activeTurnDiff.refresh(); return; } - diffPreview.refresh(); + if (selectedGitPreview) { + selectedGitPreview.refresh(); + } else { + workingTreePreview.refresh(); + branchPreview.refresh(); + } }; const selectSection = (sectionId: string) => { @@ -143,16 +171,27 @@ export function useReviewSections(input: { return { error: - diffPreview.error ?? - activeTurnDiff.error ?? + (selectedSection?.kind === "turn" + ? activeTurnDiff.error + : (selectedGitPreview?.error ?? null)) ?? (selectedSection === null && !fileAccess.isPending && !canReadFiles ? (fileAccess.error ?? "This connection cannot read local diffs.") : null), isSelectedSectionPending: - selectedSection?.kind === "turn" ? activeTurnDiff.isPending : diffPreview.isPending, + selectedSection?.kind === "turn" + ? activeTurnDiff.isPending + : (selectedGitPreview?.isPending ?? false), reviewSections, selectedSection, refreshSelectedSection, selectSection, }; } + +function gitSectionState( + isFileAccessPending: boolean, + preview: { readonly isPending: boolean; readonly error: string | null }, +) { + if (isFileAccessPending || preview.isPending) return "loading"; + return preview.error === null ? null : "failed"; +} diff --git a/apps/mobile/src/state/review.ts b/apps/mobile/src/state/review.ts index 2f4a1a5efe067..60564152bee68 100644 --- a/apps/mobile/src/state/review.ts +++ b/apps/mobile/src/state/review.ts @@ -22,10 +22,17 @@ export function lastReviewGitSectionsAtom(environmentId: EnvironmentId, cwd: str export const reviewEnvironment = createReviewEnvironmentAtoms(connectionAtomRuntime, { onDiffPreviewAnswer: (target, value, registry) => - Effect.sync(() => + Effect.sync(() => { + const atom = lastReviewGitSectionsAtom(target.environmentId, target.input.cwd); + const kind = target.input.sourceKind; registry.set( - lastReviewGitSectionsAtom(target.environmentId, target.input.cwd), - value.sources, - ), - ), + atom, + kind === undefined + ? value.sources + : [ + ...registry.get(atom).filter((source) => source.kind !== kind), + ...value.sources.filter((source) => source.kind === kind), + ], + ); + }), }); diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index baf1368054d4e..f2c1ce4802ffb 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2414,6 +2414,27 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("rejects a file selector for a different source than the request", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + yield* writeTextFile(cwd, "README.md", "dirty\n"); + const driver = yield* GitVcsDriver.GitVcsDriver; + const file = { path: "README.md", previousPath: null }; + + for (const [sourceKind, fileKind] of [ + ["working-tree", "branch-range"], + ["branch-range", "working-tree"], + ] as const) { + const conflict = yield* driver + .getReviewDiffPreview({ cwd, sourceKind, file: { ...file, sourceKind: fileKind } }) + .pipe(Effect.flip); + assert.equal(conflict.operation, "GitVcsDriver.getReviewDiffPreview"); + assert.equal(conflict.detail, "Review source selectors must agree."); + } + }), + ); + it.effect("reads complete tracked and untracked manifests beyond 1 MB", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index ef91660a0e9a7..f7d9ccb2e5075 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -3442,6 +3442,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const patchLimit = input.file ? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES : REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES; + if ( + input.sourceKind !== undefined && + input.file && + input.file.sourceKind !== input.sourceKind + ) { + return yield* new GitCommandError({ + operation: "GitVcsDriver.getReviewDiffPreview", + command: "git diff", + cwd: input.cwd, + detail: "Review source selectors must agree.", + }); + } const sourceKind = input.file?.sourceKind ?? input.sourceKind; const needsCurrentBranch = input.commitSha === undefined &&