Skip to content

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

Fix/reindex preserve owners (#3096)

Jiahui Zhou1c46d44fbcbf
diff --git a/README.md b/README.md
index 4dd0d5e38..40da934aa 100644
--- a/README.md
+++ b/README.md
@@ -598,7 +598,7 @@ ov find "what is openviking"
ov grep "openviking" --uri viking://resources/volcengine/OpenViking/docs/zh
```
-To rebuild existing indexes, `ov reindex <uri> --mode vectors_only` refreshes vectors only. Use `--mode semantic_and_vectors` when semantic artifacts (`.abstract.md` and `.overview.md`) must be regenerated before vectors; there is no `semantic` or `full` mode alias.
+To rebuild existing indexes, `ov reindex <uri> --mode vectors_only` refreshes vectors only. Use `--mode semantic_and_vectors` when semantic artifacts (`.abstract.md` and `.overview.md`) must be regenerated before vectors. Use `--mode prune_orphans` to remove vector records whose source files no longer exist; add `--dry-run` to preview the cleanup. There is no `semantic` or `full` mode alias.
Congratulations! You have successfully run OpenViking 🎉
diff --git a/crates/ov_cli/src/client.rs b/crates/ov_cli/src/client.rs
index 1f7d92afd..d79dfc26d 100644
--- a/crates/ov_cli/src/client.rs
+++ b/crates/ov_cli/src/client.rs
@@ -335,11 +335,18 @@ impl HttpClient {
})
}
- pub async fn reindex(&self, uri: &str, mode: &str, wait: bool) -> Result<serde_json::Value> {
+ pub async fn reindex(
+ &self,
+ uri: &str,
+ mode: &str,
+ wait: bool,
+ dry_run: bool,
+ ) -> Result<serde_json::Value> {
let body = serde_json::json!({
"uri": uri,
"mode": mode,
"wait": wait,
+ "dry_run": dry_run,
});
self.post("/api/v1/content/reindex", &body).await
}
diff --git a/crates/ov_cli/src/commands/content.rs b/crates/ov_cli/src/commands/content.rs
index 593ccf691..3035bfd4e 100644
--- a/crates/ov_cli/src/commands/content.rs
+++ b/crates/ov_cli/src/commands/content.rs
@@ -72,10 +72,11 @@ pub async fn reindex(
uri: &str,
mode: &str,
wait: bool,
+ dry_run: bool,
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
- let result = client.reindex(uri, mode, wait).await?;
+ let result = client.reindex(uri, mode, wait, dry_run).await?;
crate::output::output_success(result, output_format, compact);
Ok(())
}
diff --git a/crates/ov_cli/src/handlers.rs b/crates/ov_cli/src/handlers.rs
index 0114122da..8ff7447f0 100644
--- a/crates/ov_cli/src/handlers.rs
+++ b/crates/ov_cli/src/handlers.rs
@@ -1246,9 +1246,24 @@ pub async fn handle_set_tags(
.await
}
-pub async fn handle_reindex(uri: String, mode: String, wait: bool, ctx: CliContext) -> Result<()> {
+pub async fn handle_reindex(
+ uri: String,
+ mode: String,
+ wait: bool,
+ dry_run: bool,
+ ctx: CliContext,
+) -> Result<()> {
let client = ctx.get_client();
- commands::content::reindex(&client, &uri, &mode, wait, ctx.output_format, ctx.compact).await
+ commands::content::reindex(
+ &client,
+ &uri,
+ &mode,
+ wait,
+ dry_run,
+ ctx.output_format,
+ ctx.compact,
+ )
+ .await
}
pub async fn handle_get(uri: String, local_path: String, ctx: CliContext) -> Result<()> {
diff --git a/crates/ov_cli/src/help_ui.rs b/crates/ov_cli/src/help_ui.rs
index 61295e720..e075af8eb 100644
--- a/crates/ov_cli/src/help_ui.rs
+++ b/crates/ov_cli/src/help_ui.rs
@@ -1323,6 +1323,10 @@ const COMMAND_HELP_SPECS: &[CommandHelpSpec] = &[
label: "ov reindex viking://projects/acme --mode semantic_and_vectors --wait true",
description: "Regenerate semantic artifacts, then vectors.",
},
+ HelpItem {
+ label: "ov reindex viking://projects/acme --mode prune_orphans --dry-run",
+ description: "Preview orphan vector cleanup.",
+ },
],
next_steps: &[
HelpItem {
@@ -2873,7 +2877,8 @@ mod tests {
.expect("reindex help should render"),
);
- assert!(rendered.contains("--mode <vectors_only|semantic_and_vectors>"));
+ assert!(rendered.contains("--mode <vectors_only|semantic_and_vectors|prune_orphans>"));
+ assert!(rendered.contains("--dry-run"));
assert!(rendered.contains("Regenerate semantic artifacts, then vectors."));
}
diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rs
index 4090b1331..91e227e86 100644
--- a/crates/ov_cli/src/main.rs
+++ b/crates/ov_cli/src/main.rs
@@ -1003,11 +1003,11 @@ enum Commands {
/// Viking URI
#[arg(value_name = "uri")]
uri: String,
- /// Reindex mode: vectors_only rebuilds vectors; semantic_and_vectors regenerates semantic artifacts, then vectors
+ /// Reindex mode: vectors_only rebuilds vectors; semantic_and_vectors regenerates semantic artifacts, then vectors; prune_orphans deletes orphan vector records
#[arg(
long,
default_value = "vectors_only",
- value_parser = ["vectors_only", "semantic_and_vectors"],
+ value_parser = ["vectors_only", "semantic_and_vectors", "prune_orphans"],
value_name = "mode",
help_heading = "Common options"
)]
@@ -1021,6 +1021,9 @@ enum Commands {
help_heading = "Common options"
)]
wait: bool,
+ /// Preview prune_orphans deletions without mutating vectors
+ #[arg(long, help_heading = "Common options")]
+ dry_run: bool,
},
}
@@ -3111,9 +3114,12 @@ async fn main() {
mode,
recursive,
} => handlers::handle_set_tags(uri, tags, mode, recursive, ctx).await,
- Commands::Reindex { uri, mode, wait } => {
- handlers::handle_reindex(uri, mode, wait, ctx).await
- }
+ Commands::Reindex {
+ uri,
+ mode,
+ wait,
+ dry_run,
+ } => handlers::handle_reindex(uri, mode, wait, dry_run, ctx).await,
Commands::Get { uri, local_path } => handlers::handle_get(uri, local_path, ctx).await,
Commands::Find {
query,
@@ -4497,8 +4503,9 @@ mod tests {
"reindex",
"viking://resources/demo",
"--mode",
- "semantic_and_vectors",
+ "prune_orphans",
"--wait=false",
+ "--dry-run",
]);
assert!(result.is_ok(), "reindex command should parse");
diff --git a/docs/en/api/07-system.md b/docs/en/api/07-system.md
index ff6b4454e..581174033 100644
--- a/docs/en/api/07-system.md
+++ b/docs/en/api/07-system.md
@@ -443,8 +443,9 @@ This API operates on existing `viking://...` content. It does not import new fil
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| uri | str | Yes | - | Viking URI to reindex |
-| mode | str | No | `vectors_only` | Reindex mode: `vectors_only` or `semantic_and_vectors` |
+| mode | str | No | `vectors_only` | Reindex mode: `vectors_only`, `semantic_and_vectors`, or `prune_orphans` |
| wait | bool | No | `true` | Whether to wait for completion |
+| dry_run | bool | No | `false` | Only valid with `mode="prune_orphans"`; report orphan vector records without deleting them |
The HTTP request body rejects unknown fields. `uri` may use OpenViking path variables accepted by other content APIs; it is resolved before validation.
@@ -467,11 +468,14 @@ when reindexing a broader user namespace, session subtrees are skipped.
- `vectors_only`: rebuilds vector-store records from currently recoverable source data without rewriting `.abstract.md` or `.overview.md`
- `semantic_and_vectors`: regenerates semantic artifacts first, then rebuilds vectors from the refreshed semantic outputs
+- `prune_orphans`: deletes vector-store records under the requested URI whose source files no longer exist in the filesystem. With `dry_run=true`, it only reports how many records would be deleted.
For `resource` and `skill`, `semantic_and_vectors` refreshes directory/file semantic artifacts, including `.abstract.md` and `.overview.md`. For `memory`, it rebuilds the current persisted memory subtree semantics and vectors, but it does not replay historical extraction order.
For `semantic_and_vectors`, semantic generation and vector rebuilding are sequenced by the reindex executor. The semantic refresh step does not enqueue its own background vectorization work; vectors are rebuilt by the reindex step so `wait=true` reflects the reindex operation itself.
+For `prune_orphans`, source existence is checked against the filesystem. If an entire directory is missing, vector records for files and semantic sidecars below that directory, such as `.abstract.md` and `.overview.md`, are pruned together. `dry_run` is rejected for other modes.
+
**Python SDK (Embedded / HTTP)**
```python
@@ -492,6 +496,15 @@ result = client.reindex(
print(result["status"])
```
+```python
+result = client.reindex(
+ uri="viking://resources",
+ mode="prune_orphans",
+ dry_run=True,
+)
+print(result["would_delete_records"])
+```
+
**TypeScript SDK**
```typescript
@@ -511,6 +524,18 @@ if err != nil {
fmt.Println(result["status"])
```
+```go
+result, err := client.Reindex(ctx, "viking://resources", &openviking.ReindexOptions{
+ Mode: "prune_orphans",
+ Wait: true,
+ DryRun: true,
+})
+if err != nil {
+ return err
+}
+fmt.Println(result["would_delete_records"])
+```
+
**HTTP API**
```
@@ -526,8 +551,9 @@ curl -X POST http://localhost:1933/api/v1/content/reindex \
-H "X-OpenViking-Account: default" \
-d '{
"uri": "viking://resources",
- "mode": "vectors_only",
- "wait": true
+ "mode": "prune_orphans",
+ "wait": true,
+ "dry_run": true
}'
```
@@ -541,6 +567,10 @@ openviking reindex viking://resources --mode vectors_only
openviking reindex viking://user/default/skills --mode semantic_and_vectors --wait false
```
+```bash
+openviking reindex viking://resources --mode prune_orphans --dry-run
+```
+
**Synchronous response (`wait=true`)**
```json
@@ -553,6 +583,8 @@ openviking reindex viking://user/default/skills --mode semantic_and_vectors --wa
"object_type": "resource",
"scanned_records": 120,
"rebuilt_records": 118,
+ "deleted_records": 0,
+ "would_delete_records": 0,
"unsupported_records": 2,
"failed_records": 0,
"duration_ms": 1284,
@@ -604,6 +636,8 @@ Task records are persisted under `/local/{account_id}/_system/tasks/{user_id}/{t
| mode | Effective reindex mode |
| scanned_records | Number of records or semantic sources considered |
| rebuilt_records | Number of vector records successfully rebuilt |
+| deleted_records | Number of vector records deleted by `prune_orphans`; `0` for `dry_run=true` |
+| would_delete_records | Number of vector records that would be deleted by `prune_orphans` in dry-run mode |
| unsupported_records | Number of records skipped because no usable vector source was available |
| failed_records | Number of records that failed while rebuilding |
| duration_ms | Synchronous run duration in milliseconds |
@@ -612,11 +646,13 @@ Task records are persisted under `/local/{account_id}/_system/tasks/{user_id}/{t
**Behavior notes**
-- Reindex is non-destructive. It uses rebuild/upsert behavior and does not require dropping the vector collection first.
+- `vectors_only` and `semantic_and_vectors` are non-destructive. They use rebuild/upsert behavior and do not require dropping the vector collection first.
+- `prune_orphans` is destructive unless `dry_run=true`: it removes vector records whose source files no longer exist.
- `viking://` reindex fans out to supported top-level namespaces and excludes `session`.
- Namespace reindex operations such as `viking://user` propagate to supported child content types.
- `vectors_only` is the right mode when only the embedding model or vector index needs to be refreshed.
- `semantic_and_vectors` is the right mode when semantic artifacts themselves must be regenerated before re-vectorization.
+- `prune_orphans` is the right mode when the filesystem has been changed outside normal APIs and the vector store may still contain records for deleted paths.
- Only one reindex task can run for the same URI and owner at a time. A concurrent request for the same target returns a conflict.
- For resource files, text files can use file content when no summary is available. Non-text files require a generated summary or existing vector record fallback; otherwise they are counted as unsupported.
diff --git a/docs/en/concepts/11-multi-tenant.md b/docs/en/concepts/11-multi-tenant.md
index d5103569d..3082d6247 100644
--- a/docs/en/concepts/11-multi-tenant.md
+++ b/docs/en/concepts/11-multi-tenant.md
@@ -53,6 +53,8 @@ OpenViking Server supports two multi-tenant related authentication modes:
| `api_key` | `server.auth_mode = "api_key"` | Root key or user key | Standard deployment |
| `trusted` | `server.auth_mode = "trusted"` | Upstream-injected `X-OpenViking-Account` / `X-OpenViking-User` | Behind a trusted gateway |
+In trusted mode, an upstream gateway may also assert `X-OpenViking-Role: user` or `X-OpenViking-Role: admin`. Role assertion requires a configured `root_api_key` and a matching API key on the request. `X-OpenViking-Role: root` is rejected; ROOT is reserved for the validated Admin API fallback.
+
### What `root_api_key` Does
Once `server.root_api_key` is configured, OpenViking enters formal multi-tenant mode:
diff --git a/docs/en/guides/04-authentication.md b/docs/en/guides/04-authentication.md
index 117b971bc..5be8474dc 100644
--- a/docs/en/guides/04-authentication.md
+++ b/docs/en/guides/04-authentication.md
@@ -271,9 +271,10 @@ Rules in trusted mode:
- Normal data access does not require user registration or user-key provisioning first.
- `X-OpenViking-Account` and `X-OpenViking-User` are required on tenant-scoped requests.
+- A trusted upstream may also send `X-OpenViking-Role: user` or `X-OpenViking-Role: admin` to assert the request role. This header is accepted only when `root_api_key` is configured and the request presents the matching API key. `root` is not an allowed asserted value; ROOT is reserved for validated Admin API fallback.
- Use `peer_id` in session-message bodies for stable speaker attribution. Use `X-OpenViking-Actor-Peer` to filter the current user's peer collection for filesystem and retrieval operations.
- `/api/v1/admin/*` is special: when a configured `root_api_key` is presented, trusted mode treats the request as ROOT. Explicit account/user headers are allowed only when they are complete and match the target URL.
-- For ordinary trusted data APIs, role is determined by looking up the account/user in APIKeyManager. If the user exists, their configured role is used; otherwise it defaults to `USER`.
+- For ordinary trusted data APIs, role is determined by `X-OpenViking-Role` when present and authorized; otherwise by looking up the account/user in APIKeyManager. If the user exists, their configured role is used; otherwise it defaults to `USER`.
- Trusted identity comes from the headers, not from a user key. If `root_api_key` is configured, it acts as proof that the caller is an approved trusted upstream.
- If `root_api_key` is also configured, every request must still provide a matching API key.
- Only expose this mode behind a trusted network boundary or an identity-injecting gateway.
@@ -342,7 +343,7 @@ Or explicitly:
| ADMIN | Own account | Regular operations + manage users in own account |
| USER | Own account | Regular operations (ls, read, find, sessions, etc.) |
-In `trusted` mode, ordinary tenant requests default to `USER` unless the account/user is registered with a higher role. Admin routes also allow a trusted ROOT fallback when no explicit identity is provided.
+In `trusted` mode, ordinary tenant requests default to `USER` unless the account/user is registered with a higher role or the gateway asserts `X-OpenViking-Role: admin` with the configured root API key. `X-OpenViking-Role: root` is rejected. Admin routes also allow a trusted ROOT fallback when no explicit identity is provided.
## Unauthenticated Endpoints
diff --git a/docs/zh/api/07-system.md b/docs/zh/api/07-system.md
index 97e921082..50fab2dcf 100644
--- a/docs/zh/api/07-system.md
+++ b/docs/zh/api/07-system.md
@@ -437,8 +437,9 @@ ov system wait --timeout 60
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| uri | str | 是 | - | 要重新索引的 Viking URI |
-| mode | str | 否 | `vectors_only` | 重建模式:`vectors_only` 或 `semantic_and_vectors` |
+| mode | str | 否 | `vectors_only` | 重建模式:`vectors_only`、`semantic_and_vectors` 或 `prune_orphans` |
| wait | bool | 否 | `true` | 是否等待任务完成 |
+| dry_run | bool | 否 | `false` | 仅适用于 `mode="prune_orphans"`;只报告 orphan 向量记录,不实际删除 |
HTTP 请求体不接受未知字段。`uri` 可以使用其他 content API 支持的 OpenViking 路径变量,服务端会先解析再校验。
@@ -461,11 +462,14 @@ session 子树会被跳过。
- `vectors_only`:基于当前仍可恢复的源数据重建向量库记录,不会重写 `.abstract.md` 和 `.overview.md`
- `semantic_and_vectors`:先重新生成语义产物,再基于新的语义结果重建向量
+- `prune_orphans`:删除请求 URI 范围内源文件已不存在的向量库记录。设置 `dry_run=true` 时,只报告会删除多少记录,不实际删除。
对于 `resource` 和 `skill`,`semantic_and_vectors` 会刷新目录/文件语义产物,包括 `.abstract.md` 和 `.overview.md`。对于 `memory`,它会重建当前已持久化 memory 子树的语义和向量,但不会回放历史记忆抽取顺序。
对于 `semantic_and_vectors`,语义刷新和向量重建由 reindex executor 串行编排。语义刷新阶段不会再额外向后台 embedding queue 投递自己的向量化任务;向量由 reindex 阶段统一重建,因此 `wait=true` 表示等待 reindex 操作本身完成。
+对于 `prune_orphans`,源文件是否存在以当前文件系统为准。如果整个目录已经不存在,该目录下的正文文件向量和语义 sidecar 向量(例如 `.abstract.md`、`.overview.md`)会一起清理。`dry_run` 用在其他模式时会被拒绝。
+
**Python SDK (Embedded / HTTP)**
```python
@@ -486,6 +490,15 @@ result = client.reindex(
print(result["status"])
```
+```python
+result = client.reindex(
+ uri="viking://resources",
+ mode="prune_orphans",
+ dry_run=True,
+)
+print(result["would_delete_records"])
+```
+
**TypeScript SDK**
```typescript
@@ -505,6 +518,18 @@ if err != nil {
fmt.Println(result["status"])
```
+```go
+result, err := client.Reindex(ctx, "viking://resources", &openviking.ReindexOptions{
+ Mode: "prune_orphans",
+ Wait: true,
+ DryRun: true,
+})
+if err != nil {
+ return err
+}
+fmt.Println(result["would_delete_records"])
+```
+
**HTTP API**
```
@@ -520,8 +545,9 @@ curl -X POST http://localhost:1933/api/v1/content/reindex \
-H "X-OpenViking-Account: default" \
-d '{
"uri": "viking://resources",
- "mode": "vectors_only",
- "wait": true
+ "mode": "prune_orphans",
+ "wait": true,
+ "dry_run": true
}'
```
@@ -535,6 +561,10 @@ openviking reindex viking://resources --mode vectors_only
openviking reindex viking://user/default/skills --mode semantic_and_vectors --wait false
```
+```bash
+openviking reindex viking://resources --mode prune_orphans --dry-run
+```
+
**同步响应(`wait=true`)**
```json
@@ -547,6 +577,8 @@ openviking reindex viking://user/default/skills --mode semantic_and_vectors --wa
"object_type": "resource",
"scanned_records": 120,
"rebuilt_records": 118,
+ "deleted_records": 0,
+ "would_delete_records": 0,
"unsupported_records": 2,
"failed_records": 0,
"duration_ms": 1284,
@@ -598,6 +630,8 @@ GET /api/v1/tasks?task_type=admin_reindex&resource_id=viking://resources
| mode | 实际执行的 reindex 模式 |
| scanned_records | 被检查的记录或语义源数量 |
| rebuilt_records | 成功重建的向量记录数量 |
+| deleted_records | `prune_orphans` 实际删除的向量记录数量;`dry_run=true` 时为 `0` |
+| would_delete_records | `prune_orphans` dry-run 模式下将会删除的向量记录数量 |
| unsupported_records | 因没有可用向量来源而跳过的记录数量 |
| failed_records | 重建失败的记录数量 |
| duration_ms | 同步执行耗时,单位毫秒 |
@@ -606,11 +640,13 @@ GET /api/v1/tasks?task_type=admin_reindex&resource_id=viking://resources
**行为说明**
-- Reindex 是非破坏式的,采用重建/覆盖写入,不需要先 drop 向量集合。
+- `vectors_only` 和 `semantic_and_vectors` 是非破坏式的,采用重建/覆盖写入,不需要先 drop 向量集合。
+- `prune_orphans` 除非设置 `dry_run=true`,否则会删除源文件已经不存在的向量记录。
- 对 `viking://` 发起 reindex 时,会向下分发到支持的顶层命名空间,并显式排除 `session`。
- 命名空间级 reindex,例如 `viking://user`,会继续传播到其支持的子内容类型。
- 如果只是 embedding 模型或向量索引需要刷新,应使用 `vectors_only`。
- 如果语义产物本身也需要重建,再做重向量化,应使用 `semantic_and_vectors`。
+- 如果文件系统曾绕过正常 API 发生删除,向量库可能还残留已删除路径的记录,应使用 `prune_orphans`。
- 同一个 URI 和 owner 同时只能运行一个 reindex 任务。对同一目标的并发请求会返回 conflict。
- 对 resource 文件,文本文件在没有 summary 时可以使用文件正文;非文本文件需要已生成的 summary 或已有向量记录 fallback,否则会计为 unsupported。
diff --git a/docs/zh/concepts/11-multi-tenant.md b/docs/zh/concepts/11-multi-tenant.md
index a913792bf..d10a1bb10 100644
--- a/docs/zh/concepts/11-multi-tenant.md
+++ b/docs/zh/concepts/11-multi-tenant.md
@@ -53,6 +53,8 @@ OpenViking Server 支持两种多租户相关认证模式:
| `api_key` | `server.auth_mode = "api_key"` | Root key 或 user key | 标准部署方式 |
| `trusted` | `server.auth_mode = "trusted"` | 上游显式注入 `X-OpenViking-Account` / `X-OpenViking-User` | 受信网关后面 |
+在 trusted 模式下,上游网关还可以断言 `X-OpenViking-Role: user` 或 `X-OpenViking-Role: admin`。角色断言要求服务端已配置 `root_api_key`,且请求携带匹配的 API Key。`X-OpenViking-Role: root` 会被拒绝;ROOT 只保留给已校验的 Admin API 回退路径。
+
### `root_api_key` 的作用
配置 `server.root_api_key` 后,OpenViking 才进入正式多租户模式:
diff --git a/docs/zh/guides/04-authentication.md b/docs/zh/guides/04-authentication.md
index c5d8f73b1..63f5f6a6b 100644
--- a/docs/zh/guides/04-authentication.md
+++ b/docs/zh/guides/04-authentication.md
@@ -270,8 +270,9 @@ Trusted 模式规则:
- 普通数据访问不需要先注册 user key,也不依赖 user key 分发流程
- 租户级请求必须包含 `X-OpenViking-Account` 和 `X-OpenViking-User`
+- 受信上游也可以发送 `X-OpenViking-Role: user` 或 `X-OpenViking-Role: admin` 断言请求角色。该请求头只有在配置了 `root_api_key` 且请求携带匹配 API Key 时才会生效。`root` 不是允许的断言值;ROOT 只保留给已校验的 Admin API 回退路径
- `/api/v1/admin/*` 是特例:当请求携带已配置的 `root_api_key` 时,trusted 模式会将请求视为 ROOT。显式 account/user header 只有在完整且与目标 URL 匹配时才允许
-- 普通 trusted 数据 API 的角色通过在 APIKeyManager 中查找 account/user 确定。如果用户存在,使用其配置的角色;否则默认为 `USER`
+- 普通 trusted 数据 API 的角色优先使用经过授权的 `X-OpenViking-Role`;未提供该请求头时,通过在 APIKeyManager 中查找 account/user 确定。如果用户存在,使用其配置的角色;否则默认为 `USER`
- trusted 身份完全来自请求头,而不是 user key;如果同时配置了 `root_api_key`,它表示“这个上游是被允许的 trusted 调用方”
- 如果同时配置了 `root_api_key`,每个请求仍然必须带匹配的 API Key
- 只应部署在受信网络边界之后,或由身份注入网关统一转发
@@ -340,7 +341,7 @@ client = ov.SyncHTTPClient(
| ADMIN | 所属 account | 常规操作 + 管理所属 account 的用户 |
| USER | 所属 account | 常规操作(ls、read、find、sessions 等) |
-在 `trusted` 模式下,普通租户请求默认会解析为 `USER`;如果该 account/user 已注册更高角色,则使用注册角色。对于 Admin 路由,在没有显式身份时还支持 trusted ROOT 回退。
+在 `trusted` 模式下,普通租户请求默认会解析为 `USER`;如果该 account/user 已注册更高角色,或网关在携带已配置 root API key 的请求中断言 `X-OpenViking-Role: admin`,则使用更高角色。`X-OpenViking-Role: root` 会被拒绝。对于 Admin 路由,在没有显式身份时还支持 trusted ROOT 回退。
## 无需认证的端点
diff --git a/openviking/async_client.py b/openviking/async_client.py
index 6de2a82f9..9e6f11361 100644
--- a/openviking/async_client.py
+++ b/openviking/async_client.py
@@ -19,6 +19,9 @@ from openviking_cli.client.base import BaseClient
from openviking_cli.session.user_id import UserIdentifier
from openviking_cli.utils import get_logger
+if TYPE_CHECKING:
+ from openviking.snapshot_namespace import AsyncSnapshotNamespace
+
logger = get_logger(__name__)
@@ -272,6 +275,7 @@ class AsyncOpenViking:
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
) -> Dict[str, Any]:
"""Reindex semantic/vector artifacts for a URI."""
await self._ensure_initialized()
@@ -279,6 +283,7 @@ class AsyncOpenViking:
uri=uri,
mode=mode,
wait=wait,
+ dry_run=dry_run,
)
# ============= Resource methods =============
@@ -352,6 +357,7 @@ class AsyncOpenViking:
"""
if getattr(self, "_snapshot", None) is None:
from openviking.snapshot_namespace import AsyncSnapshotNamespace
+
self._snapshot = AsyncSnapshotNamespace(self)
return self._snapshot
diff --git a/openviking/client/local.py b/openviking/client/local.py
index 2becef330..610ccb0f8 100644
--- a/openviking/client/local.py
+++ b/openviking/client/local.py
@@ -315,7 +315,11 @@ class LocalClient(BaseClient):
result["abstract"] = abstract
if level is None or level == 1:
result["overview"] = await service.fs.overview(root_uri, ctx=ctx)
- if level == 2 or include_content is True or (level is None and include_content is not False):
+ if (
+ level == 2
+ or include_content is True
+ or (level is None and include_content is not False)
+ ):
from openviking.server.routers.skills import _skill_md_uri
result["content"] = await service.fs.read(_skill_md_uri(root_uri), ctx=ctx)
@@ -506,12 +510,14 @@ class LocalClient(BaseClient):
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
) -> Dict[str, Any]:
"""Reindex semantic/vector artifacts for a URI."""
return await self._service.reindex(
uri=uri,
mode=mode,
wait=wait,
+ dry_run=dry_run,
)
async def build_index(self, resource_uris: Union[str, List[str]], **kwargs) -> Dict[str, Any]:
diff --git a/openviking/server/auth/plugins/trusted.py b/openviking/server/auth/plugins/trusted.py
index b8da67c15..6d14addde 100644
--- a/openviking/server/auth/plugins/trusted.py
+++ b/openviking/server/auth/plugins/trusted.py
@@ -17,6 +17,11 @@ from openviking_cli.exceptions import InvalidArgumentError, UnauthenticatedError
_LOCALHOST_HOSTS = {"127.0.0.1", "localhost", "::1"}
_TRUSTED_RELAXED_IDENTITY_PREFIXES = ("/api/v1/admin",)
+_TRUSTED_ROLE_HEADER = "X-OpenViking-Role"
+_TRUSTED_ASSERTABLE_ROLES = {
+ Role.USER: Role.USER,
+ Role.ADMIN: Role.ADMIN,
+}
def _is_localhost(host: str) -> bool:
@@ -57,6 +62,22 @@ def _trusted_request_requires_explicit_identity(path: str) -> bool:
return True
+def _asserted_role_from_header(request: Request, *, allow_assertion: bool) -> Optional[Role]:
+ raw_role = request.headers.get(_TRUSTED_ROLE_HEADER)
+ normalized_role = _normalize_request_value(raw_role)
+ if normalized_role is None:
+ return None
+ if not allow_assertion:
+ raise InvalidArgumentError(
+ f"{_TRUSTED_ROLE_HEADER} requires trusted mode with Root API Key enabled."
+ )
+
+ role = normalized_role.lower()
+ if role not in _TRUSTED_ASSERTABLE_ROLES:
+ raise InvalidArgumentError(f"{_TRUSTED_ROLE_HEADER} must be one of: user, admin.")
+ return _TRUSTED_ASSERTABLE_ROLES[role]
+
+
class TrustedAuthPlugin(AuthPlugin):
"""Trusted mode: trust X-OpenViking-Account/User headers.
@@ -84,6 +105,10 @@ class TrustedAuthPlugin(AuthPlugin):
raise UnauthenticatedError(
"Invalid API Key in trusted mode with Root API Key enabled."
)
+ asserted_role = _asserted_role_from_header(
+ request,
+ allow_assertion=bool(configured_root_api_key),
+ )
explicit_account_id, explicit_user_id = _explicit_identity_from_request(request)
if (
@@ -137,7 +162,9 @@ class TrustedAuthPlugin(AuthPlugin):
api_key_manager = getattr(request.app.state, "api_key_manager", None)
trusted_role = Role.USER
- if api_key_manager and effective_account_id and effective_user_id:
+ if asserted_role is not None:
+ trusted_role = asserted_role
+ elif api_key_manager and effective_account_id and effective_user_id:
looked_up_role = api_key_manager.get_user_role(effective_account_id, effective_user_id)
if looked_up_role is not None:
trusted_role = looked_up_role
diff --git a/openviking/server/routers/content.py b/openviking/server/routers/content.py
index 92ce848b0..2ff54db5d 100644
--- a/openviking/server/routers/content.py
+++ b/openviking/server/routers/content.py
@@ -27,7 +27,7 @@ from openviking.server.identity import RequestContext, Role
from openviking.server.models import Response
from openviking.server.telemetry import run_operation
from openviking.telemetry import TelemetryRequest
-from openviking_cli.exceptions import NotFoundError, PermissionDeniedError
+from openviking_cli.exceptions import InvalidArgumentError, NotFoundError, PermissionDeniedError
from openviking_cli.utils import get_logger
logger = get_logger(__name__)
@@ -64,6 +64,7 @@ class ReindexRequest(BaseModel):
uri: str
mode: str = "vectors_only"
wait: bool = True
+ dry_run: bool = False
router = APIRouter(prefix="/api/v1/content", tags=["content"])
@@ -268,6 +269,8 @@ async def reindex(
ctx: RequestContext = require_role(Role.ROOT, Role.ADMIN, Role.USER),
):
"""Reindex semantic/vector artifacts for a URI-scoped maintenance target."""
+ if body.dry_run and body.mode != "prune_orphans":
+ raise InvalidArgumentError("dry_run is only supported for prune_orphans reindex mode.")
uri = resolve_path_variables(body.uri)
uri = _validate_reindex_uri(uri)
uri = _authorize_reindex_uri(uri, ctx)
@@ -276,6 +279,7 @@ async def reindex(
uri=uri,
mode=body.mode,
wait=body.wait,
+ dry_run=body.dry_run,
ctx=ctx,
)
return Response(status="ok", result=result)
diff --git a/openviking/service/core.py b/openviking/service/core.py
index 2b4f79877..0f7bd1147 100644
--- a/openviking/service/core.py
+++ b/openviking/service/core.py
@@ -10,6 +10,7 @@ import os
from typing import TYPE_CHECKING, Any, Optional
from openviking.core.directories import DirectoryInitializer
+from openviking.core.namespace import canonicalize_uri
from openviking.privacy import UserPrivacyConfigService
from openviking.resource.watch_scheduler import WatchScheduler
from openviking.server.identity import RequestContext, Role
@@ -484,6 +485,7 @@ class OpenVikingService:
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
ctx: RequestContext | None = None,
) -> dict[str, Any]:
"""Reindex semantic/vector artifacts for a URI."""
@@ -491,12 +493,14 @@ class OpenVikingService:
await self.initialize()
effective_ctx = ctx or RequestContext(user=self.user, role=Role.ROOT)
+ canonical_uri = canonicalize_uri(uri, effective_ctx)
from openviking.service.reindex_executor import get_reindex_executor
return await get_reindex_executor().execute(
- uri=uri,
+ uri=canonical_uri,
mode=mode,
wait=wait,
+ dry_run=dry_run,
ctx=effective_ctx,
)
diff --git a/openviking/service/reindex_executor.py b/openviking/service/reindex_executor.py
index 9b6cd7348..ca6d96249 100644
--- a/openviking/service/reindex_executor.py
+++ b/openviking/service/reindex_executor.py
@@ -20,12 +20,14 @@ from openviking.core.namespace import (
classify_uri,
context_type_for_uri,
is_session_uri,
+ owner_fields_for_uri,
owner_space_for_uri,
)
from openviking.server.dependencies import get_service
from openviking.server.identity import RequestContext
from openviking.service.task_tracker import get_task_tracker
from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
+from openviking.storage.expr import And, Eq, Or, PathScope
from openviking.storage.queuefs.embedding_msg_converter import EmbeddingMsgConverter
from openviking.storage.queuefs.semantic_msg import SemanticMsg
from openviking.storage.queuefs.semantic_processor import SemanticProcessor
@@ -41,13 +43,16 @@ from openviking.telemetry import get_current_telemetry
from openviking.telemetry.request_wait_tracker import get_request_wait_tracker
from openviking.utils.embedding_utils import get_resource_content_type
from openviking.utils.skill_processor import SkillProcessor
-from openviking_cli.exceptions import NotFoundError, OpenVikingError
+from openviking_cli.exceptions import InvalidArgumentError, NotFoundError, OpenVikingError
+from openviking_cli.session.user_id import UserIdentifier
from openviking_cli.utils import VikingURI, get_logger
from openviking_cli.utils.config import get_openviking_config
logger = get_logger(__name__)
REINDEX_TASK_TYPE = "admin_reindex"
+PRUNE_ORPHAN_CANDIDATE_LIMIT = 100000
+PRUNE_OUTPUT_FIELDS = ["id", "uri", "level", "context_type", "account_id", "owner_user_id"]
# Trailing markers VikingFS appends when a directory has no generated .abstract.md/.overview.md
@@ -88,6 +93,8 @@ def get_reindex_executor() -> "ReindexExecutor":
class _ReindexCounters:
scanned_records: int = 0
rebuilt_records: int = 0
+ deleted_records: int = 0
+ would_delete_records: int = 0
unsupported_records: int = 0
failed_records: int = 0
warnings: list[str] = field(default_factory=list)
@@ -100,28 +107,56 @@ class _ReindexRunContext:
lock: LockLease = NO_LOCK
+@dataclass
+class _PruneSourceRead:
+ exists: bool
+ text: str = ""
+ error: Exception | None = None
+
+
class ReindexExecutor:
"""Non-destructive reindex orchestration for admin maintenance flows."""
SUPPORTED_MODES_BY_TYPE = {
- "global_namespace": {"vectors_only", "semantic_and_vectors"},
- "user_namespace": {"vectors_only", "semantic_and_vectors"},
- "skill_namespace": {"vectors_only", "semantic_and_vectors"},
- "resource": {"vectors_only", "semantic_and_vectors"},
- "skill": {"vectors_only", "semantic_and_vectors"},
- "memory": {"vectors_only", "semantic_and_vectors"},
+ "global_namespace": {"vectors_only", "semantic_and_vectors", "prune_orphans"},
+ "user_namespace": {"vectors_only", "semantic_and_vectors", "prune_orphans"},
+ "skill_namespace": {"vectors_only", "semantic_and_vectors", "prune_orphans"},
+ "resource": {"vectors_only", "semantic_and_vectors", "prune_orphans"},
+ "skill": {"vectors_only", "semantic_and_vectors", "prune_orphans"},
+ "memory": {"vectors_only", "semantic_and_vectors", "prune_orphans"},
}
+ @staticmethod
+ def _content_owner_ctx(uri: str, ctx: RequestContext) -> RequestContext:
+ """Return the content-owner context for user-scoped reindex writes.
+
+ Reindex authorization and task ownership use the actor ctx, but semantic
+ and vector records should retain ownership from the target URI.
+ """
+ owner = owner_fields_for_uri(uri, ctx=ctx).get("owner_user_id")
+ if not owner or owner == ctx.user.user_id:
+ return ctx
+ return RequestContext(
+ user=UserIdentifier(ctx.account_id, owner),
+ role=ctx.role,
+ actor_peer_id=ctx.actor_peer_id,
+ legacy_agent_id=ctx.legacy_agent_id,
+ from_oauth=ctx.from_oauth,
+ )
+
async def execute(
self,
*,
uri: str,
mode: str,
wait: bool,
+ dry_run: bool = False,
ctx: RequestContext,
) -> dict[str, Any]:
object_type = self._infer_target_type(uri)
self._validate_mode(object_type, mode)
+ if dry_run and mode != "prune_orphans":
+ raise InvalidArgumentError("dry_run is only supported for prune_orphans reindex mode.")
tracker = get_task_tracker()
if wait:
@@ -140,6 +175,7 @@ class ReindexExecutor:
uri=uri,
object_type=object_type,
mode=mode,
+ dry_run=dry_run,
ctx=ctx,
)
@@ -162,6 +198,7 @@ class ReindexExecutor:
uri=uri,
object_type=object_type,
mode=mode,
+ dry_run=dry_run,
ctx=ctx,
)
)
@@ -384,12 +421,13 @@ class ReindexExecutor:
uri: str,
object_type: str,
mode: str,
+ dry_run: bool = False,
ctx: RequestContext,
) -> dict[str, Any]:
service = get_service()
if service.viking_fs is None or service.vikingdb_manager is None:
raise RuntimeError("OpenVikingService not initialized")
- if not service.vikingdb_manager.has_queue_manager:
+ if mode != "prune_orphans" and not service.vikingdb_manager.has_queue_manager:
raise OpenVikingError(
"Reindex requires embedding queue",
code="FAILED_PRECONDITION",
@@ -411,7 +449,15 @@ class ReindexExecutor:
counters=counters,
lock=BorrowedLockLease.from_handle(get_lock_manager(), lock_handle),
)
- if object_type == "global_namespace":
+ if mode == "prune_orphans":
+ await self._prune_orphan_vectors(
+ uri=uri,
+ object_type=object_type,
+ dry_run=dry_run,
+ counters=counters,
+ ctx=ctx,
+ )
+ elif object_type == "global_namespace":
await self._reindex_global_namespace(
uri=uri,
mode=mode,
@@ -454,7 +500,7 @@ class ReindexExecutor:
details={"uri": uri},
)
- if telemetry_id:
+ if telemetry_id and mode != "prune_orphans":
await wait_tracker.wait_for_request(telemetry_id)
self._apply_embedding_wait_status(
counters,
@@ -471,6 +517,8 @@ class ReindexExecutor:
"mode": mode,
"scanned_records": counters.scanned_records,
"rebuilt_records": counters.rebuilt_records,
+ "deleted_records": counters.deleted_records,
+ "would_delete_records": counters.would_delete_records,
"unsupported_records": counters.unsupported_records,
"failed_records": counters.failed_records,
"duration_ms": int((time.perf_counter() - started_at) * 1000),
@@ -484,6 +532,7 @@ class ReindexExecutor:
uri: str,
object_type: str,
mode: str,
+ dry_run: bool = False,
ctx: RequestContext,
) -> None:
tracker = get_task_tracker()
@@ -493,6 +542,7 @@ class ReindexExecutor:
uri=uri,
object_type=object_type,
mode=mode,
+ dry_run=dry_run,
ctx=ctx,
)
await tracker.complete(
@@ -509,6 +559,302 @@ class ReindexExecutor:
user_id=ctx.user.user_id,
)
+ async def _prune_orphan_vectors(
+ self,
+ *,
+ uri: str,
+ object_type: str,
+ dry_run: bool,
+ counters: _ReindexCounters,
+ ctx: RequestContext,
+ ) -> None:
+ service = get_service()
+ vikingdb = service.vikingdb_manager
+ delete_groups: dict[tuple[str, str], tuple[RequestContext, list[str]]] = {}
+
+ for context_type in self._prune_context_types(uri=uri, object_type=object_type):
+ offset = 0
+ while True:
+ filter_kwargs: dict[str, Any] = {}
+ if offset:
+ filter_kwargs["offset"] = offset
+ records = await vikingdb.filter(
+ filter=And(
+ [
+ Eq("account_id", ctx.account_id),
+ Eq("context_type", context_type),
+ Or(
+ [
+ PathScope("uri", uri, depth=0),
+ PathScope("uri", uri, depth=-1),
+ ]
+ ),
+ ]
+ ),
+ limit=PRUNE_ORPHAN_CANDIDATE_LIMIT,
+ output_fields=PRUNE_OUTPUT_FIELDS,
+ ctx=ctx,
+ **filter_kwargs,
+ )
+ if not records:
+ break
+
+ for record in records:
+ counters.scanned_records += 1
+ if not self._is_supported_prune_record(record, counters):
+ continue
+ if not await self._is_orphan_vector_record(record, counters=counters, ctx=ctx):
+ continue
+
+ if dry_run:
+ counters.would_delete_records += 1
+ continue
+
+ delete_ctx = self._delete_ctx_for_prune_record(record, ctx)
+ key = (delete_ctx.account_id, delete_ctx.user.user_id)
+ if key not in delete_groups:
+ delete_groups[key] = (delete_ctx, [])
+ delete_groups[key][1].append(str(record["id"]))
+
+ if len(records) < PRUNE_ORPHAN_CANDIDATE_LIMIT:
+ break
+ offset += len(records)
+
+ if dry_run:
+ return
+
+ for delete_ctx, ids in delete_groups.values():
+ try:
+ deleted = await vikingdb.delete(ids, ctx=delete_ctx)
+ deleted_count = int(deleted if deleted is not None else len(ids))
+ counters.deleted_records += deleted_count
+ if deleted_count < len(ids):
+ failed_count = len(ids) - max(deleted_count, 0)
+ counters.failed_records += failed_count
+ counters.warnings.append(
+ f"Only deleted {deleted_count} of {len(ids)} orphan vectors for owner "
+ f"{delete_ctx.user.user_id}"
+ )
+ except Exception as exc:
+ counters.failed_records += len(ids)
+ counters.warnings.append(
+ f"Failed to delete {len(ids)} orphan vectors for owner "
+ f"{delete_ctx.user.user_id}: {exc}"
+ )
+
+ def _prune_context_types(self, *, uri: str, object_type: str) -> list[str]:
+ if object_type == "resource":
+ return [ContextType.RESOURCE.value]
+ if object_type == "memory":
+ return [ContextType.MEMORY.value]
+ if object_type in {"skill", "skill_namespace"}:
+ return [ContextType.SKILL.value]
+ if object_type in {"global_namespace", "user_namespace"}:
+ return [
+ ContextType.RESOURCE.value,
+ ContextType.MEMORY.value,
+ ContextType.SKILL.value,
+ ]
+ return [str(context_type_for_uri(uri))]
+
+ def _is_supported_prune_record(
+ self,
+ record: dict[str, Any],
+ counters: _ReindexCounters,
+ ) -> bool:
+ record_id = record.get("id")
+ uri = record.get("uri")
+ context_type = str(record.get("context_type") or "")
+ level = record.get("level")
+ if not record_id or not uri:
+ counters.unsupported_records += 1
+ counters.warnings.append(f"Skipping unknown prune record without id/uri: {record!r}")
+ return False
+ if context_type not in {
+ ContextType.RESOURCE.value,
+ ContextType.MEMORY.value,
+ ContextType.SKILL.value,
+ }:
+ counters.unsupported_records += 1
+ counters.warnings.append(f"Skipping unknown context_type for prune record {uri}")
+ return False
+ try:
+ normalized_level = int(level)
+ except (TypeError, ValueError):
+ counters.unsupported_records += 1
+ counters.warnings.append(f"Skipping unknown level for prune record {uri}: {level}")
+ return False
+ if normalized_level not in {
+ int(ContextLevel.ABSTRACT),
+ int(ContextLevel.OVERVIEW),
+ int(ContextLevel.DETAIL),
+ }:
+ counters.unsupported_records += 1
+ counters.warnings.append(f"Skipping unknown level for prune record {uri}: {level}")
+ return False
+ record["_prune_level"] = normalized_level
+ return True
+
+ async def _is_orphan_vector_record(
+ self,
+ record: dict[str, Any],
+ *,
+ counters: _ReindexCounters,
+ ctx: RequestContext,
+ ) -> bool:
+ uri = str(record["uri"])
+ level = int(record.get("_prune_level", record["level"]))
+ context_type = str(record["context_type"])
+ owner_ctx = self._delete_ctx_for_prune_record(record, ctx)
+
+ if level == int(ContextLevel.ABSTRACT):
+ abstract = await self._read_prune_source(
+ f"{uri}/.abstract.md",
+ ctx=owner_ctx,
+ )
+ if abstract.error:
+ self._record_prune_source_error(
+ counters=counters,
+ uri=uri,
+ source_uri=f"{uri}/.abstract.md",
+ error=abstract.error,
+ )
+ return False
+ return (
+ not abstract.exists
+ or not abstract.text
+ or _is_not_ready_sentinel(abstract.text, _ABSTRACT_NOT_READY_SUFFIX)
+ )
+
+ if level == int(ContextLevel.OVERVIEW):
+ overview = await self._read_prune_source(
+ f"{uri}/.overview.md",
+ ctx=owner_ctx,
+ )
+ if overview.error:
+ self._record_prune_source_error(
+ counters=counters,
+ uri=uri,
+ source_uri=f"{uri}/.overview.md",
+ error=overview.error,
+ )
+ return False
+ if (
+ overview.exists
+ and overview.text
+ and not _is_not_ready_sentinel(overview.text, _OVERVIEW_NOT_READY_SUFFIX)
+ ):
+ return False
+ if context_type in {ContextType.RESOURCE.value, ContextType.SKILL.value}:
+ abstract = await self._read_prune_source(
+ f"{uri}/.abstract.md",
+ ctx=owner_ctx,
+ )
+ if abstract.error:
+ self._record_prune_source_error(
+ counters=counters,
+ uri=uri,
+ source_uri=f"{uri}/.abstract.md",
+ error=abstract.error,
+ )
+ return False
+ return (
+ not abstract.exists
+ or not abstract.text
+ or _is_not_ready_sentinel(abstract.text, _ABSTRACT_NOT_READY_SUFFIX)
+ )
+ return True
+
+ if "#" in uri:
+ if context_type == ContextType.MEMORY.value and "#chunk_" in uri:
+ base_uri = uri.split("#chunk_", 1)[0]
+ base = await self._read_prune_source(base_uri, ctx=owner_ctx)
+ if base.error:
+ self._record_prune_source_error(
+ counters=counters,
+ uri=uri,
+ source_uri=base_uri,
+ error=base.error,
+ )
+ return False
+ if not base.exists:
+ return True
+ expected = {
+ chunk_uri for chunk_uri, _chunk in self._chunk_memory_body(base_uri, base.text)
+ }
+ return uri not in expected
+ return False
+
+ if self._is_hidden_meta_file(uri):
+ return False
+ exists = await self._prune_source_exists(uri, ctx=owner_ctx)
+ if exists.error:
+ self._record_prune_source_error(
+ counters=counters,
+ uri=uri,
+ source_uri=uri,
+ error=exists.error,
+ )
+ return False
+ return not exists.exists
+
+ async def _read_prune_source(self, uri: str, *, ctx: RequestContext) -> _PruneSourceRead:
+ viking_fs = get_viking_fs()
+ try:
+ exists = await viking_fs.exists(uri, ctx=ctx)
+ except Exception as exc:
+ return _PruneSourceRead(exists=False, error=exc)
+ if not exists:
+ return _PruneSourceRead(exists=False)
+ try:
+ content = await viking_fs.read_file(uri, ctx=ctx)
+ except Exception as exc:
+ return _PruneSourceRead(exists=True, error=exc)
+ if isinstance(content, bytes):
+ text = content.decode("utf-8", errors="replace")
+ else:
+ text = str(content or "")
+ return _PruneSourceRead(exists=True, text=text)
+
+ async def _prune_source_exists(self, uri: str, *, ctx: RequestContext) -> _PruneSourceRead:
+ try:
+ exists = await get_viking_fs().exists(uri, ctx=ctx)
+ except Exception as exc:
+ return _PruneSourceRead(exists=False, error=exc)
+ return _PruneSourceRead(exists=exists)
+
+ def _record_prune_source_error(
+ self,
+ *,
+ counters: _ReindexCounters | None,
+ uri: str,
+ source_uri: str,
+ error: Exception,
+ ) -> None:
+ if counters is None:
+ return
+ counters.failed_records += 1
+ counters.warnings.append(f"Skipped prune for {uri}: failed to read {source_uri}: {error}")
+
+ def _delete_ctx_for_prune_record(
+ self,
+ record: dict[str, Any],
+ ctx: RequestContext,
+ ) -> RequestContext:
+ uri = str(record.get("uri") or "")
+ owner = record.get("owner_user_id")
+ if not owner:
+ owner = owner_fields_for_uri(uri, ctx=ctx).get("owner_user_id")
+ if not owner or owner == ctx.user.user_id:
+ return ctx
+ return RequestContext(
+ user=UserIdentifier(ctx.account_id, str(owner)),
+ role=ctx.role,
+ actor_peer_id=ctx.actor_peer_id,
+ legacy_agent_id=ctx.legacy_agent_id,
+ from_oauth=ctx.from_oauth,
+ )
+
async def _reindex_resource(
self,
*,
@@ -575,13 +921,14 @@ class ReindexExecutor:
processor = SemanticProcessor(
max_concurrent_llm=get_openviking_config().vlm.max_concurrent,
)
+ owner_ctx = self._content_owner_ctx(uri, ctx)
msg = SemanticMsg(
uri=uri,
context_type=context_type,
recursive=True,
- account_id=ctx.account_id,
- user_id=ctx.user.user_id,
- peer_id=ctx.user.user_id,
+ account_id=owner_ctx.account_id,
+ user_id=owner_ctx.user.user_id,
+ peer_id=owner_ctx.user.user_id,
role=str(ctx.role),
skip_vectorization=True,
)
@@ -768,9 +1115,7 @@ class ReindexExecutor:
ctx=ctx,
)
- async def delete_uri_level(
- self, *, uri: str, level: ContextLevel, ctx: RequestContext
- ) -> int:
+ async def delete_uri_level(self, *, uri: str, level: ContextLevel, ctx: RequestContext) -> int:
"""Delete ONLY the vector record at ``(uri, level)``. Returns count.
Used by git restore for both directory markers (dir + L0/L1) and
@@ -966,10 +1311,18 @@ class ReindexExecutor:
abstract = await self._read_directory_abstract(uri, ctx=ctx)
overview = await self._read_directory_overview(uri, ctx=ctx)
if not abstract:
- record = await self._fetch_existing_record(uri=uri, level=0, ctx=ctx)
+ record = await self._fetch_existing_record(
+ uri=uri,
+ level=0,
+ ctx=self._content_owner_ctx(uri, ctx),
+ )
abstract = self._record_abstract(record)
if not overview:
- record = await self._fetch_existing_record(uri=uri, level=1, ctx=ctx)
+ record = await self._fetch_existing_record(
+ uri=uri,
+ level=1,
+ ctx=self._content_owner_ctx(uri, ctx),
+ )
overview = self._record_abstract(record) or abstract
if not abstract and not overview:
@@ -1114,9 +1467,20 @@ class ReindexExecutor:
for file_uri in file_uris:
counters.scanned_records += 1
- body = await self._safe_read_text(file_uri, ctx=ctx)
+ body_source = await self._read_memory_body(file_uri, ctx=ctx)
+ if body_source.error:
+ counters.failed_records += 1
+ counters.warnings.append(
+ f"Skipped {file_uri}: failed to read memory body: {body_source.error}"
+ )
+ continue
+ body = body_source.text if body_source.exists else ""
memory_content = MemoryFileUtils.read(body).content if body else ""
- existing = await self._fetch_existing_record(uri=file_uri, level=2, ctx=ctx)
+ existing = await self._fetch_existing_record(
+ uri=file_uri,
+ level=2,
+ ctx=self._content_owner_ctx(file_uri, ctx),
+ )
abstract = self._best_non_empty(
self._record_abstract(existing),
await self._best_file_summary(file_uri, ctx=ctx),
@@ -1269,7 +1633,11 @@ class ReindexExecutor:
parsed = self._parse_overview_md(overviews)
if file_name in parsed:
return parsed[file_name]
- existing = await self._fetch_existing_record(uri=uri, level=2, ctx=ctx)
+ existing = await self._fetch_existing_record(
+ uri=uri,
+ level=2,
+ ctx=self._content_owner_ctx(uri, ctx),
+ )
return self._record_abstract(existing)
async def _best_resource_file_vector_text(
@@ -1279,7 +1647,11 @@ class ReindexExecutor:
ctx: RequestContext,
) -> str:
text_source = getattr(get_openviking_config().embedding, "text_source", "summary_first")
- existing = await self._fetch_existing_record(uri=uri, level=2, ctx=ctx)
+ existing = await self._fetch_existing_record(
+ uri=uri,
+ level=2,
+ ctx=self._content_owner_ctx(uri, ctx),
+ )
fallback = self._record_abstract(existing)
content_type = get_resource_content_type(uri.rsplit("/", 1)[-1])
@@ -1314,7 +1686,8 @@ class ReindexExecutor:
service = get_service()
assert service.vikingdb_manager is not None
merged_meta = dict(meta or {})
- existing = await self._fetch_existing_record(uri=uri, level=int(level), ctx=ctx)
+ owner_ctx = self._content_owner_ctx(uri, ctx)
+ existing = await self._fetch_existing_record(uri=uri, level=int(level), ctx=owner_ctx)
if (
existing
and existing.get("search_tags") is not None
@@ -1329,9 +1702,9 @@ class ReindexExecutor:
abstract=abstract or "",
context_type=context_type,
level=level,
- user=ctx.user,
- account_id=ctx.account_id,
- owner_space=owner_space_for_uri(uri, ctx),
+ user=owner_ctx.user,
+ account_id=owner_ctx.account_id,
+ owner_space=owner_space_for_uri(uri, owner_ctx),
meta=merged_meta,
)
context.set_vectorize(Vectorize(text=vector_text, full_text=full_text or vector_text))
@@ -1412,6 +1785,34 @@ class ReindexExecutor:
except Exception:
return ""
+ async def _read_memory_body(self, uri: str, *, ctx: RequestContext) -> _PruneSourceRead:
+ return await self._read_prune_source(uri, ctx=ctx)
+
+ def _chunk_memory_body(self, uri: str, body: str) -> Iterable[tuple[str, str]]:
+ semantic = get_openviking_config().semantic
+ chunk_chars = semantic.memory_chunk_chars
+ overlap = semantic.memory_chunk_overlap
+ if len(body) <= chunk_chars:
+ return []
+
+ chunks: list[str] = []
+ start = 0
+ while start < len(body):
+ previous_start = start
+ end = start + chunk_chars
+ if end < len(body):
+ boundary = body.rfind("\n\n", start, end)
+ if boundary > start + chunk_chars // 2:
+ end = boundary + 2
+ chunks.append(body[start:end].strip())
+ start = end - overlap
+ if start <= previous_start:
+ start = previous_start + 1
+ if start >= len(body):
+ break
+
+ return [(f"{uri}#chunk_{idx:04d}", chunk) for idx, chunk in enumerate(chunks) if chunk]
+
def _best_non_empty(self, *values: str) -> str:
for value in values:
if value:
diff --git a/openviking/sync_client.py b/openviking/sync_client.py
index 0c23fb3c7..d686865e4 100644
--- a/openviking/sync_client.py
+++ b/openviking/sync_client.py
@@ -184,6 +184,7 @@ class SyncOpenViking:
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
) -> Dict[str, Any]:
"""Reindex semantic/vector artifacts for a URI."""
return run_async(
@@ -191,6 +192,7 @@ class SyncOpenViking:
uri=uri,
mode=mode,
wait=wait,
+ dry_run=dry_run,
)
)
@@ -656,6 +658,7 @@ class SyncOpenViking:
"""Snapshot version control namespace (synchronous)."""
if getattr(self, "_snapshot", None) is None:
from openviking.snapshot_namespace import SyncSnapshotNamespace
+
self._snapshot = SyncSnapshotNamespace(self)
return self._snapshot
diff --git a/openviking_cli/_sdk_import.py b/openviking_cli/_sdk_import.py
index 1c12f78db..e38c36ac9 100644
--- a/openviking_cli/_sdk_import.py
+++ b/openviking_cli/_sdk_import.py
@@ -8,12 +8,12 @@ from pathlib import Path
def import_openviking_sdk():
+ sdk_root = Path(__file__).resolve().parents[1] / "sdk" / "python"
+ if (sdk_root / "openviking_sdk").is_dir() and str(sdk_root) not in sys.path:
+ sys.path.insert(0, str(sdk_root))
try:
return import_module("openviking_sdk")
except ImportError as exc:
- sdk_root = Path(__file__).resolve().parents[1] / "sdk" / "python"
- if str(sdk_root) not in sys.path:
- sys.path.insert(0, str(sdk_root))
try:
return import_module("openviking_sdk")
except ImportError:
diff --git a/openviking_cli/client/_http_compat.py b/openviking_cli/client/_http_compat.py
index bb8703092..a523f0269 100644
--- a/openviking_cli/client/_http_compat.py
+++ b/openviking_cli/client/_http_compat.py
@@ -122,8 +122,7 @@ class AsyncHTTPClient(import_openviking_sdk().AsyncHTTPClient):
name
for name, param in sig.parameters.items()
if name != "self"
- and param.kind
- in (param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD)
+ and param.kind in (param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD)
]
timeout_index = params.index("timeout")
except Exception:
diff --git a/openviking_cli/client/base.py b/openviking_cli/client/base.py
index 9ffad6c64..08d375b17 100644
--- a/openviking_cli/client/base.py
+++ b/openviking_cli/client/base.py
@@ -142,6 +142,7 @@ class BaseClient(ABC):
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
) -> Dict[str, Any]:
"""Reindex semantic/vector artifacts for a URI."""
...
diff --git a/openviking_cli/utils/config/parser_config.py b/openviking_cli/utils/config/parser_config.py
index 6d3d1a2c3..bc005f9c1 100644
--- a/openviking_cli/utils/config/parser_config.py
+++ b/openviking_cli/utils/config/parser_config.py
@@ -634,6 +634,14 @@ class SemanticConfig:
memory_chunk_overlap: int = 200
"""Character overlap between adjacent memory chunks for context continuity."""
+ def __post_init__(self):
+ if self.memory_chunk_chars <= 0:
+ raise ValueError("memory_chunk_chars must be positive")
+ if self.memory_chunk_overlap < 0:
+ raise ValueError("memory_chunk_overlap must be non-negative")
+ if self.memory_chunk_overlap >= self.memory_chunk_chars:
+ raise ValueError("memory_chunk_overlap must be smaller than memory_chunk_chars")
+
# Configuration registry for dynamic loading
PARSER_CONFIG_REGISTRY = {
diff --git a/sdk/go/client_test.go b/sdk/go/client_test.go
index 89bd27fcf..7ba58bd5e 100644
--- a/sdk/go/client_test.go
+++ b/sdk/go/client_test.go
@@ -223,6 +223,40 @@ func TestFindSendsImageQuery(t *testing.T) {
}
}
+func TestReindexSendsDryRun(t *testing.T) {
+ client, closeServer := testClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/api/v1/content/reindex" {
+ t.Fatalf("path = %s", r.URL.Path)
+ }
+ if r.Method != http.MethodPost {
+ t.Fatalf("method = %s", r.Method)
+ }
+ body := readJSONBody(t, r)
+ if got := body["uri"]; got != "viking://resources/demo" {
+ t.Fatalf("uri = %#v", got)
+ }
+ if got := body["mode"]; got != "prune_orphans" {
+ t.Fatalf("mode = %#v", got)
+ }
+ if got := body["wait"]; got != false {
+ t.Fatalf("wait = %#v", got)
+ }
+ if got := body["dry_run"]; got != true {
+ t.Fatalf("dry_run = %#v", got)
+ }
+ writeOK(t, w, map[string]any{"status": "completed"})
+ }))
+ defer closeServer()
+
+ if _, err := client.Reindex(context.Background(), "resources/demo", &ReindexOptions{
+ Mode: "prune_orphans",
+ Wait: false,
+ DryRun: true,
+ }); err != nil {
+ t.Fatal(err)
+ }
+}
+
func TestAdminCreatePathsAcceptInitialUserConfig(t *testing.T) {
var seen []map[string]any
client, closeServer := testClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
diff --git a/sdk/go/filesystem.go b/sdk/go/filesystem.go
index 8602d46e7..aad5ddc14 100644
--- a/sdk/go/filesystem.go
+++ b/sdk/go/filesystem.go
@@ -204,9 +204,10 @@ func (c *Client) Reindex(ctx context.Context, uri string, opts *ReindexOptions)
mode = "vectors_only"
}
payload := map[string]any{
- "uri": NormalizeURI(uri),
- "mode": mode,
- "wait": opts.Wait,
+ "uri": NormalizeURI(uri),
+ "mode": mode,
+ "wait": opts.Wait,
+ "dry_run": opts.DryRun,
}
var result map[string]any
err := c.doJSON(ctx, http.MethodPost, "/api/v1/content/reindex", nil, payload, &result)
diff --git a/sdk/go/types.go b/sdk/go/types.go
index 818a236d4..2eafe39ca 100644
--- a/sdk/go/types.go
+++ b/sdk/go/types.go
@@ -184,8 +184,9 @@ type SetTagsOptions struct {
// ReindexOptions controls Reindex.
type ReindexOptions struct {
- Mode string
- Wait bool
+ Mode string
+ Wait bool
+ DryRun bool
}
// FindOptions controls Find.
diff --git a/sdk/python/openviking_sdk/client.py b/sdk/python/openviking_sdk/client.py
index 38fdc8d5e..1b848cdf2 100644
--- a/sdk/python/openviking_sdk/client.py
+++ b/sdk/python/openviking_sdk/client.py
@@ -1380,11 +1380,12 @@ class AsyncHTTPClient:
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
) -> Dict[str, Any]:
response = await self._request(
"POST",
"/api/v1/content/reindex",
- json={"uri": uri, "mode": mode, "wait": wait},
+ json={"uri": uri, "mode": mode, "wait": wait, "dry_run": dry_run},
)
return self._handle_response(response)
@@ -2213,8 +2214,9 @@ class SyncHTTPClient:
uri: str,
mode: str = "vectors_only",
wait: bool = True,
+ dry_run: bool = False,
) -> Dict[str, Any]:
- return run_async(self._async_client.reindex(uri=uri, mode=mode, wait=wait))
+ return run_async(self._async_client.reindex(uri=uri, mode=mode, wait=wait, dry_run=dry_run))
def admin_create_account(
self,
diff --git a/sdk/python/tests/test_async_client_behaviors.py b/sdk/python/tests/test_async_client_behaviors.py
index a65bd4d13..36bf86299 100644
--- a/sdk/python/tests/test_async_client_behaviors.py
+++ b/sdk/python/tests/test_async_client_behaviors.py
@@ -1,6 +1,6 @@
from pathlib import Path
from types import SimpleNamespace
-from unittest.mock import AsyncMock, patch
+from unittest.mock import AsyncMock, Mock, patch
import pytest
from openviking_sdk import AsyncHTTPClient, SyncHTTPClient
@@ -70,8 +70,9 @@ async def test_async_http_client_reindex_posts_content_reindex():
result = await client.reindex(
"viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
assert result == {"status": "completed"}
@@ -79,8 +80,9 @@ async def test_async_http_client_reindex_posts_content_reindex():
"/api/v1/content/reindex",
json={
"uri": "viking://resources/demo",
- "mode": "vectors_only",
+ "mode": "prune_orphans",
"wait": False,
+ "dry_run": True,
},
)
@@ -90,6 +92,7 @@ def test_sync_http_client_reindex_forwards_to_async_client():
with patch.object(
client._async_client,
"reindex",
+ new_callable=Mock,
return_value={"status": "accepted"},
) as mock_reindex:
with patch(
@@ -98,13 +101,19 @@ def test_sync_http_client_reindex_forwards_to_async_client():
) as mock_run:
result = client.reindex(
"viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
assert result == {"status": "accepted"}
assert mock_run.called
- assert mock_reindex.called
+ mock_reindex.assert_called_once_with(
+ uri="viking://resources/demo",
+ mode="prune_orphans",
+ wait=False,
+ dry_run=True,
+ )
def test_sync_http_client_batch_add_messages_forwards_to_async_client():
diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.ts
index 370c0bc5b..447744162 100644
--- a/sdk/typescript/src/client.ts
+++ b/sdk/typescript/src/client.ts
@@ -531,13 +531,14 @@ export class OpenVikingClient {
/** Rebuild indexes for a URI. */
reindex(
uri: string,
- options: { mode?: string; wait?: boolean } = {},
+ options: { mode?: string; wait?: boolean; dryRun?: boolean } = {},
): Promise<JsonObject> {
return this.request("POST", "/api/v1/content/reindex", {
body: {
uri: normalizeURI(uri),
mode: options.mode ?? "vectors_only",
wait: options.wait ?? true,
+ dry_run: options.dryRun ?? false,
},
});
}
diff --git a/sdk/typescript/tests/client.test.ts b/sdk/typescript/tests/client.test.ts
index a1478ac5d..395aa268b 100644
--- a/sdk/typescript/tests/client.test.ts
+++ b/sdk/typescript/tests/client.test.ts
@@ -63,6 +63,29 @@ describe("OpenVikingClient", () => {
});
});
+ it("sends dry_run for prune_orphans reindex requests", async () => {
+ const fetcher = vi.fn<typeof fetch>().mockResolvedValue(ok({ status: "completed" }));
+ const client = new OpenVikingClient({
+ baseUrl: "https://example.com",
+ fetch: fetcher,
+ });
+
+ await client.reindex("resources", {
+ mode: "prune_orphans",
+ wait: true,
+ dryRun: true,
+ });
+
+ const [url, init] = fetcher.mock.calls[0]!;
+ expect(String(url)).toBe("https://example.com/api/v1/content/reindex");
+ expect(JSON.parse(String(init?.body))).toEqual({
+ uri: "viking://resources",
+ mode: "prune_orphans",
+ wait: true,
+ dry_run: true,
+ });
+ });
+
it("maps response envelopes to typed errors", async () => {
const fetcher = vi.fn<typeof fetch>().mockResolvedValue(
new Response(
diff --git a/tests/client/test_rebuild_clients.py b/tests/client/test_rebuild_clients.py
index d72b0762e..55f41351d 100644
--- a/tests/client/test_rebuild_clients.py
+++ b/tests/client/test_rebuild_clients.py
@@ -3,7 +3,7 @@ import threading
import zipfile
from pathlib import Path
from types import SimpleNamespace
-from unittest.mock import AsyncMock, patch
+from unittest.mock import AsyncMock, Mock, patch
import pytest
@@ -20,7 +20,7 @@ from openviking_cli.utils.config import OPENVIKING_CLI_CONFIG_ENV
@pytest.fixture(autouse=True)
def clear_ovcli_config(monkeypatch):
monkeypatch.delenv(OPENVIKING_CLI_CONFIG_ENV, raising=False)
- monkeypatch.setattr(http_module, "load_ovcli_config", lambda: None)
+ monkeypatch.setattr(http_module, "load_ovcli_config", lambda: None, raising=False)
def test_async_http_client_zip_directory_skips_symlinked_entries(tmp_path):
@@ -86,16 +86,18 @@ async def test_async_openviking_reindex_forwards_to_local_client(tmp_path):
result = await client.reindex(
"viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
assert result == {"status": "completed"}
mock_init.assert_awaited_once()
mock_reindex.assert_awaited_once_with(
uri="viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
@@ -104,6 +106,7 @@ def test_sync_openviking_reindex_forwards_to_async_client():
with patch.object(
client._async_client,
"reindex",
+ new_callable=Mock,
return_value={"status": "completed"},
) as mock_reindex:
with patch(
@@ -111,13 +114,19 @@ def test_sync_openviking_reindex_forwards_to_async_client():
) as mock_run:
result = client.reindex(
"viking://resources/demo",
- mode="semantic_and_vectors",
+ mode="prune_orphans",
wait=True,
+ dry_run=True,
)
assert result == {"status": "completed"}
assert mock_run.called
- assert mock_reindex.called
+ mock_reindex.assert_called_once_with(
+ uri="viking://resources/demo",
+ mode="prune_orphans",
+ wait=True,
+ dry_run=True,
+ )
async def test_local_client_reindex_forwards_to_service():
@@ -127,12 +136,18 @@ async def test_local_client_reindex_forwards_to_service():
result = await LocalClient.reindex(
client,
uri="viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
assert result == {"status": "completed"}
- client._service.reindex.assert_awaited_once()
+ client._service.reindex.assert_awaited_once_with(
+ uri="viking://resources/demo",
+ mode="prune_orphans",
+ wait=False,
+ dry_run=True,
+ )
async def test_local_client_batch_add_messages_forwards_to_session():
@@ -287,8 +302,9 @@ async def test_async_http_client_reindex_posts_content_reindex():
) as mock_handle:
result = await client.reindex(
"viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
assert result == {"status": "completed"}
@@ -296,8 +312,9 @@ async def test_async_http_client_reindex_posts_content_reindex():
"/api/v1/content/reindex",
json={
"uri": "viking://resources/demo",
- "mode": "vectors_only",
+ "mode": "prune_orphans",
"wait": False,
+ "dry_run": True,
},
)
assert mock_handle.called
@@ -308,21 +325,27 @@ def test_sync_http_client_reindex_forwards_to_async_client():
with patch.object(
client._async_client,
"reindex",
+ new_callable=Mock,
return_value={"status": "accepted"},
) as mock_reindex:
with patch(
- "openviking_cli.client.sync_http.run_async",
- return_value={"status": "accepted"},
+ "openviking_sdk.client.run_async", return_value={"status": "accepted"}
) as mock_run:
result = client.reindex(
"viking://resources/demo",
- mode="vectors_only",
+ mode="prune_orphans",
wait=False,
+ dry_run=True,
)
assert result == {"status": "accepted"}
assert mock_run.called
- assert mock_reindex.called
+ mock_reindex.assert_called_once_with(
+ uri="viking://resources/demo",
+ mode="prune_orphans",
+ wait=False,
+ dry_run=True,
+ )
def test_sync_http_client_batch_add_messages_forwards_to_async_client():
diff --git a/tests/client/test_sdk_import.py b/tests/client/test_sdk_import.py
new file mode 100644
index 000000000..7aa79213c
--- /dev/null
+++ b/tests/client/test_sdk_import.py
@@ -0,0 +1,16 @@
+import sys
+from pathlib import Path
+
+
+def test_import_openviking_sdk_prefers_workspace_sdk(monkeypatch):
+ import openviking_cli._sdk_import as sdk_import
+
+ sdk_root = Path(__file__).resolve().parents[2] / "sdk" / "python"
+ for name in list(sys.modules):
+ if name == "openviking_sdk" or name.startswith("openviking_sdk."):
+ monkeypatch.delitem(sys.modules, name, raising=False)
+ monkeypatch.setattr(sys, "path", [p for p in sys.path if p != str(sdk_root)])
+
+ sdk = sdk_import.import_openviking_sdk()
+
+ assert Path(sdk.__file__).resolve().is_relative_to(sdk_root)
diff --git a/tests/misc/test_semantic_config.py b/tests/misc/test_semantic_config.py
index 16d6e0be8..72a59b9a2 100644
--- a/tests/misc/test_semantic_config.py
+++ b/tests/misc/test_semantic_config.py
@@ -3,6 +3,8 @@
"""Tests for SemanticConfig and overview budget estimation."""
+import pytest
+
from openviking_cli.utils.config.parser_config import SemanticConfig
@@ -89,3 +91,17 @@ def test_memory_chunk_config_custom():
config = SemanticConfig(memory_chunk_chars=500, memory_chunk_overlap=50)
assert config.memory_chunk_chars == 500
assert config.memory_chunk_overlap == 50
+
+
+ ("chunk_chars", "overlap"),
+ [
+ (0, 0),
+ (100, 100),
+ (100, 101),
+ ],
+)
+def test_memory_chunk_config_rejects_non_progressing_overlap(chunk_chars, overlap):
+ """Memory chunk settings must guarantee chunking advances."""
+ with pytest.raises(ValueError, match="memory_chunk"):
+ SemanticConfig(memory_chunk_chars=chunk_chars, memory_chunk_overlap=overlap)
diff --git a/tests/server/test_admin_rebuild_api.py b/tests/server/test_admin_rebuild_api.py
index 4a5b5350d..a3ce35672 100644
--- a/tests/server/test_admin_rebuild_api.py
+++ b/tests/server/test_admin_rebuild_api.py
@@ -50,8 +50,8 @@ async def test_reindex_user_can_only_target_own_user_scope(monkeypatch):
seen = {}
class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
- seen.update(uri=uri, mode=mode, wait=wait, ctx=ctx)
+ async def reindex(self, *, uri, mode, wait, dry_run=False, ctx):
+ seen.update(uri=uri, mode=mode, wait=wait, dry_run=dry_run, ctx=ctx)
return {"status": "completed", "uri": uri, "mode": mode}
monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
@@ -110,10 +110,9 @@ async def test_reindex_rejects_unsupported_uri(admin_client: httpx.AsyncClient):
json={"uri": "viking://unknown/demo", "mode": "vectors_only"},
headers=ROOT_ACCOUNT_HEADERS,
)
- assert resp.status_code == 400
+ assert resp.status_code == 403
body = resp.json()
assert body["status"] == "error"
- assert body["error"]["code"] == "UNSUPPORTED_URI"
@pytest.mark.parametrize(
@@ -144,94 +143,821 @@ async def test_reindex_rejects_reason_field(admin_client: httpx.AsyncClient):
},
headers=ROOT_ACCOUNT_HEADERS,
)
- assert resp.status_code == 400
+ assert resp.status_code == 403
+
+
+async def test_reindex_root_requires_explicit_account(admin_client: httpx.AsyncClient):
+ resp = await admin_client.post(
+ "/api/v1/content/reindex",
+ json={"uri": "viking://resources/demo", "mode": "vectors_only"},
+ headers={"X-API-Key": ROOT_KEY},
+ )
+ assert resp.status_code == 403
+ body = resp.json()
+ assert body["status"] == "error"
+
+
+async def test_reindex_resource_vectors_only_wait_true(monkeypatch):
+ from openviking.server.routers.content import ReindexRequest, reindex
+
+ seen = {}
+
+ class FakeService:
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
+ seen["uri"] = uri
+ seen["mode"] = mode
+ seen["wait"] = wait
+ seen["ctx"] = ctx
+ return {
+ "status": "completed",
+ "uri": uri,
+ "object_type": "resource",
+ "mode": mode,
+ "rebuilt_records": 1,
+ "scanned_records": 1,
+ "unsupported_records": 0,
+ "failed_records": 0,
+ "duration_ms": 12,
+ "warnings": [],
+ }
+
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+ request = ReindexRequest(uri="viking://resources/demo", mode="vectors_only", wait=True)
+
+ monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
+ response = await reindex(body=request, ctx=ctx)
+
+ assert response.status == "ok"
+ assert response.result["status"] == "completed"
+ assert response.result["object_type"] == "resource"
+ assert response.result["rebuilt_records"] == 1
+ assert seen["uri"] == "viking://resources/demo"
+ assert seen["mode"] == "vectors_only"
+ assert seen["wait"] is True
+ assert seen["ctx"] == ctx
+ assert "reason" not in response.result
+
+
+async def test_reindex_resource_vectors_only_wait_false(monkeypatch):
+ from openviking.server.routers.content import ReindexRequest, reindex
+
+ class FakeService:
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
+ return {
+ "task_id": "rbld_123",
+ "status": "accepted",
+ "uri": uri,
+ "object_type": "resource",
+ "mode": mode,
+ }
+
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+ request = ReindexRequest(uri="viking://resources/demo", mode="vectors_only", wait=False)
+
+ monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
+ response = await reindex(body=request, ctx=ctx)
+
+ assert response.status == "ok"
+ assert response.result["status"] == "accepted"
+ assert response.result["task_id"] == "rbld_123"
+ assert response.result["object_type"] == "resource"
+ assert "reason" not in response.result
+
+
+async def test_reindex_prune_orphans_passes_dry_run_to_service(monkeypatch):
+ from openviking.server.routers.content import ReindexRequest, reindex
+
+ seen = {}
+ class FakeService:
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
+ seen["uri"] = uri
+ seen["mode"] = mode
+ seen["wait"] = wait
+ seen["dry_run"] = dry_run
+ return {
+ "status": "completed",
+ "uri": uri,
+ "object_type": "resource",
+ "mode": mode,
+ "scanned_records": 1,
+ "deleted_records": 0,
+ "would_delete_records": 1,
+ "unsupported_records": 0,
+ "failed_records": 0,
+ "duration_ms": 1,
+ "warnings": [],
+ }
+
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+ request = ReindexRequest(
+ uri="viking://resources/demo",
+ mode="prune_orphans",
+ wait=True,
+ dry_run=True,
+ )
+
+ monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
+ response = await reindex(body=request, ctx=ctx)
+
+ assert response.status == "ok"
+ assert response.result["would_delete_records"] == 1
+ assert seen == {
+ "uri": "viking://resources/demo",
+ "mode": "prune_orphans",
+ "wait": True,
+ "dry_run": True,
+ }
+
+
+async def test_reindex_rejects_dry_run_for_non_prune_mode(monkeypatch):
+ from openviking.server.routers.content import ReindexRequest, reindex
+
+ class FakeService:
+ async def reindex(self, **kwargs):
+ raise AssertionError("service should not be called")
+
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+ request = ReindexRequest(
+ uri="viking://resources/demo",
+ mode="vectors_only",
+ wait=True,
+ dry_run=True,
+ )
+
+ monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
+ with pytest.raises(OpenVikingError, match="dry_run"):
+ await reindex(body=request, ctx=ctx)
+
+
+async def test_reindex_executor_rejects_dry_run_for_non_prune_mode_direct():
+ from openviking.service.reindex_executor import ReindexExecutor
+
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+
+ with pytest.raises(OpenVikingError, match="dry_run"):
+ await ReindexExecutor().execute(
+ uri="viking://resources/demo",
+ mode="vectors_only",
+ wait=True,
+ dry_run=True,
+ ctx=ctx,
+ )
+
+
+async def test_reindex_upsert_uses_uri_owner_for_user_scoped_records(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor
+
+ captured = {}
+
+ class FakeVikingDB:
+ async def get_context_by_uri(self, uri, owner_space=None, level=None, limit=1, *, ctx=None):
+ captured["lookup_ctx"] = ctx
+ return []
+
+ async def enqueue_embedding_msg(self, msg):
+ captured["msg"] = msg
+ return True
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+
+ service = ReindexExecutor()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="acct", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await service._upsert_context(
+ uri="viking://user/bob/memories/preferences/theme.md",
+ parent_uri="viking://user/bob/memories/preferences",
+ abstract="theme",
+ vector_text="theme",
+ is_leaf=True,
+ context_type="memory",
+ level=ContextLevel.DETAIL,
+ ctx=ctx,
+ )
+
+ data = captured["msg"].context_data
+ assert data["user"]["user_id"] == "bob"
+ assert data["owner_user_id"] == "bob"
+ assert data["owner_space"] == "bob"
+ assert captured["lookup_ctx"].user.user_id == "bob"
+
+
+async def test_reindex_semantic_processor_uses_uri_owner_for_user_scoped_records(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor
+
+ captured = {}
+
+ class FakeSemanticProcessor:
+ def __init__(self, **_kwargs):
+ pass
+
+ async def on_dequeue(self, payload, lock=None):
+ captured["payload"] = payload
+ captured["lock"] = lock
+
+ monkeypatch.setattr(
+ "openviking.service.reindex_executor.SemanticProcessor",
+ FakeSemanticProcessor,
+ )
+
+ service = ReindexExecutor()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="acct", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await service._run_semantic_processor(
+ uri="viking://user/bob/memories/preferences",
+ context_type="memory",
+ ctx=ctx,
+ )
+
+ data = captured["payload"]["data"]
+ assert '"user_id": "bob"' in data
+ assert '"peer_id": "bob"' in data
+ assert '"account_id": "acct"' in data
+
+
+async def test_prune_orphans_candidate_filter_includes_target_uri(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+ from openviking.storage.expr import And, Eq, Or, PathScope
+
+ target_uri = "viking://resources/demo/missing.txt"
+
+ def matches_target_self(filter_expr):
+ if isinstance(filter_expr, And):
+ return all(matches_target_self(cond) for cond in filter_expr.conds)
+ if isinstance(filter_expr, Or):
+ return any(matches_target_self(cond) for cond in filter_expr.conds)
+ if isinstance(filter_expr, Eq):
+ return True
+ if isinstance(filter_expr, PathScope):
+ return filter_expr.path == target_uri and filter_expr.depth == 0
+ return False
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return False
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ if not matches_target_self(filter):
+ return []
+ return [
+ {
+ "id": "target",
+ "uri": target_uri,
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "test",
+ }
+ ]
+
+ async def delete(self, ids, *, ctx):
+ raise AssertionError("no records should be deleted")
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri=target_uri,
+ object_type="resource",
+ dry_run=True,
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert counters.scanned_records == 1
+ assert counters.would_delete_records == 1
+
+
+async def test_prune_orphans_dry_run_preserves_resource_l1_fallback_and_skips_unknown(
+ monkeypatch,
+):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ if uri == "viking://resources/demo/.abstract.md":
+ return True
+ if uri == "viking://resources/demo/.overview.md":
+ return False
+ return False
+
+ async def read_file(self, uri, ctx=None):
+ if uri == "viking://resources/demo/.abstract.md":
+ return "resource abstract"
+ raise FileNotFoundError(uri)
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ assert limit == 100000
+ assert ctx.account_id == "test"
+ return [
+ {
+ "id": "keep_l1",
+ "uri": "viking://resources/demo",
+ "level": 1,
+ "context_type": "resource",
+ "account_id": "test",
+ },
+ {
+ "id": "delete_l2",
+ "uri": "viking://resources/demo/missing.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "test",
+ },
+ {
+ "id": "skip_unknown",
+ "uri": "viking://resources/demo/unknown",
+ "level": 99,
+ "context_type": "resource",
+ "account_id": "test",
+ },
+ ]
+
+ async def delete(self, ids, *, ctx):
+ raise AssertionError("dry_run should not delete")
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+
+ service = ReindexExecutor()
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await service._prune_orphan_vectors(
+ uri="viking://resources/demo",
+ object_type="resource",
+ dry_run=True,
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert counters.scanned_records == 3
+ assert counters.deleted_records == 0
+ assert counters.would_delete_records == 1
+ assert counters.unsupported_records == 1
+ assert any("unknown" in warning for warning in counters.warnings)
+
+
+async def test_prune_orphans_deletes_stale_memory_chunk_with_uri_owner_ctx(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ base_uri = "viking://user/bob/memories/preferences/theme.md"
+ deleted = {}
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return uri == base_uri
+
+ async def read_file(self, uri, ctx=None):
+ assert uri == base_uri
+ return "memory body"
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ assert limit == 100000
+ assert ctx.account_id == "acct"
+ return [
+ {
+ "id": "keep_base",
+ "uri": base_uri,
+ "level": 2,
+ "context_type": "memory",
+ "account_id": "acct",
+ "owner_user_id": "bob",
+ },
+ {
+ "id": "keep_chunk",
+ "uri": f"{base_uri}#chunk_0000",
+ "level": 2,
+ "context_type": "memory",
+ "account_id": "acct",
+ "owner_user_id": "bob",
+ },
+ {
+ "id": "delete_chunk",
+ "uri": f"{base_uri}#chunk_0001",
+ "level": 2,
+ "context_type": "memory",
+ "account_id": "acct",
+ "owner_user_id": "bob",
+ },
+ ]
+
+ async def delete(self, ids, *, ctx):
+ deleted["ids"] = ids
+ deleted["ctx"] = ctx
+ return len(ids)
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+ monkeypatch.setattr(
+ ReindexExecutor,
+ "_chunk_memory_body",
+ lambda self, uri, body: [(f"{uri}#chunk_0000", body)],
+ )
+
+ service = ReindexExecutor()
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="acct", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await service._prune_orphan_vectors(
+ uri="viking://user/bob/memories",
+ object_type="memory",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert deleted["ids"] == ["delete_chunk"]
+ assert deleted["ctx"].user.user_id == "bob"
+ assert counters.deleted_records == 1
+ assert counters.would_delete_records == 0
+
+
+async def test_prune_orphans_skips_resource_l1_when_sidecar_read_fails(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return uri in {
+ "viking://resources/demo/.overview.md",
+ "viking://resources/demo/.abstract.md",
+ }
+
+ async def read_file(self, uri, ctx=None):
+ raise OSError("temporary storage failure")
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ return [
+ {
+ "id": "keep_l1",
+ "uri": "viking://resources/demo",
+ "level": 1,
+ "context_type": "resource",
+ "account_id": "test",
+ }
+ ]
+
+ async def delete(self, ids, *, ctx):
+ raise AssertionError("read failures must not delete vectors")
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri="viking://resources/demo",
+ object_type="resource",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert counters.deleted_records == 0
+ assert counters.failed_records == 1
+ assert any("failed to read" in warning for warning in counters.warnings)
+
+
+async def test_prune_orphans_skips_memory_chunks_when_base_read_fails(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ base_uri = "viking://user/bob/memories/preferences/theme.md"
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return uri == base_uri
+
+ async def read_file(self, uri, ctx=None):
+ raise OSError("temporary storage failure")
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ return [
+ {
+ "id": "keep_chunk",
+ "uri": f"{base_uri}#chunk_0001",
+ "level": 2,
+ "context_type": "memory",
+ "account_id": "acct",
+ "owner_user_id": "bob",
+ }
+ ]
+
+ async def delete(self, ids, *, ctx):
+ raise AssertionError("base read failures must not delete chunks")
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="acct", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri="viking://user/bob/memories",
+ object_type="memory",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert counters.deleted_records == 0
+ assert counters.failed_records == 1
+ assert any("failed to read" in warning for warning in counters.warnings)
+
+
+async def test_prune_orphans_deletes_missing_file_with_string_level(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ deleted = {}
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return False
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ return [
+ {
+ "id": "delete_l2",
+ "uri": "viking://resources/demo/missing.txt",
+ "level": "2",
+ "context_type": "resource",
+ "account_id": "test",
+ }
+ ]
+
+ async def delete(self, ids, *, ctx):
+ deleted["ids"] = ids
+ return len(ids)
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri="viking://resources/demo",
+ object_type="resource",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert deleted["ids"] == ["delete_l2"]
+ assert counters.deleted_records == 1
+ assert counters.unsupported_records == 0
+
+
+async def test_prune_orphans_continues_after_delete_group_failure(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ deleted = {}
+ filter_calls = 0
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return False
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx):
+ nonlocal filter_calls
+ filter_calls += 1
+ if filter_calls > 1:
+ return []
+ return [
+ {
+ "id": "fail_delete",
+ "uri": "viking://user/alice/docs/missing.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "acct",
+ "owner_user_id": "alice",
+ },
+ {
+ "id": "ok_delete",
+ "uri": "viking://user/bob/docs/missing.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "acct",
+ "owner_user_id": "bob",
+ },
+ ]
+
+ async def delete(self, ids, *, ctx):
+ if ctx.user.user_id == "alice":
+ raise RuntimeError("delete backend unavailable")
+ deleted["ids"] = ids
+ deleted["ctx"] = ctx
+ return len(ids)
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="acct", user_id="admin"),
+ role=Role.ROOT,
+ )
-async def test_reindex_root_requires_explicit_account(admin_client: httpx.AsyncClient):
- resp = await admin_client.post(
- "/api/v1/content/reindex",
- json={"uri": "viking://resources/demo", "mode": "vectors_only"},
- headers={"X-API-Key": ROOT_KEY},
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri="viking://user",
+ object_type="user_namespace",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
)
- assert resp.status_code == 400
- body = resp.json()
- assert body["status"] == "error"
- assert body["error"]["code"] == "INVALID_ARGUMENT"
+
+ assert deleted["ids"] == ["ok_delete"]
+ assert deleted["ctx"].user.user_id == "bob"
+ assert counters.deleted_records == 1
+ assert counters.failed_records == 1
+ assert any("Failed to delete" in warning for warning in counters.warnings)
@pytest.mark.asyncio
-async def test_reindex_resource_vectors_only_wait_true(monkeypatch):
- from openviking.server.routers.content import ReindexRequest, reindex
+async def test_prune_orphans_pages_past_candidate_limit(monkeypatch):
+ from openviking.service import reindex_executor
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
- seen = {}
+ filter_offsets = []
+ deleted = {}
- class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
- seen["uri"] = uri
- seen["mode"] = mode
- seen["wait"] = wait
- seen["ctx"] = ctx
- return {
- "status": "completed",
- "uri": uri,
- "object_type": "resource",
- "mode": mode,
- "rebuilt_records": 1,
- "scanned_records": 1,
- "unsupported_records": 0,
- "failed_records": 0,
- "duration_ms": 12,
- "warnings": [],
- }
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return not uri.endswith("missing.txt")
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx, offset=0):
+ filter_offsets.append(offset)
+ records = [
+ {
+ "id": "keep_1",
+ "uri": "viking://resources/demo/keep-1.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "acct",
+ },
+ {
+ "id": "keep_2",
+ "uri": "viking://resources/demo/keep-2.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "acct",
+ },
+ {
+ "id": "delete_3",
+ "uri": "viking://resources/demo/missing.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "acct",
+ },
+ ]
+ return records[offset : offset + limit]
+
+ async def delete(self, ids, *, ctx):
+ deleted["ids"] = ids
+ return len(ids)
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+ monkeypatch.setattr(reindex_executor, "PRUNE_ORPHAN_CANDIDATE_LIMIT", 2)
+ counters = _ReindexCounters()
ctx = RequestContext(
- user=UserIdentifier(account_id="test", user_id="alice"),
+ user=UserIdentifier(account_id="acct", user_id="admin"),
role=Role.ROOT,
)
- request = ReindexRequest(uri="viking://resources/demo", mode="vectors_only", wait=True)
- monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
- response = await reindex(body=request, ctx=ctx)
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri="viking://resources/demo",
+ object_type="resource",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
+ )
- assert response.status == "ok"
- assert response.result["status"] == "completed"
- assert response.result["object_type"] == "resource"
- assert response.result["rebuilt_records"] == 1
- assert seen["uri"] == "viking://resources/demo"
- assert seen["mode"] == "vectors_only"
- assert seen["wait"] is True
- assert seen["ctx"] == ctx
- assert "reason" not in response.result
+ assert filter_offsets == [0, 2]
+ assert counters.scanned_records == 3
+ assert deleted["ids"] == ["delete_3"]
+ assert counters.deleted_records == 1
@pytest.mark.asyncio
-async def test_reindex_resource_vectors_only_wait_false(monkeypatch):
- from openviking.server.routers.content import ReindexRequest, reindex
+async def test_prune_orphans_reports_zero_delete_count_as_failure(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
- class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
- return {
- "task_id": "rbld_123",
- "status": "accepted",
- "uri": uri,
- "object_type": "resource",
- "mode": mode,
- }
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return False
+
+ class FakeVikingDB:
+ async def filter(self, *, filter, limit, output_fields, ctx, offset=0):
+ if offset:
+ return []
+ return [
+ {
+ "id": "stale",
+ "uri": "viking://resources/demo/missing.txt",
+ "level": 2,
+ "context_type": "resource",
+ "account_id": "acct",
+ }
+ ]
+
+ async def delete(self, ids, *, ctx):
+ return 0
+
+ fake_service = type("Svc", (), {"vikingdb_manager": FakeVikingDB()})()
+ monkeypatch.setattr("openviking.service.reindex_executor.get_service", lambda: fake_service)
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+ counters = _ReindexCounters()
ctx = RequestContext(
- user=UserIdentifier(account_id="test", user_id="alice"),
+ user=UserIdentifier(account_id="acct", user_id="admin"),
role=Role.ROOT,
)
- request = ReindexRequest(uri="viking://resources/demo", mode="vectors_only", wait=False)
- monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
- response = await reindex(body=request, ctx=ctx)
+ await ReindexExecutor()._prune_orphan_vectors(
+ uri="viking://resources/demo",
+ object_type="resource",
+ dry_run=False,
+ counters=counters,
+ ctx=ctx,
+ )
- assert response.status == "ok"
- assert response.result["status"] == "accepted"
- assert response.result["task_id"] == "rbld_123"
- assert response.result["object_type"] == "resource"
- assert "reason" not in response.result
+ assert counters.deleted_records == 0
+ assert counters.failed_records == 1
+ assert any("Only deleted 0 of 1" in warning for warning in counters.warnings)
@pytest.mark.asyncio
@@ -353,7 +1079,7 @@ async def test_reindex_executor_rejects_deprecated_agent_namespace_root():
service = ReindexExecutor()
- with pytest.raises(OpenVikingError, match="viking://agent is deprecated"):
+ with pytest.raises(OpenVikingError, match="no longer supported"):
service._infer_target_type("viking://agent/")
@@ -859,6 +1585,9 @@ async def test_reindex_semantic_processor_runs_with_skip_vectorization(monkeypat
seen = {}
class FakeSemanticProcessor:
+ def __init__(self, **_kwargs):
+ pass
+
async def on_dequeue(self, payload, lock=None):
seen["payload"] = payload
@@ -981,6 +1710,138 @@ async def test_reindex_resource_vector_text_uses_existing_record_for_non_text(mo
assert vector_text == "existing image summary"
+async def test_reindex_file_summary_reads_existing_record_as_uri_owner(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor
+
+ captured = {}
+
+ async def fake_safe_read_text(self, uri, *, ctx):
+ return ""
+
+ async def fake_fetch_existing_record(self, *, uri, level, ctx):
+ captured["ctx"] = ctx
+ return {"abstract": "owner summary"}
+
+ monkeypatch.setattr(ReindexExecutor, "_safe_read_text", fake_safe_read_text)
+ monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
+
+ service = ReindexExecutor()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ summary = await service._best_file_summary(
+ "viking://user/bob/resources/demo/image.png",
+ ctx=ctx,
+ )
+
+ assert summary == "owner summary"
+ assert captured["ctx"].user.user_id == "bob"
+
+
+async def test_reindex_memory_fallback_reads_existing_record_as_uri_owner(monkeypatch):
+ from openviking.service.reindex_executor import (
+ ReindexExecutor,
+ _PruneSourceRead,
+ _ReindexCounters,
+ )
+
+ captured = {}
+ upserts = []
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return True
+
+ async def stat(self, uri, ctx=None):
+ return {"isDir": False}
+
+ async def fake_read_memory_body(self, uri, *, ctx):
+ return _PruneSourceRead(exists=True, text="")
+
+ async def fake_fetch_existing_record(self, *, uri, level, ctx):
+ captured["ctx"] = ctx
+ return {"abstract": "owner memory summary"}
+
+ async def fake_best_file_summary(self, uri, *, ctx):
+ return ""
+
+ async def fake_upsert_context(self, **kwargs):
+ upserts.append(kwargs)
+
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+ monkeypatch.setattr(ReindexExecutor, "_read_memory_body", fake_read_memory_body)
+ monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
+ monkeypatch.setattr(ReindexExecutor, "_best_file_summary", fake_best_file_summary)
+ monkeypatch.setattr(ReindexExecutor, "_upsert_context", fake_upsert_context)
+
+ service = ReindexExecutor()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="admin"),
+ role=Role.ROOT,
+ )
+
+ await service._reindex_memory_vectors(
+ uri="viking://user/bob/memories/preferences/theme.md",
+ counters=_ReindexCounters(),
+ ctx=ctx,
+ )
+
+ assert captured["ctx"].user.user_id == "bob"
+ assert upserts[0]["abstract"] == "owner memory summary"
+
+
+async def test_reindex_memory_skips_fallback_when_body_read_fails(monkeypatch):
+ from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+
+ upserts = []
+
+ class FakeVikingFS:
+ async def exists(self, uri, ctx=None):
+ return True
+
+ async def stat(self, uri, ctx=None):
+ return {"isDir": False}
+
+ async def read_file(self, uri, ctx=None):
+ raise OSError("backend read failed")
+
+ async def fake_fetch_existing_record(self, *, uri, level, ctx):
+ return {"abstract": "existing summary"}
+
+ async def fake_best_file_summary(self, uri, *, ctx):
+ return ""
+
+ async def fake_upsert_context(self, **kwargs):
+ upserts.append(kwargs)
+
+ monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
+ monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
+ monkeypatch.setattr(ReindexExecutor, "_best_file_summary", fake_best_file_summary)
+ monkeypatch.setattr(ReindexExecutor, "_upsert_context", fake_upsert_context)
+
+ service = ReindexExecutor()
+ counters = _ReindexCounters()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+
+ await service._reindex_memory_vectors(
+ uri="viking://user/default/memories/events/item.md",
+ counters=counters,
+ ctx=ctx,
+ )
+
+ assert upserts == []
+ assert counters.failed_records == 1
+ assert any("failed to read memory body" in warning for warning in counters.warnings)
+
+
@pytest.mark.asyncio
async def test_reindex_resource_vector_text_skips_non_text_body_without_summary(monkeypatch):
from openviking.service.reindex_executor import ReindexExecutor
@@ -1063,7 +1924,11 @@ async def test_reindex_resource_vectors_accepts_single_file_uri(monkeypatch):
@pytest.mark.asyncio
async def test_reindex_memory_l2_falls_back_to_body_when_abstract_missing(monkeypatch):
- from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+ from openviking.service.reindex_executor import (
+ ReindexExecutor,
+ _PruneSourceRead,
+ _ReindexCounters,
+ )
class FakeVikingFS:
async def exists(self, uri, ctx=None):
@@ -1074,8 +1939,8 @@ async def test_reindex_memory_l2_falls_back_to_body_when_abstract_missing(monkey
seen = {}
- async def fake_safe_read_text(self, uri, *, ctx):
- return "memory body text"
+ async def fake_read_memory_body(self, uri, *, ctx):
+ return _PruneSourceRead(exists=True, text="memory body text")
async def fake_fetch_existing_record(self, *, uri, level, ctx):
return None
@@ -1087,7 +1952,7 @@ async def test_reindex_memory_l2_falls_back_to_body_when_abstract_missing(monkey
seen[kwargs["uri"]] = kwargs
monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
- monkeypatch.setattr(ReindexExecutor, "_safe_read_text", fake_safe_read_text)
+ monkeypatch.setattr(ReindexExecutor, "_read_memory_body", fake_read_memory_body)
monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
monkeypatch.setattr(ReindexExecutor, "_best_file_summary", fake_best_file_summary)
monkeypatch.setattr(ReindexExecutor, "_upsert_context", fake_upsert_context)
@@ -1110,7 +1975,11 @@ async def test_reindex_memory_l2_falls_back_to_body_when_abstract_missing(monkey
@pytest.mark.asyncio
async def test_reindex_memory_l2_strips_memory_fields_from_abstract(monkeypatch):
- from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+ from openviking.service.reindex_executor import (
+ ReindexExecutor,
+ _PruneSourceRead,
+ _ReindexCounters,
+ )
class FakeVikingFS:
async def exists(self, uri, ctx=None):
@@ -1125,8 +1994,8 @@ async def test_reindex_memory_l2_strips_memory_fields_from_abstract(monkeypatch)
'\'我爱吃西瓜\'. <!-- MEMORY_FIELDS { "user": "user", "topic": "food_preference" } -->'
)
- async def fake_safe_read_text(self, uri, *, ctx):
- return raw_body
+ async def fake_read_memory_body(self, uri, *, ctx):
+ return _PruneSourceRead(exists=True, text=raw_body)
async def fake_fetch_existing_record(self, *, uri, level, ctx):
return None
@@ -1138,7 +2007,7 @@ async def test_reindex_memory_l2_strips_memory_fields_from_abstract(monkeypatch)
seen[kwargs["uri"]] = kwargs
monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
- monkeypatch.setattr(ReindexExecutor, "_safe_read_text", fake_safe_read_text)
+ monkeypatch.setattr(ReindexExecutor, "_read_memory_body", fake_read_memory_body)
monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
monkeypatch.setattr(ReindexExecutor, "_best_file_summary", fake_best_file_summary)
monkeypatch.setattr(ReindexExecutor, "_upsert_context", fake_upsert_context)
@@ -1168,7 +2037,11 @@ async def test_reindex_memory_l2_strips_memory_fields_from_abstract(monkeypatch)
@pytest.mark.asyncio
async def test_reindex_memory_vectors_walks_deep_subtree(monkeypatch):
- from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+ from openviking.service.reindex_executor import (
+ ReindexExecutor,
+ _PruneSourceRead,
+ _ReindexCounters,
+ )
class FakeVikingFS:
async def exists(self, uri, ctx=None):
@@ -1200,8 +2073,8 @@ async def test_reindex_memory_vectors_walks_deep_subtree(monkeypatch):
seen = {}
- async def fake_safe_read_text(self, uri, *, ctx):
- return "likes spicy food"
+ async def fake_read_memory_body(self, uri, *, ctx):
+ return _PruneSourceRead(exists=True, text="likes spicy food")
async def fake_fetch_existing_record(self, *, uri, level, ctx):
return None
@@ -1213,7 +2086,7 @@ async def test_reindex_memory_vectors_walks_deep_subtree(monkeypatch):
seen[kwargs["uri"]] = kwargs
monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
- monkeypatch.setattr(ReindexExecutor, "_safe_read_text", fake_safe_read_text)
+ monkeypatch.setattr(ReindexExecutor, "_read_memory_body", fake_read_memory_body)
monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
monkeypatch.setattr(ReindexExecutor, "_best_file_summary", fake_best_file_summary)
monkeypatch.setattr(ReindexExecutor, "_upsert_context", fake_upsert_context)
@@ -1239,7 +2112,11 @@ async def test_reindex_memory_vectors_rebuilds_directory_levels_without_regenera
monkeypatch,
):
from openviking.core.context import ContextLevel
- from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
+ from openviking.service.reindex_executor import (
+ ReindexExecutor,
+ _PruneSourceRead,
+ _ReindexCounters,
+ )
class FakeVikingFS:
async def exists(self, uri, ctx=None):
@@ -1279,8 +2156,8 @@ async def test_reindex_memory_vectors_rebuilds_directory_levels_without_regenera
return "user preferences overview"
return ""
- async def fake_safe_read_text(self, uri, *, ctx):
- return "likes spicy food"
+ async def fake_read_memory_body(self, uri, *, ctx):
+ return _PruneSourceRead(exists=True, text="likes spicy food")
async def fake_fetch_existing_record(self, *, uri, level, ctx):
return None
@@ -1300,7 +2177,7 @@ async def test_reindex_memory_vectors_rebuilds_directory_levels_without_regenera
monkeypatch.setattr("openviking.service.reindex_executor.get_viking_fs", lambda: FakeVikingFS())
monkeypatch.setattr(ReindexExecutor, "_read_directory_abstract", fake_read_directory_abstract)
monkeypatch.setattr(ReindexExecutor, "_read_directory_overview", fake_read_directory_overview)
- monkeypatch.setattr(ReindexExecutor, "_safe_read_text", fake_safe_read_text)
+ monkeypatch.setattr(ReindexExecutor, "_read_memory_body", fake_read_memory_body)
monkeypatch.setattr(ReindexExecutor, "_fetch_existing_record", fake_fetch_existing_record)
monkeypatch.setattr(ReindexExecutor, "_best_file_summary", fake_best_file_summary)
monkeypatch.setattr(ReindexExecutor, "_upsert_context", fake_upsert_context)
@@ -1546,7 +2423,7 @@ async def test_openviking_service_reindex_uses_default_root_context(monkeypatch)
seen = {}
class FakeExecutor:
- async def execute(self, *, uri, mode, wait, ctx):
+ async def execute(self, *, uri, mode, wait, ctx, dry_run=False):
seen["uri"] = uri
seen["mode"] = mode
seen["wait"] = wait
@@ -1576,3 +2453,45 @@ async def test_openviking_service_reindex_uses_default_root_context(monkeypatch)
assert seen["ctx"].role == Role.ROOT
assert seen["ctx"].user.account_id == "acct"
assert seen["ctx"].user.user_id == "alice"
+
+
+async def test_openviking_service_reindex_canonicalizes_user_shorthand(monkeypatch):
+ from openviking.service.core import OpenVikingService
+
+ seen = {}
+
+ class FakeExecutor:
+ async def execute(self, *, uri, mode, wait, ctx, dry_run=False):
+ seen["uri"] = uri
+ seen["ctx"] = ctx
+ return {"status": "completed", "uri": uri}
+
+ import openviking.service.reindex_executor as reindex_executor
+
+ monkeypatch.setattr(
+ reindex_executor,
+ "get_reindex_executor",
+ lambda: FakeExecutor(),
+ )
+
+ service = OpenVikingService.__new__(OpenVikingService)
+ service._initialized = True
+ service._user = UserIdentifier(account_id="acct", user_id="alice")
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="acct", user_id="alice"),
+ role=Role.ADMIN,
+ )
+
+ result = await OpenVikingService.reindex(
+ service,
+ uri="viking://user/memories",
+ mode="prune_orphans",
+ wait=True,
+ dry_run=True,
+ ctx=ctx,
+ )
+
+ assert result == {"status": "completed", "uri": "viking://user/alice/memories"}
+ assert seen["uri"] == "viking://user/alice/memories"
+ assert seen["ctx"] is ctx
diff --git a/tests/server/test_api_content.py b/tests/server/test_api_content.py
index ecfbffa3b..1c00b1198 100644
--- a/tests/server/test_api_content.py
+++ b/tests/server/test_api_content.py
@@ -146,7 +146,7 @@ async def test_reindex_uses_request_tenant_for_exists(monkeypatch):
seen = {}
class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
seen["uri"] = uri
seen["mode"] = mode
seen["wait"] = wait
diff --git a/tests/server/test_api_fs_content_endpoint_suite.py b/tests/server/test_api_fs_content_endpoint_suite.py
index 95c1bf2f4..f7c6137bd 100644
--- a/tests/server/test_api_fs_content_endpoint_suite.py
+++ b/tests/server/test_api_fs_content_endpoint_suite.py
@@ -198,7 +198,7 @@ async def test_write_permission_denied_returns_structured_error(app, service, mo
async def test_reindex_missing_uri_returns_not_found_error_payload(client, monkeypatch):
class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
from openviking_cli.exceptions import NotFoundError
raise NotFoundError(uri, "resource")
@@ -213,7 +213,7 @@ async def test_reindex_missing_uri_returns_not_found_error_payload(client, monke
async def test_reindex_sync_conflict_returns_error_payload(client, monkeypatch):
class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
from openviking_cli.exceptions import OpenVikingError
raise OpenVikingError(
@@ -232,7 +232,7 @@ async def test_reindex_sync_conflict_returns_error_payload(client, monkeypatch):
async def test_reindex_sync_success_returns_ok_payload(client, monkeypatch):
class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
return {"status": "completed", "mode": mode, "uri": uri}
monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())
@@ -249,7 +249,7 @@ async def test_reindex_sync_success_returns_ok_payload(client, monkeypatch):
async def test_reindex_async_returns_task_id(client, monkeypatch):
class FakeService:
- async def reindex(self, *, uri, mode, wait, ctx):
+ async def reindex(self, *, uri, mode, wait, ctx, dry_run=False):
return {
"task_id": "task-123",
"status": "accepted",
diff --git a/tests/server/test_auth.py b/tests/server/test_auth.py
index 665bbadab..6a76cd31d 100644
--- a/tests/server/test_auth.py
+++ b/tests/server/test_auth.py
@@ -90,6 +90,7 @@ def _make_request(
) -> Request:
"""Create a minimal Starlette request for auth dependency tests."""
# Ensure built-in plugins are registered
+ import openviking.server.auth.plugins # noqa: F401
raw_headers = []
for key, value in (headers or {}).items():
@@ -128,6 +129,7 @@ def _build_auth_http_test_app(
the test focused on request auth behavior and the structured HTTP error body.
"""
# Ensure built-in plugins are registered
+ import openviking.server.auth.plugins # noqa: F401
app = FastAPI()
# When auth is disabled and mode is the default api_key, fall back to dev mode
@@ -1097,6 +1099,84 @@ async def test_trusted_mode_with_root_api_key_accepts_matching_api_key():
assert identity.user_id == "alice"
+async def test_trusted_mode_root_key_allows_admin_role_header(auth_app):
+ """Trusted upstreams with the root key may assert ADMIN role for tenant routes."""
+ manager = auth_app.state.api_key_manager
+ account_id = _uid()
+ await manager.create_account(account_id, "owner")
+ await manager.register_user(account_id, "alice", "user")
+
+ request = _make_request(
+ "/api/v1/resources",
+ headers={
+ "X-API-Key": ROOT_KEY,
+ "X-OpenViking-Account": account_id,
+ "X-OpenViking-User": "alice",
+ "X-OpenViking-Role": " ADMIN ",
+ },
+ auth_enabled=True,
+ auth_mode="trusted",
+ root_api_key=ROOT_KEY,
+ api_key_manager=manager,
+ )
+
+ identity = await resolve_identity(
+ request,
+ x_api_key=ROOT_KEY,
+ x_openviking_account=account_id,
+ x_openviking_user="alice",
+ )
+
+ assert identity.role == Role.ADMIN
+ assert identity.account_id == account_id
+ assert identity.user_id == "alice"
+
+
+async def test_trusted_mode_rejects_root_role_header():
+ """Trusted role header should not allow asserting ROOT."""
+ request = _make_request(
+ "/api/v1/resources",
+ headers={
+ "X-API-Key": ROOT_KEY,
+ "X-OpenViking-Account": "acme",
+ "X-OpenViking-User": "alice",
+ "X-OpenViking-Role": "root",
+ },
+ auth_enabled=False,
+ auth_mode="trusted",
+ root_api_key=ROOT_KEY,
+ )
+
+ with pytest.raises(InvalidArgumentError, match="X-OpenViking-Role"):
+ await resolve_identity(
+ request,
+ x_api_key=ROOT_KEY,
+ x_openviking_account="acme",
+ x_openviking_user="alice",
+ )
+
+
+async def test_trusted_mode_rejects_role_header_without_root_key():
+ """Role assertions require a configured root key so localhost trusted mode cannot self-elevate."""
+ request = _make_request(
+ "/api/v1/resources",
+ headers={
+ "X-OpenViking-Account": "acme",
+ "X-OpenViking-User": "alice",
+ "X-OpenViking-Role": "admin",
+ },
+ auth_enabled=False,
+ auth_mode="trusted",
+ )
+
+ with pytest.raises(InvalidArgumentError, match="X-OpenViking-Role"):
+ await resolve_identity(
+ request,
+ x_openviking_account="acme",
+ x_openviking_user="alice",
+ )
+
+
async def test_trusted_mode_tenant_http_routes_require_explicit_identity_headers():
"""Trusted mode should reject tenant-scoped routes without account/user headers."""
app = _build_auth_http_test_app(
diff --git a/web-studio/src/gen/ov-client/types.gen.ts b/web-studio/src/gen/ov-client/types.gen.ts
index a4425c5b5..a0e69785f 100644
--- a/web-studio/src/gen/ov-client/types.gen.ts
+++ b/web-studio/src/gen/ov-client/types.gen.ts
@@ -562,6 +562,10 @@ export type ReindexRequest = {
* Wait
*/
wait?: boolean;
+ /**
+ * Dry Run
+ */
+ dry_run?: boolean;
};
/**