Files
sam 2096ae8267 feat(openshell): each sandbox's network policy admits only what its task needs (#1864, #1967)
Runtimes now take a task launch: image, mounts, env, the daemon's NATS
and State Store with the task's credentials, its model providers and the
hosts its tools may reach. Docker addresses them through the egress relay
as before. An OpenShell sandbox reaches NATS and the State Store over
plain TCP, the model providers with their credential bound, and the
task's remote MCP hosts and the allowed package registries; services
addressed by IP or loopback are named by the configured services host,
since sandbox policy admits only DNS names.
2026-11-14 03:24:37 +11:00

122 lines
4.1 KiB
Go

package daemon
import (
"context"
"github.com/samcharles93/archie-core/internal/container"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
"github.com/samcharles93/archie-core/internal/infrastructure/openshell"
"github.com/samcharles93/archie-core/internal/storage"
)
// TaskRuntime starts and stops the isolated environment a task's agent runs
// in. The Docker container pool is one implementation.
type TaskRuntime interface {
// Acquire starts the agent l describes.
Acquire(ctx context.Context, l TaskLaunch) (TaskContainer, error)
// Release stops and removes a container Acquire returned.
Release(ctx context.Context, c TaskContainer)
// Reown hands dir back to uid:gid when a killed container left files the
// daemon cannot touch.
Reown(ctx context.Context, dir string, uid, gid int) error
}
// TaskLaunch is one task's agent: what it runs, and what it may reach. Each
// runtime turns the endpoints into the addresses its sandbox can use.
type TaskLaunch struct {
Image string
Mounts []storage.Mount
// Env is the agent's environment apart from its network endpoints.
Env []string
// Worker is NATS and the State Store as the daemon reaches them, with the
// task's credentials.
Worker egress.Endpoints
// Providers are the gateway providers the agent's model calls use.
Providers []ModelProvider
// Hosts are further hosts, "host" or "host:port", the agent's tools may
// reach: its remote MCP servers and the allowed package registries.
Hosts []string
}
// TaskContainer is one running task environment.
type TaskContainer interface {
// Exited closes when the agent stops running for any reason.
Exited() <-chan struct{}
// ExitDetail says how it ended, once Exited has closed.
ExitDetail() string
}
// DockerRuntime runs tasks in pool's containers. A nil pool is no runtime, so
// the daemon parks tasks rather than holding a runtime that cannot start one.
func DockerRuntime(pool *container.Pool) TaskRuntime {
if pool == nil {
return nil
}
return dockerRuntime{pool: pool}
}
type dockerRuntime struct{ pool *container.Pool }
// Acquire starts a container on its sandbox network, where NATS and the State
// Store are the egress relay's ports. Model calls go through the egress proxy
// the session's env names; the container reaches nothing else.
func (r dockerRuntime) Acquire(ctx context.Context, l TaskLaunch) (TaskContainer, error) {
network, err := egress.WorkerEnv(l.Worker)
if err != nil {
return nil, err
}
c, err := r.pool.Acquire(ctx, l.Image, l.Mounts, append(network, l.Env...))
if err != nil {
return nil, err
}
return c, nil
}
// Release releases a container this runtime acquired; any other value has
// nothing of this pool's to release.
func (r dockerRuntime) Release(ctx context.Context, c TaskContainer) {
if ctr, ok := c.(*container.Container); ok {
r.pool.Release(ctx, ctr)
}
}
func (r dockerRuntime) Reown(ctx context.Context, dir string, uid, gid int) error {
return r.pool.Reown(ctx, dir, uid, gid)
}
// OpenShellRuntime runs tasks as rt's OpenShell sandboxes. A nil rt is no
// runtime, as with DockerRuntime.
func OpenShellRuntime(rt *openshell.Runtime) TaskRuntime {
if rt == nil {
return nil
}
return openShellRuntime{rt: rt}
}
type openShellRuntime struct{ rt *openshell.Runtime }
func (r openShellRuntime) Acquire(ctx context.Context, l TaskLaunch) (TaskContainer, error) {
providers := make([]openshell.Provider, 0, len(l.Providers))
for _, p := range l.Providers {
providers = append(providers, openshell.Provider{Gateway: p.Gateway, Host: p.Host, Port: p.Port})
}
sb, err := r.rt.Acquire(ctx, openshell.Launch{
Image: l.Image, Mounts: l.Mounts, Env: l.Env, Worker: l.Worker, Providers: providers, Hosts: l.Hosts,
})
if err != nil {
return nil, err
}
return sb, nil
}
// Release releases a sandbox this runtime acquired.
func (r openShellRuntime) Release(ctx context.Context, c TaskContainer) {
if sb, ok := c.(*openshell.Sandbox); ok {
r.rt.Release(ctx, sb)
}
}
func (r openShellRuntime) Reown(ctx context.Context, dir string, uid, gid int) error {
return r.rt.Reown(ctx, dir, uid, gid)
}