diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 5bba2212d5e1c..06e3149445149 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3960,7 +3960,10 @@ export default function ChatView(props: ChatViewProps) { }; if (diffAction !== "open" || newlyCompletedTurnId === null) return; - useDiffPanelStore.getState().selectTurn(activeThreadRef, newlyCompletedTurnId); + const diffSelection = useDiffPanelStore.getState().byThreadKey[activeThreadKey]; + if (diffSelection?.kind !== "turn" || !diffSelection.baselineTurnId) { + useDiffPanelStore.getState().selectTurn(activeThreadRef, newlyCompletedTurnId); + } useRightPanelStore.getState().open(activeThreadRef, "diff"); onDiffPanelOpen?.(); }, [ diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 87f5f1ba56f81..09d2a62fa7118 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -30,7 +30,13 @@ import { type DraftId } from "../composerDraftStore"; import { openDiffFilePrimaryAction } from "../diffFileActions"; import { useCheckpointDiff } from "~/lib/checkpointDiffState"; import { cn } from "~/lib/utils"; -import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore"; +import { + type DiffBaseline, + getCheckpointDiffRange, + THREAD_START_DIFF_BASELINE, + selectThreadDiffPanelSelection, + useDiffPanelStore, +} from "../diffPanelStore"; import { useLocalStorage } from "../hooks/useLocalStorage"; import { useTheme } from "../hooks/useTheme"; import { @@ -193,14 +199,26 @@ export default function DiffPanel({ ); useEffect(() => { - if (!routeThreadRef || diffSelection.kind !== "turn") return; + if (!routeThreadRef || !activeThread || diffSelection.kind !== "turn") return; useDiffPanelStore.getState().reconcileTurnSelection( routeThreadRef, orderedTurnDiffSummaries.map((summary) => summary.turnId), ); - }, [diffSelection, orderedTurnDiffSummaries, routeThreadRef]); + }, [activeThread, diffSelection, orderedTurnDiffSummaries, routeThreadRef]); - const selectedTurnId = diffSelection.kind === "turn" ? diffSelection.turnId : null; + const selectedTurnId = + diffSelection.kind === "turn" + ? diffSelection.followLatest + ? (orderedTurnDiffSummaries[0]?.turnId ?? diffSelection.turnId) + : diffSelection.turnId + : null; + const baselineTurnId = diffSelection.kind === "turn" ? diffSelection.baselineTurnId : undefined; + const baselineTurn = orderedTurnDiffSummaries.find( + (summary) => summary.turnId === baselineTurnId, + ); + const baselineTurnCount = + baselineTurnId === THREAD_START_DIFF_BASELINE ? 0 : baselineTurn?.checkpointTurnCount; + const followsLatest = diffSelection.kind === "turn" && diffSelection.followLatest === true; const selectedGitScope = diffSelection.kind === "unstaged" ? "unstaged" : "branch"; const selectedBaseRef = diffSelection.kind === "branch" ? diffSelection.baseRef : null; const selectedFilePath = diffSelection.kind === "turn" ? diffSelection.filePath : null; @@ -215,15 +233,41 @@ export default function DiffPanel({ selectedTurn && (selectedTurn.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[selectedTurn.turnId]); const latestTurn = orderedTurnDiffSummaries[0]; + const [baselineMenuOpen, setBaselineMenuOpen] = useState(false); + const firstCheckpoint = orderedTurnDiffSummaries.findLast( + (summary) => summary.status === "ready" && summary.checkpointTurnCount > 0, + ); + // A completion checkpoint can exist without Turn 0 when Git was initialized mid-turn. + // Check the earliest available range only when the baseline menu is opened. + const initialCheckpointDiff = useCheckpointDiff( + { + environmentId: activeThread?.environmentId ?? null, + threadId: activeThread?.id ?? null, + fromTurnCount: 0, + toTurnCount: firstCheckpoint?.checkpointTurnCount ?? null, + ignoreWhitespace: true, + }, + { enabled: baselineMenuOpen && isGitRepo }, + ); + const isLatestTurnSelection = + baselineTurnCount === undefined ? selectedTurn?.turnId === latestTurn?.turnId : followsLatest; const selectedScopeLabel = selectedTurnId === null ? selectedGitScope === "unstaged" ? "Working tree" : "Branch changes" - : selectedTurn?.turnId === latestTurn?.turnId + : isLatestTurnSelection ? "Latest turn" : `Turn ${selectedCheckpointTurnCount ?? "?"}`; - const reviewSectionId = selectedTurn ? `turn:${selectedTurn.turnId}` : selectedGitScope; + const comparisonLabel = + baselineTurnCount !== undefined + ? `Turn ${baselineTurnCount} → ${selectedScopeLabel}` + : selectedScopeLabel; + const reviewSectionId = selectedTurn + ? baselineTurnCount !== undefined + ? `turn:${baselineTurnId}:${selectedTurn.turnId}` + : `turn:${selectedTurn.turnId}` + : selectedGitScope; const collapseScopeKey = routeThreadRef ? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}:${reviewSectionId}` : null; @@ -233,19 +277,15 @@ export default function DiffPanel({ ? collapsedDiffFiles.fileKeys : EMPTY_COLLAPSED_DIFF_FILE_KEYS; const reviewSectionTitle = selectedTurn - ? `Turn ${selectedCheckpointTurnCount ?? "?"}` + ? baselineTurnCount !== undefined + ? comparisonLabel + : `Turn ${selectedCheckpointTurnCount ?? "?"}` : selectedGitScope === "unstaged" ? "Working tree" : "Branch changes"; - const selectedCheckpointRange = useMemo( - () => - typeof selectedCheckpointTurnCount === "number" - ? { - fromTurnCount: Math.max(0, selectedCheckpointTurnCount - 1), - toTurnCount: selectedCheckpointTurnCount, - } - : null, - [selectedCheckpointTurnCount], + const selectedCheckpointRange = getCheckpointDiffRange( + selectedCheckpointTurnCount, + baselineTurnCount, ); const activeCheckpointDiff = useCheckpointDiff( { @@ -254,7 +294,7 @@ export default function DiffPanel({ fromTurnCount: selectedCheckpointRange?.fromTurnCount ?? null, toTurnCount: selectedCheckpointRange?.toTurnCount ?? null, ignoreWhitespace: diffIgnoreWhitespace, - cacheScope: selectedTurn ? `turn:${selectedTurn.turnId}` : null, + cacheScope: selectedTurn ? reviewSectionId : null, }, { enabled: isGitRepo && selectedTurn !== undefined }, ); @@ -532,7 +572,11 @@ export default function DiffPanel({ const selectTurn = (turnId: TurnId) => { if (!routeThreadRef) return; - useDiffPanelStore.getState().selectTurn(routeThreadRef, turnId); + useDiffPanelStore.getState().selectTurn(routeThreadRef, turnId, undefined, { baselineTurnId }); + }; + const pinBaseline = (baseline: DiffBaseline | null) => { + if (routeThreadRef && latestTurn) + useDiffPanelStore.getState().pinBaseline(routeThreadRef, baseline, latestTurn.turnId); }; const selectGitScope = (scope: "branch" | "unstaged") => { if (!routeThreadRef) return; @@ -549,9 +593,9 @@ export default function DiffPanel({ - {selectedScopeLabel} + {comparisonLabel} @@ -577,12 +621,13 @@ export default function DiffPanel({ { - if (latestTurn) selectTurn(latestTurn.turnId); + if (latestTurn && routeThreadRef) + useDiffPanelStore.getState().selectLatestTurn(routeThreadRef, latestTurn.turnId); }} > Latest turn @@ -598,12 +643,26 @@ export default function DiffPanel({ return ( selectTurn(summary.turnId)} > - Turn {turnCount} + + Turn {turnCount} + {summary.status === "ready" && summary.files.length === 0 + ? " · No changes" + : ""} + {formatShortTimestamp(summary.completedAt, settings.timestampFormat)} @@ -612,6 +671,35 @@ export default function DiffPanel({ })} + + Pin baseline + + pinBaseline(THREAD_START_DIFF_BASELINE)} + > + Start of thread (Turn 0){baselineTurnCount === 0 ? " (pinned)" : ""} + {initialCheckpointDiff.error ? " (unavailable)" : ""} + + {orderedTurnDiffSummaries.map((summary) => ( + pinBaseline(summary.turnId)} + > + Turn {summary.checkpointTurnCount} + {summary.turnId === baselineTurnId ? " (pinned)" : ""} + + ))} + + + {baselineTurnCount !== undefined && ( + pinBaseline(null)}> + Unpin baseline (Turn {baselineTurnCount}) + + )} {selectedTurnId === null && selectedGitScope === "branch" && selectedGitSource?.baseRef && ( diff --git a/apps/web/src/diffPanelStore.test.ts b/apps/web/src/diffPanelStore.test.ts index f1d774f9cbfcd..d4a75db5b486e 100644 --- a/apps/web/src/diffPanelStore.test.ts +++ b/apps/web/src/diffPanelStore.test.ts @@ -2,7 +2,12 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { EnvironmentId, ThreadId, TurnId } from "@t3tools/contracts"; import { beforeEach, describe, expect, it } from "vite-plus/test"; -import { selectThreadDiffPanelSelection, useDiffPanelStore } from "./diffPanelStore"; +import { + getCheckpointDiffRange, + THREAD_START_DIFF_BASELINE, + selectThreadDiffPanelSelection, + useDiffPanelStore, +} from "./diffPanelStore"; const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1")); @@ -84,4 +89,93 @@ describe("diffPanelStore", () => { revealRequestId: 1, }); }); + it.each([TurnId.make("turn-1"), THREAD_START_DIFF_BASELINE] as const)( + "keeps %s pinned while latest advances and a fixed endpoint stays put", + (baseline) => { + const first = TurnId.make("turn-1"); + const second = TurnId.make("turn-2"); + const third = TurnId.make("turn-3"); + const store = useDiffPanelStore.getState(); + store.pinBaseline(THREAD_REF, baseline, second); + store.reconcileTurnSelection(THREAD_REF, [third, second, first]); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).toMatchObject({ turnId: third, baselineTurnId: baseline, followLatest: true }); + store.selectTurn(THREAD_REF, second, undefined, { baselineTurnId: baseline }); + store.reconcileTurnSelection(THREAD_REF, [third, second, first]); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).toMatchObject({ turnId: second, baselineTurnId: baseline }); + store.selectLatestTurn(THREAD_REF, third); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).toMatchObject({ turnId: third, baselineTurnId: baseline, followLatest: true }); + store.pinBaseline(THREAD_REF, null, third); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).not.toHaveProperty("baselineTurnId"); + }, + ); + + it("drops a removed baseline and falls back when every checkpoint is removed", () => { + const baseline = TurnId.make("turn-1"); + const latest = TurnId.make("turn-2"); + const store = useDiffPanelStore.getState(); + store.pinBaseline(THREAD_REF, baseline, latest); + store.reconcileTurnSelection(THREAD_REF, [latest]); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).toMatchObject({ turnId: latest, baselineTurnId: undefined }); + store.pinBaseline(THREAD_REF, THREAD_START_DIFF_BASELINE, latest); + store.reconcileTurnSelection(THREAD_REF, []); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).toEqual({ kind: "branch", baseRef: null }); + }); + + it("opens a timeline turn on its own and isolates pins by environment", () => { + const turn = TurnId.make("turn-1"); + const store = useDiffPanelStore.getState(); + store.pinBaseline(THREAD_REF, turn, turn); + const other = scopeThreadRef(EnvironmentId.make("environment-2"), THREAD_REF.threadId); + expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, other)).toEqual( + { kind: "branch", baseRef: null }, + ); + store.selectTurn(THREAD_REF, turn); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).not.toHaveProperty("baselineTurnId"); + }); + + it("preserves the existing unpinned latest-turn selection behavior", () => { + const first = TurnId.make("turn-1"); + const second = TurnId.make("turn-2"); + const store = useDiffPanelStore.getState(); + store.selectLatestTurn(THREAD_REF, first); + store.reconcileTurnSelection(THREAD_REF, [second, first]); + store.reconcileTurnSelection(THREAD_REF, []); + expect( + selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF), + ).toMatchObject({ kind: "turn", turnId: first }); + }); +}); + +describe("getCheckpointDiffRange", () => { + it("compares a single turn to its preceding snapshot by default", () => { + expect(getCheckpointDiffRange(1)).toEqual({ fromTurnCount: 0, toTurnCount: 1 }); + expect(getCheckpointDiffRange(5)).toEqual({ fromTurnCount: 4, toTurnCount: 5 }); + }); + it("excludes the pinned turn's changes and follows the supplied endpoint", () => { + expect(getCheckpointDiffRange(5, 2)).toEqual({ fromTurnCount: 2, toTurnCount: 5 }); + expect(getCheckpointDiffRange(6, 2)).toEqual({ fromTurnCount: 2, toTurnCount: 6 }); + expect(getCheckpointDiffRange(2, 2)).toEqual({ fromTurnCount: 2, toTurnCount: 2 }); + }); + it("includes turn one when pinned to the initial snapshot", () => { + expect(getCheckpointDiffRange(1, 0)).toEqual({ fromTurnCount: 0, toTurnCount: 1 }); + expect(getCheckpointDiffRange(6, 0)).toEqual({ fromTurnCount: 0, toTurnCount: 6 }); + }); + it("does not request an unavailable or reversed range", () => { + expect(getCheckpointDiffRange(null, 2)).toBeNull(); + expect(getCheckpointDiffRange(1, 2)).toBeNull(); + }); }); diff --git a/apps/web/src/diffPanelStore.ts b/apps/web/src/diffPanelStore.ts index 56b5ad23feccc..22e0108faa5bb 100644 --- a/apps/web/src/diffPanelStore.ts +++ b/apps/web/src/diffPanelStore.ts @@ -5,10 +5,19 @@ import { createJSONStorage, persist } from "zustand/middleware"; import { resolveStorage } from "./lib/storage"; +export type DiffBaseline = TurnId | typeof THREAD_START_DIFF_BASELINE; + export type DiffPanelSelection = | { kind: "branch"; baseRef: string | null } | { kind: "unstaged" } - | { kind: "turn"; turnId: TurnId; filePath: string | null; revealRequestId: number }; + | { + kind: "turn"; + turnId: TurnId; + filePath: string | null; + revealRequestId: number; + baselineTurnId?: DiffBaseline | undefined; + followLatest?: boolean; + }; const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null }; const DEFAULT_WORKING_TREE_SELECTION: DiffPanelSelection = { kind: "unstaged" }; @@ -18,7 +27,18 @@ interface DiffPanelStoreState { branchBaseRefByThreadKey: Record; selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void; selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void; - selectTurn: (ref: ScopedThreadRef, turnId: TurnId, filePath?: string) => void; + selectTurn: ( + ref: ScopedThreadRef, + turnId: TurnId, + filePath?: string, + comparison?: { baselineTurnId?: DiffBaseline | undefined; followLatest?: boolean }, + ) => void; + pinBaseline: ( + ref: ScopedThreadRef, + baselineTurnId: DiffBaseline | null, + latestTurnId: TurnId, + ) => void; + selectLatestTurn: (ref: ScopedThreadRef, turnId: TurnId) => void; reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray) => void; removeThread: (ref: ScopedThreadRef) => void; } @@ -30,7 +50,7 @@ function normalizeBaseRef(baseRef: string | null): string | null { export const useDiffPanelStore = create()( persist( - (set) => ({ + (set, get) => ({ byThreadKey: {}, branchBaseRefByThreadKey: {}, selectGitScope: (ref, scope) => @@ -70,7 +90,7 @@ export const useDiffPanelStore = create()( }, }; }), - selectTurn: (ref, turnId, filePath) => + selectTurn: (ref, turnId, filePath, comparison) => set((state) => { const threadKey = scopedThreadKey(ref); const previous = state.byThreadKey[threadKey]; @@ -80,28 +100,67 @@ export const useDiffPanelStore = create()( [threadKey]: { kind: "turn", turnId, + ...comparison, filePath: filePath?.trim() || null, revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1, }, }, }; }), + pinBaseline: (ref, baselineTurnId, latestTurnId) => { + const previous = get().byThreadKey[scopedThreadKey(ref)]; + const turnId = + !baselineTurnId && previous?.kind === "turn" ? previous.turnId : latestTurnId; + get().selectTurn( + ref, + turnId, + undefined, + baselineTurnId ? { baselineTurnId, followLatest: true } : undefined, + ); + }, + selectLatestTurn: (ref, turnId) => { + const previous = get().byThreadKey[scopedThreadKey(ref)]; + get().selectTurn( + ref, + turnId, + undefined, + previous?.kind === "turn" && previous.baselineTurnId + ? { baselineTurnId: previous.baselineTurnId, followLatest: true } + : undefined, + ); + }, reconcileTurnSelection: (ref, availableTurnIds) => set((state) => { const threadKey = scopedThreadKey(ref); const previous = state.byThreadKey[threadKey]; const latestTurnId = availableTurnIds[0]; - if ( - previous?.kind !== "turn" || - latestTurnId === undefined || - availableTurnIds.includes(previous.turnId) - ) { - return state; + if (previous?.kind !== "turn") return state; + if (latestTurnId === undefined) { + return previous.baselineTurnId + ? { byThreadKey: { ...state.byThreadKey, [threadKey]: DEFAULT_SELECTION } } + : state; } + const turnId = + previous.followLatest || !availableTurnIds.includes(previous.turnId) + ? latestTurnId + : previous.turnId; + const baselineTurnId = + previous.baselineTurnId && + (previous.baselineTurnId === THREAD_START_DIFF_BASELINE || + availableTurnIds.includes(previous.baselineTurnId)) + ? previous.baselineTurnId + : undefined; + if (turnId === previous.turnId && baselineTurnId === previous.baselineTurnId) + return state; return { byThreadKey: { ...state.byThreadKey, - [threadKey]: { ...previous, turnId: latestTurnId }, + [threadKey]: { + ...previous, + turnId, + baselineTurnId, + ...(previous.followLatest ? { followLatest: baselineTurnId !== undefined } : {}), + }, }, }; }), @@ -142,3 +201,18 @@ export function selectThreadDiffPanelSelection( (hasWorkingTreeChanges ? DEFAULT_WORKING_TREE_SELECTION : DEFAULT_SELECTION) ); } + +/** Compare completed snapshots; a pinned turn excludes that turn's own changes. */ +export function getCheckpointDiffRange( + toTurnCount: number | null | undefined, + baselineTurnCount?: number | null, +) { + if (toTurnCount == null || (baselineTurnCount != null && baselineTurnCount > toTurnCount)) + return null; + return { + fromTurnCount: baselineTurnCount ?? Math.max(0, toTurnCount - 1), + toTurnCount, + }; +} + +export const THREAD_START_DIFF_BASELINE = "thread-start"; diff --git a/docs/user/source-control.md b/docs/user/source-control.md index 2d7ed8badbec6..5583b5f63f7d6 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -62,6 +62,17 @@ For a local Git repository without a remote, **Publish Repository** creates a ho adds it as `origin`, and pushes your commits. If there are no commits yet, it creates the remote; make your first commit before pushing. +## Compare work across turns + +On web and desktop, in the diff scope menu, choose **Pin baseline** and a completed turn to compare its final state +with **Latest turn**. Choose **Start of thread (Turn 0)** to include turn 1's changes. For imported +sessions, the initial snapshot starts when T3 begins capturing checkpoints, not at the start of +historic CLI work. Choose a numbered **Turn** to fix the endpoint instead of following new turns. + +**Unpin baseline** returns to a single-turn diff. Working-tree and branch scopes clear the pin. +Pins are local to the current client, thread, and environment. An empty turn can still be a useful +comparison endpoint; its individual file count does not describe the whole pinned range. + ## Create a pull request Use a thread's Git actions to commit, push, and create a pull request. T3 Code can generate commit