File size: 3,557 Bytes
e1ced8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""final_verdict node — synthesize compliance analysis + optional peer review into structured verdict."""
from __future__ import annotations

from datetime import datetime

from google import genai
from google.genai import types

from config import GOOGLE_API_KEY, VERDICT_MODEL
from state import AgentMessage, ComplianceState


def final_verdict(state: ComplianceState) -> dict:
    """Produce the final compliance verdict, synthesizing all evidence."""
    question = state["question"]
    compliance_analysis = state.get("compliance_analysis", "")
    reviewer_analysis = state.get("reviewer_analysis", "")
    code_report = state.get("code_report", "")
    enable_consensus = state.get("enable_consensus", False)

    # If no consensus was run, pass through the analyst's determination
    if not enable_consensus or not reviewer_analysis:
        verdict_msg = AgentMessage(
            timestamp=datetime.now().strftime("%H:%M:%S"),
            agent="compliance_analyst",
            action="verdict",
            summary="Final compliance verdict issued.",
            detail=compliance_analysis[:1000],
            evidence_refs=[],
        )
        return {
            "final_verdict": compliance_analysis,
            "discussion_log": [verdict_msg],
            "status_message": ["Final verdict ready."],
        }

    # Synthesize both perspectives
    client = genai.Client(api_key=GOOGLE_API_KEY)

    synthesis_prompt = f"""\

You are producing a FINAL COMPLIANCE VERDICT for a NYC building code review.



USER QUESTION: {question}



CODE REQUIREMENTS (from legal research):

{code_report[:3000]}



ANALYST A (Gemini) compliance findings:

{compliance_analysis}



ANALYST B (GPT) peer review:

{reviewer_analysis}



YOUR TASK:

1. If both analysts AGREE on compliance status: produce a confident, unified verdict.

2. If they PARTIALLY AGREE: produce the verdict based on agreed points, and explicitly \

   note areas of disagreement with evidence from both sides.

3. If they DISAGREE: present both interpretations, explain the discrepancy, and state \

   which determination appears better supported by the evidence.



OUTPUT FORMAT:



### Compliance Verdict

**Status:** Compliant | Non-Compliant | Partially Compliant | Unverifiable



### Legal Basis

For each code requirement checked:

- **[Code Type] SS[Section] — [Title]**

  - Requirement: [specific measurable requirement]

  - Drawing Evidence: [what was observed]

  - Determination: [compliant/non-compliant/unverifiable]



### Key Findings

- Bullet points of the most important compliance determinations



### Analyst Consensus

- Agreement/disagreement between Gemini and GPT analysts

- Resolution of any conflicts



### Limitations

- What could not be verified and why

- Recommended follow-up actions



Always cite BOTH code sections AND image crop labels for every factual claim.

"""

    response = client.models.generate_content(
        model=VERDICT_MODEL,
        contents=[synthesis_prompt],
    )

    verdict_text = response.text

    verdict_msg = AgentMessage(
        timestamp=datetime.now().strftime("%H:%M:%S"),
        agent="compliance_analyst",
        action="verdict",
        summary="Final synthesized compliance verdict issued.",
        detail=verdict_text[:1000],
        evidence_refs=[],
    )

    return {
        "final_verdict": verdict_text,
        "discussion_log": [verdict_msg],
        "status_message": ["Final synthesized verdict ready."],
    }