@mycadet/cli 0.13.0-dev.202610200020.9f20440 → 0.13.0-dev.202610201855.522ff0d

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 (80) hide show
  1. package/node_modules/@mycadet/assistant-api/constants/personality-sliders.test.ts +0 -8
  2. package/node_modules/@mycadet/assistant-api/constants/personality-sliders.ts +5 -41
  3. package/node_modules/@mycadet/assistant-api/events/message-complete.ts +0 -5
  4. package/node_modules/@mycadet/assistant-api/index.ts +0 -2
  5. package/node_modules/@mycadet/assistant-api/responses/conversation-message.ts +0 -10
  6. package/node_modules/@mycadet/environments/src/seeds.ts +6 -2
  7. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/environments/src/seeds.ts +6 -2
  8. package/node_modules/@mycadet/local-mode/src/config.ts +0 -2
  9. package/node_modules/@mycadet/local-mode/src/index.ts +1 -1
  10. package/node_modules/@mycadet/local-mode/src/lockfile-contract.test.ts +1 -22
  11. package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +26 -7
  12. package/node_modules/@mycadet/local-mode/src/util.ts +1 -20
  13. package/node_modules/@mycadet/shared/github-repository.ts +5 -0
  14. package/node_modules/@mycadet/shared/package.json +10 -0
  15. package/node_modules/@mycadet/shared/scripts/run-isolated-tests.ts +700 -0
  16. package/package.json +3 -2
  17. package/src/__tests__/app-command.test.ts +164 -0
  18. package/src/__tests__/client-web-config.test.ts +0 -1
  19. package/src/__tests__/cloud-releases.test.ts +1 -1
  20. package/src/__tests__/local-version.test.ts +16 -0
  21. package/src/__tests__/nginx-ingress.test.ts +248 -630
  22. package/src/__tests__/ps-cloud-status.test.ts +85 -11
  23. package/src/__tests__/root-process-lockfile.test.ts +29 -2
  24. package/src/__tests__/tunnel-holder-heal.test.ts +119 -0
  25. package/src/__tests__/tunnel-stop.test.ts +6 -13
  26. package/src/__tests__/tunnel.test.ts +50 -193
  27. package/src/__tests__/wake.test.ts +6 -38
  28. package/src/__tests__/web-dist.test.ts +267 -89
  29. package/src/commands/app.ts +113 -0
  30. package/src/commands/backup.ts +5 -4
  31. package/src/commands/clean.ts +2 -1
  32. package/src/commands/client.ts +5 -68
  33. package/src/commands/completions.ts +12 -1
  34. package/src/commands/connect.ts +2 -1
  35. package/src/commands/debug.ts +2 -1
  36. package/src/commands/exec.ts +7 -6
  37. package/src/commands/gateway/token.ts +2 -1
  38. package/src/commands/gateway.ts +2 -1
  39. package/src/commands/hatch.ts +74 -80
  40. package/src/commands/login.ts +7 -6
  41. package/src/commands/logs.ts +2 -1
  42. package/src/commands/ps.ts +136 -6
  43. package/src/commands/restart.ts +2 -1
  44. package/src/commands/restore.ts +10 -9
  45. package/src/commands/retire.ts +4 -3
  46. package/src/commands/setup.ts +5 -4
  47. package/src/commands/sleep.ts +3 -2
  48. package/src/commands/teleport.ts +2 -1
  49. package/src/commands/terminal.ts +7 -6
  50. package/src/commands/tunnel.ts +196 -242
  51. package/src/commands/uninstall.ts +2 -1
  52. package/src/commands/upgrade.ts +2 -1
  53. package/src/commands/use.ts +2 -1
  54. package/src/commands/wake.ts +3 -2
  55. package/src/index.ts +5 -4
  56. package/src/lib/__tests__/web-dist.test.ts +1 -27
  57. package/src/lib/assistant-config.ts +101 -37
  58. package/src/lib/cli-name.ts +14 -0
  59. package/src/lib/cloud-client.ts +2 -1
  60. package/src/lib/cloud-login-poll.ts +2 -1
  61. package/src/lib/cloud-releases.ts +1 -1
  62. package/src/lib/docker.ts +32 -21
  63. package/src/lib/hatch-local.ts +1 -3
  64. package/src/lib/hatch-next-steps.ts +1 -9
  65. package/src/lib/ingress-config.ts +0 -31
  66. package/src/lib/local-version.ts +25 -4
  67. package/src/lib/local.ts +34 -9
  68. package/src/lib/nginx-ingress.ts +143 -244
  69. package/src/lib/provider-secrets.ts +0 -5
  70. package/src/lib/statefulset.ts +0 -1
  71. package/src/lib/tunnel.ts +244 -43
  72. package/src/lib/tunnels/ngrok.ts +57 -9
  73. package/src/lib/tunnels/tailscale.ts +2 -1
  74. package/src/lib/web-dist.ts +134 -56
  75. package/src/__tests__/flags.test.ts +0 -318
  76. package/src/__tests__/shell-mode.test.ts +0 -83
  77. package/src/commands/flags.ts +0 -255
  78. package/src/lib/flag-args.test.ts +0 -84
  79. package/src/lib/flag-args.ts +0 -74
  80. package/src/lib/github-repository.ts +0 -2
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  afterAll,
3
3
  afterEach,
