15 KiB
PRECISION SURGICAL AUDIT
Date: September 30, 2026
Branch: precision
Focus: Surgical Root Cause Audit for Reference Contamination, Generic Component Duplication, and Topology Repair
1. System Context & Freeze Directive
The MTO2Doc / SID-AI Universal Engineering Graph system on the precision branch has achieved 100% recall across core engineering entities on Drawing A (26-KA-901) and 95.45% recall on Drawing B (26-KA-902).
In accordance with Sections 2, 46, and 51 of the instructions:
- Upstream perception components remain frozen: EasyOCR, PaddleOCR, YOLO models, line-tag stitching, DPI preprocessing, and ray-casting algorithms are NOT to be rewritten.
- No monkey patches: No drawing-specific tag checks, no pandas/csv post-processing hacks, no hardcoded exclusions.
- Surgical scope: Changes are strictly localized to downstream entity classification, reference isolation, generic component deduplication, and topology generation/validation.
2. Comprehensive Runtime Path Audit
Path 1: Current Canonical Entity Registry
- Source Files:
src/candidate_models.py,src/agents/compiler.py - Mechanisms:
CanonicalEntityRegistryinsrc/candidate_models.pyprovides multi-factor candidate deduplication (canonical_dedup_key), tracking observations and merge provenance.- In
CompilerAgent(src/agents/compiler.py),tag_alias_mapregisters uppercase, canonicalized (canonicalize_tag), and stripped-alphanumeric forms for every compiled entity.
- Findings:
tag_alias_mapsuccessfully indexes aliases, but downstream compilation lacked a structured model forReferenceItemto hold off-sheet continuation references.
Path 2: Current Generic-Component Creation Path
- Source Files:
src/agents/compiler.py,src/agents/output_generator.py - Current Code in
_compile_generic:generic_items = [ t for t in texts if t["classification"] in ('EQUIPMENT_TAG', 'GENERIC_TAG') ] - Root Cause Identified:
_compile_genericintentionally ingested allEQUIPMENT_TAGelements in addition toGENERIC_TAG. As a direct result, canonical equipment like26-KA-902,26-CX-9021,26-CX-9222,26-KZ-902, and26-KA-902-M01were inserted intograph.generic_components. Then inoutput_generator.py, each generic component was exported tosppid_import_tables.csvas a duplicateGENERIC_COMPONENT. - Surgical Fix: Remove
EQUIPMENT_TAGfrom_compile_generic. Attempt resolution ofGENERIC_TAGitems againsttag_alias_mapbefore emitting. If matched to an existing canonical entity or reference, absorb as attribute/alias; only true unknowns remain generic.
Path 3: Current Reference Classification Path
- Source Files:
src/utils/tag_classifier.py,src/agents/compiler.py - Mechanisms:
tag_classifier.pychecksis_ref_contextusingr'\b(?:FROM|TO|TIE-IN|CONTINUED\s+ON|SEE\s+DWG|REF\s+DWG)\b'and setsit_copy["is_reference"] = True.- However, in
_compile_equipmentand_compile_instruments, theis_referenceattribute was completely ignored. - Furthermore,
_BARE_INSTRUMENT_SEARCHintag_classifier.pydid not checkis_ref_context.
- Root Cause Identified: Tags with external continuation context (such as
FROM 27-PIT-0001BandFROM 26-PIT-9087on Drawing B) were classified asINSTRUMENT_TAGand compiled directly intograph.instruments, polluting the physical entity count. - Surgical Fix: In
CompilerAgent, extract and route all items marked withis_referenceor reference context (FROM ...,TO ...,REFER DWG) into a newgraph.referencescollection (ReferenceItem). They must not become local physical equipment or instruments.
Path 4: Current Relationship Generation Path
- Source Files:
src/utils/line_tracer.py,src/agents/compiler.py - Mechanisms:
line_tracer.pytraces lines and pairs valves, instruments, PSVs, lines, panels, and earthing.- In
line_tracer.pylines 225-234, an instrument lacking a target fell back toline_items[0]orequipment[0]. - In
compiler.pylines 204-228, another fallback loop forcibly addedMONITORSrelationships from instruments tograph.lines[0].tagorgraph.equipment[0].tagwithconfidence=0.85to "guarantee zero orphan instruments".
- Root Cause Identified: Both layers generated artificial, non-physical
MONITORSedges solely to eliminate orphan instruments, creating severe relationship false positives. - Surgical Fix: Remove the generic fallback
MONITORSin bothline_tracer.pyandcompiler.py. If physical/signal connection evidence does not exist, do not fabricate edges; leave them unlinked or tag themNEEDS_REVIEW.
Path 5: Current Relationship Confidence Calculation
- Source Files:
src/utils/line_tracer.py,src/agents/compiler.py - Current Code: Confidence is hardcoded as
1.0or0.85without evaluating geometric proximity, continuity, or port alignment. - Surgical Fix: Compute
confidencefrom an evidence vector:geometry_confidence: distance from trace / snap distancetopology_confidence: port / inline match vs proximitysemantic_confidence: loop sequence number match (e.g.9054in linePV-26-9054) Composite relationship confidence =w_g \cdot C_{\text{geom}} + w_t \cdot C_{\text{topo}} + w_s \cdot C_{\text{sem}}.
Path 6: Current Relationship Validation Path
- Source Files:
src/agents/compiler.py,src/agents/validation.py - Current Code:
_compile_relationshipschecks that source and target map to canonical tags intag_alias_map, drops self-loops, and deduplicates edges. - Surgical Enhancement: Expand validation to check domain compatibility (e.g.
INSTRUMENTcannot have processFEEDSto aLINE;REFERENCEcannot have physicalCONNECTED_TOto a local entity).
Path 7: Current Graph Insertion Path
- Source Files:
src/agents/compiler.py - Mechanisms: Compiles each entity category into
UniversalEngineeringGraphfields (equipment,lines,instruments,valves,safety_relief_valves,generic_components,annotations,relationships). - Surgical Enhancement: Add
references: List[ReferenceItem]toUniversalEngineeringGraph.
Path 8: Current Re-Extraction Merge Path
- Source Files:
src/agents/re_extractor.py,src/graph.py,src/agents/compiler.py - Idempotency Guarantee: Deduplication dictionaries (
seen_equip,seen_lines,seen_loops,seen_valves) merge repeated observations of the same tag. Re-extractingPIT-9016does not produce two entities; it merges into the existing entity and updates observations/provenance.
Path 9: Current Exporter Input Path
- Source Files:
src/agents/output_generator.py - Finding: Exporters consume
UniversalEngineeringGraphdirectly. By eliminating duplicate generic components and reference pollution at the compiler stage, export outputs (Excel, CSV, JSON, XML) remain strictly canonical without requiring post-export cleanup hacks.
3. Specific Defect Discoveries & Exact Root Causes
| Defect ID | Observed Defect | Root Cause Location | Exact Root Cause |
|---|---|---|---|
| AUD-01 | 26-KA-902, 26-CX-9021, etc. duplicated in generic_components |
src/agents/compiler.py#L1363 |
_compile_generic explicitly filtered for ('EQUIPMENT_TAG', 'GENERIC_TAG'), re-compiling every equipment tag into generic components. |
| AUD-02 | STAGE-26-000001-001-26-PIT-9087 created as equipment |
src/utils/tag_stitcher.py#L385 & src/utils/tag_classifier.py#L730 |
Vertical bubble stitcher concatenated vertically adjacent drawing number and text. Then _GENERIC_EQUIP_PATTERN matched prefix STA from STAGE against _EQUIP_PREFIX_ALLOWLIST. |
| AUD-03 | 27-PIT-0001B & 26-PIT-9087 created as physical instruments |
src/agents/compiler.py#L662 |
_compile_instruments ignored is_reference flag set by tag_classifier.py, promoting off-sheet references to physical instruments. |
| AUD-04 | 27-PY-0001BA & 27-PY-0001BB missed from instruments (classified as NOTE) |
src/utils/tag_classifier.py#L696 |
if len(seq) == 6: continue assumed all 6-character sequences were drawing numbers. For 0001BA (4 digits + 2 letters), len == 6 caused it to be dropped to NOTE. |
| AUD-05 | 26-CK-921 missed from equipment on Drawing B |
src/agents/compiler.py#L283 |
Hardcoded condition re.search(r'^(?:26-)?CK-911\b', ...) only included CK-911 (Drawing A), leaving CK-921 (Drawing B) in VALVE_TAG. |
| AUD-06 | Generic fallback MONITORS relationship hallucination |
src/agents/compiler.py#L220-L226 & src/utils/line_tracer.py#L225-L242 |
Unconnected instruments were artificially linked to graph.lines[0] or equipment[0] with confidence=0.85 / 1.0. |
| AUD-07 | Non-piping specs (DSS-2500-DSS-EL, S-9003-MECHANIC) compiled as lines |
src/agents/compiler.py#L422-L432 |
Line tag filter did not reject spec codes or mechanical note sequences without standard fluid service codes. |
4. Dual-Drawing Baseline Metrics (Pre-Modification)
Evaluated via scratch_evaluate_both.py against Ground Truth from pid_stuff/PID_Extraction_RevB_Corrected_26-KA-901_902 (2).xlsx:
Drawing A (26-KA-901):
- Ground Truth: 104 entities (Equipment: 7, PSV: 2, Instruments: 20, Valves: 40, Lines: 35)
- Compiled Entities: 136
- True Positives (TP): 104
- False Positives (FP): 32
- False Negatives (FN): 0
- Overall Recall: 100.00%
- Overall Precision: 76.47%
- Overall F1: 0.8667
- Generic Components: 14 (all duplicates of equipment)
- Relationships: 85
Drawing B (26-KA-902):
- Ground Truth: 88 entities (Equipment: 6, PSV: 2, Instruments: 14, Valves: 37, Lines: 29)
- Compiled Entities: 114
- True Positives (TP): 84
- False Positives (FP): 27
- False Negatives (FN): 4 (
26-CK-921,27-PY-0001BA,27-PY-0001BB,26GB9178) - Overall Recall: 95.45%
- Overall Precision: 76.32%
- Overall F1: 0.8482
- Generic Components: 12 (all duplicates of equipment/references)
- Relationships: 78
5. Surgical Action Plan
-
Model Extension (
src/models.py):- Add
ReferenceItemmodel to represent off-sheet, continuation, and tie-in boundary references. - Add
references: List[ReferenceItem]toUniversalEngineeringGraph. - Add
domain: strandevidence_vector: Optional[Dict[str, float]]toRelationship.
- Add
-
Classification & Stitcher Precision (
src/utils/tag_classifier.py&src/utils/tag_stitcher.py):- In
tag_stitcher.py: prevent stitching multi-line bubbles if any line matches drawing number patterns (\d{5,6}) or stage note words. - In
tag_classifier.py: updatelen(seq) == 6drawing reference check toseq.isdigit() and len(seq) == 6so alphanumeric sequences like0001BA/0001BBare preserved as instruments. - In
tag_classifier.py: refine_GENERIC_EQUIP_PATTERNprefix extraction to take the full word before the hyphen (preventingSTAGEfrom matching prefixSTA). - In
tag_classifier.py: generalizeCKequipment classification for suction strainers with 3-digit sequence numbers (CK-\d{3}).
- In
-
Compiler Surgery (
src/agents/compiler.py):- Eliminate Generic Duplication: Change
_compile_genericto only ingestGENERIC_TAG. Attempt resolution ofGENERIC_TAGitems against canonical entities and aliases; if resolved, merge; otherwise emit. - Isolate References: In
_compile_equipmentand_compile_instruments, items withis_reference=Trueor contextual reference markers (FROM,TO,REFER) are compiled intograph.references, NOT physical entities. - Base Tag / Suffix Resolution: Tags with suffix qualifiers (e.g.
26-KA-902-STAGE,26-CX-9222-2500) resolve to their base canonical entity (26-KA-902,26-CX-9222), recording the suffix as an attribute/alias without creating duplicate objects. - Line Quality Filter: Exclude spec strings lacking standard fluid service codes (
DSS-2500-DSS-EL,S-9003-MECHANIC).
- Eliminate Generic Duplication: Change
-
Relationship Engine & Confidence Overhaul (
src/utils/line_tracer.py&src/agents/compiler.py):- Delete arbitrary fallback
MONITORSattachments tolines[0]/equipment[0]. - Calculate relationship confidence from the multi-factor evidence vector (geometry, topology, semantics).
- Enforce relationship type semantics and domain compatibility.
- Delete arbitrary fallback
-
Dual-Drawing Verification:
- Execute regression test across both Drawing A and Drawing B.
- Validate that Drawing A recall remains 100%, Drawing B recall reaches
\ge 98\%, generic duplication drops to near zero, reference contamination is eliminated, and relationship precision improves.
Audit Status: APPROVED FOR IMPLEMENTATION.