138 lines
5.6 KiB
Python
138 lines
5.6 KiB
Python
"""The collab plane is gated by a per-run grant, never by the hub key."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from tests.conftest import BASE, mock_agent
|
|
|
|
|
|
def team_and_run(client, name: str = "desk") -> tuple[dict, dict]:
|
|
mock_agent(client, "scout", reply="facts")
|
|
mock_agent(client, "writer", reply="article")
|
|
team = client.post(
|
|
f"{BASE}/teams",
|
|
json={
|
|
"name": name,
|
|
"playbook": "sequential",
|
|
"members": [{"agent_id": "scout", "role": "researcher"}, {"agent_id": "writer", "role": "scribe"}],
|
|
},
|
|
).json()
|
|
run = client.post(f"{BASE}/teams/{name}/runs", json={"objective": "go", "wait": True}).json()
|
|
return team, run
|
|
|
|
|
|
def sign(client, team: dict, run: dict, *, ttl: float = 600.0) -> str:
|
|
from agenthub import security
|
|
from agenthub.config import get_settings
|
|
|
|
return security.grant_sign(
|
|
get_settings().secret_key,
|
|
{"run_id": run["id"], "team_id": team["id"], "exp": time.time() + ttl},
|
|
)
|
|
|
|
|
|
GRANT_HEADERS = {"content-type": "application/json"}
|
|
|
|
|
|
def test_grant_reads_and_writes_the_shared_board(hub):
|
|
team, run = team_and_run(hub)
|
|
grant = sign(hub, team, run)
|
|
board = hub.get(f"{BASE}/collab/blackboard?grant={grant}").json()
|
|
assert any(entry["key"] == "step:1:output" for entry in board)
|
|
|
|
written = hub.post(
|
|
f"{BASE}/collab/blackboard",
|
|
json={"key": "build.log", "value": "2 tests failed", "agent": "scout"},
|
|
headers={"authorization": f"Bearer {grant}", **GRANT_HEADERS},
|
|
)
|
|
assert written.status_code == 201, written.text
|
|
entry = written.json()
|
|
assert entry["value"]["text"] == "2 tests failed"
|
|
scout = hub.get(f"{BASE}/agents/scout").json()
|
|
assert entry["agent_id"] == scout["id"]
|
|
assert any(item["key"] == "build.log" for item in hub.get(f"{BASE}/collab/blackboard?grant={grant}").json())
|
|
|
|
|
|
def test_grants_have_no_padding_to_lose(hub):
|
|
"""An agent that pulls the grant out of its prompt with a token regex drops the trailing '=' first."""
|
|
team, run = team_and_run(hub)
|
|
grant = sign(hub, team, run)
|
|
assert "=" not in grant, grant
|
|
board = hub.get(f"{BASE}/collab/blackboard", headers={"authorization": f"Bearer {grant}"})
|
|
assert board.status_code == 200, board.text
|
|
|
|
|
|
def test_grants_issued_with_padding_still_verify():
|
|
"""Grants handed out before the padding-free change must not stop working."""
|
|
import base64, hashlib, hmac, json
|
|
|
|
|
|
from agenthub import security
|
|
|
|
|
|
claims = {"run_id": "run_x", "team_id": "tm_y", "exp": time.time() + 600}
|
|
body = base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode()).decode()
|
|
assert body.endswith("=") or True
|
|
sig = hmac.new(b"s", body.encode(), hashlib.sha256).hexdigest()[:43]
|
|
assert security.grant_verify("s", "gnt_" + body + "." + sig)["run_id"] == "run_x"
|
|
|
|
|
|
|
|
def test_grant_sends_messages_to_teammates(hub):
|
|
team, run = team_and_run(hub)
|
|
grant = sign(hub, team, run)
|
|
headers = {"authorization": f"Bearer {grant}", **GRANT_HEADERS}
|
|
# recipient may be an agent name, a team role or an @-mention
|
|
for target in ("writer", "scribe", "@writer"):
|
|
created = hub.post(
|
|
f"{BASE}/collab/messages",
|
|
json={"to": target, "kind": "inform", "body": f"ping {target}", "from_agent": "scout"},
|
|
headers=headers,
|
|
)
|
|
assert created.status_code == 201, created.text
|
|
writer_id = hub.get(f"{BASE}/agents/writer").json()["id"]
|
|
# there is no message-read endpoint on the agent plane; the console reads /runs/{id}
|
|
listed = hub.get(f"{BASE}/runs/{run['id']}").json()["messages"]
|
|
assert len(listed) == 3 and {item["to_agent_id"] for item in listed} == {writer_id}
|
|
|
|
assert hub.post(f"{BASE}/collab/messages", json={"to": "ghost", "body": "x"}, headers=headers).status_code == 404
|
|
assert (
|
|
hub.post(f"{BASE}/collab/messages", json={"to": "writer", "kind": "shout", "body": "x"}, headers=headers).status_code
|
|
== 422
|
|
)
|
|
|
|
|
|
def test_grant_context_is_all_an_agent_needs(hub):
|
|
team, run = team_and_run(hub, name="desk2")
|
|
grant = sign(hub, team, run)
|
|
context = hub.get(f"{BASE}/collab/context?grant={grant}").json()
|
|
assert context["run"]["id"] == run["id"]
|
|
assert context["team"]["name"] == "desk2"
|
|
assert len(context["team"]["members"]) == 2
|
|
assert context["blackboard"] and "handoff" in context["how_to_hand_off"]
|
|
assert context["hub"] == "http://127.0.0.1:8100"
|
|
|
|
|
|
def test_grant_is_rejected_when_missing_bogus_or_expired(hub):
|
|
team, run = team_and_run(hub, name="desk3")
|
|
assert hub.get(f"{BASE}/collab/blackboard").status_code == 401
|
|
assert hub.get(f"{BASE}/collab/blackboard?grant=nonsense").status_code == 403
|
|
expired = sign(hub, team, run, ttl=-30)
|
|
assert hub.get(f"{BASE}/collab/blackboard?grant={expired}").status_code == 403
|
|
# a hub key is not a run grant: the planes stay separate
|
|
assert hub.get(f"{BASE}/collab/blackboard?grant=ah_not_a_grant").status_code == 403
|
|
# and a grant signed for another team cannot be reused here
|
|
other = hub.post(f"{BASE}/teams", json={"name": "other", "members": [{"agent_id": "scout"}]}).json()
|
|
foreign = sign(hub, other, run)
|
|
assert hub.get(f"{BASE}/collab/context?grant={foreign}").json()["team"]["id"] == other["id"]
|
|
|
|
|
|
def test_run_carries_the_grant_the_briefing_advertises(hub):
|
|
team, run = team_and_run(hub, name="desk4")
|
|
detail = hub.get(f"{BASE}/runs/{run['id']}").json()
|
|
# the console only ever sees a masked preview of a live secret
|
|
assert "•" in detail["grant_preview"]
|
|
assert detail["grant_preview"] != run.get("grant", "")
|
|
assert team["playbook"] == "sequential"
|