refactor(archiegateway): own package for the Gateway service
archie-gateway no longer links the daemon, the web UI or archied. Shared runtime layering, watches, catalog, NATS discovery and the State Store client live in servicekit; chat task profiles in chattask. The Gateway waits for the daemon's NATS endpoint instead of starting a second broker, and chat approvals go through the task-action client instead of a local service with no daemon behind it. Version stamping uses buildinfo only.
This commit is contained in:
36 files changed
+1721
-1547
No files matched your search
@@ -327,7 +327,7 @@ jobs:
|
||||
# ran on with a dead Telegram bot and no error anywhere
|
||||
# (archie-core-1c01). TestDistZipShipsEveryHostCommand fails if this
|
||||
# list and the copy list below stop matching the commands in cmd/.
|
||||
ldflags="-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion=${{ steps.version.outputs.version }} -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=${{ steps.version.outputs.version }} -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary -X github.com/samcharles93/archie-core/internal/buildinfo.Version=${{ steps.version.outputs.version }} -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=${{ steps.version.outputs.version }}"
|
||||
ldflags="-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary -X github.com/samcharles93/archie-core/internal/buildinfo.Version=${{ steps.version.outputs.version }} -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=${{ steps.version.outputs.version }}"
|
||||
mkdir -p dist
|
||||
for cmd in archied archie-gateway archie-state-store archie-ui archie-messaging archie-playbooks; do
|
||||
go build -ldflags "$ldflags" -o "dist/$cmd" "./cmd/$cmd"
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ COPY . .
|
||||
ARG GATEWAY_VERSION=dev
|
||||
ARG RUNTIME_VERSION=dev
|
||||
RUN CGO_ENABLED=0 go build \
|
||||
-ldflags "-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion=${GATEWAY_VERSION} -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=${RUNTIME_VERSION} -X github.com/samcharles93/archie-core/internal/installtype.buildType=container" \
|
||||
-ldflags "-X github.com/samcharles93/archie-core/internal/buildinfo.Version=${GATEWAY_VERSION} -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=${RUNTIME_VERSION} -X github.com/samcharles93/archie-core/internal/installtype.buildType=container" \
|
||||
-o /usr/local/bin/archied ./cmd/archied/
|
||||
|
||||
FROM debian:trixie-slim
|
||||
|
||||
@@ -239,8 +239,6 @@ tasks:
|
||||
GATEWAY_VERSION: '{{.GATEWAY_VERSION | default "dev"}}'
|
||||
RUNTIME_VERSION: '{{.RUNTIME_VERSION | default "dev"}}'
|
||||
SERVICE_LDFLAGS: >-
|
||||
-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion={{.GATEWAY_VERSION}}
|
||||
-X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion={{.RUNTIME_VERSION}}
|
||||
-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary
|
||||
-X github.com/samcharles93/archie-core/internal/buildinfo.Version={{.GATEWAY_VERSION}}
|
||||
-X github.com/samcharles93/archie-core/internal/buildinfo.Runtime={{.RUNTIME_VERSION}}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/app/archiegateway"
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
func main() { os.Exit(run()) }
|
||||
|
||||
func run() int {
|
||||
var options archied.GatewayOptions
|
||||
var options archiegateway.Options
|
||||
showVersion := buildinfo.RegisterVersionFlag("archie-gateway")
|
||||
flag.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory")
|
||||
flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
@@ -27,7 +27,7 @@ func run() int {
|
||||
showVersion()
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
if err := archied.RunGateway(ctx, options); err != nil {
|
||||
if err := archiegateway.Run(ctx, options); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
|
||||
+2
-2
@@ -179,8 +179,8 @@ echo "==> Building native archie binaries..."
|
||||
# says `dev`; its traceability is the checkout it came from.
|
||||
GATEWAY_VERSION="dev"
|
||||
RUNTIME_VERSION="dev"
|
||||
LDFLAGS="-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion=${GATEWAY_VERSION}"
|
||||
LDFLAGS="${LDFLAGS} -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=${RUNTIME_VERSION}"
|
||||
LDFLAGS="-X github.com/samcharles93/archie-core/internal/buildinfo.Version=${GATEWAY_VERSION}"
|
||||
LDFLAGS="${LDFLAGS} -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=${RUNTIME_VERSION}"
|
||||
LDFLAGS="${LDFLAGS} -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary"
|
||||
# Every host binary carries the release it was built from, so the updater can
|
||||
# ask each installed binary what it is.
|
||||
|
||||
@@ -17,17 +17,13 @@ import (
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/chattask"
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
@@ -36,13 +32,10 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agent"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -54,29 +47,14 @@ import (
|
||||
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/sessioncurator"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/skillcurator"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/toolbuilder"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/transcription"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
||||
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/skill"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/minimax"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
||||
"github.com/samcharles93/archie-core/internal/tools/sendfile"
|
||||
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
@@ -87,8 +65,9 @@ import (
|
||||
// reverse at shutdown, matching the LIFO ordering of the deferred calls
|
||||
// they replace.
|
||||
type boot struct {
|
||||
cfg config.Config
|
||||
log *slog.Logger
|
||||
catalog *servicekit.Catalog
|
||||
cfg config.Config
|
||||
log *slog.Logger
|
||||
|
||||
// stderrLog keeps setupLogging on stderr, never touching cfg.Log.File. An
|
||||
// offline command reads the bootstrap config but is not the daemon: creating
|
||||
@@ -152,48 +131,9 @@ type boot struct {
|
||||
// processName is which binary this composition is running as, and is the
|
||||
// name apply-status records carry. Set by each entry point, because one
|
||||
// boot builds both archied and archie-gateway.
|
||||
processName string
|
||||
chatSessionStore gateway.SessionStore
|
||||
// chatPool is the conversation store's pool, which the store owns and
|
||||
// closes; the Gateway also holds its serve claim on it.
|
||||
chatPool *pgxpool.Pool
|
||||
|
||||
// catalog, catalogModels and catalogMu are the model catalog the process
|
||||
// last loaded and the model references it contributes. The refresh loop
|
||||
// replaces both together while the config paths read them, so they are one
|
||||
// value behind one lock rather than two fields that can disagree.
|
||||
catalogMu sync.RWMutex
|
||||
catalog modelcatalog.Snapshot
|
||||
catalogModels []string
|
||||
// catalogCachePath is where the catalog's last download is cached, beside
|
||||
// the config file. Empty when boot had no file path to derive it from.
|
||||
catalogCachePath string
|
||||
// catalogURL overrides the catalog endpoint. Zero in production:
|
||||
// modelcatalog defaults to models.dev. Set only by tests.
|
||||
catalogURL string
|
||||
// catalogRefreshInterval is how often the running daemon re-reads the
|
||||
// catalog. The daemon's refresh loop reads it; zero means the default.
|
||||
catalogRefreshInterval time.Duration
|
||||
processName string
|
||||
|
||||
bus *events.Bus
|
||||
// taskActionsConn is the standalone Gateway process's own NATS connection:
|
||||
// it dials the broker directly (the Gateway does not build the
|
||||
// consumer/stream client the daemon does) and uses it for task actions.
|
||||
// Held so /status can report broker connectivity from the connection this
|
||||
// process actually uses instead of dialling a fresh one. Nil in the daemon.
|
||||
taskActionsConn *natsio.Conn
|
||||
// providerOutcomes records the last-known outcome of every chat-model call
|
||||
// this process makes, read back by /status (newStatusHealth). Built before
|
||||
// the chat runtime, which carries it into each turn runner.
|
||||
providerOutcomes *providerOutcomeRecorder
|
||||
// statusHealth is the /status health source this process serves. It
|
||||
// carries the broker connection and the chat-model outcomes; see
|
||||
// newStatusHealth for the facts it deliberately leaves out.
|
||||
statusHealth gateway.HealthSource
|
||||
// rateLimiter is the shared per-(channel, sender) inbound budget every
|
||||
// chat Router is given. Nil when [chat.rate_limit] is not configured,
|
||||
// which leaves rate limiting off.
|
||||
rateLimiter *ratelimit.Limiter
|
||||
// cfgHolder is the daemon's one configuration Holder. Boot owns it and
|
||||
// the daemon reads through it, so a reload swaps one snapshot and every
|
||||
// reader sees it: there is no second holder to keep in step.
|
||||
@@ -227,33 +167,13 @@ type boot struct {
|
||||
kitLauncher daemon.KitLauncher
|
||||
storeBackend storage.Backend
|
||||
|
||||
// llm is the chat runtime's provider runtime, held in a pointer the chat
|
||||
// surfaces read through chatLLM at each use. A live provider-settings or
|
||||
// model-role-assignments update swaps it wholesale: ai-sdk's Runtime
|
||||
// caches the provider instances it built, so a changed provider set needs
|
||||
// a new Runtime rather than a mutated one.
|
||||
llm atomic.Pointer[runtime.Runtime]
|
||||
// runtimeVersions records the version of every control-plane kind boot's
|
||||
// layering applied. Set once at boot before the runtime-resource watches
|
||||
// start, which read it as their resume points.
|
||||
runtimeVersions map[string]int64
|
||||
|
||||
// transcriber is the optional voice-transcription capability. It is built
|
||||
// beside the chat runtime from this process's own [models]/[providers],
|
||||
// which is the model-owning side of the Messaging boundary: a channel
|
||||
// frontend carries a voice note's bytes across the inbound wire, and this
|
||||
// process turns them into text before the turn is recorded. Nil when no
|
||||
// role is configured or the provider credential did not resolve; a turn
|
||||
// then keeps the frontend's media note rather than failing.
|
||||
transcriber messaging.Transcriber
|
||||
|
||||
toolReg *tools.Registry
|
||||
chatModels *chatModelManager
|
||||
personas *gateway.PersonaRegistry
|
||||
chatTasks gateway.TaskCreator
|
||||
chatController *gateway.StoreTaskController
|
||||
defaultChatIdentity string
|
||||
updateService *releaseupdate.Service
|
||||
|
||||
startGateways []func()
|
||||
capabilityHost *plugin.Host
|
||||
@@ -266,23 +186,7 @@ type boot struct {
|
||||
trees *worktree.Manager
|
||||
worktreeGrants *worktreerpc.Grants
|
||||
identityRunners []*daemon.IdentityRunner
|
||||
memEngines *domainmemory.Registry
|
||||
curatorRegistry *curator.Registry
|
||||
curatorRuntime *curator.Runtime
|
||||
providerRegistry *toolprovider.Registry
|
||||
// mcpMu guards mcpApplied, which records the MCP servers the provider
|
||||
// registry was last built from, keyed by configured name. A live
|
||||
// tool-settings change diffs the stored server set against it so an
|
||||
// unchanged server is left running and untouched (mcp_reconcile.go); the
|
||||
// mutex is held because registerTools runs at boot while the
|
||||
// control-plane watch may already be applying an update.
|
||||
mcpMu sync.Mutex
|
||||
mcpApplied map[string]appliedMCPServer
|
||||
// mcpProvider builds the engine for one configured MCP server. Nil uses
|
||||
// configuredMCPProvider; a test injects a fake so the live reconciliation
|
||||
// can run without spawning a server process.
|
||||
mcpProvider func(config.MCPServer) (toolprovider.Engine, error)
|
||||
d *daemon.Daemon
|
||||
d *daemon.Daemon
|
||||
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
|
||||
// Nil when no chat task creator is configured, in which case startServices
|
||||
// leaves it unstarted rather than running with no reachable job kind.
|
||||
@@ -317,20 +221,6 @@ func newBootstrap() *boot {
|
||||
return &boot{log: slog.New(slog.NewJSONHandler(os.Stderr, nil)), agentStatus: &daemon.AgentStatus{}}
|
||||
}
|
||||
|
||||
// chatLLM is the provider runtime the chat surfaces read at each use: a turn
|
||||
// resolves it when it starts, so a runtime swapped by a live model-settings
|
||||
// update is the one the turn after it runs on.
|
||||
func (b *boot) chatLLM() *runtime.Runtime { return b.llm.Load() }
|
||||
|
||||
// setLLM replaces the chat runtime. A nil runtime is a legal state: a
|
||||
// deployment whose every provider credential was disabled by a live update
|
||||
// leaves chat turns refused until another update restores one.
|
||||
func (b *boot) setLLM(rt *runtime.Runtime) { b.llm.Store(rt) }
|
||||
|
||||
func (b *boot) addCleanup(fn func()) {
|
||||
b.cleanups = append(b.cleanups, fn)
|
||||
}
|
||||
|
||||
func (b *boot) cleanup() {
|
||||
for _, v := range slices.Backward(b.cleanups) {
|
||||
v()
|
||||
@@ -368,7 +258,7 @@ func (b *boot) setupLogging() {
|
||||
// reference that may name one.
|
||||
func (b *boot) openStores(ctx context.Context) error {
|
||||
b.secrets = secret.NewRegistry()
|
||||
return b.openChatSessions(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
// openForge builds the forge client. It runs after the runtime settings are
|
||||
@@ -382,7 +272,7 @@ func (b *boot) openStateStoreAdapter(ctx context.Context) error {
|
||||
if target == "" {
|
||||
return fmt.Errorf("services.state.target is required: archied/archie-gateway no longer own archie.db; the standalone archie-state-store process owns it (docs/prds/state-store-contract.md §12 step 7)")
|
||||
}
|
||||
client, cleanup, err := composeStateStoreClient(b.cfg.Services, b.secrets)
|
||||
client, cleanup, err := servicekit.StateStoreClient(b.cfg.Services, b.secrets)
|
||||
if err != nil {
|
||||
b.log.Error("state store adapter", "err", err)
|
||||
return err
|
||||
@@ -430,36 +320,6 @@ func (b *boot) openAccessChain(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) openChatSessions(ctx context.Context) error {
|
||||
pool, err := servicekit.OpenPool(ctx, b.cfg.DatabaseURL, "the conversation store")
|
||||
if err != nil {
|
||||
return fmt.Errorf("open conversation store: %w", err)
|
||||
}
|
||||
chatSessionStore := gateway.NewPostgresSessionStore(pool)
|
||||
b.chatPool = pool
|
||||
b.chatSessionStore = chatSessionStore
|
||||
b.addCleanup(func() {
|
||||
if err := chatSessionStore.Close(); err != nil {
|
||||
b.log.Error("close conversation store", "err", err)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) claimGatewayOwnership(ctx context.Context) error {
|
||||
ownership, err := postgres.AcquireOwnership(ctx, b.chatPool, postgres.OwnerGateway)
|
||||
if err != nil {
|
||||
return fmt.Errorf("claim gateway ownership: %w", err)
|
||||
}
|
||||
releaseCtx := context.WithoutCancel(ctx)
|
||||
b.addCleanup(func() {
|
||||
if err := ownership.Release(releaseCtx); err != nil {
|
||||
b.log.Error("release gateway ownership", "err", err)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleRequeue replays a parked/waiting task by id, then signals an
|
||||
// early success exit unless -once keeps the daemon running.
|
||||
func (b *boot) handleRequeue(ctx context.Context, requeue int64, once bool) (bool, error) {
|
||||
@@ -474,38 +334,6 @@ func (b *boot) handleRequeue(ctx context.Context, requeue int64, once bool) (boo
|
||||
return !once, nil
|
||||
}
|
||||
|
||||
// catalogState returns the model catalog the process is running and the model
|
||||
// references it contributes. Both together, under one lock: the refresh loop
|
||||
// replaces them as a pair.
|
||||
func (b *boot) catalogState() (modelcatalog.Snapshot, []string) {
|
||||
b.catalogMu.RLock()
|
||||
defer b.catalogMu.RUnlock()
|
||||
return b.catalog, b.catalogModels
|
||||
}
|
||||
|
||||
// setCatalogState installs a loaded catalog. Called only after everything it
|
||||
// has to reach has been built, so a read that fails leaves the previous
|
||||
// snapshot running.
|
||||
func (b *boot) setCatalogState(snapshot modelcatalog.Snapshot, models []string) {
|
||||
b.catalogMu.Lock()
|
||||
defer b.catalogMu.Unlock()
|
||||
b.catalog = snapshot
|
||||
b.catalogModels = models
|
||||
}
|
||||
|
||||
func (b *boot) loadCatalog(ctx context.Context, cfgPath string) {
|
||||
b.catalogCachePath = filepath.Join(filepath.Dir(cfgPath), "models.json")
|
||||
catalog, err := modelcatalog.Load(ctx, b.catalogOptions(b.catalogCachePath))
|
||||
if err != nil {
|
||||
b.log.Warn("model catalog unavailable; using configured providers and models", "err", err)
|
||||
return
|
||||
}
|
||||
models := applyModelCatalog(&b.cfg, catalog)
|
||||
b.cfgHolder.Set(b.cfg.Clone())
|
||||
b.setCatalogState(catalog, models)
|
||||
b.log.Info("model catalog loaded", "providers", len(catalog.Providers), "models", len(models))
|
||||
}
|
||||
|
||||
func (b *boot) seedSoul(cfgPath string) {
|
||||
result, err := configuration.SeedSoul(cfgPath, agent.ShippedSoul())
|
||||
if err != nil {
|
||||
@@ -517,61 +345,6 @@ func (b *boot) seedSoul(cfgPath string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *boot) refreshModelCatalog(ctx context.Context) error {
|
||||
catalog, err := modelcatalog.Load(ctx, b.catalogOptions(b.catalogCachePath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
base := b.cfgHolder.Get().Clone()
|
||||
models := applyModelCatalog(&base, catalog)
|
||||
cfg, _, err := b.runtimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCatalogState(catalog, models)
|
||||
b.publishConfig(ctx, cfg)
|
||||
b.rebuildChatModelRuntime(cfg)
|
||||
b.log.Info("model catalog refreshed", "providers", len(catalog.Providers), "models", len(models))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) catalogOptions(cachePath string) modelcatalog.Options {
|
||||
opts := modelcatalog.Options{
|
||||
URL: b.catalogURL,
|
||||
CachePath: cachePath,
|
||||
}
|
||||
if b.secrets != nil {
|
||||
opts.Getenv = b.secrets.Getenv
|
||||
}
|
||||
if b.cfgHolder != nil {
|
||||
opts.Configured = b.cfgHolder.Get().Providers
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
const modelCatalogRefreshInterval = time.Hour
|
||||
|
||||
func (b *boot) startModelCatalogRefresh(ctx context.Context) {
|
||||
every := b.catalogRefreshInterval
|
||||
if every <= 0 {
|
||||
every = modelCatalogRefreshInterval
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(every)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := b.refreshModelCatalog(ctx); err != nil {
|
||||
b.log.Warn("model catalog refresh failed; keeping the loaded catalog", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (b *boot) setupObservability(ctx context.Context) {
|
||||
cfg, log := b.cfg, b.log
|
||||
bus := events.NewBus()
|
||||
@@ -593,13 +366,13 @@ func (b *boot) connectNATS(ctx context.Context) error { //nolint:nestif // embed
|
||||
return fmt.Errorf("nats.url is required when nats.mode is external")
|
||||
}
|
||||
var err error
|
||||
natsToken, err = configuredNATSToken(cfg.NATS, os.Getenv)
|
||||
natsToken, err = servicekit.NATSToken(cfg.NATS, os.Getenv)
|
||||
if err != nil {
|
||||
log.Error("nats credentials", "err", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
endpoint, readErr := readEmbeddedNATSEndpoint(cfg.StateDir)
|
||||
endpoint, readErr := servicekit.ReadNATSEndpoint(cfg.StateDir)
|
||||
if readErr == nil {
|
||||
url, natsToken = endpoint.URL, endpoint.Token
|
||||
if probe, err := nats.Connect(ctx, nats.Config{URL: url, Token: natsToken, Subjects: []string{workintake.SubjectTaskWildcard}, FilterSubject: workintake.SubjectTaskWildcard}, log); err == nil {
|
||||
@@ -676,7 +449,7 @@ func (b *boot) startEmbeddedNATS(ctx context.Context) (string, string, error) {
|
||||
}
|
||||
b.addCleanup(func() { srv.Shutdown() })
|
||||
log.Info("embedded nats started", "url", srv.ClientURL())
|
||||
if err := writeEmbeddedNATSEndpoint(cfg.StateDir, srv.ClientURL(), srv.Token()); err != nil {
|
||||
if err := servicekit.WriteNATSEndpoint(cfg.StateDir, srv.ClientURL(), srv.Token()); err != nil {
|
||||
srv.Shutdown()
|
||||
return "", "", err
|
||||
}
|
||||
@@ -690,18 +463,6 @@ func (b *boot) setupContainers(ctx context.Context) func() {
|
||||
return closeDocker
|
||||
}
|
||||
|
||||
func (b *boot) setupTranscriber(cfg config.Config, log *slog.Logger) {
|
||||
client, ok := transcription.New(cfg.Models, cfg.Providers, transcription.Options{
|
||||
ResolveSecret: b.secrets.Resolve,
|
||||
})
|
||||
if ok {
|
||||
b.transcriber = client
|
||||
log.Info("voice transcription enabled", "role", transcription.Role)
|
||||
} else if cfg.Models[transcription.Role] != "" {
|
||||
log.Warn("voice transcription configured but unavailable; capability disabled", "role", transcription.Role)
|
||||
}
|
||||
}
|
||||
|
||||
// setupGatewayClient dials the Gateway, which owns every model call. The daemon
|
||||
// only enqueues the chat tasks scheduled workflows create.
|
||||
func (b *boot) setupGatewayClient() error {
|
||||
@@ -716,28 +477,6 @@ func (b *boot) setupGatewayClient() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const rateLimiterEvictInterval = time.Minute
|
||||
|
||||
func (b *boot) startRateLimiter(ctx context.Context, cfg config.RateLimitConfig) {
|
||||
if !cfg.Enabled() {
|
||||
return
|
||||
}
|
||||
b.rateLimiter = ratelimit.New(cfg.Window, cfg.MaxRequests)
|
||||
limiter := b.rateLimiter
|
||||
go func() {
|
||||
ticker := time.NewTicker(rateLimiterEvictInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
limiter.EvictStale()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (b *boot) loadWorkflows() error {
|
||||
cfg, log := b.cfg, b.log
|
||||
if err := b.loadWorkflowRouting(cfg, log); err != nil {
|
||||
@@ -944,243 +683,6 @@ func (b *boot) registerNATSRPC() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) setupMemoryEngine() error {
|
||||
cfg, log := b.cfg, b.log
|
||||
registry := domainmemory.NewRegistry(domainmemory.Registrar{Log: log})
|
||||
|
||||
switch cfg.Memory.Engine {
|
||||
case infraMemory.EngineName, "":
|
||||
root := filepath.Join(cfg.WorkDir, "memory-engine")
|
||||
if err := registry.Register(infraMemory.NewBuiltinEngine(root, 0)); err != nil {
|
||||
return fmt.Errorf("register memory engine %q: %w", infraMemory.EngineName, err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("memory.engine %q has no registered implementation", cfg.Memory.Engine)
|
||||
}
|
||||
|
||||
if err := registry.Start(context.Background()); err != nil {
|
||||
return fmt.Errorf("start memory engine registry: %w", err)
|
||||
}
|
||||
b.memEngines = registry
|
||||
b.addCleanup(func() {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := registry.Stop(shutdownCtx); err != nil {
|
||||
log.Error("memory engine registry shutdown", "err", err)
|
||||
}
|
||||
})
|
||||
log.Info("memory engine started", "engine", cfg.Memory.Engine)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) activeMemoryEngine() (domainmemory.MemoryEngine, bool) {
|
||||
if b.memEngines == nil {
|
||||
return nil, false
|
||||
}
|
||||
name := b.cfg.Memory.Engine
|
||||
if name == "" {
|
||||
name = infraMemory.EngineName
|
||||
}
|
||||
return b.memEngines.Get(name)
|
||||
}
|
||||
|
||||
func (b *boot) memoryStore() gateway.MemoryStore {
|
||||
engine, ok := b.activeMemoryEngine()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return engine
|
||||
}
|
||||
|
||||
func (b *boot) memoryWriter() gateway.MemoryWriteStore {
|
||||
engine, ok := b.activeMemoryEngine()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return engine
|
||||
}
|
||||
|
||||
func (b *boot) setupCurators(ctx context.Context) {
|
||||
log := b.log.With("component", "curator")
|
||||
skillsRoot := b.cfg.SkillsDir
|
||||
if skillsRoot == "" {
|
||||
skillsRoot = b.cfg.WorkDir
|
||||
}
|
||||
b.curatorRegistry = curator.NewRegistry(curator.Registrar{
|
||||
Log: log.With("component", "curator"),
|
||||
Events: curatorEventSink{b.bus},
|
||||
Tools: toolbuilder.New(b.toolReg),
|
||||
MemoryEngines: b.memEngines,
|
||||
Skills: skillcurator.NewStore(skillsRoot),
|
||||
Conversations: sessioncurator.NewAdapter(b.chatSessionStore, b.cfg.BotUser),
|
||||
LLM: curatorLLMRunner{llm: b.chatLLM, outcomes: b.providerOutcomes},
|
||||
Model: b.chatModels.ActiveModel(),
|
||||
})
|
||||
|
||||
if err := b.curatorRegistry.Register(skillcurator.New(skillcurator.DefaultInterval)); err != nil {
|
||||
log.Error("skill curator registration failed", "err", err)
|
||||
}
|
||||
|
||||
if err := b.curatorRegistry.Register(sessioncurator.New(sessioncurator.DefaultInterval, infraMemory.EngineName)); err != nil {
|
||||
log.Error("session-memory curator registration failed", "err", err)
|
||||
}
|
||||
|
||||
for _, def := range b.cfg.Curators {
|
||||
if !def.Enabled {
|
||||
continue
|
||||
}
|
||||
engine := curator.NewDefinitionEngine(curator.Definition{
|
||||
Name: def.Name,
|
||||
Enabled: def.Enabled,
|
||||
Instructions: def.Instructions,
|
||||
Manifest: curator.Manifest{
|
||||
Interval: def.Interval.Std(),
|
||||
Cooldown: def.Cooldown.Std(),
|
||||
OnInput: def.OnInput,
|
||||
Tools: def.Tools,
|
||||
Skills: def.Skills,
|
||||
MemoryEngine: def.MemoryEngine,
|
||||
Conversations: def.Conversations,
|
||||
Model: def.Model,
|
||||
},
|
||||
})
|
||||
if err := b.curatorRegistry.Register(engine); err != nil {
|
||||
log.Error("config curator registration failed", "curator", def.Name, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
b.curatorRuntime = curator.NewRuntime(b.curatorRegistry, curator.RuntimeConfig{})
|
||||
curator.WakeOnPrimaryInput(ctx, b.bus, b.curatorRuntime, events.KindTurnCompleted)
|
||||
rt := b.curatorRuntime
|
||||
b.addCleanup(shutdownCuratorRuntime(rt, log))
|
||||
reg := b.curatorRegistry
|
||||
b.addCleanup(shutdownCuratorRegistry(reg, log))
|
||||
}
|
||||
|
||||
func (b *boot) registerTools() error {
|
||||
cfg, log := b.cfgHolder.Get(), b.log
|
||||
b.providerRegistry = toolprovider.NewRegistry(b.toolReg)
|
||||
b.mcpMu.Lock()
|
||||
b.mcpApplied = make(map[string]appliedMCPServer, len(cfg.Tools.MCPServers))
|
||||
b.mcpMu.Unlock()
|
||||
if workspace := cfg.Chat.Workspace; workspace != "" {
|
||||
unrestricted := cfg.Chat.UnrestrictedFilesystem
|
||||
if err := b.providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted)); err != nil {
|
||||
log.Error("workspace tool provider registration failed", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("workspace tools enabled",
|
||||
"workspace", workspace, "unrestricted_filesystem", unrestricted)
|
||||
} else {
|
||||
log.Info("workspace tools disabled (chat.workspace is unset)")
|
||||
}
|
||||
for _, srv := range cfg.Tools.MCPServers {
|
||||
provider, err := b.buildMCPProvider(srv)
|
||||
if err != nil {
|
||||
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := b.providerRegistry.RegisterOptional(provider); err != nil {
|
||||
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
b.mcpMu.Lock()
|
||||
b.mcpApplied[strings.TrimSpace(srv.Name)] = appliedMCPServer{server: srv, id: provider.Manifest().ID}
|
||||
b.mcpMu.Unlock()
|
||||
}
|
||||
if err := b.capabilityHost.Register(b.providerRegistry); err != nil {
|
||||
log.Error("tool-provider capability registration failed", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) registerStandaloneTools() {
|
||||
cfg, log := b.cfg, b.log
|
||||
|
||||
if catalog, err := skill.CatalogRoots(skill.DefaultRoots(cfg.WorkDir, cfg.SkillsDir)...); err != nil {
|
||||
log.Warn("skill catalog load failed", "err", err)
|
||||
} else if entry := skill.ActivateTool(cfg.WorkDir, catalog); entry != nil {
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("skill_activate registration failed", "err", err)
|
||||
} else {
|
||||
log.Info("skill catalog registered", "skills", len(catalog))
|
||||
}
|
||||
}
|
||||
|
||||
if spillDir := cfg.Tools.Policy.SpillDir; spillDir != "" {
|
||||
if err := toolLimits(cfg).EnsureSpillDir(); err != nil {
|
||||
log.Warn("tool spill directory unavailable; large results will be truncated instead", "err", err)
|
||||
} else if ws := cfg.Chat.Workspace; ws != "" && !cfg.Chat.UnrestrictedFilesystem && !isWithin(ws, spillDir) {
|
||||
log.Warn("tool spill directory is outside chat.workspace; the model cannot read back what is spilled there",
|
||||
"spill_dir", spillDir, "workspace", ws)
|
||||
}
|
||||
}
|
||||
|
||||
b.registerWebFetchTool(cfg)
|
||||
|
||||
if entry := sendfile.Tool(cfg.Chat.Workspace); entry != nil {
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("send_file registration failed", "err", err)
|
||||
} else {
|
||||
log.Info("file sending enabled", "workspace", cfg.Chat.Workspace)
|
||||
}
|
||||
} else {
|
||||
log.Info("file sending disabled (chat.workspace is unset)")
|
||||
}
|
||||
|
||||
b.registerMinimaxTool(cfg, log)
|
||||
}
|
||||
|
||||
func (b *boot) registerWebFetchTool(cfg config.Config) {
|
||||
log := b.log
|
||||
entry := webfetch.Tool(webfetch.Config{
|
||||
Enabled: cfg.Tools.WebFetch.IsEnabled(),
|
||||
Timeout: cfg.Tools.WebFetch.Timeout.Std(),
|
||||
MaxBytes: cfg.Tools.WebFetch.MaxBytes,
|
||||
AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks,
|
||||
})
|
||||
if entry == nil {
|
||||
log.Info("web fetch disabled")
|
||||
return
|
||||
}
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("web_fetch registration failed", "err", err)
|
||||
return
|
||||
}
|
||||
log.Info("web fetch enabled",
|
||||
"allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks)
|
||||
}
|
||||
|
||||
func (b *boot) registerMinimaxTool(cfg config.Config, log *slog.Logger) {
|
||||
if !cfg.Tools.Minimax.IsEnabled() {
|
||||
log.Info("minimax video generation disabled")
|
||||
return
|
||||
}
|
||||
|
||||
apiKey, err := b.secrets.Resolve(cfg.Tools.Minimax.APIKey)
|
||||
if err != nil {
|
||||
log.Warn("minimax video generation enabled but the API key failed to resolve; tool not registered", "err", err)
|
||||
return
|
||||
}
|
||||
if apiKey == "" {
|
||||
log.Warn("minimax video generation enabled but no API key is configured; tool not registered")
|
||||
return
|
||||
}
|
||||
|
||||
entry := minimax.Tool(minimax.Config{Enabled: true, APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL})
|
||||
if entry == nil {
|
||||
return
|
||||
}
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("generate_video registration failed", "err", err)
|
||||
return
|
||||
}
|
||||
log.Info("minimax video generation enabled")
|
||||
}
|
||||
|
||||
func (b *boot) buildDaemon() {
|
||||
log := b.log
|
||||
b.d = &daemon.Daemon{
|
||||
@@ -1415,28 +917,6 @@ func (b *boot) setupBackends(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
//nolint:contextcheck // shutdown runs after the parent context is cancelled
|
||||
func shutdownCuratorRuntime(rt *curator.Runtime, log *slog.Logger) func() {
|
||||
return func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := rt.Stop(stopCtx); err != nil {
|
||||
log.Error("curator runtime shutdown", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:contextcheck // shutdown runs after the parent context is cancelled
|
||||
func shutdownCuratorRegistry(reg *curator.Registry, log *slog.Logger) func() {
|
||||
return func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := reg.Stop(stopCtx); err != nil {
|
||||
log.Error("curator registry shutdown", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func exitCode(err error) int {
|
||||
if err != nil {
|
||||
return 1
|
||||
@@ -1460,3 +940,61 @@ func (b *boot) runLoop(ctx context.Context, once bool) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// setupChatTasks wires the task creator scheduled workflows enqueue through.
|
||||
func (b *boot) setupChatTasks(cfg config.Config) {
|
||||
profiles, defaultChatIdentity := chattask.Profiles(cfg)
|
||||
if len(profiles) > 0 {
|
||||
b.chatTasks = gateway.NewStoreTaskCreatorForProfiles(
|
||||
chattask.Writer{Enqueue: b.stateStore.EnqueueChatTask},
|
||||
profiles,
|
||||
)
|
||||
}
|
||||
b.defaultChatIdentity = defaultChatIdentity
|
||||
}
|
||||
|
||||
func (b *boot) addCleanup(fn func()) {
|
||||
b.cleanups = append(b.cleanups, fn)
|
||||
}
|
||||
|
||||
func (b *boot) catalogState() (modelcatalog.Snapshot, []string) { return b.catalog.State() }
|
||||
|
||||
// loadCatalog layers the model catalog under the file config. A catalog that
|
||||
// cannot be read leaves the configured providers and models in effect.
|
||||
func (b *boot) loadCatalog(ctx context.Context, cfgPath string) {
|
||||
b.catalog = servicekit.NewCatalog(cfgPath)
|
||||
snapshot, err := b.catalog.Fetch(ctx, b.secrets.Getenv, b.cfgHolder.Get().Providers)
|
||||
if err != nil {
|
||||
b.log.Warn("model catalog unavailable; using configured providers and models", "err", err)
|
||||
return
|
||||
}
|
||||
models := servicekit.ApplyModelCatalog(&b.cfg, snapshot)
|
||||
b.cfgHolder.Set(b.cfg.Clone())
|
||||
b.catalog.Set(snapshot, models)
|
||||
b.log.Info("model catalog loaded", "providers", len(snapshot.Providers), "models", len(models))
|
||||
}
|
||||
|
||||
func (b *boot) refreshModelCatalog(ctx context.Context) error {
|
||||
snapshot, err := b.catalog.Fetch(ctx, b.secrets.Getenv, b.cfgHolder.Get().Providers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
base := b.cfgHolder.Get().Clone()
|
||||
models := servicekit.ApplyModelCatalog(&base, snapshot)
|
||||
cfg, _, err := b.runtimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.catalog.Set(snapshot, models)
|
||||
b.publishConfig(ctx, cfg)
|
||||
b.log.Info("model catalog refreshed", "providers", len(snapshot.Providers), "models", len(models))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) startModelCatalogRefresh(ctx context.Context) {
|
||||
servicekit.Every(ctx, servicekit.CatalogRefreshInterval, func() {
|
||||
if err := b.refreshModelCatalog(ctx); err != nil {
|
||||
b.log.Warn("model catalog refresh failed; keeping the loaded catalog", "err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -20,36 +20,11 @@ import (
|
||||
// as it stands reverts each of these layers to its file value until the
|
||||
// process restarts.
|
||||
func (b *boot) runtimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) {
|
||||
cfg, versions, err := b.controlPlane.RuntimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
if settings := b.executionSettings.Load(); settings != nil {
|
||||
applyExecutionBudgets(&cfg, *settings)
|
||||
}
|
||||
if err := configuration.Validate(&cfg); err != nil {
|
||||
wrapped := fmt.Errorf("validate database settings: %w", err)
|
||||
b.reportApplied(ctx, versions, wrapped)
|
||||
return config.Config{}, nil, wrapped
|
||||
}
|
||||
// The State Store persists source references, while openStores resolved
|
||||
// only the file snapshot. Layering provider-settings restores the source
|
||||
// references, so resolve the effective map before any runtime consumes it.
|
||||
if err := resolveProviderSecrets(&cfg, b.secrets, b.log); err != nil {
|
||||
b.reportApplied(ctx, versions, err)
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
b.reportApplied(ctx, versions, nil)
|
||||
return cfg, versions, nil
|
||||
}
|
||||
|
||||
// reportApplied publishes the version of each kind this process just layered
|
||||
// in. On a validation failure every kind is reported with the error, because
|
||||
// the layering is all-or-nothing: none of the versions read took effect.
|
||||
func (b *boot) reportApplied(ctx context.Context, versions map[string]int64, applyErr error) {
|
||||
for kind, version := range versions {
|
||||
b.applyStatus.Report(ctx, kind, version, applyErr)
|
||||
}
|
||||
return servicekit.RuntimeConfig(ctx, b.controlPlane, b.applyStatus, b.secrets, b.log, base, func(cfg *config.Config) {
|
||||
if settings := b.executionSettings.Load(); settings != nil {
|
||||
applyExecutionBudgets(cfg, *settings)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (b *boot) loadRuntimeConfig(ctx context.Context) error {
|
||||
@@ -78,7 +53,7 @@ func (b *boot) reloadConfig(ctx context.Context, doc *configuration.Document) er
|
||||
// unchanged. Re-apply the same merge (idempotent: a catalog that
|
||||
// failed to load merges to identity).
|
||||
catalog, _ := b.catalogState()
|
||||
applyModelCatalog(&doc.Config, catalog)
|
||||
servicekit.ApplyModelCatalog(&doc.Config, catalog)
|
||||
// Bounded: this runs on the signal loop, which handles nothing else
|
||||
// while it waits, and a State Store that has stopped answering must
|
||||
// surface as a failed reload rather than a SIGHUP that never returns.
|
||||
@@ -109,7 +84,7 @@ func (b *boot) reloadConfig(ctx context.Context, doc *configuration.Document) er
|
||||
// keeps the stream that delivers later ones established. The first read and
|
||||
// the first stream are synchronous, so a control plane that cannot be reached
|
||||
// fails the boot that asked for it; after that the watch does not return, it
|
||||
// reconnects (see keepWatch).
|
||||
// reconnects (see servicekit.KeepWatch).
|
||||
func (b *boot) startWorkflowExecutionSettings(ctx context.Context) error {
|
||||
settings, version, err := b.controlPlane.WorkflowExecutionSettings(ctx)
|
||||
if err != nil {
|
||||
@@ -130,9 +105,9 @@ func (b *boot) startWorkflowExecutionSettings(ctx context.Context) error {
|
||||
}
|
||||
return updates, err
|
||||
}
|
||||
go keepWatch(ctx, b.log, controlplane.WorkflowExecutionSettingsKind, version, updates,
|
||||
go servicekit.KeepWatch(ctx, b.log, controlplane.WorkflowExecutionSettingsKind, version, updates,
|
||||
open,
|
||||
waitFor,
|
||||
servicekit.WaitFor,
|
||||
func(update controlplane.AppliedSettings) int64 { return update.Version },
|
||||
func(update controlplane.AppliedSettings) {
|
||||
if update.Err != nil {
|
||||
@@ -292,31 +267,23 @@ var runtimeResourceKinds = []string{
|
||||
controlplane.ContainerRuntimePoliciesKind,
|
||||
}
|
||||
|
||||
// gatewayRuntimeKinds are the kinds the Gateway applies live: the settings its
|
||||
// chat runtime and tool providers are built from.
|
||||
var gatewayRuntimeKinds = []string{
|
||||
controlplane.ProviderSettingsKind,
|
||||
controlplane.ModelRoleAssignmentsKind,
|
||||
controlplane.ToolSettingsKind,
|
||||
}
|
||||
|
||||
// startRuntimeResourceWatches keeps a watch per live kind established for the
|
||||
// life of the process. versions carries the resume point each kind starts
|
||||
// from, the versions boot's layering recorded. The first stream per kind is
|
||||
// opened synchronously, so a control plane that cannot be watched at all
|
||||
// fails the boot that asked for it; after that the watch does not return, it
|
||||
// reconnects (see keepWatch, whose backoff rules these watches ride).
|
||||
// reconnects (see servicekit.KeepWatch, whose backoff rules these watches ride).
|
||||
func (b *boot) startRuntimeResourceWatches(ctx context.Context, versions map[string]int64, kinds []string) error {
|
||||
for _, kind := range kinds {
|
||||
updates, err := b.controlPlane.WatchResource(ctx, kind, versions[kind])
|
||||
if err != nil {
|
||||
return fmt.Errorf("watch %s: %w", kind, err)
|
||||
}
|
||||
go keepWatch(ctx, b.log, kind, versions[kind], updates,
|
||||
go servicekit.KeepWatch(ctx, b.log, kind, versions[kind], updates,
|
||||
func(ctx context.Context, afterVersion int64) (<-chan controlplane.AppliedResource, error) {
|
||||
return b.controlPlane.WatchResource(ctx, kind, afterVersion)
|
||||
},
|
||||
waitFor,
|
||||
servicekit.WaitFor,
|
||||
func(update controlplane.AppliedResource) int64 { return update.Version },
|
||||
func(update controlplane.AppliedResource) { b.applyRuntimeResourceUpdate(ctx, kind, update) })
|
||||
}
|
||||
@@ -342,7 +309,7 @@ func (b *boot) applyRuntimeResourceUpdate(ctx context.Context, kind string, upda
|
||||
if update.Err != nil {
|
||||
// A stream failure carries no version at all (versions start at 1,
|
||||
// store.PutResource), and a process must never report an unreachable
|
||||
// store as its own refusal; the reconnect is keepWatch's business.
|
||||
// store as its own refusal; the reconnect is servicekit.KeepWatch's business.
|
||||
if update.Version > 0 {
|
||||
b.applyStatus.Report(ctx, kind, update.Version, update.Err)
|
||||
}
|
||||
@@ -362,7 +329,7 @@ func (b *boot) applyRuntimeResourceUpdate(ctx context.Context, kind string, upda
|
||||
// providers. The merge is idempotent, and reloadConfig re-applies it
|
||||
// before layering for the same reason.
|
||||
catalog, _ := b.catalogState()
|
||||
applyModelCatalog(&base, catalog)
|
||||
servicekit.ApplyModelCatalog(&base, catalog)
|
||||
cfg, _, err := b.runtimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
// runtimeConfig reported the refusal through apply status for every
|
||||
@@ -373,16 +340,6 @@ func (b *boot) applyRuntimeResourceUpdate(ctx context.Context, kind string, upda
|
||||
b.publishConfig(ctx, cfg)
|
||||
b.log.Info("runtime settings applied", "kind", kind, "version", update.Version)
|
||||
switch kind {
|
||||
case controlplane.ProviderSettingsKind, controlplane.ModelRoleAssignmentsKind:
|
||||
b.rebuildChatModelRuntime(cfg)
|
||||
case controlplane.ToolSettingsKind:
|
||||
// The document's server set and tool entries are live now; reconcile
|
||||
// them and report the reconciliation's own outcome, which is the one
|
||||
// that knows whether a server failed to connect.
|
||||
if err := b.reconcileToolSettings(ctx, cfg); err != nil {
|
||||
b.applyStatus.Report(ctx, kind, update.Version, err)
|
||||
b.log.Error("tool settings reconciled with errors; the running servers stay", "version", update.Version, "err", err)
|
||||
}
|
||||
case controlplane.PluginSettingsKind:
|
||||
// The document's directories are live now; load what they hold and
|
||||
// report the reconciliation's own outcome, which is the one that knows
|
||||
@@ -466,19 +423,3 @@ func (b *boot) refuseSkillsDirChange(ctx context.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rebuildChatModelRuntime re-derives the gateway chat runtime's provider set
|
||||
// after a live change to provider-settings or model-role-assignments. The
|
||||
// turn runner reads the runtime through boot.chatLLM, so the swap reaches its
|
||||
// next turn without rebuilding the runner, and the model manager re-derives
|
||||
// the references it offers from the new role assignments and the catalog.
|
||||
func (b *boot) rebuildChatModelRuntime(cfg config.Config) {
|
||||
if b.chatModels == nil {
|
||||
return // this process serves no chat turns
|
||||
}
|
||||
b.setLLM(agentexec.NewRuntime(executionProviders(cfg)))
|
||||
b.chatModels.SetConfigured(cfg.Models)
|
||||
catalog, models := b.catalogState()
|
||||
b.chatModels.SetModelCatalog(catalog, models)
|
||||
b.log.Info("chat model runtime rebuilt", "providers", len(cfg.Providers), "models", len(cfg.Models))
|
||||
}
|
||||
@@ -1,256 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
)
|
||||
|
||||
// GatewayOptions contains process inputs for the standalone Gateway.
|
||||
type GatewayOptions struct {
|
||||
Config string
|
||||
Overlay string
|
||||
Listen string
|
||||
// Token is the Bearer [REDACTED] the Gateway server validates for a
|
||||
// non-loopback listener. Empty falls back to
|
||||
// [services.gateway].target_token.
|
||||
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 = servicekit.ResolveListen("gateway", options.Listen, b.cfg.Services.Get(config.ServiceNameGateway).Listen)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
token = options.Token
|
||||
if token == "" {
|
||||
token = b.cfg.Services.ResolvedToken(config.ServiceNameGateway, b.secrets.Getenv)
|
||||
}
|
||||
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). The Gateway serves the
|
||||
// conversation store from database_url and holds the Gateway serve claim for
|
||||
// its whole life.
|
||||
func RunGateway(ctx context.Context, options GatewayOptions) error {
|
||||
b := newBootstrap()
|
||||
b.processName = applystatus.Gateway
|
||||
defer b.cleanup()
|
||||
if err := b.loadConfig(ctx, options.Config, options.Overlay); err != nil {
|
||||
return err
|
||||
}
|
||||
b.log = b.log.With("component", "gateway")
|
||||
if err := b.openStores(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
// The Gateway also no longer opens archie.db directly: it consumes the
|
||||
// same remote State Store contract the daemon does via
|
||||
// [services.state].target.
|
||||
if err := b.openGatewayState(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
b.loadCatalog(ctx, options.Config)
|
||||
b.seedSoul(options.Config)
|
||||
url, token := b.cfg.NATS.URL, ""
|
||||
var nc *natsio.Conn
|
||||
var err error
|
||||
if b.cfg.NATS.Mode == config.NATSModeExternal { //nolint:nestif // external and embedded transports have distinct credential and discovery flows
|
||||
token, err = configuredNATSToken(b.cfg.NATS, os.Getenv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nc, err = natsio.Connect(url, natsio.Token(token))
|
||||
} else {
|
||||
var endpoint embeddedNATSEndpoint
|
||||
endpoint, err = readEmbeddedNATSEndpoint(b.cfg.StateDir)
|
||||
if err == nil {
|
||||
url, token = endpoint.URL, endpoint.Token
|
||||
nc, err = natsio.Connect(url, natsio.Token(token))
|
||||
}
|
||||
if nc == nil || err != nil {
|
||||
url, token, err = b.startEmbeddedNATS(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nc, err = natsio.Connect(url, natsio.Token(token))
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect gateway task actions: %w", err)
|
||||
}
|
||||
b.addCleanup(nc.Close)
|
||||
// Held so /status can report this process's own broker connection: the
|
||||
// health source reads it through boot rather than dialling.
|
||||
b.taskActionsConn = nc
|
||||
contract, err := b.startGatewayRuntime(ctx, taskactions.Client{Conn: nc, Timeout: 30 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listen, gatewayToken, err := gatewayListenAndToken(b, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//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)
|
||||
}
|
||||
return b.serveGatewayListener(ctx, listener, loopback, contract, opts)
|
||||
}
|
||||
|
||||
// serveGatewayListener is the serving tail of RunGateway: the listener is
|
||||
// bound, the process declares itself serving, and the chat contract answers
|
||||
// until ctx ends.
|
||||
func (b *boot) serveGatewayListener(ctx context.Context, listener net.Listener, loopback bool, contract gateway.ChatContract, opts []grpc.ServerOption) error {
|
||||
defer listener.Close()
|
||||
b.log.Info("archie-gateway running", "addr", listener.Addr().String(), "token_required", !loopback)
|
||||
// Boot is over and this listener is about to accept: the same fact
|
||||
// systemd's READY=1 asserts, so the announcement goes out here rather than
|
||||
// from a second notion of "started". Not later: Serve
|
||||
// begins accepting the moment it is called, and a unit whose
|
||||
// TimeoutStartSec expires first would restart a healthy process.
|
||||
b.announceReady()
|
||||
return serveGateway(ctx, listener, contract, b.chatSessionStore, opts)
|
||||
}
|
||||
|
||||
func (b *boot) openGatewayState(ctx context.Context) error {
|
||||
if err := b.claimGatewayOwnership(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.openStateStoreAdapter(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.loadRuntimeConfig(ctx); err != nil {
|
||||
return fmt.Errorf("load runtime settings: %w", err)
|
||||
}
|
||||
go b.applyStatus.Run(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
|
||||
b.bus = events.NewBus()
|
||||
b.addCleanup(b.bus.Close)
|
||||
b.capabilityHost = plugin.NewHost()
|
||||
b.startRateLimiter(ctx, b.cfg.Chat.RateLimit)
|
||||
// The Gateway composes the process-wide worktree manager before
|
||||
// setupGatewayChat; nothing in this process reads it today. Kept for the
|
||||
// manager's other consumers; it is the only part of the daemon's tree
|
||||
// composition the Gateway takes -- it must not own identity runners, so it
|
||||
// does not call buildTreesAndIdentities.
|
||||
b.buildWorktreeManager()
|
||||
// setupMemoryEngine must run before setupGatewayChat: setupGatewayChat
|
||||
// constructs the turn runner, which captures b.memEngines at
|
||||
// construction time (see the identical ordering note in main.go's Run).
|
||||
if err := b.setupMemoryEngine(); err != nil { //nolint:contextcheck // setupMemoryEngine owns its own lifecycle context, matching the daemon's setupMemoryEngine
|
||||
return nil, err
|
||||
}
|
||||
contract, err := b.setupGatewayChat(ctx, actor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := b.registerTools(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.registerStandaloneTools()
|
||||
b.addCleanup(shutdownCapabilityHost(b.capabilityHost, b.log))
|
||||
if err := b.capabilityHost.Start(ctx); err != nil {
|
||||
return nil, fmt.Errorf("start gateway tool providers: %w", err)
|
||||
}
|
||||
for _, skipped := range b.providerRegistry.Skipped() {
|
||||
b.log.Warn("gateway tool provider unavailable", "provider", skipped.ID, "err", skipped.Err)
|
||||
}
|
||||
if err := b.startRuntimeResourceWatches(ctx, b.runtimeVersions, gatewayRuntimeKinds); err != nil {
|
||||
return nil, fmt.Errorf("runtime resource watches: %w", err)
|
||||
}
|
||||
b.startModelCatalogRefresh(ctx)
|
||||
// Curators wake on this process's turns and read its conversations.
|
||||
b.setupCurators(ctx)
|
||||
if err := b.curatorRuntime.Start(ctx); err != nil {
|
||||
b.log.Error("curator runtime startup", "err", err)
|
||||
}
|
||||
return contract, nil
|
||||
}
|
||||
|
||||
// inboundMessageHeadroomBytes leaves room for the protobuf framing and the
|
||||
// rest of the request around an attachment at the messaging contract's
|
||||
// ceiling, so the largest attachment a channel frontend may send is accepted
|
||||
// rather than rejected as an oversized gRPC message.
|
||||
const inboundMessageHeadroomBytes = 1 << 20
|
||||
|
||||
// gatewayServerOpts applies the transport security boundary: a loopback
|
||||
// listener is served insecure with no token; any non-loopback address
|
||||
// requires a Bearer [REDACTED], else the process fails closed. It mirrors
|
||||
// stateStoreServerOpts, minus the State Store's task-scoped grants: the
|
||||
// Gateway has one administrative token for all callers.
|
||||
//
|
||||
// The receive limit is raised above gRPC's 4 MB default because an inbound
|
||||
// request carries a channel attachment's bytes: the channel frontend
|
||||
// downloads the file, and this process -- which runs the turn -- holds no
|
||||
// platform credential to fetch it again.
|
||||
func gatewayServerOpts(listen, token string) (opts []grpc.ServerOption, loopback bool, err error) {
|
||||
recv := grpc.MaxRecvMsgSize(messaging.MaxInboundAttachmentBytes + inboundMessageHeadroomBytes)
|
||||
loopback, err = gatewayrpc.TargetIsLoopback(listen)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if loopback {
|
||||
return []grpc.ServerOption{recv}, true, nil
|
||||
}
|
||||
if token == "" {
|
||||
return nil, false, fmt.Errorf(
|
||||
"gateway listen address %q is non-loopback; non-loopback exposure requires a Bearer [REDACTED] (--token / [services.gateway].target_token) or TLS",
|
||||
listen,
|
||||
)
|
||||
}
|
||||
return []grpc.ServerOption{
|
||||
recv,
|
||||
grpc.ChainUnaryInterceptor(gatewayrpc.UnaryServerInterceptor(token)),
|
||||
grpc.ChainStreamInterceptor(gatewayrpc.StreamServerInterceptor(token)),
|
||||
}, false, nil
|
||||
}
|
||||
|
||||
func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract, sessions gateway.SessionStore, opts []grpc.ServerOption) error {
|
||||
server := grpc.NewServer(opts...)
|
||||
gatewayrpc.RegisterServer(server, contract, sessions)
|
||||
serveErr := make(chan error, 1)
|
||||
go func() { serveErr <- server.Serve(listener) }()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// A client can keep a streaming RPC open indefinitely. Allow draining,
|
||||
// then force cancellation before closing the session store and tools.
|
||||
timer := time.AfterFunc(10*time.Second, server.Stop)
|
||||
server.GracefulStop()
|
||||
timer.Stop()
|
||||
return nil
|
||||
case err := <-serveErr:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -21,236 +21,27 @@ import (
|
||||
|
||||
"github.com/moby/moby/client"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultChatMaxSteps bounds the model/tool round-trips in one chat
|
||||
// turn when [config.ChatConfig.MaxSteps] is unset.
|
||||
defaultChatMaxSteps = 100
|
||||
)
|
||||
|
||||
// Component versions are injected from their independent release tags.
|
||||
// Components without a release tag remain "dev" and never generate upgrade
|
||||
// notifications.
|
||||
var (
|
||||
gatewayVersion = "dev"
|
||||
runtimeVersion = "dev"
|
||||
)
|
||||
|
||||
// chatGenerateOptions builds one chat turn's request.
|
||||
func chatGenerateOptions(
|
||||
ctx context.Context,
|
||||
messages []chat.Message,
|
||||
registry *tools.Registry,
|
||||
maxSteps int,
|
||||
limits agentexec.ToolLimits,
|
||||
extra []tools.ToolEntry,
|
||||
contextWindow int,
|
||||
) (core.GenerateOptions, error) {
|
||||
toolOpts := limits.Options()
|
||||
if approval := gateway.ApprovalFromContext(ctx); approval != nil {
|
||||
toolOpts.Approval = approval
|
||||
}
|
||||
|
||||
// Progressive tool disclosure: compose the base registry with the per-turn
|
||||
// extras into a turn-local registry (extras are identity-bound and cannot
|
||||
// live in the process-wide registry), then let the ContextPressureGate
|
||||
// decide whether to serve the full arsenal directly or fall back to the
|
||||
// bridge tools. The bridge tools are never registered process-wide, so they
|
||||
// stay inside this composed, per-turn registry.
|
||||
composed, err := tools.ComposeForTurn(registry, extra)
|
||||
if err != nil {
|
||||
return core.GenerateOptions{}, err
|
||||
}
|
||||
gate := tools.NewContextPressureGate(contextWindow)
|
||||
gate.Evaluate(composed.All())
|
||||
toolSet, err := agentexec.BuildToolSetFrom(gate.FilterTools(composed), toolOpts)
|
||||
if err != nil {
|
||||
return core.GenerateOptions{}, err
|
||||
}
|
||||
|
||||
if maxSteps <= 0 {
|
||||
maxSteps = defaultChatMaxSteps
|
||||
}
|
||||
return core.GenerateOptions{
|
||||
Messages: messages,
|
||||
Tools: toolSet,
|
||||
MaxSteps: maxSteps,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// isWithin reports whether target sits inside base.
|
||||
func isWithin(base, target string) bool {
|
||||
absBase, err := filepath.Abs(base)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
absTarget, err := filepath.Abs(target)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
rel, err := filepath.Rel(absBase, absTarget)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
|
||||
}
|
||||
|
||||
// toolLimits reads the configured per-turn result limits.
|
||||
func toolLimits(cfg config.Config) agentexec.ToolLimits {
|
||||
return agentexec.ToolLimits{
|
||||
MaxResultChars: cfg.Tools.Policy.MaxResultChars,
|
||||
SpillDir: cfg.Tools.Policy.SpillDir,
|
||||
}
|
||||
}
|
||||
|
||||
// taskListOverRead multiplies the requested limit when reading rows that are
|
||||
// filtered by identity afterwards.
|
||||
const taskListOverRead = 5
|
||||
|
||||
// chatTaskListerAdapter gives the gateway a read view of one identity's tasks.
|
||||
// gateway deliberately does not import the task store, so the projection
|
||||
// happens here, as it does for the writer and controller adapters below.
|
||||
type chatTaskListerAdapter struct {
|
||||
tasks func(context.Context, int) ([]workflow.Task, error)
|
||||
}
|
||||
|
||||
func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) {
|
||||
// Over-read before filtering: Tasks applies its limit across the whole
|
||||
// table, so asking for exactly `limit` would return fewer than that for
|
||||
// this identity whenever another identity's work is more recent.
|
||||
rows, err := a.tasks(ctx, limit*taskListOverRead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]gateway.ChatTaskSummary, 0, limit)
|
||||
for _, task := range rows {
|
||||
if task.Identity != identity {
|
||||
continue
|
||||
}
|
||||
if len(out) >= limit {
|
||||
break
|
||||
}
|
||||
out = append(out, gateway.ChatTaskSummary{
|
||||
ID: task.ID,
|
||||
Repo: task.Owner + "/" + task.Repo,
|
||||
Title: task.Title,
|
||||
Status: task.Status,
|
||||
Workflow: task.Workflow,
|
||||
PRNumber: task.PRNumber,
|
||||
Attempt: task.Attempt,
|
||||
ParkReason: task.ParkReason,
|
||||
UpdatedAt: task.UpdatedAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// npmCacheServerEnv returns environment variables that make an
|
||||
// npx-launched MCP server reuse a persistent package cache across daemon
|
||||
// restarts, instead of npm re-resolving and re-downloading the package
|
||||
// from the registry every time the daemon starts. Rooted under the
|
||||
// daemon's own work dir so it persists regardless of whether the daemon
|
||||
// itself runs in an ephemeral container. See mcp.NpmCacheEnv for the
|
||||
// command-matching and env var details shared with archie-agent's
|
||||
// per-task equivalent (internal/app/agentworker/mcp_providers.go), which
|
||||
// points at a mounted cache volume instead of a work-dir subdirectory.
|
||||
//
|
||||
// workDir is unconditionally defaulted before registerTools runs
|
||||
// (internal/infrastructure/configuration's applyGeneralDefaults), but
|
||||
// filepath.Join silently accepts an empty string and would then put the
|
||||
// cache in the daemon's current working directory instead of its
|
||||
// persistent data directory -- guard it explicitly rather than trust that
|
||||
// invariant here too.
|
||||
func npmCacheServerEnv(command, workDir string) []string {
|
||||
if workDir == "" {
|
||||
return nil
|
||||
}
|
||||
return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache"))
|
||||
}
|
||||
|
||||
func configuredMCPProvider(server config.MCPServer, workDir string, sampling mcp.SamplingHandler) (toolprovider.Engine, error) {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("MCP server name is required")
|
||||
}
|
||||
transportType := strings.ToLower(strings.TrimSpace(server.Transport))
|
||||
if transportType == "" {
|
||||
transportType = "stdio"
|
||||
}
|
||||
|
||||
samplingOption := mcptoolprovider.WithSamplingHandler(sampling)
|
||||
|
||||
switch transportType {
|
||||
case "stdio":
|
||||
command := strings.TrimSpace(server.Command)
|
||||
if command == "" {
|
||||
return nil, fmt.Errorf("MCP stdio server %q requires a command", name)
|
||||
}
|
||||
transport := mcp.NewStdioTransport(mcp.StdioTransportConfig{
|
||||
Command: command,
|
||||
Args: append([]string(nil), server.Args...),
|
||||
Dir: server.WorkDir,
|
||||
Env: npmCacheServerEnv(command, workDir),
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "http", "streamablehttp":
|
||||
url := strings.TrimSpace(server.URL)
|
||||
if url == "" {
|
||||
return nil, fmt.Errorf("MCP http server %q requires a url", name)
|
||||
}
|
||||
transport := mcp.NewHTTPTransport(mcp.HTTPTransportConfig{
|
||||
Endpoint: url,
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "sse":
|
||||
sseEndpoint := strings.TrimSpace(server.SSEEndpoint)
|
||||
if sseEndpoint == "" {
|
||||
return nil, fmt.Errorf("MCP sse server %q requires an sse_endpoint", name)
|
||||
}
|
||||
transport := mcp.NewSSETransport(mcp.SSETransportConfig{
|
||||
SSEEndpoint: sseEndpoint,
|
||||
MessageEndpoint: strings.TrimSpace(server.MessageEndpoint),
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("MCP transport %q is not supported", transportType)
|
||||
}
|
||||
}
|
||||
|
||||
// resolveForge builds the forge client for one forge configuration, returning
|
||||
// the resolved token alongside it for the worktree manager.
|
||||
//
|
||||
@@ -320,7 +111,7 @@ func parseArgs() (runArgs, bool) {
|
||||
// adapter has to answer "what is installed?" from a shell, so expose the
|
||||
// same two values here. Machine-readable: one "name version" per line.
|
||||
if *showVersion {
|
||||
fmt.Printf("archied %s\narchie-agent %s\n", gatewayVersion, runtimeVersion)
|
||||
fmt.Printf("archied %s\narchie-agent %s\n", buildinfo.Version, buildinfo.Runtime)
|
||||
return args, true
|
||||
}
|
||||
return args, false
|
||||
@@ -454,274 +245,6 @@ func persistEvents(ctx context.Context, sink *events.Sub, st storecontract.TaskS
|
||||
}
|
||||
}
|
||||
|
||||
// curatorEventSink adapts the in-process event bus to the curator family's
|
||||
// narrow emission contract. Bus publish is non-blocking with bounded,
|
||||
// dropping per-subscriber buffers, so curator activity can never
|
||||
// backpressure the daemon or a chat turn.
|
||||
type curatorEventSink struct {
|
||||
b *events.Bus
|
||||
}
|
||||
|
||||
func (s curatorEventSink) Emit(kind, detail string, data map[string]any) {
|
||||
s.b.Publish(events.Event{Kind: kind, Detail: detail, Data: data})
|
||||
}
|
||||
|
||||
// curatorLLMRunner adapts the shared ai-sdk runtime to the curator
|
||||
// family's narrow LLMRunner contract: one model reference, plain
|
||||
// messages, and the declared tool set -- no streaming. Tools are built
|
||||
// through the same agentexec path a chat turn uses, so a curator's
|
||||
// declared set is converted to a runnable core.ToolSet rather than a
|
||||
// parallel catalogue.
|
||||
type curatorLLMRunner struct {
|
||||
// llm resolves the runtime at each call, so a live model-settings update
|
||||
// that swaps it wholesale is the one the next curator run reads; a
|
||||
// snapshot taken at construction would keep curators on the providers
|
||||
// boot started with.
|
||||
llm func() *runtime.Runtime
|
||||
// outcomes records this call for /status alongside sendChatTurn's. A
|
||||
// curator call is a model call this process made, and it does not pass
|
||||
// through sendChatTurn, so without this a daemon whose only recent model
|
||||
// traffic was curator work reports "no calls attempted yet" while the
|
||||
// provider is demonstrably reachable -- or worse, keeps showing a much
|
||||
// older chat outcome as current.
|
||||
outcomes *providerOutcomeRecorder
|
||||
}
|
||||
|
||||
func (r curatorLLMRunner) Chat(ctx context.Context, req curator.ChatRequest) (curator.ChatResult, error) {
|
||||
// agentexec.NewRuntime returns nil when no provider is configured, and a
|
||||
// nil *runtime.Runtime panics on the first method call. A curator asking
|
||||
// for a completion on a daemon with no providers is a misconfiguration,
|
||||
// not a reason to take the process down.
|
||||
if r.llm == nil {
|
||||
return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured")
|
||||
}
|
||||
llm := r.llm()
|
||||
if llm == nil {
|
||||
return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured")
|
||||
}
|
||||
msgs := make([]chat.Message, 0, len(req.Messages))
|
||||
for _, m := range req.Messages {
|
||||
msgs = append(msgs, chat.Message{Role: chat.Role(m.Role), Content: m.Content})
|
||||
}
|
||||
toolSet, err := agentexec.BuildToolSetFrom(req.Tools, agentexec.ToolSetOptions{})
|
||||
if err != nil {
|
||||
r.outcomes.record(req.Model, err)
|
||||
return curator.ChatResult{}, err
|
||||
}
|
||||
res, err := llm.Chat(ctx, req.Model, core.GenerateOptions{
|
||||
Messages: msgs,
|
||||
Tools: toolSet,
|
||||
MaxSteps: max(req.MaxSteps, 1),
|
||||
})
|
||||
r.outcomes.record(req.Model, err)
|
||||
if err != nil {
|
||||
return curator.ChatResult{}, err
|
||||
}
|
||||
|
||||
calls := make([]curator.ToolCall, 0, len(res.ToolCalls))
|
||||
for _, call := range res.ToolCalls {
|
||||
calls = append(calls, curator.ToolCall{Name: call.ToolName, Input: call.Input})
|
||||
}
|
||||
return curator.ChatResult{Text: res.Text, ToolCalls: calls}, nil
|
||||
}
|
||||
|
||||
type chatTaskWriterAdapter struct {
|
||||
enqueue func(
|
||||
ctx context.Context,
|
||||
owner, repo, title, body, workflow, identity string,
|
||||
inputs map[string]any,
|
||||
) (*workflow.Task, error)
|
||||
}
|
||||
|
||||
func (a chatTaskWriterAdapter) EnqueueChatTask(
|
||||
ctx context.Context,
|
||||
owner, repo, title, body, workflow, identity string,
|
||||
inputs map[string]any,
|
||||
) (int64, error) {
|
||||
task, err := a.enqueue(ctx, owner, repo, title, body, workflow, identity, inputs)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if task == nil {
|
||||
return 0, fmt.Errorf("enqueue chat task returned no task")
|
||||
}
|
||||
return task.ID, nil
|
||||
}
|
||||
|
||||
type chatTaskControllerAdapter struct {
|
||||
taskByID func(context.Context, int64) (*workflow.Task, error)
|
||||
approve func(context.Context, *string, taskactions.Actor, int64, taskactions.ActionPayload) error
|
||||
cancelExecution func(context.Context, int64, string, string) ([]int64, error)
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) {
|
||||
task, err := a.taskByID(ctx, taskID)
|
||||
if err != nil {
|
||||
return gateway.ChatTaskStatus{}, false, err
|
||||
}
|
||||
if task == nil {
|
||||
return gateway.ChatTaskStatus{}, false, nil
|
||||
}
|
||||
if !task.IsForgeBacked() {
|
||||
return gateway.ChatTaskStatus{Status: task.Status, Identity: task.Identity}, true, nil
|
||||
}
|
||||
return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID)
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error {
|
||||
if a.approve == nil {
|
||||
return fmt.Errorf("task approval is unavailable")
|
||||
}
|
||||
// The chat-bound identity is both the scope the task must belong to and
|
||||
// the actor the record names: a chat command has no cross-identity
|
||||
// authority, and /approve carries no selection syntax, so the review gate
|
||||
// answer posts every offered finding.
|
||||
scope := string(actor.Identity)
|
||||
return a.approve(ctx, &scope, actor, taskID, taskactions.ActionPayload{})
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error {
|
||||
task, err := a.taskByID(ctx, taskID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if task == nil {
|
||||
return fmt.Errorf("task %d not found", taskID)
|
||||
}
|
||||
if task.IsForgeBacked() {
|
||||
return fmt.Errorf("task %d is not chat-originated", taskID)
|
||||
}
|
||||
// An operator declining work lands in the same state whichever surface
|
||||
// they used, through the one cancel path: the store records the
|
||||
// cancellation -- every non-terminal step of the current attempt plus the
|
||||
// execution's own move -- and
|
||||
// this write is what a late worker's next step write fails against. This
|
||||
// used to record StatusRejected while the dashboard recorded
|
||||
// StatusClosedWontDo, so the same decision showed up as two different
|
||||
// states -- and StatusRejected, which the PR reconciler uses for "the
|
||||
// pull request was closed without merging", stopped meaning one thing.
|
||||
if a.cancelExecution == nil {
|
||||
return fmt.Errorf("execution cancellation is unavailable")
|
||||
}
|
||||
_, err = a.cancelExecution(ctx, taskID, reason, workflow.StatusClosedWontDo)
|
||||
return err
|
||||
}
|
||||
|
||||
// chatTaskLogReaderAdapter gives the gateway a read view of a task's
|
||||
// persisted log history without importing internal/logging or the task store
|
||||
// into the gateway package. Each identity's reader is scoped to its own
|
||||
// tasks: the identity bound at construction is used for authorization, so a
|
||||
// model cannot read another identity's task logs by passing a different
|
||||
// identity through the tool input.
|
||||
type chatTaskLogReaderAdapter struct {
|
||||
tasks func(context.Context, int64) (*workflow.Task, error)
|
||||
taskLogs *logging.TaskRegistry
|
||||
}
|
||||
|
||||
func (a chatTaskLogReaderAdapter) ReadChatTaskLogs(
|
||||
ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery,
|
||||
) (gateway.ChatTaskLogResult, error) {
|
||||
task, err := a.tasks(ctx, taskID)
|
||||
if err != nil {
|
||||
return gateway.ChatTaskLogResult{}, err
|
||||
}
|
||||
if task == nil {
|
||||
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID)
|
||||
}
|
||||
// Match the filter chatTaskListerAdapter already applies: a model
|
||||
// bound to one identity must not read logs for another identity's
|
||||
// tasks, and tasks with no identity (forge-sourced) are not
|
||||
// readable through a chat tool at all — they belong to the daemon,
|
||||
// not a particular identity. "The empty string MUST NOT retain
|
||||
// special meaning".
|
||||
if task.Identity != identity {
|
||||
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity)
|
||||
}
|
||||
|
||||
if attempt <= 0 {
|
||||
attempt = task.Attempt
|
||||
}
|
||||
path := a.taskLogs.Path(taskID, attempt)
|
||||
if path == "" {
|
||||
return gateway.ChatTaskLogResult{Attempt: attempt, Entries: []gateway.ChatTaskLogEntry{}}, nil
|
||||
}
|
||||
|
||||
page, err := logging.Page(path, logging.Query{
|
||||
Component: q.Component,
|
||||
Contains: q.Contains,
|
||||
Levels: q.Levels,
|
||||
Since: q.Since,
|
||||
Until: q.Until,
|
||||
Limit: q.Limit,
|
||||
}, q.AfterID)
|
||||
if err != nil {
|
||||
return gateway.ChatTaskLogResult{}, err
|
||||
}
|
||||
|
||||
entries := make([]gateway.ChatTaskLogEntry, len(page.Entries))
|
||||
for i, e := range page.Entries {
|
||||
entries[i] = gateway.ChatTaskLogEntry{
|
||||
Time: e.Time,
|
||||
Level: e.Level,
|
||||
Message: e.Message,
|
||||
Fields: e.Fields,
|
||||
}
|
||||
}
|
||||
return gateway.ChatTaskLogResult{
|
||||
Entries: entries,
|
||||
Attempt: attempt,
|
||||
Truncated: page.Truncated,
|
||||
Cursor: page.Cursor,
|
||||
MoreAvailable: page.MoreAvailable,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) {
|
||||
if len(cfg.Identities) > 0 {
|
||||
profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities))
|
||||
for _, identity := range cfg.Identities {
|
||||
if len(identity.Repos) == 0 {
|
||||
continue
|
||||
}
|
||||
profiles = append(profiles, newChatTaskProfile(identity.Name, identity.Repos))
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
return nil, ""
|
||||
}
|
||||
return profiles, profiles[0].Identity
|
||||
}
|
||||
if len(cfg.Repos) == 0 {
|
||||
return nil, ""
|
||||
}
|
||||
return []gateway.TaskProfile{newChatTaskProfile("", cfg.Repos)}, ""
|
||||
}
|
||||
|
||||
func newChatTaskProfile(identity string, repos []config.Repo) gateway.TaskProfile {
|
||||
allowed := make([]string, 0, len(repos))
|
||||
for _, repo := range repos {
|
||||
allowed = append(allowed, repo.Owner+"/"+repo.Name)
|
||||
}
|
||||
return gateway.TaskProfile{
|
||||
Identity: identity,
|
||||
DefaultOwner: repos[0].Owner,
|
||||
DefaultRepo: repos[0].Name,
|
||||
Repos: allowed,
|
||||
}
|
||||
}
|
||||
|
||||
func configureTaskCommands(
|
||||
router *gateway.Router,
|
||||
tasks gateway.TaskCreator,
|
||||
controller gateway.TaskController,
|
||||
lister gateway.ChatTaskLister,
|
||||
identity string,
|
||||
) {
|
||||
router.Tasks = tasks
|
||||
router.Controller = controller
|
||||
router.TaskLister = lister
|
||||
router.Identity = identity
|
||||
}
|
||||
|
||||
// parseListenAddr splits "host:port" into components, using defaults
|
||||
// when the input is empty or missing a part.
|
||||
func parseListenAddr(addr, defaultHost string, defaultPort int) (string, int) {
|
||||
@@ -739,13 +262,6 @@ func parseListenAddr(addr, defaultHost string, defaultPort int) (string, int) {
|
||||
return host, port
|
||||
}
|
||||
|
||||
// sessionKey builds a deterministic session identifier from a gateway
|
||||
// message's routing fields. Platform + channel + thread uniquely identify
|
||||
// a conversation for session persistence and history retrieval.
|
||||
func executionProviders(cfg config.Config) map[string]agentexec.Provider {
|
||||
return agentexec.ProvidersFromConfig(cfg.Providers)
|
||||
}
|
||||
|
||||
// subscribeSystemLogs subscribes to every task's system log subject at once.
|
||||
// A sandboxed container's own stderr disappears at AutoRemove; archie-agent
|
||||
// ships it here instead (agentexec.SubjectForSystem), and this is where it
|
||||
@@ -892,17 +408,6 @@ func identitySuffix(identity string) string {
|
||||
return " (" + identity + ")"
|
||||
}
|
||||
|
||||
func configuredNATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) {
|
||||
if cfg.TokenEnv == "" {
|
||||
return "", nil
|
||||
}
|
||||
token := getenv(cfg.TokenEnv)
|
||||
if token == "" {
|
||||
return "", fmt.Errorf("%s is required when nats.token_env is configured", cfg.TokenEnv)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// updateReportPath is where the update watchdog leaves the phase-2 outcome of
|
||||
// an update for this identity to relay on its next launch. The identity is
|
||||
// hashed so multiple identities sharing one daemon never collide.
|
||||
|
||||
@@ -1,61 +1,13 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
func applyModelCatalog(cfg *config.Config, snapshot modelcatalog.Snapshot) []string {
|
||||
discovered := make(map[string]config.Provider, len(snapshot.Providers))
|
||||
limits := make(map[string]config.ModelLimits)
|
||||
var models []string
|
||||
for _, provider := range snapshot.Providers {
|
||||
discovered[provider.ID] = config.Provider{
|
||||
Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, BaseURL: provider.BaseURL,
|
||||
}
|
||||
for _, model := range provider.Models {
|
||||
ref := provider.ID + "/" + model.ID
|
||||
models = append(models, ref)
|
||||
limits[ref] = config.ModelLimits{ContextWindow: model.ContextWindow, MaxOutputTokens: model.MaxOutputTokens, Reasoning: model.Reasoning}
|
||||
}
|
||||
}
|
||||
cfg.ModelLimits = limits
|
||||
cfg.Providers = mergeProviders(discovered, cfg.Providers)
|
||||
for i := range cfg.Identities {
|
||||
cfg.Identities[i].Providers = mergeProviders(discovered, cfg.Identities[i].Providers)
|
||||
}
|
||||
slices.Sort(models)
|
||||
return models
|
||||
}
|
||||
|
||||
func mergeProviders(base, overrides map[string]config.Provider) map[string]config.Provider {
|
||||
merged := make(map[string]config.Provider, len(base)+len(overrides))
|
||||
maps.Copy(merged, base)
|
||||
for id, override := range overrides {
|
||||
resolved := merged[id]
|
||||
if override.Class != "" {
|
||||
resolved.Class = override.Class
|
||||
}
|
||||
if override.APIKeyEnv != "" {
|
||||
resolved.APIKeyEnv = override.APIKeyEnv
|
||||
}
|
||||
if override.APIKey != (secret.SecretRef{}) {
|
||||
resolved.APIKey = override.APIKey
|
||||
}
|
||||
if override.BaseURL != "" {
|
||||
resolved.BaseURL = override.BaseURL
|
||||
}
|
||||
merged[id] = resolved
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
// catalogView renders the usable providers for the published config view,
|
||||
// sorted by id with each provider's model ids sorted, so the document is
|
||||
// stable across publishes.
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
@@ -61,3 +63,32 @@ func (b *boot) relayUpdateReport(path string) {
|
||||
b.log.Warn("clear pending update report failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// daemonRunningVersions reports the component versions this process can
|
||||
// vouch for, for checking a pending update report against (see
|
||||
// releaseupdate.Report.Verify).
|
||||
//
|
||||
// The daemon component is always vouched for from its own build:
|
||||
// buildinfo.Version is compiled into this binary, so if an installer claims it
|
||||
// put a version into service and this value disagrees, the installer is
|
||||
// wrong. buildinfo.Runtime is deliberately NOT used for the agent component --
|
||||
// it records the agent version archied's own release pipeline stamped, not
|
||||
// the version an agent container is actually running, and the two diverge in
|
||||
// exactly the situation this check exists to detect.
|
||||
//
|
||||
// The agent component is included only once agentStatus has actually
|
||||
// observed one running -- self-reported by an archie-agent worker in a
|
||||
// taskrun.Response (see daemon.AgentStatus), since every archie-agent
|
||||
// process is task-scoped and ephemeral, not something this process can query
|
||||
// directly. Before the first task completes, or when agentStatus is nil
|
||||
// (composition never wired one), the agent component is left out entirely
|
||||
// so it reports as unchecked rather than as confirmed.
|
||||
func daemonRunningVersions(agentStatus *daemon.AgentStatus) map[string]string {
|
||||
versions := map[string]string{releaseupdate.ComponentDaemon: buildinfo.Version}
|
||||
if agentStatus != nil {
|
||||
if version, _, ok := agentStatus.Snapshot(); ok {
|
||||
versions[releaseupdate.ComponentAgent] = version
|
||||
}
|
||||
}
|
||||
return versions
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -0,0 +1,150 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/transcription"
|
||||
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
func (b *server) openChatSessions(ctx context.Context) error {
|
||||
pool, err := servicekit.OpenPool(ctx, b.cfg.DatabaseURL, "the conversation store")
|
||||
if err != nil {
|
||||
return fmt.Errorf("open conversation store: %w", err)
|
||||
}
|
||||
chatSessionStore := gateway.NewPostgresSessionStore(pool)
|
||||
b.chatPool = pool
|
||||
b.chatSessionStore = chatSessionStore
|
||||
b.addCleanup(func() {
|
||||
if err := chatSessionStore.Close(); err != nil {
|
||||
b.log.Error("close conversation store", "err", err)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *server) claimGatewayOwnership(ctx context.Context) error {
|
||||
ownership, err := postgres.AcquireOwnership(ctx, b.chatPool, postgres.OwnerGateway)
|
||||
if err != nil {
|
||||
return fmt.Errorf("claim gateway ownership: %w", err)
|
||||
}
|
||||
releaseCtx := context.WithoutCancel(ctx)
|
||||
b.addCleanup(func() {
|
||||
if err := ownership.Release(releaseCtx); err != nil {
|
||||
b.log.Error("release gateway ownership", "err", err)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *server) setupTranscriber(cfg config.Config, log *slog.Logger) {
|
||||
client, ok := transcription.New(cfg.Models, cfg.Providers, transcription.Options{
|
||||
ResolveSecret: b.secrets.Resolve,
|
||||
})
|
||||
if ok {
|
||||
b.transcriber = client
|
||||
log.Info("voice transcription enabled", "role", transcription.Role)
|
||||
} else if cfg.Models[transcription.Role] != "" {
|
||||
log.Warn("voice transcription configured but unavailable; capability disabled", "role", transcription.Role)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *server) startRateLimiter(ctx context.Context, cfg config.RateLimitConfig) {
|
||||
if !cfg.Enabled() {
|
||||
return
|
||||
}
|
||||
b.rateLimiter = ratelimit.New(cfg.Window, cfg.MaxRequests)
|
||||
limiter := b.rateLimiter
|
||||
go func() {
|
||||
ticker := time.NewTicker(rateLimiterEvictInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
limiter.EvictStale()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// rebuildChatModelRuntime re-derives the gateway chat runtime's provider set
|
||||
// after a live change to provider-settings or model-role-assignments. The
|
||||
// turn runner reads the runtime through server.chatLLM, so the swap reaches its
|
||||
// next turn without rebuilding the runner, and the model manager re-derives
|
||||
// the references it offers from the new role assignments and the catalog.
|
||||
func (b *server) rebuildChatModelRuntime(cfg config.Config) {
|
||||
if b.chatModels == nil {
|
||||
return // this process serves no chat turns
|
||||
}
|
||||
b.setLLM(agentexec.NewRuntime(executionProviders(cfg)))
|
||||
b.chatModels.SetConfigured(cfg.Models)
|
||||
catalog, models := b.catalogState()
|
||||
b.chatModels.SetModelCatalog(catalog, models)
|
||||
b.log.Info("chat model runtime rebuilt", "providers", len(cfg.Providers), "models", len(cfg.Models))
|
||||
}
|
||||
|
||||
// sessionKey builds a deterministic session identifier from a gateway
|
||||
// message's routing fields. Platform + channel + thread uniquely identify
|
||||
// a conversation for session persistence and history retrieval.
|
||||
func executionProviders(cfg config.Config) map[string]agentexec.Provider {
|
||||
return agentexec.ProvidersFromConfig(cfg.Providers)
|
||||
}
|
||||
|
||||
// chatGenerateOptions builds one chat turn's request.
|
||||
func chatGenerateOptions(
|
||||
ctx context.Context,
|
||||
messages []chat.Message,
|
||||
registry *tools.Registry,
|
||||
maxSteps int,
|
||||
limits agentexec.ToolLimits,
|
||||
extra []tools.ToolEntry,
|
||||
contextWindow int,
|
||||
) (core.GenerateOptions, error) {
|
||||
toolOpts := limits.Options()
|
||||
if approval := gateway.ApprovalFromContext(ctx); approval != nil {
|
||||
toolOpts.Approval = approval
|
||||
}
|
||||
|
||||
// Progressive tool disclosure: compose the base registry with the per-turn
|
||||
// extras into a turn-local registry (extras are identity-bound and cannot
|
||||
// live in the process-wide registry), then let the ContextPressureGate
|
||||
// decide whether to serve the full arsenal directly or fall back to the
|
||||
// bridge tools. The bridge tools are never registered process-wide, so they
|
||||
// stay inside this composed, per-turn registry.
|
||||
composed, err := tools.ComposeForTurn(registry, extra)
|
||||
if err != nil {
|
||||
return core.GenerateOptions{}, err
|
||||
}
|
||||
gate := tools.NewContextPressureGate(contextWindow)
|
||||
gate.Evaluate(composed.All())
|
||||
toolSet, err := agentexec.BuildToolSetFrom(gate.FilterTools(composed), toolOpts)
|
||||
if err != nil {
|
||||
return core.GenerateOptions{}, err
|
||||
}
|
||||
|
||||
if maxSteps <= 0 {
|
||||
maxSteps = defaultChatMaxSteps
|
||||
}
|
||||
return core.GenerateOptions{
|
||||
Messages: messages,
|
||||
Tools: toolSet,
|
||||
MaxSteps: maxSteps,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const defaultChatMaxSteps = 100
|
||||
|
||||
const rateLimiterEvictInterval = time.Minute
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
@@ -53,10 +52,6 @@ type chatSetup struct {
|
||||
// disables turn events (tests, minimal setups).
|
||||
Bus *events.Bus
|
||||
Log *slog.Logger
|
||||
// AgentStatus is the composition root's shared tracker for the most
|
||||
// recently observed archie-agent version (see daemon.AgentStatus).
|
||||
// Nil disables the agent component of RunningVersions.
|
||||
AgentStatus *daemon.AgentStatus
|
||||
// MemoryEngine is the durable-memory read surface (b.memEngines' active
|
||||
// engine, resolved by cfg.Memory.Engine). Nil disables the chat <memory>
|
||||
// block for every turn runner built from this setup.
|
||||
@@ -76,54 +71,15 @@ type chatSetup struct {
|
||||
Transcriber messaging.Transcriber
|
||||
}
|
||||
|
||||
// daemonRunningVersions reports the component versions this process can
|
||||
// vouch for, for checking a pending update report against (see
|
||||
// releaseupdate.Report.Verify).
|
||||
//
|
||||
// The daemon component is always vouched for from its own build:
|
||||
// gatewayVersion is compiled into this binary, so if an installer claims it
|
||||
// put a version into service and this value disagrees, the installer is
|
||||
// wrong. runtimeVersion is deliberately NOT used for the agent component --
|
||||
// it records the agent version archied's own release pipeline stamped, not
|
||||
// the version an agent container is actually running, and the two diverge in
|
||||
// exactly the situation this check exists to detect.
|
||||
//
|
||||
// The agent component is included only once agentStatus has actually
|
||||
// observed one running -- self-reported by an archie-agent worker in a
|
||||
// taskrun.Response (see daemon.AgentStatus), since every archie-agent
|
||||
// process is task-scoped and ephemeral, not something this process can query
|
||||
// directly. Before the first task completes, or when agentStatus is nil
|
||||
// (composition never wired one), the agent component is left out entirely
|
||||
// so it reports as unchecked rather than as confirmed.
|
||||
func daemonRunningVersions(agentStatus *daemon.AgentStatus) map[string]string {
|
||||
versions := map[string]string{releaseupdate.ComponentDaemon: gatewayVersion}
|
||||
if agentStatus != nil {
|
||||
if version, _, ok := agentStatus.Snapshot(); ok {
|
||||
versions[releaseupdate.ComponentAgent] = version
|
||||
}
|
||||
}
|
||||
return versions
|
||||
}
|
||||
|
||||
// componentInstallTypeEnricher builds Service.Enrich: it knows how to
|
||||
// describe the three component kinds this daemon can say anything true
|
||||
// about. Anything else (a check command reporting a component ID this
|
||||
// describe the component kinds this process can say anything true about. Anything else (a check command reporting a component ID this
|
||||
// process has no source for) is left alone -- an empty return leaves
|
||||
// whatever the check command itself set.
|
||||
func componentInstallTypeEnricher(agentStatus *daemon.AgentStatus, nats config.NATSConfig) func(string) (string, string) {
|
||||
func componentInstallTypeEnricher(nats config.NATSConfig) func(string) (string, string) {
|
||||
return func(componentID string) (installType, reference string) {
|
||||
switch componentID {
|
||||
case releaseupdate.ComponentDaemon:
|
||||
return installtype.Type(), ""
|
||||
case releaseupdate.ComponentAgent:
|
||||
if agentStatus == nil {
|
||||
return "", ""
|
||||
}
|
||||
_, observedInstallType, ok := agentStatus.Snapshot()
|
||||
if !ok {
|
||||
return "", ""
|
||||
}
|
||||
return observedInstallType, ""
|
||||
case releaseupdate.ComponentNATS:
|
||||
switch nats.Mode {
|
||||
case config.NATSModeEmbedded:
|
||||
@@ -148,7 +104,7 @@ func makeUpdateService(s chatSetup) *releaseupdate.Service {
|
||||
Catalog: releaseupdate.CommandCatalog{Command: cfg.Chat.Telegram.UpdateCheckCommand},
|
||||
StatePath: filepath.Join(cfg.WorkDir, "telegram-update-deferrals.json"),
|
||||
InstallType: installtype.Type(),
|
||||
Enrich: componentInstallTypeEnricher(s.AgentStatus, cfg.NATS),
|
||||
Enrich: componentInstallTypeEnricher(cfg.NATS),
|
||||
}
|
||||
if len(cfg.Chat.Telegram.UpdateInstallCommand) != 0 {
|
||||
updates.Installer = releaseupdate.CommandInstaller{
|
||||
@@ -0,0 +1,194 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
type chatTaskControllerAdapter struct {
|
||||
taskByID func(context.Context, int64) (*workflow.Task, error)
|
||||
approve func(context.Context, *string, taskactions.Actor, int64, taskactions.ActionPayload) error
|
||||
cancelExecution func(context.Context, int64, string, string) ([]int64, error)
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) {
|
||||
task, err := a.taskByID(ctx, taskID)
|
||||
if err != nil {
|
||||
return gateway.ChatTaskStatus{}, false, err
|
||||
}
|
||||
if task == nil {
|
||||
return gateway.ChatTaskStatus{}, false, nil
|
||||
}
|
||||
if !task.IsForgeBacked() {
|
||||
return gateway.ChatTaskStatus{Status: task.Status, Identity: task.Identity}, true, nil
|
||||
}
|
||||
return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID)
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error {
|
||||
if a.approve == nil {
|
||||
return fmt.Errorf("task approval is unavailable")
|
||||
}
|
||||
// The chat-bound identity is both the scope the task must belong to and
|
||||
// the actor the record names: a chat command has no cross-identity
|
||||
// authority, and /approve carries no selection syntax, so the review gate
|
||||
// answer posts every offered finding.
|
||||
scope := string(actor.Identity)
|
||||
return a.approve(ctx, &scope, actor, taskID, taskactions.ActionPayload{})
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error {
|
||||
task, err := a.taskByID(ctx, taskID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if task == nil {
|
||||
return fmt.Errorf("task %d not found", taskID)
|
||||
}
|
||||
if task.IsForgeBacked() {
|
||||
return fmt.Errorf("task %d is not chat-originated", taskID)
|
||||
}
|
||||
// An operator declining work lands in the same state whichever surface
|
||||
// they used, through the one cancel path: the store records the
|
||||
// cancellation -- every non-terminal step of the current attempt plus the
|
||||
// execution's own move -- and
|
||||
// this write is what a late worker's next step write fails against. This
|
||||
// used to record StatusRejected while the dashboard recorded
|
||||
// StatusClosedWontDo, so the same decision showed up as two different
|
||||
// states -- and StatusRejected, which the PR reconciler uses for "the
|
||||
// pull request was closed without merging", stopped meaning one thing.
|
||||
if a.cancelExecution == nil {
|
||||
return fmt.Errorf("execution cancellation is unavailable")
|
||||
}
|
||||
_, err = a.cancelExecution(ctx, taskID, reason, workflow.StatusClosedWontDo)
|
||||
return err
|
||||
}
|
||||
|
||||
// chatTaskListerAdapter gives the gateway a read view of one identity's tasks.
|
||||
// gateway deliberately does not import the task store, so the projection
|
||||
// happens here, as it does for the writer and controller adapters below.
|
||||
type chatTaskListerAdapter struct {
|
||||
tasks func(context.Context, int) ([]workflow.Task, error)
|
||||
}
|
||||
|
||||
func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) {
|
||||
// Over-read before filtering: Tasks applies its limit across the whole
|
||||
// table, so asking for exactly `limit` would return fewer than that for
|
||||
// this identity whenever another identity's work is more recent.
|
||||
rows, err := a.tasks(ctx, limit*taskListOverRead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]gateway.ChatTaskSummary, 0, limit)
|
||||
for _, task := range rows {
|
||||
if task.Identity != identity {
|
||||
continue
|
||||
}
|
||||
if len(out) >= limit {
|
||||
break
|
||||
}
|
||||
out = append(out, gateway.ChatTaskSummary{
|
||||
ID: task.ID,
|
||||
Repo: task.Owner + "/" + task.Repo,
|
||||
Title: task.Title,
|
||||
Status: task.Status,
|
||||
Workflow: task.Workflow,
|
||||
PRNumber: task.PRNumber,
|
||||
Attempt: task.Attempt,
|
||||
ParkReason: task.ParkReason,
|
||||
UpdatedAt: task.UpdatedAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// chatTaskLogReaderAdapter gives the gateway a read view of a task's
|
||||
// persisted log history without importing internal/logging or the task store
|
||||
// into the gateway package. Each identity's reader is scoped to its own
|
||||
// tasks: the identity bound at construction is used for authorization, so a
|
||||
// model cannot read another identity's task logs by passing a different
|
||||
// identity through the tool input.
|
||||
type chatTaskLogReaderAdapter struct {
|
||||
tasks func(context.Context, int64) (*workflow.Task, error)
|
||||
taskLogs *logging.TaskRegistry
|
||||
}
|
||||
|
||||
func (a chatTaskLogReaderAdapter) ReadChatTaskLogs(
|
||||
ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery,
|
||||
) (gateway.ChatTaskLogResult, error) {
|
||||
task, err := a.tasks(ctx, taskID)
|
||||
if err != nil {
|
||||
return gateway.ChatTaskLogResult{}, err
|
||||
}
|
||||
if task == nil {
|
||||
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID)
|
||||
}
|
||||
// Match the filter chatTaskListerAdapter already applies: a model
|
||||
// bound to one identity must not read logs for another identity's
|
||||
// tasks, and tasks with no identity (forge-sourced) are not
|
||||
// readable through a chat tool at all — they belong to the daemon,
|
||||
// not a particular identity. "The empty string MUST NOT retain
|
||||
// special meaning".
|
||||
if task.Identity != identity {
|
||||
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity)
|
||||
}
|
||||
|
||||
if attempt <= 0 {
|
||||
attempt = task.Attempt
|
||||
}
|
||||
path := a.taskLogs.Path(taskID, attempt)
|
||||
if path == "" {
|
||||
return gateway.ChatTaskLogResult{Attempt: attempt, Entries: []gateway.ChatTaskLogEntry{}}, nil
|
||||
}
|
||||
|
||||
page, err := logging.Page(path, logging.Query{
|
||||
Component: q.Component,
|
||||
Contains: q.Contains,
|
||||
Levels: q.Levels,
|
||||
Since: q.Since,
|
||||
Until: q.Until,
|
||||
Limit: q.Limit,
|
||||
}, q.AfterID)
|
||||
if err != nil {
|
||||
return gateway.ChatTaskLogResult{}, err
|
||||
}
|
||||
|
||||
entries := make([]gateway.ChatTaskLogEntry, len(page.Entries))
|
||||
for i, e := range page.Entries {
|
||||
entries[i] = gateway.ChatTaskLogEntry{
|
||||
Time: e.Time,
|
||||
Level: e.Level,
|
||||
Message: e.Message,
|
||||
Fields: e.Fields,
|
||||
}
|
||||
}
|
||||
return gateway.ChatTaskLogResult{
|
||||
Entries: entries,
|
||||
Attempt: attempt,
|
||||
Truncated: page.Truncated,
|
||||
Cursor: page.Cursor,
|
||||
MoreAvailable: page.MoreAvailable,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func configureTaskCommands(
|
||||
router *gateway.Router,
|
||||
tasks gateway.TaskCreator,
|
||||
controller gateway.TaskController,
|
||||
lister gateway.ChatTaskLister,
|
||||
identity string,
|
||||
) {
|
||||
router.Tasks = tasks
|
||||
router.Controller = controller
|
||||
router.TaskLister = lister
|
||||
router.Identity = identity
|
||||
}
|
||||
|
||||
// taskListOverRead multiplies the requested limit when reading rows that are
|
||||
// filtered by identity afterwards.
|
||||
const taskListOverRead = 5
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -0,0 +1,169 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/sessioncurator"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/skillcurator"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/toolbuilder"
|
||||
)
|
||||
|
||||
func (b *server) setupCurators(ctx context.Context) {
|
||||
log := b.log.With("component", "curator")
|
||||
skillsRoot := b.cfg.SkillsDir
|
||||
if skillsRoot == "" {
|
||||
skillsRoot = b.cfg.WorkDir
|
||||
}
|
||||
b.curatorRegistry = curator.NewRegistry(curator.Registrar{
|
||||
Log: log.With("component", "curator"),
|
||||
Events: curatorEventSink{b.bus},
|
||||
Tools: toolbuilder.New(b.toolReg),
|
||||
MemoryEngines: b.memEngines,
|
||||
Skills: skillcurator.NewStore(skillsRoot),
|
||||
Conversations: sessioncurator.NewAdapter(b.chatSessionStore, b.cfg.BotUser),
|
||||
LLM: curatorLLMRunner{llm: b.chatLLM, outcomes: b.providerOutcomes},
|
||||
Model: b.chatModels.ActiveModel(),
|
||||
})
|
||||
|
||||
if err := b.curatorRegistry.Register(skillcurator.New(skillcurator.DefaultInterval)); err != nil {
|
||||
log.Error("skill curator registration failed", "err", err)
|
||||
}
|
||||
|
||||
if err := b.curatorRegistry.Register(sessioncurator.New(sessioncurator.DefaultInterval, infraMemory.EngineName)); err != nil {
|
||||
log.Error("session-memory curator registration failed", "err", err)
|
||||
}
|
||||
|
||||
for _, def := range b.cfg.Curators {
|
||||
if !def.Enabled {
|
||||
continue
|
||||
}
|
||||
engine := curator.NewDefinitionEngine(curator.Definition{
|
||||
Name: def.Name,
|
||||
Enabled: def.Enabled,
|
||||
Instructions: def.Instructions,
|
||||
Manifest: curator.Manifest{
|
||||
Interval: def.Interval.Std(),
|
||||
Cooldown: def.Cooldown.Std(),
|
||||
OnInput: def.OnInput,
|
||||
Tools: def.Tools,
|
||||
Skills: def.Skills,
|
||||
MemoryEngine: def.MemoryEngine,
|
||||
Conversations: def.Conversations,
|
||||
Model: def.Model,
|
||||
},
|
||||
})
|
||||
if err := b.curatorRegistry.Register(engine); err != nil {
|
||||
log.Error("config curator registration failed", "curator", def.Name, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
b.curatorRuntime = curator.NewRuntime(b.curatorRegistry, curator.RuntimeConfig{})
|
||||
curator.WakeOnPrimaryInput(ctx, b.bus, b.curatorRuntime, events.KindTurnCompleted)
|
||||
rt := b.curatorRuntime
|
||||
b.addCleanup(shutdownCuratorRuntime(rt, log))
|
||||
reg := b.curatorRegistry
|
||||
b.addCleanup(shutdownCuratorRegistry(reg, log))
|
||||
}
|
||||
|
||||
//nolint:contextcheck // shutdown runs after the parent context is cancelled
|
||||
func shutdownCuratorRuntime(rt *curator.Runtime, log *slog.Logger) func() {
|
||||
return func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := rt.Stop(stopCtx); err != nil {
|
||||
log.Error("curator runtime shutdown", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:contextcheck // shutdown runs after the parent context is cancelled
|
||||
func shutdownCuratorRegistry(reg *curator.Registry, log *slog.Logger) func() {
|
||||
return func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := reg.Stop(stopCtx); err != nil {
|
||||
log.Error("curator registry shutdown", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// curatorEventSink adapts the in-process event bus to the curator family's
|
||||
// narrow emission contract. Bus publish is non-blocking with bounded,
|
||||
// dropping per-subscriber buffers, so curator activity can never
|
||||
// backpressure the daemon or a chat turn.
|
||||
type curatorEventSink struct {
|
||||
b *events.Bus
|
||||
}
|
||||
|
||||
func (s curatorEventSink) Emit(kind, detail string, data map[string]any) {
|
||||
s.b.Publish(events.Event{Kind: kind, Detail: detail, Data: data})
|
||||
}
|
||||
|
||||
// curatorLLMRunner adapts the shared ai-sdk runtime to the curator
|
||||
// family's narrow LLMRunner contract: one model reference, plain
|
||||
// messages, and the declared tool set -- no streaming. Tools are built
|
||||
// through the same agentexec path a chat turn uses, so a curator's
|
||||
// declared set is converted to a runnable core.ToolSet rather than a
|
||||
// parallel catalogue.
|
||||
type curatorLLMRunner struct {
|
||||
// llm resolves the runtime at each call, so a live model-settings update
|
||||
// that swaps it wholesale is the one the next curator run reads; a
|
||||
// snapshot taken at construction would keep curators on the providers
|
||||
// boot started with.
|
||||
llm func() *runtime.Runtime
|
||||
// outcomes records this call for /status alongside sendChatTurn's. A
|
||||
// curator call is a model call this process made, and it does not pass
|
||||
// through sendChatTurn, so without this a daemon whose only recent model
|
||||
// traffic was curator work reports "no calls attempted yet" while the
|
||||
// provider is demonstrably reachable -- or worse, keeps showing a much
|
||||
// older chat outcome as current.
|
||||
outcomes *providerOutcomeRecorder
|
||||
}
|
||||
|
||||
func (r curatorLLMRunner) Chat(ctx context.Context, req curator.ChatRequest) (curator.ChatResult, error) {
|
||||
// agentexec.NewRuntime returns nil when no provider is configured, and a
|
||||
// nil *runtime.Runtime panics on the first method call. A curator asking
|
||||
// for a completion on a daemon with no providers is a misconfiguration,
|
||||
// not a reason to take the process down.
|
||||
if r.llm == nil {
|
||||
return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured")
|
||||
}
|
||||
llm := r.llm()
|
||||
if llm == nil {
|
||||
return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured")
|
||||
}
|
||||
msgs := make([]chat.Message, 0, len(req.Messages))
|
||||
for _, m := range req.Messages {
|
||||
msgs = append(msgs, chat.Message{Role: chat.Role(m.Role), Content: m.Content})
|
||||
}
|
||||
toolSet, err := agentexec.BuildToolSetFrom(req.Tools, agentexec.ToolSetOptions{})
|
||||
if err != nil {
|
||||
r.outcomes.record(req.Model, err)
|
||||
return curator.ChatResult{}, err
|
||||
}
|
||||
res, err := llm.Chat(ctx, req.Model, core.GenerateOptions{
|
||||
Messages: msgs,
|
||||
Tools: toolSet,
|
||||
MaxSteps: max(req.MaxSteps, 1),
|
||||
})
|
||||
r.outcomes.record(req.Model, err)
|
||||
if err != nil {
|
||||
return curator.ChatResult{}, err
|
||||
}
|
||||
|
||||
calls := make([]curator.ToolCall, 0, len(res.ToolCalls))
|
||||
for _, call := range res.ToolCalls {
|
||||
calls = append(calls, curator.ToolCall{Name: call.ToolName, Input: call.Input})
|
||||
}
|
||||
return curator.ChatResult{Text: res.Text, ToolCalls: calls}, nil
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
)
|
||||
|
||||
// serveGatewayListener is the serving tail of Run: the listener is
|
||||
// bound, the process declares itself serving, and the chat contract answers
|
||||
// until ctx ends.
|
||||
func (b *server) serveGatewayListener(ctx context.Context, listener net.Listener, loopback bool, contract gateway.ChatContract, opts []grpc.ServerOption) error {
|
||||
defer listener.Close()
|
||||
b.log.Info("archie-gateway running", "addr", listener.Addr().String(), "token_required", !loopback)
|
||||
// Boot is over and this listener is about to accept: the same fact
|
||||
// systemd's READY=1 asserts, so the announcement goes out here rather than
|
||||
// from a second notion of "started". Not later: Serve
|
||||
// begins accepting the moment it is called, and a unit whose
|
||||
// TimeoutStartSec expires first would restart a healthy process.
|
||||
b.announceReady()
|
||||
return serveGateway(ctx, listener, contract, b.chatSessionStore, opts)
|
||||
}
|
||||
|
||||
func (b *server) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
|
||||
b.bus = events.NewBus()
|
||||
b.addCleanup(b.bus.Close)
|
||||
b.capabilityHost = plugin.NewHost()
|
||||
b.startRateLimiter(ctx, b.cfg.Chat.RateLimit)
|
||||
// setupMemoryEngine must run before setupGatewayChat: setupGatewayChat
|
||||
// constructs the turn runner, which captures b.memEngines at
|
||||
// construction time.
|
||||
if err := b.setupMemoryEngine(); err != nil { //nolint:contextcheck // setupMemoryEngine owns its own lifecycle context
|
||||
return nil, err
|
||||
}
|
||||
contract, err := b.setupGatewayChat(ctx, actor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := b.registerTools(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.registerStandaloneTools()
|
||||
b.addCleanup(shutdownCapabilityHost(b.capabilityHost, b.log))
|
||||
if err := b.capabilityHost.Start(ctx); err != nil {
|
||||
return nil, fmt.Errorf("start gateway tool providers: %w", err)
|
||||
}
|
||||
for _, skipped := range b.providerRegistry.Skipped() {
|
||||
b.log.Warn("gateway tool provider unavailable", "provider", skipped.ID, "err", skipped.Err)
|
||||
}
|
||||
if err := b.startRuntimeWatches(ctx); err != nil {
|
||||
return nil, fmt.Errorf("runtime resource watches: %w", err)
|
||||
}
|
||||
b.startModelCatalogRefresh(ctx)
|
||||
// Curators wake on this process's turns and read its conversations.
|
||||
b.setupCurators(ctx)
|
||||
if err := b.curatorRuntime.Start(ctx); err != nil {
|
||||
b.log.Error("curator runtime startup", "err", err)
|
||||
}
|
||||
return contract, nil
|
||||
}
|
||||
|
||||
// inboundMessageHeadroomBytes leaves room for the protobuf framing and the
|
||||
// rest of the request around an attachment at the messaging contract's
|
||||
// ceiling, so the largest attachment a channel frontend may send is accepted
|
||||
// rather than rejected as an oversized gRPC message.
|
||||
const inboundMessageHeadroomBytes = 1 << 20
|
||||
|
||||
// gatewayServerOpts applies the transport security boundary: a loopback
|
||||
// listener is served insecure with no token; any non-loopback address
|
||||
// requires a Bearer [REDACTED], else the process fails closed. It mirrors
|
||||
// stateStoreServerOpts, minus the State Store's task-scoped grants: the
|
||||
// Gateway has one administrative token for all callers.
|
||||
//
|
||||
// The receive limit is raised above gRPC's 4 MB default because an inbound
|
||||
// request carries a channel attachment's bytes: the channel frontend
|
||||
// downloads the file, and this process -- which runs the turn -- holds no
|
||||
// platform credential to fetch it again.
|
||||
func gatewayServerOpts(listen, token string) (opts []grpc.ServerOption, loopback bool, err error) {
|
||||
recv := grpc.MaxRecvMsgSize(messaging.MaxInboundAttachmentBytes + inboundMessageHeadroomBytes)
|
||||
loopback, err = gatewayrpc.TargetIsLoopback(listen)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if loopback {
|
||||
return []grpc.ServerOption{recv}, true, nil
|
||||
}
|
||||
if token == "" {
|
||||
return nil, false, fmt.Errorf(
|
||||
"gateway listen address %q is non-loopback; non-loopback exposure requires a Bearer [REDACTED] (--token / [services.gateway].target_token) or TLS",
|
||||
listen,
|
||||
)
|
||||
}
|
||||
return []grpc.ServerOption{
|
||||
recv,
|
||||
grpc.ChainUnaryInterceptor(gatewayrpc.UnaryServerInterceptor(token)),
|
||||
grpc.ChainStreamInterceptor(gatewayrpc.StreamServerInterceptor(token)),
|
||||
}, false, nil
|
||||
}
|
||||
|
||||
func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract, sessions gateway.SessionStore, opts []grpc.ServerOption) error {
|
||||
server := grpc.NewServer(opts...)
|
||||
gatewayrpc.RegisterServer(server, contract, sessions)
|
||||
serveErr := make(chan error, 1)
|
||||
go func() { serveErr <- server.Serve(listener) }()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// A client can keep a streaming RPC open indefinitely. Allow draining,
|
||||
// then force cancellation before closing the session store and tools.
|
||||
timer := time.AfterFunc(10*time.Second, server.Stop)
|
||||
server.GracefulStop()
|
||||
timer.Stop()
|
||||
return nil
|
||||
case err := <-serveErr:
|
||||
return err
|
||||
}
|
||||
}
|
||||
+21
-22
@@ -1,14 +1,16 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/chattask"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agent"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
@@ -19,7 +21,7 @@ import (
|
||||
// chat task creator/controller, default chat identity and the release-update
|
||||
// service. The daemon and the standalone Gateway build through this one
|
||||
// constructor so the two processes cannot diverge.
|
||||
func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error {
|
||||
func (b *server) setupChatRuntime(ctx context.Context, cfg config.Config, actor gateway.ChatTaskActor) error {
|
||||
// ── LLM runtime ──────────────────────────────────────────────────
|
||||
// Created before the router so the LLMResponder can be wired in for
|
||||
// non-command message processing.
|
||||
@@ -57,19 +59,16 @@ func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error {
|
||||
b.statusHealth = newStatusHealth(b)
|
||||
|
||||
b.setupChatTasks(cfg)
|
||||
var canceller storecontract.ExecutionCanceller
|
||||
if ec, ok := b.stateStore.(storecontract.ExecutionCanceller); ok {
|
||||
canceller = ec
|
||||
}
|
||||
b.chatController = gateway.NewStoreTaskController(chatTaskControllerAdapter{
|
||||
taskByID: b.stateStore.TaskByID,
|
||||
// Chat's /approve reaches the daemon's one task-action service rather
|
||||
// than its own requeue, so a chat approval and a dashboard approval
|
||||
// cannot record different decisions for one operator intent
|
||||
approve: func(ctx context.Context, scope *string, actor taskactions.Actor, taskID int64, res taskactions.ActionPayload) error {
|
||||
return b.taskActions().Apply(ctx, scope, actor, taskID, taskstate.ActionApprove, res)
|
||||
approve: func(ctx context.Context, scope *string, by taskactions.Actor, taskID int64, res taskactions.ActionPayload) error {
|
||||
_, err := actor.ApplyChatTaskAction(ctx, scope, by, taskID, taskstate.ActionApprove, res)
|
||||
return err
|
||||
},
|
||||
cancelExecution: canceller.CancelExecution,
|
||||
cancelExecution: b.stateStore.CancelExecution,
|
||||
})
|
||||
b.updateService = makeUpdateService(chatSetup{Cfg: config.NewHolder(cfg)})
|
||||
return nil
|
||||
@@ -79,28 +78,28 @@ func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error {
|
||||
// process. The first stream is opened synchronously, so a control plane that
|
||||
// cannot be watched at all fails the boot that asked for it rather than
|
||||
// leaving the process running personas it can no longer update; after that the
|
||||
// watch reconnects instead of ending (see keepWatch).
|
||||
// watch reconnects instead of ending (see servicekit.KeepWatch).
|
||||
// setupChatTasks wires the task creator chat commands and scheduled workflows
|
||||
// enqueue through.
|
||||
func (b *boot) setupChatTasks(cfg config.Config) {
|
||||
profiles, defaultChatIdentity := chatTaskProfiles(cfg)
|
||||
func (b *server) setupChatTasks(cfg config.Config) {
|
||||
profiles, defaultChatIdentity := chattask.Profiles(cfg)
|
||||
if len(profiles) > 0 {
|
||||
b.chatTasks = gateway.NewStoreTaskCreatorForProfiles(
|
||||
chatTaskWriterAdapter{enqueue: b.stateStore.EnqueueChatTask},
|
||||
chattask.Writer{Enqueue: b.stateStore.EnqueueChatTask},
|
||||
profiles,
|
||||
)
|
||||
}
|
||||
b.defaultChatIdentity = defaultChatIdentity
|
||||
}
|
||||
|
||||
func (b *boot) watchPersonas(ctx context.Context, version int64) error {
|
||||
func (b *server) watchPersonas(ctx context.Context, version int64) error {
|
||||
updates, err := b.controlPlane.WatchPersonas(ctx, version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go keepWatch(ctx, b.log, controlplane.PersonasKind, version, updates,
|
||||
go servicekit.KeepWatch(ctx, b.log, controlplane.PersonasKind, version, updates,
|
||||
b.controlPlane.WatchPersonas,
|
||||
waitFor,
|
||||
servicekit.WaitFor,
|
||||
func(update controlplane.AppliedPersonas) int64 { return update.Version },
|
||||
func(update controlplane.AppliedPersonas) {
|
||||
if update.Err != nil {
|
||||
@@ -112,7 +111,7 @@ func (b *boot) watchPersonas(ctx context.Context, version int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boot) applyPersonas(collection agent.PersonaCollection, version int64) {
|
||||
func (b *server) applyPersonas(collection agent.PersonaCollection, version int64) {
|
||||
personas := make([]gateway.Persona, 0, len(collection.Personas))
|
||||
for _, persona := range collection.Personas {
|
||||
personas = append(personas, gateway.Persona{Name: persona.Name, Prompt: persona.Prompt})
|
||||
@@ -123,21 +122,21 @@ func (b *boot) applyPersonas(collection agent.PersonaCollection, version int64)
|
||||
|
||||
// setupGatewayChat is the sole production constructor of the local contract.
|
||||
// Frontends use its gRPC representation; only this service owns the router.
|
||||
func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
|
||||
if err := b.setupChatRuntime(ctx, b.cfg); err != nil {
|
||||
func (b *server) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
|
||||
if err := b.setupChatRuntime(ctx, b.cfg, actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg := b.cfg
|
||||
router := gateway.NewRouter(b.stateStore, nil, "web")
|
||||
router.Limiter = b.rateLimiter
|
||||
router.Version = fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion)
|
||||
router.Version = fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", buildinfo.Version, buildinfo.Runtime)
|
||||
router.Models = b.chatModels
|
||||
router.Personas = b.personas
|
||||
router.Updates = b.updateService
|
||||
router.InitSessions(b.chatSessionStore)
|
||||
configureTaskCommands(router, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
||||
router.Health = b.statusHealth
|
||||
// ToolLimits reads the live holder, not cfg: the daemon's runtime-resource
|
||||
// ToolLimits reads the live holder, not cfg: the runtime-resource
|
||||
// watch republishes a tool-settings change through it, so a policy edit
|
||||
// applies to the next turn in the process that serves this runner.
|
||||
setup := chatSetup{
|
||||
+12
-16
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -24,14 +24,10 @@ type appliedMCPServer struct {
|
||||
}
|
||||
|
||||
// buildMCPProvider builds the tool-provider engine for one configured MCP
|
||||
// server. boot.mcpProvider overrides it in tests; production uses
|
||||
// configuredMCPProvider with the running config's work directory and the
|
||||
// daemon's sampling handler. Boot registration and the live reconciliation
|
||||
// server: configuredMCPProvider with the running config's work directory and the
|
||||
// Gateway's sampling handler. Boot registration and the live reconciliation
|
||||
// both reach the engine through here, so the two cannot build one differently.
|
||||
func (b *boot) buildMCPProvider(srv config.MCPServer) (toolprovider.Engine, error) {
|
||||
if b.mcpProvider != nil {
|
||||
return b.mcpProvider(srv)
|
||||
}
|
||||
func (b *server) buildMCPProvider(srv config.MCPServer) (toolprovider.Engine, error) {
|
||||
return configuredMCPProvider(srv, b.cfgHolder.Get().WorkDir, b.mcpSamplingHandler())
|
||||
}
|
||||
|
||||
@@ -40,9 +36,9 @@ func (b *boot) buildMCPProvider(srv config.MCPServer) (toolprovider.Engine, erro
|
||||
// server keeps running), the web_fetch and minimax tool entries (rebuilt,
|
||||
// because the entry captured the config at construction) and the tool-output
|
||||
// policy (whose spill directory is ensured here; the task snapshot reads the
|
||||
// policy fresh per dispatch and boot.toolLimits is read per call). A nil tool
|
||||
// policy fresh per dispatch and toolLimits is read per call). A nil tool
|
||||
// registry means this process built no tools, so only the config was layered.
|
||||
func (b *boot) reconcileToolSettings(ctx context.Context, cfg config.Config) error {
|
||||
func (b *server) reconcileToolSettings(ctx context.Context, cfg config.Config) error {
|
||||
if err := toolLimits(cfg).EnsureSpillDir(); err != nil {
|
||||
b.log.Warn("tool spill directory unavailable; large results will be truncated instead", "err", err)
|
||||
}
|
||||
@@ -74,7 +70,7 @@ func (b *boot) reconcileToolSettings(ctx context.Context, cfg config.Config) err
|
||||
// registration applies. A changed server whose new engine fails to start is
|
||||
// rolled back to the old one by the registry, so a refused change does not
|
||||
// leave the server down.
|
||||
func (b *boot) reconcileMCPServers(ctx context.Context, servers []config.MCPServer) error {
|
||||
func (b *server) reconcileMCPServers(ctx context.Context, servers []config.MCPServer) error {
|
||||
if b.providerRegistry == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -102,7 +98,7 @@ type mcpRegistryState struct {
|
||||
live map[string]bool
|
||||
}
|
||||
|
||||
func (b *boot) mcpRegistryState() mcpRegistryState {
|
||||
func (b *server) mcpRegistryState() mcpRegistryState {
|
||||
registered := make(map[string]bool)
|
||||
for _, id := range b.providerRegistry.RegisteredIDs() {
|
||||
registered[id] = true
|
||||
@@ -120,7 +116,7 @@ func (b *boot) mcpRegistryState() mcpRegistryState {
|
||||
|
||||
// disconnectAbsentMCPServers removes every applied server the stored document
|
||||
// no longer names.
|
||||
func (b *boot) disconnectAbsentMCPServers(ctx context.Context, desired map[string]config.MCPServer) []error {
|
||||
func (b *server) disconnectAbsentMCPServers(ctx context.Context, desired map[string]config.MCPServer) []error {
|
||||
var problems []error
|
||||
for name := range b.mcpApplied {
|
||||
if _, ok := desired[name]; ok {
|
||||
@@ -138,7 +134,7 @@ func (b *boot) disconnectAbsentMCPServers(ctx context.Context, desired map[strin
|
||||
|
||||
// applyDesiredMCPServers connects, reconnects or restarts the servers the
|
||||
// stored document names, skipping the ones already running unchanged.
|
||||
func (b *boot) applyDesiredMCPServers(ctx context.Context, desired map[string]config.MCPServer, state mcpRegistryState) []error {
|
||||
func (b *server) applyDesiredMCPServers(ctx context.Context, desired map[string]config.MCPServer, state mcpRegistryState) []error {
|
||||
names := make([]string, 0, len(desired))
|
||||
for name := range desired {
|
||||
names = append(names, name)
|
||||
@@ -170,7 +166,7 @@ func (b *boot) applyDesiredMCPServers(ctx context.Context, desired map[string]co
|
||||
// family runs, or still registered while it has not started; a
|
||||
// registered-but-not-running server on a running family is a refused earlier
|
||||
// start and is worth another attempt.
|
||||
func (b *boot) mcpServerCurrent(name string, srv config.MCPServer, state mcpRegistryState) bool {
|
||||
func (b *server) mcpServerCurrent(name string, srv config.MCPServer, state mcpRegistryState) bool {
|
||||
applied, ok := b.mcpApplied[name]
|
||||
if !ok || !reflect.DeepEqual(applied.server, srv) {
|
||||
return false
|
||||
@@ -183,7 +179,7 @@ func (b *boot) mcpServerCurrent(name string, srv config.MCPServer, state mcpRegi
|
||||
|
||||
// applyMCPProvider replaces a registered provider and adds an absent one,
|
||||
// whichever the registry needs for that id.
|
||||
func (b *boot) applyMCPProvider(ctx context.Context, engine toolprovider.Engine, state mcpRegistryState) error {
|
||||
func (b *server) applyMCPProvider(ctx context.Context, engine toolprovider.Engine, state mcpRegistryState) error {
|
||||
if state.registered[engine.Manifest().ID] {
|
||||
return b.providerRegistry.Replace(ctx, engine)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -12,12 +12,12 @@ import (
|
||||
)
|
||||
|
||||
// mcpSamplingHandler answers an MCP server's sampling/createMessage request
|
||||
// from the daemon's own chat model, so a server can ask for a completion
|
||||
// from the Gateway's chat model, so a server can ask for a completion
|
||||
// without archie opening a second provider path. A missing runtime or model
|
||||
// is a refused request, never a dropped one: the client maps the returned
|
||||
// error to a JSON-RPC error response for that request alone, leaving the
|
||||
// server session intact.
|
||||
func (b *boot) mcpSamplingHandler() mcp.SamplingHandler {
|
||||
func (b *server) mcpSamplingHandler() mcp.SamplingHandler {
|
||||
return func(ctx context.Context, req mcp.SamplingRequest) (mcp.SamplingResult, error) {
|
||||
llm := b.chatLLM()
|
||||
if llm == nil {
|
||||
@@ -0,0 +1,68 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory"
|
||||
)
|
||||
|
||||
func (b *server) setupMemoryEngine() error {
|
||||
cfg, log := b.cfg, b.log
|
||||
registry := domainmemory.NewRegistry(domainmemory.Registrar{Log: log})
|
||||
|
||||
switch cfg.Memory.Engine {
|
||||
case infraMemory.EngineName, "":
|
||||
root := filepath.Join(cfg.WorkDir, "memory-engine")
|
||||
if err := registry.Register(infraMemory.NewBuiltinEngine(root, 0)); err != nil {
|
||||
return fmt.Errorf("register memory engine %q: %w", infraMemory.EngineName, err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("memory.engine %q has no registered implementation", cfg.Memory.Engine)
|
||||
}
|
||||
|
||||
if err := registry.Start(context.Background()); err != nil {
|
||||
return fmt.Errorf("start memory engine registry: %w", err)
|
||||
}
|
||||
b.memEngines = registry
|
||||
b.addCleanup(func() {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := registry.Stop(shutdownCtx); err != nil {
|
||||
log.Error("memory engine registry shutdown", "err", err)
|
||||
}
|
||||
})
|
||||
log.Info("memory engine started", "engine", cfg.Memory.Engine)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *server) activeMemoryEngine() (domainmemory.MemoryEngine, bool) {
|
||||
if b.memEngines == nil {
|
||||
return nil, false
|
||||
}
|
||||
name := b.cfg.Memory.Engine
|
||||
if name == "" {
|
||||
name = infraMemory.EngineName
|
||||
}
|
||||
return b.memEngines.Get(name)
|
||||
}
|
||||
|
||||
func (b *server) memoryStore() gateway.MemoryStore {
|
||||
engine, ok := b.activeMemoryEngine()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return engine
|
||||
}
|
||||
|
||||
func (b *server) memoryWriter() gateway.MemoryWriteStore {
|
||||
engine, ok := b.activeMemoryEngine()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return engine
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/secretengine"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
"github.com/samcharles93/archie-core/internal/sdnotify"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
)
|
||||
|
||||
// Options contains process inputs for the standalone Gateway.
|
||||
type Options struct {
|
||||
Config string
|
||||
Overlay string
|
||||
// Listen is the gRPC listen address. Empty falls back to
|
||||
// [services.gateway].listen.
|
||||
Listen string
|
||||
// Token is the bearer token a non-loopback listener requires. Empty falls
|
||||
// back to [services.gateway].target_token.
|
||||
Token string
|
||||
}
|
||||
|
||||
// server is the Gateway process: the owner of conversations and of every
|
||||
// model call.
|
||||
type server struct {
|
||||
cfg config.Config
|
||||
cfgHolder *config.Holder
|
||||
log *slog.Logger
|
||||
taskLogs *logging.TaskRegistry
|
||||
|
||||
secrets *secret.Registry
|
||||
stateStore *staterpc.Client
|
||||
controlPlane *controlplane.Client
|
||||
applyStatus *applystatus.Reporter
|
||||
runtimeVersions map[string]int64
|
||||
catalog *servicekit.Catalog
|
||||
chatPool *pgxpool.Pool
|
||||
chatSessionStore gateway.SessionStore
|
||||
taskActionsConn *natsio.Conn
|
||||
|
||||
bus *events.Bus
|
||||
capabilityHost *plugin.Host
|
||||
rateLimiter *ratelimit.Limiter
|
||||
llm atomic.Pointer[runtime.Runtime]
|
||||
transcriber messaging.Transcriber
|
||||
toolReg *tools.Registry
|
||||
providerRegistry *toolprovider.Registry
|
||||
mcpMu sync.Mutex
|
||||
mcpApplied map[string]appliedMCPServer
|
||||
chatModels *chatModelManager
|
||||
personas *gateway.PersonaRegistry
|
||||
memEngines *domainmemory.Registry
|
||||
curatorRegistry *curator.Registry
|
||||
curatorRuntime *curator.Runtime
|
||||
providerOutcomes *providerOutcomeRecorder
|
||||
statusHealth gateway.HealthSource
|
||||
updateService *releaseupdate.Service
|
||||
|
||||
chatTasks gateway.TaskCreator
|
||||
chatController *gateway.StoreTaskController
|
||||
defaultChatIdentity string
|
||||
|
||||
cleanups []func()
|
||||
}
|
||||
|
||||
// Run serves the Gateway's chat contract until ctx ends.
|
||||
func Run(ctx context.Context, options Options) error {
|
||||
b := &server{log: slog.New(slog.NewJSONHandler(os.Stderr, nil))}
|
||||
defer b.cleanup()
|
||||
if err := b.loadConfig(options.Config, options.Overlay); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.openState(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
b.loadCatalog(ctx, options.Config)
|
||||
nc, err := b.connectNATS(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contract, err := b.startGatewayRuntime(ctx, taskactions.Client{Conn: nc, Timeout: 30 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listen, err := servicekit.ResolveListen("gateway", options.Listen, b.cfg.Services.Get(config.ServiceNameGateway).Listen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
token := options.Token
|
||||
if token == "" {
|
||||
token = b.cfg.Services.ResolvedToken(config.ServiceNameGateway, b.secrets.Getenv)
|
||||
}
|
||||
//nolint:contextcheck // grpc.StreamServerInterceptor has no context.Context parameter; gatewayrpc.StreamServerInterceptor derives its context from stream.Context() instead
|
||||
opts, loopback, err := gatewayServerOpts(listen, token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen for gateway: %w", err)
|
||||
}
|
||||
return b.serveGatewayListener(ctx, listener, loopback, contract, opts)
|
||||
}
|
||||
|
||||
func (b *server) addCleanup(fn func()) { b.cleanups = append(b.cleanups, fn) }
|
||||
|
||||
func (b *server) cleanup() {
|
||||
for _, fn := range slices.Backward(b.cleanups) {
|
||||
fn()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *server) chatLLM() *runtime.Runtime { return b.llm.Load() }
|
||||
func (b *server) setLLM(rt *runtime.Runtime) { b.llm.Store(rt) }
|
||||
func (b *server) announceReady() { sdnotify.Ready(b.log) }
|
||||
|
||||
func (b *server) loadConfig(cfgPath, overlayPath string) error {
|
||||
_, doc, err := servicekit.Resolve(b.log, cfgPath, overlayPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.cfg = doc.Config
|
||||
b.cfgHolder = config.NewHolder(b.cfg)
|
||||
logs := servicekit.Logging(b.cfg, "gateway")
|
||||
b.log, b.taskLogs = logs.Log, logs.TaskLogs
|
||||
b.addCleanup(func() { _ = logs.Closer.Close() })
|
||||
b.secrets = secret.NewRegistry()
|
||||
return nil
|
||||
}
|
||||
|
||||
// openState claims the conversation store, dials the State Store, starts the
|
||||
// enabled secret engines and layers the stored runtime settings.
|
||||
func (b *server) openState(ctx context.Context) error {
|
||||
if err := b.openChatSessions(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.claimGatewayOwnership(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
client, cleanup, err := servicekit.StateStoreClient(b.cfg.Services, b.secrets)
|
||||
if err != nil {
|
||||
return fmt.Errorf("state store client: %w", err)
|
||||
}
|
||||
b.addCleanup(cleanup)
|
||||
b.stateStore = client
|
||||
b.controlPlane = controlplane.NewRPCClient(client.ControlPlane())
|
||||
b.applyStatus = applystatus.New(applystatus.Gateway, client, b.log)
|
||||
source := secretengine.Source{Query: b.controlPlane, Packages: client}
|
||||
report := func(ctx context.Context, version int64, err error) {
|
||||
b.applyStatus.Report(ctx, controlplanerpc.ExtensionSettingsKind, version, err)
|
||||
}
|
||||
b.addCleanup(secretengine.Supervise(ctx, b.secrets, source, applystatus.Gateway, report, b.log).Close)
|
||||
cfg, versions, err := b.runtimeConfig(ctx, b.cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load runtime settings: %w", err)
|
||||
}
|
||||
b.cfg, b.runtimeVersions = cfg, versions
|
||||
b.cfgHolder.Set(cfg)
|
||||
go b.applyStatus.Run(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *server) runtimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) {
|
||||
return servicekit.RuntimeConfig(ctx, b.controlPlane, b.applyStatus, b.secrets, b.log, base, nil)
|
||||
}
|
||||
|
||||
// connectNATS joins the broker task actions travel over. The daemon owns the
|
||||
// embedded broker, so the Gateway waits for its endpoint rather than starting
|
||||
// a second one.
|
||||
func (b *server) connectNATS(ctx context.Context) (*natsio.Conn, error) {
|
||||
url, token := b.cfg.NATS.URL, ""
|
||||
if b.cfg.NATS.Mode == config.NATSModeExternal {
|
||||
var err error
|
||||
if token, err = servicekit.NATSToken(b.cfg.NATS, os.Getenv); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
endpoint, err := b.waitForNATSEndpoint(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url, token = endpoint.URL, endpoint.Token
|
||||
}
|
||||
nc, err := natsio.Connect(url, natsio.Token(token), natsio.MaxReconnects(-1))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect gateway task actions: %w", err)
|
||||
}
|
||||
b.addCleanup(nc.Close)
|
||||
b.taskActionsConn = nc
|
||||
return nc, nil
|
||||
}
|
||||
|
||||
func (b *server) waitForNATSEndpoint(ctx context.Context) (servicekit.NATSEndpoint, error) {
|
||||
for {
|
||||
endpoint, err := servicekit.ReadNATSEndpoint(b.cfg.StateDir)
|
||||
if err == nil {
|
||||
return endpoint, nil
|
||||
}
|
||||
b.log.Warn("waiting for the daemon's NATS endpoint", "err", err)
|
||||
if !servicekit.WaitFor(ctx, 2*time.Second) {
|
||||
return servicekit.NATSEndpoint{}, ctx.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *server) catalogState() (modelcatalog.Snapshot, []string) { return b.catalog.State() }
|
||||
|
||||
// loadCatalog layers the model catalog under the running config. A catalog
|
||||
// that cannot be read leaves the configured providers and models in effect.
|
||||
func (b *server) loadCatalog(ctx context.Context, cfgPath string) {
|
||||
b.catalog = servicekit.NewCatalog(cfgPath)
|
||||
snapshot, err := b.catalog.Fetch(ctx, b.secrets.Getenv, b.cfg.Providers)
|
||||
if err != nil {
|
||||
b.log.Warn("model catalog unavailable; using configured providers and models", "err", err)
|
||||
return
|
||||
}
|
||||
models := servicekit.ApplyModelCatalog(&b.cfg, snapshot)
|
||||
b.cfgHolder.Set(b.cfg.Clone())
|
||||
b.catalog.Set(snapshot, models)
|
||||
}
|
||||
|
||||
func (b *server) startModelCatalogRefresh(ctx context.Context) {
|
||||
servicekit.Every(ctx, servicekit.CatalogRefreshInterval, func() {
|
||||
snapshot, err := b.catalog.Fetch(ctx, b.secrets.Getenv, b.cfgHolder.Get().Providers)
|
||||
if err != nil {
|
||||
b.log.Warn("model catalog refresh failed; keeping the loaded catalog", "err", err)
|
||||
return
|
||||
}
|
||||
base := b.cfgHolder.Get().Clone()
|
||||
models := servicekit.ApplyModelCatalog(&base, snapshot)
|
||||
b.catalog.Set(snapshot, models)
|
||||
b.relayer(ctx, base, "")
|
||||
})
|
||||
}
|
||||
|
||||
// gatewayRuntimeKinds are the kinds the Gateway applies live: the settings its
|
||||
// chat runtime and tool providers are built from.
|
||||
var gatewayRuntimeKinds = []string{
|
||||
controlplane.ProviderSettingsKind,
|
||||
controlplane.ModelRoleAssignmentsKind,
|
||||
controlplane.ToolSettingsKind,
|
||||
}
|
||||
|
||||
func (b *server) startRuntimeWatches(ctx context.Context) error {
|
||||
for _, kind := range gatewayRuntimeKinds {
|
||||
updates, err := b.controlPlane.WatchResource(ctx, kind, b.runtimeVersions[kind])
|
||||
if err != nil {
|
||||
return fmt.Errorf("watch %s: %w", kind, err)
|
||||
}
|
||||
go servicekit.KeepWatch(ctx, b.log, kind, b.runtimeVersions[kind], updates,
|
||||
func(ctx context.Context, afterVersion int64) (<-chan controlplane.AppliedResource, error) {
|
||||
return b.controlPlane.WatchResource(ctx, kind, afterVersion)
|
||||
},
|
||||
servicekit.WaitFor,
|
||||
func(update controlplane.AppliedResource) int64 { return update.Version },
|
||||
func(update controlplane.AppliedResource) {
|
||||
if update.Err != nil {
|
||||
b.log.Error("runtime settings watch failed", "kind", kind, "err", update.Err)
|
||||
return
|
||||
}
|
||||
base := b.cfgHolder.Get()
|
||||
catalog, _ := b.catalogState()
|
||||
servicekit.ApplyModelCatalog(&base, catalog)
|
||||
b.relayer(ctx, base, kind)
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// relayer re-runs the runtime layering over base and applies the result to the
|
||||
// chat runtime and, for tool settings, the tool providers. A refused document
|
||||
// leaves the running settings in place.
|
||||
func (b *server) relayer(ctx context.Context, base config.Config, kind string) {
|
||||
cfg, versions, err := b.runtimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
b.log.Error("runtime settings update rejected; the running settings stay", "kind", kind, "err", err)
|
||||
return
|
||||
}
|
||||
b.cfgHolder.Set(cfg)
|
||||
b.rebuildChatModelRuntime(cfg)
|
||||
if kind == controlplane.ToolSettingsKind {
|
||||
if err := b.reconcileToolSettings(ctx, cfg); err != nil {
|
||||
b.applyStatus.Report(ctx, kind, versions[kind], err)
|
||||
b.log.Error("tool settings reconciled with errors; the running servers stay", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-11
@@ -7,7 +7,7 @@
|
||||
// production constructor of a Router and it runs in the Gateway, which owns
|
||||
// neither a container pool, a poll loop nor a channel manager. A producer
|
||||
// wired for one of the three would build a section no surface ever reads.
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"sync"
|
||||
@@ -80,9 +80,8 @@ func (r *providerOutcomeRecorder) LastChatModelOutcome() (gateway.ChatModelOutco
|
||||
// subsystems. Every source is optional; a nil source means this process holds
|
||||
// no truthful input for that fact and the section is left out of the report.
|
||||
//
|
||||
// The broker source is a function rather than a captured value because the
|
||||
// connection is process state: the daemon's shared eventbus client in one
|
||||
// composition, the Gateway's own task-actions connection in the other.
|
||||
// The broker source is a function so /status reads the connection's state at
|
||||
// request time.
|
||||
type statusHealth struct {
|
||||
broker func() (connected, ok bool)
|
||||
chatModel *providerOutcomeRecorder
|
||||
@@ -112,18 +111,14 @@ func (s statusHealth) Health() gateway.HealthReport {
|
||||
// constructor of a Router; it runs in the Gateway, which holds none of the
|
||||
// three, so a producer reading them would build a section no /status reply can
|
||||
// carry.
|
||||
func newStatusHealth(b *boot) gateway.HealthSource {
|
||||
func newStatusHealth(b *server) gateway.HealthSource {
|
||||
return statusHealth{
|
||||
chatModel: b.providerOutcomes,
|
||||
broker: func() (bool, bool) {
|
||||
switch {
|
||||
case b.natsClient != nil:
|
||||
return b.natsClient.Connected(), true
|
||||
case b.taskActionsConn != nil:
|
||||
return b.taskActionsConn.IsConnected(), true
|
||||
default:
|
||||
if b.taskActionsConn == nil {
|
||||
return false, false
|
||||
}
|
||||
return b.taskActionsConn.IsConnected(), true
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/skill"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
"github.com/samcharles93/archie-core/internal/tools/minimax"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
||||
mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
|
||||
"github.com/samcharles93/archie-core/internal/tools/sendfile"
|
||||
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
||||
)
|
||||
|
||||
func (b *server) registerTools() error {
|
||||
cfg, log := b.cfgHolder.Get(), b.log
|
||||
b.providerRegistry = toolprovider.NewRegistry(b.toolReg)
|
||||
b.mcpMu.Lock()
|
||||
b.mcpApplied = make(map[string]appliedMCPServer, len(cfg.Tools.MCPServers))
|
||||
b.mcpMu.Unlock()
|
||||
if workspace := cfg.Chat.Workspace; workspace != "" {
|
||||
unrestricted := cfg.Chat.UnrestrictedFilesystem
|
||||
if err := b.providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted)); err != nil {
|
||||
log.Error("workspace tool provider registration failed", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("workspace tools enabled",
|
||||
"workspace", workspace, "unrestricted_filesystem", unrestricted)
|
||||
} else {
|
||||
log.Info("workspace tools disabled (chat.workspace is unset)")
|
||||
}
|
||||
for _, srv := range cfg.Tools.MCPServers {
|
||||
provider, err := b.buildMCPProvider(srv)
|
||||
if err != nil {
|
||||
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := b.providerRegistry.RegisterOptional(provider); err != nil {
|
||||
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
b.mcpMu.Lock()
|
||||
b.mcpApplied[strings.TrimSpace(srv.Name)] = appliedMCPServer{server: srv, id: provider.Manifest().ID}
|
||||
b.mcpMu.Unlock()
|
||||
}
|
||||
if err := b.capabilityHost.Register(b.providerRegistry); err != nil {
|
||||
log.Error("tool-provider capability registration failed", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *server) registerStandaloneTools() {
|
||||
cfg, log := b.cfg, b.log
|
||||
|
||||
if catalog, err := skill.CatalogRoots(skill.DefaultRoots(cfg.WorkDir, cfg.SkillsDir)...); err != nil {
|
||||
log.Warn("skill catalog load failed", "err", err)
|
||||
} else if entry := skill.ActivateTool(cfg.WorkDir, catalog); entry != nil {
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("skill_activate registration failed", "err", err)
|
||||
} else {
|
||||
log.Info("skill catalog registered", "skills", len(catalog))
|
||||
}
|
||||
}
|
||||
|
||||
if spillDir := cfg.Tools.Policy.SpillDir; spillDir != "" {
|
||||
if err := toolLimits(cfg).EnsureSpillDir(); err != nil {
|
||||
log.Warn("tool spill directory unavailable; large results will be truncated instead", "err", err)
|
||||
} else if ws := cfg.Chat.Workspace; ws != "" && !cfg.Chat.UnrestrictedFilesystem && !isWithin(ws, spillDir) {
|
||||
log.Warn("tool spill directory is outside chat.workspace; the model cannot read back what is spilled there",
|
||||
"spill_dir", spillDir, "workspace", ws)
|
||||
}
|
||||
}
|
||||
|
||||
b.registerWebFetchTool(cfg)
|
||||
|
||||
if entry := sendfile.Tool(cfg.Chat.Workspace); entry != nil {
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("send_file registration failed", "err", err)
|
||||
} else {
|
||||
log.Info("file sending enabled", "workspace", cfg.Chat.Workspace)
|
||||
}
|
||||
} else {
|
||||
log.Info("file sending disabled (chat.workspace is unset)")
|
||||
}
|
||||
|
||||
b.registerMinimaxTool(cfg, log)
|
||||
}
|
||||
|
||||
func (b *server) registerWebFetchTool(cfg config.Config) {
|
||||
log := b.log
|
||||
entry := webfetch.Tool(webfetch.Config{
|
||||
Enabled: cfg.Tools.WebFetch.IsEnabled(),
|
||||
Timeout: cfg.Tools.WebFetch.Timeout.Std(),
|
||||
MaxBytes: cfg.Tools.WebFetch.MaxBytes,
|
||||
AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks,
|
||||
})
|
||||
if entry == nil {
|
||||
log.Info("web fetch disabled")
|
||||
return
|
||||
}
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("web_fetch registration failed", "err", err)
|
||||
return
|
||||
}
|
||||
log.Info("web fetch enabled",
|
||||
"allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks)
|
||||
}
|
||||
|
||||
func (b *server) registerMinimaxTool(cfg config.Config, log *slog.Logger) {
|
||||
if !cfg.Tools.Minimax.IsEnabled() {
|
||||
log.Info("minimax video generation disabled")
|
||||
return
|
||||
}
|
||||
|
||||
apiKey, err := b.secrets.Resolve(cfg.Tools.Minimax.APIKey)
|
||||
if err != nil {
|
||||
log.Warn("minimax video generation enabled but the API key failed to resolve; tool not registered", "err", err)
|
||||
return
|
||||
}
|
||||
if apiKey == "" {
|
||||
log.Warn("minimax video generation enabled but no API key is configured; tool not registered")
|
||||
return
|
||||
}
|
||||
|
||||
entry := minimax.Tool(minimax.Config{Enabled: true, APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL})
|
||||
if entry == nil {
|
||||
return
|
||||
}
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("generate_video registration failed", "err", err)
|
||||
return
|
||||
}
|
||||
log.Info("minimax video generation enabled")
|
||||
}
|
||||
|
||||
// toolLimits reads the configured per-turn result limits.
|
||||
func toolLimits(cfg config.Config) agentexec.ToolLimits {
|
||||
return agentexec.ToolLimits{
|
||||
MaxResultChars: cfg.Tools.Policy.MaxResultChars,
|
||||
SpillDir: cfg.Tools.Policy.SpillDir,
|
||||
}
|
||||
}
|
||||
|
||||
// isWithin reports whether target sits inside base.
|
||||
func isWithin(base, target string) bool {
|
||||
absBase, err := filepath.Abs(base)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
absTarget, err := filepath.Abs(target)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
rel, err := filepath.Rel(absBase, absTarget)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
|
||||
}
|
||||
|
||||
func configuredMCPProvider(server config.MCPServer, workDir string, sampling mcp.SamplingHandler) (toolprovider.Engine, error) {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("MCP server name is required")
|
||||
}
|
||||
transportType := strings.ToLower(strings.TrimSpace(server.Transport))
|
||||
if transportType == "" {
|
||||
transportType = "stdio"
|
||||
}
|
||||
|
||||
samplingOption := mcptoolprovider.WithSamplingHandler(sampling)
|
||||
|
||||
switch transportType {
|
||||
case "stdio":
|
||||
command := strings.TrimSpace(server.Command)
|
||||
if command == "" {
|
||||
return nil, fmt.Errorf("MCP stdio server %q requires a command", name)
|
||||
}
|
||||
transport := mcp.NewStdioTransport(mcp.StdioTransportConfig{
|
||||
Command: command,
|
||||
Args: append([]string(nil), server.Args...),
|
||||
Dir: server.WorkDir,
|
||||
Env: npmCacheServerEnv(command, workDir),
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "http", "streamablehttp":
|
||||
url := strings.TrimSpace(server.URL)
|
||||
if url == "" {
|
||||
return nil, fmt.Errorf("MCP http server %q requires a url", name)
|
||||
}
|
||||
transport := mcp.NewHTTPTransport(mcp.HTTPTransportConfig{
|
||||
Endpoint: url,
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "sse":
|
||||
sseEndpoint := strings.TrimSpace(server.SSEEndpoint)
|
||||
if sseEndpoint == "" {
|
||||
return nil, fmt.Errorf("MCP sse server %q requires an sse_endpoint", name)
|
||||
}
|
||||
transport := mcp.NewSSETransport(mcp.SSETransportConfig{
|
||||
SSEEndpoint: sseEndpoint,
|
||||
MessageEndpoint: strings.TrimSpace(server.MessageEndpoint),
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("MCP transport %q is not supported", transportType)
|
||||
}
|
||||
}
|
||||
|
||||
// npmCacheServerEnv returns environment variables that make an
|
||||
// npx-launched MCP server reuse a persistent package cache across Gateway
|
||||
// restarts, instead of npm re-resolving and re-downloading the package
|
||||
// from the registry every time the Gateway starts. Rooted under the
|
||||
// work dir so it persists regardless of whether the Gateway
|
||||
// itself runs in an ephemeral container. See mcp.NpmCacheEnv for the
|
||||
// command-matching and env var details shared with archie-agent's
|
||||
// per-task equivalent (internal/app/agentworker/mcp_providers.go), which
|
||||
// points at a mounted cache volume instead of a work-dir subdirectory.
|
||||
//
|
||||
// workDir is unconditionally defaulted before registerTools runs
|
||||
// (internal/infrastructure/configuration's applyGeneralDefaults), but
|
||||
// filepath.Join silently accepts an empty string and would then put the
|
||||
// cache in the process's current working directory instead of its
|
||||
// persistent data directory -- guard it explicitly rather than trust that
|
||||
// invariant here too.
|
||||
func npmCacheServerEnv(command, workDir string) []string {
|
||||
if workDir == "" {
|
||||
return nil
|
||||
}
|
||||
return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache"))
|
||||
}
|
||||
|
||||
func shutdownCapabilityHost(capabilityHost *plugin.Host, log *slog.Logger) func() {
|
||||
return func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := capabilityHost.Stop(stopCtx); err != nil {
|
||||
log.Error("capability host shutdown", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"slices"
|
||||
@@ -0,0 +1,70 @@
|
||||
// Package chattask derives the chat task profiles a config allows and adapts
|
||||
// the State Store's enqueue call to the gateway's task writer.
|
||||
package chattask
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
)
|
||||
|
||||
// Writer adapts an enqueue call returning the task to gateway.TaskWriter.
|
||||
type Writer struct {
|
||||
Enqueue func(
|
||||
ctx context.Context,
|
||||
owner, repo, title, body, workflow, identity string,
|
||||
inputs map[string]any,
|
||||
) (*workflow.Task, error)
|
||||
}
|
||||
|
||||
func (a Writer) EnqueueChatTask(
|
||||
ctx context.Context,
|
||||
owner, repo, title, body, workflow, identity string,
|
||||
inputs map[string]any,
|
||||
) (int64, error) {
|
||||
task, err := a.Enqueue(ctx, owner, repo, title, body, workflow, identity, inputs)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if task == nil {
|
||||
return 0, fmt.Errorf("enqueue chat task returned no task")
|
||||
}
|
||||
return task.ID, nil
|
||||
}
|
||||
|
||||
// Profiles returns the task profiles cfg allows and the default identity.
|
||||
func Profiles(cfg config.Config) ([]gateway.TaskProfile, string) {
|
||||
if len(cfg.Identities) > 0 {
|
||||
profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities))
|
||||
for _, identity := range cfg.Identities {
|
||||
if len(identity.Repos) == 0 {
|
||||
continue
|
||||
}
|
||||
profiles = append(profiles, newProfile(identity.Name, identity.Repos))
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
return nil, ""
|
||||
}
|
||||
return profiles, profiles[0].Identity
|
||||
}
|
||||
if len(cfg.Repos) == 0 {
|
||||
return nil, ""
|
||||
}
|
||||
return []gateway.TaskProfile{newProfile("", cfg.Repos)}, ""
|
||||
}
|
||||
|
||||
func newProfile(identity string, repos []config.Repo) gateway.TaskProfile {
|
||||
allowed := make([]string, 0, len(repos))
|
||||
for _, repo := range repos {
|
||||
allowed = append(allowed, repo.Owner+"/"+repo.Name)
|
||||
}
|
||||
return gateway.TaskProfile{
|
||||
Identity: identity,
|
||||
DefaultOwner: repos[0].Owner,
|
||||
DefaultRepo: repos[0].Name,
|
||||
Repos: allowed,
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/prreview/bench"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
@@ -72,7 +72,7 @@ func Run(ctx context.Context, opts Options, w io.Writer) (bench.Summary, error)
|
||||
if err != nil {
|
||||
return bench.Summary{}, err
|
||||
}
|
||||
if err := archied.ResolveProviders(&doc.Config, slog.Default()); err != nil {
|
||||
if err := servicekit.ResolveProviders(&doc.Config, slog.Default()); err != nil {
|
||||
return bench.Summary{}, err
|
||||
}
|
||||
rt := agentexec.NewRuntime(agentexec.ProvidersFromConfig(doc.Config.Providers))
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package servicekit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maps"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// CatalogRefreshInterval is how often a running service re-reads the catalog.
|
||||
const CatalogRefreshInterval = time.Hour
|
||||
|
||||
// Catalog holds the last model catalog a service loaded.
|
||||
type Catalog struct {
|
||||
cachePath string
|
||||
|
||||
mu sync.RWMutex
|
||||
snapshot modelcatalog.Snapshot
|
||||
models []string
|
||||
}
|
||||
|
||||
// NewCatalog caches the catalog beside the config file at cfgPath.
|
||||
func NewCatalog(cfgPath string) *Catalog {
|
||||
return &Catalog{cachePath: filepath.Join(filepath.Dir(cfgPath), "models.json")}
|
||||
}
|
||||
|
||||
// State returns the loaded snapshot and its model references.
|
||||
func (c *Catalog) State() (modelcatalog.Snapshot, []string) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.snapshot, c.models
|
||||
}
|
||||
|
||||
// Set records a loaded snapshot and its model references.
|
||||
func (c *Catalog) Set(snapshot modelcatalog.Snapshot, models []string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.snapshot, c.models = snapshot, models
|
||||
}
|
||||
|
||||
// Fetch reads the catalog, resolving provider keys through getenv.
|
||||
func (c *Catalog) Fetch(ctx context.Context, getenv func(string) string, configured map[string]config.Provider) (modelcatalog.Snapshot, error) {
|
||||
return modelcatalog.Load(ctx, modelcatalog.Options{CachePath: c.cachePath, Getenv: getenv, Configured: configured})
|
||||
}
|
||||
|
||||
// Every runs fn every interval until ctx ends.
|
||||
func Every(ctx context.Context, interval time.Duration, fn func()) {
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
fn()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// ApplyModelCatalog layers the catalog's providers and model limits under cfg's
|
||||
// own and returns the catalog's model references, sorted.
|
||||
func ApplyModelCatalog(cfg *config.Config, snapshot modelcatalog.Snapshot) []string {
|
||||
discovered := make(map[string]config.Provider, len(snapshot.Providers))
|
||||
limits := make(map[string]config.ModelLimits)
|
||||
var models []string
|
||||
for _, provider := range snapshot.Providers {
|
||||
discovered[provider.ID] = config.Provider{
|
||||
Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, BaseURL: provider.BaseURL,
|
||||
}
|
||||
for _, model := range provider.Models {
|
||||
ref := provider.ID + "/" + model.ID
|
||||
models = append(models, ref)
|
||||
limits[ref] = config.ModelLimits{ContextWindow: model.ContextWindow, MaxOutputTokens: model.MaxOutputTokens, Reasoning: model.Reasoning}
|
||||
}
|
||||
}
|
||||
cfg.ModelLimits = limits
|
||||
cfg.Providers = mergeProviders(discovered, cfg.Providers)
|
||||
for i := range cfg.Identities {
|
||||
cfg.Identities[i].Providers = mergeProviders(discovered, cfg.Identities[i].Providers)
|
||||
}
|
||||
slices.Sort(models)
|
||||
return models
|
||||
}
|
||||
|
||||
func mergeProviders(base, overrides map[string]config.Provider) map[string]config.Provider {
|
||||
merged := make(map[string]config.Provider, len(base)+len(overrides))
|
||||
maps.Copy(merged, base)
|
||||
for id, override := range overrides {
|
||||
resolved := merged[id]
|
||||
if override.Class != "" {
|
||||
resolved.Class = override.Class
|
||||
}
|
||||
if override.APIKeyEnv != "" {
|
||||
resolved.APIKeyEnv = override.APIKeyEnv
|
||||
}
|
||||
if override.APIKey != (secret.SecretRef{}) {
|
||||
resolved.APIKey = override.APIKey
|
||||
}
|
||||
if override.BaseURL != "" {
|
||||
resolved.BaseURL = override.BaseURL
|
||||
}
|
||||
merged[id] = resolved
|
||||
}
|
||||
return merged
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
package archied
|
||||
package servicekit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
type embeddedNATSEndpoint struct {
|
||||
type NATSEndpoint struct {
|
||||
URL string `json:"url"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
@@ -16,9 +18,9 @@ func embeddedNATSEndpointPath(stateDir string) string {
|
||||
return filepath.Join(stateDir, "nats", "endpoint.json")
|
||||
}
|
||||
|
||||
func writeEmbeddedNATSEndpoint(stateDir, url, token string) error {
|
||||
func WriteNATSEndpoint(stateDir, url, token string) error {
|
||||
path := embeddedNATSEndpointPath(stateDir)
|
||||
data, err := json.Marshal(embeddedNATSEndpoint{URL: url, Token: token})
|
||||
data, err := json.Marshal(NATSEndpoint{URL: url, Token: token})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal embedded NATS endpoint: %w", err)
|
||||
}
|
||||
@@ -48,17 +50,28 @@ func writeEmbeddedNATSEndpoint(stateDir, url, token string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readEmbeddedNATSEndpoint(stateDir string) (embeddedNATSEndpoint, error) {
|
||||
func ReadNATSEndpoint(stateDir string) (NATSEndpoint, error) {
|
||||
data, err := os.ReadFile(embeddedNATSEndpointPath(stateDir))
|
||||
if err != nil {
|
||||
return embeddedNATSEndpoint{}, err
|
||||
return NATSEndpoint{}, err
|
||||
}
|
||||
var endpoint embeddedNATSEndpoint
|
||||
var endpoint NATSEndpoint
|
||||
if err := json.Unmarshal(data, &endpoint); err != nil {
|
||||
return embeddedNATSEndpoint{}, fmt.Errorf("decode embedded NATS endpoint: %w", err)
|
||||
return NATSEndpoint{}, fmt.Errorf("decode embedded NATS endpoint: %w", err)
|
||||
}
|
||||
if endpoint.URL == "" || endpoint.Token == "" {
|
||||
return embeddedNATSEndpoint{}, fmt.Errorf("embedded NATS endpoint is incomplete")
|
||||
return NATSEndpoint{}, fmt.Errorf("embedded NATS endpoint is incomplete")
|
||||
}
|
||||
return endpoint, nil
|
||||
}
|
||||
|
||||
func NATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) {
|
||||
if cfg.TokenEnv == "" {
|
||||
return "", nil
|
||||
}
|
||||
token := getenv(cfg.TokenEnv)
|
||||
if token == "" {
|
||||
return "", fmt.Errorf("%s is required when nats.token_env is configured", cfg.TokenEnv)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package servicekit
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
func resolveProviderSecrets(cfg *config.Config, registry *secret.Registry, log *slog.Logger) error {
|
||||
func ResolveProviderSecrets(cfg *config.Config, registry *secret.Registry, log *slog.Logger) error {
|
||||
if err := resolveProviderMap("root", cfg.Providers, registry, log); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -92,5 +92,5 @@ func providerSecretEnvName(scope, providerID string) string {
|
||||
// at boot, for a tool that calls models outside the daemon. Only the env engine
|
||||
// is available there: extension engines run inside the processes that supervise them.
|
||||
func ResolveProviders(cfg *config.Config, log *slog.Logger) error {
|
||||
return resolveProviderSecrets(cfg, secret.NewRegistry(), log)
|
||||
return ResolveProviderSecrets(cfg, secret.NewRegistry(), log)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package servicekit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// RuntimeConfig layers the control plane's stored settings over base, lets
|
||||
// adjust apply the caller's own live settings, then validates the result and
|
||||
// resolves its provider secrets. The outcome is reported for every layered
|
||||
// kind, so a refused document keeps the version still running on the record.
|
||||
func RuntimeConfig(ctx context.Context, cp *controlplane.Client, status *applystatus.Reporter, secrets *secret.Registry, log *slog.Logger, base config.Config, adjust func(*config.Config)) (config.Config, map[string]int64, error) {
|
||||
cfg, versions, err := cp.RuntimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
if adjust != nil {
|
||||
adjust(&cfg)
|
||||
}
|
||||
report := func(applyErr error) {
|
||||
for kind, version := range versions {
|
||||
status.Report(ctx, kind, version, applyErr)
|
||||
}
|
||||
}
|
||||
if err := configuration.Validate(&cfg); err != nil {
|
||||
wrapped := fmt.Errorf("validate database settings: %w", err)
|
||||
report(wrapped)
|
||||
return config.Config{}, nil, wrapped
|
||||
}
|
||||
if err := ResolveProviderSecrets(&cfg, secrets, log); err != nil {
|
||||
report(err)
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
report(nil)
|
||||
return cfg, versions, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package servicekit
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// composeStateStoreClient dials the State Store gRPC service the daemon's own
|
||||
// StateStoreClient dials the State Store gRPC service the daemon's own
|
||||
// capture/mapping/binding consumers call. Token resolution is the daemon's
|
||||
// (it owns the config keys and the secret registry); the dial itself and the
|
||||
// fail-closed non-loopback rule belong to the transport, so they come
|
||||
// from staterpc.Dial.
|
||||
func composeStateStoreClient(services config.Services, secrets *secret.Registry) (*staterpc.Client, func(), error) {
|
||||
func StateStoreClient(services config.Services, secrets *secret.Registry) (*staterpc.Client, func(), error) {
|
||||
target, err := services.RequireTarget(config.ServiceNameState)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -1,4 +1,4 @@
|
||||
package archied
|
||||
package servicekit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -44,11 +44,11 @@ const (
|
||||
// every attempt, which is the fixed-rate loop this rule exists to charge.
|
||||
const controlPlaneWatchHealthyMultiple = 2
|
||||
|
||||
// keepWatch delivers every update from first and, when that stream ends,
|
||||
// KeepWatch delivers every update from first and, when that stream ends,
|
||||
// re-establishes it in this same goroutine until ctx ends.
|
||||
//
|
||||
// A control-plane watch stream is not permanent. The State Store restarts, the
|
||||
// connection drops, or the server closes the stream. keepWatch reopens it in
|
||||
// connection drops, or the server closes the stream. KeepWatch reopens it in
|
||||
// place; workflow-execution-settings also reports the gap so apply status does
|
||||
// not keep re-stamping the previous successful version as current while no
|
||||
// live updates can arrive.
|
||||
@@ -106,13 +106,13 @@ const controlPlaneWatchHealthyMultiple = 2
|
||||
// document reaches the settings page.
|
||||
//
|
||||
// wait is the delay between a stream ending and the reopen that follows it, and
|
||||
// production passes waitFor. It is injected so that the delay the loop hands it
|
||||
// production passes WaitFor. It is injected so that the delay the loop hands it
|
||||
// is observable as a value: the retry_in log line states the delay the loop
|
||||
// charged, and a loop that charged one delay and waited another would leave
|
||||
// that attribute agreeing with itself. What the injected wait cannot show is a
|
||||
// multiple taken inside waitFor itself, which is a timer for exactly the d it
|
||||
// multiple taken inside WaitFor itself, which is a timer for exactly the d it
|
||||
// is given.
|
||||
func keepWatch[T any](
|
||||
func KeepWatch[T any](
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
kind string,
|
||||
@@ -217,10 +217,10 @@ func nextUpdate[T any](ctx context.Context, stream <-chan T) (T, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// waitFor is the wait production injects into keepWatch. It waits for d,
|
||||
// WaitFor is the wait production injects into KeepWatch. It waits for d,
|
||||
// reporting false when ctx ended first: a watch's backoff must never delay the
|
||||
// shutdown of the process that owns it.
|
||||
func waitFor(ctx context.Context, d time.Duration) bool {
|
||||
func WaitFor(ctx context.Context, d time.Duration) bool {
|
||||
timer := time.NewTimer(d)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
@@ -258,9 +258,7 @@ install -m755 "$source_dir/scripts/archie-update-check" "$BIN_DIR/archie-update-
|
||||
# update to a release predating one of these processes stays buildable.
|
||||
built=""
|
||||
if [[ ",$components," == *,daemon,* ]]; then
|
||||
ldflags="-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion=$GW"
|
||||
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=$RT"
|
||||
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary"
|
||||
ldflags="-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary"
|
||||
# The updater is a build site too: without this stamp its own outputs would
|
||||
# report "dev" and fail the skew check it just installed.
|
||||
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/buildinfo.Version=$GW"
|
||||
|
||||
Reference in new issue
Block a user