@cavelon/cli 0.1.9 → 0.1.11

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 (64) hide show
  1. package/README.md +23 -10
  2. package/dist/access.js +194 -0
  3. package/dist/acting.js +123 -0
  4. package/dist/agent-env.js +20 -2
  5. package/dist/branches.js +4 -3
  6. package/dist/capacity.js +3 -2
  7. package/dist/code-hints.js +5 -4
  8. package/dist/command.js +6 -0
  9. package/dist/commands/api.js +40 -21
  10. package/dist/commands/async.js +7 -4
  11. package/dist/commands/chat.js +18 -15
  12. package/dist/commands/db.js +367 -0
  13. package/dist/commands/docs.js +87 -9
  14. package/dist/commands/fmt.js +6 -4
  15. package/dist/commands/index.js +8 -0
  16. package/dist/commands/init.js +106 -28
  17. package/dist/commands/limits-set.js +12 -11
  18. package/dist/commands/limits.js +4 -3
  19. package/dist/commands/loops.js +75 -19
  20. package/dist/commands/models.js +12 -13
  21. package/dist/commands/sandboxes.js +22 -16
  22. package/dist/commands/schema.js +6 -5
  23. package/dist/commands/session.js +134 -37
  24. package/dist/commands/setup.js +4 -2
  25. package/dist/commands/solution.js +289 -93
  26. package/dist/commands/tenants.js +58 -22
  27. package/dist/commands/values.js +110 -45
  28. package/dist/commands/work.js +31 -17
  29. package/dist/confirm-token.js +15 -5
  30. package/dist/context.js +3 -1
  31. package/dist/contracts.js +39 -13
  32. package/dist/database-queries.js +301 -0
  33. package/dist/default-route.js +1 -1
  34. package/dist/features.js +2 -1
  35. package/dist/fsutil.js +14 -0
  36. package/dist/harness-ref.js +6 -4
  37. package/dist/http.js +39 -4
  38. package/dist/invoke.js +7 -2
  39. package/dist/kit-codes.js +14 -2
  40. package/dist/limits.js +1 -1
  41. package/dist/main.js +13 -1
  42. package/dist/mcp.js +105 -26
  43. package/dist/openapi.js +5 -2
  44. package/dist/operations.js +3 -2
  45. package/dist/package-check.js +29 -4
  46. package/dist/package-files.js +5 -1
  47. package/dist/package-format.js +10 -0
  48. package/dist/preview-report.js +46 -7
  49. package/dist/principal.js +24 -2
  50. package/dist/printed.js +182 -0
  51. package/dist/project.js +2 -1
  52. package/dist/results.js +1 -1
  53. package/dist/secret-access.js +42 -0
  54. package/dist/session.js +39 -19
  55. package/dist/shell.js +0 -3
  56. package/dist/skills/cavelon-authoring/SKILL.md +88 -4
  57. package/dist/skills/cavelon-long-running/SKILL.md +9 -3
  58. package/dist/skills/cavelon-loop/SKILL.md +81 -19
  59. package/dist/skills/cavelon-testing/SKILL.md +28 -1
  60. package/dist/tenant-choice.js +7 -7
  61. package/dist/trace-view.js +11 -1
  62. package/dist/update-check.js +2 -2
  63. package/dist/user-config.js +24 -0
  64. package/package.json +1 -1
@@ -3,7 +3,7 @@ import { boolOption, positional } from "../command.js";
3
3
  import { compareContracts, Contracts } from "../contracts.js";
4
4
  import { deleteToken, saveToken } from "../credentials.js";
5
5
  import { CavelonError, ExitCode, usageError } from "../errors.js";
6
- import { keyValues } from "../format.js";
6
+ import { clip, keyValues } from "../format.js";
7
7
  import { callStable } from "../invoke.js";
8
8
  import { formatQuota, formatValue, limitRef, readLimits, readQuotas } from "../limits.js";
9
9
  import { ApiClient, tokensDisabledError } from "../http.js";
@@ -11,11 +11,14 @@ import { readAll } from "../io.js";
11
11
  import { listPreviews, readPull } from "../local-state.js";
