@h-rig/cli 0.0.6-alpha.7 → 0.0.6-alpha.71
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 +1 -1
- package/dist/bin/rig.js +4507 -1506
- package/dist/src/commands/_async-ui.js +152 -0
- package/dist/src/commands/_authority-runs.js +2 -3
- package/dist/src/commands/_cli-format.js +369 -0
- package/dist/src/commands/_connection-state.js +30 -11
- package/dist/src/commands/_doctor-checks.js +177 -43
- package/dist/src/commands/_help-catalog.js +485 -0
- package/dist/src/commands/_json-output.js +56 -0
- package/dist/src/commands/_operator-surface.js +220 -0
- package/dist/src/commands/_operator-view.js +595 -72
- package/dist/src/commands/_parsers.js +18 -11
- package/dist/src/commands/_pi-frontend.js +411 -0
- package/dist/src/commands/_pi-install.js +4 -3
- package/dist/src/commands/_policy.js +12 -5
- package/dist/src/commands/_preflight.js +187 -127
- package/dist/src/commands/_run-driver-helpers.js +75 -22
- package/dist/src/commands/_run-replay.js +142 -0
- package/dist/src/commands/_server-client.js +343 -60
- package/dist/src/commands/_snapshot-upload.js +160 -38
- package/dist/src/commands/_spinner.js +65 -0
- package/dist/src/commands/_task-picker.js +44 -16
- package/dist/src/commands/agent.js +39 -20
- package/dist/src/commands/browser.js +28 -21
- package/dist/src/commands/connect.js +146 -33
- package/dist/src/commands/dist.js +19 -12
- package/dist/src/commands/doctor.js +304 -44
- package/dist/src/commands/github.js +301 -52
- package/dist/src/commands/inbox.js +679 -72
- package/dist/src/commands/init.js +622 -118
- package/dist/src/commands/inspect.js +515 -32
- package/dist/src/commands/inspector.js +20 -13
- package/dist/src/commands/pi.js +177 -0
- package/dist/src/commands/plugin.js +95 -27
- package/dist/src/commands/profile-and-review.js +26 -19
- package/dist/src/commands/queue.js +32 -12
- package/dist/src/commands/remote.js +43 -36
- package/dist/src/commands/repo-git-harness.js +22 -15
- package/dist/src/commands/run.js +1162 -158
- package/dist/src/commands/server.js +373 -56
- package/dist/src/commands/setup.js +316 -62
- package/dist/src/commands/stats.js +1030 -0
- package/dist/src/commands/task-report-bug.js +29 -22
- package/dist/src/commands/task-run-driver.js +862 -129
- package/dist/src/commands/task.js +1423 -311
- package/dist/src/commands/test.js +15 -8
- package/dist/src/commands/workspace.js +18 -11
- package/dist/src/commands.js +4446 -1499
- package/dist/src/index.js +4502 -1504
- package/dist/src/launcher.js +77 -13
- package/dist/src/report-bug.js +3 -3
- package/dist/src/runner.js +16 -22
- package/package.json +10 -5
|
@@ -7,12 +7,19 @@ import { resolve } from "path";
|
|
|
7
7
|
|
|
8
8
|
// packages/cli/src/runner.ts
|
|
9
9
|
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
10
|
-
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
10
|
+
import { CliError as RuntimeCliError } from "@rig/runtime/control-plane/errors";
|
|
11
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
12
|
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
15
|
-
|
|
13
|
+
|
|
14
|
+
class CliError extends RuntimeCliError {
|
|
15
|
+
hint;
|
|
16
|
+
constructor(message, exitCode = 1, options = {}) {
|
|
17
|
+
super(message, exitCode);
|
|
18
|
+
if (options.hint?.trim()) {
|
|
19
|
+
this.hint = options.hint.trim();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
16
23
|
|
|
17
24
|
// packages/cli/src/commands/_parsers.ts
|
|
18
25
|
function parsePositiveInt(value, option, fallback) {
|
|
@@ -21,7 +28,7 @@ function parsePositiveInt(value, option, fallback) {
|
|
|
21
28
|
}
|
|
22
29
|
const parsed = Number.parseInt(value, 10);
|
|
23
30
|
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
24
|
-
throw new
|
|
31
|
+
throw new CliError(`Invalid ${option} value: ${value}`, 1, { hint: `Pass a positive integer, e.g. \`${option} 10\`.` });
|
|
25
32
|
}
|
|
26
33
|
return parsed;
|
|
27
34
|
}
|
|
@@ -31,17 +38,17 @@ function parseOptionalPositiveInt(value, option) {
|
|
|
31
38
|
}
|
|
32
39
|
const parsed = Number.parseInt(value, 10);
|
|
33
40
|
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
34
|
-
throw new
|
|
41
|
+
throw new CliError(`Invalid ${option} value: ${value}`, 1, { hint: `Pass a positive integer, e.g. \`${option} 10\`.` });
|
|
35
42
|
}
|
|
36
43
|
return parsed;
|
|
37
44
|
}
|
|
38
45
|
function parseRequiredPositiveInt(value, option) {
|
|
39
46
|
if (!value) {
|
|
40
|
-
throw new
|
|
47
|
+
throw new CliError(`Missing value for ${option}.`, 1, { hint: `Provide a value after ${option}, e.g. \`${option} 10\`.` });
|
|
41
48
|
}
|
|
42
49
|
const parsed = Number.parseInt(value, 10);
|
|
43
50
|
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
44
|
-
throw new
|
|
51
|
+
throw new CliError(`Invalid ${option} value: ${value}`, 1, { hint: `Pass a positive integer, e.g. \`${option} 10\`.` });
|
|
45
52
|
}
|
|
46
53
|
return parsed;
|
|
47
54
|
}
|
|
@@ -55,7 +62,7 @@ function parseAction(value) {
|
|
|
55
62
|
if (value === "pipeline") {
|
|
56
63
|
return "pipeline";
|
|
57
64
|
}
|
|
58
|
-
throw new
|
|
65
|
+
throw new CliError(`Invalid --action value: ${value}. Use validate, verify, or pipeline.`);
|
|
59
66
|
}
|
|
60
67
|
function parseIsolationMode(value, allowOff) {
|
|
61
68
|
if (!value) {
|
|
@@ -67,7 +74,7 @@ function parseIsolationMode(value, allowOff) {
|
|
|
67
74
|
if (allowOff && value === "off") {
|
|
68
75
|
return value;
|
|
69
76
|
}
|
|
70
|
-
throw new
|
|
77
|
+
throw new CliError(`Invalid isolation mode: ${value}. Use ${allowOff ? "off|" : ""}worktree.`);
|
|
71
78
|
}
|
|
72
79
|
function parseInstallScope(value) {
|
|
73
80
|
if (!value || value === "user") {
|
|
@@ -76,7 +83,7 @@ function parseInstallScope(value) {
|
|
|
76
83
|
if (value === "system") {
|
|
77
84
|
return "system";
|
|
78
85
|
}
|
|
79
|
-
throw new
|
|
86
|
+
throw new CliError(`Invalid --scope value: ${value}. Use user|system.`);
|
|
80
87
|
}
|
|
81
88
|
function resolveInstallDir(scope, explicitPath) {
|
|
82
89
|
if (explicitPath) {
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/cli/src/commands/_pi-frontend.ts
|
|
3
|
+
import { mkdtempSync, rmSync } from "fs";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
import { join } from "path";
|
|
6
|
+
import { main as runPiMain } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import createPiRigExtension from "@rig/pi-rig";
|
|
8
|
+
|
|
9
|
+
// packages/cli/src/commands/_server-client.ts
|
|
10
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
11
|
+
import { resolve as resolve2 } from "path";
|
|
12
|
+
|
|
13
|
+
// packages/cli/src/runner.ts
|
|
14
|
+
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
15
|
+
import { CliError as RuntimeCliError } from "@rig/runtime/control-plane/errors";
|
|
16
|
+
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
17
|
+
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
18
|
+
|
|
19
|
+
class CliError extends RuntimeCliError {
|
|
20
|
+
hint;
|
|
21
|
+
constructor(message, exitCode = 1, options = {}) {
|
|
22
|
+
super(message, exitCode);
|
|
23
|
+
if (options.hint?.trim()) {
|
|
24
|
+
this.hint = options.hint.trim();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// packages/cli/src/commands/_server-client.ts
|
|
30
|
+
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
31
|
+
|
|
32
|
+
// packages/cli/src/commands/_connection-state.ts
|
|
33
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
34
|
+
import { homedir } from "os";
|
|
35
|
+
import { dirname, resolve } from "path";
|
|
36
|
+
function resolveGlobalConnectionsPath(env = process.env) {
|
|
37
|
+
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
38
|
+
if (explicit)
|
|
39
|
+
return resolve(explicit);
|
|
40
|
+
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
41
|
+
if (stateDir)
|
|
42
|
+
return resolve(stateDir, "connections.json");
|
|
43
|
+
return resolve(homedir(), ".rig", "connections.json");
|
|
44
|
+
}
|
|
45
|
+
function resolveRepoConnectionPath(projectRoot) {
|
|
46
|
+
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
47
|
+
}
|
|
48
|
+
function readJsonFile(path) {
|
|
49
|
+
if (!existsSync(path))
|
|
50
|
+
return null;
|
|
51
|
+
try {
|
|
52
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new CliError(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1, { hint: "Fix or delete that file, then re-select a server with `rig server use <alias|local>`." });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function writeJsonFile(path, value) {
|
|
58
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
59
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}
|
|
60
|
+
`, "utf8");
|
|
61
|
+
}
|
|
62
|
+
function normalizeConnection(value) {
|
|
63
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
64
|
+
return null;
|
|
65
|
+
const record = value;
|
|
66
|
+
if (record.kind === "local")
|
|
67
|
+
return { kind: "local", mode: "auto" };
|
|
68
|
+
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
69
|
+
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
70
|
+
return { kind: "remote", baseUrl };
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function readGlobalConnections(options = {}) {
|
|
75
|
+
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
76
|
+
const payload = readJsonFile(path);
|
|
77
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
78
|
+
return { connections: {} };
|
|
79
|
+
}
|
|
80
|
+
const rawConnections = payload.connections;
|
|
81
|
+
const connections = {};
|
|
82
|
+
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
83
|
+
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
84
|
+
const connection = normalizeConnection(raw);
|
|
85
|
+
if (connection)
|
|
86
|
+
connections[alias] = connection;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { connections };
|
|
90
|
+
}
|
|
91
|
+
function readRepoConnection(projectRoot) {
|
|
92
|
+
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
93
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
94
|
+
return null;
|
|
95
|
+
const record = payload;
|
|
96
|
+
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
97
|
+
if (!selected)
|
|
98
|
+
return null;
|
|
99
|
+
return {
|
|
100
|
+
selected,
|
|
101
|
+
project: typeof record.project === "string" ? record.project : undefined,
|
|
102
|
+
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
103
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function writeRepoConnection(projectRoot, state) {
|
|
107
|
+
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
108
|
+
}
|
|
109
|
+
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
110
|
+
const repo = readRepoConnection(projectRoot);
|
|
111
|
+
if (!repo)
|
|
112
|
+
return null;
|
|
113
|
+
if (repo.selected === "local")
|
|
114
|
+
return { alias: "local", connection: { kind: "local", mode: "auto" }, serverProjectRoot: repo.serverProjectRoot };
|
|
115
|
+
const global = readGlobalConnections(options);
|
|
116
|
+
const connection = global.connections[repo.selected];
|
|
117
|
+
if (!connection) {
|
|
118
|
+
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
119
|
+
}
|
|
120
|
+
return { alias: repo.selected, connection, serverProjectRoot: repo.serverProjectRoot };
|
|
121
|
+
}
|
|
122
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
123
|
+
const repo = readRepoConnection(projectRoot);
|
|
124
|
+
if (!repo)
|
|
125
|
+
return;
|
|
126
|
+
writeRepoConnection(projectRoot, { ...repo, serverProjectRoot });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// packages/cli/src/commands/_server-client.ts
|
|
130
|
+
var scopedGitHubBearerTokens = new Map;
|
|
131
|
+
var serverPhaseListener = null;
|
|
132
|
+
function reportServerPhase(label) {
|
|
133
|
+
serverPhaseListener?.(label);
|
|
134
|
+
}
|
|
135
|
+
function cleanToken(value) {
|
|
136
|
+
const trimmed = value?.trim();
|
|
137
|
+
return trimmed ? trimmed : null;
|
|
138
|
+
}
|
|
139
|
+
function readPrivateRemoteSessionToken(projectRoot) {
|
|
140
|
+
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
141
|
+
if (!existsSync2(path))
|
|
142
|
+
return null;
|
|
143
|
+
try {
|
|
144
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
145
|
+
return cleanToken(typeof parsed.apiSessionToken === "string" ? parsed.apiSessionToken : typeof parsed.sessionToken === "string" ? parsed.sessionToken : undefined);
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function readGitHubBearerTokenForRemote(projectRoot) {
|
|
151
|
+
const scopedKey = resolve2(projectRoot);
|
|
152
|
+
if (scopedGitHubBearerTokens.has(scopedKey))
|
|
153
|
+
return scopedGitHubBearerTokens.get(scopedKey) ?? null;
|
|
154
|
+
const privateSession = readPrivateRemoteSessionToken(projectRoot);
|
|
155
|
+
if (privateSession)
|
|
156
|
+
return privateSession;
|
|
157
|
+
return cleanToken(process.env.RIG_SERVER_AUTH_TOKEN) ?? cleanToken(process.env.RIG_REMOTE_AUTH_TOKEN);
|
|
158
|
+
}
|
|
159
|
+
function readStoredGitHubAuthToken(projectRoot) {
|
|
160
|
+
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
161
|
+
if (!existsSync2(path))
|
|
162
|
+
return null;
|
|
163
|
+
try {
|
|
164
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
165
|
+
return cleanToken(typeof parsed.token === "string" ? parsed.token : undefined);
|
|
166
|
+
} catch {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function readLocalConnectionFallbackToken(projectRoot) {
|
|
171
|
+
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
172
|
+
}
|
|
173
|
+
async function ensureServerForCli(projectRoot) {
|
|
174
|
+
try {
|
|
175
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
176
|
+
if (selected?.connection.kind === "remote") {
|
|
177
|
+
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
178
|
+
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
179
|
+
const serverProjectRoot = selected.serverProjectRoot ?? await backfillRemoteServerProjectRoot(projectRoot, selected.connection.baseUrl, authToken);
|
|
180
|
+
return {
|
|
181
|
+
baseUrl: selected.connection.baseUrl,
|
|
182
|
+
authToken,
|
|
183
|
+
connectionKind: "remote",
|
|
184
|
+
serverProjectRoot
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
reportServerPhase("Starting local Rig server\u2026");
|
|
188
|
+
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
189
|
+
return {
|
|
190
|
+
baseUrl: connection.baseUrl,
|
|
191
|
+
authToken: connection.authToken ?? readLocalConnectionFallbackToken(projectRoot),
|
|
192
|
+
connectionKind: "local",
|
|
193
|
+
serverProjectRoot: resolve2(projectRoot)
|
|
194
|
+
};
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (error instanceof Error) {
|
|
197
|
+
throw new CliError(error.message, 1);
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
203
|
+
const repo = readRepoConnection(projectRoot);
|
|
204
|
+
const slug = repo?.project?.trim();
|
|
205
|
+
if (!slug)
|
|
206
|
+
return null;
|
|
207
|
+
try {
|
|
208
|
+
const response = await fetch(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`, {
|
|
209
|
+
headers: mergeHeaders(undefined, authToken)
|
|
210
|
+
});
|
|
211
|
+
if (!response.ok)
|
|
212
|
+
return null;
|
|
213
|
+
const payload = await response.json();
|
|
214
|
+
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
215
|
+
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
216
|
+
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
217
|
+
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
218
|
+
if (path)
|
|
219
|
+
writeRepoServerProjectRoot(projectRoot, path);
|
|
220
|
+
return path;
|
|
221
|
+
} catch {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function mergeHeaders(headers, authToken) {
|
|
226
|
+
const merged = new Headers(headers);
|
|
227
|
+
if (authToken) {
|
|
228
|
+
merged.set("authorization", `Bearer ${authToken}`);
|
|
229
|
+
}
|
|
230
|
+
return merged;
|
|
231
|
+
}
|
|
232
|
+
function diagnosticMessage(payload) {
|
|
233
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
234
|
+
return null;
|
|
235
|
+
const record = payload;
|
|
236
|
+
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
237
|
+
const messages = diagnostics.flatMap((entry) => {
|
|
238
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
239
|
+
return [];
|
|
240
|
+
const diagnostic = entry;
|
|
241
|
+
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
242
|
+
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
243
|
+
return message ? [`${kind}: ${message}`] : [];
|
|
244
|
+
});
|
|
245
|
+
return messages.length > 0 ? messages.join("; ") : null;
|
|
246
|
+
}
|
|
247
|
+
var serverReachabilityCache = new Map;
|
|
248
|
+
async function probeServerReachability(baseUrl, authToken) {
|
|
249
|
+
try {
|
|
250
|
+
const response = await fetch(`${baseUrl.replace(/\/+$/, "")}/api/server/status`, {
|
|
251
|
+
headers: mergeHeaders(undefined, authToken),
|
|
252
|
+
signal: AbortSignal.timeout(1500)
|
|
253
|
+
});
|
|
254
|
+
return response.ok;
|
|
255
|
+
} catch {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function cachedServerReachability(projectRoot, baseUrl, authToken) {
|
|
260
|
+
const key = resolve2(projectRoot);
|
|
261
|
+
const cached = serverReachabilityCache.get(key);
|
|
262
|
+
if (cached)
|
|
263
|
+
return cached;
|
|
264
|
+
const probe = probeServerReachability(baseUrl, authToken);
|
|
265
|
+
serverReachabilityCache.set(key, probe);
|
|
266
|
+
return probe;
|
|
267
|
+
}
|
|
268
|
+
function describeSelectedServer(projectRoot, server) {
|
|
269
|
+
try {
|
|
270
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
271
|
+
if (selected) {
|
|
272
|
+
return {
|
|
273
|
+
alias: selected.alias,
|
|
274
|
+
target: selected.connection.kind === "remote" ? selected.connection.baseUrl : server.baseUrl
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
} catch {}
|
|
278
|
+
return { alias: server.connectionKind === "remote" ? "remote" : "local", target: server.baseUrl };
|
|
279
|
+
}
|
|
280
|
+
async function buildServerFailureContext(projectRoot, server) {
|
|
281
|
+
const { alias, target } = describeSelectedServer(projectRoot, server);
|
|
282
|
+
const reachable = await cachedServerReachability(projectRoot, server.baseUrl, server.authToken);
|
|
283
|
+
const reachability = reachable ? "server is reachable" : "server is unreachable";
|
|
284
|
+
return {
|
|
285
|
+
contextLine: `Currently connected to: ${alias} at ${target} (${reachability}).`,
|
|
286
|
+
hint: "Check the selected server with `rig server status`, or switch with `rig server use <alias|local>`."
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
async function requestServerJson(context, pathname, init = {}) {
|
|
290
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
291
|
+
const headers = mergeHeaders(init.headers, server.authToken);
|
|
292
|
+
if (server.serverProjectRoot)
|
|
293
|
+
headers.set("x-rig-project-root", server.serverProjectRoot);
|
|
294
|
+
reportServerPhase(`${(init.method ?? "GET").toUpperCase()} ${pathname.split("?")[0]}\u2026`);
|
|
295
|
+
let response;
|
|
296
|
+
try {
|
|
297
|
+
response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
298
|
+
...init,
|
|
299
|
+
headers
|
|
300
|
+
});
|
|
301
|
+
} catch (error) {
|
|
302
|
+
const failure = await buildServerFailureContext(context.projectRoot, server);
|
|
303
|
+
throw new CliError(`Rig server request failed: ${error instanceof Error ? error.message : String(error)}
|
|
304
|
+
${failure.contextLine}`, 1, { hint: failure.hint });
|
|
305
|
+
}
|
|
306
|
+
const text = await response.text();
|
|
307
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
308
|
+
try {
|
|
309
|
+
return JSON.parse(text);
|
|
310
|
+
} catch {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
})() : null;
|
|
314
|
+
if (!response.ok) {
|
|
315
|
+
const diagnostics = diagnosticMessage(payload);
|
|
316
|
+
const detail = diagnostics ?? (text || response.statusText);
|
|
317
|
+
const failure = await buildServerFailureContext(context.projectRoot, server);
|
|
318
|
+
throw new CliError(`Rig server request failed (${response.status}): ${detail}
|
|
319
|
+
${failure.contextLine}`, 1, { hint: failure.hint });
|
|
320
|
+
}
|
|
321
|
+
return payload;
|
|
322
|
+
}
|
|
323
|
+
async function getRunDetailsViaServer(context, runId) {
|
|
324
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}`);
|
|
325
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
326
|
+
}
|
|
327
|
+
var RESUMABLE_RUN_STATUSES = new Set([
|
|
328
|
+
"created",
|
|
329
|
+
"preparing",
|
|
330
|
+
"running",
|
|
331
|
+
"validating",
|
|
332
|
+
"reviewing",
|
|
333
|
+
"stopped",
|
|
334
|
+
"failed",
|
|
335
|
+
"needs-attention",
|
|
336
|
+
"needs_attention"
|
|
337
|
+
]);
|
|
338
|
+
|
|
339
|
+
// packages/cli/src/commands/_pi-frontend.ts
|
|
340
|
+
function setTemporaryEnv(updates) {
|
|
341
|
+
const previous = new Map;
|
|
342
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
343
|
+
previous.set(key, process.env[key]);
|
|
344
|
+
process.env[key] = value;
|
|
345
|
+
}
|
|
346
|
+
return () => {
|
|
347
|
+
for (const [key, value] of previous) {
|
|
348
|
+
if (value === undefined)
|
|
349
|
+
delete process.env[key];
|
|
350
|
+
else
|
|
351
|
+
process.env[key] = value;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function buildOperatorPiEnv(input) {
|
|
356
|
+
return {
|
|
357
|
+
PI_CODING_AGENT_SESSION_DIR: input.sessionDir,
|
|
358
|
+
PI_SKIP_VERSION_CHECK: "1",
|
|
359
|
+
RIG_PI_OPERATOR_SESSION: "1",
|
|
360
|
+
RIG_RUN_ID: input.runId,
|
|
361
|
+
RIG_SERVER_URL: input.serverUrl,
|
|
362
|
+
...input.authToken ? { RIG_AUTH_TOKEN: input.authToken } : {},
|
|
363
|
+
...input.serverProjectRoot ? { RIG_PROJECT_ROOT: input.serverProjectRoot } : {}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
async function attachRunBundledPiFrontend(context, input) {
|
|
367
|
+
const tempSessionDir = mkdtempSync(join(tmpdir(), "rig-pi-frontend-sessions-"));
|
|
368
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
369
|
+
const restoreEnv = setTemporaryEnv(buildOperatorPiEnv({
|
|
370
|
+
runId: input.runId,
|
|
371
|
+
serverUrl: server.baseUrl,
|
|
372
|
+
authToken: server.authToken,
|
|
373
|
+
serverProjectRoot: server.serverProjectRoot,
|
|
374
|
+
sessionDir: tempSessionDir
|
|
375
|
+
}));
|
|
376
|
+
const piRigExtensionFactory = (pi) => {
|
|
377
|
+
createPiRigExtension(pi);
|
|
378
|
+
};
|
|
379
|
+
let detached = false;
|
|
380
|
+
try {
|
|
381
|
+
await runPiMain([
|
|
382
|
+
"--no-extensions",
|
|
383
|
+
"--no-skills",
|
|
384
|
+
"--no-prompt-templates",
|
|
385
|
+
"--no-context-files"
|
|
386
|
+
], {
|
|
387
|
+
extensionFactories: [piRigExtensionFactory]
|
|
388
|
+
});
|
|
389
|
+
detached = true;
|
|
390
|
+
} finally {
|
|
391
|
+
restoreEnv();
|
|
392
|
+
rmSync(tempSessionDir, { recursive: true, force: true });
|
|
393
|
+
}
|
|
394
|
+
let run = { runId: input.runId, status: "unknown" };
|
|
395
|
+
try {
|
|
396
|
+
run = await getRunDetailsViaServer(context, input.runId);
|
|
397
|
+
} catch {}
|
|
398
|
+
return {
|
|
399
|
+
run,
|
|
400
|
+
logs: [],
|
|
401
|
+
timeline: [],
|
|
402
|
+
timelineCursor: null,
|
|
403
|
+
steered: input.steered === true,
|
|
404
|
+
detached,
|
|
405
|
+
rendered: "stock Pi operator console with the pi-rig extension"
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
export {
|
|
409
|
+
buildOperatorPiEnv,
|
|
410
|
+
attachRunBundledPiFrontend
|
|
411
|
+
};
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
import { existsSync, readFileSync, rmSync } from "fs";
|
|
4
4
|
import { homedir } from "os";
|
|
5
5
|
import { resolve } from "path";
|
|
6
|
-
var PI_RIG_PACKAGE_NAME = "@rig/pi-rig";
|
|
6
|
+
var PI_RIG_PACKAGE_NAME = "@h-rig/pi-rig";
|
|
7
|
+
var LEGACY_PI_RIG_PACKAGE_NAME = "@rig/pi-rig";
|
|
7
8
|
var LEGACY_PI_RIG_MARKER = `// Managed by Rig. Source package: @rig/pi-rig.
|
|
8
9
|
export { default } from '@rig/pi-rig';
|
|
9
10
|
`;
|
|
@@ -31,7 +32,7 @@ function resolvePiHomeDir(inputHomeDir) {
|
|
|
31
32
|
function piListContainsPiRig(output) {
|
|
32
33
|
return output.split(/\r?\n/).some((line) => {
|
|
33
34
|
const normalized = line.trim();
|
|
34
|
-
return normalized.includes(PI_RIG_PACKAGE_NAME) || /(?:^|[\\/])packages[\\/]pi-rig(?:$|\s)/.test(normalized);
|
|
35
|
+
return normalized.includes(PI_RIG_PACKAGE_NAME) || normalized.includes(LEGACY_PI_RIG_PACKAGE_NAME) || /(?:^|[\\/])packages[\\/]pi-rig(?:$|\s)/.test(normalized);
|
|
35
36
|
});
|
|
36
37
|
}
|
|
37
38
|
async function safeRun(runner, command, options) {
|
|
@@ -147,7 +148,7 @@ async function ensureRemotePiRigInstalled(input) {
|
|
|
147
148
|
const payload = await input.requestJson("/api/pi-rig/install", {
|
|
148
149
|
method: "POST",
|
|
149
150
|
headers: { "content-type": "application/json" },
|
|
150
|
-
body: JSON.stringify({ package:
|
|
151
|
+
body: JSON.stringify({ package: PI_RIG_PACKAGE_NAME, scope: "global" })
|
|
151
152
|
});
|
|
152
153
|
const record = payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
153
154
|
const piOk = record.piOk === true || record.ok === true;
|
|
@@ -5,12 +5,19 @@ import { resolve } from "path";
|
|
|
5
5
|
|
|
6
6
|
// packages/cli/src/runner.ts
|
|
7
7
|
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
8
|
-
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
8
|
+
import { CliError as RuntimeCliError } from "@rig/runtime/control-plane/errors";
|
|
9
9
|
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
10
|
-
import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
|
|
11
|
-
import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
|
|
12
10
|
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
13
|
-
|
|
11
|
+
|
|
12
|
+
class CliError extends RuntimeCliError {
|
|
13
|
+
hint;
|
|
14
|
+
constructor(message, exitCode = 1, options = {}) {
|
|
15
|
+
super(message, exitCode);
|
|
16
|
+
if (options.hint?.trim()) {
|
|
17
|
+
this.hint = options.hint.trim();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
14
21
|
function formatCommand(parts) {
|
|
15
22
|
return parts.map((part) => /[^a-zA-Z0-9_./:-]/.test(part) ? JSON.stringify(part) : part).join(" ");
|
|
16
23
|
}
|
|
@@ -69,7 +76,7 @@ async function enforceNativeCommandPolicy(context, args, options) {
|
|
|
69
76
|
appendControlledBashAudit(context, mode, command, args, matchedRuleIds, action === "block" ? "blocked" : action === "warn" ? "warn" : undefined);
|
|
70
77
|
}
|
|
71
78
|
if (action === "block") {
|
|
72
|
-
throw new
|
|
79
|
+
throw new CliError(`Policy blocked command: ${command}`, 126, { hint: "Review rig/policy/policy.json, or re-run with `--policy-mode observe` to log instead of block." });
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
export {
|