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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
"""Mellea-validated reconciliation for Riprap.

Wraps the existing Granite-via-Ollama reconciliation in IBM Research's
Mellea framework: typed output + programmatic post-conditions +
rejection sampling. Replaces post-hoc sentence-dropping with
"don't accept output until requirements pass."

Streaming and rejection sampling are mutually exclusive β€” by the time
we'd validate, the user has watched the bad output appear. Strict mode
trades streaming for compliance; the UI shows a "validating" skeleton
instead of token-by-token render.

The four invariants ported from the parent project's mellea_probe:

  1. no_invented_numbers       β€” every number in output appears in source
  2. no_placeholder_tokens     β€” output never contains "[source]" or
                                 raw <document> markup
  3. every_claim_cited         β€” each numeric token has a [doc_id] within
                                 ~40 chars
  4. referenced_doc_ids_exist  β€” cited doc_ids βŠ† input doc_ids
"""
from __future__ import annotations

import logging
import os
import re
import time
from typing import Any

from mellea import start_session
from mellea.stdlib.requirements import req, simple_validate
from mellea.stdlib.sampling import RejectionSamplingStrategy

from app import llm

log = logging.getLogger("riprap.mellea")

# Default reconciler model β€” same env-var contract as app/reconcile.py.
DEFAULT_MODEL = os.environ.get(
    "RIPRAP_RECONCILER_MODEL",
    os.environ.get("RIPRAP_OLLAMA_MODEL", "granite4.1:8b"),
)

# Loop budget β€” try up to N samples before falling back to the last
# candidate even if it didn't pass all requirements. Low ceiling so a
# pathological case can't run away with latency.
#
# Override at process start with RIPRAP_MELLEA_MAX_ATTEMPTS. We default
# to 2 on the local Ollama path (where each attempt is 30-90 s on the
# Mac) and 3 on remote/vLLM (where attempts are seconds). This caps
# worst-case demo latency without giving up the principal grounding
# guarantee β€” the first-attempt pass rate on the curated probes is >85%.
def _default_loop_budget() -> int:
    try:
        n = int(os.environ.get("RIPRAP_MELLEA_MAX_ATTEMPTS", "0"))
        if n > 0:
            return n
    except ValueError:
        pass
    return 2 if os.environ.get("RIPRAP_LLM_PRIMARY", "ollama").lower() == "ollama" else 3


DEFAULT_LOOP_BUDGET = _default_loop_budget()

# Number tokens β€” \b enforces a word boundary so identifier codes like
# QN1206, B12 (community board), or M14 (bus route) are skipped entirely.
# Inside QN1206 there's no \b between any chars, so no submatch leaks.
_NUM_RE = re.compile(r"\b-?\d[\d,]*(?:\.\d+)?\b")
_CITE_RE = re.compile(r"\[(?P<id>[a-z][a-z0-9_]*)\]")
# Same trivial-numbers list as the post-hoc verifier β€” well-known service
# line numbers, single digits.
_TRIVIAL_NUMS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "100",
                 "311", "911", "211"}


def _strip_markdown_for_check(text: str) -> str:
    """Drop bold markers + citation tags so the numeric scan is clean."""
    text = re.sub(r"\*\*([^*]+)\*\*", r"\1", text)
    text = re.sub(r"\[[a-z0-9_]+\]", "", text, flags=re.I)
    return text


def _normalize_num(s: str) -> set[str]:
    forms = {s}
    no_comma = s.replace(",", "")
    forms.add(no_comma)
    if "." in no_comma:
        forms.add(no_comma.rstrip("0").rstrip("."))
    return {f for f in forms if f}


def _haystack(doc_msgs: list[dict]) -> str:
    return "\n".join(m.get("content", "") for m in doc_msgs)


def _doc_ids(doc_msgs: list[dict]) -> set[str]:
    """Each doc message has role like "document <id>"; extract ids."""
    out = set()
    for m in doc_msgs:
        role = m.get("role", "")
        if role.startswith("document "):
            out.add(role.split(" ", 1)[1].strip())
    return out


# --- the four invariants ---------------------------------------------------


def _check_no_invented_numbers(doc_msgs: list[dict]):
    haystack = _haystack(doc_msgs)
    def _fn(text: str):
        clean = _strip_markdown_for_check(text)
        invented = []
        for n in _NUM_RE.findall(clean):
            if n in _TRIVIAL_NUMS:
                continue
            forms = _normalize_num(n)
            if not any(f in haystack for f in forms):
                invented.append(n)
        return not invented  # pass = no invented numbers
    return _fn


