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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""OpenCodeReview β€” AI-powered PR review with human-in-the-loop approval.

Usage
-----
    opencodereview auth login
    opencodereview auth status
    opencodereview auth logout
    opencodereview review [--smoke]
    opencodereview --help
"""

from __future__ import annotations

import logging
import os
import sys
import time
import uuid

import click
from langgraph.types import Command

from backend.integrations.auth import auth as auth_group
from backend.integrations.config import config as config_group
from backend.graph import build_graph
from backend.observability import (
    build_run_metadata,
    enable_langsmith,
    get_langfuse_handler,
    is_langsmith_enabled,
    estimate_groq_cost,
    flush_langfuse,
    format_cost,
    langfuse_trace,
    log_error_to_backends,
    log_langfuse_score,
    _resolve_langfuse_trace_id,
    TokenCostCallback,
)
from backend.state import ChangedFile, ContextChunk, Finding, Severity, Verdict

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s β€” %(message)s",
)
logger = logging.getLogger(__name__)

DB_PATH = "checkpoints.db"

NODE_LABELS: dict[str, str] = {
    "ingestion": "πŸ“₯ Ingestion",
    "retrieval": "πŸ” Retrieval",
    "correctness_review": "βœ… Correctness Review",
    "security_review": "πŸ”’ Security Review",
    "test_coverage_review": "πŸ§ͺ Test Coverage Review",
    "aggregator": "πŸ“Š Aggregation",
}


# ─── Observability initialisation ──────────────────────────────────────────


def _init_observability() -> None:
    """Initialise tracing backends and HTTP cache from env vars / keyring.

    Called once at the start of each ``review`` command.
    """
    from backend.integrations.github_client import _init_cache as _init_http_cache
    _init_http_cache()

    import keyring as _kr
    from backend.integrations.config import SERVICE_NAME as _svc

    # LangSmith: favour env var (check modern LANGSMITH_API_KEY first, then legacy)
    ls_key = (
        os.environ.get("LANGSMITH_API_KEY")
        or os.environ.get("LANGCHAIN_API_KEY")
        or _kr.get_password(_svc, "langsmith_api_key")
    )
    if ls_key:
        ls_project = (
            os.environ.get("LANGSMITH_PROJECT")
            or os.environ.get("LANGCHAIN_PROJECT")
            or _kr.get_password(_svc, "langsmith_project")
            or "opencodereview"
        )
        enable_langsmith(ls_key, ls_project)

    # Langfuse: env var overrides keyring
    for env_key, ring_key in [
        ("LANGFUSE_PUBLIC_KEY", "langfuse_public_key"),
        ("LANGFUSE_SECRET_KEY", "langfuse_secret_key"),
        ("LANGFUSE_HOST", "langfuse_host"),
    ]:
        if not os.environ.get(env_key):
            val = _kr.get_password(_svc, ring_key)
            if val:
                os.environ[env_key] = val

    if is_langsmith_enabled():
        logger.info("Observability: LangSmith enabled")
    if get_langfuse_handler():
        logger.info("Observability: Langfuse enabled")


# ─── Synthetic payload for offline testing ──────────────────────────────────

SYNTHETIC_STATE = {
    "repo": "demo-org/demo-repo",
    "pr_number": 1,
    "diff": (
        "diff --git a/src/auth.py b/src/auth.py\n"
        "index abc123..def456 100644\n"
        "--- a/src/auth.py\n"
        "+++ b/src/auth.py\n"
        "@@ -42,7 +42,9 @@ def login(username, password):\n"
        "     user = db.get_user(username)\n"
        "-    if user.password == password:\n"
        "+    if constant_time_compare(password, user.password):\n"
        "         return create_session(user)\n"
        "     return None\n"
    ),
    "changed_files": [
        ChangedFile(
            path="src/auth.py",
            status="modified",
            content="def login(username, password):\n"
                    "    user = db.get_user(username)\n"
                    "    if constant_time_compare(password, user.password):\n"
                    "        return create_session(user)\n"
                    "    return None\n",
            diff_hunk="@@ -42,7 +42,9 @@ def login(username, password): ...",
        ),
    ],
    "context_chunks": [
        ContextChunk(
            source="codebase", file_path="src/db.py",
            content="def get_user(username): ...",
            relevance_score=0.92,
        ),
    ],
    "findings": [
        Finding(
            file_path="src/auth.py", line_start=42, line_end=50,
            severity=Severity.CRITICAL, category="security",
            comment="Use constant-time comparison for passwords.",
            confidence=0.95,
        ),
    ],
    "verdict": Verdict(
        summary="Critical security vulnerability found.",
        approved=False, overall_score=3.5, critical_count=1,
        high_count=0, recommendation="block",
    ),
}


# ─── Review Command ─────────────────────────────────────────────────────────


@click.command()
@click.option(
    "--smoke", is_flag=True,
    help="Non-interactive smoke test (skips HITL prompt).",
)
@click.option(
    "--repo",
    default="demo-org/demo-repo",
    show_default=True,
    help="GitHub repository (owner/name).",
)
@click.option(
    "--pr", "pr_number",
    default=1,
    show_default=True,
    help="Pull request number.",
)
@click.option(
    "--endpoint", "endpoint_name",
    default="",
    show_default=True,
    help="LLM endpoint: Gemini/Grok (built-ins) or a custom name (see OCR_ENDPOINT_* env vars).",
)
def review(repo: str, pr_number: int, smoke: bool, endpoint_name: str) -> None:
    """Run a PR review with human-in-the-loop approval.

    By default this runs with a synthetic PR payload for demonstration.
    """
    # ── Enable LangSmith if key is available ─────────────────────────
    _init_observability()

    graph = build_graph(DB_PATH)

    try:
        if smoke:
            _test_offline(graph)
        else:
            _run_with_hitl(graph, repo, pr_number, endpoint_name)
    finally:
        # Close the graph's SQLite connection (checkpoints persist to disk,
        # so a later resume can open a fresh one) and clean up the DB file.
        conn = getattr(graph, "_opencodereview_conn", None)
        if conn:
            conn.close()
        _cleanup_db()
        # Ship any queued Langfuse scores before the process exits.
        flush_langfuse()


def _run_with_hitl(graph, repo: str, pr_number: int, endpoint_name: str = "") -> None:
    """Run the graph on a real PR β€” fetches data from GitHub, pauses for
    human approval, then posts findings back as PR comments.

    Parameters
    ----------
    endpoint_name : str
        Optional custom LLM endpoint name (``OCR_ENDPOINT_*``) to run the
        reviewers on.  Empty string uses the built-in default provider.
    """
    thread_id = str(uuid.uuid4())
    trace_name = f"opencodereview/review/{repo}#{pr_number}"
    tags = ["cli", repo]
    prompt_versions = {
        "correctness": "v1",
        "security": "v1",
        "test_coverage": "v1",
        "aggregator": "v1",
    }
    cost_tracker = TokenCostCallback()
    metadata = build_run_metadata(
        source="cli", repo=repo, pr_number=pr_number,
        session_id=thread_id,
        prompt_versions=prompt_versions,
    )
    config: dict = {
        "configurable": {"thread_id": f"hitl-{thread_id}"},
        "metadata": metadata,
    }

    # Use synthetic demo data by default; only fetch real PRs when the
    # user explicitly provides a custom repo or PR number.
    is_default = (repo == "demo-org/demo-repo" and pr_number == 1)
    if is_default:
        logger.info(
            "Using synthetic demo data β€” pass --repo and --pr to "
            "review a real PR from GitHub.\n"
        )
        initial_state = SYNTHETIC_STATE
    else:
        logger.info(
            "Starting review of %s#%d … fetching PR data from GitHub.\n",
            repo, pr_number,
        )
        initial_state = {
            "repo": repo,
            "pr_number": pr_number,
        }
        if endpoint_name:
            initial_state["endpoint"] = endpoint_name

    with langfuse_trace(
        trace_name=trace_name,
        tags=tags,
        session_id=thread_id,
        metadata={
            "source": "cli",
            "repo": repo,
            "pr_number": pr_number,
            "prompt_versions": prompt_versions,
        },
    ) as handler:
        callbacks = [cost_tracker]
        if handler:
            callbacks.append(handler)
        config["callbacks"] = callbacks

        t0 = time.time()
        for event in graph.stream(initial_state, config):
            for node_name in event:
                label = NODE_LABELS.get(node_name, node_name)
                logger.info("  βœ” %s  (+%.1fs)", label, time.time() - t0)
        logger.info("  ─── Pipeline complete in %.1fs ───", time.time() - t0)

        state = graph.get_state(config)
        tasks = state.tasks

        if tasks:
            logger.info("\n" + "=" * 60)
            logger.info("  REVIEW RESULTS β€” HUMAN APPROVAL REQUIRED")
            logger.info("=" * 60)

            if state.values.get("verdict"):
                v = state.values["verdict"]
                logger.info("  Verdict: %s", v.recommendation.upper())
                logger.info("  Score:   %.1f/10", v.overall_score)
                logger.info("  Summary: %s", v.summary)

            final_findings = state.values.get("final_findings", [])
            logger.info("  Findings: %d", len(final_findings))
            for i, f in enumerate(final_findings, 1):
                logger.info(
                    "    %d. [%.0f%%] %s:%d–%d (%s)",
                    i, f.confidence * 100, f.file_path,
                    f.line_start, f.line_end, f.severity.value,
                )
                logger.info("       %s", f.comment[:120])

            # Show cost & TTFT
            logger.info("  %s", cost_tracker.summary())

            # Capture trace_id NOW (immediately after graph completes)
            # so it can be used after the long-running RAGAS computation.
            run_trace_id = _resolve_langfuse_trace_id(handler)

            # Log verdict score to Langfuse and update trace metadata
            if state.values.get("verdict"):
                v = state.values["verdict"]
                log_langfuse_score(
                    name="verdict_score",
                    value=v.overall_score,
                    comment=f"{repo}#{pr_number} β€” {v.recommendation}: {v.summary[:100]}",
                    trace_id=run_trace_id,
                    handler=handler,
                )
                log_langfuse_score(
                    name="findings_count",
                    value=len(final_findings),
                    comment=f"{repo}#{pr_number}",
                    trace_id=run_trace_id,
                    handler=handler,
                )

            # ── Compute and log RAGAS retrieval scores ─────────────────
            context_chunks = state.values.get("context_chunks", [])
            if context_chunks:
                try:
                    from eval_data.ragas_eval import compute_ragas_retrieval_scores

                    query = state.values.get("diff", "") or ""
                    contexts = [c.content for c in context_chunks if c.content]

                    # Build answer text from findings + verdict for
                    # faithfulness & answer_relevancy metrics
                    answer_lines = []
                    for f in final_findings:
                        answer_lines.append(
                            f"[{f.severity.value}] {f.file_path}:{f.line_start}-{f.line_end}: {f.comment}"
                        )
                    if state.values.get("verdict"):
                        v = state.values["verdict"]
                        answer_lines.append(
                            f"Verdict: {v.recommendation} (score={v.overall_score}/10) β€” {v.summary}"
                        )
                    answer_text = "\n".join(answer_lines) if answer_lines else None

                    ragas_scores = compute_ragas_retrieval_scores(
                        query=query,
                        retrieved_contexts=contexts,
                        answer=answer_text,
                        endpoint=endpoint_name or None,
                    )

                    # Log RAGAS scores with the explicitly captured trace_id
                    # to avoid contextvar loss during the long computation.
                    # handler=handler is also passed as fallback resolution.
                    for metric_name, value in ragas_scores.items():
                        if value is None:
                            continue
                        log_langfuse_score(
                            name=f"ragas_{metric_name}",
                            value=value,
                            comment=f"{repo}#{pr_number} β€” {metric_name}",
                            trace_id=run_trace_id,
                            handler=handler,
                        )

                    logger.info(
                        "  RAGAS scores logged to Langfuse: %s",
                        " | ".join(
                            f"{k}={v:.4f}" for k, v in ragas_scores.items()
                            if v is not None
                        ),
                    )
                except ImportError:
                    logger.info("RAGAS not installed β€” skipping RAGAS scoring (pip install ragas)")
                except Exception as exc:
                    logger.warning("RAGAS scoring failed: %s", exc)

            logger.info("=" * 60)

            click.echo()
            click.echo("-" * 50)
            click.echo("  HUMAN-IN-THE-LOOP: Approve this review for posting?")
            click.echo("-" * 50)
            click.echo("  [a] Approve β€” post findings as GitHub comments")
            click.echo("  [r] Reject  β€” discard results, post nothing")
            click.echo("  [q] Quit    β€” leave graph paused")
            click.echo()

            choice = click.prompt("  Your choice", type=click.Choice(["a", "r", "q"], case_sensitive=False))

            if choice == "a":
                logger.info("  β†’ Approved! Resuming graph to post results …")
                graph.invoke(Command(resume={"action": "approve"}), config)
            elif choice == "r":
                logger.info("  β†’ Rejected. Resuming graph without posting …")
                graph.invoke(Command(resume={"action": "reject"}), config)
            else:
                logger.info("  β†’ Exiting. Graph remains paused β€” resume later")
                logger.info(
                    "    Resume with: graph.invoke(Command(resume=...), %s)",
                    config,
                )
                return

            final_state = graph.get_state(config)
            final_values = final_state.values

            logger.info("\n" + "=" * 60)
            logger.info("  FINAL STATE")
            logger.info("=" * 60)
            logger.info("  Human approved: %s", final_values.get("human_approved"))
            if final_values.get("verdict"):
                logger.info("  Verdict: %s", final_values["verdict"].recommendation)
            logger.info("  Final findings: %d", len(final_values.get("final_findings", [])))
            logger.info("  %s", cost_tracker.summary())
            logger.info("=" * 60)
        else:
            logger.info("Graph completed without interrupt (no findings to review?)")
            logger.info("  %s", cost_tracker.summary())


def _test_offline(graph) -> None:
    """Non-interactive test that the graph compiles and runs up to interrupt."""
    thread_id = str(uuid.uuid4())
    prompt_versions = {
        "correctness": "v1",
        "security": "v1",
        "test_coverage": "v1",
        "aggregator": "v1",
    }
    cost_tracker = TokenCostCallback()
    metadata = build_run_metadata(
        source="cli", repo="demo-org/demo-repo", pr_number=1,
        session_id=thread_id,
        prompt_versions=prompt_versions,
    )
    config: dict = {
        "configurable": {"thread_id": f"smoke-{thread_id}"},
        "metadata": metadata,
    }

    with langfuse_trace(
        trace_name="opencodereview/smoke-test",
        tags=["cli", "smoke", "demo-org/demo-repo"],
        session_id=thread_id,
        metadata={"prompt_versions": prompt_versions},
    ) as handler:
        callbacks = [cost_tracker]
        if handler:
            callbacks.append(handler)
        config["callbacks"] = callbacks

        t0 = time.time()
        for event in graph.stream(SYNTHETIC_STATE, config):
            for node_name in event:
                label = NODE_LABELS.get(node_name, node_name)
                logger.info("  βœ” %s  (+%.1fs)", label, time.time() - t0)
        logger.info("  ─── Pipeline complete in %.1fs ───", time.time() - t0)
        state = graph.get_state(config)
        tasks = state.tasks
        values = state.values

        verdict = values.get("verdict")
        final_findings = values.get("final_findings", [])

        logger.info("Smoke test: paused=True, tasks=%d, findings=%d, verdict=%s",
                    len(tasks), len(final_findings),
                    verdict.recommendation if verdict else 'None')
        logger.info("%s", cost_tracker.summary())

        assert len(tasks) > 0, "Graph did not pause at interrupt"
        assert verdict is not None, "No verdict produced"
        assert len(final_findings) >= 0, "Missing final_findings"

        graph.invoke(Command(resume={"action": "reject"}), config)
        logger.info("Smoke test: resume OK, graph completed")


def _cleanup_db() -> None:
    """Remove the checkpoint database after a run."""
    if os.path.exists(DB_PATH):
        try:
            os.remove(DB_PATH)
            logger.info("Cleaned up %s", DB_PATH)
        except PermissionError:
            pass


# ─── Root CLI ───────────────────────────────────────────────────────────────


# ─── Doctor Command ─────────────────────────────────────────────────────────


@click.command()
def doctor() -> None:
    """Run system health checks and report status."""
    from backend.integrations.config import _mask_key
    from backend.observability import (
        HealthStatus,
        check_groq_connectivity,
        check_langfuse_connectivity,
    )

    click.echo()
    click.echo("=" * 50)
    click.echo("  OpenCodeReview β€” Health Check")
    click.echo("=" * 50)
    click.echo()

    # ── Environment ────────────────────────────────────────────────────
    click.echo("πŸ“¦ Environment")
    click.echo(f"  Python:  {sys.version.split()[0]}")
    click.echo()

    # ── API keys ───────────────────────────────────────────────────────
    click.echo("πŸ”‘ API Keys")
    groq_key = os.environ.get("GROQ_API_KEY", "").strip()
    click.echo(f"  GROQ_API_KEY:  {'βœ… Set' if groq_key else '❌ Not set'}")
    gh_token = os.environ.get("GITHUB_TOKEN", "").strip()
    click.echo(f"  GITHUB_TOKEN:  {'βœ… Set' if gh_token else '❌ Not set'}")
    click.echo()

    # ── Observability ──────────────────────────────────────────────────
    click.echo("πŸ”­ Observability")
    h = HealthStatus()
    for comp, status in h.summary().items():
        click.echo(f"  {comp}: {status}")
    click.echo()

    # ── LLM endpoints (built-ins + custom) ───────────────────────────────
    click.echo("πŸ”Œ LLM Endpoints")
    from backend.integrations.endpoints import discover_endpoints

    eps = discover_endpoints()
    if eps:
        for ep in eps:
            click.echo(
                f"  {ep.describe()}"
                + (" βœ…" if ep.api_key else " ❌ missing key")
            )
    else:
        click.echo("  None configured β€” set OCR_ENDPOINT_* env vars to add one")
    click.echo()

    # ── Connectivity tests ─────────────────────────────────────────────
    click.echo("🌐 Connectivity")

    # Groq
    groq_ok, groq_msg = check_groq_connectivity()
    click.echo(f"  Groq API:    {'βœ…' if groq_ok else '❌'} {groq_msg}")

    # Langfuse (only if configured)
    lf_ok, lf_msg = check_langfuse_connectivity()
    click.echo(f"  Langfuse:    {'βœ…' if lf_ok else '⏭️'} {lf_msg}")

    click.echo()
    click.echo("=" * 50)


# ─── Root CLI ───────────────────────────────────────────────────────────────


@click.group()
def cli() -> None:
    """OpenCodeReview β€” AI-powered PR review with human-in-the-loop approval."""


cli.add_command(review)
cli.add_command(doctor)
cli.add_command(auth_group)
cli.add_command(config_group)


def main() -> None:
    cli(auto_envvar_prefix="OCR")


if __name__ == "__main__":
    main()