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): notify mobile users when agents need input (#118581)

Chris Volzer3ba5e9bb52b6
diff --git a/docs/internal/task-run-api.md b/docs/internal/task-run-api.md
index 89fe595f326c..2c267309c4d7 100644
--- a/docs/internal/task-run-api.md
+++ b/docs/internal/task-run-api.md
@@ -87,6 +87,14 @@ Both paths preserve the event timestamp and derive the analytics event ID from t
If an older payload has no timestamp, Django stores its first received time for retries and replays.
Successful captures are recorded for the event ingest token's lifetime so completed work is skipped on replay.
+Interactive permission requests and questions can send a mobile push notification to the run's creator.
+Event ingest, the agent-proxy callback and the sandbox relay share a notification gate keyed by run and permission request ID.
+The gate retains each request for 24 hours after its push is successfully enqueued, so reconnecting does not repeat that notification.
+Enqueue failures and transaction rollbacks leave only the short cooldown, allowing a later callback or relay replay to retry.
+Requests share a 30-second per-run cooldown, applied before allocating a request deduplication key.
+The push dispatcher also shares its per-run cooldown with turn-end awaiting-input notifications.
+Permission request IDs must be non-empty and no longer than 200 characters.
+
## Run summaries
`PATCH /api/projects/{team_id}/tasks/{task_id}/runs/{run_id}/set_summary/` replaces the run's progress summary.
diff --git a/products/tasks/backend/agent_proxy_callback.py b/products/tasks/backend/agent_proxy_callback.py
index 5dba0124d44d..1b93b9afff85 100644
--- a/products/tasks/backend/agent_proxy_callback.py
+++ b/products/tasks/backend/agent_proxy_callback.py
@@ -11,6 +11,7 @@ from jwt import PyJWTError
from posthog.dataclasses import frozen
+from products.tasks.backend.awaiting_input import dispatch_awaiting_input
from products.tasks.backend.facade.api import signal_workflow_completion
from products.tasks.backend.logic.services.process_killed import (
PROCESS_KILLED_EVENT,
@@ -158,6 +159,8 @@ class AgentProxyCallbackDispatcher:
return self._boot_milestone("agent_activity_observed")
case "awaiting_input":
return self._awaiting_input()
+ case "permission_requested":
+ return self._permission_requested()
case "turn_failed":
return self._turn_failed()
case "budget_steer":
@@ -198,6 +201,14 @@ class AgentProxyCallbackDispatcher:
dispatched = dispatch_turn_completed(task_run, turn_completed=self.callback.data["turn_completed"])
return CallbackOutcome(dispatched=dispatched)
+ def _permission_requested(self) -> CallbackOutcome:
+ task_run = self._load_run("task__created_by")
+ if task_run is None:
+ return CallbackOutcome()
+ return CallbackOutcome(
+ dispatched=dispatch_awaiting_input(task_run, request_id=self.callback.data.get("request_id", ""))
+ )
+
def _turn_failed(self) -> CallbackOutcome:
if self._load_run() is None:
return CallbackOutcome()
@@ -258,7 +269,8 @@ class AgentProxyCallbackDispatcher:
description=(
"Internal endpoint called by the standalone Node agent-proxy after accepting an ingest event "
"that requires a Django-side side effect. Dispatches a Temporal heartbeat, a boot milestone, "
- "an awaiting-input mobile push notification, a failed-run completion, budget-steer analytics, "
+ "a turn-completed mobile push notification, an awaiting-input mobile push notification for a "
+ "pending permission request, a failed-run completion, budget-steer analytics, "
"or a sandbox memory watchdog kill "
"depending on `kind`. "
"Authenticated with the forwarded sandbox event ingest JWT plus the X-Agent-Proxy-Secret "
diff --git a/products/tasks/backend/awaiting_input.py b/products/tasks/backend/awaiting_input.py
new file mode 100644
index 000000000000..27a2156a548b
--- /dev/null
+++ b/products/tasks/backend/awaiting_input.py
@@ -0,0 +1,44 @@
+import structlog
+
+from products.tasks.backend import push_dispatcher
+from products.tasks.backend.constants import PERMISSION_REQUEST_ID_MAX_LENGTH
+from products.tasks.backend.models import TaskRun
+from products.tasks.backend.redis import get_tasks_cache
+
+logger = structlog.get_logger(__name__)
+
+# Outlive the sandbox so replayed permission requests cannot notify again on reconnect.
+AWAITING_INPUT_DEDUPE_SECONDS = 24 * 60 * 60
+
+
+def dispatch_awaiting_input(task_run: TaskRun, *, request_id: str) -> bool:
+ """Tell the creator of an interactive run that the agent waits for their answer. The event ingest
+ and the proxy callback both report a permission request, so they share this one gate.
+ Deploy Django before the agent-proxy: an older Django rejects 'permission_requested' with a 400,
+ which the proxy logs and drops."""
+ if task_run.mode != "interactive" or not 0 < len(request_id) <= PERMISSION_REQUEST_ID_MAX_LENGTH:
+ return False
+ try:
+ cache = get_tasks_cache()
+ dedupe_key = f"tasks:awaiting_input:v1:{task_run.id}:{request_id}"
+ if cache.get(dedupe_key):
+ return False
+ # Limit admission before allocating a long-lived key for a new request.
+ if not cache.add(
+ f"tasks:awaiting_input:cooldown:{task_run.id}",
+ True,
+ timeout=push_dispatcher.AWAITING_INPUT_COOLDOWN_SECONDS,
+ ):
+ return False
+ except Exception:
+ logger.warning("awaiting_input.dedupe_failed", run_id=str(task_run.id), exc_info=True)
+ return False
+
+ def mark_enqueued() -> None:
+ try:
+ cache.set(dedupe_key, True, timeout=AWAITING_INPUT_DEDUPE_SECONDS)
+ except Exception:
+ logger.warning("awaiting_input.dedupe_failed", run_id=str(task_run.id), exc_info=True)
+
+ # A rollback or enqueue failure leaves only the short run cooldown, so a replay can retry.
+ return push_dispatcher.notify_task_run_awaiting_input(task_run, on_enqueued=mark_enqueued)
diff --git a/products/tasks/backend/constants.py b/products/tasks/backend/constants.py
index 55bb301dd6cd..ba5d4b928685 100644
--- a/products/tasks/backend/constants.py
+++ b/products/tasks/backend/constants.py
@@ -27,6 +27,8 @@ def is_html_artifact(name: str, content_type: str) -> bool:
return mime_type == "text/html" or name.lower().endswith((".html", ".htm"))
+PERMISSION_REQUEST_ID_MAX_LENGTH = 200
+
# Shared with the MCP server (services/mcp/src/hono/constants.ts): one flag decides both whether
# the MCP serves product skills through `learn` and whether a sandbox launches without bundled skills.
MCP_EXEC_SKILLS_FEATURE_FLAG = "mcp-exec-skills"
diff --git a/products/tasks/backend/logic/services/permission_broker.py b/products/tasks/backend/logic/services/permission_broker.py
index 04ba6a6e5a8f..8931d7112ceb 100644
--- a/products/tasks/backend/logic/services/permission_broker.py
+++ b/products/tasks/backend/logic/services/permission_broker.py
@@ -16,6 +16,7 @@ from django.core.cache import cache
import structlog
+from products.tasks.backend.constants import PERMISSION_REQUEST_ID_MAX_LENGTH
from products.tasks.backend.logic.services.agent_command import send_agent_command
from products.tasks.backend.logic.services.connection_token import create_sandbox_connection_token
from products.tasks.backend.logic.services.run_actor import get_actor_distinct_id, get_task_run_credential_user
@@ -83,7 +84,12 @@ def parse_permission_request(event_data: dict[str, Any]) -> dict[str, Any] | Non
request_id = payload.get("requestId")
tool_call = payload.get("toolCall")
options = _normalize_permission_options(payload.get("options"))
- if not isinstance(request_id, str) or not request_id or not isinstance(tool_call, dict) or not options:
+ if (
+ not isinstance(request_id, str)
+ or not 0 < len(request_id) <= PERMISSION_REQUEST_ID_MAX_LENGTH
+ or not isinstance(tool_call, dict)
+ or not options
+ ):
return None
return {
diff --git a/products/tasks/backend/logic/services/tests/test_permission_broker.py b/products/tasks/backend/logic/services/tests/test_permission_broker.py
index 8b632964a666..4aa11cc9c206 100644
--- a/products/tasks/backend/logic/services/tests/test_permission_broker.py
+++ b/products/tasks/backend/logic/services/tests/test_permission_broker.py
@@ -6,6 +6,8 @@ from unittest.mock import MagicMock, patch
from django.core.cache import cache
from django.test import SimpleTestCase
+from parameterized import parameterized
+
from products.tasks.backend.logic.services.permission_broker import (
parse_permission_request,
try_auto_respond_permission_request,
@@ -16,10 +18,11 @@ BROKER = "products.tasks.backend.logic.services.permission_broker"
class TestParsePermissionRequest(SimpleTestCase):
- def test_parses_bare_and_notification_shapes(self) -> None:
+ @parameterized.expand([("normal", "perm-1", True), ("max", "r" * 200, True), ("too_long", "r" * 201, False)])
+ def test_parses_bare_and_notification_shapes(self, _name: str, request_id: str, valid: bool) -> None:
bare = {
"type": "permission_request",
- "requestId": "perm-1",
+ "requestId": request_id,
"options": [
{"kind": "allow_once", "name": "Yes", "optionId": "allow"},
{"kind": "reject_once", "name": "No", "optionId": "reject"},
@@ -36,8 +39,11 @@ class TestParsePermissionRequest(SimpleTestCase):
for event in (bare, notification):
parsed = parse_permission_request(event)
+ if not valid:
+ assert parsed is None
+ continue
assert parsed is not None
- assert parsed["request_id"] == "perm-1"
+ assert parsed["request_id"] == request_id
assert [option["optionId"] for option in parsed["options"]] == ["allow", "reject"]
def test_rejects_non_permission_events(self) -> None:
diff --git a/products/tasks/backend/logic/stream/event_ingest.py b/products/tasks/backend/logic/stream/event_ingest.py
index 02b57f643202..850dcb603fad 100644
--- a/products/tasks/backend/logic/stream/event_ingest.py
+++ b/products/tasks/backend/logic/stream/event_ingest.py
@@ -19,11 +19,13 @@ from jwt import PyJWTError
from posthog.ph_client import ph_scoped_capture
+from products.tasks.backend.awaiting_input import dispatch_awaiting_input
from products.tasks.backend.facade.api import signal_workflow_completion
from products.tasks.backend.logic.services.connection_token import (
SandboxEventIngestTokenPayload,
validate_sandbox_event_ingest_token,
)
+from products.tasks.backend.logic.services.permission_broker import parse_permission_request
from products.tasks.backend.logic.services.process_killed import (
PROCESS_KILLED_EVENT,
ProcessKilledNotice,
@@ -603,6 +605,11 @@ async def _heartbeat_workflow_if_needed(redis_stream: TaskRunRedisStream, run_id
if not dispatched:
await redis_stream.release_first_agent_activity()
+ if permission_request := parse_permission_request(event):
+ # A pending permission request or question holds the turn open, so no turn-complete push
+ # reaches the user until they answer it.
+ await sync_to_async(_notify_awaiting_input, thread_sensitive=True)(run_id, permission_request["request_id"])
+
if is_turn_complete(event):
await redis_stream.set_agent_active(False)
if pi_turn_error(event):
@@ -684,6 +691,23 @@ def _dispatch_turn_completed_sync(run_id: str, *, succeeded: bool = False, turn_
dispatch_turn_completed(task_run, turn_completed=turn_completed)
+def _notify_awaiting_input(run_id: str, request_id: str) -> None:
+ if not settings.TEST:
+ close_old_connections()
+
+ try:
+ task_run = TaskRun.objects.select_related("task__created_by").get(id=run_id)
+ except TaskRun.DoesNotExist:
+ logger.warning("task_run_event_ingest_awaiting_input_run_missing", run_id=run_id)
+ return
+ except (OperationalError, InterfaceError):
+ # The event is already accepted, so a failed lookup loses only the push and must not fail the request.
+ logger.warning("task_run_event_ingest_awaiting_input_lookup_failed", run_id=run_id, exc_info=True)
+ return
+
+ dispatch_awaiting_input(task_run, request_id=request_id)
+
+
async def _dispatch_turn_failed(run_id: str) -> None:
await sync_to_async(_dispatch_turn_failed_sync, thread_sensitive=True)(run_id)
diff --git a/products/tasks/backend/presentation/serializers.py b/products/tasks/backend/presentation/serializers.py
index 2197c6c5212e..8f40204bd2ea 100644
--- a/products/tasks/backend/presentation/serializers.py
+++ b/products/tasks/backend/presentation/serializers.py
@@ -5078,6 +5078,17 @@ class AgentProxyProcessKilledSerializer(serializers.Serializer):
memory_limit_bytes = serializers.IntegerField(min_value=0, help_text="Sandbox memory limit, in bytes.")
+class AgentProxyCallbackKind(models.TextChoices):
+ HEARTBEAT = "heartbeat", "Heartbeat"
+ AWAITING_INPUT = "awaiting_input", "Awaiting input"
+ PERMISSION_REQUESTED = "permission_requested", "Permission requested"
+ TURN_FAILED = "turn_failed", "Turn failed"
+ COMMAND_DISPATCHED = "command_dispatched", "Command dispatched"
+ AGENT_ACTIVITY = "agent_activity", "Agent activity"
+ BUDGET_STEER = "budget_steer", "Budget steer"
+ PROCESS_KILLED = "process_killed", "Process killed"
+
+
class AgentProxyCallbackRequestSerializer(serializers.Serializer):
"""Request body for the agent-proxy side-effect callback.
@@ -5088,19 +5099,13 @@ class AgentProxyCallbackRequestSerializer(serializers.Serializer):
"""
kind = serializers.ChoiceField(
- choices=[
- "heartbeat",
- "awaiting_input",
- "turn_failed",
- "command_dispatched",
- "agent_activity",
- "budget_steer",
- "process_killed",
- ],
+ choices=AgentProxyCallbackKind.choices,
help_text=(
"Side effect to dispatch. 'heartbeat' signals the Temporal workflow to reset its "
"inactivity timer. 'awaiting_input' fires a mobile push notification when an "
- "interactive run finishes a turn and is waiting for user input. 'turn_failed' fails "
+ "interactive run finishes a turn and is waiting for user input. 'permission_requested' fires a "
+ "'needs your input' push when an interactive run waits on a permission request or a question. "
+ "'turn_failed' fails "
"the run outright when a pi turn ends in a runtime error. 'command_dispatched' "
"and 'agent_activity' record boot milestones. 'budget_steer' captures the agent's budget warning. "
"'process_killed' captures a sandbox memory watchdog kill."
@@ -5144,7 +5149,15 @@ class AgentProxyCallbackRequestSerializer(serializers.Serializer):
sequence = serializers.IntegerField(
required=False,
min_value=1,
- help_text="Event sequence used to deduplicate a budget steer or a process kill.",
+ help_text=(
+ "Event sequence used to deduplicate a budget steer or a process kill. "
+ "Also sent with 'permission_requested' to identify the event."
+ ),
+ )
+ request_id = serializers.CharField(
+ required=False,
+ max_length=200,
+ help_text="ID of the pending permission request that 'permission_requested' reports.",
)
timestamp = serializers.DateTimeField(required=False, help_text="Original event time, preserved across retries.")
stage = serializers.CharField(required=False, help_text="Budget stage: warn or critical.")
diff --git a/products/tasks/backend/push_dispatcher.py b/products/tasks/backend/push_dispatcher.py
index cc71476135ca..9d101a028b47 100644
--- a/products/tasks/backend/push_dispatcher.py
+++ b/products/tasks/backend/push_dispatcher.py
@@ -13,7 +13,7 @@ Two guards before we enqueue:
from __future__ import annotations
-from collections.abc import Collection
+from collections.abc import Callable, Collection
from typing import TYPE_CHECKING, Literal, cast
from django.conf import settings
@@ -39,7 +39,8 @@ if TYPE_CHECKING:
logger = structlog.get_logger(__name__)
-PUSH_TITLE = "posthog"
+PUSH_TITLE = "PostHog"
+AWAITING_INPUT_COOLDOWN_SECONDS = 30
# Cooldown windows per push kind. Terminal pushes get a longer window because
# they should only fire once per run lifetime — anything more is a retry.
@@ -50,7 +51,7 @@ _COOLDOWN_SECONDS: dict[PushKind, int] = {
"completed": 600,
"failed": 600,
"cancelled": 600,
- "awaiting": 30,
+ "awaiting": AWAITING_INPUT_COOLDOWN_SECONDS,
"turn_completed": 30,
"thread_message": 600,
"handoff": 600,
@@ -73,10 +74,12 @@ def notify_task_run_cancelled(task_run: TaskRun) -> None:
_enqueue(task_run, kind="cancelled", body=f'"{_task_title(task_run)}" was cancelled')
-def notify_task_run_awaiting_input(task_run: TaskRun) -> None:
- """Fire a push notification when an interactive run is waiting for user input."""
+def notify_task_run_awaiting_input(task_run: TaskRun, *, on_enqueued: Callable[[], None] | None = None) -> bool:
+ """Return whether a push was scheduled; on_enqueued confirms Celery accepted it after commit."""
_project_awaiting_input_activity(task_run)
- _enqueue(task_run, kind="awaiting", body=f'"{_task_title(task_run)}" needs your input')
+ return _enqueue(
+ task_run, kind="awaiting", body=f'"{_task_title(task_run)}" needs your input', on_enqueued=on_enqueued
+ )
def notify_task_run_turn_completed(task_run: TaskRun) -> None:
@@ -203,7 +206,7 @@ def _task_title(task_run: TaskRun) -> str:
return title or "Untitled task"
-def _enqueue(task_run: TaskRun, *, kind: PushKind, body: str) -> None:
+def _enqueue(task_run: TaskRun, *, kind: PushKind, body: str, on_enqueued: Callable[[], None] | None = None) -> bool:
"""Best-effort: this function MUST NOT raise.
Wrap the whole body in a bare ``except Exception`` so a DB outage,
@@ -212,7 +215,7 @@ def _enqueue(task_run: TaskRun, *, kind: PushKind, body: str) -> None:
and fail the surrounding task-lifecycle activity.
"""
try:
- _enqueue_inner(task_run, kind=kind, body=body)
+ return _enqueue_inner(task_run, kind=kind, body=body, on_enqueued=on_enqueued)
except Exception as exc:
PUSH_DISPATCHER_FAILURES_TOTAL.labels(kind=kind, reason=_failure_reason(exc)).inc()
logger.warning(
@@ -222,20 +225,23 @@ def _enqueue(task_run: TaskRun, *, kind: PushKind, body: str) -> None:
kind=kind,
exc_info=True,
)
+ return False
def _failure_reason(exc: BaseException) -> str:
return "db_connection" if isinstance(exc, OperationalError | InterfaceError) else "other"
-def _enqueue_inner(task_run: TaskRun, *, kind: PushKind, body: str) -> None:
+def _enqueue_inner(
+ task_run: TaskRun, *, kind: PushKind, body: str, on_enqueued: Callable[[], None] | None = None
+) -> bool:
if not settings.TEST:
close_old_connections()
user = task_run.task.created_by
if user is None:
PUSH_DISPATCHER_OUTCOMES_TOTAL.labels(kind=kind, outcome="no_recipient").inc()
- return
+ return False
if not user.teams.filter(id=task_run.team_id).exists():
PUSH_DISPATCHER_OUTCOMES_TOTAL.labels(kind=kind, outcome="access_denied").inc()
@@ -245,15 +251,16 @@ def _enqueue_inner(task_run: TaskRun, *, kind: PushKind, body: str) -> None:
task_id=str(task_run.task_id),
team_id=task_run.team_id,
)
- return
+ return False
- _enqueue_user(
+ return _enqueue_user(
user,
task=task_run.task,
kind=kind,
cooldown_subject=str(task_run.id),
body=body,
data={"taskId": str(task_run.task_id), "taskRunId": str(task_run.id)},
+ on_enqueued=on_enqueued,
)
@@ -265,27 +272,41 @@ def _enqueue_user(
cooldown_subject: str,
body: str,
data: dict[str, str],
-) -> None:
+ on_enqueued: Callable[[], None] | None = None,
+) -> bool:
if task.is_scout_experiment:
- return
+ return False
cooldown_key = f"push_notification:{cooldown_subject}:{kind}"
if not get_tasks_cache().add(cooldown_key, True, timeout=_COOLDOWN_SECONDS[kind]):
PUSH_DISPATCHER_OUTCOMES_TOTAL.labels(kind=kind, outcome="cooldown_deduped").inc()
logger.debug("push_dispatcher.cooldown_hit", subject=cooldown_subject, kind=kind)
- return
+ return False
suppressed = _suppressed_push_token_ids_for_task(user_id=user.id, task_id=task.id)
data["notificationKind"] = kind
+ data["projectId"] = str(task.team_id)
# on_commit so we never schedule a push for a write that ends up rolling
# back. Outside an atomic block this fires immediately, which is fine.
+ accepted = True
+
def dispatch() -> None:
+ nonlocal accepted
+ try:
+ send_user_push.delay(user.id, PUSH_TITLE, body, data, suppressed)
+ except Exception as exc:
+ accepted = False
+ PUSH_DISPATCHER_FAILURES_TOTAL.labels(kind=kind, reason=_failure_reason(exc)).inc()
+ logger.warning("push_dispatcher.enqueue_failed", task_id=str(task.id), kind=kind, exc_info=True)
+ return
outcome = "presence_suppressed" if suppressed else "enqueued"
PUSH_DISPATCHER_OUTCOMES_TOTAL.labels(kind=kind, outcome=outcome).inc()
- send_user_push.delay(user.id, PUSH_TITLE, body, data, suppressed)
+ if on_enqueued is not None:
+ on_enqueued()
transaction.on_commit(dispatch)
+ return accepted
def _suppressed_push_token_ids_for_task(*, user_id: int, task_id) -> list[str]:
diff --git a/products/tasks/backend/temporal/process_task/activities/relay_sandbox_events.py b/products/tasks/backend/temporal/process_task/activities/relay_sandbox_events.py
index 27483aeea995..20b1d8f9e3ed 100644
--- a/products/tasks/backend/temporal/process_task/activities/relay_sandbox_events.py
+++ b/products/tasks/backend/temporal/process_task/activities/relay_sandbox_events.py
@@ -20,6 +20,7 @@ from posthog.dataclasses import frozen
from posthog.temporal.common.utils import close_db_connections
from products.slack_app.backend.facade.api import agent_plan_steps, phase_for_tool_call, tool_call_from_acp_update
+from products.tasks.backend.awaiting_input import dispatch_awaiting_input
from products.tasks.backend.facade.api import record_run_failure_cause
from products.tasks.backend.feature_flags import run_stream_presence_gated, run_stream_thin_tail
from products.tasks.backend.logic.services.agent_command import (
@@ -1083,14 +1084,16 @@ def _persist_final_message(run_id: str, text: str) -> None:
def _broker_permission_request(task_run: TaskRunModel, permission_request: dict) -> None:
- """Auto-allow a Slack-origin run's sandbox permission request (see permission_broker)."""
+ """Auto-allow a Slack-origin run's sandbox permission request (see permission_broker), and
+ otherwise tell the run's creator that the agent waits for their answer."""
try:
# The close_old_connections guard mirrors event_ingest: this thread's pooled
# connection is never health-checked by Django (gated off in tests, where
# it would close the test transaction's connection).
if not settings.TEST:
close_old_connections()
- try_auto_respond_permission_request(task_run, permission_request)
+ if not try_auto_respond_permission_request(task_run, permission_request):
+ dispatch_awaiting_input(task_run, request_id=permission_request["request_id"])
except Exception:
logger.warning(
"relay_sandbox_events_permission_broker_failed",
diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_relay_sandbox_events.py b/products/tasks/backend/temporal/process_task/activities/tests/test_relay_sandbox_events.py
index 6aecd28caa82..a03ed7dea692 100644
--- a/products/tasks/backend/temporal/process_task/activities/tests/test_relay_sandbox_events.py
+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_relay_sandbox_events.py
@@ -8,7 +8,9 @@ from typing import cast
from uuid import uuid4
import pytest
-from unittest.mock import AsyncMock, MagicMock, call
+from unittest.mock import ANY, AsyncMock, MagicMock, call
+
+from django.core.cache.backends.locmem import LocMemCache
import httpx
import httpx_sse
@@ -1214,7 +1216,9 @@ class TestRelaySandboxEventsErrorHandling:
# mode="interactive" keeps the turn-complete thread-update path out of
# this test, which only cares about permission dispatch.
task_run = SimpleNamespace(id="run-id", mode="interactive")
- dispatch_mock = MagicMock()
+ dispatch_mock = MagicMock(wraps=relay_sandbox_events_module._broker_permission_request)
+ notify = MagicMock()
+ permission_cache = LocMemCache(str(uuid4()), {})
class SuccessfulEventSource:
response = SimpleNamespace(raise_for_status=lambda: None)
@@ -1242,6 +1246,9 @@ class TestRelaySandboxEventsErrorHandling:
monkeypatch.setattr(relay_sandbox_events_module, "_background_heartbeat", fake_background_heartbeat)
monkeypatch.setattr(relay_sandbox_events_module.asyncio, "to_thread", fake_to_thread)
monkeypatch.setattr(relay_sandbox_events_module, "_broker_permission_request", dispatch_mock)
+ monkeypatch.setattr(relay_sandbox_events_module, "try_auto_respond_permission_request", lambda *_args: False)
+ monkeypatch.setattr("products.tasks.backend.awaiting_input.get_tasks_cache", lambda: permission_cache)
+ monkeypatch.setattr("products.tasks.backend.push_dispatcher.notify_task_run_awaiting_input", notify)
await _relay_loop(
events_url="https://sandbox.example/events",
@@ -1262,6 +1269,7 @@ class TestRelaySandboxEventsErrorHandling:
"options": [{"optionId": "allow", "kind": "allow_once", "name": ""}],
},
)
+ notify.assert_called_once_with(task_run, on_enqueued=ANY)
redis_stream.mark_complete.assert_awaited_once()
redis_stream.mark_error.assert_not_awaited()
diff --git a/products/tasks/backend/tests/test_agent_proxy_callback.py b/products/tasks/backend/tests/test_agent_proxy_callback.py
index 3355ace0830e..6cc7f9b22fd1 100644
--- a/products/tasks/backend/tests/test_agent_proxy_callback.py
+++ b/products/tasks/backend/tests/test_agent_proxy_callback.py
@@ -2,7 +2,7 @@ import json
from typing import Any
from uuid import NAMESPACE_URL, uuid5
-from unittest.mock import patch
+from unittest.mock import ANY, patch
from django.test import TestCase, override_settings
@@ -355,6 +355,20 @@ class TestAgentProxyCallback(TestCase):
notify.assert_not_called()
signal_turn_completed.assert_called_once_with(succeeded=expected_succeeded)
+ @parameterized.expand([("interactive", True), ("background", False)])
+ def test_permission_requested_pushes_only_for_interactive_run(self, mode: str, expect_push: bool) -> None:
+ run = self.task.create_run(mode=mode)
+ body = self._body(kind="permission_requested", agent_active=False, sequence=3, request_id="request-1")
+ with patch(
+ "products.tasks.backend.push_dispatcher.notify_task_run_awaiting_input", return_value=True
+ ) as notify:
+ response = self._post(body, token=self._token(run), run_id=str(run.id))
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json()["dispatched"], expect_push)
+ self.assertEqual(notify.call_count, int(expect_push))
+ if expect_push:
+ notify.assert_called_once_with(run, on_enqueued=ANY)
+
def test_turn_failed_signals_workflow_completion_as_failed(self) -> None:
with patch(
"products.tasks.backend.agent_proxy_callback.signal_workflow_completion"
@@ -372,6 +386,7 @@ class TestAgentProxyCallback(TestCase):
("command_dispatched", {"agent_active": False}),
("agent_activity", {"agent_active": True}),
("awaiting_input", {"agent_active": False}),
+ ("permission_requested", {"agent_active": False}),
("turn_failed", {"agent_active": False}),
("process_killed", {"agent_active": False, "sequence": 5, "process_killed": PROCESS_KILLED}),
]
diff --git a/products/tasks/backend/tests/test_awaiting_input.py b/products/tasks/backend/tests/test_awaiting_input.py
new file mode 100644
index 000000000000..01c298d3e454
--- /dev/null
+++ b/products/tasks/backend/tests/test_awaiting_input.py
@@ -0,0 +1,91 @@
+from collections.abc import Callable
+from uuid import uuid4
+
+import time_machine
+from unittest.mock import patch
+
+from django.core.cache.backends.locmem import LocMemCache
+from django.test import SimpleTestCase
+
+from parameterized import parameterized
+
+from products.tasks.backend.awaiting_input import dispatch_awaiting_input
+from products.tasks.backend.models import TaskRun
+
+
+class TestAwaitingInput(SimpleTestCase):
+ @staticmethod
+ def _enqueue_successfully(task_run: TaskRun, *, on_enqueued: Callable[[], None]) -> bool:
+ on_enqueued()
+ return True
+
+ @parameterized.expand(
+ [
+ ("replayed_request", "request-1", False, 31, 1),
+ ("new_request", "request-2", False, 31, 2),
+ ("rapid_new_request", "request-2", False, 0, 1),
+ ("another_run", "request-1", True, 0, 2),
+ ("maximum_length", "r" * 200, False, 31, 2),
+ ]
+ )
+ def test_deduplicates_by_run_and_request_across_replays(
+ self, _name: str, request_id: str, another_run: bool, elapsed: int, expected_pushes: int
+ ) -> None:
+ task_run = TaskRun(id=uuid4(), state={"mode": "interactive"})
+ replay_run = TaskRun(id=task_run.id, state=task_run.state)
+ if another_run:
+ replay_run.id = uuid4()
+ cache = LocMemCache(str(uuid4()), {})
+
+ with (
+ patch("products.tasks.backend.awaiting_input.get_tasks_cache", return_value=cache),
+ patch(
+ "products.tasks.backend.push_dispatcher.notify_task_run_awaiting_input",
+ side_effect=self._enqueue_successfully,
+ ) as notify,
+ time_machine.travel("2026-10-16T12:00:00Z", tick=False) as clock,
+ ):
+ self.assertTrue(dispatch_awaiting_input(task_run, request_id="request-1"))
+ clock.shift(elapsed)
+ dispatched = dispatch_awaiting_input(replay_run, request_id=request_id)
+
+ self.assertEqual(dispatched, expected_pushes == 2)
+ self.assertEqual(notify.call_count, expected_pushes)
+
+ @parameterized.expand([("background", "request-1"), ("interactive", ""), ("interactive", "r" * 201)])
+ def test_skips_without_interactive_permission_request(self, mode: str, request_id: str) -> None:
+ with patch("products.tasks.backend.push_dispatcher.notify_task_run_awaiting_input") as notify:
+ self.assertFalse(dispatch_awaiting_input(TaskRun(state={"mode": mode}), request_id=request_id))
+ notify.assert_not_called()
+
+ def test_request_burst_does_not_allocate_dedupe_keys(self) -> None:
+ task_run = TaskRun(id=uuid4(), state={"mode": "interactive"})
+ cache = LocMemCache(str(uuid4()), {})
+ with (
+ patch("products.tasks.backend.awaiting_input.get_tasks_cache", return_value=cache),
+ patch(
+ "products.tasks.backend.push_dispatcher.notify_task_run_awaiting_input",
+ side_effect=self._enqueue_successfully,
+ ) as notify,
+ time_machine.travel("2026-10-20T12:00:00Z", tick=False),
+ ):
+ self.assertTrue(dispatch_awaiting_input(task_run, request_id="first"))
+ for index in range(100):
+ self.assertFalse(dispatch_awaiting_input(task_run, request_id=f"request-{index}"))
+ self.assertTrue(cache.get(f"tasks:awaiting_input:cooldown:{task_run.id}"))
+ self.assertTrue(cache.get(f"tasks:awaiting_input:v1:{task_run.id}:first"))
+ self.assertEqual(
+ cache.get_many([f"tasks:awaiting_input:v1:{task_run.id}:request-{index}" for index in range(100)]),
+ {},
+ )
+ notify.assert_called_once()
+
+ def test_cache_failure_does_not_fail_event_delivery(self) -> None:
+ with (
+ patch(
+ "products.tasks.backend.awaiting_input.get_tasks_cache", side_effect=RuntimeError("cache unavailable")
+ ),
+ patch("products.tasks.backend.push_dispatcher.notify_task_run_awaiting_input") as notify,
+ ):
+ self.assertFalse(dispatch_awaiting_input(TaskRun(state={"mode": "interactive"}), request_id="request-1"))
+ notify.assert_not_called()
diff --git a/products/tasks/backend/tests/test_event_ingest.py b/products/tasks/backend/tests/test_event_ingest.py
index c70355e01db4..a4ea8010f683 100644
--- a/products/tasks/backend/tests/test_event_ingest.py
+++ b/products/tasks/backend/tests/test_event_ingest.py
@@ -664,6 +664,59 @@ class TestTaskRunEventIngest(TestCase):
int(stop_reason == "idle_resume"),
)
+ @parameterized.expand(
+ [
+ ("interactive", "interactive", True),
+ ("background", "background", False),
+ ]
+ )
+ @override_settings(SANDBOX_JWT_PRIVATE_KEY=TEST_RSA_PRIVATE_KEY)
+ def test_permission_request_ingest_pushes_awaiting_input(self, _name: str, mode: str, expect_push: bool) -> None:
+ creator = User.objects.create_and_join(self.organization, "[email protected]", None)
+ self.task.created_by = creator
+ self.task.save(update_fields=["created_by"])
+ self.task_run.state = {**(self.task_run.state or {}), "mode": mode}
+ self.task_run.save(update_fields=["state"])
+ token = self._create_token()
+ permission_request_params: dict[str, object] = {
+ "requestId": "request-1",
+ "toolCall": {"title": "Run the test suite"},
+ "options": [{"optionId": "allow", "kind": "allow_once", "name": "Allow"}],
+ }
+ permission_request = {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/permission_request",
+ "params": permission_request_params,
+ },
+ }
+
+ with (
+ patch("products.tasks.backend.push_dispatcher.send_user_push.delay") as send_push,
+ self.captureOnCommitCallbacks(execute=True),
+ ):
+ status, body = self._call_ingest(token, [{"seq": 1, "event": permission_request}])
+ self.assertEqual((status, body["accepted"]), (200, 1))
+ permission_request_params["requestId"] = "request-2"
+ status, body = self._call_ingest(token, [{"seq": 2, "event": permission_request}])
+
+ self.assertEqual((status, body["accepted"]), (200, 1))
+ if not expect_push:
+ send_push.assert_not_called()
+ return
+ send_push.assert_called_once()
+ user_id, title, push_body, data, _suppressed = send_push.call_args.args
+ self.assertEqual((user_id, title, push_body), (creator.id, "PostHog", '"Test Task" needs your input'))
+ self.assertEqual(
+ data,
+ {
+ "taskId": str(self.task.id),
+ "taskRunId": str(self.task_run.id),
+ "notificationKind": "awaiting",
+ "projectId": str(self.task.team_id),
+ },
+ )
+
@parameterized.expand(
[
(runtime, reason, reason == "end_turn")
diff --git a/products/tasks/backend/tests/test_push_dispatcher.py b/products/tasks/backend/tests/test_push_dispatcher.py
index d33a73299c1b..3a9cefe52c9d 100644
--- a/products/tasks/backend/tests/test_push_dispatcher.py
+++ b/products/tasks/backend/tests/test_push_dispatcher.py
@@ -1,9 +1,11 @@
+from contextlib import nullcontext
from uuid import uuid4
+import time_machine
from unittest.mock import patch
from django.core.cache import cache
-from django.db import InterfaceError, OperationalError
+from django.db import InterfaceError, OperationalError, transaction
from django.test import TestCase
from parameterized import parameterized
@@ -12,6 +14,7 @@ from prometheus_client import REGISTRY
from posthog.models import Organization, OrganizationMembership, Team, User
from posthog.tasks.push_notifications import send_user_push
+from products.tasks.backend.awaiting_input import dispatch_awaiting_input
from products.tasks.backend.models import Task, TaskRun
from products.tasks.backend.push_dispatcher import (
notify_task_handoff,
@@ -61,9 +64,10 @@ class TestPushDispatcher(TestCase):
mock_delay.assert_called_once()
user_id, title, body, data, suppressed = mock_delay.call_args.args
self.assertEqual(user_id, self.user.id)
- self.assertEqual(title, "posthog")
+ self.assertEqual(title, "PostHog")
self.assertIn(expected_body_fragment, body)
self.assertEqual(data["taskId"], str(self.task.id))
+ self.assertEqual(data["projectId"], str(self.task.team_id))
self.assertEqual(data["taskRunId"], str(self.task_run.id))
self.assertEqual(data["notificationKind"], _name)
# No presence rows in this test's setUp, so nothing to suppress.
@@ -101,6 +105,63 @@ class TestPushDispatcher(TestCase):
notify_task_run_turn_completed(self.task_run)
self.assertEqual(mock_delay.call_count, 3)
+ @patch("products.tasks.backend.push_dispatcher.send_user_push.delay")
+ def test_awaiting_cooldown_is_shared_by_permission_requests_and_turn_end(self, mock_delay) -> None:
+ self.task_run.state = {"mode": "interactive"}
+ with time_machine.travel("2026-10-20T12:00:00Z", tick=False) as clock:
+ with self.captureOnCommitCallbacks(execute=True):
+ dispatch_awaiting_input(self.task_run, request_id="request-1")
+ dispatch_awaiting_input(self.task_run, request_id="request-2")
+ notify_task_run_awaiting_input(self.task_run)
+ self.assertEqual(mock_delay.call_count, 1)
+
+ clock.shift(31)
+ with self.captureOnCommitCallbacks(execute=True):
+ dispatch_awaiting_input(self.task_run, request_id="request-1")
+ dispatch_awaiting_input(self.task_run, request_id="request-3")
+ self.assertEqual(mock_delay.call_count, 2)
+
+ @parameterized.expand(["prepare", "immediate", "deferred", "rollback"])
+ def test_awaiting_request_can_retry_after_failed_enqueue(self, failure: str) -> None:
+ self.task_run.state = {"mode": "interactive"}
+ with (
+ time_machine.travel("2026-10-20T12:00:00Z", tick=False) as clock,
+ patch("products.tasks.backend.push_dispatcher._project_awaiting_input_activity"),
+ patch("products.tasks.backend.push_dispatcher.send_user_push.delay") as send_push,
+ ):
+ send_push.side_effect = RuntimeError("broker unavailable")
+ with (
+ patch(
+ "products.tasks.backend.push_dispatcher._suppressed_push_token_ids_for_task",
+ side_effect=OperationalError("connection unavailable"),
+ )
+ if failure == "prepare"
+ else nullcontext(),
+ patch("products.tasks.backend.push_dispatcher.transaction.on_commit", side_effect=lambda fn: fn())
+ if failure == "immediate"
+ else nullcontext(),
+ self.captureOnCommitCallbacks(execute=True),
+ transaction.atomic(),
+ ):
+ scheduled = dispatch_awaiting_input(self.task_run, request_id="retry-request")
+ self.assertEqual(scheduled, failure in ("deferred", "rollback"))
+ if failure == "rollback":
+ transaction.set_rollback(True)
+ self.assertEqual(send_push.call_count, int(failure in ("immediate", "deferred")))
+
+ clock.shift(31)
+ send_push.reset_mock()
+ send_push.side_effect = None
+ with self.captureOnCommitCallbacks(execute=True):
+ self.assertTrue(dispatch_awaiting_input(self.task_run, request_id="retry-request"))
+ send_push.assert_not_called()
+ send_push.assert_called_once()
+
+ clock.shift(31)
+ with self.captureOnCommitCallbacks(execute=True):
+ self.assertFalse(dispatch_awaiting_input(self.task_run, request_id="retry-request"))
+ send_push.assert_called_once()
+
@patch("products.tasks.backend.push_dispatcher.send_user_push.delay")
def test_handoff_cooldown_distinguishes_announcements(self, mock_delay):
announcement_id = uuid4()