fix(webui): clear stale restart prompt after reconnect (#5813)
* fix(webui): clear stale restart prompt after reconnect * fix(webui): retry settings refresh after reconnect
This commit is contained in:
1 parent
dc996aba2d
commit
155a6ada02
3 files changed
+104
-6
No files matched your search
+31
-4
@@ -1173,6 +1173,7 @@ function Shell({
|
|||||||
const skills = useSkills(getToken);
|
const skills = useSkills(getToken);
|
||||||
const pageVisible = usePageVisibility();
|
const pageVisible = usePageVisibility();
|
||||||
const [settingsSnapshot, setSettingsSnapshot] = useState<SettingsPayload | null>(null);
|
const [settingsSnapshot, setSettingsSnapshot] = useState<SettingsPayload | null>(null);
|
||||||
|
const settingsRefreshGenerationRef = useRef(0);
|
||||||
const [pendingAutomationMessage, setPendingAutomationMessage] = useState<{
|
const [pendingAutomationMessage, setPendingAutomationMessage] = useState<{
|
||||||
id: string;
|
id: string;
|
||||||
chatId: string;
|
chatId: string;
|
||||||
@@ -1290,12 +1291,17 @@ function Shell({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
|
const requestGeneration = settingsRefreshGenerationRef.current;
|
||||||
fetchSettings(getToken())
|
fetchSettings(getToken())
|
||||||
.then((payload) => {
|
.then((payload) => {
|
||||||
if (!cancelled) setSettingsSnapshot(payload);
|
if (!cancelled && requestGeneration === settingsRefreshGenerationRef.current) {
|
||||||
|
setSettingsSnapshot(payload);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
if (!cancelled) setSettingsSnapshot(null);
|
if (!cancelled && requestGeneration === settingsRefreshGenerationRef.current) {
|
||||||
|
setSettingsSnapshot(null);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
@@ -2199,7 +2205,21 @@ function Shell({
|
|||||||
}, [client, navigate]);
|
}, [client, navigate]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return client.onStatus((status) => {
|
let cancelled = false;
|
||||||
|
let retryTimer: number | undefined;
|
||||||
|
const refreshSettings = (generation: number, attempt = 0): void => {
|
||||||
|
void fetchSettings(getToken())
|
||||||
|
.then((payload) => {
|
||||||
|
if (!cancelled && generation === settingsRefreshGenerationRef.current) {
|
||||||
|
setSettingsSnapshot(payload);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (cancelled || generation !== settingsRefreshGenerationRef.current || attempt >= 3) return;
|
||||||
|
retryTimer = window.setTimeout(() => refreshSettings(generation, attempt + 1), 250);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const unsubscribe = client.onStatus((status) => {
|
||||||
const startedAt = (() => {
|
const startedAt = (() => {
|
||||||
try {
|
try {
|
||||||
return Number(window.localStorage.getItem(RESTART_STARTED_KEY) ?? "0");
|
return Number(window.localStorage.getItem(RESTART_STARTED_KEY) ?? "0");
|
||||||
@@ -2220,11 +2240,18 @@ function Shell({
|
|||||||
} catch {
|
} catch {
|
||||||
// ignore storage errors
|
// ignore storage errors
|
||||||
}
|
}
|
||||||
|
const refreshGeneration = ++settingsRefreshGenerationRef.current;
|
||||||
setIsRestarting(false);
|
setIsRestarting(false);
|
||||||
setRestartToast(t("app.restart.completed", { seconds: (elapsedMs / 1000).toFixed(1) }));
|
setRestartToast(t("app.restart.completed", { seconds: (elapsedMs / 1000).toFixed(1) }));
|
||||||
window.setTimeout(() => setRestartToast(null), 3_500);
|
window.setTimeout(() => setRestartToast(null), 3_500);
|
||||||
|
refreshSettings(refreshGeneration);
|
||||||
});
|
});
|
||||||
}, [client, t]);
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
unsubscribe();
|
||||||
|
if (retryTimer !== undefined) window.clearTimeout(retryTimer);
|
||||||
|
};
|
||||||
|
}, [client, getToken, t]);
|
||||||
|
|
||||||
const onTurnEnd = useDeferredTitleRefresh(
|
const onTurnEnd = useDeferredTitleRefresh(
|
||||||
temporaryChatActive ? null : activePaneSession,
|
temporaryChatActive ? null : activePaneSession,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import { imageGenerationFormFromPayload } from "@/components/settings/capabilities/ImageGenerationSettings";
|
import { imageGenerationFormFromPayload } from "@/components/settings/capabilities/ImageGenerationSettings";
|
||||||
@@ -90,6 +90,10 @@ export function useSettingsController({
|
|||||||
const [pendingRestartSections, setPendingRestartSections] = useState<PendingRestartSections>(
|
const [pendingRestartSections, setPendingRestartSections] = useState<PendingRestartSections>(
|
||||||
EMPTY_PENDING_RESTART_SECTIONS,
|
EMPTY_PENDING_RESTART_SECTIONS,
|
||||||
);
|
);
|
||||||
|
const previousInitialSettingsRef = useRef(initialSettings);
|
||||||
|
const latestInitialSettingsRef = useRef(initialSettings);
|
||||||
|
latestInitialSettingsRef.current = initialSettings;
|
||||||
|
const restartRefreshRef = useRef<SettingsPayload | null>(null);
|
||||||
const [localPrefs, setLocalPrefs] = useState<LocalPreferences>(() => readLocalPreferences());
|
const [localPrefs, setLocalPrefs] = useState<LocalPreferences>(() => readLocalPreferences());
|
||||||
const modelState = useModelSettingsState(initialSettings);
|
const modelState = useModelSettingsState(initialSettings);
|
||||||
const {
|
const {
|
||||||
@@ -199,13 +203,30 @@ export function useSettingsController({
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, [applyPayload, initialSettings, settings]);
|
}, [applyPayload, initialSettings, settings]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const previous = previousInitialSettingsRef.current;
|
||||||
|
previousInitialSettingsRef.current = initialSettings;
|
||||||
|
if (previous?.requires_restart && !initialSettings?.requires_restart) {
|
||||||
|
restartRefreshRef.current = initialSettings;
|
||||||
|
} else if (restartRefreshRef.current !== initialSettings) {
|
||||||
|
restartRefreshRef.current = null;
|
||||||
|
}
|
||||||
|
const shouldApplyRestartedSettings =
|
||||||
|
restartRefreshRef.current === initialSettings && settings?.requires_restart === true;
|
||||||
|
if (!shouldApplyRestartedSettings || !initialSettings) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
applyPayload(initialSettings);
|
||||||
|
}, [applyPayload, initialSettings, settings]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const showLoading = settings === null;
|
const showLoading = settings === null;
|
||||||
|
const requestInitialSettings = latestInitialSettingsRef.current;
|
||||||
if (showLoading) setLoading(true);
|
if (showLoading) setLoading(true);
|
||||||
fetchSettings(getToken())
|
fetchSettings(getToken())
|
||||||
.then((payload) => {
|
.then((payload) => {
|
||||||
if (!cancelled) {
|
if (!cancelled && latestInitialSettingsRef.current === requestInitialSettings) {
|
||||||
applyPayload(payload);
|
applyPayload(payload);
|
||||||
setError(null);
|
setError(null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1058,6 +1058,56 @@ describe("App layout", () => {
|
|||||||
expect(window.location.hash).toBe("#/settings?section=channels");
|
expect(window.location.hash).toBe("#/settings?section=channels");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("refreshes settings after the browser reconnects from a restart", async () => {
|
||||||
|
let restartCompleted = false;
|
||||||
|
let refreshedSettingsRequests = 0;
|
||||||
|
let releaseRefreshedSettings!: () => void;
|
||||||
|
const refreshedSettingsReady = new Promise<void>((resolve) => {
|
||||||
|
releaseRefreshedSettings = resolve;
|
||||||
|
});
|
||||||
|
const pendingSettings = {
|
||||||
|
...baseSettingsPayload(),
|
||||||
|
requires_restart: true,
|
||||||
|
restart_required_sections: ["runtime"],
|
||||||
|
};
|
||||||
|
const refreshedSettings = {
|
||||||
|
...baseSettingsPayload(),
|
||||||
|
requires_restart: false,
|
||||||
|
restart_required_sections: [],
|
||||||
|
};
|
||||||
|
localStorage.setItem("nanobot-webui.restartStartedAt", String(Date.now() - 2_000));
|
||||||
|
window.history.replaceState(null, "", "/#/settings?section=runtime");
|
||||||
|
mockFetchRoutes({
|
||||||
|
"/api/settings": () => {
|
||||||
|
if (!restartCompleted) return pendingSettings;
|
||||||
|
refreshedSettingsRequests += 1;
|
||||||
|
if (refreshedSettingsRequests === 1) throw new Error("gateway is still starting");
|
||||||
|
return refreshedSettingsReady.then(() => refreshedSettings);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(<App />);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getAllByText("Saved. Restart to apply changes.").length).toBeGreaterThan(0);
|
||||||
|
}, { timeout: 10_000 });
|
||||||
|
|
||||||
|
restartCompleted = true;
|
||||||
|
act(() => {
|
||||||
|
for (const handler of statusHandlers) handler("reconnecting");
|
||||||
|
for (const handler of statusHandlers) handler("open");
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => expect(refreshedSettingsRequests).toBeGreaterThan(1));
|
||||||
|
await act(async () => {
|
||||||
|
releaseRefreshedSettings();
|
||||||
|
await refreshedSettingsReady;
|
||||||
|
});
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: "Back to chat" }));
|
||||||
|
await waitFor(() => expect(window.location.hash).toBe("#/new"));
|
||||||
|
expect(await screen.findByText(HERO_GREETING_PATTERN, {}, { timeout: 10_000 })).toBeInTheDocument();
|
||||||
|
}, 30_000);
|
||||||
|
|
||||||
it("opens Skills from the main sidebar", async () => {
|
it("opens Skills from the main sidebar", async () => {
|
||||||
const longSkillDescription = [
|
const longSkillDescription = [
|
||||||
"Work with GitHub repositories, issues, pull requests, releases, workflows,",
|
"Work with GitHub repositories, issues, pull requests, releases, workflows,",
|
||||||
|
|||||||
Reference in new issue
Block a user