Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(tasks): preserve task owner in queue work (#6122)
runyunzhou1241f0c6b9e1
diff --git a/openviking/service/deletion.py b/openviking/service/deletion.pyindex 06c525d05..6a86b73d2 100644--- a/openviking/service/deletion.py+++ b/openviking/service/deletion.py@@ -16,7 +16,7 @@ from openviking.server.identity import RequestContext, Rolefrom openviking.service.task_store import SYSTEM_TASK_ACCOUNT_ID, SYSTEM_TASK_USER_IDfrom openviking.service.task_tracker import TaskStatus, get_task_trackerfrom openviking.service.task_tracker_concurrency import OwnerLoopDispatcher, run_to_completion-from openviking.service.task_work_index import extract_task_metadata+from openviking.service.task_work_index import bind_task_context, extract_task_metadatafrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResultfrom openviking.storage.viking_fs import LS_ALL_NODES@@ -115,15 +115,16 @@ class DeletionService:if task.status in (TaskStatus.PENDING, TaskStatus.RUNNING) and task_id not in (queued_task_ids):- await queue.enqueue(- _deletion_message(- task_id=task_id,- owner_account_id=owner_account_id,- owner_user_id=owner_user_id,- target_account_id=account_id,- target_user_id=user_id,+ with bind_task_context(task_id, owner_account_id, owner_user_id):+ await queue.enqueue(+ _deletion_message(+ task_id=task_id,+ owner_account_id=owner_account_id,+ owner_user_id=owner_user_id,+ target_account_id=account_id,+ target_user_id=user_id,+ ))- )queued_task_ids.add(task_id)async def _is_deleting(self, account_id: str, user_id: str | None = None) -> bool:@@ -261,15 +262,16 @@ class DeletionService:metadata = extract_task_metadata(message)if metadata is not None and metadata.task_id == task_id:return- await queue.enqueue(- _deletion_message(- task_id=task_id,- owner_account_id=owner_account_id,- owner_user_id=owner_user_id,- target_account_id=target_account_id,- target_user_id=target_user_id,+ with bind_task_context(task_id, owner_account_id, owner_user_id):+ await queue.enqueue(+ _deletion_message(+ task_id=task_id,+ owner_account_id=owner_account_id,+ owner_user_id=owner_user_id,+ target_account_id=target_account_id,+ target_user_id=target_user_id,+ ))- )async def _process(self, message: dict[str, Any]) -> Optional[str]:task_id = message["task_id"]diff --git a/openviking/service/external_task_service.py b/openviking/service/external_task_service.pyindex d9c8bd26c..c913ee861 100644--- a/openviking/service/external_task_service.py+++ b/openviking/service/external_task_service.py@@ -18,6 +18,7 @@ from openviking.service.task_tracker_concurrency import (OwnerLoopDispatcher,run_to_completion,)+from openviking.service.task_work_index import bind_task_contextfrom openviking.storage.queuefs import QueueManager, get_queue_managerfrom openviking_cli.exceptions import ConflictErrorfrom openviking_cli.utils.logger import get_logger@@ -231,14 +232,15 @@ class ExternalTaskService:account_id=ctx.account_id,user_id=ctx.user.user_id,)- await get_queue_manager().enqueue(- QueueManager.EXTERNAL_TASK,- {- "task_id": task.task_id,- "account_id": ctx.account_id,- "user_id": ctx.user.user_id,- },- )+ with bind_task_context(task.task_id, ctx.account_id, ctx.user.user_id):+ await get_queue_manager().enqueue(+ QueueManager.EXTERNAL_TASK,+ {+ "task_id": task.task_id,+ "account_id": ctx.account_id,+ "user_id": ctx.user.user_id,+ },+ )enqueued = Truecurrent = await tracker.get(task.task_id,@@ -290,14 +292,15 @@ class ExternalTaskService:async def _resume_submission(self, task: TaskRecord, ctx: RequestContext) -> None:if task.status == TaskStatus.PENDING:- await get_queue_manager().enqueue(- QueueManager.EXTERNAL_TASK,- {- "task_id": task.task_id,- "account_id": ctx.account_id,- "user_id": ctx.user.user_id,- },- )+ with bind_task_context(task.task_id, ctx.account_id, ctx.user.user_id):+ await get_queue_manager().enqueue(+ QueueManager.EXTERNAL_TASK,+ {+ "task_id": task.task_id,+ "account_id": ctx.account_id,+ "user_id": ctx.user.user_id,+ },+ )async def execute(self, task_id: str, account_id: str, user_id: str) -> bool:"""Return False when this delivery must rotate behind other target work."""diff --git a/openviking/service/reindex_executor.py b/openviking/service/reindex_executor.pyindex f44ff7b5d..92043660f 100644--- a/openviking/service/reindex_executor.py+++ b/openviking/service/reindex_executor.py@@ -27,7 +27,7 @@ from openviking.core.namespace import (from openviking.server.dependencies import get_servicefrom openviking.server.identity import RequestContextfrom openviking.service.task_tracker import get_task_tracker, run_tracked_operation-from openviking.service.task_work_index import get_task_context+from 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_bodyfrom openviking.storage.queuefs.embedding_msg_converter import EmbeddingMsgConverter@@ -303,7 +303,8 @@ class ReindexExecutor:lock_handoff=handoff,)queue_manager = get_queue_manager()- await queue_manager.enqueue(queue_manager.REINDEX, msg.to_dict())+ with bind_task_context(task.task_id, task.account_id, task.user_id):+ await queue_manager.enqueue(queue_manager.REINDEX, msg.to_dict())enqueued = Trueawait service.viking_fs._async_agfs.pathlock_handoff(lease)lease = Nonediff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.pyindex 7d72cf23e..b8f08bc71 100644--- a/openviking/service/resource_service.py+++ b/openviking/service/resource_service.py@@ -695,6 +695,7 @@ class ResourceService:) -> Any:"""Persist a job and fully own the passed lock until handoff or release completes."""from openviking.service.task_tracker import get_task_tracker+ from openviking.service.task_work_index import bind_task_contextfrom openviking.storage.queuefs import get_queue_managertracker = get_task_tracker()@@ -712,7 +713,10 @@ class ResourceService:),auth=task_auth,)- await get_queue_manager().enqueue(queue_name, msg.to_dict())+ 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 = Trueif on_enqueued is not None:on_enqueued()diff --git a/openviking/service/task_work_index.py b/openviking/service/task_work_index.pyindex dfb6ff93d..da65f42fb 100644--- a/openviking/service/task_work_index.py+++ b/openviking/service/task_work_index.py@@ -20,6 +20,8 @@ from typing import Any, Awaitable, Callable, Dict, Generator, Iterable, Mapping,from uuid import uuid4TASK_WORK_ID_FIELD = "_task_work_id"+TASK_ACCOUNT_ID_FIELD = "_task_account_id"+TASK_USER_ID_FIELD = "_task_user_id"_TERMINAL_WORK_STATUSES = frozenset({"completed", "failed", "cancelled"})@@ -155,6 +157,11 @@ def _payload_dict(message: Any) -> Optional[Dict[str, Any]]:def _owner_from_payload(payload: Mapping[str, Any]) -> tuple[str, str]:+ task_account_id = payload.get(TASK_ACCOUNT_ID_FIELD)+ task_user_id = payload.get(TASK_USER_ID_FIELD)+ if task_account_id and task_user_id:+ return str(task_account_id), str(task_user_id)+account_id = payload.get("account_id")user_id = payload.get("user_id")@@ -187,14 +194,23 @@ def prepare_task_payload(return payload, Nonetask_id = str(task_id)- account_id, user_id = _owner_from_payload(payload)- if current is not None and current.task_id == task_id:- account_id = account_id or current.account_id- user_id = user_id or current.user_id+ has_authoritative_context = (+ current is not None+ and current.task_id == task_id+ and current.account_id+ and current.user_id+ )+ if has_authoritative_context:+ account_id, user_id = current.account_id, current.user_id+ else:+ account_id, user_id = "", ""work_id = str(payload.get(TASK_WORK_ID_FIELD) or uuid4())payload["task_id"] = task_idpayload[TASK_WORK_ID_FIELD] = work_id+ if has_authoritative_context:+ payload[TASK_ACCOUNT_ID_FIELD] = account_id+ payload[TASK_USER_ID_FIELD] = user_idif account_id and user_id:payload.setdefault("account_id", account_id)payload.setdefault("user_id", user_id)@@ -216,6 +232,39 @@ def extract_task_metadata(message: Any) -> Optional[QueueTaskMetadata]:return QueueTaskMetadata(str(task_id), str(work_id), account_id, user_id)+@contextmanager+def bind_task_context_from_message(message: Any) -> Generator[None, None, None]:+ """Bind TaskContext for direct queue handler calls that bypass middleware."""+ if _current_task_context.get() is not None:+ yield+ return++ metadata = extract_task_metadata(message)+ if metadata is None:+ payload = _payload_dict(message)+ if payload is None or not payload.get("task_id"):+ yield+ return+ account_id, user_id = _owner_from_payload(payload)+ metadata = QueueTaskMetadata(+ str(payload["task_id"]),+ str(payload.get(TASK_WORK_ID_FIELD) or ""),+ account_id,+ user_id,+ )+ if not metadata.account_id or not metadata.user_id:+ yield+ return++ with bind_task_context(+ metadata.task_id,+ metadata.account_id,+ metadata.user_id,+ work_id=metadata.work_id or None,+ ):+ yield++class TaskWorkIndex:"""Thread-safe, rebuildable index of logical Work and active handlers."""diff --git a/openviking/storage/queuefs/add_resource_processor.py b/openviking/storage/queuefs/add_resource_processor.pyindex f7b4ec865..e0dd26266 100644--- a/openviking/storage/queuefs/add_resource_processor.py+++ b/openviking/storage/queuefs/add_resource_processor.py@@ -12,7 +12,7 @@ from openviking.observability.context import bind_execution_contextfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.task_tracker import TaskStatus, get_task_trackerfrom openviking.service.task_work_index import (- bind_task_context,+ bind_task_context_from_message,extract_task_metadata,)from openviking.storage.queuefs.add_resource_msg import AddResourceMsg@@ -140,6 +140,10 @@ class AddResourceProcessor(DequeueHandlerBase):return Trueasync def _process(self, msg: AddResourceMsg, data: Dict[str, Any]) -> ProcessResult:+ with bind_task_context_from_message(data):+ return await self._process_bound(msg, data)++ async def _process_bound(self, msg: AddResourceMsg, data: Dict[str, Any]) -> ProcessResult:telemetry_id = msg.telemetry_id or ""ctx = RequestContext(user=UserIdentifier(msg.account_id, msg.user_id),@@ -225,11 +229,7 @@ class AddResourceProcessor(DequeueHandlerBase):user_id=ctx.user.user_id,)- with (- bind_execution_context(),- bind_telemetry(telemetry),- bind_task_context(msg.task_id, ctx.account_id, ctx.user.user_id),- ):+ with bind_execution_context(), bind_telemetry(telemetry):terminal = Falsetry:queue_message_id = str(data.get("id") or "")diff --git a/openviking/storage/queuefs/reindex_processor.py b/openviking/storage/queuefs/reindex_processor.pyindex 229a1779b..192c53a51 100644--- a/openviking/storage/queuefs/reindex_processor.py+++ b/openviking/storage/queuefs/reindex_processor.py@@ -9,7 +9,7 @@ from typing import Any, Dict, Optionalfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.task_tracker import TaskStatus, get_task_tracker-from openviking.service.task_work_index import bind_task_context, extract_task_metadata+from openviking.service.task_work_index import bind_task_context_from_message, extract_task_metadatafrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResultfrom openviking.storage.queuefs.reindex_msg import ReindexMsg@@ -75,6 +75,10 @@ class ReindexProcessor(DequeueHandlerBase):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)@@ -129,10 +133,7 @@ class ReindexProcessor(DequeueHandlerBase):vlm_resolver=service._vlm_resolver,vector_config_resolver=service._vector_config_resolver,)- with (- bind_telemetry(telemetry),- bind_task_context(msg.task_id, ctx.account_id, ctx.user.user_id),- ):+ with bind_telemetry(telemetry):# _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.diff --git a/openviking/storage/queuefs/session_commit_processor.py b/openviking/storage/queuefs/session_commit_processor.pyindex 9eb14088b..5c06e74bc 100644--- a/openviking/storage/queuefs/session_commit_processor.py+++ b/openviking/storage/queuefs/session_commit_processor.py@@ -11,7 +11,7 @@ from openviking.observability.context import ()from openviking.server.identity import RequestContext, Rolefrom openviking.service.task_tracker import get_task_tracker-from openviking.service.task_work_index import bind_task_context+from openviking.service.task_work_index import bind_task_context_from_messagefrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResultfrom openviking.storage.queuefs.session_commit_msg import SessionCommitMsg@@ -46,6 +46,10 @@ class SessionCommitProcessor(DequeueHandlerBase):# token events are attributed to the committing account/user rather than# "__unknown__" (mirrors SemanticProcessor.on_dequeue). Restore the# worker's previous context when processing finishes.+ with bind_task_context_from_message(msg.to_dict()):+ return await self._process_bound(msg, ctx)++ async def _process_bound(self, msg: SessionCommitMsg, ctx: RequestContext) -> bool:root_attrs = create_root_span_attributes(http_method="QUEUE",http_route="/queuefs/session_commit",@@ -79,8 +83,7 @@ class SessionCommitProcessor(DequeueHandlerBase):)return Trueawait session.load()- with bind_task_context(msg.task_id, ctx.account_id, ctx.user.user_id):- processed = await session.resume_queued_commit(msg)+ processed = await session.resume_queued_commit(msg)if not processed:from openviking.storage.queuefs import QueueManager, get_queue_manager@@ -121,5 +124,6 @@ class SessionCommitProcessor(DequeueHandlerBase):msg, ctx = self._parse_message(data)except (json.JSONDecodeError, KeyError, TypeError, ValueError) as exc:return ProcessResult.failed(str(exc))- processed = await self._process(msg, ctx)+ with bind_task_context_from_message(data):+ processed = await self._process(msg, ctx)return ProcessResult.success() if processed else ProcessResult.requeued()diff --git a/tests/service/test_task_queue_middleware.py b/tests/service/test_task_queue_middleware.pyindex f6c54db28..aadc54583 100644--- a/tests/service/test_task_queue_middleware.py+++ b/tests/service/test_task_queue_middleware.py@@ -107,6 +107,54 @@ async def test_enqueue_registers_before_write_and_ack_finalizes_before_delete(trassert not index.has_work("task-1")+async def test_enqueue_keeps_content_owner_separate_from_task_owner(tracked_queue):+ queue, _, transport, _, _ = tracked_queue+ payload = {+ "task_id": "task-1",+ "account_id": "account",+ "user_id": "content-owner",+ "task_account_id": "business-account",+ "task_user_id": "business-user",+ "payload": {+ "context_data": {+ "account_id": "account",+ "owner_user_id": "content-owner",+ }+ },+ }++ with bind_task_context("task-1", "account", "task-owner"):+ await queue.enqueue(payload)++ persisted = json.loads(transport.write.await_args.args[1])+ assert persisted["user_id"] == "content-owner"+ assert persisted["task_account_id"] == "business-account"+ assert persisted["task_user_id"] == "business-user"+ assert persisted["payload"]["context_data"]["owner_user_id"] == "content-owner"+ assert persisted["_task_account_id"] == "account"+ assert persisted["_task_user_id"] == "task-owner"+ assert extract_task_metadata(persisted).user_id == "task-owner"+++async def test_enqueue_does_not_stamp_task_owner_without_authoritative_context(tracked_queue):+ queue, _, transport, _, _ = tracked_queue++ with bind_task_context("task-1", "", ""):+ await queue.enqueue(+ {+ "task_id": "task-1",+ "account_id": "account",+ "user_id": "content-owner",+ }+ )++ persisted = json.loads(transport.write.await_args.args[1])+ assert persisted["account_id"] == "account"+ assert persisted["user_id"] == "content-owner"+ assert "_task_account_id" not in persisted+ assert "_task_user_id" not in persisted++async def test_enqueue_failure_records_failed_work(tracked_queue):queue, index, transport, finalize, _ = tracked_queuetransport.write.side_effect = OSError("write failed")diff --git a/tests/storage/test_queue_manager.py b/tests/storage/test_queue_manager.pyindex b691c8fe5..10f70eed9 100644--- a/tests/storage/test_queue_manager.py+++ b/tests/storage/test_queue_manager.py@@ -358,3 +358,32 @@ async def test_bootstrap_restores_legacy_messages_into_middleware_index(transporawait queue.ack("legacy-message", message)assert not attached[0].has_work("task")transport.write.assert_awaited_once_with("/queue/Test/ack", b"legacy-message")+++async def test_bootstrap_restores_task_owner_from_cross_user_child_work(transport):+ manager = QueueManager(object())+ manager.get_queue("Test", allow_create=True)+ message = {+ "id": "cross-user-child",+ "data": json.dumps(+ {+ "task_id": "task",+ "_task_work_id": "work",+ "account_id": "account",+ "user_id": "content-owner",+ "_task_account_id": "account",+ "_task_user_id": "task-owner",+ }+ ),+ }+ transport.read.return_value = json.dumps([message]).encode()++ class Tracker:+ def attach_work_index(self, index):+ assert index.has_work("task")++ async def restore_work_tasks(self, owners):+ assert owners == {"task": ("account", "task-owner")}+ return ["restored"]++ assert await manager.prepare_task_tracking(Tracker()) == ["restored"]