From 85b8a38455b99b1d814ee6414097f2418a4e9b53 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Fri, 24 Jul 2026 17:23:35 -0700 Subject: [PATCH] fix: end the restart window when its restart has been seen through MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two more review findings, plus a self-correction. Stale wentAwayRef (Macroscope): the ref persists for the component's lifetime, so an outage from before the click satisfied the resolve predicate on the next render and re-enabled the button mid-update, allowing a second dispatch. Reset both refs when an attempt is armed so only outages during that attempt count. Unrelated outage shown as a restart (Bugbot): making the window write-once-and-expire last round traded one bug for another — any disconnect within 90s was dressed up as an intentional restart. The window now ends once the environment has gone away and come back, i.e. the restart it was armed for has been seen through. That is the narrow version of what an earlier revision got wrong by ending it on any healthy connection; the difference is requiring the disconnect first. The observation is used only for the banner. The update action's release path stays independent of it — coupling those is what produced the earlier family of bugs where each consumer invalidated the other's evidence. Also removed a test committed in the previous change that passed with and without its fix, so it verified nothing. Co-Authored-By: Claude Opus 5 (1M context) --- apps/web/src/components/ChatView.tsx | 35 +++++++++++++------ .../web/src/components/ServerUpdateAction.tsx | 5 +++ apps/web/src/serverRestartStore.test.ts | 16 +++++++++ apps/web/src/serverRestartStore.ts | 20 ++++++++--- 4 files changed, 60 insertions(+), 16 deletions(-) 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";