Skip to content

Commit 68db8d9

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 b83d731 commit 68db8d9

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
@@ -5668,7 +5668,13 @@ export default function ChatView(props: ChatViewProps) {
56685668
diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
56695669
};
56705670
if (diffAction !== "open" || newlyCompletedTurnId === null) return;
5671-
if (!panels.openProactive(activeThreadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
5671+
if (
5672+
!panels.openProactive(
5673+
activeThreadRef,
5674+
{ kind: "diff", turnId: newlyCompletedTurnId },
5675+
userActionRevision,
5676+
)
5677+
) {
56725678
return;
56735679
}
56745680
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

@@ -112,13 +112,19 @@ export interface ThreadRightPanelState {
112112
activeSurfaceId: string | null;
113113
surfaces: RightPanelSurface[];
114114
dismissedDeviceSurfaceIds?: string[];
115+
/** The last turn whose diff was offered automatically. Each turn is offered once. */
116+
proactiveDiffTurnId?: RunId;
115117
}
116118

117119
export interface ThreadPanelVisibility {
118120
inlineOpen: boolean;
119121
popoverOpen: boolean;
120122
}
121123

124+
export type ProactivePanelRequest =
125+
| Extract<RightPanelSurface, { kind: "pull-request" | "pull-requests" }>
126+
| { kind: "diff"; turnId: RunId };
127+
122128
interface RightPanelStoreState {
123129
byThreadKey: Record<string, ThreadRightPanelState>;
124130
threadPanelVisibilityByThreadKey: Record<string, ThreadPanelVisibility>;
@@ -132,7 +138,7 @@ interface RightPanelStoreState {
132138
*/
133139
openProactive: (
134140
ref: ScopedThreadRef,
135-
surface: Extract<RightPanelSurface, { kind: "diff" | "pull-request" | "pull-requests" }>,
141+
request: ProactivePanelRequest,
136142
expectedUserActionRevision: number,
137143
) => boolean;
138144
open: (
@@ -307,12 +313,18 @@ const updateThreadStateMap = (
307313
updater: (current: ThreadRightPanelState) => ThreadRightPanelState,
308314
): Record<string, ThreadRightPanelState> => {
309315
const current = byThreadKey[threadKey] ?? EMPTY_THREAD_STATE;
310-
const next = updater(current);
316+
const updated = updater(current);
317+
// Many actions rebuild the thread state from scratch. Only `openProactive` replaces the offer.
318+
const next =
319+
updated.proactiveDiffTurnId === undefined && current.proactiveDiffTurnId !== undefined
320+
? { ...updated, proactiveDiffTurnId: current.proactiveDiffTurnId }
321+
: updated;
311322
if (
312323
!next.isOpen &&
313324
next.activeSurfaceId === null &&
314325
next.surfaces.length === 0 &&
315-
!next.dismissedDeviceSurfaceIds?.length
326+
!next.dismissedDeviceSurfaceIds?.length &&
327+
next.proactiveDiffTurnId === undefined
316328
) {
317329
if (!(threadKey in byThreadKey)) return byThreadKey;
318330
const { [threadKey]: _removed, ...rest } = byThreadKey;
@@ -555,6 +567,9 @@ export function migratePersistedRightPanelState(persistedState: unknown): {
555567
isOpen,
556568
surfaces,
557569
activeSurfaceId,
570+
...(typeof validThreadState?.proactiveDiffTurnId === "string"
571+
? { proactiveDiffTurnId: RunId.make(validThreadState.proactiveDiffTurnId) }
572+
: {}),
558573
...(Array.isArray(validThreadState?.dismissedDeviceSurfaceIds)
559574
? {
560575
dismissedDeviceSurfaceIds:
@@ -595,26 +610,33 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
595610
closeRevisionByThreadKey: {},
596611
getUserActionRevision: (ref) =>
597612
get().userActionRevisionByThreadKey[scopedThreadKey(ref)] ?? 0,
598-
openProactive: (ref, surface, expectedUserActionRevision) => {
613+
openProactive: (ref, request, expectedUserActionRevision) => {
599614
let opened = false;
600615
set((state) => {
601616
const threadKey = scopedThreadKey(ref);
602-
if (
603-
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
604-
) {
605-
return state;
617+
if (request.kind === "diff") {
618+
const current = selectThreadRightPanelState(state.byThreadKey, ref);
619+
if (current.proactiveDiffTurnId === request.turnId) return state;
620+
// A linked PR takes priority over a completed-turn diff.
621+
const pullRequestActive =
622+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
623+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests";
624+
opened =
625+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) ===
626+
expectedUserActionRevision && !pullRequestActive;
627+
// Record the offer even when refused, so a revisit does not retry this turn.
628+
return automaticUpdate(state, threadKey, (thread) => ({
629+
...(opened ? upsertSurface(thread, singletonSurface("diff")) : thread),
630+
proactiveDiffTurnId: request.turnId,
631+
}));
606632
}
607-
// A linked PR takes priority over a completed-turn diff. Manual actions
608-
// always apply, and later user choices reject both proactive requests.
609633
if (
610-
surface.kind === "diff" &&
611-
(selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
612-
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests")
634+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
613635
) {
614636
return state;
615637
}
616638
opened = true;
617-
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, surface));
639+
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, request));
618640
});
619641
return opened;
620642
},

0 commit comments

Comments
 (0)