@agentage/cli 0.52.0 → 0.52.1

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.
@@ -32,7 +32,7 @@ const reasonOf = (ext, id) => {
32
32
  return ext.problem;
33
33
  const refused = kitIdProblem(id);
34
34
  if (refused)
35
- return `${refused}: the daemon takes no such row`;
35
+ return `${refused}: the daemon takes no such row; give the extension a valid id, then agentage extension install <its folder>`;
36
36
  return ext.source === 'tree' && !ext.version ? 'no exact version' : null;
37
37
  };
38
38
  const entryOf = (ext, off) => {
@@ -15,7 +15,7 @@ const LOCAL = /^\.{1,2}(?:[/\\]|$)/;
15
15
  const PACKAGE_PREFIX = /^(@agentage\/extension-|agentage-extension-)/;
16
16
  const UNSCOPED = 'agentage-extension-';
17
17
  // The daemon takes an id, not a package name: with a market it resolves only the scoped name.
18
- const unscoped = (name) => new CliError('invalid_argument', `${name}: an unscoped npm name installs only on a site with no market; nothing installed`);
18
+ const unscoped = (name) => new CliError('invalid_argument', `${name}: an unscoped npm name installs only on a site with no market; nothing installed; install its folder instead: agentage extension install ./<folder>`);
19
19
  // What 0.51 installed and the daemon does not: say what to type instead.
20
20
  const refusedName = (name) => {
21
21
  if (/^https?:\/\//i.test(name))
@@ -6,9 +6,11 @@ export interface EnsureDeps {
6
6
  spawn: (port: number) => Promise<SpawnOutcome>;
7
7
  stop: () => Promise<StopResult>;
8
8
  log: (line: string) => void;
9
+ lateMs?: number;
9
10
  }
10
11
  export declare const LOG_CAP: number;
11
- export declare const waitForHealth: (port: number, timeoutMs?: number) => Promise<boolean>;
12
+ export declare const LATE_FILE_MS = 2000;
13
+ export declare const waitForHealth: (port: number, pid: number, timeoutMs?: number) => Promise<boolean>;
12
14
  export declare const daemonLogPath: () => string;
13
15
  export declare const openDaemonLog: (cap?: number) => number;
14
16
  export declare const spawnDaemon: (port: number, entry?: string) => Promise<SpawnOutcome>;
@@ -1,5 +1,5 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { closeSync, mkdirSync, openSync, renameSync, statSync } from 'node:fs';
2
+ import { closeSync, existsSync, mkdirSync, openSync, renameSync, statSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
  import { fileURLToPath } from 'node:url';
5
5
  import { childEnv } from '../child-env.js';
@@ -8,13 +8,17 @@ import { CliError } from '../json-output.js';
8
8
  import { compareVersions } from '../update/update-check.js';
9
9
  import { VERSION } from '../../utils/version.js';
10
10
  import { waitFor } from '../wait.js';
11
- import { daemonOff, daemonPort, EADDRINUSE_EXIT, isAlive, readDaemonFile, removeDaemonFile, } from './daemon-file.js';
12
- import { portInUse, probe, probeRetry, PROGRESS_MS, PROTOCOL, proven, unresponsive, } from './daemon-probe.js';
11
+ import { daemonFilePath, daemonOff, daemonPort, EADDRINUSE_EXIT, isAlive, readDaemonFile, removeDaemonFile, } from './daemon-file.js';
12
+ import { otherFolder, unreadableFile, portInUse, probe, probeRetry, PROGRESS_MS, PROTOCOL, proven, unresponsive, } from './daemon-probe.js';
13
13
  import { stopDaemon } from './daemon-stop.js';
14
14
  const START_MS = 30_000;
15
15
  // Over this the log moves to daemon.log.1 at the next start; one old file is kept.
16
16
  export const LOG_CAP = 5 * 1024 * 1024;
17
- export const waitForHealth = (port, timeoutMs = START_MS) => waitFor(async () => (await probe(port, 500)).kind === 'ours', timeoutMs, {
17
+ // A busy port: how long daemon.json of this folder may take to appear (written after listen).
18
+ export const LATE_FILE_MS = 2_000;
19
+ const answersAs = (pid, found) => found.kind === 'ours' && found.health.pid === pid;
20
+ // Healthy only as the spawned pid: a daemon of another config folder on the port is not ours.
21
+ export const waitForHealth = (port, pid, timeoutMs = START_MS) => waitFor(async () => answersAs(pid, await probe(port, 500)), timeoutMs, {
18
22
  afterMs: PROGRESS_MS,
19
23
  line: `waiting for the daemon to start on 127.0.0.1:${port}...`,
20
24
  });
@@ -40,7 +44,7 @@ export const spawnDaemon = async (port, entry = CLI_ENTRY) => {
40
44
  closeSync(out);
41
45
  const exited = new Promise((resolve) => child.once('exit', (code) => resolve(code === EADDRINUSE_EXIT ? 'port-in-use' : 'unreachable')));
42
46
  child.unref();
43
- const healthy = waitForHealth(port).then((ok) => (ok ? 'ok' : 'unreachable'));
47
+ const healthy = waitForHealth(port, child.pid ?? 0).then((ok) => ok ? 'ok' : 'unreachable');
44
48
  return Promise.race([healthy, exited]);
45
49
  };
46
50
  export const defaultEnsureDeps = {
@@ -55,14 +59,23 @@ const lateDaemon = async (deps) => {
55
59
  return file && proven(file, await deps.probe(file.port)) ? file.port : null;
56
60
  };
57
61
  const notStarted = () => new CliError('daemon_unavailable', `the daemon did not start: it did not answer; see agentage daemon status and ${daemonLogPath()}`);
58
- // Another cli may have started one on the same port at the same time: that one serves.
62
+ // Another cli of this folder may have started one at the same time: its daemon.json can lag.
63
+ const ownLate = async (deps, port, ms) => waitFor(async () => (await lateDaemon(deps)) === port, ms);
64
+ // A busy port is ours only through this folder's daemon.json; else it is refused, never used.
65
+ const busyPort = async (deps, port) => {
66
+ if (await ownLate(deps, port, deps.lateMs ?? LATE_FILE_MS))
67
+ return port;
68
+ const found = await deps.probe(port);
69
+ if (found.kind !== 'ours')
70
+ throw portInUse(port);
71
+ if (existsSync(daemonFilePath()))
72
+ throw unreadableFile(port, found.health.pid);
73
+ throw otherFolder(port, found.health.pid);
74
+ };
59
75
  const start = async (deps, port) => {
60
76
  const outcome = await deps.spawn(port);
61
- if (outcome === 'port-in-use') {
62
- if ((await deps.probe(port)).kind === 'ours')
63
- return port;
64
- throw portInUse(port);
65
- }
77
+ if (outcome === 'port-in-use')
78
+ return busyPort(deps, port);
66
79
  if (outcome !== 'ok') {
67
80
  const late = await lateDaemon(deps);
68
81
  if (late === null)
@@ -86,7 +99,7 @@ const fromFile = async (file, deps) => {
86
99
  return (removeDaemonFile(), null);
87
100
  const health = proven(file, found);
88
101
  if (!health)
89
- throw portInUse(file.port);
102
+ throw found.kind === 'ours' ? otherFolder(file.port, found.health.pid) : portInUse(file.port);
90
103
  if (!outdated(health))
91
104
  return file.port;
92
105
  await deps.stop();
@@ -29,4 +29,6 @@ export declare const probe: (port: number, timeoutMs?: number) => Promise<Probe>
29
29
  export declare const probeRetry: (port: number, tries?: number, timeoutMs?: number) => Promise<Probe>;
30
30
  export declare const proven: (file: DaemonFile, found: Probe) => Health | null;
31
31
  export declare const unresponsive: (file: DaemonFile) => CliError;
32
+ export declare const otherFolder: (port: number, pid: number, platform?: NodeJS.Platform) => CliError;
33
+ export declare const unreadableFile: (port: number, pid: number) => CliError;
32
34
  export declare const portInUse: (port: number) => CliError;
@@ -38,4 +38,13 @@ export const probeRetry = async (port, tries = 3, timeoutMs = 2000) => {
38
38
  export const proven = (file, found) => found.kind === 'ours' && found.health.pid === file.pid ? found.health : null;
39
39
  export const unresponsive = (file) => new CliError('daemon_unresponsive', `the daemon (pid ${file.pid}) does not answer on 127.0.0.1:${file.port}; ` +
40
40
  'try again, or stop that process yourself');
41
+ const killLine = (pid, platform = process.platform) => platform === 'win32' ? `taskkill /PID ${pid} /F` : `kill ${pid}`;
42
+ const stopAdvice = (pid, platform) => `stop it (agentage daemon stop with its AGENTAGE_CONFIG_DIR, or ${killLine(pid, platform)}), ` +
43
+ 'or set AGENTAGE_DAEMON_PORT to another port';
44
+ // The daemon of another config folder (AGENTAGE_CONFIG_DIR) holds the port: never used as ours.
45
+ export const otherFolder = (port, pid, platform) => new CliError('port_in_use', `port ${port} is in use by the agentage daemon (pid ${pid}) of another config folder; ` +
46
+ stopAdvice(pid, platform));
47
+ // This folder's daemon.json exists but cannot be read: the daemon on the port is not proven ours.
48
+ export const unreadableFile = (port, pid) => new CliError('port_in_use', `port ${port} is in use by an agentage daemon (pid ${pid}), and daemon.json of this config ` +
49
+ `folder cannot be read; ${stopAdvice(pid)}`);
41
50
  export const portInUse = (port) => new CliError('port_in_use', `port ${port} is in use by another program; free it, or set AGENTAGE_DAEMON_PORT to another port`);
@@ -1,7 +1,7 @@
1
1
  import { isRecord } from '../fs/json-file.js';
2
2
  import { CliError, messageOf } from '../json-output.js';
3
3
  import { daemonFailure } from './daemon-answer.js';
4
- // An install runs npm (up to 10 min in the daemon), then starts the child: past that it is stuck.
4
+ // An install runs npm (the daemon waits up to 660 s), then starts the child: past that it is stuck.
5
5
  const WAITS = { read: 10_000, change: 12 * 60_000 };
6
6
  // The kit's queue and installer can each take 11 min, so a change can still finish after this
7
7
  // wait; /health lists it under pending until it ends.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentage/cli",
3
- "version": "0.52.0",
3
+ "version": "0.52.1",
4
4
  "description": "The agentage CLI - connect this machine to agentage from the terminal",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",