fix: update all no longer runs agents twice or loses background work #19291
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 4 commits into
main fromrefs/pull/19291/headmerged by t3dotgg(GitHub profile, opens in a new tab) 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:XXL
juliusmarminge added labelsmacroscope-review
- apps/server/src/process/agentScope.ts line 164Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -115,0 +144,21 @@144+ "show",145+ ownedSlice,146+ "--property=ActiveState,ControlGroup",147+ ]);148+ if (shown._tag === "None") return;149+ const values = parseKeyValues(shown.value.stdout);150+ if (values.get("ActiveState") !== "active") return;151+ // An active slice can be empty once its last agent exited.152+ const controlGroup = values.get("ControlGroup") ?? "";153+ const events = controlGroup ? yield* readCgroupFile(controlGroup, "cgroup.events") : "";154+ if (parseCgroupCounter(events, "populated") === 0) return;155+ yield* Effect.logWarning("Stopping agents left by a previous server", { slice: ownedSlice });156+ // Agents get SIGTERM, then SIGKILL after their stop timeout.157+ const stopped = yield* runner158+ .run({ command: systemctl, args: ["--user", "stop", ownedSlice], timeout: "30 seconds" })159+ .pipe(Effect.option);160+ if (stopped._tag === "None" || stopped.value.code !== 0) {161+ yield* Effect.logWarning("Could not stop agents left by a previous server", {162+ slice: ownedSlice,163+ stderr: stopped._tag === "Some" ? stopped.value.stderr.trim() : undefined,164+ });apps/desktop/src/updates/DesktopUpdates.ts lines 635 to 640OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -602,0 +620,21 @@620+ encodeInstallRecord({ fromVersion: environment.appVersion, targetVersion }).pipe(621+ Effect.flatMap((text) => fileSystem.writeFileString(installRecordPath, text)),622+ Effect.catch((error) =>623+ logUpdaterWarning("Could not record the update install.", { errorTag: error._tag }),624+ ),625+ );626+ const removeInstallRecord = fileSystem627+ .remove(installRecordPath, { force: true })628+ .pipe(Effect.ignore);629+630+ // Consumes the record of the last install. Returns the version it did not631+ // apply, when the app came back on the version it installed from.632+ const takeNotAppliedInstallVersion = fileSystem.readFileString(installRecordPath).pipe(633+ Effect.flatMap(decodeInstallRecord),634+ Effect.option,635+ Effect.tap(() => removeInstallRecord),636+ Effect.map((record) =>637+ Option.flatMap(record, (value) =>638+ Option.fromNullishOr(resolveNotAppliedInstallVersion(value, environment.appVersion)),639+ ),640+ ),apps/web/src/components/sidebar/useUpdateEverything.ts line 169Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -92,0 +148,21 @@148+ }149+ const localVersion = installLocal ? localUpdateVersion : null;150+ setUpdateEverythingRun(() => ({151+ localVersion,152+ phase: "providers",153+ machines: Array.from(machinesById.values()),154+ }));155+ const failedProviderMachines = new Set<UpdateRunMachine["environmentId"]>();156+ try {157+ const outcome = await runUpdateEverything({158+ ...(downloadLocal ? { downloadLocal: () => downloadLocal(reportLocalError) } : {}),159+ updateProviders: async () => {160+ const runs = await providers.updateAll({ quiet: true });161+ const outcomes = runs.map((run) => {162+ const runOutcome = getProviderUpdateRunOutcome(run);163+ if (runOutcome.status === "failed") failedProviderMachines.add(run.environmentId);164+ return runOutcome;165+ });166+ for (const machine of machinesById.values()) {167+ if (!machine.updatesServer) {168+ settleUpdateRunMachine(
- apps/desktop/src/updates/DesktopUpdates.ts lines 619 to 625Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -600,3 +610,16 @@600610 .remove(updateRestartMarkerPath, { force: true })601611 .pipe(Effect.ignore);602612613+ // Records the version an install hands to the OS installer, so the next614+ // start can tell whether it applied. Best effort, like the restart marker.615+ const installRecordPath = environment.path.join(616+ updateRestartMarkerDir,617+ DESKTOP_UPDATE_INSTALL_RECORD_FILE,618+ );619+ const writeInstallRecord = (targetVersion: string) =>620+ encodeInstallRecord({ fromVersion: environment.appVersion, targetVersion }).pipe(621+ Effect.flatMap((text) => fileSystem.writeFileString(installRecordPath, text)),622+ Effect.catch((error) =>623+ logUpdaterWarning("Could not record the update install.", { errorTag: error._tag }),624+ ),625+ );apps/server/src/process/agentScope.ts line 145Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -111,5 +129,17 @@111129 const run = (command: string, args: ReadonlyArray<string>) =>112130 runner.run({ command, args, timeout: "5 seconds" }).pipe(Effect.option);113131114- // Runs once, on the first agent launch. It proves this process can reach a115- // systemd user manager, creates the agents slice, and sets its limits.132+ const readCgroupFile = (controlGroup: string, file: string) =>133+ fileSystem134+ .readFileString(path.join("/sys/fs/cgroup", controlGroup, file))135+ .pipe(Effect.orElseSucceed(() => ""));136+137+ // Old agents keep their scopes when the server is killed, and the restarted138+ // server resumes their sessions. Stop them first, so two copies of one139+ // session never work in the same worktree.140+ const stopLeftoverAgents = (systemctl: string) =>141+ Effect.gen(function* () {142+ const shown = yield* run(systemctl, [143+ "--user",144+ "show",145+ ownedSlice,apps/server/src/process/agentScope.ts lines 157 to 165Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -115,0 +145,21 @@145+ ownedSlice,146+ "--property=ActiveState,ControlGroup",147+ ]);148+ if (shown._tag === "None") return;149+ const values = parseKeyValues(shown.value.stdout);150+ if (values.get("ActiveState") !== "active") return;151+ // An active slice can be empty once its last agent exited.152+ const controlGroup = values.get("ControlGroup") ?? "";153+ const events = controlGroup ? yield* readCgroupFile(controlGroup, "cgroup.events") : "";154+ if (parseCgroupCounter(events, "populated") === 0) return;155+ yield* Effect.logWarning("Stopping agents left by a previous server", { slice: ownedSlice });156+ // Agents get SIGTERM, then SIGKILL after their stop timeout.157+ const stopped = yield* runner158+ .run({ command: systemctl, args: ["--user", "stop", ownedSlice], timeout: "30 seconds" })159+ .pipe(Effect.option);160+ if (stopped._tag === "None" || stopped.value.code !== 0) {161+ yield* Effect.logWarning("Could not stop agents left by a previous server", {162+ slice: ownedSlice,163+ stderr: stopped._tag === "Some" ? stopped.value.stderr.trim() : undefined,164+ });165+ }
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


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