File size: 56,543 Bytes
881f9f2 | 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 | from __future__ import annotations
import asyncio
import os
import sys
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
REPO_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO_ROOT / "scripts"))
from orchestration_utils import ( # noqa: E402
available_sensor_ids,
bootstrap_aob,
build_fault_risk_adjudication_state,
build_executor,
build_parser,
build_planner_descriptions,
build_planning_question,
build_tool_catalog_for_executor,
can_retry_missing_evidence,
canonicalize_step_result,
close_executor,
compact_step_for_context,
compact_verifier_result,
SelfAskDecision,
VerificationDecision,
build_missing_evidence_repair_state,
build_llm,
build_retry_question,
build_suffix_replan_question,
current_missing_evidence_hit,
effective_server_paths,
fault_risk_adjudication_failed_step,
finalize_missing_evidence_repair_state,
generate_suffix_plan,
load_fault_risk_adjudication_config,
load_missing_evidence_repair_config,
load_plan_execute_planner,
maybe_self_ask,
normalize_plan_steps,
normalize_response_text,
parse_json_object,
preflight_aob_runtime_dependencies,
repair_sensor_task_text,
repair_target_key,
response_error_payload,
record_missing_evidence_retry_attempt,
serialize_step_result,
should_skip_invalid_sensor_step,
summarize_answer,
summarize_terminal_failures,
tool_schema_for_step,
verify_step,
)
from mitigation_guards import ( # noqa: E402
EXPLICIT_FAULT_RISK_ADJUDICATION_NAME,
MISSING_EVIDENCE_REPAIR_NAME,
_fault_risk_adjudication_applies,
apply_explicit_fault_risk_adjudication,
apply_missing_evidence_final_answer_guard,
build_explicit_fault_risk_adjudication,
env_flag_enabled,
scan_missing_evidence,
)
class DummyLLM:
def __init__(self, responses):
self._responses = list(responses)
self.prompts = []
def generate(self, prompt: str, **_kwargs) -> str:
self.prompts.append(prompt)
if not self._responses:
raise AssertionError(f"No response left for prompt: {prompt[:120]}")
return self._responses.pop(0)
class OrchestrationUtilsTests(unittest.TestCase):
def test_build_planning_question_appends_guardrails(self):
question = build_planning_question("Investigate transformer T-015.")
self.assertIn("Use only these real servers", question)
self.assertIn("get_dga_record and analyze_dga are FMSR tools", question)
def test_build_planner_descriptions_adds_repo_specific_notes(self):
descriptions = {
"fmsr": " - get_dga_record(transformer_id: string): Retrieve DGA"
}
tool_catalog = {
"fmsr": {
"get_dga_record": {
"description": "Retrieve DGA",
"schema": "transformer_id: string (transformer id like T-015)",
}
}
}
rendered = build_planner_descriptions(descriptions, tool_catalog)["fmsr"]
self.assertIn("Planner notes:", rendered)
self.assertIn("get_sensor_correlation only", rendered)
self.assertIn("Canonical tool signatures:", rendered)
def test_build_parser_reads_mcp_mode_environment_default(self):
with patch.dict(os.environ, {"MCP_MODE": "optimized"}):
parser = build_parser("runner", "Test runner.")
args = parser.parse_args(["Investigate transformer T-015."])
self.assertEqual(args.mcp_mode, "optimized")
def test_build_llm_honors_max_tokens_without_aob_usage_class(self):
captured = {}
def completion(**kwargs):
captured.update(kwargs)
return SimpleNamespace(
choices=[SimpleNamespace(message=SimpleNamespace(content="ok"))],
usage=SimpleNamespace(prompt_tokens=3, completion_tokens=2),
)
fake_litellm = SimpleNamespace(completion=completion)
with patch.dict(
os.environ,
{
"MAX_TOKENS": "17",
"LITELLM_API_KEY": "key",
"LITELLM_BASE_URL": "http://127.0.0.1:8000",
},
), patch.dict(sys.modules, {"litellm": fake_litellm}):
llm = build_llm("openai/test-model")
result = llm.generate_with_usage("prompt")
self.assertEqual(result.text, "ok")
self.assertEqual(result.input_tokens, 3)
self.assertEqual(result.output_tokens, 2)
self.assertEqual(captured["max_tokens"], 17)
def test_build_tool_catalog_for_executor_prefers_executor_cache(self):
class Executor:
def __init__(self):
self.called = False
async def get_tool_catalog(self):
self.called = True
return {"iot": {"list_assets": {"description": "", "schema": ""}}}
executor = Executor()
result = asyncio.run(
build_tool_catalog_for_executor(
executor,
{"iot": REPO_ROOT / "does_not_need_to_exist.py"},
)
)
self.assertTrue(executor.called)
self.assertIn("list_assets", result["iot"])
def test_build_executor_skips_aob_agent_package_side_effects(self):
with tempfile.TemporaryDirectory() as tmp:
aob_path = Path(tmp)
agent_path = aob_path / "src" / "agent"
plan_execute_path = agent_path / "plan_execute"
plan_execute_path.mkdir(parents=True)
(agent_path / "__init__.py").write_text(
'raise RuntimeError("agent package init should not run")\n',
encoding="utf-8",
)
(agent_path / "runner.py").write_text(
"DEFAULT_SERVER_PATHS = {'iot': 'iot.py'}\n",
encoding="utf-8",
)
(plan_execute_path / "__init__.py").write_text("", encoding="utf-8")
(plan_execute_path / "executor.py").write_text(
"from ..runner import DEFAULT_SERVER_PATHS\n\n"
"class Executor:\n"
" def __init__(self, llm, server_paths):\n"
" self.llm = llm\n"
" self.server_paths = server_paths\n"
" self.default_server_paths = DEFAULT_SERVER_PATHS\n",
encoding="utf-8",
)
old_agent_modules = {
name: module
for name, module in sys.modules.items()
if name == "agent" or name.startswith("agent.")
}
old_sys_path = list(sys.path)
for name in old_agent_modules:
sys.modules.pop(name, None)
try:
bootstrap_aob(aob_path)
executor = build_executor(
object(),
{"iot": Path("iot.py")},
mcp_mode="baseline",
)
finally:
for name in list(sys.modules):
if name == "agent" or name.startswith("agent."):
sys.modules.pop(name, None)
sys.modules.update(old_agent_modules)
sys.path[:] = old_sys_path
self.assertEqual(executor.server_paths, {"iot": Path("iot.py")})
self.assertEqual(executor.default_server_paths, {"iot": "iot.py"})
def test_load_plan_execute_planner_accepts_task_dependencies(self):
with tempfile.TemporaryDirectory() as tmp:
aob_path = Path(tmp)
agent_path = aob_path / "src" / "agent"
plan_execute_path = agent_path / "plan_execute"
plan_execute_path.mkdir(parents=True)
(agent_path / "__init__.py").write_text(
'raise RuntimeError("agent package init should not run")\n',
encoding="utf-8",
)
(plan_execute_path / "__init__.py").write_text("", encoding="utf-8")
(plan_execute_path / "planner.py").write_text(
"import re\n"
"_DEP_NUM_RE = re.compile(r'#S(\\d+)')\n\n"
"class Planner:\n"
" pass\n",
encoding="utf-8",
)
old_agent_modules = {
name: module
for name, module in sys.modules.items()
if name == "agent" or name.startswith("agent.")
}
old_sys_path = list(sys.path)
for name in old_agent_modules:
sys.modules.pop(name, None)
try:
bootstrap_aob(aob_path)
Planner = load_plan_execute_planner()
from agent.plan_execute import planner as planner_module
finally:
for name in list(sys.modules):
if name == "agent" or name.startswith("agent."):
sys.modules.pop(name, None)
sys.modules.update(old_agent_modules)
sys.path[:] = old_sys_path
self.assertEqual(Planner.__name__, "Planner")
self.assertEqual(planner_module._DEP_NUM_RE.findall("#Task1 #S2"), ["1", "2"])
def test_close_executor_awaits_optional_aclose(self):
class Executor:
def __init__(self):
self.closed = False
async def aclose(self):
self.closed = True
executor = Executor()
asyncio.run(close_executor(executor))
self.assertTrue(executor.closed)
def test_normalize_plan_steps_reroutes_unique_tool_server(self):
class Step:
def __init__(self, step_number, task, server, tool, dependencies=None):
self.step_number = step_number
self.task = task
self.server = server
self.tool = tool
self.tool_args = {}
self.dependencies = dependencies or []
self.expected_output = ""
class Plan:
def __init__(self, steps):
self.steps = steps
plan = Plan(
[
Step(1, "Fetch the latest DGA record", "iot", "get_dga_record"),
Step(2, "Analyze the gases", "iot", "analyze_dga", [1]),
]
)
tool_catalog = {
"iot": {"get_asset_metadata": {"description": "", "schema": ""}},
"fmsr": {
"get_dga_record": {"description": "", "schema": ""},
"analyze_dga": {"description": "", "schema": ""},
},
}
warnings = normalize_plan_steps(plan, tool_catalog)
self.assertEqual(plan.steps[0].server, "fmsr")
self.assertEqual(plan.steps[1].server, "fmsr")
self.assertEqual(plan.steps[1].dependencies, [1])
self.assertTrue(any("Rerouted step 1" in warning for warning in warnings))
def test_normalize_plan_steps_accepts_short_server_aliases(self):
class Step:
def __init__(self, step_number, task, server, tool, dependencies=None):
self.step_number = step_number
self.task = task
self.server = server
self.tool = tool
self.tool_args = {}
self.dependencies = dependencies or []
self.expected_output = ""
class Plan:
def __init__(self, steps):
self.steps = steps
plan = Plan([Step(1, "List transformer sensors", "i", "list_sensors")])
tool_catalog = {
"iot": {"list_sensors": {"description": "", "schema": ""}},
}
warnings = normalize_plan_steps(plan, tool_catalog)
self.assertEqual(plan.steps[0].server, "iot")
self.assertTrue(
any("Normalized server alias" in warning for warning in warnings)
)
def test_normalize_plan_steps_drops_terminal_none_step(self):
class Step:
def __init__(self, step_number, task, server, tool, dependencies=None):
self.step_number = step_number
self.task = task
self.server = server
self.tool = tool
self.tool_args = {}
self.dependencies = dependencies or []
self.expected_output = ""
class Plan:
def __init__(self, steps):
self.steps = steps
plan = Plan(
[
Step(1, "Fetch DGA record", "fmsr", "get_dga_record"),
Step(2, "Summarize findings", "none", "none", [1]),
]
)
tool_catalog = {"fmsr": {"get_dga_record": {"description": "", "schema": ""}}}
warnings = normalize_plan_steps(plan, tool_catalog)
self.assertEqual(len(plan.steps), 1)
self.assertEqual(plan.steps[0].step_number, 1)
self.assertTrue(
any(
"Dropped terminal synthesis-only step" in warning
for warning in warnings
)
)
def test_tool_schema_for_step_uses_hintful_schema(self):
tool_catalog = {
"fmsr": {
"get_sensor_correlation": {
"description": "",
"schema": "failure_mode_id: string (failure mode id like FM-006; not a transformer id)",
}
}
}
schema = tool_schema_for_step(tool_catalog, "fmsr", "get_sensor_correlation")
self.assertIn("FM-006", schema)
self.assertIn("not a transformer id", schema)
def test_normalize_response_text_wraps_newline_json_objects_into_list(self):
response = '{"sensor_id":"load_current_a"}\n{"sensor_id":"winding_temp_top_c"}'
normalized = normalize_response_text(response)
self.assertIn("[", normalized)
self.assertIn("winding_temp_top_c", normalized)
def test_available_sensor_ids_reads_list_sensors_context(self):
class Result:
tool = "list_sensors"
response = (
'[{"sensor_id":"load_current_a"},{"sensor_id":"winding_temp_top_c"}]'
)
sensors = available_sensor_ids({3: Result()})
self.assertEqual(sensors, {"load_current_a", "winding_temp_top_c"})
def test_repair_sensor_task_text_rewrites_known_alias(self):
task, warning = repair_sensor_task_text(
"Compute the trend of the winding_temp_c sensor readings.",
{"winding_temp_top_c", "oil_temp_c"},
)
self.assertIn("winding_temp_top_c", task)
self.assertIn("winding_temp_c -> winding_temp_top_c", warning)
def test_should_skip_invalid_sensor_step_for_iot_dga_lookup(self):
class Step:
tool = "get_sensor_readings"
task = "Get time-series readings for sensor dga_h2_ppm on transformer T-015"
should_skip, reason = should_skip_invalid_sensor_step(
Step(), {"load_current_a", "winding_temp_top_c"}
)
self.assertTrue(should_skip)
self.assertIn("DGA data in this repo comes from FMSR", reason)
def test_canonicalize_step_result_normalizes_response(self):
class Result:
response = (
'{"sensor_id":"load_current_a"}\n{"sensor_id":"winding_temp_top_c"}'
)
result = Result()
canonicalize_step_result(result)
self.assertIn("[", result.response)
self.assertIn("winding_temp_top_c", result.response)
def test_compact_step_for_context_truncates_large_response_and_error(self):
class Result:
response = "A" * 3000
error = "B" * 1200
result = Result()
compact_step_for_context(result)
self.assertIn("[truncated", result.response)
self.assertIn("[truncated", result.error)
self.assertLess(len(result.response), 1400)
self.assertLess(len(result.error), 800)
def test_compact_verifier_result_truncates_large_response(self):
compacted = compact_verifier_result(
{
"step": 4,
"task": "Get sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"success": True,
"response": "A" * 3000,
"error": None,
}
)
self.assertIn("[truncated", compacted["response"])
self.assertLess(len(compacted["response"]), 1400)
def test_parse_json_object_handles_markdown_fence(self):
raw = '```json\n{"decision": "continue", "reason": "looks good"}\n```'
payload = parse_json_object(raw)
self.assertEqual(payload["decision"], "continue")
def test_parse_json_object_preserves_json_substring_in_payload(self):
raw = '```json\n{"name": "json_value"}\n```'
payload = parse_json_object(raw)
self.assertEqual(payload["name"], "json_value")
def test_response_error_payload_detects_top_level_json_error(self):
self.assertEqual(
response_error_payload('{"error": "Failure mode not found."}'),
"Failure mode not found.",
)
self.assertIsNone(response_error_payload('{"result": "ok"}'))
def test_response_error_payload_detects_list_wrapped_error(self):
self.assertEqual(
response_error_payload([{"error": "No sensor data found for 'T-020'."}]),
"No sensor data found for 'T-020'.",
)
def test_response_error_payload_detects_unknown_tool_string(self):
self.assertEqual(
response_error_payload("Unknown tool: get_dga_record"),
"Unknown tool: get_dga_record",
)
def test_serialize_step_result_marks_json_error_payload_as_failure(self):
class Result:
step_number = 4
task = "Infer probable fault mode"
server = "fmsr"
tool = "get_sensor_correlation"
tool_args = {"failure_mode_id": "T-015"}
response = '{"error": "Failure mode \\"T-015\\" not found."}'
error = None
success = True
payload = serialize_step_result(Result())
self.assertFalse(payload["success"])
self.assertTrue(payload["executor_success"])
self.assertEqual(payload["error"], 'Failure mode "T-015" not found.')
def test_serialize_step_result_marks_unknown_tool_string_as_failure(self):
class Result:
step_number = 1
task = "Get DGA record"
server = "iot"
tool = "get_dga_record"
tool_args = {"transformer_id": "T-020"}
response = "Unknown tool: get_dga_record"
error = None
success = True
payload = serialize_step_result(Result())
self.assertFalse(payload["success"])
self.assertTrue(payload["executor_success"])
self.assertEqual(payload["error"], "Unknown tool: get_dga_record")
def test_serialize_step_result_carries_runner_repair_metadata(self):
class Result:
step_number = 2
task = "Inspect DGA sensor"
server = "iot"
tool = "get_sensor_readings"
tool_args = {"sensor_id": "dga_h2_ppm"}
response = "Skipped invalid IoT DGA lookup."
error = None
success = True
runner_repair = "invalid_iot_dga_sensor_lookup"
runner_repair_reason = "Use FMSR DGA tools instead."
payload = serialize_step_result(Result())
self.assertTrue(payload["success"])
self.assertEqual(payload["runner_repair"], "invalid_iot_dga_sensor_lookup")
self.assertEqual(payload["runner_repair_reason"], "Use FMSR DGA tools instead.")
def test_maybe_self_ask_defaults_to_original_question_when_false(self):
llm = DummyLLM(
[
'{"needs_self_ask": false, "clarifying_questions": [], "assumptions": [], "augmented_question": "ignored"}'
]
)
decision = maybe_self_ask("What failed at site MAIN?", llm)
self.assertEqual(
decision,
SelfAskDecision(
needs_self_ask=False,
clarifying_questions=[],
assumptions=[],
augmented_question="What failed at site MAIN?",
),
)
def test_maybe_self_ask_constructs_augmented_question_when_missing(self):
llm = DummyLLM(
[
'{"needs_self_ask": true, "clarifying_questions": ["Which transformer?"], "assumptions": ["Use the most recent incident"], "augmented_question": ""}'
]
)
decision = maybe_self_ask("Investigate the overheating alert.", llm)
self.assertTrue(decision.needs_self_ask)
self.assertIn("Which transformer?", decision.augmented_question)
self.assertIn("Use the most recent incident", decision.augmented_question)
def test_build_suffix_replan_question_includes_focus_and_remaining_steps(self):
class Step:
def __init__(self, step_number, task, server, tool):
self.step_number = step_number
self.task = task
self.server = server
self.tool = tool
history = [
{
"step": 1,
"task": "Find the transformer",
"server": "iot",
"tool": "get_asset_metadata",
"response": "Transformer is T-015.",
"error": None,
"success": True,
}
]
remaining = [Step(2, "Fetch DGA record", "fmsr", "get_dga_record")]
question = build_suffix_replan_question(
"Investigate T-015.",
"Investigate T-015.",
history,
remaining,
VerificationDecision(
decision="replan_suffix",
reason="The investigation should pivot to thermal fault confirmation.",
updated_focus="Confirm the thermal fault before creating the work order.",
),
)
self.assertIn("thermal fault", question)
self.assertIn("Fetch DGA record", question)
self.assertIn("Dependency1: None", question)
self.assertIn("never on completed steps", question)
def test_build_retry_question_includes_previous_attempt_and_reason(self):
class Step:
def __init__(self, step_number, task):
self.step_number = step_number
self.task = task
question = build_retry_question(
"Investigate T-015.",
"Investigate T-015 with internal clarifications.",
Step(2, "Fetch DGA record"),
{"response": "Returned the wrong asset record."},
VerificationDecision(
decision="retry",
reason="The tool arguments targeted the wrong asset.",
updated_focus="Use the transformer identified in step 1.",
),
retries_used=1,
)
self.assertIn("wrong asset", question)
self.assertIn("Retry attempt number: 1", question)
self.assertIn("step 1", question.lower())
def test_summarize_answer_uses_only_terminal_history_entries(self):
llm = DummyLLM(["Final answer."])
answer = summarize_answer(
"What happened?",
[
{
"step": 1,
"task": "Lookup alert",
"server": "iot",
"response": "Old retry response",
"error": None,
"success": True,
},
{
"step": 1,
"task": "Lookup alert",
"server": "iot",
"response": "Final step 1 response",
"error": None,
"success": True,
},
{
"step": 2,
"task": "Create WO",
"server": "wo",
"response": "WO created",
"error": None,
"success": True,
},
],
llm,
)
self.assertEqual(answer, "Final answer.")
self.assertIn("Final step 1 response", llm.prompts[0])
self.assertNotIn("Old retry response", llm.prompts[0])
def test_summarize_answer_falls_back_when_llm_raises(self):
class BrokenLLM:
def generate(self, _prompt: str, **_kwargs) -> str:
raise RuntimeError("context window exceeded")
with self.assertLogs("orchestration_utils", level="WARNING") as logs:
answer = summarize_answer(
"What happened?",
[
{
"step": 1,
"task": "Get readings",
"server": "iot",
"tool": "get_sensor_readings",
"response": "A" * 2000,
"error": None,
"success": True,
},
{
"step": 2,
"task": "Create WO",
"server": "wo",
"tool": "create_work_order",
"response": None,
"error": "could not create",
"success": False,
},
],
BrokenLLM(),
)
self.assertIn("Compact terminal results", answer)
self.assertIn("create_work_order", answer)
self.assertTrue(any("summarization failed" in line for line in logs.output))
def test_summarize_terminal_failures_only_keeps_terminal_failed_steps(self):
failures = summarize_terminal_failures(
[
{
"step": 1,
"task": "Lookup alert",
"server": "iot",
"tool": "get_alert",
"error": "transient failure",
"success": False,
},
{
"step": 1,
"task": "Lookup alert",
"server": "iot",
"tool": "get_alert",
"error": None,
"success": True,
},
{
"step": 2,
"task": "Create WO",
"server": "wo",
"tool": "create_work_order",
"error": "Unknown server",
"success": False,
"verifier_decision": "error",
},
]
)
self.assertEqual(
failures,
[
{
"step": 2,
"task": "Create WO",
"server": "wo",
"tool": "create_work_order",
"error": "Unknown server",
"verifier_decision": "error",
}
],
)
def test_generate_suffix_plan_returns_warning_payload_on_invalid_plan(self):
class DummyPlanner:
def generate_plan(self, _question, _descriptions):
raise ValueError("Invalid dependency reference for step 1: #S1")
with self.assertLogs("orchestration_utils", level="WARNING") as logs:
plan, error = generate_suffix_plan(
DummyPlanner(),
"Replan only the suffix.",
{"iot": "list_sensors"},
)
self.assertIsNone(plan)
self.assertEqual(error, "Invalid dependency reference for step 1: #S1")
self.assertIn(
"continuing with the original remaining plan", "\n".join(logs.output)
)
def test_verify_step_warns_and_falls_back_when_json_missing(self):
class Step:
def __init__(self):
self.step_number = 2
self.task = "Fetch DGA record"
self.server = "fmsr"
self.tool = "get_dga_record"
llm = DummyLLM(["not json at all"])
with self.assertLogs("orchestration_utils", level="WARNING") as logs:
decision = verify_step(
"Investigate T-015.",
"Investigate T-015.",
Step(),
{"response": "Some result", "success": True},
[],
[],
llm,
)
self.assertEqual(decision.decision, "continue")
self.assertTrue(any("no parseable JSON" in line for line in logs.output))
def test_verify_step_warns_and_falls_back_when_decision_unknown(self):
class Step:
def __init__(self):
self.step_number = 2
self.task = "Fetch DGA record"
self.server = "fmsr"
self.tool = "get_dga_record"
llm = DummyLLM(
[
'{"decision": "repair_everything", "reason": "nonsense", "updated_focus": ""}'
]
)
with self.assertLogs("orchestration_utils", level="WARNING") as logs:
decision = verify_step(
"Investigate T-015.",
"Investigate T-015.",
Step(),
{"response": "Some result", "success": True},
[],
[],
llm,
)
self.assertEqual(decision.decision, "continue")
self.assertTrue(any("unknown decision" in line for line in logs.output))
def test_verify_step_warns_and_falls_back_when_llm_raises(self):
class Step:
def __init__(self):
self.step_number = 4
self.task = "Get sensor readings"
self.server = "iot"
self.tool = "get_sensor_readings"
class BrokenLLM:
def generate(self, _prompt: str, **_kwargs) -> str:
raise RuntimeError("context window exceeded")
with self.assertLogs("orchestration_utils", level="WARNING") as logs:
decision = verify_step(
"Investigate T-015.",
"Investigate T-015.",
Step(),
{"response": "A" * 5000, "success": True},
[],
[],
BrokenLLM(),
)
self.assertEqual(decision.decision, "continue")
self.assertTrue(any("Verifier failed" in line for line in logs.output))
def test_effective_server_paths_exits_cleanly_on_bad_override(self):
with self.assertRaises(SystemExit) as exc:
effective_server_paths(["bad-entry"], REPO_ROOT)
self.assertIn("Invalid --server entry", str(exc.exception))
def test_preflight_aob_runtime_dependencies_reports_missing_modules(self):
def fake_import_module(name: str):
if name == "litellm":
raise ModuleNotFoundError("No module named 'litellm'")
return object()
with patch(
"orchestration_utils.importlib.import_module",
side_effect=fake_import_module,
):
with self.assertRaises(RuntimeError) as exc:
preflight_aob_runtime_dependencies()
self.assertIn("litellm", str(exc.exception))
self.assertIn("requirements-insomnia.txt", str(exc.exception))
def test_env_flag_enabled_accepts_standard_truthy_values(self):
self.assertTrue(env_flag_enabled("1"))
self.assertTrue(env_flag_enabled("true"))
self.assertFalse(env_flag_enabled("0"))
self.assertFalse(env_flag_enabled(""))
def test_missing_evidence_guard_blocks_substantive_final_answer(self):
payload = {
"answer": "Schedule immediate maintenance for transformer T-015.",
"success": True,
"failed_steps": [],
"history": [
{
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {"sensor_id": "winding_temp_top_c"},
"response": '{"readings": []}',
"error": None,
"success": True,
}
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertFalse(guarded["success"])
self.assertIn("Mitigation guard blocked", guarded["answer"])
self.assertTrue(guarded["mitigation_guard"]["triggered"])
self.assertTrue(guarded["mitigation_guard"]["blocked_final_answer"])
self.assertEqual(
guarded["failed_steps"][-1]["tool"],
"missing_evidence_final_answer_guard",
)
def test_scan_missing_evidence_returns_hit_without_mutating_payload(self):
payload = {
"answer": "Schedule immediate maintenance for transformer T-015.",
"success": True,
"history": [
{
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": '{"readings": []}',
"error": None,
"success": True,
}
],
}
scan = scan_missing_evidence(payload)
self.assertTrue(scan["triggered"])
self.assertEqual(scan["hits"][0]["step"], 1)
self.assertEqual(scan["hits"][0]["tool"], "get_sensor_readings")
self.assertEqual(
scan["hits"][0]["target"],
{"transformer_id": "T-015", "sensor_id": "winding_temp_top_c"},
)
self.assertNotIn("mitigation_guard", payload)
def test_repair_config_requires_missing_evidence_guard(self):
with patch.dict(
os.environ,
{
"ENABLE_MISSING_EVIDENCE_REPAIR": "1",
"ENABLE_MISSING_EVIDENCE_GUARD": "0",
},
):
with self.assertRaisesRegex(
RuntimeError,
"requires ENABLE_MISSING_EVIDENCE_GUARD",
):
load_missing_evidence_repair_config()
def test_repair_config_ignores_budget_env_when_disabled(self):
with patch.dict(
os.environ,
{
"ENABLE_MISSING_EVIDENCE_REPAIR": "0",
"ENABLE_MISSING_EVIDENCE_GUARD": "0",
"MISSING_EVIDENCE_REPAIR_MAX_ATTEMPTS": "0",
"MISSING_EVIDENCE_REPAIR_MAX_ATTEMPTS_PER_TARGET": "not-an-int",
},
):
config = load_missing_evidence_repair_config()
self.assertFalse(config.enabled)
self.assertEqual(config.max_attempts, 0)
self.assertEqual(config.max_attempts_per_target, 0)
def test_adjudication_config_requires_missing_evidence_guard(self):
with patch.dict(
os.environ,
{
"ENABLE_EXPLICIT_FAULT_RISK_ADJUDICATION": "1",
"ENABLE_MISSING_EVIDENCE_GUARD": "0",
},
):
with self.assertRaisesRegex(
RuntimeError,
"requires ENABLE_MISSING_EVIDENCE_GUARD",
):
load_fault_risk_adjudication_config()
def test_fault_risk_adjudication_finalizes_with_deciding_evidence(self):
payload = {
"history": [
{
"step": 1,
"task": "Analyze DGA for T-015",
"server": "fmsr",
"tool": "analyze_dga",
"tool_args": {"transformer_id": "T-015"},
"response": (
'{"diagnosis": "thermal fault", "risk_level": "high", '
'"failure_mode_id": "FM-006"}'
),
"error": None,
"success": True,
}
],
}
adjudication = build_explicit_fault_risk_adjudication(
payload,
enabled=True,
)
self.assertEqual(adjudication["name"], EXPLICIT_FAULT_RISK_ADJUDICATION_NAME)
self.assertEqual(adjudication["decision"], "finalize")
self.assertEqual(adjudication["selected_fault_id"], "FM-006")
self.assertEqual(adjudication["selected_fault_label"], "thermal fault")
self.assertEqual(adjudication["selected_risk_level"], "high")
self.assertEqual(adjudication["deciding_evidence"][0]["tool"], "analyze_dga")
def test_fault_risk_adjudication_refuses_missing_evidence(self):
payload = {
"answer": "Schedule high-priority thermal-fault maintenance.",
"success": True,
"history": [
{
"step": 1,
"task": "Fetch DGA",
"server": "fmsr",
"tool": "get_dga_record",
"tool_args": {"transformer_id": "T-015"},
"response": '{"records": []}',
"error": None,
"success": True,
}
],
}
guarded = apply_explicit_fault_risk_adjudication(payload, enabled=True)
self.assertFalse(guarded["success"])
self.assertEqual(
guarded["fault_risk_adjudication"]["decision"],
"refuse_due_missing_evidence",
)
self.assertIn("Fault/risk adjudication refused", guarded["answer"])
self.assertEqual(
guarded["failed_steps"][-1]["tool"],
EXPLICIT_FAULT_RISK_ADJUDICATION_NAME,
)
def test_fault_risk_adjudication_skips_iot_only_monitoring_task(self):
payload = {
"question": (
"Retrieve load current readings for T-005, compare them to the "
"recent baseline, and report the peak value."
),
"answer": "Peak load current was 520 A at 2024-01-07T12:00:00.",
"success": True,
"scenario": {
"type": "IoT",
"domain_tags": ["IoT"],
"text": "T-005 has elevated load current readings.",
},
"history": [
{
"step": 1,
"task": "Read load current",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-005",
"sensor_id": "load_current_a",
},
"response": '{"readings": [{"value": 520.0}]}',
"error": None,
"success": True,
},
{
"step": 2,
"task": "Check anomalous load-current readings",
"server": "tsfm",
"tool": "detect_anomalies",
"tool_args": {
"transformer_id": "T-005",
"sensor_id": "load_current_a",
},
"response": '{"anomaly_count": 3, "anomaly_rate_pct": 2.5}',
"error": None,
"success": True,
},
],
}
guarded = apply_explicit_fault_risk_adjudication(payload, enabled=True)
self.assertTrue(guarded["success"])
self.assertEqual(
guarded["fault_risk_adjudication"]["decision"],
"not_applicable",
)
self.assertEqual(
guarded["answer"],
"Peak load current was 520 A at 2024-01-07T12:00:00.",
)
self.assertNotIn("failed_steps", guarded)
def test_fault_risk_adjudication_domain_tags_are_exact(self):
monitoring_payload = {
"question": "Retrieve recent load current readings for T-005.",
"scenario": {
"domain_tags": ["wood", "multimedia"],
"text": "Retrieve current readings and report the peak value.",
},
"history": [],
}
work_order_payload = {
"question": "Summarize the current transformer status.",
"scenario": {"domain_tags": ["wo"]},
"history": [],
}
self.assertFalse(_fault_risk_adjudication_applies(monitoring_payload))
self.assertTrue(_fault_risk_adjudication_applies(work_order_payload))
def test_build_fault_risk_adjudication_state_uses_config(self):
with patch.dict(
os.environ,
{
"ENABLE_EXPLICIT_FAULT_RISK_ADJUDICATION": "1",
"ENABLE_MISSING_EVIDENCE_GUARD": "1",
},
):
config = load_fault_risk_adjudication_config()
history = [
{
"step": 1,
"task": "Analyze DGA",
"server": "fmsr",
"tool": "analyze_dga",
"tool_args": {"transformer_id": "T-015"},
"response": '{"diagnosis": "normal", "risk_level": "low"}',
"error": None,
"success": True,
}
]
adjudication = build_fault_risk_adjudication_state(
"Assess T-015 fault risk.",
history,
config,
)
self.assertEqual(adjudication["decision"], "finalize")
self.assertIsNone(fault_risk_adjudication_failed_step(adjudication))
def test_current_missing_evidence_hit_prefers_current_retry_hit(self):
first_miss = {
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": '{"readings": []}',
"error": None,
"success": True,
}
current_miss = {
**first_miss,
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "oil_temp_c",
},
"response": "No readings found for transformer T-015.",
}
hit = current_missing_evidence_hit([first_miss, current_miss], current_miss)
self.assertIsNotNone(hit)
self.assertEqual(hit["target"]["sensor_id"], "oil_temp_c")
self.assertIn("history[1]", hit["source"])
def test_detector_replan_budget_guard_blocks_after_total_attempts_exhausted(self):
with patch.dict(
os.environ,
{
"ENABLE_MISSING_EVIDENCE_REPAIR": "1",
"ENABLE_MISSING_EVIDENCE_GUARD": "1",
"MISSING_EVIDENCE_REPAIR_MAX_ATTEMPTS": "1",
"MISSING_EVIDENCE_REPAIR_MAX_ATTEMPTS_PER_TARGET": "2",
},
):
config = load_missing_evidence_repair_config()
state = build_missing_evidence_repair_state(config)
hit = {
"step": 2,
"tool": "get_sensor_readings",
"target": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"reason": "tool response was empty",
}
record_missing_evidence_retry_attempt(state, hit, action="retry_step")
target_attempts = {repair_target_key(hit): 1}
self.assertFalse(
can_retry_missing_evidence(config, state, hit, target_attempts)
)
def test_repair_state_finalizes_after_successful_retry(self):
with patch.dict(
os.environ,
{
"ENABLE_MISSING_EVIDENCE_REPAIR": "1",
"ENABLE_MISSING_EVIDENCE_GUARD": "1",
},
):
config = load_missing_evidence_repair_config()
state = build_missing_evidence_repair_state(config)
missing_entry = {
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": '{"readings": []}',
"error": None,
"success": True,
}
hit = current_missing_evidence_hit([missing_entry], missing_entry)
self.assertIsNotNone(hit)
state["triggered"] = True
state["attempts"].append(
{
"attempt_index": 1,
"source_step": hit["step"],
"tool": hit["tool"],
"target": hit["target"],
"reason": hit["reason"],
"action": "retry_step",
"result": "scheduled",
}
)
repaired_entry = {
**missing_entry,
"response": '{"readings": [{"timestamp": "2024-01-01T00:00:00", "value": 87.5}]}',
}
finalize_missing_evidence_repair_state(state, [missing_entry, repaired_entry])
self.assertEqual(state["name"], MISSING_EVIDENCE_REPAIR_NAME)
self.assertTrue(state["repaired"])
self.assertEqual(state["final_decision"], "continue")
def test_missing_evidence_guard_allows_evidence_limited_answer(self):
payload = {
"answer": "Cannot determine the maintenance decision because sensor evidence is missing.",
"success": True,
"history": [
{
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {"sensor_id": "winding_temp_top_c"},
"response": "No readings found for transformer T-015.",
"error": None,
"success": True,
}
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertTrue(guarded["success"])
self.assertTrue(guarded["mitigation_guard"]["triggered"])
self.assertFalse(guarded["mitigation_guard"]["blocked_final_answer"])
self.assertTrue(guarded["mitigation_guard"]["evidence_limited_answer"])
def test_missing_evidence_guard_allows_successful_retry_before_answer(self):
payload = {
"answer": "Create the work order using the repaired winding temperature evidence.",
"success": True,
"failed_steps": [],
"history": [
{
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": "No readings found for transformer T-015.",
"error": None,
"success": True,
},
{
"step": 1,
"task": "Retry sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": '{"readings": [{"timestamp": "2024-01-01T00:00:00", "value": 87.5}]}',
"error": None,
"success": True,
},
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertTrue(guarded["success"])
self.assertFalse(guarded["mitigation_guard"]["triggered"])
self.assertEqual(guarded["failed_steps"], [])
def test_missing_evidence_guard_allows_same_step_retry_with_repaired_args(self):
payload = {
"answer": "Use FM-006 sensor correlations for ongoing monitoring.",
"success": True,
"failed_steps": [],
"history": [
{
"step": 4,
"task": "Fetch sensor correlations",
"server": "fmsr",
"tool": "get_sensor_correlation",
"tool_args": {
"failure_mode_id": "Low-temperature overheating",
},
"response": (
'{"error": "Failure mode '
"'Low-temperature overheating' not found.\"}"
),
"error": "Failure mode 'Low-temperature overheating' not found.",
"success": False,
},
{
"step": 4,
"task": "Retry sensor correlations",
"server": "fmsr",
"tool": "get_sensor_correlation",
"tool_args": {"failure_mode_id": "FM-006"},
"response": (
'{"failure_mode_id": "FM-006", ' '"key_gases": ["C2H2", "H2"]}'
),
"error": None,
"success": True,
},
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertTrue(guarded["success"])
self.assertFalse(guarded["mitigation_guard"]["triggered"])
def test_missing_evidence_guard_blocks_work_order_before_retry(self):
payload = {
"answer": "Work order created, then evidence was repaired.",
"success": True,
"history": [
{
"step": 1,
"task": "Fetch sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": "No readings found for transformer T-015.",
"error": None,
"success": True,
},
{
"step": 2,
"task": "Create work order",
"server": "wo",
"tool": "create_work_order",
"tool_args": {"transformer_id": "T-015"},
"response": '{"work_order_id": "WO-123"}',
"error": None,
"success": True,
},
{
"step": 1,
"task": "Retry sensor readings",
"server": "iot",
"tool": "get_sensor_readings",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": '{"readings": [{"timestamp": "2024-01-01T00:00:00", "value": 87.5}]}',
"error": None,
"success": True,
},
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertFalse(guarded["success"])
self.assertTrue(guarded["mitigation_guard"]["blocked_work_order"])
def test_missing_evidence_guard_allows_valid_no_anomaly_result(self):
payload = {
"answer": "No anomalies were found, so defer outage and monitor.",
"success": True,
"history": [
{
"step": 1,
"task": "Detect anomalies",
"server": "tsfm",
"tool": "detect_anomalies",
"tool_args": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"response": (
'{"transformer_id": "T-015", "sensor_id": "winding_temp_top_c", '
'"total_readings": 720, "anomaly_count": 0, "anomalies": []}'
),
"error": None,
"success": True,
}
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertTrue(guarded["success"])
self.assertFalse(guarded["mitigation_guard"]["triggered"])
def test_missing_evidence_guard_scans_get_fault_record(self):
payload = {
"answer": "Use the missing fault record to schedule repair.",
"success": True,
"history": [
{
"step": 1,
"task": "Fetch fault record",
"server": "wo",
"tool": "get_fault_record",
"tool_args": {"fault_id": "F999"},
"response": '{"error": "Fault record F999 not found."}',
"error": None,
"success": True,
}
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertFalse(guarded["success"])
self.assertEqual(
guarded["mitigation_guard"]["hits"][0]["tool"],
"get_fault_record",
)
def test_missing_evidence_guard_scans_aat_tool_calls(self):
payload = {
"answer": "Created a work order for immediate repair.",
"success": True,
"history": [
{
"turn": 1,
"tool_calls": [
{
"name": "get_sensor_readings",
"arguments": {"sensor_id": "oil_temp_c"},
"output": "No data found for transformer T-020.",
},
{
"name": "create_work_order",
"arguments": {"transformer_id": "T-020"},
"output": "WO-123",
},
],
}
],
}
guarded = apply_missing_evidence_final_answer_guard(payload, enabled=True)
self.assertFalse(guarded["success"])
self.assertTrue(guarded["mitigation_guard"]["blocked_work_order"])
def test_missing_evidence_guard_unwraps_aat_text_envelopes(self):
outputs = [
{"type": "text", "text": "[]"},
{"type": "text", "text": '{"readings": []}'},
{"content": [{"type": "text", "text": '{"readings": []}'}]},
]
for output in outputs:
with self.subTest(output=output):
payload = {
"answer": "Schedule immediate maintenance using the empty evidence.",
"success": True,
"history": [
{
"turn": 1,
"tool_calls": [
{
"name": "get_sensor_readings",
"arguments": {
"transformer_id": "T-015",
"sensor_id": "winding_temp_top_c",
},
"output": output,
}
],
}
],
}
guarded = apply_missing_evidence_final_answer_guard(
payload,
enabled=True,
)
self.assertFalse(guarded["success"])
self.assertTrue(guarded["mitigation_guard"]["triggered"])
if __name__ == "__main__":
unittest.main()
|