@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.
Files changed (61) hide show
  1. package/README.md +697 -3
  2. package/dist/LICENSE +201 -0
  3. package/dist/agents.js +91 -0
  4. package/dist/branches.js +61 -0
  5. package/dist/capacity.js +162 -0
  6. package/dist/cli.js +4 -0
  7. package/dist/command.js +62 -0
  8. package/dist/commands/api.js +243 -0
  9. package/dist/commands/async.js +207 -0
  10. package/dist/commands/docs.js +158 -0
  11. package/dist/commands/index.js +112 -0
  12. package/dist/commands/init.js +452 -0
  13. package/dist/commands/inventory.js +56 -0
  14. package/dist/commands/limits-set.js +624 -0
  15. package/dist/commands/limits.js +156 -0
  16. package/dist/commands/loops.js +792 -0
  17. package/dist/commands/models.js +197 -0
  18. package/dist/commands/sandboxes.js +667 -0
  19. package/dist/commands/session.js +476 -0
  20. package/dist/commands/solution.js +760 -0
  21. package/dist/commands/tenants.js +266 -0
  22. package/dist/commands/values.js +383 -0
  23. package/dist/commands/work.js +744 -0
  24. package/dist/context.js +56 -0
  25. package/dist/contracts.js +189 -0
  26. package/dist/credentials.js +134 -0
  27. package/dist/errors.js +64 -0
  28. package/dist/features.js +37 -0
  29. package/dist/format.js +36 -0
  30. package/dist/fsutil.js +40 -0
  31. package/dist/git.js +41 -0
  32. package/dist/http.js +286 -0
  33. package/dist/invoke.js +161 -0
  34. package/dist/io.js +49 -0
  35. package/dist/limits.js +299 -0
  36. package/dist/local-state.js +60 -0
  37. package/dist/main.js +227 -0
  38. package/dist/markers.js +113 -0
  39. package/dist/mcp.js +130 -0
  40. package/dist/openapi.js +223 -0
  41. package/dist/operations.js +107 -0
  42. package/dist/package-check.js +342 -0
  43. package/dist/package-files.js +329 -0
  44. package/dist/pair-order.js +39 -0
  45. package/dist/paths.js +28 -0
  46. package/dist/principal.js +33 -0
  47. package/dist/project.js +138 -0
  48. package/dist/prompt.js +34 -0
  49. package/dist/results.js +66 -0
  50. package/dist/session.js +166 -0
  51. package/dist/shell.js +41 -0
  52. package/dist/skills/cavelon-authoring/SKILL.md +214 -0
  53. package/dist/skills/cavelon-long-running/SKILL.md +181 -0
  54. package/dist/skills/cavelon-loop/SKILL.md +158 -0
  55. package/dist/skills/cavelon-testing/SKILL.md +90 -0
  56. package/dist/sse.js +44 -0
  57. package/dist/tar.js +172 -0
  58. package/dist/user-config.js +36 -0
  59. package/dist/version.js +17 -0
  60. package/dist/zip.js +132 -0
  61. package/package.json +40 -4
