Files
t3code/apps/web/src/serverRestartStore.ts
T
Theo BrowneandClaude Opus 5 02abc2cb3d fix: make the restart window survive until the restart is observed
An independent gpt-5.6-sol review found that the restart overlay never
actually engaged in the normal flow — the feature was inert.

ChatView cleared the expectation on any render where the connection was
"connected". But the window is armed while the old server is still
connected (that is the point: the acknowledgement races its own
disconnect), so the store update triggered a render that destroyed the
window within milliseconds, long before the server went away. Both
re-arm points had the same problem. The user therefore still got the
outage banner and escalating backoff.

The window now tracks whether the restart's disconnect was actually
observed. A connected environment only ends the window once that has
happened; before it, "connected" just means the restart has not started
yet. Re-arming preserves an already-observed disconnect.

Also fixes the follow-on the same review found: because a rejected
restart can no longer be reported through the acknowledged RPC, the
update action could stay disabled for the full 12-minute safety window
with the mismatch unresolved. A settled restart window (disconnect seen,
window closed) now releases it so the user can retry immediately.

The restarting/settled decisions are pure exported predicates, since the
component test harness stubs useEffect and cannot exercise
effect-driven behavior. Both are tested, and both tests were confirmed
to fail against the old logic.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-24 16:59:17 -07:00

155 lines
5.7 KiB
TypeScript

/**
* Environment-scoped "a server restart is expected" overlay.
*
* Set around a server self-update so the connection UI can tell an
* intentional restart apart from a genuine outage: while the window is
* active, disconnects present as a calm "restarting" notice and the client
* retries on a fast flat cadence instead of escalating backoff. This is
* deliberately not a connection-supervisor phase — the supervisor state
* machine stays transport-only, and this store is UI-side intent that
* expires on its own if the server never comes back.
*/
import type { EnvironmentId } from "@t3tools/contracts";
import { useEffect, useReducer } from "react";
import { create } from "zustand";
/**
* How long after the last update signal (dispatch, RPC acknowledgement, or
* interrupted RPC) a disconnect is still attributed to the restart. Covers
* the respawn shim (~3s), boot-service systemd swap, server boot, and relay
* re-registration with generous margin; after it lapses the normal error
* presentation returns.
*/
export const SERVER_RESTART_EXPECTED_WINDOW_MS = 90_000;
interface ServerRestartWindow {
readonly expiresAt: number;
/**
* Whether the environment has actually gone away since the window was
* armed. The window is armed while the old server is still connected — that
* is the point, since the acknowledgement races its own disconnect — so a
* connected environment must not end the window until the restart it is
* waiting for has been observed.
*/
readonly sawDisconnect: boolean;
}
interface ServerRestartState {
readonly windowByEnvironmentId: Readonly<Record<string, ServerRestartWindow>>;
readonly expect: (environmentId: EnvironmentId) => void;
readonly markDisconnected: (environmentId: EnvironmentId) => void;
readonly clear: (environmentId: EnvironmentId) => void;
}
export const useServerRestartStore = create<ServerRestartState>()((set) => ({
windowByEnvironmentId: {},
expect: (environmentId) =>
set((state) => ({
windowByEnvironmentId: {
...state.windowByEnvironmentId,
[environmentId]: {
expiresAt: Date.now() + SERVER_RESTART_EXPECTED_WINDOW_MS,
// Re-arming (a slow install acknowledging late) must not forget a
// disconnect that was already observed.
sawDisconnect: state.windowByEnvironmentId[environmentId]?.sawDisconnect ?? false,
},
},
})),
markDisconnected: (environmentId) =>
set((state) => {
const current = state.windowByEnvironmentId[environmentId];
if (current === undefined || current.sawDisconnect) {
return state;
}
return {
windowByEnvironmentId: {
...state.windowByEnvironmentId,
[environmentId]: { ...current, sawDisconnect: true },
},
};
}),
clear: (environmentId) =>
set((state) => {
if (!(environmentId in state.windowByEnvironmentId)) {
return state;
}
const next = { ...state.windowByEnvironmentId };
delete next[environmentId];
return { windowByEnvironmentId: next };
}),
}));
export function expectServerRestart(environmentId: EnvironmentId): void {
useServerRestartStore.getState().expect(environmentId);
}
/** Records that the expected restart's disconnect has now been observed. */
export function markServerRestartDisconnected(environmentId: EnvironmentId): void {
useServerRestartStore.getState().markDisconnected(environmentId);
}
export function clearExpectedServerRestart(environmentId: EnvironmentId): void {
useServerRestartStore.getState().clear(environmentId);
}
export interface ServerRestartExpectation {
/** An unexpired restart window is armed for this environment. */
readonly expected: boolean;
/** The restart's disconnect has been observed, so a reconnect ends it. */
readonly sawDisconnect: boolean;
}
/**
* Whether an armed restart window has run its course: the disconnect was
* observed and the window has since closed. Callers use this to recover UI
* that was held for the restart — the update RPC is acknowledged before the
* restart runs, so a rejected restart cannot report itself through the call.
*
* Note that a window which never saw a disconnect is not "settled": the
* restart simply never happened, and other paths (an explicit RPC failure)
* own that case.
*/
export function isServerRestartSettled(expectation: ServerRestartExpectation): boolean {
return expectation.sawDisconnect && !expectation.expected;
}
/**
* Whether the environment should currently be presented as restarting rather
* than as an outage: a live window plus an environment that is actually away.
*/
export function isServerRestarting(input: {
readonly expectation: ServerRestartExpectation;
readonly environmentUnavailable: boolean;
}): boolean {
return input.expectation.expected && input.environmentUnavailable;
}
/**
* The restart window state for an environment. Re-renders when the window is
* armed, observes its disconnect, is cleared, or lapses.
*/
export function useServerRestartExpectation(
environmentId: EnvironmentId | null,
): ServerRestartExpectation {
const window = useServerRestartStore((state) =>
environmentId === null ? null : (state.windowByEnvironmentId[environmentId] ?? null),
);
const expiresAt = window?.expiresAt ?? null;
const [, tick] = useReducer((count: number) => count + 1, 0);
useEffect(() => {
if (expiresAt === null) {
return;
}
const remaining = expiresAt - Date.now();
if (remaining <= 0) {
return;
}
const timer = setTimeout(tick, remaining + 50);
return () => clearTimeout(timer);
}, [expiresAt]);
return {
expected: expiresAt !== null && expiresAt > Date.now(),
sawDisconnect: window?.sawDisconnect === true,
};
}