Skip to content

Self-improving AI Employees for everyone

test(chat): cover what the chat work never verified

trohitg29d9e8a15cbb
diff --git a/server/tests/services/approvals/test_handlers.py b/server/tests/services/approvals/test_handlers.py
new file mode 100644
index 00000000..3f224807
--- /dev/null
+++ b/server/tests/services/approvals/test_handlers.py
@@ -0,0 +1,104 @@
+"""The owner's view of drafts: ``list_approvals`` (by status, the open ones,
+the recent ones, per workflow, run and kind, with the pending counts) and
+``get_approvals`` (by id). Each shows the owner's own drafts only."""
+
+from __future__ import annotations
+
+from datetime import datetime, timedelta, timezone
+from types import SimpleNamespace
+
+import pytest
+from sqlalchemy import update
+
+from models.approvals import ApprovalRequest
+from services.approvals import reconcile, store
+from services.approvals.handlers import MAX_LIST, handle_get_approvals, handle_list_approvals
+
+pytestmark = pytest.mark.asyncio
+
+OWNER = SimpleNamespace(scope={"path": "/ws/status"}, state=SimpleNamespace(user_id="owner"))
+STRANGER = SimpleNamespace(scope={"path": "/ws/status"}, state=SimpleNamespace(user_id="someone-else"))
+START = datetime(2026, 10, 4, 9, 0, tzinfo=timezone.utc)
+
+
+async def _draft(database, key, minute, **fields):
+ """A draft created ``minute`` minutes after START (so newest-first is
+ certain)."""
+ values = {"owner_id": "owner", "workflow_id": "7", "node_id": "7:node:1", "draft_text": key, **fields}
+ row, _ = await store.get_or_create(database, idempotency_key=key, fields=values)
+ async with database.get_session() as session:
+ await session.execute(update(ApprovalRequest).where(ApprovalRequest.id == row.id).values(created_at=START + timedelta(minutes=minute)))
+ await session.commit()
+ return row.id
+
+
+async def drafts(harness, monkeypatch):
+ # The once-per-process reconcile is not what these tests are about.
+ monkeypatch.setattr(reconcile, "_done", True)
+ database = harness.database
+ return SimpleNamespace(
+ gate_waiting=await _draft(database, "gate-waiting", 0),
+ call_waiting=await _draft(database, "call-waiting", 1, kind="tool_call", run_id="r1"),
+ call_sent=await _draft(database, "call-sent", 2, kind="tool_call", run_id="r1", status="sent"),
+ call_failed=await _draft(database, "call-failed", 3, kind="tool_call", run_id="r2", workflow_id="8", status="failed"),
+ # Discarded with no Restore window left: no longer the owner's to act on.
+ gate_discarded=await _draft(database, "gate-discarded", 4, workflow_id="8", status="discarded"),
+ foreign=await _draft(database, "foreign", 5, owner_id="someone-else", workflow_id="9"),
+ )
+
+
+async def listed(socket=OWNER, **data):
+ reply = await handle_list_approvals(data, socket)
+ assert reply["success"] is True and reply["server_time"]
+ return [item["approval_id"] for item in reply["approvals"]], reply["counts"]
+
+
+async def test_the_waiting_drafts_by_default_with_their_counts(drafts):
+ ids, counts = await listed()
+ assert ids == [drafts.call_waiting, drafts.gate_waiting]
+ assert counts == {"7": 2}
+
+
+async def test_the_open_drafts(drafts):
+ ids, counts = await listed(status="open")
+ assert ids == [drafts.call_failed, drafts.call_waiting, drafts.gate_waiting]
+ # Only waiting drafts are counted.
+ assert counts == {"7": 2}
+ assert (await listed(status="open", run_id="r1"))[0] == [drafts.call_waiting]
+ assert (await listed(status="open", kind="tool_call"))[0] == [drafts.call_failed, drafts.call_waiting]
+
+
+async def test_the_recent_drafts_newest_first(drafts):
+ everything, counts = await listed(status="recent")
+ assert everything == [drafts.gate_discarded, drafts.call_failed, drafts.call_sent, drafts.call_waiting, drafts.gate_waiting]
+ assert counts == {"7": 2}
+ assert (await listed(status="recent", limit=2))[0] == [drafts.gate_discarded, drafts.call_failed]
+
+
+async def test_by_status_workflow_run_and_kind(drafts):
+ assert (await listed(status="sent"))[0] == [drafts.call_sent]
+ assert (await listed(status="recent", workflow_id="8"))[0] == [drafts.gate_discarded, drafts.call_failed]
+ assert (await listed(status="recent", run_id="r1"))[0] == [drafts.call_sent, drafts.call_waiting]
+ assert (await listed(kind="tool_call"))[0] == [drafts.call_waiting]
+ assert (await listed(status="recent", kind="gate"))[0] == [drafts.gate_discarded, drafts.gate_waiting]
+ # A status or kind it does not know reads as the default.
+ assert (await listed(status="nonsense", kind="nonsense"))[0] == [drafts.call_waiting, drafts.gate_waiting]
+
+
+async def test_another_owner_sees_none_of_them(drafts):
+ assert await listed(STRANGER, status="recent") == ([drafts.foreign], {"9": 1})
+ assert await listed(STRANGER, status="recent", workflow_id="7") == ([], {})
+
+
+async def test_get_approvals_returns_only_the_owners_own(drafts):
+ reply = await handle_get_approvals({"approval_ids": [drafts.gate_waiting, drafts.foreign, "missing"]}, OWNER)
+ assert reply["success"] is True and reply["server_time"]
+ assert [item["approval_id"] for item in reply["approvals"]] == [drafts.gate_waiting]
+ assert (await handle_get_approvals({"approval_ids": [drafts.gate_waiting]}, STRANGER))["approvals"] == []
+
+
[email protected]("ids", [None, "gate-waiting", [], ["x"] * (MAX_LIST + 1)])
+async def test_get_approvals_refuses_a_malformed_request(harness, ids):
+ data = {} if ids is None else {"approval_ids": ids}
+ assert await handle_get_approvals(data, OWNER) == {"success": False, "error": "invalid_request"}
diff --git a/server/tests/services/genui/test_spec.py b/server/tests/services/genui/test_spec.py
index ef696cf0..909c7864 100644
--- a/server/tests/services/genui/test_spec.py
+++ b/server/tests/services/genui/test_spec.py
@@ -108,6 +108,24 @@ def test_limits_are_refused():
assert any("bytes" in problem for problem in refused(big))
+def test_a_ui_with_too_many_elements_is_refused():
+ limits = load_catalog()["limits"]
+ # Two groups of texts under the root: no element holds more children than
+ # it may, but together they are one more element than a UI may have.
+ per_group = -(-(limits["max_elements"] - 2) // 2)
+ assert per_group <= limits["max_children"]
+ raw = {"root": "root", "elements": {"root": {"type": "Stack", "children": ["a", "b"]}}}
+ for group in ("a", "b"):
+ texts = [f"{group}{n}" for n in range(per_group)]
+ raw["elements"][group] = {"type": "Stack", "children": texts}
+ raw["elements"].update({text: {"type": "Text", "props": {"text": "x"}} for text in texts})
+ assert len(raw["elements"]) == limits["max_elements"] + 1
+ assert refused(raw) == [f"the UI has {limits['max_elements'] + 1} elements; at most {limits['max_elements']}"]
+ # At the limit it passes.
+ del raw["elements"][raw["elements"]["a"]["children"].pop()]
+ assert len(check_spec(raw).spec["elements"]) == limits["max_elements"]
+
+
def test_prototype_paths_and_reserved_names_are_refused():
raw = spec(f={"type": "TextField", "props": {"label": "Name", "value": {"$bindState": "/__proto__/x"}}})
assert any("not a usable state path" in problem for problem in refused(raw))
diff --git a/server/tests/temporal/test_agent_workflow_replay.py b/server/tests/temporal/test_agent_workflow_replay.py
index 4e19b215..15a351b5 100644
--- a/server/tests/temporal/test_agent_workflow_replay.py
+++ b/server/tests/temporal/test_agent_workflow_replay.py
@@ -5,6 +5,10 @@ and feeds the recorded event history through ``Replayer``. Provider calls
remain fully stubbed activities, so the gate needs no credentials or
network API access.
+The prepared payload is a chat run's agent: it names the run, where its text
+streams, and an image the owner attached, so the replay covers the opening
+user message's image block.
+
There is exactly one message standard: the unversioned wire shape from
``services.llm.protocol.message_to_wire``. Histories recorded before the
single-standard cleanup are deliberately non-replayable (dev decision —
@@ -34,6 +38,22 @@ from services.temporal.agent_workflow import AgentWorkflow
TASK_QUEUE = "agent-native-replay-gate"
+CHAT_STREAM = {
+ "run_id": "r_replay",
+ "session_id": "graph-replay",
+ "workflow_id": "graph-replay",
+ "reply_message_id": "a_r_replay",
+}
+#: An image the owner attached to the message the run answers (a FileRef).
+ATTACHED_IMAGE = {
+ "kind": "image",
+ "path": "uploads/receipt.png",
+ "workflow_id": "graph-replay",
+ "filename": "receipt.png",
+ "mime_type": "image/png",
+ "size_bytes": 2048,
+}
+
def _prepared_payload() -> dict[str, Any]:
return {
@@ -54,6 +74,9 @@ def _prepared_payload() -> dict[str, Any]:
"max_iterations": 1,
"thinking_config": None,
"compaction_threshold": None,
+ "chat_run_id": "r_replay",
+ "chat_stream": dict(CHAT_STREAM),
+ "user_images": [dict(ATTACHED_IMAGE)],
}
@@ -160,6 +183,14 @@ async def _run_replay_gate() -> None:
assert "message_wire_version" not in llm_input
assert "api_key" not in llm_input
assert "tool_data" not in llm_input
+ # The step knows the run (Stop) and where its text streams.
+ assert llm_input["chat_run_id"] == "r_replay"
+ assert llm_input["chat_stream"] == CHAT_STREAM
+ # The owner's message carries the attached image as a ref, never bytes.
+ [opening] = [message for message in llm_input["messages"] if message["role"] == "user"]
+ assert opening["content"] == "return done"
+ [image] = [block for block in opening["blocks"] if block["type"] == "image"]
+ assert image["source"] == {"kind": "file_ref", "ref": ATTACHED_IMAGE, "detail": "auto"}
completed = next(
event.activity_task_completed_event_attributes
diff --git a/server/tests/temporal/test_approved_tool_call_workflow.py b/server/tests/temporal/test_approved_tool_call_workflow.py
index b7bb8ce0..9a10a9df 100644
--- a/server/tests/temporal/test_approved_tool_call_workflow.py
+++ b/server/tests/temporal/test_approved_tool_call_workflow.py
@@ -11,7 +11,7 @@ from __future__ import annotations
import asyncio
import subprocess
import sys
-from datetime import datetime, timedelta, timezone
+from datetime import timedelta, timezone
from pathlib import Path
from typing import Any, Dict, List
from uuid import uuid4
@@ -99,6 +99,20 @@ async def _scenarios() -> None:
assert len(calls["send"]) == 1 and calls["record"][-1]["broke_off"] is True
+def test_every_worker_registers_the_workflow_and_its_activities() -> None:
+ """Every Worker construction carries the send workflow and both its
+ activities (the scenarios above pin the names the workflow schedules)."""
+ from services.approvals.activities import APPROVAL_ACTIVITIES
+ from services.temporal import worker
+ from services.temporal.approved_tool_call_workflow import ApprovedToolCallWorkflow
+
+ assert ApprovedToolCallWorkflow in worker._framework_workflows()
+ source = (Path(__file__).parents[2] / "services" / "temporal" / "worker.py").read_text(encoding="utf-8")
+ assert source.count("*APPROVAL_ACTIVITIES") == 3
+ names = sorted(getattr(fn, "__temporal_activity_definition").name for fn in APPROVAL_ACTIVITIES)
+ assert names == ["approvals.claim_send", "approvals.record_outcome"]
+
+
def test_the_send_workflow_on_temporal() -> None:
completed = subprocess.run(
[sys.executable, "-m", "tests.temporal.test_approved_tool_call_workflow"],