Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(resource): reject a source with no content at ingestion (#4571)

Zayn Jarvisa4aa04cfc66f
diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.py
index 9d644476c..f53f2e7a8 100644
--- a/openviking/service/resource_service.py
+++ b/openviking/service/resource_service.py
@@ -1727,10 +1727,15 @@ class ResourceService:
return completed
if task.status == TaskStatus.CANCELLED:
return {"status": "cancelled"}
- return {
+ failure: Dict[str, Any] = {
"status": "error",
"errors": [task.error],
}
+ if isinstance(task.result, dict):
+ code = task.result.get("code")
+ if isinstance(code, str) and code:
+ failure["code"] = code
+ return failure
async def _execute_resource_ingestion(
self,
diff --git a/openviking/service/task_tracker.py b/openviking/service/task_tracker.py
index cd1df387d..37ad6c4ec 100644
--- a/openviking/service/task_tracker.py
+++ b/openviking/service/task_tracker.py
@@ -540,9 +540,17 @@ class TaskTracker:
error: str,
account_id: Optional[str] = None,
user_id: Optional[str] = None,
+ *,
+ result: Optional[Dict[str, Any]] = None,
) -> None:
- """Record failure and finalize after owned work settles."""
- await self._record_outcome(task_id, account_id, user_id, error=error)
+ """Record failure and optional structured metadata, then finalize."""
+ await self._record_outcome(
+ task_id,
+ account_id,
+ user_id,
+ result=result,
+ error=error,
+ )
async def _record_outcome(
self,
diff --git a/openviking/storage/queuefs/add_resource_processor.py b/openviking/storage/queuefs/add_resource_processor.py
index 0312cb9b4..b95294593 100644
--- a/openviking/storage/queuefs/add_resource_processor.py
+++ b/openviking/storage/queuefs/add_resource_processor.py
@@ -24,6 +24,7 @@ from openviking.telemetry import (
)
from openviking.telemetry.request_wait_tracker import get_request_wait_tracker
from openviking.telemetry.resource_summary import record_resource_queue_metrics
+from openviking_cli.exceptions import OpenVikingError
from openviking_cli.session.user_id import UserIdentifier
from openviking_cli.utils.logger import get_logger
@@ -209,11 +210,14 @@ class AddResourceProcessor(DequeueHandlerBase):
)
if result.get("status") == "error":
errors = result.get("errors") or ["resource processing failed"]
+ code = result.get("code")
+ failure_result = {"code": code} if isinstance(code, str) and code else None
await tracker.fail(
msg.task_id,
"; ".join(str(error) for error in errors),
account_id=ctx.account_id,
user_id=ctx.user.user_id,
+ result=failure_result,
)
terminal = True
self.report_error("resource processing failed", data)
@@ -267,11 +271,15 @@ class AddResourceProcessor(DequeueHandlerBase):
self.report_success()
return None
except Exception as exc:
+ failure_result = (
+ {"code": exc.code} if isinstance(exc, OpenVikingError) and exc.code else None
+ )
await tracker.fail(
msg.task_id,
str(exc),
account_id=ctx.account_id,
user_id=ctx.user.user_id,
+ result=failure_result,
)
terminal = True
self.report_error(str(exc), data)
diff --git a/openviking/utils/media_processor.py b/openviking/utils/media_processor.py
index 7d51e0e4a..ad9db8f24 100644
--- a/openviking/utils/media_processor.py
+++ b/openviking/utils/media_processor.py
@@ -24,7 +24,7 @@ from openviking.server.local_input_guard import (
is_remote_resource_source,
looks_like_local_path,
)
-from openviking_cli.exceptions import PermissionDeniedError
+from openviking_cli.exceptions import InvalidArgumentError, PermissionDeniedError
from openviking_cli.utils.logger import get_logger
# All known valid extensions - only these should be stripped when getting stem
@@ -201,9 +201,52 @@ class UnifiedResourceProcessor:
)
resource = await self._get_accessor_registry().access(source, **kwargs)
- self._set_resolved_identity(resource, kwargs.get("source_name"))
+ try:
+ self._set_resolved_identity(resource, kwargs.get("source_name"))
+ self._reject_empty_resource(resource, kwargs.get("source_name"))
+ except Exception:
+ # Ownership transfers to the caller only after prepare succeeds.
+ # Until then, release temporary accessor results on every error.
+ resource.cleanup()
+ raise
return resource
+ @staticmethod
+ def _reject_empty_resource(resource: LocalResource, source_name: Optional[str]) -> None:
+ """Refuse a source with no content, wherever it was fetched from.
+
+ Every ingestion path reaches this method: prepare_durable_source
+ freezes a source here before the request touches the tree, and
+ process() calls it for anything that was not frozen earlier. An empty
+ file is refused once, at the point the bytes are first in hand, rather
+ than being parsed and indexed as an empty resource.
+
+ Directories are skipped: their size is not meaningful, and
+ directory_scan already drops zero-byte members. content/write is a
+ different path and still allows an empty file, because creating one
+ there is an explicit user action.
+ """
+ try:
+ if not resource.path.is_file() or resource.path.stat().st_size:
+ return
+ except OSError:
+ # An unreadable source is not this check's business; let the normal
+ # ingestion path report it.
+ return
+
+ meta = resource.meta
+ name = (
+ source_name
+ or meta.get("resolved_name")
+ or meta.get("original_filename")
+ or resource.path.name
+ )
+ raise InvalidArgumentError(
+ f"'{name}' is empty (0 bytes), so there is nothing to extract or index. "
+ "Add a resource with content, or use content/write if an empty file is "
+ "what you want."
+ )
+
async def process(
self,
source: str,
diff --git a/tests/api_test/resources/slow/test_temp_upload_deep.py b/tests/api_test/resources/slow/test_temp_upload_deep.py
index 0edb998eb..e62617fb8 100644
--- a/tests/api_test/resources/slow/test_temp_upload_deep.py
+++ b/tests/api_test/resources/slow/test_temp_upload_deep.py
@@ -55,13 +55,3 @@ class TestTempUploadDeep:
assert add_resp.status_code == 200, (
f"upload unicode filename should succeed, got {add_resp.status_code}"
)
-
- def test_upload_empty_file(self, api_client):
- with tempfile.TemporaryDirectory() as temp_dir:
- test_file = os.path.join(temp_dir, f"empty_{uuid.uuid4().hex[:8]}.md")
- with open(test_file, "w"):
- pass
- add_resp = api_client.add_resource(path=test_file, wait=True)
- assert add_resp.status_code == 200, (
- f"upload empty file should return 200, got {add_resp.status_code}"
- )
diff --git a/tests/api_test/scenarios/resources_retrieval/test_build_text_resources.py b/tests/api_test/scenarios/resources_retrieval/test_build_text_resources.py
index 8db0ab9fc..9710a8299 100644
--- a/tests/api_test/scenarios/resources_retrieval/test_build_text_resources.py
+++ b/tests/api_test/scenarios/resources_retrieval/test_build_text_resources.py
@@ -51,23 +51,16 @@ class TestBuildTextResources:
cleanup_temp_dir(temp_dir)
def test_build_empty_file(self, api_client):
- """TC-B15 空文件构建:验证空 .txt 文件添加不崩溃并返回合理结果"""
+ """TC-B15 空文件构建:验证空 .txt 文件被明确拒绝"""
test_file_path, temp_dir = create_test_file(content="", suffix=".txt")
try:
response = api_client.add_resource(path=test_file_path, wait=True)
- assert response.status_code == 200
-
+ assert response.status_code == 400, response.text
data = response.json()
- assert data.get("status") in ("ok", "error"), (
- f"空文件构建应返回 ok 或 error, 实际: {data.get('status')}"
- )
-
- if data.get("status") == "ok":
- result = data.get("result", {})
- root_uri = result.get("root_uri")
- if root_uri:
- assert_root_uri_valid(root_uri)
+ assert data["status"] == "error"
+ assert data["error"]["code"] == "INVALID_ARGUMENT"
+ assert "empty" in data["error"]["message"].lower()
- print("✓ TC-B15 空文件构建通过")
+ print("✓ TC-B15 空文件被拒绝并返回 INVALID_ARGUMENT")
finally:
cleanup_temp_dir(temp_dir)
diff --git a/tests/api_test/scenarios/resources_retrieval_slow/test_build_text_resources_slow.py b/tests/api_test/scenarios/resources_retrieval_slow/test_build_text_resources_slow.py
index 57985dd39..daa71e7ed 100644
--- a/tests/api_test/scenarios/resources_retrieval_slow/test_build_text_resources_slow.py
+++ b/tests/api_test/scenarios/resources_retrieval_slow/test_build_text_resources_slow.py
@@ -80,50 +80,6 @@ class TestBuildTextResourcesSlow:
finally:
cleanup_temp_dir(temp_dir)
- def test_build_empty_file(self, api_client):
- """TC-B15 空文件构建:验证空 .txt 文件添加后 root_uri 有效、fs_stat 可查、source_format 合理"""
- from build_test_helpers import assert_source_format
-
- test_file_path, temp_dir = create_test_file(content="", suffix=".txt")
- try:
- response = api_client.add_resource(path=test_file_path, wait=True)
- assert response.status_code == 200
-
- data = response.json()
- if data.get("status") == "error":
- error_msg = str(data.get("error", ""))
- assert "empty" in error_msg.lower() or "error" in error_msg.lower(), (
- f"空文件错误信息应包含 empty/error, 实际: {error_msg}"
- )
- print(f"✓ TC-B15 空文件构建通过(服务端拒绝空文件): {error_msg[:80]}")
- return
-
- assert data.get("status") == "ok"
-
- result = data.get("result", {})
- if isinstance(result, dict) and result.get("status") == "error":
- inner_errors = result.get("errors", [])
- inner_msg = " ".join(str(e) for e in inner_errors)
- assert (
- "empty" in inner_msg.lower()
- or "error" in inner_msg.lower()
- or "parse" in inner_msg.lower()
- ), f"空文件内层错误应包含 empty/error/parse, 实际: {inner_msg}"
- print(f"✓ TC-B15 空文件构建通过(内层解析错误): {inner_msg[:80]}")
- return
-
- root_uri = result.get("root_uri")
- assert_root_uri_valid(root_uri)
-
- stat_resp = api_client.fs_stat(root_uri)
- assert stat_resp.status_code == 200, f"空文件 fs_stat 应返回200, root_uri: {root_uri}"
-
- assert_source_format(api_client, root_uri, ["text", "markdown", ""])
-
- print(f"✓ TC-B15 空文件构建通过, root_uri: {root_uri}")
- finally:
- cleanup_temp_dir(temp_dir)
-
def test_build_raw_content(self, api_client):
"""TC-B14 原始内容字符串构建:验证纯文本内容写入文件后添加可检索"""
random_id = str(uuid.uuid4())[:8]
diff --git a/tests/parse/test_empty_source_rejection.py b/tests/parse/test_empty_source_rejection.py
new file mode 100644
index 000000000..3a7d64bcf
--- /dev/null
+++ b/tests/parse/test_empty_source_rejection.py
@@ -0,0 +1,44 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""Regression coverage for rejecting an empty fetched resource."""
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from openviking.parse.accessors.base import LocalResource, SourceType
+from openviking.utils.media_processor import UnifiedResourceProcessor
+from openviking_cli.exceptions import InvalidArgumentError
+
+
+def _processor(monkeypatch, resource: LocalResource) -> UnifiedResourceProcessor:
+ processor = UnifiedResourceProcessor.__new__(UnifiedResourceProcessor)
+ monkeypatch.setattr(
+ processor,
+ "_get_accessor_registry",
+ lambda: SimpleNamespace(access=AsyncMock(return_value=resource)),
+ raising=False,
+ )
+ return processor
+
+
+async def test_prepare_rejects_and_cleans_up_a_temporary_empty_file(monkeypatch, tmp_path):
+ downloaded = tmp_path / "tmp9f3a21"
+ downloaded.write_bytes(b"")
+ resource = LocalResource(
+ path=downloaded,
+ source_type=SourceType.HTTP,
+ original_source="https://example.com/quarterly-report.pdf",
+ meta={"original_filename": "quarterly-report.pdf"},
+ is_temporary=True,
+ )
+ processor = _processor(monkeypatch, resource)
+
+ with pytest.raises(InvalidArgumentError) as excinfo:
+ await processor.prepare("https://example.com/quarterly-report.pdf")
+
+ assert "quarterly-report.pdf" in str(excinfo.value)
+ assert "tmp9f3a21" not in str(excinfo.value)
+ assert not downloaded.exists()
diff --git a/tests/server/test_api_resources.py b/tests/server/test_api_resources.py
index 6832c1d1b..9b566dd5c 100644
--- a/tests/server/test_api_resources.py
+++ b/tests/server/test_api_resources.py
@@ -145,6 +145,41 @@ async def test_add_resource_with_wait(
assert "root_uri" in body["result"]
+async def test_add_resource_remote_empty_with_wait_returns_invalid_argument(
+ client: httpx.AsyncClient,
+ temp_dir,
+ monkeypatch,
+):
+ from openviking.parse.accessors.base import LocalResource, SourceType
+ from openviking.parse.accessors.http_accessor import HTTPAccessor
+
+ downloaded = temp_dir / "remote-empty.txt"
+ downloaded.write_bytes(b"")
+
+ async def return_empty_download(self, source, **kwargs):
+ return LocalResource(
+ path=downloaded,
+ source_type=SourceType.HTTP,
+ original_source=str(source),
+ meta={"original_filename": downloaded.name},
+ is_temporary=True,
+ )
+
+ monkeypatch.setattr(HTTPAccessor, "access", return_empty_download)
+
+ resp = await client.post(
+ "/api/v1/resources",
+ json={"path": "https://example.com/remote-empty.txt", "wait": True},
+ )
+
+ assert resp.status_code == 400, resp.text
+ body = resp.json()
+ assert body["status"] == "error"
+ assert body["error"]["code"] == "INVALID_ARGUMENT"
+ assert "empty" in body["error"]["message"].lower()
+ assert not downloaded.exists()
+
+
async def test_add_resource_forwards_args_to_service(
client: httpx.AsyncClient,
service,