The platform for continuously improving AI agents.
"""Analytics warn when one input gives away the label, or the input names it."""
import json
import pytest
from overbae.services.datasets import analytics
from overbae.services.datasets.profile import profile_records
pytestmark = pytest.mark.covers("data-logic")
OPTIONS = ["refund", "replacement"]
def row(label, state):
return {
"decision": {
"state": json.dumps(state) if isinstance(state, dict) else state,
"question": "Which resolution?",
"kind": "choice",
"options": OPTIONS,
"target_probabilities": [1, 0] if label == "refund" else [0, 1],
"target_semantics": "categorical_gold",
}
}
def checks(records):
report = profile_records(records)["analytics"]
return {item["id"]: item for item in report["checks"]}
def test_an_input_that_gives_the_label_on_shared_values_is_flagged():
records = [
row(label, {"queue": f"{label}-desk", "order": index, "note": "customer wrote in"})
for index, label in enumerate(["refund", "replacement"] * 60)
]
found = checks(records)
determined = found["leakage.input_determines_label"]
assert determined["status"] == "warning"
assert [item["field"] for item in determined["metrics"]["findings"]] == ["queue"]
assert "queue alone gives the label on 120 of the 120 rows" in determined["summary"]
# Distinct per row, so no value is shared by five rows: no evidence either way.
assert "order" not in determined["summary"]
assert found["leakage.label_in_input"]["status"] == "warning"
assert found["leakage.label_in_input"]["metrics"]["findings"][0]["field"] == "queue"
def test_inputs_that_only_correlate_or_name_shared_words_pass():
records = [
row(label, {"channel": "email" if index % 3 else "phone", "text": "please help"})
for index, label in enumerate(["refund", "replacement"] * 60)
]
found = checks(records)
assert found["leakage.input_determines_label"]["status"] == "pass"
assert found["leakage.label_in_input"]["status"] == "pass"
def test_text_states_are_searched_for_their_label_but_never_block():
records = [
row(label, f"Ticket: the customer wants a {label} for order {index}")
for index, label in enumerate(["refund", "replacement"] * 30)
]
found = checks(records)
assert found["leakage.label_in_input"]["status"] == "warning"
assert found["leakage.label_in_input"]["summary"] == (
"state names the row's own label on 60 of 60 rows."
)
assert found["leakage.input_determines_label"]["status"] == "not_checked"
assert not any(item["status"] == "error" for item in found.values())
def test_labels_without_distinct_words_are_not_searched():
records = [
{
"decision": {
**row("refund", {"a": 1})["decision"],
"options": ["Yes", "No"],
"target_probabilities": [1, 0] if index % 2 else [0, 1],
}
}
for index in range(60)
]
found = checks(records)
assert found["leakage.label_in_input"]["status"] == "not_checked"
single = checks([row("refund", {"a": 1}) for _ in range(60)])
assert single["leakage.label_in_input"]["status"] == "not_applicable"
assert single["leakage.input_determines_label"]["status"] == "not_applicable"
def test_an_input_past_the_tracked_fields_is_not_searched_for_its_label():
filler = {f"field_{i}": "x" for i in range(analytics.FIELD_LIMIT)}
records = [
row(label, {**filler, "queue": f"{label}-desk"}) for label in ["refund", "replacement"] * 60
]
found = checks(records)
assert found["leakage.label_in_input"]["metrics"]["findings"] == []
def test_inputs_with_too_many_values_leave_the_weights_check_alone():
records = [
row(label, {"order": index})
for index, label in enumerate(["refund", "replacement"] * (analytics.VALUE_LIMIT // 2 + 1))
]
found = checks(records)
assert found["leakage.input_determines_label"]["metrics"]["unassessed_fields"] == ["order"]
weights = found["targets.weights"]
assert (weights["status"], weights["metrics"]["aggregate_overflow"]) == ("pass", False)