fix(translate,scheduler): language detection, LLM error handling, and scheduler persistence

Translation pipeline fixes (from production error log analysis):

Lang detect:
- Add "ru" to _COMMON_SOURCE_CODES for Cyrillic source detection
- Version detector cache keys (v2:) and include detector version in
  source hash — stale cache entries from old algorithm are invalidated
- Integrate _character_block_fallback into batch_detect() pipeline;
  only assign Cyrillic fallback when exactly one Cyrillic target exists
  (multiple ru/uk/be targets stay undetermined for LLM arbitration)

Batch processing:
- Case-insensitive cache language matching (cached_by_lang lookup)
- Propagate needs_review=True for undetected language rows in pre/cache path

LLM call (critical — fixes silent error hiding):
- Validate LLM row IDs against expected set; log unknown identifiers
- Retry only missing rows on incomplete response (bounded by recursion depth)
- Exhausted retries → FAILED (new _handle_incomplete_response method)
- Parse failures → FAILED instead of SKIPPED (_handle_parse_failure)
- NULL/Empty translations → FAILED via new _add_failed helper
- Source-language identity mapping preserved as TranslationLanguage entry
  (carries source_language_detected metadata that _build_insert_rows
  relies on for detected_src_lang derivation)
- finish_reason propagated to parser for truncation diagnostics

LLM parse:
- Structured incomplete-set logging with expected/received/missing counts

Target schema validation:
- Handle all non-success SQL Lab statuses (failed, timeout, error, stopped)
- Timeout gets explicit message prefix
- Route returns HTTP 502 with correct HTTPException passthrough

Scheduler persistence fix (production pickle error):
- Backup and validation APScheduler jobs now use module-level callbacks
  (execute_scheduled_backup, execute_scheduled_validation) instead of
  bound SchedulerService methods — prevents pickle failure from
  serializing TaskManager + dynamically loaded plugin classes
- Callback func identity verified via pickle round-trip smoke test

Tests:
- Update assertions: FAILED replaces SKIPPED for LLM error paths
- Restore source-language contract tests with identity-mapped values
- Add scheduler callback identity and args verification tests
- Update detector cache key tests for versioned format
- Update target schema error route test: 200→502

Orthogonal code review: MEDIUM finding (empty rec.languages when all
targets match detected source → _build_insert_rows fallback to "und")
fixed by preserving source-language TranslationLanguage entries.
This commit is contained in:
2026-07-15 13:07:41 +03:00
parent 8f4ee25415
commit 9922d7c87a
14 changed files with 283 additions and 66 deletions

View File

@@ -12,6 +12,7 @@ from unittest.mock import MagicMock, patch
from lingua import LanguageDetectorBuilder
from src.plugins.translate._lang_detect import (
_DETECTOR_VERSION,
_detector_cache_key,
build_detector,
get_detector,
@@ -26,22 +27,22 @@ class TestDetectorCacheKey:
"""_detector_cache_key."""
def test_none(self):
"""None returns '__default'."""
assert _detector_cache_key(None) == "__default"
"""None returns a versioned default key."""
assert _detector_cache_key(None) == f"v{_DETECTOR_VERSION}:__default"
def test_empty_list(self):
"""Empty list returns '__default'."""
assert _detector_cache_key([]) == "__default"
"""Empty list returns a versioned default key."""
assert _detector_cache_key([]) == f"v{_DETECTOR_VERSION}:__default"
def test_sorted_key(self):
"""Languages are sorted for deterministic key."""
key = _detector_cache_key(["fr", "en", "de"])
assert key == "de,en,fr"
assert key == f"v{_DETECTOR_VERSION}:de,en,fr"
def test_duplicates(self):
"""Duplicates are removed."""
key = _detector_cache_key(["en", "en", "fr"])
assert key == "en,fr"
assert key == f"v{_DETECTOR_VERSION}:en,fr"
class TestBuildDetector: