aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-client/src
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-09-04 03:58:16 +0200
committerChristophe Besson <cbesson@gmail.com>2026-09-04 03:58:16 +0200
commit220e6e701806213a576ce80fa655cd9cf4a51880 (patch)
tree04b1f059044f4e02d1e8cb6c4a6588982cf395c4 /packages/meshbay-client/src
parent5098e6cb54173b27673f5761ce187d799ba36b30 (diff)
downloadmeshbay-220e6e701806213a576ce80fa655cd9cf4a51880.tar.gz
feat: Windows daemon lifecycle (W3) — Startup-folder autostart
The Linux node runs under `systemctl --user`. Windows has no per-user equivalent that works without elevation: `schtasks /create /sc ONLOGON` (even `/rl LIMITED /it`) fails with "Access is denied" for a non-admin user, because a logon trigger touches machine-wide scheduler state. So autostart is a `.vbs` in the per-user Startup folder instead: CreateObject("WScript.Shell").Run Chr(34) & "<exe>" & Chr(34), 0, False wscript runs it at every sign-in, hidden (0) and non-blocking. No admin, no console window, no new dependency. Verified end to end: the launcher brings the daemon up with no window and it answers its loopback API. node/platform.py autostart_install/remove/status — write / delete / detect the launcher autostart_run/end — start now (DETACHED|NO_WINDOW) / taskkill _node_exe — PATH, then next to sys.executable, then argv[0] node/daemon.py new `autostart install|remove|start|stop|status` verb reload (win32) -> POST /api/reload on the loopback API restart-daemon (win32) -> autostart_end + autostart_run reset (win32) -> also removes the launcher client/main.js, preload.js node:autostart handler + winAutostart* helpers (kept in step with platform.py) node:service-status (win32) probes the daemon; stop/restart/start use taskkill + a detached, windowless spawn Tests: 8 autostart cases in test_platform.py (mocked sys.platform, APPDATA pointed at tmp); `autostart status` added to the CLI dispatch sweep. Full meshbay-node suite green on Windows (784 passed / 34 skipped). Still open: no CTRL_CLOSE_EVENT handler, so a bare taskkill / window close does not run _shutdown() (SetConsoleCtrlHandler, follow-up). Service mode (pywin32/NSSM) stays Phase 2. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-client/src')
-rw-r--r--packages/meshbay-client/src/main.js106
-rw-r--r--packages/meshbay-client/src/preload.js10
2 files changed, 113 insertions, 3 deletions
diff --git a/packages/meshbay-client/src/main.js b/packages/meshbay-client/src/main.js
index 9c27cfe..d341619 100644
--- a/packages/meshbay-client/src/main.js
+++ b/packages/meshbay-client/src/main.js
@@ -804,7 +804,59 @@ function registerBridge() {
});
}
+ // ── Windows: the Startup-folder launcher that stands in for the systemd unit ──
+ // A logon-triggered Task Scheduler task needs elevation to create, which an
+ // ordinary user does not have, so autostart is a `.vbs` in the per-user
+ // Startup folder instead: wscript runs it hidden at every sign-in — no admin,
+ // no console window. Kept in step with meshbay_node.platform._startup_vbs().
+ const WIN_STARTUP_VBS = path.join(
+ app.getPath('appData'), 'Microsoft', 'Windows', 'Start Menu', 'Programs',
+ 'Startup', 'MeshBay Node.vbs');
+
+ function winAutostartInstalled() {
+ try { return fs.existsSync(WIN_STARTUP_VBS); } catch { return false; }
+ }
+
+ function winAutostartInstall(bin) {
+ fs.mkdirSync(path.dirname(WIN_STARTUP_VBS), { recursive: true });
+ // Chr(34) is a literal " — wraps the path so a space in it doesn't split
+ // the command. 0 = hidden window, False = don't wait. Kept in step with
+ // meshbay_node.platform.autostart_install().
+ fs.writeFileSync(WIN_STARTUP_VBS,
+ `CreateObject("WScript.Shell").Run Chr(34) & "${bin}" & Chr(34), 0, False\r\n`);
+ }
+
+ function winAutostartRemove() {
+ try { fs.rmSync(WIN_STARTUP_VBS, { force: true }); } catch { /* not there */ }
+ }
+
+ function killNodeProcesses() {
+ return new Promise((resolve) => {
+ execFile('taskkill', ['/IM', 'meshbay-node.exe', '/F'], () => resolve());
+ });
+ }
+
+ async function spawnNodeDetached() {
+ const bin = await findNodeBinary();
+ if (!bin) throw new Error('meshbay-node not found on PATH');
+ const child = spawn(bin, [], { detached: true, stdio: 'ignore', windowsHide: true });
+ child.unref();
+ }
+
+ async function waitForNode(deadline) {
+ while (Date.now() < deadline) {
+ const p = await probeNode();
+ if (p) return p;
+ await new Promise((r) => setTimeout(r, 800));
+ }
+ return null;
+ }
+
ipcMain.handle('node:installed', async () => {
+ if (process.platform === 'win32') {
+ const bin = await findNodeBinary();
+ return { installed: Boolean(bin), autostart: winAutostartInstalled() };
+ }
if (process.platform !== 'linux') return { installed: false };
const unit = await new Promise((resolve) => {
execFile('systemctl', ['--user', 'show', 'meshbay-node.service',
@@ -823,6 +875,16 @@ function registerBridge() {
// own HTTP API, which cannot answer while the daemon is stopped or crash-
// looping — exactly the states this panel exists to show and act on.
ipcMain.handle('node:service-status', async () => {
+ if (process.platform === 'win32') {
+ // No Task Scheduler to ask "is it running" — probe the daemon itself.
+ const p = await probeNode();
+ return {
+ supported: true,
+ installed: winAutostartInstalled(),
+ activeState: p ? 'active' : 'inactive',
+ subState: p ? 'running' : '',
+ };
+ }
if (process.platform !== 'linux') return { supported: false };
return new Promise((resolve) => {
execFile('systemctl', ['--user', 'show', 'meshbay-node.service',
@@ -848,6 +910,10 @@ function registerBridge() {
});
ipcMain.handle('node:service-stop', async () => {
+ if (process.platform === 'win32') {
+ await killNodeProcesses(); // hard kill — no CTRL_CLOSE handler yet
+ return { stopped: true };
+ }
if (process.platform !== 'linux') {
throw new Error('Service control is only supported on Linux');
}
@@ -862,6 +928,13 @@ function registerBridge() {
});
ipcMain.handle('node:service-restart', async () => {
+ if (process.platform === 'win32') {
+ await killNodeProcesses();
+ await spawnNodeDetached();
+ const p = await waitForNode(Date.now() + 30000);
+ if (!p) throw new Error('node did not come back up within 30s');
+ return { restarted: true, ...p };
+ }
if (process.platform !== 'linux') {
throw new Error('Service control is only supported on Linux');
}
@@ -875,6 +948,22 @@ function registerBridge() {
return { restarted: true };
});
+ // Install / remove the Windows Startup-folder launcher, and query it.
+ ipcMain.handle('node:autostart', async (_e, action) => {
+ if (process.platform !== 'win32') return { supported: false };
+ if (action === 'install') {
+ const bin = await findNodeBinary();
+ if (!bin) throw new Error('meshbay-node not found on PATH');
+ winAutostartInstall(bin);
+ return { supported: true, installed: true };
+ }
+ if (action === 'remove') {
+ winAutostartRemove();
+ return { supported: true, installed: false };
+ }
+ return { supported: true, installed: winAutostartInstalled() };
+ });
+
async function probeNode() {
const nc = readNodeConfig();
const dataDir = nc ? nc.dataDir : meshbayDataDir();
@@ -895,6 +984,11 @@ function registerBridge() {
} catch { return null; }
}
+ // A path inside a TOML basic string: forward slashes only. A raw Windows
+ // path there (`C:\Users\...`) is a parse error — `\U`, `\a`, ... are escape
+ // sequences. pathlib on the node reads the `/` form fine.
+ const tomlPath = (p) => p.split(path.sep).join('/');
+
function provisionNode(hubUrl, username) {
const configDir = meshbayConfigDir();
const dataDir = meshbayDataDir();
@@ -920,7 +1014,7 @@ function registerBridge() {
'ui_port = 18000',
'',
'[keystore]',
- `unlock_file = "${path.join(configDir, 'unlock.key')}"`,
+ `unlock_file = "${tomlPath(path.join(configDir, 'unlock.key'))}"`,
'',
].join('\n');
fs.writeFileSync(configFile, toml, { mode: 0o600 });
@@ -939,6 +1033,16 @@ function registerBridge() {
return { started: true, ...already };
}
+ if (process.platform === 'win32') {
+ if (opts && opts.hubUrl && opts.username) provisionNode(opts.hubUrl, opts.username);
+ await killNodeProcesses(); // clear a crash-looping one
+ await spawnNodeDetached();
+ const p = await waitForNode(Date.now() + 60000);
+ if (!p) throw new Error('the node did not start within 60s — run it from a '
+ + 'terminal (`meshbay-node`) to see why');
+ return { started: true, ...p };
+ }
+
if (process.platform !== 'linux') {
throw new Error('Automatic node start is only supported on Linux');
}
diff --git a/packages/meshbay-client/src/preload.js b/packages/meshbay-client/src/preload.js
index 96d6790..38c1a57 100644
--- a/packages/meshbay-client/src/preload.js
+++ b/packages/meshbay-client/src/preload.js
@@ -97,13 +97,19 @@ contextBridge.exposeInMainWorld('meshbay', {
call: (method, path, body) => ipcRenderer.invoke('node:call', method, path, body),
pairingCode: () => ipcRenderer.invoke('node:pairing-code'),
setPairingCode: (code) => ipcRenderer.invoke('node:set-pairing-code', code),
- // The systemd unit's own state — reachable even while the daemon itself
- // is stopped or crash-looping, which `call()` above is not.
+ // The daemon's lifecycle as seen from outside it: the systemd unit (Linux)
+ // or, on Windows, a probe of the daemon plus whether the Startup launcher
+ // is in place — reachable even while the daemon itself is stopped or
+ // crash-looping, which `call()` above is not.
service: {
status: () => ipcRenderer.invoke('node:service-status'),
stop: () => ipcRenderer.invoke('node:service-stop'),
restart: () => ipcRenderer.invoke('node:service-restart'),
},
+ // Windows only: the "run at every sign-in" Startup-folder launcher.
+ // action: 'install' | 'remove' | 'status' (default). Elsewhere returns
+ // { supported: false }.
+ autostart: (action) => ipcRenderer.invoke('node:autostart', action),
},
// LAN cast relay. The main process runs a local HTTP server and the