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.
60 lines
2.2 KiB
Go
60 lines
2.2 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
v1 "github.com/ai-openshell/sdk-go/openshell/v1"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/openshell"
|
|
"github.com/samcharles93/archie-core/internal/secret"
|
|
)
|
|
|
|
// startOpenShell connects to the OpenShell gateway task sandboxes run on and
|
|
// removes the sandboxes a previous daemon left. A gateway that is down only
|
|
// degrades: the runtime is returned, and each task parks on its failed
|
|
// acquire until the gateway answers.
|
|
func startOpenShell(ctx context.Context, cfgHolder *config.Holder, secrets *secret.Registry, log *slog.Logger) (*openshell.Runtime, func(), error) {
|
|
cfg := cfgHolder.Get()
|
|
settings := cfg.Containers.OpenShell
|
|
auth := v1.NoAuth()
|
|
if settings.Token != (config.SecretRef{}) {
|
|
if secrets == nil {
|
|
return nil, nil, fmt.Errorf("openshell gateway token: no secret engine is available")
|
|
}
|
|
token, err := secrets.Resolve(settings.Token)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("openshell gateway token: %w", err)
|
|
}
|
|
auth = v1.StaticToken(token)
|
|
}
|
|
client, err := v1.NewClient(v1.Config{Address: settings.Address, Auth: auth})
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("openshell gateway %s: %w", settings.Address, err)
|
|
}
|
|
rt := openshell.New(client, openshell.Options{
|
|
Workspace: settings.Workspace,
|
|
Instance: cfg.Instance,
|
|
ServicesHost: settings.ServicesHost,
|
|
MaxUptime: func() time.Duration { return cfgHolder.Get().Containers.MaxUptime.Std() },
|
|
Log: log,
|
|
})
|
|
checkCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
|
defer cancel()
|
|
if health, err := client.Health().Check(checkCtx); err != nil || !health.Healthy {
|
|
log.WarnContext(ctx, "openshell gateway unhealthy; tasks park until it answers", "gateway", settings.Address, "err", err)
|
|
} else if err := rt.Sweep(checkCtx); err != nil {
|
|
log.WarnContext(ctx, "openshell: orphaned sandboxes remain", "err", err)
|
|
}
|
|
closeClient := func() {
|
|
if err := client.Close(); err != nil {
|
|
log.WarnContext(ctx, "openshell client close failed", "err", err)
|
|
}
|
|
}
|
|
log.InfoContext(ctx, "task sandboxes run on openshell", "gateway", settings.Address, "workspace", settings.Workspace)
|
|
return rt, closeClient, nil
|
|
}
|