def _check_no_placeholder_tokens():
    def _fn(text: str):
        bad = []
        if "[source]" in text.lower():
            bad.append("[source]")
        if "<document" in text:
            bad.append("<document>")
        if "</document" in text:
            bad.append("</document>")
        if "[doc_id]" in text:
            # Model echoed the EXTRA_SYSTEM_PROMPT skeleton literally
            bad.append("[doc_id]")
        return not bad
    return _fn


def _check_every_claim_cited():
    """Each non-trivial numeric token must have a [doc_id] somewhere in
    the same sentence. Sentence boundaries are conservative: a period
    followed by whitespace, or end of text. This matches how a reader
    actually attributes claims β€” the citation can be anywhere in the
    sentence, not just adjacent to the number."""
    # Sentence end = `. ` or `.\n` or end-of-string. Question/exclamation
    # marks rarely appear in these briefings; period is enough.
    _SENT_END = re.compile(r"\.[\s)]|\.$")

    def _sentence_span(text: str, pos: int) -> tuple[int, int]:
        # Walk backwards to the previous sentence terminator.
        start = 0
        for m in _SENT_END.finditer(text, 0, pos):
            start = m.end()
        # Walk forwards to the next.
        m = _SENT_END.search(text, pos)
        end = m.start() + 1 if m else len(text)
        return start, end

    def _fn(text: str):
        clean = re.sub(r"\*\*([^*]+)\*\*", r"\1", text)
        for m in _NUM_RE.finditer(clean):
            n = m.group(0)
            if n in _TRIVIAL_NUMS:
                continue
            s, e = _sentence_span(clean, m.start())
            if not _CITE_RE.search(clean[s:e]):
                return False
        return True
    return _fn


def _failing_sentences_for_citations(text: str) -> list[str]:
    """Return the sentences in `text` that contain a non-trivial number
    but no [doc_id] citation. Used to give the model targeted reroll
    feedback so it can fix the exact spots that failed."""
    clean = re.sub(r"\*\*([^*]+)\*\*", r"\1", text)
    sents = re.split(r"\.[\s)]|\.$", clean)
    bad = []
    for s in sents:
        nums = [n for n in _NUM_RE.findall(s) if n not in _TRIVIAL_NUMS]
        if nums and not _CITE_RE.search(s):
            bad.append(s)
    return bad


def _check_referenced_doc_ids_exist(doc_msgs: list[dict]):
    valid = _doc_ids(doc_msgs)
    def _fn(text: str):
        cited = {m.group("id") for m in _CITE_RE.finditer(text)}
        rogue = cited - valid
        return not rogue
    return _fn


# --- main entry point ------------------------------------------------------


