Skip to content

service: on Windows the control daemon runs under a scheduled task at logon - #317

Closed
connorburgess wants to merge 1 commit into
spacesheep-dev:mainfrom
connorburgess:feat/windows-service
Closed

connorburgess wants to merge 1 commit into
spacesheep-dev:mainfrom
connorburgess:feat/windows-service

Conversation

@connorburgess

Copy link
Copy Markdown
Contributor

What

On Windows the control daemon now has a service manager: a per-user scheduled task at logon, \sessionpipe\org.sessionpipe.control, as a third manager in core's service module beside launchd and systemd. control pair, connect, pool on and ask on install it where they install the other two, control off removes it, and sessionpipe update moves it onto the copy you type. Closes #184.

The console question from the issue. Yes, node.exe started from a logon task opens a window. On Windows 11 (26300) a task whose action is node.exe <script> put a visible Windows Terminal window titled C:\tools\node\node.exe on the desktop. The same script under conhost.exe --headless opened none. So the task runs Node under conhost.exe --headless, which is what sessionpipe-gui service install already does on Windows.

Why there is a launcher file. Task Scheduler gives an action no environment, keeps none of its output, and restarts it on no exit code. The daemon needs all three: the PATH of the shell that paired, a daemon.log, and a restart on exit 75. The task therefore runs a small Node script, %LOCALAPPDATA%\sessionpipe\service\<label>.cjs, that starts the daemon with the environment it was given, appends its output to the log, starts it again at once on 75 and after 5 s on a failure, and ends with it on 0. That is what the plist and the unit say declaratively.

What the launcher runs is one line of JSON in that file. No path, argument or variable is read by cmd.exe, so &, ^, !, spaces and letters outside the code page need no escaping (the VM run below uses a profile folder named wörld & co). Only the two paths on the task's own command line (Node and the launcher) pass through conhost. One with a " or a % in it can't be carried: pairing says so and installs nothing.

The daemon knows who restarts it. The issue proposed leaving restartVia() as it is, so the daemon would supervise itself after its first update. That makes health.copy.via null, and doctor and update read null as "someone started it by hand". Instead the launcher sets SESSIONPIPE_SERVICE=schtasks:<its pid> and restartVia() answers schtasks only for the process whose parent has that pid, the way supervisor:<pid> is checked. A session the daemon starts inherits the variable and has another parent, so it never counts. The daemon then exits 75 and the launcher starts the new copy, as under launchd and systemd.

Stopping. schtasks /End ends conhost alone; the launcher and the daemon stay. Replacing or removing the task therefore also stops what it started, each with its tree (taskkill /PID <pid> /T /F). The processes are found by command line, never by a pid written down earlier, since Windows reuses pids: the headless conhost whose command line ends with the launcher, and the launcher itself (the Node the launcher file names, run on that file). An editor with the launcher open is neither.

Reading Task Scheduler goes through one PowerShell script (cmdlets only and no .NET method calls, which constrained language mode refuses; I have not run it under that mode) that prints the task's state as a number and writes the process list to a file as UTF-8. schtasks /Query answers in the console's code page and the system's language. Where PowerShell can't be asked, serviceState answers manager: null and the launcher file is read all the same.

