@h-rig/cli 0.0.6-alpha.88 → 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 +1159 -292
- package/dist/src/app/board.js +462 -48
- package/dist/src/app-opentui/adapters/doctor.js +458 -46
- package/dist/src/app-opentui/adapters/family.js +670 -147
- 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.js +484 -53
- 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 +539 -81
- package/dist/src/app-opentui/autocomplete.js +4 -2
- package/dist/src/app-opentui/bootstrap.js +1155 -288
- package/dist/src/app-opentui/command-palette.js +37 -10
- package/dist/src/app-opentui/index.js +566 -89
- package/dist/src/app-opentui/intent.js +33 -8
- package/dist/src/app-opentui/keymap.js +43 -29
- package/dist/src/app-opentui/pi-host-child.js +465 -53
- package/dist/src/app-opentui/react/App.js +104 -44
- package/dist/src/app-opentui/react/ChromeHost.js +16 -4
- package/dist/src/app-opentui/react/launch.js +555 -88
- package/dist/src/app-opentui/react/nav.js +1 -1
- package/dist/src/app-opentui/registry.js +1062 -237
- 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/runtime.js +566 -89
- 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 +465 -53
- package/dist/src/commands/_pi-frontend.js +465 -53
- 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 +465 -53
- 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 +520 -81
- package/dist/src/commands.js +670 -147
- package/dist/src/index.js +674 -147
- package/dist/src/launcher.js +4 -0
- package/package.json +8 -8
|
@@ -64,8 +64,8 @@ import { resolveHarnessPaths, resolveMonorepoRoot, runCapture } from "@rig/runti
|
|
|
64
64
|
import { readTaskArtifactPreview } from "@rig/runtime/control-plane/native/workspace-ops";
|
|
65
65
|
|
|
66
66
|
// packages/cli/src/commands/_server-client.ts
|
|
67
|
-
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
68
|
-
import { resolve as resolve2 } from "path";
|
|
67
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
68
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
|
|
69
69
|
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
70
70
|
|
|
71
71
|
// packages/cli/src/commands/_connection-state.ts
|
|
@@ -139,12 +139,28 @@ function readRepoConnection(projectRoot) {
|
|
|
139
139
|
selected,
|
|
140
140
|
project: typeof record.project === "string" ? record.project : undefined,
|
|
141
141
|
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
142
|
-
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
142
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined,
|
|
143
|
+
serverProjectRootAlias: typeof record.serverProjectRootAlias === "string" && record.serverProjectRootAlias.trim() ? record.serverProjectRootAlias.trim() : undefined,
|
|
144
|
+
serverProjectRootBaseUrl: typeof record.serverProjectRootBaseUrl === "string" && record.serverProjectRootBaseUrl.trim() ? record.serverProjectRootBaseUrl.trim().replace(/\/+$/, "") : undefined
|
|
143
145
|
};
|
|
144
146
|
}
|
|
145
147
|
function writeRepoConnection(projectRoot, state) {
|
|
146
148
|
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
147
149
|
}
|
|
150
|
+
function rootAllowedForSelection(repo, connection) {
|
|
151
|
+
const root = repo.serverProjectRoot?.trim();
|
|
152
|
+
if (!root)
|
|
153
|
+
return;
|
|
154
|
+
if (connection.kind === "remote") {
|
|
155
|
+
if (repo.serverProjectRootAlias !== repo.selected)
|
|
156
|
+
return;
|
|
157
|
+
if (repo.serverProjectRootBaseUrl !== connection.baseUrl)
|
|
158
|
+
return;
|
|
159
|
+
} else if (repo.serverProjectRootAlias && repo.serverProjectRootAlias !== repo.selected) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
return root;
|
|
163
|
+
}
|
|
148
164
|
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
149
165
|
const repo = readRepoConnection(projectRoot);
|
|
150
166
|
if (!repo)
|
|
@@ -156,13 +172,41 @@ function resolveSelectedConnection(projectRoot, options = {}) {
|
|
|
156
172
|
if (!connection) {
|
|
157
173
|
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
158
174
|
}
|
|
159
|
-
return { alias: repo.selected, connection, serverProjectRoot: repo
|
|
175
|
+
return { alias: repo.selected, connection, serverProjectRoot: rootAllowedForSelection(repo, connection) };
|
|
160
176
|
}
|
|
161
|
-
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
177
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot, metadata = {}) {
|
|
162
178
|
const repo = readRepoConnection(projectRoot);
|
|
163
179
|
if (!repo)
|
|
164
180
|
return;
|
|
165
|
-
|
|
181
|
+
let inferred = metadata;
|
|
182
|
+
if (!inferred.alias || !inferred.baseUrl) {
|
|
183
|
+
try {
|
|
184
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
185
|
+
if (selected?.connection.kind === "remote") {
|
|
186
|
+
inferred = {
|
|
187
|
+
alias: inferred.alias ?? selected.alias,
|
|
188
|
+
baseUrl: inferred.baseUrl ?? selected.connection.baseUrl
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
} catch {}
|
|
192
|
+
}
|
|
193
|
+
writeRepoConnection(projectRoot, {
|
|
194
|
+
...repo,
|
|
195
|
+
...metadata.project ? { project: metadata.project } : {},
|
|
196
|
+
serverProjectRoot,
|
|
197
|
+
...inferred.alias ? { serverProjectRootAlias: inferred.alias } : {},
|
|
198
|
+
...inferred.baseUrl ? { serverProjectRootBaseUrl: inferred.baseUrl.replace(/\/+$/, "") } : {}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function clearRepoServerProjectRoot(projectRoot) {
|
|
202
|
+
const repo = readRepoConnection(projectRoot);
|
|
203
|
+
if (!repo)
|
|
204
|
+
return;
|
|
205
|
+
writeRepoConnection(projectRoot, {
|
|
206
|
+
selected: repo.selected,
|
|
207
|
+
...repo.project ? { project: repo.project } : {},
|
|
208
|
+
...repo.linkedAt ? { linkedAt: repo.linkedAt } : {}
|
|
209
|
+
});
|
|
166
210
|
}
|
|
167
211
|
|
|
168
212
|
// packages/cli/src/commands/_server-client.ts
|
|
@@ -208,11 +252,10 @@ function readStoredGitHubAuthToken(projectRoot) {
|
|
|
208
252
|
const parsed = readRemoteAuthState(projectRoot);
|
|
209
253
|
return parsed ? cleanToken(typeof parsed.token === "string" ? parsed.token : undefined) : null;
|
|
210
254
|
}
|
|
211
|
-
function
|
|
212
|
-
|
|
213
|
-
const slug = repo?.project?.trim();
|
|
214
|
-
if (!slug || !/^[^/]+\/[^/]+$/.test(slug))
|
|
255
|
+
function inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug) {
|
|
256
|
+
if (!/^[^/]+\/[^/]+$/.test(repoSlug))
|
|
215
257
|
return null;
|
|
258
|
+
const repo = readRepoConnection(projectRoot);
|
|
216
259
|
const auth = readRemoteAuthState(projectRoot);
|
|
217
260
|
const authUpdatedAt = typeof auth?.updatedAt === "string" ? Date.parse(auth.updatedAt) : Number.NaN;
|
|
218
261
|
const repoLinkedAt = typeof repo?.linkedAt === "string" ? Date.parse(repo.linkedAt) : Number.NaN;
|
|
@@ -221,25 +264,413 @@ function inferRemoteServerProjectRootFromAuthState(projectRoot) {
|
|
|
221
264
|
const checkoutBaseDir = typeof auth?.checkoutBaseDir === "string" && auth.checkoutBaseDir.trim() ? auth.checkoutBaseDir.trim() : null;
|
|
222
265
|
if (!checkoutBaseDir)
|
|
223
266
|
return null;
|
|
224
|
-
|
|
225
|
-
writeRepoServerProjectRoot(projectRoot, inferred);
|
|
226
|
-
return inferred;
|
|
267
|
+
return `${checkoutBaseDir.replace(/\/+$/, "")}/${repoSlug}`;
|
|
227
268
|
}
|
|
228
269
|
function readLocalConnectionFallbackToken(projectRoot) {
|
|
229
270
|
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
230
271
|
}
|
|
272
|
+
function normalizeRepoSlug(value) {
|
|
273
|
+
const slug = value?.trim();
|
|
274
|
+
return slug && /^[^/\s]+\/[^/\s]+$/.test(slug) ? slug : null;
|
|
275
|
+
}
|
|
276
|
+
function readProjectLinkSlug(projectRoot) {
|
|
277
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
278
|
+
if (!existsSync2(path))
|
|
279
|
+
return null;
|
|
280
|
+
try {
|
|
281
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
282
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
283
|
+
return null;
|
|
284
|
+
return normalizeRepoSlug(typeof parsed.repoSlug === "string" ? String(parsed.repoSlug) : null);
|
|
285
|
+
} catch {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function writeProjectLinkConnection(projectRoot, repoSlug, alias) {
|
|
290
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
291
|
+
mkdirSync2(dirname2(path), { recursive: true });
|
|
292
|
+
writeFileSync2(path, `${JSON.stringify({ repoSlug, connection: alias, linkedAt: new Date().toISOString() }, null, 2)}
|
|
293
|
+
`, "utf8");
|
|
294
|
+
}
|
|
295
|
+
function remoteLinkRepairCommand(repoSlug) {
|
|
296
|
+
return repoSlug ? `rig server repair-link --repo ${repoSlug}` : "rig server repair-link --repo owner/repo";
|
|
297
|
+
}
|
|
298
|
+
function formatRemoteProjectLinkHint(resolution) {
|
|
299
|
+
const repair = remoteLinkRepairCommand(resolution.repoSlug);
|
|
300
|
+
if (resolution.status === "auth_required") {
|
|
301
|
+
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}` : ""}\`.`;
|
|
302
|
+
}
|
|
303
|
+
if (resolution.status === "missing_project") {
|
|
304
|
+
return `Record the repo slug, then run \`${repair}\` (or \`rig init --repo owner/repo --server remote --github-auth device\`).`;
|
|
305
|
+
}
|
|
306
|
+
if (resolution.status === "not_remote")
|
|
307
|
+
return "Select a remote server with `rig server use <alias>` before repairing a remote project link.";
|
|
308
|
+
return `Run \`${repair}\` to backfill or prepare a server-host checkout for the selected remote${resolution.alias ? ` (${resolution.alias})` : ""}.`;
|
|
309
|
+
}
|
|
310
|
+
function remoteProjectLinkFailure(input) {
|
|
311
|
+
const partial = {
|
|
312
|
+
status: input.status,
|
|
313
|
+
alias: input.alias,
|
|
314
|
+
baseUrl: input.baseUrl,
|
|
315
|
+
repoSlug: input.repoSlug
|
|
316
|
+
};
|
|
317
|
+
return {
|
|
318
|
+
ok: false,
|
|
319
|
+
...input,
|
|
320
|
+
hint: formatRemoteProjectLinkHint(partial),
|
|
321
|
+
next: remoteLinkRepairCommand(input.repoSlug)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function remoteProjectLinkSuccess(input) {
|
|
325
|
+
return {
|
|
326
|
+
ok: true,
|
|
327
|
+
status: input.status,
|
|
328
|
+
alias: input.alias,
|
|
329
|
+
baseUrl: input.baseUrl,
|
|
330
|
+
repoSlug: input.repoSlug,
|
|
331
|
+
serverProjectRoot: input.serverProjectRoot,
|
|
332
|
+
source: input.source,
|
|
333
|
+
prepared: input.prepared ?? input.status === "prepared",
|
|
334
|
+
validated: input.validated ?? false,
|
|
335
|
+
message: input.message ?? `Remote project link ready for ${input.repoSlug}.`,
|
|
336
|
+
hint: "Remote-scoped task/run/status requests will send x-rig-project-root for this server-host checkout.",
|
|
337
|
+
next: "rig task list"
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
function localCheckoutPathCandidate(projectRoot, candidate) {
|
|
341
|
+
try {
|
|
342
|
+
const local = resolve2(projectRoot);
|
|
343
|
+
const resolved = resolve2(candidate);
|
|
344
|
+
return resolved === local || resolved.startsWith(`${local}/`);
|
|
345
|
+
} catch {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
async function requestRemoteProjectLinkJson(baseUrl, pathname, authToken, init = {}) {
|
|
350
|
+
const requestUrl = new URL(`${baseUrl}${pathname}`);
|
|
351
|
+
if (authToken && queryAuthFallbackEnabled())
|
|
352
|
+
requestUrl.searchParams.set("rt", authToken);
|
|
353
|
+
const response = await fetch(requestUrl, {
|
|
354
|
+
...init,
|
|
355
|
+
headers: mergeHeaders(init.headers, authToken)
|
|
356
|
+
});
|
|
357
|
+
const text = await response.text();
|
|
358
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
359
|
+
try {
|
|
360
|
+
return JSON.parse(text);
|
|
361
|
+
} catch {
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
})() : null;
|
|
365
|
+
return { ok: response.ok, status: response.status, payload, text };
|
|
366
|
+
}
|
|
367
|
+
function payloadError(payload, fallback) {
|
|
368
|
+
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
|
369
|
+
const record = payload;
|
|
370
|
+
const value = record.error ?? record.message ?? record.reason;
|
|
371
|
+
if (typeof value === "string" && value.trim())
|
|
372
|
+
return value.trim();
|
|
373
|
+
}
|
|
374
|
+
return fallback;
|
|
375
|
+
}
|
|
376
|
+
function checkoutPathsFromProjectPayload(payload) {
|
|
377
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
378
|
+
return [];
|
|
379
|
+
const project = payload.project;
|
|
380
|
+
if (!project || typeof project !== "object" || Array.isArray(project))
|
|
381
|
+
return [];
|
|
382
|
+
const checkouts = project.checkouts;
|
|
383
|
+
if (!Array.isArray(checkouts))
|
|
384
|
+
return [];
|
|
385
|
+
return [...checkouts].reverse().flatMap((entry) => {
|
|
386
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
387
|
+
return [];
|
|
388
|
+
const path = entry.path;
|
|
389
|
+
return typeof path === "string" && path.trim() ? [path.trim()] : [];
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
function checkoutPathFromPreparePayload(payload) {
|
|
393
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
394
|
+
return null;
|
|
395
|
+
const checkout = payload.checkout;
|
|
396
|
+
if (!checkout || typeof checkout !== "object" || Array.isArray(checkout))
|
|
397
|
+
return null;
|
|
398
|
+
const path = checkout.path;
|
|
399
|
+
return typeof path === "string" && path.trim() ? path.trim() : null;
|
|
400
|
+
}
|
|
401
|
+
async function validateAndPersistRemoteRoot(input) {
|
|
402
|
+
const candidate = input.candidate.trim();
|
|
403
|
+
if (!candidate || !isAbsolute(candidate)) {
|
|
404
|
+
return remoteProjectLinkFailure({
|
|
405
|
+
status: "invalid_root",
|
|
406
|
+
alias: input.alias,
|
|
407
|
+
baseUrl: input.baseUrl,
|
|
408
|
+
repoSlug: input.repoSlug,
|
|
409
|
+
message: `Remote project root candidate is not an absolute server-host path: ${candidate || "(empty)"}.`
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
if (localCheckoutPathCandidate(input.projectRoot, candidate)) {
|
|
413
|
+
return remoteProjectLinkFailure({
|
|
414
|
+
status: "invalid_root",
|
|
415
|
+
alias: input.alias,
|
|
416
|
+
baseUrl: input.baseUrl,
|
|
417
|
+
repoSlug: input.repoSlug,
|
|
418
|
+
message: `Refusing to use local checkout path ${candidate} as a remote server project root.`
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
let response;
|
|
422
|
+
try {
|
|
423
|
+
response = await requestRemoteProjectLinkJson(input.baseUrl, "/api/server/project-root", input.authToken, {
|
|
424
|
+
method: "POST",
|
|
425
|
+
headers: { "content-type": "application/json" },
|
|
426
|
+
body: JSON.stringify({ projectRoot: candidate })
|
|
427
|
+
});
|
|
428
|
+
} catch (error) {
|
|
429
|
+
return remoteProjectLinkFailure({
|
|
430
|
+
status: "error",
|
|
431
|
+
alias: input.alias,
|
|
432
|
+
baseUrl: input.baseUrl,
|
|
433
|
+
repoSlug: input.repoSlug,
|
|
434
|
+
message: `Could not validate remote project root ${candidate}: ${error instanceof Error ? error.message : String(error)}`
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
if (response.status === 401 || response.status === 403) {
|
|
438
|
+
return remoteProjectLinkFailure({
|
|
439
|
+
status: "auth_required",
|
|
440
|
+
alias: input.alias,
|
|
441
|
+
baseUrl: input.baseUrl,
|
|
442
|
+
repoSlug: input.repoSlug,
|
|
443
|
+
statusCode: response.status,
|
|
444
|
+
message: `Remote server ${input.alias} requires authentication before validating project root ${candidate}.`
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
if (!response.ok) {
|
|
448
|
+
return remoteProjectLinkFailure({
|
|
449
|
+
status: "invalid_root",
|
|
450
|
+
alias: input.alias,
|
|
451
|
+
baseUrl: input.baseUrl,
|
|
452
|
+
repoSlug: input.repoSlug,
|
|
453
|
+
statusCode: response.status,
|
|
454
|
+
message: `Remote server rejected project root ${candidate} (${response.status}): ${payloadError(response.payload, response.text || "project-root validation failed")}`
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
const record = response.payload && typeof response.payload === "object" && !Array.isArray(response.payload) ? response.payload : {};
|
|
458
|
+
if (record.ok !== true) {
|
|
459
|
+
return remoteProjectLinkFailure({
|
|
460
|
+
status: "invalid_root",
|
|
461
|
+
alias: input.alias,
|
|
462
|
+
baseUrl: input.baseUrl,
|
|
463
|
+
repoSlug: input.repoSlug,
|
|
464
|
+
message: `Remote server did not accept project root ${candidate}: ${payloadError(response.payload, "project-root validation failed")}`
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
const accepted = typeof record.projectRoot === "string" && record.projectRoot.trim() ? record.projectRoot.trim() : typeof record.requestedProjectRoot === "string" && record.requestedProjectRoot.trim() ? record.requestedProjectRoot.trim() : candidate;
|
|
468
|
+
writeRepoServerProjectRoot(input.projectRoot, accepted, { alias: input.alias, baseUrl: input.baseUrl, project: input.repoSlug });
|
|
469
|
+
writeProjectLinkConnection(input.projectRoot, input.repoSlug, input.alias);
|
|
470
|
+
const status = input.status ?? (input.prepared ? "prepared" : "backfilled");
|
|
471
|
+
return remoteProjectLinkSuccess({
|
|
472
|
+
status,
|
|
473
|
+
alias: input.alias,
|
|
474
|
+
baseUrl: input.baseUrl,
|
|
475
|
+
repoSlug: input.repoSlug,
|
|
476
|
+
serverProjectRoot: accepted,
|
|
477
|
+
source: input.source,
|
|
478
|
+
prepared: input.prepared,
|
|
479
|
+
validated: true,
|
|
480
|
+
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}.`
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
async function ensureRemoteProjectRootLink(projectRoot, options = {}) {
|
|
484
|
+
let selected;
|
|
485
|
+
try {
|
|
486
|
+
selected = resolveSelectedConnection(projectRoot);
|
|
487
|
+
} catch (error) {
|
|
488
|
+
return remoteProjectLinkFailure({ status: "error", message: error instanceof Error ? error.message : String(error) });
|
|
489
|
+
}
|
|
490
|
+
if (!selected || selected.connection.kind !== "remote") {
|
|
491
|
+
return remoteProjectLinkFailure({ status: "not_remote", message: "No remote Rig server is selected for this repo." });
|
|
492
|
+
}
|
|
493
|
+
const repo = readRepoConnection(projectRoot);
|
|
494
|
+
const explicitRepoRequested = options.repoSlug !== undefined;
|
|
495
|
+
const explicitRepoSlug = explicitRepoRequested ? normalizeRepoSlug(options.repoSlug) : null;
|
|
496
|
+
const repoProjectSlug = normalizeRepoSlug(repo?.project);
|
|
497
|
+
const repoSlug = explicitRepoSlug ?? repoProjectSlug ?? readProjectLinkSlug(projectRoot);
|
|
498
|
+
const alias = selected.alias;
|
|
499
|
+
const baseUrl = selected.connection.baseUrl;
|
|
500
|
+
const authToken = options.authToken === undefined ? readGitHubBearerTokenForRemote(projectRoot) : options.authToken;
|
|
501
|
+
const mode = options.mode ?? "backfill-only";
|
|
502
|
+
if (explicitRepoRequested && !explicitRepoSlug) {
|
|
503
|
+
return remoteProjectLinkFailure({
|
|
504
|
+
status: "missing_project",
|
|
505
|
+
alias,
|
|
506
|
+
baseUrl,
|
|
507
|
+
message: `Invalid --repo value ${JSON.stringify(options.repoSlug)}. Expected owner/repo.`
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
if (!repoSlug) {
|
|
511
|
+
return remoteProjectLinkFailure({
|
|
512
|
+
status: "missing_project",
|
|
513
|
+
alias,
|
|
514
|
+
baseUrl,
|
|
515
|
+
message: `Remote server ${alias} is selected, but this checkout has no GitHub repo slug recorded.`
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
const skippedCandidates = [];
|
|
519
|
+
const storedRoot = selected.serverProjectRoot?.trim() || null;
|
|
520
|
+
const storedRootMatchesResolvedRepo = repoProjectSlug === repoSlug;
|
|
521
|
+
if (storedRoot && storedRootMatchesResolvedRepo) {
|
|
522
|
+
if (localCheckoutPathCandidate(projectRoot, storedRoot)) {
|
|
523
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
524
|
+
skippedCandidates.push(storedRoot);
|
|
525
|
+
} else {
|
|
526
|
+
const storedResult = await validateAndPersistRemoteRoot({
|
|
527
|
+
projectRoot,
|
|
528
|
+
alias,
|
|
529
|
+
baseUrl,
|
|
530
|
+
authToken,
|
|
531
|
+
repoSlug,
|
|
532
|
+
candidate: storedRoot,
|
|
533
|
+
source: "stored",
|
|
534
|
+
status: "ready"
|
|
535
|
+
});
|
|
536
|
+
if (storedResult.ok || storedResult.status === "auth_required" || storedResult.status === "error")
|
|
537
|
+
return storedResult;
|
|
538
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
539
|
+
skippedCandidates.push(storedRoot);
|
|
540
|
+
}
|
|
541
|
+
} else if (storedRoot) {
|
|
542
|
+
skippedCandidates.push(storedRoot);
|
|
543
|
+
}
|
|
544
|
+
const authCandidate = inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug);
|
|
545
|
+
if (authCandidate) {
|
|
546
|
+
const authResult = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: authCandidate, source: "auth-state" });
|
|
547
|
+
if (authResult.ok || authResult.status === "auth_required")
|
|
548
|
+
return authResult;
|
|
549
|
+
}
|
|
550
|
+
let registryResponse;
|
|
551
|
+
try {
|
|
552
|
+
registryResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}`, authToken);
|
|
553
|
+
} catch (error) {
|
|
554
|
+
return remoteProjectLinkFailure({
|
|
555
|
+
status: "error",
|
|
556
|
+
alias,
|
|
557
|
+
baseUrl,
|
|
558
|
+
repoSlug,
|
|
559
|
+
message: `Could not read remote project registry for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
if (registryResponse.status === 401 || registryResponse.status === 403) {
|
|
563
|
+
return remoteProjectLinkFailure({
|
|
564
|
+
status: "auth_required",
|
|
565
|
+
alias,
|
|
566
|
+
baseUrl,
|
|
567
|
+
repoSlug,
|
|
568
|
+
statusCode: registryResponse.status,
|
|
569
|
+
message: `Remote server ${alias} requires authentication before reading the ${repoSlug} project registry.`
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
if (registryResponse.ok) {
|
|
573
|
+
const candidates = checkoutPathsFromProjectPayload(registryResponse.payload);
|
|
574
|
+
for (const candidate of candidates) {
|
|
575
|
+
const result = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate, source: "registry" });
|
|
576
|
+
if (result.ok || result.status === "auth_required")
|
|
577
|
+
return result;
|
|
578
|
+
skippedCandidates.push(candidate);
|
|
579
|
+
}
|
|
580
|
+
if (mode === "backfill-only") {
|
|
581
|
+
return remoteProjectLinkFailure({
|
|
582
|
+
status: candidates.length > 0 ? "invalid_root" : "no_server_checkout",
|
|
583
|
+
alias,
|
|
584
|
+
baseUrl,
|
|
585
|
+
repoSlug,
|
|
586
|
+
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.`,
|
|
587
|
+
skippedCandidates
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
} else if (registryResponse.status === 404) {
|
|
591
|
+
if (mode === "backfill-only") {
|
|
592
|
+
return remoteProjectLinkFailure({
|
|
593
|
+
status: "project_not_registered",
|
|
594
|
+
alias,
|
|
595
|
+
baseUrl,
|
|
596
|
+
repoSlug,
|
|
597
|
+
statusCode: registryResponse.status,
|
|
598
|
+
message: `Remote server ${alias} has no registered project record for ${repoSlug}.`
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
} else {
|
|
602
|
+
return remoteProjectLinkFailure({
|
|
603
|
+
status: "error",
|
|
604
|
+
alias,
|
|
605
|
+
baseUrl,
|
|
606
|
+
repoSlug,
|
|
607
|
+
statusCode: registryResponse.status,
|
|
608
|
+
message: `Could not backfill ${repoSlug} from remote registry (${registryResponse.status}): ${payloadError(registryResponse.payload, registryResponse.text || "registry lookup failed")}`,
|
|
609
|
+
skippedCandidates
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
let prepareResponse;
|
|
613
|
+
try {
|
|
614
|
+
prepareResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}/prepare-checkout`, authToken, {
|
|
615
|
+
method: "POST",
|
|
616
|
+
headers: { "content-type": "application/json" },
|
|
617
|
+
body: JSON.stringify({ checkout: { kind: "managed-clone" }, repoUrl: `https://github.com/${repoSlug}.git` })
|
|
618
|
+
});
|
|
619
|
+
} catch (error) {
|
|
620
|
+
return remoteProjectLinkFailure({
|
|
621
|
+
status: "error",
|
|
622
|
+
alias,
|
|
623
|
+
baseUrl,
|
|
624
|
+
repoSlug,
|
|
625
|
+
message: `Could not prepare remote checkout for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
if (prepareResponse.status === 401 || prepareResponse.status === 403) {
|
|
629
|
+
return remoteProjectLinkFailure({
|
|
630
|
+
status: "auth_required",
|
|
631
|
+
alias,
|
|
632
|
+
baseUrl,
|
|
633
|
+
repoSlug,
|
|
634
|
+
statusCode: prepareResponse.status,
|
|
635
|
+
message: `Remote server ${alias} requires authentication before preparing ${repoSlug}.`
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
if (!prepareResponse.ok) {
|
|
639
|
+
return remoteProjectLinkFailure({
|
|
640
|
+
status: "error",
|
|
641
|
+
alias,
|
|
642
|
+
baseUrl,
|
|
643
|
+
repoSlug,
|
|
644
|
+
statusCode: prepareResponse.status,
|
|
645
|
+
message: `Remote checkout prepare failed for ${repoSlug} (${prepareResponse.status}): ${payloadError(prepareResponse.payload, prepareResponse.text || "prepare-checkout failed")}`,
|
|
646
|
+
skippedCandidates
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
const preparedPath = checkoutPathFromPreparePayload(prepareResponse.payload);
|
|
650
|
+
if (!preparedPath) {
|
|
651
|
+
return remoteProjectLinkFailure({
|
|
652
|
+
status: "invalid_root",
|
|
653
|
+
alias,
|
|
654
|
+
baseUrl,
|
|
655
|
+
repoSlug,
|
|
656
|
+
message: `Remote checkout prepare for ${repoSlug} did not return a checkout.path.`,
|
|
657
|
+
skippedCandidates
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
return validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: preparedPath, source: "prepare", prepared: true });
|
|
661
|
+
}
|
|
231
662
|
async function ensureServerForCli(projectRoot) {
|
|
232
663
|
try {
|
|
233
664
|
const selected = resolveSelectedConnection(projectRoot);
|
|
234
665
|
if (selected?.connection.kind === "remote") {
|
|
235
666
|
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
236
667
|
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
237
|
-
const
|
|
668
|
+
const link = await ensureRemoteProjectRootLink(projectRoot, { mode: "backfill-only", authToken });
|
|
238
669
|
return {
|
|
239
670
|
baseUrl: selected.connection.baseUrl,
|
|
240
671
|
authToken,
|
|
241
672
|
connectionKind: "remote",
|
|
242
|
-
serverProjectRoot
|
|
673
|
+
serverProjectRoot: link.ok ? link.serverProjectRoot ?? null : null
|
|
243
674
|
};
|
|
244
675
|
}
|
|
245
676
|
reportServerPhase("Starting local Rig server\u2026");
|
|
@@ -257,32 +688,6 @@ async function ensureServerForCli(projectRoot) {
|
|
|
257
688
|
throw error;
|
|
258
689
|
}
|
|
259
690
|
}
|
|
260
|
-
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
261
|
-
const repo = readRepoConnection(projectRoot);
|
|
262
|
-
const slug = repo?.project?.trim();
|
|
263
|
-
if (!slug)
|
|
264
|
-
return null;
|
|
265
|
-
try {
|
|
266
|
-
const url = new URL(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`);
|
|
267
|
-
if (authToken && queryAuthFallbackEnabled())
|
|
268
|
-
url.searchParams.set("rt", authToken);
|
|
269
|
-
const response = await fetch(url, {
|
|
270
|
-
headers: mergeHeaders(undefined, authToken)
|
|
271
|
-
});
|
|
272
|
-
if (!response.ok)
|
|
273
|
-
return null;
|
|
274
|
-
const payload = await response.json();
|
|
275
|
-
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
276
|
-
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
277
|
-
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
278
|
-
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
279
|
-
if (path)
|
|
280
|
-
writeRepoServerProjectRoot(projectRoot, path);
|
|
281
|
-
return path;
|
|
282
|
-
} catch {
|
|
283
|
-
return null;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
691
|
function mergeCookie(existing, name, value) {
|
|
287
692
|
const encoded = `${name}=${encodeURIComponent(value)}`;
|
|
288
693
|
if (!existing?.trim())
|
|
@@ -374,13 +779,20 @@ function canUseRemoteWithoutProjectRoot(pathname) {
|
|
|
374
779
|
async function requestServerJson(context, pathname, init = {}) {
|
|
375
780
|
const server = await ensureServerForCli(context.projectRoot);
|
|
376
781
|
const requestUrl = new URL(`${server.baseUrl}${pathname}`);
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
782
|
+
let scopedServerProjectRoot = server.serverProjectRoot;
|
|
783
|
+
if (server.connectionKind === "remote" && !scopedServerProjectRoot && !canUseRemoteWithoutProjectRoot(requestUrl.pathname) && (server.authToken || !isLoopbackRemoteBaseUrl(server.baseUrl))) {
|
|
784
|
+
const link = await ensureRemoteProjectRootLink(context.projectRoot, { mode: "backfill-only", authToken: server.authToken });
|
|
785
|
+
if (link.ok && link.serverProjectRoot) {
|
|
786
|
+
scopedServerProjectRoot = link.serverProjectRoot;
|
|
787
|
+
} else {
|
|
788
|
+
const repo = readRepoConnection(context.projectRoot);
|
|
789
|
+
const target = link.alias && link.baseUrl ? `${link.alias} (${link.baseUrl})` : server.baseUrl;
|
|
790
|
+
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 });
|
|
791
|
+
}
|
|
380
792
|
}
|
|
381
793
|
const headers = mergeHeaders(init.headers, server.authToken);
|
|
382
|
-
if (
|
|
383
|
-
headers.set("x-rig-project-root",
|
|
794
|
+
if (scopedServerProjectRoot)
|
|
795
|
+
headers.set("x-rig-project-root", scopedServerProjectRoot);
|
|
384
796
|
if (server.connectionKind === "remote" && server.authToken && queryAuthFallbackEnabled()) {
|
|
385
797
|
requestUrl.searchParams.set("rt", server.authToken);
|
|
386
798
|
}
|