File size: 29,487 Bytes
5008b9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Deep Debugger Module β€” FVDebug Logic
=====================================

Based on: FVDebug (Formal Verification Debugging with Balanced Analysis)

When code fails formal checks (SymbiYosys), this module:
1. Runs SymbiYosys and parses the failure trace / counterexample.
2. Builds a Causal Graph of signals involved in the failure.
3. For EVERY suspicious signal, the agent MUST produce:
     - 2 arguments FOR it being the bug root cause
     - 2 arguments AGAINST it being the bug root cause
   This is MANDATORY to prevent confirmation bias (the "For-and-Against" protocol).
4. Only after balanced analysis, the debugger decides on the fix.

Tools used: SymbiYosys (sby), Yosys (synthesis), Icarus Verilog (sim).
"""

import os
import re
import json
import logging
import subprocess
import tempfile
from dataclasses import dataclass, field, asdict
from typing import Any, Dict, List, Optional, Tuple

logger = logging.getLogger(__name__)


# ─── Data Structures ─────────────────────────────────────────────────

@dataclass
class ForAgainstArgument:
    """A single argument for or against a signal being the root cause."""
    stance: str        # "FOR" | "AGAINST"
    reasoning: str     # The argument text
    evidence: str = "" # Supporting evidence (line ref, VCD data, etc.)


@dataclass
class SuspiciousSignal:
    """A signal flagged as potentially responsible for the failure."""
    name: str
    module: str
    line: int
    expression: str = ""
    for_arguments: List[ForAgainstArgument] = field(default_factory=list)
    against_arguments: List[ForAgainstArgument] = field(default_factory=list)
    verdict: str = ""          # "BUG" | "NOT_BUG" | "UNCERTAIN"
    confidence: float = 0.0   # 0.0 to 1.0


@dataclass
class CausalGraphNode:
    """A node in the failure causal graph."""
    signal: str
    driver_type: str         # "always_ff", "always_comb", "assign"
    source_line: int
    dependencies: List[str]  # Signals feeding into this node
    value_at_failure: str = ""


@dataclass
class CausalGraph:
    """Directed graph of signal dependencies involved in the failure."""
    nodes: Dict[str, CausalGraphNode] = field(default_factory=dict)
    root_signal: str = ""                  # The assertion/property that failed
    failure_time: int = 0
    counterexample: str = ""

    def get_cone_of_influence(self, signal: str, max_depth: int = 8) -> List[str]:
        """Get all signals in the backward cone of influence."""
        visited = set()
        self._coi_walk(signal, visited, 0, max_depth)
        return list(visited)

    def _coi_walk(self, sig: str, visited: set, depth: int, max_depth: int):
        if depth > max_depth or sig in visited:
            return
        visited.add(sig)
        node = self.nodes.get(sig)
        if node:
            for dep in node.dependencies:
                self._coi_walk(dep, visited, depth + 1, max_depth)

    def to_mermaid(self) -> str:
        """Export the causal graph as a Mermaid diagram."""
        lines = ["graph TD"]
        for sig, node in self.nodes.items():
            safe_sig = sig.replace("[", "_").replace("]", "_").replace(".", "_")
            for dep in node.dependencies:
                safe_dep = dep.replace("[", "_").replace("]", "_").replace(".", "_")
                lines.append(f"    {safe_dep} --> {safe_sig}")
        return "\n".join(lines)


@dataclass
class FormalFailure:
    """Parsed result from a SymbiYosys formal verification run."""
    property_name: str
    property_type: str        # "assert", "cover", "assume"
    status: str               # "FAIL", "PASS", "UNKNOWN", "ERROR"
    counterexample_trace: str  # Raw CEX trace text
    failing_step: int = 0
    signals_in_cex: List[str] = field(default_factory=list)
    error_message: str = ""


@dataclass
class DebugVerdict:
    """Final verdict from the Deep Debugger."""
    root_cause_signal: str
    root_cause_line: int
    root_cause_file: str
    fix_description: str
    confidence: float
    causal_graph: CausalGraph
    suspicious_signals: List[SuspiciousSignal]
    balanced_analysis_log: str   # Full for-against reasoning for audit


# ─── SymbiYosys Interface ────────────────────────────────────────────

class SymbiYosysRunner:
    """
    Runs SymbiYosys formal verification and parses results.
    """

    def __init__(self, sby_bin: str = "sby", yosys_bin: str = "yosys"):
        self.sby_bin = sby_bin
        self.yosys_bin = yosys_bin

    def run_formal(self, sby_config_path: str, work_dir: str = "") -> FormalFailure:
        """
        Run SymbiYosys and return parsed failure info.
        
        Args:
            sby_config_path: Path to the .sby config file
            work_dir: Working directory for sby output
            
        Returns:
            FormalFailure with status, CEX trace, and signal list.
        """
        if not work_dir:
            work_dir = os.path.dirname(sby_config_path)

        # Clean previous run directory
        sby_name = os.path.splitext(os.path.basename(sby_config_path))[0]
        sby_work = os.path.join(work_dir, sby_name)
        if os.path.isdir(sby_work):
            import shutil
            shutil.rmtree(sby_work, ignore_errors=True)

        try:
            result = subprocess.run(
                [self.sby_bin, "-f", sby_config_path],
                capture_output=True,
                text=True,
                cwd=work_dir,
                timeout=300,
            )
            output = result.stdout + "\n" + result.stderr
            return self._parse_sby_output(output, sby_work, sby_name)

        except subprocess.TimeoutExpired:
            return FormalFailure(
                property_name="timeout",
                property_type="assert",
                status="ERROR",
                counterexample_trace="",
                error_message="SymbiYosys timed out after 300s",
            )
        except FileNotFoundError:
            return FormalFailure(
                property_name="missing_tool",
                property_type="assert",
                status="ERROR",
                counterexample_trace="",
                error_message=f"SymbiYosys binary not found at '{self.sby_bin}'",
            )
        except Exception as e:
            return FormalFailure(
                property_name="exception",
                property_type="assert",
                status="ERROR",
                counterexample_trace="",
                error_message=str(e),
            )

    def _parse_sby_output(self, output: str, work_dir: str, name: str) -> FormalFailure:
        """Parse SymbiYosys stdout/stderr into a FormalFailure."""
        status = "UNKNOWN"
        prop_name = ""
        prop_type = "assert"
        cex_trace = ""
        failing_step = 0
        signals = []

        # Determine overall status
        if "DONE (PASS" in output:
            status = "PASS"
        elif "DONE (FAIL" in output:
            status = "FAIL"
        elif "DONE (ERROR" in output or "ERROR" in output:
            status = "ERROR"

        # Extract failing property
        m = re.search(r'Assert failed in .+?: (.+)', output)
        if m:
            prop_name = m.group(1).strip()

        # Extract failing step
        m = re.search(r'BMC failed at step\s+(\d+)', output)
        if not m:
            m = re.search(r'Induction failed at step\s+(\d+)', output)
        if m:
            failing_step = int(m.group(1))

        # Try to read the VCD counterexample
        cex_vcd = os.path.join(work_dir, "engine_0", "trace.vcd")
        if not os.path.exists(cex_vcd):
            cex_vcd = os.path.join(work_dir, "engine_0", "trace0.vcd")
        if os.path.exists(cex_vcd):
            try:
                with open(cex_vcd, "r", errors="replace") as f:
                    cex_trace = f.read()[:10000]  # Truncate for context
                # Extract signal names from VCD
                signals = re.findall(r'\$var\s+\w+\s+\d+\s+\S+\s+(\w+)', cex_trace)
            except Exception:
                pass

        # Try text-based counterexample
        if not cex_trace:
            cex_txt = os.path.join(work_dir, "engine_0", "trace.txt")
            if os.path.exists(cex_txt):
                try:
                    with open(cex_txt, "r") as f:
                        cex_trace = f.read()[:10000]
                except Exception:
                    pass

        return FormalFailure(
            property_name=prop_name,
            property_type=prop_type,
            status=status,
            counterexample_trace=cex_trace,
            failing_step=failing_step,
            signals_in_cex=signals,
            error_message="" if status != "ERROR" else output[-500:],
        )

    def generate_sby_config(
        self,
        design_name: str,
        rtl_files: List[str],
        properties_file: str = "",
        mode: str = "bmc",
        depth: int = 20,
        engine: str = "smtbmc",
    ) -> str:
        """
        Generate a .sby configuration file content.
        
        Args:
            design_name:     Top module name
            rtl_files:       List of Verilog source files
            properties_file: Optional SVA properties file
            mode:            "bmc" | "prove" | "cover"
            depth:           BMC depth
            engine:          "smtbmc" | "aiger" | "abc"
        """
        files_section = "\n".join(rtl_files)
        if properties_file:
            files_section += f"\n{properties_file}"

        return f"""[tasks]
{mode}

