mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
refactor(cloud): hosted driver implements the upstream EnvironmentProviderDriver
Upstream now ships the generic interface (results are recipe documents; no lease, rollover, egress or entitlement concepts). Hosted extensions move to hosted-environment-provider.ts: HostedEnvironmentProviderDriver adds renewLease / rollover / leaseExpiresAt / egressInjection, and entitlement denials become HostedEntitlementDeniedError (code unauthorized, reason = deny reason). The capped-session rollover check moves from the shared kit into the hosted test.
This commit is contained in:
@@ -7,7 +7,7 @@ import {
|
|||||||
type JsonValue
|
type JsonValue
|
||||||
} from '../../shared/ephemeral-vm-recipes'
|
} from '../../shared/ephemeral-vm-recipes'
|
||||||
import type { EphemeralVmCheckoutMode } from '../../shared/orca-yaml-hook-types'
|
import type { EphemeralVmCheckoutMode } from '../../shared/orca-yaml-hook-types'
|
||||||
import type { EnvironmentHandle, EnvironmentReady } from './environment-provider-driver'
|
import type { EnvironmentHandle } from './environment-provider-driver'
|
||||||
|
|
||||||
const USER_DATA_KEY = 'environmentDriver'
|
const USER_DATA_KEY = 'environmentDriver'
|
||||||
|
|
||||||
@@ -19,18 +19,23 @@ const HandleSchema = z
|
|||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export function environmentReadyToRecipeResult(
|
export function environmentRecipeResult(
|
||||||
ready: EnvironmentReady,
|
handle: EnvironmentHandle,
|
||||||
|
orcaServer: { pairingCode: string; projectRoot: string },
|
||||||
checkoutMode: EphemeralVmCheckoutMode
|
checkoutMode: EphemeralVmCheckoutMode
|
||||||
): EphemeralVmRecipeResult {
|
): EphemeralVmRecipeResult {
|
||||||
const userData: Record<string, JsonValue> = {
|
const userData: Record<string, JsonValue> = {
|
||||||
[USER_DATA_KEY]: {
|
[USER_DATA_KEY]: {
|
||||||
driverId: ready.handle.driverId,
|
driverId: handle.driverId,
|
||||||
environmentId: ready.handle.environmentId,
|
environmentId: handle.environmentId,
|
||||||
driverState: ready.handle.driverState
|
driverState: handle.driverState
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const connection = { ...ready.connection }
|
const connection = {
|
||||||
|
type: 'orca-server' as const,
|
||||||
|
pairingCode: orcaServer.pairingCode,
|
||||||
|
projectRoot: orcaServer.projectRoot
|
||||||
|
}
|
||||||
return checkoutMode === 'provisioned-root'
|
return checkoutMode === 'provisioned-root'
|
||||||
? { schemaVersion: 2, checkoutMode: 'provisioned-root', connection, userData }
|
? { schemaVersion: 2, checkoutMode: 'provisioned-root', connection, userData }
|
||||||
: { schemaVersion: 1, connection, userData }
|
: { schemaVersion: 1, connection, userData }
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
// Orca Cloud's additions on top of the upstream EnvironmentProviderDriver: a lease on a capped
|
||||||
|
// session, rollover onto the same disk, egress-injected secrets and plan entitlement denials.
|
||||||
|
import {
|
||||||
|
EnvironmentProviderError,
|
||||||
|
type EnvironmentHandle,
|
||||||
|
type EnvironmentProviderCallContext,
|
||||||
|
type EnvironmentProviderCapabilities,
|
||||||
|
type EnvironmentProviderDriver,
|
||||||
|
type EnvironmentReady,
|
||||||
|
type EnvironmentSessionInfo,
|
||||||
|
type EnvironmentStatus
|
||||||
|
} from '../environment-providers/environment-provider-driver'
|
||||||
|
|
||||||
|
export type HostedEnvironmentCapabilities = EnvironmentProviderCapabilities &
|
||||||
|
Readonly<{
|
||||||
|
/** Secrets can be injected at the egress proxy instead of living in the VM. */
|
||||||
|
egressInjection: boolean
|
||||||
|
}>
|
||||||
|
|
||||||
|
export type HostedSessionInfo = EnvironmentSessionInfo & {
|
||||||
|
/** When the current Orca lease lapses unless renewed. */
|
||||||
|
leaseExpiresAt: number | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export type HostedStatusSnapshot = {
|
||||||
|
status: EnvironmentStatus
|
||||||
|
session: HostedSessionInfo | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export type HostedEnvironmentReady = EnvironmentReady & { session: HostedSessionInfo }
|
||||||
|
|
||||||
|
// Narrows the upstream return types; still assignable wherever an EnvironmentProviderDriver is expected.
|
||||||
|
export type HostedEnvironmentProviderDriver = Omit<
|
||||||
|
EnvironmentProviderDriver,
|
||||||
|
'capabilities' | 'create' | 'resume' | 'suspend' | 'status'
|
||||||
|
> & {
|
||||||
|
readonly capabilities: HostedEnvironmentCapabilities
|
||||||
|
create(...args: Parameters<EnvironmentProviderDriver['create']>): Promise<HostedEnvironmentReady>
|
||||||
|
resume(
|
||||||
|
handle: EnvironmentHandle,
|
||||||
|
ctx?: EnvironmentProviderCallContext
|
||||||
|
): Promise<HostedEnvironmentReady>
|
||||||
|
suspend(
|
||||||
|
handle: EnvironmentHandle,
|
||||||
|
ctx?: EnvironmentProviderCallContext
|
||||||
|
): Promise<HostedStatusSnapshot>
|
||||||
|
status(
|
||||||
|
handle: EnvironmentHandle,
|
||||||
|
ctx?: EnvironmentProviderCallContext
|
||||||
|
): Promise<HostedStatusSnapshot>
|
||||||
|
/** Keep the capped session alive while Orca reports work. */
|
||||||
|
renewLease(
|
||||||
|
handle: EnvironmentHandle,
|
||||||
|
extendMs: number,
|
||||||
|
ctx?: EnvironmentProviderCallContext
|
||||||
|
): Promise<HostedStatusSnapshot>
|
||||||
|
/** End this session and start a fresh one from the same disk. */
|
||||||
|
rollover(
|
||||||
|
handle: EnvironmentHandle,
|
||||||
|
ctx?: EnvironmentProviderCallContext
|
||||||
|
): Promise<HostedEnvironmentReady>
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The plan refused the start; `reason` is the billing deny reason. Reads as `unauthorized` generically. */
|
||||||
|
export class HostedEntitlementDeniedError extends EnvironmentProviderError {
|
||||||
|
constructor(message: string, reason: string | null) {
|
||||||
|
super('unauthorized', message, reason)
|
||||||
|
this.name = 'HostedEntitlementDeniedError'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isHostedEntitlementDenied(error: unknown): error is HostedEntitlementDeniedError {
|
||||||
|
return error instanceof HostedEntitlementDeniedError
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
// HTTPS client for the Orca sandbox control plane. The desktop holds only a short-lived Orca token;
|
// HTTPS client for the Orca sandbox control plane. The desktop holds only a short-lived Orca token;
|
||||||
// the provider credential never leaves the control plane.
|
// the provider credential never leaves the control plane.
|
||||||
import type { z } from 'zod'
|
import type { z } from 'zod'
|
||||||
|
import { HostedEntitlementDeniedError } from './hosted-environment-provider'
|
||||||
import {
|
import {
|
||||||
HostedSandboxErrorResponseSchema,
|
HostedSandboxErrorResponseSchema,
|
||||||
HostedSandboxListResponseSchema,
|
HostedSandboxListResponseSchema,
|
||||||
@@ -176,7 +177,6 @@ const STATUS_CODES: Record<string, EnvironmentProviderErrorCode> = {
|
|||||||
forbidden: 'unauthorized',
|
forbidden: 'unauthorized',
|
||||||
not_found: 'not_found',
|
not_found: 'not_found',
|
||||||
conflict: 'conflict',
|
conflict: 'conflict',
|
||||||
entitlement_denied: 'entitlement_denied',
|
|
||||||
invalid_request: 'invalid_request',
|
invalid_request: 'invalid_request',
|
||||||
invalid_json: 'invalid_request',
|
invalid_json: 'invalid_request',
|
||||||
billing_unavailable: 'unavailable',
|
billing_unavailable: 'unavailable',
|
||||||
@@ -187,6 +187,12 @@ const STATUS_CODES: Record<string, EnvironmentProviderErrorCode> = {
|
|||||||
|
|
||||||
function errorFromResponse(status: number, payload: unknown): EnvironmentProviderError {
|
function errorFromResponse(status: number, payload: unknown): EnvironmentProviderError {
|
||||||
const parsed = HostedSandboxErrorResponseSchema.safeParse(payload)
|
const parsed = HostedSandboxErrorResponseSchema.safeParse(payload)
|
||||||
|
if (parsed.success && parsed.data.error === 'entitlement_denied') {
|
||||||
|
return new HostedEntitlementDeniedError(
|
||||||
|
'Orca Cloud refused the request (entitlement_denied).',
|
||||||
|
parsed.data.reason ?? null
|
||||||
|
)
|
||||||
|
}
|
||||||
if (parsed.success) {
|
if (parsed.success) {
|
||||||
const code = STATUS_CODES[parsed.data.error] ?? 'unavailable'
|
const code = STATUS_CODES[parsed.data.error] ?? 'unavailable'
|
||||||
return new EnvironmentProviderError(
|
return new EnvironmentProviderError(
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { parseEphemeralVmRecipeResult } from '../../shared/ephemeral-vm-recipes'
|
import {
|
||||||
|
getEphemeralVmRecipeResultConnection,
|
||||||
|
parseEphemeralVmRecipeResult
|
||||||
|
} from '../../shared/ephemeral-vm-recipes'
|
||||||
import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver'
|
import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver'
|
||||||
import { runEnvironmentProviderDriverContract } from '../environment-providers/environment-provider-driver-contract.test-support'
|
import { runEnvironmentProviderDriverContract } from '../environment-providers/environment-provider-driver-contract.test-support'
|
||||||
import {
|
import {
|
||||||
environmentHandleFromRecipeResult,
|
environmentHandleFromRecipeResult,
|
||||||
environmentReadyToRecipeResult
|
environmentRecipeResult
|
||||||
} from '../environment-providers/environment-provider-recipe-adapter'
|
} from '../environment-providers/environment-provider-recipe-adapter'
|
||||||
|
import { isHostedEntitlementDenied } from './hosted-environment-provider'
|
||||||
import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
||||||
import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver'
|
import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver'
|
||||||
import { createFakeHostedControlPlane } from './fake-hosted-control-plane.test-support'
|
import { createFakeHostedControlPlane } from './fake-hosted-control-plane.test-support'
|
||||||
@@ -17,7 +21,7 @@ function setup(options: { token?: string; fetch?: typeof fetch } = {}) {
|
|||||||
getAccessToken: async () => options.token ?? plane.token,
|
getAccessToken: async () => options.token ?? plane.token,
|
||||||
fetch: options.fetch ?? plane.fetch
|
fetch: options.fetch ?? plane.fetch
|
||||||
})
|
})
|
||||||
return { plane, driver: createHostedSandboxDriver(client) }
|
return { plane, client, driver: createHostedSandboxDriver(client) }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function rejection(promise: Promise<unknown>): Promise<unknown> {
|
async function rejection(promise: Promise<unknown>): Promise<unknown> {
|
||||||
@@ -28,11 +32,12 @@ async function rejection(promise: Promise<unknown>): Promise<unknown> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runEnvironmentProviderDriverContract('HostedSandboxDriver', async () => {
|
runEnvironmentProviderDriverContract('HostedSandboxDriver', async () => {
|
||||||
const { plane, driver } = setup()
|
const { plane, client, driver } = setup()
|
||||||
return {
|
return {
|
||||||
driver,
|
driver,
|
||||||
liveEnvironmentIds: async () =>
|
liveEnvironmentIds: async () =>
|
||||||
[...plane.sandboxes.values()].filter((s) => s.status !== 'destroyed').map((s) => s.id)
|
[...plane.sandboxes.values()].filter((s) => s.status !== 'destroyed').map((s) => s.id),
|
||||||
|
reopenDriver: () => createHostedSandboxDriver(client)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -41,7 +46,7 @@ describe('HostedSandboxDriver', () => {
|
|||||||
const { plane, driver } = setup()
|
const { plane, driver } = setup()
|
||||||
plane.denyNextStart('included_hours_exhausted')
|
plane.denyNextStart('included_hours_exhausted')
|
||||||
const error = await rejection(driver.create({ workspaceId: 'ws-1' }))
|
const error = await rejection(driver.create({ workspaceId: 'ws-1' }))
|
||||||
expect(isEnvironmentProviderError(error, 'entitlement_denied')).toBe(true)
|
expect(isHostedEntitlementDenied(error)).toBe(true)
|
||||||
expect(isEnvironmentProviderError(error) && error.reason).toBe('included_hours_exhausted')
|
expect(isEnvironmentProviderError(error) && error.reason).toBe('included_hours_exhausted')
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -83,7 +88,14 @@ describe('HostedSandboxDriver', () => {
|
|||||||
it('round-trips through the recipe result the runtime record stores', async () => {
|
it('round-trips through the recipe result the runtime record stores', async () => {
|
||||||
const { driver } = setup()
|
const { driver } = setup()
|
||||||
const ready = await driver.create({ workspaceId: 'ws-1' })
|
const ready = await driver.create({ workspaceId: 'ws-1' })
|
||||||
const recipeResult = environmentReadyToRecipeResult(ready, 'provisioned-root')
|
expect(ready.result).toEqual(
|
||||||
|
environmentRecipeResult(ready.handle, readyConnection(ready), 'orca-worktree')
|
||||||
|
)
|
||||||
|
const recipeResult = environmentRecipeResult(
|
||||||
|
ready.handle,
|
||||||
|
readyConnection(ready),
|
||||||
|
'provisioned-root'
|
||||||
|
)
|
||||||
const parsed = parseEphemeralVmRecipeResult(JSON.stringify(recipeResult))
|
const parsed = parseEphemeralVmRecipeResult(JSON.stringify(recipeResult))
|
||||||
expect(parsed.ok).toBe(true)
|
expect(parsed.ok).toBe(true)
|
||||||
if (!parsed.ok) {
|
if (!parsed.ok) {
|
||||||
@@ -97,4 +109,30 @@ describe('HostedSandboxDriver', () => {
|
|||||||
ready.handle.environmentId
|
ready.handle.environmentId
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('keeps a capped session alive and rolls it over onto the same disk', async () => {
|
||||||
|
const { driver } = setup()
|
||||||
|
const created = await driver.create({ workspaceId: 'ws-rollover' })
|
||||||
|
const renewed = await driver.renewLease(created.handle, 10 * 60_000)
|
||||||
|
expect(renewed.status).toBe('running')
|
||||||
|
expect(renewed.session!.leaseExpiresAt!).toBeLessThanOrEqual(renewed.session!.deadlineAt!)
|
||||||
|
|
||||||
|
const rolled = await driver.rollover(created.handle)
|
||||||
|
expect(rolled.handle.environmentId).toBe(created.handle.environmentId)
|
||||||
|
expect(rolled.session.startedAt).toBeGreaterThan(created.session.startedAt)
|
||||||
|
expect(rolled.session.deadlineAt! - rolled.session.startedAt).toBeLessThanOrEqual(
|
||||||
|
driver.capabilities.maxSessionMs!
|
||||||
|
)
|
||||||
|
await driver.destroy(created.handle)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function readyConnection(ready: {
|
||||||
|
result: Parameters<typeof getEphemeralVmRecipeResultConnection>[0]
|
||||||
|
}) {
|
||||||
|
const connection = getEphemeralVmRecipeResultConnection(ready.result)
|
||||||
|
if (connection.type !== 'orca-server') {
|
||||||
|
throw new Error('expected an orca-server connection')
|
||||||
|
}
|
||||||
|
return connection
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,19 +10,22 @@ import {
|
|||||||
EnvironmentProviderError,
|
EnvironmentProviderError,
|
||||||
isEnvironmentProviderError,
|
isEnvironmentProviderError,
|
||||||
type EnvironmentHandle,
|
type EnvironmentHandle,
|
||||||
type EnvironmentProviderCapabilities,
|
type EnvironmentStatus
|
||||||
type EnvironmentProviderDriver,
|
|
||||||
type EnvironmentReady,
|
|
||||||
type EnvironmentSessionInfo,
|
|
||||||
type EnvironmentStatus,
|
|
||||||
type EnvironmentStatusSnapshot
|
|
||||||
} from '../environment-providers/environment-provider-driver'
|
} from '../environment-providers/environment-provider-driver'
|
||||||
|
import { environmentRecipeResult } from '../environment-providers/environment-provider-recipe-adapter'
|
||||||
|
import type {
|
||||||
|
HostedEnvironmentCapabilities,
|
||||||
|
HostedEnvironmentProviderDriver,
|
||||||
|
HostedEnvironmentReady,
|
||||||
|
HostedSessionInfo,
|
||||||
|
HostedStatusSnapshot
|
||||||
|
} from './hosted-environment-provider'
|
||||||
import type { HostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
import type { HostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
||||||
|
|
||||||
export const HOSTED_SANDBOX_DRIVER_ID = 'orca-cloud'
|
export const HOSTED_SANDBOX_DRIVER_ID = 'orca-cloud'
|
||||||
|
|
||||||
/** Measured on Vercel: stop is a reboot (PTYs lost), disk persists, Pro caps a session at 24h. */
|
/** Measured on Vercel: stop is a reboot (PTYs lost), disk persists, Pro caps a session at 24h. */
|
||||||
export const HOSTED_SANDBOX_CAPABILITIES: EnvironmentProviderCapabilities = Object.freeze({
|
export const HOSTED_SANDBOX_CAPABILITIES: HostedEnvironmentCapabilities = Object.freeze({
|
||||||
memoryPause: false,
|
memoryPause: false,
|
||||||
maxSessionMs: 24 * 60 * 60_000,
|
maxSessionMs: 24 * 60 * 60_000,
|
||||||
fsPersistOnStop: true,
|
fsPersistOnStop: true,
|
||||||
@@ -43,13 +46,12 @@ const STATUS: Record<HostedSandboxView['status'], EnvironmentStatus> = {
|
|||||||
|
|
||||||
export function createHostedSandboxDriver(
|
export function createHostedSandboxDriver(
|
||||||
client: HostedSandboxControlPlaneClient
|
client: HostedSandboxControlPlaneClient
|
||||||
): EnvironmentProviderDriver {
|
): HostedEnvironmentProviderDriver {
|
||||||
return {
|
return {
|
||||||
id: HOSTED_SANDBOX_DRIVER_ID,
|
id: HOSTED_SANDBOX_DRIVER_ID,
|
||||||
capabilities: HOSTED_SANDBOX_CAPABILITIES,
|
capabilities: HOSTED_SANDBOX_CAPABILITIES,
|
||||||
|
|
||||||
async create(spec, ctx) {
|
async create(spec, ctx) {
|
||||||
ctx?.onProgress?.({ phase: 'checking-entitlement' })
|
|
||||||
const machineSize = toMachineSize(spec.machineSize)
|
const machineSize = toMachineSize(spec.machineSize)
|
||||||
ctx?.onProgress?.({ phase: 'provisioning' })
|
ctx?.onProgress?.({ phase: 'provisioning' })
|
||||||
const ready = await client.create(
|
const ready = await client.create(
|
||||||
@@ -94,12 +96,11 @@ export function createHostedSandboxDriver(
|
|||||||
},
|
},
|
||||||
|
|
||||||
async renewLease(handle, extendMs, ctx) {
|
async renewLease(handle, extendMs, ctx) {
|
||||||
ctx?.onProgress?.({ phase: 'renewing' })
|
|
||||||
return toSnapshot(await client.renewLease(sandboxIdOf(handle), extendMs, ctx?.signal))
|
return toSnapshot(await client.renewLease(sandboxIdOf(handle), extendMs, ctx?.signal))
|
||||||
},
|
},
|
||||||
|
|
||||||
async rollover(handle, ctx) {
|
async rollover(handle, ctx) {
|
||||||
ctx?.onProgress?.({ phase: 'rolling-over' })
|
ctx?.onProgress?.({ phase: 'resuming' })
|
||||||
const ready = await client.rollover(sandboxIdOf(handle), ctx?.signal)
|
const ready = await client.rollover(sandboxIdOf(handle), ctx?.signal)
|
||||||
ctx?.onProgress?.({ phase: 'connecting' })
|
ctx?.onProgress?.({ phase: 'connecting' })
|
||||||
return toReady(ready)
|
return toReady(ready)
|
||||||
@@ -126,7 +127,7 @@ function sandboxIdOf(handle: EnvironmentHandle): string {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
function toSession(view: HostedSandboxView): EnvironmentSessionInfo | null {
|
function toSession(view: HostedSandboxView): HostedSessionInfo | null {
|
||||||
return view.session
|
return view.session
|
||||||
? {
|
? {
|
||||||
startedAt: view.session.startedAt,
|
startedAt: view.session.startedAt,
|
||||||
@@ -136,11 +137,11 @@ function toSession(view: HostedSandboxView): EnvironmentSessionInfo | null {
|
|||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
|
|
||||||
function toSnapshot(view: HostedSandboxView): EnvironmentStatusSnapshot {
|
function toSnapshot(view: HostedSandboxView): HostedStatusSnapshot {
|
||||||
return { status: STATUS[view.status], session: toSession(view) }
|
return { status: STATUS[view.status], session: toSession(view) }
|
||||||
}
|
}
|
||||||
|
|
||||||
function toReady(ready: HostedSandboxReadyResponse): EnvironmentReady {
|
function toReady(ready: HostedSandboxReadyResponse): HostedEnvironmentReady {
|
||||||
const session = toSession(ready.sandbox)
|
const session = toSession(ready.sandbox)
|
||||||
if (ready.sandbox.status !== 'running' || !session) {
|
if (ready.sandbox.status !== 'running' || !session) {
|
||||||
throw new EnvironmentProviderError(
|
throw new EnvironmentProviderError(
|
||||||
@@ -148,13 +149,14 @@ function toReady(ready: HostedSandboxReadyResponse): EnvironmentReady {
|
|||||||
'Orca Cloud reported a connection for an environment that is not running.'
|
'Orca Cloud reported a connection for an environment that is not running.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
const handle: EnvironmentHandle = {
|
||||||
|
driverId: HOSTED_SANDBOX_DRIVER_ID,
|
||||||
|
environmentId: ready.sandbox.id,
|
||||||
|
driverState: { sandboxId: ready.sandbox.id }
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
handle: {
|
handle,
|
||||||
driverId: HOSTED_SANDBOX_DRIVER_ID,
|
result: environmentRecipeResult(handle, ready.connection, 'orca-worktree'),
|
||||||
environmentId: ready.sandbox.id,
|
|
||||||
driverState: { sandboxId: ready.sandbox.id }
|
|
||||||
},
|
|
||||||
connection: ready.connection,
|
|
||||||
session
|
session
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,15 +11,13 @@ import {
|
|||||||
type RecipeStepProviderResult
|
type RecipeStepProviderResult
|
||||||
} from '../../shared/ephemeral-vm-recipe-step-providers'
|
} from '../../shared/ephemeral-vm-recipe-step-providers'
|
||||||
import { getRemoteUrl } from '../git/repo'
|
import { getRemoteUrl } from '../git/repo'
|
||||||
|
import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver'
|
||||||
|
import { environmentHandleFromRecipeResult } from '../environment-providers/environment-provider-recipe-adapter'
|
||||||
import {
|
import {
|
||||||
isEnvironmentProviderError,
|
isHostedEntitlementDenied,
|
||||||
type EnvironmentProviderDriver,
|
type HostedEnvironmentProviderDriver,
|
||||||
type EnvironmentReady
|
type HostedEnvironmentReady
|
||||||
} from '../environment-providers/environment-provider-driver'
|
} from './hosted-environment-provider'
|
||||||
import {
|
|
||||||
environmentHandleFromRecipeResult,
|
|
||||||
environmentReadyToRecipeResult
|
|
||||||
} from '../environment-providers/environment-provider-recipe-adapter'
|
|
||||||
import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
||||||
import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver'
|
import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver'
|
||||||
import type { HostedSandboxConfig } from './hosted-sandbox-config'
|
import type { HostedSandboxConfig } from './hosted-sandbox-config'
|
||||||
@@ -43,7 +41,7 @@ const LifecyclePayload = z.looseObject({ recipeResult: EphemeralVmRecipeResultSc
|
|||||||
|
|
||||||
export function createHostedSandboxRecipeDriver(
|
export function createHostedSandboxRecipeDriver(
|
||||||
config: HostedSandboxConfig
|
config: HostedSandboxConfig
|
||||||
): EnvironmentProviderDriver {
|
): HostedEnvironmentProviderDriver {
|
||||||
return createHostedSandboxDriver(
|
return createHostedSandboxDriver(
|
||||||
createHostedSandboxControlPlaneClient({
|
createHostedSandboxControlPlaneClient({
|
||||||
baseUrl: config.controlPlaneUrl,
|
baseUrl: config.controlPlaneUrl,
|
||||||
@@ -66,7 +64,7 @@ function ok(stdout: string): RecipeStepProviderResult {
|
|||||||
function failed(error: unknown): RecipeStepProviderResult {
|
function failed(error: unknown): RecipeStepProviderResult {
|
||||||
const base = { stdout: '', exitCode: 1, signal: null }
|
const base = { stdout: '', exitCode: 1, signal: null }
|
||||||
if (
|
if (
|
||||||
isEnvironmentProviderError(error, 'entitlement_denied') &&
|
isHostedEntitlementDenied(error) &&
|
||||||
(error.reason === 'concurrency_limit' || error.reason === 'member_concurrency_limit')
|
(error.reason === 'concurrency_limit' || error.reason === 'member_concurrency_limit')
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
@@ -75,7 +73,7 @@ function failed(error: unknown): RecipeStepProviderResult {
|
|||||||
failure: ephemeralVmFailure('cloudConcurrencyLimit')
|
failure: ephemeralVmFailure('cloudConcurrencyLimit')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isEnvironmentProviderError(error, 'entitlement_denied')) {
|
if (isHostedEntitlementDenied(error)) {
|
||||||
return {
|
return {
|
||||||
...base,
|
...base,
|
||||||
stderr: `Orca Cloud: ${error.reason ?? 'no plan'}\n`,
|
stderr: `Orca Cloud: ${error.reason ?? 'no plan'}\n`,
|
||||||
@@ -99,9 +97,9 @@ function failed(error: unknown): RecipeStepProviderResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createHostedSandboxStepProvider(
|
export function createHostedSandboxStepProvider(
|
||||||
driver: EnvironmentProviderDriver,
|
driver: HostedEnvironmentProviderDriver,
|
||||||
hooks: {
|
hooks: {
|
||||||
onReady?: (ready: EnvironmentReady) => void
|
onReady?: (ready: HostedEnvironmentReady) => void
|
||||||
onStopped?: (environmentId: string) => void
|
onStopped?: (environmentId: string) => void
|
||||||
} = {}
|
} = {}
|
||||||
): RecipeStepProvider {
|
): RecipeStepProvider {
|
||||||
@@ -125,7 +123,7 @@ export function createHostedSandboxStepProvider(
|
|||||||
ctx
|
ctx
|
||||||
)
|
)
|
||||||
hooks.onReady?.(ready)
|
hooks.onReady?.(ready)
|
||||||
return ok(JSON.stringify(environmentReadyToRecipeResult(ready, 'orca-worktree')))
|
return ok(JSON.stringify(ready.result))
|
||||||
}
|
}
|
||||||
const payload = LifecyclePayload.parse(JSON.parse(stdin ?? '{}'))
|
const payload = LifecyclePayload.parse(JSON.parse(stdin ?? '{}'))
|
||||||
const handle = payload.recipeResult
|
const handle = payload.recipeResult
|
||||||
@@ -137,7 +135,7 @@ export function createHostedSandboxStepProvider(
|
|||||||
if (mode === 'resume') {
|
if (mode === 'resume') {
|
||||||
const ready = await driver.resume(handle, ctx)
|
const ready = await driver.resume(handle, ctx)
|
||||||
hooks.onReady?.(ready)
|
hooks.onReady?.(ready)
|
||||||
return ok(JSON.stringify(environmentReadyToRecipeResult(ready, 'orca-worktree')))
|
return ok(JSON.stringify(ready.result))
|
||||||
}
|
}
|
||||||
await (mode === 'suspend' ? driver.suspend(handle, ctx) : driver.destroy(handle, ctx))
|
await (mode === 'suspend' ? driver.suspend(handle, ctx) : driver.destroy(handle, ctx))
|
||||||
hooks.onStopped?.(handle.environmentId)
|
hooks.onStopped?.(handle.environmentId)
|
||||||
@@ -149,7 +147,7 @@ export function createHostedSandboxStepProvider(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function registerHostedSandboxRecipe(
|
export function registerHostedSandboxRecipe(
|
||||||
driver: EnvironmentProviderDriver,
|
driver: HostedEnvironmentProviderDriver,
|
||||||
hooks: Parameters<typeof createHostedSandboxStepProvider>[1] = {}
|
hooks: Parameters<typeof createHostedSandboxStepProvider>[1] = {}
|
||||||
): () => void {
|
): () => void {
|
||||||
return registerEphemeralVmRecipeStepProvider(
|
return registerEphemeralVmRecipeStepProvider(
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
// What to do next with a session-capped, no-memory-pause environment (Vercel: stop = reboot, 24h cap).
|
// What to do next with a session-capped, no-memory-pause environment (Vercel: stop = reboot, 24h cap).
|
||||||
// Idle stop, the opportunistic refresh and the forced cap rollover are one decision so they never race.
|
// Idle stop, the opportunistic refresh and the forced cap rollover are one decision so they never race.
|
||||||
import type {
|
import type { EnvironmentProviderCapabilities } from '../environment-providers/environment-provider-driver'
|
||||||
EnvironmentProviderCapabilities,
|
import type { HostedSessionInfo } from './hosted-environment-provider'
|
||||||
EnvironmentSessionInfo
|
|
||||||
} from '../environment-providers/environment-provider-driver'
|
|
||||||
|
|
||||||
export type HostedActivitySample = {
|
export type HostedActivitySample = {
|
||||||
/** orcad's idle probes: clients, terminals, agents, automations. Unverifiable counts as busy. */
|
/** orcad's idle probes: clients, terminals, agents, automations. Unverifiable counts as busy. */
|
||||||
@@ -36,7 +34,7 @@ export const HOSTED_LEASE_POLICY = {
|
|||||||
export function decideHostedLeaseAction(input: {
|
export function decideHostedLeaseAction(input: {
|
||||||
now: number
|
now: number
|
||||||
capabilities: EnvironmentProviderCapabilities
|
capabilities: EnvironmentProviderCapabilities
|
||||||
session: EnvironmentSessionInfo
|
session: HostedSessionInfo
|
||||||
activity: HostedActivitySample
|
activity: HostedActivitySample
|
||||||
warned: ReadonlySet<HostedLeaseWarning>
|
warned: ReadonlySet<HostedLeaseWarning>
|
||||||
}): HostedLeaseAction {
|
}): HostedLeaseAction {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
// Drives one Orca Cloud environment's session: renews Orca's lease while work runs, warns before the
|
// Drives one Orca Cloud environment's session: renews Orca's lease while work runs, warns before the
|
||||||
// cap, suspends when idle, and rolls the session over (drain → stop → boot → re-pair → continue agents).
|
// cap, suspends when idle, and rolls the session over (drain → stop → boot → re-pair → continue agents).
|
||||||
|
import type { EnvironmentHandle } from '../environment-providers/environment-provider-driver'
|
||||||
import type {
|
import type {
|
||||||
EnvironmentHandle,
|
HostedEnvironmentProviderDriver,
|
||||||
EnvironmentProviderDriver,
|
HostedEnvironmentReady,
|
||||||
EnvironmentReady,
|
HostedSessionInfo
|
||||||
EnvironmentSessionInfo
|
} from './hosted-environment-provider'
|
||||||
} from '../environment-providers/environment-provider-driver'
|
|
||||||
import type { HostedAgentAutoResumeOutcome } from './hosted-agent-auto-resume'
|
import type { HostedAgentAutoResumeOutcome } from './hosted-agent-auto-resume'
|
||||||
import {
|
import {
|
||||||
HOSTED_LEASE_POLICY,
|
HOSTED_LEASE_POLICY,
|
||||||
@@ -16,13 +16,13 @@ import {
|
|||||||
} from './hosted-session-lease-policy'
|
} from './hosted-session-lease-policy'
|
||||||
|
|
||||||
export type HostedSessionLeasePorts = {
|
export type HostedSessionLeasePorts = {
|
||||||
driver: EnvironmentProviderDriver
|
driver: HostedEnvironmentProviderDriver
|
||||||
handle: EnvironmentHandle
|
handle: EnvironmentHandle
|
||||||
session: EnvironmentSessionInfo
|
session: HostedSessionInfo
|
||||||
readActivity: () => Promise<HostedActivitySample>
|
readActivity: () => Promise<HostedActivitySample>
|
||||||
warn: (minutesLeft: HostedLeaseWarning) => void
|
warn: (minutesLeft: HostedLeaseWarning) => void
|
||||||
/** Re-pairs with the rebooted orcad; resolves once the runtime answers again. */
|
/** Re-pairs with the rebooted orcad; resolves once the runtime answers again. */
|
||||||
reconnect: (ready: EnvironmentReady) => Promise<void>
|
reconnect: (ready: HostedEnvironmentReady) => Promise<void>
|
||||||
resumeAgents: (drainStartedAt: number) => Promise<HostedAgentAutoResumeOutcome>
|
resumeAgents: (drainStartedAt: number) => Promise<HostedAgentAutoResumeOutcome>
|
||||||
onSuspended: () => void
|
onSuspended: () => void
|
||||||
/** Replaces the driver suspend, e.g. to run the window's ordinary Sleep for the workspace. */
|
/** Replaces the driver suspend, e.g. to run the window's ordinary Sleep for the workspace. */
|
||||||
@@ -38,7 +38,7 @@ const UNVERIFIABLE: HostedActivitySample = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class HostedSessionLeaseScheduler {
|
export class HostedSessionLeaseScheduler {
|
||||||
private session: EnvironmentSessionInfo
|
private session: HostedSessionInfo
|
||||||
private readonly warned = new Set<HostedLeaseWarning>()
|
private readonly warned = new Set<HostedLeaseWarning>()
|
||||||
private timer: ReturnType<typeof setTimeout> | null = null
|
private timer: ReturnType<typeof setTimeout> | null = null
|
||||||
private running: Promise<HostedLeaseAction> | null = null
|
private running: Promise<HostedLeaseAction> | null = null
|
||||||
@@ -84,7 +84,7 @@ export class HostedSessionLeaseScheduler {
|
|||||||
})
|
})
|
||||||
switch (action.kind) {
|
switch (action.kind) {
|
||||||
case 'renew': {
|
case 'renew': {
|
||||||
const renewed = await this.ports.driver.renewLease?.(this.ports.handle, action.extendMs)
|
const renewed = await this.ports.driver.renewLease(this.ports.handle, action.extendMs)
|
||||||
if (renewed?.session) {
|
if (renewed?.session) {
|
||||||
this.session = renewed.session
|
this.session = renewed.session
|
||||||
}
|
}
|
||||||
@@ -117,9 +117,6 @@ export class HostedSessionLeaseScheduler {
|
|||||||
|
|
||||||
private async rollover(reason: 'opportunistic' | 'forced'): Promise<void> {
|
private async rollover(reason: 'opportunistic' | 'forced'): Promise<void> {
|
||||||
const { driver, handle } = this.ports
|
const { driver, handle } = this.ports
|
||||||
if (!driver.rollover) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const drainStartedAt = this.now()
|
const drainStartedAt = this.now()
|
||||||
this.log(`[orca-cloud] ${reason} rollover`)
|
this.log(`[orca-cloud] ${reason} rollover`)
|
||||||
const ready = await driver.rollover(handle)
|
const ready = await driver.rollover(handle)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it, vi } from 'vitest'
|
import { describe, expect, it, vi } from 'vitest'
|
||||||
import type { EnvironmentReady } from '../environment-providers/environment-provider-driver'
|
import type { HostedEnvironmentReady } from './hosted-environment-provider'
|
||||||
import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume'
|
import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume'
|
||||||
import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client'
|
||||||
import { createHostedSandboxDriver, HOSTED_SANDBOX_CAPABILITIES } from './hosted-sandbox-driver'
|
import { createHostedSandboxDriver, HOSTED_SANDBOX_CAPABILITIES } from './hosted-sandbox-driver'
|
||||||
@@ -76,7 +76,7 @@ describe('hosted session lease scheduler', () => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
const created = await driver.create({ workspaceId: 'ws-1' })
|
const created = await driver.create({ workspaceId: 'ws-1' })
|
||||||
const reconnect = vi.fn(async (_ready: EnvironmentReady) => {})
|
const reconnect = vi.fn(async (_ready: HostedEnvironmentReady) => {})
|
||||||
const resumeAgents = vi.fn(async () => ({
|
const resumeAgents = vi.fn(async () => ({
|
||||||
offered: 2,
|
offered: 2,
|
||||||
continued: 2,
|
continued: 2,
|
||||||
|
|||||||
@@ -15,11 +15,9 @@ import { callRuntimeEnvironment } from '../ipc/runtime-environments/runtime-envi
|
|||||||
import { invalidateRuntimeEnvironmentTransport } from '../ipc/runtime-environments/runtime-environments'
|
import { invalidateRuntimeEnvironmentTransport } from '../ipc/runtime-environments/runtime-environments'
|
||||||
import { applyResumedRuntimePairing } from '../ephemeral-vm/runtime-resume-pairing'
|
import { applyResumedRuntimePairing } from '../ephemeral-vm/runtime-resume-pairing'
|
||||||
import { noteRecipeVmWakePairing } from '../ephemeral-vm/recipe-vm-wake-pairings'
|
import { noteRecipeVmWakePairing } from '../ephemeral-vm/recipe-vm-wake-pairings'
|
||||||
import type { EnvironmentProviderDriver } from '../environment-providers/environment-provider-driver'
|
import { getEphemeralVmRecipeResultPairingCode } from '../../shared/ephemeral-vm-recipes'
|
||||||
import {
|
import { environmentHandleFromRecipeResult } from '../environment-providers/environment-provider-recipe-adapter'
|
||||||
environmentHandleFromRecipeResult,
|
import type { HostedEnvironmentProviderDriver } from './hosted-environment-provider'
|
||||||
environmentReadyToRecipeResult
|
|
||||||
} from '../environment-providers/environment-provider-recipe-adapter'
|
|
||||||
import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume'
|
import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume'
|
||||||
import { HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver'
|
import { HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver'
|
||||||
import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call'
|
import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call'
|
||||||
@@ -43,7 +41,7 @@ export type HostedSessionRegistry = {
|
|||||||
|
|
||||||
export function createHostedSessionRegistry(deps: {
|
export function createHostedSessionRegistry(deps: {
|
||||||
userDataPath: string
|
userDataPath: string
|
||||||
driver: EnvironmentProviderDriver
|
driver: HostedEnvironmentProviderDriver
|
||||||
windows: () => readonly BrowserWindow[]
|
windows: () => readonly BrowserWindow[]
|
||||||
log?: (line: string) => void
|
log?: (line: string) => void
|
||||||
}): HostedSessionRegistry {
|
}): HostedSessionRegistry {
|
||||||
@@ -100,12 +98,12 @@ export function createHostedSessionRegistry(deps: {
|
|||||||
},
|
},
|
||||||
reconnect: async (ready) => {
|
reconnect: async (ready) => {
|
||||||
updateEphemeralVmRuntimeStatus(deps.userDataPath, runtime.id, {
|
updateEphemeralVmRuntimeStatus(deps.userDataPath, runtime.id, {
|
||||||
recipeResult: environmentReadyToRecipeResult(ready, 'orca-worktree')
|
recipeResult: ready.result
|
||||||
})
|
})
|
||||||
const { environment } = await applyResumedRuntimePairing({
|
const { environment } = await applyResumedRuntimePairing({
|
||||||
userDataPath: deps.userDataPath,
|
userDataPath: deps.userDataPath,
|
||||||
environmentId: runtimeEnvironmentId,
|
environmentId: runtimeEnvironmentId,
|
||||||
pairingCode: ready.connection.pairingCode,
|
pairingCode: getEphemeralVmRecipeResultPairingCode(ready.result) ?? '',
|
||||||
invalidateTransport: invalidateRuntimeEnvironmentTransport
|
invalidateTransport: invalidateRuntimeEnvironmentTransport
|
||||||
})
|
})
|
||||||
noteRecipeVmWakePairing(
|
noteRecipeVmWakePairing(
|
||||||
|
|||||||
Reference in New Issue
Block a user