Skip to content

bjwanneng/veriflow-cc / src/claude_skills/vf-rtl

vf-rtl

Verified

FileKindSize
SELF_IMPROVE.mdmarkdown3.8 KB
SKILL.mdskill md11.2 KB
SKILL_INDEX.mdmarkdown5.5 KB
analysis/bug_pattern_match.pyscript26.9 KB
analysis/corner_case_generator.pyscript4.9 KB
analysis/coverage_analyzer.pyscript5.3 KB
analysis/design_graph.pyscript8.9 KB
analysis/expected_trace_gen.pyscript12.5 KB
analysis/timing_contract_checker.pyscript25.4 KB
analysis/timing_diagnostic.pyscript21.1 KB
analysis/vcd2table.pyscript31.3 KB
bug_patterns.mdmarkdown23.2 KB
coding_style.mdmarkdown9.4 KB
core/init.pyscript13.8 KB
core/rtl_utils.pyscript7.5 KB
core/state.pyscript38.9 KB
design_rules.mdmarkdown2.9 KB
error_recovery.mdmarkdown7.0 KB
kb/knowledge_base.pyscript11.7 KB
kb/reference_kb.pyscript5.6 KB
kb/self_improve.pyscript24.4 KB
references/arbiter_round_robin.vother1.4 KB
references/counter.vother545 B
references/fifo.vother1.3 KB
references/fsm_moore.vother847 B
references/handshake_valid_ready.vother1.1 KB
references/pipeline_aligner.vother760 B
runners/benchmark_runner.pyscript23.6 KB
runners/cocotb_runner.pyscript26.3 KB
runners/iverilog_runner.pyscript26.3 KB
runners/yosys_equiv.pyscript14.2 KB
stage1_spec_golden.mdmarkdown6.1 KB
stage2_codegen.mdmarkdown5.9 KB
stage3_verify_fix.mdmarkdown17.0 KB
stage4_lint_synth.mdmarkdown6.3 KB
templates/cocotb_template.pyscript42.4 KB
templates/golden_model_template.pyscript8.7 KB
templates/spec_template.jsonconfig8.5 KB
templates/tb_integration_template.vother12.4 KB
verify/candidate_selector.pyscript7.0 KB
verify/formal_prove.pyscript12.1 KB
verify/synth_score.pyscript4.4 KB

runners/iverilog_runner.py

View on GitHub
#!/usr/bin/env python3
"""Pure-Verilog simulation runner for VeriFlow-CC pipeline.

Runs iverilog + vvp for a self-checking Verilog testbench. No cocotb dependency.

Usage:
    python3 iverilog_runner.py \\
        --rtl-dir     <path/to/workspace/rtl> \\
        --tb-file     <path/to/workspace/tb/tb_design.v> \\
        --module      <module_name> \\
        --build-dir   <path/to/build_dir> \\
        [--verbose]

Exit codes:
    0 — all tests passed (ALL TESTS PASSED in output)
    1 — one or more tests failed
    2 — environment error (iverilog not found, etc.)

Output (stdout):
    JSON object: {"tests": N, "passed": M, "failed": F, "vcd_path": "..."}
"""

from __future__ import annotations

import argparse
import json
import os
import re
import subprocess
import sys
import time
from pathlib import Path

# iverilog_runner is subprocessed (via eda_env in prod, directly in some tests);
# make cross-subdir imports (rtl_utils lives in core/) resolve on direct
# invocation. No-op when the dirs are already on sys.path.
_SKILL_ROOT = Path(__file__).resolve().parent
while not (_SKILL_ROOT / "SKILL.md").exists() and _SKILL_ROOT.parent != _SKILL_ROOT:
    _SKILL_ROOT = _SKILL_ROOT.parent
for _d in [*_SKILL_ROOT.iterdir(), _SKILL_ROOT]:
    if _d.is_dir() and _d.name not in {"templates", "references", "docs", "__pycache__"} \
            and str(_d) not in sys.path:
        sys.path.insert(0, str(_d))

from rtl_utils import find_executable, collect_rtl_sources, DIVERGENCE_SEARCH_WINDOW  # noqa: E402


