@h-rig/cli 0.0.6-alpha.1 → 0.0.6-alpha.100
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 +18 -19
- package/dist/bin/build-rig-binaries.d.ts +2 -0
- package/dist/bin/build-rig-binaries.js +22 -10
- package/dist/bin/rig.d.ts +72 -0
- package/dist/bin/rig.js +15742 -7203
- package/dist/config/rig-default-config.yml +5 -0
- package/dist/src/app/drone-ui.d.ts +34 -0
- package/dist/src/app/drone-ui.js +278 -0
- package/dist/src/commands/_async-ui.d.ts +10 -0
- package/dist/src/commands/_async-ui.js +121 -0
- package/dist/src/commands/_cli-format.d.ts +56 -0
- package/dist/src/commands/_cli-format.js +319 -0
- package/dist/src/commands/_config-env.d.ts +18 -0
- package/dist/src/commands/_config-env.js +22 -0
- package/dist/src/commands/_connection-state.d.ts +54 -0
- package/dist/src/commands/_connection-state.js +75 -11
- package/dist/src/commands/_doctor-checks.d.ts +46 -0
- package/dist/src/commands/_doctor-checks.js +95 -244
- package/dist/src/commands/_help-catalog.d.ts +29 -0
- package/dist/src/commands/_help-catalog.js +146 -0
- package/dist/src/commands/_inprocess-services.d.ts +33 -0
- package/dist/src/commands/_inprocess-services.js +102 -0
- package/dist/src/commands/_json-output.d.ts +11 -0
- package/dist/src/commands/_json-output.js +60 -0
- package/dist/src/commands/_lazy-reconcile.d.ts +34 -0
- package/dist/src/commands/_lazy-reconcile.js +102 -0
- package/dist/src/commands/_parsers.d.ts +15 -0
- package/dist/src/commands/_parsers.js +18 -11
- package/dist/src/commands/_paths.d.ts +11 -0
- package/dist/src/commands/_paths.js +1 -1
- package/dist/src/commands/_pi-frontend.d.ts +35 -0
- package/dist/src/commands/_pi-frontend.js +64 -0
- package/dist/src/commands/_pi-install.d.ts +42 -0
- package/dist/src/commands/_pi-install.js +19 -36
- package/dist/src/commands/_policy.d.ts +8 -0
- package/dist/src/commands/_policy.js +67 -19
- package/dist/src/commands/_probes.d.ts +1 -0
- package/dist/src/commands/_run-bridge.d.ts +114 -0
- package/dist/src/commands/_run-bridge.js +387 -0
- package/dist/src/commands/_run-diagnostics.d.ts +9 -0
- package/dist/src/commands/_run-diagnostics.js +51 -0
- package/dist/src/commands/_run-driver-helpers.d.ts +26 -0
- package/dist/src/commands/_run-driver-helpers.js +79 -230
- package/dist/src/commands/_run-projection.d.ts +50 -0
- package/dist/src/commands/_run-projection.js +349 -0
- package/dist/src/commands/_run-subcommands.d.ts +3 -0
- package/dist/src/commands/_run-subcommands.js +31 -0
- package/dist/src/commands/_spinner.d.ts +25 -0
- package/dist/src/commands/_spinner.js +65 -0
- package/dist/src/commands/agent.d.ts +3 -0
- package/dist/src/commands/agent.js +8690 -235
- package/dist/src/commands/config.d.ts +3 -0
- package/dist/src/commands/config.js +181 -0
- package/dist/src/commands/dist.d.ts +28 -0
- package/dist/src/commands/dist.js +44 -29
- package/dist/src/commands/doctor.d.ts +3 -0
- package/dist/src/commands/doctor.js +214 -245
- package/dist/src/commands/github.d.ts +3 -0
- package/dist/src/commands/github.js +237 -171
- package/dist/src/commands/inbox.d.ts +28 -0
- package/dist/src/commands/inbox.js +530 -111
- package/dist/src/commands/init.d.ts +64 -0
- package/dist/src/commands/init.js +941 -654
- package/dist/src/commands/inspect.d.ts +20 -0
- package/dist/src/commands/inspect.js +709 -119
- package/dist/src/commands/pi.d.ts +3 -0
- package/dist/src/commands/pi.js +177 -0
- package/dist/src/commands/plugin.d.ts +16 -0
- package/dist/src/commands/plugin.js +576 -29
- package/dist/src/commands/profile-and-review.d.ts +4 -0
- package/dist/src/commands/profile-and-review.js +112 -67
- package/dist/src/commands/queue.d.ts +3 -0
- package/dist/src/commands/queue.js +20 -20
- package/dist/src/commands/remote.d.ts +3 -0
- package/dist/src/commands/remote.js +883 -50
- package/dist/src/commands/repo-git-harness.d.ts +5 -0
- package/dist/src/commands/repo-git-harness.js +78 -28
- package/dist/src/commands/run.d.ts +21 -0
- package/dist/src/commands/run.js +17732 -618
- package/dist/src/commands/server.d.ts +3 -0
- package/dist/src/commands/server.js +166 -285
- package/dist/src/commands/setup.d.ts +16 -0
- package/dist/src/commands/setup.js +230 -267
- package/dist/src/commands/stats.d.ts +15 -0
- package/dist/src/commands/stats.js +632 -0
- package/dist/src/commands/task-run-driver.d.ts +94 -0
- package/dist/src/commands/task-run-driver.js +120 -1856
- package/dist/src/commands/task.d.ts +35 -0
- package/dist/src/commands/task.js +676 -1183
- package/dist/src/commands/test.d.ts +3 -0
- package/dist/src/commands/test.js +15 -8
- package/dist/src/commands/triage.d.ts +11 -0
- package/dist/src/commands/triage.js +227 -0
- package/dist/src/commands/workspace.d.ts +3 -0
- package/dist/src/commands/workspace.js +18 -11
- package/dist/src/commands.d.ts +13 -0
- package/dist/src/commands.js +16760 -7779
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +16880 -8223
- package/dist/src/launcher.d.ts +61 -0
- package/dist/src/launcher.js +81 -13
- package/dist/src/operator-cli.d.ts +2 -0
- package/dist/src/operator-cli.js +17867 -0
- package/dist/src/operator-entry.d.ts +1 -0
- package/dist/src/operator-entry.js +3 -0
- package/dist/src/runner.d.ts +47 -0
- package/dist/src/runner.js +19 -24
- package/dist/src/withMutedConsole.d.ts +2 -0
- package/package.json +21 -7
- package/dist/src/commands/_authority-runs.js +0 -110
- package/dist/src/commands/_operator-view.js +0 -322
- package/dist/src/commands/_preflight.js +0 -460
- package/dist/src/commands/_server-client.js +0 -364
- package/dist/src/commands/_snapshot-upload.js +0 -313
- package/dist/src/commands/_task-picker.js +0 -48
- package/dist/src/commands/browser.js +0 -890
- package/dist/src/commands/connect.js +0 -180
- package/dist/src/commands/inspector.js +0 -256
- package/dist/src/commands/task-report-bug.js +0 -1083
- package/dist/src/report-bug.js +0 -260
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
// packages/cli/src/commands/_server-client.ts
|
|
3
|
-
import { spawnSync } from "child_process";
|
|
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
|
-
|
|
14
|
-
// packages/cli/src/commands/_server-client.ts
|
|
15
|
-
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
16
|
-
|
|
17
|
-
// packages/cli/src/commands/_connection-state.ts
|
|
18
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
19
|
-
import { homedir } from "os";
|
|
20
|
-
import { dirname, resolve } from "path";
|
|
21
|
-
function resolveGlobalConnectionsPath(env = process.env) {
|
|
22
|
-
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
23
|
-
if (explicit)
|
|
24
|
-
return resolve(explicit);
|
|
25
|
-
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
26
|
-
if (stateDir)
|
|
27
|
-
return resolve(stateDir, "connections.json");
|
|
28
|
-
return resolve(homedir(), ".rig", "connections.json");
|
|
29
|
-
}
|
|
30
|
-
function resolveRepoConnectionPath(projectRoot) {
|
|
31
|
-
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
32
|
-
}
|
|
33
|
-
function readJsonFile(path) {
|
|
34
|
-
if (!existsSync(path))
|
|
35
|
-
return null;
|
|
36
|
-
try {
|
|
37
|
-
return JSON.parse(readFileSync(path, "utf8"));
|
|
38
|
-
} catch (error) {
|
|
39
|
-
throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function normalizeConnection(value) {
|
|
43
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
44
|
-
return null;
|
|
45
|
-
const record = value;
|
|
46
|
-
if (record.kind === "local")
|
|
47
|
-
return { kind: "local", mode: "auto" };
|
|
48
|
-
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
49
|
-
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
50
|
-
return { kind: "remote", baseUrl };
|
|
51
|
-
}
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
function readGlobalConnections(options = {}) {
|
|
55
|
-
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
56
|
-
const payload = readJsonFile(path);
|
|
57
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
58
|
-
return { connections: {} };
|
|
59
|
-
}
|
|
60
|
-
const rawConnections = payload.connections;
|
|
61
|
-
const connections = {};
|
|
62
|
-
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
63
|
-
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
64
|
-
const connection = normalizeConnection(raw);
|
|
65
|
-
if (connection)
|
|
66
|
-
connections[alias] = connection;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return { connections };
|
|
70
|
-
}
|
|
71
|
-
function readRepoConnection(projectRoot) {
|
|
72
|
-
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
73
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
74
|
-
return null;
|
|
75
|
-
const record = payload;
|
|
76
|
-
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
77
|
-
if (!selected)
|
|
78
|
-
return null;
|
|
79
|
-
return {
|
|
80
|
-
selected,
|
|
81
|
-
project: typeof record.project === "string" ? record.project : undefined,
|
|
82
|
-
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
86
|
-
const repo = readRepoConnection(projectRoot);
|
|
87
|
-
if (!repo)
|
|
88
|
-
return null;
|
|
89
|
-
if (repo.selected === "local")
|
|
90
|
-
return { alias: "local", connection: { kind: "local", mode: "auto" } };
|
|
91
|
-
const global = readGlobalConnections(options);
|
|
92
|
-
const connection = global.connections[repo.selected];
|
|
93
|
-
if (!connection) {
|
|
94
|
-
throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
|
|
95
|
-
}
|
|
96
|
-
return { alias: repo.selected, connection };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// packages/cli/src/commands/_server-client.ts
|
|
100
|
-
var cachedGitHubBearerToken;
|
|
101
|
-
function cleanToken(value) {
|
|
102
|
-
const trimmed = value?.trim();
|
|
103
|
-
return trimmed ? trimmed : null;
|
|
104
|
-
}
|
|
105
|
-
function setGitHubBearerTokenForCurrentProcess(token) {
|
|
106
|
-
cachedGitHubBearerToken = cleanToken(token ?? undefined);
|
|
107
|
-
}
|
|
108
|
-
function readGitHubBearerTokenForRemote() {
|
|
109
|
-
if (cachedGitHubBearerToken !== undefined)
|
|
110
|
-
return cachedGitHubBearerToken;
|
|
111
|
-
const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
|
|
112
|
-
if (envToken) {
|
|
113
|
-
cachedGitHubBearerToken = envToken;
|
|
114
|
-
return cachedGitHubBearerToken;
|
|
115
|
-
}
|
|
116
|
-
const result = spawnSync("gh", ["auth", "token"], {
|
|
117
|
-
encoding: "utf8",
|
|
118
|
-
timeout: 5000,
|
|
119
|
-
stdio: ["ignore", "pipe", "ignore"]
|
|
120
|
-
});
|
|
121
|
-
cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
|
|
122
|
-
return cachedGitHubBearerToken;
|
|
123
|
-
}
|
|
124
|
-
async function ensureServerForCli(projectRoot) {
|
|
125
|
-
try {
|
|
126
|
-
const selected = resolveSelectedConnection(projectRoot);
|
|
127
|
-
if (selected?.connection.kind === "remote") {
|
|
128
|
-
return {
|
|
129
|
-
baseUrl: selected.connection.baseUrl,
|
|
130
|
-
authToken: readGitHubBearerTokenForRemote(),
|
|
131
|
-
connectionKind: "remote"
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
135
|
-
return {
|
|
136
|
-
baseUrl: connection.baseUrl,
|
|
137
|
-
authToken: connection.authToken,
|
|
138
|
-
connectionKind: "local"
|
|
139
|
-
};
|
|
140
|
-
} catch (error) {
|
|
141
|
-
if (error instanceof Error) {
|
|
142
|
-
throw new CliError2(error.message, 1);
|
|
143
|
-
}
|
|
144
|
-
throw error;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
function appendTaskFilterParams(url, filters) {
|
|
148
|
-
if (filters.assignee)
|
|
149
|
-
url.searchParams.set("assignee", filters.assignee);
|
|
150
|
-
if (filters.state)
|
|
151
|
-
url.searchParams.set("state", filters.state);
|
|
152
|
-
if (filters.status)
|
|
153
|
-
url.searchParams.set("status", filters.status);
|
|
154
|
-
if (filters.limit !== undefined)
|
|
155
|
-
url.searchParams.set("limit", String(filters.limit));
|
|
156
|
-
}
|
|
157
|
-
function mergeHeaders(headers, authToken) {
|
|
158
|
-
const merged = new Headers(headers);
|
|
159
|
-
if (authToken) {
|
|
160
|
-
merged.set("authorization", `Bearer ${authToken}`);
|
|
161
|
-
}
|
|
162
|
-
return merged;
|
|
163
|
-
}
|
|
164
|
-
function diagnosticMessage(payload) {
|
|
165
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
166
|
-
return null;
|
|
167
|
-
const record = payload;
|
|
168
|
-
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
169
|
-
const messages = diagnostics.flatMap((entry) => {
|
|
170
|
-
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
171
|
-
return [];
|
|
172
|
-
const diagnostic = entry;
|
|
173
|
-
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
174
|
-
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
175
|
-
return message ? [`${kind}: ${message}`] : [];
|
|
176
|
-
});
|
|
177
|
-
return messages.length > 0 ? messages.join("; ") : null;
|
|
178
|
-
}
|
|
179
|
-
async function requestServerJson(context, pathname, init = {}) {
|
|
180
|
-
const server = await ensureServerForCli(context.projectRoot);
|
|
181
|
-
const response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
182
|
-
...init,
|
|
183
|
-
headers: mergeHeaders(init.headers, server.authToken)
|
|
184
|
-
});
|
|
185
|
-
const text = await response.text();
|
|
186
|
-
const payload = text.trim().length > 0 ? (() => {
|
|
187
|
-
try {
|
|
188
|
-
return JSON.parse(text);
|
|
189
|
-
} catch {
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
})() : null;
|
|
193
|
-
if (!response.ok) {
|
|
194
|
-
const diagnostics = diagnosticMessage(payload);
|
|
195
|
-
const detail = diagnostics ?? (text || response.statusText);
|
|
196
|
-
throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
|
|
197
|
-
}
|
|
198
|
-
return payload;
|
|
199
|
-
}
|
|
200
|
-
async function listWorkspaceTasksViaServer(context, filters = {}) {
|
|
201
|
-
const url = new URL("http://rig.local/api/workspace/tasks");
|
|
202
|
-
appendTaskFilterParams(url, filters);
|
|
203
|
-
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
204
|
-
if (!Array.isArray(payload)) {
|
|
205
|
-
throw new CliError2("Rig server returned an invalid task list payload.", 1);
|
|
206
|
-
}
|
|
207
|
-
return payload.flatMap((entry) => entry && typeof entry === "object" && !Array.isArray(entry) ? [entry] : []);
|
|
208
|
-
}
|
|
209
|
-
async function getWorkspaceTaskViaServer(context, taskId) {
|
|
210
|
-
const payload = await requestServerJson(context, `/api/workspace/tasks/${encodeURIComponent(taskId)}`);
|
|
211
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
212
|
-
return null;
|
|
213
|
-
const task = payload.task;
|
|
214
|
-
return task && typeof task === "object" && !Array.isArray(task) ? task : null;
|
|
215
|
-
}
|
|
216
|
-
async function selectNextWorkspaceTaskViaServer(context, filters = {}) {
|
|
217
|
-
const url = new URL("http://rig.local/api/workspace/tasks/next");
|
|
218
|
-
appendTaskFilterParams(url, filters);
|
|
219
|
-
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
220
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
221
|
-
throw new CliError2("Rig server returned an invalid next-task payload.", 1);
|
|
222
|
-
}
|
|
223
|
-
const record = payload;
|
|
224
|
-
const rawTask = record.task;
|
|
225
|
-
const task = rawTask && typeof rawTask === "object" && !Array.isArray(rawTask) ? rawTask : null;
|
|
226
|
-
const count = typeof record.count === "number" && Number.isFinite(record.count) ? record.count : task ? 1 : 0;
|
|
227
|
-
return { task, count };
|
|
228
|
-
}
|
|
229
|
-
async function getGitHubAuthStatusViaServer(context) {
|
|
230
|
-
const payload = await requestServerJson(context, "/api/github/auth/status");
|
|
231
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
232
|
-
}
|
|
233
|
-
async function postGitHubTokenViaServer(context, token, options = {}) {
|
|
234
|
-
const payload = await requestServerJson(context, "/api/github/auth/token", {
|
|
235
|
-
method: "POST",
|
|
236
|
-
headers: { "content-type": "application/json" },
|
|
237
|
-
body: JSON.stringify({ token, selectedRepo: options.selectedRepo })
|
|
238
|
-
});
|
|
239
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
240
|
-
}
|
|
241
|
-
async function prepareRemoteCheckoutViaServer(context, input) {
|
|
242
|
-
const payload = await requestServerJson(context, `/api/projects/${encodeURIComponent(input.repoSlug)}/prepare-checkout`, {
|
|
243
|
-
method: "POST",
|
|
244
|
-
headers: { "content-type": "application/json" },
|
|
245
|
-
body: JSON.stringify({ checkout: input.checkout, repoUrl: input.repoUrl, baseDir: input.baseDir })
|
|
246
|
-
});
|
|
247
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
248
|
-
}
|
|
249
|
-
async function registerProjectViaServer(context, input) {
|
|
250
|
-
const payload = await requestServerJson(context, "/api/projects", {
|
|
251
|
-
method: "POST",
|
|
252
|
-
headers: { "content-type": "application/json" },
|
|
253
|
-
body: JSON.stringify(input)
|
|
254
|
-
});
|
|
255
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
256
|
-
}
|
|
257
|
-
function sleep(ms) {
|
|
258
|
-
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
259
|
-
}
|
|
260
|
-
async function switchServerProjectRootViaServer(context, projectRoot, options = {}) {
|
|
261
|
-
const switched = await requestServerJson(context, "/api/server/project-root", {
|
|
262
|
-
method: "POST",
|
|
263
|
-
headers: { "content-type": "application/json" },
|
|
264
|
-
body: JSON.stringify({ projectRoot })
|
|
265
|
-
});
|
|
266
|
-
const timeoutMs = options.timeoutMs ?? 30000;
|
|
267
|
-
const pollMs = options.pollMs ?? 1000;
|
|
268
|
-
const deadline = Date.now() + timeoutMs;
|
|
269
|
-
let lastError;
|
|
270
|
-
while (Date.now() < deadline) {
|
|
271
|
-
try {
|
|
272
|
-
const status = await requestServerJson(context, "/api/server/status");
|
|
273
|
-
if (status && typeof status === "object" && !Array.isArray(status)) {
|
|
274
|
-
const record = status;
|
|
275
|
-
if (record.projectRoot === projectRoot) {
|
|
276
|
-
return { ok: true, switched, status: record };
|
|
277
|
-
}
|
|
278
|
-
lastError = `server projectRoot=${String(record.projectRoot ?? "unknown")}`;
|
|
279
|
-
}
|
|
280
|
-
} catch (error) {
|
|
281
|
-
lastError = error;
|
|
282
|
-
}
|
|
283
|
-
await sleep(pollMs);
|
|
284
|
-
}
|
|
285
|
-
throw new CliError2(`Rig server did not switch to ${projectRoot} before timeout (${lastError instanceof Error ? lastError.message : String(lastError ?? "no status")}).`, 1);
|
|
286
|
-
}
|
|
287
|
-
async function getRunDetailsViaServer(context, runId) {
|
|
288
|
-
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}`);
|
|
289
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
290
|
-
}
|
|
291
|
-
async function getRunLogsViaServer(context, runId, options = {}) {
|
|
292
|
-
const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/logs`);
|
|
293
|
-
if (options.limit !== undefined)
|
|
294
|
-
url.searchParams.set("limit", String(options.limit));
|
|
295
|
-
if (options.cursor)
|
|
296
|
-
url.searchParams.set("cursor", options.cursor);
|
|
297
|
-
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
298
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
|
|
299
|
-
}
|
|
300
|
-
async function stopRunViaServer(context, runId) {
|
|
301
|
-
const payload = await requestServerJson(context, "/api/runs/stop", {
|
|
302
|
-
method: "POST",
|
|
303
|
-
headers: { "content-type": "application/json" },
|
|
304
|
-
body: JSON.stringify({ runId })
|
|
305
|
-
});
|
|
306
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true, runId };
|
|
307
|
-
}
|
|
308
|
-
async function steerRunViaServer(context, runId, message) {
|
|
309
|
-
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/steer`, {
|
|
310
|
-
method: "POST",
|
|
311
|
-
headers: { "content-type": "application/json" },
|
|
312
|
-
body: JSON.stringify({ message })
|
|
313
|
-
});
|
|
314
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
|
|
315
|
-
}
|
|
316
|
-
async function submitTaskRunViaServer(context, input) {
|
|
317
|
-
const isTaskRun = Boolean(input.taskId);
|
|
318
|
-
const endpoint = isTaskRun ? "/api/runs/task" : "/api/runs/adhoc";
|
|
319
|
-
const payload = await requestServerJson(context, endpoint, {
|
|
320
|
-
method: "POST",
|
|
321
|
-
headers: {
|
|
322
|
-
"content-type": "application/json"
|
|
323
|
-
},
|
|
324
|
-
body: JSON.stringify({
|
|
325
|
-
runId: input.runId,
|
|
326
|
-
taskId: input.taskId,
|
|
327
|
-
title: input.title,
|
|
328
|
-
runtimeAdapter: input.runtimeAdapter,
|
|
329
|
-
model: input.model,
|
|
330
|
-
runtimeMode: input.runtimeMode,
|
|
331
|
-
interactionMode: input.interactionMode,
|
|
332
|
-
initialPrompt: input.initialPrompt,
|
|
333
|
-
baselineMode: input.baselineMode,
|
|
334
|
-
prMode: input.prMode,
|
|
335
|
-
executionTarget: "local"
|
|
336
|
-
})
|
|
337
|
-
});
|
|
338
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
339
|
-
throw new CliError2("Rig server returned an invalid run submission payload.", 1);
|
|
340
|
-
}
|
|
341
|
-
const runId = payload.runId;
|
|
342
|
-
if (typeof runId !== "string" || runId.trim().length === 0) {
|
|
343
|
-
throw new CliError2("Rig server returned no runId for the submitted run.", 1);
|
|
344
|
-
}
|
|
345
|
-
return { runId };
|
|
346
|
-
}
|
|
347
|
-
export {
|
|
348
|
-
switchServerProjectRootViaServer,
|
|
349
|
-
submitTaskRunViaServer,
|
|
350
|
-
stopRunViaServer,
|
|
351
|
-
steerRunViaServer,
|
|
352
|
-
setGitHubBearerTokenForCurrentProcess,
|
|
353
|
-
selectNextWorkspaceTaskViaServer,
|
|
354
|
-
requestServerJson,
|
|
355
|
-
registerProjectViaServer,
|
|
356
|
-
prepareRemoteCheckoutViaServer,
|
|
357
|
-
postGitHubTokenViaServer,
|
|
358
|
-
listWorkspaceTasksViaServer,
|
|
359
|
-
getWorkspaceTaskViaServer,
|
|
360
|
-
getRunLogsViaServer,
|
|
361
|
-
getRunDetailsViaServer,
|
|
362
|
-
getGitHubAuthStatusViaServer,
|
|
363
|
-
ensureServerForCli
|
|
364
|
-
};
|
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
// packages/cli/src/commands/_snapshot-upload.ts
|
|
3
|
-
import { mkdir, readdir, readFile, writeFile } from "fs/promises";
|
|
4
|
-
import { dirname as dirname2, resolve as resolve2, relative, sep } from "path";
|
|
5
|
-
|
|
6
|
-
// packages/cli/src/commands/_server-client.ts
|
|
7
|
-
import { spawnSync } from "child_process";
|
|
8
|
-
|
|
9
|
-
// packages/cli/src/runner.ts
|
|
10
|
-
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
11
|
-
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
12
|
-
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
13
|
-
import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
|
|
14
|
-
import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
|
|
15
|
-
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
16
|
-
import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
|
|
17
|
-
|
|
18
|
-
// packages/cli/src/commands/_server-client.ts
|
|
19
|
-
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
20
|
-
|
|
21
|
-
// packages/cli/src/commands/_connection-state.ts
|
|
22
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
23
|
-
import { homedir } from "os";
|
|
24
|
-
import { dirname, resolve } from "path";
|
|
25
|
-
function resolveGlobalConnectionsPath(env = process.env) {
|
|
26
|
-
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
27
|
-
if (explicit)
|
|
28
|
-
return resolve(explicit);
|
|
29
|
-
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
30
|
-
if (stateDir)
|
|
31
|
-
return resolve(stateDir, "connections.json");
|
|
32
|
-
return resolve(homedir(), ".rig", "connections.json");
|
|
33
|
-
}
|
|
34
|
-
function resolveRepoConnectionPath(projectRoot) {
|
|
35
|
-
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
36
|
-
}
|
|
37
|
-
function readJsonFile(path) {
|
|
38
|
-
if (!existsSync(path))
|
|
39
|
-
return null;
|
|
40
|
-
try {
|
|
41
|
-
return JSON.parse(readFileSync(path, "utf8"));
|
|
42
|
-
} catch (error) {
|
|
43
|
-
throw new CliError2(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function normalizeConnection(value) {
|
|
47
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
48
|
-
return null;
|
|
49
|
-
const record = value;
|
|
50
|
-
if (record.kind === "local")
|
|
51
|
-
return { kind: "local", mode: "auto" };
|
|
52
|
-
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
53
|
-
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
54
|
-
return { kind: "remote", baseUrl };
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
function readGlobalConnections(options = {}) {
|
|
59
|
-
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
60
|
-
const payload = readJsonFile(path);
|
|
61
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
62
|
-
return { connections: {} };
|
|
63
|
-
}
|
|
64
|
-
const rawConnections = payload.connections;
|
|
65
|
-
const connections = {};
|
|
66
|
-
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
67
|
-
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
68
|
-
const connection = normalizeConnection(raw);
|
|
69
|
-
if (connection)
|
|
70
|
-
connections[alias] = connection;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return { connections };
|
|
74
|
-
}
|
|
75
|
-
function readRepoConnection(projectRoot) {
|
|
76
|
-
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
77
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
78
|
-
return null;
|
|
79
|
-
const record = payload;
|
|
80
|
-
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
81
|
-
if (!selected)
|
|
82
|
-
return null;
|
|
83
|
-
return {
|
|
84
|
-
selected,
|
|
85
|
-
project: typeof record.project === "string" ? record.project : undefined,
|
|
86
|
-
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
90
|
-
const repo = readRepoConnection(projectRoot);
|
|
91
|
-
if (!repo)
|
|
92
|
-
return null;
|
|
93
|
-
if (repo.selected === "local")
|
|
94
|
-
return { alias: "local", connection: { kind: "local", mode: "auto" } };
|
|
95
|
-
const global = readGlobalConnections(options);
|
|
96
|
-
const connection = global.connections[repo.selected];
|
|
97
|
-
if (!connection) {
|
|
98
|
-
throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
|
|
99
|
-
}
|
|
100
|
-
return { alias: repo.selected, connection };
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// packages/cli/src/commands/_server-client.ts
|
|
104
|
-
var cachedGitHubBearerToken;
|
|
105
|
-
function cleanToken(value) {
|
|
106
|
-
const trimmed = value?.trim();
|
|
107
|
-
return trimmed ? trimmed : null;
|
|
108
|
-
}
|
|
109
|
-
function readGitHubBearerTokenForRemote() {
|
|
110
|
-
if (cachedGitHubBearerToken !== undefined)
|
|
111
|
-
return cachedGitHubBearerToken;
|
|
112
|
-
const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
|
|
113
|
-
if (envToken) {
|
|
114
|
-
cachedGitHubBearerToken = envToken;
|
|
115
|
-
return cachedGitHubBearerToken;
|
|
116
|
-
}
|
|
117
|
-
const result = spawnSync("gh", ["auth", "token"], {
|
|
118
|
-
encoding: "utf8",
|
|
119
|
-
timeout: 5000,
|
|
120
|
-
stdio: ["ignore", "pipe", "ignore"]
|
|
121
|
-
});
|
|
122
|
-
cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
|
|
123
|
-
return cachedGitHubBearerToken;
|
|
124
|
-
}
|
|
125
|
-
async function ensureServerForCli(projectRoot) {
|
|
126
|
-
try {
|
|
127
|
-
const selected = resolveSelectedConnection(projectRoot);
|
|
128
|
-
if (selected?.connection.kind === "remote") {
|
|
129
|
-
return {
|
|
130
|
-
baseUrl: selected.connection.baseUrl,
|
|
131
|
-
authToken: readGitHubBearerTokenForRemote(),
|
|
132
|
-
connectionKind: "remote"
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
136
|
-
return {
|
|
137
|
-
baseUrl: connection.baseUrl,
|
|
138
|
-
authToken: connection.authToken,
|
|
139
|
-
connectionKind: "local"
|
|
140
|
-
};
|
|
141
|
-
} catch (error) {
|
|
142
|
-
if (error instanceof Error) {
|
|
143
|
-
throw new CliError2(error.message, 1);
|
|
144
|
-
}
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
function mergeHeaders(headers, authToken) {
|
|
149
|
-
const merged = new Headers(headers);
|
|
150
|
-
if (authToken) {
|
|
151
|
-
merged.set("authorization", `Bearer ${authToken}`);
|
|
152
|
-
}
|
|
153
|
-
return merged;
|
|
154
|
-
}
|
|
155
|
-
function diagnosticMessage(payload) {
|
|
156
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
157
|
-
return null;
|
|
158
|
-
const record = payload;
|
|
159
|
-
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
160
|
-
const messages = diagnostics.flatMap((entry) => {
|
|
161
|
-
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
162
|
-
return [];
|
|
163
|
-
const diagnostic = entry;
|
|
164
|
-
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
165
|
-
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
166
|
-
return message ? [`${kind}: ${message}`] : [];
|
|
167
|
-
});
|
|
168
|
-
return messages.length > 0 ? messages.join("; ") : null;
|
|
169
|
-
}
|
|
170
|
-
async function requestServerJson(context, pathname, init = {}) {
|
|
171
|
-
const server = await ensureServerForCli(context.projectRoot);
|
|
172
|
-
const response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
173
|
-
...init,
|
|
174
|
-
headers: mergeHeaders(init.headers, server.authToken)
|
|
175
|
-
});
|
|
176
|
-
const text = await response.text();
|
|
177
|
-
const payload = text.trim().length > 0 ? (() => {
|
|
178
|
-
try {
|
|
179
|
-
return JSON.parse(text);
|
|
180
|
-
} catch {
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
})() : null;
|
|
184
|
-
if (!response.ok) {
|
|
185
|
-
const diagnostics = diagnosticMessage(payload);
|
|
186
|
-
const detail = diagnostics ?? (text || response.statusText);
|
|
187
|
-
throw new CliError2(`Rig server request failed (${response.status}): ${detail}`, 1);
|
|
188
|
-
}
|
|
189
|
-
return payload;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// packages/cli/src/commands/_snapshot-upload.ts
|
|
193
|
-
var UPLOADED_SNAPSHOT_PR_MARKER = "<!-- rig:uploaded-snapshot -->";
|
|
194
|
-
var SNAPSHOT_ARCHIVE_VERSION = 1;
|
|
195
|
-
var SNAPSHOT_ARCHIVE_CONTENT_TYPE = "application/vnd.rig.snapshot+json";
|
|
196
|
-
var DEFAULT_EXCLUDED_DIRECTORIES = new Set([
|
|
197
|
-
".git",
|
|
198
|
-
".rig",
|
|
199
|
-
"node_modules",
|
|
200
|
-
".turbo",
|
|
201
|
-
".next",
|
|
202
|
-
".cache",
|
|
203
|
-
"coverage",
|
|
204
|
-
"dist",
|
|
205
|
-
"build",
|
|
206
|
-
"out"
|
|
207
|
-
]);
|
|
208
|
-
function toPosixPath(path) {
|
|
209
|
-
return path.split(sep).join("/");
|
|
210
|
-
}
|
|
211
|
-
function assertManifestPath(root, relativePath) {
|
|
212
|
-
if (!relativePath || relativePath.startsWith("/") || relativePath.includes("\x00")) {
|
|
213
|
-
throw new Error(`Invalid snapshot path: ${relativePath}`);
|
|
214
|
-
}
|
|
215
|
-
const resolved = resolve2(root, relativePath);
|
|
216
|
-
const relativeToRoot = relative(root, resolved);
|
|
217
|
-
if (relativeToRoot.startsWith("..") || relativeToRoot === ".." || resolve2(relativeToRoot) === resolved) {
|
|
218
|
-
throw new Error(`Snapshot path escapes project root: ${relativePath}`);
|
|
219
|
-
}
|
|
220
|
-
return resolved;
|
|
221
|
-
}
|
|
222
|
-
async function buildSnapshotUploadManifest(projectRoot, options = {}) {
|
|
223
|
-
const root = resolve2(projectRoot);
|
|
224
|
-
const excludedDirectories = [...new Set([
|
|
225
|
-
...DEFAULT_EXCLUDED_DIRECTORIES,
|
|
226
|
-
...options.excludedDirectories ?? []
|
|
227
|
-
])];
|
|
228
|
-
const excludedSet = new Set(excludedDirectories);
|
|
229
|
-
const files = [];
|
|
230
|
-
async function visit(dir) {
|
|
231
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
232
|
-
for (const entry of entries) {
|
|
233
|
-
if (entry.isDirectory() && excludedSet.has(entry.name))
|
|
234
|
-
continue;
|
|
235
|
-
const fullPath = resolve2(dir, entry.name);
|
|
236
|
-
if (entry.isDirectory()) {
|
|
237
|
-
await visit(fullPath);
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
if (!entry.isFile())
|
|
241
|
-
continue;
|
|
242
|
-
files.push(toPosixPath(relative(root, fullPath)));
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
await visit(root);
|
|
246
|
-
files.sort();
|
|
247
|
-
return { root, files, excludedDirectories };
|
|
248
|
-
}
|
|
249
|
-
async function createSnapshotUploadArchive(projectRoot, options = {}) {
|
|
250
|
-
const manifest = await buildSnapshotUploadManifest(projectRoot, options);
|
|
251
|
-
const files = await Promise.all(manifest.files.map(async (path) => {
|
|
252
|
-
const fullPath = assertManifestPath(manifest.root, path);
|
|
253
|
-
return {
|
|
254
|
-
path,
|
|
255
|
-
contentBase64: (await readFile(fullPath)).toString("base64")
|
|
256
|
-
};
|
|
257
|
-
}));
|
|
258
|
-
return {
|
|
259
|
-
version: SNAPSHOT_ARCHIVE_VERSION,
|
|
260
|
-
root: manifest.root,
|
|
261
|
-
files,
|
|
262
|
-
excludedDirectories: manifest.excludedDirectories,
|
|
263
|
-
createdAt: (options.now?.() ?? new Date).toISOString()
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
function encodeSnapshotUploadArchive(archive) {
|
|
267
|
-
return Buffer.from(JSON.stringify(archive), "utf8").toString("base64");
|
|
268
|
-
}
|
|
269
|
-
async function writeSnapshotUploadArchive(projectRoot, outputPath, options = {}) {
|
|
270
|
-
const archive = await createSnapshotUploadArchive(projectRoot, options);
|
|
271
|
-
const target = resolve2(outputPath);
|
|
272
|
-
await mkdir(dirname2(target), { recursive: true });
|
|
273
|
-
await writeFile(target, JSON.stringify(archive, null, 2), "utf8");
|
|
274
|
-
return archive;
|
|
275
|
-
}
|
|
276
|
-
async function uploadSnapshotArchiveViaServer(context, input) {
|
|
277
|
-
const payload = await requestServerJson(context, `/api/projects/${encodeURIComponent(input.repoSlug)}/upload-snapshot`, {
|
|
278
|
-
method: "POST",
|
|
279
|
-
headers: { "content-type": "application/json" },
|
|
280
|
-
body: JSON.stringify({
|
|
281
|
-
archiveContentBase64: encodeSnapshotUploadArchive(input.archive),
|
|
282
|
-
contentType: SNAPSHOT_ARCHIVE_CONTENT_TYPE,
|
|
283
|
-
baseDir: input.baseDir
|
|
284
|
-
})
|
|
285
|
-
});
|
|
286
|
-
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
287
|
-
}
|
|
288
|
-
function appendUploadedSnapshotPrNotice(body) {
|
|
289
|
-
if (body.includes(UPLOADED_SNAPSHOT_PR_MARKER))
|
|
290
|
-
return body;
|
|
291
|
-
const prefix = body.trimEnd();
|
|
292
|
-
const notice = [
|
|
293
|
-
UPLOADED_SNAPSHOT_PR_MARKER,
|
|
294
|
-
"This PR was seeded from an uploaded local working-tree snapshot. Review local snapshot provenance before merging if repository policy requires it."
|
|
295
|
-
].join(`
|
|
296
|
-
`);
|
|
297
|
-
return prefix.length > 0 ? `${prefix}
|
|
298
|
-
|
|
299
|
-
${notice}
|
|
300
|
-
` : `${notice}
|
|
301
|
-
`;
|
|
302
|
-
}
|
|
303
|
-
export {
|
|
304
|
-
writeSnapshotUploadArchive,
|
|
305
|
-
uploadSnapshotArchiveViaServer,
|
|
306
|
-
encodeSnapshotUploadArchive,
|
|
307
|
-
createSnapshotUploadArchive,
|
|
308
|
-
buildSnapshotUploadManifest,
|
|
309
|
-
appendUploadedSnapshotPrNotice,
|
|
310
|
-
UPLOADED_SNAPSHOT_PR_MARKER,
|
|
311
|
-
SNAPSHOT_ARCHIVE_VERSION,
|
|
312
|
-
SNAPSHOT_ARCHIVE_CONTENT_TYPE
|
|
313
|
-
};
|