Skip to content

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

fix(storage): preserve staged source on worker shutdown (#5888)

z1gon5ece19654c49
diff --git a/openviking/storage/queuefs/add_resource_processor.py b/openviking/storage/queuefs/add_resource_processor.py
index a9abebe8b..f763b967e 100644
--- a/openviking/storage/queuefs/add_resource_processor.py
+++ b/openviking/storage/queuefs/add_resource_processor.py
@@ -354,17 +354,28 @@ class AddResourceProcessor(DequeueHandlerBase):
terminal = True
return ProcessResult.success()
except asyncio.CancelledError:
- logger.warning(
- "[AddResourceCancelled] %s root=%s",
- log_correlation(
- task_id=msg.task_id,
- telemetry_id=telemetry_id,
- message_id=queue_message_id,
- ),
- msg.root_uri,
- )
- await self._record_watch_execution(msg, "cancelled")
- terminal = True
+ latest_task = None
+ with suppress(Exception):
+ latest_task = await tracker.get(
+ msg.task_id,
+ account_id=ctx.account_id,
+ user_id=ctx.user.user_id,
+ )
+ if latest_task is not None and latest_task.status in (
+ TaskStatus.CANCELLING,
+ TaskStatus.CANCELLED,
+ ):
+ logger.warning(
+ "[AddResourceCancelled] %s root=%s",
+ log_correlation(
+ task_id=msg.task_id,
+ telemetry_id=telemetry_id,
+ message_id=queue_message_id,
+ ),
+ msg.root_uri,
+ )
+ await self._record_watch_execution(msg, "cancelled")
+ terminal = True
raise
except Exception as exc:
logger.exception(
diff --git a/tests/parse/test_feishu_parser_api.py b/tests/parse/test_feishu_parser_api.py
index e6eb72ba8..e6029179b 100644
--- a/tests/parse/test_feishu_parser_api.py
+++ b/tests/parse/test_feishu_parser_api.py
@@ -102,6 +102,66 @@ async def test_add_resource_processor_cancelled_context_preserves_group_ids(monk
viking_fs._async_agfs.pathlock_release.assert_awaited_once_with(lock)
+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(),
+ get_task_auth=AsyncMock(return_value={}),
+ get=AsyncMock(return_value=SimpleNamespace(status=task_status)),
+ )
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.add_resource_processor.get_task_tracker",
+ Mock(return_value=task_tracker),
+ )
+ service = SimpleNamespace(
+ execute_add_resource_job=AsyncMock(side_effect=asyncio.CancelledError),
+ _link_resource_reason_memory=AsyncMock(),
+ )
+ viking_fs = SimpleNamespace(
+ _async_agfs=SimpleNamespace(pathlock_release=AsyncMock()),
+ delete_temp=AsyncMock(),
+ )
+ processor = AddResourceProcessor(service, QueueManager.ADD_RESOURCE, viking_fs)
+ msg = AddResourceMsg(
+ task_id="task-interrupted",
+ path="/tmp/report.md",
+ source_path="/tmp/report.md",
+ root_uri="viking://resources/report",
+ account_id="account-1",
+ user_id="user-1",
+ role="user",
+ staged_source={
+ "temp_uri": "viking://temp/account-1/task-interrupted",
+ "source_uri": "viking://temp/account-1/task-interrupted/source/report.md",
+ "source_type": "local",
+ "original_source": "/tmp/report.md",
+ "meta": {},
+ },
+ )
+ data = msg.to_dict()
+ data[TASK_WORK_ID_FIELD] = "work-1"
+
+ with pytest.raises(asyncio.CancelledError):
+ await processor._process(msg, data)
+
+ return viking_fs.delete_temp
+
+
+async def test_add_resource_worker_shutdown_preserves_staged_source_for_replay(monkeypatch):
+ delete_temp = await _interrupt_staged_add_resource(monkeypatch, TaskStatus.RUNNING)
+
+ delete_temp.assert_not_awaited()
+
+
+async def test_add_resource_user_cancellation_cleans_staged_source(monkeypatch):
+ delete_temp = await _interrupt_staged_add_resource(monkeypatch, TaskStatus.CANCELLING)
+
+ delete_temp.assert_awaited_once()
+
+
@pytest.mark.asyncio
async def test_feishu_parser_api_bypasses_accessor():
result = object()