File size: 6,522 Bytes
9a5e3ef | 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import importlib.util
import json
import unittest
from pathlib import Path
SCRIPT = Path(__file__).resolve().parents[1] / "eval" / "qwen_native_closed_loop" / "run_closed_loop.py"
SPEC = importlib.util.spec_from_file_location("run_closed_loop", SCRIPT)
MODULE = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(MODULE)
class ScriptedChat:
def __init__(self):
self.turn = 0
def __call__(self, messages, tools):
self.turn += 1
self.assert_tool_schema(tools)
if self.turn == 1:
return {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "call-1",
"type": "function",
"function": {
"name": "read_release_manifest",
"arguments": json.dumps({
"release_id": "aug3",
"component": "qwen3-coder-next",
}),
},
}],
}
if self.turn == 2:
manifest = json.loads(messages[-1]["content"])
return {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "call-2",
"type": "function",
"function": {
"name": "run_check_plan",
"arguments": json.dumps({
"component": manifest["component"],
"checks": manifest["required_checks"],
"run_nonce": manifest["run_nonce"],
}),
},
}],
}
receipt = json.loads(messages[-1]["content"])["receipt"]
return {"role": "assistant", "content": f"LAUNCH_PREFLIGHT_PASS:{receipt}"}
@staticmethod
def assert_tool_schema(tools):
assert [tool["function"]["name"] for tool in tools] == [
"read_release_manifest",
"run_check_plan",
]
class SingleToolChat:
def __init__(self):
self.turn = 0
def __call__(self, messages, tools):
self.turn += 1
if self.turn == 1:
return {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "single-1",
"type": "function",
"function": {
"name": "read_release_manifest",
"arguments": json.dumps({
"release_id": "aug3",
"component": "qwen3-coder-next",
}),
},
}],
}
nonce = json.loads(messages[-1]["content"])["run_nonce"]
return {"role": "assistant", "content": f"MANIFEST_NONCE:{nonce}"}
class RecoveryChat:
def __init__(self):
self.turn = 0
def __call__(self, messages, tools):
self.turn += 1
if self.turn == 1:
return {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "recovery-1",
"type": "function",
"function": {
"name": "read_release_manifest",
"arguments": json.dumps({
"release_id": "missing",
"component": "qwen3-coder-next",
}),
},
}],
}
if self.turn == 2:
error = json.loads(messages[-1]["content"])
assert error["error"] == "manifest_not_found"
return {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "recovery-2",
"type": "function",
"function": {
"name": "read_release_manifest",
"arguments": json.dumps({
"release_id": "aug3",
"component": "qwen3-coder-next",
}),
},
}],
}
nonce = json.loads(messages[-1]["content"])["run_nonce"]
return {"role": "assistant", "content": f"RECOVERED:{nonce}"}
class ClosedLoopTests(unittest.TestCase):
def test_single_tool_dynamic_result_passes(self):
result = MODULE.run_single_tool_roundtrip(SingleToolChat(), nonce="fixed-test-nonce")
self.assertTrue(result["passed"], result)
self.assertEqual(result["final_answer"], "MANIFEST_NONCE:fixed-test-nonce")
def test_two_tool_roundtrip_passes(self):
result = MODULE.run_closed_loop(ScriptedChat(), nonce="fixed-test-nonce")
self.assertTrue(result["passed"], result)
self.assertEqual([event["tool"] for event in result["tool_executions"]], [
"read_release_manifest",
"run_check_plan",
])
self.assertTrue(result["final_answer"].startswith("LAUNCH_PREFLIGHT_PASS:"))
def test_tool_error_recovery_passes(self):
result = MODULE.run_tool_error_recovery(RecoveryChat(), nonce="fixed-test-nonce")
self.assertTrue(result["passed"], result)
self.assertEqual(len(result["tool_executions"]), 2)
self.assertEqual(result["tool_executions"][0]["result"]["error"], "manifest_not_found")
self.assertEqual(result["final_answer"], "RECOVERED:fixed-test-nonce")
def test_wrong_first_arguments_fail(self):
def wrong_chat(messages, tools):
return {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "bad-1",
"type": "function",
"function": {
"name": "read_release_manifest",
"arguments": json.dumps({
"release_id": "aug4",
"component": "qwen3-coder-next",
}),
},
}],
}
result = MODULE.run_closed_loop(wrong_chat, nonce="fixed-test-nonce")
self.assertFalse(result["passed"])
self.assertIn("args mismatch", result["error"])
if __name__ == "__main__":
unittest.main()
|