Files
Axton Ji 04eb2ac82e fix: batch indexing journal writes for per-document status updates (#3137)
IndexingRun.document() re-reads the journal and atomically rewrites the
whole .indexing-run.json on every call. The LlamaIndex loader marks each
accepted source "embedding" in a synchronous loop on the event loop, and
publication marks every loaded Document (one per page) "completed" the
same way, so recording n statuses costs O(n^2) bytes of I/O with no
await in between. On a 4,320-document reindex this held the loop for
over 100 s, /health/ready stopped answering, and the desktop supervisor
killed the core.

Add IndexingRun.documents(sources, status, **details), which applies one
status to many sources under a single ownership check and a single
journal write, and use it from the loader, the LlamaIndex publication
step and the RAG service's indexed-file callback. document() now
delegates to it, so single updates behave as before.

Fixes #3046

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:56:02 +08:00

65 lines
2.1 KiB
Python

"""Optional parsing progress context, independent of knowledge storage."""
from __future__ import annotations
from contextvars import ContextVar
import re
from typing import Any, Protocol
class ParseProgress(Protocol):
def phase(self, name: str, **kwargs: Any) -> None: ...
def document(self, source: Any, status: str, **kwargs: Any) -> None: ...
def documents(self, sources: Any, status: str, **kwargs: Any) -> None: ...
def check(self) -> None: ...
_CURRENT: ContextVar[ParseProgress | None] = ContextVar("indexing_run", default=None)
class IndexingCancelled(RuntimeError):
pass
def current_run() -> ParseProgress | None:
return _CURRENT.get()
def _redact(detail: str) -> str:
text = re.sub(r"(?i)(bearer\s+)[^\s]+", r"\1[redacted]", str(detail))
text = re.sub(
r"(?i)((?:api[_-]?key|token|secret|password|signature|authorization)\s*[=:]\s*)[^\s&]+",
r"\1[redacted]",
text,
)
text = re.sub(
r'(?i)(["\']?(?:api[_-]?key|token|secret|password|signature|authorization)["\']?\s*[:=]\s*)["\'][^"\']*["\']',
r'\1"[redacted]"',
text,
)
text = re.sub(r"(https?://)[^/\s@]+:[^/\s@]+@", r"\1[redacted]@", text)
return text[-1600:]
def failure_code(exc: BaseException) -> str:
text = str(exc).casefold()
if isinstance(exc, (TimeoutError,)) or "timeout" in text or "timed out" in text:
return "timeout"
if isinstance(exc, MemoryError) or "out of memory" in text or "resource exhausted" in text:
return "resource_exhaustion"
if (
isinstance(exc, (FileNotFoundError, ModuleNotFoundError))
or "not installed" in text
or "cli_missing" in text
):
return "dependency_missing"
if "unsupported" in text or "doesn't support" in text:
return "unsupported_input"
if "download" in text:
return "model_download_failed"
if "embedding" in text or "vector" in text:
return "embedding_failed"
if "no content" in text or "no output" in text or "invalid" in text:
return "invalid_output"
return "runtime_failure"