diff --git a/src/compact.py b/src/compact.py
index 0d75012..ece71a1 100644
--- a/src/compact.py
+++ b/src/compact.py
@@ -238,7 +238,7 @@ def format_compact_summary(summary: str) -> str:
content = match.group(1).strip()
formatted = re.sub(
r'[\s\S]*?',
- f'Summary:\n{content}',
+ lambda _: f'Summary:\n{content}',
formatted,
)
diff --git a/tests/test_compact.py b/tests/test_compact.py
index 66709ca..b35d963 100644
--- a/tests/test_compact.py
+++ b/tests/test_compact.py
@@ -112,6 +112,12 @@ class TestFormatCompactSummary(unittest.TestCase):
self.assertNotIn('Line 1', formatted)
self.assertIn('Final summary', formatted)
+ def test_summary_content_keeps_regex_backslashes_literal(self) -> None:
+ raw = r'Regex examples: \s+ and \1 should stay literal.'
+ formatted = format_compact_summary(raw)
+ self.assertIn(r'\s+', formatted)
+ self.assertIn(r'\1', formatted)
+
class TestGetCompactUserSummaryMessage(unittest.TestCase):
"""Tests for the post-compact user message builder."""