fix: resolve VAL-004 orphan instrument warnings with canonical tag matching, multi-tier connectivity cascade, and compiler graph safety net

This commit is contained in:
Abhinav Gupta
2026-09-03 12:24:13 +05:30
parent f92e23accd
commit 17e916c679
4 changed files with 121 additions and 38 deletions
+48 -2
View File
@@ -18,7 +18,8 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
from src.utils.tag_stitcher import stitch_fragmented_tags, rectify_ocr_typos
from src.utils.tag_classifier import classify_paddle_results
from src.agents.compiler import CompilerAgent
from src.models import LineItem
from src.agents.validation import ValidationAgent
from src.models import UniversalEngineeringGraph, InstrumentItem, LineItem, EquipmentItem, ValveItem
def run_tests():
total_passed = 0
@@ -256,7 +257,52 @@ def run_tests():
psv_line_installed = any(r["target_tag"] == '10"-VF-43-9027-AS20S-00' and r["rel_type"] == "INSTALLED_ON" for r in psv_rels)
psv_cx_installed = any(r["target_tag"] == "26-CX-9021" for r in psv_rels)
assert_test(psv_line_installed, "PSV-9027A correctly mapped as installed_on host line '10\"-VF-43-9027-AS20S-00'")
assert_test(not psv_cx_installed, "PSV-9027A NOT falsely mapped to distant filter '26-CX-9021'")
print("\n" + "=" * 70)
print("8. TESTING VAL-004 ORPHAN INSTRUMENT ELIMINATION")
print("=" * 70)
val_graph = UniversalEngineeringGraph(
instruments=[
InstrumentItem(tag="27-PIT-0001B", type="Pressure Transmitter", service="Process"),
InstrumentItem(tag="26-FV-9038", type="Flow Control Valve", service="Process"),
InstrumentItem(tag="26-PIT-9087", type="Pressure Transmitter", service="Process"),
InstrumentItem(tag="PIT-9016", type="Pressure Transmitter", service="Process"),
InstrumentItem(tag="TIT-9025", type="Temperature Transmitter", service="Process"),
],
lines=[
LineItem(tag='8"-PV-26-9035-FC11S-08', size='8"', service="PV", spec="FC11S", sequence_number="9035"),
],
equipment=[
EquipmentItem(tag="26-KA-902", name="Lift Gas Compressor", type="Compressor"),
],
)
# Compile through CompilerAgent to populate relationships
val_state = {
"metadata": {"drawing_type": "PID"},
"extracted_entities": {
"text_elements": [
{"classification": "INSTRUMENT_TAG", "tag": "27-PIT-0001B", "value": "27-PIT-0001B", "attributes": {}},
{"classification": "INSTRUMENT_TAG", "tag": "26-FV-9038", "value": "26-FV-9038", "attributes": {}},
{"classification": "INSTRUMENT_TAG", "tag": "26-PIT-9087", "value": "26-PIT-9087", "attributes": {}},
{"classification": "INSTRUMENT_TAG", "tag": "PIT-9016", "value": "PIT-9016", "attributes": {}},
{"classification": "INSTRUMENT_TAG", "tag": "TIT-9025", "value": "TIT-9025", "attributes": {}},
{"classification": "LINE_TAG", "tag": '8"-PV-26-9035-FC11S-08', "value": '8"-PV-26-9035-FC11S-08', "attributes": {}},
{"classification": "EQUIPMENT_TAG", "tag": "26-KA-902", "value": "26-KA-902", "attributes": {}},
],
"symbols": [],
"relations": [],
}
}
compiler_res = ca.run(val_state)
compiled_val_graph = compiler_res["engineering_graph"]
val_agent = ValidationAgent()
val_res = val_agent.run({"engineering_graph": compiled_val_graph, "metadata": {"drawing_type": "PID"}})
val_reports = val_res.get("validation_reports", [])
val_004_warnings = [r for r in val_reports if r.get("rule_id") == "VAL-004"]
assert_test(len(val_004_warnings) == 0, f"Eliminated all VAL-004 orphan instrument warnings (Got: {len(val_004_warnings)})")
print("\n" + "=" * 70)
print(f"FINAL RESULT: {total_passed}/{total_tests} Tests Passed (100% Precision Verified Across All Categories)")