Repository navigation
service: on Windows the control daemon runs under a scheduled task at logon - #317
Closed
connorburgess wants to merge 1 commit into
Closed
connorburgess wants to merge 1 commit into
connorburgess wants to merge 1 commit into
Conversation
… 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>
Collaborator
|
Superseded by #378 (merged into |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 onandask oninstall it where they install the other two,control offremoves it, andsessionpipe updatemoves it onto the copy you type. Closes #184.The console question from the issue. Yes,
node.exestarted from a logon task opens a window. On Windows 11 (26300) a task whose action isnode.exe <script>put a visible Windows Terminal window titledC:\tools\node\node.exeon the desktop. The same script underconhost.exe --headlessopened none. So the task runs Node underconhost.exe --headless, which is whatsessionpipe-gui service installalready 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 namedwö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 makeshealth.copy.vianull, anddoctorandupdateread null as "someone started it by hand". Instead the launcher setsSESSIONPIPE_SERVICE=schtasks:<its pid>andrestartVia()answersschtasksonly for the process whose parent has that pid, the waysupervisor:<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 /Endends 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 /Queryanswers in the console's code page and the system's language. Where PowerShell can't be asked,serviceStateanswersmanager: nulland the launcher file is read all the same.In the CLI.
control pairsaysscheduled task \sessionpipe\org.sessionpipe.control: it starts when you log on to Windows, with no window (log: …).statusanddoctorno longer say updates on Windows run only "whilesessionpipe control runis running" (cli: on Windows, status, doctor, secrets and connect say what the machine does #183 added that line because there was no service).doctorandupdatesay "Task Scheduler" where they name the manager.updatewrites 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.SESSIONPIPE_SERVICE_LABEL) now gives itAPPDATAandLOCALAPPDATA. 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 realcontrol.json.Left alone on purpose.
sessionpipe reinstallstill does not run on Windows.sessionpipe-guikeeps its own Windows task code. Moving it onto core's is a separate change in that repository.NO_MANAGERfor a platform with no manager are unchanged.Checklist
git commit -s, the DCO)spec/says nothing about service managers).changeset/windows-scheduled-task.md)Proof
On Windows 11 (26300, Node 20.20.2), a user without elevation
The build of this branch, installed from
npm packinto a prefix of its own, in a world of its own (SESSIONPIPE_SERVICE_LABEL=org.sessionpipe.t184, a profile folder namedwörld & co).ask oninstalls the service with no receiver needed.The control, a task that runs
node.exedirectly, and then this branch's task. "New windows" lists the visible top-level windows that were not there before the task ran (EnumWindows), aspid|class|title. The process lines arepid <- parent name :: command line, and are printed by PowerShell, whose console showsöas�.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:A restart onto a newer copy on disk (the installed
cli.jspatched to say 0.18.3, then{"op":"restart"}on the socket, which is whatsessionpipe updatesends). Same launcher (7316), a new daemon 0.4 s later:A daemon killed with
taskkill /Fis started again by the same launcher after its 5 s wait:schtasks /Endby hand leaves the launcher and the daemon behind with the taskReady;ask onthen stops those two and runs the task again, and leaves one of each:ask offremoves the task, what it started and the launcher. The one task left in the folder issessionpipe-gui's own: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, boot11/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 wordspairandupdatesay,restartViaon Windows, and the mid-turn seed before a replace.Tests that used
win32as "the platform with no manager" now usefreebsd.Gates
On this branch (a7237c9), macOS, Node 22:
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 realgeminion its PATH, and not this branch. CI will say.npm run test:coveragedid 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.tsis new and at 100 %. The Ubuntu Node 22 leg is the one that decides.