@remcp/remcp 0.2.44 → 0.2.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/cli/service.mjs +63 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remcp/remcp",
3
- "version": "0.2.44",
3
+ "version": "0.2.45",
4
4
  "description": "ReMCP device client: pair a computer with ReMCP and run the outbound-only agent that hosts the local MCP runtime.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -8,7 +8,7 @@ import { spawnSync } from 'node:child_process';
8
8
  import { PACKAGE_NAME, VERSION } from '../version.mjs';
9
9
 
10
10
  import { saveConfig } from './config.mjs';
11
- import { home, linuxServiceFile, macLogFile, macServiceFile, macServiceLabel, npm, windowsTaskName } from './env.mjs';
11
+ import { configDir, home, linuxServiceFile, macLogFile, macServiceFile, macServiceLabel, npm, windowsTaskName } from './env.mjs';
12
12
  import { output, run } from './shell.mjs';
13
13
 
14
14
  export function servicePlatform() {
@@ -49,21 +49,72 @@ export function macLaunchDomain() {
49
49
  return `gui/${process.getuid()}`;
50
50
  }
51
51
 
52
- export function installMacService(cliPath = globalCliPath()) {
53
- const domain = macLaunchDomain();
54
- const target = `${domain}/${macServiceLabel}`;
52
+ function shellQuote(value) {
53
+ return `'${String(value).replaceAll("'", "'\\''")}'`;
54
+ }
55
+
56
+ function macServicePlist(cliPath) {
55
57
  // launchd wants an absolute path; a symlinked prefix that npm has not materialised yet (or a path
56
58
  // that is about to be replaced by the next install) must not abort the repair — a stale plist is
57
59
  // exactly the loop this function exists to break.
58
60
  const cliScript = fs.existsSync(cliPath) ? fs.realpathSync(cliPath) : path.resolve(cliPath);
61
+ return `<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0"><dict>\n<key>Label</key><string>${macServiceLabel}</string>\n<key>ProgramArguments</key><array><string>${xmlEscape(process.execPath)}</string><string>${xmlEscape(cliScript)}</string><string>start</string></array>\n<key>RunAtLoad</key><true/><key>KeepAlive</key><true/>\n<key>ProcessType</key><string>Background</string>\n<key>StandardOutPath</key><string>${xmlEscape(macLogFile)}</string>\n<key>StandardErrorPath</key><string>${xmlEscape(macLogFile)}</string>\n</dict></plist>\n`;
62
+ }
63
+
64
+ function macJobLoaded(target) {
65
+ return spawnSync('launchctl', ['print', target], { stdio: 'ignore' }).status === 0;
66
+ }
67
+
68
+ function scheduleMacServiceReload(domain, target) {
69
+ fs.mkdirSync(configDir, { recursive: true, mode: 0o700 });
70
+ const helperFile = path.join(configDir, `launchd-reload-${process.pid}.sh`);
71
+ const helperLabel = `${macServiceLabel}.reload.${process.pid}`;
72
+ const script = [
73
+ '#!/bin/sh',
74
+ 'sleep 1',
75
+ `launchctl bootout ${shellQuote(target)} >/dev/null 2>&1 || true`,
76
+ `launchctl bootstrap ${shellQuote(domain)} ${shellQuote(macServiceFile)}`,
77
+ `launchctl enable ${shellQuote(target)}`,
78
+ `launchctl kickstart -k ${shellQuote(target)}`,
79
+ `rm -f ${shellQuote(helperFile)}`,
80
+ '',
81
+ ].join('\n');
82
+ fs.writeFileSync(helperFile, script, { mode: 0o700 });
83
+ fs.chmodSync(helperFile, 0o700);
84
+ run('launchctl', ['submit', '-l', helperLabel, '--', '/bin/sh', helperFile]);
85
+ }
86
+
87
+ export function installMacService(cliPath = globalCliPath(), { restart = true } = {}) {
88
+ const domain = macLaunchDomain();
89
+ const target = `${domain}/${macServiceLabel}`;
90
+ const plist = macServicePlist(cliPath);
91
+ let previous = '';
92
+ try { previous = fs.readFileSync(macServiceFile, 'utf8'); } catch {}
93
+ const loaded = macJobLoaded(target);
94
+
59
95
  fs.mkdirSync(path.dirname(macServiceFile), { recursive: true });
60
96
  fs.mkdirSync(path.dirname(macLogFile), { recursive: true });
61
- const plist = `<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0"><dict>\n<key>Label</key><string>${macServiceLabel}</string>\n<key>ProgramArguments</key><array><string>${xmlEscape(process.execPath)}</string><string>${xmlEscape(cliScript)}</string><string>start</string></array>\n<key>RunAtLoad</key><true/><key>KeepAlive</key><true/>\n<key>ProcessType</key><string>Background</string>\n<key>StandardOutPath</key><string>${xmlEscape(macLogFile)}</string>\n<key>StandardErrorPath</key><string>${xmlEscape(macLogFile)}</string>\n</dict></plist>\n`;
62
97
  fs.writeFileSync(macServiceFile, plist, { mode: 0o600 });
63
- spawnSync('launchctl', ['bootout', domain, macServiceFile], { stdio: 'ignore' });
98
+
99
+ if (loaded && previous === plist) {
100
+ run('launchctl', ['enable', target]);
101
+ // Never boot out a healthy loaded job just to refresh an in-place npm install. The final
102
+ // kickstart keeps launchd responsible for bringing the replacement agent back.
103
+ if (restart) run('launchctl', ['kickstart', '-k', target]);
104
+ return 'loaded';
105
+ }
106
+
107
+ if (loaded) {
108
+ // A changed Node/npm prefix requires launchd to re-read ProgramArguments. A separate transient
109
+ // launchd job survives booting out com.remcp.agent even when the updater was launched by it.
110
+ scheduleMacServiceReload(domain, target);
111
+ return 'reload-scheduled';
112
+ }
113
+
64
114
  run('launchctl', ['bootstrap', domain, macServiceFile]);
65
115
  run('launchctl', ['enable', target]);
66
- run('launchctl', ['kickstart', '-k', target]);
116
+ if (restart) run('launchctl', ['kickstart', '-k', target]);
117
+ return 'bootstrapped';
67
118
  }
68
119
 
69
120
  export function installWindowsService(cliPath = globalCliPath()) {
@@ -179,11 +230,11 @@ export function ensureServiceIfRecorded(config) {
179
230
  }
180
231
  }
181
232
  } else if (platform === 'darwin') {
182
- // launchd bakes the interpreter and the CLI path into the plist, so a Node manager that moves
183
- // its global prefix leaves the agent launching a file that no longer exists the same loop the
184
- // Linux unit above is repaired for. Reinstalling is idempotent (bootout, bootstrap, enable,
185
- // kickstart) and is what the Windows task already does on every update.
186
- installMacService(cliPath);
233
+ // launchd bakes the interpreter and CLI path into the plist. Repair the file first, but never
234
+ // boot out a loaded agent inline: this updater may itself be a descendant of that LaunchAgent.
235
+ // installMacService either leaves an unchanged loaded job alone, bootstraps an unloaded job, or
236
+ // hands a changed launcher to an independent transient launchd helper.
237
+ installMacService(cliPath, { restart: false });
187
238
  } else if (platform === 'win32') installWindowsService(cliPath);
188
239
  // Upgrade the legacy inferred state only after the supervisor repair succeeded. A failed repair
189
240
  // must not turn a stale artifact into a permanent "managed service" declaration.