@@ -3,7 +3,7 @@ import { formatUptime } from "../formatUptime";
33import { IconAlert , IconExternal , IconInfo , IconRefresh , IconX } from "../icons" ;
44import { Trans } from "../i18n/provider" ;
55import { useI18n , type TKey } from "../i18n/shared" ;
6- import { startupRiskDetailKey } from "../startup-health-ui" ;
6+ import { settingsPollMayCommit , startupRiskDetailKey } from "../startup-health-ui" ;
77import { formatTokens } from "../format-tokens" ;
88import { 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 } ;
0 commit comments