@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
package/dist/src/app/board.js
CHANGED
|
@@ -197,12 +197,13 @@ var PRIMARY_GROUPS = [
|
|
|
197
197
|
{
|
|
198
198
|
name: "server",
|
|
199
199
|
summary: "Choose, inspect, and start the Rig server that owns tasks and runs.",
|
|
200
|
-
usage: ["rig server <status|list|add|use|start> [options]"],
|
|
200
|
+
usage: ["rig server <status|list|add|use|repair-link|start> [options]"],
|
|
201
201
|
commands: [
|
|
202
|
-
{ command: "status", description: "Show the selected server for this repo.", primary: true },
|
|
202
|
+
{ command: "status", description: "Show the selected server and remote project-root link for this repo.", primary: true },
|
|
203
203
|
{ command: "use local", description: "Switch this repo to the local Rig server.", primary: true },
|
|
204
204
|
{ command: "add <alias> <url>", description: "Save a remote Rig server URL.", primary: true },
|
|
205
205
|
{ command: "use <alias>", description: "Select a saved remote server alias.", primary: true },
|
|
206
|
+
{ command: "repair-link [--prepare|--backfill-only] [--repo owner/repo]", description: "Backfill or prepare the selected remote's server-host checkout link.", primary: true },
|
|
206
207
|
{ command: "list", description: "List saved local/remote server aliases.", primary: true },
|
|
207
208
|
{ command: "start [--host <host>] [--port <n>]", description: "Start a local rig-server process." }
|
|
208
209
|
],
|
|
@@ -210,6 +211,7 @@ var PRIMARY_GROUPS = [
|
|
|
210
211
|
"rig server status",
|
|
211
212
|
"rig server add prod https://where.rig-does.work",
|
|
212
213
|
"rig server use prod",
|
|
214
|
+
"rig server repair-link --repo owner/repo",
|
|
213
215
|
"rig server use local",
|
|
214
216
|
"rig server start --port 3773"
|
|
215
217
|
],
|
|
@@ -515,8 +517,8 @@ var ADVANCED_GROUPS = [
|
|
|
515
517
|
var ALL_GROUPS = [...PRIMARY_GROUPS, ...ADVANCED_GROUPS];
|
|
516
518
|
|
|
517
519
|
// packages/cli/src/commands/_server-client.ts
|
|
518
|
-
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
519
|
-
import { resolve as resolve2 } from "path";
|
|
520
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
521
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
|
|
520
522
|
|
|
521
523
|
// packages/cli/src/runner.ts
|
|
522
524
|
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
@@ -608,12 +610,28 @@ function readRepoConnection(projectRoot) {
|
|
|
608
610
|
selected,
|
|
609
611
|
project: typeof record.project === "string" ? record.project : undefined,
|
|
610
612
|
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
611
|
-
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
613
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined,
|
|
614
|
+
serverProjectRootAlias: typeof record.serverProjectRootAlias === "string" && record.serverProjectRootAlias.trim() ? record.serverProjectRootAlias.trim() : undefined,
|
|
615
|
+
serverProjectRootBaseUrl: typeof record.serverProjectRootBaseUrl === "string" && record.serverProjectRootBaseUrl.trim() ? record.serverProjectRootBaseUrl.trim().replace(/\/+$/, "") : undefined
|
|
612
616
|
};
|
|
613
617
|
}
|
|
614
618
|
function writeRepoConnection(projectRoot, state) {
|
|
615
619
|
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
616
620
|
}
|
|
621
|
+
function rootAllowedForSelection(repo, connection) {
|
|
622
|
+
const root = repo.serverProjectRoot?.trim();
|
|
623
|
+
if (!root)
|
|
624
|
+
return;
|
|
625
|
+
if (connection.kind === "remote") {
|
|
626
|
+
if (repo.serverProjectRootAlias !== repo.selected)
|
|
627
|
+
return;
|
|
628
|
+
if (repo.serverProjectRootBaseUrl !== connection.baseUrl)
|
|
629
|
+
return;
|
|
630
|
+
} else if (repo.serverProjectRootAlias && repo.serverProjectRootAlias !== repo.selected) {
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
return root;
|
|
634
|
+
}
|
|
617
635
|
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
618
636
|
const repo = readRepoConnection(projectRoot);
|
|
619
637
|
if (!repo)
|
|
@@ -625,13 +643,41 @@ function resolveSelectedConnection(projectRoot, options = {}) {
|
|
|
625
643
|
if (!connection) {
|
|
626
644
|
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
627
645
|
}
|
|
628
|
-
return { alias: repo.selected, connection, serverProjectRoot: repo
|
|
646
|
+
return { alias: repo.selected, connection, serverProjectRoot: rootAllowedForSelection(repo, connection) };
|
|
629
647
|
}
|
|
630
|
-
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
648
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot, metadata = {}) {
|
|
631
649
|
const repo = readRepoConnection(projectRoot);
|
|
632
650
|
if (!repo)
|
|
633
651
|
return;
|
|
634
|
-
|
|
652
|
+
let inferred = metadata;
|
|
653
|
+
if (!inferred.alias || !inferred.baseUrl) {
|
|
654
|
+
try {
|
|
655
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
656
|
+
if (selected?.connection.kind === "remote") {
|
|
657
|
+
inferred = {
|
|
658
|
+
alias: inferred.alias ?? selected.alias,
|
|
659
|
+
baseUrl: inferred.baseUrl ?? selected.connection.baseUrl
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
} catch {}
|
|
663
|
+
}
|
|
664
|
+
writeRepoConnection(projectRoot, {
|
|
665
|
+
...repo,
|
|
666
|
+
...metadata.project ? { project: metadata.project } : {},
|
|
667
|
+
serverProjectRoot,
|
|
668
|
+
...inferred.alias ? { serverProjectRootAlias: inferred.alias } : {},
|
|
669
|
+
...inferred.baseUrl ? { serverProjectRootBaseUrl: inferred.baseUrl.replace(/\/+$/, "") } : {}
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
function clearRepoServerProjectRoot(projectRoot) {
|
|
673
|
+
const repo = readRepoConnection(projectRoot);
|
|
674
|
+
if (!repo)
|
|
675
|
+
return;
|
|
676
|
+
writeRepoConnection(projectRoot, {
|
|
677
|
+
selected: repo.selected,
|
|
678
|
+
...repo.project ? { project: repo.project } : {},
|
|
679
|
+
...repo.linkedAt ? { linkedAt: repo.linkedAt } : {}
|
|
680
|
+
});
|
|
635
681
|
}
|
|
636
682
|
|
|
637
683
|
// packages/cli/src/commands/_server-client.ts
|
|
@@ -672,11 +718,10 @@ function readStoredGitHubAuthToken(projectRoot) {
|
|
|
672
718
|
const parsed = readRemoteAuthState(projectRoot);
|
|
673
719
|
return parsed ? cleanToken(typeof parsed.token === "string" ? parsed.token : undefined) : null;
|
|
674
720
|
}
|
|
675
|
-
function
|
|
676
|
-
|
|
677
|
-
const slug = repo?.project?.trim();
|
|
678
|
-
if (!slug || !/^[^/]+\/[^/]+$/.test(slug))
|
|
721
|
+
function inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug) {
|
|
722
|
+
if (!/^[^/]+\/[^/]+$/.test(repoSlug))
|
|
679
723
|
return null;
|
|
724
|
+
const repo = readRepoConnection(projectRoot);
|
|
680
725
|
const auth = readRemoteAuthState(projectRoot);
|
|
681
726
|
const authUpdatedAt = typeof auth?.updatedAt === "string" ? Date.parse(auth.updatedAt) : Number.NaN;
|
|
682
727
|
const repoLinkedAt = typeof repo?.linkedAt === "string" ? Date.parse(repo.linkedAt) : Number.NaN;
|
|
@@ -685,25 +730,413 @@ function inferRemoteServerProjectRootFromAuthState(projectRoot) {
|
|
|
685
730
|
const checkoutBaseDir = typeof auth?.checkoutBaseDir === "string" && auth.checkoutBaseDir.trim() ? auth.checkoutBaseDir.trim() : null;
|
|
686
731
|
if (!checkoutBaseDir)
|
|
687
732
|
return null;
|
|
688
|
-
|
|
689
|
-
writeRepoServerProjectRoot(projectRoot, inferred);
|
|
690
|
-
return inferred;
|
|
733
|
+
return `${checkoutBaseDir.replace(/\/+$/, "")}/${repoSlug}`;
|
|
691
734
|
}
|
|
692
735
|
function readLocalConnectionFallbackToken(projectRoot) {
|
|
693
736
|
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
694
737
|
}
|
|
738
|
+
function normalizeRepoSlug(value) {
|
|
739
|
+
const slug = value?.trim();
|
|
740
|
+
return slug && /^[^/\s]+\/[^/\s]+$/.test(slug) ? slug : null;
|
|
741
|
+
}
|
|
742
|
+
function readProjectLinkSlug(projectRoot) {
|
|
743
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
744
|
+
if (!existsSync2(path))
|
|
745
|
+
return null;
|
|
746
|
+
try {
|
|
747
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
748
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
749
|
+
return null;
|
|
750
|
+
return normalizeRepoSlug(typeof parsed.repoSlug === "string" ? String(parsed.repoSlug) : null);
|
|
751
|
+
} catch {
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
function writeProjectLinkConnection(projectRoot, repoSlug, alias) {
|
|
756
|
+
const path = resolve2(projectRoot, ".rig", "state", "project-link.json");
|
|
757
|
+
mkdirSync2(dirname2(path), { recursive: true });
|
|
758
|
+
writeFileSync2(path, `${JSON.stringify({ repoSlug, connection: alias, linkedAt: new Date().toISOString() }, null, 2)}
|
|
759
|
+
`, "utf8");
|
|
760
|
+
}
|
|
761
|
+
function remoteLinkRepairCommand(repoSlug) {
|
|
762
|
+
return repoSlug ? `rig server repair-link --repo ${repoSlug}` : "rig server repair-link --repo owner/repo";
|
|
763
|
+
}
|
|
764
|
+
function formatRemoteProjectLinkHint(resolution) {
|
|
765
|
+
const repair = remoteLinkRepairCommand(resolution.repoSlug);
|
|
766
|
+
if (resolution.status === "auth_required") {
|
|
767
|
+
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}` : ""}\`.`;
|
|
768
|
+
}
|
|
769
|
+
if (resolution.status === "missing_project") {
|
|
770
|
+
return `Record the repo slug, then run \`${repair}\` (or \`rig init --repo owner/repo --server remote --github-auth device\`).`;
|
|
771
|
+
}
|
|
772
|
+
if (resolution.status === "not_remote")
|
|
773
|
+
return "Select a remote server with `rig server use <alias>` before repairing a remote project link.";
|
|
774
|
+
return `Run \`${repair}\` to backfill or prepare a server-host checkout for the selected remote${resolution.alias ? ` (${resolution.alias})` : ""}.`;
|
|
775
|
+
}
|
|
776
|
+
function remoteProjectLinkFailure(input) {
|
|
777
|
+
const partial = {
|
|
778
|
+
status: input.status,
|
|
779
|
+
alias: input.alias,
|
|
780
|
+
baseUrl: input.baseUrl,
|
|
781
|
+
repoSlug: input.repoSlug
|
|
782
|
+
};
|
|
783
|
+
return {
|
|
784
|
+
ok: false,
|
|
785
|
+
...input,
|
|
786
|
+
hint: formatRemoteProjectLinkHint(partial),
|
|
787
|
+
next: remoteLinkRepairCommand(input.repoSlug)
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
function remoteProjectLinkSuccess(input) {
|
|
791
|
+
return {
|
|
792
|
+
ok: true,
|
|
793
|
+
status: input.status,
|
|
794
|
+
alias: input.alias,
|
|
795
|
+
baseUrl: input.baseUrl,
|
|
796
|
+
repoSlug: input.repoSlug,
|
|
797
|
+
serverProjectRoot: input.serverProjectRoot,
|
|
798
|
+
source: input.source,
|
|
799
|
+
prepared: input.prepared ?? input.status === "prepared",
|
|
800
|
+
validated: input.validated ?? false,
|
|
801
|
+
message: input.message ?? `Remote project link ready for ${input.repoSlug}.`,
|
|
802
|
+
hint: "Remote-scoped task/run/status requests will send x-rig-project-root for this server-host checkout.",
|
|
803
|
+
next: "rig task list"
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
function localCheckoutPathCandidate(projectRoot, candidate) {
|
|
807
|
+
try {
|
|
808
|
+
const local = resolve2(projectRoot);
|
|
809
|
+
const resolved = resolve2(candidate);
|
|
810
|
+
return resolved === local || resolved.startsWith(`${local}/`);
|
|
811
|
+
} catch {
|
|
812
|
+
return false;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
async function requestRemoteProjectLinkJson(baseUrl, pathname, authToken, init = {}) {
|
|
816
|
+
const requestUrl = new URL(`${baseUrl}${pathname}`);
|
|
817
|
+
if (authToken && queryAuthFallbackEnabled())
|
|
818
|
+
requestUrl.searchParams.set("rt", authToken);
|
|
819
|
+
const response = await fetch(requestUrl, {
|
|
820
|
+
...init,
|
|
821
|
+
headers: mergeHeaders(init.headers, authToken)
|
|
822
|
+
});
|
|
823
|
+
const text = await response.text();
|
|
824
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
825
|
+
try {
|
|
826
|
+
return JSON.parse(text);
|
|
827
|
+
} catch {
|
|
828
|
+
return null;
|
|
829
|
+
}
|
|
830
|
+
})() : null;
|
|
831
|
+
return { ok: response.ok, status: response.status, payload, text };
|
|
832
|
+
}
|
|
833
|
+
function payloadError(payload, fallback) {
|
|
834
|
+
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
|
835
|
+
const record = payload;
|
|
836
|
+
const value = record.error ?? record.message ?? record.reason;
|
|
837
|
+
if (typeof value === "string" && value.trim())
|
|
838
|
+
return value.trim();
|
|
839
|
+
}
|
|
840
|
+
return fallback;
|
|
841
|
+
}
|
|
842
|
+
function checkoutPathsFromProjectPayload(payload) {
|
|
843
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
844
|
+
return [];
|
|
845
|
+
const project = payload.project;
|
|
846
|
+
if (!project || typeof project !== "object" || Array.isArray(project))
|
|
847
|
+
return [];
|
|
848
|
+
const checkouts = project.checkouts;
|
|
849
|
+
if (!Array.isArray(checkouts))
|
|
850
|
+
return [];
|
|
851
|
+
return [...checkouts].reverse().flatMap((entry) => {
|
|
852
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
853
|
+
return [];
|
|
854
|
+
const path = entry.path;
|
|
855
|
+
return typeof path === "string" && path.trim() ? [path.trim()] : [];
|
|
856
|
+
});
|
|
857
|
+
}
|
|
858
|
+
function checkoutPathFromPreparePayload(payload) {
|
|
859
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
860
|
+
return null;
|
|
861
|
+
const checkout = payload.checkout;
|
|
862
|
+
if (!checkout || typeof checkout !== "object" || Array.isArray(checkout))
|
|
863
|
+
return null;
|
|
864
|
+
const path = checkout.path;
|
|
865
|
+
return typeof path === "string" && path.trim() ? path.trim() : null;
|
|
866
|
+
}
|
|
867
|
+
async function validateAndPersistRemoteRoot(input) {
|
|
868
|
+
const candidate = input.candidate.trim();
|
|
869
|
+
if (!candidate || !isAbsolute(candidate)) {
|
|
870
|
+
return remoteProjectLinkFailure({
|
|
871
|
+
status: "invalid_root",
|
|
872
|
+
alias: input.alias,
|
|
873
|
+
baseUrl: input.baseUrl,
|
|
874
|
+
repoSlug: input.repoSlug,
|
|
875
|
+
message: `Remote project root candidate is not an absolute server-host path: ${candidate || "(empty)"}.`
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
if (localCheckoutPathCandidate(input.projectRoot, candidate)) {
|
|
879
|
+
return remoteProjectLinkFailure({
|
|
880
|
+
status: "invalid_root",
|
|
881
|
+
alias: input.alias,
|
|
882
|
+
baseUrl: input.baseUrl,
|
|
883
|
+
repoSlug: input.repoSlug,
|
|
884
|
+
message: `Refusing to use local checkout path ${candidate} as a remote server project root.`
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
let response;
|
|
888
|
+
try {
|
|
889
|
+
response = await requestRemoteProjectLinkJson(input.baseUrl, "/api/server/project-root", input.authToken, {
|
|
890
|
+
method: "POST",
|
|
891
|
+
headers: { "content-type": "application/json" },
|
|
892
|
+
body: JSON.stringify({ projectRoot: candidate })
|
|
893
|
+
});
|
|
894
|
+
} catch (error) {
|
|
895
|
+
return remoteProjectLinkFailure({
|
|
896
|
+
status: "error",
|
|
897
|
+
alias: input.alias,
|
|
898
|
+
baseUrl: input.baseUrl,
|
|
899
|
+
repoSlug: input.repoSlug,
|
|
900
|
+
message: `Could not validate remote project root ${candidate}: ${error instanceof Error ? error.message : String(error)}`
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
if (response.status === 401 || response.status === 403) {
|
|
904
|
+
return remoteProjectLinkFailure({
|
|
905
|
+
status: "auth_required",
|
|
906
|
+
alias: input.alias,
|
|
907
|
+
baseUrl: input.baseUrl,
|
|
908
|
+
repoSlug: input.repoSlug,
|
|
909
|
+
statusCode: response.status,
|
|
910
|
+
message: `Remote server ${input.alias} requires authentication before validating project root ${candidate}.`
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
if (!response.ok) {
|
|
914
|
+
return remoteProjectLinkFailure({
|
|
915
|
+
status: "invalid_root",
|
|
916
|
+
alias: input.alias,
|
|
917
|
+
baseUrl: input.baseUrl,
|
|
918
|
+
repoSlug: input.repoSlug,
|
|
919
|
+
statusCode: response.status,
|
|
920
|
+
message: `Remote server rejected project root ${candidate} (${response.status}): ${payloadError(response.payload, response.text || "project-root validation failed")}`
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
const record = response.payload && typeof response.payload === "object" && !Array.isArray(response.payload) ? response.payload : {};
|
|
924
|
+
if (record.ok !== true) {
|
|
925
|
+
return remoteProjectLinkFailure({
|
|
926
|
+
status: "invalid_root",
|
|
927
|
+
alias: input.alias,
|
|
928
|
+
baseUrl: input.baseUrl,
|
|
929
|
+
repoSlug: input.repoSlug,
|
|
930
|
+
message: `Remote server did not accept project root ${candidate}: ${payloadError(response.payload, "project-root validation failed")}`
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
const accepted = typeof record.projectRoot === "string" && record.projectRoot.trim() ? record.projectRoot.trim() : typeof record.requestedProjectRoot === "string" && record.requestedProjectRoot.trim() ? record.requestedProjectRoot.trim() : candidate;
|
|
934
|
+
writeRepoServerProjectRoot(input.projectRoot, accepted, { alias: input.alias, baseUrl: input.baseUrl, project: input.repoSlug });
|
|
935
|
+
writeProjectLinkConnection(input.projectRoot, input.repoSlug, input.alias);
|
|
936
|
+
const status = input.status ?? (input.prepared ? "prepared" : "backfilled");
|
|
937
|
+
return remoteProjectLinkSuccess({
|
|
938
|
+
status,
|
|
939
|
+
alias: input.alias,
|
|
940
|
+
baseUrl: input.baseUrl,
|
|
941
|
+
repoSlug: input.repoSlug,
|
|
942
|
+
serverProjectRoot: accepted,
|
|
943
|
+
source: input.source,
|
|
944
|
+
prepared: input.prepared,
|
|
945
|
+
validated: true,
|
|
946
|
+
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}.`
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
async function ensureRemoteProjectRootLink(projectRoot, options = {}) {
|
|
950
|
+
let selected;
|
|
951
|
+
try {
|
|
952
|
+
selected = resolveSelectedConnection(projectRoot);
|
|
953
|
+
} catch (error) {
|
|
954
|
+
return remoteProjectLinkFailure({ status: "error", message: error instanceof Error ? error.message : String(error) });
|
|
955
|
+
}
|
|
956
|
+
if (!selected || selected.connection.kind !== "remote") {
|
|
957
|
+
return remoteProjectLinkFailure({ status: "not_remote", message: "No remote Rig server is selected for this repo." });
|
|
958
|
+
}
|
|
959
|
+
const repo = readRepoConnection(projectRoot);
|
|
960
|
+
const explicitRepoRequested = options.repoSlug !== undefined;
|
|
961
|
+
const explicitRepoSlug = explicitRepoRequested ? normalizeRepoSlug(options.repoSlug) : null;
|
|
962
|
+
const repoProjectSlug = normalizeRepoSlug(repo?.project);
|
|
963
|
+
const repoSlug = explicitRepoSlug ?? repoProjectSlug ?? readProjectLinkSlug(projectRoot);
|
|
964
|
+
const alias = selected.alias;
|
|
965
|
+
const baseUrl = selected.connection.baseUrl;
|
|
966
|
+
const authToken = options.authToken === undefined ? readGitHubBearerTokenForRemote(projectRoot) : options.authToken;
|
|
967
|
+
const mode = options.mode ?? "backfill-only";
|
|
968
|
+
if (explicitRepoRequested && !explicitRepoSlug) {
|
|
969
|
+
return remoteProjectLinkFailure({
|
|
970
|
+
status: "missing_project",
|
|
971
|
+
alias,
|
|
972
|
+
baseUrl,
|
|
973
|
+
message: `Invalid --repo value ${JSON.stringify(options.repoSlug)}. Expected owner/repo.`
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
if (!repoSlug) {
|
|
977
|
+
return remoteProjectLinkFailure({
|
|
978
|
+
status: "missing_project",
|
|
979
|
+
alias,
|
|
980
|
+
baseUrl,
|
|
981
|
+
message: `Remote server ${alias} is selected, but this checkout has no GitHub repo slug recorded.`
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
const skippedCandidates = [];
|
|
985
|
+
const storedRoot = selected.serverProjectRoot?.trim() || null;
|
|
986
|
+
const storedRootMatchesResolvedRepo = repoProjectSlug === repoSlug;
|
|
987
|
+
if (storedRoot && storedRootMatchesResolvedRepo) {
|
|
988
|
+
if (localCheckoutPathCandidate(projectRoot, storedRoot)) {
|
|
989
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
990
|
+
skippedCandidates.push(storedRoot);
|
|
991
|
+
} else {
|
|
992
|
+
const storedResult = await validateAndPersistRemoteRoot({
|
|
993
|
+
projectRoot,
|
|
994
|
+
alias,
|
|
995
|
+
baseUrl,
|
|
996
|
+
authToken,
|
|
997
|
+
repoSlug,
|
|
998
|
+
candidate: storedRoot,
|
|
999
|
+
source: "stored",
|
|
1000
|
+
status: "ready"
|
|
1001
|
+
});
|
|
1002
|
+
if (storedResult.ok || storedResult.status === "auth_required" || storedResult.status === "error")
|
|
1003
|
+
return storedResult;
|
|
1004
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
1005
|
+
skippedCandidates.push(storedRoot);
|
|
1006
|
+
}
|
|
1007
|
+
} else if (storedRoot) {
|
|
1008
|
+
skippedCandidates.push(storedRoot);
|
|
1009
|
+
}
|
|
1010
|
+
const authCandidate = inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug);
|
|
1011
|
+
if (authCandidate) {
|
|
1012
|
+
const authResult = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: authCandidate, source: "auth-state" });
|
|
1013
|
+
if (authResult.ok || authResult.status === "auth_required")
|
|
1014
|
+
return authResult;
|
|
1015
|
+
}
|
|
1016
|
+
let registryResponse;
|
|
1017
|
+
try {
|
|
1018
|
+
registryResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}`, authToken);
|
|
1019
|
+
} catch (error) {
|
|
1020
|
+
return remoteProjectLinkFailure({
|
|
1021
|
+
status: "error",
|
|
1022
|
+
alias,
|
|
1023
|
+
baseUrl,
|
|
1024
|
+
repoSlug,
|
|
1025
|
+
message: `Could not read remote project registry for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
if (registryResponse.status === 401 || registryResponse.status === 403) {
|
|
1029
|
+
return remoteProjectLinkFailure({
|
|
1030
|
+
status: "auth_required",
|
|
1031
|
+
alias,
|
|
1032
|
+
baseUrl,
|
|
1033
|
+
repoSlug,
|
|
1034
|
+
statusCode: registryResponse.status,
|
|
1035
|
+
message: `Remote server ${alias} requires authentication before reading the ${repoSlug} project registry.`
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
if (registryResponse.ok) {
|
|
1039
|
+
const candidates = checkoutPathsFromProjectPayload(registryResponse.payload);
|
|
1040
|
+
for (const candidate of candidates) {
|
|
1041
|
+
const result = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate, source: "registry" });
|
|
1042
|
+
if (result.ok || result.status === "auth_required")
|
|
1043
|
+
return result;
|
|
1044
|
+
skippedCandidates.push(candidate);
|
|
1045
|
+
}
|
|
1046
|
+
if (mode === "backfill-only") {
|
|
1047
|
+
return remoteProjectLinkFailure({
|
|
1048
|
+
status: candidates.length > 0 ? "invalid_root" : "no_server_checkout",
|
|
1049
|
+
alias,
|
|
1050
|
+
baseUrl,
|
|
1051
|
+
repoSlug,
|
|
1052
|
+
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.`,
|
|
1053
|
+
skippedCandidates
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
} else if (registryResponse.status === 404) {
|
|
1057
|
+
if (mode === "backfill-only") {
|
|
1058
|
+
return remoteProjectLinkFailure({
|
|
1059
|
+
status: "project_not_registered",
|
|
1060
|
+
alias,
|
|
1061
|
+
baseUrl,
|
|
1062
|
+
repoSlug,
|
|
1063
|
+
statusCode: registryResponse.status,
|
|
1064
|
+
message: `Remote server ${alias} has no registered project record for ${repoSlug}.`
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
} else {
|
|
1068
|
+
return remoteProjectLinkFailure({
|
|
1069
|
+
status: "error",
|
|
1070
|
+
alias,
|
|
1071
|
+
baseUrl,
|
|
1072
|
+
repoSlug,
|
|
1073
|
+
statusCode: registryResponse.status,
|
|
1074
|
+
message: `Could not backfill ${repoSlug} from remote registry (${registryResponse.status}): ${payloadError(registryResponse.payload, registryResponse.text || "registry lookup failed")}`,
|
|
1075
|
+
skippedCandidates
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
let prepareResponse;
|
|
1079
|
+
try {
|
|
1080
|
+
prepareResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}/prepare-checkout`, authToken, {
|
|
1081
|
+
method: "POST",
|
|
1082
|
+
headers: { "content-type": "application/json" },
|
|
1083
|
+
body: JSON.stringify({ checkout: { kind: "managed-clone" }, repoUrl: `https://github.com/${repoSlug}.git` })
|
|
1084
|
+
});
|
|
1085
|
+
} catch (error) {
|
|
1086
|
+
return remoteProjectLinkFailure({
|
|
1087
|
+
status: "error",
|
|
1088
|
+
alias,
|
|
1089
|
+
baseUrl,
|
|
1090
|
+
repoSlug,
|
|
1091
|
+
message: `Could not prepare remote checkout for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
if (prepareResponse.status === 401 || prepareResponse.status === 403) {
|
|
1095
|
+
return remoteProjectLinkFailure({
|
|
1096
|
+
status: "auth_required",
|
|
1097
|
+
alias,
|
|
1098
|
+
baseUrl,
|
|
1099
|
+
repoSlug,
|
|
1100
|
+
statusCode: prepareResponse.status,
|
|
1101
|
+
message: `Remote server ${alias} requires authentication before preparing ${repoSlug}.`
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
if (!prepareResponse.ok) {
|
|
1105
|
+
return remoteProjectLinkFailure({
|
|
1106
|
+
status: "error",
|
|
1107
|
+
alias,
|
|
1108
|
+
baseUrl,
|
|
1109
|
+
repoSlug,
|
|
1110
|
+
statusCode: prepareResponse.status,
|
|
1111
|
+
message: `Remote checkout prepare failed for ${repoSlug} (${prepareResponse.status}): ${payloadError(prepareResponse.payload, prepareResponse.text || "prepare-checkout failed")}`,
|
|
1112
|
+
skippedCandidates
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
const preparedPath = checkoutPathFromPreparePayload(prepareResponse.payload);
|
|
1116
|
+
if (!preparedPath) {
|
|
1117
|
+
return remoteProjectLinkFailure({
|
|
1118
|
+
status: "invalid_root",
|
|
1119
|
+
alias,
|
|
1120
|
+
baseUrl,
|
|
1121
|
+
repoSlug,
|
|
1122
|
+
message: `Remote checkout prepare for ${repoSlug} did not return a checkout.path.`,
|
|
1123
|
+
skippedCandidates
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
return validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: preparedPath, source: "prepare", prepared: true });
|
|
1127
|
+
}
|
|
695
1128
|
async function ensureServerForCli(projectRoot) {
|
|
696
1129
|
try {
|
|
697
1130
|
const selected = resolveSelectedConnection(projectRoot);
|
|
698
1131
|
if (selected?.connection.kind === "remote") {
|
|
699
1132
|
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
700
1133
|
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
701
|
-
const
|
|
1134
|
+
const link = await ensureRemoteProjectRootLink(projectRoot, { mode: "backfill-only", authToken });
|
|
702
1135
|
return {
|
|
703
1136
|
baseUrl: selected.connection.baseUrl,
|
|
704
1137
|
authToken,
|
|
705
1138
|
connectionKind: "remote",
|
|
706
|
-
serverProjectRoot
|
|
1139
|
+
serverProjectRoot: link.ok ? link.serverProjectRoot ?? null : null
|
|
707
1140
|
};
|
|
708
1141
|
}
|
|
709
1142
|
reportServerPhase("Starting local Rig server\u2026");
|
|
@@ -721,32 +1154,6 @@ async function ensureServerForCli(projectRoot) {
|
|
|
721
1154
|
throw error;
|
|
722
1155
|
}
|
|
723
1156
|
}
|
|
724
|
-
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
725
|
-
const repo = readRepoConnection(projectRoot);
|
|
726
|
-
const slug = repo?.project?.trim();
|
|
727
|
-
if (!slug)
|
|
728
|
-
return null;
|
|
729
|
-
try {
|
|
730
|
-
const url = new URL(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`);
|
|
731
|
-
if (authToken && queryAuthFallbackEnabled())
|
|
732
|
-
url.searchParams.set("rt", authToken);
|
|
733
|
-
const response = await fetch(url, {
|
|
734
|
-
headers: mergeHeaders(undefined, authToken)
|
|
735
|
-
});
|
|
736
|
-
if (!response.ok)
|
|
737
|
-
return null;
|
|
738
|
-
const payload = await response.json();
|
|
739
|
-
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
740
|
-
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
741
|
-
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
742
|
-
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
743
|
-
if (path)
|
|
744
|
-
writeRepoServerProjectRoot(projectRoot, path);
|
|
745
|
-
return path;
|
|
746
|
-
} catch {
|
|
747
|
-
return null;
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
1157
|
function appendTaskFilterParams(url, filters) {
|
|
751
1158
|
if (filters.assignee)
|
|
752
1159
|
url.searchParams.set("assignee", filters.assignee);
|
|
@@ -848,13 +1255,20 @@ function canUseRemoteWithoutProjectRoot(pathname) {
|
|
|
848
1255
|
async function requestServerJson(context, pathname, init = {}) {
|
|
849
1256
|
const server = await ensureServerForCli(context.projectRoot);
|
|
850
1257
|
const requestUrl = new URL(`${server.baseUrl}${pathname}`);
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1258
|
+
let scopedServerProjectRoot = server.serverProjectRoot;
|
|
1259
|
+
if (server.connectionKind === "remote" && !scopedServerProjectRoot && !canUseRemoteWithoutProjectRoot(requestUrl.pathname) && (server.authToken || !isLoopbackRemoteBaseUrl(server.baseUrl))) {
|
|
1260
|
+
const link = await ensureRemoteProjectRootLink(context.projectRoot, { mode: "backfill-only", authToken: server.authToken });
|
|
1261
|
+
if (link.ok && link.serverProjectRoot) {
|
|
1262
|
+
scopedServerProjectRoot = link.serverProjectRoot;
|
|
1263
|
+
} else {
|
|
1264
|
+
const repo = readRepoConnection(context.projectRoot);
|
|
1265
|
+
const target = link.alias && link.baseUrl ? `${link.alias} (${link.baseUrl})` : server.baseUrl;
|
|
1266
|
+
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 });
|
|
1267
|
+
}
|
|
854
1268
|
}
|
|
855
1269
|
const headers = mergeHeaders(init.headers, server.authToken);
|
|
856
|
-
if (
|
|
857
|
-
headers.set("x-rig-project-root",
|
|
1270
|
+
if (scopedServerProjectRoot)
|
|
1271
|
+
headers.set("x-rig-project-root", scopedServerProjectRoot);
|
|
858
1272
|
if (server.connectionKind === "remote" && server.authToken && queryAuthFallbackEnabled()) {
|
|
859
1273
|
requestUrl.searchParams.set("rt", server.authToken);
|
|
860
1274
|
}
|