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