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(compile): 禁止 system 命名空间作为输出目录Fix/system compile to namespace (#6634)
hezhang-12162425cf41b912
diff --git a/openviking/service/compile_service.py b/openviking/service/compile_service.pyindex a1c767af6..61bc69cf0 100644--- a/openviking/service/compile_service.py+++ b/openviking/service/compile_service.py@@ -857,6 +857,8 @@ class CompileService:def _validate_target(target: str) -> None:classification = classify_uri(target)parts = uri_parts(target)+ if classification.is_system:+ raise InvalidArgumentError("Compile target must not be in the system namespace")if classification.context_type == "skill":if (not classification.is_skill_namespacediff --git a/openviking/service/compile_targets.py b/openviking/service/compile_targets.pyindex cceea1fe8..a4889b1d4 100644--- a/openviking/service/compile_targets.py+++ b/openviking/service/compile_targets.py@@ -48,6 +48,7 @@ from uuid import uuid4from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError+from openviking.core.namespace import classify_urifrom openviking.pyagfs import AsyncAGFSClientfrom openviking.pyagfs.async_client import fs_ctx_from_agfs_pathfrom openviking.pyagfs.exceptions import (@@ -1058,26 +1059,31 @@ class CompileLifecycle:async def _begin_run(self, task: Any, record: Dict[str, Any], run: Optional[Dict[str, Any]]) -> Dict[str, Any]:+ """Return the persisted run planned from a task, target record and optional queued run.++ System Skills use live reads and URI identity; other Skills use snapshots.+ An unavailable current source snapshot requires full execution; busy scopes raise ResourceBusyError.+ """meta, request = task.meta["request"]["compile"], task.meta["request"]ctx = task_ctx(task.account_id, task.user_id)incremental = meta["mode"] == "incremental"+ system_skill = classify_uri(request["skill"]).is_systemrun = run or new_compile_operation(task, record)run_meta = run["meta"]run["actor"] = task.user_idrun["status"] = "running"run["started_at"] = time.time()- async with self._locked_output(request["skill"], ctx) as lease:- run_meta["skill_snapshot"] = {- "uri": request["skill"],- "snapshot_id": await commit_snapshot(+ run_meta["skill_snapshot"] = {"uri": request["skill"], "snapshot_id": None}+ if not system_skill:+ async with self._locked_output(request["skill"], ctx) as lease:+ run_meta["skill_snapshot"]["snapshot_id"] = await commit_snapshot(self._fs,[request["skill"]],f"compile {task.task_id} skill",ctx,wait_busy=False,lease_ref=lease,- ),- }+ )progress_key = "scheduled_progress" if meta["trigger"] == "scheduled" else "progress"progress = record.get(progress_key) or {}previous_skill = progress.get("skill_snapshot") or {}@@ -1088,20 +1094,20 @@ class CompileLifecycle:incrementaland (request.get("instruction") or "").strip() == progress.get("instruction")and previous_skill.get("uri") == request["skill"]- and skill_base- and skill_head):- # Skill dependencies include package files filtered from ordinary source diffs.- skill_changes = await self._fs._async_agfs.run(- "git_diff_paths",- account=ctx.account_id,- base=skill_base,- head=skill_head,- paths=[self._fs._uri_to_tree_path(request["skill"], ctx=ctx)],- )- if not skill_changes:+ if system_skill:mode = "incremental"- run_meta["execution"] = {"mode": mode}+ elif skill_base and skill_head:+ # Skill dependencies include package files filtered from ordinary source diffs.+ skill_changes = await self._fs._async_agfs.run(+ "git_diff_paths",+ account=ctx.account_id,+ base=skill_base,+ head=skill_head,+ paths=[self._fs._uri_to_tree_path(request["skill"], ctx=ctx)],+ )+ if not skill_changes:+ mode = "incremental"paths = [request["to"], *request["from"]]async with self._locked_output(request["to"], ctx) as lease:run_meta["target_base"] = await commit_snapshot(@@ -1112,6 +1118,9 @@ class CompileLifecycle:wait_busy=False,lease_ref=lease,)+ if not run_meta["target_base"]:+ mode = "full"+ run_meta["execution"] = {"mode": mode}await self.versions.record_external(task.account_id, record, run_meta["target_base"], ctx)@@ -1159,7 +1168,7 @@ class CompileLifecycle:async def runtime_changes(self, payload: Mapping[str, Any], account_id: str, task_id: str) -> Optional[Dict[str, Any]]:- """Return the frozen Skill and effective execution mode with the persisted source plan."""+ """Return Skill identity and effective execution mode with the persisted source plan."""meta = payload.get("compile")if not meta:return Nonediff --git a/tests/server/test_compile_studio.py b/tests/server/test_compile_studio.pyindex cd8eaf7be..da6480013 100644--- a/tests/server/test_compile_studio.py+++ b/tests/server/test_compile_studio.py@@ -20,59 +20,83 @@ from openviking_cli.session.user_id import UserIdentifierfrom tests.test_task_tracker import _FakeAgfs-def test_compile_accepts_builtin_example_skill():+ "skill,valid",+ [+ ("viking://system/compile/example-skills/llm-wiki/SKILL.md", True),+ ("viking://system/compile/example-skills", False),+ ("viking://system/compile/example-skills/llm-wiki/references", False),+ ("viking://system/memory/memory_templates/builtin", False),+ ],+)+def test_compile_builtin_skill_uri(skill, valid):+ """Compile accepts system Skill roots and rejects other system directories."""from openviking.service.compile_service import CompileRequest, CompileServicefrom openviking_cli.utils.config.open_viking_config import CompileApiConfigservice = CompileService(CompileApiConfig(), ExternalTaskService(), SimpleNamespace())ctx = RequestContext(user=UserIdentifier("acme", "alice"), role=Role.USER)request = CompileRequest.model_validate(- {- "from": ["viking://resources/source"],- "to": "viking://resources/wiki",- "skill": "viking://system/compile/example-skills/llm-wiki/SKILL.md",- }+ {"from": ["viking://resources/source"], "to": "viking://resources/wiki", "skill": skill})-- normalized = service._normalize_request_uris(request, ctx)-- assert normalized.skill == "viking://system/compile/example-skills/llm-wiki"+ if valid:+ assert service._normalize_request_uris(request, ctx).skill == skill.removesuffix(+ "/SKILL.md"+ )+ else:+ with pytest.raises(InvalidArgumentError, match="skill must resolve"):+ service._normalize_request_uris(request, ctx)@pytest.mark.parametrize(- "skill",+ "target",[+ "viking://system",+ "viking://system/compile","viking://system/compile/example-skills",- "viking://system/compile/example-skills/llm-wiki/references",- "viking://system/memory/memory_templates/builtin",+ "viking://system/resources/wiki",],)-def test_compile_rejects_other_system_directories(skill):- from openviking.service.compile_service import CompileRequest, CompileService- from openviking_cli.utils.config.open_viking_config import CompileApiConfig+def test_compile_builtin_skill_namespace_is_source_only(target):+ from openviking.service.compile_service import CompileService- service = CompileService(CompileApiConfig(), ExternalTaskService(), SimpleNamespace())- ctx = RequestContext(user=UserIdentifier("acme", "alice"), role=Role.USER)- request = CompileRequest.model_validate(- {- "from": ["viking://resources/source"],- "to": "viking://resources/wiki",- "skill": skill,- }- )+ CompileService._validate_target("viking://user/alice/skills")+ CompileService._validate_target("viking://agent/skills")- with pytest.raises(InvalidArgumentError, match="skill must resolve"):- service._normalize_request_uris(request, ctx)+ with pytest.raises(InvalidArgumentError, match="system namespace"):+ CompileService._validate_target(target)-def test_compile_builtin_skill_namespace_is_source_only():- from openviking.service.compile_service import CompileService+async def test_compile_api_rejects_system_target_without_creating_task(service, app, client):+ from openviking.server.auth import get_request_context+ from openviking.service.task_tracker import get_task_tracker- CompileService._validate_target("viking://user/alice/skills")- CompileService._validate_target("viking://agent/skills")+ ctx = RequestContext(user=service.user, role=Role.USER, api_key="test-compile-key")+ app.dependency_overrides[get_request_context] = lambda: ctx+ owner = {"account_id": ctx.account_id, "user_id": ctx.user.user_id}+ tracker = get_task_tracker()+ before = {+ task.task_id+ for task in await tracker.list_tasks(task_type="compile", limit=None, **owner)+ }++ response = await client.post(+ "/api/v1/compile",+ json={+ "from": ["viking://resources/source"],+ "to": "viking://system/compile-output",+ "skill": "viking://system/compile/example-skills/llm-wiki",+ },+ )- with pytest.raises(InvalidArgumentError, match="supported skills namespace"):- CompileService._validate_target("viking://system/compile/example-skills")+ assert response.status_code == 400, response.text+ assert "system namespace" in response.text+ after = {+ task.task_id+ for task in await tracker.list_tasks(task_type="compile", limit=None, **owner)+ }+ assert after == before@pytest.mark.asyncio@@ -198,8 +222,9 @@ async def test_concurrent_submission_retry_and_restart(monkeypatch):@pytest.mark.asyncio[email protected]("builtin_skill", [False, True])async def test_http_contract_preserves_retries_and_compile_target_lifecycle(- service, app, client, monkeypatch, tmp_path+ service, app, client, monkeypatch, tmp_path, builtin_skill):"""One target keeps its history, ordinary edits and live permissions across runs."""import base64@@ -225,12 +250,15 @@ async def test_http_contract_preserves_retries_and_compile_target_lifecycle(app.dependency_overrides[get_request_context] = lambda: ctxowner = {"account_id": ctx.account_id, "user_id": ctx.user.user_id}source, target = "viking://resources/source", "viking://resources/wiki"- skill = f"viking://user/{ctx.user.user_id}/skills/wiki"+ skill = (+ "viking://system/compile/example-skills/llm-wiki"+ if builtin_skill+ else f"viking://user/{ctx.user.user_id}/skills/wiki"+ )page, picture = f"{target}/page.md", f"{target}/diagram.png"png = b"\x89PNG\r\n\x1a\noriginal"for uri, content in {f"{source}/a.md": "source",- f"{skill}/SKILL.md": "Build a wiki",f"{target}/old.md": "keep renamed",f"{target}/discard.md": "remove","viking://resources/inbox/note.md": "keep imported",@@ -238,6 +266,8 @@ async def test_http_contract_preserves_retries_and_compile_target_lifecycle(picture: png,}.items():await service.viking_fs.write_file(uri, content, ctx=admin)+ if not builtin_skill:+ await service.viking_fs.write_file(f"{skill}/SKILL.md", "Build a wiki", ctx=admin)await service.runtime_config_manager.patch_account(ctx.account_id, {"acl": {"enabled": True}})await service.vikingdb_manager.upsert({@@ -363,6 +393,7 @@ async def test_http_contract_preserves_retries_and_compile_target_lifecycle(operation = await store.get_operation(ctx.account_id, target_id, first)checkpoint = dict(operation["meta"])assert checkpoint["snapshot_committed"] and checkpoint["target_head"]+ assert (checkpoint["skill_snapshot"]["snapshot_id"] is None) == builtin_skillassert checkpoint["reindex_task_id"]versions_before_resume = await store.list_version_indexes(ctx.account_id, target_id)operation.update(status="committing", finished_at=None)@@ -537,6 +568,9 @@ async def test_http_contract_preserves_retries_and_compile_target_lifecycle(scheduler = CompileScheduler(service.compile, service.compile.lifecycle.store, service.viking_fs)+ scheduler.install_expiry(+ SimpleNamespace(register_handler=Mock(), report=AsyncMock(), unregister=AsyncMock())+ )scheduler.credential_encryptor = build_credential_encryptor(service._config.encryption)scheduler.credential_encryptor.provider.key_file = tmp_path / "compile-key"groups = AsyncMock(return_value=ctx.group_ids)@@ -544,6 +578,7 @@ async def test_http_contract_preserves_retries_and_compile_target_lifecycle(AsyncMock(return_value=ResolvedIdentity(role=Role.USER, **owner)), groups)service.compile.lifecycle.save_credential = scheduler.save_credential+ service.compile.lifecycle.reschedule = scheduler.reschedulesettings = {"from": [source],"to": target,