Skip to content

Commit ed76c75

Browse files
committed
fix(web): offer each turn's diff once so a closed diff stays closed
Proactive panels reopened a completed turn's diff on every thread visit because the user-choice revision is re-read on entry and never persisted. Remember the last offered turn in the persisted panel state, keep it when other panel actions rebuild the state, and refuse a turn already offered.
1 parent 701fc83 commit ed76c75

3 files changed

Lines changed: 118 additions & 21 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5841,7 +5841,13 @@ export default function ChatView(props: ChatViewProps) {
58415841
diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
58425842
};
58435843
if (diffAction !== "open" || newlyCompletedTurnId === null) return;
5844-
if (!panels.openProactive(activeThreadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
5844+
if (
5845+
!panels.openProactive(
5846+
activeThreadRef,
5847+
{ kind: "diff", turnId: newlyCompletedTurnId },
5848+
userActionRevision,
5849+
)
5850+
) {
58455851
return;
58465852
}
58475853
useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");

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

Lines changed: 74 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";
@@ -208,7 +208,7 @@ describe("rightPanelStore", () => {
208208
},
209209
);
210210

211-
const completedDiff = { id: "diff", kind: "diff" } as const;
211+
const completedDiff = { kind: "diff", turnId: RunId.make("turn-1") } as const;
212212
const linkedPullRequest = pullRequestSurface({
213213
projectId: "project-a",
214214
repository: "pingdotgg/t3code",
@@ -281,9 +281,15 @@ describe("rightPanelStore", () => {
281281
expect(
282282
store.openProactive(refA, { id: "pull-requests", kind: "pull-requests" }, revision),
283283
).toBe(false);
284-
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
285-
chosen,
284+
const { isOpen, activeSurfaceId, surfaces } = selectThreadRightPanelState(
285+
useRightPanelStore.getState().byThreadKey,
286+
refA,
286287
);
288+
expect({ isOpen, activeSurfaceId, surfaces }).toEqual({
289+
isOpen: chosen.isOpen,
290+
activeSurfaceId: chosen.activeSurfaceId,
291+
surfaces: chosen.surfaces,
292+
});
287293
});
288294

289295
it("allows automatic panels for a later turn after a manual choice", () => {
@@ -293,10 +299,73 @@ describe("rightPanelStore", () => {
293299
expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
294300

295301
const nextTurnRevision = store.getUserActionRevision(refA);
296-
expect(store.openProactive(refA, completedDiff, nextTurnRevision)).toBe(true);
302+
expect(
303+
store.openProactive(
304+
refA,
305+
{ ...completedDiff, turnId: RunId.make("turn-2") },
306+
nextTurnRevision,
307+
),
308+
).toBe(true);
309+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
310+
});
311+
312+
it.each([
313+
{ dismissal: "hide", dismiss: () => useRightPanelStore.getState().close(refA) },
314+
{
315+
dismissal: "close tab",
316+
dismiss: () => useRightPanelStore.getState().closeSurface(refA, "diff"),
317+
},
318+
])("offers each turn's diff once after $dismissal and reload", ({ dismiss }) => {
319+
const store = useRightPanelStore.getState();
320+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
321+
dismiss();
322+
const persisted = JSON.parse(
323+
JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
324+
);
325+
useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
326+
327+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
328+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
329+
330+
const nextTurn = { ...completedDiff, turnId: RunId.make("turn-2") };
331+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(true);
297332
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
298333
});
299334

335+
it.each([
336+
{ action: "open file", act: () => useRightPanelStore.getState().openFile(refA, "src/app.ts") },
337+
{ action: "open files", act: () => useRightPanelStore.getState().open(refA, "files") },
338+
{
339+
action: "open pull request",
340+
act: () => useRightPanelStore.getState().openPullRequest(refA, linkedPullRequest),
341+
},
342+
])("keeps the offered turn after dismissing, then $action, then reload", ({ act }) => {
343+
const store = useRightPanelStore.getState();
344+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
345+
store.close(refA);
346+
act();
347+
const persisted = JSON.parse(
348+
JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
349+
);
350+
useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
351+
const before = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
352+
353+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
354+
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
355+
before,
356+
);
357+
});
358+
359+
it("does not retry a diff offer the user already declined", () => {
360+
const store = useRightPanelStore.getState();
361+
const revision = store.getUserActionRevision(refA);
362+
store.openFile(refA, "src/app.ts");
363+
expect(store.openProactive(refA, completedDiff, revision)).toBe(false);
364+
365+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
366+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("file");
367+
});
368+
300369
it("keeps manual choices scoped to their thread and environment", () => {
301370
const otherEnvironment = scopeThreadRef("env-2" as EnvironmentId, refA.threadId);
302371
const store = useRightPanelStore.getState();

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
scopedThreadKey,
1313
scopeThreadRef,
1414
} from "@t3tools/client-runtime/environment";
15-
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
15+
import { EnvironmentId, RunId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
1616
import { create } from "zustand";
1717
import { createJSONStorage, persist } from "zustand/middleware";
1818

@@ -114,13 +114,19 @@ export interface ThreadRightPanelState {
114114
dismissedDeviceSurfaceIds?: string[];
115115
/** Kept with the thread so the layout survives a ChatView remount or a reload. */
116116
maximized?: true;
117+
/** The last turn whose diff was offered automatically. Each turn is offered once. */
118+
proactiveDiffTurnId?: RunId;
117119
}
118120

119121
export interface ThreadPanelVisibility {
120122
inlineOpen: boolean;
121123
popoverOpen: boolean;
122124
}
123125

126+
export type ProactivePanelRequest =
127+
| Extract<RightPanelSurface, { kind: "pull-request" | "pull-requests" }>
128+
| { kind: "diff"; turnId: RunId };
129+
124130
interface RightPanelStoreState {
125131
byThreadKey: Record<string, ThreadRightPanelState>;
126132
/**
@@ -141,7 +147,7 @@ interface RightPanelStoreState {
141147
*/
142148
openProactive: (
143149
ref: ScopedThreadRef,
144-
surface: Extract<RightPanelSurface, { kind: "diff" | "pull-request" | "pull-requests" }>,
150+
request: ProactivePanelRequest,
145151
expectedUserActionRevision: number,
146152
) => boolean;
147153
open: (
@@ -318,12 +324,18 @@ const updateThreadStateMap = (
318324
updater: (current: ThreadRightPanelState) => ThreadRightPanelState,
319325
): Record<string, ThreadRightPanelState> => {
320326
const current = byThreadKey[threadKey] ?? EMPTY_THREAD_STATE;
321-
const next = updater(current);
327+
const updated = updater(current);
328+
// Many actions rebuild the thread state from scratch. Only `openProactive` replaces the offer.
329+
const next =
330+
updated.proactiveDiffTurnId === undefined && current.proactiveDiffTurnId !== undefined
331+
? { ...updated, proactiveDiffTurnId: current.proactiveDiffTurnId }
332+
: updated;
322333
if (
323334
!next.isOpen &&
324335
next.activeSurfaceId === null &&
325336
next.surfaces.length === 0 &&
326-
!next.dismissedDeviceSurfaceIds?.length
337+
!next.dismissedDeviceSurfaceIds?.length &&
338+
next.proactiveDiffTurnId === undefined
327339
) {
328340
if (!(threadKey in byThreadKey)) return byThreadKey;
329341
const { [threadKey]: _removed, ...rest } = byThreadKey;
@@ -566,6 +578,9 @@ export function migratePersistedRightPanelState(persistedState: unknown): {
566578
isOpen,
567579
surfaces,
568580
activeSurfaceId,
581+
...(typeof validThreadState?.proactiveDiffTurnId === "string"
582+
? { proactiveDiffTurnId: RunId.make(validThreadState.proactiveDiffTurnId) }
583+
: {}),
569584
...(Array.isArray(validThreadState?.dismissedDeviceSurfaceIds)
570585
? {
571586
dismissedDeviceSurfaceIds:
@@ -614,26 +629,33 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
614629
closeRevisionByThreadKey: {},
615630
getUserActionRevision: (ref) =>
616631
get().userActionRevisionByThreadKey[scopedThreadKey(ref)] ?? 0,
617-
openProactive: (ref, surface, expectedUserActionRevision) => {
632+
openProactive: (ref, request, expectedUserActionRevision) => {
618633
let opened = false;
619634
set((state) => {
620635
const threadKey = scopedThreadKey(ref);
621-
if (
622-
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
623-
) {
624-
return state;
636+
if (request.kind === "diff") {
637+
const current = selectThreadRightPanelState(state.byThreadKey, ref);
638+
if (current.proactiveDiffTurnId === request.turnId) return state;
639+
// A linked PR takes priority over a completed-turn diff.
640+
const pullRequestActive =
641+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
642+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests";
643+
opened =
644+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) ===
645+
expectedUserActionRevision && !pullRequestActive;
646+
// Record the offer even when refused, so a revisit does not retry this turn.
647+
return automaticUpdate(state, threadKey, (thread) => ({
648+
...(opened ? upsertSurface(thread, singletonSurface("diff")) : thread),
649+
proactiveDiffTurnId: request.turnId,
650+
}));
625651
}
626-
// A linked PR takes priority over a completed-turn diff. Manual actions
627-
// always apply, and later user choices reject both proactive requests.
628652
if (
629-
surface.kind === "diff" &&
630-
(selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
631-
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests")
653+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
632654
) {
633655
return state;
634656
}
635657
opened = true;
636-
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, surface));
658+
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, request));
637659
});
638660
return opened;
639661
},

0 commit comments

Comments
 (0)