Skip to content

Commit bccad27

Browse files
t3dotggclaude
andauthored
fix(web): keep manual panel choices during a turn (pingdotgg#10113)
Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent e0adcc8 commit bccad27

5 files changed

Lines changed: 455 additions & 172 deletions

File tree

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

Lines changed: 127 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
1414
import type { Thread, ThreadShell, TurnDiffSummary } from "../types";
1515
import { deriveProviderInstanceEntries, NO_PROVIDER_MODEL_SELECTION } from "../providerInstances";
1616
import type { CodexArtifactTemplate } from "@t3tools/client-runtime/codex-artifact-templates";
17-
import type { RightPanelSurface } from "../rightPanelStore";
17+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
18+
import {
19+
type RightPanelSurface,
20+
pullRequestSurface,
21+
selectActiveRightPanelSurface,
22+
useRightPanelStore,
23+
} from "../rightPanelStore";
24+
import {
25+
selectThreadPreviewMiniPlayer,
26+
usePreviewMiniPlayerStore,
27+
} from "../previewMiniPlayerStore";
1828
import {
1929
MAX_HIDDEN_MOUNTED_PREVIEW_THREADS,
2030
MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
@@ -39,6 +49,7 @@ import {
3949
resolveComposerInteractionMode,
4050
resolveComposerProviderSelection,
4151
resolveDraftPromotionNavigationTarget,
52+
observeProactivePanelUserChoice,
4253
resolveProactiveTurnDiffAction,
4354
resolveThreadMetadataUpdateForNextTurn,
4455
resolveSendEnvMode,
@@ -95,6 +106,37 @@ describe("agent browser close confirmation", () => {
95106
});
96107

97108
describe("floating browser preview", () => {
109+
it("keeps agent preview intent when a user selects its browser tab and then switches away", () => {
110+
useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
111+
usePreviewMiniPlayerStore.setState({ byThreadKey: {} });
112+
const ref = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1"));
113+
const panels = useRightPanelStore.getState();
114+
const revision = panels.getUserActionRevision(ref);
115+
usePreviewMiniPlayerStore.getState().open(ref, "agent-tab");
116+
panels.reconcileBrowserSurfaces(ref, ["agent-tab"]);
117+
const intent = selectThreadPreviewMiniPlayer(
118+
usePreviewMiniPlayerStore.getState().byThreadKey,
119+
ref,
120+
);
121+
const isFloating = () =>
122+
shouldRenderPreviewMiniPlayer(
123+
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, ref)
124+
?.tabId ?? null,
125+
selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, ref),
126+
);
127+
128+
panels.openProactive(ref, { id: "diff", kind: "diff" }, revision);
129+
expect(isFloating()).toBe(true);
130+
panels.activateSurface(ref, "browser:agent-tab");
131+
expect(isFloating()).toBe(false);
132+
expect(panels.openProactive(ref, { id: "diff", kind: "diff" }, revision)).toBe(false);
133+
panels.open(ref, "diff");
134+
expect(isFloating()).toBe(true);
135+
expect(
136+
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, ref),
137+
).toBe(intent);
138+
});
139+
98140
it("only hides the duplicate while the same browser is rendered in the panel", () => {
99141
expect(shouldRenderPreviewMiniPlayer(null, null)).toBe(false);
100142
expect(
@@ -116,6 +158,90 @@ describe("floating browser preview", () => {
116158
});
117159

118160
describe("proactive panels", () => {
161+
it("keeps a manual PR selection made after following a replacement while loading", () => {
162+
useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
163+
const ref = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1"));
164+
const panels = useRightPanelStore.getState();
165+
const oldPr = pullRequestSurface({
166+
projectId: "project-1",
167+
repository: "owner/repo",
168+
number: 1,
169+
});
170+
const replacement = pullRequestSurface({ ...oldPr, number: 2 });
171+
const turnId = TurnId.make("turn-1");
172+
panels.openPullRequest(ref, oldPr);
173+
const loading = observeProactivePanelUserChoice(null, {
174+
threadKey: "env-1:thread-1",
175+
runningTurnId: turnId,
176+
userActionRevision: panels.getUserActionRevision(ref),
177+
});
178+
expect(panels.openProactive(ref, replacement, loading.userActionRevision)).toBe(true);
179+
180+
panels.activateSurface(ref, oldPr.id);
181+
const loaded = observeProactivePanelUserChoice(loading, {
182+
threadKey: loading.threadKey,
183+
runningTurnId: turnId,
184+
userActionRevision: panels.getUserActionRevision(ref),
185+
});
186+
expect(panels.openProactive(ref, replacement, loaded.userActionRevision)).toBe(false);
187+
expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, ref)).toEqual(
188+
oldPr,
189+
);
190+
expect(shouldOpenProactivePullRequest(loaded.targetKey, "owner/repo:2")).toBe(false);
191+
expect(
192+
shouldOpenProactiveTurnDiff({
193+
previousRunningTurnId: loaded.runningTurnId,
194+
runningTurnId: null,
195+
settledTurnId: turnId,
196+
turnCompleted: true,
197+
}),
198+
).toBe(false);
199+
});
200+
201+
it.each(["idle", "loading", "observed"] as const)(
202+
"captures a new turn's choice once with initial state %s",
203+
(initialState) => {
204+
useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
205+
const ref = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1"));
206+
const panels = useRightPanelStore.getState();
207+
const firstTurn = TurnId.make("turn-1");
208+
const nextTurn = TurnId.make("turn-2");
209+
const initial = observeProactivePanelUserChoice(null, {
210+
threadKey: "env-1:thread-1",
211+
runningTurnId: initialState === "idle" ? null : firstTurn,
212+
userActionRevision: panels.getUserActionRevision(ref),
213+
});
214+
panels.openFile(ref, "src/first.ts");
215+
const loadingNextTurn = observeProactivePanelUserChoice(
216+
{
217+
...initial,
218+
...(initialState === "observed" ? { runningTurnId: firstTurn, targetKey: null } : {}),
219+
},
220+
{
221+
threadKey: initial.threadKey,
222+
runningTurnId: nextTurn,
223+
userActionRevision: panels.getUserActionRevision(ref),
224+
},
225+
);
226+
expect(
227+
panels.openProactive(ref, { id: "diff", kind: "diff" }, loadingNextTurn.userActionRevision),
228+
).toBe(true);
229+
230+
panels.openFile(ref, "src/second.ts");
231+
const loaded = observeProactivePanelUserChoice(loadingNextTurn, {
232+
threadKey: initial.threadKey,
233+
runningTurnId: nextTurn,
234+
userActionRevision: panels.getUserActionRevision(ref),
235+
});
236+
expect(
237+
panels.openProactive(ref, { id: "diff", kind: "diff" }, loaded.userActionRevision),
238+
).toBe(false);
239+
expect(
240+
selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, ref)?.id,
241+
).toBe("file:src/second.ts");
242+
},
243+
);
244+
119245
it("opens a pull request only after a newly observed link appears", () => {
120246
expect(shouldOpenProactivePullRequest(undefined, "project:repo:42")).toBe(false);
121247
expect(shouldOpenProactivePullRequest(null, "project:repo:42")).toBe(true);
@@ -173,14 +299,12 @@ describe("proactive panels", () => {
173299
resolveProactiveTurnDiffAction({
174300
checkpoint: changedCheckpoint,
175301
isGitRepo: true,
176-
activeSurfaceKind: null,
177302
}),
178303
).toBe("open");
179304
expect(
180305
resolveProactiveTurnDiffAction({
181306
checkpoint: unchangedCheckpoint,
182307
isGitRepo: true,
183-
activeSurfaceKind: null,
184308
}),
185309
).toBe("ignore");
186310
});
@@ -199,39 +323,21 @@ describe("proactive panels", () => {
199323
resolveProactiveTurnDiffAction({
200324
checkpoint: undefined,
201325
isGitRepo: true,
202-
activeSurfaceKind: null,
203326
}),
204327
).toBe("defer");
205328
expect(
206329
resolveProactiveTurnDiffAction({
207330
checkpoint: missingCheckpoint,
208331
isGitRepo: true,
209-
activeSurfaceKind: null,
210332
}),
211333
).toBe("defer");
212334
expect(
213335
resolveProactiveTurnDiffAction({
214336
checkpoint: changedCheckpoint,
215337
isGitRepo: undefined,
216-
activeSurfaceKind: null,
217338
}),
218339
).toBe("defer");
219340
});
220-
221-
it("keeps an active pull request above a completed turn diff", () => {
222-
const changedCheckpoint = {
223-
status: "ready",
224-
files: [{ path: "src/app.ts", kind: "modified", additions: 1, deletions: 0 }],
225-
} satisfies Pick<TurnDiffSummary, "status" | "files">;
226-
227-
expect(
228-
resolveProactiveTurnDiffAction({
229-
checkpoint: changedCheckpoint,
230-
isGitRepo: true,
231-
activeSurfaceKind: "pull-request",
232-
}),
233-
).toBe("ignore");
234-
});
235341
});
236342

237343
describe("toolGroupConsumesUpwardNavigation", () => {

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ export function shouldOpenProactivePullRequest(
106106
return previousTargetKey !== undefined && targetKey !== null && targetKey !== previousTargetKey;
107107
}
108108

109+
interface ProactivePanelObservation {
110+
threadKey: string;
111+
runningTurnId: TurnId | null | undefined;
112+
targetKey: string | null | undefined;
113+
userActionTurnId: TurnId | null;
114+
userActionRevision: number;
115+
}
116+
117+
/** Capture user intent before loading or metadata writes can defer panel activation. */
118+
export function observeProactivePanelUserChoice(
119+
previous: ProactivePanelObservation | null,
120+
input: { threadKey: string; runningTurnId: TurnId | null; userActionRevision: number },
121+
): ProactivePanelObservation {
122+
const sameThread = previous?.threadKey === input.threadKey;
123+
const newTurn =
124+
sameThread && input.runningTurnId !== null && input.runningTurnId !== previous.userActionTurnId;
125+
return {
126+
threadKey: input.threadKey,
127+
runningTurnId: sameThread ? previous.runningTurnId : undefined,
128+
targetKey: sameThread ? previous.targetKey : undefined,
129+
userActionTurnId: input.runningTurnId ?? (sameThread ? previous.userActionTurnId : null),
130+
userActionRevision:
131+
!sameThread || newTurn ? input.userActionRevision : previous.userActionRevision,
132+
};
133+
}
134+
109135
export function shouldOpenProactiveTurnDiff(input: {
110136
previousRunningTurnId: TurnId | null | undefined;
111137
runningTurnId: TurnId | null;
@@ -124,9 +150,7 @@ export function shouldOpenProactiveTurnDiff(input: {
124150
export function resolveProactiveTurnDiffAction(input: {
125151
checkpoint: Pick<TurnDiffSummary, "status" | "files"> | undefined;
126152
isGitRepo: boolean | undefined;
127-
activeSurfaceKind: RightPanelSurface["kind"] | null;
128153
}): "defer" | "ignore" | "open" {
129-
if (input.activeSurfaceKind === "pull-request") return "ignore";
130154
if (input.checkpoint === undefined || input.checkpoint.status === "missing") return "defer";
131155
if (input.isGitRepo === undefined) return "defer";
132156
if (

0 commit comments

Comments
 (0)