Compare commits

..
Author SHA1 Message Date
stumpylog f584454cf2 Chore: restrict uv lock environments to supported architectures
Limit the lock to Linux x86_64, Linux aarch64 and macOS arm64. Locked
versions are unchanged; only the unused wheel entries are removed.
2026-11-17 07:30:34 -08:00
8 changed files with 96 additions and 1316 deletions

No files matched your search

+3 -2
View File
@@ -151,8 +151,9 @@ typing = [
required-version = ">=0.9.0"
prerelease = "disallow"
environments = [
"sys_platform == 'darwin'",
"sys_platform == 'linux'",
"sys_platform == 'darwin' and platform_machine == 'arm64'",
"sys_platform == 'linux' and platform_machine == 'aarch64'",
"sys_platform == 'linux' and platform_machine == 'x86_64'",
]
package = false
[tool.uv.sources]
+2 -14
View File
@@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
from unittest import mock
import pytest
from asgiref.sync import async_to_sync
from rest_framework import status
from rest_framework.test import APIClient
from rest_framework.test import APITestCase
@@ -13,8 +12,6 @@ from paperless_testing.factories import UserFactory
from paperless_testing.permissions import grant_global
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from django.contrib.auth.models import User
from pytest_mock import MockerFixture
@@ -58,11 +55,6 @@ class TestChatStreamingViewInputValidation(APITestCase):
reaches the Django request the middleware sees.
"""
chunks = [f"token{i} " for i in range(40)]
async def answer() -> AsyncIterator[str]:
for chunk in chunks:
yield chunk
with (
mock.patch(
"documents.views.AIConfig",
@@ -70,7 +62,7 @@ class TestChatStreamingViewInputValidation(APITestCase):
),
mock.patch(
"paperless_ai.chat.stream_chat_with_documents",
return_value=answer(),
return_value=iter(chunks),
),
):
resp = self.client.post(
@@ -82,11 +74,7 @@ class TestChatStreamingViewInputValidation(APITestCase):
assert resp.status_code == status.HTTP_200_OK
assert not resp.has_header("Content-Encoding")
async def read_parts() -> list[bytes]:
return [part async for part in resp.streaming_content]
assert async_to_sync(read_parts)() == [c.encode() for c in chunks]
assert list(resp.streaming_content) == [c.encode() for c in chunks]
def test_missing_question_is_rejected(self) -> None:
with mock.patch(
+2 -1
View File
@@ -2474,7 +2474,7 @@ class ChatStreamingView(GenericAPIView[Any]):
user=request.user,
)
return StreamingHttpResponse(
response = StreamingHttpResponse(
stream_chat_with_documents(
query_str=question,
documents=documents,
@@ -2483,6 +2483,7 @@ class ChatStreamingView(GenericAPIView[Any]):
),
content_type="text/event-stream",
)
return response
@extend_schema_view(
+1 -4
View File
@@ -1,4 +1,3 @@
import asyncio
import ipaddress
import socket
from collections.abc import Collection
@@ -176,9 +175,7 @@ class PinnedHostAsyncHTTPTransport(httpx.AsyncHTTPTransport):
self.allow_internal = allow_internal
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
# Resolving the host blocks, so keep it off the event loop
request = await asyncio.to_thread(
_rewrite_request_to_pinned_ip,
request = _rewrite_request_to_pinned_ip(
request,
allow_internal=self.allow_internal,
)
-60
View File
@@ -1,19 +1,11 @@
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
from unittest import mock
import httpx
import pytest
from paperless.network import PinnedHostAsyncHTTPTransport
from paperless.network import PinnedHostHTTPTransport
from paperless.network import is_public_ip
if TYPE_CHECKING:
from pytest_mock import MockerFixture
def test_pinned_host_transport_blocks_internal_rebinding():
transport = PinnedHostHTTPTransport(allow_internal=False)
@@ -59,58 +51,6 @@ def test_pinned_host_transport_rewrites_to_vetted_ip():
assert response.status_code == 200
class TestPinnedHostAsyncHTTPTransport:
def test_blocks_internal_rebinding(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A hostname that resolves to a loopback address
WHEN:
- The async transport sends a request to it
THEN:
- The request is refused as a non-public address
"""
mocker.patch(
"paperless.network.resolve_hostname_ips",
return_value=["127.0.0.1"],
)
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "http://example.com/test")
with pytest.raises(httpx.ConnectError, match="non-public address"):
asyncio.run(transport.handle_async_request(request))
def test_rewrites_to_vetted_ip(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A hostname that resolves to a public address
WHEN:
- The async transport sends a request to it
THEN:
- The connection goes to that address, keeping the original Host
header and TLS SNI hostname
"""
mocker.patch(
"paperless.network.resolve_hostname_ips",
return_value=["93.184.216.34"],
)
sent = mocker.patch.object(
httpx.AsyncHTTPTransport,
"handle_async_request",
autospec=True,
side_effect=lambda _self, req: httpx.Response(200, request=req),
)
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "https://example.com:8443/test")
response = asyncio.run(transport.handle_async_request(request))
assert response.status_code == 200
rewritten = sent.call_args.args[1]
assert str(rewritten.url) == "https://93.184.216.34:8443/test"
assert rewritten.headers["Host"] == "example.com:8443"
assert rewritten.extensions["sni_hostname"] == "example.com"
@pytest.mark.parametrize(
"address",
[
+29 -57
View File
@@ -1,10 +1,7 @@
import json
import logging
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING
import sys
from asgiref.sync import sync_to_async
from django.db import connections
from django.db.models import QuerySet
from documents.models import Document
@@ -20,10 +17,6 @@ from paperless_ai.prompts.context import ChatQaPromptContext
from paperless_ai.prompts.context import ChatRefinePromptContext
from paperless_ai.prompts.render import render_prompt
if TYPE_CHECKING:
from llama_index.core.response_synthesizers import BaseSynthesizer
from llama_index.core.schema import NodeWithScore
logger = logging.getLogger("paperless_ai.chat")
CHAT_METADATA_DELIMITER = "\n\n__PAPERLESS_CHAT_METADATA__"
@@ -110,67 +103,35 @@ def _format_chat_metadata_trailer(references: list[dict[str, int | str]]) -> str
)
async def stream_chat_with_documents(
def stream_chat_with_documents(
query_str: str,
documents: QuerySet[Document],
*,
unrestricted: bool = False,
output_language: str | None = None,
) -> AsyncIterator[str]:
):
try:
async for chunk in _stream_chat_with_documents(
yield from _stream_chat_with_documents(
query_str,
documents,
unrestricted=unrestricted,
output_language=output_language,
):
yield chunk
)
except Exception as e:
logger.exception("Failed to stream document chat response: %s", e)
yield CHAT_ERROR_MESSAGE
async def _stream_chat_with_documents(
def _stream_chat_with_documents(
query_str: str,
documents: QuerySet[Document],
*,
unrestricted: bool = False,
output_language: str | None = None,
) -> AsyncIterator[str]:
prepared = await sync_to_async(_prepare_chat)(
query_str,
documents,
unrestricted=unrestricted,
output_language=output_language,
)
if prepared is None:
):
if not documents.exists():
yield CHAT_NO_CONTENT_MESSAGE
return
response_synthesizer, top_nodes, references = prepared
logger.debug("Document chat query: %s", query_str)
response_stream = await response_synthesizer.asynthesize(query_str, top_nodes)
async for chunk in response_stream.async_response_gen():
yield chunk
if references:
yield _format_chat_metadata_trailer(references)
def _prepare_chat(
query_str: str,
documents: QuerySet[Document],
*,
unrestricted: bool,
output_language: str | None,
) -> tuple["BaseSynthesizer", list["NodeWithScore"], list[dict[str, int | str]]] | None:
"""Everything before the answer streams, all of it blocking: ORM queries,
the index read lock and its thread-bound sqlite connection, the query
embedding, and building the LLM client. Returns None when there is no
content to answer from.
"""
if not documents.exists():
return None
from llama_index.core.prompts import PromptTemplate
from llama_index.core.response_synthesizers import get_response_synthesizer
@@ -189,6 +150,8 @@ def _prepare_chat(
# Hold the shared read lock only while reading the index, so the compaction
# swap cannot replace the database file while this connection is open.
# Everything after retrieval works on the nodes already in memory, so a slow
# LLM stream never holds the lock.
with read_store() as store:
if permitted_ids is not None:
filters = _permitted_documents_filter(store, permitted_ids)
@@ -216,26 +179,35 @@ def _prepare_chat(
if not top_nodes:
logger.warning("No nodes found for the given documents.")
return None
yield CHAT_NO_CONTENT_MESSAGE
return
client = AIClient()
references = _get_document_references(documents, top_nodes)
client = AIClient()
prompt_template = PromptTemplate(template=_build_chat_prompt(output_language))
refine_template = PromptTemplate(template=_build_refine_prompt(output_language))
response_synthesizer = get_response_synthesizer(
llm=client.llm,
prompt_helper=get_rag_prompt_helper(
chunk_size=config.llm_embedding_chunk_size,
context_size=config.llm_context_size,
),
text_qa_template=PromptTemplate(template=_build_chat_prompt(output_language)),
refine_template=PromptTemplate(
template=_build_refine_prompt(output_language),
),
text_qa_template=prompt_template,
refine_template=refine_template,
streaming=True,
)
# No ORM access happens while the answer streams, so hand the pooled DB
# connection back now rather than pin it for the whole stream. See #12976.
connections.close_all()
logger.debug("Document chat query: %s", query_str)
# Release the pooled DB connection for the slow streaming LLM response
# so it is not pinned for the whole stream; see paperless_ai.db and
# #12976.
with db_connection_released():
response_stream = response_synthesizer.synthesize(query_str, top_nodes)
for chunk in response_stream.response_gen:
yield chunk
sys.stdout.flush()
return response_synthesizer, top_nodes, references
if references:
yield _format_chat_metadata_trailer(references)
+26 -55
View File
@@ -4,12 +4,10 @@ import json
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import Any
from unittest.mock import AsyncMock
from unittest.mock import MagicMock
from unittest.mock import patch
import pytest
from asgiref.sync import async_to_sync
from django.db.models.signals import post_init
from django.utils import timezone
from llama_index.core import settings as llama_settings
@@ -28,7 +26,6 @@ from paperless_ai.chat import stream_chat_with_documents
from paperless_testing.factories import DocumentFactory
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from collections.abc import Iterator
from pathlib import Path
@@ -69,26 +66,6 @@ def assert_chat_output(
}
def collect(chunks: AsyncIterator[str]) -> list[str]:
"""Drain the chat stream from sync test code. Its blocking setup then runs
on the test's own thread, inside the test's DB transaction."""
async def drain() -> list[str]:
return [chunk async for chunk in chunks]
return async_to_sync(drain)()
def fake_response_stream(chunks: list[str]) -> MagicMock:
"""An AsyncStreamingResponse stand-in yielding ``chunks``."""
async def response_gen() -> AsyncIterator[str]:
for chunk in chunks:
yield chunk
return MagicMock(async_response_gen=response_gen)
def _fake_documents_queryset(pks: list[int]) -> MagicMock:
qs = MagicMock()
qs.exists.return_value = bool(pks)
@@ -193,19 +170,19 @@ def test_stream_chat_with_one_document_retrieval(
),
]
mock_response_stream = MagicMock()
mock_response_stream.response_gen = iter(["chunk1", "chunk2"])
mock_synthesizer = mock_get_response_synthesizer.return_value
mock_synthesizer.asynthesize = AsyncMock(
return_value=fake_response_stream(["chunk1", "chunk2"]),
)
mock_synthesizer.synthesize.return_value = mock_response_stream
with patch(
"llama_index.core.retrievers.VectorIndexRetriever",
return_value=mock_retriever_instance,
):
output = collect(stream_chat_with_documents("What is this?", documents))
output = list(stream_chat_with_documents("What is this?", documents))
mock_retriever_instance.retrieve.assert_called_once_with("What is this?")
mock_synthesizer.asynthesize.assert_awaited_once_with(
mock_synthesizer.synthesize.assert_called_once_with(
"What is this?",
mock_retriever_instance.retrieve.return_value,
)
@@ -259,19 +236,20 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non
MagicMock(metadata={"document_id": str(doc2.pk), "title": "Document 2"}),
]
mock_response_stream = MagicMock()
mock_response_stream.response_gen = iter(["chunk1", "chunk2"])
mock_synthesizer = mock_get_response_synthesizer.return_value
mock_synthesizer.asynthesize = AsyncMock(
return_value=fake_response_stream(["chunk1", "chunk2"]),
)
mock_synthesizer.synthesize.return_value = mock_response_stream
with patch(
"llama_index.core.retrievers.VectorIndexRetriever",
return_value=mock_retriever_instance,
):
output = collect(stream_chat_with_documents("What's up?", documents))
output = list(stream_chat_with_documents("What's up?", documents))
mock_retriever_instance.retrieve.assert_called_once_with("What's up?")
mock_synthesizer.asynthesize.assert_awaited_once_with(
mock_synthesizer.synthesize.assert_called_once_with(
"What's up?",
mock_retriever_instance.retrieve.return_value,
)
@@ -288,9 +266,7 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non
def test_stream_chat_empty_document_list() -> None:
with patch("paperless_ai.chat.load_or_build_index") as mock_load_index:
output = collect(
stream_chat_with_documents("Any info?", Document.objects.none()),
)
output = list(stream_chat_with_documents("Any info?", Document.objects.none()))
mock_load_index.assert_not_called()
assert output == ["Sorry, I couldn't find any content to answer your question."]
@@ -310,7 +286,7 @@ def test_stream_chat_no_matching_nodes() -> None:
mock_index.vector_store.get_nodes.return_value = []
mock_load_index.return_value = mock_index
output = collect(
output = list(
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
)
@@ -341,7 +317,7 @@ def test_stream_chat_unexpected_failure_returns_generic_error(caplog) -> None:
)
mock_retriever_cls.return_value = mock_retriever
output = collect(
output = list(
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
)
@@ -391,7 +367,7 @@ class TestStreamChatRetrieval:
) -> None:
doc = DocumentFactory.create(content="hello world")
# Nothing indexed for this document yet.
out = collect(
out = list(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=doc.pk),
@@ -414,7 +390,7 @@ class TestStreamChatRetrieval:
indexing.llm_index_add_or_update_document(included)
indexing.llm_index_add_or_update_document(excluded)
collect(
list(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=included.pk),
@@ -443,7 +419,7 @@ class TestStreamChatRetrieval:
document = DocumentFactory.create(content="indexed document content")
indexing.llm_index_add_or_update_document(document)
collect(
list(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=document.pk),
@@ -479,7 +455,7 @@ class TestStreamChatRetrieval:
deleted_at=timezone.now(),
)
collect(
list(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=kept.pk),
@@ -511,7 +487,7 @@ class TestStreamChatRetrieval:
indexing.llm_index_add_or_update_document(doc)
permitted = Document.objects.filter(pk__in=[docs[0].pk, docs[1].pk])
collect(chat.stream_chat_with_documents("question?", permitted))
list(chat.stream_chat_with_documents("question?", permitted))
metadata_filter = captured_filters[0].filters[0]
assert metadata_filter.operator == FilterOperator.NIN
@@ -544,21 +520,16 @@ class TestStreamChatRetrieval:
synthesizer = mocker.patch(
"llama_index.core.response_synthesizers.get_response_synthesizer",
).return_value
synthesizer.asynthesize = AsyncMock(
return_value=fake_response_stream(["chunk"]),
)
synthesizer.synthesize.return_value.response_gen = iter(["chunk"])
collect(
list(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=permitted_doc.pk),
),
)
synthesizer.asynthesize.assert_awaited_once_with(
"question?",
[permitted_node],
)
synthesizer.synthesize.assert_called_once_with("question?", [permitted_node])
@pytest.mark.django_db
def test_get_document_references_only_queries_referenced_documents(
@@ -641,9 +612,9 @@ class TestChatReadLock:
]
)
def asynthesize(*_args: object) -> MagicMock:
def synthesize(*_args: object) -> object:
events.append("synthesize")
return fake_response_stream(["chunk"])
return mocker.MagicMock(response_gen=iter(["chunk"]))
mocker.patch("paperless_ai.chat.read_store", fake_read_store)
mocker.patch("paperless_ai.chat.AIClient")
@@ -654,9 +625,9 @@ class TestChatReadLock:
)
mocker.patch(
"llama_index.core.response_synthesizers.get_response_synthesizer",
).return_value.asynthesize = AsyncMock(side_effect=asynthesize)
).return_value.synthesize.side_effect = synthesize
collect(
list(
stream_chat_with_documents(
"What is this?",
Document.objects.filter(pk=document.pk),
Generated
+33 -1123
View File
File diff suppressed because it is too large. Load diff