Skip to content

Commit 7400bc5

Browse files
fix(web): a closed proactive diff stays closed when you return to a thread (pingdotgg#14705)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 7f63b10 commit 7400bc5

5 files changed

Lines changed: 182 additions & 35 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,6 @@ describe("proactive completed diff guard", () => {
17441744
resolveProactiveTurnDiffAction({
17451745
checkpoint: changedCheckpoint,
17461746
isGitRepo: true,
1747-
activeSurfaceKind: null,
17481747
}),
17491748
).toBe(action);
17501749
},
@@ -1764,41 +1763,37 @@ describe("proactive completed diff guard", () => {
17641763
resolveProactiveTurnDiffAction({
17651764
checkpoint: undefined,
17661765
isGitRepo: true,
1767-
activeSurfaceKind: null,
17681766
}),
17691767
).toBe("defer");
17701768
expect(
17711769
resolveProactiveTurnDiffAction({
17721770
checkpoint: missingCheckpoint,
17731771
isGitRepo: true,
1774-
activeSurfaceKind: null,
17751772
}),
17761773
).toBe("defer");
17771774
expect(
17781775
resolveProactiveTurnDiffAction({
17791776
checkpoint: changedCheckpoint,
17801777
isGitRepo: undefined,
1781-
activeSurfaceKind: null,
17821778
}),
17831779
).toBe("defer");
17841780
});
17851781

