File size: 3,942 Bytes
1049942 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import json
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from agentescape_port.oracle import solve
from agentescape_port.runtime.service import Episode, create_app, public, upstream
DATA = Path(__file__).resolve().parents[1] / "data/questions"
ORDINARY = next(
p
for p in sorted((DATA / "5/narrative").glob("*.json"))
if "code_py_exec" not in p.read_text()
)
def victory(path=ORDINARY):
scenario = json.loads(path.read_text())
node = next(
n for n in scenario["nodes"] if n["id"] == scenario["metadata"]["win_node_id"]
)
return next(port["value"] for port in node["outputs"] if "value" in port)
def test_wrong_correct_sticky_and_no_reset(tmp_path):
app = create_app(ORDINARY, 10, tmp_path)
with TestClient(app) as client:
initial = client.get("/observation").json()
assert "available_entities" in initial
assert "_template_id" not in json.dumps(initial)
assert not client.post(
"/action", json={"action": "submit_answer", "answer": "WRONG"}
).json()["is_win"]
response = client.post(
"/action", json={"action": "submit_answer", "answer": victory()}
).json()
assert response["is_win"] is True
assert response["episode"]["action_count"] == 2
client.post("/action", json={"action": "submit_answer", "answer": "wrong"})
assert client.get("/state").json()["won"] is True
assert client.get("/state").json()["action_count"] == 2
assert client.post("/env/reset").status_code == 404
assert client.get("/stats").status_code == 404
assert client.get("/evidence/final.json").status_code == 404
def test_invalid_calls_consume_budget_and_state_is_independent():
first, second = Episode(ORDINARY, 2), Episode(ORDINARY, 2)
first.dispatch({"action": "investigate", "node_id": "NONEXISTENT"})
first.dispatch({"action": "reset"})
assert first.state()["termination_reason"] == "action_budget_exhausted"
assert second.state()["action_count"] == 0
assert not first.dispatch({"action": "submit_answer", "answer": victory()}).get(
"is_win"
)
assert second.dispatch({"action": "submit_answer", "answer": victory()})["is_win"]
def test_reference_observations_and_actions():
original = upstream.reset_case(case_file_path=str(ORDINARY), session_id="reference")
episode = Episode(ORDINARY, 500)
for key in (
"available_entities",
"available_information",
"win_node_id",
"narrative",
):
assert episode.initial[key] == original[key]
for entity in original["available_entities"]:
reference = upstream.investigate(
original["case_id"], upstream.InvestigateRequest(node_id=entity["id"])
)
adapted = episode.dispatch({"action": "investigate", "node_id": entity["id"]})
adapted.pop("episode")
assert adapted == public(reference)
def test_reference_solver_requires_submission():
scenario = json.loads(ORDINARY.read_text())
episode = Episode(ORDINARY)
actions = []
def act(payload):
actions.append(payload)
if payload["action"] == "submit_answer":
assert not episode.won
return episode.dispatch(payload)
result = solve(scenario, act, episode.initial)
assert result["won"]
assert actions[-1]["action"] == "submit_answer"
assert any(a["action"] == "investigate" for a in actions)
@pytest.mark.parametrize(
"payload",
[
{},
{"action": "investigate"},
{"action": "tool_call", "arguments": []},
{"action": "submit_answer", "answer": 3},
],
)
def test_malformed_actions_are_feedback_not_service_failures(payload):
episode = Episode(ORDINARY)
response = episode.dispatch(payload)
assert not response["ok"]
assert episode.count == 1
assert episode.reason is None
|