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