diff --git a/src/cli/client.ts b/src/cli/client.ts index 2832931..9c599d3 100644 --- a/src/cli/client.ts +++ b/src/cli/client.ts @@ -1,10 +1,12 @@ import { spawn } from "node:child_process" import { SpinNotFoundError } from "./binary" import { + type CodeConfirmingKey, type CodePreparing, type CodeResult, type ErrorCode, type ErrorEnvelope, + isCodeConfirmingKey, isCodePreparing, isErrorEnvelope, type LoginDone, @@ -59,6 +61,8 @@ export interface CodeOptions extends RunOptions { start?: boolean // Hears each step of a server the installation prepares for this VS Code, as it begins. onPreparing?: (preparing: CodePreparing["preparing"]) => void + // Hears that this computer's key waits for the person's passkey, on the page spin opened. + onConfirmingKey?: (confirming: CodeConfirmingKey["confirming_key"]) => void } // CliError is a command's failure as the CLI reported it: the envelope it wrote on stdout. @@ -107,6 +111,8 @@ const LOGIN_GRACE_MS = 30_000 // After a step of a server being prepared, how long code may run on: past the CLI's own wait for // the publish (6 minutes) and the host's attach that follows it (3). const PREPARING_GRACE_MS = 10 * 60_000 +// spin waits ten minutes for a passkey (cmd/cli/cmd/owner.go stepUpWithin), and a little past it. +const CONFIRMING_KEY_GRACE_MS = 11 * 60_000 // Between SIGTERM and SIGKILL, for a CLI that does not stop when asked. const KILL_GRACE_MS = 3_000 // After spin exits, how long its stdout and stderr may stay open: a child it left running that @@ -207,9 +213,13 @@ export class ProcessSpinCli implements SpinCli { if (isCodePreparing(p)) { extend(PREPARING_GRACE_MS) opts.onPreparing?.(p.preparing) + } else if (isCodeConfirmingKey(p)) { + // As long as spin waits for the person's passkey: its own wait ends sooner. + extend(CONFIRMING_KEY_GRACE_MS) + opts.onConfirmingKey?.(p.confirming_key) } }) - const [only, ...rest] = r.docs.filter((v) => !isCodePreparing(v)) + const [only, ...rest] = r.docs.filter((v) => !isCodePreparing(v) && !isCodeConfirmingKey(v)) if (r.exit === 0 && isObject(only) && !("error" in only) && rest.length === 0) { return only as unknown as CodeResult } @@ -410,3 +420,8 @@ function isLoginStart(v: unknown): v is LoginStart { typeof v.expires_in === "number" ) } + +// What a person is told while spin waits for their passkey to add this computer's key. +export function confirmingKeyMessage(c: CodeConfirmingKey["confirming_key"]): string { + return `Confirm with your passkey, in the browser page spin opened, that ${c.name} connects to your workspaces` +} diff --git a/src/cli/contract.ts b/src/cli/contract.ts index 888b1d3..ffda334 100644 --- a/src/cli/contract.ts +++ b/src/cli/contract.ts @@ -119,6 +119,25 @@ export interface CodeResult { not_given?: string } +// The line spin code writes while this computer's key waits to be added to the person's account: +// the page it opened, where their passkey confirms it. A first connection from a computer - VS +// Code's, before anyone ran spin there - waits on this, not on an error. +export interface CodeConfirmingKey { + confirming_key: { + url: string + fingerprint: string + name: string + } +} + +export function isCodeConfirmingKey(v: unknown): v is CodeConfirmingKey { + if (typeof v !== "object" || v === null || !("confirming_key" in v)) { + return false + } + const c = (v as { confirming_key: unknown }).confirming_key + return typeof c === "object" && c !== null && typeof (c as { url?: unknown }).url === "string" +} + export function isCodePreparing(v: unknown): v is CodePreparing { if (typeof v !== "object" || v === null || !("preparing" in v)) { return false diff --git a/src/open/open.ts b/src/open/open.ts index b149c20..523f901 100644 --- a/src/open/open.ts +++ b/src/open/open.ts @@ -1,4 +1,4 @@ -import type { SpinCli } from "../cli/client" +import { confirmingKeyMessage, type SpinCli } from "../cli/client" import type { Workspace } from "../cli/contract" import { SPIN_RESOLVER, spinFolderUri } from "../status/authority" import type { Trace } from "../telemetry" @@ -76,6 +76,7 @@ async function open( vscodeVersion: deps.vscodeVersion, signal, traceparent, + onConfirmingKey: (c) => report(confirmingKeyMessage(c)), onPreparing: (p) => report( `Preparing VS Code ${p.vscode_version} for this installation (once): ${p.step}`, diff --git a/src/remote/connect.ts b/src/remote/connect.ts index 44406ce..3f71f6b 100644 --- a/src/remote/connect.ts +++ b/src/remote/connect.ts @@ -1,4 +1,4 @@ -import type { SpinCli } from "../cli/client" +import { confirmingKeyMessage, type SpinCli } from "../cli/client" import { parseSpinRemote } from "../status/authority" import type { Trace } from "../telemetry" @@ -61,6 +61,7 @@ export async function prepareConnection( vscodeVersion: deps.vscodeVersion, signal, traceparent, + onConfirmingKey: (c) => report(confirmingKeyMessage(c)), onPreparing: (p) => report(`Preparing VS Code ${p.vscode_version} for this installation (once): ${p.step}`), }) diff --git a/test/unit/cli-client.test.ts b/test/unit/cli-client.test.ts index 1419351..b469f0b 100644 --- a/test/unit/cli-client.test.ts +++ b/test/unit/cli-client.test.ts @@ -269,6 +269,37 @@ describe("ProcessSpinCli kills a hung spin", () => { expect(heard).toEqual(["downloading", "building"]) }, 10_000) + // A first connection from this computer waits for the person's passkey to add its key: that + // moves code's deadline, is heard as it comes, and is not the answer. + it("not while this computer's key waits for a passkey and says so", async () => { + const confirming = { + url: "https://app.example/settings/keys?add=k", + fingerprint: "SHA256:x", + name: "laptop", + } + scenario({ + code: { + lineDelayMs: 1000, + stdout: [ + { confirming_key: confirming }, + { + workspace_id: "ws-1", + host: "spin.my-ws", + folder_uri: "vscode-remote://x", + server: "carried", + }, + ], + }, + }) + const heard: unknown[] = [] + await expect( + cli({ code: 1500 }).cli.code("my-ws", undefined, "a".repeat(40), { + onConfirmingKey: (c) => heard.push(c), + }), + ).resolves.toMatchObject({ server: "carried" }) + expect(heard).toEqual([confirming]) + }, 10_000) + it("not while login waits within the code's lifetime", async () => { vi.stubEnv("FAKE_SPIN_LOGIN_DELAY_MS", "3000") await expect(cli({ login: 1500 }).cli.login(() => {})).resolves.toMatchObject({