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

@@ -4,7 +4,7 @@
# @TEST_EDGE: list_datasources_error -> 502
# @TEST_EDGE: datasource_columns_value_error -> 400
# @TEST_EDGE: datasource_columns_generic_error -> 502
# @TEST_EDGE: check_target_schema_error -> 200 with error field
# @TEST_EDGE: check_target_schema_error -> 502 with error detail
# @TEST_EDGE: list_jobs_value_error -> 400
# @TEST_EDGE: create_job_value_error -> 422
# @TEST_EDGE: update_job_value_error -> 404
@@ -206,7 +206,7 @@ class TestCheckTargetSchema:
assert "target_database_id" in resp.text
def test_check_target_schema_exception_handled(self):
"""Exception in validate_target_table_schema returns 200 with error field."""
"""Exception in validate_target_table_schema returns 502."""
with patch(
"src.api.routes.translate._job_routes.validate_target_table_schema",
side_effect=RuntimeError("Connection refused"),
@@ -217,10 +217,8 @@ class TestCheckTargetSchema:
"target_database_id": "db-1",
"target_table": "my_table",
})
assert resp.status_code == 200
data = resp.json()
assert data["table_exists"] is False
assert data["error"] is not None
assert resp.status_code == 502
assert "Connection refused" in resp.text
# =============================================================================