feat(config): let the State Store and Gateway listen addresses be configured
Both gRPC listeners took their bind address from a -listen flag default alone, with no config key. The port a service binds and the port its clients dial were therefore two separate facts -- the flag default, and [services.<name>].target -- with nothing keeping them in step. On a host that already owned the default the effect read as a code fault rather than a port collision: the store died with "bind: address already in use" while every client reported "state store: connection error ... error reading server preface: EOF". Cockpit binds 9090 by default, so this is not hypothetical. Verifying the dashboard on such a host needed a hand-written overlay plus a matching -listen, two things to keep in step by hand. - [services.state].listen and [services.gateway].listen now name the bind address. Precedence is -listen, then config, then a default applied by the loader. An empty result is a startup ERROR, never a fallback: net.Listen reads "" as "any free port", so defaulting there would bind the service somewhere no client looks and still report success. - Both flag defaults become "" so configuration can win, mirroring how archie-ui already prefers [web].listen over its flag. - Gateway listen defaults to the same constant as the gateway target, so a default deployment's client and server cannot drift onto different ports. [services.state].target still has no default: an empty target stays a startup error in the daemon and gateway, which no longer serve a local store. - ServiceConnection gains the field. Its doc now says clients read only Target and TargetToken, and Listen belongs to the process that owns the service -- the previous wording claimed the struct starts no listeners while carrying one. - deployments/dev.toml states each port once, beside the target it must match, so `task dev` is retargetable on a host that owns 9090 by editing one overlay. Tests. Unit: config defaults, explicit values, and precedence. Process: the existing real-process smoke test now takes its listen from configuration instead of -listen, so the end-to-end wiring is proven by the test that already starts the binary, with no extra process. Both were verified load-bearing by mutation -- inverting the precedence fails the unit test, and making the app layer ignore config fails the process test with "state: no listen address". Verified: task check exit 0. Also documented in config.example.toml, the configuration architecture table, and the `task dev` summary.
This commit is contained in:
1 parent
7414713d61
commit
b09be5515d
14 files changed
+272
-32
No files matched your search
@@ -29,6 +29,24 @@ type GatewayOptions struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
// gatewayListenAndToken resolves the two per-listener inputs RunGateway needs:
|
||||
// the address it binds and the bearer token a non-loopback bind validates.
|
||||
//
|
||||
// Both are flag-first and config-second, and both fail closed rather than
|
||||
// inventing a value -- resolveServiceListen explains why an empty address is an
|
||||
// error rather than a default.
|
||||
func gatewayListenAndToken(b *boot, options GatewayOptions) (listen, token string, err error) {
|
||||
listen, err = resolveServiceListen("gateway", options.Listen, b.cfg.Services.Gateway.Listen)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
token = options.Token
|
||||
if token == "" {
|
||||
token = gatewayResolvedToken(b.cfg.Services.Gateway, b.secrets)
|
||||
}
|
||||
return listen, token, nil
|
||||
}
|
||||
|
||||
// RunGateway owns conversation persistence, model runtime and tool-provider
|
||||
// lifecycles. Task-store adapters access the State Store contract (remote
|
||||
// *staterpc.Client via [services.state].target), not archie.db directly: the
|
||||
@@ -85,16 +103,16 @@ func RunGateway(ctx context.Context, options GatewayOptions) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gatewayToken := options.Token
|
||||
if gatewayToken == "" {
|
||||
gatewayToken = gatewayResolvedToken(b.cfg.Services.Gateway, b.secrets)
|
||||
}
|
||||
//nolint:contextcheck // grpc.StreamServerInterceptor has no context.Context parameter; gatewayrpc.StreamServerInterceptor derives its context from stream.Context() instead
|
||||
opts, loopback, err := gatewayServerOpts(options.Listen, gatewayToken)
|
||||
listen, gatewayToken, err := gatewayListenAndToken(b, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", options.Listen)
|
||||
//nolint:contextcheck // grpc.StreamServerInterceptor has no context.Context parameter; gatewayrpc.StreamServerInterceptor derives its context from stream.Context() instead
|
||||
opts, loopback, err := gatewayServerOpts(listen, gatewayToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen for gateway: %w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// resolveServiceListen picks the address a service listener binds: the -listen
|
||||
// flag when it was given, else the address the configuration names.
|
||||
//
|
||||
// Both services this repository runs used to take their address from a flag
|
||||
// default alone, with no config key. The port a service binds and the port its
|
||||
// clients dial were therefore two separate facts -- the flag default (or a
|
||||
// hand-passed -listen) and [services.<name>].target -- with nothing keeping
|
||||
// them in step. On a host that already owned the default (cockpit owns 9090)
|
||||
// retargeting meant hand-writing an overlay and passing a matching flag, and
|
||||
// the failure looked like a code fault: the listener died with "address already
|
||||
// in use" while every client reported a gRPC handshake error against whatever
|
||||
// else held the port.
|
||||
//
|
||||
// An empty result is an error, never a fallback: net.Listen treats "" as "any
|
||||
// free port", so defaulting here would bind the service somewhere its clients
|
||||
// do not look and report success while doing it.
|
||||
func resolveServiceListen(service, flagValue, configured string) (string, error) {
|
||||
if v := strings.TrimSpace(flagValue); v != "" {
|
||||
return v, nil
|
||||
}
|
||||
if v := strings.TrimSpace(configured); v != "" {
|
||||
return v, nil
|
||||
}
|
||||
return "", fmt.Errorf(
|
||||
"%s: no listen address: pass -listen or set [services.%s].listen", service, service,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package archied
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestResolveServiceListen pins the precedence: an explicit -listen wins, else
|
||||
// the configured address, and an address is never invented.
|
||||
//
|
||||
// The error cases are the ones that matter. net.Listen reads "" as "any free
|
||||
// port", so defaulting here would bind the service somewhere its clients do not
|
||||
// look and report success while doing it -- the exact failure this helper exists
|
||||
// to make impossible.
|
||||
func TestResolveServiceListen(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, flagValue, configured, want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "the flag wins over the configured address",
|
||||
flagValue: "127.0.0.1:9191", configured: "127.0.0.1:9090",
|
||||
want: "127.0.0.1:9191",
|
||||
},
|
||||
{
|
||||
name: "the configured address is used when no flag was passed",
|
||||
configured: "127.0.0.1:9191",
|
||||
want: "127.0.0.1:9191",
|
||||
},
|
||||
{
|
||||
name: "a whitespace flag is not an address",
|
||||
flagValue: " ", configured: "127.0.0.1:9191",
|
||||
want: "127.0.0.1:9191",
|
||||
},
|
||||
{
|
||||
name: "both empty is an error, never a silent ephemeral port",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "whitespace on both sides is an error too",
|
||||
flagValue: " ", configured: "\t",
|
||||
wantErr: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := resolveServiceListen("state", tt.flagValue, tt.configured)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil, want an error; got %q", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("listen = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,11 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error {
|
||||
if err := b.loadConfig(ctx, options.Config, options.Overlay, false); err != nil {
|
||||
return err
|
||||
}
|
||||
b.log = b.log.With("component", "state-store")
|
||||
listen, err := resolveServiceListen("state", options.Listen, b.cfg.Services.State.Listen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.log = b.log.With("component", "state-store", "listen", listen)
|
||||
if err := b.openStateStore(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -67,11 +71,11 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error {
|
||||
}
|
||||
grants := &staterpc.TaskGrants{}
|
||||
//nolint:contextcheck // grpc.StreamServerInterceptor has no context.Context parameter; TaskGrants.StreamInterceptor derives its context from stream.Context() instead
|
||||
opts, loopback, err := stateStoreServerOpts(options.Listen, token, grants)
|
||||
opts, loopback, err := stateStoreServerOpts(listen, token, grants)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", options.Listen)
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen for state store: %w", err)
|
||||
}
|
||||
|
||||
@@ -14,10 +14,18 @@ type Services struct {
|
||||
}
|
||||
|
||||
// ServiceConnection is a contract adapter's gRPC address plus the bearer
|
||||
// token a client presents when the target is non-loopback. It does not
|
||||
// register services or start listeners. Changes require a daemon restart.
|
||||
// token a client presents when the target is non-loopback. A client reads only
|
||||
// Target and TargetToken; Listen belongs to the process that owns the service.
|
||||
// The struct does not itself register services or start listeners. Changes
|
||||
// require a daemon restart.
|
||||
type ServiceConnection struct {
|
||||
Target string `toml:"target" yaml:"target"`
|
||||
// Listen is the address the process that OWNS this service binds
|
||||
// (archie-state-store, archie-gateway). It is read only by that process;
|
||||
// every client ignores it. Defaulted, so a loaded config is never empty
|
||||
// here -- an empty address reaches net.Listen as "any free port", which
|
||||
// would silently move the service off the address its clients dial.
|
||||
Listen string `toml:"listen" yaml:"listen"`
|
||||
// TargetToken is the bearer token a client (daemon or agent) presents
|
||||
// when dialing a non-loopback service target, and the standalone
|
||||
// archie-state-store server validates. Empty means no token, which is
|
||||
|
||||
@@ -12,9 +12,18 @@ import (
|
||||
// Defaults applied to absent input. Named so a reader can find the value
|
||||
// without reading the code that applies it.
|
||||
const (
|
||||
defaultPollInterval = 60 * time.Second
|
||||
defaultDiffCapLines = 400
|
||||
defaultWebListen = "127.0.0.1:8484" // "off" disables
|
||||
defaultPollInterval = 60 * time.Second
|
||||
defaultDiffCapLines = 400
|
||||
defaultWebListen = "127.0.0.1:8484" // "off" disables
|
||||
// defaultGatewayAddr is where archie-gateway binds and, by default, where
|
||||
// clients look for it. One constant for both so a default deployment's
|
||||
// client and server cannot drift onto different ports.
|
||||
defaultGatewayAddr = "127.0.0.1:8585"
|
||||
// defaultStateListen is the state store's bind address. There is
|
||||
// deliberately no default for [services.state].target: an empty target is
|
||||
// a startup error in the daemon and the gateway, which no longer serve a
|
||||
// local store (docs/prds/state-store-contract.md).
|
||||
defaultStateListen = "127.0.0.1:9090"
|
||||
defaultMaxRetries = 3
|
||||
defaultForgeType = "github"
|
||||
defaultForgeHost = "https://github.com"
|
||||
@@ -63,7 +72,13 @@ const (
|
||||
// inspected, and reasoned about independently of whether the result is valid.
|
||||
func (l *Loader) applyDefaults(cfg *config.Config) {
|
||||
if cfg.Services.Gateway.Target == "" {
|
||||
cfg.Services.Gateway.Target = "127.0.0.1:8585"
|
||||
cfg.Services.Gateway.Target = defaultGatewayAddr
|
||||
}
|
||||
if cfg.Services.Gateway.Listen == "" {
|
||||
cfg.Services.Gateway.Listen = defaultGatewayAddr
|
||||
}
|
||||
if cfg.Services.State.Listen == "" {
|
||||
cfg.Services.State.Listen = defaultStateListen
|
||||
}
|
||||
if cfg.Health.Listen == "" {
|
||||
cfg.Health.Listen = defaultHealthListen
|
||||
|
||||
@@ -29,6 +29,48 @@ func TestLoadGatewayServiceTarget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadServiceListens: both gRPC services this repository runs bind an
|
||||
// address the configuration names. They used to be flag-only, so a host that
|
||||
// already owned the default port (cockpit owns 9090) could only be retargeted
|
||||
// by passing -listen and hand-writing an overlay whose target matched -- two
|
||||
// files to keep in step, and nothing to keep them there.
|
||||
func TestLoadServiceListens(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, section, wantState, wantGateway string
|
||||
}{
|
||||
{name: "defaults", wantState: "127.0.0.1:9090", wantGateway: "127.0.0.1:8585"},
|
||||
{
|
||||
name: "explicit listens",
|
||||
section: "[services.state]\nlisten = '127.0.0.1:9191'\n[services.gateway]\nlisten = '127.0.0.1:8686'",
|
||||
wantState: "127.0.0.1:9191",
|
||||
wantGateway: "127.0.0.1:8686",
|
||||
},
|
||||
{
|
||||
name: "state listen alone leaves the gateway default",
|
||||
section: "[services.state]\nlisten = '127.0.0.1:9191'",
|
||||
wantState: "127.0.0.1:9191",
|
||||
wantGateway: "127.0.0.1:8585",
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(path, []byte("bot_user = 'widget'\n"+tt.section), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := loadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Services.State.Listen != tt.wantState {
|
||||
t.Fatalf("state listen = %q, want %q", cfg.Services.State.Listen, tt.wantState)
|
||||
}
|
||||
if cfg.Services.Gateway.Listen != tt.wantGateway {
|
||||
t.Fatalf("gateway listen = %q, want %q", cfg.Services.Gateway.Listen, tt.wantGateway)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadHealthListen: the daemon's liveness surface is not optional. The
|
||||
// update watchdog restarts archied and asks whether it came back, and only
|
||||
// archied can answer that -- the dashboard's /healthz belongs to the
|
||||
|
||||
Reference in new issue
Block a user