Files
archie/internal/gateway/session_delete.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

80 lines
2.9 KiB
Go

package gateway
import (
"context"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// minDeleteRefLen is the shortest prefix /delete will act on.
//
// /resume resolves a session on any unique prefix, because resuming the
// wrong conversation is undone by resuming the right one. Deleting one is
// not undoable, so a reference short enough to be a typo is refused even
// when it happens to match exactly one session today -- the same keystroke
// would hit a different session tomorrow. A full ID always works: the
// guard applies to prefix matching, not to naming a session outright.
const minDeleteRefLen = 4
// handleDelete permanently removes a session and its history.
//
// It is the only way to retire a conversation from chat: /new starts a
// fresh one and leaves the old session listed by /sessions forever, so
// without this the list only ever grows.
func (r *Router) handleDelete(ctx context.Context, msg messaging.Message, rest string) (string, error) {
ref := strings.TrimSpace(rest)
if ref == "" {
// No bare form. Deleting "the current conversation" on an
// unqualified command is one keystroke away from destroying a
// history nobody meant to touch; the session must be named.
return "Usage: /delete <session-id> — permanently removes a conversation and its history. Run /sessions to see the ids.", nil
}
if r.Sessions == nil {
return "Session management is not configured.", nil
}
sessions, err := r.Sessions.List(ctx)
if err != nil {
return "", fmt.Errorf("list sessions: %w", err)
}
target, ambiguous := resolveSessionRef(sessions, ref)
switch {
case ambiguous:
return fmt.Sprintf("Multiple sessions match %q; be more specific.", ref), nil
case target == nil:
return fmt.Sprintf("No session matching %q.", ref), nil
case target.SessionID != ref && len([]rune(ref)) < minDeleteRefLen:
return fmt.Sprintf(
"Refusing to delete on %d characters — this cannot be undone. "+
"Give at least %d characters of the session id, or its full id.",
len([]rune(ref)), minDeleteRefLen,
), nil
}
// Read the active session before the delete, so the reply can say
// whether the operator just ended the conversation they are in.
active := ""
if r.sessionTracker != nil {
active = r.sessionTracker.getActive(msg.ConversationID.ChannelID, msg.ConversationID.ThreadID)
}
if err := r.Sessions.Delete(ctx, target.SessionID); err != nil {
return "", fmt.Errorf("delete session: %w", err)
}
// Only after the store has actually let go of it: a failed delete
// leaves the session live, and dropping the tracker entry then would
// strand the operator's conversation for no reason.
if r.sessionTracker != nil {
r.sessionTracker.forget(target.SessionID)
}
reply := fmt.Sprintf("Deleted session %s (%s) and its history.",
shortSessionID(target.SessionID), sessionDisplayTitle(*target))
if target.SessionID == active {
reply += " The next message starts a new conversation."
}
return reply, nil
}