From 2e7657f59fc9cc546acc4e01b4b5473750c2f7ba Mon Sep 17 00:00:00 2001 From: Ingwannu <186453546+Ingwannu@users.noreply.github.com> Date: Sun, 22 Nov 2026 04:12:58 +0000 Subject: [PATCH 1/3] fix(service): explain a missing Windows scheduled task on start --- .../content/docs/reference/cli/lifecycle.md | 3 +++ src/service/cli.ts | 14 +++++++++-- src/service/windows-scheduler.ts | 14 ++++++++++- structure/ops/service-and-sidecars.md | 2 +- .../windows/windows-scheduler-errors.test.ts | 24 ++++++++++++++++++- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/docs-site/src/content/docs/reference/cli/lifecycle.md b/docs-site/src/content/docs/reference/cli/lifecycle.md index fdd60495bfe..862af1648e1 100644 --- a/docs-site/src/content/docs/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/reference/cli/lifecycle.md @@ -904,6 +904,9 @@ to refresh its XML before it can hand off to a newer install. ### `ocx service [install|repair|restart|start|stop|status|uninstall|remove]` +If `ocx service start` reports that the Windows Task Scheduler service is not installed, +run `ocx service install` to register it. `start` exits with status 1 when the registration is missing. + On Windows Task Scheduler, the service wrapper restarts the proxy after five seconds even when an external tool terminates it with exit code 0. If another opencodex proxy already owns the port, the wrapper exits deliberately. Use `ocx stop` or diff --git a/src/service/cli.ts b/src/service/cli.ts index a891f052655..17aa6da64a7 100644 --- a/src/service/cli.ts +++ b/src/service/cli.ts @@ -24,7 +24,7 @@ import { ownershipMutationLeaseStatusLine } from "./ownership-mutation-lease.mjs import { foreignServiceOwnerRefusal, unknownServiceOwnerRefusal } from "./repair"; import type { ServiceBackend } from "./state"; import { unitPath } from "./systemd"; -import { inspectWindowsSchedulerServiceStatus, schtasksErrorDetail, probeWindowsSchedulerTask } from "./windows-scheduler"; +import { inspectWindowsSchedulerServiceStatus, schtasksErrorDetail, probeWindowsSchedulerTask, windowsSchedulerStartRecovery } from "./windows-scheduler"; import type { WindowsSchedulerTaskProbe } from "./windows-scheduler"; import { win32 } from "node:path"; import { serviceDiagnosticsSummary } from "./diagnostics"; @@ -393,7 +393,17 @@ export async function serviceCommand(...args: (string | undefined)[]): Promise { expect(schtasksErrorDetail(failure())).toBe("Task Scheduler command failed (exit code 1): schtasks /query"); }); }); + +describe("Windows Scheduler start recovery (#9038)", () => { + const runFailure = () => failedCommand(["/run", "/tn", task], failure(1, Buffer.from("ERROR: The system cannot find the file specified."))); + + test("proven absence returns install guidance after the start attempt fails", () => { + expect(windowsSchedulerStartRecovery(runFailure(), () => ({ status: "absent" }))) + .toBe("Service is not installed (Task Scheduler). Run 'ocx service install' first."); + }); + + for (const state of [{ status: "present" }, { status: "unknown", detail: "query failed" }] as const) { + test(`gives no install advice when the registration is ${state.status}`, () => { + expect(windowsSchedulerStartRecovery(runFailure(), () => state)).toBeNull(); + }); + } + + test("explicit permission and other command failures do not query registration", () => { + const probe = () => { throw new Error("unexpected registration probe"); }; + const denied = failedCommand(["/run", "/tn", task], failure(1, Buffer.from("ERROR: Access is denied."))); + expect(windowsSchedulerStartRecovery(denied, probe)).toBeNull(); + expect(windowsSchedulerStartRecovery(failedCommand(["/end", "/tn", task], failure()), probe)).toBeNull(); + }); +}); From f78fdef93e4858316544d44086c25dcfed4bd939 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 25 Nov 2026 10:29:48 +0900 Subject: [PATCH 2/3] fix(service): test the Scheduler start path that gives install advice Move the try/catch around the Scheduler start into startWindowsSchedulerService so the advice-or-rethrow decision is exercised directly: one start attempt, advice only for a confirmed missing registration, the original error otherwise. Supersedes #9085, which reported the same missing-task start failure (#9038). Co-authored-by: EnesKartal <43950508+EnesKartal@users.noreply.github.com> --- src/service/cli.ts | 15 ++++++--------- src/service/windows-scheduler.ts | 18 ++++++++++++++++++ tests/windows/windows-scheduler-errors.test.ts | 17 ++++++++++++++++- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/service/cli.ts b/src/service/cli.ts index 17aa6da64a7..99244fdc868 100644 --- a/src/service/cli.ts +++ b/src/service/cli.ts @@ -24,7 +24,7 @@ import { ownershipMutationLeaseStatusLine } from "./ownership-mutation-lease.mjs import { foreignServiceOwnerRefusal, unknownServiceOwnerRefusal } from "./repair"; import type { ServiceBackend } from "./state"; import { unitPath } from "./systemd"; -import { inspectWindowsSchedulerServiceStatus, schtasksErrorDetail, probeWindowsSchedulerTask, windowsSchedulerStartRecovery } from "./windows-scheduler"; +import { inspectWindowsSchedulerServiceStatus, schtasksErrorDetail, probeWindowsSchedulerTask, startWindowsSchedulerService } from "./windows-scheduler"; import type { WindowsSchedulerTaskProbe } from "./windows-scheduler"; import { win32 } from "node:path"; import { serviceDiagnosticsSummary } from "./diagnostics"; @@ -393,14 +393,11 @@ export async function serviceCommand(...args: (string | undefined)[]): Promise ops.start()) + : (ops.start(), null); + if (missingTask) { + console.error(`❌ ${missingTask}`); process.exitCode = 1; break; } diff --git a/src/service/windows-scheduler.ts b/src/service/windows-scheduler.ts index 8c9f3bffa7b..1f4834194ab 100644 --- a/src/service/windows-scheduler.ts +++ b/src/service/windows-scheduler.ts @@ -269,6 +269,24 @@ export function windowsSchedulerStartRecovery( return "Service is not installed (Task Scheduler). Run 'ocx service install' first."; } +/** + * `ocx service start` on the Scheduler backend: run the start and return install advice only for a + * confirmed missing registration. Every other failure propagates unchanged; success returns null. + */ +export function startWindowsSchedulerService( + start: () => void, + recovery: (error: unknown) => string | null = windowsSchedulerStartRecovery, +): string | null { + try { + start(); + return null; + } catch (error) { + const advice = recovery(error); + if (!advice) throw error; + return advice; + } +} + /** True when the Task Scheduler registration for the default proxy task is proven present. */ export function windowsSchedulerTaskInstalled(taskName = TASK): boolean { return probeWindowsSchedulerTask(taskName).status === "present"; diff --git a/tests/windows/windows-scheduler-errors.test.ts b/tests/windows/windows-scheduler-errors.test.ts index f0574c824d4..7ed37c51040 100644 --- a/tests/windows/windows-scheduler-errors.test.ts +++ b/tests/windows/windows-scheduler-errors.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test"; import { WindowsSchtasksError, WINDOWS_SCHTASKS_CREATE_ACCESS_DENIED_MARKER } from "../../src/lib/windows-elevation"; -import { schtasks, schtasksErrorDetail, windowsSchedulerStartRecovery, type WindowsSchedulerTaskProbe } from "../../src/service/windows-scheduler"; +import { schtasks, schtasksErrorDetail, startWindowsSchedulerService, windowsSchedulerStartRecovery, type WindowsSchedulerTaskProbe } from "../../src/service/windows-scheduler"; import { isWindowsSchedulerEndBenign } from "../../src/service/windows-ops"; const task = "opencodex-proxy"; @@ -110,4 +110,19 @@ describe("Windows Scheduler start recovery (#9038)", () => { expect(windowsSchedulerStartRecovery(denied, probe)).toBeNull(); expect(windowsSchedulerStartRecovery(failedCommand(["/end", "/tn", task], failure()), probe)).toBeNull(); }); + + test("the start runs once and only a confirmed missing registration becomes advice", () => { + let starts = 0; + const missing = runFailure(); + const advice = startWindowsSchedulerService(() => { starts++; throw missing; }, error => windowsSchedulerStartRecovery(error, () => ({ status: "absent" }))); + expect(advice).toBe("Service is not installed (Task Scheduler). Run 'ocx service install' first."); + expect(starts).toBe(1); + expect(startWindowsSchedulerService(() => { starts++; })).toBeNull(); + expect(starts).toBe(2); + // A present or unreadable registration rethrows the original Scheduler error, unchanged. + for (const state of [{ status: "present" }, { status: "unknown", detail: "query failed" }] as const) { + expect(() => startWindowsSchedulerService(() => { throw missing; }, error => windowsSchedulerStartRecovery(error, () => state))) + .toThrow(missing); + } + }); }); From f89fb79cc1ac7cf18c28d0127f7cec2ad12e5819 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 25 Nov 2026 10:30:23 +0900 Subject: [PATCH 3/3] fix(service): keep the start call literal for the environment-guard order check --- src/service/cli.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/service/cli.ts b/src/service/cli.ts index 99244fdc868..3cce746d4ca 100644 --- a/src/service/cli.ts +++ b/src/service/cli.ts @@ -24,7 +24,7 @@ import { ownershipMutationLeaseStatusLine } from "./ownership-mutation-lease.mjs import { foreignServiceOwnerRefusal, unknownServiceOwnerRefusal } from "./repair"; import type { ServiceBackend } from "./state"; import { unitPath } from "./systemd"; -import { inspectWindowsSchedulerServiceStatus, schtasksErrorDetail, probeWindowsSchedulerTask, startWindowsSchedulerService } from "./windows-scheduler"; +import { inspectWindowsSchedulerServiceStatus, schtasksErrorDetail, probeWindowsSchedulerTask, startWindowsSchedulerService, windowsSchedulerStartRecovery } from "./windows-scheduler"; import type { WindowsSchedulerTaskProbe } from "./windows-scheduler"; import { win32 } from "node:path"; import { serviceDiagnosticsSummary } from "./diagnostics"; @@ -393,9 +393,9 @@ export async function serviceCommand(...args: (string | undefined)[]): Promise ops.start()) - : (ops.start(), null); + // Only a confirmed missing Scheduler registration becomes advice; anything else still throws. + const missingTask = startWindowsSchedulerService(() => { ops.start(); }, + process.platform === "win32" && backend === "scheduler" ? windowsSchedulerStartRecovery : () => null); if (missingTask) { console.error(`❌ ${missingTask}`); process.exitCode = 1;