@50arms/platform-mcp 0.1.502 → 0.1.504

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.
@@ -9,11 +9,14 @@
9
9
  * logon trigger runs only after a logon, never after an install mid-session.
10
10
  * Linux: a systemd user unit with `Restart=on-failure`. DX-6537: an install restarts it, because `enable --now` leaves a running
11
11
  * unit on its old bundle.
12
+ * macOS: DX-8028: a per-user launchd LaunchAgent in the `gui/<uid>` domain, started when it loads (`RunAtLoad`, so at login and
13
+ * at install) and restarted after a non-zero exit (`KeepAlive` `SuccessfulExit` false), as systemd's `on-failure` does. An
14
+ * install unloads any agent already loaded and loads the new plist, so a running daemon moves onto the bundle just copied.
12
15
  *
13
16
  * A daemon whose bearer the dashboard refused stops on purpose and disables its entry (`disableAutostart`), so neither the
14
17
  * repeating trigger nor the next logon starts it into the same refusal; `install-daemon` enables it again.
15
18
  *
16
- * Every child process is started with `windowsHide: true`. The XML and unit text are pure functions so the tests pin them.
19
+ * Every child process is started with `windowsHide: true`. The XML, unit and plist text are pure functions so the tests pin them.
17
20
  * The task XML is UTF-16: `schtasks /XML` reads no other encoding (a UTF-8 file is refused "unable to switch the encoding").
18
21
  */
19
22
  import { execFileSync } from "node:child_process";
@@ -22,6 +25,7 @@ import { homedir } from "node:os";
22
25
  import { join } from "node:path";
23
26
  export const TASK_NAME = "danxbot-machine-daemon";
24
27
  export const UNIT_NAME = "danxbot-machine-daemon.service";
