@proagentstore/cli 0.4.63 → 0.4.64

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/dist/index.js +62 -45
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1097,7 +1097,7 @@ var publishCommand = new Command6("publish").description("Publish an agent to Pr
1097
1097
  });
1098
1098
 
1099
1099
  // src/commands/runner/command.ts
1100
- import { spawn as spawn3 } from "child_process";
1100
+ import { spawn as spawn4 } from "child_process";
1101
1101
  import { randomUUID as randomUUID2 } from "crypto";
1102
1102
  import { Command as Command7 } from "commander";
1103
1103
 
@@ -1205,25 +1205,53 @@ async function waitForLocalRunner(opts, timeoutMs = 15e3) {
1205
1205
  import { hostname as hostname3 } from "os";
1206
1206
 
1207
1207
  // src/commands/runner/self-update.ts
1208
- import { execFile } from "child_process";
1208
+ import { execFile, spawn as spawn3 } from "child_process";
1209
1209
  import { promisify } from "util";
1210
1210
  var run = promisify(execFile);
1211
1211
  var RUNNER_UPDATE_PATH = "/pags/runner/update";
1212
1212
  var RUNNER_RESTART_EXIT_CODE = 75;
1213
1213
  var SUPERVISED_ENV = "PAGS_UP_SUPERVISED";
1214
+ var SUPERVISOR_RESTARTS = "2";
1215
+ var SERVICE_ENV = "PAGS_SERVICE";
1216
+ var RESTART_COMMAND_ENV = "PAGS_RESTART_COMMAND";
1217
+ function restarterFrom(env) {
1218
+ if (env[SUPERVISED_ENV] === SUPERVISOR_RESTARTS) return "pags-up";
1219
+ if (env[SUPERVISED_ENV] === "1") return "pags-up-child-only";
1220
+ if (env[SERVICE_ENV] === "1") return "service";
1221
+ if (env[RESTART_COMMAND_ENV]?.trim()) return "command";
1222
+ return null;
1223
+ }
1224
+ function supervisorNote(restarter) {
1225
+ return restarter === "pags-up-child-only" ? "The `pags up` on this machine predates supervisor restarts (#860): the runner comes back on the new release, but the `pags up` window keeps running its own older code until it is restarted there once." : null;
1226
+ }
1227
+ function leaveForRestart(restarter, deps = { spawn: spawn3, exit: (code) => process.exit(code) }) {
1228
+ if (restarter === "command") {
1229
+ deps.spawn(process.env[RESTART_COMMAND_ENV], { shell: true, detached: true, stdio: "ignore", env: process.env }).unref();
1230
+ deps.exit(0);
1231
+ return;
1232
+ }
1233
+ deps.exit(RUNNER_RESTART_EXIT_CODE);
1234
+ }
1235
+ function restartUpArgs(opts) {
1236
+ const args = ["up"];
1237
+ if (opts.headless) args.push("--headless");
1238
+ if (opts.force) args.push("--force");
1239
+ if (opts.instance) args.push("--instance", opts.instance);
1240
+ return args;
1241
+ }
1214
1242
  function planRunnerUpdate(f) {
1215
1243
  if (f.fromSource) return { action: "refused", current: f.current, reason: "This runner runs from a source checkout \u2014 update it with `git pull` there, not npm." };
1216
1244
  if (!f.latest) return { action: "refused", current: f.current, reason: `npm could not be asked for the latest ${CLI_PACKAGE} from this machine.` };
1217
1245
  if (!olderThan(f.current, f.latest)) return { action: "up-to-date", current: f.current };
1218
- if (!f.supervised) {
1246
+ if (!f.restarter) {
1219
1247
  return {
1220
1248
  action: "refused",
1221
1249
  current: f.current,
1222
- reason: `This runner was not started by a \`pags up\` that can restart it (\`pags runner connect\` directly, or a \`pags up\` older than the respawn). Update once at the machine: \`npm i -g ${CLI_PACKAGE}\` and restart \`pags up\` \u2014 later updates can then be done remotely.`
1250
+ reason: `Nothing would start this runner again after an update: it was not started by \`pags up\`, and neither ${SERVICE_ENV}=1 (a launchd/systemd unit that restarts it on a non-zero exit) nor ${RESTART_COMMAND_ENV} (a command that starts it again) is set. Run it under \`pags up\`, or set one of the two where it is started \u2014 later updates can then be done remotely. (A \`pags up\` older than 0.4.62 cannot restart it either: update once at the machine with \`npm i -g ${CLI_PACKAGE}\`.)`
1223
1251
  };
1224
1252
  }
1225
1253
  if (f.busy.length > 0) return { action: "wait", current: f.current, latest: f.latest, waitingFor: f.busy };
1226
- return { action: "update", current: f.current, latest: f.latest };
1254
+ return { action: "update", current: f.current, latest: f.latest, restarter: f.restarter };
1227
1255
  }
1228
1256
  async function installVersion(version2) {
1229
1257
  if (process.env[BOOTSTRAP_ENV]) {
@@ -1380,7 +1408,7 @@ async function connectViaRelay(instanceIds, localUrl, runnerToken, opts, force =
1380
1408
  current: CLI_VERSION,
1381
1409
  latest: await latestPublishedVersion(),
1382
1410
  fromSource: runsFromSource(),
1383
- supervised: process.env[SUPERVISED_ENV] === "1",
1411
+ restarter: restarterFrom(process.env),
1384
1412
  busy: (sessions.sessions ?? []).filter((s) => s.alive && s.runState && s.runState !== "idle").map((s) => s.sessionId)
1385
1413
  };
1386
1414
  };
@@ -1390,7 +1418,7 @@ async function connectViaRelay(instanceIds, localUrl, runnerToken, opts, force =
1390
1418
  writeLine(`Installed ${plan.latest} \u2014 restarting; every agent re-attaches on the way back up.`);
1391
1419
  setTimeout(() => {
1392
1420
  for (const id of [...attached.keys()]) detach(id);
1393
- process.exit(RUNNER_RESTART_EXIT_CODE);
1421
+ leaveForRestart(plan.restarter);
1394
1422
  }, 500).unref();
1395
1423
  };
1396
1424
  let updateWaiting = false;
@@ -1421,7 +1449,8 @@ async function connectViaRelay(instanceIds, localUrl, runnerToken, opts, force =
1421
1449
  } catch (e) {
1422
1450
  return { status: 500, result: { error: `npm could not install ${plan.latest}: ${e instanceof Error ? e.message : String(e)}` } };
1423
1451
  }
1424
- return { status: 200, result: { action: "restarting", current: plan.current, latest: plan.latest } };
1452
+ const supervisor = supervisorNote(plan.restarter);
1453
+ return { status: 200, result: { action: "restarting", current: plan.current, latest: plan.latest, restartedBy: plan.restarter, ...supervisor ? { supervisor } : {} } };
1425
1454
  };
1426
1455
  const answerControl = async (path, body) => {
1427
1456
  if (path === RUNNER_UPDATE_PATH) return answerUpdate(body);
@@ -1728,7 +1757,7 @@ function createRunnerCommand() {
1728
1757
  const primary = instanceIds[0];
1729
1758
  const runnerOpts = { ...opts, host, port, token: runnerToken };
1730
1759
  const spec = runnerSpawnSpec(runnerOpts);
1731
- const runner = spawn3(spec.command, spec.args, {
1760
+ const runner = spawn4(spec.command, spec.args, {
1732
1761
  cwd: spec.cwd,
1733
1762
  stdio: ["ignore", "pipe", "pipe"],
1734
1763
  shell: process.platform === "win32"
@@ -1879,6 +1908,7 @@ function createRunnerCommand() {
1879
1908
  var runnerCommand = createRunnerCommand();
1880
1909
 
1881
1910
  // src/commands/up.ts
1911
+ import { execFileSync as execFileSync2 } from "child_process";
1882
1912
  import { createRequire as createRequire2 } from "module";
1883
1913
  import { Command as Command8 } from "commander";
1884
1914
  import { hostname as hostname5 } from "os";
@@ -2005,7 +2035,7 @@ var API_BASE3 = "https://api.proagentstore.online";
2005
2035
  var CLI_VERSION2 = createRequire2(import.meta.url)("../package.json").version;
2006
2036
  async function stopRunnerProcesses() {
2007
2037
  if (process.platform === "win32") return false;
2008
- const { execFileSync: execFileSync2 } = await import("child_process");
2038
+ const { execFileSync: execFileSync3 } = await import("child_process");
2009
2039
  const patterns = [
2010
2040
  "dist/browser-runner/index.js",
2011
2041
  "browser-runner/src/index",
@@ -2014,7 +2044,7 @@ async function stopRunnerProcesses() {
2014
2044
  let stopped = false;
2015
2045
  for (const p of patterns) {
2016
2046
  try {
2017
- execFileSync2("pkill", ["-f", p], { stdio: "ignore" });
2047
+ execFileSync3("pkill", ["-f", p], { stdio: "ignore" });
2018
2048
  stopped = true;
2019
2049
  } catch {
2020
2050
  }
@@ -2098,17 +2128,16 @@ var upCommand = new Command8("up").description("Start the browser runner for all
2098
2128
  state.activeInstance = instances.length === 1 ? instances[0].name || instances[0].slug || instances[0].id.slice(0, 8) : `${instances.length} agents`;
2099
2129
  printStep(`Connecting ${state.activeInstance}\u2026`, "wait");
2100
2130
  await stopRunnerProcesses();
2101
- const { spawn: spawn4 } = await import("child_process");
2131
+ const { spawn: spawn5 } = await import("child_process");
2102
2132
  const cliPath = process.argv[1];
2103
2133
  const args = [cliPath, "runner", "connect", ...instances.map((i) => i.id)];
2104
2134
  if (opts.headless) args.push("--headless");
2105
2135
  if (opts.force) args.push("--force");
2106
2136
  if (!opts.instance) args.push("--watch-instances");
2107
- const spawnChild = () => spawn4(process.execPath, args, {
2137
+ const child = spawn5(process.execPath, args, {
2108
2138
  stdio: ["ignore", "pipe", "pipe"],
2109
- env: { ...process.env, PAGS_TOKEN: session.token, [SUPERVISED_ENV]: "1" }
2139
+ env: { ...process.env, PAGS_TOKEN: session.token, [SUPERVISED_ENV]: SUPERVISOR_RESTARTS }
2110
2140
  });
2111
- let child = spawnChild();
2112
2141
  const logs = [];
2113
2142
  const handleOutput = (data2) => {
2114
2143
  const text = data2.toString("utf-8");
@@ -2167,17 +2196,11 @@ var upCommand = new Command8("up").description("Start the browser runner for all
2167
2196
  }
2168
2197
  };
2169
2198
  let childDead = false;
2170
- const wire = () => {
2171
- child.stdout?.on("data", handleOutput);
2172
- child.stderr?.on("data", handleOutput);
2173
- child.on("exit", onChildExit);
2174
- };
2175
- function onChildExit(code) {
2199
+ child.stdout?.on("data", handleOutput);
2200
+ child.stderr?.on("data", handleOutput);
2201
+ child.on("exit", (code) => {
2176
2202
  if (code === RUNNER_RESTART_EXIT_CODE) {
2177
- state.lastEvent = "Runner updated remotely \u2014 restarting on the new version";
2178
- printStatus(state);
2179
- child = spawnChild();
2180
- wire();
2203
+ restartUp("Runner updated remotely \u2014 restarting pags up on the new version\u2026", { PAGS_NO_PROMPT: "1" });
2181
2204
  return;
2182
2205
  }
2183
2206
  childDead = true;
@@ -2188,8 +2211,20 @@ var upCommand = new Command8("up").description("Start the browser runner for all
2188
2211
  if (recent.length) state.lastEvent += ": " + recent[recent.length - 1].slice(0, 60);
2189
2212
  printStatus(state);
2190
2213
  }
2214
+ });
2215
+ function restartUp(why, env = {}) {
2216
+ writeLine(` ${why}`);
2217
+ try {
2218
+ if (process.stdin.isTTY) process.stdin.setRawMode(false);
2219
+ process.stdin.pause();
2220
+ execFileSync2(process.execPath, [process.argv[1], ...restartUpArgs(opts)], { stdio: "inherit", env: { ...process.env, ...env } });
2221
+ } catch (e) {
2222
+ const status = e.status;
2223
+ writeLine(` Restart failed${typeof status === "number" ? ` (exit ${status})` : ""} \u2014 the runner is NOT running. Run 'pags up' again.`);
2224
+ process.exit(typeof status === "number" && status !== 0 ? status : 1);
2225
+ }
2226
+ process.exit(0);
2191
2227
  }
2192
- wire();
2193
2228
  const shutdown = () => {
2194
2229
  child.kill();
2195
2230
  clearScreen();
@@ -2217,25 +2252,7 @@ var upCommand = new Command8("up").description("Start the browser runner for all
2217
2252
  printStatus(state);
2218
2253
  }
2219
2254
  if (key === "r") {
2220
- if (childDead) {
2221
- writeLine(" Restarting runner...");
2222
- const { execFileSync: execFileSync2 } = await import("child_process");
2223
- const restartArgs = [process.argv[1], "up"];
2224
- if (opts.headless) restartArgs.push("--headless");
2225
- if (opts.force) restartArgs.push("--force");
2226
- if (opts.instance) restartArgs.push("--instance", opts.instance);
2227
- try {
2228
- execFileSync2(process.execPath, restartArgs, {
2229
- stdio: "inherit",
2230
- env: process.env
2231
- });
2232
- } catch (e) {
2233
- const status = e.status;
2234
- writeLine(` Restart failed${typeof status === "number" ? ` (exit ${status})` : ""} \u2014 the runner is NOT running. Run 'pags up' again.`);
2235
- process.exit(typeof status === "number" && status !== 0 ? status : 1);
2236
- }
2237
- process.exit(0);
2238
- }
2255
+ if (childDead) restartUp("Restarting runner...");
2239
2256
  printStatus(state);
2240
2257
  }
2241
2258
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proagentstore/cli",
3
- "version": "0.4.63",
3
+ "version": "0.4.64",
4
4
  "description": "CLI for creating, publishing, and running ProAgentStore agents",
5
5
  "license": "MIT",
6
6
  "type": "module",