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