Skip to content

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

perf(content-write): anchor semantic refresh at the file's direct parent / 写入语义刷新锚点改为文件直接父目录 (#2864)

Kchen3afe85fbd026
diff --git a/openviking/storage/content_write.py b/openviking/storage/content_write.py
index 4bbeb94a6..987ae2583 100644
--- a/openviking/storage/content_write.py
+++ b/openviking/storage/content_write.py
@@ -80,7 +80,7 @@ class ContentWriteCoordinator:
raise InvalidArgumentError(f"write only supports existing files, got directory: {uri}")
context_type = context_type_for_uri(normalized_uri)
- root_uri = await self._resolve_root_uri(normalized_uri, ctx=ctx)
+ root_uri = await self._resolve_root_uri(normalized_uri, ctx=ctx, anchor_to_parent=True)
written_bytes = len(content.encode("utf-8"))
telemetry_id = get_current_telemetry().telemetry_id
@@ -265,7 +265,6 @@ class ContentWriteCoordinator:
context_type=context_type,
ctx=ctx,
change_type="added" if mode == "create" else "modified",
- recursive=True,
)
semantic_enqueued = True
await lock_manager.release(handle)
@@ -384,7 +383,9 @@ class ContentWriteCoordinator:
raise AlreadyExistsError(uri, "file")
context_type = context_type_for_uri(uri)
- root_uri = await self._resolve_root_uri(uri, ctx=ctx, _allow_not_found=True)
+ root_uri = await self._resolve_root_uri(
+ uri, ctx=ctx, _allow_not_found=True, anchor_to_parent=True
+ )
written_bytes = len(content.encode("utf-8"))
telemetry_id = get_current_telemetry().telemetry_id
@@ -780,6 +781,7 @@ class ContentWriteCoordinator:
*,
ctx: RequestContext,
_allow_not_found: bool = False,
+ anchor_to_parent: bool = False,
) -> str:
parsed = VikingURI(uri)
parts = [part for part in parsed.full_path.split("/") if part]
@@ -789,7 +791,19 @@ class ContentWriteCoordinator:
root_uri = uri
if parts[0] == "resources":
if len(parts) >= 2:
- root_uri = VikingURI.build("resources", parts[1])
+ if anchor_to_parent:
+ # Content writes anchor the semantic refresh at the written file's
+ # direct parent directory, so the changed file is a direct child of
+ # the DAG run root: its own L2 vector and the parent's L0/L1 are
+ # (re)generated from a single-directory run, while ancestor summaries
+ # refresh via the existing parent bubble. Collapsing to the project
+ # root instead would force the run to traverse the whole project
+ # subtree before the changed file's vector is even dispatched.
+ parent = parsed.parent
+ if parent is not None:
+ root_uri = parent.uri
+ else:
+ root_uri = VikingURI.build("resources", parts[1])
elif parts[0] == "user":
if "resources" in parts:
resources_idx = parts.index("resources")
diff --git a/tests/server/test_content_write_service.py b/tests/server/test_content_write_service.py
index f4053f6d6..fb81ac433 100644
--- a/tests/server/test_content_write_service.py
+++ b/tests/server/test_content_write_service.py
@@ -497,7 +497,6 @@ async def test_resource_write_updates_target_and_queues_refresh_before_return(mo
assert captured_enqueue["root_uri"] == root_uri
assert captured_enqueue["changed_uri"] == file_uri
assert captured_enqueue["change_type"] == "modified"
- assert captured_enqueue["recursive"] is True
assert viking_fs.delete_temp_calls == []
assert lock_manager.release_calls == ["lock-1"]
@@ -935,7 +934,6 @@ async def test_create_mode_resource_scope(monkeypatch):
assert kwargs["changed_uri"] == file_uri
assert kwargs["context_type"] == "resource"
assert kwargs["change_type"] == "added"
- assert kwargs["recursive"] is True
del kwargs
return None
@@ -953,42 +951,36 @@ async def test_create_mode_resource_scope(monkeypatch):
assert viking_fs.content[file_uri] == "content"
-async def test_create_mode_nested_resource_refresh_stays_recursive_after_root_changes(monkeypatch):
- first_uri = "viking://resources/dir_repro/dir1/dir2/test12.md"
- second_uri = "viking://resources/dir_repro/dir1/dir2/test34.md"
- resource_root = "viking://resources/dir_repro"
- first_parent = "viking://resources/dir_repro/dir1/dir2"
- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
- viking_fs = _FakeVikingFSForCreate(
- file_uri=first_uri,
- root_uri=resource_root,
- file_exists=False,
- existing_dirs=set(),
- )
- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)
- lock_manager = _FakeLockManager()
- enqueued = []
+class _AnyDirVikingFS:
+ """Stats the given file uri as a file and every other uri as a directory."""
- monkeypatch.setattr("openviking.storage.content_write.get_lock_manager", lambda: lock_manager)
+ def __init__(self, file_uri: str):
+ self._file_uri = file_uri
- async def _fake_enqueue_semantic_refresh(**kwargs):
- enqueued.append(kwargs)
+ async def stat(self, uri: str, ctx=None):
+ del ctx
+ return {"isDir": uri != self._file_uri}
- monkeypatch.setattr(coordinator, "_enqueue_semantic_refresh", _fake_enqueue_semantic_refresh)
- await coordinator.write(uri=first_uri, content="hello", mode="create", ctx=ctx)
+async def test_resource_write_anchors_nested_file_to_direct_parent():
+ """A resource content write anchors the semantic refresh at the written file's
+ direct parent directory (anchor_to_parent=True), so the changed file is a direct
+ child of the DAG run root: its own L2 vector and the parent's L0/L1 are generated
+ from a single-directory run instead of a recursive walk of the whole project subtree.
+ set_tags keeps the project-root collapse (the default), which the derived
+ ``.abstract.md`` sidecar mapping relies on."""
+ file_uri = "viking://resources/dir1/sub_dir/sub_dir_1/test.md"
+ parent_uri = "viking://resources/dir1/sub_dir/sub_dir_1"
+ project_root = "viking://resources/dir1"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ coordinator = ContentWriteCoordinator(viking_fs=_AnyDirVikingFS(file_uri))
- viking_fs._file_uri = second_uri
- viking_fs._file_exists = False
- await coordinator.write(uri=second_uri, content="world", mode="create", ctx=ctx)
+ write_anchor = await coordinator._resolve_root_uri(file_uri, ctx=ctx, anchor_to_parent=True)
+ set_tags_anchor = await coordinator._resolve_root_uri(file_uri, ctx=ctx)
- assert enqueued[0]["root_uri"] == first_parent
- assert enqueued[0]["changed_uri"] == first_uri
- assert enqueued[0]["recursive"] is True
- assert enqueued[1]["root_uri"] == resource_root
- assert enqueued[1]["changed_uri"] == second_uri
- assert enqueued[1]["recursive"] is True
+ assert write_anchor == parent_uri
+ assert set_tags_anchor == project_root
@pytest.mark.asyncio