@mycadet/cli 0.13.0-dev.202611011557.373f3d6 → 0.13.0-dev.202611011818.51165c0
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/assistant-api/node_modules/@mycadet/service-contracts/package.json +1 -1
- package/node_modules/@mycadet/avatar-manifest/package.json +1 -1
- package/node_modules/@mycadet/environments/package.json +1 -1
- package/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/avatar-manifest/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/environments/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/package.json +1 -1
- package/node_modules/@mycadet/local-mode/package.json +1 -1
- package/node_modules/@mycadet/local-mode/src/__tests__/helpers/lockfile-entry.ts +1 -1
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-sleep.test.ts +3 -3
- package/node_modules/@mycadet/local-mode/src/config.ts +19 -12
- package/node_modules/@mycadet/local-mode/src/gateway-bearer.ts +2 -2
- package/node_modules/@mycadet/local-mode/src/index.ts +8 -1
- package/node_modules/@mycadet/local-mode/src/lockfile-contract.test.ts +151 -0
- package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +86 -0
- package/node_modules/@mycadet/service-contracts/package.json +1 -1
- package/node_modules/@mycadet/shared/package.json +1 -1
- package/package.json +1 -1
- package/src/__tests__/gateway-token.test.ts +22 -19
- package/src/__tests__/guardian-token.test.ts +14 -12
- package/src/__tests__/helpers/lockfile-entry.ts +1 -11
- package/src/__tests__/host-teleport-docker.test.ts +0 -1
- package/src/__tests__/nginx.test.ts +1 -6
- package/src/__tests__/pair.test.ts +53 -28
- package/src/__tests__/restore.test.ts +0 -24
- package/src/__tests__/setup.test.ts +63 -24
- package/src/__tests__/statefulset.test.ts +1 -1
- package/src/__tests__/tunnel.test.ts +46 -215
- package/src/__tests__/upgrade-local.test.ts +1 -30
- package/src/__tests__/upgrade-replay-env.test.ts +19 -7
- package/src/__tests__/wake.test.ts +0 -56
- package/src/commands/gateway/token.ts +5 -16
- package/src/commands/restore.ts +1 -10
- package/src/commands/tunnel.ts +7 -126
- package/src/commands/upgrade.ts +6 -24
- package/src/lib/__tests__/docker.test.ts +1 -6
- package/src/lib/guardian-token.ts +100 -68
- package/src/lib/live-voice/connection.ts +1 -1
- package/src/lib/remotes/docker.ts +3 -31
- package/src/lib/remotes/local.ts +3 -44
- package/src/lib/root-backup.ts +1 -6
- package/src/lib/servers/nginx.ts +0 -2
- package/src/lib/statefulset.ts +0 -3
- package/src/lib/tunnel.ts +5 -104
- package/src/lib/tunnels/cloudflare.ts +4 -7
- package/src/lib/tunnels/ngrok.ts +9 -57
- package/src/lib/tunnels/tailscale.ts +1 -2
- package/src/lib/upgrade-lifecycle.ts +19 -18
- package/src/__tests__/detached-process.test.ts +0 -152
- package/src/__tests__/tunnel-holder-heal.test.ts +0 -119
- package/src/__tests__/tunnel-startup-summary.test.ts +0 -92
- package/src/lib/detached-process.ts +0 -78
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type Remote,
|
|
9
9
|
} from "../../lockfile-contract";
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import { resolveLockfilePath } from "../../config";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The canonical lockfile-entry fixture — one `makeEntry` for every suite.
|
|
@@ -11,6 +11,8 @@ import fs from "node:fs";
|
|
|
11
11
|
import os from "node:os";
|
|
12
12
|
import path from "node:path";
|
|
13
13
|
|
|
14
|
+
import type { DrainOptions, DrainOutcome } from "../remotes/local/drain";
|
|
15
|
+
|
|
14
16
|
// Snapshot the real modules before mocking so the module-scoped `afterAll`
|
|
15
17
|
// below can restore them — bun runs every test file in one process with a
|
|
16
18
|
// shared loader, and `mock.restore()` does not undo `mock.module()`.
|
|
@@ -27,9 +29,7 @@ const isProcessAliveMock = mock(
|
|
|
27
29
|
);
|
|
28
30
|
const stopProcessByPidFileMock = mock(async () => true);
|
|
29
31
|
const drainAssistantMock = mock(
|
|
30
|
-
async (): Promise<
|
|
31
|
-
"drained" | "timeout" | "unsupported" | "unreachable" | "cancelled"
|
|
32
|
-
> => "drained",
|
|
32
|
+
async (_opts: DrainOptions): Promise<DrainOutcome> => "drained",
|
|
33
33
|
);
|
|
34
34
|
const loadGuardianTokenMock = mock((): { accessToken: string } | null => ({
|
|
35
35
|
accessToken: "test-guardian-token",
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { unlinkSync } from "node:fs";
|
|
2
|
+
|
|
1
3
|
import { SEEDS } from "@mycadet/environments";
|
|
2
4
|
|
|
3
5
|
import { resolveEnvironmentName } from "./environment";
|
|
@@ -83,20 +85,25 @@ export function basetenTokenPath(assistantId: string): string {
|
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* writers create the file and every lease-repair reader goes through here.
|
|
88
|
+
* Remove a retired guardian bootstrap secret file, if present. The secret
|
|
89
|
+
* mechanism is gone (pairing is the bootstrap door); pre-retirement installs
|
|
90
|
+
* still carry the xdg file on disk, so a successful lease unlinks it
|
|
91
|
+
* best-effort — the credential must not rot on disk unread.
|
|
91
92
|
*/
|
|
92
|
-
export function
|
|
93
|
+
export function deleteGuardianBootstrapSecret(assistantId: string): void {
|
|
93
94
|
assertSafePathSegment(assistantId, "assistant ID");
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
95
|
+
try {
|
|
96
|
+
unlinkSync(
|
|
97
|
+
joinLocalPath(
|
|
98
|
+
resolveConfigDir(),
|
|
99
|
+
"assistants",
|
|
100
|
+
assistantId,
|
|
101
|
+
"guardian-bootstrap-secret",
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
} catch {
|
|
105
|
+
// Absent or unreadable — nothing to clean.
|
|
106
|
+
}
|
|
100
107
|
}
|
|
101
108
|
|
|
102
109
|
/**
|
|
@@ -49,7 +49,7 @@ export function clearLoopbackGatewayBearerCache(): void {
|
|
|
49
49
|
async function mintBearer(
|
|
50
50
|
port: number,
|
|
51
51
|
leaseGuardianToken: GuardianTokenLease,
|
|
52
|
-
fetcher: (input:
|
|
52
|
+
fetcher: (input: string, init?: RequestInit) => Promise<Response>,
|
|
53
53
|
): Promise<MintedGatewayBearer | null> {
|
|
54
54
|
let guardianToken: string;
|
|
55
55
|
try {
|
|
@@ -99,7 +99,7 @@ export interface LoopbackGatewayBearerOptions {
|
|
|
99
99
|
* Injectable for tests; defaults to the global fetch. A plain callable —
|
|
100
100
|
* the Electron forward threads its own recorded fetcher through.
|
|
101
101
|
*/
|
|
102
|
-
fetcher?: (input:
|
|
102
|
+
fetcher?: (input: string, init?: RequestInit) => Promise<Response>;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
@@ -33,7 +33,7 @@ export {
|
|
|
33
33
|
guardianTokenPath,
|
|
34
34
|
assistantBearerTokenPath,
|
|
35
35
|
basetenTokenPath,
|
|
36
|
-
|
|
36
|
+
deleteGuardianBootstrapSecret,
|
|
37
37
|
} from "./config";
|
|
38
38
|
export type { LocalEndpointConfig } from "./config";
|
|
39
39
|
export {
|
|
@@ -66,6 +66,10 @@ export {
|
|
|
66
66
|
isDockerEntry,
|
|
67
67
|
isLocalEntry,
|
|
68
68
|
LockfileAssistantSchema,
|
|
69
|
+
MachineMemoryPolicySchema,
|
|
70
|
+
MachinePolicySchema,
|
|
71
|
+
MACHINE_APPLICABILITY,
|
|
72
|
+
getMachinePolicy,
|
|
69
73
|
parseLockfile,
|
|
70
74
|
} from "./lockfile-contract";
|
|
71
75
|
export type {
|
|
@@ -78,6 +82,9 @@ export type {
|
|
|
78
82
|
CustomAssistant,
|
|
79
83
|
BasetenAssistant,
|
|
80
84
|
LockfileWriteResult,
|
|
85
|
+
MachinePolicy,
|
|
86
|
+
MachineMemoryPolicy,
|
|
87
|
+
MachineApplicability,
|
|
81
88
|
} from "./lockfile-contract";
|
|
82
89
|
export type { LockfileAssistantPatch } from "./lockfile";
|
|
83
90
|
export {
|
|
@@ -3,10 +3,13 @@ import { describe, expect, test } from "bun:test";
|
|
|
3
3
|
import {
|
|
4
4
|
LockfileAssistantSchema,
|
|
5
5
|
LockfileSchema,
|
|
6
|
+
MachinePolicySchema,
|
|
7
|
+
MACHINE_APPLICABILITY,
|
|
6
8
|
RemoteSchema,
|
|
7
9
|
KNOWN_REMOTES,
|
|
8
10
|
GATEWAY_PORT,
|
|
9
11
|
getGatewayUrl,
|
|
12
|
+
getMachinePolicy,
|
|
10
13
|
parseLockfile,
|
|
11
14
|
stripSensitiveFields,
|
|
12
15
|
SENSITIVE_KEYS,
|
|
@@ -431,3 +434,151 @@ describe("getGatewayUrl", () => {
|
|
|
431
434
|
]);
|
|
432
435
|
});
|
|
433
436
|
});
|
|
437
|
+
|
|
438
|
+
describe("machine policy", () => {
|
|
439
|
+
test("round-trips a fully-populated policy through parseLockfile", () => {
|
|
440
|
+
const raw = {
|
|
441
|
+
activeAssistant: "asst_m1",
|
|
442
|
+
assistants: [
|
|
443
|
+
{
|
|
444
|
+
assistantId: "asst_m1",
|
|
445
|
+
name: "Sized",
|
|
446
|
+
cloud: "docker",
|
|
447
|
+
port: 7830,
|
|
448
|
+
machine: {
|
|
449
|
+
memory: { high: 6 * 1024 ** 3, max: 8 * 1024 ** 3 },
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
],
|
|
453
|
+
};
|
|
454
|
+
expect(parseLockfile(raw)).toEqual(raw as Lockfile);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("keeps entries without a machine section (absent = runtime defaults)", () => {
|
|
458
|
+
const parsed = parseLockfile({
|
|
459
|
+
assistants: [{ assistantId: "asst_m2", cloud: "local", port: 7830 }],
|
|
460
|
+
});
|
|
461
|
+
expect(getMachinePolicy(parsed.assistants[0]!)).toBeUndefined();
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
test("a policy violating high <= max quarantines to defaults, entry survives", () => {
|
|
465
|
+
const parsed = parseLockfile({
|
|
466
|
+
assistants: [
|
|
467
|
+
{
|
|
468
|
+
assistantId: "asst_m3",
|
|
469
|
+
cloud: "docker",
|
|
470
|
+
port: 7830,
|
|
471
|
+
machine: {
|
|
472
|
+
memory: { high: 10 * 1024 ** 3, max: 8 * 1024 ** 3 },
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
],
|
|
476
|
+
});
|
|
477
|
+
// The write path rejects this loudly; the parse path keeps the assistant
|
|
478
|
+
// but falls back to the defaults so one bad section can't drop the entry.
|
|
479
|
+
expect(getMachinePolicy(parsed.assistants[0]!)).toBeUndefined();
|
|
480
|
+
expect(parsed.assistants[0]).toMatchObject({
|
|
481
|
+
assistantId: "asst_m3",
|
|
482
|
+
cloud: "docker",
|
|
483
|
+
port: 7830,
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
test("unknown machine keys quarantine the section (strict at the boundary)", () => {
|
|
488
|
+
const parsed = parseLockfile({
|
|
489
|
+
assistants: [
|
|
490
|
+
{
|
|
491
|
+
assistantId: "asst_m4",
|
|
492
|
+
cloud: "local",
|
|
493
|
+
port: 7830,
|
|
494
|
+
machine: { memoryy: { max: 1024 } } as Record<string, unknown>,
|
|
495
|
+
},
|
|
496
|
+
],
|
|
497
|
+
});
|
|
498
|
+
expect(getMachinePolicy(parsed.assistants[0]!)).toBeUndefined();
|
|
499
|
+
expect(parsed.assistants[0]).toMatchObject({ assistantId: "asst_m4" });
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
test("a non-object machine section quarantines the same way", () => {
|
|
503
|
+
const parsed = parseLockfile({
|
|
504
|
+
assistants: [
|
|
505
|
+
{
|
|
506
|
+
assistantId: "asst_m5",
|
|
507
|
+
cloud: "local",
|
|
508
|
+
port: 7830,
|
|
509
|
+
machine: "12GiB" as unknown as Record<string, unknown>,
|
|
510
|
+
},
|
|
511
|
+
],
|
|
512
|
+
});
|
|
513
|
+
expect(getMachinePolicy(parsed.assistants[0]!)).toBeUndefined();
|
|
514
|
+
expect(parsed.assistants[0]).toMatchObject({ assistantId: "asst_m5" });
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test("accepts high without max and max without high", () => {
|
|
518
|
+
for (const memory of [{ high: 6 * 1024 ** 3 }, { max: 8 * 1024 ** 3 }]) {
|
|
519
|
+
const parsed = parseLockfile({
|
|
520
|
+
assistants: [
|
|
521
|
+
{
|
|
522
|
+
assistantId: "asst_m6",
|
|
523
|
+
cloud: "local",
|
|
524
|
+
port: 7830,
|
|
525
|
+
machine: { memory },
|
|
526
|
+
},
|
|
527
|
+
],
|
|
528
|
+
});
|
|
529
|
+
expect(getMachinePolicy(parsed.assistants[0]!)).toEqual({ memory });
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
test("rejects non-positive and non-integer byte values", () => {
|
|
534
|
+
for (const memory of [
|
|
535
|
+
{ max: 0 },
|
|
536
|
+
{ max: -1 },
|
|
537
|
+
{ high: 1.5 },
|
|
538
|
+
{ max: Number.NaN },
|
|
539
|
+
]) {
|
|
540
|
+
const parsed = parseLockfile({
|
|
541
|
+
assistants: [
|
|
542
|
+
{
|
|
543
|
+
assistantId: "asst_m7",
|
|
544
|
+
cloud: "local",
|
|
545
|
+
port: 7830,
|
|
546
|
+
machine: { memory },
|
|
547
|
+
},
|
|
548
|
+
],
|
|
549
|
+
});
|
|
550
|
+
expect(getMachinePolicy(parsed.assistants[0]!)).toBeUndefined();
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
describe("MachinePolicySchema", () => {
|
|
556
|
+
test("accepts a valid policy and pins the cross-field issue path", () => {
|
|
557
|
+
expect(
|
|
558
|
+
MachinePolicySchema.safeParse({
|
|
559
|
+
memory: { high: 6 * 1024 ** 3, max: 8 * 1024 ** 3 },
|
|
560
|
+
}).success,
|
|
561
|
+
).toBe(true);
|
|
562
|
+
|
|
563
|
+
const invalid = MachinePolicySchema.safeParse({
|
|
564
|
+
memory: { high: 9 * 1024 ** 3, max: 8 * 1024 ** 3 },
|
|
565
|
+
});
|
|
566
|
+
expect(invalid.success).toBe(false);
|
|
567
|
+
if (!invalid.success) {
|
|
568
|
+
expect(invalid.error.issues[0]?.path).toEqual(["memory", "high"]);
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
describe("MACHINE_APPLICABILITY", () => {
|
|
574
|
+
test("covers every known remote", () => {
|
|
575
|
+
expect(Object.keys(MACHINE_APPLICABILITY).sort()).toEqual(
|
|
576
|
+
[...KNOWN_REMOTES].sort(),
|
|
577
|
+
);
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
test("pins the materialization semantics that exist today", () => {
|
|
581
|
+
expect(MACHINE_APPLICABILITY.docker).toBe("cgroup");
|
|
582
|
+
expect(MACHINE_APPLICABILITY.local).toBe("process-hint");
|
|
583
|
+
});
|
|
584
|
+
});
|
|
@@ -250,6 +250,73 @@ export const ContainerInfoSchema = z.object({
|
|
|
250
250
|
});
|
|
251
251
|
export type ContainerInfo = z.infer<typeof ContainerInfoSchema>;
|
|
252
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Host-independent resource policy for one assistant's machine allocation —
|
|
255
|
+
* what the allocation *should be*, as opposed to the record fields (ports,
|
|
256
|
+
* instanceDir, containerInfo) that describe what *is*. The section rides
|
|
257
|
+
* every topology's entry: the stored shape is the same everywhere, while how
|
|
258
|
+
* a policy materializes differs per remote (see {@link MACHINE_APPLICABILITY}).
|
|
259
|
+
*
|
|
260
|
+
* The schema is strict — unknown keys are rejected — because the write path
|
|
261
|
+
* (future `cadet machine set`, daemon PATCH, cloud API) is the loud validator:
|
|
262
|
+
* a typo'd knob must fail at the boundary, never silently degrade to the
|
|
263
|
+
* runtime default. Parse time is deliberately softer: the field is embedded
|
|
264
|
+
* with `.catch(undefined)`, so a malformed or unrecognized section from a
|
|
265
|
+
* hand-edit or a different build quarantines to the defaults while the
|
|
266
|
+
* entry itself survives, and the drift is visible to readers instead of
|
|
267
|
+
* costing the assistant its identity fields.
|
|
268
|
+
*/
|
|
269
|
+
export const MachineMemoryPolicySchema = z
|
|
270
|
+
.strictObject({
|
|
271
|
+
/**
|
|
272
|
+
* Soft limit in bytes: where the runtime should start reclaiming early
|
|
273
|
+
* and smoothly (cgroup v2 `memory.high` on docker remotes). Absent =
|
|
274
|
+
* unconstrained.
|
|
275
|
+
*/
|
|
276
|
+
high: z.number().int().positive().optional(),
|
|
277
|
+
/** Hard limit in bytes: the backstop the runtime may not exceed
|
|
278
|
+
* (cgroup v2 `memory.max` on docker remotes). Absent = unconstrained. */
|
|
279
|
+
max: z.number().int().positive().optional(),
|
|
280
|
+
})
|
|
281
|
+
.superRefine((memory, context) => {
|
|
282
|
+
if (
|
|
283
|
+
memory.high !== undefined &&
|
|
284
|
+
memory.max !== undefined &&
|
|
285
|
+
memory.high > memory.max
|
|
286
|
+
) {
|
|
287
|
+
context.addIssue({
|
|
288
|
+
code: "custom",
|
|
289
|
+
path: ["high"],
|
|
290
|
+
message: `memory.high (${memory.high}) must not exceed memory.max (${memory.max})`,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
export type MachineMemoryPolicy = z.infer<typeof MachineMemoryPolicySchema>;
|
|
295
|
+
|
|
296
|
+
/** The `machine` section: one key per resource knob, added as they migrate here. */
|
|
297
|
+
export const MachinePolicySchema = z.strictObject({
|
|
298
|
+
memory: MachineMemoryPolicySchema.optional(),
|
|
299
|
+
});
|
|
300
|
+
export type MachinePolicy = z.infer<typeof MachinePolicySchema>;
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* How a stored `machine` policy materializes per remote topology. The store
|
|
304
|
+
* is host-independent — this table is the one place that says what a policy
|
|
305
|
+
* *means* on each remote, so the apply layer and readers derive semantics
|
|
306
|
+
* from it instead of re-deciding per call site. Remotes gain real
|
|
307
|
+
* applicability as their apply paths land (cloud/gVisor, apple-container).
|
|
308
|
+
*/
|
|
309
|
+
export type MachineApplicability = "cgroup" | "process-hint" | "unsupported";
|
|
310
|
+
|
|
311
|
+
export const MACHINE_APPLICABILITY = {
|
|
312
|
+
local: "process-hint",
|
|
313
|
+
docker: "cgroup",
|
|
314
|
+
"apple-container": "unsupported",
|
|
315
|
+
cloud: "unsupported",
|
|
316
|
+
custom: "unsupported",
|
|
317
|
+
baseten: "unsupported",
|
|
318
|
+
} as const satisfies Record<Remote, MachineApplicability>;
|
|
319
|
+
|
|
253
320
|
/**
|
|
254
321
|
* The fields every topology actually uses: the identity every reader looks
|
|
255
322
|
* entries up by, the display name, and the hatch/onboarding/conversation
|
|
@@ -273,6 +340,13 @@ const BaseAssistantSchema = z.object({
|
|
|
273
340
|
onboardedAt: z.string().optional(),
|
|
274
341
|
/** PID of the file watcher process for instances hatched with --watch. */
|
|
275
342
|
watcherPid: z.number().optional(),
|
|
343
|
+
/**
|
|
344
|
+
* Host-independent resource policy for this assistant's machine allocation
|
|
345
|
+
* (see {@link MachinePolicySchema} for the strict-at-write, fallback-at-parse
|
|
346
|
+
* contract, and {@link MACHINE_APPLICABILITY} for how it materializes per
|
|
347
|
+
* remote). Absent = the runtime's own defaults apply.
|
|
348
|
+
*/
|
|
349
|
+
machine: MachinePolicySchema.optional().catch(undefined),
|
|
276
350
|
});
|
|
277
351
|
|
|
278
352
|
/**
|
|
@@ -420,6 +494,18 @@ export function isCloudEntry(
|
|
|
420
494
|
return entry.cloud === "cloud";
|
|
421
495
|
}
|
|
422
496
|
|
|
497
|
+
/**
|
|
498
|
+
* The stored `machine` policy for an entry, exactly as validated — no default
|
|
499
|
+
* resolution here. Falling back to runtime defaults is the reader's decision
|
|
500
|
+
* (the apply layer pairs this with the spec defaults it owns), so callers that
|
|
501
|
+
* need to distinguish an unset section from a set one can.
|
|
502
|
+
*/
|
|
503
|
+
export function getMachinePolicy(
|
|
504
|
+
entry: LockfileAssistant,
|
|
505
|
+
): MachinePolicy | undefined {
|
|
506
|
+
return entry.machine;
|
|
507
|
+
}
|
|
508
|
+
|
|
423
509
|
/** Whether the entry is a docker-hosted assistant. */
|
|
424
510
|
export function isDockerEntry(
|
|
425
511
|
entry: LockfileAssistant,
|
package/package.json
CHANGED
|
@@ -83,18 +83,19 @@ async function runGatewayToken(): Promise<{
|
|
|
83
83
|
return { exited, logs: logs.join("\n"), errors: errors.join("\n") };
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/**
|
|
87
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Fetch double covering the repair choreography (readyz/pair). The body is
|
|
88
|
+
* /v1/pair's device-bound response shape (token/guardianId names) — the lease
|
|
89
|
+
* maps it onto the persisted token fields.
|
|
90
|
+
*/
|
|
91
|
+
function mockRepairFetch(pairBody: object): void {
|
|
88
92
|
globalThis.fetch = withPreconnect(async (input: RequestInfo | URL) => {
|
|
89
93
|
const url = String(input);
|
|
90
94
|
if (url.endsWith("/readyz")) {
|
|
91
95
|
return new Response("", { status: 404 }); // no starting gate — proceed
|
|
92
96
|
}
|
|
93
|
-
if (url.endsWith("/v1/
|
|
94
|
-
return new Response(
|
|
95
|
-
}
|
|
96
|
-
if (url.endsWith("/v1/guardian/init")) {
|
|
97
|
-
return new Response(JSON.stringify(initBody), {
|
|
97
|
+
if (url.endsWith("/v1/pair")) {
|
|
98
|
+
return new Response(JSON.stringify(pairBody), {
|
|
98
99
|
status: 200,
|
|
99
100
|
headers: { "content-type": "application/json" },
|
|
100
101
|
});
|
|
@@ -103,13 +104,15 @@ function mockRepairFetch(initBody: object): void {
|
|
|
103
104
|
});
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
accessTokenExpiresAt: Date.now() + 60 * 60 * 1000,
|
|
107
|
+
const FRESH_PAIR = {
|
|
108
|
+
token: "fresh-acc",
|
|
109
|
+
expiresAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
|
|
110
110
|
refreshToken: "fresh-ref",
|
|
111
|
-
refreshTokenExpiresAt: Date
|
|
112
|
-
|
|
111
|
+
refreshTokenExpiresAt: new Date(
|
|
112
|
+
Date.now() + 365 * 24 * 60 * 60 * 1000,
|
|
113
|
+
).toISOString(),
|
|
114
|
+
refreshAfter: new Date(Date.now() + 48 * 60 * 60 * 1000).toISOString(),
|
|
115
|
+
guardianId: "principal-new",
|
|
113
116
|
isNew: false,
|
|
114
117
|
};
|
|
115
118
|
|
|
@@ -192,15 +195,15 @@ describe("gateway token command", () => {
|
|
|
192
195
|
expect(errors).toContain("No assistant found with name or ID 'nope'");
|
|
193
196
|
});
|
|
194
197
|
|
|
195
|
-
test("refresh self-heals an expired credential
|
|
196
|
-
seedLocalEntry(
|
|
198
|
+
test("refresh self-heals an expired credential by re-pairing", async () => {
|
|
199
|
+
seedLocalEntry();
|
|
197
200
|
saveGuardianToken(
|
|
198
201
|
"px",
|
|
199
202
|
makeGuardianTokenData({
|
|
200
203
|
refreshTokenExpiresAt: new Date(Date.now() - 1000).toISOString(),
|
|
201
204
|
}),
|
|
202
205
|
);
|
|
203
|
-
mockRepairFetch(
|
|
206
|
+
mockRepairFetch(FRESH_PAIR);
|
|
204
207
|
|
|
205
208
|
process.argv = ["bun", "cadet", "gateway", "token", "refresh"];
|
|
206
209
|
const { exited, logs, errors } = await runGatewayToken();
|
|
@@ -215,7 +218,7 @@ describe("gateway token command", () => {
|
|
|
215
218
|
});
|
|
216
219
|
|
|
217
220
|
test("refresh reports repair failure and never suggests hatching", async () => {
|
|
218
|
-
seedLocalEntry(
|
|
221
|
+
seedLocalEntry();
|
|
219
222
|
saveGuardianToken(
|
|
220
223
|
"px",
|
|
221
224
|
makeGuardianTokenData({
|
|
@@ -236,8 +239,8 @@ describe("gateway token command", () => {
|
|
|
236
239
|
});
|
|
237
240
|
|
|
238
241
|
test("refresh re-provisions when the token file is missing entirely", async () => {
|
|
239
|
-
seedLocalEntry(
|
|
240
|
-
mockRepairFetch(
|
|
242
|
+
seedLocalEntry();
|
|
243
|
+
mockRepairFetch(FRESH_PAIR);
|
|
241
244
|
|
|
242
245
|
process.argv = ["bun", "cadet", "gateway", "token", "refresh"];
|
|
243
246
|
const { exited, logs } = await runGatewayToken();
|
|
@@ -264,12 +264,7 @@ describe("refreshGuardianToken", () => {
|
|
|
264
264
|
expect(existsSync(lockPath("px"))).toBe(false); // lock released
|
|
265
265
|
});
|
|
266
266
|
|
|
267
|
-
test("leases against the gateway's real
|
|
268
|
-
// The gateway's GuardianBootstrapResult serializes refreshAfter as a
|
|
269
|
-
// NUMBER of ms (mintRefreshToken), and the strict lease check rejects a
|
|
270
|
-
// response missing any field. A string-only schema read that number as
|
|
271
|
-
// malformed, so every real hatch failed its lease while the ISO-string
|
|
272
|
-
// test doubles here stayed green.
|
|
267
|
+
test("leases against the gateway's real pair shape — refreshAfter arrives as a number", async () => {
|
|
273
268
|
globalThis.fetch = withPreconnect(async (input: RequestInfo | URL) => {
|
|
274
269
|
const url = String(input);
|
|
275
270
|
if (url.endsWith("/readyz")) {
|
|
@@ -277,12 +272,19 @@ describe("refreshGuardianToken", () => {
|
|
|
277
272
|
}
|
|
278
273
|
return new Response(
|
|
279
274
|
JSON.stringify({
|
|
280
|
-
|
|
281
|
-
accessToken
|
|
282
|
-
|
|
275
|
+
// The gateway's device-bound pair response maps onto the lease's
|
|
276
|
+
// field names (token → accessToken, guardianId → guardianPrincipalId);
|
|
277
|
+
// refreshAfter serializes as a NUMBER of ms (mintRefreshToken), and
|
|
278
|
+
// the strict lease check rejects a response missing any field. A
|
|
279
|
+
// string-only schema read that number as malformed, so every real
|
|
280
|
+
// hatch failed its lease while the ISO-string test doubles here
|
|
281
|
+
// stayed green.
|
|
282
|
+
token: "access-lease",
|
|
283
|
+
expiresAt: Date.now() + 60 * 60 * 1000,
|
|
283
284
|
refreshToken: "refresh-lease",
|
|
284
285
|
refreshTokenExpiresAt: Date.now() + 365 * 24 * 60 * 60 * 1000,
|
|
285
286
|
refreshAfter: Date.now() + 48 * 60 * 60 * 1000,
|
|
287
|
+
guardianId: "principal-lease",
|
|
286
288
|
isNew: true,
|
|
287
289
|
mintedOverPriorEvidence: false,
|
|
288
290
|
}),
|
|
@@ -308,8 +310,8 @@ describe("refreshGuardianToken", () => {
|
|
|
308
310
|
}
|
|
309
311
|
return new Response(
|
|
310
312
|
JSON.stringify({
|
|
311
|
-
|
|
312
|
-
|
|
313
|
+
guardianId: "principal-lease",
|
|
314
|
+
token: 42,
|
|
313
315
|
refreshToken: "refresh-lease",
|
|
314
316
|
}),
|
|
315
317
|
{ status: 200, headers: { "content-type": "application/json" } },
|
|
@@ -327,7 +329,7 @@ describe("refreshGuardianToken", () => {
|
|
|
327
329
|
`expected GuardianLeasePermanentError, got ${String(err)}`,
|
|
328
330
|
);
|
|
329
331
|
}
|
|
330
|
-
expect(err.message).toContain("
|
|
332
|
+
expect(err.message).toContain("malformed token response");
|
|
331
333
|
expect(err.message).toContain("accessToken arrived as number");
|
|
332
334
|
expect(err.message).toContain("refreshTokenExpiresAt arrived as absent");
|
|
333
335
|
});
|