Closes the 12 MAJ raised by the release/0.5.0 audit pipeline (cf.
docs/audit/reports/release-0.5.0/summary.md → summary-reaudit.md).
Volet 1 — Reasoning architecture (audits 01/02/06/07 strengthening)
* Domain ports: LLMProvider, ReasoningRunner, ReasoningParseError
* Domain DTOs: LLMProviderType, ReasoningResult, ReasoningIteration
* infra/llm/ollama_provider.py — OllamaProvider with health_check
* infra/docling_agent_reasoning.py — runner adapter, encapsulates the
private _rag_loop call (tracked at docling-project/docling-agent#26),
commits OLLAMA_HOST once at boot (eliminates the per-request env race),
translates upstream IndexError into ReasoningParseError
* api/reasoning.py — zero coupling to docling-agent / mellea / docling-core,
consumes app.state.reasoning_runner via the port
* main.py — DI wires OllamaProvider + DoclingAgentReasoningRunner at boot
when REASONING_ENABLED=true and deps are importable
* Rename RAG_* env vars → REASONING_*, endpoint /rag → /reasoning,
type RAGResult → ReasoningResult, frontend feature flag wiring,
i18n strings, tests, docs (BREAKING — pre-1.0 surface, no external
consumers in production)
* 17 new tests: adapter unit tests with sys.modules stubs, OllamaProvider
httpx tests, R3 concurrent-host isolation, R6 multi-iteration trace
serialization, R13 Protocol conformance via isinstance
* E2E Karate scenario: nav-reasoning hidden when REASONING_ENABLED=false
* README — Live Reasoning section (env vars, archi, link to issue #26)
Bloc B — Security (audit 08, dev-only context)
* docker-compose.yml — DEV DEFAULTS header, OpenSearch DISABLE_SECURITY_PLUGIN
flagged as dev-only with link to OpenSearch security docs
* main.py — boot warning if NEO4J_URI is set with the default 'changeme'
password, so prod operators can't silently inherit it
Bloc C — DRY frontend (audit 05)
* shared/storage/keys.ts — STORAGE_KEYS centralised (theme, locale)
* features/settings/store.ts — dead apiUrl ref + orphan i18n keys removed
* api/schemas.py — DOCUMENT_STATUS_UPLOADED constant
Bloc D — Quality (audits 02/06/07/09/10/12)
* domain/ports.py — DocumentConverter.supports_page_batching property
(LSP fix, replaces isinstance(ServeConverter) check)
* domain/ports.py — VectorStore.ping() (encapsulation, replaces
_vector_store._client.info() reach-around)
* api/analyses.py + api/ingestion.py — path params {job_id} → {analysis_id}
aligned with the user-facing terminology (URLs unchanged)
* api/documents.py — Path.read_bytes() + generate_preview() wrapped in
asyncio.to_thread, unblocks the FastAPI event loop on /preview
* infra/docling_tree.py — PEP 604 union for isinstance (Ruff UP038)
* src/__tests__/integration/ — cross-feature integration test relocated
out of features/history/ so feature folders stay self-contained
* Tightened terminal `assert X is not None` checks (isinstance(.., datetime),
exact value comparisons)
Validation
* 446 backend pytest, 202 frontend vitest — all green
* ruff + ruff format + ESLint + Prettier + vue-tsc clean
* Re-audit verdict: 0 CRIT / 0 MAJ, score ~94/100, GO
Closes #200
129 lines
5 KiB
Python
129 lines
5 KiB
Python
"""docling-agent reasoning runner adapter.
|
|
|
|
Implements `ReasoningRunner` for an `OllamaProvider`-backed `LLMProvider`.
|
|
Encapsulates everything that talks to docling-agent / mellea so neither the
|
|
domain nor the API layer depends on those packages.
|
|
|
|
Why we still call the private `_rag_loop`: `DoclingRAGAgent.run()` wraps the
|
|
answer in a synthetic `DoclingDocument` and discards the iteration trace.
|
|
Tracked upstream at https://github.com/docling-project/docling-agent/issues/26
|
|
— switch to the public surface once the issue lands.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import os
|
|
|
|
from domain.ports import LLMProvider, ReasoningParseError
|
|
from domain.value_objects import (
|
|
LLMProviderType,
|
|
ReasoningIteration,
|
|
ReasoningResult,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def deps_present() -> bool:
|
|
"""Import-check for the heavy reasoning deps. Used by the DI wire-up to
|
|
decide whether to instantiate the runner at all (so the backend boots
|
|
cleanly when docling-agent + mellea aren't installed)."""
|
|
try:
|
|
import docling_agent.agents # noqa: F401
|
|
import mellea # noqa: F401
|
|
except ImportError:
|
|
return False
|
|
return True
|
|
|
|
|
|
class DoclingAgentReasoningRunner:
|
|
"""ReasoningRunner adapter wrapping docling-agent + mellea.
|
|
|
|
The provider's host is committed to the process-wide `OLLAMA_HOST` env
|
|
var at construction time — Ollama's Python client reads it on session
|
|
creation. Setting it once at boot (instead of per-request) eliminates the
|
|
cross-request race the previous implementation exposed.
|
|
"""
|
|
|
|
def __init__(self, provider: LLMProvider) -> None:
|
|
if provider.type is not LLMProviderType.OLLAMA:
|
|
raise NotImplementedError(
|
|
f"docling-agent v0.1.0 only supports Ollama, got provider type "
|
|
f"{provider.type!r}. See "
|
|
f"https://github.com/docling-project/docling-agent/issues/26"
|
|
)
|
|
self._provider = provider
|
|
self._deps_ok = deps_present()
|
|
# Commit the host at boot — concurrent `run()` calls then share the
|
|
# same value with no racy mutation.
|
|
os.environ["OLLAMA_HOST"] = provider.host
|
|
|
|
@property
|
|
def is_available(self) -> bool:
|
|
return self._deps_ok
|
|
|
|
async def run(
|
|
self,
|
|
*,
|
|
document_json: str,
|
|
query: str,
|
|
model_id: str | None = None,
|
|
) -> ReasoningResult:
|
|
if not self._deps_ok:
|
|
raise RuntimeError("docling-agent / mellea not importable — cannot run reasoning")
|
|
|
|
# Lazy imports keep the module loadable when deps are missing (the
|
|
# runner is only ever instantiated when `deps_present()` is True, but
|
|
# this also makes the import surface explicit).
|
|
from docling_agent.agents import DoclingRAGAgent
|
|
from docling_core.types.doc.document import DoclingDocument
|
|
from mellea.backends.model_ids import ModelIdentifier
|
|
|
|
raw_model_id = model_id or self._provider.default_model_id
|
|
# `DoclingRAGAgent` (pydantic) validates `model_id` strictly against
|
|
# `ModelIdentifier` from mellea. Wrapping on the Ollama axis is the
|
|
# only realizable path today (cf. LLMProvider docstring).
|
|
wrapped_model_id = ModelIdentifier(ollama_name=raw_model_id)
|
|
|
|
try:
|
|
doc = DoclingDocument.model_validate_json(document_json)
|
|
except Exception as e:
|
|
raise RuntimeError(f"Failed to parse document_json: {e}") from e
|
|
|
|
agent = DoclingRAGAgent(model_id=wrapped_model_id, tools=[])
|
|
logger.info(
|
|
"Reasoning run: model_id=%s ollama_host=%s query=%r",
|
|
raw_model_id,
|
|
self._provider.host,
|
|
query[:120],
|
|
)
|
|
|
|
try:
|
|
# `_rag_loop` is sync + LLM-heavy (N * model latency). Offload to
|
|
# a worker thread so concurrent calls don't block the event loop.
|
|
# Private API kept until docling-agent#26 lands.
|
|
raw_result = await asyncio.to_thread(agent._rag_loop, query=query, doc=doc)
|
|
except IndexError as e:
|
|
# docling-agent v0.1.0 bug: `_attempt_answer` / `_select_section`
|
|
# call `find_json_dicts(answer.value)[0]` without handling an
|
|
# empty list. When the model can't produce a parseable JSON after
|
|
# 3 rejection-sampling retries + 3 `select_from_failure` retries,
|
|
# the list is empty and `[0]` raises IndexError. Translate to a
|
|
# domain-level error the API can map to 502.
|
|
logger.warning(
|
|
"docling-agent produced no parseable JSON for model=%s query=%r",
|
|
raw_model_id,
|
|
query[:120],
|
|
)
|
|
raise ReasoningParseError(
|
|
model_id=raw_model_id,
|
|
reason="no parseable answer after retries",
|
|
) from e
|
|
|
|
return ReasoningResult(
|
|
answer=raw_result.answer,
|
|
iterations=[ReasoningIteration(**it.model_dump()) for it in raw_result.iterations],
|
|
converged=raw_result.converged,
|
|
)
|