Skip to content

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

fix(task): unify task tracker AGFS encryption binding (#2690)

Qin Haojie7608c90683df
diff --git a/openviking/service/core.py b/openviking/service/core.py
index 9174d65a8..b170675b7 100644
--- a/openviking/service/core.py
+++ b/openviking/service/core.py
@@ -6,13 +6,10 @@ OpenViking Service Core.
Main service class that composes all sub-services and manages infrastructure lifecycle.
"""
-import asyncio
import os
-from threading import Thread
from typing import TYPE_CHECKING, Any, Optional
from openviking.core.directories import DirectoryInitializer
-from openviking.crypto.config import bootstrap_encryption
from openviking.privacy import UserPrivacyConfigService
from openviking.resource.watch_scheduler import WatchScheduler
from openviking.server.identity import RequestContext, Role
@@ -32,7 +29,10 @@ from openviking.storage.index_consistency import check_index_consistency
from openviking.storage.queuefs.queue_manager import QueueManager, init_queue_manager
from openviking.storage.transaction import LockManager, init_lock_manager
from openviking.storage.viking_fs import VikingFS, init_viking_fs
-from openviking.utils.agfs_utils import resolve_queuefs_mount_point
+from openviking.utils.agfs_utils import (
+ build_runtime_ragfs_binding_config,
+ resolve_queuefs_mount_point,
+)
from openviking.utils.resource_processor import ResourceProcessor
from openviking.utils.skill_processor import SkillProcessor
from openviking_cli.exceptions import NotInitializedError
@@ -48,31 +48,6 @@ if TYPE_CHECKING:
from openviking.session.compressor_v2 import SessionCompressorV2
-def _run_coro_blocking(coro: Any) -> Any:
- """Run an async coroutine from sync startup code, even if an event loop is already running."""
- try:
- asyncio.get_running_loop()
- except RuntimeError:
- return asyncio.run(coro)
-
- result: list[Any] = []
- error: list[BaseException] = []
-
- def _runner() -> None:
- """Execute the coroutine in an isolated event loop on a helper thread."""
- try:
- result.append(asyncio.run(coro))
- except BaseException as exc:
- error.append(exc)
-
- thread = Thread(target=_runner, daemon=True)
- thread.start()
- thread.join()
- if error:
- raise error[0]
- return result[0] if result else None
-
-
class OpenVikingService:
"""
OpenViking main service class.
@@ -203,22 +178,8 @@ class OpenVikingService:
def _build_ragfs_binding_config(self) -> Any:
"""Build the single runtime binding config from OpenViking storage + encryption settings."""
- from openviking.utils.agfs_utils import RagfsBindingConfig
-
- full_config = self._config.to_dict()
- self._encryptor = _run_coro_blocking(bootstrap_encryption(full_config))
- if self._encryptor is None:
- return RagfsBindingConfig(agfs=self._config.storage.agfs)
-
- root_key = _run_coro_blocking(self._encryptor.provider.get_root_key())
- if not isinstance(root_key, (bytes, bytearray)) or len(root_key) != 32:
- raise RuntimeError("encryption root_key must be exactly 32 bytes")
-
- return RagfsBindingConfig(
- agfs=self._config.storage.agfs,
- root_key=bytes(root_key),
- provider_type=self._encryptor.provider_type,
- )
+ binding_config, self._encryptor = build_runtime_ragfs_binding_config(self._config)
+ return binding_config
def _ensure_data_dir_lock_acquired(self) -> None:
"""Acquire the process-level data directory lock once for this service instance."""
diff --git a/openviking/service/task_tracker.py b/openviking/service/task_tracker.py
index 134c09414..6f5b8bb26 100644
--- a/openviking/service/task_tracker.py
+++ b/openviking/service/task_tracker.py
@@ -523,9 +523,10 @@ class TaskTracker:
def _build_default_task_tracker() -> TaskTracker:
"""Build the default persistent tracker from the active storage config."""
- from openviking.utils.agfs_utils import RagfsBindingConfig, create_agfs_client
+ from openviking.utils.agfs_utils import build_runtime_ragfs_binding_config, create_agfs_client
from openviking_cli.utils.config import get_openviking_config
config = get_openviking_config()
- agfs = create_agfs_client(RagfsBindingConfig(agfs=config.storage.agfs))
+ binding_config, _ = build_runtime_ragfs_binding_config(config)
+ agfs = create_agfs_client(binding_config)
return config.storage.build_task_tracker(agfs)
diff --git a/openviking/utils/agfs_utils.py b/openviking/utils/agfs_utils.py
index f2d568a96..7178ab425 100644
--- a/openviking/utils/agfs_utils.py
+++ b/openviking/utils/agfs_utils.py
@@ -4,10 +4,12 @@
RAGFS Client utilities for creating and configuring RAGFS clients.
"""
+import asyncio
import multiprocessing
import os
from dataclasses import dataclass
from pathlib import Path
+from threading import Thread
from typing import Any, Callable, Dict
from openviking_cli.utils.config.config_loader import resolve_config_path
@@ -48,6 +50,72 @@ class RagfsBindingConfig:
return binding_config
+def _run_coro_blocking(coro: Any) -> Any:
+ """Run an async coroutine from sync startup code, even if an event loop is already running."""
+ try:
+ asyncio.get_running_loop()
+ except RuntimeError:
+ return asyncio.run(coro)
+
+ result: list[Any] = []
+ error: list[BaseException] = []
+
+ def _runner() -> None:
+ try:
+ result.append(asyncio.run(coro))
+ except BaseException as exc:
+ error.append(exc)
+
+ thread = Thread(target=_runner, daemon=True)
+ thread.start()
+ thread.join()
+ if error:
+ raise error[0]
+ return result[0] if result else None
+
+
+def _dump_openviking_config(config: Any) -> Dict[str, Any]:
+ """Return a full OpenViking config dict for encryption bootstrap."""
+ if hasattr(config, "to_dict"):
+ dumped = config.to_dict()
+ elif hasattr(config, "model_dump"):
+ dumped = config.model_dump()
+ elif isinstance(config, dict):
+ dumped = config
+ else:
+ raise TypeError("OpenViking config must expose to_dict() or model_dump()")
+ if not isinstance(dumped, dict):
+ raise TypeError("OpenViking config dump must be a dictionary")
+ return dumped
+
+
+def build_runtime_ragfs_binding_config(config: Any) -> tuple[RagfsBindingConfig, Any | None]:
+ """Build the runtime AGFS binding config from storage and encryption settings."""
+ from openviking.crypto.config import bootstrap_encryption
+
+ storage = _get_config_value(config, "storage")
+ agfs_config = _get_config_value(storage, "agfs") if storage is not None else None
+ if agfs_config is None:
+ raise ValueError("OpenViking config storage.agfs is required")
+
+ encryptor = _run_coro_blocking(bootstrap_encryption(_dump_openviking_config(config)))
+ if encryptor is None:
+ return RagfsBindingConfig(agfs=agfs_config), None
+
+ root_key = _run_coro_blocking(encryptor.provider.get_root_key())
+ if not isinstance(root_key, (bytes, bytearray)) or len(root_key) != 32:
+ raise RuntimeError("encryption root_key must be exactly 32 bytes")
+
+ return (
+ RagfsBindingConfig(
+ agfs=agfs_config,
+ root_key=bytes(root_key),
+ provider_type=encryptor.provider_type,
+ ),
+ encryptor,
+ )
+
+
def resolve_queuefs_mount_point(config: Any = None) -> str:
"""Resolve QueueFS mount point for the current process.
diff --git a/tests/test_task_tracker.py b/tests/test_task_tracker.py
index e8eb48fb9..328411997 100644
--- a/tests/test_task_tracker.py
+++ b/tests/test_task_tracker.py
@@ -430,7 +430,7 @@ async def test_singleton_reset():
assert t1 is not t2
-async def test_build_default_task_tracker_wraps_agfs_config_for_binding(monkeypatch):
+async def test_build_default_task_tracker_uses_runtime_encryption_binding(monkeypatch):
captured = {}
fake_agfs = object()
@@ -446,10 +446,28 @@ async def test_build_default_task_tracker_wraps_agfs_config_for_binding(monkeypa
def __init__(self):
self.storage = _FakeStorage()
+ def to_dict(self):
+ return {"encryption": {"enabled": True, "provider": "local"}}
+
+ class _FakeProvider:
+ async def get_root_key(self):
+ return b"t" * 32
+
+ class _FakeEncryptor:
+ provider_type = 9
+
+ def __init__(self):
+ self.provider = _FakeProvider()
+
+ async def _bootstrap(config):
+ assert config["encryption"]["enabled"] is True
+ return _FakeEncryptor()
+
def _fake_create_agfs_client(config):
captured["binding_config"] = config
return fake_agfs
+ monkeypatch.setattr("openviking.crypto.config.bootstrap_encryption", _bootstrap)
monkeypatch.setattr("openviking.utils.agfs_utils.create_agfs_client", _fake_create_agfs_client)
monkeypatch.setattr(
"openviking_cli.utils.config.get_openviking_config",
@@ -462,6 +480,10 @@ async def test_build_default_task_tracker_wraps_agfs_config_for_binding(monkeypa
assert captured["build_arg"] is fake_agfs
assert isinstance(captured["binding_config"], RagfsBindingConfig)
assert isinstance(captured["binding_config"].agfs, AGFSConfig)
+ assert captured["binding_config"].to_binding_dict()["encryption"] == {
+ "root_key": b"t" * 32,
+ "provider_type": 9,
+ }
async def test_persistent_store_cross_tracker_visibility():
diff --git a/tests/unit/service/test_core_encryption_startup.py b/tests/unit/service/test_core_encryption_startup.py
index 75d3013df..c6ac2bb9c 100644
--- a/tests/unit/service/test_core_encryption_startup.py
+++ b/tests/unit/service/test_core_encryption_startup.py
@@ -58,7 +58,7 @@ async def test_build_ragfs_binding_config_works_inside_running_event_loop(monkey
assert config["encryption"]["enabled"] is True
return _FakeEncryptor()
- monkeypatch.setattr("openviking.service.core.bootstrap_encryption", _bootstrap)
+ monkeypatch.setattr("openviking.crypto.config.bootstrap_encryption", _bootstrap)
service = OpenVikingService.__new__(OpenVikingService)
service._config = _FakeConfig()
service._encryptor = None
@@ -75,7 +75,7 @@ async def test_build_ragfs_binding_config_works_inside_running_event_loop(monkey
"encryption": {
"root_key": b"k" * 32,
"provider_type": 1,
- }
+ },
}
assert isinstance(service._encryptor, _FakeEncryptor)