: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): record sandbox exit reason on completed runs (#112153)
posthog[bot]43ecfa735d7f
diff --git a/docs/internal/sandboxes-setup-guide.md b/docs/internal/sandboxes-setup-guide.mdindex 394138317834..401af67ff2b9 100644--- a/docs/internal/sandboxes-setup-guide.md+++ b/docs/internal/sandboxes-setup-guide.md@@ -327,6 +327,8 @@ Then fill in the secrets. `POSTHOG_UI_APPS_TOKEN` and `POSTHOG_ANALYTICS_API_KEYThe memory watchdog stops tool process trees before the sandbox reaches its memory limit. A process stop, including SIGKILL escalation, does not mean the task run died.+A run whose whole sandbox stops usually still ends as completed, with `termination_reason` set to `sandbox_gone`. The `error_message` property on `task_run_completed` carries the sandbox exit reason. Exit code 137 there usually means the sandbox ran out of memory.+Cloud Claude and Codex sessions deliver each watchdog warning separately to subagents and their parent. Codex uses native MCP tool hooks for shell calls, including the final result of an `exec_command` completed through `write_stdin`. Claude shell results with exit codes 137, 143, or 144 wait briefly for the watchdog's delayed record; an exit code alone is not treated as proof of an OOM. Codex hook payloads carry the command output without its exit status, so a Codex command counts as stopped when a kill record lands between its start and shortly after its end. A quick rerun of a long Codex command waits for that record before it starts.Codex trusts only the PostHog memory hooks through session configuration. It does not store hook trust in the user's config file. The local tools MCP server opts out of Codex's tool catalog cache, so a subagent thread starts it before its first shell command instead of lazily on its first MCP tool call. Codex hook payloads do not include an `exec_command` `workdir`, so a Codex retry count is keyed by the thread directory and the command text. Put `cd <directory> &&` in the command to scope a retry count to a directory.diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py b/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.pyindex 8d425090e858..ddf5122011df 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py@@ -344,13 +344,14 @@ class TestUpdateTaskRunStatusActivity:@pytest.mark.django_db(transaction=True)@pytest.mark.parametrize(- "status,timed_out_inactivity,timeout_marker,expected_event,expected_reason",+ "status,timed_out_inactivity,timeout_marker,error_message,expected_event,expected_reason",[- (TaskRun.Status.COMPLETED, True, None, "task_run_completed", TIMED_OUT_INACTIVITY_STATE_KEY),+ (TaskRun.Status.COMPLETED, True, None, None, "task_run_completed", TIMED_OUT_INACTIVITY_STATE_KEY),(TaskRun.Status.FAILED,False,TIMED_OUT_WALL_CLOCK_STATE_KEY,+ None,"task_run_failed",TIMED_OUT_WALL_CLOCK_STATE_KEY,),@@ -358,9 +359,18 @@ class TestUpdateTaskRunStatusActivity:TaskRun.Status.COMPLETED,False,TIMED_OUT_WALL_CLOCK_STATE_KEY,+ None,"task_run_completed",TIMED_OUT_WALL_CLOCK_STATE_KEY,),+ (+ TaskRun.Status.COMPLETED,+ False,+ SANDBOX_GONE_STATE_KEY,+ "Sandbox stopped: killed with exit code 137, usually because it ran out of memory. Resume to continue.",+ "task_run_completed",+ SANDBOX_GONE_STATE_KEY,+ ),],)@patch("products.tasks.backend.models.posthoganalytics.capture")@@ -372,12 +382,14 @@ class TestUpdateTaskRunStatusActivity:status,timed_out_inactivity,timeout_marker,+ error_message,expected_event,expected_reason,):input_data = UpdateTaskRunStatusInput(run_id=str(test_task_run.id),status=status,+ error_message=error_message,timed_out_inactivity=timed_out_inactivity,timeout_marker=timeout_marker,agent_active_at_termination=False,@@ -390,6 +402,7 @@ class TestUpdateTaskRunStatusActivity:captured = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == expected_event]properties = captured[0].kwargs["properties"]assert properties["termination_reason"] == expected_reason+ assert properties["error_message"] == (error_message or "")assert properties["agent_active_at_termination"] is Falseassert properties["end_of_turn_received"] is Trueassert properties["last_agent_heartbeat_at"] == "2026-08-19T10:00:00+00:00"diff --git a/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py b/products/tasks/backend/temporal/process_task/activities/update_task_run_status.pyindex 890272289aad..1015cd9d3ea9 100644--- a/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py+++ b/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py@@ -265,6 +265,8 @@ def _capture_terminal_analytics(task_run: TaskRun, input: UpdateTaskRunStatusInp{"duration_seconds": task_run._duration_seconds(),"termination_reason": termination_reason,+ # A lost sandbox still completes the run, and this message carries its exit reason, e.g. OOM.+ "error_message": truncate_error_message(input.error_message),"has_summary": bool((task_run.state or {}).get("task_summary")),**relay_state,},