Files
archie/internal/infrastructure/openshell/runtime.go
T
sam a1ec538a87 feat(openshell): task agents can run as OpenShell sandboxes (#1864)
Container runtime policies gain a runtime (docker or openshell) and the
OpenShell gateway's address, workspace and token, set on the container
runtime page and applied on restart. The OpenShell backend creates a
sandbox per task with the worktree and volumes bound in, reports when
the agent exits or outlives its maximum uptime, deletes the sandbox on
release, and sweeps this instance's leftovers at boot. Storage volumes
are created attachable to OpenShell sandboxes.
2026-11-14 03:07:57 +11:00

293 lines
9.1 KiB
Go

// Package openshell runs task agents as NVIDIA OpenShell sandboxes. A sandbox
// has no network of its own: every connection it makes passes the gateway's
// policy, and credentials reach it only as placeholders.
package openshell
import (
"context"
"errors"
"fmt"
"log/slog"
"slices"
"strings"
"sync"
"time"
v1 "github.com/ai-openshell/sdk-go/openshell/v1"
"github.com/samcharles93/archie-core/internal/storage"
)
// instanceLabel marks the sandboxes one archie instance owns; its value is
// the instance name, so instances sharing a gateway never remove each
// other's.
const instanceLabel = "archie.instance"
// agentCommand is the sandbox's main process. The image's own entrypoint is
// replaced by OpenShell's, so the agent is named here.
var agentCommand = []string{"archie-agent"}
// writable are container paths tools write to inside the sandbox. They are
// the container's own layer, so writing them exposes nothing on the host.
var writable = []string{"/tmp", "/var/tmp", "/root", "/home"}
// readOnly are the system paths the agent and its tools read.
var readOnly = []string{"/usr", "/lib", "/lib64", "/bin", "/sbin", "/etc", "/opt"}
// Options configure a Runtime.
type Options struct {
// Workspace is the gateway workspace; empty means "default".
Workspace string
// Instance names this archie instance. It prefixes sandbox names and is
// the instance label's value.
Instance string
// MaxUptime returns the current lifetime cap; zero or a nil func means
// none. It is read per acquire, so a stored change reaches the next task.
MaxUptime func() time.Duration
// Poll is how often a running sandbox is checked for an exit; zero means
// two seconds.
Poll time.Duration
Log *slog.Logger
}
// Runtime creates, watches and removes task sandboxes on one gateway.
type Runtime struct {
client v1.ClientInterface
workspace string
instance string
maxUptime func() time.Duration
poll time.Duration
log *slog.Logger
}
// New returns a Runtime on client.
func New(client v1.ClientInterface, opts Options) *Runtime {
r := &Runtime{
client: client,
workspace: opts.Workspace,
instance: opts.Instance,
maxUptime: opts.MaxUptime,
poll: opts.Poll,
log: opts.Log,
}
if r.workspace == "" {
r.workspace = "default"
}
if r.poll <= 0 {
r.poll = 2 * time.Second
}
if r.maxUptime == nil {
r.maxUptime = func() time.Duration { return 0 }
}
if r.log == nil {
r.log = slog.New(slog.DiscardHandler)
}
return r
}
// Sandbox is one task's running sandbox.
type Sandbox struct {
name string
exited chan struct{}
// detail is written before exited closes, so a reader that saw the close
// may read it without a lock.
detail string
once sync.Once
stop context.CancelFunc
done chan struct{}
}
// Exited closes when the agent stops running for any reason: it exited, the
// sandbox failed or was removed, or it reached its maximum uptime.
func (s *Sandbox) Exited() <-chan struct{} { return s.exited }
// ExitDetail says how the sandbox ended. It is empty until Exited closes.
func (s *Sandbox) ExitDetail() string {
select {
case <-s.exited:
return s.detail
default:
return ""
}
}
// Acquire creates a sandbox running the agent from image, with mounts bound
// in and env as its environment, and waits until it is ready.
func (r *Runtime) Acquire(ctx context.Context, image string, mounts []storage.Mount, env []string) (*Sandbox, error) {
name := fmt.Sprintf("%s-agent-%d", r.instance, time.Now().UnixNano())
spec, err := r.spec(image, mounts, env)
if err != nil {
return nil, err
}
if _, err := r.client.Sandboxes().Create(ctx, r.workspace, name, spec); err != nil {
return nil, fmt.Errorf("create sandbox %s: %w", name, err)
}
if _, err := r.client.Sandboxes().WaitReady(ctx, r.workspace, name); err != nil {
r.remove(ctx, name)
return nil, fmt.Errorf("sandbox %s not ready: %w", name, err)
}
watchCtx, stop := context.WithCancel(context.WithoutCancel(ctx))
s := &Sandbox{name: name, exited: make(chan struct{}), stop: stop, done: make(chan struct{})}
go r.watch(watchCtx, s, r.maxUptime())
r.log.InfoContext(ctx, "sandbox started", "sandbox", name)
return s, nil
}
// Release stops watching s and removes its sandbox.
func (r *Runtime) Release(ctx context.Context, s *Sandbox) {
if s == nil {
return
}
s.stop()
<-s.done
r.remove(ctx, s.name)
r.log.InfoContext(ctx, "sandbox released", "sandbox", s.name)
}
// Reown is not available: the daemon cannot run a privileged container on
// the gateway to chown files back.
func (r *Runtime) Reown(context.Context, string, int, int) error {
return errors.New("the openshell runtime cannot repair file ownership; the sandbox user must match the daemon's")
}
// Sweep removes the sandboxes a previous daemon of this instance left behind.
// Work interrupted by a restart is requeued, so none of them is still needed.
func (r *Runtime) Sweep(ctx context.Context) error {
pager, err := r.client.Sandboxes().List(r.workspace, v1.ListOptions{})
if err != nil {
return fmt.Errorf("list sandboxes: %w", err)
}
all, err := pager.All(ctx)
if err != nil {
return fmt.Errorf("list sandboxes: %w", err)
}
var errs []error
for _, sb := range all {
if sb.Spec.Template == nil || sb.Spec.Template.Labels[instanceLabel] != r.instance {
continue
}
r.log.InfoContext(ctx, "removing orphaned sandbox", "sandbox", sb.Name)
if _, err := r.client.Sandboxes().Delete(ctx, r.workspace, sb.Name); err != nil && !v1.IsNotFound(err) {
errs = append(errs, fmt.Errorf("remove sandbox %s: %w", sb.Name, err))
}
}
return errors.Join(errs...)
}
// spec is the sandbox a task runs in.
func (r *Runtime) spec(image string, mounts []storage.Mount, env []string) (*v1.SandboxSpec, error) {
environment := make(map[string]string, len(env))
for _, entry := range env {
key, value, ok := strings.Cut(entry, "=")
if !ok || key == "" {
return nil, fmt.Errorf("environment entry %q is not KEY=value", entry)
}
environment[key] = value
}
readWrite := slices.Clone(writable)
var driverMounts []any
for _, m := range mounts {
mount := map[string]any{"type": m.Type, "source": m.Source, "target": m.Destination, "read_only": m.ReadOnly}
if m.Type == storage.MountTypeBind {
// The same relabelling the Docker pool applies; Docker ignores
// it on a host without SELinux.
mount["selinux_label"] = "private"
if m.Shared {
mount["selinux_label"] = "shared"
}
}
driverMounts = append(driverMounts, mount)
if !m.ReadOnly {
readWrite = append(readWrite, m.Destination)
}
}
return &v1.SandboxSpec{
Template: &v1.SandboxTemplate{
Image: image,
Labels: map[string]string{instanceLabel: r.instance},
DriverConfig: map[string]any{"docker": map[string]any{"mounts": driverMounts}},
},
Environment: environment,
Command: slices.Clone(agentCommand),
Policy: &v1.SandboxPolicy{
Filesystem: &v1.FilesystemPolicy{IncludeWorkdir: true, ReadOnly: slices.Clone(readOnly), ReadWrite: readWrite},
},
}, nil
}
// watch closes s.exited once the sandbox stops running or outlives maxUptime.
// A failed status read says nothing about the sandbox, so it never ends the
// watch: a gateway hiccup must not abort a live run.
func (r *Runtime) watch(ctx context.Context, s *Sandbox, maxUptime time.Duration) {
defer close(s.done)
var deadline <-chan time.Time
if maxUptime > 0 {
timer := time.NewTimer(maxUptime)
defer timer.Stop()
deadline = timer.C
}
ticker := time.NewTicker(r.poll)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-deadline:
r.log.WarnContext(ctx, "sandbox reached its maximum uptime", "sandbox", s.name, "max_uptime", maxUptime)
r.remove(ctx, s.name)
r.finish(s, fmt.Sprintf("killed at its maximum uptime of %s", maxUptime))
return
case <-ticker.C:
}
sb, err := r.client.Sandboxes().Get(ctx, r.workspace, s.name)
if v1.IsNotFound(err) {
r.finish(s, "sandbox was removed")
return
}
if err != nil {
continue
}
if detail, ended := exitDetail(sb); ended {
r.finish(s, detail)
return
}
}
}
// finish records how s ended and closes Exited, once.
func (r *Runtime) finish(s *Sandbox, detail string) {
s.once.Do(func() {
s.detail = detail
close(s.exited)
})
}
// exitDetail reports whether sb has stopped running and, if so, how.
func exitDetail(sb *v1.Sandbox) (string, bool) {
st := sb.Status
if st.ExitCode == nil && st.Phase != v1.SandboxError && st.Phase != v1.SandboxCompleted && st.Phase != v1.SandboxStopped {
return "", false
}
detail := fmt.Sprintf("sandbox %s", st.Phase)
if st.ExitCode != nil {
detail = fmt.Sprintf("exit code %d", *st.ExitCode)
}
for _, c := range st.Conditions {
if c.Type == "Ready" && c.Message != "" {
detail += ": " + c.Message
break
}
}
return detail, true
}
// remove deletes a sandbox with a fresh deadline, so a cancelled task still
// cleans up. A sandbox already gone is not an error.
func (r *Runtime) remove(ctx context.Context, name string) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
defer cancel()
if _, err := r.client.Sandboxes().Delete(ctx, r.workspace, name); err != nil && !v1.IsNotFound(err) {
r.log.WarnContext(ctx, "sandbox remove failed", "sandbox", name, "err", err)
}
}