28
+ export const LAUNCH_AGENT_LABEL = "ai.50arms.danxbot-machine-daemon";
25
29
  function xmlEscape(value) {
26
30
  return value
27
31
  .replace(/&/g, "&amp;")
@@ -77,6 +81,47 @@ RestartSec=10
77
81
  WantedBy=default.target
78
82
  `;
79
83
  }
84
+ /** The launchd LaunchAgent: started when loaded (login, install), restarted only after a non-zero exit (a refused bearer exits 0). */
85
+ export function buildLaunchAgentPlist(args) {
86
+ return `<?xml version="1.0" encoding="UTF-8"?>
87
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
88
+ <plist version="1.0">
89
+ <dict>
90
+ <key>Label</key>
91
+ <string>${LAUNCH_AGENT_LABEL}</string>
92
+ <key>ProgramArguments</key>
93
+ <array>
94
+ <string>${xmlEscape(args.nodePath)}</string>
95
+ <string>${xmlEscape(args.bundlePath)}</string>
96
+ </array>
97
+ <key>RunAtLoad</key>
98
+ <true/>
99
+ <key>KeepAlive</key>
100
+ <dict>
101
+ <key>SuccessfulExit</key>
102
+ <false/>
103
+ </dict>
104
+ <key>ThrottleInterval</key>
105
+ <integer>10</integer>
106
+ <key>ProcessType</key>
107
+ <string>Background</string>
108
+ </dict>
109
+ </plist>
110
+ `;
111
+ }
112
+ function launchAgentPlistPath(home) {
113
+ return join(home, "Library", "LaunchAgents", `${LAUNCH_AGENT_LABEL}.plist`);
114
+ }
115
+ /** The launchd domain of this user's login session; a macOS entry cannot be loaded without the user's numeric id. */
116
+ function launchdDomain(uid) {
117
+ if (uid === null)
118
+ throw new Error("cannot load the launch agent: the current user's numeric id is unknown");
119
+ return `gui/${uid}`;
120
+ }
121
+ /** This process's numeric user id; null where the platform has none (Windows). */
122
+ export function currentUid() {
123
+ return process.getuid?.() ?? null;
124
+ }
80
125
  /** Runs an OS command with no window and returns its stdout; a non-zero exit throws. */
81
126
  export function runHidden(file, args) {
82
127
  return execFileSync(file, args, {
@@ -96,6 +141,7 @@ export function productionAutostartDeps(bundlePath) {
96
141
  userId: process.env.USERDOMAIN && process.env.USERNAME
97
142
  ? `${process.env.USERDOMAIN}\\${process.env.USERNAME}`
98
143
  : (process.env.USER ?? ""),
144
+ uid: currentUid(),
99
145
  conhostPath: join(process.env.SystemRoot ?? "C:\\Windows", "System32", "conhost.exe"),
100
146
  home: homedir(),
101
147
  run: runHidden,
@@ -127,7 +173,26 @@ export function installAutostart(deps) {
127
173
  deps.run("systemctl", ["--user", "restart", UNIT_NAME]);
128
174
  return `systemd user unit ${UNIT_NAME} (enabled and started)`;
129
175
  }
130
- throw new Error(`no start-at-logon entry for platform ${deps.platform}: the machine daemon runs on Windows and Linux only`);
176
+ if (deps.platform === "darwin") {
177
+ const domain = launchdDomain(deps.uid);
178
+ const target = `${domain}/${LAUNCH_AGENT_LABEL}`;
179
+ const plistPath = launchAgentPlistPath(deps.home);
180
+ deps.mkdir(join(deps.home, "Library", "LaunchAgents"));
181
+ deps.writeFile(plistPath, buildLaunchAgentPlist(deps), "utf8");
182
+ // DX-8028: a refused bearer left the agent disabled, and launchd refuses to bootstrap a disabled service.
183
+ deps.run("launchctl", ["enable", target]);
184
+ try {
185
+ // A loaded agent keeps its old plist and bundle until it is unloaded. Not loaded (a first install) is the tolerated failure:
186
+ // any other leaves it loaded, and the bootstrap below then fails loudly.
187
+ deps.run("launchctl", ["bootout", target]);
188
+ }
189
+ catch {
190
+ // not loaded
191
+ }
192
+ deps.run("launchctl", ["bootstrap", domain, plistPath]);
193
+ return `launch agent ${LAUNCH_AGENT_LABEL} (loaded and started, restarted when it fails)`;
194
+ }
195
+ throw new Error(`no start-at-logon entry for platform ${deps.platform}: the machine daemon runs on Windows, macOS and Linux only`);
131
196
  }
132
197
  /**
133
198
  * Disables the start-at-logon entry without stopping the daemon that calls it (it is about to exit on its own). Called by the
@@ -142,6 +207,14 @@ export function disableAutostart(deps) {
142
207
  deps.run("systemctl", ["--user", "disable", UNIT_NAME]);
143
208
  return;
144
209
  }
210
+ if (deps.platform === "darwin") {
211
+ // DX-8028: the daemon's exit 0 is not restarted (SuccessfulExit false); the disable keeps the next login from starting it.
212
+ deps.run("launchctl", [
213
+ "disable",
214
+ `${launchdDomain(deps.uid)}/${LAUNCH_AGENT_LABEL}`,
215
+ ]);
216
+ return;
217
+ }
145
218
  throw new Error(`no start-at-logon entry for platform ${deps.platform}`);
146
219
  }
147
220
  /** Stops and removes the start-at-logon entry. Missing entries are not an error: uninstall is re-runnable. */
@@ -168,5 +241,16 @@ export function removeAutostart(deps) {
168
241
  deps.run("systemctl", ["--user", "daemon-reload"]);
169
242
  return `removed systemd user unit ${UNIT_NAME}`;
170
243
  }
244
+ if (deps.platform === "darwin") {
245
+ const target = `${launchdDomain(deps.uid)}/${LAUNCH_AGENT_LABEL}`;
246
+ try {
247
+ deps.run("launchctl", ["bootout", target]);
248
+ }
249
+ catch {
250
+ // not loaded
251
+ }
252
+ deps.remove(launchAgentPlistPath(deps.home));
253
+ return `removed launch agent ${LAUNCH_AGENT_LABEL}`;
254
+ }
171
255
  throw new Error(`no start-at-logon entry for platform ${deps.platform}`);
172
256
  }
@@ -141,6 +141,17 @@ function createMachineVitalsCollector(deps) {
141
141
  import { execFileSync as execFileSync2 } from "node:child_process";
142
142
  var TASK_NAME = "danxbot-machine-daemon";
143
143
  var UNIT_NAME = "danxbot-machine-daemon.service";
144
+ var LAUNCH_AGENT_LABEL = "ai.50arms.danxbot-machine-daemon";
145
+ function launchdDomain(uid) {
146
+ if (uid === null)
147
+ throw new Error(
148
+ "cannot load the launch agent: the current user's numeric id is unknown"
149
+ );
150
+ return `gui/${uid}`;
151
+ }
152
+ function currentUid() {
153
+ return process.getuid?.() ?? null;
154
+ }
144
155
  function runHidden(file, args) {
145
156
  return execFileSync2(file, args, {
146
157
  encoding: "utf8",
@@ -157,6 +168,13 @@ function disableAutostart(deps) {
157
168
  deps.run("systemctl", ["--user", "disable", UNIT_NAME]);
158
169
  return;
159
170
  }
171
+ if (deps.platform === "darwin") {
172
+ deps.run("launchctl", [
173
+ "disable",
174
+ `${launchdDomain(deps.uid)}/${LAUNCH_AGENT_LABEL}`
175
+ ]);
176
+ return;
177
+ }
160
178
  throw new Error(`no start-at-logon entry for platform ${deps.platform}`);
161
179
  }
162
180
 
@@ -1467,7 +1485,11 @@ async function runDaemonProcess() {
1467
1485
  });
1468
1486
  return finishBeatStop(
1469
1487
  stop,
1470
- () => disableAutostart({ platform: process.platform, run: runHidden })
1488
+ () => disableAutostart({
1489
+ platform: process.platform,
1490
+ run: runHidden,
1491
+ uid: currentUid()
1492
+ })
1471
1493
  );
1472
1494
  }
1473
1495
 
@@ -612,7 +612,7 @@ var readOwnManifest = () => JSON.parse(
612
612
  readFileSync(new URL("../../package.json", import.meta.url), "utf8")
613
613
  ).version;
614
614
  var TOOLKIT_VERSION = resolveToolkitVersion(
615
- false ? void 0 : "0.1.502",
615
+ false ? void 0 : "0.1.504",
616
616
  readOwnManifest
617
617
  );
618
618
  function envelope(now) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@50arms/platform-mcp",
3
- "version": "0.1.502",
3
+ "version": "0.1.504",
4
4
  "description": "Stdio MCP server fronting the 50 Arms dashboard API for agents (operator sessions and dispatched workers alike): the generic 50arms_api/50arms_api_list/50arms_api_spec tools reach any route the caller's credential may call, plus dedicated agent-specific tools (session_connect, request_permission, attach_file, and 50arms_slack_reply) (DX-3465, PLN-11 R-25).",
5
5
  "license": "MIT",
6
6
  "type": "module",