@@ -48,6 +48,8 @@ export default function CodexPoolStrategySetting({ |
48 | 48 | const hydratedRef = useRef(false); |
49 | 49 | const [saving, setSaving] = useState(false); |
50 | 50 | const savingRef = useRef(false); |
| 51 | + /** Set when an /active read arrives mid-save; triggers one post-save refresh. */ |
| 52 | + const deferredActiveRefreshRef = useRef(false); |
51 | 53 | const revisionRef = useRef(0); |
52 | 54 | const [loadError, setLoadError] = useState(false); |
53 | 55 | const [error, setError] = useState<string | null>(null); |
@@ -80,25 +82,47 @@ export default function CodexPoolStrategySetting({ |
80 | 82 | try { |
81 | 83 | const res = await fetch(`${apiBase}/api/codex-auth/active`); |
82 | 84 | if (!res.ok) throw new Error("load"); |
83 | | - applyServer(await res.json() as { |
| 85 | + const payload = await res.json() as { |
84 | 86 | accountPoolStrategy?: unknown; |
85 | 87 | accountPoolStickyLimit?: unknown; |
86 | | - }); |
| 88 | + }; |
| 89 | + // A save started while this GET was in flight — retry once after it settles. |
| 90 | + if (savingRef.current) { |
| 91 | + deferredActiveRefreshRef.current = true; |
| 92 | + return; |
| 93 | + } |
| 94 | + applyServer(payload); |
87 | 95 | } catch { |
88 | | - setLoadError(true); |
| 96 | + if (!savingRef.current) setLoadError(true); |
89 | 97 | } |
90 | 98 | }, [apiBase, applyServer]); |
91 | 99 |
|
| 100 | + const scheduleDeferredActiveRefresh = useCallback(() => { |
| 101 | + if (!deferredActiveRefreshRef.current) return; |
| 102 | + deferredActiveRefreshRef.current = false; |
| 103 | + queueMicrotask(() => { |
| 104 | + if (savingRef.current) { |
| 105 | + deferredActiveRefreshRef.current = true; |
| 106 | + return; |
| 107 | + } |
| 108 | + void load(); |
| 109 | + }); |
| 110 | + }, [load]); |
| 111 | + |
92 | 112 | // Shared /active observer (preferred): same payload the pool already fetched. |
93 | | - // Ignore stale polls that started before a PUT bumped revision, and never apply while saving. |
| 113 | + // Ignore stale polls that started before a PUT bumped revision; mid-save reads |
| 114 | + // arm one post-save /active refresh instead of being dropped forever. |
94 | 115 | // Replay readLastActive on subscribe so late mounts hydrate without waiting a poll. |
95 | 116 | useEffect(() => { |
96 | 117 | if (!subscribeLoadObserver) return; |
97 | 118 | const observer: CodexAccountLoadObserver = { |
98 | 119 | beginActiveRead: () => revisionRef.current, |
99 | 120 | acceptActiveRead: (value, startedRevision) => { |
100 | 121 | if (startedRevision !== revisionRef.current) return; |
101 | | - if (savingRef.current) return; |
| 122 | + if (savingRef.current) { |
| 123 | + deferredActiveRefreshRef.current = true; |
| 124 | + return; |
| 125 | + } |
102 | 126 | applyActivePayload(value); |
103 | 127 | }, |
104 | 128 | rejectActiveRead: () => { |
@@ -154,7 +178,8 @@ export default function CodexPoolStrategySetting({ |
154 | 178 | } |
155 | 179 | savingRef.current = false; |
156 | 180 | setSaving(false); |
157 | | - }, [apiBase, stickyLimit, strategy, t]); |
| 181 | + scheduleDeferredActiveRefresh(); |
| 182 | + }, [apiBase, scheduleDeferredActiveRefresh, stickyLimit, strategy, t]); |
158 | 183 |
|
159 | 184 | // Block writes until /active confirms — defaults paint for CLS but must not overwrite server state. |
160 | 185 | const controlsDisabled = saving || loadError || !hydrated; |
|