* feat(state-store): generalize storerpc as the State Store gRPC contract Implements archie-core-8cda.4.2 against docs/prds/state-store-contract.md rev. 2c: generate protobuf/gRPC client and server adapters for every ratified store contract (proto/state/v1/state.proto, 40 RPCs, one StateStoreService), add internal/infrastructure/staterpc with a bufconn conformance suite exercising both local and remote adapters, serve the service in-process (bridge address + mandatory per-task bearer token when agent containers exist, loopback-only otherwise), and flip archie-agent's workflow.Store to a long-lived gRPC client when the daemon injects STATE_STORE_URL/STATE_STORE_TOKEN (falling back to the legacy NATS storerpc path otherwise -- one agent process is never both). Also drops the *sql.Tx parameter from store.BindingDispatcher.RecordDispatch (a required wire-blocker fix: a transaction cannot cross a gRPC boundary, and production always passed nil). fix(store): drop RecordDispatch's unusable *sql.Tx parameter feat(config): add services.state contract-adapter selector * fix(state-store): apply no-mistakes review decisions - Drop StreamTokenInterceptor (unused; the State Store service has no streaming RPCs and nothing calls it). - Make StateStoreTokens.Validate compare tokens in constant time (crypto/subtle) per the auto-fix hardening.ing finding. - Amend config.example.toml / config.Services.State doc text so it frames [services.state].target as the ratified surface consumed by the upcoming standalone archie-state-store binary (.4.3), not active today.
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
// Command archie-agent runs Archie's NATS-connected agent worker.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/agentworker"
|
|
)
|
|
|
|
type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error
|
|
|
|
func main() {
|
|
os.Exit(run())
|
|
}
|
|
|
|
func run() int {
|
|
return runCommand(os.Args[1:], os.Getenv, os.Stderr, agentworker.Run)
|
|
}
|
|
|
|
func runCommand(args []string, getenv func(string) string, stderr io.Writer, runWorker workerRunner) int {
|
|
flags := flag.NewFlagSet("archie-agent", flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
natsURLFlag := flags.String("nats-url", "", "NATS server URL (defaults to NATS_URL)")
|
|
stateStoreURLFlag := flags.String("state-store-url", "", "State Store gRPC target (defaults to STATE_STORE_URL; empty uses the legacy NATS storerpc path)")
|
|
stateStoreTokenFlag := flags.String("state-store-token", "", "State Store bearer token (defaults to STATE_STORE_TOKEN)")
|
|
if err := flags.Parse(args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
|
|
natsURL, natsToken := natsConnectionSettings(*natsURLFlag, getenv)
|
|
if natsURL == "" {
|
|
fmt.Fprintln(stderr, "error: -nats-url or NATS_URL is required")
|
|
flags.Usage()
|
|
return 1
|
|
}
|
|
|
|
log := slog.New(slog.NewJSONHandler(stderr, nil))
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
stateStoreURL := *stateStoreURLFlag
|
|
if stateStoreURL == "" {
|
|
stateStoreURL = getenv("STATE_STORE_URL")
|
|
}
|
|
stateStoreToken := *stateStoreTokenFlag
|
|
if stateStoreToken == "" {
|
|
stateStoreToken = getenv("STATE_STORE_TOKEN")
|
|
}
|
|
|
|
if err := runWorker(ctx, agentworker.Settings{
|
|
NATSURL: natsURL,
|
|
NATSToken: natsToken,
|
|
StateStoreTarget: stateStoreURL,
|
|
StateStoreToken: stateStoreToken,
|
|
}, log); err != nil {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func natsConnectionSettings(flagURL string, getenv func(string) string) (url, token string) {
|
|
url = flagURL
|
|
if url == "" {
|
|
url = getenv("NATS_URL")
|
|
}
|
|
return url, getenv("NATS_TOKEN")
|
|
}
|