Files
orca/src/main/hosted-sandbox/cloud-create-from-github.ts
T
Neil ac06c09040 feat(cloud-create): a GitHub-only Orca Cloud create shows the full build sheet (GH-CREATE-SHEET)
Starting a cloud workspace from a GitHub repository with no clone here showed only a
'Starting <repo> in Orca Cloud…' toast. It now opens the same build sheet as a cloud
create from a local clone: Start sandbox, Clone repository with git's progress, Open
workspace; the same failure cards and JA-21b billing blocker (plan, hours, spend cap,
payment; members get no See plans); Retry; and Back to draft (composer back on Orca
Cloud with the GitHub repository picked and the prompt kept).

Main: createFromGitHub runs the hosted driver's create path (createSandboxWithProgress,
shared with the owned create): boot plan, create, waitForClone, with the same
[orca-cloud] progress lines streamed on ephemeralVm:provisionEvent under the window's
creation id, the same failure reason lines and codes (hostedCreateFailure), Cancel via
ephemeralVm:cancelProvision, and an Idempotency-Key. The workspace id is fixed per
creation so a Retry sends the same body.

Renderer: the GitHub start is a pending creation with the Orca Cloud recipe and a
githubSource; retryBackgroundWorktreeCreation routes it to its own runner. A Retry
after a lost answer resends the same key (same sandbox back); after an answered failure
it is a new attempt; after a failed open it only opens the sandbox it already has.
The paired workspace has no environment-checks step.
2026-11-12 15:26:26 -08:00

98 lines
3.9 KiB
TypeScript

// "Start in Orca Cloud" for a GitHub repository with no clone here (cloud-start D4), with the same
// build sheet as an owned create (GH-CREATE-SHEET): the same progress lines (boot plan, start,
// clone or update with git's percent) and the same failure lines and codes, so the window's sheet
// reads both alike.
import { createHash, randomBytes } from 'node:crypto'
import type {
CloudWorkspaceErrorCode,
CloudWorkspaceResult
} from '../../shared/hosted-sandbox/cloud-workspace-api'
import type { HostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
import { createSandboxWithProgress } from './hosted-sandbox-driver'
import { hostedCreateFailure, hostedProgressLine } from './hosted-sandbox-recipe'
/** The server's Idempotency-Key format. */
export const CLOUD_CREATE_IDEMPOTENCY_KEY = /^[A-Za-z0-9_.:-]{8,128}$/
/** A creation id from the window: the build sheet's log and its Cancel are keyed by it. */
export const CLOUD_CREATE_PROVISION_ID = /^[A-Za-z0-9_.:-]{1,128}$/
export type CreateFromGitHubInput = {
/** `owner/repo`. */
slug: string
/** The name typed in the composer; empty for none. */
title: string
provisionId: string | null
idempotencyKey: string | null
}
export type CreateFromGitHubClient = Pick<
HostedSandboxControlPlaneClient,
'create' | 'bootPlan' | 'cloneStatus' | 'destroy'
>
/**
* The workspace id: `<repo>-<hex>`, like the phone's. Fixed per creation so a Retry sends the same
* body as the first attempt; the server replays an Idempotency-Key only for the same body.
*/
export function githubWorkspaceId(slug: string, provisionId: string | null): string {
const name = (slug.split('/').pop() ?? 'workspace').replace(/[^A-Za-z0-9_.-]/g, '-').slice(0, 48)
const suffix = provisionId
? createHash('sha256').update(provisionId).digest('hex').slice(0, 8)
: randomBytes(4).toString('hex')
return `${name}-${suffix}`
}
const CODE_BY_FAILURE: Partial<Record<string, CloudWorkspaceErrorCode>> = {
cloudSignInRequired: 'unauthorized',
cloudPlanRequired: 'entitlement_denied',
cloudConcurrencyLimit: 'entitlement_denied',
cloudUnavailable: 'unavailable'
}
/**
* Starts the sandbox and waits for its clone, reporting each step through `onLog`. A failure comes
* back with the failure code as `reason` (e.g. `cloudPlanRequired`) after its reason line was logged.
*/
export async function createCloudWorkspaceFromGitHub(
client: CreateFromGitHubClient,
input: CreateFromGitHubInput,
io: { signal?: AbortSignal; onLog?: (chunk: string) => void } = {}
): Promise<CloudWorkspaceResult<{ sandboxId: string }>> {
try {
const ready = await createSandboxWithProgress(
client,
{
workspaceId: githubWorkspaceId(input.slug, input.provisionId),
machineSize: 'standard',
repo: { url: `https://github.com/${input.slug}.git` },
...(input.title ? { workspaceName: input.title } : {}),
...(input.idempotencyKey ? { idempotencyKey: input.idempotencyKey } : {})
},
{
...(io.signal ? { signal: io.signal } : {}),
onProgress: (progress) => io.onLog?.(hostedProgressLine(progress))
}
)
if (io.signal?.aborted) {
// Cancelled just as it answered: nothing will open it, so it must not keep running (and billing).
await client.destroy(ready.sandbox.id).catch(() => undefined)
return { ok: false, code: 'failed', message: 'Cancelled.' }
}
return { ok: true, value: { sandboxId: ready.sandbox.id } }
} catch (error) {
const result = hostedCreateFailure(error)
if (result.stderr) {
io.onLog?.(result.stderr)
}
const errorCode = result.failure?.errorCode ?? null
return {
ok: false,
code: (errorCode ? CODE_BY_FAILURE[errorCode] : undefined) ?? 'failed',
message:
result.failure?.error ??
(result.stderr.trim() || (error instanceof Error ? error.message : String(error))),
reason: errorCode
}
}
}