In the CLI.

  • control pair says scheduled task \sessionpipe\org.sessionpipe.control: it starts when you log on to Windows, with no window (log: …).
  • status and doctor no longer say updates on Windows run only "while sessionpipe control run is running" (cli: on Windows, status, doctor, secrets and connect say what the machine does #183 added that line because there was no service).
  • doctor and update say "Task Scheduler" where they name the manager.
  • update writes down who is mid-turn before it replaces a service on Windows, as it does for a daemon from before 0.15. The daemon writes that itself when it is asked to stop, and nothing on Windows asks: replacing the task ends its processes outright. An update of the copy the daemon already runs is not affected: it exits 75 by itself, after its own stop.
  • A sessionpipe with a service of its own (SESSIONPIPE_SERVICE_LABEL) now gives it APPDATA and LOCALAPPDATA. On Windows the state and config are under those two, and a task gets the account's, not the shell's. Without them my first VM run brought the test world's daemon up on the account's real control.json.

Left alone on purpose.

  • sessionpipe reinstall still does not run on Windows.
  • The task starts at logon and ends at logoff. Running with nobody logged on needs "Log on as a batch job", which a standard user doesn't have.
  • sessionpipe-gui keeps its own Windows task code. Moving it onto core's is a separate change in that repository.
  • The words of NO_MANAGER for a platform with no manager are unchanged.

Checklist

  • Every commit is signed off (git commit -s, the DCO)
  • Spec and fixture updated together — this PR changes neither (spec/ says nothing about service managers)
  • No telemetry added: nothing reports to anyone but the sinks the person configured
  • A changeset is included (.changeset/windows-scheduled-task.md)
  • The definition of done was checked with a command, and its output is below

Proof

On Windows 11 (26300, Node 20.20.2), a user without elevation

The build of this branch, installed from npm pack into a prefix of its own, in a world of its own (SESSIONPIPE_SERVICE_LABEL=org.sessionpipe.t184, a profile folder named wörld & co). ask on installs the service with no receiver needed.

The control, a task that runs node.exe directly, and then this branch's task. "New windows" lists the visible top-level windows that were not there before the task ran (EnumWindows), as pid|class|title. The process lines are pid <- parent name :: command line, and are printed by PowerShell, whose console shows ö as �.

== A: node.exe directly (the control)
SUCCESS: Attempted to run the scheduled task "\sessionpipe\sp184-a".
-- new windows:
3132|PseudoConsoleWindow|
9600|CASCADIA_HOSTING_WINDOW_CLASS|C:\tools\node\node.exe
2132 <- 1572 node.exe :: "C:\tools\node\node.exe" "C:\Users\connor\sp184\w�rld one\fake.js" control run
== ask on
      Background service: scheduled task \sessionpipe\org.sessionpipe.t184: it starts when you log on to Windows, with no window (log: C:\Users\connor\sp184\wörld & co\AppData\Local\sessionpipe\control\daemon.log)
    (exit 0)
-- new windows:
    1316 <- 1572 conhost.exe :: "conhost.exe" --headless C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    7316 <- 1316 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    3784 <- 7316 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\npm\node_modules\sessionpipe\dist\cli.js" control run
    task state=4 Running

No line follows -- new windows: in the second run: the three lines after it are the processes.

The daemon's own word on who restarts it, and doctor:

== the daemon's own word on who restarts it (health.copy)
    {"op":"health","health":{"accounts":[{"dir":"C:\\Users\\connor\\sp184\\wörld & co\\.claude","signed_in":null}],"copy":{"cli":"C:\\Users\\connor\\sp184\\wörld & co\\npm\\node_modules\\sessionpipe\\dist\\cli.js","via":"schtasks"}}}
== doctor
    ✓ All good
      ✓ background service  running

A restart onto a newer copy on disk (the installed cli.js patched to say 0.18.3, then {"op":"restart"} on the socket, which is what sessionpipe update sends). Same launcher (7316), a new daemon 0.4 s later:

    patched cli.js
    on disk now: 0.18.3
    {"op":"ok"}
    1316 <- 1572 conhost.exe :: "conhost.exe" --headless C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    7316 <- 1316 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    2464 <- 7316 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\npm\node_modules\sessionpipe\dist\cli.js" control run
    task state=4 Running
== daemon.log
    2026-11-22T00:58:45.833Z control daemon 0.18.2 up � pid 3784 � 0 receiver(s) � socket \\.\pipe\sessionpipe-control-t184
    2026-11-22T00:58:51.501Z restart: 0.18.2  0.18.3 (installed by hand)
    2026-11-22T00:58:51.508Z exiting 75 so the scheduled task's launcher starts 0.18.3
    2026-11-22T00:58:51.924Z control daemon 0.18.3 up � pid 2464 � 0 receiver(s) � socket \\.\pipe\sessionpipe-control-t184

A daemon killed with taskkill /F is started again by the same launcher after its 5 s wait:

== kill the daemon: the launcher starts it again after 5 s
    launcher 4148 daemon 6360
    SUCCESS: The process with PID 6360 has been terminated.
    4148 <- 9924 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    6016 <- 4148 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\npm\node_modules\sessionpipe\dist\cli.js" control run
    task state=4 Running

schtasks /End by hand leaves the launcher and the daemon behind with the task Ready; ask on then stops those two and runs the task again, and leaves one of each:

== schtasks /End by hand, then status and ask on
    SUCCESS: The scheduled task "\sessionpipe\org.sessionpipe.t184" has been terminated successfully.
    5568 <- 832 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    5108 <- 5568 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\npm\node_modules\sessionpipe\dist\cli.js" control run
    task state=3 Ready
      Background service: scheduled task \sessionpipe\org.sessionpipe.t184: it starts when you log on to Windows, with no window (log: C:\Users\connor\sp184\wörld & co\AppData\Local\sessionpipe\control\daemon.log)
    (exit 0)
    9764 <- 1572 conhost.exe :: "conhost.exe" --headless C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    6972 <- 9764 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\AppData\Local\sessionpipe\service\org.sessionpipe.t184.cjs"
    8316 <- 6972 node.exe :: C:\tools\node\node.exe "C:\Users\connor\sp184\w�rld & co\npm\node_modules\sessionpipe\dist\cli.js" control run
    task state=4 Running

ask off removes the task, what it started and the launcher. The one task left in the folder is sessionpipe-gui's own:

== ask off
      Background service: removed; with no receiver paired, nothing was left for it to do.
    (exit 0)
-- processes:
    no task
-- launcher dir:
-- folder tasks left:
    dev.spacesheep.sessionpipe-gui Running

Not run: a logoff and logon. The VM has no automatic logon, so logging off would have left it at the sign-in screen. The trigger and principal in the task definition are the ones sessionpipe-gui's task uses, and on this VM that task's last run is the second of the last interactive logon (gui task last run: 11/21/2026 02:56:55, interactive logon: 11/21/2026 02:56:55, boot 11/20/2026 11:47:34).

Tests

packages/core/test/service-windows.test.ts (new): the launcher and the task definition as whole text; the launcher run for real under Node on every CI leg (environment, appended output, a restart on 75 and after a failure, an end on 0, a log that can't be opened, a program that can't be started); which processes are a task's among a process list; and install, replace, refusal, look and removal with schtasks, taskkill and PowerShell stood in. The CLI's tests cover the words pair and update say, restartVia on Windows, and the mid-turn seed before a replace.

Tests that used win32 as "the platform with no manager" now use freebsd.

Gates

On this branch (a7237c9), macOS, Node 22:

$ npm run lint                 # exit 0
Checked 362 files in 214ms. No fixes applied.
Found 100 warnings.            # 100 on main as well; the one in a changed file (cli.ts:27, unused imports) is not from this change
$ npm run typecheck            # exit 0
$ npm run build                # exit 0
$ npm run schemas -- --check   # exit 0
schemas/v1 and packages/core/gen in sync with proto/ (19 files)
$ npm test                     # exit 1
 Test Files  1 failed | 154 passed | 1 skipped (156)
      Tests  1 failed | 4324 passed | 2 skipped (4327)

The one failure is packages/core/test/install-layout.test.ts > … > gemini-cli: the same values after every step, and the file's own bytes at the end, which times out at 20 s on my Mac. It does the same there on a checkout of main without this change (1b755ff: Error: Test timed out in 20000ms., Tests 1 failed | 90 skipped (91)), so I take it to be this machine, which has a real gemini on its PATH, and not this branch. CI will say.

npm run test:coverage did not finish clean on that Mac for the same reason, and a failed test fails the thresholds with it. With the report forced (--coverage.reportOnFailure), the files this PR touches are at 100 % of lines, branches, functions and statements; packages/core/src/service/windows.ts is new and at 100 %. The Ubuntu Node 22 leg is the one that decides.

… logon

Symptom: on Windows `control pair`, `connect`, `pool on` and `ask on` ended
with "no service manager for this platform yet; run `sessionpipe control run`
yourself", so the daemon lived as long as the terminal it was started in (spacesheep-dev#184).

Core's service module gains a third manager beside launchd and systemd: a
per-user scheduled task, \sessionpipe\<label>, registered from a definition
with a logon trigger that names the user, which needs no administrator.

- No window: a task that runs node.exe directly opens a terminal window at
  every logon (measured on Windows 11), so the task runs it under
  `conhost.exe --headless`.
- Task Scheduler gives an action no environment, keeps none of its output and
  restarts it on no exit code. The task runs a small launcher script
  (%LOCALAPPDATA%\sessionpipe\service\<label>.cjs) that starts the service
  with the environment it was given, appends its output to the log, starts it
  again at once on 75 and after 5 s on a failure, and ends with it on 0. What
  it runs is one line of JSON, so nothing is read by cmd.exe; only the node
  and the launcher path are on the task's command line, and one with a quote
  or a % is refused with why.
- The launcher sets SESSIONPIPE_SERVICE=schtasks:<its pid>, and restartVia
  answers "schtasks" only for the process whose parent that is, as it does for
  the supervisor. The daemon exits 75 and the launcher starts the new copy.
- `schtasks /End` ends conhost alone, so replacing or removing the task also
  stops the launcher and what it runs, found by command line and never by a
  pid written down earlier.
- Task Scheduler and the process list are read through one PowerShell script
  (cmdlets only), which prints a number and writes text to a file as UTF-8.
- pair, update and doctor name the task and Task Scheduler; status no longer
  says updates on Windows run only while `control run` is running; update
  writes down who is mid-turn before it replaces the task, which ends its
  processes outright.
- A service of a sessionpipe's own (SESSIONPIPE_SERVICE_LABEL) is given
  APPDATA and LOCALAPPDATA too: on Windows its files are under those, and
  without them it came up on the account's real ones.

Tests that used win32 as the platform with no manager use freebsd.

Signed-off-by: connorburgess <connorburgesscodes@gmail.com>
@kombuchapunk

Copy link
Copy Markdown
Collaborator

Superseded by #378 (merged into main, preserving @connorburgess's original commit & DCO attribution with protobuf schema rebase + security/rollback/ReDoS hardening). Thank you @connorburgess!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: nothing keeps the control daemon running (a per-user scheduled task beside launchd and systemd?)

2 participants