4
+ beforeEach,
4
5
  describe,
5
6
  expect,
6
7
  mock,
@@ -8,10 +9,18 @@ import {
8
9
  test,
9
10
  } from "bun:test";
10
11
  import * as childProcess from "node:child_process";
11
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
12
+ import {
13
+ mkdirSync,
14
+ mkdtempSync,
15
+ readFileSync,
16
+ rmSync,
17
+ writeFileSync,
18
+ } from "node:fs";
12
19
  import { tmpdir } from "node:os";
13
20
  import { join } from "node:path";
14
- import { resolveAppDistDir, resolveWebDistDir } from "../lib/web-dist.js";
21
+ import { PassThrough } from "node:stream";
22
+ import { readAppBuiltSha, stampAppBuiltSha } from "../lib/assistant-config.js";
23
+ import { resolveEdgeClientDists } from "../lib/web-dist.js";
15
24
 
16
25
  const realChildProcess = { ...childProcess };
17
26
  const spawnMock = mock(childProcess.spawn);
@@ -27,9 +36,26 @@ afterAll(() => {
27
36
  mock.module("node:child_process", () => realChildProcess);
28
37
  });
29
38
 
39
+ /**
40
+ * The HEAD sha the fixture's git boundary reports. null = a git-less tree
41
+ * (rev-parse fails), which keeps staleness detection inert.
42
+ */
43
+ let fixtureHeadSha: string | null = null;
44
+
45
+ // The built-sha stamp lives in the CLI lockfile now — each test redirects it
46
+ // to a temp dir (the root-process-lockfile.test.ts pattern).
47
+ const lockfileDirs: string[] = [];
48
+
49
+ beforeEach(() => {
50
+ lockfileDirs.push(mkdtempSync(join(tmpdir(), "web-dist-lockfile-")));
51
+ process.env.CADET_LOCKFILE_DIR = lockfileDirs.at(-1);
52
+ });
53
+
30
54
  afterEach(() => {
55
+ delete process.env.CADET_LOCKFILE_DIR;
31
56
  spawnMock.mockReset();
32
57
  spawnMock.mockImplementation(realChildProcess.spawn);
58
+ fixtureHeadSha = null;
33
59
  });
34
60
 
35
61
  const roots: string[] = [];
@@ -41,7 +67,7 @@ function makeRoot(): string {
41
67
  }
42
68
 
