Skip to content

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

perf(system-assets): 避免重复执行启动同步 (#6701)

hezhang-1216716fcf220e10
diff --git a/MANIFEST.in b/MANIFEST.in
index 1279412b8..55ecb2d66 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -9,6 +9,7 @@ include pyproject.toml
include setup.py
include Cargo.toml
include Cargo.lock
+recursive-include scripts/build_support *.py
graft crates/ragfs
graft crates/ragfs-python
recursive-include openviking *.yaml
diff --git a/openviking/core/namespace_constants.py b/openviking/core/namespace_constants.py
new file mode 100644
index 000000000..069fa6a77
--- /dev/null
+++ b/openviking/core/namespace_constants.py
@@ -0,0 +1,33 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""Constants shared by runtime namespace policy and package build tooling."""
+
+SYSTEM_ROOT_URI = "viking://system"
+SYSTEM_COMPILE_ROOT = f"{SYSTEM_ROOT_URI}/compile"
+SYSTEM_EXAMPLE_SKILLS_ROOT = f"{SYSTEM_COMPILE_ROOT}/example-skills"
+SYSTEM_MEMORY_ROOT = f"{SYSTEM_ROOT_URI}/memory"
+SYSTEM_MEMORY_TEMPLATES_ROOT = f"{SYSTEM_MEMORY_ROOT}/memory_templates"
+SYSTEM_MEMORY_BUILTIN_ROOT = f"{SYSTEM_MEMORY_TEMPLATES_ROOT}/builtin"
+SYSTEM_MEMORY_CUSTOM_ROOT = f"{SYSTEM_MEMORY_TEMPLATES_ROOT}/custom"
+
+SYSTEM_ASSET_ROOT_MAPPINGS = (
+ ("compile/example-skills", SYSTEM_EXAMPLE_SKILLS_ROOT),
+ ("memory/memory_templates/builtin", SYSTEM_MEMORY_BUILTIN_ROOT),
+)
+SYSTEM_FIXED_DIRECTORIES = frozenset(
+ {
+ SYSTEM_ROOT_URI,
+ SYSTEM_COMPILE_ROOT,
+ SYSTEM_EXAMPLE_SKILLS_ROOT,
+ SYSTEM_MEMORY_ROOT,
+ SYSTEM_MEMORY_TEMPLATES_ROOT,
+ SYSTEM_MEMORY_BUILTIN_ROOT,
+ SYSTEM_MEMORY_CUSTOM_ROOT,
+ }
+)
+SYSTEM_OFFICIAL_ASSET_ROOTS = frozenset(
+ {
+ SYSTEM_EXAMPLE_SKILLS_ROOT,
+ SYSTEM_MEMORY_BUILTIN_ROOT,
+ }
+)
diff --git a/openviking/core/namespace_policy.py b/openviking/core/namespace_policy.py
index b259b4f67..dfd4d4da2 100644
--- a/openviking/core/namespace_policy.py
+++ b/openviking/core/namespace_policy.py
@@ -5,33 +5,18 @@
from dataclasses import dataclass
from enum import Enum
+from openviking.core import namespace_constants as _namespace_constants
from openviking.core.namespace import uri_parts
-SYSTEM_ROOT_URI = "viking://system"
-SYSTEM_COMPILE_ROOT = f"{SYSTEM_ROOT_URI}/compile"
-SYSTEM_EXAMPLE_SKILLS_ROOT = f"{SYSTEM_COMPILE_ROOT}/example-skills"
-SYSTEM_MEMORY_ROOT = f"{SYSTEM_ROOT_URI}/memory"
-SYSTEM_MEMORY_TEMPLATES_ROOT = f"{SYSTEM_MEMORY_ROOT}/memory_templates"
-SYSTEM_MEMORY_BUILTIN_ROOT = f"{SYSTEM_MEMORY_TEMPLATES_ROOT}/builtin"
-SYSTEM_MEMORY_CUSTOM_ROOT = f"{SYSTEM_MEMORY_TEMPLATES_ROOT}/custom"
-
-SYSTEM_FIXED_DIRECTORIES = frozenset(
- {
- SYSTEM_ROOT_URI,
- SYSTEM_COMPILE_ROOT,
- SYSTEM_EXAMPLE_SKILLS_ROOT,
- SYSTEM_MEMORY_ROOT,
- SYSTEM_MEMORY_TEMPLATES_ROOT,
- SYSTEM_MEMORY_BUILTIN_ROOT,
- SYSTEM_MEMORY_CUSTOM_ROOT,
- }
-)
-SYSTEM_OFFICIAL_ASSET_ROOTS = frozenset(
- {
- SYSTEM_EXAMPLE_SKILLS_ROOT,
- SYSTEM_MEMORY_BUILTIN_ROOT,
- }
-)
+SYSTEM_ROOT_URI = _namespace_constants.SYSTEM_ROOT_URI
+SYSTEM_COMPILE_ROOT = _namespace_constants.SYSTEM_COMPILE_ROOT
+SYSTEM_EXAMPLE_SKILLS_ROOT = _namespace_constants.SYSTEM_EXAMPLE_SKILLS_ROOT
+SYSTEM_MEMORY_ROOT = _namespace_constants.SYSTEM_MEMORY_ROOT
+SYSTEM_MEMORY_TEMPLATES_ROOT = _namespace_constants.SYSTEM_MEMORY_TEMPLATES_ROOT
+SYSTEM_MEMORY_BUILTIN_ROOT = _namespace_constants.SYSTEM_MEMORY_BUILTIN_ROOT
+SYSTEM_MEMORY_CUSTOM_ROOT = _namespace_constants.SYSTEM_MEMORY_CUSTOM_ROOT
+SYSTEM_FIXED_DIRECTORIES = _namespace_constants.SYSTEM_FIXED_DIRECTORIES
+SYSTEM_OFFICIAL_ASSET_ROOTS = _namespace_constants.SYSTEM_OFFICIAL_ASSET_ROOTS
class NamespaceProcessingMode(str, Enum):
diff --git a/openviking/server/app.py b/openviking/server/app.py
index f4039cda1..5ee94688f 100644
--- a/openviking/server/app.py
+++ b/openviking/server/app.py
@@ -172,7 +172,7 @@ async def _initialize_runtime_state(
await service.apply_agent_evolution_config()
await _initialize_auth_plugin(app, service, config)
manager = getattr(app.state, "api_key_manager", None)
- if manager is not None:
+ if manager is not None and get_openviking_config().reconcile_system_assets_on_startup:
account_count = await service.reconcile_system_assets(manager)
logger.info("Reconciled system assets for %d active accounts", account_count)
compile_scheduler = getattr(service, "compile_scheduler", None)
diff --git a/openviking/service/core.py b/openviking/service/core.py
index 75f9bf3f0..c39797e01 100644
--- a/openviking/service/core.py
+++ b/openviking/service/core.py
@@ -750,7 +750,8 @@ class OpenVikingService:
if self._queue_manager:
self._queue_manager.start()
logger.info("QueueManager workers started")
- await system_asset_synchronizer.sync(default_ctx)
+ if self._config.reconcile_system_assets_on_startup:
+ await system_asset_synchronizer.sync(default_ctx)
compile_expiry_enabled = self._compile_scheduler is not None
if session_expiry_enabled or compile_expiry_enabled:
@@ -1022,14 +1023,15 @@ class OpenVikingService:
async def reconcile_system_assets(self, account_lister: AccountLister) -> int:
"""Synchronize packaged system assets for every active account."""
self._ensure_initialized()
- if not self._system_asset_synchronizer:
+ if not self._system_asset_synchronizer or not self._viking_fs:
return 0
return await SystemAssetReconciler(
self._system_asset_synchronizer,
account_lister,
+ self._viking_fs._async_agfs,
user_id=self._config.default_user or self._user.user_id,
skip_account_ids={self._user.account_id},
- ).sync_all()
+ ).sync_if_changed()
async def initialize_user_directories(self, ctx: RequestContext) -> int:
"""Initialize current user's directory tree."""
diff --git a/openviking/system_assets/manifest.json b/openviking/system_assets/manifest.json
new file mode 100644
index 000000000..a289fa489
--- /dev/null
+++ b/openviking/system_assets/manifest.json
@@ -0,0 +1,4 @@
+{
+ "schema_version": 1,
+ "sha256": "2468102bae1be9fbfa2e0a43555fec655fe4f22a50e7a0601ea4c50c3cfdb4f0"
+}
diff --git a/openviking/system_assets/reconcile.py b/openviking/system_assets/reconcile.py
index 18ab19174..433409a6d 100644
--- a/openviking/system_assets/reconcile.py
+++ b/openviking/system_assets/reconcile.py
@@ -3,13 +3,28 @@
"""Reconcile packaged system assets across all active accounts."""
import asyncio
+import json
+import random
from collections.abc import Collection
-from typing import Protocol
+from typing import Any, Protocol
+from openviking.pyagfs import AGFSNotFoundError, AsyncAGFSClient
+from openviking.pyagfs.async_client import fs_ctx_from_agfs_path
from openviking.server.account_stores.models import AccountSummary
from openviking.server.identity import RequestContext, Role
+from openviking.storage.errors import ResourceBusyError
from openviking.system_assets.sync import SystemAssetSynchronizer
from openviking_cli.session.user_id import UserIdentifier
+from openviking_cli.utils import get_logger
+
+logger = get_logger(__name__)
+
+_SYNC_MAX_RETRIES = 6
+_SYNC_RETRY_BASE_DELAY_SECONDS = 0.2
+_SYNC_RETRY_MAX_DELAY_SECONDS = 2.0
+
+_SYSTEM_ASSET_MANIFEST_PATH = "/local/_system/system_assets_manifest.json"
+_RECONCILE_LOCK_TIMEOUT_SECONDS = 300.0
class AccountLister(Protocol):
@@ -23,6 +38,7 @@ class SystemAssetReconciler:
self,
synchronizer: SystemAssetSynchronizer,
account_lister: AccountLister,
+ agfs: AsyncAGFSClient,
*,
user_id: str,
skip_account_ids: Collection[str] | None = None,
@@ -35,11 +51,31 @@ class SystemAssetReconciler:
raise ValueError("max_concurrency must be positive")
self._synchronizer = synchronizer
self._account_lister = account_lister
+ self._agfs = agfs
self._user_id = user_id
self._skip_account_ids = frozenset(skip_account_ids or ())
self._page_size = page_size
self._semaphore = asyncio.Semaphore(max_concurrency)
+ async def sync_if_changed(self) -> int:
+ manifest = self._synchronizer.manifest
+ digest = self._synchronizer.manifest_digest
+ if await self._read_manifest_digest() == digest:
+ return 0
+
+ lease = await self._agfs.pathlock_acquire_exact(
+ _SYSTEM_ASSET_MANIFEST_PATH,
+ timeout_secs=_RECONCILE_LOCK_TIMEOUT_SECONDS,
+ )
+ try:
+ if await self._read_manifest_digest() == digest:
+ return 0
+ synced_accounts = await self.sync_all()
+ await self._write_manifest(manifest, lease)
+ return synced_accounts
+ finally:
+ await self._agfs.pathlock_release(lease)
+
async def sync_all(self) -> int:
synced_accounts = 0
page = 1
@@ -81,14 +117,69 @@ class SystemAssetReconciler:
page += 1
return synced_accounts
+ async def _read_manifest_digest(self) -> str | None:
+ fs_ctx = fs_ctx_from_agfs_path(_SYSTEM_ASSET_MANIFEST_PATH)
+ fs_ctx["bypass_cache"] = "true"
+ try:
+ result = await self._agfs.read(_SYSTEM_ASSET_MANIFEST_PATH, fs_ctx=fs_ctx)
+ except (AGFSNotFoundError, FileNotFoundError):
+ return None
+
+ raw = result if isinstance(result, bytes) else getattr(result, "content", result)
+ if isinstance(raw, str):
+ raw = raw.encode("utf-8")
+ try:
+ payload = json.loads(raw.decode("utf-8"))
+ except (AttributeError, UnicodeDecodeError, json.JSONDecodeError):
+ return None
+ digest = payload.get("sha256") if isinstance(payload, dict) else None
+ return digest if isinstance(digest, str) else None
+
+ async def _write_manifest(self, manifest: bytes, lease: Any) -> None:
+ fs_ctx = _lease_fs_ctx(_SYSTEM_ASSET_MANIFEST_PATH, lease)
+ await self._agfs.ensure_parent_dirs(_SYSTEM_ASSET_MANIFEST_PATH, fs_ctx=fs_ctx)
+ await self._agfs.write(
+ _SYSTEM_ASSET_MANIFEST_PATH,
+ manifest,
+ fs_ctx=fs_ctx,
+ auto_pathlock=False,
+ )
+
async def _sync_account(self, account: AccountSummary) -> int:
account_id = account.get("account_id")
if not account_id:
raise ValueError("account summary is missing account_id")
- async with self._semaphore:
- return await self._synchronizer.sync(
- RequestContext(
- user=UserIdentifier(account_id, self._user_id),
- role=Role(Role.ROOT),
+ ctx = RequestContext(
+ user=UserIdentifier(account_id, self._user_id),
+ role=Role(Role.ROOT),
+ )
+ for retry in range(_SYNC_MAX_RETRIES + 1):
+ try:
+ async with self._semaphore:
+ return await self._synchronizer.sync(ctx)
+ except ResourceBusyError:
+ if retry == _SYNC_MAX_RETRIES:
+ raise
+ delay = min(
+ _SYNC_RETRY_BASE_DELAY_SECONDS * (2**retry),
+ _SYNC_RETRY_MAX_DELAY_SECONDS,
+ ) * random.uniform(0.8, 1.2)
+ logger.warning(
+ "System assets busy for account %s; retry %d/%d in %.2fs",
+ account_id,
+ retry + 1,
+ _SYNC_MAX_RETRIES,
+ delay,
)
- )
+ await asyncio.sleep(delay)
+
+ raise AssertionError("unreachable")
+
+def _lease_fs_ctx(path: str, lease: Any) -> dict[str, str]:
+ fs_ctx = fs_ctx_from_agfs_path(path)
+ lease_ref = getattr(lease, "lease_ref", None) or getattr(lease, "id", None)
+ if isinstance(lease, dict):
+ lease_ref = lease.get("lease_ref", lease_ref)
+ if isinstance(lease_ref, str) and lease_ref:
+ fs_ctx["lease_ref"] = lease_ref
+ return fs_ctx
diff --git a/openviking/system_assets/sync.py b/openviking/system_assets/sync.py
index 39fbc98a4..56378b71e 100644
--- a/openviking/system_assets/sync.py
+++ b/openviking/system_assets/sync.py
@@ -3,14 +3,16 @@
"""Synchronize packaged system assets into an account's system namespace."""
import base64
+import json
+import re
from dataclasses import replace
+from functools import cached_property
from pathlib import Path
from openviking.core.namespace import uri_parts
+from openviking.core.namespace_constants import SYSTEM_ASSET_ROOT_MAPPINGS
from openviking.core.namespace_policy import (
- SYSTEM_EXAMPLE_SKILLS_ROOT,
SYSTEM_FIXED_DIRECTORIES,
- SYSTEM_MEMORY_BUILTIN_ROOT,
)
from openviking.server.identity import RequestContext, Role
from openviking.storage.content_write import ContentWriteCoordinator
@@ -18,10 +20,9 @@ from openviking.storage.viking_fs import VikingFS
from openviking.system_assets import SYSTEM_ASSETS_DIR
from openviking_cli.exceptions import NotFoundError
-_MANAGED_ASSET_ROOTS = (
- (Path("compile/example-skills"), SYSTEM_EXAMPLE_SKILLS_ROOT),
- (Path("memory/memory_templates/builtin"), SYSTEM_MEMORY_BUILTIN_ROOT),
-)
+_MANIFEST_FILENAME = "manifest.json"
+_MANIFEST_SCHEMA_VERSION = 1
+_SHA256_PATTERN = re.compile(r"^[0-9a-f]{64}$")
class SystemAssetSynchronizer:
@@ -36,33 +37,58 @@ class SystemAssetSynchronizer:
self._coordinator = ContentWriteCoordinator(viking_fs=viking_fs)
self._assets_dir = assets_dir
- async def sync(self, ctx: RequestContext) -> int:
- root_ctx = replace(ctx, role=Role.ROOT, actor_peer_id=None)
- for uri in sorted(SYSTEM_FIXED_DIRECTORIES, key=lambda value: len(uri_parts(value))):
- await self._viking_fs.mkdir(uri, exist_ok=True, ctx=root_ctx)
+ @cached_property
+ def manifest(self) -> bytes:
+ """Return the validated manifest shipped with the current image."""
+ manifest_path = self._assets_dir / _MANIFEST_FILENAME
+ raw = manifest_path.read_bytes()
+ try:
+ payload = json.loads(raw.decode("utf-8"))
+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:
+ raise ValueError(f"invalid packaged system asset manifest: {manifest_path}") from exc
+ if (
+ not isinstance(payload, dict)
+ or payload.get("schema_version") != _MANIFEST_SCHEMA_VERSION
+ or not isinstance(payload.get("sha256"), str)
+ or not _SHA256_PATTERN.fullmatch(payload["sha256"])
+ ):
+ raise ValueError(f"invalid packaged system asset manifest: {manifest_path}")
+ return raw
- pending_by_root: dict[str, list[dict[str, str]]] = {}
- for relative_root, target_root in _MANAGED_ASSET_ROOTS:
+ @property
+ def manifest_digest(self) -> str:
+ return json.loads(self.manifest)["sha256"]
+
+ def _iter_managed_assets(self):
+ for relative_root, target_root in SYSTEM_ASSET_ROOT_MAPPINGS:
source_root = self._assets_dir / relative_root
if not source_root.is_dir():
raise FileNotFoundError(f"packaged system asset directory not found: {source_root}")
for source_path in sorted(path for path in source_root.rglob("*") if path.is_file()):
relative_path = source_path.relative_to(source_root).as_posix()
- target_uri = f"{target_root}/{relative_path}"
- expected = source_path.read_bytes()
- try:
- current = await self._viking_fs.read_file_bytes(target_uri, ctx=root_ctx)
- except (FileNotFoundError, NotFoundError):
- current = None
- if current == expected:
- continue
- pending_by_root.setdefault(target_root, []).append(
- {
- "uri": target_uri,
- "content_base64": base64.b64encode(expected).decode("ascii"),
- "mode": "replace",
- }
- )
+ yield source_path, target_root, f"{target_root}/{relative_path}"
+
+ async def sync(self, ctx: RequestContext) -> int:
+ root_ctx = replace(ctx, role=Role.ROOT, actor_peer_id=None)
+ for uri in sorted(SYSTEM_FIXED_DIRECTORIES, key=lambda value: len(uri_parts(value))):
+ await self._viking_fs.mkdir(uri, exist_ok=True, ctx=root_ctx)
+
+ pending_by_root: dict[str, list[dict[str, str]]] = {}
+ for source_path, target_root, target_uri in self._iter_managed_assets():
+ expected = source_path.read_bytes()
+ try:
+ current = await self._viking_fs.read_file_bytes(target_uri, ctx=root_ctx)
+ except (FileNotFoundError, NotFoundError):
+ current = None
+ if current == expected:
+ continue
+ pending_by_root.setdefault(target_root, []).append(
+ {
+ "uri": target_uri,
+ "content_base64": base64.b64encode(expected).decode("ascii"),
+ "mode": "replace",
+ }
+ )
for root_uri, operations in pending_by_root.items():
await self._coordinator.batch_write(
diff --git a/openviking_cli/utils/config/open_viking_config.py b/openviking_cli/utils/config/open_viking_config.py
index c5650fd4b..c66fdc25f 100644
--- a/openviking_cli/utils/config/open_viking_config.py
+++ b/openviking_cli/utils/config/open_viking_config.py
@@ -384,6 +384,14 @@ class OpenVikingConfig(BaseModel):
),
)
+ reconcile_system_assets_on_startup: bool = Field(
+ default=True,
+ description=(
+ "Whether to reconcile packaged system assets for active accounts during startup. "
+ "Disable on read-only replicas that share a writer's data."
+ ),
+ )
+
auto_generate_l0: bool = Field(
default=True, description="Automatically generate L0 (abstract) if not provided"
)
diff --git a/scripts/build_support/system_assets_manifest.py b/scripts/build_support/system_assets_manifest.py
new file mode 100644
index 000000000..80e3346ce
--- /dev/null
+++ b/scripts/build_support/system_assets_manifest.py
@@ -0,0 +1,49 @@
+"""Generate the packaged system-assets manifest."""
+
+import hashlib
+import json
+from pathlib import Path
+
+from openviking.core.namespace_constants import (
+ SYSTEM_ASSET_ROOT_MAPPINGS,
+ SYSTEM_FIXED_DIRECTORIES,
+)
+
+MANIFEST_FILENAME = "manifest.json"
+MANIFEST_SCHEMA_VERSION = 1
+
+
+def _update_digest(digest, path: bytes, content: bytes) -> None:
+ digest.update(len(path).to_bytes(8, "big"))
+ digest.update(path)
+ digest.update(len(content).to_bytes(8, "big"))
+ digest.update(content)
+
+
+def build_system_assets_manifest(assets_dir: Path) -> bytes:
+ """Return the canonical manifest for all managed assets."""
+ digest = hashlib.sha256()
+ for uri in sorted(SYSTEM_FIXED_DIRECTORIES):
+ _update_digest(digest, uri.encode("utf-8"), b"")
+
+ for relative_root, target_root in SYSTEM_ASSET_ROOT_MAPPINGS:
+ source_root = assets_dir / relative_root
+ if not source_root.is_dir():
+ raise FileNotFoundError(f"packaged system asset directory not found: {source_root}")
+ for source_path in sorted(path for path in source_root.rglob("*") if path.is_file()):
+ relative_path = source_path.relative_to(source_root).as_posix()
+ target_uri = f"{target_root}/{relative_path}"
+ _update_digest(digest, target_uri.encode("utf-8"), source_path.read_bytes())
+
+ payload = {
+ "schema_version": MANIFEST_SCHEMA_VERSION,
+ "sha256": digest.hexdigest(),
+ }
+ return (json.dumps(payload, indent=2, sort_keys=True) + "\n").encode("utf-8")
+
+
+def write_system_assets_manifest(assets_dir: Path) -> Path:
+ """Generate manifest.json in the given system-assets directory."""
+ destination = assets_dir / MANIFEST_FILENAME
+ destination.write_bytes(build_system_assets_manifest(assets_dir))
+ return destination
diff --git a/setup.py b/setup.py
index 50f37b72d..1162aebf2 100644
--- a/setup.py
+++ b/setup.py
@@ -11,6 +11,7 @@ from pathlib import Path
from setuptools import Distribution, Extension, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
+from setuptools.command.sdist import sdist
try:
from wheel.bdist_wheel import bdist_wheel
@@ -30,6 +31,9 @@ resolve_openviking_version = importlib.import_module(
pypi_long_description = importlib.import_module(
"scripts.build_support.readme"
).pypi_long_description
+write_system_assets_manifest = importlib.import_module(
+ "scripts.build_support.system_assets_manifest"
+).write_system_assets_manifest
CMAKE_PATH = shutil.which("cmake") or "cmake"
C_COMPILER_PATH = os.environ.get("CC") or shutil.which("gcc") or "gcc"
@@ -516,6 +520,7 @@ def _build_web_studio():
class OpenVikingBuildPy(build_py):
def run(self):
_build_web_studio()
+ write_system_assets_manifest(SETUP_DIR / "openviking" / "system_assets")
super().run()
package_root = Path(self.build_lib) / "vikingbot"
for asset_name in ("workspace", "bridge"):
@@ -532,6 +537,12 @@ class OpenVikingBuildPy(build_py):
)
+class OpenVikingSdist(sdist):
+ def make_release_tree(self, base_dir, files):
+ super().make_release_tree(base_dir, files)
+ write_system_assets_manifest(Path(base_dir) / "openviking" / "system_assets")
+
+
if bdist_wheel is not None:
class OpenVikingBdistWheel(bdist_wheel):
@@ -552,6 +563,7 @@ class OpenVikingDistribution(Distribution):
cmdclass = {
"build_ext": OpenVikingBuildExt,
"build_py": OpenVikingBuildPy,
+ "sdist": OpenVikingSdist,
}
if OpenVikingBdistWheel is not None:
cmdclass["bdist_wheel"] = OpenVikingBdistWheel
diff --git a/tests/server/test_server_health.py b/tests/server/test_server_health.py
index 34728d963..6608c76ad 100644
--- a/tests/server/test_server_health.py
+++ b/tests/server/test_server_health.py
@@ -331,6 +331,8 @@ async def test_initialize_runtime_state_loads_api_key_manager(monkeypatch):
"""System assets reconcile after auth loading and before deletion setup."""
events = []
+ ov_config = SimpleNamespace(reconcile_system_assets_on_startup=True)
+ monkeypatch.setattr("openviking.server.app.get_openviking_config", lambda: ov_config)
class MockService:
def __init__(self):
@@ -380,6 +382,14 @@ async def test_initialize_runtime_state_loads_api_key_manager(monkeypatch):
"deletion",
]
+ events.clear()
+ ov_config.reconcile_system_assets_on_startup = False
+ await _initialize_runtime_state(app, service, config)
+
+ assert service._initialized is True
+ assert app.state.api_key_manager is manager
+ assert events == ["service", "agent-evolution", "auth", "deletion"]
+
async def test_initialize_runtime_state_skips_reconcile_without_account_manager(monkeypatch):
service = SimpleNamespace(
diff --git a/tests/unit/test_system_asset_reconcile.py b/tests/unit/test_system_asset_reconcile.py
index 9cd0c828f..f5e653cb2 100644
--- a/tests/unit/test_system_asset_reconcile.py
+++ b/tests/unit/test_system_asset_reconcile.py
@@ -1,11 +1,38 @@
import asyncio
+import json
import pytest
from openviking.server.identity import Role
+from openviking.storage.errors import ResourceBusyError
from openviking.system_assets.reconcile import SystemAssetReconciler
+class _FakeAGFS:
+ def __init__(self):
+ self.marker = None
+ self.lock = asyncio.Lock()
+
+ async def read(self, path, fs_ctx=None):
+ if self.marker is None:
+ raise FileNotFoundError(path)
+ return self.marker
+
+ async def pathlock_acquire_exact(self, path, timeout_secs):
+ await self.lock.acquire()
+ return {"lease_ref": "manifest-lease"}
+
+ async def pathlock_release(self, lease):
+ self.lock.release()
+
+ async def ensure_parent_dirs(self, path, fs_ctx=None):
+ pass
+
+ async def write(self, path, data, fs_ctx=None, auto_pathlock=True):
+ assert self.lock.locked()
+ self.marker = data
+
+
class _FakeAccountLister:
def __init__(self, accounts):
self._accounts = accounts
@@ -18,8 +45,11 @@ class _FakeAccountLister:
class _TrackingSynchronizer:
- def __init__(self, failures=()):
+ def __init__(self, failures=(), busy_once=()):
self._failures = set(failures)
+ self._busy_once = set(busy_once)
+ self.manifest_digest = "d" * 64
+ self.manifest = json.dumps({"schema_version": 1, "sha256": self.manifest_digest}).encode()
self.accounts = []
self.active = 0
self.max_active = 0
@@ -33,6 +63,9 @@ class _TrackingSynchronizer:
self.max_active = max(self.max_active, self.active)
try:
await asyncio.sleep(0.01)
+ if account_id in self._busy_once:
+ self._busy_once.remove(account_id)
+ raise ResourceBusyError("busy")
if account_id in self._failures:
raise OSError(f"failed {account_id}")
return 0
@@ -51,19 +84,30 @@ async def test_reconciler_pages_active_accounts_with_bounded_concurrency():
{"account_id": "e", "status": "active"},
]
)
- synchronizer = _TrackingSynchronizer()
+ agfs = _FakeAGFS()
+ synchronizer = _TrackingSynchronizer(busy_once={"a"})
reconciler = SystemAssetReconciler(
synchronizer,
lister,
+ agfs,
user_id="system-user",
page_size=2,
max_concurrency=2,
)
- assert await reconciler.sync_all() == 4
+ assert await reconciler.sync_if_changed() == 4
assert lister.calls == [(2, 1), (2, 2), (2, 3)]
assert set(synchronizer.accounts) == {"a", "c", "d", "e"}
assert synchronizer.max_active == 2
+ assert synchronizer.accounts.count("a") == 2
+ assert agfs.marker == synchronizer.manifest
+ assert not agfs.lock.locked()
+
+ lister.calls.clear()
+ synchronizer.accounts.clear()
+ assert await reconciler.sync_if_changed() == 0
+ assert lister.calls == []
+ assert synchronizer.accounts == []
@pytest.mark.asyncio
@@ -75,18 +119,31 @@ async def test_reconciler_skips_already_synced_account_without_ending_pagination
{"account_id": "b", "status": "active"},
]
)
+ agfs = _FakeAGFS()
synchronizer = _TrackingSynchronizer()
reconciler = SystemAssetReconciler(
synchronizer,
lister,
+ agfs,
user_id="system-user",
skip_account_ids={"default"},
page_size=2,
)
- assert await reconciler.sync_all() == 2
+ other_writer = SystemAssetReconciler(
+ synchronizer,
+ lister,
+ agfs,
+ user_id="system-user",
+ skip_account_ids={"default"},
+ page_size=2,
+ )
+ results = await asyncio.gather(reconciler.sync_if_changed(), other_writer.sync_if_changed())
+
+ assert sorted(results) == [0, 2]
assert lister.calls == [(2, 1), (2, 2)]
- assert set(synchronizer.accounts) == {"a", "b"}
+ assert sorted(synchronizer.accounts) == ["a", "b"]
+ assert not agfs.lock.locked()
@pytest.mark.asyncio
@@ -97,17 +154,26 @@ async def test_reconciler_reports_all_failures_in_page():
{"account_id": "b", "status": "active"},
]
)
+ agfs = _FakeAGFS()
synchronizer = _TrackingSynchronizer(failures={"a", "b"})
reconciler = SystemAssetReconciler(
synchronizer,
lister,
+ agfs,
user_id="system-user",
page_size=2,
max_concurrency=2,
)
with pytest.raises(RuntimeError, match="failed for 2 account") as exc_info:
- await reconciler.sync_all()
+ await reconciler.sync_if_changed()
assert "a: OSError: failed a" in str(exc_info.value)
assert "b: OSError: failed b" in str(exc_info.value)
+
+ assert agfs.marker is None
+ assert not agfs.lock.locked()
+
+ synchronizer._failures.clear()
+ assert await reconciler.sync_if_changed() == 2
+ assert agfs.marker == synchronizer.manifest
diff --git a/tests/unit/test_system_asset_sync.py b/tests/unit/test_system_asset_sync.py
index df2a817e8..c780aac6c 100644
--- a/tests/unit/test_system_asset_sync.py
+++ b/tests/unit/test_system_asset_sync.py
@@ -11,6 +11,7 @@ from openviking.core.namespace_policy import (
from openviking.server.identity import RequestContext, Role
from openviking.system_assets.sync import SystemAssetSynchronizer
from openviking_cli.session.user_id import UserIdentifier
+from scripts.build_support.system_assets_manifest import write_system_assets_manifest
class _FakeVikingFS:
@@ -58,6 +59,7 @@ async def test_system_asset_sync_converges_managed_roots_without_touching_custom
compile_source.write_bytes(b"same skill")
reference_source.write_bytes(b"\x00guide")
memory_source.write_bytes(b"new profile")
+ write_system_assets_manifest(tmp_path)
compile_target = f"{SYSTEM_EXAMPLE_SKILLS_ROOT}/demo/SKILL.md"
reference_target = f"{SYSTEM_EXAMPLE_SKILLS_ROOT}/demo/references/guide.bin"
@@ -106,6 +108,16 @@ async def test_system_asset_sync_converges_managed_roots_without_touching_custom
assert await synchronizer.sync(ctx) == 0
assert coordinator.calls == []
+ previous_digest = synchronizer.manifest_digest
+ compile_source.write_bytes(b"updated skill")
+ write_system_assets_manifest(tmp_path)
+ updated = SystemAssetSynchronizer(viking_fs=viking_fs, assets_dir=tmp_path)
+
+ assert updated.manifest_digest != previous_digest
+ assert await updated.sync(ctx) == 1
+ assert viking_fs.files[compile_target] == b"updated skill"
+ assert viking_fs.files[custom_target] == b"custom"
+
@pytest.mark.asyncio
async def test_system_asset_sync_rejects_missing_packaged_root(tmp_path, monkeypatch):