Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent-tui): align /find per logical line so one unalignable row keeps the rest wrap-aware, and cache a subagent's unwrapped text
thinhlpg1337fca5f253
diff --git a/docs/src/pages/docs/agent/slash-commands.mdx b/docs/src/pages/docs/agent/slash-commands.mdxindex 7d5bf6998..3413dbb99 100644--- a/docs/src/pages/docs/agent/slash-commands.mdx+++ b/docs/src/pages/docs/agent/slash-commands.mdx@@ -59,7 +59,7 @@ See [Sessions](/docs/agent/sessions).| `/shells` (alias `/jobs`) | Inspect background shell commands the agent left running: one `• command` row each, with how long it has run; `x` stops the selected one || `/stop` | Stop every background shell command at once || `/monitors` | Inspect the monitors watching for a condition: id, name, polls so far and script; `x` stops the selected one |-| `/find <term>` | Search the whole transcript (case-insensitive) and jump to the nearest match, unfolding it if needed. A term that wraps across two lines is still found and highlighted on both, and the match count does not change when you resize the window. <kbd>Enter</kbd> / <kbd>Shift</kbd>+<kbd>Enter</kbd> step to the next / previous match, wrapping at the ends, while the input is empty (they type nothing, so the start of your next message is never eaten); <kbd>Esc</kbd> or the next message clears it. A miss is reported in the footer |+| `/find <term>` | Search the whole transcript (case-insensitive) and jump to the nearest match, unfolding it if needed. A term that wraps across two lines is still found and highlighted on both, and the match count does not change when you resize the window. The exception is text a narrow window lays out differently from how it was written, such as a table row whose cells wrap, or a label cut short with `…`: there only what is on screen is searched, so a match inside it can come and go with the width. <kbd>Enter</kbd> / <kbd>Shift</kbd>+<kbd>Enter</kbd> step to the next / previous match, wrapping at the ends, while the input is empty (they type nothing, so the start of your next message is never eaten); <kbd>Esc</kbd> or the next message clears it. A miss is reported in the footer |See [Run modes](/docs/agent/run-modes), [Goals and todos](/docs/agent/goals-and-todos), and[Context and compaction](/docs/agent/context).diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex e92f967f0..e5030dc57 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -3474,6 +3474,23 @@ impl Transcript {})}+ /// An expanded region's text before wrapping, one entry per logical line,+ /// for `/find`. A finished subagent's is kept on its block; the others are+ /// cheap to lay out wide, and hold no per-width row caches that doing so+ /// would disturb.+ fn region_flat(&self, index: &RegionIndex, i: usize) -> Option<Vec<String>> {+ let owned_elsewhere = index.groups.contains_key(&i)+ || index.reasoning.contains_key(&i)+ || self.tool_group.as_ref().is_some_and(|g| g.idx == i);+ if !owned_elsewhere {+ if let Some(&pos) = index.subagents.get(&i) {+ return Some(self.subagent_blocks[pos].flat_detail().clone());+ }+ }+ self.region_detail_in(index, i, find::FLAT_WIDTH)+ .map(|lines| find::flat_text(&lines))+ }+/// Where each row's region lives in `groups`, `reasoning_blocks` and/// `subagent_blocks`, so a caller asking about many rows (the search scan,/// a frame with many expanded rows) does one lookup per row instead of@@ -4792,6 +4809,10 @@ struct SubagentBlock {idx: usize,panel: SubagentPanel,outcome: SubagentOutcome,+ /// What the expanded run says before wrapping (see `find::flat_text`),+ /// laid out once: a finished child's transcript does not change, and+ /// laying it out wide refills every one of its rows' single-width caches.+ flat: std::cell::OnceCell<Vec<String>>,}impl SubagentBlock {@@ -4809,6 +4830,12 @@ impl SubagentBlock {}).collect()}++ /// The run's text before wrapping, laid out once (see `flat`).+ fn flat_detail(&self) -> &Vec<String> {+ self.flat+ .get_or_init(|| find::flat_text(&self.detail_lines(find::FLAT_WIDTH)))+ }}/// A queued git workspace snapshot. Snapshots only stage the paths the agent@@ -5400,6 +5427,7 @@ impl App {idx,panel,outcome,+ flat: std::cell::OnceCell::new(),});}}@@ -5683,13 +5711,10 @@ impl App {if let Some(cached) = row.cache.borrow().as_ref() {scan(i, false, &cached.lines, &row.flat());}- if let Some(detail) = self.transcript.region_detail_in(&index, i, width) {- let flat = self- .transcript- .region_detail_in(&index, i, find::FLAT_WIDTH)- .map(|lines| find::flat_text(&lines))- .unwrap_or_default();- scan(i, true, &detail, &flat);+ if let Some(flat) = self.transcript.region_flat(&index, i) {+ if let Some(detail) = self.transcript.region_detail_in(&index, i, width) {+ scan(i, true, &detail, &flat);+ }}}hits@@ -21219,9 +21244,7 @@ fn detail_flat_text(row: usize,) -> Option<Vec<String>> {let index = index.get_or_init(|| transcript.region_index());- transcript- .region_detail_in(index, row, find::FLAT_WIDTH)- .map(|lines| find::flat_text(&lines))+ transcript.region_flat(index, row)}impl Segment {diff --git a/src-tauri/src/core/cli/tui/find.rs b/src-tauri/src/core/cli/tui/find.rsindex 6d923b66e..ff0b2629e 100644--- a/src-tauri/src/core/cli/tui/find.rs+++ b/src-tauri/src/core/cli/tui/find.rs@@ -138,11 +138,12 @@ pub(super) struct Occurrence {/// `lines`, the part as rendered at the current width. Without `flat` the/// rendered lines are the text.///-/// The mapping lines the two renderings up by their non-blank characters, so a-/// wrap that swallows a space, or a gutter glyph repeated on a continuation-/// line, does not matter. When they do not line up (a table re-laid out, a-/// label cut short at a narrow width) the rendered lines are searched on their-/// own, as they were before the term could span lines.+/// Each logical line of `flat` is lined up with the rendered lines by its+/// non-blank characters, so a wrap that swallows a space, or a gutter glyph+/// repeated on a continuation line, does not matter. A line that does not line+/// up (a table row whose cells wrapped, a label cut short at a narrow width)+/// has the rendered lines it sits among searched on their own, and only that+/// line: the others keep their wrap-aware matching and their logical index.pub(super) fn occurrences(lines: &[Line<'_>],flat: Option<&[String]>,@@ -176,24 +177,43 @@ pub(super) fn occurrences(.collect();}let (whole, shown) = (Glyphs::of(flat), Glyphs::of(&rendered));- let Some(map) = whole.onto(&shown) else {- return per_line(&rendered, needle);- };- found- .into_iter()- .filter_map(|(logical, range)| {+ let fit = whole.align(&shown);+ let mut out = Vec::new();+ let mut last_loose: Option<&Range<usize>> = None;+ for (logical, text) in flat.iter().enumerate() {+ if let Some(span) = &fit.loose[logical] {+ // Only the rendered lines are known: search them as they are. The+ // run of loose lines shares one span, searched once and counted+ // under its first line.+ if last_loose != Some(span) {+ last_loose = Some(span);+ for line in span.clone() {+ for range in match_ranges(&rendered[line], needle) {+ out.push(Occurrence {+ logical,+ pieces: vec![(line, range)],+ });+ }+ }+ }+ continue;+ }+ for range in match_ranges(text, needle) {let mut pieces: Vec<(usize, Range<usize>)> = Vec::new();for g in whole.within(logical, &range) {- let (line, at) = shown.at[map[g]];- let end = at + shown.chars[map[g]].len_utf8();+ let (line, at) = shown.at[fit.map[g]];+ let end = at + shown.chars[fit.map[g]].len_utf8();match pieces.last_mut() {Some((l, r)) if *l == line => r.end = end,_ => pieces.push((line, at..end)),}}- (!pieces.is_empty()).then_some(Occurrence { logical, pieces })- })- .collect()+ if !pieces.is_empty() {+ out.push(Occurrence { logical, pieces });+ }+ }+ }+ out}/// Occurrences inside single lines, each its own logical line.@@ -246,40 +266,108 @@ impl Glyphs {from + before(range.start)..from + before(range.end)}- /// For each of this one's characters, the index of the same character in- /// `other`, which may carry extras (a gutter glyph repeated on every- /// continuation line). `None` when this one has text `other` does not.+ /// Lines each logical line of this one up with the rendered lines of+ /// `shown`, one logical line at a time. A line starts at the first+ /// rendered line, from where the last one ended, that opens with its first+ /// character, and takes its characters in order from there, so a wrap+ /// that swallows a space, or a gutter glyph repeated on a continuation+ /// line, does not matter. A line that does not line up (a table row whose+ /// cells wrapped and interleaved, a label cut short with `…`) is `loose`,+ /// and the next one picks up from where the last good one ended: one such+ /// line does not cost the others theirs.////// A line of only rule glyphs (a horizontal rule, a box border) is as wide/// as the render it came from, so this one has far more of them than a- /// narrower `other`. Such a line is mapped as a unit onto the matching line- /// of `other`, its surplus glyphs landing on that line's last one.- fn onto(&self, other: &Glyphs) -> Option<Vec<usize>> {- let mut map = Vec::with_capacity(self.chars.len());- let mut next = 0;- for line in 0..self.starts.len() - 1 {- let (from, to) = (self.starts[line], self.starts[line + 1]);- let own = &self.chars[from..to];- if !own.is_empty() && own.iter().all(|&c| is_rule_glyph(c)) {- if let Some(&(shown, _)) = other.at.get(next) {- let (a, b) = (other.starts[shown], other.starts[shown + 1]);- if a == next && other.chars[a..b].iter().all(|&c| is_rule_glyph(c)) {- map.extend((0..own.len()).map(|k| a + k.min(b - a - 1)));- next = b;- continue;+ /// narrower `shown`. Such a line is mapped as a unit onto the next rule+ /// line of `shown`, its surplus glyphs landing on that line's last one.+ fn align(&self, shown: &Glyphs) -> Alignment {+ let lines = self.starts.len() - 1;+ let mut map = vec![usize::MAX; self.chars.len()];+ let mut loose: Vec<Option<Range<usize>>> = vec![None; lines];+ let mut at = 0;+ // The loose lines since the last one that lined up, and the rendered+ // line they begin at: all that is known of them is that they sit+ // between that and wherever the next good line starts.+ let mut run: Vec<usize> = Vec::new();+ let mut run_from = 0;+ for line in 0..lines {+ if self.starts[line] == self.starts[line + 1] {+ continue;+ }+ match self.fit(line, shown, at, &mut map) {+ Some((first, next)) => {+ for l in run.drain(..) {+ loose[l] = Some(run_from..first);+ }+ at = next;+ }+ None => {+ if run.is_empty() {+ run_from = at;}+ run.push(line);}}- for c in own {- let skip = other.chars[next..].iter().position(|o| o == c)?;- map.push(next + skip);+ }+ for l in run {+ loose[l] = Some(run_from..shown.starts.len() - 1);+ }+ Alignment { map, loose }+ }++ /// Maps one logical line onto `shown` at or after rendered line `at`.+ /// Returns the rendered line it starts on and the one after it ends.+ fn fit(+ &self,+ line: usize,+ shown: &Glyphs,+ at: usize,+ map: &mut [usize],+ ) -> Option<(usize, usize)> {+ let from = self.starts[line];+ let own = &self.chars[from..self.starts[line + 1]];+ let rule = own.iter().all(|&c| is_rule_glyph(c));+ for start in at..shown.starts.len() - 1 {+ let (a, b) = (shown.starts[start], shown.starts[start + 1]);+ if a == b {+ continue;+ }+ if rule {+ if shown.chars[a..b].iter().all(|&c| is_rule_glyph(c)) {+ for k in 0..own.len() {+ map[from + k] = a + k.min(b - a - 1);+ }+ return Some((start, start + 1));+ }+ continue;+ }+ if shown.chars[a] != own[0] {+ continue;+ }+ // The first line that opens right decides: a later one has less+ // to match from, so it could not succeed where this one did not.+ let mut next = a;+ for (k, c) in own.iter().enumerate() {+ let skip = shown.chars[next..].iter().position(|o| o == c)?;+ map[from + k] = next + skip;next += skip + 1;}+ return Some((start, shown.at[next - 1].0 + 1));}- Some(map)+ None}}+/// How the logical lines of an unwrapped text sit among the rendered lines.+struct Alignment {+ /// For each character of the unwrapped text, the index of the same one in+ /// the rendered text. Meaningless for a loose line.+ map: Vec<usize>,+ /// Per logical line: `Some` when it could not be lined up, holding the+ /// rendered lines it can only be somewhere among.+ loose: Vec<Option<Range<usize>>>,+}+/// Glyphs a horizontal rule or a box border is drawn with.fn is_rule_glyph(c: char) -> bool {matches!(c, '-' | '+' | '|' | '=' | '_') || ('\u{2500}'..='\u{257F}').contains(&c)@@ -656,6 +744,41 @@ mod tests {assert_eq!(covered(&shown, &found[0]), vec!["code"]);}+ // A table row whose cells wrapped interleaves their lines, so it cannot be+ // lined up with its unwrapped text. Only that row loses wrap awareness: a+ // term wrapped elsewhere in the answer is still found, and the lines+ // around it keep their logical index, so a hit made at a width where the+ // table fit still names the same line after a resize.+ #[test]+ fn one_line_that_cannot_be_lined_up_does_not_cost_the_rest_its_matching() {+ let shown = [+ "the quick brown",+ "fox jumps",+ "| a | c |",+ "| b | d |",+ "tail text",+ ];+ let whole = flat(&["the quick brown fox jumps", "| a b | c d |", "tail text"]);+ let found = occurrences(&rows(&shown), Some(&whole), "brown fox");+ assert_eq!(found.len(), 1);+ assert_eq!(found[0].logical, 0);+ assert_eq!(covered(&shown, &found[0]), vec!["brown", "fox"]);++ let found = occurrences(&rows(&shown), Some(&whole), "tail");+ assert_eq!(found.len(), 1);+ assert_eq!(+ found[0].logical, 2,+ "the logical line, not the rendered one"+ );+ assert_eq!(covered(&shown, &found[0]), vec!["tail"]);++ // Inside the row that did not line up, what is on screen is searched.+ let found = occurrences(&rows(&shown), Some(&whole), "d");+ assert_eq!(found.len(), 1);+ assert_eq!(found[0].logical, 1);+ assert_eq!(found[0].pieces, vec![(3, 6..7)]);+ }+#[test]fn a_wrapped_term_is_painted_on_both_lines() {let shown = ["say hel", "lo there"];