Skip to content

Commit 6489255

Browse files
fix(web): honour browser defaults on the paths that were skipping them
Three ways the configured browser defaults were not reaching a tab: Restore defaults skipped them entirely. They are unified settings like every other preference on that button, so they now take part in both the dirty labels and the reset. The viewport is a tagged union, so comparing it needs structural equality — identity would have reported a freshly decoded copy of the default as changed. `browserAutoShowFloatingPreview: false` was ignored for `preview_open`. `normalizePreviewOpenInput` filled an omitted `open` with `true` on the server, so by the time the renderer asked "did the agent state a preference?" the answer was always yes. The server now leaves unstated visibility unstated, which is the only thing it can honestly say — the preference is desktop-local and unreadable from there. A preview opened before client settings hydrated was born at the schema defaults and never corrected. The three imperative open paths now await hydration, which costs nothing: they were already async. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent bb0492d commit 6489255

12 files changed

Lines changed: 182 additions & 20 deletions

‎apps/server/src/mcp/toolkits/preview/handlers.test.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { describe, expect, it } from "vite-plus/test";
33
import { normalizePreviewOpenInput } from "./handlers.ts";
44

55
describe("normalizePreviewOpenInput", () => {
6-
it("opens the inline preview and reuses the current tab by default", () => {
7-
expect(normalizePreviewOpenInput({})).toEqual({
8-
open: true,
9-
reuseExistingTab: true,
10-
show: true,
11-
});
6+
it("leaves an unstated visibility for the client preference to decide", () => {
7+
// Filling `open` in here would outrank `browserAutoShowFloatingPreview`,
8+
// which is desktop-local and cannot be read from the server.
9+
expect(normalizePreviewOpenInput({})).toEqual({ reuseExistingTab: true });
1210
});
1311

1412
it("preserves an explicit background-only opt-out", () => {

‎apps/server/src/mcp/toolkits/preview/handlers.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ import * as McpInvocationContext from "../../McpInvocationContext.ts";
1515
import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
1616
import { PreviewSnapshotToolkit, PreviewStandardToolkit, PreviewToolkit } from "./tools.ts";
1717

18+
/**
19+
* Collapses the `show` alias onto `open` and defaults tab reuse.
20+
*
21+
* Deliberately leaves an unstated `open` unstated. Whether a preview the agent
22+
* said nothing about surfaces is the user's `browserAutoShowFloatingPreview`
23+
* preference, which is desktop-local and unreadable from here — filling in
24+
* `true` would silently override it for every `preview_open`.
25+
*/
1826
export function normalizePreviewOpenInput(
1927
input: PreviewAutomationOpenInput,
2028
): PreviewAutomationOpenInput {
21-
const open = input.open ?? input.show ?? true;
29+
const open = input.open ?? input.show;
2230
return {
2331
...input,
24-
open,
25-
show: open,
32+
...(open === undefined ? {} : { open, show: open }),
2633
reuseExistingTab: input.reuseExistingTab ?? true,
2734
};
2835
}

‎apps/web/src/browser/browserDefaults.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import type {
1919
PreviewViewportSetting,
2020
} from "@t3tools/contracts";
2121

22-
import { getClientSettings, useClientSettings } from "~/hooks/useSettings";
22+
import {
23+
ensureClientSettingsHydrated,
24+
getClientSettings,
25+
useClientSettings,
26+
} from "~/hooks/useSettings";
2327

2428
import { resolveResponsiveBrowserViewportSize } from "./browserViewportLayout";
2529

@@ -47,6 +51,18 @@ export function getBrowserDefaults(): BrowserDefaults {
4751
return toBrowserDefaults(getClientSettings());
4852
}
4953

54+
/**
55+
* The defaults, once client settings have actually loaded.
56+
*
57+
* Opening a preview is asynchronous anyway, and before hydration the snapshot
58+
* is the schema defaults rather than the user's — a tab opened in that window
59+
* would be born at the wrong viewport, zoom and appearance and never corrected.
60+
*/
61+
export async function resolveBrowserDefaults(): Promise<BrowserDefaults> {
62+
await ensureClientSettingsHydrated();
63+
return getBrowserDefaults();
64+
}
65+
5066
export function useBrowserDefaults(): BrowserDefaults {
5167
return useClientSettings(toBrowserDefaults);
5268
}

‎apps/web/src/browser/desktopTabLifetime.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ describe("desktopTabLifetime", () => {
5353
const first = acquireDesktopTab("tab_readiness");
5454
const second = acquireDesktopTab("tab_readiness");
5555

56-
expect(createTab).toHaveBeenCalledOnce();
56+
// Both leases share one creation, and it is still in flight: creation now
57+
// waits for client settings to hydrate so the guest is born at the user's
58+
// zoom and appearance rather than painting at the defaults first.
5759
expect(first.ready).toBe(second.ready);
5860

5961
let ready = false;
6062
void first.ready.then(() => {
6163
ready = true;
6264
});
63-
await Promise.resolve();
65+
await vi.waitFor(() => expect(createTab).toHaveBeenCalledOnce());
6466
expect(ready).toBe(false);
6567

6668
resolveCreation?.();

‎apps/web/src/browser/desktopTabLifetime.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { previewBridge } from "~/components/preview/previewBridge";
22

3-
import { browserDefaultTabState } from "./browserDefaults";
3+
import { browserDefaultTabState, resolveBrowserDefaults } from "./browserDefaults";
44
import { stopBrowserRecording } from "./browserRecording";
55

66
interface DesktopTabLease {
@@ -44,8 +44,8 @@ export function acquireDesktopTab(tabId: string): AcquiredDesktopTab {
4444
closeTimer: null,
4545
// Zoom/appearance defaults travel with creation so the guest never
4646
// paints a frame at 100%/system before the preference is applied.
47-
ready: enqueueDesktopTabOperation(tabId, () =>
48-
previewBridge?.createTab(tabId, browserDefaultTabState()),
47+
ready: enqueueDesktopTabOperation(tabId, async () =>
48+
previewBridge?.createTab(tabId, browserDefaultTabState(await resolveBrowserDefaults())),
4949
),
5050
} satisfies DesktopTabLease);
5151
if (current.closeTimer !== null) window.clearTimeout(current.closeTimer);

‎apps/web/src/components/preview/PreviewAutomationHosts.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
} from "~/browser/browserRecording";
3939
import { resolveBrowserRecordingStopTarget } from "~/browser/browserRecordingScope";
4040
import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
41-
import { browserDefaultOpenViewport, getBrowserDefaults } from "~/browser/browserDefaults";
41+
import { browserDefaultOpenViewport, resolveBrowserDefaults } from "~/browser/browserDefaults";
4242
import { runBrowserViewportMutation } from "~/browser/browserViewportActions";
4343
import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
4444
import { isElectron } from "~/env";
@@ -383,7 +383,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
383383
...(resolvedInputUrl ? { url: resolvedInputUrl } : {}),
384384
// An agent that didn't state a size gets the user's
385385
// configured default, same as a hand-opened tab.
386-
viewport: browserDefaultOpenViewport(),
386+
viewport: browserDefaultOpenViewport(await resolveBrowserDefaults()),
387387
},
388388
});
389389
if (result._tag === "Failure") {
@@ -434,7 +434,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
434434
}
435435
const shouldPresentPreview = shouldOpenPreviewMiniPlayer(
436436
input,
437-
getBrowserDefaults().autoShowFloatingPreview,
437+
(await resolveBrowserDefaults()).autoShowFloatingPreview,
438438
);
439439
if (shouldPresentPreview) {
440440
usePreviewMiniPlayerStore.getState().open(threadRef, activeTabId);

‎apps/web/src/components/preview/openPreviewSession.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "@t3tools/contracts";
88
import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
99

10-
import { browserDefaultOpenViewport } from "~/browser/browserDefaults";
10+
import { browserDefaultOpenViewport, resolveBrowserDefaults } from "~/browser/browserDefaults";
1111
import { applyPreviewServerSnapshot, rememberPreviewUrl } from "~/previewStateStore";
1212

1313
interface OpenPreviewSessionInput<E> {
@@ -29,7 +29,7 @@ export async function openPreviewSession<E>(
2929
input: {
3030
threadId: input.threadRef.threadId,
3131
...(input.url === undefined ? {} : { url: input.url }),
32-
viewport: input.viewport ?? browserDefaultOpenViewport(),
32+
viewport: input.viewport ?? browserDefaultOpenViewport(await resolveBrowserDefaults()),
3333
},
3434
});
3535
if (result._tag === "Failure") {

‎apps/web/src/components/preview/previewAutomationOpenReadiness.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,18 @@ describe("preview automation open readiness", () => {
7777
).toBeNull();
7878
});
7979
});
80+
81+
describe("shouldOpenPreviewMiniPlayer with the floating-preview preference", () => {
82+
it("honours the preference when the agent said nothing either way", () => {
83+
// `preview_open` no longer arrives with `open` pre-filled, so an agent
84+
// that omitted it leaves the decision to the user's setting.
85+
expect(shouldOpenPreviewMiniPlayer({}, false)).toBe(false);
86+
expect(shouldOpenPreviewMiniPlayer({}, true)).toBe(true);
87+
});
88+
89+
it("lets an explicit request outrank the preference in both directions", () => {
90+
expect(shouldOpenPreviewMiniPlayer({ open: true }, false)).toBe(true);
91+
expect(shouldOpenPreviewMiniPlayer({ open: false }, true)).toBe(false);
92+
expect(shouldOpenPreviewMiniPlayer({ show: true }, false)).toBe(true);
93+
});
94+
});

‎apps/web/src/components/settings/SettingsPanels.logic.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
backgroundActivitySharedPolicySettings,
1313
buildProviderInstanceUpdatePatch,
1414
formatDiagnosticsDescription,
15+
getChangedBrowserSettingLabels,
1516
getChangedTypographySettingLabels,
17+
isSamePreviewViewport,
1618
hasChangedBackgroundActivitySettings,
1719
isProjectGroupingEnabled,
1820
projectGroupingModeFromToggle,
@@ -242,3 +244,54 @@ describe("buildProviderInstanceUpdatePatch", () => {
242244
expect(patch.providers).toBeUndefined();
243245
});
244246
});
247+
248+
describe("getChangedBrowserSettingLabels", () => {
249+
it("reports nothing for the defaults", () => {
250+
expect(getChangedBrowserSettingLabels(DEFAULT_UNIFIED_SETTINGS)).toEqual([]);
251+
});
252+
253+
it("treats a structurally equal viewport as unchanged", () => {
254+
// The viewport is a tagged union, so identity comparison would report a
255+
// freshly decoded copy of the default as dirty and offer to "restore" it.
256+
expect(
257+
getChangedBrowserSettingLabels({
258+
...DEFAULT_UNIFIED_SETTINGS,
259+
browserDefaultViewport: { ...DEFAULT_UNIFIED_SETTINGS.browserDefaultViewport },
260+
}),
261+
).toEqual([]);
262+
});
263+
264+
it("labels each browser default that differs", () => {
265+
expect(
266+
getChangedBrowserSettingLabels({
267+
...DEFAULT_UNIFIED_SETTINGS,
268+
browserDefaultViewport: { _tag: "freeform", width: 900, height: 600 },
269+
browserDefaultZoomFactor: 1.5,
270+
browserDefaultAppearance: "dark",
271+
browserAutoShowFloatingPreview: !DEFAULT_UNIFIED_SETTINGS.browserAutoShowFloatingPreview,
272+
}),
273+
).toEqual(["Browser viewport", "Browser zoom", "Browser appearance", "Floating preview"]);
274+
});
275+
});
276+
277+
describe("isSamePreviewViewport", () => {
278+
it("separates presets that share a size", () => {
279+
// Two presets can agree on width and height and still be different
280+
// entries in the picker, so the id has to take part in the comparison.
281+
expect(
282+
isSamePreviewViewport(
283+
{ _tag: "preset", width: 390, height: 844, presetId: "iphone-12-pro" },
284+
{ _tag: "preset", width: 390, height: 844, presetId: "ipad-mini" },
285+
),
286+
).toBe(false);
287+
});
288+
289+
it("separates a freeform viewport from a preset of the same size", () => {
290+
expect(
291+
isSamePreviewViewport(
292+
{ _tag: "freeform", width: 390, height: 844 },
293+
{ _tag: "preset", width: 390, height: 844, presetId: "iphone-12-pro" },
294+
),
295+
).toBe(false);
296+
});
297+
});

‎apps/web/src/components/settings/SettingsPanels.logic.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
BackgroundActivitySettings,
44
ProviderDriverKind,
55
ProviderInstanceConfig,
6+
PreviewViewportSetting,
67
ProviderInstanceId,
78
ServerSettings,
89
SidebarProjectGroupingMode,
@@ -108,6 +109,55 @@ export function getChangedTypographySettingLabels(settings: TypographySettings):
108109
];
109110
}
110111

