@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
|
@@ -268,8 +268,8 @@ function formatInboxList(kind, entries) {
|
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
// packages/cli/src/commands/_server-client.ts
|
|
271
|
-
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
272
|
-
import { resolve as resolve2 } from "path";
|
|
271
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
272
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
|
|
273
273
|
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
274
274
|
|
|
275
275
|
// packages/cli/src/commands/_connection-state.ts
|
|
@@ -343,12 +343,28 @@ function readRepoConnection(projectRoot) {
|
|
|
343
343
|
selected,
|
|
344
344
|
project: typeof record.project === "string" ? record.project : undefined,
|
|
345
345
|
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
346
|
-
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
346
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined,
|
|
347
|
+
serverProjectRootAlias: typeof record.serverProjectRootAlias === "string" && record.serverProjectRootAlias.trim() ? record.serverProjectRootAlias.trim() : undefined,
|
|
348
|
+
serverProjectRootBaseUrl: typeof record.serverProjectRootBaseUrl === "string" && record.serverProjectRootBaseUrl.trim() ? record.serverProjectRootBaseUrl.trim().replace(/\/+$/, "") : undefined
|
|
347
349
|
};
|
|
348
350
|
}
|
|
349
351
|
function writeRepoConnection(projectRoot, state) {
|
|
350
352
|
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
351
353
|
}
|
|
354
|
+
function rootAllowedForSelection(repo, connection) {
|
|
355
|
+
const root = repo.serverProjectRoot?.trim();
|
|
356
|
+
if (!root)
|
|
357
|
+
return;
|
|
358
|
+
if (connection.kind === "remote") {
|
|
359
|
+
if (repo.serverProjectRootAlias !== repo.selected)
|
|
360
|
+
return;
|
|
361
|
+
if (repo.serverProjectRootBaseUrl !== connection.baseUrl)
|
|
362
|
+
return;
|
|
363
|
+
} else if (repo.serverProjectRootAlias && repo.serverProjectRootAlias !== repo.selected) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
return root;
|
|
367
|
+
}
|
|
352
368
|
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
353
369
|
const repo = readRepoConnection(projectRoot);
|
|
354
370
|
if (!repo)
|
|
@@ -360,13 +376,41 @@ function resolveSelectedConnection(projectRoot, options = {}) {
|
|
|
360
376
|
if (!connection) {
|
|
361
377
|
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
362
378
|
}
|
|
363
|
-
return { alias: repo.selected, connection, serverProjectRoot: repo
|
|
379
|
+
return { alias: repo.selected, connection, serverProjectRoot: rootAllowedForSelection(repo, connection) };
|
|
380
|
+
}
|
|
381
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot, metadata = {}) {
|
|
382
|
+
const repo = readRepoConnection(projectRoot);
|
|
383
|
+
if (!repo)
|
|
384
|
+
return;
|
|
385
|
+
let inferred = metadata;
|
|
386
|
+
if (!inferred.alias || !inferred.baseUrl) {
|
|
387
|
+
try {
|
|
388
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
389
|
+
if (selected?.connection.kind === "remote") {
|
|
390
|
+
inferred = {
|
|
391
|
+
alias: inferred.alias ?? selected.alias,
|
|
392
|
+
baseUrl: inferred.baseUrl ?? selected.connection.baseUrl
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
} catch {}
|
|
396
|
+
}
|
|
397
|
+
writeRepoConnection(projectRoot, {
|
|
398
|
+
...repo,
|
|
399
|
+
...metadata.project ? { project: metadata.project } : {},
|
|
400
|
+
serverProjectRoot,
|
|
401
|
+
...inferred.alias ? { serverProjectRootAlias: inferred.alias } : {},
|
|
402
|
+
...inferred.baseUrl ? { serverProjectRootBaseUrl: inferred.baseUrl.replace(/\/+$/, "") } : {}
|
|
403
|
+
});
|
|
364
404
|
}
|
|
365
|
-
function
|
|
405
|
+
function clearRepoServerProjectRoot(projectRoot) {
|
|
366
406
|
const repo = readRepoConnection(projectRoot);
|
|
367
407
|
if (!repo)
|
|
368
408
|
return;
|
|
369
|
-
writeRepoConnection(projectRoot, {
|
|
409
|
+
writeRepoConnection(projectRoot, {
|
|
410
|
+
selected: repo.selected,
|
|
411
|
+
...repo.project ? { project: repo.project } : {},
|
|
412
|
+
...repo.linkedAt ? { linkedAt: repo.linkedAt } : {}
|
|
413
|
+
});
|
|
370
414
|
}
|
|
371
415
|
|
|
372
416
|
// packages/cli/src/commands/_server-client.ts
|
|
@@ -412,11 +456,10 @@ function readStoredGitHubAuthToken(projectRoot) {
|
|
|
412
456
|
const parsed = readRemoteAuthState(projectRoot);
|
|
413
457
|
return parsed ? cleanToken(typeof parsed.token === "string" ? parsed.token : undefined) : null;
|
|
414
458
|
}
|
|
415
|
-
function
|
|
416
|
-
|
|
417
|
-
const slug = repo?.project?.trim();
|
|
418
|
-
if (!slug || !/^[^/]+\/[^/]+$/.test(slug))
|
|
459
|
+
function inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug) {
|
|
460
|
+
if (!/^[^/]+\/[^/]+$/.test(repoSlug))
|
|
419
461
|
return null;
|
|
462
|
+
const repo = readRepoConnection(projectRoot);
|
|
420
463
|
const auth = readRemoteAuthState(projectRoot);
|
|
421
464
|
const authUpdatedAt = typeof auth?.updatedAt === "string" ? Date.parse(auth.updatedAt) : Number.NaN;
|
|
422
465
|
const repoLinkedAt = typeof repo?.linkedAt === "string" ? Date.parse(repo.linkedAt) : Number.NaN;
|
|
@@ -425,25 +468,413 @@ function inferRemoteServerProjectRootFromAuthState(projectRoot) {
|
|
|
425
468
|
const checkoutBaseDir = typeof auth?.checkoutBaseDir === "string" && auth.checkoutBaseDir.trim() ? auth.checkoutBaseDir.trim() : null;
|
|
426
469
|
if (!checkoutBaseDir)
|
|
427
470
|
return null;
|
|
428
|
-
|
|
429
|
-
writeRepoServerProjectRoot(projectRoot, inferred);
|
|
430
|
-
return inferred;
|
|
471
|
+
return `${checkoutBaseDir.replace(/\/+$/, "")}/${repoSlug}`;
|
|
431
472
|
}
|
|
432
473
|
function readLocalConnectionFallbackToken(projectRoot) {
|
|
433
474
|
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
434
475
|
}
|
|
476
|
+
function normalizeRepoSlug(value) {
|
|
477
|
+
const slug = value?.trim();
|
|
478
|
+
return slug && /^[^/\s]+\/[^/\s]+$/.test(slug) ? slug : null;
|
|
479
|
+
}
|
|
480
|
+
function readProjectLinkSlug(projectRoot) {
|
|
481
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
482
|
+
if (!existsSync2(path))
|
|
483
|
+
return null;
|
|
484
|
+
try {
|
|
485
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
486
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
487
|
+
return null;
|
|
488
|
+
return normalizeRepoSlug(typeof parsed.repoSlug === "string" ? String(parsed.repoSlug) : null);
|
|
489
|
+
} catch {
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function writeProjectLinkConnection(projectRoot, repoSlug, alias) {
|
|
494
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
495
|
+
mkdirSync2(dirname2(path), { recursive: true });
|
|
496
|
+
writeFileSync2(path, `${JSON.stringify({ repoSlug, connection: alias, linkedAt: new Date().toISOString() }, null, 2)}
|
|
497
|
+
`, "utf8");
|
|
498
|
+
}
|
|
499
|
+
function remoteLinkRepairCommand(repoSlug) {
|
|
500
|
+
return repoSlug ? `rig server repair-link --repo ${repoSlug}` : "rig server repair-link --repo owner/repo";
|
|
501
|
+
}
|
|
502
|
+
function formatRemoteProjectLinkHint(resolution) {
|
|
503
|
+
const repair = remoteLinkRepairCommand(resolution.repoSlug);
|
|
504
|
+
if (resolution.status === "auth_required") {
|
|
505
|
+
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}` : ""}\`.`;
|
|
506
|
+
}
|
|
507
|
+
if (resolution.status === "missing_project") {
|
|
508
|
+
return `Record the repo slug, then run \`${repair}\` (or \`rig init --repo owner/repo --server remote --github-auth device\`).`;
|
|
509
|
+
}
|
|
510
|
+
if (resolution.status === "not_remote")
|
|
511
|
+
return "Select a remote server with `rig server use <alias>` before repairing a remote project link.";
|
|
512
|
+
return `Run \`${repair}\` to backfill or prepare a server-host checkout for the selected remote${resolution.alias ? ` (${resolution.alias})` : ""}.`;
|
|
513
|
+
}
|
|
514
|
+
function remoteProjectLinkFailure(input) {
|
|
515
|
+
const partial = {
|
|
516
|
+
status: input.status,
|
|
517
|
+
alias: input.alias,
|
|
518
|
+
baseUrl: input.baseUrl,
|
|
519
|
+
repoSlug: input.repoSlug
|
|
520
|
+
};
|
|
521
|
+
return {
|
|
522
|
+
ok: false,
|
|
523
|
+
...input,
|
|
524
|
+
hint: formatRemoteProjectLinkHint(partial),
|
|
525
|
+
next: remoteLinkRepairCommand(input.repoSlug)
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function remoteProjectLinkSuccess(input) {
|
|
529
|
+
return {
|
|
530
|
+
ok: true,
|
|
531
|
+
status: input.status,
|
|
532
|
+
alias: input.alias,
|
|
533
|
+
baseUrl: input.baseUrl,
|
|
534
|
+
repoSlug: input.repoSlug,
|
|
535
|
+
serverProjectRoot: input.serverProjectRoot,
|
|
536
|
+
source: input.source,
|
|
537
|
+
prepared: input.prepared ?? input.status === "prepared",
|
|
538
|
+
validated: input.validated ?? false,
|
|
539
|
+
message: input.message ?? `Remote project link ready for ${input.repoSlug}.`,
|
|
540
|
+
hint: "Remote-scoped task/run/status requests will send x-rig-project-root for this server-host checkout.",
|
|
541
|
+
next: "rig task list"
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
function localCheckoutPathCandidate(projectRoot, candidate) {
|
|
545
|
+
try {
|
|
546
|
+
const local = resolve2(projectRoot);
|
|
547
|
+
const resolved = resolve2(candidate);
|
|
548
|
+
return resolved === local || resolved.startsWith(`${local}/`);
|
|
549
|
+
} catch {
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
async function requestRemoteProjectLinkJson(baseUrl, pathname, authToken, init = {}) {
|
|
554
|
+
const requestUrl = new URL(`${baseUrl}${pathname}`);
|
|
555
|
+
if (authToken && queryAuthFallbackEnabled())
|
|
556
|
+
requestUrl.searchParams.set("rt", authToken);
|
|
557
|
+
const response = await fetch(requestUrl, {
|
|
558
|
+
...init,
|
|
559
|
+
headers: mergeHeaders(init.headers, authToken)
|
|
560
|
+
});
|
|
561
|
+
const text = await response.text();
|
|
562
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
563
|
+
try {
|
|
564
|
+
return JSON.parse(text);
|
|
565
|
+
} catch {
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
})() : null;
|
|
569
|
+
return { ok: response.ok, status: response.status, payload, text };
|
|
570
|
+
}
|
|
571
|
+
function payloadError(payload, fallback) {
|
|
572
|
+
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
|
573
|
+
const record = payload;
|
|
574
|
+
const value = record.error ?? record.message ?? record.reason;
|
|
575
|
+
if (typeof value === "string" && value.trim())
|
|
576
|
+
return value.trim();
|
|
577
|
+
}
|
|
578
|
+
return fallback;
|
|
579
|
+
}
|
|
580
|
+
function checkoutPathsFromProjectPayload(payload) {
|
|
581
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
582
|
+
return [];
|
|
583
|
+
const project = payload.project;
|
|
584
|
+
if (!project || typeof project !== "object" || Array.isArray(project))
|
|
585
|
+
return [];
|
|
586
|
+
const checkouts = project.checkouts;
|
|
587
|
+
if (!Array.isArray(checkouts))
|
|
588
|
+
return [];
|
|
589
|
+
return [...checkouts].reverse().flatMap((entry) => {
|
|
590
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
591
|
+
return [];
|
|
592
|
+
const path = entry.path;
|
|
593
|
+
return typeof path === "string" && path.trim() ? [path.trim()] : [];
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
function checkoutPathFromPreparePayload(payload) {
|
|
597
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
598
|
+
return null;
|
|
599
|
+
const checkout = payload.checkout;
|
|
600
|
+
if (!checkout || typeof checkout !== "object" || Array.isArray(checkout))
|
|
601
|
+
return null;
|
|
602
|
+
const path = checkout.path;
|
|
603
|
+
return typeof path === "string" && path.trim() ? path.trim() : null;
|
|
604
|
+
}
|
|
605
|
+
async function validateAndPersistRemoteRoot(input) {
|
|
606
|
+
const candidate = input.candidate.trim();
|
|
607
|
+
if (!candidate || !isAbsolute(candidate)) {
|
|
608
|
+
return remoteProjectLinkFailure({
|
|
609
|
+
status: "invalid_root",
|
|
610
|
+
alias: input.alias,
|
|
611
|
+
baseUrl: input.baseUrl,
|
|
612
|
+
repoSlug: input.repoSlug,
|
|
613
|
+
message: `Remote project root candidate is not an absolute server-host path: ${candidate || "(empty)"}.`
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
if (localCheckoutPathCandidate(input.projectRoot, candidate)) {
|
|
617
|
+
return remoteProjectLinkFailure({
|
|
618
|
+
status: "invalid_root",
|
|
619
|
+
alias: input.alias,
|
|
620
|
+
baseUrl: input.baseUrl,
|
|
621
|
+
repoSlug: input.repoSlug,
|
|
622
|
+
message: `Refusing to use local checkout path ${candidate} as a remote server project root.`
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
let response;
|
|
626
|
+
try {
|
|
627
|
+
response = await requestRemoteProjectLinkJson(input.baseUrl, "/api/server/project-root", input.authToken, {
|
|
628
|
+
method: "POST",
|
|
629
|
+
headers: { "content-type": "application/json" },
|
|
630
|
+
body: JSON.stringify({ projectRoot: candidate })
|
|
631
|
+
});
|
|
632
|
+
} catch (error) {
|
|
633
|
+
return remoteProjectLinkFailure({
|
|
634
|
+
status: "error",
|
|
635
|
+
alias: input.alias,
|
|
636
|
+
baseUrl: input.baseUrl,
|
|
637
|
+
repoSlug: input.repoSlug,
|
|
638
|
+
message: `Could not validate remote project root ${candidate}: ${error instanceof Error ? error.message : String(error)}`
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
if (response.status === 401 || response.status === 403) {
|
|
642
|
+
return remoteProjectLinkFailure({
|
|
643
|
+
status: "auth_required",
|
|
644
|
+
alias: input.alias,
|
|
645
|
+
baseUrl: input.baseUrl,
|
|
646
|
+
repoSlug: input.repoSlug,
|
|
647
|
+
statusCode: response.status,
|
|
648
|
+
message: `Remote server ${input.alias} requires authentication before validating project root ${candidate}.`
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
if (!response.ok) {
|
|
652
|
+
return remoteProjectLinkFailure({
|
|
653
|
+
status: "invalid_root",
|
|
654
|
+
alias: input.alias,
|
|
655
|
+
baseUrl: input.baseUrl,
|
|
656
|
+
repoSlug: input.repoSlug,
|
|
657
|
+
statusCode: response.status,
|
|
658
|
+
message: `Remote server rejected project root ${candidate} (${response.status}): ${payloadError(response.payload, response.text || "project-root validation failed")}`
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
const record = response.payload && typeof response.payload === "object" && !Array.isArray(response.payload) ? response.payload : {};
|
|
662
|
+
if (record.ok !== true) {
|
|
663
|
+
return remoteProjectLinkFailure({
|
|
664
|
+
status: "invalid_root",
|
|
665
|
+
alias: input.alias,
|
|
666
|
+
baseUrl: input.baseUrl,
|
|
667
|
+
repoSlug: input.repoSlug,
|
|
668
|
+
message: `Remote server did not accept project root ${candidate}: ${payloadError(response.payload, "project-root validation failed")}`
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
const accepted = typeof record.projectRoot === "string" && record.projectRoot.trim() ? record.projectRoot.trim() : typeof record.requestedProjectRoot === "string" && record.requestedProjectRoot.trim() ? record.requestedProjectRoot.trim() : candidate;
|
|
672
|
+
writeRepoServerProjectRoot(input.projectRoot, accepted, { alias: input.alias, baseUrl: input.baseUrl, project: input.repoSlug });
|
|
673
|
+
writeProjectLinkConnection(input.projectRoot, input.repoSlug, input.alias);
|
|
674
|
+
const status = input.status ?? (input.prepared ? "prepared" : "backfilled");
|
|
675
|
+
return remoteProjectLinkSuccess({
|
|
676
|
+
status,
|
|
677
|
+
alias: input.alias,
|
|
678
|
+
baseUrl: input.baseUrl,
|
|
679
|
+
repoSlug: input.repoSlug,
|
|
680
|
+
serverProjectRoot: accepted,
|
|
681
|
+
source: input.source,
|
|
682
|
+
prepared: input.prepared,
|
|
683
|
+
validated: true,
|
|
684
|
+
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}.`
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
async function ensureRemoteProjectRootLink(projectRoot, options = {}) {
|
|
688
|
+
let selected;
|
|
689
|
+
try {
|
|
690
|
+
selected = resolveSelectedConnection(projectRoot);
|
|
691
|
+
} catch (error) {
|
|
692
|
+
return remoteProjectLinkFailure({ status: "error", message: error instanceof Error ? error.message : String(error) });
|
|
693
|
+
}
|
|
694
|
+
if (!selected || selected.connection.kind !== "remote") {
|
|
695
|
+
return remoteProjectLinkFailure({ status: "not_remote", message: "No remote Rig server is selected for this repo." });
|
|
696
|
+
}
|
|
697
|
+
const repo = readRepoConnection(projectRoot);
|
|
698
|
+
const explicitRepoRequested = options.repoSlug !== undefined;
|
|
699
|
+
const explicitRepoSlug = explicitRepoRequested ? normalizeRepoSlug(options.repoSlug) : null;
|
|
700
|
+
const repoProjectSlug = normalizeRepoSlug(repo?.project);
|
|
701
|
+
const repoSlug = explicitRepoSlug ?? repoProjectSlug ?? readProjectLinkSlug(projectRoot);
|
|
702
|
+
const alias = selected.alias;
|
|
703
|
+
const baseUrl = selected.connection.baseUrl;
|
|
704
|
+
const authToken = options.authToken === undefined ? readGitHubBearerTokenForRemote(projectRoot) : options.authToken;
|
|
705
|
+
const mode = options.mode ?? "backfill-only";
|
|
706
|
+
if (explicitRepoRequested && !explicitRepoSlug) {
|
|
707
|
+
return remoteProjectLinkFailure({
|
|
708
|
+
status: "missing_project",
|
|
709
|
+
alias,
|
|
710
|
+
baseUrl,
|
|
711
|
+
message: `Invalid --repo value ${JSON.stringify(options.repoSlug)}. Expected owner/repo.`
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
if (!repoSlug) {
|
|
715
|
+
return remoteProjectLinkFailure({
|
|
716
|
+
status: "missing_project",
|
|
717
|
+
alias,
|
|
718
|
+
baseUrl,
|
|
719
|
+
message: `Remote server ${alias} is selected, but this checkout has no GitHub repo slug recorded.`
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
const skippedCandidates = [];
|
|
723
|
+
const storedRoot = selected.serverProjectRoot?.trim() || null;
|
|
724
|
+
const storedRootMatchesResolvedRepo = repoProjectSlug === repoSlug;
|
|
725
|
+
if (storedRoot && storedRootMatchesResolvedRepo) {
|
|
726
|
+
if (localCheckoutPathCandidate(projectRoot, storedRoot)) {
|
|
727
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
728
|
+
skippedCandidates.push(storedRoot);
|
|
729
|
+
} else {
|
|
730
|
+
const storedResult = await validateAndPersistRemoteRoot({
|
|
731
|
+
projectRoot,
|
|
732
|
+
alias,
|
|
733
|
+
baseUrl,
|
|
734
|
+
authToken,
|
|
735
|
+
repoSlug,
|
|
736
|
+
candidate: storedRoot,
|
|
737
|
+
source: "stored",
|
|
738
|
+
status: "ready"
|
|
739
|
+
});
|
|
740
|
+
if (storedResult.ok || storedResult.status === "auth_required" || storedResult.status === "error")
|
|
741
|
+
return storedResult;
|
|
742
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
743
|
+
skippedCandidates.push(storedRoot);
|
|
744
|
+
}
|
|
745
|
+
} else if (storedRoot) {
|
|
746
|
+
skippedCandidates.push(storedRoot);
|
|
747
|
+
}
|
|
748
|
+
const authCandidate = inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug);
|
|
749
|
+
if (authCandidate) {
|
|
750
|
+
const authResult = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: authCandidate, source: "auth-state" });
|
|
751
|
+
if (authResult.ok || authResult.status === "auth_required")
|
|
752
|
+
return authResult;
|
|
753
|
+
}
|
|
754
|
+
let registryResponse;
|
|
755
|
+
try {
|
|
756
|
+
registryResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}`, authToken);
|
|
757
|
+
} catch (error) {
|
|
758
|
+
return remoteProjectLinkFailure({
|
|
759
|
+
status: "error",
|
|
760
|
+
alias,
|
|
761
|
+
baseUrl,
|
|
762
|
+
repoSlug,
|
|
763
|
+
message: `Could not read remote project registry for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
if (registryResponse.status === 401 || registryResponse.status === 403) {
|
|
767
|
+
return remoteProjectLinkFailure({
|
|
768
|
+
status: "auth_required",
|
|
769
|
+
alias,
|
|
770
|
+
baseUrl,
|
|
771
|
+
repoSlug,
|
|
772
|
+
statusCode: registryResponse.status,
|
|
773
|
+
message: `Remote server ${alias} requires authentication before reading the ${repoSlug} project registry.`
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
if (registryResponse.ok) {
|
|
777
|
+
const candidates = checkoutPathsFromProjectPayload(registryResponse.payload);
|
|
778
|
+
for (const candidate of candidates) {
|
|
779
|
+
const result = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate, source: "registry" });
|
|
780
|
+
if (result.ok || result.status === "auth_required")
|
|
781
|
+
return result;
|
|
782
|
+
skippedCandidates.push(candidate);
|
|
783
|
+
}
|
|
784
|
+
if (mode === "backfill-only") {
|
|
785
|
+
return remoteProjectLinkFailure({
|
|
786
|
+
status: candidates.length > 0 ? "invalid_root" : "no_server_checkout",
|
|
787
|
+
alias,
|
|
788
|
+
baseUrl,
|
|
789
|
+
repoSlug,
|
|
790
|
+
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.`,
|
|
791
|
+
skippedCandidates
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
} else if (registryResponse.status === 404) {
|
|
795
|
+
if (mode === "backfill-only") {
|
|
796
|
+
return remoteProjectLinkFailure({
|
|
797
|
+
status: "project_not_registered",
|
|
798
|
+
alias,
|
|
799
|
+
baseUrl,
|
|
800
|
+
repoSlug,
|
|
801
|
+
statusCode: registryResponse.status,
|
|
802
|
+
message: `Remote server ${alias} has no registered project record for ${repoSlug}.`
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
} else {
|
|
806
|
+
return remoteProjectLinkFailure({
|
|
807
|
+
status: "error",
|
|
808
|
+
alias,
|
|
809
|
+
baseUrl,
|
|
810
|
+
repoSlug,
|
|
811
|
+
statusCode: registryResponse.status,
|
|
812
|
+
message: `Could not backfill ${repoSlug} from remote registry (${registryResponse.status}): ${payloadError(registryResponse.payload, registryResponse.text || "registry lookup failed")}`,
|
|
813
|
+
skippedCandidates
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
let prepareResponse;
|
|
817
|
+
try {
|
|
818
|
+
prepareResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}/prepare-checkout`, authToken, {
|
|
819
|
+
method: "POST",
|
|
820
|
+
headers: { "content-type": "application/json" },
|
|
821
|
+
body: JSON.stringify({ checkout: { kind: "managed-clone" }, repoUrl: `https://github.com/${repoSlug}.git` })
|
|
822
|
+
});
|
|
823
|
+
} catch (error) {
|
|
824
|
+
return remoteProjectLinkFailure({
|
|
825
|
+
status: "error",
|
|
826
|
+
alias,
|
|
827
|
+
baseUrl,
|
|
828
|
+
repoSlug,
|
|
829
|
+
message: `Could not prepare remote checkout for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
if (prepareResponse.status === 401 || prepareResponse.status === 403) {
|
|
833
|
+
return remoteProjectLinkFailure({
|
|
834
|
+
status: "auth_required",
|
|
835
|
+
alias,
|
|
836
|
+
baseUrl,
|
|
837
|
+
repoSlug,
|
|
838
|
+
statusCode: prepareResponse.status,
|
|
839
|
+
message: `Remote server ${alias} requires authentication before preparing ${repoSlug}.`
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
if (!prepareResponse.ok) {
|
|
843
|
+
return remoteProjectLinkFailure({
|
|
844
|
+
status: "error",
|
|
845
|
+
alias,
|
|
846
|
+
baseUrl,
|
|
847
|
+
repoSlug,
|
|
848
|
+
statusCode: prepareResponse.status,
|
|
849
|
+
message: `Remote checkout prepare failed for ${repoSlug} (${prepareResponse.status}): ${payloadError(prepareResponse.payload, prepareResponse.text || "prepare-checkout failed")}`,
|
|
850
|
+
skippedCandidates
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
const preparedPath = checkoutPathFromPreparePayload(prepareResponse.payload);
|
|
854
|
+
if (!preparedPath) {
|
|
855
|
+
return remoteProjectLinkFailure({
|
|
856
|
+
status: "invalid_root",
|
|
857
|
+
alias,
|
|
858
|
+
baseUrl,
|
|
859
|
+
repoSlug,
|
|
860
|
+
message: `Remote checkout prepare for ${repoSlug} did not return a checkout.path.`,
|
|
861
|
+
skippedCandidates
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
return validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: preparedPath, source: "prepare", prepared: true });
|
|
865
|
+
}
|
|
435
866
|
async function ensureServerForCli(projectRoot) {
|
|
436
867
|
try {
|
|
437
868
|
const selected = resolveSelectedConnection(projectRoot);
|
|
438
869
|
if (selected?.connection.kind === "remote") {
|
|
439
870
|
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
440
871
|
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
441
|
-
const
|
|
872
|
+
const link = await ensureRemoteProjectRootLink(projectRoot, { mode: "backfill-only", authToken });
|
|
442
873
|
return {
|
|
443
874
|
baseUrl: selected.connection.baseUrl,
|
|
444
875
|
authToken,
|
|
445
876
|
connectionKind: "remote",
|
|
446
|
-
serverProjectRoot
|
|
877
|
+
serverProjectRoot: link.ok ? link.serverProjectRoot ?? null : null
|
|
447
878
|
};
|
|
448
879
|
}
|
|
449
880
|
reportServerPhase("Starting local Rig server\u2026");
|
|
@@ -461,32 +892,6 @@ async function ensureServerForCli(projectRoot) {
|
|
|
461
892
|
throw error;
|
|
462
893
|
}
|
|
463
894
|
}
|
|
464
|
-
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
465
|
-
const repo = readRepoConnection(projectRoot);
|
|
466
|
-
const slug = repo?.project?.trim();
|
|
467
|
-
if (!slug)
|
|
468
|
-
return null;
|
|
469
|
-
try {
|
|
470
|
-
const url = new URL(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`);
|
|
471
|
-
if (authToken && queryAuthFallbackEnabled())
|
|
472
|
-
url.searchParams.set("rt", authToken);
|
|
473
|
-
const response = await fetch(url, {
|
|
474
|
-
headers: mergeHeaders(undefined, authToken)
|
|
475
|
-
});
|
|
476
|
-
if (!response.ok)
|
|
477
|
-
return null;
|
|
478
|
-
const payload = await response.json();
|
|
479
|
-
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
480
|
-
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
481
|
-
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
482
|
-
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
483
|
-
if (path)
|
|
484
|
-
writeRepoServerProjectRoot(projectRoot, path);
|
|
485
|
-
return path;
|
|
486
|
-
} catch {
|
|
487
|
-
return null;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
895
|
function mergeCookie(existing, name, value) {
|
|
491
896
|
const encoded = `${name}=${encodeURIComponent(value)}`;
|
|
492
897
|
if (!existing?.trim())
|
|
@@ -578,13 +983,20 @@ function canUseRemoteWithoutProjectRoot(pathname) {
|
|
|
578
983
|
async function requestServerJson(context, pathname, init = {}) {
|
|
579
984
|
const server = await ensureServerForCli(context.projectRoot);
|
|
580
985
|
const requestUrl = new URL(`${server.baseUrl}${pathname}`);
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
986
|
+
let scopedServerProjectRoot = server.serverProjectRoot;
|
|
987
|
+
if (server.connectionKind === "remote" && !scopedServerProjectRoot && !canUseRemoteWithoutProjectRoot(requestUrl.pathname) && (server.authToken || !isLoopbackRemoteBaseUrl(server.baseUrl))) {
|
|
988
|
+
const link = await ensureRemoteProjectRootLink(context.projectRoot, { mode: "backfill-only", authToken: server.authToken });
|
|
989
|
+
if (link.ok && link.serverProjectRoot) {
|
|
990
|
+
scopedServerProjectRoot = link.serverProjectRoot;
|
|
991
|
+
} else {
|
|
992
|
+
const repo = readRepoConnection(context.projectRoot);
|
|
993
|
+
const target = link.alias && link.baseUrl ? `${link.alias} (${link.baseUrl})` : server.baseUrl;
|
|
994
|
+
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 });
|
|
995
|
+
}
|
|
584
996
|
}
|
|
585
997
|
const headers = mergeHeaders(init.headers, server.authToken);
|
|
586
|
-
if (
|
|
587
|
-
headers.set("x-rig-project-root",
|
|
998
|
+
if (scopedServerProjectRoot)
|
|
999
|
+
headers.set("x-rig-project-root", scopedServerProjectRoot);
|
|
588
1000
|
if (server.connectionKind === "remote" && server.authToken && queryAuthFallbackEnabled()) {
|
|
589
1001
|
requestUrl.searchParams.set("rt", server.authToken);
|
|
590
1002
|
}
|