From 712388dc6cc4cba2020e509f58441b7a8b3cbb70 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 29 Jul 2026 10:15:22 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20Phase=204=20structural=20refactor=20?= =?UTF-8?q?=E2=80=94=20reduce=20lint=20findings=20from=2028=20to=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cyclop (5→0): extract subcommand/case bodies in codesearch, subprocess, Telegram, indexing funlen (5→3): extract memory action handlers, tool provider startup gocognit (7→2): extract config finalize/loadDirInto helpers, release announcer, capture tool validation, Telegram gateway (200→108), archie-agent main loop (35→0) nestif (8→0): extract identity validation, container validation, conf.d scanning, email multipart parsing, Telegram model/update parsing, build-no-changes closure interfacebloat (3→0): split Forge/IssueForge/PRForge/RepoForge, SessionStore/SessionLifecycle/MessageHistory, TaskStore/TaskLifecycle/TaskQueries/TaskEvents Remaining: daemon process, workflow Feasibility/TDD/AgentStage, archied main.go run (108→30 remaining). These require deeper refactoring best done per-function in follow-up beads. Updated contract tests and archied wiring tests to check both main.go and the new telegram_setup.go. --- cmd/archie-agent/main.go | 180 ++++++++-------- cmd/archied/codesearch.go | 71 +++--- cmd/archied/main.go | 262 +---------------------- cmd/archied/main_test.go | 7 +- cmd/archied/release_metadata_test.go | 13 +- cmd/archied/telegram_setup.go | 227 ++++++++++++++++++++ internal/agentexec/inprocess.go | 81 ++++--- internal/agentexec/subprocess.go | 10 +- internal/channels/email/email.go | 45 ++-- internal/channels/telegram/model.go | 110 ++++++---- internal/channels/telegram/update.go | 5 + internal/config/config.go | 245 ++++++++++++--------- internal/config/feature_config.go | 171 ++++++++------- internal/forge/forge.go | 31 ++- internal/gateway/gateway.go | 10 +- internal/gateway/gateway_test.go | 9 +- internal/gateway/session_store.go | 11 +- internal/indexing/codesearch.go | 12 +- internal/memory/builtin/provider.go | 139 ++++++------ internal/releaseannounce/announcer.go | 172 ++++++++------- internal/store/interface.go | 30 +-- internal/tools/provider/contract_test.go | 16 +- internal/tools/provider/provider.go | 91 ++++---- internal/workflow/steps.go | 28 ++- 24 files changed, 1090 insertions(+), 886 deletions(-) create mode 100644 cmd/archied/telegram_setup.go diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index 015ac0fa7..b135f56d2 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -79,55 +79,14 @@ func run() int { log.Info("nats connected", "url", natsURL) // JetStream setup. - js, err := jetstream.New(nc) + _, cons, err := setupJetStream(ctx, nc, *consumer) if err != nil { - log.Error("jetstream init failed", "err", err) + log.Error("jetstream setup failed", "err", err) return 1 } - stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{ - Name: streamName, - Subjects: []string{"archie.task.>", "archie.agent.>"}, - Storage: jetstream.FileStorage, - Retention: jetstream.WorkQueuePolicy, - Duplicates: dedupWindow, - }) - if err != nil { - log.Error("stream setup failed", "err", err) - return 1 - } - - cons, err := stream.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{ - Name: *consumer, - Durable: *consumer, - FilterSubject: arnats.SubjectAgentWildcard, - AckPolicy: jetstream.AckExplicitPolicy, - MaxDeliver: 3, - AckWait: ackWait, - InactiveThreshold: 24 * time.Hour, - }) - if err != nil { - log.Error("consumer setup failed", "err", err) - return 1 - } - - // Prefer a dedicated per-task subscription over the shared queue group - // whenever this container was spawned for a specific task (task.json - // present at the worktree mount): with concurrent multi-task dispatch, - // a queue-group member could otherwise pick up a message meant for a - // sibling container that has a *different* task's worktree mounted. - var taskRunSub *nats.Subscription - if taskID, ok := bootTaskID(storage.WorktreeMountDir, log); ok { - taskRunSub, err = nc.Subscribe(taskrun.SubjectForTask(taskID), func(msg *nats.Msg) { - handleTaskRun(ctx, msg, nc, log) - }) - log.Info("taskrun: dedicated per-task subscription", "task", taskID) - } else { - taskRunSub, err = nc.QueueSubscribe(taskRunSubjectWildcard, taskRunQueueGroup, func(msg *nats.Msg) { - handleTaskRun(ctx, msg, nc, log) - }) - log.Info("taskrun: shared queue-group subscription (no task.json found)") - } + // Subscribe to task run messages. + taskRunSub, err := subscribeTaskRuns(ctx, nc, log) if err != nil { log.Error("taskrun subscribe failed", "err", err) return 1 @@ -139,51 +98,53 @@ func run() int { }() log.Info("archie-agent ready", "nats", natsURL, "consumer", *consumer) + return runMainLoop(ctx, cons, nc, log) +} - // Main loop: fetch, run, reply, ack. - for { - if ctx.Err() != nil { - log.Info("archie-agent shutting down") - return 0 - } - - batch, err := cons.Fetch(1, jetstream.FetchMaxWait(pollTimeout)) - if err != nil { - if ctx.Err() != nil { - return 0 - } - log.Error("fetch failed", "err", err) - time.Sleep(1 * time.Second) - continue - } - - var msg jetstream.Msg - for m := range batch.Messages() { - if m != nil { - msg = m - break - } - } - if err := batch.Error(); err != nil { - log.Error("fetch batch error", "err", err) - continue - } - if msg == nil { - continue - } - - // Process the message. - if err := handle(ctx, msg, nc, log); err != nil { - log.Error("handle failed", "err", err) - if err := msg.Nak(); err != nil { - log.Warn("nak failed", "err", err) - } - continue - } - if err := msg.Ack(); err != nil { - log.Warn("ack failed", "err", err) - } +func setupJetStream(ctx context.Context, nc *nats.Conn, consumerName string) (jetstream.JetStream, jetstream.Consumer, error) { + js, err := jetstream.New(nc) + if err != nil { + return nil, nil, err } + stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{ + Name: streamName, + Subjects: []string{"archie.task.>", "archie.agent.>"}, + Storage: jetstream.FileStorage, + Retention: jetstream.WorkQueuePolicy, + Duplicates: dedupWindow, + }) + if err != nil { + return nil, nil, err + } + cons, err := stream.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{ + Name: consumerName, + Durable: consumerName, + FilterSubject: arnats.SubjectAgentWildcard, + AckPolicy: jetstream.AckExplicitPolicy, + MaxDeliver: 3, + AckWait: ackWait, + InactiveThreshold: 24 * time.Hour, + }) + if err != nil { + return nil, nil, err + } + return js, cons, nil +} + +// subscribeTaskRuns prefers a dedicated per-task subscription over the +// shared queue group whenever this container was spawned for a specific +// task (task.json present at the worktree mount). +func subscribeTaskRuns(ctx context.Context, nc *nats.Conn, log *slog.Logger) (*nats.Subscription, error) { + if taskID, ok := bootTaskID(storage.WorktreeMountDir, log); ok { + log.Info("taskrun: dedicated per-task subscription", "task", taskID) + return nc.Subscribe(taskrun.SubjectForTask(taskID), func(msg *nats.Msg) { + handleTaskRun(ctx, msg, nc, log) + }) + } + log.Info("taskrun: shared queue-group subscription (no task.json found)") + return nc.QueueSubscribe(taskRunSubjectWildcard, taskRunQueueGroup, func(msg *nats.Msg) { + handleTaskRun(ctx, msg, nc, log) + }) } // bootTaskID reads the boot-time task.json brief the daemon writes into @@ -206,6 +167,49 @@ func bootTaskID(mountDir string, log *slog.Logger) (int64, bool) { return payload.ID, true } +// runMainLoop runs the fetch-handle-ack loop until the context is cancelled. +func runMainLoop(ctx context.Context, cons jetstream.Consumer, nc *nats.Conn, log *slog.Logger) int { + for { + if ctx.Err() != nil { + log.Info("archie-agent shutting down") + return 0 + } + batch, err := cons.Fetch(1, jetstream.FetchMaxWait(pollTimeout)) + if err != nil { + if ctx.Err() != nil { + return 0 + } + log.Error("fetch failed", "err", err) + time.Sleep(1 * time.Second) + continue + } + var msg jetstream.Msg + for m := range batch.Messages() { + if m != nil { + msg = m + break + } + } + if err := batch.Error(); err != nil { + log.Error("fetch batch error", "err", err) + continue + } + if msg == nil { + continue + } + if err := handle(ctx, msg, nc, log); err != nil { + log.Error("handle failed", "err", err) + if err := msg.Nak(); err != nil { + log.Warn("nak failed", "err", err) + } + continue + } + if err := msg.Ack(); err != nil { + log.Warn("ack failed", "err", err) + } + } +} + func natsConnectionSettings(flagURL string, getenv func(string) string) (url, token string) { url = flagURL if url == "" { diff --git a/cmd/archied/codesearch.go b/cmd/archied/codesearch.go index cef7433ee..2b0dfacd1 100644 --- a/cmd/archied/codesearch.go +++ b/cmd/archied/codesearch.go @@ -62,39 +62,7 @@ func runCodesearchHelper(args []string, out io.Writer) int { return 0 case "candidates": - fs := newHelperFlagSet("candidates") - index := fs.String("index", "", "path of the index sidecar to query") - pattern := fs.String("pattern", "", "search pattern") - literal := fs.Bool("literal", false, "treat the pattern as a literal string") - caseSensitive := fs.Bool("case-sensitive", false, "match case sensitively") - if err := fs.Parse(args[1:]); err != nil { - return 2 - } - if *index == "" || *pattern == "" { - helperErrorf("candidates: --index and --pattern are required\n") - return 2 - } - files, err := indexing.IndexCandidates(*index, *pattern, *literal, *caseSensitive) - if err != nil { - helperErrorf("candidates: %v\n", err) - return 1 - } - // The parent decodes a JSON array and treats any decode failure as - // "no index", so a nil slice must still marshal as [] rather than - // null. - if files == nil { - files = []string{} - } - encoded, err := json.Marshal(files) - if err != nil { - helperErrorf("candidates: encode result: %v\n", err) - return 1 - } - if _, err := out.Write(encoded); err != nil { - helperErrorf("candidates: write result: %v\n", err) - return 1 - } - return 0 + return runCandidates(args[1:], out) default: helperErrorf("%s: unknown subcommand %q\n", codesearchHelperCommand, args[0]) @@ -108,6 +76,43 @@ func newHelperFlagSet(name string) *flag.FlagSet { return fs } +// runCandidates handles the "candidates" subcommand, returning candidate +// file paths as a JSON array on stdout. +func runCandidates(args []string, out io.Writer) int { + fs := newHelperFlagSet("candidates") + index := fs.String("index", "", "path of the index sidecar to query") + pattern := fs.String("pattern", "", "search pattern") + literal := fs.Bool("literal", false, "treat the pattern as a literal string") + caseSensitive := fs.Bool("case-sensitive", false, "match case sensitively") + if err := fs.Parse(args); err != nil { + return 2 + } + if *index == "" || *pattern == "" { + helperErrorf("candidates: --index and --pattern are required\n") + return 2 + } + files, err := indexing.IndexCandidates(*index, *pattern, *literal, *caseSensitive) + if err != nil { + helperErrorf("candidates: %v\n", err) + return 1 + } + // The parent decodes a JSON array and treats any decode failure as + // "no index", so a nil slice must still marshal as [] rather than null. + if files == nil { + files = []string{} + } + encoded, err := json.Marshal(files) + if err != nil { + helperErrorf("candidates: encode result: %v\n", err) + return 1 + } + if _, err := out.Write(encoded); err != nil { + helperErrorf("candidates: write result: %v\n", err) + return 1 + } + return 0 +} + // helperErrorf writes a diagnostic to stderr. The write result is ignored // deliberately: if stderr is gone there is nowhere left to report it, and the // exit code already carries the outcome the parent acts on. diff --git a/cmd/archied/main.go b/cmd/archied/main.go index a306884e5..9cc1c6d95 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -26,7 +26,6 @@ import ( "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/channels/email" - "github.com/samcharles93/archie-core/internal/channels/telegram" "github.com/samcharles93/archie-core/internal/channels/webhook" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/container" @@ -41,8 +40,6 @@ import ( "github.com/samcharles93/archie-core/internal/nell" "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin/pluginextract" - "github.com/samcharles93/archie-core/internal/releaseannounce" - "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" @@ -283,254 +280,17 @@ func run() int { // ── Gateways ────────────────────────────────────────────────────── // Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md). var startGateways []func() - if cfg.Chat.Telegram.TokenEnv != "" { - tgToken := os.Getenv(cfg.Chat.Telegram.TokenEnv) - if tgToken == "" { - log.Error("chat.telegram configured but token env var is empty", "env", cfg.Chat.Telegram.TokenEnv) - return 1 - } - // Deny-by-default: an empty allowlist blocks everyone. A bot handle - // is public, so failing open would expose the chat agent's tools, - // which run with this daemon's authority, to any stranger. - if len(cfg.Chat.Telegram.AllowedUserIDs) == 0 { - log.Warn("chat.telegram has no allowed_user_ids: every sender will be rejected. " + - "Add your Telegram user id to chat.telegram.allowed_user_ids to enable the bot.") - } - tg := telegram.New(tgToken, "", "", cfg.Chat.Telegram.AllowedUserIDs, log) - tg.Version = func() string { - return fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion) - } - if len(cfg.Chat.Telegram.UpdateCheckCommand) != 0 { - updates := &releaseupdate.Service{ - Catalog: releaseupdate.CommandCatalog{Command: cfg.Chat.Telegram.UpdateCheckCommand}, - StatePath: filepath.Join(cfg.WorkDir, "telegram-update-deferrals.json"), - } - if len(cfg.Chat.Telegram.UpdateInstallCommand) != 0 { - updates.Installer = releaseupdate.CommandInstaller{Command: cfg.Chat.Telegram.UpdateInstallCommand} - } - tg.Updates = updates - } - releaseAnnouncements := &releaseannounce.Announcer{ - StatePath: releaseAnnouncementStatePath(cfg.WorkDir, cfg.BotUser), - Components: []releaseannounce.Component{ - { - ID: "gateway", - Label: "THE GATEWAY", - Version: gatewayVersion, - ChangelogPath: packagedGatewayChangelogPath, - }, - { - ID: "runtime", - Label: "THE RUNTIME", - Version: runtimeVersion, - ChangelogPath: packagedRuntimeChangelogPath, - }, - }, - } - tg.ReleaseAnnouncements = releaseAnnouncements - - // /restart re-reads config from disk so allowlist and token edits - // apply without restarting the daemon and killing running tasks. - // Only gateway-scoped fields are refreshed; everything else needs a - // full restart, since it was wired into the daemon at construction. - tg.Reload = func(g *telegram.Gateway) error { - newCfg, err := config.LoadOverlay(*cfgPath, *overlayPath) - if err != nil { - return fmt.Errorf("reload config: %w", err) - } - tokenEnv := newCfg.Chat.Telegram.TokenEnv - if tokenEnv == "" { - return fmt.Errorf("reload config: chat.telegram.token_env is unset") - } - token := os.Getenv(tokenEnv) - if token == "" { - return fmt.Errorf("reload config: %s is empty", tokenEnv) - } - g.Token = token - g.AllowedUserIDs = newCfg.Chat.Telegram.AllowedUserIDs - log.Info("chat gateway config reloaded", - "allowed_user_ids", len(g.AllowedUserIDs)) - return nil - } - - // Session store backed by the same NellDB log as task state. - // Sessions and messages survive daemon restarts. - var sessionStore gateway.SessionStore - if nellAdapter, ok := st.(*nell.Adapter); ok { - sessionStore = gateway.NewSessionStore(nellAdapter.Store(), cfg.BotUser) - } else { - sessionStore = gateway.NewSessionStoreMemory(cfg.BotUser) - } - - // Resolve the session ID for a message, creating one if needed. - // Uses the session tracking layer so /new, /branch, and /topic - // commands that switch the active session are respected by the - // LLM responder. - resolveSession := func(ctx context.Context, msg gateway.Message) string { - key := sessionKey(msg) - return key - } - - // Build an LLM responder from the runtime. Gateway-local commands - // (/status, /model, /spawn) are handled directly; all - // other messages are routed through the LLM with conversation - // history from the session store. - var llmResponder gateway.LLMResponder - var llmStream gateway.LLMStreamResponder - if llm != nil { - // respond runs one chat turn. When onDelta is non-nil the reply - // is streamed and each fragment reported as it arrives; the - // assembled text is returned either way, so both the blocking - // and streaming responders share this single path. - respond := func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) { - sessionKey := resolveSession(ctx, msg) - if err := sessionStore.SaveMessage(ctx, sessionKey, msg); err != nil { - return "", fmt.Errorf("save inbound chat message: %w", err) - } - - // Load recent conversation history for context. - // Use a larger fetch window so compression has room to work - // — older messages get summarised, recent ones stay intact. - history, err := sessionStore.RecentMessages(ctx, sessionKey, 100) - if err != nil { - return "", fmt.Errorf("load chat history: %w", err) - } - compressed := make([]gateway.CompressedMessage, 0, len(history)) - for _, h := range history { - role := "user" - if h.From == cfg.BotUser { - role = "assistant" - } - compressed = append(compressed, gateway.CompressedMessage{ - Role: role, - Content: h.Text, - }) - } - // Apply context compression for long-running sessions. - compCfg := gateway.DefaultCompressionConfig() - view := gateway.CompressHistory(compressed, compCfg) - - // Build the toolset first so the system prompt can advertise - // exactly the tools the model is about to be handed. Deriving - // the inventory from any other source lets the prompt drift - // from reality, which is what made the agent offer to run - // commands it had no tool for. - options, err := chatGenerateOptions(nil, toolReg) - if err != nil { - return "", fmt.Errorf("build chat tools: %w", err) - } - - chatModel := chatModels.ActiveModel() - systemPrompt := gateway.BuildSystemPrompt(gateway.SystemPromptConfig{ - Persona: personas.GetActive(sessionKey), - Tools: toolSummaries(options.Tools), - Channel: tg.Name(), - Model: chatModel, - SessionID: sessionKey, - Now: time.Now(), - }) - view.Messages = append( - []gateway.CompressedMessage{{Role: "system", Content: systemPrompt}}, - view.Messages..., - ) - - // Convert to chat.Message for the LLM. - messages := make([]chat.Message, len(view.Messages)) - for i, cm := range view.Messages { - role := chat.RoleUser - switch cm.Role { - case "assistant": - role = chat.RoleAssistant - case "system": - role = chat.RoleSystem - } - messages[i] = chat.Message{ - Role: role, - Content: cm.Content, - } - } - - options.Messages = messages - - var text string - if onDelta == nil { - result, err := llm.Chat(ctx, chatModel, options) - if err != nil { - return "", fmt.Errorf("llm chat: %w", err) - } - text = result.Text - } else { - stream, err := llm.ChatStream(ctx, chatModel, options) - if err != nil { - return "", fmt.Errorf("llm chat stream: %w", err) - } - // Consume FullStream, not TextStream. Per the ai-sdk - // channel contract FullStream is authoritative and MUST - // be drained until close: its writes are synchronous, so - // leaving it unread stalls the producer and deadlocks the - // whole turn. TextStream is a best-effort view that drops - // deltas when it is not being read, so it cannot be used - // to reconstruct the reply either. - var sb strings.Builder - for part := range stream.FullStream { - if part.Type != core.StreamPartTextDelta || part.TextDelta == "" { - continue - } - sb.WriteString(part.TextDelta) - onDelta(part.TextDelta) - } - // Resolves once the producer completes, so a mid-stream - // failure surfaces here rather than a truncated reply - // passing as a short but complete one. - if _, err := stream.FinishReason(); err != nil { - return "", fmt.Errorf("llm chat stream: %w", err) - } - text = sb.String() - } - - // Persist the response. - if err := sessionStore.SaveMessage(ctx, sessionKey, gateway.Message{ - From: cfg.BotUser, - Text: text, - }); err != nil { - return "", fmt.Errorf("save outbound chat message: %w", err) - } - - return text, nil - } - - llmResponder = func(ctx context.Context, msg gateway.Message) (string, error) { - return respond(ctx, msg, nil) - } - llmStream = func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) { - return respond(ctx, msg, onDelta) - } - } - - router := gateway.NewRouter(st, llmResponder, "telegram") - router.LLMStream = llmStream - router.Models = chatModels - router.InitSessions(sessionStore) - - // Wire session resolution so LLM turns respect active sessions - // set by /new, /branch, and /topic commands. - resolveSession = func(ctx context.Context, msg gateway.Message) string { - key, err := router.ResolveSessionKey(ctx, msg) - if err != nil { - return sessionKey(msg) - } - return key - } - - configureTaskCommands(router, chatTasks, chatController, defaultChatIdentity) - startGateways = append(startGateways, func() { - go func() { - if err := tg.Start(ctx, router); err != nil && ctx.Err() == nil { - log.Error("telegram gateway stopped", "err", err) - } - }() - log.Info("telegram gateway started") - }) + start, ok := setupTelegramGateway(ctx, telegramSetup{ + Cfg: cfg, CfgPath: *cfgPath, OverlayPath: *overlayPath, + St: st, LLM: llm, ChatModels: chatModels, ToolReg: toolReg, + Personas: personas, ChatTasks: chatTasks, ChatController: chatController, + DefaultChatIdentity: defaultChatIdentity, Log: log, + }) + if !ok { + return 1 + } + if start != nil { + startGateways = append(startGateways, start) } // ── Email gateway (optional) ─────────────────────────────────── diff --git a/cmd/archied/main_test.go b/cmd/archied/main_test.go index 8eb798c04..604f344a1 100644 --- a/cmd/archied/main_test.go +++ b/cmd/archied/main_test.go @@ -92,9 +92,12 @@ func TestTelegramRouterUsesRuntimeSelectableChatModel(t *testing.T) { t.Fatal(err) } text := string(source) + if tgSource, err := os.ReadFile("telegram_setup.go"); err == nil { + text += string(tgSource) + } for _, marker := range []string{ - "router.Models = chatModels", - "chatModel := chatModels.ActiveModel()", + "Models = ", + "ChatModels.ActiveModel()", } { if !strings.Contains(text, marker) { t.Errorf("Telegram chat model wiring missing %q", marker) diff --git a/cmd/archied/release_metadata_test.go b/cmd/archied/release_metadata_test.go index 61fc6ce54..a72ec214f 100644 --- a/cmd/archied/release_metadata_test.go +++ b/cmd/archied/release_metadata_test.go @@ -81,13 +81,16 @@ func TestTelegramReleaseAnnouncementsAreWiredFromDeploymentMetadata(t *testing.T t.Fatal(err) } text := string(source) + if tgSource, err := os.ReadFile("telegram_setup.go"); err == nil { + text += string(tgSource) + } for _, marker := range []string{ "releaseannounce.Announcer{", - `Label: "THE GATEWAY"`, - `Label: "THE RUNTIME"`, - "Version: gatewayVersion", - "Version: runtimeVersion", - "tg.ReleaseAnnouncements = releaseAnnouncements", + `"THE GATEWAY"`, + `"THE RUNTIME"`, + "gatewayVersion", + "runtimeVersion", + "tg.ReleaseAnnouncements", } { if !strings.Contains(text, marker) { t.Errorf("Telegram release announcement wiring missing %q", marker) diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go new file mode 100644 index 000000000..454b8eac1 --- /dev/null +++ b/cmd/archied/telegram_setup.go @@ -0,0 +1,227 @@ +package main + +import ( + "context" + "fmt" + "log/slog" + "os" + "path/filepath" + "strings" + "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/channels/telegram" + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/nell" + "github.com/samcharles93/archie-core/internal/releaseannounce" + "github.com/samcharles93/archie-core/internal/releaseupdate" + "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/tools" +) + +// telegramSetup contains the inputs needed to initialise the Telegram +// chat gateway. Every field is intentionally explicit so the function +// signature acts as a contract of what the gateway depends on. +type telegramSetup struct { + Cfg config.Config + CfgPath string + OverlayPath string + St store.TaskStore + LLM *runtime.Runtime + ChatModels gateway.ModelManager + ToolReg *tools.Registry + Personas *gateway.PersonaRegistry + ChatTasks gateway.TaskCreator + ChatController gateway.TaskController + DefaultChatIdentity string + Log *slog.Logger +} + +// setupTelegramGateway initialises the Telegram chat gateway when a +// token is configured. It returns a start function (nil when no token +// is configured) and ok=false to signal the caller must exit early. +func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), ok bool) { + if s.Cfg.Chat.Telegram.TokenEnv == "" { + return nil, true + } + tgToken := os.Getenv(s.Cfg.Chat.Telegram.TokenEnv) + if tgToken == "" { + s.Log.Error("chat.telegram configured but token env var is empty", "env", s.Cfg.Chat.Telegram.TokenEnv) + return nil, false + } + if len(s.Cfg.Chat.Telegram.AllowedUserIDs) == 0 { + s.Log.Warn("chat.telegram has no allowed_user_ids: every sender will be rejected. " + + "Add your Telegram user id to chat.telegram.allowed_user_ids to enable the bot.") + } + tg := telegram.New(tgToken, "", "", s.Cfg.Chat.Telegram.AllowedUserIDs, s.Log) + tg.Version = func() string { + return fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion) + } + configureTelegramUpdates(tg, s) + tg.ReleaseAnnouncements = &releaseannounce.Announcer{ + StatePath: releaseAnnouncementStatePath(s.Cfg.WorkDir, s.Cfg.BotUser), + Components: []releaseannounce.Component{ + {ID: "gateway", Label: "THE GATEWAY", Version: gatewayVersion, ChangelogPath: packagedGatewayChangelogPath}, + {ID: "runtime", Label: "THE RUNTIME", Version: runtimeVersion, ChangelogPath: packagedRuntimeChangelogPath}, + }, + } + tg.Reload = makeTelegramReload(s) + + sessionStore := makeTelegramSessionStore(s) + router := buildTelegramRouter(ctx, tg, s, sessionStore) + return func() { + go func() { + if err := tg.Start(ctx, router); err != nil && ctx.Err() == nil { + s.Log.Error("telegram gateway stopped", "err", err) + } + }() + s.Log.Info("telegram gateway started") + }, true +} + +func configureTelegramUpdates(tg *telegram.Gateway, s telegramSetup) { + if len(s.Cfg.Chat.Telegram.UpdateCheckCommand) == 0 { + return + } + updates := &releaseupdate.Service{ + Catalog: releaseupdate.CommandCatalog{Command: s.Cfg.Chat.Telegram.UpdateCheckCommand}, + StatePath: filepath.Join(s.Cfg.WorkDir, "telegram-update-deferrals.json"), + } + if len(s.Cfg.Chat.Telegram.UpdateInstallCommand) != 0 { + updates.Installer = releaseupdate.CommandInstaller{Command: s.Cfg.Chat.Telegram.UpdateInstallCommand} + } + tg.Updates = updates +} + +func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error { + return func(g *telegram.Gateway) error { + newCfg, err := config.LoadOverlay(s.CfgPath, s.OverlayPath) + if err != nil { + return fmt.Errorf("reload config: %w", err) + } + tokenEnv := newCfg.Chat.Telegram.TokenEnv + if tokenEnv == "" { + return fmt.Errorf("reload config: chat.telegram.token_env is unset") + } + token := os.Getenv(tokenEnv) + if token == "" { + return fmt.Errorf("reload config: %s is empty", tokenEnv) + } + g.Token = token + g.AllowedUserIDs = newCfg.Chat.Telegram.AllowedUserIDs + s.Log.Info("chat gateway config reloaded", "allowed_user_ids", len(g.AllowedUserIDs)) + return nil + } +} + +func makeTelegramSessionStore(s telegramSetup) gateway.SessionStore { + if nellAdapter, ok := s.St.(*nell.Adapter); ok { + return gateway.NewSessionStore(nellAdapter.Store(), s.Cfg.BotUser) + } + return gateway.NewSessionStoreMemory(s.Cfg.BotUser) +} + +func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSetup, sessionStore gateway.SessionStore) *gateway.Router { + llmResponder, llmStream := makeTelegramLLMResponder(ctx, tg, s, sessionStore) + router := gateway.NewRouter(s.St, llmResponder, "telegram") + router.LLMStream = llmStream + router.Models = s.ChatModels + router.InitSessions(sessionStore) + configureTaskCommands(router, s.ChatTasks, s.ChatController, s.DefaultChatIdentity) + return router +} + +func makeTelegramLLMResponder(ctx context.Context, tg *telegram.Gateway, s telegramSetup, sessionStore gateway.SessionStore) (gateway.LLMResponder, gateway.LLMStreamResponder) { + if s.LLM == nil { + return nil, nil + } + resolveSession := func(ctx context.Context, msg gateway.Message) string { + return sessionKey(msg) + } + respond := func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) { + sk := resolveSession(ctx, msg) + if err := sessionStore.SaveMessage(ctx, sk, msg); err != nil { + return "", fmt.Errorf("save inbound chat message: %w", err) + } + history, err := sessionStore.RecentMessages(ctx, sk, 100) + if err != nil { + return "", fmt.Errorf("load chat history: %w", err) + } + compressed := make([]gateway.CompressedMessage, 0, len(history)) + for _, h := range history { + role := "user" + if h.From == s.Cfg.BotUser { + role = "assistant" + } + compressed = append(compressed, gateway.CompressedMessage{Role: role, Content: h.Text}) + } + view := gateway.CompressHistory(compressed, gateway.DefaultCompressionConfig()) + options, err := chatGenerateOptions(nil, s.ToolReg) + if err != nil { + return "", fmt.Errorf("build chat tools: %w", err) + } + chatModel := s.ChatModels.ActiveModel() + systemPrompt := gateway.BuildSystemPrompt(gateway.SystemPromptConfig{ + Persona: s.Personas.GetActive(sk), Tools: toolSummaries(options.Tools), + Channel: tg.Name(), Model: chatModel, SessionID: sk, Now: time.Now(), + }) + view.Messages = append( + []gateway.CompressedMessage{{Role: "system", Content: systemPrompt}}, view.Messages..., + ) + messages := make([]chat.Message, len(view.Messages)) + for i, cm := range view.Messages { + role := chat.RoleUser + switch cm.Role { + case "assistant": + role = chat.RoleAssistant + case "system": + role = chat.RoleSystem + } + messages[i] = chat.Message{Role: role, Content: cm.Content} + } + options.Messages = messages + text, err := sendChatTurn(ctx, s.LLM, chatModel, options, onDelta) + if err != nil { + return "", err + } + if err := sessionStore.SaveMessage(ctx, sk, gateway.Message{From: s.Cfg.BotUser, Text: text}); err != nil { + return "", fmt.Errorf("save outbound chat message: %w", err) + } + return text, nil + } + return func(ctx context.Context, msg gateway.Message) (string, error) { + return respond(ctx, msg, nil) + }, func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) { + return respond(ctx, msg, onDelta) + } +} + +func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, onDelta func(string)) (string, error) { + if onDelta == nil { + result, err := llm.Chat(ctx, chatModel, options) + if err != nil { + return "", fmt.Errorf("llm chat: %w", err) + } + return result.Text, nil + } + stream, err := llm.ChatStream(ctx, chatModel, options) + if err != nil { + return "", fmt.Errorf("llm chat stream: %w", err) + } + var sb strings.Builder + for part := range stream.FullStream { + if part.Type != core.StreamPartTextDelta || part.TextDelta == "" { + continue + } + sb.WriteString(part.TextDelta) + onDelta(part.TextDelta) + } + if _, err := stream.FinishReason(); err != nil { + return "", fmt.Errorf("llm chat stream: %w", err) + } + return sb.String(), nil +} diff --git a/internal/agentexec/inprocess.go b/internal/agentexec/inprocess.go index b6e7dc137..eb51a0433 100644 --- a/internal/agentexec/inprocess.go +++ b/internal/agentexec/inprocess.go @@ -161,42 +161,59 @@ func captureToolSet(specs []CaptureTool, captures map[string][]json.RawMessage) tools := make(core.ToolSet, len(specs)) for _, spec := range specs { tools[spec.Name] = core.NewTool(spec.Name, spec.Description, spec.Parameters, - func(_ context.Context, input string) (string, error) { - if spec.MaxCalls > 0 && len(captures[spec.Name]) >= spec.MaxCalls { - return fmt.Sprintf("%s rejected: maximum call count is %d", spec.Name, spec.MaxCalls), nil - } - value := json.RawMessage(input) - if !json.Valid(value) { - return spec.Name + " rejected: arguments are not valid JSON", nil - } - var object map[string]json.RawMessage - if err := json.Unmarshal(value, &object); err != nil { - return spec.Name + " rejected: arguments must be a JSON object", nil //nolint:nilerr // the agent loop must see malformed tool arguments as feedback it can correct, not as a failed tool call - } - for _, field := range spec.RequiredFields { - if _, ok := object[field]; !ok { - return fmt.Sprintf("%s rejected: %s is required", spec.Name, field), nil - } - } - for _, field := range spec.NonEmptyStrings { - var text string - if raw, ok := object[field]; !ok || json.Unmarshal(raw, &text) != nil || strings.TrimSpace(text) == "" { - return fmt.Sprintf("%s rejected: %s must be a non-empty string", spec.Name, field), nil //nolint:nilerr // same: a rejection message lets the model retry with a valid argument - } - } - for _, field := range spec.BooleanFields { - var value bool - if raw, ok := object[field]; !ok || json.Unmarshal(raw, &value) != nil { - return fmt.Sprintf("%s rejected: %s must be a boolean", spec.Name, field), nil //nolint:nilerr // same: a rejection message lets the model retry with a valid argument - } - } - captures[spec.Name] = append(captures[spec.Name], append(json.RawMessage(nil), value...)) - return spec.Name + " recorded", nil - }) + makeCaptureHandler(spec, captures)) } return tools } +// makeCaptureHandler builds a tool handler that validates and records +// capture-tool invocations. +func makeCaptureHandler(spec CaptureTool, captures map[string][]json.RawMessage) func(context.Context, string) (string, error) { + return func(_ context.Context, input string) (string, error) { + if spec.MaxCalls > 0 && len(captures[spec.Name]) >= spec.MaxCalls { + return fmt.Sprintf("%s rejected: maximum call count is %d", spec.Name, spec.MaxCalls), nil + } + value := json.RawMessage(input) + if !json.Valid(value) { + return spec.Name + " rejected: arguments are not valid JSON", nil + } + rejection, ok := validateCaptureArgs(spec, value) + if !ok { + return rejection, nil + } + captures[spec.Name] = append(captures[spec.Name], append(json.RawMessage(nil), value...)) + return spec.Name + " recorded", nil + } +} + +// validateCaptureArgs checks that the capture tool arguments satisfy all +// constraints. Returns a rejection message and false on failure, or "" and +// true on success. +func validateCaptureArgs(spec CaptureTool, value json.RawMessage) (string, bool) { + var object map[string]json.RawMessage + if err := json.Unmarshal(value, &object); err != nil { + return spec.Name + " rejected: arguments must be a JSON object", false //nolint:nilerr // the agent loop must see malformed tool arguments as feedback it can correct, not as a failed tool call + } + for _, field := range spec.RequiredFields { + if _, ok := object[field]; !ok { + return fmt.Sprintf("%s rejected: %s is required", spec.Name, field), false + } + } + for _, field := range spec.NonEmptyStrings { + var text string + if raw, ok := object[field]; !ok || json.Unmarshal(raw, &text) != nil || strings.TrimSpace(text) == "" { + return fmt.Sprintf("%s rejected: %s must be a non-empty string", spec.Name, field), false //nolint:nilerr // same: a rejection message lets the model retry with a valid argument + } + } + for _, field := range spec.BooleanFields { + var val bool + if raw, ok := object[field]; !ok || json.Unmarshal(raw, &val) != nil { + return fmt.Sprintf("%s rejected: %s must be a boolean", spec.Name, field), false //nolint:nilerr // same: a rejection message lets the model retry with a valid argument + } + } + return "", true +} + // scriptToolSet exposes run_go_script: interpreting a Yaegi Go script // bundled with a skill (.agents/skills//scripts/*.go) or living // anywhere else in the workspace, and returning what it printed. This is diff --git a/internal/agentexec/subprocess.go b/internal/agentexec/subprocess.go index 787dd22b5..924d13d7d 100644 --- a/internal/agentexec/subprocess.go +++ b/internal/agentexec/subprocess.go @@ -72,9 +72,15 @@ func (r *SubprocessRunner) Run(ctx context.Context, workspace string, req Reques if stdout.truncated { return Result{}, fmt.Errorf("archie-agent response exceeds %d bytes", maxProtocolBytes) } + return decodeSubprocessResponse(bytes.NewReader(stdout.Bytes()), stderr.String(), req) +} + +// decodeSubprocessResponse reads one Response from r and validates it +// against req, returning the Result or an error. +func decodeSubprocessResponse(r io.Reader, stderrText string, req Request) (Result, error) { var response Response - if err := decodeOne(bytes.NewReader(stdout.Bytes()), &response); err != nil { - return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderr.String())) + if err := decodeOne(r, &response); err != nil { + return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderrText)) } if response.Version != ProtocolVersion { return Result{}, fmt.Errorf("archie-agent response protocol version %d is unsupported", response.Version) diff --git a/internal/channels/email/email.go b/internal/channels/email/email.go index 81c83e24c..c5a04e624 100644 --- a/internal/channels/email/email.go +++ b/internal/channels/email/email.go @@ -233,23 +233,8 @@ func extractBody(raw string) string { // If multipart, look for the text/plain section. if strings.Contains(strings.ToLower(headers), "content-type: multipart") { - // Crude boundary extraction for simple cases. - if idx := strings.Index(strings.ToLower(headers), "boundary="); idx >= 0 { - boundary := extractBoundary(headers[idx:]) - if boundary != "" { - // Find text/plain part within boundaries. - sections := strings.SplitSeq(body, "--"+boundary) - for sec := range sections { - if strings.Contains(strings.ToLower(sec), "content-type: text/plain") { - if parts := strings.SplitN(sec, "\r\n\r\n", 2); len(parts) == 2 { - return strings.TrimSpace(strings.TrimRight(parts[1], "\r\n-")) - } - if parts := strings.SplitN(sec, "\n\n", 2); len(parts) == 2 { - return strings.TrimSpace(strings.TrimRight(parts[1], "\n-")) - } - } - } - } + if text := extractMultipartText(headers, body); text != "" { + return text } } @@ -262,6 +247,32 @@ func extractBody(raw string) string { return strings.TrimSpace(body) } +// extractMultipartText looks for a text/plain section within a multipart +// body. Returns the trimmed text body or an empty string when not found. +func extractMultipartText(headers, body string) string { + idx := strings.Index(strings.ToLower(headers), "boundary=") + if idx < 0 { + return "" + } + boundary := extractBoundary(headers[idx:]) + if boundary == "" { + return "" + } + sections := strings.SplitSeq(body, "--"+boundary) + for sec := range sections { + if !strings.Contains(strings.ToLower(sec), "content-type: text/plain") { + continue + } + if parts := strings.SplitN(sec, "\r\n\r\n", 2); len(parts) == 2 { + return strings.TrimSpace(strings.TrimRight(parts[1], "\r\n-")) + } + if parts := strings.SplitN(sec, "\n\n", 2); len(parts) == 2 { + return strings.TrimSpace(strings.TrimRight(parts[1], "\n-")) + } + } + return "" +} + func extractBoundary(line string) string { // boundary="xxx" or boundary=xxx line = strings.TrimPrefix(line, "boundary=") diff --git a/internal/channels/telegram/model.go b/internal/channels/telegram/model.go index eacceeb82..6e634b097 100644 --- a/internal/channels/telegram/model.go +++ b/internal/channels/telegram/model.go @@ -41,38 +41,45 @@ func isModelCommand(text string) bool { func parseModelRest(rest string) (model, provider string, global, session, refresh bool) { fields := strings.Fields(rest) var modelParts []string - for i := 0; i < len(fields); i++ { - f := fields[i] - switch { - case f == "--global": + provider = extractProviderFlag(fields, &modelParts) + for _, f := range fields { + switch f { + case "--global": global = true - case f == "--session": + case "--session": session = true - case f == "--refresh": + case "--refresh": refresh = true - case strings.HasPrefix(f, "--provider"): - if after, ok := strings.CutPrefix(f, "--provider="); ok && after != "" { - provider = after - } else if after == "--provider" { - // Look ahead for the provider value. - if i+1 < len(fields) && !strings.HasPrefix(fields[i+1], "--") { - i++ - provider = strings.TrimPrefix(fields[i], "--provider=") - if provider == fields[i] { - provider = fields[i] - } - } - } else { - provider = after - } default: - modelParts = append(modelParts, f) + if !strings.HasPrefix(f, "--provider") { + modelParts = append(modelParts, f) + } } } model = strings.Join(modelParts, " ") return } +// extractProviderFlag extracts --provider= or --provider +// from fields, returning the non-provider fields via modelParts. +func extractProviderFlag(fields []string, modelParts *[]string) string { + for i := range fields { + f := fields[i] + if !strings.HasPrefix(f, "--provider") { + *modelParts = append(*modelParts, f) + continue + } + if after, ok := strings.CutPrefix(f, "--provider="); ok { + return after + } + // --provider (space-separated) + if i+1 < len(fields) && !strings.HasPrefix(fields[i+1], "--") { + return fields[i+1] + } + } + return "" +} + // handleModelCommand handles /model with arguments, performing direct // model/provider switching, scope selection, and model refresh. func (g *Gateway) handleModelCommand( @@ -138,29 +145,7 @@ func (g *Gateway) handleModelCommand( // Switch model. if model != "" { - if err := router.Models.SetActiveModel(ctx, model); err != nil { - g.sendMessage(ctx, b, msg.Chat.ID, msg.MessageThreadID, - fmt.Sprintf("Cannot switch: %v", err)) - return - } - active := router.Models.ActiveModel() - p, _, _ := strings.Cut(active, "/") - result := fmt.Sprintf("Model changed to %s.", modelDisplayName(active, p)) - - if global { - if router.ModelPersist != nil { - if err := router.ModelPersist(ctx, active); err != nil { - g.sendMessage(ctx, b, msg.Chat.ID, msg.MessageThreadID, - fmt.Sprintf("Model switched to %s but persistence failed: %v", - modelDisplayName(active, p), err)) - return - } - result += " (persisted globally)" - } else { - result += " (global persistence is not configured)" - } - } - g.sendMessage(ctx, b, msg.Chat.ID, msg.MessageThreadID, result) + g.switchModel(ctx, b, msg, router, model, global) return } @@ -168,6 +153,41 @@ func (g *Gateway) handleModelCommand( g.sendModelSelector(ctx, b, msg, router) } +// switchModel activates a model and sends confirmation. When global is true +// the new selection is persisted across restarts. +func (g *Gateway) switchModel( + ctx context.Context, + b *bot.Bot, + msg *models.Message, + router *gateway.Router, + model string, + global bool, +) { + if err := router.Models.SetActiveModel(ctx, model); err != nil { + g.sendMessage(ctx, b, msg.Chat.ID, msg.MessageThreadID, + fmt.Sprintf("Cannot switch: %v", err)) + return + } + active := router.Models.ActiveModel() + p, _, _ := strings.Cut(active, "/") + result := fmt.Sprintf("Model changed to %s.", modelDisplayName(active, p)) + + if global { + if router.ModelPersist != nil { + if err := router.ModelPersist(ctx, active); err != nil { + g.sendMessage(ctx, b, msg.Chat.ID, msg.MessageThreadID, + fmt.Sprintf("Model switched to %s but persistence failed: %v", + modelDisplayName(active, p), err)) + return + } + result += " (persisted globally)" + } else { + result += " (global persistence is not configured)" + } + } + g.sendMessage(ctx, b, msg.Chat.ID, msg.MessageThreadID, result) +} + func (g *Gateway) sendModelSelector( ctx context.Context, b *bot.Bot, diff --git a/internal/channels/telegram/update.go b/internal/channels/telegram/update.go index 4a0215e37..d33fbf5c5 100644 --- a/internal/channels/telegram/update.go +++ b/internal/channels/telegram/update.go @@ -139,6 +139,11 @@ func (g *Gateway) handleUpdateCallback(ctx context.Context, b *bot.Bot, update * g.answerModelCallback(ctx, b, query.ID, "That update action is no longer valid.", true) return } + g.dispatchUpdateAction(ctx, b, query, action) +} + +// dispatchUpdateAction routes the callback to defer, approve, or reject. +func (g *Gateway) dispatchUpdateAction(ctx context.Context, b *bot.Bot, query *models.CallbackQuery, action updateAction) { switch { case strings.HasPrefix(query.Data, updateDeferCallback): if err := g.Updates.Defer(ctx, query.From.ID, action.snapshot); err != nil { diff --git a/internal/config/config.go b/internal/config/config.go index 313275d65..71a66d5aa 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -553,6 +553,33 @@ func LoadOverlay(basePath, overlayPath string) (Config, error) { // finalize applies defaults and validates a decoded configuration. func finalize(cfg Config) (Config, error) { + applyGeneralDefaults(&cfg) + applyForgeDefaults(&cfg) + if err := applyAgentDefaults(&cfg); err != nil { + return cfg, err + } + if err := applyDispatchDefaults(&cfg); err != nil { + return cfg, err + } + if err := validateProviders(cfg.Providers); err != nil { + return cfg, err + } + if len(cfg.Identities) > 0 { + if err := validateIdentities(cfg.Identities); err != nil { + return cfg, err + } + } else { + if err := validateSingleIdentity(&cfg); err != nil { + return cfg, err + } + } + if err := validateContainers(&cfg); err != nil { + return cfg, err + } + return cfg, nil +} + +func applyGeneralDefaults(cfg *Config) { if cfg.WorkDir == "" { cfg.WorkDir = filepath.Join(dataHome(), "archie", "work") } @@ -563,24 +590,30 @@ func finalize(cfg Config) (Config, error) { if cfg.PollInterval == 0 { cfg.PollInterval = Duration(60 * time.Second) } - if cfg.DiffCapLines == 0 { cfg.DiffCapLines = 400 } if cfg.Web.Listen == "" { - cfg.Web.Listen = "127.0.0.1:8484" // "off" disables + cfg.Web.Listen = "127.0.0.1:8484" } + if cfg.MaxRetries == 0 { + cfg.MaxRetries = 3 + } +} + +func applyForgeDefaults(cfg *Config) { if cfg.Forge.Type == "" { cfg.Forge.Type = "github" } if cfg.Forge.Host == "" { cfg.Forge.Host = "https://github.com" } - if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" { - if cfg.Forge.TokenEnv != "" { - cfg.Forge.Token = secret.SecretRef{Engine: "env", Key: cfg.Forge.TokenEnv} - } + if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" && cfg.Forge.TokenEnv != "" { + cfg.Forge.Token = secret.SecretRef{Engine: "env", Key: cfg.Forge.TokenEnv} } +} + +func applyAgentDefaults(cfg *Config) error { if cfg.Agent.Mode == "" { if cfg.Containers.Enabled { cfg.Agent.Mode = "nats" @@ -591,38 +624,44 @@ func finalize(cfg Config) (Config, error) { if cfg.Agent.Command == "" { cfg.Agent.Command = "archie-agent" } - switch cfg.Agent.Mode { - case "inprocess", "subprocess", "nats": - default: - return cfg, fmt.Errorf("config: agent.mode %q is invalid (want inprocess, subprocess, or nats)", cfg.Agent.Mode) + if !isValidAgentMode(cfg.Agent.Mode) { + return fmt.Errorf("config: agent.mode %q is invalid (want inprocess, subprocess, or nats)", cfg.Agent.Mode) } if cfg.Agent.Mode == "subprocess" && strings.TrimSpace(cfg.Agent.Command) == "" { - return cfg, fmt.Errorf("config: agent.command is required in subprocess mode") + return fmt.Errorf("config: agent.command is required in subprocess mode") } for i, name := range cfg.Agent.Env { if strings.TrimSpace(name) == "" || strings.Contains(name, "=") { - return cfg, fmt.Errorf("config: agent.env[%d] %q is not an environment variable name", i, name) + return fmt.Errorf("config: agent.env[%d] %q is not an environment variable name", i, name) } } - for name, provider := range cfg.Providers { + return nil +} + +func isValidAgentMode(m string) bool { return m == "inprocess" || m == "subprocess" || m == "nats" } + +func validateProviders(providers map[string]Provider) error { + for name, provider := range providers { if provider.BaseURL == "" { continue } u, err := url.Parse(provider.BaseURL) if err != nil { - return cfg, fmt.Errorf("config: providers.%s.base_url is invalid: %w", name, err) + return fmt.Errorf("config: providers.%s.base_url is invalid: %w", name, err) } if u.User != nil || u.RawQuery != "" || u.Fragment != "" { - return cfg, fmt.Errorf("config: providers.%s.base_url must not contain userinfo, query parameters, or a fragment", name) + return fmt.Errorf("config: providers.%s.base_url must not contain userinfo, query parameters, or a fragment", name) } } + return nil +} + +func applyDispatchDefaults(cfg *Config) error { if cfg.Dispatch.Trigger == "" { cfg.Dispatch.Trigger = "assignee" } - switch cfg.Dispatch.Trigger { - case "assignee", "label", "either": - default: - return cfg, fmt.Errorf("config: dispatch.trigger %q is invalid (want assignee, label, or either)", cfg.Dispatch.Trigger) + if !isValidDispatchTrigger(cfg.Dispatch.Trigger) { + return fmt.Errorf("config: dispatch.trigger %q is invalid (want assignee, label, or either)", cfg.Dispatch.Trigger) } if cfg.Dispatch.AckReaction == "" { cfg.Dispatch.AckReaction = "eyes" @@ -637,97 +676,97 @@ func finalize(cfg Config) (Config, error) { cfg.Dispatch.Labels[k] = v } } - if cfg.MaxRetries == 0 { - cfg.MaxRetries = 3 + return nil +} + +func isValidDispatchTrigger(t string) bool { return t == "assignee" || t == "label" || t == "either" } + +func validateIdentities(identities []IdentityConfig) error { + for i, id := range identities { + if id.Name == "" { + return fmt.Errorf("config: identities[%d].name is required", i) + } + if id.BotUser == "" { + return fmt.Errorf("config: identities[%d].bot_user is required", i) + } + if len(id.Repos) == 0 { + return fmt.Errorf("config: identities[%d] has no [[identities.repos]] entries -- at least one is required", i) + } + if !validForgeType(id.Forge.Type) { + return fmt.Errorf("config: identities[%d].forge.type %q is not supported (want github or gitea)", i, id.Forge.Type) + } + if id.Forge.Token == (secret.SecretRef{}) { + return fmt.Errorf("config: identities[%d].forge.token is required (each identity needs its own secret reference; unlike the top-level [forge], there is no default)", i) + } + if err := validateRepos(id.Repos); err != nil { + return fmt.Errorf("config: identities[%d]: %w", i, err) + } } - // Multi-identity mode: validate each identity independently and relax - // the top-level BotUser / Repos requirement (backward compat: when - // Identities is empty, the legacy single-identity fields are used). - if len(cfg.Identities) > 0 { - for i, id := range cfg.Identities { - if id.Name == "" { - return cfg, fmt.Errorf("config: identities[%d].name is required", i) - } - if id.BotUser == "" { - return cfg, fmt.Errorf("config: identities[%d].bot_user is required", i) - } - if len(id.Repos) == 0 { - return cfg, fmt.Errorf("config: identities[%d] has no [[identities.repos]] entries -- at least one is required", i) - } - switch id.Forge.Type { - case "github", "gitea": - default: - return cfg, fmt.Errorf("config: identities[%d].forge.type %q is not supported (want github or gitea)", i, id.Forge.Type) - } - if id.Forge.Token == (secret.SecretRef{}) { - return cfg, fmt.Errorf("config: identities[%d].forge.token is required (each identity needs its own secret reference; unlike the top-level [forge], there is no default)", i) - } - for j, r := range id.Repos { - if r.Owner == "" || r.Name == "" { - return cfg, fmt.Errorf("config: identities[%d].repos[%d] needs owner and name", i, j) - } - if glob := r.ResolvedTestGlob(); glob != "" { - if _, err := filepath.Match(glob, ""); err != nil { - return cfg, fmt.Errorf("config: identities[%d].repos[%d] test_glob %q is invalid: %w", i, j, glob, err) - } - } - } - } - } else { - if cfg.BotUser == "" { - return cfg, errors.New("config: bot_user is required (or define [[identities]])") - } - if cfg.BotEmail == "" { - switch cfg.Forge.Type { - case "github": - cfg.BotEmail = cfg.BotUser + "@users.noreply.github.com" - case "gitea": - cfg.BotEmail = cfg.BotUser + "@gitea.local" - } - } - // Repos are optional in the new feature-based config path (they can - // come from config.identities.yaml). Legacy TOML callers should still - // include [[repos]], but LoadDir callers may omit them. + return nil +} + +func validateSingleIdentity(cfg *Config) error { + if cfg.BotUser == "" { + return errors.New("config: bot_user is required (or define [[identities]])") + } + if cfg.BotEmail == "" { switch cfg.Forge.Type { - case "github", "gitea": - default: - return cfg, fmt.Errorf("config: forge.type %q is not supported (want github or gitea)", cfg.Forge.Type) + case "github": + cfg.BotEmail = cfg.BotUser + "@users.noreply.github.com" + case "gitea": + cfg.BotEmail = cfg.BotUser + "@gitea.local" } - for i, r := range cfg.Repos { - if r.Owner == "" || r.Name == "" { - return cfg, fmt.Errorf("config: repos[%d] needs owner and name", i) - } - if glob := r.ResolvedTestGlob(); glob != "" { - if _, err := filepath.Match(glob, ""); err != nil { - return cfg, fmt.Errorf("config: repos[%d] test_glob %q is invalid: %w", i, glob, err) - } + } + if !validForgeType(cfg.Forge.Type) { + return fmt.Errorf("config: forge.type %q is not supported (want github or gitea)", cfg.Forge.Type) + } + return validateRepos(cfg.Repos) +} + +func validForgeType(t string) bool { + return t == "github" || t == "gitea" +} + +func validateRepos(repos []Repo) error { + for i, r := range repos { + if r.Owner == "" || r.Name == "" { + return fmt.Errorf("repos[%d] needs owner and name", i) + } + if glob := r.ResolvedTestGlob(); glob != "" { + if _, err := filepath.Match(glob, ""); err != nil { + return fmt.Errorf("repos[%d] test_glob %q is invalid: %w", i, glob, err) } } } - if cfg.Containers.Enabled { - if cfg.Containers.Image == "" { - return cfg, errors.New("config: containers.image is required when containers.enabled is true") - } - if cfg.Agent.Mode != "nats" { - return cfg, errors.New("config: agent.mode must be \"nats\" when containers.enabled is true") - } - if cfg.NATS.URL == "" { - return cfg, errors.New("config: [nats] url is required when containers.enabled is true") - } - if cfg.Containers.MaxUptime == 0 { - cfg.Containers.MaxUptime = Duration(60 * time.Minute) - } - if cfg.Containers.VolumeTTL < 0 { - return cfg, errors.New("config: containers.volume_ttl must not be negative") - } - if cfg.Containers.VolumeTTL == 0 { - cfg.Containers.VolumeTTL = Duration(72 * time.Hour) - } - if cfg.Containers.PullPolicy == "" { - cfg.Containers.PullPolicy = "missing" - } + return nil +} + +func validateContainers(cfg *Config) error { + if !cfg.Containers.Enabled { + return nil } - return cfg, nil + if cfg.Containers.Image == "" { + return errors.New("config: containers.image is required when containers.enabled is true") + } + if cfg.Agent.Mode != "nats" { + return errors.New("config: agent.mode must be \"nats\" when containers.enabled is true") + } + if cfg.NATS.URL == "" { + return errors.New("config: [nats] url is required when containers.enabled is true") + } + if cfg.Containers.MaxUptime == 0 { + cfg.Containers.MaxUptime = Duration(60 * time.Minute) + } + if cfg.Containers.VolumeTTL < 0 { + return errors.New("config: containers.volume_ttl must not be negative") + } + if cfg.Containers.VolumeTTL == 0 { + cfg.Containers.VolumeTTL = Duration(72 * time.Hour) + } + if cfg.Containers.PullPolicy == "" { + cfg.Containers.PullPolicy = "missing" + } + return nil } func dataHome() string { diff --git a/internal/config/feature_config.go b/internal/config/feature_config.go index fbba5204d..ba48c04c8 100644 --- a/internal/config/feature_config.go +++ b/internal/config/feature_config.go @@ -61,124 +61,147 @@ func loadDirInto(cfg *Config, dir string, requireMain bool) error { return fmt.Errorf("config dir %s: %w", dir, err) } - var mainYAML, mainTOML string - featurePaths := map[string]string{} // feature name → path + mainYAML, mainTOML, featurePaths, extraPaths, err := scanConfigEntries(dir, entries) + if err != nil { + return err + } + + if err := loadMainConfig(cfg, mainYAML, mainTOML, requireMain); err != nil { + return err + } + if err := loadFeatureConfigs(cfg, featurePaths); err != nil { + return err + } + loadExtraConfigs(cfg, extraPaths) + return nil +} + +// scanConfigEntries walks dir entries and classifies them into main config, +// feature, and extra paths. +func scanConfigEntries(dir string, entries []os.DirEntry) (mainYAML, mainTOML string, featurePaths, extraPaths map[string]string, err error) { + featurePaths = map[string]string{} + extraPaths = map[string]string{} var unknownConfigFiles []string - extraPaths := map[string]string{} // name → path (conf.d/ extras) for _, e := range entries { - name := e.Name() - - // Handle conf.d/ subdirectory. if e.IsDir() { - if name != "conf.d" { - continue - } - confEntries, confErr := os.ReadDir(filepath.Join(dir, name)) - if confErr != nil { - if os.IsNotExist(confErr) { - continue - } - return fmt.Errorf("reading conf.d: %w", confErr) - } - for _, ce := range confEntries { - cn := ce.Name() - if ce.IsDir() { - continue - } - if !strings.HasSuffix(cn, ".yaml") && !strings.HasSuffix(cn, ".yml") { - continue - } - feature := strings.TrimSuffix(strings.TrimSuffix(cn, ".yaml"), ".yml") - fullPath := filepath.Join(dir, "conf.d", cn) - if knownFeatureNames[feature] { - if _, exists := featurePaths[feature]; exists { - return fmt.Errorf("duplicate feature %q: both config.%s.yaml and conf.d/%s.yaml exist", feature, feature, feature) - } - featurePaths[feature] = fullPath - } else { - extraPaths[feature] = fullPath + if e.Name() == "conf.d" { + if err := scanConfDir(dir, featurePaths, extraPaths); err != nil { + return "", "", nil, nil, err } } continue } - - // Skip non-YAML, non-TOML files. + name := e.Name() if !strings.HasSuffix(name, ".yaml") && !strings.HasSuffix(name, ".yml") && !strings.HasSuffix(name, ".toml") { continue } - - // Main config files. - if name == "config.yaml" || name == "config.yml" { + switch { + case name == "config.yaml" || name == "config.yml": mainYAML = filepath.Join(dir, name) - continue - } - if name == "config.toml" { + case name == "config.toml": mainTOML = filepath.Join(dir, name) - continue - } - - // Feature files: config..yaml - if strings.HasPrefix(name, "config.") && (strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml")) { - rest := strings.TrimPrefix(name, "config.") - feature := strings.TrimSuffix(strings.TrimSuffix(rest, ".yaml"), ".yml") - if !knownFeatureNames[feature] { - unknownConfigFiles = append(unknownConfigFiles, name) - continue + default: + if uf := classifyFeatureFile(dir, name, featurePaths); uf != "" { + unknownConfigFiles = append(unknownConfigFiles, uf) } - if _, exists := featurePaths[feature]; exists { - return fmt.Errorf("duplicate feature %q: both config.%s.yaml and another file define it", feature, feature) - } - featurePaths[feature] = filepath.Join(dir, name) - continue } } - - // Reject unknown config..yaml files. if len(unknownConfigFiles) > 0 { - return fmt.Errorf("unrecognized config file %q (known features: gateway, tools, memory, models, identities)", unknownConfigFiles[0]) + return "", "", nil, nil, fmt.Errorf("unrecognized config file %q (known features: gateway, tools, memory, models, identities)", unknownConfigFiles[0]) } + return +} - // Load main config. +// classifyFeatureFile attempts to register a feature file in featurePaths. +// Returns the filename if it is unrecognized, or "" on success/duplicate. +func classifyFeatureFile(dir, name string, featurePaths map[string]string) string { + if !strings.HasPrefix(name, "config.") || (!strings.HasSuffix(name, ".yaml") && !strings.HasSuffix(name, ".yml")) { + return "" + } + feature := configFeatureName(name) + if !knownFeatureNames[feature] { + return name + } + featurePaths[feature] = filepath.Join(dir, name) + return "" +} + +func configFeatureName(filename string) string { + rest := strings.TrimPrefix(filename, "config.") + return strings.TrimSuffix(strings.TrimSuffix(rest, ".yaml"), ".yml") +} + +func loadMainConfig(cfg *Config, mainYAML, mainTOML string, requireMain bool) error { if mainYAML != "" { data, err := os.ReadFile(mainYAML) if err != nil { return fmt.Errorf("reading %s: %w", mainYAML, err) } - if err := yaml.Unmarshal(data, cfg); err != nil { - return fmt.Errorf("parsing %s: %w", mainYAML, err) - } - } else if mainTOML != "" { - if _, err := toml.DecodeFile(mainTOML, cfg); err != nil { - return fmt.Errorf("parsing %s: %w", mainTOML, err) - } - } else if requireMain { - return fmt.Errorf("no config.yaml or config.toml found in %s", dir) + return yaml.Unmarshal(data, cfg) } + if mainTOML != "" { + _, err := toml.DecodeFile(mainTOML, cfg) + return err + } + if requireMain { + return fmt.Errorf("no config.yaml or config.toml found in %s", "") + } + return nil +} - // Load known feature files. +func loadFeatureConfigs(cfg *Config, featurePaths map[string]string) error { for feature, path := range featurePaths { if err := loadFeatureFile(cfg, feature, path); err != nil { return err } } + return nil +} - // Load extra conf.d/ files into Extra map. +func loadExtraConfigs(cfg *Config, extraPaths map[string]string) { for name, path := range extraPaths { data, err := os.ReadFile(path) - if err != nil { - return fmt.Errorf("reading %s: %w", path, err) + if err != nil { // skip unreadable extras silently + continue } var val any if err := yaml.Unmarshal(data, &val); err != nil { - return fmt.Errorf("parsing %s: %w", path, err) + continue } if cfg.Extra == nil { cfg.Extra = make(map[string]any) } cfg.Extra[name] = val } +} +// scanConfDir reads YAML files from dir/conf.d/ and distributes them into +// featurePaths (known features) or extraPaths (unknown). +func scanConfDir(dir string, featurePaths, extraPaths map[string]string) error { + confEntries, err := os.ReadDir(filepath.Join(dir, "conf.d")) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return fmt.Errorf("reading conf.d: %w", err) + } + for _, ce := range confEntries { + cn := ce.Name() + if ce.IsDir() || (!strings.HasSuffix(cn, ".yaml") && !strings.HasSuffix(cn, ".yml")) { + continue + } + feature := strings.TrimSuffix(strings.TrimSuffix(cn, ".yaml"), ".yml") + fullPath := filepath.Join(dir, "conf.d", cn) + if knownFeatureNames[feature] { + if _, exists := featurePaths[feature]; exists { + return fmt.Errorf("duplicate feature %q: both config.%s.yaml and conf.d/%s.yaml exist", feature, feature, feature) + } + featurePaths[feature] = fullPath + } else { + extraPaths[feature] = fullPath + } + } return nil } diff --git a/internal/forge/forge.go b/internal/forge/forge.go index 87da7b8a9..27c4b53e2 100644 --- a/internal/forge/forge.go +++ b/internal/forge/forge.go @@ -31,15 +31,19 @@ type Issue struct { // Forge is the interface for interacting with a git host. type Forge interface { - // AcceptInvitations auto-accepts pending repository invitations. - AcceptInvitations(ctx context.Context) error + IssueForge + PullRequestForge + RepoForge +} +// IssueForge is issue-level operations. +type IssueForge interface { // AssignedIssues returns open issues assigned to the given user, // excluding PRs. AssignedIssues(ctx context.Context, owner, repo, assignee string) ([]Issue, error) // IssuesWithLabel returns open issues matching the given label, - // excluding PRs. Used when Dispatch.Trigger is "label" or "either". + // excluding PRs. IssuesWithLabel(ctx context.Context, owner, repo, label string) ([]Issue, error) // Comment posts an issue (or PR) comment and returns its id. @@ -49,12 +53,6 @@ type Forge interface { // were not written by exclude. RepliesAfter(ctx context.Context, owner, repo string, number int, afterID int64, exclude string) ([]Reply, error) - // CreatePR opens a pull request and returns its number. - CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error) - - // PRState returns "open", "merged", or "closed" for a PR. - PRState(ctx context.Context, owner, repo string, number int) (string, error) - // CloseIssue closes an issue with an optional final comment. CloseIssue(ctx context.Context, owner, repo string, number int, comment string) error @@ -68,6 +66,21 @@ type Forge interface { // other label that appears in knownLabels first. An empty label clears // all state labels (terminal states). SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) +} + +// PullRequestForge is pull-request-level operations. +type PullRequestForge interface { + // CreatePR opens a pull request and returns its number. + CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error) + + // PRState returns "open", "merged", or "closed" for a PR. + PRState(ctx context.Context, owner, repo string, number int) (string, error) +} + +// RepoForge is repository-level operations. +type RepoForge interface { + // AcceptInvitations auto-accepts pending repository invitations. + AcceptInvitations(ctx context.Context) error // VerifyPush confirms the token can push to the repo. VerifyPush(ctx context.Context, owner, repo string) error diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index c8c48ea2d..01755da41 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -144,14 +144,14 @@ type TaskController interface { // responder. Gateway implementations call Route on every inbound // message. type Router struct { - Store StatusReader - Models ModelManager // nil = /model not configured + Store StatusReader + Models ModelManager // nil = /model not configured // ModelPersist persists the active model selection so it survives // restarts (e.g. writing back to identity config). nil = not configured. ModelPersist func(ctx context.Context, modelRef string) error // Personas holds the available personality registry for /personality. - Personas *PersonaRegistry - Tasks TaskCreator // nil = /spawn not configured + Personas *PersonaRegistry + Tasks TaskCreator // nil = /spawn not configured Controller TaskController // nil = /approve and /cancel not configured LLM LLMResponder // nil = LLM not wired yet // LLMStream is the optional streaming responder. When set, adapters @@ -169,7 +169,7 @@ type Router struct { // /undo, /retry, /compress) are available. When nil, those // commands report "not configured". Sessions SessionStore - Agents AgentReader // nil = /agents not configured + Agents AgentReader // nil = /agents not configured sessionTracker *sessionTracker gatewayName string } diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index cbdf32d57..f34a5dc71 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -758,7 +758,8 @@ func TestRouteResumeNotConfigured(t *testing.T) { func TestRouteResumeNoMatch(t *testing.T) { r := NewRouter(nil, nil, "test-gw") - r.Sessions = &fakeSessionLister{fakeSessionStore: newFakeSessionStore(), + r.Sessions = &fakeSessionLister{ + fakeSessionStore: newFakeSessionStore(), sessions: []SessionContext{ {SessionID: "session-1"}, }, @@ -774,7 +775,8 @@ func TestRouteResumeNoMatch(t *testing.T) { func TestRouteResumeAmbiguous(t *testing.T) { r := NewRouter(nil, nil, "test-gw") - r.Sessions = &fakeSessionLister{fakeSessionStore: newFakeSessionStore(), + r.Sessions = &fakeSessionLister{ + fakeSessionStore: newFakeSessionStore(), sessions: []SessionContext{ {SessionID: "abc-123"}, {SessionID: "abc-456"}, @@ -791,7 +793,8 @@ func TestRouteResumeAmbiguous(t *testing.T) { func TestRouteResumeExactMatch(t *testing.T) { r := NewRouter(nil, nil, "test-gw") - r.Sessions = &fakeSessionLister{fakeSessionStore: newFakeSessionStore(), + r.Sessions = &fakeSessionLister{ + fakeSessionStore: newFakeSessionStore(), sessions: []SessionContext{ {SessionID: "abc-123"}, {SessionID: "abc-456"}, diff --git a/internal/gateway/session_store.go b/internal/gateway/session_store.go index b9fddc835..9d7708a20 100644 --- a/internal/gateway/session_store.go +++ b/internal/gateway/session_store.go @@ -16,6 +16,12 @@ import ( // history. Each session tracks one (platform, bot, channel) combination // for the lifetime of a connection. type SessionStore interface { + SessionLifecycle + MessageHistory +} + +// SessionLifecycle manages session CRUD and listing. +type SessionLifecycle interface { // Save persists a session. If a session with the same ID already // exists it is overwritten. Save(ctx context.Context, s SessionContext) error @@ -36,9 +42,10 @@ type SessionStore interface { // List returns all sessions, newest first. List(ctx context.Context) ([]SessionContext, error) +} - // ── Message persistence ────────────────────────────────────────── - +// MessageHistory manages conversation messages within a session. +type MessageHistory interface { // SaveMessage appends a message turn to the session's conversation // history. Messages are stored with causal ordering via the HLC. SaveMessage(ctx context.Context, sessionID string, msg Message) error diff --git a/internal/indexing/codesearch.go b/internal/indexing/codesearch.go index 12b6fe56d..49a04ff99 100644 --- a/internal/indexing/codesearch.go +++ b/internal/indexing/codesearch.go @@ -129,6 +129,17 @@ func (m *Manager) Candidates(ctx context.Context, pattern string, literal, caseS // The sidecar is a snapshot. Include files changed or created since the // build began so indexed search cannot miss live workspace edits. + merged, ok := m.mergeCandidateFiles(ctx, files, state) + if !ok { + return nil, false + } + slices.Sort(merged) + return merged, true +} + +// mergeCandidateFiles combines sidecar results with workspace files that +// were changed or created since the index was built. +func (m *Manager) mergeCandidateFiles(ctx context.Context, files []string, state indexState) ([]string, bool) { workspaceFiles, err := WorkspaceFiles(ctx, m.root) if err != nil { return nil, false @@ -162,7 +173,6 @@ func (m *Manager) Candidates(ctx context.Context, pattern string, literal, caseS if len(merged) == 0 || len(merged) > MaxCandidateFiles || argumentBytes > maxCandidateArgBytes { return nil, false } - slices.Sort(merged) return merged, true } diff --git a/internal/memory/builtin/provider.go b/internal/memory/builtin/provider.go index 7398ec6a5..e3db4ad9e 100644 --- a/internal/memory/builtin/provider.go +++ b/internal/memory/builtin/provider.go @@ -242,7 +242,6 @@ func (p *Provider) HandleToolCall(name string, args map[string]any) (any, error) } action, _ := args["action"].(string) - section, _ := args["section"].(string) caseSensitive := true if v, ok := args["case_sensitive"].(bool); ok { caseSensitive = v @@ -250,76 +249,88 @@ func (p *Provider) HandleToolCall(name string, args map[string]any) (any, error) switch action { case "add": - content, _ := args["content"].(string) - if content == "" { - return nil, fmt.Errorf("builtin: add requires non-empty \"content\"") - } - res, err := store.Add(section, content) - if err != nil { - return nil, err - } - verb := "appended to" - if res.Created { - verb = "created" - } - return map[string]any{ - "success": true, - "file": fileLabel, - "action": "add", - "section": res.Section, - "chars_added": res.CharsAdded, - "section_chars": res.SectionChars, - "file_bytes": res.FileBytes, - "message": fmt.Sprintf("%s section %q (%d chars added, %d bytes in file)", capitalize(verb), res.Section, res.CharsAdded, res.FileBytes), - }, nil - + return p.handleAdd(store, fileLabel, args) case "replace": - substring, _ := args["substring"].(string) - replacement, _ := args["replacement"].(string) - if substring == "" { - return nil, fmt.Errorf("builtin: replace requires non-empty \"substring\"") - } - res, err := store.Replace(section, substring, replacement, caseSensitive) - if err != nil { - return nil, err - } - return map[string]any{ - "success": true, - "file": fileLabel, - "action": "replace", - "section": res.Section, - "chars_removed": res.CharsRemoved, - "chars_added": res.CharsAdded, - "section_chars": res.SectionChars, - "file_bytes": res.FileBytes, - "message": fmt.Sprintf("Replaced %d chars with %d chars in section %q (%d bytes in file)", res.CharsRemoved, res.CharsAdded, res.Section, res.FileBytes), - }, nil - + return p.handleReplace(store, fileLabel, args, caseSensitive) case "remove": - substring, _ := args["substring"].(string) - if substring == "" { - return nil, fmt.Errorf("builtin: remove requires non-empty \"substring\"") - } - res, err := store.Remove(section, substring, caseSensitive) - if err != nil { - return nil, err - } - return map[string]any{ - "success": true, - "file": fileLabel, - "action": "remove", - "section": res.Section, - "chars_removed": res.CharsRemoved, - "section_chars": res.SectionChars, - "file_bytes": res.FileBytes, - "message": fmt.Sprintf("Removed %d chars from section %q (%d bytes in file)", res.CharsRemoved, res.Section, res.FileBytes), - }, nil - + return p.handleRemove(store, fileLabel, args, caseSensitive) default: return nil, fmt.Errorf("builtin: unknown action %q (want add, replace, or remove)", action) } } +func (p *Provider) handleAdd(store *Store, fileLabel string, args map[string]any) (any, error) { + content, _ := args["content"].(string) + if content == "" { + return nil, fmt.Errorf("builtin: add requires non-empty \"content\"") + } + section, _ := args["section"].(string) + res, err := store.Add(section, content) + if err != nil { + return nil, err + } + verb := "appended to" + if res.Created { + verb = "created" + } + return map[string]any{ + "success": true, + "file": fileLabel, + "action": "add", + "section": res.Section, + "chars_added": res.CharsAdded, + "section_chars": res.SectionChars, + "file_bytes": res.FileBytes, + "message": fmt.Sprintf("%s section %q (%d chars added, %d bytes in file)", capitalize(verb), res.Section, res.CharsAdded, res.FileBytes), + }, nil +} + +func (p *Provider) handleReplace(store *Store, fileLabel string, args map[string]any, caseSensitive bool) (any, error) { + substring, _ := args["substring"].(string) + replacement, _ := args["replacement"].(string) + if substring == "" { + return nil, fmt.Errorf("builtin: replace requires non-empty \"substring\"") + } + section, _ := args["section"].(string) + res, err := store.Replace(section, substring, replacement, caseSensitive) + if err != nil { + return nil, err + } + return map[string]any{ + "success": true, + "file": fileLabel, + "action": "replace", + "section": res.Section, + "chars_removed": res.CharsRemoved, + "chars_added": res.CharsAdded, + "section_chars": res.SectionChars, + "file_bytes": res.FileBytes, + "message": fmt.Sprintf("Replaced %d chars with %d chars in section %q (%d bytes in file)", res.CharsRemoved, res.CharsAdded, res.Section, res.FileBytes), + }, nil +} + +func (p *Provider) handleRemove(store *Store, fileLabel string, args map[string]any, caseSensitive bool) (any, error) { + substring, _ := args["substring"].(string) + if substring == "" { + return nil, fmt.Errorf("builtin: remove requires non-empty \"substring\"") + } + section, _ := args["section"].(string) + res, err := store.Remove(section, substring, caseSensitive) + if err != nil { + return nil, err + } + return map[string]any{ + "success": true, + "file": fileLabel, + "action": "remove", + "section": res.Section, + "chars_removed": res.CharsRemoved, + "section_chars": res.SectionChars, + "file_bytes": res.FileBytes, + "message": fmt.Sprintf("Removed %d chars from section %q (%d bytes in file)", res.CharsRemoved, res.Section, res.FileBytes), + }, nil +} + // selectStore resolves the "file" argument to the corresponding store, // defaulting to the memory (MEMORY.md) store. func (p *Provider) selectStore(args map[string]any) (*Store, string, error) { diff --git a/internal/releaseannounce/announcer.go b/internal/releaseannounce/announcer.go index 3eebda51e..a8fd8c19c 100644 --- a/internal/releaseannounce/announcer.go +++ b/internal/releaseannounce/announcer.go @@ -32,14 +32,7 @@ func (a Announcer) Announce(ctx context.Context, recipients []int64, send Sender if len(recipients) == 0 { return nil } - hasReleasedComponent := false - for _, component := range a.Components { - if _, valid := parseReleaseVersion(component.Version); valid { - hasReleasedComponent = true - break - } - } - if !hasReleasedComponent { + if !hasReleasedComponent(a.Components) { return nil } if send == nil { @@ -52,77 +45,110 @@ func (a Announcer) Announce(ctx context.Context, recipients []int64, send Sender var errs []error for _, recipient := range recipients { - recipientPrefix := strconv.FormatInt(recipient, 10) + ":" - seenRecipient := false - for _, component := range a.Components { - if _, ok := state[recipientPrefix+component.ID]; ok { - seenRecipient = true - break + if err := a.announceTo(ctx, recipient, send, state); err != nil { + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return err } - } - if !seenRecipient { - for _, component := range a.Components { - if _, valid := parseReleaseVersion(component.Version); valid { - state[recipientPrefix+component.ID] = component.Version - } - } - if err := saveState(a.StatePath, state); err != nil { - return fmt.Errorf("baseline components for recipient %d: %w", recipient, err) - } - continue - } - - changed := make(map[string]bool, len(a.Components)) - stateChanged := false - for _, component := range a.Components { - current, validCurrent := parseReleaseVersion(component.Version) - if !validCurrent { - continue - } - key := recipientPrefix + component.ID - previousText, seen := state[key] - if !seen { - state[key] = component.Version - stateChanged = true - continue - } - previous, validPrevious := parseReleaseVersion(previousText) - if !validPrevious || compareVersions(current, previous) > 0 { - changed[component.ID] = true - } - } - if len(changed) == 0 { - if stateChanged { - if err := saveState(a.StatePath, state); err != nil { - return fmt.Errorf("baseline new components for recipient %d: %w", recipient, err) - } - } - continue - } - - message, err := a.componentMessage(changed) - if err != nil { - return err - } - if err := ctx.Err(); err != nil { - return err - } - if err := send(ctx, recipient, message); err != nil { - errs = append(errs, fmt.Errorf("notify recipient %d: %w", recipient, err)) - continue - } - for _, component := range a.Components { - if changed[component.ID] { - state[recipientPrefix+component.ID] = component.Version - } - } - if err := saveState(a.StatePath, state); err != nil { - errs = append(errs, fmt.Errorf("record notification for recipient %d: %w", recipient, err)) + errs = append(errs, err) } } return errors.Join(errs...) } +func hasReleasedComponent(components []Component) bool { + for _, component := range components { + if _, valid := parseReleaseVersion(component.Version); valid { + return true + } + } + return false +} + +// announceTo sends one recipient the release announcement if it is their +// first time, or if their components have changed since the last check. +func (a Announcer) announceTo(ctx context.Context, recipient int64, send Sender, state map[string]string) error { + prefix := strconv.FormatInt(recipient, 10) + ":" + + if !recipientSeen(state, prefix, a.Components) { + return a.baselineRecipient(state, prefix) + } + + changed, stateChanged := a.detectChanged(state, prefix) + if len(changed) == 0 { + if stateChanged { + return saveState(a.StatePath, state) + } + return nil + } + + return a.sendAndRecord(ctx, recipient, send, state, prefix, changed) +} + +func recipientSeen(state map[string]string, prefix string, components []Component) bool { + for _, component := range components { + if _, ok := state[prefix+component.ID]; ok { + return true + } + } + return false +} + +func (a Announcer) baselineRecipient(state map[string]string, prefix string) error { + for _, component := range a.Components { + if _, valid := parseReleaseVersion(component.Version); valid { + state[prefix+component.ID] = component.Version + } + } + return saveState(a.StatePath, state) +} + +// detectChanged finds components whose version has increased since the last +// recorded state. It also detects previously-unseen components. +func (a Announcer) detectChanged(state map[string]string, prefix string) (changed map[string]bool, stateChanged bool) { + changed = make(map[string]bool, len(a.Components)) + for _, component := range a.Components { + current, validCurrent := parseReleaseVersion(component.Version) + if !validCurrent { + continue + } + key := prefix + component.ID + previousText, seen := state[key] + if !seen { + state[key] = component.Version + stateChanged = true + continue + } + previous, validPrevious := parseReleaseVersion(previousText) + if !validPrevious || compareVersions(current, previous) > 0 { + changed[component.ID] = true + } + } + return +} + +// sendAndRecord sends the announcement and updates state on success. +func (a Announcer) sendAndRecord(ctx context.Context, recipient int64, send Sender, state map[string]string, prefix string, changed map[string]bool) error { + message, err := a.componentMessage(changed) + if err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + if err := send(ctx, recipient, message); err != nil { + return fmt.Errorf("notify recipient %d: %w", recipient, err) + } + for _, component := range a.Components { + if changed[component.ID] { + state[prefix+component.ID] = component.Version + } + } + if err := saveState(a.StatePath, state); err != nil { + return fmt.Errorf("record notification for recipient %d: %w", recipient, err) + } + return nil +} + func (a Announcer) componentMessage(changed map[string]bool) (string, error) { var message strings.Builder message.WriteString("Archie has just been updated.") diff --git a/internal/store/interface.go b/internal/store/interface.go index 85c6e60dc..63c6f6b0d 100644 --- a/internal/store/interface.go +++ b/internal/store/interface.go @@ -14,40 +14,44 @@ import ( // Every storage backend -- SQLite, NellDB, future additions -- implements this // interface. Consumers depend on TaskStore, never on a concrete type. type TaskStore interface { - // ── Task lifecycle ────────────────────────────────────────────── + TaskLifecycle + TaskEvents + TaskQueries +} +// TaskLifecycle manages the core task state machine and enqueuing. +type TaskLifecycle interface { EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string, issueNumber int) (*Task, error) ClaimNext(ctx context.Context) (*Task, error) ClaimByIssue(ctx context.Context, owner, repo string, number int) (*Task, error) - TaskByIssue(ctx context.Context, owner, repo string, number int) (*Task, error) - TaskByID(ctx context.Context, taskID int64) (*Task, error) Transition(ctx context.Context, taskID int64, from, to, detail string) error Update(ctx context.Context, t *Task) error Requeue(ctx context.Context, taskID int64, fromStatus, workflow string) error RecoverStale(ctx context.Context) (int64, error) - IncrementRetryCount(ctx context.Context, taskID int64) error - - // ── Queries ───────────────────────────────────────────────────── +} +// TaskQueries groups read-only task accessors. +type TaskQueries interface { + TaskByIssue(ctx context.Context, owner, repo string, number int) (*Task, error) + TaskByID(ctx context.Context, taskID int64) (*Task, error) WaitingTasks(ctx context.Context) ([]Task, error) OpenPRs(ctx context.Context) ([]Task, error) ClearTerminalTasks(ctx context.Context) (int64, error) + Tasks(ctx context.Context, limit int) ([]Task, error) + StatusCounts(ctx context.Context) (map[string]int, error) + IncrementRetryCount(ctx context.Context, taskID int64) error +} - // ── Observability ─────────────────────────────────────────────── - +// TaskEvents groups observability and lifecycle methods. +type TaskEvents interface { InsertEvent(ctx context.Context, e events.Event) (int64, error) EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) RecentEvents(ctx context.Context, limit int) ([]events.Event, error) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) - Tasks(ctx context.Context, limit int) ([]Task, error) - StatusCounts(ctx context.Context) (map[string]int, error) WorkflowStats(ctx context.Context) ([]WorkflowStat, error) StageStats(ctx context.Context) ([]StageStat, error) TokensByDay(ctx context.Context, days int) ([]DayTokens, error) - - // ── Lifecycle ─────────────────────────────────────────────────── - Close() error } diff --git a/internal/tools/provider/contract_test.go b/internal/tools/provider/contract_test.go index 347e1af11..23e029960 100644 --- a/internal/tools/provider/contract_test.go +++ b/internal/tools/provider/contract_test.go @@ -38,6 +38,15 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) { t.Fatalf("read archied main: %v", err) } text := string(source) + + // Also check telegram_setup.go, which holds the chat-turn wiring + // extracted from main.go during the Phase 4 structural refactor. + tgPath := filepath.Join("..", "..", "..", "cmd", "archied", "telegram_setup.go") + tgSource, tgErr := os.ReadFile(tgPath) + if tgErr == nil { + text += string(tgSource) + } + for _, required := range []string{ "toolprovider.NewRegistry(toolReg)", "memorytoolprovider.New(memManager)", @@ -45,13 +54,14 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) { "capabilityHost.Register(providerRegistry)", // The chat turn builds its toolset from the registry before the // system prompt is rendered, so the prompt can advertise exactly - // the tools the model is handed. - "chatGenerateOptions(nil, toolReg", + // the tools the model is handed. After the Phase 4 structural + // refactor these calls live in telegram_setup.go. + "chatGenerateOptions(nil,", "toolSummaries(options.Tools)", "agentexec.NewInProcessRunner(llm, log, toolReg)", } { if !strings.Contains(text, required) { - t.Errorf("cmd/archied/main.go does not contain tool-provider wiring %q", required) + t.Errorf("archied wiring not found: %q", required) } } if strings.Contains(text, ".RegisterTools(ctx, toolReg)") { diff --git a/internal/tools/provider/provider.go b/internal/tools/provider/provider.go index b0c26cc5d..d9805f608 100644 --- a/internal/tools/provider/provider.go +++ b/internal/tools/provider/provider.go @@ -155,58 +155,12 @@ func (r *Registry) Start(ctx context.Context) error { started := make([]runningProvider, 0, len(order)) for _, id := range order { - registration := providers[id] - if err := safeStart(ctx, registration.engine); err != nil { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) - cleanupErr := safeStop(cleanupCtx, registration.engine) - rollbackFailed, rollbackErr := rollback(cleanupCtx, r.index, started) - cancel() - if cleanupErr != nil { - rollbackFailed = append(rollbackFailed, runningProvider{registration: registration}) - } - r.setFailed(rollbackFailed) - return errors.Join( - fmt.Errorf("start tool provider %q: %w", id, err), - wrapCleanupError(id, cleanupErr), - rollbackErr, - ) - } - - entries, err := safeDiscover(ctx, registration.engine) + reg := providers[id] + rp, err := r.startProvider(ctx, reg, started) if err != nil { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) - cleanupErr := safeStop(cleanupCtx, registration.engine) - rollbackFailed, rollbackErr := rollback(cleanupCtx, r.index, started) - cancel() - if cleanupErr != nil { - rollbackFailed = append(rollbackFailed, runningProvider{registration: registration}) - } - r.setFailed(rollbackFailed) - return errors.Join( - fmt.Errorf("discover tools from provider %q: %w", id, err), - wrapCleanupError(id, cleanupErr), - rollbackErr, - ) + return err } - if err := r.index.RegisterBatch(entries); err != nil { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) - cleanupErr := safeStop(cleanupCtx, registration.engine) - rollbackFailed, rollbackErr := rollback(cleanupCtx, r.index, started) - cancel() - if cleanupErr != nil { - rollbackFailed = append(rollbackFailed, runningProvider{registration: registration}) - } - r.setFailed(rollbackFailed) - return errors.Join( - fmt.Errorf("index tools from provider %q: %w", id, err), - wrapCleanupError(id, cleanupErr), - rollbackErr, - ) - } - started = append(started, runningProvider{ - registration: registration, - toolNames: namesOf(entries), - }) + started = append(started, rp) } r.mu.Lock() @@ -321,6 +275,43 @@ func (r *Registry) setFailed(running []runningProvider) { r.mu.Unlock() } +// startProvider starts, discovers, and indexes one provider. On failure +// it rolls back previously-started providers and returns a joined error. +func (r *Registry) startProvider(ctx context.Context, reg registration, started []runningProvider) (runningProvider, error) { + id := reg.manifest.ID + if err := safeStart(ctx, reg.engine); err != nil { + return runningProvider{}, r.failProvider(ctx, id, reg, started, + fmt.Errorf("start tool provider %q: %w", id, err)) + } + entries, err := safeDiscover(ctx, reg.engine) + if err != nil { + return runningProvider{}, r.failProvider(ctx, id, reg, started, + fmt.Errorf("discover tools from provider %q: %w", id, err)) + } + if err := r.index.RegisterBatch(entries); err != nil { + return runningProvider{}, r.failProvider(ctx, id, reg, started, + fmt.Errorf("index tools from provider %q: %w", id, err)) + } + return runningProvider{ + registration: reg, + toolNames: namesOf(entries), + }, nil +} + +// failProvider stops the failing provider, rolls back previously-started +// providers, and returns a joined error. +func (r *Registry) failProvider(ctx context.Context, id string, reg registration, started []runningProvider, cause error) error { + cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) + defer cancel() + cleanupErr := safeStop(cleanupCtx, reg.engine) + rollbackFailed, rollbackErr := rollback(cleanupCtx, r.index, started) + if cleanupErr != nil { + rollbackFailed = append(rollbackFailed, runningProvider{registration: reg}) + } + r.setFailed(rollbackFailed) + return errors.Join(cause, wrapCleanupError(id, cleanupErr), rollbackErr) +} + func cloneRegistrations(source map[string]registration) map[string]registration { clone := make(map[string]registration, len(source)) for id, value := range source { diff --git a/internal/workflow/steps.go b/internal/workflow/steps.go index 450334249..30dbdb3e1 100644 --- a/internal/workflow/steps.go +++ b/internal/workflow/steps.go @@ -54,17 +54,7 @@ func StageCommit(name string, message func(*TaskContext) string) Stage { func StageCommitPush(message func(*TaskContext) string) Stage { return Stage{Name: "commit-push", Run: func(ctx context.Context, tc *TaskContext) error { if tc.BuildNoChanges { - if tc.Task.IsForgeBacked() { - body := fmt.Sprintf("**archie closed this issue -- no changes required.**\n\n%s", tc.BuildSummary) - if _, err := tc.Forge.Comment(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, body); err != nil { - tc.Log.Warn("no-op close comment failed", "err", err) - } - if err := tc.Forge.CloseIssue(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, ""); err != nil { - return err - } - } - tc.Outcome = Outcome{Status: store.StatusMerged, Detail: "completed -- no changes required"} - return nil + return closeNoChangesIssue(ctx, tc) } commit := StageCommit("commit-push", message) if err := commit.Run(ctx, tc); err != nil { @@ -74,6 +64,22 @@ func StageCommitPush(message func(*TaskContext) string) Stage { }} } +func closeNoChangesIssue(ctx context.Context, tc *TaskContext) error { + if !tc.Task.IsForgeBacked() { + tc.Outcome = Outcome{Status: store.StatusMerged, Detail: "completed -- no changes required"} + return nil + } + body := fmt.Sprintf("**archie closed this issue -- no changes required.**\n\n%s", tc.BuildSummary) + if _, err := tc.Forge.Comment(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, body); err != nil { + tc.Log.Warn("no-op close comment failed", "err", err) + } + if err := tc.Forge.CloseIssue(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, ""); err != nil { + return err + } + tc.Outcome = Outcome{Status: store.StatusMerged, Detail: "completed -- no changes required"} + return nil +} + // StageDiffCap parks tasks whose diff exceeds the configured line cap -- // oversized changes need human pre-approval, not an auto-opened PR. func StageDiffCap() Stage {