@h-rig/cli 0.0.6-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/bin/build-rig-binaries.js +107 -0
- package/dist/bin/rig.js +9330 -0
- package/dist/src/commands/_authority-runs.js +110 -0
- package/dist/src/commands/_connection-state.js +123 -0
- package/dist/src/commands/_doctor-checks.js +501 -0
- package/dist/src/commands/_operator-view.js +322 -0
- package/dist/src/commands/_parsers.js +107 -0
- package/dist/src/commands/_paths.js +50 -0
- package/dist/src/commands/_pi-install.js +184 -0
- package/dist/src/commands/_policy.js +79 -0
- package/dist/src/commands/_preflight.js +460 -0
- package/dist/src/commands/_probes.js +13 -0
- package/dist/src/commands/_run-driver-helpers.js +289 -0
- package/dist/src/commands/_server-client.js +364 -0
- package/dist/src/commands/_snapshot-upload.js +313 -0
- package/dist/src/commands/_task-picker.js +48 -0
- package/dist/src/commands/agent.js +497 -0
- package/dist/src/commands/browser.js +890 -0
- package/dist/src/commands/connect.js +180 -0
- package/dist/src/commands/dist.js +402 -0
- package/dist/src/commands/doctor.js +511 -0
- package/dist/src/commands/github.js +276 -0
- package/dist/src/commands/inbox.js +160 -0
- package/dist/src/commands/init.js +1254 -0
- package/dist/src/commands/inspect.js +174 -0
- package/dist/src/commands/inspector.js +256 -0
- package/dist/src/commands/plugin.js +167 -0
- package/dist/src/commands/profile-and-review.js +178 -0
- package/dist/src/commands/queue.js +197 -0
- package/dist/src/commands/remote.js +507 -0
- package/dist/src/commands/repo-git-harness.js +221 -0
- package/dist/src/commands/run.js +753 -0
- package/dist/src/commands/server.js +368 -0
- package/dist/src/commands/setup.js +681 -0
- package/dist/src/commands/task-report-bug.js +1083 -0
- package/dist/src/commands/task-run-driver.js +1933 -0
- package/dist/src/commands/task.js +1325 -0
- package/dist/src/commands/test.js +39 -0
- package/dist/src/commands/workspace.js +123 -0
- package/dist/src/commands.js +9012 -0
- package/dist/src/index.js +9348 -0
- package/dist/src/launcher.js +131 -0
- package/dist/src/report-bug.js +260 -0
- package/dist/src/runner.js +272 -0
- package/dist/src/withMutedConsole.js +42 -0
- package/package.json +31 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/cli/src/commands/_run-driver-helpers.ts
|
|
3
|
+
import { readFileSync } from "fs";
|
|
4
|
+
import { resolve as resolve3 } from "path";
|
|
5
|
+
|
|
6
|
+
// packages/cli/src/runner.ts
|
|
7
|
+
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
8
|
+
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
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
|
+
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
13
|
+
import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
|
|
14
|
+
|
|
15
|
+
// packages/cli/src/commands/_run-driver-helpers.ts
|
|
16
|
+
import {
|
|
17
|
+
appendJsonlRecord,
|
|
18
|
+
readAuthorityRun as readAuthorityRun2,
|
|
19
|
+
resolveAuthorityRunDir as resolveAuthorityRunDir2,
|
|
20
|
+
writeJsonFile as writeJsonFile2
|
|
21
|
+
} from "@rig/runtime/control-plane/authority-files";
|
|
22
|
+
import { taskLookup } from "@rig/runtime/control-plane/native/task-ops";
|
|
23
|
+
import { buildProviderTaskRunInstructionLines } from "@rig/runtime/control-plane/provider/runtime-instructions";
|
|
24
|
+
import { loadRigTaskRunSkillBody } from "@rig/runtime/control-plane/provider/rig-task-run-skill";
|
|
25
|
+
|
|
26
|
+
// packages/cli/src/commands/_authority-runs.ts
|
|
27
|
+
import { existsSync } from "fs";
|
|
28
|
+
import { resolve as resolve2 } from "path";
|
|
29
|
+
import {
|
|
30
|
+
readAuthorityRun,
|
|
31
|
+
readJsonlFile,
|
|
32
|
+
resolveAuthorityRunDir,
|
|
33
|
+
writeJsonFile
|
|
34
|
+
} from "@rig/runtime/control-plane/authority-files";
|
|
35
|
+
|
|
36
|
+
// packages/cli/src/commands/_paths.ts
|
|
37
|
+
import { resolve } from "path";
|
|
38
|
+
import { resolveMonorepoRoot } from "@rig/runtime/control-plane/native/utils";
|
|
39
|
+
function resolveControlPlaneMonorepoRoot(projectRoot) {
|
|
40
|
+
return resolveMonorepoRoot(projectRoot);
|
|
41
|
+
}
|
|
42
|
+
function resolveControlPlaneTaskConfigPath(projectRoot) {
|
|
43
|
+
return resolve(resolveControlPlaneMonorepoRoot(projectRoot), ".rig", "task-config.json");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// packages/cli/src/commands/_authority-runs.ts
|
|
47
|
+
function readLatestBeadRecord(projectRoot, taskId) {
|
|
48
|
+
const issuesPath = resolve2(resolveControlPlaneMonorepoRoot(projectRoot), ".beads", "issues.jsonl");
|
|
49
|
+
if (!existsSync(issuesPath)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
let latest = null;
|
|
53
|
+
for (const issue of readJsonlFile(issuesPath)) {
|
|
54
|
+
if (!issue || typeof issue !== "object") {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const record = issue;
|
|
58
|
+
if (record.id === taskId) {
|
|
59
|
+
latest = record;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return latest;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// packages/cli/src/commands/_run-driver-helpers.ts
|
|
66
|
+
function patchAuthorityRun(projectRoot, runId, patch) {
|
|
67
|
+
const current = readAuthorityRun2(projectRoot, runId);
|
|
68
|
+
if (!current) {
|
|
69
|
+
throw new CliError2(`Run not found: ${runId}`, 2);
|
|
70
|
+
}
|
|
71
|
+
const next = {
|
|
72
|
+
...current,
|
|
73
|
+
...patch,
|
|
74
|
+
updatedAt: new Date().toISOString()
|
|
75
|
+
};
|
|
76
|
+
writeJsonFile2(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "run.json"), next);
|
|
77
|
+
return next;
|
|
78
|
+
}
|
|
79
|
+
function touchAuthorityRun(projectRoot, runId) {
|
|
80
|
+
const current = readAuthorityRun2(projectRoot, runId);
|
|
81
|
+
if (!current) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
writeJsonFile2(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "run.json"), {
|
|
85
|
+
...current,
|
|
86
|
+
updatedAt: new Date().toISOString()
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function appendRunTimeline(projectRoot, runId, value) {
|
|
90
|
+
appendJsonlRecord(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "timeline.jsonl"), value);
|
|
91
|
+
touchAuthorityRun(projectRoot, runId);
|
|
92
|
+
}
|
|
93
|
+
function appendRunLog(projectRoot, runId, value) {
|
|
94
|
+
appendJsonlRecord(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "logs.jsonl"), value);
|
|
95
|
+
touchAuthorityRun(projectRoot, runId);
|
|
96
|
+
}
|
|
97
|
+
function appendRunAction(projectRoot, runId, value) {
|
|
98
|
+
appendJsonlRecord(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "timeline.jsonl"), {
|
|
99
|
+
id: value.id,
|
|
100
|
+
type: "action",
|
|
101
|
+
actionType: value.actionType,
|
|
102
|
+
title: value.title,
|
|
103
|
+
detail: value.detail ?? null,
|
|
104
|
+
state: value.state,
|
|
105
|
+
createdAt: value.startedAt,
|
|
106
|
+
completedAt: value.completedAt ?? null,
|
|
107
|
+
payload: value.payload ?? {}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function startRunAction(projectRoot, runId, input) {
|
|
111
|
+
const startedAt = new Date().toISOString();
|
|
112
|
+
appendRunAction(projectRoot, runId, {
|
|
113
|
+
id: input.actionId,
|
|
114
|
+
actionType: input.actionType,
|
|
115
|
+
title: input.title,
|
|
116
|
+
detail: input.detail,
|
|
117
|
+
state: "running",
|
|
118
|
+
startedAt,
|
|
119
|
+
payload: input.payload
|
|
120
|
+
});
|
|
121
|
+
emitServerRunEvent({ type: "timeline", runId });
|
|
122
|
+
return {
|
|
123
|
+
startedAt,
|
|
124
|
+
complete: (detail, payload) => {
|
|
125
|
+
appendRunAction(projectRoot, runId, {
|
|
126
|
+
id: input.actionId,
|
|
127
|
+
actionType: input.actionType,
|
|
128
|
+
title: input.title,
|
|
129
|
+
detail: detail ?? input.detail ?? null,
|
|
130
|
+
state: "completed",
|
|
131
|
+
startedAt,
|
|
132
|
+
completedAt: new Date().toISOString(),
|
|
133
|
+
payload: payload ?? input.payload
|
|
134
|
+
});
|
|
135
|
+
emitServerRunEvent({ type: "timeline", runId });
|
|
136
|
+
},
|
|
137
|
+
fail: (detail, payload) => {
|
|
138
|
+
appendRunAction(projectRoot, runId, {
|
|
139
|
+
id: input.actionId,
|
|
140
|
+
actionType: input.actionType,
|
|
141
|
+
title: input.title,
|
|
142
|
+
detail,
|
|
143
|
+
state: "failed",
|
|
144
|
+
startedAt,
|
|
145
|
+
completedAt: new Date().toISOString(),
|
|
146
|
+
payload: payload ?? input.payload
|
|
147
|
+
});
|
|
148
|
+
emitServerRunEvent({ type: "timeline", runId });
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function emitServerRunEvent(event) {
|
|
153
|
+
console.log(`__RIG_RUN_EVENT__${JSON.stringify({ ...event, at: new Date().toISOString() })}`);
|
|
154
|
+
}
|
|
155
|
+
function buildRunPrompt(input) {
|
|
156
|
+
const initialPrompt = input.initialPrompt?.trim() || null;
|
|
157
|
+
if (!input.taskId) {
|
|
158
|
+
return initialPrompt ?? input.fallbackTitle?.trim() ?? "Continue the requested Rig run and complete the work.";
|
|
159
|
+
}
|
|
160
|
+
const issues = (() => {
|
|
161
|
+
try {
|
|
162
|
+
return taskLookup(input.projectRoot, input.taskId);
|
|
163
|
+
} catch {
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
})();
|
|
167
|
+
const scopeText = (() => {
|
|
168
|
+
try {
|
|
169
|
+
const parsed = JSON.parse(readFileSync(resolveControlPlaneTaskConfigPath(input.projectRoot), "utf8"));
|
|
170
|
+
const entry = parsed[input.taskId] ?? {};
|
|
171
|
+
const scope = Array.isArray(entry.scope) ? entry.scope.filter((item) => typeof item === "string") : [];
|
|
172
|
+
const validation = Array.isArray(entry.validation) ? entry.validation.filter((item) => typeof item === "string") : [];
|
|
173
|
+
const lines = [];
|
|
174
|
+
if (scope.length > 0)
|
|
175
|
+
lines.push(`Scope:
|
|
176
|
+
- ${scope.join(`
|
|
177
|
+
- `)}`);
|
|
178
|
+
if (validation.length > 0)
|
|
179
|
+
lines.push(`Validation:
|
|
180
|
+
- ${validation.join(`
|
|
181
|
+
- `)}`);
|
|
182
|
+
return lines.join(`
|
|
183
|
+
|
|
184
|
+
`);
|
|
185
|
+
} catch {
|
|
186
|
+
return "";
|
|
187
|
+
}
|
|
188
|
+
})();
|
|
189
|
+
const bead = readLatestBeadRecord(input.projectRoot, input.taskId);
|
|
190
|
+
const sourceTask = input.sourceTask ?? null;
|
|
191
|
+
const sourceDescription = firstPromptString(sourceTask?.description, sourceTask?.body);
|
|
192
|
+
const sourceAcceptance = firstPromptString(sourceTask?.acceptanceCriteria, sourceTask?.acceptance_criteria);
|
|
193
|
+
const sourceValidation = uniqueStrings([...sourceTask?.validation ?? [], ...sourceTask?.validators ?? []]);
|
|
194
|
+
const title = (bead && typeof bead === "object" && typeof bead.title === "string" ? bead.title : null) ?? firstPromptString(sourceTask?.title) ?? input.fallbackTitle ?? issues ?? input.taskId;
|
|
195
|
+
const description = (bead && typeof bead === "object" && typeof bead.description === "string" ? bead.description : null) ?? sourceDescription ?? input.fallbackDescription ?? "";
|
|
196
|
+
const acceptance = bead && typeof bead === "object" && typeof bead.acceptance_criteria === "string" ? bead.acceptance_criteria : sourceAcceptance ?? input.fallbackAcceptanceCriteria ?? "";
|
|
197
|
+
const sourceContractLines = renderSourceTaskContract(sourceTask, sourceValidation);
|
|
198
|
+
const providerLines = buildProviderTaskRunInstructionLines(input.runtimeAdapter);
|
|
199
|
+
const skillBody = loadRigTaskRunSkillBody();
|
|
200
|
+
return [
|
|
201
|
+
`You are working on task ${input.taskId}: ${title}.`,
|
|
202
|
+
description ? `Description:
|
|
203
|
+
${description}` : null,
|
|
204
|
+
acceptance ? `Acceptance criteria:
|
|
205
|
+
${acceptance}` : null,
|
|
206
|
+
sourceContractLines.length > 0 ? `Source task contract:
|
|
207
|
+
${sourceContractLines.join(`
|
|
208
|
+
`)}` : null,
|
|
209
|
+
scopeText || renderSourceScopeValidation(sourceTask, sourceValidation) || null,
|
|
210
|
+
initialPrompt ? `Additional operator guidance:
|
|
211
|
+
${initialPrompt}` : null,
|
|
212
|
+
providerLines.length > 0 ? `Provider-specific runtime tooling:
|
|
213
|
+
${providerLines.join(" ")}` : null,
|
|
214
|
+
skillBody || null
|
|
215
|
+
].filter((value) => Boolean(value)).join(`
|
|
216
|
+
|
|
217
|
+
`);
|
|
218
|
+
}
|
|
219
|
+
function firstPromptString(...values) {
|
|
220
|
+
for (const value of values) {
|
|
221
|
+
const trimmed = typeof value === "string" ? value.trim() : "";
|
|
222
|
+
if (trimmed.length > 0) {
|
|
223
|
+
return trimmed;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
function uniqueStrings(values) {
|
|
229
|
+
return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean)));
|
|
230
|
+
}
|
|
231
|
+
function renderSourceTaskContract(task, validation) {
|
|
232
|
+
if (!task) {
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
const lines = [];
|
|
236
|
+
const sourceIssueId = firstPromptString(task.sourceIssueId);
|
|
237
|
+
if (sourceIssueId)
|
|
238
|
+
lines.push(`Source issue: ${sourceIssueId}`);
|
|
239
|
+
const status = firstPromptString(task.status);
|
|
240
|
+
if (status)
|
|
241
|
+
lines.push(`Source status: ${status}`);
|
|
242
|
+
const issueType = firstPromptString(task.issueType);
|
|
243
|
+
if (issueType)
|
|
244
|
+
lines.push(`Issue type: ${issueType}`);
|
|
245
|
+
const role = firstPromptString(task.role);
|
|
246
|
+
if (role)
|
|
247
|
+
lines.push(`Role: ${role}`);
|
|
248
|
+
const externalRef = firstPromptString(task.externalRef);
|
|
249
|
+
if (externalRef)
|
|
250
|
+
lines.push(`External ref: ${externalRef}`);
|
|
251
|
+
const labels = uniqueStrings(task.labels ?? []);
|
|
252
|
+
if (labels.length > 0)
|
|
253
|
+
lines.push(`Labels:
|
|
254
|
+
- ${labels.join(`
|
|
255
|
+
- `)}`);
|
|
256
|
+
if (validation.length > 0)
|
|
257
|
+
lines.push(`Validators:
|
|
258
|
+
- ${validation.join(`
|
|
259
|
+
- `)}`);
|
|
260
|
+
return lines;
|
|
261
|
+
}
|
|
262
|
+
function renderSourceScopeValidation(task, validation) {
|
|
263
|
+
if (!task) {
|
|
264
|
+
return "";
|
|
265
|
+
}
|
|
266
|
+
const lines = [];
|
|
267
|
+
const scope = uniqueStrings(task.scope ?? []);
|
|
268
|
+
if (scope.length > 0)
|
|
269
|
+
lines.push(`Scope:
|
|
270
|
+
- ${scope.join(`
|
|
271
|
+
- `)}`);
|
|
272
|
+
if (validation.length > 0)
|
|
273
|
+
lines.push(`Validation:
|
|
274
|
+
- ${validation.join(`
|
|
275
|
+
- `)}`);
|
|
276
|
+
return lines.join(`
|
|
277
|
+
|
|
278
|
+
`);
|
|
279
|
+
}
|
|
280
|
+
export {
|
|
281
|
+
touchAuthorityRun,
|
|
282
|
+
startRunAction,
|
|
283
|
+
patchAuthorityRun,
|
|
284
|
+
emitServerRunEvent,
|
|
285
|
+
buildRunPrompt,
|
|
286
|
+
appendRunTimeline,
|
|
287
|
+
appendRunLog,
|
|
288
|
+
appendRunAction
|
|
289
|
+
};
|
|
@@ -0,0 +1,364 @@
|
|
|
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
|
+
};
|