def find_iverilog() -> str:
    return find_executable(["iverilog", "iverilog.exe"])


def find_vvp() -> str:
    return find_executable(["vvp", "vvp.exe"])


def golden_check(golden_path: str, verbose: bool = False) -> dict:
    """Run golden_model.py standalone and verify all test vectors pass.

    This runs BEFORE simulation to confirm the reference model is correct.
    If the golden model itself has bugs, any RTL comparison is meaningless.
    """
    golden_path = str(Path(golden_path).resolve())
    if not Path(golden_path).exists():
        return {"error": f"Golden model not found: {golden_path}", "passed": False}

    try:
        result = subprocess.run(
            [sys.executable, golden_path],
            capture_output=True, text=True, timeout=30,
            cwd=str(Path(golden_path).parent),
        )
        output = result.stdout + result.stderr
    except subprocess.TimeoutExpired:
        return {"error": "Golden model timed out after 30s", "passed": False}
    except Exception as e:
        return {"error": f"Golden model execution error: {e}", "passed": False}

    # Check for [PASS] and [FAIL] markers
    pass_lines = [line for line in output.splitlines() if "[PASS]" in line]
    fail_lines = [line for line in output.splitlines() if "[FAIL]" in line]

    if verbose:
        print(f"[golden_check] Output:\n{output[:3000]}", file=sys.stderr)

    if fail_lines:
        return {
            "passed": False,
            "test_count": len(pass_lines) + len(fail_lines),
            "pass_count": len(pass_lines),
            "fail_count": len(fail_lines),
            "failures": fail_lines[:10],
            "output": output[:2000],
        }

    if result.returncode != 0:
        return {
            "passed": False,
            "error": f"Golden model exited with code {result.returncode}",
            "output": output[:2000],
        }

    # Exit 0 with NO [PASS] marker means the golden model produced no verifiable
    # result (broken/empty/stub, or it wrote to a file instead of stdout). Treat
    # this as a failure so a bad reference model can't pass the Stage-1 gate and
    # then mask RTL bugs during A/B/D classification (which trusts golden values).
    if not pass_lines:
        return {
            "passed": False,
            "error": "Golden model produced no [PASS] markers — output is empty or unverified",
            "test_count": 0,
            "pass_count": 0,
            "fail_count": 0,
            "output": output[:2000],
        }

    return {
        "passed": True,
        "test_count": len(pass_lines),
        "pass_count": len(pass_lines),
        "fail_count": 0,
    }


# Permissive key=value parser. The previous fixed-order regex broke whenever
# the testbench omitted a field or reordered them. We now tolerate any
# subset/ordering of {test, vector, cycle, signal, expected, actual, phase}.
#
# Two flavors of value are accepted:
#   - "quoted strings" — may embed spaces; captured without the quotes
#   - bare tokens — non-greedy run of non-space chars terminated by
#     either whitespace, end-of-string, OR the next `<word>=` pair. The
#     `\w+=` lookahead is what protects against TB lines that forget the
#     space between kv pairs (e.g. `cycle=5signal=data_out`).
_FAIL_KV_PATTERN = re.compile(r'(\w+)=(?:"([^"]*)"|(\S+?))(?=\s|$|\w+=)')

_INT_FIELDS = {"vector", "cycle"}


def _parse_fail_line(fl: str) -> dict:
    """Parse a [FAIL] line into a failure dict.

    Returns at minimum {"test": ..., "message": fl}; any of vector, cycle,
    signal, expected, actual, phase are added when present in the line.
    """
    result: dict = {"test": "verilog_tb", "message": fl}
    for k, quoted, bare in _FAIL_KV_PATTERN.findall(fl):
        v = quoted if quoted else bare
        if k in _INT_FIELDS:
            try:
                result[k] = int(v)
            except ValueError:
                result[k] = v
        else:
            result[k] = v
    return result


