fix(logs): Make empty log pruning more robust by including sessions with missing metadata
This commit is contained in:
@@ -64,6 +64,19 @@ class TestLogPruningHeuristic(unittest.TestCase):
|
||||
self.assertNotIn("recent_not_empty", session_ids)
|
||||
self.assertNotIn("old_w", session_ids)
|
||||
|
||||
def test_get_old_non_whitelisted_sessions_includes_sessions_without_metadata(self) -> None:
|
||||
now = datetime.now()
|
||||
cutoff_time = now - timedelta(days=7)
|
||||
|
||||
# Recent, not whitelisted, NO metadata (should be included)
|
||||
# _create_session without message_count/size_kb will leave metadata=None
|
||||
self._create_session("recent_no_metadata", now - timedelta(days=1))
|
||||
|
||||
sessions = self.registry.get_old_non_whitelisted_sessions(cutoff_time)
|
||||
session_ids = {s['session_id'] for s in sessions}
|
||||
|
||||
self.assertIn("recent_no_metadata", session_ids)
|
||||
|
||||
def test_prune_removes_empty_sessions_regardless_of_age(self) -> None:
|
||||
now = datetime.now()
|
||||
|
||||
@@ -86,5 +99,25 @@ class TestLogPruningHeuristic(unittest.TestCase):
|
||||
self.assertFalse(os.path.exists(session_path))
|
||||
self.assertNotIn(session_id, self.registry.data)
|
||||
|
||||
def test_prune_removes_sessions_without_metadata_regardless_of_age(self) -> None:
|
||||
now = datetime.now()
|
||||
session_id = "recent_no_metadata_to_prune"
|
||||
session_path = os.path.join(self.logs_dir, session_id)
|
||||
os.makedirs(session_path, exist_ok=True)
|
||||
# Actual file size 0
|
||||
with open(os.path.join(session_path, "comms.log"), "w") as f:
|
||||
pass
|
||||
|
||||
self.registry.register_session(session_id, session_path, now - timedelta(hours=1))
|
||||
# NO metadata update
|
||||
|
||||
self.assertTrue(os.path.exists(session_path))
|
||||
|
||||
# Prune with max_age_days=30
|
||||
self.pruner.prune(max_age_days=30, min_size_kb=1)
|
||||
|
||||
self.assertFalse(os.path.exists(session_path))
|
||||
self.assertNotIn(session_id, self.registry.data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user