Files
archie/internal/gateway/branch_identity_test.go
T
sam e3b1a859a8 feat(gateway): flip channel boundary onto messaging.Message
Replaced gateway.Message as the currency at the channel-facing boundary
with the canonical internal/domain/messaging.Message. Channel adapters
now build the record directly (RoleUser, ConversationID, Sender) and the
Router hands it back unchanged, so nothing converts between two shapes in
process. gateway.Message is deleted; the dashboard route that had no home
in the record lives in a new Inbound envelope (Message + transport-only
Page), which reaches the system prompt and is never persisted.

Role is now set at construction instead of derived at the store boundary,
and the assistant reply is a direct messaging.Message (RoleAssistant,
Sender=botUser). The gRPC proto is unchanged: pb.Message still carries no
role, and both sides keep deriving it from the owning session's bot
identity via gateway.RoleForSender (ToStoredMessage/FromStoredMessage are
deleted).

Adds tests that the old shape could not express: Page reaches the system
prompt but not the persisted record, and a channel-built Inbound round-trips
with the Role the channel set.

Refs: archie-core-9g5e
2026-09-09 01:05:36 +10:00

104 lines
3.5 KiB
Go

package gateway
import (
"context"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// A branch copies the parent's history into a new session. Those copies used
// to keep the parent's canonical MessageIDs, because saveMessageLocked
// honours an incoming ID and handleBranch fed it messages read straight back
// from the parent. newMessageID's own doc claims the opposite -- "the same
// upstream message copied into a branch gets its own identity" -- and the
// architecture requires exact branch-point correlation on stable MessageIDs,
// which is ambiguous when three sessions hold the same ID.
//
// It also defeats dedup in the child: redelivering an upstream message that
// was inherited appends, because the inherited copy carries an ID derived
// from the parent's session rather than the child's.
func TestBranchGivesInheritedMessagesTheirOwnIdentity(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
r := NewRouter(nil, nil, "telegram")
r.InitSessions(store)
r.Identity = "archie"
parentID, err := r.sessionTracker.resolve(ctx, "telegram", "archie", "chat-1", "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
for i, text := range []string{"one", "two", "three"} {
if err := store.SaveMessage(ctx, parentID, messaging.Message{
Sender: "alice", Role: messaging.RoleUser, Text: text, SourceID: "tg-" + text, At: at(dur(i)),
}); err != nil {
t.Fatalf("SaveMessage: %v", err)
}
}
parentMsgs, err := store.RecentMessages(ctx, parentID, 100)
if err != nil {
t.Fatalf("RecentMessages(parent): %v", err)
}
reply, err := r.Route(ctx, inbound("chat-1", "/branch side-quest"))
if err != nil {
t.Fatalf("Route(/branch): %v", err)
}
if !strings.Contains(reply, "Branched") {
t.Fatalf("reply = %q, want a branch confirmation", reply)
}
childID := r.sessionTracker.getActive("chat-1", "")
if childID == parentID {
t.Fatal("branch did not switch the active session")
}
childMsgs, err := store.RecentMessages(ctx, childID, 100)
if err != nil {
t.Fatalf("RecentMessages(child): %v", err)
}
if len(childMsgs) != len(parentMsgs) {
t.Fatalf("child has %d messages, want the parent's %d", len(childMsgs), len(parentMsgs))
}
parentIDs := make(map[string]struct{}, len(parentMsgs))
for _, m := range parentMsgs {
parentIDs[string(m.ID)] = struct{}{}
}
for _, m := range childMsgs {
if _, shared := parentIDs[string(m.ID)]; shared {
t.Errorf("inherited %q kept the parent's MessageID %q: two sessions "+
"now claim one canonical identity", m.Text, m.ID)
}
if m.SourceID == "" {
t.Errorf("inherited %q lost its SourceID; upstream correlation is gone", m.Text)
}
if want := CanonicalMessageID(childID, m.SourceID); string(m.ID) != want {
t.Errorf("inherited %q: MessageID = %q, want the child-scoped %q",
m.Text, m.ID, want)
}
}
// Dedup must work in the child on its own terms.
if err := store.SaveMessage(ctx, childID, messaging.Message{
Sender: "alice", Role: messaging.RoleUser, Text: "two", SourceID: "tg-two", At: at(dur(1)),
}); err != nil {
t.Fatalf("redeliver into child: %v", err)
}
after, err := store.MessageCount(ctx, childID)
if err != nil {
t.Fatalf("MessageCount: %v", err)
}
if after != len(childMsgs) {
t.Errorf("redelivery into the branch appended: count = %d, want %d",
after, len(childMsgs))
}
})
}
}