def _normalize_value(val: str) -> int | None:
    """Normalize a hex/decimal value string to int. Returns None for unknowns."""
    if not val:
        return None
    val = str(val).strip().lower().replace("_", "")
    if val.startswith("0x"):
        digits = val[2:]
        if any(c in digits for c in "xz"):
            return None
        try:
            return int(digits, 16)
        except ValueError:
            return None
    if any(c in val for c in "xz"):
        return None
    try:
        return int(val)
    except ValueError:
        pass
    # Bare hex without 0x prefix (e.g. a TB printing expected=1f). Only treat
    # as hex when every char is a hex digit — otherwise a non-numeric token
    # like a signal mnemonic would be mis-parsed.
    if val and all(c in "0123456789abcdef" for c in val):
        try:
            return int(val, 16)
        except ValueError:
            return None
    return None


def _is_unknown(val: str) -> bool:
    """Check if a value string contains x/z unknown bits (not hex prefix)."""
    val = str(val).lower().replace("0x", "")
    return any(c in val for c in "xz")


# Shared divergence search window — imported from rtl_utils so both
# iverilog_runner and timing_diagnostic read the same value.
_DIVERGENCE_SEARCH_WINDOW = DIVERGENCE_SEARCH_WINDOW


def _find_value_in_golden(
    golden_cycles: dict[int, dict[str, str]],
    signal: str,
    actual_raw: str,
    expected_cycle: int,
    search_window: int = _DIVERGENCE_SEARCH_WINDOW,
) -> int | None:
    """Search golden trace for a cycle where signal matches actual value."""
    actual_norm = _normalize_value(actual_raw)
    if actual_norm is None:
        return None
    for offset in range(-search_window, search_window + 1):
        if offset == 0:
            continue
        check_cycle = expected_cycle + offset
        if check_cycle < 0:
            continue
        golden_at_cycle = golden_cycles.get(check_cycle, {})
        golden_val = golden_at_cycle.get(signal)
        if golden_val is not None:
            golden_norm = _normalize_value(str(golden_val))
            if golden_norm == actual_norm:
                return check_cycle
    return None


def classify_failure(
    failures: list[dict],
    golden_cycles: dict[int, dict[str, str]] | None = None,
) -> list[dict]:
    """Classify each failure into A/B/D types with reasoning.

    Classification:
        D (Initialization): RTL value is x/z, or expected=0 but actual≠0
        B (Timing):         Correct value at wrong cycle (golden trace match)
        A (Computation):    Default — value mismatch, no timing alignment
    """
    results = []
    for f in failures:
        signal = f.get("signal", "")
        expected_raw = f.get("expected", "")
        actual_raw = f.get("actual", "")
        cycle = f.get("cycle")

        expected_norm = _normalize_value(expected_raw)
        actual_norm = _normalize_value(actual_raw)

        cls = "A"
        reasoning = "Computation error — trace datapath logic"

        if _is_unknown(actual_raw):
            cls = "D"
            reasoning = "RTL value is x/z/unknown — register not initialized or undriven"
        elif expected_norm is not None and expected_norm == 0 and actual_norm not in (None, 0):
            cls = "D"
            reasoning = f"Expected 0 but got {actual_raw} — register not cleared or spurious enable"
        elif golden_cycles is not None and cycle is not None:
            found_at = _find_value_in_golden(golden_cycles, signal, actual_raw, cycle)
            if found_at is not None:
                cls = "B"
                reasoning = (f"Value {actual_raw} matches golden at cycle {found_at}, "
                             f"not at cycle {cycle} — pipeline alignment issue")

        result = dict(f)
        result["classification"] = cls
        result["reasoning"] = reasoning
        results.append(result)
    return results


def _resolve_sim_timeout(
    cli_value: int | None,
    spec_path: str | None,
    rtl_dir: Path,
    default: int = 120,
) -> int:
    """Resolve the simulation timeout.

    Priority: --sim-timeout > spec.json constraints.verification.sim_timeout_s
              > default (120 s).

    spec.json is searched at:
      1. --spec-path (if given)
      2. <rtl_dir>/../docs/spec.json (project layout convention)
    """
    if cli_value is not None and cli_value > 0:
        return cli_value

    candidates = []
    if spec_path:
        candidates.append(Path(spec_path))
    candidates.append(rtl_dir.parent / "docs" / "spec.json")

    for spec_p in candidates:
        try:
            if spec_p.exists():
                spec = json.loads(spec_p.read_text(encoding="utf-8"))
                t = (
                    spec.get("constraints", {})
                        .get("verification", {})
                        .get("sim_timeout_s")
                )
                if isinstance(t, (int, float)) and t > 0:
                    return int(t)
        except (json.JSONDecodeError, OSError):
            continue

    return default


