|
| 1 | +"""Generated solutions must be verified by peers and refined by their author.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from types import SimpleNamespace |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from optillm.mars.agent import MARSAgent |
| 10 | +from optillm.mars.verifier import MARSVerifier |
| 11 | +from optillm.mars.workspace import MARSWorkspace |
| 12 | + |
| 13 | + |
| 14 | +def response(text, reasoning_tokens=7): |
| 15 | + return SimpleNamespace( |
| 16 | + choices=[SimpleNamespace(message=SimpleNamespace(content=text))], |
| 17 | + usage=SimpleNamespace(total_tokens=20, reasoning_tokens=reasoning_tokens), |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("parallel", [False, True]) |
| 22 | +@pytest.mark.parametrize("author_id,verifier_id", [(0, 2), (1, 2), (2, 0)]) |
| 23 | +def test_generated_solution_is_verified_and_improved_by_the_right_agents( |
| 24 | + parallel, author_id, verifier_id |
| 25 | +): |
| 26 | + config = {"max_tokens": 100, "max_verification_attempts": 1} |
| 27 | + workspace = MARSWorkspace("Solve 2x + 5 = 15.", config) |
| 28 | + feedback = "INCORRECT: the subtraction is wrong. Subtract 5 before dividing." |
| 29 | + agents = [MARSAgent(i, Mock(), "test-model", config) for i in range(3)] |
| 30 | + for agent in agents: |
| 31 | + agent.client.chat.completions.create.return_value = response(feedback) |
| 32 | + |
| 33 | + author = agents[author_id] |
| 34 | + author.client.chat.completions.create.side_effect = [ |
| 35 | + response("The answer is 10."), |
| 36 | + response("The answer is 5.", reasoning_tokens=11), |
| 37 | + ] |
| 38 | + solution, _ = author.generate_solution(workspace.problem) |
| 39 | + assert solution.agent_id == str(author_id) |
| 40 | + workspace.add_solution(solution) |
| 41 | + verifier = MARSVerifier(agents, workspace, config) |
| 42 | + |
| 43 | + if parallel: |
| 44 | + verification = asyncio.run(verifier.verify_solutions_parallel()) |
| 45 | + else: |
| 46 | + verification = verifier.verify_solutions() |
| 47 | + |
| 48 | + detail = verification["verification_details"][0] |
| 49 | + assert detail["verification_results"][0]["verifier_id"] == verifier_id |
| 50 | + assert solution.verification_results[-1]["assessment"] == "INCORRECT" |
| 51 | + |
| 52 | + if parallel: |
| 53 | + improvement = asyncio.run(verifier.iterative_improvement_parallel()) |
| 54 | + else: |
| 55 | + improvement = verifier.iterative_improvement() |
| 56 | + |
| 57 | + assert improvement == { |
| 58 | + "solutions_improved": 1, |
| 59 | + "improvement_attempts": 1, |
| 60 | + "total_reasoning_tokens": 11, |
| 61 | + } |
| 62 | + assert solution.solution == "The answer is 5." |
| 63 | + assert solution.reasoning_tokens == 18 |
| 64 | + assert author.client.chat.completions.create.call_count == 2 |
| 65 | + prompt = author.client.chat.completions.create.call_args.kwargs["messages"][1]["content"] |
| 66 | + assert "The answer is 10." in prompt |
| 67 | + assert feedback in prompt |
| 68 | + for agent in agents: |
| 69 | + if agent.agent_id not in (author_id, verifier_id): |
| 70 | + agent.client.chat.completions.create.assert_not_called() |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("author_id,verifier_id", [(0, 2), (1, 2), (2, 0)]) |
| 74 | +def test_integer_ids_still_choose_a_peer_with_a_different_temperature(author_id, verifier_id): |
| 75 | + agents = [MARSAgent(i, Mock(), "test-model", {}) for i in range(3)] |
| 76 | + verifier = MARSVerifier(agents, MARSWorkspace("problem", {}), {}) |
| 77 | + |
| 78 | + assert verifier._select_verifier_agent(author_id) is agents[verifier_id] |
| 79 | + |
| 80 | + |
| 81 | +def test_aggregated_and_single_agent_solutions_keep_the_fallback(): |
| 82 | + agent = MARSAgent(0, Mock(), "test-model", {}) |
| 83 | + workspace = MARSWorkspace("problem", {}) |
| 84 | + verifier = MARSVerifier([agent], workspace, {}) |
| 85 | + |
| 86 | + assert verifier._select_verifier_agent("0") is agent |
| 87 | + assert verifier._select_verifier_agent("agg_123456") is agent |
| 88 | + assert MARSVerifier([], workspace, {})._select_verifier_agent("0") is None |
0 commit comments