@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.
- package/README.md +30 -0
- package/dist/bin/build-rig-binaries.js +107 -0
- package/dist/bin/rig.js +9330 -0
- package/dist/src/commands/_authority-runs.js +110 -0
- package/dist/src/commands/_connection-state.js +123 -0
- package/dist/src/commands/_doctor-checks.js +501 -0
- package/dist/src/commands/_operator-view.js +322 -0
- package/dist/src/commands/_parsers.js +107 -0
- package/dist/src/commands/_paths.js +50 -0
- package/dist/src/commands/_pi-install.js +184 -0
- package/dist/src/commands/_policy.js +79 -0
- package/dist/src/commands/_preflight.js +460 -0
- package/dist/src/commands/_probes.js +13 -0
- package/dist/src/commands/_run-driver-helpers.js +289 -0
- package/dist/src/commands/_server-client.js +364 -0
- package/dist/src/commands/_snapshot-upload.js +313 -0
- package/dist/src/commands/_task-picker.js +48 -0
- package/dist/src/commands/agent.js +497 -0
- package/dist/src/commands/browser.js +890 -0
- package/dist/src/commands/connect.js +180 -0
- package/dist/src/commands/dist.js +402 -0
- package/dist/src/commands/doctor.js +511 -0
- package/dist/src/commands/github.js +276 -0
- package/dist/src/commands/inbox.js +160 -0
- package/dist/src/commands/init.js +1254 -0
- package/dist/src/commands/inspect.js +174 -0
- package/dist/src/commands/inspector.js +256 -0
- package/dist/src/commands/plugin.js +167 -0
- package/dist/src/commands/profile-and-review.js +178 -0
- package/dist/src/commands/queue.js +197 -0
- package/dist/src/commands/remote.js +507 -0
- package/dist/src/commands/repo-git-harness.js +221 -0
- package/dist/src/commands/run.js +753 -0
- package/dist/src/commands/server.js +368 -0
- package/dist/src/commands/setup.js +681 -0
- package/dist/src/commands/task-report-bug.js +1083 -0
- package/dist/src/commands/task-run-driver.js +1933 -0
- package/dist/src/commands/task.js +1325 -0
- package/dist/src/commands/test.js +39 -0
- package/dist/src/commands/workspace.js +123 -0
- package/dist/src/commands.js +9012 -0
- package/dist/src/index.js +9348 -0
- package/dist/src/launcher.js +131 -0
- package/dist/src/report-bug.js +260 -0
- package/dist/src/runner.js +272 -0
- package/dist/src/withMutedConsole.js +42 -0
- package/package.json +31 -0
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __require = import.meta.require;
|
|
3
|
+
|
|
4
|
+
// packages/cli/src/commands/setup.ts
|
|
5
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync2, readdirSync, writeFileSync as writeFileSync2 } from "fs";
|
|
6
|
+
import { resolve as resolve5 } from "path";
|
|
7
|
+
|
|
8
|
+
// packages/cli/src/runner.ts
|
|
9
|
+
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
10
|
+
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
11
|
+
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
12
|
+
import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
|
|
13
|
+
import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
|
|
14
|
+
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
15
|
+
import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
|
|
16
|
+
function requireNoExtraArgs(args, usage) {
|
|
17
|
+
if (args.length > 0) {
|
|
18
|
+
throw new CliError(`Unexpected arguments: ${args.join(" ")}
|
|
19
|
+
Usage: ${usage}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// packages/cli/src/commands/setup.ts
|
|
24
|
+
import { createPluginHost } from "@rig/core";
|
|
25
|
+
import {
|
|
26
|
+
isSupportedBunVersion as isSupportedBunVersion2,
|
|
27
|
+
MIN_SUPPORTED_BUN_VERSION as MIN_SUPPORTED_BUN_VERSION2
|
|
28
|
+
} from "@rig/runtime/control-plane/setup-version";
|
|
29
|
+
|
|
30
|
+
// packages/cli/src/withMutedConsole.ts
|
|
31
|
+
function isPromise(value) {
|
|
32
|
+
if (typeof value !== "object" && typeof value !== "function") {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return value !== null && typeof value.then === "function";
|
|
36
|
+
}
|
|
37
|
+
function withMutedConsole(mute, fn) {
|
|
38
|
+
if (!mute) {
|
|
39
|
+
return fn();
|
|
40
|
+
}
|
|
41
|
+
const originalLog = console.log;
|
|
42
|
+
const originalWarn = console.warn;
|
|
43
|
+
const originalInfo = console.info;
|
|
44
|
+
const restore = () => {
|
|
45
|
+
console.log = originalLog;
|
|
46
|
+
console.warn = originalWarn;
|
|
47
|
+
console.info = originalInfo;
|
|
48
|
+
};
|
|
49
|
+
console.log = () => {};
|
|
50
|
+
console.warn = () => {};
|
|
51
|
+
console.info = () => {};
|
|
52
|
+
try {
|
|
53
|
+
const result = fn();
|
|
54
|
+
if (isPromise(result)) {
|
|
55
|
+
return result.finally(restore);
|
|
56
|
+
}
|
|
57
|
+
restore();
|
|
58
|
+
return result;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
restore();
|
|
61
|
+
throw error;
|
|
62
|
+
} finally {
|
|
63
|
+
if (console.log === originalLog) {
|
|
64
|
+
restore();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// packages/cli/src/commands/_parsers.ts
|
|
70
|
+
async function loadRigConfigOrNull(projectRoot) {
|
|
71
|
+
try {
|
|
72
|
+
const { loadConfig } = await import("@rig/core/load-config");
|
|
73
|
+
return await loadConfig(projectRoot);
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// packages/cli/src/commands/_paths.ts
|
|
80
|
+
import { resolve } from "path";
|
|
81
|
+
import { resolveMonorepoRoot } from "@rig/runtime/control-plane/native/utils";
|
|
82
|
+
function resolveControlPlaneMonorepoRoot(projectRoot) {
|
|
83
|
+
return resolveMonorepoRoot(projectRoot);
|
|
84
|
+
}
|
|
85
|
+
function resolveControlPlaneHostStateRoot(projectRoot) {
|
|
86
|
+
return resolve(projectRoot, ".rig");
|
|
87
|
+
}
|
|
88
|
+
function resolveControlPlaneHostStateDir(projectRoot) {
|
|
89
|
+
return resolve(resolveControlPlaneHostStateRoot(projectRoot), "state");
|
|
90
|
+
}
|
|
91
|
+
function resolveControlPlaneHostLogsDir(projectRoot) {
|
|
92
|
+
return resolve(resolveControlPlaneHostStateRoot(projectRoot), "logs");
|
|
93
|
+
}
|
|
94
|
+
function resolveControlPlaneArtifactsDir(projectRoot) {
|
|
95
|
+
return resolve(resolveControlPlaneMonorepoRoot(projectRoot), "artifacts");
|
|
96
|
+
}
|
|
97
|
+
function resolveControlPlaneDefinitionRoot(projectRoot) {
|
|
98
|
+
return resolve(projectRoot, "rig");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// packages/cli/src/commands/_pi-install.ts
|
|
102
|
+
import { existsSync, readFileSync, rmSync } from "fs";
|
|
103
|
+
import { homedir } from "os";
|
|
104
|
+
import { resolve as resolve2 } from "path";
|
|
105
|
+
var PI_RIG_PACKAGE_NAME = "@rig/pi-rig";
|
|
106
|
+
async function defaultCommandRunner(command, options = {}) {
|
|
107
|
+
const proc = Bun.spawn(command, { cwd: options.cwd, stdout: "pipe", stderr: "pipe" });
|
|
108
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
109
|
+
new Response(proc.stdout).text(),
|
|
110
|
+
new Response(proc.stderr).text(),
|
|
111
|
+
proc.exited
|
|
112
|
+
]);
|
|
113
|
+
return { exitCode, stdout, stderr };
|
|
114
|
+
}
|
|
115
|
+
function resolvePiRigExtensionPath(homeDir) {
|
|
116
|
+
return resolve2(homeDir, ".pi", "agent", "extensions", "pi-rig");
|
|
117
|
+
}
|
|
118
|
+
function resolvePiHomeDir(inputHomeDir) {
|
|
119
|
+
return inputHomeDir ?? process.env.RIG_PI_HOME_DIR?.trim() ?? homedir();
|
|
120
|
+
}
|
|
121
|
+
function piListContainsPiRig(output) {
|
|
122
|
+
return output.split(/\r?\n/).some((line) => {
|
|
123
|
+
const normalized = line.trim();
|
|
124
|
+
return normalized.includes(PI_RIG_PACKAGE_NAME) || /(?:^|[\\/])packages[\\/]pi-rig(?:$|\s)/.test(normalized);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async function safeRun(runner, command, options) {
|
|
128
|
+
try {
|
|
129
|
+
return await runner(command, options);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return { exitCode: 1, stdout: "", stderr: error instanceof Error ? error.message : String(error) };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async function checkPiRigInstall(input = {}) {
|
|
135
|
+
const home = resolvePiHomeDir(input.homeDir);
|
|
136
|
+
const extensionPath = resolvePiRigExtensionPath(home);
|
|
137
|
+
if (process.env.RIG_TEST_FAKE_PI_INSTALL === "1") {
|
|
138
|
+
return {
|
|
139
|
+
extensionPath,
|
|
140
|
+
pi: { ok: true, label: "pi", detail: "fake-pi" },
|
|
141
|
+
piRig: { ok: true, label: "pi-rig global extension", detail: extensionPath }
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const exists = input.exists ?? existsSync;
|
|
145
|
+
const runner = input.commandRunner ?? defaultCommandRunner;
|
|
146
|
+
const piResult = await safeRun(runner, ["pi", "--version"]);
|
|
147
|
+
const piListResult = piResult.exitCode === 0 ? await safeRun(runner, ["pi", "list"]) : { exitCode: 1, stdout: "", stderr: "" };
|
|
148
|
+
const listedPiRig = piListResult.exitCode === 0 && piListContainsPiRig(`${piListResult.stdout}
|
|
149
|
+
${piListResult.stderr}`);
|
|
150
|
+
const legacyBridge = exists(resolve2(extensionPath, "index.ts"));
|
|
151
|
+
const hasPiRig = listedPiRig;
|
|
152
|
+
return {
|
|
153
|
+
extensionPath,
|
|
154
|
+
pi: {
|
|
155
|
+
ok: piResult.exitCode === 0,
|
|
156
|
+
label: "pi",
|
|
157
|
+
detail: (piResult.stdout || piResult.stderr).trim() || undefined,
|
|
158
|
+
hint: piResult.exitCode === 0 ? undefined : "Install Pi or run `rig init --yes` to install/update the Pi runtime."
|
|
159
|
+
},
|
|
160
|
+
piRig: {
|
|
161
|
+
ok: hasPiRig,
|
|
162
|
+
label: "pi-rig global extension",
|
|
163
|
+
detail: hasPiRig ? piListResult.stdout.trim() || PI_RIG_PACKAGE_NAME : legacyBridge ? `${extensionPath} (legacy bridge; reinstall required)` : undefined,
|
|
164
|
+
hint: hasPiRig ? undefined : "Run `rig init --yes` to install/enable the global pi-rig package with `pi install`."
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
async function buildPiSetupChecks(input = {}) {
|
|
169
|
+
const status = await checkPiRigInstall(input);
|
|
170
|
+
return [status.pi, status.piRig];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// packages/cli/src/commands/_doctor-checks.ts
|
|
174
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
175
|
+
import { resolve as resolve4 } from "path";
|
|
176
|
+
import { isSupportedBunVersion, MIN_SUPPORTED_BUN_VERSION } from "@rig/runtime/control-plane/setup-version";
|
|
177
|
+
|
|
178
|
+
// packages/cli/src/commands/_connection-state.ts
|
|
179
|
+
import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "fs";
|
|
180
|
+
import { homedir as homedir2 } from "os";
|
|
181
|
+
import { dirname, resolve as resolve3 } from "path";
|
|
182
|
+
function resolveGlobalConnectionsPath(env = process.env) {
|
|
183
|
+
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
184
|
+
if (explicit)
|
|
185
|
+
return resolve3(explicit);
|
|
186
|
+
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
187
|
+
if (stateDir)
|
|
188
|
+
return resolve3(stateDir, "connections.json");
|
|
189
|
+
return resolve3(homedir2(), ".rig", "connections.json");
|
|
190
|
+
}
|
|
191
|
+
function resolveRepoConnectionPath(projectRoot) {
|
|
192
|
+
return resolve3(projectRoot, ".rig", "state", "connection.json");
|
|
193
|
+
}
|
|
194
|
+
function readJsonFile(path) {
|
|
195
|
+
if (!existsSync2(path))
|
|
196
|
+
return null;
|
|
197
|
+
try {
|
|
198
|
+
return JSON.parse(readFileSync2(path, "utf8"));
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function normalizeConnection(value) {
|
|
204
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
205
|
+
return null;
|
|
206
|
+
const record = value;
|
|
207
|
+
if (record.kind === "local")
|
|
208
|
+
return { kind: "local", mode: "auto" };
|
|
209
|
+
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
210
|
+
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
211
|
+
return { kind: "remote", baseUrl };
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
function readGlobalConnections(options = {}) {
|
|
216
|
+
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
217
|
+
const payload = readJsonFile(path);
|
|
218
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
219
|
+
return { connections: {} };
|
|
220
|
+
}
|
|
221
|
+
const rawConnections = payload.connections;
|
|
222
|
+
const connections = {};
|
|
223
|
+
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
224
|
+
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
225
|
+
const connection = normalizeConnection(raw);
|
|
226
|
+
if (connection)
|
|
227
|
+
connections[alias] = connection;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { connections };
|
|
231
|
+
}
|
|
232
|
+
function readRepoConnection(projectRoot) {
|
|
233
|
+
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
234
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
235
|
+
return null;
|
|
236
|
+
const record = payload;
|
|
237
|
+
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
238
|
+
if (!selected)
|
|
239
|
+
return null;
|
|
240
|
+
return {
|
|
241
|
+
selected,
|
|
242
|
+
project: typeof record.project === "string" ? record.project : undefined,
|
|
243
|
+
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
247
|
+
const repo = readRepoConnection(projectRoot);
|
|
248
|
+
if (!repo)
|
|
249
|
+
return null;
|
|
250
|
+
if (repo.selected === "local")
|
|
251
|
+
return { alias: "local", connection: { kind: "local", mode: "auto" } };
|
|
252
|
+
const global = readGlobalConnections(options);
|
|
253
|
+
const connection = global.connections[repo.selected];
|
|
254
|
+
if (!connection) {
|
|
255
|
+
throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
|
|
256
|
+
}
|
|
257
|
+
return { alias: repo.selected, connection };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// packages/cli/src/commands/_server-client.ts
|
|
261
|
+
import { spawnSync } from "child_process";
|
|
262
|
+
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
263
|
+
var cachedGitHubBearerToken;
|
|
264
|
+
function cleanToken(value) {
|
|
265
|
+
const trimmed = value?.trim();
|
|
266
|
+
return trimmed ? trimmed : null;
|
|
267
|
+
}
|
|
268
|
+
function readGitHubBearerTokenForRemote() {
|
|
269
|
+
if (cachedGitHubBearerToken !== undefined)
|
|
270
|
+
return cachedGitHubBearerToken;
|
|
271
|
+
const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
|
|
272
|
+
if (envToken) {
|
|
273
|
+
cachedGitHubBearerToken = envToken;
|
|
274
|
+
return cachedGitHubBearerToken;
|
|
275
|
+
}
|
|
276
|
+
const result = spawnSync("gh", ["auth", "token"], {
|
|
277
|
+
encoding: "utf8",
|
|
278
|
+
timeout: 5000,
|
|
279
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
280
|
+
});
|
|
281
|
+
cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
|
|
282
|
+
return cachedGitHubBearerToken;
|
|
283
|
+
}
|
|
284
|
+
async function ensureServerForCli(projectRoot) {
|
|
285
|
+
try {
|
|
286
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
287
|
+
if (selected?.connection.kind === "remote") {
|
|
288
|
+
return {
|
|
289
|
+
baseUrl: selected.connection.baseUrl,
|
|
290
|
+
authToken: readGitHubBearerTokenForRemote(),
|
|
291
|
+
connectionKind: "remote"
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
295
|
+
return {
|
|
296
|
+
baseUrl: connection.baseUrl,
|
|
297
|
+
authToken: connection.authToken,
|
|
298
|
+
connectionKind: "local"
|
|
299
|
+
};
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (error instanceof Error) {
|
|
302
|
+
throw new CliError2(error.message, 1);
|
|
303
|
+
}
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function mergeHeaders(headers, authToken) {
|
|
308
|
+
const merged = new Headers(headers);
|
|
309
|
+
if (authToken) {
|
|
310
|
+
merged.set("authorization", `Bearer ${authToken}`);
|
|
311
|
+
}
|
|
312
|
+
return merged;
|
|
313
|
+
}
|
|
314
|
+
function diagnosticMessage(payload) {
|
|
315
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
316
|
+
return null;
|
|
317
|
+
const record = payload;
|
|
318
|
+
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
319
|
+
const messages = diagnostics.flatMap((entry) => {
|
|
320
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
321
|
+
return [];
|
|
322
|
+
const diagnostic = entry;
|
|
323
|
+
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
324
|
+
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
325
|
+
return message ? [`${kind}: ${message}`] : [];
|
|
326
|
+
});
|
|
327
|
+
return messages.length > 0 ? messages.join("; ") : null;
|
|
328
|
+
}
|
|
329
|
+
async function requestServerJson(context, pathname, init = {}) {
|
|
330
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
331
|
+
const response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
332
|
+
...init,
|
|
333
|
+
headers: mergeHeaders(init.headers, server.authToken)
|
|
334
|
+
});
|
|
335
|
+
const text = await response.text();
|
|
336
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
337
|
+
try {
|
|
338
|
+
return JSON.parse(text);
|
|
339
|
+
} catch {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
})() : null;
|
|
343
|
+
if (!response.ok) {
|
|
344
|
+
const diagnostics = diagnosticMessage(payload);
|
|
345
|
+
const detail = diagnostics ?? (text || response.statusText);
|
|
346
|
+
throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
|
|
347
|
+
}
|
|
348
|
+
return payload;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// packages/cli/src/commands/_doctor-checks.ts
|
|
352
|
+
function check(id, label, status, detail, remediation) {
|
|
353
|
+
return {
|
|
354
|
+
id,
|
|
355
|
+
label,
|
|
356
|
+
status,
|
|
357
|
+
...detail ? { detail } : {},
|
|
358
|
+
...remediation ? { remediation } : {}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
function errorMessage(error) {
|
|
362
|
+
return error instanceof Error ? error.message : String(error);
|
|
363
|
+
}
|
|
364
|
+
function isAuthenticated(payload) {
|
|
365
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
366
|
+
return false;
|
|
367
|
+
const record = payload;
|
|
368
|
+
return record.signedIn === true || record.authenticated === true || record.status === "authenticated" || record.ok === true && typeof record.login === "string" && record.login.trim().length > 0;
|
|
369
|
+
}
|
|
370
|
+
function repoSlugFromConfig(config) {
|
|
371
|
+
const project = config?.project;
|
|
372
|
+
if (project && typeof project === "object" && !Array.isArray(project)) {
|
|
373
|
+
const record = project;
|
|
374
|
+
if (typeof record.repo === "string" && /^([^/\s]+)\/([^/\s]+)$/.test(record.repo))
|
|
375
|
+
return record.repo;
|
|
376
|
+
if (typeof record.name === "string" && /^([^/\s]+)\/([^/\s]+)$/.test(record.name))
|
|
377
|
+
return record.name;
|
|
378
|
+
}
|
|
379
|
+
const taskSource = config?.taskSource;
|
|
380
|
+
if (taskSource && typeof taskSource === "object" && !Array.isArray(taskSource)) {
|
|
381
|
+
const source = taskSource;
|
|
382
|
+
if (typeof source.owner === "string" && typeof source.repo === "string")
|
|
383
|
+
return `${source.owner}/${source.repo}`;
|
|
384
|
+
}
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
function loadFallbackConfig(projectRoot) {
|
|
388
|
+
const candidates = ["rig.config.ts", "rig.config.mts", "rig.config.json"];
|
|
389
|
+
for (const name of candidates) {
|
|
390
|
+
const path = resolve4(projectRoot, name);
|
|
391
|
+
if (!existsSync3(path))
|
|
392
|
+
continue;
|
|
393
|
+
try {
|
|
394
|
+
const source = readFileSync3(path, "utf8");
|
|
395
|
+
if (name.endsWith(".json"))
|
|
396
|
+
return JSON.parse(source);
|
|
397
|
+
const owner = source.match(/owner\s*:\s*["']([^"']+)["']/)?.[1];
|
|
398
|
+
const repo = source.match(/repo\s*:\s*["']([^"']+)["']/)?.[1];
|
|
399
|
+
const projectRepo = source.match(/project\s*:\s*\{[^}]*repo\s*:\s*["']([^"']+)["']/s)?.[1] ?? (owner && repo ? `${owner}/${repo}` : undefined);
|
|
400
|
+
const taskKind = source.match(/taskSource\s*:\s*\{[^}]*kind\s*:\s*["']([^"']+)["']/s)?.[1];
|
|
401
|
+
if (projectRepo || taskKind) {
|
|
402
|
+
return {
|
|
403
|
+
...projectRepo ? { project: { name: projectRepo, repo: projectRepo } } : {},
|
|
404
|
+
...taskKind ? { taskSource: { kind: taskKind, ...owner ? { owner } : {}, ...repo ? { repo } : {} } } : {}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
} catch {
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
function projectStatusSlug(projectRoot, config) {
|
|
414
|
+
return readRepoConnection(projectRoot)?.project ?? repoSlugFromConfig(config);
|
|
415
|
+
}
|
|
416
|
+
function githubProjectsCheck(config) {
|
|
417
|
+
const github = config?.github;
|
|
418
|
+
const projects = github?.projects;
|
|
419
|
+
if (!projects?.enabled) {
|
|
420
|
+
return check("github-projects", "GitHub Projects status sync", "warn", "disabled or not configured", "Run `rig init --github-project <project>` or configure github.projects when Project status sync should be authoritative.");
|
|
421
|
+
}
|
|
422
|
+
if (projects.projectId && projects.statusFieldId) {
|
|
423
|
+
return check("github-projects", "GitHub Projects status sync", "pass", `project ${projects.projectId}`);
|
|
424
|
+
}
|
|
425
|
+
return check("github-projects", "GitHub Projects status sync", "fail", "enabled but projectId/statusFieldId is incomplete", "Configure github.projects.projectId and github.projects.statusFieldId, or disable github.projects.enabled.");
|
|
426
|
+
}
|
|
427
|
+
function permissionAllowsPr(payload) {
|
|
428
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
429
|
+
return null;
|
|
430
|
+
const record = payload;
|
|
431
|
+
if (record.canOpenPullRequest === true || record.pullRequests === true || record.push === true || record.maintain === true || record.admin === true)
|
|
432
|
+
return true;
|
|
433
|
+
if (record.canOpenPullRequest === false || record.pullRequests === false || record.push === false)
|
|
434
|
+
return false;
|
|
435
|
+
const permissions = record.permissions;
|
|
436
|
+
if (permissions && typeof permissions === "object" && !Array.isArray(permissions)) {
|
|
437
|
+
const p = permissions;
|
|
438
|
+
if (p.push === true || p.maintain === true || p.admin === true)
|
|
439
|
+
return true;
|
|
440
|
+
if (p.push === false && p.maintain !== true && p.admin !== true)
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
function labelsReady(payload) {
|
|
446
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
447
|
+
return null;
|
|
448
|
+
const record = payload;
|
|
449
|
+
if (record.ok === true || record.ready === true || record.labelsReady === true)
|
|
450
|
+
return true;
|
|
451
|
+
if (record.ok === false || record.ready === false || record.labelsReady === false)
|
|
452
|
+
return false;
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
function prMergeCheck(config) {
|
|
456
|
+
const pr = config?.pr;
|
|
457
|
+
const merge = config?.merge;
|
|
458
|
+
if (pr?.mode === "off" || merge?.mode === "off") {
|
|
459
|
+
return check("pr-merge", "PR/merge automation", "warn", "automatic PR or merge is disabled", "Set pr.mode and merge.mode to auto for autonomous YOLO runs.");
|
|
460
|
+
}
|
|
461
|
+
return check("pr-merge", "PR/merge automation", "pass", `pr=${pr?.mode ?? "auto"}, merge=${merge?.mode ?? "auto"}, method=${merge?.method ?? "repo-default"}`);
|
|
462
|
+
}
|
|
463
|
+
async function runRigDoctorChecks(options) {
|
|
464
|
+
const projectRoot = options.projectRoot;
|
|
465
|
+
const checks = [];
|
|
466
|
+
const which = options.which ?? ((binary) => Bun.which(binary));
|
|
467
|
+
const bunVersion = options.bunVersion ?? Bun.version;
|
|
468
|
+
const request = options.requestJson ?? ((pathname, init) => requestServerJson({ projectRoot }, pathname, init));
|
|
469
|
+
const loadConfig = options.loadConfig ?? loadRigConfigOrNull;
|
|
470
|
+
checks.push(check("bun", `bun >= ${MIN_SUPPORTED_BUN_VERSION}`, isSupportedBunVersion(bunVersion) ? "pass" : "fail", `found ${bunVersion}`, `Install Bun ${MIN_SUPPORTED_BUN_VERSION} or newer.`), check("git", "git", which("git") ? "pass" : "fail", which("git") ?? undefined, "Install git and ensure it is on PATH."), check("jq", "jq", which("jq") ? "pass" : "warn", which("jq") ?? undefined, "Install jq (for example `brew install jq`)."));
|
|
471
|
+
const loadedConfig = await loadConfig(projectRoot).catch(() => null);
|
|
472
|
+
const config = loadedConfig ?? loadFallbackConfig(projectRoot);
|
|
473
|
+
const hasConfigFile = ["rig.config.ts", "rig.config.mts", "rig.config.json"].some((name) => existsSync3(resolve4(projectRoot, name)));
|
|
474
|
+
checks.push(config ? check("config", "rig.config loadable", "pass") : check("config", "rig.config loadable", hasConfigFile ? "fail" : "fail", hasConfigFile ? "config file exists but failed to load" : "missing rig.config.ts/json", "Run `rig init` or fix the config error."));
|
|
475
|
+
const taskSourceKind = config?.taskSource?.kind;
|
|
476
|
+
checks.push(taskSourceKind ? check("task-source", "task source configured", "pass", taskSourceKind) : check("task-source", "task source configured", "fail", "missing taskSource", "Configure taskSource in rig.config.ts."));
|
|
477
|
+
const repo = readRepoConnection(projectRoot);
|
|
478
|
+
checks.push(repo ? check("project-link", "repo selected Rig connection", repo.project ? "pass" : "warn", `${repo.selected}${repo.project ? ` -> ${repo.project}` : ""}`, "Run `rig init --yes --repo owner/repo` to link this checkout to a GitHub repo slug.") : check("project-link", "repo selected Rig connection", "fail", "missing .rig/state/connection.json", "Run `rig init` or `rig connect use <alias|local>`."));
|
|
479
|
+
const selected = (() => {
|
|
480
|
+
try {
|
|
481
|
+
return resolveSelectedConnection(projectRoot);
|
|
482
|
+
} catch {
|
|
483
|
+
return null;
|
|
484
|
+
}
|
|
485
|
+
})();
|
|
486
|
+
checks.push(selected ? check("connection", "selected server connection", "pass", selected.connection.kind === "remote" ? selected.connection.baseUrl : "local auto") : check("connection", "selected server connection", repo ? "fail" : "warn", repo ? "selected alias is missing" : "will auto-start local server", repo ? "Run `rig connect list` and `rig connect use <alias|local>`." : undefined));
|
|
487
|
+
let server = null;
|
|
488
|
+
try {
|
|
489
|
+
server = await (options.resolveServer ?? ensureServerForCli)(projectRoot);
|
|
490
|
+
checks.push(check("server", "Rig server reachable", "pass", `${server.connectionKind} ${server.baseUrl}`));
|
|
491
|
+
} catch (error) {
|
|
492
|
+
checks.push(check("server", "Rig server reachable", "fail", errorMessage(error), "Start the local Rig server or fix the selected remote connection."));
|
|
493
|
+
}
|
|
494
|
+
if (server || options.requestJson) {
|
|
495
|
+
try {
|
|
496
|
+
const status = await request("/api/server/status");
|
|
497
|
+
checks.push(check("server-status", "server project status", "pass", JSON.stringify(status).slice(0, 180)));
|
|
498
|
+
} catch (error) {
|
|
499
|
+
checks.push(check("server-status", "server project status", "fail", errorMessage(error), "Run `rig doctor` after the selected server is reachable."));
|
|
500
|
+
}
|
|
501
|
+
try {
|
|
502
|
+
const auth = await request("/api/github/auth/status");
|
|
503
|
+
checks.push(isAuthenticated(auth) ? check("github-auth", "GitHub auth", "pass") : check("github-auth", "GitHub auth", "fail", "not authenticated", "Run `rig github auth import-gh` or `rig github auth token --token <token>`."));
|
|
504
|
+
} catch (error) {
|
|
505
|
+
checks.push(check("github-auth", "GitHub auth", "fail", errorMessage(error), "Authenticate GitHub through Rig and ensure the server exposes auth status."));
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
const permissions = await request("/api/github/repo/permissions");
|
|
509
|
+
const allowed = permissionAllowsPr(permissions);
|
|
510
|
+
checks.push(allowed === true ? check("github-repo-permissions", "GitHub repo PR permissions", "pass", JSON.stringify(permissions).slice(0, 180)) : allowed === false ? check("github-repo-permissions", "GitHub repo PR permissions", "fail", JSON.stringify(permissions).slice(0, 180), "Grant the selected GitHub token permission to push branches, open PRs, and merge according to repo rules.") : check("github-repo-permissions", "GitHub repo PR permissions", "warn", JSON.stringify(permissions).slice(0, 180), "Confirm the selected token can push branches and open PRs."));
|
|
511
|
+
} catch (error) {
|
|
512
|
+
checks.push(check("github-repo-permissions", "GitHub repo PR permissions", "warn", errorMessage(error), "Ensure the server exposes repo permission checks and the token can open PRs."));
|
|
513
|
+
}
|
|
514
|
+
try {
|
|
515
|
+
const labels = await request("/api/workspace/task-labels");
|
|
516
|
+
const ready = labelsReady(labels);
|
|
517
|
+
checks.push(ready === false ? check("task-labels", "GitHub issue labels", "fail", JSON.stringify(labels).slice(0, 180), "Let Rig create required labels or create the configured lifecycle labels manually.") : check("task-labels", "GitHub issue labels", ready === true ? "pass" : "warn", JSON.stringify(labels).slice(0, 180), "Confirm required Rig lifecycle labels exist."));
|
|
518
|
+
} catch (error) {
|
|
519
|
+
checks.push(check("task-labels", "GitHub issue labels", "warn", errorMessage(error), "Run `rig init`/`rig doctor` after label setup is wired on the server."));
|
|
520
|
+
}
|
|
521
|
+
try {
|
|
522
|
+
const projection = await request("/api/workspace/task-projection");
|
|
523
|
+
checks.push(check("task-projection", "task projection", "pass", JSON.stringify(projection).slice(0, 180)));
|
|
524
|
+
} catch (error) {
|
|
525
|
+
checks.push(check("task-projection", "task projection", "warn", errorMessage(error), "Refresh task projection with `rig task list` or fix the task source."));
|
|
526
|
+
}
|
|
527
|
+
const slug = projectStatusSlug(projectRoot, config);
|
|
528
|
+
if (slug) {
|
|
529
|
+
try {
|
|
530
|
+
const project = await request(`/api/projects/${encodeURIComponent(slug)}`);
|
|
531
|
+
checks.push(check("remote-checkout", "server project checkout", "pass", JSON.stringify(project).slice(0, 180)));
|
|
532
|
+
} catch (error) {
|
|
533
|
+
checks.push(check("remote-checkout", "server project checkout", "warn", errorMessage(error), "Run `rig init --yes --repo owner/repo` to register/link the server project checkout."));
|
|
534
|
+
}
|
|
535
|
+
} else {
|
|
536
|
+
checks.push(check("remote-checkout", "server project checkout", "warn", "repo slug unknown", "Set project.repo or run `rig init --repo owner/repo`."));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (taskSourceKind === "github-issues") {
|
|
540
|
+
checks.push(check("gh", "gh CLI fallback", which("gh") ? "pass" : "warn", which("gh") ?? undefined, "Install gh for local/dev GitHub fallback operations."));
|
|
541
|
+
}
|
|
542
|
+
checks.push(githubProjectsCheck(config));
|
|
543
|
+
checks.push(prMergeCheck(config));
|
|
544
|
+
const piChecks = await (options.piChecks ?? (() => buildPiSetupChecks()))().catch((error) => [{
|
|
545
|
+
ok: false,
|
|
546
|
+
label: "pi/pi-rig checks",
|
|
547
|
+
hint: errorMessage(error)
|
|
548
|
+
}]);
|
|
549
|
+
for (const pi of piChecks) {
|
|
550
|
+
checks.push(check(pi.label === "pi" ? "pi" : "pi-rig", pi.label, pi.ok ? "pass" : "warn", pi.detail, pi.hint ?? (pi.ok ? undefined : "Run `rig init --yes` to install/update Pi and enable pi-rig.")));
|
|
551
|
+
}
|
|
552
|
+
return checks;
|
|
553
|
+
}
|
|
554
|
+
function formatDoctorChecks(checks) {
|
|
555
|
+
return checks.map((entry) => {
|
|
556
|
+
const prefix = entry.status.toUpperCase();
|
|
557
|
+
const detail = entry.detail ? ` \u2014 ${entry.detail}` : "";
|
|
558
|
+
const remediation = entry.remediation && entry.status !== "pass" ? ` (${entry.remediation})` : "";
|
|
559
|
+
return `${prefix}: ${entry.label}${detail}${remediation}`;
|
|
560
|
+
}).join(`
|
|
561
|
+
`);
|
|
562
|
+
}
|
|
563
|
+
function countDoctorFailures(checks) {
|
|
564
|
+
return checks.filter((entry) => entry.status === "fail").length;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// packages/cli/src/commands/setup.ts
|
|
568
|
+
async function executeSetup(context, args) {
|
|
569
|
+
const [command = "check", ...rest] = args;
|
|
570
|
+
switch (command) {
|
|
571
|
+
case "bootstrap":
|
|
572
|
+
requireNoExtraArgs(rest, "bun run rig setup bootstrap");
|
|
573
|
+
{
|
|
574
|
+
const hostBash = Bun.which("bash") || "/bin/bash";
|
|
575
|
+
const env = { ...process.env };
|
|
576
|
+
delete env.BASH;
|
|
577
|
+
delete env.RIG_BASH_MODE;
|
|
578
|
+
delete env.RIG_TASK_ID;
|
|
579
|
+
delete env.RIG_TASK_RUNTIME_ID;
|
|
580
|
+
delete env.RIG_TASK_WORKSPACE;
|
|
581
|
+
const proc = Bun.spawn([hostBash, "./bootstrap.sh"], {
|
|
582
|
+
cwd: context.projectRoot,
|
|
583
|
+
env,
|
|
584
|
+
stdin: "inherit",
|
|
585
|
+
stdout: "inherit",
|
|
586
|
+
stderr: "inherit"
|
|
587
|
+
});
|
|
588
|
+
const exitCode = await proc.exited;
|
|
589
|
+
if (exitCode !== 0) {
|
|
590
|
+
throw new CliError2(`Command failed (${exitCode}): ${hostBash} ./bootstrap.sh`, exitCode);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return { ok: true, group: "setup", command };
|
|
594
|
+
case "check":
|
|
595
|
+
requireNoExtraArgs(rest, `bun run rig setup ${command}`);
|
|
596
|
+
{
|
|
597
|
+
const checks = await withMutedConsole(context.outputMode === "json", () => runSetupCheck(context.projectRoot));
|
|
598
|
+
return { ok: true, group: "setup", command, details: { checks, failures: countDoctorFailures(checks) } };
|
|
599
|
+
}
|
|
600
|
+
case "setup":
|
|
601
|
+
requireNoExtraArgs(rest, "bun run rig setup setup");
|
|
602
|
+
withMutedConsole(context.outputMode === "json", () => runSetupInit(context.projectRoot));
|
|
603
|
+
return { ok: true, group: "setup", command };
|
|
604
|
+
case "preflight":
|
|
605
|
+
requireNoExtraArgs(rest, "bun run rig setup preflight");
|
|
606
|
+
await withMutedConsole(context.outputMode === "json", () => runSetupPreflight(context.projectRoot));
|
|
607
|
+
return { ok: true, group: "setup", command };
|
|
608
|
+
case "install-agent-shell":
|
|
609
|
+
requireNoExtraArgs(rest, "bun run rig setup install-agent-shell");
|
|
610
|
+
if (context.outputMode === "text") {
|
|
611
|
+
console.log("install-agent-shell is deprecated. Runtime shells now use compiled rig-agent directly.");
|
|
612
|
+
}
|
|
613
|
+
return { ok: true, group: "setup", command };
|
|
614
|
+
default:
|
|
615
|
+
throw new CliError2(`Unknown setup command: ${command}`);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function formatTaskSourceKinds(kinds) {
|
|
619
|
+
return kinds.length > 0 ? kinds.join(", ") : "none";
|
|
620
|
+
}
|
|
621
|
+
function buildTaskSourceKindSetupCheck(config) {
|
|
622
|
+
if (!config.taskSource)
|
|
623
|
+
return null;
|
|
624
|
+
const kind = config.taskSource.kind;
|
|
625
|
+
const pluginHost = createPluginHost(config.plugins ?? []);
|
|
626
|
+
const registeredKinds = pluginHost.listExecutableTaskSources().map((source) => source.kind);
|
|
627
|
+
const isRegistered = Boolean(pluginHost.resolveTaskSourceFactoryByKind(kind));
|
|
628
|
+
return {
|
|
629
|
+
ok: isRegistered,
|
|
630
|
+
label: `task source kind "${kind}" registered`,
|
|
631
|
+
hint: isRegistered ? undefined : `registered kinds: ${formatTaskSourceKinds(registeredKinds)}; load a plugin that contributes an executable task source factory for "${kind}"`
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
function runSetupInit(projectRoot) {
|
|
635
|
+
const stateDir = resolveControlPlaneHostStateDir(projectRoot);
|
|
636
|
+
const logsDir = resolveControlPlaneHostLogsDir(projectRoot);
|
|
637
|
+
const artifactsDir = resolveControlPlaneArtifactsDir(projectRoot);
|
|
638
|
+
mkdirSync2(stateDir, { recursive: true });
|
|
639
|
+
mkdirSync2(logsDir, { recursive: true });
|
|
640
|
+
mkdirSync2(artifactsDir, { recursive: true });
|
|
641
|
+
const failuresPath = resolve5(stateDir, "failed_approaches.md");
|
|
642
|
+
if (!existsSync4(failuresPath)) {
|
|
643
|
+
writeFileSync2(failuresPath, `# Failed Approaches
|
|
644
|
+
|
|
645
|
+
`, "utf-8");
|
|
646
|
+
}
|
|
647
|
+
console.log("Harness directories ready.");
|
|
648
|
+
}
|
|
649
|
+
async function runSetupCheck(projectRoot) {
|
|
650
|
+
const doctorChecks = await runRigDoctorChecks({ projectRoot });
|
|
651
|
+
console.log(formatDoctorChecks(doctorChecks));
|
|
652
|
+
const failures = countDoctorFailures(doctorChecks);
|
|
653
|
+
if (failures > 0) {
|
|
654
|
+
throw new CliError2(`Setup check failed (${failures} failing doctor check${failures === 1 ? "" : "s"}).`, 1);
|
|
655
|
+
}
|
|
656
|
+
return doctorChecks;
|
|
657
|
+
}
|
|
658
|
+
async function runSetupPreflight(projectRoot) {
|
|
659
|
+
await runSetupCheck(projectRoot);
|
|
660
|
+
const validationRoot = resolve5(resolveControlPlaneDefinitionRoot(projectRoot), "validation");
|
|
661
|
+
if (existsSync4(validationRoot)) {
|
|
662
|
+
const validators = readdirSync(validationRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory());
|
|
663
|
+
for (const validator of validators) {
|
|
664
|
+
const script = resolve5(validationRoot, validator.name, "validate.sh");
|
|
665
|
+
if (existsSync4(script)) {
|
|
666
|
+
console.log(`OK: validator script ${script}`);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
const hooksRoot = resolve5(resolveControlPlaneDefinitionRoot(projectRoot), "hooks");
|
|
671
|
+
if (existsSync4(hooksRoot)) {
|
|
672
|
+
const hooks = readdirSync(hooksRoot).filter((name) => name.endsWith(".sh"));
|
|
673
|
+
for (const hook of hooks) {
|
|
674
|
+
console.log(`OK: hook ${hook}`);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
export {
|
|
679
|
+
executeSetup,
|
|
680
|
+
buildTaskSourceKindSetupCheck
|
|
681
|
+
};
|