File size: 27,976 Bytes
5774aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Uplan FastAPI Server
====================
Serves the UI and exposes backend API endpoints.

Routes:
  GET  /                  -> serves ui/index.html
  POST /api/validate      -> quick Flash doc-type check (per upload)
  POST /api/analyze       -> full pipeline with SSE progress stream (legacy)
  POST /api/start         -> start async pipeline, returns task_id immediately
  GET  /api/status/{id}   -> poll task progress + result
  GET  /api/health        -> health check
"""

from __future__ import annotations

import asyncio
import json
import os
import sys
import time
import traceback
import uuid
from pathlib import Path
from typing import AsyncGenerator

import fitz
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel

load_dotenv()

# -- Path setup so local modules are importable --
ROOT = Path(__file__).parent
sys.path.insert(0, str(ROOT))

from config import client, FLASH_MODEL
from encoding.extractor import extract_pages
from encoding.graph_builder import build_graph
from encoding.schema import PageExtraction, PageType
from agents.base_agent import AgentFinding
from agents.financial_agent import FinancialAgent
from agents.identity_agent import IdentityAgent
from agents.temporal_agent import TemporalAgent
from agents.sponsor_agent import SponsorAgent
from agents.enrollment_agent import EnrollmentAgent
from agents.narrative import NarrativeAgent
from rules.engine import run_rules
from google.genai import types

app = FastAPI(title="Uplan API", version="1.0.0")

# CORS for local dev
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# -- Static UI --
UI_DIR = ROOT / "ui"


@app.get("/")
async def serve_ui():
    return FileResponse(UI_DIR / "index.html")


@app.get("/api/health")
async def health():
    return {"status": "ok", "model_flash": os.environ.get("FLASH_MODEL", "gemini-3-flash-preview")}


# ─────────────────────────────────────────────────────────────────
# In-memory Task Store for async polling
# ─────────────────────────────────────────────────────────────────

_tasks: dict[str, dict] = {}

# Auto-purge completed tasks older than 30 minutes
TASK_TTL_SECONDS = 1800


# ─────────────────────────────────────────────────────────────────
# Quick document type validation endpoint
# Called per-upload in Step 2. Returns detected page_type + match.
# ─────────────────────────────────────────────────────────────────

VALIDATE_PROMPT = """You are a document classifier for immigration visa applications.
Look at this document page and classify it. Return ONLY a JSON object with:
{
  "detected_type": "<one of: passport, bank_statement, payslip, tax_return, sponsor_letter, employment_letter, enrollment_letter, affidavit, unknown>",
  "confidence": <0.0-1.0>,
  "reason": "<one sentence explaining the classification>"
}"""

TYPE_DISPLAY = {
    "passport": "Passport",
    "bank_statement": "Bank Statement",
    "payslip": "Payslip",
    "tax_return": "Tax Return",
    "sponsor_letter": "Sponsor Letter",
    "employment_letter": "Employment Letter",
    "enrollment_letter": "Enrollment Letter / COE",
    "affidavit": "Affidavit",
    "unknown": "Unknown",
    "supporting_docs": "Supporting Document",
    "cas_letter": "CAS Letter",
    "i20_letter": "I-20 Form",
    "acceptance_letter": "Acceptance Letter",
    "proof_of_funds": "Proof of Funds",
    "cos_letter": "Certificate of Sponsorship",
    "itinerary": "Travel Itinerary",
    "financial_affidavit": "Financial Affidavit",
}

# Map UI slot types -> acceptable Flash types (some are aliases)
SLOT_TYPE_MAP = {
    "bank_statement": ["bank_statement"],
    "payslip": ["payslip"],
    "passport": ["passport"],
    "enrollment_letter": ["enrollment_letter"],
    "sponsor_letter": ["sponsor_letter", "affidavit"],
    "employment_letter": ["employment_letter"],
    "affidavit": ["affidavit", "sponsor_letter"],
    "financial_affidavit": ["affidavit", "sponsor_letter"],
    "cas_letter": ["enrollment_letter"],
    "i20_letter": ["enrollment_letter"],
    "acceptance_letter": ["enrollment_letter"],
    "cos_letter": ["employment_letter", "sponsor_letter"],
    "itinerary": ["unknown"],  # No strict validation needed
    "supporting_docs": None,   # Accept anything
    "proof_of_funds": ["bank_statement", "affidavit"],
    "tax_return": ["tax_return"],
}


@app.post("/api/validate")
async def validate_document(
    file: UploadFile = File(...),
    expected_type: str = Form(...),
):
    """
    Validate a single uploaded document against its expected type.
    Returns: {match: bool, detected: str, confidence: float, reason: str}
    """
    try:
        file_bytes = await file.read()
        filename = getattr(file, "filename", "").lower()

        if filename.endswith((".png", ".jpg", ".jpeg")):
            img_bytes = file_bytes
            mime_type = "image/jpeg" if filename.endswith((".jpg", ".jpeg")) else "image/png"
        else:
            # Assume PDF and render first page to PNG
            doc = fitz.open(stream=file_bytes, filetype="pdf")
            page = doc[0]
            pix = page.get_pixmap(dpi=100)  # Low DPI for quick validation
            img_bytes = pix.tobytes("png")
            doc.close()
            mime_type = "image/png"

        # Call Flash for type classification
        loop = asyncio.get_event_loop()
        response = await loop.run_in_executor(
            None,
            lambda: client.models.generate_content(
                model=FLASH_MODEL,
                contents=[
                    VALIDATE_PROMPT,
                    types.Part.from_bytes(data=img_bytes, mime_type=mime_type),
                ],
                config={"response_mime_type": "application/json"},
            )
        )

        try:
            result = json.loads(response.text)
            detected = result.get("detected_type", "unknown")
            confidence = float(result.get("confidence", 0.5))
            reason = result.get("reason", "")
        except Exception:
            detected = "unknown"
            confidence = 0.3
            reason = "Classification parsing failed"

        # Check match
        acceptable = SLOT_TYPE_MAP.get(expected_type)
        if acceptable is None:
            # "supporting_docs" β€” accept anything
            match = True
        else:
            match = detected in acceptable

        return JSONResponse({
            "match": match,
            "detected": detected,
            "detected_display": TYPE_DISPLAY.get(detected, detected),
            "expected_display": TYPE_DISPLAY.get(expected_type, expected_type),
            "confidence": confidence,
            "reason": reason,
        })

    except Exception as e:
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"{type(e).__name__}: {str(e)}")


# ─────────────────────────────────────────────────────────────────
# Full pipeline analysis with SSE progress streaming
# ─────────────────────────────────────────────────────────────────

AGENT_CLASSES = [
    ("identity_agent", IdentityAgent),
    ("financial_agent", FinancialAgent),
    ("temporal_agent", TemporalAgent),
    ("sponsor_agent", SponsorAgent),
    ("enrollment_agent", EnrollmentAgent),
]


def _sse(event_dict: dict) -> str:
    """Format a dict as an SSE data line."""
    return f"data: {json.dumps(event_dict, default=str)}\n\n"


async def _run_pipeline(
    pdf_bytes_list: list[bytes],
    context: dict,
    debug: bool = False,
) -> AsyncGenerator[str, None]:
    """
    Async generator that runs the full Uplan pipeline and yields SSE events.
    Each stage yields start + done events. Final event is 'complete' with full result.
    """
    loop = asyncio.get_event_loop()
    inter_agent_delay = float(os.environ.get("INTER_AGENT_DELAY", "1.0"))

    try:
        # ── Stage 1: Structural encoding ──────────────────────────────
        yield _sse({"type": "stage_start", "stage": "encode",
                    "message": "Rendering pages and running Flash extraction..."})
        t0 = time.time()

        all_pages: list[PageExtraction] = []
        for pdf_bytes in pdf_bytes_list:
            pages = await loop.run_in_executor(None, extract_pages, pdf_bytes)
            all_pages.extend(pages)

        graph = await loop.run_in_executor(None, build_graph, all_pages)
        encode_elapsed = time.time() - t0

        yield _sse({
            "type": "stage_done", "stage": "encode",
            "elapsed_s": round(encode_elapsed, 1),
            "data": {
                "page_count": graph.source_page_count,
                "token_count": graph.token_count,
                "raw_tokens": graph.estimated_raw_tokens,
                "doc_types": graph.source_doc_types,
                "compression_pct": round((1 - graph.token_count / max(graph.estimated_raw_tokens, 1)) * 100, 1),
            }
        })

        # ── Stage 2: Privacy purge ────────────────────────────────────
        yield _sse({"type": "stage_start", "stage": "purge",
                    "message": "Purging raw document bytes from memory..."})
        pdf_bytes_list = []  # Purge -- raw bytes no longer referenced
        yield _sse({"type": "stage_done", "stage": "purge", "elapsed_s": 0.0})

        # ── Stage 3: Rule engine ──────────────────────────────────────
        yield _sse({"type": "stage_start", "stage": "rules",
                    "message": "Applying 9 deterministic rules to semantic graph..."})
        t0 = time.time()

        # Build visa context from intake form
        visa_context_dict = None
        try:
            from rules.context_profiles import VisaContext, build_context
            dest = context.get("dest", "")
            visa_type = context.get("type", "student")
            visa_context_dict = build_context(dest, visa_type)
        except Exception:
            pass

        rule_findings_raw = await loop.run_in_executor(
            None,
            lambda: run_rules(graph, context=None)
        )
        rule_findings = [f.model_dump() for f in rule_findings_raw]
        rules_elapsed = time.time() - t0

        yield _sse({
            "type": "stage_done", "stage": "rules",
            "elapsed_s": round(rules_elapsed, 1),
            "data": {"finding_count": len(rule_findings)}
        })

        # ── Stages 4-8: Specialist agents ────────────────────────────
        graph_dict = graph.model_dump()
        agent_findings: list[dict] = []
        completed_agents: list[str] = []

        state = {
            "semantic_graph": graph_dict,
            "rule_findings": rule_findings,
            "agent_findings": [],
            "completed_agents": [],
            "visa_context": visa_context_dict,
            "document_checklist": [],
            "verdict": None,
            "risk_score": None,
            "raw_purged": True,
            "_debug_logger": None,
        }

        for i, (agent_id, agent_cls) in enumerate(AGENT_CLASSES):
            if i > 0:
                await asyncio.sleep(inter_agent_delay)

            yield _sse({"type": "agent_start", "agent": agent_id})
            t0 = time.time()

            try:
                result = await loop.run_in_executor(None, lambda cls=agent_cls, s=state: cls().run(s))
                agent_findings.extend(result.get("agent_findings", []))
                completed_agents.extend(result.get("completed_agents", []))
                state["agent_findings"] = agent_findings
                state["completed_agents"] = completed_agents
                agent_elapsed = time.time() - t0

                # Count anomalies at this agent's findings
                this_agent_findings = result.get("agent_findings", [])
                anomaly_count = sum(len(f.get("anomalies", [])) for f in this_agent_findings)

                yield _sse({
                    "type": "agent_done", "agent": agent_id,
                    "elapsed_s": round(agent_elapsed, 1),
                    "verdict": this_agent_findings[0].get("agent_verdict") if this_agent_findings else None,
                    "anomaly_count": anomaly_count,
                })

            except Exception as e:
                yield _sse({"type": "agent_error", "agent": agent_id, "message": str(e)})
                completed_agents.append(agent_id)

        # ── Stage 9: Narrative synthesis ──────────────────────────────
        await asyncio.sleep(inter_agent_delay)
        yield _sse({"type": "stage_start", "stage": "narrative",
                    "message": "Synthesising cross-document verdict and rebuttal guidance..."})
        t0 = time.time()

        state["agent_findings"] = agent_findings
        narrative_result = await loop.run_in_executor(None, lambda: NarrativeAgent().run(state))
        narrative_elapsed = time.time() - t0

        yield _sse({
            "type": "stage_done", "stage": "narrative",
            "elapsed_s": round(narrative_elapsed, 1),
        })

        # ── Final: assemble result ────────────────────────────────────
        verdict = narrative_result.get("verdict", {})
        risk_score = narrative_result.get("risk_score", 1.0)

        final_result = {
            "verdict": verdict.get("verdict", "FAIL"),
            "risk_score": risk_score,
            "missing_documents": verdict.get("missing_documents", []),
            "rejection_narrative": verdict.get("rejection_narrative", ""),
            "rebuttal_guidance": verdict.get("rebuttal_guidance", []),
            "citations": verdict.get("citations", []),
            "critical_issues": verdict.get("critical_issues", []),
            "warning_issues": verdict.get("warning_issues", []),
            "agent_findings": agent_findings,
            "rule_findings": rule_findings,
            "graph": {
                "identity": graph_dict.get("identity", {}),
                "financial": graph_dict.get("financial", {}),
                "sponsor": graph_dict.get("sponsor"),
                "enrollment": graph_dict.get("enrollment"),
                "temporal": graph_dict.get("temporal", {}),
                "source_doc_types": graph_dict.get("source_doc_types", []),
                "token_count": graph_dict.get("token_count", 0),
                "page_count": graph_dict.get("source_page_count", 0),
                "estimated_raw_tokens": graph_dict.get("estimated_raw_tokens", 0),
            },
            "purged_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        }

        yield _sse({"type": "complete", "result": final_result})

    except Exception as e:
        tb = traceback.format_exc()
        yield _sse({"type": "error", "message": str(e), "traceback": tb[:800]})


async def _run_pipeline_to_store(
    task_id: str,
    pdf_bytes_list: list[bytes],
    context: dict,
) -> None:
    """
    Runs the full pipeline and writes progress events into _tasks[task_id].
    This is the background coroutine launched by POST /api/start.
    """
    task = _tasks[task_id]

    def emit(event_dict: dict):
        """Append an event to the task's progress log."""
        event_dict["_ts"] = time.time()
        task["progress"].append(event_dict)
        task["current_stage"] = event_dict.get("stage", event_dict.get("agent", task["current_stage"]))

    loop = asyncio.get_event_loop()
    inter_agent_delay = float(os.environ.get("INTER_AGENT_DELAY", "1.0"))

    try:
        # ── Stage 1: Structural encoding
        emit({"type": "stage_start", "stage": "encode", "message": "Rendering pages and running Flash extraction..."})
        t0 = time.time()

        all_pages: list[PageExtraction] = []
        for pdf_bytes in pdf_bytes_list:
            pages = await loop.run_in_executor(None, extract_pages, pdf_bytes)
            all_pages.extend(pages)

        graph = await loop.run_in_executor(None, build_graph, all_pages)
        encode_elapsed = time.time() - t0

        emit({
            "type": "stage_done", "stage": "encode",
            "elapsed_s": round(encode_elapsed, 1),
            "data": {
                "page_count": graph.source_page_count,
                "token_count": graph.token_count,
                "raw_tokens": graph.estimated_raw_tokens,
                "doc_types": graph.source_doc_types,
                "compression_pct": round((1 - graph.token_count / max(graph.estimated_raw_tokens, 1)) * 100, 1),
            }
        })

        # ── Stage 2: Privacy purge
        emit({"type": "stage_start", "stage": "purge", "message": "Purging raw document bytes from memory..."})
        pdf_bytes_list = []
        emit({"type": "stage_done", "stage": "purge", "elapsed_s": 0.0})

        # ── Stage 3: Rule engine
        emit({"type": "stage_start", "stage": "rules", "message": "Applying 9 deterministic rules to semantic graph..."})
        t0 = time.time()

        visa_context_dict = None
        try:
            from rules.context_profiles import VisaContext, build_context
            dest = context.get("dest", "")
            visa_type = context.get("type", "student")
            visa_context_dict = build_context(dest, visa_type)
        except Exception:
            pass

        rule_findings_raw = await loop.run_in_executor(
            None, lambda: run_rules(graph, context=None)
        )
        rule_findings = [f.model_dump() for f in rule_findings_raw]
        rules_elapsed = time.time() - t0

        emit({
            "type": "stage_done", "stage": "rules",
            "elapsed_s": round(rules_elapsed, 1),
            "data": {"finding_count": len(rule_findings)}
        })

        # ── Stages 4-8: Specialist agents
        graph_dict = graph.model_dump()
        agent_findings: list[dict] = []
        completed_agents: list[str] = []

        state = {
            "semantic_graph": graph_dict,
            "rule_findings": rule_findings,
            "agent_findings": [],
            "completed_agents": [],
            "visa_context": visa_context_dict,
            "document_checklist": [],
            "verdict": None,
            "risk_score": None,
            "raw_purged": True,
            "_debug_logger": None,
        }

        for i, (agent_id, agent_cls) in enumerate(AGENT_CLASSES):
            if i > 0:
                await asyncio.sleep(inter_agent_delay)

            emit({"type": "agent_start", "agent": agent_id})
            t0 = time.time()

            try:
                result = await loop.run_in_executor(None, lambda cls=agent_cls, s=state: cls().run(s))
                agent_findings.extend(result.get("agent_findings", []))
                completed_agents.extend(result.get("completed_agents", []))
                state["agent_findings"] = agent_findings
                state["completed_agents"] = completed_agents
                agent_elapsed = time.time() - t0

                this_agent_findings = result.get("agent_findings", [])
                anomaly_count = sum(len(f.get("anomalies", [])) for f in this_agent_findings)

                emit({
                    "type": "agent_done", "agent": agent_id,
                    "elapsed_s": round(agent_elapsed, 1),
                    "verdict": this_agent_findings[0].get("agent_verdict") if this_agent_findings else None,
                    "anomaly_count": anomaly_count,
                })
            except Exception as e:
                emit({"type": "agent_error", "agent": agent_id, "message": str(e)})
                completed_agents.append(agent_id)

        # ── Stage 9: Narrative synthesis
        await asyncio.sleep(inter_agent_delay)
        emit({"type": "stage_start", "stage": "narrative", "message": "Synthesising cross-document verdict and rebuttal guidance..."})
        t0 = time.time()

        state["agent_findings"] = agent_findings
        narrative_result = await loop.run_in_executor(None, lambda: NarrativeAgent().run(state))
        narrative_elapsed = time.time() - t0

        emit({"type": "stage_done", "stage": "narrative", "elapsed_s": round(narrative_elapsed, 1)})

        # ── Final: assemble result
        verdict = narrative_result.get("verdict", {})
        risk_score = narrative_result.get("risk_score", 1.0)

        final_result = {
            "verdict": verdict.get("verdict", "FAIL"),
            "risk_score": risk_score,
            "missing_documents": verdict.get("missing_documents", []),
            "rejection_narrative": verdict.get("rejection_narrative", ""),
            "rebuttal_guidance": verdict.get("rebuttal_guidance", []),
            "citations": verdict.get("citations", []),
            "critical_issues": verdict.get("critical_issues", []),
            "warning_issues": verdict.get("warning_issues", []),
            "agent_findings": agent_findings,
            "rule_findings": rule_findings,
            "graph": {
                "identity": graph_dict.get("identity", {}),
                "financial": graph_dict.get("financial", {}),
                "sponsor": graph_dict.get("sponsor"),
                "enrollment": graph_dict.get("enrollment"),
                "temporal": graph_dict.get("temporal", {}),
                "source_doc_types": graph_dict.get("source_doc_types", []),
                "token_count": graph_dict.get("token_count", 0),
                "page_count": graph_dict.get("source_page_count", 0),
                "estimated_raw_tokens": graph_dict.get("estimated_raw_tokens", 0),
            },
            "purged_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        }

        emit({"type": "complete", "result": final_result})
        task["status"] = "complete"
        task["result"] = final_result
        task["completed_at"] = time.time()

    except Exception as e:
        tb = traceback.format_exc()
        emit({"type": "error", "message": str(e), "traceback": tb[:800]})
        task["status"] = "error"
        task["error"] = str(e)
        task["completed_at"] = time.time()


@app.post("/api/analyze")
async def analyze(
    files: list[UploadFile] = File(...),
    context: str = Form(default="{}"),
):
    """
    Full pipeline analysis with SSE progress streaming.
    Accepts multipart form:
      - files: one or more PDF uploads
      - context: JSON string with {from, dest, type}
    """
    try:
        ctx = json.loads(context)
    except Exception:
        ctx = {}

    pdf_bytes_list = []
    for f in files:
        data = await f.read()
        pdf_bytes_list.append(data)

    if not pdf_bytes_list:
        raise HTTPException(400, "No files uploaded")

    async def generate():
        async for chunk in _run_pipeline(pdf_bytes_list, ctx):
            yield chunk

    return StreamingResponse(
        generate(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "X-Accel-Buffering": "no",
            "Access-Control-Allow-Origin": "*",
        }
    )


# ─────────────────────────────────────────────────────────────────
# Async Polling Endpoints (Task 2)
# ─────────────────────────────────────────────────────────────────

def _purge_stale_tasks():
    """Remove completed/errored tasks older than TTL."""
    now = time.time()
    stale = [tid for tid, t in _tasks.items()
             if t.get("completed_at") and (now - t["completed_at"]) > TASK_TTL_SECONDS]
    for tid in stale:
        del _tasks[tid]


@app.post("/api/start")
async def start_analysis(
    files: list[UploadFile] = File(...),
    context: str = Form(default="{}"),
):
    """
    Start async pipeline. Returns task_id immediately (HTTP 202).
    The pipeline runs in the background. Poll GET /api/status/{task_id} for progress.
    """
    _purge_stale_tasks()

    try:
        ctx = json.loads(context)
    except Exception:
        ctx = {}

    pdf_bytes_list = []
    for f in files:
        data = await f.read()
        pdf_bytes_list.append(data)

    if not pdf_bytes_list:
        raise HTTPException(400, "No files uploaded")

    task_id = str(uuid.uuid4())
    _tasks[task_id] = {
        "status": "running",
        "progress": [],
        "current_stage": None,
        "result": None,
        "error": None,
        "started_at": time.time(),
        "completed_at": None,
    }

    # Launch the pipeline as a background coroutine
    asyncio.create_task(_run_pipeline_to_store(task_id, pdf_bytes_list, ctx))

    return JSONResponse(
        status_code=202,
        content={"task_id": task_id, "status": "running"},
    )


@app.get("/api/status/{task_id}")
async def get_task_status(
    task_id: str,
    since: int = 0,
):
    """
    Poll task progress. Returns current status + new progress events since index `since`.
    Query param `since` is the index of the last event the client has seen.
    This avoids sending the entire progress array on every poll.
    """
    if task_id not in _tasks:
        raise HTTPException(404, f"Task {task_id} not found")

    task = _tasks[task_id]
    elapsed = time.time() - task["started_at"]

    return {
        "task_id": task_id,
        "status": task["status"],
        "current_stage": task["current_stage"],
        "elapsed_s": round(elapsed, 1),
        "progress": task["progress"][since:],
        "progress_total": len(task["progress"]),
        "result": task["result"],
        "error": task["error"],
    }


# ─────────────────────────────────────────────────────────────────
# Entry point
# ─────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 8000))
    print(f"\n  Uplan server starting on http://0.0.0.0:{port}")
    print(f"  Flash model : {os.environ.get('FLASH_MODEL', 'gemini-3-flash-preview')}")
    print(f"  Pro model   : {os.environ.get('PRO_MODEL', 'gemini-3.1-pro-preview')}")
    print(f"  Debug mode  : {os.environ.get('UPLAN_DEBUG', '1')}\n")

    uvicorn.run(
        "server:app",
        host="0.0.0.0",
        port=port,
        reload=os.environ.get("DEV", "0") == "1",
        log_level="info",
    )