@h-rig/cli 0.0.6-alpha.87 → 0.0.6-alpha.89
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/dist/bin/rig.js +1380 -865
- package/dist/src/app/board.js +462 -48
- package/dist/src/app-opentui/adapters/common.d.ts +3 -0
- package/dist/src/app-opentui/adapters/common.js +4 -0
- package/dist/src/app-opentui/adapters/doctor.js +458 -46
- package/dist/src/app-opentui/adapters/family.js +701 -151
- package/dist/src/app-opentui/adapters/fleet.js +477 -46
- package/dist/src/app-opentui/adapters/inbox.js +477 -46
- package/dist/src/app-opentui/adapters/init.js +497 -74
- package/dist/src/app-opentui/adapters/inspect.js +477 -46
- package/dist/src/app-opentui/adapters/pi-attach.d.ts +7 -0
- package/dist/src/app-opentui/adapters/pi-attach.js +1004 -519
- package/dist/src/app-opentui/adapters/run-detail.js +477 -46
- package/dist/src/app-opentui/adapters/server.d.ts +26 -0
- package/dist/src/app-opentui/adapters/server.js +676 -59
- package/dist/src/app-opentui/adapters/tasks.js +621 -549
- package/dist/src/app-opentui/autocomplete.js +4 -2
- package/dist/src/app-opentui/bootstrap.js +1376 -861
- package/dist/src/app-opentui/command-palette.js +37 -10
- package/dist/src/app-opentui/index.js +632 -528
- package/dist/src/app-opentui/intent.js +33 -8
- package/dist/src/app-opentui/keymap.js +43 -414
- package/dist/src/app-opentui/pi-host-child.js +496 -57
- package/dist/src/app-opentui/pi-pty-host.d.ts +14 -64
- package/dist/src/app-opentui/pi-pty-host.js +3 -397
- package/dist/src/app-opentui/react/App.js +144 -469
- package/dist/src/app-opentui/react/ChromeHost.js +44 -415
- package/dist/src/app-opentui/react/launch.js +659 -552
- package/dist/src/app-opentui/react/nav.js +1 -1
- package/dist/src/app-opentui/registry.js +1181 -742
- package/dist/src/app-opentui/remote-link.d.ts +10 -0
- package/dist/src/app-opentui/remote-link.js +47 -0
- package/dist/src/app-opentui/render/terminal-handoff.d.ts +16 -0
- package/dist/src/app-opentui/render/terminal-handoff.js +14 -0
- package/dist/src/app-opentui/runtime.js +632 -528
- package/dist/src/app-opentui/scenes/doctor.js +1 -1
- package/dist/src/app-opentui/scenes/error.js +50 -4
- package/dist/src/app-opentui/scenes/family.js +60 -6
- package/dist/src/app-opentui/scenes/fleet.js +65 -13
- package/dist/src/app-opentui/scenes/help.js +4 -2
- package/dist/src/app-opentui/scenes/init.js +12 -12
- package/dist/src/app-opentui/scenes/main.js +7 -7
- package/dist/src/app-opentui/scenes/server.js +83 -11
- package/dist/src/app-opentui/scenes/tasks.js +79 -16
- package/dist/src/app-opentui/state.js +25 -5
- package/dist/src/app-opentui/surface-catalog.js +4 -2
- package/dist/src/app-opentui/types.d.ts +1 -1
- package/dist/src/commands/_cli-format.d.ts +10 -1
- package/dist/src/commands/_cli-format.js +5 -2
- package/dist/src/commands/_connection-state.d.ts +11 -1
- package/dist/src/commands/_connection-state.js +50 -5
- package/dist/src/commands/_doctor-checks.js +458 -46
- package/dist/src/commands/_help-catalog.js +4 -2
- package/dist/src/commands/_json-output.js +4 -0
- package/dist/src/commands/_operator-view.js +496 -57
- package/dist/src/commands/_pi-frontend.d.ts +25 -0
- package/dist/src/commands/_pi-frontend.js +497 -57
- package/dist/src/commands/_preflight.js +509 -72
- package/dist/src/commands/_server-client.d.ts +33 -0
- package/dist/src/commands/_server-client.js +477 -46
- package/dist/src/commands/_server-events.js +446 -41
- package/dist/src/commands/_snapshot-upload.js +460 -48
- package/dist/src/commands/connect.js +620 -15
- package/dist/src/commands/doctor.js +458 -46
- package/dist/src/commands/github.js +462 -50
- package/dist/src/commands/inbox.js +458 -46
- package/dist/src/commands/init.js +497 -74
- package/dist/src/commands/inspect.js +458 -46
- package/dist/src/commands/run.js +496 -57
- package/dist/src/commands/server.js +647 -163
- package/dist/src/commands/setup.js +463 -51
- package/dist/src/commands/stats.js +462 -48
- package/dist/src/commands/task-run-driver.js +464 -52
- package/dist/src/commands/task.js +551 -85
- package/dist/src/commands.js +701 -151
- package/dist/src/index.js +705 -151
- package/dist/src/launcher.js +4 -0
- package/package.json +8 -8
|
@@ -95,12 +95,28 @@ function readRepoConnection(projectRoot) {
|
|
|
95
95
|
selected,
|
|
96
96
|
project: typeof record.project === "string" ? record.project : undefined,
|
|
97
97
|
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
98
|
-
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
98
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined,
|
|
99
|
+
serverProjectRootAlias: typeof record.serverProjectRootAlias === "string" && record.serverProjectRootAlias.trim() ? record.serverProjectRootAlias.trim() : undefined,
|
|
100
|
+
serverProjectRootBaseUrl: typeof record.serverProjectRootBaseUrl === "string" && record.serverProjectRootBaseUrl.trim() ? record.serverProjectRootBaseUrl.trim().replace(/\/+$/, "") : undefined
|
|
99
101
|
};
|
|
100
102
|
}
|
|
101
103
|
function writeRepoConnection(projectRoot, state) {
|
|
102
104
|
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
103
105
|
}
|
|
106
|
+
function rootAllowedForSelection(repo, connection) {
|
|
107
|
+
const root = repo.serverProjectRoot?.trim();
|
|
108
|
+
if (!root)
|
|
109
|
+
return;
|
|
110
|
+
if (connection.kind === "remote") {
|
|
111
|
+
if (repo.serverProjectRootAlias !== repo.selected)
|
|
112
|
+
return;
|
|
113
|
+
if (repo.serverProjectRootBaseUrl !== connection.baseUrl)
|
|
114
|
+
return;
|
|
115
|
+
} else if (repo.serverProjectRootAlias && repo.serverProjectRootAlias !== repo.selected) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
return root;
|
|
119
|
+
}
|
|
104
120
|
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
105
121
|
const repo = readRepoConnection(projectRoot);
|
|
106
122
|
if (!repo)
|
|
@@ -112,18 +128,46 @@ function resolveSelectedConnection(projectRoot, options = {}) {
|
|
|
112
128
|
if (!connection) {
|
|
113
129
|
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
114
130
|
}
|
|
115
|
-
return { alias: repo.selected, connection, serverProjectRoot: repo
|
|
131
|
+
return { alias: repo.selected, connection, serverProjectRoot: rootAllowedForSelection(repo, connection) };
|
|
116
132
|
}
|
|
117
|
-
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
133
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot, metadata = {}) {
|
|
118
134
|
const repo = readRepoConnection(projectRoot);
|
|
119
135
|
if (!repo)
|
|
120
136
|
return;
|
|
121
|
-
|
|
137
|
+
let inferred = metadata;
|
|
138
|
+
if (!inferred.alias || !inferred.baseUrl) {
|
|
139
|
+
try {
|
|
140
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
141
|
+
if (selected?.connection.kind === "remote") {
|
|
142
|
+
inferred = {
|
|
143
|
+
alias: inferred.alias ?? selected.alias,
|
|
144
|
+
baseUrl: inferred.baseUrl ?? selected.connection.baseUrl
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
} catch {}
|
|
148
|
+
}
|
|
149
|
+
writeRepoConnection(projectRoot, {
|
|
150
|
+
...repo,
|
|
151
|
+
...metadata.project ? { project: metadata.project } : {},
|
|
152
|
+
serverProjectRoot,
|
|
153
|
+
...inferred.alias ? { serverProjectRootAlias: inferred.alias } : {},
|
|
154
|
+
...inferred.baseUrl ? { serverProjectRootBaseUrl: inferred.baseUrl.replace(/\/+$/, "") } : {}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function clearRepoServerProjectRoot(projectRoot) {
|
|
158
|
+
const repo = readRepoConnection(projectRoot);
|
|
159
|
+
if (!repo)
|
|
160
|
+
return;
|
|
161
|
+
writeRepoConnection(projectRoot, {
|
|
162
|
+
selected: repo.selected,
|
|
163
|
+
...repo.project ? { project: repo.project } : {},
|
|
164
|
+
...repo.linkedAt ? { linkedAt: repo.linkedAt } : {}
|
|
165
|
+
});
|
|
122
166
|
}
|
|
123
167
|
|
|
124
168
|
// packages/cli/src/commands/_server-client.ts
|
|
125
|
-
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
126
|
-
import { resolve as resolve2 } from "path";
|
|
169
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
170
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
|
|
127
171
|
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
128
172
|
var scopedGitHubBearerTokens = new Map;
|
|
129
173
|
var serverPhaseListener = null;
|
|
@@ -162,11 +206,10 @@ function readStoredGitHubAuthToken(projectRoot) {
|
|
|
162
206
|
const parsed = readRemoteAuthState(projectRoot);
|
|
163
207
|
return parsed ? cleanToken(typeof parsed.token === "string" ? parsed.token : undefined) : null;
|
|
164
208
|
}
|
|
165
|
-
function
|
|
166
|
-
|
|
167
|
-
const slug = repo?.project?.trim();
|
|
168
|
-
if (!slug || !/^[^/]+\/[^/]+$/.test(slug))
|
|
209
|
+
function inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug) {
|
|
210
|
+
if (!/^[^/]+\/[^/]+$/.test(repoSlug))
|
|
169
211
|
return null;
|
|
212
|
+
const repo = readRepoConnection(projectRoot);
|
|
170
213
|
const auth = readRemoteAuthState(projectRoot);
|
|
171
214
|
const authUpdatedAt = typeof auth?.updatedAt === "string" ? Date.parse(auth.updatedAt) : Number.NaN;
|
|
172
215
|
const repoLinkedAt = typeof repo?.linkedAt === "string" ? Date.parse(repo.linkedAt) : Number.NaN;
|
|
@@ -175,25 +218,413 @@ function inferRemoteServerProjectRootFromAuthState(projectRoot) {
|
|
|
175
218
|
const checkoutBaseDir = typeof auth?.checkoutBaseDir === "string" && auth.checkoutBaseDir.trim() ? auth.checkoutBaseDir.trim() : null;
|
|
176
219
|
if (!checkoutBaseDir)
|
|
177
220
|
return null;
|
|
178
|
-
|
|
179
|
-
writeRepoServerProjectRoot(projectRoot, inferred);
|
|
180
|
-
return inferred;
|
|
221
|
+
return `${checkoutBaseDir.replace(/\/+$/, "")}/${repoSlug}`;
|
|
181
222
|
}
|
|
182
223
|
function readLocalConnectionFallbackToken(projectRoot) {
|
|
183
224
|
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
184
225
|
}
|
|
226
|
+
function normalizeRepoSlug(value) {
|
|
227
|
+
const slug = value?.trim();
|
|
228
|
+
return slug && /^[^/\s]+\/[^/\s]+$/.test(slug) ? slug : null;
|
|
229
|
+
}
|
|
230
|
+
function readProjectLinkSlug(projectRoot) {
|
|
231
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
232
|
+
if (!existsSync2(path))
|
|
233
|
+
return null;
|
|
234
|
+
try {
|
|
235
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
236
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
237
|
+
return null;
|
|
238
|
+
return normalizeRepoSlug(typeof parsed.repoSlug === "string" ? String(parsed.repoSlug) : null);
|
|
239
|
+
} catch {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function writeProjectLinkConnection(projectRoot, repoSlug, alias) {
|
|
244
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
245
|
+
mkdirSync2(dirname2(path), { recursive: true });
|
|
246
|
+
writeFileSync2(path, `${JSON.stringify({ repoSlug, connection: alias, linkedAt: new Date().toISOString() }, null, 2)}
|
|
247
|
+
`, "utf8");
|
|
248
|
+
}
|
|
249
|
+
function remoteLinkRepairCommand(repoSlug) {
|
|
250
|
+
return repoSlug ? `rig server repair-link --repo ${repoSlug}` : "rig server repair-link --repo owner/repo";
|
|
251
|
+
}
|
|
252
|
+
function formatRemoteProjectLinkHint(resolution) {
|
|
253
|
+
const repair = remoteLinkRepairCommand(resolution.repoSlug);
|
|
254
|
+
if (resolution.status === "auth_required") {
|
|
255
|
+
return `Authenticate the selected remote${resolution.alias ? ` (${resolution.alias})` : ""}, then run \`${repair}\`. Use \`rig github auth import-gh\` or \`rig init --repair --server remote --github-auth device${resolution.repoSlug ? ` --repo ${resolution.repoSlug}` : ""}\`.`;
|
|
256
|
+
}
|
|
257
|
+
if (resolution.status === "missing_project") {
|
|
258
|
+
return `Record the repo slug, then run \`${repair}\` (or \`rig init --repo owner/repo --server remote --github-auth device\`).`;
|
|
259
|
+
}
|
|
260
|
+
if (resolution.status === "not_remote")
|
|
261
|
+
return "Select a remote server with `rig server use <alias>` before repairing a remote project link.";
|
|
262
|
+
return `Run \`${repair}\` to backfill or prepare a server-host checkout for the selected remote${resolution.alias ? ` (${resolution.alias})` : ""}.`;
|
|
263
|
+
}
|
|
264
|
+
function remoteProjectLinkFailure(input) {
|
|
265
|
+
const partial = {
|
|
266
|
+
status: input.status,
|
|
267
|
+
alias: input.alias,
|
|
268
|
+
baseUrl: input.baseUrl,
|
|
269
|
+
repoSlug: input.repoSlug
|
|
270
|
+
};
|
|
271
|
+
return {
|
|
272
|
+
ok: false,
|
|
273
|
+
...input,
|
|
274
|
+
hint: formatRemoteProjectLinkHint(partial),
|
|
275
|
+
next: remoteLinkRepairCommand(input.repoSlug)
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function remoteProjectLinkSuccess(input) {
|
|
279
|
+
return {
|
|
280
|
+
ok: true,
|
|
281
|
+
status: input.status,
|
|
282
|
+
alias: input.alias,
|
|
283
|
+
baseUrl: input.baseUrl,
|
|
284
|
+
repoSlug: input.repoSlug,
|
|
285
|
+
serverProjectRoot: input.serverProjectRoot,
|
|
286
|
+
source: input.source,
|
|
287
|
+
prepared: input.prepared ?? input.status === "prepared",
|
|
288
|
+
validated: input.validated ?? false,
|
|
289
|
+
message: input.message ?? `Remote project link ready for ${input.repoSlug}.`,
|
|
290
|
+
hint: "Remote-scoped task/run/status requests will send x-rig-project-root for this server-host checkout.",
|
|
291
|
+
next: "rig task list"
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function localCheckoutPathCandidate(projectRoot, candidate) {
|
|
295
|
+
try {
|
|
296
|
+
const local = resolve2(projectRoot);
|
|
297
|
+
const resolved = resolve2(candidate);
|
|
298
|
+
return resolved === local || resolved.startsWith(`${local}/`);
|
|
299
|
+
} catch {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async function requestRemoteProjectLinkJson(baseUrl, pathname, authToken, init = {}) {
|
|
304
|
+
const requestUrl = new URL(`${baseUrl}${pathname}`);
|
|
305
|
+
if (authToken && queryAuthFallbackEnabled())
|
|
306
|
+
requestUrl.searchParams.set("rt", authToken);
|
|
307
|
+
const response = await fetch(requestUrl, {
|
|
308
|
+
...init,
|
|
309
|
+
headers: mergeHeaders(init.headers, authToken)
|
|
310
|
+
});
|
|
311
|
+
const text = await response.text();
|
|
312
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
313
|
+
try {
|
|
314
|
+
return JSON.parse(text);
|
|
315
|
+
} catch {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
})() : null;
|
|
319
|
+
return { ok: response.ok, status: response.status, payload, text };
|
|
320
|
+
}
|
|
321
|
+
function payloadError(payload, fallback) {
|
|
322
|
+
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
|
323
|
+
const record = payload;
|
|
324
|
+
const value = record.error ?? record.message ?? record.reason;
|
|
325
|
+
if (typeof value === "string" && value.trim())
|
|
326
|
+
return value.trim();
|
|
327
|
+
}
|
|
328
|
+
return fallback;
|
|
329
|
+
}
|
|
330
|
+
function checkoutPathsFromProjectPayload(payload) {
|
|
331
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
332
|
+
return [];
|
|
333
|
+
const project = payload.project;
|
|
334
|
+
if (!project || typeof project !== "object" || Array.isArray(project))
|
|
335
|
+
return [];
|
|
336
|
+
const checkouts = project.checkouts;
|
|
337
|
+
if (!Array.isArray(checkouts))
|
|
338
|
+
return [];
|
|
339
|
+
return [...checkouts].reverse().flatMap((entry) => {
|
|
340
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
341
|
+
return [];
|
|
342
|
+
const path = entry.path;
|
|
343
|
+
return typeof path === "string" && path.trim() ? [path.trim()] : [];
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function checkoutPathFromPreparePayload(payload) {
|
|
347
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
348
|
+
return null;
|
|
349
|
+
const checkout = payload.checkout;
|
|
350
|
+
if (!checkout || typeof checkout !== "object" || Array.isArray(checkout))
|
|
351
|
+
return null;
|
|
352
|
+
const path = checkout.path;
|
|
353
|
+
return typeof path === "string" && path.trim() ? path.trim() : null;
|
|
354
|
+
}
|
|
355
|
+
async function validateAndPersistRemoteRoot(input) {
|
|
356
|
+
const candidate = input.candidate.trim();
|
|
357
|
+
if (!candidate || !isAbsolute(candidate)) {
|
|
358
|
+
return remoteProjectLinkFailure({
|
|
359
|
+
status: "invalid_root",
|
|
360
|
+
alias: input.alias,
|
|
361
|
+
baseUrl: input.baseUrl,
|
|
362
|
+
repoSlug: input.repoSlug,
|
|
363
|
+
message: `Remote project root candidate is not an absolute server-host path: ${candidate || "(empty)"}.`
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
if (localCheckoutPathCandidate(input.projectRoot, candidate)) {
|
|
367
|
+
return remoteProjectLinkFailure({
|
|
368
|
+
status: "invalid_root",
|
|
369
|
+
alias: input.alias,
|
|
370
|
+
baseUrl: input.baseUrl,
|
|
371
|
+
repoSlug: input.repoSlug,
|
|
372
|
+
message: `Refusing to use local checkout path ${candidate} as a remote server project root.`
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
let response;
|
|
376
|
+
try {
|
|
377
|
+
response = await requestRemoteProjectLinkJson(input.baseUrl, "/api/server/project-root", input.authToken, {
|
|
378
|
+
method: "POST",
|
|
379
|
+
headers: { "content-type": "application/json" },
|
|
380
|
+
body: JSON.stringify({ projectRoot: candidate })
|
|
381
|
+
});
|
|
382
|
+
} catch (error) {
|
|
383
|
+
return remoteProjectLinkFailure({
|
|
384
|
+
status: "error",
|
|
385
|
+
alias: input.alias,
|
|
386
|
+
baseUrl: input.baseUrl,
|
|
387
|
+
repoSlug: input.repoSlug,
|
|
388
|
+
message: `Could not validate remote project root ${candidate}: ${error instanceof Error ? error.message : String(error)}`
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
if (response.status === 401 || response.status === 403) {
|
|
392
|
+
return remoteProjectLinkFailure({
|
|
393
|
+
status: "auth_required",
|
|
394
|
+
alias: input.alias,
|
|
395
|
+
baseUrl: input.baseUrl,
|
|
396
|
+
repoSlug: input.repoSlug,
|
|
397
|
+
statusCode: response.status,
|
|
398
|
+
message: `Remote server ${input.alias} requires authentication before validating project root ${candidate}.`
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
if (!response.ok) {
|
|
402
|
+
return remoteProjectLinkFailure({
|
|
403
|
+
status: "invalid_root",
|
|
404
|
+
alias: input.alias,
|
|
405
|
+
baseUrl: input.baseUrl,
|
|
406
|
+
repoSlug: input.repoSlug,
|
|
407
|
+
statusCode: response.status,
|
|
408
|
+
message: `Remote server rejected project root ${candidate} (${response.status}): ${payloadError(response.payload, response.text || "project-root validation failed")}`
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const record = response.payload && typeof response.payload === "object" && !Array.isArray(response.payload) ? response.payload : {};
|
|
412
|
+
if (record.ok !== true) {
|
|
413
|
+
return remoteProjectLinkFailure({
|
|
414
|
+
status: "invalid_root",
|
|
415
|
+
alias: input.alias,
|
|
416
|
+
baseUrl: input.baseUrl,
|
|
417
|
+
repoSlug: input.repoSlug,
|
|
418
|
+
message: `Remote server did not accept project root ${candidate}: ${payloadError(response.payload, "project-root validation failed")}`
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
const accepted = typeof record.projectRoot === "string" && record.projectRoot.trim() ? record.projectRoot.trim() : typeof record.requestedProjectRoot === "string" && record.requestedProjectRoot.trim() ? record.requestedProjectRoot.trim() : candidate;
|
|
422
|
+
writeRepoServerProjectRoot(input.projectRoot, accepted, { alias: input.alias, baseUrl: input.baseUrl, project: input.repoSlug });
|
|
423
|
+
writeProjectLinkConnection(input.projectRoot, input.repoSlug, input.alias);
|
|
424
|
+
const status = input.status ?? (input.prepared ? "prepared" : "backfilled");
|
|
425
|
+
return remoteProjectLinkSuccess({
|
|
426
|
+
status,
|
|
427
|
+
alias: input.alias,
|
|
428
|
+
baseUrl: input.baseUrl,
|
|
429
|
+
repoSlug: input.repoSlug,
|
|
430
|
+
serverProjectRoot: accepted,
|
|
431
|
+
source: input.source,
|
|
432
|
+
prepared: input.prepared,
|
|
433
|
+
validated: true,
|
|
434
|
+
message: status === "ready" ? `Remote project link already points to ${accepted} and was validated on ${input.alias}.` : input.prepared ? `Prepared and linked remote checkout ${accepted} for ${input.repoSlug}.` : `Backfilled remote checkout ${accepted} for ${input.repoSlug}.`
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
async function ensureRemoteProjectRootLink(projectRoot, options = {}) {
|
|
438
|
+
let selected;
|
|
439
|
+
try {
|
|
440
|
+
selected = resolveSelectedConnection(projectRoot);
|
|
441
|
+
} catch (error) {
|
|
442
|
+
return remoteProjectLinkFailure({ status: "error", message: error instanceof Error ? error.message : String(error) });
|
|
443
|
+
}
|
|
444
|
+
if (!selected || selected.connection.kind !== "remote") {
|
|
445
|
+
return remoteProjectLinkFailure({ status: "not_remote", message: "No remote Rig server is selected for this repo." });
|
|
446
|
+
}
|
|
447
|
+
const repo = readRepoConnection(projectRoot);
|
|
448
|
+
const explicitRepoRequested = options.repoSlug !== undefined;
|
|
449
|
+
const explicitRepoSlug = explicitRepoRequested ? normalizeRepoSlug(options.repoSlug) : null;
|
|
450
|
+
const repoProjectSlug = normalizeRepoSlug(repo?.project);
|
|
451
|
+
const repoSlug = explicitRepoSlug ?? repoProjectSlug ?? readProjectLinkSlug(projectRoot);
|
|
452
|
+
const alias = selected.alias;
|
|
453
|
+
const baseUrl = selected.connection.baseUrl;
|
|
454
|
+
const authToken = options.authToken === undefined ? readGitHubBearerTokenForRemote(projectRoot) : options.authToken;
|
|
455
|
+
const mode = options.mode ?? "backfill-only";
|
|
456
|
+
if (explicitRepoRequested && !explicitRepoSlug) {
|
|
457
|
+
return remoteProjectLinkFailure({
|
|
458
|
+
status: "missing_project",
|
|
459
|
+
alias,
|
|
460
|
+
baseUrl,
|
|
461
|
+
message: `Invalid --repo value ${JSON.stringify(options.repoSlug)}. Expected owner/repo.`
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
if (!repoSlug) {
|
|
465
|
+
return remoteProjectLinkFailure({
|
|
466
|
+
status: "missing_project",
|
|
467
|
+
alias,
|
|
468
|
+
baseUrl,
|
|
469
|
+
message: `Remote server ${alias} is selected, but this checkout has no GitHub repo slug recorded.`
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
const skippedCandidates = [];
|
|
473
|
+
const storedRoot = selected.serverProjectRoot?.trim() || null;
|
|
474
|
+
const storedRootMatchesResolvedRepo = repoProjectSlug === repoSlug;
|
|
475
|
+
if (storedRoot && storedRootMatchesResolvedRepo) {
|
|
476
|
+
if (localCheckoutPathCandidate(projectRoot, storedRoot)) {
|
|
477
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
478
|
+
skippedCandidates.push(storedRoot);
|
|
479
|
+
} else {
|
|
480
|
+
const storedResult = await validateAndPersistRemoteRoot({
|
|
481
|
+
projectRoot,
|
|
482
|
+
alias,
|
|
483
|
+
baseUrl,
|
|
484
|
+
authToken,
|
|
485
|
+
repoSlug,
|
|
486
|
+
candidate: storedRoot,
|
|
487
|
+
source: "stored",
|
|
488
|
+
status: "ready"
|
|
489
|
+
});
|
|
490
|
+
if (storedResult.ok || storedResult.status === "auth_required" || storedResult.status === "error")
|
|
491
|
+
return storedResult;
|
|
492
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
493
|
+
skippedCandidates.push(storedRoot);
|
|
494
|
+
}
|
|
495
|
+
} else if (storedRoot) {
|
|
496
|
+
skippedCandidates.push(storedRoot);
|
|
497
|
+
}
|
|
498
|
+
const authCandidate = inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug);
|
|
499
|
+
if (authCandidate) {
|
|
500
|
+
const authResult = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: authCandidate, source: "auth-state" });
|
|
501
|
+
if (authResult.ok || authResult.status === "auth_required")
|
|
502
|
+
return authResult;
|
|
503
|
+
}
|
|
504
|
+
let registryResponse;
|
|
505
|
+
try {
|
|
506
|
+
registryResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}`, authToken);
|
|
507
|
+
} catch (error) {
|
|
508
|
+
return remoteProjectLinkFailure({
|
|
509
|
+
status: "error",
|
|
510
|
+
alias,
|
|
511
|
+
baseUrl,
|
|
512
|
+
repoSlug,
|
|
513
|
+
message: `Could not read remote project registry for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
if (registryResponse.status === 401 || registryResponse.status === 403) {
|
|
517
|
+
return remoteProjectLinkFailure({
|
|
518
|
+
status: "auth_required",
|
|
519
|
+
alias,
|
|
520
|
+
baseUrl,
|
|
521
|
+
repoSlug,
|
|
522
|
+
statusCode: registryResponse.status,
|
|
523
|
+
message: `Remote server ${alias} requires authentication before reading the ${repoSlug} project registry.`
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
if (registryResponse.ok) {
|
|
527
|
+
const candidates = checkoutPathsFromProjectPayload(registryResponse.payload);
|
|
528
|
+
for (const candidate of candidates) {
|
|
529
|
+
const result = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate, source: "registry" });
|
|
530
|
+
if (result.ok || result.status === "auth_required")
|
|
531
|
+
return result;
|
|
532
|
+
skippedCandidates.push(candidate);
|
|
533
|
+
}
|
|
534
|
+
if (mode === "backfill-only") {
|
|
535
|
+
return remoteProjectLinkFailure({
|
|
536
|
+
status: candidates.length > 0 ? "invalid_root" : "no_server_checkout",
|
|
537
|
+
alias,
|
|
538
|
+
baseUrl,
|
|
539
|
+
repoSlug,
|
|
540
|
+
message: candidates.length > 0 ? `Remote registry has checkout candidates for ${repoSlug}, but none validated for ${alias}.` : `Remote registry has ${repoSlug}, but no server checkout path is linked yet.`,
|
|
541
|
+
skippedCandidates
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
} else if (registryResponse.status === 404) {
|
|
545
|
+
if (mode === "backfill-only") {
|
|
546
|
+
return remoteProjectLinkFailure({
|
|
547
|
+
status: "project_not_registered",
|
|
548
|
+
alias,
|
|
549
|
+
baseUrl,
|
|
550
|
+
repoSlug,
|
|
551
|
+
statusCode: registryResponse.status,
|
|
552
|
+
message: `Remote server ${alias} has no registered project record for ${repoSlug}.`
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
} else {
|
|
556
|
+
return remoteProjectLinkFailure({
|
|
557
|
+
status: "error",
|
|
558
|
+
alias,
|
|
559
|
+
baseUrl,
|
|
560
|
+
repoSlug,
|
|
561
|
+
statusCode: registryResponse.status,
|
|
562
|
+
message: `Could not backfill ${repoSlug} from remote registry (${registryResponse.status}): ${payloadError(registryResponse.payload, registryResponse.text || "registry lookup failed")}`,
|
|
563
|
+
skippedCandidates
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
let prepareResponse;
|
|
567
|
+
try {
|
|
568
|
+
prepareResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}/prepare-checkout`, authToken, {
|
|
569
|
+
method: "POST",
|
|
570
|
+
headers: { "content-type": "application/json" },
|
|
571
|
+
body: JSON.stringify({ checkout: { kind: "managed-clone" }, repoUrl: `https://github.com/${repoSlug}.git` })
|
|
572
|
+
});
|
|
573
|
+
} catch (error) {
|
|
574
|
+
return remoteProjectLinkFailure({
|
|
575
|
+
status: "error",
|
|
576
|
+
alias,
|
|
577
|
+
baseUrl,
|
|
578
|
+
repoSlug,
|
|
579
|
+
message: `Could not prepare remote checkout for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
if (prepareResponse.status === 401 || prepareResponse.status === 403) {
|
|
583
|
+
return remoteProjectLinkFailure({
|
|
584
|
+
status: "auth_required",
|
|
585
|
+
alias,
|
|
586
|
+
baseUrl,
|
|
587
|
+
repoSlug,
|
|
588
|
+
statusCode: prepareResponse.status,
|
|
589
|
+
message: `Remote server ${alias} requires authentication before preparing ${repoSlug}.`
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
if (!prepareResponse.ok) {
|
|
593
|
+
return remoteProjectLinkFailure({
|
|
594
|
+
status: "error",
|
|
595
|
+
alias,
|
|
596
|
+
baseUrl,
|
|
597
|
+
repoSlug,
|
|
598
|
+
statusCode: prepareResponse.status,
|
|
599
|
+
message: `Remote checkout prepare failed for ${repoSlug} (${prepareResponse.status}): ${payloadError(prepareResponse.payload, prepareResponse.text || "prepare-checkout failed")}`,
|
|
600
|
+
skippedCandidates
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
const preparedPath = checkoutPathFromPreparePayload(prepareResponse.payload);
|
|
604
|
+
if (!preparedPath) {
|
|
605
|
+
return remoteProjectLinkFailure({
|
|
606
|
+
status: "invalid_root",
|
|
607
|
+
alias,
|
|
608
|
+
baseUrl,
|
|
609
|
+
repoSlug,
|
|
610
|
+
message: `Remote checkout prepare for ${repoSlug} did not return a checkout.path.`,
|
|
611
|
+
skippedCandidates
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
return validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: preparedPath, source: "prepare", prepared: true });
|
|
615
|
+
}
|
|
185
616
|
async function ensureServerForCli(projectRoot) {
|
|
186
617
|
try {
|
|
187
618
|
const selected = resolveSelectedConnection(projectRoot);
|
|
188
619
|
if (selected?.connection.kind === "remote") {
|
|
189
620
|
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
190
621
|
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
191
|
-
const
|
|
622
|
+
const link = await ensureRemoteProjectRootLink(projectRoot, { mode: "backfill-only", authToken });
|
|
192
623
|
return {
|
|
193
624
|
baseUrl: selected.connection.baseUrl,
|
|
194
625
|
authToken,
|
|
195
626
|
connectionKind: "remote",
|
|
196
|
-
serverProjectRoot
|
|
627
|
+
serverProjectRoot: link.ok ? link.serverProjectRoot ?? null : null
|
|
197
628
|
};
|
|
198
629
|
}
|
|
199
630
|
reportServerPhase("Starting local Rig server\u2026");
|
|
@@ -211,32 +642,6 @@ async function ensureServerForCli(projectRoot) {
|
|
|
211
642
|
throw error;
|
|
212
643
|
}
|
|
213
644
|
}
|
|
214
|
-
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
215
|
-
const repo = readRepoConnection(projectRoot);
|
|
216
|
-
const slug = repo?.project?.trim();
|
|
217
|
-
if (!slug)
|
|
218
|
-
return null;
|
|
219
|
-
try {
|
|
220
|
-
const url = new URL(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`);
|
|
221
|
-
if (authToken && queryAuthFallbackEnabled())
|
|
222
|
-
url.searchParams.set("rt", authToken);
|
|
223
|
-
const response = await fetch(url, {
|
|
224
|
-
headers: mergeHeaders(undefined, authToken)
|
|
225
|
-
});
|
|
226
|
-
if (!response.ok)
|
|
227
|
-
return null;
|
|
228
|
-
const payload = await response.json();
|
|
229
|
-
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
230
|
-
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
231
|
-
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
232
|
-
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
233
|
-
if (path)
|
|
234
|
-
writeRepoServerProjectRoot(projectRoot, path);
|
|
235
|
-
return path;
|
|
236
|
-
} catch {
|
|
237
|
-
return null;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
645
|
function mergeCookie(existing, name, value) {
|
|
241
646
|
const encoded = `${name}=${encodeURIComponent(value)}`;
|
|
242
647
|
if (!existing?.trim())
|
|
@@ -328,13 +733,20 @@ function canUseRemoteWithoutProjectRoot(pathname) {
|
|
|
328
733
|
async function requestServerJson(context, pathname, init = {}) {
|
|
329
734
|
const server = await ensureServerForCli(context.projectRoot);
|
|
330
735
|
const requestUrl = new URL(`${server.baseUrl}${pathname}`);
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
736
|
+
let scopedServerProjectRoot = server.serverProjectRoot;
|
|
737
|
+
if (server.connectionKind === "remote" && !scopedServerProjectRoot && !canUseRemoteWithoutProjectRoot(requestUrl.pathname) && (server.authToken || !isLoopbackRemoteBaseUrl(server.baseUrl))) {
|
|
738
|
+
const link = await ensureRemoteProjectRootLink(context.projectRoot, { mode: "backfill-only", authToken: server.authToken });
|
|
739
|
+
if (link.ok && link.serverProjectRoot) {
|
|
740
|
+
scopedServerProjectRoot = link.serverProjectRoot;
|
|
741
|
+
} else {
|
|
742
|
+
const repo = readRepoConnection(context.projectRoot);
|
|
743
|
+
const target = link.alias && link.baseUrl ? `${link.alias} (${link.baseUrl})` : server.baseUrl;
|
|
744
|
+
throw new CliError(`Remote server ${target} is selected for ${link.repoSlug ?? repo?.project ?? "this repo"}, but this checkout has no server-host project root link. ${link.message}`, 1, { hint: link.hint });
|
|
745
|
+
}
|
|
334
746
|
}
|
|
335
747
|
const headers = mergeHeaders(init.headers, server.authToken);
|
|
336
|
-
if (
|
|
337
|
-
headers.set("x-rig-project-root",
|
|
748
|
+
if (scopedServerProjectRoot)
|
|
749
|
+
headers.set("x-rig-project-root", scopedServerProjectRoot);
|
|
338
750
|
if (server.connectionKind === "remote" && server.authToken && queryAuthFallbackEnabled()) {
|
|
339
751
|
requestUrl.searchParams.set("rt", server.authToken);
|
|
340
752
|
}
|
|
@@ -67,12 +67,13 @@ var PRIMARY_GROUPS = [
|
|
|
67
67
|
{
|
|
68
68
|
name: "server",
|
|
69
69
|
summary: "Choose, inspect, and start the Rig server that owns tasks and runs.",
|
|
70
|
-
usage: ["rig server <status|list|add|use|start> [options]"],
|
|
70
|
+
usage: ["rig server <status|list|add|use|repair-link|start> [options]"],
|
|
71
71
|
commands: [
|
|
72
|
-
{ command: "status", description: "Show the selected server for this repo.", primary: true },
|
|
72
|
+
{ command: "status", description: "Show the selected server and remote project-root link for this repo.", primary: true },
|
|
73
73
|
{ command: "use local", description: "Switch this repo to the local Rig server.", primary: true },
|
|
74
74
|
{ command: "add <alias> <url>", description: "Save a remote Rig server URL.", primary: true },
|
|
75
75
|
{ command: "use <alias>", description: "Select a saved remote server alias.", primary: true },
|
|
76
|
+
{ command: "repair-link [--prepare|--backfill-only] [--repo owner/repo]", description: "Backfill or prepare the selected remote's server-host checkout link.", primary: true },
|
|
76
77
|
{ command: "list", description: "List saved local/remote server aliases.", primary: true },
|
|
77
78
|
{ command: "start [--host <host>] [--port <n>]", description: "Start a local rig-server process." }
|
|
78
79
|
],
|
|
@@ -80,6 +81,7 @@ var PRIMARY_GROUPS = [
|
|
|
80
81
|
"rig server status",
|
|
81
82
|
"rig server add prod https://where.rig-does.work",
|
|
82
83
|
"rig server use prod",
|
|
84
|
+
"rig server repair-link --repo owner/repo",
|
|
83
85
|
"rig server use local",
|
|
84
86
|
"rig server start --port 3773"
|
|
85
87
|
],
|
|
@@ -7,7 +7,9 @@ import {
|
|
|
7
7
|
RigInboxInputsOutput,
|
|
8
8
|
RigRunListOutput,
|
|
9
9
|
RigRunShowOutput,
|
|
10
|
+
RigServerRepairLinkOutput,
|
|
10
11
|
RigServerStatusOutput,
|
|
12
|
+
RigServerUseOutput,
|
|
11
13
|
RigStatsOutput,
|
|
12
14
|
RigTaskListOutput,
|
|
13
15
|
RigTaskShowOutput
|
|
@@ -17,7 +19,9 @@ var CLI_OUTPUT_SCHEMAS = {
|
|
|
17
19
|
"task show": RigTaskShowOutput,
|
|
18
20
|
"run list": RigRunListOutput,
|
|
19
21
|
"run show": RigRunShowOutput,
|
|
22
|
+
"server use": RigServerUseOutput,
|
|
20
23
|
"server status": RigServerStatusOutput,
|
|
24
|
+
"server repair-link": RigServerRepairLinkOutput,
|
|
21
25
|
"inbox approvals": RigInboxApprovalsOutput,
|
|
22
26
|
"inbox inputs": RigInboxInputsOutput,
|
|
23
27
|
"doctor check": RigDoctorCheckOutput,
|