Files
archie/internal/daemon/daemon.go
T

278 lines
10 KiB
Go

// Package daemon is archied's resident loop: poll GitHub for labelled
// issues, enqueue them, and process tasks through their routed workflows.
// State lives in the store; the daemon is restartable at any point.
package daemon
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"sync"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/container"
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/taskstate"
"github.com/samcharles93/archie-core/internal/worktree"
)
// TaskTransport publishes discovered work, collects it back, and asks a worker
// to run a task. The daemon's TaskBus runs on it.
type TaskTransport interface {
PublishUnique(ctx context.Context, subject, idempotencyKey string, payload []byte) error
Fetch(ctx context.Context) (eventbus.Message, error)
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
}
// RunCredentialIssuer issues the one credential a task's container holds: it
// authorizes the task's own State Store calls and the push of its branch.
// It lapses at the task's time limit plus a margin if never revoked.
type RunCredentialIssuer interface {
Issue(ctx context.Context, task *workflow.Task, limit time.Duration) (token string, revoke func(), err error)
}
// NATSEndpoint is the broker address and credential archied connected with at
// startup. It is runtime state rather than configuration: embedded mode
// generates both values, and external secret references are resolved once.
type NATSEndpoint struct {
URL string
Token string
// Scoped is true on the embedded broker, where a container logs in with
// its run credential and reaches only its own task's subjects.
Scoped bool
}
// ContainerToken is the broker token a container is handed: none on a
// scoped broker. An external broker cannot scope a container, so it still
// gets the broker's token (reported as a health issue by archied).
func (e NATSEndpoint) ContainerToken() string {
if e.Scoped {
return ""
}
return e.Token
}
// StateStoreEndpoint is the State Store target and the CA its certificate
// chains to, both passed to agent containers.
type StateStoreEndpoint struct {
URL string
CA []byte
}
type Daemon struct {
ResolveRepository func(context.Context, *workflow.Task, string) (RepositoryAccess, error)
// Cfg publishes the running configuration. Read through Cfg() so a
// reload swaps the published snapshot atomically. See config.Holder.
Cfg *config.Holder
// ConnectedNATS is the endpoint the daemon connected with at startup;
// container env uses it, never live config.
ConnectedNATS NATSEndpoint
// ConnectedStateStore is the State Store target the daemon dialed at
// startup, passed to containers.
ConnectedStateStore StateStoreEndpoint
Store storecontract.TaskStore
// BindingDispatch offers captured events to armed bindings and starts their
// tasks. Built by the composition root from its own stores.
BindingDispatch *BindingDispatch
// Identity answers identity, org and access questions. Built by the
// composition root with NewIdentityGate.
Identity *IdentityGate
Forge forge.Forge
Trees *worktree.Manager
Bus *events.Bus
Log *slog.Logger
// TaskTransport is the NATS connection the task bus runs on. The bus is
// built by WireTaskBus.
TaskTransport TaskTransport
// TaskBus takes, runs and publishes tasks. Set by WireTaskBus.
TaskBus *TaskBus
// ReviewScanner reconciles PRs, scans their reviews and drains review
// reactions into remediate runs.
ReviewScanner *ReviewScanner
// RunCredentials issues each task's run credential (see runCredential).
// Required whenever ConnectedStateStore.URL is set: the task is parked
// rather than handed the daemon's own administrative token.
RunCredentials RunCredentialIssuer
// ModelEgress gives native task containers model access without provider
// keys. Nil parks a task whose providers need a key.
ModelEgress ModelEgress
// TaskModels answers the model aliases a task's org resolves against.
TaskModels TaskModels
// TaskInstructions answers the instance and organisation instructions a
// task's agent runs follow.
TaskInstructions TaskInstructions
// AgentStatus records agent versions for update verification.
AgentStatus *AgentStatus
// ContainerPool manages Docker container lifecycle. Nil means autonomous
// execution is unavailable and process parks tasks; every runnable task gets
// a fresh container.
ContainerPool *container.Pool
// Storage is the pluggable storage backend for container mounts.
// A runnable task requires it; acquireTaskContainer parks on nil.
Storage storage.Backend
// IssuePoller discovers forge issues and publishes them as tasks.
IssuePoller *IssuePoller
// PackageAuthorities bounds the tools a package workflow's run is offered.
PackageAuthorities PackageAuthorities
// Playbooks are the loaded EDA playbooks, consulted before kind/label
// bindings when pinning a workflow.
Playbooks interface {
Dispatch(context.Context, playbook.DispatchInput) (playbook.Decision, bool)
Run(context.Context, storecontract.PlaybookDispatcher, *slog.Logger, playbook.DispatchInput) error
}
// PlaybookLedger is the at-most-once gate action playbooks run through.
PlaybookLedger storecontract.PlaybookDispatcher
// WorkflowDefinitions supplies the active database definitions. A task is
// pinned once before dispatch; retries reuse the task's stored YAML.
WorkflowDefinitions interface {
WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error)
}
// TaskLogs persists each task's log output and mirrors it to the dashboard.
// Nil disables task logging.
TaskLogs *logging.TaskRegistry
// running holds a cancel function for every task currently executing,
// so /stop can reach work already in flight. Its zero value is ready
// to use.
running runningTasks
// loopPasses maps each run loop ("cycle", "drain") to when its latest
// pass began, as Unix nanoseconds. StalestLoop reports the oldest, so one
// wedged loop isn't hidden by a healthy one.
loopPasses sync.Map
}
// IssueSource is one forge connection's repositories as the identity
// permitted to read them sees them.
type IssueSource struct {
Identity identity.IdentityID
Forge forge.Forge
// BotUser is the connection's external account, the assignee polled for.
BotUser string
// Dispatch and Label are the discovery rule the connection's org polls
// with. Empty means the daemon's own.
Dispatch config.Dispatch
Label string
Repos []config.Repo
}
// taskHold is one submitted task's claim on execution capacity: its slot under
// the global limit, and its place at the tail of its repo's chain, which the
// next task for that repo is blocked on. A run that waits on a callee gives
// both back so the callee can run, and takes them again when the wait ends.
type taskHold struct {
dispatcher *taskDispatcher
repo string
// chain is true when the repo runs one task at a time, so the run holds a
// place in that repo's chain.
chain bool
// baton is the run's place in its repo's chain, closed when it gives that
// place up; nil while it holds none.
baton chan struct{}
// slot is true while the run counts against the global limit.
slot bool
// waits counts the callees the run is waiting on: an open call gives capacity
// back and the last closed call takes it again.
waits int
// finished is true once the run's process returned.
finished bool
// id is the task this hold belongs to, named by the run once it knows the
// task's id.
id int64
attempt int
}
// name records which task's capacity this hold is, so a wait on a callee can
// give it back by task id. Zero names nothing: a task arriving on the NATS
// stream has no id until the store claims its row inside the run.
func (h *taskHold) name(id int64, attempt int) {
if id == 0 {
return
}
h.dispatcher.mu.Lock()
h.id = id
h.attempt = attempt
h.dispatcher.holds[id] = h
h.dispatcher.mu.Unlock()
}
// passFailed reports a maintenance pass whose items failed; each item's cause
// is already logged where it happened.
func passFailed(pass string, failed []string) error {
if len(failed) == 0 {
return nil
}
return fmt.Errorf("%s failed for %d item(s): %s", pass, len(failed), strings.Join(failed, ", "))
}
// pinFailure carries the park class for a pin failure: operator-actionable
// for a bad pin or missing workflow, transient for a store error.
type pinFailure struct {
class taskstate.ParkClass
err error
}
func (e pinFailure) Error() string { return e.err.Error() }
func (e pinFailure) Unwrap() error { return e.err }
// pinParkClass reports the class a pin failure carries, defaulting to
// taskstate.ParkNeedsDecision for a cause that did not classify itself: the safe
// misread is "an operator should look at this", never "a requeue will fix it".
func pinParkClass(err error) taskstate.ParkClass {
if failure, ok := errors.AsType[pinFailure](err); ok {
return failure.class
}
return taskstate.ParkNeedsDecision
}
// blockedPark names what a park is missing alongside the failure.
type blockedPark struct {
blocker taskstate.Blocker
err error
}
func (e blockedPark) Error() string { return e.err.Error() }
func (e blockedPark) Unwrap() error { return e.err }
// parkBlocker reports the blocker err names, or empty.
func parkBlocker(err error) taskstate.Blocker {
if b, ok := errors.AsType[blockedPark](err); ok {
return b.blocker
}
if failure, ok := errors.AsType[*forge.AccessError](err); ok {
return accessBlocker(failure.Code)
}
return ""
}
// accessBlocker maps a repository access failure code onto what fixes it.
func accessBlocker(code string) taskstate.Blocker {
switch code {
case "operation_denied":
return taskstate.BlockerConnectionPermit
case "repository_not_configured", "connection_required", "connection_missing":
return taskstate.BlockerRepository
case "credential_unavailable", "account_unverified":
return taskstate.BlockerConnectionCredential
case "forge_unavailable":
return taskstate.BlockerForgeExtension
}
return ""
}