From cfb0f51e781396c6173faa042bede466cc3ea842 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Mon, 12 Oct 2026 11:36:42 +0800 Subject: [PATCH] fix(webui): complete restarts independently in each tab --- docs/troubleshooting.md | 4 ++++ webui/src/App.tsx | 9 +++++++-- webui/src/tests/app-layout.test.tsx | 25 ++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index f49e36f39..8c6ab164d 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -42,6 +42,10 @@ current chat. Update the gateway to stop creating these topics. Existing topics are not deleted by the update; check their contents before removing them. A typed `/restart` command remains part of that chat's display history. +If settings stay disabled after a restart while another tab has reconnected, +reload the affected page. Updated WebUI clients track restart completion per tab, +so one tab cannot leave another tab waiting by clearing the shared restart marker. + ## Session storage overlaps the workspace Sessions live at `/sessions//`, outside the agent workspace. If startup reports a conflict, compare the printed paths. A valid layout is `bot/config.json`, `bot/sessions/`, and `bot/workspace/`. diff --git a/webui/src/App.tsx b/webui/src/App.tsx index a881d1f3a..574fe879e 100644 --- a/webui/src/App.tsx +++ b/webui/src/App.tsx @@ -1220,6 +1220,7 @@ function Shell({ label: string; } | null>(null); const restartSawDisconnectRef = useRef(false); + const restartStartedAtRef = useRef(0); const [restartToast, setRestartToast] = useState(null); const [isRestarting, setIsRestarting] = useState(false); const [pairingRequests, setPairingRequests] = useState([]); @@ -2220,10 +2221,11 @@ function Shell({ const chatId = activeSession?.chatId ?? client.defaultChatId; if (!chatId) return; restartSawDisconnectRef.current = false; + restartStartedAtRef.current = Date.now(); setIsRestarting(true); rememberRestartRoute(); try { - window.localStorage.setItem(RESTART_STARTED_KEY, String(Date.now())); + window.localStorage.setItem(RESTART_STARTED_KEY, String(restartStartedAtRef.current)); } catch { // ignore storage errors } @@ -2309,7 +2311,8 @@ function Shell({ }); }; const unsubscribe = client.onStatus((status) => { - const startedAt = (() => { + // Another tab can consume the reload marker before this tab reconnects. + const startedAt = restartStartedAtRef.current || (() => { try { return Number(window.localStorage.getItem(RESTART_STARTED_KEY) ?? "0"); } catch { @@ -2318,6 +2321,7 @@ function Shell({ })(); if (!startedAt) return; if (status !== "open") { + restartStartedAtRef.current = startedAt; restartSawDisconnectRef.current = true; return; } @@ -2330,6 +2334,7 @@ function Shell({ // ignore storage errors } const refreshGeneration = ++settingsRefreshGenerationRef.current; + restartStartedAtRef.current = 0; setIsRestarting(false); setRestartToast(t("app.restart.completed", { seconds: (elapsedMs / 1000).toFixed(1) })); window.setTimeout(() => setRestartToast(null), 3_500); diff --git a/webui/src/tests/app-layout.test.tsx b/webui/src/tests/app-layout.test.tsx index 451e58b3d..d93833d36 100644 --- a/webui/src/tests/app-layout.test.tsx +++ b/webui/src/tests/app-layout.test.tsx @@ -27,6 +27,8 @@ const requestMutationSpy = vi.fn(); const discardTemporaryChatSpy = vi.fn(); const newTemporaryChatSpy = vi.fn<() => Promise>(); const sendMessageSpy = vi.fn(); +const sendSystemCommandSpy = vi.fn().mockResolvedValue(undefined); +let mockDefaultChatId: string | null = null; const statusHandlers = new Set<(status: ConnectionStatus) => void>(); const runStatusHandlers = new Set<(chatId: string, startedAt: number | null) => void>(); const sessionUpdateHandlers = new Set<( @@ -242,7 +244,8 @@ vi.mock("@/lib/nanobot-client", async (importOriginal) => { const actual = await importOriginal(); class MockClient { status = "idle" as const; - defaultChatId: string | null = null; + defaultChatId = mockDefaultChatId; + sendSystemCommand = sendSystemCommandSpy; connect = connectSpy; onStatus = (handler: (status: ConnectionStatus) => void) => { statusHandlers.add(handler); @@ -317,6 +320,8 @@ describe("App layout", () => { `00000000-0000-4000-8000-${String(++temporaryChatCounter).padStart(12, "0")}` )); sendMessageSpy.mockReset(); + sendSystemCommandSpy.mockReset().mockResolvedValue(undefined); + mockDefaultChatId = null; statusHandlers.clear(); runStatusHandlers.clear(); sessionUpdateHandlers.clear(); @@ -1238,6 +1243,24 @@ describe("App layout", () => { expect(window.location.hash).toBe("#/settings?section=channels"); }); + it.each(["before disconnect", "after disconnect"])("finishes a requested restart when another tab clears its marker %s", async (clearWhen) => { + mockDefaultChatId = "restart-fixture"; + window.history.replaceState(null, "", "/#/settings?section=runtime"); + mockFetchRoutes({ "/api/settings": baseSettingsPayload() }); + render(); + fireEvent.click(await screen.findByRole("button", { name: "Restart", exact: true })); + expect(sendSystemCommandSpy).toHaveBeenCalledWith("restart-fixture", "/restart"); + expect(screen.getByRole("button", { name: "Restarting...", exact: true })).toBeDisabled(); + act(() => { + if (clearWhen === "before disconnect") localStorage.removeItem("nanobot-webui.restartStartedAt"); + for (const handler of statusHandlers) handler("reconnecting"); + localStorage.removeItem("nanobot-webui.restartStartedAt"); + for (const handler of statusHandlers) handler("open"); + }); + await waitFor(() => expect(screen.getByRole("button", { name: "Restart", exact: true })).toBeEnabled()); + expect(createChatSpy).not.toHaveBeenCalled(); + }); + it("refreshes settings after the browser reconnects from a restart", async () => { let restartCompleted = false; let refreshedSettingsRequests = 0;