pi-ai-vault 0.12.2 → 0.12.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-ai-vault",
3
- "version": "0.12.2",
3
+ "version": "0.12.4",
4
4
  "description": "pi extension for ai-vault: one machine-wide encrypted secret store, live Bitwarden and Ente Auth. Works in pi and in omp (the omp fork reads the same manifest).",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,7 @@
25
25
  "security"
26
26
  ],
27
27
  "dependencies": {
28
- "@xiaolikt/ai-vault": "0.12.2"
28
+ "@xiaolikt/ai-vault": "0.12.4"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@earendil-works/pi-coding-agent": "^1.5.0",
package/src/bitwarden.ts CHANGED
@@ -298,14 +298,27 @@ const WIN32 = process.platform === "win32";
298
298
  /** A file under the Windows directory: `%SystemRoot%` already is `C:\Windows`. */
299
299
  const systemFile = (...parts: string[]): string => path.join(process.env.SystemRoot ?? "C:/Windows", ...parts);
300
300
 
301
- /** `taskkill /T /F`: the process and everything it started. Windows has no process groups. Exported for tests. */
302
- export function killTree(pid: number): void {
301
+ /**
302
+ * `taskkill /T /F`: the process and everything it started. Windows has no
303
+ * process groups. Exported for tests.
304
+ *
305
+ * Asynchronous on purpose: this runs when a command times out or the user
306
+ * cancels, which is a moment the host's UI must stay live. The promise is
307
+ * unref'd, so a pending taskkill never holds the process open.
308
+ */
309
+ export function killTree(pid: number): Promise<void> {
303
310
  const taskkill = systemFile("System32", "taskkill.exe");
304
- try {
305
- spawnSync(taskkill, ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
306
- } catch {
307
- /* best-effort: the caller still kills the process itself */
308
- }
311
+ return new Promise<void>((resolve) => {
312
+ try {
313
+ const child = spawn(taskkill, ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
314
+ child.on("error", () => resolve());
315
+ child.on("close", () => resolve());
316
+ child.unref();
317
+ setTimeout(resolve, 5000).unref();
318
+ } catch {
319
+ resolve();
320
+ }
321
+ });
309
322
  }
310
323
 
311
324
  /** How long a copy command may take to set the clipboard and exit. */
@@ -366,9 +379,11 @@ function exec(cmd: string, args: string[], opts: ExecOptions = {}): Promise<Exec
366
379
  const kill = () => {
367
380
  if (WIN32) {
368
381
  // No process groups on Windows: take the whole tree down, or a command
369
- // that timed out leaves what it started running with the secret in its environment.
370
- if (child.pid) killTree(child.pid);
382
+ // that timed out leaves what it started running with the secret in its
383
+ // environment. The child goes first and at once; the tree follows off
384
+ // the event loop, so a slow taskkill cannot freeze the host's UI.
371
385
  child.kill("SIGKILL");
386
+ if (child.pid) void killTree(child.pid);
372
387
  return;
373
388
  }
374
389
  if (opts.group && child.pid) {
@@ -457,22 +472,28 @@ export async function auditEvent(event: string, detail: string, session?: string
457
472
  let bwLaunch: CliLaunch | undefined;
458
473
 
459
474
  /** Runs at all: answers `--version` with exit 0. */
460
- async function bwRuns(launch: CliLaunch): Promise<boolean> {
475
+ async function bwRuns(launch: CliLaunch): Promise<{ ok: boolean; why?: string }> {
461
476
  try {
462
477
  const r = await exec(launch.program, [...launch.pre, "--version"], { timeout: 20_000 });
463
- return r.code === 0;
464
- } catch {
465
- return false;
478
+ return r.code === 0 ? { ok: true } : { ok: false, why: `exit ${r.code}${r.stderr ? `: ${tail(r.stderr, 120)}` : ""}` };
479
+ } catch (error) {
480
+ // A timeout or a refused start is worth reporting; a missing file is not.
481
+ const why = error instanceof Error && !(error as NodeJS.ErrnoException).code ? error.message : undefined;
482
+ return { ok: false, why };
466
483
  }
467
484
  }
468
485
 
469
486
  export async function resolveBw(): Promise<CliLaunch> {
470
487
  if (bwLaunch) return bwLaunch;
471
488
  // `bw.exe` is found by the bare name on Windows, `bw` elsewhere.
472
- if (await bwRuns({ program: "bw", pre: [] })) {
489
+ const first = await bwRuns({ program: "bw", pre: [] });
490
+ if (first.ok) {
473
491
  bwLaunch = { program: "bw", pre: [] };
474
492
  return bwLaunch;
475
493
  }
494
+ // "Not found" is what the search below fixes; a bw that is there but will
495
+ // not answer is not, and saying so beats a list of places it is not.
496
+ let trouble = first.why;
476
497
  // npm installs `bw` as bw.cmd (with a .ps1 and a sh script beside it), which
477
498
  // Windows cannot start without a shell: start the JavaScript it wraps with
478
499
  // this Node instead.
@@ -483,10 +504,13 @@ export async function resolveBw(): Promise<CliLaunch> {
483
504
  const candidate = path.join(dir, name);
484
505
  if (!existsSync(candidate)) continue;
485
506
  const launch = name === "bw.exe" ? { program: candidate, pre: [] } : npmShimLaunch(candidate);
486
- if (launch && (await bwRuns(launch))) {
507
+ if (!launch) continue;
508
+ const run = await bwRuns(launch);
509
+ if (run.ok) {
487
510
  bwLaunch = launch;
488
511
  return bwLaunch;
489
512
  }
513
+ trouble ??= run.why && `${candidate}: ${run.why}`;
490
514
  }
491
515
  }
492
516
  }
@@ -506,6 +530,7 @@ export async function resolveBw(): Promise<CliLaunch> {
506
530
  } catch {
507
531
  /* ignore */
508
532
  }
533
+ if (trouble) throw new Error(`Bitwarden CLI (\`bw\`) does not start: ${trouble}`);
509
534
  throw new Error("Bitwarden CLI (`bw`) not found in PATH or ~/.nvm. Install it, `bw login`, and retry.");
510
535
  }
511
536
 
package/src/cli.ts CHANGED
@@ -174,12 +174,32 @@ export function resolveBinary(): string | undefined {
174
174
  * Start one CLI call. A program Node will not start is reported through
175
175
  * `CliError` like every other failure, not thrown from inside the promise.
176
176
  */
177
- function startCli(launch: CliLaunch, args: string[], env: NodeJS.ProcessEnv): ChildProcessWithoutNullStreams {
177
+ /** The message of the last start failure, for the callback below. */
178
+ let lastStartError = "";
179
+
180
+ /**
181
+ * Start one CLI call. A program Node will not start is reported through
182
+ * `CliError` like every other failure — thrown here when `spawn` refuses
183
+ * outright, or handed to `onError` when the failure arrives as an event (a
184
+ * policy, a virus scanner holding the file) — never thrown from inside the
185
+ * promise the caller is building.
186
+ */
187
+ function startCli(
188
+ launch: CliLaunch,
189
+ args: string[],
190
+ env: NodeJS.ProcessEnv,
191
+ onError: () => void,
192
+ ): ChildProcessWithoutNullStreams {
178
193
  try {
179
194
  // windowsHide: the CLI is a console program, and gets no window of its
180
195
  // own when the host has none. The dialogs it opens are windows it opens
181
196
  // on purpose, and show regardless.
182
- return spawn(launch.program, [...launch.pre, ...args], { stdio: ["pipe", "pipe", "pipe"], env, windowsHide: true });
197
+ const child = spawn(launch.program, [...launch.pre, ...args], { stdio: ["pipe", "pipe", "pipe"], env, windowsHide: true });
198
+ child.on("error", (error) => {
199
+ lastStartError = error.message;
200
+ onError();
201
+ });
202
+ return child;
183
203
  } catch (error) {
184
204
  throw new CliError(EXIT.error, undefined, `cannot run ${launch.program}: ${error instanceof Error ? error.message : String(error)}`, "");
185
205
  }
@@ -266,7 +286,10 @@ export class VaultCli {
266
286
  const run = await new Promise<CliRun>((resolve, reject) => {
267
287
  // A program Node will not start fails here like any other CLI failure,
268
288
  // not as a bare exception from inside the promise.
269
- const child = startCli(launch, args, { ...process.env, ...this.host.env() });
289
+ const child = startCli(launch, args, { ...process.env, ...this.host.env() }, () => {
290
+ opts.signal?.removeEventListener("abort", onAbort);
291
+ reject(new CliError(EXIT.error, undefined, `cannot run ${launch.program}: ${lastStartError}`, ""));
292
+ });
270
293
  let stdout = "";
271
294
  let stderr = "";
272
295
  const onAbort = () => child.kill("SIGKILL");