Skip to content

Commit 410c41d

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 858b98b commit 410c41d

3 files changed

Lines changed: 118 additions & 20 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5349,7 +5349,13 @@ export default function ChatView(props: ChatViewProps) {
53495349
diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
53505350
};
53515351
if (diffAction !== "open" || newlyCompletedTurnId === null) return;
5352-
if (!panels.openProactive(activeThreadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
5352+
if (
5353+
!panels.openProactive(
5354+
activeThreadRef,
5355+
{ kind: "diff", turnId: newlyCompletedTurnId },
5356+
userActionRevision,
5357+
)
5358+
) {
53535359
return;
53545360
}
53555361
useDiffPanelStore.getState().selectGitScope(activeThreadRef, "unstaged");

‎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 {
@@ -108,7 +108,7 @@ describe("rightPanelStore", () => {
108108
},
109109
);
110110

111-
const completedDiff = { id: "diff", kind: "diff" } as const;
111+
const completedDiff = { kind: "diff", turnId: RunId.make("turn-1") } as const;
112112
const linkedPullRequest = pullRequestSurface({
113113
projectId: "project-a",
114114
repository: "pingdotgg/t3code",
@@ -181,9 +181,15 @@ describe("rightPanelStore", () => {
181181
expect(
182182
store.openProactive(refA, { id: "pull-requests", kind: "pull-requests" }, revision),
183183
).toBe(false);
184-
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
185-
chosen,
184+
const { isOpen, activeSurfaceId, surfaces } = selectThreadRightPanelState(
185+
useRightPanelStore.getState().byThreadKey,
186+
refA,
186187
);
188+
expect({ isOpen, activeSurfaceId, surfaces }).toEqual({
189+
isOpen: chosen.isOpen,
190+
activeSurfaceId: chosen.activeSurfaceId,
191+
surfaces: chosen.surfaces,
192+
});
187193
});
188194

189195
it("allows automatic panels for a later turn after a manual choice", () => {
@@ -193,10 +199,73 @@ describe("rightPanelStore", () => {
193199
expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
194200

195201
const nextTurnRevision = store.getUserActionRevision(refA);
196-
expect(store.openProactive(refA, completedDiff, nextTurnRevision)).toBe(true);
202+
expect(
203+
store.openProactive(
204+
refA,
205+
{ ...completedDiff, turnId: RunId.make("turn-2") },
206+
nextTurnRevision,
207+
),
208+
).toBe(true);
197209
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
198210
});
199211

212+
it.each([
213+
{ dismissal: "hide", dismiss: () => useRightPanelStore.getState().close(refA) },
214+
{
215+
dismissal: "close tab",
216+
dismiss: () => useRightPanelStore.getState().closeSurface(refA, "diff"),
217+
},
218+
])("offers each turn's diff once after $dismissal and reload", ({ dismiss }) => {
219+
const store = useRightPanelStore.getState();
220+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
221+
dismiss();
222+
const persisted = JSON.parse(
223+
JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
224+
);
225+
useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
226+
227+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
228+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
229+
230+
const nextTurn = { ...completedDiff, turnId: RunId.make("turn-2") };
231+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(true);
232+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
233+
});
234+
235+
it.each([
236+
{ action: "open file", act: () => useRightPanelStore.getState().openFile(refA, "src/app.ts") },
237+
{ action: "open files", act: () => useRightPanelStore.getState().open(refA, "files") },
238+
{
239+
action: "open pull request",
240+
act: () => useRightPanelStore.getState().openPullRequest(refA, linkedPullRequest),
241+
},
242+
])("keeps the offered turn after dismissing, then $action, then reload", ({ act }) => {
243+
const store = useRightPanelStore.getState();
244+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
245+
store.close(refA);
246+
act();
247+
const persisted = JSON.parse(
248+
JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
249+
);
250+
useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
251+
const before = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
252+
253+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
254+
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
255+
before,
256+
);
257+
});
258+
259+
it("does not retry a diff offer the user already declined", () => {
260+
const store = useRightPanelStore.getState();
261+
const revision = store.getUserActionRevision(refA);
262+
store.openFile(refA, "src/app.ts");
263+
expect(store.openProactive(refA, completedDiff, revision)).toBe(false);
264+
265+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
266+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("file");
267+
});
268+
200269
it("keeps manual choices scoped to their thread and environment", () => {
201270
const otherEnvironment = scopeThreadRef("env-2" as EnvironmentId, refA.threadId);
202271
const store = useRightPanelStore.getState();

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
1111
import {
1212
EnvironmentId,
13+
RunId,
1314
ThreadId,
1415
type ChatFileAttachment,
1516
type ScopedThreadRef,
@@ -111,13 +112,19 @@ export interface ThreadRightPanelState {
111112
activeSurfaceId: string | null;
112113
surfaces: RightPanelSurface[];
113114
dismissedDeviceSurfaceIds?: string[];
115+
/** The last turn whose diff was offered automatically. Each turn is offered once. */
116+
proactiveDiffTurnId?: RunId;
114117
}
115118

116119
export interface ThreadPanelVisibility {
117120
inlineOpen: boolean;
118121
popoverOpen: boolean;
119122
}
120123

124+
export type ProactivePanelRequest =
125+
| Extract<RightPanelSurface, { kind: "pull-request" | "pull-requests" }>
126+
| { kind: "diff"; turnId: RunId };
127+
121128
interface RightPanelStoreState {
122129
byThreadKey: Record<string, ThreadRightPanelState>;
123130
threadPanelVisibilityByThreadKey: Record<string, ThreadPanelVisibility>;
@@ -130,7 +137,7 @@ interface RightPanelStoreState {
130137
*/
131138
openProactive: (
132139
ref: ScopedThreadRef,
133-
surface: Extract<RightPanelSurface, { kind: "diff" | "pull-request" | "pull-requests" }>,
140+
request: ProactivePanelRequest,
134141
expectedUserActionRevision: number,
135142
) => boolean;
136143
open: (
@@ -301,12 +308,18 @@ const updateThreadStateMap = (
301308
updater: (current: ThreadRightPanelState) => ThreadRightPanelState,
302309
): Record<string, ThreadRightPanelState> => {
303310
const current = byThreadKey[threadKey] ?? EMPTY_THREAD_STATE;
304-
const next = updater(current);
311+
const updated = updater(current);
312+
// Many actions rebuild the thread state from scratch. Only `openProactive` replaces the offer.
313+
const next =
314+
updated.proactiveDiffTurnId === undefined && current.proactiveDiffTurnId !== undefined
315+
? { ...updated, proactiveDiffTurnId: current.proactiveDiffTurnId }
316+
: updated;
305317
if (
306318
!next.isOpen &&
307319
next.activeSurfaceId === null &&
308320
next.surfaces.length === 0 &&
309-
!next.dismissedDeviceSurfaceIds?.length
321+
!next.dismissedDeviceSurfaceIds?.length &&
322+
next.proactiveDiffTurnId === undefined
310323
) {
311324
if (!(threadKey in byThreadKey)) return byThreadKey;
312325
const { [threadKey]: _removed, ...rest } = byThreadKey;
@@ -534,6 +547,9 @@ export function migratePersistedRightPanelState(persistedState: unknown): {
534547
isOpen,
535548
surfaces,
536549
activeSurfaceId,
550+
...(typeof validThreadState?.proactiveDiffTurnId === "string"
551+
? { proactiveDiffTurnId: RunId.make(validThreadState.proactiveDiffTurnId) }
552+
: {}),
537553
...(Array.isArray(validThreadState?.dismissedDeviceSurfaceIds)
538554
? {
539555
dismissedDeviceSurfaceIds:
@@ -573,26 +589,33 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
573589
userActionRevisionByThreadKey: {},
574590
getUserActionRevision: (ref) =>
575591
get().userActionRevisionByThreadKey[scopedThreadKey(ref)] ?? 0,
576-
openProactive: (ref, surface, expectedUserActionRevision) => {
592+
openProactive: (ref, request, expectedUserActionRevision) => {
577593
let opened = false;
578594
set((state) => {
579595
const threadKey = scopedThreadKey(ref);
580-
if (
581-
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
582-
) {
583-
return state;
596+
if (request.kind === "diff") {
597+
const current = selectThreadRightPanelState(state.byThreadKey, ref);
598+
if (current.proactiveDiffTurnId === request.turnId) return state;
599+
// A linked PR takes priority over a completed-turn diff.
600+
const pullRequestActive =
601+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
602+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests";
603+
opened =
604+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) ===
605+
expectedUserActionRevision && !pullRequestActive;
606+
// Record the offer even when refused, so a revisit does not retry this turn.
607+
return automaticUpdate(state, threadKey, (thread) => ({
608+
...(opened ? upsertSurface(thread, singletonSurface("diff")) : thread),
609+
proactiveDiffTurnId: request.turnId,
610+
}));
584611
}
585-
// A linked PR takes priority over a completed-turn diff. Manual actions
586-
// always apply, and later user choices reject both proactive requests.
587612
if (
588-
surface.kind === "diff" &&
589-
(selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
590-
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests")
613+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
591614
) {
592615
return state;
593616
}
594617
opened = true;
595-
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, surface));
618+
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, request));
596619
});
597620
return opened;
598621
},

0 commit comments

Comments
 (0)