Skip to content

The agent that grows with you

fix: _check_disk_usage_warning runs full rglob on every terminal call

AMATH9d7c2d450ccc
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index 6572174819a..1768f80aed3 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -189,8 +189,24 @@ def _check_vercel_sandbox_requirements(config: dict[str, Any]) -> bool:
return False
+# Cache for disk usage warning to avoid full rglob scan on every call.
+# The check is advisory-only — staleness for up to 5 minutes is acceptable.
+_disk_usage_cache: dict = {"timestamp": 0.0, "result": False}
+_DISK_USAGE_CACHE_TTL = 300.0 # seconds
+
+
def _check_disk_usage_warning():
- """Check if total disk usage exceeds warning threshold."""
+ """Check if total disk usage exceeds warning threshold.
+
+ Result is cached for :data:`_DISK_USAGE_CACHE_TTL` seconds (default:
+ 5 minutes) to avoid an expensive recursive filesystem scan on every
+ terminal command. The check is advisory-only so a stale result is
+ harmless.
+ """
+ import time as _time_mod
+ now = _time_mod.monotonic()
+ if now - _disk_usage_cache["timestamp"] < _DISK_USAGE_CACHE_TTL:
+ return _disk_usage_cache["result"]
try:
scratch_dir = _get_scratch_dir()
@@ -207,14 +223,16 @@ def _check_disk_usage_warning():
total_gb = total_bytes / (1024 ** 3)
- if total_gb > DISK_USAGE_WARNING_THRESHOLD_GB:
+ exceeded = total_gb > DISK_USAGE_WARNING_THRESHOLD_GB
+ if exceeded:
logger.warning("Disk usage (%.1fGB) exceeds threshold (%.0fGB). Consider running cleanup_all_environments().",
total_gb, DISK_USAGE_WARNING_THRESHOLD_GB)
- return True
-
- return False
+ _disk_usage_cache["timestamp"] = _time_mod.monotonic()
+ _disk_usage_cache["result"] = exceeded
+ return exceeded
except Exception as e:
logger.debug("Disk usage warning check failed: %s", e, exc_info=True)
+ # Don't update cache on error so the next call retries.
return False