Merge pull request #430 from nwparker/cloud/ja21-preflight-flag

Cloud: Run on preflight behind an off-by-default flag (JA-21)
This commit is contained in:
Neil
2026-11-11 04:24:00 -08:00
committed by GitHub
5 changed files with 286 additions and 7 deletions
@@ -57,12 +57,15 @@ function Preview({
vmSelected = false,
recipes = NO_RECIPES,
recipeValue = null,
access
access,
composer = false
}: {
vmSelected?: boolean
recipes?: EphemeralVmRecipeOption[]
recipeValue?: string | null
access?: OrcaCloudRunTargetAccess
/** The composer's picker: setup comes after Start, JA-21's preflight applies when flagged. */
composer?: boolean
}): React.JSX.Element {
return (
<div className="h-[460px] p-4">
@@ -81,6 +84,7 @@ function Preview({
recipes.some((recipe) => recipe.provider === 'orca-cloud') ? 'offered' : 'needs-sign-in'
}
{...(access ? { orcaCloudAccess: access } : {})}
{...(composer ? { onSelectOrcaCloud: () => {} } : {})}
/>
</div>
)
@@ -281,3 +285,68 @@ export const rowActions = defineFixture({
...vmSelectedClosed,
interact: pressKeys('ArrowDown', 'ArrowDown')
})
// JA-21 (flagged): the composer's picker for an owner at a billing wall, flag off then on.
const preflight = (on: boolean): void => {
if (on) {
localStorage.setItem('orca:dev:runOnPreflight', '1')
} else {
localStorage.removeItem('orca:dev:runOnPreflight')
}
}
export const composerOutOfHoursPreflightOff = defineFixture({
width: 420,
render: () => {
preflight(false)
return (
<Preview
composer
recipes={withCloud}
access={{ state: 'out-of-hours', cause: 'hours', canManageBilling: true }}
/>
)
},
interact: open
})
export const composerOutOfHoursPreflight = defineFixture({
width: 420,
render: () => {
preflight(true)
return (
<Preview
composer
recipes={withCloud}
access={{ state: 'out-of-hours', cause: 'hours', canManageBilling: true }}
/>
)
},
interact: open
})
export const composerPaymentIssuePreflight = defineFixture({
width: 420,
render: () => {
preflight(true)
return (
<Preview
composer
recipes={withCloud}
access={{ state: 'payment-issue', canManageBilling: true }}
/>
)
},
interact: open
})
export const composerLapsedPreflight = defineFixture({
width: 420,
render: () => {
preflight(true)
return (
<Preview
composer
recipes={withCloud}
access={{ state: 'no-plan', lapsed: true, canManageBilling: true }}
/>
)
},
interact: open
})
@@ -0,0 +1,151 @@
// @vitest-environment happy-dom
import React, { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { ProjectHostSetupOption } from '@/lib/project-host-setup-options'
import { TooltipProvider } from '@/components/ui/tooltip'
import RunTargetCombobox from './RunTargetCombobox'
import { useAppStore } from '@/store/index'
import { resetOrcaCloudBillingCache } from './orca-cloud-billing-cache'
import type { EphemeralVmRecipeOption } from './run-target-options'
import type { OrcaCloudRunTargetAccess } from './orca-cloud-run-target-access'
vi.mock('@/components/ui/popover', () => ({
Popover: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
PopoverAnchor: ({ children }: { children: React.ReactNode }) => <>{children}</>,
PopoverContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>
}))
vi.mock('@/components/billing/plans/plans-page-store', () => ({ openPlansPage: vi.fn() }))
const hosts: ProjectHostSetupOption[] = [
{
id: 'mac',
kind: 'ready',
projectId: 'p',
hostId: 'local',
repoId: 'mac',
label: 'Local Mac',
detail: 'desktop-app',
path: '/tmp/desktop-app'
}
]
const cloud: EphemeralVmRecipeOption = {
id: 'cloud',
name: 'Orca Cloud',
provider: 'orca-cloud',
create: 'orca-cloud create'
}
let container: HTMLDivElement
let root: Root
const openSettingsTarget = vi.fn()
function input(): HTMLInputElement {
const node = container.querySelector<HTMLInputElement>('[role="combobox"]')
if (!node) {
throw new Error('run target input not found')
}
return node
}
function press(key: string): void {
act(() => {
input().dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true }))
})
}
/** The composer's picker: Orca Cloud picks go through onSelectOrcaCloud (setup after Start). */
function renderComposer(access: OrcaCloudRunTargetAccess, onSelectOrcaCloud = vi.fn()): void {
act(() => {
root.render(
<TooltipProvider>
<RunTargetCombobox
hostOptions={hosts}
hostValue="mac"
hostExecutionHostId="local"
recipes={[cloud]}
orcaCloudAvailability="offered"
recipeValue={null}
onRecipeChange={() => {}}
onAddSshHost={() => {}}
onAddRemoteServer={() => {}}
orcaCloudAccess={access}
onSelectOrcaCloud={onSelectOrcaCloud}
/>
</TooltipProvider>
)
})
act(() => {
input().focus()
input().click()
})
}
function cloudRow(): HTMLElement | null {
return (
[...container.querySelectorAll<HTMLElement>('[role="option"]')].find((row) =>
row.textContent?.startsWith('Orca Cloud')
) ?? null
)
}
const outOfHours: OrcaCloudRunTargetAccess = {
state: 'out-of-hours',
cause: 'hours',
canManageBilling: true
}
beforeEach(() => {
resetOrcaCloudBillingCache()
openSettingsTarget.mockClear()
useAppStore.setState({ openSettingsTarget })
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
})
afterEach(() => {
act(() => {
root.unmount()
})
container.remove()
localStorage.removeItem('orca:dev:runOnPreflight')
})
describe('Run on preflight (JA-21, flagged)', () => {
it('flag off: nothing changes, the composer picks Orca Cloud and setup comes after Start', () => {
const onSelectOrcaCloud = vi.fn()
renderComposer(outOfHours, onSelectOrcaCloud)
expect(cloudRow()?.getAttribute('aria-disabled')).toBeNull()
expect(container.textContent).not.toContain('Turn on overage')
press('ArrowDown')
press('Enter')
expect(onSelectOrcaCloud).toHaveBeenCalledOnce()
})
it('flag on: a billing wall disables the row with its reason and one fix, and Enter runs the fix', () => {
localStorage.setItem('orca:dev:runOnPreflight', '1')
const onSelectOrcaCloud = vi.fn()
renderComposer(outOfHours, onSelectOrcaCloud)
expect(cloudRow()?.getAttribute('aria-disabled')).toBe('true')
expect(cloudRow()?.textContent).toContain('Out of sandbox hours')
expect(cloudRow()?.textContent).toContain('Turn on overage')
press('ArrowDown')
press('Enter')
expect(onSelectOrcaCloud).not.toHaveBeenCalled()
expect(openSettingsTarget).toHaveBeenCalledWith(
expect.objectContaining({ pane: 'cloud-billing' })
)
})
it('flag on: a plan not started yet is setup, not a wall, so it stays pickable', () => {
localStorage.setItem('orca:dev:runOnPreflight', '1')
const onSelectOrcaCloud = vi.fn()
renderComposer({ state: 'no-plan', lapsed: false, canManageBilling: true }, onSelectOrcaCloud)
expect(cloudRow()?.getAttribute('aria-disabled')).toBeNull()
press('ArrowDown')
press('Enter')
expect(onSelectOrcaCloud).toHaveBeenCalledOnce()
})
})
@@ -14,6 +14,7 @@ import {
type OrcaCloudRunTargetAccess
} from './orca-cloud-run-target-access'
import { ORCA_CLOUD_SANDBOX_SPEC } from '../../../../shared/hosted-sandbox/hosted-sandbox-wire'
import { orcaCloudNeedsFixFirst, useRunOnPreflightEnabled } from '@/lib/orca-cloud/run-on-preflight'
/**
* Looks like a button but is plain option content: focus stays in the combobox input, Enter on the
@@ -274,7 +275,11 @@ function OrcaCloudMeta({
* In the composer, hours and plan details wait until after create (Neil): the row only keeps what
* explains why it can't be picked, "Ask an owner or admin".
*/
function showsMetaAfterStart(access: OrcaCloudRunTargetAccess): boolean {
function showsMetaAfterStart(access: OrcaCloudRunTargetAccess, preflight: boolean): boolean {
// JA-21 (flagged): a billing wall is fixed here, before Start, not on the setup page after it.
if (preflight && orcaCloudNeedsFixFirst(access)) {
return true
}
switch (access.state) {
case 'no-plan':
case 'out-of-hours':
@@ -315,6 +320,7 @@ export function RunTargetOrcaCloudRow({
setupAfterStart?: boolean
}): React.JSX.Element {
const t = useTranslate()
const preflight = useRunOnPreflightEnabled() && setupAfterStart
return (
<RunTargetRow
icon={<Cloud className="size-3.5 shrink-0 text-muted-foreground" />}
@@ -331,10 +337,11 @@ export function RunTargetOrcaCloudRow({
armed={armed}
current={current}
disabled={
(access.state === 'no-plan' ||
((access.state === 'no-plan' ||
access.state === 'out-of-hours' ||
access.state === 'payment-issue') &&
!access.canManageBilling
!access.canManageBilling) ||
(preflight && orcaCloudNeedsFixFirst(access))
}
optionId={optionId}
busy={pending}
@@ -342,7 +349,7 @@ export function RunTargetOrcaCloudRow({
onCommit={onSelect}
description={orcaCloudRowHint(access, t)}
trailing={
setupAfterStart && !showsMetaAfterStart(access) ? undefined : (
setupAfterStart && !showsMetaAfterStart(access, preflight) ? undefined : (
<OrcaCloudMeta access={access} onAction={onBlockedAction} />
)
}
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { orcaCloudNeedsFixFirst, useRunOnPreflightEnabled } from '@/lib/orca-cloud/run-on-preflight'
import type { OrcaCloudAvailability } from '../../../../shared/orca-yaml-hook-types'
import { isOrcaCloudRecipe, type EphemeralVmRecipeOption } from './run-target-options'
import { getOrcaCloudPick, type OrcaCloudRunTargetAccess } from './orca-cloud-run-target-access'
@@ -70,6 +71,8 @@ export function useRunTargetOrcaCloud({
runBlockedAction: () => void
select: (recipe: EphemeralVmRecipeOption | null) => void
} {
// JA-21 (flagged): in the composer a billing wall routes to its fix instead of picking Orca Cloud.
const preflight = useRunOnPreflightEnabled() && Boolean(onSelectOrcaCloud)
const fetched = useOrcaCloudRunTargetAccess(
!override && (recipes.some(isOrcaCloudRecipe) || offer?.state === 'no-plan')
)
@@ -100,7 +103,11 @@ export function useRunTargetOrcaCloud({
const pickFor = useCallback(
(resolved: OrcaCloudRunTargetAccess, recipe: EphemeralVmRecipeOption | null): void => {
const pick = getOrcaCloudPick(resolved)
if (onSelectOrcaCloud && pick.kind !== 'blocked') {
if (
onSelectOrcaCloud &&
pick.kind !== 'blocked' &&
!(preflight && orcaCloudNeedsFixFirst(resolved))
) {
close()
onSelectOrcaCloud()
return
@@ -114,7 +121,7 @@ export function useRunTargetOrcaCloud({
selectRecipe(recipe.id)
}
},
[close, onSelectOrcaCloud, selectRecipe]
[close, onSelectOrcaCloud, preflight, selectRecipe]
)
const runBlockedAction = useCallback((): void => pickFor(access, null), [access, pickFor])
const select = useCallback(
@@ -0,0 +1,45 @@
// JA-21, behind a flag Neil can veto (off unless the account's capabilities turn it on): the
// composer's Run on preflight. An owner or admin whose team can't start a sandbox right now (out of
// hours, payment failed, plan ended) sees Orca Cloud disabled in place, with the reason and the one
// fix, instead of picking it and hitting the wall after Start. Members already get "Ask an owner or
// admin". A plan not started yet (trial offer) is setup, not a wall, so it stays pickable.
import { useAppStore } from '@/store'
import type { OrcaCloudRunTargetAccess } from '@/components/new-workspace/orca-cloud-run-target-access'
export const RUN_ON_PREFLIGHT_CAPABILITY = 'runOnPreflight'
/** Dev/QA override for the harnesses: localStorage['orca:dev:runOnPreflight'] = '1'. */
const DEV_OVERRIDE_KEY = 'orca:dev:runOnPreflight'
function devOverride(): boolean {
try {
return globalThis.localStorage?.getItem(DEV_OVERRIDE_KEY) === '1'
} catch {
return false
}
}
export function useRunOnPreflightEnabled(): boolean {
const capability = useAppStore((state) => {
const status = state.orcaProfileAuthStatus
return status?.state === 'connected'
? status.capabilities?.flags[RUN_ON_PREFLIGHT_CAPABILITY] === true
: false
})
return capability || devOverride()
}
/** An owner or admin must fix billing before a sandbox can start: the row offers that fix. */
export function orcaCloudNeedsFixFirst(access: OrcaCloudRunTargetAccess): boolean {
switch (access.state) {
case 'out-of-hours':
case 'payment-issue':
return access.canManageBilling
case 'no-plan':
return access.canManageBilling && access.lapsed
case 'loading':
case 'unknown':
case 'entitled':
case 'signed-out':
return false
}
}