@h-rig/cli 0.0.6-alpha.87 → 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 +1380 -865
- package/dist/src/app/board.js +462 -48
- package/dist/src/app-opentui/adapters/common.d.ts +3 -0
- package/dist/src/app-opentui/adapters/common.js +4 -0
- package/dist/src/app-opentui/adapters/doctor.js +458 -46
- package/dist/src/app-opentui/adapters/family.js +701 -151
- 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.d.ts +7 -0
- package/dist/src/app-opentui/adapters/pi-attach.js +1004 -519
- 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 +621 -549
- package/dist/src/app-opentui/autocomplete.js +4 -2
- package/dist/src/app-opentui/bootstrap.js +1376 -861
- package/dist/src/app-opentui/command-palette.js +37 -10
- package/dist/src/app-opentui/index.js +632 -528
- package/dist/src/app-opentui/intent.js +33 -8
- package/dist/src/app-opentui/keymap.js +43 -414
- package/dist/src/app-opentui/pi-host-child.js +496 -57
- package/dist/src/app-opentui/pi-pty-host.d.ts +14 -64
- package/dist/src/app-opentui/pi-pty-host.js +3 -397
- package/dist/src/app-opentui/react/App.js +144 -469
- package/dist/src/app-opentui/react/ChromeHost.js +44 -415
- package/dist/src/app-opentui/react/launch.js +659 -552
- package/dist/src/app-opentui/react/nav.js +1 -1
- package/dist/src/app-opentui/registry.js +1181 -742
- 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/render/terminal-handoff.d.ts +16 -0
- package/dist/src/app-opentui/render/terminal-handoff.js +14 -0
- package/dist/src/app-opentui/runtime.js +632 -528
- 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 +496 -57
- package/dist/src/commands/_pi-frontend.d.ts +25 -0
- package/dist/src/commands/_pi-frontend.js +497 -57
- 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 +496 -57
- 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 +551 -85
- package/dist/src/commands.js +701 -151
- package/dist/src/index.js +705 -151
- package/dist/src/launcher.js +4 -0
- package/package.json +8 -8
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var __require = import.meta.require;
|
|
3
3
|
|
|
4
4
|
// packages/cli/src/commands/setup.ts
|
|
5
|
-
import { existsSync as existsSync5, mkdirSync as
|
|
5
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync3, readdirSync, writeFileSync as writeFileSync3 } from "fs";
|
|
6
6
|
import { resolve as resolve6 } from "path";
|
|
7
7
|
|
|
8
8
|
// packages/cli/src/runner.ts
|
|
@@ -254,12 +254,28 @@ function readRepoConnection(projectRoot) {
|
|
|
254
254
|
selected,
|
|
255
255
|
project: typeof record.project === "string" ? record.project : undefined,
|
|
256
256
|
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
257
|
-
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
257
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined,
|
|
258
|
+
serverProjectRootAlias: typeof record.serverProjectRootAlias === "string" && record.serverProjectRootAlias.trim() ? record.serverProjectRootAlias.trim() : undefined,
|
|
259
|
+
serverProjectRootBaseUrl: typeof record.serverProjectRootBaseUrl === "string" && record.serverProjectRootBaseUrl.trim() ? record.serverProjectRootBaseUrl.trim().replace(/\/+$/, "") : undefined
|
|
258
260
|
};
|
|
259
261
|
}
|
|
260
262
|
function writeRepoConnection(projectRoot, state) {
|
|
261
263
|
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
262
264
|
}
|
|
265
|
+
function rootAllowedForSelection(repo, connection) {
|
|
266
|
+
const root = repo.serverProjectRoot?.trim();
|
|
267
|
+
if (!root)
|
|
268
|
+
return;
|
|
269
|
+
if (connection.kind === "remote") {
|
|
270
|
+
if (repo.serverProjectRootAlias !== repo.selected)
|
|
271
|
+
return;
|
|
272
|
+
if (repo.serverProjectRootBaseUrl !== connection.baseUrl)
|
|
273
|
+
return;
|
|
274
|
+
} else if (repo.serverProjectRootAlias && repo.serverProjectRootAlias !== repo.selected) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
return root;
|
|
278
|
+
}
|
|
263
279
|
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
264
280
|
const repo = readRepoConnection(projectRoot);
|
|
265
281
|
if (!repo)
|
|
@@ -271,18 +287,46 @@ function resolveSelectedConnection(projectRoot, options = {}) {
|
|
|
271
287
|
if (!connection) {
|
|
272
288
|
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
273
289
|
}
|
|
274
|
-
return { alias: repo.selected, connection, serverProjectRoot: repo
|
|
290
|
+
return { alias: repo.selected, connection, serverProjectRoot: rootAllowedForSelection(repo, connection) };
|
|
275
291
|
}
|
|
276
|
-
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
292
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot, metadata = {}) {
|
|
277
293
|
const repo = readRepoConnection(projectRoot);
|
|
278
294
|
if (!repo)
|
|
279
295
|
return;
|
|
280
|
-
|
|
296
|
+
let inferred = metadata;
|
|
297
|
+
if (!inferred.alias || !inferred.baseUrl) {
|
|
298
|
+
try {
|
|
299
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
300
|
+
if (selected?.connection.kind === "remote") {
|
|
301
|
+
inferred = {
|
|
302
|
+
alias: inferred.alias ?? selected.alias,
|
|
303
|
+
baseUrl: inferred.baseUrl ?? selected.connection.baseUrl
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
} catch {}
|
|
307
|
+
}
|
|
308
|
+
writeRepoConnection(projectRoot, {
|
|
309
|
+
...repo,
|
|
310
|
+
...metadata.project ? { project: metadata.project } : {},
|
|
311
|
+
serverProjectRoot,
|
|
312
|
+
...inferred.alias ? { serverProjectRootAlias: inferred.alias } : {},
|
|
313
|
+
...inferred.baseUrl ? { serverProjectRootBaseUrl: inferred.baseUrl.replace(/\/+$/, "") } : {}
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
function clearRepoServerProjectRoot(projectRoot) {
|
|
317
|
+
const repo = readRepoConnection(projectRoot);
|
|
318
|
+
if (!repo)
|
|
319
|
+
return;
|
|
320
|
+
writeRepoConnection(projectRoot, {
|
|
321
|
+
selected: repo.selected,
|
|
322
|
+
...repo.project ? { project: repo.project } : {},
|
|
323
|
+
...repo.linkedAt ? { linkedAt: repo.linkedAt } : {}
|
|
324
|
+
});
|
|
281
325
|
}
|
|
282
326
|
|
|
283
327
|
// packages/cli/src/commands/_server-client.ts
|
|
284
|
-
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
285
|
-
import { resolve as resolve4 } from "path";
|
|
328
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
329
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve4 } from "path";
|
|
286
330
|
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
287
331
|
var scopedGitHubBearerTokens = new Map;
|
|
288
332
|
var serverPhaseListener = null;
|
|
@@ -326,11 +370,10 @@ function readStoredGitHubAuthToken(projectRoot) {
|
|
|
326
370
|
const parsed = readRemoteAuthState(projectRoot);
|
|
327
371
|
return parsed ? cleanToken(typeof parsed.token === "string" ? parsed.token : undefined) : null;
|
|
328
372
|
}
|
|
329
|
-
function
|
|
330
|
-
|
|
331
|
-
const slug = repo?.project?.trim();
|
|
332
|
-
if (!slug || !/^[^/]+\/[^/]+$/.test(slug))
|
|
373
|
+
function inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug) {
|
|
374
|
+
if (!/^[^/]+\/[^/]+$/.test(repoSlug))
|
|
333
375
|
return null;
|
|
376
|
+
const repo = readRepoConnection(projectRoot);
|
|
334
377
|
const auth = readRemoteAuthState(projectRoot);
|
|
335
378
|
const authUpdatedAt = typeof auth?.updatedAt === "string" ? Date.parse(auth.updatedAt) : Number.NaN;
|
|
336
379
|
const repoLinkedAt = typeof repo?.linkedAt === "string" ? Date.parse(repo.linkedAt) : Number.NaN;
|
|
@@ -339,25 +382,413 @@ function inferRemoteServerProjectRootFromAuthState(projectRoot) {
|
|
|
339
382
|
const checkoutBaseDir = typeof auth?.checkoutBaseDir === "string" && auth.checkoutBaseDir.trim() ? auth.checkoutBaseDir.trim() : null;
|
|
340
383
|
if (!checkoutBaseDir)
|
|
341
384
|
return null;
|
|
342
|
-
|
|
343
|
-
writeRepoServerProjectRoot(projectRoot, inferred);
|
|
344
|
-
return inferred;
|
|
385
|
+
return `${checkoutBaseDir.replace(/\/+$/, "")}/${repoSlug}`;
|
|
345
386
|
}
|
|
346
387
|
function readLocalConnectionFallbackToken(projectRoot) {
|
|
347
388
|
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
348
389
|
}
|
|
390
|
+
function normalizeRepoSlug(value) {
|
|
391
|
+
const slug = value?.trim();
|
|
392
|
+
return slug && /^[^/\s]+\/[^/\s]+$/.test(slug) ? slug : null;
|
|
393
|
+
}
|
|
394
|
+
function readProjectLinkSlug(projectRoot) {
|
|
395
|
+
const path = resolve4(projectRoot, ".rig", "state", "project-link.json");
|
|
396
|
+
if (!existsSync3(path))
|
|
397
|
+
return null;
|
|
398
|
+
try {
|
|
399
|
+
const parsed = JSON.parse(readFileSync3(path, "utf8"));
|
|
400
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
401
|
+
return null;
|
|
402
|
+
return normalizeRepoSlug(typeof parsed.repoSlug === "string" ? String(parsed.repoSlug) : null);
|
|
403
|
+
} catch {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function writeProjectLinkConnection(projectRoot, repoSlug, alias) {
|
|
408
|
+
const path = resolve4(projectRoot, ".rig", "state", "project-link.json");
|
|
409
|
+
mkdirSync2(dirname2(path), { recursive: true });
|
|
410
|
+
writeFileSync2(path, `${JSON.stringify({ repoSlug, connection: alias, linkedAt: new Date().toISOString() }, null, 2)}
|
|
411
|
+
`, "utf8");
|
|
412
|
+
}
|
|
413
|
+
function remoteLinkRepairCommand(repoSlug) {
|
|
414
|
+
return repoSlug ? `rig server repair-link --repo ${repoSlug}` : "rig server repair-link --repo owner/repo";
|
|
415
|
+
}
|
|
416
|
+
function formatRemoteProjectLinkHint(resolution) {
|
|
417
|
+
const repair = remoteLinkRepairCommand(resolution.repoSlug);
|
|
418
|
+
if (resolution.status === "auth_required") {
|
|
419
|
+
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}` : ""}\`.`;
|
|
420
|
+
}
|
|
421
|
+
if (resolution.status === "missing_project") {
|
|
422
|
+
return `Record the repo slug, then run \`${repair}\` (or \`rig init --repo owner/repo --server remote --github-auth device\`).`;
|
|
423
|
+
}
|
|
424
|
+
if (resolution.status === "not_remote")
|
|
425
|
+
return "Select a remote server with `rig server use <alias>` before repairing a remote project link.";
|
|
426
|
+
return `Run \`${repair}\` to backfill or prepare a server-host checkout for the selected remote${resolution.alias ? ` (${resolution.alias})` : ""}.`;
|
|
427
|
+
}
|
|
428
|
+
function remoteProjectLinkFailure(input) {
|
|
429
|
+
const partial = {
|
|
430
|
+
status: input.status,
|
|
431
|
+
alias: input.alias,
|
|
432
|
+
baseUrl: input.baseUrl,
|
|
433
|
+
repoSlug: input.repoSlug
|
|
434
|
+
};
|
|
435
|
+
return {
|
|
436
|
+
ok: false,
|
|
437
|
+
...input,
|
|
438
|
+
hint: formatRemoteProjectLinkHint(partial),
|
|
439
|
+
next: remoteLinkRepairCommand(input.repoSlug)
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function remoteProjectLinkSuccess(input) {
|
|
443
|
+
return {
|
|
444
|
+
ok: true,
|
|
445
|
+
status: input.status,
|
|
446
|
+
alias: input.alias,
|
|
447
|
+
baseUrl: input.baseUrl,
|
|
448
|
+
repoSlug: input.repoSlug,
|
|
449
|
+
serverProjectRoot: input.serverProjectRoot,
|
|
450
|
+
source: input.source,
|
|
451
|
+
prepared: input.prepared ?? input.status === "prepared",
|
|
452
|
+
validated: input.validated ?? false,
|
|
453
|
+
message: input.message ?? `Remote project link ready for ${input.repoSlug}.`,
|
|
454
|
+
hint: "Remote-scoped task/run/status requests will send x-rig-project-root for this server-host checkout.",
|
|
455
|
+
next: "rig task list"
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function localCheckoutPathCandidate(projectRoot, candidate) {
|
|
459
|
+
try {
|
|
460
|
+
const local = resolve4(projectRoot);
|
|
461
|
+
const resolved = resolve4(candidate);
|
|
462
|
+
return resolved === local || resolved.startsWith(`${local}/`);
|
|
463
|
+
} catch {
|
|
464
|
+
return false;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
async function requestRemoteProjectLinkJson(baseUrl, pathname, authToken, init = {}) {
|
|
468
|
+
const requestUrl = new URL(`${baseUrl}${pathname}`);
|
|
469
|
+
if (authToken && queryAuthFallbackEnabled())
|
|
470
|
+
requestUrl.searchParams.set("rt", authToken);
|
|
471
|
+
const response = await fetch(requestUrl, {
|
|
472
|
+
...init,
|
|
473
|
+
headers: mergeHeaders(init.headers, authToken)
|
|
474
|
+
});
|
|
475
|
+
const text = await response.text();
|
|
476
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
477
|
+
try {
|
|
478
|
+
return JSON.parse(text);
|
|
479
|
+
} catch {
|
|
480
|
+
return null;
|
|
481
|
+
}
|
|
482
|
+
})() : null;
|
|
483
|
+
return { ok: response.ok, status: response.status, payload, text };
|
|
484
|
+
}
|
|
485
|
+
function payloadError(payload, fallback) {
|
|
486
|
+
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
|
487
|
+
const record = payload;
|
|
488
|
+
const value = record.error ?? record.message ?? record.reason;
|
|
489
|
+
if (typeof value === "string" && value.trim())
|
|
490
|
+
return value.trim();
|
|
491
|
+
}
|
|
492
|
+
return fallback;
|
|
493
|
+
}
|
|
494
|
+
function checkoutPathsFromProjectPayload(payload) {
|
|
495
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
496
|
+
return [];
|
|
497
|
+
const project = payload.project;
|
|
498
|
+
if (!project || typeof project !== "object" || Array.isArray(project))
|
|
499
|
+
return [];
|
|
500
|
+
const checkouts = project.checkouts;
|
|
501
|
+
if (!Array.isArray(checkouts))
|
|
502
|
+
return [];
|
|
503
|
+
return [...checkouts].reverse().flatMap((entry) => {
|
|
504
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
505
|
+
return [];
|
|
506
|
+
const path = entry.path;
|
|
507
|
+
return typeof path === "string" && path.trim() ? [path.trim()] : [];
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
function checkoutPathFromPreparePayload(payload) {
|
|
511
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
512
|
+
return null;
|
|
513
|
+
const checkout = payload.checkout;
|
|
514
|
+
if (!checkout || typeof checkout !== "object" || Array.isArray(checkout))
|
|
515
|
+
return null;
|
|
516
|
+
const path = checkout.path;
|
|
517
|
+
return typeof path === "string" && path.trim() ? path.trim() : null;
|
|
518
|
+
}
|
|
519
|
+
async function validateAndPersistRemoteRoot(input) {
|
|
520
|
+
const candidate = input.candidate.trim();
|
|
521
|
+
if (!candidate || !isAbsolute(candidate)) {
|
|
522
|
+
return remoteProjectLinkFailure({
|
|
523
|
+
status: "invalid_root",
|
|
524
|
+
alias: input.alias,
|
|
525
|
+
baseUrl: input.baseUrl,
|
|
526
|
+
repoSlug: input.repoSlug,
|
|
527
|
+
message: `Remote project root candidate is not an absolute server-host path: ${candidate || "(empty)"}.`
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
if (localCheckoutPathCandidate(input.projectRoot, candidate)) {
|
|
531
|
+
return remoteProjectLinkFailure({
|
|
532
|
+
status: "invalid_root",
|
|
533
|
+
alias: input.alias,
|
|
534
|
+
baseUrl: input.baseUrl,
|
|
535
|
+
repoSlug: input.repoSlug,
|
|
536
|
+
message: `Refusing to use local checkout path ${candidate} as a remote server project root.`
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
let response;
|
|
540
|
+
try {
|
|
541
|
+
response = await requestRemoteProjectLinkJson(input.baseUrl, "/api/server/project-root", input.authToken, {
|
|
542
|
+
method: "POST",
|
|
543
|
+
headers: { "content-type": "application/json" },
|
|
544
|
+
body: JSON.stringify({ projectRoot: candidate })
|
|
545
|
+
});
|
|
546
|
+
} catch (error) {
|
|
547
|
+
return remoteProjectLinkFailure({
|
|
548
|
+
status: "error",
|
|
549
|
+
alias: input.alias,
|
|
550
|
+
baseUrl: input.baseUrl,
|
|
551
|
+
repoSlug: input.repoSlug,
|
|
552
|
+
message: `Could not validate remote project root ${candidate}: ${error instanceof Error ? error.message : String(error)}`
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
if (response.status === 401 || response.status === 403) {
|
|
556
|
+
return remoteProjectLinkFailure({
|
|
557
|
+
status: "auth_required",
|
|
558
|
+
alias: input.alias,
|
|
559
|
+
baseUrl: input.baseUrl,
|
|
560
|
+
repoSlug: input.repoSlug,
|
|
561
|
+
statusCode: response.status,
|
|
562
|
+
message: `Remote server ${input.alias} requires authentication before validating project root ${candidate}.`
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
if (!response.ok) {
|
|
566
|
+
return remoteProjectLinkFailure({
|
|
567
|
+
status: "invalid_root",
|
|
568
|
+
alias: input.alias,
|
|
569
|
+
baseUrl: input.baseUrl,
|
|
570
|
+
repoSlug: input.repoSlug,
|
|
571
|
+
statusCode: response.status,
|
|
572
|
+
message: `Remote server rejected project root ${candidate} (${response.status}): ${payloadError(response.payload, response.text || "project-root validation failed")}`
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
const record = response.payload && typeof response.payload === "object" && !Array.isArray(response.payload) ? response.payload : {};
|
|
576
|
+
if (record.ok !== true) {
|
|
577
|
+
return remoteProjectLinkFailure({
|
|
578
|
+
status: "invalid_root",
|
|
579
|
+
alias: input.alias,
|
|
580
|
+
baseUrl: input.baseUrl,
|
|
581
|
+
repoSlug: input.repoSlug,
|
|
582
|
+
message: `Remote server did not accept project root ${candidate}: ${payloadError(response.payload, "project-root validation failed")}`
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
const accepted = typeof record.projectRoot === "string" && record.projectRoot.trim() ? record.projectRoot.trim() : typeof record.requestedProjectRoot === "string" && record.requestedProjectRoot.trim() ? record.requestedProjectRoot.trim() : candidate;
|
|
586
|
+
writeRepoServerProjectRoot(input.projectRoot, accepted, { alias: input.alias, baseUrl: input.baseUrl, project: input.repoSlug });
|
|
587
|
+
writeProjectLinkConnection(input.projectRoot, input.repoSlug, input.alias);
|
|
588
|
+
const status = input.status ?? (input.prepared ? "prepared" : "backfilled");
|
|
589
|
+
return remoteProjectLinkSuccess({
|
|
590
|
+
status,
|
|
591
|
+
alias: input.alias,
|
|
592
|
+
baseUrl: input.baseUrl,
|
|
593
|
+
repoSlug: input.repoSlug,
|
|
594
|
+
serverProjectRoot: accepted,
|
|
595
|
+
source: input.source,
|
|
596
|
+
prepared: input.prepared,
|
|
597
|
+
validated: true,
|
|
598
|
+
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}.`
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
async function ensureRemoteProjectRootLink(projectRoot, options = {}) {
|
|
602
|
+
let selected;
|
|
603
|
+
try {
|
|
604
|
+
selected = resolveSelectedConnection(projectRoot);
|
|
605
|
+
} catch (error) {
|
|
606
|
+
return remoteProjectLinkFailure({ status: "error", message: error instanceof Error ? error.message : String(error) });
|
|
607
|
+
}
|
|
608
|
+
if (!selected || selected.connection.kind !== "remote") {
|
|
609
|
+
return remoteProjectLinkFailure({ status: "not_remote", message: "No remote Rig server is selected for this repo." });
|
|
610
|
+
}
|
|
611
|
+
const repo = readRepoConnection(projectRoot);
|
|
612
|
+
const explicitRepoRequested = options.repoSlug !== undefined;
|
|
613
|
+
const explicitRepoSlug = explicitRepoRequested ? normalizeRepoSlug(options.repoSlug) : null;
|
|
614
|
+
const repoProjectSlug = normalizeRepoSlug(repo?.project);
|
|
615
|
+
const repoSlug = explicitRepoSlug ?? repoProjectSlug ?? readProjectLinkSlug(projectRoot);
|
|
616
|
+
const alias = selected.alias;
|
|
617
|
+
const baseUrl = selected.connection.baseUrl;
|
|
618
|
+
const authToken = options.authToken === undefined ? readGitHubBearerTokenForRemote(projectRoot) : options.authToken;
|
|
619
|
+
const mode = options.mode ?? "backfill-only";
|
|
620
|
+
if (explicitRepoRequested && !explicitRepoSlug) {
|
|
621
|
+
return remoteProjectLinkFailure({
|
|
622
|
+
status: "missing_project",
|
|
623
|
+
alias,
|
|
624
|
+
baseUrl,
|
|
625
|
+
message: `Invalid --repo value ${JSON.stringify(options.repoSlug)}. Expected owner/repo.`
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
if (!repoSlug) {
|
|
629
|
+
return remoteProjectLinkFailure({
|
|
630
|
+
status: "missing_project",
|
|
631
|
+
alias,
|
|
632
|
+
baseUrl,
|
|
633
|
+
message: `Remote server ${alias} is selected, but this checkout has no GitHub repo slug recorded.`
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
const skippedCandidates = [];
|
|
637
|
+
const storedRoot = selected.serverProjectRoot?.trim() || null;
|
|
638
|
+
const storedRootMatchesResolvedRepo = repoProjectSlug === repoSlug;
|
|
639
|
+
if (storedRoot && storedRootMatchesResolvedRepo) {
|
|
640
|
+
if (localCheckoutPathCandidate(projectRoot, storedRoot)) {
|
|
641
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
642
|
+
skippedCandidates.push(storedRoot);
|
|
643
|
+
} else {
|
|
644
|
+
const storedResult = await validateAndPersistRemoteRoot({
|
|
645
|
+
projectRoot,
|
|
646
|
+
alias,
|
|
647
|
+
baseUrl,
|
|
648
|
+
authToken,
|
|
649
|
+
repoSlug,
|
|
650
|
+
candidate: storedRoot,
|
|
651
|
+
source: "stored",
|
|
652
|
+
status: "ready"
|
|
653
|
+
});
|
|
654
|
+
if (storedResult.ok || storedResult.status === "auth_required" || storedResult.status === "error")
|
|
655
|
+
return storedResult;
|
|
656
|
+
clearRepoServerProjectRoot(projectRoot);
|
|
657
|
+
skippedCandidates.push(storedRoot);
|
|
658
|
+
}
|
|
659
|
+
} else if (storedRoot) {
|
|
660
|
+
skippedCandidates.push(storedRoot);
|
|
661
|
+
}
|
|
662
|
+
const authCandidate = inferRemoteServerProjectRootCandidateFromAuthState(projectRoot, repoSlug);
|
|
663
|
+
if (authCandidate) {
|
|
664
|
+
const authResult = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: authCandidate, source: "auth-state" });
|
|
665
|
+
if (authResult.ok || authResult.status === "auth_required")
|
|
666
|
+
return authResult;
|
|
667
|
+
}
|
|
668
|
+
let registryResponse;
|
|
669
|
+
try {
|
|
670
|
+
registryResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}`, authToken);
|
|
671
|
+
} catch (error) {
|
|
672
|
+
return remoteProjectLinkFailure({
|
|
673
|
+
status: "error",
|
|
674
|
+
alias,
|
|
675
|
+
baseUrl,
|
|
676
|
+
repoSlug,
|
|
677
|
+
message: `Could not read remote project registry for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
if (registryResponse.status === 401 || registryResponse.status === 403) {
|
|
681
|
+
return remoteProjectLinkFailure({
|
|
682
|
+
status: "auth_required",
|
|
683
|
+
alias,
|
|
684
|
+
baseUrl,
|
|
685
|
+
repoSlug,
|
|
686
|
+
statusCode: registryResponse.status,
|
|
687
|
+
message: `Remote server ${alias} requires authentication before reading the ${repoSlug} project registry.`
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
if (registryResponse.ok) {
|
|
691
|
+
const candidates = checkoutPathsFromProjectPayload(registryResponse.payload);
|
|
692
|
+
for (const candidate of candidates) {
|
|
693
|
+
const result = await validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate, source: "registry" });
|
|
694
|
+
if (result.ok || result.status === "auth_required")
|
|
695
|
+
return result;
|
|
696
|
+
skippedCandidates.push(candidate);
|
|
697
|
+
}
|
|
698
|
+
if (mode === "backfill-only") {
|
|
699
|
+
return remoteProjectLinkFailure({
|
|
700
|
+
status: candidates.length > 0 ? "invalid_root" : "no_server_checkout",
|
|
701
|
+
alias,
|
|
702
|
+
baseUrl,
|
|
703
|
+
repoSlug,
|
|
704
|
+
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.`,
|
|
705
|
+
skippedCandidates
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
} else if (registryResponse.status === 404) {
|
|
709
|
+
if (mode === "backfill-only") {
|
|
710
|
+
return remoteProjectLinkFailure({
|
|
711
|
+
status: "project_not_registered",
|
|
712
|
+
alias,
|
|
713
|
+
baseUrl,
|
|
714
|
+
repoSlug,
|
|
715
|
+
statusCode: registryResponse.status,
|
|
716
|
+
message: `Remote server ${alias} has no registered project record for ${repoSlug}.`
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
return remoteProjectLinkFailure({
|
|
721
|
+
status: "error",
|
|
722
|
+
alias,
|
|
723
|
+
baseUrl,
|
|
724
|
+
repoSlug,
|
|
725
|
+
statusCode: registryResponse.status,
|
|
726
|
+
message: `Could not backfill ${repoSlug} from remote registry (${registryResponse.status}): ${payloadError(registryResponse.payload, registryResponse.text || "registry lookup failed")}`,
|
|
727
|
+
skippedCandidates
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
let prepareResponse;
|
|
731
|
+
try {
|
|
732
|
+
prepareResponse = await requestRemoteProjectLinkJson(baseUrl, `/api/projects/${encodeURIComponent(repoSlug)}/prepare-checkout`, authToken, {
|
|
733
|
+
method: "POST",
|
|
734
|
+
headers: { "content-type": "application/json" },
|
|
735
|
+
body: JSON.stringify({ checkout: { kind: "managed-clone" }, repoUrl: `https://github.com/${repoSlug}.git` })
|
|
736
|
+
});
|
|
737
|
+
} catch (error) {
|
|
738
|
+
return remoteProjectLinkFailure({
|
|
739
|
+
status: "error",
|
|
740
|
+
alias,
|
|
741
|
+
baseUrl,
|
|
742
|
+
repoSlug,
|
|
743
|
+
message: `Could not prepare remote checkout for ${repoSlug} on ${alias}: ${error instanceof Error ? error.message : String(error)}`
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
if (prepareResponse.status === 401 || prepareResponse.status === 403) {
|
|
747
|
+
return remoteProjectLinkFailure({
|
|
748
|
+
status: "auth_required",
|
|
749
|
+
alias,
|
|
750
|
+
baseUrl,
|
|
751
|
+
repoSlug,
|
|
752
|
+
statusCode: prepareResponse.status,
|
|
753
|
+
message: `Remote server ${alias} requires authentication before preparing ${repoSlug}.`
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
if (!prepareResponse.ok) {
|
|
757
|
+
return remoteProjectLinkFailure({
|
|
758
|
+
status: "error",
|
|
759
|
+
alias,
|
|
760
|
+
baseUrl,
|
|
761
|
+
repoSlug,
|
|
762
|
+
statusCode: prepareResponse.status,
|
|
763
|
+
message: `Remote checkout prepare failed for ${repoSlug} (${prepareResponse.status}): ${payloadError(prepareResponse.payload, prepareResponse.text || "prepare-checkout failed")}`,
|
|
764
|
+
skippedCandidates
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
const preparedPath = checkoutPathFromPreparePayload(prepareResponse.payload);
|
|
768
|
+
if (!preparedPath) {
|
|
769
|
+
return remoteProjectLinkFailure({
|
|
770
|
+
status: "invalid_root",
|
|
771
|
+
alias,
|
|
772
|
+
baseUrl,
|
|
773
|
+
repoSlug,
|
|
774
|
+
message: `Remote checkout prepare for ${repoSlug} did not return a checkout.path.`,
|
|
775
|
+
skippedCandidates
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
return validateAndPersistRemoteRoot({ projectRoot, alias, baseUrl, authToken, repoSlug, candidate: preparedPath, source: "prepare", prepared: true });
|
|
779
|
+
}
|
|
349
780
|
async function ensureServerForCli(projectRoot) {
|
|
350
781
|
try {
|
|
351
782
|
const selected = resolveSelectedConnection(projectRoot);
|
|
352
783
|
if (selected?.connection.kind === "remote") {
|
|
353
784
|
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
354
785
|
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
355
|
-
const
|
|
786
|
+
const link = await ensureRemoteProjectRootLink(projectRoot, { mode: "backfill-only", authToken });
|
|
356
787
|
return {
|
|
357
788
|
baseUrl: selected.connection.baseUrl,
|
|
358
789
|
authToken,
|
|
359
790
|
connectionKind: "remote",
|
|
360
|
-
serverProjectRoot
|
|
791
|
+
serverProjectRoot: link.ok ? link.serverProjectRoot ?? null : null
|
|
361
792
|
};
|
|
362
793
|
}
|
|
363
794
|
reportServerPhase("Starting local Rig server\u2026");
|
|
@@ -375,32 +806,6 @@ async function ensureServerForCli(projectRoot) {
|
|
|
375
806
|
throw error;
|
|
376
807
|
}
|
|
377
808
|
}
|
|
378
|
-
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
379
|
-
const repo = readRepoConnection(projectRoot);
|
|
380
|
-
const slug = repo?.project?.trim();
|
|
381
|
-
if (!slug)
|
|
382
|
-
return null;
|
|
383
|
-
try {
|
|
384
|
-
const url = new URL(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`);
|
|
385
|
-
if (authToken && queryAuthFallbackEnabled())
|
|
386
|
-
url.searchParams.set("rt", authToken);
|
|
387
|
-
const response = await fetch(url, {
|
|
388
|
-
headers: mergeHeaders(undefined, authToken)
|
|
389
|
-
});
|
|
390
|
-
if (!response.ok)
|
|
391
|
-
return null;
|
|
392
|
-
const payload = await response.json();
|
|
393
|
-
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
394
|
-
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
395
|
-
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
396
|
-
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
397
|
-
if (path)
|
|
398
|
-
writeRepoServerProjectRoot(projectRoot, path);
|
|
399
|
-
return path;
|
|
400
|
-
} catch {
|
|
401
|
-
return null;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
809
|
function mergeCookie(existing, name, value) {
|
|
405
810
|
const encoded = `${name}=${encodeURIComponent(value)}`;
|
|
406
811
|
if (!existing?.trim())
|
|
@@ -492,13 +897,20 @@ function canUseRemoteWithoutProjectRoot(pathname) {
|
|
|
492
897
|
async function requestServerJson(context, pathname, init = {}) {
|
|
493
898
|
const server = await ensureServerForCli(context.projectRoot);
|
|
494
899
|
const requestUrl = new URL(`${server.baseUrl}${pathname}`);
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
900
|
+
let scopedServerProjectRoot = server.serverProjectRoot;
|
|
901
|
+
if (server.connectionKind === "remote" && !scopedServerProjectRoot && !canUseRemoteWithoutProjectRoot(requestUrl.pathname) && (server.authToken || !isLoopbackRemoteBaseUrl(server.baseUrl))) {
|
|
902
|
+
const link = await ensureRemoteProjectRootLink(context.projectRoot, { mode: "backfill-only", authToken: server.authToken });
|
|
903
|
+
if (link.ok && link.serverProjectRoot) {
|
|
904
|
+
scopedServerProjectRoot = link.serverProjectRoot;
|
|
905
|
+
} else {
|
|
906
|
+
const repo = readRepoConnection(context.projectRoot);
|
|
907
|
+
const target = link.alias && link.baseUrl ? `${link.alias} (${link.baseUrl})` : server.baseUrl;
|
|
908
|
+
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 });
|
|
909
|
+
}
|
|
498
910
|
}
|
|
499
911
|
const headers = mergeHeaders(init.headers, server.authToken);
|
|
500
|
-
if (
|
|
501
|
-
headers.set("x-rig-project-root",
|
|
912
|
+
if (scopedServerProjectRoot)
|
|
913
|
+
headers.set("x-rig-project-root", scopedServerProjectRoot);
|
|
502
914
|
if (server.connectionKind === "remote" && server.authToken && queryAuthFallbackEnabled()) {
|
|
503
915
|
requestUrl.searchParams.set("rt", server.authToken);
|
|
504
916
|
}
|
|
@@ -1029,12 +1441,12 @@ function runSetupInit(projectRoot) {
|
|
|
1029
1441
|
const stateDir = resolveControlPlaneHostStateDir(projectRoot);
|
|
1030
1442
|
const logsDir = resolveControlPlaneHostLogsDir(projectRoot);
|
|
1031
1443
|
const artifactsDir = resolveControlPlaneArtifactsDir(projectRoot);
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1444
|
+
mkdirSync3(stateDir, { recursive: true });
|
|
1445
|
+
mkdirSync3(logsDir, { recursive: true });
|
|
1446
|
+
mkdirSync3(artifactsDir, { recursive: true });
|
|
1035
1447
|
const failuresPath = resolve6(stateDir, "failed_approaches.md");
|
|
1036
1448
|
if (!existsSync5(failuresPath)) {
|
|
1037
|
-
|
|
1449
|
+
writeFileSync3(failuresPath, `# Failed Approaches
|
|
1038
1450
|
|
|
1039
1451
|
`, "utf-8");
|
|
1040
1452
|
}
|