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.

feat(tasks): emit agent ready-to-first-activity latency as a histogram (#113743)

Alessandro Pogliaghi6d1d669dd566
diff --git a/products/tasks/backend/temporal/metrics.py b/products/tasks/backend/temporal/metrics.py
index 417ab744b127..e27de0c2c39d 100644
--- a/products/tasks/backend/temporal/metrics.py
+++ b/products/tasks/backend/temporal/metrics.py
@@ -16,6 +16,7 @@ TASKS_LATENCY_HISTOGRAM_METRICS = (
"tasks_process_sandbox_step_latency",
"tasks_process_snapshot_create_latency",
"tasks_boot_total_latency",
+ "tasks_agent_boot_milestone_latency",
)
TASKS_LATENCY_HISTOGRAM_BUCKETS = [
100.0,
@@ -586,6 +587,37 @@ def record_boot_total_ms(
pass
+def record_agent_boot_milestone_ms(
+ since_agent_ready_ms: int,
+ *,
+ milestone: str,
+ origin_product: str | None,
+ boot_path: str | None,
+ runtime: str,
+ sandbox_backend: str | None,
+ runtime_adapter: str | None,
+ prewarmed: bool,
+) -> None:
+ """Prometheus twin of the boot milestone analytics events, plotted next to `tasks_boot_total_latency`."""
+ try:
+ attributes: Attributes = {
+ "milestone": milestone,
+ "origin_product": origin_product or "unknown",
+ "boot_path": boot_path or "unknown",
+ "runtime": runtime,
+ "sandbox_backend": sandbox_backend or "unknown",
+ "runtime_adapter": _runtime_adapter_label(runtime_adapter),
+ "prewarmed": _bool_label(prewarmed),
+ }
+ workflow.metric_meter().with_additional_attributes(attributes).create_histogram_timedelta(
+ "tasks_agent_boot_milestone_latency",
+ "Latency from agent-server ready to the first agent boot milestone",
+ unit="ms",
+ ).record(dt.timedelta(milliseconds=since_agent_ready_ms))
+ except Exception:
+ pass
+
+
class StepTimer:
def __init__(
self,
diff --git a/products/tasks/backend/temporal/process_task/tests/test_workflow.py b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
index f2ac66950dc0..f6c1b4c519eb 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_workflow.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
@@ -1464,16 +1464,27 @@ async def test_boot_milestone_signal_before_context_is_dropped(monkeypatch):
assert workflow_instance._first_command_dispatched_recorded is False
-async def test_boot_milestone_contains_only_timing_and_runtime_dimensions(monkeypatch):
+ ("agent_ready_at", "since_agent_ready_ms"),
+ [
+ (datetime(2026, 8, 28, 10, 0, 20, tzinfo=UTC), 1_000),
+ (None, None),
+ ],
+)
+async def test_boot_milestone_contains_only_timing_and_runtime_dimensions(
+ monkeypatch, agent_ready_at, since_agent_ready_ms
+):
workflow_instance = ProcessTaskWorkflow()
workflow_instance._context = _build_context(github_integration_id=123)
workflow_instance._sandbox_id_for_cleanup = "sandbox-123"
workflow_instance._chain_started_at = datetime(2026, 8, 28, 10, 0, tzinfo=UTC)
- workflow_instance._agent_ready_at = datetime(2026, 8, 28, 10, 0, 20, tzinfo=UTC)
+ workflow_instance._agent_ready_at = agent_ready_at
workflow_instance._boot_path = "overlap"
workflow_instance._image_source = "base_image"
track = AsyncMock()
monkeypatch.setattr(workflow_instance, "_track_boot_milestone", track)
+ record_histogram = Mock()
+ monkeypatch.setattr(process_task_workflow_module, "record_agent_boot_milestone_ms", record_histogram)
monkeypatch.setattr(
process_task_workflow_module.workflow,
"now",
@@ -1491,8 +1502,9 @@ async def test_boot_milestone_contains_only_timing_and_runtime_dimensions(monkey
"task_id": "task-id",
"sandbox_id": "sandbox-123",
"elapsed_ms": 21_000,
- "since_agent_ready_ms": 1_000,
+ "since_agent_ready_ms": since_agent_ready_ms,
"boot_path": "overlap",
+ "runtime": "gvisor",
"image_source": "base_image",
"origin_product": None,
"mode": "background",
@@ -1504,6 +1516,25 @@ async def test_boot_milestone_contains_only_timing_and_runtime_dimensions(monkey
"prewarmed": False,
},
)
+ if since_agent_ready_ms is None:
+ # The milestone beat readiness: the sample waits for the ready stamp and clamps to zero.
+ record_histogram.assert_not_called()
+ workflow_instance._agent_ready_at = datetime(2026, 8, 28, 10, 0, 25, tzinfo=UTC)
+ workflow_instance._flush_boot_milestones_before_ready()
+ workflow_instance._flush_boot_milestones_before_ready()
+ expected_sample_ms = 0
+ else:
+ expected_sample_ms = since_agent_ready_ms
+ record_histogram.assert_called_once_with(
+ expected_sample_ms,
+ milestone="agent_first_command_dispatched",
+ origin_product=None,
+ boot_path="overlap",
+ runtime="gvisor",
+ sandbox_backend="modal",
+ runtime_adapter=None,
+ prewarmed=False,
+ )
@pytest.mark.parametrize(
diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.py
index a32405b4afc2..7b20d2be8c60 100644
--- a/products/tasks/backend/temporal/process_task/workflow.py
+++ b/products/tasks/backend/temporal/process_task/workflow.py
@@ -28,7 +28,11 @@ from products.tasks.backend.temporal.babysit_pr.prompts import (
)
from products.tasks.backend.temporal.babysit_pr.snapshot import AttentionSet, BabysitJournal, PRSnapshot
from products.tasks.backend.temporal.create_snapshot.workflow import CreateSnapshotForRepositoryInput
-from products.tasks.backend.temporal.metrics import increment_pr_babysit_decision
+from products.tasks.backend.temporal.metrics import (
+ increment_pr_babysit_decision,
+ record_agent_boot_milestone_ms,
+ sandbox_runtime_label,
+)
from products.tasks.backend.temporal.patches import ci_follow_up_actionable_gate
from products.tasks.backend.temporal.process_task.activities.get_pr_babysit_snapshot import (
GetPrBabysitSnapshotInput,
@@ -615,6 +619,8 @@ class ProcessTaskWorkflow(PostHogWorkflow):
self._boot_path: str | None = None
self._image_source: str | None = None
self._agent_ready_at: datetime | None = None
+ # Milestones the agent hits before readiness is stamped, sampled once it is.
+ self._boot_milestones_before_ready: list[tuple[str, datetime]] = []
self._boot_telemetry_tasks: list[asyncio.Task[None]] = []
self._progress_chain: asyncio.Task[None] | None = None
self._slack_setup_chain: asyncio.Task[None] | None = None
@@ -1918,6 +1924,7 @@ class ProcessTaskWorkflow(PostHogWorkflow):
agent_server_output = await self._start_agent_server(sandbox_output, boot_excluded_ms=wizard_ms)
self._agent_shadow_launched = bool(sandbox_output.agent_shadow_launched or agent_server_output.shadow_launched)
self._agent_ready_at = workflow.now() if self._agent_boot_interaction_telemetry_enabled else None
+ self._flush_boot_milestones_before_ready()
await self._emit_progress("agent", "completed", "Agent ready", "setup", wait=False)
await self._track_workflow_event(
@@ -2758,15 +2765,18 @@ class ProcessTaskWorkflow(PostHogWorkflow):
def _schedule_boot_milestone(self, event_name: str) -> None:
now = workflow.now()
+ since_agent_ready_ms = (
+ max(0, int((now - self._agent_ready_at).total_seconds() * 1000)) if self._agent_ready_at else None
+ )
+ runtime = sandbox_runtime_label(self.context.use_modal_vm_sandbox)
properties = {
"run_id": self.context.run_id,
"task_id": self.context.task_id,
"sandbox_id": self._sandbox_id_for_cleanup,
"elapsed_ms": max(0, int((now - self._chain_start_time()).total_seconds() * 1000)),
- "since_agent_ready_ms": (
- max(0, int((now - self._agent_ready_at).total_seconds() * 1000)) if self._agent_ready_at else None
- ),
+ "since_agent_ready_ms": since_agent_ready_ms,
"boot_path": self._boot_path,
+ "runtime": runtime,
"image_source": self._image_source,
"origin_product": self.context.origin_product,
"mode": self.context.mode,
@@ -2777,10 +2787,36 @@ class ProcessTaskWorkflow(PostHogWorkflow):
"transport": "sequenced_ingest" if self.context.sandbox_event_ingest_enabled else "sse",
"prewarmed": self._prewarmed,
}
+ if since_agent_ready_ms is not None:
+ self._record_boot_milestone_sample(event_name, since_agent_ready_ms)
+ else:
+ # A non-interactive agent can start its first turn before the readiness
+ # activity returns; the sample is taken once readiness is stamped.
+ self._boot_milestones_before_ready.append((event_name, now))
task = asyncio.create_task(self._track_boot_milestone(event_name, properties))
self._boot_telemetry_tasks.append(task)
task.add_done_callback(self._boot_telemetry_tasks.remove)
+ def _record_boot_milestone_sample(self, event_name: str, since_agent_ready_ms: int) -> None:
+ record_agent_boot_milestone_ms(
+ since_agent_ready_ms,
+ milestone=event_name,
+ origin_product=self.context.origin_product,
+ boot_path=self._boot_path,
+ runtime=sandbox_runtime_label(self.context.use_modal_vm_sandbox),
+ sandbox_backend=self.context.sandbox_backend,
+ runtime_adapter=self.context.runtime_adapter,
+ prewarmed=self._prewarmed,
+ )
+
+ def _flush_boot_milestones_before_ready(self) -> None:
+ if self._agent_ready_at is None:
+ return
+ for event_name, observed_at in self._boot_milestones_before_ready:
+ since_agent_ready_ms = max(0, int((observed_at - self._agent_ready_at).total_seconds() * 1000))
+ self._record_boot_milestone_sample(event_name, since_agent_ready_ms)
+ self._boot_milestones_before_ready.clear()
+
def _record_first_command_dispatched(self) -> None:
if self._first_command_dispatched_recorded or not self._agent_boot_interaction_telemetry_enabled:
return