@av-pi-studio/server 0.0.103 → 0.0.104

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.
package/README.md CHANGED
@@ -55,7 +55,8 @@ The daemon is **provider-agnostic**: the rest of the code only ever touches the
55
55
  - **Node.js ≥ 20** (developed and tested on Node 24). ESM only (`"type": "module"`).
56
56
  - **npm** with workspaces (this package lives in the Pi-Studio monorepo).
57
57
  - For the real **`pi` provider**: pi *credentials* only. The `pi` CLI is bundled as a dependency
58
- (`@earendil-works/pi-coding-agent`) — the daemon launches `node <pkg>/dist/cli.js --mode rpc`, so
58
+ (`@earendil-works/pi-coding-agent`) — the daemon launches `node <pkg>/<pi's declared bin> --mode
59
+ rpc` (`dist/bundle/cli.js` as of Pi 0.84.4), so
59
60
  **no global `pi` install is required**. Provide credentials via an API key
60
61
  (`ANTHROPIC_API_KEY`, etc.) in the daemon's environment, or a configured `~/.pi/agent/auth.json`.
61
62
  - The built-in **`mock` provider** needs **no credentials** and is ideal for smoke tests.
@@ -34,18 +34,18 @@ export declare function resolveBinaryOnPath(bin: string, env?: Record<string, st
34
34
  * imports of `expandHome` from this module (via `agent/index.ts`'s barrel) keep working. */
35
35
  export { expandHome };
36
36
  /**
37
- * Resolve the `pi` CLI **bundled inside the `@earendil-works/pi-coding-agent` dependency** (its
38
- * `bin` is `dist/cli.js`, a sibling of the package's `dist/index.js`). This is what lets pi-studio
39
- * run the agent without a separate global `pi` install. Returns `null` if the package isn't present.
37
+ * Resolve the `pi` CLI **bundled inside the `@earendil-works/pi-coding-agent` dependency**. This is
38
+ * what lets pi-studio run the agent without a separate global `pi` install. Returns `null` if the
39
+ * package isn't present.
40
40
  *
41
41
  * Note: `import.meta.resolve` is used (not `require.resolve`) because the package's `exports` map
42
42
  * only defines the `import` condition, so CJS resolution throws `ERR_PACKAGE_PATH_NOT_EXPORTED`.
43
43
  */
44
44
  export declare function resolveBundledPiCli(): string | null;
45
45
  /**
46
- * Default Pi launch command. Prefers the bundled CLI (`node <pkg>/dist/cli.js --mode rpc`) so no
47
- * global install is required; falls back to a global `pi --mode rpc` on `$PATH` if the dependency
48
- * is absent.
46
+ * Default Pi launch command. Prefers the bundled CLI (`node <pkg>/<pi's declared bin> --mode rpc`)
47
+ * so no global install is required; falls back to a global `pi --mode rpc` on `$PATH` if the
48
+ * dependency is absent.
49
49
  */
50
50
  export declare function defaultPiCommand(): string[];
51
51
  /**
@@ -1,5 +1,5 @@
1
1
  import { spawn } from "node:child_process";
2
- import { existsSync } from "node:fs";
2
+ import { existsSync, readFileSync } from "node:fs";
3
3
  import { dirname, join } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import treeKill from "tree-kill";
@@ -16,31 +16,68 @@ export function resolveBinaryOnPath(bin, env = process.env) {
16
16
  * imports of `expandHome` from this module (via `agent/index.ts`'s barrel) keep working. */
17
17
  export { expandHome };
18
18
  /**
19
- * Resolve the `pi` CLI **bundled inside the `@earendil-works/pi-coding-agent` dependency** (its
20
- * `bin` is `dist/cli.js`, a sibling of the package's `dist/index.js`). This is what lets pi-studio
21
- * run the agent without a separate global `pi` install. Returns `null` if the package isn't present.
19
+ * Candidate CLI entrypoints inside the `@earendil-works/pi-coding-agent` package, relative to its
20
+ * root, tried in order when the package's own `bin.pi` cannot be read. `dist/bundle/cli.js` is the
21
+ * prebundled entry Pi declares as `bin` since 0.84.4; `dist/cli.js` is the unbundled entry it
22
+ * declared through 0.84.3 and still ships. Both start the same program.
23
+ */
24
+ const PI_CLI_FALLBACKS = [join("dist", "bundle", "cli.js"), join("dist", "cli.js")];
25
+ /**
26
+ * Resolve the `pi` CLI entrypoint inside an already-located package root, preferring whatever the
27
+ * package's own `package.json` declares as `bin.pi`.
28
+ *
29
+ * Reading the declared `bin` rather than hardcoding a path is deliberate: Pi moved it from
30
+ * `dist/cli.js` to `dist/bundle/cli.js` in 0.84.4, and the dependency range intentionally accepts
31
+ * future minor releases (`>=0.84.4 <1.0.0`), so a relocation must not silently fall back to a
32
+ * global `pi` — or to an entry upstream has stopped shipping.
33
+ */
34
+ function resolvePiCliInPackage(root) {
35
+ try {
36
+ const manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
37
+ const declared = typeof manifest.bin === "string" ? manifest.bin : manifest.bin?.pi;
38
+ if (declared) {
39
+ const cli = join(root, declared);
40
+ if (existsSync(cli))
41
+ return cli;
42
+ }
43
+ }
44
+ catch {
45
+ // Unreadable/malformed manifest — fall through to the known entrypoints.
46
+ }
47
+ for (const rel of PI_CLI_FALLBACKS) {
48
+ const cli = join(root, rel);
49
+ if (existsSync(cli))
50
+ return cli;
51
+ }
52
+ return null;
53
+ }
54
+ /**
55
+ * Resolve the `pi` CLI **bundled inside the `@earendil-works/pi-coding-agent` dependency**. This is
56
+ * what lets pi-studio run the agent without a separate global `pi` install. Returns `null` if the
57
+ * package isn't present.
22
58
  *
23
59
  * Note: `import.meta.resolve` is used (not `require.resolve`) because the package's `exports` map
24
60
  * only defines the `import` condition, so CJS resolution throws `ERR_PACKAGE_PATH_NOT_EXPORTED`.
25
61
  */
26
62
  export function resolveBundledPiCli() {
27
- // 1) ESM resolution — works in the compiled daemon (real `node` ESM).
63
+ // 1) ESM resolution — works in the compiled daemon (real `node` ESM). Resolves the package's
64
+ // `main` (`dist/index.js`); its package root is the parent of that `dist` directory.
28
65
  try {
29
66
  const indexPath = fileURLToPath(import.meta.resolve("@earendil-works/pi-coding-agent"));
30
- const cli = join(dirname(indexPath), "cli.js");
31
- if (existsSync(cli))
67
+ const cli = resolvePiCliInPackage(dirname(dirname(indexPath)));
68
+ if (cli)
32
69
  return cli;
33
70
  }
34
71
  catch {
35
72
  // `import.meta.resolve` is unavailable under some loaders (e.g. vitest); fall through.
36
73
  }
37
74
  // 2) Walk up from cwd looking for the dependency in node_modules (loader-independent).
38
- const rel = join("node_modules", "@earendil-works", "pi-coding-agent", "dist", "cli.js");
75
+ const rel = join("node_modules", "@earendil-works", "pi-coding-agent");
39
76
  let dir = process.cwd();
40
77
  for (;;) {
41
- const candidate = join(dir, rel);
42
- if (existsSync(candidate))
43
- return candidate;
78
+ const cli = resolvePiCliInPackage(join(dir, rel));
79
+ if (cli)
80
+ return cli;
44
81
  const parent = dirname(dir);
45
82
  if (parent === dir)
46
83
  return null;
@@ -48,9 +85,9 @@ export function resolveBundledPiCli() {
48
85
  }
49
86
  }
50
87
  /**
51
- * Default Pi launch command. Prefers the bundled CLI (`node <pkg>/dist/cli.js --mode rpc`) so no
52
- * global install is required; falls back to a global `pi --mode rpc` on `$PATH` if the dependency
53
- * is absent.
88
+ * Default Pi launch command. Prefers the bundled CLI (`node <pkg>/<pi's declared bin> --mode rpc`)
89
+ * so no global install is required; falls back to a global `pi --mode rpc` on `$PATH` if the
90
+ * dependency is absent.
54
91
  */
55
92
  export function defaultPiCommand() {
56
93
  const cli = resolveBundledPiCli();
@@ -18,6 +18,14 @@ export interface DaemonOptions {
18
18
  /** Test-only injection for the preinstalled-extensions sync executor's process seam; production
19
19
  * always spawns the bundled `pi` (`defaultInstallSpawn`). */
20
20
  extensionsInstallSpawn?: InstallSpawn;
21
+ /**
22
+ * Called after an unrecoverable HTTP-server error (typically `EADDRINUSE` on bind) once the WS
23
+ * server has been torn down. `startDaemon` logs the error and sets `process.exitCode = 1`, but
24
+ * never terminates the process itself — a library must not kill its host. The process entry
25
+ * point supplies the actual exit (`daemon/main.ts`); tests leave it unset so a bind failure
26
+ * surfaces as a normal test failure instead of an opaque process kill.
27
+ */
28
+ onFatalError?: (err: NodeJS.ErrnoException) => void;
21
29
  }
22
30
  export interface DaemonHandle {
23
31
  httpServer: HttpServer;
@@ -728,6 +728,13 @@ export function startDaemon(opts) {
728
728
  logger.error({ err: err?.message ?? String(err) }, "agent recovery failed");
729
729
  });
730
730
  // Fail cleanly on a bind error (e.g. EADDRINUSE) instead of an unhandled 'error' throw.
731
+ //
732
+ // Deliberately does NOT call `process.exit()` itself: `startDaemon` is a library entry point
733
+ // (embedded by `packages/desktop`, and booted in-process by the daemon integration tests), and a
734
+ // library must never kill its host. Killing the process here made a port collision in the test
735
+ // suite surface as an opaque, misattributed "process.exit unexpectedly called with 1" unhandled
736
+ // rejection with every test still reported green. The process-level decision belongs to the
737
+ // process entry point, which passes `onFatalError` (see `daemon/main.ts`).
731
738
  httpServer.on("error", (err) => {
732
739
  if (err.code === "EADDRINUSE") {
733
740
  logger.error({ host: opts.host, port: opts.port }, "cannot bind — address already in use");
@@ -736,7 +743,7 @@ export function startDaemon(opts) {
736
743
  logger.error({ err: err.message }, "http server error");
737
744
  }
738
745
  process.exitCode = 1;
739
- void wsHandle.close().finally(() => process.exit(1));
746
+ void wsHandle.close().finally(() => opts.onFatalError?.(err));
740
747
  });
741
748
  httpServer.listen(opts.port, opts.host);
742
749
  logger.info({ host: opts.host, port: opts.port }, "http/ws server accepting connections");
@@ -12,6 +12,9 @@ const info = createDaemonRuntimeInfo({
12
12
  const handle = startDaemon({
13
13
  host: info.listen.host,
14
14
  port: info.listen.port,
15
+ // The process-level half of `startDaemon`'s fatal-error contract: the bootstrap logs the error
16
+ // and tears down the WS server, but leaves terminating the process to its entry point.
17
+ onFatalError: () => process.exit(1),
15
18
  });
16
19
  const log = handle.logger;
17
20
  log.info({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@av-pi-studio/server",
3
- "version": "0.0.103",
3
+ "version": "0.0.104",
4
4
  "bin": {
5
5
  "pi-studio-daemon": "dist/daemon/main.js"
6
6
  },
@@ -24,10 +24,10 @@
24
24
  "clean": "rm -rf dist *.tsbuildinfo"
25
25
  },
26
26
  "dependencies": {
27
- "@av-pi-studio/highlight": "^0.0.103",
28
- "@av-pi-studio/protocol": "^0.0.103",
29
- "@av-pi-studio/relay": "^0.0.103",
30
- "@earendil-works/pi-coding-agent": "^0.84.1",
27
+ "@av-pi-studio/highlight": "^0.0.104",
28
+ "@av-pi-studio/protocol": "^0.0.104",
29
+ "@av-pi-studio/relay": "^0.0.104",
30
+ "@earendil-works/pi-coding-agent": "^0.84.4",
31
31
  "@xterm/addon-serialize": "^0.14.0",
32
32
  "@xterm/headless": "^6.0.0",
33
33
  "bcryptjs": "^3.0.3",