feat(web): automatically update idle servers #7503
Closedt3-code-bot[bot](GitHub profile, opens in a new tab)wants to merge 9 commits into
main fromfeat/automatic-server-updatesopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/settings/ConnectionsSettings.tsx line 2325OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2245,3 +2321,5 @@22452321 savedBackendSshUsername,22462322 ]);224723232324+ const handleEnableAutomaticUpdates = async () => {2325+ const connectedEnvironments = environments.filter(packages/client-runtime/src/state/server.ts lines 255 to 258OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -191,3 +248,11 @@191248 };192249 }193250251+export function serverUpdateStateForAutomaticDecision(252+ current: ServerUpdateState,253+ pending: Extract<ServerUpdateState, { status: "pending" }> | null,254+): ServerUpdateState {255+ if (current.status === "running" || current.status === "failed") {256+ return current;257+ }258+ return pending ?? IDLE_SERVER_UPDATE_STATE;apps/server/src/cloud/selfUpdate.ts lines 122 to 124OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -79,6 +105,20 @@79105 if (!isExactServiceVersion(targetVersion)) {80106 return yield* failWith(`'${targetVersion}' is not an exact t3 version.`);81107 }108+ if (109+ input.automatic === true &&110+ compareSemverVersions(targetVersion, packageJson.version) <= 0111+ ) {112+ return yield* failWith("Automatic updates must move the server to a newer version.");113+ }114+ if (input.automatic === true && (yield* Ref.get(lastFailedTarget)) === targetVersion) {115+ return yield* failWith(116+ "An automatic update to this version already failed. Retry it manually before automatic updates resume.",117+ );118+ }82119 if (yield* Ref.getAndSet(inFlight, true)) {83120 return yield* failWith("A server update is already in progress.");84121 }122+ if (input.automatic !== true) {123+ yield* Ref.set(lastFailedTarget, null);124+ }
- apps/server/src/cloud/selfUpdate.ts lines 47 to 47OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -41,3 +44,4 @@4144 readonly update: (4245 input: ServerSelfUpdateInput,4346 reportProgress?: (stage: ServerSelfUpdateProgressStage) => Effect.Effect<void>,47+ confirmAutomaticUpdateIdle?: () => Effect.Effect<void, ServerSelfUpdateError>,apps/server/src/orchestration/http.ts lines 21 to 21OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -18,3 +18,4 @@1818 } from "../auth/http.ts";1919 import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";2020 import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";21+import { ServerSelfUpdate } from "../cloud/selfUpdate.ts";
- apps/web/src/components/settings/ConnectionsSettings.tsx lines 1532 to 1536OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1462,3 +1529,8 @@14621529 </TooltipPopup>14631530 </Tooltip>14641531 ) : null}1532+ <AutomaticServerUpdateControl1533+ environmentId={environmentId}1534+ serverConfig={environment.serverConfig}1535+ compact1536+ />apps/web/src/components/settings/ConnectionsSettings.tsx lines 3511 to 3525OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3397,14 +3519,7 @@3397- render={3398- <DialogTrigger3399- render={3400- <Button3401- size="xs"3402- variant="ghost"3403- className="h-5 gap-1 rounded-sm px-1 text-[11px] font-normal text-muted-foreground/60 hover:text-muted-foreground"3404- aria-label="Add environment"3405- >3406- <PlusIcon className="size-3" />3407- <span>Add environment</span>3408- </Button>3409- }3410- />3519+ onClick={() => void handleEnableAutomaticUpdates()}3520+ >3521+ {isEnablingAutomaticUpdates ? (3522+ <RefreshCwIcon className="size-3 animate-spin" />3523+ ) : null}3524+ <span>{isEnablingAutomaticUpdates ? "Applying…" : "Enable auto-updates"}</span>3525+ </Button>apps/web/src/components/ChatView.tsx lines 2136 to 2137OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2133,4 +2133,5 @@21332133 (serverUpdateState.status !== "idle" ||21342134 (showVersionMismatchBanner && versionMismatch && versionMismatchDismissKey))21352135 ) {2136- const updateInProgress = serverUpdateState.status === "running";2136+ const updateInProgress =2137+ serverUpdateState.status === "running" || serverUpdateState.status === "pending";
- apps/server/src/cloud/selfUpdate.ts line 314Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -189,3 +238,19 @@238+ yield* Effect.logInfo("Server update prepared; handing off to the service launcher.", {239+ updateId,240+ targetVersion,241+ runtimePath: paths.entryPath,242+ });243+ return { targetVersion, method: "boot-service" as const, updateId };189244 });190- return { targetVersion, method: "boot-service" as const, updateId };191- }).pipe(Effect.onError(() => Ref.set(inFlight, false)));245+ return yield* activatePreparedUpdate;246+ }).pipe(247+ Effect.onError(() =>248+ Effect.all(249+ [250+ Ref.set(inFlight, false),251+ Ref.set(automaticHandoffPending, false),252+ Ref.set(lastFailedTarget, targetVersion),253+ ],254+ { discard: true },255+ ),256+ ),
- apps/server/src/cloud/selfUpdate.ts line 286OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -189,3 +268,19 @@268+ );269+270+ yield* Effect.logInfo("Server update prepared; handing off to the service launcher.", {271+ updateId,272+ targetVersion,273+ runtimePath: paths.entryPath,274+ });275+ return { targetVersion, method: "boot-service" as const, updateId };189276 });190- return { targetVersion, method: "boot-service" as const, updateId };191- }).pipe(Effect.onError(() => Ref.set(inFlight, false)));277+ return yield* activatePreparedUpdate;278+ }).pipe(279+ Effect.onError(() =>280+ Effect.all(281+ [282+ Ref.set(inFlight, false),283+ Ref.set(automaticHandoffPending, false),284+ Ref.set(285+ lastFailedTarget,286+ input.automatic === true ||apps/server/src/cloud/selfUpdate.ts line 316Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -191,0 +277,21 @@277+ return yield* activatePreparedUpdate;278+ }).pipe(279+ Effect.onError(() =>280+ Effect.all(281+ [282+ Ref.set(inFlight, false),283+ Ref.set(automaticHandoffPending, false),284+ Ref.set(285+ lastFailedTarget,286+ input.automatic === true ||287+ previousFailedTarget === null ||288+ previousFailedTarget === targetVersion289+ ? targetVersion290+ : previousFailedTarget,291+ ),292+ ],293+ { discard: true },294+ ),295+ ),296+ );297+ return yield* input.automatic === true ? Effect.uninterruptible(operation) : operation;apps/server/src/orchestration/http.ts line 98OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -96 +98 @@9698 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
- @@ -3048,4 +3173,5 @@30483173 control={30493174 primaryVersionMismatch &&30503175 primaryEnvironmentId !== null &&3051- primaryServerUpdateState.status !== "running" ? (3176+ (primaryServerUpdateState.status === "idle" ||3177+ primaryServerUpdateState.status === "failed") ? (apps/web/src/components/settings/ConnectionsSettings.tsx lines 1398 to 1402OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1350,0 +1382,21 @@1382+ onCheckedChange={(checked) => updateSettings({ automaticallyUpdateWhenIdle: checked })}1383+ />1384+ );1385+ const controlWithExplanation = disabledReason ? (1386+ <Tooltip>1387+ <TooltipTrigger render={<span className="inline-flex">{control}</span>} />1388+ <TooltipPopup side="top" className="max-w-80">1389+ {disabledReason}1390+ </TooltipPopup>1391+ </Tooltip>1392+ ) : (1393+ control1394+ );1395+1396+ if (compact) {1397+ return (1398+ <div className="flex items-center gap-2 pt-1 text-xs text-muted-foreground">1399+ {controlWithExplanation}1400+ <span>Auto-update when idle</span>1401+ </div>1402+ );apps/web/src/components/ChatView.tsx lines 2140 to 2140Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2134,3 +2134,4 @@21342134 (showVersionMismatchBanner && versionMismatch && versionMismatchDismissKey))21352135 ) {21362136 const updateInProgress = serverUpdateState.status === "running";2137+ const updatePending = serverUpdateState.status === "pending";
- apps/web/src/components/ChatView.tsx line 2178Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2171,4 +2172,4 @@21712172 "Server update available"21722173 ),21732174 description:2174- updateInProgress || updateFailed ? (2175+ updateInProgress || updatePending || updateFailed ? (
- apps/server/src/orchestration/http.ts lines 114 to 114OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -98,5 +98,17 @@98- );99- return yield* orchestrationEngine100- .dispatch(normalizedCommand)98+ return yield* serverSelfUpdate99+ .withCommandAdmission(100+ Effect.gen(function* () {101+ const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(102+ Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),103+ );104+ return yield* orchestrationEngine105+ .dispatch(normalizedCommand)106+ .pipe(107+ Effect.catch((cause) =>108+ failEnvironmentInternal("orchestration_dispatch_failed", cause),109+ ),110+ );111+ }),112+ )101113 .pipe(102- Effect.catch((cause) =>114+ Effect.catchTag("ServerSelfUpdateError", (cause) =>
- apps/web/src/components/settings/ConnectionsSettings.tsx lines 1399 to 1399OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1350,0 +1379,21 @@1379+ aria-label="Automatically update this server when idle"1380+ checked={supported && enabled}1381+ disabled={!supported}1382+ onCheckedChange={(checked) => updateSettings({ automaticallyUpdateWhenIdle: checked })}1383+ />1384+ );1385+ const controlWithExplanation = disabledReason ? (1386+ <Tooltip>1387+ <TooltipTrigger render={<span className="inline-flex">{control}</span>} />1388+ <TooltipPopup side="top" className="max-w-80">1389+ {disabledReason}1390+ </TooltipPopup>1391+ </Tooltip>1392+ ) : (1393+ control1394+ );1395+1396+ if (compact) {1397+ return (1398+ <label1399+ className="flex w-fit cursor-pointer items-center gap-2 pt-1 text-xs text-muted-foreground"apps/web/src/components/settings/ConnectionsSettings.tsx lines 3192 to 3197OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3060,3 +3189,9 @@30603189 }30613190 />30623191 ) : null}3192+ {primaryEnvironmentId !== null ? (3193+ <AutomaticServerUpdateControl3194+ environmentId={primaryEnvironmentId}3195+ serverConfig={primaryServerConfig}3196+ />3197+ ) : null}
- packages/contracts/src/server.ts lines 652 to 652OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -647,3 +649,4 @@647649 "ServerSelfUpdateError",648650 {649651 reason: TrimmedNonEmptyString,652+ retryWhenIdle: Schema.optional(Schema.Boolean),
- apps/server/src/ws.ts lines 1136 to 1136OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1125,8 +1128,9 @@11251128 }11261129 return result;11271130 }).pipe(1131+ serverSelfUpdate.withCommandAdmission,11281132 Effect.mapError((cause) =>11291133 isOrchestrationDispatchCommandError(cause)11301134 ? cause11311135 : new OrchestrationDispatchCommandError({1132- message: "Failed to dispatch orchestration command",1136+ message: isServerSelfUpdateError(cause)
- apps/web/src/components/AutomaticServerUpdateCoordinator.tsx lines 28 to 35Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +13,21 @@13+function EnvironmentAutomaticServerUpdate({14+ environmentId,15+}: {16+ readonly environmentId: EnvironmentId;17+}) {18+ const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));19+ const shell = useAtomValue(environmentShell.stateValueAtom(environmentId));20+ const updateState = useAtomValue(serverEnvironment.updateStateAtom(environmentId));21+ const updateServer = useAtomCommand(serverEnvironment.updateServer, { reportFailure: false });22+ const setAutomaticUpdatePending = useAtomCommand(serverEnvironment.setAutomaticUpdatePending, {23+ reportFailure: false,24+ });25+ const attemptedTargetRef = useRef<string | null>(null);26+ const decision = automaticServerUpdateDecision({27+ enabled: serverConfig?.settings.automaticallyUpdateWhenIdle ?? false,28+ selfUpdate: serverConfig?.environment.capabilities.serverSelfUpdate ?? null,29+ clientVersion: APP_VERSION,30+ serverVersion: serverConfig?.environment.serverVersion ?? "",31+ shellStatus: shell.status,32+ threads: Option.isSome(shell.snapshot) ? shell.snapshot.value.threads : [],33+ });
- @@ -0,0 +70,21 @@70+ return;71+ }72+73+ if (decisionStatus !== "ready" || fromVersion === null || targetVersion === null) {74+ return;75+ }76+77+ const targetKey = `${fromVersion}->${targetVersion}`;78+ if (updateState.status === "running") {79+ attemptedTargetRef.current = targetKey;80+ return;81+ }82+ if (83+ updateState.status === "failed" &&84+ updateState.fromVersion === fromVersion &&85+ updateState.targetVersion === targetVersion86+ ) {87+ attemptedTargetRef.current = targetKey;88+ return;89+ }90+ if (attemptedTargetRef.current === targetKey) {
juliusmarminge closed this
Sign in to join this conversation.






macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed