package gateway import ( "context" "fmt" "maps" "strconv" "strings" "sync" "time" "github.com/google/uuid" "github.com/samcharles93/archie-core/internal/domain/messaging" ) // sessionTracker maps (channelID, threadID) pairs to the currently active // session ID. When a new session is created via /new, /branch, or /topic, // subsequent messages go to that session. type sessionTracker struct { sessions SessionStore // mu guards active. Channel gateways dispatch each inbound update in // its own goroutine, so resolve, setActive and getActive all run // concurrently. mu sync.RWMutex // active maps "channelID:threadID" → sessionID active map[string]string } // newSessionID uses time-ordered UUIDv7 identifiers so session listings and // storage indexes retain creation order in the identifier itself. func newSessionID() string { id, err := uuid.NewV7() if err != nil { // Preserve session creation if the host entropy source is unavailable. return uuid.NewString() } return id.String() } func newSessionTracker(sessions SessionStore) *sessionTracker { return &sessionTracker{ sessions: sessions, active: make(map[string]string), } } // resolve returns the session ID for a message, creating one if needed. // ThreadID may be empty for flat chats. func (t *sessionTracker) resolve(ctx context.Context, platform, botUser, channelID, threadID string) (string, error) { key := sessionTrackerKey(channelID, threadID) t.mu.RLock() id, ok := t.active[key] t.mu.RUnlock() // An empty cached value is treated as a miss rather than an answer, so a // poisoned entry repairs itself instead of persisting every subsequent // message under session "". if ok && id != "" { t.touch(ctx, id) return id, nil } // Look up existing sessions for this channel+thread. The lock is not // held across this I/O: concurrent callers may duplicate the lookup, // but the session ID is derived deterministically from channel+thread // so they converge on the same value and Save is an idempotent upsert. sessions, err := t.sessions.GetByChannel(ctx, platform, channelID) if err != nil { return "", fmt.Errorf("resolve session: %w", err) } // Find the most recent session matching this thread and bot. // // Pick by LastActiveAt rather than trusting storage order so a restart, // which always re-resolves because the cache is in-memory, cannot drop the // operator back into a session they had left with /new. var best *SessionContext for i, s := range sessions { if s.Source.BotUser != botUser || s.Source.ThreadID != threadID { continue } if best == nil || sessionRecency(s).After(sessionRecency(*best)) { best = &sessions[i] } } if best != nil { resolved := t.cacheActive(key, best.SessionID) t.touch(ctx, resolved) return resolved, nil } // Create a new session with a deterministic key so it matches the // sessionKey format used by the LLM responder. // // Claim the cache slot before writing to the store, not after. Saving // first and asking the cache who won afterwards meant the loser of a race // had already persisted a session that nothing pointed at -- unreachable, // but still listed by /topic and /sessions as "(untitled)", and // resurrectable by a later restart now that resolution goes by recency. // Whoever holds the slot is the only one that writes. id = sessionKeyFromFields(channelID, threadID) if winner, won := t.claimActive(key, id); !won { return winner, nil } now := time.Now() sc := SessionContext{ SessionID: id, Source: SessionSource{ Platform: platform, BotUser: botUser, ChannelID: channelID, ThreadID: threadID, }, CreatedAt: now, LastActiveAt: now, } if err := t.sessions.Save(ctx, sc); err != nil { // Drop the claim: leaving it would point every later message in this // chat at a session that was never stored. t.releaseActive(key, id) return "", fmt.Errorf("create session: %w", err) } return id, nil } // touch records that a session is in use now. // // Nothing called Touch before, so LastActiveAt only ever held the creation // time and "resolve the most recently active session" really meant the most // recently created one -- an operator who switched to an older session with // /topic and worked there was dropped back into the newer, abandoned one on // the next restart. // // Best-effort: a failed timestamp update must not fail the turn, and the // worst case is the ordering this restores being stale again. func (t *sessionTracker) touch(ctx context.Context, sessionID string) { if sessionID == "" { return } _ = t.sessions.Touch(ctx, sessionID) } // claimActive reserves key for sessionID unless another caller already holds // it. It returns the winning session ID and whether this caller won. // // This is the same double-check cacheActive performs, moved ahead of the // store write so only the winner writes. func (t *sessionTracker) claimActive(key, sessionID string) (string, bool) { t.mu.Lock() defer t.mu.Unlock() if existing, ok := t.active[key]; ok && existing != "" { return existing, false } t.active[key] = sessionID return sessionID, true } // releaseActive drops a claim that could not be completed. It only removes // the entry when it is still this caller's, so a racing setActive that has // since replaced it is left alone. func (t *sessionTracker) releaseActive(key, sessionID string) { t.mu.Lock() defer t.mu.Unlock() if t.active[key] == sessionID { delete(t.active, key) } } // cacheActive records the resolved session for a channel+thread. A racing // caller may have cached one first; its value wins so every caller returns // the same session ID. func (t *sessionTracker) cacheActive(key, sessionID string) string { t.mu.Lock() defer t.mu.Unlock() if existing, ok := t.active[key]; ok && existing != "" { return existing } t.active[key] = sessionID return sessionID } func sessionKeyFromFields(channelID, threadID string) string { if threadID == "" { return channelID } return channelID + ":" + threadID } // setActive sets the current session for a channel+thread, overriding any // previously resolved session (used by /new, /branch and /topic). func (t *sessionTracker) setActive(channelID, threadID, sessionID string) { // An empty session ID is never meaningful, and caching one is actively // destructive: resolve treats map presence as authoritative, so a "" here // makes every later message in this chat persist under session "" with no // way to recover. Rejecting it at the setter means no caller can poison // the map, whatever it computes. if sessionID == "" { return } t.mu.Lock() defer t.mu.Unlock() t.active[sessionTrackerKey(channelID, threadID)] = sessionID } // flattenTopic points a channel's flat key at whatever session its thread is // currently using, in one critical section. // // Doing this as setActive(get(...)) was two separate critical sections, so a // /resume or /branch landing between them was silently clobbered by the value // read before the switch. func (t *sessionTracker) flattenTopic(channelID, threadID string) { t.mu.Lock() defer t.mu.Unlock() current, ok := t.active[sessionTrackerKey(channelID, threadID)] if !ok || current == "" { // Nothing to carry over. The next message resolves from the store, // which is the correct answer anyway. return } t.active[sessionTrackerKey(channelID, "")] = current } // forget drops every channel+thread whose active session is sessionID. // // It is the counterpart to deleting a session from the store. resolve // treats a cached entry as authoritative and never checks that the session // still exists, so a pointer left behind here would make every later // message in that chat persist under an ID the store no longer knows -- // history written to a session nothing can list, resume or delete. // // One session can be active under more than one key (a flat chat and a // thread that /resume'd the same session), so every entry is swept, not // just the one the command was typed in. func (t *sessionTracker) forget(sessionID string) { // An empty ID is a prefix of nothing and a value of everything a // poisoned map would hold; sweeping on it would clear unrelated chats. if sessionID == "" { return } t.mu.Lock() defer t.mu.Unlock() maps.DeleteFunc(t.active, func(_, id string) bool { return id == sessionID }) } // getActive returns the current session ID for a channel+thread. func (t *sessionTracker) getActive(channelID, threadID string) string { t.mu.RLock() defer t.mu.RUnlock() return t.active[sessionTrackerKey(channelID, threadID)] } // parsePositiveInt is a nilerr-safe wrapper: it returns (n, true) on // success or (0, false) when the string is not a positive integer. It parses // at 32 bits because the value reaches int32 SQL LIMIT parameters; anything // larger is rejected here rather than wrapping at the conversion. func parsePositiveInt(s string) (int, bool) { n, err := strconv.ParseInt(s, 10, 32) return int(n), err == nil && n > 0 } func sessionTrackerKey(channelID, threadID string) string { if threadID == "" { return channelID } return channelID + ":" + threadID } // sessionPlatform is the platform a session for this inbound belongs to: the // channel that carried the message when the frontend named itself, else the // Gateway's own name. Naming it is what makes SessionSource.Platform truthful -- // one Router serves every channel, so its own name is "web" even for a Telegram // turn, which made the platform a constant and left the per-user identity policy // unreachable with a real channel name (archie-core-c1qx). func (r *Router) sessionPlatform(in Inbound) string { if in.Platform != "" { return in.Platform } return r.gatewayName } // ResolveSessionKey resolves or creates a session for a message and // returns the session ID. This is the entry point the LLM responder // in main.go should call instead of its inline sessionKey helper. func (r *Router) ResolveSessionKey(ctx context.Context, in Inbound) (string, error) { conv := in.Message.ConversationID if r.sessionTracker == nil { return sessionKeyFromFields(conv.ChannelID, conv.ThreadID), nil } return r.sessionTracker.resolve(ctx, r.sessionPlatform(in), r.Identity, conv.ChannelID, conv.ThreadID) } // ── Router session command handlers ──────────────────────────────────────── // handleStart acknowledges the bot is running without consuming a reply. func (r *Router) handleStart() (string, error) { return "Archie is running. Send a message to chat, or type /help for available commands.", nil } // handleNew creates a fresh session for the current channel+thread, // optionally setting a title. func (r *Router) handleNew(ctx context.Context, msg messaging.Message, platform, rest string) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } id := newSessionID() now := time.Now() title := strings.TrimSpace(rest) sc := SessionContext{ SessionID: id, Source: SessionSource{ Platform: platform, BotUser: r.Identity, ChannelID: msg.ConversationID.ChannelID, ThreadID: msg.ConversationID.ThreadID, }, Title: title, CreatedAt: now, LastActiveAt: now, } if err := r.sessionTracker.sessions.Save(ctx, sc); err != nil { return "", fmt.Errorf("create session: %w", err) } r.sessionTracker.setActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID, id) if title != "" { return fmt.Sprintf("New session created: %s (%s)", title, shortSessionID(id)), nil } return fmt.Sprintf("New session created (%s). Conversation history has been cleared.", shortSessionID(id)), nil } // handleTopic manages topic/session switching for threaded conversations. // "off" disables topic routing (reverts to flat), "help" shows info, // and a session-id switches to that session. func (r *Router) handleTopic(ctx context.Context, msg messaging.Message, platform, rest string) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } switch rest { case "off": r.sessionTracker.flattenTopic(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID) return "Topic routing disabled. Messages will use the General topic.", nil case "help": return topicHelpText(), nil case "": return r.topicListSessions(ctx, msg, platform) default: return r.topicSwitchSession(ctx, msg, platform, rest) } } func topicHelpText() string { return "Usage: /topic [off|help|]\n" + " off — disable topic routing\n" + " help — show this help\n" + " — switch to a session by the ID shown in /topic" } func (r *Router) topicListSessions(ctx context.Context, msg messaging.Message, platform string) (string, error) { active := r.sessionTracker.getActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID) sessions, err := r.sessionTracker.sessions.GetByChannel(ctx, platform, msg.ConversationID.ChannelID) if err != nil { return "", fmt.Errorf("list sessions: %w", err) } var b strings.Builder b.WriteString("Sessions for this channel:\n") for _, s := range sessions { marker := " " if s.SessionID == active { marker = "*" } fmt.Fprintf(&b, " %s %s — %s", marker, shortSessionID(s.SessionID), sessionDisplayTitle(s)) if s.BranchName != "" { fmt.Fprintf(&b, " [branch: %s]", s.BranchName) } fmt.Fprintln(&b) } return b.String(), nil } func (r *Router) topicSwitchSession(ctx context.Context, msg messaging.Message, platform, rest string) (string, error) { sessions, err := r.sessionTracker.sessions.GetByChannel(ctx, platform, msg.ConversationID.ChannelID) if err != nil { return "", fmt.Errorf("list sessions: %w", err) } match, ambiguous := resolveSessionRef(sessions, rest) if ambiguous { return fmt.Sprintf("Multiple sessions match %q. Use more of the session ID.", rest), nil } if match != nil { r.sessionTracker.setActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID, match.SessionID) return fmt.Sprintf("Switched to session %s: %s", shortSessionID(match.SessionID), sessionDisplayTitle(*match)), nil } return fmt.Sprintf("No session found matching %q. Use /topic to list available sessions.", rest), nil } // handleRetry removes the last assistant message from history and replays // the last user message through the LLM. func (r *Router) handleRetry(ctx context.Context, msg messaging.Message) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } sessionID := r.sessionTracker.getActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID) if sessionID == "" { return "No active session. Send a message first.", nil } // Delete the last message (assistant response). deleted, err := r.sessionTracker.sessions.DeleteRecentMessages(ctx, sessionID, 1) if err != nil { return "", fmt.Errorf("retry: %w", err) } if deleted == 0 { return "Nothing to retry — no messages in this session.", nil } // Retrieve the last user message and replay it. msgs, err := r.sessionTracker.sessions.RecentMessages(ctx, sessionID, 1) if err != nil { return "", fmt.Errorf("retry load messages: %w", err) } if len(msgs) == 0 { return "Nothing to retry — no messages remain.", nil } if r.LLM == nil { return "LLM is not configured. The last response has been removed; send another message to continue.", nil } // Replay the stored record, but route it with the live message's // addressing. Stored history is read back with the conversation // address of the session that holds it, which for a replay must be // the chat the /retry was typed in. A replay carries no page: the // operator's route is transport context of the message they sent, // and this message is not one they just sent. replay := msgs[0] replay.ConversationID = msg.ConversationID reply, err := r.LLM(ctx, Inbound{Message: replay}) if err == nil { r.maybeAutoTitle(ctx, replay) } return reply, err } // handleUndo removes the last N messages (default 1) from the session // history. func (r *Router) handleUndo(ctx context.Context, msg messaging.Message, rest string) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } sessionID := r.sessionTracker.getActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID) if sessionID == "" { return "No active session. Send a message first.", nil } n := 1 if rest != "" { parsed, ok := parsePositiveInt(strings.TrimSpace(rest)) if !ok { return "Usage: /undo [N] — N must be a positive number (default: 1)", nil } n = parsed } deleted, err := r.sessionTracker.sessions.DeleteRecentMessages(ctx, sessionID, n) if err != nil { return "", fmt.Errorf("undo: %w", err) } if deleted == 0 { return "Nothing to undo — no messages in this session.", nil } if deleted == 1 { return "Removed 1 message.", nil } return fmt.Sprintf("Removed %d messages.", deleted), nil } // handleTitle sets a display title on the current session. func (r *Router) handleTitle(ctx context.Context, msg messaging.Message, rest string) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } sessionID := r.sessionTracker.getActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID) if sessionID == "" { return "No active session. Send a message first.", nil } title := strings.TrimSpace(rest) if title == "" { // Show current title. sc, err := r.sessionTracker.sessions.Get(ctx, sessionID) if err != nil { return "", fmt.Errorf("get session: %w", err) } if sc == nil || sc.Title == "" { return "No title set. Use /title to set one.", nil } return fmt.Sprintf("Session title: %s", sc.Title), nil } sc, err := r.sessionTracker.sessions.Get(ctx, sessionID) if err != nil { return "", fmt.Errorf("get session: %w", err) } if sc == nil { return "Session not found.", nil } sc.Title = title if err := r.sessionTracker.sessions.Save(ctx, *sc); err != nil { return "", fmt.Errorf("save session: %w", err) } return fmt.Sprintf("Session title set to: %s", title), nil } // handleBranch creates a new child session that inherits the current // session's history. The new session becomes the active session. func (r *Router) handleBranch(ctx context.Context, msg messaging.Message, platform, rest string) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } channelID := msg.ConversationID.ChannelID threadID := msg.ConversationID.ThreadID parentID := r.sessionTracker.getActive(channelID, threadID) if parentID == "" { // Nothing to branch from. Telegram never reaches this -- submitTurn // resolves first -- but the email and webhook channels call Route // directly, and slicing an empty parent ID for the default title // panicked. The email path runs in a goroutine with no recover, so // that took the daemon down over a single message. return "No active session to branch from. Send a message first.", nil } branchName := strings.TrimSpace(rest) // Get parent session for title inheritance. parentSC, err := r.sessionTracker.sessions.Get(ctx, parentID) if err != nil { return "", fmt.Errorf("get parent session: %w", err) } id := newSessionID() now := time.Now() title := branchName if parentSC != nil && parentSC.Title != "" && branchName == "" { title = parentSC.Title + " (branch)" } if title == "" { title = "Branch of " + shortSessionID(parentID) } sc := SessionContext{ SessionID: id, Source: SessionSource{ Platform: platform, BotUser: r.Identity, ChannelID: channelID, ThreadID: threadID, }, Title: title, ParentSessionID: parentID, BranchName: branchName, CreatedAt: now, LastActiveAt: now, } if err := r.sessionTracker.sessions.Save(ctx, sc); err != nil { return "", fmt.Errorf("create branch session: %w", err) } // Inherit history from parent. msgs, err := r.sessionTracker.sessions.RecentMessages(ctx, parentID, 200) if err != nil { return "", fmt.Errorf("inherit history: %w", err) } if len(msgs) > 0 { // Clear the canonical ID so the child derives its own. The store // honours an incoming MessageID -- which is what lets a // read-modify-write keep identities -- so copying the parent's // messages verbatim made two sessions claim one identity, leaving // branch-point correlation ambiguous. It also broke dedup in the // child: an inherited message carried an ID derived from the // parent's session, so redelivering it appended. inherited := make([]messaging.Message, 0, len(msgs)) for _, m := range msgs { m.ID = "" inherited = append(inherited, m) } if err := r.sessionTracker.sessions.SaveMessages(ctx, id, inherited); err != nil { return "", fmt.Errorf("copy history: %w", err) } } r.sessionTracker.setActive(channelID, threadID, id) if branchName != "" { return fmt.Sprintf("Branched to \"%s\" (%s) with %d inherited messages.", branchName, shortSessionID(id), len(msgs)), nil } return fmt.Sprintf("Branched session (%s) with %d inherited messages.", shortSessionID(id), len(msgs)), nil } // handleCompress applies context compression to the current session's // history. Sub-commands: here [N], focus , --preview, --dry-run. func (r *Router) handleCompress(ctx context.Context, msg messaging.Message, rest string) (string, error) { if r.sessionTracker == nil { return "Session management is not configured.", nil } sessionID := r.sessionTracker.getActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID) if sessionID == "" { return "No active session. Send a message first.", nil } cfg := DefaultCompressionConfig() fields := strings.Fields(rest) // 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) } switch fields[0] { case "--preview", "--dry-run": return r.compressPreview(ctx, sessionID) case "here": if len(fields) < 2 { return "Usage: /compress here — keep the last N messages", nil } n, ok := parsePositiveInt(fields[1]) if !ok { return "Usage: /compress here — N must be a positive number", nil } cfg.ProtectLast = n return r.applyCompress(ctx, sessionID, cfg) case "focus": if len(fields) < 2 { return "Usage: /compress focus ", 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 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 | focus | --preview]", nil } } func (r *Router) compressPreview(ctx context.Context, sessionID string) (string, error) { cfg := DefaultCompressionConfig() count, err := r.sessionTracker.sessions.MessageCount(ctx, sessionID) if err != nil { return "", fmt.Errorf("message count: %w", err) } // 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) } compressed := r.messagesToCompressed(msgs) view := CompressHistory(compressed, cfg) if !view.WasCompressed { return fmt.Sprintf("Compression not needed (%d messages, ~%d tokens).", count, view.TokensBefore), nil } removed := count - cfg.ProtectFirst - cfg.ProtectLast return fmt.Sprintf( "Compression preview:\n"+ " Messages: %d\n"+ " Tokens before: ~%d\n"+ " Tokens after: ~%d\n"+ " %d messages would be summarised.\n"+ "Run /compress to apply, or /compress here to keep the last N messages protected.", count, view.TokensBefore, view.TokensAfter, removed, ), nil } // 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 -- the summary is // recorded as an assistant message instead -- and attributing a description // of the conversation to the user makes the model replay it as something // the human said. func compressedHistory(original []messaging.Message, view CompressedView, identity string) []messaging.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) // The summary stands in for the span it replaced, so it takes a // timestamp inside that span. Ordering is by timestamp, not by the order // the caller writes: the retained messages keep their original times -- // that is what preserves their identity -- so a summary stamped with now // would sort after the tail it is supposed to precede, and the model // would read the recent conversation before being told earlier context // was summarised. if summaryAt >= len(view.Messages) { // A view whose summary index is outside its own messages cannot be // reconstructed; leaving history untouched is the safe answer. return original } out := make([]messaging.Message, 0, head+1+tail) out = append(out, original[:head]...) out = append(out, messaging.Message{ Sender: identity, Role: messaging.RoleAssistant, Text: view.Messages[summaryAt].Content, At: summaryInstant(original, head, tail), }) out = append(out, original[len(original)-tail:]...) return out } // summaryInstant returns a time inside the gap the summary fills: after the // last retained head message and before the first retained tail message. func summaryInstant(original []messaging.Message, head, tail int) time.Time { // The first message the summary replaces is the natural anchor -- the // summary begins where the summarised span began. if head < len(original)-tail { return original[head].At } // Nothing was actually replaced. Sit just after the head instead of // colliding with the tail. if head > 0 { return original[head-1].At.Add(time.Millisecond) } return time.Time{} } func (r *Router) messagesToCompressed(msgs []messaging.Message) []CompressedMessage { compressed := make([]CompressedMessage, 0, len(msgs)) for _, h := range msgs { role := "user" if h.Role == messaging.RoleAssistant { role = "assistant" } compressed = append(compressed, CompressedMessage{ Role: role, Content: h.Text, }) } return compressed } // applyCompress rewrites a session's stored history with the compressed view // of cfg and reports what changed. It is the operator-facing wrapper around // compressSessionHistory, which the automatic turn-path trigger also calls. func (r *Router) applyCompress(ctx context.Context, sessionID string, cfg CompressionConfig) (string, error) { view, loaded, err := r.compressSessionHistory(ctx, sessionID, cfg) if err != nil { return "", err } if !view.WasCompressed { return fmt.Sprintf("Compression not needed (%d messages, ~%d tokens).", loaded, view.TokensBefore), nil } return fmt.Sprintf( "Compressed: %d messages → %d (~%d → ~%d tokens).", loaded, len(view.Messages), view.TokensBefore, view.TokensAfter, ), nil } // compressSessionHistory loads a session's whole stored history, compresses it // with cfg and, when that removes anything, persists the replacement. It is // the one compression implementation: the /compress command and the automatic // trigger on the inbound turn path both call it, so what an operator asks for // and what a session does unattended cannot diverge. // // loaded is the number of stored messages read, which is what the caller's // reply describes when nothing was compressed. func (r *Router) compressSessionHistory(ctx context.Context, sessionID string, cfg CompressionConfig) (view CompressedView, loaded int, err error) { // Read the whole history: the replacement below becomes the session's // entire history, so summarising only a recent window would silently // discard everything older than it. count, err := r.sessionTracker.sessions.MessageCount(ctx, sessionID) if err != nil { return CompressedView{}, 0, fmt.Errorf("count messages: %w", err) } msgs, err := r.sessionTracker.sessions.RecentMessages(ctx, sessionID, count) if err != nil { return CompressedView{}, 0, fmt.Errorf("load messages: %w", err) } view = CompressHistory(r.messagesToCompressed(msgs), cfg) if !view.WasCompressed { return view, count, nil } compressedMsgs := compressedHistory(msgs, view, r.Identity) // Name exactly the messages this compression consumed. A turn that // arrives between the read above and the write below was never read and // never summarised, so it must not be swept away with the ones that // were -- that was unrecoverable loss of a message nobody had seen. superseded := make([]string, 0, len(msgs)) for _, m := range msgs { if m.ID != "" { superseded = append(superseded, string(m.ID)) } } // 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. if err := r.sessionTracker.sessions.ReplaceMessages(ctx, sessionID, compressedMsgs, superseded); err != nil { return CompressedView{}, count, fmt.Errorf("replace with compressed messages: %w", err) } return view, count, nil } // compressSessionIfOverBudget compresses a session's stored history when it // sits past the budget in cfg, and reports whether it wrote a replacement. // It is the automatic trigger's entry point into the one compression path; a // session inside its budget is left untouched. func (r *Router) compressSessionIfOverBudget(ctx context.Context, sessionID string, cfg CompressionConfig) (bool, error) { if r.sessionTracker == nil { return false, nil } view, _, err := r.compressSessionHistory(ctx, sessionID, cfg) if err != nil { return false, err } return view.WasCompressed, nil }