Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77c6ec7b6e | ||
|
|
c50b216bbd | ||
|
|
6a604d8c75 |
No files matched your search
@@ -27,11 +27,10 @@ import {
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useDebouncedValue } from "@tanstack/react-pacer";
|
||||
import { gitBranchesQueryOptions, gitCreateWorktreeMutationOptions } from "~/lib/gitReactQuery";
|
||||
import { projectSearchEntriesQueryOptions } from "~/lib/projectReactQuery";
|
||||
import { checkpointDiffQueryOptions } from "~/lib/providerReactQuery";
|
||||
import { serverConfigQueryOptions, serverQueryKeys } from "~/lib/serverReactQuery";
|
||||
|
||||
import { isElectron } from "../env";
|
||||
@@ -50,7 +49,6 @@ import {
|
||||
resolveModelSlug,
|
||||
} from "../model-logic";
|
||||
import {
|
||||
countDiffStat,
|
||||
derivePendingApprovals,
|
||||
derivePhase,
|
||||
deriveTimelineEntries,
|
||||
@@ -374,6 +372,7 @@ export default function ChatView() {
|
||||
const dragDepthRef = useRef(0);
|
||||
const terminalOpenByThreadRef = useRef<Record<string, boolean>>({});
|
||||
const checkpointHydrationSessionRequestRef = useRef(new Set<string>());
|
||||
const checkpointTurnCountSyncFingerprintRef = useRef(new Map<string, string>());
|
||||
|
||||
const activeThread = state.threads.find((t) => t.id === state.activeThreadId);
|
||||
const activeThreadId = activeThread?.id ?? null;
|
||||
@@ -486,68 +485,6 @@ export default function ChatView() {
|
||||
});
|
||||
}, [activeThreadId, dispatch, inferredCheckpointTurnCountByTurnId, turnDiffSummaries]);
|
||||
|
||||
const checkpointDiffHydrationTargets = useMemo(() => {
|
||||
if (!api || !activeThreadId || !activeSessionId || turnDiffSummaries.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return turnDiffSummaries.flatMap((summary) => {
|
||||
const turnCount = inferredCheckpointTurnCountByTurnId[summary.turnId];
|
||||
if (summary.checkpointDiffLoaded || typeof turnCount !== "number") {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
{
|
||||
turnId: summary.turnId,
|
||||
fromTurnCount: Math.max(0, turnCount - 1),
|
||||
toTurnCount: turnCount,
|
||||
},
|
||||
];
|
||||
});
|
||||
}, [
|
||||
activeSessionId,
|
||||
activeThreadId,
|
||||
api,
|
||||
inferredCheckpointTurnCountByTurnId,
|
||||
turnDiffSummaries,
|
||||
]);
|
||||
|
||||
const checkpointDiffHydrationQueries = useQueries({
|
||||
queries: checkpointDiffHydrationTargets.map((target) =>
|
||||
checkpointDiffQueryOptions(api, {
|
||||
sessionId: activeSessionId,
|
||||
threadRuntimeId: activeThreadRuntimeId,
|
||||
fromTurnCount: target.fromTurnCount,
|
||||
toTurnCount: target.toTurnCount,
|
||||
cacheScope: `turn:${target.turnId}`,
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeThreadId || checkpointDiffHydrationTargets.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const checkpointDiffByTurnId: Record<string, string> = {};
|
||||
for (let index = 0; index < checkpointDiffHydrationTargets.length; index += 1) {
|
||||
const target = checkpointDiffHydrationTargets[index];
|
||||
const query = checkpointDiffHydrationQueries[index];
|
||||
const diff = query?.data?.diff;
|
||||
if (!target || !diff) {
|
||||
continue;
|
||||
}
|
||||
checkpointDiffByTurnId[target.turnId] = diff;
|
||||
}
|
||||
if (Object.keys(checkpointDiffByTurnId).length === 0) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_THREAD_TURN_CHECKPOINT_DIFFS",
|
||||
threadId: activeThreadId,
|
||||
checkpointDiffByTurnId,
|
||||
});
|
||||
}, [activeThreadId, checkpointDiffHydrationQueries, checkpointDiffHydrationTargets, dispatch]);
|
||||
|
||||
const completionSummary = useMemo(() => {
|
||||
if (!activeThread?.latestTurnStartedAt) return null;
|
||||
if (!activeThread.latestTurnCompletedAt) return null;
|
||||
@@ -1400,6 +1337,53 @@ export default function ChatView() {
|
||||
focusComposer();
|
||||
};
|
||||
|
||||
const syncCheckpointTurnCounts = useCallback(
|
||||
async (input: {
|
||||
threadId: string;
|
||||
sessionId: string;
|
||||
threadRuntimeId: string | null;
|
||||
}): Promise<void> => {
|
||||
if (!api || turnDiffSummaries.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const turnIds = turnDiffSummaries.map((summary) => summary.turnId);
|
||||
if (turnIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const syncKey = `${input.threadId}:${input.sessionId}:${input.threadRuntimeId ?? "none"}`;
|
||||
const turnFingerprint = turnIds.join(",");
|
||||
if (checkpointTurnCountSyncFingerprintRef.current.get(syncKey) === turnFingerprint) {
|
||||
return;
|
||||
}
|
||||
checkpointTurnCountSyncFingerprintRef.current.set(syncKey, turnFingerprint);
|
||||
|
||||
try {
|
||||
const result = await api.providers.listCheckpoints({
|
||||
sessionId: input.sessionId,
|
||||
});
|
||||
const turnIdSet = new Set(turnIds);
|
||||
const checkpointTurnCountByTurnId = Object.fromEntries(
|
||||
result.checkpoints
|
||||
.filter((checkpoint) => checkpoint.turnCount > 0 && turnIdSet.has(checkpoint.id))
|
||||
.map((checkpoint) => [checkpoint.id, checkpoint.turnCount] as const),
|
||||
);
|
||||
if (Object.keys(checkpointTurnCountByTurnId).length === 0) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_THREAD_TURN_CHECKPOINT_COUNTS",
|
||||
threadId: input.threadId,
|
||||
checkpointTurnCountByTurnId,
|
||||
});
|
||||
} catch {
|
||||
checkpointTurnCountSyncFingerprintRef.current.delete(syncKey);
|
||||
}
|
||||
},
|
||||
[api, dispatch, turnDiffSummaries],
|
||||
);
|
||||
|
||||
const ensureSession = useCallback(
|
||||
async (cwdOverride?: string): Promise<EnsuredSessionInfo | null> => {
|
||||
if (!api || !activeThread || !activeProject) return null;
|
||||
@@ -1411,6 +1395,11 @@ export default function ChatView() {
|
||||
: sessionThreadId === activeThread.codexThreadId
|
||||
? "resumed"
|
||||
: "fallback_new";
|
||||
await syncCheckpointTurnCounts({
|
||||
threadId: activeThread.id,
|
||||
sessionId: activeThread.session.sessionId,
|
||||
threadRuntimeId: sessionThreadId,
|
||||
});
|
||||
return {
|
||||
sessionId: activeThread.session.sessionId,
|
||||
resolvedThreadId: sessionThreadId,
|
||||
@@ -1441,6 +1430,11 @@ export default function ChatView() {
|
||||
: resolvedThreadId === priorCodexThreadId
|
||||
? "resumed"
|
||||
: "fallback_new";
|
||||
await syncCheckpointTurnCounts({
|
||||
threadId: activeThread.id,
|
||||
sessionId: session.sessionId,
|
||||
threadRuntimeId: resolvedThreadId,
|
||||
});
|
||||
return {
|
||||
sessionId: session.sessionId,
|
||||
resolvedThreadId,
|
||||
@@ -1465,6 +1459,7 @@ export default function ChatView() {
|
||||
runtimeApprovalPolicy,
|
||||
runtimeSandboxMode,
|
||||
selectedModel,
|
||||
syncCheckpointTurnCounts,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -1473,22 +1468,7 @@ export default function ChatView() {
|
||||
if (phase === "running" || isSending || isConnecting || isRevertingCheckpoint) return;
|
||||
const hasThreadIdentity = Boolean(activeThreadRuntimeId);
|
||||
if (!hasThreadIdentity) return;
|
||||
const selectedDiffTurnId =
|
||||
state.diffOpen && state.diffThreadId === activeThreadId ? state.diffTurnId : null;
|
||||
const selectedDiffTurn =
|
||||
selectedDiffTurnId === null
|
||||
? undefined
|
||||
: (turnDiffSummaries.find((summary) => summary.turnId === selectedDiffTurnId) ??
|
||||
turnDiffSummaries[0]);
|
||||
const selectedTurnMissingPatchBody = Boolean(
|
||||
selectedDiffTurn &&
|
||||
!selectedDiffTurn.unifiedDiff &&
|
||||
selectedDiffTurn.files.every((file) => !file.diff),
|
||||
);
|
||||
const hasPendingDiffHydration =
|
||||
turnDiffSummaries.some((summary) => !summary.checkpointDiffLoaded) ||
|
||||
selectedTurnMissingPatchBody;
|
||||
if (!hasPendingDiffHydration) return;
|
||||
if (turnDiffSummaries.length === 0) return;
|
||||
|
||||
const requestKey = `${activeThreadId}:${activeThreadRuntimeId ?? "none"}`;
|
||||
if (checkpointHydrationSessionRequestRef.current.has(requestKey)) {
|
||||
@@ -1515,9 +1495,6 @@ export default function ChatView() {
|
||||
isSending,
|
||||
phase,
|
||||
setThreadError,
|
||||
state.diffOpen,
|
||||
state.diffThreadId,
|
||||
state.diffTurnId,
|
||||
turnDiffSummaries,
|
||||
]);
|
||||
|
||||
@@ -2645,45 +2622,32 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
timelineEntry.message.id,
|
||||
);
|
||||
if (!turnSummary) return null;
|
||||
const isCheckpointDiffLoading =
|
||||
!turnSummary.checkpointDiffLoaded && turnSummary.files.length === 0;
|
||||
const summaryStat = turnSummary.unifiedDiff
|
||||
? countDiffStat(turnSummary.unifiedDiff)
|
||||
: turnSummary.files.reduce(
|
||||
(acc, file) => {
|
||||
const next =
|
||||
typeof file.additions === "number" && typeof file.deletions === "number"
|
||||
? { additions: file.additions, deletions: file.deletions }
|
||||
: file.diff
|
||||
? countDiffStat(file.diff)
|
||||
: null;
|
||||
if (!next) {
|
||||
return acc;
|
||||
}
|
||||
return {
|
||||
additions: acc.additions + next.additions,
|
||||
deletions: acc.deletions + next.deletions,
|
||||
};
|
||||
},
|
||||
{ additions: 0, deletions: 0 },
|
||||
);
|
||||
const changedFileCountLabel = isCheckpointDiffLoading
|
||||
? "..."
|
||||
: String(turnSummary.files.length);
|
||||
const summaryStat = turnSummary.files.reduce(
|
||||
(acc, file) => {
|
||||
if (typeof file.additions !== "number" || typeof file.deletions !== "number") {
|
||||
return acc;
|
||||
}
|
||||
return {
|
||||
additions: acc.additions + file.additions,
|
||||
deletions: acc.deletions + file.deletions,
|
||||
};
|
||||
},
|
||||
{ additions: 0, deletions: 0 },
|
||||
);
|
||||
const changedFileCountLabel = String(turnSummary.files.length);
|
||||
return (
|
||||
<div className="mt-2 rounded-lg border border-border/80 bg-card/45 p-2.5">
|
||||
<div className="mb-1.5 flex items-center justify-between gap-2">
|
||||
<p className="text-[10px] uppercase tracking-[0.12em] text-muted-foreground/65">
|
||||
<span>Changed files ({changedFileCountLabel})</span>
|
||||
{!isCheckpointDiffLoading &&
|
||||
(summaryStat.additions > 0 || summaryStat.deletions > 0) && (
|
||||
<>
|
||||
<span className="mx-1">•</span>
|
||||
<span className="text-success">+{summaryStat.additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">/</span>
|
||||
<span className="text-destructive">-{summaryStat.deletions}</span>
|
||||
</>
|
||||
)}
|
||||
{(summaryStat.additions > 0 || summaryStat.deletions > 0) && (
|
||||
<>
|
||||
<span className="mx-1">•</span>
|
||||
<span className="text-success">+{summaryStat.additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">/</span>
|
||||
<span className="text-destructive">-{summaryStat.deletions}</span>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
<Button
|
||||
type="button"
|
||||
@@ -2696,11 +2660,6 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
View diff
|
||||
</Button>
|
||||
</div>
|
||||
{isCheckpointDiffLoading && (
|
||||
<p className="mb-1.5 text-[11px] text-muted-foreground/70">
|
||||
Loading checkpoint diff...
|
||||
</p>
|
||||
)}
|
||||
{turnSummary.files.length > 0 ? (
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{turnSummary.files.map((file) => (
|
||||
@@ -2715,9 +2674,7 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
typeof file.additions === "number" &&
|
||||
typeof file.deletions === "number"
|
||||
? { additions: file.additions, deletions: file.deletions }
|
||||
: file.diff
|
||||
? countDiffStat(file.diff)
|
||||
: null;
|
||||
: null;
|
||||
if (!stat) {
|
||||
return file.path;
|
||||
}
|
||||
@@ -2735,9 +2692,9 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : !isCheckpointDiffLoading ? (
|
||||
) : (
|
||||
<p className="text-[11px] text-muted-foreground/70">No changed files.</p>
|
||||
) : null}
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})()}
|
||||
|
||||
@@ -5,10 +5,10 @@ import {
|
||||
Virtualizer,
|
||||
WorkerPoolContextProvider,
|
||||
} from "@pierre/diffs/react";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Columns2Icon, Rows3Icon } from "lucide-react";
|
||||
import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { checkpointDiffQueryOptions, providerQueryKeys } from "~/lib/providerReactQuery";
|
||||
import { checkpointDiffQueryOptions } from "~/lib/providerReactQuery";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { isElectron } from "../env";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
@@ -102,7 +102,6 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
const api = useNativeApi();
|
||||
const { resolvedTheme } = useTheme();
|
||||
const { state, dispatch } = useStore();
|
||||
const queryClient = useQueryClient();
|
||||
const [diffRenderMode, setDiffRenderMode] = useState<DiffRenderMode>("stacked");
|
||||
const patchViewportRef = useRef<HTMLDivElement>(null);
|
||||
const activeThread = state.threads.find((thread) => thread.id === state.activeThreadId);
|
||||
@@ -189,99 +188,7 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
? "Failed to load checkpoint diff."
|
||||
: null;
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeThread?.id || !selectedTurn || !selectedTurnCheckpointDiff) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_THREAD_TURN_CHECKPOINT_DIFFS",
|
||||
threadId: activeThread.id,
|
||||
checkpointDiffByTurnId: {
|
||||
[selectedTurn.turnId]: selectedTurnCheckpointDiff,
|
||||
},
|
||||
});
|
||||
}, [activeThread?.id, dispatch, selectedTurn, selectedTurnCheckpointDiff]);
|
||||
|
||||
const selectedPatch = useMemo(() => {
|
||||
const patchForSummary = (summary: (typeof turnDiffSummaries)[number]): string | undefined => {
|
||||
const checkpointTurnCount =
|
||||
summary.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[summary.turnId];
|
||||
if (activeSessionId && typeof checkpointTurnCount === "number") {
|
||||
const checkpointPatch = queryClient.getQueryData<{ diff: string }>(
|
||||
providerQueryKeys.checkpointDiff({
|
||||
sessionId: activeSessionId,
|
||||
threadRuntimeId: activeThreadRuntimeId,
|
||||
fromTurnCount: Math.max(0, checkpointTurnCount - 1),
|
||||
toTurnCount: checkpointTurnCount,
|
||||
cacheScope: `turn:${summary.turnId}`,
|
||||
}),
|
||||
)?.diff;
|
||||
if (checkpointPatch) {
|
||||
return checkpointPatch;
|
||||
}
|
||||
}
|
||||
if (summary.unifiedDiff) {
|
||||
return summary.unifiedDiff;
|
||||
}
|
||||
const filePatches = summary.files
|
||||
.map((file) => file.diff?.trim())
|
||||
.filter((patch): patch is string => Boolean(patch));
|
||||
if (filePatches.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return filePatches.join("\n\n");
|
||||
};
|
||||
|
||||
if (selectedTurn) {
|
||||
return selectedTurnCheckpointDiff ?? patchForSummary(selectedTurn);
|
||||
}
|
||||
|
||||
if (conversationCheckpointDiff) {
|
||||
return conversationCheckpointDiff;
|
||||
}
|
||||
|
||||
// Fallback when a conversation checkpoint diff isn't available yet:
|
||||
// keep one patch per file path (latest change wins) so files aren't duplicated.
|
||||
const latestPatchByPath = new Map<string, string>();
|
||||
for (const summary of turnDiffSummaries) {
|
||||
for (const file of summary.files) {
|
||||
if (latestPatchByPath.has(file.path)) {
|
||||
continue;
|
||||
}
|
||||
const patch = file.diff?.trim();
|
||||
if (!patch) {
|
||||
continue;
|
||||
}
|
||||
latestPatchByPath.set(file.path, patch);
|
||||
}
|
||||
}
|
||||
if (latestPatchByPath.size > 0) {
|
||||
return Array.from(latestPatchByPath.entries())
|
||||
.toSorted(([leftPath], [rightPath]) =>
|
||||
leftPath.localeCompare(rightPath, undefined, { numeric: true, sensitivity: "base" }),
|
||||
)
|
||||
.map(([, patch]) => patch)
|
||||
.join("\n\n");
|
||||
}
|
||||
|
||||
const patches = turnDiffSummaries
|
||||
.toReversed()
|
||||
.map((summary) => patchForSummary(summary)?.trim())
|
||||
.filter((patch): patch is string => Boolean(patch));
|
||||
if (patches.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return patches.join("\n\n");
|
||||
}, [
|
||||
activeSessionId,
|
||||
activeThreadRuntimeId,
|
||||
conversationCheckpointDiff,
|
||||
inferredCheckpointTurnCountByTurnId,
|
||||
queryClient,
|
||||
selectedTurn,
|
||||
selectedTurnCheckpointDiff,
|
||||
turnDiffSummaries,
|
||||
]);
|
||||
const selectedPatch = selectedTurn ? selectedTurnCheckpointDiff : conversationCheckpointDiff;
|
||||
const renderablePatch = useMemo(() => getRenderablePatch(selectedPatch), [selectedPatch]);
|
||||
const renderableFiles = useMemo(() => {
|
||||
if (!renderablePatch || renderablePatch.kind !== "files") {
|
||||
|
||||
@@ -52,4 +52,60 @@ describe("checkpointDiffQueryOptions", () => {
|
||||
toTurnCount: 4,
|
||||
});
|
||||
});
|
||||
|
||||
it("retries checkpoint-not-ready errors longer than generic failures", () => {
|
||||
const options = checkpointDiffQueryOptions(undefined, {
|
||||
sessionId: "session-id",
|
||||
threadRuntimeId: "thread-id",
|
||||
fromTurnCount: 1,
|
||||
toTurnCount: 2,
|
||||
cacheScope: "turn:abc",
|
||||
});
|
||||
const retry = options.retry;
|
||||
expect(typeof retry).toBe("function");
|
||||
if (typeof retry !== "function") {
|
||||
throw new Error("Expected retry to be a function.");
|
||||
}
|
||||
|
||||
expect(retry(1, new Error("Checkpoint turn count 2 exceeds current turn count 1."))).toBe(true);
|
||||
expect(
|
||||
retry(
|
||||
11,
|
||||
new Error("Filesystem checkpoint is unavailable for turn 2 in thread thread-1."),
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
retry(
|
||||
12,
|
||||
new Error("Filesystem checkpoint is unavailable for turn 2 in thread thread-1."),
|
||||
),
|
||||
).toBe(false);
|
||||
expect(retry(2, new Error("Something else failed."))).toBe(true);
|
||||
expect(retry(3, new Error("Something else failed."))).toBe(false);
|
||||
});
|
||||
|
||||
it("backs off longer for checkpoint-not-ready errors", () => {
|
||||
const options = checkpointDiffQueryOptions(undefined, {
|
||||
sessionId: "session-id",
|
||||
threadRuntimeId: "thread-id",
|
||||
fromTurnCount: 1,
|
||||
toTurnCount: 2,
|
||||
cacheScope: "turn:abc",
|
||||
});
|
||||
const retryDelay = options.retryDelay;
|
||||
expect(typeof retryDelay).toBe("function");
|
||||
if (typeof retryDelay !== "function") {
|
||||
throw new Error("Expected retryDelay to be a function.");
|
||||
}
|
||||
|
||||
const checkpointDelay = retryDelay(
|
||||
4,
|
||||
new Error("Checkpoint turn count 2 exceeds current turn count 1."),
|
||||
);
|
||||
const genericDelay = retryDelay(4, new Error("Network failure"));
|
||||
|
||||
expect(typeof checkpointDelay).toBe("number");
|
||||
expect(typeof genericDelay).toBe("number");
|
||||
expect((checkpointDelay ?? 0) > (genericDelay ?? 0)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -23,6 +23,25 @@ export const providerQueryKeys = {
|
||||
] as const,
|
||||
};
|
||||
|
||||
function asCheckpointErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
if (typeof error === "string") {
|
||||
return error;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function isCheckpointTemporarilyUnavailable(error: unknown): boolean {
|
||||
const message = asCheckpointErrorMessage(error).toLowerCase();
|
||||
return (
|
||||
message.includes("exceeds current turn count") ||
|
||||
message.includes("checkpoint is unavailable for turn") ||
|
||||
message.includes("filesystem checkpoint is unavailable")
|
||||
);
|
||||
}
|
||||
|
||||
export function checkpointDiffQueryOptions(
|
||||
api: NativeApi | undefined,
|
||||
input: CheckpointDiffQueryInput,
|
||||
@@ -54,7 +73,15 @@ export function checkpointDiffQueryOptions(
|
||||
},
|
||||
enabled: !!api && !!input.sessionId && hasValidRange,
|
||||
staleTime: Infinity,
|
||||
retry: 8,
|
||||
retryDelay: (attempt) => Math.min(2_000, 150 * 2 ** (attempt - 1)),
|
||||
retry: (failureCount, error) => {
|
||||
if (isCheckpointTemporarilyUnavailable(error)) {
|
||||
return failureCount < 12;
|
||||
}
|
||||
return failureCount < 3;
|
||||
},
|
||||
retryDelay: (attempt, error) =>
|
||||
isCheckpointTemporarilyUnavailable(error)
|
||||
? Math.min(5_000, 250 * 2 ** (attempt - 1))
|
||||
: Math.min(1_000, 100 * 2 ** (attempt - 1)),
|
||||
});
|
||||
}
|
||||
@@ -264,7 +264,7 @@ describe("hydratePersistedState", () => {
|
||||
expect(hydrated?.threads[0]?.activeTerminalGroupId).toBe(`group-${DEFAULT_THREAD_TERMINAL_ID}`);
|
||||
});
|
||||
|
||||
it("hydrates persisted turn diff summaries and clears persisted loaded flags", () => {
|
||||
it("hydrates persisted turn diff summaries as metadata-only entries", () => {
|
||||
const payload = JSON.stringify({
|
||||
version: 7,
|
||||
runtimeMode: "full-access",
|
||||
@@ -291,7 +291,6 @@ describe("hydratePersistedState", () => {
|
||||
turnId: "turn-1",
|
||||
completedAt: "2026-02-08T10:05:00.000Z",
|
||||
checkpointTurnCount: 1,
|
||||
checkpointDiffLoaded: true,
|
||||
files: [
|
||||
{
|
||||
path: "src/app.ts",
|
||||
@@ -422,10 +421,8 @@ describe("toPersistedState", () => {
|
||||
kind: "modified",
|
||||
additions: 3,
|
||||
deletions: 1,
|
||||
diff: "diff --git a/src/app.ts b/src/app.ts",
|
||||
},
|
||||
],
|
||||
unifiedDiff: "diff --git a/src/app.ts b/src/app.ts",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -61,7 +61,6 @@ const persistedTurnDiffSummarySchema = z.object({
|
||||
files: z.array(persistedTurnDiffFileChangeSchema),
|
||||
assistantMessageId: z.string().min(1).optional(),
|
||||
checkpointTurnCount: z.number().int().min(0).optional(),
|
||||
checkpointDiffLoaded: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const persistedThreadSchema = z.object({
|
||||
|
||||
@@ -493,11 +493,11 @@ describe("deriveTurnDiffSummaries", () => {
|
||||
expect(summaries).toHaveLength(1);
|
||||
expect(summaries[0]?.turnId).toBe("turn-1");
|
||||
expect(summaries[0]?.assistantMessageId).toBe("msg-1");
|
||||
expect(summaries[0]?.unifiedDiff).toContain("diff --git a/src/a.ts b/src/a.ts");
|
||||
expect(summaries[0]?.files.map((file) => file.path)).toEqual(["src/a.ts", "src/b.ts"]);
|
||||
expect(summaries[0]?.files[0]?.kind).toBe("modified");
|
||||
expect(summaries[0]?.files[0]?.additions).toBe(1);
|
||||
expect(summaries[0]?.files[0]?.deletions).toBe(1);
|
||||
expect(summaries[0]).not.toHaveProperty("unifiedDiff");
|
||||
});
|
||||
|
||||
it("includes completed turns even when no fileChange tool events were emitted", () => {
|
||||
@@ -537,9 +537,10 @@ describe("deriveTurnDiffSummaries", () => {
|
||||
payload: {
|
||||
diff: [
|
||||
"diff --git a/src/example.ts b/src/example.ts",
|
||||
"@@ -1 +1 @@",
|
||||
"@@ -1 +1,2 @@",
|
||||
"-old",
|
||||
"+older",
|
||||
"+extra",
|
||||
].join("\n"),
|
||||
},
|
||||
}),
|
||||
@@ -572,8 +573,9 @@ describe("deriveTurnDiffSummaries", () => {
|
||||
]);
|
||||
|
||||
expect(summaries).toHaveLength(1);
|
||||
expect(summaries[0]?.unifiedDiff).toContain("+newest");
|
||||
expect(summaries[0]?.unifiedDiff).not.toContain("+older");
|
||||
expect(summaries[0]?.files[0]?.path).toBe("src/example.ts");
|
||||
expect(summaries[0]?.files[0]?.additions).toBe(1);
|
||||
expect(summaries[0]?.files[0]?.deletions).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -593,8 +595,6 @@ describe("deriveTurnDiffFilesFromUnifiedDiff", () => {
|
||||
);
|
||||
|
||||
expect(files.map((file) => file.path)).toEqual(["src/a.ts", "src/b.ts"]);
|
||||
expect(files[0]?.diff).toContain("diff --git a/src/a.ts b/src/a.ts");
|
||||
expect(files[1]?.diff).toContain("diff --git a/src/b.ts b/src/b.ts");
|
||||
expect(files[0]?.additions).toBe(1);
|
||||
expect(files[0]?.deletions).toBe(1);
|
||||
});
|
||||
|
||||
@@ -453,7 +453,7 @@ interface MutableTurnDiffSummary {
|
||||
completedAt: string | undefined;
|
||||
status: string | undefined;
|
||||
assistantMessageId: string | undefined;
|
||||
unifiedDiff: string | undefined;
|
||||
hasTurnDiffUpdate: boolean;
|
||||
filesByPath: Map<string, TurnDiffFileChange>;
|
||||
}
|
||||
|
||||
@@ -485,7 +485,6 @@ function parseFileChangeEntriesFromEvent(event: ProviderEvent): TurnDiffFileChan
|
||||
parsed.push({
|
||||
path,
|
||||
...(kind ? { kind } : {}),
|
||||
...(diff ? { diff } : {}),
|
||||
...(additions !== undefined ? { additions } : stat ? { additions: stat.additions } : {}),
|
||||
...(deletions !== undefined ? { deletions } : stat ? { deletions: stat.deletions } : {}),
|
||||
});
|
||||
@@ -507,9 +506,6 @@ function mergeTurnDiffFileChange(
|
||||
if (existing.kind === undefined && incoming.kind !== undefined) {
|
||||
existing.kind = incoming.kind;
|
||||
}
|
||||
if (existing.diff === undefined && incoming.diff !== undefined) {
|
||||
existing.diff = incoming.diff;
|
||||
}
|
||||
if (existing.additions === undefined && incoming.additions !== undefined) {
|
||||
existing.additions = incoming.additions;
|
||||
}
|
||||
@@ -565,7 +561,6 @@ export function deriveTurnDiffFilesFromUnifiedDiff(diff: string): TurnDiffFileCh
|
||||
const stat = countDiffStat(fileDiff);
|
||||
return {
|
||||
path,
|
||||
diff: fileDiff,
|
||||
additions: stat.additions,
|
||||
deletions: stat.deletions,
|
||||
};
|
||||
@@ -645,7 +640,7 @@ export function deriveTurnDiffSummaries(events: ProviderEvent[]): TurnDiffSummar
|
||||
completedAt: undefined,
|
||||
status: undefined,
|
||||
assistantMessageId: undefined,
|
||||
unifiedDiff: undefined,
|
||||
hasTurnDiffUpdate: false,
|
||||
filesByPath: new Map<string, TurnDiffFileChange>(),
|
||||
};
|
||||
byTurnId.set(turnId, next);
|
||||
@@ -677,10 +672,10 @@ export function deriveTurnDiffSummaries(events: ProviderEvent[]): TurnDiffSummar
|
||||
}
|
||||
}
|
||||
|
||||
if (event.method === "turn/diff/updated" && summary.unifiedDiff === undefined) {
|
||||
if (event.method === "turn/diff/updated" && !summary.hasTurnDiffUpdate) {
|
||||
const diff = normalizeDetail(asString(asObject(event.payload)?.diff));
|
||||
if (diff) {
|
||||
summary.unifiedDiff = diff;
|
||||
summary.hasTurnDiffUpdate = true;
|
||||
for (const file of deriveTurnDiffFilesFromUnifiedDiff(diff)) {
|
||||
mergeTurnDiffFileChange(summary.filesByPath, file);
|
||||
}
|
||||
@@ -703,7 +698,6 @@ export function deriveTurnDiffSummaries(events: ProviderEvent[]): TurnDiffSummar
|
||||
files: Array.from(summary.filesByPath.values()).toSorted((a, b) =>
|
||||
a.path.localeCompare(b.path),
|
||||
),
|
||||
...(summary.unifiedDiff ? { unifiedDiff: summary.unifiedDiff } : {}),
|
||||
...(summary.assistantMessageId ? { assistantMessageId: summary.assistantMessageId } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
+18
-24
@@ -901,7 +901,7 @@ describe("store reducer thread continuity", () => {
|
||||
expect(next.threads[0]?.turnDiffSummaries.map((summary) => summary.turnId)).toEqual(["turn_1"]);
|
||||
});
|
||||
|
||||
it("keeps checkpoint-derived turn files when later events for the same turn arrive", () => {
|
||||
it("keeps existing turn file metadata when later events for the same turn arrive", () => {
|
||||
const state = makeState(
|
||||
makeThread({
|
||||
turnDiffSummaries: [
|
||||
@@ -911,11 +911,10 @@ describe("store reducer thread continuity", () => {
|
||||
files: [
|
||||
{
|
||||
path: "src/from-checkpoint.ts",
|
||||
diff: "diff --git a/src/from-checkpoint.ts b/src/from-checkpoint.ts",
|
||||
additions: 1,
|
||||
deletions: 1,
|
||||
},
|
||||
],
|
||||
unifiedDiff: "diff --git a/src/from-checkpoint.ts b/src/from-checkpoint.ts",
|
||||
checkpointDiffLoaded: true,
|
||||
},
|
||||
],
|
||||
}),
|
||||
@@ -935,11 +934,11 @@ describe("store reducer thread continuity", () => {
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.files.map((file) => file.path)).toEqual([
|
||||
"src/from-checkpoint.ts",
|
||||
]);
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.checkpointDiffLoaded).toBe(true);
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.unifiedDiff).toContain("from-checkpoint.ts");
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.files[0]?.additions).toBe(1);
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.files[0]?.deletions).toBe(1);
|
||||
});
|
||||
|
||||
it("updates turn summaries from checkpoint diffs and marks them as loaded", () => {
|
||||
it("updates checkpoint turn counts from authoritative checkpoint mappings", () => {
|
||||
const state = makeState(
|
||||
makeThread({
|
||||
turnDiffSummaries: [
|
||||
@@ -949,31 +948,26 @@ describe("store reducer thread continuity", () => {
|
||||
files: [],
|
||||
checkpointTurnCount: 1,
|
||||
},
|
||||
{
|
||||
turnId: "turn-2",
|
||||
completedAt: "2026-02-09T00:00:04.000Z",
|
||||
files: [],
|
||||
checkpointTurnCount: 2,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
const next = reducer(state, {
|
||||
type: "SET_THREAD_TURN_CHECKPOINT_DIFFS",
|
||||
type: "SET_THREAD_TURN_CHECKPOINT_COUNTS",
|
||||
threadId: "thread-local-1",
|
||||
checkpointDiffByTurnId: {
|
||||
"turn-1": [
|
||||
"diff --git a/src/a.ts b/src/a.ts",
|
||||
"@@ -1 +1 @@",
|
||||
"-old-a",
|
||||
"+new-a",
|
||||
"diff --git a/src/b.ts b/src/b.ts",
|
||||
"@@ -1 +1 @@",
|
||||
"-old-b",
|
||||
"+new-b",
|
||||
].join("\n"),
|
||||
checkpointTurnCountByTurnId: {
|
||||
"turn-1": 2,
|
||||
"turn-2": 3,
|
||||
},
|
||||
});
|
||||
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.files.map((file) => file.path)).toEqual([
|
||||
"src/a.ts",
|
||||
"src/b.ts",
|
||||
]);
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.checkpointDiffLoaded).toBe(true);
|
||||
expect(next.threads[0]?.turnDiffSummaries[0]?.checkpointTurnCount).toBe(2);
|
||||
expect(next.threads[0]?.turnDiffSummaries[1]?.checkpointTurnCount).toBe(3);
|
||||
});
|
||||
});
|
||||
+1
-72
@@ -15,7 +15,6 @@ import {
|
||||
applyEventToMessages,
|
||||
asObject,
|
||||
asString,
|
||||
deriveTurnDiffFilesFromUnifiedDiff,
|
||||
deriveTurnDiffSummaries,
|
||||
inferCheckpointTurnCountByTurnId,
|
||||
evolveSession,
|
||||
@@ -93,11 +92,6 @@ type Action =
|
||||
threadId: string;
|
||||
checkpointTurnCountByTurnId: Record<string, number>;
|
||||
}
|
||||
| {
|
||||
type: "SET_THREAD_TURN_CHECKPOINT_DIFFS";
|
||||
threadId: string;
|
||||
checkpointDiffByTurnId: Record<string, string>;
|
||||
}
|
||||
| {
|
||||
type: "SET_THREAD_BRANCH";
|
||||
threadId: string;
|
||||
@@ -237,21 +231,12 @@ function mergeTurnDiffSummaries(
|
||||
return summary;
|
||||
}
|
||||
|
||||
const prefersCheckpointDiff = previous.checkpointDiffLoaded || summary.checkpointDiffLoaded;
|
||||
const files =
|
||||
prefersCheckpointDiff ||
|
||||
(summary.files.length === 0 && previous.files.length > 0 && summary.unifiedDiff === undefined)
|
||||
? previous.files
|
||||
: summary.files;
|
||||
const unifiedDiff =
|
||||
prefersCheckpointDiff || summary.unifiedDiff === undefined
|
||||
? (previous.unifiedDiff ?? summary.unifiedDiff)
|
||||
: summary.unifiedDiff;
|
||||
summary.files.length === 0 && previous.files.length > 0 ? previous.files : summary.files;
|
||||
|
||||
return {
|
||||
...summary,
|
||||
files,
|
||||
...(unifiedDiff !== undefined ? { unifiedDiff } : {}),
|
||||
...(summary.assistantMessageId
|
||||
? {}
|
||||
: previous.assistantMessageId
|
||||
@@ -262,9 +247,6 @@ function mergeTurnDiffSummaries(
|
||||
: typeof previous.checkpointTurnCount === "number"
|
||||
? { checkpointTurnCount: previous.checkpointTurnCount }
|
||||
: {}),
|
||||
...(summary.checkpointDiffLoaded || previous.checkpointDiffLoaded
|
||||
? { checkpointDiffLoaded: true }
|
||||
: {}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1084,59 +1066,6 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
}),
|
||||
};
|
||||
|
||||
case "SET_THREAD_TURN_CHECKPOINT_DIFFS":
|
||||
return {
|
||||
...state,
|
||||
threads: updateThread(state.threads, action.threadId, (t) => {
|
||||
let hasUpdates = false;
|
||||
const updatedSummaries = t.turnDiffSummaries.map((summary) => {
|
||||
const checkpointDiff = action.checkpointDiffByTurnId[summary.turnId];
|
||||
if (checkpointDiff === undefined) {
|
||||
return summary;
|
||||
}
|
||||
|
||||
const files = deriveTurnDiffFilesFromUnifiedDiff(checkpointDiff);
|
||||
const filesUnchanged =
|
||||
files.length === summary.files.length &&
|
||||
files.every((file, index) => {
|
||||
const existing = summary.files[index];
|
||||
return (
|
||||
existing !== undefined &&
|
||||
existing.path === file.path &&
|
||||
existing.diff === file.diff &&
|
||||
existing.kind === file.kind &&
|
||||
existing.additions === file.additions &&
|
||||
existing.deletions === file.deletions
|
||||
);
|
||||
});
|
||||
if (
|
||||
summary.unifiedDiff === checkpointDiff &&
|
||||
filesUnchanged &&
|
||||
summary.checkpointDiffLoaded
|
||||
) {
|
||||
return summary;
|
||||
}
|
||||
|
||||
hasUpdates = true;
|
||||
return {
|
||||
...summary,
|
||||
unifiedDiff: checkpointDiff,
|
||||
files,
|
||||
checkpointDiffLoaded: true,
|
||||
};
|
||||
});
|
||||
|
||||
if (!hasUpdates) {
|
||||
return t;
|
||||
}
|
||||
|
||||
return {
|
||||
...t,
|
||||
turnDiffSummaries: updatedSummaries,
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
case "SET_THREAD_BRANCH": {
|
||||
return {
|
||||
...state,
|
||||
|
||||
@@ -40,7 +40,6 @@ export interface ChatMessage {
|
||||
export interface TurnDiffFileChange {
|
||||
path: string;
|
||||
kind?: string | undefined;
|
||||
diff?: string | undefined;
|
||||
additions?: number | undefined;
|
||||
deletions?: number | undefined;
|
||||
}
|
||||
@@ -50,10 +49,8 @@ export interface TurnDiffSummary {
|
||||
completedAt: string;
|
||||
status?: string | undefined;
|
||||
files: TurnDiffFileChange[];
|
||||
unifiedDiff?: string | undefined;
|
||||
assistantMessageId?: string | undefined;
|
||||
checkpointTurnCount?: number | undefined;
|
||||
checkpointDiffLoaded?: boolean | undefined;
|
||||
}
|
||||
|
||||
export interface Project {
|
||||
|
||||
Reference in new issue
Block a user