Files
archie/internal/gateway/session_resolve_test.go
T
sam fa6f4aef5f fix(gateway,daemon): repair regressions found by the adversarial review
Six defects, four of them introduced by the fixes in dbacb96, 44d43ef,
d5e016e, de1a899 and 4d44ef7. This is the third round in this codebase
where a round of fixes broke something the next round caught.

SANDBOX SILENTLY DROPPED. startContainers degrades a failed container
pool to nil, and the daemon's only test was `if ContainerPool != nil`.
So `containers.enabled = true` plus a missing agent image ran every
task's agent loop in-process, with the daemon's own authority and the
host filesystem -- the one thing [containers] exists to prevent -- while
logging only that containers were disabled. Nil now means two things and
they are distinguished: SandboxRequired parks the task instead. The
daemon still stays up; chat and the dashboard are unaffected.

SUMMARY AT THE WRONG END OF HISTORY. compressedHistory returns [head,
summary, tail], but ordering is by timestamp, not slice position. The
retained messages keep their original timestamps -- that is what
preserves their identity -- so the summary, being new, was clamped to
now and sorted after the tail it was meant to precede. The model read
the recent conversation and was only then told earlier context had been
summarised. Confirmed on both backends: index 23 of 24.

The monotonic clamp belongs to the append path, where an inbound message
must not sort ahead of the one that provoked it. ReplaceMessages is a
rewrite primitive and now honours explicit placement, and the summary
takes a timestamp inside the span it replaces.

A COMPRESSION SUMMARY ANSWERED A QUESTION. Following from the above,
PriorReply saw a bot-attributed record immediately after an unanswered
message and replayed it as the answer: a redelivered question was
"answered" with the compression banner and the model never ran. The
user's question was silently swallowed.

RECENCY STILL DIVERGED. sessionRecency preferred LastActiveAt whenever
set; SQLite used MAX(last_active_at, created_at). Those agree only when
LastActiveAt is the later of the two, so a clock step or an older record
made the caller and the store order the same sessions differently.

LASTACTIVEAT WAS NEVER UPDATED. Touch had no caller, so "resolve the
most recently active session" meant the most recently created one -- an
operator who moved to an older session with /topic and worked there was
dropped back into the newer, abandoned one on restart, which is the bug
d5e016e claimed to fix, with the direction reversed.

/branch WITH NO ACTIVE SESSION PANICKED, slicing an empty parent ID for
its default title. Pre-existing, but reachable: email and webhook call
Route directly without resolving first, and the email path runs in a
goroutine with no recover, so one message took the daemon down.

The sandbox guard has no test. The honest reason is that exercising it
needs a daemon with a forge and worktree the current harness does not
build, and a test that re-states the condition proves nothing -- which
is the same false confidence that let these regressions through.
2026-08-05 04:24:16 +10:00

294 lines
8.5 KiB
Go

