@h-rig/cli 0.0.6-alpha.0

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 (47) hide show
  1. package/README.md +30 -0
  2. package/dist/bin/build-rig-binaries.js +107 -0
  3. package/dist/bin/rig.js +9330 -0
  4. package/dist/src/commands/_authority-runs.js +110 -0
  5. package/dist/src/commands/_connection-state.js +123 -0
  6. package/dist/src/commands/_doctor-checks.js +501 -0
  7. package/dist/src/commands/_operator-view.js +322 -0
  8. package/dist/src/commands/_parsers.js +107 -0
  9. package/dist/src/commands/_paths.js +50 -0
  10. package/dist/src/commands/_pi-install.js +184 -0
  11. package/dist/src/commands/_policy.js +79 -0
  12. package/dist/src/commands/_preflight.js +460 -0
  13. package/dist/src/commands/_probes.js +13 -0
  14. package/dist/src/commands/_run-driver-helpers.js +289 -0
  15. package/dist/src/commands/_server-client.js +364 -0
  16. package/dist/src/commands/_snapshot-upload.js +313 -0
  17. package/dist/src/commands/_task-picker.js +48 -0
  18. package/dist/src/commands/agent.js +497 -0
  19. package/dist/src/commands/browser.js +890 -0
  20. package/dist/src/commands/connect.js +180 -0
  21. package/dist/src/commands/dist.js +402 -0
  22. package/dist/src/commands/doctor.js +511 -0
  23. package/dist/src/commands/github.js +276 -0
  24. package/dist/src/commands/inbox.js +160 -0
  25. package/dist/src/commands/init.js +1254 -0
  26. package/dist/src/commands/inspect.js +174 -0
  27. package/dist/src/commands/inspector.js +256 -0
  28. package/dist/src/commands/plugin.js +167 -0
  29. package/dist/src/commands/profile-and-review.js +178 -0
  30. package/dist/src/commands/queue.js +197 -0
  31. package/dist/src/commands/remote.js +507 -0
  32. package/dist/src/commands/repo-git-harness.js +221 -0
  33. package/dist/src/commands/run.js +753 -0
  34. package/dist/src/commands/server.js +368 -0
  35. package/dist/src/commands/setup.js +681 -0
  36. package/dist/src/commands/task-report-bug.js +1083 -0
  37. package/dist/src/commands/task-run-driver.js +1933 -0
  38. package/dist/src/commands/task.js +1325 -0
  39. package/dist/src/commands/test.js +39 -0
  40. package/dist/src/commands/workspace.js +123 -0
  41. package/dist/src/commands.js +9012 -0
  42. package/dist/src/index.js +9348 -0
  43. package/dist/src/launcher.js +131 -0
  44. package/dist/src/report-bug.js +260 -0
  45. package/dist/src/runner.js +272 -0
  46. package/dist/src/withMutedConsole.js +42 -0
  47. package/package.json +31 -0
