From 8d0479dd3254a6fb039a3fd4259fdb53348b98c4 Mon Sep 17 00:00:00 2001 From: Bharat Kunwar Date: Sun, 22 Nov 2026 08:21:50 +0545 Subject: [PATCH] fix: a search of only spaces is no search Typing a space in the search box was read as a search that matched every session but still ranked them, so named sessions jumped above untitled ones and the preview showed only the first and last messages with gaps. Every place that reads the search now goes through searchQuery(), the box with the spaces around it trimmed: the filter and its narrowing, ranking, HITS, the preview and its hit jumps, the reorg-run filter, ? help, the hidden sidebar and the live tick's re-match, which now uses the same word rule as the filter instead of a substring of the whole query. ccs mcp's search_sessions trims its query too, so its reply names "foo", not " foo ". Co-Authored-By: Claude Opus 5.5 --- internal/mcp/mcp.go | 1 + internal/mcp/mcp_test.go | 18 +++++++ internal/tui/live.go | 4 +- internal/tui/preview.go | 13 ++--- internal/tui/ui.go | 17 ++++--- internal/tui/ui_test.go | 104 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 14 deletions(-) diff --git a/internal/mcp/mcp.go b/internal/mcp/mcp.go index 2ab102f..22e010e 100644 --- a/internal/mcp/mcp.go +++ b/internal/mcp/mcp.go @@ -724,6 +724,7 @@ func Header(c transcript.Conversation) string { } func mcpSearch(convs []transcript.Conversation, query string, n int) (string, error) { + query = strings.TrimSpace(query) // as the list reads it: " foo " is "foo" terms := strings.Fields(strings.ToLower(query)) if len(terms) == 0 { return "", fmt.Errorf("query is empty") diff --git a/internal/mcp/mcp_test.go b/internal/mcp/mcp_test.go index aa011c4..c474b8c 100644 --- a/internal/mcp/mcp_test.go +++ b/internal/mcp/mcp_test.go @@ -123,6 +123,24 @@ func TestMCPNameMatchIgnoresDashes(t *testing.T) { } } +// search_sessions reads a query as the list does: only spaces is as empty +// as no query, and spaces around the words change nothing. +func TestMCPSearchTrimsSpaces(t *testing.T) { + convs := []transcript.Conversation{ + {SessionID: "s", Title: "deploy fix", LastTimestamp: "2026-10-09T00:00:00Z", Messages: []transcript.Message{{Role: "user", Text: "deploy it"}}}, + } + _, emptyErr := mcpSearch(convs, "", 5) + for _, q := range []string{" ", " ", "\t"} { + if _, err := mcpSearch(convs, q, 5); err == nil || emptyErr == nil || err.Error() != emptyErr.Error() { + t.Errorf("%q: %v, want %v as for no query", q, err, emptyErr) + } + } + want, _ := mcpSearch(convs, "deploy", 5) + if got, err := mcpSearch(convs, " deploy ", 5); err != nil || got != want { + t.Errorf("padded query:\n%s\nwant:\n%s", got, want) + } +} + // search_sessions leaves reorg planning runs out (their prompt holds every // session's first message) unless the search names one. func TestMCPSearchSkipsReorgRuns(t *testing.T) { diff --git a/internal/tui/live.go b/internal/tui/live.go index fd086db..e514dbf 100644 --- a/internal/tui/live.go +++ b/internal/tui/live.go @@ -214,12 +214,12 @@ func (m *model) applyLive(updated []transcript.Conversation) { for _, item := range m.filtered { shown[item.conv.SessionID] = true } - query := strings.ToLower(m.textInput.Value()) + terms := strings.Fields(strings.ToLower(m.searchQuery())) filtered := make([]listItem, 0, len(m.filtered)+len(updated)) for _, item := range items { match := shown[item.conv.SessionID] if _, changed := byID[item.conv.SessionID]; changed { - match = query == "" || strings.Contains(item.searchLower, query) + match = len(terms) == 0 || transcript.QueryMatches(item.searchLower, item.conv.Title, terms) // updateFilter's rule // Most appends are tool calls: HITS only moves with new messages. if m.hits != nil && newMessages[item.conv.SessionID] { delete(m.hits.byID, item.conv.SessionID) diff --git a/internal/tui/preview.go b/internal/tui/preview.go index f070859..19e688a 100644 --- a/internal/tui/preview.go +++ b/internal/tui/preview.go @@ -120,7 +120,7 @@ func countHits(conv transcript.Conversation, query string) int { // hitCount returns the memoised hit count for item under the current query. func (m model) hitCount(item listItem) int { - query := m.textInput.Value() + query := m.searchQuery() if query == "" { return 0 } @@ -148,8 +148,8 @@ func (m model) previewLines() []string { return nil // still scrolling through the list: the header alone } conv := m.filtered[m.cursor].conv - conv.PeerNotes = m.cachedPeerLabels(conv) // the other ends, which other sessions' transcripts show - return cachedPreview(m.preview, conv, m.textInput.Value(), m.paneWidth()-1) // the last column is the scrollbar + conv.PeerNotes = m.cachedPeerLabels(conv) // the other ends, which other sessions' transcripts show + return cachedPreview(m.preview, conv, m.searchQuery(), m.paneWidth()-1) // the last column is the scrollbar } // previewDebounce is how long the selection must rest before its @@ -218,6 +218,7 @@ func buildPreview(conv transcript.Conversation, query string, width int) ([]stri var msgLines []string // Find messages containing the query + query = strings.TrimSpace(query) // spaces alone are no search terms := strings.Fields(strings.ToLower(query)) matchSet := make(map[int]bool) if query != "" { @@ -507,7 +508,7 @@ func (m model) hitLines() []int { return hitLinesIn(m.previewLines()) } // jumpHit scrolls the preview so the next (or previous) search hit is at the // top, wrapping round at either end. func (m *model) jumpHit(next bool) { - if len(m.filtered) == 0 || m.textInput.Value() == "" { + if len(m.filtered) == 0 || !m.searching() { return } lines := m.previewLines() @@ -878,7 +879,7 @@ func (m model) maxPreviewScroll() int { // header, matching what View gives renderPreview. func (m model) previewMessageRows(conv transcript.Conversation) int { _, _, previewTop := m.listLayout() - return max(m.height-previewTop-m.chatRows()-len(m.previewHeader(conv, m.textInput.Value())), 1) + return max(m.height-previewTop-m.chatRows()-len(m.previewHeader(conv, m.searchQuery())), 1) } // previewHeader is the preview's fixed header (always visible above the @@ -1001,7 +1002,7 @@ func (m *model) toggleHeader() { } func (m model) renderPreview(item listItem, height int) string { - query := m.textInput.Value() + query := m.searchQuery() conv := item.conv header := m.previewHeader(conv, query) // fixed, always visible diff --git a/internal/tui/ui.go b/internal/tui/ui.go index 78f016a..7df0508 100644 --- a/internal/tui/ui.go +++ b/internal/tui/ui.go @@ -334,7 +334,7 @@ func (m model) listLayout() (listTop, listHeight, previewTop int) { // or while ^X L hides it. A search filters the list, so it shows while one is // typed and hides again once the search is cleared, ^X L staying as it was. func (m model) sidebarWidth() int { - if m.table || m.hideSidebar && m.textInput.Value() == "" { + if m.table || m.hideSidebar && !m.searching() { return 0 } return min(max(m.width/4, 30), 48) @@ -524,7 +524,7 @@ func initialModel(items []listItem, filterQuery string, claudeFlags []string) mo func (m *model) updateFilter() { m.foldedAway = nil // rebuilt from m.items, folded sessions included - queryLower := strings.ToLower(m.textInput.Value()) + queryLower := strings.ToLower(m.searchQuery()) if queryLower == "" { // Make a copy to avoid sharing backing array with m.items m.filtered = make([]listItem, len(m.items)) @@ -591,7 +591,7 @@ func (m *model) hideReorgRuns() { if !m.searching() { return } - q := m.textInput.Value() + q := m.searchQuery() m.filtered = slices.DeleteFunc(m.filtered, func(it listItem) bool { return transcript.HideFromSearch(it.conv, q) && !m.revealed[it.conv.SessionID] }) @@ -2311,7 +2311,7 @@ func (m model) update(msg tea.Msg) (tea.Model, tea.Cmd) { // ? opens help when it can't be meant as text: the box being typed // into (the message box when focused, else the search) is empty, as // on Claude Code's prompt. After text it's a ?. - box := m.textInput.Value() + box := m.searchQuery() // spaces alone are no search, so ? is still help if m.chatFocus { box = m.chatInput.Value() } @@ -2941,7 +2941,7 @@ func (m model) shortcuts() [][2]string { group("moving around") add(len(m.filtered) > 1, "↑↓ ^P/N", "move through the list") add(sel, "pgup/dn", "scroll the conversation (the mouse wheel too)") - add(sel && m.textInput.Value() != "" && len(m.hitLines()) > 0, "^] ^\\", "jump to the next / previous search hit") + add(sel && m.searching() && len(m.hitLines()) > 0, "^] ^\\", "jump to the next / previous search hit") switch nested := sel && m.parentRow(m.cursor) >= 0; { case !m.chatFocus && nested: add(true, keyEsc, "select the session this one is nested under (again: its parent, then the search)") @@ -3468,7 +3468,12 @@ const ( ) // searching: a search is typed, so HITS has something to count. -func (m model) searching() bool { return strings.TrimSpace(m.textInput.Value()) != "" } +// searchQuery is the search as typed with the spaces around it trimmed: the +// one place the list, HITS, the preview and its hit jumps read the search +// from, so a query of only spaces is no search at all and " foo " is "foo". +func (m model) searchQuery() string { return strings.TrimSpace(m.textInput.Value()) } + +func (m model) searching() bool { return m.searchQuery() != "" } // topicColWidth flexes the TOPIC column to fill the terminal width. func (m model) topicColWidth() int { diff --git a/internal/tui/ui_test.go b/internal/tui/ui_test.go index a4c0908..b47d8bb 100644 --- a/internal/tui/ui_test.go +++ b/internal/tui/ui_test.go @@ -1903,3 +1903,107 @@ func TestDeleteConversationErrorHandling(t *testing.T) { t.Errorf("expected IsNotExist error, got: %v", err) } } + +// spacedModel is startedModel with only c named and every session long +// enough that a search preview would leave messages out, so a search that +// ranks (c, named, would go first) or trims the preview shows. +func spacedModel(t *testing.T) model { + t.Helper() + m := startedModel(t) + for i := range m.items { + c := m.items[i].conv + if c.SessionID != "c" { + c.Title = "" + } + for n := range 8 { + c.Messages = append(c.Messages, transcript.Message{Role: "user", Text: "deploy step " + string(rune('a'+n))}) + } + c.SearchText = "" + m.items[i] = buildItems([]transcript.Conversation{c})[0] + } + m.textInput.SetValue("") + m.updateFilter() + return m +} + +// A search of only spaces is no search: the list in its usual order, no +// HITS, the whole conversation in the preview, no hit jumps, ? still help. +func TestSpacesAloneAreNoSearch(t *testing.T) { + base := spacedModel(t) + order, lines := startOrder(base), base.previewLines() + if order != "b,a,c" { + t.Fatalf("with no search the list is newest started first: %s", order) + } + for _, q := range []string{" ", " ", "\t", " \t "} { + m := spacedModel(t) + m.textInput.SetValue(q) + m.updateFilter() + if m.searching() || m.searchQuery() != "" { + t.Errorf("%q: counts as a search", q) + } + if got := startOrder(m); got != order { + t.Errorf("%q: the list is reordered or filtered: %s, want %s", q, got, order) + } + for _, it := range m.filtered { + if it.rank != 0 || m.hitCount(it) != 0 { + t.Errorf("%q: %s ranked %d with %d hits", q, it.conv.SessionID, it.rank, m.hitCount(it)) + } + } + if got := m.previewLines(); !slices.Equal(got, lines) { + t.Errorf("%q: the preview isn't the whole conversation: %d lines, want %d", q, len(got), len(lines)) + } + if got := buildPreviewLines(m.filtered[0].conv, q, 80); !slices.Equal(got, buildPreviewLines(m.filtered[0].conv, "", 80)) { + t.Errorf("%q: buildPreviewLines differs from no search", q) + } + m.previewScroll = 3 + if m.jumpHit(true); m.previewScroll != 3 { + t.Errorf("%q: ^] jumped to a hit", q) + } + m.hideSidebar = true + if m.sidebarWidth() != 0 { + t.Errorf("%q: a hidden sidebar comes back as for a search", q) + } + m.hideSidebar = false + if m, _ = key(m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("?")}); !m.helpOpen { + t.Errorf("%q: ? isn't help", q) + } + } +} + +// Spaces around the words change nothing: " deploy " finds, ranks, counts +// and previews as "deploy" does, after an update from the live tick too. +func TestPaddedSearchMatchesUnpadded(t *testing.T) { + plain := spacedModel(t) + plain.textInput.SetValue("deploy") + plain.updateFilter() + for _, q := range []string{" deploy ", "\tdeploy", "deploy "} { + m := spacedModel(t) + m.textInput.SetValue(q) + m.updateFilter() + if got, want := startOrder(m), startOrder(plain); got != want { + t.Errorf("%q: %s, want %s", q, got, want) + } + for i, it := range m.filtered { + if it.rank != plain.filtered[i].rank || m.hitCount(it) != plain.hitCount(plain.filtered[i]) { + t.Errorf("%q: %s rank %d hits %d, want %d and %d", q, it.conv.SessionID, it.rank, m.hitCount(it), + plain.filtered[i].rank, plain.hitCount(plain.filtered[i])) + } + } + if !slices.Equal(m.previewLines(), plain.previewLines()) { + t.Errorf("%q: the preview differs from the unpadded search", q) + } + // The live tick re-matches a changed session by the same rule: the + // same update keeps the same sessions, padded or not. + p := spacedModel(t) + p.textInput.SetValue("deploy") + p.updateFilter() + c := m.filtered[0].conv + c.Messages = append(c.Messages, transcript.Message{Role: "assistant", Text: "deployed"}) + c.SearchText = "" + m.applyLive([]transcript.Conversation{c}) + p.applyLive([]transcript.Conversation{c}) + if got, want := startOrder(m), startOrder(p); got != want || len(m.filtered) != 3 { + t.Errorf("%q: after a live update: %s, want %s", q, got, want) + } + } +}