Files
archie/internal/container/pty.go
T
sam 7ef58293be feat(harness): serve a daemon-side setup session to the dashboard
The dashboard's setup terminal was a 503: it had a WebSocket route, an
authorizer and an xterm.js surface, but nothing owned a container. The
daemon owns containers, so the session contract lands there and the
dashboard consumes it over a new gRPC contract.

One session concept, two targets. A setup session is addressed by Kit
profile and is ephemeral; a run session (archie-core-qi91) is addressed by
task and attempt and is bounded by the task. This slice serves setup and
answers Unimplemented for run.

The contract, the authorisation, the lifetime and the egress phase are
settled in docs/prds/external-agent-harness.md, "Session contract". The
short version: the daemon serves harness.v1.HarnessService; the dashboard
authorizes the operator as an update on the secret and then dials the
daemon with the harness service token, required even on loopback because a
session is a shell holding an org's credential; the setup container runs
no worker, mounts no worktree, holds a keepalive, and carries an empty
container environment. Install hooks run at the install phase and the
session enters runtime before the shell, so a runtime-phase credential's
token endpoint is the one the proxy intercepts.

Capture is gated by the egress proxy on a granted service, and a setup
session has no task, so the daemon registers a session grant: the
session's proxy token resolves to the org's bindings for the Kit's
declared services. The real token never enters the container: the token
endpoint response is sentinelized and stored as the org's harness secret.

Every teardown path releases the pool slot, revokes the egress session and
the grant, removes the sandbox network, and removes the session's volumes.
The listener requires HARNESS_TOKEN and stops with a 10s grace before
Stop, so an open session cannot hold shutdown to the watchdog leash.
2026-10-09 01:38:41 +11:00

62 lines
2.0 KiB
Go

package container
import (
"bufio"
"context"
"net"
"github.com/moby/moby/client"
"github.com/samcharles93/archie-core/internal/infrastructure/kit"
)
// PTY is an interactive exec attached to a container's TTY: writes are
// terminal input, reads are terminal output, and Close closes the hijacked
// socket so a blocked Read unblocks. Docker cannot reattach to a disconnected
// exec, so the caller holds this for the life of the session.
type PTY struct {
execID string
conn net.Conn
reader *bufio.Reader
cli *client.Client
}
// ExecPTY starts argv in a container as user with exactly env, on a TTY. It
// mirrors ExecIn's explicit user and env: the exec must never inherit the
// container's worker environment.
func ExecPTY(ctx context.Context, cli *client.Client, id, user string, env, argv []string, rows, cols uint) (*PTY, error) {
exec, err := cli.ExecCreate(ctx, id, client.ExecCreateOptions{
User: user, Env: env, Cmd: argv, WorkingDir: kit.WorkspaceDir,
TTY: true, AttachStdin: true, AttachStdout: true, AttachStderr: true,
ConsoleSize: client.ConsoleSize{Height: rows, Width: cols},
})
if err != nil {
return nil, err
}
attached, err := cli.ExecAttach(ctx, exec.ID, client.ExecAttachOptions{TTY: true})
if err != nil {
return nil, err
}
return &PTY{execID: exec.ID, conn: attached.Conn, reader: attached.Reader, cli: cli}, nil
}
// Read reads terminal output. A TTY stream is raw, so there is no
// stdout/stderr to demultiplex.
func (p *PTY) Read(b []byte) (int, error) { return p.reader.Read(b) }
// Write writes terminal input.
func (p *PTY) Write(b []byte) (int, error) { return p.conn.Write(b) }
// ExitCode reports the exec's exit status. It is meaningful only once the
// output stream has ended.
func (p *PTY) ExitCode(ctx context.Context) (int, error) {
inspected, err := p.cli.ExecInspect(ctx, p.execID, client.ExecInspectOptions{})
if err != nil {
return 0, err
}
return inspected.ExitCode, nil
}
// Close closes the hijacked socket, which unblocks a blocked Read.
func (p *PTY) Close() error { return p.conn.Close() }