[options]
{mode}:
mode {mode}
depth {depth}

[engines]
{mode}:
{engine}

[script]
read -formal {' '.join(os.path.basename(f) for f in rtl_files)}
{f'read -formal {os.path.basename(properties_file)}' if properties_file else ''}
prep -top {design_name}

[files]
{files_section}
"""


# ─── Causal Graph Builder ────────────────────────────────────────────

class CausalGraphBuilder:
    """
    Builds a causal graph from RTL + formal failure trace.
    
    The causal graph connects the failing assertion to the cone of
    signals that contributed to the failure, enabling systematic
    root-cause isolation.
    """

    def __init__(self):
        self._assignments: List[Dict[str, Any]] = []

    def build(
        self,
        rtl_path: str,
        failure: FormalFailure,
    ) -> CausalGraph:
        """Build a causal graph from RTL and formal failure."""
        graph = CausalGraph(
            root_signal=failure.property_name,
            failure_time=failure.failing_step,
            counterexample=failure.counterexample_trace[:2000],
        )

        # Parse RTL assignments
        self._parse_rtl(rtl_path)

        # Build graph nodes from assignments
        for asgn in self._assignments:
            sig = asgn["signal"]
            graph.nodes[sig] = CausalGraphNode(
                signal=sig,
                driver_type=asgn["type"],
                source_line=asgn["line"],
                dependencies=asgn["deps"],
            )

        # If we have CEX signals, annotate values
        if failure.signals_in_cex:
            for sig_name in failure.signals_in_cex:
                if sig_name in graph.nodes:
                    graph.nodes[sig_name].value_at_failure = "in_cex"

        return graph

    def _parse_rtl(self, rtl_path: str):
        """Parse RTL to extract signal assignments (regex-based)."""
        self._assignments.clear()
        if not os.path.exists(rtl_path):
            return

        try:
            with open(rtl_path, "r") as f:
                lines = f.readlines()
        except Exception:
            return

        in_ff = False
        in_comb = False

        for i, line in enumerate(lines, 1):
            s = line.strip()

            if re.search(r'always_ff\b|always\s*@\s*\(\s*posedge', s):
                in_ff = True
                in_comb = False
            elif re.search(r'always_comb\b|always\s*@\s*\(\*\)', s):
                in_comb = True
                in_ff = False
            elif s.startswith("end") and (in_ff or in_comb):
                in_ff = False
                in_comb = False

            # Continuous assign
            m = re.match(r'\s*assign\s+(\w+)\s*=\s*(.+?)\s*;', s)
            if m:
                sig, rval = m.groups()
                deps = re.findall(r'\b([a-zA-Z_]\w*)\b', rval)
                self._assignments.append({
                    "signal": sig, "rvalue": rval, "type": "assign",
                    "line": i, "deps": deps,
                })
                continue

            # Non-blocking
            m = re.match(r'\s*(\w+)\s*<=\s*(.+?)\s*;', s)
            if m:
                sig, rval = m.groups()
                deps = re.findall(r'\b([a-zA-Z_]\w*)\b', rval)
                self._assignments.append({
                    "signal": sig, "rvalue": rval,
                    "type": "always_ff" if in_ff else "always_comb",
                    "line": i, "deps": deps,
                })
                continue

            # Blocking in always
            if in_comb or in_ff:
                m = re.match(r'\s*(\w+)\s*=\s*(.+?)\s*;', s)
                if m:
                    sig, rval = m.groups()
                    deps = re.findall(r'\b([a-zA-Z_]\w*)\b', rval)
                    self._assignments.append({
                        "signal": sig, "rvalue": rval,
                        "type": "always_comb" if in_comb else "always_ff",
                        "line": i, "deps": deps,
                    })


# ─── Balanced Analysis Engine ────────────────────────────────────────

FOR_AGAINST_PROMPT = """\
You are performing root-cause analysis on a formal verification failure.

