diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index c1745d7d36..c75a35dbcc 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -25,7 +25,11 @@ import { connectionStatusTitle, type EnvironmentConnectionPresentation, } from "@t3tools/client-runtime/connection"; -import { isServerRestarting, useServerRestartExpected } from "~/serverRestartStore"; +import { + clearExpectedServerRestart, + isServerRestarting, + useServerRestartExpected, +} from "~/serverRestartStore"; import { effectiveSettled, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled"; import { parseScopedThreadKey, @@ -1631,19 +1635,28 @@ function ChatViewContent(props: ChatViewProps) { environmentUnavailable: activeEnvironmentUnavailable, }); // While the restart is expected, poke the supervisor on a flat cadence so a - // 16-second backoff sleep cannot outlast the server's return. The window is - // never cleared here: it is armed while the old server is still connected, - // so nothing observed on the connection distinguishes "before the restart" - // from "after it". Presentation already gates on the environment being - // away, which makes a lingering window inert once the replacement is back. + // 16-second backoff sleep cannot outlast the server's return. + // + // The window also has to end at the right moment. Ending it on any healthy + // connection is wrong — it is armed while the old server is still connected, + // so that clears it before the restart it exists to cover. Never ending it + // is also wrong — an unrelated outage later in the window would be dressed + // up as an intentional restart. So it ends once this environment has gone + // away and come back: the restart it was armed for has been seen through. + const restartSawDisconnectRef = useRef(false); useEffect(() => { - if ( - activeEnvironmentId === null || - !activeEnvironmentRestartExpected || - activeEnvironmentConnectionPhase === "connected" - ) { + if (activeEnvironmentId === null || !activeEnvironmentRestartExpected) { + restartSawDisconnectRef.current = false; return; } + if (activeEnvironmentConnectionPhase === "connected") { + if (restartSawDisconnectRef.current) { + restartSawDisconnectRef.current = false; + clearExpectedServerRestart(activeEnvironmentId); + } + return; + } + restartSawDisconnectRef.current = true; // Only wake the supervisor out of a backoff sleep; while an attempt is // live, retryNow would interrupt and restart it. if (activeEnvironment?.connection.retryAt == null) { diff --git a/apps/web/src/components/ServerUpdateAction.tsx b/apps/web/src/components/ServerUpdateAction.tsx index e8a8bbfb4a..e10bfaab64 100644 --- a/apps/web/src/components/ServerUpdateAction.tsx +++ b/apps/web/src/components/ServerUpdateAction.tsx @@ -161,6 +161,11 @@ export function ServerUpdateAction({ return; } inFlightRef.current = true; + // Only outages observed during this attempt may resolve it. An earlier + // unrelated blip would otherwise satisfy the resolve predicate on the + // very next render and re-enable the button mid-update. + wentAwayRef.current = false; + sawWindowRef.current = false; const attempt = attemptRef.current + 1; attemptRef.current = attempt; const ownsAttempt = () => attemptRef.current === attempt; diff --git a/apps/web/src/serverRestartStore.test.ts b/apps/web/src/serverRestartStore.test.ts index 36ab446c06..53ed4b2fe0 100644 --- a/apps/web/src/serverRestartStore.test.ts +++ b/apps/web/src/serverRestartStore.test.ts @@ -75,6 +75,22 @@ describe("serverRestartStore", () => { false, ); }); + + it("does not dress up an unrelated outage as a restart once cleared", () => { + // The consumer clears the window after seeing the restart through, so a + // later ordinary outage inside the original 90s must read as an outage. + expectServerRestart(ENVIRONMENT_ID); + clearExpectedServerRestart(ENVIRONMENT_ID); + vi.advanceTimersByTime(10_000); + + expect(expiresAtFor(ENVIRONMENT_ID)).toBeNull(); + expect( + isServerRestarting({ + restartExpected: expiresAtFor(ENVIRONMENT_ID) !== null, + environmentUnavailable: true, + }), + ).toBe(false); + }); }); describe("isExpectedRestartResolved", () => { diff --git a/apps/web/src/serverRestartStore.ts b/apps/web/src/serverRestartStore.ts index 2db7cf62ab..4c294dc6ae 100644 --- a/apps/web/src/serverRestartStore.ts +++ b/apps/web/src/serverRestartStore.ts @@ -9,11 +9,21 @@ * machine stays transport-only, and this store is UI-side intent that * expires on its own if the server never comes back. * - * The window is intentionally write-once-and-expire: it is armed while the - * old server is still connected (the acknowledgement races its own - * disconnect), and nothing observed on the connection can reliably end it - * early. Presentation gates on the environment actually being unavailable, - * so a lingering window is inert once the replacement is back. + * Ending the window is the subtle part. It is armed while the old server is + * still connected (the acknowledgement races its own disconnect), so a + * connected environment cannot by itself mean the restart is over — that is + * how an earlier revision made the whole feature inert. But never ending it + * is also wrong: an unrelated outage later in the window would be dressed up + * as an intentional restart. So the window ends when the environment has + * gone away and come back, i.e. the restart it was armed for has been seen. + * + * A blip during a long install can consume the window early; the + * acknowledgement re-arms it, which is what covers that case. + * + * Note this observation is only used for presentation. The update action's + * own release path deliberately does not depend on it — coupling the two is + * what produced a family of bugs where each consumer invalidated the other's + * evidence. */ import type { EnvironmentId } from "@t3tools/contracts"; import { useEffect, useReducer } from "react";