fix(pipeline): complete Round 3 full repair pass for SID-AI extraction accuracy

This commit is contained in:
Abhinav Gupta
2026-08-13 10:35:28 +05:30
parent 4adbbfae46
commit 6c89ab9fb7
9 changed files with 443 additions and 112 deletions
+29
View File
@@ -98,3 +98,32 @@ class TestTagCanonicalization:
assert len(inst_tags) == 2, (
f"Expected 2 distinct INSTRUMENT_TAGs, got {len(inst_tags)}"
)
def test_relationship_ck921_canonicalized_and_self_loop_dropped(self):
"""
Test that CompilerAgent resolves bare tag CK-921 to master tag 26-CK-921
and drops self-loop edges (CK-921 -> 26-CK-921).
"""
from src.agents.compiler import CompilerAgent
from src.models import EquipmentItem, UniversalEngineeringGraph
compiler = CompilerAgent()
graph = UniversalEngineeringGraph(drawing_type="PID", discipline="Piping")
graph.equipment = [
EquipmentItem(tag="26-CK-921", type="Check Valve", name="Check Valve", aliases=["CK-921"])
]
raw_relations = [
{"source_tag": "CK-921", "target_tag": "26-CK-921", "rel_type": "connects_to"},
{"source_tag": "LP FLARE", "target_tag": "CK-921", "rel_type": "feeds"},
]
tag_alias_map = {"26-CK-921": "26-CK-921", "CK-921": "26-CK-921", "LP FLARE": "LP FLARE"}
compiled_rels = compiler._compile_relationships(raw_relations, tag_alias_map)
# 1. Self-loop edge CK-921 -> 26-CK-921 must be dropped
# 2. LP FLARE -> CK-921 must be resolved to target "26-CK-921"
assert len(compiled_rels) == 1
assert compiled_rels[0].source == "LP FLARE"
assert compiled_rels[0].target == "26-CK-921"
+15 -17
View File
@@ -19,13 +19,21 @@ def _make_ocr_item(text: str, conf: float = 0.95) -> dict:
class TestLineTagGrammar:
def test_corrupt_size_262_inch_is_flagged(self):
"""262\"-VA-26-9110-AS20S-00 has an impossible size — must be flagged."""
"""262\"-VA-26-9110-AS20S-00 has an impossible size — must be flagged invalid."""
valid, flag_reason = _validate_line_tag_size('262"-VA-26-9110-AS20S-00')
assert not valid, "Expected invalid=True for 262\" size"
assert flag_reason and "corrupt_merge" in flag_reason, (
f"Expected 'corrupt_merge' in flag_reason, got: {flag_reason}"
assert flag_reason and "size_out_of_range" in flag_reason, (
f"Expected 'size_out_of_range' in flag_reason, got: {flag_reason}"
)
def test_12mm_size_prefix_preserved(self):
"""12mm-PV-26-9116-FD70X-00 must retain 12mm size prefix and classify cleanly."""
items = [_make_ocr_item('12mm-PV-26-9116-FD70X-00')]
result = classify_paddle_results(items, drawing_type="PID")
line_tags = [r for r in result if r["classification"] == "LINE_TAG"]
assert len(line_tags) == 1
assert "12MM" in line_tags[0]["tag"]
def test_valid_sizes_pass_without_flag(self):
"""Standard pipe sizes should pass without any flag_reason."""
for size_prefix in ['2"-VA-26-9110', '8"-PV-26-9035', '12"-PV-26-9035', '3/4"-VA-26-9114']:
@@ -40,27 +48,17 @@ class TestLineTagGrammar:
items = [_make_ocr_item('3/4"-VA-26-9114-AC21-00NOTE20')]
result = classify_paddle_results(items, drawing_type="PID")
line_tags = [r for r in result if r["classification"] == "LINE_TAG"]
# The tag should be found and NOTE20 should NOT be in the tag string
for lt in line_tags:
assert "NOTE20" not in lt["tag"] and "NOTE" not in lt["tag"].upper().replace("NOTE", ""), (
f"NOTE20 should be stripped from line tag, got: '{lt['tag']}'"
)
# Verify that a flag_reason is recorded
if line_tags:
flag_reasons = [lt.get("flag_reason") for lt in line_tags]
# At minimum one should be flagged for note stripping
# (acceptable if stripped version is valid)
def test_corrupt_size_emitted_with_low_confidence(self):
"""Corrupt-size tags should be emitted (for human review) but with confidence <= 0.5."""
def test_corrupt_size_rejected_from_line_tag_emission(self):
"""Corrupt-size tags (262") must NOT be emitted as LINE_TAG."""
items = [_make_ocr_item('262"-VA-26-9110-AS20S-00')]
result = classify_paddle_results(items, drawing_type="PID")
line_tags = [r for r in result if r["classification"] == "LINE_TAG"]
for lt in line_tags:
if lt.get("flag_reason") and "corrupt" in lt["flag_reason"]:
assert lt.get("confidence", 1.0) <= 0.5, (
f"Expected confidence <= 0.5 for corrupt-size tag, got {lt.get('confidence')}"
)
assert len(line_tags) == 0, f"Expected 0 LINE_TAGs for corrupt 262\" size, got {line_tags}"
def test_clean_line_tag_has_high_confidence(self):
"""A well-formed line tag should have full confidence and no flag_reason."""
@@ -77,7 +75,7 @@ class TestLineTagGrammar:
def test_size_validation_out_of_range(self):
"""A numeric size > 600 that is not a standard NPS should be flagged invalid."""
valid, flag_reason = _validate_line_tag_size('900-VA-26-9110-AS20S-00')
# 900 is out of range for standard pipe sizes
assert flag_reason is not None, (
"Expected a flag_reason for out-of-range size 900"
)
+62
View File
@@ -117,3 +117,65 @@ class TestEquipmentDatasheet:
if "26-KA-901" in result:
assert "material" in result["26-KA-901"]
assert "LTCS" in result["26-KA-901"]["material"]
def test_full_ka902_datasheet_block_parsing(self):
"""Test complete 26-KA-902 synthetic datasheet block matching QA report."""
block = [
_make_item("26-KA-902", "EQUIPMENT_TAG", 0.20),
_make_item("SERVICE 3RD STAGE HP GAS EXPORT COMPRESSOR", "NOTE", 0.21),
_make_item("DUTY kW 1835", "NOTE", 0.22),
_make_item("FLOW RATE kg/h 62809", "NOTE", 0.23),
_make_item("DISCHARGE / SUCTION OP. PRESS. (MAX) Barg 199 / 108.5", "NOTE", 0.24),
_make_item("DISCHARGE / SUCTION DESIGN PRESS. (MAX) Barg FV / 286", "NOTE", 0.25),
_make_item("DISCHARGE / SUCTION DESIGN TEMP. °C -46 / 160", "NOTE", 0.26),
_make_item("MATERIAL LTCS (1.7218)", "NOTE", 0.27),
_make_item("QUANTITY 1x100%", "NOTE", 0.28),
_make_item("TYPE VARIABLE SPEED MOTOR DRIVEN CENTRIFUGAL", "NOTE", 0.29),
_make_item("VENDOR MAN ENERGY SOLUTIONS", "NOTE", 0.30),
]
result = parse_equipment_datasheets(block)
assert "26-KA-902" in result
fields = result["26-KA-902"]
assert "duty" in fields and "1835" in fields["duty"]
assert "flow_rate" in fields and "62809" in fields["flow_rate"]
assert "design_pressure" in fields and "286" in fields["design_pressure"]
assert "design_temperature" in fields and "160" in fields["design_temperature"]
assert "material" in fields and "LTCS" in fields["material"]
assert "quantity" in fields and "1x100%" in fields["quantity"]
assert "type" in fields and "CENTRIFUGAL" in fields["type"]
assert "vendor" in fields and "MAN ENERGY SOLUTIONS" in fields["vendor"]
def test_vendor_scope_marker_rejected(self):
"""Bare word 'VENDOR' scope marker must NOT populate the Vendor field."""
items = [
_make_item("26-CX-9021", "EQUIPMENT_TAG", 0.30),
_make_item("VENDOR", "NOTE", 0.31),
]
result = parse_equipment_datasheets(items)
if "26-CX-9021" in result:
assert "vendor" not in result["26-CX-9021"]
def test_parenthetical_note_rejected_from_type(self):
"""Raw parenthetical note sentences must NOT populate the equipment Type field."""
items = [
_make_item("26-KZ-902", "EQUIPMENT_TAG", 0.40),
_make_item("(MOTOR PURGE SYSTEM ; EX-P TYPE MOTOR)", "NOTE", 0.41),
]
result = parse_equipment_datasheets(items)
if "26-KZ-902" in result:
assert "type" not in result["26-KZ-902"]
def test_psv_flange_spec_parsing(self):
"""Test parse_psv_flange_specs for 3"x4" 300# 150# near PSV tag."""
from src.utils.datasheet_parser import parse_psv_flange_specs
items = [
_make_item("26-PSV-9066A", "PSV_TAG", 0.50),
_make_item('3"x4" 300# 150#', "NOTE", 0.52),
]
result = parse_psv_flange_specs(items)
assert "26-PSV-9066A" in result
flange = result["26-PSV-9066A"]
assert flange["inlet_size"] == '3"'
assert flange["outlet_size"] == '4"'
assert flange["inlet_spec"] == "300#"
+10
View File
@@ -116,3 +116,13 @@ class TestSetpointFilter:
f"Expected confidence <= 0.25 for flagged item '{r['tag']}', "
f"got {r.get('confidence')}"
)
def test_spatial_multi_box_setpoint_detection(self):
"""When PI-150 is in one box and 'SD HH: 150 BARG' is adjacent, PI-150 must be demoted."""
from src.utils.tag_classifier import _is_setpoint_context_spatial
items = [
{"text": "PI-150", "confidence": 0.95, "center_x": 0.50, "center_y": 0.30},
{"text": "SD HH: 150 BARG", "confidence": 0.95, "center_x": 0.52, "center_y": 0.32},
]
assert _is_setpoint_context_spatial(0, items) is True