|
| 1 | +"""Acceptance test for the record-ingest contract on POST /sessions/records/ingest. |
| 2 | +
|
| 3 | +Pins the regression from debug/qa-sessions-rebase/FINDING-record-ingest-422.md: the runner |
| 4 | +posts a 16-hex OTel span_id, and the API had typed span_id as UUID (32 hex) — so every |
| 5 | +ingest 422'd and session records silently dropped. This drives the endpoint with the body the |
| 6 | +runner actually sends and asserts it is accepted (200) and lands as a queryable record whose |
| 7 | +span_id round-trips. |
| 8 | +
|
| 9 | +Requires a live stack (AGENTA_API_URL/AGENTA_AUTH_KEY) with the record worker running — see |
| 10 | +the pytest `acceptance` marker. |
| 11 | +""" |
| 12 | + |
| 13 | +import time |
| 14 | +import uuid |
| 15 | + |
| 16 | + |
| 17 | +class TestRecordIngestContract: |
| 18 | + """POST /sessions/records/ingest accepts the runner-shaped body (16-hex span_id) and the |
| 19 | + record becomes queryable with the span id intact.""" |
| 20 | + |
| 21 | + def test_runner_shaped_ingest_is_accepted_and_persists(self, authed_api): |
| 22 | + session_id = str(uuid.uuid4()) |
| 23 | + # Exactly the shape services/runner/src/sessions/persist.ts posts: a 16-hex OTel span |
| 24 | + # id (NOT a UUID), a turn_id, a per-turn record_index, and the event as attributes. |
| 25 | + span_id = uuid.uuid4().hex[:16] |
| 26 | + turn_id = f"turn-{uuid.uuid4().hex[:8]}" |
| 27 | + |
| 28 | + ingest = authed_api( |
| 29 | + "POST", |
| 30 | + "/sessions/records/ingest", |
| 31 | + json={ |
| 32 | + "session_id": session_id, |
| 33 | + "record_index": 0, |
| 34 | + "timestamp": "2026-07-18T00:00:00.000Z", |
| 35 | + "record_source": "agent", |
| 36 | + "record_type": "message", |
| 37 | + "attributes": {"type": "message", "text": "hello from the runner"}, |
| 38 | + "turn_id": turn_id, |
| 39 | + "span_id": span_id, |
| 40 | + }, |
| 41 | + ) |
| 42 | + assert ingest.status_code == 200, ingest.text |
| 43 | + assert ingest.json() == {"ok": True} |
| 44 | + |
| 45 | + # The record flows through Redis -> worker -> DB; poll the query endpoint until it lands. |
| 46 | + record = None |
| 47 | + deadline = time.time() + 20 |
| 48 | + while time.time() < deadline: |
| 49 | + query = authed_api( |
| 50 | + "POST", "/sessions/records/query", json={"session_id": session_id} |
| 51 | + ) |
| 52 | + assert query.status_code == 200, query.text |
| 53 | + records = query.json().get("records", []) |
| 54 | + if records: |
| 55 | + record = records[0] |
| 56 | + break |
| 57 | + time.sleep(1) |
| 58 | + |
| 59 | + assert record is not None, "ingested record never became queryable" |
| 60 | + assert record["turn_id"] == turn_id |
| 61 | + assert record["span_id"] == span_id |
| 62 | + |
| 63 | + def test_a_uuid_span_id_is_rejected(self, authed_api): |
| 64 | + """A 32-hex UUID is not a span id; the endpoint must reject it (422), which is exactly |
| 65 | + the validation that a 16-hex span id must pass.""" |
| 66 | + response = authed_api( |
| 67 | + "POST", |
| 68 | + "/sessions/records/ingest", |
| 69 | + json={ |
| 70 | + "session_id": str(uuid.uuid4()), |
| 71 | + "record_source": "agent", |
| 72 | + "span_id": uuid.uuid4().hex, # 32 hex — a UUID, not a span id |
| 73 | + }, |
| 74 | + ) |
| 75 | + assert response.status_code == 422, response.text |
0 commit comments