Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/cli/client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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`
}
19 changes: 19 additions & 0 deletions src/cli/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/open/open.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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}`,
Expand Down
3 changes: 2 additions & 1 deletion src/remote/connect.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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}`),
})
Expand Down
31 changes: 31 additions & 0 deletions test/unit/cli-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading