fix(harness): keep the setup container alive with archie's own binary

A setup session started its container with "sleep infinity" and opened the
PTY with "sh -l". Neither binary is guaranteed: the spec capability whose
floor is a shell and bash is com.docker.sandbox/sbx@1, and archie's
supported map does not list it, so a Kit image need not provide either. A
missing sleep killed the container at start and a missing sh failed the
exec, both as an opaque error.

The container now runs archie-agent hold as its entrypoint. The agent
binary is archie's own and already mounted into every Kit, so the
keepalive needs nothing the image may lack and the container env stays
empty. Before opening the PTY, harnesssession probes for bash then sh and
returns ErrNoShell naming the Kit reference when neither is present.

Test: TestPickShell covers bash-preferred, sh-only, neither, and a probe
that cannot run.
This commit is contained in:
sam committed 2026-10-09 13:05:05 +11:00
1 parent d91ab1ab0f
commit 71fa670512
4 files changed
+105 -10

No files matched your search

+14
View File
@@ -19,6 +19,17 @@ import (
type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error
// runHold blocks until signalled and exits 0. It is the keepalive for a Kit
// container that runs no worker: the agent binary is archie's own and is
// mounted into every Kit, so the container needs no binary the image may not
// ship, and holds no environment of its own.
func runHold() int {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done()
return 0
}
func main() {
os.Exit(run())
}
@@ -28,6 +39,9 @@ func run() int {
}
func runCommand(args []string, getenv func(string) string, stderr io.Writer, runWorker workerRunner) int {
if len(args) > 0 && args[0] == "hold" {
return runHold()
}
if len(args) > 0 && args[0] == "relay" {
return runRelay(args[1:], stderr, agentworker.RunRelay)
}
@@ -25,6 +25,9 @@ import (
var (
ErrProfileUnknown = errors.New("agent profile is not configured")
ErrProfileNotKit = errors.New("agent profile is not a Kit profile")
// ErrNoShell reports a Kit image that provides no login shell, so a setup
// terminal cannot be opened in it.
ErrNoShell = errors.New("the Kit image provides no shell")
)
// Launcher starts and ends a setup session's container. *kitrun.Launcher
@@ -63,7 +66,15 @@ func (m *Manager) Open(ctx context.Context, org, profile string) (*Session, erro
if err != nil {
return nil, err
}
pty, err := container.ExecPTY(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, []string{"sh", "-l"}, 24, 80)
shell, err := pickShell(func(argv []string) (int, error) {
code, _, err := container.ExecIn(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, argv)
return code, err
})
if err != nil {
_ = m.Launcher.Release(context.WithoutCancel(ctx), run)
return nil, fmt.Errorf("%w: %s", ErrNoShell, p.Kit)
}
pty, err := container.ExecPTY(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, shell, 24, 80)
if err != nil {
_ = m.Launcher.Release(context.WithoutCancel(ctx), run)
return nil, err
@@ -107,6 +118,18 @@ func (s *Session) Close() error {
return err
}
// pickShell returns the login shell the image provides, preferring bash over
// sh, or ErrNoShell when it provides neither. The image, not archie, decides
// what is installed, so the probe runs in the container.
func pickShell(probe func(argv []string) (int, error)) ([]string, error) {
for _, shell := range []string{"bash", "sh"} {
if code, err := probe([]string{shell, "-c", "exit 0"}); err == nil && code == 0 {
return []string{shell, "-l"}, nil
}
}
return nil, ErrNoShell
}
// sessionID is the per-session key for the container, network and volumes. It
// is random so two sessions for one profile never collide on a container name.
func sessionID() (string, error) {
@@ -0,0 +1,54 @@
package harnesssession
import (
"errors"
"slices"
"testing"
)
// A setup terminal must run a shell the image provides, not one archie assumes.
// bash is preferred when both exist; neither is the image's honest failure.
func TestPickShell(t *testing.T) {
t.Parallel()
cases := []struct {
name string
have map[string]bool
failing bool
want []string
wantErr bool
}{
{name: "bash wins when both exist", have: map[string]bool{"bash": true, "sh": true}, want: []string{"bash", "-l"}},
{name: "sh when bash is absent", have: map[string]bool{"sh": true}, want: []string{"sh", "-l"}},
{name: "neither shell is an error", have: map[string]bool{}, wantErr: true},
{name: "a probe that cannot run is not a shell", failing: true, wantErr: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
probe := func(argv []string) (int, error) {
if tc.failing {
return 0, errors.New("exec failed")
}
if tc.have[argv[0]] {
return 0, nil
}
return 127, nil
}
got, err := pickShell(probe)
if tc.wantErr {
if !errors.Is(err, ErrNoShell) {
t.Fatalf("pickShell() = %v, want ErrNoShell", err)
}
return
}
if err != nil {
t.Fatalf("pickShell() = %v", err)
}
if !slices.Equal(got, tc.want) {
t.Errorf("pickShell() = %v, want %v", got, tc.want)
}
})
}
}
+13 -9
View File
@@ -183,16 +183,20 @@ func (l *Launcher) LaunchSetup(ctx context.Context, req SetupRequest) (*Run, err
l.release(run)
return nil, err
}
// The image's own entrypoint is the harness CLI and would exit or run a
// task; a setup container holds still until the PTY's shell ends. The
// container environment stays empty (WorkerEnv nil): the harness env is
// passed explicitly on the exec, and no worker credential reaches it.
// The container runs archie's own agent as a keepalive rather than the
// image's entrypoint (the harness CLI, which would exit) and rather than an
// image binary such as sleep, which a Kit image need not provide. The
// environment stays empty (WorkerEnv nil): the harness env is passed
// explicitly on the exec, and no worker credential reaches it.
run.Container, err = l.Pool.AcquireKit(ctx, container.KitSpec{
Name: "archie-kit-" + req.Session,
Image: req.Kit,
Network: run.network,
Worker: []string{"sleep", "infinity"},
Binds: append([]string{l.CAFile + ":" + caPath + ":ro"}, skillsBinds(k.skills, l.skillsDir())...),
Name: "archie-kit-" + req.Session,
Image: req.Kit,
Network: run.network,
Worker: []string{agentPath, "hold"},
Binds: append([]string{
l.AgentBinary + ":" + agentPath + ":ro",
l.CAFile + ":" + caPath + ":ro",
}, skillsBinds(k.skills, l.skillsDir())...),
Launch: launch,
InstallDone: session.EnterRuntime,
})