refactor: pre-cleanup.

This commit is contained in:
sam committed 2026-08-06 01:40:15 +10:00
1 parent 0ceab9d897
commit e10cbbc92c
53 files changed
+3596 -648

No files matched your search

+139 -14
View File
@@ -10,6 +10,7 @@ import (
"flag"
"fmt"
"log/slog"
"maps"
"net"
"os"
"os/signal"
@@ -56,6 +57,7 @@ import (
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
memorytoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/memory"
"github.com/samcharles93/archie-core/internal/tools/webfetch"
"github.com/samcharles93/archie-core/internal/webui"
"github.com/samcharles93/archie-core/internal/workflow/skillbuild"
"github.com/samcharles93/archie-core/internal/workflow/wfeval"
@@ -76,12 +78,6 @@ func main() {
const (
// defaultChatMaxSteps bounds the model/tool round-trips in one chat
// turn when [config.ChatConfig.MaxSteps] is unset.
//
// It was 8, which is roughly one read, one grep and a couple of edits
// -- less than a single small change to a Go package costs. Turns hit
// the cap and stopped mid-task with no indication they had been cut
// off. Interruption is /stop's job; this only has to stop a genuine
// runaway, so it is set well above real work.
defaultChatMaxSteps = 100
packagedGatewayChangelogPath = "/usr/share/archie/CHANGELOG.archied.md"
packagedRuntimeChangelogPath = "/usr/share/archie/CHANGELOG.archie.md"
@@ -95,15 +91,24 @@ var (
runtimeVersion = "dev"
)
// chatGenerateOptions builds one chat turn's request.
func chatGenerateOptions(
messages []chat.Message,
registry *tools.Registry,
maxSteps int,
limits agentexec.ToolLimits,
extra []tools.ToolEntry,
) (core.GenerateOptions, error) {
toolSet, err := agentexec.BuildToolSet(registry)
toolOpts := limits.Options()
toolSet, err := agentexec.BuildToolSet(registry, toolOpts)
if err != nil {
return core.GenerateOptions{}, err
}
extraSet, err := agentexec.BuildToolSetFrom(extra, toolOpts)
if err != nil {
return core.GenerateOptions{}, err
}
maps.Copy(toolSet, extraSet)
if maxSteps <= 0 {
maxSteps = defaultChatMaxSteps
}
@@ -114,6 +119,71 @@ func chatGenerateOptions(
}, nil
}
// isWithin reports whether target sits inside base.
func isWithin(base, target string) bool {
absBase, err := filepath.Abs(base)
if err != nil {
return false
}
absTarget, err := filepath.Abs(target)
if err != nil {
return false
}
rel, err := filepath.Rel(absBase, absTarget)
if err != nil {
return false
}
return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
}
// toolLimits reads the configured per-turn result limits.
func toolLimits(cfg config.Config) agentexec.ToolLimits {
return agentexec.ToolLimits{
MaxResultChars: cfg.Tools.Policy.MaxResultChars,
TurnBudgetChars: cfg.Tools.Policy.TurnBudgetChars,
SpillDir: cfg.Tools.Policy.SpillDir,
}
}
// taskListOverRead multiplies the requested limit when reading rows that are
// filtered by identity afterwards.
const taskListOverRead = 5
// chatTaskListerAdapter gives the gateway a read view of one identity's tasks.
// gateway deliberately does not import internal/store, so the projection
// happens here, as it does for the writer and controller adapters below.
type chatTaskListerAdapter struct {
tasks func(context.Context, int) ([]store.Task, error)
}
func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) {
// Over-read before filtering: Tasks applies its limit across the whole
// table, so asking for exactly `limit` would return fewer than that for
// this identity whenever another identity's work is more recent.
rows, err := a.tasks(ctx, limit*taskListOverRead)
if err != nil {
return nil, err
}
out := make([]gateway.ChatTaskSummary, 0, limit)
for _, task := range rows {
if task.Identity != identity {
continue
}
if len(out) >= limit {
break
}
out = append(out, gateway.ChatTaskSummary{
ID: task.ID,
Repo: task.Owner + "/" + task.Repo,
Title: task.Title,
Status: task.Status,
Workflow: task.Workflow,
PRNumber: task.PRNumber,
})
}
return out, nil
}
func configuredMCPProvider(server config.MCPServer) (toolprovider.Engine, error) {
name := strings.TrimSpace(server.Name)
if name == "" {
@@ -394,21 +464,27 @@ func run() int {
}
}
webRouter.Models = chatModels
webRouter.Updates = updateService
if updateService != nil {
webRouter.Updates = updateService
}
webRouter.Personas = personas
webRouter.InitSessions(chatSessionStore)
configureTaskCommands(webRouter, chatTasks, chatController, defaultChatIdentity)
webSetup := telegramSetup{
Cfg: cfg, St: st, LLM: llm, ChatModels: chatModels, ToolReg: toolReg,
Personas: personas, ChatTasks: chatTasks, ChatController: chatController,
ChatTaskLister: chatTaskListerAdapter{tasks: st.Tasks},
DefaultChatIdentity: defaultChatIdentity, SessionStore: chatSessionStore,
Bus: bus, Log: log,
}
webRouter.LLM, webRouter.LLMStream = makeChatLLMResponder(ctx, "web", webSetup, chatSessionStore, webRouter)
webRouter.LLM, webRouter.LLMStream = makeChatLLMResponder("web", webSetup, chatSessionStore, webRouter)
web.Chat = &webui.ChatService{
Router: webRouter, Sessions: chatSessionStore,
Turns: gateway.NewTurns(log),
Models: chatModels, Personas: personas, Updates: updateService,
Models: chatModels, Personas: personas,
}
if updateService != nil {
web.Chat.Updates = updateService
}
if l := cfg.Web.Listen; l != "" && l != "off" {
web.Token = webTokenFor(l, cfg.DBPath, log)
@@ -426,6 +502,7 @@ func run() int {
Cfg: cfg, CfgPath: *cfgPath, OverlayPath: *overlayPath,
St: st, LLM: llm, ChatModels: chatModels, ToolReg: toolReg,
Personas: personas, ChatTasks: chatTasks, ChatController: chatController,
ChatTaskLister: chatTaskListerAdapter{tasks: st.Tasks},
DefaultChatIdentity: defaultChatIdentity, SessionStore: chatSessionStore, Updates: updateService,
Bus: bus,
RegisterRestart: func(request func() error) { restartTelegram = request }, Log: log,
@@ -485,7 +562,9 @@ func run() int {
Providers: providers,
}
case "inprocess":
agentRunner = agentexec.NewInProcessRunner(llm, log, toolReg)
inproc := agentexec.NewInProcessRunner(llm, log, toolReg)
inproc.Limits = toolLimits(cfg)
agentRunner = inproc
case "nats":
if natsClient == nil {
log.Error("agent.mode is nats but [nats] is not configured")
@@ -581,7 +660,9 @@ func run() int {
Providers: idProviders,
}
case "inprocess":
runner.Agent = agentexec.NewInProcessRunner(idRuntime, log.With("identity", idCfg.Name), toolReg)
idRunner := agentexec.NewInProcessRunner(idRuntime, log.With("identity", idCfg.Name), toolReg)
idRunner.Limits = toolLimits(cfg)
runner.Agent = idRunner
case "nats":
runner.Agent = &agentexec.NATSRunner{
Bus: natsClient, Providers: idProviders,
@@ -698,11 +779,16 @@ func run() int {
// configured: these read, write and execute, so the directory is a
// deliberate choice rather than a default.
if workspace := cfg.Chat.Workspace; workspace != "" {
if err := providerRegistry.Register(builtintoolprovider.New(workspace)); err != nil {
unrestricted := cfg.Chat.UnrestrictedFilesystem
if err := providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted)); err != nil {
log.Error("workspace tool provider registration failed", "err", err)
return 1
}
log.Info("workspace tools enabled", "workspace", workspace)
// Logged at the same level either way: which of the two postures is
// running is the first thing worth knowing when a file tool refuses a
// path, or reaches one it should not have.
log.Info("workspace tools enabled",
"workspace", workspace, "unrestricted_filesystem", unrestricted)
} else {
log.Info("workspace tools disabled (chat.workspace is unset)")
}
@@ -741,6 +827,45 @@ func run() int {
}
}
// Tool results too large to inline are written here. Created once, at
// startup: the write failure inside a turn is silent, so without this
// every oversized result would quietly fall back to truncation and
// spilling would look configured while doing nothing.
if spillDir := cfg.Tools.Policy.SpillDir; spillDir != "" {
if err := toolLimits(cfg).EnsureSpillDir(); err != nil {
log.Warn("tool spill directory unavailable; large results will be truncated instead", "err", err)
} else if ws := cfg.Chat.Workspace; ws != "" && !cfg.Chat.UnrestrictedFilesystem && !isWithin(ws, spillDir) {
// A spill hands the model a path to read back, and a confined read
// tool refuses anything outside the workspace. Outside it, the
// spill reference is a dead end and the result is lost rather than
// displaced -- worse than truncating in the first place.
//
// Not a concern when the filesystem is unrestricted: the read tool
// can reach the spill wherever it lives.
log.Warn("tool spill directory is outside chat.workspace; the model cannot read back what is spilled there",
"spill_dir", spillDir, "workspace", ws)
}
}
// web_fetch. Registered directly rather than as a tool provider: it has
// no process to start or stop, so the provider lifecycle would buy
// nothing. Disabled by configuration returns nil and advertises nothing.
if entry := webfetch.Tool(webfetch.Config{
Enabled: cfg.Tools.WebFetch.IsEnabled(),
Timeout: cfg.Tools.WebFetch.Timeout.Std(),
MaxBytes: cfg.Tools.WebFetch.MaxBytes,
AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks,
}); entry != nil {
if err := toolReg.Register(*entry); err != nil {
log.Warn("web_fetch registration failed", "err", err)
} else {
log.Info("web fetch enabled",
"allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks)
}
} else {
log.Info("web fetch disabled")
}
// Built-in tools are registered at startup via init(). Tool discovery
// via Yaegi is deferred to the container/agent runtime.
+6 -6
View File
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
@@ -17,6 +16,7 @@ import (
natsio "github.com/nats-io/nats.go"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/forgerpc"
@@ -39,7 +39,7 @@ import (
func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) {
t.Parallel()
log := slog.New(slog.NewTextHandler(io.Discard, nil))
log := slog.New(slog.DiscardHandler)
tests := []struct {
name string
@@ -90,7 +90,7 @@ func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) {
// Not parallel: t.Setenv mutates process state and panics under t.Parallel.
func TestResolveForgeBuildsClientWhenTokenResolves(t *testing.T) {
t.Setenv("ARCHIE_TEST_FORGE_TOKEN", "ghp_exampletoken")
log := slog.New(slog.NewTextHandler(io.Discard, nil))
log := slog.New(slog.DiscardHandler)
client, token := resolveForge(config.Forge{
Type: "github",
@@ -200,7 +200,7 @@ func TestChatGenerateOptionsIncludesToolsAndMultipleSteps(t *testing.T) {
}
messages := []chat.Message{{Role: chat.RoleUser, Content: "remember this"}}
options, err := chatGenerateOptions(messages, registry, 0)
options, err := chatGenerateOptions(messages, registry, 0, agentexec.ToolLimits{}, nil)
if err != nil {
t.Fatal(err)
}
@@ -226,7 +226,7 @@ func TestChatGenerateOptionsReportsInvalidToolSchemas(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
if _, err := chatGenerateOptions(nil, registry, 0); err == nil {
if _, err := chatGenerateOptions(nil, registry, 0, agentexec.ToolLimits{}, nil); err == nil {
t.Fatal("chatGenerateOptions() error = nil, want invalid tool schema error")
}
}
@@ -235,7 +235,7 @@ func TestChatGenerateOptionsReportsInvalidToolSchemas(t *testing.T) {
// deployment-controlled. A cap that cannot be raised without a rebuild is
// the situation this replaced.
func TestChatGenerateOptionsHonoursConfiguredMaxSteps(t *testing.T) {
options, err := chatGenerateOptions(nil, tools.NewRegistry(), 250)
options, err := chatGenerateOptions(nil, tools.NewRegistry(), 250, agentexec.ToolLimits{}, nil)
if err != nil {
t.Fatal(err)
}
+21 -18
View File
@@ -39,6 +39,7 @@ type telegramSetup struct {
Personas *gateway.PersonaRegistry
ChatTasks gateway.TaskCreator
ChatController gateway.TaskController
ChatTaskLister gateway.ChatTaskLister
DefaultChatIdentity string
SessionStore gateway.SessionStore
Updates telegram.UpdateService
@@ -164,20 +165,26 @@ func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSe
router.InitSessions(sessionStore)
configureTaskCommands(router, s.ChatTasks, s.ChatController, s.DefaultChatIdentity)
router.LLM, router.LLMStream = makeChatLLMResponder(ctx, tg.Name(), s, sessionStore, router)
router.LLM, router.LLMStream = makeChatLLMResponder(tg.Name(), s, sessionStore, router)
return router
}
func makeChatLLMResponder(ctx context.Context, channel string, s telegramSetup, sessionStore gateway.SessionStore, router *gateway.Router) (gateway.LLMResponder, gateway.LLMStreamResponder) {
func makeChatLLMResponder(
channel string,
s telegramSetup,
sessionStore gateway.SessionStore,
router *gateway.Router,
) (gateway.LLMResponder, gateway.LLMStreamResponder) {
if s.LLM == nil {
return nil, nil
}
// Built once: these carry this gateway's identity, which does not change
// between turns, so there is nothing per-turn to rebuild.
taskTools := gateway.TaskTools(s.ChatTaskLister, s.ChatTasks, s.DefaultChatIdentity)
respond := func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) {
// Resolve through the router's session tracker, not a local
// channel-keyed helper. They agree until /new, /branch or /resume
// assigns a session a generated id; from then on the commands and
// the conversation address different histories, so /new would report
// history cleared while the next turn still saw all of it.
// Use the router's session tracker, not a local channel-keyed helper.
// Commands like /new, /branch, or /resume may reassign a session to a
// generated ID; after that, only the router sees the correct history.
sk, err := router.ResolveSessionKey(ctx, msg)
if err != nil {
return "", fmt.Errorf("resolve chat session: %w", err)
@@ -186,12 +193,10 @@ func makeChatLLMResponder(ctx context.Context, channel string, s telegramSetup,
if err != nil {
return "", fmt.Errorf("load chat history: %w", err)
}
// A redelivered update must not be answered twice. The store already
// refuses to store the message again, but that alone changed
// nothing here: the model still ran, costing a second billed turn,
// running any tool side effects again, and appending a second reply
// (replies carry no upstream reference, so they always append).
// Replaying the answer we already gave is both cheaper and correct.
// Deduplicate redelivered updates: the store already rejects the
// duplicate message, but that alone doesn't prevent a second model
// call with its cost and side effects. Replaying the prior reply
// is both cheaper and correct.
if prior := gateway.PriorReply(history, sk, msg.SourceID, s.Cfg.BotUser); prior != "" {
s.Log.Info("replaying the reply to a redelivered message",
"session", sk, "source_id", msg.SourceID)
@@ -204,10 +209,8 @@ func makeChatLLMResponder(ctx context.Context, channel string, s telegramSetup,
return "", fmt.Errorf("save inbound chat message: %w", err)
}
history = append(history, msg)
// Chat turns were entirely unlogged, so a report of lost context had
// no evidence to check against: session id, history depth and the
// resolved persona are the three things needed to tell "wrong
// session" from "history not loaded".
// Log session id, history depth, and resolved persona to
// distinguish "wrong session" bugs from "history not loaded".
s.Log.Info("chat turn",
"session", sk,
"channel", msg.ChannelID,
@@ -222,7 +225,7 @@ func makeChatLLMResponder(ctx context.Context, channel string, s telegramSetup,
compressed = append(compressed, gateway.CompressedMessage{Role: role, Content: h.Text})
}
view := gateway.CompressHistory(compressed, gateway.DefaultCompressionConfig())
options, err := chatGenerateOptions(nil, s.ToolReg, s.Cfg.Chat.MaxSteps)
options, err := chatGenerateOptions(nil, s.ToolReg, s.Cfg.Chat.MaxSteps, toolLimits(s.Cfg), taskTools)
if err != nil {
return "", fmt.Errorf("build chat tools: %w", err)
}