From 657409e15ff0ef15142f74f190bfcb9f0c0ee493 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 6 Oct 2026 03:37:48 +0200 Subject: [PATCH] feat(i18n): reject an answer that carries its source and then continues The gateway answered two sources with "\n---\n" and the whole answer was stored, so twenty languages shipped the English text as their own translation and the release gate stayed green. Every criterion passed it: echo compares for equality and the answer is longer, the markup comparison counts only []{}`*() so a rule of hyphens is invisible to it, and the rendering the server appended keeps the spans, the names and the length. echoes() reports the same "echo" criterion when the translation contains its whole source, under the floor the equality case already uses: a short source may legitimately survive inside a longer rendering, a sentence may not. reason() is the single source of truth for both ends, so the client now drops such an answer on arrival and the gate reports one that got in earlier; make i18n-prune clears those. Swept every core catalog: exactly the 21 damaged entries are reported, all as echo, and nothing else in the corpus. The old predicate returns no reason at all for the same input. Co-Authored-By: Claude Opus 5 (1M context) --- tests/unit/python/utils/i18n/test_damage.py | 46 +++++++++++++++++++++ utils/i18n/damage.py | 23 +++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/unit/python/utils/i18n/test_damage.py diff --git a/tests/unit/python/utils/i18n/test_damage.py b/tests/unit/python/utils/i18n/test_damage.py new file mode 100644 index 0000000000..80c9ddfa67 --- /dev/null +++ b/tests/unit/python/utils/i18n/test_damage.py @@ -0,0 +1,46 @@ +"""Unit tests for the criterion that condemns an answer carrying its source. + +The shape is one the gateway produced across twenty languages: the English +source, a markdown rule, then a rendering. Every other criterion passed it, +because the rendering keeps the spans and the names and the separator is made +of characters the markup comparison does not count. +""" + +from __future__ import annotations + +import unittest +from typing import ClassVar + +from utils.i18n.damage import reason + + +class TestEchoAndContinue(unittest.TestCase): + """A translation holding its whole source is the server failing, not translating.""" + + SOURCE: ClassVar[str] = ( + "Corporate design injection for NGINX-served apps, unifying light and " + "dark theming from OKLCH design tokens" + ) + RENDERING: ClassVar[str] = ( + "Corporate-Design-Injection für NGINX-bediente Anwendungen, vereinheitlicht " + "helles und dunkles Theming aus OKLCH-Design-Tokens" + ) + + def test_an_answer_that_prepends_its_source_is_condemned(self) -> None: + answer = f"{self.SOURCE}\n---\n\n{self.RENDERING}" + self.assertEqual(reason(self.SOURCE, answer, "de"), "echo") + + def test_an_answer_that_appends_its_source_is_condemned(self) -> None: + answer = f"{self.RENDERING}\n\n{self.SOURCE}" + self.assertEqual(reason(self.SOURCE, answer, "de"), "echo") + + def test_a_rendering_that_drops_the_source_is_kept(self) -> None: + self.assertEqual(reason(self.SOURCE, self.RENDERING, "de"), "") + + def test_a_short_source_may_live_inside_a_longer_rendering(self) -> None: + source = "Mail" + self.assertEqual(reason(source, "Mail-Konto des Benutzers", "de"), "") + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/i18n/damage.py b/utils/i18n/damage.py index 2f02e8ccd3..f868c62163 100644 --- a/utils/i18n/damage.py +++ b/utils/i18n/damage.py @@ -204,6 +204,27 @@ def collapsed(source: str, translation: str) -> bool: ) +def echoes(source: str, translation: str) -> bool: + """Return whether the translation carries its whole source and continues. + + A server that answers ``\\n---\\n`` passes every other + rule: the rendering it appended keeps the spans, the names and the markup, + and the separator is made of characters ``structure`` does not count. The + reader is then served the English text as its own translation. + + The floor is the one :func:`reason` applies to a plain echo, because the + risk it guards against is the same: a short source legitimately survives + inside a longer rendering, a sentence does not. + + Args: + source: the source message. + translation: what came back for it. + """ + if source not in translation: + return False + return sum(character.isalpha() for character in prose(source)) >= ECHO_FLOOR + + def structure(text: str) -> Counter: """Return the markup characters of ``text`` with their multiplicity. @@ -250,6 +271,8 @@ def reason(source: str, translation: str, language: str = "") -> str: if translation == source: alphabetic = sum(character.isalpha() for character in prose(source)) return "echo" if alphabetic >= ECHO_FLOOR else "" + if echoes(source, translation): + return "echo" if protected_spans(source) - protected_spans(translation): return "protected-span" if added_spans(source, translation):