diff --git a/api/streaming.py b/api/streaming.py index 8251c2e42..c31883d16 100644 --- a/api/streaming.py +++ b/api/streaming.py @@ -74,6 +74,11 @@ def _session_payload_with_full_messages(session, *, tool_calls=None): return raw +def _compact_for_echo_compare(value: str) -> str: + """Normalize visible stream text for duplicate echo detection.""" + return re.sub(r'\s+', '', str(value or '')) + + # Global lock for os.environ writes. Per-session locks (_agent_lock) prevent # concurrent runs of the SAME session, but two DIFFERENT sessions can still # interleave their os.environ writes. This global lock serializes the env @@ -6347,9 +6352,6 @@ def _run_agent_streaming( stats.setdefault('estimated', False) put('metering', stats) - def _compact_for_echo_compare(value: str) -> str: - return re.sub(r'\s+', '', str(value or '')) - def _is_visible_output_echo(text: str) -> bool: candidate = _compact_for_echo_compare(text) if not candidate: diff --git a/tests/test_run_journal_streaming_static.py b/tests/test_run_journal_streaming_static.py index 6ab50d89c..a25421715 100644 --- a/tests/test_run_journal_streaming_static.py +++ b/tests/test_run_journal_streaming_static.py @@ -1,5 +1,7 @@ from pathlib import Path +from api.streaming import _compact_for_echo_compare + def test_streaming_initializes_one_run_journal_writer_per_stream(): src = Path("api/streaming.py").read_text(encoding="utf-8") @@ -24,12 +26,7 @@ def test_streaming_journals_sse_events_before_queue_delivery(): def test_visible_process_echo_compare_ignores_all_whitespace(): - src = Path("api/streaming.py").read_text(encoding="utf-8") - helper_idx = src.index("def _compact_for_echo_compare(value: str) -> str:") - helper = src[helper_idx:src.index("def _is_visible_output_echo", helper_idx)] + token_text = "先把 issue 4249 拉下来\n\n先看正文和评论" + interim_text = "先把 issue 4249 拉下来先看正文和评论" - assert "re.sub(r'\\s+', '', str(value or ''))" in helper, ( - "Visible process-prose echo detection must ignore paragraph and line-break " - "formatting so interim_assistant does not duplicate token prose that only " - "differs by whitespace." - ) + assert _compact_for_echo_compare(token_text) == _compact_for_echo_compare(interim_text)