From 3afbab82017e72e3601e840009091eccba8ddd3c Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 9 Aug 2026 19:47:33 +1000 Subject: [PATCH] refactor(gateway): persist conversations in sqlite --- cmd/archied/main.go | 11 +++++- cmd/archied/telegram_session_store_test.go | 41 ++++++++++++++++++++++ cmd/archied/telegram_setup.go | 18 ++++++---- 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 cmd/archied/telegram_session_store_test.go diff --git a/cmd/archied/main.go b/cmd/archied/main.go index 61a451132..0659312f1 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -347,7 +347,16 @@ func run() int { log.Error("close store", "err", err) } }() - chatSessionStore := makeTelegramSessionStore(telegramSetup{St: st, Cfg: cfg}) + chatSessionStore, err := makeTelegramSessionStore(cfg) + if err != nil { + log.Error("open conversation store", "path", conversationDBPath(cfg.DBPath), "err", err) + return 1 + } + defer func() { + if err := chatSessionStore.Close(); err != nil { + log.Error("close conversation store", "err", err) + } + }() if *requeue > 0 { if err := st.Requeue(context.Background(), *requeue, "manual", ""); err != nil { diff --git a/cmd/archied/telegram_session_store_test.go b/cmd/archied/telegram_session_store_test.go new file mode 100644 index 000000000..bac6849e9 --- /dev/null +++ b/cmd/archied/telegram_session_store_test.go @@ -0,0 +1,41 @@ +package main + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +func TestTelegramConversationStoreUsesFreshSQLiteWithoutReadingTaskLog(t *testing.T) { + taskLogPath := filepath.Join(t.TempDir(), "tasks.nell") + taskLog := []byte("not a SQLite database") + if err := os.WriteFile(taskLogPath, taskLog, 0o600); err != nil { + t.Fatalf("write task log fixture: %v", err) + } + + sessions, err := makeTelegramSessionStore(config.Config{ + DBPath: taskLogPath, + }) + if err != nil { + t.Fatalf("makeTelegramSessionStore: %v", err) + } + t.Cleanup(func() { _ = sessions.Close() }) + + if got, want := fmt.Sprintf("%T", sessions), "*gateway.sqliteSessionStore"; got != want { + t.Fatalf("conversation store type = %q, want %q", got, want) + } + if _, err := os.Stat(conversationDBPath(taskLogPath)); err != nil { + t.Fatalf("stat conversation database: %v", err) + } + gotTaskLog, err := os.ReadFile(taskLogPath) + if err != nil { + t.Fatalf("read task log fixture: %v", err) + } + if !bytes.Equal(gotTaskLog, taskLog) { + t.Fatalf("task log changed during conversation-store cutover: got %q, want %q", gotTaskLog, taskLog) + } +} diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index 63d706331..2f9837ee3 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -18,7 +18,6 @@ import ( "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" - "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" @@ -94,7 +93,8 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o sessionStore := s.SessionStore if sessionStore == nil { - sessionStore = makeTelegramSessionStore(s) + s.Log.Error("telegram conversation store is not configured") + return nil, false } router := buildTelegramRouter(tg, s, sessionStore) return func() { @@ -155,11 +155,15 @@ func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error { } } -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 makeTelegramSessionStore(cfg config.Config) (gateway.SessionStore, error) { + return gateway.OpenSQLiteSessionStore(conversationDBPath(cfg.DBPath)) +} + +// conversationDBPath keeps conversation state separate while db_path still +// names the NellDB task log. The suffix is deterministic so the eventual task +// store cutover can open db_path as SQLite without moving conversation data. +func conversationDBPath(taskDBPath string) string { + return taskDBPath + "-conversations.sqlite" } func buildTelegramRouter(tg *telegram.Gateway, s telegramSetup, sessionStore gateway.SessionStore) *gateway.Router {