Files
archie/internal/gateway/session_delete.go
T
sam 1081a6c3ed feat(daemon): identity-scoped isolation + gateway session titles and /delete
Multi-identity daemon isolation:
- Split runIdentities into per-identity poll loops plus one shared
  maintenance-and-drain loop, so reconcilePRs and storage cleanup run in
  multi-identity mode and the global max_concurrency / per-repo
  serialization holds across identities.
- Make the poll path identity-aware: each identity polls with its own
  forge client, bot user, label, trigger, and ack reaction.
- Make allowConcurrentFor task-based and identity-aware via repoFor.
- Roll back storage mounts when container acquisition fails; log
  Store.Update failures after branch set.

Container-mode RPC identity routing:
- forgerpc/worktreerpc gain identity-scoped subjects (SubjectFor,
  RegisterFor) and identity-carrying clients.
- registerTaskRPCServers registers one server pair per identity (plus
  root), so a container-mode task owned by a non-root identity is served
  by its own forge client and worktree manager.

Gateway/Telegram:
- LLM-proposed session titles generated in the background after turns
  (TitleGenerator + chatTitleGenerator), persisted by the gateway.
- /delete session command with prefix guard and tracker forget.
- Shared session display helpers (shortSessionID, sessionDisplayTitle,
  resolveSessionRef).
2026-08-08 01:32:35 +10:00

77 lines
2.8 KiB
Go

package gateway
import (
"context"
"fmt"
"strings"
)
// 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 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.ChannelID, msg.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
}