def _build_diff_summary(
    classified_failures: list[dict],
    num_tests: int,
    num_passed: int,
    num_failed: int,
) -> str:
    """Build a human-readable diff summary for appending to sim.log."""
    lines = [
        "",
        "=" * 60,
        "SIMULATION DIFF SUMMARY",
        f"Tests: {num_passed}/{num_tests} passed, {num_failed} failed",
        "=" * 60,
    ]
    for i, f in enumerate(classified_failures[:10]):
        cls = f.get("classification", "?")
        signal = f.get("signal", "?")
        cycle = f.get("cycle", "?")
        expected = f.get("expected", "?")
        actual = f.get("actual", "?")
        reasoning = f.get("reasoning", "")
        lines.append(
            f"  [{cls}] #{i+1} cycle={cycle} signal={signal} "
            f"expected={expected} actual={actual}"
        )
        if reasoning:
            lines.append(f"       {reasoning}")
    if len(classified_failures) > 10:
        lines.append(f"  ... and {len(classified_failures) - 10} more failure(s)")
    lines.append("=" * 60)
    lines.append("")
    return "\n".join(lines)


def _build_compile_cmd(iverilog_exe, output_vvp, tb_stem, rtl_sources, tb_file,
                       no_vcd=False):
    """Build the iverilog compile command.

    -g2005 keeps Verilog-2005 compatibility; -DCOCOTB_SIM=0 marks the pure-Verilog
    path. When no_vcd is set, -DNODUMP is passed so a testbench that gates
    $dumpvars behind `ifndef NODUMP` skips waveform generation.
    """
    cmd = [iverilog_exe, "-g2005", "-o", output_vvp, "-s", tb_stem, "-DCOCOTB_SIM=0"]
    if no_vcd:
        cmd.append("-DNODUMP")
    cmd.extend(rtl_sources)
    cmd.append(str(tb_file))
    return cmd


def _syntax_check(rtl_sources, tb_file, iverilog_exe, sim_env, build_dir):
    """Cheap syntax/wiring gate: iverilog -Wall -tnull (parse+elaborate, no vvp).

    Surfaces syntax errors and common wiring issues (e.g. default_nettype/
    forward-declaration problems) in seconds. Returns (ok, stderr_snippet).
    Never raises — a failed invocation is reported as (False, reason).
    """
    cmd = [iverilog_exe, "-Wall", "-tnull", "-g2005", "-DCOCOTB_SIM=0"]
    cmd.extend(rtl_sources)
    cmd.append(str(tb_file))
    try:
        r = subprocess.run(cmd, capture_output=True, text=True,
                           cwd=str(build_dir), env=sim_env)
    except Exception as e:
        return (False, f"syntax-check invocation failed: {e}")
    if r.returncode == 0:
        return (True, "")
    return (False, (r.stderr or r.stdout)[:2000])