112+
export type BrowserDefaultSettings = Pick<
113+
UnifiedSettings,
114+
| "browserDefaultViewport"
115+
| "browserDefaultZoomFactor"
116+
| "browserDefaultAppearance"
117+
| "browserAutoShowFloatingPreview"
118+
>;
119+
120+
/**
121+
* True when two viewport settings describe the same viewport.
122+
*
123+
* The setting is a tagged union rather than a scalar, so identity comparison
124+
* reports every stored viewport as changed — including one that matches the
125+
* default.
126+
*/
127+
export function isSamePreviewViewport(
128+
left: PreviewViewportSetting,
129+
right: PreviewViewportSetting,
130+
): boolean {
131+
if (left._tag !== right._tag) return false;
132+
if (left._tag === "fill" || right._tag === "fill") return true;
133+
if (left.width !== right.width || left.height !== right.height) return false;
134+
return left._tag === "preset" && right._tag === "preset"
135+
? left.presetId === right.presetId
136+
: true;
137+
}
138+
139+
/** Labels the browser-default rows that differ from the defaults. */
140+
export function getChangedBrowserSettingLabels(settings: BrowserDefaultSettings): string[] {
141+
return [
142+
...(isSamePreviewViewport(
143+
settings.browserDefaultViewport,
144+
DEFAULT_UNIFIED_SETTINGS.browserDefaultViewport,
145+
)
146+
? []
147+
: ["Browser viewport"]),
148+
...(settings.browserDefaultZoomFactor !== DEFAULT_UNIFIED_SETTINGS.browserDefaultZoomFactor
149+
? ["Browser zoom"]
150+
: []),
151+
...(settings.browserDefaultAppearance !== DEFAULT_UNIFIED_SETTINGS.browserDefaultAppearance
152+
? ["Browser appearance"]
153+
: []),
154+
...(settings.browserAutoShowFloatingPreview !==
155+
DEFAULT_UNIFIED_SETTINGS.browserAutoShowFloatingPreview
156+
? ["Floating preview"]
157+
: []),
158+
];
159+
}
160+
111161
export function resolveBackgroundActivityProfileOption(
112162
settings: ServerSettings,
113163
): BackgroundActivityProfile | "advanced" {

0 commit comments

Comments
 (0)