@cavelon/cli 0.0.0 → 0.1.1
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/README.md +697 -3
- package/dist/LICENSE +201 -0
- package/dist/agents.js +91 -0
- package/dist/branches.js +61 -0
- package/dist/capacity.js +162 -0
- package/dist/cli.js +4 -0
- package/dist/command.js +62 -0
- package/dist/commands/api.js +243 -0
- package/dist/commands/async.js +207 -0
- package/dist/commands/docs.js +158 -0
- package/dist/commands/index.js +112 -0
- package/dist/commands/init.js +452 -0
- package/dist/commands/inventory.js +56 -0
- package/dist/commands/limits-set.js +624 -0
- package/dist/commands/limits.js +156 -0
- package/dist/commands/loops.js +792 -0
- package/dist/commands/models.js +197 -0
- package/dist/commands/sandboxes.js +667 -0
- package/dist/commands/session.js +476 -0
- package/dist/commands/solution.js +760 -0
- package/dist/commands/tenants.js +266 -0
- package/dist/commands/values.js +383 -0
- package/dist/commands/work.js +744 -0
- package/dist/context.js +56 -0
- package/dist/contracts.js +189 -0
- package/dist/credentials.js +134 -0
- package/dist/errors.js +64 -0
- package/dist/features.js +37 -0
- package/dist/format.js +36 -0
- package/dist/fsutil.js +40 -0
- package/dist/git.js +41 -0
- package/dist/http.js +286 -0
- package/dist/invoke.js +161 -0
- package/dist/io.js +49 -0
- package/dist/limits.js +299 -0
- package/dist/local-state.js +60 -0
- package/dist/main.js +227 -0
- package/dist/markers.js +113 -0
- package/dist/mcp.js +130 -0
- package/dist/openapi.js +223 -0
- package/dist/operations.js +107 -0
- package/dist/package-check.js +342 -0
- package/dist/package-files.js +329 -0
- package/dist/pair-order.js +39 -0
- package/dist/paths.js +28 -0
- package/dist/principal.js +33 -0
- package/dist/project.js +138 -0
- package/dist/prompt.js +34 -0
- package/dist/results.js +66 -0
- package/dist/session.js +166 -0
- package/dist/shell.js +41 -0
- package/dist/skills/cavelon-authoring/SKILL.md +214 -0
- package/dist/skills/cavelon-long-running/SKILL.md +181 -0
- package/dist/skills/cavelon-loop/SKILL.md +158 -0
- package/dist/skills/cavelon-testing/SKILL.md +90 -0
- package/dist/sse.js +44 -0
- package/dist/tar.js +172 -0
- package/dist/user-config.js +36 -0
- package/dist/version.js +17 -0
- package/dist/zip.js +132 -0
- package/package.json +40 -4
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import { originText } from "../capacity.js";
|
|
2
|
+
import { boolOption, positional } from "../command.js";
|
|
3
|
+
import { CavelonError, ExitCode, usageError } from "../errors.js";
|
|
4
|
+
import { callOperation, openapiOrWarn, workflowOperation } from "../invoke.js";
|
|
5
|
+
import { changedBy, docsPage, formatValue, isOperatorChange, LIMIT_ABOVE_CEILING, limitRef, quotaSetting, readLimits, } from "../limits.js";
|
|
6
|
+
import { deref, jsonBodySchema, operationForPath, validateBody } from "../openapi.js";
|
|
7
|
+
import { readGlobalRole, readPrincipal } from "../principal.js";
|
|
8
|
+
import { cavelonCommand, shellWord } from "../shell.js";
|
|
9
|
+
import { ENV_OPTION, targetFlags } from "./values.js";
|
|
10
|
+
const UNIT_WORDS = {
|
|
11
|
+
megabytes: ["mb", "megabytes"],
|
|
12
|
+
bytes: ["b", "bytes"],
|
|
13
|
+
seconds: ["s", "sec", "seconds"],
|
|
14
|
+
characters: ["chars", "characters"],
|
|
15
|
+
requests_per_minute: ["rpm", "/min", "requests/min"],
|
|
16
|
+
};
|
|
17
|
+
const TRUE_WORDS = new Set(["true", "on", "yes", "enabled"]);
|
|
18
|
+
const FALSE_WORDS = new Set(["false", "off", "no", "disabled"]);
|
|
19
|
+
const CLEAR_WORDS = new Set(["none", "null"]);
|
|
20
|
+
const RUN_CAPS_PAGE = "Platform › Operations › Rate limits › Run caps";
|
|
21
|
+
const TENANT_LIMITS_PAGE = "the tenant's Limits section (Concurrent Agent Runs)";
|
|
22
|
+
const FEATURE_FLAGS_PAGE = "Configure › Feature Flags";
|
|
23
|
+
function notPublished(message, hint, details = {}) {
|
|
24
|
+
return new CavelonError(ExitCode.failure, { code: "operation_unavailable", message, hint, details: { ...details, sent: false } });
|
|
25
|
+
}
|
|
26
|
+
const SWITCHED_BY = { kb_upload_archive_formats: "kb_upload_archive_enabled" };
|
|
27
|
+
function switchFor(published, key) {
|
|
28
|
+
const name = SWITCHED_BY[key];
|
|
29
|
+
const limit = name ? published.byKey.get(name) : undefined;
|
|
30
|
+
return limit?.change && !isOperatorChange(limit.change) ? limit : undefined;
|
|
31
|
+
}
|
|
32
|
+
function operatorRefusal(limit, switched) {
|
|
33
|
+
const where = limit.source === "licence" ? `the licence entitlement ${limit.setting}` : (originText(limit) ?? `${limit.setting} (source: ${limit.source})`);
|
|
34
|
+
const who = limit.source === "licence"
|
|
35
|
+
? "The instance operator installs a renewed licence."
|
|
36
|
+
: limit.origin
|
|
37
|
+
? "Ask the instance operator; the API does not change it."
|
|
38
|
+
: `It cannot be changed at runtime: the instance operator changes ${limit.setting} with a deploy.`;
|
|
39
|
+
return new CavelonError(ExitCode.unauthorized, {
|
|
40
|
+
code: "limit_changed_by_operator",
|
|
41
|
+
message: `${limit.key} is not the tenant's to change: ${changedBy(limit)} sets it, with ${where}. Nothing was sent.`,
|
|
42
|
+
hint: (switched ? `A tenant admin turns it on with ${cavelonCommand("limits", "set", switched.key, "true")} (${switched.setting}). ` : `${who} `) +
|
|
43
|
+
`Docs: ${cavelonCommand("docs", "get", docsPage(limit.docs))}`,
|
|
44
|
+
docs: limit.docs || undefined,
|
|
45
|
+
details: { limit: limitRef(limit), ...(switched ? { switch: switched.key } : {}), sent: false },
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function scopeOf(change) {
|
|
49
|
+
if (!isOperatorChange(change))
|
|
50
|
+
return "tenant";
|
|
51
|
+
return change.path.includes("{tenant_id}") ? "one_tenant" : "platform";
|
|
52
|
+
}
|
|
53
|
+
function flagOf(limit, scope) {
|
|
54
|
+
if (typeof limit.value !== "boolean" || !limit.switches?.length)
|
|
55
|
+
return undefined;
|
|
56
|
+
const source = scope === "platform" ? "platform" : "tenant";
|
|
57
|
+
const matching = limit.switches.filter((s) => s.source === source);
|
|
58
|
+
return matching.length === 1 ? matching[0] : undefined;
|
|
59
|
+
}
|
|
60
|
+
function limitTarget(key, limit, change) {
|
|
61
|
+
const scope = scopeOf(change);
|
|
62
|
+
const flag = flagOf(limit, scope);
|
|
63
|
+
return { key, kind: "limit", change, limit, scope, ...(flag ? { flag } : {}) };
|
|
64
|
+
}
|
|
65
|
+
function changeableKeys(published) {
|
|
66
|
+
const tenant = [];
|
|
67
|
+
const operator = [];
|
|
68
|
+
for (const limit of published.values) {
|
|
69
|
+
const changes = [limit.change, limit.tenant_change].filter((c) => Boolean(c));
|
|
70
|
+
if (!changes.length)
|
|
71
|
+
continue;
|
|
72
|
+
(changes.some(isOperatorChange) ? operator : tenant).push(limit.key);
|
|
73
|
+
}
|
|
74
|
+
for (const quota of published.tenantQuotas?.changes ?? [])
|
|
75
|
+
(isOperatorChange(quota) ? operator : tenant).push(quota.key);
|
|
76
|
+
return { tenant, operator };
|
|
77
|
+
}
|
|
78
|
+
function findTarget(published, key, explicitTenant) {
|
|
79
|
+
const limit = published.byKey.get(key);
|
|
80
|
+
if (limit) {
|
|
81
|
+
if (explicitTenant && limit.tenant_change)
|
|
82
|
+
return limitTarget(key, limit, limit.tenant_change);
|
|
83
|
+
if (limit.change)
|
|
84
|
+
return limitTarget(key, limit, limit.change);
|
|
85
|
+
if (limit.changeable_by !== "tenant_admin")
|
|
86
|
+
throw operatorRefusal(limit, switchFor(published, key));
|
|
87
|
+
throw notPublished(`This instance does not publish how to change ${key}, so nothing was sent.`, `It runs an older Cavelon version. A tenant admin changes ${limit.setting} in the Admin's settings. ` +
|
|
88
|
+
`Docs: ${cavelonCommand("docs", "get", docsPage(limit.docs))}`, { limit: limitRef(limit) });
|
|
89
|
+
}
|
|
90
|
+
const quotaChange = published.tenantQuotas?.changes.find((c) => c.key === key);
|
|
91
|
+
const quota = published.tenantQuotas?.values.find((v) => v.key === key);
|
|
92
|
+
if (quotaChange || quota?.change) {
|
|
93
|
+
const change = quotaChange ? (({ key: _key, ...rest }) => rest)(quotaChange) : quota.change;
|
|
94
|
+
return { key, kind: "quota", change, ...(quota ? { quota } : {}), scope: scopeOf(change) };
|
|
95
|
+
}
|
|
96
|
+
const { tenant, operator } = changeableKeys(published);
|
|
97
|
+
const lines = [
|
|
98
|
+
...(tenant.length ? [`A tenant admin or owner changes these: ${tenant.join(", ")}.`] : []),
|
|
99
|
+
...(operator.length ? [`An operator changes these in Platform mode: ${operator.join(", ")}.`] : []),
|
|
100
|
+
];
|
|
101
|
+
throw new CavelonError(ExitCode.failure, {
|
|
102
|
+
code: "limit_not_found",
|
|
103
|
+
message: `This instance lists no limit ${key}.`,
|
|
104
|
+
hint: lines.length
|
|
105
|
+
? `${lines.join(" ")} \`cavelon limits\` lists every limit.`
|
|
106
|
+
: "`cavelon limits` lists every limit; this instance names none that changes through the API.",
|
|
107
|
+
details: { key, changeable: [...tenant, ...operator], sent: false },
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function changeOperation(ctx, change, key) {
|
|
111
|
+
const doc = await openapiOrWarn(ctx);
|
|
112
|
+
const found = doc ? operationForPath(doc, change.method, change.path) : undefined;
|
|
113
|
+
if (found)
|
|
114
|
+
return { doc, ...found };
|
|
115
|
+
const { doc: none, op } = await workflowOperation(ctx, change.method, change.path, `changing ${key}`);
|
|
116
|
+
return { doc: none, op, fixed: {} };
|
|
117
|
+
}
|
|
118
|
+
function fieldSchema(doc, op, field) {
|
|
119
|
+
let schema = deref(doc, jsonBodySchema(op));
|
|
120
|
+
for (const part of field.split(".")) {
|
|
121
|
+
const properties = nonNull(doc, schema)?.properties;
|
|
122
|
+
if (!properties || !(part in properties))
|
|
123
|
+
return undefined;
|
|
124
|
+
schema = deref(doc, properties[part]);
|
|
125
|
+
}
|
|
126
|
+
return nonNull(doc, schema);
|
|
127
|
+
}
|
|
128
|
+
function nonNull(doc, schema) {
|
|
129
|
+
const branches = schema?.anyOf ?? schema?.oneOf;
|
|
130
|
+
if (!Array.isArray(branches))
|
|
131
|
+
return schema;
|
|
132
|
+
const kept = branches.map((b) => deref(doc, b)).filter((b) => b && b.type !== "null");
|
|
133
|
+
return kept.length === 1 ? kept[0] : schema;
|
|
134
|
+
}
|
|
135
|
+
function kindOf(schema, target) {
|
|
136
|
+
const type = schema?.type;
|
|
137
|
+
if (type === "boolean")
|
|
138
|
+
return "boolean";
|
|
139
|
+
if (type === "array")
|
|
140
|
+
return "list";
|
|
141
|
+
if (type === "integer")
|
|
142
|
+
return "integer";
|
|
143
|
+
if (type === "number")
|
|
144
|
+
return "number";
|
|
145
|
+
if (typeof target.limit?.value === "boolean")
|
|
146
|
+
return "boolean";
|
|
147
|
+
return Array.isArray(target.limit?.value) ? "list" : "integer";
|
|
148
|
+
}
|
|
149
|
+
function splitNumber(raw, fraction) {
|
|
150
|
+
const isDigit = (c) => c !== undefined && c >= "0" && c <= "9";
|
|
151
|
+
let i = 0;
|
|
152
|
+
while (isDigit(raw[i]))
|
|
153
|
+
i++;
|
|
154
|
+
if (fraction && i > 0 && raw[i] === "." && isDigit(raw[i + 1])) {
|
|
155
|
+
i++;
|
|
156
|
+
while (isDigit(raw[i]))
|
|
157
|
+
i++;
|
|
158
|
+
}
|
|
159
|
+
return { digits: raw.slice(0, i), rest: raw.slice(i).trim().toLowerCase() };
|
|
160
|
+
}
|
|
161
|
+
function bareExtension(word) {
|
|
162
|
+
let start = 0;
|
|
163
|
+
while (start < word.length && word[start] === ".")
|
|
164
|
+
start++;
|
|
165
|
+
return word.slice(start);
|
|
166
|
+
}
|
|
167
|
+
function clears(target) {
|
|
168
|
+
if (target.flag)
|
|
169
|
+
return false;
|
|
170
|
+
return target.kind === "limit" || Boolean(target.quota);
|
|
171
|
+
}
|
|
172
|
+
function parseValue(raw, kind, target) {
|
|
173
|
+
const word = raw.trim();
|
|
174
|
+
const lower = word.toLowerCase();
|
|
175
|
+
if (CLEAR_WORDS.has(lower)) {
|
|
176
|
+
if (!clears(target)) {
|
|
177
|
+
throw usageError(target.flag
|
|
178
|
+
? `${target.key} is switched on or off (${target.flag.setting}): give on or off; got "${raw}".`
|
|
179
|
+
: `${target.key} takes a whole number; it has no value of the tenant's own to clear.`);
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (kind === "boolean") {
|
|
184
|
+
if (TRUE_WORDS.has(lower))
|
|
185
|
+
return true;
|
|
186
|
+
if (FALSE_WORDS.has(lower))
|
|
187
|
+
return false;
|
|
188
|
+
const clear = clears(target) ? ", or none to clear it" : "";
|
|
189
|
+
throw usageError(`${target.key} is switched on or off (${target.change.field}): give true or false${clear}; got "${raw}".`);
|
|
190
|
+
}
|
|
191
|
+
if (kind === "list") {
|
|
192
|
+
const items = [...new Set(word.split(",").map((w) => bareExtension(w.trim())).filter(Boolean))];
|
|
193
|
+
if (!items.length)
|
|
194
|
+
throw usageError(`${target.key} takes a comma-separated list (e.g. pdf,docx), or none to clear it; got "${raw}".`);
|
|
195
|
+
return items;
|
|
196
|
+
}
|
|
197
|
+
const { digits, rest } = splitNumber(word, kind === "number");
|
|
198
|
+
const unit = target.limit?.unit ?? target.quota?.unit ?? "count";
|
|
199
|
+
const units = unit === "count" || unit === "processing_steps" ? "" : ` of ${unit}`;
|
|
200
|
+
if (!digits)
|
|
201
|
+
throw usageError(`${target.key} takes a ${kind === "number" ? "number" : "whole number"}${units}${clears(target) ? ", or none to clear it" : ""}; got "${raw}".`);
|
|
202
|
+
if (rest && !(UNIT_WORDS[unit] ?? []).includes(rest)) {
|
|
203
|
+
throw usageError(`${target.key} is in ${unit}; "${raw}" is not. Give the number of ${unit}${UNIT_WORDS[unit] ? ` (e.g. ${digits} or ${digits}${UNIT_WORDS[unit][0]})` : ""}.`);
|
|
204
|
+
}
|
|
205
|
+
return Number(digits);
|
|
206
|
+
}
|
|
207
|
+
function aboveCeiling(change, value) {
|
|
208
|
+
return typeof value === "number" && Boolean(change.maximum_setting) && change.maximum !== undefined && value > change.maximum;
|
|
209
|
+
}
|
|
210
|
+
async function ceilingCode(ctx) {
|
|
211
|
+
const catalog = await (await ctx.contracts()).errorCatalog().catch(() => null);
|
|
212
|
+
return catalog?.api_error_codes?.some((e) => e.code === LIMIT_ABOVE_CEILING) ? LIMIT_ABOVE_CEILING : "request_invalid";
|
|
213
|
+
}
|
|
214
|
+
function checkBounds(target, value, aboveCode = "request_invalid") {
|
|
215
|
+
if (typeof value !== "number")
|
|
216
|
+
return;
|
|
217
|
+
const { minimum, maximum, maximum_setting: ceiling } = target.change;
|
|
218
|
+
const unit = target.limit ? (v) => formatValue({ value: v, unit: target.limit.unit }) : String;
|
|
219
|
+
const refuse = (message, hint, code = "request_invalid") => new CavelonError(ExitCode.validation, {
|
|
220
|
+
code,
|
|
221
|
+
message: `${message} Nothing was sent.`,
|
|
222
|
+
hint,
|
|
223
|
+
docs: target.limit?.docs || target.quota?.docs || undefined,
|
|
224
|
+
details: {
|
|
225
|
+
key: target.key,
|
|
226
|
+
...(target.limit?.setting ? { setting: target.limit.setting } : {}),
|
|
227
|
+
value,
|
|
228
|
+
minimum: minimum ?? null,
|
|
229
|
+
maximum: maximum ?? null,
|
|
230
|
+
...(ceiling ? { maximum_setting: ceiling } : {}),
|
|
231
|
+
sent: false,
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
if (minimum !== undefined && value < minimum)
|
|
235
|
+
throw refuse(`${target.key} is at least ${unit(minimum)}; ${unit(value)} is below it.`, `Give a value from ${unit(minimum)}${maximum === undefined ? "" : ` to ${unit(maximum)}`}.`);
|
|
236
|
+
if (maximum !== undefined && value > maximum) {
|
|
237
|
+
throw ceiling
|
|
238
|
+
? refuse(`${target.key} may only be lowered: the platform ceiling is ${unit(maximum)} (${ceiling}), and ${unit(value)} is above it.`, `Only the instance operator raises the ceiling, with ${ceiling}; ask them. A tenant admin sets a value up to ${unit(maximum)}.`, aboveCode)
|
|
239
|
+
: refuse(`${target.key} is at most ${unit(maximum)}; ${unit(value)} is above it.`, `Give a value from ${unit(minimum ?? 0)} to ${unit(maximum)}.`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function bodyFor(field, value) {
|
|
243
|
+
const parts = field.split(".");
|
|
244
|
+
let body = { [parts.pop()]: value };
|
|
245
|
+
while (parts.length)
|
|
246
|
+
body = { [parts.pop()]: body };
|
|
247
|
+
return body;
|
|
248
|
+
}
|
|
249
|
+
function credentialName(principal) {
|
|
250
|
+
if (principal.token)
|
|
251
|
+
return `the personal access token "${principal.token.name}"`;
|
|
252
|
+
if (principal.api_key)
|
|
253
|
+
return `the API key "${principal.api_key.name}"`;
|
|
254
|
+
return "this session";
|
|
255
|
+
}
|
|
256
|
+
function anyOf(names, none = "a permission the instance does not name") {
|
|
257
|
+
if (names.length <= 1)
|
|
258
|
+
return names[0] ?? none;
|
|
259
|
+
return `${names.slice(0, -1).join(", ")} or ${names.at(-1)}`;
|
|
260
|
+
}
|
|
261
|
+
function adminPage(target) {
|
|
262
|
+
if (target.flag)
|
|
263
|
+
return FEATURE_FLAGS_PAGE;
|
|
264
|
+
return target.scope === "one_tenant" ? TENANT_LIMITS_PAGE : RUN_CAPS_PAGE;
|
|
265
|
+
}
|
|
266
|
+
function permissionRefusal(principal, target) {
|
|
267
|
+
const permissions = target.change.permissions;
|
|
268
|
+
if (!principal || !permissions.length)
|
|
269
|
+
return undefined;
|
|
270
|
+
if (principal.permissions) {
|
|
271
|
+
if (permissions.some((p) => principal.permissions.includes(p)))
|
|
272
|
+
return undefined;
|
|
273
|
+
return new CavelonError(ExitCode.unauthorized, {
|
|
274
|
+
code: "forbidden",
|
|
275
|
+
message: `${capitalized(credentialName(principal))} may not change ${target.key}: it needs ${anyOf(permissions)}, which this credential does not hold. Nothing was sent.`,
|
|
276
|
+
hint: `${capitalized(changedByTarget(target))} with ${anyOf(permissions)} runs the command, or changes it in the Admin.` +
|
|
277
|
+
(principal.token ? " A personal access token holds at most its ceiling role's permissions." : ""),
|
|
278
|
+
details: { key: target.key, permissions, held: principal.permissions, sent: false },
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
const key = principal.kind === "api_key" ? principal.api_key : null;
|
|
282
|
+
if (!key)
|
|
283
|
+
return undefined;
|
|
284
|
+
const scopes = key.scopes ?? [];
|
|
285
|
+
if (scopes.includes("admin") || permissions.some((p) => scopes.includes(p)))
|
|
286
|
+
return undefined;
|
|
287
|
+
return new CavelonError(ExitCode.unauthorized, {
|
|
288
|
+
code: "forbidden",
|
|
289
|
+
message: `The API key "${key.name}" may not change ${target.key}: its scopes (${scopes.join(", ") || "none"}) hold none of ${permissions.join(", ")}. Nothing was sent.`,
|
|
290
|
+
hint: "The operation takes a person's session or personal access token with one of these permissions, or an admin API key of the tenant. " +
|
|
291
|
+
"A person runs the command, or a tenant administrator issues an admin key in Settings → API keys.",
|
|
292
|
+
details: { key: target.key, permissions, scopes, sent: false },
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function changedByTarget(target) {
|
|
296
|
+
if (target.quota)
|
|
297
|
+
return changedBy(target.quota);
|
|
298
|
+
if (target.limit)
|
|
299
|
+
return changedBy(target.limit);
|
|
300
|
+
return "a person with the permission";
|
|
301
|
+
}
|
|
302
|
+
async function operatorCheck(client, target) {
|
|
303
|
+
const roles = target.change.requires_role ?? [];
|
|
304
|
+
const principal = await readPrincipal(client, { sendTenant: false });
|
|
305
|
+
let why;
|
|
306
|
+
if (!principal)
|
|
307
|
+
why = "The instance does not say who this credential is (/api/v1/meta/principal)";
|
|
308
|
+
else if (principal.kind === "api_key")
|
|
309
|
+
why = `${capitalized(credentialName(principal))} is a tenant's key, and a key never works in Platform mode`;
|
|
310
|
+
else if (principal.kind !== "personal_access_token" || !principal.token)
|
|
311
|
+
why = "This credential is not a personal access token";
|
|
312
|
+
else if (!principal.token.platform_mode_allowed || principal.mode !== "platform") {
|
|
313
|
+
why = `${capitalized(credentialName(principal))} does not work in Platform mode (it was created without "Platform mode")`;
|
|
314
|
+
}
|
|
315
|
+
else if (roles.length && !roles.includes(principal.token.ceiling_role)) {
|
|
316
|
+
why = `${capitalized(credentialName(principal))} has the ceiling role ${principal.token.ceiling_role}`;
|
|
317
|
+
}
|
|
318
|
+
else if (roles.length) {
|
|
319
|
+
const global = await readGlobalRole(client);
|
|
320
|
+
if (global === null)
|
|
321
|
+
why = "This person holds no platform role";
|
|
322
|
+
else if (global !== undefined && !roles.includes(global))
|
|
323
|
+
why = `This person's global role is ${global}`;
|
|
324
|
+
}
|
|
325
|
+
if (!why)
|
|
326
|
+
return { principal };
|
|
327
|
+
const role = anyOf(roles, "a platform role");
|
|
328
|
+
return {
|
|
329
|
+
principal,
|
|
330
|
+
refusal: new CavelonError(ExitCode.unauthorized, {
|
|
331
|
+
code: "platform_role_required",
|
|
332
|
+
message: `${target.key} is an operator's change: it needs a personal access token in Platform mode of a ${role}. ${why}. Nothing was sent.`,
|
|
333
|
+
hint: `An operator with that role changes it in the Admin (${adminPage(target)}), or runs this command with a personal access token ` +
|
|
334
|
+
`created with "Platform mode" on /account/access-tokens. Docs: ${cavelonCommand("docs", "get", docsPage(target.limit?.docs ?? ""))}`,
|
|
335
|
+
docs: target.limit?.docs || undefined,
|
|
336
|
+
details: { key: target.key, requires_role: roles, mode: target.change.mode ?? null, sent: false },
|
|
337
|
+
}),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
function capitalized(text) {
|
|
341
|
+
return text ? `${text[0].toUpperCase()}${text.slice(1)}` : text;
|
|
342
|
+
}
|
|
343
|
+
async function pathParams(ctx, change, principal) {
|
|
344
|
+
const params = {};
|
|
345
|
+
for (const match of change.path.matchAll(/\{([^}]+)\}/g)) {
|
|
346
|
+
const name = match[1];
|
|
347
|
+
if (name !== "tenant_id") {
|
|
348
|
+
throw notPublished(`${change.method} ${change.path} needs ${name}, which the kit cannot fill, so nothing was sent.`, "The instance names an operation this kit does not know how to call; `cavelon api describe` shows it.");
|
|
349
|
+
}
|
|
350
|
+
const tenantId = (await ctx.client()).target.tenantId ?? principal?.tenant_id ?? undefined;
|
|
351
|
+
if (!tenantId)
|
|
352
|
+
throw usageError("Which tenant? Choose one with `cavelon use <tenant>` or --tenant.");
|
|
353
|
+
params[name] = [tenantId];
|
|
354
|
+
}
|
|
355
|
+
return params;
|
|
356
|
+
}
|
|
357
|
+
function valueText(target, value, kind) {
|
|
358
|
+
if (value === undefined)
|
|
359
|
+
return "not published";
|
|
360
|
+
if (value === null) {
|
|
361
|
+
if (target.quota)
|
|
362
|
+
return "none (no cap)";
|
|
363
|
+
if (target.scope === "one_tenant")
|
|
364
|
+
return "none (the platform's cap applies)";
|
|
365
|
+
if (target.scope === "platform")
|
|
366
|
+
return "none (the baseline applies)";
|
|
367
|
+
return "none (the platform's value applies)";
|
|
368
|
+
}
|
|
369
|
+
if (typeof value === "boolean")
|
|
370
|
+
return target.flag || target.limit?.unit === "boolean" ? (value ? "on" : "off") : `${target.change.field} ${value}`;
|
|
371
|
+
if (Array.isArray(value))
|
|
372
|
+
return value.length ? value.join(", ") : "none";
|
|
373
|
+
if (target.limit && kind !== "list")
|
|
374
|
+
return formatValue({ value, unit: target.limit.unit });
|
|
375
|
+
return String(value);
|
|
376
|
+
}
|
|
377
|
+
function argText(value) {
|
|
378
|
+
if (value === null)
|
|
379
|
+
return "none";
|
|
380
|
+
return Array.isArray(value) ? value.join(",") : String(value);
|
|
381
|
+
}
|
|
382
|
+
function sameValue(a, b) {
|
|
383
|
+
const sorted = (list) => list.map(String).sort((x, y) => x.localeCompare(y)).join("\n");
|
|
384
|
+
if (Array.isArray(a) && Array.isArray(b))
|
|
385
|
+
return a.length === b.length && sorted(a) === sorted(b);
|
|
386
|
+
return a === b;
|
|
387
|
+
}
|
|
388
|
+
async function previousValue(ctx, published, target) {
|
|
389
|
+
if (target.flag)
|
|
390
|
+
return target.flag.enabled;
|
|
391
|
+
if (target.kind === "quota") {
|
|
392
|
+
const row = await quotaSetting(ctx, published, target.key).catch(() => undefined);
|
|
393
|
+
if (row && (typeof row.value === "number" || row.value === null))
|
|
394
|
+
return row.value;
|
|
395
|
+
return target.quota ? target.quota.value : undefined;
|
|
396
|
+
}
|
|
397
|
+
const limit = target.limit;
|
|
398
|
+
if (target.scope === "one_tenant")
|
|
399
|
+
return limit.source === "tenant" ? limit.value : null;
|
|
400
|
+
if (target.scope === "platform")
|
|
401
|
+
return limit.source === "tenant" ? undefined : limit.value;
|
|
402
|
+
return limit.value;
|
|
403
|
+
}
|
|
404
|
+
function noChange(target, previous, value, from) {
|
|
405
|
+
if (target.flag)
|
|
406
|
+
return previous === value ? `${target.key} is already ${from} (${target.flag.setting})` : undefined;
|
|
407
|
+
if (target.kind === "quota")
|
|
408
|
+
return previous !== undefined && sameValue(previous, value) ? `${target.key} is already ${from}` : undefined;
|
|
409
|
+
const limit = target.limit;
|
|
410
|
+
if (target.scope === "one_tenant") {
|
|
411
|
+
if (value === null && previous === null)
|
|
412
|
+
return `${target.key} has no cap of this tenant's own to clear (source: ${limit.source})`;
|
|
413
|
+
return previous !== null && sameValue(previous, value) ? `${target.key} is already ${from} (source: tenant)` : undefined;
|
|
414
|
+
}
|
|
415
|
+
if (target.scope !== "tenant")
|
|
416
|
+
return undefined;
|
|
417
|
+
if (typeof value === "boolean")
|
|
418
|
+
return previous === value ? `${target.key} is already ${from} (source: ${limit.source})` : undefined;
|
|
419
|
+
const own = limit.source === "tenant";
|
|
420
|
+
if (value === null && !own)
|
|
421
|
+
return `${target.key} has no value of this tenant's own to clear (source: ${limit.source})`;
|
|
422
|
+
if (own && sameValue(previous, value))
|
|
423
|
+
return `${target.key} is already ${from} (source: tenant)`;
|
|
424
|
+
return undefined;
|
|
425
|
+
}
|
|
426
|
+
function dig(answer, field) {
|
|
427
|
+
let node = answer;
|
|
428
|
+
for (const part of field.split(".")) {
|
|
429
|
+
if (!node || typeof node !== "object" || !(part in node))
|
|
430
|
+
return { found: false };
|
|
431
|
+
node = node[part];
|
|
432
|
+
}
|
|
433
|
+
return { found: true, value: node };
|
|
434
|
+
}
|
|
435
|
+
function answeredValue(target, data, sent) {
|
|
436
|
+
const answer = (data && typeof data === "object" ? data : {});
|
|
437
|
+
const changed = Array.isArray(answer.changed) ? answer.changed : [];
|
|
438
|
+
const now = Array.isArray(answer.limits) ? answer.limits.find((l) => l?.key === target.key) : undefined;
|
|
439
|
+
if (now && "value" in now)
|
|
440
|
+
return { value: now.value, source: typeof now.source === "string" ? now.source : undefined, changed };
|
|
441
|
+
const quota = answer.quota;
|
|
442
|
+
if (quota && typeof quota === "object" && quota.key === target.key)
|
|
443
|
+
return { value: quota.value ?? null, changed };
|
|
444
|
+
const field = dig(answer, target.change.field);
|
|
445
|
+
if (field.found) {
|
|
446
|
+
const origin = answer[`${target.change.field}_origin`];
|
|
447
|
+
return { value: field.value, ...(typeof origin === "string" ? { origin } : {}), changed };
|
|
448
|
+
}
|
|
449
|
+
const own = changed.find((c) => c?.setting === target.limit?.setting);
|
|
450
|
+
return { value: own && "new" in own ? own.new : sent, changed };
|
|
451
|
+
}
|
|
452
|
+
function explicitTenant(session) {
|
|
453
|
+
return session.tenantSource === "option" && Boolean(session.tenant);
|
|
454
|
+
}
|
|
455
|
+
export const limitsSet = {
|
|
456
|
+
name: "limits set",
|
|
457
|
+
summary: "Change a limit through the operation the instance names: a tenant's, or an operator's in Platform mode (needs --confirm).",
|
|
458
|
+
description: "Reads the limit's published change (operation, body field, bounds, permissions, roles) and checks the value against it\n" +
|
|
459
|
+
"before sending. Without --confirm, shows the old and new value, the operation and who may run it, and changes nothing.\n" +
|
|
460
|
+
"Also changes the tenant quotas in tenant_quotas.changes (the inference budget, the monthly Processing Step cap).\n" +
|
|
461
|
+
"An operator's change (a run cap) is sent only with a personal access token in Platform mode of a role it names, without\n" +
|
|
462
|
+
"X-Tenant-Id; --tenant <id|slug> then sets one tenant's own run cap. An environment or licence limit, a value out of bounds,\n" +
|
|
463
|
+
"an instance that does not publish how to change the limit, and a credential without the permission or the role are refused\n" +
|
|
464
|
+
"before anything is sent. Propose the change to the person; never raise a limit on your own.",
|
|
465
|
+
readOnly: false,
|
|
466
|
+
destructive: true,
|
|
467
|
+
idempotent: true,
|
|
468
|
+
mcpTool: "limits_set",
|
|
469
|
+
positionals: [
|
|
470
|
+
{ name: "key", description: "The limit's key, as `cavelon limits` lists it (e.g. kb_upload_max_file_size_mb).", required: true },
|
|
471
|
+
{
|
|
472
|
+
name: "value",
|
|
473
|
+
description: "The new value in the limit's unit (50 or 50MB), a comma-separated list of file types, true/false, or none to clear the tenant's own value.",
|
|
474
|
+
required: true,
|
|
475
|
+
},
|
|
476
|
+
],
|
|
477
|
+
options: {
|
|
478
|
+
confirm: { type: "boolean", description: "Change it; without this nothing is changed." },
|
|
479
|
+
env: ENV_OPTION,
|
|
480
|
+
},
|
|
481
|
+
examples: [
|
|
482
|
+
"cavelon limits set kb_upload_max_file_size_mb 50",
|
|
483
|
+
"cavelon limits set kb_upload_max_file_size_mb 50 --confirm",
|
|
484
|
+
"cavelon limits set rate_limit_chat_rpm none --confirm",
|
|
485
|
+
"cavelon limits set monthly_inference_token_budget 2000000",
|
|
486
|
+
"cavelon limits set monthly_processing_step_cap none --confirm",
|
|
487
|
+
"cavelon limits set max_concurrent_agent_runs_global 150 --confirm",
|
|
488
|
+
"cavelon limits set max_concurrent_agent_runs_per_tenant 6 --tenant acme --confirm",
|
|
489
|
+
],
|
|
490
|
+
async run(ctx, input) {
|
|
491
|
+
const key = positional(input, "key").trim();
|
|
492
|
+
const raw = positional(input, "value");
|
|
493
|
+
const contracts = await ctx.contracts();
|
|
494
|
+
const published = await readLimits(ctx);
|
|
495
|
+
if (contracts.needsTenant) {
|
|
496
|
+
throw new CavelonError(ExitCode.usage, {
|
|
497
|
+
code: "tenant_required",
|
|
498
|
+
message: "Limits belong to a tenant, and none is chosen.",
|
|
499
|
+
hint: "Choose one with `cavelon use <tenant>` or pass --tenant; an operator's change reads the limits of any tenant and sends in Platform mode.",
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
if (!published.published) {
|
|
503
|
+
throw notPublished(`This instance does not publish its limits, so it does not say how to change ${key}; nothing was sent.`, "It is older than the limits in /api/v1/meta/capabilities. A tenant admin changes tenant settings in the Admin.");
|
|
504
|
+
}
|
|
505
|
+
const session = await ctx.session();
|
|
506
|
+
const target = findTarget(published, key, explicitTenant(session));
|
|
507
|
+
const { change } = target;
|
|
508
|
+
const operator = isOperatorChange(change);
|
|
509
|
+
const { doc, op, fixed } = await changeOperation(ctx, change, key);
|
|
510
|
+
const schema = doc ? fieldSchema(doc, op, change.field) : undefined;
|
|
511
|
+
if (doc && !schema) {
|
|
512
|
+
throw notPublished(`This instance names ${change.method} ${change.path} for ${key}, but its OpenAPI body has no ${change.field}, so nothing was sent.`, "The instance's capabilities and OpenAPI disagree; `cavelon status` shows its version.");
|
|
513
|
+
}
|
|
514
|
+
const kind = kindOf(schema, target);
|
|
515
|
+
const value = parseValue(raw, kind, target);
|
|
516
|
+
checkBounds(target, value, aboveCeiling(change, value) ? await ceilingCode(ctx) : undefined);
|
|
517
|
+
const body = bodyFor(change.field, value);
|
|
518
|
+
if (doc)
|
|
519
|
+
validateBody(doc, op, body);
|
|
520
|
+
const client = await ctx.client();
|
|
521
|
+
let principal;
|
|
522
|
+
if (operator) {
|
|
523
|
+
const checked = await operatorCheck(client, target);
|
|
524
|
+
if (checked.refusal)
|
|
525
|
+
throw checked.refusal;
|
|
526
|
+
principal = checked.principal;
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
principal = await readPrincipal(client);
|
|
530
|
+
}
|
|
531
|
+
const refused = permissionRefusal(principal, target);
|
|
532
|
+
if (refused)
|
|
533
|
+
throw refused;
|
|
534
|
+
const params = { ...fixed, ...(await pathParams(ctx, change, operator ? undefined : principal)) };
|
|
535
|
+
const flags = targetFlags(session);
|
|
536
|
+
const previous = await previousValue(ctx, published, target);
|
|
537
|
+
const operation = {
|
|
538
|
+
operation: change.operation,
|
|
539
|
+
method: change.method,
|
|
540
|
+
path: change.path,
|
|
541
|
+
field: change.field,
|
|
542
|
+
body,
|
|
543
|
+
...(Object.keys(params).length ? { params: Object.fromEntries(Object.entries(params).map(([k, v]) => [k, v[0]])) } : {}),
|
|
544
|
+
...(operator ? { mode: "platform" } : {}),
|
|
545
|
+
};
|
|
546
|
+
const base = {
|
|
547
|
+
key,
|
|
548
|
+
kind: target.kind,
|
|
549
|
+
scope: target.scope,
|
|
550
|
+
previous: previous ?? null,
|
|
551
|
+
value,
|
|
552
|
+
operation,
|
|
553
|
+
permissions: change.permissions,
|
|
554
|
+
...(change.requires_role ? { requires_role: change.requires_role } : {}),
|
|
555
|
+
...(target.limit ? { limit: limitRef(target.limit) } : {}),
|
|
556
|
+
...(target.flag ? { switch: target.flag.setting } : {}),
|
|
557
|
+
};
|
|
558
|
+
const from = valueText(target, previous, kind);
|
|
559
|
+
const to = valueText(target, value, kind);
|
|
560
|
+
const nothing = noChange(target, previous, value, from);
|
|
561
|
+
if (nothing)
|
|
562
|
+
return { data: { ...base, changed: false, sent: false }, text: `${nothing}; nothing to change.` };
|
|
563
|
+
const sends = `${change.method} ${change.path} ${JSON.stringify(body)}${operator ? " (Platform mode, no X-Tenant-Id)" : ""}`;
|
|
564
|
+
const allowed = operator
|
|
565
|
+
? `a personal access token in Platform mode of a ${anyOf(change.requires_role ?? [], "platform role")}`
|
|
566
|
+
: `a credential with ${anyOf(change.permissions)} (a session, a personal access token, or an admin API key of the tenant)`;
|
|
567
|
+
if (!boolOption(input, "confirm")) {
|
|
568
|
+
const confirm = `cavelon limits set ${shellWord(key)} ${shellWord(argText(value))}${flags} --confirm`;
|
|
569
|
+
const source = target.limit && target.kind === "limit" ? ` (source now: ${target.limit.source}${target.limit.origin ? `, origin: ${target.limit.origin}` : ""})` : "";
|
|
570
|
+
const what = target.scope === "platform" && target.limit?.tenant_change
|
|
571
|
+
? [`This changes the platform's cap for every tenant without its own; --tenant <id|slug> sets one tenant's own cap.`]
|
|
572
|
+
: target.scope === "one_tenant" && params.tenant_id
|
|
573
|
+
? [`For tenant ${params.tenant_id[0]} only.`]
|
|
574
|
+
: [];
|
|
575
|
+
return {
|
|
576
|
+
data: { ...base, changed: false, sent: false, confirm },
|
|
577
|
+
text: [`${key}: ${from} → ${to}${source}.`, ...what, `Sends: ${sends}`, `Allowed: ${allowed}.`, `Nothing was changed. Change it with: ${confirm}`].join("\n"),
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
let result;
|
|
581
|
+
try {
|
|
582
|
+
result = await callOperation(ctx, client, doc, op, { params, body, ...(operator ? { sendTenant: false } : {}) });
|
|
583
|
+
}
|
|
584
|
+
catch (error) {
|
|
585
|
+
if (error instanceof CavelonError && error.status === 403) {
|
|
586
|
+
const needs = operator ? `a Platform-mode token of a ${anyOf(change.requires_role ?? [], "platform role")}` : anyOf(change.permissions);
|
|
587
|
+
throw new CavelonError(ExitCode.unauthorized, {
|
|
588
|
+
code: error.code,
|
|
589
|
+
status: 403,
|
|
590
|
+
message: `${error.message} (${key} was not changed.)`,
|
|
591
|
+
hint: `Changing ${key} needs ${needs}. A person with it runs the command, or uses the Admin.` + (error.hint ? ` ${error.hint}` : ""),
|
|
592
|
+
docs: error.docs,
|
|
593
|
+
details: { key, permissions: change.permissions, ...(change.requires_role ? { requires_role: change.requires_role } : {}), changed: false },
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
if (error instanceof CavelonError && error.code === LIMIT_ABOVE_CEILING) {
|
|
597
|
+
throw new CavelonError(ExitCode.validation, {
|
|
598
|
+
code: error.code,
|
|
599
|
+
status: error.status,
|
|
600
|
+
message: `${error.message} (${key} was not changed.)`,
|
|
601
|
+
hint: error.hint,
|
|
602
|
+
docs: error.docs,
|
|
603
|
+
details: { key, ...(error.details && typeof error.details === "object" ? error.details : {}), changed: false },
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
throw error;
|
|
607
|
+
}
|
|
608
|
+
const answered = answeredValue(target, result.data, value);
|
|
609
|
+
const now = valueText(target, answered.value, Array.isArray(answered.value) ? "list" : kind);
|
|
610
|
+
const where = answered.source ? ` (source: ${answered.source})` : answered.origin ? ` (origin: ${answered.origin})` : "";
|
|
611
|
+
return {
|
|
612
|
+
data: {
|
|
613
|
+
...base,
|
|
614
|
+
now: answered.value ?? null,
|
|
615
|
+
...(answered.source ? { source: answered.source } : {}),
|
|
616
|
+
...(answered.origin ? { origin: answered.origin } : {}),
|
|
617
|
+
changes: answered.changed,
|
|
618
|
+
changed: true,
|
|
619
|
+
sent: true,
|
|
620
|
+
},
|
|
621
|
+
text: `Changed ${key}: ${from} → ${now}${where}.`,
|
|
622
|
+
};
|
|
623
|
+
},
|
|
624
|
+
};
|