43
69
  afterAll(() => {
44
- for (const dir of roots.splice(0)) {
70
+ for (const dir of [...roots.splice(0), ...lockfileDirs.splice(0)]) {
45
71
  rmSync(dir, { recursive: true, force: true });
46
72
  }
47
73
  });
@@ -51,152 +77,304 @@ function resolvePackageThrows(): string {
51
77
  throw new Error("no npm package in this fixture");
52
78
  }
53
79
 
54
- /** A build spawn that must never fire: a build here is a bug. */
55
- function unexpectedSpawn(): typeof childProcess.spawn {
56
- return (() => {
57
- throw new Error("unexpected spawn");
58
- }) as unknown as typeof childProcess.spawn;
80
+ /** A git child that answers rev-parse with the fixture HEAD, or fails. */
81
+ function fakeGitChild(): childProcess.ChildProcess {
82
+ const child = new childProcess.ChildProcess();
83
+ if (fixtureHeadSha === null) {
84
+ Object.defineProperty(child, "stdout", { value: null });
85
+ setTimeout(() => child.emit("exit", 128), 0);
86
+ return child;
87
+ }
88
+ const stream = new PassThrough();
89
+ Object.defineProperty(child, "stdout", { value: stream });
90
+ setTimeout(() => {
91
+ stream.write(`${fixtureHeadSha}\n`);
92
+ stream.end();
93
+ child.emit("exit", 0);
94
+ }, 0);
95
+ return child;
59
96
  }
60
97
 
61
98
  /**
62
- * A build-spawn fake: optionally "lands" the bundle files where the real
63
- * build would write them, then exits with the given code.
99
+ * The process-boundary fake: git rev-parse answers with the fixture HEAD,
100
+ * and bun spawns run the fake build — optionally "landing" the bundle files
101
+ * where the real build would write them — then exit with the given code.
64
102
  */
65
- function fakeBuildSpawn(
66
- code: number,
67
- land?: () => void,
103
+ function fakeSpawn(
104
+ landBun?: () => void,
105
+ bunCode = 0,
68
106
  ): typeof childProcess.spawn {
69
- return (() => {
70
- land?.();
107
+ return ((command: string) => {
108
+ if (command === "git") {
109
+ return fakeGitChild();
110
+ }
111
+ landBun?.();
71
112
  const child = new childProcess.ChildProcess();
72
- setTimeout(() => child.emit("exit", code), 10);
113
+ setTimeout(() => child.emit("exit", bunCode), 10);
73
114
  return child;
74
115
  }) as unknown as typeof childProcess.spawn;
75
116
  }
76
117
 
77
- describe("resolveWebDistDir", () => {
78
- test("returns the source-checkout dist without building when present", async () => {
79
- const root = makeRoot();
80
- const distDir = join(root, "clients", "web", "dist");
81
- mkdirSync(distDir, { recursive: true });
82
- writeFileSync(join(distDir, "index.html"), "<html></html>");
83
- spawnMock.mockImplementation(unexpectedSpawn());
118
+ /** A build spawn that must never fire: a build here is a bug. */
119
+ function unexpectedSpawn(): typeof childProcess.spawn {
120
+ return (() => {
121
+ throw new Error("unexpected spawn");
122
+ }) as unknown as typeof childProcess.spawn;
123
+ }
84
124
 
85
- const resolved = await resolveWebDistDir({
125
+ function bunCalls(): Array<Parameters<typeof childProcess.spawn>> {
126
+ return spawnMock.mock.calls.filter(([command]) => command === "bun") as Array<
127
+ Parameters<typeof childProcess.spawn>
128
+ >;
129
+ }
130
+
131
+ function seedWebDist(packageDir: string): string {
132
+ const distDir = join(packageDir, "dist");
133
+ mkdirSync(distDir, { recursive: true });
134
+ writeFileSync(join(distDir, "index.html"), "<html></html>");
135
+ return distDir;
136
+ }
137
+
138
+ function seedAppDist(packageDir: string): string {
139
+ const distDir = join(packageDir, "dist", "standalone");
140
+ mkdirSync(distDir, { recursive: true });
141
+ writeFileSync(join(distDir, "index.html"), "<html></html>");
142
+ return distDir;
143
+ }
144
+
145
+ /** A fixture checkout with both client packages declared. */
146
+ function seedCheckout(): { root: string; web: string; app: string } {
147
+ const root = makeRoot();
148
+ const web = join(root, "clients", "web");
149
+ const app = join(root, "clients", "app");
150
+ mkdirSync(web, { recursive: true });
151
+ mkdirSync(app, { recursive: true });
152
+ writeFileSync(join(web, "package.json"), "{}");
153
+ writeFileSync(join(app, "package.json"), "{}");
154
+ return { root, web, app };
155
+ }
156
+
157
+ describe("resolveEdgeClientDists", () => {
158
+ test("returns both source-checkout dists without building when present", async () => {
159
+ const { root, web, app } = seedCheckout();
160
+ const webDist = seedWebDist(web);
161
+ const appDist = seedAppDist(app);
162
+ // git-less tree: staleness detection is inert, the dists serve as-is.
163
+ fixtureHeadSha = null;
164
+ spawnMock.mockImplementation(fakeSpawn());
165
+
166
+ const resolved = await resolveEdgeClientDists({
86
167
  startDir: root,
87
168
  resolvePackage: resolvePackageThrows,
88
169
  });
89
170
 
90
- expect(resolved).toBe(distDir);
91
- expect(spawnMock.mock.calls).toHaveLength(0);
171
+ expect(resolved).toEqual({ webDistDir: webDist, appDistDir: appDist });
172
+ expect(bunCalls()).toHaveLength(0);
92
173
  });
93
174
 
94
- test("builds the missing bundle once from the source checkout", async () => {
95
- const root = makeRoot();
96
- const packageDir = join(root, "clients", "web");
97
- mkdirSync(packageDir, { recursive: true });
98
- writeFileSync(join(packageDir, "package.json"), "{}");
175
+ test("builds both missing bundles once and stamps the lockfile's app record", async () => {
176
+ const { root, web, app } = seedCheckout();
177
+ fixtureHeadSha = "a".repeat(40);
99
178
  const logSpy = spyOn(console, "log");
100
179
  spawnMock.mockImplementation(
101
- fakeBuildSpawn(0, () => {
102
- mkdirSync(join(packageDir, "dist"), { recursive: true });
103
- writeFileSync(join(packageDir, "dist", "index.html"), "<html></html>");
180
+ fakeSpawn(() => {
181
+ seedWebDist(web);
182
+ seedAppDist(app);
104
183
  }),
105
184
  );
106
185
 
107
- const resolved = await resolveWebDistDir({
186
+ const resolved = await resolveEdgeClientDists({
187
+ startDir: root,
188
+ resolvePackage: resolvePackageThrows,
189
+ });
190
+
191
+ expect(resolved).toEqual({
192
+ webDistDir: join(web, "dist"),
193
+ appDistDir: join(app, "dist", "standalone"),
194
+ });
195
+ const builds = bunCalls();
196
+ expect(builds).toHaveLength(2);
197
+ for (const [command, args] of builds) {
198
+ expect(command).toBe("bun");
199
+ expect(args).toEqual(["run", "build"]);
200
+ }
201
+ // The stamp is the commit both bundles consumed, recorded on the
202
+ // lockfile's `app` entry.
203
+ expect(readAppBuiltSha()).toBe(fixtureHeadSha);
204
+ const rawLockfile = JSON.parse(
205
+ readFileSync(
206
+ join(process.env.CADET_LOCKFILE_DIR!, "lockfile.json"),
207
+ "utf-8",
208
+ ),
209
+ );
210
+ expect(rawLockfile.app.builtSha).toBe(fixtureHeadSha);
211
+ logSpy.mockRestore();
212
+
213
+ // The second resolve finds a fresh stamp and does not rebuild.
214
+ expect(
215
+ await resolveEdgeClientDists({
216
+ startDir: root,
217
+ resolvePackage: resolvePackageThrows,
218
+ }),
219
+ ).toEqual(resolved);
220
+ expect(bunCalls()).toHaveLength(2);
221
+ });
222
+
223
+ test("rebuilds both bundles when the checkout moved past the stamp", async () => {
224
+ const { root, web, app } = seedCheckout();
225
+ seedWebDist(web);
226
+ seedAppDist(app);
227
+ stampAppBuiltSha("b".repeat(40));
228
+ // The pull moved HEAD past the stamped build.
229
+ fixtureHeadSha = "c".repeat(40);
230
+ const logSpy = spyOn(console, "log");
231
+ spawnMock.mockImplementation(fakeSpawn());
232
+
233
+ const resolved = await resolveEdgeClientDists({
108
234
  startDir: root,
109
235
  resolvePackage: resolvePackageThrows,
110
236
  });
111
237
 
112
- expect(resolved).toBe(join(packageDir, "dist"));
238
+ expect(resolved.webDistDir).toBe(join(web, "dist"));
239
+ expect(bunCalls()).toHaveLength(2);
113
240
  expect(
114
241
  logSpy.mock.calls.some((call) =>
115
- call.join(" ").includes("No built web bundle found"),
242
+ call.join(" ").includes("older than the current checkout"),
116
243
  ),
117
244
  ).toBe(true);
118
245
  logSpy.mockRestore();
246
+ expect(readAppBuiltSha()).toBe(fixtureHeadSha);
247
+ });
119
248
 
120
- const [command, args, options] = spawnMock.mock.calls[0] ?? [];
121
- expect(command).toBe("bun");
122
- expect(args).toEqual(["run", "build"]);
123
- expect(options?.env?.VITE_CLOUD_MODE).toBe("false");
124
- expect(options?.cwd).toBe(packageDir);
249
+ test("serves fresh bundles without rebuilding", async () => {
250
+ const { root, web, app } = seedCheckout();
251
+ const webDist = seedWebDist(web);
252
+ const appDist = seedAppDist(app);
253
+ fixtureHeadSha = "d".repeat(40);
254
+ stampAppBuiltSha(fixtureHeadSha);
255
+ spawnMock.mockImplementation(fakeSpawn());
256
+
257
+ const resolved = await resolveEdgeClientDists({
258
+ startDir: root,
259
+ resolvePackage: resolvePackageThrows,
260
+ });
261
+
262
+ expect(resolved).toEqual({ webDistDir: webDist, appDistDir: appDist });
263
+ expect(bunCalls()).toHaveLength(0);
264
+ });
125
265
 
126
- // The second resolve finds the built bundle without rebuilding.
266
+ test("rebuilds bundles that predate the stamp once", async () => {
267
+ const { root, web, app } = seedCheckout();
268
+ seedWebDist(web);
269
+ seedAppDist(app);
270
+ fixtureHeadSha = "e".repeat(40);
271
+ const logSpy = spyOn(console, "log");
272
+ spawnMock.mockImplementation(fakeSpawn());
273
+
274
+ const resolved = await resolveEdgeClientDists({
275
+ startDir: root,
276
+ resolvePackage: resolvePackageThrows,
277
+ });
278
+
279
+ expect(resolved.webDistDir).toBe(join(web, "dist"));
280
+ expect(bunCalls()).toHaveLength(2);
127
281
  expect(
128
- await resolveWebDistDir({
129
- startDir: root,
130
- resolvePackage: resolvePackageThrows,
131
- }),
132
- ).toBe(join(packageDir, "dist"));
133
- expect(spawnMock.mock.calls).toHaveLength(1);
282
+ logSpy.mock.calls.some((call) =>
283
+ call.join(" ").includes("no build stamp"),
284
+ ),
285
+ ).toBe(true);
286
+ logSpy.mockRestore();
287
+ expect(readAppBuiltSha()).toBe(fixtureHeadSha);
134
288
  });
135
289
 
136
- test("returns null when there is no dist and no source package to build", async () => {
290
+ test("returns null dists when there is no dist and no source package", async () => {
137
291
  const root = makeRoot();
138
292
  spawnMock.mockImplementation(unexpectedSpawn());
139
293
 
140
- const resolved = await resolveWebDistDir({
294
+ const resolved = await resolveEdgeClientDists({
141
295
  startDir: root,
142
296
  resolvePackage: resolvePackageThrows,
143
297
  });
144
298
 
145
- expect(resolved).toBeNull();
299
+ expect(resolved).toEqual({ webDistDir: null, appDistDir: null });
146
300
  expect(spawnMock.mock.calls).toHaveLength(0);
147
301
  });
148
302
 
149
303
  test("reports a failed source build with the manual recipe", async () => {
150
- const root = makeRoot();
151
- const packageDir = join(root, "clients", "web");
152
- mkdirSync(packageDir, { recursive: true });
153
- writeFileSync(join(packageDir, "package.json"), "{}");
304
+ const { root } = seedCheckout();
305
+ fixtureHeadSha = "f".repeat(40);
154
306
  const logSpy = spyOn(console, "log");
155
- spawnMock.mockImplementation(fakeBuildSpawn(1));
307
+ spawnMock.mockImplementation(fakeSpawn(undefined, 1));
156
308
 
157
309
  await expect(
158
- resolveWebDistDir({
310
+ resolveEdgeClientDists({
159
311
  startDir: root,
160
312
  resolvePackage: resolvePackageThrows,
161
313
  }),
162
- ).rejects.toThrow("cd clients/web && VITE_CLOUD_MODE=false bun run build");
163
- expect(
164
- logSpy.mock.calls.some((call) =>
165
- call.join(" ").includes("No built web bundle found"),
166
- ),
167
- ).toBe(true);
314
+ ).rejects.toThrow("cd clients/web && bun run build");
168
315
  logSpy.mockRestore();
169
316
  });
170
- });
171
317
 
172
- describe("resolveAppDistDir", () => {
173
- test("mirrors the auto-build for the app shell bundle", async () => {
174
- const root = makeRoot();
175
- const packageDir = join(root, "clients", "app");
176
- mkdirSync(packageDir, { recursive: true });
177
- writeFileSync(join(packageDir, "package.json"), "{}");
178
- const logSpy = spyOn(console, "log");
179
- spawnMock.mockImplementation(
180
- fakeBuildSpawn(0, () => {
181
- mkdirSync(join(packageDir, "dist", "standalone"), { recursive: true });
182
- writeFileSync(
183
- join(packageDir, "dist", "standalone", "index.html"),
184
- "<html></html>",
185
- );
318
+ test("records the stamp only after both builds succeed", async () => {
319
+ const { root, web } = seedCheckout();
320
+ fixtureHeadSha = "1".repeat(40);
321
+ let builds = 0;
322
+ spawnMock.mockImplementation(((command: string) => {
323
+ if (command === "git") {
324
+ return fakeGitChild();
325
+ }
326
+ builds++;
327
+ const child = new childProcess.ChildProcess();
328
+ // The web build lands; the app build fails — a half-stamped state must
329
+ // never pretend the second bundle is fresh.
330
+ if (builds === 1) {
331
+ seedWebDist(web);
332
+ setTimeout(() => child.emit("exit", 0), 10);
333
+ } else {
334
+ setTimeout(() => child.emit("exit", 1), 10);
335
+ }
336
+ return child;
337
+ }) as unknown as typeof childProcess.spawn);
338
+
339
+ await expect(
340
+ resolveEdgeClientDists({
341
+ startDir: root,
342
+ resolvePackage: resolvePackageThrows,
186
343
  }),
187
- );
344
+ ).rejects.toThrow("Building the app bundle from source failed");
345
+
346
+ expect(readAppBuiltSha()).toBeUndefined();
347
+ });
188
348
 
189
- const resolved = await resolveAppDistDir({
349
+ test("leaves installed-package dists alone even with a stale source checkout", async () => {
350
+ const { root, web, app } = seedCheckout();
351
+ // The npm arm resolves first and owns the web dist; the source checkout
352
+ // may be as stale as it likes.
353
+ const npmWebDir = join(root, "npm", "@mycadet", "web");
354
+ seedWebDist(npmWebDir);
355
+ const appDist = seedAppDist(app);
356
+ seedWebDist(web);
357
+ fixtureHeadSha = "2".repeat(40);
358
+ // The source-built app arm is fresh at HEAD, so nothing rebuilds.
359
+ stampAppBuiltSha(fixtureHeadSha);
360
+ spawnMock.mockImplementation(((command: string) => {
361
+ // git HEAD is a legitimate probe (the app arm is source-built); a
362
+ // BUILD spawn here is a bug.
363
+ if (command === "git") {
364
+ return fakeGitChild();
365
+ }
366
+ throw new Error("unexpected spawn");
367
+ }) as unknown as typeof childProcess.spawn);
368
+
369
+ const resolved = await resolveEdgeClientDists({
190
370
  startDir: root,
191
- resolvePackage: resolvePackageThrows,
371
+ resolvePackage: () => join(npmWebDir, "package.json"),
192
372
  });
193
373
 
194
- expect(resolved).toBe(join(packageDir, "dist", "standalone"));
195
- logSpy.mockRestore();
196
-
197
- const [command, args, options] = spawnMock.mock.calls[0] ?? [];
198
- expect(command).toBe("bun");
199
- expect(args).toEqual(["run", "build"]);
200
- expect(options?.cwd).toBe(packageDir);
374
+ expect(resolved).toEqual({
375
+ webDistDir: join(npmWebDir, "dist"),
376
+ appDistDir: appDist,
377
+ });
378
+ expect(bunCalls()).toHaveLength(0);
201
379
  });
202
380
  });
@@ -0,0 +1,113 @@
1
+ import { loadAllAssistants } from "../lib/assistant-config";
2
+ import { getDefaultWorkspaceDir } from "../lib/ingress-config.js";
3
+ import {
4
+ ensureTunnelEdge,
5
+ isIngressRunning,
6
+ readIngressState,
7
+ stopIngressNginx,
8
+ type TunnelEdge,
9
+ } from "../lib/nginx-ingress.js";
10
+ import {
11
+ ensureNginxForEdge,
12
+ resolveLocalTunnelTarget,
13
+ toLocalTunnelTarget,
14
+ } from "../lib/tunnel.js";
15
+ import { cliCommand } from "../lib/cli-name.js";
16
+
17
+ function printAppHelp(): void {
18
+ console.log(
19
+ `Usage: ${cliCommand()} app start [options]\n ${cliCommand()} app stop`,
20
+ );
21
+ console.log("");
22
+ console.log(
23
+ "The App server hosts the assistant-agnostic frontend bundle, is the home",
24
+ );
25
+ console.log(
26
+ "for assistant management commands, and proxies all assistant-specific",
27
+ );
28
+ console.log("APIs & webhooks to the correct Cadet.");
29
+ console.log("");
30
+ console.log(
31
+ `The App server is spun up automatically as part of the \`${cliCommand()} tunnel start\` command.`,
32
+ );
33
+ process.exit(0);
34
+ }
35
+
36
+ /** `cadet app start`: bring the nginx edge up for the active local assistant. */
37
+ async function startAppEdge(): Promise<void> {
38
+ await ensureNginxForEdge();
39
+
40
+ const { entry, gatewayPort, workspaceDir } = resolveLocalTunnelTarget();
41
+
42
+ let edge: TunnelEdge;
43
+ try {
44
+ edge = await ensureTunnelEdge({
45
+ assistantId: entry.assistantId,
46
+ workspaceDir,
47
+ gatewayPort,
48
+ });
49
+ } catch (err) {
50
+ console.error(`Error: ${err instanceof Error ? err.message : String(err)}`);
51
+ process.exit(1);
52
+ }
53
+
54
+ console.log(
55
+ `${edge.started ? "Started" : "Reusing"} the app edge on 127.0.0.1:${edge.port} (app shell + remote web + webhooks).`,
56
+ );
57
+ console.log(`Stop with: ${cliCommand()} app stop`);
58
+ }
59
+
60
+ /**
61
+ * Workspaces that could hold a running edge: every locally reachable
62
+ * assistant's own, then the default one container assistants share. Usually
63
+ * exactly one is live.
64
+ */
65
+ function candidateEdgeWorkspaces(): string[] {
66
+ const workspaces: string[] = [];
67
+ for (const entry of loadAllAssistants()) {
68
+ const target = toLocalTunnelTarget(entry);
69
+ if (target && !workspaces.includes(target.workspaceDir)) {
70
+ workspaces.push(target.workspaceDir);
71
+ }
72
+ }
73
+ if (!workspaces.includes(getDefaultWorkspaceDir())) {
74
+ workspaces.push(getDefaultWorkspaceDir());
75
+ }
76
+ return workspaces;
77
+ }
78
+
79
+ /** `cadet app stop`: take the nginx edge down. */
80
+ async function stopAppEdge(): Promise<void> {
81
+ let stoppedAny = false;
82
+ for (const workspaceDir of candidateEdgeWorkspaces()) {
83
+ if (!isIngressRunning(workspaceDir)) continue;
84
+ const port = readIngressState(workspaceDir)?.listenPort;
85
+ if (await stopIngressNginx(workspaceDir)) {
86
+ stoppedAny = true;
87
+ console.log(`Stopped the app edge on 127.0.0.1:${port ?? "?"}.`);
88
+ } else {
89
+ console.error(
90
+ `Error: could not stop the app edge on 127.0.0.1:${port ?? "?"}. Check the nginx log: ${workspaceDir}/data/logs/nginx-ingress.log`,
91
+ );
92
+ process.exit(1);
93
+ }
94
+ }
95
+ if (!stoppedAny) {
96
+ console.log("No app edge is running.");
97
+ }
98
+ }
99
+
100
+ export const app = Object.assign(
101
+ async function app(): Promise<void> {
102
+ const subcommand = process.argv[3];
103
+ if (subcommand === "--help" || subcommand === "-h" || !subcommand) {
104
+ printAppHelp();
105
+ return;
106
+ }
107
+ if (subcommand === "start") return startAppEdge();
108
+ if (subcommand === "stop") return stopAppEdge();
109
+ console.error(`Error: Unknown argument '${subcommand}'.`);
110
+ printAppHelp();
111
+ },
112
+ { description: "Start or stop the local app edge (nginx)" },
113
+ );
@@ -1,4 +1,5 @@
1
1
  import { stat } from "node:fs/promises";
2
+ import { cliCommand } from "../lib/cli-name.js";
2
3
  import { join } from "node:path";
3
4
  import { findAssistantByName } from "../lib/assistant-config.js";
4
5
  import { formatSize, getBackupsDir } from "../lib/backup-ops.js";
@@ -11,25 +12,25 @@ export const backup = Object.assign(
11
12
  async function backup(): Promise<void> {
12
13
  const args = process.argv.slice(3);
13
14
  if (args.includes("--help") || args.includes("-h")) {
14
- console.log("Usage: cadet backup <name>");
15
+ console.log(`Usage: ${cliCommand()} backup <name>`);
15
16
  console.log(
16
17
  "Local/Docker: gracefully stop services, capture all persistent roots as tar.gz, then resume.",
17
18
  );
18
19
  console.log(
19
20
  "Cloud: create a backup through the cloud API using your cloud session.",
20
21
  );
21
- console.log("Example: cadet backup my-assistant");
22
+ console.log(`Example: ${cliCommand()} backup my-assistant`);
22
23
  return;
23
24
  }
24
25
  const name = args[0];
25
26
  if (!name || name.startsWith("-"))
26
- throw new Error("Usage: cadet backup <name>");
27
+ throw new Error(`Usage: ${cliCommand()} backup <name>`);
27
28
  if (args.length > 1) throw new Error(`Unknown backup option: ${args[1]}`);
28
29
  const entry = findAssistantByName(name);
29
30
  if (!entry) throw new Error(`No assistant found with name '${name}'.`);
30
31
  if (isCloudHome(entry.cloud)) {
31
32
  const token = readCloudToken();
32
- if (!token) throw new Error("Not logged in. Run 'cadet login' first.");
33
+ if (!token) throw new Error(`Not logged in. Run '${cliCommand()} login' first.`);
33
34
  await createPlatformBackup(entry, token);
34
35
  console.log("Cloud backup is ready.");
35
36
  return;
@@ -1,11 +1,12 @@
1
1
  import { detectOrphanedProcesses } from "../lib/orphan-detection";
2
+ import { cliCommand } from "../lib/cli-name.js";
2
3
  import { stopProcess } from "../lib/process";
3
4
 
4
5
  export const clean = Object.assign(
5
6
  async function clean(): Promise<void> {
6
7
  const args = process.argv.slice(3);
7
8
  if (args.includes("--help") || args.includes("-h")) {
8
- console.log("Usage: cadet clean");
9
+ console.log(`Usage: ${cliCommand()} clean`);
9
10
  console.log("");
10
11
  console.log(
11
12
  "Kill all orphaned cadet processes that are not tracked by any assistant.",