1786-
it("keeps an active pull request above a completed turn diff", () => {
1782+
it("delegates a qualifying completed diff to the store while a pull request is active", () => {
17871783
const changedCheckpoint = {
17881784
status: "ready",
1789-
files: [{ path: "src/app.ts", kind: "modified", additions: 1, deletions: 0 }],
1785+
files: [{ path: "src/app.ts", kind: "modified", additions: 50, deletions: 0 }],
17901786
} satisfies Pick<TurnDiffSummary, "status" | "files">;
17911787

17921788
expect(
17931789
resolveProactiveTurnDiffAction({
17941790
checkpoint: changedCheckpoint,
17951791
isGitRepo: true,
1796-
activeSurfaceKind: "pull-request",
17971792
}),
1798-
).toBe("ignore");
1793+
).toBe("open");
17991794
});
18001795

1801-
it("leaves an already open diff and its chosen scope alone", () => {
1796+
it("delegates a qualifying completion to the store while a diff is already open", () => {
18021797
const largeCheckpoint = {
18031798
status: "ready",
18041799
files: Array.from({ length: 3 }, (_, index) => ({
@@ -1813,9 +1808,8 @@ describe("proactive completed diff guard", () => {
18131808
resolveProactiveTurnDiffAction({
18141809
checkpoint: largeCheckpoint,
18151810
isGitRepo: true,
1816-
activeSurfaceKind: "diff",
18171811
}),
1818-
).toBe("ignore");
1812+
).toBe("open");
18191813
});
18201814
});
18211815

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,7 @@ export function shouldOpenProactiveTurnDiff(input: {
181181
export function resolveProactiveTurnDiffAction(input: {
182182
checkpoint: Pick<TurnDiffSummary, "status" | "files"> | undefined;
183183
isGitRepo: boolean | undefined;
184-
activeSurfaceKind: RightPanelSurface["kind"] | null;
185184
}): "defer" | "ignore" | "open" {
186-
// An open diff already shows the work; reopening it would reset the chosen scope.
187-
if (input.activeSurfaceKind === "pull-request" || input.activeSurfaceKind === "diff") {
188-
return "ignore";
189-
}
190185
if (input.checkpoint === undefined || input.checkpoint.status === "missing") return "defer";
191186
if (input.isGitRepo === undefined) return "defer";
192187
if (

‎apps/web/src/hooks/useProactivePanels.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ export function useProactivePanels(input: {
169169
? resolveProactiveTurnDiffAction({
170170
checkpoint: completedCheckpoint,
171171
isGitRepo,
172-
activeSurfaceKind: openSurface?.kind ?? null,
173172
})
174173
: "ignore";
175174
proactivePanelObservationRef.current = {
@@ -179,7 +178,13 @@ export function useProactivePanels(input: {
179178
diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
180179
};
181180
if (diffAction !== "open" || newlyCompletedTurnId === null) return;
182-
if (!panels.openProactive(threadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
181+
if (
182+
!panels.openProactive(
183+
threadRef,
184+
{ kind: "diff", turnId: newlyCompletedTurnId },
185+
userActionRevision,
186+
)
187+
) {
183188
return;
184189
}
185190
useDiffPanelStore.getState().selectGitScope(threadRef, "branch");

‎apps/web/src/rightPanelStore.test.ts‎

Lines changed: 141 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2-
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { type EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
33
import { beforeEach, describe, expect, it } from "vite-plus/test";
44

55
import { useClosedViewStore } from "./closedViewStore";
@@ -29,6 +29,16 @@ beforeEach(() => {
2929
});
3030
});
3131

32+
async function reloadRightPanelStore() {
33+
const { storage, name } = useRightPanelStore.persist.getOptions();
34+
const persisted = await storage!.getItem(name!);
35+
expect(persisted).not.toBeNull();
36+
useRightPanelStore.setState(useRightPanelStore.getInitialState());
37+
await storage!.setItem(name!, persisted!);
38+
await useRightPanelStore.persist.rehydrate();
39+
expect(useRightPanelStore.persist.hasHydrated()).toBe(true);
40+
}
41+
3242
describe("rightPanelStore", () => {
3343
it("records single and bulk tab closes, newest first", () => {
3444
const store = useRightPanelStore.getState();
@@ -208,7 +218,7 @@ describe("rightPanelStore", () => {
208218
},
209219
);
210220

211-
const completedDiff = { id: "diff", kind: "diff" } as const;
221+
const completedDiff = { kind: "diff", turnId: RunId.make("turn-1") } as const;
212222
const linkedPullRequest = pullRequestSurface({
213223
projectId: "project-a",
214224
repository: "pingdotgg/t3code",
@@ -281,9 +291,15 @@ describe("rightPanelStore", () => {
281291
expect(
282292
store.openProactive(refA, { id: "pull-requests", kind: "pull-requests" }, revision),
283293
).toBe(false);
284-
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
285-
chosen,
294+
const { isOpen, activeSurfaceId, surfaces } = selectThreadRightPanelState(
295+
useRightPanelStore.getState().byThreadKey,
296+
refA,
286297
);
298+
expect({ isOpen, activeSurfaceId, surfaces }).toEqual({
299+
isOpen: chosen.isOpen,
300+
activeSurfaceId: chosen.activeSurfaceId,
301+
surfaces: chosen.surfaces,
302+
});
287303
});
288304

289305
it("allows automatic panels for a later turn after a manual choice", () => {
@@ -293,7 +309,127 @@ describe("rightPanelStore", () => {
293309
expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
294310

295311
const nextTurnRevision = store.getUserActionRevision(refA);
296-
expect(store.openProactive(refA, completedDiff, nextTurnRevision)).toBe(true);
312+
expect(
313+
store.openProactive(
314+
refA,
315+
{ ...completedDiff, turnId: RunId.make("turn-2") },
316+
nextTurnRevision,
317+
),
318+
).toBe(true);
319+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
320+
});
321+
322+
it("offers each completed diff once across dismissal, panel changes, and reload", async () => {
323+
const store = useRightPanelStore.getState();
324+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
325+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
326+
327+
store.close(refA);
328+
await reloadRightPanelStore();
329+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
330+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
331+
332+
store.open(refA, "files");
333+
store.openFile(refA, "src/app.ts");
334+
await reloadRightPanelStore();
335+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
336+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("file");
337+
338+
store.closeAllSurfaces(refA);
339+
await reloadRightPanelStore();
340+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
341+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
342+
343+
const otherEnvironment = scopeThreadRef("env-2" as EnvironmentId, refA.threadId);
344+
for (const ref of [refB, otherEnvironment]) {
345+
expect(store.openProactive(ref, completedDiff, store.getUserActionRevision(ref))).toBe(true);
346+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, ref)).toBe("diff");
347+
}
348+
349+
const nextTurn = { ...completedDiff, turnId: RunId.make("turn-2") };
350+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(true);
351+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
352+
353+
const completedWhileOpen = { ...completedDiff, turnId: RunId.make("turn-3") };
354+
const existingDiff = selectActiveRightPanelSurface(
355+
useRightPanelStore.getState().byThreadKey,
356+
refA,
357+
);
358+
expect(store.openProactive(refA, completedWhileOpen, store.getUserActionRevision(refA))).toBe(
359+
false,
360+
);
361+
expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual(
362+
existingDiff,
363+
);
364+
store.closeSurface(refA, "diff");
365+
await reloadRightPanelStore();
366+
expect(store.openProactive(refA, completedWhileOpen, store.getUserActionRevision(refA))).toBe(
367+
false,
368+
);
369+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
370+
371+
// A later storage migration must preserve the dismissal, just like an ordinary reload.
372+
const { storage, name, version } = useRightPanelStore.persist.getOptions();
373+
const persisted = await storage!.getItem(name!);
374+
await storage!.setItem(name!, { ...persisted!, version: version! - 1 });
375+
await reloadRightPanelStore();
376+
expect(store.openProactive(refA, completedWhileOpen, store.getUserActionRevision(refA))).toBe(
377+
false,
378+
);
379+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
380+
381+
expect(
382+
store.openProactive(
383+
refA,
384+
{ ...completedDiff, turnId: RunId.make("turn-4") },
385+
store.getUserActionRevision(refA),
386+
),
387+
).toBe(true);
388+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
389+
store.closeSurface(refA, "diff");
390+
store.open(refA, "diff");
391+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
392+
});
393+
394+
it("does not retry declined diff offers after manual choices or PR priority clear", async () => {
395+
const store = useRightPanelStore.getState();
396+
const revision = store.getUserActionRevision(refA);
397+
store.openFile(refA, "src/app.ts");
398+
expect(store.openProactive(refA, completedDiff, revision)).toBe(false);
399+
await reloadRightPanelStore();
400+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
401+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("file");
402+
403+
store.closeAllSurfaces(refA);
404+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
405+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
406+
407+
const nextTurn = { ...completedDiff, turnId: RunId.make("turn-2") };
408+
store.openPullRequest(refA, linkedPullRequest);
409+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(false);
410+
expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual(
411+
linkedPullRequest,
412+
);
413+
expect(
414+
selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
415+
).toEqual([linkedPullRequest]);
416+
await reloadRightPanelStore();
417+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(false);
418+
expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual(
419+
linkedPullRequest,
420+
);
421+
store.closeSurface(refA, linkedPullRequest.id);
422+
await reloadRightPanelStore();
423+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(false);
424+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
425+
426+
expect(
427+
store.openProactive(
428+
refA,
429+
{ ...completedDiff, turnId: RunId.make("turn-3") },
430+
store.getUserActionRevision(refA),
431+
),
432+
).toBe(true);
297433
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
298434
});
299435

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
scopedThreadKey,
44
scopeThreadRef,
55
} from "@t3tools/client-runtime/environment";
6-
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
6+
import { EnvironmentId, RunId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
77
import { create } from "zustand";
88
import { createJSONStorage, persist } from "zustand/middleware";
99

@@ -95,6 +95,7 @@ export interface ThreadRightPanelState {
9595
surfaces: RightPanelSurface[];
9696
dismissedDeviceSurfaceIds?: string[];
9797
maximized?: true;
98+
proactiveDiffTurnId?: RunId;
9899
}
99100

100101
export interface ThreadPanelVisibility {
@@ -117,7 +118,9 @@ interface RightPanelStoreState {
117118
*/
118119
openProactive: (
119120
ref: ScopedThreadRef,
120-
surface: Extract<RightPanelSurface, { kind: "diff" | "pull-request" | "pull-requests" }>,
121+
request:
122+
| Extract<RightPanelSurface, { kind: "pull-request" | "pull-requests" }>
123+
| { kind: "diff"; turnId: RunId },
121124
expectedUserActionRevision: number,
122125
) => boolean;
123126
open: (
@@ -310,7 +313,8 @@ const updateThreadStateMap = (
310313
!next.isOpen &&
311314
next.activeSurfaceId === null &&
312315
next.surfaces.length === 0 &&
313-
!next.dismissedDeviceSurfaceIds?.length
316+
!next.dismissedDeviceSurfaceIds?.length &&
317+
next.proactiveDiffTurnId === undefined
314318
) {
315319
if (!(threadKey in byThreadKey)) return byThreadKey;
316320
const { [threadKey]: _removed, ...rest } = byThreadKey;
@@ -545,6 +549,9 @@ export function migratePersistedRightPanelState(persistedState: unknown): {
545549
isOpen,
546550
surfaces,
547551
activeSurfaceId,
552+
...(typeof validThreadState?.proactiveDiffTurnId === "string"
553+
? { proactiveDiffTurnId: RunId.make(validThreadState.proactiveDiffTurnId) }
554+
: {}),
548555
...(Array.isArray(validThreadState?.dismissedDeviceSurfaceIds)
549556
? {
550557
dismissedDeviceSurfaceIds:
@@ -593,24 +600,34 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
593600
closeRevisionByThreadKey: {},
594601
getUserActionRevision: (ref) =>
595602
get().userActionRevisionByThreadKey[scopedThreadKey(ref)] ?? 0,
596-
openProactive: (ref, surface, expectedUserActionRevision) => {
603+
openProactive: (ref, request, expectedUserActionRevision) => {
597604
let opened = false;
598605
set((state) => {
599606
const threadKey = scopedThreadKey(ref);
600-
if (
601-
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
602-
) {
603-
return state;
607+
if (request.kind === "diff") {
608+
const current = selectThreadRightPanelState(state.byThreadKey, ref);
609+
if (current.proactiveDiffTurnId === request.turnId) return state;
610+
const activePanel = selectActiveRightPanel(state.byThreadKey, ref);
611+
const pullRequestActive =
612+
activePanel === "pull-request" || activePanel === "pull-requests";
613+
opened =
614+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) ===
615+
expectedUserActionRevision &&
616+
activePanel !== "diff" &&
617+
!pullRequestActive;
618+
// Record the offer even when refused, so a revisit does not retry this turn.
619+
return automaticUpdate(state, threadKey, (thread) => ({
620+
...(opened ? upsertSurface(thread, singletonSurface("diff")) : thread),
621+
proactiveDiffTurnId: request.turnId,
622+
}));
604623
}
605624
if (
606-
surface.kind === "diff" &&
607-
(selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
608-
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests")
625+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
609626
) {
610627
return state;
611628
}
612629
opened = true;
613-
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, surface));
630+
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, request));
614631
});
615632
return opened;
616633
},

0 commit comments

Comments
 (0)