package gateway
import (
"context"
"testing"
"time"
)
// resolve claims to find "the most recent session matching this thread and
// bot" but took whichever session the store happened to return first. The
// tracker cache is in-memory, so every daemon restart re-resolves from the
// store -- and dropped the operator back into an arbitrary older session,
// answering against pre-/new history. LastActiveAt was populated on every
// record and never consulted.
func TestResolvePicksTheMostRecentSession(t *testing.T) {
// Session IDs are chosen so that ordering by ID disagrees with ordering
// by recency: a store that returns document order hands back "aaa" or
// "zzz" first depending on direction, and neither is the answer.
tests := []struct {
name string
sessions []struct {
id string
active time.Duration
}
want string
}{
{
name: "newest sorts last by id",
sessions: []struct {
id string
active time.Duration
}{
{"aaa-old", 0},
{"zzz-new", time.Hour},
},
want: "zzz-new",
},
{
name: "newest sorts first by id",
sessions: []struct {
id string
active time.Duration
}{
{"zzz-old", 0},
{"aaa-new", time.Hour},
},
want: "aaa-new",
},
{
name: "newest is in the middle",
sessions: []struct {
id string
active time.Duration
}{
{"aaa", 0},
{"mmm", 2 * time.Hour},
{"zzz", time.Hour},
},
want: "mmm",
},
}
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
for _, s := range tc.sessions {
if err := store.Save(ctx, SessionContext{
SessionID: s.id,
Source: SessionSource{
Platform: "telegram",
BotUser: "archie",
ChannelID: "chat-1",
},
CreatedAt: base.Add(s.active),
LastActiveAt: base.Add(s.active),
}); err != nil {
t.Fatalf("Save %s: %v", s.id, err)
}
}
// A cold tracker, as after a restart.
tr := newSessionTracker(store)
got, err := tr.resolve(ctx, "telegram", "archie", "chat-1", "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
if got != tc.want {
t.Errorf("resolve = %q, want the most recently active %q", got, tc.want)
}
})
}
})
}
}
// Matching is still scoped to the bot and thread: a more recent session
// belonging to another identity or another thread must not be picked up.
func TestResolveIgnoresOtherBotsAndThreads(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
save := func(id, botUser, threadID string, offset time.Duration) {
t.Helper()
if err := store.Save(ctx, SessionContext{
SessionID: id,
Source: SessionSource{
Platform: "telegram",
BotUser: botUser,
ChannelID: "chat-1",
ThreadID: threadID,
},
CreatedAt: base.Add(offset),
LastActiveAt: base.Add(offset),
}); err != nil {
t.Fatalf("Save %s: %v", id, err)
}
}
save("ours", "archie", "", 0)
save("other-bot", "winter", "", 5*time.Hour)
save("other-thread", "archie", "t-9", 9*time.Hour)
tr := newSessionTracker(store)
got, err := tr.resolve(ctx, "telegram", "archie", "chat-1", "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
if got != "ours" {
t.Errorf("resolve = %q, want %q: a newer session for another bot or "+
"thread must not be adopted", got, "ours")
}
})
}
}
// List promises newest-first in its interface doc. It reversed document
// order, which is by session ID, so the dashboard and /sessions listed an
// arbitrary order that looked like recency.
func TestListIsNewestFirst(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
// Creation order deliberately disagrees with ID order.
order := []struct {
id string
offset time.Duration
}{
{"zzz-oldest", 0},
{"aaa-middle", time.Hour},
{"mmm-newest", 2 * time.Hour},
}
for _, o := range order {
if err := store.Save(ctx, SessionContext{
SessionID: o.id,
Source: SessionSource{Platform: "telegram", ChannelID: "chat-1"},
CreatedAt: base.Add(o.offset),
LastActiveAt: base.Add(o.offset),
}); err != nil {
t.Fatalf("Save: %v", err)
}
}
got, err := store.List(ctx)
if err != nil {
t.Fatalf("List: %v", err)
}
want := []string{"mmm-newest", "aaa-middle", "zzz-oldest"}
if len(got) != len(want) {
t.Fatalf("List returned %d sessions, want %d", len(got), len(want))
}
for i, w := range want {
if got[i].SessionID != w {
t.Errorf("List[%d] = %q, want %q (full order %v)",
i, got[i].SessionID, w, sessionIDs(got))
}
}
})
}
}
func sessionIDs(ss []SessionContext) []string {
out := make([]string, 0, len(ss))
for _, s := range ss {
out = append(out, s.SessionID)
}
return out
}
// The two backends must order the same records identically. The Go side used
// to prefer LastActiveAt whenever it was set, while SQLite used
// MAX(last_active_at, created_at); those agree only when LastActiveAt is the
// later of the two. A clock step, or a record written by an older version,
// made the store's own ORDER BY and the caller's pick disagree.
func TestRecencyAgreesWhenLastActivePredatesCreation(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
// X was created later but carries an older LastActiveAt.
save := func(id string, created, active time.Duration) {
t.Helper()
if err := store.Save(ctx, SessionContext{
SessionID: id,
Source: SessionSource{Platform: "telegram", BotUser: "archie", ChannelID: "chat-1"},
CreatedAt: base.Add(created),
LastActiveAt: base.Add(active),
}); err != nil {
t.Fatalf("Save %s: %v", id, err)
}
}
save("x-created-late", 5*time.Hour, -5*time.Hour)
save("y-normal", 3*time.Hour, 3*time.Hour)
got, err := store.GetByChannel(ctx, "telegram", "chat-1")
if err != nil {
t.Fatalf("GetByChannel: %v", err)
}
if len(got) != 2 {
t.Fatalf("got %d sessions, want 2", len(got))
}
if got[0].SessionID != "x-created-late" {
t.Errorf("order = %v, want x-created-late first (its creation is "+
"the later instant)", sessionIDs(got))
}
// The caller's own pick must match the store's order.
tr := newSessionTracker(store)
resolved, err := tr.resolve(ctx, "telegram", "archie", "chat-1", "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
if resolved != got[0].SessionID {
t.Errorf("resolve = %q but the store orders %q first: the caller "+
"and the store disagree", resolved, got[0].SessionID)
}
})
}
}
// Resolving a session is what "the session is in use" means, so it must
// update LastActiveAt. Without it the recency ordering ranked by creation
// time, and an operator who switched to an older session with /topic and
// worked there was dropped back into the newer, abandoned one on restart.
func TestResolveMarksTheSessionActive(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
// "old" is the one the operator is actually working in.
for _, s := range []struct {
id string
offset time.Duration
}{{"old", 0}, {"new", 5 * time.Hour}} {
if err := store.Save(ctx, SessionContext{
SessionID: s.id,
Source: SessionSource{Platform: "telegram", BotUser: "archie", ChannelID: "chat-1"},
CreatedAt: base.Add(s.offset),
LastActiveAt: base.Add(s.offset),
}); err != nil {
t.Fatal(err)
}
}
tr := newSessionTracker(store)
tr.setActive("chat-1", "", "old")
if got, err := tr.resolve(ctx, "telegram", "archie", "chat-1", ""); err != nil || got != "old" {
t.Fatalf("resolve = (%q, %v), want old", got, err)
}
// A cold tracker, as after a restart, must now pick "old".
cold := newSessionTracker(store)
got, err := cold.resolve(ctx, "telegram", "archie", "chat-1", "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
if got != "old" {
t.Errorf("after restart resolve = %q, want %q: working in a session "+
"did not mark it active, so recency still ranked by creation",
got, "old")
}
})
}
}