Skip to content

Commit c189c94

Browse files
committed
fix(web): update machines together in auto balance
1 parent 3bf74eb commit c189c94

4 files changed

Lines changed: 474 additions & 60 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ import {
437437
} from "./ui/alert-dialog";
438438
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
439439
import { ServerUpdateAction } from "./ServerUpdateAction";
440+
import { useAutoBalanceUpdateBanner } from "./chat/useAutoBalanceUpdateBanner";
440441
import {
441442
ComposerServerUpdateIcon,
442443
ComposerServerUpdateStatus,
@@ -2314,6 +2315,35 @@ export default function ChatView(props: ChatViewProps) {
23142315
advertisedFileAttachmentBytes === null
23152316
? null
23162317
: clampFileAttachmentUploadBytes(advertisedFileAttachmentBytes);
2318+
const envLocked = Boolean(
2319+
activeThread &&
2320+
(activeThread.messages.length > 0 ||
2321+
(activeThread.session !== null && activeThread.session.status !== "stopped")),
2322+
);
2323+
2324+
const loadBalancingSettings = useClientSettings();
2325+
const automaticEnvironment = Boolean(
2326+
clientSettingsHydrated &&
2327+
draftId &&
2328+
!envLocked &&
2329+
hasMultipleEnvironments &&
2330+
loadBalancingSettings.loadBalancingEnabled &&
2331+
draftThread?.environmentSelection !== "manual" &&
2332+
(!composerHasAttachments || Boolean(draftThread?.loadBalancedEnvironmentId)) &&
2333+
(!draftThread?.branch || draftThread.environmentSelection === "auto") &&
2334+
!draftThread?.worktreePath,
2335+
);
2336+
const autoUpdateEnvironments = useMemo(
2337+
() =>
2338+
automaticEnvironment
2339+
? logicalProjectEnvironments.flatMap(({ environmentId }) => {
2340+
const environment = environmentById.get(environmentId);
2341+
return environment ? [environment] : [];
2342+
})
2343+
: [],
2344+
[automaticEnvironment, logicalProjectEnvironments, environmentById],
2345+
);
2346+
const autoBalanceUpdateBanner = useAutoBalanceUpdateBanner(autoUpdateEnvironments);
23172347
const versionMismatch = resolveServerConfigVersionMismatch(serverConfig);
23182348
const versionMismatchDismissKey =
23192349
versionMismatch && activeThread
@@ -2417,6 +2447,7 @@ export default function ChatView(props: ChatViewProps) {
24172447
}
24182448
}
24192449
if (
2450+
!automaticEnvironment &&
24202451
serverUpdateEnvironmentId &&
24212452
!reconnectingThroughVersionSkew &&
24222453
(serverUpdateState.status === "idle"
@@ -2495,8 +2526,11 @@ export default function ChatView(props: ChatViewProps) {
24952526
}),
24962527
});
24972528
}
2529+
if (autoBalanceUpdateBanner) items.push(autoBalanceUpdateBanner);
24982530
return items;
24992531
}, [
2532+
automaticEnvironment,
2533+
autoBalanceUpdateBanner,
25002534
activeEnvironmentUnavailableState,
25012535
reconnectWarningGraceElapsed,
25022536
handleReconnectActiveEnvironment,
@@ -3230,24 +3264,6 @@ export default function ChatView(props: ChatViewProps) {
32303264
}
32313265
}, [activeThreadRef, diffOpen, isServerThread, onDiffPanelOpen]);
32323266