def reconcile_strict(doc_msgs: list[dict],
                     system_prompt: str,
                     user_prompt: str = "Write the cited briefing now.",
                     model: str | None = None,
                     loop_budget: int = DEFAULT_LOOP_BUDGET,
                     ollama_options: dict | None = None) -> dict[str, Any]:
    """Run Granite reconciliation with Mellea rejection sampling.

    Returns a dict with:
        paragraph         β€” final validated text
        rerolls           β€” number of resamples (0 = passed first try)
        requirements_passed β€” list of requirement names that passed in the
                              accepted sample
        requirements_failed β€” list of requirement names that failed
                              (empty on accepted sample)
        elapsed_s         β€” total seconds including rerolls
        model             β€” model id used
        loop_budget       β€” configured budget
    """
    model = model or DEFAULT_MODEL
    t0 = time.time()

    # Per-requirement closures wired with the doc context.
    # Keep the validator functions in our own table so we can re-run them
    # on the final paragraph to produce reliable pass/fail metadata for
    # the report β€” Mellea's internal validation-result objects vary by
    # version and aren't great for downstream display.
    checks = [
        ("numerics_grounded",
         "All numbers in the output must appear verbatim in the source documents.",
         _check_no_invented_numbers(doc_msgs)),
        ("no_placeholder_tokens",
         "The output must not contain placeholder tokens like [source] or raw <document> markup.",
         _check_no_placeholder_tokens()),
        ("citations_dense",
         "Every numeric claim must have a [doc_id] citation within ~120 characters.",
         _check_every_claim_cited()),
        ("citations_resolve",
         "Every cited [doc_id] must correspond to a real source document.",
         _check_referenced_doc_ids_exist(doc_msgs)),
    ]
    requirements = [
        req(desc, validation_fn=simple_validate(fn, reason=name))
        for name, desc, fn in checks
    ]

    session = start_session(backend_name="ollama", model_id=model,
                            model_options=ollama_options or {})
    try:
        # Build the prompt: system + serialized doc context + user task.
        # Mellea's instruct() takes the whole instruction; we serialize
        # the doc messages into the description so the haystack is
        # available to the model the same way it would be via
        # ollama.chat with role="document <id>" messages.
        doc_block = "\n\n".join(
            f"<document id=\"{m['role'].split(' ', 1)[1] if m['role'].startswith('document ') else 'unknown'}\">\n"
            f"{m['content']}\n</document>"
            for m in doc_msgs
        )
        instruction = (
            f"{system_prompt}\n\n"
            f"DOCUMENTS:\n{doc_block}\n\n"
            f"TASK: {user_prompt}"
        )

        result = session.instruct(
            description=instruction,
            strategy=RejectionSamplingStrategy(
                loop_budget=loop_budget,
                requirements=requirements,
            ),
            requirements=requirements,
            return_sampling_results=True,
            model_options={"temperature": 0,
                           "num_ctx": int(os.environ.get("RIPRAP_MELLEA_NUM_CTX", "4096")),
                           "num_predict": int(os.environ.get("RIPRAP_MELLEA_NUM_PREDICT", "400")),
                           **(ollama_options or {})},
        )

        paragraph = _extract_text(result).strip()
        n_attempts = _extract_attempts(result)
        rerolls = max(0, n_attempts - 1)
    finally:
        try:
            session.cleanup()
        except Exception:
            pass

    # Re-run our own checks on the final paragraph for clean pass/fail
    # metadata. This is what shows up in the report's compliance section.
    passed: list[str] = []
    failed: list[str] = []
    for name, _desc, fn in checks:
        try:
            if fn(paragraph):
                passed.append(name)
            else:
                failed.append(name)
        except Exception as e:
            log.warning("requirement %s raised: %r", name, e)
            failed.append(name)

    return {
        "paragraph": paragraph,
        "rerolls": rerolls,
        "n_attempts": n_attempts,
        "requirements_total": len(checks),
        "requirements_passed": passed,
        "requirements_failed": failed,
        "elapsed_s": round(time.time() - t0, 2),
        "model": model,
        "loop_budget": loop_budget,
    }


