@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,753 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/cli/src/commands/run.ts
|
|
3
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
4
|
+
import { resolve as resolve2 } from "path";
|
|
5
|
+
import { createInterface as createInterface2 } from "readline/promises";
|
|
6
|
+
|
|
7
|
+
// packages/cli/src/runner.ts
|
|
8
|
+
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
9
|
+
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
10
|
+
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
11
|
+
import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
|
|
12
|
+
import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
|
|
13
|
+
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
14
|
+
import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
|
|
15
|
+
function takeFlag(args, flag) {
|
|
16
|
+
const rest = [];
|
|
17
|
+
let value = false;
|
|
18
|
+
for (const arg of args) {
|
|
19
|
+
if (arg === flag) {
|
|
20
|
+
value = true;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
rest.push(arg);
|
|
24
|
+
}
|
|
25
|
+
return { value, rest };
|
|
26
|
+
}
|
|
27
|
+
function takeOption(args, option) {
|
|
28
|
+
const rest = [];
|
|
29
|
+
let value;
|
|
30
|
+
for (let index = 0;index < args.length; index += 1) {
|
|
31
|
+
const current = args[index];
|
|
32
|
+
if (current === option) {
|
|
33
|
+
const next = args[index + 1];
|
|
34
|
+
if (!next || next.startsWith("-")) {
|
|
35
|
+
throw new CliError(`Missing value for ${option}`);
|
|
36
|
+
}
|
|
37
|
+
value = next;
|
|
38
|
+
index += 1;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (current !== undefined) {
|
|
42
|
+
rest.push(current);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { value, rest };
|
|
46
|
+
}
|
|
47
|
+
function requireNoExtraArgs(args, usage) {
|
|
48
|
+
if (args.length > 0) {
|
|
49
|
+
throw new CliError(`Unexpected arguments: ${args.join(" ")}
|
|
50
|
+
Usage: ${usage}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// packages/cli/src/commands/run.ts
|
|
55
|
+
import {
|
|
56
|
+
listAuthorityRuns,
|
|
57
|
+
readAuthorityRun,
|
|
58
|
+
readJsonlFile,
|
|
59
|
+
resolveAuthorityRunDir
|
|
60
|
+
} from "@rig/runtime/control-plane/authority-files";
|
|
61
|
+
import {
|
|
62
|
+
cleanupRunState,
|
|
63
|
+
deleteRunState,
|
|
64
|
+
listOpenEpics,
|
|
65
|
+
resolveDefaultEpic,
|
|
66
|
+
runResume,
|
|
67
|
+
runStatus,
|
|
68
|
+
runStop,
|
|
69
|
+
startRun,
|
|
70
|
+
defaultStartRunOptions
|
|
71
|
+
} from "@rig/runtime/control-plane/native/run-ops";
|
|
72
|
+
import { loadRuntimeContextFromEnv as loadRuntimeContextFromEnv2 } from "@rig/runtime/control-plane/runtime/context";
|
|
73
|
+
|
|
74
|
+
// packages/cli/src/commands/_parsers.ts
|
|
75
|
+
function parsePositiveInt(value, option, fallback) {
|
|
76
|
+
if (!value) {
|
|
77
|
+
return fallback;
|
|
78
|
+
}
|
|
79
|
+
const parsed = Number.parseInt(value, 10);
|
|
80
|
+
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
81
|
+
throw new CliError2(`Invalid ${option} value: ${value}`);
|
|
82
|
+
}
|
|
83
|
+
return parsed;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// packages/cli/src/commands/_server-client.ts
|
|
87
|
+
import { spawnSync } from "child_process";
|
|
88
|
+
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
89
|
+
|
|
90
|
+
// packages/cli/src/commands/_connection-state.ts
|
|
91
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
92
|
+
import { homedir } from "os";
|
|
93
|
+
import { dirname, resolve } from "path";
|
|
94
|
+
function resolveGlobalConnectionsPath(env = process.env) {
|
|
95
|
+
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
96
|
+
if (explicit)
|
|
97
|
+
return resolve(explicit);
|
|
98
|
+
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
99
|
+
if (stateDir)
|
|
100
|
+
return resolve(stateDir, "connections.json");
|
|
101
|
+
return resolve(homedir(), ".rig", "connections.json");
|
|
102
|
+
}
|
|
103
|
+
function resolveRepoConnectionPath(projectRoot) {
|
|
104
|
+
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
105
|
+
}
|
|
106
|
+
function readJsonFile(path) {
|
|
107
|
+
if (!existsSync(path))
|
|
108
|
+
return null;
|
|
109
|
+
try {
|
|
110
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function normalizeConnection(value) {
|
|
116
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
117
|
+
return null;
|
|
118
|
+
const record = value;
|
|
119
|
+
if (record.kind === "local")
|
|
120
|
+
return { kind: "local", mode: "auto" };
|
|
121
|
+
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
122
|
+
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
123
|
+
return { kind: "remote", baseUrl };
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
function readGlobalConnections(options = {}) {
|
|
128
|
+
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
129
|
+
const payload = readJsonFile(path);
|
|
130
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
131
|
+
return { connections: {} };
|
|
132
|
+
}
|
|
133
|
+
const rawConnections = payload.connections;
|
|
134
|
+
const connections = {};
|
|
135
|
+
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
136
|
+
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
137
|
+
const connection = normalizeConnection(raw);
|
|
138
|
+
if (connection)
|
|
139
|
+
connections[alias] = connection;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return { connections };
|
|
143
|
+
}
|
|
144
|
+
function readRepoConnection(projectRoot) {
|
|
145
|
+
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
146
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
147
|
+
return null;
|
|
148
|
+
const record = payload;
|
|
149
|
+
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
150
|
+
if (!selected)
|
|
151
|
+
return null;
|
|
152
|
+
return {
|
|
153
|
+
selected,
|
|
154
|
+
project: typeof record.project === "string" ? record.project : undefined,
|
|
155
|
+
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
159
|
+
const repo = readRepoConnection(projectRoot);
|
|
160
|
+
if (!repo)
|
|
161
|
+
return null;
|
|
162
|
+
if (repo.selected === "local")
|
|
163
|
+
return { alias: "local", connection: { kind: "local", mode: "auto" } };
|
|
164
|
+
const global = readGlobalConnections(options);
|
|
165
|
+
const connection = global.connections[repo.selected];
|
|
166
|
+
if (!connection) {
|
|
167
|
+
throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
|
|
168
|
+
}
|
|
169
|
+
return { alias: repo.selected, connection };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// packages/cli/src/commands/_server-client.ts
|
|
173
|
+
var cachedGitHubBearerToken;
|
|
174
|
+
function cleanToken(value) {
|
|
175
|
+
const trimmed = value?.trim();
|
|
176
|
+
return trimmed ? trimmed : null;
|
|
177
|
+
}
|
|
178
|
+
function readGitHubBearerTokenForRemote() {
|
|
179
|
+
if (cachedGitHubBearerToken !== undefined)
|
|
180
|
+
return cachedGitHubBearerToken;
|
|
181
|
+
const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
|
|
182
|
+
if (envToken) {
|
|
183
|
+
cachedGitHubBearerToken = envToken;
|
|
184
|
+
return cachedGitHubBearerToken;
|
|
185
|
+
}
|
|
186
|
+
const result = spawnSync("gh", ["auth", "token"], {
|
|
187
|
+
encoding: "utf8",
|
|
188
|
+
timeout: 5000,
|
|
189
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
190
|
+
});
|
|
191
|
+
cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
|
|
192
|
+
return cachedGitHubBearerToken;
|
|
193
|
+
}
|
|
194
|
+
async function ensureServerForCli(projectRoot) {
|
|
195
|
+
try {
|
|
196
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
197
|
+
if (selected?.connection.kind === "remote") {
|
|
198
|
+
return {
|
|
199
|
+
baseUrl: selected.connection.baseUrl,
|
|
200
|
+
authToken: readGitHubBearerTokenForRemote(),
|
|
201
|
+
connectionKind: "remote"
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
205
|
+
return {
|
|
206
|
+
baseUrl: connection.baseUrl,
|
|
207
|
+
authToken: connection.authToken,
|
|
208
|
+
connectionKind: "local"
|
|
209
|
+
};
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (error instanceof Error) {
|
|
212
|
+
throw new CliError2(error.message, 1);
|
|
213
|
+
}
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function mergeHeaders(headers, authToken) {
|
|
218
|
+
const merged = new Headers(headers);
|
|
219
|
+
if (authToken) {
|
|
220
|
+
merged.set("authorization", `Bearer ${authToken}`);
|
|
221
|
+
}
|
|
222
|
+
return merged;
|
|
223
|
+
}
|
|
224
|
+
function diagnosticMessage(payload) {
|
|
225
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
226
|
+
return null;
|
|
227
|
+
const record = payload;
|
|
228
|
+
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
229
|
+
const messages = diagnostics.flatMap((entry) => {
|
|
230
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
231
|
+
return [];
|
|
232
|
+
const diagnostic = entry;
|
|
233
|
+
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
234
|
+
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
235
|
+
return message ? [`${kind}: ${message}`] : [];
|
|
236
|
+
});
|
|
237
|
+
return messages.length > 0 ? messages.join("; ") : null;
|
|
238
|
+
}
|
|
239
|
+
async function requestServerJson(context, pathname, init = {}) {
|
|
240
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
241
|
+
const response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
242
|
+
...init,
|
|
243
|
+
headers: mergeHeaders(init.headers, server.authToken)
|
|
244
|
+
});
|
|
245
|
+
const text = await response.text();
|
|
246
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
247
|
+
try {
|
|
248
|
+
return JSON.parse(text);
|
|
249
|
+
} catch {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
})() : null;
|
|
253
|
+
if (!response.ok) {
|
|
254
|
+
const diagnostics = diagnosticMessage(payload);
|
|
255
|
+
const detail = diagnostics ?? (text || response.statusText);
|
|
256
|
+
throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
|
|
257
|
+
}
|
|
258
|
+
return payload;
|
|
259
|
+
}
|
|
260
|
+
async function getRunDetailsViaServer(context, runId) {
|
|
261
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}`);
|
|
262
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
263
|
+
}
|
|
264
|
+
async function getRunLogsViaServer(context, runId, options = {}) {
|
|
265
|
+
const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/logs`);
|
|
266
|
+
if (options.limit !== undefined)
|
|
267
|
+
url.searchParams.set("limit", String(options.limit));
|
|
268
|
+
if (options.cursor)
|
|
269
|
+
url.searchParams.set("cursor", options.cursor);
|
|
270
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
271
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
|
|
272
|
+
}
|
|
273
|
+
async function stopRunViaServer(context, runId) {
|
|
274
|
+
const payload = await requestServerJson(context, "/api/runs/stop", {
|
|
275
|
+
method: "POST",
|
|
276
|
+
headers: { "content-type": "application/json" },
|
|
277
|
+
body: JSON.stringify({ runId })
|
|
278
|
+
});
|
|
279
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true, runId };
|
|
280
|
+
}
|
|
281
|
+
async function steerRunViaServer(context, runId, message) {
|
|
282
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/steer`, {
|
|
283
|
+
method: "POST",
|
|
284
|
+
headers: { "content-type": "application/json" },
|
|
285
|
+
body: JSON.stringify({ message })
|
|
286
|
+
});
|
|
287
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// packages/cli/src/commands/_operator-view.ts
|
|
291
|
+
import { createInterface } from "readline";
|
|
292
|
+
var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
|
|
293
|
+
var CANONICAL_STAGES = [
|
|
294
|
+
"Connect",
|
|
295
|
+
"GitHub/task sync",
|
|
296
|
+
"Prepare workspace",
|
|
297
|
+
"Launch Pi",
|
|
298
|
+
"Plan",
|
|
299
|
+
"Implement",
|
|
300
|
+
"Validate",
|
|
301
|
+
"Commit",
|
|
302
|
+
"Open PR",
|
|
303
|
+
"Review/CI",
|
|
304
|
+
"Merge",
|
|
305
|
+
"Complete"
|
|
306
|
+
];
|
|
307
|
+
function renderOperatorSnapshot(snapshot) {
|
|
308
|
+
const run = snapshot.run.run && typeof snapshot.run.run === "object" ? snapshot.run.run : snapshot.run;
|
|
309
|
+
const runId = String(run.runId ?? run.id ?? "run");
|
|
310
|
+
const status = String(run.status ?? "unknown");
|
|
311
|
+
const logs = snapshot.logs ?? [];
|
|
312
|
+
const stageLines = CANONICAL_STAGES.flatMap((stage) => {
|
|
313
|
+
const match = logs.find((log) => String(log.title ?? "").toLowerCase() === stage.toLowerCase() || String(log.stage ?? "").toLowerCase() === stage.toLowerCase());
|
|
314
|
+
return match ? [`${stage}: ${String(match.status ?? status)}`] : [];
|
|
315
|
+
});
|
|
316
|
+
return [`Rig run ${runId}: ${status}`, ...stageLines].join(`
|
|
317
|
+
`);
|
|
318
|
+
}
|
|
319
|
+
function runStatusFromPayload(payload) {
|
|
320
|
+
const run = payload.run && typeof payload.run === "object" && !Array.isArray(payload.run) ? payload.run : payload;
|
|
321
|
+
return String(run.status ?? "unknown").toLowerCase();
|
|
322
|
+
}
|
|
323
|
+
async function applyOperatorCommand(context, input, deps = {}) {
|
|
324
|
+
const line = input.line.trim();
|
|
325
|
+
if (!line)
|
|
326
|
+
return { action: "ignored" };
|
|
327
|
+
if (line === "/detach" || line === "/quit" || line === "/q") {
|
|
328
|
+
return { action: "detach", message: "Detached from run." };
|
|
329
|
+
}
|
|
330
|
+
if (line === "/stop") {
|
|
331
|
+
await (deps.stop ?? stopRunViaServer)(context, input.runId);
|
|
332
|
+
return { action: "stopped", message: "Stop requested." };
|
|
333
|
+
}
|
|
334
|
+
const userMessage = line.startsWith("/user ") ? line.slice("/user ".length).trim() : line;
|
|
335
|
+
if (!userMessage)
|
|
336
|
+
return { action: "ignored" };
|
|
337
|
+
await (deps.steer ?? steerRunViaServer)(context, input.runId, userMessage);
|
|
338
|
+
return { action: "continue", message: "Steering message queued." };
|
|
339
|
+
}
|
|
340
|
+
async function readOperatorSnapshot(context, runId) {
|
|
341
|
+
const run = await getRunDetailsViaServer(context, runId);
|
|
342
|
+
const logsPage = await getRunLogsViaServer(context, runId, { limit: 100 });
|
|
343
|
+
const entries = Array.isArray(logsPage.entries) ? logsPage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))) : [];
|
|
344
|
+
return { run, logs: entries, rendered: renderOperatorSnapshot({ run, logs: entries }) };
|
|
345
|
+
}
|
|
346
|
+
async function attachRunOperatorView(context, input) {
|
|
347
|
+
let steered = false;
|
|
348
|
+
if (input.message?.trim()) {
|
|
349
|
+
await steerRunViaServer(context, input.runId, input.message.trim());
|
|
350
|
+
steered = true;
|
|
351
|
+
}
|
|
352
|
+
let snapshot = await readOperatorSnapshot(context, input.runId);
|
|
353
|
+
if (context.outputMode === "text") {
|
|
354
|
+
console.log(snapshot.rendered);
|
|
355
|
+
if (steered)
|
|
356
|
+
console.log("Steering message queued.");
|
|
357
|
+
}
|
|
358
|
+
let detached = false;
|
|
359
|
+
let rl = null;
|
|
360
|
+
if (input.follow && !input.once && context.outputMode === "text") {
|
|
361
|
+
if (input.interactive !== false && process.stdin.isTTY) {
|
|
362
|
+
console.log("Controls: /user <message>, /stop, /detach");
|
|
363
|
+
rl = createInterface({ input: process.stdin, output: process.stdout, terminal: false });
|
|
364
|
+
rl.on("line", (line) => {
|
|
365
|
+
applyOperatorCommand(context, { runId: input.runId, line }).then((result) => {
|
|
366
|
+
if (result.message)
|
|
367
|
+
console.log(result.message);
|
|
368
|
+
if (result.action === "detach" || result.action === "stopped") {
|
|
369
|
+
detached = true;
|
|
370
|
+
rl?.close();
|
|
371
|
+
}
|
|
372
|
+
}).catch((error) => console.log(`Operator command failed: ${error instanceof Error ? error.message : String(error)}`));
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
let lastRendered = snapshot.rendered;
|
|
376
|
+
const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 2000));
|
|
377
|
+
while (!detached && !TERMINAL_RUN_STATUSES.has(runStatusFromPayload(snapshot.run))) {
|
|
378
|
+
await Bun.sleep(pollMs);
|
|
379
|
+
snapshot = await readOperatorSnapshot(context, input.runId);
|
|
380
|
+
if (snapshot.rendered !== lastRendered) {
|
|
381
|
+
console.log(snapshot.rendered);
|
|
382
|
+
lastRendered = snapshot.rendered;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
rl?.close();
|
|
386
|
+
}
|
|
387
|
+
return { ...snapshot, steered, detached };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// packages/cli/src/commands/run.ts
|
|
391
|
+
function shouldPromptForEpicSelection(context, command, promptEpic, noEpicPrompt) {
|
|
392
|
+
if (noEpicPrompt) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
if (promptEpic) {
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
if (command !== "start-serial") {
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
return context.outputMode === "text" && process.stdin.isTTY && process.stdout.isTTY;
|
|
402
|
+
}
|
|
403
|
+
async function promptForEpicSelection(projectRoot, command) {
|
|
404
|
+
const epics = listOpenEpics(projectRoot);
|
|
405
|
+
const defaultEpic = await resolveDefaultEpic(projectRoot);
|
|
406
|
+
const options = epics.map((epic) => epic.id);
|
|
407
|
+
if (defaultEpic && !options.includes(defaultEpic)) {
|
|
408
|
+
options.unshift(defaultEpic);
|
|
409
|
+
}
|
|
410
|
+
if (options.length === 0) {
|
|
411
|
+
throw new CliError2("No open epic found. Pass --epic <id>.");
|
|
412
|
+
}
|
|
413
|
+
console.log(`Select epic for run ${command}:`);
|
|
414
|
+
options.forEach((id, index) => {
|
|
415
|
+
const metadata = epics.find((epic) => epic.id === id);
|
|
416
|
+
const details = [
|
|
417
|
+
metadata?.priority !== undefined ? `priority:${metadata.priority}` : "",
|
|
418
|
+
metadata?.createdAt ? `created:${metadata.createdAt.slice(0, 10)}` : "",
|
|
419
|
+
id === defaultEpic ? "default" : ""
|
|
420
|
+
].filter(Boolean).join(" ");
|
|
421
|
+
const suffix = details ? ` (${details})` : "";
|
|
422
|
+
console.log(` ${index + 1}. ${id}${suffix}`);
|
|
423
|
+
});
|
|
424
|
+
const fallback = defaultEpic || options[0];
|
|
425
|
+
const rl = createInterface2({ input: process.stdin, output: process.stdout });
|
|
426
|
+
try {
|
|
427
|
+
while (true) {
|
|
428
|
+
const answer = (await rl.question(`Epic [1-${options.length}] or id (Enter for ${fallback}, q to cancel): `)).trim();
|
|
429
|
+
if (!answer) {
|
|
430
|
+
return fallback ?? options[0];
|
|
431
|
+
}
|
|
432
|
+
if (answer === "q" || answer === "quit") {
|
|
433
|
+
throw new CliError2("Run cancelled by user.");
|
|
434
|
+
}
|
|
435
|
+
if (/^\d+$/.test(answer)) {
|
|
436
|
+
const index = Number.parseInt(answer, 10) - 1;
|
|
437
|
+
if (index >= 0 && index < options.length) {
|
|
438
|
+
return options[index];
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (options.includes(answer)) {
|
|
442
|
+
return answer;
|
|
443
|
+
}
|
|
444
|
+
console.log("Invalid selection. Choose a listed number, exact epic id, or q to cancel.");
|
|
445
|
+
}
|
|
446
|
+
} finally {
|
|
447
|
+
rl.close();
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
async function executeRun(context, args) {
|
|
451
|
+
const [command = "status", ...rest] = args;
|
|
452
|
+
const runtimeContext = loadRuntimeContextFromEnv2() ?? undefined;
|
|
453
|
+
switch (command) {
|
|
454
|
+
case "list": {
|
|
455
|
+
requireNoExtraArgs(rest, "bun run rig run list");
|
|
456
|
+
const runs = listAuthorityRuns(context.projectRoot);
|
|
457
|
+
if (context.outputMode === "text") {
|
|
458
|
+
if (runs.length === 0) {
|
|
459
|
+
console.log("No runs recorded in .rig/runs.");
|
|
460
|
+
} else {
|
|
461
|
+
for (const run of runs) {
|
|
462
|
+
console.log(`- ${run.runId} \xB7 ${run.status} \xB7 ${run.title}`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return { ok: true, group: "run", command, details: { runs } };
|
|
467
|
+
}
|
|
468
|
+
case "delete": {
|
|
469
|
+
let pending = rest;
|
|
470
|
+
const run = takeOption(pending, "--run");
|
|
471
|
+
pending = run.rest;
|
|
472
|
+
const purgeArtifacts = takeFlag(pending, "--purge-artifacts");
|
|
473
|
+
pending = purgeArtifacts.rest;
|
|
474
|
+
requireNoExtraArgs(pending, "bun run rig run delete --run <id> [--purge-artifacts]");
|
|
475
|
+
if (!run.value) {
|
|
476
|
+
throw new CliError2("run delete requires --run <id>.");
|
|
477
|
+
}
|
|
478
|
+
const result = await deleteRunState(context.projectRoot, {
|
|
479
|
+
runId: run.value,
|
|
480
|
+
purgeRuntime: true,
|
|
481
|
+
purgeTaskArtifacts: purgeArtifacts.value
|
|
482
|
+
});
|
|
483
|
+
if (context.outputMode === "text") {
|
|
484
|
+
console.log(`Deleted run ${result.runId}`);
|
|
485
|
+
if (result.runtimeIds.length > 0) {
|
|
486
|
+
console.log(`Cleaned runtimes: ${result.runtimeIds.join(", ")}`);
|
|
487
|
+
}
|
|
488
|
+
if (result.taskArtifactsDeleted) {
|
|
489
|
+
console.log(`Cleared task artifacts for ${result.taskId}`);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return { ok: true, group: "run", command, details: result };
|
|
493
|
+
}
|
|
494
|
+
case "cleanup": {
|
|
495
|
+
let pending = rest;
|
|
496
|
+
const all = takeFlag(pending, "--all");
|
|
497
|
+
pending = all.rest;
|
|
498
|
+
const keepArtifacts = takeFlag(pending, "--keep-artifacts");
|
|
499
|
+
pending = keepArtifacts.rest;
|
|
500
|
+
const keepRuntimes = takeFlag(pending, "--keep-runtimes");
|
|
501
|
+
pending = keepRuntimes.rest;
|
|
502
|
+
const keepQueue = takeFlag(pending, "--keep-queue");
|
|
503
|
+
pending = keepQueue.rest;
|
|
504
|
+
requireNoExtraArgs(pending, "bun run rig run cleanup --all [--keep-artifacts] [--keep-runtimes] [--keep-queue]");
|
|
505
|
+
if (!all.value) {
|
|
506
|
+
throw new CliError2("run cleanup currently requires --all.");
|
|
507
|
+
}
|
|
508
|
+
const result = await cleanupRunState(context.projectRoot, {
|
|
509
|
+
includeArtifacts: !keepArtifacts.value,
|
|
510
|
+
includeRuntimes: !keepRuntimes.value,
|
|
511
|
+
includeQueue: !keepQueue.value
|
|
512
|
+
});
|
|
513
|
+
if (context.outputMode === "text") {
|
|
514
|
+
console.log(`Deleted runs: ${result.runIds.length}`);
|
|
515
|
+
console.log(`Cleaned runtimes: ${result.runtimeIds.length}`);
|
|
516
|
+
console.log(`Artifacts cleared: ${result.artifactsCleared ? "yes" : "no"}`);
|
|
517
|
+
console.log(`Queue cleared: ${result.queueCleared ? "yes" : "no"}`);
|
|
518
|
+
}
|
|
519
|
+
return { ok: true, group: "run", command, details: result };
|
|
520
|
+
}
|
|
521
|
+
case "show": {
|
|
522
|
+
let pending = rest;
|
|
523
|
+
const run = takeOption(pending, "--run");
|
|
524
|
+
pending = run.rest;
|
|
525
|
+
requireNoExtraArgs(pending, "bun run rig run show --run <id>");
|
|
526
|
+
if (!run.value) {
|
|
527
|
+
throw new CliError2("run show requires --run <id>.");
|
|
528
|
+
}
|
|
529
|
+
const record = readAuthorityRun(context.projectRoot, run.value);
|
|
530
|
+
if (!record) {
|
|
531
|
+
throw new CliError2(`Run not found: ${run.value}`, 2);
|
|
532
|
+
}
|
|
533
|
+
if (context.outputMode === "text") {
|
|
534
|
+
console.log(JSON.stringify(record, null, 2));
|
|
535
|
+
}
|
|
536
|
+
return { ok: true, group: "run", command, details: record };
|
|
537
|
+
}
|
|
538
|
+
case "timeline": {
|
|
539
|
+
let pending = rest;
|
|
540
|
+
const run = takeOption(pending, "--run");
|
|
541
|
+
pending = run.rest;
|
|
542
|
+
const follow = takeFlag(pending, "--follow");
|
|
543
|
+
pending = follow.rest;
|
|
544
|
+
requireNoExtraArgs(pending, "bun run rig run timeline --run <id> [--follow]");
|
|
545
|
+
if (!run.value) {
|
|
546
|
+
throw new CliError2("run timeline requires --run <id>.");
|
|
547
|
+
}
|
|
548
|
+
const timelinePath = resolve2(resolveAuthorityRunDir(context.projectRoot, run.value), "timeline.jsonl");
|
|
549
|
+
const printEvents = () => {
|
|
550
|
+
const events2 = readJsonlFile(timelinePath);
|
|
551
|
+
if (context.outputMode === "text") {
|
|
552
|
+
for (const event of events2) {
|
|
553
|
+
console.log(JSON.stringify(event));
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return events2;
|
|
557
|
+
};
|
|
558
|
+
const events = printEvents();
|
|
559
|
+
if (follow.value && context.outputMode === "text") {
|
|
560
|
+
let lastLength = existsSync2(timelinePath) ? readFileSync2(timelinePath, "utf8").length : 0;
|
|
561
|
+
while (true) {
|
|
562
|
+
await Bun.sleep(1000);
|
|
563
|
+
if (!existsSync2(timelinePath))
|
|
564
|
+
continue;
|
|
565
|
+
const next = readFileSync2(timelinePath, "utf8");
|
|
566
|
+
if (next.length <= lastLength)
|
|
567
|
+
continue;
|
|
568
|
+
const delta = next.slice(lastLength);
|
|
569
|
+
lastLength = next.length;
|
|
570
|
+
for (const line of delta.split(/\r?\n/).map((entry) => entry.trim()).filter(Boolean)) {
|
|
571
|
+
console.log(line);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return { ok: true, group: "run", command, details: { runId: run.value, events } };
|
|
576
|
+
}
|
|
577
|
+
case "attach": {
|
|
578
|
+
let pending = rest;
|
|
579
|
+
const runOption = takeOption(pending, "--run");
|
|
580
|
+
pending = runOption.rest;
|
|
581
|
+
const messageOption = takeOption(pending, "--message");
|
|
582
|
+
pending = messageOption.rest;
|
|
583
|
+
const once = takeFlag(pending, "--once");
|
|
584
|
+
pending = once.rest;
|
|
585
|
+
const follow = takeFlag(pending, "--follow");
|
|
586
|
+
pending = follow.rest;
|
|
587
|
+
const pollMs = takeOption(pending, "--poll-ms");
|
|
588
|
+
pending = pollMs.rest;
|
|
589
|
+
const positionalRunId = pending.length > 0 ? pending[0] : undefined;
|
|
590
|
+
const extra = positionalRunId ? pending.slice(1) : pending;
|
|
591
|
+
requireNoExtraArgs(extra, "bun run rig run attach <run-id>|--run <run-id> [--message <text>] [--once|--follow] [--poll-ms <ms>]");
|
|
592
|
+
const runId = runOption.value ?? positionalRunId;
|
|
593
|
+
if (!runId) {
|
|
594
|
+
throw new CliError2("run attach requires a run id.", 2);
|
|
595
|
+
}
|
|
596
|
+
const attached = await attachRunOperatorView(context, {
|
|
597
|
+
runId,
|
|
598
|
+
message: messageOption.value ?? null,
|
|
599
|
+
once: once.value,
|
|
600
|
+
follow: follow.value,
|
|
601
|
+
pollMs: parsePositiveInt(pollMs.value, "--poll-ms", 2000)
|
|
602
|
+
});
|
|
603
|
+
return { ok: true, group: "run", command, details: attached };
|
|
604
|
+
}
|
|
605
|
+
case "status": {
|
|
606
|
+
requireNoExtraArgs(rest, "bun run rig run status");
|
|
607
|
+
if (context.dryRun) {
|
|
608
|
+
if (context.outputMode === "text") {
|
|
609
|
+
console.log("[dry-run] rig run status");
|
|
610
|
+
}
|
|
611
|
+
return { ok: true, group: "run", command };
|
|
612
|
+
}
|
|
613
|
+
const summary = runStatus(context.projectRoot, runtimeContext);
|
|
614
|
+
if (context.outputMode === "text") {
|
|
615
|
+
console.log(`Active runs: ${summary.activeRuns.length}`);
|
|
616
|
+
for (const run of summary.activeRuns) {
|
|
617
|
+
console.log(`- ${run.runId} \xB7 ${run.status} \xB7 ${run.taskId ?? run.title}`);
|
|
618
|
+
}
|
|
619
|
+
if (summary.recentRuns.length > 0) {
|
|
620
|
+
console.log("");
|
|
621
|
+
console.log("Recent runs:");
|
|
622
|
+
for (const run of summary.recentRuns) {
|
|
623
|
+
console.log(`- ${run.runId} \xB7 ${run.status} \xB7 ${run.taskId ?? run.title}`);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return { ok: true, group: "run", command, details: summary };
|
|
628
|
+
}
|
|
629
|
+
case "start":
|
|
630
|
+
case "start-serial":
|
|
631
|
+
case "start-parallel": {
|
|
632
|
+
let pending = rest;
|
|
633
|
+
const epicResult = takeOption(pending, "--epic");
|
|
634
|
+
pending = epicResult.rest;
|
|
635
|
+
const promptEpicResult = takeFlag(pending, "--prompt-epic");
|
|
636
|
+
pending = promptEpicResult.rest;
|
|
637
|
+
const noEpicPromptResult = takeFlag(pending, "--no-epic-prompt");
|
|
638
|
+
pending = noEpicPromptResult.rest;
|
|
639
|
+
const wsPortResult = takeOption(pending, "--ws-port");
|
|
640
|
+
pending = wsPortResult.rest;
|
|
641
|
+
const serverHostResult = takeOption(pending, "--server-host");
|
|
642
|
+
pending = serverHostResult.rest;
|
|
643
|
+
const serverPortResult = takeOption(pending, "--server-port");
|
|
644
|
+
pending = serverPortResult.rest;
|
|
645
|
+
const pollResult = takeOption(pending, "--poll-ms");
|
|
646
|
+
pending = pollResult.rest;
|
|
647
|
+
const noServerResult = takeFlag(pending, "--no-server");
|
|
648
|
+
pending = noServerResult.rest;
|
|
649
|
+
requireNoExtraArgs(pending, "bun run rig run start [--epic <id>] [--prompt-epic|--no-epic-prompt] [--ws-port <n>] [--server-host <host>] [--server-port <n>] [--poll-ms <n>] [--no-server]");
|
|
650
|
+
if (promptEpicResult.value && noEpicPromptResult.value) {
|
|
651
|
+
throw new CliError2("Cannot use --prompt-epic and --no-epic-prompt together.");
|
|
652
|
+
}
|
|
653
|
+
if (promptEpicResult.value && (context.outputMode !== "text" || !process.stdin.isTTY || !process.stdout.isTTY)) {
|
|
654
|
+
throw new CliError2("--prompt-epic requires an interactive terminal (TTY) in text mode.");
|
|
655
|
+
}
|
|
656
|
+
let resolvedEpicId = epicResult.value || undefined;
|
|
657
|
+
if (!resolvedEpicId && shouldPromptForEpicSelection(context, command, promptEpicResult.value, noEpicPromptResult.value)) {
|
|
658
|
+
resolvedEpicId = await promptForEpicSelection(context.projectRoot, command);
|
|
659
|
+
}
|
|
660
|
+
const defaults = defaultStartRunOptions(command === "start-parallel" ? "parallel" : "serial");
|
|
661
|
+
const result = await startRun(context.projectRoot, {
|
|
662
|
+
mode: command === "start-parallel" ? "parallel" : "serial",
|
|
663
|
+
workers: defaults.workers,
|
|
664
|
+
epicId: resolvedEpicId,
|
|
665
|
+
wsPort: parsePositiveInt(wsPortResult.value, "--ws-port", defaults.wsPort),
|
|
666
|
+
startEventServer: noServerResult.value ? false : defaults.startEventServer,
|
|
667
|
+
serverHost: serverHostResult.value || defaults.serverHost,
|
|
668
|
+
serverPort: parsePositiveInt(serverPortResult.value, "--server-port", defaults.serverPort),
|
|
669
|
+
serverPollMs: parsePositiveInt(pollResult.value, "--poll-ms", defaults.serverPollMs),
|
|
670
|
+
dryRun: context.dryRun,
|
|
671
|
+
runtimeContext
|
|
672
|
+
});
|
|
673
|
+
if (context.outputMode === "text") {
|
|
674
|
+
console.log(`Epic: ${result.epicId}`);
|
|
675
|
+
console.log(`Server: ${result.baseUrl}`);
|
|
676
|
+
if (result.eventServerUrl) {
|
|
677
|
+
console.log(`Events: ${result.eventServerUrl}`);
|
|
678
|
+
}
|
|
679
|
+
console.log(`Runs: ${result.runIds.join(", ")}`);
|
|
680
|
+
}
|
|
681
|
+
if (result.exitCode !== 0) {
|
|
682
|
+
throw new CliError2(`run ${command} failed with exit code ${result.exitCode}.`, result.exitCode);
|
|
683
|
+
}
|
|
684
|
+
return {
|
|
685
|
+
ok: true,
|
|
686
|
+
group: "run",
|
|
687
|
+
command,
|
|
688
|
+
details: {
|
|
689
|
+
epicId: result.epicId,
|
|
690
|
+
baseUrl: result.baseUrl,
|
|
691
|
+
runIds: result.runIds,
|
|
692
|
+
eventServerUrl: result.eventServerUrl || null
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
case "resume": {
|
|
697
|
+
requireNoExtraArgs(rest, "bun run rig run resume");
|
|
698
|
+
if (context.dryRun) {
|
|
699
|
+
if (context.outputMode === "text") {
|
|
700
|
+
console.log("[dry-run] rig run resume");
|
|
701
|
+
}
|
|
702
|
+
return { ok: true, group: "run", command };
|
|
703
|
+
}
|
|
704
|
+
const resumed = await runResume(context.projectRoot, runtimeContext);
|
|
705
|
+
if (context.outputMode === "text") {
|
|
706
|
+
console.log(`Resumed run: ${resumed.runId}`);
|
|
707
|
+
}
|
|
708
|
+
return { ok: true, group: "run", command, details: resumed };
|
|
709
|
+
}
|
|
710
|
+
case "stop": {
|
|
711
|
+
const runOption = takeOption(rest, "--run");
|
|
712
|
+
const positionalRunId = runOption.rest.length > 0 ? runOption.rest[0] : undefined;
|
|
713
|
+
const extra = positionalRunId ? runOption.rest.slice(1) : runOption.rest;
|
|
714
|
+
requireNoExtraArgs(extra, "bun run rig run stop [<run-id>|--run <id>]");
|
|
715
|
+
const runId = runOption.value ?? positionalRunId;
|
|
716
|
+
if (context.dryRun) {
|
|
717
|
+
return {
|
|
718
|
+
ok: true,
|
|
719
|
+
group: "run",
|
|
720
|
+
command,
|
|
721
|
+
details: runId ? { runId, accepted: true } : { stopped: 0, remaining: [] }
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
if (runId) {
|
|
725
|
+
const stopped = await stopRunViaServer(context, runId);
|
|
726
|
+
if (context.outputMode === "text")
|
|
727
|
+
console.log(`Stop requested: ${runId}`);
|
|
728
|
+
return { ok: true, group: "run", command, details: stopped };
|
|
729
|
+
}
|
|
730
|
+
const result = await runStop(context.projectRoot);
|
|
731
|
+
if (result.remaining.length > 0) {
|
|
732
|
+
throw new CliError2(`Failed to stop run(s): ${result.remaining.join(", ")}`, 1);
|
|
733
|
+
}
|
|
734
|
+
if (context.outputMode === "text") {
|
|
735
|
+
console.log(`Stopped process count: ${result.stopped}`);
|
|
736
|
+
}
|
|
737
|
+
return {
|
|
738
|
+
ok: true,
|
|
739
|
+
group: "run",
|
|
740
|
+
command,
|
|
741
|
+
details: {
|
|
742
|
+
stopped: result.stopped,
|
|
743
|
+
remaining: result.remaining
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
default:
|
|
748
|
+
throw new CliError2(`Unknown run command: ${command}`);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
export {
|
|
752
|
+
executeRun
|
|
753
|
+
};
|