Skip to content

Commit a8a0426

Browse files
authored
Fix calculation for num-lines contributed by q-cli (#2738)
1 parent 8733a51 commit a8a0426

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

crates/chat-cli/src/cli/chat/line_tracker.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ pub struct FileLineTracker {
1313
pub before_fswrite_lines: usize,
1414
/// Line count after `fs_write` executes
1515
pub after_fswrite_lines: usize,
16+
/// Lines added by agent in the current operation
17+
pub lines_added_by_agent: usize,
18+
/// Lines removed by agent in the current operation
19+
pub lines_removed_by_agent: usize,
1620
/// Whether or not this is the first `fs_write` invocation
1721
pub is_first_write: bool,
1822
}
@@ -23,6 +27,8 @@ impl Default for FileLineTracker {
2327
prev_fswrite_lines: 0,
2428
before_fswrite_lines: 0,
2529
after_fswrite_lines: 0,
30+
lines_added_by_agent: 0,
31+
lines_removed_by_agent: 0,
2632
is_first_write: true,
2733
}
2834
}
@@ -34,7 +40,6 @@ impl FileLineTracker {
3440
}
3541

3642
pub fn lines_by_agent(&self) -> isize {
37-
let lines = (self.after_fswrite_lines as isize) - (self.before_fswrite_lines as isize);
38-
lines.abs()
43+
(self.lines_added_by_agent + self.lines_removed_by_agent) as isize
3944
}
4045
}

crates/chat-cli/src/cli/chat/tools/fs_write.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,57 @@ impl FsWrite {
247247

248248
let tracker = line_tracker.entry(path.to_string_lossy().to_string()).or_default();
249249
tracker.after_fswrite_lines = after_lines;
250+
251+
// Calculate actual lines added and removed by analyzing the diff
252+
let (lines_added, lines_removed) = self.calculate_diff_lines(os).await?;
253+
tracker.lines_added_by_agent = lines_added;
254+
tracker.lines_removed_by_agent = lines_removed;
255+
250256
tracker.is_first_write = false;
251257

252258
Ok(())
253259
}
254260

261+
async fn calculate_diff_lines(&self, os: &Os) -> Result<(usize, usize)> {
262+
let path = self.path(os);
263+
264+
let result = match self {
265+
FsWrite::Create { .. } => {
266+
// For create operations, all lines in the new file are added
267+
let new_content = os.fs.read_to_string(&path).await?;
268+
let lines_added = new_content.lines().count();
269+
(lines_added, 0)
270+
},
271+
FsWrite::StrReplace { old_str, new_str, .. } => {
272+
// Use actual diff analysis for accurate line counting
273+
let diff = similar::TextDiff::from_lines(old_str, new_str);
274+
let mut lines_added = 0;
275+
let mut lines_removed = 0;
276+
277+
for change in diff.iter_all_changes() {
278+
match change.tag() {
279+
similar::ChangeTag::Insert => lines_added += 1,
280+
similar::ChangeTag::Delete => lines_removed += 1,
281+
similar::ChangeTag::Equal => {},
282+
}
283+
}
284+
(lines_added, lines_removed)
285+
},
286+
FsWrite::Insert { new_str, .. } => {
287+
// For insert operations, all lines in new_str are added
288+
let lines_added = new_str.lines().count();
289+
(lines_added, 0)
290+
},
291+
FsWrite::Append { new_str, .. } => {
292+
// For append operations, all lines in new_str are added
293+
let lines_added = new_str.lines().count();
294+
(lines_added, 0)
295+
},
296+
};
297+
298+
Ok(result)
299+
}
300+
255301
pub fn queue_description(&self, os: &Os, output: &mut impl Write) -> Result<()> {
256302
let cwd = os.env.current_dir()?;
257303
self.print_relative_path(os, output)?;

0 commit comments

Comments
 (0)