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
8 changes: 7 additions & 1 deletion openhands/sdk/llm/utils/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ def _log_completion(
if not self.log_dir:
return
try:
os.makedirs(self.log_dir, exist_ok=True)
# Only log if directory exists and is writable.
# Do not create directories implicitly.
if not os.path.isdir(self.log_dir):
raise FileNotFoundError(f"log_dir does not exist: {self.log_dir}")
if not os.access(self.log_dir, os.W_OK):
raise PermissionError(f"log_dir is not writable: {self.log_dir}")

fname = os.path.join(
self.log_dir,
f"{self.model_name.replace('/', '__')}-{time.time():.3f}.json",
Expand Down
2 changes: 1 addition & 1 deletion openhands/tools/execute_bash/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def to_ps1_prompt(cls) -> str:
"username": r"\u",
"hostname": r"\h",
"working_dir": r"$(pwd)",
"py_interpreter_path": r'$(which python 2>/dev/null || echo "")',
"py_interpreter_path": r'$(command -v python || echo "")',
},
indent=2,
)
Expand Down
41 changes: 28 additions & 13 deletions tests/sdk/llm/test_llm_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,22 +425,37 @@ def test_log_completion_model_name_sanitization(self, mock_metrics, mock_respons

def test_log_completion_error_handling(self, mock_metrics, mock_response):
"""Test logging error handling."""
telemetry = Telemetry(
model_name="gpt-4o",
log_enabled=True,
log_dir="/invalid/path/that/does/not/exist",
metrics=mock_metrics,
)
# Use a guaranteed-invalid log_dir by pointing at a regular file path
# rather than a directory. This avoids reliance on environment-specific
# directories that may unexpectedly exist or be writable in CI.
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
bogus_path = tmp.name
telemetry = Telemetry(
model_name="gpt-4o",
log_enabled=True,
log_dir=bogus_path,
metrics=mock_metrics,
)

telemetry.on_request({})
telemetry.on_request({})

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
telemetry._log_completion(mock_response, 0.25)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
telemetry._log_completion(mock_response, 0.25)

# Should issue a warning but not crash
assert len(w) == 1
assert "Telemetry logging failed" in str(w[0].message)
# Should issue a warning but not crash
assert len(w) == 1
assert "Telemetry logging failed" in str(w[0].message)
finally:
try:
tmp.close()
except Exception:
pass
try:
os.unlink(tmp.name)
except Exception:
pass


class TestTelemetryIntegration:
Expand Down