Files
paseo/packages/cli/src/commands/hub/connect.ts
T
Mohamed Boudra 0110302b6f fix(daemon): keep lifecycle operations on the selected instance (#4575)
* fix(daemon): keep lifecycle operations on the selected instance

Restart reconstructed launch inputs and address fallback could redirect a home-selected shutdown to another daemon. Let supervisors publish bound endpoints, retain their worker restart contract, and share captured local lifecycle ownership between CLI and Desktop.

* test(daemon): verify bounded desktop readiness and cancellable artifacts

* test(cli): verify reload target hints and durable offline pairing

* test(daemon): align lifecycle fixtures with ownership and targeting

* fix(daemon): preserve reachability when status details time out

Provider discovery can outlast a status request after authentication succeeds. Keep local connection facts separate from unavailable details, while explicit remote queries retain their error outcome.

Migrate CLI and packaged Desktop fixtures to persistent managed configuration and explicit targets. Isolate generic Git observation from forge discovery and reuse disposable CLI homes on Windows.

* test(daemon): observe restart through the worker status RPC

The bounded CLI status observation may omit worker details while an authenticated connection remains healthy. Exercise the app-style restart through its full RPC and retain the replacement observation, with a real slow provider probe covering the distinction.
2026-09-11 08:33:22 +02:00

82 lines
3.1 KiB
TypeScript

import type { Command } from "commander";
import { withOutput } from "../../output/index.js";
import { addJsonAndDaemonHostOptions } from "../../utils/command-options.js";
import { resolveHubCredential, resolveHubOrigin } from "./authority.js";
import type { HubHttpClient } from "./hub-client/index.js";
import type { HubCredentialStore } from "./credentials.js";
import type { HubDaemonConnection } from "./daemon-client.js";
import { withHubDaemon } from "./daemon-client.js";
import { hubStatusResult } from "./status-output.js";
import { reportHubProgress, type HubReporter } from "./reporter.js";
import { addHubResolutionHelp } from "./help.js";
interface HubConnectOptions {
apiKey?: string;
host?: string;
daemonTarget: import("../../utils/daemon-target.js").DaemonTarget;
json?: boolean;
permission?: readonly string[];
permissions?: readonly string[];
}
interface HubConnectDependencies {
env: Readonly<Record<string, string | undefined>>;
credentials: HubCredentialStore;
hub: Pick<HubHttpClient, "issueEnrollmentToken">;
daemon: HubDaemonConnection;
reporter: HubReporter;
}
export async function runHubConnect(
originInput: string | undefined,
options: HubConnectOptions,
dependencies: HubConnectDependencies,
) {
const resolution = {
options: { origin: originInput, apiKey: options.apiKey },
env: dependencies.env,
credentials: dependencies.credentials,
};
const origin = resolveHubOrigin(resolution);
reportHubProgress(dependencies.reporter, options, `Connecting this daemon to ${origin}`);
const credential = resolveHubCredential({ ...resolution, origin });
const token = await dependencies.hub.issueEnrollmentToken(origin, credential);
const permissions = options.permissions ?? options.permission ?? [];
return withHubDaemon(dependencies.daemon, options.daemonTarget, async (daemon) => {
const response = await daemon.connectHub(origin, token, permissions);
if (
response.status.hubOrigin !== null &&
!samePermissions(response.status.permissions, permissions)
) {
await daemon.disconnectHub(false).catch(() => undefined);
throw new Error(
"The daemon did not honor the requested Hub access. Update Paseo before connecting it.",
);
}
return hubStatusResult(response.status);
});
}
export function addHubConnectCommand(parent: Command, dependencies: HubConnectDependencies): void {
addJsonAndDaemonHostOptions(
addHubResolutionHelp(
parent
.command("connect")
.description("Enroll this daemon with a Paseo Hub")
.argument("[origin]", "Paseo Hub origin")
.option("--api-key <secret>", "Organization API key")
.option("--permission <permission...>", "Grant daemon permission during connection"),
),
).action(
withOutput(async (...args) => {
const origin = args[0] as string | undefined;
const options = args.at(-2) as HubConnectOptions;
return runHubConnect(origin, options, dependencies);
}),
);
}
function samePermissions(actual: readonly string[], expected: readonly string[]): boolean {
return actual.length === expected.length && expected.every((scope) => actual.includes(scope));
}