From f2d34b774fdced8e1d385076974ad22a447d9c1d Mon Sep 17 00:00:00 2001 From: ScriptedAlchemy Date: Tue, 17 Nov 2026 11:22:02 -0800 Subject: [PATCH] fix(server): preserve headless Windows service tasks --- apps/server/src/cloud/bootService.test.ts | 11 ++++++++++- apps/server/src/cloud/bootService.ts | 23 ++++++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 4d89805bb19d..f4ca034a98a2 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -1492,15 +1492,24 @@ process.disconnect(); it.effect("stops a Windows launcher by ending its task, and restarts one left disabled", () => Effect.gen(function* () { - const { service, commands, control, makeService } = yield* makeHarness("win32"); + const { service, fs, commands, control, makeService } = yield* makeHarness("win32"); const plan = yield* service.install(); const launcher = new NodeSqlite.DatabaseSync(serviceLauncherLockPath(plan.baseDir)); launcher.exec("BEGIN EXCLUSIVE"); control.launcher = launcher; + const headlessTask = (yield* fs.readFileString(plan.unitPath)).replace( + "InteractiveToken", + "S4U", + ); + yield* fs.writeFileString(plan.unitPath, headlessTask); + control.task = { xml: headlessTask, enabled: true }; + expect((yield* service.status).current).toBe(true); // Task Scheduler runs its own copy of the task, so a deferred restart registers the new one. const newer = yield* makeService(undefined, "1.2.4"); yield* newer.install({ start: false }); + expect(yield* fs.readFileString(plan.unitPath)).toContain("S4U"); + expect(control.task?.xml).toContain("S4U"); const newerRuntime = pinnedRuntimePaths(yield* Path.Path, plan.baseDir, "1.2.4", "win32"); expect(BootService.bootServiceProgramOf(control.task?.xml ?? "")).toBe( newerRuntime.entryPath, diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index 0f94aaad95ba..60ea9d90a024 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -211,7 +211,12 @@ export function renderBootServicePlist( */ export function renderBootServiceTask( plan: BootServicePlan, - options: { readonly userId: string; readonly homeDir: string; readonly conhostPath: string }, + options: { + readonly userId: string; + readonly homeDir: string; + readonly conhostPath: string; + readonly logonType?: "InteractiveToken" | "S4U"; + }, ): string { const launcherArguments = [ ...plan.program, @@ -246,7 +251,7 @@ export function renderBootServiceTask( ` `, ` `, ` ${escapeXmlText(options.userId)}`, - ` InteractiveToken`, + ` ${options.logonType ?? "InteractiveToken"}`, ` LeastPrivilege`, ` `, ` `, @@ -286,7 +291,7 @@ const STOP_STEP_TIMEOUT = Duration.seconds(120); export interface BootServiceManager { readonly kind: "systemd" | "launchd" | "schtasks"; readonly unitPath: string; - readonly render: (plan: BootServicePlan) => string; + readonly render: (plan: BootServicePlan, existingUnit?: string) => string; readonly stop: ReadonlyArray; readonly activate: ReadonlyArray; readonly restart: ReadonlyArray; @@ -493,11 +498,14 @@ function schtasksManager(input: { return { kind: "schtasks", unitPath, - render: (plan) => + render: (plan, existingUnit) => renderBootServiceTask(plan, { userId: input.session.userId, homeDir: input.homeDir, conhostPath: input.path.join(input.session.systemRoot, "System32", "conhost.exe"), + logonType: existingUnit?.includes("S4U") + ? "S4U" + : "InteractiveToken", }), stop, // Registration also enables the task. @@ -1378,7 +1386,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { .pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))); // Files can match the new version even when activation fails; keep that install retryable. yield* fs.writeFileString(restartPendingPath, `${input.cliVersion}\n`, { mode: 0o600 }); - const plan = planForUnit(currentlyInstalled ? yield* fs.readFileString(unitPath) : ""); + const previousUnit = currentlyInstalled ? yield* fs.readFileString(unitPath) : ""; + const plan = planForUnit(previousUnit); yield* writeDurably( statePath, // @effect-diagnostics-next-line globalJsonInEffect:off - fixed launcher-owned document. @@ -1397,7 +1406,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { return yield* new BootServiceUpdatePendingError(); } } - const unit = manager.render(plan); + const unit = manager.render(plan, previousUnit); yield* writeDurably(unitPath, unit); if (!start && manager.kind === "schtasks") yield* refreshWindowsTask(unit); return plan; @@ -1578,7 +1587,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { current: problems.length === 0 && !(manager.kind === "systemd" && systemdUnsafeValue(planForUnit(unit))) && - normalizeUnit(unit) === normalizeUnit(manager.render(planForUnit(unit))) && + normalizeUnit(unit) === normalizeUnit(manager.render(planForUnit(unit), unit)) && runtimeEntryExists && Option.isSome(runtimeSentinel) && runtimeSentinel.value.trim() === input.cliVersion &&