Skip to content

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

Set tags (#2706)

Jiahui Zhou21c58bd8f610
diff --git a/crates/ov_cli/src/client.rs b/crates/ov_cli/src/client.rs
index 3c981bfdb..7078797a7 100644
--- a/crates/ov_cli/src/client.rs
+++ b/crates/ov_cli/src/client.rs
@@ -215,6 +215,22 @@ impl HttpClient {
self.post("/api/v1/content/write", &body).await
}
+ pub async fn set_tags(
+ &self,
+ uri: &str,
+ tags: Vec<String>,
+ mode: &str,
+ recursive: bool,
+ ) -> Result<serde_json::Value> {
+ let body = serde_json::json!({
+ "uri": uri,
+ "tags": tags,
+ "mode": mode,
+ "recursive": recursive,
+ });
+ self.post("/api/v1/content/set_tags", &body).await
+ }
+
fn build_write_body(
uri: &str,
content: &str,
@@ -410,6 +426,7 @@ impl HttpClient {
time_field: Option<String>,
level: Option<Vec<i32>>,
context_type: Option<Vec<String>>,
+ tags: Option<Vec<String>>,
) -> Result<serde_json::Value> {
let mut body = serde_json::json!({
"query": query,
@@ -421,6 +438,7 @@ impl HttpClient {
"time_field": time_field,
"level": level,
"context_type": context_type,
+ "tags": tags,
});
self.attach_legacy_agent_scope(&mut body);
self.post("/api/v1/search/find", &body).await
@@ -438,6 +456,7 @@ impl HttpClient {
time_field: Option<String>,
level: Option<Vec<i32>>,
context_type: Option<Vec<String>>,
+ tags: Option<Vec<String>>,
) -> Result<serde_json::Value> {
let mut body = serde_json::json!({
"query": query,
@@ -450,6 +469,7 @@ impl HttpClient {
"time_field": time_field,
"level": level,
"context_type": context_type,
+ "tags": tags,
});
self.attach_legacy_agent_scope(&mut body);
self.post("/api/v1/search/search", &body).await
diff --git a/crates/ov_cli/src/commands/content.rs b/crates/ov_cli/src/commands/content.rs
index 60130d306..b9848c6f5 100644
--- a/crates/ov_cli/src/commands/content.rs
+++ b/crates/ov_cli/src/commands/content.rs
@@ -1,7 +1,8 @@
use crate::client::HttpClient;
use crate::error::Result;
use crate::output::OutputFormat;
-use serde_json::Value;
+use serde_json::{Value, json};
+use std::collections::BTreeSet;
use std::fs::File;
use std::io::Write;
use std::path::Path;
@@ -51,6 +52,21 @@ pub async fn write(
Ok(())
}
+pub async fn set_tags(
+ client: &HttpClient,
+ uri: &str,
+ tags: Vec<String>,
+ mode: &str,
+ recursive: bool,
+ output_format: OutputFormat,
+ compact: bool,
+) -> Result<()> {
+ // Tags are explicit k=v metadata labels; append mode replaces any existing value for the same key.
+ let result = client.set_tags(uri, tags, mode, recursive).await?;
+ output_set_tags_result(result, output_format, compact);
+ Ok(())
+}
+
pub async fn reindex(
client: &HttpClient,
uri: &str,
@@ -107,10 +123,96 @@ fn output_content_result(result: Value, output_format: OutputFormat, compact: bo
Ok(())
}
+fn output_set_tags_result(result: Value, output_format: OutputFormat, compact: bool) {
+ match output_format {
+ OutputFormat::Json => crate::output::output_success(result, output_format, compact),
+ OutputFormat::Table => {
+ if let Some(rendered) = render_set_tags_result_for_table(&result) {
+ println!("{rendered}");
+ } else {
+ crate::output::output_success(result, output_format, compact);
+ }
+ }
+ }
+}
+
+fn render_set_tags_result_for_table(result: &Value) -> Option<String> {
+ let obj = result.as_object()?;
+ let uri = obj.get("uri")?.as_str()?;
+
+ let tags = obj
+ .get("tags")
+ .and_then(Value::as_array)
+ .map(|items| {
+ items
+ .iter()
+ .filter_map(Value::as_str)
+ .collect::<Vec<_>>()
+ .join(", ")
+ })
+ .unwrap_or_default();
+
+ let updated_uris = obj
+ .get("updated_uris")
+ .and_then(Value::as_array)
+ .map(|items| {
+ let unique = items
+ .iter()
+ .filter_map(Value::as_str)
+ .collect::<BTreeSet<_>>();
+ let count = unique.len();
+ if count == 0 {
+ String::new()
+ } else if count == 1 {
+ unique.into_iter().next().unwrap_or_default().to_string()
+ } else {
+ let lines = unique
+ .into_iter()
+ .map(|item| format!("- {item}"))
+ .collect::<Vec<_>>()
+ .join("\n");
+ format!("{count} updates\n{lines}")
+ }
+ })
+ .unwrap_or_default();
+
+ let display = json!({
+ "uri": uri,
+ "tags": tags,
+ "updated_uris": updated_uris,
+ "mode": obj.get("mode").cloned().unwrap_or(Value::Null),
+ "success_count": obj.get("success_count").cloned().unwrap_or(Value::Null),
+ "skipped_count": obj.get("skipped_count").cloned().unwrap_or(Value::Null),
+ "failed_count": obj.get("failed_count").cloned().unwrap_or(Value::Null),
+ "tags_updated": obj.get("tags_updated").cloned().unwrap_or(Value::Null),
+ });
+
+ crate::output::render_table_with_optional_profile(&display, true)
+}
+
#[cfg(test)]
mod tests {
use serde_json::json;
+ fn strip_ansi(input: &str) -> String {
+ let mut output = String::with_capacity(input.len());
+ let mut chars = input.chars().peekable();
+
+ while let Some(ch) = chars.next() {
+ if ch == '\u{1b}' && chars.peek() == Some(&'[') {
+ chars.next();
+ for next in chars.by_ref() {
+ if next.is_ascii_alphabetic() {
+ break;
+ }
+ }
+ continue;
+ }
+ output.push(ch);
+ }
+ output
+ }
+
#[test]
fn table_output_renders_profiled_scalar_content() {
let result = json!({
@@ -128,4 +230,44 @@ mod tests {
Some(["content", "", "profile", "line one", "line two", "",].join("\n"))
);
}
+
+ #[test]
+ fn table_output_renders_set_tags_uri_and_updated_uris() {
+ let result = json!({
+ "uri": "viking://resources/demo/doc.md",
+ "updated_uris": [
+ "viking://resources/demo/doc.md",
+ "viking://resources/demo/doc.md",
+ "viking://resources/demo/doc.md/doc_v2.md"
+ ],
+ "tags": ["team=test"],
+ "mode": "replace",
+ "success_count": 3,
+ "skipped_count": 0,
+ "failed_count": 0,
+ "tags_updated": true
+ });
+
+ let rendered = super::render_set_tags_result_for_table(&result).map(|value| strip_ansi(&value));
+
+ assert_eq!(
+ rendered,
+ Some(
+ [
+ "uri viking://resources/demo/doc.md",
+ "tags team=test",
+ "updated_uris 2 updates",
+ "- viking://resources/demo/doc.md",
+ "- viking://resources/demo/doc.md/doc_v2.md",
+ "mode replace",
+ "success_count 3",
+ "skipped_count 0",
+ "failed_count 0",
+ "tags_updated true",
+ ]
+ .join("\n")
+ + "\n"
+ )
+ );
+ }
}
diff --git a/crates/ov_cli/src/commands/search.rs b/crates/ov_cli/src/commands/search.rs
index ea51583d3..9d6142acd 100644
--- a/crates/ov_cli/src/commands/search.rs
+++ b/crates/ov_cli/src/commands/search.rs
@@ -55,6 +55,7 @@ pub async fn find(
time_field: Option<&str>,
level: Option<Vec<i32>>,
context_type: Option<Vec<String>>,
+ tags: Option<Vec<String>>,
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
@@ -69,6 +70,7 @@ pub async fn find(
time_field.map(|s| s.to_string()),
level,
context_type,
+ tags,
)
.await?;
output_search_results(
@@ -92,6 +94,7 @@ pub async fn search(
time_field: Option<&str>,
level: Option<Vec<i32>>,
context_type: Option<Vec<String>>,
+ tags: Option<Vec<String>>,
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
@@ -107,6 +110,7 @@ pub async fn search(
time_field.map(|s| s.to_string()),
level,
context_type,
+ tags,
)
.await?;
output_search_results(
diff --git a/crates/ov_cli/src/handlers.rs b/crates/ov_cli/src/handlers.rs
index ccae60177..d6b97482c 100644
--- a/crates/ov_cli/src/handlers.rs
+++ b/crates/ov_cli/src/handlers.rs
@@ -1207,6 +1207,26 @@ pub async fn handle_write(
.await
}
+pub async fn handle_set_tags(
+ uri: String,
+ tags: Vec<String>,
+ mode: String,
+ recursive: bool,
+ ctx: CliContext,
+) -> Result<()> {
+ let client = ctx.get_client();
+ commands::content::set_tags(
+ &client,
+ &uri,
+ tags,
+ &mode,
+ recursive,
+ ctx.output_format,
+ ctx.compact,
+ )
+ .await
+}
+
pub async fn handle_reindex(uri: String, mode: String, wait: bool, ctx: CliContext) -> Result<()> {
let client = ctx.get_client();
commands::content::reindex(&client, &uri, &mode, wait, ctx.output_format, ctx.compact).await
@@ -1226,6 +1246,7 @@ pub async fn handle_find(
before: Option<String>,
level: Option<Vec<i32>>,
context_type: Option<Vec<String>>,
+ tags: Option<Vec<String>>,
ctx: CliContext,
) -> Result<()> {
let mut params = vec![format!("--uri={}", uri), format!("-n {}", node_limit)];
@@ -1245,6 +1266,9 @@ pub async fn handle_find(
if let Some(ref context_types) = context_type {
params.push(format!("--context-type {}", context_types.join(",")));
}
+ if let Some(ref t) = tags {
+ params.push(format!("--tags {}", t.join(",")));
+ }
params.push(format!("\"{}\"", query));
print_command_echo("ov find", &params.join(" "), ctx.config.echo_command);
let client = ctx.get_client();
@@ -1259,6 +1283,7 @@ pub async fn handle_find(
None,
level,
context_type,
+ tags,
ctx.output_format,
ctx.compact,
)
@@ -1275,6 +1300,7 @@ pub async fn handle_search(
before: Option<String>,
level: Option<Vec<i32>>,
context_type: Option<Vec<String>>,
+ tags: Option<Vec<String>>,
ctx: CliContext,
) -> Result<()> {
let mut params = vec![format!("--uri={}", uri), format!("-n {}", node_limit)];
@@ -1297,6 +1323,9 @@ pub async fn handle_search(
if let Some(ref context_types) = context_type {
params.push(format!("--context-type {}", context_types.join(",")));
}
+ if let Some(ref t) = tags {
+ params.push(format!("--tags {}", t.join(",")));
+ }
params.push(format!("\"{}\"", query));
print_command_echo("ov search", &params.join(" "), ctx.config.echo_command);
let client = ctx.get_client();
@@ -1312,6 +1341,7 @@ pub async fn handle_search(
None,
level,
context_type,
+ tags,
ctx.output_format,
ctx.compact,
)
diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rs
index 38fb5386f..6363b7153 100644
--- a/crates/ov_cli/src/main.rs
+++ b/crates/ov_cli/src/main.rs
@@ -518,6 +518,20 @@ enum Commands {
#[arg(long, value_name = "seconds", help_heading = "Common options")]
timeout: Option<f64>,
},
+ /// [Data] Update explicit retrieval tags metadata for a file or directory
+ SetTags {
+ /// Viking URI
+ uri: String,
+ /// Comma-separated k=v tags, e.g. env=prod,team=search
+ #[arg(long = "tags", value_delimiter = ',')]
+ tags: Vec<String>,
+ /// Tag update mode: replace or append (append replaces existing values by key)
+ #[arg(long, default_value = "replace")]
+ mode: String,
+ /// Recursively update descendant files and semantic nodes when target is a directory
+ #[arg(long, default_value = "false")]
+ recursive: bool,
+ },
/// [Data] Download file to local path (supports binaries/images)
Get {
/// Viking URI
@@ -581,6 +595,9 @@ enum Commands {
help_heading = "Common options"
)]
context_type: Option<Vec<String>>,
+ /// Only include results matching any of these explicit tags
+ #[arg(long = "tags", value_delimiter = ',')]
+ tags: Option<Vec<String>>,
},
/// [Experimental][Data] Run context-aware retrieval
Search {
@@ -639,6 +656,9 @@ enum Commands {
help_heading = "Advanced options"
)]
context_type: Option<Vec<String>>,
+ /// Only include results matching any of these explicit tags
+ #[arg(long = "tags", value_delimiter = ',')]
+ tags: Option<Vec<String>>,
},
/// [Data] Run content pattern search
Grep {
@@ -2876,6 +2896,12 @@ async fn main() {
handlers::handle_write(uri, content, from_file, effective_mode, wait, timeout, ctx)
.await
}
+ Commands::SetTags {
+ uri,
+ tags,
+ 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
}
@@ -2889,6 +2915,7 @@ async fn main() {
before,
level,
context_type,
+ tags,
} => {
handlers::handle_find(
query,
@@ -2899,6 +2926,7 @@ async fn main() {
before,
level,
context_type,
+ tags,
ctx,
)
.await
@@ -2913,6 +2941,7 @@ async fn main() {
before,
level,
context_type,
+ tags,
} => {
handlers::handle_search(
query,
@@ -2924,6 +2953,7 @@ async fn main() {
before,
level,
context_type,
+ tags,
ctx,
)
.await
@@ -3057,6 +3087,14 @@ mod tests {
}
}
+ #[test]
+ fn cli_find_and_search_reject_removed_peer_id_flag() {
+ assert!(Cli::try_parse_from(["ov", "find", "invoice", "--peer-id", "peer-a"]).is_err());
+ assert!(
+ Cli::try_parse_from(["ov", "search", "invoice", "--peer-id", "peer-a"]).is_err()
+ );
+ }
+
#[test]
fn cli_chat_sender_uses_long_flag_and_session_keeps_short_s() {
Cli::command().debug_assert();
diff --git a/openviking/async_client.py b/openviking/async_client.py
index 53926b42c..1e3f373b2 100644
--- a/openviking/async_client.py
+++ b/openviking/async_client.py
@@ -391,6 +391,7 @@ class AsyncOpenViking:
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
since: Optional[str] = None,
until: Optional[str] = None,
@@ -421,6 +422,7 @@ class AsyncOpenViking:
score_threshold=score_threshold,
filter=filter,
context_type=context_type,
+ tags=tags,
telemetry=telemetry,
since=since,
until=until,
@@ -436,6 +438,7 @@ class AsyncOpenViking:
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
since: Optional[str] = None,
until: Optional[str] = None,
@@ -451,6 +454,7 @@ class AsyncOpenViking:
score_threshold=score_threshold,
filter=filter,
context_type=context_type,
+ tags=tags,
telemetry=telemetry,
since=since,
until=until,
@@ -495,6 +499,24 @@ class AsyncOpenViking:
telemetry=telemetry,
)
+ async def set_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ telemetry: TelemetryRequest = False,
+ ) -> Dict[str, Any]:
+ """Replace explicit retrieval tags for a file or directory."""
+ await self._ensure_initialized()
+ return await self._client.set_tags(
+ uri=uri,
+ tags=tags,
+ mode=mode,
+ recursive=recursive,
+ telemetry=telemetry,
+ )
+
async def ls(self, uri: str, **kwargs) -> List[Any]:
"""
List directory contents.
diff --git a/openviking/client/local.py b/openviking/client/local.py
index bca521406..2741a1cc5 100644
--- a/openviking/client/local.py
+++ b/openviking/client/local.py
@@ -17,6 +17,7 @@ from openviking.telemetry.execution import (
run_with_telemetry,
)
from openviking.utils.search_filters import SearchContextTypeInput, merge_search_filter
+from openviking.utils.tags import normalize_search_tags
from openviking_cli.client.base import BaseClient
from openviking_cli.exceptions import InvalidArgumentError, NotFoundError
from openviking_cli.session.user_id import UserIdentifier
@@ -41,15 +42,23 @@ def _resolve_search_filter(
since: Optional[str],
until: Optional[str],
time_field: Optional[str],
+ tags: Optional[List[str]] = None,
) -> Optional[Dict[str, Any]]:
"""Merge public retrieval filter shortcuts into the metadata filter."""
- return merge_search_filter(
+ merged = merge_search_filter(
filter,
context_type=context_type,
since=since,
until=until,
time_field=time_field,
)
+ normalized_tags = normalize_search_tags(tags)
+ if not normalized_tags:
+ return merged
+ tag_filter = {"op": "must", "field": "search_tags", "conds": normalized_tags}
+ if merged:
+ return {"op": "and", "conds": [merged, tag_filter]}
+ return tag_filter
class LocalClient(BaseClient):
@@ -316,6 +325,31 @@ class LocalClient(BaseClient):
execution.telemetry,
)
+ async def set_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ telemetry: TelemetryRequest = False,
+ ) -> Dict[str, Any]:
+ """Replace explicit retrieval tags for a file or directory."""
+ execution = await run_with_telemetry(
+ operation="content.set_tags",
+ telemetry=telemetry,
+ fn=lambda: self._service.fs.set_tags(
+ uri=uri,
+ tags=tags,
+ mode=mode,
+ recursive=recursive,
+ ctx=self._ctx,
+ ),
+ )
+ return attach_telemetry_payload(
+ execution.result,
+ execution.telemetry,
+ )
+
# ============= Search =============
async def find(
@@ -326,6 +360,7 @@ class LocalClient(BaseClient):
score_threshold: Optional[float] = None,
filter: Optional[Dict[str, Any]] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
since: Optional[str] = None,
until: Optional[str] = None,
@@ -333,7 +368,9 @@ class LocalClient(BaseClient):
level: Optional[List[int]] = None,
) -> Any:
"""Semantic search without session context."""
- resolved_filter = _resolve_search_filter(filter, context_type, since, until, time_field)
+ resolved_filter = _resolve_search_filter(
+ filter, context_type, since, until, time_field, tags
+ )
execution = await run_with_telemetry(
operation="search.find",
telemetry=telemetry,
@@ -361,6 +398,7 @@ class LocalClient(BaseClient):
score_threshold: Optional[float] = None,
filter: Optional[Dict[str, Any]] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
since: Optional[str] = None,
until: Optional[str] = None,
@@ -368,7 +406,9 @@ class LocalClient(BaseClient):
level: Optional[List[int]] = None,
) -> Any:
"""Semantic search with optional session context."""
- resolved_filter = _resolve_search_filter(filter, context_type, since, until, time_field)
+ resolved_filter = _resolve_search_filter(
+ filter, context_type, since, until, time_field, tags
+ )
async def _search():
session = None
diff --git a/openviking/server/routers/content.py b/openviking/server/routers/content.py
index 06aa25ad9..4ae591cd9 100644
--- a/openviking/server/routers/content.py
+++ b/openviking/server/routers/content.py
@@ -40,6 +40,18 @@ class WriteContentRequest(BaseModel):
telemetry: TelemetryRequest = False
+class SetTagsRequest(BaseModel):
+ """Request to set explicit k=v retrieval tags metadata for a file or directory."""
+
+ model_config = ConfigDict(extra="forbid")
+
+ uri: str
+ tags: list[str]
+ mode: str = "replace"
+ recursive: bool = False
+ telemetry: TelemetryRequest = False
+
+
class ReindexRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
@@ -198,6 +210,32 @@ async def write(
).model_dump(exclude_none=True)
[email protected]("/set_tags")
+async def set_tags(
+ request: SetTagsRequest = Body(...),
+ _ctx: RequestContext = Depends(get_request_context),
+):
+ """Set explicit k=v retrieval tags metadata for a file or directory."""
+ service = get_service()
+ uri = resolve_path_variables(request.uri)
+ execution = await run_operation(
+ operation="content.set_tags",
+ telemetry=request.telemetry,
+ fn=lambda: service.fs.set_tags(
+ uri=uri,
+ tags=request.tags,
+ mode=request.mode,
+ recursive=request.recursive,
+ ctx=_ctx,
+ ),
+ )
+ return Response(
+ status="ok",
+ result=execution.result,
+ telemetry=execution.telemetry,
+ ).model_dump(exclude_none=True)
+
+
@router.post("/reindex")
async def reindex(
body: ReindexRequest = Body(...),
diff --git a/openviking/server/routers/search.py b/openviking/server/routers/search.py
index 442c287ab..0e80c27d3 100644
--- a/openviking/server/routers/search.py
+++ b/openviking/server/routers/search.py
@@ -24,6 +24,7 @@ from openviking.utils.search_filters import (
_resolve_levels,
merge_search_filter,
)
+from openviking.utils.tags import normalize_search_tags
from openviking_cli.exceptions import InvalidArgumentError, NotFoundError
@@ -54,15 +55,27 @@ def _resolve_search_filter(
since: Optional[str],
until: Optional[str],
time_field: Optional[TimeField],
+ tags: Optional[List[str]],
) -> Optional[Dict[str, Any]]:
try:
- return merge_search_filter(
+ merged = merge_search_filter(
request_filter,
context_type=context_type,
since=since,
until=until,
time_field=time_field,
)
+ normalized_tags = normalize_search_tags(tags)
+ if not normalized_tags:
+ return merged
+ tag_filter: Dict[str, Any] = {
+ "op": "must",
+ "field": "search_tags",
+ "conds": normalized_tags,
+ }
+ if merged:
+ return {"op": "and", "conds": [merged, tag_filter]}
+ return tag_filter
except ValueError as exc:
raise InvalidArgumentError(str(exc)) from exc
@@ -104,6 +117,7 @@ class FindRequest(BaseModel):
score_threshold: Optional[float] = None
filter: Optional[Dict[str, Any]] = None
include_provenance: bool = False
+ tags: Optional[List[str]] = None
since: Optional[str] = None
until: Optional[str] = None
time_field: Optional[TimeField] = None
@@ -136,6 +150,7 @@ class SearchRequest(BaseModel):
score_threshold: Optional[float] = None
filter: Optional[Dict[str, Any]] = None
include_provenance: bool = False
+ tags: Optional[List[str]] = None
since: Optional[str] = None
until: Optional[str] = None
@@ -187,6 +202,7 @@ async def find(
request.since,
request.until,
request.time_field,
+ request.tags,
)
resolved_target_uri = _resolve_uri_or_uris(request.target_uri)
execution = await run_operation(
@@ -228,6 +244,7 @@ async def search(
request.since,
request.until,
request.time_field,
+ request.tags,
)
resolved_target_uri = _resolve_uri_or_uris(request.target_uri)
diff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.py
index e02ddfb72..92edbe3a8 100644
--- a/openviking/service/fs_service.py
+++ b/openviking/service/fs_service.py
@@ -507,3 +507,23 @@ class FSService:
wait=wait,
timeout=timeout,
)
+
+ async def set_tags(
+ self,
+ uri: str,
+ tags: list[str],
+ mode: str,
+ recursive: bool,
+ ctx: RequestContext,
+ ) -> Dict[str, Any]:
+ """Set explicit retrieval tags for a file or directory semantic nodes."""
+ uri = validate_viking_uri(uri)
+ viking_fs = self._ensure_initialized()
+ coordinator = ContentWriteCoordinator(viking_fs=viking_fs)
+ return await coordinator.set_tags(
+ uri=uri,
+ tags=tags,
+ mode=mode,
+ recursive=recursive,
+ ctx=ctx,
+ )
diff --git a/openviking/service/reindex_executor.py b/openviking/service/reindex_executor.py
index 611ca699a..199ec08b1 100644
--- a/openviking/service/reindex_executor.py
+++ b/openviking/service/reindex_executor.py
@@ -1247,6 +1247,14 @@ class ReindexExecutor:
) -> None:
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)
+ if (
+ existing
+ and existing.get("search_tags") is not None
+ and "search_tags" not in merged_meta
+ ):
+ merged_meta["search_tags"] = existing.get("search_tags")
context = Context(
uri=uri,
@@ -1258,7 +1266,7 @@ class ReindexExecutor:
user=ctx.user,
account_id=ctx.account_id,
owner_space=owner_space_for_uri(uri, ctx),
- meta=meta or {},
+ meta=merged_meta,
)
context.set_vectorize(Vectorize(text=vector_text))
msg = EmbeddingMsgConverter.from_context(context)
diff --git a/openviking/storage/collection_schemas.py b/openviking/storage/collection_schemas.py
index a0e3b574d..e8789df88 100644
--- a/openviking/storage/collection_schemas.py
+++ b/openviking/storage/collection_schemas.py
@@ -102,6 +102,7 @@ class CollectionSchemas:
{"FieldName": "name", "FieldType": "string"},
{"FieldName": "description", "FieldType": "string"},
{"FieldName": "tags", "FieldType": "string"},
+ {"FieldName": "search_tags", "FieldType": "list<string>"},
{"FieldName": "abstract", "FieldType": "string"},
{"FieldName": "account_id", "FieldType": "string"},
{"FieldName": "owner_user_id", "FieldType": "string"},
@@ -120,6 +121,7 @@ class CollectionSchemas:
"level",
"name",
"tags",
+ "search_tags",
"account_id",
"owner_user_id",
]
@@ -688,7 +690,12 @@ class TextEmbeddingHandler(DequeueHandlerBase):
user_id="default",
)
ctx = RequestContext(user=user, role=Role.ROOT)
- record_id = await self._vikingdb.upsert(inserted_data, ctx=ctx)
+ result = await self._vikingdb.upsert(
+ inserted_data,
+ ctx=ctx,
+ partial_update=True,
+ )
+ record_id = result
if record_id:
logger.debug(
f"Successfully wrote embedding to database: {record_id} abstract {inserted_data['abstract']} vector {inserted_data['vector'][:5]}"
diff --git a/openviking/storage/content_write.py b/openviking/storage/content_write.py
index 33cb8cf73..c46d26bc6 100644
--- a/openviking/storage/content_write.py
+++ b/openviking/storage/content_write.py
@@ -23,6 +23,7 @@ from openviking.storage.viking_fs import VikingFS
from openviking.telemetry import get_current_telemetry
from openviking.telemetry.request_wait_tracker import get_request_wait_tracker
from openviking.telemetry.resource_summary import build_queue_status_payload
+from openviking.utils.tags import normalize_search_tags
from openviking_cli.exceptions import (
AlreadyExistsError,
DeadlineExceededError,
@@ -109,6 +110,39 @@ class ContentWriteCoordinator:
telemetry_id=telemetry_id,
)
+ async def set_tags(
+ self,
+ *,
+ uri: str,
+ tags: list[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ ctx: RequestContext,
+ ) -> Dict[str, Any]:
+ try:
+ normalized_uri = canonicalize_uri(uri, ctx)
+ except NamespaceShapeError as exc:
+ raise InvalidArgumentError(str(exc)) from exc
+
+ self._validate_tag_mode(mode)
+ normalized_tags = normalize_search_tags(tags)
+ stat = await self._safe_stat(normalized_uri, ctx=ctx)
+ if stat.get("isDir"):
+ return await self._set_directory_tags(
+ uri=normalized_uri,
+ tags=normalized_tags,
+ mode=mode,
+ recursive=recursive,
+ ctx=ctx,
+ )
+ return await self._set_single_uri_tags(
+ uri=normalized_uri,
+ tags=normalized_tags,
+ mode=mode,
+ recursive=recursive,
+ ctx=ctx,
+ )
+
def _build_write_result(
self,
*,
@@ -143,6 +177,31 @@ class ContentWriteCoordinator:
result["overview_status"] = overview_status
return result
+ def _build_tags_result(
+ self,
+ *,
+ uri: str,
+ updated_uris: list[str],
+ skipped_count: int,
+ failed_count: int,
+ root_uri: str,
+ context_type: str,
+ tags: list[str],
+ mode: str,
+ ) -> Dict[str, Any]:
+ return {
+ "uri": uri,
+ "updated_uris": updated_uris,
+ "root_uri": root_uri,
+ "context_type": context_type,
+ "tags": tags,
+ "mode": mode,
+ "success_count": len(updated_uris),
+ "skipped_count": skipped_count,
+ "failed_count": failed_count,
+ "tags_updated": len(updated_uris) > 0,
+ }
+
def _refresh_statuses(
self,
*,
@@ -262,6 +321,10 @@ class ContentWriteCoordinator:
if mode not in {"replace", "append", "create"}:
raise InvalidArgumentError(f"unsupported write mode: {mode}")
+ def _validate_tag_mode(self, mode: str) -> None:
+ if mode not in {"replace", "append"}:
+ raise InvalidArgumentError(f"unsupported tag mode: {mode}")
+
def _validate_target_uri(self, uri: str) -> None:
name = uri.rstrip("/").split("/")[-1]
if name in _DERIVED_FILENAMES:
@@ -391,6 +454,7 @@ class ContentWriteCoordinator:
ctx: RequestContext,
change_type: str = "modified",
target_uri: str = "",
+ recursive: bool = False,
) -> None:
queue_manager = get_queue_manager()
semantic_queue = queue_manager.get_queue(queue_manager.SEMANTIC, allow_create=True)
@@ -399,9 +463,9 @@ class ContentWriteCoordinator:
uri=root_uri,
target_uri=target_uri,
context_type=context_type,
+ recursive=recursive,
account_id=ctx.account_id,
user_id=ctx.user.user_id,
- peer_id=ctx.user.user_id,
role=ctx.role.value,
skip_vectorization=False,
telemetry_id=telemetry.telemetry_id,
@@ -411,7 +475,6 @@ class ContentWriteCoordinator:
uri=root_uri,
account_id=ctx.account_id,
user_id=ctx.user.user_id,
- peer_id=ctx.user.user_id,
)
if context_type in {"resource", "skill"}
else ""
@@ -543,6 +606,173 @@ class ContentWriteCoordinator:
_, vector_status = self._refresh_statuses(wait=True, queue_status=queue_status)
return vector_status
+ async def _set_single_uri_tags(
+ self,
+ *,
+ uri: str,
+ tags: list[str],
+ mode: str,
+ recursive: bool,
+ ctx: RequestContext,
+ ) -> Dict[str, Any]:
+ del recursive
+ context_type = context_type_for_uri(uri)
+ root_uri = await self._resolve_root_uri(uri, ctx=ctx)
+ target_uri = uri
+ levels: list[int] | None = None
+ if uri.endswith("/.abstract.md"):
+ parent = VikingURI(uri).parent
+ if parent is not None:
+ target_uri = parent.uri.rstrip("/")
+ levels = [0]
+ elif uri.endswith("/.overview.md"):
+ parent = VikingURI(uri).parent
+ if parent is not None:
+ target_uri = parent.uri.rstrip("/")
+ levels = [1]
+ updated_uris = await self._upsert_uri_tags(
+ uri=target_uri,
+ tags=tags,
+ mode=mode,
+ ctx=ctx,
+ levels=levels,
+ )
+ if not updated_uris:
+ return self._build_tags_result(
+ uri=uri,
+ updated_uris=[],
+ skipped_count=1,
+ failed_count=0,
+ root_uri=root_uri,
+ context_type=context_type,
+ tags=tags,
+ mode=mode,
+ )
+ return self._build_tags_result(
+ uri=uri,
+ updated_uris=updated_uris,
+ skipped_count=0,
+ failed_count=0,
+ root_uri=root_uri,
+ context_type=context_type,
+ tags=tags,
+ mode=mode,
+ )
+
+ async def _set_directory_tags(
+ self,
+ *,
+ uri: str,
+ tags: list[str],
+ mode: str,
+ recursive: bool,
+ ctx: RequestContext,
+ ) -> Dict[str, Any]:
+ updated_targets = await self._collect_directory_tag_targets(
+ uri=uri, recursive=recursive, ctx=ctx
+ )
+
+ if not updated_targets:
+ raise NotFoundError(uri, "semantic file")
+
+ applied_uris: list[str] = []
+ skipped_count = 0
+ for target in updated_targets:
+ updated_uris = await self._upsert_uri_tags(
+ uri=target["uri"],
+ tags=tags,
+ mode=mode,
+ ctx=ctx,
+ levels=target.get("levels"),
+ )
+ if updated_uris:
+ applied_uris.extend(updated_uris)
+ else:
+ skipped_count += 1
+
+ context_type = context_type_for_uri(uri)
+ return self._build_tags_result(
+ uri=uri,
+ updated_uris=applied_uris,
+ skipped_count=skipped_count,
+ failed_count=0,
+ root_uri=uri,
+ context_type=context_type,
+ tags=tags,
+ mode=mode,
+ )
+
+ async def _collect_directory_tag_targets(
+ self,
+ *,
+ uri: str,
+ recursive: bool,
+ ctx: RequestContext,
+ ) -> list[dict[str, object]]:
+ if not recursive:
+ return [{"uri": uri.rstrip("/"), "levels": [0, 1]}]
+
+ entries = await self._viking_fs.tree(
+ uri,
+ ctx=ctx,
+ output="original",
+ show_all_hidden=True,
+ )
+
+ deduped: list[dict[str, object]] = []
+ seen: set[str] = set()
+ directory_levels: dict[str, set[int]] = {}
+ for entry in entries:
+ entry_uri = entry.get("uri", "")
+ if not entry_uri or is_watch_task_control_uri(entry_uri):
+ continue
+ normalized_uri = entry_uri.rstrip("/")
+ dedupe_key = f"dir:{normalized_uri}" if entry.get("isDir") else f"file:{normalized_uri}"
+ if dedupe_key in seen:
+ continue
+ seen.add(dedupe_key)
+ if entry.get("isDir"):
+ directory_levels.setdefault(normalized_uri, set()).update({0, 1})
+ elif normalized_uri.endswith("/.abstract.md"):
+ parent = VikingURI(normalized_uri).parent
+ if parent is not None:
+ directory_levels.setdefault(parent.uri.rstrip("/"), set()).add(0)
+ elif normalized_uri.endswith("/.overview.md"):
+ parent = VikingURI(normalized_uri).parent
+ if parent is not None:
+ directory_levels.setdefault(parent.uri.rstrip("/"), set()).add(1)
+ else:
+ deduped.append({"uri": normalized_uri})
+ for directory_uri, levels in directory_levels.items():
+ deduped.append({"uri": directory_uri, "levels": sorted(levels)})
+ return deduped
+
+ async def _upsert_uri_tags(
+ self,
+ *,
+ uri: str,
+ tags: list[str],
+ mode: str,
+ ctx: RequestContext,
+ levels: list[int] | None = None,
+ ) -> list[str]:
+ store = self._viking_fs._get_vector_store()
+ if not store:
+ raise RuntimeError("Vector store not initialized. Call OpenViking.initialize() first.")
+ if levels:
+ updated_records = await store.update_search_tags(
+ uri,
+ tags,
+ mode=mode,
+ levels=levels,
+ ctx=ctx,
+ )
+ return [str(record.get("uri")) for record in updated_records if record.get("uri")]
+ updated_records = await store.update_search_tags(uri, tags, mode=mode, ctx=ctx)
+ if not updated_records:
+ return []
+ return [str(record.get("uri")) for record in updated_records if record.get("uri")]
+
async def _resolve_root_uri(
self,
uri: str,
@@ -560,20 +790,25 @@ class ContentWriteCoordinator:
if len(parts) >= 2:
root_uri = VikingURI.build("resources", parts[1])
elif parts[0] == "user":
- try:
- memories_idx = parts.index("memories")
- except ValueError as exc:
- raise InvalidArgumentError(
- f"write only supports memory files under user scope: {uri}"
- ) from exc
- if len(parts) <= memories_idx + 1:
- raise InvalidArgumentError(
- f"memory write target must be inside a memory type directory: {uri}"
- )
- parent = VikingURI(uri).parent
- if parent is None:
- raise InvalidArgumentError(f"could not resolve write root for {uri}")
- root_uri = parent.uri
+ if "resources" in parts:
+ resources_idx = parts.index("resources")
+ if len(parts) <= resources_idx + 1:
+ raise InvalidArgumentError(
+ f"resource write target must be inside a resource directory: {uri}"
+ )
+ root_uri = VikingURI.build(*parts[: resources_idx + 2])
+ else:
+ try:
+ memories_idx = parts.index("memories")
+ except ValueError as exc:
+ raise InvalidArgumentError(
+ f"write only supports memory or resource files under user scope: {uri}"
+ ) from exc
+ if len(parts) <= memories_idx + 1:
+ raise InvalidArgumentError(
+ f"memory write target must be inside a memory type directory: {uri}"
+ )
+ root_uri = VikingURI.build(*parts[: memories_idx + 2])
stat = await self._safe_stat(root_uri, ctx=ctx, allow_not_found=_allow_not_found)
if stat.get("not_found") or not stat.get("isDir"):
diff --git a/openviking/storage/vectordb/collection/collection.py b/openviking/storage/vectordb/collection/collection.py
index 4d50cdafe..0104ae304 100644
--- a/openviking/storage/vectordb/collection/collection.py
+++ b/openviking/storage/vectordb/collection/collection.py
@@ -151,6 +151,10 @@ class ICollection(ABC):
def upsert_data(self, data_list: List[Dict[str, Any]], ttl=0):
raise NotImplementedError
+ @abstractmethod
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ raise NotImplementedError
+
@abstractmethod
def fetch_data(self, primary_keys: List[Any]):
raise NotImplementedError
@@ -579,6 +583,21 @@ class Collection:
raise RuntimeError("Collection is closed")
return self.__collection.upsert_data(data_list, ttl)
+ def update_data(self, data_list: List[Dict[str, Any]]) -> Any:
+ """
+ Update existing data in the collection using only explicitly provided fields.
+
+ Args:
+ data_list (List[Dict[str, Any]]): List of partial data documents to update.
+ Each document must contain the primary key.
+
+ Returns:
+ Any: Implementation-specific result for the updated records.
+ """
+ if self.__collection is None:
+ raise RuntimeError("Collection is closed")
+ return self.__collection.update_data(data_list)
+
def fetch_data(self, primary_keys: List[Any]) -> List[Dict[str, Any]]:
"""
Retrieve data documents from the collection using their primary keys.
diff --git a/openviking/storage/vectordb/collection/http_collection.py b/openviking/storage/vectordb/collection/http_collection.py
index 530a7af6c..29516e909 100644
--- a/openviking/storage/vectordb/collection/http_collection.py
+++ b/openviking/storage/vectordb/collection/http_collection.py
@@ -279,6 +279,23 @@ class HttpCollection(ICollection):
result = json.loads(response.text)
return result.get("data", [])
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ url = self.url_prefix + "api/vikingdb/data/update"
+ response = requests.post(
+ url,
+ headers=headers,
+ json={
+ "project": self.project_name,
+ "collection_name": self.collection_name,
+ "fields": json.dumps(data_list),
+ },
+ timeout=DEFAULT_TIMEOUT,
+ )
+ if response.status_code != 200:
+ return []
+ result = json.loads(response.text)
+ return result.get("data", [])
+
def fetch_data(self, primary_keys: List[Any]) -> FetchDataInCollectionResult:
url = self.url_prefix + "api/vikingdb/data/fetch_in_collection"
response = requests.get(
diff --git a/openviking/storage/vectordb/collection/local_collection.py b/openviking/storage/vectordb/collection/local_collection.py
index ff7c0146a..2c32e9d48 100644
--- a/openviking/storage/vectordb/collection/local_collection.py
+++ b/openviking/storage/vectordb/collection/local_collection.py
@@ -541,20 +541,52 @@ class LocalCollection(ICollection):
# data interface
def upsert_data(self, raw_data_list: List[Dict[str, Any]], ttl=0):
- result = UpsertDataResult()
- data_list = []
+ data_list = self._validate_raw_data_list(raw_data_list)
+ return self._write_data_list(data_list, ttl=ttl)
+
+ def update_data(self, raw_data_list: List[Dict[str, Any]]):
+ if not raw_data_list:
+ return UpsertDataResult()
+
+ pk = self.meta.primary_key
+ primary_keys = []
+ for raw_data in raw_data_list:
+ if pk not in raw_data:
+ raise ValueError(f"primary key '{pk}' is required for update")
+ primary_keys.append(raw_data[pk])
+
+ existing = self.fetch_data(primary_keys)
+ existing_map = {item.id: item.fields for item in existing.items}
+ missing_ids = [key for key in primary_keys if key not in existing_map]
+ if missing_ids:
+ raise ValueError(f"record not found for primary key(s): {missing_ids}")
+ merged_list = []
+ for raw_data in raw_data_list:
+ existing_fields = existing_map[raw_data[pk]] or {}
+ merged = dict(existing_fields)
+ merged.update(raw_data)
+ merged_list.append(merged)
+
+ processed_list = self._validate_raw_data_list(merged_list)
+ return self._write_data_list(processed_list, ttl=0)
+
+ def _validate_raw_data_list(self, raw_data_list: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
+ data_list = []
for raw_data in raw_data_list:
if self.data_processor:
try:
data = self.data_processor.validate_and_process(raw_data)
except ValueError as e:
logger.error(f"Data validation failed: {e}, raw_data: {raw_data}")
- return result
+ raise
else:
- # Should not happen given init logic, but for safety
data = raw_data
data_list.append(data)
+ return data_list
+
+ def _write_data_list(self, data_list: List[Dict[str, Any]], ttl=0):
+ result = UpsertDataResult()
dense_emb, sparse_emb = (
self.vectorizer_adapter.vectorize_raw_data(data_list)
diff --git a/openviking/storage/vectordb/collection/qdrant_collection.py b/openviking/storage/vectordb/collection/qdrant_collection.py
index 7651cf3ad..375c40825 100644
--- a/openviking/storage/vectordb/collection/qdrant_collection.py
+++ b/openviking/storage/vectordb/collection/qdrant_collection.py
@@ -117,9 +117,7 @@ def _sparse_to_qdrant(sparse_vector: Optional[Dict[str, float]]) -> Optional[Dic
hashed_terms[idx] = str(raw_term)
merged[idx] = merged.get(idx, 0.0) + weight
if collisions:
- sample = ", ".join(
- f"{left!r}<->{right!r}@{idx}" for left, right, idx in collisions[:3]
- )
+ sample = ", ".join(f"{left!r}<->{right!r}@{idx}" for left, right, idx in collisions[:3])
logger.warning(
"Qdrant sparse term hash collision detected; colliding terms are merged into the same sparse index bucket. count=%s sample=%s",
len(collisions),
@@ -135,7 +133,9 @@ def _sparse_to_qdrant(sparse_vector: Optional[Dict[str, float]]) -> Optional[Dic
class QdrantIndex(IIndex):
"""Lightweight logical index facade backed by Qdrant payload/vector config."""
- def __init__(self, collection: "QdrantCollection", index_name: str, meta: Dict[str, Any]) -> None:
+ def __init__(
+ self, collection: "QdrantCollection", index_name: str, meta: Dict[str, Any]
+ ) -> None:
super().__init__(meta=meta)
self._collection = collection
self._index_name = index_name
@@ -160,7 +160,9 @@ class QdrantIndex(IIndex):
def aggregate(self, filters: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
raise NotImplementedError("QdrantIndex.aggregate is not exposed via raw index interface")
- def update(self, scalar_index: Optional[Dict[str, Any]] = None, description: Optional[str] = None):
+ def update(
+ self, scalar_index: Optional[Dict[str, Any]] = None, description: Optional[str] = None
+ ):
self._collection.update_index(
index_name=self._index_name,
scalar_index=scalar_index,
@@ -629,7 +631,9 @@ class QdrantCollection(ICollection):
field_type_map = self._field_type_map()
scalar_fields = list(meta_data.get("ScalarIndex", []) or [])
for field_name in scalar_fields:
- field_schema = self._payload_field_schema(field_name, field_type_map.get(field_name, ""))
+ field_schema = self._payload_field_schema(
+ field_name, field_type_map.get(field_name, "")
+ )
if not field_schema:
continue
self._client.request(
@@ -804,9 +808,7 @@ class QdrantCollection(ICollection):
points = points[offset:]
if len(points) > limit:
points = points[:limit]
- return SearchResult(
- data=self._parse_points_as_search_result(points, score=1.0).data
- )
+ return SearchResult(data=self._parse_points_as_search_result(points, score=1.0).data)
def search_by_id(
self,
@@ -938,9 +940,7 @@ class QdrantCollection(ICollection):
points = points[offset:]
if len(points) > limit:
points = points[:limit]
- return SearchResult(
- data=self._parse_points_as_search_result(points, score=1.0).data
- )
+ return SearchResult(data=self._parse_points_as_search_result(points, score=1.0).data)
def search_by_scalar(
self,
@@ -1028,7 +1028,9 @@ class QdrantCollection(ICollection):
for field_name in new_scalar_fields:
if field_name in old_scalar_fields:
continue
- field_schema = self._payload_field_schema(field_name, field_type_map.get(field_name, ""))
+ field_schema = self._payload_field_schema(
+ field_name, field_type_map.get(field_name, "")
+ )
if not field_schema:
continue
self._client.request(
@@ -1054,7 +1056,9 @@ class QdrantCollection(ICollection):
return current_meta
def get_index_meta_data(self, index_name: str):
- return self._meta_store.get_index_meta(collection_key=self.collection_key, index_name=index_name)
+ return self._meta_store.get_index_meta(
+ collection_key=self.collection_key, index_name=index_name
+ )
def list_indexes(self):
return self._meta_store.list_indexes(collection_key=self.collection_key)
@@ -1063,7 +1067,9 @@ class QdrantCollection(ICollection):
index_meta = self.get_index_meta_data(index_name) or {}
for field_name in index_meta.get("ScalarIndex", []) or []:
self._delete_field_index(field_name)
- self._meta_store.delete_index_meta(collection_key=self.collection_key, index_name=index_name)
+ self._meta_store.delete_index_meta(
+ collection_key=self.collection_key, index_name=index_name
+ )
def upsert_data(self, data_list: List[Dict[str, Any]], ttl=0):
# Qdrant does not provide a collection-level per-point TTL primitive that
@@ -1079,6 +1085,54 @@ class QdrantCollection(ICollection):
)
return [data.get("id") for data in data_list]
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ updated_records: List[Dict[str, Any]] = []
+ updated_ids: List[Any] = []
+
+ for raw_data in data_list:
+ if "id" not in raw_data or raw_data.get("id") in (None, ""):
+ raise ValueError("Qdrant update requires id")
+
+ original_id = raw_data.get("id")
+ response = self._client.request(
+ "POST",
+ f"/collections/{self._physical_collection_name}/points",
+ json_body={
+ "ids": [_to_qdrant_point_id(original_id)],
+ "with_payload": True,
+ "with_vector": True,
+ },
+ )
+ points = _extract_points(response)
+ if not points:
+ raise ValueError(f"Qdrant point does not exist for update: {original_id}")
+
+ point = points[0]
+ existing_id, existing_payload = self._point_payload_for_read(point)
+ dense_vector, sparse_vector = self._extract_named_vectors(point)
+
+ merged_record: Dict[str, Any] = {"id": existing_id, **existing_payload}
+ if dense_vector is not None:
+ merged_record[self._dense_vector_name] = dense_vector
+ if sparse_vector is not None:
+ merged_record[self._sparse_vector_name] = sparse_vector
+
+ for field_name, field_value in raw_data.items():
+ merged_record[field_name] = field_value
+
+ updated_records.append(merged_record)
+ updated_ids.append(existing_id)
+
+ points = [self._make_point(data) for data in updated_records]
+ self._client.request(
+ "PUT",
+ f"/collections/{self._physical_collection_name}/points",
+ json_body={"points": points},
+ params={"wait": "true"},
+ expected_statuses=(200, 202),
+ )
+ return updated_ids
+
def fetch_data(self, primary_keys: List[Any]):
if not primary_keys:
return FetchDataInCollectionResult()
diff --git a/openviking/storage/vectordb/collection/result.py b/openviking/storage/vectordb/collection/result.py
index d796c70d0..7078fb6c1 100644
--- a/openviking/storage/vectordb/collection/result.py
+++ b/openviking/storage/vectordb/collection/result.py
@@ -9,6 +9,15 @@ class UpsertDataResult:
ids: List[Any] = field(default_factory=list)
+@dataclass
+class UpdateResult:
+ ok: bool = False
+ ids: List[str] = field(default_factory=list)
+ updated_count: int = 0
+ error_code: Optional[str] = None
+ error_message: Optional[str] = None
+
+
@dataclass
class DataItem:
id: Any = None
diff --git a/openviking/storage/vectordb/collection/vikingdb_collection.py b/openviking/storage/vectordb/collection/vikingdb_collection.py
index 94577e27c..2bab31a8e 100644
--- a/openviking/storage/vectordb/collection/vikingdb_collection.py
+++ b/openviking/storage/vectordb/collection/vikingdb_collection.py
@@ -21,6 +21,9 @@ from openviking_cli.utils.logger import default_logger as logger
class VikingDBCollection(ICollection):
"""
VikingDB collection implementation for private deployment.
+
+ Data-plane operations in this class target the VikingDB V2 API surface
+ under /api/vikingdb/data/*.
"""
def __init__(
@@ -158,6 +161,15 @@ class VikingDBCollection(ICollection):
}
return self._data_post(path, data)
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ path = "/api/vikingdb/data/update"
+ data = {
+ "project": self.project_name,
+ "collection_name": self.collection_name,
+ "data": data_list,
+ }
+ return self._data_post(path, data)
+
def fetch_data(self, primary_keys: List[Any]) -> FetchDataInCollectionResult:
path = "/api/vikingdb/data/fetch_in_collection"
data = {
diff --git a/openviking/storage/vectordb/collection/volcengine_api_key_collection.py b/openviking/storage/vectordb/collection/volcengine_api_key_collection.py
index 975fc4c6a..a0d0dfe23 100644
--- a/openviking/storage/vectordb/collection/volcengine_api_key_collection.py
+++ b/openviking/storage/vectordb/collection/volcengine_api_key_collection.py
@@ -300,6 +300,14 @@ class VolcengineApiKeyCollection(ICollection):
}
return self._data_post(path, data)
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ path = "/api/vikingdb/data/update"
+ data = {
+ **self._base_data_payload(),
+ "data": data_list,
+ }
+ return self._data_post(path, data)
+
def fetch_data(self, primary_keys: List[Any]) -> FetchDataInCollectionResult:
path = "/api/vikingdb/data/fetch_in_collection"
data = {
diff --git a/openviking/storage/vectordb/collection/volcengine_collection.py b/openviking/storage/vectordb/collection/volcengine_collection.py
index 45e58aef8..853381aef 100644
--- a/openviking/storage/vectordb/collection/volcengine_collection.py
+++ b/openviking/storage/vectordb/collection/volcengine_collection.py
@@ -395,6 +395,15 @@ class VolcengineCollection(ICollection):
}
return self._data_post(path, data)
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ path = "/api/vikingdb/data/update"
+ data = {
+ "project": self.project_name,
+ "collection_name": self.collection_name,
+ "data": data_list,
+ }
+ return self._data_post(path, data)
+
def fetch_data(self, primary_keys: List[Any]) -> FetchDataInCollectionResult:
path = "/api/vikingdb/data/fetch_in_collection"
data = {
diff --git a/openviking/storage/vectordb/service/api_fastapi.py b/openviking/storage/vectordb/service/api_fastapi.py
index 95b20379b..c4c16deb6 100644
--- a/openviking/storage/vectordb/service/api_fastapi.py
+++ b/openviking/storage/vectordb/service/api_fastapi.py
@@ -246,6 +246,20 @@ async def upsert_data(request: DataUpsertRequest, req: Request):
return error_response(e.message, e.code.value, request=req)
+@data_router.post("/update", response_model=ApiResponse)
+async def update_data(request: DataUpsertRequest, req: Request):
+ """Update existing data in collection using only explicitly provided fields."""
+ try:
+ collection = get_collection_or_raise(request.collection_name, request.project or "default")
+ data_list = data_utils.convert_dict(request.fields)
+
+ logger.debug(f"Updating {len(data_list)} records in {request.collection_name}")
+ result = collection.update_data(data_list=data_list)
+ return success_response("update data success", result, request=req)
+ except VikingDBException as e:
+ return error_response(e.message, e.code.value, request=req)
+
+
@data_router.get("/fetch_in_collection", response_model=ApiResponse)
async def fetch_data(
req: Request,
diff --git a/openviking/storage/vectordb/utils/data_processor.py b/openviking/storage/vectordb/utils/data_processor.py
index 109eb0d40..bea3e5de8 100644
--- a/openviking/storage/vectordb/utils/data_processor.py
+++ b/openviking/storage/vectordb/utils/data_processor.py
@@ -57,8 +57,8 @@ class DataProcessor:
"float32": "float32",
"string": "string",
"bool": "bool",
- "list<string>": "string",
- "list<int64>": "string",
+ "list<string>": "list<string>",
+ "list<int64>": "list<int64>",
"vector": None,
"sparse_vector": None,
"text": "string",
diff --git a/openviking/storage/vectordb_adapters/local_adapter.py b/openviking/storage/vectordb_adapters/local_adapter.py
index f8f7e18d5..4bef6eb95 100644
--- a/openviking/storage/vectordb_adapters/local_adapter.py
+++ b/openviking/storage/vectordb_adapters/local_adapter.py
@@ -6,7 +6,7 @@ from __future__ import annotations
import os
from pathlib import Path
-from typing import Any, Dict
+from typing import Any, Dict, List
from openviking.storage.vectordb.collection.collection import Collection
from openviking.storage.vectordb.collection.local_collection import get_or_create_local_collection
@@ -55,3 +55,8 @@ class LocalCollectionAdapter(CollectionAdapter):
if collection_path:
os.makedirs(collection_path, exist_ok=True)
return get_or_create_local_collection(meta_data=meta, path=collection_path)
+
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ collection = self.get_collection()
+ result = collection.update_data(data_list)
+ return list(result.ids or [])
diff --git a/openviking/storage/vectordb_adapters/volcengine_adapter.py b/openviking/storage/vectordb_adapters/volcengine_adapter.py
index ad6caf4f8..9128b5097 100644
--- a/openviking/storage/vectordb_adapters/volcengine_adapter.py
+++ b/openviking/storage/vectordb_adapters/volcengine_adapter.py
@@ -4,7 +4,7 @@
from __future__ import annotations
-from typing import Any, Dict
+from typing import Any, Dict, List
from openviking.storage.vectordb.collection.collection import Collection
from openviking.storage.vectordb.collection.volcengine_api_key_collection import (
@@ -177,3 +177,25 @@ class VolcengineCollectionAdapter(CollectionAdapter):
def _normalize_record_for_read(self, record: Dict[str, Any]) -> Dict[str, Any]:
return super()._normalize_record_for_read(record)
+
+ def update_data(self, data_list: List[Dict[str, Any]]):
+ collection = self.get_collection()
+ result = collection.update_data(data_list)
+ if isinstance(result, dict):
+ updated = result.get("updated")
+ primary_keys = result.get("primary_keys")
+ if isinstance(primary_keys, list):
+ return list(primary_keys)
+ ids = result.get("ids")
+ if isinstance(ids, list):
+ return list(ids)
+ if updated == 0:
+ return []
+ if isinstance(updated, int) and updated > 0:
+ fallback_ids = [item.get("id") for item in data_list if item.get("id") is not None]
+ return [str(item) for item in fallback_ids]
+ return []
+ if isinstance(result, list):
+ return [str(item) for item in result if item is not None]
+ fallback_ids = [item.get("id") for item in data_list if item.get("id") is not None]
+ return [str(item) for item in fallback_ids]
diff --git a/openviking/storage/viking_vector_index_backend.py b/openviking/storage/viking_vector_index_backend.py
index 344039be6..13440e346 100644
--- a/openviking/storage/viking_vector_index_backend.py
+++ b/openviking/storage/viking_vector_index_backend.py
@@ -12,6 +12,7 @@ from openviking.core.namespace import canonicalize_uri, visible_roots
from openviking.server.identity import RequestContext, Role
from openviking.storage.expr import And, Eq, FilterExpr, In, Or, PathScope, RawDSL
from openviking.storage.vectordb.collection.collection import Collection
+from openviking.storage.vectordb.collection.result import UpdateResult
from openviking.storage.vectordb.utils.logging_init import init_cpp_logging
from openviking.storage.vectordb_adapters import create_collection_adapter
from openviking_cli.utils import get_logger
@@ -26,6 +27,7 @@ RETRIEVAL_OUTPUT_FIELDS = [
"abstract",
"active_count",
"updated_at",
+ "search_tags",
]
LOOKUP_OUTPUT_FIELDS = [
@@ -47,6 +49,7 @@ MEMORY_DEDUP_OUTPUT_FIELDS = [
]
FETCH_BY_URI_OUTPUT_FIELDS = [
+ "id",
"uri",
"type",
"context_type",
@@ -57,6 +60,7 @@ FETCH_BY_URI_OUTPUT_FIELDS = [
"name",
"description",
"tags",
+ "search_tags",
"abstract",
"account_id",
"owner_user_id",
@@ -155,6 +159,11 @@ class _SingleAccountBackend:
filtered = self._filter_known_fields(payload)
return {k: v for k, v in filtered.items() if v is not None}
+ @staticmethod
+ def _is_not_found_error(exc: Exception) -> bool:
+ message = str(exc).lower()
+ return "not found" in message or "does not exist" in message
+
async def _refresh_meta_data_async(self) -> None:
self._meta_data_cache = await self._async_adapter.collection_meta()
@@ -235,12 +244,11 @@ class _SingleAccountBackend:
# Data Operations (with tenant enforcement)
# =========================================================================
- async def upsert(self, data: Dict[str, Any]) -> str:
+ async def upsert(self, data: Dict[str, Any], partial_update: bool = False) -> str:
payload = dict(data)
logger.debug(
f"[_SingleAccountBackend.upsert] Input data.account_id={payload.get('account_id')}, bound_account_id={self._bound_account_id}"
)
-
if self._bound_account_id and not payload.get("account_id"):
payload["account_id"] = self._bound_account_id
logger.debug(
@@ -259,10 +267,79 @@ class _SingleAccountBackend:
if not payload.get("id"):
payload["id"] = str(uuid.uuid4())
+ if partial_update:
+ try:
+ existing_records = await self._async_adapter.call("get", [payload["id"]])
+ if self._bound_account_id:
+ existing_records = [
+ record
+ for record in existing_records
+ if record.get("account_id") == self._bound_account_id
+ ]
+ except Exception as e:
+ logger.error("Error reading existing record before partial update: %s", e)
+ return ""
+
+ if existing_records:
+ existing = dict(existing_records[0])
+ existing.update({k: v for k, v in payload.items() if v is not None})
+ payload = existing
+
payload = await self._async_adapter.run(self._prepare_upsert_payload, payload)
ids = await self._async_adapter.call("upsert", payload)
return ids[0] if ids else ""
+ async def update(self, data: Dict[str, Any]) -> UpdateResult:
+ """Strict update path. The target record must already exist."""
+ try:
+ payload = dict(data)
+ logger.debug(
+ f"[_SingleAccountBackend.update] Input data.account_id={payload.get('account_id')}, bound_account_id={self._bound_account_id}"
+ )
+
+ if self._bound_account_id and not payload.get("account_id"):
+ payload["account_id"] = self._bound_account_id
+
+ if not payload.get("id"):
+ raise ValueError("id is required for update")
+
+ context_type = payload.get("context_type")
+ if context_type and context_type not in VikingVectorIndexBackend.ALLOWED_CONTEXT_TYPES:
+ allowed = sorted(VikingVectorIndexBackend.ALLOWED_CONTEXT_TYPES)
+ raise ValueError(f"Invalid context_type: {context_type}. Must be one of {allowed}")
+
+ payload = await self._async_adapter.run(self._prepare_upsert_payload, payload)
+ ids = await self._async_adapter.call("update_data", [payload])
+ normalized_ids = [str(item) for item in (ids or []) if item is not None]
+ return UpdateResult(
+ ok=bool(normalized_ids),
+ ids=normalized_ids,
+ updated_count=len(normalized_ids),
+ error_code=None if normalized_ids else "UPDATE_FAILED",
+ error_message=None
+ if normalized_ids
+ else "update completed without any updated ids",
+ )
+ except ValueError as e:
+ message = str(e)
+ error_code = "NOT_FOUND" if "not found" in message.lower() else "INVALID_ARGUMENT"
+ return UpdateResult(
+ ok=False,
+ ids=[],
+ updated_count=0,
+ error_code=error_code,
+ error_message=message,
+ )
+ except Exception as e:
+ logger.error("Error updating record: %s", e)
+ return UpdateResult(
+ ok=False,
+ ids=[],
+ updated_count=0,
+ error_code="UPDATE_FAILED",
+ error_message=str(e),
+ )
+
async def get(self, ids: List[str]) -> List[Dict[str, Any]]:
try:
records = await self._async_adapter.call("get", ids)
@@ -627,16 +704,40 @@ class VikingVectorIndexBackend:
# 公开数据操作 API(强制要求 ctx)
# =========================================================================
- async def upsert(self, data: Dict[str, Any], *, ctx: RequestContext) -> str:
+ async def upsert(
+ self, data: Dict[str, Any], *, ctx: RequestContext, partial_update: bool = False
+ ) -> str:
+ """Main write entrypoint.
+
+ With the default ``partial_update=False``, this preserves the legacy
+ full-record upsert behavior. When ``partial_update=True``, the backend
+ first reads the current record and preserves unspecified existing
+ fields before issuing the final upsert.
+ """
logger.debug(
- f"[VikingVectorIndexBackend.upsert] Called with ctx.account_id={ctx.account_id}, data={data}"
+ f"[VikingVectorIndexBackend.upsert] Called with ctx.account_id={ctx.account_id}, partial_update={partial_update}, data={data}"
)
backend = self._get_backend_for_context(ctx)
logger.debug(
f"[VikingVectorIndexBackend.upsert] Using backend for account_id={ctx.account_id}"
)
- result = await backend.upsert(data)
- logger.debug(f"[VikingVectorIndexBackend.upsert] Completed, result={result}")
+ result = await backend.upsert(data, partial_update=partial_update)
+ logger.debug(
+ f"[VikingVectorIndexBackend.upsert] Completed with partial_update={partial_update}, result={result}"
+ )
+ return result
+
+ async def update(self, data: Dict[str, Any], *, ctx: RequestContext) -> UpdateResult:
+ """Strict update path. The target record must already exist."""
+ logger.debug(
+ f"[VikingVectorIndexBackend.update] Called with ctx.account_id={ctx.account_id}, data={data}"
+ )
+ backend = self._get_backend_for_context(ctx)
+ logger.debug(
+ f"[VikingVectorIndexBackend.update] Using backend for account_id={ctx.account_id}"
+ )
+ result = await backend.update(data)
+ logger.debug(f"[VikingVectorIndexBackend.update] Completed, result={result}")
return result
async def get(self, ids: List[str], *, ctx: RequestContext) -> List[Dict[str, Any]]:
@@ -655,6 +756,116 @@ class VikingVectorIndexBackend:
backend = self._get_backend_for_context(ctx)
return await backend.fetch_by_uri(uri)
+ async def update_search_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ *,
+ mode: str,
+ levels: Optional[List[int]] = None,
+ ctx: RequestContext,
+ ) -> List[Dict[str, Any]]:
+ """Update search tags for the exact indexed record or directory summary records."""
+ if mode not in {"replace", "append"}:
+ raise ValueError(f"unsupported tag mode: {mode}")
+
+ from openviking.utils.tags import merge_search_tags
+
+ canonical_uri = canonicalize_uri(uri, ctx)
+ if levels is None:
+ record = await self.fetch_by_uri(canonical_uri, ctx=ctx)
+ if not record or not record.get("id"):
+ return []
+
+ full_records = await self.get([str(record["id"])], ctx=ctx)
+ if not full_records:
+ logger.warning(
+ "update_search_tags failed to fetch full exact record uri=%s account_id=%s id=%s",
+ canonical_uri,
+ ctx.account_id,
+ record.get("id"),
+ )
+ return []
+
+ updated_record = dict(full_records[0])
+ try:
+ if mode == "append":
+ updated_record["search_tags"] = merge_search_tags(
+ updated_record.get("search_tags"), tags
+ )
+ else:
+ updated_record["search_tags"] = list(tags)
+ except Exception as exc:
+ logger.warning(
+ "update_search_tags failed to merge exact record tags uri=%s "
+ "account_id=%s existing_tags=%s incoming_tags=%s error=%s",
+ canonical_uri,
+ ctx.account_id,
+ updated_record.get("search_tags"),
+ tags,
+ exc,
+ )
+ return []
+
+ if await self.upsert(updated_record, ctx=ctx):
+ return [updated_record]
+ return []
+
+ records = await self.filter(
+ filter=And([Eq("uri", canonical_uri), In("level", levels)]),
+ limit=max(len(levels), 2),
+ output_fields=FETCH_BY_URI_OUTPUT_FIELDS,
+ ctx=ctx,
+ )
+ if not records:
+ return []
+
+ record_ids = [str(record["id"]) for record in records if record.get("id")]
+ if not record_ids:
+ return []
+ full_records = await self.get(record_ids, ctx=ctx)
+ full_records_by_id = {
+ str(record["id"]): record for record in full_records if record.get("id") is not None
+ }
+
+ updated_records: List[Dict[str, Any]] = []
+ for record in records:
+ if not record or not record.get("id"):
+ continue
+ full_record = full_records_by_id.get(str(record["id"]))
+ if not full_record:
+ logger.warning(
+ "update_search_tags failed to fetch full leveled record uri=%s account_id=%s level=%s id=%s",
+ canonical_uri,
+ ctx.account_id,
+ record.get("level"),
+ record.get("id"),
+ )
+ continue
+ updated_record = dict(full_record)
+ try:
+ if mode == "append":
+ updated_record["search_tags"] = merge_search_tags(
+ updated_record.get("search_tags"), tags
+ )
+ else:
+ updated_record["search_tags"] = list(tags)
+ except Exception as exc:
+ logger.warning(
+ "update_search_tags failed to merge leveled record tags uri=%s "
+ "account_id=%s level=%s existing_tags=%s incoming_tags=%s error=%s",
+ canonical_uri,
+ ctx.account_id,
+ updated_record.get("level"),
+ updated_record.get("search_tags"),
+ tags,
+ exc,
+ )
+ return []
+ if await self.upsert(updated_record, ctx=ctx):
+ updated_records.append(updated_record)
+ return updated_records
+
async def query(
self,
query_vector: Optional[List[float]] = None,
@@ -1047,7 +1258,8 @@ class VikingVectorIndexBackend:
record.get("id"),
)
continue
- if await self.upsert(updated, ctx=ctx):
+ result = await self.upsert(updated, ctx=ctx)
+ if result:
success = True
old_id = record.get("id")
if old_id and old_id != new_id:
@@ -1072,8 +1284,8 @@ class VikingVectorIndexBackend:
uri_updated = False
for record in full_records:
current = int(record.get("active_count", 0) or 0)
- record["active_count"] = current + 1
- if await self.upsert(record, ctx=ctx):
+ result = await self.upsert(record | {"active_count": current + 1}, ctx=ctx)
+ if result:
uri_updated = True
if uri_updated:
updated += 1
diff --git a/openviking/storage/vikingdb_manager.py b/openviking/storage/vikingdb_manager.py
index 5b03b13d1..5701403b8 100644
--- a/openviking/storage/vikingdb_manager.py
+++ b/openviking/storage/vikingdb_manager.py
@@ -182,8 +182,8 @@ class VikingDBManagerProxy:
manager = VikingDBManager(...)
proxy = VikingDBManagerProxy(manager, ctx)
- # 使用(无需传 ctx,API 完全兼容)
- await proxy.upsert(data)
+ # 使用(无需传 ctx;仅在需要保留未显式传入字段时开启 partial_update)
+ await proxy.upsert(data, partial_update=True)
results = await proxy.search_similar_memories(...)
```
"""
@@ -286,8 +286,14 @@ class VikingDBManagerProxy:
# 数据操作 API(自动携带 ctx)
# =========================================================================
- async def upsert(self, data: Dict[str, Any]) -> str:
- return await self._manager.upsert(data, ctx=self._ctx)
+ async def upsert(self, data: Dict[str, Any], partial_update: bool = False):
+ """Bound write entrypoint.
+
+ ``partial_update=False`` keeps the legacy full-record upsert semantics.
+ ``partial_update=True`` reads the current record first and preserves
+ fields that are omitted from ``data`` before writing.
+ """
+ return await self._manager.upsert(data, ctx=self._ctx, partial_update=partial_update)
async def get(self, ids: List[str]) -> List[Dict[str, Any]]:
return await self._manager.get(ids, ctx=self._ctx)
diff --git a/openviking/sync_client.py b/openviking/sync_client.py
index 2d04fb872..fdbdd1cbd 100644
--- a/openviking/sync_client.py
+++ b/openviking/sync_client.py
@@ -258,6 +258,7 @@ class SyncOpenViking:
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
since: Optional[str] = None,
until: Optional[str] = None,
@@ -275,6 +276,7 @@ class SyncOpenViking:
score_threshold=score_threshold,
filter=filter,
context_type=context_type,
+ tags=tags,
telemetry=telemetry,
since=since,
until=until,
@@ -291,6 +293,7 @@ class SyncOpenViking:
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
since: Optional[str] = None,
until: Optional[str] = None,
@@ -306,6 +309,7 @@ class SyncOpenViking:
score_threshold,
filter,
context_type,
+ tags,
telemetry,
since,
until,
@@ -347,6 +351,25 @@ class SyncOpenViking:
)
)
+ def set_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ telemetry: TelemetryRequest = False,
+ ) -> Dict[str, Any]:
+ """Replace explicit retrieval tags for a file or directory."""
+ return run_async(
+ self._async_client.set_tags(
+ uri=uri,
+ tags=tags,
+ mode=mode,
+ recursive=recursive,
+ telemetry=telemetry,
+ )
+ )
+
def ls(self, uri: str, **kwargs) -> List[Any]:
"""
List directory contents.
diff --git a/openviking/utils/tags.py b/openviking/utils/tags.py
new file mode 100644
index 000000000..ae882bb0c
--- /dev/null
+++ b/openviking/utils/tags.py
@@ -0,0 +1,59 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""Utilities for explicit k=v search tags."""
+
+from __future__ import annotations
+
+from collections import OrderedDict
+from typing import Iterable
+
+from openviking_cli.exceptions import InvalidArgumentError
+
+
+def normalize_search_tag(tag: str) -> str:
+ """Validate and normalize a single k=v search tag."""
+ value = str(tag).strip().lower()
+ if not value:
+ raise InvalidArgumentError("search tag must be a non-empty k=v string")
+ if value.count("=") != 1:
+ raise InvalidArgumentError(f"invalid search tag '{tag}': expected strict k=v format")
+
+ key, raw_value = value.split("=", 1)
+ if not key or not raw_value:
+ raise InvalidArgumentError(
+ f"invalid search tag '{tag}': key and value must both be non-empty"
+ )
+ return f"{key}={raw_value}"
+
+
+def normalize_search_tags(tags: Iterable[str] | None) -> list[str]:
+ """Normalize explicit search tags while preserving stable order."""
+ if not tags:
+ return []
+
+ normalized: list[str] = []
+ seen: set[str] = set()
+ for item in tags:
+ if item is None:
+ continue
+ value = normalize_search_tag(item)
+ if value in seen:
+ continue
+ seen.add(value)
+ normalized.append(value)
+ return normalized
+
+
+def merge_search_tags(existing: Iterable[str] | None, incoming: Iterable[str] | None) -> list[str]:
+ """Merge normalized search tags by key, replacing old values with incoming ones."""
+ ordered: OrderedDict[str, str] = OrderedDict()
+
+ for item in normalize_search_tags(existing):
+ key, value = item.split("=", 1)
+ ordered[key] = value
+
+ for item in normalize_search_tags(incoming):
+ key, value = item.split("=", 1)
+ ordered[key] = value
+
+ return [f"{key}={value}" for key, value in ordered.items()]
diff --git a/openviking_cli/client/base.py b/openviking_cli/client/base.py
index dbac2cd85..d23a30de7 100644
--- a/openviking_cli/client/base.py
+++ b/openviking_cli/client/base.py
@@ -165,6 +165,18 @@ class BaseClient(ABC):
"""Write text content to an existing file and refresh semantics/vectors."""
...
+ @abstractmethod
+ async def set_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ telemetry: TelemetryRequest = False,
+ ) -> Dict[str, Any]:
+ """Update explicit retrieval tags metadata for a file or directory."""
+ ...
+
# ============= Search =============
@abstractmethod
@@ -176,6 +188,7 @@ class BaseClient(ABC):
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
) -> Any:
"""Semantic search without session context."""
@@ -191,6 +204,7 @@ class BaseClient(ABC):
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
) -> Any:
"""Semantic search with optional session context."""
diff --git a/openviking_cli/client/http.py b/openviking_cli/client/http.py
index 90c65b53e..ed7bcecfa 100644
--- a/openviking_cli/client/http.py
+++ b/openviking_cli/client/http.py
@@ -961,6 +961,30 @@ class AsyncHTTPClient(BaseClient):
response_data = self._handle_response_data(response)
return self._attach_telemetry(response_data.get("result") or {}, response_data)
+ async def set_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ telemetry: TelemetryRequest = False,
+ ) -> Dict[str, Any]:
+ """Update explicit retrieval tags metadata for a file or directory."""
+ telemetry = self._validate_telemetry(telemetry)
+ uri = VikingURI.normalize(uri)
+ response = await self._http.post(
+ "/api/v1/content/set_tags",
+ json={
+ "uri": uri,
+ "tags": tags,
+ "mode": mode,
+ "recursive": recursive,
+ "telemetry": telemetry,
+ },
+ )
+ response_data = self._handle_response_data(response)
+ return self._attach_telemetry(response_data.get("result") or {}, response_data)
+
# ============= Search =============
async def find(
@@ -972,6 +996,7 @@ class AsyncHTTPClient(BaseClient):
score_threshold: Optional[float] = None,
filter: Optional[Dict[str, Any]] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
) -> FindResult:
"""Semantic search without session context."""
@@ -986,6 +1011,7 @@ class AsyncHTTPClient(BaseClient):
"score_threshold": score_threshold,
"filter": filter,
"context_type": context_type,
+ "tags": tags,
"telemetry": telemetry,
}
)
@@ -1004,6 +1030,7 @@ class AsyncHTTPClient(BaseClient):
score_threshold: Optional[float] = None,
filter: Optional[Dict[str, Any]] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
) -> FindResult:
"""Semantic search with optional session context."""
@@ -1020,6 +1047,7 @@ class AsyncHTTPClient(BaseClient):
"score_threshold": score_threshold,
"filter": filter,
"context_type": context_type,
+ "tags": tags,
"telemetry": telemetry,
}
)
diff --git a/openviking_cli/client/sync_http.py b/openviking_cli/client/sync_http.py
index fb29ef322..726a38dc1 100644
--- a/openviking_cli/client/sync_http.py
+++ b/openviking_cli/client/sync_http.py
@@ -454,6 +454,7 @@ class SyncHTTPClient:
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
):
"""Semantic search with optional session context."""
@@ -468,6 +469,7 @@ class SyncHTTPClient:
score_threshold=score_threshold,
filter=filter,
context_type=context_type,
+ tags=tags,
telemetry=telemetry,
)
)
@@ -481,6 +483,7 @@ class SyncHTTPClient:
score_threshold: Optional[float] = None,
filter: Optional[Dict] = None,
context_type: Optional[SearchContextTypeInput] = None,
+ tags: Optional[List[str]] = None,
telemetry: TelemetryRequest = False,
):
"""Semantic search without session context."""
@@ -493,6 +496,7 @@ class SyncHTTPClient:
score_threshold,
filter,
context_type,
+ tags,
telemetry=telemetry,
)
)
@@ -615,6 +619,25 @@ class SyncHTTPClient:
)
)
+ def set_tags(
+ self,
+ uri: str,
+ tags: List[str],
+ mode: str = "replace",
+ recursive: bool = False,
+ telemetry: TelemetryRequest = False,
+ ) -> Dict[str, Any]:
+ """Update explicit retrieval tags metadata for a file or directory."""
+ return run_async(
+ self._async_client.set_tags(
+ uri=uri,
+ tags=tags,
+ mode=mode,
+ recursive=recursive,
+ telemetry=telemetry,
+ )
+ )
+
# ============= Relations =============
def relations(self, uri: str) -> List[Dict[str, Any]]:
diff --git a/src/index/detail/scalar/scalar_index.cpp b/src/index/detail/scalar/scalar_index.cpp
index 7a7b4dd4f..c58e589e4 100644
--- a/src/index/detail/scalar/scalar_index.cpp
+++ b/src/index/detail/scalar/scalar_index.cpp
@@ -14,7 +14,8 @@ int get_type_id(const std::string& field_type) {
return BitmapGroupBase::kBitmapGroupBothBitmapsAndRange;
} else if (field_type == "float32") {
return BitmapGroupBase::kBitmapGroupRangedMap;
- } else if (field_type == "string" || field_type == "bool") {
+ } else if (field_type == "string" || field_type == "bool" ||
+ field_type == "list<string>" || field_type == "list<int64>") {
return BitmapGroupBase::kBitmapGroupBitmaps;
} else if (field_type == "path") {
return BitmapGroupBase::kBitmapGroupDir;
diff --git a/tests/server/test_admin_rebuild_api.py b/tests/server/test_admin_rebuild_api.py
index 8745da0de..9e3f9c063 100644
--- a/tests/server/test_admin_rebuild_api.py
+++ b/tests/server/test_admin_rebuild_api.py
@@ -3,6 +3,7 @@
import httpx
import pytest
+from openviking.core.context import ContextLevel
from openviking.server.identity import RequestContext, Role
from openviking_cli.exceptions import OpenVikingError
from openviking_cli.session.user_id import UserIdentifier
@@ -605,6 +606,65 @@ async def test_reindex_fetch_existing_record_uses_get_context_by_uri(monkeypatch
assert fake_service.vikingdb_manager.lookup_calls
+async def test_reindex_upsert_context_preserves_existing_search_tags(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):
+ del owner_space, limit, ctx
+ return [
+ {
+ "uri": uri,
+ "level": level,
+ "abstract": "existing",
+ "search_tags": ["team-a", "project-x"],
+ }
+ ]
+
+ 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)
+
+ class _FakeMsg:
+ def __init__(self):
+ self.telemetry_id = ""
+ self.id = "msg-1"
+
+ def fake_from_context(context):
+ captured["meta"] = dict(context.meta or {})
+ return _FakeMsg()
+
+ monkeypatch.setattr(
+ "openviking.service.reindex_executor.EmbeddingMsgConverter.from_context",
+ fake_from_context,
+ )
+
+ service = ReindexExecutor()
+ ctx = RequestContext(
+ user=UserIdentifier(account_id="test", user_id="alice"),
+ role=Role.ROOT,
+ )
+
+ await service._upsert_context(
+ uri="viking://resources/demo.txt",
+ parent_uri="viking://resources",
+ abstract="new abstract",
+ vector_text="new vector text",
+ is_leaf=True,
+ context_type="resource",
+ level=ContextLevel.DETAIL,
+ ctx=ctx,
+ )
+
+ assert captured["meta"]["search_tags"] == ["team-a", "project-x"]
+
+
@pytest.mark.asyncio
async def test_reindex_resource_vectors_only_continues_after_single_record_failure(monkeypatch):
from openviking.service.reindex_executor import ReindexExecutor, _ReindexCounters
diff --git a/tests/server/test_api_content_write.py b/tests/server/test_api_content_write.py
index 605197994..134201591 100644
--- a/tests/server/test_api_content_write.py
+++ b/tests/server/test_api_content_write.py
@@ -22,6 +22,11 @@ async def test_write_endpoint_registered(client):
assert resp.status_code == 405
+async def test_set_tags_endpoint_registered(client):
+ resp = await client.get("/api/v1/content/set_tags")
+ assert resp.status_code == 405
+
+
async def test_write_rejects_directory_uri(client_with_resource):
client, uri = client_with_resource
resp = await client.post(
@@ -252,3 +257,80 @@ async def test_api_create_mode_regression_replace_unchanged(client_with_resource
read_resp = await client.get("/api/v1/content/read", params={"uri": file_uri})
assert read_resp.status_code == 200
assert read_resp.json()["result"] == "# Updated\n\nFresh content."
+
+
+async def test_set_tags_requires_tags_field(client_with_resource):
+ client, uri = client_with_resource
+ file_uri = await _first_file_uri(client, uri)
+ resp = await client.post("/api/v1/content/set_tags", json={"uri": file_uri})
+ assert resp.status_code == 400
+
+
+async def test_set_tags_passes_tags_to_service(client, service, monkeypatch):
+ captured = {}
+
+ async def fake_set_tags(
+ *,
+ uri,
+ tags,
+ ctx,
+ mode="replace",
+ recursive=False,
+ ):
+ del ctx
+ captured["uri"] = uri
+ captured["tags"] = tags
+ captured["mode"] = mode
+ captured["recursive"] = recursive
+ return {"uri": uri, "tags": tags}
+
+ monkeypatch.setattr(service.fs, "set_tags", fake_set_tags)
+
+ resp = await client.post(
+ "/api/v1/content/set_tags",
+ json={
+ "uri": "viking://resources/demo/file.md",
+ "tags": ["a", "b"],
+ "mode": "append",
+ "recursive": True,
+ },
+ )
+
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "ok"
+ assert captured == {
+ "uri": "viking://resources/demo/file.md",
+ "tags": ["a", "b"],
+ "mode": "append",
+ "recursive": True,
+ }
+
+
+async def test_set_tags_rejects_wait_and_timeout_fields(client_with_resource):
+ client, uri = client_with_resource
+ file_uri = await _first_file_uri(client, uri)
+ resp = await client.post(
+ "/api/v1/content/set_tags",
+ json={
+ "uri": file_uri,
+ "tags": ["team=search"],
+ "wait": True,
+ "timeout": 3,
+ },
+ )
+ assert resp.status_code == 400
+ body = resp.json()
+ assert body["status"] == "error"
+
+
+async def test_set_tags_rejects_invalid_kv_tag(client_with_resource):
+ client, uri = client_with_resource
+ file_uri = await _first_file_uri(client, uri)
+ resp = await client.post(
+ "/api/v1/content/set_tags",
+ json={"uri": file_uri, "tags": ["project-a"]},
+ )
+ assert resp.status_code == 400
+ body = resp.json()
+ assert body["status"] == "error"
+ assert body["error"]["code"] == "INVALID_ARGUMENT"
diff --git a/tests/server/test_api_search.py b/tests/server/test_api_search.py
index 33508c465..5a2ea8735 100644
--- a/tests/server/test_api_search.py
+++ b/tests/server/test_api_search.py
@@ -20,11 +20,20 @@ from openviking_cli.session.user_id import UserIdentifier
@pytest.fixture(autouse=True)
def fake_query_embedder(service):
class FakeEmbedder:
+ def __init__(self):
+ vector_dim = getattr(
+ getattr(service.viking_fs, "_vector_store", None),
+ "vector_dim",
+ 1024,
+ )
+ self._vector = [0.1] * int(vector_dim)
+
def prepare_embedding_input(self, text: str) -> str:
return text
def embed(self, text: str, is_query: bool = False) -> EmbedResult:
- return EmbedResult(dense_vector=[0.1, 0.2, 0.3])
+ del text, is_query
+ return EmbedResult(dense_vector=list(self._vector))
async def embed_async(self, text: str, is_query: bool = False) -> EmbedResult:
return self.embed(text, is_query=is_query)
@@ -69,6 +78,34 @@ async def test_find_with_target_uri(client_with_resource):
assert resp.json()["status"] == "ok"
+async def test_find_with_target_uri_and_tags_after_set_tags(client_with_resource):
+ client, uri = client_with_resource
+
+ set_tags_resp = await client.post(
+ "/api/v1/content/set_tags",
+ json={"uri": uri, "tags": ["team=search"]},
+ )
+ assert set_tags_resp.status_code == 200
+ assert set_tags_resp.json()["status"] == "ok"
+
+ untagged_resp = await client.post(
+ "/api/v1/search/find",
+ json={"query": "sample", "target_uri": uri, "limit": 10},
+ )
+ assert untagged_resp.status_code == 200
+ assert untagged_resp.json()["status"] == "ok"
+ untagged_total = untagged_resp.json()["result"]["total"]
+ assert untagged_total > 0
+
+ tagged_resp = await client.post(
+ "/api/v1/search/find",
+ json={"query": "sample", "target_uri": uri, "tags": ["team=search"], "limit": 10},
+ )
+ assert tagged_resp.status_code == 200
+ assert tagged_resp.json()["status"] == "ok"
+ assert tagged_resp.json()["result"]["total"] > 0
+
+
async def test_find_with_level_passes_to_service(client: httpx.AsyncClient, service, monkeypatch):
captured = {}
@@ -510,6 +547,37 @@ async def test_find_with_context_type_compiles_filter(
assert captured["filter"] == {"op": "must", "field": "context_type", "conds": ["memory"]}
+async def test_find_combines_tags_with_existing_filter(
+ client: httpx.AsyncClient, service, monkeypatch
+):
+ captured = {}
+
+ async def fake_find(*, filter=None, **kwargs):
+ captured["filter"] = filter
+ return {"items": []}
+
+ monkeypatch.setattr(service.search, "find", fake_find)
+
+ resp = await client.post(
+ "/api/v1/search/find",
+ json={
+ "query": "sample",
+ "filter": {"op": "must", "field": "kind", "conds": ["email"]},
+ "tags": ["Env=Prod", " env=prod "],
+ },
+ )
+
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "ok"
+ assert captured["filter"] == {
+ "op": "and",
+ "conds": [
+ {"op": "must", "field": "kind", "conds": ["email"]},
+ {"op": "must", "field": "search_tags", "conds": ["env=prod"]},
+ ],
+ }
+
+
async def test_search_combines_context_type_list_with_existing_filter(
client: httpx.AsyncClient, service, monkeypatch
):
@@ -541,6 +609,29 @@ async def test_search_combines_context_type_list_with_existing_filter(
}
+async def test_search_compiles_tags_only_filter(client: httpx.AsyncClient, service, monkeypatch):
+ captured = {}
+
+ async def fake_search(*, filter=None, **kwargs):
+ captured["filter"] = filter
+ return {"items": []}
+
+ monkeypatch.setattr(service.search, "search", fake_search)
+
+ resp = await client.post(
+ "/api/v1/search/search",
+ json={"query": "sample", "tags": ["Team=Search"]},
+ )
+
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "ok"
+ assert captured["filter"] == {
+ "op": "must",
+ "field": "search_tags",
+ "conds": ["team=search"],
+ }
+
+
async def test_find_with_invalid_context_type_returns_invalid_argument(client: httpx.AsyncClient):
resp = await client.post(
"/api/v1/search/find",
@@ -554,6 +645,18 @@ async def test_find_with_invalid_context_type_returns_invalid_argument(client: h
assert "context_type" in body["error"]["message"]
+async def test_search_rejects_invalid_kv_tags(client: httpx.AsyncClient):
+ resp = await client.post(
+ "/api/v1/search/search",
+ json={"query": "sample", "tags": ["team-search"]},
+ )
+
+ assert resp.status_code == 400
+ body = resp.json()
+ assert body["status"] == "error"
+ assert body["error"]["code"] == "INVALID_ARGUMENT"
+
+
async def test_find_with_invalid_time_returns_invalid_argument(client: httpx.AsyncClient):
resp = await client.post(
"/api/v1/search/find",
diff --git a/tests/server/test_content_write_service.py b/tests/server/test_content_write_service.py
index ee13babeb..90d9aa8dd 100644
--- a/tests/server/test_content_write_service.py
+++ b/tests/server/test_content_write_service.py
@@ -320,10 +320,12 @@ class _FakeVikingFS:
self.write_file_calls = []
self.rm_calls = []
self.content = {file_uri: "original"}
+ self.vector_store = None
+ self.tree_entries = []
async def stat(self, uri: str, ctx=None):
del ctx
- if uri == self._file_uri:
+ if uri == self._file_uri or uri in self.content:
return {"isDir": False}
if uri == self._root_uri:
return {"isDir": True}
@@ -351,6 +353,23 @@ class _FakeVikingFS:
self.rm_calls.append(uri)
self.content.pop(uri, None)
+ async def tree(
+ self,
+ uri: str,
+ ctx=None,
+ output: str = "original",
+ show_all_hidden: bool = False,
+ node_limit: int = 1000,
+ level_limit: int = 3,
+ abs_limit: int = 256,
+ ):
+ del ctx, output, show_all_hidden, node_limit, level_limit, abs_limit
+ assert uri == self._root_uri
+ return list(self.tree_entries)
+
+ def _get_vector_store(self):
+ return self.vector_store
+
class _FakeSemanticQueue:
def __init__(self):
@@ -973,3 +992,430 @@ async def test_create_mode_regression_append_unchanged(monkeypatch):
)
assert result["mode"] == "append"
+
+
+async def test_set_tags_updates_vector_record(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md"
+ root_uri = "viking://resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ if levels is None:
+ self.update_calls.append((uri, list(tags), mode))
+ return [{"uri": uri}]
+ self.update_calls.append((uri, list(tags), mode, list(levels)))
+ return []
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["Env=Prod", " env=prod "],
+ ctx=ctx,
+ )
+
+ assert result["tags"] == ["env=prod"]
+ assert result["tags_updated"] is True
+ assert "semantic_status" not in result
+ assert "vector_status" not in result
+ assert "queue_status" not in result
+ assert fake_store.update_calls == [(file_uri, ["env=prod"], "replace")]
+
+
+async def test_set_tags_uses_store_update_api_without_fetch(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md"
+ root_uri = "viking://resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def fetch_by_uri(self, uri: str, ctx=None):
+ del uri, ctx
+ raise AssertionError("set_tags should not depend on fetch_by_uri")
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ assert levels is None
+ self.update_calls.append((uri, list(tags), mode))
+ return [{"uri": uri}]
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["Env=Prod"],
+ mode="replace",
+ ctx=ctx,
+ )
+
+ assert result["success_count"] == 1
+ assert result["skipped_count"] == 0
+ assert result["failed_count"] == 0
+ assert result["root_uri"] == root_uri
+ assert fake_store.update_calls == [(file_uri, ["env=prod"], "replace")]
+
+
+async def test_set_tags_user_scope_resource_leaf_returns_parent_root_uri(monkeypatch):
+ file_uri = "viking://user/default/resources/demo/doc.md"
+ root_uri = "viking://user/default/resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ assert levels is None
+ self.update_calls.append((uri, list(tags), mode))
+ return [{"uri": uri}]
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["team=search"],
+ mode="replace",
+ ctx=ctx,
+ )
+
+ assert result["success_count"] == 1
+ assert result["root_uri"] == root_uri
+ assert result["context_type"] == "resource"
+ assert fake_store.update_calls == [(file_uri, ["team=search"], "replace")]
+
+
+async def test_set_tags_derived_abstract_maps_to_parent_level_zero(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md/.abstract.md"
+ root_uri = "viking://resources/demo"
+ updated_uri = "viking://resources/demo/doc.md"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ self.update_calls.append((uri, list(tags), mode, levels))
+ return [{"uri": uri}]
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["team=test"],
+ mode="replace",
+ ctx=ctx,
+ )
+
+ assert result["success_count"] == 1
+ assert result["skipped_count"] == 0
+ assert result["updated_uris"] == [updated_uri]
+ assert fake_store.update_calls == [(updated_uri, ["team=test"], "replace", [0])]
+
+
+async def test_set_tags_append_merges_existing_tags(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md"
+ root_uri = "viking://resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def fetch_by_uri(self, uri: str, ctx=None):
+ del uri, ctx
+ raise AssertionError("append should be handled inside store update API")
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ assert levels is None
+ self.update_calls.append((uri, list(tags), mode))
+ return [{"uri": uri}]
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["Env=Prod", " team=search "],
+ mode="append",
+ ctx=ctx,
+ )
+
+ assert result["mode"] == "append"
+ assert "recursive" not in result
+ assert result["success_count"] == 1
+ assert result["skipped_count"] == 0
+ assert result["failed_count"] == 0
+ assert fake_store.update_calls == [(file_uri, ["env=prod", "team=search"], "append")]
+
+
+async def test_set_tags_rejects_non_kv_tags(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md"
+ root_uri = "viking://resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ raise AssertionError("invalid tags must fail before store update")
+
+ fake_vfs.vector_store = _FakeVectorStore()
+ with pytest.raises(InvalidArgumentError, match="k=v"):
+ await coordinator.set_tags(uri=file_uri, tags=["project-a"], ctx=ctx)
+
+
+async def test_set_tags_recursive_directory_updates_descendants(monkeypatch):
+ root_uri = "viking://resources/demo"
+ file_uri = f"{root_uri}/doc.md"
+ abstract_uri = f"{root_uri}/.abstract.md"
+ overview_uri = f"{root_uri}/.overview.md"
+ nested_dir_uri = f"{root_uri}/nested"
+ nested_abstract_uri = f"{nested_dir_uri}/.abstract.md"
+ nested_overview_uri = f"{nested_dir_uri}/.overview.md"
+ nested_file_uri = f"{nested_dir_uri}/note.md"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ fake_vfs.tree_entries = [
+ {"uri": abstract_uri, "isDir": False},
+ {"uri": overview_uri, "isDir": False},
+ {"uri": file_uri, "isDir": False},
+ {"uri": nested_dir_uri, "isDir": True},
+ {"uri": nested_abstract_uri, "isDir": False},
+ {"uri": nested_overview_uri, "isDir": False},
+ {"uri": nested_file_uri, "isDir": False},
+ ]
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+ self.directory_update_calls = []
+
+ async def fetch_by_uri(self, uri: str, ctx=None):
+ del uri, ctx
+ raise AssertionError("recursive tag updates should use store update API")
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ if levels is None:
+ self.update_calls.append((uri, list(tags), mode))
+ return [{"uri": uri}]
+ self.directory_update_calls.append((uri, list(tags), mode, list(levels)))
+ return [{"uri": uri}]
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+ result = await coordinator.set_tags(
+ uri=root_uri,
+ tags=["env=prod"],
+ mode="append",
+ recursive=True,
+ ctx=ctx,
+ )
+
+ assert result["mode"] == "append"
+ assert "recursive" not in result
+ assert result["success_count"] == 4
+ assert result["skipped_count"] == 0
+ assert result["failed_count"] == 0
+ assert set(result["updated_uris"]) == {
+ root_uri,
+ file_uri,
+ nested_dir_uri,
+ nested_file_uri,
+ }
+ assert sorted(fake_store.update_calls) == sorted(
+ [(file_uri, ["env=prod"], "append"), (nested_file_uri, ["env=prod"], "append")]
+ )
+ assert sorted(fake_store.directory_update_calls) == sorted(
+ [
+ (root_uri, ["env=prod"], "append", [0, 1]),
+ (nested_dir_uri, ["env=prod"], "append", [0, 1]),
+ ]
+ )
+ assert nested_dir_uri in result["updated_uris"]
+
+
+async def test_set_tags_recursive_directory_all_missing_vector_records_returns_zero_counts(
+ monkeypatch,
+):
+ root_uri = "viking://resources/demo"
+ file_uri = f"{root_uri}/doc.md"
+ abstract_uri = f"{root_uri}/.abstract.md"
+ overview_uri = f"{root_uri}/.overview.md"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ fake_vfs.tree_entries = [
+ {"uri": abstract_uri, "isDir": False},
+ {"uri": overview_uri, "isDir": False},
+ {"uri": file_uri, "isDir": False},
+ ]
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ if levels is None:
+ self.update_calls.append((uri, list(tags), mode))
+ return []
+ self.update_calls.append((uri, list(tags), mode, list(levels)))
+ return []
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+ result = await coordinator.set_tags(
+ uri=root_uri,
+ tags=["env=prod"],
+ mode="replace",
+ recursive=True,
+ ctx=ctx,
+ )
+
+ assert result["success_count"] == 0
+ assert result["skipped_count"] == 3
+ assert result["failed_count"] == 0
+ assert result["updated_uris"] == []
+ assert result["tags_updated"] is False
+
+
+async def test_set_tags_non_recursive_directory_all_missing_vector_records_returns_zero_counts(
+ monkeypatch,
+):
+ root_uri = "viking://resources/demo"
+ file_uri = f"{root_uri}/doc.md"
+ abstract_uri = f"{root_uri}/.abstract.md"
+ overview_uri = f"{root_uri}/.overview.md"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ fake_vfs.content[abstract_uri] = "abstract"
+ fake_vfs.content[overview_uri] = "overview"
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, levels=None, ctx=None):
+ del ctx
+ if levels is None:
+ self.update_calls.append((uri, list(tags), mode))
+ return []
+ self.update_calls.append((uri, list(tags), mode, list(levels)))
+ return []
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+ result = await coordinator.set_tags(
+ uri=root_uri,
+ tags=["env=prod"],
+ mode="replace",
+ recursive=False,
+ ctx=ctx,
+ )
+
+ assert result["success_count"] == 0
+ assert result["skipped_count"] == 1
+ assert result["failed_count"] == 0
+ assert result["updated_uris"] == []
+ assert result["tags_updated"] is False
+ assert fake_store.update_calls == [(root_uri, ["env=prod"], "replace", [0, 1])]
+
+
+async def test_set_tags_single_uri_missing_vector_record_returns_zero_counts(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md"
+ root_uri = "viking://resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ def __init__(self):
+ self.update_calls = []
+
+ async def update_search_tags(self, uri: str, tags, *, mode: str, ctx=None):
+ del ctx
+ self.update_calls.append((uri, list(tags), mode))
+ return False
+
+ fake_store = _FakeVectorStore()
+ fake_vfs.vector_store = fake_store
+
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["env=prod"],
+ mode="replace",
+ ctx=ctx,
+ )
+
+ assert result["success_count"] == 0
+ assert result["skipped_count"] == 1
+ assert result["failed_count"] == 0
+ assert result["updated_uris"] == []
+ assert result["root_uri"] == root_uri
+ assert result["tags_updated"] is False
+
+
+async def test_set_tags_does_not_return_write_queue_fields(monkeypatch):
+ file_uri = "viking://resources/demo/doc.md"
+ root_uri = "viking://resources/demo"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ fake_vfs = _FakeVikingFS(file_uri=file_uri, root_uri=root_uri)
+ coordinator = ContentWriteCoordinator(viking_fs=fake_vfs)
+
+ class _FakeVectorStore:
+ async def update_search_tags(self, uri: str, tags, *, mode: str, ctx=None):
+ del ctx
+ assert uri == file_uri
+ assert list(tags) == ["env=prod"]
+ assert mode == "replace"
+ return True
+
+ fake_vfs.vector_store = _FakeVectorStore()
+
+ result = await coordinator.set_tags(
+ uri=file_uri,
+ tags=["env=prod"],
+ mode="replace",
+ ctx=ctx,
+ )
+
+ assert result["tags_updated"] is True
+ assert "semantic_status" not in result
+ assert "vector_status" not in result
+ assert "queue_status" not in result
diff --git a/tests/server/test_http_client_sdk.py b/tests/server/test_http_client_sdk.py
index 23a4d1657..93a14e7e7 100644
--- a/tests/server/test_http_client_sdk.py
+++ b/tests/server/test_http_client_sdk.py
@@ -357,6 +357,42 @@ async def test_sdk_find(http_client):
assert hasattr(result, "total")
+async def test_sdk_find_accepts_tags(http_client):
+ client, _ = http_client
+ f = TEST_TMP_DIR / "sdk_find_tags.md"
+ f.parent.mkdir(parents=True, exist_ok=True)
+ f.write_text(SAMPLE_MD_CONTENT)
+ await client.add_resource(path=str(f), reason="find tags test", wait=True)
+
+ result = await client.find(query="sample document", limit=5, tags=["team=search"])
+ assert hasattr(result, "resources")
+
+
+async def test_sdk_search_accepts_tags(http_client):
+ client, _ = http_client
+ f = TEST_TMP_DIR / "sdk_search_tags.md"
+ f.parent.mkdir(parents=True, exist_ok=True)
+ f.write_text(SAMPLE_MD_CONTENT)
+ await client.add_resource(path=str(f), reason="search tags test", wait=True)
+
+ result = await client.search(query="sample document", limit=5, tags=["team=search"])
+ assert hasattr(result, "resources")
+
+
+async def test_sdk_set_tags_accepts_tags(http_client):
+ client, _ = http_client
+ f = TEST_TMP_DIR / "sdk_write_tags.md"
+ f.parent.mkdir(parents=True, exist_ok=True)
+ f.write_text("hello")
+ added = await client.add_resource(path=str(f), reason="write tags test", wait=True)
+ uri = added["root_uri"]
+ children = await client.ls(uri, simple=True)
+ file_uri = children[0]
+
+ result = await client.set_tags(file_uri, ["team=search"])
+ assert isinstance(result, dict)
+
+
async def test_sdk_find_telemetry(http_client):
client, _ = http_client
f = TEST_TMP_DIR / "sdk_search_telemetry.md"
diff --git a/tests/storage/test_collection_schemas.py b/tests/storage/test_collection_schemas.py
index 8dbe0d48b..c158321e3 100644
--- a/tests/storage/test_collection_schemas.py
+++ b/tests/storage/test_collection_schemas.py
@@ -11,6 +11,7 @@ from types import SimpleNamespace
import pytest
from openviking.models.embedder.base import DenseEmbedderBase, EmbedResult
+from openviking.server.identity import RequestContext, Role, UserIdentifier
from openviking.storage.collection_schemas import (
CollectionSchemas,
TextEmbeddingHandler,
@@ -20,19 +21,35 @@ from openviking.storage.collection_schemas import (
from openviking.storage.errors import EmbeddingRebuildRequiredError
from openviking.storage.expr import Eq
from openviking.storage.queuefs.embedding_msg import EmbeddingMsg
-from openviking.storage.viking_vector_index_backend import _SingleAccountBackend
-from openviking_cli.utils.config.vectordb_config import VectorDBBackendConfig
+from openviking.storage.vectordb import engine as vectordb_engine
+from openviking.storage.vectordb.collection.result import UpsertDataResult
+from openviking.storage.vectordb_adapters.local_adapter import LocalCollectionAdapter
+from openviking.storage.viking_vector_index_backend import (
+ VikingVectorIndexBackend,
+ _SingleAccountBackend,
+)
+from openviking_cli.utils.config.vectordb_config import (
+ QdrantConfig,
+ VectorDBBackendConfig,
+ VolcengineConfig,
+)
class _DummyEmbedder:
def __init__(self):
self.calls = 0
+ def prepare_embedding_input(self, content):
+ return content
+
def embed(self, text: str, is_query: bool = False) -> EmbedResult:
del is_query
self.calls += 1
return EmbedResult(dense_vector=[0.1, 0.2])
+ async def embed_async(self, text: str, is_query: bool = False) -> EmbedResult:
+ return self.embed(text, is_query=is_query)
+
class _DummyConfig:
def __init__(
@@ -49,6 +66,12 @@ class _DummyConfig:
volcengine=SimpleNamespace(api_key=volcengine_data_api_key),
)
)
+ self.log = SimpleNamespace(
+ output="stdout",
+ rotation=False,
+ rotation_interval="midnight",
+ rotation_days=3,
+ )
self.embedding = SimpleNamespace(
dimension=2,
get_embedder=lambda: embedder,
@@ -311,7 +334,8 @@ async def test_embedding_handler_treats_shutdown_write_lock_as_success(monkeypat
self.is_closing = False
self.calls = 0
- async def upsert(self, _data, *, ctx):
+ async def upsert(self, _data, *, ctx, partial_update=False):
+ assert partial_update is True
self.calls += 1
self.is_closing = True
raise RuntimeError("IO error: lock /tmp/LOCK: already held by process")
@@ -373,9 +397,16 @@ async def test_embedding_handler_propagates_account_id_on_error(monkeypatch):
has_queue_manager = False
class _BrokenEmbedder:
+ def prepare_embedding_input(self, content):
+ return content
+
def embed(self, text: str) -> EmbedResult:
raise RuntimeError("boom")
+ async def embed_async(self, text: str, is_query: bool = False) -> EmbedResult:
+ del is_query
+ return self.embed(text)
+
captured: dict[str, object] = {}
monkeypatch.setattr(
"openviking_cli.utils.config.get_openviking_config",
@@ -450,10 +481,16 @@ async def test_embedding_handler_drops_input_too_large_without_requeue(monkeypat
return None
class _OversizedInputEmbedder:
+ def prepare_embedding_input(self, content):
+ return content
+
def embed(self, text: str, is_query: bool = False) -> EmbedResult:
del text, is_query
raise RuntimeError("Malformed input request: expected maxLength: 50000, actual: 75000")
+ async def embed_async(self, text: str, is_query: bool = False) -> EmbedResult:
+ return self.embed(text, is_query=is_query)
+
vikingdb = _QueueingVikingDB()
monkeypatch.setattr(
"openviking_cli.utils.config.get_openviking_config",
@@ -484,7 +521,8 @@ async def test_embedding_handler_preserves_parent_uri_for_backend_upsert_logic(m
is_closing = False
mode = "local"
- async def upsert(self, data, *, ctx):
+ async def upsert(self, data, *, ctx, partial_update=False):
+ assert partial_update is True
captured["data"] = dict(data)
return "rec-1"
@@ -513,7 +551,8 @@ async def test_embedding_handler_marks_success_only_after_tracker_completion(mon
is_closing = False
mode = "local"
- async def upsert(self, _data, *, ctx):
+ async def upsert(self, _data, *, ctx, partial_update=False):
+ assert partial_update is True
return "rec-1"
embedder = _DummyEmbedder()
@@ -848,6 +887,1122 @@ async def test_single_account_backend_upsert_runs_adapter_in_threadpool(monkeypa
)
+async def test_single_account_backend_update_runs_adapter_in_threadpool(monkeypatch):
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id"},
+ {"FieldName": "uri"},
+ {"FieldName": "abstract"},
+ {"FieldName": "account_id"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def update_data(self, data):
+ return [data[0]["id"]]
+
+ async def _fake_to_thread(func, /, *args, **kwargs):
+ calls.append((func.__name__, args, kwargs))
+ return func(*args, **kwargs)
+
+ monkeypatch.setattr(
+ "openviking.storage.viking_vector_index_backend.asyncio.to_thread", _fake_to_thread
+ )
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.update(
+ {
+ "id": "rec-1",
+ "uri": "viking://resources/sample",
+ "abstract": "sample",
+ "account_id": "acc1",
+ "unknown": "legacy",
+ }
+ )
+
+ assert result.ok is True
+ assert result.ids == ["rec-1"]
+ assert result.updated_count == 1
+ assert result.error_code is None
+ assert result.error_message is None
+ assert [call[0] for call in calls] == ["_prepare_upsert_payload", "update_data"]
+ assert calls[-1][1] == (
+ [
+ {
+ "id": "rec-1",
+ "uri": "viking://resources/sample",
+ "abstract": "sample",
+ "account_id": "acc1",
+ }
+ ],
+ )
+
+
+async def test_local_backend_update_preserves_omitted_fields_end_to_end(tmp_path):
+ if not getattr(vectordb_engine, "PersistStore", None):
+ pytest.skip("local persistent vectordb engine is not available in this environment")
+
+ backend = VikingVectorIndexBackend(
+ config=VectorDBBackendConfig(
+ backend="local",
+ name="context",
+ dimension=4,
+ path=str(tmp_path),
+ )
+ )
+ ctx = SimpleNamespace(account_id="acc1")
+
+ created = await backend.create_collection(
+ "context", CollectionSchemas.context_collection("context", 4)
+ )
+ assert created is True
+
+ record_id = await backend.upsert(
+ {
+ "id": "rec-1",
+ "uri": "viking://resources/sample",
+ "account_id": "acc1",
+ "abstract": "before",
+ "name": "keep-me",
+ "vector": [0.1, 0.2, 0.3, 0.4],
+ },
+ ctx=ctx,
+ )
+
+ assert record_id == "rec-1"
+
+ result = await backend.update(
+ {
+ "id": "rec-1",
+ "account_id": "acc1",
+ "abstract": "after",
+ },
+ ctx=ctx,
+ )
+
+ assert result.ok is True
+ assert result.ids == ["rec-1"]
+ assert result.updated_count == 1
+
+ records = await backend.get(["rec-1"], ctx=ctx)
+
+ assert len(records) == 1
+ assert records[0]["abstract"] == "after"
+ assert records[0]["name"] == "keep-me"
+ assert records[0]["account_id"] == "acc1"
+ assert records[0]["uri"] == "viking://resources/sample"
+ assert records[0]["vector"] == pytest.approx([0.1, 0.2, 0.3, 0.4])
+
+
+async def test_local_backend_update_can_clear_string_field_end_to_end(tmp_path):
+ if not getattr(vectordb_engine, "PersistStore", None):
+ pytest.skip("local persistent vectordb engine is not available in this environment")
+
+ backend = VikingVectorIndexBackend(
+ config=VectorDBBackendConfig(
+ backend="local",
+ name="context",
+ dimension=4,
+ path=str(tmp_path),
+ )
+ )
+ ctx = SimpleNamespace(account_id="acc1")
+
+ created = await backend.create_collection(
+ "context", CollectionSchemas.context_collection("context", 4)
+ )
+ assert created is True
+
+ record_id = await backend.upsert(
+ {
+ "id": "rec-1",
+ "uri": "viking://resources/sample",
+ "account_id": "acc1",
+ "name": "keep-me",
+ "tags": "alpha,beta",
+ "vector": [0.1, 0.2, 0.3, 0.4],
+ },
+ ctx=ctx,
+ )
+
+ assert record_id == "rec-1"
+
+ result = await backend.update(
+ {
+ "id": "rec-1",
+ "account_id": "acc1",
+ "tags": "",
+ },
+ ctx=ctx,
+ )
+
+ assert result.ok is True
+ assert result.ids == ["rec-1"]
+ assert result.updated_count == 1
+
+ records = await backend.get(["rec-1"], ctx=ctx)
+
+ assert len(records) == 1
+ assert records[0]["tags"] == ""
+ assert records[0]["name"] == "keep-me"
+ assert records[0]["vector"] == pytest.approx([0.1, 0.2, 0.3, 0.4])
+
+
+def test_local_collection_adapter_update_data_returns_ids():
+ adapter = LocalCollectionAdapter(
+ collection_name="context", project_path="", index_name="default"
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return UpsertDataResult(ids=["doc-1"])
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+async def test_single_account_backend_update_injects_bound_account_id(monkeypatch):
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id"},
+ {"FieldName": "abstract"},
+ {"FieldName": "account_id"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def update_data(self, data):
+ calls.append(("update_data_payload", data))
+ return [data[0]["id"]]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.update({"id": "rec-1", "abstract": "patched"})
+
+ assert result.ok is True
+ assert result.ids == ["rec-1"]
+ assert result.updated_count == 1
+ assert calls == [
+ (
+ "update_data_payload",
+ [{"id": "rec-1", "abstract": "patched", "account_id": "acc1"}],
+ )
+ ]
+
+
+async def test_single_account_backend_update_requires_id_before_adapter_call():
+ class _Collection:
+ def get_meta_data(self):
+ return {"Fields": [{"FieldName": "id"}, {"FieldName": "account_id"}]}
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def update_data(self, data): # pragma: no cover - should never run
+ raise AssertionError("update_data should not be called without id")
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.update({"abstract": "patched"})
+
+ assert result.ok is False
+ assert result.ids == []
+ assert result.updated_count == 0
+ assert result.error_code == "INVALID_ARGUMENT"
+ assert "id is required for update" in (result.error_message or "")
+
+
+async def test_single_account_backend_update_rejects_invalid_context_type_without_adapter_call():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id"},
+ {"FieldName": "abstract"},
+ {"FieldName": "account_id"},
+ {"FieldName": "context_type"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def update_data(self, data): # pragma: no cover - should never run
+ calls.append(data)
+ raise AssertionError("update_data should not be called for invalid context_type")
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.update(
+ {
+ "id": "rec-1",
+ "abstract": "patched",
+ "context_type": "not-a-real-type",
+ }
+ )
+
+ assert result.ok is False
+ assert result.ids == []
+ assert result.updated_count == 0
+ assert result.error_code == "INVALID_ARGUMENT"
+ assert "Invalid context_type" in (result.error_message or "")
+ assert calls == []
+
+
+async def test_single_account_backend_update_returns_structured_error_when_adapter_update_fails():
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id"},
+ {"FieldName": "abstract"},
+ {"FieldName": "account_id"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def update_data(self, data):
+ del data
+ raise RuntimeError("backend exploded")
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.update({"id": "rec-1", "abstract": "patched"})
+
+ assert result.ok is False
+ assert result.ids == []
+ assert result.updated_count == 0
+ assert result.error_code == "UPDATE_FAILED"
+ assert "backend exploded" in (result.error_message or "")
+
+
+async def test_single_account_backend_update_returns_not_found_when_adapter_reports_missing_record():
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id"},
+ {"FieldName": "abstract"},
+ {"FieldName": "account_id"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def update_data(self, data):
+ del data
+ raise ValueError("record not found for primary key(s): ['rec-404']")
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.update({"id": "rec-404", "abstract": "patched"})
+
+ assert result.ok is False
+ assert result.ids == []
+ assert result.updated_count == 0
+ assert result.error_code == "NOT_FOUND"
+ assert "record not found" in (result.error_message or "")
+
+
+async def test_single_account_backend_upsert_partial_update_reads_then_upserts_existing_record():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id", "FieldType": "string"},
+ {"FieldName": "uri", "FieldType": "path"},
+ {"FieldName": "abstract", "FieldType": "string"},
+ {"FieldName": "account_id", "FieldType": "string"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def get(self, ids):
+ calls.append(("get", ids))
+ return [
+ {
+ "id": "rec-1",
+ "abstract": "before",
+ "account_id": "acc1",
+ "uri": "viking://resources/old",
+ }
+ ]
+
+ def upsert(self, data):
+ calls.append(("upsert", data))
+ return ["rec-1"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {"id": "rec-1", "abstract": "patched"},
+ partial_update=True,
+ )
+
+ assert result == "rec-1"
+ assert calls == [
+ ("get", ["rec-1"]),
+ (
+ "upsert",
+ {
+ "id": "rec-1",
+ "abstract": "patched",
+ "account_id": "acc1",
+ "uri": "viking://resources/old",
+ },
+ ),
+ ]
+
+
+async def test_single_account_backend_upsert_partial_update_creates_when_record_does_not_exist():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id", "FieldType": "string", "IsPrimaryKey": True},
+ {"FieldName": "uri", "FieldType": "path"},
+ {"FieldName": "abstract", "FieldType": "string"},
+ {"FieldName": "vector", "FieldType": "vector", "Dim": 2},
+ {"FieldName": "sparse_vector", "FieldType": "sparse_vector"},
+ {"FieldName": "active_count", "FieldType": "int64"},
+ {"FieldName": "account_id", "FieldType": "string"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "local"
+
+ def get_collection(self):
+ return _Collection()
+
+ def get(self, ids):
+ calls.append(("get", ids))
+ return []
+
+ def upsert(self, data):
+ calls.append(("upsert", data))
+ return ["rec-404"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {
+ "id": "rec-404",
+ "uri": "viking://resources/new",
+ "abstract": "created",
+ "unknown": "ignored",
+ },
+ partial_update=True,
+ )
+
+ assert result == "rec-404"
+ assert calls[0] == (
+ "get",
+ ["rec-404"],
+ )
+ assert calls[1] == (
+ "upsert",
+ {
+ "id": "rec-404",
+ "uri": "viking://resources/new",
+ "abstract": "created",
+ "account_id": "acc1",
+ },
+ )
+
+
+async def test_single_account_backend_upsert_partial_update_returns_empty_when_get_fails():
+ class _Adapter:
+ mode = "local"
+
+ def get(self, ids):
+ del ids
+ raise RuntimeError("backend exploded")
+
+ def upsert(self, data): # pragma: no cover - should never run
+ raise AssertionError("upsert should not be called when get fails")
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {"id": "rec-1", "abstract": "patched"},
+ partial_update=True,
+ )
+
+ assert result == ""
+
+
+async def test_single_account_backend_upsert_without_partial_update_keeps_legacy_upsert_behavior():
+ calls = []
+
+ class _Adapter:
+ mode = "local"
+
+ def upsert(self, data):
+ calls.append(data)
+ return ["rec-1"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert({"id": "rec-1", "abstract": "patched"})
+
+ assert result == "rec-1"
+ assert calls == [{"id": "rec-1", "abstract": "patched", "account_id": "acc1"}]
+
+
+async def test_viking_vector_index_backend_upsert_partial_update_delegates_to_account_backend():
+ backend = VikingVectorIndexBackend(
+ config=VectorDBBackendConfig(backend="local", name="context", dimension=2)
+ )
+ ctx = SimpleNamespace(account_id="acc1")
+ calls = []
+
+ class _BoundBackend:
+ async def upsert(self, data, partial_update=False):
+ calls.append((data, partial_update))
+ return data["id"]
+
+ backend._get_backend_for_context = lambda _ctx: _BoundBackend()
+
+ result = await backend.upsert(
+ {"id": "rec-1", "abstract": "patched"},
+ ctx=ctx,
+ partial_update=True,
+ )
+
+ assert result == "rec-1"
+ assert calls == [({"id": "rec-1", "abstract": "patched"}, True)]
+
+
+async def test_vikingdb_manager_proxy_upsert_partial_update_forwards_bound_context():
+ ctx = SimpleNamespace(account_id="acc1")
+ captured = {}
+
+ class _Manager:
+ collection_name = "context"
+ mode = "local"
+ queue_manager = None
+ embedding_queue = None
+ has_queue_manager = False
+ is_closing = False
+
+ async def upsert(self, data, *, ctx, partial_update=False):
+ captured["data"] = data
+ captured["ctx"] = ctx
+ captured["partial_update"] = partial_update
+ return data["id"]
+
+ from openviking.storage.vikingdb_manager import VikingDBManagerProxy
+
+ proxy = VikingDBManagerProxy(_Manager(), ctx)
+ result = await proxy.upsert(
+ {"id": "rec-1", "abstract": "patched"},
+ partial_update=True,
+ )
+
+ assert result == "rec-1"
+ assert captured == {
+ "data": {"id": "rec-1", "abstract": "patched"},
+ "ctx": ctx,
+ "partial_update": True,
+ }
+
+
+async def test_qdrant_backend_upsert_partial_update_reads_then_upserts_existing_record():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id", "FieldType": "string"},
+ {"FieldName": "uri", "FieldType": "path"},
+ {"FieldName": "abstract", "FieldType": "string"},
+ {"FieldName": "account_id", "FieldType": "string"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "qdrant"
+
+ def get(self, ids):
+ calls.append(("get", ids))
+ return [
+ {
+ "id": "doc-1",
+ "uri": "viking://resources/qdrant",
+ "abstract": "before",
+ "account_id": "acc1",
+ }
+ ]
+
+ def upsert(self, data):
+ calls.append(("upsert", data))
+ return ["doc-1"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(
+ backend="qdrant",
+ name="context",
+ dimension=2,
+ qdrant=QdrantConfig(url="http://qdrant:6333"),
+ ),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {"id": "doc-1", "uri": "viking://resources/qdrant", "abstract": "patched"},
+ partial_update=True,
+ )
+
+ assert result == "doc-1"
+ assert calls == [
+ (
+ "get",
+ ["doc-1"],
+ ),
+ (
+ "upsert",
+ {
+ "id": "doc-1",
+ "uri": "viking://resources/qdrant",
+ "abstract": "patched",
+ "account_id": "acc1",
+ },
+ ),
+ ]
+
+
+async def test_qdrant_backend_upsert_partial_update_creates_when_record_does_not_exist():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id", "FieldType": "string"},
+ {"FieldName": "uri", "FieldType": "path"},
+ {"FieldName": "abstract", "FieldType": "string"},
+ {"FieldName": "vector", "FieldType": "vector", "Dim": 2},
+ {"FieldName": "sparse_vector", "FieldType": "sparse_vector"},
+ {"FieldName": "active_count", "FieldType": "int64"},
+ {"FieldName": "account_id", "FieldType": "string"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "qdrant"
+
+ def get(self, ids):
+ calls.append(("get", ids))
+ return []
+
+ def upsert(self, data):
+ calls.append(("upsert", data))
+ return ["doc-404"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(
+ backend="qdrant",
+ name="context",
+ dimension=2,
+ qdrant=QdrantConfig(url="http://qdrant:6333"),
+ ),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {"id": "doc-404", "uri": "viking://resources/qdrant/new", "abstract": "created"},
+ partial_update=True,
+ )
+
+ assert result == "doc-404"
+ assert calls[0] == (
+ "get",
+ ["doc-404"],
+ )
+ assert calls[1] == (
+ "upsert",
+ {
+ "id": "doc-404",
+ "uri": "viking://resources/qdrant/new",
+ "abstract": "created",
+ "account_id": "acc1",
+ },
+ )
+
+
+async def test_volcengine_backend_upsert_partial_update_reads_then_upserts_existing_record():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id", "FieldType": "string"},
+ {"FieldName": "uri", "FieldType": "path"},
+ {"FieldName": "abstract", "FieldType": "string"},
+ {"FieldName": "account_id", "FieldType": "string"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "volcengine"
+
+ def get(self, ids):
+ calls.append(("get", ids))
+ return [
+ {
+ "id": "doc-1",
+ "uri": "viking://resources/volc",
+ "abstract": "before",
+ "account_id": "acc1",
+ }
+ ]
+
+ def upsert(self, data):
+ calls.append(("upsert", data))
+ return ["doc-1"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(
+ backend="volcengine",
+ name="context",
+ dimension=2,
+ volcengine=VolcengineConfig(ak="ak", sk="sk", region="cn-beijing"),
+ ),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {"id": "doc-1", "uri": "viking://resources/volc", "abstract": "patched"},
+ partial_update=True,
+ )
+
+ assert result == "doc-1"
+ assert calls == [
+ (
+ "get",
+ ["doc-1"],
+ ),
+ (
+ "upsert",
+ {
+ "id": "doc-1",
+ "uri": "viking://resources/volc",
+ "abstract": "patched",
+ "account_id": "acc1",
+ },
+ ),
+ ]
+
+
+async def test_volcengine_backend_upsert_partial_update_creates_when_record_does_not_exist():
+ calls = []
+
+ class _Collection:
+ def get_meta_data(self):
+ return {
+ "Fields": [
+ {"FieldName": "id", "FieldType": "string"},
+ {"FieldName": "uri", "FieldType": "path"},
+ {"FieldName": "abstract", "FieldType": "string"},
+ {"FieldName": "vector", "FieldType": "vector", "Dim": 2},
+ {"FieldName": "sparse_vector", "FieldType": "sparse_vector"},
+ {"FieldName": "active_count", "FieldType": "int64"},
+ {"FieldName": "account_id", "FieldType": "string"},
+ ]
+ }
+
+ class _Adapter:
+ mode = "volcengine"
+
+ def get(self, ids):
+ calls.append(("get", ids))
+ return []
+
+ def upsert(self, data):
+ calls.append(("upsert", data))
+ return ["doc-404"]
+
+ backend = _SingleAccountBackend(
+ config=VectorDBBackendConfig(
+ backend="volcengine",
+ name="context",
+ dimension=2,
+ volcengine=VolcengineConfig(ak="ak", sk="sk", region="cn-beijing"),
+ ),
+ bound_account_id="acc1",
+ shared_adapter=_Adapter(),
+ )
+
+ result = await backend.upsert(
+ {"id": "doc-404", "uri": "viking://resources/volc/new", "abstract": "created"},
+ partial_update=True,
+ )
+
+ assert result == "doc-404"
+ assert calls[0] == (
+ "get",
+ ["doc-404"],
+ )
+ assert calls[1] == (
+ "upsert",
+ {
+ "id": "doc-404",
+ "uri": "viking://resources/volc/new",
+ "abstract": "created",
+ "account_id": "acc1",
+ },
+ )
+
+
+async def test_viking_vector_index_backend_update_search_tags_updates_exact_uri_only():
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.ROOT)
+ backend = object.__new__(VikingVectorIndexBackend)
+ calls = {"fetch_by_uri": [], "upsert": []}
+
+ resource_uri = "viking://resources/demo/doc.md"
+
+ async def _fake_fetch_by_uri(uri, *, ctx):
+ calls["fetch_by_uri"].append((uri, ctx.account_id))
+ return {"id": "root-id", "uri": resource_uri, "search_tags": ["old=root"]}
+
+ async def _fake_upsert(data, *, ctx, partial_update=False):
+ del ctx, partial_update
+ calls["upsert"].append(dict(data))
+ return data["id"]
+
+ backend.fetch_by_uri = _fake_fetch_by_uri
+ backend.upsert = _fake_upsert
+
+ updated = await backend.update_search_tags(
+ resource_uri,
+ ["team=search"],
+ mode="append",
+ ctx=ctx,
+ )
+
+ assert updated == [
+ {"id": "root-id", "uri": resource_uri, "search_tags": ["old=root", "team=search"]}
+ ]
+ assert calls["fetch_by_uri"] == [(resource_uri, ctx.account_id)]
+ assert calls["upsert"] == [
+ {"id": "root-id", "uri": resource_uri, "search_tags": ["old=root", "team=search"]}
+ ]
+
+
+async def test_update_search_tags_for_leaf_uri_queries_exact_uri_only(monkeypatch):
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ overview_uri = "viking://resources/demo/doc.md/.overview.md"
+ calls = {"fetch_by_uri": [], "upsert": []}
+
+ backend = VikingVectorIndexBackend.__new__(VikingVectorIndexBackend)
+
+ async def _fake_fetch_by_uri(uri, *, ctx):
+ calls["fetch_by_uri"].append((uri, ctx.account_id))
+ assert uri == overview_uri
+ return {"id": "overview-id", "uri": overview_uri, "search_tags": ["existing=1"]}
+
+ async def _fake_upsert(data, *, ctx, partial_update=False):
+ del ctx, partial_update
+ calls["upsert"].append(dict(data))
+ return data["id"]
+
+ backend.fetch_by_uri = _fake_fetch_by_uri
+ backend.upsert = _fake_upsert
+
+ updated = await backend.update_search_tags(
+ overview_uri,
+ ["team=search"],
+ mode="append",
+ ctx=ctx,
+ )
+
+ assert updated == [
+ {"id": "overview-id", "uri": overview_uri, "search_tags": ["existing=1", "team=search"]}
+ ]
+ assert calls["fetch_by_uri"] == [(overview_uri, ctx.account_id)]
+ assert calls["upsert"] == [
+ {"id": "overview-id", "uri": overview_uri, "search_tags": ["existing=1", "team=search"]}
+ ]
+
+
+async def test_update_search_tags_with_levels_queries_directory_uri_only():
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ directory_uri = "viking://resources/demo/doc.md"
+ calls = {"filter": [], "upsert": []}
+
+ backend = VikingVectorIndexBackend.__new__(VikingVectorIndexBackend)
+
+ async def _fake_filter(*, filter, limit, output_fields, ctx):
+ calls["filter"].append(
+ {
+ "filter": filter,
+ "limit": limit,
+ "output_fields": list(output_fields),
+ "account_id": ctx.account_id,
+ }
+ )
+ return [
+ {"id": "dir-l0", "uri": directory_uri, "level": 0, "search_tags": ["old=0"]},
+ {"id": "dir-l1", "uri": directory_uri, "level": 1, "search_tags": ["old=1"]},
+ ]
+
+ async def _fake_upsert(data, *, ctx, partial_update=False):
+ del ctx, partial_update
+ calls["upsert"].append(dict(data))
+ return data["id"]
+
+ backend.filter = _fake_filter
+ backend.upsert = _fake_upsert
+
+ updated = await backend.update_search_tags(
+ directory_uri,
+ ["team=search"],
+ mode="append",
+ levels=[0, 1],
+ ctx=ctx,
+ )
+
+ assert len(updated) == 2
+ assert len(calls["filter"]) == 1
+ assert calls["filter"][0]["limit"] == 2
+ assert "id" in calls["filter"][0]["output_fields"]
+ assert calls["upsert"] == [
+ {
+ "id": "dir-l0",
+ "uri": directory_uri,
+ "level": 0,
+ "search_tags": ["old=0", "team=search"],
+ },
+ {
+ "id": "dir-l1",
+ "uri": directory_uri,
+ "level": 1,
+ "search_tags": ["old=1", "team=search"],
+ },
+ ]
+
+
+async def test_update_search_tags_with_levels_skips_records_without_id_and_private_helper_is_removed():
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ calls = {"filter": [], "upsert": []}
+
+ backend = VikingVectorIndexBackend.__new__(VikingVectorIndexBackend)
+
+ async def _fake_filter(*, filter, limit, output_fields, ctx):
+ del filter, limit, output_fields, ctx
+ calls["filter"].append(True)
+ return [
+ {
+ "id": "r1",
+ "uri": "viking://resources/demo/doc.md",
+ "level": 0,
+ "search_tags": ["old=1"],
+ },
+ {"uri": "viking://resources/demo/missing-id.md", "level": 1, "search_tags": ["old=2"]},
+ {"id": "r2", "uri": "viking://resources/demo/doc.md", "level": 2, "search_tags": None},
+ ]
+
+ async def _fake_upsert(data, *, ctx, partial_update=False):
+ del ctx, partial_update
+ calls["upsert"].append(dict(data))
+ return data["id"]
+
+ backend.filter = _fake_filter
+ backend.upsert = _fake_upsert
+
+ updated = await backend.update_search_tags(
+ "viking://resources/demo/doc.md",
+ ["team=search"],
+ mode="append",
+ levels=[0, 1, 2],
+ ctx=ctx,
+ )
+
+ assert not hasattr(VikingVectorIndexBackend, "_apply_search_tags_to_records")
+ assert calls["filter"] == [True]
+ assert updated == [
+ {
+ "id": "r1",
+ "uri": "viking://resources/demo/doc.md",
+ "level": 0,
+ "search_tags": ["old=1", "team=search"],
+ },
+ {
+ "id": "r2",
+ "uri": "viking://resources/demo/doc.md",
+ "level": 2,
+ "search_tags": ["team=search"],
+ },
+ ]
+ assert calls["upsert"] == updated
+
+
+async def test_update_search_tags_rejects_invalid_mode_before_fetch():
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ backend = VikingVectorIndexBackend.__new__(VikingVectorIndexBackend)
+ calls = {"fetch_by_uri": 0}
+
+ async def _fake_fetch_by_uri(uri, *, ctx):
+ del uri, ctx
+ calls["fetch_by_uri"] += 1
+ return None
+
+ backend.fetch_by_uri = _fake_fetch_by_uri
+
+ with pytest.raises(ValueError, match="unsupported tag mode"):
+ await backend.update_search_tags(
+ "viking://resources/demo/doc.md",
+ ["team=search"],
+ mode="invalid",
+ ctx=ctx,
+ )
+
+ assert calls["fetch_by_uri"] == 0
+
+
+async def test_update_search_tags_with_levels_rejects_invalid_mode_before_filter():
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.USER)
+ backend = VikingVectorIndexBackend.__new__(VikingVectorIndexBackend)
+ calls = {"filter": 0}
+
+ async def _fake_filter(*, filter, limit, output_fields, ctx):
+ del filter, limit, output_fields, ctx
+ calls["filter"] += 1
+ return []
+
+ backend.filter = _fake_filter
+
+ with pytest.raises(ValueError, match="unsupported tag mode"):
+ await backend.update_search_tags(
+ "viking://resources/demo",
+ ["team=search"],
+ mode="invalid",
+ levels=[0, 1],
+ ctx=ctx,
+ )
+
+ assert calls["filter"] == 0
+
+
@pytest.mark.asyncio
async def test_single_account_backend_mutations_run_adapter_in_threadpool(monkeypatch):
calls = []
diff --git a/tests/storage/test_local_collection_update.py b/tests/storage/test_local_collection_update.py
new file mode 100644
index 000000000..11a3fbdd2
--- /dev/null
+++ b/tests/storage/test_local_collection_update.py
@@ -0,0 +1,240 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+import pytest
+
+from openviking.storage.vectordb.collection.local_collection import LocalCollection
+from openviking.storage.vectordb.collection.result import (
+ DataItem,
+ FetchDataInCollectionResult,
+ UpsertDataResult,
+)
+
+
+def _build_local_collection(
+ existing_by_id: dict[str, dict], captured: list[dict]
+) -> LocalCollection:
+ collection = object.__new__(LocalCollection)
+ collection.meta = SimpleNamespace(primary_key="id")
+ collection.data_processor = SimpleNamespace(
+ validate_and_process=lambda raw: {
+ **({"name": ""} if "name" not in raw else {}),
+ **({"active_count": 0} if "active_count" not in raw else {}),
+ **({"tags": []} if "tags" not in raw else {}),
+ **raw,
+ }
+ )
+ collection.fetch_data = lambda primary_keys: FetchDataInCollectionResult(
+ items=[
+ DataItem(id=key, fields=existing_by_id[key])
+ for key in primary_keys
+ if key in existing_by_id
+ ],
+ ids_not_exist=[key for key in primary_keys if key not in existing_by_id],
+ )
+ collection._write_data_list = lambda data_list, ttl=0: captured.append(
+ {"data_list": data_list, "ttl": ttl}
+ ) or UpsertDataResult(ids=[row["id"] for row in data_list])
+ return collection
+
+
+def test_local_collection_update_preserves_omitted_fields():
+ captured = []
+ collection = _build_local_collection(
+ {
+ "doc-1": {
+ "id": "doc-1",
+ "name": "before",
+ "active_count": 7,
+ "tags": ["alpha", "beta"],
+ }
+ },
+ captured,
+ )
+
+ result = collection.update_data([{"id": "doc-1", "name": "after"}])
+
+ assert result.ids == ["doc-1"]
+ assert captured == [
+ {
+ "data_list": [
+ {
+ "id": "doc-1",
+ "name": "after",
+ "active_count": 7,
+ "tags": ["alpha", "beta"],
+ }
+ ],
+ "ttl": 0,
+ }
+ ]
+
+
+def test_local_collection_update_overwrites_explicit_empty_values():
+ captured = []
+ collection = _build_local_collection(
+ {
+ "doc-1": {
+ "id": "doc-1",
+ "name": "before",
+ "active_count": 7,
+ "tags": ["alpha", "beta"],
+ }
+ },
+ captured,
+ )
+
+ result = collection.update_data([{"id": "doc-1", "tags": []}])
+
+ assert result.ids == ["doc-1"]
+ assert captured[0]["data_list"][0] == {
+ "id": "doc-1",
+ "name": "before",
+ "active_count": 7,
+ "tags": [],
+ }
+
+
+def test_local_collection_update_overwrites_explicit_none_values():
+ captured = []
+ collection = _build_local_collection(
+ {
+ "doc-1": {
+ "id": "doc-1",
+ "name": "before",
+ "active_count": 7,
+ "tags": ["alpha", "beta"],
+ }
+ },
+ captured,
+ )
+
+ result = collection.update_data([{"id": "doc-1", "name": None}])
+
+ assert result.ids == ["doc-1"]
+ assert captured[0]["data_list"][0] == {
+ "id": "doc-1",
+ "name": None,
+ "active_count": 7,
+ "tags": ["alpha", "beta"],
+ }
+
+
+def test_local_collection_update_validates_after_merge_for_required_fields():
+ captured = []
+ collection = _build_local_collection(
+ {
+ "doc-1": {
+ "id": "doc-1",
+ "name": "before",
+ "vector": [0.1, 0.2, 0.3, 0.4],
+ "active_count": 7,
+ "tags": ["alpha"],
+ }
+ },
+ captured,
+ )
+
+ collection.data_processor = SimpleNamespace(
+ validate_and_process=lambda raw: (
+ (_ for _ in ()).throw(ValueError("vector required")) if "vector" not in raw else raw
+ )
+ )
+
+ result = collection.update_data([{"id": "doc-1", "name": "after"}])
+
+ assert result.ids == ["doc-1"]
+ assert captured[0]["data_list"][0]["vector"] == [0.1, 0.2, 0.3, 0.4]
+ assert captured[0]["data_list"][0]["name"] == "after"
+
+
+def test_local_collection_update_merges_multiple_rows_independently():
+ captured = []
+ collection = _build_local_collection(
+ {
+ "doc-1": {
+ "id": "doc-1",
+ "name": "before-1",
+ "active_count": 7,
+ "tags": ["alpha"],
+ },
+ "doc-2": {
+ "id": "doc-2",
+ "name": "before-2",
+ "active_count": 9,
+ "tags": ["beta"],
+ },
+ },
+ captured,
+ )
+
+ result = collection.update_data(
+ [
+ {"id": "doc-1", "name": "after-1"},
+ {"id": "doc-2", "tags": []},
+ ]
+ )
+
+ assert result.ids == ["doc-1", "doc-2"]
+ assert captured == [
+ {
+ "data_list": [
+ {
+ "id": "doc-1",
+ "name": "after-1",
+ "active_count": 7,
+ "tags": ["alpha"],
+ },
+ {
+ "id": "doc-2",
+ "name": "before-2",
+ "active_count": 9,
+ "tags": [],
+ },
+ ],
+ "ttl": 0,
+ }
+ ]
+
+
+def test_local_collection_update_requires_existing_record():
+ collection = _build_local_collection({}, [])
+
+ with pytest.raises(ValueError, match="not found"):
+ collection.update_data([{"id": "missing", "name": "after"}])
+
+
+def test_local_collection_update_rejects_batch_when_any_record_is_missing():
+ captured = []
+ collection = _build_local_collection(
+ {
+ "doc-1": {
+ "id": "doc-1",
+ "name": "before-1",
+ "active_count": 7,
+ "tags": ["alpha"],
+ }
+ },
+ captured,
+ )
+
+ with pytest.raises(ValueError, match="not found"):
+ collection.update_data(
+ [
+ {"id": "doc-1", "name": "after-1"},
+ {"id": "doc-404", "name": "after-404"},
+ ]
+ )
+
+ assert captured == []
+
+
+def test_local_collection_update_requires_primary_key():
+ collection = _build_local_collection({}, [])
+
+ with pytest.raises(ValueError, match="primary key"):
+ collection.update_data([{"name": "after"}])
diff --git a/tests/storage/test_qdrant_collection.py b/tests/storage/test_qdrant_collection.py
index fc18c0123..69a156eed 100644
--- a/tests/storage/test_qdrant_collection.py
+++ b/tests/storage/test_qdrant_collection.py
@@ -11,8 +11,8 @@ import openviking.storage.vectordb.collection.qdrant_collection as qdrant_collec
from openviking.storage.vectordb.collection.qdrant_collection import (
ORIGINAL_ID_FIELD,
QdrantCollection,
- _to_qdrant_point_id,
_sparse_to_qdrant,
+ _to_qdrant_point_id,
)
from openviking.storage.vectordb.collection.qdrant_rest import QdrantRestError
@@ -214,6 +214,56 @@ def test_fetch_data_maps_ids_and_returns_original_ids():
assert result.ids_not_exist == ["doc_2"]
+def test_update_data_preserves_omitted_fields_and_overwrites_explicit_fields():
+ point_id = _to_qdrant_point_id("doc_1")
+ client = _StubClient(
+ [
+ {
+ "result": [
+ {
+ "id": point_id,
+ "payload": {
+ ORIGINAL_ID_FIELD: "doc_1",
+ "name": "Before",
+ "status": "active",
+ },
+ "vector": {"vector": [0.1, 0.2, 0.3]},
+ }
+ ]
+ },
+ {"status": "ok"},
+ ]
+ )
+ collection = _build_collection_stub(client)
+
+ updated_ids = collection.update_data([{"id": "doc_1", "name": "After"}])
+
+ assert updated_ids == ["doc_1"]
+ assert client.calls[0]["method"] == "POST"
+ assert client.calls[0]["path"] == "/collections/proj__context/points"
+ assert client.calls[0]["json_body"]["ids"] == [point_id]
+ assert client.calls[0]["json_body"]["with_vector"] is True
+
+ assert client.calls[1]["method"] == "PUT"
+ assert client.calls[1]["path"] == "/collections/proj__context/points"
+ point = client.calls[1]["json_body"]["points"][0]
+ assert point["id"] == point_id
+ assert point["vector"]["vector"] == [0.1, 0.2, 0.3]
+ assert point["payload"] == {
+ ORIGINAL_ID_FIELD: "doc_1",
+ "name": "After",
+ "status": "active",
+ }
+
+
+def test_update_data_requires_existing_record():
+ client = _StubClient([{"result": []}])
+ collection = _build_collection_stub(client)
+
+ with pytest.raises(ValueError, match="Qdrant point does not exist for update: doc_404"):
+ collection.update_data([{"id": "doc_404", "name": "After"}])
+
+
def test_search_by_id_uses_stored_vector_for_similarity_query():
point_id = _to_qdrant_point_id("doc_1")
client = _StubClient(
diff --git a/tests/storage/test_volcengine_clients.py b/tests/storage/test_volcengine_clients.py
index b2e34e4d0..4e53b155b 100644
--- a/tests/storage/test_volcengine_clients.py
+++ b/tests/storage/test_volcengine_clients.py
@@ -8,8 +8,8 @@ from openviking.storage.vectordb.collection.volcengine_clients import (
from openviking.storage.vectordb.collection.volcengine_collection import VolcengineCollection
from openviking.storage.vectordb_adapters.volcengine_adapter import VolcengineCollectionAdapter
from openviking_cli.utils.config.vectordb_config import (
- VolcengineConfig,
VectorDBBackendConfig,
+ VolcengineConfig,
)
@@ -216,3 +216,520 @@ def test_volcengine_collection_get_meta_data_returns_empty_on_collection_not_fou
monkeypatch.setattr(collection.console_client, "do_req", lambda *args, **kwargs: _Response())
assert collection.get_meta_data() == {}
+
+
+def test_volcengine_collection_update_data_posts_to_update_endpoint(monkeypatch):
+ captured = {}
+
+ class _Response:
+ status_code = 200
+
+ @staticmethod
+ def json():
+ return {"result": {"updated": 1}}
+
+ collection = VolcengineCollection(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ meta_data={"ProjectName": "default", "CollectionName": "context"},
+ )
+
+ def _fake_do_req(method, path=None, req_params=None, req_body=None):
+ captured["method"] = method
+ captured["path"] = path
+ captured["req_params"] = req_params
+ captured["req_body"] = req_body
+ return _Response()
+
+ monkeypatch.setattr(collection.data_client, "do_req", _fake_do_req)
+
+ result = collection.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == {"updated": 1}
+ assert captured["method"] == "POST"
+ assert captured["path"] == "/api/vikingdb/data/update"
+ assert captured["req_body"] == {
+ "project": "default",
+ "collection_name": "context",
+ "data": [{"id": "doc-1", "name": "updated"}],
+ }
+
+
+def test_volcengine_collection_update_data_sanitizes_uri_fields(monkeypatch):
+ captured = {}
+
+ class _Response:
+ status_code = 200
+
+ @staticmethod
+ def json():
+ return {"result": {"updated": 1}}
+
+ collection = VolcengineCollection(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ meta_data={"ProjectName": "default", "CollectionName": "context"},
+ )
+
+ def _fake_do_req(method, path=None, req_params=None, req_body=None):
+ captured["method"] = method
+ captured["path"] = path
+ captured["req_body"] = req_body
+ return _Response()
+
+ monkeypatch.setattr(collection.data_client, "do_req", _fake_do_req)
+
+ collection.update_data(
+ [{"id": "doc-1", "uri": "viking://resources/demo", "parent_uri": "viking://resources"}]
+ )
+
+ assert captured["path"] == "/api/vikingdb/data/update"
+ assert captured["req_body"] == {
+ "project": "default",
+ "collection_name": "context",
+ "data": [{"id": "doc-1", "uri": "/resources/demo", "parent_uri": "/resources"}],
+ }
+
+
+def test_volcengine_api_key_collection_update_data_posts_to_update_endpoint(monkeypatch):
+ captured = {}
+
+ class _Response:
+ status_code = 200
+
+ @staticmethod
+ def json():
+ return {"result": {"updated": 1}}
+
+ from openviking.storage.vectordb.collection.volcengine_api_key_collection import (
+ VolcengineApiKeyCollection,
+ )
+
+ collection = VolcengineApiKeyCollection(
+ api_key="vk-test-token",
+ region="cn-beijing",
+ meta_data={"ProjectName": "default", "CollectionName": "context", "IndexName": "default"},
+ )
+
+ def _fake_do_req(method, req_path=None, req_params=None, req_body=None):
+ captured["method"] = method
+ captured["path"] = req_path
+ captured["req_params"] = req_params
+ captured["req_body"] = req_body
+ return _Response()
+
+ monkeypatch.setattr(collection.data_client, "do_req", _fake_do_req)
+
+ result = collection.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == {"updated": 1}
+ assert captured["method"] == "POST"
+ assert captured["path"] == "/api/vikingdb/data/update"
+ assert captured["req_body"] == {
+ "project": "default",
+ "collection_name": "context",
+ "data": [{"id": "doc-1", "name": "updated"}],
+ }
+
+
+def test_volcengine_api_key_collection_update_data_sanitizes_uri_fields(monkeypatch):
+ captured = {}
+
+ class _Response:
+ status_code = 200
+
+ @staticmethod
+ def json():
+ return {"result": {"updated": 1}}
+
+ from openviking.storage.vectordb.collection.volcengine_api_key_collection import (
+ VolcengineApiKeyCollection,
+ )
+
+ collection = VolcengineApiKeyCollection(
+ api_key="vk-test-token",
+ region="cn-beijing",
+ meta_data={"ProjectName": "default", "CollectionName": "context", "IndexName": "default"},
+ )
+
+ def _fake_do_req(method, req_path=None, req_params=None, req_body=None):
+ captured["path"] = req_path
+ captured["req_body"] = req_body
+ return _Response()
+
+ monkeypatch.setattr(collection.data_client, "do_req", _fake_do_req)
+
+ collection.update_data(
+ [{"id": "doc-1", "uri": "viking://resources/demo", "parent_uri": "viking://resources"}]
+ )
+
+ assert captured["path"] == "/api/vikingdb/data/update"
+ assert captured["req_body"] == {
+ "project": "default",
+ "collection_name": "context",
+ "data": [{"id": "doc-1", "uri": "/resources/demo", "parent_uri": "/resources"}],
+ }
+
+
+def test_volcengine_adapter_update_data_returns_ids():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return {"updated": 1, "primary_keys": ["doc-1"]}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+def test_volcengine_adapter_update_data_returns_batch_primary_keys():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [
+ {"id": "doc-1", "name": "updated-1"},
+ {"id": "doc-2", "name": "updated-2"},
+ ]
+ return {"updated": 2, "primary_keys": ["doc-1", "doc-2"]}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data(
+ [
+ {"id": "doc-1", "name": "updated-1"},
+ {"id": "doc-2", "name": "updated-2"},
+ ]
+ )
+
+ assert result == ["doc-1", "doc-2"]
+
+
+def test_volcengine_adapter_update_data_returns_ids_from_ids_key():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return {"updated": 1, "ids": ["doc-1"]}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+def test_volcengine_adapter_update_data_returns_batch_ids_from_ids_key():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [
+ {"id": "doc-1", "name": "updated-1"},
+ {"id": "doc-2", "name": "updated-2"},
+ ]
+ return {"updated": 2, "ids": ["doc-1", "doc-2"]}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data(
+ [
+ {"id": "doc-1", "name": "updated-1"},
+ {"id": "doc-2", "name": "updated-2"},
+ ]
+ )
+
+ assert result == ["doc-1", "doc-2"]
+
+
+def test_volcengine_adapter_update_data_falls_back_to_request_ids_when_backend_omits_them():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return {"updated": 1}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+def test_volcengine_adapter_update_data_falls_back_to_batch_request_ids_when_backend_omits_them():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [
+ {"id": "doc-1", "name": "updated-1"},
+ {"id": "doc-2", "name": "updated-2"},
+ ]
+ return {"updated": 2}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data(
+ [
+ {"id": "doc-1", "name": "updated-1"},
+ {"id": "doc-2", "name": "updated-2"},
+ ]
+ )
+
+ assert result == ["doc-1", "doc-2"]
+
+
+def test_volcengine_adapter_update_data_falls_back_when_ids_key_is_not_a_list():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return {"updated": 1, "ids": "doc-1"}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+def test_volcengine_adapter_update_data_falls_back_when_primary_keys_is_not_a_list():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return {"updated": 1, "primary_keys": "doc-1"}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+def test_volcengine_adapter_update_data_returns_empty_when_backend_reports_zero_updates():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-404", "name": "updated"}]
+ return {"updated": 0}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-404", "name": "updated"}])
+
+ assert result == []
+
+
+def test_volcengine_adapter_update_data_returns_empty_when_backend_dict_has_no_ids_or_updated_count():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-404", "name": "updated"}]
+ return {"status": "ok"}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-404", "name": "updated"}])
+
+ assert result == []
+
+
+def test_volcengine_adapter_update_data_skips_records_without_id_in_fallback():
+ adapter = VolcengineCollectionAdapter(
+ ak="test-ak",
+ sk="test-sk",
+ region="cn-beijing",
+ session_token=None,
+ api_key=None,
+ host=None,
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}, {"name": "missing-id"}]
+ return {"updated": 2}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}, {"name": "missing-id"}])
+
+ assert result == ["doc-1"]
+
+
+def test_volcengine_adapter_update_data_supports_api_key_mode():
+ adapter = VolcengineCollectionAdapter(
+ ak=None,
+ sk=None,
+ region="cn-beijing",
+ session_token=None,
+ api_key="vk-test-token",
+ host="api-vikingdb.vikingdb.cn-beijing.volces.com",
+ project_name="default",
+ collection_name="context",
+ index_name="default",
+ )
+
+ class _Collection:
+ def update_data(self, data_list):
+ assert data_list == [{"id": "doc-1", "name": "updated"}]
+ return {"updated": 1, "primary_keys": ["doc-1"]}
+
+ adapter._collection = _Collection()
+
+ result = adapter.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+
+
+def test_http_collection_update_data_posts_to_update_endpoint(monkeypatch):
+ captured = {}
+
+ class _Response:
+ status_code = 200
+ text = '{"data": ["doc-1"]}'
+
+ def _fake_post(url, headers=None, json=None, timeout=None):
+ captured["url"] = url
+ captured["headers"] = headers
+ captured["json"] = json
+ captured["timeout"] = timeout
+ return _Response()
+
+ monkeypatch.setattr(
+ "openviking.storage.vectordb.collection.http_collection.requests.post",
+ _fake_post,
+ )
+
+ from openviking.storage.vectordb.collection.http_collection import HttpCollection
+
+ collection = HttpCollection(
+ ip="127.0.0.1",
+ port=1933,
+ meta_data={"ProjectName": "default", "CollectionName": "context"},
+ )
+
+ result = collection.update_data([{"id": "doc-1", "name": "updated"}])
+
+ assert result == ["doc-1"]
+ assert captured["url"] == "http://127.0.0.1:1933/api/vikingdb/data/update"
+ assert captured["json"] == {
+ "project": "default",
+ "collection_name": "context",
+ "fields": '[{"id": "doc-1", "name": "updated"}]',
+ }
diff --git a/tests/vectordb/test_data_processor.py b/tests/vectordb/test_data_processor.py
index a95130c05..2dbc0e20a 100644
--- a/tests/vectordb/test_data_processor.py
+++ b/tests/vectordb/test_data_processor.py
@@ -35,7 +35,7 @@ class TestDataProcessor(unittest.TestCase):
self.assertIn(("geo_lon", "float32"), mapped)
self.assertIn(("geo_lat", "float32"), mapped)
self.assertIn(("uri", "path"), mapped)
- self.assertIn(("tags", "string"), mapped)
+ self.assertIn(("tags", "list<string>"), mapped)
def test_datetime_and_geo_point_conversion(self):
data = {
diff --git a/tests/vectordb/test_filter_ops.py b/tests/vectordb/test_filter_ops.py
index f7f9ccec4..a1b50ad56 100644
--- a/tests/vectordb/test_filter_ops.py
+++ b/tests/vectordb/test_filter_ops.py
@@ -1,4 +1,5 @@
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
import gc
@@ -141,9 +142,7 @@ class TestFilterOpsLargeFields(unittest.TestCase):
def test_upsert_record_with_oversized_json_field_raises(self):
large_abstract = 'prefix "quoted" \\\\ path\n' + ("x" * 66000)
uri = "viking://user/memories/large.md"
- with self.assertRaisesRegex(
- (RuntimeError, ValueError), "fields.*exceeds 65535 bytes"
- ):
+ with self.assertRaisesRegex((RuntimeError, ValueError), "fields.*exceeds 65535 bytes"):
self.collection.upsert_data(
[
{
@@ -1175,6 +1174,15 @@ class TestFilterOpsTypes(unittest.TestCase):
self.assertEqual(
self._search({"op": "must", "field": "f_list_str", "conds": ["b"]}), [1, 2]
)
+ # Multi-cond must on list<string> uses contains-any semantics
+ self.assertEqual(
+ self._search({"op": "must", "field": "f_list_str", "conds": ["b", "d"]}),
+ [1, 2, 3, 4],
+ )
+ self.assertEqual(
+ self._search({"op": "must", "field": "f_list_str", "conds": ["missing"]}),
+ [],
+ )
# List<Int64> contains
# 3 is in id 2, 3
@@ -1260,6 +1268,10 @@ class TestFilterOpsTypes(unittest.TestCase):
self.assertEqual(
self._search({"op": "must", "field": "f_list_str", "conds": ["a"]}), [1]
)
+ self.assertEqual(
+ self._search({"op": "must", "field": "f_list_str", "conds": ["b", "d"]}),
+ [1, 2, 3, 4],
+ )
# Date range
self.assertEqual(
self._search({"op": "range", "field": "f_date", "gt": "2023-01-01T10:00:00+00:00"}),