feat(backend): deliver Phase 3 document and voice integration

Add authenticated upload, review, retry and knowledge-selection HTTP flows with bounded JSON processing and frozen OCR policy.

Preserve Gemini/OpenAI provider selection and fenced lifecycle semantics; retain FAQ preambles and uncertain vision candidates, and expose opt-in uncalibrated voice transport diagnostics.

Include frontend integration examples, aligned contracts and deterministic regressions. Validation: 1767 provider-free tests passed; all 404 PostgreSQL scenarios have passing evidence across the documented runs.
This commit is contained in:
11linear11
2026-10-09 17:43:52 -04:00
parent 9be47191d6
commit a73a496c54
67 changed files with 5988 additions and 234 deletions
+9 -5
View File
@@ -11,7 +11,7 @@ import unicodedata
from uuid import UUID
from zipfile import ZipFile
from .extraction_artifacts import extraction_profile_digest, validate_artifact, bounded_json_bytes, bounded_json_size, serialize_artifact
from .extraction_artifacts import extraction_profile_digest, validate_artifact, bounded_json_bytes, bounded_json_size, serialize_artifact, ocr_review_required
from .formats import DOCX_MIME, validate_docx, validate_image
from .normalization import normalize_search_text, normalized_inputs
from .types import ExtractedBlock, ExtractionEnvelope, ExtractionReviewAnnotation
@@ -230,6 +230,7 @@ class PageBlockExtractor:
self.owned()
self.profile_digest = extraction_profile_digest(profile) # legacy fails before any provider
self.original_hash, self.profile, self.reviewed = original_hash, profile, False
self.automatic_ocr = False
self.document_id, self.title = document_id, title
self.annotations = []
extraction = profile["extraction"]
@@ -273,7 +274,8 @@ class PageBlockExtractor:
blocks = tuple(b.model_copy(update={"ordinal": i}) for i, b in enumerate(blocks))
envelope = ExtractionEnvelope(document_id=document_id, title=title, original_sha256=original_hash,
extraction_profile_sha256=self.profile_digest, normalization_policy=profile["normalizer"],
complete=True, quality_decision="reviewed" if self.reviewed else "passed",
complete=True, quality_decision=("partially_reviewed" if self.reviewed and self.automatic_ocr
else "reviewed" if self.reviewed else "passed"),
review_annotations=tuple(self.annotations), blocks=blocks)
normalized_inputs(envelope, profile)
payload = serialize_artifact(envelope, profile)
@@ -795,7 +797,8 @@ class PageBlockExtractor:
data = {"artifact_schema": 1, "document_id": str(self.document_id), "title": self.title,
"original_sha256": self.original_hash, "extraction_profile_sha256": self.profile_digest,
"normalization_policy": self.profile["normalizer"], "complete": True,
"quality_decision": "reviewed" if self.reviewed else "passed", "warnings": [],
"quality_decision": ("partially_reviewed" if self.reviewed and getattr(self, "automatic_ocr", False)
else "reviewed" if self.reviewed else "passed"), "warnings": [],
"review_annotations": [a.model_dump(mode="json") for a in self.annotations],
"blocks": [dict(b.model_dump(mode="json"), ordinal=i) for i, b in enumerate(blocks)]}
bounded_json_size(data, self.profile["extraction"]["max_extraction_artifact_bytes"])
@@ -824,11 +827,12 @@ class PageBlockExtractor:
or b.page != page or not b.source_locator.startswith(locator + "/")
or unreadable(b.text)):
raise ExtractionError("Vision evidence identity, location or readability mismatch")
if any("unreadable" in w.lower() or "ناخوانا" in w for w in b.warnings):
raise ExtractionError("Vision reports unreadable source regions")
# Validate local source relationships before manually attesting to the result.
ExtractionEnvelope(document_id=UUID(int=0), title="validation", original_sha256=self.original_hash,
extraction_profile_sha256=self.profile_digest, normalization_policy=self.profile["normalizer"],
complete=True, quality_decision="passed",
blocks=tuple(blocks))
if not ocr_review_required(self.profile) and not any(b.warnings for b in blocks):
self.automatic_ocr = True
return blocks
return self._review(blocks, image, locator, reasons + ("Vision completeness and critical fields inspected",))