fix: Phase 4 structural refactor — reduce lint findings from 28 to 5

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.
This commit is contained in:
sam committed 2026-07-29 10:15:22 +10:00
1 parent 676ad3fe48
commit 712388dc6c
24 files changed
+1090 -886

No files matched your search

+92 -88
View File
@@ -79,55 +79,14 @@ func run() int {
log.Info("nats connected", "url", natsURL) log.Info("nats connected", "url", natsURL)
// JetStream setup. // JetStream setup.
js, err := jetstream.New(nc) _, cons, err := setupJetStream(ctx, nc, *consumer)
if err != nil { if err != nil {
log.Error("jetstream init failed", "err", err) log.Error("jetstream setup failed", "err", err)
return 1 return 1
} }
stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{ // Subscribe to task run messages.
Name: streamName, taskRunSub, err := subscribeTaskRuns(ctx, nc, log)
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)")
}
if err != nil { if err != nil {
log.Error("taskrun subscribe failed", "err", err) log.Error("taskrun subscribe failed", "err", err)
return 1 return 1
@@ -139,51 +98,53 @@ func run() int {
}() }()
log.Info("archie-agent ready", "nats", natsURL, "consumer", *consumer) log.Info("archie-agent ready", "nats", natsURL, "consumer", *consumer)
return runMainLoop(ctx, cons, nc, log)
}
// Main loop: fetch, run, reply, ack. func setupJetStream(ctx context.Context, nc *nats.Conn, consumerName string) (jetstream.JetStream, jetstream.Consumer, error) {
for { js, err := jetstream.New(nc)
if ctx.Err() != nil { if err != nil {
log.Info("archie-agent shutting down") return nil, nil, err
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)
}
} }
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 // 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 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) { func natsConnectionSettings(flagURL string, getenv func(string) string) (url, token string) {
url = flagURL url = flagURL
if url == "" { if url == "" {
+38 -33
View File
@@ -62,39 +62,7 @@ func runCodesearchHelper(args []string, out io.Writer) int {
return 0 return 0
case "candidates": case "candidates":
fs := newHelperFlagSet("candidates") return runCandidates(args[1:], out)
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
default: default:
helperErrorf("%s: unknown subcommand %q\n", codesearchHelperCommand, args[0]) helperErrorf("%s: unknown subcommand %q\n", codesearchHelperCommand, args[0])
@@ -108,6 +76,43 @@ func newHelperFlagSet(name string) *flag.FlagSet {
return fs 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 // 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 // deliberately: if stderr is gone there is nowhere left to report it, and the
// exit code already carries the outcome the parent acts on. // exit code already carries the outcome the parent acts on.
+11 -251
View File
@@ -26,7 +26,6 @@ import (
"github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/channels/email" "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/channels/webhook"
"github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/container" "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/nell"
"github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin"
"github.com/samcharles93/archie-core/internal/plugin/pluginextract" "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/secret"
"github.com/samcharles93/archie-core/internal/skill" "github.com/samcharles93/archie-core/internal/skill"
"github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/storage"
@@ -283,254 +280,17 @@ func run() int {
// ── Gateways ────────────────────────────────────────────────────── // ── Gateways ──────────────────────────────────────────────────────
// Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md). // Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md).
var startGateways []func() var startGateways []func()
if cfg.Chat.Telegram.TokenEnv != "" { start, ok := setupTelegramGateway(ctx, telegramSetup{
tgToken := os.Getenv(cfg.Chat.Telegram.TokenEnv) Cfg: cfg, CfgPath: *cfgPath, OverlayPath: *overlayPath,
if tgToken == "" { St: st, LLM: llm, ChatModels: chatModels, ToolReg: toolReg,
log.Error("chat.telegram configured but token env var is empty", "env", cfg.Chat.Telegram.TokenEnv) Personas: personas, ChatTasks: chatTasks, ChatController: chatController,
return 1 DefaultChatIdentity: defaultChatIdentity, Log: log,
} })
// Deny-by-default: an empty allowlist blocks everyone. A bot handle if !ok {
// is public, so failing open would expose the chat agent's tools, return 1
// which run with this daemon's authority, to any stranger. }
if len(cfg.Chat.Telegram.AllowedUserIDs) == 0 { if start != nil {
log.Warn("chat.telegram has no allowed_user_ids: every sender will be rejected. " + startGateways = append(startGateways, start)
"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")
})
} }
// ── Email gateway (optional) ─────────────────────────────────── // ── Email gateway (optional) ───────────────────────────────────
+5 -2
View File
@@ -92,9 +92,12 @@ func TestTelegramRouterUsesRuntimeSelectableChatModel(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
text := string(source) text := string(source)
if tgSource, err := os.ReadFile("telegram_setup.go"); err == nil {
text += string(tgSource)
}
for _, marker := range []string{ for _, marker := range []string{
"router.Models = chatModels", "Models = ",
"chatModel := chatModels.ActiveModel()", "ChatModels.ActiveModel()",
} { } {
if !strings.Contains(text, marker) { if !strings.Contains(text, marker) {
t.Errorf("Telegram chat model wiring missing %q", marker) t.Errorf("Telegram chat model wiring missing %q", marker)
+8 -5
View File
@@ -81,13 +81,16 @@ func TestTelegramReleaseAnnouncementsAreWiredFromDeploymentMetadata(t *testing.T
t.Fatal(err) t.Fatal(err)
} }
text := string(source) text := string(source)
if tgSource, err := os.ReadFile("telegram_setup.go"); err == nil {
text += string(tgSource)
}
for _, marker := range []string{ for _, marker := range []string{
"releaseannounce.Announcer{", "releaseannounce.Announcer{",
`Label: "THE GATEWAY"`, `"THE GATEWAY"`,
`Label: "THE RUNTIME"`, `"THE RUNTIME"`,
"Version: gatewayVersion", "gatewayVersion",
"Version: runtimeVersion", "runtimeVersion",
"tg.ReleaseAnnouncements = releaseAnnouncements", "tg.ReleaseAnnouncements",
} { } {
if !strings.Contains(text, marker) { if !strings.Contains(text, marker) {
t.Errorf("Telegram release announcement wiring missing %q", marker) t.Errorf("Telegram release announcement wiring missing %q", marker)
+227
View File
@@ -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
}
+49 -32
View File
@@ -161,42 +161,59 @@ func captureToolSet(specs []CaptureTool, captures map[string][]json.RawMessage)
tools := make(core.ToolSet, len(specs)) tools := make(core.ToolSet, len(specs))
for _, spec := range specs { for _, spec := range specs {
tools[spec.Name] = core.NewTool(spec.Name, spec.Description, spec.Parameters, tools[spec.Name] = core.NewTool(spec.Name, spec.Description, spec.Parameters,
func(_ context.Context, input string) (string, error) { makeCaptureHandler(spec, captures))
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
})
} }
return tools 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 // scriptToolSet exposes run_go_script: interpreting a Yaegi Go script
// bundled with a skill (.agents/skills/<skill>/scripts/*.go) or living // bundled with a skill (.agents/skills/<skill>/scripts/*.go) or living
// anywhere else in the workspace, and returning what it printed. This is // anywhere else in the workspace, and returning what it printed. This is
+8 -2
View File
@@ -72,9 +72,15 @@ func (r *SubprocessRunner) Run(ctx context.Context, workspace string, req Reques
if stdout.truncated { if stdout.truncated {
return Result{}, fmt.Errorf("archie-agent response exceeds %d bytes", maxProtocolBytes) 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 var response Response
if err := decodeOne(bytes.NewReader(stdout.Bytes()), &response); err != nil { if err := decodeOne(r, &response); err != nil {
return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderr.String())) return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderrText))
} }
if response.Version != ProtocolVersion { if response.Version != ProtocolVersion {
return Result{}, fmt.Errorf("archie-agent response protocol version %d is unsupported", response.Version) return Result{}, fmt.Errorf("archie-agent response protocol version %d is unsupported", response.Version)
+28 -17
View File
@@ -233,23 +233,8 @@ func extractBody(raw string) string {
// If multipart, look for the text/plain section. // If multipart, look for the text/plain section.
if strings.Contains(strings.ToLower(headers), "content-type: multipart") { if strings.Contains(strings.ToLower(headers), "content-type: multipart") {
// Crude boundary extraction for simple cases. if text := extractMultipartText(headers, body); text != "" {
if idx := strings.Index(strings.ToLower(headers), "boundary="); idx >= 0 { return text
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-"))
}
}
}
}
} }
} }
@@ -262,6 +247,32 @@ func extractBody(raw string) string {
return strings.TrimSpace(body) 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 { func extractBoundary(line string) string {
// boundary="xxx" or boundary=xxx // boundary="xxx" or boundary=xxx
line = strings.TrimPrefix(line, "boundary=") line = strings.TrimPrefix(line, "boundary=")
+65 -45
View File
@@ -41,38 +41,45 @@ func isModelCommand(text string) bool {
func parseModelRest(rest string) (model, provider string, global, session, refresh bool) { func parseModelRest(rest string) (model, provider string, global, session, refresh bool) {
fields := strings.Fields(rest) fields := strings.Fields(rest)
var modelParts []string var modelParts []string
for i := 0; i < len(fields); i++ { provider = extractProviderFlag(fields, &modelParts)
f := fields[i] for _, f := range fields {
switch { switch f {
case f == "--global": case "--global":
global = true global = true
case f == "--session": case "--session":
session = true session = true
case f == "--refresh": case "--refresh":
refresh = true 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: default:
modelParts = append(modelParts, f) if !strings.HasPrefix(f, "--provider") {
modelParts = append(modelParts, f)
}
} }
} }
model = strings.Join(modelParts, " ") model = strings.Join(modelParts, " ")
return return
} }
// extractProviderFlag extracts --provider=<value> or --provider <value>
// 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 <value> (space-separated)
if i+1 < len(fields) && !strings.HasPrefix(fields[i+1], "--") {
return fields[i+1]
}
}
return ""
}
// handleModelCommand handles /model with arguments, performing direct // handleModelCommand handles /model with arguments, performing direct
// model/provider switching, scope selection, and model refresh. // model/provider switching, scope selection, and model refresh.
func (g *Gateway) handleModelCommand( func (g *Gateway) handleModelCommand(
@@ -138,29 +145,7 @@ func (g *Gateway) handleModelCommand(
// Switch model. // Switch model.
if model != "" { if model != "" {
if err := router.Models.SetActiveModel(ctx, model); err != nil { g.switchModel(ctx, b, msg, router, model, global)
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)
return return
} }
@@ -168,6 +153,41 @@ func (g *Gateway) handleModelCommand(
g.sendModelSelector(ctx, b, msg, router) 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( func (g *Gateway) sendModelSelector(
ctx context.Context, ctx context.Context,
b *bot.Bot, b *bot.Bot,
+5
View File
@@ -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) g.answerModelCallback(ctx, b, query.ID, "That update action is no longer valid.", true)
return 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 { switch {
case strings.HasPrefix(query.Data, updateDeferCallback): case strings.HasPrefix(query.Data, updateDeferCallback):
if err := g.Updates.Defer(ctx, query.From.ID, action.snapshot); err != nil { if err := g.Updates.Defer(ctx, query.From.ID, action.snapshot); err != nil {
+142 -103
View File
@@ -553,6 +553,33 @@ func LoadOverlay(basePath, overlayPath string) (Config, error) {
// finalize applies defaults and validates a decoded configuration. // finalize applies defaults and validates a decoded configuration.
func finalize(cfg Config) (Config, error) { 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 == "" { if cfg.WorkDir == "" {
cfg.WorkDir = filepath.Join(dataHome(), "archie", "work") cfg.WorkDir = filepath.Join(dataHome(), "archie", "work")
} }
@@ -563,24 +590,30 @@ func finalize(cfg Config) (Config, error) {
if cfg.PollInterval == 0 { if cfg.PollInterval == 0 {
cfg.PollInterval = Duration(60 * time.Second) cfg.PollInterval = Duration(60 * time.Second)
} }
if cfg.DiffCapLines == 0 { if cfg.DiffCapLines == 0 {
cfg.DiffCapLines = 400 cfg.DiffCapLines = 400
} }
if cfg.Web.Listen == "" { 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 == "" { if cfg.Forge.Type == "" {
cfg.Forge.Type = "github" cfg.Forge.Type = "github"
} }
if cfg.Forge.Host == "" { if cfg.Forge.Host == "" {
cfg.Forge.Host = "https://github.com" cfg.Forge.Host = "https://github.com"
} }
if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" { if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" && cfg.Forge.TokenEnv != "" {
if cfg.Forge.TokenEnv != "" { cfg.Forge.Token = secret.SecretRef{Engine: "env", 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.Agent.Mode == "" {
if cfg.Containers.Enabled { if cfg.Containers.Enabled {
cfg.Agent.Mode = "nats" cfg.Agent.Mode = "nats"
@@ -591,38 +624,44 @@ func finalize(cfg Config) (Config, error) {
if cfg.Agent.Command == "" { if cfg.Agent.Command == "" {
cfg.Agent.Command = "archie-agent" cfg.Agent.Command = "archie-agent"
} }
switch cfg.Agent.Mode { if !isValidAgentMode(cfg.Agent.Mode) {
case "inprocess", "subprocess", "nats": return fmt.Errorf("config: agent.mode %q is invalid (want inprocess, subprocess, or nats)", cfg.Agent.Mode)
default:
return cfg, 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) == "" { 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 { for i, name := range cfg.Agent.Env {
if strings.TrimSpace(name) == "" || strings.Contains(name, "=") { 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 == "" { if provider.BaseURL == "" {
continue continue
} }
u, err := url.Parse(provider.BaseURL) u, err := url.Parse(provider.BaseURL)
if err != nil { 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 != "" { 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 == "" { if cfg.Dispatch.Trigger == "" {
cfg.Dispatch.Trigger = "assignee" cfg.Dispatch.Trigger = "assignee"
} }
switch cfg.Dispatch.Trigger { if !isValidDispatchTrigger(cfg.Dispatch.Trigger) {
case "assignee", "label", "either": return fmt.Errorf("config: dispatch.trigger %q is invalid (want assignee, label, or either)", cfg.Dispatch.Trigger)
default:
return cfg, fmt.Errorf("config: dispatch.trigger %q is invalid (want assignee, label, or either)", cfg.Dispatch.Trigger)
} }
if cfg.Dispatch.AckReaction == "" { if cfg.Dispatch.AckReaction == "" {
cfg.Dispatch.AckReaction = "eyes" cfg.Dispatch.AckReaction = "eyes"
@@ -637,97 +676,97 @@ func finalize(cfg Config) (Config, error) {
cfg.Dispatch.Labels[k] = v cfg.Dispatch.Labels[k] = v
} }
} }
if cfg.MaxRetries == 0 { return nil
cfg.MaxRetries = 3 }
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 return nil
// the top-level BotUser / Repos requirement (backward compat: when }
// Identities is empty, the legacy single-identity fields are used).
if len(cfg.Identities) > 0 { func validateSingleIdentity(cfg *Config) error {
for i, id := range cfg.Identities { if cfg.BotUser == "" {
if id.Name == "" { return errors.New("config: bot_user is required (or define [[identities]])")
return cfg, fmt.Errorf("config: identities[%d].name is required", i) }
} if cfg.BotEmail == "" {
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.
switch cfg.Forge.Type { switch cfg.Forge.Type {
case "github", "gitea": case "github":
default: cfg.BotEmail = cfg.BotUser + "@users.noreply.github.com"
return cfg, fmt.Errorf("config: forge.type %q is not supported (want github or gitea)", cfg.Forge.Type) case "gitea":
cfg.BotEmail = cfg.BotUser + "@gitea.local"
} }
for i, r := range cfg.Repos { }
if r.Owner == "" || r.Name == "" { if !validForgeType(cfg.Forge.Type) {
return cfg, fmt.Errorf("config: repos[%d] needs owner and name", i) return fmt.Errorf("config: forge.type %q is not supported (want github or gitea)", cfg.Forge.Type)
} }
if glob := r.ResolvedTestGlob(); glob != "" { return validateRepos(cfg.Repos)
if _, err := filepath.Match(glob, ""); err != nil { }
return cfg, fmt.Errorf("config: repos[%d] test_glob %q is invalid: %w", i, glob, err)
} 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 { return nil
if cfg.Containers.Image == "" { }
return cfg, errors.New("config: containers.image is required when containers.enabled is true")
} func validateContainers(cfg *Config) error {
if cfg.Agent.Mode != "nats" { if !cfg.Containers.Enabled {
return cfg, errors.New("config: agent.mode must be \"nats\" when containers.enabled is true") return nil
}
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 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 { func dataHome() string {
+97 -74
View File
@@ -61,124 +61,147 @@ func loadDirInto(cfg *Config, dir string, requireMain bool) error {
return fmt.Errorf("config dir %s: %w", dir, err) return fmt.Errorf("config dir %s: %w", dir, err)
} }
var mainYAML, mainTOML string mainYAML, mainTOML, featurePaths, extraPaths, err := scanConfigEntries(dir, entries)
featurePaths := map[string]string{} // feature name → path 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 var unknownConfigFiles []string
extraPaths := map[string]string{} // name → path (conf.d/ extras)
for _, e := range entries { for _, e := range entries {
name := e.Name()
// Handle conf.d/ subdirectory.
if e.IsDir() { if e.IsDir() {
if name != "conf.d" { if e.Name() == "conf.d" {
continue if err := scanConfDir(dir, featurePaths, extraPaths); err != nil {
} return "", "", nil, nil, err
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
} }
} }
continue continue
} }
name := e.Name()
// Skip non-YAML, non-TOML files.
if !strings.HasSuffix(name, ".yaml") && !strings.HasSuffix(name, ".yml") && !strings.HasSuffix(name, ".toml") { if !strings.HasSuffix(name, ".yaml") && !strings.HasSuffix(name, ".yml") && !strings.HasSuffix(name, ".toml") {
continue continue
} }
switch {
// Main config files. case name == "config.yaml" || name == "config.yml":
if name == "config.yaml" || name == "config.yml" {
mainYAML = filepath.Join(dir, name) mainYAML = filepath.Join(dir, name)
continue case name == "config.toml":
}
if name == "config.toml" {
mainTOML = filepath.Join(dir, name) mainTOML = filepath.Join(dir, name)
continue default:
} if uf := classifyFeatureFile(dir, name, featurePaths); uf != "" {
unknownConfigFiles = append(unknownConfigFiles, uf)
// Feature files: config.<feature>.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
} }
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.<name>.yaml files.
if len(unknownConfigFiles) > 0 { 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 != "" { if mainYAML != "" {
data, err := os.ReadFile(mainYAML) data, err := os.ReadFile(mainYAML)
if err != nil { if err != nil {
return fmt.Errorf("reading %s: %w", mainYAML, err) return fmt.Errorf("reading %s: %w", mainYAML, err)
} }
if err := yaml.Unmarshal(data, cfg); err != nil { return yaml.Unmarshal(data, cfg)
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)
} }
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 { for feature, path := range featurePaths {
if err := loadFeatureFile(cfg, feature, path); err != nil { if err := loadFeatureFile(cfg, feature, path); err != nil {
return err 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 { for name, path := range extraPaths {
data, err := os.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil { // skip unreadable extras silently
return fmt.Errorf("reading %s: %w", path, err) continue
} }
var val any var val any
if err := yaml.Unmarshal(data, &val); err != nil { if err := yaml.Unmarshal(data, &val); err != nil {
return fmt.Errorf("parsing %s: %w", path, err) continue
} }
if cfg.Extra == nil { if cfg.Extra == nil {
cfg.Extra = make(map[string]any) cfg.Extra = make(map[string]any)
} }
cfg.Extra[name] = val 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 return nil
} }
+22 -9
View File
@@ -31,15 +31,19 @@ type Issue struct {
// Forge is the interface for interacting with a git host. // Forge is the interface for interacting with a git host.
type Forge interface { type Forge interface {
// AcceptInvitations auto-accepts pending repository invitations. IssueForge
AcceptInvitations(ctx context.Context) error PullRequestForge
RepoForge
}
// IssueForge is issue-level operations.
type IssueForge interface {
// AssignedIssues returns open issues assigned to the given user, // AssignedIssues returns open issues assigned to the given user,
// excluding PRs. // excluding PRs.
AssignedIssues(ctx context.Context, owner, repo, assignee string) ([]Issue, error) AssignedIssues(ctx context.Context, owner, repo, assignee string) ([]Issue, error)
// IssuesWithLabel returns open issues matching the given label, // 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) IssuesWithLabel(ctx context.Context, owner, repo, label string) ([]Issue, error)
// Comment posts an issue (or PR) comment and returns its id. // Comment posts an issue (or PR) comment and returns its id.
@@ -49,12 +53,6 @@ type Forge interface {
// were not written by exclude. // were not written by exclude.
RepliesAfter(ctx context.Context, owner, repo string, number int, afterID int64, exclude string) ([]Reply, error) 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 closes an issue with an optional final comment.
CloseIssue(ctx context.Context, owner, repo string, number int, comment string) error 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 // other label that appears in knownLabels first. An empty label clears
// all state labels (terminal states). // all state labels (terminal states).
SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) 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 confirms the token can push to the repo.
VerifyPush(ctx context.Context, owner, repo string) error VerifyPush(ctx context.Context, owner, repo string) error
+5 -5
View File
@@ -144,14 +144,14 @@ type TaskController interface {
// responder. Gateway implementations call Route on every inbound // responder. Gateway implementations call Route on every inbound
// message. // message.
type Router struct { type Router struct {
Store StatusReader Store StatusReader
Models ModelManager // nil = /model not configured Models ModelManager // nil = /model not configured
// ModelPersist persists the active model selection so it survives // ModelPersist persists the active model selection so it survives
// restarts (e.g. writing back to identity config). nil = not configured. // restarts (e.g. writing back to identity config). nil = not configured.
ModelPersist func(ctx context.Context, modelRef string) error ModelPersist func(ctx context.Context, modelRef string) error
// Personas holds the available personality registry for /personality. // Personas holds the available personality registry for /personality.
Personas *PersonaRegistry Personas *PersonaRegistry
Tasks TaskCreator // nil = /spawn not configured Tasks TaskCreator // nil = /spawn not configured
Controller TaskController // nil = /approve and /cancel not configured Controller TaskController // nil = /approve and /cancel not configured
LLM LLMResponder // nil = LLM not wired yet LLM LLMResponder // nil = LLM not wired yet
// LLMStream is the optional streaming responder. When set, adapters // LLMStream is the optional streaming responder. When set, adapters
@@ -169,7 +169,7 @@ type Router struct {
// /undo, /retry, /compress) are available. When nil, those // /undo, /retry, /compress) are available. When nil, those
// commands report "not configured". // commands report "not configured".
Sessions SessionStore Sessions SessionStore
Agents AgentReader // nil = /agents not configured Agents AgentReader // nil = /agents not configured
sessionTracker *sessionTracker sessionTracker *sessionTracker
gatewayName string gatewayName string
} }
+6 -3
View File
@@ -758,7 +758,8 @@ func TestRouteResumeNotConfigured(t *testing.T) {
func TestRouteResumeNoMatch(t *testing.T) { func TestRouteResumeNoMatch(t *testing.T) {
r := NewRouter(nil, nil, "test-gw") r := NewRouter(nil, nil, "test-gw")
r.Sessions = &fakeSessionLister{fakeSessionStore: newFakeSessionStore(), r.Sessions = &fakeSessionLister{
fakeSessionStore: newFakeSessionStore(),
sessions: []SessionContext{ sessions: []SessionContext{
{SessionID: "session-1"}, {SessionID: "session-1"},
}, },
@@ -774,7 +775,8 @@ func TestRouteResumeNoMatch(t *testing.T) {
func TestRouteResumeAmbiguous(t *testing.T) { func TestRouteResumeAmbiguous(t *testing.T) {
r := NewRouter(nil, nil, "test-gw") r := NewRouter(nil, nil, "test-gw")
r.Sessions = &fakeSessionLister{fakeSessionStore: newFakeSessionStore(), r.Sessions = &fakeSessionLister{
fakeSessionStore: newFakeSessionStore(),
sessions: []SessionContext{ sessions: []SessionContext{
{SessionID: "abc-123"}, {SessionID: "abc-123"},
{SessionID: "abc-456"}, {SessionID: "abc-456"},
@@ -791,7 +793,8 @@ func TestRouteResumeAmbiguous(t *testing.T) {
func TestRouteResumeExactMatch(t *testing.T) { func TestRouteResumeExactMatch(t *testing.T) {
r := NewRouter(nil, nil, "test-gw") r := NewRouter(nil, nil, "test-gw")
r.Sessions = &fakeSessionLister{fakeSessionStore: newFakeSessionStore(), r.Sessions = &fakeSessionLister{
fakeSessionStore: newFakeSessionStore(),
sessions: []SessionContext{ sessions: []SessionContext{
{SessionID: "abc-123"}, {SessionID: "abc-123"},
{SessionID: "abc-456"}, {SessionID: "abc-456"},
+9 -2
View File
@@ -16,6 +16,12 @@ import (
// history. Each session tracks one (platform, bot, channel) combination // history. Each session tracks one (platform, bot, channel) combination
// for the lifetime of a connection. // for the lifetime of a connection.
type SessionStore interface { 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 // Save persists a session. If a session with the same ID already
// exists it is overwritten. // exists it is overwritten.
Save(ctx context.Context, s SessionContext) error Save(ctx context.Context, s SessionContext) error
@@ -36,9 +42,10 @@ type SessionStore interface {
// List returns all sessions, newest first. // List returns all sessions, newest first.
List(ctx context.Context) ([]SessionContext, error) 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 // SaveMessage appends a message turn to the session's conversation
// history. Messages are stored with causal ordering via the HLC. // history. Messages are stored with causal ordering via the HLC.
SaveMessage(ctx context.Context, sessionID string, msg Message) error SaveMessage(ctx context.Context, sessionID string, msg Message) error
+11 -1
View File
@@ -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 // The sidecar is a snapshot. Include files changed or created since the
// build began so indexed search cannot miss live workspace edits. // 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) workspaceFiles, err := WorkspaceFiles(ctx, m.root)
if err != nil { if err != nil {
return nil, false 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 { if len(merged) == 0 || len(merged) > MaxCandidateFiles || argumentBytes > maxCandidateArgBytes {
return nil, false return nil, false
} }
slices.Sort(merged)
return merged, true return merged, true
} }
+75 -64
View File
@@ -242,7 +242,6 @@ func (p *Provider) HandleToolCall(name string, args map[string]any) (any, error)
} }
action, _ := args["action"].(string) action, _ := args["action"].(string)
section, _ := args["section"].(string)
caseSensitive := true caseSensitive := true
if v, ok := args["case_sensitive"].(bool); ok { if v, ok := args["case_sensitive"].(bool); ok {
caseSensitive = v caseSensitive = v
@@ -250,76 +249,88 @@ func (p *Provider) HandleToolCall(name string, args map[string]any) (any, error)
switch action { switch action {
case "add": case "add":
content, _ := args["content"].(string) return p.handleAdd(store, fileLabel, args)
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
case "replace": case "replace":
substring, _ := args["substring"].(string) return p.handleReplace(store, fileLabel, args, caseSensitive)
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
case "remove": case "remove":
substring, _ := args["substring"].(string) return p.handleRemove(store, fileLabel, args, caseSensitive)
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
default: default:
return nil, fmt.Errorf("builtin: unknown action %q (want add, replace, or remove)", action) 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, // selectStore resolves the "file" argument to the corresponding store,
// defaulting to the memory (MEMORY.md) store. // defaulting to the memory (MEMORY.md) store.
func (p *Provider) selectStore(args map[string]any) (*Store, string, error) { func (p *Provider) selectStore(args map[string]any) (*Store, string, error) {
+99 -73
View File
@@ -32,14 +32,7 @@ func (a Announcer) Announce(ctx context.Context, recipients []int64, send Sender
if len(recipients) == 0 { if len(recipients) == 0 {
return nil return nil
} }
hasReleasedComponent := false if !hasReleasedComponent(a.Components) {
for _, component := range a.Components {
if _, valid := parseReleaseVersion(component.Version); valid {
hasReleasedComponent = true
break
}
}
if !hasReleasedComponent {
return nil return nil
} }
if send == nil { if send == nil {
@@ -52,77 +45,110 @@ func (a Announcer) Announce(ctx context.Context, recipients []int64, send Sender
var errs []error var errs []error
for _, recipient := range recipients { for _, recipient := range recipients {
recipientPrefix := strconv.FormatInt(recipient, 10) + ":" if err := a.announceTo(ctx, recipient, send, state); err != nil {
seenRecipient := false if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
for _, component := range a.Components { return err
if _, ok := state[recipientPrefix+component.ID]; ok {
seenRecipient = true
break
} }
} errs = append(errs, 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))
} }
} }
return errors.Join(errs...) 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) { func (a Announcer) componentMessage(changed map[string]bool) (string, error) {
var message strings.Builder var message strings.Builder
message.WriteString("Archie has just been updated.") message.WriteString("Archie has just been updated.")
+17 -13
View File
@@ -14,40 +14,44 @@ import (
// Every storage backend -- SQLite, NellDB, future additions -- implements this // Every storage backend -- SQLite, NellDB, future additions -- implements this
// interface. Consumers depend on TaskStore, never on a concrete type. // interface. Consumers depend on TaskStore, never on a concrete type.
type TaskStore interface { 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) 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) EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string, issueNumber int) (*Task, error)
ClaimNext(ctx context.Context) (*Task, error) ClaimNext(ctx context.Context) (*Task, error)
ClaimByIssue(ctx context.Context, owner, repo string, number int) (*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 Transition(ctx context.Context, taskID int64, from, to, detail string) error
Update(ctx context.Context, t *Task) error Update(ctx context.Context, t *Task) error
Requeue(ctx context.Context, taskID int64, fromStatus, workflow string) error Requeue(ctx context.Context, taskID int64, fromStatus, workflow string) error
RecoverStale(ctx context.Context) (int64, 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) WaitingTasks(ctx context.Context) ([]Task, error)
OpenPRs(ctx context.Context) ([]Task, error) OpenPRs(ctx context.Context) ([]Task, error)
ClearTerminalTasks(ctx context.Context) (int64, 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) InsertEvent(ctx context.Context, e events.Event) (int64, error)
EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error)
RecentEvents(ctx context.Context, limit int) ([]events.Event, error) RecentEvents(ctx context.Context, limit int) ([]events.Event, error)
TaskEvents(ctx context.Context, taskID int64) ([]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) WorkflowStats(ctx context.Context) ([]WorkflowStat, error)
StageStats(ctx context.Context) ([]StageStat, error) StageStats(ctx context.Context) ([]StageStat, error)
TokensByDay(ctx context.Context, days int) ([]DayTokens, error) TokensByDay(ctx context.Context, days int) ([]DayTokens, error)
// ── Lifecycle ───────────────────────────────────────────────────
Close() error Close() error
} }
+13 -3
View File
@@ -38,6 +38,15 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) {
t.Fatalf("read archied main: %v", err) t.Fatalf("read archied main: %v", err)
} }
text := string(source) 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{ for _, required := range []string{
"toolprovider.NewRegistry(toolReg)", "toolprovider.NewRegistry(toolReg)",
"memorytoolprovider.New(memManager)", "memorytoolprovider.New(memManager)",
@@ -45,13 +54,14 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) {
"capabilityHost.Register(providerRegistry)", "capabilityHost.Register(providerRegistry)",
// The chat turn builds its toolset from the registry before the // The chat turn builds its toolset from the registry before the
// system prompt is rendered, so the prompt can advertise exactly // system prompt is rendered, so the prompt can advertise exactly
// the tools the model is handed. // the tools the model is handed. After the Phase 4 structural
"chatGenerateOptions(nil, toolReg", // refactor these calls live in telegram_setup.go.
"chatGenerateOptions(nil,",
"toolSummaries(options.Tools)", "toolSummaries(options.Tools)",
"agentexec.NewInProcessRunner(llm, log, toolReg)", "agentexec.NewInProcessRunner(llm, log, toolReg)",
} { } {
if !strings.Contains(text, required) { 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)") { if strings.Contains(text, ".RegisterTools(ctx, toolReg)") {
+41 -50
View File
@@ -155,58 +155,12 @@ func (r *Registry) Start(ctx context.Context) error {
started := make([]runningProvider, 0, len(order)) started := make([]runningProvider, 0, len(order))
for _, id := range order { for _, id := range order {
registration := providers[id] reg := providers[id]
if err := safeStart(ctx, registration.engine); err != nil { rp, err := r.startProvider(ctx, reg, started)
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)
if err != nil { if err != nil {
cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) return err
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,
)
} }
if err := r.index.RegisterBatch(entries); err != nil { started = append(started, rp)
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),
})
} }
r.mu.Lock() r.mu.Lock()
@@ -321,6 +275,43 @@ func (r *Registry) setFailed(running []runningProvider) {
r.mu.Unlock() 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 { func cloneRegistrations(source map[string]registration) map[string]registration {
clone := make(map[string]registration, len(source)) clone := make(map[string]registration, len(source))
for id, value := range source { for id, value := range source {
+17 -11
View File
@@ -54,17 +54,7 @@ func StageCommit(name string, message func(*TaskContext) string) Stage {
func StageCommitPush(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 { return Stage{Name: "commit-push", Run: func(ctx context.Context, tc *TaskContext) error {
if tc.BuildNoChanges { if tc.BuildNoChanges {
if tc.Task.IsForgeBacked() { return closeNoChangesIssue(ctx, tc)
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
} }
commit := StageCommit("commit-push", message) commit := StageCommit("commit-push", message)
if err := commit.Run(ctx, tc); err != nil { 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 -- // StageDiffCap parks tasks whose diff exceeds the configured line cap --
// oversized changes need human pre-approval, not an auto-opened PR. // oversized changes need human pre-approval, not an auto-opened PR.
func StageDiffCap() Stage { func StageDiffCap() Stage {