Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(computer): add Windows BYOA supervision (#71)

LILAC793b0262750e
diff --git a/docs/BYOA.md b/docs/BYOA.md
index dbe8c2a..51083f7 100644
--- a/docs/BYOA.md
+++ b/docs/BYOA.md
@@ -388,9 +388,10 @@ npx cumora@latest agent computer --pair <code> [--server <url>]
separate copy. The repo's root `package.json` stays `private`; only
this thin package is published.
- `--install-service` installs the daemon as a supervised service
- (launchd `io.cumora.daemon` on macOS, `systemd --user` on Linux) so it
- survives reboots and — on macOS — runs in the GUI domain where the
- engine's keychain-backed login actually works.
+ (launchd `io.cumora.daemon` on macOS, `systemd --user` on Linux, a per-user
+ Task Scheduler watchdog on Windows) so it restarts at user login (including
+ after a reboot) and — on macOS — runs in the GUI domain where the engine's
+ keychain-backed login actually works.
- `--doctor` probes the big/small models and the wake path end-to-end.
- In-repo dev uses `./bin/cumora agent computer …` (tsx) — the same
code, unbundled.
diff --git a/server/src/__tests__/agents-computer-windows-service.test.ts b/server/src/__tests__/agents-computer-windows-service.test.ts
new file mode 100644
index 0000000..7f3837f
--- /dev/null
+++ b/server/src/__tests__/agents-computer-windows-service.test.ts
@@ -0,0 +1,106 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import {
+ isWindowsSupervisorProcess,
+ parseWindowsProcessList,
+ renderWindowsSupervisor,
+ resolveNpx,
+ windowsScheduledTaskCommand,
+ windowsScheduledTaskCreateArgs,
+ windowsScheduledTaskQueryCommand,
+ windowsScheduledTaskSettingsCommand,
+ windowsTaskName,
+} from '../agents/computer/daemon.js'
+
+test('Windows service resolves the executable command shim', () => {
+ assert.equal(resolveNpx('win32', 'Z:\\missing\\node.exe'), 'npx.cmd')
+ assert.equal(resolveNpx('linux', '/missing/node'), 'npx')
+})
+
+test('Windows supervisor restarts the latest daemon with supervision enabled', () => {
+ const script = renderWindowsSupervisor(
+ 'C:\\Program Files\\nodejs\\npx.cmd',
+ "https://example.test/tenant's-api",
+ "C:\\Users\\O'Brien\\.cumora\\daemon.log",
+ "C:\\Users\\O'Brien\\.cumora\\daemon-supervisor.disabled",
+ "C:\\Program Files\\nodejs;C:\\Users\\O'Brien\\bin",
+ )
+
+ assert.match(script, /\$env:CUMORA_SUPERVISED = '1'/)
+ assert.match(script, /while \(-not \(Test-Path -LiteralPath/)
+ assert.match(script, /& 'C:\\Program Files\\nodejs\\npx\.cmd' -y cumora@latest agent computer --server/)
+ assert.match(script, /tenant''s-api/)
+ assert.match(script, /O''Brien/)
+ assert.match(script, /2>&1 \| ForEach-Object/)
+ assert.match(script, /\[System\.IO\.File\]::AppendAllText/)
+ assert.doesNotMatch(script, /Out-File/)
+ assert.doesNotMatch(script, /\*>>/)
+ assert.match(script, /Start-Sleep -Seconds 5/)
+})
+
+test('Windows scheduled task runs the watchdog at login with limited privileges', () => {
+ const scriptPath = 'C:\\Users\\Test User\\.cumora\\daemon-supervisor.ps1'
+ const taskName = windowsTaskName('C:\\Users\\Test User')
+ assert.equal(
+ windowsScheduledTaskCommand(scriptPath),
+ 'powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\\Users\\Test User\\.cumora\\daemon-supervisor.ps1"',
+ )
+ assert.deepEqual(windowsScheduledTaskCreateArgs(scriptPath, taskName), [
+ '/Create', '/TN', taskName,
+ '/TR', windowsScheduledTaskCommand(scriptPath),
+ '/SC', 'ONLOGON', '/RL', 'LIMITED', '/IT', '/F',
+ ])
+
+ const settings = windowsScheduledTaskSettingsCommand(taskName)
+ assert.match(settings, /ExecutionTimeLimit \(\[TimeSpan\]::Zero\)/)
+ assert.match(settings, /-AllowStartIfOnBatteries/)
+ assert.match(settings, /-DontStopIfGoingOnBatteries/)
+ assert.match(settings, /-StartWhenAvailable/)
+})
+
+test('Windows task names are stable and isolated per user home', () => {
+ assert.equal(windowsTaskName('C:\\Users\\Alice'), windowsTaskName('c:\\users\\alice'))
+ assert.notEqual(windowsTaskName('C:\\Users\\Alice'), windowsTaskName('C:\\Users\\Bob'))
+})
+
+test('Windows task query distinguishes absence from scheduler failures', () => {
+ const query = windowsScheduledTaskQueryCommand("Cumora O'Brien")
+ assert.match(query, /Get-ScheduledTask -TaskName 'Cumora O''Brien' -ErrorAction Stop/)
+ assert.match(query, /CmdletizationQuery_NotFound_TaskName,\*/)
+ assert.match(query, /exit 3/)
+ assert.match(query, /Write-Error \$_; exit 1/)
+})
+
+test('Windows CIM process output accepts both singleton and array JSON', () => {
+ assert.deepEqual(parseWindowsProcessList('{"ProcessId":42,"CommandLine":"node cli.js agent computer"}'), [
+ { ProcessId: 42, CommandLine: 'node cli.js agent computer' },
+ ])
+ assert.deepEqual(parseWindowsProcessList('[{"ProcessId":42,"CommandLine":"a"},{"ProcessId":0,"CommandLine":"b"}]'), [
+ { ProcessId: 42, CommandLine: 'a' },
+ ])
+ assert.deepEqual(parseWindowsProcessList(''), [])
+})
+
+test('Windows watchdog matching requires PowerShell -File with the exact script', () => {
+ const scriptPath = 'C:\\Users\\Test User\\.cumora\\daemon-supervisor.ps1'
+ assert.equal(isWindowsSupervisorProcess({
+ ProcessId: 42,
+ Name: 'powershell.exe',
+ CommandLine: `powershell.exe -NoProfile -File "${scriptPath}"`,
+ }, scriptPath), true)
+ assert.equal(isWindowsSupervisorProcess({
+ ProcessId: 43,
+ Name: 'notepad.exe',
+ CommandLine: `notepad.exe "${scriptPath}"`,
+ }, scriptPath), false)
+ assert.equal(isWindowsSupervisorProcess({
+ ProcessId: 44,
+ Name: 'powershell.exe',
+ CommandLine: `powershell.exe -Command "Get-Content '${scriptPath}'"`,
+ }, scriptPath), false)
+ assert.equal(isWindowsSupervisorProcess({
+ ProcessId: 45,
+ Name: 'powershell.exe',
+ CommandLine: 'powershell.exe -File "C:\\other\\daemon-supervisor.ps1"',
+ }, scriptPath), false)
+})
diff --git a/server/src/agents/computer/daemon.ts b/server/src/agents/computer/daemon.ts
index 8f7d28d..b9031fb 100644
--- a/server/src/agents/computer/daemon.ts
+++ b/server/src/agents/computer/daemon.ts
@@ -293,6 +293,11 @@ const SERVICE_LABEL = 'io.cumora.daemon'
* to apply an update. A manually-run daemon just logs the available version. */
const SUPERVISED = process.env.CUMORA_SUPERVISED === '1'
+export function windowsTaskName(home = homedir()): string {
+ const userKey = createHash('sha256').update(home.toLowerCase()).digest('hex').slice(0, 12)
+ return `Cumora BYOA Daemon (${userKey})`
+}
+
/** semver-ish a > b for plain MAJOR.MINOR.PATCH (ignores pre-release tags). */
function versionGt(a: string, b: string): boolean {
const pa = a.split('.').map((n) => parseInt(n, 10) || 0)
@@ -2406,9 +2411,13 @@ async function doRun(serverOverride?: string): Promise<void> {
// Record what THIS process is running, keyed by pid, so `--status` can report
// the version of the live service instance reliably (cross-checked against the
// running pid — survives log rotation, no log-scraping).
+ const windowsShutdownRequest = process.platform === 'win32'
+ ? windowsShutdownRequestPath(process.pid)
+ : null
+ if (windowsShutdownRequest) await rm(windowsShutdownRequest, { force: true }).catch(() => {})
await writeRunningState()
if (!SUPERVISED) {
- console.log(`[computer] 💡 tip: run \`npx cumora@latest agent computer --install-service\` to keep this running in the background — auto-start on boot, auto-restart on crash, and auto-update. (This terminal must stay open otherwise.)`)
+ console.log(`[computer] 💡 tip: run \`npx cumora@latest agent computer --install-service\` to keep this running in the background — auto-start when you sign in, auto-restart on crash, and auto-update. (This terminal must stay open otherwise.)`)
}
const runners = new Map<string, AgentRunner>()
@@ -2478,6 +2487,7 @@ async function doRun(serverOverride?: string): Promise<void> {
let upd: ReturnType<typeof setInterval> | undefined
let idleWatch: ReturnType<typeof setInterval> | undefined
+ let controlWatch: ReturnType<typeof setInterval> | undefined
let shuttingDown = false
const anyBusy = (): boolean => [...runners.values()].some((r) => r.isBusy)
@@ -2491,6 +2501,8 @@ async function doRun(serverOverride?: string): Promise<void> {
clearInterval(poll); clearInterval(beat); clearInterval(logrot)
if (upd) clearInterval(upd)
if (idleWatch) clearInterval(idleWatch)
+ if (controlWatch) clearInterval(controlWatch)
+ if (windowsShutdownRequest) await rm(windowsShutdownRequest, { force: true }).catch(() => {})
for (const runner of runners.values()) runner.beginStop() // no new turns; leave in-flight running
const deadline = Date.now() + SHUTDOWN_GRACE_MS
if (anyBusy()) console.log(`[computer] ${why}: waiting up to ${Math.round(SHUTDOWN_GRACE_MS / 1000)}s for in-flight turn(s) to finish…`)
@@ -2501,6 +2513,12 @@ async function doRun(serverOverride?: string): Promise<void> {
}
process.on('SIGINT', () => { void shutdown('SIGINT') })
process.on('SIGTERM', () => { void shutdown('SIGTERM') })
+ if (windowsShutdownRequest) {
+ controlWatch = setInterval(() => {
+ if (existsSync(windowsShutdownRequest)) void shutdown('service control')
+ }, 250)
+ controlWatch.unref?.()
+ }
// Self-update: compare to npm's latest periodically. When supervised
// (--install-service), a clean exit relaunches the service on cumora@latest =
@@ -2549,15 +2567,95 @@ async function checkForUpdate(onSupervisedUpdate: () => void): Promise<void> {
/** Absolute path to npx next to the node that's running us, so the supervisor
* doesn't depend on its (minimal) PATH resolving `npx`. Falls back to PATH. */
-function resolveNpx(): string {
- const sibling = join(dirname(process.execPath), 'npx')
- return existsSync(sibling) ? sibling : 'npx'
+export function resolveNpx(
+ platform: NodeJS.Platform = process.platform,
+ execPath = process.execPath,
+): string {
+ const executable = platform === 'win32' ? 'npx.cmd' : 'npx'
+ const sibling = join(dirname(execPath), executable)
+ return existsSync(sibling) ? sibling : executable
+}
+
+function windowsSupervisorPath(): string {
+ return join(CONFIG_DIR, 'daemon-supervisor.ps1')
+}
+
+function windowsSupervisorDisabledPath(): string {
+ return join(CONFIG_DIR, 'daemon-supervisor.disabled')
+}
+
+function windowsShutdownRequestPath(pid: number): string {
+ return join(CONFIG_DIR, `shutdown-${pid}.request`)
+}
+
+function quotePowerShell(value: string): string {
+ return `'${value.replaceAll("'", "''")}'`
+}
+
+/** The scheduled task starts this watchdog at login. It deliberately restarts
+ * after every daemon exit, including the clean exit used to apply an update. */
+export function renderWindowsSupervisor(
+ npx: string,
+ serverUrl: string,
+ logPath: string,
+ disabledPath = windowsSupervisorDisabledPath(),
+ path = process.env.PATH ?? '',
+): string {
+ return [
+ "$ErrorActionPreference = 'Continue'",
+ "$env:CUMORA_SUPERVISED = '1'",
+ `$env:PATH = ${quotePowerShell(path)}`,
+ '$utf8 = New-Object System.Text.UTF8Encoding($false)',
+ `while (-not (Test-Path -LiteralPath ${quotePowerShell(disabledPath)})) {`,
+ ` & ${quotePowerShell(npx)} -y cumora@latest agent computer --server ${quotePowerShell(serverUrl)} 2>&1 | ForEach-Object {`,
+ ` [System.IO.File]::AppendAllText(${quotePowerShell(logPath)}, ([string]$_ + [Environment]::NewLine), $utf8)`,
+ ' }',
+ ' Start-Sleep -Seconds 5',
+ '}',
+ '',
+ ].join('\r\n')
+}
+
+export function windowsScheduledTaskCommand(scriptPath: string): string {
+ return `powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File "${scriptPath.replaceAll('"', '""')}"`
+}
+
+export function windowsScheduledTaskCreateArgs(
+ scriptPath: string,
+ taskName = windowsTaskName(),
+): string[] {
+ return [
+ '/Create', '/TN', taskName,
+ '/TR', windowsScheduledTaskCommand(scriptPath),
+ '/SC', 'ONLOGON', '/RL', 'LIMITED', '/IT', '/F',
+ ]
+}
+
+export function windowsScheduledTaskSettingsCommand(taskName = windowsTaskName()): string {
+ return `$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit ([TimeSpan]::Zero) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable; Set-ScheduledTask -TaskName ${quotePowerShell(taskName)} -Settings $settings | Out-Null`
+}
+
+export function windowsScheduledTaskQueryCommand(taskName = windowsTaskName()): string {
+ return `try { Get-ScheduledTask -TaskName ${quotePowerShell(taskName)} -ErrorAction Stop | Out-Null; exit 0 } catch { if ($_.FullyQualifiedErrorId -like 'CmdletizationQuery_NotFound_TaskName,*') { exit 3 }; Write-Error $_; exit 1 }`
+}
+
+async function isWindowsTaskInstalled(taskName = windowsTaskName()): Promise<boolean> {
+ try {
+ await execFileP('powershell.exe', [
+ '-NoProfile', '-NonInteractive', '-Command', windowsScheduledTaskQueryCommand(taskName),
+ ])
+ return true
+ } catch (err) {
+ if (String((err as { code?: number | string }).code) === '3') return false
+ throw err
+ }
}
/** Install a per-user supervisor (LaunchAgent on macOS, systemd --user on
- * Linux) that runs `npx -y cumora@latest agent computer --server <url>` with
- * auto-restart + run-at-boot. `@latest` + restart-on-update is what makes the
- * daemon self-update (see checkForUpdate). Must be paired first. */
+ * Linux, Task Scheduler on Windows) that runs
+ * `npx -y cumora@latest agent computer --server <url>` with auto-restart +
+ * start-at-login. `@latest` + restart-on-update is what makes the daemon
+ * self-update (see checkForUpdate). Must be paired first. */
async function installService(serverUrl: string): Promise<void> {
if (!(await loadConfig())) {
throw new Error('pair this computer first: cumora agent computer --pair <code>')
@@ -2619,7 +2717,48 @@ WantedBy=default.target
return
}
- throw new Error(`--install-service supports macOS and Linux (not ${process.platform})`)
+ if (process.platform === 'win32') {
+ const scriptPath = windowsSupervisorPath()
+ const disabledPath = windowsSupervisorDisabledPath()
+ const taskName = windowsTaskName()
+ const replacing = await isWindowsTaskInstalled(taskName)
+ await writeFile(scriptPath, renderWindowsSupervisor(npx, serverUrl, logPath, disabledPath), 'utf8')
+ try {
+ if (!replacing) {
+ await execFileP('schtasks.exe', windowsScheduledTaskCreateArgs(scriptPath, taskName))
+ }
+ await execFileP('powershell.exe', [
+ '-NoProfile', '-NonInteractive', '-Command', windowsScheduledTaskSettingsCommand(taskName),
+ ])
+ } catch (err) {
+ if (!replacing) {
+ try {
+ await execFileP('schtasks.exe', ['/Delete', '/TN', taskName, '/F'])
+ } catch (rollbackErr) {
+ throw new Error(`scheduled task setup failed and rollback could not delete '${taskName}'; the watchdog script was kept in place (${rollbackErr instanceof Error ? rollbackErr.message : String(rollbackErr)})`, { cause: err })
+ }
+ await rm(scriptPath, { force: true }).catch(() => {})
+ }
+ throw err
+ }
+ await writeFile(disabledPath, '', 'utf8')
+ await killRunningDaemons()
+ await stopWindowsWatchdog(taskName)
+ // The old watchdog could have spawned a daemon between the first process
+ // sweep and its own exit. Sweep once more only after it cannot relaunch one.
+ await killRunningDaemons()
+ await rm(disabledPath, { force: true })
+ try {
+ await execFileP('schtasks.exe', ['/Run', '/TN', taskName])
+ } catch (err) {
+ throw new Error(`scheduled task installed but could not be started; retry with --restart (${err instanceof Error ? err.message : String(err)})`)
+ }
+ console.log(`[computer] installed scheduled task '${taskName}' — start-at-login, auto-restart, auto-update. Logs: ${logPath}`)
+ console.log(`[computer] you can now close this terminal; the task is running in the background.`)
+ return
+ }
+
+ throw new Error(`--install-service is not supported on ${process.platform}`)
}
async function uninstallService(): Promise<void> {
@@ -2637,7 +2776,28 @@ async function uninstallService(): Promise<void> {
console.log(`[computer] removed systemd --user service 'cumora'`)
return
}
- throw new Error(`--uninstall-service supports macOS and Linux (not ${process.platform})`)
+ if (process.platform === 'win32') {
+ const taskName = windowsTaskName()
+ const disabledPath = windowsSupervisorDisabledPath()
+ await mkdir(CONFIG_DIR, { recursive: true })
+ await writeFile(disabledPath, '', 'utf8')
+ if (await isWindowsTaskInstalled(taskName)) {
+ await execFileP('schtasks.exe', ['/Delete', '/TN', taskName, '/F'])
+ }
+ await killRunningDaemons()
+ await stopWindowsWatchdog(taskName)
+ await killRunningDaemons()
+ if (await isWindowsTaskInstalled(taskName)) {
+ throw new Error(`scheduled task '${taskName}' still exists after deletion`)
+ }
+ await rm(windowsSupervisorPath(), { force: true })
+ // Keep this sentinel on every failed stop path so a surviving watchdog
+ // cannot revive a daemon after the command has reported an error.
+ await rm(disabledPath, { force: true })
+ console.log(`[computer] removed scheduled task '${taskName}'`)
+ return
+ }
+ throw new Error(`--uninstall-service is not supported on ${process.platform}`)
}
function darwinPlistPath(): string {
@@ -2648,9 +2808,10 @@ function linuxUnitPath(): string {
}
/** Is the background supervisor already installed on this machine? */
-function isServiceInstalled(): boolean {
+async function isServiceInstalled(): Promise<boolean> {
if (process.platform === 'darwin') return existsSync(darwinPlistPath())
if (process.platform === 'linux') return existsSync(linuxUnitPath())
+ if (process.platform === 'win32') return isWindowsTaskInstalled()
return false
}
@@ -2666,6 +2827,18 @@ async function reloadService(): Promise<void> {
}
if (process.platform === 'linux') {
await execFileP('systemctl', ['--user', 'restart', 'cumora'])
+ return
+ }
+ if (process.platform === 'win32') {
+ const taskName = windowsTaskName()
+ const disabledPath = windowsSupervisorDisabledPath()
+ await mkdir(CONFIG_DIR, { recursive: true })
+ await writeFile(disabledPath, '', 'utf8')
+ await killRunningDaemons()
+ await stopWindowsWatchdog(taskName)
+ await killRunningDaemons()
+ await rm(disabledPath, { force: true })
+ await execFileP('schtasks.exe', ['/Run', '/TN', taskName])
}
}
@@ -2673,7 +2846,7 @@ async function reloadService(): Promise<void> {
* `launchctl kickstart …`. Restarts the installed service (which relaunches on
* cumora@latest, so it's also the "apply the update now" button). */
async function restartService(): Promise<void> {
- if (!isServiceInstalled()) {
+ if (!(await isServiceInstalled())) {
console.log('[computer] service not installed — run: npx cumora@latest agent computer --install-service')
return
}
@@ -2686,8 +2859,10 @@ async function restartService(): Promise<void> {
}
} else if (process.platform === 'linux') {
await execFileP('systemctl', ['--user', 'restart', 'cumora'])
+ } else if (process.platform === 'win32') {
+ await reloadService()
} else {
- throw new Error(`--restart supports macOS and Linux (not ${process.platform})`)
+ throw new Error(`--restart is not supported on ${process.platform}`)
}
console.log('[computer] service restarted — it relaunches on cumora@latest (also applies any pending update). Check: npx cumora@latest agent computer --status')
}
@@ -2716,19 +2891,93 @@ export function isStoppableDaemonCommand(cmd: string): boolean {
return /agent computer/.test(cmd) && !ONE_SHOT_FLAG_RE.test(cmd)
}
-/** Kill any RUNNING daemon process — the supervised one (after the service is
+async function commandLineForPid(pid: number): Promise<string> {
+ if (process.platform === 'win32') {
+ const command = `(Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}" -ErrorAction SilentlyContinue).CommandLine`
+ return (await execFileP('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', command])).stdout.trim()
+ }
+ return (await execFileP('ps', ['-p', String(pid), '-o', 'command='])).stdout.trim()
+}
+
+interface WindowsProcessInfo {
+ ProcessId: number
+ CommandLine: string
+ Name?: string
+}
+
+export function parseWindowsProcessList(output: string): WindowsProcessInfo[] {
+ if (!output.trim()) return []
+ const parsed = JSON.parse(output) as WindowsProcessInfo | WindowsProcessInfo[]
+ return (Array.isArray(parsed) ? parsed : [parsed]).filter((item) =>
+ Number.isInteger(item?.ProcessId) && item.ProcessId > 0 && typeof item.CommandLine === 'string')
+}
+
+async function windowsDaemonProcesses(): Promise<WindowsProcessInfo[]> {
+ const command = [
+ '$OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)',
+ `@(Get-CimInstance Win32_Process -ErrorAction Stop | Where-Object { $_.CommandLine -match 'agent\\s+computer' } | Select-Object ProcessId, CommandLine) | ConvertTo-Json -Compress`,
+ ].join('; ')
+ const { stdout } = await execFileP('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', command])
+ return parseWindowsProcessList(stdout)
+}
+
+async function windowsSupervisorProcesses(): Promise<WindowsProcessInfo[]> {
+ const command = [
+ '$OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)',
+ `@(Get-CimInstance Win32_Process -ErrorAction Stop | Where-Object { ($_.Name -ieq 'powershell.exe' -or $_.Name -ieq 'pwsh.exe') -and $_.CommandLine -match 'daemon-supervisor\\.ps1' } | Select-Object ProcessId, CommandLine, Name) | ConvertTo-Json -Compress`,
+ ].join('; ')
+ const { stdout } = await execFileP('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', command])
+ return parseWindowsProcessList(stdout).filter((item) => isWindowsSupervisorProcess(item))
+}
+
+export function isWindowsSupervisorProcess(
+ item: WindowsProcessInfo,
+ scriptPath = windowsSupervisorPath(),
+): boolean {
+ if (!item.Name || !['powershell.exe', 'pwsh.exe'].includes(item.Name.toLowerCase())) return false
+ const match = item.CommandLine.match(/(?:^|\s)-File\s+(?:"([^"]+)"|(\S+))(?:\s|$)/i)
+ const invokedScript = match?.[1] ?? match?.[2]
+ return invokedScript?.toLowerCase() === scriptPath.toLowerCase()
+}
+
+async function stopWindowsWatchdog(taskName: string): Promise<void> {
+ await execFileP('schtasks.exe', ['/End', '/TN', taskName]).catch(() => { /* deleted/already stopped */ })
+ const deadline = Date.now() + 2_000
+ let watchdogs = await windowsSupervisorProcesses()
+ while (watchdogs.length > 0 && Date.now() < deadline) {
+ await new Promise((resolve) => setTimeout(resolve, 100))
+ watchdogs = await windowsSupervisorProcesses()
+ }
+ for (const watchdog of watchdogs) {
+ await execFileP('taskkill.exe', ['/PID', String(watchdog.ProcessId), '/T', '/F']).catch(() => {})
+ }
+ const forceDeadline = Date.now() + 2_000
+ watchdogs = await windowsSupervisorProcesses()
+ while (watchdogs.length > 0 && Date.now() < forceDeadline) {
+ await new Promise((resolve) => setTimeout(resolve, 100))
+ watchdogs = await windowsSupervisorProcesses()
+ }
+ if (watchdogs.length > 0) {
+ throw new Error(`Windows watchdog process(es) still running: ${watchdogs.map((item) => item.ProcessId).join(', ')}`)
+ }
+}
+
+/** Stop any RUNNING daemon process — the supervised one (after the service is
* uninstalled), a foreground one, or a stray/orphaned one. Sources: the pid the
- * daemon records in running.json, plus a `pgrep` sweep for "agent computer". We
+ * daemon records in running.json, plus an OS process sweep for "agent computer". We
* verify each candidate's command line really is a long-running daemon (and is
- * NOT this --stop command or another one-shot CLI) before killing — SIGTERM,
- * then SIGKILL anything that ignores it. Best-effort. */
+ * NOT this --stop command or another one-shot CLI) before stopping it. Windows
+ * uses a per-PID request file for graceful shutdown, then taskkill /T as the
+ * bounded fallback so engine descendants cannot be orphaned. */
async function killRunningDaemons(): Promise<void> {
const candidates = new Set<number>()
try {
const pid = (JSON.parse(await readFile(RUNNING_STATE_PATH, 'utf8')) as { pid?: number }).pid
if (typeof pid === 'number' && pid > 0) candidates.add(pid)
} catch { /* no pid file */ }
- if (process.platform !== 'win32') {
+ if (process.platform === 'win32') {
+ for (const item of await windowsDaemonProcesses()) candidates.add(item.ProcessId)
+ } else {
try {
const { stdout } = await execFileP('pgrep', ['-f', 'agent computer'])
for (const l of stdout.split('\n')) { const p = parseInt(l.trim(), 10); if (p > 0) candidates.add(p) }
@@ -2739,8 +2988,7 @@ async function killRunningDaemons(): Promise<void> {
const victims: number[] = []
for (const pid of candidates) {
try {
- const { stdout } = await execFileP('ps', ['-p', String(pid), '-o', 'command='])
- const cmd = stdout.trim()
+ const cmd = await commandLineForPid(pid)
// A genuine long-running daemon: "agent computer" with NO one-shot flag —
// so we never kill a sibling one-shot CLI. (Ourselves and our parent are
// already out of `candidates`.)
@@ -2749,11 +2997,49 @@ async function killRunningDaemons(): Promise<void> {
}
} catch { /* gone */ }
}
- for (const pid of victims) { try { process.kill(pid, 'SIGTERM') } catch { /* gone */ } }
+ if (process.platform === 'win32') {
+ if (victims.length > 0) await mkdir(CONFIG_DIR, { recursive: true })
+ for (const pid of victims) {
+ await writeFile(windowsShutdownRequestPath(pid), 'stop\n', 'utf8').catch(() => {})
+ }
+ const deadline = Date.now() + SHUTDOWN_GRACE_MS + 2_000
+ let survivors = victims
+ while (survivors.length > 0 && Date.now() < deadline) {
+ await new Promise((resolve) => setTimeout(resolve, 250))
+ const next: number[] = []
+ for (const pid of survivors) {
+ try {
+ if (isStoppableDaemonCommand(await commandLineForPid(pid))) next.push(pid)
+ } catch { /* exited */ }
+ }
+ survivors = next
+ }
+ for (const pid of survivors) {
+ await execFileP('taskkill.exe', ['/PID', String(pid), '/T', '/F']).catch(() => {})
+ }
+ for (const pid of victims) await rm(windowsShutdownRequestPath(pid), { force: true }).catch(() => {})
+ const forceDeadline = Date.now() + 2_000
+ let remaining = (await windowsDaemonProcesses()).filter((item) =>
+ item.ProcessId !== process.pid && item.ProcessId !== process.ppid &&
+ isStoppableDaemonCommand(item.CommandLine))
+ while (remaining.length > 0 && Date.now() < forceDeadline) {
+ await new Promise((resolve) => setTimeout(resolve, 100))
+ remaining = (await windowsDaemonProcesses()).filter((item) =>
+ item.ProcessId !== process.pid && item.ProcessId !== process.ppid &&
+ isStoppableDaemonCommand(item.CommandLine))
+ }
+ if (remaining.length > 0) {
+ throw new Error(`Windows daemon process(es) still running: ${remaining.map((item) => item.ProcessId).join(', ')}`)
+ }
+ } else {
+ for (const pid of victims) { try { process.kill(pid, 'SIGTERM') } catch { /* gone */ } }
+ if (victims.length > 0) {
+ await new Promise((r) => setTimeout(r, 1500))
+ for (const pid of victims) { try { process.kill(pid, 0); process.kill(pid, 'SIGKILL') } catch { /* already exited */ } }
+ }
+ }
if (victims.length > 0) {
- await new Promise((r) => setTimeout(r, 1500))
- for (const pid of victims) { try { process.kill(pid, 0); process.kill(pid, 'SIGKILL') } catch { /* already exited */ } }
- console.log(`[computer] killed ${victims.length} running daemon process(es)`)
+ console.log(`[computer] stopped ${victims.length} running daemon process(es)`)
}
await rm(RUNNING_STATE_PATH, { force: true }).catch(() => {})
}
@@ -2762,12 +3048,16 @@ async function killRunningDaemons(): Promise<void> {
* can't relaunch it) AND kill any daemon process still running. After this the
* agents are fully offline until you re-pair / re-install. */
async function stopService(): Promise<void> {
- if (isServiceInstalled()) {
+ if (process.platform === 'win32') {
+ // Always clean up on Windows: a manually deleted task may still have left a
+ // watchdog process or script behind, and that watchdog can relaunch a daemon.
+ await uninstallService()
+ } else if (await isServiceInstalled()) {
await uninstallService() // removes the plist/unit + unloads → kills the supervised process
} else {
console.log('[computer] no background service installed — killing any running daemon process directly.')
}
- await killRunningDaemons()
+ if (process.platform !== 'win32') await killRunningDaemons()
console.log('[computer] stopped — service removed and daemon process(es) killed. Re-pair to start again: npx cumora@latest agent computer --pair <code>')
}
@@ -2780,7 +3070,7 @@ async function printStatus(): Promise<void> {
// so we report the service's running version separately, read from its log.
console.log(`cli: cumora ${CURRENT_VERSION} (this command)`)
console.log(cfg ? `paired: computer ${cfg.computerId} @ ${cfg.serverUrl}` : 'paired: NO — run: npx cumora@latest agent computer --pair <code>')
- if (!isServiceInstalled()) {
+ if (!(await isServiceInstalled())) {
console.log('service: not installed — run: npx cumora@latest agent computer --install-service')
return
}
@@ -2800,6 +3090,16 @@ async function printStatus(): Promise<void> {
const pid = await execFileP('systemctl', ['--user', 'show', 'cumora', '-p', 'MainPID', '--value']).then((r) => r.stdout.trim()).catch(() => '')
livePid = pid && pid !== '0' ? Number(pid) : null
console.log(`service: installed · ${active}${livePid ? ` (pid ${livePid})` : ''}`)
+ } else if (process.platform === 'win32') {
+ try {
+ const state = JSON.parse(await readFile(RUNNING_STATE_PATH, 'utf8')) as { pid?: number }
+ if (typeof state.pid === 'number' && isStoppableDaemonCommand(await commandLineForPid(state.pid))) {
+ livePid = state.pid
+ }
+ } catch { /* task is between restarts or has not started yet */ }
+ console.log(livePid
+ ? `service: installed · running (pid ${livePid}, Task Scheduler)`
+ : `service: installed · registered in Task Scheduler`)
}
const running = await resolveRunningVersion(livePid)
if (running) {
@@ -2841,8 +3141,8 @@ async function resolveRunningVersion(livePid: number | null): Promise<string> {
} catch { return '' }
}
-/** `--logs`: follow the service's output. macOS tails the log file; Linux
- * streams journald (systemd doesn't write a file). Runs until Ctrl+C. */
+/** `--logs`: follow the service's output. macOS/Windows tail the log file;
+ * Linux streams journald (systemd doesn't write a file). Runs until Ctrl+C. */
async function tailLogs(): Promise<void> {
if (process.platform === 'linux') {
await new Promise<void>((resolve) => {
@@ -2857,7 +3157,12 @@ async function tailLogs(): Promise<void> {
return
}
await new Promise<void>((resolve) => {
- const c = spawn('tail', ['-n', '100', '-f', logPath], { stdio: 'inherit' })
+ const c = process.platform === 'win32'
+ ? spawn('powershell.exe', [
+ '-NoProfile', '-NonInteractive', '-Command',
+ `Get-Content -LiteralPath ${quotePowerShell(logPath)} -Encoding UTF8 -Tail 100 -Wait`,
+ ], { stdio: 'inherit' })
+ : spawn('tail', ['-n', '100', '-f', logPath], { stdio: 'inherit' })
c.on('close', () => resolve()); c.on('error', () => resolve())
})
}
@@ -2948,7 +3253,7 @@ export async function runComputerDaemon(argv: string[]): Promise<void> {
// Re-paired on a machine already managed by the background service: reload
// the service so it adopts the new config (e.g. moved to another company),
// instead of starting a SECOND foreground daemon that races the service.
- if (args.pair && isServiceInstalled()) {
+ if (args.pair && await isServiceInstalled()) {
await reloadService()
console.log(`[computer] re-paired — reloaded the background service with the new config. (No foreground daemon needed; you can close this terminal.)`)
return
diff --git a/src/desktop/MeView.tsx b/src/desktop/MeView.tsx
index 2d5fc30..b63d634 100644
--- a/src/desktop/MeView.tsx
+++ b/src/desktop/MeView.tsx
@@ -10,7 +10,6 @@ import { Checkbox } from '@/components/Checkbox'
import { LanguagePicker } from '@/components/LanguagePicker'
import { useT, type MessageKey } from '@/lib/i18n'
import { cn } from '@/lib/utils'
-import { isWindows } from '@/lib/runtime'
import { api, getPairingServerOrigin, getServerOrigin, type ApiProject, type ApiQuotaSnapshot, type ApiQuotaWindow } from '@/api/client'
// The tab's identity is its `key`; the label is a message key resolved at
@@ -758,8 +757,7 @@ function ComputersTab() {
// explicitly, or the daemon auto-detects and engines[0] silently wins.
const [engine, setEngine] = useState<'claude' | 'codex' | 'grok' | 'cursor'>('claude')
// Default on: install the always-on service (auto-start/restart/update).
- // --install-service is macOS/Linux only (daemon throws on Windows) → off + hidden there.
- const [asService, setAsService] = useState(!isWindows)
+ const [asService, setAsService] = useState(true)
// Per-computer re-pair (reconnect) command, keyed by computer id.
const [repairFor, setRepairFor] = useState<string | null>(null)
const [repairCode, setRepairCode] = useState<string | null>(null)
@@ -915,19 +913,12 @@ function ComputersTab() {
</div>
<span className="text-[11px] text-ink-400">{t('me.engineDefaultHint')}</span>
</div>
- {isWindows ? (
- <div className="mb-2.5 text-[12px] text-ink-600">
- {t('me.keepTerminalOpen')}
- <span className="text-ink-400"> — {t('me.bgServiceUnsupported')}</span>
- </div>
- ) : (
- <label className="flex items-start gap-2 mb-2.5 cursor-pointer select-none">
- <input type="checkbox" checked={asService} onChange={(e) => setAsService(e.target.checked)} className="mt-[3px]" />
- <span className="text-[12px] text-ink-600">
- {t('me.keepInBackground')} <span className="text-ink-400">— {t('me.keepInBackgroundDetail')}</span>
- </span>
- </label>
- )}
+ <label className="flex items-start gap-2 mb-2.5 cursor-pointer select-none">
+ <input type="checkbox" checked={asService} onChange={(e) => setAsService(e.target.checked)} className="mt-[3px]" />
+ <span className="text-[12px] text-ink-600">
+ {t('me.keepInBackground')} <span className="text-ink-400">— {t('me.keepInBackgroundDetail')}</span>
+ </span>
+ </label>
<pre className="bg-ink-900 text-cloud rounded-[10px] p-3 text-[12px] overflow-x-auto whitespace-pre-wrap break-all font-mono select-all">{pairCommand}</pre>
<div className="flex gap-2 mt-3">
<button onClick={copyCommand} aria-live="polite"
diff --git a/src/desktop/Onboarding.tsx b/src/desktop/Onboarding.tsx
index 76556bb..2162831 100644
--- a/src/desktop/Onboarding.tsx
+++ b/src/desktop/Onboarding.tsx
@@ -1,7 +1,6 @@
import { useEffect, useState } from 'react'
import { api, getPairingServerOrigin } from '@/api/client'
import { useComputers } from '@/stores/computers'
-import { isWindows } from '@/lib/runtime'
import { TitleBar } from '@/desktop/TitleBar'
import { useT } from '@/lib/i18n'
@@ -23,12 +22,10 @@ export function Onboarding() {
// append `--engine claude` so a Claude-less machine still auto-detects rather
// than erroring on a Claude it doesn't have.
const [engine, setEngine] = useState<'claude' | 'codex' | 'grok' | 'cursor'>('claude')
- // Default to installing the always-on service: it auto-starts on boot,
+ // Default to installing the always-on service: it auto-starts at sign-in,
// auto-restarts on crash, and auto-updates — so the user isn't tied to a
// terminal that must stay open. Appends `--install-service` to the command.
- // `--install-service` only supports macOS + Linux (the daemon throws on
- // Windows), so default it off there and hide the option entirely.
- const [asService, setAsService] = useState(!isWindows)
+ const [asService, setAsService] = useState(true)
useEffect(() => { void useComputers.getState().refresh() }, [])
useEffect(() => {
@@ -102,19 +99,12 @@ export function Onboarding() {
</div>
<span className="text-[11px] text-ink-400">{t('onboard.engineHint')}</span>
</div>
- {isWindows ? (
- <div className="mb-2.5 text-[12px] text-ink-600">
- {t('onboard.windowsKeep')}
- <span className="text-ink-400"> — {t('onboard.windowsServiceNote')}</span>
- </div>
- ) : (
- <label className="flex items-start gap-2 mb-2.5 cursor-pointer select-none">
- <input type="checkbox" checked={asService} onChange={(e) => setAsService(e.target.checked)} className="mt-[3px]" />
- <span className="text-[12px] text-ink-600">
- {t('onboard.background')} <span className="text-ink-400">{t('onboard.backgroundDetail')}</span>
- </span>
- </label>
- )}
+ <label className="flex items-start gap-2 mb-2.5 cursor-pointer select-none">
+ <input type="checkbox" checked={asService} onChange={(e) => setAsService(e.target.checked)} className="mt-[3px]" />
+ <span className="text-[12px] text-ink-600">
+ {t('onboard.background')} <span className="text-ink-400">{t('onboard.backgroundDetail')}</span>
+ </span>
+ </label>
<pre className="bg-ink-900 text-cloud rounded-[10px] p-3 text-[12px] overflow-x-auto whitespace-pre-wrap break-all font-mono select-all">{cmd}</pre>
<div className="flex items-center gap-3 mt-3">
<button onClick={() => { void navigator.clipboard?.writeText(cmd); setCopied(true) }}
diff --git a/src/locales/en.ts b/src/locales/en.ts
index 4ef6a00..a195d7a 100644
--- a/src/locales/en.ts
+++ b/src/locales/en.ts
@@ -1045,10 +1045,8 @@ export const en = {
'onboard.tokenHint': 'This pairing token stays valid. The computer appears here and you’ll continue automatically once it connects.',
'onboard.engine': 'Engine',
'onboard.engineHint': 'just the default — this computer can still run agents on any detected engine',
- 'onboard.windowsKeep': 'Keep this terminal open while the agents run.',
- 'onboard.windowsServiceNote': 'background service install isn’t supported on Windows yet.',
'onboard.background': 'Keep it running in the background',
- 'onboard.backgroundDetail': '— auto-start on boot, auto-restart on crash, auto-update. Otherwise this terminal has to stay open.',
+ 'onboard.backgroundDetail': '— auto-start when you sign in, auto-restart on crash, auto-update. Otherwise this terminal has to stay open.',
'onboard.copy': 'Copy command',
'onboard.copied': '✓ Copied!',
'onboard.waiting': 'Waiting for your computer to connect…',
@@ -1223,10 +1221,8 @@ export const en = {
'me.copiedShort': '✓ Copied',
'me.runOnHost': 'Run this on the machine you want to host agents:',
'me.engineRequired': 'Needs <span class="font-mono not-italic">claude</span>, <span class="font-mono not-italic">codex</span>, <span class="font-mono not-italic">grok</span>, or <span class="font-mono not-italic">cursor-agent</span> installed. The computer names itself after that machine and appears here once paired. This token stays valid.',
- 'me.keepTerminalOpen': 'Keep this terminal open while the agents run.',
- 'me.bgServiceUnsupported': 'background service install isn’t supported on Windows yet.',
'me.keepInBackground': 'Keep it running in the background',
- 'me.keepInBackgroundDetail': '— auto-start on boot, auto-restart on crash, auto-update. Otherwise this terminal has to stay open.',
+ 'me.keepInBackgroundDetail': '— auto-start when you sign in, auto-restart on crash, auto-update. Otherwise this terminal has to stay open.',
'me.done': 'Done',
'me.addComputer': '+ Add a computer',
'me.updateAvailableTitle': 'Update available',
@@ -1748,4 +1744,4 @@ export const en = {
'download.generic': 'Download Cumora',
'auth.presetProduction': 'Production',
'auth.presetLocalDev': 'Local Dev',
-} as const
\ No newline at end of file
+} as const
diff --git a/src/locales/zh-CN.ts b/src/locales/zh-CN.ts
index 57f01e3..733edf8 100644
--- a/src/locales/zh-CN.ts
+++ b/src/locales/zh-CN.ts
@@ -1046,10 +1046,8 @@ export const zhCN: Partial<Record<keyof typeof en, string>> = {
'onboard.tokenHint': '配对码长期有效。计算机连上后会在这里出现,连接成功会自动继续。',
'onboard.engine': '引擎',
'onboard.engineHint': '只是默认值 —— 这台机器仍然可以在任何检测到的引擎上跑智能体',
- 'onboard.windowsKeep': '智能体运行期间,请保持这个终端窗口打开。',
- 'onboard.windowsServiceNote': 'Windows 上暂不支持后台服务安装。',
'onboard.background': '让它在后台保持运行',
- 'onboard.backgroundDetail': '—— 开机自启、崩溃自恢复、自动更新。否则这个终端就得一直开着。',
+ 'onboard.backgroundDetail': '—— 登录自启、崩溃自恢复、自动更新。否则这个终端就得一直开着。',
'onboard.copy': '复制命令',
'onboard.copied': '✓ 已复制!',
'onboard.waiting': '等待你的计算机连接…',
@@ -1225,10 +1223,8 @@ export const zhCN: Partial<Record<keyof typeof en, string>> = {
'me.copiedShort': '✓ 已复制',
'me.runOnHost': '在想要托管智能体的机器上跑这条命令:',
'me.engineRequired': '需要装好 <span class="font-mono not-italic">claude</span>、<span class="font-mono not-italic">codex</span>、<span class="font-mono not-italic">grok</span> 或 <span class="font-mono not-italic">cursor-agent</span>。计算机会以那台机器命名,配对后出现在这里。这个令牌长期有效。',
- 'me.keepTerminalOpen': '智能体运行期间,请保持这个终端窗口打开。',
- 'me.bgServiceUnsupported': 'Windows 上暂不支持后台服务安装。',
'me.keepInBackground': '让它在后台保持运行',
- 'me.keepInBackgroundDetail': '—— 开机自启、崩溃自恢复、自动更新。否则这个终端就得一直开着。',
+ 'me.keepInBackgroundDetail': '—— 登录自启、崩溃自恢复、自动更新。否则这个终端就得一直开着。',
'me.done': '完成',
'me.addComputer': '+ 添加一台计算机',
'me.updateAvailableTitle': '有可用更新',
@@ -1750,4 +1746,4 @@ export const zhCN: Partial<Record<keyof typeof en, string>> = {
'download.generic': '下载 Cumora',
'auth.presetProduction': '正式环境',
'auth.presetLocalDev': '本地开发',
-} as const
\ No newline at end of file
+} as const