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
feat(storage): add durable content write task workflow (#6352)
runyunzhou8540d3209e91
diff --git a/openviking/connector/delegate.py b/openviking/connector/delegate.pyindex b774b2ba4..4fba11d8b 100644--- a/openviking/connector/delegate.py+++ b/openviking/connector/delegate.py@@ -51,6 +51,7 @@ from openviking.resource.processing_mode import (ProcessingMode,)from openviking.server.identity import RequestContext+from openviking.service.task_domain import TaskPatchfrom openviking.utils.tags import normalize_search_tagsfrom openviking_cli.exceptions import InternalError, InvalidArgumentErrorfrom openviking_cli.utils import get_logger@@ -859,9 +860,9 @@ class ConnectorDelegate:)last_status = status- await task_tracker.update_stage(+ await task_tracker.patch_task(ov_task_id,- f"connector:{status}",+ TaskPatch(stage=f"connector:{status}"),account_id=ctx.account_id,user_id=ctx.user.user_id,)diff --git a/openviking/service/core.py b/openviking/service/core.pyindex c8bcc855b..cde6f86ca 100644--- a/openviking/service/core.py+++ b/openviking/service/core.py@@ -35,6 +35,7 @@ from openviking.storage.acl import AclManagerfrom openviking.storage.collection_schemas import init_context_collectionfrom openviking.storage.index_consistency import check_index_consistencyfrom openviking.storage.queuefs.add_resource_processor import AddResourceProcessor+from openviking.storage.queuefs.content_write_processor import ContentWriteProcessorfrom openviking.storage.queuefs.external_task_processor import ExternalTaskProcessorfrom openviking.storage.queuefs.queue_manager import QueueManager, init_queue_managerfrom openviking.storage.queuefs.reindex_processor import ReindexProcessor@@ -641,6 +642,11 @@ class OpenVikingService:dequeue_handler=ReindexProcessor(self._viking_fs),allow_create=True,)+ self._queue_manager.get_queue(+ self._queue_manager.CONTENT_WRITE,+ dequeue_handler=ContentWriteProcessor(self._viking_fs, self._vikingdb_manager),+ allow_create=True,+ )self._queue_manager.get_queue(self._queue_manager.SESSION_COMMIT,dequeue_handler=SessionCommitProcessor(diff --git a/openviking/service/external_task_service.py b/openviking/service/external_task_service.pyindex c075ec561..e8a1440d7 100644--- a/openviking/service/external_task_service.py+++ b/openviking/service/external_task_service.py@@ -12,6 +12,7 @@ from typing import Any, Awaitable, Callable, Iterable, Mapping, Protocolfrom uuid import uuid4from openviking.server.identity import RequestContext+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_tracker import TaskRecord, TaskStatus, get_task_trackerfrom openviking.service.task_tracker_concurrency import (KeyedAsyncLockPool,@@ -226,9 +227,9 @@ class ExternalTaskService:return taskenqueued = Falsetry:- await tracker.update_stage(+ await tracker.patch_task(task.task_id,- "queued",+ TaskPatch(stage="queued"),account_id=ctx.account_id,user_id=ctx.user.user_id,)@@ -384,9 +385,9 @@ class ExternalTaskService:poll_interval=provider.poll_interval_seconds,max_attempts=provider.poll_max_attempts,)- await tracker.update_task_auth(+ await tracker.patch_task(task_id,- {"external_task_id": external_task_id},+ TaskPatch(auth={"external_task_id": external_task_id}),account_id=account_id,user_id=user_id,)@@ -484,12 +485,11 @@ class ExternalTaskService:key not in task.meta or task.meta[key] != value for key, value in meta.items())):- await tracker.update_stage(+ await tracker.patch_task(task_id,- stage,+ TaskPatch(stage=stage, meta=meta),account_id=account_id,user_id=user_id,- meta=meta,)if snapshot.status in _ACTIVE_STATUSES:return False@@ -557,9 +557,9 @@ class ExternalTaskService:poll_interval=provider.poll_interval_seconds,max_attempts=max_attempts,)- await get_task_tracker().update_task_auth(+ await get_task_tracker().patch_task(ov_task_id,- {"external_task_id": external_task_id},+ TaskPatch(auth={"external_task_id": external_task_id}),account_id=account_id,user_id=user_id,)diff --git a/openviking/service/memory_compile.py b/openviking/service/memory_compile.pyindex 3c527ed64..12e75b6d7 100644--- a/openviking/service/memory_compile.py+++ b/openviking/service/memory_compile.py@@ -19,6 +19,7 @@ from openviking.core.path_variables import resolve_path_variablesfrom openviking.core.uri_validation import validate_request_viking_urifrom openviking.server.identity import RequestContextfrom openviking.service.fs_service import FSService+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_tracker import TaskRecord, get_task_trackerfrom openviking.service.task_work_index import bind_task_contextfrom openviking.session.memory.consolidation_context_provider import (@@ -138,9 +139,9 @@ class MemoryCompileRunner:}},)- await tracker.update_stage(+ await tracker.patch_task(task.task_id,- "queued",+ TaskPatch(stage="queued"),account_id=ctx.account_id,user_id=ctx.user.user_id,)diff --git a/openviking/service/reindex_executor.py b/openviking/service/reindex_executor.pyindex ef56e2375..b3c5749b5 100644--- a/openviking/service/reindex_executor.py+++ b/openviking/service/reindex_executor.py@@ -27,10 +27,12 @@ from openviking.core.namespace import ()from openviking.server.dependencies import get_servicefrom openviking.server.identity import RequestContext+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_tracker import TaskStatus, get_task_trackerfrom openviking.service.task_work_index import bind_task_context, get_task_contextfrom openviking.session.memory.utils.memory_file_utils import MemoryFileUtilsfrom openviking.storage.abstract_overview import body_for_preview, embedding_text_for_body+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs.embedding_msg_converter import EmbeddingMsgConverterfrom openviking.storage.queuefs.semantic_msg import SemanticMsgfrom openviking.storage.queuefs.semantic_processor import SemanticProcessor@@ -113,7 +115,7 @@ class _ReindexRunContext:counters: _ReindexCounterslock: dict | None = Nonelease: dict | None = None- lease_handed_off: bool = False+ lease_guard: PathLockGuard | None = Nonelock_root_uri: str | None = Noneingest_options: IngestOptions | None = Noneforce: bool = False@@ -235,7 +237,7 @@ class ReindexExecutor:from openviking.storage.queuefs.reindex_msg import ReindexMsgservice = None- lease = None+ lock_guard = Noneenqueued = Falsetry:service = get_service()@@ -247,7 +249,8 @@ class ReindexExecutor:else service.viking_fs._async_agfs.pathlock_acquire_exact)lease = await acquire(path)- handoff = await service.viking_fs._async_agfs.pathlock_to_handoff(lease)+ lock_guard = PathLockGuard(service.viking_fs._async_agfs, lease)+ handoff = await lock_guard.to_handoff()msg = ReindexMsg(task_id=task.task_id,uri=uri,@@ -267,17 +270,24 @@ class ReindexExecutor:lock_handoff=handoff,)queue_manager = get_queue_manager()- with bind_task_context(task.task_id, ctx.account_id, ctx.user.user_id):- await queue_manager.enqueue(queue_manager.REINDEX, msg.to_dict())- enqueued = True- await service.viking_fs._async_agfs.pathlock_handoff(lease)- lease = None- await tracker.update_stage(- task.task_id, "queued", account_id=ctx.account_id, user_id=ctx.user.user_id++ async def dispatch() -> str:+ nonlocal enqueued+ with bind_task_context(task.task_id, ctx.account_id, ctx.user.user_id):+ result = await queue_manager.enqueue(queue_manager.REINDEX, msg.to_dict())+ enqueued = True+ return result++ await lock_guard.handoff_and_dispatch(handoff, dispatch)+ await tracker.patch_task(+ task.task_id,+ TaskPatch(stage="queued"),+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,)except BaseException:- if lease is not None and service is not None:- await service.viking_fs._async_agfs.pathlock_release(lease)+ if lock_guard is not None:+ await lock_guard.close()if not enqueued:await tracker.fail(task.task_id,@@ -605,6 +615,7 @@ class ReindexExecutor:if not root_is_dir:acquire_lock = service.viking_fs._async_agfs.pathlock_acquire_exactlease = existing_lease or await acquire_lock(path)+ lease_guard = PathLockGuard(service.viking_fs._async_agfs, lease)run: _ReindexRunContext | None = Nonetry:borrowed = (@@ -617,6 +628,7 @@ class ReindexExecutor:counters=counters,lock=borrowed,lease=lease,+ lease_guard=lease_guard,ingest_options=ingest_options,force=force,root_is_dir=root_is_dir,@@ -673,9 +685,7 @@ class ReindexExecutor:await get_task_tracker().build_queue_status(task_context.task_id),)finally:- active_lease = run.lease if run is not None else lease- if active_lease is not None and (run is None or not run.lease_handed_off):- await service.viking_fs._async_agfs.pathlock_release(active_lease)+ await lease_guard.close()return {"status": "completed",@@ -900,35 +910,34 @@ class ReindexExecutor:)acquired_plan_lease = Trueassert plan_lease is not None- handoff = await agfs.pathlock_to_handoff(plan_lease)- handed_off = False+ plan_guard = (+ run.lease_guard+ if not acquired_plan_lease and run.lease_guard is not None+ else PathLockGuard(agfs, plan_lease)+ )+ handoff = await plan_guard.to_handoff()+ msg.lock_handoff = handofftry:- await agfs.pathlock_handoff(plan_lease)- handed_off = True- msg.lock_handoff = handoff- enqueue_id = await semantic_queue.enqueue(msg)+ enqueue_id = await plan_guard.handoff_and_dispatch(+ handoff,+ lambda: semantic_queue.enqueue(msg),+ accepted=lambda result: result != "deduplicated",+ )if enqueue_id == "deduplicated":- lock = await agfs.pathlock_adopt(handoff)- await agfs.pathlock_release(lock)+ await plan_guard.close()if not acquired_plan_lease:- run.lease_handed_off = Truerun.lease = Nonerun.lock = Nonereturnif not acquired_plan_lease:- run.lease_handed_off = Truerun.lease = Nonerun.lock = Noneexcept BaseException:- if handed_off:- recovered_lease = await agfs.pathlock_adopt(handoff)- if acquired_plan_lease:- await agfs.pathlock_release(recovered_lease)- else:- run.lease = recovered_lease- run.lock = await agfs.pathlock_as_borrowed(run.lease)- elif acquired_plan_lease:- await agfs.pathlock_release(plan_lease)+ if acquired_plan_lease:+ await plan_guard.close()+ elif plan_guard.lease is not None:+ run.lease = plan_guard.lease+ run.lock = await agfs.pathlock_as_borrowed(run.lease)raiseasync def _reindex_skill(diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.pyindex b8c52b6ee..002a0ba04 100644--- a/openviking/service/resource_service.py+++ b/openviking/service/resource_service.py@@ -16,6 +16,7 @@ import timefrom collections.abc import Awaitable, Callablefrom dataclasses import dataclass, fieldfrom pathlib import Path+from types import SimpleNamespacefrom typing import TYPE_CHECKING, Any, Dict, List, Optionalfrom urllib.parse import urlparsefrom uuid import uuid4@@ -66,7 +67,9 @@ from openviking.server.user_config import (effective_resource_add_target,effective_skill_add_target,)+from openviking.service.task_domain import TaskPatchfrom openviking.storage.acl import AclAction, AclSpec+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs import QueueManager, get_queue_managerfrom openviking.storage.queuefs.add_resource_msg import AddResourcePhasefrom openviking.storage.viking_fs import LS_ALL_NODES, VikingFS@@ -582,7 +585,7 @@ class ResourceService:return Noneasync_agfs = getattr(self._viking_fs, "_async_agfs", None)if async_agfs is not None:- return await async_agfs.pathlock_to_handoff(lock_ref)+ return await PathLockGuard(async_agfs, lock_ref).to_handoff()to_handoff = getattr(lock_ref, "to_handoff", None)if callable(to_handoff):handoff = to_handoff()@@ -603,6 +606,17 @@ class ResourceService:if inspect.isawaitable(result):await result+ async def _adopt_lock_ref(self, handoff_ref: Any) -> Any:+ """Recover ownership from a handoff after enqueue fails."""+ async_agfs = getattr(self._viking_fs, "_async_agfs", None)+ if async_agfs is not None:+ return await async_agfs.pathlock_adopt(handoff_ref)+ adopt = getattr(handoff_ref, "adopt", None)+ if callable(adopt):+ result = adopt()+ return await result if inspect.isawaitable(result) else result+ raise RuntimeError("Lock handoff cannot be adopted")+async def _release_lock_ref(self, lock_ref: Any) -> None:"""Release either a native pathlock ref or legacy lease."""if lock_ref is None:@@ -706,6 +720,12 @@ class ResourceService:tracker = get_task_tracker()task = Noneenqueued = False+ lock_ops = getattr(self._viking_fs, "_async_agfs", None) or SimpleNamespace(+ pathlock_handoff=self._handoff_lock_ref,+ pathlock_adopt=self._adopt_lock_ref,+ pathlock_release=self._release_lock_ref,+ )+ lock_guard = PathLockGuard(lock_ops, resource_lock)try:task = await tracker.create("add_resource",@@ -720,25 +740,33 @@ class ResourceService:)task_account_id = getattr(task, "account_id", msg.account_id)task_user_id = getattr(task, "user_id", msg.user_id)- with bind_task_context(task.task_id, task_account_id, task_user_id):- await get_queue_manager().enqueue(queue_name, msg.to_dict())- enqueued = True- if on_enqueued is not None:- on_enqueued()++ async def dispatch() -> str:+ nonlocal enqueued+ with bind_task_context(task.task_id, task_account_id, task_user_id):+ result = await get_queue_manager().enqueue(queue_name, msg.to_dict())+ enqueued = True+ if on_enqueued is not None:+ on_enqueued()+ return result+if resource_lock is not None:- await self._handoff_lock_ref(resource_lock)- resource_lock = None- await tracker.update_stage(+ await lock_guard.handoff_and_dispatch(msg.lock_handoff, dispatch)+ else:+ await dispatch()+ await tracker.patch_task(task.task_id,- "queued",+ TaskPatch(stage="queued"),account_id=msg.account_id,user_id=msg.user_id,)except BaseException:- if resource_lock is not None:- if on_failed_with_lock is not None:- await on_failed_with_lock(resource_lock)- await self._release_lock_ref(resource_lock)+ if lock_guard.lease is not None:+ try:+ if on_failed_with_lock is not None:+ await on_failed_with_lock(lock_guard.lease)+ finally:+ await lock_guard.close()if task is not None and not enqueued:await tracker.fail(task.task_id,@@ -809,8 +837,11 @@ class ResourceService:saved = dict(task.meta.get("feishu_responses", {})) if task else {}async def save_response(entry: str, response_id: str) -> None:- await tracker.record_feishu_response(- msg.task_id, entry, response_id, ctx.account_id, ctx.user.user_id+ await tracker.patch_task(+ msg.task_id,+ TaskPatch(meta={"feishu_responses": {entry: response_id}}),+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,)saved[entry] = response_iddiff --git a/openviking/service/task_domain.py b/openviking/service/task_domain.pyindex ffadf1d6e..5c323e77c 100644--- a/openviking/service/task_domain.py+++ b/openviking/service/task_domain.py@@ -57,6 +57,52 @@ class TaskWorkRejected(Exception):"""Task lifecycle state prevented Work registration."""+_TASK_PATCHABLE_FIELDS = frozenset({"result", "stage", "meta", "auth"})+++@dataclass(frozen=True)+class TaskPatch:+ """One atomic, field-scoped Task mutation.++ Non-``None`` values update their matching fields. ``clear_fields`` is the+ explicit counterpart for nullable fields, keeping ``None`` available to+ mean "leave unchanged". Status, errors, and Work state remain owned by+ their dedicated state-transition methods.+ """++ result: Optional[Dict[str, Any]] = None+ stage: Optional[str] = None+ meta: Optional[Dict[str, Any]] = None+ auth: Optional[Dict[str, Any]] = None+ clear_fields: frozenset[str] = field(default_factory=frozenset)+ add_enqueue_complete_work_ids: frozenset[str] = field(default_factory=frozenset)++ def __post_init__(self) -> None:+ clear_fields = frozenset(str(name) for name in self.clear_fields)+ unknown = clear_fields - _TASK_PATCHABLE_FIELDS+ if unknown:+ raise ValueError(f"unsupported TaskPatch clear fields: {sorted(unknown)}")+ for field_name in clear_fields:+ if getattr(self, field_name) is not None:+ raise ValueError(f"TaskPatch cannot update and clear {field_name}")+ object.__setattr__(self, "clear_fields", clear_fields)+ object.__setattr__(+ self,+ "add_enqueue_complete_work_ids",+ frozenset(str(work_id) for work_id in self.add_enqueue_complete_work_ids if work_id),+ )++ def is_empty(self) -> bool:+ return (+ self.result is None+ and self.stage is None+ and self.meta is None+ and self.auth is None+ and not self.clear_fields+ and not self.add_enqueue_complete_work_ids+ )++@dataclass(frozen=True)class TaskRetentionPolicy:completed_seconds: floatdiff --git a/openviking/service/task_store.py b/openviking/service/task_store.pyindex 6b19fd387..7924cbb43 100644--- a/openviking/service/task_store.py+++ b/openviking/service/task_store.py@@ -24,6 +24,7 @@ from openviking.service.task_domain import (TERMINAL_TASK_STATUSES,DeletedTask,DeleteExpiredResult,+ TaskPatch,TaskRecord,TaskRetentionPolicy,TaskStatus,@@ -205,29 +206,10 @@ class TaskWorkStore(Protocol):user_id: Optional[str],) -> StoreOperationResult: ...- async def update_stage(+ async def patch_task(self,task_id: str,- stage: str,- *,- meta: Optional[Dict[str, Any]],- account_id: Optional[str],- user_id: Optional[str],- ) -> StoreOperationResult: ...-- async def update_auth(- self,- task_id: str,- values: Dict[str, Any],- *,- account_id: str,- user_id: str,- ) -> StoreOperationResult: ...-- async def merge_meta(- self,- task_id: str,- values: Dict[str, Any],+ patch: TaskPatch,*,account_id: str,user_id: str,@@ -265,24 +247,6 @@ class TaskWorkStore(Protocol):user_id: str,) -> StoreOperationResult: ...- async def mark_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> StoreOperationResult: ...-- async def is_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> bool: ...-async def add_work(self, work: WorkRecord, *, account_id: str, user_id: str) -> StoreOperationResult: ...@@ -507,53 +471,20 @@ class PersistentTaskStore(TaskWorkStore):return await self._mutate_task(task_id, account_id, user_id, mutate)- async def update_stage(+ async def patch_task(self,task_id: str,- stage: str,- *,- meta: Optional[Dict[str, Any]],- account_id: Optional[str],- user_id: Optional[str],- ) -> StoreOperationResult:- def mutate(task: TaskRecord, _work_set: _WorkSet) -> StoreOperationResult:- if task.status not in ACTIVE_TASK_STATUSES:- return _inactive_task_result(task, "Task stage cannot be updated")- task.stage = stage- if meta:- _deep_merge(task.meta, deepcopy(meta))- return StoreOperationResult.applied()-- return await self._mutate_task(task_id, account_id, user_id, mutate)-- async def update_auth(- self,- task_id: str,- values: Dict[str, Any],+ patch: TaskPatch,*,account_id: str,user_id: str,) -> StoreOperationResult:def mutate(task: TaskRecord, _work_set: _WorkSet) -> StoreOperationResult:if task.status not in ACTIVE_TASK_STATUSES:- return _inactive_task_result(task, "Task auth cannot be updated")- _deep_merge(task.auth, deepcopy(values))- return StoreOperationResult.applied()-- return await self._mutate_task(task_id, account_id, user_id, mutate)-- async def merge_meta(- self,- task_id: str,- values: Dict[str, Any],- *,- account_id: str,- user_id: str,- ) -> StoreOperationResult:- def mutate(task: TaskRecord, _work_set: _WorkSet) -> StoreOperationResult:- if task.status not in (TaskStatus.PENDING, TaskStatus.RUNNING):- return _inactive_task_result(task, "Task meta cannot be updated")- _deep_merge(task.meta, deepcopy(values))+ return _inactive_task_result(task, "Task cannot be patched")+ if patch.is_empty():+ return StoreOperationResult.noop()+ _apply_task_patch(task, patch)return StoreOperationResult.applied()return await self._mutate_task(task_id, account_id, user_id, mutate)@@ -649,40 +580,6 @@ class PersistentTaskStore(TaskWorkStore):return await self._mutate_task(task_id, account_id, user_id, mutate)- async def mark_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> StoreOperationResult:- def mutate(task: TaskRecord, _work_set: _WorkSet) -> StoreOperationResult:- if task.status not in ACTIVE_TASK_STATUSES:- return StoreOperationResult.noop(- StoreOperationCode.ALREADY_TERMINAL,- f"Task is already {task.status.value}",- )- completed = set(task.auth.get("enqueue_complete_work_ids") or ())- if work_id in completed:- return StoreOperationResult.noop(StoreOperationCode.DUPLICATE)- completed.add(work_id)- task.auth["enqueue_complete_work_ids"] = sorted(completed)- return StoreOperationResult.applied()-- return await self._mutate_task(task_id, account_id, user_id, mutate)-- async def is_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> bool:- task = await self.get_task(task_id, account_id=account_id, user_id=user_id)- return bool(task and work_id in set(task.auth.get("enqueue_complete_work_ids") or ()))-async def add_work(self, work: WorkRecord, *, account_id: str, user_id: str) -> StoreOperationResult:@@ -1695,6 +1592,33 @@ def _deep_merge(target: Dict[str, Any], values: Dict[str, Any]) -> None:target[key] = value+def _apply_task_patch(task: TaskRecord, patch: TaskPatch) -> None:+ if "result" in patch.clear_fields:+ task.result = None+ elif patch.result is not None:+ task.result = deepcopy(patch.result)++ if "stage" in patch.clear_fields:+ task.stage = None+ elif patch.stage is not None:+ task.stage = patch.stage++ if "meta" in patch.clear_fields:+ task.meta = {}+ elif patch.meta is not None:+ _deep_merge(task.meta, deepcopy(patch.meta))++ if "auth" in patch.clear_fields:+ task.auth = {}+ elif patch.auth is not None:+ _deep_merge(task.auth, deepcopy(patch.auth))++ if patch.add_enqueue_complete_work_ids:+ completed = set(task.auth.get("enqueue_complete_work_ids") or ())+ completed.update(patch.add_enqueue_complete_work_ids)+ task.auth["enqueue_complete_work_ids"] = sorted(completed)++def _inactive_task_result(task: TaskRecord, message: str) -> StoreOperationResult:if task.status in TERMINAL_TASK_STATUSES:return StoreOperationResult.rejected(diff --git a/openviking/service/task_store_mysql.py b/openviking/service/task_store_mysql.pyindex 7ecc3014a..ecbb3338b 100644--- a/openviking/service/task_store_mysql.py+++ b/openviking/service/task_store_mysql.py@@ -28,6 +28,7 @@ from openviking.service.task_domain import (TERMINAL_TASK_STATUSES,DeletedTask,DeleteExpiredResult,+ TaskPatch,TaskRecord,TaskRetentionPolicy,TaskStatus,@@ -992,71 +993,28 @@ class MySQLTaskWorkStore(TaskWorkStore):await self._store_event_history(session, task_id, task.execution_events)return StoreOperationResult.applied()- async def update_stage(+ async def patch_task(self,task_id: str,- stage: str,- *,- meta: Optional[Dict[str, Any]],- account_id: Optional[str],- user_id: Optional[str],- ) -> StoreOperationResult:- Task = self._models.Task- async with self._transaction() as session:- query_result = await session.execute(- select(- Task.status,- Task.stage,- Task.meta,- Task.updated_at,- )- .where(*self._task_query(Task, task_id, account_id, user_id))- .with_for_update()- )- row = query_result.one_or_none()- if row is None:- return _not_found_result()- if row.status not in {item.value for item in ACTIVE_TASK_STATUSES}:- return _inactive_result(row.status, "Task stage cannot be updated")-- previous = TaskRecord(- task_id=task_id,- task_type="",- status=TaskStatus(row.status),- stage=row.stage,- meta=_json_copy(row.meta) or {},- execution_events=await self._event_history(session, task_id),- )- task = deepcopy(previous)- task.stage = stage- if meta:- _deep_merge(task.meta, deepcopy(meta))- append_task_events(task, previous)- await session.execute(- update(Task)- .where(*self._task_query(Task, task_id, account_id, user_id))- .values(- stage=task.stage,- meta=_json_copy(task.meta),- is_internal=bool(task.meta.get("internal")),- updated_at=TaskRecord.next_updated_at(row.updated_at),- )- )- await self._store_event_history(session, task_id, task.execution_events)- return StoreOperationResult.applied()-- async def update_auth(- self,- task_id: str,- values: Dict[str, Any],+ patch: TaskPatch,*,account_id: str,user_id: str,) -> StoreOperationResult:Task = self._models.Taskasync with self._transaction() as session:+ selected = [Task.status, Task.updated_at]+ if "stage" in patch.clear_fields or patch.stage is not None:+ selected.append(Task.stage)+ if "meta" not in patch.clear_fields and patch.meta is not None:+ selected.append(Task.meta)+ if (+ "auth" not in patch.clear_fields+ and (patch.auth is not None or patch.add_enqueue_complete_work_ids)+ ):+ selected.append(Task.auth)result = await session.execute(- select(Task.status, Task.auth, Task.updated_at)+ select(*selected).where(*self._task_query(Task, task_id, account_id, user_id)).with_for_update())@@ -1064,49 +1022,56 @@ class MySQLTaskWorkStore(TaskWorkStore):if row is None:return _not_found_result()if row.status not in {item.value for item in ACTIVE_TASK_STATUSES}:- return _inactive_result(row.status, "Task auth cannot be updated")- auth = _json_copy(row.auth) or {}- _deep_merge(auth, deepcopy(values))- await session.execute(- update(Task)- .where(*self._task_query(Task, task_id, account_id, user_id))- .values(- auth=auth,- updated_at=TaskRecord.next_updated_at(row.updated_at),- )- )- return StoreOperationResult.applied()+ return _inactive_result(row.status, "Task cannot be patched")+ if patch.is_empty():+ return StoreOperationResult.noop()- async def merge_meta(- self,- task_id: str,- values: Dict[str, Any],- *,- account_id: str,- user_id: str,- ) -> StoreOperationResult:- Task = self._models.Task- async with self._transaction() as session:- result = await session.execute(- select(Task.status, Task.meta, Task.updated_at)- .where(*self._task_query(Task, task_id, account_id, user_id))- .with_for_update()- )- row = result.one_or_none()- if row is None:- return _not_found_result()- if row.status not in (TaskStatus.PENDING.value, TaskStatus.RUNNING.value):- return _inactive_result(row.status, "Task meta cannot be updated")- meta = _json_copy(row.meta) or {}- _deep_merge(meta, deepcopy(values))+ values: dict[str, Any] = {+ "updated_at": TaskRecord.next_updated_at(row.updated_at),+ }+ if "result" in patch.clear_fields or patch.result is not None:+ values["result"] = (+ None if "result" in patch.clear_fields else _json_copy(patch.result)+ )+ if "stage" in patch.clear_fields or patch.stage is not None:+ stage = None if "stage" in patch.clear_fields else patch.stage+ values["stage"] = stage+ if stage != row.stage:+ previous = TaskRecord(+ task_id=task_id,+ task_type="",+ status=TaskStatus(row.status),+ stage=row.stage,+ execution_events=await self._event_history(session, task_id),+ )+ task = deepcopy(previous)+ task.stage = stage+ append_task_events(task, previous)+ await self._store_event_history(session, task_id, task.execution_events)+ if "meta" in patch.clear_fields or patch.meta is not None:+ meta = {}+ if "meta" not in patch.clear_fields:+ meta = _json_copy(row.meta) or {}+ _deep_merge(meta, _json_copy(patch.meta))+ values["meta"] = meta+ values["is_internal"] = bool(meta.get("internal"))+ if (+ "auth" in patch.clear_fields+ or patch.auth is not None+ or patch.add_enqueue_complete_work_ids+ ):+ auth = {} if "auth" in patch.clear_fields else (_json_copy(row.auth) or {})+ if patch.auth is not None:+ _deep_merge(auth, _json_copy(patch.auth))+ if patch.add_enqueue_complete_work_ids:+ completed = set(auth.get("enqueue_complete_work_ids") or ())+ completed.update(patch.add_enqueue_complete_work_ids)+ auth["enqueue_complete_work_ids"] = sorted(completed)+ values["auth"] = authawait session.execute(update(Task).where(*self._task_query(Task, task_id, account_id, user_id))- .values(- meta=meta,- is_internal=bool(meta.get("internal")),- updated_at=TaskRecord.next_updated_at(row.updated_at),- )+ .values(**values))return StoreOperationResult.applied()@@ -1343,60 +1308,6 @@ class MySQLTaskWorkStore(TaskWorkStore):return StoreOperationResult.applied()return StoreOperationResult.applied()- async def mark_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> StoreOperationResult:- Task = self._models.Task- async with self._transaction() as session:- result = await session.execute(- select(Task.status, Task.auth, Task.updated_at)- .where(*self._task_query(Task, task_id, account_id, user_id))- .with_for_update()- )- row = result.one_or_none()- if row is None:- return _not_found_result()- if row.status not in {item.value for item in ACTIVE_TASK_STATUSES}:- return StoreOperationResult.noop(- StoreOperationCode.ALREADY_TERMINAL,- f"Task is already {row.status}",- )- completed = set((row.auth or {}).get("enqueue_complete_work_ids") or ())- if work_id in completed:- return StoreOperationResult.noop(StoreOperationCode.DUPLICATE)- completed.add(work_id)- auth = _json_copy(row.auth) or {}- auth["enqueue_complete_work_ids"] = sorted(completed)- await session.execute(- update(Task)- .where(*self._task_query(Task, task_id, account_id, user_id))- .values(- auth=auth,- updated_at=TaskRecord.next_updated_at(row.updated_at),- )- )- return StoreOperationResult.applied()-- async def is_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> bool:- Task = self._models.Task- async with self._session_factory() as session:- auth = await session.scalar(- select(Task.auth).where(*self._task_query(Task, task_id, account_id, user_id))- )- return bool(auth and work_id in set(auth.get("enqueue_complete_work_ids") or ()))-async def add_work(self, work: WorkRecord, *, account_id: str, user_id: str) -> StoreOperationResult:diff --git a/openviking/service/task_tracker.py b/openviking/service/task_tracker.pyindex e448a0668..ebaf18316 100644--- a/openviking/service/task_tracker.py+++ b/openviking/service/task_tracker.py@@ -35,6 +35,7 @@ from openviking.service.task_domain import (TERMINAL_WORK_STATES,DeleteExpiredResult,QueueWorkSummary,+ TaskPatch,TaskRecord,TaskRetentionPolicy,TaskStatus,@@ -449,42 +450,19 @@ class TaskTracker:) -> None:await self._store.start_task(task_id, stage=stage, account_id=account_id, user_id=user_id)- async def update_stage(+ async def patch_task(self,task_id: str,- stage: str,- account_id: Optional[str] = None,- user_id: Optional[str] = None,- *,- meta: Optional[Dict[str, Any]] = None,- ) -> None:- await self._store.update_stage(- task_id, stage, meta=meta, account_id=account_id, user_id=user_id- )-- async def update_task_auth(- self,- task_id: str,- values: Dict[str, Any],+ patch: TaskPatch,*,account_id: str,user_id: str,- ) -> None:- self._validate_owner(account_id, user_id)- await self._store.update_auth(task_id, values, account_id=account_id, user_id=user_id)-- async def record_feishu_response(- self,- task_id: str,- entry: str,- response_id: str,- account_id: str,- user_id: str,- ) -> None:+ ) -> StoreOperationResult:+ """Atomically apply permitted Task fields without changing task state."""self._validate_owner(account_id, user_id)- await self._store.merge_meta(+ return await self._store.patch_task(task_id,- {"feishu_responses": {entry: response_id}},+ patch,account_id=account_id,user_id=user_id,)@@ -1164,21 +1142,6 @@ class TaskTracker:poll_interval if remaining is None else min(poll_interval, remaining))- async def mark_enqueue_complete(- self,- task_id: str,- work_id: str,- *,- account_id: str,- user_id: str,- ) -> None:- await self._store.mark_enqueue_complete(- task_id,- work_id,- account_id=account_id,- user_id=user_id,- )-async def is_enqueue_complete(self,task_id: str,@@ -1187,12 +1150,8 @@ class TaskTracker:account_id: str,user_id: str,) -> bool:- return await self._store.is_enqueue_complete(- task_id,- work_id,- account_id=account_id,- user_id=user_id,- )+ auth = await self.get_task_auth(task_id, account_id=account_id, user_id=user_id)+ return work_id in set(auth.get("enqueue_complete_work_ids") or ())async def build_queue_status(self,diff --git a/openviking/storage/content_write.py b/openviking/storage/content_write.pyindex 5f53d98f8..c2ca630cf 100644--- a/openviking/storage/content_write.py+++ b/openviking/storage/content_write.py@@ -10,8 +10,10 @@ import binasciiimport hashlibimport osfrom collections import defaultdict+from copy import deepcopyfrom dataclasses import dataclass, field, replace-from typing import Any, Dict, Optional+from typing import Any, Awaitable, Callable, Dict, Optional+from uuid import uuid4from openviking.core.context import ContextLevelfrom openviking.core.namespace import (@@ -28,9 +30,16 @@ from openviking.resource.processing_mode import ()from openviking.resource.watch_storage import is_watch_task_control_urifrom openviking.server.identity import RequestContext-from openviking.service.task_tracker import get_task_tracker, run_tracked_operation+from openviking.service.task_tracker import (+ TaskStatus,+ get_task_tracker,+)from openviking.service.task_tracker_concurrency import run_to_completion-from openviking.service.task_work_index import get_enqueued_work_ids, get_task_context+from openviking.service.task_work_index import (+ bind_task_context,+ get_enqueued_work_ids,+ get_task_context,+)from openviking.session.memory.memory_updater import MemoryUpdaterfrom openviking.session.memory.utils.memory_file_utils import MemoryFileUtilsfrom openviking.session.memory.utils.resource_refs import (@@ -45,7 +54,6 @@ from openviking.storage.abstract_overview import ()from openviking.storage.acl import AclAction, AclSpecfrom openviking.storage.context_update_execution import (- commit_and_enqueue_plan,enqueue_direct_index_actions,)from openviking.storage.context_update_plan import (@@ -60,6 +68,7 @@ from openviking.storage.context_update_plan import (from openviking.storage.errors import LockAcquisitionError, ResourceBusyErrorfrom openviking.storage.index_action import FieldPatch, IndexActionfrom openviking.storage.internal_names import is_storage_internal_name+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs import SemanticMsg, get_queue_managerfrom openviking.storage.queuefs.semantic_msg import build_semantic_coalesce_keyfrom openviking.storage.queuefs.semantic_ops.freshness_policy import FreshnessAction@@ -178,6 +187,31 @@ def _result_has_queue_errors(result: Any) -> bool:return any(_queue_has_errors(queue_status, name) for name in ("Semantic", "Embedding"))+_ROOT_OWNS_FAILURE_ATTR = "_openviking_content_write_root_owns_failure"+++def _mark_root_owns_failure(exc: BaseException) -> None:+ try:+ setattr(exc, _ROOT_OWNS_FAILURE_ATTR, True)+ except Exception:+ pass+++def _root_owns_failure(exc: BaseException) -> bool:+ return bool(getattr(exc, _ROOT_OWNS_FAILURE_ATTR, False))+++def _failure_payload(exc: BaseException) -> Dict[str, Any]:+ payload: Dict[str, Any] = {+ "message": str(exc) or type(exc).__name__,+ "type": type(exc).__name__,+ }+ if isinstance(exc, OpenVikingError):+ payload["code"] = exc.code+ payload["details"] = exc.details+ return payload++@dataclass(frozen=True)class _BatchRefreshOutcome:queue_status: Optional[Dict[str, Any]]@@ -264,14 +298,26 @@ class ContentWriteCoordinator:tag_mode: str = "replace",acl: AclSpec | Dict[str, Any] | None = None,) -> Dict[str, Any]:- return_terminal_result = wait and get_task_context() is None+ task = None+ task_id = None++ async def create_root_task() -> Any:+ nonlocal task, task_id+ if task is None:+ task_id = task_id or str(uuid4())+ task = await get_task_tracker().create(+ "content_write",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ resource_id=uri,+ task_id=task_id,+ )+ if task is None:+ raise RuntimeError("Content write Task was not created")+ return task- async def execute() -> Dict[str, Any]:- context = get_task_context()- wait_for_queues = not (- return_terminal_result and context is not None and context.work_id is not None- )- return await self._write(+ try:+ result = await self._write(uri=uri,content=content,ctx=ctx,@@ -281,19 +327,153 @@ class ContentWriteCoordinator:tags=tags,tag_mode=tag_mode,acl=acl,- wait_for_queues=wait_for_queues,+ root_operation="content_write",+ root_task_factory=create_root_task,)+ except BaseException as exc:+ if task_id is not None and not _root_owns_failure(exc):+ await self._fail_content_write_task(+ task.task_id if task is not None else task_id, exc, ctx=ctx+ )+ raise+ if task is None:+ raise RuntimeError("Content write completed without creating a root Task")+ return await self._return_or_wait_for_content_write_task(+ task.task_id,+ result,+ ctx=ctx,+ wait=wait,+ )- result, _task_id = await run_tracked_operation(- "content_write",+ async def _fail_content_write_task(+ self,+ task_id: str,+ exc: BaseException,+ *,+ ctx: RequestContext,+ ) -> None:+ await run_to_completion(+ lambda: get_task_tracker().fail(+ task_id,+ str(exc) or type(exc).__name__,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ )++ async def _return_or_wait_for_content_write_task(+ self,+ task_id: str,+ result: Dict[str, Any],+ *,+ ctx: RequestContext,+ wait: bool,+ accept_failed_result: Any = None,+ ) -> Dict[str, Any]:+ if not wait:+ result.setdefault("task_id", task_id)+ return result+ terminal = await self._wait_for_content_write_terminal(task_id, ctx=ctx)+ terminal_result = (+ deepcopy(terminal.result)+ if isinstance(terminal.result, dict)+ else deepcopy(result)+ )+ if isinstance(terminal_result, dict):+ terminal_result.pop("task_id", None)+ if terminal.status == TaskStatus.FAILED and not (+ accept_failed_result is not None and accept_failed_result(terminal_result)+ ):+ raise OpenVikingError(+ terminal.error or "Task processing failed",+ code="PROCESSING_ERROR",+ details={"task_id": task_id},+ )+ if terminal.status == TaskStatus.CANCELLED:+ raise OpenVikingError(+ "Task processing was cancelled",+ code="PROCESSING_ERROR",+ details={"task_id": task_id, "status": "cancelled"},+ )+ return terminal_result++ async def _wait_for_content_write_terminal(+ self,+ task_id: str,+ *,+ ctx: RequestContext,+ ) -> Any:+ tracker = get_task_tracker()+ try:+ return await tracker.wait(+ task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ except (TimeoutError, asyncio.TimeoutError) as exc:+ raise DeadlineExceededError("queue processing", None) from exc++ async def _enqueue_content_write_root(+ self,+ *,+ operation: str,+ resource_id: str,+ result: Dict[str, Any],+ refresh: Dict[str, Any],+ ctx: RequestContext,+ telemetry_id: str,+ lock_handoff: dict[str, Any] | None = None,+ lock_scope: str = "descendants",+ failure: dict[str, Any] | None = None,+ root_task_factory: Callable[[], Awaitable[Any]] | None = None,+ ) -> str:+ context = get_task_context()+ if context is None:+ if root_task_factory is None:+ raise RuntimeError("TaskContext is required for content-write root work")+ task = await root_task_factory()+ with bind_task_context(+ task.task_id,+ ctx.account_id,+ ctx.user.user_id,+ enqueued_work=[],+ ):+ return await self._enqueue_content_write_root(+ operation=operation,+ resource_id=resource_id,+ result=result,+ refresh=refresh,+ ctx=ctx,+ telemetry_id=telemetry_id,+ lock_handoff=lock_handoff,+ lock_scope=lock_scope,+ failure=failure,+ )+ from openviking.storage.queuefs.content_write_msg import ContentWriteMsg++ queue_manager = get_queue_manager()+ queue = queue_manager.get_queue(queue_manager.CONTENT_WRITE, allow_create=True)+ msg = ContentWriteMsg(+ task_id=context.task_id,+ operation=operation,+ resource_id=resource_id,+ result=deepcopy(result),+ refresh=deepcopy(refresh),account_id=ctx.account_id,user_id=ctx.user.user_id,- operation=execute,- wait=wait,- resource_id=uri,- return_terminal_result=return_terminal_result,+ role=str(ctx.role),+ group_ids=list(ctx.group_ids),+ actor_peer_id=ctx.actor_peer_id,+ bypass_acl=ctx.bypass_acl,+ telemetry_id=telemetry_id,+ lock_handoff=lock_handoff,+ lock_scope=lock_scope,+ failure=deepcopy(failure) if failure is not None else None,)- return result+ receipt = await queue.enqueue(msg.to_dict())+ if not receipt.work_id:+ raise RuntimeError("Content write root was enqueued without Task Work identity")+ return receipt.work_idasync def _write(self,@@ -307,7 +487,8 @@ class ContentWriteCoordinator:tags: list[str] | None = None,tag_mode: str = "replace",acl: AclSpec | Dict[str, Any] | None = None,- wait_for_queues: bool = True,+ root_operation: str = "content_write",+ root_task_factory: Callable[[], Awaitable[Any]] | None = None,) -> Dict[str, Any]:self._validate_mode(mode)processing_mode = normalize_processing_mode(processing_mode)@@ -349,11 +530,11 @@ class ContentWriteCoordinator:content=content,mode=mode,response_mode=response_mode,- wait=wait,ctx=ctx,telemetry_id=telemetry_id,ingest_options=ingest_options,- wait_for_queues=wait_for_queues,+ root_operation=root_operation,+ root_task_factory=root_task_factory,)return await self._write_direct_with_refresh(@@ -368,7 +549,8 @@ class ContentWriteCoordinator:telemetry_id=telemetry_id,processing_mode=processing_mode,ingest_options=ingest_options,- wait_for_queues=wait_for_queues,+ root_operation=root_operation,+ root_task_factory=root_task_factory,)async def batch_write(@@ -381,34 +563,57 @@ class ContentWriteCoordinator:skip_conflicts: bool = False,processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,) -> Dict[str, Any]:- return_terminal_result = wait and get_task_context() is None+ task = None+ task_id = None++ async def create_root_task() -> Any:+ nonlocal task, task_id+ if task is None:+ task_id = task_id or str(uuid4())+ task = await get_task_tracker().create(+ "content_batch_write",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ resource_id=root_uri,+ task_id=task_id,+ )+ if task is None:+ raise RuntimeError("Content batch write Task was not created")+ return task- async def execute() -> Dict[str, Any]:- context = get_task_context()- wait_for_queues = not (- return_terminal_result and context is not None and context.work_id is not None- )- return await self._batch_write(+ try:+ result = await self._batch_write(root_uri=root_uri,operations=operations,ctx=ctx,wait=wait,skip_conflicts=skip_conflicts,processing_mode=processing_mode,- wait_for_queues=wait_for_queues,+ root_operation="content_batch_write",+ root_task_factory=create_root_task,)-- result, _task_id = await run_tracked_operation(- "content_batch_write",- account_id=ctx.account_id,- user_id=ctx.user.user_id,- operation=execute,+ except BaseException as exc:+ if task_id is not None:+ if _root_owns_failure(exc):+ if wait and task is not None:+ await self._wait_for_content_write_terminal(task.task_id, ctx=ctx)+ else:+ await self._fail_content_write_task(+ task.task_id if task is not None else task_id,+ exc,+ ctx=ctx,+ )+ raise+ if task is None:+ raise RuntimeError("Content batch write completed without creating a root Task")+ result = await self._return_or_wait_for_content_write_task(+ task.task_id,+ result,+ ctx=ctx,wait=wait,- resource_id=root_uri,- accept_failed_result=_result_has_queue_errors if return_terminal_result else None,- return_terminal_result=return_terminal_result,+ accept_failed_result=_result_has_queue_errors if wait else None,)- if return_terminal_result:+ if wait:self._raise_refresh_errors(result.get("queue_status", {}))return result@@ -421,7 +626,8 @@ class ContentWriteCoordinator:wait: bool = True,skip_conflicts: bool = False,processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,- wait_for_queues: bool = True,+ root_operation: str = "content_batch_write",+ root_task_factory: Callable[[], Awaitable[Any]] | None = None,) -> Dict[str, Any]:"""Write a bundle under one directory, then refresh it as a batch.@@ -454,6 +660,7 @@ class ContentWriteCoordinator:f"resource is busy and cannot be written now: {normalized_root}",uri=normalized_root,) from exc+ lock_guard = PathLockGuard(self._viking_fs._async_agfs, lease)created: list[str] = []updated: list[str] = []@@ -467,6 +674,7 @@ class ContentWriteCoordinator:prepared_resources: dict[str, _PreparedInlineResource] = {}committed_resource_plans: list[_PreparedInlineResource] = []write_error: Exception | None = None+ lock_settled = Falsetry:for operation in normalized_operations:uri = operation["uri"]@@ -585,154 +793,107 @@ class ContentWriteCoordinator:if prepared is None:refresh_kinds[uri] = "added"except BaseException:- await self._viking_fs._async_agfs.pathlock_release(lease)+ await lock_guard.close()raisetelemetry_id = get_current_telemetry().telemetry_id- coordinator_work_id: str | None = None- refresh_outcome: Optional[_BatchRefreshOutcome] = Nonetry:- if refresh_kinds or sidecar_directories or committed_resource_plans:- coordinator_work_id = await self._enqueue_lock_coordinator(- root_uri=normalized_root,- context_type=context_type_for_uri(normalized_root),- ctx=ctx,- telemetry_id=telemetry_id,- lease=lease,+ if root_task_factory is not None:+ result = {+ "root_uri": normalized_root,+ "created": created,+ "updated": updated,+ "unchanged": unchanged,+ "queue_status": None,+ }+ if skip_conflicts:+ result["conflicts"] = conflicts+ has_refresh_work = bool(+ refresh_kinds or sidecar_directories or committed_resource_plans)- try:- for directory_uri, options in sorted(sidecar_directories.items()):- await self._vectorize_semantic_directory(- directory_uri=directory_uri,- ctx=ctx,- ingest_options=options,- )- direct_index_requested = False- for prepared in committed_resource_plans:- direct_index_requested = (- await enqueue_direct_index_actions(- prepared.plan.direct_index_actions,- ctx=ctx,- acl_update=prepared.ingest_options.acl_update,- )- or direct_index_requested- )- planned_refreshes = [- FileRefreshRequest(- file_uri=prepared.plan.file_refresh.file_uri,- created=not prepared.existed,- md5=prepared.plan.file_refresh.md5,- abstract=prepared.previous_abstract,- vector_action="upsert" if not prepared.existed else "",- ingest_options=prepared.ingest_options,- )- for prepared in committed_resource_plans- if prepared.plan.file_refresh is not None- ]- parent_result = (- await Summarizer(vlm_processor=None).refresh_file_parents(- changes=planned_refreshes,- ctx=ctx,- generation_trigger="content_write",- force_refresh=wait,- )- if planned_refreshes- else {"enqueued_count": 0, "parent_actions": {}}- )- legacy_refresh_result = (- await self._refresh_batch(- refresh_kinds=refresh_kinds,- ctx=ctx,- # The batch coordinator owns the one request-wide- # wait after both legacy special paths and RNFV- # plans have enqueued their work.- wait=False,- telemetry_id=telemetry_id,- file_md5s=file_md5s,- file_abstracts=file_abstracts,- file_ingest_options={- operation["uri"]: operation["ingest_options"]- for operation, _, _ in pending- },- )- if refresh_kinds- else None- )- # The production helper always returns _BatchRefreshOutcome;- # keep the empty outcome fallback for lightweight callers- # and test doubles that deliberately perform no refresh.- legacy_outcome = (- legacy_refresh_result- if isinstance(legacy_refresh_result, _BatchRefreshOutcome)- else _BatchRefreshOutcome(queue_status=None)- )- planned_actions = tuple(- FreshnessAction(action)- for action in parent_result["parent_actions"].values()- )- refresh_outcome = _BatchRefreshOutcome(- queue_status=legacy_outcome.queue_status,- semantic_actions=planned_actions + legacy_outcome.semantic_actions,- memory_refreshed=legacy_outcome.memory_refreshed,- embedding_requested=(- direct_index_requested or legacy_outcome.embedding_requested- ),+ if write_error is not None and not has_refresh_work:+ await lock_guard.close()+ lock_settled = True+ raise write_error+ if has_refresh_work:+ result["semantic_status"] = "queued"+ result["vector_status"] = "queued"+ lock_handoff = await lock_guard.to_handoff()+ legacy_refresh_uris = set(refresh_kinds)+ refresh = {+ "kind": "batch",+ "force_refresh": wait,+ "refresh_kinds": dict(refresh_kinds),+ "file_md5s": {+ uri: value+ for uri, value in file_md5s.items()+ if uri in legacy_refresh_uris+ },+ "file_abstracts": {+ uri: value+ for uri, value in file_abstracts.items()+ if uri in legacy_refresh_uris+ },+ "sidecar_directories": {+ uri: options.to_dict()+ for uri, options in sorted(sidecar_directories.items())+ },+ "file_ingest_options": {+ operation["uri"]: operation["ingest_options"].to_dict()+ for operation, _, _ in pending+ if operation["uri"] in legacy_refresh_uris+ },+ "resource_plans": [+ {+ "plan": prepared.plan.after_content_commit().to_dict(),+ "existed": prepared.existed,+ "previous_abstract": prepared.previous_abstract,+ "ingest_options": prepared.ingest_options.to_dict(),+ }+ for prepared in committed_resource_plans+ ],+ }+ else:+ lock_handoff = None+ refresh = {"kind": "none"}+ await root_task_factory()+ root_dispatched = False++ async def dispatch() -> str:+ nonlocal root_dispatched+ await self._enqueue_content_write_root(+ operation=root_operation,+ resource_id=normalized_root,+ result=result,+ refresh=refresh,+ ctx=ctx,+ telemetry_id=telemetry_id,+ lock_handoff=lock_handoff,+ failure=_failure_payload(write_error) if write_error is not None else None,+ root_task_factory=root_task_factory,)- except Exception as exc:- if write_error is not None:- logger.error(- "Batch refresh failed while preserving an earlier write error",- exc_info=True,- )+ root_dispatched = True+ return "enqueued"++ try:+ if lock_handoff is not None:+ await lock_guard.handoff_and_dispatch(lock_handoff, dispatch)else:- cause = str(exc).strip() or type(exc).__name__- raise OpenVikingError(- "Content is already at the requested state, but semantic/index "- f"refresh failed: {cause}. Re-run with the final body using "- "replace/create/upsert or use ov compile to retry the refresh; "- "do not blindly retry an already-successful append.",- code="REFRESH_FAILED",- details={- "root_uri": normalized_root,- "created": created,- "updated": updated,- "unchanged": unchanged,- "cause": cause,- },- ) from exc+ await lock_guard.dispatch_and_close(dispatch)+ except BaseException as exc:+ if root_dispatched:+ _mark_root_owns_failure(exc)+ raise+ lock_settled = True+ if write_error is not None:+ _mark_root_owns_failure(write_error)+ raise write_error+ return resultfinally:- await run_to_completion(- lambda: self._finish_write_lock(lease, coordinator_work_id, ctx)- )+ if not lock_settled:+ await lock_guard.close()- queue_status = (- await self._wait_for_request(telemetry_id=telemetry_id)- if coordinator_work_id is not None and wait and wait_for_queues- else refresh_outcome.queue_status- if refresh_outcome is not None- else None- )- if wait and wait_for_queues and queue_status is not None and write_error is None:- self._raise_refresh_errors(queue_status)- if refresh_outcome is not None and queue_status is not None:- refresh_outcome = replace(refresh_outcome, queue_status=queue_status)-- if write_error is not None:- raise write_error- result = {- "root_uri": normalized_root,- "created": created,- "updated": updated,- "unchanged": unchanged,- "queue_status": queue_status,- }- if skip_conflicts:- result["conflicts"] = conflicts- if refresh_outcome is not None:- semantic_status, vector_status = refresh_outcome.statuses(wait=wait and wait_for_queues)- result["semantic_status"] = semantic_status- result["vector_status"] = vector_status- return result+ raise RuntimeError("Content batch write root Task was not created")def _validate_uri_path(self, uri: str, *, field_name: str) -> str:try:@@ -955,6 +1116,102 @@ class ContentWriteCoordinator:embedding_requested=embedding_requested,)+ async def _execute_batch_refresh_payload(+ self,+ payload: dict[str, Any],+ *,+ ctx: RequestContext,+ ) -> _BatchRefreshOutcome:+ force_refresh = bool(payload.get("force_refresh", False))+ for directory_uri, options in sorted(+ (payload.get("sidecar_directories") or {}).items()+ ):+ await self._vectorize_semantic_directory(+ directory_uri=str(directory_uri),+ ctx=ctx,+ ingest_options=IngestOptions.from_value(options),+ )++ direct_index_requested = False+ planned_refreshes: list[FileRefreshRequest] = []+ for item in payload.get("resource_plans") or ():+ if not isinstance(item, dict):+ continue+ plan = ContextUpdatePlan.from_dict(item["plan"])+ ingest_options = IngestOptions.from_value(item.get("ingest_options"))+ direct_index_requested = (+ await enqueue_direct_index_actions(+ plan.direct_index_actions,+ ctx=ctx,+ acl_update=ingest_options.acl_update,+ )+ or direct_index_requested+ )+ if plan.file_refresh is not None:+ existed = bool(item.get("existed", False))+ planned_refreshes.append(+ FileRefreshRequest(+ file_uri=plan.file_refresh.file_uri,+ created=not existed,+ md5=plan.file_refresh.md5,+ abstract=str(item.get("previous_abstract") or ""),+ vector_action="upsert" if not existed else "",+ ingest_options=ingest_options,+ )+ )++ parent_result = (+ await Summarizer(vlm_processor=None).refresh_file_parents(+ changes=planned_refreshes,+ ctx=ctx,+ generation_trigger="content_write",+ force_refresh=force_refresh,+ )+ if planned_refreshes+ else {"enqueued_count": 0, "parent_actions": {}}+ )+ file_ingest_options = {+ str(uri): IngestOptions.from_value(options)+ for uri, options in (payload.get("file_ingest_options") or {}).items()+ }+ refresh_kinds = {+ str(uri): str(change_type)+ for uri, change_type in (payload.get("refresh_kinds") or {}).items()+ }+ legacy_refresh_result = (+ await self._refresh_batch(+ refresh_kinds=refresh_kinds,+ ctx=ctx,+ wait=False,+ telemetry_id="",+ file_md5s={+ str(uri): str(value)+ for uri, value in (payload.get("file_md5s") or {}).items()+ },+ file_abstracts={+ str(uri): str(value)+ for uri, value in (payload.get("file_abstracts") or {}).items()+ },+ file_ingest_options=file_ingest_options,+ )+ if refresh_kinds+ else None+ )+ legacy_outcome = (+ legacy_refresh_result+ if isinstance(legacy_refresh_result, _BatchRefreshOutcome)+ else _BatchRefreshOutcome(queue_status=None)+ )+ planned_actions = tuple(+ FreshnessAction(action) for action in parent_result["parent_actions"].values()+ )+ return _BatchRefreshOutcome(+ queue_status=legacy_outcome.queue_status,+ semantic_actions=planned_actions + legacy_outcome.semantic_actions,+ memory_refreshed=legacy_outcome.memory_refreshed,+ embedding_requested=direct_index_requested or legacy_outcome.embedding_requested,+ )+async def _enqueue_semantic_refresh_changes(self,*,@@ -1030,68 +1287,6 @@ class ContentWriteCoordinator:await semantic_queue.enqueue(msg)return action- async def _enqueue_lock_coordinator(- self,- *,- root_uri: str,- context_type: str,- ctx: RequestContext,- telemetry_id: str,- lease: Any,- ) -> str:- """Enqueue the sole lease owner before scheduling request work."""- context = get_task_context()- if context is None:- raise RuntimeError("TaskContext is required for lock-coordinated content writes")- queue_manager = get_queue_manager()- semantic_queue = queue_manager.get_queue(queue_manager.SEMANTIC, allow_create=True)- msg = SemanticMsg(- uri=root_uri,- context_type=context_type,- recursive=False,- account_id=ctx.account_id,- user_id=ctx.user.user_id,- group_ids=ctx.group_ids,- role=str(ctx.role),- skip_vectorization=True,- telemetry_id=telemetry_id,- lock_handoff=await self._viking_fs._async_agfs.pathlock_to_handoff(lease),- generation_trigger="content_write",- aggregate_directory=False,- propagate_to_parent=False,- wait_for_request_completion=True,- )- msg.coalesce_key = f"lock-coordinator|{msg.id}"- receipt = await semantic_queue.enqueue(msg)- if not receipt.work_id:- raise RuntimeError("Lock coordinator was enqueued without Task Work identity")- return receipt.work_id-- async def _finish_write_lock(- self,- lease: Any,- coordinator_work_id: str | None,- ctx: RequestContext,- ) -> None:- """Publish completion only after the producer has handed off its lease."""- agfs = self._viking_fs._async_agfs- if coordinator_work_id is None:- await agfs.pathlock_release(lease)- return- try:- await agfs.pathlock_handoff(lease)- except BaseException:- await agfs.pathlock_release(lease)- raise- context = get_task_context()- assert context is not None- await get_task_tracker().mark_enqueue_complete(- context.task_id,- coordinator_work_id,- account_id=ctx.account_id,- user_id=ctx.user.user_id,- )-@staticmethoddef _raise_refresh_errors(queue_status: Dict[str, Any]) -> None:for name in ("Semantic", "Embedding"):@@ -1565,7 +1760,8 @@ class ContentWriteCoordinator:telemetry_id: str,processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,ingest_options: IngestOptions | None = None,- wait_for_queues: bool = True,+ root_operation: str = "content_write",+ root_task_factory: Callable[[], Awaitable[Any]] | None = None,) -> Dict[str, Any]:# ``.abstract.md`` / ``.overview.md`` are generated sidecars whose body is# re-embedded in place (no LLM regeneration and no RNFV plan). Keep that@@ -1578,16 +1774,17 @@ class ContentWriteCoordinator:mode=mode,response_mode=response_mode,context_type=context_type,- wait=wait,ctx=ctx,telemetry_id=telemetry_id,ingest_options=ingest_options,- wait_for_queues=wait_for_queues,+ root_operation=root_operation,+ root_task_factory=root_task_factory,)lock_path = self._viking_fs._uri_to_path(uri, ctx=ctx)+ acquire_lock = self._viking_fs._async_agfs.pathlock_acquire_exacttry:- lease = await self._viking_fs._async_agfs.pathlock_acquire_exact(lock_path)+ lease = await acquire_lock(lock_path)except LockAcquisitionError as exc:raise ResourceBusyError(f"resource is busy and cannot be written now: {uri}",@@ -1595,7 +1792,8 @@ class ContentWriteCoordinator:) from excprevious_content: Optional[str] = None- coordinator_work_id: str | None = None+ lock_settled = False+ lock_guard = PathLockGuard(self._viking_fs._async_agfs, lease)try:target_state = await self._classify_locked_write_target(uri, ctx=ctx)target_preexisting = target_state.preexisting@@ -1632,59 +1830,92 @@ class ContentWriteCoordinator:has_async_work = bool(prepared.plan.direct_index_actions or prepared.plan.file_refresh is not None)- if has_async_work:- coordinator_work_id = await self._enqueue_lock_coordinator(- root_uri=uri,+ if root_task_factory is not None:+ await execute_content_tree_actions(+ prepared.plan.content_tree_actions,+ store=prepared.store,+ artifact_ref=prepared.artifact_ref,+ target=prepared.target,+ )+ if ingest_options.acl_update:+ await self._viking_fs.acl_manager.apply_indexed_update(+ ingest_options.acl_update, ctx+ )++ written_bytes = len(final_bytes)+ semantic_status = "queued" if prepared.plan.file_refresh is not None else "skipped"+ vector_status = (+ "queued"+ if prepared.plan.direct_index_actions or prepared.plan.file_refresh is not None+ else "skipped"+ )+ result = self._build_write_result(+ uri=uri,+ root_uri=root_uri,context_type=context_type,- ctx=ctx,- telemetry_id=telemetry_id,- lease=lease,+ mode=response_mode,+ written_bytes=written_bytes,+ queue_status=None,+ semantic_status=semantic_status,+ vector_status=vector_status,)- work = await commit_and_enqueue_plan(- prepared.plan,- ctx=ctx,- inline_store=prepared.store,- inline_ref=prepared.artifact_ref,- target=prepared.target,- ingest_options=ingest_options,- file_created=not target_preexisting,- # Synchronous writes force parent aggregation; asynchronous writes- # defer it through the freshness gate (parity with the previous- # ``force_refresh=wait`` behavior).- force_refresh=wait,- generation_trigger="content_write",- )- if ingest_options.acl_update:- await self._viking_fs.acl_manager.apply_indexed_update(- ingest_options.acl_update, ctx+ refresh = (+ {+ "kind": "plan",+ "plan": prepared.plan.after_content_commit().to_dict(),+ "file_created": not target_preexisting,+ "force_refresh": wait,+ "ingest_options": ingest_options.to_dict()+ if ingest_options is not None+ else IngestOptions().to_dict(),+ }+ if has_async_work+ else {"kind": "none"})+ # Skill semantic processing owns the package tree lock. Keep the+ # synchronous write lock scoped to this target file and release+ # it after durable root dispatch, so Semantic can acquire its+ # own package lock without inheriting a conflicting lease.+ lock_scope = "dispatch" if context_type == "skill" else "descendants"+ if has_async_work and lock_scope != "dispatch":+ lock_handoff = await lock_guard.to_handoff()+ else:+ lock_handoff = None+ await root_task_factory()+ root_dispatched = False++ async def dispatch() -> str:+ nonlocal root_dispatched+ await self._enqueue_content_write_root(+ operation=root_operation,+ resource_id=uri,+ result=result,+ refresh=refresh,+ ctx=ctx,+ telemetry_id=telemetry_id,+ lock_handoff=lock_handoff,+ lock_scope=lock_scope,+ root_task_factory=root_task_factory,+ )+ root_dispatched = True+ return "enqueued"+ try:+ if lock_handoff is not None:+ await lock_guard.handoff_and_dispatch(lock_handoff, dispatch)+ else:+ await lock_guard.dispatch_and_close(dispatch)+ except BaseException as exc:+ if root_dispatched:+ _mark_root_owns_failure(exc)+ raise+ lock_settled = True+ return resultfinally:- # Committed content is retained even if a later enqueue fails. Already- # queued work must still finish before its coordinator releases the lock.- await run_to_completion(- lambda: self._finish_write_lock(lease, coordinator_work_id, ctx)- )+ if not lock_settled:+ await lock_guard.close()- queue_status = (- await self._wait_for_request(telemetry_id=telemetry_id)- if wait and wait_for_queues- else None- )- written_bytes = len(final_bytes)- semantic_status, vector_status = self._plan_statuses(- work, wait=wait and wait_for_queues, queue_status=queue_status- )- return self._build_write_result(- uri=uri,- root_uri=root_uri,- context_type=context_type,- mode=response_mode,- written_bytes=written_bytes,- queue_status=queue_status,- semantic_status=semantic_status,- vector_status=vector_status,- )+ raise RuntimeError("Content write root Task was not created")@staticmethoddef _ensure_explicit_write_content_action(@@ -1757,23 +1988,28 @@ class ContentWriteCoordinator:mode: str,response_mode: str,context_type: str,- wait: bool,ctx: RequestContext,telemetry_id: str,ingest_options: IngestOptions | None = None,- wait_for_queues: bool = True,+ root_operation: str = "content_write",+ root_task_factory: Callable[[], Awaitable[Any]] | None = None,) -> Dict[str, Any]:"""Re-embed a manually edited L0/L1 sidecar body without LLM regeneration."""lock_path = self._viking_fs._uri_to_path(uri, ctx=ctx)+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)try:- lease = await self._viking_fs._async_agfs.pathlock_acquire_exact(lock_path)+ lease = await lock_guard.acquire(+ lambda: self._viking_fs._async_agfs.pathlock_acquire_exact(lock_path)+ )except LockAcquisitionError as exc:raise ResourceBusyError(f"resource is busy and cannot be written now: {uri}",uri=uri,) from exc+ except BaseException:+ await lock_guard.close()+ raise- lock_released = Falsetry:# Preserve the public invariant that generated sidecars cannot be# materialized, while keeping the state check inside the exact lock.@@ -1782,36 +2018,52 @@ class ContentWriteCoordinator:await self._write_in_place(uri, content, mode=mode, ctx=ctx, lease_ref=lease, existing_raw=previous_content)- vector_enqueued = await self._vectorize_abstract_overview(- uri=uri, ctx=ctx, ingest_options=ingest_options- )- await self._viking_fs._async_agfs.pathlock_release(lease)- lock_released = True- queue_status = (- await self._wait_for_request(telemetry_id=telemetry_id)- if wait and wait_for_queues- else None- )- vector_status = self._queue_work_status(- requested=vector_enqueued,- wait=wait and wait_for_queues,- queue_status=queue_status,- queue_name="Embedding",- )- return self._build_write_result(+ result = self._build_write_result(uri=uri,root_uri=root_uri,context_type=context_type,mode=response_mode,written_bytes=len(content.encode("utf-8")),- queue_status=queue_status,+ queue_status=None,semantic_status="skipped",- vector_status=vector_status,+ vector_status="queued",)- except Exception:- if not lock_released:- await self._viking_fs._async_agfs.pathlock_release(lease)- raise+ if root_task_factory is not None:+ await root_task_factory()+ root_dispatched = False++ async def dispatch() -> str:+ nonlocal root_dispatched+ work_id = await self._enqueue_content_write_root(+ operation=root_operation,+ resource_id=uri,+ result=result,+ refresh={+ "kind": "abstract_overview",+ "uri": uri,+ "ingest_options": ingest_options.to_dict()+ if ingest_options is not None+ else IngestOptions().to_dict(),+ },+ ctx=ctx,+ telemetry_id=telemetry_id,+ lock_scope="dispatch",+ root_task_factory=root_task_factory,+ )+ root_dispatched = True+ return work_id++ try:+ await lock_guard.dispatch_and_close(dispatch)+ except BaseException as exc:+ if root_dispatched:+ _mark_root_owns_failure(exc)+ raise+ return result+ raise RuntimeError("Content write root Task was not created")+ finally:+ if lock_guard.lease is not None:+ await lock_guard.close()async def _vectorize_abstract_overview(self,@@ -2111,6 +2363,30 @@ class ContentWriteCoordinator:context.task_id, work_ids if context.work_id is not None else None)+ async def _refresh_memory_after_write(+ self,+ *,+ root_uri: str,+ uri: str,+ ctx: RequestContext,+ ingest_options: IngestOptions | None = None,+ ) -> tuple[bool, bool]:+ overview_refreshed = await MemoryUpdater.refresh_schema_overview(+ viking_fs=self._viking_fs,+ directory_uri=root_uri,+ ctx=ctx,+ )+ embedding_requested = await MemoryUpdater.refresh_file_embedding(+ viking_fs=self._viking_fs,+ vikingdb=self._vikingdb,+ uri=uri,+ memory_type=MemoryUpdater.memory_type_from_uri(root_uri),+ ctx=ctx,+ strict=True,+ ingest_options=ingest_options,+ )+ return overview_refreshed, embedding_requested+async def _write_memory_with_refresh(self,*,@@ -2119,22 +2395,27 @@ class ContentWriteCoordinator:content: str,mode: str,response_mode: str,- wait: bool,ctx: RequestContext,telemetry_id: str,ingest_options: IngestOptions | None = None,- wait_for_queues: bool = True,+ root_operation: str = "content_write",+ root_task_factory: Callable[[], Awaitable[Any]] | None = None,) -> Dict[str, Any]:lock_path = self._viking_fs._uri_to_path(uri, ctx=ctx)+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)try:- lease = await self._viking_fs._async_agfs.pathlock_acquire_exact(lock_path)+ lease = await lock_guard.acquire(+ lambda: self._viking_fs._async_agfs.pathlock_acquire_exact(lock_path)+ )except LockAcquisitionError as exc:raise ResourceBusyError(f"resource is busy and cannot be written now: {uri}",uri=uri,) from exc+ except BaseException:+ await lock_guard.close()+ raise- released = Falsetry:target_state = await self._classify_locked_write_target(uri, ctx=ctx)is_new_file = target_state.is_new_file@@ -2151,50 +2432,54 @@ class ContentWriteCoordinator:**write_kwargs,)written_bytes = len(final_bytes)- await self._viking_fs._async_agfs.pathlock_release(lease)- released = True- overview_refreshed = await MemoryUpdater.refresh_schema_overview(- viking_fs=self._viking_fs,- directory_uri=root_uri,- ctx=ctx,- )- embedding_requested = await MemoryUpdater.refresh_file_embedding(- viking_fs=self._viking_fs,- vikingdb=self._vikingdb,- uri=uri,- memory_type=MemoryUpdater.memory_type_from_uri(root_uri),- ctx=ctx,- strict=True,- ingest_options=ingest_options,- )- queue_status = None- if embedding_requested and wait and wait_for_queues:- queue_status = await self._wait_for_request(- telemetry_id=telemetry_id,+ if root_task_factory is not None:+ result = self._build_write_result(+ uri=uri,+ root_uri=root_uri,+ context_type="memory",+ mode=response_mode,+ written_bytes=written_bytes,+ queue_status=None,+ semantic_status="skipped",+ vector_status="queued",+ overview_status="queued",)- vector_status = self._queue_work_status(- requested=embedding_requested,- wait=wait and wait_for_queues,- queue_status=queue_status,- queue_name="Embedding",- )- return self._build_write_result(- uri=uri,- root_uri=root_uri,- context_type="memory",- mode=response_mode,- written_bytes=written_bytes,- queue_status=queue_status,- semantic_status="skipped",- vector_status=vector_status,- overview_status="complete" if overview_refreshed else "skipped",- )- except Exception:- if not released:- await self._viking_fs._async_agfs.pathlock_release(lease)- raise+ await root_task_factory()+ root_dispatched = False++ async def dispatch() -> str:+ nonlocal root_dispatched+ work_id = await self._enqueue_content_write_root(+ operation=root_operation,+ resource_id=uri,+ result=result,+ refresh={+ "kind": "memory",+ "root_uri": root_uri,+ "uri": uri,+ "ingest_options": ingest_options.to_dict()+ if ingest_options is not None+ else IngestOptions().to_dict(),+ },+ ctx=ctx,+ telemetry_id=telemetry_id,+ lock_scope="dispatch",+ root_task_factory=root_task_factory,+ )+ root_dispatched = True+ return work_id++ try:+ await lock_guard.dispatch_and_close(dispatch)+ except BaseException as exc:+ if root_dispatched:+ _mark_root_owns_failure(exc)+ raise+ return result+ raise RuntimeError("Content write root Task was not created")finally:- pass+ if lock_guard.lease is not None:+ await lock_guard.close()def _vikingdb_has_queue(self) -> bool:if not self._vikingdb:diff --git a/openviking/storage/context_update_execution.py b/openviking/storage/context_update_execution.pyindex 81bc7771e..5eb8a440f 100644--- a/openviking/storage/context_update_execution.py+++ b/openviking/storage/context_update_execution.py@@ -219,6 +219,7 @@ async def commit_and_enqueue_plan(plan: ContextUpdatePlan,*,ctx: RequestContext,+ semantic_lock_handoff: dict[str, Any] | None = None,inline_store: Any = None,inline_ref: Any = None,target: Any = None,@@ -270,6 +271,7 @@ async def commit_and_enqueue_plan(refresh_result = await summarizer.refresh_file_parent(file_uri=plan.file_refresh.file_uri,ctx=ctx,+ lock_handoff=semantic_lock_handoff,ingest_options=IngestOptions.from_value(ingest_options),created=file_created,file_md5=plan.file_refresh.md5,diff --git a/openviking/storage/pathlock_transfer.py b/openviking/storage/pathlock_transfer.pynew file mode 100644index 000000000..bb040c9ac--- /dev/null+++ b/openviking/storage/pathlock_transfer.py@@ -0,0 +1,146 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Cancellation-safe ownership transfer for pathlock leases."""++from __future__ import annotations++from typing import Any, Awaitable, Callable, TypeVar++from openviking.service.task_tracker_concurrency import run_to_completion++_ResultT = TypeVar("_ResultT")+++class PathLockGuard:+ """Track one owned lease across cancellation-safe handoff and release."""++ def __init__(self, agfs: Any, lease: Any = None) -> None:+ self._agfs = agfs+ self._lease = lease+ self._transferred = False++ @property+ def lease(self) -> Any:+ return self._lease++ @property+ def transferred(self) -> bool:+ return self._transferred++ def detach(self) -> Any:+ """Give the owned lease to another owner without awaiting."""+ lease = self._lease+ self._lease = None+ return lease++ async def acquire(self, factory: Callable[[], Awaitable[Any]]) -> Any:+ """Acquire and record a lease before cancellation can escape."""++ async def settle() -> Any:+ lease = await factory()+ self._lease = lease+ self._transferred = False+ return lease++ return await run_to_completion(settle)++ async def to_handoff(self) -> Any:+ if self._lease is None:+ raise RuntimeError("pathlock guard does not own a lease")+ return await run_to_completion(lambda: self._agfs.pathlock_to_handoff(self._lease))++ async def adopt(+ self,+ handoff: Any,+ *,+ fallback: Callable[[], Awaitable[Any]] | None = None,+ ) -> Any:+ """Adopt a handoff and record ownership before cancellation can escape."""++ async def settle() -> Any:+ try:+ lease = await self._agfs.pathlock_adopt(handoff)+ except Exception:+ if fallback is None:+ raise+ lease = await fallback()+ self._lease = lease+ self._transferred = False+ return lease++ return await run_to_completion(settle)++ async def handoff(self) -> None:+ """Transfer ownership when dispatch was performed by a compatibility caller."""+ if self._lease is None:+ raise RuntimeError("pathlock guard does not own a lease")++ async def settle() -> None:+ await self._agfs.pathlock_handoff(self._lease)+ self._lease = None+ self._transferred = True++ await run_to_completion(settle)++ async def handoff_and_dispatch(+ self,+ handoff: Any,+ dispatch: Callable[[], Awaitable[_ResultT]],+ *,+ accepted: Callable[[_ResultT], bool] | None = None,+ ) -> _ResultT:+ """Settle handoff, durable dispatch, and ownership state as one operation."""+ if self._lease is None:+ raise RuntimeError("pathlock guard does not own a lease")++ async def settle() -> _ResultT:+ lease = self._lease+ await self._agfs.pathlock_handoff(lease)+ self._lease = None++ async def reclaim() -> None:+ adopt = getattr(self._agfs, "pathlock_adopt", None)+ self._lease = await adopt(handoff) if callable(adopt) else lease++ try:+ result = await dispatch()+ is_accepted = accepted(result) if accepted is not None else True+ except BaseException:+ await reclaim()+ raise+ if is_accepted:+ self._transferred = True+ else:+ await reclaim()+ return result++ return await run_to_completion(settle)++ async def dispatch_and_close(+ self,+ dispatch: Callable[[], Awaitable[_ResultT]],+ ) -> _ResultT:+ """Durably dispatch before releasing a lease as one settlement."""+ if self._lease is None:+ raise RuntimeError("pathlock guard does not own a lease")++ async def settle() -> _ResultT:+ result = await dispatch()+ lease = self._lease+ await self._agfs.pathlock_release(lease)+ self._lease = None+ return result++ return await run_to_completion(settle)++ async def close(self) -> None:+ """Release the owned lease, if any, before propagating cancellation."""++ async def settle() -> None:+ if self._lease is None:+ return+ lease = self._lease+ await self._agfs.pathlock_release(lease)+ self._lease = None++ await run_to_completion(settle)diff --git a/openviking/storage/queuefs/__init__.py b/openviking/storage/queuefs/__init__.pyindex 4a34054c1..06cb3482b 100644--- a/openviking/storage/queuefs/__init__.py+++ b/openviking/storage/queuefs/__init__.py@@ -1,5 +1,7 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0+from .content_write_msg import ContentWriteMsg+from .content_write_processor import ContentWriteProcessorfrom .embedding_msg import EmbeddingMsgfrom .embedding_queue import EmbeddingQueuefrom .named_queue import NamedQueue, QueueError, QueueStatus@@ -29,4 +31,6 @@ __all__ = ["SemanticMsg","SemanticProcessor","SessionCommitMsg",+ "ContentWriteMsg",+ "ContentWriteProcessor",]diff --git a/openviking/storage/queuefs/add_resource_processor.py b/openviking/storage/queuefs/add_resource_processor.pyindex d2c004634..a54bde19c 100644--- a/openviking/storage/queuefs/add_resource_processor.py+++ b/openviking/storage/queuefs/add_resource_processor.py@@ -10,11 +10,13 @@ from typing import Any, Dict, Optionalfrom openviking.observability.context import bind_execution_contextfrom openviking.server.identity import RequestContext, Role+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_tracker import TaskStatus, get_task_trackerfrom openviking.service.task_work_index import (bind_task_context_from_message,extract_task_metadata,)+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs.add_resource_msg import AddResourceMsgfrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResult@@ -85,17 +87,19 @@ class AddResourceProcessor(DequeueHandlerBase):ctx: RequestContext,) -> None:if msg.lock_handoff is not None:+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)try:- lock = await self._viking_fs._async_agfs.pathlock_adopt(msg.lock_handoff)+ await lock_guard.adopt(msg.lock_handoff)if msg.cleanup_empty_target_on_failure:await self._resource_service._cleanup_reserved_target_if_empty(root_uri=msg.root_uri,ctx=ctx,- resource_lock=lock,+ resource_lock=lock_guard.lease,)- await self._viking_fs._async_agfs.pathlock_release(lock)except Exception as exc:logger.warning("[AddResource] Failed to release cancelled lock handoff: %s", exc)+ finally:+ await lock_guard.close()with suppress(Exception):await self._cleanup_staged_source(msg, ctx)with suppress(Exception):@@ -185,9 +189,13 @@ class AddResourceProcessor(DequeueHandlerBase):replay_result = getattr(task, "result", None)resource_lock = None+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)if replay_result is None:try:- resource_lock = await self._load_lock(msg, ctx)+ await lock_guard.acquire(lambda: self._load_lock(msg, ctx))+ except asyncio.CancelledError:+ await lock_guard.close()+ raiseexcept Exception as exc:if await self._requeue_lock_handoff(msg, exc):return ProcessResult.requeued()@@ -222,9 +230,9 @@ class AddResourceProcessor(DequeueHandlerBase):async def _set_stage(stage: str) -> None:nonlocal current_stagecurrent_stage = stage- await tracker.update_stage(+ await tracker.patch_task(msg.task_id,- stage,+ TaskPatch(stage=stage),account_id=ctx.account_id,user_id=ctx.user.user_id,)@@ -250,16 +258,18 @@ class AddResourceProcessor(DequeueHandlerBase):user_id=ctx.user.user_id,stage="queued",)+ task_auth = await tracker.get_task_auth(+ msg.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ resource_lock = lock_guard.detach()result = await self._resource_service.execute_add_resource_job(msg,ctx=ctx,resource_lock=resource_lock,stage_callback=_set_stage,- task_auth=await tracker.get_task_auth(- msg.task_id,- account_id=ctx.account_id,- user_id=ctx.user.user_id,- ),+ task_auth=task_auth,)if result.get("status") == "error":errors = result.get("errors") or ["resource processing failed"]@@ -416,9 +426,14 @@ class AddResourceProcessor(DequeueHandlerBase):return ProcessResult.failed(str(exc))finally:unregister_telemetry(telemetry_id)+ await lock_guard.close()with suppress(Exception):if resource_lock is not None:- await self._viking_fs._async_agfs.pathlock_release(resource_lock)+ cleanup_guard = PathLockGuard(+ self._viking_fs._async_agfs,+ resource_lock,+ )+ await cleanup_guard.close()if terminal:with suppress(Exception):await self._cleanup_staged_source(msg, ctx)diff --git a/openviking/storage/queuefs/content_write_msg.py b/openviking/storage/queuefs/content_write_msg.pynew file mode 100644index 000000000..03c42c398--- /dev/null+++ b/openviking/storage/queuefs/content_write_msg.py@@ -0,0 +1,77 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Durable root request for content-write task finalization."""++from __future__ import annotations++from dataclasses import asdict, dataclass, field+from typing import Any, Dict, Optional+++@dataclass(kw_only=True)+class ContentWriteMsg:+ task_id: str+ operation: str+ resource_id: str+ result: Dict[str, Any]+ refresh: Dict[str, Any]+ account_id: str+ user_id: str+ role: str+ _task_work_id: str = ""+ group_ids: list[str] = field(default_factory=list)+ actor_peer_id: Optional[str] = None+ bypass_acl: bool = False+ telemetry_id: Optional[str] = None+ lock_handoff: Optional[Dict[str, Any]] = None+ lock_scope: str = "descendants"+ lock_handoff_retry: int = 0+ failure: Optional[Dict[str, Any]] = None++ def to_dict(self) -> Dict[str, Any]:+ return asdict(self)++ @classmethod+ def from_dict(cls, data: Dict[str, Any]) -> "ContentWriteMsg":+ required = ("task_id", "operation", "resource_id", "account_id", "user_id")+ missing = [name for name in required if not data.get(name)]+ result = data.get("result")+ refresh = data.get("refresh")+ if not isinstance(result, dict):+ missing.append("result")+ if not isinstance(refresh, dict):+ missing.append("refresh")+ if missing:+ raise ValueError(f"Missing required content-write fields: {missing}")+ try:+ lock_handoff_retry = max(0, int(data.get("lock_handoff_retry", 0) or 0))+ except (TypeError, ValueError):+ lock_handoff_retry = 0+ lock_scope = str(data.get("lock_scope") or "descendants")+ if lock_scope not in {"dispatch", "descendants", "semantic_handoff"}:+ lock_scope = "descendants"+ return cls(+ task_id=str(data["task_id"]),+ _task_work_id=str(data.get("_task_work_id") or ""),+ operation=str(data["operation"]),+ resource_id=str(data["resource_id"]),+ result=dict(result),+ refresh=dict(refresh),+ account_id=str(data["account_id"]),+ user_id=str(data["user_id"]),+ role=str(data.get("role") or "root"),+ group_ids=[str(value) for value in data.get("group_ids", [])],+ actor_peer_id=data.get("actor_peer_id")+ if isinstance(data.get("actor_peer_id"), str)+ else None,+ bypass_acl=bool(data.get("bypass_acl", False)),+ telemetry_id=data.get("telemetry_id")+ if isinstance(data.get("telemetry_id"), str)+ else None,+ lock_handoff=data.get("lock_handoff")+ if isinstance(data.get("lock_handoff"), dict)+ else None,+ lock_scope=lock_scope,+ lock_handoff_retry=lock_handoff_retry,+ failure=data.get("failure") if isinstance(data.get("failure"), dict) else None,+ )diff --git a/openviking/storage/queuefs/content_write_processor.py b/openviking/storage/queuefs/content_write_processor.pynew file mode 100644index 000000000..ea226069c--- /dev/null+++ b/openviking/storage/queuefs/content_write_processor.py@@ -0,0 +1,423 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Queue consumer that owns durable content-write task finalization."""++from __future__ import annotations++import asyncio+import json+from copy import deepcopy+from typing import Any, Dict, Optional++from openviking.server.identity import RequestContext, Role+from openviking.service.task_domain import TaskPatch, TaskStatus+from openviking.service.task_tracker import get_task_tracker+from openviking.service.task_work_index import (+ bind_task_context_from_message,+ extract_task_metadata,+)+from openviking.storage.context_update_execution import commit_and_enqueue_plan+from openviking.storage.context_update_plan import ContextUpdatePlan+from openviking.storage.pathlock_transfer import PathLockGuard+from openviking.storage.queuefs.content_write_msg import ContentWriteMsg+from openviking.storage.queuefs.named_queue import DequeueHandlerBase+from openviking.storage.queuefs.process_result import ProcessResult+from openviking.telemetry import bind_telemetry, resolve_telemetry, unregister_telemetry+from openviking.utils.ingest_options import IngestOptions+from openviking_cli.exceptions import OpenVikingError+from openviking_cli.session.user_id import UserIdentifier+from openviking_cli.utils import get_logger++logger = get_logger(__name__)+++class ContentWriteProcessor(DequeueHandlerBase):+ def __init__(self, viking_fs: Any, vikingdb: Any = None) -> None:+ self._viking_fs = viking_fs+ self._vikingdb = vikingdb++ @staticmethod+ def _ctx(msg: ContentWriteMsg) -> RequestContext:+ return RequestContext(+ user=UserIdentifier(account_id=msg.account_id, user_id=msg.user_id),+ role=Role(msg.role),+ group_ids=tuple(msg.group_ids),+ actor_peer_id=msg.actor_peer_id,+ bypass_acl=msg.bypass_acl,+ )++ def _uris_for_reacquire(self, msg: ContentWriteMsg) -> list[str]:+ refresh = msg.refresh+ if str(refresh.get("kind") or "") != "batch":+ uri = str(refresh.get("uri") or msg.resource_id)+ return [uri] if uri else []++ uris: set[str] = set()+ result = msg.result if isinstance(msg.result, dict) else {}+ for field in ("created", "updated", "unchanged"):+ values = result.get(field)+ if isinstance(values, list):+ uris.update(str(value) for value in values if value)+ for field in ("refresh_kinds", "file_ingest_options"):+ values = refresh.get(field)+ if isinstance(values, dict):+ uris.update(str(uri) for uri in values if uri)+ for item in refresh.get("resource_plans") or ():+ if not isinstance(item, dict):+ continue+ plan = item.get("plan")+ if isinstance(plan, dict) and plan.get("root_uri"):+ uris.add(str(plan["root_uri"]))+ return sorted(uris)++ async def _reacquire_lock(self, msg: ContentWriteMsg, ctx: RequestContext) -> Dict[str, Any]:+ covered_paths = (+ msg.lock_handoff.get("covered_paths")+ if isinstance(msg.lock_handoff, dict)+ else None+ )+ if isinstance(covered_paths, list) and covered_paths:+ return await self._viking_fs._async_agfs.pathlock_acquire_batch(covered_paths)++ uris = self._uris_for_reacquire(msg)+ if not uris:+ raise ValueError("content-write lock reacquire has no resource URI")+ paths = [self._viking_fs._uri_to_path(uri, ctx=ctx) for uri in uris]+ if len(paths) == 1:+ return await self._viking_fs._async_agfs.pathlock_acquire_exact(paths[0])+ return await self._viking_fs._async_agfs.pathlock_acquire_exact_batch(paths)++ async def _load_lock(self, msg: ContentWriteMsg, ctx: RequestContext) -> Dict[str, Any] | None:+ if msg.lock_handoff is None:+ return None+ try:+ return await self._viking_fs._async_agfs.pathlock_adopt(msg.lock_handoff)+ except Exception as handoff_error:+ try:+ return await self._reacquire_lock(msg, ctx)+ except Exception:+ raise handoff_error++ async def _requeue_lock_handoff(self, msg: ContentWriteMsg, exc: Exception) -> bool:+ if msg.lock_handoff_retry >= 2:+ return False++ from openviking.storage.queuefs import get_queue_manager++ payload = msg.to_dict()+ payload["lock_handoff_retry"] = msg.lock_handoff_retry + 1+ queue_manager = get_queue_manager()+ await queue_manager.enqueue(queue_manager.CONTENT_WRITE, payload)+ logger.warning(+ "[ContentWrite] Requeued task %s after lock handoff failure: %s",+ msg.task_id,+ exc,+ )+ return True++ async def _release_orphaned_lock_handoff(self, msg: ContentWriteMsg) -> None:+ if msg.lock_handoff is None:+ return++ lock_guard = PathLockGuard(self._viking_fs._async_agfs)+ try:+ await lock_guard.adopt(msg.lock_handoff)+ except Exception as exc:+ logger.warning("[ContentWrite] Failed to release orphaned lock handoff: %s", exc)+ finally:+ await lock_guard.close()++ async def on_cancelled(self, data: Optional[Dict[str, Any]]) -> ProcessResult:+ """Release a queued content-write lock before cancelled work is ACKed."""+ try:+ payload = data.get("data", data) if isinstance(data, dict) else data+ if isinstance(payload, str):+ payload = json.loads(payload)+ msg = ContentWriteMsg.from_dict(payload)+ except (json.JSONDecodeError, TypeError, ValueError) as exc:+ return ProcessResult.failed(str(exc))++ await self._release_orphaned_lock_handoff(msg)+ unregister_telemetry(msg.telemetry_id or "")+ return ProcessResult.cancelled()++ async def on_dequeue(self, data: Optional[Dict[str, Any]]) -> ProcessResult:+ if not data:+ return ProcessResult.success()+ with bind_task_context_from_message(data):+ return await self._on_dequeue_bound(data)++ async def _on_dequeue_bound(self, data: Dict[str, Any]) -> ProcessResult:+ payload = data.get("data", data)+ if isinstance(payload, str):+ payload = json.loads(payload)+ msg = ContentWriteMsg.from_dict(payload)+ ctx = self._ctx(msg)+ tracker = get_task_tracker()+ task = await tracker.get(+ msg.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ if task is not None and task.status in {+ TaskStatus.COMPLETED,+ TaskStatus.FAILED,+ TaskStatus.CANCELLED,+ }:+ await self._release_orphaned_lock_handoff(msg)+ unregister_telemetry(msg.telemetry_id or "")+ return ProcessResult.success()+ await tracker.start(+ msg.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )++ metadata = extract_task_metadata(data)+ dispatch_complete = (+ metadata is not None+ and await tracker.is_enqueue_complete(+ msg.task_id,+ metadata.work_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ )+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)+ if not dispatch_complete or (+ msg.lock_scope == "descendants" and msg.lock_handoff is not None+ ):+ try:+ await lock_guard.acquire(lambda: self._load_lock(msg, ctx))+ except asyncio.CancelledError:+ await lock_guard.close()+ raise+ except Exception as exc:+ if await self._requeue_lock_handoff(msg, exc):+ return ProcessResult.requeued()+ await tracker.fail(+ msg.task_id,+ f"Invalid lock_handoff: {exc}",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ return ProcessResult.failed(f"Invalid lock_handoff: {exc}")++ telemetry_id = msg.telemetry_id or ""+ telemetry = resolve_telemetry(telemetry_id) if telemetry_id else None+ semantic_lock_handoff = None+ try:+ result_updates: Dict[str, Any] = {}+ checkpoint_result = (+ deepcopy(task.result) if dispatch_complete and isinstance(task.result, dict) else None+ )+ if not dispatch_complete:+ if lock_guard.lease is not None and msg.lock_scope == "semantic_handoff":+ semantic_lock_handoff = await lock_guard.to_handoff()++ async def execute_refresh() -> tuple[Dict[str, Any], bool]:+ if semantic_lock_handoff is None:+ return await self._execute_refresh(msg, ctx)+ return await self._execute_refresh(+ msg,+ ctx,+ semantic_lock_handoff=semantic_lock_handoff,+ )++ async def dispatch_refresh() -> tuple[Dict[str, Any], bool]:+ if telemetry is None:+ return await execute_refresh()+ with bind_telemetry(telemetry):+ return await execute_refresh()++ if semantic_lock_handoff is None:+ refresh_result = await dispatch_refresh()+ else:+ refresh_result = await lock_guard.handoff_and_dispatch(+ semantic_lock_handoff,+ dispatch_refresh,+ accepted=lambda value: (+ isinstance(value, tuple)+ and len(value) == 2+ and bool(value[1])+ ),+ )+ if (+ isinstance(refresh_result, tuple)+ and len(refresh_result) == 2+ and isinstance(refresh_result[0], dict)+ ):+ result_updates, _ = refresh_result+ else:+ result_updates = refresh_result+ checkpoint_result = deepcopy(msg.result)+ checkpoint_result.update(result_updates)+ if lock_guard.lease is not None and msg.lock_scope == "dispatch":+ await lock_guard.close()+ checkpoint = await tracker.patch_task(+ msg.task_id,+ TaskPatch(+ result=checkpoint_result,+ add_enqueue_complete_work_ids=(+ frozenset({metadata.work_id}) if metadata is not None else frozenset()+ ),+ ),+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ if not checkpoint.accepted:+ raise RuntimeError(+ f"Content write dispatch checkpoint rejected: {checkpoint.message or checkpoint.code.value}"+ )+ else:+ logger.info(+ "[ContentWrite] Resuming after completed dispatch task_id=%s work_id=%s",+ msg.task_id,+ metadata.work_id if metadata is not None else "",+ )+ await tracker.wait_for_task_work(+ msg.task_id,+ exclude_work_id=metadata.work_id if metadata is not None else None,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ queue_status = await tracker.build_queue_status(+ msg.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ result = deepcopy(checkpoint_result or msg.result)+ result.update(result_updates)+ result["queue_status"] = queue_status+ result.setdefault("task_id", msg.task_id)+ if msg.failure is not None:+ result["failure"] = deepcopy(msg.failure)+ await tracker.fail(+ msg.task_id,+ str(msg.failure.get("message") or "Content write failed"),+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ result=result,+ )+ logger.info(+ "[ContentWrite] Failed task after refresh task_id=%s operation=%s "+ "resource_id=%s",+ msg.task_id,+ msg.operation,+ msg.resource_id,+ )+ return ProcessResult.success()+ await tracker.complete(+ msg.task_id,+ result,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ resource_id=msg.resource_id,+ )+ logger.info(+ "[ContentWrite] Completed task result task_id=%s operation=%s resource_id=%s",+ msg.task_id,+ msg.operation,+ msg.resource_id,+ )+ return ProcessResult.success()+ except Exception as exc:+ await tracker.fail(+ msg.task_id,+ str(exc),+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ result={"code": exc.code, "details": exc.details}+ if isinstance(exc, OpenVikingError)+ else None,+ )+ if msg.lock_scope == "descendants" and lock_guard.lease is not None:+ await tracker.wait_for_task_work(+ msg.task_id,+ exclude_work_id=metadata.work_id if metadata is not None else None,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ return ProcessResult.failed(str(exc))+ finally:+ await lock_guard.close()+ unregister_telemetry(telemetry_id)++ async def _execute_refresh(+ self,+ msg: ContentWriteMsg,+ ctx: RequestContext,+ *,+ semantic_lock_handoff: Dict[str, Any] | None = None,+ ) -> tuple[Dict[str, Any], bool]:+ refresh = msg.refresh+ kind = str(refresh.get("kind") or "none")+ if kind == "none":+ return {}, False++ from openviking.storage.content_write import ContentWriteCoordinator++ coordinator = ContentWriteCoordinator(self._viking_fs, self._vikingdb)+ if kind == "plan":+ plan = ContextUpdatePlan.from_dict(refresh["plan"])+ work = await commit_and_enqueue_plan(+ plan,+ ctx=ctx,+ semantic_lock_handoff=semantic_lock_handoff,+ ingest_options=IngestOptions.from_value(refresh.get("ingest_options")),+ file_created=bool(refresh.get("file_created", False)),+ force_refresh=bool(refresh.get("force_refresh", False)),+ generation_trigger="content_write",+ )+ semantic_status, vector_status = ContentWriteCoordinator._plan_statuses(+ work,+ wait=False,+ queue_status=None,+ )+ return (+ {"semantic_status": semantic_status, "vector_status": vector_status},+ semantic_lock_handoff is not None and work.semantic_requested,+ )+ if kind == "abstract_overview":+ vector_enqueued = await coordinator._vectorize_abstract_overview(+ uri=str(refresh["uri"]),+ ctx=ctx,+ ingest_options=IngestOptions.from_value(refresh.get("ingest_options")),+ )+ return (+ {+ "vector_status": coordinator._queue_work_status(+ requested=vector_enqueued,+ wait=False,+ queue_status=None,+ queue_name="Embedding",+ )+ },+ False,+ )+ if kind == "memory":+ root_uri = str(refresh["root_uri"])+ uri = str(refresh["uri"])+ overview_refreshed, embedding_requested = await coordinator._refresh_memory_after_write(+ root_uri=root_uri,+ uri=uri,+ ctx=ctx,+ ingest_options=IngestOptions.from_value(refresh.get("ingest_options")),+ )+ return (+ {+ "overview_status": "complete" if overview_refreshed else "skipped",+ "vector_status": coordinator._queue_work_status(+ requested=embedding_requested,+ wait=False,+ queue_status=None,+ queue_name="Embedding",+ ),+ },+ False,+ )+ if kind == "batch":+ outcome = await coordinator._execute_batch_refresh_payload(refresh, ctx=ctx)+ semantic_status, vector_status = outcome.statuses(wait=False)+ return {"semantic_status": semantic_status, "vector_status": vector_status}, False+ raise ValueError(f"Unsupported content-write refresh kind: {kind}")diff --git a/openviking/storage/queuefs/queue_manager.py b/openviking/storage/queuefs/queue_manager.pyindex 0785df7e7..3d18d6874 100644--- a/openviking/storage/queuefs/queue_manager.py+++ b/openviking/storage/queuefs/queue_manager.py@@ -95,6 +95,7 @@ class QueueManager:EXTERNAL_PARSE = "ExternalParse"ADD_RESOURCE = "AddResource"REINDEX = "Reindex"+ CONTENT_WRITE = "ContentWrite"SESSION_COMMIT = "SessionCommit"EXTERNAL_TASK = "ExternalTask"# Account and user cleanup share one consumer. Retain the persisted namediff --git a/openviking/storage/queuefs/reindex_processor.py b/openviking/storage/queuefs/reindex_processor.pyindex 4632ef224..8ea397928 100644--- a/openviking/storage/queuefs/reindex_processor.py+++ b/openviking/storage/queuefs/reindex_processor.py@@ -4,12 +4,14 @@from __future__ import annotations+import asyncioimport jsonfrom typing import Any, Dict, Optionalfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.task_tracker import TaskStatus, get_task_trackerfrom openviking.service.task_work_index import bind_task_context_from_message, extract_task_metadata+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResultfrom openviking.storage.queuefs.reindex_msg import ReindexMsg@@ -98,9 +100,12 @@ class ReindexProcessor(DequeueHandlerBase):}:unregister_telemetry(msg.telemetry_id or "")return ProcessResult.success()- lease = None+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)try:- lease = await self._adopt_or_reacquire(msg, ctx)+ await lock_guard.acquire(lambda: self._adopt_or_reacquire(msg, ctx))+ except asyncio.CancelledError:+ await lock_guard.close()+ raiseexcept Exception as exc:if await self._requeue_lock_handoff(msg, exc):return ProcessResult.requeued()@@ -138,8 +143,7 @@ class ReindexProcessor(DequeueHandlerBase):# _run owns an existing lease and releases or hands it off on# every exit path. Clear this consumer's reference before the# call so an exception cannot release the same lease twice.- executor_lease = lease- lease = None+ executor_lease = lock_guard.detach()result = await executor._run(uri=msg.uri,object_type=msg.object_type,@@ -173,8 +177,7 @@ class ReindexProcessor(DequeueHandlerBase):)return ProcessResult.failed(str(exc))finally:- if lease is not None:- await self._viking_fs._async_agfs.pathlock_release(lease)+ await lock_guard.close()unregister_telemetry(telemetry_id)async def on_cancelled(self, data: Optional[Dict[str, Any]]) -> ProcessResult:@@ -188,9 +191,11 @@ class ReindexProcessor(DequeueHandlerBase):return ProcessResult.failed(str(exc))if msg.lock_handoff is not None:+ lock_guard = PathLockGuard(self._viking_fs._async_agfs)try:- lock = await self._viking_fs._async_agfs.pathlock_adopt(msg.lock_handoff)- await self._viking_fs._async_agfs.pathlock_release(lock)+ await lock_guard.adopt(msg.lock_handoff)except Exception as exc:logger.warning("[Reindex] Failed to release cancelled lock handoff: %s", exc)+ finally:+ await lock_guard.close()return ProcessResult.cancelled()diff --git a/openviking/storage/queuefs/semantic_lock.py b/openviking/storage/queuefs/semantic_lock.pyindex 4f1cfa8e9..d4b0c7794 100644--- a/openviking/storage/queuefs/semantic_lock.py+++ b/openviking/storage/queuefs/semantic_lock.py@@ -9,6 +9,7 @@ from typing import Any, Callable, Dict, Iterable, Optionalfrom openviking.storage.errors import LockAcquisitionErrorfrom openviking.storage.internal_names import MULTIWRITE_PATH_LOCK_FILE+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.viking_fs import get_viking_fsfrom openviking_cli.utils.logger import get_logger@@ -84,4 +85,9 @@ class SemanticLockScope:async def close(self) -> None:"""Release the owned lock lease if held."""if self.lock is not None and self._owned:- await get_viking_fs()._async_agfs.pathlock_release(self.lock)+ guard = PathLockGuard(get_viking_fs()._async_agfs, self.lock)+ try:+ await guard.close()+ finally:+ self.lock = guard.lease+ self._owned = self.lock is not Nonediff --git a/openviking/storage/queuefs/semantic_msg.py b/openviking/storage/queuefs/semantic_msg.pyindex 626bc6839..3b6d63181 100644--- a/openviking/storage/queuefs/semantic_msg.py+++ b/openviking/storage/queuefs/semantic_msg.py@@ -76,8 +76,6 @@ class SemanticMsg:aggregate_directory: bool = Trueuse_hierarchical_aggregation: bool = Falsepropagate_to_parent: bool = True- # Hold a producer's lease until every other root in this request settles.- wait_for_request_completion: bool = Falsecopy_source_uri: str = ""# Per-file md5 of final stored bytes, keyed by target URI. Supplied by the# local incremental apply so the tree executor's re-vectorization writes a fresh@@ -116,7 +114,6 @@ class SemanticMsg:aggregate_directory: bool = True,use_hierarchical_aggregation: bool = False,propagate_to_parent: bool = True,- wait_for_request_completion: bool = False,copy_source_uri: str = "",file_md5s: Optional[Dict[str, str]] = None,artifact_ref: Optional[Dict[str, Any]] = None,@@ -156,7 +153,6 @@ class SemanticMsg:self.aggregate_directory = bool(aggregate_directory)self.use_hierarchical_aggregation = bool(use_hierarchical_aggregation)self.propagate_to_parent = bool(propagate_to_parent)- self.wait_for_request_completion = bool(wait_for_request_completion)self.copy_source_uri = copy_source_uriself.file_md5s = dict(file_md5s or {})self.artifact_ref = dict(artifact_ref) if artifact_ref else None@@ -246,7 +242,6 @@ class SemanticMsg:aggregate_directory=data.get("aggregate_directory", True),use_hierarchical_aggregation=data.get("use_hierarchical_aggregation", False),propagate_to_parent=data.get("propagate_to_parent", True),- wait_for_request_completion=data.get("wait_for_request_completion", False),copy_source_uri=data.get("copy_source_uri", ""),file_md5s=(data.get("file_md5s") if isinstance(data.get("file_md5s"), dict) else None),artifact_ref=(diff --git a/openviking/storage/queuefs/semantic_processor.py b/openviking/storage/queuefs/semantic_processor.pyindex 21b9ed482..d0e1ba136 100644--- a/openviking/storage/queuefs/semantic_processor.py+++ b/openviking/storage/queuefs/semantic_processor.py@@ -51,6 +51,7 @@ from openviking.storage.abstract_overview import ()from openviking.storage.errors import LockAcquisitionErrorfrom openviking.storage.index_action import FieldPatch+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResultfrom openviking.storage.queuefs.semantic_executor import SemanticTreeExecutor, SemanticTreeStats@@ -90,6 +91,18 @@ def _is_permanent_storage_error(error: BaseException) -> bool:return any(isinstance(item, _PERMANENT_STORAGE_ERRORS) for item in (error, error.__cause__))+def _executor_lock(msg: SemanticMsg, lock: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:+ """Reuse content-write ownership only when it covers semantic sidecars."""+ if msg.generation_trigger != "content_write" or msg.lock_handoff is None:+ return lock+ covered_paths = msg.lock_handoff.get("covered_paths")+ if isinstance(covered_paths, list) and any(+ isinstance(item, dict) and item.get("kind") == "tree" for item in covered_paths+ ):+ return lock+ return None++class RequestQueueStats:processed: int = 0requeue_count: int = 0@@ -310,19 +323,29 @@ class SemanticProcessor(DequeueHandlerBase):which conflicts with an update request still waiting under its outer lease."""agfs = get_viking_fs()._async_agfs- handoff = await agfs.pathlock_to_handoff(scope.lock)- handed_off = False- try:- await agfs.pathlock_handoff(scope.lock)- handed_off = True+ lock_guard = PathLockGuard(agfs, scope.lock)+ handoff = await lock_guard.to_handoff()+ msg.lock_handoff = handoff++ def accept(result: Any) -> bool:+ if result == "deduplicated":+ return False+ scope.lock = Nonescope._owned = False- msg.lock_handoff = handoff- await queue.enqueue(msg)- except BaseException:- if handed_off:- scope.lock = await agfs.pathlock_adopt(handoff)+ return True++ try:+ enqueue_id = await lock_guard.handoff_and_dispatch(+ handoff,+ lambda: queue.enqueue(msg, deduplicate_memory=False),+ accepted=accept,+ )+ if enqueue_id == "deduplicated":+ raise RuntimeError("Semantic retry was unexpectedly deduplicated")+ finally:+ if lock_guard.lease is not None:+ scope.lock = lock_guard.leasescope._owned = True- raiseasync def _enqueue_skill_retry(self, queue, msg: SemanticMsg, scope: SemanticLockScope) -> None:"""Compatibility wrapper for existing skill retry callers."""@@ -570,43 +593,14 @@ class SemanticProcessor(DequeueHandlerBase):logger.debug("Processing semantic message id=%s uri=%s", msg.id, msg.uri)- task_context = get_task_context()- enqueue_complete = True- if msg.wait_for_request_completion and task_context is not None:- tracker = get_task_tracker()- enqueue_complete = await tracker.is_enqueue_complete(- task_context.task_id,- task_context.work_id or "",- account_id=task_context.account_id,- user_id=task_context.user_id,- )- siblings_pending = await tracker.has_work(- task_context.task_id,- exclude_work_id=task_context.work_id,- account_id=task_context.account_id,- user_id=task_context.user_id,- )- if siblings_pending or (- not enqueue_complete- and tracker.has_active_producer(task_context.task_id)- ):- # Leave ownership with the producer until it finishes- # submission and hands off, without occupying a worker.- await asyncio.sleep(0.05)- await work.reenqueue()- execute_status = "requeued"- return ProcessResult.requeued()-# Resolving a deleted root can recreate it for lock metadata.# Settle queued ownership before acknowledging skipped work.- if not msg.wait_for_request_completion and not await get_viking_fs().exists(- msg.uri, ctx=current_ctx- ):+ if not await get_viking_fs().exists(msg.uri, ctx=current_ctx):logger.info("Skipping semantic message for missing root: uri=%s", msg.uri)await work.skip()return ProcessResult.success()- if not msg.wait_for_request_completion and self._vlm_resolver is None:+ if self._vlm_resolver is None:raise RuntimeError("SemanticProcessor requires a VLM resolver for account-owned work")@@ -614,17 +608,10 @@ class SemanticProcessor(DequeueHandlerBase):return ProcessResult.success()semantic_lock = work.scopeassert semantic_lock is not None- if not enqueue_complete:- return ProcessResult.failed(- "Content write producer ended before child Work enqueue completed"- )dag_stats = Noneprocessing_succeeded = Falsetry:- if msg.wait_for_request_completion:- # The coordinator only releases the lease after sibling work settles.- pass- elif msg.plan is not None:+ if msg.plan is not None:if msg.uri.rstrip("/") != msg.plan.root_uri:raise ValueError("semantic message URI must match plan root_uri")if msg.context_type != msg.plan.context_type:@@ -748,7 +735,7 @@ class SemanticProcessor(DequeueHandlerBase):incremental_update=is_incremental,target_uri=target_uri,recursive=msg.recursive,- lock=semantic_lock.lock,+ lock=_executor_lock(msg, semantic_lock.lock),is_code_repo=msg.is_code_repo,changes=changes,skip_vectorization=msg.skip_vectorization,@@ -792,9 +779,7 @@ class SemanticProcessor(DequeueHandlerBase):failure = work.failure_result(dag_stats)if failure is not None:return failure- is_request_coordinator = msg.wait_for_request_completion- if not is_request_coordinator:- self._merge_request_stats(msg.telemetry_id, processed=1)+ self._merge_request_stats(msg.telemetry_id, processed=1)logger.info(f"Completed semantic generation for: {msg.uri}")breaker.record_success()await self._cleanup_local_artifact(msg)@@ -905,12 +890,13 @@ class SemanticProcessor(DequeueHandlerBase):async def _release_cancelled_semantic_lock(self, msg: SemanticMsg) -> None:if msg.lock_handoff is not None:+ lock_guard = PathLockGuard(get_viking_fs()._async_agfs)try:- viking_fs = get_viking_fs()- lock = await viking_fs._async_agfs.pathlock_adopt(msg.lock_handoff)- await viking_fs._async_agfs.pathlock_release(lock)+ await lock_guard.adopt(msg.lock_handoff)except Exception as exc:logger.warning("Failed to release cancelled semantic lock: %s", exc)+ finally:+ await lock_guard.close()async def _resolve_skill_semantic_lock(self,diff --git a/openviking/storage/queuefs/semantic_queue.py b/openviking/storage/queuefs/semantic_queue.pyindex e85db2280..7ff8f5a68 100644--- a/openviking/storage/queuefs/semantic_queue.py+++ b/openviking/storage/queuefs/semantic_queue.py@@ -42,9 +42,9 @@ class SemanticQueue(NamedQueue):def _memory_parent_semantic_key(msg: SemanticMsg) -> str:return f"{msg.account_id}|{msg.user_id}|{msg.peer_id}|{msg.uri}"- async def enqueue(self, msg: SemanticMsg) -> str:+ async def enqueue(self, msg: SemanticMsg, *, deduplicate_memory: bool = True) -> str:"""Serialize SemanticMsg object and store in queue."""- if msg.context_type == "memory" and not msg.coalesce_key:+ if deduplicate_memory and msg.context_type == "memory" and not msg.coalesce_key:key = self._memory_parent_semantic_key(msg)now = time.monotonic()with self._memory_parent_semantic_lock:diff --git a/openviking/storage/queuefs/semantic_work.py b/openviking/storage/queuefs/semantic_work.pyindex ab8973bc2..2ec972a03 100644--- a/openviking/storage/queuefs/semantic_work.py+++ b/openviking/storage/queuefs/semantic_work.py@@ -51,11 +51,14 @@ class SemanticMessageWork:return resolve_telemetry(self.msg.telemetry_id)async def acquire_lock(self, ctx: RequestContext) -> bool:- self.scope = await SemanticLockScope.resolve(- self.msg.lock_handoff,- caller_lock=self.caller_lock,- fallback_path_factory=lambda: get_viking_fs()._uri_to_path(self.msg.uri, ctx=ctx),- )+ async def acquire() -> None:+ self.scope = await SemanticLockScope.resolve(+ self.msg.lock_handoff,+ caller_lock=self.caller_lock,+ fallback_path_factory=lambda: get_viking_fs()._uri_to_path(self.msg.uri, ctx=ctx),+ )++ await run_to_completion(acquire)return Trueasync def run_write(self, factory: Callable[[], Awaitable[_Result]]) -> _Result:@@ -63,13 +66,7 @@ class SemanticMessageWork:async def finish_processing(self, succeeded: bool) -> None:assert self.scope is not None- context = get_task_context()- if self.msg.wait_for_request_completion and context is not None:- await get_task_tracker().wait_for_task_work(- context.task_id,- exclude_work_id=context.work_id,- )- elif self.msg.lock_handoff is not None:+ if self.msg.lock_handoff is not None:await self._wait_for_embedding_work(stop_waiting=getattr(self.processor, "_embedding_worker_stopped", None),)diff --git a/openviking/storage/viking_fs/_snapshot.py b/openviking/storage/viking_fs/_snapshot.pyindex eb1cb7c7a..51118e89c 100644--- a/openviking/storage/viking_fs/_snapshot.py+++ b/openviking/storage/viking_fs/_snapshot.py@@ -15,6 +15,7 @@ from openviking.pyagfs.exceptions import ()from openviking.server.error_mapping import is_not_found_error, map_exceptionfrom openviking.server.identity import RequestContext, Role+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_work_index import detach_task_contextfrom openviking.storage.acl import AclActionfrom openviking.storage.viking_fs._base import (@@ -526,9 +527,9 @@ class _SnapshotMixin:account_id=real_ctx.account_id,user_id=real_ctx.user.user_id,)- await tracker.update_stage(+ await tracker.patch_task(task.task_id,- "queued",+ TaskPatch(stage="queued"),account_id=real_ctx.account_id,user_id=real_ctx.user.user_id,)diff --git a/openviking/utils/resource_processor.py b/openviking/utils/resource_processor.pyindex 9a23b3b34..476678417 100644--- a/openviking/utils/resource_processor.py+++ b/openviking/utils/resource_processor.py@@ -33,6 +33,7 @@ from openviking.storage.errors import LockAcquisitionErrorfrom openviking.storage.expr import And, Eq, PathScopefrom openviking.storage.index_action import FieldPatchfrom openviking.storage.internal_names import is_storage_internal_name+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs.semantic_processor import SemanticProcessorfrom openviking.storage.resource_rnfv import RequestIntentfrom openviking.storage.viking_fs import LS_ALL_NODES, get_viking_fs@@ -1300,18 +1301,27 @@ class ResourceProcessor:if should_summarize:try:- summary_result = await (await self._summarizer_for(ctx)).summarize(- resource_uris=[root_uri],- ctx=ctx,- skip_vectorization=not build_index,- lock=resource_lock,- temp_uris=[temp_uri],- is_code_repo=bool(prepared.get("is_code_repo")),- ingest_options=ingest_options,- semantic_source=semantic_source,- generation_trigger="resource_ingest",- semantic_plan=semantic_plan,+ summary_guard = (+ PathLockGuard(get_viking_fs()._async_agfs, resource_lock)+ if resource_lock is not None+ else None)+ try:+ summary_result = await (await self._summarizer_for(ctx)).summarize(+ resource_uris=[root_uri],+ ctx=ctx,+ skip_vectorization=not build_index,+ lock=summary_guard,+ temp_uris=[temp_uri],+ is_code_repo=bool(prepared.get("is_code_repo")),+ ingest_options=ingest_options,+ semantic_source=semantic_source,+ generation_trigger="resource_ingest",+ semantic_plan=semantic_plan,+ )+ finally:+ if summary_guard is not None:+ resource_lock = summary_guard.leaseif semantic_plan is not None and summary_result.get("status") != "success":raise RuntimeError(str(summary_result.get("message") or "semantic plan enqueue failed")@@ -1321,7 +1331,14 @@ class ResourceProcessor:and summary_result.get("status") == "success"and summary_result.get("enqueued_count", 0) > 0):- await get_viking_fs()._async_agfs.pathlock_handoff(resource_lock)+ if (+ not summary_result.get("lock_handed_off", False)+ and summary_guard is not None+ ):+ try:+ await summary_guard.handoff()+ finally:+ resource_lock = summary_guard.leaseresource_lock = Noneif semantic_plan is not None and (summary_result.get("status") == "success"diff --git a/openviking/utils/skill_processor.py b/openviking/utils/skill_processor.pyindex cdd1d2429..2d852a38e 100644--- a/openviking/utils/skill_processor.py+++ b/openviking/utils/skill_processor.py@@ -29,6 +29,7 @@ from openviking.privacy.skill_extractor import extract_skill_privacy_valuesfrom openviking.server.identity import RequestContextfrom openviking.server.local_input_guard import deny_direct_local_skill_inputfrom openviking.service.task_tracker_concurrency import run_to_completion+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.viking_fs import VikingFSfrom openviking.storage.vikingdb_manager import VikingDBManagerfrom openviking.telemetry import get_current_telemetry, register_telemetry@@ -747,6 +748,7 @@ class SkillProcessor:telemetry = get_current_telemetry()register_telemetry(telemetry)+ lock_guard = PathLockGuard(viking_fs._async_agfs, lease)msg = SemanticMsg(uri=uri,context_type="skill",@@ -756,24 +758,17 @@ class SkillProcessor:group_ids=ctx.group_ids,role=str(ctx.role),telemetry_id=telemetry.telemetry_id,- lock_handoff=await viking_fs._async_agfs.pathlock_to_handoff(lease),+ lock_handoff=await lock_guard.to_handoff(),generation_trigger="skill_ingest",propagate_to_parent=False,source={"path": source_path},)- handed_off = Falsetry:queue_manager = get_queue_manager()- # Consumers, including cancelled-message cleanup, must never see- # a handoff that the producer has not made ready yet.- await viking_fs._async_agfs.pathlock_handoff(lease)- handed_off = True- await queue_manager.get_queue(queue_manager.SEMANTIC, allow_create=True).enqueue(msg)+ queue = queue_manager.get_queue(queue_manager.SEMANTIC, allow_create=True)+ await lock_guard.handoff_and_dispatch(msg.lock_handoff, lambda: queue.enqueue(msg))except BaseException:- if handed_off:- # Enqueue was rejected/failed. Return ownership to the caller's- # finally block; adopting creates a new owned lease reference.- reclaimed = await viking_fs._async_agfs.pathlock_adopt(msg.lock_handoff)+ if lock_guard.lease is not None and lock_guard.lease is not lease:lease.clear()- lease.update(reclaimed)+ lease.update(lock_guard.lease)raisediff --git a/openviking/utils/summarizer.py b/openviking/utils/summarizer.pyindex ddea97a24..e2a745ff9 100644--- a/openviking/utils/summarizer.py+++ b/openviking/utils/summarizer.py@@ -10,6 +10,7 @@ from dataclasses import dataclass, fieldfrom typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuplefrom openviking.core.namespace import context_type_for_uri+from openviking.storage.pathlock_transfer import PathLockGuardfrom openviking.storage.queuefs import SemanticMsg, get_queue_managerfrom openviking.storage.queuefs.semantic_msg import build_semantic_coalesce_keyfrom openviking.storage.viking_fs import LS_ALL_NODES, get_viking_fs@@ -60,6 +61,7 @@ class Summarizer:*,file_uri: str,ctx: "RequestContext",+ lock_handoff: Dict[str, Any] | None = None,skip_vectorization: bool = False,ingest_options: IngestOptions | None = None,created: bool = False,@@ -94,6 +96,7 @@ class Summarizer:),),ctx=ctx,+ lock_handoff=lock_handoff,skip_vectorization=skip_vectorization,ingest_options=ingest_options,generation_trigger=generation_trigger,@@ -110,6 +113,7 @@ class Summarizer:*,changes: Sequence[FileRefreshRequest],ctx: "RequestContext",+ lock_handoff: Dict[str, Any] | None = None,skip_vectorization: bool = False,ingest_options: IngestOptions | None = None,generation_trigger: str = "semantic_refresh",@@ -135,6 +139,8 @@ class Summarizer:if not grouped:return {"status": "success", "enqueued_count": 0, "parent_actions": {}}+ if lock_handoff is not None and len(grouped) != 1:+ raise ValueError("one semantic lock handoff requires exactly one parent refresh")queue_manager = get_queue_manager()semantic_queue = queue_manager.get_queue(queue_manager.SEMANTIC, allow_create=True)@@ -178,6 +184,7 @@ class Summarizer:user_id=ctx.user.user_id,peer_id=ctx.user.user_id,role=str(ctx.role),+ lock_handoff=lock_handoff,skip_vectorization=skip_vectorization,telemetry_id=telemetry_id,changes={@@ -193,7 +200,7 @@ class Summarizer:user_id=ctx.user.user_id,peer_id=ctx.user.user_id,)- if aggregate_directory+ if aggregate_directory and lock_handoff is Noneelse ""),generation_trigger=generation_trigger,@@ -232,7 +239,7 @@ class Summarizer:resource_uris: List[str],ctx: "RequestContext",skip_vectorization: bool = False,- lock: Optional[Dict[str, Any]] = None,+ lock: Optional[Dict[str, Any] | PathLockGuard] = None,**kwargs,) -> Dict[str, Any]:"""@@ -267,12 +274,20 @@ class Summarizer:"status": "error","message": "temp_uris length must match resource_uris length",}+ if lock is not None and len(resource_uris) != 1:+ raise ValueError("one semantic lock handoff requires exactly one resource")enqueued_count = 0telemetry = get_current_telemetry()+ agfs = get_viking_fs()._async_agfs+ lock_guard = (+ lock+ if isinstance(lock, PathLockGuard)+ else PathLockGuard(agfs, lock) if lock is not None else None+ )lock_handoff: Optional[Dict[str, Any]] = None- if lock is not None:- lock_handoff = await get_viking_fs()._async_agfs.pathlock_to_handoff(lock)+ if lock_guard is not None:+ lock_handoff = await lock_guard.to_handoff()def is_resources_root(uri: str) -> bool:return (uri or "").rstrip("/") == "viking://resources"@@ -333,7 +348,25 @@ class Summarizer:file_abstracts=file_abstracts,plan=semantic_plan,)- enqueue_id = await semantic_queue.enqueue(msg)+ try:+ if lock_guard is None:+ enqueue_id = await semantic_queue.enqueue(msg)+ else:+ enqueue_id = await lock_guard.handoff_and_dispatch(+ lock_handoff,+ lambda msg=msg: semantic_queue.enqueue(msg),+ accepted=lambda result: result != "deduplicated",+ )+ except BaseException:+ if (+ isinstance(lock, dict)+ and lock_guard is not None+ and lock_guard.lease is not None+ and lock_guard.lease is not lock+ ):+ lock.clear()+ lock.update(lock_guard.lease)+ raiseif enqueue_id == "deduplicated":logger.info("Semantic generation already queued for: %s", target_uri)continue@@ -342,4 +375,8 @@ class Summarizer:f"Enqueued semantic generation for: {target_uri} (skip_vectorization={skip_vectorization})")- return {"status": "success", "enqueued_count": enqueued_count}+ return {+ "status": "success",+ "enqueued_count": enqueued_count,+ "lock_handed_off": lock_guard is not None and lock_guard.transferred,+ }diff --git a/tests/parse/test_feishu_parser_api.py b/tests/parse/test_feishu_parser_api.pyindex 29edc074f..f641cec40 100644--- a/tests/parse/test_feishu_parser_api.py+++ b/tests/parse/test_feishu_parser_api.py@@ -16,7 +16,8 @@ from openviking.parse.understanding_api import ()from openviking.server.identity import RequestContext, Rolefrom openviking.service.resource_service import ResourceService-from openviking.service.task_tracker import TaskStatus+from openviking.service.task_domain import QueueWorkSummary, WorkSummary+from openviking.service.task_tracker import TaskStatus, TaskTrackerfrom openviking.service.task_work_index import TASK_WORK_ID_FIELDfrom openviking.storage.queuefs.add_resource_msg import AddResourceMsg, AddResourcePhasefrom openviking.storage.queuefs.add_resource_processor import AddResourceProcessor@@ -105,7 +106,7 @@ async def _interrupt_staged_add_resource(monkeypatch, task_status):task_tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(status=TaskStatus.PENDING, result=None)),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),get_task_auth=AsyncMock(return_value={}),get=AsyncMock(return_value=SimpleNamespace(status=task_status)),)@@ -586,7 +587,7 @@ async def test_uat_producer_payload_reaches_worker_without_persisting_token(task_tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),)@@ -797,12 +798,19 @@ async def test_uat_producer_cancellation_respects_queue_ownership(enqueue = AsyncMock()handoff = AsyncMock()release = AsyncMock()+ handoff_started = asyncio.Event()+ finish_handoff = asyncio.Event()if cancel_stage == "submit":submit_url.side_effect = asyncio.CancelledErrorelif cancel_stage == "enqueue":enqueue.side_effect = asyncio.CancelledErrorelse:- handoff.side_effect = asyncio.CancelledError++ async def slow_handoff(_lease):+ handoff_started.set()+ await finish_handoff.wait()++ handoff.side_effect = slow_handoffresource_processor = SimpleNamespace(should_use_understanding_directly=lambda _source, **_kwargs: True,@@ -811,7 +819,7 @@ async def test_uat_producer_cancellation_respects_queue_ownership()task_tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),fail=AsyncMock(),)queue_manager = SimpleNamespace(enqueue=enqueue)@@ -877,8 +885,8 @@ async def test_uat_producer_cancellation_respects_queue_ownership(role=Role.USER,)- with pytest.raises(asyncio.CancelledError):- await service.add_resource(+ request = asyncio.create_task(+ service.add_resource(path=source,ctx=ctx,to=root_uri,@@ -886,21 +894,33 @@ async def test_uat_producer_cancellation_respects_queue_ownership(allow_local_path_resolution=False,args={"feishu_access_token": "u-secret"},)+ )+ if cancel_stage == "handoff":+ await handoff_started.wait()+ request.cancel()+ await asyncio.sleep(0)+ assert not request.done()+ finish_handoff.set()+ with pytest.raises(asyncio.CancelledError):+ await requestif cancel_stage == "submit":release.assert_not_awaited()task_tracker.create.assert_not_awaited()task_tracker.fail.assert_not_awaited()enqueue.assert_not_awaited()- else:+ elif cancel_stage == "enqueue":release.assert_awaited_once_with(lock_lease)task_tracker.create.assert_awaited_once()enqueue.assert_awaited_once()assert enqueue.await_args.args[0] == QueueManager.EXTERNAL_PARSE- if cancel_stage == "enqueue":- task_tracker.fail.assert_awaited_once()- else:- task_tracker.fail.assert_not_awaited()+ task_tracker.fail.assert_awaited_once()+ else:+ release.assert_not_awaited()+ task_tracker.create.assert_awaited_once()+ enqueue.assert_awaited_once()+ assert enqueue.await_args.args[0] == QueueManager.EXTERNAL_PARSE+ task_tracker.fail.assert_not_awaited()@pytest.mark.parametrize(@@ -1080,29 +1100,19 @@ async def test_add_resource_processor_persists_final_uri_and_cleans_staged_sourctask_tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(status=TaskStatus.PENDING)),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),get_task_auth=AsyncMock(return_value={}),wait_for_task_work=AsyncMock(),- build_queue_status=Mock(- return_value={- "Semantic": {- "processed": 0,- "requeue_count": 0,- "error_count": 0,- "errors": [],- },- "Embedding": {- "processed": 9,- "requeue_count": 0,- "error_count": 0,- "errors": [],- },- }+ get_work_summary=AsyncMock(+ return_value=WorkSummary(+ queues={"Embedding": QueueWorkSummary(processed=9)},+ context_count=9,+ )),- get_context_count=Mock(return_value=9),- get_queue_timing=Mock(return_value={}),+ format_queue_status=TaskTracker.format_queue_status,+ format_queue_timing=TaskTracker.format_queue_timing,)service = SimpleNamespace(@@ -1226,14 +1236,14 @@ async def test_add_resource_processor_collects_stats_without_registered_telemetrtask_tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(status=TaskStatus.PENDING)),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),get_task_auth=AsyncMock(return_value={}),wait_for_task_work=AsyncMock(),- build_queue_status=Mock(return_value={}),- get_context_count=Mock(return_value=context_count),- get_queue_timing=Mock(return_value={}),+ get_work_summary=AsyncMock(return_value=WorkSummary(context_count=context_count)),+ format_queue_status=Mock(return_value={}),+ format_queue_timing=Mock(return_value={}),)monkeypatch.setattr("openviking.storage.queuefs.add_resource_processor.get_task_tracker",@@ -1307,12 +1317,13 @@ async def test_add_resource_processor_replay_skips_lock_adopt_when_result_exists)),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),wait_for_task_work=AsyncMock(),- build_queue_status=Mock(return_value={}),- get_queue_timing=Mock(return_value={}),+ get_work_summary=AsyncMock(return_value=WorkSummary()),+ format_queue_status=Mock(return_value={}),+ format_queue_timing=Mock(return_value={}),)monkeypatch.setattr("openviking.storage.queuefs.add_resource_processor.get_task_tracker",diff --git a/tests/parse/test_feishu_unified_import.py b/tests/parse/test_feishu_unified_import.pyindex 68198ab0d..d90a171b8 100644--- a/tests/parse/test_feishu_unified_import.py+++ b/tests/parse/test_feishu_unified_import.py@@ -13,6 +13,7 @@ from openviking.parse.feishu_import import FeishuImportPlanfrom openviking.parse.parser_router import ParserRouterfrom openviking.parse.parsers.directory import DirectoryParserfrom openviking.parse.understanding_api import UnderstandingAPI+from openviking.service.task_domain import TaskPatchfrom openviking.utils.media_processor import UnifiedResourceProcessorfrom openviking_cli.exceptions import InvalidArgumentErrorfrom openviking_cli.utils.config.parser_config import FeishuConfig@@ -500,7 +501,7 @@ async def test_background_collection_restores_auth_and_child_checkpoint(monkeypatracker = SimpleNamespace(get=AsyncMock(return_value=SimpleNamespace(meta={"feishu_responses": {"entry": "old"}})),- record_feishu_response=AsyncMock(),+ patch_task=AsyncMock(),)monkeypatch.setattr("openviking.service.task_tracker.get_task_tracker", lambda: tracker)service = ResourceService()@@ -527,8 +528,11 @@ async def test_background_collection_restores_auth_and_child_checkpoint(monkeypasaved, record = options["_feishu_checkpoint"]assert saved == {"entry": "old"}await record("next", "new")- tracker.record_feishu_response.assert_awaited_once_with(- "task", "next", "new", "account", "user"+ tracker.patch_task.assert_awaited_once_with(+ "task",+ TaskPatch(meta={"feishu_responses": {"next": "new"}}),+ account_id="account",+ user_id="user",)assert saved["next"] == "new"diff --git a/tests/server/test_admin_rebuild_api.py b/tests/server/test_admin_rebuild_api.pyindex 333b055a2..031cc817a 100644--- a/tests/server/test_admin_rebuild_api.py+++ b/tests/server/test_admin_rebuild_api.py@@ -526,7 +526,7 @@ async def test_reindex_executor_passes_tags_to_background_run(monkeypatch):async def create_if_no_running(self, *args, **kwargs):return FakeTask()- update_stage = AsyncMock()+ patch_task = AsyncMock()fail = AsyncMock()class FakeAGFS:@@ -950,7 +950,8 @@ async def test_reindex_rfv_enqueues_semantic_plan_without_snapshot_source_bytes()assert calls == ["to_handoff", "handoff", "enqueue"]- assert run.lease_handed_off is True+ assert run.lease is None+ assert run.lock is Noneassert queue.message.plan == planassert queue.message.propagate_to_parent is Falseassert queue.message.generation_trigger == "reindex"diff --git a/tests/server/test_api_content_write.py b/tests/server/test_api_content_write.pyindex 604d3680c..640a16da5 100644--- a/tests/server/test_api_content_write.py+++ b/tests/server/test_api_content_write.py@@ -157,11 +157,48 @@ async def test_write_without_wait_is_immediately_readable(client_with_resource):body = write_resp.json()assert body["result"]["semantic_status"] == "queued"assert body["result"]["vector_status"] == "queued"+ task_id = body["result"]["task_id"]read_resp = await client.get("/api/v1/content/read", params={"uri": file_uri})assert read_resp.status_code == 200assert read_resp.json()["result"] == original + "\nImmediate append."+ wait_resp = await client.post("/api/v1/system/wait", json={})+ assert wait_resp.status_code == 200+ task_resp = await client.get(f"/api/v1/tasks/{task_id}")+ assert task_resp.status_code == 200+ assert task_resp.json()["result"]["status"] == "completed"+++async def test_write_agent_skill_completes_without_lock_self_blocking(client):+ uri = "viking://agent/skills/content-write-lock/SKILL.md"+ content = """---+name: content-write-lock+description: Verify direct Agent Skill writes complete.+---++# Content Write Lock++Use this skill to verify lock handoff behavior.+"""++ write_resp = await client.post(+ "/api/v1/content/write",+ json={+ "uri": uri,+ "content": content,+ "mode": "create",+ "wait": True,+ },+ )++ assert write_resp.status_code == 200+ body = write_resp.json()+ assert body["status"] == "ok"+ assert body["result"]["uri"] == uri+ assert body["result"]["semantic_status"] == "complete"+ assert body["result"]["vector_status"] == "complete"+@pytest.mark.asyncioasync def test_write_missing_uri_validation(client):diff --git a/tests/server/test_compile_studio.py b/tests/server/test_compile_studio.pyindex 51c93f68c..ebd7a7658 100644--- a/tests/server/test_compile_studio.py+++ b/tests/server/test_compile_studio.py@@ -10,6 +10,7 @@ import pytestfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.external_task_service import ExternalTaskService+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_pagination import decode_cursor, encode_cursor, page_scopefrom openviking.service.task_store import PersistentTaskStorefrom openviking.service.task_tracker import TaskStatus, TaskTracker@@ -116,7 +117,11 @@ async def test_concurrent_submission_retry_and_restart(monkeypatch):owner = {"account_id": ctx.account_id, "user_id": ctx.user.user_id}await tracker.start(again.task_id, stage="polling", **owner)- await tracker.update_task_auth(again.task_id, {"external_task_id": "submitted"}, **owner)+ await tracker.patch_task(+ again.task_id,+ TaskPatch(auth={"external_task_id": "submitted"}),+ **owner,+ )tracker = TaskTracker(PersistentTaskStore(agfs))service = ExternalTaskService()service.register(provider)diff --git a/tests/server/test_content_batch_write.py b/tests/server/test_content_batch_write.pyindex cc0f7fa0a..d96db6098 100644--- a/tests/server/test_content_batch_write.py+++ b/tests/server/test_content_batch_write.py@@ -9,14 +9,10 @@ import pytestimport openviking.storage.content_write as content_write_modulefrom openviking.server.identity import RequestContext, Rolefrom openviking.service.task_store import PersistentTaskStore-from openviking.service.task_tracker import TaskTracker, get_task_tracker, run_tracked_operation-from openviking.service.task_work_index import (- bind_task_context,- get_task_context,- record_enqueued_work,-)+from openviking.service.task_tracker import TaskTrackerfrom openviking.session.memory.dataclass import MemoryFilefrom openviking.session.memory.utils import MemoryFileUtils+from openviking.storage.acl import AclSpec, AclUpdatefrom openviking.storage.content_write import ContentWriteCoordinatorfrom openviking.storage.context_update_plan import (ContentTreeAction,@@ -31,7 +27,6 @@ from openviking.utils.content_hash import content_md5from openviking_cli.exceptions import (InvalidArgumentError,NotFoundError,- OpenVikingError,)from openviking_cli.session.user_id import UserIdentifierfrom tests.test_task_tracker import _FakeAgfs@@ -63,6 +58,11 @@ class _PathLockClient:self.held = Falseself.handoffs += 1+ async def pathlock_adopt(self, handoff):+ assert handoff["owner_id"] == "lock-1"+ self.held = True+ return {"lease_ref": "lock-1"}+async def pathlock_release(self, lease):assert lease == {"lease_ref": "lock-1"}self.held = False@@ -129,21 +129,33 @@ def _coordinator_queue_for_unit_tests(monkeypatch, request):yield Nonereturn- queue = SimpleNamespace(enqueue=AsyncMock(return_value=EnqueueReceipt("queued", "work-1")))+ semantic_queue = SimpleNamespace(+ enqueue=AsyncMock(return_value=EnqueueReceipt("queued", "semantic-work-1"))+ )+ content_write_queue = SimpleNamespace(+ enqueue=AsyncMock(return_value=EnqueueReceipt("queued", "content-write-work-1"))+ )+ queues = {+ "semantic": semantic_queue,+ "ContentWrite": content_write_queue,+ }queue_manager = SimpleNamespace(SEMANTIC="semantic",- get_queue=lambda *args, **kwargs: queue,+ CONTENT_WRITE="ContentWrite",+ get_queue=lambda name, *args, **kwargs: queues[name],)monkeypatch.setattr(content_write_module, "get_queue_manager", lambda: queue_manager)tracker = TaskTracker(PersistentTaskStore(_FakeAgfs()))monkeypatch.setattr("openviking.service.task_tracker._instance", tracker)+ yield SimpleNamespace(semantic=semantic_queue, content_write=content_write_queue)- async def run_tracked_operation(_task_type, *, operation, **_kwargs):- with bind_task_context("test-task", "default", "default", enqueued_work=[]):- return await operation(), "test-task"- monkeypatch.setattr(content_write_module, "run_tracked_operation", run_tracked_operation)- yield queue+def _last_content_write_payload(queue_fixture):+ return queue_fixture.content_write.enqueue.await_args.args[0]+++def _last_content_write_refresh(queue_fixture):+ return _last_content_write_payload(queue_fixture)["refresh"]@pytest.mark.asyncio@@ -246,7 +258,10 @@ async def test_batch_missing_append_creates_its_initial_body(monkeypatch):@pytest.mark.asyncio-async def test_batch_resource_files_compile_independent_rnfv_plans(monkeypatch):+async def test_batch_resource_files_compile_independent_rnfv_plans(+ monkeypatch,+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"first = f"{root}/docs/a.md"second = f"{root}/docs/b.md"@@ -348,11 +363,22 @@ async def test_batch_resource_files_compile_independent_rnfv_plans(monkeypatch):assert snapshots_by_uri[second]["vector_inventory"] == {}assert len(commits) == 2assert direct_actions == []- assert len(refresh_calls) == 1- assert {change.file_uri for change in refresh_calls[0]} == {first, second}- assert {change.file_uri: change.vector_action for change in refresh_calls[0]} == {- first: "",- second: "upsert",+ assert refresh_calls == []+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ plans = [+ ContextUpdatePlan.from_dict(item["plan"])+ for item in refresh["resource_plans"]+ ]+ assert {plan.file_refresh.file_uri for plan in plans if plan.file_refresh is not None} == {+ first,+ second,+ }+ assert {+ item["plan"]["file_refresh"]["file_uri"]: item["existed"]+ for item in refresh["resource_plans"]+ } == {+ first: True,+ second: False,}@@ -415,7 +441,10 @@ async def test_batch_existing_files_defer_full_vector_reads_to_embedding_worker(@pytest.mark.asyncio-async def test_batch_same_content_with_missing_abstract_enqueues_file_refresh(monkeypatch):+async def test_batch_same_content_with_missing_abstract_enqueues_file_refresh(+ monkeypatch,+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"uri = f"{root}/docs/a.md"vfs = _VFS(root, {uri: "unchanged"})@@ -455,14 +484,20 @@ async def test_batch_same_content_with_missing_abstract_enqueues_file_refresh(moprocessing_mode="semantic_and_vectors",)- changes = refresh_file_parents.await_args.kwargs["changes"]- assert [(change.file_uri, change.md5) for change in changes] == [- (uri, content_md5(b"unchanged"))- ]+ refresh_file_parents.assert_not_awaited()+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ plan = ContextUpdatePlan.from_dict(refresh["resource_plans"][0]["plan"])+ assert (plan.file_refresh.file_uri, plan.file_refresh.md5) == (+ uri,+ content_md5(b"unchanged"),+ )@pytest.mark.asyncio-async def test_batch_vectors_only_existing_file_enqueues_merge_patch_without_hydration(monkeypatch):+async def test_batch_vectors_only_existing_file_enqueues_merge_patch_without_hydration(+ monkeypatch,+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"uri = f"{root}/a.md"vfs = _VFS(root, {uri: "old"})@@ -513,8 +548,11 @@ async def test_batch_vectors_only_existing_file_enqueues_merge_patch_without_hydprocessing_mode="vectors_only",)- assert len(actions) == 1- action = actions[0]+ assert actions == []+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ plan = ContextUpdatePlan.from_dict(refresh["resource_plans"][0]["plan"])+ assert len(plan.direct_index_actions) == 1+ action = plan.direct_index_actions[0]assert action.action.value == "merge"assert action.record_id == "existing-l2"assert action.field_patch is not None@@ -616,16 +654,20 @@ async def test_batch_resource_plan_allows_clear_tags_without_tags(monkeypatch):@pytest.mark.asyncio-async def test_batch_applies_operation_acl_and_forwards_file_options(monkeypatch):+async def test_batch_applies_operation_acl_and_forwards_file_options(+ monkeypatch,+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"uri = f"{root}/a.md"vfs = _VFS(root)- prepared_update = object()+ prepared_update = AclUpdate(+ uri=uri,+ acl=AclSpec(acl_mode="restricted", entries=()),+ )vfs.prepare_acl_update = AsyncMock(return_value=prepared_update)vfs.acl_manager = SimpleNamespace(apply_indexed_update=AsyncMock())coordinator = ContentWriteCoordinator(vfs)- refresh = AsyncMock(return_value=None)- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)await coordinator.batch_write(@@ -645,20 +687,23 @@ async def test_batch_applies_operation_acl_and_forwards_file_options(monkeypatchvfs.prepare_acl_update.assert_awaited_once()vfs.acl_manager.apply_indexed_update.assert_awaited_once_with(prepared_update, ctx)- file_options = refresh.await_args.kwargs["file_ingest_options"]- assert file_options[uri].search_tags == ["team=search"]- assert file_options[uri].search_tag_mode == "append"- assert file_options[uri].acl_update is prepared_update+ file_options = _last_content_write_refresh(_coordinator_queue_for_unit_tests)[+ "file_ingest_options"+ ]+ assert file_options[uri]["search_tags"] == ["team=search"]+ assert file_options[uri]["search_tag_mode"] == "append"+ assert file_options[uri]["acl_update"] == prepared_update.model_dump(mode="json")@pytest.mark.asyncio-async def test_batch_same_content_with_tags_is_not_skipped_as_a_conflict_noop(monkeypatch):+async def test_batch_same_content_with_tags_is_not_skipped_as_a_conflict_noop(+ monkeypatch,+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"uri = f"{root}/a.md"vfs = _VFS(root, {uri: "same"})coordinator = ContentWriteCoordinator(vfs)- refresh = AsyncMock(return_value=None)- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)await coordinator.batch_write(@@ -677,82 +722,127 @@ async def test_batch_same_content_with_tags_is_not_skipped_as_a_conflict_noop(mo)assert vfs.writes == [uri]- assert refresh.await_args.kwargs["file_ingest_options"][uri].search_tags == ["team=search"]+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ assert refresh["file_ingest_options"][uri]["search_tags"] == ["team=search"]@pytest.mark.asyncio-async def test_batch_hands_off_file_locks_after_one_aggregated_refresh(monkeypatch):+async def test_batch_hands_off_file_locks_after_one_aggregated_refresh(+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"a = f"{root}/a.md"b = f"{root}/b.md"vfs = _VFS(root)locks = vfs._async_agfscoordinator = ContentWriteCoordinator(vfs)- calls = []- workers = []- tracker = get_task_tracker()-- async def enqueue(msg):- context = get_task_context()- owner = {"account_id": context.account_id, "user_id": context.user_id}- await tracker.register_work(context.task_id, "coordinator", "Semantic", **owner)- record_enqueued_work("Semantic", "coordinator")-- async def finish_after_producer():- await tracker.wait_for_task_work(- context.task_id, exclude_work_id="coordinator", poll_interval=0.001, **owner- )- await tracker.mark_work_done(context.task_id, "coordinator", **owner)-- workers.append(asyncio.create_task(finish_after_producer()))- return EnqueueReceipt("queued", "coordinator")-- queue = SimpleNamespace(enqueue=AsyncMock(side_effect=enqueue))- queue_manager = SimpleNamespace(- SEMANTIC="semantic",- get_queue=lambda *args, **kwargs: queue,- )telemetry = OperationTelemetry(operation="content.batch_write", enabled=False)+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- async def refresh(**kwargs):- assert locks.held is True- calls.append(kwargs["refresh_kinds"])- return {"Semantic": {"processed": 1, "error_count": 0}}+ with bind_telemetry(telemetry):+ result = await coordinator.batch_write(+ root_uri=root,+ operations=[+ {"uri": b, "content": "B", "mode": "upsert"},+ {"uri": a, "content": "A", "mode": "upsert"},+ ],+ ctx=ctx,+ wait=False,+ )- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)- monkeypatch.setattr(content_write_module, "get_queue_manager", lambda: queue_manager)- monkeypatch.setattr(content_write_module, "run_tracked_operation", run_tracked_operation)- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- try:- with bind_telemetry(telemetry):- result = await asyncio.wait_for(- coordinator.batch_write(- root_uri=root,- operations=[- {"uri": b, "content": "B", "mode": "upsert"},- {"uri": a, "content": "A", "mode": "upsert"},- ],- ctx=ctx,- wait=True,- ),- timeout=1,- )- finally:- for worker in workers:- worker.cancel()- await asyncio.gather(*workers, return_exceptions=True)assert vfs.writes == [a, b]assert result["created"] == [a, b]- assert result["queue_status"]["Semantic"]["processed"] == 1- assert calls == [{a: "added", b: "added"}]+ assert result["queue_status"] is None+ assert result["semantic_status"] == "queued"+ assert result["vector_status"] == "queued"assert locks.releases == 0assert locks.handoffs == 1- coordinator_msg = queue.enqueue.await_args.args[0]- assert coordinator_msg.wait_for_request_completion is True+ payload = _coordinator_queue_for_unit_tests.content_write.enqueue.await_args.args[0]+ assert payload["operation"] == "content_batch_write"+ assert payload["resource_id"] == root+ assert payload["result"]["created"] == [a, b]+ assert payload["refresh"]["kind"] == "batch"+ assert payload["refresh"]["refresh_kinds"] == {a: "added", b: "added"}+ assert payload["lock_handoff"]["owner_id"] == "lock-1"+++async def test_batch_retries_after_root_enqueue_failure(+ _coordinator_queue_for_unit_tests,+):+ root = "viking://resources/wiki"+ target = f"{root}/a.md"+ vfs = _VFS(root)+ coordinator = ContentWriteCoordinator(vfs)+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)+ _coordinator_queue_for_unit_tests.content_write.enqueue.side_effect = RuntimeError(+ "queue unavailable"+ )++ with pytest.raises(RuntimeError, match="queue unavailable"):+ await coordinator.batch_write(+ root_uri=root,+ operations=[{"uri": target, "content": "A", "mode": "upsert"}],+ ctx=ctx,+ wait=False,+ )++ _coordinator_queue_for_unit_tests.content_write.enqueue.side_effect = None+ result = await coordinator.batch_write(+ root_uri=root,+ operations=[{"uri": target, "content": "B", "mode": "upsert"}],+ ctx=ctx,+ wait=False,+ )++ assert result["updated"] == [target]+ assert vfs.files[target] == "B"+++async def test_batch_cancellation_waits_for_root_enqueue_settlement(+ _coordinator_queue_for_unit_tests,+):+ root = "viking://resources/wiki"+ target = f"{root}/a.md"+ vfs = _VFS(root)+ coordinator = ContentWriteCoordinator(vfs)+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)+ enqueue_started = asyncio.Event()+ finish_enqueue = asyncio.Event()++ async def enqueue(_payload):+ enqueue_started.set()+ await finish_enqueue.wait()+ return EnqueueReceipt("queued", "content-write-work-1")++ _coordinator_queue_for_unit_tests.content_write.enqueue.side_effect = enqueue+ request = asyncio.create_task(+ coordinator.batch_write(+ root_uri=root,+ operations=[{"uri": target, "content": "A", "mode": "upsert"}],+ ctx=ctx,+ wait=False,+ )+ )+ await enqueue_started.wait()+ request.cancel()+ await asyncio.sleep(0)+ assert not request.done()++ finish_enqueue.set()+ with pytest.raises(asyncio.CancelledError):+ await request++ assert vfs._async_agfs.handoffs == 1+ assert vfs._async_agfs.releases == 0+ assert vfs._async_agfs.held is False@pytest.mark.asyncio-async def test_batch_passes_final_md5s_and_old_abstracts_to_refresh(monkeypatch):+async def test_batch_passes_final_md5s_and_old_abstracts_to_refresh(+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"existing = f"{root}/existing.md"created = f"{root}/new.md"@@ -764,13 +854,6 @@ async def test_batch_passes_final_md5s_and_old_abstracts_to_refresh(monkeypatch)return {existing: {"abstract": "old abstract", "md5": "old-md5"}}coordinator = ContentWriteCoordinator(vfs, vikingdb=_VikingDB())- captured = {}-- async def refresh(**kwargs):- captured.update(kwargs)- return None-- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)await coordinator.batch_write(@@ -783,11 +866,12 @@ async def test_batch_passes_final_md5s_and_old_abstracts_to_refresh(monkeypatch)wait=False,)- assert captured["file_md5s"] == {+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ assert refresh["file_md5s"] == {existing: content_md5(b"new"),created: content_md5(b"created"),}- assert captured["file_abstracts"] == {existing: "old abstract"}+ assert refresh["file_abstracts"] == {existing: "old abstract"}@pytest.mark.asyncio@@ -858,27 +942,19 @@ async def test_batch_reports_skipped_directory_and_queued_file_vectors(monkeypatwait=False,)- assert result["semantic_status"] == "skipped"+ assert result["semantic_status"] == "queued"assert result["vector_status"] == "queued"assert result["queue_status"] is None@pytest.mark.asyncio-async def test_batch_upserts_binary_content(monkeypatch):+async def test_batch_upserts_binary_content(_coordinator_queue_for_unit_tests):root = "viking://resources/wiki"image = f"{root}/figure.png"original = b"\x89PNG\r\n\x1a\nold"replacement = b"\x89PNG\r\n\x1a\nnew"vfs = _VFS(root, {image: original})coordinator = ContentWriteCoordinator(vfs)-- refresh_calls = []-- async def refresh(**kwargs):- refresh_calls.append(kwargs)- return None-- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)operation = {"uri": image,@@ -890,7 +966,8 @@ async def test_batch_upserts_binary_content(monkeypatch):)assert result["updated"] == [image]assert vfs.files[image] == replacement- assert refresh_calls[0]["file_md5s"] == {image: content_md5(replacement)}+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ assert refresh["file_md5s"] == {image: content_md5(replacement)}retry = await coordinator.batch_write(root_uri=root, operations=[operation], ctx=ctx, wait=False@@ -935,20 +1012,14 @@ async def test_batch_rejects_invalid_binary_payload_and_memory_binary():@pytest.mark.asyncio-async def test_batch_partial_failure_refreshes_successful_files_and_retry_is_safe(monkeypatch):+async def test_batch_partial_failure_refreshes_successful_files_and_retry_is_safe(+ _coordinator_queue_for_unit_tests,+):root = "viking://resources/wiki"a = f"{root}/a.md"b = f"{root}/b.md"vfs = _VFS(root, fail_uri=b)- locks = vfs._async_agfscoordinator = ContentWriteCoordinator(vfs)- calls = []-- async def refresh(**kwargs):- assert locks.held is True- calls.append(dict(kwargs["refresh_kinds"]))-- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)operations = [{"uri": a, "content": "A", "mode": "upsert"},@@ -956,7 +1027,9 @@ async def test_batch_partial_failure_refreshes_successful_files_and_retry_is_saf]with pytest.raises(OSError, match="injected"):await coordinator.batch_write(root_uri=root, operations=operations, ctx=ctx, wait=False)- assert calls == [{a: "added"}]+ payload = _last_content_write_payload(_coordinator_queue_for_unit_tests)+ assert payload["failure"]["message"] == "injected write failure"+ assert payload["refresh"]["refresh_kinds"] == {a: "added"}vfs.fail_uri = Noneresult = await coordinator.batch_write(@@ -964,47 +1037,8 @@ async def test_batch_partial_failure_refreshes_successful_files_and_retry_is_saf)assert result["updated"] == [a]assert result["created"] == [b]- assert calls[-1] == {a: "modified", b: "added"}---async def test_batch_refresh_failure_retry_rewrites_and_refreshes(monkeypatch):- root = "viking://resources/wiki"- page = f"{root}/page.md"- vfs = _VFS(root)- coordinator = ContentWriteCoordinator(vfs)- calls = []-- async def refresh(**kwargs):- calls.append(dict(kwargs["refresh_kinds"]))- if len(calls) == 1:- raise RuntimeError("injected refresh failure")-- monkeypatch.setattr(coordinator, "_refresh_batch", refresh)- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- operations = [- {- "uri": page,- "content": "landed",- "mode": "upsert",- }- ]-- with pytest.raises(OpenVikingError) as error:- await coordinator.batch_write(root_uri=root, operations=operations, ctx=ctx, wait=False)- assert error.value.code == "REFRESH_FAILED"- assert "injected refresh failure" in str(error.value)- assert "do not blindly retry an already-successful append" in str(error.value)- assert error.value.details["created"] == [page]- assert vfs.files[page] == "landed"- assert vfs.writes == [page]-- result = await coordinator.batch_write(- root_uri=root, operations=operations, ctx=ctx, wait=False- )- assert result["updated"] == [page]- assert vfs.writes == [page, page]- assert calls == [{page: "added"}, {page: "modified"}]+ refresh = _last_content_write_refresh(_coordinator_queue_for_unit_tests)+ assert refresh["refresh_kinds"] == {a: "modified", b: "added"}@pytest.mark.asynciodiff --git a/tests/server/test_content_write_service.py b/tests/server/test_content_write_service.pyindex 4f11591a5..4e47c8761 100644--- a/tests/server/test_content_write_service.py+++ b/tests/server/test_content_write_service.py@@ -8,7 +8,6 @@ from types import SimpleNamespaceimport pytestfrom openviking.server.identity import RequestContext, Role-from openviking.service.task_work_index import bind_task_contextfrom openviking.session.memory.dataclass import MemoryFilefrom openviking.session.memory.utils import MemoryFileUtilsfrom openviking.session.memory.utils.content_visibility import visible_content@@ -23,21 +22,6 @@ from openviking_cli.exceptions import (from openviking_cli.session.user_id import UserIdentifier[email protected](autouse=True)-def run_direct_coordinator_operations_inline(request, monkeypatch):- if "service" in request.fixturenames:- return-- async def run_tracked_operation(_task_type, *, operation, **_kwargs):- with bind_task_context("test-task", "default", "test-user", enqueued_work=[]):- return await operation(), "test-task"-- monkeypatch.setattr(- "openviking.storage.content_write.run_tracked_operation",- run_tracked_operation,- )--@pytest.mark.asyncioasync def test_write_updates_memory_file_and_parent_overview(service):ctx = RequestContext(user=service.user, role=Role.USER)@@ -493,12 +477,12 @@ async def test_memory_create_refreshes_nested_schema_overview(service):),ctx=ctx,mode="create",- wait=False,+ wait=True,)overview = await service.viking_fs.read_file(f"{memory_dir}/.overview.md", ctx=ctx)assert result["root_uri"] == memory_dir- assert "[不二周助-link-test.md](./不二周助-link-test.md)" in overview+ assert "(./不二周助-link-test.md)" in overview@pytest.mark.asyncio@@ -708,340 +692,6 @@ async def test_write_lock_storage_error_is_not_mapped_to_resource_busy(file_uri))-async def test_memory_write_wait_skips_semantic_queue_and_releases_write_lock(monkeypatch):- file_uri = "viking://user/default/memories/preferences/theme.md"- root_uri = "viking://user/default/memories/preferences"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- async def _fake_write_in_place(uri, content, *, mode, ctx, lock_handle=None, lease_ref=None):- del uri, content, mode, ctx, lock_handle, lease_ref- return b"updated"-- async def _fail_wait_for_request(*, telemetry_id):- del telemetry_id- raise AssertionError("memory write should not wait for semantic refresh")-- async def _fake_refresh_schema_overview(**kwargs):- del kwargs- return True-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)- monkeypatch.setattr(coordinator, "_wait_for_request", _fail_wait_for_request)- monkeypatch.setattr(- "openviking.storage.content_write.MemoryUpdater.refresh_schema_overview",- _fake_refresh_schema_overview,- )-- result = await coordinator.write(- uri=file_uri,- content="updated",- ctx=ctx,- wait=True,- )-- assert viking_fs._async_agfs.release_calls == ["lock-1"]- assert result["semantic_status"] == "skipped"- assert result["vector_status"] == "skipped"- assert result["overview_status"] == "complete"- assert result["queue_status"] is None---# Create-mode test helpers---class _FakeVikingFSForCreate:- """Variant of _FakeVikingFS that supports 'file doesn't exist' scenarios."""-- def __init__(- self,- file_uri: str,- root_uri: str,- file_exists: bool = True,- existing_dirs: set[str] | None = None,- ):- self._file_uri = file_uri- self._root_uri = root_uri- self._file_exists = file_exists- self.delete_temp_calls = []- self.write_file_calls = []- self.rm_calls = []- self.content = {}- self.existing_dirs = set({root_uri} if existing_dirs is None else existing_dirs)- self._async_agfs = _FakePathLock()-- async def stat(self, uri: str, ctx=None, skip_count=False):- del ctx- assert skip_count is True- if uri == self._file_uri:- if self._file_exists:- return {"isDir": False}- raise NotFoundError(uri, "file")- if uri in self.existing_dirs:- return {"isDir": True}- # Parent directories should exist for creation- if uri != self._root_uri and uri.startswith(self._root_uri) and uri != self._file_uri:- return {"isDir": True}- raise NotFoundError(uri, "path")-- def _uri_to_path(self, uri: str, ctx=None):- del ctx- return f"/fake/{uri.replace('://', '/').strip('/')}"-- async def _ensure_access(self, uri: str, ctx, *, action):- del uri, ctx, action-- async def delete_temp(self, temp_uri: str, ctx=None):- del ctx- self.delete_temp_calls.append(temp_uri)-- async def write_file(- self,- uri: str,- content: str,- *,- ctx=None,- lock_handle=None,- lease_ref=None,- ):- del ctx- self.write_file_calls.append((uri, content, lease_ref or lock_handle))- self.content[uri] = content- parent = uri.rsplit("/", 1)[0]- while parent.startswith("viking://") and parent not in self.existing_dirs:- self.existing_dirs.add(parent)- if "/" not in parent.removeprefix("viking://"):- break- parent = parent.rsplit("/", 1)[0]-- async def rm(self, uri: str, *, ctx=None, lock_handle=None, lease_ref=None):- del ctx, lock_handle, lease_ref- self.rm_calls.append(uri)- self.content.pop(uri, None)---# Create-mode tests---async def test_create_mode_new_file_success(monkeypatch):- file_uri = "viking://user/default/memories/new_file.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=False)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- write_calls = []-- async def _fake_write_in_place(- uri, content, *, mode, ctx, lock_handle=None, lease_ref=None, is_new_file=None- ):- del ctx, lock_handle, lease_ref- write_calls.append((uri, content, mode, is_new_file))- return content-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)-- result = await coordinator.write(- uri=file_uri, content="new content", mode="create", ctx=ctx, wait=True- )-- assert result["mode"] == "create"- assert write_calls == [(file_uri, "new content", "replace", True)]---async def test_create_mode_refreshes_canonical_user_memory_uri(monkeypatch):- canonical_uri = "viking://user/default/memories/new_file.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(- file_uri=canonical_uri,- root_uri=root_uri,- file_exists=False,- )- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- write_calls = []- refresh_calls = []-- async def _fake_write_in_place(- uri, content, *, mode, ctx, lock_handle=None, lease_ref=None, is_new_file=None- ):- del ctx, lock_handle, lease_ref- write_calls.append((uri, content, mode, is_new_file))- return content-- async def _fake_refresh_schema_overview(**kwargs):- refresh_calls.append(kwargs)- return None-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)- monkeypatch.setattr(- "openviking.storage.content_write.MemoryUpdater.refresh_schema_overview",- _fake_refresh_schema_overview,- )-- result = await coordinator.write(- uri=canonical_uri, content="new content", mode="create", ctx=ctx, wait=True- )-- assert result["uri"] == canonical_uri- assert result["root_uri"] == root_uri- assert result["context_type"] == "memory"- assert write_calls == [(canonical_uri, "new content", "replace", True)]- assert refresh_calls[0]["directory_uri"] == root_uri---async def test_create_mode_overwrites_existing_file(monkeypatch):- # create is an upsert alias for replace: an existing target is overwritten,- # never rejected with AlreadyExistsError.- file_uri = "viking://user/default/memories/existing.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=True)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- write_calls = []-- async def _fake_write_in_place(- uri, content, *, mode, ctx, lock_handle=None, lease_ref=None, is_new_file=None- ):- del ctx, lock_handle, lease_ref- write_calls.append((uri, content, mode, is_new_file))- return content.encode("utf-8")-- async def _fake_refresh_schema_overview(**kwargs):- del kwargs- return True-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)- monkeypatch.setattr(- "openviking.storage.content_write.MemoryUpdater.refresh_schema_overview",- _fake_refresh_schema_overview,- )-- result = await coordinator.write(- uri=file_uri, content="content", mode="create", ctx=ctx, wait=False- )-- # response echoes the requested mode, but an existing target uses replace- # rendering (memory keeps its metadata trailer).- assert result["mode"] == "create"- assert write_calls == [(file_uri, "content", "replace", None)]---async def test_create_mode_invalid_extension_raises_400(monkeypatch):- file_uri = "viking://user/default/memories/test.exe"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=False)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- async def _fake_write_in_place(uri, content, *, mode, ctx, lock_handle=None, lease_ref=None):- del uri, content, mode, ctx, lock_handle, lease_ref- return None-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)-- with pytest.raises(InvalidArgumentError):- await coordinator.write(uri=file_uri, content="content", mode="create", ctx=ctx, wait=True)---async def test_create_mode_parent_dirs_auto_created(monkeypatch):- file_uri = "viking://user/default/memories/new_subdir/test.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=False)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- write_calls = []-- async def _fake_write_in_place(- uri, content, *, mode, ctx, lock_handle=None, lease_ref=None, is_new_file=None- ):- del ctx, lock_handle, lease_ref- write_calls.append((uri, content, mode, is_new_file))- return content-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)-- result = await coordinator.write(- uri=file_uri, content="nested content", mode="create", ctx=ctx, wait=True- )-- assert result["mode"] == "create"- assert write_calls == [(file_uri, "nested content", "replace", True)]---async def test_create_mode_valid_extensions_pass(monkeypatch):- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)-- # Test a representative set of valid extensions- valid_extensions = [".md", ".txt", ".json", ".yaml", ".yml", ".py", ".js", ".ts"]-- for ext in valid_extensions:- file_uri = f"viking://user/default/memories/test{ext}"- root_uri = "viking://user/default/memories"- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=False)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- async def _fake_write_in_place(- uri, content, *, mode, ctx, lock_handle=None, lease_ref=None, is_new_file=None- ):- del uri, mode, ctx, lock_handle, lease_ref, is_new_file- return content-- async def _fake_wait_for_queues(**_kwargs):- return None-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)- monkeypatch.setattr(coordinator, "_wait_for_request", _fake_wait_for_queues)-- result = await coordinator.write(- uri=file_uri, content="content", mode="create", ctx=ctx, wait=True- )- assert result["mode"] == "create"---async def test_create_mode_memory_scope(monkeypatch):- file_uri = "viking://user/default/memories/test.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=False)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- async def _fake_write_in_place(- uri, content, *, mode, ctx, lock_handle=None, lease_ref=None, is_new_file=None- ):- del uri, mode, ctx, lock_handle, lease_ref, is_new_file- return content-- refresh_calls = []-- async def _fake_refresh_schema_overview(**kwargs):- refresh_calls.append(kwargs)- return None-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)- monkeypatch.setattr(- "openviking.storage.content_write.MemoryUpdater.refresh_schema_overview",- _fake_refresh_schema_overview,- )-- result = await coordinator.write(- uri=file_uri, content="content", mode="create", ctx=ctx, wait=True- )- assert result["context_type"] == "memory"- assert refresh_calls[0]["directory_uri"] == root_uri-class _AnyDirVikingFS:"""Stats the given file uri as a file and every other uri as a directory."""@@ -1092,51 +742,6 @@ async def test_resource_write_anchors_nested_file_to_direct_parent(assert set_tags_anchor == project_root-async def test_create_mode_regression_replace_unchanged(monkeypatch):- file_uri = "viking://user/default/memories/theme.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=True)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- async def _fake_write_in_place(uri, content, *, mode, ctx, lock_handle=None, lease_ref=None):- # Verify mode="replace" still works- assert mode == "replace"- del uri, content, ctx, lock_handle, lease_ref- return b"updated"-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)-- result = await coordinator.write(- uri=file_uri, content="updated", ctx=ctx, mode="replace", wait=True- )-- assert result["mode"] == "replace"---async def test_create_mode_regression_append_unchanged(monkeypatch):- file_uri = "viking://user/default/memories/theme.md"- root_uri = "viking://user/default/memories"- ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)- viking_fs = _FakeVikingFSForCreate(file_uri=file_uri, root_uri=root_uri, file_exists=True)- coordinator = ContentWriteCoordinator(viking_fs=viking_fs)-- async def _fake_write_in_place(uri, content, *, mode, ctx, lock_handle=None, lease_ref=None):- # Verify mode="append" still works- assert mode == "append"- del uri, content, ctx, lock_handle, lease_ref- return b"previousappended"-- monkeypatch.setattr(coordinator, "_write_in_place", _fake_write_in_place)-- result = await coordinator.write(- uri=file_uri, content="appended", ctx=ctx, mode="append", wait=True- )-- assert result["mode"] == "append"-@pytest.mark.asyncioasync def test_set_tags_updates_vector_record(monkeypatch):diff --git a/tests/service/test_resource_service_connector.py b/tests/service/test_resource_service_connector.pyindex 1d80b4f78..dc0db6ef3 100644--- a/tests/service/test_resource_service_connector.py+++ b/tests/service/test_resource_service_connector.py@@ -109,7 +109,7 @@ def _task_tracker():return SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),mark_cancelled=AsyncMock(),@@ -2512,7 +2512,7 @@ async def test_monitor_connector_task_maps_terminal_status(on_success=on_success,)- assert tracker.update_stage.await_args.args[1] == expected_stage+ assert tracker.patch_task.await_args.args[1].stage == expected_stageassert outcome["status"] == expected_statusif expected_status == "completed":on_success.assert_awaited_once_with(None)diff --git a/tests/service/test_resource_service_understanding_routing.py b/tests/service/test_resource_service_understanding_routing.pyindex 65b94b366..24c147e06 100644--- a/tests/service/test_resource_service_understanding_routing.py+++ b/tests/service/test_resource_service_understanding_routing.py@@ -75,7 +75,7 @@ async def test_extensionless_remote_url_queues_frozen_understanding_route(service._connector_delegate = SimpleNamespace(should_delegate=lambda *_args, **_kwargs: False)tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),fail=AsyncMock(),)queue_manager = SimpleNamespace(enqueue=AsyncMock())@@ -169,7 +169,7 @@ async def test_remote_mpeg_ts_url_queues_understanding_after_prepare(service._connector_delegate = SimpleNamespace(should_delegate=lambda *_args, **_kwargs: False)tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),fail=AsyncMock(),)queue_manager = SimpleNamespace(enqueue=AsyncMock())@@ -269,7 +269,7 @@ async def test_temp_uploaded_file_queues_external_parse_with_file_id(service._connector_delegate = SimpleNamespace(should_delegate=lambda *_args, **_kwargs: False)tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),fail=AsyncMock(),)queue_manager = SimpleNamespace(enqueue=AsyncMock())diff --git a/tests/service/test_resource_service_watch.py b/tests/service/test_resource_service_watch.pyindex 0d2813782..b7a0a3d7a 100644--- a/tests/service/test_resource_service_watch.py+++ b/tests/service/test_resource_service_watch.py@@ -1022,7 +1022,7 @@ async def test_add_resource_processor_records_paused_watch_result(task_tracker = SimpleNamespace(create=AsyncMock(return_value=SimpleNamespace(status=TaskStatus.PENDING)),start=AsyncMock(),- update_stage=AsyncMock(),+ patch_task=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),get_task_auth=AsyncMock(return_value={}),diff --git a/tests/storage/test_content_write_processing_mode.py b/tests/storage/test_content_write_processing_mode.pyindex 5dec204dd..38129aae4 100644--- a/tests/storage/test_content_write_processing_mode.py+++ b/tests/storage/test_content_write_processing_mode.py@@ -1,7 +1,6 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0-import asynciofrom types import SimpleNamespacefrom unittest.mock import AsyncMock@@ -9,39 +8,17 @@ import pytestfrom openviking.core.context import ContextLevelfrom openviking.server.identity import RequestContext, Role-from openviking.service.task_store import PersistentTaskStore-from openviking.service.task_tracker import TaskTracker, get_task_tracker-from openviking.service.task_work_index import bind_task_contextfrom openviking.session.memory.utils.memory_file_utils import MemoryFileUtilsfrom openviking.storage import content_write as content_write_modulefrom openviking.storage.abstract_overview import (render_abstract_overview,)from openviking.storage.content_write import ContentWriteCoordinator-from openviking.storage.context_update_plan import ContextUpdatePlan, FileRefreshIntent-from openviking.storage.queuefs.queue_middleware import EnqueueReceipt+from openviking.storage.context_update_plan import ContextUpdatePlanfrom openviking.storage.queuefs.semantic_ops.freshness_policy import FreshnessAction-from openviking.storage.queuefs.semantic_processor import SemanticProcessorfrom openviking.utils.ingest_options import IngestOptions-from openviking_cli.exceptions import InvalidArgumentError, NotFoundError+from openviking_cli.exceptions import InvalidArgumentErrorfrom openviking_cli.session.user_id import UserIdentifier-from tests.test_task_tracker import _FakeAgfs--[email protected](autouse=True)-def run_content_write_operations_inline(monkeypatch):- tracker = TaskTracker(PersistentTaskStore(_FakeAgfs()))- monkeypatch.setattr("openviking.service.task_tracker._instance", tracker)-- async def run_tracked_operation(_task_type, *, operation, **_kwargs):- with bind_task_context("test-task", "account-1", "user-1", enqueued_work=[]):- return await operation(), "test-task"-- monkeypatch.setattr(- content_write_module,- "run_tracked_operation",- run_tracked_operation,- )class _FakePathLock:@@ -49,17 +26,25 @@ class _FakePathLock:def __init__(self):self._lease = SimpleNamespace(id="lock-1")+ self.exact_acquire_calls = []+ self.tree_acquire_calls = []self.release_calls = []+ self.to_handoff_calls = []self.handoff_calls = []async def pathlock_acquire_exact(self, lock_path):- del lock_path+ self.exact_acquire_calls.append(lock_path)return self._lease+ async def pathlock_acquire_tree(self, lock_path):+ self.tree_acquire_calls.append(lock_path)+ raise AssertionError("Skill content writes must not acquire a package tree lock")+async def pathlock_release(self, lease):self.release_calls.append(lease.id)async def pathlock_to_handoff(self, lease):+ self.to_handoff_calls.append(lease.id)return {"owner_id": lease.id,"covered_paths": [{"path": "/fake/file", "kind": "exact"}],@@ -162,245 +147,6 @@ async def test_old_abstract_lookup_uses_viking_fs_vector_store_when_not_injected}-async def test_write_builds_ingest_options_before_scheduling_resource_refresh(ctx):- coordinator = ContentWriteCoordinator(viking_fs=_FakeVikingFS())- coordinator._safe_stat = AsyncMock(return_value={"isDir": False})- coordinator._resolve_root_uri = AsyncMock(return_value="viking://resources")- coordinator._write_direct_with_refresh = AsyncMock(- return_value={"uri": "viking://resources/demo.md"}- )-- await coordinator.write(- uri="viking://resources/demo.md",- content="updated",- ctx=ctx,- tags=["team=search"],- tag_mode="append",- )-- ingest_options = coordinator._write_direct_with_refresh.await_args.kwargs["ingest_options"]- assert ingest_options.search_tags == ["team=search"]- assert ingest_options.search_tag_mode == "append"---async def test_create_routes_as_replace_without_prelock_target_stat(ctx):- coordinator = ContentWriteCoordinator(viking_fs=_FakeVikingFS())- coordinator._safe_stat = AsyncMock(return_value={"not_found": True})- coordinator._resolve_root_uri = AsyncMock(return_value="viking://resources")- coordinator._write_direct_with_refresh = AsyncMock(- return_value={"uri": "viking://resources/new.md"}- )-- await coordinator.write(- uri="viking://resources/new.md",- content="new content",- mode="create",- ctx=ctx,- )-- kwargs = coordinator._write_direct_with_refresh.await_args.kwargs- assert kwargs["mode"] == "replace"- coordinator._safe_stat.assert_not_awaited()- coordinator._resolve_root_uri.assert_awaited_once_with(- "viking://resources/new.md",- ctx=ctx,- _allow_not_found=True,- anchor_to_parent=True,- validate_storage=False,- )---async def test_direct_write_reads_target_once_after_lock_and_reuses_formal_state(monkeypatch, ctx):- fake_fs = _FakeVikingFS()- events = []- original_acquire = fake_fs._async_agfs.pathlock_acquire_exact-- async def _acquire(path):- events.append("lock")- return await original_acquire(path)-- async def _stat(*args, **kwargs):- del args, kwargs- events.append("stat")- return {"isDir": False}-- fake_fs._async_agfs.pathlock_acquire_exact = _acquire- fake_fs.stat = _stat- coordinator = ContentWriteCoordinator(viking_fs=fake_fs)- captured = {}-- async def _snapshot(**kwargs):- captured["formal_snapshot"] = kwargs["formal_snapshot"]- captured["target_preexisting"] = kwargs["target_preexisting"]- captured["vector_inventory"] = kwargs.get("vector_inventory")- return SimpleNamespace()-- async def _plan(**kwargs):- del kwargs- return None, ContextUpdatePlan("viking://resources/demo.md", "resource")-- async def _commit(*args, **kwargs):- del args, kwargs- return SimpleNamespace(- semantic_requested=False, vector_requested=False, semantic_action=None- )-- monkeypatch.setattr(content_write_module, "build_rnfv_snapshot", _snapshot)- monkeypatch.setattr(content_write_module, "build_context_update_plan_from_snapshot", _plan)- monkeypatch.setattr(content_write_module, "commit_and_enqueue_plan", _commit)-- await coordinator._write_direct_with_refresh(- uri="viking://resources/demo.md",- root_uri="viking://resources",- content="updated",- mode="replace",- response_mode="replace",- context_type="resource",- wait=False,- ctx=ctx,- telemetry_id="",- ingest_options=IngestOptions(),- )-- assert events == ["lock", "stat"]- assert captured["target_preexisting"] is True- assert captured["formal_snapshot"] == (- {"": content_write_module.FormalEntry(is_dir=False)},- True,- )- assert captured["vector_inventory"] is None--[email protected]("write_error", [None, RuntimeError, asyncio.CancelledError])-async def test_direct_write_holds_lock_until_submitted_work_settles(monkeypatch, ctx, write_error):- uri = "viking://resources/demo.md"- fake_fs = _FakeVikingFS()- fake_fs.stat = AsyncMock(return_value={"not_found": True})- fake_fs.exists = AsyncMock(return_value=False)- coordinator = ContentWriteCoordinator(viking_fs=fake_fs)- coordinator._prepare_inline_resource_plan = AsyncMock(- return_value=SimpleNamespace(- plan=ContextUpdatePlan(uri, "resource", file_refresh=FileRefreshIntent(uri, "new-md5")),- store=None,- artifact_ref=None,- target=None,- )- )- queue = SimpleNamespace(enqueue=AsyncMock(return_value=EnqueueReceipt("queued", "work-1")))- manager = SimpleNamespace(SEMANTIC="Semantic", get_queue=lambda *args, **kwargs: queue)- monkeypatch.setattr(content_write_module, "get_queue_manager", lambda: manager)- monkeypatch.setattr(- "openviking.storage.queuefs.semantic_processor.get_viking_fs", lambda: fake_fs- )- scope = SimpleNamespace(lock={"id": "lock-1"}, close=AsyncMock())- monkeypatch.setattr(- "openviking.storage.queuefs.semantic_processor.SemanticLockScope.resolve",- AsyncMock(return_value=scope),- )- processor = SemanticProcessor(vlm_resolver=None)- processor._reenqueue_semantic_msg = AsyncMock()- tracker = get_task_tracker()- owner = {"account_id": ctx.account_id, "user_id": ctx.user.user_id}- task = await tracker.create("content_write", **owner)- for work_id, queue_name in (- ("producer", "Inline"),- ("work-1", "Semantic"),- ("embed", "Embedding"),- ):- await tracker.register_work(task.task_id, work_id, queue_name, **owner)-- async def consume():- with bind_task_context(task.task_id, **owner, work_id="work-1", enqueued_work=[]):- return await processor.on_dequeue(queue.enqueue.await_args.args[0].to_dict())-- async def commit(*args, **kwargs):- # The coordinator runs before the new file exists or the producer hands off.- assert (await consume()).outcome.value == "requeued"- scope.close.assert_not_awaited()- if write_error is not None:- raise write_error("submission interrupted")- return SimpleNamespace(- semantic_requested=True,- vector_requested=True,- semantic_action=FreshnessAction.REFRESH_NOW.value,- )-- monkeypatch.setattr(content_write_module, "commit_and_enqueue_plan", commit)- with bind_task_context(task.task_id, **owner, work_id="producer", enqueued_work=[]):- try:- result = await coordinator._write_direct_with_refresh(- uri=uri,- root_uri="viking://resources",- content="updated",- mode="replace",- response_mode="replace",- context_type="resource",- wait=False,- ctx=ctx,- telemetry_id="",- ingest_options=IngestOptions(),- )- except BaseException as exc:- assert write_error is not None and isinstance(exc, write_error)- else:- assert write_error is None- assert result["semantic_status"] == result["vector_status"] == "queued"-- assert fake_fs._async_agfs.release_calls == []- assert fake_fs._async_agfs.handoff_calls == ["lock-1"]- assert await tracker.is_enqueue_complete(task.task_id, "work-1", **owner)- await tracker.mark_work_done(task.task_id, "producer", **owner)- assert (await consume()).outcome.value == "requeued"- scope.close.assert_not_awaited()- await tracker.mark_work_done(task.task_id, "embed", **owner)- assert (await consume()).outcome.value == "success"- scope.close.assert_awaited_once()- await tracker.mark_work_done(task.task_id, "work-1", **owner)---async def test_direct_write_commits_final_bytes_when_rnfv_content_is_unchanged(monkeypatch, ctx):- """An explicit write owns F even when N already matches the indexed V md5."""- uri = "viking://resources/demo.md"- fake_fs = _FakeVikingFS()- fake_fs.stat = AsyncMock(return_value={"isDir": False})- fake_fs.write_file_bytes = AsyncMock()- coordinator = ContentWriteCoordinator(viking_fs=fake_fs)-- async def _snapshot(**kwargs):- del kwargs- return SimpleNamespace()-- async def _plan(**kwargs):- del kwargs- # This is the plan RNFV produces when N.md5 == V.md5. The explicit- # write must still commit its bytes to F because V may lag F.- return None, ContextUpdatePlan(uri, "resource")-- monkeypatch.setattr(content_write_module, "build_rnfv_snapshot", _snapshot)- monkeypatch.setattr(content_write_module, "build_context_update_plan_from_snapshot", _plan)-- await coordinator._write_direct_with_refresh(- uri=uri,- root_uri="viking://resources",- content="indexed content",- mode="replace",- response_mode="replace",- context_type="resource",- wait=False,- ctx=ctx,- telemetry_id="",- ingest_options=IngestOptions(),- )-- fake_fs.write_file_bytes.assert_awaited_once()- assert fake_fs.write_file_bytes.await_args.args == (uri, b"indexed content")--@pytest.mark.asyncioasync def test_prepare_inline_resource_plan_reuses_locked_facts_and_preloaded_inventory(monkeypatch, ctx@@ -450,51 +196,6 @@ async def test_prepare_inline_resource_plan_reuses_locked_facts_and_preloaded_inassert captured["vector_inventory"] == {"a-l2": {"id": "a-l2"}}-async def test_missing_append_starts_from_empty_content(monkeypatch, ctx):- fake_fs = _FakeVikingFS()- fake_fs.stat = AsyncMock(side_effect=NotFoundError("viking://resources/new.md", "file"))- coordinator = ContentWriteCoordinator(viking_fs=fake_fs)- captured = {}-- async def _snapshot(**kwargs):- captured["bytes"] = kwargs["store"]._data- captured["vector_inventory"] = kwargs.get("vector_inventory")- return SimpleNamespace()-- async def _plan(**kwargs):- del kwargs- return None, ContextUpdatePlan("viking://resources/new.md", "resource")-- async def _commit(*args, **kwargs):- del args, kwargs- return SimpleNamespace(- semantic_requested=False, vector_requested=False, semantic_action=None- )-- monkeypatch.setattr(content_write_module, "build_rnfv_snapshot", _snapshot)- monkeypatch.setattr(content_write_module, "build_context_update_plan_from_snapshot", _plan)- monkeypatch.setattr(content_write_module, "commit_and_enqueue_plan", _commit)-- result = await coordinator._write_direct_with_refresh(- uri="viking://resources/new.md",- root_uri="viking://resources",- content="first line",- mode="append",- response_mode="append",- context_type="resource",- wait=False,- ctx=ctx,- telemetry_id="",- ingest_options=IngestOptions(),- )-- fake_fs.read_file.assert_not_awaited()- assert captured["bytes"] == b"first line"- assert captured["vector_inventory"] == {}- assert result["mode"] == "append"--def test_write_result_keeps_content_updated_compatibility_field():coordinator = ContentWriteCoordinator(viking_fs=_FakeVikingFS())diff --git a/tests/storage/test_content_write_processor.py b/tests/storage/test_content_write_processor.pynew file mode 100644index 000000000..e8db828f3--- /dev/null+++ b/tests/storage/test_content_write_processor.py@@ -0,0 +1,70 @@+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest++from openviking.server.identity import RequestContext, Role+from openviking.service.task_store import PersistentTaskStore+from openviking.service.task_tracker import TaskTracker, set_task_tracker+from openviking.storage.queuefs.content_write_msg import ContentWriteMsg+from openviking.storage.queuefs.content_write_processor import ContentWriteProcessor+from openviking_cli.session.user_id import UserIdentifier+from tests.test_task_tracker import _FakeAgfs+++def _ctx() -> RequestContext:+ return RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)+++async def test_content_write_processor_recovers_dispatch_result_from_checkpoint():+ tracker = TaskTracker(PersistentTaskStore(_FakeAgfs()))+ set_task_tracker(tracker)+ ctx = _ctx()+ task = await tracker.create(+ "content_write",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ resource_id="viking://user/default/memories/note.md",+ )+ await tracker.start(task.task_id, account_id=ctx.account_id, user_id=ctx.user.user_id)+ await tracker.register_work(+ task.task_id,+ "root-work-1",+ "ContentWrite",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ msg = ContentWriteMsg(+ task_id=task.task_id,+ _task_work_id="root-work-1",+ operation="content_write",+ resource_id="viking://user/default/memories/note.md",+ result={"overview_status": "queued", "vector_status": "queued"},+ refresh={"kind": "memory"},+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ role="user",+ )+ processor = ContentWriteProcessor(SimpleNamespace(_async_agfs=SimpleNamespace()))+ processor._execute_refresh = AsyncMock(+ return_value={"overview_status": "complete", "vector_status": "skipped"}+ )+ data = {"data": msg.to_dict()}++ try:+ await processor._on_dequeue_bound(data)+ await processor._on_dequeue_bound(data)++ restored = await tracker.get(+ task.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ assert restored is not None+ assert restored.result is not None+ assert restored.result["overview_status"] == "complete"+ assert restored.result["vector_status"] == "skipped"+ assert processor._execute_refresh.await_count == 1+ finally:+ set_task_tracker(None)diff --git a/tests/storage/test_semantic_msg_ingest_options.py b/tests/storage/test_semantic_msg_ingest_options.pyindex c1fe702db..9e6644525 100644--- a/tests/storage/test_semantic_msg_ingest_options.py+++ b/tests/storage/test_semantic_msg_ingest_options.py@@ -73,28 +73,6 @@ def test_semantic_msg_round_trips_deferred_aggregation_flag():assert SemanticMsg.from_json(msg.to_json()).aggregate_directory is False-def test_semantic_msg_round_trips_request_completion_coordinator_flag():- msg = SemanticMsg(- uri="viking://resources/wide",- context_type="resource",- wait_for_request_completion=True,- )-- assert SemanticMsg.from_json(msg.to_json()).wait_for_request_completion is True---def test_semantic_msg_defaults_request_completion_coordinator_flag_for_legacy_payload():- msg = SemanticMsg.from_dict(- {- "uri": "viking://resources/wide",- "context_type": "resource",- "account_id": "default",- }- )-- assert msg.wait_for_request_completion is False--def test_semantic_msg_round_trips_hierarchical_aggregation_policy():msg = SemanticMsg(uri="viking://user/alice/memories",diff --git a/tests/storage/test_semantic_queue_memory_dedupe.py b/tests/storage/test_semantic_queue_memory_dedupe.pyindex 3f023bc8b..6b0775f47 100644--- a/tests/storage/test_semantic_queue_memory_dedupe.py+++ b/tests/storage/test_semantic_queue_memory_dedupe.py@@ -51,6 +51,27 @@ async def test_memory_semantic_enqueue_deduped_within_window():assert named_enqueue.call_count == 1+async def test_memory_semantic_worker_retry_bypasses_dedupe_window():+ mock_agfs = MagicMock()+ with patch.object(NamedQueue, "enqueue", new_callable=AsyncMock) as named_enqueue:+ named_enqueue.return_value = "queued-id"+ q = SemanticQueue(mock_agfs, "/queue", "semantic")+ msg = SemanticMsg(+ uri="viking://user/default/memories/entities",+ context_type="memory",+ account_id="acc",+ user_id="u1",+ peer_id="p1",+ )++ await q.enqueue(msg)+ retry = await q.enqueue(msg, deduplicate_memory=False)++ assert retry == "queued-id"+ assert named_enqueue.call_count == 2++@pytest.mark.asyncioasync def test_memory_semantic_enqueue_different_uri_not_deduped():mock_agfs = MagicMock()diff --git a/tests/test_task_tracker.py b/tests/test_task_tracker.pyindex 4dc592149..3cddb1051 100644--- a/tests/test_task_tracker.py+++ b/tests/test_task_tracker.py@@ -15,6 +15,7 @@ import pytestfrom openviking.pyagfs.exceptions import AGFSAlreadyExistsError, AGFSNotFoundErrorfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.session_service import SessionService+from openviking.service.task_domain import TaskPatchfrom openviking.service.task_store import (PersistentTaskStore,_task_from_payload,@@ -184,10 +185,10 @@ async def test_start_task(tracker: TaskTracker):assert retrieved.status == TaskStatus.RUNNING-async def test_update_stage_and_record_owner_cancelled(tracker: TaskTracker):+async def test_patch_stage_and_record_owner_cancelled(tracker: TaskTracker):task = await tracker.create("add_resource", **_owner_kwargs())await tracker.start(task.task_id, stage="queued")- await tracker.update_stage(task.task_id, "parsing")+ await tracker.patch_task(task.task_id, TaskPatch(stage="parsing"), **_owner_kwargs())retrieved = await tracker.get(task.task_id)assert retrieved is not Noneassert retrieved.status == TaskStatus.RUNNING@@ -200,6 +201,47 @@ async def test_update_stage_and_record_owner_cancelled(tracker: TaskTracker):assert cancelled.stage == "cancelled"+async def test_patch_task_updates_fields_and_dispatch_marker_atomically(tracker: TaskTracker):+ task = await tracker.create("content_write", **_owner_kwargs())+ await tracker.start(task.task_id)++ patch = await tracker.patch_task(+ task.task_id,+ TaskPatch(+ result={"semantic_status": "deferred", "vector_status": "queued"},+ stage="dispatched",+ meta={"request": {"attempt": 1}},+ auth={"source": "content_write"},+ add_enqueue_complete_work_ids=frozenset({"root-work-1"}),+ ),+ **_owner_kwargs(),+ )++ assert patch.is_applied+ stored = await tracker.get(task.task_id, **_owner_kwargs())+ assert stored is not None+ assert stored.status == TaskStatus.RUNNING+ assert stored.result == {"semantic_status": "deferred", "vector_status": "queued"}+ assert stored.stage == "dispatched"+ assert stored.meta == {"request": {"attempt": 1}}+ assert await tracker.get_task_auth(task.task_id, **_owner_kwargs()) == {+ "source": "content_write",+ "enqueue_complete_work_ids": ["root-work-1"],+ }++ cleared = await tracker.patch_task(+ task.task_id,+ TaskPatch(clear_fields=frozenset({"result", "stage"})),+ **_owner_kwargs(),+ )++ assert cleared.is_applied+ stored = await tracker.get(task.task_id, **_owner_kwargs())+ assert stored is not None+ assert stored.result is None+ assert stored.stage is None++async def test_complete_task(tracker: TaskTracker):task = await tracker.create("session_commit", resource_id="s1", **_owner_kwargs())await tracker.start(task.task_id)@@ -465,9 +507,9 @@ async def test_to_dict(tracker: TaskTracker):"provider": "git_http_basic","password": "secret",}- await tracker.update_task_auth(+ await tracker.patch_task(task.task_id,- {"external_task_id": "session-1"},+ TaskPatch(auth={"external_task_id": "session-1"}),**_owner_kwargs(),)assert await tracker.get_task_auth(task.task_id, **_owner_kwargs()) == {@@ -1191,7 +1233,11 @@ async def test_session_service_get_commit_task_also_filters_account():async def test_feishu_response_checkpoint_survives_reload(tracker):task = await tracker.create("add_resource", **_owner_kwargs())- await tracker.record_feishu_response(task.task_id, "nested/doc", "response-1", "acme", "alice")+ await tracker.patch_task(+ task.task_id,+ TaskPatch(meta={"feishu_responses": {"nested/doc": "response-1"}}),+ **_owner_kwargs(),+ )restored = TaskTracker(store=tracker._store)record = await restored.get(task.task_id, **_owner_kwargs())assert record.meta["feishu_responses"] == {"nested/doc": "response-1"}@@ -1268,7 +1314,11 @@ async def test_event_history_is_bounded_and_reports_truncation(tracker, stage_prtask = await tracker.create("add_resource", **_owner_kwargs())for index in range(80):- await tracker.update_stage(task.task_id, f"{index}:{stage_prefix}", **_owner_kwargs())+ await tracker.patch_task(+ task.task_id,+ TaskPatch(stage=f"{index}:{stage_prefix}"),+ **_owner_kwargs(),+ )await tracker.complete(task.task_id, {"done": True}, **_owner_kwargs())history = (await tracker.get_with_events(task.task_id, **_owner_kwargs())).execution_eventsassert len(history["items"]) <= MAX_TASK_EVENTS@@ -1316,8 +1366,8 @@ async def test_processing_time_persists_and_restored_active_tasks_are_unknown(moawait tracker.register_running_task(task.task_id, **_owner_kwargs())now[0] = 13assert (await tracker.get(task.task_id)).processing_seconds == 3- # A stage update neither resets nor stops the processing clock.- await tracker.update_stage(task.task_id, "semantic", **_owner_kwargs())+ # A stage patch neither resets nor stops the processing clock.+ await tracker.patch_task(task.task_id, TaskPatch(stage="semantic"), **_owner_kwargs())restarted = TaskTracker(store=PersistentTaskStore(agfs))assert (await restarted.get(task.task_id, **_owner_kwargs())).processing_seconds is None# Waiting, I/O, and backoff after a worker claims the task remain part of