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
package storage
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// s3URIScheme prefixes an arcPath that names its own bucket.
const s3URIScheme = "s3://"
// ArchiveStore transfers portable .boxlite archives between runners and object
// storage. The caller owns archive retention and removal.
//
// The arcPath addressing every object is chosen by the control plane and travels
// in the job payload, so a redelivered job acts on the object its first delivery
// did — and on the object the control plane recorded for the box.
type ArchiveStore interface {
Upload(ctx context.Context, arcPath, localPath string) error
Download(ctx context.Context, arcPath, localPath string) error
Remove(ctx context.Context, arcPath string) error
}
// SchemeArchiveStore hands each arcPath to the store its scheme names: gs:// to
// Cloud Storage, s3:// or a bare key to S3. Either store may be absent — a GCP
// runner has no S3 bucket — and an arcPath that needs the absent one fails its
// job with the reason instead of the runner refusing to start.
type SchemeArchiveStore struct {
GCS ArchiveStore
S3 ArchiveStore
}
func (s *SchemeArchiveStore) storeFor(arcPath string) (ArchiveStore, error) {
if strings.HasPrefix(arcPath, gcsURIScheme) {
if _, _, err := parseGCSURI(arcPath); err != nil {
return nil, err
}
if s.GCS == nil {
return nil, fmt.Errorf("no Cloud Storage archive store on this runner for %q", arcPath)
}
return s.GCS, nil
}
if arcPath == "" {
return nil, fmt.Errorf("arcPath must not be empty")
}
if strings.HasPrefix(arcPath, s3URIScheme) {
bucket, key, found := strings.Cut(strings.TrimPrefix(arcPath, s3URIScheme), "/")
if !found || bucket == "" || key == "" {
return nil, fmt.Errorf("invalid arcPath %q: want s3://<bucket>/<key>", arcPath)
}
} else if strings.Contains(arcPath, "://") {
return nil, fmt.Errorf("unsupported archive scheme in %q", arcPath)
}
if s.S3 == nil {
return nil, fmt.Errorf("S3 archive store is not configured: set AWS_DEFAULT_BUCKET and the AWS credentials on this runner")
}
return s.S3, nil
}
// Check reports why arcPath cannot be served, if its backend is missing, so a
// job can refuse before it does any work.
func (s *SchemeArchiveStore) Check(arcPath string) error {
_, err := s.storeFor(arcPath)
return err
}
func (s *SchemeArchiveStore) Upload(ctx context.Context, arcPath, localPath string) error {
store, err := s.storeFor(arcPath)
if err != nil {
return err
}
return store.Upload(ctx, arcPath, localPath)
}
func (s *SchemeArchiveStore) Download(ctx context.Context, arcPath, localPath string) error {
store, err := s.storeFor(arcPath)
if err != nil {
return err
}
return store.Download(ctx, arcPath, localPath)
}
func (s *SchemeArchiveStore) Remove(ctx context.Context, arcPath string) error {
store, err := s.storeFor(arcPath)
if err != nil {
return err
}
return store.Remove(ctx, arcPath)
}
// S3ArchiveStore keeps migration archives as objects in a single S3 bucket.
type S3ArchiveStore struct {
client *minio.Client
bucket string
}
// S3ArchiveStoreConfig carries the runner's own S3 credentials — the archive is
// infrastructure state moved between two runners, not organization-scoped user
// data, so it does not go through the control plane's push-access grant.
type S3ArchiveStoreConfig struct {
EndpointUrl string
Region string
AccessKeyId string
SecretAccessKey string
Bucket string
}
func NewS3ArchiveStore(cfg S3ArchiveStoreConfig) (*S3ArchiveStore, error) {
if cfg.Bucket == "" {
return nil, fmt.Errorf("migration archive bucket is required (AWS_DEFAULT_BUCKET)")
}
endpoint, secure, err := s3Endpoint(cfg.EndpointUrl, cfg.Region)
if err != nil {
return nil, err
}
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(cfg.AccessKeyId, cfg.SecretAccessKey, ""),
Secure: secure,
Region: cfg.Region,
})
if err != nil {
return nil, fmt.Errorf("failed to create object store client for %s: %w", endpoint, err)
}
return &S3ArchiveStore{client: client, bucket: cfg.Bucket}, nil
}
// s3Endpoint resolves the host[:port] minio-go expects, plus whether to dial
// TLS. An explicit AWS_ENDPOINT_URL wins (minio in development, a VPC endpoint
// in production); with none set the regional AWS host is derived.
func s3Endpoint(endpointUrl, region string) (string, bool, error) {
if endpointUrl == "" {
if region == "" {
return "", false, fmt.Errorf("either AWS_ENDPOINT_URL or AWS_REGION must be set to reach the object store")
}
return fmt.Sprintf("s3.%s.amazonaws.com", region), true, nil
}
parsed, err := url.Parse(endpointUrl)
if err != nil {
return "", false, fmt.Errorf("invalid AWS_ENDPOINT_URL %q: %w", endpointUrl, err)
}
if parsed.Host == "" {
return "", false, fmt.Errorf("invalid AWS_ENDPOINT_URL %q: no host", endpointUrl)
}
return parsed.Host, parsed.Scheme == "https", nil
}
func (s *S3ArchiveStore) Upload(ctx context.Context, arcPath, localPath string) error {
bucket, key, err := s.resolveArcPath(arcPath)
if err != nil {
return err
}
if _, err := s.client.FPutObject(ctx, bucket, key, localPath, minio.PutObjectOptions{}); err != nil {
return fmt.Errorf("failed to upload %s to s3://%s/%s: %w", localPath, bucket, key, err)
}
return nil
}
func (s *S3ArchiveStore) Download(ctx context.Context, arcPath, localPath string) error {
bucket, key, err := s.resolveArcPath(arcPath)
if err != nil {
return err
}
if err := s.client.FGetObject(ctx, bucket, key, localPath, minio.GetObjectOptions{}); err != nil {
return fmt.Errorf("failed to download s3://%s/%s to %s: %w", bucket, key, localPath, err)
}
return nil
}
// Remove deletes the archive object. S3 deletes are idempotent — removing a key
// that is already gone reports success — which is what a redelivered rollback or
// finish job needs to converge.
func (s *S3ArchiveStore) Remove(ctx context.Context, arcPath string) error {
bucket, key, err := s.resolveArcPath(arcPath)
if err != nil {
return err
}
if err := s.client.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}); err != nil {
return fmt.Errorf("failed to remove s3://%s/%s: %w", bucket, key, err)
}
return nil
}
// resolveArcPath splits the control plane's arcPath into the bucket and key to
// act on. A bare key goes to the bucket this runner is configured with; an
// s3://<bucket>/<key> URI lets the control plane name a bucket the runner does
// not have configured, which is what lets one migration span two runners whose
// own buckets differ.
func (s *S3ArchiveStore) resolveArcPath(arcPath string) (string, string, error) {
if arcPath == "" {
return "", "", fmt.Errorf("arcPath must not be empty")
}
if !strings.HasPrefix(arcPath, s3URIScheme) {
return s.bucket, arcPath, nil
}
bucket, key, found := strings.Cut(strings.TrimPrefix(arcPath, s3URIScheme), "/")
if !found || bucket == "" || key == "" {
return "", "", fmt.Errorf("invalid arcPath %q: want s3://<bucket>/<key>", arcPath)
}
return bucket, key, nil
}