diff --git a/src/langbot/pkg/utils/logcache.py b/src/langbot/pkg/utils/logcache.py index 99c6c76a8..b48d070c2 100644 --- a/src/langbot/pkg/utils/logcache.py +++ b/src/langbot/pkg/utils/logcache.py @@ -57,12 +57,12 @@ class LogCache: start_offset: int, ) -> tuple[str, int, int]: """获取指定页码和偏移量的日志""" - final_logs_str = '' + final_logs: list[str] = [] for page in self.log_pages: if page.number == start_page_number: - final_logs_str += '\n'.join(page.logs[start_offset:]) + final_logs.extend(page.logs[start_offset:]) elif page.number > start_page_number: - final_logs_str += '\n'.join(page.logs) + final_logs.extend(page.logs) - return final_logs_str, page.number, len(page.logs) + return '\n'.join(final_logs), page.number, len(page.logs) diff --git a/tests/unit_tests/utils/test_logcache.py b/tests/unit_tests/utils/test_logcache.py index 03f2b509a..ed4e20757 100644 --- a/tests/unit_tests/utils/test_logcache.py +++ b/tests/unit_tests/utils/test_logcache.py @@ -159,6 +159,17 @@ class TestLogCache: assert 'page0 log 0' in result assert 'page1 log 0' in result + def test_get_log_by_pointer_across_pages_keeps_line_boundaries(self): + """Lines from adjacent pages are separated by a newline, not glued together.""" + cache = LogCache() + logs = [f'log {i}' for i in range(LOG_PAGE_SIZE * 2 + 1)] + for log in logs: + cache.add_log(log) + + result, _, _ = cache.get_log_by_pointer(0, 0) + + assert result.split('\n') == logs + def test_get_log_by_pointer_from_second_page(self): """get_log_by_pointer can start from second page.""" cache = LogCache()