fix(sessions): count repeated search matches
- commit
- c7b7d25d484d123bc3c643b275560b2ba3737574
- author
- brice · · 2026-08-19 09:36 UTC
- parent
- ae3daad
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
3 files · +43 -15
M README.md +1 -1
| @@ -359,7 +359,7 @@ Type `!` followed by a command to run it directly without going through the mode | |||
| 359 | 359 | ||
| 360 | 360 | ### `/sessions` | |
| 361 | 361 | ||
| 362 | - | Shows previous sessions for the current working directory, newest first, with timestamp, model, message count, cost, and the session title. Press `Tab` to rotate between **this directory** and **all sessions** in the active normal or Zutfile-agent namespace; other agent namespaces never appear. Press `/` and type at least two characters to search contiguous text in an ephemeral in-memory corpus of persisted user and assistant messages. Tool calls, tool results, images, developer context, and compaction payloads are excluded. The picker displays each matching session's count and a highlighted best excerpt; changing a query reuses the corpus rather than reopening transcripts. Session entries and search corpus load in the background. Once an entry appears, `up`/`down` and `enter` can select it immediately; `esc` clears a search or cancels and closes the picker. Selecting a session from another working directory switches the live workspace to that session's recorded directory before the transcript resumes, so tools, instructions, sandbox state, and file suggestions do not retain the old directory. Sessions remember their stored provider and model: resuming rebuilds the live agent when necessary instead of silently using a different model; if a staged switch fails, the current session is left unchanged. Branches created for tree navigation remain available to `/session tree` but are hidden from this flat picker. `/sessions` is unavailable with `--no-session`. A fresh interactive session asks the active model for a concise title after the first real prompt and restores that title when resumed. | |
| 362 | + | Shows previous sessions for the current working directory, newest first, with timestamp, model, message count, cost, and the session title. Press `Tab` to rotate between **this directory** and **all sessions** in the active normal or Zutfile-agent namespace; other agent namespaces never appear. Press `/` and type at least two characters to search contiguous text in an ephemeral in-memory corpus of persisted user and assistant messages. Tool calls, tool results, images, developer context, and compaction payloads are excluded. The picker displays each matching session's count and a highlighted first matching excerpt; changing a query reuses the corpus rather than reopening transcripts. Session entries and search corpus load in the background. Once an entry appears, `up`/`down` and `enter` can select it immediately; `esc` clears a search or cancels and closes the picker. Selecting a session from another working directory switches the live workspace to that session's recorded directory before the transcript resumes, so tools, instructions, sandbox state, and file suggestions do not retain the old directory. Sessions remember their stored provider and model: resuming rebuilds the live agent when necessary instead of silently using a different model; if a staged switch fails, the current session is left unchanged. Branches created for tree navigation remain available to `/session tree` but are hidden from this flat picker. `/sessions` is unavailable with `--no-session`. A fresh interactive session asks the active model for a concise title after the first real prompt and restores that title when resumed. | |
| 363 | 363 | ||
| 364 | 364 | ### `/session` | |
| 365 | 365 | ||
| @@ -359,7 +359,7 @@ Type `!` followed by a command to run it directly without going through the mode | |||||
| 359 | 359 | ||||
| 360 | ### `/sessions` | 360 | ### `/sessions` | ||
| 361 | 361 | ||||
| 362 | - | Shows previous sessions for the current working directory, newest first, with timestamp, model, message count, cost, and the session title. Press `Tab` to rotate between **this directory** and **all sessions** in the active normal or Zutfile-agent namespace; other agent namespaces never appear. Press `/` and type at least two characters to search contiguous text in an ephemeral in-memory corpus of persisted user and assistant messages. Tool calls, tool results, images, developer context, and compaction payloads are excluded. The picker displays each matching session's count and a highlighted best excerpt; changing a query reuses the corpus rather than reopening transcripts. Session entries and search corpus load in the background. Once an entry appears, `up`/`down` and `enter` can select it immediately; `esc` clears a search or cancels and closes the picker. Selecting a session from another working directory switches the live workspace to that session's recorded directory before the transcript resumes, so tools, instructions, sandbox state, and file suggestions do not retain the old directory. Sessions remember their stored provider and model: resuming rebuilds the live agent when necessary instead of silently using a different model; if a staged switch fails, the current session is left unchanged. Branches created for tree navigation remain available to `/session tree` but are hidden from this flat picker. `/sessions` is unavailable with `--no-session`. A fresh interactive session asks the active model for a concise title after the first real prompt and restores that title when resumed. | 362 | + | Shows previous sessions for the current working directory, newest first, with timestamp, model, message count, cost, and the session title. Press `Tab` to rotate between **this directory** and **all sessions** in the active normal or Zutfile-agent namespace; other agent namespaces never appear. Press `/` and type at least two characters to search contiguous text in an ephemeral in-memory corpus of persisted user and assistant messages. Tool calls, tool results, images, developer context, and compaction payloads are excluded. The picker displays each matching session's count and a highlighted first matching excerpt; changing a query reuses the corpus rather than reopening transcripts. Session entries and search corpus load in the background. Once an entry appears, `up`/`down` and `enter` can select it immediately; `esc` clears a search or cancels and closes the picker. Selecting a session from another working directory switches the live workspace to that session's recorded directory before the transcript resumes, so tools, instructions, sandbox state, and file suggestions do not retain the old directory. Sessions remember their stored provider and model: resuming rebuilds the live agent when necessary instead of silently using a different model; if a staged switch fails, the current session is left unchanged. Branches created for tree navigation remain available to `/session tree` but are hidden from this flat picker. `/sessions` is unavailable with `--no-session`. A fresh interactive session asks the active model for a concise title after the first real prompt and restores that title when resumed. |
| 363 | 363 | ||||
| 364 | ### `/session` | 364 | ### `/session` | ||
| 365 | 365 | ||||
M packages/agent/modes/session_dialog.go +23 -14
| @@ -563,12 +563,12 @@ func matchSessionSearchSegments(ctx context.Context, query string, segments []co | |||
| 563 | 563 | if text == "" { | |
| 564 | 564 | text = core.NormalizeSessionSearchText(segment.Text) | |
| 565 | 565 | } | |
| 566 | - | indexes := sessionSearchSubstringIndexes(text, query) | |
| 567 | - | if len(indexes) == 0 { | |
| 566 | + | count, indexes := sessionSearchSubstringMatches(text, query) | |
| 567 | + | if count == 0 { | |
| 568 | 568 | continue | |
| 569 | 569 | } | |
| 570 | 570 | current := matches[segment.Path] | |
| 571 | - | current.count++ | |
| 571 | + | current.count += count | |
| 572 | 572 | if current.excerpt == "" { | |
| 573 | 573 | current.excerpt = text | |
| 574 | 574 | current.indexes = indexes | |
| @@ -582,18 +582,27 @@ func sessionSearchQuery(query string) bool { | |||
| 582 | 582 | return len([]rune(core.NormalizeSessionSearchText(query))) >= minSessionSearchQueryRunes | |
| 583 | 583 | } | |
| 584 | 584 | ||
| 585 | - | func sessionSearchSubstringIndexes(text, query string) []int { | |
| 586 | - | start := strings.Index(text, query) | |
| 587 | - | if start < 0 { | |
| 588 | - | return nil | |
| 589 | - | } | |
| 590 | - | indexes := make([]int, 0, utf8.RuneCountInString(query)) | |
| 591 | - | for index := start; index < start+len(query); { | |
| 592 | - | indexes = append(indexes, index) | |
| 593 | - | _, size := utf8.DecodeRuneInString(text[index:]) | |
| 594 | - | index += size | |
| 585 | + | func sessionSearchSubstringMatches(text, query string) (int, []int) { | |
| 586 | + | count := 0 | |
| 587 | + | var firstIndexes []int | |
| 588 | + | for start := 0; start < len(text); { | |
| 589 | + | offset := strings.Index(text[start:], query) | |
| 590 | + | if offset < 0 { | |
| 591 | + | break | |
| 592 | + | } | |
| 593 | + | index := start + offset | |
| 594 | + | count++ | |
| 595 | + | if firstIndexes == nil { | |
| 596 | + | firstIndexes = make([]int, 0, utf8.RuneCountInString(query)) | |
| 597 | + | for end := index; end < index+len(query); { | |
| 598 | + | firstIndexes = append(firstIndexes, end) | |
| 599 | + | _, size := utf8.DecodeRuneInString(text[end:]) | |
| 600 | + | end += size | |
| 601 | + | } | |
| 602 | + | } | |
| 603 | + | start = index + len(query) | |
| 595 | 604 | } | |
| 596 | - | return indexes | |
| 605 | + | return count, firstIndexes | |
| 597 | 606 | } | |
| 598 | 607 | ||
| 599 | 608 | func (d *sessionDialog) applySearchFilter() { | |
| @@ -563,12 +563,12 @@ func matchSessionSearchSegments(ctx context.Context, query string, segments []co | |||||
| 563 | if text == "" { | 563 | if text == "" { | ||
| 564 | text = core.NormalizeSessionSearchText(segment.Text) | 564 | text = core.NormalizeSessionSearchText(segment.Text) | ||
| 565 | } | 565 | } | ||
| 566 | - | indexes := sessionSearchSubstringIndexes(text, query) | 566 | + | count, indexes := sessionSearchSubstringMatches(text, query) |
| 567 | - | if len(indexes) == 0 { | 567 | + | if count == 0 { |
| 568 | continue | 568 | continue | ||
| 569 | } | 569 | } | ||
| 570 | current := matches[segment.Path] | 570 | current := matches[segment.Path] | ||
| 571 | - | current.count++ | 571 | + | current.count += count |
| 572 | if current.excerpt == "" { | 572 | if current.excerpt == "" { | ||
| 573 | current.excerpt = text | 573 | current.excerpt = text | ||
| 574 | current.indexes = indexes | 574 | current.indexes = indexes | ||
| @@ -582,18 +582,27 @@ func sessionSearchQuery(query string) bool { | |||||
| 582 | return len([]rune(core.NormalizeSessionSearchText(query))) >= minSessionSearchQueryRunes | 582 | return len([]rune(core.NormalizeSessionSearchText(query))) >= minSessionSearchQueryRunes | ||
| 583 | } | 583 | } | ||
| 584 | 584 | ||||
| 585 | - | func sessionSearchSubstringIndexes(text, query string) []int { | 585 | + | func sessionSearchSubstringMatches(text, query string) (int, []int) { |
| 586 | - | start := strings.Index(text, query) | 586 | + | count := 0 |
| 587 | - | if start < 0 { | 587 | + | var firstIndexes []int |
| 588 | - | return nil | 588 | + | for start := 0; start < len(text); { |
| 589 | - | } | 589 | + | offset := strings.Index(text[start:], query) |
| 590 | - | indexes := make([]int, 0, utf8.RuneCountInString(query)) | 590 | + | if offset < 0 { |
| 591 | - | for index := start; index < start+len(query); { | 591 | + | break |
| 592 | - | indexes = append(indexes, index) | 592 | + | } |
| 593 | - | _, size := utf8.DecodeRuneInString(text[index:]) | 593 | + | index := start + offset |
| 594 | - | index += size | 594 | + | count++ |
| 595 | + | if firstIndexes == nil { | |||
| 596 | + | firstIndexes = make([]int, 0, utf8.RuneCountInString(query)) | |||
| 597 | + | for end := index; end < index+len(query); { | |||
| 598 | + | firstIndexes = append(firstIndexes, end) | |||
| 599 | + | _, size := utf8.DecodeRuneInString(text[end:]) | |||
| 600 | + | end += size | |||
| 601 | + | } | |||
| 602 | + | } | |||
| 603 | + | start = index + len(query) | |||
| 595 | } | 604 | } | ||
| 596 | - | return indexes | 605 | + | return count, firstIndexes |
| 597 | } | 606 | } | ||
| 598 | 607 | ||||
| 599 | func (d *sessionDialog) applySearchFilter() { | 608 | func (d *sessionDialog) applySearchFilter() { | ||
M packages/agent/modes/session_dialog_test.go +19 -0
| @@ -444,6 +444,25 @@ func TestSessionSearchUsesContiguousQueryAndRequiresTwoCharacters(t *testing.T) | |||
| 444 | 444 | } | |
| 445 | 445 | } | |
| 446 | 446 | ||
| 447 | + | func TestSessionSearchCountsRepeatedSegmentMatches(t *testing.T) { | |
| 448 | + | matches := matchSessionSearchSegments(context.Background(), "sharm", []core.SessionSearchSegment{ | |
| 449 | + | {Path: "repeated", Text: "sharm then sharm"}, | |
| 450 | + | }) | |
| 451 | + | match, ok := matches["repeated"] | |
| 452 | + | if !ok { | |
| 453 | + | t.Fatalf("matches = %#v, missing repeated session", matches) | |
| 454 | + | } | |
| 455 | + | if match.count != 2 { | |
| 456 | + | t.Fatalf("match count = %d, want 2", match.count) | |
| 457 | + | } | |
| 458 | + | if match.excerpt != "sharm then sharm" { | |
| 459 | + | t.Fatalf("match excerpt = %q", match.excerpt) | |
| 460 | + | } | |
| 461 | + | if want := []int{0, 1, 2, 3, 4}; len(match.indexes) != len(want) || match.indexes[0] != want[0] || match.indexes[1] != want[1] || match.indexes[2] != want[2] || match.indexes[3] != want[3] || match.indexes[4] != want[4] { | |
| 462 | + | t.Fatalf("highlight indexes = %v, want %v", match.indexes, want) | |
| 463 | + | } | |
| 464 | + | } | |
| 465 | + | ||
| 447 | 466 | func TestSessionDialogDefersSearchUntilSecondCharacter(t *testing.T) { | |
| 448 | 467 | d := newSessionDialog() | |
| 449 | 468 | d.active = true | |
| @@ -444,6 +444,25 @@ func TestSessionSearchUsesContiguousQueryAndRequiresTwoCharacters(t *testing.T) | |||||
| 444 | } | 444 | } | ||
| 445 | } | 445 | } | ||
| 446 | 446 | ||||
| 447 | + | func TestSessionSearchCountsRepeatedSegmentMatches(t *testing.T) { | |||
| 448 | + | matches := matchSessionSearchSegments(context.Background(), "sharm", []core.SessionSearchSegment{ | |||
| 449 | + | {Path: "repeated", Text: "sharm then sharm"}, | |||
| 450 | + | }) | |||
| 451 | + | match, ok := matches["repeated"] | |||
| 452 | + | if !ok { | |||
| 453 | + | t.Fatalf("matches = %#v, missing repeated session", matches) | |||
| 454 | + | } | |||
| 455 | + | if match.count != 2 { | |||
| 456 | + | t.Fatalf("match count = %d, want 2", match.count) | |||
| 457 | + | } | |||
| 458 | + | if match.excerpt != "sharm then sharm" { | |||
| 459 | + | t.Fatalf("match excerpt = %q", match.excerpt) | |||
| 460 | + | } | |||
| 461 | + | if want := []int{0, 1, 2, 3, 4}; len(match.indexes) != len(want) || match.indexes[0] != want[0] || match.indexes[1] != want[1] || match.indexes[2] != want[2] || match.indexes[3] != want[3] || match.indexes[4] != want[4] { | |||
| 462 | + | t.Fatalf("highlight indexes = %v, want %v", match.indexes, want) | |||
| 463 | + | } | |||
| 464 | + | } | |||
| 465 | + | ||||
| 447 | func TestSessionDialogDefersSearchUntilSecondCharacter(t *testing.T) { | 466 | func TestSessionDialogDefersSearchUntilSecondCharacter(t *testing.T) { | ||
| 448 | d := newSessionDialog() | 467 | d := newSessionDialog() | ||
| 449 | d.active = true | 468 | d.active = true | ||