@mycadet/cli 0.13.0-dev.202610231952.fa65022 → 0.13.0-dev.202610241003.f9cbe80
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/node_modules/@mycadet/local-mode/src/__tests__/status.test.ts +0 -4
- package/node_modules/@mycadet/local-mode/src/index.ts +6 -2
- package/node_modules/@mycadet/local-mode/src/lockfile-contract.test.ts +0 -3
- package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +0 -2
- package/node_modules/@mycadet/local-mode/src/lockfile.test.ts +0 -31
- package/node_modules/@mycadet/local-mode/src/lockfile.ts +28 -48
- package/package.json +1 -1
- package/src/__tests__/app-command.test.ts +2 -2
- package/src/__tests__/assistant-client-cloud-auth.test.ts +2 -2
- package/src/__tests__/assistant-client-refresh.test.ts +2 -2
- package/src/__tests__/assistant-config.test.ts +28 -27
- package/src/__tests__/backup-ops.test.ts +4 -3
- package/src/__tests__/backup.test.ts +2 -2
- package/src/__tests__/client-tui-refresh.test.ts +3 -3
- package/src/__tests__/connect-import.test.ts +4 -52
- package/src/__tests__/debug.test.ts +1 -1
- package/src/__tests__/devices.test.ts +5 -6
- package/src/__tests__/gateway-token.test.ts +3 -3
- package/src/__tests__/helpers/lockfile-entry.ts +37 -0
- package/src/__tests__/host-teleport-docker.test.ts +2 -2
- package/src/__tests__/host-teleport.test.ts +15 -15
- package/src/__tests__/local-runtime-version.test.ts +4 -2
- package/src/__tests__/multi-local.test.ts +14 -25
- package/src/__tests__/nginx-ingress.test.ts +73 -5
- package/src/__tests__/orphan-detection.test.ts +13 -12
- package/src/__tests__/ps-cloud-status.test.ts +9 -9
- package/src/__tests__/restore.test.ts +32 -28
- package/src/__tests__/retire-local.test.ts +25 -29
- package/src/__tests__/retire.test.ts +33 -32
- package/src/__tests__/root-backup-docker.test.ts +2 -2
- package/src/__tests__/root-backup.test.ts +2 -3
- package/src/__tests__/root-restore-command.test.ts +2 -2
- package/src/__tests__/setup.test.ts +2 -5
- package/src/__tests__/signing-key-continuity.test.ts +2 -93
- package/src/__tests__/sleep.test.ts +13 -6
- package/src/__tests__/teleport-backup.test.ts +8 -2
- package/src/__tests__/teleport.test.ts +1 -1
- package/src/__tests__/tui-midsession-refresh.test.ts +4 -23
- package/src/__tests__/tunnel-stop.test.ts +2 -3
- package/src/__tests__/tunnel.test.ts +27 -16
- package/src/__tests__/uninstall.test.ts +31 -28
- package/src/__tests__/upgrade-local.test.ts +29 -15
- package/src/__tests__/upgrade-root-recovery.test.ts +2 -2
- package/src/__tests__/use.test.ts +16 -24
- package/src/__tests__/wake-error.test.ts +0 -1
- package/src/__tests__/wake.test.ts +22 -24
- package/src/commands/client.ts +5 -5
- package/src/commands/completions.test.ts +2 -2
- package/src/commands/debug.ts +13 -5
- package/src/commands/devices.ts +5 -5
- package/src/commands/gateway/token.ts +4 -4
- package/src/commands/hatch.ts +2 -2
- package/src/commands/login.ts +2 -2
- package/src/commands/logs.ts +4 -4
- package/src/commands/pair.ts +5 -5
- package/src/commands/ps.ts +23 -17
- package/src/commands/restore.ts +32 -20
- package/src/commands/retire.ts +6 -6
- package/src/commands/sleep.ts +117 -96
- package/src/commands/uninstall.ts +6 -4
- package/src/commands/upgrade.ts +200 -140
- package/src/commands/wake.ts +93 -93
- package/src/lib/__tests__/local-gateway-cli.test.ts +3 -3
- package/src/lib/__tests__/runtime-url.test.ts +1 -15
- package/src/lib/assistant-client.ts +5 -2
- package/src/lib/assistant-config.ts +43 -81
- package/src/lib/cloud-login.ts +12 -6
- package/src/lib/constants.ts +0 -1
- package/src/lib/docker.ts +11 -11
- package/src/lib/exec-apple-container.ts +2 -2
- package/src/lib/hatch-local.ts +12 -6
- package/src/lib/health-check.ts +123 -109
- package/src/lib/host-teleport.ts +31 -23
- package/src/lib/ingress-config.ts +2 -2
- package/src/lib/live-voice/connection.ts +2 -2
- package/src/lib/local.ts +76 -48
- package/src/lib/nginx-ingress.ts +41 -10
- package/src/lib/orphan-detection.ts +4 -3
- package/src/lib/retire-apple-container.ts +2 -2
- package/src/lib/retire-local.ts +11 -5
- package/src/lib/root-backup.ts +35 -21
- package/src/lib/runtime-url.ts +4 -4
- package/src/lib/ssh-apple-container.ts +2 -2
- package/src/lib/sync-cloud-assistants.ts +5 -5
- package/src/lib/teleport-backup.ts +15 -13
- package/src/lib/tunnel.ts +6 -6
- package/src/lib/upgrade-lifecycle.ts +36 -30
package/src/commands/wake.ts
CHANGED
|
@@ -7,11 +7,12 @@ import { cliCommand } from "../lib/cli-name.js";
|
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
9
|
getDaemonPidPath,
|
|
10
|
+
getLocalInstanceDir,
|
|
10
11
|
loadAllAssistants,
|
|
11
12
|
resolveTargetAssistant,
|
|
12
|
-
|
|
13
|
+
saveLockfileAssistant,
|
|
13
14
|
} from "../lib/assistant-config.js";
|
|
14
|
-
import type {
|
|
15
|
+
import type { LockfileAssistant } from "../lib/assistant-config.js";
|
|
15
16
|
import {
|
|
16
17
|
dockerResourceNames,
|
|
17
18
|
isNoSuchContainerError,
|
|
@@ -51,91 +52,91 @@ import {
|
|
|
51
52
|
|
|
52
53
|
export const wake = Object.assign(
|
|
53
54
|
async function wake(): Promise<void> {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
55
|
+
const args = process.argv.slice(3);
|
|
56
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
57
|
+
console.log(`Usage: ${cliCommand()} wake [<name> | --all] [options]`);
|
|
58
|
+
console.log("");
|
|
59
|
+
console.log("Start the assistant and gateway processes.");
|
|
60
|
+
console.log("");
|
|
61
|
+
console.log("Arguments:");
|
|
62
|
+
console.log(
|
|
63
|
+
" <name> Name of the assistant to start, defaults to active or only local",
|
|
64
|
+
);
|
|
65
|
+
console.log("");
|
|
66
|
+
console.log("Options:");
|
|
67
|
+
console.log(
|
|
68
|
+
" --all Start every assistant across all remotes instead of one",
|
|
69
|
+
);
|
|
70
|
+
console.log(
|
|
71
|
+
" --watch Run assistant and gateway in watch mode (hot reload on source changes)",
|
|
72
|
+
);
|
|
73
|
+
console.log(
|
|
74
|
+
" --foreground Run assistant in foreground with logs printed to terminal",
|
|
75
|
+
);
|
|
76
|
+
console.log(
|
|
77
|
+
" --repair-guardian Force-re-provision the guardian token (resets the\n" +
|
|
78
|
+
" gateway bootstrap and re-leases — REVOKES other device-bound\n" +
|
|
79
|
+
" tokens; replaces an unrecoverable signing key and restarts services only then.\n" +
|
|
80
|
+
" Clients may need to authenticate again. Use deliberately.)",
|
|
81
|
+
);
|
|
82
|
+
process.exit(0);
|
|
83
|
+
}
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
const watch = args.includes("--watch");
|
|
86
|
+
const foreground = args.includes("--foreground");
|
|
87
|
+
// Re-leasing the guardian token calls guardian/init, which revokes every
|
|
88
|
+
// other device-bound token (other tabs, other local clients on this machine).
|
|
89
|
+
// Gate it behind an explicit flag so the automatic connect-repair path
|
|
90
|
+
// (`runWake` spawns `wake <id>` with no flags) can never revoke a live session
|
|
91
|
+
// — it only ever restarts + sibling-seeds. A genuine spent-bootstrap brick is
|
|
92
|
+
// recovered deliberately via `cadet wake <id> --repair-guardian`.
|
|
93
|
+
const repairGuardian = args.includes("--repair-guardian");
|
|
94
|
+
const nameArg = args.find((a) => !a.startsWith("-"));
|
|
95
|
+
const all = args.includes("--all");
|
|
95
96
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
97
|
+
if (all && nameArg) {
|
|
98
|
+
console.error("Error: --all cannot be combined with an assistant name.");
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
if (all && (foreground || repairGuardian)) {
|
|
102
|
+
console.error(
|
|
103
|
+
"Error: --all cannot be combined with --foreground or --repair-guardian.",
|
|
104
|
+
);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
106
107
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
if (all) {
|
|
109
|
+
await wakeAll({ watch });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
111
112
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
113
|
+
const entry = resolveTargetAssistant(nameArg);
|
|
114
|
+
try {
|
|
115
|
+
await wakeAssistant(entry, { watch, foreground, repairGuardian });
|
|
116
|
+
} catch (err) {
|
|
117
|
+
if (err instanceof WakeExit) {
|
|
118
|
+
console.error(`Error: ${err.message}`);
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
throw err;
|
|
119
122
|
}
|
|
120
|
-
throw err;
|
|
121
|
-
}
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
// Foreground blocking is a single-assistant concern (--all refuses the
|
|
125
|
+
// flag), and the docker path already warned that foreground is ignored.
|
|
126
|
+
if (foreground && entry.cloud !== "docker") {
|
|
127
|
+
console.log("Running in the foreground, Ctrl+C to stop...\n");
|
|
128
|
+
// Block forever — the daemon is running with inherited stdio so its
|
|
129
|
+
// output streams to this terminal. When the user hits Ctrl+C, SIGINT
|
|
130
|
+
// propagates to the daemon child and both exit.
|
|
131
|
+
await new Promise<void>((resolve) => {
|
|
132
|
+
process.on("SIGINT", () => {
|
|
133
|
+
console.log("\nShutting down...");
|
|
134
|
+
resolve();
|
|
135
|
+
});
|
|
136
|
+
process.on("SIGTERM", () => resolve());
|
|
134
137
|
});
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
},
|
|
138
|
+
}
|
|
139
|
+
},
|
|
139
140
|
{ description: "Start the assistant and gateway" },
|
|
140
141
|
);
|
|
141
142
|
|
|
@@ -148,7 +149,9 @@ export const wake = Object.assign(
|
|
|
148
149
|
async function wakeAll(opts: { watch: boolean }): Promise<void> {
|
|
149
150
|
const entries = loadAllAssistants();
|
|
150
151
|
if (entries.length === 0) {
|
|
151
|
-
console.error(
|
|
152
|
+
console.error(
|
|
153
|
+
`Error: no assistants found. Run '${cliCommand()} hatch' first.`,
|
|
154
|
+
);
|
|
152
155
|
process.exit(1);
|
|
153
156
|
}
|
|
154
157
|
|
|
@@ -203,7 +206,7 @@ class WakeExit extends Error {}
|
|
|
203
206
|
* continuing to the next entry (`--all`).
|
|
204
207
|
*/
|
|
205
208
|
export async function wakeAssistant(
|
|
206
|
-
entry:
|
|
209
|
+
entry: LockfileAssistant,
|
|
207
210
|
opts: WakeAssistantOptions,
|
|
208
211
|
): Promise<void> {
|
|
209
212
|
const { watch, foreground, repairGuardian } = opts;
|
|
@@ -213,9 +216,7 @@ export async function wakeAssistant(
|
|
|
213
216
|
const ignored = [watch && "--watch", foreground && "--foreground"]
|
|
214
217
|
.filter(Boolean)
|
|
215
218
|
.join(" and ");
|
|
216
|
-
console.warn(
|
|
217
|
-
`Warning: ${ignored} ignored for Docker instances.`,
|
|
218
|
-
);
|
|
219
|
+
console.warn(`Warning: ${ignored} ignored for Docker instances.`);
|
|
219
220
|
}
|
|
220
221
|
const res = dockerResourceNames(entry.assistantId);
|
|
221
222
|
try {
|
|
@@ -279,7 +280,7 @@ export async function wakeAssistant(
|
|
|
279
280
|
if (replacingSigningKey) {
|
|
280
281
|
for (const [file, label, port] of [
|
|
281
282
|
[
|
|
282
|
-
join(resources
|
|
283
|
+
join(getLocalInstanceDir(resources), "gateway.pid"),
|
|
283
284
|
"gateway",
|
|
284
285
|
resources.gatewayPort,
|
|
285
286
|
],
|
|
@@ -308,7 +309,7 @@ export async function wakeAssistant(
|
|
|
308
309
|
}
|
|
309
310
|
if (replacingSigningKey || signingKey !== resources.signingKey) {
|
|
310
311
|
resources.signingKey = signingKey;
|
|
311
|
-
|
|
312
|
+
saveLockfileAssistant(entry);
|
|
312
313
|
}
|
|
313
314
|
|
|
314
315
|
const pidFile = getDaemonPidPath(resources);
|
|
@@ -380,7 +381,7 @@ export async function wakeAssistant(
|
|
|
380
381
|
if (!bootstrapSecret) {
|
|
381
382
|
bootstrapSecret = generateLocalSigningKey();
|
|
382
383
|
entry.guardianBootstrapSecret = bootstrapSecret;
|
|
383
|
-
|
|
384
|
+
saveLockfileAssistant(entry);
|
|
384
385
|
bootstrapSecretBackfilled = true;
|
|
385
386
|
}
|
|
386
387
|
|
|
@@ -420,7 +421,7 @@ export async function wakeAssistant(
|
|
|
420
421
|
// means credential operations will fail until the next wake. Relaunch the
|
|
421
422
|
// sibling so the daemon's lazy reconnect (secure-keys.ts) picks it up on
|
|
422
423
|
// the next credential read. startCes always launches the sibling.
|
|
423
|
-
const cadetDir = resources
|
|
424
|
+
const cadetDir = getLocalInstanceDir(resources);
|
|
424
425
|
const cesPidFile = join(cadetDir, "ces.pid");
|
|
425
426
|
const cesAlive = isProcessAlive(cesPidFile).alive;
|
|
426
427
|
if (!cesAlive) {
|
|
@@ -432,7 +433,7 @@ export async function wakeAssistant(
|
|
|
432
433
|
// Start gateway
|
|
433
434
|
let gatewayStarted = false;
|
|
434
435
|
{
|
|
435
|
-
const cadetDir = resources
|
|
436
|
+
const cadetDir = getLocalInstanceDir(resources);
|
|
436
437
|
const gatewayPidFile = join(cadetDir, "gateway.pid");
|
|
437
438
|
const gatewayState = await resolveProcessState(
|
|
438
439
|
gatewayPidFile,
|
|
@@ -564,7 +565,7 @@ export async function wakeAssistant(
|
|
|
564
565
|
await new Promise((r) => setTimeout(r, 2000 * 2 ** (attempt - 1)));
|
|
565
566
|
} else {
|
|
566
567
|
console.warn(
|
|
567
|
-
` Guardian token re-provision failed${err instanceof GuardianLeasePermanentError ?", not retrying — a malformed 200 will not fix itself" : ` after ${maxAttempts} attempts`}: ${err}`,
|
|
568
|
+
` Guardian token re-provision failed${err instanceof GuardianLeasePermanentError ? ", not retrying — a malformed 200 will not fix itself" : ` after ${maxAttempts} attempts`}: ${err}`,
|
|
568
569
|
);
|
|
569
570
|
// The user explicitly confirmed this destructive repair — a
|
|
570
571
|
// success exit here would make callers (the web recovery flow)
|
|
@@ -579,7 +580,7 @@ export async function wakeAssistant(
|
|
|
579
580
|
// Restore the nginx edge and point the auto-tunnel at it. The spawned agent
|
|
580
581
|
// records itself in the root lockfile under `tunnel`. Non-fatal: a down
|
|
581
582
|
// edge is a degraded remote-web/webhook path, not a broken assistant.
|
|
582
|
-
const workspaceDir = join(resources
|
|
583
|
+
const workspaceDir = join(getLocalInstanceDir(resources), "workspace");
|
|
583
584
|
await restoreAppServerAndAutoTunnel(
|
|
584
585
|
entry.assistantId,
|
|
585
586
|
resources.gatewayPort,
|
|
@@ -597,4 +598,3 @@ export async function wakeAssistant(
|
|
|
597
598
|
}
|
|
598
599
|
console.log("Wake complete.");
|
|
599
600
|
}
|
|
600
|
-
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { LocalAssistantResources } from "@mycadet/local-mode/contract";
|
|
4
4
|
import { gatewayCliInvocation } from "../local.js";
|
|
5
5
|
|
|
6
|
-
function fakeResources(instanceDir: string):
|
|
6
|
+
function fakeResources(instanceDir: string): LocalAssistantResources {
|
|
7
7
|
// gatewayCliInvocation only reads instanceDir; the rest of the resources
|
|
8
8
|
// shape is irrelevant here.
|
|
9
|
-
return { instanceDir
|
|
9
|
+
return { instanceDir, daemonPort: 0, gatewayPort: 0 };
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
describe("gatewayCliInvocation", () => {
|
|
@@ -1,21 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
|
|
3
|
-
import type { AssistantEntry } from "../assistant-config.js";
|
|
4
3
|
import { resolveRuntimeUrl } from "../runtime-url.js";
|
|
5
|
-
|
|
6
|
-
function makeEntry(
|
|
7
|
-
overrides: Partial<AssistantEntry> & {
|
|
8
|
-
cloud: string;
|
|
9
|
-
runtimeUrl: string;
|
|
10
|
-
assistantId: string;
|
|
11
|
-
},
|
|
12
|
-
): Pick<AssistantEntry, "cloud" | "runtimeUrl" | "assistantId"> {
|
|
13
|
-
return {
|
|
14
|
-
cloud: overrides.cloud,
|
|
15
|
-
runtimeUrl: overrides.runtimeUrl,
|
|
16
|
-
assistantId: overrides.assistantId,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
4
|
+
import { makeEntry } from "../../__tests__/helpers/lockfile-entry.js";
|
|
19
5
|
|
|
20
6
|
describe("resolveRuntimeUrl", () => {
|
|
21
7
|
test("local cloud uses gateway-loopback /v1/<subpath>", () => {
|
|
@@ -102,9 +102,12 @@ export class AssistantClient {
|
|
|
102
102
|
|
|
103
103
|
this._assistantId = entry.assistantId;
|
|
104
104
|
const cloudManaged = !opts?.sessionToken && isCloudHome(entry.cloud);
|
|
105
|
-
const cloudHost =
|
|
105
|
+
const cloudHost = entry.runtimeUrl
|
|
106
|
+
? stripTrailingSlashes(entry.runtimeUrl)
|
|
107
|
+
: undefined;
|
|
106
108
|
|
|
107
|
-
this.runtimeUrl =
|
|
109
|
+
this.runtimeUrl =
|
|
110
|
+
cloudManaged && cloudHost ? cloudHost : getGatewayUrl(entry);
|
|
108
111
|
|
|
109
112
|
if (opts?.sessionToken) {
|
|
110
113
|
// Caller supplied the credential: X-Session-Token + Cadet-Organization-Id.
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
type TunnelProcessRecord,
|
|
21
21
|
} from "@mycadet/local-mode/contract";
|
|
22
22
|
|
|
23
|
-
export type { AppProcessRecord, TunnelProcessRecord };
|
|
23
|
+
export type { AppProcessRecord, LockfileAssistant, TunnelProcessRecord };
|
|
24
24
|
|
|
25
25
|
import { crossEnvironmentAssistantHint } from "./environments/detect.js";
|
|
26
26
|
import {
|
|
@@ -36,43 +36,15 @@ import { probePort } from "./port-probe.js";
|
|
|
36
36
|
/**
|
|
37
37
|
* Per-instance resource paths and ports. Each local assistant instance gets
|
|
38
38
|
* its own directory tree, ports, and socket so multiple instances can run
|
|
39
|
-
* side-by-side without conflicts. The contract models every field
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* present on entries this CLI wrote.
|
|
39
|
+
* side-by-side without conflicts. The contract models every field, optional
|
|
40
|
+
* fields are optional everywhere, and code that needs one guards once and
|
|
41
|
+
* falls back (see {@link getLocalInstanceDir} / {@link getDaemonPidPath}).
|
|
43
42
|
*/
|
|
44
|
-
export type LocalInstanceResources = LocalAssistantResources & {
|
|
45
|
-
/**
|
|
46
|
-
* Instance-specific data root. New local assistants are placed under
|
|
47
|
-
* `$XDG_DATA_HOME/cadet{-env}/assistants/<name>/` with state flattened
|
|
48
|
-
* directly into it. Legacy entries (pre env-data-layout) may still point
|
|
49
|
-
* at `~` — the read path honors whatever `instanceDir` is stored, with
|
|
50
|
-
* state in `~/.cadet` for those.
|
|
51
|
-
*/
|
|
52
|
-
instanceDir: string;
|
|
53
|
-
qdrantPort: number;
|
|
54
|
-
cesPort: number;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The CLI's full view of an assistant entry: the one canonical Zod contract
|
|
59
|
-
* (`LockfileAssistant` — strict; every field the CLI reads and writes is
|
|
60
|
-
* modeled there) plus the two narrowings the CLI's load path guarantees.
|
|
61
|
-
* `runtimeUrl` is required (`loadAllAssistants` only returns entries that
|
|
62
|
-
* have one) and `resources` carries the host's required `instanceDir`.
|
|
63
|
-
* `cloud` is always present (normalized at the read seam — see
|
|
64
|
-
* `loadAllAssistants`).
|
|
65
|
-
*/
|
|
66
|
-
export type AssistantEntry = LockfileAssistant & {
|
|
67
|
-
runtimeUrl: string;
|
|
68
|
-
/** Per-instance resource config. Present for local entries in multi-instance setups. */
|
|
69
|
-
resources?: LocalInstanceResources;
|
|
70
|
-
};
|
|
71
43
|
|
|
72
44
|
export type AssistantLookupResult =
|
|
73
|
-
| { status: "found"; entry:
|
|
45
|
+
| { status: "found"; entry: LockfileAssistant }
|
|
74
46
|
| { status: "not_found" }
|
|
75
|
-
| { status: "ambiguous"; matches:
|
|
47
|
+
| { status: "ambiguous"; matches: LockfileAssistant[] };
|
|
76
48
|
|
|
77
49
|
/** Either lockfile-root process record, keyed by `RootProcessKind`. */
|
|
78
50
|
export type RootProcessRecord = TunnelProcessRecord | AppProcessRecord;
|
|
@@ -87,15 +59,22 @@ export type RootProcessKind = keyof RootProcessRecords;
|
|
|
87
59
|
|
|
88
60
|
/**
|
|
89
61
|
* Derive the daemon PID file path from a resources object. The PID file
|
|
90
|
-
* lives inside the instance's workspace directory.
|
|
91
|
-
*
|
|
62
|
+
* lives inside the instance's workspace directory. Falls back to the XDG
|
|
63
|
+
* data root's workspace when the entry carries no instance dir
|
|
92
64
|
* (`$XDG_DATA_HOME/cadet[-<env>]/workspace/cadet.pid`).
|
|
93
65
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
66
|
+
/**
|
|
67
|
+
* The instance's data root: the recorded instanceDir, or the shared state
|
|
68
|
+
* root for legacy entries predating per-instance dirs.
|
|
69
|
+
*/
|
|
70
|
+
export function getLocalInstanceDir(
|
|
71
|
+
resources?: LocalAssistantResources,
|
|
72
|
+
): string {
|
|
73
|
+
return resources?.instanceDir ?? getStateDir(getCurrentEnvironment());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function getDaemonPidPath(resources?: LocalAssistantResources): string {
|
|
77
|
+
return join(getLocalInstanceDir(resources), "workspace", "cadet.pid");
|
|
99
78
|
}
|
|
100
79
|
|
|
101
80
|
function readLockfile(): Lockfile {
|
|
@@ -241,20 +220,11 @@ export function clearAppProcessRecord(expectedPid?: number): void {
|
|
|
241
220
|
* Returns undefined if the URL is malformed or has no explicit port.
|
|
242
221
|
*/
|
|
243
222
|
|
|
244
|
-
export function loadAllAssistants():
|
|
245
|
-
|
|
246
|
-
return data.assistants.filter(isAssistantEntry);
|
|
223
|
+
export function loadAllAssistants(): LockfileAssistant[] {
|
|
224
|
+
return readLockfile().assistants;
|
|
247
225
|
}
|
|
248
226
|
|
|
249
|
-
|
|
250
|
-
* `assistantId` is the schema's job; `runtimeUrl` stays a plain read because
|
|
251
|
-
* only entries that carry one are addressable.
|
|
252
|
-
*/
|
|
253
|
-
function isAssistantEntry(entry: LockfileAssistant): entry is AssistantEntry {
|
|
254
|
-
return typeof entry.runtimeUrl === "string";
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function writeAssistants(entries: AssistantEntry[]): void {
|
|
227
|
+
function writeAssistants(entries: LockfileAssistant[]): void {
|
|
258
228
|
withCliLockfileLock(() => {
|
|
259
229
|
const data = readLockfile();
|
|
260
230
|
data.assistants = entries;
|
|
@@ -262,7 +232,7 @@ function writeAssistants(entries: AssistantEntry[]): void {
|
|
|
262
232
|
});
|
|
263
233
|
}
|
|
264
234
|
|
|
265
|
-
export function loadLatestAssistant():
|
|
235
|
+
export function loadLatestAssistant(): LockfileAssistant | null {
|
|
266
236
|
const entries = loadAllAssistants();
|
|
267
237
|
if (entries.length === 0) {
|
|
268
238
|
return null;
|
|
@@ -275,17 +245,17 @@ export function loadLatestAssistant(): AssistantEntry | null {
|
|
|
275
245
|
return sorted[0];
|
|
276
246
|
}
|
|
277
247
|
|
|
278
|
-
export function findAssistantByName(name: string):
|
|
248
|
+
export function findAssistantByName(name: string): LockfileAssistant | null {
|
|
279
249
|
return (
|
|
280
250
|
loadAllAssistants().find((entry) => entry.assistantId === name) ?? null
|
|
281
251
|
);
|
|
282
252
|
}
|
|
283
253
|
|
|
284
|
-
export function getAssistantDisplayName(entry:
|
|
254
|
+
export function getAssistantDisplayName(entry: LockfileAssistant): string {
|
|
285
255
|
return entry.name?.trim() || entry.assistantId;
|
|
286
256
|
}
|
|
287
257
|
|
|
288
|
-
function getAssistantDisplayNameCandidates(entry:
|
|
258
|
+
function getAssistantDisplayNameCandidates(entry: LockfileAssistant): string[] {
|
|
289
259
|
const name = entry.name?.trim();
|
|
290
260
|
return name ? [name] : [];
|
|
291
261
|
}
|
|
@@ -328,7 +298,7 @@ export function formatAssistantLookupError(
|
|
|
328
298
|
return `No assistant found with name or ID '${identifier}'.${crossEnvironmentAssistantHint() ?? ""}`;
|
|
329
299
|
}
|
|
330
300
|
|
|
331
|
-
export function
|
|
301
|
+
export function removeLockfileAssistant(assistantId: string): void {
|
|
332
302
|
withCliLockfileLock(() => {
|
|
333
303
|
const data = readLockfile();
|
|
334
304
|
const entries = data.assistants.filter(
|
|
@@ -390,11 +360,11 @@ function readLockfileForEnv(env: EnvironmentDefinition): Lockfile {
|
|
|
390
360
|
*/
|
|
391
361
|
export function loadAllAssistantsAcrossEnvs(
|
|
392
362
|
envs?: EnvironmentDefinition[],
|
|
393
|
-
):
|
|
363
|
+
): LockfileAssistant[] {
|
|
394
364
|
const envList = envs ?? Object.values(SEEDS).map((env) => ({ ...env }));
|
|
395
|
-
const all:
|
|
365
|
+
const all: LockfileAssistant[] = [];
|
|
396
366
|
for (const env of envList) {
|
|
397
|
-
all.push(...readLockfileForEnv(env).assistants
|
|
367
|
+
all.push(...readLockfileForEnv(env).assistants);
|
|
398
368
|
}
|
|
399
369
|
return all;
|
|
400
370
|
}
|
|
@@ -421,7 +391,7 @@ export function setActiveAssistant(assistantId: string): void {
|
|
|
421
391
|
* 2. Active assistant set via `cadet use`
|
|
422
392
|
* 3. Sole lockfile entry (any cloud)
|
|
423
393
|
*/
|
|
424
|
-
export function resolveAssistant(nameArg?: string):
|
|
394
|
+
export function resolveAssistant(nameArg?: string): LockfileAssistant | null {
|
|
425
395
|
if (nameArg) {
|
|
426
396
|
return findAssistantByName(nameArg);
|
|
427
397
|
}
|
|
@@ -448,7 +418,7 @@ export function resolveAssistant(nameArg?: string): AssistantEntry | null {
|
|
|
448
418
|
* 2. Active assistant set via `cadet use`
|
|
449
419
|
* 3. Sole lockfile entry (any cloud)
|
|
450
420
|
*/
|
|
451
|
-
export function resolveTargetAssistant(nameArg?: string):
|
|
421
|
+
export function resolveTargetAssistant(nameArg?: string): LockfileAssistant {
|
|
452
422
|
if (nameArg) {
|
|
453
423
|
const result = lookupAssistantByIdentifier(nameArg);
|
|
454
424
|
if (result.status === "found") return result.entry;
|
|
@@ -486,7 +456,8 @@ export function resolveTargetAssistant(nameArg?: string): AssistantEntry {
|
|
|
486
456
|
* entry's recorded runtime URL. One seam so the runtimeUrl death (a later
|
|
487
457
|
* PR) rewires a single site instead of every ssh flow.
|
|
488
458
|
*/
|
|
489
|
-
export function extractHostFromEntry(entry:
|
|
459
|
+
export function extractHostFromEntry(entry: LockfileAssistant): string {
|
|
460
|
+
if (!entry.runtimeUrl) return "";
|
|
490
461
|
try {
|
|
491
462
|
return new URL(entry.runtimeUrl).hostname;
|
|
492
463
|
} catch {
|
|
@@ -494,7 +465,7 @@ export function extractHostFromEntry(entry: AssistantEntry): string {
|
|
|
494
465
|
}
|
|
495
466
|
}
|
|
496
467
|
|
|
497
|
-
export function
|
|
468
|
+
export function saveLockfileAssistant(entry: LockfileAssistant): void {
|
|
498
469
|
withCliLockfileLock(() => {
|
|
499
470
|
const entries = loadAllAssistants().filter(
|
|
500
471
|
(e) => e.assistantId !== entry.assistantId,
|
|
@@ -511,10 +482,10 @@ export function saveAssistantEntry(entry: AssistantEntry): void {
|
|
|
511
482
|
* conversation key so creation is retry-safe). Minted once and persisted
|
|
512
483
|
* on the entry so every session shares one conversation.
|
|
513
484
|
*/
|
|
514
|
-
export function resolveConversationKey(entry:
|
|
485
|
+
export function resolveConversationKey(entry: LockfileAssistant): string {
|
|
515
486
|
if (entry.conversationKey) return entry.conversationKey;
|
|
516
487
|
const minted = crypto.randomUUID();
|
|
517
|
-
|
|
488
|
+
saveLockfileAssistant({ ...entry, conversationKey: minted });
|
|
518
489
|
return minted;
|
|
519
490
|
}
|
|
520
491
|
|
|
@@ -550,7 +521,7 @@ async function findAvailablePort(
|
|
|
550
521
|
*/
|
|
551
522
|
export async function allocateLocalResources(
|
|
552
523
|
instanceName: string,
|
|
553
|
-
): Promise<
|
|
524
|
+
): Promise<LocalAssistantResources> {
|
|
554
525
|
const env = getCurrentEnvironment();
|
|
555
526
|
const instanceDir = join(getMultiInstanceDir(env), instanceName);
|
|
556
527
|
mkdirSync(instanceDir, { recursive: true });
|
|
@@ -559,12 +530,10 @@ export async function allocateLocalResources(
|
|
|
559
530
|
const reservedPorts: number[] = [];
|
|
560
531
|
for (const entry of loadAllAssistants()) {
|
|
561
532
|
if (entry.cloud !== "local" || !entry.resources) continue;
|
|
562
|
-
reservedPorts.push(
|
|
563
|
-
|
|
564
|
-
entry.resources.
|
|
565
|
-
|
|
566
|
-
entry.resources.cesPort,
|
|
567
|
-
);
|
|
533
|
+
reservedPorts.push(entry.resources.daemonPort, entry.resources.gatewayPort);
|
|
534
|
+
if (entry.resources.cesPort !== undefined) {
|
|
535
|
+
reservedPorts.push(entry.resources.cesPort);
|
|
536
|
+
}
|
|
568
537
|
}
|
|
569
538
|
|
|
570
539
|
// Env-aware bases: non-prod envs sit in their own 1000-port window so
|
|
@@ -576,23 +545,16 @@ export async function allocateLocalResources(
|
|
|
576
545
|
...reservedPorts,
|
|
577
546
|
daemonPort,
|
|
578
547
|
]);
|
|
579
|
-
const qdrantPort = await findAvailablePort(basePorts.qdrant, [
|
|
580
|
-
...reservedPorts,
|
|
581
|
-
daemonPort,
|
|
582
|
-
gatewayPort,
|
|
583
|
-
]);
|
|
584
548
|
const cesPort = await findAvailablePort(basePorts.ces, [
|
|
585
549
|
...reservedPorts,
|
|
586
550
|
daemonPort,
|
|
587
551
|
gatewayPort,
|
|
588
|
-
qdrantPort,
|
|
589
552
|
]);
|
|
590
553
|
|
|
591
554
|
return {
|
|
592
555
|
instanceDir,
|
|
593
556
|
daemonPort,
|
|
594
557
|
gatewayPort,
|
|
595
|
-
qdrantPort,
|
|
596
558
|
cesPort,
|
|
597
559
|
};
|
|
598
560
|
}
|
package/src/lib/cloud-login.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type { AddressInfo } from "net";
|
|
|
14
14
|
import { isCloudHome } from "./cloud-values.js";
|
|
15
15
|
import {
|
|
16
16
|
resolveAssistant,
|
|
17
|
-
|
|
17
|
+
saveLockfileAssistant,
|
|
18
18
|
} from "./assistant-config.js";
|
|
19
19
|
import { resolveAssistantApiKeyForInjection } from "./assistant-api-key-resolution.js";
|
|
20
20
|
import { computeDeviceId } from "./guardian-token.js";
|
|
@@ -361,11 +361,17 @@ export async function registerLocalAssistantWithPlatform(
|
|
|
361
361
|
// Skip managed ("cadet") assistants — they are handled by the cloud.
|
|
362
362
|
if (!entry || isCloudHome(entry.cloud)) return;
|
|
363
363
|
|
|
364
|
+
const gatewayUrl = entry.runtimeUrl;
|
|
365
|
+
if (!gatewayUrl) {
|
|
366
|
+
log(`Assistant '${entry.assistantId}' has no recorded gateway URL; skipping credential injection.`);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
364
370
|
const orgId = await fetchOrganizationId(token);
|
|
365
371
|
const clientInstallationId = computeDeviceId();
|
|
366
372
|
const [assistantVersion, ingressUrl] = await Promise.all([
|
|
367
|
-
fetchCurrentVersion(
|
|
368
|
-
fetchAssistantIngressUrl(
|
|
373
|
+
fetchCurrentVersion(gatewayUrl),
|
|
374
|
+
fetchAssistantIngressUrl(gatewayUrl, options.bearerToken ?? entry.bearerToken),
|
|
369
375
|
]);
|
|
370
376
|
const cloudUrl = getCloudUrl();
|
|
371
377
|
const registration = await ensureSelfHostedLocalRegistration(
|
|
@@ -378,7 +384,7 @@ export async function registerLocalAssistantWithPlatform(
|
|
|
378
384
|
cloudUrl,
|
|
379
385
|
ingressUrl,
|
|
380
386
|
);
|
|
381
|
-
|
|
387
|
+
saveLockfileAssistant({
|
|
382
388
|
...entry,
|
|
383
389
|
cloudAssistantId: registration.assistant.id,
|
|
384
390
|
cloudBaseUrl: cloudUrl,
|
|
@@ -392,7 +398,7 @@ export async function registerLocalAssistantWithPlatform(
|
|
|
392
398
|
// the two injection paths cannot drift.
|
|
393
399
|
const resolved = await resolveAssistantApiKeyForInjection({
|
|
394
400
|
registrationApiKey: registration.assistant_api_key,
|
|
395
|
-
runtimeUrl:
|
|
401
|
+
runtimeUrl: gatewayUrl,
|
|
396
402
|
bearerToken: options.bearerToken ?? entry.bearerToken,
|
|
397
403
|
token,
|
|
398
404
|
organizationId: orgId,
|
|
@@ -408,7 +414,7 @@ export async function registerLocalAssistantWithPlatform(
|
|
|
408
414
|
// Inject credentials into the running assistant via the gateway,
|
|
409
415
|
// mirroring the desktop app's LocalAssistantBootstrapService flow.
|
|
410
416
|
const allInjected = await injectCredentialsIntoAssistant({
|
|
411
|
-
gatewayUrl:
|
|
417
|
+
gatewayUrl: gatewayUrl,
|
|
412
418
|
bearerToken: options.bearerToken ?? entry.bearerToken,
|
|
413
419
|
assistantApiKey,
|
|
414
420
|
cloudAssistantId: registration.assistant.id,
|
package/src/lib/constants.ts
CHANGED
|
@@ -13,7 +13,6 @@ export const GATEWAY_PORT = process.env.GATEWAY_PORT
|
|
|
13
13
|
/** Default ports used as scan start points for multi-instance allocation. */
|
|
14
14
|
export const DEFAULT_DAEMON_PORT = 7821;
|
|
15
15
|
export const DEFAULT_GATEWAY_PORT = 7830;
|
|
16
|
-
export const DEFAULT_QDRANT_PORT = 6333;
|
|
17
16
|
export const DEFAULT_CES_PORT = 8090;
|
|
18
17
|
|
|
19
18
|
export const VALID_REMOTE_HOSTS = [
|