Skip to content

Commit d364920

Browse files
committed
fix(ui): keep imported custom themes out of profile storage
Custom palettes are browser-local by contract, so a profile must never carry theme=custom to a browser that cannot render it. The wire contract drops custom from the storable theme values (readers self-heal any stored value), a profile-bound custom selection stays retained browser-local (including the offline-queued path), and the exhaustive theme test now encodes the exception.
1 parent 25f70bb commit d364920

5 files changed

Lines changed: 59 additions & 16 deletions

File tree

‎docs/concepts/user-model.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Turning **Git co-author credit** off stops attribution for future runs. It does
3434

3535
When a Control UI connection is bound to an authenticated Gateway profile, its theme, theme mode, and accent color are stored per profile in the existing `user_preferences` table in the shared state database. Those choices follow that person across devices without changing appearance for other people on the same Gateway.
3636

37-
Profile theme and theme mode preferences override their gateway-wide `ui.prefs` settings and otherwise fall back to the active theme's defaults. Accent precedence is the profile's `ui.accent` preference, gateway-wide `ui.prefs.accent`, `ui.seamColor`, and finally the active theme's default accent. Restoring a default clears only the profile preference. Connections without an authenticated profile, including token, password, or unauthenticated connections, keep the existing gateway-wide appearance behavior unchanged. Language, chat preferences, and sidebar entries continue using gateway configuration.
37+
Profile theme and theme mode preferences override their gateway-wide `ui.prefs` settings and otherwise fall back to the active theme's defaults. The imported custom theme is the exception: its palette lives only in the browser that imported it, so selecting it stays browser-local and never follows the profile. Accent precedence is the profile's `ui.accent` preference, gateway-wide `ui.prefs.accent`, `ui.seamColor`, and finally the active theme's default accent. Restoring a default clears only the profile preference. Connections without an authenticated profile, including token, password, or unauthenticated connections, keep the existing gateway-wide appearance behavior unchanged. Language, chat preferences, and sidebar entries continue using gateway configuration.
3838

3939
## Write directives, not observations
4040

‎packages/gateway-protocol/src/schema/ui-appearance-preferences.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ export const UI_APPEARANCE_PREFERENCE_KEYS = {
77
export type UiAppearancePreferenceKey =
88
(typeof UI_APPEARANCE_PREFERENCE_KEYS)[keyof typeof UI_APPEARANCE_PREFERENCE_KEYS];
99

10-
// Wire-contract list of storable theme names. The Control UI derives its own
11-
// theme set from this tuple; a theme shipped in the UI but missing here would
12-
// silently drop that profile preference on read.
10+
// Wire-contract list of profile-storable theme names. The Control UI derives
11+
// its synced-theme handling from this tuple; a theme shipped in the UI but
12+
// missing here would silently drop that profile preference on read.
13+
// "custom" is deliberately absent: imported palettes are browser-local, so a
14+
// custom selection must never follow the profile to a browser that cannot
15+
// render it — it stays device-local instead.
1316
export const UI_APPEARANCE_THEME_VALUES = [
1417
"claw",
1518
"knot",
@@ -18,7 +21,6 @@ export const UI_APPEARANCE_THEME_VALUES = [
1821
"tide",
1922
"beacon",
2023
"phosphor",
21-
"custom",
2224
] as const;
2325
const UI_APPEARANCE_THEMES = new Set<string>(UI_APPEARANCE_THEME_VALUES);
2426
const UI_APPEARANCE_THEME_MODES = new Set(["light", "dark", "system"]);

‎ui/src/app/server-prefs-state.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import type { ThemeMode, ThemeName } from "./theme.ts";
1515

1616
// Derived from the wire contract so a theme the profile store rejects can never
1717
// be offered here; new Set<ThemeName> makes an unknown protocol name a type error.
18-
const THEMES: ReadonlySet<ThemeName> = new Set<ThemeName>(UI_APPEARANCE_THEME_VALUES);
18+
// "custom" is config-syncable (honored only by browsers with an imported
19+
// palette) but intentionally not profile-storable, so it is appended here
20+
// rather than added to the wire contract.
21+
const THEMES: ReadonlySet<ThemeName> = new Set<ThemeName>([
22+
...UI_APPEARANCE_THEME_VALUES,
23+
"custom",
24+
]);
1925

2026
export function isAppearancePref(key: string): key is "theme" | "themeMode" | "accent" {
2127
return key === "theme" || key === "themeMode" || key === "accent";

‎ui/src/app/server-prefs.profile.test.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,49 @@ afterEach(() => {
3535

3636
describe("profile-bound appearance preferences", () => {
3737
it("stores every Control UI theme name the profile wire contract knows", () => {
38-
// Record<ThemeName, true> turns a theme added to the UI but missing from
39-
// UI_APPEARANCE_THEME_VALUES into a compile error here, and the loop turns
40-
// it into a runtime failure — a mismatch silently drops profile themes.
41-
const everyTheme: Record<ThemeName, true> = {
38+
// Record<ThemeName, boolean> turns a theme added to the UI but missing from
39+
// this table into a compile error, and the loop turns a wire-contract
40+
// mismatch into a runtime failure — a mismatch silently drops profile
41+
// themes. "custom" is the deliberate exception: its palette is
42+
// browser-local, so the selection must never follow the profile.
43+
const profileStorable: Record<ThemeName, boolean> = {
4244
claw: true,
4345
knot: true,
4446
dash: true,
4547
absolutely: true,
4648
tide: true,
4749
beacon: true,
4850
phosphor: true,
49-
custom: true,
51+
custom: false,
5052
};
51-
for (const theme of Object.keys(everyTheme)) {
53+
for (const [theme, storable] of Object.entries(profileStorable)) {
5254
expect(normalizeUiAppearancePreference(UI_APPEARANCE_PREFERENCE_KEYS.theme, theme)).toBe(
53-
theme,
55+
storable ? theme : undefined,
5456
);
5557
}
5658
});
5759

60+
it("keeps a profile-bound custom theme selection in this browser only", async () => {
61+
const request = vi.fn(async () => ({ status: "ok" as const }));
62+
const writer = createServerPrefsWriter(request, scope, true, { ok: true }, false);
63+
const afterCommit = vi.fn();
64+
65+
pushServerUiPrefs(
66+
writer,
67+
{ theme: "custom", accent: "#123456" },
68+
{ profileId, canWrite: true, afterCommit },
69+
);
70+
71+
// The accent still syncs; the custom theme is retained browser-local and
72+
// never reaches users.prefs.set.
73+
await waitForFast(() =>
74+
expect(request).toHaveBeenCalledExactlyOnceWith("users.prefs.set", {
75+
entries: { "ui.accent": "#123456" },
76+
}),
77+
);
78+
expect(afterCommit).toHaveBeenCalledWith({ needsRefresh: false, retainedLocal: true });
79+
});
80+
5881
it("overlays profile appearance values without changing anonymous snapshot resolution", () => {
5982
const config = configWithPrefs({ theme: "claw" });
6083
const settings = { ...loadSettings(), theme: "knot" as const };

‎ui/src/app/server-prefs.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ async function drainPendingPrefs(writer: ServerUiPrefsWriter, epoch: number): Pr
495495
if (!pendingPrefs) {
496496
return;
497497
}
498+
if (pushProfileId && pendingPrefs.theme === "custom") {
499+
// Offline-queued custom theme reaching a profile connection: browser-local
500+
// by contract, so retain it here instead of syncing it to the profile.
501+
cancelPendingKeys(pendingScope, ["theme"]);
502+
updateRetainedLocalKeys(pendingScope, ["theme"], true);
503+
continue;
504+
}
498505
const profileBatch: ServerUiPrefs = {};
499506
if (pushProfileId && pushCanWrite) {
500507
for (const key of SYNCED_PREF_KEYS) {
@@ -658,9 +665,14 @@ export function pushServerUiPrefs(
658665
pushAfterCommit = hooks.afterCommit;
659666
const keys = SYNCED_PREF_KEYS.filter((key) => Object.hasOwn(prefs, key));
660667
const blockedKeys = writer.state.connected
661-
? keys.filter((key) =>
662-
pushProfileId && isAppearancePref(key) ? !pushCanWrite : writer.canPatch === false,
663-
)
668+
? keys.filter((key) => {
669+
if (pushProfileId && isAppearancePref(key)) {
670+
// Imported custom palettes are browser-local by contract; a profile
671+
// must never carry a theme another browser cannot render.
672+
return !pushCanWrite || (key === "theme" && prefs.theme === "custom");
673+
}
674+
return writer.canPatch === false;
675+
})
664676
: [];
665677
if (blockedKeys.length) {
666678
// A connected read-only edit is intentionally browser-local. Supersede only

0 commit comments

Comments
 (0)