package/dist/sse.js ADDED
@@ -0,0 +1,44 @@
1
+ export async function* readEvents(body) {
2
+ const decoder = new TextDecoder();
3
+ let buffer = "";
4
+ let event = "message";
5
+ let data = [];
6
+ let id;
7
+ const reader = body.getReader();
8
+ try {
9
+ for (;;) {
10
+ const { value, done } = await reader.read();
11
+ if (done)
12
+ break;
13
+ buffer += decoder.decode(value, { stream: true });
14
+ let newline;
15
+ while ((newline = buffer.search(/\r?\n/)) >= 0) {
16
+ const line = buffer.slice(0, newline);
17
+ buffer = buffer.slice(buffer[newline] === "\r" ? newline + 2 : newline + 1);
18
+ if (line === "") {
19
+ if (data.length)
20
+ yield { event, data: data.join("\n"), id };
21
+ event = "message";
22
+ data = [];
23
+ continue;
24
+ }
25
+ if (line.startsWith(":"))
26
+ continue;
27
+ const colon = line.indexOf(":");
28
+ const field = colon < 0 ? line : line.slice(0, colon);
29
+ const content = colon < 0 ? "" : line.slice(colon + 1).replace(/^ /, "");
30
+ if (field === "data")
31
+ data.push(content);
32
+ else if (field === "event")
33
+ event = content;
34
+ else if (field === "id")
35
+ id = content;
36
+ }
37
+ }
38
+ if (data.length)
39
+ yield { event, data: data.join("\n"), id };
40
+ }
41
+ finally {
42
+ reader.releaseLock();
43
+ }
44
+ }
package/dist/tar.js ADDED
@@ -0,0 +1,172 @@
1
+ import { createHash } from "node:crypto";
2
+ import { promises as fs } from "node:fs";
3
+ import path from "node:path";
4
+ import { CavelonError, ExitCode, usageError } from "./errors.js";
5
+ const BLOCK = 512;
6
+ const MAX_NAME_BYTES = 1024;
7
+ export function archiveName(parts) {
8
+ if (parts.length === 0)
9
+ throw usageError("An empty path cannot go into the archive.");
10
+ for (const part of parts) {
11
+ if (part === "" || part === "." || part === "..")
12
+ throw usageError(`"${parts.join("/")}" has an empty, "." or ".." part.`);
13
+ for (const ch of part) {
14
+ const code = ch.codePointAt(0);
15
+ if (code < 0x20 || code === 0x7f || code === 0x2f || code === 0x5c) {
16
+ throw usageError(`"${parts.join("/")}" has a character a Sandbox path cannot hold (a control character, or a slash or backslash in a name).`);
17
+ }
18
+ }
19
+ }
20
+ const name = parts.join("/");
21
+ if (Buffer.byteLength(name, "utf8") > MAX_NAME_BYTES)
22
+ throw usageError(`"${name.slice(0, 80)}…" is longer than ${MAX_NAME_BYTES} bytes.`);
23
+ return name;
24
+ }
25
+ function octal(value, width) {
26
+ return `${value.toString(8).padStart(width - 1, "0")}\0`;
27
+ }
28
+ function header(name, size, type, prefix = "") {
29
+ const block = Buffer.alloc(BLOCK);
30
+ block.write(name, 0, 100, "utf8");
31
+ block.write(octal(type === "5" ? 0o755 : 0o644, 8), 100, "ascii");
32
+ block.write(octal(0, 8), 108, "ascii");
33
+ block.write(octal(0, 8), 116, "ascii");
34
+ block.write(octal(size, 12), 124, "ascii");
35
+ block.write(octal(0, 12), 136, "ascii");
36
+ block.write(" ", 148, "ascii");
37
+ block.write(type, 156, "ascii");
38
+ block.write("ustar\0", 257, "ascii");
39
+ block.write("00", 263, "ascii");
40
+ block.write(prefix, 345, 155, "utf8");
41
+ let sum = 0;
42
+ for (const byte of block)
43
+ sum += byte;
44
+ block.write(`${sum.toString(8).padStart(6, "0")}\0 `, 148, "ascii");
45
+ return block;
46
+ }
47
+ function padded(content) {
48
+ const rest = content.length % BLOCK;
49
+ return rest ? [Buffer.from(content), Buffer.alloc(BLOCK - rest)] : [Buffer.from(content)];
50
+ }
51
+ function ustarFields(name) {
52
+ if (!/^[\x20-\x7e]+$/.test(name))
53
+ return undefined;
54
+ if (name.length <= 100)
55
+ return { name, prefix: "" };
56
+ for (let at = name.indexOf("/"); at !== -1; at = name.indexOf("/", at + 1)) {
57
+ const prefix = name.slice(0, at);
58
+ const rest = name.slice(at + 1);
59
+ if (prefix.length <= 155 && rest.length <= 100 && rest.length > 0)
60
+ return { name: rest, prefix };
61
+ }
62
+ return undefined;
63
+ }
64
+ function paxRecord(name) {
65
+ const body = ` path=${name}\n`;
66
+ let length = Buffer.byteLength(body, "utf8");
67
+ while (String(length).length + Buffer.byteLength(body, "utf8") !== length)
68
+ length = String(length).length + Buffer.byteLength(body, "utf8");
69
+ return Buffer.from(`${length}${body}`, "utf8");
70
+ }
71
+ function entryBlocks(entry) {
72
+ const name = entry.directory ? `${entry.name}/` : entry.name;
73
+ const size = entry.directory ? 0 : (entry.content?.length ?? 0);
74
+ const type = entry.directory ? "5" : "0";
75
+ const fields = ustarFields(name);
76
+ const blocks = [];
77
+ if (fields)
78
+ blocks.push(header(fields.name, size, type, fields.prefix));
79
+ else {
80
+ const record = paxRecord(entry.name);
81
+ blocks.push(header("PaxHeader", record.length, "x"), ...padded(record), header(name.replaceAll(/[^\x20-\x7e]/g, "_").slice(-100), size, type));
82
+ }
83
+ if (size)
84
+ blocks.push(...padded(entry.content));
85
+ return blocks;
86
+ }
87
+ function byName(a, b) {
88
+ if (a.name === b.name)
89
+ return 0;
90
+ return a.name < b.name ? -1 : 1;
91
+ }
92
+ export function buildArchive(entries) {
93
+ const sorted = [...entries].sort(byName);
94
+ const bytes = Buffer.concat([...sorted.flatMap(entryBlocks), Buffer.alloc(BLOCK * 2)]);
95
+ const directories = sorted.filter((e) => e.directory).length;
96
+ return { bytes, sha256: createHash("sha256").update(bytes).digest("hex"), files: sorted.length - directories, directories };
97
+ }
98
+ export function isUstar(bytes) {
99
+ return bytes.length >= BLOCK && Buffer.from(bytes.subarray(257, 262)).toString("latin1") === "ustar";
100
+ }
101
+ const COMPRESSED = [".tar.gz", ".tgz", ".tar.bz2", ".tbz2", ".tar.xz", ".txz", ".tar.zst", ".tzst"];
102
+ export async function archiveFrom(source, maxBytes) {
103
+ let stat;
104
+ try {
105
+ stat = await fs.stat(source);
106
+ }
107
+ catch {
108
+ throw usageError(`${source} does not exist.`);
109
+ }
110
+ const lower = source.toLowerCase();
111
+ if (stat.isFile() && COMPRESSED.some((ext) => lower.endsWith(ext))) {
112
+ throw usageError(`${path.basename(source)} is compressed; a Sandbox imports an uncompressed tar.`, "Decompress it first (for example `gunzip`), or pass the folder.");
113
+ }
114
+ if (stat.isFile() && lower.endsWith(".tar")) {
115
+ if (stat.size > maxBytes)
116
+ throw tooLarge(stat.size, maxBytes);
117
+ const bytes = new Uint8Array(await fs.readFile(source));
118
+ if (!isUstar(bytes))
119
+ throw usageError(`${path.basename(source)} is not an uncompressed (USTAR) tar archive.`);
120
+ return { bytes, sha256: createHash("sha256").update(bytes).digest("hex"), files: -1, directories: -1, from: "tar" };
121
+ }
122
+ if (stat.isFile()) {
123
+ if (stat.size > maxBytes)
124
+ throw tooLarge(stat.size, maxBytes);
125
+ const name = archiveName([path.basename(source)]);
126
+ return { ...buildArchive([{ name, directory: false, content: await fs.readFile(source) }]), from: "file" };
127
+ }
128
+ if (!stat.isDirectory())
129
+ throw usageError(`${source} is neither a folder nor a file.`);
130
+ const entries = [];
131
+ const refused = [];
132
+ let total = 0;
133
+ const walk = async (dir, parts) => {
134
+ const items = await fs.readdir(dir, { withFileTypes: true });
135
+ const visit = async (item) => {
136
+ const here = [...parts, item.name];
137
+ const full = path.join(dir, item.name);
138
+ if (item.isDirectory()) {
139
+ entries.push({ name: archiveName(here), directory: true });
140
+ return walk(full, here);
141
+ }
142
+ if (!item.isFile()) {
143
+ refused.push(here.join("/"));
144
+ return;
145
+ }
146
+ const name = archiveName(here);
147
+ total += (await fs.stat(full)).size + BLOCK;
148
+ if (total > maxBytes)
149
+ throw tooLarge(total, maxBytes);
150
+ entries.push({ name, directory: false, content: await fs.readFile(full) });
151
+ };
152
+ await Promise.all(items.map(visit));
153
+ };
154
+ await walk(source, []);
155
+ refused.sort((a, b) => a.localeCompare(b, "en"));
156
+ if (refused.length) {
157
+ throw usageError(`${refused.length} entries are links or special files, which a Sandbox does not take: ${refused.slice(0, 5).join(", ")}${refused.length > 5 ? ", …" : ""}.`, "Replace links with the files they point to, or leave them out of the folder.");
158
+ }
159
+ if (!entries.some((e) => !e.directory))
160
+ throw usageError(`${source} holds no files.`);
161
+ const archive = buildArchive(entries);
162
+ if (archive.bytes.length > maxBytes)
163
+ throw tooLarge(archive.bytes.length, maxBytes);
164
+ return { ...archive, from: "folder" };
165
+ }
166
+ function tooLarge(size, max) {
167
+ return new CavelonError(ExitCode.validation, {
168
+ code: "sandbox_transfer_size_limit",
169
+ message: `The archive would be ${size} bytes; a Sandbox imports at most ${max} bytes at once.`,
170
+ hint: "Seed fewer or smaller files; a seed replaces the workspace, so everything the loop starts from goes into one archive.",
171
+ });
172
+ }
@@ -0,0 +1,36 @@
1
+ import path from "node:path";
2
+ import { configDir } from "./paths.js";
3
+ import { readJsonFile, writeFileAtomic } from "./fsutil.js";
4
+ export function userConfigPath(env) {
5
+ return path.join(configDir(env), "config.json");
6
+ }
7
+ export async function loadUserConfig(env) {
8
+ const raw = await readJsonFile(userConfigPath(env));
9
+ return { current_instance: raw?.current_instance, instances: raw?.instances ?? {} };
10
+ }
11
+ export async function saveUserConfig(env, config) {
12
+ await writeFileAtomic(userConfigPath(env), JSON.stringify(config, null, 2) + "\n", 0o600, 0o700);
13
+ }
14
+ export async function updateInstance(env, url, change, options = {}) {
15
+ const config = await loadUserConfig(env);
16
+ const next = change({ ...(config.instances[url] ?? {}) });
17
+ if (next === undefined) {
18
+ delete config.instances[url];
19
+ if (config.current_instance === url)
20
+ config.current_instance = undefined;
21
+ }
22
+ else {
23
+ config.instances[url] = next;
24
+ if (options.makeCurrent)
25
+ config.current_instance = url;
26
+ }
27
+ await saveUserConfig(env, config);
28
+ return config;
29
+ }
30
+ export function tokenKind(token) {
31
+ if (token.startsWith("cvpat_"))
32
+ return "personal_access_token";
33
+ if (token.startsWith("cbp_"))
34
+ return "api_key";
35
+ return "unknown";
36
+ }
@@ -0,0 +1,17 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { fileURLToPath } from "node:url";
3
+ import path from "node:path";
4
+ function readVersion() {
5
+ const file = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "package.json");
6
+ try {
7
+ return JSON.parse(readFileSync(file, "utf8")).version ?? "0.0.0";
8
+ }
9
+ catch {
10
+ return "0.0.0";
11
+ }
12
+ }
13
+ export const KIT_VERSION = readVersion();
14
+ export const SUPPORTED_CONTRACTS = {
15
+ api_versions: ["v1"],
16
+ package_versions: ["v1", "v2", "v3"],
17
+ };
package/dist/zip.js ADDED
@@ -0,0 +1,132 @@
1
+ import { promises as fs } from "node:fs";
2
+ export class ZipError extends Error {
3
+ notZip;
4
+ constructor(message, notZip) {
5
+ super(message);
6
+ this.notZip = notZip;
7
+ this.name = "ZipError";
8
+ }
9
+ }
10
+ const EOCD = 0x06054b50;
11
+ const EOCD64_LOCATOR = 0x07064b50;
12
+ const EOCD64 = 0x06064b50;
13
+ const CENTRAL_HEADER = 0x02014b50;
14
+ const EOCD_SIZE = 22;
15
+ const MAX_COMMENT = 0xffff;
16
+ const U16_MAX = 0xffff;
17
+ const U32_MAX = 0xffffffff;
18
+ const MAX_RECORDS = 100_000;
19
+ async function readAt(handle, position, length) {
20
+ const buffer = Buffer.alloc(length);
21
+ const { bytesRead } = await handle.read(buffer, 0, length, position);
22
+ if (bytesRead !== length)
23
+ throw new ZipError("The archive ends early.", false);
24
+ return buffer;
25
+ }
26
+ function u64(buffer, offset) {
27
+ const value = buffer.readBigUInt64LE(offset);
28
+ if (value > BigInt(Number.MAX_SAFE_INTEGER))
29
+ throw new ZipError("The archive declares a size no file has.", false);
30
+ return Number(value);
31
+ }
32
+ function findEnd(tail) {
33
+ for (let i = tail.length - EOCD_SIZE; i >= 0; i--) {
34
+ if (tail.readUInt32LE(i) === EOCD && i + EOCD_SIZE + tail.readUInt16LE(i + 20) <= tail.length)
35
+ return i;
36
+ }
37
+ return -1;
38
+ }
39
+ async function directoryOf(handle, fileSize) {
40
+ if (fileSize < EOCD_SIZE)
41
+ throw new ZipError("The file is too short to be a zip archive.", true);
42
+ const tailStart = Math.max(0, fileSize - EOCD_SIZE - MAX_COMMENT);
43
+ const tail = await readAt(handle, tailStart, fileSize - tailStart);
44
+ const end = findEnd(tail);
45
+ if (end < 0)
46
+ throw new ZipError("The file has no zip end record; it is not a zip archive.", true);
47
+ let records = tail.readUInt16LE(end + 10);
48
+ let size = tail.readUInt32LE(end + 12);
49
+ let offset = tail.readUInt32LE(end + 16);
50
+ if (records === U16_MAX || size === U32_MAX || offset === U32_MAX) {
51
+ const locatorAt = tailStart + end - 20;
52
+ if (locatorAt < 0)
53
+ throw new ZipError("The archive's zip64 record is missing.", false);
54
+ const locator = await readAt(handle, locatorAt, 20);
55
+ if (locator.readUInt32LE(0) !== EOCD64_LOCATOR)
56
+ throw new ZipError("The archive's zip64 record is missing.", false);
57
+ const record = await readAt(handle, u64(locator, 8), 56);
58
+ if (record.readUInt32LE(0) !== EOCD64)
59
+ throw new ZipError("The archive's zip64 record is damaged.", false);
60
+ records = u64(record, 32);
61
+ size = u64(record, 40);
62
+ offset = u64(record, 48);
63
+ }
64
+ if (offset + size > fileSize)
65
+ throw new ZipError("The archive's directory lies outside the file.", false);
66
+ return { records, size, offset };
67
+ }
68
+ function zip64Sizes(extra, uncompressed, compressed) {
69
+ let at = 0;
70
+ while (at + 4 <= extra.length) {
71
+ const id = extra.readUInt16LE(at);
72
+ const length = extra.readUInt16LE(at + 2);
73
+ if (id === 0x0001) {
74
+ let field = at + 4;
75
+ const fieldEnd = Math.min(field + length, extra.length);
76
+ if (uncompressed === U32_MAX && field + 8 <= fieldEnd) {
77
+ uncompressed = u64(extra, field);
78
+ field += 8;
79
+ }
80
+ if (compressed === U32_MAX && field + 8 <= fieldEnd)
81
+ compressed = u64(extra, field);
82
+ break;
83
+ }
84
+ at += 4 + length;
85
+ }
86
+ return { uncompressed, compressed };
87
+ }
88
+ function ratioOf(uncompressed, compressed) {
89
+ if (uncompressed <= 0)
90
+ return 0;
91
+ if (compressed <= 0)
92
+ return Infinity;
93
+ return uncompressed / compressed;
94
+ }
95
+ export async function readZipSummary(file) {
96
+ const handle = await fs.open(file, "r");
97
+ try {
98
+ const { size: fileSize } = await handle.stat();
99
+ const directory = await directoryOf(handle, fileSize);
100
+ const data = await readAt(handle, directory.offset, directory.size);
101
+ const summary = { files: 0, uncompressedBytes: 0, maxRatio: 0, maxRatioEntry: null };
102
+ let at = 0;
103
+ for (let n = 0; n < Math.min(directory.records, MAX_RECORDS); n++) {
104
+ if (at + 46 > data.length || data.readUInt32LE(at) !== CENTRAL_HEADER)
105
+ throw new ZipError("The archive's directory is damaged.", false);
106
+ const nameLength = data.readUInt16LE(at + 28);
107
+ const extraLength = data.readUInt16LE(at + 30);
108
+ const commentLength = data.readUInt16LE(at + 32);
109
+ const next = at + 46 + nameLength + extraLength + commentLength;
110
+ if (next > data.length)
111
+ throw new ZipError("The archive's directory is damaged.", false);
112
+ const name = data.toString("utf8", at + 46, at + 46 + nameLength);
113
+ const sizes = zip64Sizes(data.subarray(at + 46 + nameLength, at + 46 + nameLength + extraLength), data.readUInt32LE(at + 24), data.readUInt32LE(at + 20));
114
+ at = next;
115
+ if (name.endsWith("/"))
116
+ continue;
117
+ summary.files++;
118
+ summary.uncompressedBytes += sizes.uncompressed;
119
+ const ratio = ratioOf(sizes.uncompressed, sizes.compressed);
120
+ if (ratio > summary.maxRatio) {
121
+ summary.maxRatio = ratio;
122
+ summary.maxRatioEntry = name;
123
+ }
124
+ }
125
+ if (directory.records > MAX_RECORDS)
126
+ summary.files = Math.max(summary.files, directory.records);
127
+ return summary;
128
+ }
129
+ finally {
130
+ await handle.close();
131
+ }
132
+ }
package/package.json CHANGED
@@ -1,12 +1,48 @@
1
1
  {
2
2
  "name": "@cavelon/cli",
3
- "version": "0.0.0",
4
- "description": "Placeholder for the Cavelon dev-kit CLI. Not usable yet.",
3
+ "version": "0.1.1",
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",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/goodguys-gmbh/cavelon-dev-kit.git"
9
+ "url": "git+https://github.com/goodguys-gmbh/cavelon-dev-kit.git",
10
+ "directory": "cli"
10
11
  },
