diff --git a/cmd/archied/main.go b/cmd/archied/main.go index 246edb9e5..cbcc4c960 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -352,27 +352,9 @@ func run() int { var overlayStore *overlay.Store var bootOverlayErr atomic.Pointer[string] if !skipOverlay { - overlayStore, err = overlay.Open(ctx, configDBPath(doc.Config.DBPath)) - if err != nil { - reason := err.Error() - bootOverlayErr.Store(&reason) - log.Error("config overlay unavailable; booting on file config alone", "path", configDBPath(doc.Config.DBPath), "err", err) - } else { + overlayStore, doc = bootConfigOverlay(ctx, loader, doc, &bootOverlayErr, log) + if overlayStore != nil { defer overlayStore.Close() - if overrides, err := overlayStore.Snapshot(ctx); err != nil { - reason := err.Error() - bootOverlayErr.Store(&reason) - log.Error("config overlay unreadable; booting on file config alone", "err", err) - } else if len(overrides) > 0 { - applied, err := loader.ApplyOverlay(doc, overrides) - if err != nil { - reason := err.Error() - bootOverlayErr.Store(&reason) - log.Error("config overlay rejected by validation; booting on file config alone", "err", err) - } else { - doc = applied - } - } } } cfg := doc.Config @@ -590,7 +572,7 @@ func run() int { DefaultChatIdentity: defaultChatIdentity, SessionStore: chatSessionStore, Bus: bus, Log: log, } - webRouter.LLM, webRouter.LLMStream = makeChatLLMResponder("web", webSetup, chatSessionStore, webRouter) + webRouter.LLM, webRouter.LLMStream = makeChatLLMResponder(ctx, "web", webSetup, chatSessionStore, webRouter) webRouter.Titles = newChatTitleGenerator(webSetup) webRouter.Log = log web.Chat = &webui.ChatService{ @@ -1247,6 +1229,54 @@ func run() int { return 0 } +// bootConfigOverlay layers the runtime config overlay over the resolved +// file config. Every failure degrades rather than aborts -- the daemon +// boots on file config alone and the reason is recorded in bootOverlayErr, +// which /api/config surfaces as the reload status. The returned store is +// nil only when Open failed; a store that opened but could not be read +// stays open and non-nil so the dashboard PATCH path and reload wiring +// keep exactly the behaviour of the original inline block. +func bootConfigOverlay( + ctx context.Context, + loader *configuration.Loader, + doc *configuration.Document, + bootOverlayErr *atomic.Pointer[string], + log *slog.Logger, +) (*overlay.Store, *configuration.Document) { + store, err := overlay.Open(ctx, configDBPath(doc.Config.DBPath)) + if err != nil { + recordBootOverlayError(bootOverlayErr, log, err, + "config overlay unavailable; booting on file config alone", + "path", configDBPath(doc.Config.DBPath)) + return nil, doc + } + overrides, err := store.Snapshot(ctx) + if err != nil { + recordBootOverlayError(bootOverlayErr, log, err, + "config overlay unreadable; booting on file config alone") + return store, doc + } + if len(overrides) == 0 { + return store, doc + } + applied, err := loader.ApplyOverlay(doc, overrides) + if err != nil { + recordBootOverlayError(bootOverlayErr, log, err, + "config overlay rejected by validation; booting on file config alone") + return store, doc + } + return store, applied +} + +// recordBootOverlayError stores the degrade reason for /api/config and +// logs it, so the operator sees both where they are looking. +func recordBootOverlayError(p *atomic.Pointer[string], log *slog.Logger, err error, msg string, args ...any) { + reason := err.Error() + p.Store(&reason) + args = append(args, "err", err) + log.Error(msg, args...) +} + func persistAndBroadcastEvents(sink *events.Sub, st store.TaskStore, web *webui.Server, log *slog.Logger) { for e := range sink.C { if e.ID == 0 { diff --git a/cmd/archied/task_actions_test.go b/cmd/archied/task_actions_test.go index e599c70ef..af4991913 100644 --- a/cmd/archied/task_actions_test.go +++ b/cmd/archied/task_actions_test.go @@ -57,32 +57,9 @@ func TestDashboardAndChatAgreeOnTerminalStates(t *testing.T) { } if tc.viaChat { - controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{ - taskByID: st.TaskByID, - requeue: st.Requeue, - transition: st.Transition, - }) - var actErr error - if tc.action == "approve" { - actErr = controller.Approve(ctx, task.ID, "reviewer") - } else { - actErr = controller.Cancel(ctx, task.ID, "reviewer") - } - if actErr != nil { - t.Fatalf("chat %s: %v", tc.action, actErr) - } + runChatAction(t, ctx, st, tc.action, task.ID) } else { - srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{MaxRetries: 3})} - req := httptest.NewRequestWithContext(ctx, http.MethodPost, - "/api/tasks/"+strconv.FormatInt(task.ID, 10)+"/action", - bytes.NewBufferString(`{"action":"`+tc.action+`"}`)) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Archie-CSRF", "1") - w := httptest.NewRecorder() - srv.Handler().ServeHTTP(w, req) - if w.Code != http.StatusOK { - t.Fatalf("dashboard %s: status %d, body %s", tc.action, w.Code, w.Body) - } + runDashboardAction(t, ctx, st, tc.action, task.ID) } got, err := st.TaskByID(ctx, task.ID) @@ -96,3 +73,41 @@ func TestDashboardAndChatAgreeOnTerminalStates(t *testing.T) { }) } } + +// runChatAction performs the approve/cancel action through the chat +// controller path and fails the test on error. +func runChatAction(t *testing.T, ctx context.Context, st store.TaskStore, action string, taskID int64) { + t.Helper() + controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{ + taskByID: st.TaskByID, + requeue: st.Requeue, + transition: st.Transition, + }) + var err error + if action == "approve" { + err = controller.Approve(ctx, taskID, "reviewer") + } else { + err = controller.Cancel(ctx, taskID, "reviewer") + } + if err != nil { + t.Fatalf("chat %s: %v", action, err) + } +} + +// runDashboardAction performs the approve/cancel action through the +// dashboard's HTTP handler and fails the test when the request is +// rejected. +func runDashboardAction(t *testing.T, ctx context.Context, st store.TaskStore, action string, taskID int64) { + t.Helper() + srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{MaxRetries: 3})} + req := httptest.NewRequestWithContext(ctx, http.MethodPost, + "/api/tasks/"+strconv.FormatInt(taskID, 10)+"/action", + bytes.NewBufferString(`{"action":"`+action+`"}`)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Archie-CSRF", "1") + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Fatalf("dashboard %s: status %d, body %s", action, w.Code, w.Body) + } +} diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index 6f0e95c08..11aa9b547 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -102,7 +102,7 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o s.Log.Error("telegram conversation store is not configured") return nil, false } - router := buildTelegramRouter(tg, s, sessionStore) + router := buildTelegramRouter(ctx, tg, s, sessionStore) return func() { if s.ChannelManager != nil { s.ChannelManager.MarkStarting("telegram") @@ -175,7 +175,7 @@ func conversationDBPath(taskDBPath string) string { return taskDBPath + "-conversations.sqlite" } -func buildTelegramRouter(tg *telegram.Gateway, s telegramSetup, sessionStore gateway.SessionStore) *gateway.Router { +func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSetup, sessionStore gateway.SessionStore) *gateway.Router { // The router is built before the responder so the responder can resolve // sessions through it. Both orderings work at runtime -- the responder is // a closure -- but this way the dependency is visible rather than @@ -189,7 +189,7 @@ func buildTelegramRouter(tg *telegram.Gateway, s telegramSetup, sessionStore gat configureTaskCommands(router, s.ChatTasks, s.ChatController, s.DefaultChatIdentity) if s.LLM != nil { - turnRunner := newChatTurnRunner(tg.Name(), s, sessionStore, router) + turnRunner := newChatTurnRunner(ctx, tg.Name(), s, sessionStore, router) router.LLM = turnRunner.Respond router.LLMStream = turnRunner.RespondStream } @@ -197,6 +197,7 @@ func buildTelegramRouter(tg *telegram.Gateway, s telegramSetup, sessionStore gat } func makeChatLLMResponder( + ctx context.Context, channel string, s telegramSetup, sessionStore gateway.SessionStore, @@ -205,11 +206,12 @@ func makeChatLLMResponder( if s.LLM == nil { return nil, nil } - runner := newChatTurnRunner(channel, s, sessionStore, router) + runner := newChatTurnRunner(ctx, channel, s, sessionStore, router) return runner.Respond, runner.RespondStream } func newChatTurnRunner( + ctx context.Context, channel string, s telegramSetup, sessionStore gateway.SessionStore, @@ -232,7 +234,7 @@ func newChatTurnRunner( Operator: cfg.Chat.Operator, Log: s.Log, }) - if err := runner.Recover(context.Background()); err != nil && s.Log != nil { + if err := runner.Recover(ctx); err != nil && s.Log != nil { s.Log.Error("recover chat turns", "channel", channel, "err", err) } return runner