@h-rig/cli 0.0.6-alpha.19 → 0.0.6-alpha.190
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 +7 -28
- package/bin/rig-run.js +19 -0
- package/bin/rig.js +19 -0
- package/package.json +15 -27
- package/dist/bin/build-rig-binaries.js +0 -107
- package/dist/bin/rig.js +0 -10233
- package/dist/src/commands/_authority-runs.js +0 -111
- package/dist/src/commands/_connection-state.js +0 -123
- package/dist/src/commands/_doctor-checks.js +0 -506
- package/dist/src/commands/_operator-surface.js +0 -204
- package/dist/src/commands/_operator-view.js +0 -496
- package/dist/src/commands/_parsers.js +0 -107
- package/dist/src/commands/_paths.js +0 -50
- package/dist/src/commands/_pi-install.js +0 -184
- package/dist/src/commands/_policy.js +0 -79
- package/dist/src/commands/_preflight.js +0 -482
- package/dist/src/commands/_probes.js +0 -13
- package/dist/src/commands/_run-driver-helpers.js +0 -289
- package/dist/src/commands/_server-client.js +0 -435
- package/dist/src/commands/_snapshot-upload.js +0 -318
- package/dist/src/commands/_task-picker.js +0 -56
- package/dist/src/commands/agent.js +0 -499
- package/dist/src/commands/browser.js +0 -890
- package/dist/src/commands/connect.js +0 -180
- package/dist/src/commands/dist.js +0 -402
- package/dist/src/commands/doctor.js +0 -516
- package/dist/src/commands/github.js +0 -281
- package/dist/src/commands/inbox.js +0 -160
- package/dist/src/commands/init.js +0 -1562
- package/dist/src/commands/inspect.js +0 -174
- package/dist/src/commands/inspector.js +0 -256
- package/dist/src/commands/plugin.js +0 -167
- package/dist/src/commands/profile-and-review.js +0 -178
- package/dist/src/commands/queue.js +0 -198
- package/dist/src/commands/remote.js +0 -507
- package/dist/src/commands/repo-git-harness.js +0 -221
- package/dist/src/commands/run.js +0 -972
- package/dist/src/commands/server.js +0 -373
- package/dist/src/commands/setup.js +0 -686
- package/dist/src/commands/task-report-bug.js +0 -1083
- package/dist/src/commands/task-run-driver.js +0 -2469
- package/dist/src/commands/task.js +0 -1523
- package/dist/src/commands/test.js +0 -39
- package/dist/src/commands/workspace.js +0 -123
- package/dist/src/commands.js +0 -9912
- package/dist/src/index.js +0 -10251
- package/dist/src/launcher.js +0 -133
- package/dist/src/report-bug.js +0 -260
- package/dist/src/runner.js +0 -273
- package/dist/src/withMutedConsole.js +0 -42
package/dist/src/commands/run.js
DELETED
|
@@ -1,972 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
// packages/cli/src/commands/run.ts
|
|
3
|
-
import { createInterface as createInterface2 } from "readline/promises";
|
|
4
|
-
|
|
5
|
-
// packages/cli/src/runner.ts
|
|
6
|
-
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
7
|
-
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
8
|
-
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
9
|
-
import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
|
|
10
|
-
import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
|
|
11
|
-
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
12
|
-
import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
|
|
13
|
-
function takeFlag(args, flag) {
|
|
14
|
-
const rest = [];
|
|
15
|
-
let value = false;
|
|
16
|
-
for (const arg of args) {
|
|
17
|
-
if (arg === flag) {
|
|
18
|
-
value = true;
|
|
19
|
-
continue;
|
|
20
|
-
}
|
|
21
|
-
rest.push(arg);
|
|
22
|
-
}
|
|
23
|
-
return { value, rest };
|
|
24
|
-
}
|
|
25
|
-
function takeOption(args, option) {
|
|
26
|
-
const rest = [];
|
|
27
|
-
let value;
|
|
28
|
-
for (let index = 0;index < args.length; index += 1) {
|
|
29
|
-
const current = args[index];
|
|
30
|
-
if (current === option) {
|
|
31
|
-
const next = args[index + 1];
|
|
32
|
-
if (!next || next.startsWith("-")) {
|
|
33
|
-
throw new CliError(`Missing value for ${option}`);
|
|
34
|
-
}
|
|
35
|
-
value = next;
|
|
36
|
-
index += 1;
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (current !== undefined) {
|
|
40
|
-
rest.push(current);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return { value, rest };
|
|
44
|
-
}
|
|
45
|
-
function requireNoExtraArgs(args, usage) {
|
|
46
|
-
if (args.length > 0) {
|
|
47
|
-
throw new CliError(`Unexpected arguments: ${args.join(" ")}
|
|
48
|
-
Usage: ${usage}`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// packages/cli/src/commands/run.ts
|
|
53
|
-
import {
|
|
54
|
-
listAuthorityRuns,
|
|
55
|
-
readAuthorityRun
|
|
56
|
-
} from "@rig/runtime/control-plane/authority-files";
|
|
57
|
-
import {
|
|
58
|
-
cleanupRunState,
|
|
59
|
-
deleteRunState,
|
|
60
|
-
listOpenEpics,
|
|
61
|
-
resolveDefaultEpic,
|
|
62
|
-
runResume,
|
|
63
|
-
runRestart,
|
|
64
|
-
runStatus,
|
|
65
|
-
runStop,
|
|
66
|
-
startRun,
|
|
67
|
-
defaultStartRunOptions
|
|
68
|
-
} from "@rig/runtime/control-plane/native/run-ops";
|
|
69
|
-
import { loadRuntimeContextFromEnv as loadRuntimeContextFromEnv2 } from "@rig/runtime/control-plane/runtime/context";
|
|
70
|
-
|
|
71
|
-
// packages/cli/src/commands/_parsers.ts
|
|
72
|
-
function parsePositiveInt(value, option, fallback) {
|
|
73
|
-
if (!value) {
|
|
74
|
-
return fallback;
|
|
75
|
-
}
|
|
76
|
-
const parsed = Number.parseInt(value, 10);
|
|
77
|
-
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
78
|
-
throw new CliError2(`Invalid ${option} value: ${value}`);
|
|
79
|
-
}
|
|
80
|
-
return parsed;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// packages/cli/src/commands/_server-client.ts
|
|
84
|
-
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
85
|
-
import { resolve as resolve2 } from "path";
|
|
86
|
-
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
87
|
-
|
|
88
|
-
// packages/cli/src/commands/_connection-state.ts
|
|
89
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
90
|
-
import { homedir } from "os";
|
|
91
|
-
import { dirname, resolve } from "path";
|
|
92
|
-
function resolveGlobalConnectionsPath(env = process.env) {
|
|
93
|
-
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
94
|
-
if (explicit)
|
|
95
|
-
return resolve(explicit);
|
|
96
|
-
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
97
|
-
if (stateDir)
|
|
98
|
-
return resolve(stateDir, "connections.json");
|
|
99
|
-
return resolve(homedir(), ".rig", "connections.json");
|
|
100
|
-
}
|
|
101
|
-
function resolveRepoConnectionPath(projectRoot) {
|
|
102
|
-
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
103
|
-
}
|
|
104
|
-
function readJsonFile(path) {
|
|
105
|
-
if (!existsSync(path))
|
|
106
|
-
return null;
|
|
107
|
-
try {
|
|
108
|
-
return JSON.parse(readFileSync(path, "utf8"));
|
|
109
|
-
} catch (error) {
|
|
110
|
-
throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
function normalizeConnection(value) {
|
|
114
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
115
|
-
return null;
|
|
116
|
-
const record = value;
|
|
117
|
-
if (record.kind === "local")
|
|
118
|
-
return { kind: "local", mode: "auto" };
|
|
119
|
-
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
120
|
-
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
121
|
-
return { kind: "remote", baseUrl };
|
|
122
|
-
}
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
function readGlobalConnections(options = {}) {
|
|
126
|
-
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
127
|
-
const payload = readJsonFile(path);
|
|
128
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
129
|
-
return { connections: {} };
|
|
130
|
-
}
|
|
131
|
-
const rawConnections = payload.connections;
|
|
132
|
-
const connections = {};
|
|
133
|
-
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
134
|
-
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
135
|
-
const connection = normalizeConnection(raw);
|
|
136
|
-
if (connection)
|
|
137
|
-
connections[alias] = connection;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return { connections };
|
|
141
|
-
}
|
|
142
|
-
function readRepoConnection(projectRoot) {
|
|
143
|
-
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
144
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
145
|
-
return null;
|
|
146
|
-
const record = payload;
|
|
147
|
-
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
148
|
-
if (!selected)
|
|
149
|
-
return null;
|
|
150
|
-
return {
|
|
151
|
-
selected,
|
|
152
|
-
project: typeof record.project === "string" ? record.project : undefined,
|
|
153
|
-
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
157
|
-
const repo = readRepoConnection(projectRoot);
|
|
158
|
-
if (!repo)
|
|
159
|
-
return null;
|
|
160
|
-
if (repo.selected === "local")
|
|
161
|
-
return { alias: "local", connection: { kind: "local", mode: "auto" } };
|
|
162
|
-
const global = readGlobalConnections(options);
|
|
163
|
-
const connection = global.connections[repo.selected];
|
|
164
|
-
if (!connection) {
|
|
165
|
-
throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
|
|
166
|
-
}
|
|
167
|
-
return { alias: repo.selected, connection };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// packages/cli/src/commands/_server-client.ts
|
|
171
|
-
var scopedGitHubBearerTokens = new Map;
|
|
172
|
-
function cleanToken(value) {
|
|
173
|
-
const trimmed = value?.trim();
|
|
174
|
-
return trimmed ? trimmed : null;
|
|
175
|
-
}
|
|
176
|
-
function readPrivateRemoteSessionToken(projectRoot) {
|
|
177
|
-
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
178
|
-
if (!existsSync2(path))
|
|
179
|
-
return null;
|
|
180
|
-
try {
|
|
181
|
-
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
182
|
-
return cleanToken(typeof parsed.apiSessionToken === "string" ? parsed.apiSessionToken : typeof parsed.sessionToken === "string" ? parsed.sessionToken : undefined);
|
|
183
|
-
} catch {
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
function readGitHubBearerTokenForRemote(projectRoot) {
|
|
188
|
-
const scopedKey = resolve2(projectRoot);
|
|
189
|
-
if (scopedGitHubBearerTokens.has(scopedKey))
|
|
190
|
-
return scopedGitHubBearerTokens.get(scopedKey) ?? null;
|
|
191
|
-
const privateSession = readPrivateRemoteSessionToken(projectRoot);
|
|
192
|
-
if (privateSession)
|
|
193
|
-
return privateSession;
|
|
194
|
-
return cleanToken(process.env.RIG_SERVER_AUTH_TOKEN) ?? cleanToken(process.env.RIG_REMOTE_AUTH_TOKEN);
|
|
195
|
-
}
|
|
196
|
-
async function ensureServerForCli(projectRoot) {
|
|
197
|
-
try {
|
|
198
|
-
const selected = resolveSelectedConnection(projectRoot);
|
|
199
|
-
if (selected?.connection.kind === "remote") {
|
|
200
|
-
return {
|
|
201
|
-
baseUrl: selected.connection.baseUrl,
|
|
202
|
-
authToken: readGitHubBearerTokenForRemote(projectRoot),
|
|
203
|
-
connectionKind: "remote"
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
207
|
-
return {
|
|
208
|
-
baseUrl: connection.baseUrl,
|
|
209
|
-
authToken: connection.authToken,
|
|
210
|
-
connectionKind: "local"
|
|
211
|
-
};
|
|
212
|
-
} catch (error) {
|
|
213
|
-
if (error instanceof Error) {
|
|
214
|
-
throw new CliError2(error.message, 1);
|
|
215
|
-
}
|
|
216
|
-
throw error;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
function mergeHeaders(headers, authToken) {
|
|
220
|
-
const merged = new Headers(headers);
|
|
221
|
-
if (authToken) {
|
|
222
|
-
merged.set("authorization", `Bearer ${authToken}`);
|
|
223
|
-
}
|
|
224
|
-
return merged;
|
|
225
|
-
}
|
|
226
|
-
function diagnosticMessage(payload) {
|
|
227
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
228
|
-
return null;
|
|
229
|
-
const record = payload;
|
|
230
|
-
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
231
|
-
const messages = diagnostics.flatMap((entry) => {
|
|
232
|
-
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
233
|
-
return [];
|
|
234
|
-
const diagnostic = entry;
|
|
235
|
-
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
236
|
-
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
237
|
-
return message ? [`${kind}: ${message}`] : [];
|
|
238
|
-
});
|
|
239
|
-
return messages.length > 0 ? messages.join("; ") : null;
|
|
240
|
-
}
|
|
241
|
-
async function requestServerJson(context, pathname, init = {}) {
|
|
242
|
-
const server = await ensureServerForCli(context.projectRoot);
|
|
243
|
-
const response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
244
|
-
...init,
|
|
245
|
-
headers: mergeHeaders(init.headers, server.authToken)
|
|
246
|
-
});
|
|
247
|
-
const text = await response.text();
|
|
248
|
-
const payload = text.trim().length > 0 ? (() => {
|
|
249
|
-
try {
|
|
250
|
-
return JSON.parse(text);
|
|
251
|
-
} catch {
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
254
|
-
})() : null;
|
|
255
|
-
if (!response.ok) {
|
|
256
|
-
const diagnostics = diagnosticMessage(payload);
|
|
257
|
-
const detail = diagnostics ?? (text || response.statusText);
|
|
258
|
-
throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
|
|
259
|
-
}
|
|
260
|
-
return payload;
|
|
261
|
-
}
|
|
262
|
-
async function listRunsViaServer(context, options = {}) {
|
|
263
|
-
const url = new URL("http://rig.local/api/runs");
|
|
264
|
-
if (options.limit !== undefined)
|
|
265
|
-
url.searchParams.set("limit", String(options.limit));
|
|
266
|
-
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
267
|
-
const runs = Array.isArray(payload) ? payload : payload && typeof payload === "object" && !Array.isArray(payload) && Array.isArray(payload.runs) ? payload.runs : [];
|
|
268
|
-
return runs.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry)));
|
|
269
|
-
}
|
|
270
|
-
async function getRunDetailsViaServer(context, runId) {
|
|
271
|
-
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}`);
|
|
272
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
273
|
-
}
|
|
274
|
-
async function getRunLogsViaServer(context, runId, options = {}) {
|
|
275
|
-
const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/logs`);
|
|
276
|
-
if (options.limit !== undefined)
|
|
277
|
-
url.searchParams.set("limit", String(options.limit));
|
|
278
|
-
if (options.cursor)
|
|
279
|
-
url.searchParams.set("cursor", options.cursor);
|
|
280
|
-
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
281
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
|
|
282
|
-
}
|
|
283
|
-
async function getRunTimelineViaServer(context, runId, options = {}) {
|
|
284
|
-
const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/timeline`);
|
|
285
|
-
if (options.limit !== undefined)
|
|
286
|
-
url.searchParams.set("limit", String(options.limit));
|
|
287
|
-
if (options.cursor)
|
|
288
|
-
url.searchParams.set("cursor", options.cursor);
|
|
289
|
-
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
290
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
|
|
291
|
-
}
|
|
292
|
-
async function stopRunViaServer(context, runId) {
|
|
293
|
-
const payload = await requestServerJson(context, "/api/runs/stop", {
|
|
294
|
-
method: "POST",
|
|
295
|
-
headers: { "content-type": "application/json" },
|
|
296
|
-
body: JSON.stringify({ runId })
|
|
297
|
-
});
|
|
298
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true, runId };
|
|
299
|
-
}
|
|
300
|
-
async function steerRunViaServer(context, runId, message) {
|
|
301
|
-
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/steer`, {
|
|
302
|
-
method: "POST",
|
|
303
|
-
headers: { "content-type": "application/json" },
|
|
304
|
-
body: JSON.stringify({ message })
|
|
305
|
-
});
|
|
306
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// packages/cli/src/commands/_operator-surface.ts
|
|
310
|
-
import { createInterface } from "readline";
|
|
311
|
-
var CANONICAL_STAGES = [
|
|
312
|
-
"Connect",
|
|
313
|
-
"GitHub/task sync",
|
|
314
|
-
"Prepare workspace",
|
|
315
|
-
"Launch Pi",
|
|
316
|
-
"Plan",
|
|
317
|
-
"Implement",
|
|
318
|
-
"Validate",
|
|
319
|
-
"Commit",
|
|
320
|
-
"Open PR",
|
|
321
|
-
"Review/CI",
|
|
322
|
-
"Merge",
|
|
323
|
-
"Complete"
|
|
324
|
-
];
|
|
325
|
-
function logDetail(log) {
|
|
326
|
-
return typeof log.detail === "string" ? log.detail.trim() : "";
|
|
327
|
-
}
|
|
328
|
-
function parseProviderProtocolLog(title, detail) {
|
|
329
|
-
if (title.trim().toLowerCase() !== "agent output")
|
|
330
|
-
return null;
|
|
331
|
-
if (!detail.startsWith("{") || !detail.endsWith("}"))
|
|
332
|
-
return null;
|
|
333
|
-
try {
|
|
334
|
-
const record = JSON.parse(detail);
|
|
335
|
-
if (!record || typeof record !== "object" || Array.isArray(record))
|
|
336
|
-
return null;
|
|
337
|
-
const type = record.type;
|
|
338
|
-
return typeof type === "string" && [
|
|
339
|
-
"assistant",
|
|
340
|
-
"message_start",
|
|
341
|
-
"message_update",
|
|
342
|
-
"message_end",
|
|
343
|
-
"stream_event",
|
|
344
|
-
"tool_result",
|
|
345
|
-
"tool_execution_start",
|
|
346
|
-
"tool_execution_update",
|
|
347
|
-
"tool_execution_end",
|
|
348
|
-
"turn_start",
|
|
349
|
-
"turn_end"
|
|
350
|
-
].includes(type) ? record : null;
|
|
351
|
-
} catch {
|
|
352
|
-
return null;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
function renderProviderProtocolLog(record) {
|
|
356
|
-
const type = typeof record.type === "string" ? record.type : "";
|
|
357
|
-
if (type === "tool_execution_start" || type === "tool_execution_update" || type === "tool_execution_end") {
|
|
358
|
-
const toolName = String(record.toolName ?? record.name ?? "tool");
|
|
359
|
-
const status = type === "tool_execution_start" ? "started" : type === "tool_execution_end" ? record.isError === true || record.result && typeof record.result === "object" && !Array.isArray(record.result) && record.result.isError === true ? "failed" : "completed" : "running";
|
|
360
|
-
return `[Pi tool] ${toolName} ${status}`;
|
|
361
|
-
}
|
|
362
|
-
return null;
|
|
363
|
-
}
|
|
364
|
-
function entryId(entry, fallback) {
|
|
365
|
-
return typeof entry.id === "string" && entry.id.trim() ? entry.id : fallback;
|
|
366
|
-
}
|
|
367
|
-
function renderOperatorSnapshot(snapshot) {
|
|
368
|
-
const run = snapshot.run.run && typeof snapshot.run.run === "object" ? snapshot.run.run : snapshot.run;
|
|
369
|
-
const runId = String(run.runId ?? run.id ?? "run");
|
|
370
|
-
const status = String(run.status ?? "unknown");
|
|
371
|
-
const logs = snapshot.logs ?? [];
|
|
372
|
-
const latestByStage = new Map;
|
|
373
|
-
for (const log of logs) {
|
|
374
|
-
const title = String(log.title ?? "").toLowerCase();
|
|
375
|
-
const stageName = String(log.stage ?? "").toLowerCase();
|
|
376
|
-
const stage = CANONICAL_STAGES.find((candidate) => candidate.toLowerCase() === title || candidate.toLowerCase() === stageName);
|
|
377
|
-
if (stage)
|
|
378
|
-
latestByStage.set(stage, log);
|
|
379
|
-
}
|
|
380
|
-
const stageLines = CANONICAL_STAGES.flatMap((stage) => {
|
|
381
|
-
const match = latestByStage.get(stage);
|
|
382
|
-
return match ? [`${stage}: ${String(match.status ?? status)}${logDetail(match) ? ` \u2014 ${logDetail(match)}` : ""}`] : [];
|
|
383
|
-
});
|
|
384
|
-
return [`Rig run ${runId}: ${status}`, ...stageLines].join(`
|
|
385
|
-
`);
|
|
386
|
-
}
|
|
387
|
-
function createPiRunStreamRenderer(output = process.stdout) {
|
|
388
|
-
let lastSnapshot = "";
|
|
389
|
-
const assistantTextById = new Map;
|
|
390
|
-
const seenTimeline = new Set;
|
|
391
|
-
const seenLogs = new Set;
|
|
392
|
-
const writeLine = (line) => output.write(`${line}
|
|
393
|
-
`);
|
|
394
|
-
return {
|
|
395
|
-
renderSnapshot(snapshot) {
|
|
396
|
-
const rendered = renderOperatorSnapshot(snapshot);
|
|
397
|
-
if (rendered && rendered !== lastSnapshot) {
|
|
398
|
-
writeLine(rendered);
|
|
399
|
-
lastSnapshot = rendered;
|
|
400
|
-
}
|
|
401
|
-
},
|
|
402
|
-
renderTimeline(entries) {
|
|
403
|
-
for (const [index, entry] of entries.entries()) {
|
|
404
|
-
const id = entryId(entry, `timeline:${index}:${String(entry.cursor ?? "")}`);
|
|
405
|
-
if (entry.type === "assistant_message" && typeof entry.text === "string") {
|
|
406
|
-
const text = entry.text;
|
|
407
|
-
const previousText = assistantTextById.get(id) ?? "";
|
|
408
|
-
if (!previousText && text.trim()) {
|
|
409
|
-
writeLine("[Pi assistant]");
|
|
410
|
-
}
|
|
411
|
-
if (text.startsWith(previousText)) {
|
|
412
|
-
const delta = text.slice(previousText.length);
|
|
413
|
-
if (delta)
|
|
414
|
-
output.write(delta);
|
|
415
|
-
} else if (text.trim() && text !== previousText) {
|
|
416
|
-
if (previousText)
|
|
417
|
-
writeLine(`
|
|
418
|
-
[Pi assistant]`);
|
|
419
|
-
output.write(text);
|
|
420
|
-
}
|
|
421
|
-
assistantTextById.set(id, text);
|
|
422
|
-
continue;
|
|
423
|
-
}
|
|
424
|
-
if (seenTimeline.has(id))
|
|
425
|
-
continue;
|
|
426
|
-
seenTimeline.add(id);
|
|
427
|
-
if (entry.type === "tool_execution_start" || entry.type === "tool_execution_update" || entry.type === "tool_execution_end" || entry.type === "mcp_tool_call") {
|
|
428
|
-
writeLine(`[Pi tool] ${String(entry.toolName ?? entry.name ?? entry.title ?? entry.type)} ${String(entry.status ?? entry.state ?? "")}`.trim());
|
|
429
|
-
continue;
|
|
430
|
-
}
|
|
431
|
-
if (entry.type === "timeline_warning") {
|
|
432
|
-
writeLine(`[Rig timeline] ${String(entry.detail ?? entry.message ?? "timeline unavailable")}`);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
},
|
|
436
|
-
renderLogs(entries) {
|
|
437
|
-
for (const [index, entry] of entries.entries()) {
|
|
438
|
-
const id = entryId(entry, `log:${index}:${String(entry.createdAt ?? "")}:${String(entry.title ?? "")}`);
|
|
439
|
-
if (seenLogs.has(id))
|
|
440
|
-
continue;
|
|
441
|
-
seenLogs.add(id);
|
|
442
|
-
const title = String(entry.title ?? "");
|
|
443
|
-
if (CANONICAL_STAGES.some((stage) => stage.toLowerCase() === title.toLowerCase()))
|
|
444
|
-
continue;
|
|
445
|
-
const detail = logDetail(entry);
|
|
446
|
-
if (!detail)
|
|
447
|
-
continue;
|
|
448
|
-
const protocolRecord = parseProviderProtocolLog(title, detail);
|
|
449
|
-
if (protocolRecord) {
|
|
450
|
-
const protocolLine = renderProviderProtocolLog(protocolRecord);
|
|
451
|
-
if (protocolLine)
|
|
452
|
-
writeLine(protocolLine);
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
writeLine(`[${title || "Rig log"}] ${detail}`);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
function createOperatorSurface(options = {}) {
|
|
461
|
-
const input = options.input ?? process.stdin;
|
|
462
|
-
const output = options.output ?? process.stdout;
|
|
463
|
-
const errorOutput = options.errorOutput ?? process.stderr;
|
|
464
|
-
const renderer = createPiRunStreamRenderer(output);
|
|
465
|
-
const writeLine = (line) => output.write(`${line}
|
|
466
|
-
`);
|
|
467
|
-
return {
|
|
468
|
-
mode: "pi-compatible-text",
|
|
469
|
-
...renderer,
|
|
470
|
-
info: writeLine,
|
|
471
|
-
error: (message) => errorOutput.write(`${message}
|
|
472
|
-
`),
|
|
473
|
-
attachCommandInput(handler) {
|
|
474
|
-
if (options.interactive === false || !input.isTTY)
|
|
475
|
-
return null;
|
|
476
|
-
const rl = createInterface({ input, output: process.stdout, terminal: false });
|
|
477
|
-
rl.on("line", (line) => {
|
|
478
|
-
Promise.resolve(handler(line)).catch((error) => writeLine(`Operator command failed: ${error instanceof Error ? error.message : String(error)}`));
|
|
479
|
-
});
|
|
480
|
-
return { close: () => rl.close() };
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// packages/cli/src/commands/_operator-view.ts
|
|
486
|
-
var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
|
|
487
|
-
function runStatusFromPayload(payload) {
|
|
488
|
-
const run = payload.run && typeof payload.run === "object" && !Array.isArray(payload.run) ? payload.run : payload;
|
|
489
|
-
return String(run.status ?? "unknown").toLowerCase();
|
|
490
|
-
}
|
|
491
|
-
async function applyOperatorCommand(context, input, deps = {}) {
|
|
492
|
-
const line = input.line.trim();
|
|
493
|
-
if (!line)
|
|
494
|
-
return { action: "ignored" };
|
|
495
|
-
if (line === "/detach" || line === "/quit" || line === "/q") {
|
|
496
|
-
return { action: "detach", message: "Detached from run." };
|
|
497
|
-
}
|
|
498
|
-
if (line === "/stop") {
|
|
499
|
-
await (deps.stop ?? stopRunViaServer)(context, input.runId);
|
|
500
|
-
return { action: "stopped", message: "Stop requested." };
|
|
501
|
-
}
|
|
502
|
-
const userMessage = line.startsWith("/user ") ? line.slice("/user ".length).trim() : line;
|
|
503
|
-
if (!userMessage)
|
|
504
|
-
return { action: "ignored" };
|
|
505
|
-
await (deps.steer ?? steerRunViaServer)(context, input.runId, userMessage);
|
|
506
|
-
return { action: "continue", message: "Steering message queued." };
|
|
507
|
-
}
|
|
508
|
-
async function readOperatorSnapshot(context, runId, options = {}) {
|
|
509
|
-
const run = await getRunDetailsViaServer(context, runId);
|
|
510
|
-
const logsPage = await getRunLogsViaServer(context, runId, { limit: 100 });
|
|
511
|
-
const timelinePage = await getRunTimelineViaServer(context, runId, { limit: 200, ...options.timelineCursor ? { cursor: options.timelineCursor } : {} }).catch((error) => ({
|
|
512
|
-
entries: [{
|
|
513
|
-
id: `timeline-unavailable:${runId}`,
|
|
514
|
-
type: "timeline_warning",
|
|
515
|
-
detail: `Selected Rig server did not provide run timeline events: ${error instanceof Error ? error.message : String(error)}`,
|
|
516
|
-
createdAt: new Date().toISOString()
|
|
517
|
-
}],
|
|
518
|
-
nextCursor: options.timelineCursor ?? null
|
|
519
|
-
}));
|
|
520
|
-
const logs = Array.isArray(logsPage.entries) ? logsPage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))).toReversed() : [];
|
|
521
|
-
const timeline = Array.isArray(timelinePage.entries) ? timelinePage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))) : [];
|
|
522
|
-
const timelineCursor = typeof timelinePage.nextCursor === "string" ? timelinePage.nextCursor : options.timelineCursor ?? null;
|
|
523
|
-
return { run, logs, timeline, timelineCursor, rendered: renderOperatorSnapshot({ run, logs, timeline }) };
|
|
524
|
-
}
|
|
525
|
-
async function attachRunOperatorView(context, input) {
|
|
526
|
-
let steered = false;
|
|
527
|
-
if (input.message?.trim()) {
|
|
528
|
-
await steerRunViaServer(context, input.runId, input.message.trim());
|
|
529
|
-
steered = true;
|
|
530
|
-
}
|
|
531
|
-
const surface = createOperatorSurface({ interactive: input.interactive !== false });
|
|
532
|
-
let snapshot = await readOperatorSnapshot(context, input.runId);
|
|
533
|
-
if (context.outputMode === "text") {
|
|
534
|
-
surface.renderSnapshot(snapshot);
|
|
535
|
-
surface.renderTimeline(snapshot.timeline);
|
|
536
|
-
surface.renderLogs(snapshot.logs);
|
|
537
|
-
if (steered)
|
|
538
|
-
surface.info("Steering message queued.");
|
|
539
|
-
}
|
|
540
|
-
let detached = false;
|
|
541
|
-
let commandInput = null;
|
|
542
|
-
if (input.follow && !input.once && context.outputMode === "text") {
|
|
543
|
-
if (input.interactive !== false && process.stdin.isTTY) {
|
|
544
|
-
surface.info("Controls: /user <message>, /stop, /detach");
|
|
545
|
-
commandInput = surface.attachCommandInput(async (line) => {
|
|
546
|
-
const result = await applyOperatorCommand(context, { runId: input.runId, line });
|
|
547
|
-
if (result.message)
|
|
548
|
-
surface.info(result.message);
|
|
549
|
-
if (result.action === "detach" || result.action === "stopped") {
|
|
550
|
-
detached = true;
|
|
551
|
-
commandInput?.close();
|
|
552
|
-
}
|
|
553
|
-
});
|
|
554
|
-
}
|
|
555
|
-
const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 2000));
|
|
556
|
-
let timelineCursor = snapshot.timelineCursor;
|
|
557
|
-
while (!detached && !TERMINAL_RUN_STATUSES.has(runStatusFromPayload(snapshot.run))) {
|
|
558
|
-
await Bun.sleep(pollMs);
|
|
559
|
-
snapshot = await readOperatorSnapshot(context, input.runId, { timelineCursor });
|
|
560
|
-
timelineCursor = snapshot.timelineCursor;
|
|
561
|
-
surface.renderSnapshot(snapshot);
|
|
562
|
-
surface.renderTimeline(snapshot.timeline);
|
|
563
|
-
surface.renderLogs(snapshot.logs);
|
|
564
|
-
}
|
|
565
|
-
commandInput?.close();
|
|
566
|
-
}
|
|
567
|
-
return { ...snapshot, steered, detached };
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
// packages/cli/src/commands/run.ts
|
|
571
|
-
function normalizeRemoteRunDetails(payload) {
|
|
572
|
-
const run = payload.run;
|
|
573
|
-
if (!run || typeof run !== "object" || Array.isArray(run))
|
|
574
|
-
return null;
|
|
575
|
-
return {
|
|
576
|
-
...run,
|
|
577
|
-
...Array.isArray(payload.timeline) ? { timeline: payload.timeline } : {},
|
|
578
|
-
...Array.isArray(payload.approvals) ? { approvals: payload.approvals } : {},
|
|
579
|
-
...Array.isArray(payload.userInputs) ? { userInputs: payload.userInputs } : {}
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
var REMOTE_TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged"]);
|
|
583
|
-
function isRemoteConnectionSelected(projectRoot) {
|
|
584
|
-
return resolveSelectedConnection(projectRoot)?.connection.kind === "remote";
|
|
585
|
-
}
|
|
586
|
-
async function listRunsForSelectedConnection(context, options = {}) {
|
|
587
|
-
if (isRemoteConnectionSelected(context.projectRoot)) {
|
|
588
|
-
return { runs: await listRunsViaServer(context, options), source: "server" };
|
|
589
|
-
}
|
|
590
|
-
return { runs: listAuthorityRuns(context.projectRoot), source: "local" };
|
|
591
|
-
}
|
|
592
|
-
function runStringField(run, key, fallback = "") {
|
|
593
|
-
const value = run[key];
|
|
594
|
-
return typeof value === "string" && value.trim() ? value : fallback;
|
|
595
|
-
}
|
|
596
|
-
function runDisplayTitle(run) {
|
|
597
|
-
return runStringField(run, "title", runStringField(run, "taskId", "(untitled)"));
|
|
598
|
-
}
|
|
599
|
-
function buildServerRunStatus(runs) {
|
|
600
|
-
const activeRuns = runs.filter((run) => !REMOTE_TERMINAL_RUN_STATUSES.has(runStringField(run, "status").toLowerCase()));
|
|
601
|
-
const recentRuns = runs.filter((run) => REMOTE_TERMINAL_RUN_STATUSES.has(runStringField(run, "status").toLowerCase()));
|
|
602
|
-
return { activeRuns, recentRuns, runs };
|
|
603
|
-
}
|
|
604
|
-
function shouldPromptForEpicSelection(context, command, promptEpic, noEpicPrompt) {
|
|
605
|
-
if (noEpicPrompt) {
|
|
606
|
-
return false;
|
|
607
|
-
}
|
|
608
|
-
if (promptEpic) {
|
|
609
|
-
return true;
|
|
610
|
-
}
|
|
611
|
-
if (command !== "start-serial") {
|
|
612
|
-
return false;
|
|
613
|
-
}
|
|
614
|
-
return context.outputMode === "text" && process.stdin.isTTY && process.stdout.isTTY;
|
|
615
|
-
}
|
|
616
|
-
async function promptForEpicSelection(projectRoot, command) {
|
|
617
|
-
const epics = listOpenEpics(projectRoot);
|
|
618
|
-
const defaultEpic = await resolveDefaultEpic(projectRoot);
|
|
619
|
-
const options = epics.map((epic) => epic.id);
|
|
620
|
-
if (defaultEpic && !options.includes(defaultEpic)) {
|
|
621
|
-
options.unshift(defaultEpic);
|
|
622
|
-
}
|
|
623
|
-
if (options.length === 0) {
|
|
624
|
-
throw new CliError2("No open epic found. Pass --epic <id>.");
|
|
625
|
-
}
|
|
626
|
-
console.log(`Select epic for run ${command}:`);
|
|
627
|
-
options.forEach((id, index) => {
|
|
628
|
-
const metadata = epics.find((epic) => epic.id === id);
|
|
629
|
-
const details = [
|
|
630
|
-
metadata?.priority !== undefined ? `priority:${metadata.priority}` : "",
|
|
631
|
-
metadata?.createdAt ? `created:${metadata.createdAt.slice(0, 10)}` : "",
|
|
632
|
-
id === defaultEpic ? "default" : ""
|
|
633
|
-
].filter(Boolean).join(" ");
|
|
634
|
-
const suffix = details ? ` (${details})` : "";
|
|
635
|
-
console.log(` ${index + 1}. ${id}${suffix}`);
|
|
636
|
-
});
|
|
637
|
-
const fallback = defaultEpic || options[0];
|
|
638
|
-
const rl = createInterface2({ input: process.stdin, output: process.stdout });
|
|
639
|
-
try {
|
|
640
|
-
while (true) {
|
|
641
|
-
const answer = (await rl.question(`Epic [1-${options.length}] or id (Enter for ${fallback}, q to cancel): `)).trim();
|
|
642
|
-
if (!answer) {
|
|
643
|
-
return fallback ?? options[0];
|
|
644
|
-
}
|
|
645
|
-
if (answer === "q" || answer === "quit") {
|
|
646
|
-
throw new CliError2("Run cancelled by user.");
|
|
647
|
-
}
|
|
648
|
-
if (/^\d+$/.test(answer)) {
|
|
649
|
-
const index = Number.parseInt(answer, 10) - 1;
|
|
650
|
-
if (index >= 0 && index < options.length) {
|
|
651
|
-
return options[index];
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
if (options.includes(answer)) {
|
|
655
|
-
return answer;
|
|
656
|
-
}
|
|
657
|
-
console.log("Invalid selection. Choose a listed number, exact epic id, or q to cancel.");
|
|
658
|
-
}
|
|
659
|
-
} finally {
|
|
660
|
-
rl.close();
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
async function executeRun(context, args) {
|
|
664
|
-
const [command = "status", ...rest] = args;
|
|
665
|
-
const runtimeContext = loadRuntimeContextFromEnv2() ?? undefined;
|
|
666
|
-
switch (command) {
|
|
667
|
-
case "list": {
|
|
668
|
-
requireNoExtraArgs(rest, "bun run rig run list");
|
|
669
|
-
const { runs, source } = await listRunsForSelectedConnection(context, { limit: 100 });
|
|
670
|
-
if (context.outputMode === "text") {
|
|
671
|
-
if (runs.length === 0) {
|
|
672
|
-
console.log(source === "server" ? "No runs recorded on the selected Rig server." : "No runs recorded in .rig/runs.");
|
|
673
|
-
} else {
|
|
674
|
-
for (const run of runs) {
|
|
675
|
-
console.log(`- ${runStringField(run, "runId", "(unknown-run)")} \xB7 ${runStringField(run, "status", "unknown")} \xB7 ${runDisplayTitle(run)}`);
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
return { ok: true, group: "run", command, details: { runs, source } };
|
|
680
|
-
}
|
|
681
|
-
case "delete": {
|
|
682
|
-
let pending = rest;
|
|
683
|
-
const run = takeOption(pending, "--run");
|
|
684
|
-
pending = run.rest;
|
|
685
|
-
const purgeArtifacts = takeFlag(pending, "--purge-artifacts");
|
|
686
|
-
pending = purgeArtifacts.rest;
|
|
687
|
-
requireNoExtraArgs(pending, "bun run rig run delete --run <id> [--purge-artifacts]");
|
|
688
|
-
if (!run.value) {
|
|
689
|
-
throw new CliError2("run delete requires --run <id>.");
|
|
690
|
-
}
|
|
691
|
-
const result = await deleteRunState(context.projectRoot, {
|
|
692
|
-
runId: run.value,
|
|
693
|
-
purgeRuntime: true,
|
|
694
|
-
purgeTaskArtifacts: purgeArtifacts.value
|
|
695
|
-
});
|
|
696
|
-
if (context.outputMode === "text") {
|
|
697
|
-
console.log(`Deleted run ${result.runId}`);
|
|
698
|
-
if (result.runtimeIds.length > 0) {
|
|
699
|
-
console.log(`Cleaned runtimes: ${result.runtimeIds.join(", ")}`);
|
|
700
|
-
}
|
|
701
|
-
if (result.taskArtifactsDeleted) {
|
|
702
|
-
console.log(`Cleared task artifacts for ${result.taskId}`);
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
return { ok: true, group: "run", command, details: result };
|
|
706
|
-
}
|
|
707
|
-
case "cleanup": {
|
|
708
|
-
let pending = rest;
|
|
709
|
-
const all = takeFlag(pending, "--all");
|
|
710
|
-
pending = all.rest;
|
|
711
|
-
const keepArtifacts = takeFlag(pending, "--keep-artifacts");
|
|
712
|
-
pending = keepArtifacts.rest;
|
|
713
|
-
const keepRuntimes = takeFlag(pending, "--keep-runtimes");
|
|
714
|
-
pending = keepRuntimes.rest;
|
|
715
|
-
const keepQueue = takeFlag(pending, "--keep-queue");
|
|
716
|
-
pending = keepQueue.rest;
|
|
717
|
-
requireNoExtraArgs(pending, "bun run rig run cleanup --all [--keep-artifacts] [--keep-runtimes] [--keep-queue]");
|
|
718
|
-
if (!all.value) {
|
|
719
|
-
throw new CliError2("run cleanup currently requires --all.");
|
|
720
|
-
}
|
|
721
|
-
const result = await cleanupRunState(context.projectRoot, {
|
|
722
|
-
includeArtifacts: !keepArtifacts.value,
|
|
723
|
-
includeRuntimes: !keepRuntimes.value,
|
|
724
|
-
includeQueue: !keepQueue.value
|
|
725
|
-
});
|
|
726
|
-
if (context.outputMode === "text") {
|
|
727
|
-
console.log(`Deleted runs: ${result.runIds.length}`);
|
|
728
|
-
console.log(`Cleaned runtimes: ${result.runtimeIds.length}`);
|
|
729
|
-
console.log(`Artifacts cleared: ${result.artifactsCleared ? "yes" : "no"}`);
|
|
730
|
-
console.log(`Queue cleared: ${result.queueCleared ? "yes" : "no"}`);
|
|
731
|
-
}
|
|
732
|
-
return { ok: true, group: "run", command, details: result };
|
|
733
|
-
}
|
|
734
|
-
case "show": {
|
|
735
|
-
let pending = rest;
|
|
736
|
-
const run = takeOption(pending, "--run");
|
|
737
|
-
pending = run.rest;
|
|
738
|
-
requireNoExtraArgs(pending, "bun run rig run show --run <id>");
|
|
739
|
-
if (!run.value) {
|
|
740
|
-
throw new CliError2("run show requires --run <id>.");
|
|
741
|
-
}
|
|
742
|
-
const record = readAuthorityRun(context.projectRoot, run.value) ?? normalizeRemoteRunDetails(await getRunDetailsViaServer(context, run.value).catch(() => ({})));
|
|
743
|
-
if (!record) {
|
|
744
|
-
throw new CliError2(`Run not found: ${run.value}`, 2);
|
|
745
|
-
}
|
|
746
|
-
if (context.outputMode === "text") {
|
|
747
|
-
console.log(JSON.stringify(record, null, 2));
|
|
748
|
-
}
|
|
749
|
-
return { ok: true, group: "run", command, details: record };
|
|
750
|
-
}
|
|
751
|
-
case "timeline": {
|
|
752
|
-
let pending = rest;
|
|
753
|
-
const run = takeOption(pending, "--run");
|
|
754
|
-
pending = run.rest;
|
|
755
|
-
const follow = takeFlag(pending, "--follow");
|
|
756
|
-
pending = follow.rest;
|
|
757
|
-
requireNoExtraArgs(pending, "bun run rig run timeline --run <id> [--follow]");
|
|
758
|
-
if (!run.value) {
|
|
759
|
-
throw new CliError2("run timeline requires --run <id>.");
|
|
760
|
-
}
|
|
761
|
-
const renderer = createPiRunStreamRenderer();
|
|
762
|
-
let cursor = null;
|
|
763
|
-
const page = await getRunTimelineViaServer(context, run.value, { limit: 500 });
|
|
764
|
-
const events = Array.isArray(page.entries) ? page.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))) : [];
|
|
765
|
-
cursor = typeof page.nextCursor === "string" ? page.nextCursor : null;
|
|
766
|
-
if (context.outputMode === "text") {
|
|
767
|
-
renderer.renderTimeline(events);
|
|
768
|
-
}
|
|
769
|
-
if (follow.value && context.outputMode === "text") {
|
|
770
|
-
while (true) {
|
|
771
|
-
await Bun.sleep(1000);
|
|
772
|
-
const nextPage = await getRunTimelineViaServer(context, run.value, { limit: 500, ...cursor ? { cursor } : {} });
|
|
773
|
-
const nextEvents = Array.isArray(nextPage.entries) ? nextPage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))) : [];
|
|
774
|
-
cursor = typeof nextPage.nextCursor === "string" ? nextPage.nextCursor : cursor;
|
|
775
|
-
renderer.renderTimeline(nextEvents);
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
return { ok: true, group: "run", command, details: { runId: run.value, events, cursor } };
|
|
779
|
-
}
|
|
780
|
-
case "attach": {
|
|
781
|
-
let pending = rest;
|
|
782
|
-
const runOption = takeOption(pending, "--run");
|
|
783
|
-
pending = runOption.rest;
|
|
784
|
-
const messageOption = takeOption(pending, "--message");
|
|
785
|
-
pending = messageOption.rest;
|
|
786
|
-
const once = takeFlag(pending, "--once");
|
|
787
|
-
pending = once.rest;
|
|
788
|
-
const follow = takeFlag(pending, "--follow");
|
|
789
|
-
pending = follow.rest;
|
|
790
|
-
const pollMs = takeOption(pending, "--poll-ms");
|
|
791
|
-
pending = pollMs.rest;
|
|
792
|
-
const positionalRunId = pending.length > 0 ? pending[0] : undefined;
|
|
793
|
-
const extra = positionalRunId ? pending.slice(1) : pending;
|
|
794
|
-
requireNoExtraArgs(extra, "bun run rig run attach <run-id>|--run <run-id> [--message <text>] [--once|--follow] [--poll-ms <ms>]");
|
|
795
|
-
const runId = runOption.value ?? positionalRunId;
|
|
796
|
-
if (!runId) {
|
|
797
|
-
throw new CliError2("run attach requires a run id.", 2);
|
|
798
|
-
}
|
|
799
|
-
const attached = await attachRunOperatorView(context, {
|
|
800
|
-
runId,
|
|
801
|
-
message: messageOption.value ?? null,
|
|
802
|
-
once: once.value,
|
|
803
|
-
follow: follow.value,
|
|
804
|
-
pollMs: parsePositiveInt(pollMs.value, "--poll-ms", 2000)
|
|
805
|
-
});
|
|
806
|
-
return { ok: true, group: "run", command, details: attached };
|
|
807
|
-
}
|
|
808
|
-
case "status": {
|
|
809
|
-
requireNoExtraArgs(rest, "bun run rig run status");
|
|
810
|
-
if (context.dryRun) {
|
|
811
|
-
if (context.outputMode === "text") {
|
|
812
|
-
console.log("[dry-run] rig run status");
|
|
813
|
-
}
|
|
814
|
-
return { ok: true, group: "run", command };
|
|
815
|
-
}
|
|
816
|
-
const summary = isRemoteConnectionSelected(context.projectRoot) ? buildServerRunStatus(await listRunsViaServer(context, { limit: 100 })) : runStatus(context.projectRoot, runtimeContext);
|
|
817
|
-
const activeRuns = Array.isArray(summary.activeRuns) ? summary.activeRuns.filter((run) => Boolean(run && typeof run === "object" && !Array.isArray(run))) : [];
|
|
818
|
-
const recentRuns = Array.isArray(summary.recentRuns) ? summary.recentRuns.filter((run) => Boolean(run && typeof run === "object" && !Array.isArray(run))) : [];
|
|
819
|
-
if (context.outputMode === "text") {
|
|
820
|
-
console.log(`Active runs: ${activeRuns.length}`);
|
|
821
|
-
for (const run of activeRuns) {
|
|
822
|
-
console.log(`- ${runStringField(run, "runId", "(unknown-run)")} \xB7 ${runStringField(run, "status", "unknown")} \xB7 ${runStringField(run, "taskId", runDisplayTitle(run))}`);
|
|
823
|
-
}
|
|
824
|
-
if (recentRuns.length > 0) {
|
|
825
|
-
console.log("");
|
|
826
|
-
console.log("Recent runs:");
|
|
827
|
-
for (const run of recentRuns) {
|
|
828
|
-
console.log(`- ${runStringField(run, "runId", "(unknown-run)")} \xB7 ${runStringField(run, "status", "unknown")} \xB7 ${runStringField(run, "taskId", runDisplayTitle(run))}`);
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
return { ok: true, group: "run", command, details: summary };
|
|
833
|
-
}
|
|
834
|
-
case "start":
|
|
835
|
-
case "start-serial":
|
|
836
|
-
case "start-parallel": {
|
|
837
|
-
let pending = rest;
|
|
838
|
-
const epicResult = takeOption(pending, "--epic");
|
|
839
|
-
pending = epicResult.rest;
|
|
840
|
-
const promptEpicResult = takeFlag(pending, "--prompt-epic");
|
|
841
|
-
pending = promptEpicResult.rest;
|
|
842
|
-
const noEpicPromptResult = takeFlag(pending, "--no-epic-prompt");
|
|
843
|
-
pending = noEpicPromptResult.rest;
|
|
844
|
-
const wsPortResult = takeOption(pending, "--ws-port");
|
|
845
|
-
pending = wsPortResult.rest;
|
|
846
|
-
const serverHostResult = takeOption(pending, "--server-host");
|
|
847
|
-
pending = serverHostResult.rest;
|
|
848
|
-
const serverPortResult = takeOption(pending, "--server-port");
|
|
849
|
-
pending = serverPortResult.rest;
|
|
850
|
-
const pollResult = takeOption(pending, "--poll-ms");
|
|
851
|
-
pending = pollResult.rest;
|
|
852
|
-
const noServerResult = takeFlag(pending, "--no-server");
|
|
853
|
-
pending = noServerResult.rest;
|
|
854
|
-
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]");
|
|
855
|
-
if (promptEpicResult.value && noEpicPromptResult.value) {
|
|
856
|
-
throw new CliError2("Cannot use --prompt-epic and --no-epic-prompt together.");
|
|
857
|
-
}
|
|
858
|
-
if (promptEpicResult.value && (context.outputMode !== "text" || !process.stdin.isTTY || !process.stdout.isTTY)) {
|
|
859
|
-
throw new CliError2("--prompt-epic requires an interactive terminal (TTY) in text mode.");
|
|
860
|
-
}
|
|
861
|
-
let resolvedEpicId = epicResult.value || undefined;
|
|
862
|
-
if (!resolvedEpicId && shouldPromptForEpicSelection(context, command, promptEpicResult.value, noEpicPromptResult.value)) {
|
|
863
|
-
resolvedEpicId = await promptForEpicSelection(context.projectRoot, command);
|
|
864
|
-
}
|
|
865
|
-
const defaults = defaultStartRunOptions(command === "start-parallel" ? "parallel" : "serial");
|
|
866
|
-
const result = await startRun(context.projectRoot, {
|
|
867
|
-
mode: command === "start-parallel" ? "parallel" : "serial",
|
|
868
|
-
workers: defaults.workers,
|
|
869
|
-
epicId: resolvedEpicId,
|
|
870
|
-
wsPort: parsePositiveInt(wsPortResult.value, "--ws-port", defaults.wsPort),
|
|
871
|
-
startEventServer: noServerResult.value ? false : defaults.startEventServer,
|
|
872
|
-
serverHost: serverHostResult.value || defaults.serverHost,
|
|
873
|
-
serverPort: parsePositiveInt(serverPortResult.value, "--server-port", defaults.serverPort),
|
|
874
|
-
serverPollMs: parsePositiveInt(pollResult.value, "--poll-ms", defaults.serverPollMs),
|
|
875
|
-
dryRun: context.dryRun,
|
|
876
|
-
runtimeContext
|
|
877
|
-
});
|
|
878
|
-
if (context.outputMode === "text") {
|
|
879
|
-
console.log(`Epic: ${result.epicId}`);
|
|
880
|
-
console.log(`Server: ${result.baseUrl}`);
|
|
881
|
-
if (result.eventServerUrl) {
|
|
882
|
-
console.log(`Events: ${result.eventServerUrl}`);
|
|
883
|
-
}
|
|
884
|
-
console.log(`Runs: ${result.runIds.join(", ")}`);
|
|
885
|
-
}
|
|
886
|
-
if (result.exitCode !== 0) {
|
|
887
|
-
throw new CliError2(`run ${command} failed with exit code ${result.exitCode}.`, result.exitCode);
|
|
888
|
-
}
|
|
889
|
-
return {
|
|
890
|
-
ok: true,
|
|
891
|
-
group: "run",
|
|
892
|
-
command,
|
|
893
|
-
details: {
|
|
894
|
-
epicId: result.epicId,
|
|
895
|
-
baseUrl: result.baseUrl,
|
|
896
|
-
runIds: result.runIds,
|
|
897
|
-
eventServerUrl: result.eventServerUrl || null
|
|
898
|
-
}
|
|
899
|
-
};
|
|
900
|
-
}
|
|
901
|
-
case "resume": {
|
|
902
|
-
requireNoExtraArgs(rest, "bun run rig run resume");
|
|
903
|
-
if (context.dryRun) {
|
|
904
|
-
if (context.outputMode === "text") {
|
|
905
|
-
console.log("[dry-run] rig run resume");
|
|
906
|
-
}
|
|
907
|
-
return { ok: true, group: "run", command };
|
|
908
|
-
}
|
|
909
|
-
const resumed = await runResume(context.projectRoot, runtimeContext);
|
|
910
|
-
if (context.outputMode === "text") {
|
|
911
|
-
console.log(`Resumed run: ${resumed.runId}`);
|
|
912
|
-
}
|
|
913
|
-
return { ok: true, group: "run", command, details: resumed };
|
|
914
|
-
}
|
|
915
|
-
case "restart": {
|
|
916
|
-
requireNoExtraArgs(rest, "bun run rig run restart");
|
|
917
|
-
if (context.dryRun) {
|
|
918
|
-
if (context.outputMode === "text") {
|
|
919
|
-
console.log("[dry-run] rig run restart");
|
|
920
|
-
}
|
|
921
|
-
return { ok: true, group: "run", command };
|
|
922
|
-
}
|
|
923
|
-
const restarted = await runRestart(context.projectRoot, runtimeContext);
|
|
924
|
-
if (context.outputMode === "text") {
|
|
925
|
-
console.log(`Restarted run: ${restarted.runId}`);
|
|
926
|
-
}
|
|
927
|
-
return { ok: true, group: "run", command, details: restarted };
|
|
928
|
-
}
|
|
929
|
-
case "stop": {
|
|
930
|
-
const runOption = takeOption(rest, "--run");
|
|
931
|
-
const positionalRunId = runOption.rest.length > 0 ? runOption.rest[0] : undefined;
|
|
932
|
-
const extra = positionalRunId ? runOption.rest.slice(1) : runOption.rest;
|
|
933
|
-
requireNoExtraArgs(extra, "bun run rig run stop [<run-id>|--run <id>]");
|
|
934
|
-
const runId = runOption.value ?? positionalRunId;
|
|
935
|
-
if (context.dryRun) {
|
|
936
|
-
return {
|
|
937
|
-
ok: true,
|
|
938
|
-
group: "run",
|
|
939
|
-
command,
|
|
940
|
-
details: runId ? { runId, accepted: true } : { stopped: 0, remaining: [] }
|
|
941
|
-
};
|
|
942
|
-
}
|
|
943
|
-
if (runId) {
|
|
944
|
-
const stopped = await stopRunViaServer(context, runId);
|
|
945
|
-
if (context.outputMode === "text")
|
|
946
|
-
console.log(`Stop requested: ${runId}`);
|
|
947
|
-
return { ok: true, group: "run", command, details: stopped };
|
|
948
|
-
}
|
|
949
|
-
const result = await runStop(context.projectRoot);
|
|
950
|
-
if (result.remaining.length > 0) {
|
|
951
|
-
throw new CliError2(`Failed to stop run(s): ${result.remaining.join(", ")}`, 1);
|
|
952
|
-
}
|
|
953
|
-
if (context.outputMode === "text") {
|
|
954
|
-
console.log(`Stopped process count: ${result.stopped}`);
|
|
955
|
-
}
|
|
956
|
-
return {
|
|
957
|
-
ok: true,
|
|
958
|
-
group: "run",
|
|
959
|
-
command,
|
|
960
|
-
details: {
|
|
961
|
-
stopped: result.stopped,
|
|
962
|
-
remaining: result.remaining
|
|
963
|
-
}
|
|
964
|
-
};
|
|
965
|
-
}
|
|
966
|
-
default:
|
|
967
|
-
throw new CliError2(`Unknown run command: ${command}`);
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
export {
|
|
971
|
-
executeRun
|
|
972
|
-
};
|