Skip to content

fix(telemetry): categorize every terminal automation failure (OSS-5810) - #766

Merged
neubig merged 2 commits into
mainfrom
fix/oss-5810-terminal-failure-telemetry
Nov 17, 2026
Merged

neubig merged 2 commits into
mainfrom
fix/oss-5810-terminal-failure-telemetry

Conversation

@neubig

@neubig neubig commented Nov 16, 2026

Copy link
Copy Markdown
Member

Summary

Implements the remaining OSS-5810 work: every terminal automation failure now emits bounded failure_stage / failure_kind values so PostHog funnels can categorize failures and count unique users with >=1 success vs failure-only users. No existing open PR covered this (checked; #704 failure_kind derivation is already merged and is built on, not duplicated).

This PR was created by an AI agent (OpenHands) on behalf of the requester.

What was broken

  • A callback FAILED with no usable error_detail produced automation_run_failed with no failure category at all.
  • Watchdog verified-failure/timeout branches update the row via SQL, so telemetry read the stale in-memory run (run_status: RUNNING, no derived kind).
  • The dispatcher's background-task safety net (_execute_run_safe) marked runs FAILED with no telemetry event.
  • No terminal failure event carried a failure_stage.

Change

  • telemetry.py: _ensure_failure_properties guarantees bounded failure_stage/failure_kind on automation_run_failed / automation_run_dispatch_failed. Stage is derived from the stage that already determined the outcome (the same values first-run outcomes record): callback -> execution, dispatcher -> dispatch, watchdog -> watchdog. Kind falls back to execution_error / dispatch_error. Explicit caller-provided values always win; raw error text is never copied into properties. Success events are untouched.
  • watchdog.py: sync the in-memory run (status/error_detail) with the SQL update before capture in all three terminal branches, so run status and derived kinds (e.g. timeout for exit -1) reflect the persisted row.
  • dispatcher.py: same sync in _fail; _execute_run_safe now emits a categorized automation_run_failed (dispatch / dispatch_error) after marking the run FAILED.
callback FAILED (no error body) -> failure_stage=execution, failure_kind=execution_error
dispatcher task exception       -> failure_stage=dispatch,  failure_kind=dispatch_error
watchdog verified exit 1        -> failure_stage=watchdog,  failure_kind=execution_error
watchdog verified exit -1       -> failure_stage=watchdog,  failure_kind=timeout
watchdog verification exception -> failure_stage=watchdog,  failure_kind=verification_failed (unchanged explicit kind)

Actor identity, consent checks, trigger/deployment context and explicit category overrides are preserved; only the external PostHog HTTP transport is faked in the new tests (the real capture code path, DB sessions and watchdog/dispatcher logic run for real).

Tests

New regression tests failed before the fix (9 failures: missing failure_stage / missing event) and pass after:

  • tests/test_telemetry.py: parametrized terminal-failure categorization for callback/dispatcher/watchdog (via a real local capture server faking only PostHog transport), explicit-override preservation, no raw error/secret in payloads, success events gain no failure properties, and a unique-user aggregation test separating users with >=1 success from failure-only users.
  • tests/test_watchdog.py: real _verify_and_mark_run verified-failure captures (exit 1 -> execution_error, exit -1 -> timeout).
  • tests/test_dispatcher.py: real _execute_run_safe failure emits the categorized terminal event.

Results:

  • tests/test_telemetry.py + tests/test_watchdog.py: 76 passed
  • tests/test_dispatcher.py: 103 passed, 1 failed (TestDispatcherLoop::test_dispatcher_loop_dispatches_runs — pre-existing: also fails on clean main, verified via git stash)
  • tests/test_router.py callback/cancel/first-run subset: 27 passed; tests/test_run_utils.py + tests/test_cancel_run.py: 17 passed
  • ruff check and ruff format --check clean on changed files

Live local evidence (synthetic data only, local fake PostHog — not production)

Ran the real service locally (uvicorn openhands.automation.app:app, SQLite, local auth, AUTOMATION_POSTHOG_HOST pointed at a local capture server on 127.0.0.1), seeded two synthetic users with consent + RUNNING runs, then POSTed real callbacks:

POST /api/automation/v1/runs/<A-success>/complete {"status":"COMPLETED"} -> 200
POST /api/automation/v1/runs/<A-fail>/complete    {"status":"FAILED"}    -> 200   (no error in body)
POST /api/automation/v1/runs/<B-fail>/complete    {"status":"FAILED"}    -> 200

Captured payloads:

automation_run_completed | distinct: synthetic-user-a | stage: None      | kind: None
automation_run_failed    | distinct: synthetic-user-a | stage: execution | kind: execution_error | run_status: FAILED | trigger_type: cron | deployment: local
automation_run_failed    | distinct: synthetic-user-b | stage: execution | kind: execution_error | run_status: FAILED | trigger_type: cron | deployment: local
users with >=1 success: ['synthetic-user-a']
failure-only users:     ['synthetic-user-b']
no raw error_detail / secrets in any payload: OK

The watchdog live path stayed transient by design (local agent server unreachable -> verification retries, run left RUNNING), so watchdog terminal classification is evidenced by the real-capture tests above rather than a forced live run.

Not done here, per scope: no Linear state changes, no PostHog dashboard/funnel changes, PostHog-side distinct-id aliasing for already-sent anonymous events is out of scope for this repo change.

Guarantee bounded failure_stage/failure_kind on automation_run_failed
and automation_run_dispatch_failed events, derived from the existing
trigger_source vocabulary (callback->execution, dispatcher->dispatch,
watchdog->watchdog) with an execution_error/dispatch_error fallback
kind. Explicit caller properties still win and no raw error text is
copied into telemetry.

Watchdog SQL updates now sync the in-memory run before capture so
verified execution failures/timeouts report the persisted status and
derived kind instead of stale RUNNING/None. The dispatcher mirrors
the same sync after mark_run_terminal, and unexpected dispatch-task
exceptions now emit a categorized automation_run_failed event instead
of failing silently.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions github-actions Bot added the type: fix A bug fix label Nov 16, 2026
Fixes the pyright reportArgumentType error from the backend CI check.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

@neubig
neubig requested a review from all-hands-bot November 16, 2026 21:52

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This review was posted by an AI agent (OpenHands).

Reviewed head d32f9f24a5bf097993c868d4731837c52bfbee67 against main in a clean checkout, with AGENTS.md and the repo review guide applied.

Scope: All changed files (telemetry.py, watchdog.py, dispatcher.py + tests) belong to this repository per the ownership boundaries in AGENTS.md. The PR description provides the design context the guide asks for on cross-cutting lifecycle changes: intent, before/after behavior, compatibility notes, and commit-pinned code references.

Verification:

  • All required checks pass on this exact head (unit-tests, backend, windows-tests, image builds).
  • Locally: all 36 tests/test_telemetry.py tests pass, the two new TestVerifyAndMarkRunTelemetry watchdog tests pass (exit 1 → execution_error, exit -1 → timeout), and ruff check / ruff format --check are clean on all six changed files. The remaining local errors are Docker-fixture availability, covered by CI.
  • New tests exercise the real capture, DB, watchdog, and dispatcher paths with only the PostHog HTTP transport faked, matching the repo's evidence rule for lifecycle fixes.

Correctness checks (no material findings):

  • _ensure_failure_properties only ever writes bounded stage/kind values; raw error_detail text is never copied into properties, and explicit caller-provided categories win. Verified against the callback, dispatcher, and watchdog capture sites.
  • The in-memory run syncs mirror the preceding conditional UPDATE ... WHERE status = 'RUNNING' exactly: watchdog terminal branches set the same status the SQL writes (COMPLETED iff exit_code == 0), and error_detail is synced only on failure branches. Mutating the detached run object for telemetry reads follows an existing in-repo precedent (mark_deferred_runs_expired does the same for SKIPPED runs); the object is not re-attached or reused for writes afterward, so no race or persisted-state drift is introduced.
  • The callback path refreshes the run after the terminal UPDATE before capturing, so run_status is now correct there, and the Timed out: reconcile path in router.py is unaffected (dispatcher failures never carry that prefix).
  • _execute_run_safe emits its event only on a genuine background-task exception, after mark_run_terminal marks the run FAILED, satisfying the fire-and-forget "owner that observes and records failure" checkpoint. run.automation is eagerly loaded by _poll_pending_runs on this path.
  • Consent gating, distinct-id resolution, actor identity, and first-run outcome recording are unchanged; success events gain no failure properties.
  • No schema or migration changes; behavior is driver-agnostic (SQLite local tests + PostgreSQL CI).

Every terminal failure path now emits a categorized event, and the fallback vocabulary stays closed even when no trigger source is supplied. Nothing blocking found.

✅ APPROVED

@neubig
neubig merged commit 0ee6949 into main Nov 17, 2026
10 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.33.0 Shipped in 1.33.0 label Nov 17, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.33.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.33.0 Shipped in 1.33.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants