Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/mcp/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions internal/mcp/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions internal/tui/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions internal/tui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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]
})
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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 {
Expand Down
104 changes: 104 additions & 0 deletions internal/tui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Loading