Python · 3313 bytes Raw Blame History
1 """Tests for transcript compaction and summary compression."""
2
3 from __future__ import annotations
4
5 from loader.llm.base import Message, Role
6 from loader.runtime.compaction import (
7 SummaryCompressionBudget,
8 build_session_summary,
9 compact_session_messages,
10 compress_summary,
11 resolve_auto_compaction_input_tokens_threshold,
12 )
13
14
15 def test_compress_summary_dedupes_lines_and_collapses_whitespace() -> None:
16 summary = "\n".join(
17 [
18 "Conversation summary:",
19 "- Scope: compact earlier messages.",
20 "- Scope: compact earlier messages.",
21 "- Current work: finish session persistence.",
22 "- Current work: finish session persistence.",
23 ]
24 )
25
26 result = compress_summary(summary, budget=SummaryCompressionBudget())
27
28 assert result.removed_duplicate_lines == 2
29 assert "- Scope: compact earlier messages." in result.summary
30 assert " compact earlier" not in result.summary
31
32
33 def test_compact_session_messages_preserves_recent_messages() -> None:
34 messages = [
35 Message(role=Role.USER, content="First task framing"),
36 Message(role=Role.ASSISTANT, content="Initial plan"),
37 Message(role=Role.USER, content="Focus on runtime quality"),
38 Message(role=Role.ASSISTANT, content="Tracked updated files"),
39 Message(role=Role.USER, content="Verify the result"),
40 Message(role=Role.ASSISTANT, content="Verification passed"),
41 ]
42
43 result = compact_session_messages(
44 messages,
45 keep_last_messages=4,
46 current_task="Improve Loader runtime continuity",
47 )
48
49 assert result is not None
50 assert result.removed_message_count == 2
51 assert [message.content for message in result.messages[-4:]] == [
52 message.content for message in messages[-4:]
53 ]
54 assert result.messages[0].content.startswith("[COMPACTED CONTEXT]")
55 assert "Continuation instructions:" in result.messages[0].content
56
57
58 def test_build_session_summary_skips_nested_compacted_context_content() -> None:
59 messages = [
60 Message(
61 role=Role.USER,
62 content=(
63 "[COMPACTED CONTEXT]\nConversation summary:\n"
64 "- Scope: older work\n- Current work: old state"
65 ),
66 ),
67 Message(role=Role.ASSISTANT, content="Read the chapter index."),
68 Message(role=Role.USER, content="Update the chapter links."),
69 ]
70
71 summary = build_session_summary(
72 messages,
73 previous_summary="[COMPACTED CONTEXT]\nConversation summary:\n- Scope: older work",
74 current_task="Repair the table of contents links",
75 )
76
77 assert "Recent user requests: [COMPACTED CONTEXT]" not in summary
78 assert "Pending work: [COMPACTED CONTEXT]" not in summary
79 assert "- Previously compacted context retained." in summary
80
81
82 def test_resolve_auto_compaction_threshold_uses_context_window_as_upper_bound() -> None:
83 assert resolve_auto_compaction_input_tokens_threshold(
84 100_000,
85 context_window=131_072,
86 ) == 98_304
87 assert resolve_auto_compaction_input_tokens_threshold(
88 100_000,
89 context_window=262_144,
90 ) == 100_000
91 assert resolve_auto_compaction_input_tokens_threshold(
92 100_000,
93 context_window=8_192,
94 ) == 12_000