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(admin): 异步清理账号数据 (#5015)
Qin Haojie2a38736eb2f9
diff --git a/crates/ov_cli/src/config_wizard/wizard.rs b/crates/ov_cli/src/config_wizard/wizard.rsindex 0259f2094..f98e318b2 100644--- a/crates/ov_cli/src/config_wizard/wizard.rs+++ b/crates/ov_cli/src/config_wizard/wizard.rs@@ -3682,11 +3682,15 @@ async fn run_account_management_menu("正在删除账户...",),))?;- delete_root_account(&client, &account.account_id).await?;+ let task = delete_root_account(&client, &account.account_id).await?;if clear_config_account_selection(config, root_api_key, &account.account_id) {save_user_management_local_config(store, config_name.as_deref(), config)?;}- *account_list_notice = Some(deleted_account_notice(&account.account_id));+ let task_id = task["task_id"].as_str().ok_or_else(|| {+ Error::Parse("Account deletion response has no task_id".to_string())+ })?;+ *account_list_notice =+ Some(account_deletion_notice(&account.account_id, task_id));return Ok(false);}PromptResult::Value(false) | PromptResult::Back => continue,@@ -4099,10 +4103,10 @@ fn user_management_save_target(config_name: &str) -> String {}}-fn deleted_account_notice(account_id: &str) -> String {+fn account_deletion_notice(account_id: &str, task_id: &str) -> String {match Language::current() {- Language::En => format!("Deleted account {account_id}/."),- Language::ZhCn => format!("已删除账户 {account_id}/。"),+ Language::En => format!("Account {account_id}/ disabled. Cleanup task: {task_id}"),+ Language::ZhCn => format!("账户 {account_id}/ 已停用,后台清理任务:{task_id}"),}}@@ -4651,10 +4655,9 @@ async fn create_root_user(client: &BaseClient, account_id: &str, user_id: &str).await}-async fn delete_root_account(client: &BaseClient, account_id: &str) -> Result<()> {+async fn delete_root_account(client: &BaseClient, account_id: &str) -> Result<Value> {let path = format!("/api/v1/admin/accounts/{account_id}");- let _: Value = client.delete(&path, &[]).await?;- Ok(())+ client.delete(&path, &[]).await}async fn delete_root_user(client: &BaseClient, account_id: &str, user_id: &str) -> Result<()> {@@ -4679,7 +4682,9 @@ fn root_accounts_from_value(value: &Value) -> Vec<RootAccountSummary> {.flat_map(|items| items.iter()).filter_map(|item| {let account_id = item.get("account_id")?.as_str()?.trim();- if account_id.is_empty() {+ if account_id.is_empty()+ || item.get("status").and_then(Value::as_str) == Some("deleting")+ {return None;}Some(RootAccountSummary {@@ -8113,6 +8118,7 @@ mod tests {let accounts = root_accounts_from_value(&json!([{"account_id": "beta", "user_count": 0},{"account_id": "", "user_count": 9},+ {"account_id": "deleting-account", "user_count": 2, "status": "deleting"},{"account_id": "alpha", "user_count": 2}]));assert_eq!(diff --git a/docs/design/mcp-oauth2-1.md b/docs/design/mcp-oauth2-1.mdindex e4a65b6d9..4371dba5e 100644--- a/docs/design/mcp-oauth2-1.md+++ b/docs/design/mcp-oauth2-1.md@@ -110,7 +110,7 @@OAuth token = API Key 等效,能调任何当前用户身份能调的 REST 端点(不仅 `/mcp`)。- **不是权限放大**:opaque token 都钉死 `(account_id, user_id, role)`-- **撤销粒度**:以 `(account, user)` 为单位 — 删除某 user 的 API Key 时一刀切撤销该 user 名下所有 OAuth token,见 `OAuthStore.revoke_user_tokens()`+- **撤销粒度**:以 `(account, user)` 为单位 — 删除某 user 的 API Key 时一刀切撤销该 user 名下所有 OAuth token,见 `OAuthStore.revoke_tokens()`- Phase 2 计划引入 OAuth scope 做更细收紧### 3. Token 形态(全部 opaque)@@ -281,7 +281,7 @@ server {### ✅ M1 — 基础设施- `OAuthConfig` 接入 `OpenVikingConfig`(默认 disabled)-- `OAuthStore` 5 张表 + CRUD + 原子一次性消费 + revoke_user_tokens+- `OAuthStore` 5 张表 + CRUD + 原子一次性消费 + revoke_tokens- `oauth/otp.py` OTP 生成- `app.py` lifespan 注入 store + provider + GC@@ -395,7 +395,7 @@ curl -X POST -H "Authorization: Bearer ovat_..." \| 反代后 `issuer` 派生错(HTTPS 终结于代理) | `OPENVIKING_PUBLIC_BASE_URL` env 或 `oauth.issuer` 配置;非 localhost 部署强烈建议显式设 || 同源 quick-authorize 是隐式确认 | 即使检测到 sessionStorage,**仍需点击 "Authorize" 按钮**才生效,不会一步跳转 || display_code 暴力枚举 | 6 字符 × 32 字母表 = ~1B 组合;TTL 10min;pending 一次性消费;建议在反代层加每 IP 速率限制 |-| Refresh token 重放 | 实现:检测重放→`store.revoke_user_tokens(account, user)` 一并撤销该 user 名下所有 OAuth state |+| Refresh token 重放 | 实现:检测重放→`store.revoke_tokens(account, user)` 一并撤销该 user 名下所有 OAuth state || Token 权限范围 = 整个 REST API | 已与用户确认 Phase 1 不限制;Phase 2 引入 scope 机制收紧 |-| API Key → 撤销 OAuth token 的精度 | 当前粒度 `(account, user)`:删 user 时调 `revoke_user_tokens` cascade;满足需求 |+| API Key → 撤销 OAuth token 的精度 | 当前粒度 `(account, user)`:删 user 时调 `revoke_tokens` cascade;满足需求 || Console 与 OAuth page 同源依赖反代 | 文档提供 nginx 模板;不反代时退化为"console 复制 OTP,page 输入"流程仍可用 |diff --git a/docs/en/api/08-admin.md b/docs/en/api/08-admin.mdindex c6db772bb..a05e2e484 100644--- a/docs/en/api/08-admin.md+++ b/docs/en/api/08-admin.md@@ -715,17 +715,18 @@ ov --sudo admin list-accounts --limit 50 --page 2#### 1. API Implementation Overview-Delete a workspace and all associated users and data (ROOT only).+Asynchronously delete a workspace and all associated users and data (ROOT only). The endpoint returns HTTP `202` and a `task_id` without waiting for data cleanup.**Processing Flow:**-1. Verify requester has ROOT privileges-2. Cascade delete all AGFS data for the account (`user/` and `resources/`; sessions live under `user/`)-3. Cascade delete all vector DB records for the account-4. Finally delete account metadata and all user keys+1. Verify ROOT privileges and persist the account deletion fence, immediately rejecting its keys and ordinary requests; persist a system-owned `account_delete` Task and queued work, then return `status=deleting` and `task_id`+2. Stop account watches and business tasks, then remove vectors, OAuth grants, usage/audit data, and the entire account AGFS directory, including account-owned task records+3. Remove the account registry entry and complete the Task only after cleanup succeeds++Account and user cleanup share one queue with a single consumer. Account tasks clean the entire account directly. Later user cleanup tasks complete without further cleanup if their target was deleted. Old tasks also skip accounts or users recreated with the same IDs. Account-owned task records are deleted with the account and are not recreated by late deliveries; system-owned cleanup Tasks remain queryable.**Code Entry Points:**- `openviking/server/routers/admin.py:delete_account` - HTTP route-- `openviking/server/api_keys/new.py:APIKeyManager.delete_account` - Core implementation+- `openviking/service/deletion.py:DeletionService.delete` - Core implementation- `openviking_cli/client/sync_http.py:SyncHTTPClient.admin_delete_account` - Python SDK#### 2. Interface and Parameters@@ -738,7 +739,13 @@ Delete a workspace and all associated users and data (ROOT only).**Notes:**- Delete operation is irreversible and cascades to all account data-- If some data fails to delete, warnings are logged and deletion continues+- Cleanup failure marks the Task as `failed` and records the error; the account remains `deleting`+- Repeated requests during cleanup return the same Task; requesting deletion after failure creates a retry Task for remaining data+- Unfinished work is recovered on restart; the account cannot be recreated or re-enabled during cleanup+- Vector IDs are enumerated by account before deletion is submitted in batches of at most 100 records, without the former 100,000-record total ceiling+- Vector deletion succeeds when the delete requests succeed; it does not require an immediate zero count or empty read-back. Remote indexes may briefly return stale data even after the Task completes+- Account listings expose `status=active|deleting` and the cleanup `task_id` when deleting+- Query `GET /api/v1/tasks/{task_id}` as ROOT for status and errors; cleanup uses only `pending`, `running`, `completed`, and `failed`, without separate cleanup stages. Only `completed` confirms cleanup finished#### 3. Usage Examples@@ -762,7 +769,7 @@ client = ov.SyncHTTPClient(api_key="<root-key>")client.initialize()result = client.admin_delete_account(account_id="acme")-print(f"Account deleted: {result['deleted']}")+print(f"Cleanup task: {result['task_id']}")```**TypeScript SDK**@@ -778,7 +785,7 @@ result, err := client.AdminDeleteAccount(ctx, "acme")if err != nil {return err}-fmt.Println(result["deleted"])+fmt.Println(result["task_id"])```**CLI**@@ -786,6 +793,7 @@ fmt.Println(result["deleted"])```bash# Requires ROOT privileges, use --sudoov --sudo admin delete-account acme+ov --sudo task status <task_id>```**Response Example**@@ -794,7 +802,9 @@ ov --sudo admin delete-account acme{"status": "ok","result": {- "deleted": true+ "account_id": "acme",+ "status": "deleting",+ "task_id": "550e8400-e29b-41d4-a716-446655440000"},"time": 0.1}@@ -1070,7 +1080,7 @@ Remove a user from a workspace. The user's API key is revoked immediately, and o**Code Entry Points:**- `openviking/server/routers/admin.py:remove_user` - HTTP route-- `openviking/service/user_deletion.py:UserDeletionService.delete_user` - Core implementation+- `openviking/service/deletion.py:DeletionService.delete` - Core implementation- `openviking_cli/client/sync_http.py:SyncHTTPClient.admin_remove_user` - Python SDK#### 2. Interface and Parameters@@ -1086,6 +1096,7 @@ Remove a user from a workspace. The user's API key is revoked immediately, and o- ADMIN can only remove users in their own account- Cannot delete the last admin user of an account- After deletion starts, the user key is invalid and list_users omits the user+- Vector deletion succeeds when the delete requests succeed, without waiting for remote index synchronization; counts and queries may briefly lag after the Task completes#### 3. Usage Examplesdiff --git a/docs/zh/api/08-admin.md b/docs/zh/api/08-admin.mdindex 954bfe88e..07a09d669 100644--- a/docs/zh/api/08-admin.md+++ b/docs/zh/api/08-admin.md@@ -659,17 +659,18 @@ ov --sudo admin list-accounts --limit 50 --page 2#### 1. API 实现介绍-删除工作区及其所有关联用户和数据(仅 ROOT)。+异步删除工作区及其所有关联用户和数据(仅 ROOT)。接口返回 HTTP `202` 和 `task_id`,不等待数据清理完成。**处理流程:**-1. 验证请求者具有 ROOT 权限-2. 级联删除账户下的所有 AGFS 数据(`user/` 和 `resources/`;sessions 位于 `user/` 下)-3. 级联删除向量数据库中该账户的所有记录-4. 最后删除账户元数据和所有用户密钥+1. 验证 ROOT 权限,持久化账号删除标记,立即拒绝账号密钥和普通请求;创建系统作用域的 `account_delete` Task 并持久化入队,返回 `status=deleting` 和 `task_id`+2. 后台停止该账号的 Watch 和业务任务,清理向量、OAuth 授权、用量审计数据和整个账号 AGFS 目录(包含账号内的任务记录)+3. 清理成功后移除账号注册记录,将 Task 标记为 `completed`++账号和用户清理共用一个串行消费的数据清理队列。账号任务直接清理整个账号。后续用户清理任务发现目标已删除时,完成并跳过;旧任务也不会清理重建的同名账号或用户。归属该账号的任务记录一并删除,后续消息不会重建这些记录;系统作用域的清理 Task 仍可查询。**代码入口:**- `openviking/server/routers/admin.py:delete_account` - HTTP 路由-- `openviking/server/api_keys/new.py:APIKeyManager.delete_account` - 核心实现+- `openviking/service/deletion.py:DeletionService.delete` - 核心实现- `openviking_cli/client/sync_http.py:SyncHTTPClient.admin_delete_account` - Python SDK#### 2. 接口和参数说明@@ -682,7 +683,13 @@ ov --sudo admin list-accounts --limit 50 --page 2**说明:**- 删除操作是不可逆的,会级联删除该账户下的所有数据-- 如果部分数据删除失败,会记录警告日志并继续删除其他数据+- 清理失败时,Task 标记为 `failed` 并记录错误原因;账号保持 `deleting`+- 正在删除时重复请求返回同一个 Task;失败后再次请求会创建重试 Task,处理剩余数据+- 服务重启后恢复未完成任务;删除期间不能重建同名账号,也不能恢复账号使用+- 向量先按账号条件分页枚举 ID,再分批提交删除,每次删除请求最多 100 条,不受原来的单次 10 万条总量上限限制+- 向量删除以删除接口成功为准,不要求即时 Count 归零或回读为空;即使 Task 已完成,远程索引仍可能因同步延迟短暂返回旧数据+- 账号列表中的 `status` 为 `active` 或 `deleting`;删除中的账号同时返回 `task_id`+- 使用 ROOT 调用 `GET /api/v1/tasks/{task_id}` 查看状态和错误;清理任务只使用 `pending`、`running`、`completed`、`failed` 状态,不细分清理阶段,只有 `completed` 表示清理完成#### 3. 使用示例@@ -706,7 +713,7 @@ client = ov.SyncHTTPClient(api_key="<root-key>")client.initialize()result = client.admin_delete_account(account_id="acme")-print(f"Account deleted: {result['deleted']}")+print(f"Cleanup task: {result['task_id']}")```**TypeScript SDK**@@ -722,7 +729,7 @@ result, err := client.AdminDeleteAccount(ctx, "acme")if err != nil {return err}-fmt.Println(result["deleted"])+fmt.Println(result["task_id"])```**CLI**@@ -730,6 +737,7 @@ fmt.Println(result["deleted"])```bash# 需要 ROOT 权限,使用 --sudoov --sudo admin delete-account acme+ov --sudo task status <task_id>```**响应示例**@@ -738,7 +746,9 @@ ov --sudo admin delete-account acme{"status": "ok","result": {- "deleted": true+ "account_id": "acme",+ "status": "deleting",+ "task_id": "550e8400-e29b-41d4-a716-446655440000"},"time": 0.1}@@ -1013,7 +1023,7 @@ ov admin list-users acme --limit 50 --page 2**代码入口:**- `openviking/server/routers/admin.py:remove_user` - HTTP 路由-- `openviking/service/user_deletion.py:UserDeletionService.delete_user` - 核心实现+- `openviking/service/deletion.py:DeletionService.delete` - 核心实现- `openviking_cli/client/sync_http.py:SyncHTTPClient.admin_remove_user` - Python SDK#### 2. 接口和参数说明@@ -1029,6 +1039,7 @@ ov admin list-users acme --limit 50 --page 2- ADMIN 只能移除自己所属的 account 中的用户- 不能删除账户的最后一个 admin 用户- 删除开始后,用户 key 立即失效,list_users 不再返回该用户+- 向量删除以删除接口成功为准,不等待远程索引同步;Task 完成后,Count 或查询结果仍可能短暂滞后#### 3. 使用示例diff --git a/openviking/observability/usage_audit/runtime.py b/openviking/observability/usage_audit/runtime.pyindex 505df0680..b7c5ffd8d 100644--- a/openviking/observability/usage_audit/runtime.py+++ b/openviking/observability/usage_audit/runtime.py@@ -36,10 +36,10 @@ class UsageAuditRuntime:api_service: UsageAuditQueryServiceshutdown_flush_timeout_seconds: float- async def delete_user_data(self, *, account_id: str, user_id: str) -> dict[str, int]:- """Drain accepted events before purging one user's projections."""+ async def delete_data(self, *, account_id: str, user_id: str | None = None) -> dict[str, int]:+ """Drain accepted events before purging one account/user scope's projections."""await self.worker.flush()- return await self.store.delete_user_data(account_id=account_id, user_id=user_id)+ return await self.store.delete_data(account_id=account_id, user_id=user_id)def _resolve_sqlite_path(config: ServerConfig) -> Path:diff --git a/openviking/observability/usage_audit/sqlite_store.py b/openviking/observability/usage_audit/sqlite_store.pyindex c3859fe47..d37b9c530 100644--- a/openviking/observability/usage_audit/sqlite_store.py+++ b/openviking/observability/usage_audit/sqlite_store.py@@ -134,12 +134,12 @@ class SQLiteUsageAuditStore:self._conn.close()self._conn = None- async def delete_user_data(self, *, account_id: str, user_id: str) -> dict[str, int]:- """Delete all usage/audit rows for an account/user pair."""+ async def delete_data(self, *, account_id: str, user_id: str | None = None) -> dict[str, int]:+ """Delete all usage/audit rows for an account, or one user within it."""async with self._lock:- return await asyncio.to_thread(self._delete_user_data_sync, account_id, user_id)+ return await asyncio.to_thread(self._delete_data_sync, account_id, user_id)- def _delete_user_data_sync(self, account_id: str, user_id: str) -> dict[str, int]:+ def _delete_data_sync(self, account_id: str, user_id: str | None = None) -> dict[str, int]:assert self._conn is not Nonetables = ("usage_token_hourly",@@ -147,12 +147,14 @@ class SQLiteUsageAuditStore:"usage_context_write_bucket","request_audit",)+ predicate = "account_id = ?" + (" AND user_id = ?" if user_id is not None else "")+ params = (account_id, user_id) if user_id is not None else (account_id,)self._conn.execute("BEGIN")try:deleted = {table: self._conn.execute(- f"DELETE FROM {table} WHERE account_id = ? AND user_id = ?",- (account_id, user_id),+ f"DELETE FROM {table} WHERE {predicate}",+ params,).rowcountfor table in tables}diff --git a/openviking/observability/usage_audit/store.py b/openviking/observability/usage_audit/store.pyindex 3be0ac4bd..2f92dc96c 100644--- a/openviking/observability/usage_audit/store.py+++ b/openviking/observability/usage_audit/store.py@@ -26,8 +26,8 @@ class UsageAuditStore(Protocol):async def record_batch(self, events: Sequence[ObservabilityEvent]) -> None:"""Persist a batch of observability events."""- async def delete_user_data(self, *, account_id: str, user_id: str) -> dict[str, int]:- """Delete every persisted usage/audit row owned by one user."""+ async def delete_data(self, *, account_id: str, user_id: str | None = None) -> dict[str, int]:+ """Delete every persisted usage/audit row owned by one account/user scope."""async def get_today_tokens(self,diff --git a/openviking/pyagfs/async_client.py b/openviking/pyagfs/async_client.pyindex eeb310c72..0e7cb6399 100644--- a/openviking/pyagfs/async_client.py+++ b/openviking/pyagfs/async_client.py@@ -8,6 +8,8 @@ import asynciofrom collections.abc import Iteratorfrom typing import Any, BinaryIO, Dict, List, Union+from openviking.service.task_tracker_concurrency import run_to_completion+from .protocols import AGFSSyncClientProtocol_SYSTEM_ACCOUNT_ID = "_system"@@ -93,15 +95,19 @@ class AsyncAGFSClient:async def run(self, method_name: str, /, *args: Any, **kwargs: Any) -> Any:"""Run a sync client method in a worker thread, preserving ctx when supported."""try:- return await asyncio.to_thread(getattr(self._client, method_name), *args, **kwargs)+ return await run_to_completion(+ lambda: asyncio.to_thread(getattr(self._client, method_name), *args, **kwargs)+ )except TypeError as exc:message = str(exc)if "ctx" not in kwargs or "unexpected keyword argument 'ctx'" not in message:raiselegacy_kwargs = dict(kwargs)legacy_kwargs.pop("ctx", None)- return await asyncio.to_thread(- getattr(self._client, method_name), *args, **legacy_kwargs+ return await run_to_completion(+ lambda: asyncio.to_thread(+ getattr(self._client, method_name), *args, **legacy_kwargs+ ))async def ls(diff --git a/openviking/resource/watch_manager.py b/openviking/resource/watch_manager.pyindex d021761b1..62b61f2f5 100644--- a/openviking/resource/watch_manager.py+++ b/openviking/resource/watch_manager.py@@ -897,7 +897,12 @@ class WatchManager:self._tasks.pop(task_id, None)self._index_remove(task.account_id, task.to_uri, task_id)- await self._save_tasks()+ try:+ await self._save_tasks()+ except Exception:+ self._tasks[task_id] = task+ self._index_add(task.account_id, task.to_uri, task_id)+ raiselogger.info(f"[WatchManager] Deleted task {task_id} by user {account_id}/{user_id}")diff --git a/openviking/resource/watch_scheduler.py b/openviking/resource/watch_scheduler.pyindex db3195bcf..13d852195 100644--- a/openviking/resource/watch_scheduler.py+++ b/openviking/resource/watch_scheduler.py@@ -23,7 +23,7 @@ from openviking.resource.git_watch_auth import (is_git_http_auth_state,)from openviking.resource.uri_mutation_coordinator import UriMutationCoordinator-from openviking.resource.watch_manager import WatchManager+from openviking.resource.watch_manager import WatchManager, WatchTaskfrom openviking.server.error_mapping import is_not_found_errorfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.resource_service import ResourceService@@ -79,7 +79,7 @@ class WatchScheduler:self._running = Falseself._scheduler_task: Optional[asyncio.Task] = Noneself._executing_tasks: Set[str] = set()- self._execution_tasks: Set[asyncio.Task[None]] = set()+ self._execution_tasks: Dict[asyncio.Task, WatchTask] = {}self._lock = asyncio.Lock()@property@@ -165,13 +165,36 @@ class WatchScheduler:logger.info(f"[WatchScheduler] Task {task_id} is already executing, skipping")return False+ execution = asyncio.current_task()+ self._execution_tasks[execution] = tasktry:async with self._semaphore:await self._execute_task(task)return Truefinally:+ self._execution_tasks.pop(execution, None)await asyncio.shield(self._discard_executing(task_id))+ async def delete_tasks(self, account_id: str, user_id: str | None = None) -> None:+ """Remove an identity's watches and settle their current executions."""+ manager = self._watch_manager+ if manager is None:+ return+ actor_user_id = user_id or "root"+ for watch in await manager.get_all_tasks(account_id, actor_user_id, Role.ROOT):+ if watch.account_id == account_id and (user_id is None or watch.user_id == user_id):+ await manager.delete_task(watch.task_id, account_id, actor_user_id, Role.ROOT)++ executions = [+ execution+ for execution, watch in self._execution_tasks.items()+ if watch.account_id == account_id and (user_id is None or watch.user_id == user_id)+ ]+ for execution in executions:+ execution.cancel()+ if executions:+ await asyncio.gather(*executions, return_exceptions=True)+async def _run_scheduler(self) -> None:"""Background task loop that periodically checks and executes due tasks.@@ -234,11 +257,11 @@ class WatchScheduler:for due_task in tasks_to_run:execution = asyncio.create_task(run_one(due_task))- self._execution_tasks.add(execution)+ self._execution_tasks[execution] = due_taskexecution.add_done_callback(self._on_execution_done)def _on_execution_done(self, task: asyncio.Task[None]) -> None:- self._execution_tasks.discard(task)+ self._execution_tasks.pop(task, None)if task.cancelled():returnerror = task.exception()diff --git a/openviking/server/api_keys/legacy.py b/openviking/server/api_keys/legacy.pyindex fb5aa5052..2d1ceeb39 100644--- a/openviking/server/api_keys/legacy.py+++ b/openviking/server/api_keys/legacy.py@@ -110,7 +110,7 @@ class LegacyAPIKeyManager:# Serializes all mutations made by the unified manager in this process.# File locks remain responsible for inter-instance coordination.self._mutation_lock = asyncio.Lock()- self._user_deletion_lock = asyncio.Lock()+ self._deletion_lock = asyncio.Lock()@propertydef mutation_lock(self) -> asyncio.Lock:@@ -240,9 +240,11 @@ class LegacyAPIKeyManager:self._accounts[account_id] = AccountInfo(created_at=created_at,groups_loaded=False,+ deletion=info.get("deletion"),)else:account.created_at = created_at+ account.deletion = info.get("deletion")async def refresh_account_users_from_store(self, account_id: str) -> None:"""Refresh one account's users without reading unrelated registries."""@@ -340,6 +342,7 @@ class LegacyAPIKeyManager:created_at=info.get("created_at", ""),users=users,groups=groups,+ deletion=info.get("deletion"),)user_group_ids.update({(account_id, user_id): group_ids@@ -435,6 +438,8 @@ class LegacyAPIKeyManager:candidates = self._prefix_index.get(key_prefix, [])for entry in candidates:+ if self.get_deletion(entry.account_id) is not None:+ continueif entry.is_hashed:# Verify hashed keyif self._verify_api_key(api_key, entry.key_or_hash):@@ -541,9 +546,8 @@ class LegacyAPIKeyManager:if account_id not in self._accounts:raise NotFoundError(account_id, "account")- self._discard_account_state(account_id)-await self._save_accounts_json(delete_account_ids={account_id})+ self._discard_account_state(account_id)async def register_user(self,@@ -559,6 +563,7 @@ class LegacyAPIKeyManager:if verr:raise InvalidArgumentError(verr)+ self.ensure_account_active(account_id)account = self._accounts.get(account_id)if account is None:raise NotFoundError(account_id, "account")@@ -621,7 +626,11 @@ class LegacyAPIKeyManager:async def _ensure_trusted_identities_unlocked(self, identities: Dict[str, set[str]]) -> dict[str, int]:- normalized = {account_id: set(user_ids) for account_id, user_ids in identities.items() if user_ids}+ normalized = {+ account_id: set(user_ids)+ for account_id, user_ids in identities.items()+ if user_ids and self.get_deletion(account_id) is None+ }if not normalized:return {"created_accounts": 0, "created_users": 0}@@ -706,20 +715,35 @@ class LegacyAPIKeyManager:await self._update_identity_registry_signatures(set(normalized))return {"created_accounts": created_accounts, "created_users": created_users}- async def begin_user_deletion(+ async def begin_deletion(self,account_id: str,- user_id: str,+ user_id: str | None,*,task_id: str,owner_account_id: str,owner_user_id: str,) -> tuple[dict, bool]:- """Revoke the user key and persist the deletion task fence."""- async with self._reload_lock, self._user_deletion_lock:+ """Revoke an account or user and persist its cleanup task fence."""+ async with self._reload_lock, self._deletion_lock:account = self._accounts.get(account_id)if account is None:raise NotFoundError(account_id, "account")+ if user_id is None:+ if account.deletion is not None:+ return dict(account.deletion), False+ account.deletion = {+ "task_id": task_id,+ "owner_account_id": owner_account_id,+ "owner_user_id": owner_user_id,+ }+ try:+ await self._save_accounts_json(updated_account_ids={account_id})+ except BaseException:+ account.deletion = None+ raise+ return dict(account.deletion), True+ self.ensure_account_active(account_id)user_info = account.users.get(user_id)if user_info is None:raise NotFoundError(user_id, "user")@@ -753,10 +777,10 @@ class LegacyAPIKeyManager:self._remove_key_index_entry(account_id, user_id, original)return dict(deletion), True- async def replace_user_deletion_task(+ async def replace_deletion_task(self,account_id: str,- user_id: str,+ user_id: str | None,*,expected_task_id: str,task_id: str,@@ -764,10 +788,26 @@ class LegacyAPIKeyManager:owner_user_id: str,) -> dict:"""Replace the task that owns an existing deletion fence."""- async with self._reload_lock, self._user_deletion_lock:+ async with self._reload_lock, self._deletion_lock:account = self._accounts.get(account_id)if account is None:raise NotFoundError(account_id, "account")+ if user_id is None:+ current = account.deletion+ if current is None or current["task_id"] != expected_task_id:+ return dict(current) if current else {}+ account.deletion = {+ "task_id": task_id,+ "owner_account_id": owner_account_id,+ "owner_user_id": owner_user_id,+ }+ try:+ await self._save_accounts_json(updated_account_ids={account_id})+ except BaseException:+ account.deletion = current+ raise+ return dict(account.deletion)+ self.ensure_account_active(account_id)user_info = account.users.get(user_id)if user_info is None:raise NotFoundError(user_id, "user")@@ -788,12 +828,17 @@ class LegacyAPIKeyManager:raisereturn dict(replacement)- async def finish_user_deletion(self, account_id: str, user_id: str, task_id: str) -> bool:- """Remove the user only when this task still owns the deletion fence."""- async with self._reload_lock, self._user_deletion_lock:+ async def finish_deletion(self, account_id: str, user_id: str | None, task_id: str) -> bool:+ """Remove the identity only when this task still owns its deletion fence."""+ async with self._reload_lock, self._deletion_lock:account = self._accounts.get(account_id)if account is None:return False+ if user_id is None:+ if account.deletion is None or account.deletion["task_id"] != task_id:+ return False+ await self.delete_account(account_id)+ return Trueuser_info = account.users.get(user_id)if user_info is None:return False@@ -823,30 +868,39 @@ class LegacyAPIKeyManager:self._rebuild_account_group_index(account_id)return True- def get_user_deletion(self, account_id: str, user_id: str) -> Optional[dict]:+ def get_deletion(self, account_id: str, user_id: str | None = None) -> Optional[dict]:account = self._accounts.get(account_id)if account is None:return None+ if user_id is None:+ return dict(account.deletion) if account.deletion is not None else Noneuser_info = account.users.get(user_id)deletion = user_info.get("deletion") if user_info else Nonereturn dict(deletion) if isinstance(deletion, dict) else None- def iter_user_deletions(self) -> list[tuple[str, str, dict]]:+ def iter_deletions(self) -> list[tuple[str, str | None, dict]]:return [(account_id, user_id, dict(deletion))for account_id, account in self._accounts.items()for user_id, user_info in account.users.items()- if isinstance((deletion := user_info.get("deletion")), dict)- and deletion.get("task_id")+ if isinstance((deletion := user_info.get("deletion")), dict) and deletion.get("task_id")+ ] + [+ (account_id, None, dict(account.deletion))+ for account_id, account in self._accounts.items()+ if account.deletion is not None]- def is_user_deleting(self, account_id: str, user_id: str) -> bool:- return self.get_user_deletion(account_id, user_id) is not None+ def is_deleting(self, account_id: str, user_id: str | None = None) -> bool:+ return (+ self.get_deletion(account_id) is not None+ or self.get_deletion(account_id, user_id) is not None+ )async def regenerate_key(self, account_id: str, user_id: str, seed: Optional[str] = None) -> str:"""Regenerate a user's API key. Old key is immediately invalidated."""+ self.ensure_account_active(account_id)account = self._accounts.get(account_id)if account is None:raise NotFoundError(account_id, "account")@@ -918,6 +972,7 @@ class LegacyAPIKeyManager:async def set_role(self, account_id: str, user_id: str, role: str) -> None:"""Update a user's role."""resolved_role = validate_account_user_role(role)+ self.ensure_account_active(account_id)account = self._accounts.get(account_id)if account is None:raise NotFoundError(account_id, "account")@@ -969,6 +1024,8 @@ class LegacyAPIKeyManager:"account_id": account_id,"created_at": info.created_at,"user_count": len(info.users),+ "status": "deleting" if info.deletion else "active",+ **({"task_id": info.deletion["task_id"]} if info.deletion else {}),})return _paginate(result, limit, page)@@ -1133,7 +1190,7 @@ class LegacyAPIKeyManager:stored value is empty (no fingerprint to bind to)."""account = self._accounts.get(account_id)- if account is None:+ if account is None or account.deletion is not None:return Noneuser = account.users.get(user_id)if user is None:@@ -1145,7 +1202,16 @@ class LegacyAPIKeyManager:# ---- internal helpers ----+ def ensure_account_active(self, account_id: str) -> None:+ deletion = self.get_deletion(account_id)+ if deletion is not None:+ raise FailedPreconditionError(+ "Account deletion is in progress",+ details={"task_id": deletion["task_id"]},+ )+def _require_account(self, account_id: str) -> AccountInfo:+ self.ensure_account_active(account_id)account = self._accounts.get(account_id)if account is None:raise NotFoundError(account_id, "account")@@ -1366,6 +1432,8 @@ class LegacyAPIKeyManager:if account_id not in accounts:created_account_ids.add(account_id)accounts[account_id] = {"created_at": info.created_at}+ if info.deletion is not None:+ accounts[account_id]["deletion"] = dict(info.deletion)for account_id in delete_account_ids or set():accounts.pop(account_id, None)await self._write_json(ACCOUNTS_PATH, data, lease_ref=lease)diff --git a/openviking/server/api_keys/models.py b/openviking/server/api_keys/models.pyindex 3f1608de5..328fe05ec 100644--- a/openviking/server/api_keys/models.py+++ b/openviking/server/api_keys/models.py@@ -40,3 +40,4 @@ class AccountInfo:users: Dict[str, dict] = field(default_factory=dict)groups: Dict[str, dict] = field(default_factory=dict)groups_loaded: bool = True+ deletion: dict | None = Nonediff --git a/openviking/server/api_keys/new.py b/openviking/server/api_keys/new.pyindex 19afc1c02..b372044c0 100644--- a/openviking/server/api_keys/new.py+++ b/openviking/server/api_keys/new.py@@ -164,7 +164,7 @@ class NewAPIKeyManager:# Verify the user exists in the account using legacy's dataaccount = self._legacy._accounts.get(account_id)- if account and user_id in account.users:+ if account and account.deletion is None and user_id in account.users:user_info = account.users[user_id]stored_key_or_hash = user_info.get("key", "")@@ -330,6 +330,7 @@ class NewAPIKeyManager:raise InvalidArgumentError(verr)# Check account exists first+ self._legacy.ensure_account_active(account_id)account = self._legacy._accounts.get(account_id)if account is None:from openviking_cli.exceptions import NotFoundError@@ -384,17 +385,17 @@ class NewAPIKeyManager:raisereturn key- async def begin_user_deletion(+ async def begin_deletion(self,account_id: str,- user_id: str,+ user_id: str | None,*,task_id: str,owner_account_id: str,owner_user_id: str,) -> tuple[dict, bool]:async with self._legacy.mutation_lock:- return await self._legacy.begin_user_deletion(+ return await self._legacy.begin_deletion(account_id,user_id,task_id=task_id,@@ -402,10 +403,10 @@ class NewAPIKeyManager:owner_user_id=owner_user_id,)- async def replace_user_deletion_task(+ async def replace_deletion_task(self,account_id: str,- user_id: str,+ user_id: str | None,*,expected_task_id: str,task_id: str,@@ -413,7 +414,7 @@ class NewAPIKeyManager:owner_user_id: str,) -> dict:async with self._legacy.mutation_lock:- return await self._legacy.replace_user_deletion_task(+ return await self._legacy.replace_deletion_task(account_id,user_id,expected_task_id=expected_task_id,@@ -422,18 +423,21 @@ class NewAPIKeyManager:owner_user_id=owner_user_id,)- async def finish_user_deletion(self, account_id: str, user_id: str, task_id: str) -> bool:+ async def finish_deletion(self, account_id: str, user_id: str | None, task_id: str) -> bool:async with self._legacy.mutation_lock:- return await self._legacy.finish_user_deletion(account_id, user_id, task_id)+ return await self._legacy.finish_deletion(account_id, user_id, task_id)++ def get_deletion(self, account_id: str, user_id: str | None = None) -> Optional[dict]:+ return self._legacy.get_deletion(account_id, user_id)- def get_user_deletion(self, account_id: str, user_id: str) -> Optional[dict]:- return self._legacy.get_user_deletion(account_id, user_id)+ def iter_deletions(self) -> list[tuple[str, str | None, dict]]:+ return self._legacy.iter_deletions()- def iter_user_deletions(self) -> list[tuple[str, str, dict]]:- return self._legacy.iter_user_deletions()+ def is_deleting(self, account_id: str, user_id: str | None = None) -> bool:+ return self._legacy.is_deleting(account_id, user_id)- def is_user_deleting(self, account_id: str, user_id: str) -> bool:- return self._legacy.is_user_deleting(account_id, user_id)+ def ensure_account_active(self, account_id: str) -> None:+ self._legacy.ensure_account_active(account_id)async def regenerate_key(self,@@ -455,6 +459,7 @@ class NewAPIKeyManager:Generates new format key regardless of original format."""# Check account and user exist+ self._legacy.ensure_account_active(account_id)account = self._legacy._accounts.get(account_id)if account is None:from openviking_cli.exceptions import NotFoundErrordiff --git a/openviking/server/app.py b/openviking/server/app.pyindex 766977c9a..3b422d39f 100644--- a/openviking/server/app.py+++ b/openviking/server/app.py@@ -170,9 +170,9 @@ async def _initialize_runtime_state(if item["account_id"] != service.user.account_id])- from openviking.service.user_deletion import setup_user_deletion+ from openviking.service.deletion import setup_deletion- app.state.user_deletion_service = await setup_user_deletion(+ app.state.deletion_service = await setup_deletion(service=service,manager=app.state.api_key_manager,oauth_store=getattr(app.state, "oauth_store", None),@@ -437,7 +437,7 @@ def create_app(app.state.config = configapp.state.api_key_manager = None- app.state.user_deletion_service = None+ app.state.deletion_service = Noneset_server_config(config)# Body dump middleware must be registered BEFORE observability so it ends updiff --git a/openviking/server/auth/__init__.py b/openviking/server/auth/__init__.pyindex 054c1d7d2..7598ecbe8 100644--- a/openviking/server/auth/__init__.py+++ b/openviking/server/auth/__init__.py@@ -127,15 +127,19 @@ def _build_request_context(api_key=api_key,)manager = getattr(request.app.state, "api_key_manager", None)- is_user_deleting = getattr(manager, "is_user_deleting", None)+ is_deleting = getattr(manager, "is_deleting", None)if (ctx.role != Role.ROOT- and callable(is_user_deleting)- and is_user_deleting(ctx.account_id, ctx.user.user_id)+ and callable(is_deleting)+ and is_deleting(ctx.account_id, ctx.user.user_id)):- deletion = manager.get_user_deletion(ctx.account_id, ctx.user.user_id) or {}+ deletion = (+ manager.get_deletion(ctx.account_id)+ or manager.get_deletion(ctx.account_id, ctx.user.user_id)+ or {}+ )raise FailedPreconditionError(- "User deletion is in progress",+ "Identity deletion is in progress",details={"task_id": deletion.get("task_id")},)update_root_span_identity(diff --git a/openviking/server/auth/plugins/trusted.py b/openviking/server/auth/plugins/trusted.pyindex afeaf11d7..71b91b4b3 100644--- a/openviking/server/auth/plugins/trusted.py+++ b/openviking/server/auth/plugins/trusted.py@@ -274,7 +274,10 @@ class TrustedAuthPlugin(AuthPlugin):return self._flush_interval_seconds > 0def _queue_trusted_identity(self, account_id: str, user_id: str) -> None:- if self._api_key_manager and self._api_key_manager.has_user(account_id, user_id):+ if self._api_key_manager and (+ self._api_key_manager.is_deleting(account_id, user_id)+ or self._api_key_manager.has_user(account_id, user_id)+ ):returnif user_id in self._in_flight.get(account_id, set()):returndiff --git a/openviking/server/oauth/provider.py b/openviking/server/oauth/provider.pyindex 67b25b4c7..e713655fc 100644--- a/openviking/server/oauth/provider.py+++ b/openviking/server/oauth/provider.py@@ -330,7 +330,7 @@ class OpenVikingOAuthProvider(refresh_token.account_id,refresh_token.user_id,)- await self._store.revoke_user_tokens(+ await self._store.revoke_tokens(account_id=refresh_token.account_id,user_id=refresh_token.user_id,)diff --git a/openviking/server/oauth/storage.py b/openviking/server/oauth/storage.pyindex 4f3860c48..af5e36c59 100644--- a/openviking/server/oauth/storage.py+++ b/openviking/server/oauth/storage.py@@ -568,35 +568,31 @@ class OAuthStore:async with self._lock:return await asyncio.to_thread(_revoke)- async def revoke_user_tokens(self, *, account_id: str, user_id: str) -> dict[str, int]:- """Revoke all access + refresh tokens for a (account, user) pair.-- Used when an API key for that user is rotated / deleted: every OAuth- token derived from that user identity gets invalidated. Auth codes for- the same user are also wiped to prevent in-flight completion of an- exchange started before the revocation.- """+ async def revoke_tokens(self, *, account_id: str, user_id: str | None = None) -> dict[str, int]:+ """Revoke OAuth grants for one account, or for one user within it."""def _revoke() -> dict[str, int]:assert self._conn is not None+ predicate = "account_id = ?" + (" AND user_id = ?" if user_id is not None else "")+ pending_predicate = "verified_account_id = ?" + (+ " AND verified_user_id = ?" if user_id is not None else ""+ )+ params = (account_id, user_id) if user_id is not None else (account_id,)access = self._conn.execute(- "UPDATE oauth_access_tokens SET revoked = 1 "- "WHERE account_id = ? AND user_id = ? AND revoked = 0",- (account_id, user_id),+ f"UPDATE oauth_access_tokens SET revoked = 1 WHERE {predicate} AND revoked = 0",+ params,).rowcountrefresh = self._conn.execute(- "UPDATE oauth_refresh_tokens SET consumed = 1 "- "WHERE account_id = ? AND user_id = ? AND consumed = 0",- (account_id, user_id),+ f"UPDATE oauth_refresh_tokens SET consumed = 1 WHERE {predicate} AND consumed = 0",+ params,).rowcountcodes = self._conn.execute(- "UPDATE oauth_codes SET used = 1 WHERE account_id = ? AND user_id = ? AND used = 0",- (account_id, user_id),+ f"UPDATE oauth_codes SET used = 1 WHERE {predicate} AND used = 0",+ params,).rowcountpending = self._conn.execute(- "DELETE FROM oauth_pending_authorizations "- "WHERE verified_account_id = ? AND verified_user_id = ?",- (account_id, user_id),+ f"DELETE FROM oauth_pending_authorizations WHERE {pending_predicate}",+ params,).rowcountreturn {"access_tokens_revoked": access,diff --git a/openviking/server/routers/admin.py b/openviking/server/routers/admin.pyindex 7bd419718..57baeeefc 100644--- a/openviking/server/routers/admin.py+++ b/openviking/server/routers/admin.py@@ -51,7 +51,6 @@ from openviking.session.memory.account_templates import ()from openviking.session.memory.memory_type_registry import get_default_registryfrom openviking.session.memory_policy import MemoryPolicy-from openviking.storage.viking_fs import get_viking_fsfrom openviking_cli.exceptions import (FailedPreconditionError,InvalidArgumentError,@@ -192,6 +191,7 @@ async def _check_account_exists(accounts = manager.get_accounts()if not any(item.get("account_id") == account_id for item in accounts):raise NotFoundError(account_id, "account")+ manager.ensure_account_active(account_id)if refresh_scope is not None and not watcher_running:await manager.refresh_account_users_from_store(refresh_scope)return manager@@ -410,37 +410,19 @@ async def migrate_legacy_data(return Response(status="ok", result={"task_id": task.task_id})[email protected]("/accounts/{account_id}")[email protected]("/accounts/{account_id}", status_code=202)@require_auth_rootasync def delete_account(request: Request,account_id: str = Path(..., description="Account ID"),ctx: RequestContext = Depends(get_request_context),):- """Delete an account and cascade-clean its storage (AGFS + VectorDB)."""- manager = _get_api_key_manager(request)-- # Cascade: remove AGFS data for the account.- # Use the raw AGFS path to bypass the VikingFS namespace guard- # (viking://user is a protected namespace root, not a real directory).- viking_fs = get_viking_fs()- try:- await viking_fs._async_agfs.rm(f"/local/{account_id}", recursive=True)- except Exception as e:- logger.warning(f"AGFS cleanup for account {account_id}: {e}")-- # Cascade: remove VectorDB records for the account- try:- storage = viking_fs._get_vector_store()- if storage:- deleted = await storage.delete_account_data(account_id, ctx=ctx)- logger.info(f"VectorDB cascade delete for account {account_id}: {deleted} records")- except Exception as e:- logger.warning(f"VectorDB cleanup for account {account_id}: {e}")-- # Finally delete the account metadata- await manager.delete_account(account_id)- return Response(status="ok", result={"deleted": True})+ """Revoke an account and submit durable cleanup of its data."""+ deletion_service = request.app.state.deletion_service+ if deletion_service is None:+ raise FailedPreconditionError("Deletion service is not initialized.")+ result = await deletion_service.delete(account_id, actor=ctx)+ return Response(status="ok", result=result)@router.get("/accounts/{account_id}/settings")@@ -731,10 +713,10 @@ async def remove_user():"""Revoke a user and start durable cleanup of their owned data."""_check_account_access(ctx, account_id)- deletion_service = request.app.state.user_deletion_service+ deletion_service = request.app.state.deletion_serviceif deletion_service is None:- raise FailedPreconditionError("User deletion service is not initialized.")- result = await deletion_service.delete_user(account_id, user_id, actor=ctx)+ raise FailedPreconditionError("Deletion service is not initialized.")+ result = await deletion_service.delete(account_id, user_id, actor=ctx)return Response(status="ok", result=result)diff --git a/openviking/service/core.py b/openviking/service/core.pyindex dc8a916d9..c3dd0d124 100644--- a/openviking/service/core.py+++ b/openviking/service/core.py@@ -521,12 +521,9 @@ class OpenVikingService:allow_create=True,)# Auth state is initialized by the HTTP server after the core service.- # Register the durable queue now so task tracking can rebuild its work;- # the user-deletion service binds the handler once auth is ready.- self._queue_manager.get_queue(- self._queue_manager.USER_DELETION,- allow_create=True,- )+ # Register durable cleanup work before restoring tracked tasks;+ # the deletion service binds consumers once auth is ready.+ self._queue_manager.get_queue(self._queue_manager.DATA_CLEANUP, allow_create=True)restored_tasks = await self._queue_manager.prepare_task_tracking(get_task_tracker())await self._external_task_service.restore_tasks(restored_tasks)diff --git a/openviking/service/user_deletion.py b/openviking/service/deletion.pysimilarity index 56%rename from openviking/service/user_deletion.pyrename to openviking/service/deletion.pyindex 78bcc3ab6..5971d77a7 100644--- a/openviking/service/user_deletion.py+++ b/openviking/service/deletion.py@@ -1,20 +1,21 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0-"""Durable, idempotent deletion of one user and their owned data."""+"""Durable, idempotent cleanup of accounts and users."""from __future__ import annotationsimport asyncioimport jsonimport time-from typing import Any, Awaitable, Callable, Optional+from typing import Any, Optionalfrom uuid import uuid4from openviking.core.namespace import canonical_user_root+from openviking.server.error_mapping import is_not_found_errorfrom 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_tracker-from openviking.service.task_tracker_concurrency import OwnerLoopDispatcher+from openviking.service.task_tracker_concurrency import OwnerLoopDispatcher, run_to_completionfrom openviking.service.task_work_index import extract_task_metadatafrom openviking.storage.queuefs.named_queue import DequeueHandlerBasefrom openviking.storage.queuefs.process_result import ProcessResult@@ -25,7 +26,6 @@ from openviking_cli.utils.logger import get_loggerlogger = get_logger(__name__)-TASK_TYPE = "user_delete"_CANCEL_WAIT_SECONDS = 10 * 60_SHARED_UPLOAD_ROOT = "viking://upload"_ACTIVE_TASK_STATUSES = (@@ -46,7 +46,7 @@ def _deletion_message(owner_account_id: str,owner_user_id: str,target_account_id: str,- target_user_id: str,+ target_user_id: str | None,) -> dict[str, Any]:return {"task_id": task_id,@@ -59,7 +59,7 @@ def _deletion_message(}-class UserDeletionService:+class DeletionService:"""Own the deletion fence, tracked task, durable work, and cleanup pipeline."""def __init__(@@ -80,21 +80,18 @@ class UserDeletionService:async def initialize(self) -> None:"""Bind the queue consumer and reconcile persisted deletion fences."""- self._service.viking_fs.set_user_deletion_guard(self._manager.is_user_deleting)+ self._service.viking_fs.set_deletion_guard(self._manager.is_deleting)queue_manager = self._service._queue_manager- queue = queue_manager.get_queue(queue_manager.USER_DELETION)+ queue = queue_manager.get_queue(queue_manager.DATA_CLEANUP)queued_task_ids = {metadata.task_idfor message in await queue.snapshot()if (metadata := extract_task_metadata(message)) is not None}- queue_manager.get_queue(- queue_manager.USER_DELETION,- dequeue_handler=_UserDeletionProcessor(self, self._service_loop),- )+ queue.set_dequeue_handler(_DeletionProcessor(self, self._service_loop))tracker = get_task_tracker()- for account_id, user_id, deletion in self._manager.iter_user_deletions():+ for account_id, user_id, deletion in self._manager.iter_deletions():task_id = deletion.get("task_id")owner_account_id = deletion.get("owner_account_id")owner_user_id = deletion.get("owner_user_id")@@ -107,8 +104,8 @@ class UserDeletionService:)if task is None:task = await tracker.create(- TASK_TYPE,- resource_id=f"{account_id}/{user_id}",+ "account_delete" if user_id is None else "user_delete",+ resource_id=account_id if user_id is None else f"{account_id}/{user_id}",task_id=task_id,account_id=owner_account_id,user_id=owner_user_id,@@ -127,16 +124,24 @@ class UserDeletionService:)queued_task_ids.add(task_id)- async def delete_user(+ async def delete(self,account_id: str,- user_id: str,+ user_id: str | None = None,*,actor: RequestContext,) -> dict[str, str]:- """Revoke the user immediately and ensure one durable cleanup task exists."""- if actor.role == Role.ROOT or (- actor.account_id == account_id and actor.user.user_id == user_id+ """Revoke the identity immediately and ensure one durable cleanup task exists."""+ # Request cancellation must not interrupt the fence/task/queue handoff.+ return await run_to_completion(lambda: self._submit(account_id, user_id, actor=actor))++ async def _submit(+ self, account_id: str, user_id: str | None, *, actor: RequestContext+ ) -> dict[str, str]:+ if (+ user_id is None+ or actor.role == Role.ROOT+ or (actor.account_id == account_id and actor.user.user_id == user_id)):request_owner_account_id = SYSTEM_TASK_ACCOUNT_IDrequest_owner_user_id = SYSTEM_TASK_USER_ID@@ -146,7 +151,7 @@ class UserDeletionService:async with self._request_lock:task_id = str(uuid4())- deletion, created = await self._manager.begin_user_deletion(+ deletion, created = await self._manager.begin_deletion(account_id,user_id,task_id=task_id,@@ -163,7 +168,7 @@ class UserDeletionService:user_id=owner_user_id,)if existing is not None and existing.status in _TERMINAL_TASK_STATUSES:- deletion = await self._manager.replace_user_deletion_task(+ deletion = await self._manager.replace_deletion_task(account_id,user_id,expected_task_id=task_id,@@ -172,14 +177,14 @@ class UserDeletionService:owner_user_id=request_owner_user_id,)if not deletion:- raise NotFoundError(user_id, "user")+ raise NotFoundError(user_id or account_id, "user" if user_id else "account")task_id = deletion["task_id"]owner_account_id = deletion["owner_account_id"]owner_user_id = deletion["owner_user_id"]await get_task_tracker().create(- TASK_TYPE,- resource_id=f"{account_id}/{user_id}",+ "account_delete" if user_id is None else "user_delete",+ resource_id=account_id if user_id is None else f"{account_id}/{user_id}",task_id=task_id,account_id=owner_account_id,user_id=owner_user_id,@@ -194,7 +199,7 @@ class UserDeletionService:return {"account_id": account_id,- "user_id": user_id,+ **({"user_id": user_id} if user_id is not None else {}),"status": "deleting","task_id": task_id,}@@ -206,10 +211,10 @@ class UserDeletionService:owner_account_id: str,owner_user_id: str,target_account_id: str,- target_user_id: str,+ target_user_id: str | None,) -> None:queue_manager = self._service._queue_manager- queue = queue_manager.get_queue(queue_manager.USER_DELETION)+ queue = queue_manager.get_queue(queue_manager.DATA_CLEANUP)for message in await queue.snapshot():metadata = extract_task_metadata(message)if metadata is not None and metadata.task_id == task_id:@@ -226,210 +231,135 @@ class UserDeletionService:async def _process(self, message: dict[str, Any]) -> Optional[str]:task_id = message["task_id"]- owner_account_id = message["account_id"]- owner_user_id = message["user_id"]- target_account_id = message["target"]["account_id"]- target_user_id = message["target"]["user_id"]+ owner = {"account_id": message["account_id"], "user_id": message["user_id"]}+ account_id = message["target"]["account_id"]+ user_id = message["target"]["user_id"]+ scope = "account" if user_id is None else "user"tracker = get_task_tracker()+ deletion = self._manager.get_deletion(account_id, user_id)+ if deletion is None or deletion["task_id"] != task_id:+ task = await tracker.get(task_id, **owner)+ # Account-owned records are removed with their account. A late+ # message must not recreate them, even if the ID has been reused.+ if task is None and owner["account_id"] != SYSTEM_TASK_ACCOUNT_ID:+ return Nonetask = await tracker.create(- TASK_TYPE,- resource_id=f"{target_account_id}/{target_user_id}",+ f"{scope}_delete",+ resource_id=account_id if user_id is None else f"{account_id}/{user_id}",task_id=task_id,- account_id=owner_account_id,- user_id=owner_user_id,+ **owner,)- owner = {- "account_id": owner_account_id,- "user_id": owner_user_id,- }if task.status in _TERMINAL_TASK_STATUSES:return None-- deletion = self._manager.get_user_deletion(target_account_id, target_user_id)- if deletion is None or deletion.get("task_id") != task_id:- deleted = not self._manager.has_user(target_account_id, target_user_id)- await tracker.complete(- task_id,- {"deleted": deleted, "stale": True},- **owner,+ if deletion is None or deletion["task_id"] != task_id:+ exists = (+ self._manager.has_user(account_id, user_id)+ if user_id is not None+ else any(item["account_id"] == account_id for item in self._manager.get_accounts()))+ await tracker.complete(task_id, {"deleted": not exists, "stale": True}, **owner)return None- try:- await tracker.start(task_id, stage="stopping_watches", **owner)- await self._stop_watches(target_account_id, target_user_id)- except Exception:- logger.exception(- "Failed to stop watches for user %s/%s",- target_account_id,- target_user_id,- )- error = "User cleanup failed at stages: watches"- await tracker.fail(task_id, error, **owner)- return error-- try:- await tracker.update_stage(task_id, "cancelling_tasks", **owner)- await self._cancel_user_tasks(target_account_id, target_user_id)- except Exception:- logger.exception(- "Failed to settle tasks for user %s/%s",- target_account_id,- target_user_id,- )- error = "User cleanup failed at stages: tasks"- await tracker.fail(task_id, error, **owner)- return error-- cleanup_ctx = RequestContext(- user=UserIdentifier(target_account_id, target_user_id),- role=Role.ROOT,+ ctx = RequestContext(+ user=UserIdentifier(account_id, user_id or SYSTEM_TASK_USER_ID), role=Role.ROOT)- steps: list[tuple[str, Callable[[], Awaitable[Any]]]] = [- (- "task_records",- lambda: tracker.delete_user_tasks(target_account_id, target_user_id),- ),- (- "agfs",- lambda: self._service.viking_fs.rm(- canonical_user_root(cleanup_ctx),- recursive=True,- ctx=cleanup_ctx,- ),- ),- (- "uploads",- lambda: self._delete_uploads(cleanup_ctx),- ),- ]- vector_store = self._service.viking_fs.vector_store- if vector_store is not None:- steps.insert(- 1,- (- "vectordb",- lambda: vector_store.delete_user_data(- target_account_id,- target_user_id,- ctx=cleanup_ctx,- ),- ),- )- if self._oauth_store is not None:- steps.append(- (- "oauth",- lambda: self._oauth_store.revoke_user_tokens(- account_id=target_account_id,- user_id=target_user_id,- ),+ await tracker.start(task_id, **owner)+ try:+ # Settle each started operation before propagating cancellation;+ # the durable message can then resume cleanup after restart.+ scheduler = self._service.watch_scheduler+ if scheduler is not None:+ await run_to_completion(lambda: scheduler.delete_tasks(account_id, user_id))+ await run_to_completion(lambda: self._cancel_tasks(account_id, user_id))+ if user_id is not None:+ await run_to_completion(lambda: tracker.delete_user_tasks(account_id, user_id))++ vectors = self._service.viking_fs.vector_store+ if vectors is not None:+ if user_id is None:+ await run_to_completion(+ lambda: vectors.delete_account_data(account_id, ctx=ctx)+ )+ else:+ await run_to_completion(+ lambda: vectors.delete_user_data(account_id, user_id, ctx=ctx)+ )+ if self._oauth_store is not None:+ await run_to_completion(+ lambda: self._oauth_store.revoke_tokens(account_id=account_id, user_id=user_id))- )- if self._usage_audit_runtime is not None:- steps.append(- (- "usage_audit",- lambda: self._usage_audit_runtime.delete_user_data(- account_id=target_account_id,- user_id=target_user_id,- ),+ if self._usage_audit_runtime is not None:+ await run_to_completion(+ lambda: self._usage_audit_runtime.delete_data(+ account_id=account_id, user_id=user_id+ ))- )-- failed_stages: list[str] = []- for stage, cleanup in steps:- await tracker.update_stage(task_id, f"cleaning_{stage}", **owner)- try:- await cleanup()- except Exception:- failed_stages.append(stage)- logger.exception(- "User cleanup stage %s failed for %s/%s",- stage,- target_account_id,- target_user_id,+ if user_id is None:+ await run_to_completion(lambda: self._delete_account_files(account_id))+ else:+ await run_to_completion(lambda: self._delete_uploads(ctx))+ await run_to_completion(+ lambda: self._service.viking_fs.rm(+ canonical_user_root(ctx), recursive=True, ctx=ctx+ ))- if failed_stages:- error = "User cleanup failed at stages: " + ", ".join(failed_stages)- await tracker.fail(task_id, error, **owner)- return error-- await tracker.update_stage(task_id, "removing_user", **owner)- try:- removed = await self._manager.finish_user_deletion(- target_account_id,- target_user_id,- task_id,- )- except Exception:- logger.exception(- "Failed to remove user registry entry for %s/%s",- target_account_id,- target_user_id,+ deleted = await run_to_completion(+ lambda: self._manager.finish_deletion(account_id, user_id, task_id))- error = "User cleanup failed at stages: registry"+ if deleted is not True:+ raise RuntimeError("Cleanup task no longer owns the identity deletion")+ except Exception as exc:+ logger.exception("%s cleanup failed for %s/%s", scope, account_id, user_id)+ error = f"{scope.capitalize()} cleanup failed: {exc}"await tracker.fail(task_id, error, **owner)return error-- await tracker.complete(- task_id,- {"deleted": removed, "stale": not removed},- **owner,- )+ await tracker.complete(task_id, {"deleted": True}, **owner)return None- async def _stop_watches(self, account_id: str, user_id: str) -> None:- scheduler = self._service.watch_scheduler- manager = scheduler.watch_manager if scheduler is not None else None- if manager is None:- return- for task in await manager.get_all_tasks(account_id, user_id, Role.USER):- await manager.delete_task(task.task_id, account_id, user_id, Role.USER)+ async def _delete_account_files(self, account_id: str) -> None:+ agfs = self._service.viking_fs._async_agfs+ path = f"/local/{account_id}"+ try:+ await agfs.rm(path, recursive=True)+ except Exception as exc:+ if not is_not_found_error(exc):+ raise+ try:+ await agfs.stat(path)+ except Exception as exc:+ if is_not_found_error(exc):+ get_task_tracker().forget_account_tasks(account_id)+ return+ raise+ raise RuntimeError("Account directory still exists after deletion")- async def _cancel_user_tasks(self, account_id: str, user_id: str) -> None:+ async def _cancel_tasks(self, account_id: str, user_id: str | None) -> None:tracker = get_task_tracker()+ tasks = await tracker.list_tasks(account_id=account_id, user_id=user_id, limit=None)active = [task- for task in await tracker.list_tasks(- account_id=account_id,- user_id=user_id,- limit=10_000,- )+ for task in tasksif task.status in _ACTIVE_TASK_STATUSES+ # The single cleanup consumer cannot be executing a user cleanup+ # concurrently. Later deliveries will skip the deleted account.+ and not (user_id is None and task.task_type == "user_delete")]- blockers: list[str] = []- waiting: list[str] = []+ deadline = time.monotonic() + _CANCEL_WAIT_SECONDSfor task in active:+ owner = {"account_id": task.account_id, "user_id": task.user_id}try:- await tracker.cancel(- task.task_id,- account_id=account_id,- user_id=user_id,- )- waiting.append(task.task_id)+ await tracker.cancel(task.task_id, **owner)except ValueError:- current = await tracker.get(- task.task_id,- account_id=account_id,- user_id=user_id,- )- if current is not None and current.status in _ACTIVE_TASK_STATUSES:- blockers.append(f"{current.task_id}({current.task_type})")- if blockers:- raise RuntimeError("Active tasks do not support cancellation: " + ", ".join(blockers))-- deadline = time.monotonic() + _CANCEL_WAIT_SECONDS- for task_id in waiting:+ # Some system work cannot be cancelled. Wait for its owner to+ # finish instead of deleting the data underneath it.+ pass+ for task in active:remaining = deadline - time.monotonic()if remaining <= 0:- raise TimeoutError("Timed out waiting for user tasks to stop")+ raise TimeoutError("Timed out waiting for tasks to stop")await tracker.wait(- task_id,- account_id=account_id,- user_id=user_id,- timeout=remaining,+ task.task_id, account_id=task.account_id, user_id=task.user_id, timeout=remaining)async def _delete_uploads(self, ctx: RequestContext) -> None:@@ -466,10 +396,10 @@ class UserDeletionService:await viking_fs.rm(uri, recursive=True, ctx=ctx)-class _UserDeletionProcessor(DequeueHandlerBase):+class _DeletionProcessor(DequeueHandlerBase):def __init__(self,- deletion_service: UserDeletionService,+ deletion_service: DeletionService,service_loop: asyncio.AbstractEventLoop,) -> None:self._deletion_service = deletion_service@@ -481,22 +411,24 @@ class _UserDeletionProcessor(DequeueHandlerBase):if isinstance(payload, str):payload = json.loads(payload)if not isinstance(payload, dict):- raise ValueError("Invalid user deletion message")+ raise ValueError("Invalid deletion message")target = payload.get("target")if not isinstance(target, dict):- raise ValueError("Invalid user deletion target")+ raise ValueError("Invalid deletion target")required = ("task_id", "account_id", "user_id")if any(not payload.get(field) for field in required):- raise ValueError("Invalid user deletion owner")- if not target.get("account_id") or not target.get("user_id"):- raise ValueError("Invalid user deletion target")+ raise ValueError("Invalid deletion owner")+ if not target.get("account_id") or "user_id" not in target:+ raise ValueError("Invalid deletion target")+ if target["user_id"] is not None and not target["user_id"]:+ raise ValueError("Invalid deletion user")return {"task_id": str(payload["task_id"]),"account_id": str(payload["account_id"]),"user_id": str(payload["user_id"]),"target": {"account_id": str(target["account_id"]),- "user_id": str(target["user_id"]),+ "user_id": str(target["user_id"]) if target["user_id"] is not None else None,},}@@ -512,17 +444,17 @@ class _UserDeletionProcessor(DequeueHandlerBase):return ProcessResult.success() if error is None else ProcessResult.failed(error)-async def setup_user_deletion(+async def setup_deletion(*,service: Any,manager: Any,oauth_store: Any = None,usage_audit_runtime: Any = None,-) -> Optional[UserDeletionService]:+) -> Optional[DeletionService]:"""Create the deletion service after storage and authentication are ready."""if manager is None or service.viking_fs is None or service._queue_manager is None:return None- deletion_service = UserDeletionService(+ deletion_service = DeletionService(service=service,manager=manager,service_loop=asyncio.get_running_loop(),diff --git a/openviking/service/task_store.py b/openviking/service/task_store.pyindex d410a2978..ed4381992 100644--- a/openviking/service/task_store.py+++ b/openviking/service/task_store.py@@ -71,8 +71,16 @@ class PersistentTaskStore:return json.loads(_decode_bytes(raw))async def list(self, account_id: str, *, user_id: Optional[str] = None) -> List[Dict[str, Any]]:- if not user_id:- return []+ if user_id is None:+ try:+ owners = await self._agfs.ls(self._task_root_dir(account_id))+ except (AGFSNotFoundError, FileNotFoundError):+ return []+ tasks: List[Dict[str, Any]] = []+ for owner in owners:+ if owner.get("isDir") and owner.get("name") not in (".", ".."):+ tasks.extend(await self.list(account_id, user_id=owner["name"]))+ return tasksdirectory = self._task_dir(account_id, user_id)try:items = await self._agfs.ls(directory)diff --git a/openviking/service/task_tracker.py b/openviking/service/task_tracker.pyindex 8722daa99..a8e6d691d 100644--- a/openviking/service/task_tracker.py+++ b/openviking/service/task_tracker.py@@ -902,6 +902,17 @@ class TaskTracker:return await self._dispatcher.run(load)+ def forget_account_tasks(self, account_id: str) -> None:+ """Invalidate snapshots after executions settle and account storage is removed."""+ with self._lock:+ task_ids = [+ task_id for task_id, task in self._tasks.items() if task.account_id == account_id+ ]+ for task_id in task_ids:+ del self._tasks[task_id]+ for task_id in task_ids:+ self._work_index.clear_failure(task_id)+async def delete_user_tasks(self, account_id: str, user_id: str) -> int:"""Delete terminal task records for one user from storage and cache."""self._validate_owner(account_id, user_id)@@ -1056,7 +1067,7 @@ class TaskTracker:task_type: Optional[str] = None,status: Optional[str] = None,resource_id: Optional[str] = None,- limit: int = 50,+ limit: Optional[int] = 50,account_id: Optional[str] = None,user_id: Optional[str] = None,include_internal: bool = True,@@ -1079,7 +1090,7 @@ class TaskTracker:task_type: Optional[str],status: Optional[str],resource_id: Optional[str],- limit: int,+ limit: Optional[int],account_id: Optional[str],user_id: Optional[str],include_internal: bool,diff --git a/openviking/storage/queuefs/queue_manager.py b/openviking/storage/queuefs/queue_manager.pyindex 3249b3ca2..b4d6f53b8 100644--- a/openviking/storage/queuefs/queue_manager.py+++ b/openviking/storage/queuefs/queue_manager.py@@ -91,7 +91,9 @@ class QueueManager:ADD_RESOURCE = "AddResource"SESSION_COMMIT = "SessionCommit"EXTERNAL_TASK = "ExternalTask"- USER_DELETION = "UserDeletion"+ # Account and user cleanup share one consumer. Retain the persisted name+ # so user cleanup messages queued before this change resume in place.+ DATA_CLEANUP = "UserDeletion"# Deferred work re-enqueues itself; throttle the next scheduling round._REQUEUE_POLL_INTERVAL = 1.0@@ -214,7 +216,7 @@ class QueueManager:def _max_concurrent_for_queue(self, queue_name: str) -> int:"""Return the worker concurrency limit for a named queue."""- if queue_name == self.USER_DELETION:+ if queue_name == self.DATA_CLEANUP:return 1if queue_name == self.EMBEDDING:return self._max_concurrent_embeddingdiff --git a/openviking/storage/vectordb/collection/http_collection.py b/openviking/storage/vectordb/collection/http_collection.pyindex 5dc9e2d3a..5388aa46c 100644--- a/openviking/storage/vectordb/collection/http_collection.py+++ b/openviking/storage/vectordb/collection/http_collection.py@@ -635,8 +635,7 @@ class HttpCollection(ICollection):timeout=DEFAULT_TIMEOUT,)# logger.info(f"SearchByScalar response: {response.text}")- if response.status_code != 200:- return SearchResult()+ response.raise_for_status()data = json.loads(response.text).get("data", {})result = SearchResult()@@ -675,8 +674,7 @@ class HttpCollection(ICollection):},timeout=DEFAULT_TIMEOUT,)- if response.status_code != 200:- return AggregateResult(agg={}, op=op, field=field)+ response.raise_for_status()result = json.loads(response.text)data = result.get("data", {})return self._parse_aggregate_result(data, op, field)diff --git a/openviking/storage/vectordb_adapters/base.py b/openviking/storage/vectordb_adapters/base.pyindex 4c7155f57..bfb79e2b0 100644--- a/openviking/storage/vectordb_adapters/base.py+++ b/openviking/storage/vectordb_adapters/base.py@@ -9,6 +9,7 @@ import mathimport randomimport uuidfrom abc import ABC, abstractmethod+from tempfile import TemporaryFilefrom typing import Any, Dict, Iterable, Optionalfrom urllib.parse import urlparse@@ -553,28 +554,39 @@ class CollectionAdapter(ABC):*,ids: Optional[list[str]] = None,filter: Optional[Dict[str, Any] | FilterExpr] = None,- limit: int = 100000,) -> int:+ """Submit IDs for deletion in batches, without waiting for index visibility."""coll = self.get_collection()- delete_ids = list(ids or [])- if not delete_ids and filter is not None:- matched = self.query(- filter=filter,- limit=limit,- output_fields=["id"],- )- delete_ids = [record["id"] for record in matched if record.get("id")]-- if not delete_ids:+ batch_size = self._DATA_BATCH_SIZE or 100+ if ids is not None:+ for start in range(0, len(ids), batch_size):+ coll.delete_data(ids[start : start + batch_size])+ return len(ids)+ if filter is None:return 0- batch_size = self._DATA_BATCH_SIZE- if batch_size and len(delete_ids) > batch_size:- for i in range(0, len(delete_ids), batch_size):- coll.delete_data(delete_ids[i : i + batch_size])- else:- coll.delete_data(delete_ids)- return len(delete_ids)+ # Enumerate before deleting so our own deletes cannot shift offset pages.+ # Spool only IDs to disk to keep memory bounded for large accounts.+ with TemporaryFile(mode="w+t", encoding="utf-8") as pending_ids:+ offset = 0+ while True:+ matched = self.query(+ filter=filter,+ limit=batch_size,+ offset=offset,+ output_fields=["id"],+ order_by="updated_at",+ order_desc=False,+ )+ if not matched:+ break+ pending_ids.write(json.dumps([record["id"] for record in matched]) + "\n")+ offset += len(matched)++ pending_ids.seek(0)+ for batch in pending_ids:+ coll.delete_data(json.loads(batch))+ return offset@staticmethoddef _coerce_int(value: Any) -> Optional[int]:@@ -611,7 +623,7 @@ class CollectionAdapter(ABC):if parsed_total is not None:return parsed_total- return 0+ raise RuntimeError("Vector backend returned an invalid count result")def search_by_keywords(self,diff --git a/openviking/storage/viking_fs/__init__.py b/openviking/storage/viking_fs/__init__.pyindex 3729bf16f..ccbda0cb9 100644--- a/openviking/storage/viking_fs/__init__.py+++ b/openviking/storage/viking_fs/__init__.py@@ -155,7 +155,7 @@ class VikingFS("vikingfs_bound_ctx", default=None)self._background_tasks: set = set()- self._user_deletion_guard: Optional[Callable[[str, str], bool]] = None+ self._deletion_guard: Optional[Callable[[str, str], bool]] = NoneVikingFS.__module__ = __name__diff --git a/openviking/storage/viking_fs/_access.py b/openviking/storage/viking_fs/_access.pyindex 9f7606b71..b16103156 100644--- a/openviking/storage/viking_fs/_access.py+++ b/openviking/storage/viking_fs/_access.py@@ -83,8 +83,8 @@ class _AccessMixin:}_NO_VECTOR_DERIVED = frozenset({".relations.json", ".ovgitignore"})- def set_user_deletion_guard(self, guard: Optional[Callable[[str, str], bool]]) -> None:- self._user_deletion_guard = guard+ def set_deletion_guard(self, guard: Optional[Callable[[str, str], bool]]) -> None:+ self._deletion_guard = guard@staticmethoddef _default_ctx() -> RequestContext:@@ -227,7 +227,7 @@ class _AccessMixin:if action is AclAction.READ:return- self._ensure_user_not_deleting(real_ctx)+ self._ensure_identity_not_deleting(real_ctx)for uri in uris:self._safe_uri_parts(uri)if uri == "viking://" and real_ctx.role == Role.USER:@@ -360,10 +360,10 @@ class _AccessMixin:async def delete_acl(self, uri: str, ctx: Optional[RequestContext] = None) -> Dict[str, Any]:return await self.set_acl(uri, [], acl_mode=AclMode.INHERIT, ctx=ctx)- def _ensure_user_not_deleting(self, ctx: RequestContext) -> None:- guard = getattr(self, "_user_deletion_guard", None)+ def _ensure_identity_not_deleting(self, ctx: RequestContext) -> None:+ guard = getattr(self, "_deletion_guard", None)if ctx.role != Role.ROOT and guard is not None and guard(ctx.account_id, ctx.user.user_id):- raise FailedPreconditionError("User deletion is in progress")+ raise FailedPreconditionError("Identity deletion is in progress")def _ensure_supported_delete_namespace(self, normalized_uri: str) -> None:parts = [p for p in normalized_uri[len("viking://") :].strip("/").split("/") if p]diff --git a/openviking/storage/viking_vector_index_backend.py b/openviking/storage/viking_vector_index_backend.pyindex a5bb4789d..5211b2bc8 100644--- a/openviking/storage/viking_vector_index_backend.py+++ b/openviking/storage/viking_vector_index_backend.py@@ -17,6 +17,7 @@ from openviking.core.namespace import (visible_roots,)from openviking.server.identity import RequestContext, Role+from openviking.service.task_tracker_concurrency import run_to_completionfrom openviking.storage.acl import (ACL_CONTEXT_FIELDS,ACL_MODE_FIELD,@@ -151,7 +152,9 @@ class _AsyncVectorAdapter:self._adapter = adapterasync def call(self, method_name: str, /, *args: Any, **kwargs: Any) -> Any:- return await asyncio.to_thread(getattr(self._adapter, method_name), *args, **kwargs)+ return await run_to_completion(+ lambda: asyncio.to_thread(getattr(self._adapter, method_name), *args, **kwargs)+ )async def run(self, func: Any, /, *args: Any, **kwargs: Any) -> Any:return await asyncio.to_thread(func, *args, **kwargs)diff --git a/tests/agfs/test_async_client.py b/tests/agfs/test_async_client.pyindex b5aa084e4..9c8b025d4 100644--- a/tests/agfs/test_async_client.py+++ b/tests/agfs/test_async_client.py@@ -1,69 +1,43 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0+import asyncio+import threading+import pytest-import openviking.pyagfs.async_client as async_clientfrom openviking.pyagfs import AsyncAGFSClient---class _SyncAGFS:- """Minimal synchronous binding stub used by the async adapter tests."""-- def read(self, path, **kwargs):- """Return read call arguments."""- return ("read", path, kwargs)-- def write(self, path, data, **kwargs):- """Return write call arguments."""- return ("write", path, data, kwargs)-- def rm(self, path, **kwargs):- """Return remove call arguments."""- return ("rm", path, kwargs)-- def pathlock_is_locked(self, ctx, path, ignore_stale):- """Return pathlock query arguments."""- return ("pathlock_is_locked", ctx, path, ignore_stale)+from openviking.storage.viking_vector_index_backend import _AsyncVectorAdapter@pytest.mark.asyncio-async def test_async_agfs_client_hides_threadpool(monkeypatch):- to_thread_calls = []-- async def fake_to_thread(func, *args, **kwargs):- to_thread_calls.append((func.__name__, args, kwargs))- return func(*args, **kwargs)-- monkeypatch.setattr(async_client.asyncio, "to_thread", fake_to_thread)-- sync_agfs = _SyncAGFS()- agfs = AsyncAGFSClient(sync_agfs)-- assert agfs._client is sync_agfs- assert await agfs.write("/tasks/1", b"data") == (- "write",- "/tasks/1",- b"data",- {"ctx": {"account_id": "_system"}},[email protected]("backend", ["agfs", "vector"])+async def test_async_agfs_client_hides_threadpool(backend):+ """Cancellation settles physical writes before cleanup may remove their data."""+ started, release = threading.Event(), threading.Event()+ writes = []++ class Writer:+ def write(self, path, data, **kwargs):+ started.set()+ if not release.wait(timeout=5):+ raise TimeoutError("Test did not release the write")+ writes.append((path, data))++ writer = Writer()+ operation = asyncio.create_task(+ AsyncAGFSClient(writer).write("/local/account/file", b"data")+ if backend == "agfs"+ else _AsyncVectorAdapter(writer).call("write", "record", b"data"))- assert await agfs.read("/queue/dequeue") == (- "read",- "/queue/dequeue",- {"ctx": {"account_id": "_system"}},- )- assert await agfs.rm("/redo/id", recursive=True) == (- "rm",- "/redo/id",- {"recursive": True, "ctx": {"account_id": "_system"}},- )-- assert to_thread_calls == [- ("write", ("/tasks/1", b"data"), {"ctx": {"account_id": "_system"}}),- ("read", ("/queue/dequeue",), {"ctx": {"account_id": "_system"}}),- (- "rm",- ("/redo/id",),- {"recursive": True, "ctx": {"account_id": "_system"}},- ),- ]+ try:+ assert await asyncio.to_thread(started.wait, 5)+ operation.cancel()+ await asyncio.sleep(0)+ await asyncio.sleep(0)+ assert not operation.done()+ finally:+ release.set()+ with pytest.raises(asyncio.CancelledError):+ await operation+ assert len(writes) == 1diff --git a/tests/resource/test_watch_scheduler.py b/tests/resource/test_watch_scheduler.pyindex fdad269e4..518ae4f4b 100644--- a/tests/resource/test_watch_scheduler.py+++ b/tests/resource/test_watch_scheduler.py@@ -62,6 +62,35 @@ class TestWatchSchedulerExecutionHold:assert len(resource_service.calls) == 1assert scheduler._executing_tasks == set()+ # A manual execution must also settle before account cleanup proceeds.+ started, cancelled, release = asyncio.Event(), asyncio.Event(), asyncio.Event()++ async def blocked_refresh(**kwargs):+ started.set()+ try:+ await asyncio.Event().wait()+ except asyncio.CancelledError:+ cancelled.set()+ await release.wait()+ raise++ resource_service.refresh_resource = blocked_refresh+ execution = asyncio.create_task(scheduler.schedule_task(task.task_id))+ await asyncio.wait_for(started.wait(), 1)+ other = await manager.create_task(+ path="https://example.com/other", to_uri="viking://resources/other",+ account_id="other", user_id="user", watch_interval=5,+ )+ deletion = asyncio.create_task(scheduler.delete_tasks(task.account_id))+ await asyncio.wait_for(cancelled.wait(), 1)+ assert not deletion.done()+ release.set()+ await asyncio.wait_for(deletion, 1)+ await asyncio.gather(execution, return_exceptions=True)+ assert await manager.get_task(task.task_id) is None+ assert await manager.get_task(other.task_id) is not None+ assert scheduler.executing_tasks == set()+@pytest.mark.asyncioasync def test_stuck_ingestion_does_not_block_later_scheduler_passes(self, monkeypatch):second_started = asyncio.Event()diff --git a/tests/server/oauth/test_storage.py b/tests/server/oauth/test_storage.pyindex 6be8013cf..fcdb08412 100644--- a/tests/server/oauth/test_storage.py+++ b/tests/server/oauth/test_storage.py@@ -301,7 +301,7 @@ async def test_access_token_expired_invisible(store):@pytest.mark.asyncio-async def test_revoke_user_tokens_cascades(store):+async def test_revoke_tokens_cascades(store):"""Revoking a user wipes their access, refresh, and unused codes."""await store.insert_access(token_plain="at-1",@@ -372,7 +372,7 @@ async def test_revoke_user_tokens_cascades(store):verified_key_fp=_FP_OTHER,)- counts = await store.revoke_user_tokens(account_id="acct", user_id="alice")+ counts = await store.revoke_tokens(account_id="acct", user_id="alice")assert counts["access_tokens_revoked"] == 1assert counts["refresh_tokens_revoked"] == 1assert counts["codes_revoked"] == 1@@ -385,6 +385,12 @@ async def test_revoke_user_tokens_cascades(store):assert await store.load_pending_authorization(alice_pending) is Noneassert await store.load_pending_authorization(bob_pending) is not None+ account_counts = await store.revoke_tokens(account_id="acct")+ assert account_counts["access_tokens_revoked"] == 1+ assert account_counts["pending_authorizations_revoked"] == 1+ assert await store.load_access("at-other") is None+ assert await store.load_pending_authorization(bob_pending) is None+@pytest.mark.asyncioasync def test_gc_keeps_refresh_tombstones_until_natural_expiry(store):diff --git a/tests/server/test_admin_api.py b/tests/server/test_admin_api.pyindex 574375046..d4feeeec9 100644--- a/tests/server/test_admin_api.py+++ b/tests/server/test_admin_api.py@@ -31,25 +31,12 @@ from openviking.server.user_config import (user_config_backup_uri,)from openviking.service.core import OpenVikingService+from openviking.service.deletion import setup_deletionfrom openviking.service.task_store import (SYSTEM_TASK_ACCOUNT_ID,SYSTEM_TASK_USER_ID,)from openviking.service.task_tracker import get_task_tracker-from openviking.service.user_deletion import setup_user_deletion-from openviking.session.memory.account_templates import (- EDITABLE_MEMORY_TEMPLATE_FIELDS,- account_memory_template_path,- resolve_account_memory_registry,-)-from openviking.session.memory.extract_loop import ExtractLoop-from openviking.session.memory.memory_isolation_handler import MemoryIsolationHandler-from openviking.session.memory.memory_type_registry import (- MemoryTypeRegistry,- get_default_registry,-)-from openviking.session.memory.patch_merge_context_provider import PatchMergeContextProvider-from openviking.session.memory.session_extract_context_provider import SessionExtractContextProviderfrom openviking_cli.exceptions import OpenVikingError, PermissionDeniedErrorfrom openviking_cli.session.user_id import UserIdentifierfrom openviking_cli.utils.config import get_openviking_config@@ -199,13 +186,9 @@ async def lightweight_admin_client(lightweight_admin_app):@pytest_asyncio.fixture(scope="function")-async def admin_service(temp_dir):- svc = OpenVikingService(- path=str(temp_dir / "admin_data"), user=UserIdentifier.the_default_user("admin_user")- )- await svc.initialize()- yield svc- await svc.close()+async def admin_service(service):+ """Use the shared service fixture with local fake models."""+ yield service@pytest_asyncio.fixture(scope="function")@@ -220,7 +203,7 @@ async def admin_app(admin_service):manager = APIKeyManager(root_key=ROOT_KEY, viking_fs=admin_service.viking_fs)await manager.load()app.state.api_key_manager = manager- app.state.user_deletion_service = await setup_user_deletion(+ app.state.deletion_service = await setup_deletion(service=admin_service,manager=manager,)@@ -1718,26 +1701,176 @@ async def test_identity_settings_without_watcher_use_scoped_registry_reads(]-async def test_delete_account(admin_client: httpx.AsyncClient):- """ROOT can delete an account."""[email protected]("index_updates_immediately", [True, False])+async def test_delete_account(+ admin_client, admin_service, admin_app, monkeypatch, index_updates_immediately+):+ """Cleanup submits all IDs without depending on immediate query consistency."""+ from itertools import islice+ from types import SimpleNamespace++ from openviking.storage.expr import Eq+ from openviking.storage.queuefs.queue_manager import QueueManager+ from openviking.storage.vectordb_adapters.local_adapter import LocalCollectionAdapter+ from openviking.storage.viking_vector_index_backend import _SingleAccountBackend+acct = _uid()resp = await admin_client.post("/api/v1/admin/accounts",json={"account_id": acct, "admin_user_id": "alice"},headers=root_headers(),)- user_key = resp.json()["result"]["user_key"]+ key = resp.json()["result"]["user_key"]+ path = f"/local/{acct}/resources/project/nested/file.md"+ await _agfs_write(admin_service, path, "project data")+ await _agfs_write(admin_service, "/local/other/resources/keep.md", "keep")+ old_task = await get_task_tracker().create(+ "session_commit", account_id=acct, user_id="alice", resource_id="old-session"+ )+ user_cleanup = await get_task_tracker().create(+ "user_delete", account_id=acct, user_id="alice", resource_id=f"{acct}/bob"+ )- resp = await admin_client.delete(f"/api/v1/admin/accounts/{acct}", headers=root_headers())- assert resp.status_code == 200- assert resp.json()["result"]["deleted"] is True+ manager = admin_app.state.api_key_manager- # User key should now be invalid- resp = await admin_client.get(- "/api/v1/fs/ls?uri=viking://",- headers={"X-API-Key": user_key},+ # Exercise the real filter-deletion loop with bounded in-memory I/O.+ rows = {str(i): {"id": str(i), "account_id": acct} for i in range(100_001)}+ rows["keep"] = {"id": "keep", "account_id": "other"}+ indexed_rows = rows.copy()+ deleted_ids = []+ adapter = LocalCollectionAdapter("context", "", "default")++ def delete_data(ids):+ assert 0 < len(ids) <= 100+ deleted_ids.extend(ids)+ for record_id in ids:+ rows.pop(record_id, None)++ adapter._collection = SimpleNamespace(delete_data=delete_data)+ scan_rows = rows if index_updates_immediately else indexed_rows+ adapter.query = lambda *, filter, limit, offset=0, **kwargs: list(+ islice(+ (row for row in scan_rows.values() if row[filter.field] == filter.value),+ offset,+ offset + limit,+ ))- assert resp.status_code == 401+ # Other read APIs may still expose data after delete requests succeed.+ adapter.get = lambda ids: [+ indexed_rows[record_id] for record_id in ids if record_id in indexed_rows+ ]+ adapter.count = lambda filter: sum(+ row[filter.field] == filter.value for row in indexed_rows.values()+ )+ vectors = admin_service.viking_fs.vector_store+ vectors._root_backend = _SingleAccountBackend(+ vectors._config, bound_account_id=None, shared_adapter=adapter+ )+ original_delete = vectors.delete_account_data+ started, release = asyncio.Event(), asyncio.Event()++ async def fail_vectors(account_id, *, ctx):+ started.set()+ await release.wait()+ raise RuntimeError("injected vector failure")++ monkeypatch.setattr(vectors, "delete_account_data", fail_vectors)++ resp = await admin_client.delete(f"/api/v1/admin/accounts/{acct}", headers=root_headers())+ assert resp.status_code == 202+ result = resp.json()["result"]+ assert result["status"] == "deleting"+ task_id = result["task_id"]+ await asyncio.wait_for(started.wait(), 10)+ duplicate = await admin_client.delete(f"/api/v1/admin/accounts/{acct}", headers=root_headers())+ assert duplicate.json()["result"]["task_id"] == task_id+ denied = await admin_client.get("/api/v1/fs/ls?uri=viking://", headers={"X-API-Key": key})+ assert denied.status_code == 401+ conflict = await admin_client.post(+ "/api/v1/admin/accounts",+ json={"account_id": acct, "admin_user_id": "new"},+ headers=root_headers(),+ )+ assert conflict.status_code == 409+ release.set()+ failed = await _wait_for_task(admin_client, task_id)+ assert failed["status"] == "failed"+ assert "injected vector failure" in failed["error"]+ assert await _agfs_exists(admin_service, path)+ await manager.reload()+ assert manager.get_deletion(acct)["task_id"] == task_id++ retried = await admin_client.delete(f"/api/v1/admin/accounts/{acct}", headers=root_headers())+ retry_task_id = retried.json()["result"]["task_id"]+ assert retry_task_id != task_id+ assert (await _wait_for_task(admin_client, retry_task_id))["status"] == "failed"++ monkeypatch.setattr(vectors, "delete_account_data", original_delete)+ # Reconcile a durable fence whose previous process stopped before enqueue.+ retry = await manager.replace_deletion_task(+ acct,+ None,+ expected_task_id=retry_task_id,+ task_id=str(uuid.uuid4()),+ owner_account_id=SYSTEM_TASK_ACCOUNT_ID,+ owner_user_id=SYSTEM_TASK_USER_ID,+ )+ await admin_app.state.deletion_service.initialize()+ completed = await _wait_for_task(admin_client, retry["task_id"])+ assert completed["status"] == "completed", completed+ assert completed["result"] == {"deleted": True}+ assert set(rows) == {"keep"}+ assert len(deleted_ids) == len(set(deleted_ids)) == 100_001+ assert adapter.count(Eq("account_id", acct)) == 100_001+ assert adapter.get(deleted_ids[:100])+ assert not await _agfs_exists(admin_service, f"/local/{acct}")+ assert await _agfs_exists(admin_service, "/local/other/resources/keep.md")+ assert manager.get_deletion(acct) is None+ assert not manager.has_user(acct, "alice")+ assert await get_task_tracker().get(old_task.task_id, account_id=acct, user_id="alice") is None+ assert await get_task_tracker().get(user_cleanup.task_id, account_id=acct, user_id="alice") is None+ assert (await _wait_for_task(admin_client, task_id))["status"] == "failed"++ # Late cleanup deliveries are harmless after deletion, including when the+ # same account/user IDs have since been recreated.+ queue = admin_service._queue_manager.get_queue(QueueManager.DATA_CLEANUP)+ for recreated in (False, True):+ if recreated:+ replacement_key = await manager.create_account(acct, "bob")+ await _agfs_write(admin_service, path, "new data")+ late_task_id = str(uuid.uuid4())+ await get_task_tracker().create(+ "user_delete",+ task_id=late_task_id,+ resource_id=f"{acct}/bob",+ account_id=SYSTEM_TASK_ACCOUNT_ID,+ user_id=SYSTEM_TASK_USER_ID,+ )+ await queue.enqueue(+ {+ "task_id": user_cleanup.task_id,+ "account_id": acct,+ "user_id": "alice",+ "target": {"account_id": acct, "user_id": "bob"},+ }+ )+ await queue.enqueue(+ {+ "task_id": late_task_id,+ "account_id": SYSTEM_TASK_ACCOUNT_ID,+ "user_id": SYSTEM_TASK_USER_ID,+ "target": {"account_id": acct, "user_id": "bob"},+ }+ )+ skipped = await _wait_for_task(admin_client, late_task_id)+ assert skipped["status"] == "completed"+ assert skipped["result"] == {"deleted": not recreated, "stale": True}+ assert await get_task_tracker().get(+ user_cleanup.task_id, account_id=acct, user_id="alice"+ ) is None+ assert await _agfs_exists(admin_service, path) is recreated+ if recreated:+ assert manager.resolve(replacement_key).user_id == "bob"async def test_create_duplicate_account_fails(admin_client: httpx.AsyncClient):diff --git a/tests/server/test_api_key_manager.py b/tests/server/test_api_key_manager.pyindex 478c33c6a..dcccef29d 100644--- a/tests/server/test_api_key_manager.py+++ b/tests/server/test_api_key_manager.py@@ -14,7 +14,6 @@ from openviking.pyagfs.exceptions import AGFSNotFoundErrorfrom openviking.server.api_keys import APIKeyManagerfrom openviking.server.api_keys.legacy import ACCOUNTS_PATH, USERS_PATH_TEMPLATEfrom openviking.server.identity import Role-from openviking.service.core import OpenVikingServicefrom openviking_cli.exceptions import (AlreadyExistsError,InvalidArgumentError,@@ -22,7 +21,6 @@ from openviking_cli.exceptions import (PermissionDeniedError,UnauthenticatedError,)-from openviking_cli.session.user_id import UserIdentifierdef _uid() -> str:@@ -38,14 +36,9 @@ ROOT_KEY = "test-root-key-abcdef1234567890abcdef1234567890"@pytest_asyncio.fixture(scope="function")-async def manager_service(temp_dir):- """OpenVikingService for APIKeyManager tests."""- svc = OpenVikingService(- path=str(temp_dir / "mgr_data"), user=UserIdentifier.the_default_user("mgr_user")- )- await svc.initialize()- yield svc- await svc.close()+async def manager_service(service):+ """Use the shared service fixture with local fake models."""+ yield service@pytest_asyncio.fixture(scope="function")@@ -326,7 +319,7 @@ async def test_scoped_store_refresh_observes_another_manager(await replica.load()acct = _uid()- await manager.create_account(acct, "alice")+ key = await manager.create_account(acct, "alice")await replica.refresh_accounts_from_store()account = next(item for item in replica.get_accounts() if item["account_id"] == acct)assert account["user_count"] == 0@@ -338,6 +331,14 @@ async def test_scoped_store_refresh_observes_another_manager(account = next(item for item in replica.get_accounts() if item["account_id"] == acct)assert account["user_count"] == 1+ deletion, _ = await manager.begin_deletion(+ acct, None, task_id="delete-account", owner_account_id="_system", owner_user_id="root"+ )+ await replica.refresh_accounts_from_store()+ assert replica.get_deletion(acct) == deletion+ with pytest.raises(UnauthenticatedError):+ replica.resolve(key)+async def test_account_only_refresh_loads_groups_before_group_write(manager_service):"""A partially discovered account must not overwrite persisted groups."""@@ -389,6 +390,7 @@ async def test_account_only_refresh_discards_recreated_account_state("account_id": acct,"created_at": "recreated","user_count": 0,+ "status": "active",}with pytest.raises(UnauthenticatedError):manager.resolve(old_key)@@ -498,7 +500,11 @@ async def test_management_registration_does_not_replace_a_trusted_user(verifier = APIKeyManager(root_key=ROOT_KEY, viking_fs=manager_service.viking_fs)await verifier.load()assert verifier.get_users(acct, expose_key=True) == [- {"user_id": "admin", "role": "admin", "api_key": manager._accounts[acct].users["admin"]["key"]},+ {+ "user_id": "admin",+ "role": "admin",+ "api_key": manager._accounts[acct].users["admin"]["key"],+ },{"user_id": "alice", "role": "user"},]@@ -570,15 +576,46 @@ async def test_create_account_rolls_back_when_user_persistence_fails(async def test_delete_account(manager: APIKeyManager):- """Deleting account should invalidate all its user keys."""+ """The durable account fence revokes keys and survives reload until its owner finishes."""+ from openviking_cli.exceptions import FailedPreconditionError+acct = _uid()key = await manager.create_account(acct, "alice")- identity = manager.resolve(key)- assert identity.account_id == acct+ assert manager.resolve(key).account_id == acct+ deletion, created = await manager.begin_deletion(+ acct, None, task_id="delete-account-1", owner_account_id="_system", owner_user_id="root"+ )+ assert created+ with pytest.raises(UnauthenticatedError):+ manager.resolve(key)+ assert manager.get_user_key_fingerprint(acct, "alice") is None+ with pytest.raises(AlreadyExistsError):+ await manager.create_account(acct, "bob")+ with pytest.raises(FailedPreconditionError):+ await manager.register_user(acct, "bob")+ with pytest.raises(FailedPreconditionError):+ await manager.regenerate_key(acct, "alice")+ await manager.ensure_trusted_identities({acct: {"trusted-user"}})+ assert not manager.has_user(acct, "trusted-user")- await manager.delete_account(acct)+ await manager.reload()+ assert manager.get_deletion(acct) == deletionwith pytest.raises(UnauthenticatedError):manager.resolve(key)+ assert await manager.finish_deletion(acct, None, "stale-task") is False+ replacement = await manager.replace_deletion_task(+ acct,+ None,+ expected_task_id="delete-account-1",+ task_id="delete-account-2",+ owner_account_id="_system",+ owner_user_id="root",+ )+ assert replacement["task_id"] == "delete-account-2"+ assert await manager.finish_deletion(acct, None, "delete-account-1") is False+ assert await manager.finish_deletion(acct, None, "delete-account-2") is True+ assert not manager.has_user(acct, "alice")+ assert manager.get_deletion(acct) is Noneasync def test_recreated_account_does_not_restore_deleted_users(manager: APIKeyManager):@@ -697,7 +734,7 @@ async def test_user_deletion_fence_revokes_key_and_rejects_stale_finish(await manager.create_account(acct, "alice")bob_key = await manager.register_user(acct, "bob", "user")- deletion, created = await manager.begin_user_deletion(+ deletion, created = await manager.begin_deletion(acct,"bob",task_id="delete-1",@@ -707,18 +744,18 @@ async def test_user_deletion_fence_revokes_key_and_rejects_stale_finish(assert created is Trueassert deletion["task_id"] == "delete-1"- assert manager.is_user_deleting(acct, "bob")+ assert manager.is_deleting(acct, "bob")with pytest.raises(UnauthenticatedError):manager.resolve(bob_key)with pytest.raises(AlreadyExistsError):await manager.register_user(acct, "bob", "user")- assert await manager.finish_user_deletion(acct, "bob", "stale") is False+ assert await manager.finish_deletion(acct, "bob", "stale") is Falseassert manager.has_user(acct, "bob")- assert await manager.finish_user_deletion(acct, "bob", "delete-1") is True+ assert await manager.finish_deletion(acct, "bob", "delete-1") is Trueassert not manager.has_user(acct, "bob")new_key = await manager.register_user(acct, "bob", "user")- assert await manager.finish_user_deletion(acct, "bob", "delete-1") is False+ assert await manager.finish_deletion(acct, "bob", "delete-1") is Falseassert manager.resolve(new_key).user_id == "bob"async def test_regenerate_key(manager: APIKeyManager):"""Regenerating key should invalidate old key and return new valid key."""@@ -759,7 +796,7 @@ async def test_get_user_key_fingerprint_changes_on_rotation(manager: APIKeyManagassert fp2 != fp1# Deletion fence immediately removes the fingerprint.- await manager.begin_user_deletion(+ await manager.begin_deletion(acct,"bob",task_id="delete-1",@@ -803,7 +840,7 @@ async def test_get_users(manager: APIKeyManager):assert roles["alice"] == "admin"assert roles["bob"] == "user"- await manager.begin_user_deletion(+ await manager.begin_deletion(acct,"bob",task_id="delete-bob",@@ -930,14 +967,14 @@ async def test_user_and_group_persistence_across_reload(manager_service):assert identity.role == Role.ADMINassert mgr2.get_user_group_ids(acct, "bob") == (group_id,)- await mgr2.begin_user_deletion(+ await mgr2.begin_deletion(acct,"bob",task_id="delete-bob",owner_account_id=acct,owner_user_id="alice",)- await mgr2.finish_user_deletion(acct, "bob", "delete-bob")+ await mgr2.finish_deletion(acct, "bob", "delete-bob")mgr3 = APIKeyManager(root_key=ROOT_KEY, viking_fs=manager_service.viking_fs)await mgr3.load()@@ -1539,14 +1576,14 @@ async def test_reload_reflects_removed_user(manager_service):# Reader accepts bob at first.assert reader.resolve(user_key).user_id == "bob"- await writer.begin_user_deletion(+ await writer.begin_deletion(acct,"bob",task_id="delete-1",owner_account_id=acct,owner_user_id="alice",)- await writer.finish_user_deletion(acct, "bob", "delete-1")+ await writer.finish_deletion(acct, "bob", "delete-1")await reader.reload()with pytest.raises(UnauthenticatedError):diff --git a/web-studio/src/gen/ov-client/sdk.gen.ts b/web-studio/src/gen/ov-client/sdk.gen.tsindex c76211b6c..a8f2cfa03 100644--- a/web-studio/src/gen/ov-client/sdk.gen.ts+++ b/web-studio/src/gen/ov-client/sdk.gen.ts@@ -117,7 +117,7 @@ export const postAdminAccounts = <ThrowOnError extends boolean = false>(options:/*** Delete Account*- * Delete an account and cascade-clean its storage (AGFS + VectorDB).+ * Revoke an account and submit durable cleanup of its data.*/export const deleteAdminAccountByAccountId = <ThrowOnError extends boolean = false>(options: Options<DeleteAdminAccountByAccountIdData, ThrowOnError>) => (options.client ?? client).delete<DeleteAdminAccountByAccountIdResponses, DeleteAdminAccountByAccountIdErrors, ThrowOnError>({responseType: 'json',diff --git a/web-studio/src/gen/ov-client/types.gen.ts b/web-studio/src/gen/ov-client/types.gen.tsindex 700596ebe..33dd0826d 100644--- a/web-studio/src/gen/ov-client/types.gen.ts+++ b/web-studio/src/gen/ov-client/types.gen.ts@@ -1074,7 +1074,7 @@ export type DeleteAdminAccountByAccountIdResponses = {/*** Successful Response*/- 200: unknown;+ 202: unknown;};export type GetAdminAccountIdUsersData = {diff --git a/web-studio/src/i18n/locales/en/workspace.ts b/web-studio/src/i18n/locales/en/workspace.tsindex 36982bbdb..623402be0 100644--- a/web-studio/src/i18n/locales/en/workspace.ts+++ b/web-studio/src/i18n/locales/en/workspace.ts@@ -914,9 +914,9 @@ const workspace = {},toast: {accountCreated: 'Account created',- accountDeleted: '{{account}} deleted',- accountDeletedRecoveryFailed:- 'The account was deleted, but the remaining account list could not be loaded: {{error}}',+ accountDeletionStarted: '{{account}} disabled. Cleanup task: {{taskId}}',+ accountDeletionRecoveryFailed:+ 'Account cleanup was submitted, but the remaining account list could not be loaded: {{error}}',connectionSaved: 'Connection saved',copyFailed: 'Copy failed',copied: 'Copied',diff --git a/web-studio/src/i18n/locales/zh-CN/workspace.ts b/web-studio/src/i18n/locales/zh-CN/workspace.tsindex 4238b8417..eef49c0d7 100644--- a/web-studio/src/i18n/locales/zh-CN/workspace.ts+++ b/web-studio/src/i18n/locales/zh-CN/workspace.ts@@ -874,9 +874,9 @@ const workspace = {},toast: {accountCreated: '账号已创建',- accountDeleted: '{{account}} 已删除',- accountDeletedRecoveryFailed:- '账号已删除,但无法加载剩余账号列表:{{error}}',+ accountDeletionStarted: '{{account}} 已停用,后台清理任务:{{taskId}}',+ accountDeletionRecoveryFailed:+ '账号清理已提交,但无法加载剩余账号列表:{{error}}',connectionSaved: '连接已保存',copyFailed: '复制失败',copied: '已复制',diff --git a/web-studio/src/lib/admin.ts b/web-studio/src/lib/admin.tsindex ad2b001b5..4f5cb2790 100644--- a/web-studio/src/lib/admin.ts+++ b/web-studio/src/lib/admin.ts@@ -298,7 +298,7 @@ export async function probeStudioConnection(}function normalizeAccount(value: unknown): AdminAccount | null {- if (!isRecord(value)) {+ if (!isRecord(value) || value.status === 'deleting') {return null}@@ -397,8 +397,8 @@ export async function createAdminAccount(export async function deleteAdminAccount(connection: AdminConnection,accountId: string,-): Promise<void> {- await getOvResult<unknown>(+): Promise<string> {+ const result = await getOvResult<{ task_id: string }>(deleteAdminAccountByAccountId({client: createAdminClient(connection),path: {@@ -406,6 +406,7 @@ export async function deleteAdminAccount(},}),)+ return result.task_id}export async function createAdminUser(diff --git a/web-studio/src/routes/users/-components/delete-account-button.test.tsx b/web-studio/src/routes/users/-components/delete-account-button.test.tsxindex 2acc9dd8e..96999c60c 100644--- a/web-studio/src/routes/users/-components/delete-account-button.test.tsx+++ b/web-studio/src/routes/users/-components/delete-account-button.test.tsx@@ -66,7 +66,7 @@ function renderButton() {}beforeEach(() => {- adminMocks.deleteAdminAccount.mockResolvedValue(undefined)+ adminMocks.deleteAdminAccount.mockResolvedValue('cleanup-task')adminMocks.fetchAdminAccounts.mockResolvedValue([{ accountId: 'account-b', userCount: 1 },])@@ -108,7 +108,7 @@ describe('DeleteAccountButton', () => {expect(connectionMocks.switchManagementAccount).toHaveBeenCalledWith('account-b',)- expect(toastMocks.success).toHaveBeenCalledWith('toast.accountDeleted')+ expect(toastMocks.success).toHaveBeenCalledWith('toast.accountDeletionStarted')})it('clears the deleted identity and opens settings when no account remains', async () => {diff --git a/web-studio/src/routes/users/-components/delete-account-button.tsx b/web-studio/src/routes/users/-components/delete-account-button.tsxindex eb83c6f09..6b0b79390 100644--- a/web-studio/src/routes/users/-components/delete-account-button.tsx+++ b/web-studio/src/routes/users/-components/delete-account-button.tsx@@ -37,14 +37,14 @@ export function DeleteAccountButton({const deleteAccount = useMutation({mutationFn: () => deleteAdminAccount(adminConnection, accountId),onError: (error) => toast.error(getErrorMessage(error)),- onSuccess: async () => {+ onSuccess: async (taskId) => {let nextAccountId = ''try {const remainingAccounts = await fetchAdminAccounts(adminConnection)nextAccountId = remainingAccounts[0]?.accountId ?? ''} catch (error) {toast.warning(- t('toast.accountDeletedRecoveryFailed', {+ t('toast.accountDeletionRecoveryFailed', {error: getErrorMessage(error),}),)@@ -55,7 +55,7 @@ export function DeleteAccountButton({await queryClient.invalidateQueries({ queryKey: ['account-switcher'] })setOpen(false)setConfirmation('')- toast.success(t('toast.accountDeleted', { account: accountId }))+ toast.success(t('toast.accountDeletionStarted', { account: accountId, taskId }))if (!nextAccountId) {openConnectionSettings()