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.

chore(tasks): pin the turn-event predicate contract across both runtimes (#102379)

Alessandro Pogliaghi769693fc911d
diff --git a/.github/workflows/ci-agent-proxy.yml b/.github/workflows/ci-agent-proxy.yml
index 21e2cabd6eda..40cd716f60bd 100644
--- a/.github/workflows/ci-agent-proxy.yml
+++ b/.github/workflows/ci-agent-proxy.yml
@@ -46,6 +46,7 @@ jobs:
filters: |
agent_proxy:
- 'services/agent-proxy/**'
+ - 'ee/hogai/sandbox/turn_event_contract.json'
- '.github/workflows/ci-agent-proxy.yml'
agent-proxy:
diff --git a/ee/hogai/sandbox/test_turn_event_contract.py b/ee/hogai/sandbox/test_turn_event_contract.py
new file mode 100644
index 000000000000..2e7a44251160
--- /dev/null
+++ b/ee/hogai/sandbox/test_turn_event_contract.py
@@ -0,0 +1,21 @@
+import json
+from pathlib import Path
+
+import pytest
+
+from ee.hogai.sandbox.types import is_idle_resume_turn_complete, is_turn_complete, pi_turn_error
+
+
+ ("event", "expected"),
+ [
+ pytest.param(case["event"], case["expect"], id=case["name"])
+ for case in json.loads(Path(__file__).with_name("turn_event_contract.json").read_text())
+ ],
+)
+def test_turn_event_contract(event: dict[str, object], expected: dict[str, bool]) -> None:
+ assert {
+ "turn_complete": is_turn_complete(event),
+ "idle_resume": is_idle_resume_turn_complete(event),
+ "pi_error": pi_turn_error(event),
+ } == {key: expected[key] for key in ("turn_complete", "idle_resume", "pi_error")}
diff --git a/ee/hogai/sandbox/turn_event_contract.json b/ee/hogai/sandbox/turn_event_contract.json
new file mode 100644
index 000000000000..51f4f3b7f4a2
--- /dev/null
+++ b/ee/hogai/sandbox/turn_event_contract.json
@@ -0,0 +1,357 @@
+[
+ {
+ "name": "synthetic without stop reason",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete"
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "synthetic end turn",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "params": {
+ "stopReason": "end_turn"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "idle resume",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "params": {
+ "stopReason": "idle_resume"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": true,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "synthetic unknown stop reason",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "params": {
+ "stopReason": "future_reason"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "synthetic null params",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "params": null
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "synthetic nonobject params",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "params": "idle_resume"
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "idle resume in result",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "result": {
+ "stopReason": "idle_resume"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "raw end turn",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "result": {
+ "stopReason": "end_turn"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "raw idle reason",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "result": {
+ "stopReason": "idle_resume"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "raw other stop reason",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "result": {
+ "stopReason": "max_tokens"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "pi completion",
+ "event": {
+ "type": "pi_event",
+ "event": {
+ "type": "turn_completed"
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "pi end turn",
+ "event": {
+ "type": "pi_event",
+ "event": {
+ "type": "turn_completed",
+ "stopReason": "end_turn"
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "pi runtime error",
+ "event": {
+ "type": "pi_event",
+ "event": {
+ "type": "turn_completed",
+ "stopReason": "error"
+ }
+ },
+ "expect": {
+ "turn_complete": true,
+ "idle_resume": false,
+ "pi_error": true,
+ "session_update": false
+ }
+ },
+ {
+ "name": "pi nonterminal error",
+ "event": {
+ "type": "pi_event",
+ "event": {
+ "type": "assistant_message_chunk",
+ "stopReason": "error"
+ }
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "pi missing event",
+ "event": {
+ "type": "pi_event"
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "pi malformed event",
+ "event": {
+ "type": "pi_event",
+ "event": "turn_completed"
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "session update",
+ "event": {
+ "type": "notification",
+ "notification": {
+ "method": "session/update",
+ "params": {
+ "stopReason": "idle_resume"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": true
+ }
+ },
+ {
+ "name": "wrong outer type",
+ "event": {
+ "type": "console",
+ "notification": {
+ "method": "_posthog/turn_complete",
+ "params": {
+ "stopReason": "idle_resume"
+ }
+ }
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "missing notification",
+ "event": {
+ "type": "notification"
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "null notification",
+ "event": {
+ "type": "notification",
+ "notification": null
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "scalar notification",
+ "event": {
+ "type": "notification",
+ "notification": "turn_completed"
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "array notification",
+ "event": {
+ "type": "notification",
+ "notification": []
+ },
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ },
+ {
+ "name": "empty event",
+ "event": {},
+ "expect": {
+ "turn_complete": false,
+ "idle_resume": false,
+ "pi_error": false,
+ "session_update": false
+ }
+ }
+]
diff --git a/products/tasks/backend/tests/test_event_ingest.py b/products/tasks/backend/tests/test_event_ingest.py
index 4f0020f28b06..6c81d37bb590 100644
--- a/products/tasks/backend/tests/test_event_ingest.py
+++ b/products/tasks/backend/tests/test_event_ingest.py
@@ -2,12 +2,13 @@ import json
import asyncio
import threading
from collections.abc import Sequence
+from pathlib import Path
from uuid import NAMESPACE_URL, uuid5
from unittest.mock import call, patch
from django.db import OperationalError
-from django.test import TestCase, override_settings
+from django.test import SimpleTestCase, TestCase, override_settings
from asgiref.sync import async_to_sync
from parameterized import parameterized
@@ -27,6 +28,7 @@ from products.tasks.backend.logic.stream.event_ingest import (
MAX_EVENT_LINE_BYTES,
MAX_EVENTS_PER_REQUEST,
STREAM_COMPLETE_CONTROL_TYPE,
+ _is_session_update,
handle_task_run_event_ingest,
)
from products.tasks.backend.logic.stream.redis_stream import (
@@ -45,6 +47,19 @@ from ee.hogai.sandbox import PI_RUNTIME_ERROR_MESSAGE
SKIP_COUNTER_SAMPLE = "posthog_tasks_task_run_stream_write_skipped_total"
+class TestSessionUpdateContract(SimpleTestCase):
+ @parameterized.expand(
+ [
+ (case["name"], case["event"], case["expect"]["session_update"])
+ for case in json.loads(
+ (Path(__file__).parents[4] / "ee/hogai/sandbox/turn_event_contract.json").read_text()
+ )
+ ]
+ )
+ def test_session_update_contract(self, _name: str, event: dict[str, object], expected: bool) -> None:
+ self.assertEqual(_is_session_update(event), expected)
+
+
class TestTaskRunEventIngest(TestCase):
def setUp(self) -> None:
super().setUp()
diff --git a/services/agent-proxy/tests/lib/side-effects.test.ts b/services/agent-proxy/tests/lib/side-effects.test.ts
new file mode 100644
index 000000000000..4d6625c680fe
--- /dev/null
+++ b/services/agent-proxy/tests/lib/side-effects.test.ts
@@ -0,0 +1,23 @@
+import { readFileSync } from 'node:fs'
+import { describe, expect, it } from 'vitest'
+
+import { isIdleResumeTurnComplete, isPiTurnError, isSessionUpdate, isTurnComplete } from '../../src/lib/side-effects.js'
+
+const cases = JSON.parse(
+ readFileSync(new URL('../../../../ee/hogai/sandbox/turn_event_contract.json', import.meta.url), 'utf8')
+) as {
+ name: string
+ event: Record<string, unknown>
+ expect: { turn_complete: boolean; idle_resume: boolean; pi_error: boolean; session_update: boolean }
+}[]
+
+describe('turn event contract', () => {
+ it.each(cases)('$name', ({ event, expect: expected }) => {
+ expect({
+ turn_complete: isTurnComplete(event),
+ idle_resume: isIdleResumeTurnComplete(event),
+ pi_error: isPiTurnError(event),
+ session_update: isSessionUpdate(event),
+ }).toEqual(expected)
+ })
+})