Skip to content

Commit 33f7c17

Browse files
authored
[codex] Fallback when Windows gateway task exits early (#76245)
* fix(windows): bound clean scheduler exits before fallback * test(windows): cover scheduled task startup fallback * test(windows): assert startup fallback is not running
1 parent 14b2ca1 commit 33f7c17

4 files changed

Lines changed: 90 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Docs: https://docs.openclaw.ai
2020
- **Plugin module identity:** keep OpenClaw package chunks on Node's native module graph when jiti transforms plugin entries, preventing duplicate evaluation and class identity drift. (#88384) Thanks @vincentkoc.
2121
- **Shell completion repair:** generate core-only caches during doctor and update repair while preserving full plugin command completion for onboarding and explicit user rebuilds. (#76235)
2222
- **MCP schema diagnostics:** attribute draft-2020-12 compiler failures to the external MCP schema so malformed patterns produce actionable setup errors. Thanks @vincentkoc.
23+
- **Windows Scheduled Task recovery:** keep clean early exits inside the existing bounded launch poll, falling back only when neither the task process nor Gateway listener becomes observable. (#76245)
2324
- **iMessage group warnings:** suppress the false drop-all startup warning when an effective group sender allowlist can admit groups, and point true empty-allowlist configurations at the correct remedy. (#100046)
2425
- **Control UI mobile login:** keep Gateway recovery guidance visible after connection failures, make the disconnected gate scroll safely on constrained screens, and improve mobile keyboard and tap-target behavior. (#100208)
2526

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@
19361936
"test:unit:fast:audit": "node scripts/test-unit-fast-audit.mjs",
19371937
"test:voicecall:closedloop": "node scripts/test-voicecall-closedloop.mjs",
19381938
"test:watch": "node scripts/test-projects.mjs --watch",
1939-
"test:windows:ci": "node scripts/test-projects.mjs src/shared/runtime-import.test.ts src/process/exec.windows.test.ts src/process/windows-command.test.ts src/infra/windows-install-roots.test.ts src/node-host/invoke-system-run-allowlist.test.ts extensions/lobster/src/lobster-runner.test.ts test/scripts/format-generated-module.test.ts test/scripts/npm-runner.test.ts test/scripts/pnpm-runner.test.ts test/scripts/run-with-env.test.ts test/scripts/ui.test.ts test/scripts/vitest-process-group.test.ts",
1939+
"test:windows:ci": "node scripts/test-projects.mjs src/shared/runtime-import.test.ts src/process/exec.windows.test.ts src/process/windows-command.test.ts src/infra/windows-install-roots.test.ts src/node-host/invoke-system-run-allowlist.test.ts src/daemon/schtasks.startup-fallback.test.ts extensions/lobster/src/lobster-runner.test.ts test/scripts/format-generated-module.test.ts test/scripts/npm-runner.test.ts test/scripts/pnpm-runner.test.ts test/scripts/run-with-env.test.ts test/scripts/ui.test.ts test/scripts/vitest-process-group.test.ts",
19401940
"tool-display:check": "node --import tsx scripts/tool-display.ts --check",
19411941
"tool-display:write": "node --import tsx scripts/tool-display.ts --write",
19421942
"ts-topology": "node --import tsx scripts/ts-topology.ts",

‎src/daemon/schtasks.startup-fallback.test.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ function notYetRunTaskQueryOutput() {
257257
].join("\r\n");
258258
}
259259

260+
function cleanExitTaskQueryOutput(lastRunTime = "5/2/2026 2:41:39 PM") {
261+
return ["Status: Ready", `Last Run Time: ${lastRunTime}`, "Last Run Result: 0", ""].join("\r\n");
262+
}
263+
264+
function addAcceptedRunCleanExitResponses(initialOutput = cleanExitTaskQueryOutput()): void {
265+
addStartupFallbackMissingResponses([
266+
{ code: 0, stdout: "", stderr: "" },
267+
{ code: 0, stdout: "", stderr: "" },
268+
{ code: 0, stdout: "", stderr: "" },
269+
{ code: 0, stdout: initialOutput, stderr: "" },
270+
{ code: 0, stdout: "", stderr: "" },
271+
{ code: 0, stdout: cleanExitTaskQueryOutput(), stderr: "" },
272+
]);
273+
}
274+
260275
beforeEach(() => {
261276
resetSchtasksBaseMocks();
262277
findVerifiedGatewayListenerPidsOnPortSync.mockReset();
@@ -392,6 +407,51 @@ describe("Windows startup fallback", () => {
392407
});
393408
});
394409

410+
it("falls back after an accepted task exits cleanly without launch evidence", async () => {
411+
await withWindowsEnv("openclaw-win-startup-", async ({ env }) => {
412+
fastForwardTaskStartWait();
413+
addAcceptedRunCleanExitResponses();
414+
415+
await installGatewayScheduledTask(env);
416+
417+
expectStartupFallbackSpawn();
418+
});
419+
});
420+
421+
it("falls back when Task Scheduler records a fresh clean exit without launch evidence", async () => {
422+
await withWindowsEnv("openclaw-win-startup-", async ({ env }) => {
423+
fastForwardTaskStartWait();
424+
addAcceptedRunCleanExitResponses(cleanExitTaskQueryOutput("5/2/2026 2:40:00 PM"));
425+
426+
await installGatewayScheduledTask(env);
427+
428+
expectStartupFallbackSpawn();
429+
});
430+
});
431+
432+
it("keeps polling when an accepted task transitions from not-yet-run to clean exit", async () => {
433+
await withWindowsEnv("openclaw-win-startup-", async ({ env }) => {
434+
fastForwardTaskStartWait();
435+
addAcceptedRunCleanExitResponses(notYetRunTaskQueryOutput());
436+
437+
await installGatewayScheduledTask(env);
438+
439+
expectStartupFallbackSpawn();
440+
});
441+
});
442+
443+
it("does not fall back when a listener appears after the clean task exit", async () => {
444+
await withWindowsEnv("openclaw-win-startup-", async ({ env }) => {
445+
fastForwardTaskStartWait();
446+
findVerifiedGatewayListenerPidsOnPortSync.mockReturnValueOnce([]).mockReturnValue([4242]);
447+
addAcceptedRunCleanExitResponses();
448+
449+
await installGatewayScheduledTask(env);
450+
451+
expect(spawn).not.toHaveBeenCalled();
452+
});
453+
});
454+
395455
it("does not treat a gateway listener as node Scheduled Task launch evidence", async () => {
396456
await withWindowsEnv("openclaw-win-startup-", async ({ env }) => {
397457
fastForwardTaskStartWait();
@@ -722,7 +782,7 @@ describe("Windows startup fallback", () => {
722782
});
723783

724784
const runtime = await readScheduledTaskRuntime(nodeEnv);
725-
expect(runtime.status).toBe("unknown");
785+
expect(runtime.status).not.toBe("running");
726786
expect(runtime.pid).toBeUndefined();
727787
expect(inspectPortUsage).not.toHaveBeenCalled();
728788
});

‎src/daemon/schtasks.ts‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ async function shouldFallbackScheduledTaskLaunch(params: {
10991099
scriptPath: string;
11001100
}): Promise<boolean> {
11011101
const readLaunchObservation = async (): Promise<{
1102-
state: "running" | "not-yet-run" | "other";
1102+
state: "running" | "not-yet-run" | "stopped-success" | "other";
11031103
signature: string;
11041104
}> => {
11051105
const runtime = await readScheduledTaskRuntime(params.env).catch(() => null);
@@ -1120,6 +1120,14 @@ async function shouldFallbackScheduledTaskLaunch(params: {
11201120
.join("|"),
11211121
};
11221122
}
1123+
if (normalizedResult === "0x0") {
1124+
return {
1125+
state: "stopped-success",
1126+
signature: [runtime?.state, runtime?.lastRunTime, runtime?.lastRunResult, runtime?.detail]
1127+
.filter(Boolean)
1128+
.join("|"),
1129+
};
1130+
}
11231131
return {
11241132
state: "other",
11251133
signature: [runtime?.state, runtime?.lastRunTime, runtime?.lastRunResult, runtime?.detail]
@@ -1186,23 +1194,36 @@ async function shouldFallbackScheduledTaskLaunch(params: {
11861194
});
11871195
};
11881196

1189-
const initial = await readLaunchObservation();
1190-
if (initial.state !== "not-yet-run") {
1197+
let previous = await readLaunchObservation();
1198+
if (previous.state !== "not-yet-run" && previous.state !== "stopped-success") {
11911199
return false;
11921200
}
11931201

11941202
const deadline = Date.now() + SCHEDULED_TASK_FALLBACK_TIMEOUT_MS;
11951203
while (Date.now() < deadline) {
11961204
await sleep(SCHEDULED_TASK_FALLBACK_POLL_MS);
11971205
const current = await readLaunchObservation();
1198-
if (current.state !== "not-yet-run") {
1206+
if (current.state !== "not-yet-run" && current.state !== "stopped-success") {
11991207
return false;
12001208
}
1201-
if (current.signature !== initial.signature) {
1209+
if (
1210+
current.state === "not-yet-run" &&
1211+
previous.state === "not-yet-run" &&
1212+
current.signature !== previous.signature
1213+
) {
1214+
return false;
1215+
}
1216+
// A queued task may finish cleanly before its process/listener becomes observable.
1217+
// Keep that transition inside this bounded poll; the reverse means a new run is starting.
1218+
if (previous.state === "stopped-success" && current.state === "not-yet-run") {
1219+
return false;
1220+
}
1221+
previous = current;
1222+
if (await hasLaunchEvidence()) {
12021223
return false;
12031224
}
12041225
}
1205-
return !(await hasLaunchEvidence());
1226+
return true;
12061227
}
12071228

12081229
async function runScheduledTaskOrThrow(params: {

0 commit comments

Comments
 (0)