cleandataengineering.aiDrug-Safety Agent on Databricks, drawn simply
You are here: Checks › Grounding

Drug-Safety Agent on Databricks · Checks · Grounding

Grounding: every number equals a tool result

This is the check that makes the agent trustworthy for numbers. After the draft is parsed, code looks at what the tools returned during the same investigation and compares: the number of cases, the PRR, and whether the event is on the label. Any mismatch stops the draft from being shown as finished.

Goes inThe parsed draft and the tool results from the same investigation
What happensCompare cases, PRR and the label verdict with the tool results; also fail if a statistic is quoted but the tool was never called
Comes outAn empty list (clean), or a list of exact mismatches

What it does not check. Whether the assessment text is wise. A draft can be fully grounded and still weak. That is why a person approves every draft.

Evidence so far. In the 20-pair evaluation all 20 drafts passed this check. Twenty is a small sample; the number to watch is how this holds up on the larger set the specification calls for.

The real code

Cut from the repository each time this site is built, so it always matches what runs. Each header names the file and the line it was cut from.

src/drugsafety/agent.py:133check_grounding(): the numbers and the label verdict
def check_grounding(draft: Draft) -> list[str]:
    """Deterministic check: every number and the label verdict in the draft must equal what the tools returned."""
    if draft.body is None:
        return []
    b = draft.body
    results = [t["result"] for t in draft.trace if t["kind"] == "tool" and "error" not in t["result"]]
    stats = next((r for r in reversed(results) if "prr" in r), None)
    label = next((r for r in reversed(results) if "on_label" in r), None)
    problems = []
    if stats is None:
        problems.append("draft quotes statistics but signal_stats was never called")
    else:
        if b.get("cases") != stats["cases"]:
            problems.append(f"cases {b.get('cases')} != tool {stats['cases']}")
        got, want = b.get("prr"), stats["prr"]
        if got is None or want is None or abs(got - want) > 0.05:
            problems.append(f"prr {got} != tool {want}")
    if label is None:
        problems.append("draft states label status but label_mentions was never called")
    elif b.get("on_label") != label["on_label"]:
        problems.append(f"on_label {b.get('on_label')} != tool {label['on_label']}")
    return problems
src/drugsafety/hagent.py:141Called on every draft before anyone sees it
def grounding(d: Draft) -> list[str]:
    """Every number and the label verdict in the draft must equal what a tool returned in the same task."""
    from drugsafety.agent import Draft as V0

    return check_grounding(V0(d.status, d.body, d.reason, d.trace))
src/drugsafety/entrypoint.py:144On a mismatch: pass to a person with the mismatches listed
    if problems := hagent.grounding(d):
        eid, _ = _escalate(store, "grounding-failed", text, reviewer_id)
        ref = f" Reference {eid}." if eid else ""
        return Reply(
            "A number in my draft did not match what the tools returned, so I am passing it to a person. "
            + "; ".join(problems)