Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Ask the evidence whether an OTHER rejection can be explained, on both sides

Daoud Clarke09ed4b465f32
diff --git a/mwmbl/moderation/rules.py b/mwmbl/moderation/rules.py
index 8638e2e..fb60e14 100644
--- a/mwmbl/moderation/rules.py
+++ b/mwmbl/moderation/rules.py
@@ -205,16 +205,57 @@ def decisive(items: list[EvidenceItem]) -> Optional[EvidenceItem]:
return max(decisive_items, key=lambda item: item.implies_confidence)
+# What a check tells the *submitter*, where its label is the wrong sentence to send. A label
+# is written for the moderator deciding the case, so it may name the exception we caught or be
+# written from our side of the transaction; a detail is read by the person whose site was
+# rejected. Most labels serve both - "Homepage returns HTTP 404" is exactly what a submitter
+# needs to hear, and naming the domain a site redirects to tells them what to submit instead -
+# and the ones that do not are overridden here. Any new check that implies OTHER needs an
+# entry unless its label reads as a sentence to the submitter.
+#
+# Keyed on kind rather than carried on the item so that evidence stored before an entry was
+# written is covered too: crawl evidence is cached and a rescore reuses it, so a new field on
+# EvidenceItem would stay empty until the domain happened to be crawled again.
+SUBMITTER_DETAIL = {
+ "unreachable": "We could not fetch this site when we tried to crawl it.",
+ "do_not_crawl": "We don't index search engines or our own site.",
+}
+
+# "Some cached check implies OTHER", as a containment test the queue's SQL can run against the
+# stored evidence. It is the same question :func:`other_detail` answers, and the two have to
+# answer alike: the queue filters and orders thousands of rows on this without being able to
+# call the Python (see mwmbl.moderation.suggest.annotate_queue), and where they disagree it
+# hides rows that are on screen. Written down here so the pair stays visible from one place.
+IMPLIES_OTHER = [{"implies_action": "REJECT", "implies_reason": "OTHER"}]
+
+
def implied_detail(item: EvidenceItem) -> str:
"""What a check's rejection tells the submitter, for a reason that says nothing on its own.
- Only OTHER needs one - "spam" explains itself, "other" does not - and the check's label is
- already that sentence: "Homepage returns HTTP 404" is exactly what was wrong. Every OTHER
- a check implies is one of these, which is what lets the API refuse an OTHER rejection with
- no detail (mwmbl.platform.schemas.RejectionFieldsMixin) without making the suggestion
+ Only OTHER needs one - "spam" explains itself, "other" does not - and for most checks the
+ label is already that sentence; SUBMITTER_DETAIL covers the rest. Every OTHER a check
+ implies has one of these, which is what lets the API refuse an OTHER rejection with no
+ detail (mwmbl.platform.schemas.RejectionFieldsMixin) without making the suggestion
untakeable.
"""
- return item.label if item.implies_reason == "OTHER" else ""
+ if item.implies_reason != "OTHER":
+ return ""
+ return SUBMITTER_DETAIL.get(item.kind, item.label)
+
+
+def other_detail(items: list[EvidenceItem]) -> str:
+ """The sentence behind an OTHER rejection a check decided, or "" when none did.
+
+ Deliberately not :func:`decisive`: the reason being explained is OTHER, so what explains it
+ is the strongest check implying OTHER, which need not be the strongest check in the list.
+ That is also the only form of the question SQL can ask of stored evidence - see
+ IMPLIES_OTHER - which is what lets the queue agree with what the moderator is shown.
+ """
+ implying_other = [item for item in items
+ if item.implies_action == "REJECT" and item.implies_reason == "OTHER"]
+ if not implying_other:
+ return ""
+ return implied_detail(max(implying_other, key=lambda item: item.implies_confidence))
def _tld(domain: str) -> str:
diff --git a/mwmbl/moderation/suggest.py b/mwmbl/moderation/suggest.py
index 421625f..8f01fd1 100644
--- a/mwmbl/moderation/suggest.py
+++ b/mwmbl/moderation/suggest.py
@@ -109,14 +109,18 @@ def suggestion_for(submission: DomainSubmission,
reason = evidence.suggested_reason if action == "REJECT" else ""
source = evidence.reason_source or "model"
# OTHER is the one reason that explains nothing on its own, and a decision may not carry
- # it without the sentence the submitter is shown. When a check decided it that sentence is
- # the check's own label, which is what reason_source == "rule" means here; when the model
+ # it without the sentence the submitter is shown. When a check decided it that sentence
+ # comes from the check, which is what reason_source == "rule" means here; when the model
# did there is none, and a suggestion no client can send back is not one to draw - so it
# goes to the moderator as UNSURE instead. model.suggest already stops storing those; this
# is for the rows stored before it did, and annotate_queue mirrors it so the queue filters
# and orders on what is actually drawn.
- detail = (rules.implied_detail(cached_decisive)
- if reason == "OTHER" and source == "rule" and cached_decisive is not None else "")
+ #
+ # The stored reason is not taken as proof that a check is still behind it: reason_source
+ # says one was when the row was written, and a check that stops being decisive - or stops
+ # implying OTHER - leaves rows saying "rule" with nothing left to explain them until the
+ # next rescore. rules.other_detail asks the evidence itself, as does the queue.
+ detail = rules.other_detail(cached) if reason == "OTHER" and source == "rule" else ""
if action == "REJECT" and reason in UNUSABLE_MODEL_REASONS and not detail:
action, confidence, reason = "UNSURE", 0.0, ""
@@ -179,6 +183,12 @@ def annotate_queue(submissions):
name=OuterRef("name"), status="REJECTED")),
submitter_decided=Exists(DomainSubmission.objects.filter(
DECIDED, submitted_by_id=OuterRef("submitted_by_id"))),
+ # rules.other_detail, as far as SQL can ask it: is there a cached check implying
+ # OTHER, and so a sentence to send with an OTHER rejection. A containment test rather
+ # than a column comparison because it is a question about the evidence list, and
+ # Exists rather than a lookup on the annotation above so it reads as the join it is.
+ explained_by_a_check=Exists(DomainEvidence.objects.filter(
+ domain=OuterRef("name"), evidence__contains=rules.IMPLIES_OTHER)),
)
scored = Q(evidence_state=DomainEvidence.State.READY)
@@ -195,10 +205,12 @@ def annotate_queue(submissions):
withheld = scored & Q(evidence_action="APPROVE") & Q(submitter_decided=False)
# suggestion_for's other downgrade: a stored REJECT nobody can send back - one whose
# reason is OTHER with no check behind it, so there is no detail to send, or one carrying
- # no reason at all. reason_source is again how we know a check produced it, and only then
- # is the detail the check's own label.
+ # no reason at all. Both halves of what suggestion_for asks: reason_source says a check
+ # produced the reason, and the evidence still has to contain the check that explains it -
+ # a stale row can say "rule" and have nothing implying OTHER left in its evidence.
+ explained = Q(evidence_source="rule") & Q(explained_by_a_check=True)
unexplained = scored & Q(evidence_action="REJECT") & (
- (Q(evidence_reason="OTHER") & ~Q(evidence_source="rule"))
+ (Q(evidence_reason="OTHER") & ~explained)
| Q(evidence_reason="") | Q(evidence_reason__isnull=True))
submissions = submissions.annotate(
diff --git a/test/test_domain_moderation.py b/test/test_domain_moderation.py
index 05e73af..87f994b 100644
--- a/test/test_domain_moderation.py
+++ b/test/test_domain_moderation.py
@@ -167,6 +167,31 @@ def test_a_check_that_implies_other_carries_the_detail_the_submitter_is_shown():
assert suggestion.reason_detail == "Homepage returns HTTP 404"
+def test_a_check_written_for_the_moderator_tells_the_submitter_something_readable():
+ """A label is read by whoever is deciding the case and a detail by the person whose site
+ was rejected, so a label naming the exception we caught is not the sentence to send."""
+ items = rules.crawl_evidence(
+ "example.com", {"error": "AbortError", "pages": [], "signals": {}})
+ suggestion = suggest("example.com", [], items, model=mock.Mock(version="test"))
+
+ assert (suggestion.action, suggestion.reason) == ("REJECT", "OTHER")
+ assert suggestion.reason_detail == "We could not fetch this site when we tried to crawl it."
+ # The moderator still gets the exception, in the evidence list where it belongs.
+ assert "Could not be fetched (AbortError)" in [
+ item["label"] for item in suggestion.evidence]
+
+
+def test_the_do_not_crawl_list_explains_itself_from_the_submitters_side():
+ """Its label is written from ours - "we don't crawl ourselves" - and is about our policy
+ rather than about the site the submitter sent."""
+ items = rules.crawl_evidence(
+ "google.com", {"http_status": 200, "pages": [], "signals": {}})
+ suggestion = suggest("google.com", [], items, model=mock.Mock(version="test"))
+
+ assert (suggestion.action, suggestion.reason) == ("REJECT", "OTHER")
+ assert suggestion.reason_detail == "We don't index search engines or our own site."
+
+
def test_a_reason_that_explains_itself_needs_no_detail():
model = mock.Mock(version="test")
model.predict.return_value = [(0.92, "SPAM", 0.8)]
@@ -1298,6 +1323,25 @@ def test_a_previous_rejection_of_the_same_domain_says_so_as_the_detail(submitter
assert suggestion.reason_detail == "This domain has already been rejected before"
+def test_a_rule_scored_other_with_no_check_left_behind_it_is_not_suggested(submitter):
+ """reason_source records that a check decided the reason when the row was written, not
+ that one still does. A check that stops being decisive - or stops implying OTHER - leaves
+ stored rows claiming "rule" with nothing left to explain them, and a rejection whose
+ detail cannot be written is one the API would refuse. So it goes back as UNSURE, and the
+ detail is read off the evidence rather than assumed from the column."""
+ demoted = rules.EvidenceItem(
+ "http_status", rules.REJECT, "Homepage returns HTTP 404").to_dict()
+ submission = DomainSubmission.objects.create(name="stale.example", submitted_by=submitter)
+ evidence = ready_evidence("stale.example", suggested_action="REJECT",
+ suggested_reason="OTHER", confidence=0.9,
+ reason_source="rule", evidence=[demoted])
+
+ suggestion = suggestion_for(submission, evidence)
+
+ assert (suggestion.action, suggestion.reason, suggestion.reason_detail) == ("UNSURE", "", "")
+
+
# --------------------------------------------------------------------- queue parity
@pytest.mark.django_db
@@ -1326,6 +1370,13 @@ def test_queue_display_matches_suggestion_for(submitter, established):
# A reason the model cannot explain, so not shown as a suggestion at all.
("unexplained.example", submitter, {"suggested_action": "REJECT",
"suggested_reason": "OTHER", "confidence": 0.93}),
+ # The same, from the other side: a row that says a check decided the reason, with
+ # nothing implying OTHER left in its evidence to write the detail from.
+ ("stale-rule.example", submitter,
+ {"suggested_action": "REJECT", "suggested_reason": "OTHER", "confidence": 0.93,
+ "reason_source": "rule",
+ "evidence": [rules.EvidenceItem(
+ "http_status", rules.REJECT, "Homepage returns HTTP 404").to_dict()]}),
("approved-before.example", submitter, {"suggested_action": "REJECT",
"suggested_reason": "SPAM"}),
("rejected-before.example", submitter, {}),