Six defects, four of them introduced by the fixes indbacb96,44d43ef,d5e016e,de1a899and4d44ef7. 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 bugd5e016eclaimed 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.
127 lines
4.1 KiB
Go
127 lines
4.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// /topic off copied the thread's active session onto the flat key. When
|
|
// nothing was cached for that thread it copied the empty string -- and
|
|
// resolve treats map presence as authoritative, so it returned "" from then
|
|
// on, permanently. Every later message in the chat persisted under session
|
|
// ID "", history was shared with any other poisoned chat, and the turn-lane
|
|
// key became "" so /stop in one chat cancelled a turn in another.
|
|
//
|
|
// Reachable as a first message on the webhook and email channels, which call
|
|
// Route directly without resolving a session first.
|
|
func TestTopicOffDoesNotPoisonTheCache(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
// preResolve simulates a channel that resolves before routing (as
|
|
// telegram's submitTurn does) versus one that does not.
|
|
preResolve bool
|
|
}{
|
|
{name: "no session resolved yet"},
|
|
{name: "session already resolved", preResolve: true},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := NewSessionStoreMemory("test-node")
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
|
|
r := NewRouter(nil, nil, "telegram")
|
|
r.InitSessions(store)
|
|
r.Identity = "archie"
|
|
|
|
var want string
|
|
if tc.preResolve {
|
|
id, err := r.sessionTracker.resolve(ctx, "telegram", "", "chat-x", "")
|
|
if err != nil {
|
|
t.Fatalf("resolve: %v", err)
|
|
}
|
|
want = id
|
|
}
|
|
|
|
if _, err := r.Route(ctx, Message{Text: "/topic off", ChannelID: "chat-x"}); err != nil {
|
|
t.Fatalf("Route(/topic off): %v", err)
|
|
}
|
|
|
|
got, err := r.sessionTracker.resolve(ctx, "telegram", "", "chat-x", "")
|
|
if err != nil {
|
|
t.Fatalf("resolve after /topic off: %v", err)
|
|
}
|
|
if got == "" {
|
|
t.Fatal("resolve returned an empty session ID: the cache was poisoned " +
|
|
"and every later message would persist under session \"\"")
|
|
}
|
|
if tc.preResolve && got != want {
|
|
t.Errorf("resolve = %q, want the session that was already active %q", got, want)
|
|
}
|
|
|
|
// Sticky check: the damage previously survived repeat calls
|
|
// because cacheActive also returned the empty existing value.
|
|
again, err := r.sessionTracker.resolve(ctx, "telegram", "", "chat-x", "")
|
|
if err != nil {
|
|
t.Fatalf("resolve (repeat): %v", err)
|
|
}
|
|
if again != got {
|
|
t.Errorf("resolve is unstable: %q then %q", got, again)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// An empty session ID is never a meaningful value to cache. Rejecting it at
|
|
// the setter means no caller can poison the map, whatever it computes.
|
|
func TestSetActiveRejectsAnEmptySession(t *testing.T) {
|
|
store := NewSessionStoreMemory("test-node")
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
tr := newSessionTracker(store)
|
|
|
|
tr.setActive("chat-1", "", "real-session")
|
|
tr.setActive("chat-1", "", "")
|
|
|
|
if got := tr.getActive("chat-1", ""); got != "real-session" {
|
|
t.Errorf("getActive = %q, want the previous value %q: an empty set must not "+
|
|
"overwrite a real session", got, "real-session")
|
|
}
|
|
|
|
tr.setActive("chat-2", "", "")
|
|
if got := tr.getActive("chat-2", ""); got != "" {
|
|
t.Errorf("getActive = %q, want empty", got)
|
|
}
|
|
// The key must be absent, not present-and-empty: resolve treats presence
|
|
// as authoritative.
|
|
id, err := tr.resolve(context.Background(), "telegram", "", "chat-2", "")
|
|
if err != nil {
|
|
t.Fatalf("resolve: %v", err)
|
|
}
|
|
if id == "" {
|
|
t.Fatal("resolve returned empty: an empty setActive left a poisoned entry")
|
|
}
|
|
}
|
|
|
|
// /branch with no active session sliced an empty parent ID for its default
|
|
// title and panicked. Telegram resolves a session before routing, so it never
|
|
// reached this -- but the email and webhook channels call Route directly, and
|
|
// the email path runs in a goroutine with no recover, so one message took the
|
|
// daemon down.
|
|
func TestBranchWithNoActiveSessionDoesNotPanic(t *testing.T) {
|
|
store := NewSessionStoreMemory("test-node")
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
|
|
r := NewRouter(nil, nil, "email")
|
|
r.InitSessions(store)
|
|
r.Identity = "archie"
|
|
|
|
reply, err := r.Route(context.Background(), Message{Text: "/branch", ChannelID: "inbox-1"})
|
|
if err != nil {
|
|
t.Fatalf("Route(/branch): %v", err)
|
|
}
|
|
if reply == "" {
|
|
t.Fatal("no reply; the user is told nothing")
|
|
}
|
|
}
|