edbeeching HF Staff
Port 270 AgentEscapeBench tasks to Harbor with OpenEnv integration (part 13)
1049942 verified Download tests/test_semantics.py from HuggingFaceSwarms/AgentEscapeBench: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/AgentEscapeBench/resolve/refs%2Fpr%2F3/tests/test_semantics.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/AgentEscapeBench@refs/pr/3/tests/test_semantics.py
-
curl -L -o test_semantics.py https://huggingface.co/datasets/HuggingFaceSwarms/AgentEscapeBench/resolve/refs%2Fpr%2F3/tests/test_semantics.py
3.94 kB
| 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) | |
| 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 | |