fix(gateway): stop /compress destroying history it should have kept

Two defects, both confirmed on the NellDB and SQLite backends.

IDENTITY. applyCompress rebuilt the protected head and tail as bare
Message{From, Text}, so every message that survived a compression was
deleted and re-inserted with a fresh canonical MessageID, no SourceID
and a timestamp of now. A rebuilt message is a different message: after
one compression, redelivery deduplication was broken for the rest of the
session's life, so a Telegram backlog replay after a restart appended
every surviving message a second time -- 24 became 25 on a redelivery
that was a correct no-op beforehand. That made the dedup guarantee
survive only until the first /compress, and violated
messaging-and-work-intake.md:88-90 and 112-114.

CompressHistory now reports which input messages it protected and the
write-back carries those originals through untouched, so only the
summary is a new record. The summary is attributed to the bot rather
than falling through to "user": CompressHistory emits it with role
"system", which has no representation in the store, so the model was
replaying "[Earlier conversation has been summarised...]" as if the
human had typed it.

DISPATCH. Three ways to rewrite a history the operator did not ask to
rewrite, or to fail to rewrite one they did:

- Bare /compress took the preview branch, and the preview closes with
  "Run /compress to apply" -- the documented way to compress could never
  compress, and the failure-safe path was unreachable.
- /compress focus <topic> discarded the topic and ran a full default
  compression. Nothing implements focused summarisation; it now says so
  instead of doing something else irreversibly.
- /compress here (no count) and any typo fell through to a full
  compression. Both now print usage.

compressPreview also read a fixed 200-message window while reporting the
real count, so the two numbers in one sentence described different
conversations and it advised against compressions that were needed.

Refs archie-core-cz8, archie-core-8k8, archie-core-le6
This commit is contained in:
sam committed 2026-08-04 22:11:53 +10:00
1 parent 0d4d045050
commit dbacb96994
4 files changed
+510 -35

No files matched your search