MANDATORY PROTOCOL: For the suspicious signal below, you MUST write:
  - EXACTLY 2 arguments FOR it being the root cause of the bug
  - EXACTLY 2 arguments AGAINST it being the root cause of the bug

Then give your VERDICT: BUG | NOT_BUG | UNCERTAIN (with confidence 0.0-1.0)

This balanced analysis is REQUIRED to prevent confirmation bias.

SIGNAL UNDER ANALYSIS:
  Name: {signal_name}
  Module: {module}
  Line: {line}
  Expression: {expression}
  Driver type: {driver_type}
  Dependencies: {dependencies}

FAILURE CONTEXT:
  Property: {property_name}
  Failure step: {failure_step}
  Counterexample signals: {cex_signals}

FULL RTL CONTEXT:
```verilog
{rtl_context}
```

Respond in this EXACT format:
FOR_1: <argument>
FOR_2: <argument>
AGAINST_1: <argument>
AGAINST_2: <argument>
VERDICT: <BUG|NOT_BUG|UNCERTAIN>
CONFIDENCE: <0.0 to 1.0>
REASONING: <one-line summary>
"""


class BalancedAnalyzer:
    """
    Implements the mandatory "For-and-Against" protocol for every suspicious signal.
    
    For every signal in the failure's cone of influence:
    1. The LLM MUST produce 2 FOR arguments (why it could be the bug)
    2. The LLM MUST produce 2 AGAINST arguments (why it might NOT be the bug)
    3. Only then: verdict + confidence score
    
    Signals are ranked by confidence and the highest-confidence BUG verdict
    identifies the root cause.
    """

    def __init__(self, llm, verbose: bool = False):
        from crewai import Agent, Task, Crew
        self.llm = llm
        self.verbose = verbose
        self._Agent = Agent
        self._Task = Task
        self._Crew = Crew

    def analyze_signal(
        self,
        signal_name: str,
        graph: CausalGraph,
        failure: FormalFailure,
        rtl_code: str,
    ) -> SuspiciousSignal:
        """
        Run balanced for-and-against analysis on a single signal.
        """
        node = graph.nodes.get(signal_name)
        if not node:
            return SuspiciousSignal(
                name=signal_name, module="", line=0,
                verdict="UNCERTAIN", confidence=0.0,
            )

        # Build context: extract surrounding lines from RTL
        rtl_lines = rtl_code.split("\n")
        ctx_start = max(0, node.source_line - 6)
        ctx_end = min(len(rtl_lines), node.source_line + 5)
        rtl_context = "\n".join(rtl_lines[ctx_start:ctx_end])

        prompt = FOR_AGAINST_PROMPT.format(
            signal_name=signal_name,
            module="top",
            line=node.source_line,
            expression=f"{signal_name} driven by {node.driver_type}",
            driver_type=node.driver_type,
            dependencies=", ".join(node.dependencies),
            property_name=failure.property_name,
            failure_step=failure.failing_step,
            cex_signals=", ".join(failure.signals_in_cex[:20]),
            rtl_context=rtl_context,
        )

        agent = self._Agent(
            role="Formal Verification Debugger",
            goal=f"Analyze signal '{signal_name}' for root-cause determination",
            backstory=(
                "You are a senior formal verification engineer. You ALWAYS perform "
                "balanced analysis: 2 FOR + 2 AGAINST arguments before any verdict. "
                "This prevents confirmation bias in root-cause identification."
            ),
            llm=self.llm,
            verbose=self.verbose,
        )

        task = self._Task(
            description=prompt,
            expected_output="FOR_1, FOR_2, AGAINST_1, AGAINST_2, VERDICT, CONFIDENCE, REASONING",
            agent=agent,
        )

        try:
            raw = str(self._Crew(agents=[agent], tasks=[task]).kickoff())
            return self._parse_analysis(raw, signal_name, node)
        except Exception as e:
            logger.warning(f"[BalancedAnalyzer] Analysis failed for {signal_name}: {e}")
            return SuspiciousSignal(
                name=signal_name, module="", line=node.source_line,
                expression=f"{node.driver_type} at line {node.source_line}",
                verdict="UNCERTAIN", confidence=0.0,
            )

    def _parse_analysis(
        self, raw: str, signal_name: str, node: CausalGraphNode
    ) -> SuspiciousSignal:
        """Parse the LLM's balanced analysis response."""
        result = SuspiciousSignal(
            name=signal_name,
            module="top",
            line=node.source_line,
            expression=f"{node.driver_type} at line {node.source_line}",
        )

        # Extract FOR arguments
        for i in (1, 2):
            m = re.search(rf'FOR_{i}\s*:\s*(.+?)(?:\n|$)', raw, re.IGNORECASE)
            if m:
                result.for_arguments.append(
                    ForAgainstArgument(stance="FOR", reasoning=m.group(1).strip())
                )

        # Extract AGAINST arguments
        for i in (1, 2):
            m = re.search(rf'AGAINST_{i}\s*:\s*(.+?)(?:\n|$)', raw, re.IGNORECASE)
            if m:
                result.against_arguments.append(
                    ForAgainstArgument(stance="AGAINST", reasoning=m.group(1).strip())
                )

        # Extract verdict
        m = re.search(r'VERDICT\s*:\s*(BUG|NOT_BUG|UNCERTAIN)', raw, re.IGNORECASE)
        if m:
            result.verdict = m.group(1).upper()
        else:
            result.verdict = "UNCERTAIN"

        # Extract confidence
        m = re.search(r'CONFIDENCE\s*:\s*([\d.]+)', raw, re.IGNORECASE)
        if m:
            try:
                result.confidence = float(m.group(1))
            except ValueError:
                result.confidence = 0.5
        else:
            result.confidence = 0.5

        # Validate: enforce mandatory 2+2 rule
        if len(result.for_arguments) < 2:
            logger.warning(
                f"[BalancedAnalyzer] Signal '{signal_name}': only {len(result.for_arguments)} "
                "FOR arguments (2 required) β€” reducing confidence"
            )
            result.confidence *= 0.5
        if len(result.against_arguments) < 2:
            logger.warning(
                f"[BalancedAnalyzer] Signal '{signal_name}': only {len(result.against_arguments)} "
                "AGAINST arguments (2 required) β€” reducing confidence"
            )
            result.confidence *= 0.5

        return result


