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

@@ -217,6 +217,7 @@ class BatchProcessingService:
detected_lang = row.get("_detected_lang", "und") or "und"
source_text = row.get("source_text", "")
is_same = row.get("_same_language")
needs_review = str(detected_lang).lower() == "und"
# target_sql: prefer approved_translation, then first cached value, then source
primary_cached = next(iter(cl.values()), "") if cl else ""
@@ -235,15 +236,22 @@ class BatchProcessingService:
if is_same and str(lc).lower() == str(detected_lang).lower():
# Same language: use source text as-is (no translation needed)
fv = source_text
elif cl and lc in cl:
fv = cl[lc]
elif cl:
cached_by_lang = {
str(code).lower(): value for code, value in cl.items()
}
cached_value = cached_by_lang.get(str(lc).lower())
if cached_value is None:
# No translation available — skip this language.
continue
fv = cached_value
else:
# No translation available — skip this language
continue
self.db.add(TranslationLanguage(
id=str(uuid.uuid4()), record_id=rec.id, language_code=lc,
source_language_detected=detected_lang, translated_value=fv,
final_value=fv, status="translated", needs_review=False,
final_value=fv, status="translated", needs_review=needs_review,
))
count += 1
return count