11
- "files": ["README.md"]
12
+ "type": "module",
13
+ "bin": {
14
+ "cavelon": "dist/cli.js"
15
+ },
16
+ "files": [
17
+ "dist/",
18
+ "README.md"
19
+ ],
20
+ "engines": {
21
+ "node": ">=20.3"
22
+ },
23
+ "scripts": {
24
+ "build": "tsc -p tsconfig.build.json && node scripts/copy-skills.mjs",
25
+ "typecheck": "tsc -p tsconfig.json --noEmit",
26
+ "lint": "eslint .",
27
+ "test": "vitest run",
28
+ "docs:commands": "vitest run test/commands-doc.test.ts --update",
29
+ "prepack": "npm run build"
30
+ },
31
+ "dependencies": {
32
+ "@modelcontextprotocol/sdk": "^1.31.0",
33
+ "ajv": "^8.20.0",
34
+ "ajv-formats": "^3.0.1",
35
+ "yaml": "^2.9.1"
36
+ },
37
+ "optionalDependencies": {
38
+ "@napi-rs/keyring": "^2.1.0"
39
+ },
40
+ "devDependencies": {
41
+ "@eslint/js": "^9.39.0",
42
+ "@types/node": "^20.19.0",
43
+ "eslint": "^9.39.0",
44
+ "typescript": "~5.9.3",
45
+ "typescript-eslint": "^8.71.0",
46
+ "vitest": "^4.0.0"
47
+ }
12
48
  }