Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions azchess/utils/memory_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ def _check_memory_and_alert(self, device: str) -> None:
self.memory_history.append(memory_usage_gb)
self.history_timestamps.append(current_time)

# Calculate memory ratio
# Calculate memory ratio and human-readable usage string
if memory_limit_gb > 0:
memory_ratio = memory_usage_gb / memory_limit_gb
usage_message = (
f"memory {memory_usage_gb:.1f}/{memory_limit_gb:.1f} GB "
f"({memory_ratio:.0%})"
)
else:
memory_ratio = 0
usage_message = f"memory {memory_usage_gb:.1f} GB (limit unknown)"

# Check for alerts
alert = None
Expand All @@ -132,7 +137,7 @@ def _check_memory_and_alert(self, device: str) -> None:
if current_time - self.last_alert_time > self.alert_cooldown:
alert = MemoryAlert(
alert_type='critical',
message=".1f",
message=usage_message,
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original message was '.1f' which appears to be a corrupted format string. The fix correctly uses usage_message, but this suggests there may have been a systematic issue with message formatting that should be verified across the codebase.

Copilot uses AI. Check for mistakes.
memory_usage_gb=memory_usage_gb,
memory_limit_gb=memory_limit_gb,
timestamp=current_time,
Expand All @@ -153,7 +158,7 @@ def _check_memory_and_alert(self, device: str) -> None:
if current_time - self.last_alert_time > self.alert_cooldown:
alert = MemoryAlert(
alert_type='warning',
message=".1f",
message=usage_message,
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the critical alert, this line was previously '.1f' which is clearly a corrupted format string. The fix correctly uses usage_message.

Copilot uses AI. Check for mistakes.
memory_usage_gb=memory_usage_gb,
memory_limit_gb=memory_limit_gb,
timestamp=current_time,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_memory_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging

from azchess.utils import memory_monitor as memory_monitor_module
from azchess.utils.memory_monitor import MemoryMonitor


def test_memory_alert_messages_include_usage(monkeypatch, caplog):
monitor = MemoryMonitor(warning_threshold=0.5, critical_threshold=0.8, alert_cooldown=0)
device = "cuda"
monitor.device_limits[device] = 10.0

monkeypatch.setattr(memory_monitor_module, "get_memory_usage", lambda d: {"memory_gb": 9.0})
monkeypatch.setattr(memory_monitor_module, "emergency_memory_cleanup", lambda d: None)
monkeypatch.setattr(memory_monitor_module, "clear_memory_cache", lambda d: None)

recorded_alerts = []
monitor.alert_callbacks.append(recorded_alerts.append)

with caplog.at_level(logging.CRITICAL):
monitor._check_memory_and_alert(device)

assert recorded_alerts, "Expected a critical alert to be recorded"
critical_alert = recorded_alerts[-1]
assert critical_alert.alert_type == "critical"
assert critical_alert.message == "memory 9.0/10.0 GB (90%)"
assert any(
message == "MEMORY_ALERT: memory 9.0/10.0 GB (90%)"
for message in caplog.messages
), "Critical MEMORY_ALERT log should include usage details"

caplog.clear()
monkeypatch.setattr(memory_monitor_module, "get_memory_usage", lambda d: {"memory_gb": 7.0})
monitor.last_alert_time -= 1 # allow another alert immediately

with caplog.at_level(logging.WARNING):
monitor._check_memory_and_alert(device)

warning_alert = recorded_alerts[-1]
assert warning_alert.alert_type == "warning"
assert warning_alert.message == "memory 7.0/10.0 GB (70%)"
assert any(
message == "MEMORY_ALERT: memory 7.0/10.0 GB (70%)"
for message in caplog.messages
), "Warning MEMORY_ALERT log should include usage details"
Loading