Files
archie/internal/config/servicespec.go
T
sam bba5c9d7de 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

125 lines
4.5 KiB
Go

package config
import (
"fmt"
"sort"
"strings"
"sync"
)
// Service contexts. The context decides which addresses a service must end up
// with: a process that owns a service needs a listen address, and a process
// that dials one needs a target.
const (
// ServiceClient is dialled but not hosted by this repository.
ServiceClient = "client"
// ServiceServer is hosted but not dialled.
ServiceServer = "server"
// ServiceBoth is hosted by one of our processes and dialled by others.
ServiceBoth = "both"
)
// ServiceSpec declares one service: name, defaults and token environment
// variable.
type ServiceSpec struct {
// Context is ServiceClient, ServiceServer, or ServiceBoth.
Context string
// Name is the [services.<name>] section and the key callers look up.
Name string
// Target is the default dial address. Empty means there is no default
// and the operator must supply one, which is how "services.state.target
// is required" is stated without a branch that names the service.
Target string
// Listen is the default bind address for the process that owns the
// service. It is never empty for a hosted service: an empty address
// reaches net.Listen as "any free port", silently moving the service
// off the address its clients dial.
Listen string
// TokenEnv names the environment variable a client falls back to when
// [services.<name>].target_token is empty.
TokenEnv string
}
// Registered service names. They live beside the registrations below so the
// spelling a consumer looks up and the spelling that was registered are the
// same token, not two string literals that a typo can separate.
const (
ServiceNameGateway = "gateway"
ServiceNameState = "state"
ServiceNameMessaging = "messaging"
ServiceNameHarness = "harness"
)
var (
serviceRegistryMu sync.RWMutex
serviceRegistry = map[string]ServiceSpec{}
)
// RegisterService declares one service, replacing any existing spec of that
// name. It panics on an invalid registration.
func RegisterService(context, name, target, listen, tokenEnv string) {
switch context {
case ServiceClient, ServiceServer, ServiceBoth:
default:
panic(fmt.Sprintf(
"config.RegisterService(%q): unknown context %q, want %q, %q or %q",
name, context, ServiceClient, ServiceServer, ServiceBoth,
))
}
if context != ServiceClient && strings.TrimSpace(listen) == "" {
panic(fmt.Sprintf(
"config.RegisterService(%q): a %q service needs a default listen address; "+
"an empty one reaches net.Listen as \"any free port\"",
name, context,
))
}
serviceRegistryMu.Lock()
defer serviceRegistryMu.Unlock()
serviceRegistry[name] = ServiceSpec{
Context: context,
Name: name,
Target: target,
Listen: listen,
TokenEnv: tokenEnv,
}
}
// LookupService returns the spec registered for name. The registry is the
// authority on which service names exist, which is what lets the loader still
// report a typo'd [services.gatway] once Services is a map that would
// otherwise decode any section at all.
func LookupService(name string) (ServiceSpec, bool) {
serviceRegistryMu.RLock()
defer serviceRegistryMu.RUnlock()
spec, ok := serviceRegistry[name]
return spec, ok
}
// RegisteredServices returns every spec, ordered by name so defaulting and
// validation run in a stable order regardless of map iteration.
func RegisteredServices() []ServiceSpec {
serviceRegistryMu.RLock()
defer serviceRegistryMu.RUnlock()
out := make([]ServiceSpec, 0, len(serviceRegistry))
for _, spec := range serviceRegistry {
out = append(out, spec)
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out
}
func init() {
// The services this repository ships. Everything that differs
// between them is an argument here, not a branch somewhere else: the
// gateway's target is defaulted while the State Store's is supplied by
// the operator, and each falls back to its own token variable.
RegisterService(ServiceBoth, ServiceNameGateway, "127.0.0.1:8585", "127.0.0.1:8585", "GATEWAY_TOKEN")
RegisterService(ServiceClient, ServiceNameState, "", "127.0.0.1:9090", "STATE_STORE_TOKEN")
RegisterService(ServiceBoth, ServiceNameMessaging, "127.0.0.1:8586", "127.0.0.1:8586", "MESSAGING_TOKEN")
// The daemon hosts the harness session contract in this repository; the
// dashboard dials it, and its target is the operator's to supply, so a
// deployment with no [services.harness].target leaves the setup terminal
// unwired rather than dialling a default nothing owns.
RegisterService(ServiceBoth, ServiceNameHarness, "", "127.0.0.1:8587", "HARNESS_TOKEN")
}