# ─── Deep Debugger Module ────────────────────────────────────────────

class DeepDebuggerModule:
    """
    FVDebug: Formal Verification Deep Debugger with Balanced Analysis.
    
    Pipeline:
        1. Run SymbiYosys formal verification
        2. Parse failure β†’ build Causal Graph
        3. Identify suspicious signals (cone of influence)
        4. For each suspicious signal: mandatory For-and-Against analysis
        5. Rank signals by confidence β†’ identify root cause
        6. Generate precise fix prompt
    """

    def __init__(self, llm, sby_bin: str = "sby", yosys_bin: str = "yosys",
                 verbose: bool = False, max_signals_to_analyze: int = 5):
        self.llm = llm
        self.verbose = verbose
        self.max_signals = max_signals_to_analyze
        self.sby_runner = SymbiYosysRunner(sby_bin, yosys_bin)
        self.graph_builder = CausalGraphBuilder()
        self.analyzer = BalancedAnalyzer(llm, verbose)

    def debug_formal_failure(
        self,
        rtl_path: str,
        sby_config_path: str,
        design_name: str,
        rtl_code: str = "",
    ) -> Optional[DebugVerdict]:
        """
        Full FVDebug pipeline: formal check β†’ causal graph β†’ balanced analysis β†’ verdict.
        
        Args:
            rtl_path:        Path to the RTL source file
            sby_config_path: Path to the .sby configuration
            design_name:     Top module name
            rtl_code:        RTL source code (read from file if empty)
        
        Returns:
            DebugVerdict with root cause, fix, and full balanced analysis log.
        """
        logger.info(f"[DeepDebugger] Starting FVDebug pipeline for {design_name}")

        # Load RTL if not provided
        if not rtl_code and os.path.exists(rtl_path):
            with open(rtl_path, "r") as f:
                rtl_code = f.read()

        # Step 1: Run formal verification
        logger.info("[DeepDebugger] Step 1: Running SymbiYosys formal checks")
        failure = self.sby_runner.run_formal(sby_config_path)
        
        if failure.status == "PASS":
            logger.info("[DeepDebugger] All formal properties passed!")
            return None  # No debugging needed
        
        if failure.status == "ERROR":
            logger.error(f"[DeepDebugger] SymbiYosys error: {failure.error_message}")
            return None

        # Step 2: Build causal graph
        logger.info("[DeepDebugger] Step 2: Building causal graph")
        graph = self.graph_builder.build(rtl_path, failure)

        # Step 3: Identify suspicious signals (cone of influence)
        logger.info("[DeepDebugger] Step 3: Identifying suspicious signals")
        if failure.property_name and failure.property_name in graph.nodes:
            coi = graph.get_cone_of_influence(failure.property_name)
        elif failure.signals_in_cex:
            # Use CEX signals as starting points
            coi = set()
            for sig in failure.signals_in_cex[:5]:
                if sig in graph.nodes:
                    coi.update(graph.get_cone_of_influence(sig))
            coi = list(coi)
        else:
            # Fallback: analyze all signals
            coi = list(graph.nodes.keys())

        # Filter to most relevant signals
        coi = [s for s in coi if s not in ("clk", "rst_n", "reset")]
        coi = coi[:self.max_signals]

        # Step 4: Balanced For-and-Against analysis for each signal
        logger.info(f"[DeepDebugger] Step 4: Balanced analysis on {len(coi)} signals")
        suspicious: List[SuspiciousSignal] = []
        analysis_log_parts: List[str] = []

        for sig_name in coi:
            logger.info(f"[DeepDebugger] Analyzing signal: {sig_name}")
            ss = self.analyzer.analyze_signal(sig_name, graph, failure, rtl_code)
            suspicious.append(ss)
            
            # Build audit log
            analysis_log_parts.append(f"\n--- Signal: {sig_name} (line {ss.line}) ---")
            for fa in ss.for_arguments:
                analysis_log_parts.append(f"  FOR:     {fa.reasoning}")
            for fa in ss.against_arguments:
                analysis_log_parts.append(f"  AGAINST: {fa.reasoning}")
            analysis_log_parts.append(f"  VERDICT: {ss.verdict} (confidence: {ss.confidence:.2f})")

        # Step 5: Rank and select root cause
        bugs = [s for s in suspicious if s.verdict == "BUG"]
        bugs.sort(key=lambda s: s.confidence, reverse=True)

        if bugs:
            root = bugs[0]
            fix_desc = (
                f"Signal '{root.name}' at line {root.line} is the most likely root cause "
                f"(confidence: {root.confidence:.2f}). "
                f"FOR: {'; '.join(a.reasoning for a in root.for_arguments)}. "
                f"Fix the {root.expression}."
            )
        else:
            # No confident BUG verdict β€” use highest confidence UNCERTAIN
            suspicious.sort(key=lambda s: s.confidence, reverse=True)
            root = suspicious[0] if suspicious else SuspiciousSignal(
                name="unknown", module="", line=0, verdict="UNCERTAIN"
            )
            fix_desc = (
                f"No clear root cause identified. Most suspicious: '{root.name}' "
                f"at line {root.line} (confidence: {root.confidence:.2f}). "
                "Manual review recommended."
            )

        return DebugVerdict(
            root_cause_signal=root.name,
            root_cause_line=root.line,
            root_cause_file=rtl_path,
            fix_description=fix_desc,
            confidence=root.confidence,
            causal_graph=graph,
            suspicious_signals=suspicious,
            balanced_analysis_log="\n".join(analysis_log_parts),
        )

    def generate_fix_prompt(self, verdict: DebugVerdict, rtl_code: str) -> str:
        """
        Generate a precise LLM fix prompt from the debug verdict.
        
        Unlike generic "fix the formal error" prompts, this includes:
        - The exact root-cause signal and line
        - The balanced analysis reasoning
        - The causal dependency chain
        """
        parts = [
            "# FORMAL VERIFICATION DEBUG FIX REQUEST",
            "",
            "## Root Cause (from FVDebug balanced analysis)",
            f"Signal: {verdict.root_cause_signal}",
            f"File: {verdict.root_cause_file}:{verdict.root_cause_line}",
            f"Confidence: {verdict.confidence:.2f}",
            f"Description: {verdict.fix_description}",
            "",
            "## Balanced Analysis Log",
            verdict.balanced_analysis_log,
            "",
            "## Causal Graph (Mermaid)",
            "```mermaid",
            verdict.causal_graph.to_mermaid(),
            "```",
            "",
            "## Instructions",
            f"1. Fix signal '{verdict.root_cause_signal}' at line {verdict.root_cause_line}.",
            "2. Ensure the fix satisfies ALL formal properties.",
            "3. Do NOT break existing passing properties.",
            "4. Return ONLY corrected Verilog inside ```verilog fences.",
            "",
            "## Current RTL",
            "```verilog",
            rtl_code,
            "```",
        ]
        return "\n".join(parts)

    def debug_from_existing_failure(
        self,
        rtl_path: str,
        failure: FormalFailure,
        rtl_code: str = "",
    ) -> Optional[DebugVerdict]:
        """
        Run balanced analysis on an already-parsed formal failure.
        
        Use this when SymbiYosys has already been run and you have the
        FormalFailure data β€” skips re-running sby.
        """
        if not rtl_code and os.path.exists(rtl_path):
            with open(rtl_path, "r") as f:
                rtl_code = f.read()

        graph = self.graph_builder.build(rtl_path, failure)
        
        coi = list(graph.nodes.keys())
        coi = [s for s in coi if s not in ("clk", "rst_n", "reset")]
        coi = coi[:self.max_signals]

        suspicious: List[SuspiciousSignal] = []
        log_parts: List[str] = []

        for sig in coi:
            ss = self.analyzer.analyze_signal(sig, graph, failure, rtl_code)
            suspicious.append(ss)
            log_parts.append(f"Signal {sig}: {ss.verdict} ({ss.confidence:.2f})")

        bugs = sorted([s for s in suspicious if s.verdict == "BUG"],
                       key=lambda s: s.confidence, reverse=True)
        root = bugs[0] if bugs else (suspicious[0] if suspicious else
                                      SuspiciousSignal(name="unknown", module="", line=0))

        return DebugVerdict(
            root_cause_signal=root.name,
            root_cause_line=root.line,
            root_cause_file=rtl_path,
            fix_description=f"Root cause: {root.name} at line {root.line}",
            confidence=root.confidence,
            causal_graph=graph,
            suspicious_signals=suspicious,
            balanced_analysis_log="\n".join(log_parts),
        )