Files
archie/internal/app/archieui/run.go
T
sam d201257382 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

184 lines
6.0 KiB
Go

package archieui
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"slices"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/samcharles93/archie-core/internal/app/servicekit"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/presence"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
"github.com/samcharles93/archie-core/internal/infrastructure/harnessrpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/webui"
)
// buildAccessChain builds the live access engine over the stored policies.
// An unreachable State Store returns nil and logs a warning.
func buildAccessChain(ctx context.Context, store *staterpc.Client, log *slog.Logger) (*infraaccess.Live, error) {
live, err := infraaccess.NewLive(ctx, store.ListPolicies, log)
if status.Code(err) == codes.Unavailable {
log.Warn("access chain unavailable; the credential check is the gate", "err", err)
return nil, nil
}
if err != nil {
return nil, err
}
go live.Run(ctx, applystatus.RestampInterval)
return live, nil
}
// Run serves the dashboard until ctx is cancelled. It resolves its own
// options, dials the Gateway and State Store contracts itself, composes the
// HTTP surface over them, and unwinds listener then clients in reverse order
// on shutdown.
func Run(ctx context.Context, options Options) error {
log := slog.New(slog.NewJSONHandler(os.Stderr, nil)).With("component", "ui")
opts, err := Resolve(options, log)
if err != nil {
return err
}
var cleanups []func()
defer func() {
for _, cleanup := range slices.Backward(cleanups) {
cleanup()
}
}()
tasks, closeState, err := staterpc.Dial(opts.State.Target, presence.UI, opts.State.Token)
if err != nil {
return err
}
cleanups = append(cleanups, closeState)
chat, closeGateway, err := gatewayrpc.Dial(opts.Gateway.Target, opts.Gateway.Token)
if err != nil {
return err
}
cleanups = append(cleanups, closeGateway)
chain, err := buildAccessChain(ctx, tasks, log)
if err != nil {
return err
}
provider, err := signInProvider(opts)
if err != nil {
return err
}
authenticate := dashboardAuthenticator(provider, opts, tasks, log)
login, err := dashboardLoginFlow(provider, opts)
if err != nil {
return err
}
srv := compose(ctx, deps{
Options: opts,
Log: log,
Store: tasks,
Chat: chat,
Updates: controlplanerpc.UpdateService(controlplanerpc.NewRPCClient(tasks.ControlPlane()), filepath.Join(configuration.DefaultWorkDir(), "ui-update-deferrals.json"), ""),
Health: newReadinessRegistry(opts, tasks, chat, chain, provider),
ControlPlane: tasks.ControlPlane(),
Identities: tasks,
Authenticate: authenticate,
Login: login,
Access: chain,
Principals: tasks,
Denials: tasks,
})
if err := wireHarness(opts, srv, &cleanups); err != nil {
return err
}
// Live activity has no in-process bus in this process: the pump reads
// events back out of the State Store over the same cursor SSE catch-up
// uses. Backgrounded so priming past a large events table cannot delay the
// listener.
go pumpEvents(ctx, srv, opts.EventPollInterval, log)
go presence.Run(ctx, presence.UI, servicekit.Build(), tasks, srv.Health, log)
liveCtx, stopLive := context.WithCancel(ctx)
defer stopLive()
go srv.RunLive(liveCtx)
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", opts.Listen)
if err != nil {
return fmt.Errorf("listen for ui: %w", err)
}
cleanups = append(cleanups, func() { _ = listener.Close() })
attrs := []any{
"addr", listener.Addr().String(),
"open", webui.DashboardURL(listener.Addr().String()),
"gateway", opts.Gateway.Target,
"state", opts.State.Target,
}
// Name the file holding the token, never the token itself: this line
// reaches the journal and any log an operator pastes.
if opts.TokenFile != "" {
attrs = append(attrs, "token_file", opts.TokenFile)
}
log.Info("archie-ui running", attrs...)
return serve(ctx, listener, srv.Handler(), opts)
}
// wireHarness attaches the setup terminal to the daemon's session contract
// when the operator configured one, and registers its cleanup.
func wireHarness(opts Options, srv *webui.Server, cleanups *[]func()) error {
if opts.Harness.Target == "" {
return nil
}
harness, closeHarness, err := harnessrpc.Dial(opts.Harness.Target, opts.Harness.Token)
if err != nil {
return err
}
srv.HarnessTerminal = harness
*cleanups = append(*cleanups, closeHarness)
return nil
}
// serve runs handler until ctx ends, then shuts down within
// ShutdownTimeout.
func serve(ctx context.Context, listener net.Listener, handler http.Handler, opts Options) error {
server := &http.Server{Handler: handler, ReadHeaderTimeout: opts.ReadHeaderTimeout}
serveErr := make(chan error, 1)
go func() { serveErr <- server.Serve(listener) }()
select {
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), opts.ShutdownTimeout)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
// Shutdown returned once the drain deadline elapsed with a request
// still in flight. Force-close what is left so shutdown finishes
// and the process exits cleanly rather than carrying a deadline
// error to the operator as a fatal exit.
_ = server.Close()
}
return nil
case err := <-serveErr:
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
}
func pumpEvents(ctx context.Context, srv *webui.Server, interval time.Duration, log *slog.Logger) {
if err := srv.PumpEvents(ctx, interval); err != nil {
log.Error("event pump stopped; the activity feed will only show history", "err", err)
}
}