Skip to content

Commit 5d50230

Browse files
committed
fix(windows): preserve scheduled task identity
1 parent 1085977 commit 5d50230

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/daemon/schtasks.install.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ describe("installScheduledTask", () => {
7575
});
7676
}
7777

78-
function expectInitialTaskQueries(): void {
78+
function expectInitialTaskQueries(taskName = "OpenClaw Gateway"): void {
7979
expect(schtasksCalls[0]).toEqual(["/Query"]);
80-
expect(schtasksCalls[1]).toEqual(["/Query", "/TN", "OpenClaw Gateway"]);
80+
expect(schtasksCalls[1]).toEqual(["/Query", "/TN", taskName]);
8181
}
8282

83-
function expectTaskRunCall(index: number): void {
84-
expect(schtasksCalls[index]).toEqual(["/Run", "/TN", "OpenClaw Gateway"]);
83+
function expectTaskRunCall(index: number, taskName = "OpenClaw Gateway"): void {
84+
expect(schtasksCalls[index]).toEqual(["/Run", "/TN", taskName]);
8585
}
8686

8787
it("writes quoted set assignments and escapes metacharacters", async () => {
@@ -251,6 +251,7 @@ describe("installScheduledTask", () => {
251251
HOME: env.USERPROFILE,
252252
USERDOMAIN: "WORKSTATION",
253253
USERNAME: "alice",
254+
OPENCLAW_WINDOWS_TASK_NAME: "OpenClaw Custom Gateway",
254255
};
255256
const gatewayEnv = buildServiceEnvironment({
256257
env: callerEnv,
@@ -260,6 +261,7 @@ describe("installScheduledTask", () => {
260261

261262
expect(callerEnv.OPENCLAW_WINDOWS_TASK_HIDDEN_LAUNCHER).toBeUndefined();
262263
expect(gatewayEnv.OPENCLAW_WINDOWS_TASK_HIDDEN_LAUNCHER).toBe("1");
264+
expect(gatewayEnv.OPENCLAW_WINDOWS_TASK_NAME).toBe("OpenClaw Gateway");
263265

264266
const { scriptPath } = await installScheduledTask({
265267
env: callerEnv,
@@ -274,15 +276,15 @@ describe("installScheduledTask", () => {
274276
"/Create",
275277
"/F",
276278
"/TN",
277-
"OpenClaw Gateway",
279+
"OpenClaw Custom Gateway",
278280
"/XML",
279281
]);
280282
expect(schtasksCalls[2]?.slice(6)).toEqual(["/RU", "WORKSTATION\\alice", "/NP"]);
281283
const captured = xmlPayloadCaptures.find((entry) => entry.index === 2);
282284
expect(captured?.xml).toContain("gateway.vbs</Command>");
283285
expect(launcher).toContain("WScript.Shell");
284286
expect(launcher).toContain(`Run """${scriptPath}""", 0, False`);
285-
expectTaskRunCall(3);
287+
expectTaskRunCall(3, "OpenClaw Custom Gateway");
286288
});
287289
});
288290

src/daemon/schtasks.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,11 +918,27 @@ async function restartStartupEntry(
918918
return { outcome: "completed" };
919919
}
920920

921+
const CALLER_OWNED_SERVICE_IDENTITY_KEYS = [
922+
"OPENCLAW_LAUNCHD_LABEL",
923+
"OPENCLAW_SYSTEMD_UNIT",
924+
"OPENCLAW_WINDOWS_TASK_NAME",
925+
] as const;
926+
921927
function resolveScheduledTaskRenderEnv(
922928
env: GatewayServiceEnv,
923929
environment: GatewayServiceEnv | undefined,
924930
): GatewayServiceEnv {
925-
return environment ? { ...env, ...environment } : env;
931+
if (!environment) {
932+
return env;
933+
}
934+
const merged = { ...env, ...environment };
935+
for (const key of CALLER_OWNED_SERVICE_IDENTITY_KEYS) {
936+
const value = env[key]?.trim();
937+
if (value) {
938+
merged[key] = value;
939+
}
940+
}
941+
return merged;
926942
}
927943

928944
async function writeScheduledTaskScript({

0 commit comments

Comments
 (0)