Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/mobile/src/features/review/ReviewSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,8 @@ export function ReviewSheet(props: ReviewSheetProps) {
)}
/>
)
) : selectedSection.isLoading && selectedSection.diff === null ? (
) : error && selectedSection.diff === null ? null : selectedSection.isLoading &&
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
selectedSection.diff === null ? (
<ContentStatus loading detail={gt("Loading diff…")} />
) : parsedDiff.kind === "empty" ? (
<ContentStatus
Expand Down
39 changes: 39 additions & 0 deletions apps/mobile/src/features/review/reviewModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
import {
applyReviewDiffMetadata,
buildReviewParsedDiff,
buildReviewSectionItems,
resolveReviewSelectedSectionId,
type ReviewSectionItem,
} from "./reviewModel";
Expand Down Expand Up @@ -274,6 +275,44 @@ describe("buildReviewParsedDiff", () => {
});
});

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<ReturnType<typeof source>>,
gitSectionStates: Parameters<typeof buildReviewSectionItems>[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(
Expand Down
72 changes: 40 additions & 32 deletions apps/mobile/src/features/review/reviewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReviewSectionKind, "turn">;
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<ReviewGitSectionKind, unknown>;
const UNCOMMITTED_SUBTITLE = msg("Staged, unstaged, and untracked files");

export interface ReviewSectionItem {
Expand All @@ -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 {
Expand Down Expand Up @@ -533,7 +539,7 @@ export function buildReviewSectionItems(input: {
readonly checkpoints: ReadonlyArray<ThreadCheckpointSummary>;
readonly gitSections: ReadonlyArray<ReviewDiffPreviewSource>;
readonly turnDiffById: Readonly<Record<string, string | undefined>>;
readonly loadingGitSections: boolean;
readonly gitSectionStates: Readonly<Record<ReviewGitSectionKind, "loading" | "failed" | null>>;
readonly resolve?: MessageResolver;
}): ReadonlyArray<ReviewSectionItem> {
const resolve = input.resolve ?? m;
Expand All @@ -551,39 +557,41 @@ export function buildReviewSectionItems(input: {
},
);

const gitItems = input.gitSections
.filter(
(section): section is typeof section & { kind: ReviewSectionKind } =>
section.kind !== "commit",
)
.map<ReviewSectionItem>((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<ReviewSectionItem>((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(
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/features/review/useReviewDiffData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),
Expand Down
61 changes: 50 additions & 11 deletions apps/mobile/src/features/review/useReviewSections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getReviewSectionIdForCheckpoint,
resolveReviewSelectedSectionId,
reviewTurnDiffsBySectionId,
type ReviewGitSectionKind,
type ReviewSectionItem,
} from "./reviewModel";
import { setReviewSelectedSectionId, type ReviewCacheForThread } from "./reviewState";
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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) => {
Expand All @@ -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";
}
17 changes: 12 additions & 5 deletions apps/mobile/src/state/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
);
}),
});
21 changes: 21 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
Loading