@vellumai/cli 0.4.42 → 0.4.43

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.
@@ -3,7 +3,6 @@ import { homedir } from "os";
3
3
  import { join } from "path";
4
4
 
5
5
  import {
6
- defaultLocalResources,
7
6
  findAssistantByName,
8
7
  getActiveAssistant,
9
8
  loadAllAssistants,
@@ -178,45 +177,65 @@ interface DetectedProcess {
178
177
  pid: string | null;
179
178
  port: number;
180
179
  running: boolean;
180
+ watch: boolean;
181
+ }
182
+
183
+ async function isWatchMode(pid: string): Promise<boolean> {
184
+ try {
185
+ const args = await execOutput("ps", ["-p", pid, "-o", "args="]);
186
+ return args.includes("--watch");
187
+ } catch {
188
+ return false;
189
+ }
181
190
  }
182
191
 
183
192
  async function detectProcess(spec: ProcessSpec): Promise<DetectedProcess> {
184
193
  // Tier 1: pgrep by process title
185
194
  const pids = await pgrepExact(spec.pgrepName);
186
195
  if (pids.length > 0) {
187
- return { name: spec.name, pid: pids[0], port: spec.port, running: true };
196
+ const watch = await isWatchMode(pids[0]);
197
+ return { name: spec.name, pid: pids[0], port: spec.port, running: true, watch };
188
198
  }
189
199
 
190
200
  // Tier 2: TCP port probe (skip for processes without a port)
191
201
  const listening = spec.port > 0 && (await probePort(spec.port));
192
202
  if (listening) {
193
203
  const filePid = readPidFile(spec.pidFile);
204
+ const watch = filePid ? await isWatchMode(filePid) : false;
194
205
  return {
195
206
  name: spec.name,
196
207
  pid: filePid,
197
208
  port: spec.port,
198
209
  running: true,
210
+ watch,
199
211
  };
200
212
  }
201
213
 
202
214
  // Tier 3: PID file fallback
203
215
  const filePid = readPidFile(spec.pidFile);
204
216
  if (filePid && isProcessAlive(filePid)) {
205
- return { name: spec.name, pid: filePid, port: spec.port, running: true };
217
+ const watch = await isWatchMode(filePid);
218
+ return { name: spec.name, pid: filePid, port: spec.port, running: true, watch };
206
219
  }
207
220
 
208
- return { name: spec.name, pid: null, port: spec.port, running: false };
221
+ return { name: spec.name, pid: null, port: spec.port, running: false, watch: false };
209
222
  }
210
223
 
211
224
  function formatDetectionInfo(proc: DetectedProcess): string {
212
225
  const parts: string[] = [];
213
226
  if (proc.pid) parts.push(`PID ${proc.pid}`);
214
227
  if (proc.port > 0) parts.push(`port ${proc.port}`);
228
+ if (proc.watch) parts.push("watch");
215
229
  return parts.join(" | ");
216
230
  }
217
231
 
218
232
  async function getLocalProcesses(entry: AssistantEntry): Promise<TableRow[]> {
219
- const resources = entry.resources ?? defaultLocalResources();
233
+ if (!entry.resources) {
234
+ throw new Error(
235
+ `Local assistant '${entry.assistantId}' is missing resource configuration. Re-hatch to fix.`,
236
+ );
237
+ }
238
+ const resources = entry.resources;
220
239
  const vellumDir = join(resources.instanceDir, ".vellum");
221
240
 
222
241
  const specs: ProcessSpec[] = [
@@ -409,9 +428,7 @@ async function listAllAssistants(): Promise<void> {
409
428
  // process isn't running, the assistant is sleeping — skip the
410
429
  // network health check to avoid a misleading "unreachable" status.
411
430
  let health: { status: string; detail: string | null };
412
- const resources =
413
- a.resources ??
414
- (a.cloud === "local" ? defaultLocalResources() : undefined);
431
+ const resources = a.resources;
415
432
  if (a.cloud === "local" && resources) {
416
433
  const pid = readPidFile(resources.pidFile);
417
434
  const alive = pid !== null && isProcessAlive(pid);
@@ -37,7 +37,17 @@ export async function recover(): Promise<void> {
37
37
  process.exit(1);
38
38
  }
39
39
 
40
- // 2. Check ~/.vellum doesn't already exist
40
+ // 2. Read and validate metadata before any side effects
41
+ const entry: AssistantEntry = JSON.parse(readFileSync(metadataPath, "utf-8"));
42
+ if (!entry.resources) {
43
+ throw new Error(
44
+ `Retired assistant '${name}' is missing resource configuration. ` +
45
+ `Fix the archive metadata at ${metadataPath} and retry, ` +
46
+ `or run 'vellum hatch' to re-provision with proper resource allocation.`,
47
+ );
48
+ }
49
+
50
+ // 3. Check ~/.vellum doesn't already exist
41
51
  const vellumDir = join(homedir(), ".vellum");
42
52
  if (existsSync(vellumDir)) {
43
53
  console.error(
@@ -46,21 +56,20 @@ export async function recover(): Promise<void> {
46
56
  process.exit(1);
47
57
  }
48
58
 
49
- // 3. Extract archive
59
+ // 4. Extract archive
50
60
  await exec("tar", ["xzf", archivePath, "-C", homedir()]);
51
61
 
52
- // 4. Restore lockfile entry
53
- const entry: AssistantEntry = JSON.parse(readFileSync(metadataPath, "utf-8"));
62
+ // 5. Restore lockfile entry
54
63
  saveAssistantEntry(entry);
55
64
 
56
- // 5. Clean up archive
65
+ // 6. Clean up archive
57
66
  unlinkSync(archivePath);
58
67
  unlinkSync(metadataPath);
59
68
 
60
- // 6. Start daemon + gateway (same as wake)
61
- await startLocalDaemon();
69
+ // 7. Start daemon + gateway (same as wake)
70
+ await startLocalDaemon(false, entry.resources);
62
71
  if (!process.env.VELLUM_DESKTOP_APP) {
63
- await startGateway();
72
+ await startGateway(undefined, false, entry.resources);
64
73
  }
65
74
 
66
75
  console.log(`✅ Recovered assistant '${name}'.`);
@@ -4,13 +4,13 @@ import { homedir } from "os";
4
4
  import { basename, dirname, join } from "path";
5
5
 
6
6
  import {
7
- defaultLocalResources,
8
7
  findAssistantByName,
9
8
  loadAllAssistants,
10
9
  removeAssistantEntry,
11
10
  } from "../lib/assistant-config";
12
11
  import type { AssistantEntry } from "../lib/assistant-config";
13
12
  import { retireInstance as retireAwsInstance } from "../lib/aws";
13
+ import { retireDocker } from "../lib/docker";
14
14
  import { retireInstance as retireGcpInstance } from "../lib/gcp";
15
15
  import {
16
16
  stopOrphanedDaemonProcesses,
@@ -45,22 +45,20 @@ function extractHostFromUrl(url: string): string {
45
45
  async function retireLocal(name: string, entry: AssistantEntry): Promise<void> {
46
46
  console.log("\u{1F5D1}\ufe0f Stopping local assistant...\n");
47
47
 
48
- // Use entry resources when available; for legacy entries, derive paths
49
- // from baseDataDir (which may differ from homedir if BASE_DATA_DIR was set).
50
- const resources = entry.resources ?? defaultLocalResources();
51
- const legacyDir = entry.baseDataDir;
52
- const vellumDir = legacyDir ?? join(resources.instanceDir, ".vellum");
48
+ if (!entry.resources) {
49
+ throw new Error(
50
+ `Local assistant '${name}' is missing resource configuration. Re-hatch to fix.`,
51
+ );
52
+ }
53
+ const resources = entry.resources;
54
+ const vellumDir = join(resources.instanceDir, ".vellum");
53
55
 
54
56
  // Check whether another local assistant shares the same data directory.
55
- // Legacy entries without `resources` all resolve to ~/.vellum/ — if we
56
- // blindly kill processes and archive the directory, we'd destroy the
57
- // other assistant's running daemon and data.
58
57
  const otherSharesDir = loadAllAssistants().some((other) => {
59
58
  if (other.cloud !== "local") return false;
60
59
  if (other.assistantId === name) return false;
61
- const otherVellumDir =
62
- other.baseDataDir ??
63
- join((other.resources ?? defaultLocalResources()).instanceDir, ".vellum");
60
+ if (!other.resources) return false;
61
+ const otherVellumDir = join(other.resources.instanceDir, ".vellum");
64
62
  return otherVellumDir === vellumDir;
65
63
  });
66
64
 
@@ -72,17 +70,8 @@ async function retireLocal(name: string, entry: AssistantEntry): Promise<void> {
72
70
  return;
73
71
  }
74
72
 
75
- // Stop daemon via PID file — prefer resources paths, but for legacy entries
76
- // with a custom baseDataDir, derive from that directory instead.
77
- const daemonPidFile = legacyDir
78
- ? join(legacyDir, "vellum.pid")
79
- : resources.pidFile;
80
- const socketFile = legacyDir
81
- ? join(legacyDir, "vellum.sock")
82
- : resources.socketPath;
83
- const daemonStopped = await stopProcessByPidFile(daemonPidFile, "daemon", [
84
- socketFile,
85
- ]);
73
+ const daemonPidFile = resources.pidFile;
74
+ const daemonStopped = await stopProcessByPidFile(daemonPidFile, "daemon");
86
75
 
87
76
  // Stop gateway via PID file — use a longer timeout because the gateway has a
88
77
  // configurable drain window (GATEWAY_SHUTDOWN_DRAIN_MS, default 5s) before it exits.
@@ -286,6 +275,8 @@ async function retireInner(): Promise<void> {
286
275
  process.exit(1);
287
276
  }
288
277
  await retireAwsInstance(name, region, source);
278
+ } else if (cloud === "docker") {
279
+ await retireDocker(name);
289
280
  } else if (cloud === "local") {
290
281
  await retireLocal(name, entry);
291
282
  } else if (cloud === "custom") {
@@ -1,15 +1,50 @@
1
+ import { existsSync, readFileSync } from "fs";
1
2
  import { join } from "path";
2
3
 
3
- import {
4
- defaultLocalResources,
5
- resolveTargetAssistant,
6
- } from "../lib/assistant-config.js";
7
- import { stopProcessByPidFile } from "../lib/process";
4
+ import { resolveTargetAssistant } from "../lib/assistant-config.js";
5
+ import type { AssistantEntry } from "../lib/assistant-config.js";
6
+ import { isProcessAlive, stopProcessByPidFile } from "../lib/process";
7
+
8
+ const ACTIVE_CALL_LEASES_FILE = "active-call-leases.json";
9
+
10
+ type ActiveCallLease = {
11
+ callSessionId: string;
12
+ };
13
+
14
+ function getAssistantRootDir(entry: AssistantEntry): string {
15
+ if (!entry.resources) {
16
+ throw new Error(
17
+ `Local assistant '${entry.assistantId}' is missing resource configuration. Re-hatch to fix.`,
18
+ );
19
+ }
20
+ return join(entry.resources.instanceDir, ".vellum");
21
+ }
22
+
23
+ function readActiveCallLeases(vellumDir: string): ActiveCallLease[] {
24
+ const path = join(vellumDir, ACTIVE_CALL_LEASES_FILE);
25
+ if (!existsSync(path)) {
26
+ return [];
27
+ }
28
+
29
+ const raw = JSON.parse(readFileSync(path, "utf-8")) as {
30
+ version?: number;
31
+ leases?: Array<{ callSessionId?: unknown }>;
32
+ };
33
+ if (raw.version !== 1 || !Array.isArray(raw.leases)) {
34
+ throw new Error(`Invalid active call lease file at ${path}`);
35
+ }
36
+
37
+ return raw.leases.filter(
38
+ (lease): lease is ActiveCallLease =>
39
+ typeof lease?.callSessionId === "string" &&
40
+ lease.callSessionId.length > 0,
41
+ );
42
+ }
8
43
 
9
44
  export async function sleep(): Promise<void> {
10
45
  const args = process.argv.slice(3);
11
46
  if (args.includes("--help") || args.includes("-h")) {
12
- console.log("Usage: vellum sleep [<name>]");
47
+ console.log("Usage: vellum sleep [<name>] [--force]");
13
48
  console.log("");
14
49
  console.log("Stop the assistant and gateway processes.");
15
50
  console.log("");
@@ -17,9 +52,15 @@ export async function sleep(): Promise<void> {
17
52
  console.log(
18
53
  " <name> Name of the assistant to stop (default: active or only local)",
19
54
  );
55
+ console.log("");
56
+ console.log("Options:");
57
+ console.log(
58
+ " --force Stop the assistant even if a phone call keepalive lease is active",
59
+ );
20
60
  process.exit(0);
21
61
  }
22
62
 
63
+ const force = args.includes("--force");
23
64
  const nameArg = args.find((a) => !a.startsWith("-"));
24
65
  const entry = resolveTargetAssistant(nameArg);
25
66
 
@@ -30,18 +71,49 @@ export async function sleep(): Promise<void> {
30
71
  process.exit(1);
31
72
  }
32
73
 
33
- const resources = entry.resources ?? defaultLocalResources();
34
-
35
- const daemonPidFile = resources.pidFile;
36
- const socketFile = resources.socketPath;
37
- const vellumDir = join(resources.instanceDir, ".vellum");
74
+ if (!entry.resources) {
75
+ console.error(
76
+ `Error: Local assistant '${entry.assistantId}' is missing resource configuration. Re-hatch to fix.`,
77
+ );
78
+ process.exit(1);
79
+ }
80
+ const resources = entry.resources;
81
+ const assistantPidFile = resources.pidFile;
82
+ const vellumDir = getAssistantRootDir(entry);
38
83
  const gatewayPidFile = join(vellumDir, "gateway.pid");
39
84
 
40
- // Stop daemon
41
- const daemonStopped = await stopProcessByPidFile(daemonPidFile, "daemon", [
42
- socketFile,
43
- ]);
44
- if (!daemonStopped) {
85
+ if (!force) {
86
+ const assistantAlive = isProcessAlive(assistantPidFile).alive;
87
+ if (assistantAlive) {
88
+ try {
89
+ const activeCallLeases = readActiveCallLeases(vellumDir);
90
+ if (activeCallLeases.length > 0) {
91
+ const activeIds = activeCallLeases.map(
92
+ (lease) => lease.callSessionId,
93
+ );
94
+ console.error(
95
+ `Error: assistant is staying awake for active phone calls (${activeIds.join(
96
+ ", ",
97
+ )}). Use 'vellum sleep --force' to stop it anyway.`,
98
+ );
99
+ process.exit(1);
100
+ }
101
+ } catch (err) {
102
+ console.error(
103
+ `Error: ${
104
+ err instanceof Error ? err.message : String(err)
105
+ }. Use 'vellum sleep --force' to override if you want to stop the assistant anyway.`,
106
+ );
107
+ process.exit(1);
108
+ }
109
+ }
110
+ }
111
+
112
+ const assistantStopped = await stopProcessByPidFile(
113
+ assistantPidFile,
114
+ "assistant",
115
+ );
116
+ if (!assistantStopped) {
45
117
  console.log("Assistant is not running.");
46
118
  } else {
47
119
  console.log("Assistant stopped.");
@@ -1,10 +1,7 @@
1
1
  import { existsSync, readFileSync } from "fs";
2
2
  import { join } from "path";
3
3
 
4
- import {
5
- defaultLocalResources,
6
- resolveTargetAssistant,
7
- } from "../lib/assistant-config.js";
4
+ import { resolveTargetAssistant } from "../lib/assistant-config.js";
8
5
  import { isProcessAlive, stopProcessByPidFile } from "../lib/process";
9
6
  import { startLocalDaemon, startGateway } from "../lib/local";
10
7
 
@@ -38,10 +35,15 @@ export async function wake(): Promise<void> {
38
35
  process.exit(1);
39
36
  }
40
37
 
41
- const resources = entry.resources ?? defaultLocalResources();
38
+ if (!entry.resources) {
39
+ console.error(
40
+ `Error: Local assistant '${entry.assistantId}' is missing resource configuration. Re-hatch to fix.`,
41
+ );
42
+ process.exit(1);
43
+ }
44
+ const resources = entry.resources;
42
45
 
43
46
  const pidFile = resources.pidFile;
44
- const socketFile = resources.socketPath;
45
47
 
46
48
  // Check if daemon is already running
47
49
  let daemonRunning = false;
@@ -57,7 +59,7 @@ export async function wake(): Promise<void> {
57
59
  console.log(
58
60
  `Assistant running (pid ${pid}) — restarting in watch mode...`,
59
61
  );
60
- await stopProcessByPidFile(pidFile, "assistant", [socketFile]);
62
+ await stopProcessByPidFile(pidFile, "assistant");
61
63
  daemonRunning = false;
62
64
  } else {
63
65
  console.log(`Assistant already running (pid ${pid}).`);