Skip to content

Commit 15193df

Browse files
authored
fix(web): update machines together in auto balance (pingdotgg#10596)
1 parent b7c002f commit 15193df

4 files changed

Lines changed: 471 additions & 74 deletions

File tree

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ import {
440440
} from "./ui/alert-dialog";
441441
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
442442
import { ServerUpdateAction } from "./ServerUpdateAction";
443+
import { useAutoBalanceUpdateBanner } from "./chat/useAutoBalanceUpdateBanner";
443444
import {
444445
ComposerServerUpdateIcon,
445446
ComposerServerUpdateStatus,
@@ -2317,6 +2318,35 @@ export default function ChatView(props: ChatViewProps) {
23172318
advertisedFileAttachmentBytes === null
23182319
? null
23192320
: clampFileAttachmentUploadBytes(advertisedFileAttachmentBytes);
2321+
const envLocked = Boolean(
2322+
activeThread &&
2323+
(activeThread.messages.length > 0 ||
2324+
(activeThread.session !== null && activeThread.session.status !== "stopped")),
2325+
);
2326+
2327+
const loadBalancingSettings = useClientSettings();
2328+
const automaticEnvironment = Boolean(
2329+
clientSettingsHydrated &&
2330+
draftId &&
2331+
!envLocked &&
2332+
hasMultipleEnvironments &&
2333+
loadBalancingSettings.loadBalancingEnabled &&
2334+
draftThread?.environmentSelection !== "manual" &&
2335+
(!composerHasAttachments || Boolean(draftThread?.loadBalancedEnvironmentId)) &&
2336+
(!draftThread?.branch || draftThread.environmentSelection === "auto") &&
2337+
!draftThread?.worktreePath,
2338+
);
2339+
const autoUpdateEnvironments = useMemo(
2340+
() =>
2341+
automaticEnvironment
2342+
? logicalProjectEnvironments.flatMap(({ environmentId }) => {
2343+
const environment = environmentById.get(environmentId);
2344+
return environment ? [environment] : [];
2345+
})
2346+
: [],
2347+
[automaticEnvironment, logicalProjectEnvironments, environmentById],
2348+
);
2349+
const autoBalanceUpdateBanner = useAutoBalanceUpdateBanner(autoUpdateEnvironments);
23202350
const versionMismatch = resolveServerConfigVersionMismatch(serverConfig);
23212351
const versionMismatchDismissKey =
23222352
versionMismatch && activeThread
@@ -2420,6 +2450,7 @@ export default function ChatView(props: ChatViewProps) {
24202450
}
24212451
}
24222452
if (
2453+
!automaticEnvironment &&
24232454
serverUpdateEnvironmentId &&
24242455
!reconnectingThroughVersionSkew &&
24252456
(serverUpdateState.status === "idle"
@@ -2498,8 +2529,11 @@ export default function ChatView(props: ChatViewProps) {
24982529
}),
24992530
});
25002531
}
2532+
if (autoBalanceUpdateBanner) items.push(autoBalanceUpdateBanner);
25012533
return items;
25022534
}, [
2535+
automaticEnvironment,
2536+
autoBalanceUpdateBanner,
25032537
activeEnvironmentUnavailableState,
25042538
reconnectWarningGraceElapsed,
25052539
handleReconnectActiveEnvironment,
@@ -3233,24 +3267,6 @@ export default function ChatView(props: ChatViewProps) {
32333267
}
32343268
}, [activeThreadRef, diffOpen, isServerThread, onDiffPanelOpen]);
32353269

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

‎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)