From 8e41bb9a661dc5816a7fbfe3ffc1fc168a940e34 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 6 Sep 2026 01:35:15 +1000 Subject: [PATCH] feat(gateway): consolidate task actions and repair daemon default path Phase 1 (extract the Gateway Service), first stage. Repairs the half-migrated working tree so the default (inproc) daemon path works again while keeping the WIP's direction: - chatTaskActorAdapter is contract-only (no HTTP detour); nil contract returns ErrChatCapabilityUnavailable instead of panicking. - openStores opens the conversation store for both processes again; the standalone gateway no longer owns it exclusively. Phase 1 keeps the daemon's Telegram/email routers and session-memory curator reading the same SQLite file; remote conversations arrive when a process that cannot open the file needs them. - setupLLMAndChat composes the local gateway (setupGatewayChat) in default mode and a remote gRPC client in mode=remote; the returned contract is never nil, and Run handles the error. - taskactions.Service is now the single task-mutation implementation shared by the dashboard (webui TaskActionService), the daemon's local gateway actor, and the NATS responder. The daemon registers taskactions.Register so the standalone Gateway's client can forward actions; transport follows the repo's natsrpc pattern. - uncommitted sessions.proto / SessionStoreService gRPC contract deferred: it has no wired consumers (scope creep for this stage). - task_action_adapter_test.go rewritten against the contract chain. task check passes; archied and archie-gateway both build. --- cmd/archie-gateway/main.go | 28 +- internal/app/archied/bootstrap.go | 159 +++------- internal/app/archied/chat_service.go | 9 - internal/app/archied/gateway.go | 153 +++++----- internal/app/archied/gateway_runtime.go | 77 +++++ internal/app/archied/main.go | 117 +------ .../app/archied/task_action_adapter_test.go | 88 ++---- internal/app/archied/task_actions.go | 78 +++++ internal/domain/taskactions/service.go | 201 ++++++++++++ internal/infrastructure/taskactions/rpc.go | 77 +++++ .../infrastructure/taskactions/service.go | 32 ++ internal/infrastructure/taskactions/store.go | 35 +++ internal/webui/api_tasks.go | 286 +++--------------- 13 files changed, 732 insertions(+), 608 deletions(-) create mode 100644 internal/app/archied/gateway_runtime.go create mode 100644 internal/app/archied/task_actions.go create mode 100644 internal/domain/taskactions/service.go create mode 100644 internal/infrastructure/taskactions/rpc.go create mode 100644 internal/infrastructure/taskactions/service.go create mode 100644 internal/infrastructure/taskactions/store.go diff --git a/cmd/archie-gateway/main.go b/cmd/archie-gateway/main.go index c409f8542..521c26b3a 100644 --- a/cmd/archie-gateway/main.go +++ b/cmd/archie-gateway/main.go @@ -2,9 +2,35 @@ package main import ( + "context" + "flag" + "fmt" "os" + "os/signal" + "path/filepath" + "syscall" "github.com/samcharles93/archie-core/internal/app/archied" ) -func main() { os.Exit(archied.RunGateway()) } +func main() { os.Exit(run()) } + +func run() int { + base, err := os.UserConfigDir() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + var options archied.GatewayOptions + flag.StringVar(&options.Config, "config", filepath.Join(base, "archie", "config.toml"), "configuration file or directory") + flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory") + flag.StringVar(&options.Listen, "listen", "127.0.0.1:8585", "gateway gRPC listen address") + flag.Parse() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + if err := archied.RunGateway(ctx, options); err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + return 0 +} diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 7cd8c07fd..f30476e2e 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -23,7 +23,6 @@ import ( "github.com/samcharles93/ai-sdk/runtime" - "github.com/samcharles93/archie-core/internal/agentexec" channelruntime "github.com/samcharles93/archie-core/internal/channels" "github.com/samcharles93/archie-core/internal/channels/email" "github.com/samcharles93/archie-core/internal/channels/webhook" @@ -48,6 +47,7 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" "github.com/samcharles93/archie-core/internal/infrastructure/sessioncurator" "github.com/samcharles93/archie-core/internal/infrastructure/skillcurator" + "github.com/samcharles93/archie-core/internal/infrastructure/taskactions" "github.com/samcharles93/archie-core/internal/logging" "github.com/samcharles93/archie-core/internal/memory" "github.com/samcharles93/archie-core/internal/plugin" @@ -128,7 +128,6 @@ type boot struct { chatController *gateway.StoreTaskController defaultChatIdentity string updateService *releaseupdate.Service - webRouter *gateway.Router startGateways []func() capabilityHost *plugin.Host @@ -273,6 +272,12 @@ func (b *boot) openStores(ctx context.Context) error { log.Error("close store", "err", err) } }) + // The conversation store is Gateway-owned data, but Phase 1 keeps the + // daemon's own Telegram/email routers and the session-memory curator + // reading the same SQLite file directly (Messaging extraction is + // Phase 4). Both processes opening the same file is the accepted + // Phase 1 shape; remote consumers of the conversation store arrive + // when a process that cannot open the file needs it. chatSessionStore, err := makeTelegramSessionStore(cfg) //nolint:contextcheck // the session store opener takes no context by design; its schema init must complete at boot regardless of cancellation if err != nil { log.Error("open conversation store", "path", conversationDBPath(cfg.DBPath), "err", err) @@ -481,114 +486,37 @@ func (b *boot) setupContainers(ctx context.Context) func() { // setupLLMAndChat wires the runtime, tool registry, model management, // personas and the dashboard's chat service. -func (b *boot) setupLLMAndChat(ctx context.Context) { //nolint:funlen // composition root keeps the Gateway wiring in one lifecycle phase - cfg, log := b.cfg, b.log +func (b *boot) setupLLMAndChat(ctx context.Context) error { + cfg := b.cfg - // ── LLM runtime ────────────────────────────────────────────────── - // Created before gateways so the LLMResponder can be wired into the - // Telegram router for non-command message processing. - providers := executionProviders(cfg) - b.llm = agentexec.NewRuntime(providers) - b.toolReg = tools.NewRegistry() - chatModels := newChatModelManager(cfg.Models, cfg.Chat.Models, b.catalogModels) - chatModels.ApplyModelCatalog(b.catalog) - b.chatModels = chatModels - - // ── Persona registry ───────────────────────────────────────────── - b.personas = gateway.NewPersonaRegistry(gateway.DefaultPersonas()) - - profiles, defaultChatIdentity := chatTaskProfiles(cfg) - var chatTasks gateway.TaskCreator - if len(profiles) > 0 { - chatTasks = gateway.NewStoreTaskCreatorForProfiles( - chatTaskWriterAdapter{enqueue: b.st.EnqueueChatTask}, - profiles, - ) + // The daemon owns the local gateway in default mode: the modular + // monolith keeps serving the ChatContract in-process, with the + // standalone archie-gateway binary prepared as a remote alternative + // (mode = "remote"). The local and remote adapters both satisfy + // gateway.ChatContract; composition picks the transport. + var local gateway.ChatContract + if cfg.Services.Gateway.Mode != "remote" { + // Full local contract: router, sessions, models, personas, task + // commands and the LLM responder. The in-process task-actions + // actor applies mutations directly to the store the daemon owns + // -- no HTTP detour through webui, no NATS hop. + local = b.setupGatewayChat(ctx, taskActionsActor{b}) + } else { + // The daemon still runs its own Telegram/email routers in Phase 1, + // so the runtime objects survive even though the webui chat goes + // over gRPC to the standalone Gateway. + b.setupChatRuntime(cfg) } - b.chatTasks = chatTasks - b.defaultChatIdentity = defaultChatIdentity - chatController := gateway.NewStoreTaskController(chatTaskControllerAdapter{ - taskByID: b.st.TaskByID, - requeue: b.st.Requeue, - transition: b.st.Transition, - }) - b.chatController = chatController - b.updateService = makeUpdateService(telegramSetup{Cfg: config.NewHolder(cfg)}) + + contract, cleanup, err := composeChatContract(cfg.Services.Gateway, local) + if err != nil { + return err + } + b.addCleanup(cleanup) + b.web.Chat = &webui.ChatService{Contract: contract, Updates: b.updateService} b.web.WorkRequests = b.chatTasks - if cfg.Services.Gateway.Mode == "remote" { - // The daemon is a Gateway consumer in this mode. The Gateway Service - // owns Router, session SQLite, model selection and persona state; keep - // those implementation objects out of the daemon's web composition. - contract, closeContract := b.chatContract(cfg.Services.Gateway, nil) - b.addCleanup(closeContract) - b.web.Chat = &webui.ChatService{Contract: contract} - if b.updateService != nil { - b.web.Chat.Updates = b.updateService - } - b.setupReadinessProbes() - return - } - - // The dashboard is another gateway, not a second chat implementation. It - // shares the router, session history, model selection, personas and LLM - // responder with Telegram while using a stable browser channel id. - b.webRouter = gateway.NewRouter(b.st, nil, "web") - b.webRouter.Version = fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion) - if cfg.Chat.Telegram.Token != (secret.SecretRef{}) || cfg.Chat.Telegram.TokenEnv != "" { - b.webRouter.Restart = func(context.Context) error { - if b.restartTelegram == nil { - return fmt.Errorf("telegram gateway is not ready") - } - return b.restartTelegram() - } - } - b.webRouter.Models = b.chatModels - if b.updateService != nil { - b.webRouter.Updates = b.updateService - } - b.webRouter.Personas = b.personas - b.webRouter.InitSessions(b.chatSessionStore) - configureTaskCommands(b.webRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.st.Tasks}, b.defaultChatIdentity) - webSetup := telegramSetup{ - Cfg: config.NewHolder(cfg), St: b.st, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, - Personas: b.personas, ChatTasks: b.chatTasks, ChatController: b.chatController, - ChatTaskLister: chatTaskListerAdapter{tasks: b.st.Tasks}, - ChatTaskLogs: chatTaskLogReaderAdapter{ - tasks: b.st.TaskByID, - taskLogs: b.taskLogs, - }, - ChatTaskActor: chatTaskActorAdapter{ - tasks: b.st.TaskByID, - contract: func() gateway.ChatContract { - if b.cfg.Services.Gateway.Mode == "remote" && b.web != nil && b.web.Chat != nil { - return b.web.Chat.Contract - } - return nil - }(), - token: func() string { return b.web.Token }, - }, - DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, - Bus: b.bus, Log: log, Secrets: b.secrets, - } - b.webRouter.LLM, b.webRouter.LLMStream = makeChatLLMResponder(ctx, "web", webSetup, b.chatSessionStore, b.webRouter) - b.webRouter.Titles = newChatTitleGenerator(webSetup) - b.webRouter.Log = log - localChat := &gateway.LocalChatAdapter{ - Router: b.webRouter, Sessions: b.chatSessionStore, - Turns: gateway.NewTurns(log), - Models: b.chatModels, Personas: b.personas, - TaskActor: chatTaskActorAdapter{tasks: b.st.TaskByID, apply: applyGatewayTaskAction(b.st)}, - } - contract, closeContract := b.chatContract(cfg.Services.Gateway, localChat) - b.addCleanup(closeContract) - b.web.Chat = &webui.ChatService{Contract: contract} - if b.updateService != nil { - b.web.Chat.Updates = b.updateService - } - - // Operator readiness probes: wired once the chat surface exists so the - // gateway probe can read the real session store and model manager. b.setupReadinessProbes() + return nil } // setupGateways assembles the Telegram, email and webhook gateways. It @@ -608,14 +536,12 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b taskLogs: b.taskLogs, }, ChatTaskActor: chatTaskActorAdapter{ - tasks: b.st.TaskByID, contract: func() gateway.ChatContract { - if b.cfg.Services.Gateway.Mode == "remote" && b.web != nil && b.web.Chat != nil { + if b.web != nil && b.web.Chat != nil { return b.web.Chat.Contract } return nil }(), - token: func() string { return b.web.Token }, }, DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, Updates: b.updateService, Secrets: b.secrets, @@ -915,6 +841,17 @@ func (b *boot) registerNATSRPC() error { return err } b.addCleanup(unsubscribeAgentEvents) + + // The standalone Gateway forwards operator task actions to the daemon + // over NATS. The daemon owns execution cancellation, retry policy, + // forge closure and event persistence; the responder applies the same + // taskactions.Service the dashboard uses. + unsubscribeTaskActions, err := taskactions.Register(coreConn, b.taskActions(), log) + if err != nil { + log.Error("gateway task action register failed", "err", err) + return err + } + b.addCleanup(unsubscribeTaskActions) return nil } @@ -941,7 +878,9 @@ func (b *boot) setupMemory() error { b.memManager = memManager // The dashboard is built before memory exists, so it is wired in here // rather than at construction. - b.web.Memory = memManager + if b.web != nil { + b.web.Memory = memManager + } if err := memManager.Initialize("daemon"); err != nil { log.Warn("memory manager initialize", "err", err) diff --git a/internal/app/archied/chat_service.go b/internal/app/archied/chat_service.go index 0ff5cc390..981e9b7a2 100644 --- a/internal/app/archied/chat_service.go +++ b/internal/app/archied/chat_service.go @@ -39,12 +39,3 @@ func composeChatContract(settings config.ServiceConnection, local gateway.ChatCo return nil, nil, fmt.Errorf("unknown gateway service mode %q", settings.Mode) } } - -func (b *boot) chatContract(settings config.ServiceConnection, local gateway.ChatContract) (gateway.ChatContract, func()) { - contract, cleanup, err := composeChatContract(settings, local) - if err != nil { - b.log.Error("configure gateway contract", "error", err) - return nil, func() {} - } - return contract, cleanup -} diff --git a/internal/app/archied/gateway.go b/internal/app/archied/gateway.go index afaccc67c..fff023eaf 100644 --- a/internal/app/archied/gateway.go +++ b/internal/app/archied/gateway.go @@ -2,102 +2,111 @@ package archied import ( "context" - "flag" "fmt" "net" "os" - "os/signal" - "syscall" + "time" + natsio "github.com/nats-io/nats.go" "google.golang.org/grpc" + "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" ) -// RunGateway starts the standalone Gateway Service. The service owns the -// conversation store and the Gateway composition; the daemon reaches it only -// through the generated ChatContract RPC. -func RunGateway() int { - args := gatewayArgs() - if args.version { - fmt.Printf("archie-gateway %s\n", gatewayVersion) - return 0 - } - - root, cancel := context.WithCancel(context.Background()) - defer cancel() - ctx, stop := signal.NotifyContext(root, os.Interrupt, syscall.SIGTERM) - defer stop() +// GatewayOptions contains process inputs for the standalone Gateway. +type GatewayOptions struct { + Config string + Overlay string + Listen string +} +// RunGateway owns conversation persistence, model runtime and tool-provider +// lifecycles. Task-store adapters continue to access the existing task database +// until the State Store Service extraction; no task data is relocated. +func RunGateway(ctx context.Context, options GatewayOptions) error { b := newBootstrap() - if err := b.loadConfig(ctx, args.config, args.overlay, false); err != nil { - return 1 - } defer b.cleanup() + if err := b.loadConfig(ctx, options.Config, options.Overlay, false); err != nil { + return err + } + b.log = b.log.With("component", "gateway") if err := b.openStores(ctx); err != nil { - return 1 + return err } - b.loadCatalog(ctx, args.config) - // setupLLMAndChat also wires the task adapters used by Gateway tools. It - // normally publishes a dashboard contract, but the standalone service - // publishes that same local adapter over gRPC below. - b.setupObservability() - b.cfg.Services.Gateway.Mode = "inproc" - b.cfg.Services.Gateway.Target = "" - b.setupLLMAndChat(ctx) - if b.web == nil || b.web.Chat == nil || b.web.Chat.Contract == nil { - b.log.Error("gateway contract was not composed") - return 1 + b.loadCatalog(ctx, options.Config) + if b.cfg.NATS.URL == "" { + return fmt.Errorf("archie-gateway requires nats.url pointing to the daemon's shared NATS server") } - - listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", args.listen) + token, err := configuredNATSToken(b.cfg.NATS, os.Getenv) if err != nil { - b.log.Error("listen for gateway", "addr", args.listen, "err", err) - return 1 + return err + } + nc, err := natsio.Connect(b.cfg.NATS.URL, natsio.Token(token)) + if err != nil { + return fmt.Errorf("connect gateway task actions: %w", err) + } + b.addCleanup(nc.Close) + contract, err := b.startGatewayRuntime(ctx, taskactions.Client{Conn: nc, Timeout: 30 * time.Second}) + if err != nil { + return err + } + host, _, err := net.SplitHostPort(options.Listen) + if err != nil || !net.ParseIP(host).IsLoopback() { + return fmt.Errorf("gateway listen address must use a loopback IP; use a secured tunnel for remote access") + } + listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", options.Listen) + if err != nil { + return fmt.Errorf("listen for gateway: %w", err) } defer listener.Close() + b.log.Info("archie-gateway running", "addr", listener.Addr().String()) + return serveGateway(ctx, listener, contract) +} + +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() + contract := b.setupGatewayChat(ctx, actor) + // Memory is opened with its own long-lived context inside (matching the + // daemon path via setupMemoryAll): shutdown of the file-backed provider + // outlives the boot context by design, so the legacy manager wires itself + // with a background context rather than the gateway's boot ctx. + if err := b.setupMemory(); err != nil { //nolint:contextcheck // setupMemory owns its lifecycle contexts, matching the daemon's setupMemoryAll + 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) + } + return contract, nil +} + +func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract) error { server := grpc.NewServer() - gatewayrpc.RegisterServer(server, b.web.Chat.Contract) + gatewayrpc.RegisterServer(server, contract) serveErr := make(chan error, 1) go func() { serveErr <- server.Serve(listener) }() - b.log.Info("archie-gateway running", "addr", listener.Addr().String()) - 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() - return 0 + timer.Stop() + return nil case err := <-serveErr: - if err != nil { - b.log.Error("gateway server stopped", "err", err) - return 1 - } - return 0 + return err } } - -type gatewayRunArgs struct { - config string - overlay string - listen string - version bool -} - -func gatewayArgs() gatewayRunArgs { - args := gatewayRunArgs{config: filepathConfigHome()} - flag.StringVar(&args.config, "config", args.config, "path to a TOML/YAML config file or configuration directory") - flag.StringVar(&args.overlay, "config-overlay", "", "path to a TOML/YAML overlay file or configuration directory") - flag.StringVar(&args.listen, "listen", "127.0.0.1:8585", "gateway gRPC listen address") - flag.BoolVar(&args.version, "version", false, "print the Gateway Service version and exit") - flag.Parse() - return args -} - -func filepathConfigHome() string { - base := os.Getenv("XDG_CONFIG_HOME") - if base == "" { - if home, err := os.UserHomeDir(); err == nil { - base = home + "/.config" - } - } - return base + "/archie/config.toml" -} diff --git a/internal/app/archied/gateway_runtime.go b/internal/app/archied/gateway_runtime.go new file mode 100644 index 000000000..bc591687c --- /dev/null +++ b/internal/app/archied/gateway_runtime.go @@ -0,0 +1,77 @@ +package archied + +import ( + "context" + "fmt" + + "github.com/samcharles93/archie-core/internal/agentexec" + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/tools" +) + +// setupChatRuntime wires the model runtime, tool registry, persona registry, +// chat task creator/controller, default chat identity and the release-update +// service. Both the daemon (its own Telegram/email routers) and the standalone +// Gateway (the web router) build through this one constructor so the two +// processes cannot diverge. +func (b *boot) setupChatRuntime(cfg config.Config) { + // ── LLM runtime ────────────────────────────────────────────────── + // Created before gateways so the LLMResponder can be wired into the + // Telegram router for non-command message processing. + providers := executionProviders(cfg) + b.llm = agentexec.NewRuntime(providers) + b.toolReg = tools.NewRegistry() + chatModels := newChatModelManager(cfg.Models, cfg.Chat.Models, b.catalogModels) + chatModels.ApplyModelCatalog(b.catalog) + b.chatModels = chatModels + + // ── Persona registry ───────────────────────────────────────────── + b.personas = gateway.NewPersonaRegistry(gateway.DefaultPersonas()) + + profiles, defaultChatIdentity := chatTaskProfiles(cfg) + var chatTasks gateway.TaskCreator + if len(profiles) > 0 { + chatTasks = gateway.NewStoreTaskCreatorForProfiles( + chatTaskWriterAdapter{enqueue: b.st.EnqueueChatTask}, + profiles, + ) + } + b.chatTasks = chatTasks + b.defaultChatIdentity = defaultChatIdentity + b.chatController = gateway.NewStoreTaskController(chatTaskControllerAdapter{ + taskByID: b.st.TaskByID, + requeue: b.st.Requeue, + transition: b.st.Transition, + }) + b.updateService = makeUpdateService(telegramSetup{Cfg: config.NewHolder(cfg)}) +} + +// 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 { + b.setupChatRuntime(b.cfg) + cfg := b.cfg + router := gateway.NewRouter(b.st, nil, "web") + router.Version = fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion) + 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.st.Tasks}, b.defaultChatIdentity) + setup := telegramSetup{ + Cfg: config.NewHolder(cfg), St: b.st, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, + Personas: b.personas, ChatTasks: b.chatTasks, ChatController: b.chatController, + ChatTaskLister: chatTaskListerAdapter{tasks: b.st.Tasks}, + ChatTaskLogs: chatTaskLogReaderAdapter{tasks: b.st.TaskByID, taskLogs: b.taskLogs}, + ChatTaskActor: actor, DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, + Bus: b.bus, Log: b.log, Secrets: b.secrets, + } + router.LLM, router.LLMStream = makeChatLLMResponder(ctx, "web", setup, b.chatSessionStore, router) + router.Titles = newChatTitleGenerator(setup) + router.Log = b.log + return &gateway.LocalChatAdapter{ + Router: router, Sessions: b.chatSessionStore, Turns: gateway.NewTurns(b.log), + Models: b.chatModels, Personas: b.personas, TaskActor: actor, + } +} diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 1ef46337e..7195ceb55 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -5,7 +5,6 @@ package archied import ( - "bytes" "context" "crypto/sha256" "encoding/json" @@ -13,8 +12,6 @@ import ( "fmt" "log/slog" "net" - "net/http" - "net/http/httptest" "os" "os/signal" "path/filepath" @@ -353,7 +350,7 @@ func parseArgs() (runArgs, bool) { return args, false } -func Run() int { +func Run() int { //nolint:cyclop // the composition root's setup sequence is deliberately flat and sequential args, exit := parseArgs() if exit { return 0 @@ -392,7 +389,9 @@ func Run() int { return 1 } - b.setupLLMAndChat(ctx) + if err := b.setupLLMAndChat(ctx); err != nil { + return 1 + } if !b.setupGateways(ctx, args.cfgPath, args.overlayPath) { return 1 } @@ -660,118 +659,18 @@ func (a chatTaskLogReaderAdapter) ReadChatTaskLogs( }, nil } -// chatTaskActorAdapter executes operator actions on a task via the same -// action path the dashboard uses, ensuring chat and webui cannot diverge. -// It enforces identity scoping so an identity cannot mutate another -// identity's tasks. +// chatTaskActorAdapter sends messaging actions through the Gateway boundary. type chatTaskActorAdapter struct { - tasks func(context.Context, int64) (*store.Task, error) - apply func(context.Context, string, int64, taskstate.Action) (gateway.TaskActionResult, error) contract gateway.ChatContract - handler http.Handler - token func() string -} - -func applyGatewayTaskAction(st store.TaskStore) func(context.Context, string, int64, taskstate.Action) (gateway.TaskActionResult, error) { - return func(ctx context.Context, identity string, taskID int64, action taskstate.Action) (gateway.TaskActionResult, error) { - task, err := st.TaskByID(ctx, taskID) - if err != nil { - return gateway.TaskActionResult{}, err - } - if task == nil { - return gateway.TaskActionResult{}, fmt.Errorf("task %d not found", taskID) - } - if task.Identity != identity { - return gateway.TaskActionResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) - } - if err := taskstate.CheckAction(task.Status, action); err != nil { - return gateway.TaskActionResult{}, err - } - switch action { - case taskstate.ActionApprove: - err = st.Requeue(ctx, taskID, store.StatusWaitingHuman, "implement") - case taskstate.ActionRetry: - err = st.RetryTask(ctx, taskID, store.StatusParked, task.Workflow) - case taskstate.ActionCancel, taskstate.ActionReject: - err = st.Transition(ctx, taskID, task.Status, store.StatusClosedWontDo, "declined by "+identity) - case taskstate.ActionAbandon: - err = st.Transition(ctx, taskID, store.StatusParked, store.StatusClosedWontDo, "abandoned by "+identity) - case taskstate.ActionStop: - return gateway.TaskActionResult{}, fmt.Errorf("stopping running tasks is unavailable in the Gateway Service") - case taskstate.ActionArchive: - _, err = st.ArchiveTask(ctx, taskID, task.Status, events.Event{Kind: events.KindTaskArchiveRequested, TaskID: taskID}) - default: - return gateway.TaskActionResult{}, fmt.Errorf("unsupported task action %q", action) - } - if err != nil { - return gateway.TaskActionResult{}, err - } - return gateway.TaskActionResult{TaskID: taskID, Action: string(action), Message: fmt.Sprintf("Applied %s to task %d.", action, taskID)}, nil - } } func (a chatTaskActorAdapter) ApplyChatTaskAction( ctx context.Context, identity string, taskID int64, action taskstate.Action, ) (gateway.TaskActionResult, error) { - if a.contract != nil { - return a.contract.ApplyTaskAction(ctx, identity, taskID, action) + if a.contract == nil { + return gateway.TaskActionResult{}, gateway.ErrChatCapabilityUnavailable } - if a.apply != nil { - return a.apply(ctx, identity, taskID, action) - } - if a.tasks == nil || a.handler == nil { - return gateway.TaskActionResult{}, fmt.Errorf("task actions are not configured") - } - task, err := a.tasks(ctx, taskID) - if err != nil { - return gateway.TaskActionResult{}, err - } - if task == nil { - return gateway.TaskActionResult{}, fmt.Errorf("task %d not found", taskID) - } - // Match the filter chatTaskListerAdapter and chatTaskLogReaderAdapter apply: - // a model bound to one identity must not mutate another identity's tasks, - // and tasks with no identity (forge-sourced) are not actionable through a - // chat tool at all — they belong to the daemon, not a particular identity. - if task.Identity != identity { - return gateway.TaskActionResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) - } - if err := taskstate.CheckAction(task.Status, action); err != nil { - return gateway.TaskActionResult{}, err - } - - body, err := json.Marshal(map[string]any{"action": string(action)}) - if err != nil { - return gateway.TaskActionResult{}, fmt.Errorf("marshal action request: %w", err) - } - req := httptest.NewRequestWithContext( - ctx, http.MethodPost, - fmt.Sprintf("/api/tasks/%d/action", taskID), - bytes.NewReader(body), - ) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Archie-CSRF", "1") - if a.token != nil { - if tok := a.token(); tok != "" { - req.Header.Set("Authorization", "Bearer "+tok) - } - } - rec := httptest.NewRecorder() - a.handler.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - errMsg := strings.TrimSpace(rec.Body.String()) - if errMsg == "" { - errMsg = fmt.Sprintf("HTTP %d", rec.Code) - } - return gateway.TaskActionResult{}, fmt.Errorf("action %s on task %d failed: %s", action, taskID, errMsg) - } - - return gateway.TaskActionResult{ - TaskID: taskID, - Action: string(action), - Message: fmt.Sprintf("Applied %s to task %d.", action, taskID), - }, nil + return a.contract.ApplyTaskAction(ctx, identity, taskID, action) } func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) { diff --git a/internal/app/archied/task_action_adapter_test.go b/internal/app/archied/task_action_adapter_test.go index 4078c73e5..78cd64a31 100644 --- a/internal/app/archied/task_action_adapter_test.go +++ b/internal/app/archied/task_action_adapter_test.go @@ -2,16 +2,30 @@ package archied import ( "context" + "log/slog" "path/filepath" "strings" "testing" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/store" "github.com/samcharles93/archie-core/internal/taskstate" - "github.com/samcharles93/archie-core/internal/webui" ) +// newChatTaskActorForTest wires the same production chain the daemon uses in +// default (in-process) mode: chatTaskActorAdapter → LocalChatAdapter → +// taskActionsActor → taskactions.Service over the given store. +func newChatTaskActorForTest(t *testing.T, st store.TaskStore, cfg config.Config) chatTaskActorAdapter { + t.Helper() + b := &boot{ + st: st, + cfg: cfg, + log: slog.Default(), + } + return chatTaskActorAdapter{contract: &gateway.LocalChatAdapter{TaskActor: taskActionsActor{b}}} +} + func TestChatTaskActorAdapterCrossIdentityRefused(t *testing.T) { ctx := context.Background() st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db")) @@ -28,12 +42,7 @@ func TestChatTaskActorAdapterCrossIdentityRefused(t *testing.T) { t.Fatal(err) } - srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{})} - adapter := chatTaskActorAdapter{ - tasks: st.TaskByID, - handler: srv.Handler(), - token: func() string { return srv.Token }, - } + adapter := newChatTaskActorForTest(t, st, config.Config{}) tests := []struct { name string @@ -88,12 +97,7 @@ func TestChatTaskActorAdapterRefusesDisallowedStateAction(t *testing.T) { } t.Cleanup(func() { _ = st.Close() }) - srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{})} - adapter := chatTaskActorAdapter{ - tasks: st.TaskByID, - handler: srv.Handler(), - token: func() string { return srv.Token }, - } + adapter := newChatTaskActorForTest(t, st, config.Config{}) tests := []struct { name string @@ -170,12 +174,7 @@ func TestChatTaskActorAdapterTaskNotFound(t *testing.T) { } t.Cleanup(func() { _ = st.Close() }) - srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{})} - adapter := chatTaskActorAdapter{ - tasks: st.TaskByID, - handler: srv.Handler(), - token: func() string { return srv.Token }, - } + adapter := newChatTaskActorForTest(t, st, config.Config{}) _, err = adapter.ApplyChatTaskAction(ctx, "archie", 999999, taskstate.ActionAbandon) if err == nil { @@ -247,12 +246,7 @@ func TestChatTaskActorAdapterAppliesActionsToStore(t *testing.T) { } t.Cleanup(func() { _ = st.Close() }) - srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{MaxRetries: 3})} - adapter := chatTaskActorAdapter{ - tasks: st.TaskByID, - handler: srv.Handler(), - token: func() string { return srv.Token }, - } + adapter := newChatTaskActorForTest(t, st, config.Config{MaxRetries: 3}) task, err := st.EnqueueChatTask(ctx, "acme", "widget", "actionable task", "", "", "archie") if err != nil { @@ -295,46 +289,12 @@ func TestChatTaskActorAdapterAppliesActionsToStore(t *testing.T) { } } -func TestChatTaskActorAdapterWithTokenAuth(t *testing.T) { +func TestChatTaskActorAdapterNilContract(t *testing.T) { ctx := context.Background() - st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db")) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = st.Close() }) + adapter := chatTaskActorAdapter{contract: nil} - srv := &webui.Server{ - Store: st, - Cfg: config.NewHolder(config.Config{MaxRetries: 3}), - Token: "secret-token-12345", - } - adapter := chatTaskActorAdapter{ - tasks: st.TaskByID, - handler: srv.Handler(), - token: func() string { return srv.Token }, - } - - task, err := st.EnqueueChatTask(ctx, "acme", "widget", "auth task", "", "", "archie") - if err != nil { - t.Fatal(err) - } - if err := st.Transition(ctx, task.ID, store.StatusQueued, store.StatusParked, "park"); err != nil { - t.Fatal(err) - } - - result, err := adapter.ApplyChatTaskAction(ctx, "archie", task.ID, taskstate.ActionAbandon) - if err != nil { - t.Fatalf("ApplyChatTaskAction with token auth failed: %v", err) - } - if result.TaskID != task.ID { - t.Errorf("result.TaskID = %d, want %d", result.TaskID, task.ID) - } - - current, err := st.TaskByID(ctx, task.ID) - if err != nil || current == nil { - t.Fatalf("TaskByID = (%+v, %v)", current, err) - } - if current.Status != store.StatusClosedWontDo { - t.Errorf("status = %q, want %q", current.Status, store.StatusClosedWontDo) + _, err := adapter.ApplyChatTaskAction(ctx, "archie", 1, taskstate.ActionAbandon) + if err == nil { + t.Fatal("expected error for nil contract, got nil") } } diff --git a/internal/app/archied/task_actions.go b/internal/app/archied/task_actions.go new file mode 100644 index 000000000..fe4e55ee0 --- /dev/null +++ b/internal/app/archied/task_actions.go @@ -0,0 +1,78 @@ +package archied + +import ( + "context" + "fmt" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/taskactions" + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/gateway" + taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// taskActionsActor applies operator actions in-process for the daemon's own +// local gateway. It is the identity-scoping implementation behind the +// ChatContract's ApplyTaskAction when the daemon is the Gateway (default +// mode). The standalone Gateway instead forwards over NATS with +// taskactions.Client; both paths converge on the one taskactions.Service. +type taskActionsActor struct { + b *boot +} + +func (a taskActionsActor) ApplyChatTaskAction( + ctx context.Context, identity string, taskID int64, action taskstate.Action, +) (gateway.TaskActionResult, error) { + if err := a.b.taskActions().Apply(ctx, &identity, taskID, action); err != nil { + return gateway.TaskActionResult{}, err + } + return gateway.TaskActionResult{ + TaskID: taskID, + Action: string(action), + Message: fmt.Sprintf("Applied %s to task %d.", action, taskID), + }, nil +} + +// taskActions assembles the daemon-owned operator action service. The daemon +// retains execution cancellation, retry policy, forge closure and event +// ownership; the dashboard's webui service and the NATS responder both build +// through the same constructor so the surfaces cannot diverge. +func (b *boot) taskActions() taskactions.Service { + return taskactionstore.NewService( + taskactionstore.Store{TaskStore: b.st}, + taskactionstore.MaxRetries(config.NewHolder(b.cfg)), + b.cancelTask, + b.closeIssue, + b.removeTaskLogs, + b.publishEvent, + b.log.Warn, + ) +} + +func (b *boot) cancelTask(id int64) bool { + if b.d == nil { + return false + } + return b.d.CancelTask(id) +} + +func (b *boot) closeIssue(ctx context.Context, owner, repo string, number int, comment string) error { + if b.forgeClient == nil { + return fmt.Errorf("forge is not configured") + } + return b.forgeClient.CloseIssue(ctx, owner, repo, number, comment) +} + +func (b *boot) removeTaskLogs(id int64) error { + if b.taskLogs == nil { + return nil + } + return b.taskLogs.Remove(id) +} + +func (b *boot) publishEvent(e events.Event) { + if b.bus != nil { + b.bus.Publish(e) + } +} diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go new file mode 100644 index 000000000..c28113228 --- /dev/null +++ b/internal/domain/taskactions/service.go @@ -0,0 +1,201 @@ +// Package taskactions owns operator task mutations shared by chat and dashboard. +package taskactions + +import ( + "context" + "errors" + "fmt" + + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +var ( + ErrNotFound = errors.New("task not found") + ErrConflict = errors.New("task action conflict") + ErrUnavailable = errors.New("runtime task control is unavailable") +) + +// Task is the state needed to authorize and apply an operator action. +type Task struct { + ID int64 + Owner, Repo, Identity, Status, Stage, ParkReason string + IssueNumber, RetryCount int + ForgeBacked bool +} + +type Store interface { + TaskByID(context.Context, int64) (*Task, error) + Transition(context.Context, int64, string, string, string) error + Requeue(context.Context, int64, string, string) error + RetryTask(context.Context, int64, string, string) error + ArchiveTask(context.Context, int64, string, events.Event) (int64, error) + InsertEvent(context.Context, events.Event) (int64, error) +} + +// Service runs in the daemon, which owns execution cancellation and events. +type Service struct { + Store Store + MaxRetries func(*Task) int + CancelTask func(int64) bool + CloseIssue func(context.Context, string, string, int, string) error + RemoveLogs func(int64) error + Publish func(events.Event) + Warn func(string, ...any) +} + +// Apply scopes chat requests to an identity. A nil identity denotes an +// authenticated dashboard operator, who may act across identities. +func (s Service) Apply(ctx context.Context, identity *string, id int64, action taskstate.Action) error { + task, err := s.Store.TaskByID(ctx, id) + if err != nil { + return err + } + if task == nil { + return fmt.Errorf("task %d: %w", id, ErrNotFound) + } + if identity != nil && task.Identity != *identity { + return fmt.Errorf("task %d belongs to %q, not %q", id, task.Identity, *identity) + } + if err := taskstate.CheckAction(task.Status, action); err != nil { + return fmt.Errorf("%w: %w", ErrConflict, err) + } + mutation, err := s.apply(ctx, task, identity, action) + if err != nil { + return err + } + if mutation.verb != "" && task.ForgeBacked { + s.closeRejectedIssue(ctx, task, identity, mutation.verb) + } + s.emit(ctx, mutation.event) + return nil +} + +type outcome struct { + event events.Event + verb string +} + +// apply performs the store mutation for one action and returns the event to +// record alongside it. Errors from the store are returned unwrapped: the +// caller maps them to a response, and the rules-level errors (ErrConflict) +// were already checked above. +func (s Service) apply(ctx context.Context, task *Task, identity *string, action taskstate.Action) (outcome, error) { + source := "the dashboard" + if identity != nil { + source = "chat" + } + o := outcome{event: events.Event{TaskID: task.ID, Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber}} + switch action { + case taskstate.ActionApprove: + err := s.Store.Requeue(ctx, task.ID, "waiting_human", "implement") + o.event.Kind, o.event.Detail = events.KindHumanApproved, "approved via "+source + return o, err + case taskstate.ActionRetry: + return s.applyRetry(ctx, task, source, o) + case taskstate.ActionStop: + return s.applyStop(ctx, task, source, o) + case taskstate.ActionReject: + o.event.Kind, o.event.Detail, o.verb = events.KindHumanRejected, "rejected via "+source, "rejected" + if task.Status == "running" && s.CancelTask != nil { + s.CancelTask(task.ID) + } + return o, s.Store.Transition(ctx, task.ID, task.Status, "closed_wont_do", "declined from "+source) + case taskstate.ActionCancel, taskstate.ActionAbandon: + return s.applyCancelOrAbandon(ctx, task, action, source, o) + case taskstate.ActionArchive: + return s.applyArchive(ctx, task, source, o) + default: + return o, fmt.Errorf("unsupported task action %q", action) + } +} + +func (s Service) applyRetry(ctx context.Context, task *Task, source string, o outcome) (outcome, error) { + limit := 0 + if s.MaxRetries != nil { + limit = s.MaxRetries(task) + } + if limit > 0 && task.RetryCount >= limit { + reason := fmt.Sprintf("max retries reached (%d/%d)", task.RetryCount, limit) + if err := s.Store.Transition(ctx, task.ID, "parked", "dead", reason); err != nil { + s.warn("transition to dead failed", "task", task.ID, "err", err) + } else { + o.event.Kind, o.event.Detail = events.KindTaskDead, reason + s.emit(ctx, o.event) + } + return o, fmt.Errorf("%w: %s", ErrConflict, reason) + } + err := s.Store.RetryTask(ctx, task.ID, "parked", "") + o.event.Kind, o.event.Detail = events.KindTaskRetried, "retried via "+source + o.event.Data = map[string]any{"retry_count": task.RetryCount + 1, "previous_stage": task.Stage, "previous_reason": task.ParkReason} + return o, err +} + +func (s Service) applyStop(ctx context.Context, task *Task, source string, o outcome) (outcome, error) { + if s.CancelTask == nil { + return o, ErrUnavailable + } + o.event.Kind, o.event.Detail = events.KindTaskStopped, "stopped via "+source+"; recoverable work remains parked" + if !s.CancelTask(task.ID) { + o.event.Detail = "no active execution was found; recoverable work was parked via " + source + } + return o, s.Store.Transition(ctx, task.ID, "running", "parked", o.event.Detail) +} + +func (s Service) applyCancelOrAbandon(ctx context.Context, task *Task, action taskstate.Action, source string, o outcome) (outcome, error) { + from, kind, verb := "queued", events.KindTaskCancelled, "cancelled" + if action == taskstate.ActionAbandon { + from, kind, verb = "parked", events.KindTaskAbandoned, "abandoned" + } + o.event.Kind, o.event.Detail, o.verb = kind, verb+" via "+source, verb + return o, s.Store.Transition(ctx, task.ID, from, "closed_wont_do", o.event.Detail) +} + +func (s Service) applyArchive(ctx context.Context, task *Task, source string, o outcome) (outcome, error) { + o.event.Kind, o.event.Detail = events.KindTaskArchiveRequested, "archive requested via "+source + id, err := s.Store.ArchiveTask(ctx, task.ID, task.Status, o.event) + if err != nil { + return o, err + } + o.event.ID = id + if s.RemoveLogs != nil { + if cleanupErr := s.RemoveLogs(task.ID); cleanupErr != nil { + s.warn("task log cleanup failed", "task", task.ID, "err", cleanupErr) + } + } + return o, nil +} + +func (s Service) closeRejectedIssue(ctx context.Context, task *Task, identity *string, verb string) { + if s.CloseIssue == nil { + s.warn("task rejected but no forge is wired; the issue stays open and will be re-polled", "task", task.ID) + return + } + comment := fmt.Sprintf("Closing: this task was %s from the archie %s. Reopen the issue to have archie pick it up again.", verb, "dashboard") + if identity != nil { + comment = fmt.Sprintf("Closing: this task was %s from archie chat. Reopen the issue to have archie pick it up again.", verb) + } + if err := s.CloseIssue(ctx, task.Owner, task.Repo, task.IssueNumber, comment); err != nil { + s.warn("closing rejected issue failed; it stays open and will be re-polled", "task", task.ID, "err", err) + } +} + +func (s Service) emit(ctx context.Context, e events.Event) { + if e.ID == 0 { + id, err := s.Store.InsertEvent(ctx, e) + if err != nil { + s.warn("operator activity persistence failed", "kind", e.Kind, "task", e.TaskID, "err", err) + } else { + e.ID = id + } + } + if s.Publish != nil { + s.Publish(e) + } +} + +func (s Service) warn(msg string, args ...any) { + if s.Warn != nil { + s.Warn(msg, args...) + } +} diff --git a/internal/infrastructure/taskactions/rpc.go b/internal/infrastructure/taskactions/rpc.go new file mode 100644 index 000000000..b4208be42 --- /dev/null +++ b/internal/infrastructure/taskactions/rpc.go @@ -0,0 +1,77 @@ +package taskactions + +import ( + "context" + "encoding/json" + "fmt" + "log/slog" + "time" + + "github.com/nats-io/nats.go" + + "github.com/samcharles93/archie-core/internal/domain/taskactions" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/natsrpc" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// actionSubject is the request/reply subject the Gateway's ChatTaskActor +// client issues and the daemon's responder serves. The daemon owns task +// execution; the standalone Gateway never mutates the task store directly. +const actionSubject = "archie.gateway.task-action" + +type actionRequest struct { + Identity string `json:"identity"` + TaskID int64 `json:"task_id"` + Action taskstate.Action `json:"action"` +} + +type actionResponse struct { + natsrpc.Envelope +} + +// Register exposes only identity-scoped operator actions on the daemon. The +// daemon retains execution cancellation, retry policy, forge closure and event +// ownership; the Gateway's client reaches this responder over NATS. +// Handlers run with context.Background() (the storerpc convention): the +// daemon's lifecycle ctx is not request-scoped, and a cancelled boot ctx must +// not poison later requests. +func Register(nc *nats.Conn, service taskactions.Service, log *slog.Logger) (func(), error) { + return natsrpc.RegisterAll(nc, []natsrpc.Registration{{ + Subject: actionSubject, + Handler: func(msg *nats.Msg) { + var req actionRequest + if err := json.Unmarshal(msg.Data, &req); err != nil { + natsrpc.Respond(msg, log, "taskactions", actionResponse{Envelope: natsrpc.NewEnvelope(err)}) + return + } + err := service.Apply(context.Background(), &req.Identity, req.TaskID, req.Action) + natsrpc.Respond(msg, log, "taskactions", actionResponse{Envelope: natsrpc.NewEnvelope(err)}) + }, + }}) +} + +// Client forwards Gateway task actions to their daemon owner over NATS. +type Client struct { + Conn *nats.Conn + Timeout time.Duration // bounds each call when ctx has no deadline +} + +func (c Client) rpc() *natsrpc.Client { + return &natsrpc.Client{Conn: c.Conn, Timeout: c.Timeout} +} + +// ApplyChatTaskAction sends an operator action to the daemon's responder. +func (c Client) ApplyChatTaskAction(ctx context.Context, identity string, id int64, action taskstate.Action) (gateway.TaskActionResult, error) { + if c.Conn == nil { + return gateway.TaskActionResult{}, fmt.Errorf("task action connection is unavailable") + } + resp, err := natsrpc.Call[actionResponse](ctx, c.rpc(), actionSubject, actionRequest{Identity: identity, TaskID: id, Action: action}) + if err != nil { + return gateway.TaskActionResult{}, err + } + if err := resp.Err(); err != nil { + return gateway.TaskActionResult{}, err + } + return gateway.TaskActionResult{TaskID: id, Action: string(action), Message: fmt.Sprintf("Applied %s to task %d.", action, id)}, nil +} diff --git a/internal/infrastructure/taskactions/service.go b/internal/infrastructure/taskactions/service.go new file mode 100644 index 000000000..e9e387456 --- /dev/null +++ b/internal/infrastructure/taskactions/service.go @@ -0,0 +1,32 @@ +package taskactions + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/domain/taskactions" + "github.com/samcharles93/archie-core/internal/events" +) + +// NewService assembles the operator task-action service from its production +// dependencies. Both the dashboard's in-process service (webui) and the +// daemon's NATS responder build through this one constructor so the two +// surfaces cannot diverge. +func NewService( + store taskactions.Store, + maxRetries func(*taskactions.Task) int, + cancel func(int64) bool, + closeIssue func(context.Context, string, string, int, string) error, + removeLogs func(int64) error, + publish func(events.Event), + warn func(string, ...any), +) taskactions.Service { + return taskactions.Service{ + Store: store, + MaxRetries: maxRetries, + CancelTask: cancel, + CloseIssue: closeIssue, + RemoveLogs: removeLogs, + Publish: publish, + Warn: warn, + } +} diff --git a/internal/infrastructure/taskactions/store.go b/internal/infrastructure/taskactions/store.go new file mode 100644 index 000000000..77b8afa8a --- /dev/null +++ b/internal/infrastructure/taskactions/store.go @@ -0,0 +1,35 @@ +// Package taskactions adapts legacy task persistence to operator actions. +package taskactions + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/taskactions" + "github.com/samcharles93/archie-core/internal/store" +) + +type Store struct{ store.TaskStore } + +func (s Store) TaskByID(ctx context.Context, id int64) (*taskactions.Task, error) { + t, err := s.TaskStore.TaskByID(ctx, id) + if err != nil || t == nil { + return nil, err + } + return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, Stage: t.Stage, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, RetryCount: t.RetryCount, ForgeBacked: t.IsForgeBacked()}, nil +} + +func MaxRetries(cfg *config.Holder) func(*taskactions.Task) int { + return func(t *taskactions.Task) int { + if cfg == nil { + return 0 + } + c := cfg.Get() + for _, repo := range c.Repos { + if repo.Owner == t.Owner && repo.Name == t.Repo { + return repo.EffectiveMaxRetries(c.MaxRetries) + } + } + return c.MaxRetries + } +} diff --git a/internal/webui/api_tasks.go b/internal/webui/api_tasks.go index b45bb8f38..e11e5fb73 100644 --- a/internal/webui/api_tasks.go +++ b/internal/webui/api_tasks.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "fmt" "io" "mime" "net/http" @@ -13,7 +12,9 @@ import ( "strings" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/taskactions" "github.com/samcharles93/archie-core/internal/events" + taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions" "github.com/samcharles93/archie-core/internal/store" "github.com/samcharles93/archie-core/internal/taskstate" ) @@ -146,36 +147,20 @@ func (s *Server) handleTaskAction(w http.ResponseWriter, r *http.Request) { if !ok { return } - task, err := s.Store.TaskByID(r.Context(), id) - if err != nil { - s.logf("task action lookup failed", "task", id, "err", err) - http.Error(w, "task action failed", http.StatusInternalServerError) + if err := s.TaskActionService().Apply(r.Context(), nil, id, action); err != nil { + switch { + case errors.Is(err, taskactions.ErrNotFound): + http.Error(w, "task not found", http.StatusNotFound) + case errors.Is(err, taskactions.ErrConflict), errors.Is(err, store.ErrStaleTransition): + http.Error(w, err.Error(), http.StatusConflict) + case errors.Is(err, taskactions.ErrUnavailable): + http.Error(w, err.Error(), http.StatusServiceUnavailable) + default: + s.logf("task action failed", "task", id, "err", err) + http.Error(w, "task action failed", http.StatusInternalServerError) + } return } - if task == nil { - http.Error(w, "task not found", http.StatusNotFound) - return - } - if err := taskstate.CheckAction(task.Status, action); err != nil { - http.Error(w, err.Error(), http.StatusConflict) - return - } - - // Each action reports the event to record, or writes its own error - // response and returns nothing. Keeping the dispatch flat here and the - // rules in one function each is what stops this growing back into a - // single branch nobody can read. - outcome := s.applyTaskAction(r.Context(), w, task, action) - if outcome == nil { - return - } - - s.emit(r.Context(), events.Event{ - ID: outcome.eventID, - Kind: outcome.kind, TaskID: task.ID, - Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber, - Detail: outcome.detail, Data: outcome.data, - }) writeJSON(w, map[string]any{"ok": true, "action": action, "task_id": id}) } @@ -204,32 +189,6 @@ func decodeTaskAction(w http.ResponseWriter, r *http.Request) (taskstate.Action, return action, true } -func (s *Server) applyTaskAction( - ctx context.Context, - w http.ResponseWriter, - task *store.Task, - action taskstate.Action, -) *actionOutcome { - switch action { - case taskstate.ActionCancel: - return s.declineTask(ctx, w, task, store.StatusQueued, events.KindTaskCancelled, "cancelled") - case taskstate.ActionStop: - return s.stopTask(ctx, w, task) - case taskstate.ActionApprove: - return s.approveTask(ctx, w, task) - case taskstate.ActionRetry: - return s.retryTask(ctx, w, task) - case taskstate.ActionReject: - return s.rejectTask(ctx, w, task) - case taskstate.ActionAbandon: - return s.declineTask(ctx, w, task, store.StatusParked, events.KindTaskAbandoned, "abandoned") - case taskstate.ActionArchive: - return s.archiveTask(ctx, w, task) - default: - return nil - } -} - func taskMutation(action taskstate.Action) bool { switch action { case taskstate.ActionCancel, taskstate.ActionStop, taskstate.ActionApprove, @@ -315,205 +274,46 @@ func validOrigin(u *url.URL, wantScheme, wantHost string) bool { return strings.EqualFold(u.Scheme, wantScheme) && strings.EqualFold(u.Host, wantHost) } -// actionOutcome is what a completed operator action should record. -type actionOutcome struct { - kind string - detail string - data map[string]any - eventID int64 +// TaskActionService supplies the same operator action implementation used by +// the daemon's task-control endpoint. +func (s *Server) TaskActionService() taskactions.Service { + return taskactionstore.NewService( + taskactionstore.Store{TaskStore: s.Store}, + taskactionstore.MaxRetries(s.Cfg), + s.taskStopper(), + s.issueCloser(), + s.logRemover(), + s.eventPublisher(), + s.logf, + ) } -// storeFailed writes the response for a failed store write and reports -// whether it did. A store write that fails is not the caller's fault; -// answering 409 made the UI say "conflict" for a broken database. -func (s *Server) storeFailed(w http.ResponseWriter, err error) bool { - if err == nil { - return false - } - if errors.Is(err, store.ErrStaleTransition) { - http.Error(w, "task changed state; refresh and try again", http.StatusConflict) - return true - } - s.logf("task action store write failed", "err", err) - http.Error(w, "task action failed", http.StatusInternalServerError) - return true -} - -func (s *Server) declineTask( - ctx context.Context, - w http.ResponseWriter, - task *store.Task, - from, kind, verb string, -) *actionOutcome { - detail := verb + " via the dashboard" - if s.storeFailed(w, s.Store.Transition(ctx, task.ID, from, store.StatusClosedWontDo, detail)) { - return nil - } - s.closeDeclinedIssue(ctx, task, verb) - return &actionOutcome{kind: kind, detail: detail} -} - -func (s *Server) stopTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome { +func (s *Server) taskStopper() func(int64) bool { if s.TaskStopper == nil { - http.Error(w, "runtime task control is unavailable", http.StatusServiceUnavailable) return nil } - cancelled := s.TaskStopper.CancelTask(task.ID) - detail := "stopped via the dashboard; recoverable work remains parked" - if !cancelled { - // A running row without a runtime entry is stale work from a crashed or - // migrated process. Nothing is executing, but parking the guarded row - // still gives the operator the promised recoverable state. - detail = "no active execution was found; recoverable work was parked via the dashboard" - } - if s.storeFailed(w, s.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, detail)) { - return nil - } - return &actionOutcome{kind: events.KindTaskStopped, detail: detail} + return s.TaskStopper.CancelTask } -func (s *Server) archiveTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome { - detail := "archive requested via the dashboard" - eventID, err := s.Store.ArchiveTask(ctx, task.ID, task.Status, events.Event{ - Kind: events.KindTaskArchiveRequested, TaskID: task.ID, - Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber, - Detail: detail, - }) - if s.storeFailed(w, err) { - return nil - } - // Best-effort: an archived task's log files not getting cleaned up is - // far less costly than the archive action itself failing over it, so - // this never blocks the response the way s.storeFailed above does. - // - // Known, accepted gap: a task reaches a terminal status (satisfying - // ArchiveTask's precondition) partway through Daemon.process, but its - // TaskLogs sink isn't closed until process() itself returns -- which can - // be later, if teardown after the terminal transition takes any time at - // all. A system-log message landing in that window races this Remove - // with an open Write to the same directory. Not corrupting (the write - // lands on a soon-to-be-unlinked inode and is discarded) and not worth - // closing here: doing so would mean tying TaskLogs.Close to the store - // transition instead of to process()'s return, a larger change than - // this call site should make unilaterally. - if err := s.TaskLogs.Remove(task.ID); err != nil { - s.Log.Warn("task log cleanup failed", "task", task.ID, "err", err) - } - return &actionOutcome{kind: events.KindTaskArchiveRequested, detail: detail, eventID: eventID} -} - -func (s *Server) approveTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome { - if err := taskstate.CheckApprove(task.Status); err != nil { - http.Error(w, err.Error(), http.StatusConflict) - return nil - } - if s.storeFailed(w, s.Store.Requeue(ctx, task.ID, store.StatusWaitingHuman, "implement")) { - return nil - } - return &actionOutcome{kind: events.KindHumanApproved, detail: "approved via the dashboard"} -} - -// retryTask requeues a parked task, or retires it once max_retries is spent. -// -// max_retries was configured, defaulted and reported by /api/config while -// nothing enforced it: retry_count climbed forever, so a task that fails -// every time could be retried without limit. At the cap the task belongs in -// dead, which is what the setting exists to say. -func (s *Server) retryTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome { - if err := taskstate.CheckRetry(task.Status); err != nil { - http.Error(w, err.Error(), http.StatusConflict) - return nil - } - if limit := s.maxRetriesFor(task); limit > 0 && task.RetryCount >= limit { - s.retireTask(ctx, task, fmt.Sprintf("max retries reached (%d/%d)", task.RetryCount, limit)) - http.Error(w, fmt.Sprintf("max retries reached (%d/%d)", task.RetryCount, limit), http.StatusConflict) - return nil - } - if s.storeFailed(w, s.Store.RetryTask(ctx, task.ID, store.StatusParked, "")) { - return nil - } - return &actionOutcome{ - kind: events.KindTaskRetried, - detail: "retried via the dashboard", - data: map[string]any{ - "retry_count": task.RetryCount + 1, - "previous_stage": task.Stage, - "previous_reason": task.ParkReason, - }, - } -} - -// retireTask moves a spent task to dead. Failure is logged, not returned: -// the caller is already answering with the reason it refused the retry. -func (s *Server) retireTask(ctx context.Context, task *store.Task, reason string) { - if err := s.Store.Transition(ctx, task.ID, store.StatusParked, store.StatusDead, reason); err != nil { - s.logf("transition to dead failed", "task", task.ID, "err", err) - return - } - s.emit(ctx, events.Event{ - Kind: events.KindTaskDead, TaskID: task.ID, - Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber, Detail: reason, - }) -} - -func (s *Server) rejectTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome { - // Reject is available from any non-terminal lifecycle state: work the - // operator refuses belongs in the terminal Declined state regardless of - // where it currently sits. Running work is interrupted first so nothing - // keeps executing against a task that is being closed out. - if task.Status == store.StatusRunning && s.TaskStopper != nil { - s.TaskStopper.CancelTask(task.ID) - } - err := s.Store.Transition(ctx, task.ID, task.Status, store.StatusClosedWontDo, "declined from the dashboard") - if s.storeFailed(w, err) { - return nil - } - s.closeDeclinedIssue(ctx, task, "rejected") - return &actionOutcome{kind: events.KindHumanRejected, detail: "rejected via the dashboard"} -} - -// maxRetriesFor resolves the retry cap for a task, honouring a per-repo -// override. Zero means unlimited. -func (s *Server) maxRetriesFor(task *store.Task) int { - if s.Cfg == nil { - return 0 - } - cfg := s.Cfg.Get() - for _, repo := range cfg.Repos { - if repo.Owner == task.Owner && repo.Name == task.Repo { - return repo.EffectiveMaxRetries(cfg.MaxRetries) - } - } - return cfg.MaxRetries -} - -// closeRejectedIssue closes the forge issue behind a task the operator has -// declined. -// -// Without this the issue stays open, labelled and assigned, so the next poll -// re-enqueues it -- and once the operator uses Clear, which deletes the task -// row, that is a second implementation and a second PR for work already -// refused. Approve and retry deliberately leave the issue open: the task is -// going back to work. -// -// Failure is logged, not returned. The operator's decision is already -// recorded, and reporting an error would invite them to click again. -func (s *Server) closeDeclinedIssue(ctx context.Context, task *store.Task, verb string) { +func (s *Server) issueCloser() func(context.Context, string, string, int, string) error { if s.Issues == nil { - s.logf("task rejected but no forge is wired; the issue stays open and will be re-polled", - "task", task.ID, "issue", task.IssueNumber) - return + return nil } - // A chat task's issue number is synthetic and matches no forge issue. - if !task.IsForgeBacked() { - return + return s.Issues.CloseIssue +} + +func (s *Server) logRemover() func(int64) error { + if s.TaskLogs == nil { + return nil } - comment := fmt.Sprintf("Closing: this task was %s from the archie dashboard. "+ - "Reopen the issue to have archie pick it up again.", verb) - if err := s.Issues.CloseIssue(ctx, task.Owner, task.Repo, task.IssueNumber, comment); err != nil { - s.logf("closing rejected issue failed; it stays open and will be re-polled", - "task", task.ID, "issue", task.IssueNumber, "err", err) + return s.TaskLogs.Remove +} + +func (s *Server) eventPublisher() func(events.Event) { + if s.Events == nil { + return nil } + return s.Events.Publish } // emit publishes an operator action so it reaches the task timeline and the