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

@@ -66,7 +66,7 @@ class TestCreateRecordsUndetectedLang:
assert langs[0].needs_review is True
def test_detected_lang_same_as_target_lang_identity(self, db_with_run):
"""When detected_lang matches target language code, value is source_text (identity mapping)."""
"""When detected_lang matches a target, source-language gets identity mapping (source_text)."""
session, run_id = db_with_run
svc = LLMTranslationService(session)
rows = _batch_rows(1, detected_lang="fr")
@@ -81,8 +81,14 @@ class TestCreateRecordsUndetectedLang:
assert "fr" in lang_codes
assert "de" in lang_codes
fr_lang = [l for l in langs if l.language_code == "fr"][0]
# fr translation value should be source_text (not the LLM response "Bonjour")
de_lang = [l for l in langs if l.language_code == "de"][0]
# Source-language entry carries source_text for _build_insert_rows metadata
assert fr_lang.translated_value == "Hello world 0"
assert fr_lang.final_value == "Hello world 0"
assert fr_lang.source_language_detected == "fr"
# Translated language entry carries LLM response
assert de_lang.translated_value == "Hallo"
assert de_lang.final_value == "Hallo"
def test_detected_lang_fallback_to_llm_response(self, db_with_run):
"""When _detected_lang is 'und', fall back to LLM response field."""