feat(ingestion): add opt-in best-effort document admission

Freeze temporary partial-extraction admission in new uploads. Bypass manual approval, retain recognition warnings, and accept ambiguous legal references during bounded chunking. Preserve strict defaults, immutable evidence and atomic publication. Expose effective policy and safe failure categories with regression coverage.
This commit is contained in:
amirhosein mazaheri
2026-10-13 14:09:21 +03:30
parent 18e595ac93
commit 90a164f5a6
18 changed files with 301 additions and 31 deletions
+15 -9
View File
@@ -10,7 +10,7 @@ from threading import Event, Thread
from .chunking import chunk_extraction
from .embeddings import EmbeddingError, stage_document
from .extraction import ExtractionError, ReviewRequired
from .extraction_artifacts import extraction_profile_digest, ocr_review_required
from .extraction_artifacts import extraction_profile_digest, ocr_review_required, allow_partial_extraction
from .review import FactualReviewRequired, prepare_chunks
from .operational_limits import validate_worker_durations
@@ -18,10 +18,13 @@ from .operational_limits import validate_worker_durations
logger = logging.getLogger(__name__)
def report(claim, stage, category):
def report(claim, stage, category, *, reason=None):
"""Only internal categories and durable IDs; never exception/provider text."""
logger.info(json.dumps(dict(document_id=str(claim.document.get("id")),
job_id=str(claim.job_id), stage=stage, category=category)))
event = dict(document_id=str(claim.document.get("id")),
job_id=str(claim.job_id), stage=stage, category=category)
if reason is not None:
event["reason"] = reason
logger.info(json.dumps(event))
class LostClaim(RuntimeError):
@@ -62,14 +65,17 @@ def validate_source(envelope, document, content):
"""Coverage checks independent of chunking and embedding settings."""
if envelope.title != document["title"]:
raise ExtractionError("Extraction title differs from immutable source")
partial = allow_partial_extraction(document["processing_profile"])
if envelope.quality_decision == "accepted_with_warnings" and not partial:
raise ExtractionError("Partial admission requires the frozen opt-in policy")
warnings = (*envelope.warnings, *(w for b in envelope.blocks for w in b.warnings))
from .extraction import unreadable, PLACEHOLDER
from .review import factual_groups
groups = factual_groups(envelope)
if any(unreadable(b.text) or (PLACEHOLDER.search(b.text) and b.group_id not in groups)
if any((unreadable(b.text) and not (partial and any(c.isalnum() for c in b.text))) or (PLACEHOLDER.search(b.text) and b.group_id not in groups)
for b in envelope.blocks):
raise ExtractionError("Unreadable or placeholder extracted evidence")
if warnings and envelope.quality_decision not in ("reviewed", "partially_reviewed"):
if warnings and not partial and envelope.quality_decision not in ("reviewed", "partially_reviewed"):
raise ExtractionError("Unreviewed extraction warnings")
mime = document["mime_type"]
if envelope.quality_decision == "partially_reviewed":
@@ -89,10 +95,10 @@ def validate_source(envelope, document, content):
if (source.page_count > document["processing_profile"]["extraction"]["max_pdf_pages"]
or {b.page for b in envelope.blocks} != set(range(1, source.page_count + 1))):
raise ExtractionError("Incomplete PDF source coverage")
if envelope.quality_decision not in ("reviewed", "partially_reviewed") and not automatic_ocr:
if envelope.quality_decision not in ("reviewed", "partially_reviewed") and not automatic_ocr and not partial:
raise ExtractionError("PDF extraction requires inspection")
elif mime in ("image/jpeg", "image/png"):
if (envelope.quality_decision != "reviewed" and not automatic_ocr) or any(
if (envelope.quality_decision != "reviewed" and not automatic_ocr and not partial) or any(
b.page is not None or b.method not in ("vision", "ocr") for b in envelope.blocks):
raise ExtractionError("Image extraction requires inspected complete evidence")
elif mime in ("text/markdown", "text/plain",
@@ -250,7 +256,7 @@ class Worker:
category = ("embedding_error" if isinstance(exc, EmbeddingError)
else "extraction_error" if isinstance(exc, ExtractionError)
else "storage_error" if isinstance(exc, OSError) else "validation_error")
report(claim, stage, category)
report(claim, stage, category, reason=getattr(exc, "reason_code", None))
except LostClaim:
report(claim, stage, "ownership_lost")
except Exception: