Skip to content

Commit d6f5342

Browse files
committed
fix(gui): ignore stale settings polls
1 parent 9fcba57 commit d6f5342

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎gui/src/pages/Dashboard.tsx‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { formatUptime } from "../formatUptime";
33
import { IconAlert, IconExternal, IconInfo, IconRefresh, IconX } from "../icons";
44
import { Trans } from "../i18n/provider";
55
import { useI18n, type TKey } from "../i18n/shared";
6-
import { startupRiskDetailKey } from "../startup-health-ui";
6+
import { settingsPollMayCommit, startupRiskDetailKey } from "../startup-health-ui";
77
import { formatTokens } from "../format-tokens";
88
import { EmptyState, Select } from "../ui";
99

@@ -222,6 +222,9 @@ export default function Dashboard({ apiBase }: { apiBase: string }) {
222222
const updateRetryRef = useRef(0);
223223
const updateRetryTimerRef = useRef<number | null>(null);
224224
const updateRequestEpochRef = useRef(0);
225+
const settingsRequestEpochRef = useRef(0);
226+
const settingsMutationEpochRef = useRef(0);
227+
const settingsMutationInFlightRef = useRef(false);
225228
const [updateCheck, setUpdateCheck] = useState<UpdateCheckData | null>(null);
226229
const [updateError, setUpdateError] = useState<string | null>(null);
227230
const [updateJob, setUpdateJob] = useState<UpdateJob | null>(null);
@@ -244,6 +247,8 @@ export default function Dashboard({ apiBase }: { apiBase: string }) {
244247

245248
useEffect(() => {
246249
const fetchData = async () => {
250+
const settingsRequestEpoch = ++settingsRequestEpochRef.current;
251+
const settingsMutationEpoch = settingsMutationEpochRef.current;
247252
try {
248253
const [hRes, pRes, sRes, scRes, shRes, uRes] = await Promise.all([
249254
fetch(`${apiBase}/healthz`),
@@ -255,7 +260,15 @@ export default function Dashboard({ apiBase }: { apiBase: string }) {
255260
]);
256261
setHealth(await hRes.json());
257262
setProviders(await pRes.json());
258-
setSettings(await sRes.json());
263+
const nextSettings = await sRes.json() as SettingsData;
264+
if (settingsPollMayCommit(
265+
{ request: settingsRequestEpoch, mutation: settingsMutationEpoch },
266+
{
267+
request: settingsRequestEpochRef.current,
268+
mutation: settingsMutationEpochRef.current,
269+
mutationInFlight: settingsMutationInFlightRef.current,
270+
},
271+
)) setSettings(nextSettings);
259272
setSidecar(await scRes.json());
260273
// Old servers fall through to the SPA HTML for this route; don't let a parse
261274
// failure here take down the whole dashboard.
@@ -296,7 +309,10 @@ export default function Dashboard({ apiBase }: { apiBase: string }) {
296309
};
297310
fetchData();
298311
const interval = setInterval(fetchData, 5000);
299-
return () => clearInterval(interval);
312+
return () => {
313+
clearInterval(interval);
314+
settingsRequestEpochRef.current += 1;
315+
};
300316
}, [apiBase]);
301317

302318
useEffect(() => {
@@ -450,6 +466,7 @@ export default function Dashboard({ apiBase }: { apiBase: string }) {
450466
if (!settings || settingsSaving) return;
451467
const next = !settings.codexAutoStart;
452468
setSettingsSaving(true);
469+
settingsMutationInFlightRef.current = true;
453470
setSettings({ ...settings, codexAutoStart: next });
454471
try {
455472
const res = await fetch(`${apiBase}/api/settings`, {
@@ -459,11 +476,13 @@ export default function Dashboard({ apiBase }: { apiBase: string }) {
459476
});
460477
if (!res.ok) throw new Error("save failed");
461478
const data = await res.json() as { codexAutoStart: boolean; startupHealth?: SettingsData["startupHealth"] };
479+
settingsMutationEpochRef.current += 1;
462480
setSettings(prev => prev ? { ...prev, codexAutoStart: data.codexAutoStart, startupHealth: data.startupHealth ?? prev.startupHealth } : prev);
463481
} catch {
464482
setSettings(prev => prev ? { ...prev, codexAutoStart: !next } : prev);
465483
setError(true);
466484
} finally {
485+
settingsMutationInFlightRef.current = false;
467486
setSettingsSaving(false);
468487
}
469488
};

‎gui/src/startup-health-ui.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,17 @@ export function startupRiskDetailKey(health: StartupRiskDetail): TKey {
1010
if (health.shimCoverage === "cli-only") return "startup.riskDetailWindowsShim";
1111
return "startup.riskDetail";
1212
}
13+
14+
export interface SettingsPollEpoch {
15+
request: number;
16+
mutation: number;
17+
}
18+
19+
export function settingsPollMayCommit(
20+
started: SettingsPollEpoch,
21+
current: SettingsPollEpoch & { mutationInFlight: boolean },
22+
): boolean {
23+
return !current.mutationInFlight
24+
&& started.request === current.request
25+
&& started.mutation === current.mutation;
26+
}

‎tests/startup-health-ui.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { settingsPollMayCommit, startupRiskDetailKey } from "../gui/src/startup-health-ui";
3+
4+
describe("startup health UI decisions", () => {
5+
test("selects the shared risk-detail message", () => {
6+
expect(startupRiskDetailKey({ routingKind: "custom-local", shimCoverage: "none" }))
7+
.toBe("startup.riskDetailCustomLocal");
8+
expect(startupRiskDetailKey({ routingKind: "opencodex-local", shimCoverage: "cli-only" }))
9+
.toBe("startup.riskDetailWindowsShim");
10+
expect(startupRiskDetailKey({ routingKind: "unknown", shimCoverage: "none" }))
11+
.toBe("startup.riskDetail");
12+
});
13+
14+
test("rejects stale or mutation-racing settings polls", () => {
15+
const started = { request: 4, mutation: 2 };
16+
expect(settingsPollMayCommit(started, { request: 4, mutation: 2, mutationInFlight: false })).toBe(true);
17+
expect(settingsPollMayCommit(started, { request: 5, mutation: 2, mutationInFlight: false })).toBe(false);
18+
expect(settingsPollMayCommit(started, { request: 4, mutation: 3, mutationInFlight: false })).toBe(false);
19+
expect(settingsPollMayCommit(started, { request: 4, mutation: 2, mutationInFlight: true })).toBe(false);
20+
});
21+
});

0 commit comments

Comments
 (0)