Skip to content

Commit d46b129

Browse files
ayaangazaliclaude
andcommitted
test: isolate the chunking tests from the shared conversations dir
test_dynamic_chunking_handles_an_empty_history asserted an empty history and got one that already had messages in it. It passes alone and fails in a whole-file run, which is why the branch looked clean: the branch's base predates #1925. Conversation defaults to name="conversation-test", and since #1925 a bare Conversation() loads conversations/conversation_conversation-test.json when that file exists. An earlier test in this module writes it, and it survives between runs, so every bare Conversation() in these six tests started with a history it never added. Successfully loaded conversation from .../conversations/conversation_conversation-test.json AssertionError: assert 'user: Hello\n...\nuser: Hello' == '' _chunking_conversation pins conversations_dir to pytest's tmp_path, so each test gets an empty directory and cannot inherit or leak one. That matches what the rest of this file already does with setup_temp_conversations_dir. Verified: whole file green on repeat runs and under random ordering, and the merge with master now has the same failure set as master (0 = 0). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 622db6f commit d46b129

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

tests/structs/test_conversation.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -900,17 +900,32 @@ def test_time_enabled_end_to_end(tmp_path):
900900
datetime.fromisoformat(ts)
901901

902902

903-
def _overflowing_conversation(context_length=512, messages=60):
903+
def _chunking_conversation(tmp_path, **kwargs):
904+
"""A Conversation that cannot inherit another test's save file.
905+
906+
Conversation defaults to ``name="conversation-test"``, and since #1925 a
907+
bare ``Conversation()`` loads
908+
``conversations/conversation_conversation-test.json`` when it exists. That
909+
file is written by an earlier test in this module and survives between
910+
runs, so these tests would start with a history they never added --
911+
silently, and only when run as part of the file.
912+
"""
913+
return Conversation(conversations_dir=str(tmp_path), **kwargs)
914+
915+
916+
def _overflowing_conversation(
917+
tmp_path, context_length=512, messages=60
918+
):
904919
"""A conversation whose history is well past ``context_length``."""
905-
conv = Conversation(
906-
context_length=context_length, time_enabled=True
920+
conv = _chunking_conversation(
921+
tmp_path, context_length=context_length, time_enabled=True
907922
)
908923
for index in range(messages):
909924
conv.add("user", f"message {index} " + "word " * 100)
910925
return conv
911926

912927

913-
def test_dynamic_chunking_stays_within_context_length():
928+
def test_dynamic_chunking_stays_within_context_length(tmp_path):
914929
"""The trimmed history must fit the window it was trimmed for.
915930
916931
Swept across window and message sizes because the per-message budget
@@ -919,8 +934,10 @@ def test_dynamic_chunking_stays_within_context_length():
919934
"""
920935
for context_length in (64, 200, 512, 2048):
921936
for words in (5, 40, 100):
922-
conv = Conversation(
923-
context_length=context_length, time_enabled=True
937+
conv = _chunking_conversation(
938+
tmp_path,
939+
context_length=context_length,
940+
time_enabled=True,
924941
)
925942
for index in range(40):
926943
conv.add("user", f"m{index} " + "word " * words)
@@ -932,9 +949,9 @@ def test_dynamic_chunking_stays_within_context_length():
932949
), f"overflow at context_length={context_length}, words={words}"
933950

934951

935-
def test_dynamic_chunking_keeps_whole_messages():
952+
def test_dynamic_chunking_keeps_whole_messages(tmp_path):
936953
"""Trimming happens on message boundaries, not mid-message."""
937-
conv = _overflowing_conversation(messages=60)
954+
conv = _overflowing_conversation(tmp_path, messages=60)
938955
result = conv.return_history_as_string()
939956

940957
# Every complete message starts with its [timestamp] under time_enabled.
@@ -944,7 +961,9 @@ def test_dynamic_chunking_keeps_whole_messages():
944961
assert "message 59 " in result
945962

946963

947-
def test_dynamic_chunking_work_is_bounded_by_the_window(monkeypatch):
964+
def test_dynamic_chunking_work_is_bounded_by_the_window(
965+
monkeypatch, tmp_path
966+
):
948967
"""Trimming must not re-tokenize the whole transcript on every read."""
949968
import swarms.structs.conversation as conversation_module
950969

@@ -959,20 +978,26 @@ def recording(text, *args, **kwargs):
959978
conversation_module, "count_tokens", recording
960979
)
961980

962-
_overflowing_conversation(messages=30).return_history_as_string()
981+
_overflowing_conversation(
982+
tmp_path, messages=30
983+
).return_history_as_string()
963984
small = sum(tokenized)
964985

965986
tokenized.clear()
966-
_overflowing_conversation(messages=240).return_history_as_string()
987+
_overflowing_conversation(
988+
tmp_path, messages=240
989+
).return_history_as_string()
967990
large = sum(tokenized)
968991

969992
# 8x the transcript, same window: tokenized characters should stay flat.
970993
assert large < small * 2
971994

972995

973-
def test_dynamic_chunking_leaves_a_short_history_alone():
996+
def test_dynamic_chunking_leaves_a_short_history_alone(tmp_path):
974997
"""Under the limit, the full history is returned verbatim."""
975-
conv = Conversation(context_length=8192, time_enabled=True)
998+
conv = _chunking_conversation(
999+
tmp_path, context_length=8192, time_enabled=True
1000+
)
9761001
conv.add("user", "hello there")
9771002
conv.add("assistant", "general kenobi")
9781003

@@ -982,19 +1007,23 @@ def test_dynamic_chunking_leaves_a_short_history_alone():
9821007
)
9831008

9841009

985-
def test_dynamic_chunking_handles_a_single_oversized_message():
1010+
def test_dynamic_chunking_handles_a_single_oversized_message(
1011+
tmp_path,
1012+
):
9861013
"""One message bigger than the window is trimmed, not dropped."""
987-
conv = Conversation(context_length=40, time_enabled=False)
1014+
conv = _chunking_conversation(
1015+
tmp_path, context_length=40, time_enabled=False
1016+
)
9881017
conv.add("user", "word " * 4000)
9891018
result = conv.return_history_as_string()
9901019

9911020
assert result
9921021
assert count_tokens(result) <= conv.context_length
9931022

9941023

995-
def test_dynamic_chunking_handles_an_empty_history():
1024+
def test_dynamic_chunking_handles_an_empty_history(tmp_path):
9961025
"""No messages must not raise on the newest-message fallback."""
997-
conv = Conversation(context_length=40)
1026+
conv = _chunking_conversation(tmp_path, context_length=40)
9981027

9991028
assert conv.return_history_as_string() == ""
10001029

0 commit comments

Comments
 (0)