@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
@@ -18,7 +18,16 @@ use it when you parse the result.
18
18
  deactivate; whether it is ready to
19
19
  activate, its latest test run), the last pull and the open previews.
20
20
  `cavelon whoami` says who the token acts as, when it expires, whether it may
21
- enter Platform mode and which tenants it reaches.
21
+ enter Platform mode, which tenants it reaches, and what it may do there: its
22
+ permissions (an API key's scopes too), whether it may activate and set
23
+ variables, and the operations a person runs instead.
24
+ - **Offer only what the credential may do.** Over MCP, a tool whose
25
+ description starts with "Not for this credential" is one the instance says
26
+ this token or key may not use in this tenant; `api_list` marks such
27
+ operations (`may_send: false`). Do not call them to find out: tell the
28
+ person who does it (the description says who), as for a secret. Where the
29
+ instance does not say (an older one), try, and a refusal names what the
30
+ credential lacks.
22
31
  - **Never handle a token.** If a command exits 7 (not authorised) or says no
23
32
  token, ask the person to run `cavelon login --instance <url>` themselves (in a
24
33
  terminal, or with a `!` prefix where your client offers one). Never ask them
@@ -28,7 +37,11 @@ use it when you parse the result.
28
37
  their terminal, `cavelon secrets set <name>` (as `apply` prints it, with its
29
38
  `--env` and `--tenant`). Never ask for the value, never pipe or pass one yourself; there is
30
39
  no MCP tool for it. Plain-text variables you may set with `cavelon variables
31
- set <name> <value>` when the value is not a credential.
40
+ set <name> <value>` when the value is not a credential and your role may
41
+ manage the tenant's settings, as for a secret: a Builder's may not, and
42
+ `cavelon whoami` then says "may set variables: no". Then tell the person
43
+ that a tenant Owner sets it, in the Admin under Settings › Variables or with
44
+ their own token.
32
45
  - **Pass on an update warning.** The first MCP result of a session may warn
33
46
  that `cavelon`, the Cavelon plugin or this folder's skills are behind the
34
47
  latest release (`… is out; this is …`, `The Cavelon plugin is …`), with the
@@ -58,7 +71,13 @@ use it when you parse the result.
58
71
  pass `--tenant` (and `--instance`) to every command, `harness new` and
59
72
  `pull` included: without it a command acts in the tenant `cavelon use`
60
73
  chose, which may be another one. The commands `cavelon` prints carry the
61
- `--tenant` you gave; run them as printed. `apply` never creates a solution: when the one an env file names
74
+ `--tenant` (and `--env`) you gave, confirm lines included; run them as printed.
75
+ Every preview names where it acts (`acts on:`, `target` in `--json`): the
76
+ instance, the tenant and the mode; show it to the person with the change.
77
+ Over MCP, `use_tenant` chooses the tenant for this session only and never
78
+ changes the person's stored tenant. A tenant API key in `CAVELON_TOKEN` acts
79
+ only in its own tenant: a command that names another is refused with
80
+ `tenant_mismatch`; never work around it, tell the person which key fits. `apply` never creates a solution: when the one an env file names
62
81
  is missing it stops with `solution_not_found` and names that command. To start from a package file (a blueprint, an
63
82
  export from another instance), run
64
83
  `cavelon init --instance <url> --tenant <tenant> --from <file>` instead of
@@ -71,6 +90,18 @@ use it when you parse the result.
71
90
  In their own terminal, a plain `cavelon init` asks them. Ask for the instance
72
91
  URL if you do not know it.
73
92
 
93
+ - **Database connections are set up by a person.** A database query tool
94
+ needs a connection of the name and dialect its package names, tested
95
+ successfully, in every tenant it is applied to. A superadmin creates it in
96
+ the Admin (Settings › Security & access › Databases), and the tenant Owner tests it
97
+ (`cavelon db test <connection>`); you never handle its host, user or
98
+ password. Use the same connection name in every tenant and environment
99
+ (`env/test.yaml`, `env/prod.yaml`), each pointing at that environment's
100
+ database, so one package serves them all. `cavelon db connections` shows
101
+ which exist and whether their last test passed. A pull writes each query's
102
+ SQL into `package/tools.yaml`, so the repository holds it: review it like
103
+ code.
104
+
74
105
  ## The loop
75
106
 
76
107
  1. **Pull** what is live: `cavelon pull`. It refuses when `package/` has
@@ -84,7 +115,9 @@ use it when you parse the result.
84
115
  import. `pull --include-tenant-wide` writes them; `apply
85
116
  --include-tenant-wide` (the MCP tool's `include_tenant_wide`) imports them,
86
117
  for every solution of the tenant, so only with the person's say-so; its
87
- preview names the active solutions the change reaches.
118
+ preview names the sections the confirm would import (`would_import`) and
119
+ the active solutions the change reaches, and warns of a change only where
120
+ one of those sections differs from the instance's.
88
121
  `validate` warns about a tenant-wide file in the folder
89
122
  (`tenant_wide_section`); on an instance that does not publish
90
123
  `include_tenant_wide`, `apply` imports such a file anyway and says so:
@@ -117,6 +150,13 @@ use it when you parse the result.
117
150
  code, package file and path, and hint; `cavelon explain <code>` says more.
118
151
  A preview that changes nothing says "Nothing to import" and stores no
119
152
  preview: there is nothing to confirm.
153
+ **A database query your package creates or changes stops the whole
154
+ apply** (`database_query_needs_superadmin`): only a superadmin writes a
155
+ query, in the Admin, and `apply` says so before it sends. Tell the person;
156
+ to apply the other changes now, leave the query as the instance holds it
157
+ (restore the tool's entry as the last pull wrote it, or remove its
158
+ `database_query` block) and preview again. Once a superadmin has imported
159
+ the package in the Admin, `apply` passes while the queries match.
120
160
  5. **Confirm** exactly that preview: `cavelon apply --confirm <preview-id>`
121
161
  (the line `apply` printed, with the same `--env` and `--tenant`). Exit 4
122
162
  means the preview is stale and nothing was imported: the target changed on
@@ -162,7 +202,8 @@ use it when you parse the result.
162
202
  were put on the VM (customer VM).
163
203
  7. **Test**: `cavelon test run --suite <suite>`, then `cavelon wait <operation>`.
164
204
  See the cavelon-testing skill. A solution with a Masterloop node is run with
165
- `cavelon loop start <trigger>` and followed with `cavelon loop watch <run>`;
205
+ `cavelon loop start <trigger>` (it previews; run the confirm command it
206
+ prints) and followed with `cavelon loop watch <run>`;
166
207
  see the cavelon-long-running skill.
167
208
  8. **Fix** what the results and traces show, and go back to step 2. Commit
168
209
  when a step works.
@@ -219,13 +260,23 @@ only show what would happen. Show it to the person before `cavelon trigger
219
260
  identity <trigger> <key> --confirm` (it gives a trigger standing authority),
220
261
  before `cavelon harness default … --confirm`, `cavelon activate
221
262
  --make-default --confirm` or `cavelon deactivate --confirm` (they move live
222
- traffic), and
263
+ traffic), before `cavelon activate --confirm` of a solution its preview says a
264
+ channel or trigger reaches (it goes live for them at once), before
265
+ `cavelon tenant create … --confirm` (a new tenant on the platform) and
266
+ `cavelon variables set … --confirm` that replaces a value (every solution of
267
+ the tenant reads it, active ones too), and
223
268
  before `cavelon sandbox seed … --confirm` or `cavelon loop cancel … --confirm` on
224
- anything but a test Sandbox or a run you started yourself.
269
+ anything but a test Sandbox or a run you started yourself. `cavelon loop start
270
+ <trigger>` previews too, because a run acts as the person and spends budget:
271
+ confirm it on your own only for a trigger of a draft solution in a test
272
+ environment, and show the person every other one. A new variable, and a draft
273
+ solution no channel or trigger reaches, need no confirm.
225
274
 
226
- **Over MCP, confirm with the preview's token.** `api`, `limits_set`,
275
+ **Over MCP, confirm with the preview's token.** `api`, `tenant_create`,
276
+ `variables_set` where it replaces a value, `loop_start`, `limits_set`,
227
277
  `models_set_limit`, `loop_cancel`, `sandbox_seed`, `trigger_identity`,
228
- `harness_default`, `deactivate`, `activate` with `make_default`, and
278
+ `harness_default`, `deactivate`, `activate` of a solution a channel or
279
+ trigger reaches or with `make_default`, and
229
280
  `kb_upload` where it would deactivate documents return a `confirm_token` with
230
281
  their preview. Show
231
282
  the preview, then call the tool again with the same arguments and `confirm`
@@ -234,22 +285,27 @@ refused (`confirm_token_required`), and a token of another change returns the
234
285
  new preview with `token_mismatch` (exit code 4): show that one instead. Tool
235
286
  arguments are spelled in snake_case (`make_default`, `keep_both`, `dry_run`);
236
287
  an argument a tool does not list is refused (`unknown_argument`) with the
237
- closest one named.
288
+ closest one named. Over MCP the next steps a tool returns (hints, `next`,
289
+ `resume`, `confirm`) are tool calls, `harness_default {"solution":"support"}`:
290
+ make the call as written, filling a value in angle brackets (a
291
+ `confirm_token` comes from that tool's preview).
238
292
 
239
293
  **From your shell, `--confirm` takes the same token.** Run by a coding agent,
294
+ `tenant create`, `variables set` (replacing a value), `loop start`,
240
295
  `limits set`, `models set-limit`, `loop cancel`, `sandbox seed`,
241
- `trigger identity`, `harness default`, `activate --make-default`,
242
- `deactivate`, `kb upload --replace`, `variables delete` and `secrets delete`
296
+ `trigger identity`, `harness default`, `activate` (of a solution a channel or
297
+ trigger reaches, or with `--make-default`),
298
+ `deactivate`, `kb upload --replace` and `variables delete`
243
299
  print their preview with a confirm token and the command that confirms it
244
300
  (`… --confirm <token>`). Show the preview, then run exactly that command. A
245
301
  bare `--confirm`, as the docs show it for a person's terminal, changes nothing
246
302
  from your shell and exits 5; a token of another change exits 4 with the new
247
- preview.
303
+ preview. A line printed before its preview exists ends in
304
+ `--confirm <confirm_token of its preview>`: run the command without
305
+ `--confirm` first for the preview and its token.
248
306
 
249
307
  **`cavelon api` from your shell has the guards of the MCP `api` tool**, since
250
- `cavelon` sees that a coding agent runs it (`CLAUDECODE`, `CODEX_THREAD_ID`, `CODEX_SANDBOX`,
251
- `CURSOR_AGENT`, `GEMINI_CLI`, `COPILOT_CLI`, `COPILOT_AGENT`, `AI_AGENT` or
252
- `CAVELON_AGENT=1`):
308
+ `cavelon` sees that a coding agent runs it:
253
309
 
254
310
  - For an operation that is not read-only, it prints the request and a confirm
255
311
  token and sends nothing. Show the request to the person when the rules above
@@ -262,10 +318,14 @@ preview.
262
318
  both marks up front.
263
319
  Tell the person what the error's hint says; for a secret field, send the rest
264
320
  without it and let the person enter the value (`cavelon secrets set <name>`
265
- or the Admin). Never work around a refusal, by unsetting the variable or by
266
- any other way.
321
+ or the Admin). Never work around a refusal in any way.
267
322
  - Its `@file` body, `--file` and `--output` stay inside the solution folder.
268
323
 
324
+ `cavelon secrets set` and `cavelon secrets delete` are refused from your shell
325
+ (`operation_for_a_person`, exit 5), with or without `--confirm`, before a value
326
+ is read or anything is sent: give the person the command from the error's hint
327
+ to run in their own terminal.
328
+
269
329
  `activate` goes through the readiness gate only, and only with a token that
270
330
  may activate. It never forces: activating without the evidence stays a person's
271
331
  decision in the Admin, so do not ask for it as a shortcut.
@@ -322,7 +382,9 @@ preview · 5 needs a person · 6 timed out (still running) · 7 not authorised
322
382
  - `cavelon docs search <query>` and `cavelon docs get <page>`: the instance's
323
383
  own docs, for its version. They are in English; a German question works for
324
384
  the core concepts, English words for the rest. `cavelon docs get index`
325
- lists every page.
385
+ lists every page. A page marked "Platform page" (`mark` in `--json`)
386
+ describes an operator's actions this token cannot take: tell the person who
387
+ can, never ask for a Platform-mode token.
326
388
  - `.cavelon/inventory.md`: the tenant's solutions, knowledge bases, tools,
327
389
  skills, models, test suites and sandboxes from the last pull; `cavelon sandbox list` for their
328
390
  current state and mode.
@@ -133,6 +133,30 @@ no verdict; `cavelon explain <status>` says what it means and what to do next:
133
133
  The case's own reason (which knowledge base, which value) is in the output of
134
134
  `test run --wait`, `wait` and `cavelon trace <run>`.
135
135
 
136
+ ## Database query tools
137
+
138
+ Test a solution with database query tools against a test database, never the
139
+ customer's live one: the test tenant (`--env test`) has a connection of the
140
+ same name pointing at a test database with known rows, set up by a person in
141
+ the Admin. Then:
142
+
143
+ - A step can assert the agent calls the query (`tool_called` with the tool's
144
+ slug), and the judge checks the answer against the known rows.
145
+ - A test run has no signed-in visitor. A query with an identity parameter
146
+ (`end_user.*`), or one without `allows_anonymous`, answers
147
+ `identity_required` there and runs nothing: such a step checks that the
148
+ agent asks the visitor to sign in, not the rows. The instance does not yet
149
+ let a suite name a test Chat User whose identity its queries use (a suite's
150
+ `as_chat_user` reader reads knowledge as that person, nothing more).
151
+ - To check what an identity-scoped query returns for one customer, the tenant
152
+ Owner runs it once with `cavelon db test-run <query> --value <name>=<value>`,
153
+ identity parameters included; it shows what the model would see. Never put
154
+ a real customer's data into a test file.
155
+ - `cavelon db runs <query>` lists each run's outcome and error code (never a
156
+ value or a row), and `cavelon trace` shows the code a failed call answered
157
+ the model with on the tool's span (`error_code`); `cavelon explain <code>`
158
+ says how to fix it.
159
+
136
160
  ## Reading what happened
137
161
 
138
162
  ```bash
@@ -228,4 +252,7 @@ the body does the same, but needs the id and a confirm. `cavelon deactivate
228
252
  previews first and is the person's decision, so show the preview and confirm
229
253
  only with their yes. When every suite passes,
230
254
  `cavelon activate --harness <name or slug>` goes through the readiness gate; if the
231
- token may not activate, a person activates in the Admin.
255
+ token may not activate, a person activates in the Admin. A solution that a
256
+ channel or an active trigger reaches goes live for them at once, so its
257
+ activation previews first and needs the confirm it prints: show it to the
258
+ person and confirm only with their yes.
@@ -2,7 +2,7 @@ import { closest, displayName, MAX_LISTED, pick } from "./choose.js";
2
2
  import { CavelonError, ExitCode } from "./errors.js";
3
3
  import { readTenantless } from "./principal.js";
4
4
  import { canAsk } from "./prompt.js";
5
- import { cavelonCommand } from "./shell.js";
5
+ import { cavelonCommand, fill } from "./printed.js";
6
6
  export function listsTenants(tenantless) {
7
7
  return Boolean(tenantless && !tenantless.refused && tenantless.tenants);
8
8
  }
@@ -27,7 +27,7 @@ export async function chooseTenant(ctx, client, reach, options = {}) {
27
27
  return { kind: "open" };
28
28
  const later = reachesAll && options.later;
29
29
  const intro = later
30
- ? `This token works in every tenant on ${client.url}, one at a time: \`cavelon use\` switches, ` +
30
+ ? `This token works in every tenant on ${client.url}, one at a time: \`${cavelonCommand("use")}\` switches, ` +
31
31
  `and --tenant or \`tenant:\` in cavelon.yaml choose one per command or per solution folder.${tenants.length ? "\nYours:" : ""}`
32
32
  : reachesAll
33
33
  ? `This token reaches every tenant on ${client.url}.${tenants.length ? " Yours:" : ""}`
@@ -52,18 +52,18 @@ export function commandLines(tenants, command) {
52
52
  const width = Math.max(...shown.map((s) => s.text.length));
53
53
  const lines = shown.map((s) => ` ${s.name ? `${s.text.padEnd(width)} ${s.name}` : s.text}`);
54
54
  if (tenants.length > shown.length)
55
- lines.push(` … and ${tenants.length - shown.length} more (\`cavelon tenant list\` lists them)`);
55
+ lines.push(` … and ${tenants.length - shown.length} more (\`${cavelonCommand("tenant", "list")}\` lists them)`);
56
56
  return lines.join("\n");
57
57
  }
58
58
  export function choicesOf(tenants, command) {
59
59
  return tenants.map((t) => ({ id: t.id, slug: t.slug, name: t.name, role: t.role, is_default: t.is_default, command: command(tenantRef(t)) }));
60
60
  }
61
- export function tenantOpenError(url, reach, lead, command = { line: (ref) => cavelonCommand("use", ref), template: "cavelon use <name or slug>" }) {
61
+ export function tenantOpenError(url, reach, lead, command = { line: (ref) => cavelonCommand("use", ref), template: cavelonCommand("use", fill("name or slug")) }) {
62
62
  const { tenants, reachesAll } = reach;
63
63
  const what = reachesAll ? "every tenant" : `${tenants.length} tenants`;
64
64
  const lines = tenants.length ? `Run the line for the tenant you want:\n${commandLines(tenants, command.line)}` : "";
65
65
  const search = reachesAll
66
- ? `${lines ? "\nAny other tenant: " : "Choose one: "}\`${command.template}\`; \`cavelon tenant list --search <part of the name>\` finds its slug.`
66
+ ? `${lines ? "\nAny other tenant: " : "Choose one: "}\`${command.template}\`; \`${cavelonCommand("tenant", "list", "--search", fill("part of the name"))}\` finds its slug.`
67
67
  : "";
68
68
  return new CavelonError(ExitCode.usage, {
69
69
  code: "tenant_required",
@@ -89,8 +89,8 @@ export function tenantMissError(ref, pool, reachesAll, from) {
89
89
  if (listed.length)
90
90
  hints.push(`Run the line for the tenant you mean:\n${commandLines(listed, (r) => cavelonCommand("use", r))}`);
91
91
  hints.push(reachesAll
92
- ? "This token reaches every tenant: `cavelon tenant list --search <part of the name>` finds one."
93
- : "`cavelon tenant list` shows the tenants this token reaches with name, slug and id; `cavelon use` alone lets you choose.");
92
+ ? `This token reaches every tenant: \`${cavelonCommand("tenant", "list", "--search", fill("part of the name"))}\` finds one.`
93
+ : `\`${cavelonCommand("tenant", "list")}\` shows the tenants this token reaches with name, slug and id; \`${cavelonCommand("use")}\` alone lets you choose.`);
94
94
  return new CavelonError(ExitCode.failure, {
95
95
  code: "tenant_not_found",
96
96
  message: `No tenant "${ref}" that this token reaches${from ? ` (from ${from})` : ""}.${names ? ` ${label}: ${names}.` : ""}`,
@@ -1,3 +1,4 @@
1
+ import { QUERY_TOOL_TYPE } from "./database-queries.js";
1
2
  import { clip } from "./format.js";
2
3
  const REASONING_CHARS = 300;
3
4
  const VALUE_CHARS = 120;
@@ -121,6 +122,15 @@ export function retrievalRows(view) {
121
122
  document_id: h.document_id,
122
123
  }));
123
124
  }
125
+ export function queryErrorCode(s) {
126
+ if (s.tool_type !== QUERY_TOOL_TYPE)
127
+ return null;
128
+ const output = spanAttributes(s.output_json);
129
+ const preview = typeof output?.output_preview === "string" ? output.output_preview : typeof s.output_json === "string" ? s.output_json : undefined;
130
+ const answer = (preview !== undefined ? spanAttributes(preview) : undefined) ?? output;
131
+ const code = answer?.error;
132
+ return typeof code === "string" && /^[a-z][a-z0-9_]{0,63}$/.test(code) ? code : null;
133
+ }
124
134
  export function suggestedSpan(spans) {
125
135
  const inOrder = spans.slice().sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0));
126
136
  const last = (pick) => inOrder.filter(pick).pop();
@@ -148,7 +158,7 @@ export const KNOWLEDGE_OUTCOMES = [
148
158
  value: "unusable_hits",
149
159
  recorded_as: "no_usable_evidence",
150
160
  message: "The agent recorded no_usable_evidence although the search returned hits: none of them answers the question. A question the knowledge base does not cover usually shows here, not as content_gap, because a search almost always returns its closest passages.",
151
- hint: "Read the hits on the retrieval span (`cavelon trace … --span <span_id>`). Add the missing content, or improve the document whose passage should have answered it.",
161
+ hint: "Read the hits on the retrieval span (`cavelon trace <run> --trace <trace_id> --span <span_id>`). Add the missing content, or improve the document whose passage should have answered it.",
152
162
  },
153
163
  {
154
164
  value: "retrieval_fault",
@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
3
  import { SKILL_ROOTS } from "./agents.js";
4
4
  import { currentInstall, NPM_PACKAGE, REPOSITORY } from "./install.js";
5
- import { readJsonFile, readTextFile, writeFileAtomic } from "./fsutil.js";
5
+ import { readJsonFile, readTextFile, writePrivateFile } from "./fsutil.js";
6
6
  import { isGenerated } from "./markers.js";
7
7
  import { cacheDir } from "./paths.js";
8
8
  import { findProject } from "./project.js";
@@ -71,7 +71,7 @@ async function withState(io, source, version, fetchImpl, use) {
71
71
  }
72
72
  const result = use(state, now);
73
73
  if (JSON.stringify(state) !== before)
74
- await writeFileAtomic(file, `${JSON.stringify(state, null, 2)}\n`, 0o600, 0o700);
74
+ await writePrivateFile(cacheDir(io.env), file, `${JSON.stringify(state, null, 2)}\n`);
75
75
  return result;
76
76
  }
77
77
  function within(stamp, now) {
@@ -1,6 +1,30 @@
1
+ import { createHash } from "node:crypto";
1
2
  import path from "node:path";
2
3
  import { configDir } from "./paths.js";
3
4
  import { readJsonFile, writeFileAtomic } from "./fsutil.js";
5
+ export const TENANT_REF_TTL_MS = 24 * 60 * 60 * 1000;
6
+ export function credentialFingerprint(token) {
7
+ return createHash("sha256").update(token).digest("hex").slice(0, 16);
8
+ }
9
+ export function cachedTenantRef(settings, ref, token, now) {
10
+ const entry = settings.tenant_refs?.[ref];
11
+ if (!entry || !token || entry.credential !== credentialFingerprint(token))
12
+ return undefined;
13
+ const age = now.getTime() - new Date(entry.resolved_at).getTime();
14
+ return age >= 0 && age < TENANT_REF_TTL_MS ? entry.id : undefined;
15
+ }
16
+ export function withTenantRefs(settings, refs, id, token, now) {
17
+ const credential = credentialFingerprint(token);
18
+ const kept = Object.entries(settings.tenant_refs ?? {}).filter(([, e]) => now.getTime() - new Date(e.resolved_at).getTime() < TENANT_REF_TTL_MS);
19
+ const added = refs.filter((r) => Boolean(r) && !isUuidLike(r)).map((r) => [r, { id, credential, resolved_at: now.toISOString() }]);
20
+ const { tenant_ids: _old, ...rest } = settings;
21
+ return { ...rest, tenant_refs: Object.fromEntries([...kept, ...added]) };
22
+ }
23
+ export function withoutTenantRef(settings, ref) {
24
+ const { [ref]: _gone, ...others } = settings.tenant_refs ?? {};
25
+ return { ...settings, tenant_refs: others };
26
+ }
27
+ const isUuidLike = (value) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
4
28
  export function userConfigPath(env) {
5
29
  return path.join(configDir(env), "config.json");
6
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cavelon/cli",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "The cavelon binary: CLI and local MCP server for building Cavelon solutions with a coding agent.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/goodguys-gmbh/cavelon-dev-kit",