Files
archie/internal/infrastructure/sessioncurator/adapter_test.go
T
sam 4877e16379 fix(sessioncurator): address curator memory by the deployment's bot user
The curator derived the agent half of its agent-user scope from the
session record's BotUser, which is the router's session-resolution match
key: empty when no [[identities]] are configured (the shipped default),
identities[0].Name when they are. The engine validates the scope and
refuses an empty agent, so every pass in a single-identity deployment
failed on its first session and wrote nothing, forever; in a
multi-identity deployment the write landed at an agent no chat turn
reads.

A turn addresses its own memory with cfg.BotUser, so the composition now
hands the curator that same value through NewAdapter. The record's
BotUser stays exactly what it is, the match key resolution compares
against.

A session the pass cannot address is now skipped with a memory.skipped
action naming why, at the point the scope is produced, instead of
aborting the pass and dropping the actions already collected: one
session with no address must not starve the rest. Real infrastructure
failures -- an unregistered engine, an unreadable store -- remain pass
errors.
2026-09-22 01:57:54 +10:00

148 lines
4.8 KiB
Go

package sessioncurator
import (
"context"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/gateway"
)
func newTestStore(t *testing.T) gateway.SessionStore {
t.Helper()
s := gateway.NewSessionStoreMemory()
t.Cleanup(func() { _ = s.Close() })
return s
}
func TestAdapterRecentSessionsFiltersByActivity(t *testing.T) {
t.Parallel()
store := newTestStore(t)
ctx := context.Background()
old := gateway.SessionContext{SessionID: "old", LastActiveAt: time.Unix(100, 0)}
recent := gateway.SessionContext{SessionID: "recent", LastActiveAt: time.Unix(2000, 0)}
if err := store.Save(ctx, old); err != nil {
t.Fatalf("Save(old) = %v", err)
}
if err := store.Save(ctx, recent); err != nil {
t.Fatalf("Save(recent) = %v", err)
}
a := NewAdapter(store, "archie-bot")
got, err := a.RecentSessions(ctx, time.Unix(1000, 0))
if err != nil {
t.Fatalf("RecentSessions() = %v, want nil", err)
}
if len(got) != 1 || got[0].ID != "recent" {
t.Fatalf("RecentSessions() = %v, want only 'recent'", got)
}
}
func TestAdapterMessagesReadsRoleFromRecords(t *testing.T) {
t.Parallel()
store := newTestStore(t)
ctx := context.Background()
sess := gateway.SessionContext{SessionID: "s1", LastActiveAt: time.Unix(1000, 0)}
if err := store.Save(ctx, sess); err != nil {
t.Fatalf("Save() = %v", err)
}
// Roles are set by whoever produced the message; the adapter reads them
// from the records, needing no bot identity of its own.
for _, m := range []messaging.Message{
{Sender: "user123", Role: messaging.RoleUser, Text: "hello", At: time.Unix(1, 0)},
{Sender: "archie", Role: messaging.RoleAssistant, Text: "hi there", At: time.Unix(2, 0)},
} {
if err := store.SaveMessage(ctx, "s1", m); err != nil {
t.Fatalf("SaveMessage(%v) = %v", m, err)
}
}
// A hand-built record without a role reads as a user message.
if err := store.SaveMessage(ctx, "s1", messaging.Message{Text: "bare", At: time.Unix(3, 0)}); err != nil {
t.Fatalf("SaveMessage(bare) = %v", err)
}
a := NewAdapter(store, "archie-bot")
got, err := a.Messages(ctx, "s1", 10)
if err != nil {
t.Fatalf("Messages() = %v, want nil", err)
}
if len(got) != 3 {
t.Fatalf("Messages() = %v, want 3", got)
}
if got[0].Role != "user" || got[0].Content != "hello" {
t.Errorf("Messages()[0] = %+v, want user/hello", got[0])
}
if got[1].Role != "assistant" || got[1].Content != "hi there" {
t.Errorf("Messages()[1] = %+v, want assistant/hi there", got[1])
}
if got[2].Role != "user" || got[2].Content != "bare" {
t.Errorf("Messages()[2] = %+v, want user/bare", got[2])
}
}
// TestAdapterRecentSessionsCarriesAgentID pins that the agent a session's
// memory is addressed to comes from the composition, not from the record's
// own BotUser: BotUser is the router's session-resolution key
// (gateway/session_commands.go), which is empty in a single-identity
// deployment -- an addressing component, never a match key.
func TestAdapterRecentSessionsCarriesAgentID(t *testing.T) {
t.Parallel()
store := newTestStore(t)
ctx := context.Background()
sess := gateway.SessionContext{
SessionID: "s1",
Source: messaging.SessionSource{Platform: "telegram", BotUser: "primary", ChannelID: "chat-1"},
LastActiveAt: time.Unix(2000, 0),
}
if err := store.Save(ctx, sess); err != nil {
t.Fatalf("Save() = %v", err)
}
got, err := NewAdapter(store, "archie-bot").RecentSessions(ctx, time.Unix(1000, 0))
if err != nil {
t.Fatalf("RecentSessions() = %v, want nil", err)
}
if len(got) != 1 || got[0].AgentID != "archie-bot" {
t.Fatalf("RecentSessions() = %+v, want one session whose AgentID is the composition's %q", got, "archie-bot")
}
}
// TestAdapterMessagesCarriesSenderID pins that the channel-native participant
// identifier survives the store-to-curator hop, and stays empty for a message
// whose channel offers none.
func TestAdapterMessagesCarriesSenderID(t *testing.T) {
t.Parallel()
store := newTestStore(t)
ctx := context.Background()
if err := store.Save(ctx, gateway.SessionContext{SessionID: "s1", LastActiveAt: time.Unix(1000, 0)}); err != nil {
t.Fatalf("Save() = %v", err)
}
for _, m := range []messaging.Message{
{Sender: "alice", SenderID: "u-42", Role: messaging.RoleUser, Text: "hello", At: time.Unix(1, 0)},
{Sender: "archie", Role: messaging.RoleAssistant, Text: "hi", At: time.Unix(2, 0)},
} {
if err := store.SaveMessage(ctx, "s1", m); err != nil {
t.Fatalf("SaveMessage(%v) = %v", m, err)
}
}
got, err := NewAdapter(store, "archie-bot").Messages(ctx, "s1", 10)
if err != nil {
t.Fatalf("Messages() = %v, want nil", err)
}
if len(got) != 2 {
t.Fatalf("Messages() = %+v, want 2", got)
}
if got[0].SenderID != "u-42" {
t.Errorf("Messages()[0].SenderID = %q, want %q", got[0].SenderID, "u-42")
}
if got[1].SenderID != "" {
t.Errorf("Messages()[1].SenderID = %q, want empty", got[1].SenderID)
}
}