agenthub/backend/tests/test_collab_briefing.py

131 lines
5.0 KiB
Python

"""The collaboration briefing is appended once per step, never stacked.
Providers that echo their prompt (``mock`` without a fixed reply, the connector
``command`` provider) return the briefing inside their output, and that output
becomes the next step input, so both the transcript and the append must stay
idempotent.
"""
from __future__ import annotations
from tests.conftest import BASE, add_agent
MARKER = "[AgentHub collaboration]"
def echo_agent(client, name: str, **config):
# no "reply" config -> the mock driver echoes the prompt back verbatim
return add_agent(client, name, driver="mock", config={"delay_s": 0, **config})
def run_prompts(client, run_id: str) -> list[str]:
tasks = client.get(f"{BASE}/runs/{run_id}/tasks").json()
assert tasks, "the run created no tasks"
return [task_prompt(client, task) for task in tasks]
def task_prompt(client, task) -> str:
return client.get(f"{BASE}/tasks/{task['id']}").json()["prompt"]
def test_briefing_is_not_stacked_across_sequential_steps(hub):
echo_agent(hub, "alpha")
echo_agent(hub, "beta")
hub.post(
f"{BASE}/teams",
json={
"name": "echo-team",
"playbook": "sequential",
"members": [{"agent_id": "alpha", "role": "first"}, {"agent_id": "beta", "role": "second"}],
},
)
run = hub.post(f"{BASE}/teams/echo-team/runs", json={"objective": "count to three", "wait": True}).json()
assert run["status"] == "completed", run
prompts = run_prompts(hub, run["id"])
assert len(prompts) == 2
assert all(prompt.count(MARKER) == 1 for prompt in prompts), prompts
# the second step still sees what the first one produced
assert "count to three" in prompts[1]
def test_briefing_is_not_stacked_across_handoff_hops(hub):
echo_agent(hub, "lead")
echo_agent(hub, "review")
hub.post(
f"{BASE}/teams",
json={
"name": "hop-team",
"playbook": "handoff",
"spec": {"max_hops": 2},
"members": [{"agent_id": "lead", "role": "lead"}, {"agent_id": "review", "role": "reviewer"}],
},
)
run = hub.post(f"{BASE}/teams/hop-team/runs", json={"objective": "ship it", "wait": True}).json()
assert run["status"] == "completed", run
prompts = run_prompts(hub, run["id"])
assert all(prompt.count(MARKER) == 1 for prompt in prompts), prompts
def test_briefing_tells_each_agent_which_teammate_it_is(hub):
"""A real nanobot handed its own work back to itself until max_hops ran out."""
echo_agent(hub, "alpha")
echo_agent(hub, "beta")
hub.post(
f"{BASE}/teams",
json={
"name": "mirror-team",
"playbook": "sequential",
"members": [{"agent_id": "alpha", "role": "first"}, {"agent_id": "beta", "role": "second"}],
},
)
run = hub.post(f"{BASE}/teams/mirror-team/runs", json={"objective": "who am i", "wait": True}).json()
assert run["status"] == "completed", run
steps = hub.get(f"{BASE}/runs/{run['id']}").json()["steps"]
prompts = [hub.get(f"{BASE}/tasks/{step['task_id']}").json()["prompt"] for step in steps]
assert [step["agent_name"] for step in steps] == ["alpha", "beta"], steps
assert "you: alpha (first)" in prompts[0], prompts[0]
assert "you: beta (second)" in prompts[1], prompts[1]
# the identity line is scaffolding, so it must not reach the next step as content
from agenthub.services.collab import _strip_briefing
assert "you: " not in _strip_briefing(prompts[0])
def test_strip_briefing_removes_only_the_scaffolding():
from agenthub.services.collab import _strip_briefing
assert _strip_briefing("") == ""
assert _strip_briefing("plain answer") == "plain answer"
scaffolding = MARKER + "\nhub: http://x\nhow to hand off"
assert _strip_briefing("answer text\n\n" + scaffolding) == "answer text"
def test_briefing_uses_the_hub_url_the_agent_registered(hub):
"""A containerized agent cannot reach the hub's own 127.0.0.1.
The connector reports the URL it dials us on, so the briefing has to
advertise that address instead of the one the hub listens on.
"""
echo_agent(hub, "alpha")
echo_agent(hub, "beta", hub_url="http://host.docker.internal:8100")
hub.post(
f"{BASE}/teams",
json={
"name": "docker-team",
"playbook": "sequential",
"members": [{"agent_id": "alpha", "role": "first"}, {"agent_id": "beta", "role": "second"}],
},
)
run = hub.post(f"{BASE}/teams/docker-team/runs", json={"objective": "plan it", "wait": True}).json()
assert run["status"] == "completed", run
steps = {task["agent_name"]: task for task in hub.get(f"{BASE}/runs/{run['id']}/tasks").json()}
# the dockerized teammate is told the address it can actually reach
assert "http://host.docker.internal:8100/api/v1/collab/blackboard" in task_prompt(hub, steps["beta"])
# an agent that advertised nothing keeps the hub's own address
assert "host.docker.internal" not in task_prompt(hub, steps["alpha"])