Files
aigc-home/calliope-backend/tests/test_agent_interaction.py
T
Benjiyaya a11317ae0a feat(harness): canvas/skills/memory/interaction plugins + guard hardening
Self-registering harness plugins for the merged Canvas surface: canvas tools
(summarize/create/connect/link/update/run/post/delete, get-or-create sandbox
canvas), SKILL.md skills library with / composer picker, durable agent memory,
and ask_user question cards with structured HITL approvals. Guards emit typed
reason codes; stop cancels session jobs via ComfyUI interrupt; contract
vocabulary gate (contracts.json + export script) keeps web/backend names in sync.
2026-09-06 05:15:00 +08:00

174 lines
6.5 KiB
Python

"""Tests for the interaction plugin (ask_user) + structured approvals."""
from __future__ import annotations
import pytest
from calliope.agent.harness import log as session_log
from calliope.agent.harness import policy
from calliope.agent.harness.plugins import interaction
from calliope.agent.harness.registry import ToolContext
from calliope.agent.harness.tools import execute_tool
@pytest.fixture
def session(client):
resp = client.post("/api/agent/sessions", json={})
assert resp.status_code in (200, 201), resp.text
return resp.json()
def _ctx(session, project_id=None):
return ToolContext(session_id=session["id"], project_id=project_id)
def test_ask_user_appends_question_event(session):
result = execute_tool(
_ctx(session),
"ask_user",
{"question": "Generate images for all 3 characters?", "options": ["Yes, generate", "No, not yet"], "scope": "render"},
)
import asyncio
result = asyncio.run(result) if hasattr(result, "__await__") else result
assert result["ok"] is True
assert result["awaiting_user_input"] is True
events = session_log.read_events(session["id"])
asked = [e for e in events if e.type == session_log.QUESTION_ASKED]
assert len(asked) == 1
assert asked[0].data["scope"] == "render"
assert asked[0].data["options"] == ["Yes, generate", "No, not yet"]
def test_ask_user_validates_input(session):
import asyncio
ctx = _ctx(session)
too_few = asyncio.run(execute_tool(ctx, "ask_user", {"question": "q", "options": ["yes"]}))
assert too_few["ok"] is False
empty = asyncio.run(execute_tool(ctx, "ask_user", {"question": "", "options": ["a", "b"]}))
assert empty["ok"] is False
bad_scope = asyncio.run(
execute_tool(ctx, "ask_user", {"question": "q", "options": ["a", "b"], "scope": "bogus"})
)
assert bad_scope["ok"] is False
def test_structured_approval_grants_render(session):
import asyncio
ctx = _ctx(session)
asked = asyncio.run(
execute_tool(ctx, "ask_user", {"question": "Render?", "options": ["Yes", "No"], "scope": "render"})
)
session_log.append_event(
session["id"],
session_log.QUESTION_ANSWERED,
{"question_seq": asked["question_seq"], "answer": "Yes", "scope": "render"},
)
session_log.append_event(session["id"], session_log.USER_MESSAGE, {"content": "Yes"})
assert policy.has_structured_approval(ctx, "render") is True
assert policy.user_allows_render(ctx) is True
# scope isolation: a render approval is not a destructive approval
assert policy.has_structured_approval(ctx, "destructive_replace") is False
def test_structured_approval_negative_answer_grants_nothing(session):
import asyncio
ctx = _ctx(session)
asked = asyncio.run(
execute_tool(ctx, "ask_user", {"question": "Render?", "options": ["Yes", "No"], "scope": "render"})
)
session_log.append_event(
session["id"],
session_log.QUESTION_ANSWERED,
{"question_seq": asked["question_seq"], "answer": "No", "scope": "render"},
)
session_log.append_event(session["id"], session_log.USER_MESSAGE, {"content": "No"})
assert policy.has_structured_approval(ctx, "render") is False
assert policy.user_allows_render(ctx) is False
def test_scope_info_never_grants(session):
import asyncio
ctx = _ctx(session)
asked = asyncio.run(
execute_tool(ctx, "ask_user", {"question": "Continue?", "options": ["Yes", "No"], "scope": "info"})
)
session_log.append_event(
session["id"],
session_log.QUESTION_ANSWERED,
{"question_seq": asked["question_seq"], "answer": "Yes", "scope": "info"},
)
session_log.append_event(session["id"], session_log.USER_MESSAGE, {"content": "Yes"})
assert policy.has_structured_approval(ctx, "info") is False
assert policy.has_structured_approval(ctx, "render") is False
def test_open_question_is_visible_for_card_ui(session):
import asyncio
ctx = _ctx(session)
asked = asyncio.run(
execute_tool(ctx, "ask_user", {"question": "Which style?", "options": ["Painterly", "Anime"]})
)
open_q = interaction.latest_open_question(session["id"])
assert open_q is not None
assert open_q["seq"] == asked["question_seq"]
assert open_q["options"] == ["Painterly", "Anime"]
session_log.append_event(
session["id"],
session_log.QUESTION_ANSWERED,
{"question_seq": asked["question_seq"], "answer": "Anime", "scope": "info"},
)
assert interaction.latest_open_question(session["id"]) is None
def test_denied_tool_result_carries_reason_code(client, session, monkeypatch):
"""Task 3: guard refusals expose a stable machine token."""
import asyncio
from calliope.agent.harness import get_registry
project = client.post("/api/projects", json={"title": "Guard Codes"}).json()
ctx = ToolContext(session_id=session["id"], project_id=project["id"])
# A non-empty project + replace on a destructive tool → destructive guard
registry = get_registry()
tool = registry.get("generate_story")
assert tool is not None and tool.destructive
client.post(f"/api/projects/{project['id']}/characters", json={"name": "Someone"})
result = asyncio.run(
registry.execute(ctx, "generate_story", {"replace": True})
)
assert result["ok"] is False
assert result.get("reason_code") == "guard_destructive_replace"
def test_answer_to_message_records_structured_approval(client, session):
"""The runner records question/answered before the user/message echo, so
policy.latest_answer's adjacency rule sees a valid card answer."""
import asyncio
from calliope.agent.harness import policy
ctx = ToolContext(session_id=session["id"], project_id=None)
asked = asyncio.run(
execute_tool(ctx, "ask_user", {"question": "Render now?", "options": ["Yes", "No"], "scope": "render"})
)
# MessageCreate.answer_to flows through the API the way the card click does.
# The turn starts a real run, which needs an LLM — the message is persisted
# regardless; the approval event is what we assert on.
resp = client.post(
f"/api/agent/sessions/{session['id']}/messages",
json={"content": "Yes", "answer_to": asked["question_seq"]},
)
# The run may fail fast without an LLM configured; the API contract only
# requires the message to be accepted (200) — the answer event must exist.
assert resp.status_code == 200, resp.text
assert policy.has_structured_approval(ctx, "render") is True
assert policy.user_allows_render(ctx) is True