@mycadet/cli 0.13.0-dev.202611142107.f5e00a2 → 0.13.0-dev.202611142321.f0c8539

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 (42) hide show
  1. package/node_modules/@mycadet/assistant-api/node_modules/@mycadet/service-contracts/package.json +1 -1
  2. package/node_modules/@mycadet/assistant-api/node_modules/@mycadet/service-contracts/src/remote-web-pairing.ts +12 -0
  3. package/node_modules/@mycadet/avatar-manifest/package.json +1 -1
  4. package/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
  5. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/avatar-manifest/package.json +1 -1
  6. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
  7. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/package.json +1 -1
  8. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/src/remote-web-pairing.ts +12 -0
  9. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/package.json +1 -1
  10. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/seeds.ts +5 -22
  11. package/node_modules/@mycadet/local-mode/package.json +3 -2
  12. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token.test.ts +5 -5
  13. package/node_modules/@mycadet/local-mode/src/__tests__/hatch-local-config-api.test.ts +21 -4
  14. package/node_modules/@mycadet/local-mode/src/__tests__/operations-pair.test.ts +21 -8
  15. package/node_modules/@mycadet/local-mode/src/__tests__/pair.test.ts +0 -2
  16. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-docker-pair.test.ts +16 -6
  17. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-local-pair.test.ts +18 -8
  18. package/node_modules/@mycadet/local-mode/src/__tests__/root-backup-docker.test.ts +2 -3
  19. package/node_modules/@mycadet/local-mode/src/__tests__/upgrade-root-recovery.test.ts +4 -2
  20. package/node_modules/@mycadet/local-mode/src/guardian-contract.ts +36 -0
  21. package/node_modules/@mycadet/local-mode/src/guardian-token.ts +9 -22
  22. package/node_modules/@mycadet/local-mode/src/operations/pair.ts +26 -7
  23. package/node_modules/@mycadet/local-mode/src/pair.ts +0 -1
  24. package/node_modules/@mycadet/local-mode/src/remotes/docker/hatch.ts +75 -61
  25. package/node_modules/@mycadet/local-mode/src/remotes/docker/pair.ts +7 -3
  26. package/node_modules/@mycadet/local-mode/src/remotes/docker/wake.ts +40 -15
  27. package/node_modules/@mycadet/local-mode/src/remotes/local/hatch.ts +18 -0
  28. package/node_modules/@mycadet/local-mode/src/remotes/local/pair.ts +7 -3
  29. package/node_modules/@mycadet/local-mode/src/upgrade-lifecycle.ts +2 -3
  30. package/node_modules/@mycadet/service-contracts/package.json +1 -1
  31. package/node_modules/@mycadet/service-contracts/src/remote-web-pairing.ts +12 -0
  32. package/node_modules/@mycadet/shared/package.json +1 -1
  33. package/node_modules/@mycadet/shared/seeds.ts +5 -22
  34. package/package.json +1 -1
  35. package/src/__tests__/assistant-client-refresh.test.ts +5 -2
  36. package/src/__tests__/client-tui-refresh.test.ts +12 -1
  37. package/src/__tests__/guardian-token.test.ts +57 -31
  38. package/src/__tests__/setup.test.ts +10 -6
  39. package/src/__tests__/tui-midsession-refresh.test.ts +12 -1
  40. package/src/commands/pair.ts +9 -8
  41. package/src/index.ts +1 -9
  42. package/src/lib/guardian-token.ts +16 -34
@@ -233,7 +233,6 @@ describe("refreshGuardianToken", () => {
233
233
  refreshAfter: "",
234
234
  deviceId: "dev",
235
235
  leasedAt: new Date().toISOString(),
236
- pairedGatewayUrl: "https://gw.example.com",
237
236
  });
238
237
  }
239
238
 
