The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
/*
* Copyright 2026 BoxLite AI
* SPDX-License-Identifier: AGPL-3.0
*/
import { Injectable } from '@nestjs/common'
import { InjectDataSource } from '@nestjs/typeorm'
import { DataSource, EntityManager, IsNull } from 'typeorm'
import { Image } from '../entities/image.entity'
import { ImageAlias } from '../entities/image-alias.entity'
import { ImageAliasEvent } from '../entities/image-alias-event.entity'
import { ImageVersion } from '../entities/image-version.entity'
import { ImageSourceKind } from '../enums/image-source-kind.enum'
import { CuratedImagePinService } from './curated-image-pin.service'
import { IMPLICIT_TAG, isSha256Digest, parseImageRef, upstreamRefOf } from '../utils/image-ref.util'
/** What a runner reported about the image a box actually booted from. */
export type ReportedImage = {
/** The registry's digest for the build that was pulled. */
digest: string
/** Declared on-registry size in bytes, as the manifest summed to. */
sizeBytes: number
}
/**
* The image a box booted from, and whose it is.
*
* Ownership travels with the ref rather than being derived from it here. The
* curated set is env-driven and an operator can rotate it, so asking "is this
* curated?" now answers about the set as it is now, not the set the box was
* created against — and the caller is the one holding the answer recorded when
* the box was created.
*/
export type BootedImage = {
/** The ref the box was dispatched with. */
ref: string
/** False for the operator's curated set, which no tenant catalog may hold. */
isOrgOwned: boolean
/** Where it booted, which is whose cache a curated pin describes. */
runnerId?: string
}
/**
* Turns "a box started from this image" into the organization's catalog row.
*
* The catalog is written here and nowhere else, and only from a report: an
* image is in it because it was successfully pulled and booted, so a ref that
* does not exist upstream never leaves a row behind. That also makes this the
* only place that learns a digest — the control plane never contacts a
* registry itself.
*
* Every write is idempotent. A report can arrive more than once for the same
* box, and two boxes can report the same new ref at the same moment; both are
* settled by unique constraints rather than by reading first, so there is no
* window between the check and the insert. The one read that decides — whether
* a new name still fits the organization's image limit — is taken under a lock
* on the organization row.
*/
@Injectable()
export class ImageRegistrarService {
constructor(
@InjectDataSource() private readonly dataSource: DataSource,
private readonly curatedImagePins: CuratedImagePinService,
) {}
/**
* Record what a box booted from.
*
* Curated images stay out of the catalog: they are the operator's, shared by
* every organization, and putting them in a tenant's catalog would count them
* against that tenant's limit and let one tenant delete what everyone uses.
* What is kept for them instead is the build this runner booted, so the next
* curated box it is given reads its cache rather than asking the registry.
*
* Which it is comes from the box rather than from a fresh look at the curated
* set, because that set moves: an operator who rotates a curated reference
* would otherwise have every box still running the old one file an image into
* its organization's catalog, against that organization's limit.
*/
async onBoxStarted(organizationId: string, image: BootedImage, reported: ReportedImage): Promise<void> {
// Refused here rather than by the column, which would take a short bad
// digest without complaint. The reason is downstream: this value becomes a
// `storageRef` or a pinned ref a runner is expected to pull, and a key the
// resolver matches against, so a digest of any other shape is a row that
// can never be used and a ref no registry answers. Refused on its own, too —
// the box is running either way and its state still has to be recorded.
if (!isSha256Digest(reported.digest)) {
throw new Error(`Refusing to record image digest '${reported.digest}': not a sha256 digest`)
}
if (!image.isOrgOwned) {
if (image.runnerId) {
await this.curatedImagePins.remember(image.runnerId, image.ref, reported.digest)
}
return
}
// Filed under the upstream name. A private image boots from a registry
// proxy ref, and a catalog keyed by that would list the proxy as the
// registry and miss every lookup made by the name the tenant uses.
const imageRef = upstreamRefOf(image.ref)
const { host, repository, tag, digest } = parseImageRef(imageRef)
const name = `${host}/${repository}`
await this.dataSource.transaction(async (manager) => {
await this.assertRoomForImage(manager, organizationId, name)
// `lastUsedAt` is what eviction and the usage view read, so the update
// is the point of the upsert for an image that already exists.
const image = await manager
.createQueryBuilder()
.insert()
.into(Image)
.values({ organizationId, name, lastUsedAt: new Date() })
.orUpdate(['lastUsedAt'], ['organizationId', 'name'], {
indexPredicate: '"deletedAt" IS NULL',
})
.returning(['id'])
.execute()
// Both branches of the upsert return the row, and a name held only by a
// soft-deleted image does not conflict at all: the insert succeeds and
// the organization gets a fresh active row. That is what the partial
// index is for — deleting an image is how a tenant picks up a moved tag,
// and using it again brings it back as a new entry.
const imageId: string | undefined = image.raw?.[0]?.id
if (!imageId) {
throw new Error(`Image upsert for '${name}' returned no row`)
}
// The same upsert shape as the image's: a build already recorded keeps
// everything it was recorded with, and only when it was last used moves.
await manager
.createQueryBuilder()
.insert()
.into(ImageVersion)
.values({
imageId,
digest: reported.digest,
sizeBytes: reported.sizeBytes,
sourceKind: ImageSourceKind.PULL,
sourceSpec: { sourceRef: imageRef },
storageRef: `${name}@${reported.digest}`,
lastUsedAt: new Date(),
})
.orUpdate(['lastUsedAt'], ['imageId', 'digest'])
.execute()
// A ref the caller already pinned names no tag to record — it is the
// digest that identifies it. A bare repository does name one: the
// registry served it by fetching `latest`, so that is the tag this box
// booted from, and it has to be recorded under the same name the
// resolver looks it up by or the reference never pins.
const recordedTag = tag ?? IMPLICIT_TAG
if (digest) {
return
}
const version = await manager
.createQueryBuilder(ImageVersion, 'version')
.select('version.id', 'id')
.where('version."imageId" = :imageId', { imageId })
.andWhere('version.digest = :digest', { digest: reported.digest })
.getRawOne<{ id: string }>()
if (!version) {
return
}
// A registry pull seeds a catalog alias only while that name is absent.
// Existing bindings remain pinned even if the upstream tag changes.
// Replacing an alias requires explicit deletion and creation.
const recorded = await manager
.createQueryBuilder()
.insert()
.into(ImageAlias)
.values({ imageId, name: recordedTag, versionId: version.id })
.orIgnore()
.returning(['id'])
.execute()
if (recorded.raw.length === 0) {
return
}
// The alias's first event. No actor: a registry pull recorded it.
await manager.insert(ImageAliasEvent, {
imageId,
aliasName: recordedTag,
fromVersionId: null,
toVersionId: version.id,
actorId: null,
})
})
}
/**
* Refuse a name the organization does not hold once it holds its limit.
*
* Admission counts only images already recorded, so several first pulls in
* flight can each pass it; this count is the one that holds the limit. A
* name already held adds no kind of image, so the common path — a create
* the catalog answered — takes no lock. A new name locks the organization
* row, so two registrations cannot both count under the limit, and looks
* again once it has the lock, in case the other one recorded this name.
* The limit is read by column rather than through the organization entity,
* which this service has no other reason to know.
*/
private async assertRoomForImage(manager: EntityManager, organizationId: string, name: string): Promise<void> {
const isHeld = () => manager.exists(Image, { where: { organizationId, name, deletedAt: IsNull() } })
if (await isHeld()) {
return
}
await manager.query("SET LOCAL lock_timeout = '5s'")
const [organization] = await manager.query(
`SELECT "image_count_limit" AS "limit" FROM "organization" WHERE "id" = $1 FOR UPDATE`,
[organizationId],
)
if (!organization) {
throw new Error(`Organization ${organizationId} not found; not recording image '${name}'`)
}
if (await isHeld()) {
return
}
const held = await manager.count(Image, { where: { organizationId, deletedAt: IsNull() } })
if (held >= organization.limit) {
throw new Error(
`Organization ${organizationId} holds its limit of ${organization.limit} images; not recording '${name}'`,
)
}
}
}