@cavelon/cli 0.0.0 → 0.1.2
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 +713 -3
- package/dist/LICENSE +201 -0
- package/dist/agents.js +107 -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 +313 -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 +481 -0
- package/dist/commands/inventory.js +56 -0
- package/dist/commands/limits-set.js +633 -0
- package/dist/commands/limits.js +156 -0
- package/dist/commands/loops.js +798 -0
- package/dist/commands/models.js +197 -0
- package/dist/commands/sandboxes.js +674 -0
- package/dist/commands/session.js +480 -0
- package/dist/commands/solution.js +830 -0
- package/dist/commands/tenants.js +266 -0
- package/dist/commands/values.js +386 -0
- package/dist/commands/work.js +761 -0
- package/dist/context.js +60 -0
- package/dist/contracts.js +234 -0
- package/dist/credentials.js +134 -0
- package/dist/errors.js +64 -0
- package/dist/features.js +52 -0
- package/dist/format.js +36 -0
- package/dist/fsutil.js +43 -0
- package/dist/git.js +41 -0
- package/dist/http.js +304 -0
- package/dist/invoke.js +165 -0
- package/dist/io.js +49 -0
- package/dist/limits.js +307 -0
- package/dist/local-state.js +78 -0
- package/dist/main.js +227 -0
- package/dist/markers.js +113 -0
- package/dist/mcp.js +137 -0
- package/dist/openapi.js +233 -0
- package/dist/operations.js +107 -0
- package/dist/package-check.js +342 -0
- package/dist/package-files.js +394 -0
- package/dist/pair-order.js +39 -0
- package/dist/paths.js +77 -0
- package/dist/principal.js +33 -0
- package/dist/project.js +157 -0
- package/dist/prompt.js +34 -0
- package/dist/results.js +104 -0
- package/dist/session.js +190 -0
- package/dist/shell.js +41 -0
- package/dist/skills/cavelon-authoring/SKILL.md +224 -0
- package/dist/skills/cavelon-long-running/SKILL.md +185 -0
- package/dist/skills/cavelon-loop/SKILL.md +159 -0
- package/dist/skills/cavelon-testing/SKILL.md +97 -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,674 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { promises as fs } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { boolOption, CURSOR_OPTION, intOption, LIMIT_OPTION, listOption, positional, stringOption, } from "../command.js";
|
|
5
|
+
import { CavelonError, ExitCode, usageError } from "../errors.js";
|
|
6
|
+
import { confinedPath } from "../paths.js";
|
|
7
|
+
import { idempotencyKey, instanceModes, requireFeature, stableKey, UUID_KEY_OPTION, withRetryKey } from "../features.js";
|
|
8
|
+
import { clip, keyValues, moreHint, table } from "../format.js";
|
|
9
|
+
import { bodyBytes } from "../http.js";
|
|
10
|
+
import { buildRequest, callStable, workflowOperation } from "../invoke.js";
|
|
11
|
+
import { isUuid } from "../session.js";
|
|
12
|
+
import { cavelonCommand } from "../shell.js";
|
|
13
|
+
import { archiveFrom } from "../tar.js";
|
|
14
|
+
import { TIMEOUT_OPTION, timeoutMs, waitAndReport } from "./async.js";
|
|
15
|
+
import { resolveHarnessId } from "./tenants.js";
|
|
16
|
+
const MODES = {
|
|
17
|
+
isolated_container: {
|
|
18
|
+
offers: ["archive", "receipt"],
|
|
19
|
+
commands: ["validate", "files", "cat", "activity", "logs", "receipt", "seed", "artifacts export"],
|
|
20
|
+
},
|
|
21
|
+
customer_vm: {
|
|
22
|
+
offers: ["refresh"],
|
|
23
|
+
commands: ["validate", "files", "cat", "activity", "logs", "refresh"],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const WAIT_OPTION = { type: "boolean", description: "Wait for the job to finish (see `cavelon wait`).", cliOnly: true };
|
|
27
|
+
const HARNESS_OPTION = {
|
|
28
|
+
type: "string",
|
|
29
|
+
value: "<harness>",
|
|
30
|
+
description: "The solution the Sandbox is read for (default: cavelon.yaml's, or the Sandbox's only allowed one).",
|
|
31
|
+
};
|
|
32
|
+
const REVISION_OPTION = {
|
|
33
|
+
type: "string",
|
|
34
|
+
value: "<revision>",
|
|
35
|
+
description: "The workspace revision the job expects (default: the current one).",
|
|
36
|
+
};
|
|
37
|
+
const SANDBOX_PAGE = 100;
|
|
38
|
+
const MAX_SANDBOX_PAGES = 10;
|
|
39
|
+
async function sandboxesOn(ctx) {
|
|
40
|
+
await requireFeature(ctx, "sandbox_feature_enabled", "sandbox_feature_disabled", "Sandboxes");
|
|
41
|
+
}
|
|
42
|
+
export async function resolveSandbox(ctx, ref) {
|
|
43
|
+
await sandboxesOn(ctx);
|
|
44
|
+
if (isUuid(ref))
|
|
45
|
+
return callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}", "Sandboxes", { params: { sandbox_id: [ref] } });
|
|
46
|
+
const all = [];
|
|
47
|
+
let after;
|
|
48
|
+
for (let page = 0; page < MAX_SANDBOX_PAGES; page++) {
|
|
49
|
+
const items = await callStable(ctx, "GET", "/api/v1/sandboxes", "Sandboxes", { query: { limit: SANDBOX_PAGE, after } });
|
|
50
|
+
all.push(...items);
|
|
51
|
+
if (items.length < SANDBOX_PAGE)
|
|
52
|
+
break;
|
|
53
|
+
after = items.at(-1).id;
|
|
54
|
+
}
|
|
55
|
+
let hits = all.filter((s) => s.name === ref);
|
|
56
|
+
if (hits.length === 0)
|
|
57
|
+
hits = all.filter((s) => s.name.toLowerCase() === ref.toLowerCase());
|
|
58
|
+
if (hits.length === 1)
|
|
59
|
+
return hits[0];
|
|
60
|
+
throw new CavelonError(ExitCode.failure, {
|
|
61
|
+
code: hits.length ? "sandbox_ambiguous" : "sandbox_not_found",
|
|
62
|
+
message: hits.length ? `${hits.length} Sandboxes are named "${ref}"; pass its id.` : `No Sandbox "${ref}" in this tenant.`,
|
|
63
|
+
hint: "`cavelon sandbox list` lists them.",
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const REFUSALS = {
|
|
67
|
+
archive: {
|
|
68
|
+
code: "sandbox_capability_unavailable",
|
|
69
|
+
what: "archive import and export",
|
|
70
|
+
elsewhere: (s) => `On a customer VM, put the files into its directory on the VM, then run \`${cavelonCommand("sandbox", "refresh", s.name)}\`; ` +
|
|
71
|
+
`read results with \`${cavelonCommand("sandbox", "files", s.name)}\` and \`${cavelonCommand("sandbox", "cat", s.name)} <path>\`.`,
|
|
72
|
+
},
|
|
73
|
+
receipt: {
|
|
74
|
+
code: "sandbox_validation_receipt_unavailable",
|
|
75
|
+
what: "a runner validation receipt (it reports agent_reported completion only)",
|
|
76
|
+
elsewhere: (s) => `The evidence is the files: \`${cavelonCommand("sandbox", "files", s.name)}\` and \`${cavelonCommand("sandbox", "cat", s.name)} <path>\`.`,
|
|
77
|
+
},
|
|
78
|
+
refresh: {
|
|
79
|
+
code: "sandbox_workspace_refresh_unavailable",
|
|
80
|
+
what: "a workspace refresh (nothing edits it outside Cavelon)",
|
|
81
|
+
elsewhere: (s) => `Seed an isolated container with \`${cavelonCommand("sandbox", "seed", s.name)} <folder>\`.`,
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
function requireOffer(ctx, sandbox, offer, command) {
|
|
85
|
+
const mode = MODES[sandbox.execution_mode];
|
|
86
|
+
if (!mode) {
|
|
87
|
+
ctx.warn(`Sandbox "${sandbox.name}" runs in mode ${sandbox.execution_mode}, which this cavelon does not know; the instance decides.`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (mode.offers.includes(offer))
|
|
91
|
+
return;
|
|
92
|
+
const refusal = REFUSALS[offer];
|
|
93
|
+
const offering = Object.entries(MODES).filter(([, m]) => m.offers.includes(offer)).map(([name]) => name);
|
|
94
|
+
throw new CavelonError(ExitCode.validation, {
|
|
95
|
+
code: refusal.code,
|
|
96
|
+
message: `Sandbox "${sandbox.name}" runs in ${sandbox.execution_mode} mode, which offers no ${refusal.what}; \`cavelon ${command}\` needs ${offering.map((m) => "an " + m).join(" or ")} Sandbox.`,
|
|
97
|
+
hint: refusal.elsewhere(sandbox),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function harnessFor(ctx, input, sandbox) {
|
|
101
|
+
const ref = stringOption(input, "harness") ?? (await ctx.session()).project?.harness;
|
|
102
|
+
const allowed = sandbox.allowed_harness_ids ?? [];
|
|
103
|
+
if (allowed.length === 0) {
|
|
104
|
+
throw new CavelonError(ExitCode.unauthorized, {
|
|
105
|
+
code: "sandbox_harness_not_allowed",
|
|
106
|
+
message: `No solution may use Sandbox "${sandbox.name}" yet.`,
|
|
107
|
+
hint: "A person grants Sandbox Access (the allowed solutions) in the Admin.",
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (!ref) {
|
|
111
|
+
if (allowed.length === 1)
|
|
112
|
+
return allowed[0];
|
|
113
|
+
throw usageError(`Sandbox "${sandbox.name}" allows ${allowed.length} solutions; name one with --harness.`, `Allowed: ${allowed.join(", ")}`);
|
|
114
|
+
}
|
|
115
|
+
const id = await resolveHarnessId(ctx, ref);
|
|
116
|
+
if (!allowed.includes(id)) {
|
|
117
|
+
throw new CavelonError(ExitCode.unauthorized, {
|
|
118
|
+
code: "sandbox_harness_not_allowed",
|
|
119
|
+
message: `Solution ${ref} may not use Sandbox "${sandbox.name}".`,
|
|
120
|
+
hint: "A person grants Sandbox Access (the allowed solutions) in the Admin; --harness names another solution.",
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return id;
|
|
124
|
+
}
|
|
125
|
+
function sandboxView(s) {
|
|
126
|
+
return {
|
|
127
|
+
name: s.name,
|
|
128
|
+
id: s.id,
|
|
129
|
+
mode: s.execution_mode,
|
|
130
|
+
state: s.lifecycle_state,
|
|
131
|
+
ready: s.readiness?.passed ?? null,
|
|
132
|
+
revision: s.observed_revision,
|
|
133
|
+
connection: s.connection?.state ?? null,
|
|
134
|
+
writer_run_id: s.writer_owner_run_id,
|
|
135
|
+
offers: MODES[s.execution_mode]?.commands ?? null,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
export const sandboxList = {
|
|
139
|
+
name: "sandbox list",
|
|
140
|
+
summary: "The tenant's Sandboxes: mode, state, revision, and what each mode offers.",
|
|
141
|
+
readOnly: true,
|
|
142
|
+
idempotent: true,
|
|
143
|
+
mcpTool: "sandbox_list",
|
|
144
|
+
options: { limit: { ...LIMIT_OPTION, description: "Return at most n Sandboxes (at most 100)." }, cursor: CURSOR_OPTION },
|
|
145
|
+
examples: ["cavelon sandbox list", "cavelon sandbox list --json"],
|
|
146
|
+
async run(ctx, input) {
|
|
147
|
+
await sandboxesOn(ctx);
|
|
148
|
+
const limit = intOption(input, "limit", { min: 1, max: 100, fallback: 50 });
|
|
149
|
+
const after = stringOption(input, "cursor");
|
|
150
|
+
if (after !== undefined && !isUuid(after))
|
|
151
|
+
throw usageError(`--cursor "${after}" is not a cursor from a previous page.`);
|
|
152
|
+
const items = (await callStable(ctx, "GET", "/api/v1/sandboxes", "Sandboxes", { query: { limit, after } })).map(sandboxView);
|
|
153
|
+
const next = items.length === limit ? items.at(-1).id : null;
|
|
154
|
+
const modes = await instanceModes(ctx);
|
|
155
|
+
return {
|
|
156
|
+
data: { items, next_cursor: next, instance_modes: modes ?? null },
|
|
157
|
+
text: (table(items, ["name", "mode", "state", "ready", "revision", "connection", "id"]) || "No Sandboxes.") +
|
|
158
|
+
moreHint(next, "cavelon sandbox list") +
|
|
159
|
+
(modes ? `\n\nNew Sandboxes on this instance: ${modes.join(", ") || "none (switched off)"}.` : ""),
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
export const sandboxValidate = {
|
|
164
|
+
name: "sandbox validate",
|
|
165
|
+
summary: "Run the Sandbox's readiness checks on its runner; exit 3 when one fails.",
|
|
166
|
+
description: "Needs sandboxes.manage. A Sandbox that is not ready refuses loops and seeds.",
|
|
167
|
+
readOnly: false,
|
|
168
|
+
idempotent: true,
|
|
169
|
+
mcpTool: "sandbox_validate",
|
|
170
|
+
positionals: [{ name: "sandbox", description: "Sandbox name or id.", required: true }],
|
|
171
|
+
examples: ["cavelon sandbox validate orders-test"],
|
|
172
|
+
async run(ctx, input) {
|
|
173
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
174
|
+
const result = await callStable(ctx, "POST", "/api/v1/sandboxes/{sandbox_id}/validate", "validating Sandboxes", {
|
|
175
|
+
params: { sandbox_id: [sandbox.id] },
|
|
176
|
+
headers: { "If-Match": `"${sandbox.config_version}"` },
|
|
177
|
+
timeoutMs: 90_000,
|
|
178
|
+
});
|
|
179
|
+
const checks = result.readiness?.checks ?? [];
|
|
180
|
+
const passed = result.readiness?.passed === true;
|
|
181
|
+
const failed = checks.filter((c) => !c.passed);
|
|
182
|
+
return {
|
|
183
|
+
data: { ...sandboxView(result), checks },
|
|
184
|
+
text: `Sandbox "${result.name}" (${result.execution_mode}): ${passed ? "ready" : "not ready (" + result.lifecycle_state + ")"}` +
|
|
185
|
+
(failed.length ? `\n${table(failed.map((c) => ({ check: c.name, reason: c.reason ?? "" })), ["check", "reason"])}` : ""),
|
|
186
|
+
exitCode: passed ? ExitCode.ok : ExitCode.validation,
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
export const sandboxFiles = {
|
|
191
|
+
name: "sandbox files",
|
|
192
|
+
summary: "List a folder of the Sandbox's workspace.",
|
|
193
|
+
readOnly: true,
|
|
194
|
+
idempotent: true,
|
|
195
|
+
mcpTool: "sandbox_files",
|
|
196
|
+
positionals: [
|
|
197
|
+
{ name: "sandbox", description: "Sandbox name or id.", required: true },
|
|
198
|
+
{ name: "path", description: "Folder in the workspace (default: its root)." },
|
|
199
|
+
],
|
|
200
|
+
options: { harness: HARNESS_OPTION, limit: { ...LIMIT_OPTION, description: "Return at most n entries (at most 100)." }, cursor: CURSOR_OPTION },
|
|
201
|
+
examples: ["cavelon sandbox files orders-test", "cavelon sandbox files orders-test output --json"],
|
|
202
|
+
async run(ctx, input) {
|
|
203
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
204
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
205
|
+
const folder = positional(input, "path") ?? ".";
|
|
206
|
+
const limit = intOption(input, "limit", { min: 1, max: 100, fallback: 50 });
|
|
207
|
+
const page = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/files", "Sandbox files", {
|
|
208
|
+
params: { sandbox_id: [sandbox.id] },
|
|
209
|
+
query: { harness_id: harness, path: folder, limit, cursor: stringOption(input, "cursor") },
|
|
210
|
+
});
|
|
211
|
+
return {
|
|
212
|
+
data: { sandbox: sandbox.name, path: folder, workspace_revision: page.workspace_revision, entries: page.entries, next_cursor: page.next_cursor },
|
|
213
|
+
text: `${sandbox.name}:${folder} (${page.workspace_revision})\n` +
|
|
214
|
+
(table(page.entries, ["kind", "size", "path"], 120) || "Empty.") +
|
|
215
|
+
moreHint(page.next_cursor, cavelonCommand("sandbox", "files", sandbox.name, folder)),
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
export const sandboxCat = {
|
|
220
|
+
name: "sandbox cat",
|
|
221
|
+
summary: "Print a file of the Sandbox's workspace, a page at a time.",
|
|
222
|
+
description: "At most 32 KiB per call; --offset reads on. --base64 for a binary file.",
|
|
223
|
+
readOnly: true,
|
|
224
|
+
idempotent: true,
|
|
225
|
+
mcpTool: "sandbox_cat",
|
|
226
|
+
positionals: [
|
|
227
|
+
{ name: "sandbox", description: "Sandbox name or id.", required: true },
|
|
228
|
+
{ name: "path", description: "File in the workspace.", required: true },
|
|
229
|
+
],
|
|
230
|
+
options: {
|
|
231
|
+
harness: HARNESS_OPTION,
|
|
232
|
+
offset: { type: "string", value: "<bytes>", description: "Start at this byte." },
|
|
233
|
+
length: { type: "string", value: "<bytes>", description: "Read at most this many bytes (at most 32768)." },
|
|
234
|
+
base64: { type: "boolean", description: "Return the bytes as base64." },
|
|
235
|
+
},
|
|
236
|
+
examples: ["cavelon sandbox cat orders-test output/summary.json", "cavelon sandbox cat orders-test data.bin --base64 --json"],
|
|
237
|
+
async run(ctx, input) {
|
|
238
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
239
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
240
|
+
const file = positional(input, "path");
|
|
241
|
+
const page = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/files/content", "Sandbox file content", {
|
|
242
|
+
params: { sandbox_id: [sandbox.id] },
|
|
243
|
+
query: {
|
|
244
|
+
harness_id: harness,
|
|
245
|
+
path: file,
|
|
246
|
+
offset: intOption(input, "offset", { min: 0 }),
|
|
247
|
+
length: intOption(input, "length", { min: 1, max: 32768 }),
|
|
248
|
+
encoding: boolOption(input, "base64") ? "base64" : "utf-8",
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
if (page.next_offset !== null && !ctx.json && ctx.mode === "cli") {
|
|
252
|
+
ctx.io.stderr.write(`Bytes ${page.offset}–${page.next_offset} of ${page.size}. Read on: ${cavelonCommand("sandbox", "cat", sandbox.name, file, "--offset", String(page.next_offset))}\n`);
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
data: { sandbox: sandbox.name, path: file, ...page },
|
|
256
|
+
text: page.content,
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
export const sandboxActivity = {
|
|
261
|
+
name: "sandbox activity",
|
|
262
|
+
summary: "What ran in the Sandbox for a solution: commands, transfers, validations; or one of them.",
|
|
263
|
+
readOnly: true,
|
|
264
|
+
idempotent: true,
|
|
265
|
+
mcpTool: "sandbox_activity",
|
|
266
|
+
positionals: [
|
|
267
|
+
{ name: "sandbox", description: "Sandbox name or id.", required: true },
|
|
268
|
+
{ name: "activity", description: "One activity's id, for its detail." },
|
|
269
|
+
],
|
|
270
|
+
options: { harness: HARNESS_OPTION, limit: { ...LIMIT_OPTION, description: "Return at most n entries (at most 100)." }, cursor: CURSOR_OPTION },
|
|
271
|
+
examples: ["cavelon sandbox activity orders-test", "cavelon sandbox activity orders-test <activity_id> --json"],
|
|
272
|
+
async run(ctx, input) {
|
|
273
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
274
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
275
|
+
const one = positional(input, "activity");
|
|
276
|
+
const view = (e) => ({
|
|
277
|
+
activity_id: e.id,
|
|
278
|
+
action: e.action,
|
|
279
|
+
kind: e.kind,
|
|
280
|
+
status: e.status,
|
|
281
|
+
revision: e.workspace_revision,
|
|
282
|
+
run_id: e.child_run_id ?? e.owner_run_id,
|
|
283
|
+
artifact: e.artifact?.artifact_id ?? null,
|
|
284
|
+
created_at: e.created_at,
|
|
285
|
+
operation_id: e.operation_id,
|
|
286
|
+
});
|
|
287
|
+
if (one) {
|
|
288
|
+
const detail = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/activity/{operation_link_id}", "Sandbox activity", {
|
|
289
|
+
params: { sandbox_id: [sandbox.id], operation_link_id: [one] },
|
|
290
|
+
query: { harness_id: harness },
|
|
291
|
+
});
|
|
292
|
+
const entry = view(detail.operation);
|
|
293
|
+
const more = [`Logs: ${cavelonCommand("sandbox", "logs", sandbox.name, one)}`];
|
|
294
|
+
if (MODES[sandbox.execution_mode]?.offers.includes("receipt"))
|
|
295
|
+
more.push(`Receipt: ${cavelonCommand("sandbox", "receipt", sandbox.name, one)}`);
|
|
296
|
+
return { data: entry, text: `${keyValues(Object.entries(entry))}\n\n${more.join("\n")}` };
|
|
297
|
+
}
|
|
298
|
+
const after = stringOption(input, "cursor");
|
|
299
|
+
const page = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/activity", "Sandbox activity", {
|
|
300
|
+
params: { sandbox_id: [sandbox.id] },
|
|
301
|
+
query: { harness_id: harness, limit: intOption(input, "limit", { min: 1, max: 100, fallback: 50 }), after },
|
|
302
|
+
});
|
|
303
|
+
const items = page.entries.map(view);
|
|
304
|
+
return {
|
|
305
|
+
data: { sandbox: sandbox.name, items, next_cursor: page.next_cursor },
|
|
306
|
+
text: (table(items, ["activity_id", "action", "status", "created_at"]) || "No activity.") + moreHint(page.next_cursor, cavelonCommand("sandbox", "activity", sandbox.name)),
|
|
307
|
+
};
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
export const sandboxLogs = {
|
|
311
|
+
name: "sandbox logs",
|
|
312
|
+
summary: "One activity's log output, a page at a time.",
|
|
313
|
+
description: "The runner keeps a bounded tail; the next page needs the same --snapshot, which each page prints.",
|
|
314
|
+
readOnly: true,
|
|
315
|
+
idempotent: true,
|
|
316
|
+
mcpTool: "sandbox_logs",
|
|
317
|
+
positionals: [
|
|
318
|
+
{ name: "sandbox", description: "Sandbox name or id.", required: true },
|
|
319
|
+
{ name: "activity", description: "The activity id (from `sandbox activity`).", required: true },
|
|
320
|
+
],
|
|
321
|
+
options: {
|
|
322
|
+
harness: HARNESS_OPTION,
|
|
323
|
+
offset: { type: "string", value: "<bytes>", description: "Start at this byte." },
|
|
324
|
+
length: { type: "string", value: "<bytes>", description: "Read at most this many bytes (at most 32768)." },
|
|
325
|
+
snapshot: { type: "string", value: "<digest>", description: "The snapshot digest of the first page, to read on." },
|
|
326
|
+
},
|
|
327
|
+
examples: ["cavelon sandbox logs orders-test <activity_id>"],
|
|
328
|
+
async run(ctx, input) {
|
|
329
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
330
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
331
|
+
const activity = positional(input, "activity");
|
|
332
|
+
const page = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/activity/{operation_link_id}/logs", "Sandbox logs", {
|
|
333
|
+
params: { sandbox_id: [sandbox.id], operation_link_id: [activity] },
|
|
334
|
+
query: {
|
|
335
|
+
harness_id: harness,
|
|
336
|
+
offset: intOption(input, "offset", { min: 0 }),
|
|
337
|
+
length: intOption(input, "length", { min: 1, max: 32768 }),
|
|
338
|
+
snapshot_digest: stringOption(input, "snapshot"),
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
const text = Buffer.from(page.content_base64, "base64").toString("utf8");
|
|
342
|
+
const next = page.next_offset === null
|
|
343
|
+
? null
|
|
344
|
+
: cavelonCommand("sandbox", "logs", sandbox.name, activity, "--offset", String(page.next_offset), "--snapshot", page.snapshot_digest);
|
|
345
|
+
if (next && !ctx.json && ctx.mode === "cli")
|
|
346
|
+
ctx.io.stderr.write(`Bytes ${page.offset}–${page.next_offset} of ${page.size_bytes}. Read on: ${next}\n`);
|
|
347
|
+
return {
|
|
348
|
+
data: { sandbox: sandbox.name, activity_id: activity, text, offset: page.offset, next_offset: page.next_offset, size_bytes: page.size_bytes, snapshot_digest: page.snapshot_digest, next },
|
|
349
|
+
text,
|
|
350
|
+
};
|
|
351
|
+
},
|
|
352
|
+
};
|
|
353
|
+
export const sandboxReceipt = {
|
|
354
|
+
name: "sandbox receipt",
|
|
355
|
+
summary: "The runner's validation receipt of one activity (isolated container only).",
|
|
356
|
+
description: "A trusted receipt exists only on an isolated container; a customer VM reports agent_reported completion.",
|
|
357
|
+
readOnly: true,
|
|
358
|
+
idempotent: true,
|
|
359
|
+
mcpTool: "sandbox_receipt",
|
|
360
|
+
positionals: [
|
|
361
|
+
{ name: "sandbox", description: "Sandbox name or id.", required: true },
|
|
362
|
+
{ name: "activity", description: "The activity id (from `sandbox activity`).", required: true },
|
|
363
|
+
],
|
|
364
|
+
options: { harness: HARNESS_OPTION },
|
|
365
|
+
examples: ["cavelon sandbox receipt orders-test <activity_id> --json"],
|
|
366
|
+
async run(ctx, input) {
|
|
367
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
368
|
+
requireOffer(ctx, sandbox, "receipt", "sandbox receipt");
|
|
369
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
370
|
+
const activity = positional(input, "activity");
|
|
371
|
+
const result = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/activity/{operation_link_id}/receipt", "Sandbox receipts", {
|
|
372
|
+
params: { sandbox_id: [sandbox.id], operation_link_id: [activity] },
|
|
373
|
+
query: { harness_id: harness },
|
|
374
|
+
});
|
|
375
|
+
const text = JSON.stringify(result.receipt, null, 2);
|
|
376
|
+
return { data: { sandbox: sandbox.name, activity_id: activity, receipt: result.receipt }, text: clip(text, 20_000) };
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
export const sandboxRefresh = {
|
|
380
|
+
name: "sandbox refresh",
|
|
381
|
+
summary: "Accept a customer VM's workspace as it is now, after files were put on the VM (customer VM only).",
|
|
382
|
+
description: "Edits on the VM outside Cavelon are legitimate; refresh makes the current files the new baseline revision.",
|
|
383
|
+
readOnly: false,
|
|
384
|
+
idempotent: true,
|
|
385
|
+
mcpTool: "sandbox_refresh",
|
|
386
|
+
positionals: [{ name: "sandbox", description: "Sandbox name or id.", required: true }],
|
|
387
|
+
options: { "idempotency-key": UUID_KEY_OPTION },
|
|
388
|
+
examples: ["cavelon sandbox refresh spec-vm"],
|
|
389
|
+
async run(ctx, input) {
|
|
390
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
391
|
+
requireOffer(ctx, sandbox, "refresh", "sandbox refresh");
|
|
392
|
+
const key = idempotencyKey(input);
|
|
393
|
+
const result = await callStable(ctx, "POST", "/api/v1/sandboxes/{sandbox_id}/refresh-workspace", "refreshing Sandboxes", {
|
|
394
|
+
params: { sandbox_id: [sandbox.id] },
|
|
395
|
+
headers: { "Idempotency-Key": key, "If-Match": `"${sandbox.config_version}"` },
|
|
396
|
+
timeoutMs: 90_000,
|
|
397
|
+
}).catch((error) => {
|
|
398
|
+
throw withRetryKey(error, key);
|
|
399
|
+
});
|
|
400
|
+
return {
|
|
401
|
+
data: { ...sandboxView(result), previous_revision: sandbox.observed_revision },
|
|
402
|
+
text: `Sandbox "${result.name}": baseline ${sandbox.observed_revision ?? "none"} → ${result.observed_revision ?? "none"} (${result.lifecycle_state}).`,
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
async function currentRevision(ctx, input, sandbox, harness) {
|
|
407
|
+
const given = stringOption(input, "revision");
|
|
408
|
+
if (given !== undefined) {
|
|
409
|
+
if (!/^revision-\d{1,16}$/.test(given))
|
|
410
|
+
throw usageError(`--revision must look like revision-12, got "${given}".`);
|
|
411
|
+
return given;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
const page = await callStable(ctx, "GET", "/api/v1/sandboxes/{sandbox_id}/files", "Sandbox files", {
|
|
415
|
+
params: { sandbox_id: [sandbox.id] },
|
|
416
|
+
query: { harness_id: harness, path: ".", limit: 1 },
|
|
417
|
+
});
|
|
418
|
+
return page.workspace_revision;
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
if (!(error instanceof CavelonError) || error.status === undefined || error.status >= 500 || !sandbox.observed_revision)
|
|
422
|
+
throw error;
|
|
423
|
+
ctx.warn(`Could not read the workspace (${error.code}); using the revision the instance last saw, ${sandbox.observed_revision}.`);
|
|
424
|
+
return sandbox.observed_revision;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
async function maxArchiveBytes(ctx) {
|
|
428
|
+
const fallback = 64 * 1024 * 1024;
|
|
429
|
+
try {
|
|
430
|
+
const doc = await (await ctx.contracts()).openapi();
|
|
431
|
+
const schema = doc.components?.schemas?.SandboxArtifactJobCreate;
|
|
432
|
+
const size = schema?.properties?.size_bytes;
|
|
433
|
+
const max = size?.maximum ?? size?.anyOf?.find((s) => typeof s.maximum === "number")?.maximum;
|
|
434
|
+
return typeof max === "number" ? max : fallback;
|
|
435
|
+
}
|
|
436
|
+
catch {
|
|
437
|
+
return fallback;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
const JOB_ROUTE = "/api/v1/sandboxes/{sandbox_id}/artifact-jobs/{job_id}";
|
|
441
|
+
async function getJob(ctx, sandboxId, jobId) {
|
|
442
|
+
return callStable(ctx, "GET", JOB_ROUTE, "archive jobs", { params: { sandbox_id: [sandboxId], job_id: [jobId] } });
|
|
443
|
+
}
|
|
444
|
+
function jobFailed(job) {
|
|
445
|
+
return new CavelonError(ExitCode.failure, {
|
|
446
|
+
code: job.error_code ?? "sandbox_artifact_job_failed",
|
|
447
|
+
message: `Archive job ${job.id} ${job.status}${job.error_code ? ": " + job.error_code : ""}.`,
|
|
448
|
+
hint: job.error_code ? `\`cavelon explain ${job.error_code}\` says what it means.` : undefined,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
const UPLOAD_READY_MS = 30_000;
|
|
452
|
+
function seedCommand(sandbox, input) {
|
|
453
|
+
const harness = stringOption(input, "harness");
|
|
454
|
+
return cavelonCommand("sandbox", "seed", sandbox.name, positional(input, "source"), ...(harness ? ["--harness", harness] : []), "--confirm");
|
|
455
|
+
}
|
|
456
|
+
export const sandboxSeed = {
|
|
457
|
+
name: "sandbox seed",
|
|
458
|
+
summary: "Replace an isolated container's workspace with a folder or tar archive (needs --confirm).",
|
|
459
|
+
description: "Isolated container only; on a customer VM, put the files on the VM and run `sandbox refresh`. The folder is sent as an\n" +
|
|
460
|
+
"uncompressed tar (files and folders only); a .tar is sent as it is; any other file lands under its own name. The archive\n" +
|
|
461
|
+
"becomes the workspace. Without --confirm, shows what would be sent. Running the same seed again resumes it.",
|
|
462
|
+
readOnly: false,
|
|
463
|
+
destructive: true,
|
|
464
|
+
idempotent: true,
|
|
465
|
+
mcpTool: "sandbox_seed",
|
|
466
|
+
positionals: [
|
|
467
|
+
{ name: "sandbox", description: "Sandbox name or id.", required: true },
|
|
468
|
+
{ name: "source", description: "A folder, an uncompressed .tar, or one file.", required: true },
|
|
469
|
+
],
|
|
470
|
+
options: {
|
|
471
|
+
harness: HARNESS_OPTION,
|
|
472
|
+
revision: REVISION_OPTION,
|
|
473
|
+
confirm: { type: "boolean", description: "Send it; without it nothing changes." },
|
|
474
|
+
wait: WAIT_OPTION,
|
|
475
|
+
timeout: TIMEOUT_OPTION,
|
|
476
|
+
"idempotency-key": UUID_KEY_OPTION,
|
|
477
|
+
},
|
|
478
|
+
examples: ["cavelon sandbox seed orders-test seeds/orders", "cavelon sandbox seed orders-test seeds/orders --confirm --wait"],
|
|
479
|
+
async run(ctx, input) {
|
|
480
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
481
|
+
requireOffer(ctx, sandbox, "archive", "sandbox seed");
|
|
482
|
+
const source = await confinedPath(ctx, positional(input, "source"), "The source");
|
|
483
|
+
const archive = await archiveFrom(source, await maxArchiveBytes(ctx));
|
|
484
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
485
|
+
const revision = await currentRevision(ctx, input, sandbox, harness);
|
|
486
|
+
const summary = {
|
|
487
|
+
sandbox: sandbox.name,
|
|
488
|
+
sandbox_id: sandbox.id,
|
|
489
|
+
mode: sandbox.execution_mode,
|
|
490
|
+
harness_id: harness,
|
|
491
|
+
revision,
|
|
492
|
+
archive: { from: archive.from, sha256: archive.sha256, size_bytes: archive.bytes.length, ...(archive.files >= 0 ? { files: archive.files, folders: archive.directories } : {}) },
|
|
493
|
+
};
|
|
494
|
+
if (sandbox.lifecycle_state !== "ready")
|
|
495
|
+
ctx.warn(`Sandbox "${sandbox.name}" is ${sandbox.lifecycle_state}; the instance accepts a seed only when it is ready (\`cavelon sandbox validate\`).`);
|
|
496
|
+
if (sandbox.writer_owner_run_id)
|
|
497
|
+
ctx.warn(`Run ${sandbox.writer_owner_run_id} holds the Sandbox's writer; the instance refuses a seed until it ends.`);
|
|
498
|
+
const what = archive.files >= 0 ? `${archive.files} files in ${archive.directories} folders` : "the tar archive";
|
|
499
|
+
if (!boolOption(input, "confirm")) {
|
|
500
|
+
const confirm = seedCommand(sandbox, input);
|
|
501
|
+
return {
|
|
502
|
+
data: { ...summary, seeded: false, confirm },
|
|
503
|
+
text: `Would replace the workspace of "${sandbox.name}" (${revision}) with ${what}, ${archive.bytes.length} bytes, sha256 ${archive.sha256}.\n` +
|
|
504
|
+
`Nothing was sent. Seed it with: ${confirm}`,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
const key = stringOption(input, "idempotency-key") !== undefined ? idempotencyKey(input) : stableKey(`seed:${sandbox.id}:${revision}:${archive.sha256}`);
|
|
508
|
+
let job = await callStable(ctx, "POST", "/api/v1/sandboxes/{sandbox_id}/artifact-jobs", "archive jobs", {
|
|
509
|
+
params: { sandbox_id: [sandbox.id] },
|
|
510
|
+
headers: { "Idempotency-Key": key },
|
|
511
|
+
body: { harness_id: harness, direction: "import", expected_workspace_revision: revision, sha256: archive.sha256, size_bytes: archive.bytes.length },
|
|
512
|
+
});
|
|
513
|
+
const until = Date.now() + UPLOAD_READY_MS;
|
|
514
|
+
while (job.status === "active" && job.phase !== "awaiting_upload" && ["acquire", "operation"].includes(job.phase) && Date.now() < until) {
|
|
515
|
+
await ctx.io.sleep(Math.min(1000, Math.max(0, until - Date.now())));
|
|
516
|
+
job = await getJob(ctx, sandbox.id, job.id);
|
|
517
|
+
}
|
|
518
|
+
if (job.status === "failed" || job.status === "cancelled")
|
|
519
|
+
throw jobFailed(job);
|
|
520
|
+
const resume = seedCommand(sandbox, input);
|
|
521
|
+
if (job.status === "active" && job.phase === "awaiting_upload") {
|
|
522
|
+
job = await upload(ctx, sandbox.id, job.id, archive.bytes);
|
|
523
|
+
}
|
|
524
|
+
else if (job.status === "active" && ["acquire", "operation"].includes(job.phase)) {
|
|
525
|
+
return {
|
|
526
|
+
data: { ...summary, seeded: false, job_id: job.id, phase: job.phase, operation_id: job.operation_id, resume },
|
|
527
|
+
text: `Job ${job.id} is still taking the workspace (${job.phase}). Run the same seed again to send the archive: ${resume}`,
|
|
528
|
+
exitCode: ExitCode.timeout,
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
const result = { ...summary, seeded: true, job_id: job.id, phase: job.phase, operation_id: job.operation_id };
|
|
532
|
+
if (boolOption(input, "wait") && ctx.mode === "cli") {
|
|
533
|
+
const waited = await waitAndReport(ctx, [job.operation_id], timeoutMs(ctx, stringOption(input, "timeout")));
|
|
534
|
+
return { data: { ...result, ...waited.data }, text: `Sent ${what} to "${sandbox.name}".\n${waited.text}`, exitCode: waited.exitCode };
|
|
535
|
+
}
|
|
536
|
+
return {
|
|
537
|
+
data: result,
|
|
538
|
+
text: `Sent ${what} to "${sandbox.name}"; the import runs on the instance.\nWait with: cavelon wait ${job.operation_id}`,
|
|
539
|
+
};
|
|
540
|
+
},
|
|
541
|
+
};
|
|
542
|
+
async function upload(ctx, sandboxId, jobId, bytes) {
|
|
543
|
+
const client = await ctx.client();
|
|
544
|
+
const { op } = await workflowOperation(ctx, "PUT", `${JOB_ROUTE}/content`, "archive uploads");
|
|
545
|
+
const { path: target } = buildRequest(op, { params: { sandbox_id: [sandboxId], job_id: [jobId] }, bytes });
|
|
546
|
+
const signal = AbortSignal.timeout(Number(ctx.io.env.CAVELON_HTTP_TIMEOUT_MS) || 120_000);
|
|
547
|
+
const response = await client.fetchRaw("PUT", target, { bytes, headers: { "Content-Type": "application/octet-stream" }, signal });
|
|
548
|
+
const text = new TextDecoder().decode(await bodyBytes(response, client.resolve(target), signal));
|
|
549
|
+
let data = text;
|
|
550
|
+
try {
|
|
551
|
+
data = JSON.parse(text);
|
|
552
|
+
}
|
|
553
|
+
catch {
|
|
554
|
+
}
|
|
555
|
+
if (!response.ok)
|
|
556
|
+
throw await client.refusal(response.status, data, `PUT ${target}`, response.headers);
|
|
557
|
+
return data;
|
|
558
|
+
}
|
|
559
|
+
async function download(ctx, sandbox, job, out) {
|
|
560
|
+
if (!isUuid(job.id))
|
|
561
|
+
throw new CavelonError(ExitCode.failure, { code: "invalid_job_id", message: `The instance returned an unexpected job id.` });
|
|
562
|
+
const file = await confinedPath(ctx, out ?? `sandbox-${job.id}.tar`, "The archive file");
|
|
563
|
+
const client = await ctx.client();
|
|
564
|
+
const { op } = await workflowOperation(ctx, "GET", `${JOB_ROUTE}/content`, "archive downloads");
|
|
565
|
+
const { path: target } = buildRequest(op, { params: { sandbox_id: [sandbox.id], job_id: [job.id] } });
|
|
566
|
+
const signal = AbortSignal.timeout(Number(ctx.io.env.CAVELON_HTTP_TIMEOUT_MS) || 120_000);
|
|
567
|
+
const response = await client.fetchRaw("GET", target, { accept: "application/x-tar", signal });
|
|
568
|
+
const bytes = await bodyBytes(response, client.resolve(target), signal);
|
|
569
|
+
if (!response.ok) {
|
|
570
|
+
const text = new TextDecoder().decode(bytes);
|
|
571
|
+
let data = text;
|
|
572
|
+
try {
|
|
573
|
+
data = JSON.parse(text);
|
|
574
|
+
}
|
|
575
|
+
catch {
|
|
576
|
+
}
|
|
577
|
+
throw await client.refusal(response.status, data, `GET ${target}`, response.headers);
|
|
578
|
+
}
|
|
579
|
+
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
|
580
|
+
const declared = response.headers.get("x-content-sha256");
|
|
581
|
+
if (declared && declared.toLowerCase() !== sha256) {
|
|
582
|
+
throw new CavelonError(ExitCode.server, {
|
|
583
|
+
code: "sandbox_artifact_digest_mismatch",
|
|
584
|
+
message: `The export arrived with sha256 ${sha256}, but the instance declared ${declared}; nothing was written.`,
|
|
585
|
+
hint: "Download it again.",
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
try {
|
|
589
|
+
await fs.writeFile(file, bytes, { flag: "wx" });
|
|
590
|
+
}
|
|
591
|
+
catch (error) {
|
|
592
|
+
if (error.code === "EEXIST") {
|
|
593
|
+
throw new CavelonError(ExitCode.conflict, {
|
|
594
|
+
code: "file_exists",
|
|
595
|
+
message: `${file} exists; cavelon does not overwrite it.`,
|
|
596
|
+
hint: `Choose another file with --out, then: ${cavelonCommand("artifacts", "export", sandbox.name, "--job", job.id, "--out")} <file>`,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
throw error;
|
|
600
|
+
}
|
|
601
|
+
return { file: path.relative(ctx.io.cwd, file) || file, size_bytes: bytes.length, sha256 };
|
|
602
|
+
}
|
|
603
|
+
export const artifactsExport = {
|
|
604
|
+
name: "artifacts export",
|
|
605
|
+
summary: "Take files out of an isolated container as a tar archive (isolated container only).",
|
|
606
|
+
description: "Starts an export job and returns its operation id; with --wait (or later with --job <id>) the tar is written to --out\n" +
|
|
607
|
+
"(default sandbox-<job>.tar), never over an existing file. On a customer VM, read results with `sandbox cat`.",
|
|
608
|
+
readOnly: false,
|
|
609
|
+
mcpTool: "artifacts_export",
|
|
610
|
+
positionals: [{ name: "sandbox", description: "Sandbox name or id.", required: true }],
|
|
611
|
+
options: {
|
|
612
|
+
path: { type: "string", multiple: true, value: "<path>", description: "Export only these workspace paths (repeatable; default: all)." },
|
|
613
|
+
out: { type: "string", value: "<file>", description: "Where to write the tar (default: sandbox-<job>.tar)." },
|
|
614
|
+
job: { type: "string", value: "<job_id>", description: "Download a job started earlier, instead of starting one." },
|
|
615
|
+
harness: HARNESS_OPTION,
|
|
616
|
+
revision: REVISION_OPTION,
|
|
617
|
+
wait: WAIT_OPTION,
|
|
618
|
+
timeout: TIMEOUT_OPTION,
|
|
619
|
+
"idempotency-key": UUID_KEY_OPTION,
|
|
620
|
+
},
|
|
621
|
+
examples: ["cavelon artifacts export orders-test --path output --wait", "cavelon artifacts export orders-test --job <job_id> --out results.tar"],
|
|
622
|
+
async run(ctx, input) {
|
|
623
|
+
const sandbox = await resolveSandbox(ctx, positional(input, "sandbox"));
|
|
624
|
+
requireOffer(ctx, sandbox, "archive", "artifacts export");
|
|
625
|
+
const out = stringOption(input, "out");
|
|
626
|
+
const jobRef = stringOption(input, "job");
|
|
627
|
+
let job;
|
|
628
|
+
if (jobRef) {
|
|
629
|
+
if (!isUuid(jobRef))
|
|
630
|
+
throw usageError(`--job "${jobRef}" is not a job id.`);
|
|
631
|
+
job = await getJob(ctx, sandbox.id, jobRef);
|
|
632
|
+
if (job.direction !== "export")
|
|
633
|
+
throw usageError(`Job ${job.id} is an ${job.direction}, not an export.`);
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
const harness = await harnessFor(ctx, input, sandbox);
|
|
637
|
+
const revision = await currentRevision(ctx, input, sandbox, harness);
|
|
638
|
+
const paths = listOption(input, "path");
|
|
639
|
+
const key = idempotencyKey(input);
|
|
640
|
+
job = await callStable(ctx, "POST", "/api/v1/sandboxes/{sandbox_id}/artifact-jobs", "archive jobs", {
|
|
641
|
+
params: { sandbox_id: [sandbox.id] },
|
|
642
|
+
headers: { "Idempotency-Key": key },
|
|
643
|
+
body: { harness_id: harness, direction: "export", expected_workspace_revision: revision, ...(paths.length ? { paths } : {}) },
|
|
644
|
+
}).catch((error) => {
|
|
645
|
+
throw withRetryKey(error, key);
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
const later = cavelonCommand("artifacts", "export", sandbox.name, "--job", job.id, ...(out ? ["--out", out] : []));
|
|
649
|
+
const base = { sandbox: sandbox.name, job_id: job.id, operation_id: job.operation_id };
|
|
650
|
+
if (job.status === "active" || job.status === "cancelling") {
|
|
651
|
+
if (!(boolOption(input, "wait") && ctx.mode === "cli")) {
|
|
652
|
+
return {
|
|
653
|
+
data: { ...base, status: job.status, phase: job.phase, download: later },
|
|
654
|
+
text: `Export job ${job.id} is ${job.phase}.\nWait with: cavelon wait ${job.operation_id}\nThen download: ${later}`,
|
|
655
|
+
exitCode: jobRef ? ExitCode.timeout : ExitCode.ok,
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
const waited = await waitAndReport(ctx, [job.operation_id], timeoutMs(ctx, stringOption(input, "timeout")));
|
|
659
|
+
job = await getJob(ctx, sandbox.id, job.id);
|
|
660
|
+
if (job.status !== "succeeded") {
|
|
661
|
+
if (job.status === "failed" || job.status === "cancelled")
|
|
662
|
+
throw jobFailed(job);
|
|
663
|
+
return { data: { ...base, ...waited.data, download: later }, text: `${waited.text}\nThen download: ${later}`, exitCode: waited.exitCode };
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (job.status !== "succeeded")
|
|
667
|
+
throw jobFailed(job);
|
|
668
|
+
const written = await download(ctx, sandbox, job, out);
|
|
669
|
+
return {
|
|
670
|
+
data: { ...base, status: job.status, ...written },
|
|
671
|
+
text: `Wrote ${written.file} (${written.size_bytes} bytes, sha256 ${written.sha256}).`,
|
|
672
|
+
};
|
|
673
|
+
},
|
|
674
|
+
};
|