feat(translation): tunable acceptance floor (0.90) + per-post sticky override (#155)
The gate at a fixed 0.80 couldn't catch the real pain: Interpreter (fresh == cached, verified by probe) confidently mis-detects short ASCII English like "... WIP Part 1" as German at 0.86 — above the floor — so it was accepted and a re-translate reproduced it. Confidence alone can't separate the 0.86 collision (genuine German lands there too), and single-word mis-flags sit at a confident 1.0 no floor catches. Two operator-approved levers: - Acceptance floor is now a live Settings value (ImportSettings. translation_min_confidence, default 0.90; surfaced in the Translation card), so it's tunable without a redeploy. _accept takes the threshold as a parameter. - Per-post sticky override (Post.translation_override: auto/force/original). 'force' stores a translation even below the floor (rescue a skipped legit-foreign title); 'original' keeps the original and clears any stored translation (kill a confident mis-flag no floor catches). The sweep honors it on every run and _reset_translations skips 'original', so the choice survives a Re-translate-all. POST /api/posts/<id>/translation-override applies it immediately (translate now when the service is up, else queue for the sweep). UI: PostTranslationControl on the posts-feed card. Migration 0084 (both columns + a CHECK on the override). The feed + provenance serializers expose translation_override. With a stricter floor the rollback finally works: raise it -> Re-translate all -> the 0.86 mis-flags are rejected and restored to the original; force / keep-original handle the residual either way. Tests: gate thresholds against the param (0.86 rejected at 0.90, explicit-floor cases); sweep force/original + re-translate-skips-original; override endpoint (validation, original clears, force queues when disabled, feed exposes it); settings min_confidence default/save/validate. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CgZP9v2otxVJymiYsnVuMy
This commit is contained in:
@@ -184,6 +184,68 @@ async def test_rejects_bad_direction(client):
|
||||
assert (await resp.get_json())["error"] == "invalid_direction"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_translation_override_rejects_bad_value(client, seeded_post):
|
||||
_, _, post = seeded_post
|
||||
resp = await client.post(
|
||||
f"/api/posts/{post.id}/translation-override", json={"override": "nope"}
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert (await resp.get_json())["error"] == "invalid_override"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_translation_override_404_for_unknown(client):
|
||||
resp = await client.post(
|
||||
"/api/posts/999999/translation-override", json={"override": "original"}
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_translation_override_original_clears_and_keeps(client, db, seeded_post):
|
||||
# 'keep original' clears a stored translation immediately (no Interpreter) and
|
||||
# marks the post handled (== target). Asserts on the response, which reflects
|
||||
# the endpoint's own committed session.
|
||||
_, _, post = seeded_post
|
||||
post.post_title_translated = "STALE"
|
||||
post.description_translated = "STALE"
|
||||
post.translated_source_lang = "de"
|
||||
await db.commit()
|
||||
resp = await client.post(
|
||||
f"/api/posts/{post.id}/translation-override", json={"override": "original"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = await resp.get_json()
|
||||
assert body["translation_override"] == "original"
|
||||
assert body["applied"] == "cleared"
|
||||
assert body["post_title_translated"] is None
|
||||
assert body["translated_source_lang"] == "en"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_translation_override_force_queues_when_disabled(client, seeded_post):
|
||||
# Translation disabled (default) → can't translate inline; the override is
|
||||
# saved and the post is queued (columns NULLed) for the next sweep.
|
||||
_, _, post = seeded_post
|
||||
resp = await client.post(
|
||||
f"/api/posts/{post.id}/translation-override", json={"override": "force"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = await resp.get_json()
|
||||
assert body["translation_override"] == "force"
|
||||
assert body["applied"] == "queued"
|
||||
assert body["translated_source_lang"] is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_exposes_translation_override(client, seeded_post):
|
||||
resp = await client.get("/api/posts")
|
||||
assert resp.status_code == 200
|
||||
body = await resp.get_json()
|
||||
assert body["items"][0]["translation_override"] == "auto" # default
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_detail_returns_uncapped_thumbnails(client, db):
|
||||
"""Feed query caps thumbnails at 6 for previews; detail endpoint
|
||||
|
||||
@@ -53,6 +53,26 @@ async def test_translation_settings_defaults_and_patch(client):
|
||||
"/api/settings/import", json={"interpreter_base_url": 123})).status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_translation_min_confidence_default_and_patch(client):
|
||||
# #155: the latin-script acceptance floor is an operator-tunable setting,
|
||||
# default 0.9 (stricter than the old hardcoded 0.8).
|
||||
body = await (await client.get("/api/settings/import")).get_json()
|
||||
assert body["translation_min_confidence"] == 0.9
|
||||
|
||||
ok = await client.patch(
|
||||
"/api/settings/import", json={"translation_min_confidence": 0.95}
|
||||
)
|
||||
assert ok.status_code == 200
|
||||
assert (await ok.get_json())["translation_min_confidence"] == 0.95
|
||||
|
||||
# Out-of-range + wrong-type are rejected.
|
||||
assert (await client.patch(
|
||||
"/api/settings/import", json={"translation_min_confidence": 1.5})).status_code == 400
|
||||
assert (await client.patch(
|
||||
"/api/settings/import", json={"translation_min_confidence": "high"})).status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_translation_status_defaults(client):
|
||||
# Off + no URL → no network health call, healthy False (#143).
|
||||
|
||||
@@ -314,6 +314,41 @@ def test_translate_posts_accepts_low_confidence_cjk(db_sync, monkeypatch):
|
||||
assert p.translated_source_lang == "zh"
|
||||
|
||||
|
||||
def test_translate_posts_force_override_translates_below_floor(db_sync, monkeypatch):
|
||||
# override='force' stores the translation even below the confidence floor —
|
||||
# rescuing a legitimately-foreign title the gate would otherwise skip.
|
||||
_patch(monkeypatch, db_sync)
|
||||
_mock_lang(monkeypatch, lang="de", conf=0.42) # below the 0.90 default floor
|
||||
a = _artist(db_sync)
|
||||
p = _post(db_sync, a.id, title="Kurz", ext="p1")
|
||||
p.translation_override = "force"
|
||||
_enable(db_sync)
|
||||
db_sync.commit()
|
||||
|
||||
translate_posts()
|
||||
db_sync.refresh(p)
|
||||
assert p.post_title_translated == "EN:Kurz" # forced through despite 0.42
|
||||
assert p.translated_source_lang == "de"
|
||||
|
||||
|
||||
def test_translate_posts_original_override_keeps_original(db_sync, monkeypatch):
|
||||
# override='original' never translates — even a confident (0.98) detection is
|
||||
# kept as the original. The sweep marks it handled (== target) and stores
|
||||
# nothing; Interpreter isn't consulted for the decision.
|
||||
_patch(monkeypatch, db_sync)
|
||||
_mock_lang(monkeypatch, lang="de", conf=0.98) # would be accepted under auto
|
||||
a = _artist(db_sync)
|
||||
p = _post(db_sync, a.id, title="Das Mädchen", ext="p1")
|
||||
p.translation_override = "original"
|
||||
_enable(db_sync)
|
||||
db_sync.commit()
|
||||
|
||||
translate_posts()
|
||||
db_sync.refresh(p)
|
||||
assert p.post_title_translated is None # kept original
|
||||
assert p.translated_source_lang == "en" # marked handled
|
||||
|
||||
|
||||
def _mock_new_model(monkeypatch, *, ver="v2"):
|
||||
"""Interpreter is up and translates with a NEW engine version."""
|
||||
monkeypatch.setattr("backend.app.tasks.translation.ic.health", lambda *a, **k: True)
|
||||
@@ -344,6 +379,25 @@ def test_retranslate_resets_and_reruns(db_sync, monkeypatch):
|
||||
assert p.translation_engine_version == "v2"
|
||||
|
||||
|
||||
def test_retranslate_leaves_keep_original_posts(db_sync, monkeypatch):
|
||||
# A 'keep original' post survives a Re-translate-all: _reset_translations skips
|
||||
# override='original', so it stays handled + untranslated instead of being
|
||||
# re-run (which would re-introduce the mis-flag the operator killed).
|
||||
_patch(monkeypatch, db_sync)
|
||||
_mock_new_model(monkeypatch)
|
||||
a = _artist(db_sync)
|
||||
p = _post(db_sync, a.id, title="ねこ", ext="p1")
|
||||
p.translation_override = "original"
|
||||
p.translated_source_lang = "en" # handled by a prior 'keep original' apply
|
||||
_enable(db_sync)
|
||||
db_sync.commit()
|
||||
|
||||
retranslate_posts(artist_ids=[a.id])
|
||||
db_sync.refresh(p)
|
||||
assert p.translated_source_lang == "en" # not reset
|
||||
assert p.post_title_translated is None # not re-translated
|
||||
|
||||
|
||||
def test_retranslate_artist_scope_leaves_others(db_sync, monkeypatch):
|
||||
# Scoping to one artist must not touch another artist's translations.
|
||||
_patch(monkeypatch, db_sync)
|
||||
|
||||
@@ -1,26 +1,39 @@
|
||||
"""Translation acceptance gate (#1376) — pure unit tests for ``_accept``, which
|
||||
decides whether curator stores Interpreter's translation or keeps the original.
|
||||
Curator does no detection of its own; it only thresholds Interpreter's reported
|
||||
detected language + confidence (Scribe rule 133). No DB, no network."""
|
||||
from backend.app.tasks.translation import _MIN_LATIN_CONFIDENCE, _accept
|
||||
"""Translation acceptance gate (#1376, #155) — pure unit tests for ``_accept``,
|
||||
which decides whether curator stores Interpreter's translation or keeps the
|
||||
original. Curator does no detection of its own; it only thresholds Interpreter's
|
||||
reported detected language + confidence against the operator-tunable floor
|
||||
(Scribe rule 133). No DB, no network."""
|
||||
from backend.app.tasks.translation import _DEFAULT_MIN_LATIN_CONFIDENCE, _accept
|
||||
|
||||
|
||||
def test_accept_trusts_cjk_regardless_of_confidence():
|
||||
# ja/ko/zh are script-detected — accepted even at a low reported number
|
||||
# (pure-kanji Japanese can come back labelled zh ~0.75, still translated).
|
||||
def test_accept_trusts_cjk_regardless_of_confidence_or_floor():
|
||||
# ja/ko/zh are script-detected — accepted even at a low reported number and
|
||||
# even against a high floor (pure-kanji Japanese can come back labelled zh
|
||||
# ~0.75, still translated).
|
||||
assert _accept("ja", None) is True
|
||||
assert _accept("ja", 0.10) is True
|
||||
assert _accept("ja", 0.10, 0.99) is True
|
||||
assert _accept("ko", 0.99) is True
|
||||
assert _accept("zh", 0.75) is True
|
||||
assert _accept("zh-CN", 0.20) is True # region suffix is still CJK
|
||||
|
||||
|
||||
def test_accept_requires_floor_for_latin():
|
||||
def test_accept_default_floor_is_strict():
|
||||
# Default floor is 0.90 (#155): the ~0.86 band — where genuine German AND
|
||||
# mis-flagged short English collide — is now REJECTED (the whole point of the
|
||||
# stricter default). Genuine high-confidence non-English (~1.0) still passes.
|
||||
assert _accept("de", 0.98) is True
|
||||
assert _accept("de", 0.86) is True # calibrated: ambiguous-but-correct German stays
|
||||
assert _accept("de", _MIN_LATIN_CONFIDENCE) is True # exactly at floor
|
||||
assert _accept("de", _MIN_LATIN_CONFIDENCE - 0.01) is False
|
||||
assert _accept("fr", 0.40) is False # short mis-flag
|
||||
assert _accept("de", 0.86) is False # the collision band, now cut
|
||||
assert _accept("de", _DEFAULT_MIN_LATIN_CONFIDENCE) is True # at floor
|
||||
assert _accept("de", _DEFAULT_MIN_LATIN_CONFIDENCE - 0.01) is False
|
||||
assert _accept("fr", 0.40) is False # short mis-flag
|
||||
|
||||
|
||||
def test_accept_honors_an_explicit_floor():
|
||||
# The floor is a parameter now (fed from ImportSettings). A looser floor keeps
|
||||
# the 0.86 band; a stricter one rejects even a fairly confident detection.
|
||||
assert _accept("de", 0.86, 0.80) is True
|
||||
assert _accept("de", 0.86, 0.95) is False
|
||||
assert _accept("de", 0.96, 0.95) is True
|
||||
|
||||
|
||||
def test_accept_is_case_insensitive():
|
||||
@@ -32,3 +45,4 @@ def test_accept_missing_confidence_fails_open():
|
||||
# No confidence reported → don't silently drop a translation.
|
||||
assert _accept("de", None) is True
|
||||
assert _accept("", None) is True
|
||||
assert _accept("de", None, 0.99) is True # fail-open beats a high floor
|
||||
|
||||
Reference in New Issue
Block a user