|
9 | 9 | from gptme.llm.models import get_default_model, get_model |
10 | 10 | from gptme.message import Message, len_tokens |
11 | 11 | from gptme.tools.autocompact import ( |
12 | | - _get_backup_name, |
| 12 | + _get_compacted_name, |
13 | 13 | auto_compact_log, |
14 | 14 | should_auto_compact, |
15 | 15 | ) |
@@ -161,97 +161,97 @@ def test_auto_compact_preserves_pinned_messages(): |
161 | 161 | assert compacted[2].pinned |
162 | 162 |
|
163 | 163 |
|
164 | | -def test_get_backup_name_no_suffix(): |
165 | | - """Test backup name generation for conversation without existing suffix.""" |
| 164 | +def test_get_compacted_name_no_suffix(): |
| 165 | + """Test compacted name generation for conversation without existing suffix.""" |
166 | 166 | import re |
167 | 167 |
|
168 | | - name = _get_backup_name("2025-10-13-flying-yellow-alien") |
169 | | - # Should match: base-name-before-compact-XXXX where XXXX is 4 hex chars |
170 | | - assert re.match( |
171 | | - r"^2025-10-13-flying-yellow-alien-before-compact-[0-9a-f]{4}$", name |
172 | | - ) |
| 168 | + name = _get_compacted_name("2025-10-13-flying-yellow-alien") |
| 169 | + # Should match: base-name-compacted-YYYYMMDD-HHMMSS |
| 170 | + assert re.match(r"^2025-10-13-flying-yellow-alien-compacted-\d{8}-\d{6}$", name) |
173 | 171 |
|
174 | 172 |
|
175 | | -def test_get_backup_name_one_suffix(): |
176 | | - """Test backup name generation when suffix already exists (gets new unique suffix).""" |
| 173 | +def test_get_compacted_name_one_suffix(): |
| 174 | + """Test compacted name generation when suffix already exists (gets new unique suffix).""" |
177 | 175 | import re |
178 | 176 |
|
179 | | - name = _get_backup_name("2025-10-13-flying-yellow-alien-before-compact") |
180 | | - # Should strip old suffix and add new one with random suffix |
181 | | - assert re.match( |
182 | | - r"^2025-10-13-flying-yellow-alien-before-compact-[0-9a-f]{4}$", name |
| 177 | + name = _get_compacted_name( |
| 178 | + "2025-10-13-flying-yellow-alien-compacted-20251028-120000" |
183 | 179 | ) |
| 180 | + # Should strip old timestamp suffix and add new timestamp |
| 181 | + assert re.match(r"^2025-10-13-flying-yellow-alien-compacted-\d{8}-\d{6}$", name) |
184 | 182 |
|
185 | 183 |
|
186 | | -def test_get_backup_name_multiple_suffixes(): |
187 | | - """Test backup name generation with multiple accumulated suffixes (should strip all).""" |
| 184 | +def test_get_compacted_name_multiple_suffixes(): |
| 185 | + """Test compacted name generation with multiple accumulated timestamp suffixes (should strip all).""" |
188 | 186 | import re |
189 | 187 |
|
190 | | - name = _get_backup_name( |
191 | | - "2025-10-13-flying-yellow-alien-before-compact-before-compact-before-compact" |
192 | | - ) |
193 | | - assert re.match( |
194 | | - r"^2025-10-13-flying-yellow-alien-before-compact-[0-9a-f]{4}$", name |
| 188 | + name = _get_compacted_name( |
| 189 | + "2025-10-13-flying-yellow-alien-compacted-20251027-100000-compacted-20251028-110000-compacted-20251028-120000" |
195 | 190 | ) |
| 191 | + assert re.match(r"^2025-10-13-flying-yellow-alien-compacted-\d{8}-\d{6}$", name) |
196 | 192 |
|
197 | 193 |
|
198 | | -def test_get_backup_name_edge_cases(): |
199 | | - """Test backup name generation with various edge cases.""" |
| 194 | +def test_get_compacted_name_edge_cases(): |
| 195 | + """Test compacted name generation with various edge cases.""" |
200 | 196 | import re |
201 | 197 |
|
202 | 198 | # Short name |
203 | | - name = _get_backup_name("conv") |
204 | | - assert re.match(r"^conv-before-compact-[0-9a-f]{4}$", name) |
| 199 | + name = _get_compacted_name("conv") |
| 200 | + assert re.match(r"^conv-compacted-\d{8}-\d{6}$", name) |
205 | 201 |
|
206 | 202 | # Name containing 'compact' but not as suffix |
207 | | - name = _get_backup_name("compact-test") |
208 | | - assert re.match(r"^compact-test-before-compact-[0-9a-f]{4}$", name) |
| 203 | + name = _get_compacted_name("compact-test") |
| 204 | + assert re.match(r"^compact-test-compacted-\d{8}-\d{6}$", name) |
209 | 205 |
|
210 | 206 | # Name ending with similar but different suffix |
211 | | - name = _get_backup_name("test-before-compaction") |
212 | | - assert re.match(r"^test-before-compaction-before-compact-[0-9a-f]{4}$", name) |
| 207 | + name = _get_compacted_name("test-before-compaction") |
| 208 | + assert re.match(r"^test-before-compaction-compacted-\d{8}-\d{6}$", name) |
| 209 | + |
213 | 210 |
|
| 211 | +def test_get_compacted_name_uniqueness(): |
| 212 | + """Test that multiple calls produce unique compacted names with timestamps.""" |
| 213 | + import time |
214 | 214 |
|
215 | | -def test_get_backup_name_uniqueness(): |
216 | | - """Test that multiple calls produce unique backup names.""" |
217 | | - name1 = _get_backup_name("my-conversation") |
218 | | - name2 = _get_backup_name("my-conversation") |
219 | | - name3 = _get_backup_name("my-conversation") |
| 215 | + name1 = _get_compacted_name("my-conversation") |
| 216 | + time.sleep(1.1) # Ensure different second for timestamp |
| 217 | + name2 = _get_compacted_name("my-conversation") |
| 218 | + time.sleep(1.1) # Ensure different second for timestamp |
| 219 | + name3 = _get_compacted_name("my-conversation") |
220 | 220 |
|
221 | | - # All should have the same base but different random suffixes |
| 221 | + # All should have the same base but different timestamps |
222 | 222 | assert name1 != name2 |
223 | 223 | assert name2 != name3 |
224 | 224 | assert name1 != name3 |
225 | 225 |
|
226 | | - # All should start with the same base |
227 | | - assert name1.startswith("my-conversation-before-compact-") |
228 | | - assert name2.startswith("my-conversation-before-compact-") |
229 | | - assert name3.startswith("my-conversation-before-compact-") |
| 226 | + # All should have compacted suffix with timestamp |
| 227 | + assert name1.startswith("my-conversation-compacted-") |
| 228 | + assert name2.startswith("my-conversation-compacted-") |
| 229 | + assert name3.startswith("my-conversation-compacted-") |
230 | 230 |
|
231 | 231 |
|
232 | | -def test_get_backup_name_with_hex_suffix(): |
233 | | - """Test that backup names with hex suffixes are correctly stripped. |
| 232 | +def test_get_compacted_name_with_hex_suffix(): |
| 233 | + """Test that compacted names with timestamp suffixes are correctly stripped. |
234 | 234 |
|
235 | 235 | This is a regression test for the bug where: |
236 | | - "my-conversation-before-compact-a7c9" would become |
237 | | - "my-conversation-before-compact-a7c9-before-compact-k2p5" |
| 236 | + "my-conversation-compacted-20251028-120000" would become |
| 237 | + "my-conversation-compacted-20251028-120000-compacted-20251029-130000" |
238 | 238 | instead of |
239 | | - "my-conversation-before-compact-k2p5" |
| 239 | + "my-conversation-compacted-20251029-140000" |
240 | 240 | """ |
241 | 241 | import re |
242 | 242 |
|
243 | 243 | # Test with a backup that has a valid hex suffix (only 0-9a-f) |
244 | | - name = _get_backup_name("my-conversation-before-compact-a7c9") |
| 244 | + name = _get_compacted_name("my-conversation-compacted-20251028-120000") |
245 | 245 | # Should strip the old suffix and add a new one |
246 | | - assert re.match(r"^my-conversation-before-compact-[0-9a-f]{4}$", name) |
247 | | - # Should NOT contain the old hex suffix |
248 | | - assert "a7c9" not in name |
| 246 | + assert re.match(r"^my-conversation-compacted-\d{8}-\d{6}$", name) |
| 247 | + # Should NOT contain the old timestamp |
| 248 | + assert "20251028-120000" not in name |
249 | 249 |
|
250 | 250 |
|
251 | | -def test_get_backup_name_empty_string(): |
252 | | - """Test backup name generation with empty string raises ValueError.""" |
| 251 | +def test_get_compacted_name_empty_string(): |
| 252 | + """Test compacted name generation with empty string raises ValueError.""" |
253 | 253 | with pytest.raises(ValueError, match="conversation name cannot be empty"): |
254 | | - _get_backup_name("") |
| 254 | + _get_compacted_name("") |
255 | 255 |
|
256 | 256 |
|
257 | 257 | if __name__ == "__main__": |
|
0 commit comments