@@ -262,11 +261,14 @@ describe("refreshGuardianToken", () => {
262
261
  sawSignal = init?.signal instanceof AbortSignal;
263
262
  return new Response(
264
263
  JSON.stringify({
264
+ guardianPrincipalId: "imported",
265
265
  accessToken: "new-acc",
266
- refreshToken: "new-ref",
267
266
  accessTokenExpiresAt: future(),
267
+ refreshToken: "new-ref",
268
268
  refreshTokenExpiresAt: future(),
269
- refreshAfter: "",
269
+ refreshAfter: future(),
270
+ deviceId: "dev",
271
+ leasedAt: new Date().toISOString(),
270
272
  }),
271
273
  { status: 200, headers: { "content-type": "application/json" } },
272
274
  );
@@ -277,20 +279,15 @@ describe("refreshGuardianToken", () => {
277
279
 
278
280
  expect(result?.accessToken).toBe("new-acc");
279
281
  expect(loadGuardianToken("px")?.accessToken).toBe("new-acc");
280
- expect(loadGuardianToken("px")?.pairedGatewayUrl).toBe(
281
- "https://gw.example.com",
282
- );
282
+ expect(loadGuardianToken("px")?.refreshToken).toBe("new-ref");
283
283
  expect(sawSignal).toBe(true); // fetch carries a timeout AbortSignal
284
284
  expect(existsSync(lockPath("px"))).toBe(false); // lock released
285
285
  });
286
286
 
287
- test("leases against the gateway's real pair shape — refreshAfter arrives as a number", async () => {
288
- // The gateway's device-bound pair response maps onto the lease's field
289
- // names (token → accessToken, guardianId → guardianPrincipalId);
290
- // refreshAfter serializes as a NUMBER of ms (mintRefreshToken), and the
291
- // strict lease check rejects a response missing any field. A string-only
292
- // schema read that number as malformed, so every real hatch failed its
293
- // lease while the ISO-string test doubles here stayed green.
287
+ test("persists the gateway's credential contract verbatim", async () => {
288
+ // The pair response IS the token file's shape now: the gateway mints
289
+ // ISO-8601 instants, echoes the device binding, and stamps the lease,
290
+ // and the lease persists the parse as-is.
294
291
  globalThis.fetch = withPreconnect(async (input: RequestInfo | URL) => {
295
292
  const url = String(input);
296
293
  if (url.endsWith("/readyz")) {
@@ -301,13 +298,18 @@ describe("refreshGuardianToken", () => {
301
298
  pairDeviceMock.mockResolvedValue({
302
299
  ok: true,
303
300
  response: {
304
- token: "access-lease",
305
- expiresAt: Date.now() + 60 * 60 * 1000,
301
+ guardianPrincipalId: "principal-lease",
302
+ accessToken: "access-lease",
303
+ accessTokenExpiresAt: new Date(
304
+ Date.now() + 60 * 60 * 1000,
305
+ ).toISOString(),
306
306
  refreshToken: "refresh-lease",
307
- refreshTokenExpiresAt: Date.now() + 365 * 24 * 60 * 60 * 1000,
308
- refreshAfter: Date.now() + 48 * 60 * 60 * 1000,
309
- guardianId: "principal-lease",
310
- mintedOverPriorEvidence: false,
307
+ refreshTokenExpiresAt: new Date(
308
+ Date.now() + 365 * 24 * 60 * 60 * 1000,
309
+ ).toISOString(),
310
+ refreshAfter: new Date(Date.now() + 48 * 60 * 60 * 1000).toISOString(),
311
+ deviceId: "device-lease",
312
+ leasedAt: new Date().toISOString(),
311
313
  },
312
314
  });
313
315
 
@@ -332,8 +334,8 @@ describe("refreshGuardianToken", () => {
332
334
  pairDeviceMock.mockResolvedValue({
333
335
  ok: true,
334
336
  response: {
335
- guardianId: "principal-lease",
336
- token: 42,
337
+ guardianPrincipalId: "principal-lease",
338
+ accessToken: 42,
337
339
  refreshToken: "refresh-lease",
338
340
  },
339
341
  });
@@ -350,8 +352,8 @@ describe("refreshGuardianToken", () => {
350
352
  );
351
353
  }
352
354
  expect(err.message).toContain("malformed token response");
353
- expect(err.message).toContain("accessToken arrived as number");
354
- expect(err.message).toContain("refreshTokenExpiresAt arrived as absent");
355
+ expect(err.message).toContain("expected string, received number");
356
+ expect(err.message).toContain("expected string, received undefined");
355
357
  });
356
358
 
357
359
  test("returns null without calling the gateway when the refresh token is expired", async () => {
@@ -397,10 +399,22 @@ describe("refreshGuardianToken", () => {
397
399
 
398
400
  globalThis.fetch = withPreconnect(
399
401
  async (_url: unknown, _init?: RequestInit) =>
400
- new Response(JSON.stringify({ accessToken: "new-acc" }), {
401
- status: 200,
402
- headers: { "content-type": "application/json" },
403
- }),
402
+ new Response(
403
+ JSON.stringify({
404
+ guardianPrincipalId: "imported",
405
+ accessToken: "new-acc",
406
+ accessTokenExpiresAt: future(),
407
+ refreshToken: "new-ref",
408
+ refreshTokenExpiresAt: future(),
409
+ refreshAfter: future(),
410
+ deviceId: "dev",
411
+ leasedAt: new Date().toISOString(),
412
+ }),
413
+ {
414
+ status: 200,
415
+ headers: { "content-type": "application/json" },
416
+ },
417
+ ),
404
418
  );
405
419
 
406
420
  const result = await refreshGuardianToken("https://gw.example.com", "px");
@@ -423,10 +437,22 @@ describe("refreshGuardianToken", () => {
423
437
  globalThis.fetch = withPreconnect(
424
438
  async (_url: unknown, _init?: RequestInit) => {
425
439
  called = true;
426
- return new Response(JSON.stringify({ accessToken: "new-acc" }), {
427
- status: 200,
428
- headers: { "content-type": "application/json" },
429
- });
440
+ return new Response(
441
+ JSON.stringify({
442
+ guardianPrincipalId: "imported",
443
+ accessToken: "new-acc",
444
+ accessTokenExpiresAt: future(),
445
+ refreshToken: "new-ref",
446
+ refreshTokenExpiresAt: future(),
447
+ refreshAfter: future(),
448
+ deviceId: "dev",
449
+ leasedAt: new Date().toISOString(),
450
+ }),
451
+ {
452
+ status: 200,
453
+ headers: { "content-type": "application/json" },
454
+ },
455
+ );
430
456
  },
431
457
  );
432
458
 
@@ -269,12 +269,14 @@ describe("setup command", () => {
269
269
  pairDeviceMock.mockResolvedValue({
270
270
  ok: true,
271
271
  response: {
272
- token: t.accessToken,
273
- expiresAt: t.accessTokenExpiresAt,
272
+ guardianPrincipalId: t.guardianPrincipalId,
273
+ accessToken: t.accessToken,
274
+ accessTokenExpiresAt: t.accessTokenExpiresAt,
274
275
  refreshToken: t.refreshToken,
275
276
  refreshTokenExpiresAt: t.refreshTokenExpiresAt,
276
277
  refreshAfter: t.refreshAfter,
277
- guardianId: t.guardianPrincipalId,
278
+ deviceId: t.deviceId,
279
+ leasedAt: t.leasedAt,
278
280
  },
279
281
  });
280
282
  installFetchStub();
@@ -309,12 +311,14 @@ describe("setup command", () => {
309
311
  pairDeviceMock.mockResolvedValue({
310
312
  ok: true,
311
313
  response: {
312
- token: t.accessToken,
313
- expiresAt: t.accessTokenExpiresAt,
314
+ guardianPrincipalId: t.guardianPrincipalId,
315
+ accessToken: t.accessToken,
316
+ accessTokenExpiresAt: t.accessTokenExpiresAt,
314
317
  refreshToken: t.refreshToken,
315
318
  refreshTokenExpiresAt: t.refreshTokenExpiresAt,
316
319
  refreshAfter: t.refreshAfter,
317
- guardianId: t.guardianPrincipalId,
320
+ deviceId: t.deviceId,
321
+ leasedAt: t.leasedAt,
318
322
  },
319
323
  });
320
324
 
@@ -64,7 +64,18 @@ function stubRefresh(ok: boolean): {
64
64
  if (String(url).includes("/v1/guardian/refresh")) {
65
65
  calledUrl = String(url);
66
66
  return new Response(
67
- ok ? JSON.stringify({ accessToken: "new-acc" }) : "x",
67
+ ok
68
+ ? JSON.stringify({
69
+ guardianPrincipalId: "imported",
70
+ accessToken: "new-acc",
71
+ accessTokenExpiresAt: Date.now() + 60 * 60 * 1000,
72
+ refreshToken: "new-ref",
73
+ refreshTokenExpiresAt: Date.now() + 60 * 60 * 1000,
74
+ refreshAfter: new Date(Date.now() + 30 * 60 * 1000).toISOString(),
75
+ deviceId: "dev",
76
+ leasedAt: new Date().toISOString(),
77
+ })
78
+ : "x",
68
79
  {
69
80
  status: ok ? 200 : 401,
70
81
  headers: { "content-type": "application/json" },
@@ -50,6 +50,7 @@ import { appleContainerPair } from "@mycadet/local-mode";
50
50
  import { cloudPair } from "../lib/remotes/cloud.js";
51
51
  import { formatWebApproveFailure, parseGatewayErrorCode } from "../lib/pair.js";
52
52
  import { STALE_CLI_UPDATE_HINT } from "../lib/stale-cli-hint.js";
53
+ import { RemoteWebPairingChallengeResponseSchema } from "@mycadet/service-contracts/remote-web-pairing";
53
54
 
54
55
  function assistantDisplayName(entry: LockfileAssistant): string {
55
56
  return entry.name || entry.assistantId;
@@ -207,19 +208,19 @@ function parseMintedChallenge(
207
208
  stdout: string,
208
209
  ): RemoteWebPairingChallengeResponse {
209
210
  // Same last-line JSON contract the guardian lease's pair door uses — the
210
- // CLI runs with pino silenced, so the payload line is reliably last.
211
- const parsed = pairCliStdoutToResult(stdout);
211
+ // CLI runs with pino silenced, so the payload line is reliably last, and
212
+ // the parser zod-parses the shape so nothing downstream casts.
213
+ const parsed = pairCliStdoutToResult(
214
+ stdout,
215
+ RemoteWebPairingChallengeResponseSchema,
216
+ );
212
217
  if (!parsed.ok) {
213
- console.error("Error: could not parse the gateway's pairing mint output.");
214
- process.exit(1);
215
- }
216
- const challenge = parsed.response as RemoteWebPairingChallengeResponse;
217
- if (!challenge.deviceCode || !challenge.userCode) {
218
218
  console.error(
219
- "Error: the gateway's pairing mint output is missing the challenge codes.",
219
+ `Error: could not parse the gateway's pairing mint output: ${parsed.error}`,
220
220
  );
221
221
  process.exit(1);
222
222
  }
223
+ const challenge = parsed.response;
223
224
  return challenge;
224
225
  }
225
226
 
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@ import { readFileSync } from "node:fs";
5
5
 
6
6
  import { checkHealth, resolveConfigDir } from "@mycadet/local-mode";
7
7
 
8
- import { resolveVersion, stampedVersion } from "@mycadet/shared/version";
8
+ import { stampedVersion } from "@mycadet/shared/version";
9
9
 
10
10
  import cliPkg from "../package.json";
11
11
  import { backup } from "./commands/backup";
@@ -40,14 +40,6 @@ import { voice } from "./commands/voice";
40
40
  import { wake } from "./commands/wake";
41
41
  import { red } from "./lib/color";
42
42
  import { resolveAssistant, setActiveAssistant } from "./lib/assistant-config";
43
-
44
- // The environment resolver's version channel reads this global (see
45
- // shared/seeds); the entrypoint is the one place that knows the running
46
- // package root. `??=` so a test preload's value survives (cli tests run from
47
- // a source checkout, whose `-local.` stamp would pin the ambient environment
48
- // to `local`).
49
- (globalThis as { __CADET_OWN_VERSION__?: string }).__CADET_OWN_VERSION__ ??=
50
- resolveVersion(join(import.meta.dir, "../.."));
51
43
  const commands = {
52
44
  backup,
53
45
  app,
@@ -8,13 +8,14 @@ import {
8
8
  } from "fs";
9
9
 
10
10
  import { dirname, join } from "path";
11
- import { z } from "zod";
12
11
  import {
13
12
  computeDeviceId,
13
+ GuardianLeasePermanentError,
14
14
  guardianTokenPath,
15
15
  isConfidentialRefreshUrl,
16
16
  loadGuardianToken,
17
17
  loopbackSafeFetch,
18
+ parseGuardianToken,
18
19
  saveGuardianToken,
19
20
  type GuardianTokenData,
20
21
  } from "@mycadet/local-mode";
@@ -159,6 +160,15 @@ function isUnreachableGatewayError(error: unknown): boolean {
159
160
  function classifyRefreshThrownError(
160
161
  error: unknown,
161
162
  ): GuardianTokenRefreshResult {
163
+ // A malformed 200 body throws from parseGuardianToken: the gateway
164
+ // answered, so this is its contract that's broken — 502, not 500.
165
+ if (error instanceof GuardianLeasePermanentError) {
166
+ return {
167
+ ok: false,
168
+ status: 502,
169
+ error: "Assistant gateway returned a malformed token refresh response",
170
+ };
171
+ }
162
172
  if (isTimeoutLikeError(error)) {
163
173
  return {
164
174
  ok: false,
@@ -310,32 +320,11 @@ export async function refreshGuardianTokenResult(
310
320
  return classifyRefreshHttpStatus(response.status);
311
321
  }
312
322
 
313
- // The refresh endpoint answers with the rotated access credential only
314
- // (no refresh-token family, no device binding), so it parses with its own
315
- // lenient shape rather than the pair response's full token-file schema.
316
- const parsed = GuardianRefreshResponseSchema.safeParse(
317
- await response.json(),
318
- );
319
- if (!parsed.success) {
320
- return {
321
- ok: false,
322
- status: 502,
323
- error: "Assistant gateway returned a malformed token refresh response",
324
- };
325
- }
326
- const refreshed: GuardianTokenData = {
327
- guardianPrincipalId:
328
- parsed.data.guardianPrincipalId ?? tokenData.guardianPrincipalId,
329
- accessToken: parsed.data.accessToken,
330
- accessTokenExpiresAt:
331
- parsed.data.accessTokenExpiresAt ?? tokenData.accessTokenExpiresAt,
332
- refreshToken: tokenData.refreshToken,
333
- refreshTokenExpiresAt: tokenData.refreshTokenExpiresAt,
334
- refreshAfter: parsed.data.refreshAfter ?? tokenData.refreshAfter,
335
- deviceId: tokenData.deviceId,
336
- leasedAt: new Date().toISOString(),
337
- pairedGatewayUrl: tokenData.pairedGatewayUrl,
338
- };
323
+ // The refresh endpoint answers with the full guardian credential
324
+ // contract — the same shape the pairing mint emits — so one schema
325
+ // parses the credential from mint to rotation to token file. A
326
+ // malformed body throws and the surrounding catch classifies it.
327
+ const refreshed = parseGuardianToken(await response.json());
339
328
  saveGuardianToken(assistantId, refreshed);
340
329
  return { ok: true, token: refreshed };
341
330
  } catch (error) {
@@ -347,13 +336,6 @@ export async function refreshGuardianTokenResult(
347
336
  }
348
337
  }
349
338
 
350
- const GuardianRefreshResponseSchema = z.object({
351
- guardianPrincipalId: z.string().optional(),
352
- accessToken: z.string(),
353
- accessTokenExpiresAt: z.union([z.string(), z.number()]).optional(),
354
- refreshAfter: z.string().optional(),
355
- });
356
-
357
339
  /** Convenience wrapper around {@link refreshGuardianTokenResult}. */
358
340
  export async function refreshGuardianToken(
359
341
  gatewayUrl: string,