diff --git a/apps/mobile/src/features/review/ReviewSheet.tsx b/apps/mobile/src/features/review/ReviewSheet.tsx
index 233fb9f9df01b..19e3c4a9b90fb 100644
--- a/apps/mobile/src/features/review/ReviewSheet.tsx
+++ b/apps/mobile/src/features/review/ReviewSheet.tsx
@@ -722,7 +722,8 @@ export function ReviewSheet(props: ReviewSheetProps) {
)}
/>
)
- ) : selectedSection.isLoading && selectedSection.diff === null ? (
+ ) : error && selectedSection.diff === null ? null : selectedSection.isLoading &&
+ selectedSection.diff === null ? (
) : parsedDiff.kind === "empty" ? (
{
});
});
+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 a8844eab226de..a5d5a18cec341 100644
--- a/apps/mobile/src/features/review/reviewModel.ts
+++ b/apps/mobile/src/features/review/reviewModel.ts
@@ -14,9 +14,15 @@ import { msg } from "gt-react-native";
import { m, type MessageResolver } from "../../i18n/messages";
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 = msg("Changes", { $context: "diff section: branch changes" });
+const GIT_SECTION_KINDS = ["working-tree", "branch-range"] as const;
+const GIT_PLACEHOLDER_TITLES = {
+ "working-tree": msg("Uncommitted", { $context: "diff section: working tree" }),
+ "branch-range": msg("Changes", { $context: "diff section: branch changes" }),
+} as const satisfies Record;
const UNCOMMITTED_SUBTITLE = msg("Staged, unstaged, and untracked files");
export interface ReviewSectionItem {
@@ -28,7 +34,7 @@ export interface ReviewSectionItem {
readonly isLoading: boolean;
readonly files?: ReviewDiffPreviewSource["files"];
readonly truncated?: boolean;
- readonly source?: ReviewDiffPreviewSource;
+ readonly source?: ReviewGitSource;
}
export interface ReviewRenderableHunkRow {
@@ -533,7 +539,7 @@ export function buildReviewSectionItems(input: {
readonly checkpoints: ReadonlyArray;
readonly gitSections: ReadonlyArray;
readonly turnDiffById: Readonly>;
- readonly loadingGitSections: boolean;
+ readonly gitSectionStates: Readonly>;
readonly resolve?: MessageResolver;
}): ReadonlyArray {
const resolve = input.resolve ?? m;
@@ -551,39 +557,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, resolve),
- 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, resolve),
+ 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: resolve(CHANGES_TITLE),
+ id: `git:${kind}`,
+ kind,
+ title: resolve(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 0f3c06381f474..7d72be164a0be 100644
--- a/apps/mobile/src/features/review/useReviewDiffData.ts
+++ b/apps/mobile/src/features/review/useReviewDiffData.ts
@@ -177,7 +177,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 f20944dd1e82a..49b0a8b44c6ac 100644
--- a/apps/mobile/src/features/review/useReviewSections.ts
+++ b/apps/mobile/src/features/review/useReviewSections.ts
@@ -20,6 +20,7 @@ import {
getReviewSectionIdForCheckpoint,
resolveReviewSelectedSectionId,
reviewTurnDiffsBySectionId,
+ type ReviewGitSectionKind,
type ReviewSectionItem,
} from "./reviewModel";
import { setReviewSelectedSectionId, type ReviewCacheForThread } from "./reviewState";
@@ -58,20 +59,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)
@@ -88,10 +102,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),
+ },
resolve: messages,
});
const selectedSectionId = resolveReviewSelectedSectionId(
@@ -130,6 +146,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;
@@ -138,7 +161,12 @@ export function useReviewSections(input: {
activeTurnDiff.refresh();
return;
}
- diffPreview.refresh();
+ if (selectedGitPreview) {
+ selectedGitPreview.refresh();
+ } else {
+ workingTreePreview.refresh();
+ branchPreview.refresh();
+ }
};
const selectSection = (sectionId: string) => {
@@ -149,16 +177,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 ?? messages(msg("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 c51f6fef60260..8846ed29a818f 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -3345,6 +3345,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 a4bde8c983817..de990226ebacb 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -4331,6 +4331,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 &&