12
12
  import { solutionState, solutionStateLines } from "./solution.js";
13
13
  import { expiryOf, readPrincipal, readTenantless } from "../principal.js";
14
+ import { maySetSecrets } from "../secret-access.js";
14
15
  import { readHidden } from "../prompt.js";
15
- import { isUuid, lookupTenantId, requireInstance, requireToken, tenantRequiredError } from "../session.js";
16
- import { cavelonCommand } from "../shell.js";
16
+ import { isUuid, knownTenantId, lookupTenantId, requireInstance, requireToken, tenantRequiredError } from "../session.js";
17
+ import { cavelonCommand, fill } from "../printed.js";
17
18
  import { choicesOf, chooseTenant, commandLines, describeTenant, listsTenants, noTenantError, tenantOpenError, tenantRef, tenantTitle } from "../tenant-choice.js";
18
- import { loadUserConfig, saveUserConfig, tokenKind, updateInstance } from "../user-config.js";
19
+ import { loadUserConfig, saveUserConfig, tokenKind, updateInstance, withTenantRefs } from "../user-config.js";
20
+ import { checkKeyTenant } from "../acting.js";
21
+ import { accessFor, accessOf, mayActivate, operationAccess } from "../access.js";
19
22
  async function readCapabilities(contracts, refresh) {
20
23
  const caps = await contracts.capabilities({ refresh });
21
24
  return { caps, needsTenant: contracts.needsTenant };
@@ -69,6 +72,8 @@ async function readToken(ctx, fromStdin) {
69
72
  }
70
73
  export const login = {
71
74
  name: "login",
75
+ storesTarget: true,
76
+ tenantless: true,
72
77
  summary: "Store a token for an instance (a person runs this, never the agent).",
73
78
  description: "Asks for the token without echoing it, or reads it from standard input with --token-stdin. It is never an argument.\n" +
74
79
  "Create a personal access token (cvpat_…) on /account/access-tokens; a tenant API key (cbp_…) also works.\n" +
@@ -98,6 +103,8 @@ export const login = {
98
103
  ctx.warn("The token starts with neither cvpat_ nor cbp_; the instance may refuse it.");
99
104
  const client = new ApiClient({ url, token }, ctx.io.env);
100
105
  const contracts = new Contracts(client, ctx.io.env, ctx.io.now);
106
+ if (kind === "api_key")
107
+ await checkKeyTenant(client, { ...session, token, tokenKind: kind }, (m) => ctx.warn(m));
101
108
  let tenant;
102
109
  let chosen;
103
110
  if (session.tenant && kind !== "api_key") {
@@ -150,7 +157,7 @@ export const login = {
150
157
  const store = await saveToken(ctx.io.env, url, token);
151
158
  const remember = tenant && chosen;
152
159
  await updateInstance(ctx.io.env, url, (current) => ({
153
- ...current,
160
+ ...withTenantRefs(current, remember ? [tenant.ref, tenant.slug] : [], tenant?.id ?? "", token, ctx.io.now()),
154
161
  credential_store: store.kind,
155
162
  token_kind: kind,
156
163
  logged_in_at: ctx.io.now().toISOString(),
@@ -160,7 +167,6 @@ export const login = {
160
167
  tenant_id: tenant.id,
161
168
  tenant_name: tenant.name,
162
169
  tenant_slug: tenant.slug,
163
- tenant_ids: { ...current.tenant_ids, [tenant.ref]: tenant.id, ...(tenant.slug ? { [tenant.slug]: tenant.id } : {}) },
164
170
  }
165
171
  : {}),
166
172
  }), { makeCurrent: true });
@@ -174,11 +180,12 @@ export const login = {
174
180
  return {
175
181
  data: { instance: url, credential: { kind, store: store.kind }, owner: null, tenant: null, instance_version: null, contracts: null },
176
182
  text: `Logged in to ${url}. Token stored in the ${store.kind === "keyring" ? "credential store" : "user-only file"}. ` +
177
- "No tenant is chosen yet: `cavelon use <name or slug>` chooses one; `cavelon tenant list --search <part of the name>` finds its slug.",
183
+ `No tenant is chosen yet: \`${cavelonCommand("use", fill("name or slug"))}\` chooses one; ` +
184
+ `\`${cavelonCommand("tenant", "list", "--search", fill("part of the name"))}\` finds its slug.`,
178
185
  };
179
186
  }
180
187
  if (needsTenant) {
181
- ctx.warn("This token works in Platform mode; choose a tenant with `cavelon use <tenant>` before tenant commands.");
188
+ ctx.warn(`This token works in Platform mode; choose a tenant with \`${cavelonCommand("use", fill("tenant"))}\` before tenant commands.`);
182
189
  }
183
190
  else {
184
191
  if (acting && reach) {
@@ -187,7 +194,7 @@ export const login = {
187
194
  ctx.warn(`Without a tenant this token acts in ${acting.name ?? acting.id}. To work in another tenant, run:\n${commandLines(others, (r) => cavelonCommand("use", r))}`);
188
195
  }
189
196
  else if (acting && acting.others.length) {
190
- ctx.warn(`Without a tenant this token acts in ${acting.name ?? acting.id}. Your other tenants: ${acting.others.join(", ")}; \`cavelon use <tenant>\` chooses one this token reaches.`);
197
+ ctx.warn(`Without a tenant this token acts in ${acting.name ?? acting.id}. Your other tenants: ${acting.others.join(", ")}; \`${cavelonCommand("use", fill("tenant"))}\` chooses one this token reaches.`);
191
198
  }
192
199
  for (const warning of compareContracts(caps))
193
200
  ctx.warn(warning);
@@ -227,7 +234,7 @@ export const login = {
227
234
  if (tenant && chosen === "only")
228
235
  where = ` Using tenant ${tenantTitle(tenant)}, the only one this token reaches.`;
229
236
  else if (tenant && chosen === "picked")
230
- where = ` Using tenant ${tenantTitle(tenant)}; \`cavelon use\` chooses another.`;
237
+ where = ` Using tenant ${tenantTitle(tenant)}; \`${cavelonCommand("use")}\` chooses another.`;
231
238
  else if (acting) {
232
239
  const named = { id: acting.id, name: acting.name ?? actingNamed?.name, slug: actingNamed?.slug };
233
240
  where = ` Acting in tenant ${tenantTitle(named)}, the one the instance chooses for this token.`;
@@ -240,6 +247,7 @@ export const login = {
240
247
  };
241
248
  export const logout = {
242
249
  name: "logout",
250
+ tenantless: true,
243
251
  summary: "Delete the stored token for an instance.",
244
252
  readOnly: false,
245
253
  destructive: true,
@@ -281,6 +289,8 @@ function tenantSourceText(session) {
281
289
  return session.project
282
290
  ? "`cavelon use` (this folder's cavelon.yaml names no tenant for this instance)"
283
291
  : "`cavelon use`, for every folder without a cavelon.yaml (--tenant chooses another for one command)";
292
+ case "session":
293
+ return "use_tenant, for this MCP session only (the tenant stored for your user is unchanged)";
284
294
  default:
285
295
  return session.tenantSource;
286
296
  }
@@ -301,7 +311,7 @@ function reachText(reach) {
301
311
  const own = reach.tenants.length;
302
312
  return `every tenant (as operator; ${own ? `${own} membership${own === 1 ? "" : "s"} of your own` : "no memberships of your own"})`;
303
313
  }
304
- return `${reach.tenants.length} tenant${reach.tenants.length === 1 ? "" : "s"} (\`cavelon tenant list\`)`;
314
+ return `${reach.tenants.length} tenant${reach.tenants.length === 1 ? "" : "s"} (\`${cavelonCommand("tenant", "list")}\`)`;
305
315
  }
306
316
  export const whoami = {
307
317
  name: "whoami",
@@ -343,8 +353,9 @@ export const whoami = {
343
353
  }
344
354
  const listed = reach?.tenants.find((t) => t.id === contextTenant);
345
355
  const stored = session.settings.tenant_id === contextTenant ? session.settings : undefined;
346
- let tenantName = listed?.name ?? membership?.tenant_name ?? stored?.tenant_name ?? null;
347
- let tenantSlug = listed?.slug ?? membership?.tenant_slug ?? stored?.tenant_slug ?? null;
356
+ const acting = principal?.tenant?.id === contextTenant ? principal.tenant : undefined;
357
+ let tenantName = acting?.name ?? listed?.name ?? membership?.tenant_name ?? stored?.tenant_name ?? null;
358
+ let tenantSlug = acting?.slug ?? listed?.slug ?? membership?.tenant_slug ?? stored?.tenant_slug ?? null;
348
359
  if (contextTenant && (!tenantName || !tenantSlug)) {
349
360
  const described = await describeTenant(client, contextTenant);
350
361
  tenantName ??= described.name ?? null;
@@ -352,12 +363,13 @@ export const whoami = {
352
363
  }
353
364
  if (nowhere && reach) {
354
365
  ctx.warn(reach.tenants.length
355
- ? `No tenant is chosen. Choose one with \`cavelon use\`, or run the line for the tenant you want:\n${commandLines(reach.tenants, (r) => cavelonCommand("use", r))}`
356
- : "No tenant is chosen. This token reaches every tenant: `cavelon use <name or slug>` chooses one.");
366
+ ? `No tenant is chosen. Choose one with \`${cavelonCommand("use")}\`, or run the line for the tenant you want:\n${commandLines(reach.tenants, (r) => cavelonCommand("use", r))}`
367
+ : `No tenant is chosen. This token reaches every tenant: \`${cavelonCommand("use", fill("name or slug"))}\` chooses one.`);
357
368
  }
358
369
  const expiry = expiryOf(principal, ctx.io.now());
359
370
  if (expiry.warning)
360
371
  ctx.warn(expiry.warning);
372
+ const access = accessOf(principal);
361
373
  const data = {
362
374
  instance: {
363
375
  url: session.url,
@@ -373,10 +385,14 @@ export const whoami = {
373
385
  prefix: principal?.token?.prefix ?? principal?.api_key?.prefix ?? null,
374
386
  expires_at: expiry.expires_at,
375
387
  expires_in_days: expiry.days_left,
376
- may_activate: principal?.token ? principal.token.may_activate : null,
388
+ may_activate: mayActivate(access),
389
+ may_set_secrets: maySetSecrets(principal),
390
+ may_set_variables: maySetVariables(access),
377
391
  ceiling_role: principal?.token?.ceiling_role ?? null,
378
392
  platform_mode_allowed: principal?.token?.platform_mode_allowed ?? null,
379
393
  scopes: principal?.api_key?.scopes ?? null,
394
+ permissions: access?.permissions ?? null,
395
+ needs_a_person: access?.complete ? access.needsAPerson : null,
380
396
  published: Boolean(principal),
381
397
  },
382
398
  owner: me
@@ -396,7 +412,7 @@ export const whoami = {
396
412
  role: me?.context?.effective_role ?? me?.role ?? null,
397
413
  };
398
414
  if (needsTenant)
399
- ctx.warn("No tenant selected; this token is in Platform mode. Choose one with `cavelon use <tenant>`.");
415
+ ctx.warn(`No tenant selected; this token is in Platform mode. Choose one with \`${cavelonCommand("use", fill("tenant"))}\`.`);
400
416
  const owner = session.tokenKind === "api_key"
401
417
  ? principal?.api_key
402
418
  ? `the tenant API key "${principal.api_key.name}"`
@@ -420,7 +436,12 @@ export const whoami = {
420
436
  ["role", data.role ?? undefined],
421
437
  ["credential", `${session.tokenKind === "api_key" ? "tenant API key" : session.tokenKind === "personal_access_token" ? "personal access token" : "token"}${tokenName} from ${credentialSource(session)}`],
422
438
  ["expires", expires],
423
- ["may activate", principal?.token ? (principal.token.may_activate ? "yes" : "no (a person activates in the Admin)") : undefined],
439
+ ["may activate", activateText(data.credential.may_activate, access)],
440
+ ["may set secrets", secretsText(data.credential.may_set_secrets)],
441
+ ["may set variables", variablesText(data.credential.may_set_variables)],
442
+ ["scopes", data.credential.scopes ? data.credential.scopes.join(", ") || "none" : undefined],
443
+ ["permissions", permissionsText(access)],
444
+ ["needs a person", needsAPersonText(access)],
424
445
  ["platform mode", typeof principal?.token?.platform_mode_allowed === "boolean" ? platformModeText(principal.token) : undefined],
425
446
  ["reaches", reach ? reachText(reach) : undefined],
426
447
  ["version", data.instance.version ? `${data.instance.version}${versionCachedAt ? ` (cached at ${versionCachedAt})` : ""}` : undefined],
@@ -428,12 +449,52 @@ export const whoami = {
428
449
  };
429
450
  },
430
451
  };
452
+ const LISTED = 12;
453
+ function activateText(may, access) {
454
+ if (may === null)
455
+ return undefined;
456
+ if (may)
457
+ return "yes";
458
+ return access?.principal.token && !access.principal.token.may_activate
459
+ ? "no (the token was created without \"may activate\"; a person activates in the Admin)"
460
+ : "no (a person whose role may activate does it, in the Admin)";
461
+ }
462
+ export function maySetVariables(access) {
463
+ return operationAccess(access, "PUT /api/v1/variables/{name}").allowed;
464
+ }
465
+ function variablesText(may) {
466
+ if (may === null)
467
+ return undefined;
468
+ return may ? "yes" : "no (a tenant Owner sets them, in the Admin or with their own token)";
469
+ }
470
+ function permissionsText(access) {
471
+ if (!access?.permissions)
472
+ return access ? "not published for this credential by this instance" : undefined;
473
+ const all = access.permissions;
474
+ if (!all.length)
475
+ return "none";
476
+ return `${all.slice(0, LISTED).join(", ")}${all.length > LISTED ? `, … ${all.length - LISTED} more (--json)` : ""}`;
477
+ }
478
+ function needsAPersonText(access) {
479
+ if (!access?.complete || !access.needsAPerson.length)
480
+ return undefined;
481
+ const all = access.needsAPerson;
482
+ const shown = all.slice(0, LISTED).map((o) => `\n ${o.method} ${o.path}${o.reason ? ` (${clip(o.reason, 80)})` : ""}`);
483
+ return `${all.length} operation${all.length === 1 ? "" : "s"} a person runs, not this credential:${shown.join("")}${all.length > LISTED ? `\n … ${all.length - LISTED} more (--json)` : ""}`;
484
+ }
485
+ function secretsText(may) {
486
+ if (may === null)
487
+ return undefined;
488
+ return may ? "yes" : "no (a tenant Owner sets them, in the Admin or with their own token)";
489
+ }
431
490
  export const use = {
432
491
  name: "use",
433
492
  summary: "Choose the tenant this instance's commands act in.",
434
493
  description: "Stored per instance for your user. CAVELON_TENANT, --tenant and a cavelon.yaml tenant take precedence over it.\n" +
435
494
  "Without a tenant, it lists the tenants the token reaches: a person chooses one on a terminal by number or part of its name; " +
436
- "without a terminal it prints one `cavelon use` line per tenant, and as an MCP tool it returns them as choices and changes nothing.",
495
+ "without a terminal it prints one `cavelon use` line per tenant, and as an MCP tool it returns them as choices and changes nothing.\n" +
496
+ "As an MCP tool it never changes the tenant stored for your user: it chooses the tenant for that MCP session only, " +
497
+ "until the session ends or it is cleared, so an agent's choice never moves where your own commands go.",
437
498
  readOnly: false,
438
499
  idempotent: true,
439
500
  mcpTool: "use_tenant",
@@ -444,7 +505,17 @@ export const use = {
444
505
  const session = await ctx.session();
445
506
  const url = requireInstance(session);
446
507
  requireToken(session);
508
+ const inSession = ctx.mode === "mcp";
509
+ if (inSession && !ctx.globals.sessionTenants)
510
+ throw usageError("This MCP server keeps no session, so use_tenant cannot choose a tenant; pass tenant to each tool instead.");
447
511
  if (boolOption(input, "clear")) {
512
+ if (inSession) {
513
+ ctx.globals.sessionTenants.delete(url);
514
+ return {
515
+ data: { instance: url, tenant: null, scope: "session" },
516
+ text: `No tenant chosen for ${url} in this MCP session; tools act in the tenant chosen for this folder or with \`cavelon use\` again.`,
517
+ };
518
+ }
448
519
  await updateInstance(ctx.io.env, url, (c) => ({ ...c, tenant: undefined, tenant_id: undefined, tenant_name: undefined, tenant_slug: undefined }));
449
520
  return { data: { instance: url, tenant: null }, text: `No tenant chosen for ${url}.` };
450
521
  }
@@ -460,7 +531,7 @@ export const use = {
460
531
  else {
461
532
  const tenantless = await readTenantless(client);
462
533
  if (!listsTenants(tenantless)) {
463
- throw usageError("Which tenant? This instance does not list the tenants a token reaches.", "Run `cavelon use <name, slug or id>`; `cavelon tenant list` shows the tenants this token can see.");
534
+ throw usageError("Which tenant? This instance does not list the tenants a token reaches.", `Run \`${cavelonCommand("use", fill("name, slug or id"))}\`; \`${cavelonCommand("tenant", "list")}\` shows the tenants this token can see.`);
464
535
  }
465
536
  const command = (r) => cavelonCommand("use", r);
466
537
  if (ctx.mode === "mcp" && (tenantless.reachesAll || tenantless.tenants.length !== 1)) {
@@ -484,21 +555,27 @@ export const use = {
484
555
  const probe = new ApiClient({ ...client.target, tenantId: found.id }, ctx.io.env);
485
556
  await probe.get("/api/v1/meta/capabilities", { allow: [404] });
486
557
  const stored = found.slug ?? ref;
558
+ const title = tenantTitle({ id: found.id, name: found.name, slug: found.slug ?? (isUuid(ref) ? undefined : ref) });
559
+ if (session.tenantSource && session.tenantSource !== "use" && session.tenantSource !== "session") {
560
+ ctx.warn(`${session.tenantSource} names tenant "${session.tenant}" and takes precedence over \`use\` here.`);
561
+ }
562
+ const shown = { ref: stored, id: found.id, name: found.name ?? null, slug: found.slug ?? null };
563
+ if (inSession) {
564
+ ctx.globals.sessionTenants.set(url, { ref: stored, id: found.id, ...(found.name ? { name: found.name } : {}), ...(found.slug ? { slug: found.slug } : {}) });
565
+ return {
566
+ data: { instance: url, tenant: shown, scope: "session" },
567
+ text: `Using tenant ${title} on ${url} for this MCP session; the tenant stored for your user (\`cavelon use\` in a terminal) is unchanged.`,
568
+ };
569
+ }
570
+ const token = requireToken(session);
487
571
  await updateInstance(ctx.io.env, url, (c) => ({
488
- ...c,
572
+ ...withTenantRefs(c, [ref, stored], found.id, token, ctx.io.now()),
489
573
  tenant: stored,
490
574
  tenant_id: found.id,
491
575
  tenant_name: found.name ?? (c.tenant_id === found.id ? c.tenant_name : undefined),
492
576
  tenant_slug: found.slug ?? (c.tenant_id === found.id ? c.tenant_slug : undefined),
493
- tenant_ids: { ...c.tenant_ids, [ref]: found.id, [stored]: found.id },
494
577
  }));
495
- if (session.tenantSource && session.tenantSource !== "use") {
496
- ctx.warn(`${session.tenantSource} names tenant "${session.tenant}" and takes precedence over \`use\` here.`);
497
- }
498
- return {
499
- data: { instance: url, tenant: { ref: stored, id: found.id, name: found.name ?? null, slug: found.slug ?? null } },
500
- text: `Using tenant ${tenantTitle({ id: found.id, name: found.name, slug: found.slug ?? (isUuid(ref) ? undefined : ref) })} on ${url}.`,
501
- };
578
+ return { data: { instance: url, tenant: shown }, text: `Using tenant ${title} on ${url}.` };
502
579
  },
503
580
  };
504
581
  async function nearQuotas(ctx) {
@@ -537,7 +614,7 @@ function limitsLine(state, near) {
537
614
  return "not published by this instance";
538
615
  const quotas = state.quotas_unavailable ? `\n quotas not readable: ${state.quotas_unavailable}` : "";
539
616
  if (!near.length)
540
- return `${state.quotas_unavailable ? "quotas unknown" : "none close to a quota"} (\`cavelon limits\` lists them)${quotas}`;
617
+ return `${state.quotas_unavailable ? "quotas unknown" : "none close to a quota"} (\`${cavelonCommand("limits")}\` lists them)${quotas}`;
541
618
  return `close to a quota: ${near.map((q) => `${q.key} ${formatQuota(q)}`).join("; ")}${quotas}`;
542
619
  }
543
620
  export const status = {
@@ -573,8 +650,10 @@ export const status = {
573
650
  let published;
574
651
  let reachError;
575
652
  let client;
653
+ let access;
576
654
  if (offline && session.url) {
577
- const cached = await new Contracts(new ApiClient({ url: session.url }, ctx.io.env), ctx.io.env, ctx.io.now)
655
+ const target = { url: session.url, token: session.token, tenantId: knownTenantId(session, ctx.io.now()) };
656
+ const cached = await new Contracts(new ApiClient(target, ctx.io.env), ctx.io.env, ctx.io.now)
578
657
  .cachedOnly("capabilities.json")
579
658
  .catch(() => undefined);
580
659
  if (cached?.value?.instance?.version) {
@@ -594,6 +673,15 @@ export const status = {
594
673
  shown.name = stored?.tenant_name ?? described.name ?? null;
595
674
  shown.slug = stored?.tenant_slug ?? described.slug ?? null;
596
675
  }
676
+ access = await accessFor(client);
677
+ if (access && data.credential) {
678
+ Object.assign(data.credential, {
679
+ scopes: access.scopes,
680
+ permissions: access.permissions,
681
+ may_activate: mayActivate(access),
682
+ needs_a_person: access.complete ? access.needsAPerson.length : null,
683
+ });
684
+ }
597
685
  const contracts = await ctx.contracts();
598
686
  const caps = await contracts.liveCapabilities();
599
687
  data.instance_version = caps?.instance.version ?? null;
@@ -645,10 +733,12 @@ export const status = {
645
733
  ? `${shownTenant?.id && (shownTenant.name || shownTenant.slug) ? tenantTitle({ id: shownTenant.id, name: shownTenant.name, slug: shownTenant.slug }) : session.tenant}, from ${tenantSourceText(session)}`
646
734
  : session.tokenKind === "api_key"
647
735
  ? "the API key's tenant"
648
- : "not chosen (`cavelon use` lists your tenants to choose from)";
736
+ : `not chosen (\`${cavelonCommand("use")}\` lists your tenants to choose from)`;
649
737
  const lines = [
650
- ["instance", session.url ? `${session.url} (${session.urlSource})` : "none (`cavelon login --instance <url>`)"],
738
+ ["instance", session.url ? `${session.url} (${session.urlSource})` : `none (\`${cavelonCommand("login", "--instance", fill("url"))}\`)`],
651
739
  ["credential", session.token ? `${session.tokenKind} from ${credentialSource(session)}` : "none"],
740
+ ["scopes", access?.scopes ? access.scopes.join(", ") || "none" : undefined],
741
+ ["permissions", permissionsText(access)],
652
742
  ["tenant", tenantText],
653
743
  ["solution", session.project ? session.project.file : "none (no cavelon.yaml here or above)"],
654
744
  ];
@@ -663,7 +753,7 @@ export const status = {
663
753
  "open previews",
664
754
  solution.open_previews.length
665
755
  ? solution.open_previews
666
- .map((p) => `\n ${p.preview_id}${p.env ? ` env ${p.env}` : ""} ${p.created_at} ${p.expired ? "expired (`cavelon apply --discard all` removes it)" : `expires ${p.expires_at}`}`)
756
+ .map((p) => `\n ${p.preview_id}${p.env ? ` env ${p.env}` : ""} ${p.created_at} ${p.expired ? `expired (\`${cavelonCommand("apply", "--discard", "all")}\` removes it)` : `expires ${p.expires_at}`}`)
667
757
  .join("")
668
758
  : "none",
669
759
  ]);
@@ -687,7 +777,7 @@ export const status = {
687
777
  data.capacity_waits = waiting;
688
778
  const ops = data.operations;
689
779
  const others = ops.other_solutions
690
- ? ` (${ops.other_solutions} operation${ops.other_solutions === 1 ? "" : "s"} of other solutions not shown; \`cavelon api list_operations_route\` lists all)`
780
+ ? ` (${ops.other_solutions} operation${ops.other_solutions === 1 ? "" : "s"} of other solutions not shown; \`${cavelonCommand("api", "list_operations_route")}\` lists all)`
691
781
  : "";
692
782
  const heading = ops.scope === "solution" ? `Running operations of ${ops.harness} and the tenant's shared work${others}` : "Running operations in the tenant";
693
783
  text += operations.length
@@ -741,12 +831,19 @@ async function operationSolutions(ctx, operations) {
741
831
  return owners;
742
832
  }
743
833
  export async function rememberTenant(ctx, url, tenant) {
834
+ if (ctx.mode === "mcp") {
835
+ if (!ctx.globals.sessionTenants)
836
+ throw usageError("This MCP server keeps no session to choose the new tenant in; pass tenant to each tool instead.");
837
+ ctx.globals.sessionTenants.set(url, tenant);
838
+ return "session";
839
+ }
840
+ const token = requireToken(await ctx.session());
744
841
  await updateInstance(ctx.io.env, url, (c) => ({
745
- ...c,
842
+ ...withTenantRefs(c, [tenant.ref], tenant.id, token, ctx.io.now()),
746
843
  tenant: tenant.ref,
747
844
  tenant_id: tenant.id,
748
845
  tenant_name: tenant.name,
749
846
  tenant_slug: tenant.slug,
750
- tenant_ids: { ...c.tenant_ids, [tenant.ref]: tenant.id },
751
847
  }));
848
+ return "user";
752
849
  }
@@ -7,7 +7,7 @@ import { readJsonFile, writeFileAtomic } from "../fsutil.js";
7
7
  import { configDir } from "../paths.js";
8
8
  import { canAsk, readLine } from "../prompt.js";
9
9
  import { probeMcpServer } from "../run-program.js";
10
- import { cavelonCommand } from "../shell.js";
10
+ import { cavelonCommand } from "../printed.js";
11
11
  import { agentByName, applyPlan, checkAgent, findAgent, loadSkills, planAgent, removeAgent, serverCommand, setupAgents, } from "../setup-agents.js";
12
12
  import { KIT_VERSION } from "../version.js";
13
13
  import { login, whoami } from "./session.js";
@@ -215,7 +215,7 @@ async function runSetup(ctx, input) {
215
215
  const changed = results.filter((r) => r.changes.some((c) => c.outcome === "done"));
216
216
  const next = [];
217
217
  if (declined)
218
- next.push("Nothing was changed. Run cavelon setup again when you are ready.");
218
+ next.push(`Nothing was changed. Run ${cavelonCommand("setup")} again when you are ready.`);
219
219
  else {
220
220
  if (loginReport?.next)
221
221
  next.push(loginReport.next);
@@ -397,6 +397,8 @@ async function runRemove(ctx, input) {
397
397
  }
398
398
  export const setup = {
399
399
  name: "setup",
400
+ storesTarget: true,
401
+ tenantless: true,
400
402
  summary: "Set up your coding agents for Cavelon and log in, in one guided step.",
401
403
  description: "Finds Claude Code, Codex, Cursor, VS Code with GitHub Copilot, Gemini CLI and Kiro, shows what it will change for each, asks once\n" +
402
404
  "and does it: Claude Code and Codex get the Cavelon plugin through their own plugin command; the others get the `cavelon` MCP\n" +