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 {
Check,
Column,
CreateDateColumn,
Entity,
Index,
PrimaryColumn,
OneToOne,
Unique,
UpdateDateColumn,
} from 'typeorm'
import { BoxState } from '../enums/box-state.enum'
import { BoxDesiredState } from '../enums/box-desired-state.enum'
import { BoxClass } from '../enums/box-class.enum'
import { BoxSecret, BoxVolume } from '../dto/box.dto'
import { nanoid } from 'nanoid'
import { BoxLastActivity } from './box-last-activity.entity'
import { BOX_ID_LENGTH, BOX_ID_REGEX, generateBoxId } from '../utils/box-id.util'
import {
AUTO_DELETE_DISABLED,
DEFAULT_AUTO_STOP_SECONDS,
DEFAULT_AUTO_RESUME,
} from '../constants/box-lifecycle.constants'
@Entity('box')
@Unique(['organizationId', 'name'])
@Index('box_state_idx', ['state'])
@Index('box_desiredstate_idx', ['desiredState'])
@Index('box_runnerid_idx', ['runnerId'])
@Index('box_runner_state_idx', ['runnerId', 'state'])
@Index('box_organizationid_idx', ['organizationId'])
@Index('box_creation_quota_idx', { synchronize: false })
@Index('box_app_environment_idx', ['organizationId', 'appId', 'environmentId'])
@Check(
'box_app_owner',
'(app_id IS NULL AND environment_id IS NULL) OR (app_id IS NOT NULL AND environment_id IS NOT NULL)',
)
@Index('box_region_idx', ['region'])
@Index('box_resources_idx', ['cpu', 'mem', 'disk', 'gpu'])
@Index('box_runner_state_desired_idx', ['runnerId', 'state', 'desiredState'], {
where: '"pending" = false',
})
@Index('box_active_only_idx', ['id'], {
where: `"state" <> ALL (ARRAY['destroyed'::box_state_enum, 'archived'::box_state_enum])`,
})
@Index('box_pending_idx', ['id'], {
where: `"pending" = true`,
})
@Index('idx_box_authtoken', ['authToken'])
@Index('box_image_idx', ['image'])
@Index('box_labels_gin_full_idx', { synchronize: false })
@Index('idx_box_volumes_gin', { synchronize: false })
export class Box {
@PrimaryColumn({ type: 'character varying', length: BOX_ID_LENGTH })
id: string
@Column({
type: 'uuid',
})
organizationId: string
@Column({ name: 'app_id', type: 'text', nullable: true })
appId: string | null = null
@Column({ name: 'environment_id', type: 'text', nullable: true })
environmentId: string | null = null
@Column()
name: string
@Column()
region: string
@Column({ nullable: true })
image?: string
/**
* Whether this box's image is the organization's rather than the operator's
* curated set, decided once when the box was created.
*
* Recorded instead of recomputed because the curated set is env-driven and
* mutable: `isCuratedSelector` answers against the set as it is *now*, so an
* operator who rotates a curated reference turns every box already running
* the old one into a box whose image reads as tenant-owned. `box.image` is
* fixed at create and never rewritten, so the answer has to be fixed with it.
*
* Null on rows that predate the column. Recomputing is still the best answer
* available for those, which is what the reader does.
*/
@Column({ type: 'boolean', nullable: true })
imageIsOrgOwned?: boolean | null
@Column({
type: 'uuid',
nullable: true,
})
runnerId?: string
// this is the runnerId of the runner that was previously assigned to the box
// if something goes wrong with new runner assignment, we can revert to the previous runner
@Column({
type: 'uuid',
nullable: true,
})
prevRunnerId?: string
@Column({
type: 'enum',
enum: BoxClass,
default: BoxClass.SMALL,
})
class = BoxClass.SMALL
@Column({
type: 'enum',
enum: BoxState,
default: BoxState.UNKNOWN,
})
state = BoxState.UNKNOWN
@Column({
type: 'enum',
enum: BoxDesiredState,
default: BoxDesiredState.STARTED,
})
desiredState = BoxDesiredState.STARTED
// Daytona-era provisioning label. It only ever surfaced as a
// DAYTONA_SANDBOX_USER env var for a computer-use daemon that never read it;
// today it keys the warm pool (see warm_pool_find_idx) and is echoed back as
// `user` by box.dto.ts. It is NOT the container's process user — that is
// `runAsUser` below.
@Column()
osUser: string
// OCI process.user override (docker `-u`), resolved inside the guest against
// the container image's /etc/passwd. Null leaves the image's USER directive
// in force, which is the default the runner has had since 16d9248bb
// ("VM provides isolation, run as root").
@Column({ nullable: true })
runAsUser?: string
@Column({ default: false })
privileged: boolean
@Column({ nullable: true })
workingDir?: string
@Column({ type: 'jsonb', nullable: true })
entrypoint?: string[]
@Column({ type: 'jsonb', nullable: true })
cmd?: string[]
@Column({ nullable: true })
errorReason?: string
@Column({ default: false, type: 'boolean' })
recoverable = false
@Column({
type: 'jsonb',
default: {},
})
env: { [key: string]: string } = {}
// Network policy, one flag per direction; true is the `/v1` NetworkSpec's
// `enabled`. Inbound enabled lets the proxy serve the box's exposed services
// subject to each tunnel's access policy; it never affects the terminal or the API.
@Column({ type: 'boolean', default: false })
inboundEnabled = false
@Column({ type: 'boolean', default: true })
outboundEnabled = true
// Egress allowlist entries (hosts, wildcards, IPs, CIDRs). Null means full
// egress while outbound is enabled; it is irrelevant otherwise.
@Column({ type: 'text', array: true, nullable: true })
outboundAllowNet?: string[] | null
@Column('jsonb', { nullable: true })
labels: { [key: string]: string }
@Column({ type: 'double precision', default: 2 })
cpu = 2
@Column({ type: 'int', default: 0 })
gpu = 0
@Column({ type: 'int', default: 4 })
mem = 4
// Authoritative for new writes; mem remains the rounded legacy GiB projection.
@Column({ name: 'memory_mib', type: 'int', nullable: true })
memoryMib?: number | null
@Column({ type: 'int', default: 10 })
disk = 10
@Column({
type: 'jsonb',
default: [],
})
volumes: BoxVolume[] = []
@Column({
type: 'jsonb',
default: [],
})
secrets: BoxSecret[] = []
@CreateDateColumn({
type: 'timestamp with time zone',
})
createdAt: Date
@UpdateDateColumn({
type: 'timestamp with time zone',
})
updatedAt: Date
@OneToOne(() => BoxLastActivity, (lastActivity) => lastActivity.box)
lastActivityAt?: BoxLastActivity
@Column({ default: DEFAULT_AUTO_STOP_SECONDS, type: 'int' })
autoStop: number = DEFAULT_AUTO_STOP_SECONDS
@Column({ default: AUTO_DELETE_DISABLED, type: 'int' })
autoDelete: number = AUTO_DELETE_DISABLED
@Column({ default: DEFAULT_AUTO_RESUME, type: 'boolean' })
autoResume: boolean = DEFAULT_AUTO_RESUME
@Column({ default: false, type: 'boolean' })
pending: boolean | undefined = false
@Column({ type: 'character varying' })
authToken = nanoid(32).toLowerCase()
@Column({ nullable: true })
daemonVersion?: string
constructor(region: string, name?: string) {
this.id = generateBoxId()
// Set name - use provided name or fallback to ID
this.name = name || this.id
this.region = region
}
/**
* Helper method that returns the update data needed for a soft delete operation.
*/
static getSoftDeleteUpdate(box: Box): Partial<Box> {
return {
pending: true,
desiredState: BoxDesiredState.DESTROYED,
name: 'DESTROYED_' + box.name + '_' + Date.now(),
}
}
/**
* Asserts that the current entity state is valid.
*/
assertValid(): void {
this.validateBoxId()
this.validateDesiredStateTransition()
}
private validateBoxId(): void {
if (!BOX_ID_REGEX.test(this.id)) {
throw new Error(`Box has invalid id ${this.id}`)
}
}
private validateDesiredStateTransition(): void {
switch (this.desiredState) {
case BoxDesiredState.STARTED:
if (
[
BoxState.STARTED,
BoxState.STOPPED,
BoxState.STARTING,
BoxState.CREATING,
BoxState.UNKNOWN,
BoxState.RESTORING,
BoxState.ERROR,
BoxState.RESIZING,
].includes(this.state)
) {
break
}
throw new Error(`Box ${this.id} is not in a valid state to be started. State: ${this.state}`)
case BoxDesiredState.STOPPED:
if (
[
BoxState.STARTED,
BoxState.STOPPING,
BoxState.STOPPED,
BoxState.RESTORING,
BoxState.ERROR,
BoxState.RESIZING,
].includes(this.state)
) {
break
}
throw new Error(`Box ${this.id} is not in a valid state to be stopped. State: ${this.state}`)
case BoxDesiredState.DESTROYED:
if (
[
BoxState.DESTROYED,
BoxState.DESTROYING,
BoxState.STOPPED,
BoxState.STARTED,
BoxState.ARCHIVED,
BoxState.ERROR,
BoxState.ARCHIVING,
].includes(this.state)
) {
break
}
throw new Error(`Box ${this.id} is not in a valid state to be destroyed. State: ${this.state}`)
}
}
/**
* Enforces domain invariants on the current entity state.
*
* @returns Additional field changes that invariant enforcement produced.
*/
enforceInvariants(): Partial<Box> {
const changes = this.getInvariantChanges()
Object.assign(this, changes)
return changes
}
private getInvariantChanges(): Partial<Box> {
const changes: Partial<Box> = {}
if (!this.pending && String(this.state) !== String(this.desiredState)) {
changes.pending = true
}
if (this.pending && String(this.state) === String(this.desiredState)) {
changes.pending = false
}
if (this.state === BoxState.ERROR && this.desiredState !== BoxDesiredState.DESTROYED) {
changes.pending = false
}
if (this.state === BoxState.DESTROYED || this.state === BoxState.ARCHIVED) {
changes.runnerId = null
}
return changes
}
/**
* Drops the secrets field whenever the entity is serialized with
* JSON.stringify — analytics debug logs and the Redis box-event channel both
* do this. Secrets are plaintext in this column by design (POL-303), so the
* field must never ride a JSON dump out of the API process, not even as a
* count placeholder: consumers of the event channel expect the key to be
* absent.
*/
toJSON() {
const { secrets, ...rest } = this
return rest
}
}