+169
View File
@@ -0,0 +1,169 @@
package gateway
import (
"context"
"strings"
"testing"
)
// /compress dispatch. Every case here is a command an operator can type, and
// the contract is that a command either does what it says or explains itself
// -- it never silently does something else to an irreplaceable history.
func TestCompressCommandDispatch(t *testing.T) {
tests := []struct {
name string
rest string
// wantApplied says the history must actually be rewritten.
wantApplied bool
// wantReply is a fragment the reply must contain.
wantReply string
// wantProtected, when non-zero, is the number of trailing messages
// that must survive alongside the summary.
wantProtected int
}{
{
// The preview's own closing line says "Run /compress to apply".
// Taking the preview branch here made that instruction a loop:
// the documented way to apply compression could never apply it.
name: "bare compress applies",
rest: "",
wantApplied: true,
wantReply: "Compressed:",
wantProtected: DefaultCompressionConfig().ProtectLast,
},
{
name: "--preview does not apply",
rest: "--preview",
wantReply: "preview",
},
{
name: "--dry-run does not apply",
rest: "--dry-run",
wantReply: "preview",
},
{
name: "here N protects N",
rest: "here 5",
wantApplied: true,
wantReply: "Compressed:",
wantProtected: 5,
},
{
// Previously fell through to a full default compression,
// rewriting history from a command that named no count.
name: "here without a count explains itself",
rest: "here",
wantReply: "Usage",
},
{
name: "here with a non-number explains itself",
rest: "here abc",
wantReply: "Usage",
},
{
// focus has no implementation: cfg was never modified, so this
// ran a full default compression and reported success.
name: "focus reports that it is unsupported",
rest: "focus auth",
wantReply: "not supported",
},
{
name: "focus without a topic explains itself",
rest: "focus",
wantReply: "Usage",
},
{
// Previously compressed the whole history.
name: "an unknown argument explains itself",
rest: "asdf",
wantReply: "Usage",
},
}
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, sessionID := routerOn(t, store, "chat-1")
const seeded = 40
seedForCompress(t, store, sessionID, seeded)
reply, err := r.handleCompress(ctx, Message{ChannelID: "chat-1"}, tc.rest)
if err != nil {
t.Fatalf("handleCompress(%q): %v", tc.rest, err)
}
if !strings.Contains(strings.ToLower(reply), strings.ToLower(tc.wantReply)) {
t.Errorf("reply = %q, want it to contain %q", reply, tc.wantReply)
}
count, err := store.MessageCount(ctx, sessionID)
if err != nil {
t.Fatalf("MessageCount: %v", err)
}
if !tc.wantApplied {
if count != seeded {
t.Fatalf("history was rewritten by %q: count = %d, want the original %d",
"/compress "+tc.rest, count, seeded)
}
return
}
if count == seeded {
t.Fatalf("history was not compressed by %q: count is still %d", "/compress "+tc.rest, count)
}
if tc.wantProtected > 0 {
// head + summary + tail
want := DefaultCompressionConfig().ProtectFirst + 1 + tc.wantProtected
if count != want {
t.Errorf("count = %d, want %d (first %d + summary + last %d)",
count, want, DefaultCompressionConfig().ProtectFirst, tc.wantProtected)
}
}
})
}
}
// The preview reports a message count from the store but token figures from a
// fixed window, so on a long history the two halves of one sentence described
// different conversations -- and it advised against a compression that was in
// fact needed.
func TestCompressPreviewReadsTheWholeHistory(t *testing.T) {
ctx := context.Background()
store := NewSessionStoreMemory("test-node")
t.Cleanup(func() { _ = store.Close() })
r, sessionID := routerOn(t, store, "chat-1")
// Enough messages to exceed the old fixed 200-message read, with the
// bulk of the tokens outside that window.
seedForCompress(t, store, sessionID, 40)
for i := range 250 {
if err := store.SaveMessage(ctx, sessionID, Message{
From: "alice", Text: "tiny", SourceID: pad(i), At: at(dur(1000 + i)),
}); err != nil {
t.Fatalf("SaveMessage: %v", err)
}
}
preview, err := r.compressPreview(ctx, sessionID)
if err != nil {
t.Fatalf("compressPreview: %v", err)
}
if strings.Contains(preview, "not needed") {
t.Fatalf("preview says compression is not needed, but applying it compresses: %q", preview)
}
applied, err := r.applyCompress(ctx, sessionID, DefaultCompressionConfig())
if err != nil {
t.Fatalf("applyCompress: %v", err)
}
if !strings.Contains(applied, "Compressed:") {
t.Fatalf("apply did not compress after the preview said it would: %q", applied)
}
}
func pad(i int) string {
return "late-" + string(rune('a'+i%26)) + string(rune('a'+(i/26)%26)) + string(rune('a'+(i/676)%26))
}
+236
View File
@@ -0,0 +1,236 @@
package gateway
import (
"context"
"fmt"
"strings"
"testing"
)
// compressBackends is the pair every compression test runs against. The two
// stores must agree here for the same reason the conformance suite exists:
// identity is a store-level contract, and /compress is the one production
// caller that rewrites history wholesale.
func compressBackends() map[string]func(t *testing.T) SessionStore {
return map[string]func(t *testing.T) SessionStore{
"nell": func(t *testing.T) SessionStore {
s := NewSessionStoreMemory("test-node")
t.Cleanup(func() { _ = s.Close() })
return s
},
"sqlite": func(t *testing.T) SessionStore {
s, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatalf("NewSQLiteSessionStoreMemory: %v", err)
}
t.Cleanup(func() { _ = s.Close() })
return s
},
}
}
// routerOn builds a Router backed by store with an active session, and
// returns the session ID.
func routerOn(t *testing.T, store SessionStore, channelID string) (*Router, string) {
t.Helper()
r := NewRouter(nil, nil, "telegram")
r.InitSessions(store)
r.Identity = "archie"
sessionID, err := r.sessionTracker.resolve(context.Background(), "telegram", "archie", channelID, "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
return r, sessionID
}
// seedForCompress fills a session with enough large messages to trip the
// default threshold, each carrying an upstream SourceID as Telegram does.
func seedForCompress(t *testing.T, store SessionStore, sessionID string, n int) []Message {
t.Helper()
// Large enough that n messages exceed 50% of a 128k context window
// (tokenEstimate is len/4, so n*bodyLen/4 must clear 64k).
body := make([]byte, 8000)
for i := range body {
body[i] = 'x'
}
for i := range n {
m := Message{
From: "alice",
Text: fmt.Sprintf("m%d %s", i, body),
SourceID: fmt.Sprintf("tg-%d", i),
At: at(dur(i)),
}
if err := store.SaveMessage(context.Background(), sessionID, m); err != nil {
t.Fatalf("SaveMessage %d: %v", i, err)
}
}
stored, err := store.RecentMessages(context.Background(), sessionID, n)
if err != nil {
t.Fatalf("RecentMessages: %v", err)
}
if len(stored) != n {
t.Fatalf("seeded %d messages, store has %d", n, len(stored))
}
return stored
}
// A message that survives compression is the same message. Rebuilding the
// protected head and tail as fresh records re-mints their canonical
// MessageID, drops the upstream SourceID and resets the timestamp, which
// silently breaks redelivery dedup for the rest of the session's life.
//
// Architecture: messaging-and-work-intake.md lines 88-90 (immutable
// application-generated MessageID) and 112-114 (immutable records).
func TestCompressPreservesRetainedMessageIdentity(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
r, sessionID := routerOn(t, store, "chat-1")
before := seedForCompress(t, store, sessionID, 40)
byText := make(map[string]Message, len(before))
for _, m := range before {
byText[m.Text] = m
}
if _, err := r.applyCompress(ctx, sessionID, DefaultCompressionConfig()); err != nil {
t.Fatalf("applyCompress: %v", err)
}
after, err := store.RecentMessages(ctx, sessionID, 1000)
if err != nil {
t.Fatalf("RecentMessages: %v", err)
}
retained := 0
for _, m := range after {
orig, ok := byText[m.Text]
if !ok {
continue // the summary, which is legitimately new
}
retained++
if m.MessageID != orig.MessageID {
t.Errorf("retained %q: MessageID = %q, want the original %q",
shortText(m.Text), m.MessageID, orig.MessageID)
}
if m.SourceID != orig.SourceID {
t.Errorf("retained %q: SourceID = %q, want the original %q",
shortText(m.Text), m.SourceID, orig.SourceID)
}
if !m.At.Equal(orig.At) {
t.Errorf("retained %q: At = %s, want the original %s",
shortText(m.Text), m.At, orig.At)
}
}
if retained == 0 {
t.Fatal("no original message survived compression: nothing was protected")
}
})
}
}
// The consequence of losing identity: after a compression, a redelivered
// upstream message appends a duplicate instead of being recognised. Telegram
// replays its undelivered queue after a restart, so this is the difference
// between a quiet reboot and a doubled conversation.
func TestCompressKeepsRedeliveryIdempotent(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
r, sessionID := routerOn(t, store, "chat-1")
before := seedForCompress(t, store, sessionID, 40)
last := before[len(before)-1]
// Redelivery is a no-op before compression.
if err := store.SaveMessage(ctx, sessionID, Message{
From: last.From, Text: last.Text, SourceID: last.SourceID, At: last.At,
}); err != nil {
t.Fatalf("redeliver before compress: %v", err)
}
preCount, err := store.MessageCount(ctx, sessionID)
if err != nil {
t.Fatalf("MessageCount: %v", err)
}
if preCount != len(before) {
t.Fatalf("redelivery before compress appended: count = %d, want %d", preCount, len(before))
}
if _, err := r.applyCompress(ctx, sessionID, DefaultCompressionConfig()); err != nil {
t.Fatalf("applyCompress: %v", err)
}
compressed, err := store.MessageCount(ctx, sessionID)
if err != nil {
t.Fatalf("MessageCount: %v", err)
}
// The same redelivery must still be a no-op afterwards.
if err := store.SaveMessage(ctx, sessionID, Message{
From: last.From, Text: last.Text, SourceID: last.SourceID, At: last.At,
}); err != nil {
t.Fatalf("redeliver after compress: %v", err)
}
got, err := store.MessageCount(ctx, sessionID)
if err != nil {
t.Fatalf("MessageCount: %v", err)
}
if got != compressed {
t.Errorf("redelivery after compress appended a duplicate: count = %d, want %d",
got, compressed)
}
})
}
}
// The summary describes the conversation; it is not something the user said.
// CompressHistory emits it with role "system", and the write-back must not
// let that fall through to the user, or the model replays "[Earlier
// conversation has been summarised...]" as if the human typed it.
func TestCompressSummaryIsNotAttributedToTheUser(t *testing.T) {
for name, newStore := range compressBackends() {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
store := newStore(t)
r, sessionID := routerOn(t, store, "chat-1")
seedForCompress(t, store, sessionID, 40)
if _, err := r.applyCompress(ctx, sessionID, DefaultCompressionConfig()); err != nil {
t.Fatalf("applyCompress: %v", err)
}
after, err := store.RecentMessages(ctx, sessionID, 1000)
if err != nil {
t.Fatalf("RecentMessages: %v", err)
}
marker := DefaultCompressionConfig().SummaryMarker
found := false
for _, m := range after {
if !strings.Contains(m.Text, marker) {
continue
}
found = true
if m.From != r.Identity {
t.Errorf("summary From = %q, want the bot identity %q: consumers "+
"reconstruct the role by comparing From against the identity, so "+
"any other value replays the summary as a user turn", m.From, r.Identity)
}
}
if !found {
t.Fatal("no summary message found after compression")
}
})
}
}
func shortText(s string) string {
if len(s) > 12 {
return s[:12]
}
return s
}
+31 -4
View File
@@ -71,6 +71,20 @@ type CompressedView struct {
// TokensAfter is the estimated token count after compression.
TokensAfter int
// ProtectedFirst and ProtectedLast are how many leading and trailing
// input messages passed through untouched, and are only meaningful when
// WasCompressed is true.
//
// They exist so a caller persisting the result can write back the
// original records for those positions rather than rebuilding them from
// Role and Content. A rebuilt message is a different message: it loses
// its canonical MessageID, its upstream SourceID and its timestamp, which
// breaks redelivery deduplication permanently and contradicts the
// immutability requirement in docs/architecture/messaging-and-work-intake
// .md lines 112-114.
ProtectedFirst int
ProtectedLast int
}
// CompressedMessage is a message in a compressed conversation view.
@@ -121,13 +135,26 @@ func CompressHistory(messages []CompressedMessage, cfg CompressionConfig) Compre
compressedTokens := estimateTotal(compressed)
return CompressedView{
Messages: compressed,
WasCompressed: true,
TokensBefore: totalTokens,
TokensAfter: compressedTokens,
Messages: compressed,
WasCompressed: true,
TokensBefore: totalTokens,
TokensAfter: compressedTokens,
ProtectedFirst: protectFirst,
ProtectedLast: protectLast,
}
}
// SummaryIndex returns the position of the generated summary within Messages,
// or -1 when nothing was compressed. The summary is the one element of a
// compressed view that is genuinely new; everything else corresponds to an
// input message at a known offset.
func (v CompressedView) SummaryIndex() int {
if !v.WasCompressed {
return -1
}
return v.ProtectedFirst
}
// estimateTotal returns the sum of estimated token counts across messages.
func estimateTotal(messages []CompressedMessage) int {
total := 0
+74 -31
View File
@@ -456,26 +456,48 @@ func (r *Router) handleCompress(ctx context.Context, msg Message, rest string) (
return "No active session. Send a message first.", nil
}
cfg := DefaultCompressionConfig()
fields := strings.Fields(rest)
isPreview := len(fields) == 0 || fields[0] == "--preview" || fields[0] == "--dry-run"
if isPreview {
return r.compressPreview(ctx, sessionID)
// Bare /compress applies. The preview's own closing line tells the
// operator to "Run /compress to apply", so routing it to the preview
// made that instruction circular -- the documented way to compress
// could never compress.
if len(fields) == 0 {
return r.applyCompress(ctx, sessionID, cfg)
}
cfg := DefaultCompressionConfig()
if fields[0] == "here" && len(fields) > 1 {
switch fields[0] {
case "--preview", "--dry-run":
return r.compressPreview(ctx, sessionID)
case "here":
if len(fields) < 2 {
return "Usage: /compress here <N> — keep the last N messages", nil
}
n, ok := parsePositiveInt(fields[1])
if !ok {
return "Usage: /compress here <N> — N must be a positive number", nil
}
cfg.ProtectLast = n
}
if fields[0] == "focus" && len(fields) < 2 {
return "Usage: /compress focus <topic>", nil
}
return r.applyCompress(ctx, sessionID, cfg)
return r.applyCompress(ctx, sessionID, cfg)
case "focus":
if len(fields) < 2 {
return "Usage: /compress focus <topic>", nil
}
// Nothing implements topic-focused summarisation: the config has no
// field for it, so this used to discard the topic and silently run a
// full default compression instead. Rewriting history is not
// something to do as a side effect of an unimplemented option.
return "Focused compression is not supported yet — /compress summarises " +
"the whole history. Use /compress here <N> to keep the last N messages.", nil
default:
// Anything unrecognised used to fall through and compress. An
// irreversible rewrite is the wrong response to a typo.
return "Usage: /compress [here <N> | focus <topic> | --preview]", nil
}
}
func (r *Router) compressPreview(ctx context.Context, sessionID string) (string, error) {
@@ -485,7 +507,11 @@ func (r *Router) compressPreview(ctx context.Context, sessionID string) (string,
return "", fmt.Errorf("message count: %w", err)
}
msgs, err := r.sessionTracker.sessions.RecentMessages(ctx, sessionID, 200)
// Read the same history applyCompress will. Reading a fixed window here
// while reporting count from the store made the two numbers in one
// sentence describe different conversations, and made the preview advise
// against a compression that was in fact needed.
msgs, err := r.sessionTracker.sessions.RecentMessages(ctx, sessionID, count)
if err != nil {
return "", fmt.Errorf("load messages: %w", err)
}
@@ -509,11 +535,41 @@ func (r *Router) compressPreview(ctx context.Context, sessionID string) (string,
), nil
}
// compressedUserFrom is the sender recorded for user turns that survive
// compression. The original username is not recoverable from a
// CompressedMessage, and consumers only test From against the bot identity,
// so any non-bot value reconstructs the user role correctly.
const compressedUserFrom = "user"
// compressedHistory builds the message list that replaces a session's
// history after compression.
//
// Protected messages are carried over as the ORIGINAL records, not rebuilt
// from the compressed view. A rebuilt message is a different message: it gets
// a fresh canonical MessageID, loses the upstream SourceID and has its
// timestamp reset, so every message surviving a compression stops being
// recognisable on redelivery and the session doubles the next time a channel
// replays its queue. Only the summary is a new record.
//
// The summary is attributed to the bot. CompressHistory emits it with role
// "system", which has no representation in the store -- consumers recover the
// role by comparing From against the bot identity -- and attributing a
// description of the conversation to the user makes the model replay it as
// something the human said.
func compressedHistory(original []Message, view CompressedView, identity string) []Message {
summaryAt := view.SummaryIndex()
if summaryAt < 0 {
return original
}
// Defend the slice arithmetic: a view whose protected counts do not fit
// the input would silently drop or duplicate history.
head := min(view.ProtectedFirst, len(original))
tail := min(view.ProtectedLast, len(original)-head)
out := make([]Message, 0, head+1+tail)
out = append(out, original[:head]...)
out = append(out, Message{
From: identity,
Text: view.Messages[summaryAt].Content,
})
out = append(out, original[len(original)-tail:]...)
return out
}
func (r *Router) messagesToCompressed(msgs []Message) []CompressedMessage {
compressed := make([]CompressedMessage, 0, len(msgs))
@@ -549,26 +605,13 @@ func (r *Router) applyCompress(ctx context.Context, sessionID string, cfg Compre
return fmt.Sprintf("Compression not needed (%d messages, ~%d tokens).", len(msgs), view.TokensBefore), nil
}
compressedMsgs := compressedHistory(msgs, view, r.Identity)
// Replace the history with the compressed view in one store-level
// operation. ReplaceMessages writes the replacement before removing
// the originals, so a failure leaves recoverable duplicates instead of
// wiping the session -- the previous delete-then-save order lost the
// entire history if anything went wrong in between.
// Write back sender identities, not role labels. Consumers reconstruct
// the role by comparing From against the bot's identity, so persisting
// the literal string "assistant" would make every retained bot turn
// replay to the model as a user turn.
compressedMsgs := make([]Message, 0, len(view.Messages))
for _, cm := range view.Messages {
from := compressedUserFrom
if cm.Role == "assistant" {
from = r.Identity
}
compressedMsgs = append(compressedMsgs, Message{
From: from,
Text: cm.Content,
})
}
if err := r.sessionTracker.sessions.ReplaceMessages(ctx, sessionID, compressedMsgs); err != nil {
return "", fmt.Errorf("replace with compressed messages: %w", err)
}