def main():
    parser = argparse.ArgumentParser(
        description="Pure-Verilog simulation runner for VeriFlow-CC pipeline"
    )
    parser.add_argument("--rtl-dir", required=False, default=None,
                        help="Directory containing *.v RTL source files")
    parser.add_argument("--tb-file", required=False, default=None,
                        help="Path to Verilog testbench file (tb_<design>.v)")
    parser.add_argument("--module", required=False, default=None,
                        help="Verilog top-level module name (for reference)")
    parser.add_argument("--build-dir", required=False, default=None,
                        help="Build directory for compilation artifacts")
    parser.add_argument("--no-vcd", action="store_true",
                        help="Disable VCD waveform dump")
    parser.add_argument("--no-syntax-check", action="store_true",
                        help="Skip the iverilog -Wall -tnull syntax gate (default: run)")
    parser.add_argument("--verbose", action="store_true",
                        help="Print detailed output")
    parser.add_argument("--save-raw-log",
                        help="Save raw simulation output to this file path")
    parser.add_argument("--golden-check",
                        help="Run golden model self-check (path to golden_model.py)")
    parser.add_argument("--sim-timeout", type=int, default=None,
                        help="vvp simulation timeout in seconds. If omitted, "
                             "reads spec.json constraints.verification.sim_timeout_s "
                             "(searched relative to --rtl-dir/..), else falls back to 120.")
    parser.add_argument("--spec-path", default=None,
                        help="Optional spec.json path used to resolve --sim-timeout "
                             "when the flag itself is omitted.")
    parser.add_argument("--golden-model", default=None,
                        help="Path to golden_model.py for coverage analysis. "
                             "If provided, the runner compares exercised signals "
                             "against golden trace signals and reports coverage ratio.")
    args = parser.parse_args()

    if args.golden_check:
        result = golden_check(args.golden_check, verbose=args.verbose)
        print(json.dumps(result, indent=2))
        sys.exit(0 if result.get("passed") else 1)

    if not args.rtl_dir or not args.tb_file or not args.module or not args.build_dir:
        parser.error("--rtl-dir, --tb-file, --module, --build-dir required "
                     "when not using --golden-check")

    rtl_dir = Path(args.rtl_dir).resolve()
    tb_file = Path(args.tb_file).resolve()
    build_dir = Path(args.build_dir).resolve()

    # Validate inputs
    if not tb_file.exists():
        print(json.dumps({
            "tests": 0, "passed": 0, "failed": 0,
            "error": f"Testbench file not found: {tb_file}"
        }))
        sys.exit(2)

    build_dir.mkdir(parents=True, exist_ok=True)

    # Clean stale VCD files from previous runs to avoid mtime-based picker
    # picking a stale dump when the current run fails to produce one.
    for stale_vcd in build_dir.glob("*.vcd"):
        try:
            stale_vcd.unlink()
        except OSError as e:
            # Best-effort cleanup; downstream `ls -t` picks the newest. We
            # still surface the failure so a locked-file or perm-denied
            # situation isn't invisible when debugging stale VCDs.
            print(f"[iverilog_runner] could not remove stale {stale_vcd.name}: {e}",
                  file=sys.stderr)

    # Find executables
    iverilog_exe = find_iverilog()
    vvp_exe = find_vvp()

    if not iverilog_exe:
        print(json.dumps({
            "tests": 0, "passed": 0, "failed": 0,
            "error": "iverilog not found. Install Icarus Verilog or set EDA_BIN."
        }))
        sys.exit(2)

    if not vvp_exe:
        print(json.dumps({
            "tests": 0, "passed": 0, "failed": 0,
            "error": "vvp not found. Install Icarus Verilog or set EDA_BIN."
        }))
        sys.exit(2)

    # Collect RTL sources
    rtl_sources = collect_rtl_sources(rtl_dir)

    if args.verbose:
        print(f"[iverilog_runner] iverilog  : {iverilog_exe}", file=sys.stderr)
        print(f"[iverilog_runner] vvp       : {vvp_exe}", file=sys.stderr)
        print(f"[iverilog_runner] RTL dir   : {rtl_dir}", file=sys.stderr)
        print(f"[iverilog_runner] RTL files : {len(rtl_sources)}", file=sys.stderr)
        print(f"[iverilog_runner] TB file   : {tb_file}", file=sys.stderr)
        print(f"[iverilog_runner] Module    : {args.module}", file=sys.stderr)
        print(f"[iverilog_runner] Build dir : {build_dir}", file=sys.stderr)

    # Ensure EDA binary/lib paths are in PATH for DLL resolution on Windows.
    # eda_env.sh sets these in the shell, but subprocess needs explicit PATH.
    sim_env = os.environ.copy()
    eda_bin = os.environ.get("EDA_BIN", "")
    eda_lib = os.environ.get("EDA_LIB", "")
    existing_path = sim_env.get("PATH", "")
    existing_dirs = set(existing_path.split(os.pathsep))
    extra = [p for p in [eda_bin, eda_lib] if p and p not in existing_dirs]
    if extra:
        sim_env["PATH"] = os.pathsep.join(extra) + os.pathsep + existing_path

    # Smoke gate: cheap iverilog -Wall -tnull syntax/wiring check before the
    # full compile. Catches errors (default_nettype / forward-decl, typos) in
    # seconds without producing a vvp. Skip with --no-syntax-check.
    if not args.no_syntax_check:
        ok, snippet = _syntax_check(rtl_sources, tb_file, iverilog_exe, sim_env, build_dir)
        if not ok:
            print(json.dumps({
                "tests": 0, "passed": 0, "failed": 0,
                "error": "iverilog syntax check failed (-Wall -tnull)",
                "syntax_stderr": snippet,
            }))
            if args.verbose:
                print("[iverilog_runner] SYNTAX CHECK FAILED:\n" + snippet, file=sys.stderr)
            sys.exit(2)
        if args.verbose:
            print("[iverilog_runner] Syntax check passed.", file=sys.stderr)

    # Compile with iverilog (-g2005 for Verilog-2005 compatibility).
    output_vvp = str(build_dir / f"{args.module}.vvp")
    compile_cmd = _build_compile_cmd(
        iverilog_exe, output_vvp, tb_file.stem, rtl_sources, tb_file,
        no_vcd=args.no_vcd,
    )

    if args.verbose:
        print(f"[iverilog_runner] Compile cmd: {' '.join(compile_cmd)}", file=sys.stderr)

    try:
        _t0 = time.perf_counter()
        result = subprocess.run(
            compile_cmd,
            capture_output=True,
            text=True,
            cwd=str(build_dir),
            env=sim_env,
        )
        if args.verbose:
            print(f"[TIMING] step=iverilog_compile duration={time.perf_counter() - _t0:.2f}s",
                  file=sys.stderr)
        if result.returncode != 0:
            print(json.dumps({
                "tests": 0, "passed": 0, "failed": 0,
                "error": "iverilog compilation failed",
                "compile_stderr": result.stderr[:2000],
                "compile_stdout": result.stdout[:2000],
            }))
            if args.verbose:
                print("[iverilog_runner] COMPILE FAILED:", file=sys.stderr)
                print(result.stderr, file=sys.stderr)
            sys.exit(2)
        if result.stderr.strip() and args.verbose:
            print(f"[iverilog_runner] Compile warnings:\n{result.stderr}", file=sys.stderr)
    except Exception as e:
        print(json.dumps({
            "tests": 0, "passed": 0, "failed": 0,
            "error": f"iverilog execution error: {e}"
        }))
        sys.exit(2)

    if args.verbose:
        print("[iverilog_runner] Compilation successful, running simulation...", file=sys.stderr)

    sim_timeout = _resolve_sim_timeout(args.sim_timeout, args.spec_path, rtl_dir)
    if args.verbose:
        print(f"[iverilog_runner] sim timeout: {sim_timeout}s", file=sys.stderr)

    # Run simulation with vvp
    sim_cmd = [vvp_exe, output_vvp]
    # VCD is generated by $dumpfile/$dumpvars in the testbench itself

    try:
        _t0 = time.perf_counter()
        result = subprocess.run(
            sim_cmd,
            capture_output=True,
            text=True,
            cwd=str(build_dir),
            env=sim_env,
            timeout=sim_timeout,
        )
        if args.verbose:
            print(f"[TIMING] step=iverilog_sim duration={time.perf_counter() - _t0:.2f}s",
                  file=sys.stderr)

        sim_output = result.stdout + result.stderr

        # Save raw simulation output for post-mortem debug
        if args.save_raw_log:
            raw_log_path = Path(args.save_raw_log)
            raw_log_path.parent.mkdir(parents=True, exist_ok=True)
            try:
                raw_log_path.write_text(sim_output, encoding="utf-8")
            except OSError as e:
                print(f"[iverilog_runner] Warning: could not save raw log: {e}", file=sys.stderr)

        if args.verbose:
            print("[iverilog_runner] Simulation output:", file=sys.stderr)
            print(sim_output[:5000], file=sys.stderr)

    except subprocess.TimeoutExpired:
        print(json.dumps({
            "tests": 0, "passed": 0, "failed": 1,
            "error": f"Simulation timed out after {sim_timeout} seconds "
                     f"(override with --sim-timeout or "
                     f"spec.json constraints.verification.sim_timeout_s)"
        }))
        sys.exit(1)
    except Exception as e:
        print(json.dumps({
            "tests": 0, "passed": 0, "failed": 1,
            "error": f"vvp execution error: {e}"
        }))
        sys.exit(1)

    # Parse simulation output
    # Look for: "ALL TESTS PASSED" or "[FAIL]" or "FAILED: N"
    all_passed = "ALL TESTS PASSED" in sim_output
    fail_lines = re.findall(r'\[FAIL\].*', sim_output)
    failed_summary = re.search(r'FAILED:\s*(\d+)\s*assertion', sim_output)
    pass_lines = re.findall(r'\[PASS\].*', sim_output)

    # Coverage analysis: count exercised test vectors from log
    coverage_info = {}
    if args.golden_model:
        try:
            import importlib.util
            spec = importlib.util.spec_from_file_location("golden_model", args.golden_model)
            gm = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(gm)
            tv_count = len(getattr(gm, "TEST_VECTORS", []))
            exercised = len(pass_lines) + len(fail_lines)
            coverage_info = {
                "test_vectors_total": tv_count,
                "test_vectors_exercised": exercised,
                "coverage_ratio": round(exercised / tv_count, 2) if tv_count else None,
            }
        except Exception as e:
            print(f"[iverilog_runner] coverage analysis failed: {e}", file=sys.stderr)

    # Parse structured [FAIL] lines
    failures = [_parse_fail_line(fl) for fl in fail_lines]

    # Extract first failure cycle from parsed data
    first_fail_cycle = None
    for f in failures:
        if "cycle" in f and f["cycle"] is not None:
            first_fail_cycle = f["cycle"]
            break

    num_passed = len(pass_lines)
    num_failed = len(fail_lines)
    if failed_summary:
        num_failed = max(num_failed, int(failed_summary.group(1)))
    num_tests = num_passed + num_failed

    # Find VCD file — prefer module-specific name for parallel-sim safety,
    # fall back to most-recently-modified for backward compatibility.
    vcd_files = list(build_dir.glob("*.vcd"))
    module_vcd = build_dir / f"{args.module}.vcd"
    if module_vcd.exists():
        vcd_path = str(module_vcd)
    elif vcd_files:
        vcd_path = str(max(vcd_files, key=lambda p: p.stat().st_mtime))
    else:
        vcd_path = None

    if num_failed > 0 or not all_passed:
        # Feed the golden trace so classify_failure can detect B-class
        # (timing-offset) bugs — without it every failure silently falls
        # through to A (computation) and the ±window golden search is dead.
        golden_cycles = None
        if args.golden_model:
            try:
                from rtl_utils import load_golden_trace_as_list
                trace = load_golden_trace_as_list(args.golden_model)
                if trace:
                    golden_cycles = {i: row for i, row in enumerate(trace)}
            except Exception as e:
                print(f"[iverilog_runner] golden trace load failed "
                      f"(B-classification disabled): {e}", file=sys.stderr)
        classified = classify_failure(failures, golden_cycles)
        result = {
            "tests": num_tests,
            "passed": num_passed,
            "failed": num_failed,
            "vcd_path": vcd_path,
            "failures": classified,
            "first_fail_cycle": first_fail_cycle,
            "coverage": coverage_info,
        }
        print(json.dumps(result))

        # Append inline diff summary to the raw sim log
        diff_summary = _build_diff_summary(classified, num_tests, num_passed, num_failed)
        if args.save_raw_log:
            raw_log_path = Path(args.save_raw_log)
            if raw_log_path.exists():
                with open(raw_log_path, "a", encoding="utf-8") as f:
                    f.write(diff_summary)
        # Also print summary to stderr for orchestrator visibility
        print(diff_summary, file=sys.stderr)

        sys.exit(1)
    else:
        result = {
            "tests": num_tests,
            "passed": num_passed,
            "failed": 0,
            "vcd_path": vcd_path,
            "coverage": coverage_info,
        }
        print(json.dumps(result))
        sys.exit(0)


if __name__ == "__main__":
    main()
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall