The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
/*
* Copyright 2025 Daytona Platforms Inc.
* Modified by BoxLite AI, 2025-2026
* SPDX-License-Identifier: AGPL-3.0
*/
import { Injectable, Logger, ServiceUnavailableException } from '@nestjs/common'
import { BoxRepository } from '../../repositories/box.repository'
import { Box } from '../../entities/box.entity'
import { BoxState } from '../../enums/box-state.enum'
import { DONT_SYNC_AGAIN, BoxAction, SYNC_AGAIN, SyncState } from './box.action'
import { RunnerState } from '../../enums/runner-state.enum'
import { RunnerService } from '../../services/runner.service'
import { RunnerAdapterFactory } from '../../runner-adapter/runnerAdapter'
import { OrganizationService } from '../../../organization/services/organization.service'
import { TypedConfigService } from '../../../config/typed-config.service'
import { LockCode, RedisLockProvider } from '../../common/redis-lock.provider'
import { WithSpan } from '../../../common/decorators/otel.decorator'
import { BoxActivityService } from '../../services/box-activity.service'
import { CuratedImagePinService } from '../../../image/services/curated-image-pin.service'
import { boxImageIsOrgOwned } from '../../utils/image-ownership.util'
import { boxVolumeForDispatch } from '../../utils/box-volume.util'
import { FilestoreInstanceResolver } from '../../services/filestore-instance.resolver'
import { resolveNestedVirtualization } from '../../runner-adapter/nested-virtualization-policy'
@Injectable()
export class BoxStartAction extends BoxAction {
protected readonly logger = new Logger(BoxStartAction.name)
constructor(
protected runnerService: RunnerService,
protected runnerAdapterFactory: RunnerAdapterFactory,
protected boxRepository: BoxRepository,
protected readonly organizationService: OrganizationService,
protected readonly configService: TypedConfigService,
protected readonly redisLockProvider: RedisLockProvider,
private readonly boxActivityService: BoxActivityService,
private readonly curatedImagePins: CuratedImagePinService,
private readonly filestoreInstanceResolver: FilestoreInstanceResolver,
) {
super(runnerService, runnerAdapterFactory, boxRepository, redisLockProvider)
}
@WithSpan()
async run(box: Box, lockCode: LockCode): Promise<SyncState> {
switch (box.state) {
case BoxState.UNKNOWN: {
return this.handleRunnerBoxUnknownStateOnDesiredStateStart(box, lockCode)
}
case BoxState.STOPPED: {
return this.handleRunnerBoxStoppedStateOnDesiredStateStart(box, lockCode)
}
case BoxState.RESTORING:
case BoxState.CREATING:
case BoxState.STARTING: {
return this.handleRunnerBoxStartedStateCheck(box, lockCode)
}
case BoxState.ERROR: {
this.logger.error(`Box ${box.id} is in error state on desired state start`)
return DONT_SYNC_AGAIN
}
}
return DONT_SYNC_AGAIN
}
private async handleRunnerBoxUnknownStateOnDesiredStateStart(box: Box, lockCode: LockCode): Promise<SyncState> {
const runner = await this.runnerService.findOneOrFail(box.runnerId)
if (runner.state !== RunnerState.READY) {
return DONT_SYNC_AGAIN
}
if (!box.image) {
await this.updateBoxState(box, BoxState.ERROR, lockCode, undefined, 'Box has no image to create from')
return DONT_SYNC_AGAIN
}
const organization = await this.organizationService.findOne(box.organizationId)
const volumes = await this.resolveBoxVolumes(box)
const metadata = this.volumeMetadata({ ...organization?.boxMetadata }, volumes)
// A tenant's image was pinned by the resolver when the box was created. A
// curated one keeps its tag on the box — the warm pool matches on it — and
// is pinned here instead, to the build this runner already booted.
const image = boxImageIsOrgOwned(box) ? box.image : await this.curatedImagePins.refFor(runner.id, box.image)
const runnerAdapter = await this.runnerAdapterFactory.create(runner)
await runnerAdapter.createBox(
this.withVolumes(box, volumes),
image,
metadata,
await resolveNestedVirtualization(
box.organizationId,
this.organizationService,
runnerAdapter,
this.configService.getOrThrow('runnerHealthTimeout') * 1000,
),
)
await this.updateBoxState(box, BoxState.CREATING, lockCode)
return SYNC_AGAIN
}
private async handleRunnerBoxStoppedStateOnDesiredStateStart(box: Box, lockCode: LockCode): Promise<SyncState> {
const organization = await this.organizationService.findOne(box.organizationId)
// A stopped box restarts on its own runner. Cross-runner recovery is not supported.
if (box.runnerId === null) {
await this.updateBoxState(box, BoxState.ERROR, lockCode, undefined, 'Box has no runner')
return DONT_SYNC_AGAIN
}
const runner = await this.runnerService.findOneOrFail(box.runnerId)
if (runner.state !== RunnerState.READY) {
return DONT_SYNC_AGAIN
}
const runnerAdapter = await this.runnerAdapterFactory.create(runner)
let volumes: Box['volumes']
try {
volumes = await this.resolveBoxVolumes(box)
} catch (error) {
if (error instanceof ServiceUnavailableException) {
this.logger.warn(`Filestore resolution for box ${box.id} is temporarily unavailable; restart will be retried`)
return DONT_SYNC_AGAIN
}
throw error
}
const dispatchBox = this.withVolumes(box, volumes)
const metadata = this.volumeMetadata({ ...organization?.boxMetadata }, volumes)
try {
await runnerAdapter.assertFilestoreVolumeSupport(dispatchBox)
} catch (error) {
if ((error as { code?: string })?.code === 'FILESTORE_CAPABILITY_MISSING') {
this.logger.warn(`Filestore capability for box ${box.id} is not currently available; restart will be retried`)
return DONT_SYNC_AGAIN
}
throw error
}
await runnerAdapter.startBox(dispatchBox.id, dispatchBox.authToken, metadata)
await this.updateBoxState(box, BoxState.STARTING, lockCode)
return SYNC_AGAIN
}
private async resolveBoxVolumes(box: Box): Promise<Box['volumes']> {
if (!box.volumes?.length) return box.volumes
return Promise.all(
box.volumes.map(async (volume) => {
const dispatch = boxVolumeForDispatch(volume)
if (!volume.filestore) return dispatch
const target = await this.filestoreInstanceResolver.resolve(volume.filestore.instance, 'configured')
return {
...dispatch,
filestore: { instance: volume.filestore.instance, ...target },
}
}),
)
}
private withVolumes(box: Box, volumes: Box['volumes']): Box {
if (!box.volumes?.length) return box
return Object.assign(Object.create(Object.getPrototypeOf(box)) as Box, box, { volumes })
}
private volumeMetadata(metadata: Record<string, string>, volumes: Box['volumes']): Record<string, string> {
if (!volumes?.length) return metadata
return { ...metadata, volumes: JSON.stringify(volumes) }
}
// used to check if box is started on runner and update box state accordingly
// also used to handle the case where a box is started on a runner and then transferred to a new runner
private async handleRunnerBoxStartedStateCheck(box: Box, lockCode: LockCode): Promise<SyncState> {
// edge case when box is being transferred to a new runner
if (!box.runnerId) {
return SYNC_AGAIN
}
const runner = await this.runnerService.findOneOrFail(box.runnerId)
const runnerAdapter = await this.runnerAdapterFactory.create(runner)
const boxInfo = await runnerAdapter.boxInfo(box.id)
switch (boxInfo.state) {
case BoxState.STARTED: {
await this.updateBoxState(box, BoxState.STARTED, lockCode, undefined, undefined, boxInfo.daemonVersion)
// if box was transferred to a new runner, remove it from the old runner
if (box.prevRunnerId) {
await this.removeBoxFromPreviousRunner(box)
}
return DONT_SYNC_AGAIN
}
case BoxState.STARTING:
if (await this.checkTimeoutError(box, 5, 'Timeout while starting box')) {
return DONT_SYNC_AGAIN
}
break
case BoxState.RESTORING:
if (await this.checkTimeoutError(box, 30, 'Timeout while starting box')) {
return DONT_SYNC_AGAIN
}
break
case BoxState.CREATING: {
if (await this.checkTimeoutError(box, 15, 'Timeout while creating box')) {
return DONT_SYNC_AGAIN
}
break
}
case BoxState.UNKNOWN: {
await this.updateBoxState(box, BoxState.UNKNOWN, lockCode)
break
}
case BoxState.ERROR: {
await this.updateBoxState(
box,
BoxState.ERROR,
lockCode,
undefined,
'Box entered error state on runner during startup wait loop',
)
break
}
case BoxState.DESTROYED: {
this.logger.warn(
`Box ${box.id} is in destroyed state while starting on runner ${box.runnerId}, prev runner ${box.prevRunnerId}`,
)
await this.checkTimeoutError(box, 15, 'Timeout while starting box: Box is in unknown state on runner')
return DONT_SYNC_AGAIN
}
// also any other state that is not STARTED
default: {
this.logger.error(`Box ${box.id} is in unexpected state ${boxInfo.state}`)
await this.updateBoxState(
box,
BoxState.ERROR,
lockCode,
undefined,
`Box is in unexpected state: ${boxInfo.state}`,
)
break
}
}
return SYNC_AGAIN
}
private async checkTimeoutError(box: Box, timeoutMinutes: number, errorReason: string): Promise<boolean> {
const lastActivityAt = await this.boxActivityService.getLastActivityAt(box.id)
if (lastActivityAt && lastActivityAt.getTime() < Date.now() - 1000 * 60 * timeoutMinutes) {
const updateData: Partial<Box> = {
state: BoxState.ERROR,
errorReason,
recoverable: false,
}
await this.boxRepository.update(box.id, { updateData, entity: box })
return true
}
return false
}
private async removeBoxFromPreviousRunner(box: Box): Promise<void> {
const runner = await this.runnerService.findOne(box.prevRunnerId)
if (!runner) {
this.logger.warn(`Previously assigned runner ${box.prevRunnerId} for box ${box.id} not found`)
await this.boxRepository.update(box.id, { updateData: { prevRunnerId: null } }, true)
return
}
const runnerAdapter = await this.runnerAdapterFactory.create(runner)
try {
// First try to destroy the box
await runnerAdapter.destroyBox(box.id)
} catch (error) {
if (error.response?.status !== 404 && error.statusCode !== 404) {
this.logger.error(`Failed to cleanup box ${box.id} on previous runner ${runner.id}:`, error)
throw error
}
}
await this.boxRepository.update(box.id, { updateData: { prevRunnerId: null } }, true)
}
}