def reconcile_strict_streaming(
    doc_msgs: list[dict],
    system_prompt: str,
    user_prompt: str = "Write the cited briefing now.",
    model: str | None = None,
    loop_budget: int = DEFAULT_LOOP_BUDGET,
    ollama_options: dict | None = None,
    on_token=None,
    on_attempt_end=None,
) -> dict[str, Any]:
    """Hand-rolled rejection sampler that *streams* each attempt to the
    user instead of waiting silently for Mellea to validate behind the
    scenes. Same compliance contract as reconcile_strict β€” runs the
    same four checks, accepts the first attempt that passes, falls back
    to the last attempt if the budget is exhausted.

    Callbacks (both optional, both fire on the calling thread):
      on_token(delta: str, attempt_idx: int)
        β€” fires for every token chunk as it arrives from Granite.
      on_attempt_end(attempt_idx: int, passed: list[str], failed: list[str])
        β€” fires after each attempt with its per-requirement outcome.
        The frontend uses this to render reroll banners + reset the
        briefing buffer when a new attempt begins.
    """
    model = model or DEFAULT_MODEL
    t0 = time.time()

    checks = [
        ("numerics_grounded",
         _check_no_invented_numbers(doc_msgs)),
        ("no_placeholder_tokens",
         _check_no_placeholder_tokens()),
        ("citations_dense",
         _check_every_claim_cited()),
        ("citations_resolve",
         _check_referenced_doc_ids_exist(doc_msgs)),
    ]

    base_messages = doc_msgs + [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt},
    ]
    # num_ctx 4096 fits a typical trimmed prompt (β‰ˆ700 system + β‰ˆ2500 docs);
    # num_predict 400 caps the 4-section briefing at β‰ˆ300-350 tokens. With
    # RIPRAP_TRIM_DOCS=1 and the planner picking 6-9 specialists, the 4096
    # window has been sufficient on every probe; the previous 6144/600 was
    # sized for the *untrimmed* fan-out and was forcing Ollama to grow the
    # KV cache (33% more memory + a full re-init) every Mellea attempt.
    # Override with RIPRAP_MELLEA_NUM_CTX / RIPRAP_MELLEA_NUM_PREDICT.
    base_opts = {"temperature": 0,
                 "num_ctx": int(os.environ.get("RIPRAP_MELLEA_NUM_CTX", "4096")),
                 "num_predict": int(os.environ.get("RIPRAP_MELLEA_NUM_PREDICT", "400")),
                 **(ollama_options or {})}

    paragraph = ""
    last_passed: list[str] = []
    last_failed: list[str] = [name for name, _ in checks]
    last_paragraph = ""
    attempts = 0

    for attempt_idx in range(loop_budget):
        attempts = attempt_idx + 1
        # On reroll, append a tight feedback message naming what failed AND
        # the specific failing sentences (so the model knows exactly which
        # ones to fix). Granite responds well to surgical corrections.
        messages = list(base_messages)
        if attempt_idx > 0 and last_failed:
            feedback = [
                f"Your previous draft failed: {', '.join(last_failed)}.",
            ]
            if "citations_dense" in last_failed and last_paragraph:
                bad = _failing_sentences_for_citations(last_paragraph)
                if bad:
                    feedback.append(
                        "Specific sentences with uncited numbers:"
                    )
                    for s in bad[:3]:
                        feedback.append(f"  - {s.strip()}")
                    feedback.append(
                        "Add a [doc_id] citation at the end of each. "
                        "Re-emit the FULL briefing."
                    )
            else:
                feedback.append(
                    "Re-write so every sentence containing a number ends "
                    "with a [doc_id] citation."
                )
            messages.append({"role": "user", "content": "\n".join(feedback)})

        chunks: list[str] = []
        for chunk in llm.chat(model=model, messages=messages,
                                 stream=True, options=base_opts):
            delta = (chunk.get("message") or {}).get("content") or ""
            if delta:
                chunks.append(delta)
                if on_token is not None:
                    try:
                        on_token(delta, attempt_idx)
                    except Exception:
                        log.exception("on_token callback raised")
        paragraph = "".join(chunks).strip()

        passed: list[str] = []
        failed: list[str] = []
        for name, fn in checks:
            try:
                (passed if fn(paragraph) else failed).append(name)
            except Exception as e:
                log.warning("requirement %s raised: %r", name, e)
                failed.append(name)

        last_passed = passed
        last_failed = failed
        last_paragraph = paragraph
        if on_attempt_end is not None:
            try:
                on_attempt_end(attempt_idx, passed, failed)
            except Exception:
                log.exception("on_attempt_end callback raised")

        if not failed:
            break

    return {
        "paragraph": paragraph,
        "rerolls": max(0, attempts - 1),
        "n_attempts": attempts,
        "requirements_total": len(checks),
        "requirements_passed": last_passed,
        "requirements_failed": last_failed,
        "elapsed_s": round(time.time() - t0, 2),
        "model": model,
        "loop_budget": loop_budget,
    }


def _extract_text(result) -> str:
    """SamplingResult / ModelOutputThunk text extraction."""
    for attr in ("sample", "result", "value", "content"):
        v = getattr(result, attr, None)
        if v is not None:
            if hasattr(v, "value"):
                return str(v.value)
            return str(v)
    return str(result)


def _extract_attempts(result) -> int:
    """How many samples were drawn before stopping."""
    for attr in ("n_attempts", "num_attempts", "attempts"):
        v = getattr(result, attr, None)
        if isinstance(v, int):
            return v
    samples = getattr(result, "sample_validations", None) or getattr(result, "samples", None)
    if isinstance(samples, list):
        return len(samples)
    return 1


def _extract_pass_fail(result) -> tuple[list[str], list[str]]:
    """Best-effort extraction of which requirements passed on the
    accepted sample. mellea v0.4 exposes sample_validations as a list
    where each entry is itself a list of (Requirement, ValidationResult)
    tuples β€” duck-type defensively.
    """
    validations = getattr(result, "sample_validations", None)
    if not validations:
        return [], []
    last = validations[-1] if isinstance(validations, list) else validations
    passed: list[str] = []
    failed: list[str] = []
    items = last if isinstance(last, list) else [last]
    for item in items:
        # Item might be (Requirement, ValidationResult) tuple, or a single
        # ValidationResult, or a Requirement, depending on mellea version.
        ok = None
        descr = ""
        if isinstance(item, tuple) and len(item) >= 2:
            descr = str(item[0])[:80]
            v = item[1]
            ok = bool(getattr(v, "passed", getattr(v, "is_valid",
                          getattr(v, "result", False))))
        else:
            descr = str(getattr(item, "requirement", item))[:80]
            ok = bool(getattr(item, "passed", getattr(item, "is_valid",
                          getattr(item, "result", False))))
        if ok:
            passed.append(descr)
        else:
            failed.append(descr)
    return passed, failed