@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,79 @@
1
+ // @bun
2
+ // packages/cli/src/commands/_policy.ts
3
+ import { appendFileSync, mkdirSync } from "fs";
4
+ import { resolve } from "path";
5
+
6
+ // packages/cli/src/runner.ts
7
+ import { EventBus } from "@rig/runtime/control-plane/runtime/events";
8
+ import { CliError } from "@rig/runtime/control-plane/errors";
9
+ import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
10
+ import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
11
+ import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
12
+ import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
13
+ import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
14
+ function formatCommand(parts) {
15
+ return parts.map((part) => /[^a-zA-Z0-9_./:-]/.test(part) ? JSON.stringify(part) : part).join(" ");
16
+ }
17
+
18
+ // packages/cli/src/commands/_policy.ts
19
+ import { evaluate as evaluate2, loadPolicy as loadPolicy2, resolveAction as resolveAction2 } from "@rig/runtime/control-plane/runtime/guard";
20
+ import { resolveHarnessPaths } from "@rig/runtime/control-plane/native/utils";
21
+ function resolveEffectivePolicyMode(context) {
22
+ const envMode = process.env.RIG_BASH_MODE;
23
+ if (envMode === "off" || envMode === "observe" || envMode === "enforce") {
24
+ return envMode;
25
+ }
26
+ return context.policyMode || loadPolicy2(context.projectRoot).mode;
27
+ }
28
+ function appendControlledBashAudit(context, mode, command, args, matchedRuleIds, action) {
29
+ if (mode === "off") {
30
+ return;
31
+ }
32
+ const logsDir = resolveHarnessPaths(context.projectRoot).logsDir;
33
+ const logFile = resolve(logsDir, "controlled-bash.jsonl");
34
+ mkdirSync(logsDir, { recursive: true });
35
+ const payload = {
36
+ timestamp: new Date().toISOString(),
37
+ mode,
38
+ cwd: process.cwd(),
39
+ pid: process.pid,
40
+ ppid: process.ppid,
41
+ argv: args,
42
+ command,
43
+ matchedRuleIds
44
+ };
45
+ if (action) {
46
+ payload.action = action;
47
+ }
48
+ appendFileSync(logFile, `${JSON.stringify(payload)}
49
+ `, "utf-8");
50
+ }
51
+ async function enforceNativeCommandPolicy(context, args, options) {
52
+ const mode = resolveEffectivePolicyMode(context);
53
+ const command = formatCommand([options.commandPrefix, ...args]);
54
+ const decision = evaluate2({
55
+ projectRoot: context.projectRoot,
56
+ evaluation: { type: "command", command }
57
+ });
58
+ const action = resolveAction2(mode, decision.matchedRules);
59
+ const matchedRuleIds = decision.matchedRules.map((rule) => rule.id);
60
+ await context.emitEvent("policy.decision", {
61
+ target: "command",
62
+ command,
63
+ mode,
64
+ allowed: action !== "block",
65
+ matchedRuleIds,
66
+ reasons: decision.matchedRules.map((rule) => rule.reason)
67
+ });
68
+ if (options.writeAuditLog) {
69
+ appendControlledBashAudit(context, mode, command, args, matchedRuleIds, action === "block" ? "blocked" : action === "warn" ? "warn" : undefined);
70
+ }
71
+ if (action === "block") {
72
+ throw new CliError2(`Policy blocked command: ${command}`, 126);
73
+ }
74
+ }
75
+ export {
76
+ resolveEffectivePolicyMode,
77
+ enforceNativeCommandPolicy,
78
+ appendControlledBashAudit
79
+ };
@@ -0,0 +1,460 @@
1
+ // @bun
2
+ // packages/cli/src/runner.ts
3
+ import { EventBus } from "@rig/runtime/control-plane/runtime/events";
4
+ import { CliError } from "@rig/runtime/control-plane/errors";
5
+ import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
6
+ import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
7
+ import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
8
+ import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
9
+ import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
10
+
11
+ // packages/cli/src/commands/_preflight.ts
12
+ import { ensureProjectMainFreshBeforeRun } from "@rig/runtime/control-plane/project-main-pre-run-sync";
13
+
14
+ // packages/cli/src/commands/_connection-state.ts
15
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
16
+ import { homedir } from "os";
17
+ import { dirname, resolve } from "path";
18
+ function resolveGlobalConnectionsPath(env = process.env) {
19
+ const explicit = env.RIG_CONNECTIONS_FILE?.trim();
20
+ if (explicit)
21
+ return resolve(explicit);
22
+ const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
23
+ if (stateDir)
24
+ return resolve(stateDir, "connections.json");
25
+ return resolve(homedir(), ".rig", "connections.json");
26
+ }
27
+ function resolveRepoConnectionPath(projectRoot) {
28
+ return resolve(projectRoot, ".rig", "state", "connection.json");
29
+ }
30
+ function readJsonFile(path) {
31
+ if (!existsSync(path))
32
+ return null;
33
+ try {
34
+ return JSON.parse(readFileSync(path, "utf8"));
35
+ } catch (error) {
36
+ throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
37
+ }
38
+ }
39
+ function normalizeConnection(value) {
40
+ if (!value || typeof value !== "object" || Array.isArray(value))
41
+ return null;
42
+ const record = value;
43
+ if (record.kind === "local")
44
+ return { kind: "local", mode: "auto" };
45
+ if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
46
+ const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
47
+ return { kind: "remote", baseUrl };
48
+ }
49
+ return null;
50
+ }
51
+ function readGlobalConnections(options = {}) {
52
+ const path = resolveGlobalConnectionsPath(options.env ?? process.env);
53
+ const payload = readJsonFile(path);
54
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
55
+ return { connections: {} };
56
+ }
57
+ const rawConnections = payload.connections;
58
+ const connections = {};
59
+ if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
60
+ for (const [alias, raw] of Object.entries(rawConnections)) {
61
+ const connection = normalizeConnection(raw);
62
+ if (connection)
63
+ connections[alias] = connection;
64
+ }
65
+ }
66
+ return { connections };
67
+ }
68
+ function readRepoConnection(projectRoot) {
69
+ const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
70
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
71
+ return null;
72
+ const record = payload;
73
+ const selected = typeof record.selected === "string" ? record.selected.trim() : "";
74
+ if (!selected)
75
+ return null;
76
+ return {
77
+ selected,
78
+ project: typeof record.project === "string" ? record.project : undefined,
79
+ linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
80
+ };
81
+ }
82
+ function resolveSelectedConnection(projectRoot, options = {}) {
83
+ const repo = readRepoConnection(projectRoot);
84
+ if (!repo)
85
+ return null;
86
+ if (repo.selected === "local")
87
+ return { alias: "local", connection: { kind: "local", mode: "auto" } };
88
+ const global = readGlobalConnections(options);
89
+ const connection = global.connections[repo.selected];
90
+ if (!connection) {
91
+ throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
92
+ }
93
+ return { alias: repo.selected, connection };
94
+ }
95
+
96
+ // packages/cli/src/commands/_server-client.ts
97
+ import { spawnSync } from "child_process";
98
+ import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
99
+ var cachedGitHubBearerToken;
100
+ function cleanToken(value) {
101
+ const trimmed = value?.trim();
102
+ return trimmed ? trimmed : null;
103
+ }
104
+ function readGitHubBearerTokenForRemote() {
105
+ if (cachedGitHubBearerToken !== undefined)
106
+ return cachedGitHubBearerToken;
107
+ const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
108
+ if (envToken) {
109
+ cachedGitHubBearerToken = envToken;
110
+ return cachedGitHubBearerToken;
111
+ }
112
+ const result = spawnSync("gh", ["auth", "token"], {
113
+ encoding: "utf8",
114
+ timeout: 5000,
115
+ stdio: ["ignore", "pipe", "ignore"]
116
+ });
117
+ cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
118
+ return cachedGitHubBearerToken;
119
+ }
120
+ async function ensureServerForCli(projectRoot) {
121
+ try {
122
+ const selected = resolveSelectedConnection(projectRoot);
123
+ if (selected?.connection.kind === "remote") {
124
+ return {
125
+ baseUrl: selected.connection.baseUrl,
126
+ authToken: readGitHubBearerTokenForRemote(),
127
+ connectionKind: "remote"
128
+ };
129
+ }
130
+ const connection = await ensureLocalRigServerConnection(projectRoot);
131
+ return {
132
+ baseUrl: connection.baseUrl,
133
+ authToken: connection.authToken,
134
+ connectionKind: "local"
135
+ };
136
+ } catch (error) {
137
+ if (error instanceof Error) {
138
+ throw new CliError2(error.message, 1);
139
+ }
140
+ throw error;
141
+ }
142
+ }
143
+ function mergeHeaders(headers, authToken) {
144
+ const merged = new Headers(headers);
145
+ if (authToken) {
146
+ merged.set("authorization", `Bearer ${authToken}`);
147
+ }
148
+ return merged;
149
+ }
150
+ function diagnosticMessage(payload) {
151
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
152
+ return null;
153
+ const record = payload;
154
+ const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
155
+ const messages = diagnostics.flatMap((entry) => {
156
+ if (!entry || typeof entry !== "object" || Array.isArray(entry))
157
+ return [];
158
+ const diagnostic = entry;
159
+ const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
160
+ const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
161
+ return message ? [`${kind}: ${message}`] : [];
162
+ });
163
+ return messages.length > 0 ? messages.join("; ") : null;
164
+ }
165
+ async function requestServerJson(context, pathname, init = {}) {
166
+ const server = await ensureServerForCli(context.projectRoot);
167
+ const response = await fetch(`${server.baseUrl}${pathname}`, {
168
+ ...init,
169
+ headers: mergeHeaders(init.headers, server.authToken)
170
+ });
171
+ const text = await response.text();
172
+ const payload = text.trim().length > 0 ? (() => {
173
+ try {
174
+ return JSON.parse(text);
175
+ } catch {
176
+ return null;
177
+ }
178
+ })() : null;
179
+ if (!response.ok) {
180
+ const diagnostics = diagnosticMessage(payload);
181
+ const detail = diagnostics ?? (text || response.statusText);
182
+ throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
183
+ }
184
+ return payload;
185
+ }
186
+
187
+ // packages/cli/src/commands/_pi-install.ts
188
+ import { existsSync as existsSync2, readFileSync as readFileSync2, rmSync } from "fs";
189
+ import { homedir as homedir2 } from "os";
190
+ import { resolve as resolve2 } from "path";
191
+ var PI_RIG_PACKAGE_NAME = "@rig/pi-rig";
192
+ async function defaultCommandRunner(command, options = {}) {
193
+ const proc = Bun.spawn(command, { cwd: options.cwd, stdout: "pipe", stderr: "pipe" });
194
+ const [stdout, stderr, exitCode] = await Promise.all([
195
+ new Response(proc.stdout).text(),
196
+ new Response(proc.stderr).text(),
197
+ proc.exited
198
+ ]);
199
+ return { exitCode, stdout, stderr };
200
+ }
201
+ function resolvePiRigExtensionPath(homeDir) {
202
+ return resolve2(homeDir, ".pi", "agent", "extensions", "pi-rig");
203
+ }
204
+ function resolvePiHomeDir(inputHomeDir) {
205
+ return inputHomeDir ?? process.env.RIG_PI_HOME_DIR?.trim() ?? homedir2();
206
+ }
207
+ function piListContainsPiRig(output) {
208
+ return output.split(/\r?\n/).some((line) => {
209
+ const normalized = line.trim();
210
+ return normalized.includes(PI_RIG_PACKAGE_NAME) || /(?:^|[\\/])packages[\\/]pi-rig(?:$|\s)/.test(normalized);
211
+ });
212
+ }
213
+ async function safeRun(runner, command, options) {
214
+ try {
215
+ return await runner(command, options);
216
+ } catch (error) {
217
+ return { exitCode: 1, stdout: "", stderr: error instanceof Error ? error.message : String(error) };
218
+ }
219
+ }
220
+ async function checkPiRigInstall(input = {}) {
221
+ const home = resolvePiHomeDir(input.homeDir);
222
+ const extensionPath = resolvePiRigExtensionPath(home);
223
+ if (process.env.RIG_TEST_FAKE_PI_INSTALL === "1") {
224
+ return {
225
+ extensionPath,
226
+ pi: { ok: true, label: "pi", detail: "fake-pi" },
227
+ piRig: { ok: true, label: "pi-rig global extension", detail: extensionPath }
228
+ };
229
+ }
230
+ const exists = input.exists ?? existsSync2;
231
+ const runner = input.commandRunner ?? defaultCommandRunner;
232
+ const piResult = await safeRun(runner, ["pi", "--version"]);
233
+ const piListResult = piResult.exitCode === 0 ? await safeRun(runner, ["pi", "list"]) : { exitCode: 1, stdout: "", stderr: "" };
234
+ const listedPiRig = piListResult.exitCode === 0 && piListContainsPiRig(`${piListResult.stdout}
235
+ ${piListResult.stderr}`);
236
+ const legacyBridge = exists(resolve2(extensionPath, "index.ts"));
237
+ const hasPiRig = listedPiRig;
238
+ return {
239
+ extensionPath,
240
+ pi: {
241
+ ok: piResult.exitCode === 0,
242
+ label: "pi",
243
+ detail: (piResult.stdout || piResult.stderr).trim() || undefined,
244
+ hint: piResult.exitCode === 0 ? undefined : "Install Pi or run `rig init --yes` to install/update the Pi runtime."
245
+ },
246
+ piRig: {
247
+ ok: hasPiRig,
248
+ label: "pi-rig global extension",
249
+ detail: hasPiRig ? piListResult.stdout.trim() || PI_RIG_PACKAGE_NAME : legacyBridge ? `${extensionPath} (legacy bridge; reinstall required)` : undefined,
250
+ hint: hasPiRig ? undefined : "Run `rig init --yes` to install/enable the global pi-rig package with `pi install`."
251
+ }
252
+ };
253
+ }
254
+ async function buildPiSetupChecks(input = {}) {
255
+ const status = await checkPiRigInstall(input);
256
+ return [status.pi, status.piRig];
257
+ }
258
+
259
+ // packages/cli/src/commands/_preflight.ts
260
+ function preflightCheck(id, label, status, detail, remediation) {
261
+ return {
262
+ id,
263
+ label,
264
+ status,
265
+ ...detail ? { detail } : {},
266
+ ...remediation ? { remediation } : {}
267
+ };
268
+ }
269
+ function message(error) {
270
+ return error instanceof Error ? error.message : String(error);
271
+ }
272
+ function isAuthenticated(payload) {
273
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
274
+ return false;
275
+ const record = payload;
276
+ return record.signedIn === true || record.authenticated === true || record.status === "authenticated" || record.ok === true && typeof record.login === "string" && record.login.trim().length > 0;
277
+ }
278
+ function taskMatchesId(entry, taskId) {
279
+ if (!entry || typeof entry !== "object" || Array.isArray(entry))
280
+ return false;
281
+ const record = entry;
282
+ if (record.id === taskId || record.taskId === taskId)
283
+ return true;
284
+ const raw = record.raw;
285
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
286
+ const rawRecord = raw;
287
+ if (String(rawRecord.number ?? "") === taskId.replace(/^#/, ""))
288
+ return true;
289
+ }
290
+ return false;
291
+ }
292
+ function isActiveRunStatus(status) {
293
+ const normalized = String(status ?? "running").toLowerCase();
294
+ return !["completed", "complete", "done", "merged", "closed", "failed", "cancelled", "canceled", "needs_attention", "needs-attention", "stopped"].includes(normalized);
295
+ }
296
+ function permissionAllowsPr(payload) {
297
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
298
+ return null;
299
+ const record = payload;
300
+ if (record.canOpenPullRequest === true || record.pullRequests === true || record.push === true || record.maintain === true || record.admin === true)
301
+ return true;
302
+ if (record.canOpenPullRequest === false || record.pullRequests === false || record.push === false)
303
+ return false;
304
+ const permissions = record.permissions;
305
+ if (permissions && typeof permissions === "object" && !Array.isArray(permissions)) {
306
+ const p = permissions;
307
+ if (p.push === true || p.maintain === true || p.admin === true)
308
+ return true;
309
+ if (p.push === false && p.maintain !== true && p.admin !== true)
310
+ return false;
311
+ }
312
+ return null;
313
+ }
314
+ function projectCheckoutReady(payload) {
315
+ if (!payload || typeof payload !== "object" || Array.isArray(payload))
316
+ return null;
317
+ const record = payload;
318
+ if (record.checkoutReady === true || record.ready === true)
319
+ return true;
320
+ if (record.checkoutReady === false || record.ready === false)
321
+ return false;
322
+ const project = record.project;
323
+ if (project && typeof project === "object" && !Array.isArray(project)) {
324
+ const checkouts = project.checkouts;
325
+ if (Array.isArray(checkouts))
326
+ return checkouts.length > 0;
327
+ }
328
+ return null;
329
+ }
330
+ function activeDuplicateRun(runs, taskId) {
331
+ if (!Array.isArray(runs))
332
+ return null;
333
+ for (const run of runs) {
334
+ if (!run || typeof run !== "object" || Array.isArray(run))
335
+ continue;
336
+ const record = run;
337
+ if ((record.taskId === taskId || record.task === taskId) && isActiveRunStatus(record.status))
338
+ return record;
339
+ }
340
+ return null;
341
+ }
342
+ async function runFastTaskRunPreflight(context, options = {}) {
343
+ const checks = [];
344
+ const request = options.requestJson ?? ((pathname, init) => requestServerJson(context, pathname, init));
345
+ const taskId = options.taskId?.trim() || null;
346
+ try {
347
+ await request("/api/server/status");
348
+ checks.push(preflightCheck("server", "Rig server reachable", "pass"));
349
+ } catch (error) {
350
+ checks.push(preflightCheck("server", "Rig server reachable", "fail", message(error), "Start or select a reachable Rig server."));
351
+ }
352
+ const repo = readRepoConnection(context.projectRoot);
353
+ checks.push(repo ? preflightCheck("project-link", "project linked to Rig connection", repo.project ? "pass" : "warn", `${repo.selected}${repo.project ? ` -> ${repo.project}` : ""}`, "Run `rig init --yes --repo owner/repo` to record the GitHub repo slug.") : preflightCheck("project-link", "project linked to Rig connection", "fail", "missing .rig/state/connection.json", "Run `rig init` or `rig connect use <alias|local>`."));
354
+ try {
355
+ const auth = await request("/api/github/auth/status");
356
+ checks.push(isAuthenticated(auth) ? preflightCheck("github-auth", "GitHub auth valid", "pass") : preflightCheck("github-auth", "GitHub auth valid", "fail", "not authenticated", "Run `rig github auth import-gh` or `rig github auth token --token <token>`."));
357
+ } catch (error) {
358
+ checks.push(preflightCheck("github-auth", "GitHub auth valid", "fail", message(error), "Fix GitHub auth on the selected Rig server."));
359
+ }
360
+ try {
361
+ const projection = await request("/api/workspace/task-projection");
362
+ checks.push(preflightCheck("task-projection", "task projection ready", "pass", JSON.stringify(projection).slice(0, 120)));
363
+ } catch (error) {
364
+ checks.push(preflightCheck("task-projection", "task projection ready", "warn", message(error), "Refresh task projection with `rig task list` before launching."));
365
+ }
366
+ try {
367
+ const permissions = await request("/api/github/repo/permissions");
368
+ const allowed = permissionAllowsPr(permissions);
369
+ checks.push(allowed === false ? preflightCheck("github-repo-permissions", "GitHub PR permissions", "fail", JSON.stringify(permissions).slice(0, 120), "Grant the selected GitHub token permission to push branches and open PRs.") : preflightCheck("github-repo-permissions", "GitHub PR permissions", allowed === true ? "pass" : "warn", JSON.stringify(permissions).slice(0, 120), "Confirm the selected token can push branches and open PRs."));
370
+ } catch (error) {
371
+ checks.push(preflightCheck("github-repo-permissions", "GitHub PR permissions", "warn", message(error), "Ensure the selected token can push branches and open PRs."));
372
+ }
373
+ if (repo?.project) {
374
+ try {
375
+ const project = await request(`/api/projects/${encodeURIComponent(repo.project)}`);
376
+ const ready = projectCheckoutReady(project);
377
+ checks.push(ready === false ? preflightCheck("remote-checkout", "execution checkout ready", "fail", JSON.stringify(project).slice(0, 120), "Repair the server checkout or rerun `rig init` with a valid checkout strategy.") : preflightCheck("remote-checkout", "execution checkout ready", ready === true ? "pass" : "warn", JSON.stringify(project).slice(0, 120), "Confirm the selected server has a prepared execution checkout."));
378
+ } catch (error) {
379
+ checks.push(preflightCheck("remote-checkout", "execution checkout ready", "warn", message(error), "Run `rig init` or repair the server checkout before launch."));
380
+ }
381
+ }
382
+ if (taskId) {
383
+ try {
384
+ const tasks = await request(`/api/workspace/tasks?limit=200&refresh=1`);
385
+ const found = Array.isArray(tasks) && tasks.some((task) => taskMatchesId(task, taskId));
386
+ checks.push(found ? preflightCheck("issue", "task/issue accessible", "pass", taskId) : preflightCheck("issue", "task/issue accessible", "fail", taskId, "Confirm the issue exists and matches the configured task filters."));
387
+ } catch (error) {
388
+ checks.push(preflightCheck("issue", "task/issue accessible", "fail", message(error), "Fix the task source before launching a run."));
389
+ }
390
+ try {
391
+ const runs = await request("/api/runs?limit=200");
392
+ const duplicate = activeDuplicateRun(runs, taskId);
393
+ checks.push(duplicate ? preflightCheck("duplicate-active-run", "one active run per task", "fail", String(duplicate.runId ?? taskId), "Attach to or stop the existing run before starting another one for this task.") : preflightCheck("duplicate-active-run", "one active run per task", "pass", taskId));
394
+ } catch (error) {
395
+ checks.push(preflightCheck("duplicate-active-run", "one active run per task", "warn", message(error), "Could not list active runs; the server will still reject conflicting runs if configured."));
396
+ }
397
+ }
398
+ if ((options.runtimeAdapter ?? "pi") === "pi") {
399
+ const piChecks = await (options.piChecks ?? (() => buildPiSetupChecks()))().catch((error) => [{
400
+ ok: false,
401
+ label: "pi/pi-rig checks",
402
+ hint: message(error)
403
+ }]);
404
+ for (const pi of piChecks) {
405
+ checks.push(preflightCheck(pi.label === "pi" ? "pi" : "pi-rig", pi.label, pi.ok ? "pass" : "fail", pi.detail, pi.hint ?? (pi.ok ? undefined : "Run `rig init --yes` to install/update Pi and enable pi-rig.")));
406
+ }
407
+ } else {
408
+ checks.push(preflightCheck("runtime", "runtime adapter", "pass", options.runtimeAdapter));
409
+ }
410
+ const failures = checks.filter((check) => check.status === "fail");
411
+ if (failures.length > 0) {
412
+ const summary = failures.map((check) => `${check.label}${check.detail ? `: ${check.detail}` : ""}`).join("; ");
413
+ if (failures.some((check) => check.id === "duplicate-active-run") && taskId) {
414
+ throw new CliError2(`Task ${taskId} already has an active Rig run. ${summary}`, 1);
415
+ }
416
+ throw new CliError2(`Task run preflight failed: ${summary}`, 1);
417
+ }
418
+ return { ok: true, checks };
419
+ }
420
+ async function runProjectMainSyncPreflight(context, options) {
421
+ if (context.dryRun) {
422
+ if (context.outputMode === "text" && !options.disabled) {
423
+ console.log("[dry-run] project-rig pre-run sync check");
424
+ }
425
+ return;
426
+ }
427
+ const result = await ensureProjectMainFreshBeforeRun({
428
+ projectRoot: context.projectRoot,
429
+ disabled: options.disabled,
430
+ runBootstrap: async () => {
431
+ const bootstrap = await context.runCommand(["bun", "run", "bootstrap"]);
432
+ if (bootstrap.exitCode !== 0) {
433
+ throw new CliError2(bootstrap.stderr || bootstrap.stdout || "bun run bootstrap failed during project pre-run sync", bootstrap.exitCode || 1);
434
+ }
435
+ }
436
+ });
437
+ if (context.outputMode !== "text") {
438
+ return;
439
+ }
440
+ switch (result.status) {
441
+ case "disabled":
442
+ console.log("Project pre-run sync skipped (--skip-project-sync).");
443
+ break;
444
+ case "skipped_not_main":
445
+ console.log(`Project pre-run sync skipped (current branch: ${result.branch}).`);
446
+ break;
447
+ case "up_to_date":
448
+ break;
449
+ case "local_ahead":
450
+ console.log(`Project pre-run sync skipped (local main ahead by ${result.localAhead} commit(s)).`);
451
+ break;
452
+ case "updated":
453
+ console.log(`Project pre-run sync updated local main from origin/main (+${result.remoteAhead}) and bootstrapped.`);
454
+ break;
455
+ }
456
+ }
457
+ export {
458
+ runProjectMainSyncPreflight,
459
+ runFastTaskRunPreflight
460
+ };
@@ -0,0 +1,13 @@
1
+ // @bun
2
+ // packages/cli/src/commands/_probes.ts
3
+ async function runQuietBinaryProbe(binaryPath, args, cwd) {
4
+ try {
5
+ const run = await Bun.$`${binaryPath} ${args}`.cwd(cwd).quiet().nothrow();
6
+ return run.exitCode === 0;
7
+ } catch {
8
+ return false;
9
+ }
10
+ }
11
+ export {
12
+ runQuietBinaryProbe
13
+ };