Task containers log in with their run credential on an external broker and reach only their own subjects, decided by the embedded broker's authenticator. The broker token is never put in a container environment.
202 lines
7.9 KiB
Go
202 lines
7.9 KiB
Go
// Package agentworker composes and runs Archie's NATS-connected agent worker.
|
|
package agentworker
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/agentboot"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/agentgit"
|
|
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
|
"github.com/samcharles93/archie-core/internal/logging"
|
|
"github.com/samcharles93/archie-core/internal/storage"
|
|
"github.com/samcharles93/archie-core/internal/taskrun"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
)
|
|
|
|
const rpcTimeout = 60 * time.Second
|
|
|
|
// StartupError reports which worker startup operation failed. The application
|
|
// logs the operation-specific failure once; the command only maps it to exit 1.
|
|
type StartupError struct {
|
|
Operation string
|
|
Err error
|
|
}
|
|
|
|
func (e *StartupError) Error() string { return e.Operation + ": " + e.Err.Error() }
|
|
func (e *StartupError) Unwrap() error { return e.Err }
|
|
|
|
// Settings are process-derived inputs for one worker runtime.
|
|
type Settings struct {
|
|
NATSURL string
|
|
WorkDir string
|
|
|
|
// StateStoreTarget and StateStoreToken are the daemon-injected State Store
|
|
// gRPC endpoint and bearer token. StateStoreTarget is required: the legacy
|
|
// NATS storerpc path is deleted.
|
|
StateStoreTarget string
|
|
StateStoreToken string
|
|
// StateStoreCA is the PEM CA the State Store's certificate chains to.
|
|
StateStoreCA []byte
|
|
|
|
// ModelProxy, ModelProxyHosts and ModelProxyCA route model provider
|
|
// requests through the daemon's egress proxy, which holds the keys.
|
|
// Empty ModelProxy sends them directly.
|
|
ModelProxy string
|
|
ModelProxyHosts []string
|
|
ModelProxyCA string
|
|
}
|
|
|
|
type workerTransport interface {
|
|
taskServiceTransport
|
|
Close()
|
|
LogPublisher() agentexec.LogPublisher
|
|
SubscribeTasks(context.Context, int64, agentnats.TaskHandler, *slog.Logger) (agentnats.Subscription, error)
|
|
}
|
|
|
|
type workerDependencies struct {
|
|
connect func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error)
|
|
markSafe func(context.Context, string, *slog.Logger) bool
|
|
bootID func(string) (int64, error)
|
|
wait func(context.Context, *slog.Logger)
|
|
// steps is the workflow step vocabulary this process compiles pinned
|
|
// definitions against, registered by the composition root before the first
|
|
// task is served (infrastructure/workflowsteps is the provider set).
|
|
steps *workflow.Manager
|
|
// guardrails is the guardrail engine every served task records agent-stage
|
|
// outcomes against, built once at this process's composition root.
|
|
// archie-agent is the process that runs the stages, so the engine that
|
|
// observes them lives here; a live engine cannot cross the task handoff.
|
|
guardrails *tools.GuardrailEngine
|
|
}
|
|
|
|
// productionWorkerDependencies builds the worker's process dependencies.
|
|
func productionWorkerDependencies() (workerDependencies, error) {
|
|
steps, err := workflowsteps.NewManager()
|
|
if err != nil {
|
|
return workerDependencies{}, err
|
|
}
|
|
return workerDependencies{
|
|
connect: func(ctx context.Context, config agentnats.Config, log *slog.Logger) (workerTransport, error) {
|
|
return agentnats.Connect(ctx, config, log)
|
|
},
|
|
markSafe: agentgit.MarkSafe,
|
|
bootID: agentboot.TaskID,
|
|
wait: func(ctx context.Context, log *slog.Logger) {
|
|
<-ctx.Done()
|
|
log.InfoContext(ctx, "archie-agent shutting down")
|
|
},
|
|
steps: steps,
|
|
guardrails: tools.NewGuardrailEngine(tools.DefaultGuardrailConfig()),
|
|
}, nil
|
|
}
|
|
|
|
// Run starts the worker, serves full-task requests, and returns after ctx
|
|
// cancellation or a startup failure.
|
|
func Run(ctx context.Context, settings Settings, log *slog.Logger) error {
|
|
dependencies, err := productionWorkerDependencies()
|
|
if err != nil {
|
|
return &StartupError{Operation: "register workflow step vocabulary", Err: err}
|
|
}
|
|
return run(ctx, settings, log, dependencies)
|
|
}
|
|
|
|
func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies workerDependencies) error {
|
|
if log == nil {
|
|
log = slog.New(slog.DiscardHandler)
|
|
}
|
|
workDir := settings.WorkDir
|
|
if workDir == "" {
|
|
workDir = storage.WorktreeMountDir
|
|
}
|
|
|
|
var modelTransport http.RoundTripper
|
|
if settings.ModelProxy != "" {
|
|
var err error
|
|
modelTransport, err = egress.ModelTransport(settings.ModelProxy, settings.ModelProxyHosts, settings.ModelProxyCA)
|
|
if err != nil {
|
|
return &StartupError{Operation: "model proxy", Err: err}
|
|
}
|
|
}
|
|
dependencies.markSafe(ctx, workDir, log)
|
|
|
|
// The task is known before connecting: the worker logs in as its task,
|
|
// with its run credential.
|
|
taskID, err := dependencies.bootID(workDir)
|
|
if err != nil {
|
|
log.ErrorContext(ctx, "task boot failed", "err", err)
|
|
return &StartupError{Operation: "task boot failed", Err: err}
|
|
}
|
|
transport, err := dependencies.connect(ctx, agentnats.Config{
|
|
URL: settings.NATSURL,
|
|
TaskID: taskID,
|
|
StateStoreURL: settings.StateStoreTarget,
|
|
StateStoreToken: settings.StateStoreToken,
|
|
StateStoreCA: settings.StateStoreCA,
|
|
}, log)
|
|
if err != nil {
|
|
operation := "nats connect failed"
|
|
log.ErrorContext(ctx, operation, "err", err)
|
|
return &StartupError{Operation: operation, Err: err}
|
|
}
|
|
defer transport.Close()
|
|
log = slog.New(logging.WithTrace(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID)))
|
|
log.InfoContext(ctx, "system log publisher attached", "task", taskID)
|
|
|
|
subscription, err := transport.SubscribeTasks(ctx, taskID, func(ctx context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
|
return executeTaskRequest(ctx, request, transport, workDir, log, dependencies.steps, dependencies.guardrails, modelTransport)
|
|
}, log)
|
|
if err != nil {
|
|
log.ErrorContext(ctx, "taskrun subscribe failed", "err", err)
|
|
return &StartupError{Operation: "taskrun subscribe failed", Err: err}
|
|
}
|
|
defer func() {
|
|
if err := subscription.Close(); err != nil {
|
|
log.WarnContext(ctx, "task run unsubscribe failed", "err", err)
|
|
}
|
|
}()
|
|
|
|
log.InfoContext(ctx, "archie-agent ready", "nats", settings.NATSURL)
|
|
dependencies.wait(ctx, log)
|
|
return nil
|
|
}
|
|
|
|
type taskServiceTransport interface {
|
|
Forger(identity, credential string, timeout time.Duration) workflow.Forger
|
|
Messenger(identity, credential string, timeout time.Duration) workflow.Messenger
|
|
Store(time.Duration) workflow.Store
|
|
Calls(time.Duration) task.Caller
|
|
Trees(string, string, time.Duration) agentnats.RemoteTrees
|
|
EventPublisher() agentexec.EventPublisher
|
|
}
|
|
|
|
func executeTaskRequest(ctx context.Context, request taskrun.Request, transport taskServiceTransport, workDir string, log *slog.Logger, steps *workflow.Manager, guardrails *tools.GuardrailEngine, modelTransport http.RoundTripper) (*taskrun.Response, error) {
|
|
log.InfoContext(ctx, "running task", "task", request.Task.ID, "repo", request.Repo.FullName(), "issue", request.Task.IssueNumber)
|
|
dependencies := taskDependencies{
|
|
forge: transport.Forger(request.Task.Identity, request.RunCredential, rpcTimeout),
|
|
messenger: transport.Messenger(request.Task.Identity, request.RunCredential, rpcTimeout),
|
|
store: transport.Store(rpcTimeout),
|
|
calls: transport.Calls(rpcTimeout),
|
|
trees: transport.Trees(request.Task.Identity, request.RunCredential, rpcTimeout),
|
|
events: transport.EventPublisher(),
|
|
steps: steps,
|
|
guardrails: guardrails,
|
|
|
|
modelTransport: modelTransport,
|
|
}
|
|
response, err := runTask(ctx, request, dependencies, newTaskRunner(modelTransport), workDir, log)
|
|
if err != nil {
|
|
log.ErrorContext(ctx, "task run failed", "task", request.Task.ID, "err", err)
|
|
return nil, err
|
|
}
|
|
log.InfoContext(ctx, "task run complete", "task", request.Task.ID, "status", response.Status)
|
|
return response, nil
|
|
}
|