Skip to content

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

fix(server): preserve MySQL isolation and legacy API compatibility (#6674)

runyunzhoub34a7dc48423
diff --git a/Dockerfile b/Dockerfile
index 9694bfd3f..c2d4038d2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -83,13 +83,13 @@ RUN --mount=type=cache,target=/root/.cache/uv,id=uv-${TARGETPLATFORM} \
fi; \
case "${UV_LOCK_STRATEGY}" in \
locked) \
- uv sync --locked --no-editable --reinstall-package openviking --extra bot --extra gemini --extra opengauss --extra expiry \
+ uv sync --locked --no-editable --reinstall-package openviking --extra bot --extra gemini --extra opengauss --extra expiry --extra mysql --extra redis-mysql \
;; \
auto) \
if ! uv lock --check; then \
uv lock; \
fi; \
- uv sync --locked --no-editable --reinstall-package openviking --extra bot --extra gemini --extra opengauss --extra expiry \
+ uv sync --locked --no-editable --reinstall-package openviking --extra bot --extra gemini --extra opengauss --extra expiry --extra mysql --extra redis-mysql \
;; \
*) \
echo "Unsupported UV_LOCK_STRATEGY: ${UV_LOCK_STRATEGY}" >&2; \
diff --git a/docs/en/api/02-resources.md b/docs/en/api/02-resources.md
index bb4b52b5e..f3b6657cb 100644
--- a/docs/en/api/02-resources.md
+++ b/docs/en/api/02-resources.md
@@ -83,7 +83,7 @@ Source Input -> Parse -> Resource Tree Build -> Persistence -> Semantic Processi
OpenViking first checks whether the source and request parameters qualify for Connector ingestion. Connector runs ingestion outside the standard pipeline. Otherwise, formats selected by [`parser_api`](../guides/01-configuration.md#parser-api) use the external Understanding parser; remaining formats use built-in parsers. External parsing replaces only the Parse stage and returns to TreeBuilder and the standard storage/semantic pipeline. Feishu documents already normalized to Markdown by an accessor bypass external parsing.
-Connector requires an exact `to` target and does not support `wait=true`. A `tos://` source cannot fall back to a built-in accessor. Git may fall back when Connector does not apply, except when Connector-only credentials were supplied. For supported standard-pipeline requests, `wait=true` waits for work belonging to this import, not unrelated work in the global queues; an HTTP request timeout does not imply that the import was cancelled. There is no per-import `timeout` parameter; configure HTTP timeouts on the client.
+Connector requires an exact `to` target and does not support `wait=true`. A `tos://` source cannot fall back to a built-in accessor. Git may fall back when Connector does not apply, except when Connector-only credentials were supplied. For supported standard-pipeline requests, `wait=true` waits for work belonging to this import, not unrelated work in the global queues; an HTTP request timeout does not imply that the import was cancelled. The legacy per-import `timeout` parameter is deprecated: requests still accept it, but ignore its value. Configure HTTP timeouts on the client.
#### Stage 1: Parse
- Uses `UnifiedResourceProcessor` to parse content based on resource type
@@ -187,6 +187,7 @@ This endpoint is the core entry point for resource management. It supports vario
| reason | string | No | "" | Reason for adding the resource. When non-empty, OpenViking runs it through the normal session memory extraction pipeline with the resource URI and records resource references in the resulting memory |
| instruction | string | No | "" | Processing instructions for semantic extraction (experimental feature) |
| wait | bool | No | False | Whether to wait for semantic processing and vectorization to complete before returning |
+| timeout | float | No | None | Deprecated: accepted for compatibility and ignored; configure HTTP timeouts on the client |
| strict | bool | No | False | Whether to use strict mode |
| ignore_dirs | string | No | None | Directory names to ignore (comma-separated) |
| include | string | No | None | File patterns to include (glob) |
diff --git a/docs/en/api/03-filesystem.md b/docs/en/api/03-filesystem.md
index 3d0955e32..3e91926b8 100644
--- a/docs/en/api/03-filesystem.md
+++ b/docs/en/api/03-filesystem.md
@@ -602,6 +602,7 @@ Invalid URI formats, unsupported schemes, and internal scopes such as `temp` or
|-----------|------|----------|---------|-------------|
| uri | str | Yes | - | Viking URI to remove |
| wait | bool | No | False | Wait for the semantic refresh after deletion |
+| timeout | float | No | None | Deprecated: accepted for compatibility and ignored; configure HTTP timeouts on the client |
| recursive | bool | No | False | Remove directory recursively |
diff --git a/docs/en/api/04-skills.md b/docs/en/api/04-skills.md
index 55da4f491..be34e4837 100644
--- a/docs/en/api/04-skills.md
+++ b/docs/en/api/04-skills.md
@@ -189,6 +189,7 @@ This endpoint stores Skill instructions and supporting files, then generates sum
| data | Any | No | - | Inline SKILL.md, structured/MCP data, or Git URL; choose this or `temp_file_id` |
| temp_file_id | str | No | - | Temporary upload file ID (from `temp_upload`); choose this or `data` |
| wait | bool | No | False | Wait for skill processing to complete |
+| timeout | float | No | None | Deprecated: accepted for compatibility and ignored; configure HTTP timeouts on the client |
| telemetry | TelemetryRequest | No | False | Whether to return telemetry data |
| target_uri | str | No | Target resolution order | Skills root, such as `viking://~/skills` or `viking://agent/skills` |
| skills | List[str] | No | `[]` | Select directory names from a directory/Git collection; empty or `["*"]` selects all |
@@ -704,6 +705,7 @@ Missing names/descriptions and invalid YAML produce errors. Directory/name misma
| `target_uri` | Unspecified | Skills root to try first; see read resolution fallback |
| `source_metadata` | Generated automatically | Updated source tracking |
| `wait` | `false` | Wait for background processing |
+| `timeout` | `null` | Deprecated: accepted and ignored |
| `telemetry` | `false` | Telemetry configuration |
Supply new content/an upload, or set `from_source=true`. The new name must match `skill_name` in the URL; update is neither rename nor a partial patch. The server parses and checks the new package before backing up and replacing the old one, and attempts restoration on synchronous failure. Include all auxiliary files that should remain in the replacement package.
diff --git a/docs/en/api/07-system.md b/docs/en/api/07-system.md
index b656b4eaf..8453bdd5d 100644
--- a/docs/en/api/07-system.md
+++ b/docs/en/api/07-system.md
@@ -342,7 +342,7 @@ Wait until the processing queues are drained. This check is not scoped to the ca
**Parameters**
-None. HTTP requests use the client timeout configuration.
+No parameters are required. The optional JSON body accepts legacy `timeout`, which is deprecated and ignored. Empty-body calls continue to work. Configure HTTP timeouts on the client.
#### 3. Usage Examples
diff --git a/docs/en/api/12-content.md b/docs/en/api/12-content.md
index 48b668996..33f91b8fa 100644
--- a/docs/en/api/12-content.md
+++ b/docs/en/api/12-content.md
@@ -218,6 +218,7 @@ Write a file and automatically refresh related semantics and vectors.
| content | str | Yes | - | New content to write |
| mode | str | No | `replace` | `replace` overwrites an existing file or creates a missing file; `append` appends to an existing file or creates a missing file; `create` is a compatibility alias for `replace` |
| wait | bool | No | `false` | Wait for background semantic/vector refresh |
+| timeout | float | No | None | Deprecated: accepted for compatibility and ignored; configure HTTP timeouts on the client |
| tags | string[] | No | Unset | Explicit retrieval tags for the written file, for example `["team=search", "env=prod"]` |
| tag_mode | string | No | `replace` | Tag update mode: `replace` overwrites tags, `append` merges by key, and `clear` removes existing tags without requiring `tags` |
@@ -345,6 +346,7 @@ Write multiple files below one Resource or Memory directory, then refresh the af
| `root_uri` | string | Yes | - | Existing Resource or Memory directory containing every target |
| `operations` | array | Yes | - | File writes to validate and apply |
| `wait` | boolean | No | `true` | Wait for semantic/vector refresh |
+| timeout | float | No | None | Deprecated: accepted for compatibility and ignored; configure HTTP timeouts on the client |
| `telemetry` | boolean/object | No | `false` | Include operation telemetry |
| `processing_mode` | string | No | `semantic_and_vectors` | One processing mode for the whole batch: `semantic_and_vectors` or `vectors_only` |
| `defer_processing` | boolean | No | `false` | Save files without scheduling semantic or vector work; the caller schedules later processing |
diff --git a/docs/en/guides/01-configuration.md b/docs/en/guides/01-configuration.md
index b03a93e6a..08eeafb3d 100644
--- a/docs/en/guides/01-configuration.md
+++ b/docs/en/guides/01-configuration.md
@@ -1315,10 +1315,12 @@ Storage configuration for context data, including file storage (RAGFS) and vecto
Task records use the local FileStore by default. To share Task/Work state across
OpenViking processes, provision `openviking/service/sql/task_store_init.sql` in
-MySQL and select the relational backend:
+MySQL, apply the SQL changes under `openviking/service/sql/changes/`, configure
+a stable top-level `service_name`, and select the relational backend:
```json
{
+ "service_name": "production",
"storage": {
"task_store": {
"provider": "mysql",
@@ -1342,9 +1344,15 @@ Replace `ov_tasks` consistently in the provisioning SQL when using a custom pref
The MySQL backend requires the `openviking[mysql]` extra. Its mutations,
Task finalization, and execution-event updates commit in one database
-transaction. `task_id` is globally unique; `account_id` and `user_id` scope
-authorization and queries. Do not use the FileStore tables and MySQL tables as
-active writers for the same deployment.
+transaction. Top-level `service_name` is stored on every Task/Work/Event row and
+isolates multiple OpenViking deployments that share one table set. Within one
+`service_name`, `task_id` is unique; `account_id` and `user_id` scope
+authorization and queries. Fresh deployments should run the init SQL and then
+the change scripts in order; existing TaskStore tables created before
+`service_name` must run
+`openviking/service/sql/changes/0001_task_store_service_name.sql` before
+upgrading. Do not use the FileStore tables and MySQL tables as active writers
+for the same deployment.
#### agfs (RAGFS)
diff --git a/docs/zh/api/02-resources.md b/docs/zh/api/02-resources.md
index 7b1fad739..387c237ef 100644
--- a/docs/zh/api/02-resources.md
+++ b/docs/zh/api/02-resources.md
@@ -186,7 +186,8 @@ Connector 要求精确的 `to` 目标,不支持 `wait=true`。`tos://` 来源
| source_name | string | 否 | None | 来源显示名;上传文件省略时使用原始文件名 |
| reason | string | 否 | "" | 添加资源的原因;非空时会随资源 URI 进入常规 session 记忆抽取链路,并在生成的记忆中记录资源引用 |
| instruction | string | 否 | "" | 语义提取的处理指令(实验特性) |
-| wait | bool | 否 | False | 是否等待语义处理和向量化完成才返回;不支持单独的 `timeout` 参数,HTTP 超时使用客户端配置 |
+| wait | bool | 否 | False | 是否等待语义处理和向量化完成才返回 |
+| timeout | float | 否 | None | 已废弃:仅为兼容旧请求保留,传入值会被忽略;HTTP 超时使用客户端配置 |
| strict | bool | 否 | False | 是否使用严格模式 |
| ignore_dirs | string | 否 | None | 要忽略的目录名(逗号分隔) |
| include | string | 否 | None | 包含的文件模式(glob) |
diff --git a/docs/zh/api/03-filesystem.md b/docs/zh/api/03-filesystem.md
index cbbc56601..dab3a430d 100644
--- a/docs/zh/api/03-filesystem.md
+++ b/docs/zh/api/03-filesystem.md
@@ -640,6 +640,7 @@ URI 格式非法、scheme 不支持,或使用 `temp`、`queue` 等内部作用
|------|------|------|--------|------|
| uri | str | 是 | - | 要删除的 Viking URI |
| wait | bool | 否 | False | 等待删除后的语义刷新完成 |
+| timeout | float | 否 | None | 已废弃:仅为兼容旧请求保留,传入值会被忽略;HTTP 超时使用客户端配置 |
| recursive | bool | 否 | False | 递归删除目录 |
diff --git a/docs/zh/api/04-skills.md b/docs/zh/api/04-skills.md
index ded61a3e7..a47e50170 100644
--- a/docs/zh/api/04-skills.md
+++ b/docs/zh/api/04-skills.md
@@ -189,6 +189,7 @@ This tool wraps the MCP tool `search-web`. Call this when the user needs functio
| data | Any | 否 | - | 内联 SKILL.md、结构化/MCP 数据或 Git URL;与 `temp_file_id` 选择一个 |
| temp_file_id | string | 否 | - | 临时上传文件 ID(通过 `temp_upload` 获取);与 `data` 选择一个 |
| wait | bool | 否 | False | 是否等待技能处理完成 |
+| timeout | float | 否 | None | 已废弃:仅为兼容旧请求保留,传入值会被忽略;HTTP 超时使用客户端配置 |
| telemetry | TelemetryRequest | 否 | False | 是否返回遥测数据 |
| target_uri | string | 否 | 按目标优先级解析 | 目标 skills 根,例如 `viking://~/skills` 或 `viking://agent/skills` |
| skills | List[str] | 否 | `[]` | 从目录/Git 集合按目录名选择 Skill;空列表或 `["*"]` 选择全部 |
@@ -706,6 +707,7 @@ curl -X POST http://localhost:1933/api/v1/skills/find \
| `target_uri` | 未指定 | 优先查找的 skills 根,参见读取接口的回退规则 |
| `source_metadata` | 自动生成 | 更新来源信息 |
| `wait` | `false` | 是否等待后台处理 |
+| `timeout` | `null` | 已废弃:兼容旧请求,传入值会被忽略 |
| `telemetry` | `false` | 遥测配置 |
必须提供新内容/上传包,或设 `from_source=true`。新内容的名称必须与 URL 中的 `skill_name` 一致;更新不是重命名或局部 patch。先解析并检查新包,再备份替换;同步失败会尝试恢复旧包。需保留的辅助文件应随新包一起提交。
diff --git a/docs/zh/api/07-system.md b/docs/zh/api/07-system.md
index 62ecc611f..548233490 100644
--- a/docs/zh/api/07-system.md
+++ b/docs/zh/api/07-system.md
@@ -333,7 +333,7 @@ ov system consistency viking://resources/my-project
**参数**
-无参数。HTTP 请求超时使用客户端配置。
+无必填参数。可选 JSON 请求体仍接受旧版 `timeout` 字段,但该字段已废弃,传入值会被忽略。不传请求体仍可调用。HTTP 请求超时使用客户端配置。
#### 3. 使用示例
diff --git a/docs/zh/api/12-content.md b/docs/zh/api/12-content.md
index 8949e62d1..5062a107f 100644
--- a/docs/zh/api/12-content.md
+++ b/docs/zh/api/12-content.md
@@ -218,6 +218,7 @@ ov read viking://resources/docs/api.md
| content | str | 是 | - | 要写入的新内容 |
| mode | str | 否 | `replace` | `replace` 覆盖已有文件、缺失时创建;`append` 追加已有文件、缺失时创建;`create` 是 `replace` 的兼容别名 |
| wait | bool | 否 | `false` | 是否等待后台语义/向量刷新完成 |
+| timeout | float | 否 | None | 已废弃:仅为兼容旧请求保留,传入值会被忽略;HTTP 超时使用客户端配置 |
| tags | string[] | 否 | 未设置 | 写入文件的显式检索标签,例如 `["team=search", "env=prod"]` |
| tag_mode | string | 否 | `replace` | 标签更新方式:`replace` 覆盖、`append` 按 key 合并、`clear` 清空已有标签且不要求传 `tags` |
@@ -345,6 +346,7 @@ ov write viking://resources/docs/api.md \
| `root_uri` | string | 是 | - | 包含所有写入目标的已存在 Resource 或 Memory 目录 |
| `operations` | array | 是 | - | 要校验并执行的文件写入 |
| `wait` | boolean | 否 | `true` | 是否等待语义/向量刷新 |
+| timeout | float | 否 | None | 已废弃:仅为兼容旧请求保留,传入值会被忽略;HTTP 超时使用客户端配置 |
| `telemetry` | boolean/object | 否 | `false` | 是否返回操作遥测信息 |
| `processing_mode` | string | 否 | `semantic_and_vectors` | 整批统一的处理模式:`semantic_and_vectors` 或 `vectors_only` |
| `defer_processing` | boolean | 否 | `false` | 只保存文件,不派发语义和向量处理;后处理由调用方安排 |
diff --git a/docs/zh/guides/01-configuration.md b/docs/zh/guides/01-configuration.md
index 46929682d..082b15657 100644
--- a/docs/zh/guides/01-configuration.md
+++ b/docs/zh/guides/01-configuration.md
@@ -1303,10 +1303,13 @@ Glob 引擎配置,用于路径模式匹配。这些设置为服务端配置,
Task 记录默认使用本地 FileStore。需要多个 OpenViking 进程共享
Task/Work 状态时,先在 MySQL 中执行
-`openviking/service/sql/task_store_init.sql`,再选择关系型后端:
+`openviking/service/sql/task_store_init.sql`,再按顺序执行
+`openviking/service/sql/changes/` 下的变更脚本,配置稳定的顶层
+`service_name` 后选择关系型后端:
```json
{
+ "service_name": "production",
"storage": {
"task_store": {
"provider": "mysql",
@@ -1329,7 +1332,11 @@ Task/Work 状态时,先在 MySQL 中执行
MySQL 后端需要安装 `openviking[mysql]` extra。Task mutation、Task
最终化和 execution event 更新在同一个数据库事务中提交。`task_id`
-是全局唯一身份;`account_id` 和 `user_id` 用于鉴权和查询范围。同一部署
+在同一个 `service_name` 分区内唯一;顶层 `service_name` 会写入每条
+Task/Work/Event 记录,用于隔离共用同一套表的多个 OpenViking 部署。
+`account_id` 和 `user_id` 用于鉴权和查询范围。新库需要先执行 init SQL,
+再按顺序执行变更脚本;升级使用旧 TaskStore 表的库前,先执行
+`openviking/service/sql/changes/0001_task_store_service_name.sql`。同一部署
不要同时让 FileStore 和 MySQL 表作为写入端。
#### agfs (RAGFS)
diff --git a/openviking/server/account_stores/mysql.py b/openviking/server/account_stores/mysql.py
index bab71d9cd..098c7b8fc 100644
--- a/openviking/server/account_stores/mysql.py
+++ b/openviking/server/account_stores/mysql.py
@@ -11,12 +11,13 @@ import hmac
import secrets
import ssl as ssl_module
from contextlib import asynccontextmanager
+from datetime import timezone
from typing import AsyncGenerator
from uuid import uuid4
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
-from sqlalchemy import and_, case, delete, func, select, update
+from sqlalchemy import and_, case, delete, exists, func, insert, literal, select, update
from sqlalchemy.engine import URL
from sqlalchemy.exc import DBAPIError, IntegrityError
from sqlalchemy.ext.asyncio import (
@@ -66,6 +67,7 @@ from openviking_cli.utils.config.config_utils import normalize_service_name
_MYSQL_IDENTIFIER_MAX_LENGTH = MAX_IDENTIFIER_LENGTH
_RETAINED_REVOKED_CREDENTIALS = 10
+_DEFAULT_ACCOUNT_ID = "default"
def _validate_length(value: str, name: str) -> None:
@@ -129,6 +131,12 @@ def _deletion(record) -> DeletionRecord | None:
)
+def _utc_isoformat(value) -> str:
+ if value.tzinfo is None:
+ value = value.replace(tzinfo=timezone.utc)
+ return value.astimezone(timezone.utc).isoformat()
+
+
@mysql_runtime_store
class MySQLAccountStore(AccountStore):
"""A manually provisioned relational account store scoped by service name."""
@@ -326,6 +334,17 @@ class MySQLAccountStore(AccountStore):
async with sessions() as session:
for model in (Account, User, Group, GroupMember, Credential):
await session.execute(select(*model.__table__.columns).limit(1))
+ await session.execute(
+ insert(Account)
+ .prefix_with("IGNORE", dialect="mysql")
+ .from_select(
+ ["service_name", "account_id"],
+ select(literal(self._service_name), literal(_DEFAULT_ACCOUNT_ID)).where(
+ ~exists().where(Account.service_name == self._service_name)
+ ),
+ )
+ )
+ await session.commit()
except ImportError:
await wait_mysql_cleanup(cleanup_failed_load())
raise RuntimeError("MySQL account storage requires openviking[mysql]") from None
@@ -427,7 +446,7 @@ class MySQLAccountStore(AccountStore):
def _account_summary(account, user_count: int) -> AccountSummary:
return {
"account_id": account.account_id,
- "created_at": account.created_at.isoformat(),
+ "created_at": _utc_isoformat(account.created_at),
"user_count": user_count,
"status": "deleting" if account.deletion_task_id else "active",
**({"task_id": account.deletion_task_id} if account.deletion_task_id else {}),
diff --git a/openviking/server/account_stores/mysql_schema.py b/openviking/server/account_stores/mysql_schema.py
index 9cd2f6245..177d970e4 100644
--- a/openviking/server/account_stores/mysql_schema.py
+++ b/openviking/server/account_stores/mysql_schema.py
@@ -48,6 +48,12 @@ class Account(Base):
"service_name",
"deletion_task_id",
),
+ Index(
+ "ix_ov_accounts_account_created",
+ "service_name",
+ "created_at",
+ "account_id",
+ ),
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
)
@@ -103,6 +109,23 @@ class User(Base):
"service_name",
"deletion_task_id",
),
+ Index(
+ "ix_ov_accounts_user_active_created",
+ "service_name",
+ "account_pk",
+ "deletion_task_id",
+ "created_at",
+ "user_pk",
+ ),
+ Index(
+ "ix_ov_accounts_user_role_active_created",
+ "service_name",
+ "account_pk",
+ "role",
+ "deletion_task_id",
+ "created_at",
+ "user_pk",
+ ),
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
)
diff --git a/openviking/server/account_stores/redis_mysql.py b/openviking/server/account_stores/redis_mysql.py
index 8827b4424..c21f9c920 100644
--- a/openviking/server/account_stores/redis_mysql.py
+++ b/openviking/server/account_stores/redis_mysql.py
@@ -9,12 +9,14 @@ import base64
import hashlib
import json
import secrets
+import threading
import time
from collections.abc import AsyncIterator, Awaitable, Callable
from contextlib import asynccontextmanager
-from typing import Any, TypeVar
+from typing import Any, TypeVar, cast
from urllib.parse import parse_qs, unquote, urlsplit
+from openviking.concurrency import AsyncSemaphore
from openviking.server.account_stores.base import AccountStore
from openviking.server.account_stores.models import (
AccountSummary,
@@ -28,13 +30,16 @@ from openviking.server.account_stores.redis_client import (
RedisClientHandle,
create_redis_client,
)
-from openviking_cli.exceptions import InvalidArgumentError
+from openviking_cli.exceptions import InvalidArgumentError, UnavailableError
from openviking_cli.utils import get_logger
from openviking_cli.utils.config.config_utils import normalize_service_name
logger = get_logger(__name__)
T = TypeVar("T")
+_MYSQL_FALLBACK_MAX_CONCURRENT = 8
+_MYSQL_FALLBACK_WAIT_SECONDS = 0.1
+
_RELEASE_LOCK_SCRIPT = """
-- release-lock
if redis.call('GET', KEYS[1]) == ARGV[1] then
@@ -243,6 +248,11 @@ class RedisMySQLAccountStore(AccountStore):
)
self._mysql = mysql_store
+ pool_size = cast(int, mysql_store._engine_options.get("pool_size", 10))
+ fallback_concurrency = max(1, min(pool_size, _MYSQL_FALLBACK_MAX_CONCURRENT))
+ self._mysql_fallback_io = AsyncSemaphore(fallback_concurrency)
+ # Bound running plus queued fallbacks across all cached read methods.
+ self._mysql_fallback_admission = threading.BoundedSemaphore(fallback_concurrency * 2)
self._index_ttl_seconds = self._ttl_seconds + self._lock_ttl_seconds
self._service_name = normalized_service_name
self._redis = redis_client
@@ -413,6 +423,26 @@ class RedisMySQLAccountStore(AccountStore):
)
return bool(result)
+ async def _read_mysql_fallback(self, loader: Callable[[], Awaitable[T]]) -> T:
+ """Bound Redis-failure reads without occupying a thread while waiting."""
+ if not self._mysql_fallback_admission.acquire(blocking=False):
+ raise UnavailableError("account storage", "MySQL fallback capacity exceeded")
+ acquired = False
+ try:
+ try:
+ await asyncio.wait_for(
+ self._mysql_fallback_io.acquire(), timeout=_MYSQL_FALLBACK_WAIT_SECONDS
+ )
+ except asyncio.TimeoutError:
+ raise UnavailableError("account storage", "MySQL fallback is busy") from None
+ acquired = True
+ return await loader()
+ finally:
+ # MySQLRuntime settles database cancellation before returning here.
+ if acquired:
+ self._mysql_fallback_io.release()
+ self._mysql_fallback_admission.release()
+
async def _read_through(
self,
account_id: str,
@@ -425,17 +455,20 @@ class RedisMySQLAccountStore(AccountStore):
return value
except Exception:
logger.exception("Redis cache read failed; reading %s from MySQL", key)
- return await loader()
+ return await self._read_mysql_fallback(loader)
+ mysql_started = False
try:
async with self._account_lock(account_id) as token:
owned, payload = await self._get_if_locked(account_id, token, key)
if not owned:
logger.warning("Redis cache lease expired before reading %s", key)
- return await loader()
+ mysql_started = True
+ return await self._read_mysql_fallback(loader)
hit, value = self._read_nullable(payload)
if hit:
return value
+ mysql_started = True
value = await loader()
try:
if not await self._set_if_locked(
@@ -451,8 +484,10 @@ class RedisMySQLAccountStore(AccountStore):
except _CacheLockUnavailable:
logger.warning("Redis cache lock unavailable; reading %s from MySQL", key)
except Exception:
+ if mysql_started:
+ raise
logger.exception("Redis cache read failed; reading %s from MySQL", key)
- return await loader()
+ return await self._read_mysql_fallback(loader)
async def _delete_and_unindex(self, account_id: str, *keys: str) -> None:
assert self._redis is not None
@@ -669,10 +704,13 @@ class RedisMySQLAccountStore(AccountStore):
account_id_hint: str | None = None,
user_id_hint: str | None = None,
) -> tuple[str, str] | None:
- if account_id_hint is None:
+ async def verify_in_mysql() -> tuple[str, str] | None:
return await self._mysql.verify_api_key(
- api_key, account_id_hint=None, user_id_hint=user_id_hint
+ api_key, account_id_hint=account_id_hint, user_id_hint=user_id_hint
)
+
+ if account_id_hint is None:
+ return await self._read_mysql_fallback(verify_in_mysql)
try:
hit, binding = await self._cached_binding(
account_id_hint,
@@ -683,6 +721,8 @@ class RedisMySQLAccountStore(AccountStore):
return binding
except Exception:
logger.exception("Redis API-key cache read failed; falling back to MySQL")
+ return await self._read_mysql_fallback(verify_in_mysql)
+ mysql_started = False
try:
async with self._account_lock(account_id_hint) as token:
hit, binding = await self._cached_binding(
@@ -692,11 +732,8 @@ class RedisMySQLAccountStore(AccountStore):
)
if hit:
return binding
- binding = await self._mysql.verify_api_key(
- api_key,
- account_id_hint=account_id_hint,
- user_id_hint=user_id_hint,
- )
+ mysql_started = True
+ binding = await verify_in_mysql()
try:
await self._cache_binding(
account_id_hint,
@@ -711,10 +748,10 @@ class RedisMySQLAccountStore(AccountStore):
except _CacheLockUnavailable:
logger.warning("Redis cache lock unavailable; verifying API key in MySQL")
except Exception:
+ if mysql_started:
+ raise
logger.exception("Redis API-key cache write failed; verifying in MySQL")
- return await self._mysql.verify_api_key(
- api_key, account_id_hint=account_id_hint, user_id_hint=user_id_hint
- )
+ return await self._read_mysql_fallback(verify_in_mysql)
async def get_user(self, account_id: str, user_id: str) -> UserSummary | None:
value = await self._read_through(
@@ -724,9 +761,7 @@ class RedisMySQLAccountStore(AccountStore):
)
return value if value is None else UserSummary(value)
- async def get_user_membership(
- self, account_id: str, user_id: str
- ) -> UserSummary | None:
+ async def get_user_membership(self, account_id: str, user_id: str) -> UserSummary | None:
# Bypass the read-through cache: the admin membership probe must reflect
# the latest persisted state, which MySQL indexes directly.
return await self._mysql.get_user_membership(account_id, user_id)
diff --git a/openviking/server/account_stores/sql/changes/0000_template.sql b/openviking/server/account_stores/sql/changes/0000_template.sql
index 414ec0cd3..f67316470 100644
--- a/openviking/server/account_stores/sql/changes/0000_template.sql
+++ b/openviking/server/account_stores/sql/changes/0000_template.sql
@@ -1,4 +1,4 @@
--- Copy this file as YYYYMMDD_description.sql for each reviewed schema change.
+-- Copy this file as the next NNNN_description.sql for each reviewed schema change.
-- Keep changes forward-only. Do not modify init.sql after it is released.
-- MySQL DDL may commit implicitly; review rollback and rollout separately.
diff --git a/openviking/server/account_stores/sql/changes/0001_account_user_list_indexes.sql b/openviking/server/account_stores/sql/changes/0001_account_user_list_indexes.sql
new file mode 100644
index 000000000..8545b993c
--- /dev/null
+++ b/openviking/server/account_stores/sql/changes/0001_account_user_list_indexes.sql
@@ -0,0 +1,17 @@
+-- Add indexes for account and user management list pagination.
+--
+-- These indexes support:
+-- - list_accounts(): service-scoped ordering by created_at/account_id
+-- - list_users_page(): active-user pagination by account and created_at/user_pk
+-- - list_users_page(role_filter=...): active-user pagination by account, role,
+-- and created_at/user_pk
+
+ALTER TABLE `ov_accounts`
+ ADD INDEX ix_ov_accounts_account_created
+ (service_name, created_at, account_id);
+
+ALTER TABLE `ov_accounts_users`
+ ADD INDEX ix_ov_accounts_user_active_created
+ (service_name, account_pk, deletion_task_id, created_at, user_pk),
+ ADD INDEX ix_ov_accounts_user_role_active_created
+ (service_name, account_pk, `role`, deletion_task_id, created_at, user_pk);
diff --git a/openviking/server/mcp_endpoint.py b/openviking/server/mcp_endpoint.py
index 61feb8ffc..7c0e51a7b 100644
--- a/openviking/server/mcp_endpoint.py
+++ b/openviking/server/mcp_endpoint.py
@@ -1403,6 +1403,12 @@ async def write(
content: str,
mode: Literal["replace", "append", "create"] = "replace",
wait: bool = False,
+ timeout: Annotated[
+ Optional[float],
+ Field(
+ deprecated=True, description="Deprecated compatibility parameter; accepted and ignored."
+ ),
+ ] = None,
acl: Optional[AclSpec] = None,
) -> str:
"""Write text to a viking:// file. Use this for files you author yourself (notes, profiles, state), the same way you would use a working directory. To change part of an existing file, prefer the edit tool over a full rewrite. To store a file, document, URL, or repo the user gives you, use add_resource; for a skill, use add_skill. Do not copy its text into a file here instead.
@@ -1440,6 +1446,12 @@ async def edit(
new_string: str,
replace_all: bool = False,
wait: bool = False,
+ timeout: Annotated[
+ Optional[float],
+ Field(
+ deprecated=True, description="Deprecated compatibility parameter; accepted and ignored."
+ ),
+ ] = None,
) -> str:
"""Replace an exact string with new text in an existing viking:// file. Use this for targeted changes instead of rewriting the whole file with the write tool. old_string must match the file's current content exactly, including indentation and newlines; use the read tool first to see it. The edit fails and the file is left unchanged if old_string is not found, or if it matches more than once and replace_all is false (pass more surrounding context to make it unique, or set replace_all=true to replace every occurrence). Pass new_string="" to delete old_string.
diff --git a/openviking/server/resource_ingest.py b/openviking/server/resource_ingest.py
index e1366c1fc..deb38ef54 100644
--- a/openviking/server/resource_ingest.py
+++ b/openviking/server/resource_ingest.py
@@ -58,6 +58,11 @@ class AddResourceRequest(BaseModel):
reason: str = ""
instruction: str = ""
wait: bool = False
+ timeout: float | None = Field(
+ default=None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ )
strict: bool = False
internal_task: bool = False
source_name: Optional[str] = None
diff --git a/openviking/server/routers/content.py b/openviking/server/routers/content.py
index 4f666406d..ee706418d 100644
--- a/openviking/server/routers/content.py
+++ b/openviking/server/routers/content.py
@@ -46,6 +46,11 @@ class WriteContentRequest(BaseModel):
content: str
mode: str = "replace"
wait: bool = False
+ timeout: float | None = Field(
+ default=None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ )
telemetry: TelemetryRequest = False
processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE
tags: list[str] | None = None
@@ -81,6 +86,11 @@ class BatchWriteRequest(BaseModel):
root_uri: str
operations: list[BatchWriteOperation]
wait: bool = True
+ timeout: float | None = Field(
+ default=None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ )
telemetry: TelemetryRequest = False
# Preserve conflicting target files and return their URI/reason while writing other files.
skip_conflicts: bool = False
diff --git a/openviking/server/routers/filesystem.py b/openviking/server/routers/filesystem.py
index 5f4edf058..df3e50e60 100644
--- a/openviking/server/routers/filesystem.py
+++ b/openviking/server/routers/filesystem.py
@@ -303,6 +303,11 @@ async def rm(
uri: str = Query(..., description="Viking URI"),
recursive: bool = Query(False, description="Remove recursively"),
wait: bool = Query(False, description="Wait for semantic refresh to complete"),
+ timeout: Optional[float] = Query(
+ None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ ),
_ctx: RequestContext = Depends(get_request_context),
):
"""Remove resource."""
diff --git a/openviking/server/routers/resources.py b/openviking/server/routers/resources.py
index 78a4ed342..e4d79ab1b 100644
--- a/openviking/server/routers/resources.py
+++ b/openviking/server/routers/resources.py
@@ -44,6 +44,11 @@ class AddSkillRequest(BaseModel):
skills: list[str] = Field(default_factory=list)
list_only: bool = False
wait: bool = False
+ timeout: float | None = Field(
+ default=None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ )
source_metadata: Optional[Dict[str, Any]] = None
target_uri: Optional[str] = None
telemetry: TelemetryRequest = False
diff --git a/openviking/server/routers/skills.py b/openviking/server/routers/skills.py
index 5ed325154..5369db75c 100644
--- a/openviking/server/routers/skills.py
+++ b/openviking/server/routers/skills.py
@@ -14,7 +14,7 @@ from typing import Any, AsyncIterator, Dict, Optional
import yaml
from fastapi import APIRouter, Depends, Request
from fastapi import Path as ApiPath
-from pydantic import BaseModel, ConfigDict, model_validator
+from pydantic import BaseModel, ConfigDict, Field, model_validator
from openviking.core.namespace import canonical_user_root
from openviking.core.path_variables import resolve_path_variables
@@ -69,6 +69,11 @@ class UpdateSkillRequest(BaseModel):
temp_file_id: Optional[str] = None
from_source: bool = False
wait: bool = False
+ timeout: float | None = Field(
+ default=None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ )
source_metadata: Optional[Dict[str, Any]] = None
telemetry: TelemetryRequest = False
target_uri: Optional[str] = None
diff --git a/openviking/server/routers/system.py b/openviking/server/routers/system.py
index 344254741..9fa5d9cf2 100644
--- a/openviking/server/routers/system.py
+++ b/openviking/server/routers/system.py
@@ -2,16 +2,15 @@
# SPDX-License-Identifier: AGPL-3.0
"""System endpoints for OpenViking HTTP Server."""
-from fastapi import APIRouter, Depends, Request
+from fastapi import APIRouter, Body, Depends, Request
from fastapi.responses import JSONResponse
-from pydantic import BaseModel
+from pydantic import BaseModel, Field
from openviking.core.path_variables import resolve_path_variables
from openviking.core.uri_validation import validate_request_viking_uri
-from openviking.pyagfs.exceptions import AGFSInvalidOperationError, AGFSNotSupportedError
-from openviking.server.auth import get_request_context, require_role
+from openviking.server.auth import get_request_context
from openviking.server.dependencies import get_service
-from openviking.server.identity import AuthMode, RequestContext, Role
+from openviking.server.identity import AuthMode, RequestContext
from openviking.server.models import Response
from openviking.storage.viking_fs import get_viking_fs
from openviking_cli.exceptions import UnauthenticatedError
@@ -200,6 +199,16 @@ async def system_status(
)
+class WaitRequest(BaseModel):
+ """Legacy wait parameters, retained for request compatibility."""
+
+ timeout: float | None = Field(
+ default=None,
+ deprecated=True,
+ description="Deprecated compatibility parameter; accepted and ignored.",
+ )
+
+
class ConsistencyRequest(BaseModel):
"""Request model for filesystem/vector-index consistency checks."""
@@ -208,6 +217,7 @@ class ConsistencyRequest(BaseModel):
@router.post("/api/v1/system/wait", tags=["system"])
async def wait_processed(
+ request: WaitRequest | None = Body(None),
_ctx: RequestContext = Depends(get_request_context),
):
"""Wait for all processing to complete."""
diff --git a/openviking/service/core.py b/openviking/service/core.py
index 993e7b154..75f9bf3f0 100644
--- a/openviking/service/core.py
+++ b/openviking/service/core.py
@@ -59,6 +59,7 @@ from openviking_cli.session.user_id import UserIdentifier
from openviking_cli.utils import get_logger
from openviking_cli.utils.config import OPENVIKING_ENABLE_RECORDER_ENV, get_openviking_config
from openviking_cli.utils.config.agent_evolution_config import AgentEvolutionConfig
+from openviking_cli.utils.config.config_utils import DEFAULT_SERVICE_NAME
from openviking_cli.utils.config.git_config import GitConfig
from openviking_cli.utils.config.memory_config import SessionAutoCommitConfig
from openviking_cli.utils.config.open_viking_config import initialize_openviking_config
@@ -172,6 +173,7 @@ class OpenVikingService:
max_concurrent_external_task=config.queue_workers.external_task.max_concurrent,
binding_config=binding_config,
git_config=config.git,
+ service_name=config.service_name,
)
def _init_storage(
@@ -187,6 +189,7 @@ class OpenVikingService:
binding_config: Any = None,
*,
git_config: Optional[GitConfig] = None,
+ service_name: str = DEFAULT_SERVICE_NAME,
) -> None:
"""Initialize storage resources."""
from openviking.utils.agfs_utils import RagfsBindingConfig, create_agfs_client
@@ -220,7 +223,7 @@ class OpenVikingService:
self._vikingdb_manager.acl_manager = AclManager(self._vikingdb_manager)
# PathLock has been moved to Rust ragfs; Python-layer LockManager is no longer needed.
- set_task_tracker(config.build_task_tracker(self._agfs_client))
+ set_task_tracker(config.build_task_tracker(self._agfs_client, service_name=service_name))
def _build_ragfs_binding_config(self) -> Any:
"""Build the single runtime binding config from OpenViking storage + encryption settings."""
diff --git a/openviking/service/deletion.py b/openviking/service/deletion.py
index 708a8255c..f6b66253d 100644
--- a/openviking/service/deletion.py
+++ b/openviking/service/deletion.py
@@ -75,6 +75,7 @@ class DeletionService:
self._service = service
self._manager = manager
self._service_loop = service_loop
+ self._manager_dispatcher = OwnerLoopDispatcher(service_loop)
self._oauth_store = oauth_store
self._usage_audit_runtime = usage_audit_runtime
self._gateway_app = gateway_app
@@ -128,7 +129,10 @@ class DeletionService:
queued_task_ids.add(task_id)
async def _is_deleting(self, account_id: str, user_id: str | None = None) -> bool:
- return await self._manager.get_deletion(account_id, user_id) is not None
+ deletion = await self._manager_dispatcher.run(
+ lambda: self._manager.get_deletion(account_id, user_id)
+ )
+ return deletion is not None
async def delete(
self,
diff --git a/openviking/service/sql/changes/0001_task_store_service_name.sql b/openviking/service/sql/changes/0001_task_store_service_name.sql
new file mode 100644
index 000000000..4c4a4091a
--- /dev/null
+++ b/openviking/service/sql/changes/0001_task_store_service_name.sql
@@ -0,0 +1,92 @@
+-- Add service_name isolation to an existing OpenViking MySQL Task/Work store.
+--
+-- This script migrates tables created from openviking/service/sql/task_store_init.sql
+-- before service_name was introduced. If the existing rows belong to a deployment
+-- whose configured service_name is not 'openviking-server', replace that literal
+-- consistently before running this script. For a custom table prefix, replace
+-- ov_tasks consistently in all table and index names.
+
+ALTER TABLE `ov_tasks_works`
+ DROP FOREIGN KEY fk_ov_tasks_works_task;
+
+ALTER TABLE `ov_tasks_execution_events`
+ DROP FOREIGN KEY fk_ov_tasks_execution_events_task;
+
+ALTER TABLE `ov_tasks`
+ ADD COLUMN service_name VARCHAR(255) COLLATE utf8mb4_bin
+ NOT NULL DEFAULT 'openviking-server' FIRST,
+ ADD COLUMN resource_id_hash VARCHAR(64) COLLATE ascii_bin NULL AFTER resource_id,
+ ADD COLUMN active_business_key_hex VARCHAR(64) COLLATE ascii_bin NULL
+ AFTER active_business_key;
+
+UPDATE `ov_tasks`
+SET
+ resource_id_hash = CASE
+ WHEN resource_id IS NULL THEN NULL
+ ELSE SHA2(resource_id, 256)
+ END,
+ active_business_key_hex = CASE
+ WHEN active_business_key IS NULL THEN NULL
+ ELSE LOWER(HEX(active_business_key))
+ END;
+
+ALTER TABLE `ov_tasks_works`
+ ADD COLUMN service_name VARCHAR(255) COLLATE utf8mb4_bin
+ NOT NULL DEFAULT 'openviking-server' FIRST;
+
+ALTER TABLE `ov_tasks_execution_events`
+ ADD COLUMN service_name VARCHAR(255) COLLATE utf8mb4_bin
+ NOT NULL DEFAULT 'openviking-server' FIRST;
+
+ALTER TABLE `ov_tasks`
+ DROP INDEX uq_ov_tasks_active_business_key,
+ DROP COLUMN active_business_key,
+ CHANGE COLUMN active_business_key_hex active_business_key
+ VARCHAR(64) COLLATE ascii_bin NULL,
+ DROP INDEX ix_ov_tasks_owner_created,
+ DROP INDEX ix_ov_tasks_account_created,
+ DROP INDEX ix_ov_tasks_created,
+ DROP INDEX ix_ov_tasks_owner_status,
+ DROP INDEX ix_ov_tasks_owner_internal_created,
+ DROP INDEX ix_ov_tasks_expiry,
+ DROP PRIMARY KEY,
+ ADD PRIMARY KEY (service_name, task_id),
+ ADD UNIQUE KEY uq_ov_tasks_active_business_key
+ (service_name, active_business_key),
+ ADD KEY ix_ov_tasks_owner_created
+ (service_name, account_id, user_id, created_at),
+ ADD KEY ix_ov_tasks_account_created
+ (service_name, account_id, created_at, task_id),
+ ADD KEY ix_ov_tasks_created
+ (service_name, created_at, task_id),
+ ADD KEY ix_ov_tasks_owner
+ (service_name, account_id, user_id),
+ ADD KEY ix_ov_tasks_owner_internal_created
+ (service_name, account_id, user_id, is_internal, created_at),
+ ADD KEY ix_ov_tasks_acct_type_created
+ (service_name, account_id, task_type, created_at),
+ ADD KEY ix_ov_tasks_acct_status_created
+ (service_name, account_id, status, created_at),
+ ADD KEY ix_ov_tasks_acct_resource_created
+ (service_name, account_id, resource_id_hash, created_at),
+ ADD KEY ix_ov_tasks_acct_internal_created
+ (service_name, account_id, is_internal, created_at),
+ ADD KEY ix_ov_tasks_expiry
+ (service_name, status, updated_at, task_id);
+
+ALTER TABLE `ov_tasks_works`
+ DROP INDEX ix_ov_tasks_works_state,
+ DROP INDEX ix_ov_tasks_works_queue,
+ DROP INDEX ix_ov_tasks_works_owner,
+ DROP PRIMARY KEY,
+ ADD PRIMARY KEY (service_name, task_id, work_id),
+ ADD KEY ix_ov_tasks_works_state
+ (service_name, task_id, state),
+ ADD KEY ix_ov_tasks_works_queue
+ (service_name, task_id, queue_name),
+ ADD KEY ix_ov_tasks_works_owner
+ (service_name, account_id, user_id);
+
+ALTER TABLE `ov_tasks_execution_events`
+ DROP PRIMARY KEY,
+ ADD PRIMARY KEY (service_name, task_id, seq);
diff --git a/openviking/service/task_store_mysql.py b/openviking/service/task_store_mysql.py
index ecbb3338b..9db121819 100644
--- a/openviking/service/task_store_mysql.py
+++ b/openviking/service/task_store_mysql.py
@@ -65,11 +65,10 @@ from openviking.storage.mysql_runtime import (
wait_mysql_cleanup,
)
from openviking.storage.mysql_session import MySQLAsyncSession
+from openviking_cli.utils.config.config_utils import DEFAULT_SERVICE_NAME, normalize_service_name
-def _business_key(
- account_id: str, user_id: str, task_type: str, resource_id: Optional[str]
-) -> bytes:
+def _business_key(account_id: str, user_id: str, task_type: str, resource_id: Optional[str]) -> str:
"""Hash the guarded business key into a fixed-width unique-index value."""
required = (account_id, user_id, task_type)
encoded = [len(value.encode()).to_bytes(2, "big") + value.encode() for value in required]
@@ -78,7 +77,11 @@ def _business_key(
else:
value = resource_id.encode()
encoded.append(b"\x01" + len(value).to_bytes(2, "big") + value)
- return hashlib.sha256(b"".join(encoded)).digest()
+ return hashlib.sha256(b"".join(encoded)).hexdigest()
+
+
+def _resource_id_hash(resource_id: Optional[str]) -> str | None:
+ return hashlib.sha256(resource_id.encode()).hexdigest() if resource_id is not None else None
def _json_copy(value: Any) -> Any:
@@ -113,9 +116,13 @@ class MySQLTaskWorkStore(TaskWorkStore):
self,
*,
params: dict[str, object] | None = None,
+ service_name: str | None = None,
runtime: MySQLRuntime | None = None,
) -> None:
values = dict(params or {})
+ param_service_name = values.pop("service_name", DEFAULT_SERVICE_NAME)
+ raw_service_name = service_name if service_name is not None else param_service_name
+ self._service_name = normalize_service_name(raw_service_name)
table_prefix = validate_table_prefix(values.pop("table", "ov_tasks"))
self._models: TaskStoreModels = make_task_store_models(table_prefix)
self._resource_id = os.environ.get("OV_RESOURCE_ID", "").strip()
@@ -254,29 +261,29 @@ class MySQLTaskWorkStore(TaskWorkStore):
raise ValueError("MySQLTaskWorkStore requires account_id and user_id")
return account_id, user_id
- @staticmethod
def _task_query(
+ self,
Task: Any,
task_id: str,
account_id: Optional[str],
user_id: Optional[str],
):
- predicates = [Task.task_id == task_id]
+ predicates = [Task.service_name == self._service_name, Task.task_id == task_id]
if account_id is not None:
predicates.append(Task.account_id == account_id)
if user_id is not None:
predicates.append(Task.user_id == user_id)
return predicates
- @staticmethod
def _work_query(
+ self,
Work: Any,
task_id: str,
work_id: Optional[str],
account_id: Optional[str],
user_id: Optional[str],
):
- predicates = [Work.task_id == task_id]
+ predicates = [Work.service_name == self._service_name, Work.task_id == task_id]
if work_id is not None:
predicates.append(Work.work_id == work_id)
if account_id is not None:
@@ -285,6 +292,9 @@ class MySQLTaskWorkStore(TaskWorkStore):
predicates.append(Work.user_id == user_id)
return predicates
+ def _event_query(self, Event: Any, task_id: str):
+ return Event.service_name == self._service_name, Event.task_id == task_id
+
@staticmethod
def _task_from_row(
row: Any,
@@ -382,10 +392,8 @@ class MySQLTaskWorkStore(TaskWorkStore):
await session.scalar(
select(
exists().where(
- Work.task_id == task_id,
- Work.state.not_in(
- [WorkState.DONE.value, WorkState.FAILED.value]
- ),
+ *self._work_query(Work, task_id, None, None, None),
+ Work.state.not_in([WorkState.DONE.value, WorkState.FAILED.value]),
)
)
)
@@ -401,9 +409,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
user_id: Optional[str] = None,
) -> list[WorkRecord]:
Work = self._models.Work
- statement = select(Work).where(
- *self._work_query(Work, task_id, None, account_id, user_id)
- )
+ statement = select(Work).where(*self._work_query(Work, task_id, None, account_id, user_id))
if work_ids is not None:
ids = set(work_ids)
if not ids:
@@ -416,7 +422,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
Event = self._models.Event
rows = (
await session.scalars(
- select(Event).where(Event.task_id == task_id).order_by(Event.seq)
+ select(Event).where(*self._event_query(Event, task_id)).order_by(Event.seq)
)
).all()
if not rows:
@@ -454,7 +460,10 @@ class MySQLTaskWorkStore(TaskWorkStore):
seqs = [int(item["seq"]) for item in items]
existing = set(
await session.scalars(
- select(Event.seq).where(Event.task_id == task_id, Event.seq.in_(seqs))
+ select(Event.seq).where(
+ *self._event_query(Event, task_id),
+ Event.seq.in_(seqs),
+ )
)
)
metadata = {
@@ -462,13 +471,14 @@ class MySQLTaskWorkStore(TaskWorkStore):
"started_mid_task": bool(history["started_mid_task"]),
}
await session.execute(
- update(Event).where(Event.task_id == task_id).values(**metadata)
+ update(Event).where(*self._event_query(Event, task_id)).values(**metadata)
)
for item in items:
if int(item["seq"]) in existing:
continue
session.add(
Event(
+ service_name=self._service_name,
task_id=task_id,
seq=int(item["seq"]),
recorded_at=item["recorded_at"],
@@ -481,13 +491,15 @@ class MySQLTaskWorkStore(TaskWorkStore):
)
)
await session.execute(
- delete(Event).where(Event.task_id == task_id, Event.seq < min(seqs))
+ delete(Event).where(*self._event_query(Event, task_id), Event.seq < min(seqs))
)
async def _lock_work(self, session: AsyncSession, task_id: str, work_id: str) -> Any:
Work = self._models.Work
return await session.scalar(
- select(Work).where(Work.task_id == task_id, Work.work_id == work_id).with_for_update()
+ select(Work)
+ .where(*self._work_query(Work, task_id, work_id, None, None))
+ .with_for_update()
)
@asynccontextmanager
@@ -510,12 +522,14 @@ class MySQLTaskWorkStore(TaskWorkStore):
stored._extra_fields = {}
append_task_events(stored, None)
row = Task(
+ service_name=self._service_name,
task_id=stored.task_id,
task_type=stored.task_type,
status=stored.status.value,
created_at=stored.created_at,
updated_at=stored.updated_at,
resource_id=stored.resource_id,
+ resource_id_hash=_resource_id_hash(stored.resource_id),
account_id=account_id,
user_id=user_id,
meta=_json_copy(stored.meta),
@@ -594,6 +608,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
intervals: dict[str, list[WorkRecord]] = {task.task_id: [] for task in batch}
rows = await session.execute(
select(Work.task_id, Work.started_at, Work.finished_at).where(
+ Work.service_name == self._service_name,
Work.task_id.in_(intervals),
Work.started_at.is_not(None),
)
@@ -674,7 +689,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
Task = self._models.Task
def statement_for(cursor: tuple[float, str] | None, batch_limit: int):
- predicates = []
+ predicates = [Task.service_name == self._service_name]
if account_id is not None:
predicates.append(Task.account_id == account_id)
if user_id is not None:
@@ -684,6 +699,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
if status is not None:
predicates.append(Task.status == status)
if resource_id is not None:
+ predicates.append(Task.resource_id_hash == _resource_id_hash(resource_id))
predicates.append(Task.resource_id == resource_id)
if not include_internal:
predicates.append(Task.is_internal.is_(False))
@@ -748,6 +764,18 @@ class MySQLTaskWorkStore(TaskWorkStore):
"""Return no stale process-local snapshot; callers use durable list APIs."""
return {}
+ async def _delete_task_records(self, session: AsyncSession, task_ids: Collection[str]) -> None:
+ """Delete locked Tasks and their dependent records in the current transaction."""
+ if not task_ids:
+ return
+ for model in (self._models.Work, self._models.Event, self._models.Task):
+ await session.execute(
+ delete(model).where(
+ model.service_name == self._service_name,
+ model.task_id.in_(task_ids),
+ )
+ )
+
async def delete_tasks(
self,
task_id: Optional[str] = None,
@@ -762,7 +790,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
raise ValueError("Deleting one Task requires user_id")
Task = self._models.Task
async with self._transaction() as session:
- predicates = [Task.account_id == account_id]
+ predicates = [Task.service_name == self._service_name, Task.account_id == account_id]
if task_id is not None:
predicates.append(Task.task_id == task_id)
if user_id is not None:
@@ -781,10 +809,9 @@ class MySQLTaskWorkStore(TaskWorkStore):
await session.scalars(
select(Work.task_id)
.where(
+ Work.service_name == self._service_name,
Work.task_id.in_(task_ids),
- Work.state.not_in(
- [WorkState.DONE.value, WorkState.FAILED.value]
- ),
+ Work.state.not_in([WorkState.DONE.value, WorkState.FAILED.value]),
)
.distinct()
)
@@ -807,9 +834,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
)
)
if blockers:
- active = [
- item for item in blockers if item.code is StoreOperationCode.ACTIVE_TASK
- ]
+ active = [item for item in blockers if item.code is StoreOperationCode.ACTIVE_TASK]
if active:
code = StoreOperationCode.ACTIVE_TASK
message = "Cannot delete active task records: " + ", ".join(
@@ -834,8 +859,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
DeletedTask(row.task_id, row.account_id, row.user_id)
for row in sorted(rows, key=lambda row: (row.account_id, row.user_id, row.task_id))
)
- if task_ids:
- await session.execute(delete(Task).where(Task.task_id.in_(task_ids)))
+ await self._delete_task_records(session, task_ids)
return DeleteTasksResult(deleted)
async def delete_expired(
@@ -849,6 +873,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
statement = (
select(Task)
.where(
+ Task.service_name == self._service_name,
or_(
and_(
Task.status.in_([TaskStatus.COMPLETED.value, TaskStatus.CANCELLED.value]),
@@ -858,7 +883,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
Task.status == TaskStatus.FAILED.value,
Task.updated_at < failed_cutoff,
),
- )
+ ),
)
.order_by(Task.updated_at, Task.task_id)
.limit(limit)
@@ -874,10 +899,9 @@ class MySQLTaskWorkStore(TaskWorkStore):
await session.scalars(
select(Work.task_id)
.where(
+ Work.service_name == self._service_name,
Work.task_id.in_(task_ids),
- Work.state.not_in(
- [WorkState.DONE.value, WorkState.FAILED.value]
- ),
+ Work.state.not_in([WorkState.DONE.value, WorkState.FAILED.value]),
)
.distinct()
)
@@ -885,8 +909,8 @@ class MySQLTaskWorkStore(TaskWorkStore):
for row in rows:
if row.task_id in open_task_ids:
continue
- await session.delete(row)
deleted.append(DeletedTask(row.task_id, row.account_id, row.user_id))
+ await self._delete_task_records(session, [task.task_id for task in deleted])
return DeleteExpiredResult(
tuple(deleted),
scanned=len(rows),
@@ -933,7 +957,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
Task = self._models.Task
await session.execute(
update(Task)
- .where(Task.task_id == task_id)
+ .where(*self._task_query(Task, task_id, account_id, user_id))
.values(
status=task.status.value,
updated_at=task.updated_at,
@@ -1008,9 +1032,8 @@ class MySQLTaskWorkStore(TaskWorkStore):
selected.append(Task.stage)
if "meta" not in patch.clear_fields and patch.meta is not None:
selected.append(Task.meta)
- if (
- "auth" not in patch.clear_fields
- and (patch.auth is not None or patch.add_enqueue_complete_work_ids)
+ if "auth" not in patch.clear_fields and (
+ patch.auth is not None or patch.add_enqueue_complete_work_ids
):
selected.append(Task.auth)
result = await session.execute(
@@ -1163,6 +1186,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
values["result"] = deepcopy(result)
if resource_id is not None:
values["resource_id"] = resource_id
+ values["resource_id_hash"] = _resource_id_hash(resource_id)
values["active_business_key"] = (
_business_key(
row.account_id,
@@ -1323,9 +1347,10 @@ class MySQLTaskWorkStore(TaskWorkStore):
row = result.one_or_none()
if row is None:
return _not_found_result()
- if row.status in {
- item.value for item in TERMINAL_TASK_STATUSES
- } or row.status == TaskStatus.CANCELLING.value:
+ if (
+ row.status in {item.value for item in TERMINAL_TASK_STATUSES}
+ or row.status == TaskStatus.CANCELLING.value
+ ):
return StoreOperationResult.rejected(
StoreOperationCode.ALREADY_TERMINAL
if row.status in {item.value for item in TERMINAL_TASK_STATUSES}
@@ -1337,6 +1362,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
return StoreOperationResult.noop(StoreOperationCode.DUPLICATE)
session.add(
Work(
+ service_name=self._service_name,
task_id=work.task_id,
work_id=work.work_id,
account_id=account_id,
@@ -1377,6 +1403,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
if existing is None:
session.add(
self._models.Work(
+ service_name=self._service_name,
task_id=work.task_id,
work_id=work.work_id,
account_id=account_id,
@@ -1400,8 +1427,13 @@ class MySQLTaskWorkStore(TaskWorkStore):
await session.execute(
update(self._models.Work)
.where(
- self._models.Work.task_id == work.task_id,
- self._models.Work.work_id == work.work_id,
+ *self._work_query(
+ self._models.Work,
+ work.task_id,
+ work.work_id,
+ None,
+ None,
+ )
)
.values(queue_name=work.queue_name)
)
@@ -1695,12 +1727,8 @@ class MySQLTaskWorkStore(TaskWorkStore):
) -> bool:
Work = self._models.Work
async with self._session_factory() as session:
- predicates = list(
- self._work_query(Work, task_id, None, account_id, user_id)
- )
- predicates.append(
- Work.state.not_in([WorkState.DONE.value, WorkState.FAILED.value])
- )
+ predicates = list(self._work_query(Work, task_id, None, account_id, user_id))
+ predicates.append(Work.state.not_in([WorkState.DONE.value, WorkState.FAILED.value]))
if exclude_work_id is not None:
predicates.append(Work.work_id != exclude_work_id)
return bool(await session.scalar(select(exists().where(*predicates))))
@@ -1739,6 +1767,7 @@ class MySQLTaskWorkStore(TaskWorkStore):
rows = (
await session.scalars(
select(Task.task_id).where(
+ Task.service_name == self._service_name,
Task.task_id.in_(ids),
Task.account_id == account_id,
Task.user_id == user_id,
diff --git a/openviking/service/task_store_mysql_schema.py b/openviking/service/task_store_mysql_schema.py
index c959c11b0..eaa0e82cb 100644
--- a/openviking/service/task_store_mysql_schema.py
+++ b/openviking/service/task_store_mysql_schema.py
@@ -8,8 +8,8 @@ import re
from dataclasses import dataclass
from typing import Any, Optional
-from sqlalchemy import Boolean, ForeignKey, Index, Integer, String, Text, UniqueConstraint
-from sqlalchemy.dialects.mysql import DOUBLE, JSON, VARBINARY
+from sqlalchemy import Boolean, Index, Integer, String, Text, UniqueConstraint
+from sqlalchemy.dialects.mysql import DOUBLE, JSON
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
# The longest derived name is ix_<prefix>_owner_internal_created (26 extra chars).
@@ -55,43 +55,75 @@ def make_task_store_models(table_prefix: str = "ov_tasks") -> TaskStoreModels:
__tablename__ = table_prefix
__table_args__ = (
UniqueConstraint(
+ "service_name",
"active_business_key",
name=f"uq_{table_prefix}_active_business_key",
),
Index(
f"ix_{table_prefix}_owner_created",
+ "service_name",
"account_id",
"user_id",
"created_at",
- "task_id",
),
Index(
f"ix_{table_prefix}_account_created",
+ "service_name",
"account_id",
"created_at",
"task_id",
),
Index(
f"ix_{table_prefix}_created",
+ "service_name",
"created_at",
"task_id",
),
Index(
- f"ix_{table_prefix}_owner_status",
+ f"ix_{table_prefix}_owner",
+ "service_name",
"account_id",
"user_id",
- "status",
),
Index(
f"ix_{table_prefix}_owner_internal_created",
+ "service_name",
"account_id",
"user_id",
"is_internal",
"created_at",
- "task_id",
+ ),
+ Index(
+ f"ix_{table_prefix}_acct_type_created",
+ "service_name",
+ "account_id",
+ "task_type",
+ "created_at",
+ ),
+ Index(
+ f"ix_{table_prefix}_acct_status_created",
+ "service_name",
+ "account_id",
+ "status",
+ "created_at",
+ ),
+ Index(
+ f"ix_{table_prefix}_acct_resource_created",
+ "service_name",
+ "account_id",
+ "resource_id_hash",
+ "created_at",
+ ),
+ Index(
+ f"ix_{table_prefix}_acct_internal_created",
+ "service_name",
+ "account_id",
+ "is_internal",
+ "created_at",
),
Index(
f"ix_{table_prefix}_expiry",
+ "service_name",
"status",
"updated_at",
"task_id",
@@ -103,14 +135,20 @@ def make_task_store_models(table_prefix: str = "ov_tasks") -> TaskStoreModels:
},
)
+ service_name: Mapped[str] = mapped_column(
+ String(255, collation="utf8mb4_bin"), primary_key=True
+ )
task_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"), primary_key=True)
task_type: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"))
status: Mapped[str] = mapped_column(String(16, collation="ascii_bin"))
- created_at: Mapped[float] = mapped_column(DOUBLE)
- updated_at: Mapped[float] = mapped_column(DOUBLE)
+ created_at: Mapped[float] = mapped_column(DOUBLE(asdecimal=False))
+ updated_at: Mapped[float] = mapped_column(DOUBLE(asdecimal=False))
resource_id: Mapped[Optional[str]] = mapped_column(
Text(collation="utf8mb4_bin"), nullable=True
)
+ resource_id_hash: Mapped[Optional[str]] = mapped_column(
+ String(64, collation="ascii_bin"), nullable=True
+ )
account_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"))
user_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"))
meta: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
@@ -118,30 +156,35 @@ def make_task_store_models(table_prefix: str = "ov_tasks") -> TaskStoreModels:
result: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
error: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
auth: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
- processing_seconds: Mapped[Optional[float]] = mapped_column(DOUBLE, nullable=True)
+ processing_seconds: Mapped[Optional[float]] = mapped_column(
+ DOUBLE(asdecimal=False), nullable=True
+ )
reject_if_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
is_internal: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
- active_business_key: Mapped[Optional[bytes]] = mapped_column(VARBINARY(32), nullable=True)
+ active_business_key: Mapped[Optional[str]] = mapped_column(
+ String(64, collation="ascii_bin"), nullable=True
+ )
class Work(Base):
__tablename__ = work_table
__table_args__ = (
Index(
f"ix_{work_table}_state",
+ "service_name",
"task_id",
"state",
),
Index(
f"ix_{work_table}_queue",
+ "service_name",
"task_id",
"queue_name",
- "state",
),
Index(
f"ix_{work_table}_owner",
+ "service_name",
"account_id",
"user_id",
- "task_id",
),
{
"mysql_engine": "InnoDB",
@@ -150,15 +193,10 @@ def make_task_store_models(table_prefix: str = "ov_tasks") -> TaskStoreModels:
},
)
- task_id: Mapped[str] = mapped_column(
- String(255, collation="utf8mb4_bin"),
- ForeignKey(
- f"{table_prefix}.task_id",
- ondelete="CASCADE",
- name=f"fk_{work_table}_task",
- ),
- primary_key=True,
+ service_name: Mapped[str] = mapped_column(
+ String(255, collation="utf8mb4_bin"), primary_key=True
)
+ task_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"), primary_key=True)
work_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"), primary_key=True)
account_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"))
user_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"))
@@ -168,10 +206,10 @@ def make_task_store_models(table_prefix: str = "ov_tasks") -> TaskStoreModels:
error: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
errors: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
context_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
- enqueued_at: Mapped[float] = mapped_column(DOUBLE)
- started_at: Mapped[Optional[float]] = mapped_column(DOUBLE, nullable=True)
- finished_at: Mapped[Optional[float]] = mapped_column(DOUBLE, nullable=True)
- updated_at: Mapped[float] = mapped_column(DOUBLE)
+ enqueued_at: Mapped[float] = mapped_column(DOUBLE(asdecimal=False))
+ started_at: Mapped[Optional[float]] = mapped_column(DOUBLE(asdecimal=False), nullable=True)
+ finished_at: Mapped[Optional[float]] = mapped_column(DOUBLE(asdecimal=False), nullable=True)
+ updated_at: Mapped[float] = mapped_column(DOUBLE(asdecimal=False))
class Event(Base):
__tablename__ = event_table
@@ -183,15 +221,10 @@ def make_task_store_models(table_prefix: str = "ov_tasks") -> TaskStoreModels:
},
)
- task_id: Mapped[str] = mapped_column(
- String(255, collation="utf8mb4_bin"),
- ForeignKey(
- f"{table_prefix}.task_id",
- ondelete="CASCADE",
- name=f"fk_{event_table}_task",
- ),
- primary_key=True,
+ service_name: Mapped[str] = mapped_column(
+ String(255, collation="utf8mb4_bin"), primary_key=True
)
+ task_id: Mapped[str] = mapped_column(String(255, collation="utf8mb4_bin"), primary_key=True)
seq: Mapped[int] = mapped_column(Integer, primary_key=True)
recorded_at: Mapped[str] = mapped_column(String(64, collation="ascii_bin"))
kind: Mapped[str] = mapped_column(String(64, collation="utf8mb4_bin"))
diff --git a/openviking_cli/utils/config/storage_config.py b/openviking_cli/utils/config/storage_config.py
index 4b4f6aedc..b41acfdee 100644
--- a/openviking_cli/utils/config/storage_config.py
+++ b/openviking_cli/utils/config/storage_config.py
@@ -138,7 +138,7 @@ class StorageConfig(BaseModel):
upload_temp_dir.mkdir(parents=True, exist_ok=True)
return upload_temp_dir
- def build_task_tracker(self, agfs: Any):
+ def build_task_tracker(self, agfs: Any, *, service_name: str | None = None):
"""Build the persistent TaskTracker from storage config."""
from openviking.service.task_store import TaskWorkStore
from openviking.service.task_tracker import TaskTracker
@@ -147,7 +147,7 @@ class StorageConfig(BaseModel):
if self.task_store.provider == "mysql":
from openviking.service.task_store_mysql import MySQLTaskWorkStore
- store = MySQLTaskWorkStore(params=self.task_store.params)
+ store = MySQLTaskWorkStore(params=self.task_store.params, service_name=service_name)
else:
from openviking.service.task_store import PersistentTaskStore
diff --git a/pyproject.toml b/pyproject.toml
index dc05a57ce..5a224c4ff 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -256,6 +256,10 @@ openviking = [
"lib/ragfs_python*.pyd",
"bin/ov",
"bin/ov.exe",
+ "server/account_stores/sql/*.sql",
+ "server/account_stores/sql/changes/*.sql",
+ "service/sql/*.sql",
+ "service/sql/changes/*.sql",
"storage/vectordb/engine/*.abi3.so",
"storage/vectordb/engine/*.pyd",
]
diff --git a/sdk/go/system.go b/sdk/go/system.go
index 85f33d501..e031199d3 100644
--- a/sdk/go/system.go
+++ b/sdk/go/system.go
@@ -11,7 +11,8 @@ import (
)
// WaitProcessed waits until all queued processing completes.
-func (c *Client) WaitProcessed(ctx context.Context) (map[string]any, error) {
+// The optional opts argument is deprecated and ignored.
+func (c *Client) WaitProcessed(ctx context.Context, opts ...*WaitProcessedOptions) (map[string]any, error) {
payload := map[string]any{}
var result map[string]any
err := c.doJSON(ctx, http.MethodPost, "/api/v1/system/wait", nil, payload, &result)
diff --git a/sdk/go/types.go b/sdk/go/types.go
index 6600209be..33d8c11bf 100644
--- a/sdk/go/types.go
+++ b/sdk/go/types.go
@@ -25,14 +25,16 @@ type Config struct {
// AddResourceOptions controls AddResource.
type AddResourceOptions struct {
- ACL *ACLSpec
- To string
- Parent string
- CreateParent *bool
- AddType string
- Reason string
- Instruction string
- Wait bool
+ ACL *ACLSpec
+ To string
+ Parent string
+ CreateParent *bool
+ AddType string
+ Reason string
+ Instruction string
+ Wait bool
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64
Strict bool
IgnoreDirs string
Include string
@@ -50,7 +52,9 @@ type AddResourceOptions struct {
// AddSkillOptions controls AddSkill.
type AddSkillOptions struct {
- Wait bool
+ Wait bool
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64
Telemetry any
Extra map[string]any
// TargetURI scopes the operation to a skills root such as
@@ -138,7 +142,9 @@ type GetSkillOptions struct {
// UpdateSkillOptions controls UpdateSkill.
type UpdateSkillOptions struct {
- Wait bool
+ Wait bool
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64
SourceMetadata map[string]any
Telemetry any
TargetURI any
@@ -150,6 +156,14 @@ type DeleteSkillOptions struct {
TargetURI any
}
+// WaitProcessedOptions controls WaitProcessed.
+//
+// Deprecated: Waiting-specific timeout options are ignored.
+type WaitProcessedOptions struct {
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64 `json:"timeout,omitempty"`
+}
+
// ObserverStatusOptions controls observer status formatting.
type ObserverStatusOptions struct {
Format string
@@ -231,13 +245,17 @@ type TreePage struct {
type RemoveOptions struct {
Recursive bool
Wait bool
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64
}
// WriteOptions controls Write.
type WriteOptions struct {
- ACL *ACLSpec
- Mode string
- Wait bool
+ ACL *ACLSpec
+ Mode string
+ Wait bool
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64
Telemetry any
ProcessingMode string
Tags []string
@@ -260,9 +278,11 @@ type BatchWriteOperation struct {
type BatchWriteOptions struct {
DeferProcessing bool
Wait *bool
- Telemetry any
- ProcessingMode string
- Extra map[string]any
+ // Deprecated: Timeout is accepted for compatibility and ignored.
+ Timeout *float64
+ Telemetry any
+ ProcessingMode string
+ Extra map[string]any
}
// SetTagsOptions controls SetTags.
diff --git a/sdk/python/openviking_sdk/client.py b/sdk/python/openviking_sdk/client.py
index 442fa6ddc..7c5882d01 100644
--- a/sdk/python/openviking_sdk/client.py
+++ b/sdk/python/openviking_sdk/client.py
@@ -10,7 +10,7 @@ import os
import tempfile
from enum import Enum
from pathlib import Path
-from typing import Any, Callable, Dict, List, Literal, Mapping, Optional, Type, Union
+from typing import Any, Callable, Dict, List, Literal, Mapping, Optional, Type, TypeVar, Union, cast
from urllib.parse import quote
import httpx
@@ -97,6 +97,7 @@ ERROR_CODE_TO_EXCEPTION = {
GATEWAY_MARKER_HEADER = "X-VikingBot-Gateway"
GATEWAY_TOKEN_HEADER = "X-Gateway-Token"
_SESSION_CONFIG_UNSET = object()
+_OptionsT = TypeVar("_OptionsT", bound=Mapping[str, Any])
def _option_keys(options_type: Type[Any]) -> set[str]:
@@ -107,6 +108,19 @@ def _option_keys(options_type: Type[Any]) -> set[str]:
return set(getattr(options_type, "__annotations__", {}))
+def _normalize_timeout_options(timeout: Any, options: Optional[_OptionsT]) -> Optional[_OptionsT]:
+ """Accept options in the positional slot used after timeout was removed.
+
+ Timeout values are deprecated and ignored. A mapping in that slot is treated
+ as options; supplying options in both slots is an error.
+ """
+ if isinstance(timeout, Mapping):
+ if options is not None:
+ raise TypeError("options were provided in both the timeout and options slots")
+ return cast(_OptionsT, timeout)
+ return options
+
+
def _image_mime_type(file_name: str = "") -> str:
mime_type, _ = mimetypes.guess_type(file_name or "")
if mime_type and mime_type.startswith("image/"):
@@ -715,9 +729,10 @@ class AsyncHTTPClient:
to: Optional[str] = None,
parent: Optional[str] = None,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[AddResourceOptions] = None,
) -> Dict[str, Any]:
- option_values = dict(options or {})
+ option_values = dict(_normalize_timeout_options(timeout, options) or {})
add_type = option_values.get("add_type")
if add_type is not None:
add_type = add_type.strip() or None
@@ -790,9 +805,10 @@ class AsyncHTTPClient:
self,
data: Any,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[AddSkillOptions] = None,
) -> Dict[str, Any]:
- option_values = dict(options or {})
+ option_values = dict(_normalize_timeout_options(timeout, options) or {})
if "target_uri" in option_values:
option_values["target_uri"] = VikingURI.normalize(option_values["target_uri"])
request_data = self._build_options_payload(
@@ -900,9 +916,10 @@ class AsyncHTTPClient:
skill_name: str,
data: Any,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[UpdateSkillOptions] = None,
) -> Dict[str, Any]:
- option_values = dict(options or {})
+ option_values = dict(_normalize_timeout_options(timeout, options) or {})
if "target_uri" in option_values:
option_values["target_uri"] = VikingURI.normalize(option_values["target_uri"])
request_data = self._build_options_payload(
@@ -1035,7 +1052,8 @@ class AsyncHTTPClient:
)
return self._handle_response(response)
- async def wait_processed(self) -> Dict[str, Any]:
+ async def wait_processed(self, timeout: Optional[float] = None) -> Dict[str, Any]:
+ """Wait for processing; timeout is deprecated and ignored."""
response = await self._request(
"POST",
"/api/v1/system/wait",
@@ -1261,6 +1279,7 @@ class AsyncHTTPClient:
uri: str,
recursive: bool = False,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
) -> None:
params = {"uri": VikingURI.normalize(uri), "recursive": recursive, "wait": wait}
response = await self._request("DELETE", "/api/v1/fs", params=params)
@@ -1329,10 +1348,11 @@ class AsyncHTTPClient:
content: str,
mode: str = "replace",
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[WriteOptions] = None,
) -> Dict[str, Any]:
payload = self._build_options_payload(
- options,
+ _normalize_timeout_options(timeout, options),
WriteOptions,
fixed={
"uri": VikingURI.normalize(uri),
@@ -1353,6 +1373,7 @@ class AsyncHTTPClient:
root_uri: str,
operations: List[Dict[str, Any]],
wait: bool = True,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[BatchWriteOptions] = None,
) -> Dict[str, Any]:
"""Apply multiple content writes, then refresh semantics once."""
@@ -1362,7 +1383,7 @@ class AsyncHTTPClient:
item["uri"] = VikingURI.normalize(str(item.get("uri") or ""))
normalized_operations.append(item)
payload = self._build_options_payload(
- options,
+ _normalize_timeout_options(timeout, options),
BatchWriteOptions,
fixed={
"root_uri": VikingURI.normalize(root_uri),
@@ -2484,6 +2505,7 @@ class SyncHTTPClient:
to: Optional[str] = None,
parent: Optional[str] = None,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[AddResourceOptions] = None,
) -> Dict[str, Any]:
return run_async(
@@ -2492,7 +2514,7 @@ class SyncHTTPClient:
to=to,
parent=parent,
wait=wait,
- options=options,
+ options=_normalize_timeout_options(timeout, options),
)
)
@@ -2508,9 +2530,14 @@ class SyncHTTPClient:
self,
data: Any,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[AddSkillOptions] = None,
) -> Dict[str, Any]:
- return run_async(self._async_client.add_skill(data, wait=wait, options=options))
+ return run_async(
+ self._async_client.add_skill(
+ data, wait=wait, options=_normalize_timeout_options(timeout, options)
+ )
+ )
def list_skills(
self,
@@ -2586,10 +2613,13 @@ class SyncHTTPClient:
skill_name: str,
data: Any,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[UpdateSkillOptions] = None,
) -> Dict[str, Any]:
return run_async(
- self._async_client.update_skill(skill_name, data, wait=wait, options=options)
+ self._async_client.update_skill(
+ skill_name, data, wait=wait, options=_normalize_timeout_options(timeout, options)
+ )
)
def delete_skill(
@@ -2650,7 +2680,8 @@ class SyncHTTPClient:
) -> Dict[str, Any]:
return run_async(self._async_client.trigger_watch(task_id, to_uri=to_uri))
- def wait_processed(self) -> Dict[str, Any]:
+ def wait_processed(self, timeout: Optional[float] = None) -> Dict[str, Any]:
+ """Wait for processing; timeout is deprecated and ignored."""
return run_async(self._async_client.wait_processed())
def ls(
@@ -2825,6 +2856,7 @@ class SyncHTTPClient:
uri: str,
recursive: bool = False,
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
) -> None:
run_async(self._async_client.rm(uri, recursive=recursive, wait=wait))
@@ -2852,10 +2884,17 @@ class SyncHTTPClient:
content: str,
mode: str = "replace",
wait: bool = False,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[WriteOptions] = None,
) -> Dict[str, Any]:
return run_async(
- self._async_client.write(uri, content, mode=mode, wait=wait, options=options)
+ self._async_client.write(
+ uri,
+ content,
+ mode=mode,
+ wait=wait,
+ options=_normalize_timeout_options(timeout, options),
+ )
)
def batch_write(
@@ -2863,10 +2902,16 @@ class SyncHTTPClient:
root_uri: str,
operations: List[Dict[str, Any]],
wait: bool = True,
+ timeout: Optional[float] = None, # Deprecated and ignored.
options: Optional[BatchWriteOptions] = None,
) -> Dict[str, Any]:
return run_async(
- self._async_client.batch_write(root_uri, operations, wait=wait, options=options)
+ self._async_client.batch_write(
+ root_uri,
+ operations,
+ wait=wait,
+ options=_normalize_timeout_options(timeout, options),
+ )
)
def set_tags(
diff --git a/sdk/python/tests/test_async_client_behaviors.py b/sdk/python/tests/test_async_client_behaviors.py
index cc9f94bcb..60d267037 100644
--- a/sdk/python/tests/test_async_client_behaviors.py
+++ b/sdk/python/tests/test_async_client_behaviors.py
@@ -20,6 +20,59 @@ from openviking_sdk.client import Session, SyncSession
from openviking_sdk.errors import NotFoundError
[email protected]("client_type", [AsyncHTTPClient, SyncHTTPClient])
+ "method_name,args",
+ [
+ ("add_resource", ("https://example.com/doc.md", None, None, True)),
+ ("add_skill", ({"name": "demo"}, True)),
+ ("update_skill", ("demo", {"name": "demo"}, True)),
+ ("write", ("viking://resources/demo.md", "hello", "replace", True)),
+ (
+ "batch_write",
+ (
+ "viking://resources/demo",
+ [{"uri": "viking://resources/demo/a.md", "content": "hello"}],
+ True,
+ ),
+ ),
+ ],
+)
+async def test_write_clients_preserve_options_when_timeout_is_ignored(
+ client_type, method_name, args
+):
+ client = client_type(url="http://localhost:1933")
+ async_client = client if client_type is AsyncHTTPClient else client._async_client
+ async_client._request = AsyncMock(return_value=object())
+ async_client._handle_response_data = lambda _response: {"result": {"ok": True}}
+ method = getattr(client, method_name)
+ options = {"telemetry": {"enabled": True}}
+
+ async def invoke(*positional, **keywords):
+ result = method(*positional, **keywords)
+ return await result if client_type is AsyncHTTPClient else result
+
+ assert await invoke(*args, options=options) == {"ok": True}
+ expected_request = async_client._request.await_args
+ assert expected_request.kwargs["json"]["telemetry"] == options["telemetry"]
+ assert "timeout" not in expected_request.kwargs["json"]
+ for positional, keywords in (
+ ((*args, options), {}),
+ ((*args, 30, options), {}),
+ (args, {"timeout": 30, "options": options}),
+ ):
+ async_client._request.reset_mock()
+ assert await invoke(*positional, **keywords) == {"ok": True}
+ assert async_client._request.await_args == expected_request
+
+ async_client._request.reset_mock()
+ with pytest.raises(TypeError, match="options were provided in both"):
+ await invoke(*args, options, options=options)
+ async_client._request.assert_not_awaited()
+ assert options == {"telemetry": {"enabled": True}}
+
+
@pytest.mark.asyncio
async def test_async_http_client_initialize_forwards_event_hooks():
async def request_hook(_request):
@@ -1216,9 +1269,7 @@ async def test_admin_get_user_reads_membership_from_point_read_path():
client._handle_response = lambda _response: {"user_id": "alice", "role": "admin"}
result = await client.admin_get_user("acct", "alice")
- client._request.assert_awaited_once_with(
- "GET", "/api/v1/admin/accounts/acct/users/alice"
- )
+ client._request.assert_awaited_once_with("GET", "/api/v1/admin/accounts/acct/users/alice")
assert result == {"user_id": "alice", "role": "admin"}
assert "api_key" not in result and "key_prefix" not in result
diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.ts
index 51edacee3..37edc0863 100644
--- a/sdk/typescript/src/client.ts
+++ b/sdk/typescript/src/client.ts
@@ -612,7 +612,12 @@ export class OpenVikingClient {
/** Remove a resource or directory. */
remove(
uri: string,
- options: { recursive?: boolean; wait?: boolean } = {},
+ options: {
+ recursive?: boolean;
+ wait?: boolean;
+ /** @deprecated Accepted for compatibility and ignored. */
+ timeout?: number;
+ } = {},
): Promise<void> {
return this.request("DELETE", "/api/v1/fs", {
query: {
@@ -1079,8 +1084,11 @@ export class OpenVikingClient {
},
});
}
- /** Wait for queued processing to finish. */
- waitProcessed(): Promise<JsonObject> {
+ /**
+ * Wait for queued processing to finish.
+ * @param timeout - Deprecated. Accepted for compatibility and ignored.
+ */
+ waitProcessed(timeout?: number): Promise<JsonObject> {
return this.request("POST", "/api/v1/system/wait", {
body: {},
});
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index be2492d28..c50db55f0 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -74,6 +74,8 @@ export interface RequestOptions {
/** Options shared by asynchronous processing APIs. */
export interface WaitOptions {
wait?: boolean;
+ /** @deprecated Accepted for compatibility and ignored. */
+ timeout?: number;
telemetry?: unknown;
}
/** Writable direct grants and inheritance mode. */
diff --git a/setup.py b/setup.py
index 74ba4393f..50f37b72d 100644
--- a/setup.py
+++ b/setup.py
@@ -521,18 +521,14 @@ class OpenVikingBuildPy(build_py):
for asset_name in ("workspace", "bridge"):
source = SETUP_DIR / "bot" / asset_name
if not source.is_dir():
- raise FileNotFoundError(
- f"required VikingBot asset directory not found: {source}"
- )
+ raise FileNotFoundError(f"required VikingBot asset directory not found: {source}")
destination = package_root / asset_name
if destination.exists():
shutil.rmtree(destination)
shutil.copytree(
source,
destination,
- ignore=shutil.ignore_patterns(
- "node_modules", "dist", "__pycache__", "*.pyc"
- ),
+ ignore=shutil.ignore_patterns("node_modules", "dist", "__pycache__", "*.pyc"),
)
@@ -585,6 +581,10 @@ setup(
"lib/ragfs_python*.pyd",
"bin/ov",
"bin/ov.exe",
+ "server/account_stores/sql/*.sql",
+ "server/account_stores/sql/changes/*.sql",
+ "service/sql/*.sql",
+ "service/sql/changes/*.sql",
"server/static/**/*",
"web_studio/dist/**/*",
"storage/vectordb/engine/*.abi3.so",
diff --git a/tests/server/test_account_services.py b/tests/server/test_account_services.py
index 4a5ede6d9..5e27963a8 100644
--- a/tests/server/test_account_services.py
+++ b/tests/server/test_account_services.py
@@ -5,6 +5,7 @@ import asyncio
import json
import os
import threading
+from datetime import datetime
from types import SimpleNamespace
from uuid import uuid4
@@ -13,7 +14,14 @@ from sqlalchemy import delete, func, select
from sqlalchemy.ext.asyncio import create_async_engine
from openviking.server.account_stores.file import FileStore
-from openviking.server.account_stores.mysql_schema import Account, Base, Credential, GroupMember
+from openviking.server.account_stores.mysql import MySQLAccountStore
+from openviking.server.account_stores.mysql_schema import (
+ Account,
+ Base,
+ Credential,
+ GroupMember,
+ User,
+)
from openviking.server.api_keys import APIKeyManager, generate_api_key
from openviking.server.identity import Role
from openviking.server.store_assembly import build_api_key_manager
@@ -29,6 +37,48 @@ def _file_store() -> FileStore:
return FileStore(SimpleNamespace(agfs=InMemoryAGFS()))
+def test_mysql_account_schema_defines_management_list_indexes():
+ indexes = {
+ str(index.name): tuple(column.name for column in index.columns)
+ for table_name in (Account.__tablename__, User.__tablename__)
+ for index in Base.metadata.tables[table_name].indexes
+ }
+
+ assert indexes["ix_ov_accounts_account_created"] == (
+ "service_name",
+ "created_at",
+ "account_id",
+ )
+ assert indexes["ix_ov_accounts_user_active_created"] == (
+ "service_name",
+ "account_pk",
+ "deletion_task_id",
+ "created_at",
+ "user_pk",
+ )
+ assert indexes["ix_ov_accounts_user_role_active_created"] == (
+ "service_name",
+ "account_pk",
+ "role",
+ "deletion_task_id",
+ "created_at",
+ "user_pk",
+ )
+
+
+def test_mysql_account_summary_formats_naive_datetimes_as_utc():
+ summary = MySQLAccountStore._account_summary(
+ SimpleNamespace(
+ account_id="default",
+ created_at=datetime(2026, 10, 24, 1, 2, 3, 456789),
+ deletion_task_id=None,
+ ),
+ 0,
+ )
+
+ assert summary["created_at"] == "2026-10-24T01:02:03.456789+00:00"
+
+
@pytest.mark.parametrize(
"account_id,user_id",
[
@@ -259,6 +309,53 @@ async def test_regenerate_key_preserves_management_not_found_errors():
await manager.regenerate_key("acme", "missing")
+async def test_mysql_load_creates_default_account():
+ raw_mysql = os.environ.get("OV_TEST_MYSQL")
+ if not raw_mysql:
+ pytest.skip("Set OV_TEST_MYSQL to exercise MySQL account-store startup")
+
+ service_name = f"test-{uuid4().hex}"
+ manager = build_api_key_manager(
+ "root",
+ None,
+ service_name=service_name,
+ account_store_provider="mysql",
+ account_store_params=json.loads(raw_mysql),
+ )
+ mysql = manager._store
+ assert isinstance(mysql, MySQLAccountStore)
+ engine = create_async_engine(mysql._url, **mysql._engine_options)
+ try:
+ async with engine.begin() as connection:
+ await connection.run_sync(Base.metadata.create_all)
+
+ await manager.load()
+
+ accounts = await manager.list_accounts()
+ assert [account["account_id"] for account in accounts] == ["default"]
+ assert accounts[0]["user_count"] == 0
+ await manager.close()
+ await manager.load()
+
+ async with engine.connect() as connection:
+ assert (
+ await connection.scalar(
+ select(func.count())
+ .select_from(Account)
+ .where(
+ Account.service_name == service_name,
+ Account.account_id == "default",
+ )
+ )
+ == 1
+ )
+ finally:
+ await manager.close()
+ async with engine.begin() as connection:
+ await connection.execute(delete(Account).where(Account.service_name == service_name))
+ await engine.dispose()
+
+
async def test_distributed_authentication_state_is_shared_and_service_scoped():
"""Key rotation and group deletion refresh caches within the owning service."""
raw_mysql = os.environ.get("OV_TEST_MYSQL")
diff --git a/tests/server/test_api_resources.py b/tests/server/test_api_resources.py
index 90032cbf5..886ed44f1 100644
--- a/tests/server/test_api_resources.py
+++ b/tests/server/test_api_resources.py
@@ -171,11 +171,7 @@ async def test_add_resource_with_wait(
"reason": "test resource",
"wait": True,
}
- rejected = await client.post("/api/v1/resources", json={**payload, "timeout": 1.0})
- assert rejected.status_code == 400
- assert rejected.json()["error"]["code"] == "INVALID_ARGUMENT"
-
- resp = await client.post("/api/v1/resources", json=payload)
+ resp = await client.post("/api/v1/resources", json={**payload, "timeout": 0.0})
assert resp.status_code == 200
body = resp.json()
assert body["status"] == "ok"
diff --git a/tests/service/test_task_work_contract.py b/tests/service/test_task_work_contract.py
index 975e24aa5..1c302138d 100644
--- a/tests/service/test_task_work_contract.py
+++ b/tests/service/test_task_work_contract.py
@@ -11,7 +11,7 @@ from uuid import uuid4
import pytest
-from openviking.service.task_domain import TaskStatus, WorkState
+from openviking.service.task_domain import TaskRetentionPolicy, TaskStatus, WorkState
from openviking.service.task_store import PersistentTaskStore, WorkRegistration
from openviking.service.task_tracker import TaskTracker
from tests.test_task_tracker import _FakeAgfs
@@ -60,8 +60,97 @@ async def test_task_work_wait_includes_recovered_and_later_enqueued_work(tracker
await asyncio.gather(waiting, return_exceptions=True)
+def test_mysql_task_store_schema_uses_service_name_partition():
+ from openviking.service.task_store_mysql import MySQLTaskWorkStore
+ from openviking.service.task_store_mysql_schema import make_task_store_models
+
+ store = MySQLTaskWorkStore(
+ params={
+ "user": "openviking",
+ "password": "change-me",
+ "database": "openviking",
+ "service_name": "tenant-a",
+ }
+ )
+ assert store._service_name == "tenant-a"
+
+ override = MySQLTaskWorkStore(
+ params={
+ "user": "openviking",
+ "password": "change-me",
+ "database": "openviking",
+ "service_name": "tenant-a",
+ },
+ service_name="tenant-b",
+ )
+ assert override._service_name == "tenant-b"
+
+ models = make_task_store_models("ov_contract")
+ Task, Work, Event = models.Task, models.Work, models.Event
+
+ assert [column.name for column in Task.__table__.primary_key.columns] == [
+ "service_name",
+ "task_id",
+ ]
+ assert [column.name for column in Work.__table__.primary_key.columns] == [
+ "service_name",
+ "task_id",
+ "work_id",
+ ]
+ assert [column.name for column in Event.__table__.primary_key.columns] == [
+ "service_name",
+ "task_id",
+ "seq",
+ ]
+
+ task_uniques = {
+ constraint.name: [column.name for column in constraint.columns]
+ for constraint in Task.__table__.constraints
+ if constraint.name
+ }
+ assert task_uniques["uq_ov_contract_active_business_key"] == [
+ "service_name",
+ "active_business_key",
+ ]
+ task_indexes = {
+ index.name: [column.name for column in index.columns] for index in Task.__table__.indexes
+ }
+ assert task_indexes["ix_ov_contract_owner_created"] == [
+ "service_name",
+ "account_id",
+ "user_id",
+ "created_at",
+ ]
+ assert task_indexes["ix_ov_contract_acct_type_created"] == [
+ "service_name",
+ "account_id",
+ "task_type",
+ "created_at",
+ ]
+ assert task_indexes["ix_ov_contract_acct_status_created"] == [
+ "service_name",
+ "account_id",
+ "status",
+ "created_at",
+ ]
+ assert task_indexes["ix_ov_contract_acct_resource_created"] == [
+ "service_name",
+ "account_id",
+ "resource_id_hash",
+ "created_at",
+ ]
+ assert task_indexes["ix_ov_contract_acct_internal_created"] == [
+ "service_name",
+ "account_id",
+ "is_internal",
+ "created_at",
+ ]
+ assert "resource_id_hash" in Task.__table__.columns
+
+
@pytest.mark.asyncio
-async def test_mysql_requeue_and_concurrent_completion_are_durable():
[email protected]("deletion", ["manual", "expired"])
+async def test_mysql_requeue_and_concurrent_completion_are_durable(deletion):
raw = os.environ.get("OV_TEST_MYSQL")
if not raw:
pytest.skip("Set OV_TEST_MYSQL to exercise the shared Task/Work contract")
@@ -72,16 +161,27 @@ async def test_mysql_requeue_and_concurrent_completion_are_durable():
params = {**json.loads(raw), "table": f"ov_contract_{uuid4().hex[:12]}"}
first = task_store_mysql.MySQLTaskWorkStore(params=params)
second = task_store_mysql.MySQLTaskWorkStore(params=params)
+ isolated_store = task_store_mysql.MySQLTaskWorkStore(
+ params=params,
+ service_name="contract-isolated",
+ )
admin = sqlalchemy.create_async_engine(first._url, **first._engine_options)
- schema = Path(task_store_mysql.__file__).parent / "sql/task_store_init.sql"
+ sql_dir = Path(task_store_mysql.__file__).parent / "sql"
+ schema_files = [
+ sql_dir / "task_store_init.sql",
+ sql_dir / "changes/0001_task_store_service_name.sql",
+ ]
writer, observer = TaskTracker(first), TaskTracker(second)
+ isolated = TaskTracker(isolated_store)
try:
async with admin.begin() as connection:
- for statement in schema.read_text().replace("ov_tasks", params["table"]).split(";"):
- if "CREATE TABLE" in statement:
- await connection.exec_driver_sql(statement)
+ for schema in schema_files:
+ for statement in schema.read_text().replace("ov_tasks", params["table"]).split(";"):
+ if statement.strip():
+ await connection.exec_driver_sql(statement)
await first.load()
await second.load()
+ await isolated_store.load()
uri = "viking://resources/" + "/".join(["directory" * 4] * 8)
candidates = await asyncio.gather(
writer.create_if_no_running("content_write", uri, **OWNER),
@@ -92,6 +192,13 @@ async def test_mysql_requeue_and_concurrent_completion_are_durable():
assert await observer.get(task_id, account_id="other", user_id=OWNER["user_id"]) is None
assert await observer.get(task_id, account_id=OWNER["account_id"], user_id="other") is None
+ assert await isolated.get(task_id, **OWNER) is None
+ isolated_task = await isolated.create_if_no_running(
+ "content_write", uri, task_id=task_id, **OWNER
+ )
+ assert isolated_task is not None
+ await isolated.register_work(task_id, "semantic", "Semantic", **OWNER)
+
await writer.start(task_id, **OWNER)
await writer.register_work(task_id, "semantic", "Semantic", **OWNER)
await observer.register_work(task_id, "embedding", "Embedding", **OWNER)
@@ -138,11 +245,31 @@ async def test_mysql_requeue_and_concurrent_completion_are_durable():
assert (
await observer.build_queue_status(task_id, **OWNER) == completed.result["queue_status"]
)
- assert await observer.delete_tasks(**OWNER) == 1
+ if deletion == "manual":
+ assert await observer.delete_tasks(**OWNER) == 1
+ else:
+ expired = await second.delete_expired(
+ TaskRetentionPolicy(completed_seconds=0, failed_seconds=0),
+ now=completed.updated_at + 1,
+ limit=1,
+ )
+ assert expired.deleted_count == 1
assert await observer.get(task_id, **OWNER) is None
+ assert await observer.get_work_state(task_id, "semantic", **OWNER) is None
+ assert await observer.get_work_state(task_id, "embedding", **OWNER) is None
+ assert await isolated.get(task_id, **OWNER) is not None
+ assert await isolated.get_work_state(task_id, "semantic", **OWNER) is WorkState.PENDING
+ async with admin.connect() as connection:
+ for table in first._models.table_names:
+ remaining = await connection.exec_driver_sql(
+ f"SELECT COUNT(*) FROM `{table}` WHERE service_name = %s AND task_id = %s",
+ (first._service_name, task_id),
+ )
+ assert remaining.scalar_one() == 0
finally:
await first.close()
await second.close()
+ await isolated_store.close()
async with admin.begin() as connection:
await connection.run_sync(first._models.Base.metadata.drop_all)
await admin.dispose()
diff --git a/tests/test_task_backend_config.py b/tests/test_task_backend_config.py
index aa2a1bd42..2689554f0 100644
--- a/tests/test_task_backend_config.py
+++ b/tests/test_task_backend_config.py
@@ -54,3 +54,66 @@ def test_storage_config_builds_persistent_task_tracker():
tracker = StorageConfig().build_task_tracker(_FakeAgfs())
assert isinstance(tracker, TaskTracker)
assert isinstance(tracker._store, PersistentTaskStore)
+
+
+def test_storage_config_passes_service_name_to_mysql_task_store(monkeypatch):
+ from openviking.service import task_store_mysql
+
+ created = []
+
+ class _FakeMySQLTaskWorkStore:
+ def __init__(self, *, params, service_name):
+ created.append((params, service_name))
+
+ monkeypatch.setattr(task_store_mysql, "MySQLTaskWorkStore", _FakeMySQLTaskWorkStore)
+
+ tracker = StorageConfig.model_validate(
+ {
+ "task_store": {
+ "provider": "mysql",
+ "params": {"user": "u", "password": "p", "database": "db"},
+ }
+ }
+ ).build_task_tracker(_FakeAgfs(), service_name="tenant-a")
+
+ assert isinstance(tracker, TaskTracker)
+ assert tracker._store.__class__ is _FakeMySQLTaskWorkStore
+ assert created == [({"user": "u", "password": "p", "database": "db"}, "tenant-a")]
+
+
+def test_storage_config_allows_mysql_task_store_params_service_name(monkeypatch):
+ from openviking.service import task_store_mysql
+
+ created = []
+
+ class _FakeMySQLTaskWorkStore:
+ def __init__(self, *, params, service_name):
+ created.append((params, service_name))
+
+ monkeypatch.setattr(task_store_mysql, "MySQLTaskWorkStore", _FakeMySQLTaskWorkStore)
+
+ StorageConfig.model_validate(
+ {
+ "task_store": {
+ "provider": "mysql",
+ "params": {
+ "user": "u",
+ "password": "p",
+ "database": "db",
+ "service_name": "tenant-from-params",
+ },
+ },
+ }
+ ).build_task_tracker(_FakeAgfs())
+
+ assert created == [
+ (
+ {
+ "user": "u",
+ "password": "p",
+ "database": "db",
+ "service_name": "tenant-from-params",
+ },
+ None,
+ )
+ ]