An open source, non-profit web search engine
Degrade when the model is newer than the code, instead of failing the whole queue
Daoud Clarkec1490d9a7a54
diff --git a/mwmbl/moderation/artifacts/README.md b/mwmbl/moderation/artifacts/README.mdindex ea8d893..3f02901 100644--- a/mwmbl/moderation/artifacts/README.md+++ b/mwmbl/moderation/artifacts/README.md@@ -58,6 +58,22 @@ and every deploy runs on the deterministic checks alone until the first retrainprobe is what turns that from an exception per domain inside the enrichment task into an honest"not assessed yet" — it is not a licence to leave the artifact stale.+## Retrain from the image the workers are running++`is_compatible()` protects the running code from an artifact **older** than it. Nothing can+protect it from one that is *newer*: a featuriser is pickled whole, so a model trained by code+with one more feature than the workers build produces a matrix they cannot use, and they have no+way to know that in advance.++That is not hypothetical. In August 2026 a retrain run through `dokku enter` on a freshly built+image published a model with nine shape features while the long-running container was still on+eight. Every one of the 2,409 rows in the queue rescore failed with `X has 57673 features, but+LogisticRegression is expecting 57674`, and the task retried on the same exception.++So **deploy first, then retrain** — the process that publishes must be running the same image as+the process that serves. If it happens anyway, delete the offending `ModerationModelArtifact` row+and the workers fall back to the warm start here; then deploy and retrain again.+## What this version was trained onThe committed artifact comes from the judgments export, which carries no page text. Only the 21diff --git a/mwmbl/moderation/model.py b/mwmbl/moderation/model.pyindex 9f3f8e2..cc596ba 100644--- a/mwmbl/moderation/model.py+++ b/mwmbl/moderation/model.py@@ -155,8 +155,21 @@ def suggest(domain: str, page_texts: list[str], evidence_items: list[EvidenceItereturn Suggestion(action="UNSURE", confidence=0.0, model_version="",evidence=[item.to_dict() for item in evidence_items])- reject_probability, reason, reason_confidence = model.predict(- [ModerationExample(domain, page_texts)])[0]+ try:+ reject_probability, reason, reason_confidence = model.predict(+ [ModerationExample(domain, page_texts)])[0]+ except Exception:+ # The same degradation, for a model that loaded but cannot score. is_compatible()+ # catches this at load time, but only for artifacts *older* than the running code -+ # it cannot protect code that is older than the artifact. A retrain run from a freshly+ # built image publishes a model every not-yet-restarted worker then chokes on, and in+ # August 2026 that failed all 2,409 rows of a queue rescore and left the task retrying+ # on the same exception. One unscorable domain is a suggestion we do not have; it is+ # not a reason to end the run.+ logger.exception("Model %s could not score %s; falling back to the deterministic "+ "checks", model.version, domain)+ return Suggestion(action="UNSURE", confidence=0.0, model_version="",+ evidence=[item.to_dict() for item in evidence_items])if reject_probability >= settings.MODERATION_REJECT_THRESHOLD:action, confidence = "REJECT", reject_probabilitydiff --git a/test/test_domain_moderation.py b/test/test_domain_moderation.pyindex 2e1577c..84a42c0 100644--- a/test/test_domain_moderation.py+++ b/test/test_domain_moderation.py@@ -1409,6 +1409,38 @@ def test_an_artifact_from_a_different_feature_set_degrades_to_the_rules(tmp_pathassert (suggestion.action, suggestion.model_version) == ("UNSURE", "")+def test_a_model_newer_than_the_code_degrades_instead_of_ending_the_run(tmp_path, settings):+ """is_compatible() protects the running code from an artifact older than it. Nothing can+ protect it from a *newer* one: a featuriser is pickled whole, so a model trained by code+ with one more feature produces a matrix the workers cannot use and they cannot know that+ until they try. A retrain from a freshly built image did exactly that in August 2026 and+ failed all 2,409 rows of a queue rescore, retrying forever on the same exception."""+ joblib.dump(_StaleFeatureSetModel(), tmp_path / "model.joblib")+ settings.DOMAIN_MODERATION_MODEL_DIR = str(tmp_path)++ # The model is handed in directly, as a worker holding an already-loaded one would have it,+ # so the load-time probe is bypassed exactly the way it is in production.+ suggestion = suggest("a.com", [], [], model=_StaleFeatureSetModel())++ assert (suggestion.action, suggestion.confidence) == ("UNSURE", 0.0)+ assert suggestion.model_version == ""+++def test_a_decisive_check_still_wins_over_a_model_that_cannot_score():+ """The deterministic checks do not need the model, so an unscorable one must not downgrade+ a decisive answer to UNSURE."""+ items = [rules.EvidenceItem(kind="http_error", direction="REJECT",+ label="Homepage returns 404", implies_action="REJECT",+ implies_reason="OTHER", implies_confidence=0.95)]++ suggestion = suggest("a.com", [], items, model=_StaleFeatureSetModel())++ assert suggestion.action == "REJECT"+ assert suggestion.reason_source == "rule"++# --------------------------------------------------------------------- request validation@pytest.mark.django_db