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
22 changes: 22 additions & 0 deletions openhands/automation/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ def _resolve_distinct_id(
)


def _failure_kind_from_error_detail(error_detail: str | None) -> str | None:
"""Coarse, non-identifying failure category derived from stored prefixes.

``error_detail`` itself is free text and must never reach telemetry; only
the closed set of prefixes this service writes is mapped.
"""
if not error_detail:
return None
if error_detail.startswith("Timed out: verification failed"):
return "verification_failed"
if error_detail.startswith("Timed out:"):
return "timeout"
if error_detail.startswith("Cancelled"):
return "cancelled"
if error_detail.startswith("Failed to get execution context"):
return "dispatch_error"
return "execution_error"


@lru_cache(maxsize=1)
def _installed_sdk_version() -> str:
# Only successful lookups are cached: lru_cache does not memoize exceptions.
Expand Down Expand Up @@ -350,6 +369,9 @@ def _base_properties(
"has_conversation_id": bool(run.conversation_id),
}
)
failure_kind = _failure_kind_from_error_detail(run.error_detail)
if failure_kind is not None:
properties["failure_kind"] = failure_kind
properties.setdefault("automation_id", str(run.automation_id))
if run.started_at and run.completed_at:
duration_ms = int(
Expand Down
39 changes: 39 additions & 0 deletions tests/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,45 @@ def test_base_properties_normalizes_sqlite_run_timestamps(monkeypatch):
assert properties["duration_ms"] == 2000


@pytest.mark.parametrize(
("error_detail", "expected"),
[
(None, None),
("", None),
("Timed out: verification failed: boom", "verification_failed"),
("Timed out: sandbox stalled", "timeout"),
("Cancelled by user", "cancelled"),
("Failed to get execution context", "dispatch_error"),
("Script exited with code 1", "execution_error"),
],
)
def test_failure_kind_from_error_detail(error_detail, expected):
assert telemetry._failure_kind_from_error_detail(error_detail) == expected


def test_base_properties_includes_derived_failure_kind(monkeypatch):
monkeypatch.setattr(
telemetry,
"get_server_version_info",
lambda **kwargs: {"package_version": "test", "sdk_version": "test"},
)
automation = _automation()
run = _run(automation)
run.status = AutomationRunStatus.FAILED
run.error_detail = "Timed out: sandbox stalled; secret path /home/user/x"

properties = telemetry._base_properties(
request_context=TelemetryRequestContext(),
user=None,
automation=automation,
run=run,
backend_distinct_id="automation-backend:test",
)

assert properties["failure_kind"] == "timeout"
assert "secret path" not in str(properties.values())


def test_base_properties_report_run_as_kind(monkeypatch):
monkeypatch.setattr(
telemetry,
Expand Down
Loading