Skip to content

Build and run agents you can see, understand and trust.

fix(tui): count changed lines that start with a diff marker (#2830)

lihongyuan998537813a496d
diff --git a/src/agentscope/tui/_messages.py b/src/agentscope/tui/_messages.py
index 1e9a00be2..167fde1e1 100644
--- a/src/agentscope/tui/_messages.py
+++ b/src/agentscope/tui/_messages.py
@@ -296,14 +296,22 @@ def _group_tool_calls(content: Iterable[ContentBlock]) -> list[_DisplayBlock]:
def _diff_stats(diff: str) -> tuple[int, int]:
- insertions = 0
- deletions = 0
- for line in diff.splitlines():
- if line.startswith("+") and not line.startswith("+++"):
- insertions += 1
- elif line.startswith("-") and not line.startswith("---"):
- deletions += 1
- return insertions, deletions
+ """Count the changed lines in a unified diff.
+
+ Args:
+ diff (`str`):
+ The unified diff recorded in a tool result's ``metadata``.
+
+ Returns:
+ `tuple[int, int]`: The added and removed line counts.
+ """
+ lines = diff.splitlines()
+ # Only the leading ``---``/``+++`` pair is a header, not every such line
+ body = lines[2:] if lines[:1] and lines[0].startswith("--- ") else lines
+ return (
+ sum(_.startswith("+") for _ in body),
+ sum(_.startswith("-") for _ in body),
+ )
def _file_path(call: ToolCallBlock) -> str | None:
diff --git a/tests/tui_test.py b/tests/tui_test.py
index bd2658716..81369693a 100644
--- a/tests/tui_test.py
+++ b/tests/tui_test.py
@@ -61,6 +61,7 @@ from agentscope.tui._ask_user import AskUserUI
from agentscope.tui._chat import ComposerUI, HitlUI, _ComposerTextArea
from agentscope.tui._launcher import _AgentScopeTUI, _RealtimeTUI
from agentscope.tui._messages import (
+ _diff_stats,
MessageUI,
TextBlockUI,
ThinkingUI,
@@ -414,6 +415,11 @@ class MessagesUITest(unittest.IsolatedAsyncioTestCase):
self.assertIs(text_widget, app.query_one(TextBlockUI))
self.assertEqual(ui.messages[0].get_text_content(), "hello")
+ def test_diff_stats_counts_lines_starting_with_a_marker(self) -> None:
+ """A removed ``---`` and an added ``++counter;`` both count."""
+ diff = "--- a\n+++ b\n@@ -1 +1 @@\n----\n+++counter;\n"
+ self.assertEqual(_diff_stats(diff), (1, 1))
+
class ChatUITest(unittest.IsolatedAsyncioTestCase):
async def test_submission_does_not_append_history(self) -> None: