fix(gateway): make UUIDv7 session references unambiguous

This commit is contained in:
sam committed 2026-08-25 03:28:38 +10:00
1 parent a27fb5f36e
commit 9d2c1e661d
4 files changed
+44 -20

No files matched your search

+4 -1
View File
@@ -955,7 +955,10 @@ func renderSessionList(sessions []SessionContext, active string, now time.Time)
// shortSessionIDLen is how much of a session ID the chat surfaces show. It
// is also the shorthand /resume and /delete are given back, so every
// listing must truncate to the same width.
const shortSessionIDLen = 8
// UUIDv7 stores its timestamp at the front, so no fixed shorter prefix is
// guaranteed to distinguish sessions. UUID session references therefore use
// all 36 characters. The limit only abbreviates longer legacy/non-UUID keys.
const shortSessionIDLen = 36
// shortSessionID renders the abbreviated form of a session ID used
// wherever a session is listed.
+14 -12
View File
@@ -316,9 +316,9 @@ func (r *Router) handleNew(ctx context.Context, msg Message, rest string) (strin
r.sessionTracker.setActive(msg.ChannelID, msg.ThreadID, id)
if title != "" {
return fmt.Sprintf("New session created: %s (%s)", title, id[:8]), nil
return fmt.Sprintf("New session created: %s (%s)", title, shortSessionID(id)), nil
}
return fmt.Sprintf("New session created (%s). Conversation history has been cleared.", id[:8]), nil
return fmt.Sprintf("New session created (%s). Conversation history has been cleared.", shortSessionID(id)), nil
}
// handleTopic manages topic/session switching for threaded conversations.
@@ -346,7 +346,7 @@ func topicHelpText() string {
return "Usage: /topic [off|help|<session-id>]\n" +
" off — disable topic routing\n" +
" help — show this help\n" +
" <session-id> — switch to a session by its first 8 characters"
" <session-id> — switch to a session by the ID shown in /topic"
}
func (r *Router) topicListSessions(ctx context.Context, msg Message) (string, error) {
@@ -377,12 +377,14 @@ func (r *Router) topicSwitchSession(ctx context.Context, msg Message, rest strin
if err != nil {
return "", fmt.Errorf("list sessions: %w", err)
}
for _, s := range sessions {
if len(s.SessionID) >= len(rest) && s.SessionID[:len(rest)] == rest {
r.sessionTracker.setActive(msg.ChannelID, msg.ThreadID, s.SessionID)
return fmt.Sprintf("Switched to session %s: %s",
shortSessionID(s.SessionID), sessionDisplayTitle(s)), nil
}
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.ChannelID, msg.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
}
@@ -541,7 +543,7 @@ func (r *Router) handleBranch(ctx context.Context, msg Message, rest string) (st
title = parentSC.Title + " (branch)"
}
if title == "" {
title = "Branch of " + parentID[:8]
title = "Branch of " + shortSessionID(parentID)
}
sc := SessionContext{
@@ -588,9 +590,9 @@ func (r *Router) handleBranch(ctx context.Context, msg Message, rest string) (st
r.sessionTracker.setActive(channelID, threadID, id)
if branchName != "" {
return fmt.Sprintf("Branched to \"%s\" (%s) with %d inherited messages.", branchName, id[:8], len(msgs)), nil
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.", id[:8], 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
+21 -2
View File
@@ -325,7 +325,7 @@ func TestRouteTopicSwitch(t *testing.T) {
secondID := r.sessionTracker.getActive("chat-6", "")
// Switch back to first using prefix.
prefix := firstID[:min(8, len(firstID))]
prefix := shortSessionID(firstID)
reply, err := r.Route(context.Background(), Message{Text: "/topic " + prefix, ChannelID: "chat-6"})
if err != nil {
t.Fatalf("Route: %v", err)
@@ -336,7 +336,26 @@ func TestRouteTopicSwitch(t *testing.T) {
current := r.sessionTracker.getActive("chat-6", "")
if current != firstID {
t.Errorf("active = %s, want %s (firstID=%s, secondID=%s)", current[:8], firstID[:8], firstID[:8], secondID[:8])
t.Errorf("active = %s, want %s (secondID=%s)", current, firstID, secondID)
}
}
func TestRouteTopicRejectsAmbiguousPrefix(t *testing.T) {
r := newTestRouter("telegram")
_, _ = r.Route(context.Background(), Message{Text: "/new first", ChannelID: "chat-ambiguous"})
firstID := r.sessionTracker.getActive("chat-ambiguous", "")
_, _ = r.Route(context.Background(), Message{Text: "/new second", ChannelID: "chat-ambiguous"})
secondID := r.sessionTracker.getActive("chat-ambiguous", "")
reply, err := r.Route(context.Background(), Message{Text: "/topic " + firstID[:8], ChannelID: "chat-ambiguous"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Multiple sessions") {
t.Errorf("reply = %q, want ambiguity warning", reply)
}
if current := r.sessionTracker.getActive("chat-ambiguous", ""); current != secondID {
t.Errorf("ambiguous switch changed active session to %q, want %q", current, secondID)
}
}
+5 -5
View File
@@ -150,9 +150,9 @@ func TestRenderSessionList(t *testing.T) {
}
got := renderSessionList(sessions, "aaaaaaaa-1111-2222-3333-444444444444", now)
want := "Sessions (3):\n" +
" 1. aaaaaaaa Deploy NATS worker image · active · 5m ago\n" +
" 2. bbbbbbbb Fix telegram redelivery · 2h ago\n" +
" 3. cccccccc (untitled) · 3d ago"
" 1. aaaaaaaa-1111-2222-3333-444444444444 Deploy NATS worker image · active · 5m ago\n" +
" 2. bbbbbbbb-1111-2222-3333-444444444444 Fix telegram redelivery · 2h ago\n" +
" 3. cccccccc-1111-2222-3333-444444444444 (untitled) · 3d ago"
if got != want {
t.Errorf("renderSessionList:\n got:\n%q\nwant:\n%q", got, want)
}
@@ -190,8 +190,8 @@ func TestRouteSessionsRenderedList(t *testing.T) {
}
for _, want := range []string{
"Sessions (2):",
"1. aaaaaaaa Deploy NATS worker image",
"2. bbbbbbbb (untitled)",
"1. aaaaaaaa-1111-2222-3333-444444444444 Deploy NATS worker image",
"2. bbbbbbbb-1111-2222-3333-444444444444 (untitled)",
"5m ago",
"2h ago",
} {