@@ -0,0 +1,313 @@
1
+ // @bun
2
+ // packages/cli/src/commands/_snapshot-upload.ts
3
+ import { mkdir, readdir, readFile, writeFile } from "fs/promises";
4
+ import { dirname as dirname2, resolve as resolve2, relative, sep } from "path";
5
+
6
+ // packages/cli/src/commands/_server-client.ts
7
+ import { spawnSync } from "child_process";
8
+
9
+ // packages/cli/src/runner.ts
10
+ import { EventBus } from "@rig/runtime/control-plane/runtime/events";
11
+ import { CliError } from "@rig/runtime/control-plane/errors";
12
+ import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
13
+ import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
14
+ import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
15
+ import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
16
+ import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
17
+
18
+ // packages/cli/src/commands/_server-client.ts
19
+ import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
20
+
21
+ // packages/cli/src/commands/_connection-state.ts
22
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
23
+ import { homedir } from "os";
24
+ import { dirname, resolve } from "path";
25
+ function resolveGlobalConnectionsPath(env = process.env) {
26
+ const explicit = env.RIG_CONNECTIONS_FILE?.trim();
27
+ if (explicit)
28
+ return resolve(explicit);
29
+ const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
30
+ if (stateDir)
31
+ return resolve(stateDir, "connections.json");
32
+ return resolve(homedir(), ".rig", "connections.json");
33
+ }
34
+ function resolveRepoConnectionPath(projectRoot) {
35
+ return resolve(projectRoot, ".rig", "state", "connection.json");
36
+ }
37
+ function readJsonFile(path) {
38
+ if (!existsSync(path))
39
+ return null;
40
+ try {
41
+ return JSON.parse(readFileSync(path, "utf8"));
42
+ } catch (error) {
43
+ throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
44
+ }
45
+ }
46
+ function normalizeConnection(value) {
47
+ if (!value || typeof value !== "object" || Array.isArray(value))
48
+ return null;
49
+ const record = value;
50
+ if (record.kind === "local")
51
+ return { kind: "local", mode: "auto" };
52
+ if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
53
+ const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
54
+ return { kind: "remote", baseUrl };
55
+ }
56
+ return null;
57
+ }
58
+ function readGlobalConnections(options = {}) {
59
+ const path = resolveGlobalConnectionsPath(options.env ?? process.env);
60
+ const payload = readJsonFile(path);
61
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
62
+ return { connections: {} };
63
+ }
64
+ const rawConnections = payload.connections;
65
+ const connections = {};
66
+ if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
67
+ for (const [alias, raw] of Object.entries(rawConnections)) {
68
+ const connection = normalizeConnection(raw);
69
+ if (connection)
70
+ connections[alias] = connection;
71
+ }
72
+ }
73
+ return { connections };
74
+ }
75
+ function readRepoConnection(projectRoot) {
76
+ const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
77
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
78
+ return null;
79
+ const record = payload;
80
+ const selected = typeof record.selected === "string" ? record.selected.trim() : "";
81
+ if (!selected)
82
+ return null;
83
+ return {
84
+ selected,
85
+ project: typeof record.project === "string" ? record.project : undefined,
86
+ linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
87
+ };
88
+ }
89
+ function resolveSelectedConnection(projectRoot, options = {}) {
90
+ const repo = readRepoConnection(projectRoot);
91
+ if (!repo)
92
+ return null;
93
+ if (repo.selected === "local")
94
+ return { alias: "local", connection: { kind: "local", mode: "auto" } };
95
+ const global = readGlobalConnections(options);
96
+ const connection = global.connections[repo.selected];
97
+ if (!connection) {
98
+ throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
99
+ }
100
+ return { alias: repo.selected, connection };
101
+ }
102
+
103
+ // packages/cli/src/commands/_server-client.ts
104
+ var cachedGitHubBearerToken;
105
+ function cleanToken(value) {
106
+ const trimmed = value?.trim();
107
+ return trimmed ? trimmed : null;
108
+ }
109
+ function readGitHubBearerTokenForRemote() {
110
+ if (cachedGitHubBearerToken !== undefined)
111
+ return cachedGitHubBearerToken;
112
+ const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
113
+ if (envToken) {
114
+ cachedGitHubBearerToken = envToken;
115
+ return cachedGitHubBearerToken;
116
+ }
117
+ const result = spawnSync("gh", ["auth", "token"], {
118
+ encoding: "utf8",
119
+ timeout: 5000,
120
+ stdio: ["ignore", "pipe", "ignore"]
121
+ });
122
+ cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
123
+ return cachedGitHubBearerToken;
124
+ }
125
+ async function ensureServerForCli(projectRoot) {
126
+ try {
127
+ const selected = resolveSelectedConnection(projectRoot);
128
+ if (selected?.connection.kind === "remote") {
129
+ return {
130
+ baseUrl: selected.connection.baseUrl,
131
+ authToken: readGitHubBearerTokenForRemote(),
132
+ connectionKind: "remote"
133
+ };
134
+ }
135
+ const connection = await ensureLocalRigServerConnection(projectRoot);
136
+ return {
137
+ baseUrl: connection.baseUrl,
138
+ authToken: connection.authToken,
139
+ connectionKind: "local"
140
+ };
141
+ } catch (error) {
142
+ if (error instanceof Error) {
143
+ throw new CliError2(error.message, 1);
144
+ }
145
+ throw error;
146
+ }
147
+ }
148
+ function mergeHeaders(headers, authToken) {
149
+ const merged = new Headers(headers);
150
+ if (authToken) {
151
+ merged.set("authorization", `Bearer ${authToken}`);
152
+ }
153
+ return merged;
154
+ }
155
+ function diagnosticMessage(payload) {
156
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
157
+ return null;
158
+ const record = payload;
159
+ const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
160
+ const messages = diagnostics.flatMap((entry) => {
161
+ if (!entry || typeof entry !== "object" || Array.isArray(entry))
162
+ return [];
163
+ const diagnostic = entry;
164
+ const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
165
+ const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
166
+ return message ? [`${kind}: ${message}`] : [];
167
+ });
168
+ return messages.length > 0 ? messages.join("; ") : null;
169
+ }
170
+ async function requestServerJson(context, pathname, init = {}) {
171
+ const server = await ensureServerForCli(context.projectRoot);
172
+ const response = await fetch(`${server.baseUrl}${pathname}`, {
173
+ ...init,
174
+ headers: mergeHeaders(init.headers, server.authToken)
175
+ });
176
+ const text = await response.text();
177
+ const payload = text.trim().length > 0 ? (() => {
178
+ try {
179
+ return JSON.parse(text);
180
+ } catch {
181
+ return null;
182
+ }
183
+ })() : null;
184
+ if (!response.ok) {
185
+ const diagnostics = diagnosticMessage(payload);
186
+ const detail = diagnostics ?? (text || response.statusText);
187
+ throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
188
+ }
189
+ return payload;
190
+ }
191
+
192
+ // packages/cli/src/commands/_snapshot-upload.ts
193
+ var UPLOADED_SNAPSHOT_PR_MARKER = "<!-- rig:uploaded-snapshot -->";
194
+ var SNAPSHOT_ARCHIVE_VERSION = 1;
195
+ var SNAPSHOT_ARCHIVE_CONTENT_TYPE = "application/vnd.rig.snapshot+json";
196
+ var DEFAULT_EXCLUDED_DIRECTORIES = new Set([
197
+ ".git",
198
+ ".rig",
199
+ "node_modules",
200
+ ".turbo",
201
+ ".next",
202
+ ".cache",
203
+ "coverage",
204
+ "dist",
205
+ "build",
206
+ "out"
207
+ ]);
208
+ function toPosixPath(path) {
209
+ return path.split(sep).join("/");
210
+ }
211
+ function assertManifestPath(root, relativePath) {
212
+ if (!relativePath || relativePath.startsWith("/") || relativePath.includes("\x00")) {
213
+ throw new Error(`Invalid snapshot path: ${relativePath}`);
214
+ }
215
+ const resolved = resolve2(root, relativePath);
216
+ const relativeToRoot = relative(root, resolved);
217
+ if (relativeToRoot.startsWith("..") || relativeToRoot === ".." || resolve2(relativeToRoot) === resolved) {
218
+ throw new Error(`Snapshot path escapes project root: ${relativePath}`);
219
+ }
220
+ return resolved;
221
+ }
222
+ async function buildSnapshotUploadManifest(projectRoot, options = {}) {
223
+ const root = resolve2(projectRoot);
224
+ const excludedDirectories = [...new Set([
225
+ ...DEFAULT_EXCLUDED_DIRECTORIES,
226
+ ...options.excludedDirectories ?? []
227
+ ])];
228
+ const excludedSet = new Set(excludedDirectories);
229
+ const files = [];
230
+ async function visit(dir) {
231
+ const entries = await readdir(dir, { withFileTypes: true });
232
+ for (const entry of entries) {
233
+ if (entry.isDirectory() && excludedSet.has(entry.name))
234
+ continue;
235
+ const fullPath = resolve2(dir, entry.name);
236
+ if (entry.isDirectory()) {
237
+ await visit(fullPath);
238
+ continue;
239
+ }
240
+ if (!entry.isFile())
241
+ continue;
242
+ files.push(toPosixPath(relative(root, fullPath)));
243
+ }
244
+ }
245
+ await visit(root);
246
+ files.sort();
247
+ return { root, files, excludedDirectories };
248
+ }
249
+ async function createSnapshotUploadArchive(projectRoot, options = {}) {
250
+ const manifest = await buildSnapshotUploadManifest(projectRoot, options);
251
+ const files = await Promise.all(manifest.files.map(async (path) => {
252
+ const fullPath = assertManifestPath(manifest.root, path);
253
+ return {
254
+ path,
255
+ contentBase64: (await readFile(fullPath)).toString("base64")
256
+ };
257
+ }));
258
+ return {
259
+ version: SNAPSHOT_ARCHIVE_VERSION,
260
+ root: manifest.root,
261
+ files,
262
+ excludedDirectories: manifest.excludedDirectories,
263
+ createdAt: (options.now?.() ?? new Date).toISOString()
264
+ };
265
+ }
266
+ function encodeSnapshotUploadArchive(archive) {
267
+ return Buffer.from(JSON.stringify(archive), "utf8").toString("base64");
268
+ }
269
+ async function writeSnapshotUploadArchive(projectRoot, outputPath, options = {}) {
270
+ const archive = await createSnapshotUploadArchive(projectRoot, options);
271
+ const target = resolve2(outputPath);
272
+ await mkdir(dirname2(target), { recursive: true });
273
+ await writeFile(target, JSON.stringify(archive, null, 2), "utf8");
274
+ return archive;
275
+ }
276
+ async function uploadSnapshotArchiveViaServer(context, input) {
277
+ const payload = await requestServerJson(context, `/api/projects/${encodeURIComponent(input.repoSlug)}/upload-snapshot`, {
278
+ method: "POST",
279
+ headers: { "content-type": "application/json" },
280
+ body: JSON.stringify({
281
+ archiveContentBase64: encodeSnapshotUploadArchive(input.archive),
282
+ contentType: SNAPSHOT_ARCHIVE_CONTENT_TYPE,
283
+ baseDir: input.baseDir
284
+ })
285
+ });
286
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
287
+ }
288
+ function appendUploadedSnapshotPrNotice(body) {
289
+ if (body.includes(UPLOADED_SNAPSHOT_PR_MARKER))
290
+ return body;
291
+ const prefix = body.trimEnd();
292
+ const notice = [
293
+ UPLOADED_SNAPSHOT_PR_MARKER,
294
+ "This PR was seeded from an uploaded local working-tree snapshot. Review local snapshot provenance before merging if repository policy requires it."
295
+ ].join(`
296
+ `);
297
+ return prefix.length > 0 ? `${prefix}
298
+
299
+ ${notice}
300
+ ` : `${notice}
301
+ `;
302
+ }
303
+ export {
304
+ writeSnapshotUploadArchive,
305
+ uploadSnapshotArchiveViaServer,
306
+ encodeSnapshotUploadArchive,
307
+ createSnapshotUploadArchive,
308
+ buildSnapshotUploadManifest,
309
+ appendUploadedSnapshotPrNotice,
310
+ UPLOADED_SNAPSHOT_PR_MARKER,
311
+ SNAPSHOT_ARCHIVE_VERSION,
312
+ SNAPSHOT_ARCHIVE_CONTENT_TYPE
313
+ };
@@ -0,0 +1,48 @@
1
+ // @bun
2
+ // packages/cli/src/commands/_task-picker.ts
3
+ import { createInterface } from "readline/promises";
4
+ function taskId(task) {
5
+ return typeof task.id === "string" && task.id.trim() ? task.id : "<unknown>";
6
+ }
7
+ function taskTitle(task) {
8
+ return typeof task.title === "string" && task.title.trim() ? task.title : "Untitled task";
9
+ }
10
+ function taskStatus(task) {
11
+ return typeof task.status === "string" && task.status.trim() ? task.status : "unknown";
12
+ }
13
+ function renderTaskPickerRows(tasks) {
14
+ return tasks.map((task, index) => `${index + 1}. ${taskId(task)} \xB7 ${taskStatus(task)} \xB7 ${taskTitle(task)}`);
15
+ }
16
+ async function selectTaskWithTextPicker(tasks, io = {}) {
17
+ if (tasks.length === 0)
18
+ return null;
19
+ if (tasks.length === 1)
20
+ return tasks[0];
21
+ const isTty = io.isTty ?? Boolean(process.stdin.isTTY && process.stdout.isTTY);
22
+ if (!isTty) {
23
+ throw new Error("task run requires an interactive terminal to pick a task; pass --task <id>, --next, or --detach with a task id.");
24
+ }
25
+ const prompt = io.prompt ?? (async (question) => {
26
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
27
+ try {
28
+ return await rl.question(question);
29
+ } finally {
30
+ rl.close();
31
+ }
32
+ });
33
+ console.log("Select Rig task:");
34
+ for (const row of renderTaskPickerRows(tasks))
35
+ console.log(` ${row}`);
36
+ const answer = (await prompt(`Task [1-${tasks.length}] or id: `)).trim();
37
+ if (!answer)
38
+ return null;
39
+ if (/^\d+$/.test(answer)) {
40
+ const index = Number.parseInt(answer, 10) - 1;
41
+ return tasks[index] ?? null;
42
+ }
43
+ return tasks.find((task) => taskId(task) === answer) ?? null;
44
+ }
45
+ export {
46
+ selectTaskWithTextPicker,
47
+ renderTaskPickerRows
48
+ };