Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(api): 修正输入错误与模型失败的 HTTP 状态码 (#6045)
Qin Haojie3385dd5966df
diff --git a/openviking/core/skill_loader.py b/openviking/core/skill_loader.pyindex d0b3a8a87..4b6565940 100644--- a/openviking/core/skill_loader.py+++ b/openviking/core/skill_loader.py@@ -70,7 +70,10 @@ class SkillLoader:if not frontmatter:raise ValueError("SKILL.md must have YAML frontmatter")- meta = yaml.safe_load(frontmatter)+ try:+ meta = yaml.safe_load(frontmatter)+ except yaml.YAMLError as exc:+ raise ValueError(f"Invalid YAML frontmatter: {exc}") from excif not isinstance(meta, dict):raise ValueError("Invalid YAML frontmatter")diff --git a/openviking/parse/parsers/zip_parser.py b/openviking/parse/parsers/zip_parser.pyindex 370c03d7a..b045ce623 100644--- a/openviking/parse/parsers/zip_parser.py+++ b/openviking/parse/parsers/zip_parser.py@@ -17,6 +17,7 @@ from typing import List, Optional, Unionfrom openviking.parse.base import ParseResultfrom openviking.parse.parsers.base_parser import BaseParserfrom openviking.utils.zip_safe import safe_extract_zip+from openviking_cli.exceptions import InvalidArgumentErrorfrom openviking_cli.utils.logger import get_loggerlogger = get_logger(__name__)@@ -62,20 +63,16 @@ class ZipParser(BaseParser):if not path.exists():raise FileNotFoundError(f"ZIP file not found: {path}")- # Check if it's a valid ZIP file (non-blocking)- def _is_zipfile() -> bool:- return zipfile.is_zipfile(path)-- if not await asyncio.to_thread(_is_zipfile):- raise ValueError(f"Not a valid ZIP file: {path}")-# Extract ZIP to temporary directory (non-blocking)temp_dir = Path(await asyncio.to_thread(tempfile.mkdtemp, prefix="ov_zip_"))try:# Extract zip (non-blocking)def _extract_zip():- with zipfile.ZipFile(path, "r") as zipf:- safe_extract_zip(zipf, temp_dir)+ try:+ with zipfile.ZipFile(path, "r") as zipf:+ safe_extract_zip(zipf, temp_dir)+ except zipfile.BadZipFile as exc:+ raise InvalidArgumentError(f"Not a valid ZIP file: {path.name}") from excawait asyncio.to_thread(_extract_zip)diff --git a/openviking/server/error_mapping.py b/openviking/server/error_mapping.pyindex 856f96ba7..8766f5819 100644--- a/openviking/server/error_mapping.py+++ b/openviking/server/error_mapping.py@@ -5,6 +5,8 @@ import astimport refrom typing import Any, Iterator+import httpx+from openviking.pyagfs.exceptions import (AGFSAlreadyExistsError,AGFSClientError,@@ -30,7 +32,11 @@ from openviking.pyagfs.exceptions import (GitConcurrentCommitError,)from openviking.storage.errors import LockAcquisitionError, ResourceBusyError-from openviking.utils.exceptions import HTTP_STATUS_TO_ERROR_CODE, error_code_from_http_status+from openviking.utils.exceptions import (+ HTTP_STATUS_TO_ERROR_CODE,+ AllCredentialsFailedError,+ error_code_from_http_status,+)from openviking_cli.exceptions import (ConflictError,FailedPreconditionError,@@ -335,6 +341,21 @@ def _build_upstream_error(def _map_upstream_api_error(exc: Exception) -> OpenVikingError | None:+ for item in _iter_exception_chain(exc):+ if isinstance(item, AllCredentialsFailedError):+ failures = [_map_upstream_api_error(error) for _, _, error, _ in item.errors]+ codes = {failure.code if failure is not None else "INTERNAL" for failure in failures}+ # A mixed failure has no single accurate status. Do not infer one+ # from whichever credential happens to appear first in the message.+ if len(codes) != 1 or "INTERNAL" in codes:+ return InternalError(str(exc))+ return _build_upstream_error(+ code=codes.pop(),+ message=_upstream_detail_message(exc),+ source=item,+ vlm_context=exc,+ )+if _is_model_api_key_configuration_error(exc):return FailedPreconditionError("Model provider API key is not configured",@@ -343,6 +364,19 @@ def _map_upstream_api_error(exc: Exception) -> OpenVikingError | None:status, source = _extract_structured_http_status(exc)if status is None:+ for item in _iter_exception_chain(exc):+ if isinstance(item, httpx.TimeoutException):+ code = "DEADLINE_EXCEEDED"+ elif isinstance(item, (httpx.NetworkError, httpx.RemoteProtocolError)):+ code = "UNAVAILABLE"+ else:+ continue+ return _build_upstream_error(+ code=code,+ message=_upstream_detail_message(exc),+ source=item,+ vlm_context=exc,+ )status = _extract_text_http_status(exc)if status is not None:code = error_code_from_http_status(status, default="UNKNOWN")@@ -475,12 +509,9 @@ def map_exception(return PermissionDeniedError(str(exc), resource=resource)if isinstance(exc, FileNotFoundError):return _not_found_error(resource, resource_type)- if _is_model_api_key_configuration_error(exc):- return FailedPreconditionError(- "Model provider API key is not configured",- details={"reason": _trim_message(_exception_chain_text(exc))},- )if isinstance(exc, ValueError):+ if _is_model_api_key_configuration_error(exc):+ return _map_upstream_api_error(exc)message = str(exc)if is_invalid_uri_error(exc):return InvalidURIError(resource or message, message)diff --git a/openviking/service/search_service.py b/openviking/service/search_service.pyindex 8d7c8cb70..840d72bc6 100644--- a/openviking/service/search_service.py+++ b/openviking/service/search_service.py@@ -27,18 +27,22 @@ if TYPE_CHECKING:logger = get_logger(__name__)-def _ensure_non_empty_query(+def _validate_query(query: str,image_url: Optional[str] = None,filter: Optional[Dict] = None,) -> None:- """Reject a request that gives the search nothing to work with.+ """Reject malformed query text or a request with nothing to search for.A filter is an acceptable substitute for a query: the result set is thenfully determined by the filter, which is exactly what an exact-match lookup(e.g. by a tag carrying an external id) needs. Without either one the callwould return an arbitrary slice of the whole store."""+ try:+ query.encode("utf-8")+ except UnicodeEncodeError as exc:+ raise InvalidArgumentError("Search query must contain valid Unicode text.") from excif query.strip() or image_url or filter:returnraise InvalidArgumentError(@@ -137,7 +141,7 @@ class SearchService:if search_type == "keywords" and image_url:raise InvalidArgumentError("image_url is not supported when search_type='keywords'")resolved_image_url = await self._resolve_image_url(image_url, ctx)- _ensure_non_empty_query(query, resolved_image_url)+ _validate_query(query, resolved_image_url)if search_type == "keywords":await self.ensure_keywords_search_supported(ctx)viking_fs = self._ensure_initialized()@@ -192,7 +196,7 @@ class SearchService:if search_type == "keywords" and image_url:raise InvalidArgumentError("image_url is not supported when search_type='keywords'")resolved_image_url = await self._resolve_image_url(image_url, ctx)- _ensure_non_empty_query(query, resolved_image_url, filter)+ _validate_query(query, resolved_image_url, filter)viking_fs = self._ensure_initialized()result = await viking_fs.find(query=query,@@ -225,7 +229,7 @@ class SearchService:from openviking.retrieve.skill_results import SkillResultResolverfrom openviking_cli.retrieve import ContextType, FindResult, TypedQuery- _ensure_non_empty_query(query)+ _validate_query(query)fs = self._ensure_initialized()targets = resolve_retrieval_targets(target_uri, ctx).target_directoriesfor target in targets:diff --git a/openviking/storage/viking_fs/_grep.py b/openviking/storage/viking_fs/_grep.pyindex ad8f79331..ed7f3b38d 100644--- a/openviking/storage/viking_fs/_grep.py+++ b/openviking/storage/viking_fs/_grep.py@@ -13,7 +13,7 @@ from openviking.pyagfs.exceptions import AGFSNotSupportedErrorfrom openviking.server.identity import RequestContextfrom openviking.storage.expr import And, PathScope, RawDSLfrom openviking.storage.viking_fs._base import logger-from openviking_cli.exceptions import PermissionDeniedError+from openviking_cli.exceptions import InvalidArgumentError, PermissionDeniedErrorfrom openviking_cli.utils.config.grep_config import GrepEngine_GREP_LS_PAGE_SIZE = 1000@@ -112,35 +112,38 @@ class _GrepMixin:if record.get("uri")}- if resolved_engine == "fs":- result = await self._grep_fs(- uri=uri,- pattern=pattern,- exclude_uri=exclude_uri,- case_insensitive=case_insensitive,- node_limit=node_limit,- level_limit=level_limit,- ctx=ctx,- content_transform=content_transform,- allowed_uris=allowed_uris,- before_context=before_context,- after_context=after_context,- )- else: # "vikingdb_then_fs"- result = await self._grep_vikingdb_then_fs(- uri=uri,- pattern=pattern,- exclude_uri=exclude_uri,- case_insensitive=case_insensitive,- node_limit=node_limit,- level_limit=level_limit,- ctx=ctx,- allowed_uris=allowed_uris,- tag_filter=tag_filter,- include_tags=include_tags,- before_context=before_context,- after_context=after_context,- )+ try:+ if resolved_engine == "fs":+ result = await self._grep_fs(+ uri=uri,+ pattern=pattern,+ exclude_uri=exclude_uri,+ case_insensitive=case_insensitive,+ node_limit=node_limit,+ level_limit=level_limit,+ ctx=ctx,+ content_transform=content_transform,+ allowed_uris=allowed_uris,+ before_context=before_context,+ after_context=after_context,+ )+ else: # "vikingdb_then_fs"+ result = await self._grep_vikingdb_then_fs(+ uri=uri,+ pattern=pattern,+ exclude_uri=exclude_uri,+ case_insensitive=case_insensitive,+ node_limit=node_limit,+ level_limit=level_limit,+ ctx=ctx,+ allowed_uris=allowed_uris,+ tag_filter=tag_filter,+ include_tags=include_tags,+ before_context=before_context,+ after_context=after_context,+ )+ except re.error as exc:+ raise InvalidArgumentError(f"Invalid grep pattern: {exc}") from excreturn self._attach_grep_tags(result, tags_by_uri)async def _resolve_grep_engine(diff --git a/tests/server/test_api_resources.py b/tests/server/test_api_resources.pyindex 7c7b7a5a0..fe9ec50ea 100644--- a/tests/server/test_api_resources.py+++ b/tests/server/test_api_resources.py@@ -548,23 +548,27 @@ async def test_add_skill_business_error_uses_error_envelope(assert body["error"]["message"] == "Skill parse error: boom"-async def test_add_skill_missing_name_returns_invalid_argument(client: httpx.AsyncClient):- resp = await client.post(- "/api/v1/skills",- json={- "data": {- "description": "Skill without name",- "content": "# No Name Skill\nTest content.",- },- },- )+ ("data", "message"),+ [+ (+ {"description": "Skill without name", "content": "# No Name"},+ "Skill must have 'name' field",+ ),+ ("---\nname: [broken\ndescription: invalid YAML\n---\n# Skill", "Invalid YAML frontmatter"),+ ],+)+async def test_add_skill_invalid_content_returns_invalid_argument(+ client: httpx.AsyncClient, data, message+):+ resp = await client.post("/api/v1/skills", json={"data": data})assert resp.status_code == 400body = resp.json()assert body["status"] == "error"assert "result" not in body or body["result"] is Noneassert body["error"]["code"] == "INVALID_ARGUMENT"- assert body["error"]["message"] == "Skill must have 'name' field"+ assert message in body["error"]["message"]async def test_add_skill_empty_dict_returns_invalid_argument(client: httpx.AsyncClient):@@ -645,14 +649,28 @@ async def test_add_resource_with_to(assert "custom" in body["result"]["root_uri"][email protected]("archive_state", ["valid", "invalid", "bad_crc", "io_error"])async def test_add_resource_with_resources_root_to_uses_child_uri(client: httpx.AsyncClient,upload_temp_dir,+ monkeypatch,+ archive_state,):archive_path = upload_temp_dir / "tt_b.zip"with zipfile.ZipFile(archive_path, "w") as zf:zf.writestr("tt_b/bb/readme.md", "# hello\n")+ if archive_state == "invalid":+ archive_path.write_bytes(b"not a zip archive")+ elif archive_state == "bad_crc":+ archive_path.write_bytes(archive_path.read_bytes().replace(b"# hello", b"# wrong"))+ elif archive_state == "io_error":++ def fail_to_open(*args, **kwargs):+ raise OSError("disk unavailable")++ monkeypatch.setattr(zipfile, "ZipFile", fail_to_open)+resp = await client.post("/api/v1/resources",json={@@ -662,6 +680,14 @@ async def test_add_resource_with_resources_root_to_uses_child_uri("wait": True,},)+ if archive_state != "valid":+ is_bad_input = archive_state != "io_error"+ assert resp.status_code == (400 if is_bad_input else 500), resp.text+ error = resp.json()["error"]+ assert error["code"] == ("INVALID_ARGUMENT" if is_bad_input else "PROCESSING_ERROR")+ assert ("Not a valid ZIP file" if is_bad_input else "disk unavailable") in error["message"]+ return+assert resp.status_code == 200body = resp.json()assert body["status"] == "ok"diff --git a/tests/server/test_api_search.py b/tests/server/test_api_search.pyindex db59f9966..4d79e52e4 100644--- a/tests/server/test_api_search.py+++ b/tests/server/test_api_search.py@@ -3,6 +3,7 @@"""Tests for search endpoints: find, search, grep, glob."""+import jsonfrom datetime import datetime, timezonefrom types import SimpleNamespace@@ -597,50 +598,38 @@ async def test_find_no_results(client: httpx.AsyncClient):assert resp.json()["status"] == "ok"[email protected]("query", ["", " \t\n"])-async def test_find_rejects_empty_query(client: httpx.AsyncClient, service, query: str):- class RaisingEmbedder:- def embed(self, text: str, is_query: bool = False) -> EmbedResult:- raise AssertionError("empty query should not be embedded")-- async def embed_async(self, text: str, is_query: bool = False) -> EmbedResult:- raise AssertionError("empty query should not be embedded")-- service.viking_fs.query_embedder = RaisingEmbedder()-- resp = await client.post(- "/api/v1/search/find",- json={"query": query},- )-- assert resp.status_code == 400- body = resp.json()- assert body["status"] == "error"- assert body["error"]["code"] == "INVALID_ARGUMENT"- assert "must not be empty" in body["error"]["message"]--[email protected]("query", ["", " \t\n"])-async def test_search_rejects_empty_query(client: httpx.AsyncClient, service, query: str):[email protected]("endpoint", ["find", "search"])+ ("query", "message"),+ [+ ("", "must not be empty"),+ (" \t\n", "must not be empty"),+ ("broken\ud8b7", "valid Unicode"),+ ],+)+async def test_search_rejects_invalid_query(+ client: httpx.AsyncClient, service, endpoint, query, message+):class RaisingEmbedder:def embed(self, text: str, is_query: bool = False) -> EmbedResult:- raise AssertionError("empty query should not be embedded")+ raise AssertionError("invalid query should not be embedded")async def embed_async(self, text: str, is_query: bool = False) -> EmbedResult:- raise AssertionError("empty query should not be embedded")+ raise AssertionError("invalid query should not be embedded")service.viking_fs.query_embedder = RaisingEmbedder()resp = await client.post(- "/api/v1/search/search",- json={"query": query},+ f"/api/v1/search/{endpoint}",+ content=json.dumps({"query": query}),+ headers={"Content-Type": "application/json"},)assert resp.status_code == 400body = resp.json()assert body["status"] == "error"assert body["error"]["code"] == "INVALID_ARGUMENT"- assert "must not be empty" in body["error"]["message"]+ assert message in body["error"]["message"]@pytest.mark.parametrize("method_name", ["find", "search"])diff --git a/tests/server/test_error_mapping.py b/tests/server/test_error_mapping.pyindex 0bfa23407..33d7f8056 100644--- a/tests/server/test_error_mapping.py+++ b/tests/server/test_error_mapping.py@@ -3,6 +3,7 @@"""Focused tests for HTTP server exception-to-error mapping."""+import httpximport pytestfrom openviking.pyagfs.exceptions import (@@ -20,6 +21,7 @@ from openviking.pyagfs.exceptions import (from openviking.server.error_mapping import map_exceptionfrom openviking.server.models import ERROR_CODE_TO_HTTP_STATUSfrom openviking.storage.errors import LockAcquisitionError, ResourceBusyError+from openviking.utils.exceptions import AllCredentialsFailedErrorfrom openviking_cli.exceptions import (FailedPreconditionError,InvalidArgumentError,@@ -186,12 +188,74 @@ def test_upstream_text_status_maps_to_permission_denied():assert mapped.details["upstream_status_code"] == 403-def test_upstream_502_maps_to_unavailable():- mapped = map_exception(RuntimeError("Volcengine embedding failed: HTTP 502 Bad Gateway"))+ ("errors", "expected_code", "expected_status"),+ [+ ([RuntimeError("Volcengine embedding failed: HTTP 502 Bad Gateway")], "UNAVAILABLE", 503),+ (+ [httpx.ReadTimeout("Request timed out."), httpx.ConnectTimeout("Request timed out.")],+ "DEADLINE_EXCEEDED",+ 504,+ ),+ (+ [httpx.ConnectError("Connection refused"), httpx.ReadError("Connection reset")],+ "UNAVAILABLE",+ 503,+ ),+ (+ [+ _UpstreamHTTPError(401, "invalid_api_key"),+ _UpstreamHTTPError(401, "invalid_api_key"),+ ],+ "UNAUTHENTICATED",+ 401,+ ),+ (+ [_UpstreamHTTPError(401, "invalid_api_key"), httpx.ReadTimeout("Request timed out.")],+ "INTERNAL",+ 500,+ ),+ (+ [httpx.ReadTimeout("Request timed out."), _UpstreamHTTPError(401, "invalid_api_key")],+ "INTERNAL",+ 500,+ ),+ (+ [httpx.ReadTimeout("Request timed out."), RuntimeError("Unexpected tensor shape")],+ "INTERNAL",+ 500,+ ),+ (+ [+ ValueError("Embedding provider api_key is required"),+ httpx.ReadTimeout("Request timed out."),+ ],+ "INTERNAL",+ 500,+ ),+ ],+)+def test_upstream_failures_keep_their_status_through_wrappers(+ errors, expected_code, expected_status+):+ failures = []+ for index, error in enumerate(errors):+ wrapped = RuntimeError(f"Volcengine embedding failed: {error}")+ wrapped.__cause__ = error+ failures.append((f"credential-{index}", "unknown", wrapped, index))++ aggregate = AllCredentialsFailedError(failures)+ outer = RuntimeError(f"Embedding failed: {aggregate}")+ outer.__cause__ = aggregate+ mapped = map_exception(outer)assert mapped is not None- assert mapped.code == "UNAVAILABLE"- assert mapped.details["upstream_status_code"] == 502+ assert mapped.code == expected_code+ assert ERROR_CODE_TO_HTTP_STATUS[mapped.code] == expected_status+ if expected_code == "INTERNAL":+ assert mapped.message == str(outer)+ else:+ assert mapped.details["upstream_error_type"] == "AllCredentialsFailedError"def test_model_api_key_configuration_error_maps_to_failed_precondition():diff --git a/tests/storage/test_viking_fs_grep.py b/tests/storage/test_viking_fs_grep.pyindex c57ed0d1f..06be08078 100644--- a/tests/storage/test_viking_fs_grep.py+++ b/tests/storage/test_viking_fs_grep.py@@ -13,7 +13,7 @@ from openviking.storage.acl import AclEntry, AclLevel, AclMode, DirectAcl, Effecfrom openviking.storage.expr import And, PathScope, RawDSLfrom openviking.storage.viking_fs import _DEFAULT_GREP_FILE_CONCURRENCY, VikingFSfrom openviking.storage.viking_fs import _grep as grep_module-from openviking_cli.exceptions import PermissionDeniedError+from openviking_cli.exceptions import InvalidArgumentError, PermissionDeniedErrorfrom openviking_cli.session.user_id import UserIdentifierfrom openviking_cli.utils.config.grep_config import GrepConfig@@ -860,7 +860,8 @@ async def test_grep_vikingdb_pushes_tag_filter_into_bm25_request(monkeypatch):@pytest.mark.asyncio-async def test_grep_preserves_dfs_order_and_node_limit(monkeypatch):[email protected]("pattern", ["match", "["])+async def test_grep_preserves_dfs_order_and_node_limit(monkeypatch, pattern):fs = VikingFS(agfs=_DummyAgfs())async def fake_stat(uri, ctx=None, skip_count=False):@@ -899,7 +900,12 @@ async def test_grep_preserves_dfs_order_and_node_limit(monkeypatch):)monkeypatch.setattr(fs.agfs, "read", fake_agfs_read, raising=False)- result = await fs.grep("viking://resources", pattern="match", node_limit=3)+ if pattern == "[":+ with pytest.raises(InvalidArgumentError, match="Invalid grep pattern"):+ await fs.grep("viking://resources", pattern=pattern, node_limit=3)+ return++ result = await fs.grep("viking://resources", pattern=pattern, node_limit=3)assert result["count"] == 3assert result["files_scanned"] == 2diff --git a/tests/unit/test_find_skills_entrypoint.py b/tests/unit/test_find_skills_entrypoint.pyindex b327db006..6fc57ff8d 100644--- a/tests/unit/test_find_skills_entrypoint.py+++ b/tests/unit/test_find_skills_entrypoint.py@@ -78,8 +78,8 @@ async def test_find_skills_forwards_keywords_search_type(seen):assert seen["retrieve"]["search_type"] == "keywords"[email protected]("query", ["", " "])-async def test_find_skills_needs_a_query_even_with_a_filter(seen, query):[email protected]("query", ["", " ", "broken\ud835"])+async def test_find_skills_needs_a_valid_query_even_with_a_filter(seen, query):"""It embeds the query, so a filter cannot stand in the way it can on find."""with pytest.raises(InvalidArgumentError):await _service(None).find_skills(diff --git a/tests/unit/test_search_filter_only_query.py b/tests/unit/test_search_filter_only_query.pyindex 01dccb011..db3ab2f68 100644--- a/tests/unit/test_search_filter_only_query.py+++ b/tests/unit/test_search_filter_only_query.py@@ -10,7 +10,7 @@ happens to produce.import pytest-from openviking.service.search_service import _ensure_non_empty_query+from openviking.service.search_service import _validate_queryfrom openviking.storage.viking_fs._base import (_ensure_filter_present,build_matched_context_from_record,@@ -23,23 +23,23 @@ TAG_FILTER = {"op": "must", "field": "search_tags", "conds": ["legacy_memory_id=def test_empty_query_with_filter_is_accepted():- _ensure_non_empty_query("", None, TAG_FILTER)- _ensure_non_empty_query(" ", None, TAG_FILTER)+ _validate_query("", None, TAG_FILTER)+ _validate_query(" ", None, TAG_FILTER)def test_empty_query_without_filter_is_still_rejected():with pytest.raises(InvalidArgumentError):- _ensure_non_empty_query("", None, None)+ _validate_query("", None, None)with pytest.raises(InvalidArgumentError):- _ensure_non_empty_query(" ", None, {})+ _validate_query(" ", None, {})def test_non_empty_query_is_always_accepted():- _ensure_non_empty_query("hello", None, None)+ _validate_query("hello 你好 🧭", None, None)def test_image_only_query_is_still_accepted():- _ensure_non_empty_query("", "https://example.com/a.png", None)+ _validate_query("", "https://example.com/a.png", None)@pytest.mark.parametrize(