Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(tasks): surface agent error category on custom-prompt failures (#61500)

Joshua Snyderb3abe0095fed
diff --git a/products/tasks/backend/services/custom_prompt_internals.py b/products/tasks/backend/services/custom_prompt_internals.py
index 2333bd2ff92d..5a406f027a6a 100644
--- a/products/tasks/backend/services/custom_prompt_internals.py
+++ b/products/tasks/backend/services/custom_prompt_internals.py
@@ -30,6 +30,24 @@ POLL_INTERVAL_SECONDS = 10
MAX_POLL_SECONDS = 30 * 60 # 30 minutes (matches sandbox TTL)
MAX_CONSECUTIVE_STORAGE_ERRORS = 3
+# Notification method the sandbox agent emits on a terminal failure. The agent
+# classifies upstream failures (rate limits, stream/connection drops, provider
+# errors) via classifyAgentError() and writes the category + raw message here, so
+# the PostHog side can surface the real cause instead of Temporal's opaque
+# "Activity task failed" wrapper.
+AGENT_ERROR_METHOD = "_posthog/error"
+
+
+@dataclass(frozen=True)
+class AgentError:
+ """A terminal error the sandbox agent reported in its S3 log."""
+
+ message: str
+ category: str | None = None
+
+ def describe(self) -> str:
+ return f"{self.category}: {self.message}" if self.category else self.message
+
@dataclass(frozen=True)
class CustomPromptSandboxContext:
@@ -258,6 +276,19 @@ async def _drain_final_log(
printed_lines = _stream_new_lines(final_log, printed_lines, verbose=verbose, output_fn=output_fn)
if final_message:
return final_message, final_log, final_lines, printed_lines
+ # Prefer the agent's own classified error over the generic terminal-status message
+ # (Temporal's "Activity task failed"). Only on FAILED — CANCELLED is a user action and
+ # COMPLETED-with-no-message is the empty-turn path, neither of which we want to relabel.
+ if refreshed_status == TaskRun.Status.FAILED:
+ agent_error = _extract_agent_error(final_log, skip_lines=original_skip_lines)
+ if agent_error is not None:
+ cause_text = agent_error.describe()
+ # Persist the real cause so the TaskRun stops showing "Activity task failed".
+ await _persist_task_run_error_message(str(task_run.id), cause_text)
+ raise RuntimeError(
+ f"custom_prompt - drain_final_log: TaskRun reached terminal status={refreshed_status} "
+ f"(cause: {cause_text})"
+ )
reason = "end_turn with empty response" if final_empty_end_turn else "no agent message"
cause = f" (cause: {error_message})" if error_message else ""
raise RuntimeError(
@@ -265,6 +296,59 @@ async def _drain_final_log(
)
+def _extract_agent_error(log_content: str | None, skip_lines: int = 0) -> AgentError | None:
+ """Scan log lines for the agent's structured terminal-error notification.
+
+ Returns the last `_posthog/error` entry carrying a non-empty message (with the
+ classified `error_category` when the agent build provides it), or None when no
+ such entry exists — e.g. an older agent build or a non-agent failure — in which
+ case the caller falls back to the generic terminal-status message.
+ """
+ if not log_content:
+ return None
+ lines = log_content.strip().split("\n")
+ found: AgentError | None = None
+ for line in lines[skip_lines:]:
+ line = line.strip()
+ if not line:
+ continue
+ try:
+ entry = json.loads(line)
+ except json.JSONDecodeError:
+ continue
+ notification = entry.get("notification")
+ if not isinstance(notification, dict) or notification.get("method") != AGENT_ERROR_METHOD:
+ continue
+ params = notification.get("params")
+ if not isinstance(params, dict):
+ continue
+ message = params.get("message")
+ if not isinstance(message, str) or not message.strip():
+ continue
+ raw_category = params.get("error_category")
+ category = raw_category.strip() if isinstance(raw_category, str) and raw_category.strip() else None
+ found = AgentError(message=message.strip(), category=category)
+ return found
+
+
+def _update_task_run_error_message(run_id: str, message: str) -> None:
+ TaskRun.objects.filter(id=run_id).update(error_message=message)
+
+
+async def _persist_task_run_error_message(run_id: str, message: str) -> None:
+ """Best-effort write of the agent's real error onto the TaskRun. The raised
+ RuntimeError already carries the message for Temporal, so a failed write here
+ must not mask the underlying failure."""
+ try:
+ await sync_to_async(_update_task_run_error_message)(run_id, message)
+ except Exception:
+ logger.warning(
+ "custom_prompt - drain_final_log: failed to persist agent error to TaskRun run=%s",
+ run_id,
+ exc_info=True,
+ )
+
+
def _stream_new_lines(
full_log: str | None, printed_lines: int, *, verbose: bool = False, output_fn: OutputFn = None
) -> int:
diff --git a/products/tasks/backend/tests/agent_log_fixtures.py b/products/tasks/backend/tests/agent_log_fixtures.py
index 7b5b19bed678..723095463e11 100644
--- a/products/tasks/backend/tests/agent_log_fixtures.py
+++ b/products/tasks/backend/tests/agent_log_fixtures.py
@@ -45,6 +45,16 @@ def _user_message_line(text: str) -> str:
)
+def _agent_error_line(message: str, category: str | None = None) -> str:
+ """Build a `_posthog/error` notification line as the sandbox agent emits on a
+ terminal failure. `category` mirrors classifyAgentError() output and is absent
+ on older agent builds."""
+ params: dict = {"message": message}
+ if category is not None:
+ params["error_category"] = category
+ return json.dumps({"notification": {"method": "_posthog/error", "params": params}})
+
+
def _usage_update_line(used: int = 1000) -> str:
return json.dumps(
{
diff --git a/products/tasks/backend/tests/test_multi_turn_session.py b/products/tasks/backend/tests/test_multi_turn_session.py
index 1b97a98b0756..85a0e386648e 100644
--- a/products/tasks/backend/tests/test_multi_turn_session.py
+++ b/products/tasks/backend/tests/test_multi_turn_session.py
@@ -5,6 +5,7 @@ import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from asgiref.sync import sync_to_async
+from parameterized import parameterized
from pydantic import BaseModel
from posthog.models import Integration, Organization, Team
@@ -12,14 +13,17 @@ from posthog.models.user import User
from products.tasks.backend.models import TaskRun
from products.tasks.backend.services.custom_prompt_internals import (
+ AgentError,
CustomPromptSandboxContext,
EmptyAgentTurnError,
+ _extract_agent_error,
create_task_and_trigger,
poll_for_turn,
)
from products.tasks.backend.services.custom_prompt_multi_turn_runner import _EMPTY_TURN_RETRY_NUDGE, MultiTurnSession
from products.tasks.backend.tests.agent_log_fixtures import (
FakeTaskRun,
+ _agent_error_line,
_agent_message_line,
_end_turn_line,
_usage_update_line,
@@ -215,6 +219,204 @@ class TestPollForTurnTerminalDrain:
assert last_message == "partial-before-death"
+class TestExtractAgentError:
+ @parameterized.expand(
+ [
+ ("upstream_provider_failure", "API Error: 429 rate_limit_error"),
+ ("upstream_connection_error", "API Error: Connection error"),
+ ("upstream_stream_terminated", "API Error: terminated"),
+ ("agent_error", "Something broke inside the agent"),
+ ]
+ )
+ def test_extracts_category_and_message(self, category, message):
+ log = "\n".join([_user_message_line("prompt"), _agent_error_line(message, category=category)])
+ result = _extract_agent_error(log)
+ assert result == AgentError(message=message, category=category)
+ assert result.describe() == f"{category}: {message}"
+
+ def test_extracts_message_without_category(self):
+ message = "API Error: 429 rate_limit_error"
+ result = _extract_agent_error(_agent_error_line(message))
+ assert result == AgentError(message=message, category=None)
+ assert result.describe() == message
+
+ def test_returns_none_when_no_error_line(self):
+ log = "\n".join([_agent_message_line("hello"), _end_turn_line()])
+ assert _extract_agent_error(log) is None
+
+ def test_returns_none_for_empty_log(self):
+ assert _extract_agent_error(None) is None
+ assert _extract_agent_error("") is None
+
+ def test_ignores_error_lines_before_skip_cursor(self):
+ # The previous turn's error must not leak into the current turn's drain.
+ turn_1 = [_agent_error_line("old failure", category="agent_error")]
+ turn_2 = [
+ _user_message_line("retry"),
+ _agent_error_line("API Error: 429 rate_limit_error", category="upstream_provider_failure"),
+ ]
+ log = "\n".join(turn_1 + turn_2)
+ result = _extract_agent_error(log, skip_lines=len(turn_1))
+ assert result == AgentError(message="API Error: 429 rate_limit_error", category="upstream_provider_failure")
+
+ def test_returns_last_error_when_multiple(self):
+ log = "\n".join(
+ [
+ _agent_error_line("first", category="upstream_connection_error"),
+ _agent_error_line("API Error: 429 rate_limit_error", category="upstream_provider_failure"),
+ ]
+ )
+ result = _extract_agent_error(log)
+ assert result == AgentError(message="API Error: 429 rate_limit_error", category="upstream_provider_failure")
+
+
+class TestPollForTurnSurfacesAgentError:
+ """On a FAILED terminal status, the drain must surface the agent's classified error
+ (category + raw message) on both TaskRun.error_message and the raised RuntimeError
+ that Temporal records — never the opaque 'Activity task failed' wrapper."""
+
+ @parameterized.expand(
+ [
+ ("upstream_provider_failure", "API Error: 429 rate_limit_error"),
+ ("upstream_connection_error", "API Error: Connection error"),
+ ("upstream_stream_terminated", "API Error: terminated"),
+ ("agent_error", "Unhandled exception in agent loop"),
+ ]
+ )
+ @pytest.mark.asyncio
+ async def test_surfaces_classified_error(self, category, message):
+ turn_1 = [_agent_message_line("turn-1-response"), _end_turn_line()]
+ # Turn 2 died with a classified error and no agent_message / end_turn.
+ turn_2 = [_user_message_line("followup"), _usage_update_line(0), _agent_error_line(message, category=category)]
+ log = "\n".join(turn_1 + turn_2)
+ skip = len(turn_1)
+ # The workflow recorded the generic Temporal wrapper — the drain must override it.
+ fake_task_run = FakeTaskRun(status="failed", error_message="Activity task failed")
+
+ persist = AsyncMock()
+ with (
+ patch("posthog.storage.object_storage.read", return_value=log),
+ patch("asyncio.sleep", new=AsyncMock()),
+ patch("products.tasks.backend.services.custom_prompt_internals.POLL_INTERVAL_SECONDS", 0),
+ patch("products.tasks.backend.models.TaskRun.objects.get", return_value=fake_task_run),
+ patch(
+ "products.tasks.backend.services.custom_prompt_internals._persist_task_run_error_message",
+ new=persist,
+ ),
+ ):
+ with pytest.raises(RuntimeError) as exc_info:
+ await poll_for_turn(fake_task_run, skip_lines=skip)
+
+ expected = f"{category}: {message}"
+ # Temporal activity failure carries the classified error, not "Activity task failed".
+ assert expected in str(exc_info.value)
+ assert "Activity task failed" not in str(exc_info.value)
+ # The same classified error is persisted onto TaskRun.error_message.
+ persist.assert_awaited_once_with(str(fake_task_run.id), expected)
+
+ @pytest.mark.asyncio
+ async def test_acceptance_provider_failure_429(self):
+ turn = [
+ _user_message_line("summarize"),
+ _agent_error_line("API Error: 429 rate_limit_error", category="upstream_provider_failure"),
+ ]
+ fake_task_run = FakeTaskRun(status="failed", error_message="Activity task failed")
+
+ persist = AsyncMock()
+ with (
+ patch("posthog.storage.object_storage.read", return_value="\n".join(turn)),
+ patch("asyncio.sleep", new=AsyncMock()),
+ patch("products.tasks.backend.services.custom_prompt_internals.POLL_INTERVAL_SECONDS", 0),
+ patch("products.tasks.backend.models.TaskRun.objects.get", return_value=fake_task_run),
+ patch(
+ "products.tasks.backend.services.custom_prompt_internals._persist_task_run_error_message",
+ new=persist,
+ ),
+ ):
+ with pytest.raises(RuntimeError) as exc_info:
+ await poll_for_turn(fake_task_run, skip_lines=0)
+
+ # The value persisted to TaskRun.error_message carries the category + 429 text.
+ expected = "upstream_provider_failure: API Error: 429 rate_limit_error"
+ persist.assert_awaited_once_with(str(fake_task_run.id), expected)
+ assert "upstream_provider_failure" in str(exc_info.value)
+ assert "429 rate_limit_error" in str(exc_info.value)
+
+ @pytest.mark.asyncio
+ async def test_error_without_category_falls_back_to_message(self):
+ # Older agent build: no error_category, but the raw message still beats the wrapper.
+ turn = [_user_message_line("summarize"), _agent_error_line("API Error: 429 rate_limit_error")]
+ fake_task_run = FakeTaskRun(status="failed", error_message="Activity task failed")
+
+ persist = AsyncMock()
+ with (
+ patch("posthog.storage.object_storage.read", return_value="\n".join(turn)),
+ patch("asyncio.sleep", new=AsyncMock()),
+ patch("products.tasks.backend.services.custom_prompt_internals.POLL_INTERVAL_SECONDS", 0),
+ patch("products.tasks.backend.models.TaskRun.objects.get", return_value=fake_task_run),
+ patch(
+ "products.tasks.backend.services.custom_prompt_internals._persist_task_run_error_message",
+ new=persist,
+ ),
+ ):
+ with pytest.raises(RuntimeError) as exc_info:
+ await poll_for_turn(fake_task_run, skip_lines=0)
+
+ # No category prefix — the raw message is persisted and surfaced.
+ persist.assert_awaited_once_with(str(fake_task_run.id), "API Error: 429 rate_limit_error")
+ assert "API Error: 429 rate_limit_error" in str(exc_info.value)
+
+ @pytest.mark.asyncio
+ async def test_missing_structured_error_keeps_generic_behavior(self):
+ # No _posthog/error line: the drain must keep today's generic message and
+ # must not touch TaskRun.error_message.
+ turn = [_user_message_line("summarize"), _usage_update_line(0)]
+ fake_task_run = FakeTaskRun(status="failed", error_message="Activity task failed")
+
+ persist = AsyncMock()
+ with (
+ patch("posthog.storage.object_storage.read", return_value="\n".join(turn)),
+ patch("asyncio.sleep", new=AsyncMock()),
+ patch("products.tasks.backend.services.custom_prompt_internals.POLL_INTERVAL_SECONDS", 0),
+ patch("products.tasks.backend.models.TaskRun.objects.get", return_value=fake_task_run),
+ patch(
+ "products.tasks.backend.services.custom_prompt_internals._persist_task_run_error_message",
+ new=persist,
+ ),
+ ):
+ with pytest.raises(RuntimeError, match="no agent message") as exc_info:
+ await poll_for_turn(fake_task_run, skip_lines=0)
+
+ assert "Activity task failed" in str(exc_info.value)
+ persist.assert_not_awaited()
+ assert fake_task_run.error_message == "Activity task failed"
+
+ @pytest.mark.asyncio
+ async def test_cancelled_status_does_not_surface_agent_error(self):
+ # CANCELLED is a user action — even with an agent error present, keep generic behavior.
+ turn = [
+ _user_message_line("summarize"),
+ _agent_error_line("API Error: terminated", category="upstream_stream_terminated"),
+ ]
+ fake_task_run = FakeTaskRun(status="cancelled", error_message="cancelled by user")
+
+ persist = AsyncMock()
+ with (
+ patch("posthog.storage.object_storage.read", return_value="\n".join(turn)),
+ patch("asyncio.sleep", new=AsyncMock()),
+ patch("products.tasks.backend.services.custom_prompt_internals.POLL_INTERVAL_SECONDS", 0),
+ patch("products.tasks.backend.models.TaskRun.objects.get", return_value=fake_task_run),
+ patch(
+ "products.tasks.backend.services.custom_prompt_internals._persist_task_run_error_message",
+ new=persist,
+ ),
+ ):
+ with pytest.raises(RuntimeError, match="no agent message"):
+ await poll_for_turn(fake_task_run, skip_lines=0)
+
+ persist.assert_not_awaited()
+
+
class TestMultiTurnSessionRetry:
"""send_followup must retry once on EmptyAgentTurnError and propagate if the retry
also fails, so upstream sees a typed failure rather than a timeout or parse error."""