3233-
const envLocked = Boolean(
3234-
activeThread &&
3235-
(activeThread.messages.length > 0 ||
3236-
(activeThread.session !== null && activeThread.session.status !== "stopped")),
3237-
);
3238-
3239-
const loadBalancingSettings = useClientSettings();
3240-
const automaticEnvironment = Boolean(
3241-
clientSettingsHydrated &&
3242-
draftId &&
3243-
!envLocked &&
3244-
hasMultipleEnvironments &&
3245-
loadBalancingSettings.loadBalancingEnabled &&
3246-
draftThread?.environmentSelection !== "manual" &&
3247-
(!composerHasAttachments || Boolean(draftThread?.loadBalancedEnvironmentId)) &&
3248-
(!draftThread?.branch || draftThread.environmentSelection === "auto") &&
3249-
!draftThread?.worktreePath,
3250-
);
32513267
const needsLoadBalancing = automaticEnvironment && !draftThread?.loadBalancedEnvironmentId;
32523268
const loadBalancingCandidates = useMemo(
32533269
() =>

‎apps/web/src/components/ServerUpdateAction.test.tsx‎

Lines changed: 158 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { ReactElement } from "react";
1+
import { act, type ReactElement } from "react";
2+
import { create, type ReactTestRenderer } from "react-test-renderer";
23
import { renderToStaticMarkup } from "react-dom/server";
34
import type { EnvironmentId } from "@t3tools/contracts";
45
import * as Cause from "effect/Cause";
56
import { AsyncResult } from "effect/unstable/reactivity";
6-
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
7+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
78

89
const testState = vi.hoisted(() => ({
910
updateServer: vi.fn(),
@@ -30,7 +31,18 @@ vi.mock("./ui/toast", () => ({
3031
toastManager: { add: testState.toast },
3132
}));
3233

33-
import { ServerUpdateAction, ServerUpdateProgress } from "./ServerUpdateAction";
34+
import {
35+
readConfirmDialogState,
36+
registerConfirmDialogHost,
37+
resetConfirmDialogForTests,
38+
respondToConfirmDialog,
39+
} from "~/confirmDialog";
40+
import {
41+
ServerUpdateAction,
42+
ServerUpdateProgress,
43+
ServerUpdatesAction,
44+
type ServerUpdateTarget,
45+
} from "./ServerUpdateAction";
3446

3547
type ActionElement = ReactElement<{
3648
readonly onClick?: () => void;
@@ -194,6 +206,149 @@ describe("ServerUpdateAction", () => {
194206
});
195207
});
196208

209+
describe("ServerUpdatesAction", () => {
210+
let renderer: ReactTestRenderer | undefined;
211+
const targets: ReadonlyArray<ServerUpdateTarget> = [
212+
{
213+
environmentId: "batch-a" as EnvironmentId,
214+
serverLabel: "Laptop",
215+
selfUpdate: "boot-service",
216+
targetVersion: "0.0.31",
217+
threadContinuation: true,
218+
continueThreadsAfterServerUpdate: true,
219+
},
220+
{
221+
environmentId: "batch-b" as EnvironmentId,
222+
serverLabel: "Office",
223+
selfUpdate: "respawn",
224+
targetVersion: "0.0.31",
225+
threadContinuation: true,
226+
continueThreadsAfterServerUpdate: false,
227+
},
228+
{
229+
environmentId: "batch-c" as EnvironmentId,
230+
serverLabel: "Manual",
231+
selfUpdate: null,
232+
targetVersion: "0.0.31",
233+
},
234+
];
235+
const success = AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const });
236+
237+
async function mount(batch = targets) {
238+
await act(async () => {
239+
renderer = create(<ServerUpdatesAction targets={batch} />);
240+
});
241+
return renderer!.root.findByType("button");
242+
}
243+
244+
beforeEach(() => {
245+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
246+
testState.updateServer.mockReset();
247+
testState.toast.mockReset();
248+
resetConfirmDialogForTests();
249+
});
250+
afterEach(async () => {
251+
await act(async () => {
252+
renderer?.unmount();
253+
});
254+
renderer = undefined;
255+
resetConfirmDialogForTests();
256+
vi.unstubAllGlobals();
257+
});
258+
259+
it("updates both supported machines with their own continuation preference and skips the manual machine", async () => {
260+
testState.updateServer.mockResolvedValue(success);
261+
const button = await mount();
262+
await act(async () => {
263+
button.props.onClick();
264+
});
265+
266+
expect(testState.updateServer.mock.calls.map(([target]) => target)).toEqual([
267+
{
268+
environmentId: "batch-a",
269+
input: { targetVersion: "0.0.31", continueRunningThreads: true },
270+
},
271+
{ environmentId: "batch-b", input: { targetVersion: "0.0.31" } },
272+
]);
273+
expect(testState.toast.mock.calls.map(([toast]) => toast.title)).toEqual([
274+
"Laptop updated",
275+
"Office updated",
276+
]);
277+
});
278+
279+
it("names a failed machine while letting the other machine complete", async () => {
280+
testState.updateServer
281+
.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("Download failed"))))
282+
.mockResolvedValueOnce(success);
283+
const button = await mount();
284+
await act(async () => {
285+
button.props.onClick();
286+
});
287+
288+
expect(testState.updateServer).toHaveBeenCalledTimes(2);
289+
expect(testState.toast).toHaveBeenCalledWith({
290+
type: "error",
291+
title: "Laptop update failed",
292+
description: "Download failed",
293+
});
294+
expect(testState.toast).toHaveBeenCalledWith(
295+
expect.objectContaining({ type: "success", title: "Office updated" }),
296+
);
297+
expect(button.props.disabled).toBe(false);
298+
});
299+
300+
it("starts each machine once when double-clicked and disables the action until both finish", async () => {
301+
const completions: Array<() => void> = [];
302+
testState.updateServer.mockImplementation(
303+
() =>
304+
new Promise((resolve) => {
305+
completions.push(() => resolve(success));
306+
}),
307+
);
308+
const button = await mount();
309+
await act(async () => {
310+
button.props.onClick();
311+
button.props.onClick();
312+
});
313+
expect(testState.updateServer).toHaveBeenCalledTimes(2);
314+
expect(button.props.disabled).toBe(true);
315+
await act(async () => {
316+
completions[0]!();
317+
});
318+
expect(button.props.disabled).toBe(true);
319+
await act(async () => {
320+
completions[1]!();
321+
});
322+
expect(button.props.disabled).toBe(false);
323+
expect(testState.toast).toHaveBeenCalledTimes(2);
324+
});
325+
326+
it("asks once for desktop machines and cancels the entire batch", async () => {
327+
registerConfirmDialogHost();
328+
const button = await mount(
329+
targets.map((target, index) =>
330+
index < 2 ? { ...target, selfUpdate: "desktop-managed", desktopAppUpdate: true } : target,
331+
),
332+
);
333+
await act(async () => {
334+
button.props.onClick();
335+
});
336+
const confirmation = readConfirmDialogState();
337+
expect(confirmation).toEqual(
338+
expect.objectContaining({
339+
status: "confirming",
340+
message: expect.stringContaining("Laptop, Office"),
341+
}),
342+
);
343+
expect(testState.updateServer).not.toHaveBeenCalled();
344+
await act(async () => {
345+
respondToConfirmDialog(false);
346+
});
347+
expect(testState.updateServer).not.toHaveBeenCalled();
348+
expect(button.props.disabled).toBe(false);
349+
});
350+
});
351+
197352
describe("ServerUpdateProgress", () => {
198353
it("shows one calm status row for the restart wait", () => {
199354
const markup = renderToStaticMarkup(

0 commit comments

Comments
 (0)