fix(vector): URL-encode DAV paths and unwrap TaskGroup exceptions
Two ingest-robustness fixes from card 309 (OHR-Bench smoke-test triage). WebDAV paths flowed through the client already URL-decoded (unquote on the PROPFIND/REPORT <d:href>, or raw MCP-tool input), so a '#' reached httpx as a URL fragment and silently truncated the request -> spurious 404 on otherwise valid files (e.g. law filenames with '#', commas, double/trailing spaces). Route every caller-path builder through a new _webdav_path helper that percent-encodes the path once (preserving separators); the MOVE/COPY Destination header is encoded too. Vector-sync runs inside anyio task groups, so a child-task failure surfaced as a BaseExceptionGroup whose str() is the useless "unhandled errors in a TaskGroup (N sub-exception)" -- hiding the real ConnectError operators need. Add format_exception_group to flatten the group to its leaf exceptions and use it at the broad catch/log sites in processor.py and oauth_sync.py. Refs: Deck board 12 card 309 (AC #4 filename handling, AC #2 observability). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
457c115ef4
commit
0388735593
@@ -510,3 +510,81 @@ def test_parse_search_response_decodes_non_ascii_paths(mocker):
|
||||
assert results[0]["href"] == "/remote.php/dav/files/testuser/学生邮箱/report.pdf"
|
||||
# name comes from <d:displayname>, which is not URL-encoded; sanity-check it.
|
||||
assert results[0]["name"] == "report.pdf"
|
||||
|
||||
|
||||
def _request_url(mock_http_client) -> str:
|
||||
"""Positional URL passed to the underlying httpx ``request`` call."""
|
||||
return mock_http_client.request.call_args[0][1]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_read_file_encodes_special_chars(mocker):
|
||||
"""read_file must percent-encode '#', commas, and spaces in the path (card 309).
|
||||
|
||||
Paths arrive already URL-decoded from PROPFIND/REPORT, so an unencoded '#'
|
||||
reaches httpx as a URL fragment and silently truncates the request → 404 on
|
||||
valid files (e.g. OHR-Bench law filenames). The outgoing request path must be
|
||||
percent-encoded.
|
||||
"""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.content = b"%PDF-1.4 data"
|
||||
mock_response.headers = {"content-type": "application/pdf"}
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
# Name with a '#', a comma, a double space and a trailing space before ".pdf".
|
||||
await client.read_file("law/ADMA BioManufacturing, LLC - Amendment #2 .pdf")
|
||||
|
||||
url = _request_url(mock_http_client)
|
||||
assert url.startswith("/remote.php/dav/files/testuser/")
|
||||
# The hazardous characters are encoded; path separators are preserved.
|
||||
assert "%23" in url # '#'
|
||||
assert "%2C" in url # ','
|
||||
assert "%20" in url # space
|
||||
assert "#" not in url
|
||||
assert ", " not in url
|
||||
assert "/law/" in url
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_read_file_ascii_path_unchanged(mocker):
|
||||
"""A plain ASCII path must pass through unchanged (no spurious encoding)."""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.content = b"data"
|
||||
mock_response.headers = {"content-type": "text/plain"}
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
await client.read_file("Documents/notes.txt")
|
||||
|
||||
assert (
|
||||
_request_url(mock_http_client)
|
||||
== "/remote.php/dav/files/testuser/Documents/notes.txt"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_move_resource_encodes_destination_header(mocker):
|
||||
"""The MOVE Destination header must be percent-encoded too (card 309)."""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.status_code = 201
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
await client.move_resource("a/old.pdf", "b/new #1.pdf")
|
||||
|
||||
call = mock_http_client.request.call_args
|
||||
# Source is the request path; destination is the header.
|
||||
assert call[0][1] == "/remote.php/dav/files/testuser/a/old.pdf"
|
||||
destination = call.kwargs["headers"]["Destination"]
|
||||
assert "%23" in destination
|
||||
assert "#" not in destination
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Unit tests for vector-sync error formatting (card 309)."""
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from nextcloud_mcp_server.vector._errors import format_exception_group
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_format_plain_exception_returns_repr():
|
||||
exc = httpx.ConnectError("Connection error")
|
||||
assert format_exception_group(exc) == repr(exc)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_format_exception_group_names_leaf_cause():
|
||||
"""A single-child group must surface the real ConnectError, not the group's
|
||||
useless 'unhandled errors in a TaskGroup' default message."""
|
||||
leaf = httpx.ConnectError("Connection error")
|
||||
group = BaseExceptionGroup("unhandled errors in a TaskGroup", [leaf])
|
||||
|
||||
formatted = format_exception_group(group)
|
||||
|
||||
assert "ConnectError" in formatted
|
||||
assert "unhandled errors in a TaskGroup" not in formatted
|
||||
assert "1 sub-exception" in formatted
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_format_nested_exception_group_flattens_all_leaves():
|
||||
inner = BaseExceptionGroup(
|
||||
"inner", [ValueError("bad value"), httpx.ConnectError("conn")]
|
||||
)
|
||||
outer = BaseExceptionGroup("outer", [inner, RuntimeError("boom")])
|
||||
|
||||
formatted = format_exception_group(outer)
|
||||
|
||||
assert "ValueError" in formatted
|
||||
assert "ConnectError" in formatted
|
||||
assert "RuntimeError" in formatted
|
||||
assert "3 sub-exception(s)" in formatted
|
||||
Reference in New Issue
Block a user