From a2c97449112cd2015431569a5aa283bf6d7d9fe6 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:01:09 -0800 Subject: [PATCH 01/19] rust(feat): generalize bulk creates into a bulk-write core --- rust/crates/sift_mcp/src/tool/common/bulk.rs | 444 ++++++++++++++++++ .../sift_mcp/src/tool/common/bulk_create.rs | 191 +------- rust/crates/sift_mcp/src/tool/common/mod.rs | 1 + 3 files changed, 462 insertions(+), 174 deletions(-) create mode 100644 rust/crates/sift_mcp/src/tool/common/bulk.rs diff --git a/rust/crates/sift_mcp/src/tool/common/bulk.rs b/rust/crates/sift_mcp/src/tool/common/bulk.rs new file mode 100644 index 000000000..f37116d9c --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -0,0 +1,444 @@ +use std::collections::HashMap; +use std::future::Future; + +use rmcp::{ + ErrorData, + model::{CallToolResult, ContentBlock, ErrorCode}, + schemars::{self, JsonSchema}, +}; +use serde::{Deserialize, Deserializer, de::DeserializeOwned}; +use serde_json::{Map, Value, json}; + +use crate::{error::McpResult, service::batch}; + +pub const MAX_ITEMS: usize = 100; + +/// Shared item list and approval mask for every bulk write tool. +#[derive(Debug, JsonSchema)] +#[schemars(deny_unknown_fields)] +pub struct BulkItems { + /// 1–100 items. Pass one item to write one resource. + #[schemars(length(min = 1, max = 100))] + #[schemars(with = "Vec")] + pub items: Vec, + /// Approval host's zero-based selection of `items` to write. Omit when proposing. + /// An empty selection performs no writes. + pub selected_indices: Option>, + #[schemars(skip)] + _item: std::marker::PhantomData, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct BulkCall { + items: Vec, + selected_indices: Option>, +} + +impl<'de, T> Deserialize<'de> for BulkItems { + fn deserialize>(deserializer: D) -> Result { + let object = Map::::deserialize(deserializer)?; + let BulkCall { + items, + selected_indices, + } = bulk_call(object).map_err(serde::de::Error::custom)?; + Ok(Self { + items, + selected_indices, + _item: std::marker::PhantomData, + }) + } +} + +/// The single place that decides which top-level shapes a bulk tool accepts. +/// Only `items` (plus the host's `selected_indices`) is accepted. A call in the +/// old one-resource shape is rejected with a migration hint. +fn bulk_call(object: Map) -> Result { + if !object.contains_key("items") { + return Err( + "missing field `items`: pass the fields for each resource as one object in the \ + `items` array, for example `{\"items\": [{...}]}`" + .into(), + ); + } + serde_json::from_value(Value::Object(object)).map_err(|error| error.to_string()) +} + +/// Prefix an item's validation error with its index in `items`. +pub fn at_item(index: usize, error: ErrorData) -> ErrorData { + ErrorData::new( + error.code, + format!("`items` item {index}: {}", error.message), + error.data, + ) +} + +pub fn parse_items( + items: Vec, +) -> Result, ErrorData> { + if items.is_empty() || items.len() > MAX_ITEMS { + return Err(ErrorData::invalid_params( + format!("`items` must contain 1–{MAX_ITEMS} items"), + None, + )); + } + let schema = schemars::schema_for!(T); + items + .into_iter() + .enumerate() + .map(|(index, item)| { + if let Some(fields) = schema.get("properties").and_then(Value::as_object) + && let Some(object) = item.as_object() + && let Some(unknown) = object.keys().find(|key| !fields.contains_key(*key)) + { + return Err(ErrorData::invalid_params( + format!("`items` item {index}: unknown field `{unknown}`"), + None, + )); + } + serde_json::from_value(item).map_err(|error| { + ErrorData::invalid_params(format!("`items` item {index}: {error}"), None) + }) + }) + .collect() +} + +/// Reject a call in which two items write the same resource, so the result +/// never depends on item order. `target` returns `None` for items without one. +pub fn reject_duplicate_targets( + items: &[T], + resource: &str, + target: impl Fn(&T) -> Option, +) -> Result<(), ErrorData> { + let mut seen = HashMap::new(); + for (index, item) in items.iter().enumerate() { + let Some(key) = target(item) else { continue }; + if let Some(first) = seen.insert(key, index) { + return Err(ErrorData::invalid_params( + format!("`items` item {index}: targets the same {resource} as item {first}"), + None, + )); + } + } + Ok(()) +} + +/// The approval mask and whether this server requires one. +#[derive(Debug, Clone)] +pub struct Selection { + pub indices: Option>, + /// Set when an approval host runs this server (`sift-cli mcp --chat`). A missing mask + /// then fails closed so a host regression cannot write unapproved items. + pub required: bool, +} + +/// The kind of write a bulk tool performs. It sets the result's count field, +/// its wording, and which errors stop the remaining items. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Op { + Create, + Update, + Archive, + Unarchive, +} + +impl Op { + fn past(self) -> &'static str { + match self { + Self::Create => "Created", + Self::Update => "Updated", + Self::Archive => "Archived", + Self::Unarchive => "Unarchived", + } + } + + fn count_key(self) -> &'static str { + match self { + Self::Create => "created_count", + Self::Update => "updated_count", + Self::Archive => "archived_count", + Self::Unarchive => "unarchived_count", + } + } +} + +fn reason(error: &ErrorData) -> Option<&str> { + error.data.as_ref()?.get("reason")?.as_str() +} + +/// Item-specific errors let the remaining items run. Anything that points at the +/// backend or the caller's session stops them. Permission on an existing resource +/// is scoped to that resource (rule assets, artifact creator), so it is item-specific +/// for writes to existing resources. +pub(crate) fn should_stop(op: Op, error: &ErrorData) -> bool { + if error.code == ErrorCode::INVALID_PARAMS || error.code == ErrorCode::RESOURCE_NOT_FOUND { + return false; + } + match reason(error) { + Some("AlreadyExists" | "Conflict") => false, + Some("PermissionDenied") => op == Op::Create, + _ => true, + } +} + +pub async fn run( + op: Op, + items: Vec, + selection: Selection, + partial_writes: bool, + operation: F, +) -> McpResult +where + F: FnMut(T) -> Fut, + Fut: Future, +{ + let requested_count = items.len(); + let selected_indices = match selection.indices { + Some(indices) => indices, + None if selection.required => { + return Err(ErrorData::invalid_params( + "Writes require explicit `selected_indices` from the approval host; nothing was written. \ + The chat host is out of date for this tool. Tell the user the change could not be \ + applied from chat yet.", + None, + )); + } + None => (0..requested_count).collect(), + }; + let mut selected = vec![false; requested_count]; + for &index in &selected_indices { + if index >= requested_count || selected[index] { + return Err(ErrorData::invalid_params( + "`selected_indices` must contain unique, in-range input indices", + None, + )); + } + selected[index] = true; + } + let indices = (0..requested_count) + .filter(|&index| selected[index]) + .collect::>(); + let not_selected = (0..requested_count) + .filter(|&index| !selected[index]) + .collect::>(); + let items = items + .into_iter() + .enumerate() + .filter_map(|(index, item)| selected[index].then_some(item)) + .collect(); + let outcome = batch::execute(items, operation, |error| should_stop(op, error)).await; + let not_attempted = outcome + .not_attempted + .into_iter() + .map(|index| indices[index]) + .collect::>(); + let successes = outcome + .successes + .into_iter() + .map( + |(index, result)| json!({"index": indices[index], "result": result.structured_content}), + ) + .collect::>(); + let failures = outcome + .failures + .into_iter() + .map(|(index, error)| { + // An interrupted write or a composite task can commit before returning + // an error. Never instruct callers to blindly replay a failed write. + let outcome_unknown = partial_writes + || (error.code != ErrorCode::INVALID_PARAMS + && error.code != ErrorCode::RESOURCE_NOT_FOUND + && !matches!( + reason(&error), + Some("AlreadyExists" | "PermissionDenied" | "Unauthenticated") + )); + json!({"index": indices[index], "error": error, "outcome_unknown": outcome_unknown}) + }) + .collect::>(); + let has_errors = !failures.is_empty() || !not_attempted.is_empty(); + let next_step = format!( + "{} {} items; {} failed; {} not attempted. \ + On success, confirm in one short sentence using resource names or links; omit unrequested metadata, \ + internal indices/masks, and approval/retry narration. Report actionable warnings and failures. \ + Never replay the batch: successful writes are committed, and the user rejected the items in \ + `not_selected`, so never retry those. `outcome_unknown` items may also have been written or \ + partially written. Inspect uncertain outcomes before retrying, follow each error's guidance, \ + and do not retry backend-wide failures in this prompt.", + op.past(), + successes.len(), + failures.len(), + not_attempted.len(), + ); + let mut body = Map::new(); + body.insert("requested_count".into(), json!(requested_count)); + body.insert("selected_count".into(), json!(indices.len())); + body.insert("not_selected".into(), json!(not_selected)); + body.insert(op.count_key().into(), json!(successes.len())); + body.insert("successes".into(), json!(successes)); + body.insert("failures".into(), json!(failures)); + body.insert("not_attempted".into(), json!(not_attempted)); + body.insert("next_step".into(), json!(next_step)); + let body = Value::Object(body); + let text = body.to_string(); + let mut result = CallToolResult::structured(body); + result.is_error = Some(has_errors); + // Models in some MCP clients cannot see structuredContent. + result.content = vec![ContentBlock::text(next_step), ContentBlock::text(text)]; + Ok(result) +} + +#[cfg(test)] +mod test { + use super::*; + + #[derive(Debug, Deserialize, JsonSchema)] + struct Item { + id: String, + } + + fn all() -> Selection { + Selection { + indices: None, + required: false, + } + } + + fn signal(code: tonic::Code) -> ErrorData { + crate::error::from_anyhow(tonic::Status::new(code, "failure").into()) + } + + #[test] + fn the_legacy_single_item_shape_is_rejected_with_a_migration_hint() { + let error = serde_json::from_value::>(json!({"id": "a"})).unwrap_err(); + assert!(error.to_string().contains("`items` array"), "{error}"); + let error = + serde_json::from_value::>(json!({"items": [], "id": "a"})).unwrap_err(); + assert!(error.to_string().contains("unknown field `id`"), "{error}"); + let call: BulkItems = + serde_json::from_value(json!({"items": [{"id": "a"}], "selected_indices": [0]})) + .unwrap(); + assert_eq!(call.items.len(), 1); + assert_eq!(call.selected_indices, Some(vec![0])); + } + + #[test] + fn the_schema_is_a_bounded_array_that_requires_items() { + let schema = serde_json::to_value(schemars::schema_for!(BulkItems)).unwrap(); + let items = &schema["properties"]["items"]; + assert_eq!(items["type"], "array"); + assert_eq!(items["minItems"], 1); + assert_eq!(items["maxItems"], 100); + assert_eq!(schema["required"], json!(["items"])); + assert_eq!(schema["additionalProperties"], false); + } + + #[test] + fn duplicate_targets_name_both_items() { + let items = ["a", "b", "a"].map(|id| Item { id: id.into() }); + let error = + reject_duplicate_targets(&items, "rule", |item| Some(item.id.clone())).unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + assert!(error.message.contains("item 2"), "{}", error.message); + assert!(error.message.contains("as item 0"), "{}", error.message); + assert!( + reject_duplicate_targets(&items[..2], "rule", |item| Some(item.id.clone())).is_ok() + ); + } + + #[test] + fn permission_errors_stop_creates_but_not_writes_to_existing_resources() { + let denied = signal(tonic::Code::PermissionDenied); + assert!(should_stop(Op::Create, &denied)); + for op in [Op::Update, Op::Archive, Op::Unarchive] { + assert!(!should_stop(op, &denied), "{op:?}"); + assert!(!should_stop( + op, + &ErrorData::resource_not_found("missing", None) + )); + assert!(!should_stop(op, &signal(tonic::Code::Aborted))); + for code in [ + tonic::Code::Unavailable, + tonic::Code::ResourceExhausted, + tonic::Code::DeadlineExceeded, + tonic::Code::Internal, + tonic::Code::Unauthenticated, + tonic::Code::Cancelled, + tonic::Code::Unknown, + ] { + assert!(should_stop(op, &signal(code)), "{op:?} {code:?}"); + } + } + } + + #[tokio::test] + async fn each_op_reports_its_own_count_and_verb() { + for (op, key, verb) in [ + (Op::Create, "created_count", "Created 2 items"), + (Op::Update, "updated_count", "Updated 2 items"), + (Op::Archive, "archived_count", "Archived 2 items"), + (Op::Unarchive, "unarchived_count", "Unarchived 2 items"), + ] { + let result = run(op, vec![0, 1], all(), false, |item| async move { + Ok(CallToolResult::structured(json!({"item": item}))) + }) + .await + .unwrap(); + let body = result.structured_content.unwrap(); + assert_eq!(body[key], 2, "{op:?}"); + let next_step = body["next_step"].as_str().unwrap(); + assert!(next_step.starts_with(verb), "{next_step}"); + assert!(next_step.contains("never retry those"), "{next_step}"); + } + } + + #[tokio::test] + async fn a_permission_failure_on_one_archive_does_not_stop_the_rest() { + let result = run( + Op::Archive, + vec![0, 1, 2], + Selection { + indices: Some(vec![0, 1, 2]), + required: true, + }, + false, + |item| async move { + if item == 1 { + Err(signal(tonic::Code::PermissionDenied)) + } else { + Ok(CallToolResult::structured(json!({"item": item}))) + } + }, + ) + .await + .unwrap(); + assert_eq!(result.is_error, Some(true)); + let body = result.structured_content.unwrap(); + assert_eq!(body["archived_count"], 2); + assert_eq!(body["failures"][0]["index"], 1); + assert_eq!(body["failures"][0]["outcome_unknown"], false); + assert_eq!(body["not_attempted"], json!([])); + } + + #[tokio::test] + async fn a_required_mask_fails_closed_for_every_op() { + for op in [Op::Update, Op::Archive, Op::Unarchive] { + let error = run( + op, + vec![0], + Selection { + indices: None, + required: true, + }, + false, + |_| async { panic!("no write may run without a mask") }, + ) + .await + .unwrap_err(); + assert!( + error.message.contains("nothing was written"), + "{}", + error.message + ); + } + } +} diff --git a/rust/crates/sift_mcp/src/tool/common/bulk_create.rs b/rust/crates/sift_mcp/src/tool/common/bulk_create.rs index 68fe30f72..f61e7adae 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk_create.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk_create.rs @@ -1,97 +1,16 @@ use std::future::Future; -use rmcp::{ - ErrorData, - model::{CallToolResult, ContentBlock, ErrorCode}, - schemars::{self, JsonSchema}, -}; -use serde::{Deserialize, de::DeserializeOwned}; -use serde_json::{Value, json}; +use crate::error::McpResult; -use crate::{error::McpResult, service::batch}; - -pub const MAX_CREATE_ITEMS: usize = 100; - -/// Shared item list and approval mask for every create tool. -#[derive(Debug, Deserialize, JsonSchema)] -#[serde(deny_unknown_fields)] -pub struct CreateItems { - /// 1–100 items to create. Pass one item to create one resource. - #[schemars(length(min = 1, max = 100))] - #[schemars(with = "Vec")] - pub items: Vec, - /// Approval host's zero-based selection of `items` to create. Omit when proposing. - /// An empty selection performs no writes. - pub selected_indices: Option>, - #[serde(skip)] - #[schemars(skip)] - _item: std::marker::PhantomData, -} - -/// Prefix an item's validation error with its index in `items`. -pub fn at_item(index: usize, error: ErrorData) -> ErrorData { - ErrorData::new( - error.code, - format!("`items` item {index}: {}", error.message), - error.data, - ) -} - -pub fn parse_items( - items: Vec, -) -> Result, ErrorData> { - if items.is_empty() || items.len() > MAX_CREATE_ITEMS { - return Err(ErrorData::invalid_params( - format!("`items` must contain 1–{MAX_CREATE_ITEMS} items"), - None, - )); - } - let schema = schemars::schema_for!(T); - items - .into_iter() - .enumerate() - .map(|(index, item)| { - if let Some(fields) = schema.get("properties").and_then(Value::as_object) - && let Some(object) = item.as_object() - && let Some(unknown) = object.keys().find(|key| !fields.contains_key(*key)) - { - return Err(ErrorData::invalid_params( - format!("`items` item {index}: unknown field `{unknown}`"), - None, - )); - } - serde_json::from_value(item).map_err(|error| { - ErrorData::invalid_params(format!("`items` item {index}: {error}"), None) - }) - }) - .collect() -} - -fn reason(error: &ErrorData) -> Option<&str> { - error.data.as_ref()?.get("reason")?.as_str() -} - -fn should_stop(error: &ErrorData) -> bool { - error.code != ErrorCode::INVALID_PARAMS - && error.code != ErrorCode::RESOURCE_NOT_FOUND - && !matches!(reason(error), Some("AlreadyExists" | "Conflict")) -} +pub use super::bulk::{BulkItems as CreateItems, Selection, at_item, parse_items}; +use super::bulk::{Op, run}; pub async fn create_many(items: Vec, selection: Selection, operation: F) -> McpResult where F: FnMut(T) -> Fut, Fut: Future, { - create_many_with_partial_writes(items, selection, operation, false).await -} - -/// The approval mask and whether this server requires one. -#[derive(Debug, Clone)] -pub struct Selection { - pub indices: Option>, - /// Set when an approval host runs this server (`sift-cli mcp --chat`). A missing mask - /// then fails closed so a host regression cannot create unapproved items. - pub required: bool, + run(Op::Create, items, selection, false, operation).await } pub async fn create_many_with_partial_writes( @@ -104,95 +23,7 @@ where F: FnMut(T) -> Fut, Fut: Future, { - let requested_count = items.len(); - let selected_indices = match selection.indices { - Some(indices) => indices, - None if selection.required => { - return Err(ErrorData::invalid_params( - "Creates require explicit `selected_indices` from the approval host; no unapproved items were created", - None, - )); - } - None => (0..requested_count).collect(), - }; - let mut selected = vec![false; requested_count]; - for &index in &selected_indices { - if index >= requested_count || selected[index] { - return Err(ErrorData::invalid_params( - "`selected_indices` must contain unique, in-range input indices", - None, - )); - } - selected[index] = true; - } - let indices = (0..requested_count) - .filter(|&index| selected[index]) - .collect::>(); - let not_selected = (0..requested_count) - .filter(|&index| !selected[index]) - .collect::>(); - let items = items - .into_iter() - .enumerate() - .filter_map(|(index, item)| selected[index].then_some(item)) - .collect(); - let outcome = batch::execute(items, operation, should_stop).await; - let not_attempted = outcome - .not_attempted - .into_iter() - .map(|index| indices[index]) - .collect::>(); - let successes = outcome - .successes - .into_iter() - .map( - |(index, result)| json!({"index": indices[index], "result": result.structured_content}), - ) - .collect::>(); - let failures = outcome - .failures - .into_iter() - .map(|(index, error)| { - // An interrupted write or a composite task can commit before returning - // an error. Never instruct callers to blindly replay a failed create. - let outcome_unknown = partial_writes - || (error.code != ErrorCode::INVALID_PARAMS - && error.code != ErrorCode::RESOURCE_NOT_FOUND - && !matches!( - reason(&error), - Some("AlreadyExists" | "PermissionDenied" | "Unauthenticated") - )); - json!({"index": indices[index], "error": error, "outcome_unknown": outcome_unknown}) - }) - .collect::>(); - let has_errors = !failures.is_empty() || !not_attempted.is_empty(); - let next_step = format!( - "Created {} items; {} failed; {} not attempted. \ - On success, confirm in one short sentence using resource names or links; omit unrequested metadata, \ - internal indices/masks, and approval/retry narration. Report actionable warnings and failures. \ - Never replay the batch: successful writes are committed; `outcome_unknown` items may also exist \ - or be partially created. Inspect uncertain outcomes before retrying, follow each error's guidance, \ - and do not retry backend-wide failures in this prompt.", - successes.len(), - failures.len(), - not_attempted.len(), - ); - let body = json!({ - "requested_count": requested_count, - "selected_count": indices.len(), - "not_selected": not_selected, - "created_count": successes.len(), - "successes": successes, - "failures": failures, - "not_attempted": not_attempted, - "next_step": next_step, - }); - let text = body.to_string(); - let mut result = CallToolResult::structured(body); - result.is_error = Some(has_errors); - // Models in some MCP clients cannot see structuredContent. - result.content = vec![ContentBlock::text(next_step), ContentBlock::text(text)]; - Ok(result) + run(Op::Create, items, selection, partial_writes, operation).await } #[cfg(test)] @@ -205,8 +36,20 @@ mod retry_test; #[cfg(test)] mod test { + use rmcp::{ + ErrorData, + model::CallToolResult, + schemars::{self, JsonSchema}, + }; + use serde::Deserialize; + use serde_json::{Value, json}; + use super::*; + fn should_stop(error: &ErrorData) -> bool { + super::super::bulk::should_stop(Op::Create, error) + } + #[derive(Debug, Deserialize, JsonSchema)] struct Item { name: String, diff --git a/rust/crates/sift_mcp/src/tool/common/mod.rs b/rust/crates/sift_mcp/src/tool/common/mod.rs index edc64f746..f400c5ad2 100644 --- a/rust/crates/sift_mcp/src/tool/common/mod.rs +++ b/rust/crates/sift_mcp/src/tool/common/mod.rs @@ -1,3 +1,4 @@ +pub(crate) mod bulk; pub(crate) mod bulk_create; use rmcp::ErrorData; From 766c6e906fcb1a05514fa60200a6d31efed255ae Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:01:09 -0800 Subject: [PATCH 02/19] rust(feat): take bulk items in the rule update and archive tools --- rust/crates/sift_mcp/src/service/rules/mod.rs | 22 +- .../crates/sift_mcp/src/service/rules/test.rs | 27 +- rust/crates/sift_mcp/src/tool/rules/mod.rs | 375 +++++++++++------- rust/crates/sift_mcp/src/tool/rules/test.rs | 253 ++++++++++++ 4 files changed, 530 insertions(+), 147 deletions(-) diff --git a/rust/crates/sift_mcp/src/service/rules/mod.rs b/rust/crates/sift_mcp/src/service/rules/mod.rs index 2ede41561..78734bcda 100644 --- a/rust/crates/sift_mcp/src/service/rules/mod.rs +++ b/rust/crates/sift_mcp/src/service/rules/mod.rs @@ -10,6 +10,7 @@ use sift_rs::{ UpdateConditionRequest, UpdateRuleRequest, rule_service_client::RuleServiceClient, }, }; +use tonic::Status; /// A partial set of changes to apply to an existing rule. Every field is /// optional; `None` means "leave unchanged". This is what makes `update_rule` @@ -134,10 +135,7 @@ impl RuleService { /// fields set in `changes`, and sends the merged definition back. Fields not /// named in `changes` are preserved. Returns the `rule_id`. pub async fn update_rule(&self, rule_id: String, changes: RuleUpdate) -> Result { - let current = self - .get_rule(rule_id.clone()) - .await? - .ok_or_else(|| anyhow::anyhow!("rule '{rule_id}' not found"))?; + let current = self.find_rule(rule_id, String::new()).await?; let mut update = rule_to_update_request(current); @@ -195,19 +193,26 @@ impl RuleService { Ok(resp.rule_id) } - /// Retrieves the latest version of a rule by id, or `None` if it does not exist. - async fn get_rule(&self, rule_id: String) -> Result> { + /// Retrieves the latest version of a rule by `rule_id` or `client_key` (the + /// caller sets exactly one). A missing rule is a `NotFound` status. + pub async fn find_rule(&self, rule_id: String, client_key: String) -> Result { let channel = self.channel.clone(); + let target = if rule_id.is_empty() { + format!("client key '{client_key}'") + } else { + format!("id '{rule_id}'") + }; let resp = with_retry(&self.policy, move || { let channel = channel.clone(); let rule_id = rule_id.clone(); + let client_key = client_key.clone(); async move { let mut client = RuleServiceClient::new(channel); client .get_rule(GetRuleRequest { rule_id, - client_key: String::new(), + client_key, }) .await .map(|resp| resp.into_inner()) @@ -216,7 +221,8 @@ impl RuleService { .await .context("failed to fetch rule")?; - Ok(resp.rule) + resp.rule + .ok_or_else(|| Status::not_found(format!("no rule with {target}")).into()) } /// Lists the version history for a single rule. diff --git a/rust/crates/sift_mcp/src/service/rules/test.rs b/rust/crates/sift_mcp/src/service/rules/test.rs index 275e405d2..d4725c231 100644 --- a/rust/crates/sift_mcp/src/service/rules/test.rs +++ b/rust/crates/sift_mcp/src/service/rules/test.rs @@ -353,7 +353,32 @@ async fn update_rule_errors_when_rule_missing() { .await .expect_err("expected error"); - assert!(err.to_string().contains("not found")); + let status = err.downcast_ref::().expect("a gRPC status"); + assert_eq!(status.code(), tonic::Code::NotFound); +} + +#[tokio::test] +async fn find_rule_looks_up_by_client_key() { + let mut mock = MockRuleServiceImpl::new(); + mock.expect_get_rule() + .withf(|req| req.get_ref().client_key == "ck-1" && req.get_ref().rule_id.is_empty()) + .returning(|_| { + Ok(Response::new(GetRuleResponse { + rule: Some(Rule { + rule_id: "rule-1".into(), + ..Default::default() + }), + })) + }); + + let (service, _h) = service_with_mock(mock).await; + + let rule = service + .find_rule(String::new(), "ck-1".to_string()) + .await + .expect("find_rule failed"); + + assert_eq!(rule.rule_id, "rule-1"); } #[tokio::test] diff --git a/rust/crates/sift_mcp/src/tool/rules/mod.rs b/rust/crates/sift_mcp/src/tool/rules/mod.rs index 170fcb0c7..46a1fd27f 100644 --- a/rust/crates/sift_mcp/src/tool/rules/mod.rs +++ b/rust/crates/sift_mcp/src/tool/rules/mod.rs @@ -8,7 +8,10 @@ use rmcp::{ use serde::Deserialize; use sift_rs::rules::v1::{UpdateConditionRequest, UpdateRuleRequest}; -use crate::tool::common::bulk_create; +use crate::tool::common::{ + bulk::{self, BulkItems, Op}, + bulk_create, +}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, @@ -32,7 +35,7 @@ pub struct RuleVersionListParams { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateRuleParams { +pub struct UpdateRuleItem { rule_id: String, name: Option, description: Option, @@ -44,7 +47,7 @@ pub struct UpdateRuleParams { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct RuleArchiveParams { +pub struct RuleArchiveItem { rule_id: Option, client_key: Option, } @@ -326,38 +329,54 @@ impl SiftMcpServer { #[tool( name = "update_rule", description = " - Update specific fields of an existing Sift rule, identified by `rule_id`. This is a WRITE. Only the - fields you set are changed; everything else on the rule is preserved. The tool fetches the current - rule, overlays your changes, and saves the result, so it is safe to call with just the fields you - want to modify. + Update specific fields of one or more existing Sift rules. This is a WRITE. Only the fields you + set on an item are changed; everything else on that rule is preserved. The tool fetches the + current rule, overlays your changes, and saves the result, so each item needs only the fields + you want to modify. Output: - - `{ \"rule_id\": \"\", \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_url` is the - rule's Sift web link, or null when the host can't be derived. + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }` per updated item, where + `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, + outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any + failure sets `isError`. Each `result` is: + - `{ \"rule_id\": \"\", \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_url` is + the rule's Sift web link, or null when the host can't be derived. Parameters: - - `rule_id`: required. The rule to update. - - `name`: optional. New rule name. - - `description`: optional. New description. - - `version_notes`: optional. Notes attached to this update in the rule's version history. - - `is_live_evaluation_enabled`: optional. Enable or disable live evaluation. - - `asset_ids`: optional. Replaces the rule's set of target asset ids. - - `tag_ids`: optional. Replaces the rule's set of target tag ids. - - `conditions_json`: optional. Replaces the rule's conditions. A JSON array matching the - `conditions` field of `create_rule`'s `rule_json`. Omit it to keep the existing conditions - unchanged — only provide it when you intend to rewrite the rule's logic. - - At least one field besides `rule_id` must be set. + - `items`: required array of 1–100 objects, one per rule. Pass one item to update one + rule. Each item takes: + - `rule_id`: required. The rule to update. + - `name`: optional. New rule name. + - `description`: optional. New description. + - `version_notes`: optional. Notes attached to this update in the rule's version history. + - `is_live_evaluation_enabled`: optional. Enable or disable live evaluation. + - `asset_ids`: optional. Replaces the rule's set of target asset ids. + - `tag_ids`: optional. Replaces the rule's set of target tag ids. + - `conditions_json`: optional. Replaces the rule's conditions. A JSON array matching the + `conditions` field of `create_rule`'s `rule_json`. Omit it to keep the existing conditions + unchanged — only provide it when you intend to rewrite the rule's logic. + - At least one field besides `rule_id` must be set. + - `selected_indices`: zero-based positions in `items` to update. An approval host sets this + from the user's per-item decisions; omit it when proposing. Omitted means every item, + except in a Sift chat pod, where a missing value is rejected. `[]` updates nothing. Errors: - - `INVALID_PARAMS` if no field besides `rule_id` is set, or if `conditions_json` is not a valid - conditions array. - - `RESOURCE_NOT_FOUND` if no rule has the given `rule_id`. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two + items target the same `rule_id`, or if an item has an unknown field, an empty `rule_id`, no + field besides `rule_id`, or a `conditions_json` that is not a valid conditions array. The + message names the item index. + - Once writing starts, an item-specific error (`RESOURCE_NOT_FOUND` for a missing rule, + invalid values, or no permission on that rule) fails only that item. A backend-wide error + stops the remaining items; earlier updates stay committed. Guidance: - Prefer this over recreating a rule. To change only the name, pass `rule_id` and `name`; the conditions, metadata, and asset targeting are preserved automatically. - Confirm the intended changes with the user before calling. + - Items run sequentially, one update per item; this is not an atomic batch. Never replay + the whole call after a partial failure. Retry only failed items. + - Use one call with several items, not repeated calls, when updating several rules. ", annotations( title = "rules/update_rule", @@ -366,49 +385,34 @@ impl SiftMcpServer { idempotent_hint = true, ) )] - pub async fn update_rule(&self, params: Parameters) -> error::McpResult { + pub async fn update_rule( + &self, + params: Parameters>, + ) -> error::McpResult { self.require_destructive()?; - - let Parameters(UpdateRuleParams { - rule_id, - name, - description, - version_notes, - is_live_evaluation_enabled, - asset_ids, - tag_ids, - conditions_json, + let Parameters(BulkItems { + items, + selected_indices, + .. }) = params; + let items = bulk::parse_items::(items)?; + let updates = items + .into_iter() + .enumerate() + .map(|(index, item)| rule_update(item).map_err(|error| bulk::at_item(index, error))) + .collect::, _>>()?; + bulk::reject_duplicate_targets(&updates, "rule", |(rule_id, _)| Some(rule_id.clone()))?; + bulk::run( + Op::Update, + updates, + self.selection(selected_indices), + false, + |(rule_id, changes)| self.update_rule_item(rule_id, changes), + ) + .await + } - let conditions = conditions_json - .as_deref() - .map(parse_conditions) - .transpose()?; - - let changes = RuleUpdate { - name, - description, - version_notes, - is_live_evaluation_enabled, - asset_ids, - tag_ids, - conditions, - }; - - let nothing_to_change = changes.name.is_none() - && changes.description.is_none() - && changes.version_notes.is_none() - && changes.is_live_evaluation_enabled.is_none() - && changes.asset_ids.is_none() - && changes.tag_ids.is_none() - && changes.conditions.is_none(); - if nothing_to_change { - return Err(ErrorData::invalid_params( - "at least one field besides `rule_id` must be set", - None, - )); - } - + async fn update_rule_item(&self, rule_id: String, changes: RuleUpdate) -> error::McpResult { let rule_name = changes.name.clone(); let rule_id = self .rule_service @@ -418,9 +422,7 @@ impl SiftMcpServer { let rule_url = self.url_service.build_rule_url(&rule_id).ok(); let next_step = format!( - "Updated rule `{rule_id}`.{} Tell the user the update succeeded. If they haven't \ - indicated a next step, offer to confirm it with `list_rules` \ - (filter `rule_id == \"{rule_id}\"`).", + "Updated rule `{rule_id}`.{}", url_clause("rule", rule_name.as_deref(), rule_url.as_deref()), ); @@ -436,25 +438,40 @@ impl SiftMcpServer { #[tool( name = "archive_rule", description = " - Archive a Sift rule so it no longer evaluates. This is a WRITE. Reversible with `unarchive_rule`. + Archive one or more Sift rules so they no longer evaluate. This is a WRITE. Reversible with + `unarchive_rule`. Output: - - `{ \"archived\": true, \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_url` is the - rule's Sift web link when targeted by `rule_id`; null when targeted by `client_key` or when the - host can't be derived. + - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }` per archived item, where + `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, + outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any + failure sets `isError`. Each `result` is: + - `{ \"rule_id\": \"\", \"name\": string, \"archived\": true, \"rule_url\": string|null }`. + `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `rule_id`: optional. The id of the rule to archive. - - `client_key`: optional. The caller-defined key of the rule to archive. - - Exactly one of `rule_id` or `client_key` must be set. + - `items`: required array of 1–100 objects, one per rule. Pass one item to archive one + rule. Each item takes exactly one of: + - `rule_id`: the id of the rule to archive. + - `client_key`: the caller-defined key of the rule to archive. + - `selected_indices`: zero-based positions in `items` to archive. An approval host sets this + from the user's per-item decisions; omit it when proposing. Omitted means every item, + except in a Sift chat pod, where a missing value is rejected. `[]` archives nothing. Errors: - - `INVALID_PARAMS` if neither or both of `rule_id` and `client_key` are set. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two + items name the same rule id or client key, or if an item sets neither or both of `rule_id` + and `client_key`. The message names the item index. + - Once writing starts, an item-specific error (`RESOURCE_NOT_FOUND` when no rule matches, + an external rule, or no permission on that rule) fails only that item. A backend-wide error + stops the remaining items; earlier archives stay committed. Guidance: - Archiving stops live evaluation but does not delete the rule; it can be restored with - `unarchive_rule`. Confirm the target rule with the user before calling. + `unarchive_rule`. Confirm the target rules with the user before calling. + - A rule that is already archived counts as a success. + - Use one call with several items, not repeated calls, when archiving several rules. ", annotations( title = "rules/archive_rule", @@ -463,59 +480,49 @@ impl SiftMcpServer { idempotent_hint = true, ) )] - pub async fn archive_rule(&self, params: Parameters) -> error::McpResult { + pub async fn archive_rule( + &self, + params: Parameters>, + ) -> error::McpResult { self.require_destructive()?; - - let Parameters(RuleArchiveParams { - rule_id, - client_key, - }) = params; - - let (rule_id, client_key) = rule_identifier(rule_id, client_key)?; - - let rule_url = rule_url_for(self, &rule_id); - - self.rule_service - .archive_rule(rule_id, client_key) - .await - .map_err(from_anyhow)?; - - let next_step = format!( - "Rule archived.{} Tell the user it is archived and will no longer evaluate, \ - and that `unarchive_rule` restores it.", - url_clause("rule", None, rule_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ - "archived": true, - "rule_url": rule_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + self.set_rules_archived(params, Op::Archive).await } #[tool( name = "unarchive_rule", description = " - Restore a previously archived Sift rule so it evaluates again. This is a WRITE. + Restore one or more previously archived Sift rules so they evaluate again. This is a WRITE. Output: - - `{ \"unarchived\": true, \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_url` is the - rule's Sift web link when targeted by `rule_id`; null when targeted by `client_key` or when the - host can't be derived. + - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }` per unarchived item, + where `index` is the item's zero-based position in `items`. `failures` holds `{ index, + error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. + Any failure sets `isError`. Each `result` is: + - `{ \"rule_id\": \"\", \"name\": string, \"unarchived\": true, \"rule_url\": string|null }`. + `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `rule_id`: optional. The id of the rule to unarchive. - - `client_key`: optional. The caller-defined key of the rule to unarchive. - - Exactly one of `rule_id` or `client_key` must be set. + - `items`: required array of 1–100 objects, one per rule. Pass one item to unarchive one + rule. Each item takes exactly one of: + - `rule_id`: the id of the rule to unarchive. + - `client_key`: the caller-defined key of the rule to unarchive. + - `selected_indices`: zero-based positions in `items` to unarchive. An approval host sets + this from the user's per-item decisions; omit it when proposing. Omitted means every item, + except in a Sift chat pod, where a missing value is rejected. `[]` unarchives nothing. Errors: - - `INVALID_PARAMS` if neither or both of `rule_id` and `client_key` are set. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two + items name the same rule id or client key, or if an item sets neither or both of `rule_id` + and `client_key`. The message names the item index. + - Once writing starts, an item-specific error (`RESOURCE_NOT_FOUND` when no rule matches, + an external rule, or no permission on that rule) fails only that item. A backend-wide error + stops the remaining items; earlier unarchives stay committed. Guidance: - - Confirm the target rule with the user before calling. + - Confirm the target rules with the user before calling. + - A rule that is not archived counts as a success. + - Use one call with several items, not repeated calls, when unarchiving several rules. ", annotations( title = "rules/unarchive_rule", @@ -524,36 +531,128 @@ impl SiftMcpServer { idempotent_hint = true, ) )] - pub async fn unarchive_rule(&self, params: Parameters) -> error::McpResult { + pub async fn unarchive_rule( + &self, + params: Parameters>, + ) -> error::McpResult { self.require_destructive()?; + self.set_rules_archived(params, Op::Unarchive).await + } - let Parameters(RuleArchiveParams { - rule_id, - client_key, + async fn set_rules_archived( + &self, + params: Parameters>, + op: Op, + ) -> error::McpResult { + let Parameters(BulkItems { + items, + selected_indices, + .. }) = params; + let targets = bulk::parse_items::(items)? + .into_iter() + .enumerate() + .map(|(index, item)| { + rule_identifier(item.rule_id, item.client_key) + .map_err(|error| bulk::at_item(index, error)) + }) + .collect::, _>>()?; + bulk::reject_duplicate_targets(&targets, "rule", |(rule_id, client_key)| { + Some(if rule_id.is_empty() { + format!("client_key:{client_key}") + } else { + format!("rule_id:{rule_id}") + }) + })?; + bulk::run( + op, + targets, + self.selection(selected_indices), + false, + |(rule_id, client_key)| self.set_rule_archived(rule_id, client_key, op), + ) + .await + } - let (rule_id, client_key) = rule_identifier(rule_id, client_key)?; - - let rule_url = rule_url_for(self, &rule_id); - - self.rule_service - .unarchive_rule(rule_id, client_key) + /// Resolve the rule first: the archive RPCs succeed silently when nothing + /// matches, which would report a typo as archived. + async fn set_rule_archived( + &self, + rule_id: String, + client_key: String, + op: Op, + ) -> error::McpResult { + let rule = self + .rule_service + .find_rule(rule_id, client_key) .await .map_err(from_anyhow)?; + let archived = op == Op::Archive; + let write = if archived { + self.rule_service + .archive_rule(rule.rule_id.clone(), String::new()) + .await + } else { + self.rule_service + .unarchive_rule(rule.rule_id.clone(), String::new()) + .await + }; + write.map_err(from_anyhow)?; + + let state = if archived { "archived" } else { "unarchived" }; + Ok(CallToolResult::structured(serde_json::json!({ + "rule_id": rule.rule_id, + "name": rule.name, + state: true, + "rule_url": rule_url_for(self, &rule.rule_id), + }))) + } +} - let next_step = format!( - "Rule unarchived.{} Tell the user it is restored and will evaluate again.", - url_clause("rule", None, rule_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ - "unarchived": true, - "rule_url": rule_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) +fn rule_update(item: UpdateRuleItem) -> Result<(String, RuleUpdate), ErrorData> { + let UpdateRuleItem { + rule_id, + name, + description, + version_notes, + is_live_evaluation_enabled, + asset_ids, + tag_ids, + conditions_json, + } = item; + if rule_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`rule_id` must not be empty", + None, + )); + } + let conditions = conditions_json + .as_deref() + .map(parse_conditions) + .transpose()?; + let changes = RuleUpdate { + name, + description, + version_notes, + is_live_evaluation_enabled, + asset_ids, + tag_ids, + conditions, + }; + let nothing_to_change = changes.name.is_none() + && changes.description.is_none() + && changes.version_notes.is_none() + && changes.is_live_evaluation_enabled.is_none() + && changes.asset_ids.is_none() + && changes.tag_ids.is_none() + && changes.conditions.is_none(); + if nothing_to_change { + return Err(ErrorData::invalid_params( + "at least one field besides `rule_id` must be set", + None, + )); } + Ok((rule_id, changes)) } /// Build the web URL for a rule when its `rule_id` is known. Returns `None` when diff --git a/rust/crates/sift_mcp/src/tool/rules/test.rs b/rust/crates/sift_mcp/src/tool/rules/test.rs index c07f453a8..8c103b972 100644 --- a/rust/crates/sift_mcp/src/tool/rules/test.rs +++ b/rust/crates/sift_mcp/src/tool/rules/test.rs @@ -44,3 +44,256 @@ fn parse_rule_definition_rejects_invalid_json() { let err = parse_rule_definition("not json").expect_err("should reject"); assert_eq!(err.code, ErrorCode::INVALID_PARAMS); } + +mod bulk { + use std::sync::{Arc, Mutex}; + + use rmcp::{handler::server::wrapper::Parameters, model::ErrorCode}; + use serde_json::{Value, json}; + use sift_rs::rules::v1::{ + ArchiveRuleResponse, GetRuleResponse, Rule, UpdateRuleResponse, + rule_service_server::RuleServiceServer, + }; + use sift_test_util::{grpc::memory_sift_channel, mock::rules::v1::MockRuleServiceImpl}; + use tonic::{Response, Status, transport::Server}; + + use crate::{server::SiftMcpServer, tool::common::test_support::structured}; + + async fn server(mock: MockRuleServiceImpl) -> (SiftMcpServer, tokio::task::JoinHandle<()>) { + let (client, incoming) = tokio::io::duplex(1024); + let channel = memory_sift_channel(client).await; + let handle = tokio::spawn(async move { + Server::builder() + .add_service(RuleServiceServer::new(mock)) + .serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(incoming))) + .await + .unwrap(); + }); + let server = SiftMcpServer::new(channel, "https://app.test.local".into(), false, true) + .with_require_selection(true); + (server, handle) + } + + fn params(value: Value) -> Parameters { + Parameters(serde_json::from_value(value).unwrap()) + } + + fn found(rule_id: &str) -> Result, Status> { + Ok(Response::new(GetRuleResponse { + rule: Some(Rule { + rule_id: rule_id.into(), + name: format!("name-{rule_id}"), + ..Default::default() + }), + })) + } + + #[tokio::test] + async fn archiving_ten_rules_reports_each_item_and_skips_rejected_ones() { + let archived = Arc::new(Mutex::new(Vec::new())); + let mut mock = MockRuleServiceImpl::new(); + mock.expect_get_rule().returning(|request| { + let rule_id = request.get_ref().rule_id.clone(); + if rule_id == "rule-5" { + Err(Status::not_found("rule not found")) + } else { + found(&rule_id) + } + }); + let seen = archived.clone(); + mock.expect_archive_rule().returning(move |request| { + seen.lock().unwrap().push(request.get_ref().rule_id.clone()); + Ok(Response::new(ArchiveRuleResponse {})) + }); + let (server, handle) = server(mock).await; + let items = (0..10) + .map(|i| json!({"rule_id": format!("rule-{i}")})) + .collect::>(); + let selected = (0..10).filter(|&i| i != 3).collect::>(); + let result = server + .archive_rule(params( + json!({"items": items, "selected_indices": selected}), + )) + .await + .unwrap(); + assert_eq!(result.is_error, Some(true)); + let body = structured(result); + assert_eq!(body["archived_count"], 8); + assert_eq!(body["not_selected"], json!([3])); + assert_eq!(body["not_attempted"], json!([])); + assert_eq!(body["failures"][0]["index"], 5); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 + ); + assert_eq!(body["successes"][0]["result"]["name"], "name-rule-0"); + assert_eq!(body["successes"][0]["result"]["archived"], true); + let archived = archived.lock().unwrap().clone(); + assert_eq!(archived.len(), 8); + assert!(!archived.contains(&"rule-3".to_string())); + assert!(!archived.contains(&"rule-5".to_string())); + handle.abort(); + } + + #[tokio::test] + async fn a_client_key_resolves_to_its_rule_id_before_unarchiving() { + let mut mock = MockRuleServiceImpl::new(); + mock.expect_get_rule() + .withf(|request| request.get_ref().client_key == "ck-1") + .returning(|_| found("rule-1")); + mock.expect_unarchive_rule() + .withf(|request| { + request.get_ref().rule_id == "rule-1" && request.get_ref().client_key.is_empty() + }) + .times(1) + .returning(|_| Ok(Response::new(Default::default()))); + let (server, handle) = server(mock).await; + let body = structured( + server + .unarchive_rule(params( + json!({"items": [{"client_key": "ck-1"}], "selected_indices": [0]}), + )) + .await + .unwrap(), + ); + assert_eq!(body["unarchived_count"], 1); + assert_eq!( + body["successes"][0]["result"]["rule_url"], + "https://app.test.local/rules/rule-1" + ); + handle.abort(); + } + + #[tokio::test] + async fn permission_denied_fails_one_item_and_unavailable_stops_the_rest() { + let mut mock = MockRuleServiceImpl::new(); + mock.expect_get_rule() + .returning(|request| match request.get_ref().rule_id.as_str() { + "denied" => Err(Status::permission_denied("no edit permission")), + "down" => Err(Status::unavailable("backend down")), + rule_id => found(rule_id), + }); + mock.expect_archive_rule() + .times(1) + .returning(|_| Ok(Response::new(ArchiveRuleResponse {}))); + let (server, handle) = server(mock).await; + let items = ["denied", "ok", "down", "never"].map(|id| json!({"rule_id": id})); + let body = structured( + server + .archive_rule(params( + json!({"items": items, "selected_indices": [0, 1, 2, 3]}), + )) + .await + .unwrap(), + ); + assert_eq!(body["archived_count"], 1); + assert_eq!(body["failures"][0]["index"], 0); + assert_eq!( + body["failures"][0]["error"]["data"]["reason"], + "PermissionDenied" + ); + assert_eq!(body["failures"][1]["index"], 2); + assert_eq!(body["not_attempted"], json!([3])); + handle.abort(); + } + + #[tokio::test] + async fn invalid_or_duplicate_items_fail_before_any_rpc() { + let (server, handle) = server(MockRuleServiceImpl::new()).await; + for (items, needle) in [ + (json!([{"rule_id": "a"}, {"rule_id": "a"}]), "item 1"), + ( + json!([{"rule_id": "a"}, {"client_key": "k", "rule_id": "b"}]), + "item 1", + ), + (json!([{}]), "item 0"), + ( + json!([{"rule_id": "a", "typo": true}]), + "unknown field `typo`", + ), + ] { + let error = server + .archive_rule(params(json!({"items": items, "selected_indices": [0]}))) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + assert!(error.message.contains(needle), "{}", error.message); + } + for (items, needle) in [ + ( + json!([{"rule_id": "a", "name": "x"}, {"rule_id": "a", "name": "y"}]), + "item 1", + ), + ( + json!([{"rule_id": "a", "name": "x"}, {"rule_id": "b"}]), + "item 1", + ), + (json!([{"rule_id": " ", "name": "x"}]), "item 0"), + ( + json!([{"rule_id": "a", "conditions_json": "nope"}]), + "item 0", + ), + ] { + let error = server + .update_rule(params(json!({"items": items, "selected_indices": [0]}))) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + assert!(error.message.contains(needle), "{}", error.message); + } + handle.abort(); + } + + #[tokio::test] + async fn the_chat_host_must_send_a_mask() { + let (server, handle) = server(MockRuleServiceImpl::new()).await; + let error = server + .archive_rule(params(json!({"items": [{"rule_id": "a"}]}))) + .await + .unwrap_err(); + assert!( + error.message.contains("nothing was written"), + "{}", + error.message + ); + handle.abort(); + } + + #[test] + fn the_legacy_single_rule_shape_is_rejected() { + let error = serde_json::from_value::< + crate::tool::common::bulk::BulkItems, + >(json!({"rule_id": "a"})) + .unwrap_err(); + assert!(error.to_string().contains("`items`"), "{error}"); + } + + #[tokio::test] + async fn update_rule_runs_one_get_and_update_per_selected_item() { + let mut mock = MockRuleServiceImpl::new(); + mock.expect_get_rule() + .times(2) + .returning(|request| found(&request.get_ref().rule_id)); + mock.expect_update_rule() + .times(2) + .withf(|request| request.get_ref().name == "renamed") + .returning(|request| { + Ok(Response::new(UpdateRuleResponse { + rule_id: request.get_ref().rule_id.clone().unwrap_or_default(), + })) + }); + let (server, handle) = server(mock).await; + let items = ["a", "b", "c"].map(|id| json!({"rule_id": id, "name": "renamed"})); + let body = structured( + server + .update_rule(params(json!({"items": items, "selected_indices": [0, 2]}))) + .await + .unwrap(), + ); + assert_eq!(body["updated_count"], 2); + assert_eq!(body["successes"][1]["index"], 2); + assert_eq!(body["successes"][1]["result"]["rule_id"], "c"); + assert_eq!(body["not_selected"], json!([1])); + handle.abort(); + } +} From 4e8d7a0711263c6ea89907ff94f9029e99713750 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:02:37 -0800 Subject: [PATCH 03/19] docs: describe bulk update and archive tools in the skill and changelog --- rust/crates/sift_cli/CHANGELOG.md | 14 ++++++++++++++ rust/crates/sift_cli/assets/skills/sift/SKILL.md | 11 +++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/rust/crates/sift_cli/CHANGELOG.md b/rust/crates/sift_cli/CHANGELOG.md index 5504985f9..3aff0e80e 100644 --- a/rust/crates/sift_cli/CHANGELOG.md +++ b/rust/crates/sift_cli/CHANGELOG.md @@ -7,6 +7,20 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### What's New +- MCP update, archive and unarchive tools now write 1 to 100 resources per + call: `update_rule`, `archive_rule`, `unarchive_rule`, `update_artifact`, + `archive_artifact`, `unarchive_artifact`, `update_run`, `update_asset`, + `update_calculated_channel`, `archive_calculated_channel`, + `unarchive_calculated_channel`, `update_user_defined_function`, + `archive_user_defined_function`, `unarchive_user_defined_function`, + `update_report`, and `update_report_template`. Each takes an `items` array + of the fields it took before. This is a breaking change for existing + callers; pass a one-element `items` list for one resource. Results use the + same per-item `successes`, `failures`, and `not_attempted` lists as creates. + A missing or forbidden resource fails only its own item. +- `archive_rule` and `unarchive_rule` now report a missing rule as not found + instead of reporting success. + ## [v0.9.0] - November 12, 2026 ### What's New diff --git a/rust/crates/sift_cli/assets/skills/sift/SKILL.md b/rust/crates/sift_cli/assets/skills/sift/SKILL.md index a99e34b5d..41b4edf4b 100644 --- a/rust/crates/sift_cli/assets/skills/sift/SKILL.md +++ b/rust/crates/sift_cli/assets/skills/sift/SKILL.md @@ -117,9 +117,11 @@ tool. read-only mode and need no confirmation. - **Links:** `explore_url`. - **Docs:** `search_docs`. -- **Creates:** every `create_*` write takes an `items` array of 1 to 100 - objects. Pass one item to create one resource, or several to create a batch - in one call. Results report each item by its index in `items`. +- **Bulk writes:** every `create_*` write, and every `update_*`, `archive_*` + and `unarchive_*` write except `update_annotation`, takes an `items` array of + 1 to 100 objects. Pass one item to write one resource, or several to write a + batch in one call. Results report each item by its index in `items`. Never + replay a whole call after a partial failure; retry only the failed items. - **Writes:** `create_rule`, `update_rule`, `archive_rule`, `unarchive_rule`, `create_annotation`, `update_annotation`, `create_report`, `update_report`, `create_report_template`, `update_report_template`, @@ -128,7 +130,8 @@ tool. `create_user_defined_function`, `update_user_defined_function`, `archive_user_defined_function`, `unarchive_user_defined_function`, `create_test_report`, `append_test_measurements`, `update_asset`, - `update_run`, `create_artifact`, `archive_artifact`, `unarchive_artifact`. + `update_run`, `create_artifact`, `update_artifact`, `archive_artifact`, + `unarchive_artifact`. ## Workflows that span tools From 77060de34b670c795a0a6e304277ed5019d286fd Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:21:27 -0800 Subject: [PATCH 04/19] rust(feat): take bulk items in calculated channel write tools --- .../src/service/calculated_channels/mod.rs | 6 +- .../src/tool/calculated_channels/mod.rs | 270 ++++++++++++------ .../src/tool/calculated_channels/test.rs | 246 +++++++++++++--- 3 files changed, 389 insertions(+), 133 deletions(-) diff --git a/rust/crates/sift_mcp/src/service/calculated_channels/mod.rs b/rust/crates/sift_mcp/src/service/calculated_channels/mod.rs index 59825a51e..e9444b06c 100644 --- a/rust/crates/sift_mcp/src/service/calculated_channels/mod.rs +++ b/rust/crates/sift_mcp/src/service/calculated_channels/mod.rs @@ -574,7 +574,11 @@ impl CalculatedChannelService { let mut channel = self .get_calculated_channel(calculated_channel_id.clone()) .await? - .ok_or_else(|| anyhow!("calculated channel '{calculated_channel_id}' not found"))?; + .ok_or_else(|| { + tonic::Status::not_found(format!( + "calculated channel '{calculated_channel_id}' not found" + )) + })?; channel.calculated_channel_id = calculated_channel_id; let CalculatedChannelUpdate { diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs index ce365c159..f308705f5 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs @@ -10,7 +10,10 @@ use sift_rs::{ calculated_channels::v2::CalculatedChannelAbstractChannelReference, metadata::v1::MetadataValue, }; -use crate::tool::common::bulk_create; +use crate::tool::common::{ + bulk::{self, BulkItems, Op}, + bulk_create, +}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, @@ -48,7 +51,7 @@ pub struct CreateCalculatedChannelItem { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateCalculatedChannelParams { +pub struct UpdateCalculatedChannelItem { calculated_channel_id: String, name: Option, description: Option, @@ -63,7 +66,7 @@ pub struct UpdateCalculatedChannelParams { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct CalculatedChannelArchiveParams { +pub struct CalculatedChannelArchiveItem { calculated_channel_id: String, } @@ -421,17 +424,24 @@ impl SiftMcpServer { #[tool( name = "update_calculated_channel", description = " - Update an existing calculated channel, creating a new version. This is a WRITE. Only the fields you + Update one or more calculated channels, creating a new version per item. This is a WRITE. Only the fields you set are changed; the tool reads the current channel, overlays your changes, and saves the result, so unspecified fields are preserved. Output: - - `{ \"calculated_channel\": CalculatedChannel, \"inapplicable_assets\": [...], + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is: + `{ \"calculated_channel\": CalculatedChannel, \"inapplicable_assets\": [...], \"next_step\": string }`. The returned channel is the new version's post-update state. `inapplicable_assets` lists in-scope assets missing a channel the expression references. Parameters: - - `calculated_channel_id`: required. The calculated channel to update. + - `items`: required array of 1–100 objects, one per calculated channel, with the fields below. + Pass one item to update one channel. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `calculated_channel_id`: required in each item. The calculated channel to update. - `name`: optional. New name. - `description`: optional. New description. - `units`: optional. New units. @@ -449,6 +459,9 @@ impl SiftMcpServer { `asset_ids`, or `tag_ids` must be set. Errors: + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate targets, unknown fields, + empty ids, or invalid items. Errors name the item index. Item-local errors fail only that item; + backend-wide errors stop the tail, leaving earlier writes committed. - `INVALID_PARAMS` if no updatable field is set (including a `user_notes`-only call), `expression` and `expression_channel_references_json` are not supplied together, the references JSON is invalid, or both asset-scope forms are set. @@ -460,6 +473,8 @@ impl SiftMcpServer { - Read the current definition with `list_calculated_channel_versions` before rewriting the expression or the asset scope, and confirm the change with the user. - This does not archive. Use `archive_calculated_channel` to retire a channel. + - Items run sequentially; this is not atomic. Use one call for several channels. Never replay + the whole call after a partial failure; retry only failed items, not `not_selected` items. - Every update adds a version; the previous version stays readable through `list_calculated_channel_versions`. ", @@ -472,68 +487,34 @@ impl SiftMcpServer { )] pub async fn update_calculated_channel( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(UpdateCalculatedChannelParams { - calculated_channel_id, - name, - description, - units, - expression, - expression_channel_references_json, - all_assets, - asset_ids, - tag_ids, - metadata, - user_notes, - }) = params; - - require_id(&calculated_channel_id)?; - - if expression.is_some() != expression_channel_references_json.is_some() { - return Err(ErrorData::invalid_params( - "`expression` and `expression_channel_references_json` must be provided together", - None, - )); - } - check_scope_exclusive(all_assets, &asset_ids, &tag_ids)?; - if all_assets == Some(false) && asset_ids.is_none() && tag_ids.is_none() { - return Err(ErrorData::invalid_params( - "`all_assets: false` needs `asset_ids` or `tag_ids` to define the new scope", - None, - )); - } - - let expression_channel_references = expression_channel_references_json - .as_deref() - .map(parse_channel_references) - .transpose()?; - - let changes = CalculatedChannelUpdate { - name, - description, - units, - metadata: metadata.map(|m| m.into_iter().map(MetadataValue::from).collect()), - expression, - expression_channel_references, - all_assets, - asset_ids, - tag_ids, - user_notes, - }; - - if changes.is_empty() { - return Err(ErrorData::invalid_params( - "at least one changed field besides `calculated_channel_id` must be set; \ - `user_notes` only annotates a change, so it must accompany at least one of \ - `name`, `description`, `units`, `metadata`, `expression`, `all_assets`, \ - `asset_ids`, or `tag_ids`", - None, - )); - } + let Parameters(params) = params; + let updates = bulk::parse_items::(params.items)? + .into_iter() + .enumerate() + .map(|(index, item)| { + calculated_channel_update(item).map_err(|error| bulk::at_item(index, error)) + }) + .collect::, _>>()?; + bulk::reject_duplicate_targets(&updates, "calculated channel", |(id, _)| Some(id.clone()))?; + bulk::run( + Op::Update, + updates, + self.selection(params.selected_indices), + false, + |(id, changes)| self.update_calculated_channel_item(id, changes), + ) + .await + } + async fn update_calculated_channel_item( + &self, + calculated_channel_id: String, + changes: CalculatedChannelUpdate, + ) -> error::McpResult { let written = self .calculated_channel_service .update_calculated_channel(calculated_channel_id, changes) @@ -541,8 +522,7 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let next_step = format!( - "Updated calculated channel `{}` to version {}.{} Surface the new definition to the user \ - and confirm it matches their intent.", + "Updated calculated channel `{}` to version {}.{}", written.calculated_channel.calculated_channel_id, written.calculated_channel.version, inapplicable_clause(&written), @@ -558,19 +538,26 @@ impl SiftMcpServer { Reversible with `unarchive_calculated_channel`. Output: - - `{ \"archived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. + - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"archived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. `archived` reflects the returned channel state. Parameters: - - `calculated_channel_id`: required. The calculated channel to archive. + - `items`: required array of 1–100 objects, each with `calculated_channel_id`, the id to archive. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. Errors: - - `INVALID_PARAMS` if `calculated_channel_id` is empty. - - `INVALID_REQUEST` if the server was launched without `--allow-destructive`. - - `RESOURCE_NOT_FOUND` if no calculated channel matches `calculated_channel_id`. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. + - `INVALID_REQUEST` without `--allow-destructive`. Guidance: + - An already archived channel counts as a success. Items run sequentially; this is not atomic. + Never replay the whole call after a partial failure; retry only failed, not `not_selected`, items. - Archiving does not delete the definition or its version history, and `unarchive_calculated_channel` restores it. Confirm the target with the user before calling. - Other calculated channels and rules may reference this one. Check with @@ -585,16 +572,17 @@ impl SiftMcpServer { )] pub async fn archive_calculated_channel( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + self.bulk_calculated_channel_archive(params, Op::Archive) + .await + } - let Parameters(CalculatedChannelArchiveParams { - calculated_channel_id, - }) = params; - - require_id(&calculated_channel_id)?; - + async fn archive_calculated_channel_item( + &self, + calculated_channel_id: String, + ) -> error::McpResult { let written = self .calculated_channel_service .archive_calculated_channel(calculated_channel_id) @@ -604,8 +592,7 @@ impl SiftMcpServer { let archived = written.calculated_channel.is_archived; let next_step = if archived { format!( - "Archived calculated channel `{}`. Tell the user it is archived and no longer offered \ - for plotting or querying, and that `unarchive_calculated_channel` restores it.", + "Archived calculated channel `{}`.", written.calculated_channel.calculated_channel_id, ) } else { @@ -629,19 +616,26 @@ impl SiftMcpServer { Restore a previously archived calculated channel so it is offered again. This is a WRITE. Output: - - `{ \"unarchived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. + - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"unarchived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. `unarchived` reflects the returned channel state. Parameters: - - `calculated_channel_id`: required. The calculated channel to restore. + - `items`: required array of 1–100 objects, each with `calculated_channel_id`, the id to restore. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. Errors: - - `INVALID_PARAMS` if `calculated_channel_id` is empty. - - `INVALID_REQUEST` if the server was launched without `--allow-destructive`. - - `RESOURCE_NOT_FOUND` if no calculated channel matches `calculated_channel_id`. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. + - `INVALID_REQUEST` without `--allow-destructive`. Guidance: + - A channel already unarchived counts as a success. Items run sequentially; this is not atomic. + Never replay the whole call after a partial failure; retry only failed, not `not_selected`, items. - Find archived channels with `list_calculated_channels` filtered by `is_archived == true`. - Confirm the target with the user before calling. ", @@ -654,16 +648,48 @@ impl SiftMcpServer { )] pub async fn unarchive_calculated_channel( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + self.bulk_calculated_channel_archive(params, Op::Unarchive) + .await + } - let Parameters(CalculatedChannelArchiveParams { - calculated_channel_id, - }) = params; - - require_id(&calculated_channel_id)?; + async fn bulk_calculated_channel_archive( + &self, + params: Parameters>, + op: Op, + ) -> error::McpResult { + let Parameters(params) = params; + let items = bulk::parse_items::(params.items)?; + for (index, item) in items.iter().enumerate() { + require_id(&item.calculated_channel_id).map_err(|error| bulk::at_item(index, error))?; + } + bulk::reject_duplicate_targets(&items, "calculated channel", |item| { + Some(item.calculated_channel_id.clone()) + })?; + bulk::run( + op, + items, + self.selection(params.selected_indices), + false, + |item| async move { + if op == Op::Archive { + self.archive_calculated_channel_item(item.calculated_channel_id) + .await + } else { + self.unarchive_calculated_channel_item(item.calculated_channel_id) + .await + } + }, + ) + .await + } + async fn unarchive_calculated_channel_item( + &self, + calculated_channel_id: String, + ) -> error::McpResult { let written = self .calculated_channel_service .unarchive_calculated_channel(calculated_channel_id) @@ -673,7 +699,7 @@ impl SiftMcpServer { let unarchived = !written.calculated_channel.is_archived; let next_step = if unarchived { format!( - "Unarchived calculated channel `{}`. Tell the user it is restored and available again.", + "Unarchived calculated channel `{}`.", written.calculated_channel.calculated_channel_id, ) } else { @@ -692,9 +718,63 @@ impl SiftMcpServer { } } +fn calculated_channel_update( + item: UpdateCalculatedChannelItem, +) -> Result<(String, CalculatedChannelUpdate), ErrorData> { + let UpdateCalculatedChannelItem { + calculated_channel_id, + name, + description, + units, + expression, + expression_channel_references_json, + all_assets, + asset_ids, + tag_ids, + metadata, + user_notes, + } = item; + require_id(&calculated_channel_id)?; + if expression.is_some() != expression_channel_references_json.is_some() { + return Err(ErrorData::invalid_params( + "`expression` and `expression_channel_references_json` must be provided together", + None, + )); + } + check_scope_exclusive(all_assets, &asset_ids, &tag_ids)?; + if all_assets == Some(false) && asset_ids.is_none() && tag_ids.is_none() { + return Err(ErrorData::invalid_params( + "`all_assets: false` needs `asset_ids` or `tag_ids` to define the new scope", + None, + )); + } + let changes = CalculatedChannelUpdate { + name, + description, + units, + expression, + all_assets, + asset_ids, + tag_ids, + user_notes, + expression_channel_references: expression_channel_references_json + .as_deref() + .map(parse_channel_references) + .transpose()?, + metadata: metadata.map(|m| m.into_iter().map(MetadataValue::from).collect()), + }; + if changes.is_empty() { + return Err(ErrorData::invalid_params( + "at least one changed field besides `calculated_channel_id` must be set; `user_notes` only annotates a change, so it must accompany at least one of `name`, `description`, `units`, `metadata`, `expression`, `all_assets`, `asset_ids`, or `tag_ids`", + None, + )); + } + Ok((calculated_channel_id, changes)) +} + /// Reject an empty `calculated_channel_id` before any RPC. fn require_id(calculated_channel_id: &str) -> Result<(), ErrorData> { - if calculated_channel_id.is_empty() { + if calculated_channel_id.trim().is_empty() { return Err(ErrorData::invalid_params( "`calculated_channel_id` must not be empty", None, diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs index 034155a4f..bd377e554 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs @@ -12,8 +12,7 @@ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; use super::{ - CalculatedChannelArchiveParams, CalculatedChannelVersionListParams, - CreateCalculatedChannelItem, UpdateCalculatedChannelParams, + CalculatedChannelVersionListParams, CreateCalculatedChannelItem, UpdateCalculatedChannelItem, }; use crate::{ server::SiftMcpServer, @@ -76,8 +75,195 @@ fn create_params() -> CreateCalculatedChannelItem { } } -fn update_params() -> UpdateCalculatedChannelParams { - UpdateCalculatedChannelParams { +mod bulk { + use super::*; + use serde_json::{Value, json}; + + const TOOLS: [&str; 3] = ["update", "archive", "unarchive"]; + + async fn call(server: &SiftMcpServer, tool: &str, value: Value) -> crate::error::McpResult { + match tool { + "update" => { + server + .update_calculated_channel(Parameters(serde_json::from_value(value).unwrap())) + .await + } + "archive" => { + server + .archive_calculated_channel(Parameters(serde_json::from_value(value).unwrap())) + .await + } + _ => { + server + .unarchive_calculated_channel(Parameters( + serde_json::from_value(value).unwrap(), + )) + .await + } + } + } + + fn item(id: &str, tool: &str) -> Value { + if tool == "update" { + json!({"calculated_channel_id": id, "name": "renamed"}) + } else { + json!({"calculated_channel_id": id}) + } + } + + fn mock_reads(mock: &mut MockCalculatedChannelServiceImpl) { + mock.expect_get_calculated_channel().returning(|req| { + let id = req.into_inner().calculated_channel_id; + let mut channel = stored_channel(); + channel.calculated_channel_id = id; + Ok(Response::new(GetCalculatedChannelResponse { + calculated_channel: if channel.calculated_channel_id == "missing" { + None + } else { + Some(channel) + }, + })) + }); + } + + #[tokio::test] + async fn repeated_archive_and_unarchive_are_successes() { + for tool in ["archive", "unarchive"] { + let mut mock = MockCalculatedChannelServiceImpl::new(); + mock.expect_update_calculated_channel() + .times(2) + .returning(|req| { + Ok(Response::new(UpdateCalculatedChannelResponse { + calculated_channel: req.into_inner().calculated_channel, + inapplicable_assets: vec![], + })) + }); + let (server, handle) = server_with_mock(mock).await; + for _ in 0..2 { + let result = call(&server, tool, json!({"items": [item("same", tool)]})) + .await + .unwrap(); + assert!(!result.is_error.unwrap_or(false)); + assert_eq!(structured(result)["successes"].as_array().unwrap().len(), 1); + } + handle.abort(); + } + } + + #[tokio::test] + async fn selected_subset_writes_only_selected_items() { + for tool in TOOLS { + let mut mock = MockCalculatedChannelServiceImpl::new(); + mock_reads(&mut mock); + mock.expect_update_calculated_channel() + .times(1) + .withf(|req| { + req.get_ref() + .calculated_channel + .as_ref() + .unwrap() + .calculated_channel_id + == "selected" + }) + .returning(|req| { + Ok(Response::new(UpdateCalculatedChannelResponse { + calculated_channel: req.into_inner().calculated_channel, + inapplicable_assets: vec![], + })) + }); + let (server, handle) = server_with_mock(mock).await; + let result = call(&server, tool, json!({"items": [item("skip", tool), item("selected", tool)], "selected_indices": [1]})).await.unwrap(); + let body = structured(result); + assert_eq!(body["successes"][0]["index"], 1); + assert_eq!(body["not_selected"], json!([0])); + handle.abort(); + } + } + + #[tokio::test] + async fn not_found_continues_and_unavailable_stops_tail() { + for tool in TOOLS { + let mut mock = MockCalculatedChannelServiceImpl::new(); + mock_reads(&mut mock); + mock.expect_update_calculated_channel().returning(|req| { + let channel = req.into_inner().calculated_channel.unwrap(); + match channel.calculated_channel_id.as_str() { + "missing" => Err(Status::not_found("missing channel")), + "down" => Err(Status::unavailable("backend down")), + "never" => panic!("tail must not run"), + _ => Ok(Response::new(UpdateCalculatedChannelResponse { + calculated_channel: Some(channel), + inapplicable_assets: vec![], + })), + } + }); + let (server, handle) = server_with_mock(mock).await; + let body = structured(call(&server, tool, json!({"items": [item("missing", tool), item("ok", tool), item("down", tool), item("never", tool)]})).await.unwrap()); + assert_eq!(body["successes"][0]["index"], 1); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 + ); + assert_eq!(body["failures"][1]["index"], 2); + assert_eq!(body["not_attempted"], json!([3])); + handle.abort(); + } + } + + #[tokio::test] + async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { + for tool in TOOLS { + let (server, handle) = server_with_mock(MockCalculatedChannelServiceImpl::new()).await; + let server = server.with_require_selection(true); + for items in [ + json!([item("a", tool), item("a", tool)]), + json!([item("a", tool), item(" ", tool)]), + json!([{"calculated_channel_id": "b", "unknown": true}]), + ] { + let error = call( + &server, + tool, + json!({"items": items, "selected_indices": [0]}), + ) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + } + let error = call(&server, tool, json!({"items": [item("a", tool)]})) + .await + .unwrap_err(); + assert!(error.message.contains("nothing was written")); + handle.abort(); + } + } +} + +fn update_call( + item: UpdateCalculatedChannelItem, +) -> Parameters> { + assert!(item.metadata.is_none()); + Parameters( + serde_json::from_value(serde_json::json!({"items": [{ + "calculated_channel_id": item.calculated_channel_id, + "name": item.name, "description": item.description, "units": item.units, + "expression": item.expression, + "expression_channel_references_json": item.expression_channel_references_json, + "all_assets": item.all_assets, "asset_ids": item.asset_ids, "tag_ids": item.tag_ids, + "user_notes": item.user_notes, + }]})) + .unwrap(), + ) +} + +fn archive_call(id: &str) -> Parameters> { + Parameters( + serde_json::from_value(serde_json::json!({"items": [{"calculated_channel_id": id}]})) + .unwrap(), + ) +} + +fn update_params() -> UpdateCalculatedChannelItem { + UpdateCalculatedChannelItem { calculated_channel_id: "cc1".into(), name: None, description: None, @@ -429,7 +615,7 @@ async fn update_calculated_channel_blocked_without_allow_destructive() { params.name = Some("renamed".into()); let err = server - .update_calculated_channel(Parameters(params)) + .update_calculated_channel(update_call(params)) .await .expect_err("expected destructive gate to reject the call"); @@ -442,7 +628,7 @@ async fn update_calculated_channel_rejects_empty_update() { let (server, _h) = server_with_mock(MockCalculatedChannelServiceImpl::new()).await; let err = server - .update_calculated_channel(Parameters(update_params())) + .update_calculated_channel(update_call(update_params())) .await .expect_err("expected error"); @@ -460,7 +646,7 @@ async fn update_calculated_channel_rejects_user_notes_only_update() { params.user_notes = Some("just a note".into()); let err = server - .update_calculated_channel(Parameters(params)) + .update_calculated_channel(update_call(params)) .await .expect_err("expected error"); @@ -480,7 +666,7 @@ async fn update_calculated_channel_rejects_expression_without_references() { params.expression = Some("$1 * 2".into()); let err = server - .update_calculated_channel(Parameters(params)) + .update_calculated_channel(update_call(params)) .await .expect_err("expected error"); @@ -496,7 +682,7 @@ async fn update_calculated_channel_rejects_malformed_references_json() { params.expression_channel_references_json = Some("{not json".into()); let err = server - .update_calculated_channel(Parameters(params)) + .update_calculated_channel(update_call(params)) .await .expect_err("expected error"); @@ -512,7 +698,7 @@ async fn update_calculated_channel_rejects_all_assets_with_asset_ids() { params.asset_ids = Some(vec!["asset-1".into()]); let err = server - .update_calculated_channel(Parameters(params)) + .update_calculated_channel(update_call(params)) .await .expect_err("expected error"); @@ -547,11 +733,11 @@ async fn update_calculated_channel_returns_structured_result() { params.name = Some("renamed".into()); let resp = server - .update_calculated_channel(Parameters(params)) + .update_calculated_channel(update_call(params)) .await .expect("update_calculated_channel failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["calculated_channel"]["name"], "renamed"); assert_eq!(body["calculated_channel"]["version"], 4); assert!(body["next_step"].is_string()); @@ -563,9 +749,7 @@ async fn archive_calculated_channel_blocked_without_allow_destructive() { let (server, _h) = server_with_mock_and_flags(mock, false, false).await; let err = server - .archive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: "cc1".into(), - })) + .archive_calculated_channel(archive_call("cc1")) .await .expect_err("expected destructive gate to reject the call"); @@ -579,9 +763,7 @@ async fn unarchive_calculated_channel_blocked_without_allow_destructive() { let (server, _h) = server_with_mock_and_flags(mock, false, false).await; let err = server - .unarchive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: "cc1".into(), - })) + .unarchive_calculated_channel(archive_call("cc1")) .await .expect_err("expected destructive gate to reject the call"); @@ -613,13 +795,11 @@ async fn archive_calculated_channel_returns_structured_result() { let (server, _h) = server_with_mock(mock).await; let resp = server - .archive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: "cc1".into(), - })) + .archive_calculated_channel(archive_call("cc1")) .await .expect("archive_calculated_channel failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["archived"], true); assert_eq!(body["calculated_channel"]["calculatedChannelId"], "cc1"); assert!(body["next_step"].is_string()); @@ -649,13 +829,11 @@ async fn unarchive_calculated_channel_returns_structured_result() { let (server, _h) = server_with_mock(mock).await; let resp = server - .unarchive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: "cc1".into(), - })) + .unarchive_calculated_channel(archive_call("cc1")) .await .expect("unarchive_calculated_channel failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["unarchived"], true); assert!(body["next_step"].is_string()); } @@ -678,13 +856,11 @@ async fn archive_calculated_channel_surfaces_a_returned_unarchived_state() { let (server, _h) = server_with_mock(mock).await; let resp = server - .archive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: "cc1".into(), - })) + .archive_calculated_channel(archive_call("cc1")) .await .expect("archive_calculated_channel failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["archived"], false); assert!(body["next_step"].as_str().unwrap().contains("unarchived")); } @@ -707,13 +883,11 @@ async fn unarchive_calculated_channel_surfaces_a_returned_archived_state() { let (server, _h) = server_with_mock(mock).await; let resp = server - .unarchive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: "cc1".into(), - })) + .unarchive_calculated_channel(archive_call("cc1")) .await .expect("unarchive_calculated_channel failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["unarchived"], false); assert!(body["next_step"].as_str().unwrap().contains("archived")); } @@ -723,9 +897,7 @@ async fn archive_calculated_channel_rejects_empty_id() { let (server, _h) = server_with_mock(MockCalculatedChannelServiceImpl::new()).await; let err = server - .archive_calculated_channel(Parameters(CalculatedChannelArchiveParams { - calculated_channel_id: String::new(), - })) + .archive_calculated_channel(archive_call("")) .await .expect_err("expected error"); From 2d4b7a5668300e7866aeffaed0b509bf083500c4 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:21:27 -0800 Subject: [PATCH 05/19] rust(feat): take bulk items in user defined function write tools --- .../src/service/user_defined_functions/mod.rs | 5 +- .../src/tool/user_defined_functions/mod.rs | 254 ++++++++++++------ .../src/tool/user_defined_functions/test.rs | 214 +++++++++++++-- 3 files changed, 366 insertions(+), 107 deletions(-) diff --git a/rust/crates/sift_mcp/src/service/user_defined_functions/mod.rs b/rust/crates/sift_mcp/src/service/user_defined_functions/mod.rs index 3f7c5ff55..b78ef85aa 100644 --- a/rust/crates/sift_mcp/src/service/user_defined_functions/mod.rs +++ b/rust/crates/sift_mcp/src/service/user_defined_functions/mod.rs @@ -332,9 +332,8 @@ impl UserDefinedFunctionService { .await .context("failed to fetch user defined function")?; - resp.user_defined_function.ok_or_else(|| { - anyhow!("get_user_defined_function response missing user defined function") - }) + resp.user_defined_function + .ok_or_else(|| tonic::Status::not_found("user defined function not found").into()) } async fn send_update( diff --git a/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs b/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs index daefbb7f0..d8d21b0a6 100644 --- a/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs +++ b/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs @@ -11,7 +11,10 @@ use sift_rs::{ metadata::v1::MetadataValue, }; -use crate::tool::common::bulk_create; +use crate::tool::common::{ + bulk::{self, BulkItems, Op}, + bulk_create, +}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, @@ -43,7 +46,7 @@ pub struct CreateUserDefinedFunctionItem { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateUserDefinedFunctionParams { +pub struct UpdateUserDefinedFunctionItem { pub(crate) user_defined_function_id: String, pub(crate) name: Option, pub(crate) description: Option, @@ -53,7 +56,7 @@ pub struct UpdateUserDefinedFunctionParams { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct ArchiveUserDefinedFunctionParams { +pub struct ArchiveUserDefinedFunctionItem { pub(crate) user_defined_function_id: String, } @@ -398,17 +401,23 @@ impl SiftMcpServer { #[tool( name = "update_user_defined_function", description = " - Update a user defined function. This is a WRITE. Only the fields you set are written; the + Update one or more user defined functions. This is a WRITE. Only the fields you set are written; the rest of the function is left as it is. The update creates a NEW version and leaves the previous version intact, so callers pinned to an older version are unaffected. Output: - - `{ \"user_defined_function\": UserDefinedFunction, \"next_step\": \"...\" }`. The + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"user_defined_function\": UserDefinedFunction, \"next_step\": \"...\" }`. The returned function is the newly created version, with its own `user_defined_function_version_id` and incremented `version`. Parameters: - - `user_defined_function_id`: required. The function to update. + - `items`: required array of 1–100 objects, one per function, with the fields below. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `user_defined_function_id`: required in each item. The function to update. - `name`: optional. New name. It must start with a letter, then contain only alphanumeric characters or `_`, and be at most 253 characters. The API applies a rename BY ITSELF and ignores any other field in the same call, so this tool rejects `name` combined with another @@ -424,6 +433,9 @@ impl SiftMcpServer { `unarchive_user_defined_function`. Errors: + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. - `INVALID_PARAMS` if no updatable field is set, `name` is combined with another field, or `function_inputs_json` is not a valid input array. The server also rejects updates that its dependency rules forbid: `name` cannot change once the function has ever had @@ -434,6 +446,8 @@ impl SiftMcpServer { - `INTERNAL_ERROR` for upstream gRPC failures. Guidance: + - Items run sequentially; this is not atomic. Use one call for several functions. Never replay + the whole call after a partial failure; retry only failed, not `not_selected`, items. - Confirm the target function and the exact changes with the user before calling. - `function_inputs_json` and `metadata` are REPLACE, not merge. Read the current values with `list_user_defined_functions` (filter `user_defined_function_id == \"\"`) and @@ -450,62 +464,34 @@ impl SiftMcpServer { )] pub async fn update_user_defined_function( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(UpdateUserDefinedFunctionParams { - user_defined_function_id, - name, - description, - expression, - function_inputs_json, - metadata, - }) = params; - - if user_defined_function_id.is_empty() { - return Err(ErrorData::invalid_params( - "`user_defined_function_id` must not be empty", - None, - )); - } - if let Some(name) = &name { - validate_function_name(name)?; - } - - let function_inputs = function_inputs_json - .as_deref() - .map(parse_function_inputs) - .transpose()?; - - let changes = UdfUpdate { - name, - description, - expression, - function_inputs, - metadata: metadata_values(metadata), - }; - - let others_set = changes.description.is_some() - || changes.expression.is_some() - || changes.function_inputs.is_some() - || changes.metadata.is_some(); - - if changes.name.is_none() && !others_set { - return Err(ErrorData::invalid_params( - "at least one of `name`, `description`, `expression`, `function_inputs_json`, or \ - `metadata` must be set", - None, - )); - } - if changes.name.is_some() && others_set { - return Err(ErrorData::invalid_params( - "the API applies a `name` change on its own and ignores every other field in the \ - same request; send the rename as a separate call", - None, - )); - } + let Parameters(params) = params; + let updates = bulk::parse_items::(params.items)? + .into_iter() + .enumerate() + .map(|(index, item)| udf_update(item).map_err(|error| bulk::at_item(index, error))) + .collect::, _>>()?; + bulk::reject_duplicate_targets(&updates, "user defined function", |(id, _)| { + Some(id.clone()) + })?; + bulk::run( + Op::Update, + updates, + self.selection(params.selected_indices), + false, + |(id, changes)| self.update_user_defined_function_item(id, changes), + ) + .await + } + async fn update_user_defined_function_item( + &self, + user_defined_function_id: String, + changes: UdfUpdate, + ) -> error::McpResult { let function = self .user_defined_function_service .update_user_defined_function(user_defined_function_id, changes) @@ -513,9 +499,7 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let next_step = format!( - "Updated user defined function `{}` ({}); it is now at version {}. Earlier versions are \ - untouched. Surface the new state to the user and confirm nothing was unintentionally \ - replaced — input and metadata lists are REPLACE operations.", + "Updated user defined function `{}` ({}); it is now at version {}.", function.name, function.user_defined_function_id, function.version, ); @@ -534,18 +518,24 @@ impl SiftMcpServer { WRITE. Reversible with `unarchive_user_defined_function`. Output: - - `{ \"archived\": true, \"user_defined_function\": UserDefinedFunction, - \"next_step\": \"...\" }`. The returned function is the post-archive state. + - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"archived\": true, \"user_defined_function\": UserDefinedFunction, \"next_step\": string }`. Parameters: - - `user_defined_function_id`: required. The function to archive. + - `items`: required array of 1–100 objects, each with `user_defined_function_id`, the id to archive. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. Errors: - - `INVALID_PARAMS` if `user_defined_function_id` is empty. - - `RESOURCE_NOT_FOUND` if no function matches `user_defined_function_id`. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. Guidance: + - An already archived function counts as a success. Items run sequentially; this is not atomic. + Never replay the whole call after a partial failure; retry only failed, not `not_selected`, items. - Archiving does not delete the function and does not rewrite anything that already calls it. Calculated channels and rules that call it keep referencing it. Confirm the target with the user before calling. @@ -562,19 +552,20 @@ impl SiftMcpServer { )] pub async fn archive_user_defined_function( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + self.bulk_udf_archive(params, Op::Archive).await + } - let Parameters(ArchiveUserDefinedFunctionParams { - user_defined_function_id, - }) = params; - + async fn archive_user_defined_function_item( + &self, + user_defined_function_id: String, + ) -> error::McpResult { let function = self.set_archived(user_defined_function_id, true).await?; let next_step = format!( - "Archived user defined function `{}` ({}). Tell the user it no longer appears as an \ - available function and that `unarchive_user_defined_function` restores it.", + "Archived user defined function `{}` ({}).", function.name, function.user_defined_function_id, ); @@ -593,18 +584,24 @@ impl SiftMcpServer { Restore a previously archived user defined function. This is a WRITE. Output: - - `{ \"unarchived\": true, \"user_defined_function\": UserDefinedFunction, - \"next_step\": \"...\" }`. The returned function is the post-unarchive state. + - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"unarchived\": true, \"user_defined_function\": UserDefinedFunction, \"next_step\": string }`. Parameters: - - `user_defined_function_id`: required. The function to unarchive. + - `items`: required array of 1–100 objects, each with `user_defined_function_id`, the id to restore. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. Errors: - - `INVALID_PARAMS` if `user_defined_function_id` is empty. - - `RESOURCE_NOT_FOUND` if no function matches `user_defined_function_id`. - - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. Guidance: + - A function already unarchived counts as a success. Items run sequentially; this is not atomic. + Never replay the whole call after a partial failure; retry only failed, not `not_selected`, items. - Confirm the target function with the user before calling. Find archived functions with `list_user_defined_functions` and filter `is_archived == true`. ", @@ -617,18 +614,56 @@ impl SiftMcpServer { )] pub async fn unarchive_user_defined_function( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + self.bulk_udf_archive(params, Op::Unarchive).await + } - let Parameters(ArchiveUserDefinedFunctionParams { - user_defined_function_id, - }) = params; + async fn bulk_udf_archive( + &self, + params: Parameters>, + op: Op, + ) -> error::McpResult { + let Parameters(params) = params; + let items = bulk::parse_items::(params.items)?; + for (index, item) in items.iter().enumerate() { + if item.user_defined_function_id.trim().is_empty() { + return Err(bulk::at_item( + index, + ErrorData::invalid_params("`user_defined_function_id` must not be empty", None), + )); + } + } + bulk::reject_duplicate_targets(&items, "user defined function", |item| { + Some(item.user_defined_function_id.clone()) + })?; + bulk::run( + op, + items, + self.selection(params.selected_indices), + false, + |item| async move { + if op == Op::Archive { + self.archive_user_defined_function_item(item.user_defined_function_id) + .await + } else { + self.unarchive_user_defined_function_item(item.user_defined_function_id) + .await + } + }, + ) + .await + } + async fn unarchive_user_defined_function_item( + &self, + user_defined_function_id: String, + ) -> error::McpResult { let function = self.set_archived(user_defined_function_id, false).await?; let next_step = format!( - "Unarchived user defined function `{}` ({}). Tell the user it is available again.", + "Unarchived user defined function `{}` ({}).", function.name, function.user_defined_function_id, ); @@ -651,7 +686,7 @@ impl SiftMcpServer { user_defined_function_id: String, is_archived: bool, ) -> Result { - if user_defined_function_id.is_empty() { + if user_defined_function_id.trim().is_empty() { return Err(ErrorData::invalid_params( "`user_defined_function_id` must not be empty", None, @@ -665,6 +700,53 @@ impl SiftMcpServer { } } +fn udf_update(item: UpdateUserDefinedFunctionItem) -> Result<(String, UdfUpdate), ErrorData> { + let UpdateUserDefinedFunctionItem { + user_defined_function_id, + name, + description, + expression, + function_inputs_json, + metadata, + } = item; + if user_defined_function_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`user_defined_function_id` must not be empty", + None, + )); + } + if let Some(name) = &name { + validate_function_name(name)?; + } + let changes = UdfUpdate { + name, + description, + expression, + function_inputs: function_inputs_json + .as_deref() + .map(parse_function_inputs) + .transpose()?, + metadata: metadata_values(metadata), + }; + let others_set = changes.description.is_some() + || changes.expression.is_some() + || changes.function_inputs.is_some() + || changes.metadata.is_some(); + if changes.name.is_none() && !others_set { + return Err(ErrorData::invalid_params( + "at least one of `name`, `description`, `expression`, `function_inputs_json`, or `metadata` must be set", + None, + )); + } + if changes.name.is_some() && others_set { + return Err(ErrorData::invalid_params( + "the API applies a `name` change on its own and ignores every other field in the same request; send the rename as a separate call", + None, + )); + } + Ok((user_defined_function_id, changes)) +} + /// Resolve the `(user_defined_function_id, name)` request fields from the /// mutually exclusive optional params. The proto silently ignores `name` when an /// id is present, so reject the ambiguous call rather than pick for the caller. diff --git a/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs b/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs index ed430df15..13536d3cf 100644 --- a/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs +++ b/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs @@ -19,8 +19,8 @@ use crate::{ tool::{ common::test_support::{list_params, list_params_with_fields, structured}, user_defined_functions::{ - ArchiveUserDefinedFunctionParams, CreateUserDefinedFunctionItem, - UpdateUserDefinedFunctionParams, UserDefinedFunctionVersionListParams, + ArchiveUserDefinedFunctionItem, CreateUserDefinedFunctionItem, + UpdateUserDefinedFunctionItem, UserDefinedFunctionVersionListParams, }, }, }; @@ -85,25 +85,203 @@ fn create_params(function_inputs_json: &str) -> Parameters crate::error::McpResult { + match tool { + "update" => { + server + .update_user_defined_function(Parameters( + serde_json::from_value(value).unwrap(), + )) + .await + } + "archive" => { + server + .archive_user_defined_function(Parameters( + serde_json::from_value(value).unwrap(), + )) + .await + } + _ => { + server + .unarchive_user_defined_function(Parameters( + serde_json::from_value(value).unwrap(), + )) + .await + } + } + } + + fn item(id: &str, tool: &str) -> Value { + if tool == "update" { + json!({"user_defined_function_id": id, "description": "updated"}) + } else { + json!({"user_defined_function_id": id}) + } + } + + fn mock_reads(mock: &mut MockUserDefinedFunctionServiceImpl) { + mock.expect_get_user_defined_function().returning(|req| { + let id = req.into_inner().user_defined_function_id; + Ok(Response::new(GetUserDefinedFunctionResponse { + user_defined_function: if id == "missing" { + None + } else { + Some(UserDefinedFunction { + user_defined_function_id: id, + name: "function".into(), + ..Default::default() + }) + }, + })) + }); + } + + #[tokio::test] + async fn already_archived_or_unarchived_are_successes() { + for tool in ["archive", "unarchive"] { + let mut mock = MockUserDefinedFunctionServiceImpl::new(); + let is_archived = tool == "archive"; + mock.expect_get_user_defined_function() + .times(1) + .returning(move |_| { + Ok(Response::new(GetUserDefinedFunctionResponse { + user_defined_function: Some(UserDefinedFunction { + user_defined_function_id: "same".into(), + is_archived, + ..Default::default() + }), + })) + }); + mock.expect_update_user_defined_function() + .times(1) + .returning(|req| { + Ok(Response::new(UpdateUserDefinedFunctionResponse { + user_defined_function: req.into_inner().user_defined_function, + })) + }); + let (server, handle) = server_with_mock(mock, true, true).await; + let result = call(&server, tool, json!({"items": [item("same", tool)]})) + .await + .unwrap(); + assert!(!result.is_error.unwrap_or(false)); + assert_eq!(structured(result)["successes"].as_array().unwrap().len(), 1); + handle.abort(); + } + } + + #[tokio::test] + async fn selected_subset_writes_only_selected_items() { + for tool in TOOLS { + let mut mock = MockUserDefinedFunctionServiceImpl::new(); + mock_reads(&mut mock); + mock.expect_update_user_defined_function() + .times(1) + .withf(|req| { + req.get_ref() + .user_defined_function + .as_ref() + .unwrap() + .user_defined_function_id + == "selected" + }) + .returning(|req| { + Ok(Response::new(UpdateUserDefinedFunctionResponse { + user_defined_function: req.into_inner().user_defined_function, + })) + }); + let (server, handle) = server_with_mock(mock, true, true).await; + let body = structured(call(&server, tool, json!({"items": [item("skip", tool), item("selected", tool)], "selected_indices": [1]})).await.unwrap()); + assert_eq!(body["successes"][0]["index"], 1); + assert_eq!(body["not_selected"], json!([0])); + handle.abort(); + } + } + + #[tokio::test] + async fn not_found_continues_and_unavailable_stops_tail() { + for tool in TOOLS { + let mut mock = MockUserDefinedFunctionServiceImpl::new(); + mock_reads(&mut mock); + mock.expect_update_user_defined_function().returning(|req| { + let function = req.into_inner().user_defined_function.unwrap(); + match function.user_defined_function_id.as_str() { + "missing" => Err(Status::not_found("missing function")), + "down" => Err(Status::unavailable("backend down")), + "never" => panic!("tail must not run"), + _ => Ok(Response::new(UpdateUserDefinedFunctionResponse { + user_defined_function: Some(function), + })), + } + }); + let (server, handle) = server_with_mock(mock, true, true).await; + let body = structured(call(&server, tool, json!({"items": [item("missing", tool), item("ok", tool), item("down", tool), item("never", tool)]})).await.unwrap()); + assert_eq!(body["successes"][0]["index"], 1); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 + ); + assert_eq!(body["failures"][1]["index"], 2); + assert_eq!(body["not_attempted"], json!([3])); + handle.abort(); + } + } + + #[tokio::test] + async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { + for tool in TOOLS { + let (server, handle) = + server_with_mock(MockUserDefinedFunctionServiceImpl::new(), true, true).await; + let server = server.with_require_selection(true); + for items in [ + json!([item("a", tool), item("a", tool)]), + json!([item("a", tool), item(" ", tool)]), + json!([{"user_defined_function_id": "b", "unknown": true}]), + ] { + let error = call( + &server, + tool, + json!({"items": items, "selected_indices": [0]}), + ) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + } + let error = call(&server, tool, json!({"items": [item("a", tool)]})) + .await + .unwrap_err(); + assert!(error.message.contains("nothing was written")); + handle.abort(); + } + } +} + fn update_params( name: Option<&str>, description: Option<&str>, function_inputs_json: Option<&str>, -) -> Parameters { - Parameters(UpdateUserDefinedFunctionParams { - user_defined_function_id: "f1".into(), - name: name.map(str::to_string), - description: description.map(str::to_string), - expression: None, - function_inputs_json: function_inputs_json.map(str::to_string), - metadata: None, - }) +) -> Parameters> { + Parameters( + serde_json::from_value(serde_json::json!({"items": [{ + "user_defined_function_id": "f1", "name": name, "description": description, + "function_inputs_json": function_inputs_json, + }]})) + .unwrap(), + ) } -fn archive_params(id: &str) -> Parameters { - Parameters(ArchiveUserDefinedFunctionParams { - user_defined_function_id: id.into(), - }) +fn archive_params(id: &str) -> Parameters> { + Parameters( + serde_json::from_value(serde_json::json!({"items": [{ + "user_defined_function_id": id, + }]})) + .unwrap(), + ) } fn expect_current_udf(mock: &mut MockUserDefinedFunctionServiceImpl, id: &str) { @@ -600,7 +778,7 @@ async fn update_returns_the_new_version() { .await .expect("update_user_defined_function failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["user_defined_function"]["version"], 4); assert_eq!( body["user_defined_function"]["userDefinedFunctionVersionId"], @@ -679,7 +857,7 @@ async fn archive_sets_the_archive_flag_and_reports_it() { .await .expect("archive_user_defined_function failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["archived"], true); assert_eq!(body["user_defined_function"]["isArchived"], true); } @@ -713,7 +891,7 @@ async fn unarchive_clears_the_archive_flag_and_reports_it() { .await .expect("unarchive_user_defined_function failed"); - let body = structured(resp); + let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["unarchived"], true); } From 69b7fa6b4f1e10090856b208691638779750e724 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:21:27 -0800 Subject: [PATCH 06/19] rust(feat): take bulk items in report updates --- .../sift_mcp/src/service/reports/mod.rs | 2 +- rust/crates/sift_mcp/src/tool/reports/mod.rs | 79 +++++++---- rust/crates/sift_mcp/src/tool/reports/test.rs | 130 ++++++++++++++++-- 3 files changed, 177 insertions(+), 34 deletions(-) diff --git a/rust/crates/sift_mcp/src/service/reports/mod.rs b/rust/crates/sift_mcp/src/service/reports/mod.rs index 5c54788f1..5b1b1a4b1 100644 --- a/rust/crates/sift_mcp/src/service/reports/mod.rs +++ b/rust/crates/sift_mcp/src/service/reports/mod.rs @@ -342,6 +342,6 @@ impl ReportService { .context("failed to fetch report")?; resp.report - .ok_or_else(|| anyhow!("get_report response missing report")) + .ok_or_else(|| tonic::Status::not_found("report not found").into()) } } diff --git a/rust/crates/sift_mcp/src/tool/reports/mod.rs b/rust/crates/sift_mcp/src/tool/reports/mod.rs index fc1d03a19..14f9c4e79 100644 --- a/rust/crates/sift_mcp/src/tool/reports/mod.rs +++ b/rust/crates/sift_mcp/src/tool/reports/mod.rs @@ -87,7 +87,7 @@ pub struct CreateReportItem { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateReportParams { +pub struct UpdateReportItem { report_id: String, metadata: Vec, } @@ -431,16 +431,22 @@ impl SiftMcpServer { #[tool( name = "update_report", description = " - Update an existing report's metadata. Wraps `reports/v1 UpdateReport`. + Update one or more reports' metadata. This is a WRITE. Wraps `reports/v1 UpdateReport`. Output: - - `{ \"report\": Report, \"report_url\": string|null, \"next_step\": string }`. The returned `Report` + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"report\": Report, \"report_url\": string|null, \"next_step\": string }`. The returned `Report` is re-fetched after the update (the update RPC itself returns no body). `report_url` is the report's Sift web link, or null when the host can't be derived. `Report` has the `list_reports` item shape: rule totals, no per-rule summaries. Parameters: - - `report_id`: required; the id of the report to update. + - `items`: required array of 1–100 objects, one per report, with the fields below. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `report_id`: required in each item; the id of the report to update. - `metadata`: required; REPLACES the report's full metadata list. Each entry is `{ \"name\": \"\", \"value\": }` where `value` is a string, number, or boolean. Pass `[]` to clear all metadata. A `name` that does not yet exist in the organization's @@ -451,12 +457,17 @@ impl SiftMcpServer { archive flow are not exposed here. Errors: + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. - `INVALID_PARAMS` if `report_id` is empty, the `metadata` list contains duplicate key names, or a value's type does not match an existing metadata key's type. - `RESOURCE_NOT_FOUND` if no report matches `report_id`. - `INTERNAL_ERROR` for upstream gRPC failures. Guidance: + - Items run sequentially; this is not atomic. Use one call for several reports. Never replay + the whole call after a partial failure; retry only failed, not `not_selected`, items. - This is a write with REPLACE semantics. CONFIRM the full metadata list with the user — for appends, read the current report via `list_reports` filtered by `report_id == \"\"` and send the union. ", @@ -467,26 +478,49 @@ impl SiftMcpServer { idempotent_hint = true, ) )] - pub async fn update_report(&self, params: Parameters) -> error::McpResult { + pub async fn update_report( + &self, + params: Parameters>, + ) -> error::McpResult { + use crate::tool::common::bulk::{self, Op}; self.require_destructive()?; - - let Parameters(UpdateReportParams { - report_id, - metadata, - }) = params; - - if report_id.is_empty() { - return Err(ErrorData::invalid_params( - "`report_id` must not be empty", - None, - )); - } - - let metadata = metadata + let Parameters(params) = params; + let items = bulk::parse_items::(params.items)?; + let updates = items .into_iter() - .map(MetadataValue::from) - .collect::>(); + .enumerate() + .map(|(index, item)| { + if item.report_id.trim().is_empty() { + return Err(bulk::at_item( + index, + ErrorData::invalid_params("`report_id` must not be empty", None), + )); + } + Ok(( + item.report_id, + item.metadata + .into_iter() + .map(MetadataValue::from) + .collect::>(), + )) + }) + .collect::, _>>()?; + bulk::reject_duplicate_targets(&updates, "report", |(id, _)| Some(id.clone()))?; + bulk::run( + Op::Update, + updates, + self.selection(params.selected_indices), + false, + |(id, metadata)| self.update_report_item(id, metadata), + ) + .await + } + async fn update_report_item( + &self, + report_id: String, + metadata: Vec, + ) -> error::McpResult { let report = self .report_service .update_report(report_id, metadata) @@ -495,8 +529,7 @@ impl SiftMcpServer { let report_url = self.url_service.build_report_url(&report.report_id).ok(); let next_step = format!( - "Updated metadata on report `{}` ({}).{} Surface the new state to the user and confirm \ - nothing was unintentionally dropped — metadata is a REPLACE operation.", + "Updated metadata on report `{}` ({}).{}", report.name, report.report_id, url_clause("report", Some(&report.name), report_url.as_deref()), diff --git a/rust/crates/sift_mcp/src/tool/reports/test.rs b/rust/crates/sift_mcp/src/tool/reports/test.rs index 457582ba0..6941e6609 100644 --- a/rust/crates/sift_mcp/src/tool/reports/test.rs +++ b/rust/crates/sift_mcp/src/tool/reports/test.rs @@ -18,7 +18,113 @@ use sift_test_util::{ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; -use super::{CreateReportItem, ReportListParams, ReportRuleSummaryListParams, UpdateReportParams}; +use super::{CreateReportItem, ReportListParams, ReportRuleSummaryListParams}; + +mod bulk { + use super::*; + use serde_json::{Value, json}; + + async fn call(server: &SiftMcpServer, value: Value) -> crate::error::McpResult { + server + .update_report(Parameters(serde_json::from_value(value).unwrap())) + .await + } + fn item(id: &str) -> Value { + json!({"report_id": id, "metadata": []}) + } + fn body(result: rmcp::model::CallToolResult) -> Value { + result.structured_content.unwrap() + } + + #[tokio::test] + async fn selected_subset_writes_only_selected_items() { + let mut mock = MockReportServiceImpl::new(); + mock.expect_update_report() + .times(1) + .withf(|req| req.get_ref().report.as_ref().unwrap().report_id == "selected") + .returning(|_| Ok(Response::new(UpdateReportResponse {}))); + mock.expect_get_report().times(1).returning(|_| { + Ok(Response::new(GetReportResponse { + report: Some(Report { + report_id: "selected".into(), + ..Default::default() + }), + })) + }); + let (server, handle) = server_with_mock(mock).await; + let body = body( + call( + &server, + json!({"items": [item("skip"), item("selected")], "selected_indices": [1]}), + ) + .await + .unwrap(), + ); + assert_eq!(body["successes"][0]["index"], 1); + assert_eq!(body["not_selected"], json!([0])); + handle.abort(); + } + + #[tokio::test] + async fn not_found_continues_and_unavailable_stops_tail() { + let mut mock = MockReportServiceImpl::new(); + mock.expect_update_report().returning(|req| { + match req.get_ref().report.as_ref().unwrap().report_id.as_str() { + "missing" => Err(Status::not_found("missing report")), + "down" => Err(Status::unavailable("backend down")), + "never" => panic!("tail must not run"), + _ => Ok(Response::new(UpdateReportResponse {})), + } + }); + mock.expect_get_report().times(1).returning(|_| { + Ok(Response::new(GetReportResponse { + report: Some(Report { + report_id: "ok".into(), + ..Default::default() + }), + })) + }); + let (server, handle) = server_with_mock(mock).await; + let body = body( + call( + &server, + json!({"items": [item("missing"), item("ok"), item("down"), item("never")]}), + ) + .await + .unwrap(), + ); + assert_eq!(body["updated_count"], 1); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 + ); + assert_eq!(body["failures"][1]["index"], 2); + assert_eq!(body["not_attempted"], json!([3])); + handle.abort(); + } + + #[tokio::test] + async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { + let (server, handle) = server_with_mock(MockReportServiceImpl::new()).await; + let server = server.with_require_selection(true); + for items in [ + json!([item("a"), item("a")]), + json!([item("a"), item(" ")]), + json!([{"report_id": "a", "metadata": [], "unknown": true}]), + ] { + let error = call(&server, json!({"items": items, "selected_indices": [0]})) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + } + let error = call(&server, json!({"items": [item("a")]})) + .await + .unwrap_err(); + assert!(error.message.contains("nothing was written")); + handle.abort(); + } +} + use crate::{ server::SiftMcpServer, tool::common::test_support::{content_texts, structured_field}, @@ -553,14 +659,16 @@ async fn update_report_happy_path() { let (server, _h) = server_with_mock(mock).await; let resp = server - .update_report(Parameters(UpdateReportParams { - report_id: "rep1".into(), - metadata: vec![], - })) + .update_report(Parameters( + serde_json::from_value( + serde_json::json!({"items": [{"report_id": "rep1", "metadata": []}]}), + ) + .unwrap(), + )) .await .expect("update_report failed"); - let report = structured_field(resp, "report"); + let report = structured_field(resp, "successes")[0]["result"]["report"].clone(); assert_eq!(report["name"], "after update"); } @@ -569,10 +677,12 @@ async fn update_report_rejects_empty_id() { let (server, _h) = server_with_mock(MockReportServiceImpl::new()).await; let err = server - .update_report(Parameters(UpdateReportParams { - report_id: String::new(), - metadata: vec![], - })) + .update_report(Parameters( + serde_json::from_value( + serde_json::json!({"items": [{"report_id": "", "metadata": []}]}), + ) + .unwrap(), + )) .await .expect_err("expected error"); From 6ff5ae8326336f944bdd74b18e3793ef034fa949 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:21:27 -0800 Subject: [PATCH 07/19] rust(feat): take bulk items in report template updates --- .../sift_mcp/src/tool/report_templates/mod.rs | 157 +++++++++++------- .../src/tool/report_templates/test.rs | 137 ++++++++++++++- 2 files changed, 224 insertions(+), 70 deletions(-) diff --git a/rust/crates/sift_mcp/src/tool/report_templates/mod.rs b/rust/crates/sift_mcp/src/tool/report_templates/mod.rs index 84bbe9911..759142ab9 100644 --- a/rust/crates/sift_mcp/src/tool/report_templates/mod.rs +++ b/rust/crates/sift_mcp/src/tool/report_templates/mod.rs @@ -8,7 +8,10 @@ use rmcp::{ use serde::Deserialize; use sift_rs::metadata::v1::MetadataValue; -use crate::tool::common::bulk_create; +use crate::tool::common::{ + bulk::{self, BulkItems, Op}, + bulk_create, +}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, @@ -40,7 +43,7 @@ pub struct CreateReportTemplateItem { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateReportTemplateParams { +pub struct UpdateReportTemplateItem { report_template_id: String, name: Option, description: Option, @@ -51,6 +54,59 @@ pub struct UpdateReportTemplateParams { is_archived: Option, } +fn template_update( + item: UpdateReportTemplateItem, +) -> Result<(String, ReportTemplateUpdate), ErrorData> { + let UpdateReportTemplateItem { + report_template_id, + name, + description, + tag_names, + rule_ids, + rule_client_keys, + metadata, + is_archived, + } = item; + if report_template_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`report_template_id` must not be empty", + None, + )); + } + let rules = match (rule_ids, rule_client_keys) { + (None, None) => None, + (Some(ids), None) => Some(TemplateRuleIdentifier::RuleIds(ids)), + (None, Some(keys)) => Some(TemplateRuleIdentifier::RuleClientKeys(keys)), + (Some(_), Some(_)) => { + return Err(ErrorData::invalid_params( + "provide only one of `rule_ids` or `rule_client_keys`, not both", + None, + )); + } + }; + let changes = ReportTemplateUpdate { + name, + description, + tag_names, + rules, + is_archived, + metadata: metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()), + }; + if changes.name.is_none() + && changes.description.is_none() + && changes.tag_names.is_none() + && changes.rules.is_none() + && changes.metadata.is_none() + && changes.is_archived.is_none() + { + return Err(ErrorData::invalid_params( + "provide at least one field to update: `name`, `description`, `tag_names`, `rule_ids`, `rule_client_keys`, `metadata`, or `is_archived`", + None, + )); + } + Ok((report_template_id, changes)) +} + #[tool_router(router = report_templates_router, vis = "pub(crate)")] impl SiftMcpServer { #[tool( @@ -298,16 +354,22 @@ impl SiftMcpServer { #[tool( name = "update_report_template", description = " - Update selected fields on an existing report template. Wraps `report_templates/v1 UpdateReportTemplate`. + Update selected fields on one or more report templates. This is a WRITE. Wraps `report_templates/v1 UpdateReportTemplate`. Output: - - `{ \"report_template\": ReportTemplate, \"report_template_url\": string|null, \"next_step\": string }`. + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds + `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is + `{ \"report_template\": ReportTemplate, \"report_template_url\": string|null, \"next_step\": string }`. The returned template reflects the post-update server state. `report_template_url` is the template's Sift web link, or null when the host can't be derived. Link the template only to this URL, never to a report's page. Parameters: - - `report_template_id`: required; the id of the template to update. + - `items`: required array of 1–100 objects, one per template, with the fields below. + - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. + Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `report_template_id`: required in each item; the id of the template to update. - `name`: optional new name. - `description`: optional new description. - `tag_names`: optional REPLACEMENT list of tag names. Passing this overwrites the template's full @@ -326,12 +388,17 @@ impl SiftMcpServer { `metadata`, or `is_archived`) must be set. Errors: + - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, + or empty ids; errors name the item index. Item-local errors fail only that item. + Backend-wide errors stop the tail; earlier writes stay committed. - `INVALID_PARAMS` if `report_template_id` is empty, no updatable field is provided, or both `rule_ids` and `rule_client_keys` are provided. - `RESOURCE_NOT_FOUND` if no template matches `report_template_id`. - `INTERNAL_ERROR` for upstream gRPC failures. Guidance: + - Items run sequentially; this is not atomic. Use one call for several templates. Never replay + the whole call after a partial failure; retry only failed, not `not_selected`, items. - This is a write with REPLACE semantics on `tags`, `rules`, and `metadata`. CONFIRM the intended shape with the user — for appends, read the current template via `list_report_templates` filtered by `report_template_id == \"\"` and send the union. @@ -347,65 +414,32 @@ impl SiftMcpServer { )] pub async fn update_report_template( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(UpdateReportTemplateParams { - report_template_id, - name, - description, - tag_names, - rule_ids, - rule_client_keys, - metadata, - is_archived, - }) = params; - - if report_template_id.is_empty() { - return Err(ErrorData::invalid_params( - "`report_template_id` must not be empty", - None, - )); - } - - let rules = match (rule_ids, rule_client_keys) { - (None, None) => None, - (Some(ids), None) => Some(TemplateRuleIdentifier::RuleIds(ids)), - (None, Some(keys)) => Some(TemplateRuleIdentifier::RuleClientKeys(keys)), - (Some(_), Some(_)) => { - return Err(ErrorData::invalid_params( - "provide only one of `rule_ids` or `rule_client_keys`, not both", - None, - )); - } - }; - - let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); - - let changes = ReportTemplateUpdate { - name, - description, - tag_names, - rules, - metadata, - is_archived, - }; - - if changes.name.is_none() - && changes.description.is_none() - && changes.tag_names.is_none() - && changes.rules.is_none() - && changes.metadata.is_none() - && changes.is_archived.is_none() - { - return Err(ErrorData::invalid_params( - "provide at least one field to update: `name`, `description`, `tag_names`, `rule_ids`, \ - `rule_client_keys`, `metadata`, or `is_archived`", - None, - )); - } + let Parameters(params) = params; + let updates = bulk::parse_items::(params.items)? + .into_iter() + .enumerate() + .map(|(index, item)| template_update(item).map_err(|error| bulk::at_item(index, error))) + .collect::, _>>()?; + bulk::reject_duplicate_targets(&updates, "report template", |(id, _)| Some(id.clone()))?; + bulk::run( + Op::Update, + updates, + self.selection(params.selected_indices), + false, + |(id, changes)| self.update_report_template_item(id, changes), + ) + .await + } + async fn update_report_template_item( + &self, + report_template_id: String, + changes: ReportTemplateUpdate, + ) -> error::McpResult { let template = self .report_template_service .update_report_template(report_template_id, changes) @@ -422,8 +456,7 @@ impl SiftMcpServer { .build_report_template_url(&template.report_template_id) .ok(); let next_step = format!( - "Updated report template `{}` ({}).{}{} Surface the new state to the user and confirm nothing \ - was unintentionally dropped — `tags`, `rules`, and `metadata` use REPLACE semantics.", + "Updated report template `{}` ({}).{}{}", template.name, template.report_template_id, archive_clause, diff --git a/rust/crates/sift_mcp/src/tool/report_templates/test.rs b/rust/crates/sift_mcp/src/tool/report_templates/test.rs index 4956696d6..f1c9391cb 100644 --- a/rust/crates/sift_mcp/src/tool/report_templates/test.rs +++ b/rust/crates/sift_mcp/src/tool/report_templates/test.rs @@ -9,7 +9,7 @@ use sift_test_util::{ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; -use super::{CreateReportTemplateItem, ReportTemplateListParams, UpdateReportTemplateParams}; +use super::{CreateReportTemplateItem, ReportTemplateListParams, UpdateReportTemplateItem}; use crate::{server::SiftMcpServer, tool::common::test_support::structured_field}; async fn server_with_mock(mock: MockReportTemplateServiceImpl) -> (SiftMcpServer, JoinHandle<()>) { @@ -56,8 +56,126 @@ fn create_params() -> CreateReportTemplateItem { } } -fn update_params() -> UpdateReportTemplateParams { - UpdateReportTemplateParams { +mod bulk { + use super::*; + use serde_json::{Value, json}; + + async fn call(server: &SiftMcpServer, value: Value) -> crate::error::McpResult { + server + .update_report_template(Parameters(serde_json::from_value(value).unwrap())) + .await + } + fn item(id: &str) -> Value { + json!({"report_template_id": id, "name": "renamed"}) + } + fn body(result: rmcp::model::CallToolResult) -> Value { + result.structured_content.unwrap() + } + + #[tokio::test] + async fn selected_subset_writes_only_selected_items() { + let mut mock = MockReportTemplateServiceImpl::new(); + mock.expect_update_report_template() + .times(1) + .withf(|req| { + req.get_ref() + .report_template + .as_ref() + .unwrap() + .report_template_id + == "selected" + }) + .returning(|req| { + Ok(Response::new(UpdateReportTemplateResponse { + report_template: req.into_inner().report_template, + })) + }); + let (server, handle) = server_with_mock(mock).await; + let body = body( + call( + &server, + json!({"items": [item("skip"), item("selected")], "selected_indices": [1]}), + ) + .await + .unwrap(), + ); + assert_eq!(body["successes"][0]["index"], 1); + assert_eq!(body["not_selected"], json!([0])); + handle.abort(); + } + + #[tokio::test] + async fn not_found_continues_and_unavailable_stops_tail() { + let mut mock = MockReportTemplateServiceImpl::new(); + mock.expect_update_report_template().returning(|req| { + let template = req.into_inner().report_template.unwrap(); + match template.report_template_id.as_str() { + "missing" => Err(Status::not_found("missing template")), + "down" => Err(Status::unavailable("backend down")), + "never" => panic!("tail must not run"), + _ => Ok(Response::new(UpdateReportTemplateResponse { + report_template: Some(template), + })), + } + }); + let (server, handle) = server_with_mock(mock).await; + let body = body( + call( + &server, + json!({"items": [item("missing"), item("ok"), item("down"), item("never")]}), + ) + .await + .unwrap(), + ); + assert_eq!(body["updated_count"], 1); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 + ); + assert_eq!(body["failures"][1]["index"], 2); + assert_eq!(body["not_attempted"], json!([3])); + handle.abort(); + } + + #[tokio::test] + async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { + let (server, handle) = server_with_mock(MockReportTemplateServiceImpl::new()).await; + let server = server.with_require_selection(true); + for items in [ + json!([item("a"), item("a")]), + json!([item("a"), item(" ")]), + json!([{"report_template_id": "a", "unknown": true}]), + ] { + let error = call(&server, json!({"items": items, "selected_indices": [0]})) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + } + let error = call(&server, json!({"items": [item("a")]})) + .await + .unwrap_err(); + assert!(error.message.contains("nothing was written")); + handle.abort(); + } +} + +fn update_call( + item: UpdateReportTemplateItem, +) -> Parameters> { + assert!(item.metadata.is_none()); + Parameters( + serde_json::from_value(serde_json::json!({"items": [{ + "report_template_id": item.report_template_id, "name": item.name, + "description": item.description, "tag_names": item.tag_names, + "rule_ids": item.rule_ids, "rule_client_keys": item.rule_client_keys, + "is_archived": item.is_archived, + }]})) + .unwrap(), + ) +} + +fn update_params() -> UpdateReportTemplateItem { + UpdateReportTemplateItem { report_template_id: "tmpl-1".into(), name: None, description: None, @@ -257,9 +375,12 @@ async fn update_report_template_happy_path() { params.name = Some("renamed".into()); let resp = server - .update_report_template(Parameters(params)) + .update_report_template(update_call(params)) .await .expect("update_report_template failed"); + let resp = rmcp::model::CallToolResult::structured( + structured_field(resp, "successes")[0]["result"].clone(), + ); let url = structured_field(resp.clone(), "report_template_url"); assert_eq!(url, "https://app.test.local/report-templates/tmpl-1"); @@ -284,7 +405,7 @@ async fn update_report_template_rejects_empty_id() { params.name = Some("renamed".into()); let err = server - .update_report_template(Parameters(params)) + .update_report_template(update_call(params)) .await .expect_err("expected error"); @@ -296,7 +417,7 @@ async fn update_report_template_rejects_no_fields() { let (server, _h) = server_with_mock(MockReportTemplateServiceImpl::new()).await; let err = server - .update_report_template(Parameters(update_params())) + .update_report_template(update_call(update_params())) .await .expect_err("expected error"); @@ -312,7 +433,7 @@ async fn update_report_template_rejects_both_rule_shapes() { params.rule_client_keys = Some(vec!["ck-1".into()]); let err = server - .update_report_template(Parameters(params)) + .update_report_template(update_call(params)) .await .expect_err("expected error"); @@ -342,7 +463,7 @@ async fn update_report_template_blocked_without_allow_destructive() { params.name = Some("renamed".into()); let err = server - .update_report_template(Parameters(params)) + .update_report_template(update_call(params)) .await .expect_err("expected destructive gate to reject the call"); From 35bc777227df708963ce8468d351ab415a7fc100 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:22:56 -0800 Subject: [PATCH 08/19] rust(feat): take bulk items in artifact run and asset writes --- .../sift_mcp/src/tool/artifacts/bulk_test.rs | 232 ++++++++++++++++++ .../crates/sift_mcp/src/tool/artifacts/mod.rs | 216 ++++++++++++---- .../sift_mcp/src/tool/artifacts/test.rs | 34 +-- .../sift_mcp/src/tool/assets/bulk_test.rs | 108 ++++++++ rust/crates/sift_mcp/src/tool/assets/mod.rs | 83 +++++-- rust/crates/sift_mcp/src/tool/assets/test.rs | 7 +- .../sift_mcp/src/tool/runs/bulk_test.rs | 107 ++++++++ rust/crates/sift_mcp/src/tool/runs/mod.rs | 99 +++++--- rust/crates/sift_mcp/src/tool/runs/test.rs | 11 +- 9 files changed, 785 insertions(+), 112 deletions(-) create mode 100644 rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs create mode 100644 rust/crates/sift_mcp/src/tool/assets/bulk_test.rs create mode 100644 rust/crates/sift_mcp/src/tool/runs/bulk_test.rs diff --git a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs new file mode 100644 index 000000000..92c28fbdc --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs @@ -0,0 +1,232 @@ +use super::*; +use crate::{error, tool::common::bulk::BulkItems}; +use serde_json::{Value, json}; + +async fn call( + server: &SiftMcpServer, + op: &str, + items: Value, + selected: Option>, +) -> error::McpResult { + let args = json!({"items": items, "selected_indices": selected}); + match op { + "archive" => { + server + .archive_artifact(Parameters( + serde_json::from_value::>(args) + .unwrap(), + )) + .await + } + "unarchive" => { + server + .unarchive_artifact(Parameters( + serde_json::from_value::>(args) + .unwrap(), + )) + .await + } + _ => { + server + .update_artifact(Parameters( + serde_json::from_value::>(args) + .unwrap(), + )) + .await + } + } +} + +fn item(op: &str, id: &str) -> Value { + if op == "update" { + json!({"artifact_id":id,"title":"new title"}) + } else { + json!({"artifact_id":id}) + } +} + +fn updated(id: &str) -> UpdateArtifactResponse { + UpdateArtifactResponse { + artifact: Some(sample_with( + |artifact| artifact.artifact_id = id.into(), + |version| { + version.artifact_id = id.into(); + version.version = 2; + version.title = Some("new title".into()); + }, + )), + } +} + +#[tokio::test] +async fn selection_preserves_positions_and_artifact_relay_text() { + for op in ["archive", "unarchive", "update"] { + let mut mock = MockArtifactServiceImpl::new(); + match op { + "archive" => { + mock.expect_archive_artifact() + .times(2) + .withf(|req| req.get_ref().artifact_id != "skip") + .returning(|_| Ok(Response::new(ArchiveArtifactResponse {}))); + } + "unarchive" => { + mock.expect_unarchive_artifact() + .times(2) + .withf(|req| req.get_ref().artifact_id != "skip") + .returning(|_| Ok(Response::new(UnarchiveArtifactResponse {}))); + } + _ => { + mock.expect_update_artifact() + .times(2) + .withf(|req| req.get_ref().artifact_id != "skip") + .returning(|req| Ok(Response::new(updated(&req.get_ref().artifact_id)))); + } + } + let (server, _h) = server_with_mock(mock, true).await; + let body = structured( + call( + &server, + op, + json!([item(op, "a"), item(op, "skip"), item(op, "c")]), + Some(vec![0, 2]), + ) + .await + .unwrap(), + ); + let count = match op { + "archive" => "archived_count", + "unarchive" => "unarchived_count", + _ => "updated_count", + }; + assert_eq!(body[count], 2); + assert_eq!(body["not_selected"], json!([1])); + assert_eq!(body["successes"][1]["index"], 2); + if op == "update" { + assert!( + body["successes"][0]["result"]["next_step"] + .as_str() + .unwrap() + .starts_with("Wrote version 2 of artifact a") + ); + assert_eq!( + body["successes"][1]["result"]["artifact"]["artifactId"], + "c" + ); + } + } +} + +#[tokio::test] +async fn resource_errors_continue_and_backend_errors_stop() { + for op in ["archive", "unarchive", "update"] { + for code in [ + tonic::Code::NotFound, + tonic::Code::PermissionDenied, + tonic::Code::Unavailable, + ] { + let mut mock = MockArtifactServiceImpl::new(); + let stop = code == tonic::Code::Unavailable; + match op { + "archive" => { + mock.expect_archive_artifact() + .withf(|req| req.get_ref().artifact_id == "bad") + .returning(move |_| Err(Status::new(code, "failed"))); + if !stop { + mock.expect_archive_artifact() + .times(1) + .withf(|req| req.get_ref().artifact_id == "good") + .returning(|_| Ok(Response::new(ArchiveArtifactResponse {}))); + } + } + "unarchive" => { + mock.expect_unarchive_artifact() + .withf(|req| req.get_ref().artifact_id == "bad") + .returning(move |_| Err(Status::new(code, "failed"))); + if !stop { + mock.expect_unarchive_artifact() + .times(1) + .withf(|req| req.get_ref().artifact_id == "good") + .returning(|_| Ok(Response::new(UnarchiveArtifactResponse {}))); + } + } + _ => { + mock.expect_update_artifact() + .withf(|req| req.get_ref().artifact_id == "bad") + .returning(move |_| Err(Status::new(code, "failed"))); + if !stop { + mock.expect_update_artifact() + .times(1) + .withf(|req| req.get_ref().artifact_id == "good") + .returning(|req| { + Ok(Response::new(updated(&req.get_ref().artifact_id))) + }); + } + } + } + let (server, _h) = server_with_mock(mock, true).await; + let result = call( + &server, + op, + json!([item(op, "bad"), item(op, "good")]), + None, + ) + .await + .unwrap(); + assert_eq!(result.is_error, Some(true)); + let body = structured(result); + assert_eq!(body["failures"].as_array().unwrap().len(), 1); + if op == "update" { + assert_eq!(body["failures"][0]["outcome_unknown"], true); + } + assert_eq!( + body["successes"].as_array().unwrap().len(), + usize::from(!stop) + ); + assert_eq!( + body["not_attempted"].as_array().unwrap().len(), + usize::from(stop) + ); + } + } +} + +#[tokio::test] +async fn invalid_items_duplicates_and_missing_masks_write_nothing() { + for op in ["archive", "unarchive", "update"] { + let (server, _h) = server_with_mock(MockArtifactServiceImpl::new(), true).await; + for items in [ + json!([item(op, "a"), item(op, " a ")]), + json!([item(op, "a"), item(op, " ")]), + json!([{"artifact_id":"a","unknown":true}]), + ] { + assert_eq!( + call(&server, op, items, None).await.unwrap_err().code, + ErrorCode::INVALID_PARAMS + ); + } + if op == "update" { + for bad in [ + json!({"artifact_id":"b"}), + json!({"artifact_id":"b","file_path":""}), + json!({"artifact_id":"b","file_path":"/nonexistent/bulk-artifact"}), + json!({"artifact_id":"b","links":[{"entity_id":"x","entity_type":"invalid","relation":"source"}]}), + ] { + assert_eq!( + call(&server, op, json!([item(op, "a"), bad]), None) + .await + .unwrap_err() + .code, + ErrorCode::INVALID_PARAMS + ); + } + } + let server = server.with_require_selection(true); + assert!( + call(&server, op, json!([item(op, "a")]), None) + .await + .unwrap_err() + .message + .contains("approval host") + ); + } +} diff --git a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs index 15cc4b828..99bb02697 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs @@ -12,7 +12,10 @@ use sift_rs::artifacts::v1::{ ArtifactLinkRelation, ArtifactStorageClass, ArtifactVersion, }; -use crate::tool::common::bulk_create; +use crate::tool::common::{ + bulk::{self, BulkItems, Op}, + bulk_create, +}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, @@ -47,7 +50,7 @@ pub struct ArtifactVersionListParams { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct ArtifactArchiveParams { +pub struct ArtifactArchiveItem { artifact_id: String, } @@ -64,7 +67,7 @@ pub struct CreateArtifactItem { } #[derive(Debug, Default, Deserialize, JsonSchema)] -pub struct UpdateArtifactParams { +pub struct UpdateArtifactItem { artifact_id: String, title: Option, summary: Option, @@ -73,7 +76,7 @@ pub struct UpdateArtifactParams { file_path: Option, } -#[derive(Debug, Deserialize, JsonSchema)] +#[derive(Debug, Clone, Deserialize, JsonSchema)] pub struct ArtifactLinkParam { relation: String, entity_type: String, @@ -225,6 +228,40 @@ fn parse_links(links: Option>) -> Result Result<(), ErrorData> { + if item.artifact_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`artifact_id` must not be empty", + None, + )); + } + let file_path = normalized_file_path(item.file_path.clone())?; + let links = parse_links(item.links.clone())?; + if item.title.is_none() + && item.summary.is_none() + && item.metadata.is_none() + && links.is_empty() + && file_path.is_none() + { + return Err(ErrorData::invalid_params( + "pass at least one of `title`, `summary`, `metadata`, `links`, or `file_path`", + None, + )); + } + if let Some(path) = file_path { + let metadata = std::fs::metadata(&path).map_err(|error| { + ErrorData::invalid_params(format!("invalid `file_path` `{path}`: {error}"), None) + })?; + if !metadata.is_file() || metadata.len() == 0 || metadata.len() > 1024 * 1024 * 1024 { + return Err(ErrorData::invalid_params( + "`file_path` must name a regular, non-empty file up to 1 GiB", + None, + )); + } + } + Ok(()) +} + fn content_note(artifact: &ArtifactView, uploaded: bool) -> &'static str { if uploaded && artifact.download_url.is_some() { " Its file content was uploaded and the user can preview and download it." @@ -571,26 +608,32 @@ impl SiftMcpServer { #[tool( name = "archive_artifact", description = " - Archive an artifact. Archived artifacts stay stored but disappear from default artifact listings. - This is a WRITE. Reversible with `unarchive_artifact`. + Archive one or more artifacts. Archived artifacts stay stored but disappear from default listings. + This is a WRITE. Reversible with `unarchive_artifact`. Already archived artifacts succeed unchanged. Output: - - `{ \"artifact_id\": string, \"archived\": true, \"artifact_url\": string|null, - \"next_step\": string }`. `artifact_url` is the artifact's Sift web link - (`/artifacts/`), or null when the host can't be derived. + - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, + not_attempted, next_step }`. Each success holds `{ index, result }`, where result has + `artifact_id`, `archived: true`, `artifact_url`, and `next_step`. The URL links the artifact + in Sift, or is null if the host cannot be derived. Failures hold `{ index, error, outcome_unknown }`. + Any failure sets `isError`. Parameters: - - `artifact_id`: required stable artifact id. + - `items`: required array of 1–100 objects. Each item takes `artifact_id`, the stable artifact id. + - `selected_indices`: zero-based item positions supplied by the approval host; omit when proposing. + Omitted selects all outside chat; chat requires a mask. `[]` archives nothing. Errors: - - `INVALID_PARAMS` if `artifact_id` is empty. - - `INVALID_REQUEST` if the server was launched without `--allow-destructive`. - - `RESOURCE_NOT_FOUND` if the artifact does not exist or is not visible to the caller. - - `INTERNAL_ERROR` for upstream failures, including a stop signal when the caller did not - create the artifact. + - `INVALID_PARAMS` before any write for empty or oversized `items`, duplicate targets, unknown + item fields, or an empty id. The message names the item index. + - `INVALID_REQUEST` without `--allow-destructive`. + - `RESOURCE_NOT_FOUND` if an artifact is missing. Permission errors fail only that item. + - Backend-wide errors stop remaining items; earlier writes stay committed. Guidance: - - Archiving preserves every version, link, and uploaded file. Confirm the target with the user. + - Items run sequentially; this is not an atomic batch. Never replay the whole call after a + partial failure. Check `outcome_unknown` items before retrying, and never retry `not_selected` items. + - Archiving preserves every version, link, and uploaded file. Confirm targets with the user. - Use `list_artifacts` with `include_archived: true` to find archived artifacts. ", annotations( @@ -602,10 +645,17 @@ impl SiftMcpServer { )] pub async fn archive_artifact( &self, - params: Parameters, + params: Parameters>, + ) -> error::McpResult { + self.archive_artifacts(params, Op::Archive).await + } + + async fn archive_artifact_item( + &self, + params: Parameters, ) -> error::McpResult { self.require_destructive()?; - let Parameters(ArtifactArchiveParams { artifact_id }) = params; + let Parameters(ArtifactArchiveItem { artifact_id }) = params; // The service trims the id, so the link has to use the trimmed value too. let artifact_id = artifact_id.trim().to_owned(); if artifact_id.is_empty() { @@ -622,8 +672,7 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let next_step = format!( - "Archived artifact `{artifact_id}`.{} Tell the user it is hidden from default artifact \ - listings and that `unarchive_artifact` restores it.", + "Archived artifact `{artifact_id}`.{}", url_clause("artifact", None, artifact_url.as_deref()), ); let mut result = CallToolResult::structured(serde_json::json!({ @@ -639,26 +688,32 @@ impl SiftMcpServer { #[tool( name = "unarchive_artifact", description = " - Restore an archived artifact so it appears in default artifact listings again. This is a WRITE. + Restore one or more archived artifacts to default listings. This is a WRITE. + Already unarchived artifacts succeed unchanged. Output: - - `{ \"artifact_id\": string, \"unarchived\": true, \"artifact_url\": string|null, - \"next_step\": string }`. `artifact_url` is the artifact's Sift web link - (`/artifacts/`), or null when the host can't be derived. + - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, + not_attempted, next_step }`. Each success holds `{ index, result }`, where result has + `artifact_id`, `unarchived: true`, `artifact_url`, and `next_step`. The URL links the artifact + in Sift, or is null if the host cannot be derived. Failures hold `{ index, error, outcome_unknown }`. + Any failure sets `isError`. Parameters: - - `artifact_id`: required stable artifact id. + - `items`: required array of 1–100 objects. Each item takes `artifact_id`, the stable artifact id. + - `selected_indices`: zero-based item positions supplied by the approval host; omit when proposing. + Omitted selects all outside chat; chat requires a mask. `[]` unarchives nothing. Errors: - - `INVALID_PARAMS` if `artifact_id` is empty. - - `INVALID_REQUEST` if the server was launched without `--allow-destructive`. - - `RESOURCE_NOT_FOUND` if the artifact does not exist or is not visible to the caller. - - `INTERNAL_ERROR` for upstream failures, including a stop signal when the caller did not - create the artifact. + - `INVALID_PARAMS` before any write for empty or oversized `items`, duplicate targets, unknown + item fields, or an empty id. The message names the item index. + - `INVALID_REQUEST` without `--allow-destructive`. + - `RESOURCE_NOT_FOUND` if an artifact is missing. Permission errors fail only that item. + - Backend-wide errors stop remaining items; earlier writes stay committed. Guidance: - - Confirm the target with the user before calling. - - Find archived artifacts with `list_artifacts` and `include_archived: true`. + - Items run sequentially; this is not an atomic batch. Never replay the whole call after a + partial failure. Check `outcome_unknown` items before retrying, and never retry `not_selected` items. + - Confirm targets with the user. Find archived artifacts with `list_artifacts` and `include_archived: true`. ", annotations( title = "artifacts/unarchive_artifact", @@ -669,10 +724,54 @@ impl SiftMcpServer { )] pub async fn unarchive_artifact( &self, - params: Parameters, + params: Parameters>, + ) -> error::McpResult { + self.archive_artifacts(params, Op::Unarchive).await + } + + async fn archive_artifacts( + &self, + params: Parameters>, + op: Op, ) -> error::McpResult { self.require_destructive()?; - let Parameters(ArtifactArchiveParams { artifact_id }) = params; + let Parameters(BulkItems { + items, + selected_indices, + .. + }) = params; + let mut items = bulk::parse_items::(items)?; + for (index, item) in items.iter_mut().enumerate() { + item.artifact_id = item.artifact_id.trim().to_owned(); + if item.artifact_id.is_empty() { + return Err(bulk::at_item( + index, + ErrorData::invalid_params("`artifact_id` must not be empty", None), + )); + } + } + bulk::reject_duplicate_targets(&items, "artifact", |item| Some(item.artifact_id.clone()))?; + bulk::run( + op, + items, + self.selection(selected_indices), + false, + |item| async move { + match op { + Op::Archive => self.archive_artifact_item(Parameters(item)).await, + _ => self.unarchive_artifact_item(Parameters(item)).await, + } + }, + ) + .await + } + + async fn unarchive_artifact_item( + &self, + params: Parameters, + ) -> error::McpResult { + self.require_destructive()?; + let Parameters(ArtifactArchiveItem { artifact_id }) = params; // The service trims the id, so the link has to use the trimmed value too. let artifact_id = artifact_id.trim().to_owned(); if artifact_id.is_empty() { @@ -689,8 +788,7 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let next_step = format!( - "Unarchived artifact `{artifact_id}`.{} Tell the user it is restored to default artifact \ - listings.", + "Unarchived artifact `{artifact_id}`.{}", url_clause("artifact", None, artifact_url.as_deref()), ); let mut result = CallToolResult::structured(serde_json::json!({ @@ -881,18 +979,24 @@ impl SiftMcpServer { #[tool( name = "update_artifact", description = " - Write a new version of an existing artifact, changing only what you pass. This is a WRITE. + Write new versions of one or more existing artifacts, changing only what you pass. This is a WRITE. Every field you leave out keeps its current value, so a title-only edit does not have to resend the metadata or the file. That is the whole point of this tool: passing nothing for a field means `leave it alone`, never `blank it`. Output: + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. Each success holds `{ index, result }`, each failure holds + `{ index, error, outcome_unknown }`, and any failure sets `isError`. Each result is: - `{ \"artifact\": Artifact, \"artifact_url\": string|null, \"next_step\": string }`. Same shape as `download_artifact`, resolved to the version this call wrote. `artifact_url` links the artifact rather than this version, so it keeps showing the latest. Parameters: + - `items`: required array of 1–100 objects, one per artifact. Each item takes the fields below. + - `selected_indices`: zero-based item positions supplied by the approval host; omit when proposing. + Omitted selects all outside chat; chat requires a mask. `[]` updates nothing. - `artifact_id`: required stable container id. Resolve it with `list_artifacts` if you only have a title. - `title`: new display title. Pass an empty string to leave the artifact showing its file name. @@ -917,8 +1021,13 @@ impl SiftMcpServer { - `RESOURCE_NOT_FOUND` if the artifact does not exist or is not visible to the caller. - `INTERNAL_ERROR` for upstream failures. When the message says the version was written but the upload failed, the version exists without content — report that instead of calling again. + - `INVALID_PARAMS` before any write for empty or oversized `items`, duplicate targets, unknown + item fields, or invalid items. The message names the item index. + - Item-local errors fail only that item. Backend-wide errors stop remaining items. Guidance: + - Items run sequentially; this is not an atomic batch. Never replay the whole call after a + partial failure. Check `outcome_unknown` items before retrying, and never retry `not_selected` items. - This is how you refine an artifact. Do NOT call `create_artifact` again for an edit; that mints a duplicate the user has to reconcile. - Confirm the change with the user before calling. @@ -932,11 +1041,37 @@ impl SiftMcpServer { )] pub async fn update_artifact( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + let Parameters(BulkItems { + items, + selected_indices, + .. + }) = params; + let mut items = bulk::parse_items::(items)?; + for (index, item) in items.iter_mut().enumerate() { + item.artifact_id = item.artifact_id.trim().to_owned(); + validate_update_artifact(item).map_err(|error| bulk::at_item(index, error))?; + } + bulk::reject_duplicate_targets(&items, "artifact", |item| Some(item.artifact_id.clone()))?; + bulk::run( + Op::Update, + items, + self.selection(selected_indices), + true, + |item| self.update_artifact_item(Parameters(item)), + ) + .await + } - let Parameters(UpdateArtifactParams { + async fn update_artifact_item( + &self, + params: Parameters, + ) -> error::McpResult { + self.require_destructive()?; + validate_update_artifact(¶ms.0)?; + let Parameters(UpdateArtifactItem { artifact_id, title, summary, @@ -995,8 +1130,7 @@ impl SiftMcpServer { let artifact_url = artifact_url_for(self, &artifact.artifact_id(), None); let next_step = format!( - "Wrote version {} of artifact {}.{}{} Surface the new version to the user and confirm it \ - matches their intent.", + "Wrote version {} of artifact {}.{}{}", artifact.version().version, artifact.artifact_id(), content_note(&artifact, uploaded), diff --git a/rust/crates/sift_mcp/src/tool/artifacts/test.rs b/rust/crates/sift_mcp/src/tool/artifacts/test.rs index e3f53847e..4eea72977 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/test.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/test.rs @@ -1,3 +1,6 @@ +#[path = "bulk_test.rs"] +mod bulk; + use rmcp::{handler::server::wrapper::Parameters, model::ErrorCode}; use sift_rs::{ artifacts::v1::{ @@ -19,8 +22,9 @@ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; use super::{ - ArtifactArchiveParams, ArtifactVersionListParams, CreateArtifactItem, DownloadArtifactParams, - UpdateArtifactParams, display_name, parse_created_via, parse_storage_class, + ArtifactArchiveItem as ArtifactArchiveParams, ArtifactVersionListParams, CreateArtifactItem, + DownloadArtifactParams, UpdateArtifactItem as UpdateArtifactParams, display_name, + parse_created_via, parse_storage_class, }; use crate::{ server::SiftMcpServer, @@ -434,7 +438,7 @@ async fn archive_artifact_blocked_without_allow_destructive() { ) .await; let err = server - .archive_artifact(Parameters(ArtifactArchiveParams { + .archive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: "art-1".into(), })) .await @@ -453,7 +457,7 @@ async fn unarchive_artifact_blocked_without_allow_destructive() { ) .await; let err = server - .unarchive_artifact(Parameters(ArtifactArchiveParams { + .unarchive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: "art-1".into(), })) .await @@ -466,7 +470,7 @@ async fn unarchive_artifact_blocked_without_allow_destructive() { async fn archive_artifact_rejects_empty_artifact_id() { let (server, _h) = server_with_mock(MockArtifactServiceImpl::new(), true).await; let err = server - .archive_artifact(Parameters(ArtifactArchiveParams { + .archive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: " ".into(), })) .await @@ -478,7 +482,7 @@ async fn archive_artifact_rejects_empty_artifact_id() { async fn unarchive_artifact_rejects_empty_artifact_id() { let (server, _h) = server_with_mock(MockArtifactServiceImpl::new(), true).await; let err = server - .unarchive_artifact(Parameters(ArtifactArchiveParams { + .unarchive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: " ".into(), })) .await @@ -495,7 +499,7 @@ async fn archive_artifact_returns_structured_result() { let (server, _h) = server_with_mock(mock, true).await; let response = server - .archive_artifact(Parameters(ArtifactArchiveParams { + .archive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: "art-1".into(), })) .await @@ -523,7 +527,7 @@ async fn unarchive_artifact_returns_structured_result() { let (server, _h) = server_with_mock(mock, true).await; let response = server - .unarchive_artifact(Parameters(ArtifactArchiveParams { + .unarchive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: "art-1".into(), })) .await @@ -554,13 +558,13 @@ async fn archive_and_unarchive_trim_the_artifact_id() { let (server, _h) = server_with_mock(mock, true).await; let archived = server - .archive_artifact(Parameters(ArtifactArchiveParams { + .archive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: " art-1\n".into(), })) .await .expect("archive"); let unarchived = server - .unarchive_artifact(Parameters(ArtifactArchiveParams { + .unarchive_artifact_item(Parameters(ArtifactArchiveParams { artifact_id: " art-1\n".into(), })) .await @@ -595,7 +599,7 @@ async fn update_artifact_blocked_without_allow_destructive() { ) .await; let err = server - .update_artifact(Parameters(UpdateArtifactParams { + .update_artifact_item(Parameters(UpdateArtifactParams { artifact_id: "art-1".into(), title: Some("v2".into()), ..Default::default() @@ -640,7 +644,7 @@ async fn update_artifact_masks_only_what_was_passed() { let (server, _h) = server_with_mock(mock, true).await; let resp = server - .update_artifact(Parameters(UpdateArtifactParams { + .update_artifact_item(Parameters(UpdateArtifactParams { artifact_id: "art-1".into(), title: Some("Renamed".into()), ..Default::default() @@ -692,7 +696,7 @@ async fn update_artifact_without_a_file_reports_the_kept_file() { let (server, _h) = server_with_mocks(mock, remote_files, true, true).await; let resp = server - .update_artifact(Parameters(UpdateArtifactParams { + .update_artifact_item(Parameters(UpdateArtifactParams { artifact_id: "art-1".into(), title: Some("Renamed".into()), ..Default::default() @@ -763,7 +767,7 @@ async fn update_artifact_maps_each_parameter_to_its_own_path() { }); let (server, _h) = server_with_mock(mock, true).await; server - .update_artifact(Parameters(params)) + .update_artifact_item(Parameters(params)) .await .unwrap_or_else(|err| panic!("{want}: {err:?}")); } @@ -784,7 +788,7 @@ async fn update_artifact_requires_an_id_and_something_to_change() { }, ] { let err = server - .update_artifact(Parameters(params)) + .update_artifact_item(Parameters(params)) .await .expect_err("rejected"); assert_eq!(err.code, ErrorCode::INVALID_PARAMS); diff --git a/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs b/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs new file mode 100644 index 000000000..f36b89b52 --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs @@ -0,0 +1,108 @@ +use super::*; +use crate::tool::common::{bulk::BulkItems, test_support::structured}; +use serde_json::{Value, json}; +use sift_rs::assets::v1::UpdateAssetResponse; + +fn params( + items: Value, + selected: Option>, +) -> Parameters> { + Parameters( + serde_json::from_value(json!({"items": items, "selected_indices": selected})).unwrap(), + ) +} + +#[tokio::test] +async fn selected_subset_only_updates_selected_assets() { + let mut mock = MockAssetServiceImpl::new(); + mock.expect_update_asset() + .times(2) + .withf(|req| req.get_ref().asset.as_ref().unwrap().asset_id != "skip") + .returning(|req| { + Ok(Response::new(UpdateAssetResponse { + asset: req.into_inner().asset, + })) + }); + let (server, _h) = server_with_mock(mock).await; + let body = structured(server.update_asset(params(json!([ + {"asset_id":"a","tags":[]}, {"asset_id":"skip","tags":[]}, {"asset_id":"c","tags":[]} + ]), Some(vec![0,2]))).await.unwrap()); + assert_eq!(body["updated_count"], 2); + assert_eq!(body["not_selected"], json!([1])); + assert_eq!(body["successes"][1]["index"], 2); + assert!( + body["successes"][0]["result"]["asset_url"] + .as_str() + .unwrap() + .contains("/asset/a") + ); +} + +#[tokio::test] +async fn item_errors_continue_but_unavailable_stops_the_tail() { + for code in [ + tonic::Code::NotFound, + tonic::Code::PermissionDenied, + tonic::Code::Unavailable, + ] { + let mut mock = MockAssetServiceImpl::new(); + mock.expect_update_asset() + .withf(|req| req.get_ref().asset.as_ref().unwrap().asset_id == "bad") + .returning(move |_| Err(Status::new(code, "failed"))); + if code != tonic::Code::Unavailable { + mock.expect_update_asset() + .times(1) + .withf(|req| req.get_ref().asset.as_ref().unwrap().asset_id == "good") + .returning(|req| { + Ok(Response::new(UpdateAssetResponse { + asset: req.into_inner().asset, + })) + }); + } + let (server, _h) = server_with_mock(mock).await; + let result = server + .update_asset(params( + json!([{"asset_id":"bad","tags":[]},{"asset_id":"good","tags":[]}]), + None, + )) + .await + .unwrap(); + assert_eq!(result.is_error, Some(true)); + let body = structured(result); + assert_eq!(body["failures"].as_array().unwrap().len(), 1); + assert_eq!( + body["not_attempted"].as_array().unwrap().len(), + usize::from(code == tonic::Code::Unavailable) + ); + assert_eq!( + body["updated_count"], + usize::from(code != tonic::Code::Unavailable) + ); + } +} + +#[tokio::test] +async fn invalid_calls_and_missing_chat_masks_write_nothing() { + let (server, _h) = server_with_mock(MockAssetServiceImpl::new()).await; + for items in [ + json!([{"asset_id":"a","tags":[]},{"asset_id":"a","tags":[]}]), + json!([{"asset_id":"a","tags":[]},{"asset_id":" ","tags":[]}]), + json!([{"asset_id":"a","tags":[]},{"asset_id":"b"}]), + json!([{"asset_id":"a","tags":[],"unknown":true}]), + ] { + assert_eq!( + server + .update_asset(params(items, None)) + .await + .unwrap_err() + .code, + ErrorCode::INVALID_PARAMS + ); + } + let error = server + .with_require_selection(true) + .update_asset(params(json!([{"asset_id":"a","tags":[]}]), None)) + .await + .unwrap_err(); + assert!(error.message.contains("approval host")); +} diff --git a/rust/crates/sift_mcp/src/tool/assets/mod.rs b/rust/crates/sift_mcp/src/tool/assets/mod.rs index bdfb70501..7218b3500 100644 --- a/rust/crates/sift_mcp/src/tool/assets/mod.rs +++ b/rust/crates/sift_mcp/src/tool/assets/mod.rs @@ -11,11 +11,15 @@ use sift_rs::metadata::v1::MetadataValue; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, - tool::common::{ListParams, MetadataEntry, list_body, url_clause, with_urls}, + tool::common::{ + ListParams, MetadataEntry, + bulk::{self, BulkItems, Op}, + list_body, url_clause, with_urls, + }, }; #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateAssetParams { +pub struct UpdateAssetItem { asset_id: String, tags: Option>, metadata: Option>, @@ -24,6 +28,22 @@ pub struct UpdateAssetParams { #[cfg(test)] mod test; +fn validate_update_asset(item: &UpdateAssetItem) -> Result<(), ErrorData> { + if item.asset_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`asset_id` must not be empty", + None, + )); + } + if item.tags.is_none() && item.metadata.is_none() { + return Err(ErrorData::invalid_params( + "at least one of `tags` or `metadata` must be provided", + None, + )); + } + Ok(()) +} + #[tool_router(router = assets_router, vis = "pub(crate)")] impl SiftMcpServer { #[tool( @@ -102,14 +122,20 @@ impl SiftMcpServer { #[tool( name = "update_asset", description = " - Update an existing asset's tags and/or metadata. Wraps `assets/v1 UpdateAsset`. + Update one or more assets' tags and/or metadata. Wraps `assets/v1 UpdateAsset`. Output: + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. Each success holds `{ index, result }`, each failure holds + `{ index, error, outcome_unknown }`, and any failure sets `isError`. Each result is: - `{ \"asset\": Asset, \"asset_url\": string|null, \"next_step\": string }`. The returned `Asset` is the post-update state from the server; `asset_url` is its Sift web link (`/asset/`), or null when the host can't be derived. Parameters: + - `items`: required array of 1–100 objects, one per asset. Each item takes the fields below. + - `selected_indices`: zero-based item positions supplied by the approval host; omit when + proposing. Omitted selects all outside chat; chat requires a mask. `[]` updates nothing. - `asset_id`: required; the id of the asset to update. - `tags`: optional; REPLACES the asset's full tag list with this array. To add a single tag, first read the current tags via `list_assets` filtered by @@ -130,8 +156,13 @@ impl SiftMcpServer { type does not match an existing metadata key's type. - `RESOURCE_NOT_FOUND` if no asset matches `asset_id`. - `INTERNAL_ERROR` for upstream gRPC failures. + - `INVALID_PARAMS` before any write for an empty or oversized `items`, duplicate targets, + unknown item fields, or invalid items; the error names the item index. + - Item-local errors fail only that item. Backend-wide errors stop the remaining items. Guidance: + - Items run sequentially; the call is not atomic. Never replay the whole call after a + partial failure. Check `outcome_unknown` items before retrying, and never retry `not_selected` items. - This is a write. CONFIRM the target asset and the full proposed `tags` / `metadata` lists with the user before invoking — silent truncation is the most common foot-gun because the operation is REPLACE, not merge. @@ -146,28 +177,40 @@ impl SiftMcpServer { idempotent_hint = true, ) )] - pub async fn update_asset(&self, params: Parameters) -> error::McpResult { + pub async fn update_asset( + &self, + params: Parameters>, + ) -> error::McpResult { self.require_destructive()?; + let Parameters(BulkItems { + items, + selected_indices, + .. + }) = params; + let items = bulk::parse_items::(items)?; + for (index, item) in items.iter().enumerate() { + validate_update_asset(item).map_err(|error| bulk::at_item(index, error))?; + } + bulk::reject_duplicate_targets(&items, "asset", |item| Some(item.asset_id.clone()))?; + bulk::run( + Op::Update, + items, + self.selection(selected_indices), + false, + |item| self.update_asset_item(Parameters(item)), + ) + .await + } - let Parameters(UpdateAssetParams { + async fn update_asset_item(&self, params: Parameters) -> error::McpResult { + self.require_destructive()?; + validate_update_asset(¶ms.0)?; + let Parameters(UpdateAssetItem { asset_id, tags, metadata, }) = params; - if asset_id.is_empty() { - return Err(ErrorData::invalid_params( - "`asset_id` must not be empty", - None, - )); - } - if tags.is_none() && metadata.is_none() { - return Err(ErrorData::invalid_params( - "at least one of `tags` or `metadata` must be provided", - None, - )); - } - let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); let asset = self @@ -178,9 +221,7 @@ impl SiftMcpServer { let asset_url = self.url_service.build_asset_url(&asset.asset_id).ok(); let next_step = format!( - "Updated asset `{}` ({}).{} Surface the new state to the user and confirm the change \ - matches their intent. Remember: tags and metadata are REPLACE operations — verify \ - nothing was unintentionally dropped.", + "Updated asset `{}` ({}).{}", asset.name, asset.asset_id, url_clause("asset", Some(&asset.name), asset_url.as_deref()), diff --git a/rust/crates/sift_mcp/src/tool/assets/test.rs b/rust/crates/sift_mcp/src/tool/assets/test.rs index b96fe88c0..08e6a6313 100644 --- a/rust/crates/sift_mcp/src/tool/assets/test.rs +++ b/rust/crates/sift_mcp/src/tool/assets/test.rs @@ -1,3 +1,6 @@ +#[path = "bulk_test.rs"] +mod bulk; + use rmcp::{ServerHandler, handler::server::wrapper::Parameters, model::ErrorCode}; use sift_rs::assets::v1::{Asset, ListAssetsResponse, asset_service_server::AssetServiceServer}; use sift_test_util::{grpc::memory_sift_channel, mock::assets::v1::MockAssetServiceImpl}; @@ -8,7 +11,7 @@ use crate::{ server::SiftMcpServer, service::common::DEFAULT_LIMIT, tool::{ - assets::UpdateAssetParams, + assets::UpdateAssetItem as UpdateAssetParams, common::test_support::{list_params, structured_field}, }, }; @@ -242,7 +245,7 @@ async fn update_asset_blocked_without_allow_destructive() { let (server, _h) = server_with_mock_and_flag(mock, false).await; let err = server - .update_asset(Parameters(UpdateAssetParams { + .update_asset_item(Parameters(UpdateAssetParams { asset_id: "a1".into(), tags: Some(vec!["engine".into()]), metadata: None, diff --git a/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs b/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs new file mode 100644 index 000000000..d79bc2bab --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs @@ -0,0 +1,107 @@ +use super::*; +use crate::tool::common::bulk::BulkItems; +use serde_json::{Value, json}; + +fn params( + items: Value, + selected: Option>, +) -> Parameters> { + Parameters( + serde_json::from_value(json!({"items": items, "selected_indices": selected})).unwrap(), + ) +} + +#[tokio::test] +async fn selected_subset_only_updates_selected_runs() { + let mut mock = MockRunServiceImpl::new(); + mock.expect_update_run() + .times(2) + .withf(|req| req.get_ref().run.as_ref().unwrap().run_id != "skip") + .returning(|req| { + Ok(Response::new(UpdateRunResponse { + run: req.into_inner().run, + })) + }); + let (server, _h) = server_with_mock(mock).await; + let body = structured(server.update_run(params(json!([ + {"run_id":"a","name":"first"}, {"run_id":"skip","name":"second"}, {"run_id":"c","name":"third"} + ]), Some(vec![0,2]))).await.unwrap()); + assert_eq!(body["updated_count"], 2); + assert_eq!(body["not_selected"], json!([1])); + assert_eq!(body["successes"][1]["index"], 2); + assert!( + body["successes"][0]["result"]["run_url"] + .as_str() + .unwrap() + .contains("/run/a") + ); +} + +#[tokio::test] +async fn item_errors_continue_but_unavailable_stops_the_tail() { + for code in [ + tonic::Code::NotFound, + tonic::Code::PermissionDenied, + tonic::Code::Unavailable, + ] { + let mut mock = MockRunServiceImpl::new(); + mock.expect_update_run() + .withf(|req| req.get_ref().run.as_ref().unwrap().run_id == "bad") + .returning(move |_| Err(Status::new(code, "failed"))); + if code != tonic::Code::Unavailable { + mock.expect_update_run() + .times(1) + .withf(|req| req.get_ref().run.as_ref().unwrap().run_id == "good") + .returning(|req| { + Ok(Response::new(UpdateRunResponse { + run: req.into_inner().run, + })) + }); + } + let (server, _h) = server_with_mock(mock).await; + let result = server + .update_run(params( + json!([{"run_id":"bad","name":"x"},{"run_id":"good","name":"y"}]), + None, + )) + .await + .unwrap(); + assert_eq!(result.is_error, Some(true)); + let body = structured(result); + assert_eq!(body["failures"].as_array().unwrap().len(), 1); + assert_eq!( + body["not_attempted"].as_array().unwrap().len(), + usize::from(code == tonic::Code::Unavailable) + ); + assert_eq!( + body["updated_count"], + usize::from(code != tonic::Code::Unavailable) + ); + } +} + +#[tokio::test] +async fn invalid_calls_and_missing_chat_masks_write_nothing() { + let (server, _h) = server_with_mock(MockRunServiceImpl::new()).await; + for items in [ + json!([{"run_id":"a","name":"x"},{"run_id":"a","name":"y"}]), + json!([{"run_id":"a","name":"x"},{"run_id":" " ,"name":"y"}]), + json!([{"run_id":"a","name":"x"},{"run_id":"b"}]), + json!([{"run_id":"a","name":"x","unknown":true}]), + ] { + assert_eq!( + server + .update_run(params(items, None)) + .await + .unwrap_err() + .code, + ErrorCode::INVALID_PARAMS + ); + } + let error = server + .with_require_selection(true) + .update_run(params(json!([{"run_id":"a","name":"x"}]), None)) + .await + .unwrap_err(); + assert!(error.message.contains("approval host")); +} diff --git a/rust/crates/sift_mcp/src/tool/runs/mod.rs b/rust/crates/sift_mcp/src/tool/runs/mod.rs index 123f22342..d59b56612 100644 --- a/rust/crates/sift_mcp/src/tool/runs/mod.rs +++ b/rust/crates/sift_mcp/src/tool/runs/mod.rs @@ -11,14 +11,18 @@ use sift_rs::metadata::v1::MetadataValue; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, - tool::common::{ListParams, MetadataEntry, list_body, url_clause, with_urls}, + tool::common::{ + ListParams, MetadataEntry, + bulk::{self, BulkItems, Op}, + list_body, url_clause, with_urls, + }, }; #[cfg(test)] mod test; #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateRunParams { +pub struct UpdateRunItem { run_id: String, name: Option, description: Option, @@ -30,6 +34,30 @@ pub struct UpdateRunParams { metadata: Option>, } +fn validate_update_run(item: &UpdateRunItem) -> Result<(), ErrorData> { + if item.run_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`run_id` must not be empty", + None, + )); + } + if !(item.name.is_some() + || item.description.is_some() + || item.start_time_unix_nanos.is_some() + || item.stop_time_unix_nanos.is_some() + || item.is_pinned.is_some() + || item.client_key.is_some() + || item.tags.is_some() + || item.metadata.is_some()) + { + return Err(ErrorData::invalid_params( + "at least one updatable field besides `run_id` must be provided", + None, + )); + } + Ok(()) +} + #[tool_router(router = runs_router, vis = "pub(crate)")] impl SiftMcpServer { #[tool( @@ -119,15 +147,21 @@ impl SiftMcpServer { #[tool( name = "update_run", description = " - Update specific fields of an existing run, identified by `run_id`. This is a WRITE. Only the fields you + Update specific fields of one or more runs, each identified by `run_id`. This is a WRITE. Only the fields you set are changed; everything else on the run is preserved. Wraps `runs/v2 UpdateRun`. Output: + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. Each success holds `{ index, result }`, each failure holds + `{ index, error, outcome_unknown }`, and any failure sets `isError`. Each result is: - `{ \"run\": Run, \"run_url\": string|null, \"next_step\": string }`. The returned `Run` is the post-update state from the server; `run_url` is its Sift web link (`/run/`), or null when the host can't be derived. Parameters: + - `items`: required array of 1–100 objects, one per run. Each item takes the fields below. + - `selected_indices`: zero-based item positions supplied by the approval host; omit when + proposing. Omitted selects all outside chat; chat requires a mask. `[]` updates nothing. - `run_id`: required; the id of the run to update. - `name`: optional new name. - `description`: optional new description. @@ -144,11 +178,16 @@ impl SiftMcpServer { - `INVALID_PARAMS` if `run_id` is empty or no updatable field is provided. - `RESOURCE_NOT_FOUND` if no run matches `run_id`. - `INTERNAL_ERROR` for upstream gRPC failures (e.g. re-setting an already-set `client_key`). + - `INVALID_PARAMS` before any write for an empty or oversized `items`, duplicate targets, + unknown item fields, or invalid items; the error names the item index. + - Item-local errors fail only that item. Backend-wide errors stop the remaining items. Guidance: - This is a write with REPLACE semantics for `tags`/`metadata`. CONFIRM the full proposed values with the user before invoking; for appends, read the run via `list_runs` and send the union. - Note: `start_time` may be overwritten automatically if data is later ingested for this run. + - Items run sequentially; the call is not atomic. Never replay the whole call after a + partial failure. Check `outcome_unknown` items before retrying, and never retry `not_selected` items. ", annotations( title = "runs/update_run", @@ -157,10 +196,35 @@ impl SiftMcpServer { idempotent_hint = true, ) )] - pub async fn update_run(&self, params: Parameters) -> error::McpResult { + pub async fn update_run( + &self, + params: Parameters>, + ) -> error::McpResult { self.require_destructive()?; + let Parameters(BulkItems { + items, + selected_indices, + .. + }) = params; + let items = bulk::parse_items::(items)?; + for (index, item) in items.iter().enumerate() { + validate_update_run(item).map_err(|error| bulk::at_item(index, error))?; + } + bulk::reject_duplicate_targets(&items, "run", |item| Some(item.run_id.clone()))?; + bulk::run( + Op::Update, + items, + self.selection(selected_indices), + false, + |item| self.update_run_item(Parameters(item)), + ) + .await + } - let Parameters(UpdateRunParams { + async fn update_run_item(&self, params: Parameters) -> error::McpResult { + self.require_destructive()?; + validate_update_run(¶ms.0)?; + let Parameters(UpdateRunItem { run_id, name, description, @@ -172,28 +236,6 @@ impl SiftMcpServer { metadata, }) = params; - if run_id.is_empty() { - return Err(ErrorData::invalid_params( - "`run_id` must not be empty", - None, - )); - } - - let has_update = name.is_some() - || description.is_some() - || start_time_unix_nanos.is_some() - || stop_time_unix_nanos.is_some() - || is_pinned.is_some() - || client_key.is_some() - || tags.is_some() - || metadata.is_some(); - if !has_update { - return Err(ErrorData::invalid_params( - "at least one updatable field besides `run_id` must be provided", - None, - )); - } - let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); let run = self @@ -214,8 +256,7 @@ impl SiftMcpServer { let run_url = self.url_service.build_run_url(&run.run_id).ok(); let next_step = format!( - "Updated run `{}` ({}).{} Surface the new state to the user and confirm the change matches their \ - intent. Remember: tags and metadata are REPLACE operations.", + "Updated run `{}` ({}).{}", run.name, run.run_id, url_clause("run", Some(&run.name), run_url.as_deref()), diff --git a/rust/crates/sift_mcp/src/tool/runs/test.rs b/rust/crates/sift_mcp/src/tool/runs/test.rs index cc33564d8..69e51ca78 100644 --- a/rust/crates/sift_mcp/src/tool/runs/test.rs +++ b/rust/crates/sift_mcp/src/tool/runs/test.rs @@ -1,3 +1,6 @@ +#[path = "bulk_test.rs"] +mod bulk; + use rmcp::{handler::server::wrapper::Parameters, model::ErrorCode}; use sift_rs::runs::v2::{ ListRunsResponse, Run, UpdateRunResponse, run_service_server::RunServiceServer, @@ -6,7 +9,7 @@ use sift_test_util::{grpc::memory_sift_channel, mock::runs::v2::MockRunServiceIm use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; -use super::UpdateRunParams; +use super::UpdateRunItem as UpdateRunParams; use crate::{ server::SiftMcpServer, tool::common::test_support::{ @@ -216,7 +219,7 @@ async fn update_run_happy_path_surfaces_url() { params.name = Some("renamed".into()); let resp = server - .update_run(Parameters(params)) + .update_run_item(Parameters(params)) .await .expect("update_run failed"); @@ -231,7 +234,7 @@ async fn update_run_rejects_empty_id() { let (server, _h) = server_with_mock(MockRunServiceImpl::new()).await; let err = server - .update_run(Parameters(update_run_params(""))) + .update_run_item(Parameters(update_run_params(""))) .await .expect_err("expected error"); @@ -243,7 +246,7 @@ async fn update_run_rejects_no_fields() { let (server, _h) = server_with_mock(MockRunServiceImpl::new()).await; let err = server - .update_run(Parameters(update_run_params("r1"))) + .update_run_item(Parameters(update_run_params("r1"))) .await .expect_err("expected error"); From 8671473ac26d260c7d0e45c179239b5e0924095e Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:23:49 -0800 Subject: [PATCH 09/19] rust(fix): import the bulk core at the top of the report tools --- rust/crates/sift_mcp/src/tool/reports/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rust/crates/sift_mcp/src/tool/reports/mod.rs b/rust/crates/sift_mcp/src/tool/reports/mod.rs index 14f9c4e79..c450b793b 100644 --- a/rust/crates/sift_mcp/src/tool/reports/mod.rs +++ b/rust/crates/sift_mcp/src/tool/reports/mod.rs @@ -16,7 +16,11 @@ use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, service::reports::{ReportSource, RuleIdentifier}, - tool::common::{MetadataEntry, bulk_create, read_result, url_clause}, + tool::common::{ + MetadataEntry, + bulk::{self, BulkItems, Op}, + bulk_create, read_result, url_clause, + }, }; #[cfg(test)] @@ -480,9 +484,8 @@ impl SiftMcpServer { )] pub async fn update_report( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { - use crate::tool::common::bulk::{self, Op}; self.require_destructive()?; let Parameters(params) = params; let items = bulk::parse_items::(params.items)?; From 48acacf439f4633e57600bcccde92a1c4da9e280 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:30:50 -0800 Subject: [PATCH 10/19] rust(fix): fail only the item on a FailedPrecondition in bulk writes --- rust/crates/sift_mcp/src/error/mod.rs | 8 +++- .../sift_mcp/src/tool/artifacts/bulk_test.rs | 1 + rust/crates/sift_mcp/src/tool/common/bulk.rs | 42 ++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/rust/crates/sift_mcp/src/error/mod.rs b/rust/crates/sift_mcp/src/error/mod.rs index fd784e564..1969e07d9 100644 --- a/rust/crates/sift_mcp/src/error/mod.rs +++ b/rust/crates/sift_mcp/src/error/mod.rs @@ -118,15 +118,21 @@ pub fn from_anyhow(error: anyhow::Error) -> rmcp::ErrorData { let message = format!("{error:?}"); let mut code = ErrorCode::INTERNAL_ERROR; + let mut data = None; if let Ok(grpc_status) = error.downcast::() { code = from_grpc_code(grpc_status.code()); + // The resource's state rejected the write, so nothing was written and the + // backend is healthy. Bulk writes use this to fail only the one item. + if grpc_status.code() == Code::FailedPrecondition { + data = Some(serde_json::json!({"reason": "FailedPrecondition"})); + } } rmcp::ErrorData { code, message: message.into(), - data: None, + data, } } diff --git a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs index 92c28fbdc..4b2ddee50 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs @@ -122,6 +122,7 @@ async fn resource_errors_continue_and_backend_errors_stop() { for code in [ tonic::Code::NotFound, tonic::Code::PermissionDenied, + tonic::Code::FailedPrecondition, tonic::Code::Unavailable, ] { let mut mock = MockArtifactServiceImpl::new(); diff --git a/rust/crates/sift_mcp/src/tool/common/bulk.rs b/rust/crates/sift_mcp/src/tool/common/bulk.rs index f37116d9c..fa7fc7162 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -176,7 +176,7 @@ pub(crate) fn should_stop(op: Op, error: &ErrorData) -> bool { } match reason(error) { Some("AlreadyExists" | "Conflict") => false, - Some("PermissionDenied") => op == Op::Create, + Some("PermissionDenied" | "FailedPrecondition") => op == Op::Create, _ => true, } } @@ -250,7 +250,12 @@ where && error.code != ErrorCode::RESOURCE_NOT_FOUND && !matches!( reason(&error), - Some("AlreadyExists" | "PermissionDenied" | "Unauthenticated") + Some( + "AlreadyExists" + | "PermissionDenied" + | "Unauthenticated" + | "FailedPrecondition" + ) )); json!({"index": indices[index], "error": error, "outcome_unknown": outcome_unknown}) }) @@ -370,6 +375,39 @@ mod test { } } + #[tokio::test] + async fn a_failed_precondition_fails_one_update_without_stopping_the_rest() { + let precondition = signal(tonic::Code::FailedPrecondition); + assert_eq!( + precondition.data.as_ref().unwrap()["reason"], + "FailedPrecondition" + ); + assert!(should_stop(Op::Create, &precondition)); + let result = run( + Op::Update, + vec![0, 1, 2], + Selection { + indices: Some(vec![0, 1, 2]), + required: true, + }, + false, + |item| async move { + if item == 0 { + Err(signal(tonic::Code::FailedPrecondition)) + } else { + Ok(CallToolResult::structured(json!({"item": item}))) + } + }, + ) + .await + .unwrap(); + let body = result.structured_content.unwrap(); + assert_eq!(body["updated_count"], 2); + assert_eq!(body["failures"][0]["index"], 0); + assert_eq!(body["failures"][0]["outcome_unknown"], false); + assert_eq!(body["not_attempted"], json!([])); + } + #[tokio::test] async fn each_op_reports_its_own_count_and_verb() { for (op, key, verb) in [ From 896ddae89dbd6ab5951f71accc151a9b7d201293 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 10:57:57 -0800 Subject: [PATCH 11/19] rust(refactor): share bulk selection and result building --- rust/crates/sift_mcp/src/tool/common/bulk.rs | 168 ++++++++++++++----- 1 file changed, 127 insertions(+), 41 deletions(-) diff --git a/rust/crates/sift_mcp/src/tool/common/bulk.rs b/rust/crates/sift_mcp/src/tool/common/bulk.rs index fa7fc7162..8086b01da 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -181,18 +181,30 @@ pub(crate) fn should_stop(op: Op, error: &ErrorData) -> bool { } } -pub async fn run( - op: Op, - items: Vec, - selection: Selection, - partial_writes: bool, - operation: F, -) -> McpResult -where - F: FnMut(T) -> Fut, - Fut: Future, -{ - let requested_count = items.len(); +/// The approval mask applied to `count` input items. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Selected { + /// Input positions to write, in input order. + pub indices: Vec, + /// Input positions the approval host left out. + pub not_selected: Vec, +} + +impl Selected { + /// Keep only the selected items, in input order. + pub fn filter(&self, items: Vec) -> Vec { + let mut keep = self.indices.iter().peekable(); + items + .into_iter() + .enumerate() + .filter_map(|(index, item)| keep.next_if(|&&selected| selected == index).map(|_| item)) + .collect() + } +} + +/// Validate the approval mask for `count` input items. A required mask that is +/// missing fails closed before any write. +pub fn select(count: usize, selection: Selection) -> Result { let selected_indices = match selection.indices { Some(indices) => indices, None if selection.required => { @@ -203,11 +215,11 @@ where None, )); } - None => (0..requested_count).collect(), + None => (0..count).collect(), }; - let mut selected = vec![false; requested_count]; + let mut selected = vec![false; count]; for &index in &selected_indices { - if index >= requested_count || selected[index] { + if index >= count || selected[index] { return Err(ErrorData::invalid_params( "`selected_indices` must contain unique, in-range input indices", None, @@ -215,32 +227,62 @@ where } selected[index] = true; } - let indices = (0..requested_count) - .filter(|&index| selected[index]) - .collect::>(); - let not_selected = (0..requested_count) - .filter(|&index| !selected[index]) - .collect::>(); - let items = items - .into_iter() - .enumerate() - .filter_map(|(index, item)| selected[index].then_some(item)) - .collect(); + Ok(Selected { + indices: (0..count).filter(|&index| selected[index]).collect(), + not_selected: (0..count).filter(|&index| !selected[index]).collect(), + }) +} + +pub async fn run( + op: Op, + items: Vec, + selection: Selection, + partial_writes: bool, + operation: F, +) -> McpResult +where + F: FnMut(T) -> Fut, + Fut: Future, +{ + let requested_count = items.len(); + let selected = select(requested_count, selection)?; + let items = selected.filter(items); let outcome = batch::execute(items, operation, |error| should_stop(op, error)).await; - let not_attempted = outcome - .not_attempted - .into_iter() - .map(|index| indices[index]) - .collect::>(); - let successes = outcome - .successes + let original = |index: usize| selected.indices[index]; + report( + op, + requested_count, + &selected, + outcome + .successes + .into_iter() + .map(|(index, result)| (original(index), result)) + .collect(), + outcome + .failures + .into_iter() + .map(|(index, error)| (original(index), error)) + .collect(), + outcome.not_attempted.into_iter().map(original).collect(), + partial_writes, + ) +} + +/// Build the bulk result body. Every index is a position in the caller's input. +pub fn report( + op: Op, + requested_count: usize, + selected: &Selected, + successes: Vec<(usize, CallToolResult)>, + failures: Vec<(usize, ErrorData)>, + not_attempted: Vec, + partial_writes: bool, +) -> McpResult { + let successes = successes .into_iter() - .map( - |(index, result)| json!({"index": indices[index], "result": result.structured_content}), - ) + .map(|(index, result)| json!({"index": index, "result": result.structured_content})) .collect::>(); - let failures = outcome - .failures + let failures = failures .into_iter() .map(|(index, error)| { // An interrupted write or a composite task can commit before returning @@ -257,7 +299,7 @@ where | "FailedPrecondition" ) )); - json!({"index": indices[index], "error": error, "outcome_unknown": outcome_unknown}) + json!({"index": index, "error": error, "outcome_unknown": outcome_unknown}) }) .collect::>(); let has_errors = !failures.is_empty() || !not_attempted.is_empty(); @@ -276,8 +318,8 @@ where ); let mut body = Map::new(); body.insert("requested_count".into(), json!(requested_count)); - body.insert("selected_count".into(), json!(indices.len())); - body.insert("not_selected".into(), json!(not_selected)); + body.insert("selected_count".into(), json!(selected.indices.len())); + body.insert("not_selected".into(), json!(selected.not_selected)); body.insert(op.count_key().into(), json!(successes.len())); body.insert("successes".into(), json!(successes)); body.insert("failures".into(), json!(failures)); @@ -408,6 +450,50 @@ mod test { assert_eq!(body["not_attempted"], json!([])); } + #[test] + fn select_keeps_input_positions_and_filters_in_order() { + let selected = select( + 4, + Selection { + indices: Some(vec![3, 1]), + required: true, + }, + ) + .unwrap(); + assert_eq!(selected.indices, vec![1, 3]); + assert_eq!(selected.not_selected, vec![0, 2]); + assert_eq!(selected.filter(vec!["a", "b", "c", "d"]), vec!["b", "d"]); + for indices in [Some(vec![4]), Some(vec![1, 1])] { + assert!( + select( + 4, + Selection { + indices, + required: false + } + ) + .is_err() + ); + } + assert!( + select( + 2, + Selection { + indices: None, + required: true + } + ) + .is_err() + ); + assert_eq!( + select(2, all()).unwrap(), + Selected { + indices: vec![0, 1], + not_selected: vec![], + } + ); + } + #[tokio::test] async fn each_op_reports_its_own_count_and_verb() { for (op, key, verb) in [ From 61e49331783cffcb852a38d7d1ba3bd158b8ba65 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 11:06:55 -0800 Subject: [PATCH 12/19] rust(feat): approve update_annotation ids per item --- .../sift_mcp/src/service/annotations/mod.rs | 234 ++++------ .../sift_mcp/src/service/annotations/test.rs | 203 ++++++-- .../sift_mcp/src/tool/annotations/mod.rs | 195 ++++---- .../sift_mcp/src/tool/annotations/test.rs | 437 ++++++++++++------ 4 files changed, 628 insertions(+), 441 deletions(-) diff --git a/rust/crates/sift_mcp/src/service/annotations/mod.rs b/rust/crates/sift_mcp/src/service/annotations/mod.rs index 7732b26ec..28b5a72f2 100644 --- a/rust/crates/sift_mcp/src/service/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/service/annotations/mod.rs @@ -1,19 +1,22 @@ use crate::policy::{RetryPolicy, once, with_retry}; -use crate::service::common; -use anyhow::{Context, Error, Result, anyhow}; +use crate::{ + error::from_anyhow, + service::common, + tool::common::bulk::{self, Op}, +}; +use anyhow::{Context, Result, anyhow}; use futures::{StreamExt, stream}; use pbjson_types::{FieldMask, Timestamp}; use sift_rs::{ SiftChannel, annotations::v1::{ Annotation, AnnotationLinkedChannel, AnnotationState, AnnotationType, - BatchArchiveAnnotationsRequest, CreateAnnotationRequest, ListAnnotationsRequest, - ListAnnotationsResponse, UpdateAnnotationRequest, annotation_linked_channel, - annotation_service_client::AnnotationServiceClient, + ArchiveAnnotationRequest, CreateAnnotationRequest, ListAnnotationsRequest, + ListAnnotationsResponse, UnarchiveAnnotationRequest, UpdateAnnotationRequest, + annotation_linked_channel, annotation_service_client::AnnotationServiceClient, }, metadata::v1::MetadataValue, }; -use tonic::Code; #[cfg(test)] mod test; @@ -23,7 +26,7 @@ const MAX_CONCURRENT_ANNOTATION_UPDATES: usize = 50; #[derive(Debug)] pub struct AnnotationUpdateFailure { pub annotation_id: String, - pub error: Error, + pub error: rmcp::ErrorData, } #[derive(Debug)] @@ -31,8 +34,6 @@ pub struct UpdateAnnotationsResult { pub annotations: Vec, pub failures: Vec, pub not_attempted: Vec, - pub batch_archive_error: Option, - pub archive_skipped: bool, } async fn fan_out_bounded(items: Vec, concurrency: usize, mut op: F) -> Vec @@ -52,21 +53,6 @@ where results.into_iter().map(|(_, result)| result).collect() } -fn is_backend_wide_failure(error: &Error) -> bool { - error.downcast_ref::().is_some_and(|status| { - matches!( - status.code(), - Code::ResourceExhausted - | Code::Unavailable - | Code::DeadlineExceeded - | Code::Internal - | Code::Unauthenticated - | Code::PermissionDenied - | Code::Cancelled - ) - }) -} - /// Build a protobuf `Timestamp` from Unix nanoseconds via the shared helper. fn timestamp_from_unix_nanos(nanos: i64) -> Timestamp { let (seconds, nanos) = common::unix_nanos_to_secs_and_subsec_nanos(nanos); @@ -223,26 +209,23 @@ impl AnnotationService { .ok_or_else(|| anyhow!("create_annotation response missing annotation")) } - pub async fn batch_archive_annotations( - &self, - annotation_ids: Vec, - ) -> Result> { + pub async fn archive_annotation(&self, annotation_id: String) -> Result { let channel = self.channel.clone(); let resp = with_retry(&self.policy, move || { let channel = channel.clone(); - let annotation_ids = annotation_ids.clone(); + let annotation_id = annotation_id.clone(); async move { let mut client = AnnotationServiceClient::new(channel); client - .batch_archive_annotations(BatchArchiveAnnotationsRequest { annotation_ids }) + .archive_annotation(ArchiveAnnotationRequest { annotation_id }) .await .map(|resp| resp.into_inner()) } }) .await - .context("failed to batch archive annotations")?; - - Ok(resp.annotations) + .context("failed to archive annotation")?; + resp.annotation + .ok_or_else(|| anyhow!("archive_annotation response missing annotation")) } #[allow(clippy::too_many_arguments)] @@ -269,136 +252,85 @@ impl AnnotationService { || tags.is_some() || linked_channel_ids.is_some() || metadata.is_some(); - let mut annotations = Vec::new(); + let mut annotations = Vec::with_capacity(annotation_ids.len()); let mut failures = Vec::new(); let mut not_attempted = Vec::new(); - if has_field_update { - annotations.reserve(annotation_ids.len()); - for chunk_start in (0..annotation_ids.len()).step_by(MAX_CONCURRENT_ANNOTATION_UPDATES) - { - let chunk_end = - (chunk_start + MAX_CONCURRENT_ANNOTATION_UPDATES).min(annotation_ids.len()); - let service = self.clone(); - let name = name.clone(); - let description = description.clone(); - let assigned_to_user_id = assigned_to_user_id.clone(); - let tags = tags.clone(); - let linked_channel_ids = linked_channel_ids.clone(); - let metadata = metadata.clone(); - let updates = fan_out_bounded( - annotation_ids[chunk_start..chunk_end].to_vec(), - MAX_CONCURRENT_ANNOTATION_UPDATES, - move |annotation_id| { - let service = service.clone(); - let name = name.clone(); - let description = description.clone(); - let assigned_to_user_id = assigned_to_user_id.clone(); - let tags = tags.clone(); - let linked_channel_ids = linked_channel_ids.clone(); - let metadata = metadata.clone(); - async move { - let result = service - .update_annotation( - annotation_id.clone(), - name, - description, - start_time_unix_nanos, - end_time_unix_nanos, - assigned_to_user_id, - state, - tags, - linked_channel_ids, - metadata, + for chunk in annotation_ids.chunks(MAX_CONCURRENT_ANNOTATION_UPDATES) { + let updates = fan_out_bounded( + chunk.to_vec(), + MAX_CONCURRENT_ANNOTATION_UPDATES, + |annotation_id| { + let service = self.clone(); + let name = name.clone(); + let description = description.clone(); + let assigned_to_user_id = assigned_to_user_id.clone(); + let tags = tags.clone(); + let linked_channel_ids = linked_channel_ids.clone(); + let metadata = metadata.clone(); + async move { + let result = async { + let annotation = if has_field_update { + Some( + service + .update_annotation( + annotation_id.clone(), + name, + description, + start_time_unix_nanos, + end_time_unix_nanos, + assigned_to_user_id, + state, + tags, + linked_channel_ids, + metadata, + ) + .await?, ) - .await; - (annotation_id, result) + } else { + None + }; + match is_archived { + Some(true) => { + service.archive_annotation(annotation_id.clone()).await + } + Some(false) => { + service.unarchive_annotation(annotation_id.clone()).await + } + None => annotation.context("no annotation changes requested"), + } } - }, - ) - .await; - let stop_after_batch = updates - .iter() - .any(|(_, result)| result.as_ref().is_err_and(is_backend_wide_failure)); - - for (annotation_id, result) in updates { - match result { - Ok(annotation) => annotations.push(annotation), - Err(error) => failures.push(AnnotationUpdateFailure { - annotation_id, - error, - }), + .await; + (annotation_id, result) } - } - - if stop_after_batch { - not_attempted.extend_from_slice(&annotation_ids[chunk_end..]); - break; - } - } - } - - let mut batch_archive_error = None; - let mut archive_skipped = false; - if is_archived == Some(true) { - if failures.is_empty() && not_attempted.is_empty() { - let result = self.batch_archive_annotations(annotation_ids).await; + }, + ) + .await; + let mut stop = false; + for (annotation_id, result) in updates { match result { - Ok(archived) => annotations = archived, - Err(error) => batch_archive_error = Some(error), - } - } else { - archive_skipped = true; - } - } else if is_archived == Some(false) && failures.is_empty() && not_attempted.is_empty() { - annotations.clear(); - annotations.reserve(annotation_ids.len()); - for chunk_start in (0..annotation_ids.len()).step_by(MAX_CONCURRENT_ANNOTATION_UPDATES) - { - let chunk_end = - (chunk_start + MAX_CONCURRENT_ANNOTATION_UPDATES).min(annotation_ids.len()); - let service = self.clone(); - let unarchives = fan_out_bounded( - annotation_ids[chunk_start..chunk_end].to_vec(), - MAX_CONCURRENT_ANNOTATION_UPDATES, - move |annotation_id| { - let service = service.clone(); - async move { - let result = service.unarchive_annotation(annotation_id.clone()).await; - (annotation_id, result) - } - }, - ) - .await; - let stop_after_batch = unarchives - .iter() - .any(|(_, result)| result.as_ref().is_err_and(is_backend_wide_failure)); - - for (annotation_id, result) in unarchives { - match result { - Ok(annotation) => annotations.push(annotation), - Err(error) => failures.push(AnnotationUpdateFailure { + Ok(annotation) => annotations.push(annotation), + Err(error) => { + let error = from_anyhow(error); + stop |= bulk::should_stop(Op::Update, &error); + failures.push(AnnotationUpdateFailure { annotation_id, error, - }), + }); } } - - if stop_after_batch { - not_attempted.extend_from_slice(&annotation_ids[chunk_end..]); - break; - } } - } else if is_archived == Some(false) { - archive_skipped = true; + if stop { + let attempted = annotations.len() + failures.len(); + not_attempted.extend_from_slice(&annotation_ids[attempted..]); + break; + } } Ok(UpdateAnnotationsResult { annotations, failures, not_attempted, - batch_archive_error, - archive_skipped, }) } @@ -489,7 +421,6 @@ impl AnnotationService { .ok_or_else(|| anyhow!("update_annotation response missing annotation")) } - #[allow(deprecated)] // The backend requires this deprecated field to unarchive annotations. pub async fn unarchive_annotation(&self, annotation_id: String) -> Result { let channel = self.channel.clone(); let resp = with_retry(&self.policy, move || { @@ -498,16 +429,7 @@ impl AnnotationService { async move { let mut client = AnnotationServiceClient::new(channel); client - .update_annotation(UpdateAnnotationRequest { - annotation: Some(Annotation { - annotation_id, - deleted_date: None, - ..Default::default() - }), - update_mask: Some(FieldMask { - paths: vec!["deleted_date".into()], - }), - }) + .unarchive_annotation(UnarchiveAnnotationRequest { annotation_id }) .await .map(|resp| resp.into_inner()) } diff --git a/rust/crates/sift_mcp/src/service/annotations/test.rs b/rust/crates/sift_mcp/src/service/annotations/test.rs index 743d3b250..2f7668133 100644 --- a/rust/crates/sift_mcp/src/service/annotations/test.rs +++ b/rust/crates/sift_mcp/src/service/annotations/test.rs @@ -7,9 +7,9 @@ use std::{ }; use sift_rs::annotations::v1::{ - Annotation, AnnotationState, AnnotationType, BatchArchiveAnnotationsResponse, - CreateAnnotationResponse, ListAnnotationsResponse, UpdateAnnotationResponse, - annotation_service_server::AnnotationServiceServer, + Annotation, AnnotationState, AnnotationType, ArchiveAnnotationResponse, + CreateAnnotationResponse, ListAnnotationsResponse, UnarchiveAnnotationResponse, + UpdateAnnotationResponse, annotation_service_server::AnnotationServiceServer, }; use sift_test_util::{grpc::memory_sift_channel, mock::annotations::v1::MockAnnotationServiceImpl}; use tokio::{sync::Semaphore, task::JoinHandle}; @@ -326,55 +326,192 @@ async fn create_annotation_propagates_grpc_error() { } #[tokio::test] -async fn batch_archive_annotations_forwards_ids() { +async fn archive_annotation_forwards_id() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_batch_archive_annotations() - .withf(|req| req.get_ref().annotation_ids == ["ann1", "ann2"]) + mock.expect_archive_annotation() + .times(1) + .withf(|req| req.get_ref().annotation_id == "ann1") .returning(|req| { - let annotations = req - .into_inner() - .annotation_ids - .into_iter() - .map(|annotation_id| Annotation { - annotation_id, + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: req.into_inner().annotation_id, is_archived: true, ..Default::default() - }) - .collect(); - Ok(Response::new(BatchArchiveAnnotationsResponse { - annotations, + }), })) }); - let (service, _h) = service_with_mock(mock).await; - - let annotations = service - .batch_archive_annotations(vec!["ann1".into(), "ann2".into()]) + let annotation = service + .archive_annotation("ann1".into()) .await - .expect("batch archive failed"); - - assert_eq!(annotations.len(), 2); - assert!(annotations.iter().all(|annotation| annotation.is_archived)); + .expect("archive failed"); + assert_eq!(annotation.annotation_id, "ann1"); + assert!(annotation.is_archived); } #[tokio::test] -async fn batch_archive_annotations_propagates_grpc_error() { +async fn archive_annotation_propagates_grpc_error() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_batch_archive_annotations() + mock.expect_archive_annotation() .returning(|_| Err(Status::not_found("no such annotation"))); let (service, _h) = service_with_mock(mock).await; let error = service - .batch_archive_annotations(vec!["ann1".into()]) + .archive_annotation("ann1".into()) .await - .expect_err("expected batch archive error"); + .expect_err("expected archive error"); + + assert!(error.to_string().contains("failed to archive annotation")); +} + +#[tokio::test] +async fn update_annotations_only_archives_ids_whose_field_update_succeeded() { + for archived in [true, false] { + let mut mock = MockAnnotationServiceImpl::new(); + let updated = Arc::new(std::sync::Mutex::new(std::collections::HashSet::new())); + let updates = Arc::clone(&updated); + mock.expect_update_annotation() + .times(3) + .returning(move |req| { + let annotation = req.into_inner().annotation.unwrap(); + if annotation.annotation_id == "ann2" { + return Err(Status::not_found("missing")); + } + updates + .lock() + .unwrap() + .insert(annotation.annotation_id.clone()); + Ok(Response::new(UpdateAnnotationResponse { + annotation: Some(annotation), + })) + }); + if archived { + mock.expect_archive_annotation() + .times(2) + .returning(move |req| { + let id = req.into_inner().annotation_id; + assert!(updated.lock().unwrap().contains(&id)); + assert_ne!(id, "ann2"); + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: id, + is_archived: true, + ..Default::default() + }), + })) + }); + } else { + mock.expect_unarchive_annotation() + .times(2) + .returning(move |req| { + let id = req.into_inner().annotation_id; + assert!(updated.lock().unwrap().contains(&id)); + assert_ne!(id, "ann2"); + Ok(Response::new(UnarchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: id, + ..Default::default() + }), + })) + }); + } + let (service, _h) = service_with_mock(mock).await; + let outcome = service + .update_annotations( + vec!["ann1".into(), "ann2".into(), "ann3".into()], + Some("renamed".into()), + None, + None, + None, + None, + None, + None, + None, + None, + Some(archived), + ) + .await + .unwrap(); + assert_eq!(outcome.annotations.len(), 2); + assert_eq!(outcome.failures.len(), 1); + assert_eq!(outcome.failures[0].annotation_id, "ann2"); + } +} - assert!( - error - .to_string() - .contains("failed to batch archive annotations") - ); +#[tokio::test] +async fn archive_item_errors_do_not_stop_later_groups() { + for code in [ + tonic::Code::NotFound, + tonic::Code::PermissionDenied, + tonic::Code::FailedPrecondition, + ] { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_archive_annotation() + .times(51) + .returning(move |req| { + let annotation_id = req.into_inner().annotation_id; + if annotation_id == "ann0" { + return Err(Status::new(code, "item failed")); + } + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id, + is_archived: true, + ..Default::default() + }), + })) + }); + let (service, _h) = service_with_mock(mock).await; + let outcome = service + .update_annotations( + (0..51).map(|i| format!("ann{i}")).collect(), + None, + None, + None, + None, + None, + None, + None, + None, + None, + Some(true), + ) + .await + .unwrap(); + assert_eq!(outcome.annotations.len(), 50); + assert_eq!(outcome.failures.len(), 1); + assert!(outcome.not_attempted.is_empty()); + assert_eq!(outcome.annotations.last().unwrap().annotation_id, "ann50"); + } +} + +#[tokio::test] +async fn archive_backend_failure_stops_ids_not_yet_started() { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_archive_annotation() + .times(50) + .returning(|_| Err(Status::unavailable("offline"))); + let (service, _h) = service_with_mock(mock).await; + let ids = (0..60).map(|i| format!("ann{i}")).collect::>(); + let outcome = service + .update_annotations( + ids.clone(), + None, + None, + None, + None, + None, + None, + None, + None, + None, + Some(true), + ) + .await + .unwrap(); + assert_eq!(outcome.failures.len(), 50); + assert_eq!(outcome.not_attempted, ids[50..]); } #[tokio::test] diff --git a/rust/crates/sift_mcp/src/tool/annotations/mod.rs b/rust/crates/sift_mcp/src/tool/annotations/mod.rs index 2e3c566d3..e2512b4c1 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/mod.rs @@ -11,7 +11,10 @@ use sift_rs::{ metadata::v1::MetadataValue, }; -use crate::tool::common::bulk_create; +use crate::tool::common::{ + bulk::{self, Op}, + bulk_create, +}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, @@ -23,14 +26,6 @@ mod test; const MAX_UPDATE_ANNOTATIONS: usize = 1_000; -fn upstream_error_message(error: &anyhow::Error) -> String { - if let Some(status) = error.downcast_ref::() { - format!("{}: {}", status.code(), status.message()) - } else { - error.to_string() - } -} - #[derive(Debug, Deserialize, JsonSchema)] pub struct AnnotationListParams { pub(crate) filter: String, @@ -123,8 +118,10 @@ pub struct CreateAnnotationParams { } #[derive(Debug, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] pub struct UpdateAnnotationParams { annotation_ids: Vec, + selected_indices: Option>, name: Option, description: Option, start_time_unix_nanos: Option, @@ -444,22 +441,23 @@ impl SiftMcpServer { #[tool( name = "update_annotation", description = " - Update one or more existing annotations. Uses `annotations/v1 BatchArchiveAnnotations` when archiving - and one `UpdateAnnotation` request per annotation when unarchiving. + Update one or more existing annotations with shared changes and per-id approval. + Each selected id receives a field update when requested, then an archive or unarchive when requested. + These use the per-id UpdateAnnotation, ArchiveAnnotation and UnarchiveAnnotation RPCs. Output: - - `{ \"annotations\": [Annotation, ...], \"failures\": [...], \"batch_archive_error\": object|null, - \"not_attempted\": [string, ...], \"archive_skipped\": bool, \"next_step\": string }`. Successful - annotations include a `url` field when the host can be derived. Each individual failure includes - `annotation_id` and an upstream `message`. `batch_archive_error` includes `annotation_ids` and an - upstream `message` only for a failed `BatchArchiveAnnotations` request. Partial failures set the tool result's - `isError` flag. `archive_skipped` is true when the requested archive-state change was not attempted - because field updates failed. + - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, + not_attempted, next_step }`. Successes hold `{ index, result: { annotation } }`; the annotation + includes a `url` field when the host can be derived. Failures hold `{ index, error, outcome_unknown }`. + Every index is a zero-based position in `annotation_ids`. `not_attempted` lists positions not started + after a backend-wide error. Any failure sets `isError`. Parameters: - - `annotation_ids`: required list of 1 to 1000 annotation ids. The same changes are applied to every - annotation. This replaces the former `annotation_id` parameter; pass a single annotation as a - one-element list. + - `annotation_ids`: required list of 1–1000 unique, nonempty annotation ids, or 1–100 in chat. + Split chat work into calls of at most 100 ids. Pass one id to update one annotation. + The same changes apply to every selected annotation. + - `selected_indices`: zero-based positions in `annotation_ids` supplied by the approval host; + omit when proposing. Omitted selects all outside chat; chat requires a mask. `[]` writes nothing. - `name`: optional new name. - `description`: optional new description. - `start_time_unix_nanos` / `end_time_unix_nanos`: optional new time bounds in Unix nanoseconds. @@ -471,28 +469,27 @@ impl SiftMcpServer { Pass `[]` to clear. Bit-field and calculated-channel links are not exposed here. - `metadata`: optional; REPLACES the full metadata list. Each entry is `{ \"name\": \"\", \"value\": }`. Pass `[]` to clear. - - `is_archived`: optional archive state. `true` uses one batch-archive request; `false` uses one - per-annotation update request that clears the annotation's delete date. When combined with other - fields, annotations are updated before their archive state changes. + - `is_archived`: optional archive state. `true` archives each selected annotation; `false` + unarchives it. When combined with other fields, each id's fields are updated first. + If that field update fails, that id's archive-state change is skipped. At least one updatable field must be set; otherwise the tool returns `INVALID_PARAMS`. Errors: - - `INVALID_PARAMS` if `annotation_ids` is empty, contains an empty id, exceeds 1000 ids, `state` is - unrecognized, or no updatable field is set. - - Individual upstream failures, including unarchive failures, are returned in `failures` next to - successful results. Follow each failure's guidance and retry only eligible failed ids. - - Backend-wide failures stop later batches. Their ids are returned in `not_attempted` without an API - request. - - Batch archive failures are returned in `batch_archive_error`. The archive outcome may be unknown. + - `INVALID_PARAMS` before any write for empty, duplicate or invalid ids; more than 1000 ids + (100 in chat); an invalid selection mask; an unrecognized state; or no changes. + - `INVALID_REQUEST` without `--allow-destructive`. + - Item-local failures, including NotFound, PermissionDenied and FailedPrecondition, fail only that id. + - Backend-wide failures stop later groups; ids not started are reported in `not_attempted`. Guidance: - This is a write. CONFIRM every target and the full proposed values with the user before invoking — `tags`, `linked_channel_ids`, and `metadata` are REPLACE operations, not merges. - - General field updates and unarchive issue one API request per annotation and are not atomic. Requests - run in batches of up to 50. A backend-wide failure stops later batches. Archive uses one batch API - request. Requested archive-state changes begin only after all individual updates succeed; otherwise - `archive_skipped` is true. + - Up to 50 ids run concurrently, with each id's field update before its archive change. + The call is not atomic; successful writes stay committed even when other ids fail. Never replay + the whole call after a partial failure. Check `outcome_unknown` items before retrying, and never + retry `not_selected` ids. A failed archive can follow a committed field update; inspect that id + before retrying its shared changes. - For appends, read the current annotation via `list_annotations` filtered by `annotation_id == \"\"`, then send the union. ", @@ -511,6 +508,7 @@ impl SiftMcpServer { let Parameters(UpdateAnnotationParams { annotation_ids, + selected_indices, name, description, start_time_unix_nanos, @@ -523,6 +521,13 @@ impl SiftMcpServer { is_archived, }) = params; + let selection = self.selection(selected_indices); + if selection.required && annotation_ids.len() > 100 { + return Err(ErrorData::invalid_params( + "`annotation_ids` must contain at most 100 ids in chat; split the work into calls of at most 100 ids", + None, + )); + } if annotation_ids.is_empty() { return Err(ErrorData::invalid_params( "`annotation_ids` must contain at least one id", @@ -537,13 +542,15 @@ impl SiftMcpServer { )); } - if annotation_ids.iter().any(String::is_empty) { + if annotation_ids.iter().any(|id| id.trim().is_empty()) { return Err(ErrorData::invalid_params( "`annotation_ids` must not contain empty ids", None, )); } + bulk::reject_duplicate_targets(&annotation_ids, "annotation", |id| Some(id.clone()))?; + let has_field_update = name.is_some() || description.is_some() || start_time_unix_nanos.is_some() @@ -562,8 +569,15 @@ impl SiftMcpServer { let state = state.map(AnnotationState::from); let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); - let requested_ids = annotation_ids.clone(); let requested_count = annotation_ids.len(); + let positions = annotation_ids + .iter() + .cloned() + .enumerate() + .map(|(index, id)| (id, index)) + .collect::>(); + let selected = bulk::select(requested_count, selection)?; + let annotation_ids = selected.filter(annotation_ids); let outcome = self .annotation_service .update_annotations( @@ -582,87 +596,40 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let updated_count = outcome.annotations.len(); - let failures = outcome - .failures - .into_iter() - .map(|failure| { - serde_json::json!({ - "annotation_id": failure.annotation_id, - "message": upstream_error_message(&failure.error), - }) - }) - .collect::>(); - let failure_count = failures.len(); - let not_attempted = outcome.not_attempted; - let not_attempted_count = not_attempted.len(); - let batch_archive_error = outcome.batch_archive_error.map(|error| { - serde_json::json!({ - "annotation_ids": requested_ids, - "message": upstream_error_message(&error), - }) - }); - let has_errors = - failure_count > 0 || not_attempted_count > 0 || batch_archive_error.is_some(); - - let annotations = outcome.annotations; - let annotations = with_urls(&annotations, |annotation| { + let annotations = with_urls(&outcome.annotations, |annotation| { self.url_service .build_annotation_url(&annotation.annotation_id) .ok() })?; - let next_step = if batch_archive_error.is_some() { - format!( - "Completed field updates for {updated_count} of {requested_count} annotation(s), but batch archive \ - failed. Archive state may be unknown. Verify the targets with `list_annotations` before retrying.", - ) - } else if outcome.archive_skipped { - if not_attempted_count > 0 { - format!( - "Updated {updated_count} of {requested_count} annotation(s); {failure_count} failed and \ - {not_attempted_count} were not attempted after a backend-wide failure. The archive state change was not \ - attempted. Review `failures` before retrying eligible failed and not-attempted ids." + let successes = outcome + .annotations + .iter() + .zip(annotations) + .map(|(annotation, value)| { + ( + positions[&annotation.annotation_id], + CallToolResult::structured(serde_json::json!({"annotation": value})), ) - } else { - format!( - "Updated {updated_count} of {requested_count} annotation(s); {failure_count} failed. The archive state change was \ - not attempted because individual updates failed. Review `failures`, follow their guidance, and \ - retry only eligible failed ids." - ) - } - } else if not_attempted_count > 0 { - format!( - "Updated {updated_count} of {requested_count} annotation(s); {failure_count} failed and \ - {not_attempted_count} were not attempted after a backend-wide failure. Review `failures` before \ - retrying eligible failed and not-attempted ids." - ) - } else if failure_count > 0 { - format!( - "Updated {updated_count} of {requested_count} annotation(s); {failure_count} failed. Successful \ - annotations are already changed. Review `failures`, follow their guidance, and retry only eligible \ - failed ids." - ) - } else { - format!( - "Updated {updated_count} annotation(s). Surface the new states and links to the user and confirm the \ - changes match their intent. Remember: tags, linked channels, and metadata are REPLACE operations." - ) - }; - - let structured = serde_json::json!({ - "annotations": annotations, - "failures": failures, - "not_attempted": not_attempted, - "batch_archive_error": batch_archive_error, - "archive_skipped": outcome.archive_skipped, - "next_step": next_step, - }); - let mut result = if has_errors { - CallToolResult::structured_error(structured) - } else { - CallToolResult::structured(structured) - }; - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }) + .collect(); + let failures = outcome + .failures + .into_iter() + .map(|failure| (positions[&failure.annotation_id], failure.error)) + .collect(); + let not_attempted = outcome + .not_attempted + .into_iter() + .map(|id| positions[&id]) + .collect(); + bulk::report( + Op::Update, + requested_count, + &selected, + successes, + failures, + not_attempted, + false, + ) } } diff --git a/rust/crates/sift_mcp/src/tool/annotations/test.rs b/rust/crates/sift_mcp/src/tool/annotations/test.rs index 54b2128b3..5e1ab7f95 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/test.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/test.rs @@ -8,8 +8,9 @@ use rmcp::{ model::{ErrorCode, Tool}, }; use sift_rs::annotations::v1::{ - Annotation, BatchArchiveAnnotationsResponse, CreateAnnotationResponse, ListAnnotationsResponse, - UpdateAnnotationResponse, annotation_service_server::AnnotationServiceServer, + Annotation, ArchiveAnnotationResponse, CreateAnnotationResponse, ListAnnotationsResponse, + UnarchiveAnnotationResponse, UpdateAnnotationResponse, + annotation_service_server::AnnotationServiceServer, }; use sift_test_util::{grpc::memory_sift_channel, mock::annotations::v1::MockAnnotationServiceImpl}; use tokio::task::JoinHandle; @@ -168,6 +169,7 @@ fn create_params() -> CreateAnnotationItem { fn update_params(annotation_id: &str) -> UpdateAnnotationParams { UpdateAnnotationParams { annotation_ids: vec![annotation_id.into()], + selected_indices: None, name: None, description: None, start_time_unix_nanos: None, @@ -181,6 +183,151 @@ fn update_params(annotation_id: &str) -> UpdateAnnotationParams { } } +#[tokio::test] +async fn update_annotation_approves_two_of_three_ids() { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_update_annotation() + .times(2) + .withf(|req| req.get_ref().annotation.as_ref().unwrap().annotation_id != "ann2") + .returning(|req| { + Ok(Response::new(UpdateAnnotationResponse { + annotation: req.into_inner().annotation, + })) + }); + let (server, _h) = server_with_mock(mock).await; + let server = server.with_require_selection(true); + let mut params = update_params("ann1"); + params.annotation_ids.extend(["ann2".into(), "ann3".into()]); + params.selected_indices = Some(vec![0, 2]); + params.name = Some("new name".into()); + let body = structured(server.update_annotation(Parameters(params)).await.unwrap()); + assert_eq!(body["requested_count"], 3); + assert_eq!(body["selected_count"], 2); + assert_eq!(body["updated_count"], 2); + assert_eq!(body["not_selected"], serde_json::json!([1])); + assert_eq!(body["successes"][0]["index"], 0); + assert_eq!(body["successes"][1]["index"], 2); + assert_eq!( + body["successes"][1]["result"]["annotation"]["url"], + "https://app.test.local/annotation/ann3" + ); +} + +#[tokio::test] +async fn update_annotation_reports_original_positions_for_selected_failures_and_unstarted_ids() { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_update_annotation() + .times(50) + .returning(|_| Err(Status::unavailable("offline"))); + let (server, _h) = server_with_mock(mock).await; + let mut params = update_params("ann0"); + params + .annotation_ids + .extend((1..100).map(|i| format!("ann{i}"))); + params.selected_indices = Some((0..50).chain(std::iter::once(99)).collect()); + params.name = Some("new name".into()); + let body = structured( + server + .with_require_selection(true) + .update_annotation(Parameters(params)) + .await + .unwrap(), + ); + assert_eq!(body["selected_count"], 51); + assert_eq!(body["failures"][49]["index"], 49); + assert_eq!(body["not_attempted"], serde_json::json!([99])); + assert_eq!(body["not_selected"][0], 50); +} + +#[tokio::test] +async fn update_annotation_chat_requires_a_mask_and_at_most_100_ids() { + let (server, _h) = server_with_mock(MockAnnotationServiceImpl::new()).await; + let server = server.with_require_selection(true); + let mut params = update_params("ann1"); + params.name = Some("new name".into()); + let error = server + .update_annotation(Parameters(params)) + .await + .unwrap_err(); + assert!(error.message.contains("approval host")); + let mut params = update_params("ann0"); + params + .annotation_ids + .extend((1..101).map(|i| format!("ann{i}"))); + params.selected_indices = Some(vec![0]); + params.is_archived = Some(true); + let error = server + .update_annotation(Parameters(params)) + .await + .unwrap_err(); + assert_eq!(error.code, ErrorCode::INVALID_PARAMS); + assert!( + error + .message + .contains("split the work into calls of at most 100 ids") + ); +} + +#[tokio::test] +async fn update_annotation_accepts_100_ids_in_chat_and_1000_without_a_host() { + for (count, chat) in [(100, true), (1000, false)] { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_update_annotation() + .times(count) + .returning(|req| { + Ok(Response::new(UpdateAnnotationResponse { + annotation: req.into_inner().annotation, + })) + }); + let (server, _h) = server_with_mock(mock).await; + let server = server.with_require_selection(chat); + let mut params = update_params("ann0"); + params + .annotation_ids + .extend((1..count).map(|i| format!("ann{i}"))); + params.selected_indices = chat.then(|| (0..count).collect()); + params.name = Some("new name".into()); + let body = structured(server.update_annotation(Parameters(params)).await.unwrap()); + assert_eq!(body["updated_count"], count); + assert_eq!(body["successes"][count - 1]["index"], count - 1); + } +} + +#[tokio::test] +async fn update_annotation_rejects_duplicates_and_invalid_masks_before_any_rpc() { + let (server, _h) = server_with_mock(MockAnnotationServiceImpl::new()).await; + let mut duplicate = update_params("ann1"); + duplicate.annotation_ids.push("ann1".into()); + duplicate.is_archived = Some(true); + assert_eq!( + server + .update_annotation(Parameters(duplicate)) + .await + .unwrap_err() + .code, + ErrorCode::INVALID_PARAMS + ); + for mask in [vec![0, 0], vec![1]] { + let mut params = update_params("ann1"); + params.name = Some("new name".into()); + params.selected_indices = Some(mask); + assert_eq!( + server + .update_annotation(Parameters(params)) + .await + .unwrap_err() + .code, + ErrorCode::INVALID_PARAMS + ); + } + let mut params = update_params("ann1"); + params.name = Some("new name".into()); + params.selected_indices = Some(vec![]); + let body = structured(server.update_annotation(Parameters(params)).await.unwrap()); + assert_eq!(body["updated_count"], 0); + assert_eq!(body["not_selected"], serde_json::json!([0])); +} + #[test] fn update_annotation_params_accept_bulk_ids() { let params = serde_json::from_value::(serde_json::json!({ @@ -379,10 +526,10 @@ async fn update_annotation_happy_path() { .await .expect("update_annotation failed"); - let annotations = structured_field(resp, "annotations"); - assert_eq!(annotations[0]["name"], "renamed"); + let successes = structured_field(resp, "successes"); + assert_eq!(successes[0]["result"]["annotation"]["name"], "renamed"); assert_eq!( - annotations[0]["url"], + successes[0]["result"]["annotation"]["url"], "https://app.test.local/annotation/ann1" ); } @@ -408,9 +555,9 @@ async fn update_annotation_updates_each_annotation() { .await .expect("update_annotation failed"); - let annotations = structured_field(resp, "annotations"); - assert_eq!(annotations[0]["annotationId"], "ann1"); - assert_eq!(annotations[1]["annotationId"], "ann2"); + let successes = structured_field(resp, "successes"); + assert_eq!(successes[0]["result"]["annotation"]["annotationId"], "ann1"); + assert_eq!(successes[1]["result"]["annotation"]["annotationId"], "ann2"); } #[tokio::test] @@ -439,16 +586,22 @@ async fn update_annotation_reports_partial_failures_and_continues() { assert_eq!(resp.is_error, Some(true)); let content = serde_json::to_string(&resp.content).expect("content should serialize"); - assert_eq!(resp.content.len(), 1); - assert!(!content.contains("ann2")); - assert!(!content.contains("no such annotation")); + assert_eq!(resp.content.len(), 2); + assert!(content.contains("no such annotation")); let body = structured(resp); - assert_eq!(body["annotations"][0]["annotationId"], "ann1"); - assert_eq!(body["annotations"][1]["annotationId"], "ann3"); - assert_eq!(body["failures"][0]["annotation_id"], "ann2"); assert_eq!( - body["failures"][0]["message"], - "Some requested entity was not found: no such annotation" + body["successes"][0]["result"]["annotation"]["annotationId"], + "ann1" + ); + assert_eq!( + body["successes"][1]["result"]["annotation"]["annotationId"], + "ann3" + ); + assert_eq!(body["successes"][1]["index"], 2); + assert_eq!(body["failures"][0]["index"], 1); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 ); assert_eq!(body["not_attempted"], serde_json::json!([])); } @@ -475,37 +628,27 @@ async fn update_annotation_reports_ids_not_attempted_after_backend_wide_failure( assert_eq!(resp.is_error, Some(true)); let content = serde_json::to_string(&resp.content).expect("content should serialize"); - assert_eq!(resp.content.len(), 1); - assert!(!content.contains("ann0")); - assert!(!content.contains("ann59")); + assert_eq!(resp.content.len(), 2); + assert!(content.contains("RateLimited")); let body = structured(resp); assert_eq!(body["failures"].as_array().unwrap().len(), 50); assert_eq!(body["not_attempted"].as_array().unwrap().len(), 10); - assert_eq!(body["not_attempted"][0], "ann50"); - assert_eq!(body["not_attempted"][9], "ann59"); + assert_eq!(body["not_attempted"][0], 50); + assert_eq!(body["not_attempted"][9], 59); } #[tokio::test] -async fn update_annotation_batch_archives_annotations() { +async fn update_annotation_archives_each_annotation() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_batch_archive_annotations() - .withf(|req| req.get_ref().annotation_ids == ["ann1", "ann2"]) - .returning(|_| { - Ok(Response::new(BatchArchiveAnnotationsResponse { - annotations: vec![ - Annotation { - annotation_id: "ann1".into(), - is_archived: true, - ..Default::default() - }, - Annotation { - annotation_id: "ann2".into(), - is_archived: true, - ..Default::default() - }, - ], - })) - }); + mock.expect_archive_annotation().times(2).returning(|req| { + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: req.into_inner().annotation_id, + is_archived: true, + ..Default::default() + }), + })) + }); let (server, _h) = server_with_mock(mock).await; @@ -518,9 +661,9 @@ async fn update_annotation_batch_archives_annotations() { .await .expect("update_annotation failed"); - let annotations = structured_field(resp, "annotations"); - assert_eq!(annotations.as_array().unwrap().len(), 2); - assert_eq!(annotations[0]["isArchived"], true); + let successes = structured_field(resp, "successes"); + assert_eq!(successes.as_array().unwrap().len(), 2); + assert_eq!(successes[0]["result"]["annotation"]["isArchived"], true); } #[tokio::test] @@ -540,21 +683,16 @@ async fn update_annotation_updates_fields_before_archiving() { }); let archive_count = Arc::clone(&updated_count); - mock.expect_batch_archive_annotations() - .withf(move |_| archive_count.load(Ordering::SeqCst) == 2) + mock.expect_archive_annotation() + .times(2) + .withf(move |_| archive_count.load(Ordering::SeqCst) >= 1) .returning(|req| { - let annotations = req - .into_inner() - .annotation_ids - .into_iter() - .map(|annotation_id| Annotation { - annotation_id, + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: req.into_inner().annotation_id, is_archived: true, ..Default::default() - }) - .collect(); - Ok(Response::new(BatchArchiveAnnotationsResponse { - annotations, + }), })) }); @@ -583,7 +721,18 @@ async fn update_annotation_skips_archive_after_partial_field_failure() { annotation: Some(annotation), })) }); - mock.expect_batch_archive_annotations().times(0); + mock.expect_archive_annotation() + .times(1) + .withf(|req| req.get_ref().annotation_id == "ann1") + .returning(|req| { + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: req.into_inner().annotation_id, + is_archived: true, + ..Default::default() + }), + })) + }); let (server, _h) = server_with_mock(mock).await; @@ -599,16 +748,34 @@ async fn update_annotation_skips_archive_after_partial_field_failure() { assert_eq!(resp.is_error, Some(true)); let body = structured(resp); - assert_eq!(body["archive_skipped"], true); - assert_eq!(body["annotations"][0]["annotationId"], "ann1"); - assert_eq!(body["failures"][0]["annotation_id"], "ann2"); + assert!(body.get("archive_skipped").is_none()); + assert_eq!( + body["successes"][0]["result"]["annotation"]["annotationId"], + "ann1" + ); + assert_eq!( + body["successes"][0]["result"]["annotation"]["isArchived"], + true + ); + assert_eq!(body["failures"][0]["index"], 1); } #[tokio::test] -async fn update_annotation_reports_batch_archive_error() { +async fn update_annotation_reports_one_archive_error_without_stopping_others() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_batch_archive_annotations() - .returning(|_| Err(Status::not_found("one target was not found"))); + mock.expect_archive_annotation().times(2).returning(|req| { + let annotation_id = req.into_inner().annotation_id; + if annotation_id == "ann1" { + return Err(Status::permission_denied("not the creator")); + } + Ok(Response::new(ArchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id, + is_archived: true, + ..Default::default() + }), + })) + }); let (server, _h) = server_with_mock(mock).await; @@ -619,42 +786,38 @@ async fn update_annotation_reports_batch_archive_error() { let resp = server .update_annotation(Parameters(params)) .await - .expect("batch archive failure details should be returned"); + .expect("per-id archive failure details should be returned"); assert_eq!(resp.is_error, Some(true)); let body = structured(resp); + assert_eq!(body["updated_count"], 1); + assert_eq!(body["failures"][0]["index"], 0); + assert_eq!(body["successes"][0]["index"], 1); assert_eq!( - body["batch_archive_error"]["annotation_ids"], - serde_json::json!(["ann1", "ann2"]) + body["failures"][0]["error"]["data"]["reason"], + "PermissionDenied" ); - assert_eq!( - body["batch_archive_error"]["message"], - "Some requested entity was not found: one target was not found" - ); - assert!(body["batch_archive_error"].get("code").is_none()); + assert!(body.get("batch_archive_error").is_none()); } #[tokio::test] -#[allow(deprecated)] // The backend requires this deprecated field to unarchive annotations. async fn update_annotation_reports_partial_unarchive_failures() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_update_annotation().times(2).returning(|req| { - let req = req.into_inner(); - assert_eq!(req.update_mask.unwrap().paths, ["deleted_date"]); - let annotation = req.annotation.unwrap(); - assert_eq!(annotation.deleted_date, None); - let annotation_id = annotation.annotation_id; - if annotation_id == "ann2" { - return Err(Status::not_found("no such annotation")); - } - Ok(Response::new(UpdateAnnotationResponse { - annotation: Some(Annotation { - annotation_id, - is_archived: false, - ..Default::default() - }), - })) - }); + mock.expect_unarchive_annotation() + .times(2) + .returning(|req| { + let annotation_id = req.into_inner().annotation_id; + if annotation_id == "ann2" { + return Err(Status::not_found("no such annotation")); + } + Ok(Response::new(UnarchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id, + is_archived: false, + ..Default::default() + }), + })) + }); let (server, _h) = server_with_mock(mock).await; @@ -669,33 +832,32 @@ async fn update_annotation_reports_partial_unarchive_failures() { assert_eq!(resp.is_error, Some(true)); let body = structured(resp); - assert_eq!(body["annotations"][0]["annotationId"], "ann1"); - assert_eq!(body["failures"][0]["annotation_id"], "ann2"); assert_eq!( - body["failures"][0]["message"], - "Some requested entity was not found: no such annotation" + body["successes"][0]["result"]["annotation"]["annotationId"], + "ann1" + ); + assert_eq!(body["failures"][0]["index"], 1); + assert_eq!( + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 ); - assert_eq!(body["batch_archive_error"], serde_json::Value::Null); - assert!(body["failures"][0].get("code").is_none()); + assert!(body.get("batch_archive_error").is_none()); } #[tokio::test] -#[allow(deprecated)] // The backend requires this deprecated field to unarchive annotations. -async fn update_annotation_batch_unarchives_annotations() { +async fn update_annotation_unarchives_each_annotation() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_update_annotation().times(2).returning(|req| { - let req = req.into_inner(); - assert_eq!(req.update_mask.unwrap().paths, ["deleted_date"]); - let annotation = req.annotation.unwrap(); - assert_eq!(annotation.deleted_date, None); - Ok(Response::new(UpdateAnnotationResponse { - annotation: Some(Annotation { - annotation_id: annotation.annotation_id, - is_archived: false, - ..Default::default() - }), - })) - }); + mock.expect_unarchive_annotation() + .times(2) + .returning(|req| { + Ok(Response::new(UnarchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: req.into_inner().annotation_id, + is_archived: false, + ..Default::default() + }), + })) + }); let (server, _h) = server_with_mock(mock).await; @@ -708,22 +870,18 @@ async fn update_annotation_batch_unarchives_annotations() { .await .expect("update_annotation failed"); - let annotations = structured_field(resp, "annotations"); - assert_eq!(annotations.as_array().unwrap().len(), 2); - assert_eq!(annotations[0]["annotationId"], "ann1"); - assert_eq!(annotations[1]["annotationId"], "ann2"); + let successes = structured_field(resp, "successes"); + assert_eq!(successes.as_array().unwrap().len(), 2); + assert_eq!(successes[0]["result"]["annotation"]["annotationId"], "ann1"); + assert_eq!(successes[1]["result"]["annotation"]["annotationId"], "ann2"); } #[tokio::test] -#[allow(deprecated)] // The backend requires this deprecated field to unarchive annotations. async fn update_annotation_reports_unarchive_ids_not_attempted_after_backend_wide_failure() { let mut mock = MockAnnotationServiceImpl::new(); - mock.expect_update_annotation().times(50).returning(|req| { - let req = req.into_inner(); - assert_eq!(req.update_mask.unwrap().paths, ["deleted_date"]); - assert_eq!(req.annotation.unwrap().deleted_date, None); - Err(Status::resource_exhausted("slow down")) - }); + mock.expect_unarchive_annotation() + .times(50) + .returning(|_| Err(Status::unavailable("backend unavailable"))); let (server, _h) = server_with_mock(mock).await; @@ -741,32 +899,35 @@ async fn update_annotation_reports_unarchive_ids_not_attempted_after_backend_wid assert_eq!(resp.is_error, Some(true)); let body = structured(resp); assert_eq!(body["failures"].as_array().unwrap().len(), 50); - assert_eq!(body["not_attempted"], serde_json::json!(["ann51"])); - assert_eq!(body["batch_archive_error"], serde_json::Value::Null); + assert_eq!(body["not_attempted"], serde_json::json!([50])); + assert!(body.get("batch_archive_error").is_none()); } #[tokio::test] -#[allow(deprecated)] // The backend requires this deprecated field to unarchive annotations. async fn update_annotation_updates_fields_before_unarchiving() { let mut mock = MockAnnotationServiceImpl::new(); let updated_count = Arc::new(AtomicUsize::new(0)); let update_count = Arc::clone(&updated_count); mock.expect_update_annotation() - .times(4) + .times(2) .returning(move |req| { let req = req.into_inner(); - let paths = req.update_mask.unwrap().paths; - let annotation = req.annotation.unwrap(); - if paths == ["deleted_date"] { - assert_eq!(update_count.load(Ordering::SeqCst), 2); - assert_eq!(annotation.deleted_date, None); - } else { - assert_eq!(paths, ["name"]); - update_count.fetch_add(1, Ordering::SeqCst); - } + assert_eq!(req.update_mask.unwrap().paths, ["name"]); + update_count.fetch_add(1, Ordering::SeqCst); Ok(Response::new(UpdateAnnotationResponse { - annotation: Some(annotation), + annotation: req.annotation, + })) + }); + mock.expect_unarchive_annotation() + .times(2) + .withf(move |_| updated_count.load(Ordering::SeqCst) >= 1) + .returning(|req| { + Ok(Response::new(UnarchiveAnnotationResponse { + annotation: Some(Annotation { + annotation_id: req.into_inner().annotation_id, + ..Default::default() + }), })) }); @@ -802,7 +963,7 @@ async fn update_annotation_rejects_empty_ids() { async fn update_annotation_rejects_more_than_1000_ids_without_requests() { let mut mock = MockAnnotationServiceImpl::new(); mock.expect_update_annotation().times(0); - mock.expect_batch_archive_annotations().times(0); + mock.expect_archive_annotation().times(0); let (server, _h) = server_with_mock(mock).await; @@ -862,9 +1023,9 @@ async fn update_annotation_propagates_grpc_error() { assert_eq!(resp.is_error, Some(true)); let body = structured(resp); - assert_eq!(body["failures"][0]["annotation_id"], "ann1"); + assert_eq!(body["failures"][0]["index"], 0); assert_eq!( - body["failures"][0]["message"], - "Some requested entity was not found: no such annotation" + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 ); } From b2a072c3a3072e3bd29e03af83901bb195a7d4d6 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 11:08:49 -0800 Subject: [PATCH 13/19] rust(fix): map annotation results by requested id and flag partial archive writes --- .../sift_mcp/src/service/annotations/mod.rs | 10 ++++- .../sift_mcp/src/tool/annotations/mod.rs | 9 +++-- .../sift_mcp/src/tool/annotations/test.rs | 38 +++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/rust/crates/sift_mcp/src/service/annotations/mod.rs b/rust/crates/sift_mcp/src/service/annotations/mod.rs index 28b5a72f2..3ea8f3eb6 100644 --- a/rust/crates/sift_mcp/src/service/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/service/annotations/mod.rs @@ -32,6 +32,9 @@ pub struct AnnotationUpdateFailure { #[derive(Debug)] pub struct UpdateAnnotationsResult { pub annotations: Vec, + /// The requested id of each entry in `annotations`, in the same order. The + /// returned annotation's own id may differ in form from the request. + pub updated_ids: Vec, pub failures: Vec, pub not_attempted: Vec, } @@ -253,6 +256,7 @@ impl AnnotationService { || linked_channel_ids.is_some() || metadata.is_some(); let mut annotations = Vec::with_capacity(annotation_ids.len()); + let mut updated_ids = Vec::with_capacity(annotation_ids.len()); let mut failures = Vec::new(); let mut not_attempted = Vec::new(); @@ -309,7 +313,10 @@ impl AnnotationService { let mut stop = false; for (annotation_id, result) in updates { match result { - Ok(annotation) => annotations.push(annotation), + Ok(annotation) => { + annotations.push(annotation); + updated_ids.push(annotation_id); + } Err(error) => { let error = from_anyhow(error); stop |= bulk::should_stop(Op::Update, &error); @@ -329,6 +336,7 @@ impl AnnotationService { Ok(UpdateAnnotationsResult { annotations, + updated_ids, failures, not_attempted, }) diff --git a/rust/crates/sift_mcp/src/tool/annotations/mod.rs b/rust/crates/sift_mcp/src/tool/annotations/mod.rs index e2512b4c1..60a13cd2d 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/mod.rs @@ -602,12 +602,12 @@ impl SiftMcpServer { .ok() })?; let successes = outcome - .annotations + .updated_ids .iter() .zip(annotations) - .map(|(annotation, value)| { + .map(|(annotation_id, value)| { ( - positions[&annotation.annotation_id], + positions[annotation_id], CallToolResult::structured(serde_json::json!({"annotation": value})), ) }) @@ -629,7 +629,8 @@ impl SiftMcpServer { successes, failures, not_attempted, - false, + // An archive change that fails can follow a committed field update. + has_field_update && is_archived.is_some(), ) } } diff --git a/rust/crates/sift_mcp/src/tool/annotations/test.rs b/rust/crates/sift_mcp/src/tool/annotations/test.rs index 5e1ab7f95..16d5d4758 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/test.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/test.rs @@ -1029,3 +1029,41 @@ async fn update_annotation_propagates_grpc_error() { ErrorCode::RESOURCE_NOT_FOUND.0 ); } + +#[tokio::test] +async fn update_annotation_maps_results_by_the_requested_id() { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_update_annotation().times(1).returning(|req| { + let mut annotation = req.into_inner().annotation.unwrap(); + annotation.annotation_id = annotation.annotation_id.to_lowercase(); + Ok(Response::new(UpdateAnnotationResponse { + annotation: Some(annotation), + })) + }); + let (server, _h) = server_with_mock(mock).await; + let mut params = update_params("ANN-1"); + params.name = Some("renamed".into()); + let body = structured(server.update_annotation(Parameters(params)).await.unwrap()); + assert_eq!(body["updated_count"], 1); + assert_eq!(body["successes"][0]["index"], 0); +} + +#[tokio::test] +async fn a_failed_archive_after_a_field_update_is_an_uncertain_outcome() { + let mut mock = MockAnnotationServiceImpl::new(); + mock.expect_update_annotation().times(1).returning(|req| { + Ok(Response::new(UpdateAnnotationResponse { + annotation: req.into_inner().annotation, + })) + }); + mock.expect_archive_annotation() + .times(1) + .returning(|_| Err(Status::not_found("gone"))); + let (server, _h) = server_with_mock(mock).await; + let mut params = update_params("ann1"); + params.name = Some("renamed".into()); + params.is_archived = Some(true); + let body = structured(server.update_annotation(Parameters(params)).await.unwrap()); + assert_eq!(body["failures"][0]["index"], 0); + assert_eq!(body["failures"][0]["outcome_unknown"], true); +} From e88be0982b2c738dd9f68e4ab331fa81aa7594b9 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 11:09:19 -0800 Subject: [PATCH 14/19] docs: describe per-id update_annotation approval --- rust/crates/sift_cli/CHANGELOG.md | 8 ++++++++ rust/crates/sift_cli/assets/skills/sift/SKILL.md | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/rust/crates/sift_cli/CHANGELOG.md b/rust/crates/sift_cli/CHANGELOG.md index 3aff0e80e..f74726873 100644 --- a/rust/crates/sift_cli/CHANGELOG.md +++ b/rust/crates/sift_cli/CHANGELOG.md @@ -20,6 +20,14 @@ This project adheres to [Semantic Versioning](http://semver.org/). A missing or forbidden resource fails only its own item. - `archive_rule` and `unarchive_rule` now report a missing rule as not found instead of reporting success. +- `update_annotation` takes `selected_indices` over `annotation_ids`, so an + approval host can apply the shared changes to some ids and not others. It + takes up to 1000 ids, or up to 100 when it runs under an approval host + (`sift-cli mcp --chat`). Archiving and unarchiving now run per id. The result + now uses the same per-item `successes`, `failures`, and `not_attempted` lists + as the other bulk writes, indexed by position in `annotation_ids`. This is a + breaking change: the `batch_archive_error` and `archive_skipped` fields are + removed. ## [v0.9.0] - November 12, 2026 diff --git a/rust/crates/sift_cli/assets/skills/sift/SKILL.md b/rust/crates/sift_cli/assets/skills/sift/SKILL.md index 41b4edf4b..509a93dec 100644 --- a/rust/crates/sift_cli/assets/skills/sift/SKILL.md +++ b/rust/crates/sift_cli/assets/skills/sift/SKILL.md @@ -120,8 +120,10 @@ tool. - **Bulk writes:** every `create_*` write, and every `update_*`, `archive_*` and `unarchive_*` write except `update_annotation`, takes an `items` array of 1 to 100 objects. Pass one item to write one resource, or several to write a - batch in one call. Results report each item by its index in `items`. Never - replay a whole call after a partial failure; retry only the failed items. + batch in one call. `update_annotation` instead applies one set of changes to + every id in `annotation_ids`. Results report each item by its position in + `items` or `annotation_ids`. Never replay a whole call after a partial + failure; retry only the failed items. - **Writes:** `create_rule`, `update_rule`, `archive_rule`, `unarchive_rule`, `create_annotation`, `update_annotation`, `create_report`, `update_report`, `create_report_template`, `update_report_template`, From 917472fa344c1097bd2a5daab35d913a903a9d23 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Thu, 12 Nov 2026 15:53:23 -0800 Subject: [PATCH 15/19] rust(refactor): simplify bulk write validation and quote enum examples --- rust/crates/sift_mcp/src/error/mod.rs | 2 - .../sift_mcp/src/tool/annotations/mod.rs | 11 +- .../sift_mcp/src/tool/annotations/test.rs | 14 ++ .../sift_mcp/src/tool/artifacts/bulk_test.rs | 44 +++--- .../crates/sift_mcp/src/tool/artifacts/mod.rs | 60 ++++---- .../sift_mcp/src/tool/assets/bulk_test.rs | 43 ------ rust/crates/sift_mcp/src/tool/assets/mod.rs | 14 +- .../src/tool/calculated_channels/mod.rs | 30 ++-- rust/crates/sift_mcp/src/tool/common/bulk.rs | 141 ++++++++---------- .../sift_mcp/src/tool/report_templates/mod.rs | 12 +- .../src/tool/report_templates/test.rs | 33 ---- rust/crates/sift_mcp/src/tool/reports/mod.rs | 21 ++- rust/crates/sift_mcp/src/tool/reports/test.rs | 38 ----- rust/crates/sift_mcp/src/tool/rules/mod.rs | 89 +++++------ rust/crates/sift_mcp/src/tool/rules/test.rs | 57 ------- .../sift_mcp/src/tool/runs/bulk_test.rs | 43 ------ rust/crates/sift_mcp/src/tool/runs/mod.rs | 14 +- .../src/tool/user_defined_functions/mod.rs | 44 +++--- 18 files changed, 237 insertions(+), 473 deletions(-) diff --git a/rust/crates/sift_mcp/src/error/mod.rs b/rust/crates/sift_mcp/src/error/mod.rs index 1969e07d9..db988a0fe 100644 --- a/rust/crates/sift_mcp/src/error/mod.rs +++ b/rust/crates/sift_mcp/src/error/mod.rs @@ -122,8 +122,6 @@ pub fn from_anyhow(error: anyhow::Error) -> rmcp::ErrorData { if let Ok(grpc_status) = error.downcast::() { code = from_grpc_code(grpc_status.code()); - // The resource's state rejected the write, so nothing was written and the - // backend is healthy. Bulk writes use this to fail only the one item. if grpc_status.code() == Code::FailedPrecondition { data = Some(serde_json::json!({"reason": "FailedPrecondition"})); } diff --git a/rust/crates/sift_mcp/src/tool/annotations/mod.rs b/rust/crates/sift_mcp/src/tool/annotations/mod.rs index 60a13cd2d..6c780b597 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/mod.rs @@ -243,9 +243,9 @@ impl SiftMcpServer { - `description`: optional free-text description. - `start_time_unix_nanos` / `end_time_unix_nanos`: required time bounds in Unix nanoseconds. `end_time_unix_nanos` must be >= `start_time_unix_nanos`. - - `annotation_type`: required; `ANNOTATION_TYPE_DATA_REVIEW` or `ANNOTATION_TYPE_PHASE`. - - `state`: optional; `ANNOTATION_STATE_OPEN`, `ANNOTATION_STATE_FLAGGED`, or - `ANNOTATION_STATE_RESOLVED`. MUST be omitted when `annotation_type` is `ANNOTATION_TYPE_PHASE` + - `annotation_type`: required; `\"ANNOTATION_TYPE_DATA_REVIEW\"` or `\"ANNOTATION_TYPE_PHASE\"`. + - `state`: optional; `\"ANNOTATION_STATE_OPEN\"`, `\"ANNOTATION_STATE_FLAGGED\"`, or + `\"ANNOTATION_STATE_RESOLVED\"`. MUST be omitted when `annotation_type` is `\"ANNOTATION_TYPE_PHASE\"` (the server rejects a phase annotation with a state). - `assets`: optional list of asset NAMES to associate. - `tags`: optional list of tag names to associate. Names that do not yet exist are created. @@ -462,8 +462,8 @@ impl SiftMcpServer { - `description`: optional new description. - `start_time_unix_nanos` / `end_time_unix_nanos`: optional new time bounds in Unix nanoseconds. - `assigned_to_user_id`: optional new assignee user id. - - `state`: optional; `ANNOTATION_STATE_OPEN`, `ANNOTATION_STATE_FLAGGED`, or - `ANNOTATION_STATE_RESOLVED`. + - `state`: optional JSON string: `\"ANNOTATION_STATE_OPEN\"`, `\"ANNOTATION_STATE_FLAGGED\"`, or + `\"ANNOTATION_STATE_RESOLVED\"`. - `tags`: optional; REPLACES the full tag list. Pass `[]` to clear all tags. - `linked_channel_ids`: optional; REPLACES the full linked-channel list with plain channel links. Pass `[]` to clear. Bit-field and calculated-channel links are not exposed here. @@ -629,7 +629,6 @@ impl SiftMcpServer { successes, failures, not_attempted, - // An archive change that fails can follow a committed field update. has_field_update && is_archived.is_some(), ) } diff --git a/rust/crates/sift_mcp/src/tool/annotations/test.rs b/rust/crates/sift_mcp/src/tool/annotations/test.rs index 16d5d4758..8ab13f6e5 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/test.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/test.rs @@ -111,6 +111,20 @@ fn annotation_tool_schemas_publish_exact_enum_values() { ); } +#[test] +fn write_descriptions_quote_json_enum_values() { + for name in ["create_annotation", "update_annotation"] { + let tool = annotation_tool(name); + let description = tool.description.as_deref().unwrap(); + for state in ["OPEN", "FLAGGED", "RESOLVED"] { + assert!( + description.contains(&format!("`\"ANNOTATION_STATE_{state}\"`")), + "{name}: {description}" + ); + } + } +} + #[test] fn list_annotations_description_publishes_filter_enum_values() { let tool = annotation_tool("list_annotations"); diff --git a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs index 4b2ddee50..cd07d78e8 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs @@ -130,38 +130,30 @@ async fn resource_errors_continue_and_backend_errors_stop() { match op { "archive" => { mock.expect_archive_artifact() - .withf(|req| req.get_ref().artifact_id == "bad") - .returning(move |_| Err(Status::new(code, "failed"))); - if !stop { - mock.expect_archive_artifact() - .times(1) - .withf(|req| req.get_ref().artifact_id == "good") - .returning(|_| Ok(Response::new(ArchiveArtifactResponse {}))); - } + .times(if stop { 1 } else { 2 }) + .returning(move |req| match req.get_ref().artifact_id.as_str() { + "bad" => Err(Status::new(code, "failed")), + "good" if !stop => Ok(Response::new(ArchiveArtifactResponse {})), + _ => panic!("unexpected write"), + }); } "unarchive" => { mock.expect_unarchive_artifact() - .withf(|req| req.get_ref().artifact_id == "bad") - .returning(move |_| Err(Status::new(code, "failed"))); - if !stop { - mock.expect_unarchive_artifact() - .times(1) - .withf(|req| req.get_ref().artifact_id == "good") - .returning(|_| Ok(Response::new(UnarchiveArtifactResponse {}))); - } + .times(if stop { 1 } else { 2 }) + .returning(move |req| match req.get_ref().artifact_id.as_str() { + "bad" => Err(Status::new(code, "failed")), + "good" if !stop => Ok(Response::new(UnarchiveArtifactResponse {})), + _ => panic!("unexpected write"), + }); } _ => { mock.expect_update_artifact() - .withf(|req| req.get_ref().artifact_id == "bad") - .returning(move |_| Err(Status::new(code, "failed"))); - if !stop { - mock.expect_update_artifact() - .times(1) - .withf(|req| req.get_ref().artifact_id == "good") - .returning(|req| { - Ok(Response::new(updated(&req.get_ref().artifact_id))) - }); - } + .times(if stop { 1 } else { 2 }) + .returning(move |req| match req.get_ref().artifact_id.as_str() { + "bad" => Err(Status::new(code, "failed")), + "good" if !stop => Ok(Response::new(updated("good"))), + _ => panic!("unexpected write"), + }); } } let (server, _h) = server_with_mock(mock, true).await; diff --git a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs index 99bb02697..2531b1fef 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs @@ -740,17 +740,21 @@ impl SiftMcpServer { selected_indices, .. }) = params; - let mut items = bulk::parse_items::(items)?; - for (index, item) in items.iter_mut().enumerate() { - item.artifact_id = item.artifact_id.trim().to_owned(); - if item.artifact_id.is_empty() { - return Err(bulk::at_item( - index, - ErrorData::invalid_params("`artifact_id` must not be empty", None), - )); - } - } - bulk::reject_duplicate_targets(&items, "artifact", |item| Some(item.artifact_id.clone()))?; + let items = bulk::prepare( + items, + "artifact", + |mut item: ArtifactArchiveItem| { + item.artifact_id = item.artifact_id.trim().to_owned(); + if item.artifact_id.is_empty() { + return Err(ErrorData::invalid_params( + "`artifact_id` must not be empty", + None, + )); + } + Ok(item) + }, + |item| Some(item.artifact_id.clone()), + )?; bulk::run( op, items, @@ -828,18 +832,18 @@ impl SiftMcpServer { - `summary`: optional short description stored on the version. - `conversation_id`: optional. Links the new artifact to that conversation. The caller must be the conversation\'s author. - - `storage_class`: optional; `file` (default) or `blob`, matched case-insensitively. Proto names + - `storage_class`: optional; `\"file\"` (default) or `\"blob\"`, matched case-insensitively. Proto names are also accepted. - - `created_via`: optional; `agent` (default), `canvas`, or `upload`, matched + - `created_via`: optional; `\"agent\"` (default), `\"canvas\"`, or `\"upload\"`, matched case-insensitively. Proto names are also accepted. - `metadata`: optional list of `{ \"name\": \"\", \"value\": }` entries. - `links`: optional list of `{ \"relation\", \"entity_type\", \"entity_id\" }` entries. `relation` - accepts `attached_to`, `source`, or `derived_from`, plus proto names. `entity_type` accepts - `conversation`, `canvas`, `run`, `asset`, `artifact`, or `tool_use`, plus proto names. - Use `source` for what the artifact was built from, such as the `run` or `asset` whose data - it summarizes. `derived_from` is only for another artifact (`entity_type: \"artifact\"`); - the server rejects it for any other entity type. `attached_to` places the artifact where it - surfaces, such as a `conversation` or `canvas`; prefer `conversation_id` over an explicit + accepts `\"attached_to\"`, `\"source\"`, or `\"derived_from\"`, plus proto names. `entity_type` accepts + `\"conversation\"`, `\"canvas\"`, `\"run\"`, `\"asset\"`, `\"artifact\"`, or `\"tool_use\"`, plus proto names. + Use `\"source\"` for what the artifact was built from, such as the `\"run\"` or `\"asset\"` whose data + it summarizes. `\"derived_from\"` is only for another artifact (`entity_type: \"artifact\"`); + the server rejects it for any other entity type. `\"attached_to\"` places the artifact where it + surfaces, such as a `\"conversation\"` or `\"canvas\"`; prefer `conversation_id` over an explicit conversation link. - `file_path`: optional absolute or relative path of a local file to upload as this version\'s content. The file streams to Sift\'s file store; its name and extension drive @@ -871,7 +875,7 @@ impl SiftMcpServer { - Use `storage_class: \"blob\"` for opaque intermediates. - To store JSON, such as a computed table, write it to a local `.json` file and pass it as `file_path`. - - Leave `created_via` unset inside a Sift agent session; it defaults to `agent`. + - Leave `created_via` unset inside a Sift agent session; it defaults to `\"agent\"`. - One artifact per real deliverable. Do not create artifacts for intermediate scratch files. - Items run sequentially, one create per item; this is not an atomic batch. Never replay the whole call after a partial failure. Retry only failed items, and check any @@ -1049,12 +1053,16 @@ impl SiftMcpServer { selected_indices, .. }) = params; - let mut items = bulk::parse_items::(items)?; - for (index, item) in items.iter_mut().enumerate() { - item.artifact_id = item.artifact_id.trim().to_owned(); - validate_update_artifact(item).map_err(|error| bulk::at_item(index, error))?; - } - bulk::reject_duplicate_targets(&items, "artifact", |item| Some(item.artifact_id.clone()))?; + let items = bulk::prepare( + items, + "artifact", + |mut item: UpdateArtifactItem| { + item.artifact_id = item.artifact_id.trim().to_owned(); + validate_update_artifact(&item)?; + Ok(item) + }, + |item| Some(item.artifact_id.clone()), + )?; bulk::run( Op::Update, items, diff --git a/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs b/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs index f36b89b52..8816f86a6 100644 --- a/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs +++ b/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs @@ -38,49 +38,6 @@ async fn selected_subset_only_updates_selected_assets() { ); } -#[tokio::test] -async fn item_errors_continue_but_unavailable_stops_the_tail() { - for code in [ - tonic::Code::NotFound, - tonic::Code::PermissionDenied, - tonic::Code::Unavailable, - ] { - let mut mock = MockAssetServiceImpl::new(); - mock.expect_update_asset() - .withf(|req| req.get_ref().asset.as_ref().unwrap().asset_id == "bad") - .returning(move |_| Err(Status::new(code, "failed"))); - if code != tonic::Code::Unavailable { - mock.expect_update_asset() - .times(1) - .withf(|req| req.get_ref().asset.as_ref().unwrap().asset_id == "good") - .returning(|req| { - Ok(Response::new(UpdateAssetResponse { - asset: req.into_inner().asset, - })) - }); - } - let (server, _h) = server_with_mock(mock).await; - let result = server - .update_asset(params( - json!([{"asset_id":"bad","tags":[]},{"asset_id":"good","tags":[]}]), - None, - )) - .await - .unwrap(); - assert_eq!(result.is_error, Some(true)); - let body = structured(result); - assert_eq!(body["failures"].as_array().unwrap().len(), 1); - assert_eq!( - body["not_attempted"].as_array().unwrap().len(), - usize::from(code == tonic::Code::Unavailable) - ); - assert_eq!( - body["updated_count"], - usize::from(code != tonic::Code::Unavailable) - ); - } -} - #[tokio::test] async fn invalid_calls_and_missing_chat_masks_write_nothing() { let (server, _h) = server_with_mock(MockAssetServiceImpl::new()).await; diff --git a/rust/crates/sift_mcp/src/tool/assets/mod.rs b/rust/crates/sift_mcp/src/tool/assets/mod.rs index 7218b3500..213650196 100644 --- a/rust/crates/sift_mcp/src/tool/assets/mod.rs +++ b/rust/crates/sift_mcp/src/tool/assets/mod.rs @@ -187,11 +187,15 @@ impl SiftMcpServer { selected_indices, .. }) = params; - let items = bulk::parse_items::(items)?; - for (index, item) in items.iter().enumerate() { - validate_update_asset(item).map_err(|error| bulk::at_item(index, error))?; - } - bulk::reject_duplicate_targets(&items, "asset", |item| Some(item.asset_id.clone()))?; + let items = bulk::prepare( + items, + "asset", + |item: UpdateAssetItem| { + validate_update_asset(&item)?; + Ok(item) + }, + |item| Some(item.asset_id.clone()), + )?; bulk::run( Op::Update, items, diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs index f308705f5..176bee071 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs @@ -492,14 +492,12 @@ impl SiftMcpServer { self.require_destructive()?; let Parameters(params) = params; - let updates = bulk::parse_items::(params.items)? - .into_iter() - .enumerate() - .map(|(index, item)| { - calculated_channel_update(item).map_err(|error| bulk::at_item(index, error)) - }) - .collect::, _>>()?; - bulk::reject_duplicate_targets(&updates, "calculated channel", |(id, _)| Some(id.clone()))?; + let updates = bulk::prepare( + params.items, + "calculated channel", + calculated_channel_update, + |(id, _)| Some(id.clone()), + )?; bulk::run( Op::Update, updates, @@ -661,13 +659,15 @@ impl SiftMcpServer { op: Op, ) -> error::McpResult { let Parameters(params) = params; - let items = bulk::parse_items::(params.items)?; - for (index, item) in items.iter().enumerate() { - require_id(&item.calculated_channel_id).map_err(|error| bulk::at_item(index, error))?; - } - bulk::reject_duplicate_targets(&items, "calculated channel", |item| { - Some(item.calculated_channel_id.clone()) - })?; + let items = bulk::prepare( + params.items, + "calculated channel", + |item: CalculatedChannelArchiveItem| { + require_id(&item.calculated_channel_id)?; + Ok(item) + }, + |item| Some(item.calculated_channel_id.clone()), + )?; bulk::run( op, items, diff --git a/rust/crates/sift_mcp/src/tool/common/bulk.rs b/rust/crates/sift_mcp/src/tool/common/bulk.rs index 8086b01da..7d98f0900 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -13,7 +13,6 @@ use crate::{error::McpResult, service::batch}; pub const MAX_ITEMS: usize = 100; -/// Shared item list and approval mask for every bulk write tool. #[derive(Debug, JsonSchema)] #[schemars(deny_unknown_fields)] pub struct BulkItems { @@ -50,9 +49,6 @@ impl<'de, T> Deserialize<'de> for BulkItems { } } -/// The single place that decides which top-level shapes a bulk tool accepts. -/// Only `items` (plus the host's `selected_indices`) is accepted. A call in the -/// old one-resource shape is rejected with a migration hint. fn bulk_call(object: Map) -> Result { if !object.contains_key("items") { return Err( @@ -64,7 +60,6 @@ fn bulk_call(object: Map) -> Result { serde_json::from_value(Value::Object(object)).map_err(|error| error.to_string()) } -/// Prefix an item's validation error with its index in `items`. pub fn at_item(index: usize, error: ErrorData) -> ErrorData { ErrorData::new( error.code, @@ -73,6 +68,21 @@ pub fn at_item(index: usize, error: ErrorData) -> ErrorData { ) } +pub fn prepare( + items: Vec, + resource: &str, + validate: impl Fn(T) -> Result, + target: impl Fn(&U) -> Option, +) -> Result, ErrorData> { + let items = parse_items::(items)? + .into_iter() + .enumerate() + .map(|(index, item)| validate(item).map_err(|error| at_item(index, error))) + .collect::, _>>()?; + reject_duplicate_targets(&items, resource, target)?; + Ok(items) +} + pub fn parse_items( items: Vec, ) -> Result, ErrorData> { @@ -103,8 +113,6 @@ pub fn parse_items( .collect() } -/// Reject a call in which two items write the same resource, so the result -/// never depends on item order. `target` returns `None` for items without one. pub fn reject_duplicate_targets( items: &[T], resource: &str, @@ -123,7 +131,6 @@ pub fn reject_duplicate_targets( Ok(()) } -/// The approval mask and whether this server requires one. #[derive(Debug, Clone)] pub struct Selection { pub indices: Option>, @@ -132,8 +139,6 @@ pub struct Selection { pub required: bool, } -/// The kind of write a bulk tool performs. It sets the result's count field, -/// its wording, and which errors stop the remaining items. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Op { Create, @@ -166,10 +171,6 @@ fn reason(error: &ErrorData) -> Option<&str> { error.data.as_ref()?.get("reason")?.as_str() } -/// Item-specific errors let the remaining items run. Anything that points at the -/// backend or the caller's session stops them. Permission on an existing resource -/// is scoped to that resource (rule assets, artifact creator), so it is item-specific -/// for writes to existing resources. pub(crate) fn should_stop(op: Op, error: &ErrorData) -> bool { if error.code == ErrorCode::INVALID_PARAMS || error.code == ErrorCode::RESOURCE_NOT_FOUND { return false; @@ -181,7 +182,6 @@ pub(crate) fn should_stop(op: Op, error: &ErrorData) -> bool { } } -/// The approval mask applied to `count` input items. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Selected { /// Input positions to write, in input order. @@ -191,7 +191,6 @@ pub struct Selected { } impl Selected { - /// Keep only the selected items, in input order. pub fn filter(&self, items: Vec) -> Vec { let mut keep = self.indices.iter().peekable(); items @@ -202,8 +201,6 @@ impl Selected { } } -/// Validate the approval mask for `count` input items. A required mask that is -/// missing fails closed before any write. pub fn select(count: usize, selection: Selection) -> Result { let selected_indices = match selection.indices { Some(indices) => indices, @@ -268,7 +265,6 @@ where ) } -/// Build the bulk result body. Every index is a position in the caller's input. pub fn report( op: Op, requested_count: usize, @@ -285,8 +281,6 @@ pub fn report( let failures = failures .into_iter() .map(|(index, error)| { - // An interrupted write or a composite task can commit before returning - // an error. Never instruct callers to blindly replay a failed write. let outcome_unknown = partial_writes || (error.code != ErrorCode::INVALID_PARAMS && error.code != ErrorCode::RESOURCE_NOT_FOUND @@ -329,7 +323,6 @@ pub fn report( let text = body.to_string(); let mut result = CallToolResult::structured(body); result.is_error = Some(has_errors); - // Models in some MCP clients cannot see structuredContent. result.content = vec![ContentBlock::text(next_step), ContentBlock::text(text)]; Ok(result) } @@ -417,39 +410,6 @@ mod test { } } - #[tokio::test] - async fn a_failed_precondition_fails_one_update_without_stopping_the_rest() { - let precondition = signal(tonic::Code::FailedPrecondition); - assert_eq!( - precondition.data.as_ref().unwrap()["reason"], - "FailedPrecondition" - ); - assert!(should_stop(Op::Create, &precondition)); - let result = run( - Op::Update, - vec![0, 1, 2], - Selection { - indices: Some(vec![0, 1, 2]), - required: true, - }, - false, - |item| async move { - if item == 0 { - Err(signal(tonic::Code::FailedPrecondition)) - } else { - Ok(CallToolResult::structured(json!({"item": item}))) - } - }, - ) - .await - .unwrap(); - let body = result.structured_content.unwrap(); - assert_eq!(body["updated_count"], 2); - assert_eq!(body["failures"][0]["index"], 0); - assert_eq!(body["failures"][0]["outcome_unknown"], false); - assert_eq!(body["not_attempted"], json!([])); - } - #[test] fn select_keeps_input_positions_and_filters_in_order() { let selected = select( @@ -516,31 +476,52 @@ mod test { } #[tokio::test] - async fn a_permission_failure_on_one_archive_does_not_stop_the_rest() { - let result = run( - Op::Archive, - vec![0, 1, 2], - Selection { - indices: Some(vec![0, 1, 2]), - required: true, - }, - false, - |item| async move { - if item == 1 { - Err(signal(tonic::Code::PermissionDenied)) - } else { - Ok(CallToolResult::structured(json!({"item": item}))) - } - }, - ) - .await - .unwrap(); - assert_eq!(result.is_error, Some(true)); - let body = result.structured_content.unwrap(); - assert_eq!(body["archived_count"], 2); - assert_eq!(body["failures"][0]["index"], 1); - assert_eq!(body["failures"][0]["outcome_unknown"], false); - assert_eq!(body["not_attempted"], json!([])); + async fn selected_item_failures_and_stop_rules_preserve_input_positions() { + use tonic::Code::{ + AlreadyExists, FailedPrecondition, NotFound, PermissionDenied, Unavailable, + }; + for op in [Op::Create, Op::Update, Op::Archive, Op::Unarchive] { + for code in [ + NotFound, + PermissionDenied, + FailedPrecondition, + AlreadyExists, + Unavailable, + ] { + let stops = code == Unavailable + || (op == Op::Create && matches!(code, PermissionDenied | FailedPrecondition)); + let result = run( + op, + vec![0, 1, 2, 3], + Selection { + indices: Some(vec![3, 2, 0]), + required: true, + }, + false, + |item| async move { + match item { + 1 => panic!("unselected item ran"), + 2 => Err(signal(code)), + 3 if stops => panic!("tail ran after backend failure"), + _ => Ok(CallToolResult::structured(json!({"item": item}))), + } + }, + ) + .await + .unwrap(); + assert_eq!(result.is_error, Some(true)); + let body = result.structured_content.unwrap(); + assert_eq!(body["not_selected"], json!([1])); + assert_eq!(body[op.count_key()], if stops { 1 } else { 2 }); + assert_eq!(body["successes"][0]["index"], 0); + assert_eq!(body["failures"][0]["index"], 2); + assert_eq!(body["failures"][0]["outcome_unknown"], code == Unavailable); + assert_eq!( + body["not_attempted"], + if stops { json!([3]) } else { json!([]) } + ); + } + } } #[tokio::test] diff --git a/rust/crates/sift_mcp/src/tool/report_templates/mod.rs b/rust/crates/sift_mcp/src/tool/report_templates/mod.rs index 759142ab9..238358d6d 100644 --- a/rust/crates/sift_mcp/src/tool/report_templates/mod.rs +++ b/rust/crates/sift_mcp/src/tool/report_templates/mod.rs @@ -419,12 +419,12 @@ impl SiftMcpServer { self.require_destructive()?; let Parameters(params) = params; - let updates = bulk::parse_items::(params.items)? - .into_iter() - .enumerate() - .map(|(index, item)| template_update(item).map_err(|error| bulk::at_item(index, error))) - .collect::, _>>()?; - bulk::reject_duplicate_targets(&updates, "report template", |(id, _)| Some(id.clone()))?; + let updates = bulk::prepare( + params.items, + "report template", + template_update, + |(id, _)| Some(id.clone()), + )?; bulk::run( Op::Update, updates, diff --git a/rust/crates/sift_mcp/src/tool/report_templates/test.rs b/rust/crates/sift_mcp/src/tool/report_templates/test.rs index f1c9391cb..bb06d3806 100644 --- a/rust/crates/sift_mcp/src/tool/report_templates/test.rs +++ b/rust/crates/sift_mcp/src/tool/report_templates/test.rs @@ -104,39 +104,6 @@ mod bulk { handle.abort(); } - #[tokio::test] - async fn not_found_continues_and_unavailable_stops_tail() { - let mut mock = MockReportTemplateServiceImpl::new(); - mock.expect_update_report_template().returning(|req| { - let template = req.into_inner().report_template.unwrap(); - match template.report_template_id.as_str() { - "missing" => Err(Status::not_found("missing template")), - "down" => Err(Status::unavailable("backend down")), - "never" => panic!("tail must not run"), - _ => Ok(Response::new(UpdateReportTemplateResponse { - report_template: Some(template), - })), - } - }); - let (server, handle) = server_with_mock(mock).await; - let body = body( - call( - &server, - json!({"items": [item("missing"), item("ok"), item("down"), item("never")]}), - ) - .await - .unwrap(), - ); - assert_eq!(body["updated_count"], 1); - assert_eq!( - body["failures"][0]["error"]["code"], - ErrorCode::RESOURCE_NOT_FOUND.0 - ); - assert_eq!(body["failures"][1]["index"], 2); - assert_eq!(body["not_attempted"], json!([3])); - handle.abort(); - } - #[tokio::test] async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { let (server, handle) = server_with_mock(MockReportTemplateServiceImpl::new()).await; diff --git a/rust/crates/sift_mcp/src/tool/reports/mod.rs b/rust/crates/sift_mcp/src/tool/reports/mod.rs index c450b793b..5c21ae03e 100644 --- a/rust/crates/sift_mcp/src/tool/reports/mod.rs +++ b/rust/crates/sift_mcp/src/tool/reports/mod.rs @@ -488,15 +488,14 @@ impl SiftMcpServer { ) -> error::McpResult { self.require_destructive()?; let Parameters(params) = params; - let items = bulk::parse_items::(params.items)?; - let updates = items - .into_iter() - .enumerate() - .map(|(index, item)| { + let updates = bulk::prepare( + params.items, + "report", + |item: UpdateReportItem| { if item.report_id.trim().is_empty() { - return Err(bulk::at_item( - index, - ErrorData::invalid_params("`report_id` must not be empty", None), + return Err(ErrorData::invalid_params( + "`report_id` must not be empty", + None, )); } Ok(( @@ -506,9 +505,9 @@ impl SiftMcpServer { .map(MetadataValue::from) .collect::>(), )) - }) - .collect::, _>>()?; - bulk::reject_duplicate_targets(&updates, "report", |(id, _)| Some(id.clone()))?; + }, + |(id, _)| Some(id.clone()), + )?; bulk::run( Op::Update, updates, diff --git a/rust/crates/sift_mcp/src/tool/reports/test.rs b/rust/crates/sift_mcp/src/tool/reports/test.rs index 6941e6609..26f7b5382 100644 --- a/rust/crates/sift_mcp/src/tool/reports/test.rs +++ b/rust/crates/sift_mcp/src/tool/reports/test.rs @@ -65,44 +65,6 @@ mod bulk { handle.abort(); } - #[tokio::test] - async fn not_found_continues_and_unavailable_stops_tail() { - let mut mock = MockReportServiceImpl::new(); - mock.expect_update_report().returning(|req| { - match req.get_ref().report.as_ref().unwrap().report_id.as_str() { - "missing" => Err(Status::not_found("missing report")), - "down" => Err(Status::unavailable("backend down")), - "never" => panic!("tail must not run"), - _ => Ok(Response::new(UpdateReportResponse {})), - } - }); - mock.expect_get_report().times(1).returning(|_| { - Ok(Response::new(GetReportResponse { - report: Some(Report { - report_id: "ok".into(), - ..Default::default() - }), - })) - }); - let (server, handle) = server_with_mock(mock).await; - let body = body( - call( - &server, - json!({"items": [item("missing"), item("ok"), item("down"), item("never")]}), - ) - .await - .unwrap(), - ); - assert_eq!(body["updated_count"], 1); - assert_eq!( - body["failures"][0]["error"]["code"], - ErrorCode::RESOURCE_NOT_FOUND.0 - ); - assert_eq!(body["failures"][1]["index"], 2); - assert_eq!(body["not_attempted"], json!([3])); - handle.abort(); - } - #[tokio::test] async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { let (server, handle) = server_with_mock(MockReportServiceImpl::new()).await; diff --git a/rust/crates/sift_mcp/src/tool/rules/mod.rs b/rust/crates/sift_mcp/src/tool/rules/mod.rs index 46a1fd27f..c64ab78ec 100644 --- a/rust/crates/sift_mcp/src/tool/rules/mod.rs +++ b/rust/crates/sift_mcp/src/tool/rules/mod.rs @@ -329,23 +329,18 @@ impl SiftMcpServer { #[tool( name = "update_rule", description = " - Update specific fields of one or more existing Sift rules. This is a WRITE. Only the fields you - set on an item are changed; everything else on that rule is preserved. The tool fetches the - current rule, overlays your changes, and saves the result, so each item needs only the fields - you want to modify. + Update fields of existing Sift rules, preserving omitted fields. This is a WRITE. Output: - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, - not_attempted, next_step }`. `successes` holds `{ index, result }` per updated item, where - `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, - outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any - failure sets `isError`. Each `result` is: + not_attempted, next_step }`. Successes: `{ index, result }`; failures: `{ index, error, + outcome_unknown }`. Indices refer to input positions; backend failures leave a `not_attempted` + tail. Any failure sets `isError`. Each result is: - `{ \"rule_id\": \"\", \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per rule. Pass one item to update one - rule. Each item takes: + - `items`: 1–100 objects, each with: - `rule_id`: required. The rule to update. - `name`: optional. New rule name. - `description`: optional. New description. @@ -357,9 +352,8 @@ impl SiftMcpServer { `conditions` field of `create_rule`'s `rule_json`. Omit it to keep the existing conditions unchanged — only provide it when you intend to rewrite the rule's logic. - At least one field besides `rule_id` must be set. - - `selected_indices`: zero-based positions in `items` to update. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` updates nothing. + - `selected_indices`: host-owned zero-based input positions; omit when proposing. + Omitted selects all outside Sift chat; chat requires a mask. `[]` writes nothing. Errors: - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two @@ -395,13 +389,9 @@ impl SiftMcpServer { selected_indices, .. }) = params; - let items = bulk::parse_items::(items)?; - let updates = items - .into_iter() - .enumerate() - .map(|(index, item)| rule_update(item).map_err(|error| bulk::at_item(index, error))) - .collect::, _>>()?; - bulk::reject_duplicate_targets(&updates, "rule", |(rule_id, _)| Some(rule_id.clone()))?; + let updates = bulk::prepare(items, "rule", rule_update, |(rule_id, _)| { + Some(rule_id.clone()) + })?; bulk::run( Op::Update, updates, @@ -443,21 +433,18 @@ impl SiftMcpServer { Output: - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, - not_attempted, next_step }`. `successes` holds `{ index, result }` per archived item, where - `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, - outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any - failure sets `isError`. Each `result` is: + not_attempted, next_step }`. Successes: `{ index, result }`; failures: `{ index, error, + outcome_unknown }`. Indices refer to input positions; backend failures leave a `not_attempted` + tail. Any failure sets `isError`. Each result is: - `{ \"rule_id\": \"\", \"name\": string, \"archived\": true, \"rule_url\": string|null }`. `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per rule. Pass one item to archive one - rule. Each item takes exactly one of: + - `items`: 1–100 objects, each with exactly one of: - `rule_id`: the id of the rule to archive. - `client_key`: the caller-defined key of the rule to archive. - - `selected_indices`: zero-based positions in `items` to archive. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` archives nothing. + - `selected_indices`: host-owned zero-based input positions; omit when proposing. + Omitted selects all outside Sift chat; chat requires a mask. `[]` writes nothing. Errors: - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two @@ -495,21 +482,18 @@ impl SiftMcpServer { Output: - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, - not_attempted, next_step }`. `successes` holds `{ index, result }` per unarchived item, - where `index` is the item's zero-based position in `items`. `failures` holds `{ index, - error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. - Any failure sets `isError`. Each `result` is: + not_attempted, next_step }`. Successes: `{ index, result }`; failures: `{ index, error, + outcome_unknown }`. Indices refer to input positions; backend failures leave a `not_attempted` + tail. Any failure sets `isError`. Each result is: - `{ \"rule_id\": \"\", \"name\": string, \"unarchived\": true, \"rule_url\": string|null }`. `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per rule. Pass one item to unarchive one - rule. Each item takes exactly one of: + - `items`: 1–100 objects, each with exactly one of: - `rule_id`: the id of the rule to unarchive. - `client_key`: the caller-defined key of the rule to unarchive. - - `selected_indices`: zero-based positions in `items` to unarchive. An approval host sets - this from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` unarchives nothing. + - `selected_indices`: host-owned zero-based input positions; omit when proposing. + Omitted selects all outside Sift chat; chat requires a mask. `[]` writes nothing. Errors: - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two @@ -549,21 +533,18 @@ impl SiftMcpServer { selected_indices, .. }) = params; - let targets = bulk::parse_items::(items)? - .into_iter() - .enumerate() - .map(|(index, item)| { - rule_identifier(item.rule_id, item.client_key) - .map_err(|error| bulk::at_item(index, error)) - }) - .collect::, _>>()?; - bulk::reject_duplicate_targets(&targets, "rule", |(rule_id, client_key)| { - Some(if rule_id.is_empty() { - format!("client_key:{client_key}") - } else { - format!("rule_id:{rule_id}") - }) - })?; + let targets = bulk::prepare( + items, + "rule", + |item: RuleArchiveItem| rule_identifier(item.rule_id, item.client_key), + |(rule_id, client_key)| { + Some(if rule_id.is_empty() { + format!("client_key:{client_key}") + } else { + format!("rule_id:{rule_id}") + }) + }, + )?; bulk::run( op, targets, @@ -574,8 +555,6 @@ impl SiftMcpServer { .await } - /// Resolve the rule first: the archive RPCs succeed silently when nothing - /// matches, which would report a typo as archived. async fn set_rule_archived( &self, rule_id: String, diff --git a/rust/crates/sift_mcp/src/tool/rules/test.rs b/rust/crates/sift_mcp/src/tool/rules/test.rs index 8c103b972..3ff6de4f6 100644 --- a/rust/crates/sift_mcp/src/tool/rules/test.rs +++ b/rust/crates/sift_mcp/src/tool/rules/test.rs @@ -164,39 +164,6 @@ mod bulk { handle.abort(); } - #[tokio::test] - async fn permission_denied_fails_one_item_and_unavailable_stops_the_rest() { - let mut mock = MockRuleServiceImpl::new(); - mock.expect_get_rule() - .returning(|request| match request.get_ref().rule_id.as_str() { - "denied" => Err(Status::permission_denied("no edit permission")), - "down" => Err(Status::unavailable("backend down")), - rule_id => found(rule_id), - }); - mock.expect_archive_rule() - .times(1) - .returning(|_| Ok(Response::new(ArchiveRuleResponse {}))); - let (server, handle) = server(mock).await; - let items = ["denied", "ok", "down", "never"].map(|id| json!({"rule_id": id})); - let body = structured( - server - .archive_rule(params( - json!({"items": items, "selected_indices": [0, 1, 2, 3]}), - )) - .await - .unwrap(), - ); - assert_eq!(body["archived_count"], 1); - assert_eq!(body["failures"][0]["index"], 0); - assert_eq!( - body["failures"][0]["error"]["data"]["reason"], - "PermissionDenied" - ); - assert_eq!(body["failures"][1]["index"], 2); - assert_eq!(body["not_attempted"], json!([3])); - handle.abort(); - } - #[tokio::test] async fn invalid_or_duplicate_items_fail_before_any_rpc() { let (server, handle) = server(MockRuleServiceImpl::new()).await; @@ -244,30 +211,6 @@ mod bulk { handle.abort(); } - #[tokio::test] - async fn the_chat_host_must_send_a_mask() { - let (server, handle) = server(MockRuleServiceImpl::new()).await; - let error = server - .archive_rule(params(json!({"items": [{"rule_id": "a"}]}))) - .await - .unwrap_err(); - assert!( - error.message.contains("nothing was written"), - "{}", - error.message - ); - handle.abort(); - } - - #[test] - fn the_legacy_single_rule_shape_is_rejected() { - let error = serde_json::from_value::< - crate::tool::common::bulk::BulkItems, - >(json!({"rule_id": "a"})) - .unwrap_err(); - assert!(error.to_string().contains("`items`"), "{error}"); - } - #[tokio::test] async fn update_rule_runs_one_get_and_update_per_selected_item() { let mut mock = MockRuleServiceImpl::new(); diff --git a/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs b/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs index d79bc2bab..f86226d66 100644 --- a/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs +++ b/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs @@ -37,49 +37,6 @@ async fn selected_subset_only_updates_selected_runs() { ); } -#[tokio::test] -async fn item_errors_continue_but_unavailable_stops_the_tail() { - for code in [ - tonic::Code::NotFound, - tonic::Code::PermissionDenied, - tonic::Code::Unavailable, - ] { - let mut mock = MockRunServiceImpl::new(); - mock.expect_update_run() - .withf(|req| req.get_ref().run.as_ref().unwrap().run_id == "bad") - .returning(move |_| Err(Status::new(code, "failed"))); - if code != tonic::Code::Unavailable { - mock.expect_update_run() - .times(1) - .withf(|req| req.get_ref().run.as_ref().unwrap().run_id == "good") - .returning(|req| { - Ok(Response::new(UpdateRunResponse { - run: req.into_inner().run, - })) - }); - } - let (server, _h) = server_with_mock(mock).await; - let result = server - .update_run(params( - json!([{"run_id":"bad","name":"x"},{"run_id":"good","name":"y"}]), - None, - )) - .await - .unwrap(); - assert_eq!(result.is_error, Some(true)); - let body = structured(result); - assert_eq!(body["failures"].as_array().unwrap().len(), 1); - assert_eq!( - body["not_attempted"].as_array().unwrap().len(), - usize::from(code == tonic::Code::Unavailable) - ); - assert_eq!( - body["updated_count"], - usize::from(code != tonic::Code::Unavailable) - ); - } -} - #[tokio::test] async fn invalid_calls_and_missing_chat_masks_write_nothing() { let (server, _h) = server_with_mock(MockRunServiceImpl::new()).await; diff --git a/rust/crates/sift_mcp/src/tool/runs/mod.rs b/rust/crates/sift_mcp/src/tool/runs/mod.rs index d59b56612..7a46cbbfc 100644 --- a/rust/crates/sift_mcp/src/tool/runs/mod.rs +++ b/rust/crates/sift_mcp/src/tool/runs/mod.rs @@ -206,11 +206,15 @@ impl SiftMcpServer { selected_indices, .. }) = params; - let items = bulk::parse_items::(items)?; - for (index, item) in items.iter().enumerate() { - validate_update_run(item).map_err(|error| bulk::at_item(index, error))?; - } - bulk::reject_duplicate_targets(&items, "run", |item| Some(item.run_id.clone()))?; + let items = bulk::prepare( + items, + "run", + |item: UpdateRunItem| { + validate_update_run(&item)?; + Ok(item) + }, + |item| Some(item.run_id.clone()), + )?; bulk::run( Op::Update, items, diff --git a/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs b/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs index d8d21b0a6..9c361a951 100644 --- a/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs +++ b/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs @@ -294,8 +294,8 @@ impl SiftMcpServer { Errors: - `INVALID_PARAMS` if `name` or `expression` is empty, `function_inputs_json` is not a JSON - array of input objects, an `identifier` is empty, a `data_type` is not one of `numeric`, - `string`, or `bool`, or the server rejects the expression (unresolved identifier, type + array of input objects, an `identifier` is empty, a `data_type` is not one of `\"numeric\"`, + `\"string\"`, or `\"bool\"`, or the server rejects the expression (unresolved identifier, type mismatch, duplicate function name). - `INTERNAL_ERROR` for upstream gRPC failures. - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, or an @@ -469,14 +469,12 @@ impl SiftMcpServer { self.require_destructive()?; let Parameters(params) = params; - let updates = bulk::parse_items::(params.items)? - .into_iter() - .enumerate() - .map(|(index, item)| udf_update(item).map_err(|error| bulk::at_item(index, error))) - .collect::, _>>()?; - bulk::reject_duplicate_targets(&updates, "user defined function", |(id, _)| { - Some(id.clone()) - })?; + let updates = bulk::prepare( + params.items, + "user defined function", + udf_update, + |(id, _)| Some(id.clone()), + )?; bulk::run( Op::Update, updates, @@ -626,18 +624,20 @@ impl SiftMcpServer { op: Op, ) -> error::McpResult { let Parameters(params) = params; - let items = bulk::parse_items::(params.items)?; - for (index, item) in items.iter().enumerate() { - if item.user_defined_function_id.trim().is_empty() { - return Err(bulk::at_item( - index, - ErrorData::invalid_params("`user_defined_function_id` must not be empty", None), - )); - } - } - bulk::reject_duplicate_targets(&items, "user defined function", |item| { - Some(item.user_defined_function_id.clone()) - })?; + let items = bulk::prepare( + params.items, + "user defined function", + |item: ArchiveUserDefinedFunctionItem| { + if item.user_defined_function_id.trim().is_empty() { + return Err(ErrorData::invalid_params( + "`user_defined_function_id` must not be empty", + None, + )); + } + Ok(item) + }, + |item| Some(item.user_defined_function_id.clone()), + )?; bulk::run( op, items, From e60aacbeb71bbf5f0a9ad49753571ef808645e30 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Fri, 13 Nov 2026 15:23:52 -0800 Subject: [PATCH 16/19] rust(refactor): share bulk write plumbing and drop per-item next_step --- rust/crates/sift_cli/CHANGELOG.md | 5 + rust/crates/sift_mcp/CLAUDE.md | 9 + .../sift_mcp/src/tool/annotations/mod.rs | 39 +- .../sift_mcp/src/tool/artifacts/bulk_test.rs | 8 +- .../crates/sift_mcp/src/tool/artifacts/mod.rs | 298 +++++---------- .../sift_mcp/src/tool/artifacts/test.rs | 344 ++++++------------ rust/crates/sift_mcp/src/tool/assets/mod.rs | 59 +-- rust/crates/sift_mcp/src/tool/assets/test.rs | 14 +- .../src/tool/calculated_channels/mod.rs | 301 +++++---------- .../src/tool/calculated_channels/test.rs | 35 +- rust/crates/sift_mcp/src/tool/common/bulk.rs | 50 ++- .../sift_mcp/src/tool/common/bulk_create.rs | 14 +- rust/crates/sift_mcp/src/tool/common/mod.rs | 56 +++ rust/crates/sift_mcp/src/tool/common/test.rs | 35 +- .../sift_mcp/src/tool/report_templates/mod.rs | 114 ++---- .../src/tool/report_templates/test.rs | 16 - rust/crates/sift_mcp/src/tool/reports/mod.rs | 96 ++--- rust/crates/sift_mcp/src/tool/rules/mod.rs | 192 +++------- rust/crates/sift_mcp/src/tool/rules/test.rs | 31 +- rust/crates/sift_mcp/src/tool/runs/mod.rs | 59 +-- rust/crates/sift_mcp/src/tool/runs/test.rs | 70 ++-- .../sift_mcp/src/tool/test_reports/mod.rs | 26 +- .../src/tool/user_defined_functions/mod.rs | 257 ++++--------- .../src/tool/user_defined_functions/test.rs | 11 +- 24 files changed, 740 insertions(+), 1399 deletions(-) diff --git a/rust/crates/sift_cli/CHANGELOG.md b/rust/crates/sift_cli/CHANGELOG.md index a1a1f1023..657b5ed2c 100644 --- a/rust/crates/sift_cli/CHANGELOG.md +++ b/rust/crates/sift_cli/CHANGELOG.md @@ -28,6 +28,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). as the other bulk writes, indexed by position in `annotation_ids`. This is a breaking change: the `batch_archive_error` and `archive_skipped` fields are removed. +- Bulk write results no longer repeat a `next_step` inside every item. The + call-level `next_step` covers the whole batch, and an item carries a `warning` + only when it needs attention, such as a calculated channel that will not + evaluate on some assets. `create_artifact` and `update_artifact` items keep a + one-line `next_step` naming the artifact and version. - MCP `create_declarative_chart` takes an optional `surface`: `explore`, `canvas`, `edge`, or `agents` (agent chat). The default is `agents` inside Sift Agents and `explore` elsewhere. It validates the spec for that surface and diff --git a/rust/crates/sift_mcp/CLAUDE.md b/rust/crates/sift_mcp/CLAUDE.md index 19fe99920..5becdb773 100644 --- a/rust/crates/sift_mcp/CLAUDE.md +++ b/rust/crates/sift_mcp/CLAUDE.md @@ -288,6 +288,15 @@ pub async fn list_webhooks(&self, params: Parameters) -> error::McpR set both the structured `next_step` field and `result.content = vec![Content::text(next_step)]` so the calling agent sees it. `get_data` and `explore_url` are the reference patterns. Write tools use `next_step` to force a confirmation before the agent proceeds. +- **Bulk writes.** A tool that takes `items` gates itself first, then calls + `self.bulk_write(op, params.0, resource, validate, target, write)` from `tool/common/bulk.rs`. + `validate` checks one item before any write, `target` returns the key that must not repeat + across items, and `write` performs one item. Reach for `bulk::prepare` and `bulk::run` directly + only when a failure can follow a partial write (`create_artifact`, `create_report`). The + call-level `next_step` covers the batch, so an item result carries no `next_step` of its own, + only a `warning` when that item needs the user's attention. Artifact results are the one + exception: the Sift chat host matches `Created artifact ` in them. Do not repeat the + `selected_indices` or `items` bounds in the description; the `BulkItems` schema documents them. - **Read tools keep their payload in `content`.** Clients such as Claude Code show the model `content`, not `structuredContent`. Replacing `content` with `next_step` alone hides the payload from the agent. A `list_*` tool built on `list_body` passes its `next_step` to `into_result` diff --git a/rust/crates/sift_mcp/src/tool/annotations/mod.rs b/rust/crates/sift_mcp/src/tool/annotations/mod.rs index 6c780b597..21a9bea53 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -18,7 +18,7 @@ use crate::tool::common::{ use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, - tool::common::{MetadataEntry, list_body, url_clause, with_urls}, + tool::common::{MetadataEntry, list_body, with_urls}, }; #[cfg(test)] @@ -108,8 +108,8 @@ pub struct CreateAnnotationParams { #[schemars(length(min = 1, max = 100))] #[schemars(with = "Vec")] items: Vec, - /// Approval host's zero-based selection of `items` to create. Omit when proposing. - /// An empty selection performs no writes. + /// Approval host's zero-based selection of `items` to create. Omit when proposing; + /// omitted means every item. An empty selection performs no writes. selected_indices: Option>, /// Run associated with every item. run_id: Option, @@ -121,6 +121,8 @@ pub struct CreateAnnotationParams { #[serde(deny_unknown_fields)] pub struct UpdateAnnotationParams { annotation_ids: Vec, + /// Approval host's zero-based selection of `annotation_ids` to write. Omit when proposing; + /// omitted means every id. An empty selection performs no writes. selected_indices: Option>, name: Option, description: Option, @@ -231,14 +233,13 @@ impl SiftMcpServer { `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - - `{ \"annotation\": Annotation, \"annotation_url\": string|null, \"next_step\": string }`. The + - `{ \"annotation\": Annotation, \"annotation_url\": string|null }`. The returned `Annotation` is the server-assigned state including its new `annotation_id`; `annotation_url` is its Sift web link (`/annotation/`), or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per annotation. Pass one item to create one - annotation. Each item takes: + - `items`: one object per annotation. Each item takes: - `name`: required; the annotation's display name. - `description`: optional free-text description. - `start_time_unix_nanos` / `end_time_unix_nanos`: required time bounds in Unix nanoseconds. @@ -260,9 +261,6 @@ impl SiftMcpServer { inside an item. - `organization_id`: optional, shared by every item. Required only when the caller belongs to multiple organizations. - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Errors: - `INVALID_PARAMS` if `name` is empty, the time range is inverted, `annotation_type`/`state` is not a @@ -417,25 +415,10 @@ impl SiftMcpServer { .url_service .build_annotation_url(&annotation.annotation_id) .ok(); - let next_step = format!( - "Created annotation `{}` ({}).{} Surface the new annotation to the user and confirm it \ - matches their intent.", - annotation.name, - annotation.annotation_id, - url_clause( - "annotation", - Some(&annotation.name), - annotation_url.as_deref() - ), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "annotation": annotation, "annotation_url": annotation_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } #[tool( @@ -456,8 +439,6 @@ impl SiftMcpServer { - `annotation_ids`: required list of 1–1000 unique, nonempty annotation ids, or 1–100 in chat. Split chat work into calls of at most 100 ids. Pass one id to update one annotation. The same changes apply to every selected annotation. - - `selected_indices`: zero-based positions in `annotation_ids` supplied by the approval host; - omit when proposing. Omitted selects all outside chat; chat requires a mask. `[]` writes nothing. - `name`: optional new name. - `description`: optional new description. - `start_time_unix_nanos` / `end_time_unix_nanos`: optional new time bounds in Unix nanoseconds. diff --git a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs index cd07d78e8..76dd4f568 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs @@ -102,11 +102,9 @@ async fn selection_preserves_positions_and_artifact_relay_text() { assert_eq!(body["not_selected"], json!([1])); assert_eq!(body["successes"][1]["index"], 2); if op == "update" { - assert!( - body["successes"][0]["result"]["next_step"] - .as_str() - .unwrap() - .starts_with("Wrote version 2 of artifact a") + assert_eq!( + body["successes"][0]["result"]["next_step"], + "Wrote version 2 of artifact a." ); assert_eq!( body["successes"][1]["result"]["artifact"]["artifactId"], diff --git a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs index 2531b1fef..e7846a92b 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -12,15 +12,15 @@ use sift_rs::artifacts::v1::{ ArtifactLinkRelation, ArtifactStorageClass, ArtifactVersion, }; -use crate::tool::common::{ - bulk::{self, BulkItems, Op}, - bulk_create, -}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, service::artifacts::{ArtifactView, CreateArtifactInput, UpdateArtifactInput, UpdatePath}, - tool::common::{MetadataEntry, list_body, to_values, url_clause}, + tool::common::{ + MetadataEntry, + bulk::{self, BulkItems, Op}, + list_body, require_non_empty, to_values, + }, }; #[cfg(test)] @@ -229,12 +229,7 @@ fn parse_links(links: Option>) -> Result Result<(), ErrorData> { - if item.artifact_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`artifact_id` must not be empty", - None, - )); - } + require_non_empty("artifact_id", &item.artifact_id)?; let file_path = normalized_file_path(item.file_path.clone())?; let links = parse_links(item.links.clone())?; if item.title.is_none() @@ -262,34 +257,38 @@ fn validate_update_artifact(item: &UpdateArtifactItem) -> Result<(), ErrorData> Ok(()) } -fn content_note(artifact: &ArtifactView, uploaded: bool) -> &'static str { - if uploaded && artifact.download_url.is_some() { - " Its file content was uploaded and the user can preview and download it." - } else if uploaded { - " Its file content was uploaded, but the refreshed artifact or its download link \ - could not be fetched; call `download_artifact` for the link." - } else if artifact.download_url.is_some() { - " It keeps the previous version's file, which the user can preview and download." - } else if artifact.version().remote_file_id.is_some() { - " It keeps the previous version's file, but its download link could not be fetched; \ - call `download_artifact` for the link." - } else { - " It has no file content; the user has nothing to preview or download." - } +/// A warning when the artifact has file content but no download link came back, so +/// the caller does not tell the user the file is missing. +fn download_warning(artifact: &ArtifactView, uploaded: bool) -> Option<&'static str> { + let has_file = uploaded || artifact.version().remote_file_id.is_some(); + (has_file && artifact.download_url.is_none()).then_some( + "The file content is stored, but its download link could not be fetched; call \ + `download_artifact` for the link.", + ) } +/// One bulk write result: the artifact, the name the UI shows for it, its Sift link, a +/// one-line `next_step`, and a `warning` when one applies. +/// +/// Unlike other bulk results, artifact results keep a per-item `next_step`: the Sift chat +/// host finds the artifacts to show by matching `Created artifact ` and +/// `artifact ` in the tool result, so this text is a host contract. fn artifact_result( + server: &SiftMcpServer, artifact: &ArtifactView, - artifact_url: Option, next_step: String, + warning: Option<&str>, ) -> error::McpResult { - let mut result = CallToolResult::structured(serde_json::json!({ + let mut body = serde_json::json!({ "artifact": artifact_view_value(artifact)?, - "artifact_url": artifact_url, + "name": display_name(artifact), + "artifact_url": artifact_url_for(server, &artifact.artifact_id(), None), "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }); + if let Some(warning) = warning { + body["warning"] = warning.into(); + } + Ok(CallToolResult::structured(body)) } /// Build the web URL for an artifact, pinned to `artifact_version_id` when set. @@ -614,14 +613,12 @@ impl SiftMcpServer { Output: - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, not_attempted, next_step }`. Each success holds `{ index, result }`, where result has - `artifact_id`, `archived: true`, `artifact_url`, and `next_step`. The URL links the artifact + `artifact_id`, `archived: true`, and `artifact_url`. The URL links the artifact in Sift, or is null if the host cannot be derived. Failures hold `{ index, error, outcome_unknown }`. Any failure sets `isError`. Parameters: - - `items`: required array of 1–100 objects. Each item takes `artifact_id`, the stable artifact id. - - `selected_indices`: zero-based item positions supplied by the approval host; omit when proposing. - Omitted selects all outside chat; chat requires a mask. `[]` archives nothing. + - `items`: each item takes `artifact_id`, the stable artifact id. Errors: - `INVALID_PARAMS` before any write for empty or oversized `items`, duplicate targets, unknown @@ -646,43 +643,9 @@ impl SiftMcpServer { pub async fn archive_artifact( &self, params: Parameters>, - ) -> error::McpResult { - self.archive_artifacts(params, Op::Archive).await - } - - async fn archive_artifact_item( - &self, - params: Parameters, ) -> error::McpResult { self.require_destructive()?; - let Parameters(ArtifactArchiveItem { artifact_id }) = params; - // The service trims the id, so the link has to use the trimmed value too. - let artifact_id = artifact_id.trim().to_owned(); - if artifact_id.is_empty() { - return Err(ErrorData::invalid_params( - "`artifact_id` must not be empty", - None, - )); - } - - let artifact_url = artifact_url_for(self, &artifact_id, None); - self.artifact_service - .archive_artifact(artifact_id.clone()) - .await - .map_err(from_anyhow)?; - - let next_step = format!( - "Archived artifact `{artifact_id}`.{}", - url_clause("artifact", None, artifact_url.as_deref()), - ); - let mut result = CallToolResult::structured(serde_json::json!({ - "artifact_id": artifact_id, - "archived": true, - "artifact_url": artifact_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + self.set_artifacts_archived(params, Op::Archive).await } #[tool( @@ -694,14 +657,12 @@ impl SiftMcpServer { Output: - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, not_attempted, next_step }`. Each success holds `{ index, result }`, where result has - `artifact_id`, `unarchived: true`, `artifact_url`, and `next_step`. The URL links the artifact + `artifact_id`, `unarchived: true`, and `artifact_url`. The URL links the artifact in Sift, or is null if the host cannot be derived. Failures hold `{ index, error, outcome_unknown }`. Any failure sets `isError`. Parameters: - - `items`: required array of 1–100 objects. Each item takes `artifact_id`, the stable artifact id. - - `selected_indices`: zero-based item positions supplied by the approval host; omit when proposing. - Omitted selects all outside chat; chat requires a mask. `[]` unarchives nothing. + - `items`: each item takes `artifact_id`, the stable artifact id. Errors: - `INVALID_PARAMS` before any write for empty or oversized `items`, duplicate targets, unknown @@ -726,83 +687,48 @@ impl SiftMcpServer { &self, params: Parameters>, ) -> error::McpResult { - self.archive_artifacts(params, Op::Unarchive).await + self.require_destructive()?; + self.set_artifacts_archived(params, Op::Unarchive).await } - async fn archive_artifacts( + async fn set_artifacts_archived( &self, params: Parameters>, op: Op, ) -> error::McpResult { - self.require_destructive()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - let items = bulk::prepare( - items, - "artifact", - |mut item: ArtifactArchiveItem| { - item.artifact_id = item.artifact_id.trim().to_owned(); - if item.artifact_id.is_empty() { - return Err(ErrorData::invalid_params( - "`artifact_id` must not be empty", - None, - )); - } - Ok(item) - }, - |item| Some(item.artifact_id.clone()), - )?; - bulk::run( + self.bulk_write( op, - items, - self.selection(selected_indices), - false, - |item| async move { - match op { - Op::Archive => self.archive_artifact_item(Parameters(item)).await, - _ => self.unarchive_artifact_item(Parameters(item)).await, - } + params.0, + "artifact", + |item: ArtifactArchiveItem| { + // The service trims the id, so the link has to use the trimmed value too. + let artifact_id = item.artifact_id.trim().to_owned(); + require_non_empty("artifact_id", &artifact_id)?; + Ok(artifact_id) }, + |artifact_id| Some(artifact_id.clone()), + |artifact_id| self.set_artifact_archived(artifact_id, op), ) .await } - async fn unarchive_artifact_item( - &self, - params: Parameters, - ) -> error::McpResult { - self.require_destructive()?; - let Parameters(ArtifactArchiveItem { artifact_id }) = params; - // The service trims the id, so the link has to use the trimmed value too. - let artifact_id = artifact_id.trim().to_owned(); - if artifact_id.is_empty() { - return Err(ErrorData::invalid_params( - "`artifact_id` must not be empty", - None, - )); - } - - let artifact_url = artifact_url_for(self, &artifact_id, None); - self.artifact_service - .unarchive_artifact(artifact_id.clone()) - .await - .map_err(from_anyhow)?; + async fn set_artifact_archived(&self, artifact_id: String, op: Op) -> error::McpResult { + let write = if op == Op::Archive { + self.artifact_service + .archive_artifact(artifact_id.clone()) + .await + } else { + self.artifact_service + .unarchive_artifact(artifact_id.clone()) + .await + }; + write.map_err(from_anyhow)?; - let next_step = format!( - "Unarchived artifact `{artifact_id}`.{}", - url_clause("artifact", None, artifact_url.as_deref()), - ); - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "artifact_id": artifact_id, - "unarchived": true, - "artifact_url": artifact_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + op.state_key(): true, + "artifact_url": artifact_url_for(self, &artifact_id, None), + }))) } #[tool( @@ -819,15 +745,16 @@ impl SiftMcpServer { `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - - `{ \"artifact\": Artifact, \"artifact_url\": string|null, \"next_step\": string }`. The + - `{ \"artifact\": Artifact, \"name\": string|null, \"artifact_url\": string|null, \"next_step\": string, + \"warning\"?: string }`. `name` is the title, or the file name when the title is blank. The returned artifact is version 1, including `artifact_id`, `artifact_version_id`, and `version`. When a file was uploaded it also carries `file_name`, `file_mime_type`, `remote_file_id`, and a short-lived signed `download_url`. `artifact_url` is the artifact's Sift web link - (`/artifacts/`), or null when the host can't be derived. + (`/artifacts/`), or null when the host can't be derived. `warning` + appears when the file is stored but its download link could not be fetched. Parameters: - - `items`: required array of 1–100 objects, one per artifact. Pass one item to create one - artifact. Each item takes: + - `items`: one object per artifact. Each item takes: - `title`: optional display title stored on the version. - `summary`: optional short description stored on the version. - `conversation_id`: optional. Links the new artifact to that conversation. The caller must be @@ -849,9 +776,6 @@ impl SiftMcpServer { version\'s content. The file streams to Sift\'s file store; its name and extension drive the mime type and how the UI previews it. Regular, non-empty files up to 1 GiB. Omit it to record metadata only (content can not be attached later to the same version). - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Access: - Needs `--allow-create`. @@ -892,20 +816,21 @@ impl SiftMcpServer { )] pub async fn create_artifact( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(bulk_create::CreateItems { + let Parameters(BulkItems { items, selected_indices, .. }) = params; - let items = bulk_create::parse_items::(items)?; - bulk_create::create_many_with_partial_writes( - items, + // An upload can fail after the artifact exists, so every failure may be a partial write. + bulk::run( + Op::Create, + bulk::parse_items::(items)?, self.selection(selected_indices), - |item| self.create_artifact_item(Parameters(item)), true, + |item| self.create_artifact_item(Parameters(item)), ) .await } @@ -964,20 +889,17 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let artifact_url = artifact_url_for(self, &artifact.artifact_id(), None); let next_step = format!( - "Created artifact {} version {}.{}{} Surface the title and destination to the \ - user and confirm they match their intent before further edits.", + "Created artifact {} version {}.", artifact.artifact_id(), artifact.version().version, - content_note(&artifact, uploaded), - url_clause( - "artifact", - display_name(&artifact).as_deref(), - artifact_url.as_deref() - ), ); - artifact_result(&artifact, artifact_url, next_step) + artifact_result( + self, + &artifact, + next_step, + download_warning(&artifact, uploaded), + ) } #[tool( @@ -993,14 +915,14 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, not_attempted, next_step }`. Each success holds `{ index, result }`, each failure holds `{ index, error, outcome_unknown }`, and any failure sets `isError`. Each result is: - - `{ \"artifact\": Artifact, \"artifact_url\": string|null, \"next_step\": string }`. Same + - `{ \"artifact\": Artifact, \"name\": string|null, \"artifact_url\": string|null, \"next_step\": string, + \"warning\"?: string }`. Same shape as `download_artifact`, resolved to the version this call wrote. `artifact_url` links - the artifact rather than this version, so it keeps showing the latest. + the artifact rather than this version, so it keeps showing the latest. `warning` appears + when the version has a file but its download link could not be fetched. Parameters: - - `items`: required array of 1–100 objects, one per artifact. Each item takes the fields below. - - `selected_indices`: zero-based item positions supplied by the approval host; omit when proposing. - Omitted selects all outside chat; chat requires a mask. `[]` updates nothing. + - `items`: one object per artifact. Each item takes the fields below. - `artifact_id`: required stable container id. Resolve it with `list_artifacts` if you only have a title. - `title`: new display title. Pass an empty string to leave the artifact showing its file name. @@ -1063,38 +985,26 @@ impl SiftMcpServer { }, |item| Some(item.artifact_id.clone()), )?; + // An upload can fail after the version is written, so every failure may be a partial write. bulk::run( Op::Update, items, self.selection(selected_indices), true, - |item| self.update_artifact_item(Parameters(item)), + |item| self.update_artifact_item(item), ) .await } - async fn update_artifact_item( - &self, - params: Parameters, - ) -> error::McpResult { - self.require_destructive()?; - validate_update_artifact(¶ms.0)?; - let Parameters(UpdateArtifactItem { + async fn update_artifact_item(&self, item: UpdateArtifactItem) -> error::McpResult { + let UpdateArtifactItem { artifact_id, title, summary, metadata, links, file_path, - }) = params; - - if artifact_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`artifact_id` must not be empty", - None, - )); - } - let file_path = normalized_file_path(file_path)?; + } = item; let mut version = ArtifactVersion::default(); let mut update_mask = Vec::new(); @@ -1114,12 +1024,6 @@ impl SiftMcpServer { if !links.is_empty() { update_mask.push(UpdatePath::Links); } - if update_mask.is_empty() && file_path.is_none() { - return Err(ErrorData::invalid_params( - "pass at least one of `title`, `summary`, `metadata`, `links`, or `file_path`", - None, - )); - } let uploaded = file_path.is_some(); let artifact = self @@ -1136,18 +1040,16 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let artifact_url = artifact_url_for(self, &artifact.artifact_id(), None); let next_step = format!( - "Wrote version {} of artifact {}.{}{}", + "Wrote version {} of artifact {}.", artifact.version().version, artifact.artifact_id(), - content_note(&artifact, uploaded), - url_clause( - "artifact", - display_name(&artifact).as_deref(), - artifact_url.as_deref() - ), ); - artifact_result(&artifact, artifact_url, next_step) + artifact_result( + self, + &artifact, + next_step, + download_warning(&artifact, uploaded), + ) } } diff --git a/rust/crates/sift_mcp/src/tool/artifacts/test.rs b/rust/crates/sift_mcp/src/tool/artifacts/test.rs index 4eea72977..05db4324b 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/test.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/test.rs @@ -21,19 +21,24 @@ use sift_test_util::{ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; +use serde_json::{Value, json}; + use super::{ - ArtifactArchiveItem as ArtifactArchiveParams, ArtifactVersionListParams, CreateArtifactItem, - DownloadArtifactParams, UpdateArtifactItem as UpdateArtifactParams, display_name, + ArtifactVersionListParams, CreateArtifactItem, DownloadArtifactParams, display_name, parse_created_via, parse_storage_class, }; use crate::{ + error::McpResult, server::SiftMcpServer, service::artifacts::ArtifactView, tool::{ artifacts::ArtifactListParams, common::{ MetadataEntry, MetadataScalar, - test_support::{assert_keys_before, content_texts, structured, structured_field}, + test_support::{ + assert_keys_before, bulk_params, content_texts, first_result, structured, + structured_field, + }, }, }, }; @@ -428,27 +433,23 @@ async fn list_artifact_versions_trims_the_artifact_id() { ); } -#[tokio::test] -async fn archive_artifact_blocked_without_allow_destructive() { - let (server, _h) = server_with_mocks( - MockArtifactServiceImpl::new(), - MockRemoteFileServiceImpl::new(), - true, - false, - ) - .await; - let err = server - .archive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: "art-1".into(), - })) +async fn set_archived(server: &SiftMcpServer, archive: bool, artifact_id: &str) -> McpResult { + let params = bulk_params(json!([{ "artifact_id": artifact_id }]), None); + if archive { + server.archive_artifact(params).await + } else { + server.unarchive_artifact(params).await + } +} + +async fn update_one(server: &SiftMcpServer, item: Value) -> McpResult { + server + .update_artifact(bulk_params(json!([item]), None)) .await - .expect_err("archive gated"); - assert_eq!(err.code, ErrorCode::INVALID_REQUEST); - assert!(err.message.contains("--allow-destructive")); } #[tokio::test] -async fn unarchive_artifact_blocked_without_allow_destructive() { +async fn archive_and_unarchive_are_blocked_without_allow_destructive() { let (server, _h) = server_with_mocks( MockArtifactServiceImpl::new(), MockRemoteFileServiceImpl::new(), @@ -456,98 +457,28 @@ async fn unarchive_artifact_blocked_without_allow_destructive() { false, ) .await; - let err = server - .unarchive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: "art-1".into(), - })) - .await - .expect_err("unarchive gated"); - assert_eq!(err.code, ErrorCode::INVALID_REQUEST); - assert!(err.message.contains("--allow-destructive")); -} - -#[tokio::test] -async fn archive_artifact_rejects_empty_artifact_id() { - let (server, _h) = server_with_mock(MockArtifactServiceImpl::new(), true).await; - let err = server - .archive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: " ".into(), - })) - .await - .expect_err("empty id"); - assert_eq!(err.code, ErrorCode::INVALID_PARAMS); + for archive in [true, false] { + let err = set_archived(&server, archive, "art-1") + .await + .expect_err("gated"); + assert_eq!(err.code, ErrorCode::INVALID_REQUEST); + assert!(err.message.contains("--allow-destructive")); + } } #[tokio::test] -async fn unarchive_artifact_rejects_empty_artifact_id() { +async fn archive_and_unarchive_reject_an_empty_artifact_id() { let (server, _h) = server_with_mock(MockArtifactServiceImpl::new(), true).await; - let err = server - .unarchive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: " ".into(), - })) - .await - .expect_err("empty id"); - assert_eq!(err.code, ErrorCode::INVALID_PARAMS); -} - -#[tokio::test] -async fn archive_artifact_returns_structured_result() { - let mut mock = MockArtifactServiceImpl::new(); - mock.expect_archive_artifact() - .withf(|req| req.get_ref().artifact_id == "art-1") - .returning(|_| Ok(Response::new(ArchiveArtifactResponse {}))); - - let (server, _h) = server_with_mock(mock, true).await; - let response = server - .archive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: "art-1".into(), - })) - .await - .expect("archive"); - let body = structured(response); - assert_eq!(body["artifact_id"], "art-1"); - assert_eq!(body["archived"], true); - assert_eq!( - body["artifact_url"], - "https://app.test.local/artifacts/art-1" - ); - let next_step = body["next_step"].as_str().unwrap(); - assert!( - next_step.contains("https://app.test.local/artifacts/art-1"), - "{next_step}" - ); -} - -#[tokio::test] -async fn unarchive_artifact_returns_structured_result() { - let mut mock = MockArtifactServiceImpl::new(); - mock.expect_unarchive_artifact() - .withf(|req| req.get_ref().artifact_id == "art-1") - .returning(|_| Ok(Response::new(UnarchiveArtifactResponse {}))); - - let (server, _h) = server_with_mock(mock, true).await; - let response = server - .unarchive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: "art-1".into(), - })) - .await - .expect("unarchive"); - let body = structured(response); - assert_eq!(body["artifact_id"], "art-1"); - assert_eq!(body["unarchived"], true); - assert_eq!( - body["artifact_url"], - "https://app.test.local/artifacts/art-1" - ); - let next_step = body["next_step"].as_str().unwrap(); - assert!( - next_step.contains("https://app.test.local/artifacts/art-1"), - "{next_step}" - ); + for archive in [true, false] { + let err = set_archived(&server, archive, " ") + .await + .expect_err("empty id"); + assert_eq!(err.code, ErrorCode::INVALID_PARAMS); + } } #[tokio::test] -async fn archive_and_unarchive_trim_the_artifact_id() { +async fn archive_and_unarchive_trim_the_id_and_return_structured_results() { let mut mock = MockArtifactServiceImpl::new(); mock.expect_archive_artifact() .withf(|req| req.get_ref().artifact_id == "art-1") @@ -557,24 +488,15 @@ async fn archive_and_unarchive_trim_the_artifact_id() { .returning(|_| Ok(Response::new(UnarchiveArtifactResponse {}))); let (server, _h) = server_with_mock(mock, true).await; - let archived = server - .archive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: " art-1\n".into(), - })) - .await - .expect("archive"); - let unarchived = server - .unarchive_artifact_item(Parameters(ArtifactArchiveParams { - artifact_id: " art-1\n".into(), - })) - .await - .expect("unarchive"); - for body in [structured(archived), structured(unarchived)] { + for (archive, state) in [(true, "archived"), (false, "unarchived")] { + let body = first_result(set_archived(&server, archive, " art-1\n").await.unwrap()); assert_eq!(body["artifact_id"], "art-1"); + assert_eq!(body[state], true); assert_eq!( body["artifact_url"], "https://app.test.local/artifacts/art-1" ); + assert!(body.get("next_step").is_none(), "{body}"); } } @@ -598,12 +520,7 @@ async fn update_artifact_blocked_without_allow_destructive() { false, ) .await; - let err = server - .update_artifact_item(Parameters(UpdateArtifactParams { - artifact_id: "art-1".into(), - title: Some("v2".into()), - ..Default::default() - })) + let err = update_one(&server, json!({"artifact_id": "art-1", "title": "v2"})) .await .expect_err("update gated"); assert_eq!(err.code, ErrorCode::INVALID_REQUEST); @@ -643,35 +560,25 @@ async fn update_artifact_masks_only_what_was_passed() { }); let (server, _h) = server_with_mock(mock, true).await; - let resp = server - .update_artifact_item(Parameters(UpdateArtifactParams { - artifact_id: "art-1".into(), - title: Some("Renamed".into()), - ..Default::default() - })) - .await - .expect("update"); - let artifact = structured_field(resp.clone(), "artifact"); - assert_eq!(artifact["version"], 2); - assert_eq!(artifact["title"], "Renamed"); + let body = first_result( + update_one(&server, json!({"artifact_id": "art-1", "title": "Renamed"})) + .await + .expect("update"), + ); + assert_eq!(body["artifact"]["version"], 2); + assert_eq!(body["artifact"]["title"], "Renamed"); + assert_eq!(body["name"], "Renamed"); assert_eq!( - structured_field(resp.clone(), "artifact_url"), + body["artifact_url"], "https://app.test.local/artifacts/art-1" ); - let next_step = structured_field(resp, "next_step"); - let next_step = next_step.as_str().unwrap(); - assert!( - next_step.starts_with("Wrote version 2 of artifact art-1"), - "{next_step}" - ); - assert!( - next_step.contains("[Renamed](https://app.test.local/artifacts/art-1)"), - "{next_step}" - ); + // The chat host relays artifacts by matching this text. + assert_eq!(body["next_step"], "Wrote version 2 of artifact art-1."); + assert!(body.get("warning").is_none(), "{body}"); } #[tokio::test] -async fn update_artifact_without_a_file_reports_the_kept_file() { +async fn update_artifact_without_a_file_keeps_the_previous_file_and_link() { let mut mock = MockArtifactServiceImpl::new(); mock.expect_update_artifact().returning(|_| { Ok(Response::new(UpdateArtifactResponse { @@ -695,65 +602,64 @@ async fn update_artifact_without_a_file_reports_the_kept_file() { }); let (server, _h) = server_with_mocks(mock, remote_files, true, true).await; - let resp = server - .update_artifact_item(Parameters(UpdateArtifactParams { - artifact_id: "art-1".into(), - title: Some("Renamed".into()), - ..Default::default() + let body = first_result( + update_one(&server, json!({"artifact_id": "art-1", "title": "Renamed"})) + .await + .expect("update"), + ); + assert_eq!( + body["artifact"]["download_url"], + "https://files.test.local/rf-1" + ); + assert!(body.get("warning").is_none(), "{body}"); +} + +#[tokio::test] +async fn update_artifact_warns_when_a_kept_file_has_no_download_link() { + let mut mock = MockArtifactServiceImpl::new(); + mock.expect_update_artifact().returning(|_| { + Ok(Response::new(UpdateArtifactResponse { + artifact: Some(sample_with( + |_| {}, + |version| version.remote_file_id = Some("rf-1".into()), + )), })) - .await - .expect("update"); - let artifact = structured_field(resp.clone(), "artifact"); - assert_eq!(artifact["download_url"], "https://files.test.local/rf-1"); - let next_step = structured_field(resp, "next_step"); - let next_step = next_step.as_str().unwrap(); - assert!( - next_step.contains("keeps the previous version's file"), - "{next_step}" + }); + let mut remote_files = MockRemoteFileServiceImpl::new(); + remote_files + .expect_get_remote_file_download_url() + .returning(|_| Err(Status::unavailable("no link"))); + + let (server, _h) = server_with_mocks(mock, remote_files, true, true).await; + let body = first_result( + update_one(&server, json!({"artifact_id": "art-1", "title": "Renamed"})) + .await + .expect("update"), ); - assert!(!next_step.contains("no file content"), "{next_step}"); + let warning = body["warning"].as_str().expect("warning"); + assert!(warning.contains("call `download_artifact`"), "{warning}"); } #[tokio::test] async fn update_artifact_maps_each_parameter_to_its_own_path() { - for (params, want) in [ + for (item, want) in [ ( - UpdateArtifactParams { - artifact_id: "art-1".into(), - title: Some("t".into()), - ..Default::default() - }, + json!({"artifact_id": "art-1", "title": "t"}), "artifact_version.title", ), ( - UpdateArtifactParams { - artifact_id: "art-1".into(), - summary: Some("s".into()), - ..Default::default() - }, + json!({"artifact_id": "art-1", "summary": "s"}), "artifact_version.summary", ), ( - UpdateArtifactParams { - artifact_id: "art-1".into(), - metadata: Some(vec![MetadataEntry { - name: "k".into(), - value: MetadataScalar::String("v".into()), - }]), - ..Default::default() - }, + json!({"artifact_id": "art-1", "metadata": [{"name": "k", "value": "v"}]}), "artifact_version.metadata", ), ( - UpdateArtifactParams { - artifact_id: "art-1".into(), - links: Some(vec![super::ArtifactLinkParam { - relation: "source".into(), - entity_type: "run".into(), - entity_id: "run-1".into(), - }]), - ..Default::default() - }, + json!({ + "artifact_id": "art-1", + "links": [{"relation": "source", "entity_type": "run", "entity_id": "run-1"}], + }), "links", ), ] { @@ -766,31 +672,22 @@ async fn update_artifact_maps_each_parameter_to_its_own_path() { })) }); let (server, _h) = server_with_mock(mock, true).await; - server - .update_artifact_item(Parameters(params)) - .await - .unwrap_or_else(|err| panic!("{want}: {err:?}")); + first_result( + update_one(&server, item) + .await + .unwrap_or_else(|err| panic!("{want}: {err:?}")), + ); } } #[tokio::test] async fn update_artifact_requires_an_id_and_something_to_change() { let (server, _h) = server_with_mock(MockArtifactServiceImpl::new(), true).await; - for params in [ - UpdateArtifactParams { - artifact_id: " ".into(), - title: Some("Renamed".into()), - ..Default::default() - }, - UpdateArtifactParams { - artifact_id: "art-1".into(), - ..Default::default() - }, + for item in [ + json!({"artifact_id": " ", "title": "Renamed"}), + json!({"artifact_id": "art-1"}), ] { - let err = server - .update_artifact_item(Parameters(params)) - .await - .expect_err("rejected"); + let err = update_one(&server, item).await.expect_err("rejected"); assert_eq!(err.code, ErrorCode::INVALID_PARAMS); } } @@ -918,11 +815,9 @@ async fn create_artifact_happy_path() { body["artifact_url"], "https://app.test.local/artifacts/art-1" ); - let next_step = body["next_step"].as_str().unwrap(); - assert!( - next_step.contains("[report](https://app.test.local/artifacts/art-1)"), - "{next_step}" - ); + assert_eq!(body["name"], "report"); + // The chat host relays artifacts by matching this text. + assert_eq!(body["next_step"], "Created artifact art-1 version 1."); } #[tokio::test] @@ -998,14 +893,7 @@ async fn create_artifact_with_file_path_uploads_and_returns_the_refreshed_artifa assert_eq!(artifact["remoteFileId"], "rf-1"); assert_eq!(artifact["fileName"], "report.md"); assert_eq!(artifact["download_url"], "https://files.test.local/rf-1"); - let next_step = structured_field(resp, "next_step"); - assert!( - next_step - .as_str() - .unwrap() - .contains("file content was uploaded"), - "{next_step}" - ); + assert!(structured(resp).get("warning").is_none()); } #[tokio::test] @@ -1127,14 +1015,12 @@ async fn create_artifact_with_file_path_says_so_when_the_download_link_is_missin let artifact = structured_field(resp.clone(), "artifact"); assert!(artifact.get("download_url").is_none()); - let next_step = structured_field(resp, "next_step"); - let next_step = next_step.as_str().unwrap(); + let warning = structured_field(resp, "warning"); assert!( - next_step.contains("call `download_artifact`"), - "{next_step}" - ); - assert!( - !next_step.contains("can preview and download"), - "{next_step}" + warning + .as_str() + .unwrap() + .contains("call `download_artifact`"), + "{warning}" ); } diff --git a/rust/crates/sift_mcp/src/tool/assets/mod.rs b/rust/crates/sift_mcp/src/tool/assets/mod.rs index 213650196..87a6e0316 100644 --- a/rust/crates/sift_mcp/src/tool/assets/mod.rs +++ b/rust/crates/sift_mcp/src/tool/assets/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -13,8 +13,8 @@ use crate::{ server::SiftMcpServer, tool::common::{ ListParams, MetadataEntry, - bulk::{self, BulkItems, Op}, - list_body, url_clause, with_urls, + bulk::{BulkItems, Op}, + list_body, require_non_empty, with_urls, }, }; @@ -29,12 +29,7 @@ pub struct UpdateAssetItem { mod test; fn validate_update_asset(item: &UpdateAssetItem) -> Result<(), ErrorData> { - if item.asset_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`asset_id` must not be empty", - None, - )); - } + require_non_empty("asset_id", &item.asset_id)?; if item.tags.is_none() && item.metadata.is_none() { return Err(ErrorData::invalid_params( "at least one of `tags` or `metadata` must be provided", @@ -128,14 +123,12 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, not_attempted, next_step }`. Each success holds `{ index, result }`, each failure holds `{ index, error, outcome_unknown }`, and any failure sets `isError`. Each result is: - - `{ \"asset\": Asset, \"asset_url\": string|null, \"next_step\": string }`. The returned `Asset` is + - `{ \"asset\": Asset, \"asset_url\": string|null }`. The returned `Asset` is the post-update state from the server; `asset_url` is its Sift web link (`/asset/`), or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per asset. Each item takes the fields below. - - `selected_indices`: zero-based item positions supplied by the approval host; omit when - proposing. Omitted selects all outside chat; chat requires a mask. `[]` updates nothing. + - `items`: one object per asset. Each item takes the fields below. - `asset_id`: required; the id of the asset to update. - `tags`: optional; REPLACES the asset's full tag list with this array. To add a single tag, first read the current tags via `list_assets` filtered by @@ -182,38 +175,26 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - let items = bulk::prepare( - items, + self.bulk_write( + Op::Update, + params.0, "asset", |item: UpdateAssetItem| { validate_update_asset(&item)?; Ok(item) }, |item| Some(item.asset_id.clone()), - )?; - bulk::run( - Op::Update, - items, - self.selection(selected_indices), - false, - |item| self.update_asset_item(Parameters(item)), + |item| self.update_asset_item(item), ) .await } - async fn update_asset_item(&self, params: Parameters) -> error::McpResult { - self.require_destructive()?; - validate_update_asset(¶ms.0)?; - let Parameters(UpdateAssetItem { + async fn update_asset_item(&self, item: UpdateAssetItem) -> error::McpResult { + let UpdateAssetItem { asset_id, tags, metadata, - }) = params; + } = item; let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); @@ -224,19 +205,9 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let asset_url = self.url_service.build_asset_url(&asset.asset_id).ok(); - let next_step = format!( - "Updated asset `{}` ({}).{}", - asset.name, - asset.asset_id, - url_clause("asset", Some(&asset.name), asset_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "asset": asset, "asset_url": asset_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } } diff --git a/rust/crates/sift_mcp/src/tool/assets/test.rs b/rust/crates/sift_mcp/src/tool/assets/test.rs index 08e6a6313..3c17f11a1 100644 --- a/rust/crates/sift_mcp/src/tool/assets/test.rs +++ b/rust/crates/sift_mcp/src/tool/assets/test.rs @@ -10,10 +10,7 @@ use tonic::{Response, Status, transport::Server}; use crate::{ server::SiftMcpServer, service::common::DEFAULT_LIMIT, - tool::{ - assets::UpdateAssetItem as UpdateAssetParams, - common::test_support::{list_params, structured_field}, - }, + tool::common::test_support::{bulk_params, list_params, structured_field}, }; async fn server_with_mock(mock: MockAssetServiceImpl) -> (SiftMcpServer, JoinHandle<()>) { @@ -245,11 +242,10 @@ async fn update_asset_blocked_without_allow_destructive() { let (server, _h) = server_with_mock_and_flag(mock, false).await; let err = server - .update_asset_item(Parameters(UpdateAssetParams { - asset_id: "a1".into(), - tags: Some(vec!["engine".into()]), - metadata: None, - })) + .update_asset(bulk_params( + serde_json::json!([{"asset_id": "a1", "tags": ["engine"]}]), + None, + )) .await .expect_err("expected destructive gate to reject the call"); diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs index 176bee071..4c9389425 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -10,17 +10,17 @@ use sift_rs::{ calculated_channels::v2::CalculatedChannelAbstractChannelReference, metadata::v1::MetadataValue, }; -use crate::tool::common::{ - bulk::{self, BulkItems, Op}, - bulk_create, -}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, service::calculated_channels::{ CalculatedChannelUpdate, CalculatedChannelWrite, NewCalculatedChannel, }, - tool::common::{ListParams, MetadataEntry, list_body, to_values}, + tool::common::{ + ListParams, MetadataEntry, + bulk::{BulkItems, Op}, + list_body, require_non_empty, to_values, + }, }; #[cfg(test)] @@ -228,7 +228,7 @@ impl SiftMcpServer { fields, }) = params; - require_id(&calculated_channel_id)?; + require_non_empty("calculated_channel_id", &calculated_channel_id)?; let page = self .calculated_channel_service @@ -267,14 +267,13 @@ impl SiftMcpServer { outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - `{ \"calculated_channel\": CalculatedChannel, \"inapplicable_assets\": [...], - \"next_step\": string }`. `inapplicable_assets` lists in-scope assets that do NOT have every + \"warning\"?: string }`. `inapplicable_assets` lists in-scope assets that do NOT have every channel the expression references; each entry carries `asset_id`, `asset_name`, `tag_names`, and `missing_channels`. A non-empty list means the channel was created but will not evaluate on those - assets. + assets, and `warning` says so. Parameters: - - `items`: required array of 1–100 objects, one per calculated channel. Pass one item to create one - calculated channel. Each item takes: + - `items`: one object per calculated channel. Each item takes: - `name`: required. The calculated channel's name. - `expression`: required. A SEL expression whose channel operands are placeholders (`$1`, `$2`, …) resolved by `expression_channel_references_json`. Example: `\"$1 - $2\"`. @@ -293,9 +292,6 @@ impl SiftMcpServer { - Exactly one scope form is allowed: either `all_assets: true`, or a non-empty `asset_ids` / `tag_ids` selection. Setting both is rejected. - `metadata`: optional. Array of `{ \"name\": \"\", \"value\": }` entries. - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Errors: - `INVALID_PARAMS` if `name` or `expression` is empty, `expression_channel_references_json` is not a @@ -330,18 +326,17 @@ impl SiftMcpServer { )] pub async fn create_calculated_channel( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(bulk_create::CreateItems { - items, - selected_indices, - .. - }) = params; - let items = bulk_create::parse_items::(items)?; - bulk_create::create_many(items, self.selection(selected_indices), |item| { - self.create_calculated_channel_item(Parameters(item)) - }) + self.bulk_write( + Op::Create, + params.0, + "calculated channel", + Ok, + |_| None, + |item| self.create_calculated_channel_item(Parameters(item)), + ) .await } @@ -408,17 +403,7 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let next_step = format!( - "Created calculated channel `{}` (`{}`).{} Tell the user the new id. If they haven't \ - indicated a next step, offer to confirm it with `list_calculated_channels` \ - (filter `calculated_channel_id == \"{}\"`).", - written.calculated_channel.name, - written.calculated_channel.calculated_channel_id, - inapplicable_clause(&written), - written.calculated_channel.calculated_channel_id, - ); - - Ok(write_result(written, next_step, None)) + Ok(write_result(written)) } #[tool( @@ -433,14 +418,12 @@ impl SiftMcpServer { not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is: `{ \"calculated_channel\": CalculatedChannel, \"inapplicable_assets\": [...], - \"next_step\": string }`. The returned channel is the new version's post-update state. - `inapplicable_assets` lists in-scope assets missing a channel the expression references. + \"warning\"?: string }`. The returned channel is the new version's post-update state. + `inapplicable_assets` lists in-scope assets missing a channel the expression references; + `warning` appears when it is non-empty. Parameters: - - `items`: required array of 1–100 objects, one per calculated channel, with the fields below. - Pass one item to update one channel. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: one object per calculated channel, with the fields below. - `calculated_channel_id`: required in each item. The calculated channel to update. - `name`: optional. New name. - `description`: optional. New description. @@ -490,45 +473,24 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(params) = params; - let updates = bulk::prepare( - params.items, + self.bulk_write( + Op::Update, + params.0, "calculated channel", calculated_channel_update, |(id, _)| Some(id.clone()), - )?; - bulk::run( - Op::Update, - updates, - self.selection(params.selected_indices), - false, - |(id, changes)| self.update_calculated_channel_item(id, changes), + |(id, changes)| async move { + let written = self + .calculated_channel_service + .update_calculated_channel(id, changes) + .await + .map_err(from_anyhow)?; + Ok(write_result(written)) + }, ) .await } - async fn update_calculated_channel_item( - &self, - calculated_channel_id: String, - changes: CalculatedChannelUpdate, - ) -> error::McpResult { - let written = self - .calculated_channel_service - .update_calculated_channel(calculated_channel_id, changes) - .await - .map_err(from_anyhow)?; - - let next_step = format!( - "Updated calculated channel `{}` to version {}.{}", - written.calculated_channel.calculated_channel_id, - written.calculated_channel.version, - inapplicable_clause(&written), - ); - - Ok(write_result(written, next_step, None)) - } - #[tool( name = "archive_calculated_channel", description = " @@ -539,13 +501,11 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"archived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. - `archived` reflects the returned channel state. + `{ \"archived\": boolean, \"calculated_channel\": CalculatedChannel, \"warning\"?: string }`. + `archived` reflects the returned channel state; `warning` appears when the server did not archive it. Parameters: - - `items`: required array of 1–100 objects, each with `calculated_channel_id`, the id to archive. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: each item takes `calculated_channel_id`, the id to archive. Errors: - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, @@ -573,39 +533,8 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - self.bulk_calculated_channel_archive(params, Op::Archive) - .await - } - - async fn archive_calculated_channel_item( - &self, - calculated_channel_id: String, - ) -> error::McpResult { - let written = self - .calculated_channel_service - .archive_calculated_channel(calculated_channel_id) + self.set_calculated_channels_archived(params, Op::Archive) .await - .map_err(from_anyhow)?; - - let archived = written.calculated_channel.is_archived; - let next_step = if archived { - format!( - "Archived calculated channel `{}`.", - written.calculated_channel.calculated_channel_id, - ) - } else { - format!( - "The server returned calculated channel `{}` as unarchived after the archive request. \ - Do not report it as archived; verify its state before continuing.", - written.calculated_channel.calculated_channel_id, - ) - }; - - Ok(write_result( - written, - next_step, - Some(("archived", archived)), - )) } #[tool( @@ -617,13 +546,11 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"unarchived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. - `unarchived` reflects the returned channel state. + `{ \"unarchived\": boolean, \"calculated_channel\": CalculatedChannel, \"warning\"?: string }`. + `unarchived` reflects the returned channel state; `warning` appears when the server did not restore it. Parameters: - - `items`: required array of 1–100 objects, each with `calculated_channel_id`, the id to restore. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: each item takes `calculated_channel_id`, the id to restore. Errors: - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, @@ -649,72 +576,58 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - self.bulk_calculated_channel_archive(params, Op::Unarchive) + self.set_calculated_channels_archived(params, Op::Unarchive) .await } - async fn bulk_calculated_channel_archive( + async fn set_calculated_channels_archived( &self, params: Parameters>, op: Op, ) -> error::McpResult { - let Parameters(params) = params; - let items = bulk::prepare( - params.items, + self.bulk_write( + op, + params.0, "calculated channel", |item: CalculatedChannelArchiveItem| { - require_id(&item.calculated_channel_id)?; - Ok(item) - }, - |item| Some(item.calculated_channel_id.clone()), - )?; - bulk::run( - op, - items, - self.selection(params.selected_indices), - false, - |item| async move { - if op == Op::Archive { - self.archive_calculated_channel_item(item.calculated_channel_id) - .await - } else { - self.unarchive_calculated_channel_item(item.calculated_channel_id) - .await - } + require_non_empty("calculated_channel_id", &item.calculated_channel_id)?; + Ok(item.calculated_channel_id) }, + |id| Some(id.clone()), + |id| self.set_calculated_channel_archived(id, op), ) .await } - async fn unarchive_calculated_channel_item( - &self, - calculated_channel_id: String, - ) -> error::McpResult { - let written = self - .calculated_channel_service - .unarchive_calculated_channel(calculated_channel_id) - .await - .map_err(from_anyhow)?; - - let unarchived = !written.calculated_channel.is_archived; - let next_step = if unarchived { - format!( - "Unarchived calculated channel `{}`.", - written.calculated_channel.calculated_channel_id, - ) + async fn set_calculated_channel_archived(&self, id: String, op: Op) -> error::McpResult { + let archive = op == Op::Archive; + let written = if archive { + self.calculated_channel_service + .archive_calculated_channel(id) + .await } else { - format!( - "The server returned calculated channel `{}` as archived after the unarchive request. \ - Do not report it as restored; verify its state before continuing.", - written.calculated_channel.calculated_channel_id, + self.calculated_channel_service + .unarchive_calculated_channel(id) + .await + } + .map_err(from_anyhow)?; + + let channel = written.calculated_channel; + let applied = channel.is_archived == archive; + let mut body = serde_json::json!({ + op.state_key(): applied, + "calculated_channel": channel, + }); + if !applied { + body["warning"] = format!( + "The server returned this calculated channel with `is_archived: {}` after the \ + request. Do not report it as {}; verify its state before continuing.", + channel.is_archived, + op.state_key(), ) - }; - - Ok(write_result( - written, - next_step, - Some(("unarchived", unarchived)), - )) + .into(); + } + Ok(CallToolResult::structured(body)) } } @@ -734,7 +647,7 @@ fn calculated_channel_update( metadata, user_notes, } = item; - require_id(&calculated_channel_id)?; + require_non_empty("calculated_channel_id", &calculated_channel_id)?; if expression.is_some() != expression_channel_references_json.is_some() { return Err(ErrorData::invalid_params( "`expression` and `expression_channel_references_json` must be provided together", @@ -772,17 +685,6 @@ fn calculated_channel_update( Ok((calculated_channel_id, changes)) } -/// Reject an empty `calculated_channel_id` before any RPC. -fn require_id(calculated_channel_id: &str) -> Result<(), ErrorData> { - if calculated_channel_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`calculated_channel_id` must not be empty", - None, - )); - } - Ok(()) -} - /// Deserialize the JSON-string channel reference array, mapping any parse error /// to `INVALID_PARAMS` so the agent can correct it. Nested channel references /// are the one irreducibly nested input on these tools. @@ -825,41 +727,20 @@ fn metadata_values(metadata: Option>) -> Vec { .collect() } -/// A trailing `next_step` clause naming the assets the API reported the channel -/// cannot evaluate on. Empty when every in-scope asset applies. -fn inapplicable_clause(written: &CalculatedChannelWrite) -> String { - if written.inapplicable_assets.is_empty() { - return String::new(); - } - format!( - " {} in-scope asset(s) are missing a referenced channel and will not evaluate this \ - calculated channel; see `inapplicable_assets`.", - written.inapplicable_assets.len() - ) -} - -/// Shape a write result: the stored channel, the inapplicable assets when the -/// API reported any, `next_step` on both the structured body and the content -/// block, plus an optional state flag (`archived` / `unarchived`). -fn write_result( - written: CalculatedChannelWrite, - next_step: String, - flag: Option<(&str, bool)>, -) -> CallToolResult { +/// Shape a create or update result: the stored channel, the assets the API reported +/// it will not evaluate on, and a `warning` naming them when there are any. +fn write_result(written: CalculatedChannelWrite) -> CallToolResult { let mut body = serde_json::json!({ "calculated_channel": written.calculated_channel, - "next_step": next_step.clone(), + "inapplicable_assets": written.inapplicable_assets, }); - match flag { - Some((key, value)) => { - body[key] = serde_json::json!(value); - } - None => { - body["inapplicable_assets"] = serde_json::json!(written.inapplicable_assets); - } + if !written.inapplicable_assets.is_empty() { + body["warning"] = format!( + "{} in-scope asset(s) are missing a referenced channel and will not evaluate this \ + calculated channel; see `inapplicable_assets`.", + written.inapplicable_assets.len() + ) + .into(); } - - let mut result = CallToolResult::structured(body); - result.content = vec![ContentBlock::text(next_step)]; - result + CallToolResult::structured(body) } diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs index bd377e554..17e6d5145 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs @@ -17,7 +17,8 @@ use super::{ use crate::{ server::SiftMcpServer, tool::common::test_support::{ - assert_keys_before, content_texts, list_params, list_params_with_fields, structured, + assert_keys_before, bulk_params, content_texts, list_params, list_params_with_fields, + structured, }, }; @@ -256,10 +257,7 @@ fn update_call( } fn archive_call(id: &str) -> Parameters> { - Parameters( - serde_json::from_value(serde_json::json!({"items": [{"calculated_channel_id": id}]})) - .unwrap(), - ) + bulk_params(serde_json::json!([{ "calculated_channel_id": id }]), None) } fn update_params() -> UpdateCalculatedChannelItem { @@ -526,7 +524,12 @@ async fn create_calculated_channel_returns_structured_result() { .len(), 1 ); - assert!(body["next_step"].is_string()); + assert!( + body["warning"] + .as_str() + .expect("warning") + .contains("1 in-scope asset(s)") + ); } #[tokio::test] @@ -740,7 +743,7 @@ async fn update_calculated_channel_returns_structured_result() { let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["calculated_channel"]["name"], "renamed"); assert_eq!(body["calculated_channel"]["version"], 4); - assert!(body["next_step"].is_string()); + assert!(body.get("next_step").is_none(), "{body}"); } #[tokio::test] @@ -802,7 +805,7 @@ async fn archive_calculated_channel_returns_structured_result() { let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["archived"], true); assert_eq!(body["calculated_channel"]["calculatedChannelId"], "cc1"); - assert!(body["next_step"].is_string()); + assert!(body.get("warning").is_none(), "{body}"); } #[tokio::test] @@ -835,7 +838,7 @@ async fn unarchive_calculated_channel_returns_structured_result() { let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["unarchived"], true); - assert!(body["next_step"].is_string()); + assert!(body.get("warning").is_none(), "{body}"); } #[tokio::test] @@ -862,7 +865,12 @@ async fn archive_calculated_channel_surfaces_a_returned_unarchived_state() { let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["archived"], false); - assert!(body["next_step"].as_str().unwrap().contains("unarchived")); + let warning = body["warning"].as_str().expect("warning"); + assert!(warning.contains("`is_archived: false`"), "{warning}"); + assert!( + warning.contains("Do not report it as archived"), + "{warning}" + ); } #[tokio::test] @@ -889,7 +897,12 @@ async fn unarchive_calculated_channel_surfaces_a_returned_archived_state() { let body = structured(resp)["successes"][0]["result"].clone(); assert_eq!(body["unarchived"], false); - assert!(body["next_step"].as_str().unwrap().contains("archived")); + let warning = body["warning"].as_str().expect("warning"); + assert!(warning.contains("`is_archived: true`"), "{warning}"); + assert!( + warning.contains("Do not report it as unarchived"), + "{warning}" + ); } #[tokio::test] diff --git a/rust/crates/sift_mcp/src/tool/common/bulk.rs b/rust/crates/sift_mcp/src/tool/common/bulk.rs index 7d98f0900..2d24a2211 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -9,7 +9,7 @@ use rmcp::{ use serde::{Deserialize, Deserializer, de::DeserializeOwned}; use serde_json::{Map, Value, json}; -use crate::{error::McpResult, service::batch}; +use crate::{error::McpResult, server::SiftMcpServer, service::batch}; pub const MAX_ITEMS: usize = 100; @@ -20,8 +20,8 @@ pub struct BulkItems { #[schemars(length(min = 1, max = 100))] #[schemars(with = "Vec")] pub items: Vec, - /// Approval host's zero-based selection of `items` to write. Omit when proposing. - /// An empty selection performs no writes. + /// Approval host's zero-based selection of `items` to write. Omit when proposing; + /// omitted means every item. An empty selection performs no writes. pub selected_indices: Option>, #[schemars(skip)] _item: std::marker::PhantomData, @@ -165,6 +165,45 @@ impl Op { Self::Unarchive => "unarchived_count", } } + + /// The key an archive or unarchive result sets to `true`: `archived` or `unarchived`. + pub fn state_key(self) -> &'static str { + match self { + Self::Create => "created", + Self::Update => "updated", + Self::Archive => "archived", + Self::Unarchive => "unarchived", + } + } +} + +impl SiftMcpServer { + /// Validate every item, reject duplicate targets, then write the selected items one at a + /// time with `write`. The tool handler still gates itself before calling this. + pub(crate) async fn bulk_write( + &self, + op: Op, + params: BulkItems, + resource: &str, + validate: impl Fn(T) -> Result, + target: impl Fn(&U) -> Option, + write: F, + ) -> McpResult + where + T: DeserializeOwned + JsonSchema, + F: FnMut(U) -> Fut, + Fut: Future, + { + let items = prepare(params.items, resource, validate, target)?; + run( + op, + items, + self.selection(params.selected_indices), + false, + write, + ) + .await + } } fn reason(error: &ErrorData) -> Option<&str> { @@ -300,7 +339,10 @@ pub fn report( let next_step = format!( "{} {} items; {} failed; {} not attempted. \ On success, confirm in one short sentence using resource names or links; omit unrequested metadata, \ - internal indices/masks, and approval/retry narration. Report actionable warnings and failures. \ + internal indices/masks, and approval/retry narration. Write each link as a Markdown link whose \ + text is the resource's name and whose target is its `*_url`, never a bare id. Backslash-escape \ + any `[`, `]` or `\\` in the name so the link renders. Report every \ + result's `warning` and every failure. \ Never replay the batch: successful writes are committed, and the user rejected the items in \ `not_selected`, so never retry those. `outcome_unknown` items may also have been written or \ partially written. Inspect uncertain outcomes before retrying, follow each error's guidance, \ diff --git a/rust/crates/sift_mcp/src/tool/common/bulk_create.rs b/rust/crates/sift_mcp/src/tool/common/bulk_create.rs index f61e7adae..b1f6364ea 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk_create.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk_create.rs @@ -2,7 +2,7 @@ use std::future::Future; use crate::error::McpResult; -pub use super::bulk::{BulkItems as CreateItems, Selection, at_item, parse_items}; +pub use super::bulk::{BulkItems as CreateItems, Selection, parse_items}; use super::bulk::{Op, run}; pub async fn create_many(items: Vec, selection: Selection, operation: F) -> McpResult @@ -201,6 +201,18 @@ mod test { "omit unrequested metadata, internal indices/masks, and approval/retry narration" )); assert!(!guidance.contains("Present each indexed result")); + assert!( + guidance.contains("whose target is its `*_url`"), + "{guidance}" + ); + assert!( + guidance.contains("Backslash-escape any `[`, `]` or `\\` in the name"), + "{guidance}" + ); + assert!( + guidance.contains("Report every result's `warning`"), + "{guidance}" + ); assert_eq!(body["successes"][0]["index"], 0); assert_eq!(body["successes"][1]["index"], 2); assert_eq!(body["not_selected"], json!([1])); diff --git a/rust/crates/sift_mcp/src/tool/common/mod.rs b/rust/crates/sift_mcp/src/tool/common/mod.rs index 630ca5cf1..567bcba97 100644 --- a/rust/crates/sift_mcp/src/tool/common/mod.rs +++ b/rust/crates/sift_mcp/src/tool/common/mod.rs @@ -48,6 +48,38 @@ pub struct MetadataEntry { pub(crate) value: MetadataScalar, } +/// Reject a blank required id or name with `INVALID_PARAMS` naming the field. +pub(crate) fn require_non_empty(field: &str, value: &str) -> Result<(), ErrorData> { + if value.trim().is_empty() { + return Err(ErrorData::invalid_params( + format!("`{field}` must not be empty"), + None, + )); + } + Ok(()) +} + +/// Resolve two mutually exclusive optional params into the `(first, second)` request +/// fields, with an empty string for the unset one. Returns `INVALID_PARAMS` unless +/// exactly one is set, since the API would silently pick one for the caller. +pub(crate) fn exactly_one( + (first_name, first): (&str, Option), + (second_name, second): (&str, Option), +) -> Result<(String, String), ErrorData> { + match (first, second) { + (Some(first), None) => Ok((first, String::new())), + (None, Some(second)) => Ok((String::new(), second)), + (Some(_), Some(_)) => Err(ErrorData::invalid_params( + format!("exactly one of `{first_name}` or `{second_name}` must be set, not both"), + None, + )), + (None, None) => Err(ErrorData::invalid_params( + format!("one of `{first_name}` or `{second_name}` must be set"), + None, + )), + } +} + /// A trailing clause for a tool's `next_step` that tells the model how to /// present an entity's Sift web link. The Sift chat renders a Markdown link as /// a chip whose text must be the entity's own name: `[name](url)`. When the @@ -540,6 +572,30 @@ pub(crate) mod test_support { } } + /// Build a bulk write tool's params from a JSON `items` array and an optional mask. + pub(crate) fn bulk_params( + items: Value, + selected_indices: Option>, + ) -> Parameters> { + Parameters( + serde_json::from_value( + serde_json::json!({"items": items, "selected_indices": selected_indices}), + ) + .expect("valid bulk params"), + ) + } + + /// The `result` of the first success in a bulk write. Panics when any item failed. + pub(crate) fn first_result(result: CallToolResult) -> Value { + let body = structured(result); + assert_eq!( + body["failures"], + serde_json::json!([]), + "expected no failures in {body}" + ); + body["successes"][0]["result"].clone() + } + /// Extract a single field from a tool result's structured JSON body. pub(crate) fn structured_field(result: CallToolResult, key: &str) -> Value { let mut value = structured(result); diff --git a/rust/crates/sift_mcp/src/tool/common/test.rs b/rust/crates/sift_mcp/src/tool/common/test.rs index f1663e385..8d1244c56 100644 --- a/rust/crates/sift_mcp/src/tool/common/test.rs +++ b/rust/crates/sift_mcp/src/tool/common/test.rs @@ -1,5 +1,6 @@ use super::{ - ListParams, PendingOutput, list_body, project_fields, read_result, + ListParams, PendingOutput, exactly_one, list_body, project_fields, read_result, + require_non_empty, test_support::{assert_keys_before, content_texts, dir_entries}, url_clause, }; @@ -7,6 +8,38 @@ use serde_json::{Value, json}; use std::{io::Write, path::PathBuf}; use tempdir::TempDir; +#[test] +fn exactly_one_returns_the_set_field_and_rejects_both_or_neither() { + let one = |a: Option<&str>, b: Option<&str>| { + exactly_one( + ("rule_id", a.map(Into::into)), + ("client_key", b.map(Into::into)), + ) + }; + assert_eq!(one(Some("r"), None).unwrap(), ("r".into(), String::new())); + assert_eq!(one(None, Some("c")).unwrap(), (String::new(), "c".into())); + let both = one(Some("r"), Some("c")).unwrap_err(); + assert_eq!(both.code, rmcp::model::ErrorCode::INVALID_PARAMS); + assert!(both.message.contains("not both"), "{}", both.message); + let neither = one(None, None).unwrap_err(); + assert_eq!(neither.code, rmcp::model::ErrorCode::INVALID_PARAMS); + assert!( + neither.message.contains("`rule_id` or `client_key`"), + "{}", + neither.message + ); +} + +#[test] +fn require_non_empty_names_the_blank_field() { + assert!(require_non_empty("rule_id", "rule-1").is_ok()); + for blank in ["", " \n"] { + let error = require_non_empty("rule_id", blank).unwrap_err(); + assert_eq!(error.code, rmcp::model::ErrorCode::INVALID_PARAMS); + assert_eq!(error.message, "`rule_id` must not be empty"); + } +} + /// `filter` is deliberately required on list tools: the caller must choose a /// scope explicitly, with the empty string as the documented way to request /// everything. Loosening this to `Option` would let unfiltered queries fire diff --git a/rust/crates/sift_mcp/src/tool/report_templates/mod.rs b/rust/crates/sift_mcp/src/tool/report_templates/mod.rs index 238358d6d..80ea86ad0 100644 --- a/rust/crates/sift_mcp/src/tool/report_templates/mod.rs +++ b/rust/crates/sift_mcp/src/tool/report_templates/mod.rs @@ -1,22 +1,22 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; use serde::Deserialize; use sift_rs::metadata::v1::MetadataValue; -use crate::tool::common::{ - bulk::{self, BulkItems, Op}, - bulk_create, -}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, service::report_templates::{ReportTemplateUpdate, TemplateRuleIdentifier}, - tool::common::{MetadataEntry, url_clause, with_urls}, + tool::common::{ + MetadataEntry, + bulk::{BulkItems, Op}, + require_non_empty, with_urls, + }, }; #[cfg(test)] @@ -67,12 +67,7 @@ fn template_update( metadata, is_archived, } = item; - if report_template_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`report_template_id` must not be empty", - None, - )); - } + require_non_empty("report_template_id", &report_template_id)?; let rules = match (rule_ids, rule_client_keys) { (None, None) => None, (Some(ids), None) => Some(TemplateRuleIdentifier::RuleIds(ids)), @@ -198,15 +193,14 @@ impl SiftMcpServer { `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - - `{ \"report_template\": ReportTemplate, \"report_template_url\": string|null, \"next_step\": string }`. + - `{ \"report_template\": ReportTemplate, \"report_template_url\": string|null }`. The returned template is the server-assigned state including its new `report_template_id`, resolved `rules` (with `display_order` set), and timestamps. `report_template_url` is the template's Sift web link, or null when the host can't be derived. Link the template only to this URL, never to a report's page. Parameters: - - `items`: required array of 1–100 objects, one per report template. Pass one item to create one - report template. Each item takes: + - `items`: one object per report template. Each item takes: - `name`: required; the template name. - `description`: optional; free-form description of what the template evaluates. - `client_key`: optional; a stable caller-defined key for referring to this template. Must be unique @@ -220,9 +214,6 @@ impl SiftMcpServer { on the template (first = 0, second = 1, ...). - `rule_client_keys`: list of rule client keys. Server resolves keys to rules; the resulting rule order on the template is server-defined (not the position in this list). - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Errors: - `INVALID_PARAMS` if `name` is empty, or if zero or more than one of `rule_ids`/`rule_client_keys` @@ -252,18 +243,17 @@ impl SiftMcpServer { )] pub async fn create_report_template( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(bulk_create::CreateItems { - items, - selected_indices, - .. - }) = params; - let items = bulk_create::parse_items::(items)?; - bulk_create::create_many(items, self.selection(selected_indices), |item| { - self.create_report_template_item(Parameters(item)) - }) + self.bulk_write( + Op::Create, + params.0, + "report template", + Ok, + |_| None, + |item| self.create_report_template_item(Parameters(item)), + ) .await } @@ -285,9 +275,7 @@ impl SiftMcpServer { rule_client_keys, }) = params; - if name.is_empty() { - return Err(ErrorData::invalid_params("`name` must not be empty", None)); - } + require_non_empty("name", &name)?; let rules = match (rule_ids, rule_client_keys) { (Some(ids), None) => TemplateRuleIdentifier::RuleIds(ids), @@ -328,27 +316,10 @@ impl SiftMcpServer { .url_service .build_report_template_url(&template.report_template_id) .ok(); - let next_step = format!( - "Created report template `{}` ({}) with {} rule(s).{} Surface it to the user. To run the \ - template over a run, call `create_report` with `report_template_id = \"{}\"`.", - template.name, - template.report_template_id, - template.rules.len(), - url_clause( - "report template", - Some(&template.name), - report_template_url.as_deref() - ), - template.report_template_id, - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "report_template": template, "report_template_url": report_template_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } #[tool( @@ -360,15 +331,13 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"report_template\": ReportTemplate, \"report_template_url\": string|null, \"next_step\": string }`. + `{ \"report_template\": ReportTemplate, \"report_template_url\": string|null }`. The returned template reflects the post-update server state. `report_template_url` is the template's Sift web link, or null when the host can't be derived. Link the template only to this URL, never to a report's page. Parameters: - - `items`: required array of 1–100 objects, one per template, with the fields below. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: one object per template, with the fields below. - `report_template_id`: required in each item; the id of the template to update. - `name`: optional new name. - `description`: optional new description. @@ -417,19 +386,12 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(params) = params; - let updates = bulk::prepare( - params.items, + self.bulk_write( + Op::Update, + params.0, "report template", template_update, |(id, _)| Some(id.clone()), - )?; - bulk::run( - Op::Update, - updates, - self.selection(params.selected_indices), - false, |(id, changes)| self.update_report_template_item(id, changes), ) .await @@ -446,33 +408,13 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let archive_clause = if template.is_archived { - " Template is now archived." - } else { - "" - }; let report_template_url = self .url_service .build_report_template_url(&template.report_template_id) .ok(); - let next_step = format!( - "Updated report template `{}` ({}).{}{}", - template.name, - template.report_template_id, - archive_clause, - url_clause( - "report template", - Some(&template.name), - report_template_url.as_deref() - ), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "report_template": template, "report_template_url": report_template_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } } diff --git a/rust/crates/sift_mcp/src/tool/report_templates/test.rs b/rust/crates/sift_mcp/src/tool/report_templates/test.rs index bb06d3806..d0e4092f5 100644 --- a/rust/crates/sift_mcp/src/tool/report_templates/test.rs +++ b/rust/crates/sift_mcp/src/tool/report_templates/test.rs @@ -267,14 +267,6 @@ async fn create_report_template_happy_path() { let url = structured_field(resp.clone(), "report_template_url"); assert_eq!(url, "https://app.test.local/report-templates/tmpl-new"); - let next_step = structured_field(resp.clone(), "next_step"); - assert!( - next_step - .as_str() - .unwrap() - .contains("[safety](https://app.test.local/report-templates/tmpl-new)"), - "got {next_step}" - ); let template = structured_field(resp, "report_template"); assert_eq!(template["reportTemplateId"], "tmpl-new"); } @@ -351,14 +343,6 @@ async fn update_report_template_happy_path() { let url = structured_field(resp.clone(), "report_template_url"); assert_eq!(url, "https://app.test.local/report-templates/tmpl-1"); - let next_step = structured_field(resp.clone(), "next_step"); - assert!( - next_step - .as_str() - .unwrap() - .contains("[renamed](https://app.test.local/report-templates/tmpl-1)"), - "got {next_step}" - ); let template = structured_field(resp, "report_template"); assert_eq!(template["name"], "renamed"); } diff --git a/rust/crates/sift_mcp/src/tool/reports/mod.rs b/rust/crates/sift_mcp/src/tool/reports/mod.rs index 5c21ae03e..153b9224b 100644 --- a/rust/crates/sift_mcp/src/tool/reports/mod.rs +++ b/rust/crates/sift_mcp/src/tool/reports/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -19,7 +19,7 @@ use crate::{ tool::common::{ MetadataEntry, bulk::{self, BulkItems, Op}, - bulk_create, read_result, url_clause, + read_result, require_non_empty, url_clause, }, }; @@ -254,14 +254,13 @@ impl SiftMcpServer { outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - `{ \"report\": Report, \"report_url\": string|null, \"job_id\": string|null, - \"created_annotation_count\": number, \"next_step\": string }`. The returned `Report` may still + \"created_annotation_count\": number }`. The returned `Report` may still be in a running state — poll `list_report_rule_summaries` to track progress. `report_url` is the report's Sift web link, or null when it can't be derived. `Report` has the `list_reports` item shape: rule totals, no per-rule summaries. Parameters: - - `items`: required array of 1–100 objects, one per report. Pass one item to create one - report. Each item takes: + - `items`: one object per report. Each item takes: - `run_id`: required; the run the report is generated over. - `name`: required; the report name. - `organization_id`: optional. Required only when the caller belongs to multiple organizations. @@ -274,9 +273,6 @@ impl SiftMcpServer { - Rules: leave `report_template_id` unset and provide EXACTLY ONE of `rule_ids`, `rule_client_keys`, or `rule_version_ids`. `rule_ids` / `rule_client_keys` resolve to each rule's current version; `rule_version_ids` pins to specific historical versions. - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Errors: - `INVALID_PARAMS` if `run_id` or `name` is empty, if both a template and rule identifiers are given, @@ -308,20 +304,21 @@ impl SiftMcpServer { )] pub async fn create_report( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(bulk_create::CreateItems { + let Parameters(BulkItems { items, selected_indices, .. }) = params; - let items = bulk_create::parse_items::(items)?; - bulk_create::create_many_with_partial_writes( - items, + // Evaluation can fail after the report exists, so every failure may be a partial write. + bulk::run( + Op::Create, + bulk::parse_items::(items)?, self.selection(selected_indices), - |item| self.create_report_item(Parameters(item)), true, + |item| self.create_report_item(Parameters(item)), ) .await } @@ -345,15 +342,8 @@ impl SiftMcpServer { rule_version_ids, }) = params; - if run_id.is_empty() { - return Err(ErrorData::invalid_params( - "`run_id` must not be empty", - None, - )); - } - if name.is_empty() { - return Err(ErrorData::invalid_params("`name` must not be empty", None)); - } + require_non_empty("run_id", &run_id)?; + require_non_empty("name", &name)?; let rule_sources = [ rule_ids.map(RuleIdentifier::RuleIds), @@ -407,29 +397,12 @@ impl SiftMcpServer { .url_service .build_report_url(&output.report.report_id) .ok(); - let job_clause = output - .job_id - .as_deref() - .map(|jid| format!(" Evaluation job `{jid}` is running asynchronously.")) - .unwrap_or_default(); - let next_step = format!( - "Created report `{}` ({}) and queued its evaluation.{}{} Surface it to the user. Use \ - `list_report_rule_summaries` with this `report_id` to track per-rule progress.", - output.report.name, - output.report.report_id, - job_clause, - url_clause("report", Some(&output.report.name), report_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "report": report_view(&output.report, None)?, "report_url": report_url, "job_id": output.job_id, "created_annotation_count": output.created_annotation_count, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } #[tool( @@ -441,15 +414,13 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"report\": Report, \"report_url\": string|null, \"next_step\": string }`. The returned `Report` + `{ \"report\": Report, \"report_url\": string|null }`. The returned `Report` is re-fetched after the update (the update RPC itself returns no body). `report_url` is the report's Sift web link, or null when the host can't be derived. `Report` has the `list_reports` item shape: rule totals, no per-rule summaries. Parameters: - - `items`: required array of 1–100 objects, one per report, with the fields below. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: one object per report, with the fields below. - `report_id`: required in each item; the id of the report to update. - `metadata`: required; REPLACES the report's full metadata list. Each entry is `{ \"name\": \"\", \"value\": }` where `value` is a string, number, or boolean. @@ -487,17 +458,12 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(params) = params; - let updates = bulk::prepare( - params.items, + self.bulk_write( + Op::Update, + params.0, "report", |item: UpdateReportItem| { - if item.report_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`report_id` must not be empty", - None, - )); - } + require_non_empty("report_id", &item.report_id)?; Ok(( item.report_id, item.metadata @@ -507,12 +473,6 @@ impl SiftMcpServer { )) }, |(id, _)| Some(id.clone()), - )?; - bulk::run( - Op::Update, - updates, - self.selection(params.selected_indices), - false, |(id, metadata)| self.update_report_item(id, metadata), ) .await @@ -530,19 +490,9 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let report_url = self.url_service.build_report_url(&report.report_id).ok(); - let next_step = format!( - "Updated metadata on report `{}` ({}).{}", - report.name, - report.report_id, - url_clause("report", Some(&report.name), report_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "report": report_view(&report, None)?, "report_url": report_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } } diff --git a/rust/crates/sift_mcp/src/tool/rules/mod.rs b/rust/crates/sift_mcp/src/tool/rules/mod.rs index c64ab78ec..25d2f2ec5 100644 --- a/rust/crates/sift_mcp/src/tool/rules/mod.rs +++ b/rust/crates/sift_mcp/src/tool/rules/mod.rs @@ -1,22 +1,22 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; use serde::Deserialize; use sift_rs::rules::v1::{UpdateConditionRequest, UpdateRuleRequest}; -use crate::tool::common::{ - bulk::{self, BulkItems, Op}, - bulk_create, -}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, service::rules::RuleUpdate, - tool::common::{ListParams, list_body, read_result, url_clause, with_urls}, + tool::common::{ + ListParams, + bulk::{BulkItems, Op}, + exactly_one, list_body, read_result, require_non_empty, url_clause, with_urls, + }, }; #[cfg(test)] @@ -217,13 +217,12 @@ impl SiftMcpServer { `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - - `{ \"rule_id\": \"\", \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_id` is the + - `{ \"rule_id\": \"\", \"name\": string, \"rule_url\": string|null }`. `rule_id` is the server-assigned id of the new rule; `rule_url` is its Sift web link (`/rules/`), or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per rule. Pass one item to create one - rule. Each item takes: + - `items`: one object per rule. Each item takes: - `rule_json`: a JSON object matching `protos/sift/rules/v1/rules.proto::UpdateRuleRequest`. Use the proto field names. Omit `rule_id` (this is a create). Key fields: - `name` (required), `description` (required). @@ -235,9 +234,6 @@ impl SiftMcpServer { - `metadata` (required): array of metadata values (may be empty: `[]`). - `client_key` (optional): a caller-defined identifier. Immutable after creation. - `is_live_evaluation_enabled` (optional): evaluate against live data. - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Errors: - `INVALID_PARAMS` if `rule_json` is not valid JSON or does not match the rule schema. @@ -266,64 +262,42 @@ impl SiftMcpServer { )] pub async fn create_rule( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(bulk_create::CreateItems { - items, - selected_indices, - .. - }) = params; - let items = bulk_create::parse_items::(items)?; - for (index, item) in items.iter().enumerate() { - let definition = parse_rule_definition(&item.rule_json) - .map_err(|error| bulk_create::at_item(index, error))?; - if definition.rule_id.is_some() { - return Err(ErrorData::invalid_params( - format!("`items` item {index}: omit `rule_id`; this tool only creates"), - None, - )); - } - } - bulk_create::create_many(items, self.selection(selected_indices), |item| { - self.create_rule_item(Parameters(item)) - }) + self.bulk_write( + Op::Create, + params.0, + "rule", + |item: CreateRuleItem| { + let definition = parse_rule_definition(&item.rule_json)?; + if definition.rule_id.is_some() { + return Err(ErrorData::invalid_params( + "omit `rule_id`; this tool only creates", + None, + )); + } + Ok(definition) + }, + |_| None, + |definition| self.create_rule_item(definition), + ) .await } - /// Create one item. Callers check access and validate the whole call first. - pub(crate) async fn create_rule_item( - &self, - params: Parameters, - ) -> error::McpResult { - self.require_create()?; - - let Parameters(CreateRuleItem { rule_json }) = params; - - let update = parse_rule_definition(&rule_json)?; - let rule_name = update.name.clone(); - + async fn create_rule_item(&self, definition: UpdateRuleRequest) -> error::McpResult { + let name = definition.name.clone(); let rule_id = self .rule_service - .create_rule(update) + .create_rule(definition) .await .map_err(from_anyhow)?; - let rule_url = self.url_service.build_rule_url(&rule_id).ok(); - let next_step = format!( - "Created rule with id `{rule_id}`.{} Tell the user the new rule id. If they haven't \ - indicated a next step, offer to confirm it with `list_rules` \ - (filter `rule_id == \"{rule_id}\"`).", - url_clause("rule", Some(rule_name.as_str()), rule_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "rule_id": rule_id, - "rule_url": rule_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + "name": name, + "rule_url": rule_url_for(self, &rule_id), + }))) } #[tool( @@ -336,11 +310,11 @@ impl SiftMcpServer { not_attempted, next_step }`. Successes: `{ index, result }`; failures: `{ index, error, outcome_unknown }`. Indices refer to input positions; backend failures leave a `not_attempted` tail. Any failure sets `isError`. Each result is: - - `{ \"rule_id\": \"\", \"rule_url\": string|null, \"next_step\": \"...\" }`. `rule_url` is + - `{ \"rule_id\": \"\", \"rule_url\": string|null }`. `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `items`: 1–100 objects, each with: + - `items`: one object per rule, each with: - `rule_id`: required. The rule to update. - `name`: optional. New rule name. - `description`: optional. New description. @@ -352,8 +326,6 @@ impl SiftMcpServer { `conditions` field of `create_rule`'s `rule_json`. Omit it to keep the existing conditions unchanged — only provide it when you intend to rewrite the rule's logic. - At least one field besides `rule_id` must be set. - - `selected_indices`: host-owned zero-based input positions; omit when proposing. - Omitted selects all outside Sift chat; chat requires a mask. `[]` writes nothing. Errors: - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two @@ -384,45 +356,28 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - let updates = bulk::prepare(items, "rule", rule_update, |(rule_id, _)| { - Some(rule_id.clone()) - })?; - bulk::run( + self.bulk_write( Op::Update, - updates, - self.selection(selected_indices), - false, + params.0, + "rule", + rule_update, + |(rule_id, _)| Some(rule_id.clone()), |(rule_id, changes)| self.update_rule_item(rule_id, changes), ) .await } async fn update_rule_item(&self, rule_id: String, changes: RuleUpdate) -> error::McpResult { - let rule_name = changes.name.clone(); let rule_id = self .rule_service .update_rule(rule_id, changes) .await .map_err(from_anyhow)?; - let rule_url = self.url_service.build_rule_url(&rule_id).ok(); - let next_step = format!( - "Updated rule `{rule_id}`.{}", - url_clause("rule", rule_name.as_deref(), rule_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "rule_id": rule_id, - "rule_url": rule_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + "rule_url": rule_url_for(self, &rule_id), + }))) } #[tool( @@ -440,11 +395,9 @@ impl SiftMcpServer { `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `items`: 1–100 objects, each with exactly one of: + - `items`: one object per rule, each with exactly one of: - `rule_id`: the id of the rule to archive. - `client_key`: the caller-defined key of the rule to archive. - - `selected_indices`: host-owned zero-based input positions; omit when proposing. - Omitted selects all outside Sift chat; chat requires a mask. `[]` writes nothing. Errors: - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two @@ -489,11 +442,9 @@ impl SiftMcpServer { `rule_url` is the rule's Sift web link, or null when the host can't be derived. Parameters: - - `items`: 1–100 objects, each with exactly one of: + - `items`: one object per rule, each with exactly one of: - `rule_id`: the id of the rule to unarchive. - `client_key`: the caller-defined key of the rule to unarchive. - - `selected_indices`: host-owned zero-based input positions; omit when proposing. - Omitted selects all outside Sift chat; chat requires a mask. `[]` writes nothing. Errors: - `INVALID_PARAMS` before any write if `items` is empty or has more than 100 entries, if two @@ -528,15 +479,13 @@ impl SiftMcpServer { params: Parameters>, op: Op, ) -> error::McpResult { - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - let targets = bulk::prepare( - items, + self.bulk_write( + op, + params.0, "rule", - |item: RuleArchiveItem| rule_identifier(item.rule_id, item.client_key), + |item: RuleArchiveItem| { + exactly_one(("rule_id", item.rule_id), ("client_key", item.client_key)) + }, |(rule_id, client_key)| { Some(if rule_id.is_empty() { format!("client_key:{client_key}") @@ -544,12 +493,6 @@ impl SiftMcpServer { format!("rule_id:{rule_id}") }) }, - )?; - bulk::run( - op, - targets, - self.selection(selected_indices), - false, |(rule_id, client_key)| self.set_rule_archived(rule_id, client_key, op), ) .await @@ -566,8 +509,7 @@ impl SiftMcpServer { .find_rule(rule_id, client_key) .await .map_err(from_anyhow)?; - let archived = op == Op::Archive; - let write = if archived { + let write = if op == Op::Archive { self.rule_service .archive_rule(rule.rule_id.clone(), String::new()) .await @@ -578,11 +520,10 @@ impl SiftMcpServer { }; write.map_err(from_anyhow)?; - let state = if archived { "archived" } else { "unarchived" }; Ok(CallToolResult::structured(serde_json::json!({ "rule_id": rule.rule_id, "name": rule.name, - state: true, + op.state_key(): true, "rule_url": rule_url_for(self, &rule.rule_id), }))) } @@ -599,12 +540,7 @@ fn rule_update(item: UpdateRuleItem) -> Result<(String, RuleUpdate), ErrorData> tag_ids, conditions_json, } = item; - if rule_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`rule_id` must not be empty", - None, - )); - } + require_non_empty("rule_id", &rule_id)?; let conditions = conditions_json .as_deref() .map(parse_conditions) @@ -665,23 +601,3 @@ fn parse_conditions(conditions_json: &str) -> Result ) }) } - -/// Resolve the `(rule_id, client_key)` request fields from the mutually -/// exclusive optional params, returning `INVALID_PARAMS` unless exactly one is set. -fn rule_identifier( - rule_id: Option, - client_key: Option, -) -> Result<(String, String), ErrorData> { - match (rule_id, client_key) { - (Some(r), None) => Ok((r, String::new())), - (None, Some(c)) => Ok((String::new(), c)), - (Some(_), Some(_)) => Err(ErrorData::invalid_params( - "exactly one of `rule_id` or `client_key` must be set, not both", - None, - )), - (None, None) => Err(ErrorData::invalid_params( - "one of `rule_id` or `client_key` must be set", - None, - )), - } -} diff --git a/rust/crates/sift_mcp/src/tool/rules/test.rs b/rust/crates/sift_mcp/src/tool/rules/test.rs index 3ff6de4f6..dc3e104dd 100644 --- a/rust/crates/sift_mcp/src/tool/rules/test.rs +++ b/rust/crates/sift_mcp/src/tool/rules/test.rs @@ -1,35 +1,6 @@ use rmcp::model::ErrorCode; -use super::{parse_rule_definition, rule_identifier}; - -#[test] -fn rule_identifier_accepts_rule_id_only() { - let (rule_id, client_key) = - rule_identifier(Some("rule-1".to_string()), None).expect("should accept rule_id"); - assert_eq!(rule_id, "rule-1"); - assert_eq!(client_key, ""); -} - -#[test] -fn rule_identifier_accepts_client_key_only() { - let (rule_id, client_key) = - rule_identifier(None, Some("ck-1".to_string())).expect("should accept client_key"); - assert_eq!(rule_id, ""); - assert_eq!(client_key, "ck-1"); -} - -#[test] -fn rule_identifier_rejects_both() { - let err = rule_identifier(Some("rule-1".to_string()), Some("ck-1".to_string())) - .expect_err("should reject both"); - assert_eq!(err.code, ErrorCode::INVALID_PARAMS); -} - -#[test] -fn rule_identifier_rejects_neither() { - let err = rule_identifier(None, None).expect_err("should reject neither"); - assert_eq!(err.code, ErrorCode::INVALID_PARAMS); -} +use super::parse_rule_definition; #[test] fn parse_rule_definition_parses_valid_json() { diff --git a/rust/crates/sift_mcp/src/tool/runs/mod.rs b/rust/crates/sift_mcp/src/tool/runs/mod.rs index 7a46cbbfc..aad0a3428 100644 --- a/rust/crates/sift_mcp/src/tool/runs/mod.rs +++ b/rust/crates/sift_mcp/src/tool/runs/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -13,8 +13,8 @@ use crate::{ server::SiftMcpServer, tool::common::{ ListParams, MetadataEntry, - bulk::{self, BulkItems, Op}, - list_body, url_clause, with_urls, + bulk::{BulkItems, Op}, + list_body, require_non_empty, with_urls, }, }; @@ -35,12 +35,7 @@ pub struct UpdateRunItem { } fn validate_update_run(item: &UpdateRunItem) -> Result<(), ErrorData> { - if item.run_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`run_id` must not be empty", - None, - )); - } + require_non_empty("run_id", &item.run_id)?; if !(item.name.is_some() || item.description.is_some() || item.start_time_unix_nanos.is_some() @@ -154,14 +149,12 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, not_attempted, next_step }`. Each success holds `{ index, result }`, each failure holds `{ index, error, outcome_unknown }`, and any failure sets `isError`. Each result is: - - `{ \"run\": Run, \"run_url\": string|null, \"next_step\": string }`. The returned `Run` is the + - `{ \"run\": Run, \"run_url\": string|null }`. The returned `Run` is the post-update state from the server; `run_url` is its Sift web link (`/run/`), or null when the host can't be derived. Parameters: - - `items`: required array of 1–100 objects, one per run. Each item takes the fields below. - - `selected_indices`: zero-based item positions supplied by the approval host; omit when - proposing. Omitted selects all outside chat; chat requires a mask. `[]` updates nothing. + - `items`: one object per run. Each item takes the fields below. - `run_id`: required; the id of the run to update. - `name`: optional new name. - `description`: optional new description. @@ -201,34 +194,22 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - let items = bulk::prepare( - items, + self.bulk_write( + Op::Update, + params.0, "run", |item: UpdateRunItem| { validate_update_run(&item)?; Ok(item) }, |item| Some(item.run_id.clone()), - )?; - bulk::run( - Op::Update, - items, - self.selection(selected_indices), - false, - |item| self.update_run_item(Parameters(item)), + |item| self.update_run_item(item), ) .await } - async fn update_run_item(&self, params: Parameters) -> error::McpResult { - self.require_destructive()?; - validate_update_run(¶ms.0)?; - let Parameters(UpdateRunItem { + async fn update_run_item(&self, item: UpdateRunItem) -> error::McpResult { + let UpdateRunItem { run_id, name, description, @@ -238,7 +219,7 @@ impl SiftMcpServer { client_key, tags, metadata, - }) = params; + } = item; let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); @@ -259,19 +240,9 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let run_url = self.url_service.build_run_url(&run.run_id).ok(); - let next_step = format!( - "Updated run `{}` ({}).{}", - run.name, - run.run_id, - url_clause("run", Some(&run.name), run_url.as_deref()), - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "run": run, "run_url": run_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } } diff --git a/rust/crates/sift_mcp/src/tool/runs/test.rs b/rust/crates/sift_mcp/src/tool/runs/test.rs index 69e51ca78..c80a5a2db 100644 --- a/rust/crates/sift_mcp/src/tool/runs/test.rs +++ b/rust/crates/sift_mcp/src/tool/runs/test.rs @@ -9,28 +9,16 @@ use sift_test_util::{grpc::memory_sift_channel, mock::runs::v2::MockRunServiceIm use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; -use super::UpdateRunItem as UpdateRunParams; +use serde_json::json; + use crate::{ server::SiftMcpServer, tool::common::test_support::{ - list_params, list_params_with_fields, structured, structured_field, + bulk_params, first_result, list_params, list_params_with_fields, structured, + structured_field, }, }; -fn update_run_params(run_id: &str) -> UpdateRunParams { - UpdateRunParams { - run_id: run_id.into(), - name: None, - description: None, - start_time_unix_nanos: None, - stop_time_unix_nanos: None, - is_pinned: None, - client_key: None, - tags: None, - metadata: None, - } -} - async fn server_with_mock(mock: MockRunServiceImpl) -> (SiftMcpServer, JoinHandle<()>) { let (client, server) = tokio::io::duplex(1024); let channel = memory_sift_channel(client).await; @@ -215,42 +203,32 @@ async fn update_run_happy_path_surfaces_url() { let (server, _h) = server_with_mock(run_mock).await; - let mut params = update_run_params("r1"); - params.name = Some("renamed".into()); - - let resp = server - .update_run_item(Parameters(params)) - .await - .expect("update_run failed"); - - let run_url = structured_field(resp.clone(), "run_url"); - assert_eq!(run_url, "https://app.test.local/run/r1"); - let run = structured_field(resp, "run"); - assert_eq!(run["name"], "renamed"); -} - -#[tokio::test] -async fn update_run_rejects_empty_id() { - let (server, _h) = server_with_mock(MockRunServiceImpl::new()).await; - - let err = server - .update_run_item(Parameters(update_run_params(""))) - .await - .expect_err("expected error"); + let result = first_result( + server + .update_run(bulk_params( + json!([{"run_id": "r1", "name": "renamed"}]), + None, + )) + .await + .expect("update_run failed"), + ); - assert_eq!(err.code, ErrorCode::INVALID_PARAMS); + assert_eq!(result["run_url"], "https://app.test.local/run/r1"); + assert_eq!(result["run"]["name"], "renamed"); + assert!(result.get("next_step").is_none(), "{result}"); } #[tokio::test] -async fn update_run_rejects_no_fields() { +async fn update_run_rejects_an_empty_id_or_no_fields_before_writing() { let (server, _h) = server_with_mock(MockRunServiceImpl::new()).await; - let err = server - .update_run_item(Parameters(update_run_params("r1"))) - .await - .expect_err("expected error"); - - assert_eq!(err.code, ErrorCode::INVALID_PARAMS); + for item in [json!({"run_id": "", "name": "x"}), json!({"run_id": "r1"})] { + let err = server + .update_run(bulk_params(json!([item]), None)) + .await + .expect_err("expected error"); + assert_eq!(err.code, ErrorCode::INVALID_PARAMS); + } } #[tokio::test] diff --git a/rust/crates/sift_mcp/src/tool/test_reports/mod.rs b/rust/crates/sift_mcp/src/tool/test_reports/mod.rs index 4f7f48b21..71c1ae846 100644 --- a/rust/crates/sift_mcp/src/tool/test_reports/mod.rs +++ b/rust/crates/sift_mcp/src/tool/test_reports/mod.rs @@ -369,8 +369,7 @@ impl SiftMcpServer { with the user before calling, and report back what was created. Parameters: - - `items`: required array of 1–100 objects, one per test report. Pass one item to create one - test report. Each item takes: + - `items`: one object per test report. Each item takes: - `report_json`: a JSON object describing the report. Shape: - Report fields: `name` (required), `test_system_name` (required), `test_case` (required), `status` (optional, default `PASSED`; accepts `PASSED` or @@ -392,9 +391,6 @@ impl SiftMcpServer { equals `string_expected`, else true), `measurement_type` (optional; derived from the value kind), `timestamp` (optional RFC3339, default the step start), `description`, `channel_names` (array), and `metadata`. - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Output: - `{ requested_count, selected_count, not_selected, created_count, successes, failures, @@ -402,7 +398,7 @@ impl SiftMcpServer { `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - - `{ \"test_report_id\": \"...\", \"steps_created\": N, \"measurements_created\": M, \"report_url\": string|null, \"next_step\": \"...\" }`. + - `{ \"test_report_id\": \"...\", \"steps_created\": N, \"measurements_created\": M, \"report_url\": string|null }`. `report_url` is the report's Sift web link (`/test-results/`), or null when the host cannot be derived (self-hosted deployments). Surface it to the user as a clickable link. @@ -479,26 +475,12 @@ impl SiftMcpServer { .build_test_report_url(&created.test_report_id) .ok(); - let next_step = format!( - "Created test report `{}` with {} step(s) and {} measurement(s) in Sift.{} Tell the \ - user the new `test_report_id` and surface the link. If they haven't indicated a next \ - step, offer to verify with `list_test_steps` (filter `test_report_id == \"{}\"`).", - created.test_report_id, - created.steps_created, - created.measurements_created, - url_clause("test report", None, report_url.as_deref()), - created.test_report_id, - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "test_report_id": created.test_report_id, "steps_created": created.steps_created, "measurements_created": created.measurements_created, "report_url": report_url, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } #[tool( diff --git a/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs b/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs index 9c361a951..59c7674c0 100644 --- a/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs +++ b/rust/crates/sift_mcp/src/tool/user_defined_functions/mod.rs @@ -1,7 +1,7 @@ use rmcp::{ ErrorData, handler::server::wrapper::Parameters, - model::{CallToolResult, ContentBlock}, + model::CallToolResult, schemars::{self, JsonSchema}, tool, tool_router, }; @@ -11,15 +11,15 @@ use sift_rs::{ metadata::v1::MetadataValue, }; -use crate::tool::common::{ - bulk::{self, BulkItems, Op}, - bulk_create, -}; use crate::{ error::{self, from_anyhow}, server::SiftMcpServer, service::user_defined_functions::UdfUpdate, - tool::common::{ListParams, MetadataEntry, list_body, to_values}, + tool::common::{ + ListParams, MetadataEntry, + bulk::{BulkItems, Op}, + exactly_one, list_body, require_non_empty, to_values, + }, }; #[cfg(test)] @@ -227,7 +227,11 @@ impl SiftMcpServer { fields, }) = params; - let (user_defined_function_id, name) = function_identifier(user_defined_function_id, name)?; + // The proto ignores `name` when an id is present, so reject the ambiguous call. + let (user_defined_function_id, name) = exactly_one( + ("user_defined_function_id", user_defined_function_id), + ("name", name), + )?; let page = self .user_defined_function_service @@ -264,13 +268,12 @@ impl SiftMcpServer { `index` is the item's zero-based position in `items`. `failures` holds `{ index, error, outcome_unknown }`. `not_attempted` lists items skipped after a backend-wide error. Any failure sets `isError`. Each `result` is: - - `{ \"user_defined_function\": UserDefinedFunction, \"user_defined_function_id\": \"\", - \"next_step\": \"...\" }`. The returned function is the server's post-create state, + - `{ \"user_defined_function\": UserDefinedFunction, \"user_defined_function_id\": \"\" }`. + The returned function is the server's post-create state, including the resolved `function_output_type` and `version` 1. Parameters: - - `items`: required array of 1–100 objects, one per user defined function. Pass one item to create one - user defined function. Each item takes: + - `items`: one object per user defined function. Each item takes: - `name`: required. The name callers use to reference the function. It must start with a letter, then contain only alphanumeric characters or `_`, and be at most 253 characters. - `expression`: required. The function body. Reference each declared input by its @@ -288,9 +291,6 @@ impl SiftMcpServer { `value` is a string, number, or boolean. A `name` that does not yet exist in the organization's metadata schema is created on the fly with the type inferred from `value`; for an existing key the type must match. - - `selected_indices`: zero-based positions in `items` to create. An approval host sets this - from the user's per-item decisions; omit it when proposing. Omitted means every item, - except in a Sift chat pod, where a missing value is rejected. `[]` creates nothing. Errors: - `INVALID_PARAMS` if `name` or `expression` is empty, `function_inputs_json` is not a JSON @@ -324,18 +324,17 @@ impl SiftMcpServer { )] pub async fn create_user_defined_function( &self, - params: Parameters>, + params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(bulk_create::CreateItems { - items, - selected_indices, - .. - }) = params; - let items = bulk_create::parse_items::(items)?; - bulk_create::create_many(items, self.selection(selected_indices), |item| { - self.create_user_defined_function_item(Parameters(item)) - }) + self.bulk_write( + Op::Create, + params.0, + "user defined function", + Ok, + |_| None, + |item| self.create_user_defined_function_item(Parameters(item)), + ) .await } @@ -355,16 +354,9 @@ impl SiftMcpServer { metadata, }) = params; - if name.trim().is_empty() { - return Err(ErrorData::invalid_params("`name` must not be empty", None)); - } + require_non_empty("name", &name)?; validate_function_name(&name)?; - if expression.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`expression` must not be empty", - None, - )); - } + require_non_empty("expression", &expression)?; let function_inputs = parse_function_inputs(&function_inputs_json)?; let metadata = metadata_values(metadata).unwrap_or_default(); @@ -382,20 +374,10 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let next_step = format!( - "Created user defined function `{}` with id `{}` at version {}. Tell the user the new id \ - and confirm the expression matches their intent. Reference it from a calculated channel \ - or rule by name.", - function.name, function.user_defined_function_id, function.version, - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "user_defined_function": function, "user_defined_function_id": function.user_defined_function_id, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } #[tool( @@ -409,14 +391,12 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"user_defined_function\": UserDefinedFunction, \"next_step\": \"...\" }`. The + `{ \"user_defined_function\": UserDefinedFunction }`. The returned function is the newly created version, with its own `user_defined_function_version_id` and incremented `version`. Parameters: - - `items`: required array of 1–100 objects, one per function, with the fields below. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: one object per function, with the fields below. - `user_defined_function_id`: required in each item. The function to update. - `name`: optional. New name. It must start with a letter, then contain only alphanumeric characters or `_`, and be at most 253 characters. The API applies a rename BY ITSELF and @@ -467,19 +447,12 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(params) = params; - let updates = bulk::prepare( - params.items, + self.bulk_write( + Op::Update, + params.0, "user defined function", udf_update, |(id, _)| Some(id.clone()), - )?; - bulk::run( - Op::Update, - updates, - self.selection(params.selected_indices), - false, |(id, changes)| self.update_user_defined_function_item(id, changes), ) .await @@ -496,17 +469,9 @@ impl SiftMcpServer { .await .map_err(from_anyhow)?; - let next_step = format!( - "Updated user defined function `{}` ({}); it is now at version {}.", - function.name, function.user_defined_function_id, function.version, - ); - - let mut result = CallToolResult::structured(serde_json::json!({ + Ok(CallToolResult::structured(serde_json::json!({ "user_defined_function": function, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + }))) } #[tool( @@ -519,12 +484,10 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"archived\": true, \"user_defined_function\": UserDefinedFunction, \"next_step\": string }`. + `{ \"archived\": true, \"user_defined_function\": UserDefinedFunction }`. Parameters: - - `items`: required array of 1–100 objects, each with `user_defined_function_id`, the id to archive. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: each item takes `user_defined_function_id`, the id to archive. Errors: - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, @@ -553,27 +516,7 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - self.bulk_udf_archive(params, Op::Archive).await - } - - async fn archive_user_defined_function_item( - &self, - user_defined_function_id: String, - ) -> error::McpResult { - let function = self.set_archived(user_defined_function_id, true).await?; - - let next_step = format!( - "Archived user defined function `{}` ({}).", - function.name, function.user_defined_function_id, - ); - - let mut result = CallToolResult::structured(serde_json::json!({ - "archived": true, - "user_defined_function": function, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) + self.set_udfs_archived(params, Op::Archive).await } #[tool( @@ -585,12 +528,10 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"unarchived\": true, \"user_defined_function\": UserDefinedFunction, \"next_step\": string }`. + `{ \"unarchived\": true, \"user_defined_function\": UserDefinedFunction }`. Parameters: - - `items`: required array of 1–100 objects, each with `user_defined_function_id`, the id to restore. - - `selected_indices`: host-owned zero-based positions in `items`. Omit when proposing. - Omitted selects every item except in Sift chat, where a missing mask fails closed. `[]` writes nothing. + - `items`: each item takes `user_defined_function_id`, the id to restore. Errors: - `INVALID_PARAMS` before any write for empty/oversized items, duplicate ids, unknown fields, @@ -615,89 +556,41 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - self.bulk_udf_archive(params, Op::Unarchive).await + self.set_udfs_archived(params, Op::Unarchive).await } +} - async fn bulk_udf_archive( +impl SiftMcpServer { + /// Shared body of the archive and unarchive tools. Both flip the same + /// `is_archived` field through the update mask. + async fn set_udfs_archived( &self, params: Parameters>, op: Op, ) -> error::McpResult { - let Parameters(params) = params; - let items = bulk::prepare( - params.items, + self.bulk_write( + op, + params.0, "user defined function", |item: ArchiveUserDefinedFunctionItem| { - if item.user_defined_function_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`user_defined_function_id` must not be empty", - None, - )); - } - Ok(item) + require_non_empty("user_defined_function_id", &item.user_defined_function_id)?; + Ok(item.user_defined_function_id) }, - |item| Some(item.user_defined_function_id.clone()), - )?; - bulk::run( - op, - items, - self.selection(params.selected_indices), - false, - |item| async move { - if op == Op::Archive { - self.archive_user_defined_function_item(item.user_defined_function_id) - .await - } else { - self.unarchive_user_defined_function_item(item.user_defined_function_id) - .await - } + |id| Some(id.clone()), + |id| async move { + let function = self + .user_defined_function_service + .set_user_defined_function_archived(id, op == Op::Archive) + .await + .map_err(from_anyhow)?; + Ok(CallToolResult::structured(serde_json::json!({ + op.state_key(): true, + "user_defined_function": function, + }))) }, ) .await } - - async fn unarchive_user_defined_function_item( - &self, - user_defined_function_id: String, - ) -> error::McpResult { - let function = self.set_archived(user_defined_function_id, false).await?; - - let next_step = format!( - "Unarchived user defined function `{}` ({}).", - function.name, function.user_defined_function_id, - ); - - let mut result = CallToolResult::structured(serde_json::json!({ - "unarchived": true, - "user_defined_function": function, - "next_step": next_step, - })); - result.content = vec![ContentBlock::text(next_step)]; - Ok(result) - } -} - -impl SiftMcpServer { - /// Shared body of the archive and unarchive tools. Both flip the same - /// `is_archived` field through the update mask, so only the validation and - /// the reported wording differ. - async fn set_archived( - &self, - user_defined_function_id: String, - is_archived: bool, - ) -> Result { - if user_defined_function_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`user_defined_function_id` must not be empty", - None, - )); - } - - self.user_defined_function_service - .set_user_defined_function_archived(user_defined_function_id, is_archived) - .await - .map_err(from_anyhow) - } } fn udf_update(item: UpdateUserDefinedFunctionItem) -> Result<(String, UdfUpdate), ErrorData> { @@ -709,12 +602,7 @@ fn udf_update(item: UpdateUserDefinedFunctionItem) -> Result<(String, UdfUpdate) function_inputs_json, metadata, } = item; - if user_defined_function_id.trim().is_empty() { - return Err(ErrorData::invalid_params( - "`user_defined_function_id` must not be empty", - None, - )); - } + require_non_empty("user_defined_function_id", &user_defined_function_id)?; if let Some(name) = &name { validate_function_name(name)?; } @@ -747,27 +635,6 @@ fn udf_update(item: UpdateUserDefinedFunctionItem) -> Result<(String, UdfUpdate) Ok((user_defined_function_id, changes)) } -/// Resolve the `(user_defined_function_id, name)` request fields from the -/// mutually exclusive optional params. The proto silently ignores `name` when an -/// id is present, so reject the ambiguous call rather than pick for the caller. -fn function_identifier( - user_defined_function_id: Option, - name: Option, -) -> Result<(String, String), ErrorData> { - match (user_defined_function_id, name) { - (Some(id), None) => Ok((id, String::new())), - (None, Some(name)) => Ok((String::new(), name)), - (Some(_), Some(_)) => Err(ErrorData::invalid_params( - "exactly one of `user_defined_function_id` or `name` must be set, not both", - None, - )), - (None, None) => Err(ErrorData::invalid_params( - "one of `user_defined_function_id` or `name` must be set", - None, - )), - } -} - fn validate_function_name(name: &str) -> Result<(), ErrorData> { let valid = name.len() <= 253 && matches!(name.as_bytes().first(), Some(b'A'..=b'Z' | b'a'..=b'z')) diff --git a/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs b/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs index 13536d3cf..402edcfc8 100644 --- a/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs +++ b/rust/crates/sift_mcp/src/tool/user_defined_functions/test.rs @@ -612,7 +612,7 @@ async fn create_rejects_a_name_longer_than_253_characters() { } #[tokio::test] -async fn create_returns_the_new_function_and_next_step() { +async fn create_returns_the_new_function() { let mut mock = MockUserDefinedFunctionServiceImpl::new(); mock.expect_create_user_defined_function() .times(1) @@ -650,12 +650,7 @@ async fn create_returns_the_new_function_and_next_step() { let body = structured(resp); assert_eq!(body["user_defined_function_id"], "f1"); assert_eq!(body["user_defined_function"]["name"], "rms"); - assert!( - body["next_step"] - .as_str() - .expect("next_step") - .contains("f1") - ); + assert!(body.get("next_step").is_none(), "{body}"); } #[tokio::test] @@ -784,7 +779,7 @@ async fn update_returns_the_new_version() { body["user_defined_function"]["userDefinedFunctionVersionId"], "v4" ); - assert!(body["next_step"].as_str().expect("next_step").contains("4")); + assert!(body.get("next_step").is_none(), "{body}"); } #[tokio::test] From 0615059378272b37811d28487ce70bcd6f77cdab Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Fri, 13 Nov 2026 15:47:44 -0800 Subject: [PATCH 17/19] rust(fix): address bulk write review findings --- rust/crates/sift_cli/CHANGELOG.md | 7 ++- rust/crates/sift_mcp/CLAUDE.md | 5 +- .../sift_mcp/src/service/annotations/mod.rs | 26 ++++----- .../sift_mcp/src/service/annotations/test.rs | 23 +++++++- .../sift_mcp/src/tool/annotations/mod.rs | 36 ++++++++---- .../sift_mcp/src/tool/annotations/test.rs | 57 +++++++++---------- .../crates/sift_mcp/src/tool/artifacts/mod.rs | 36 ++++-------- .../src/tool/calculated_channels/mod.rs | 40 +++++++------ .../src/tool/calculated_channels/test.rs | 34 ++++++----- rust/crates/sift_mcp/src/tool/common/bulk.rs | 57 +++++++++++++++---- rust/crates/sift_mcp/src/tool/common/mod.rs | 41 ++++++++----- rust/crates/sift_mcp/src/tool/common/test.rs | 8 +++ rust/crates/sift_mcp/src/tool/reports/mod.rs | 21 +++---- rust/crates/sift_mcp/src/tool/reports/test.rs | 16 ++++++ rust/crates/sift_mcp/src/tool/rules/test.rs | 8 +++ 15 files changed, 259 insertions(+), 156 deletions(-) diff --git a/rust/crates/sift_cli/CHANGELOG.md b/rust/crates/sift_cli/CHANGELOG.md index 657b5ed2c..8b230fcc0 100644 --- a/rust/crates/sift_cli/CHANGELOG.md +++ b/rust/crates/sift_cli/CHANGELOG.md @@ -25,9 +25,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). takes up to 1000 ids, or up to 100 when it runs under an approval host (`sift-cli mcp --chat`). Archiving and unarchiving now run per id. The result now uses the same per-item `successes`, `failures`, and `not_attempted` lists - as the other bulk writes, indexed by position in `annotation_ids`. This is a - breaking change: the `batch_archive_error` and `archive_skipped` fields are - removed. + as the other bulk writes, indexed by position in `annotation_ids`. Each + success holds a compact `annotation_id`, `name`, `state`, `is_archived` and + `url`, not the full annotation. This is a breaking change: the + `batch_archive_error` and `archive_skipped` fields are removed. - Bulk write results no longer repeat a `next_step` inside every item. The call-level `next_step` covers the whole batch, and an item carries a `warning` only when it needs attention, such as a calculated channel that will not diff --git a/rust/crates/sift_mcp/CLAUDE.md b/rust/crates/sift_mcp/CLAUDE.md index 5becdb773..128a8a061 100644 --- a/rust/crates/sift_mcp/CLAUDE.md +++ b/rust/crates/sift_mcp/CLAUDE.md @@ -291,8 +291,9 @@ pub async fn list_webhooks(&self, params: Parameters) -> error::McpR - **Bulk writes.** A tool that takes `items` gates itself first, then calls `self.bulk_write(op, params.0, resource, validate, target, write)` from `tool/common/bulk.rs`. `validate` checks one item before any write, `target` returns the key that must not repeat - across items, and `write` performs one item. Reach for `bulk::prepare` and `bulk::run` directly - only when a failure can follow a partial write (`create_artifact`, `create_report`). The + across items, and `write` performs one item. Use `self.bulk_write_partial` instead when one + item takes several RPCs, so a failure can follow a committed change (`create_artifact`, + `update_report`). The call-level `next_step` covers the batch, so an item result carries no `next_step` of its own, only a `warning` when that item needs the user's attention. Artifact results are the one exception: the Sift chat host matches `Created artifact ` in them. Do not repeat the diff --git a/rust/crates/sift_mcp/src/service/annotations/mod.rs b/rust/crates/sift_mcp/src/service/annotations/mod.rs index 3ea8f3eb6..cc7d76e63 100644 --- a/rust/crates/sift_mcp/src/service/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/service/annotations/mod.rs @@ -1,9 +1,5 @@ use crate::policy::{RetryPolicy, once, with_retry}; -use crate::{ - error::from_anyhow, - service::common, - tool::common::bulk::{self, Op}, -}; +use crate::service::common; use anyhow::{Context, Result, anyhow}; use futures::{StreamExt, stream}; use pbjson_types::{FieldMask, Timestamp}; @@ -24,18 +20,18 @@ mod test; const MAX_CONCURRENT_ANNOTATION_UPDATES: usize = 50; #[derive(Debug)] -pub struct AnnotationUpdateFailure { +pub struct AnnotationUpdateFailure { pub annotation_id: String, - pub error: rmcp::ErrorData, + pub error: E, } #[derive(Debug)] -pub struct UpdateAnnotationsResult { +pub struct UpdateAnnotationsResult { pub annotations: Vec, /// The requested id of each entry in `annotations`, in the same order. The /// returned annotation's own id may differ in form from the request. pub updated_ids: Vec, - pub failures: Vec, + pub failures: Vec>, pub not_attempted: Vec, } @@ -231,8 +227,11 @@ impl AnnotationService { .ok_or_else(|| anyhow!("archive_annotation response missing annotation")) } + /// Update and then archive or unarchive each id, up to 50 at a time. `classify` maps + /// each failure to the caller's error type and says whether it stops the ids not yet + /// started, so this service stays independent of how a caller reports errors. #[allow(clippy::too_many_arguments)] - pub async fn update_annotations( + pub async fn update_annotations( &self, annotation_ids: Vec, name: Option, @@ -245,7 +244,8 @@ impl AnnotationService { linked_channel_ids: Option>, metadata: Option>, is_archived: Option, - ) -> Result { + classify: impl Fn(anyhow::Error) -> (E, bool), + ) -> Result> { let has_field_update = name.is_some() || description.is_some() || start_time_unix_nanos.is_some() @@ -318,8 +318,8 @@ impl AnnotationService { updated_ids.push(annotation_id); } Err(error) => { - let error = from_anyhow(error); - stop |= bulk::should_stop(Op::Update, &error); + let (error, stops) = classify(error); + stop |= stops; failures.push(AnnotationUpdateFailure { annotation_id, error, diff --git a/rust/crates/sift_mcp/src/service/annotations/test.rs b/rust/crates/sift_mcp/src/service/annotations/test.rs index 2f7668133..d6605e61b 100644 --- a/rust/crates/sift_mcp/src/service/annotations/test.rs +++ b/rust/crates/sift_mcp/src/service/annotations/test.rs @@ -16,6 +16,22 @@ use tokio::{sync::Semaphore, task::JoinHandle}; use tonic::{Response, Status, transport::Server}; use super::{AnnotationService, fan_out_bounded}; + +/// Stop the batch on anything but an item-local gRPC failure, as the tool's classifier does. +fn classify(error: anyhow::Error) -> (anyhow::Error, bool) { + let item_local = error.downcast_ref::().is_some_and(|status| { + matches!( + status.code(), + tonic::Code::NotFound + | tonic::Code::InvalidArgument + | tonic::Code::PermissionDenied + | tonic::Code::FailedPrecondition + | tonic::Code::AlreadyExists + | tonic::Code::Aborted + ) + }); + (error, !item_local) +} use crate::service::common::DEFAULT_LIMIT; async fn service_with_mock(mock: MockAnnotationServiceImpl) -> (AnnotationService, JoinHandle<()>) { @@ -430,6 +446,7 @@ async fn update_annotations_only_archives_ids_whose_field_update_succeeded() { None, None, Some(archived), + classify, ) .await .unwrap(); @@ -476,6 +493,7 @@ async fn archive_item_errors_do_not_stop_later_groups() { None, None, Some(true), + classify, ) .await .unwrap(); @@ -491,7 +509,7 @@ async fn archive_backend_failure_stops_ids_not_yet_started() { let mut mock = MockAnnotationServiceImpl::new(); mock.expect_archive_annotation() .times(50) - .returning(|_| Err(Status::unavailable("offline"))); + .returning(|_| Err(Status::resource_exhausted("offline"))); let (service, _h) = service_with_mock(mock).await; let ids = (0..60).map(|i| format!("ann{i}")).collect::>(); let outcome = service @@ -507,6 +525,7 @@ async fn archive_backend_failure_stops_ids_not_yet_started() { None, None, Some(true), + classify, ) .await .unwrap(); @@ -542,6 +561,7 @@ async fn update_annotations_collects_failures_and_continues() { None, None, None, + classify, ) .await .expect("bulk update failed"); @@ -581,6 +601,7 @@ async fn update_annotations_stops_after_backend_wide_failure() { None, None, None, + classify, ) .await .expect("bulk update failed"); diff --git a/rust/crates/sift_mcp/src/tool/annotations/mod.rs b/rust/crates/sift_mcp/src/tool/annotations/mod.rs index 21a9bea53..f64dd0b99 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/mod.rs @@ -430,8 +430,9 @@ impl SiftMcpServer { Output: - `{ requested_count, selected_count, not_selected, updated_count, successes, failures, - not_attempted, next_step }`. Successes hold `{ index, result: { annotation } }`; the annotation - includes a `url` field when the host can be derived. Failures hold `{ index, error, outcome_unknown }`. + not_attempted, next_step }`. Successes hold `{ index, result: { annotation_id, name, state, + is_archived, url } }`; `url` is null when the host can't be derived. Read the full annotation + with `list_annotations` if you need other fields. Failures hold `{ index, error, outcome_unknown }`. Every index is a zero-based position in `annotation_ids`. `not_attempted` lists positions not started after a backend-wide error. Any failure sets `isError`. @@ -573,23 +574,38 @@ impl SiftMcpServer { linked_channel_ids, metadata, is_archived, + |error| { + let error = from_anyhow(error); + let stop = bulk::should_stop(Op::Update, &error); + (error, stop) + }, ) .await .map_err(from_anyhow)?; - let annotations = with_urls(&outcome.annotations, |annotation| { - self.url_service - .build_annotation_url(&annotation.annotation_id) - .ok() - })?; + // A compact result per id keeps 100 updates well inside a chat host's output cap. let successes = outcome .updated_ids .iter() - .zip(annotations) - .map(|(annotation_id, value)| { + .zip(&outcome.annotations) + .map(|(annotation_id, annotation)| { + let state = annotation + .state + .and_then(|state| AnnotationState::try_from(state).ok()) + .map(|state| state.as_str_name()); + let url = self + .url_service + .build_annotation_url(&annotation.annotation_id) + .ok(); ( positions[annotation_id], - CallToolResult::structured(serde_json::json!({"annotation": value})), + CallToolResult::structured(serde_json::json!({ + "annotation_id": annotation.annotation_id, + "name": annotation.name, + "state": state, + "is_archived": annotation.is_archived, + "url": url, + })), ) }) .collect(); diff --git a/rust/crates/sift_mcp/src/tool/annotations/test.rs b/rust/crates/sift_mcp/src/tool/annotations/test.rs index 8ab13f6e5..1148ae7d7 100644 --- a/rust/crates/sift_mcp/src/tool/annotations/test.rs +++ b/rust/crates/sift_mcp/src/tool/annotations/test.rs @@ -222,7 +222,7 @@ async fn update_annotation_approves_two_of_three_ids() { assert_eq!(body["successes"][0]["index"], 0); assert_eq!(body["successes"][1]["index"], 2); assert_eq!( - body["successes"][1]["result"]["annotation"]["url"], + body["successes"][1]["result"]["url"], "https://app.test.local/annotation/ann3" ); } @@ -232,7 +232,7 @@ async fn update_annotation_reports_original_positions_for_selected_failures_and_ let mut mock = MockAnnotationServiceImpl::new(); mock.expect_update_annotation() .times(50) - .returning(|_| Err(Status::unavailable("offline"))); + .returning(|_| Err(Status::resource_exhausted("offline"))); let (server, _h) = server_with_mock(mock).await; let mut params = update_params("ann0"); params @@ -541,11 +541,23 @@ async fn update_annotation_happy_path() { .expect("update_annotation failed"); let successes = structured_field(resp, "successes"); - assert_eq!(successes[0]["result"]["annotation"]["name"], "renamed"); + assert_eq!(successes[0]["result"]["name"], "renamed"); assert_eq!( - successes[0]["result"]["annotation"]["url"], + successes[0]["result"]["url"], "https://app.test.local/annotation/ann1" ); + let mut keys = successes[0]["result"] + .as_object() + .unwrap() + .keys() + .cloned() + .collect::>(); + keys.sort(); + assert_eq!( + keys, + ["annotation_id", "is_archived", "name", "state", "url"], + "the per-id result stays compact" + ); } #[tokio::test] @@ -570,8 +582,8 @@ async fn update_annotation_updates_each_annotation() { .expect("update_annotation failed"); let successes = structured_field(resp, "successes"); - assert_eq!(successes[0]["result"]["annotation"]["annotationId"], "ann1"); - assert_eq!(successes[1]["result"]["annotation"]["annotationId"], "ann2"); + assert_eq!(successes[0]["result"]["annotation_id"], "ann1"); + assert_eq!(successes[1]["result"]["annotation_id"], "ann2"); } #[tokio::test] @@ -603,14 +615,8 @@ async fn update_annotation_reports_partial_failures_and_continues() { assert_eq!(resp.content.len(), 2); assert!(content.contains("no such annotation")); let body = structured(resp); - assert_eq!( - body["successes"][0]["result"]["annotation"]["annotationId"], - "ann1" - ); - assert_eq!( - body["successes"][1]["result"]["annotation"]["annotationId"], - "ann3" - ); + assert_eq!(body["successes"][0]["result"]["annotation_id"], "ann1"); + assert_eq!(body["successes"][1]["result"]["annotation_id"], "ann3"); assert_eq!(body["successes"][1]["index"], 2); assert_eq!(body["failures"][0]["index"], 1); assert_eq!( @@ -677,7 +683,7 @@ async fn update_annotation_archives_each_annotation() { let successes = structured_field(resp, "successes"); assert_eq!(successes.as_array().unwrap().len(), 2); - assert_eq!(successes[0]["result"]["annotation"]["isArchived"], true); + assert_eq!(successes[0]["result"]["is_archived"], true); } #[tokio::test] @@ -763,14 +769,8 @@ async fn update_annotation_skips_archive_after_partial_field_failure() { assert_eq!(resp.is_error, Some(true)); let body = structured(resp); assert!(body.get("archive_skipped").is_none()); - assert_eq!( - body["successes"][0]["result"]["annotation"]["annotationId"], - "ann1" - ); - assert_eq!( - body["successes"][0]["result"]["annotation"]["isArchived"], - true - ); + assert_eq!(body["successes"][0]["result"]["annotation_id"], "ann1"); + assert_eq!(body["successes"][0]["result"]["is_archived"], true); assert_eq!(body["failures"][0]["index"], 1); } @@ -846,10 +846,7 @@ async fn update_annotation_reports_partial_unarchive_failures() { assert_eq!(resp.is_error, Some(true)); let body = structured(resp); - assert_eq!( - body["successes"][0]["result"]["annotation"]["annotationId"], - "ann1" - ); + assert_eq!(body["successes"][0]["result"]["annotation_id"], "ann1"); assert_eq!(body["failures"][0]["index"], 1); assert_eq!( body["failures"][0]["error"]["code"], @@ -886,8 +883,8 @@ async fn update_annotation_unarchives_each_annotation() { let successes = structured_field(resp, "successes"); assert_eq!(successes.as_array().unwrap().len(), 2); - assert_eq!(successes[0]["result"]["annotation"]["annotationId"], "ann1"); - assert_eq!(successes[1]["result"]["annotation"]["annotationId"], "ann2"); + assert_eq!(successes[0]["result"]["annotation_id"], "ann1"); + assert_eq!(successes[1]["result"]["annotation_id"], "ann2"); } #[tokio::test] @@ -895,7 +892,7 @@ async fn update_annotation_reports_unarchive_ids_not_attempted_after_backend_wid let mut mock = MockAnnotationServiceImpl::new(); mock.expect_unarchive_annotation() .times(50) - .returning(|_| Err(Status::unavailable("backend unavailable"))); + .returning(|_| Err(Status::resource_exhausted("backend unavailable"))); let (server, _h) = server_with_mock(mock).await; diff --git a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs index e7846a92b..964dba3e9 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs @@ -18,7 +18,7 @@ use crate::{ service::artifacts::{ArtifactView, CreateArtifactInput, UpdateArtifactInput, UpdatePath}, tool::common::{ MetadataEntry, - bulk::{self, BulkItems, Op}, + bulk::{BulkItems, Op}, list_body, require_non_empty, to_values, }, }; @@ -819,17 +819,13 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - // An upload can fail after the artifact exists, so every failure may be a partial write. - bulk::run( + // An upload can fail after the artifact exists. + self.bulk_write_partial( Op::Create, - bulk::parse_items::(items)?, - self.selection(selected_indices), - true, + params.0, + "artifact", + Ok, + |_| None, |item| self.create_artifact_item(Parameters(item)), ) .await @@ -970,13 +966,10 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - let items = bulk::prepare( - items, + // An upload can fail after the version is written. + self.bulk_write_partial( + Op::Update, + params.0, "artifact", |mut item: UpdateArtifactItem| { item.artifact_id = item.artifact_id.trim().to_owned(); @@ -984,13 +977,6 @@ impl SiftMcpServer { Ok(item) }, |item| Some(item.artifact_id.clone()), - )?; - // An upload can fail after the version is written, so every failure may be a partial write. - bulk::run( - Op::Update, - items, - self.selection(selected_indices), - true, |item| self.update_artifact_item(item), ) .await diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs index 4c9389425..ec9df3cf3 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/mod.rs @@ -501,8 +501,8 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, archived_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"archived\": boolean, \"calculated_channel\": CalculatedChannel, \"warning\"?: string }`. - `archived` reflects the returned channel state; `warning` appears when the server did not archive it. + `{ \"archived\": true, \"calculated_channel\": CalculatedChannel }`. If the server returns the + channel still unarchived, that item is a failure with `outcome_unknown: true`. Parameters: - `items`: each item takes `calculated_channel_id`, the id to archive. @@ -546,8 +546,8 @@ impl SiftMcpServer { - `{ requested_count, selected_count, not_selected, unarchived_count, successes, failures, not_attempted, next_step }`. `successes` holds `{ index, result }`; `failures` holds `{ index, error, outcome_unknown }`. Any failure sets `isError`. Each result is - `{ \"unarchived\": boolean, \"calculated_channel\": CalculatedChannel, \"warning\"?: string }`. - `unarchived` reflects the returned channel state; `warning` appears when the server did not restore it. + `{ \"unarchived\": true, \"calculated_channel\": CalculatedChannel }`. If the server returns the + channel still archived, that item is a failure with `outcome_unknown: true`. Parameters: - `items`: each item takes `calculated_channel_id`, the id to restore. @@ -613,21 +613,25 @@ impl SiftMcpServer { .map_err(from_anyhow)?; let channel = written.calculated_channel; - let applied = channel.is_archived == archive; - let mut body = serde_json::json!({ - op.state_key(): applied, - "calculated_channel": channel, - }); - if !applied { - body["warning"] = format!( - "The server returned this calculated channel with `is_archived: {}` after the \ - request. Do not report it as {}; verify its state before continuing.", - channel.is_archived, - op.state_key(), - ) - .into(); + if channel.is_archived != archive { + // A Conflict fails only this item and marks its outcome unknown, without + // stopping the rest of the batch. + return Err(ErrorData::internal_error( + format!( + "The server returned calculated channel `{}` with `is_archived: {}` after the \ + request, so it may not be {}. Check it with `list_calculated_channels` \ + before retrying or reporting it.", + channel.calculated_channel_id, + channel.is_archived, + op.state_key(), + ), + Some(serde_json::json!({"reason": "Conflict"})), + )); } - Ok(CallToolResult::structured(body)) + Ok(CallToolResult::structured(serde_json::json!({ + op.state_key(): true, + "calculated_channel": channel, + }))) } } diff --git a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs index 17e6d5145..1818b5864 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs @@ -862,15 +862,14 @@ async fn archive_calculated_channel_surfaces_a_returned_unarchived_state() { .archive_calculated_channel(archive_call("cc1")) .await .expect("archive_calculated_channel failed"); - - let body = structured(resp)["successes"][0]["result"].clone(); - assert_eq!(body["archived"], false); - let warning = body["warning"].as_str().expect("warning"); - assert!(warning.contains("`is_archived: false`"), "{warning}"); - assert!( - warning.contains("Do not report it as archived"), - "{warning}" - ); + assert_eq!(resp.is_error, Some(true)); + let body = structured(resp); + assert_eq!(body["archived_count"], 0); + let failure = &body["failures"][0]; + assert_eq!(failure["outcome_unknown"], true); + let message = failure["error"]["message"].as_str().expect("message"); + assert!(message.contains("`is_archived: false`"), "{message}"); + assert!(message.contains("may not be archived"), "{message}"); } #[tokio::test] @@ -894,15 +893,14 @@ async fn unarchive_calculated_channel_surfaces_a_returned_archived_state() { .unarchive_calculated_channel(archive_call("cc1")) .await .expect("unarchive_calculated_channel failed"); - - let body = structured(resp)["successes"][0]["result"].clone(); - assert_eq!(body["unarchived"], false); - let warning = body["warning"].as_str().expect("warning"); - assert!(warning.contains("`is_archived: true`"), "{warning}"); - assert!( - warning.contains("Do not report it as unarchived"), - "{warning}" - ); + assert_eq!(resp.is_error, Some(true)); + let body = structured(resp); + assert_eq!(body["unarchived_count"], 0); + let failure = &body["failures"][0]; + assert_eq!(failure["outcome_unknown"], true); + let message = failure["error"]["message"].as_str().expect("message"); + assert!(message.contains("`is_archived: true`"), "{message}"); + assert!(message.contains("may not be unarchived"), "{message}"); } #[tokio::test] diff --git a/rust/crates/sift_mcp/src/tool/common/bulk.rs b/rust/crates/sift_mcp/src/tool/common/bulk.rs index 2d24a2211..c00926d30 100644 --- a/rust/crates/sift_mcp/src/tool/common/bulk.rs +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -195,14 +195,29 @@ impl SiftMcpServer { Fut: Future, { let items = prepare(params.items, resource, validate, target)?; - run( - op, - items, - self.selection(params.selected_indices), - false, - write, - ) - .await + let selection = self.selection(params.selected_indices); + run(op, items, selection, false, write).await + } + + /// [`Self::bulk_write`] for an item whose write takes several RPCs, so a failure can + /// follow a committed change. Every failure is then reported as `outcome_unknown`. + pub(crate) async fn bulk_write_partial( + &self, + op: Op, + params: BulkItems, + resource: &str, + validate: impl Fn(T) -> Result, + target: impl Fn(&U) -> Option, + write: F, + ) -> McpResult + where + T: DeserializeOwned + JsonSchema, + F: FnMut(U) -> Fut, + Fut: Future, + { + let items = prepare(params.items, resource, validate, target)?; + let selection = self.selection(params.selected_indices); + run(op, items, selection, true, write).await } } @@ -361,9 +376,10 @@ pub fn report( body.insert("failures".into(), json!(failures)); body.insert("not_attempted".into(), json!(not_attempted)); body.insert("next_step".into(), json!(next_step)); - let body = Value::Object(body); - let text = body.to_string(); - let mut result = CallToolResult::structured(body); + // Successes go last in the text block: when a host truncates a large batch, the + // counts, failures and guidance survive. + let text = super::json_with_key_last(&body, "successes"); + let mut result = CallToolResult::structured(Value::Object(body)); result.is_error = Some(has_errors); result.content = vec![ContentBlock::text(next_step), ContentBlock::text(text)]; Ok(result) @@ -496,6 +512,25 @@ mod test { ); } + #[tokio::test] + async fn the_text_block_writes_successes_after_counts_and_failures() { + let result = run(Op::Update, vec![0, 1], all(), false, |item| async move { + if item == 1 { + Err(ErrorData::resource_not_found("missing", None)) + } else { + Ok(CallToolResult::structured(json!({"item": item}))) + } + }) + .await + .unwrap(); + let text = &result.content[1].as_text().unwrap().text; + crate::tool::common::test_support::assert_keys_before( + text, + &["updated_count", "failures", "not_attempted", "next_step"], + "successes", + ); + } + #[tokio::test] async fn each_op_reports_its_own_count_and_verb() { for (op, key, verb) in [ diff --git a/rust/crates/sift_mcp/src/tool/common/mod.rs b/rust/crates/sift_mcp/src/tool/common/mod.rs index 567bcba97..a1093559a 100644 --- a/rust/crates/sift_mcp/src/tool/common/mod.rs +++ b/rust/crates/sift_mcp/src/tool/common/mod.rs @@ -61,14 +61,21 @@ pub(crate) fn require_non_empty(field: &str, value: &str) -> Result<(), ErrorDat /// Resolve two mutually exclusive optional params into the `(first, second)` request /// fields, with an empty string for the unset one. Returns `INVALID_PARAMS` unless -/// exactly one is set, since the API would silently pick one for the caller. +/// exactly one is set and not blank, since the API would silently pick one for the +/// caller and a blank value would only fail after earlier bulk items were written. pub(crate) fn exactly_one( (first_name, first): (&str, Option), (second_name, second): (&str, Option), ) -> Result<(String, String), ErrorData> { match (first, second) { - (Some(first), None) => Ok((first, String::new())), - (None, Some(second)) => Ok((String::new(), second)), + (Some(first), None) => { + require_non_empty(first_name, &first)?; + Ok((first, String::new())) + } + (None, Some(second)) => { + require_non_empty(second_name, &second)?; + Ok((String::new(), second)) + } (Some(_), Some(_)) => Err(ErrorData::invalid_params( format!("exactly one of `{first_name}` or `{second_name}` must be set, not both"), None, @@ -298,8 +305,7 @@ impl ListBody { /// leads with an instruction; it becomes its own text block ahead of the /// JSON, as in [`read_result`]. pub(crate) fn into_result(self, next_step: Option) -> CallToolResult { - let text = serde_json::to_string(&ItemsLast(&self)) - .unwrap_or_else(|_| Value::Object(self.body.clone()).to_string()); + let text = json_with_key_last(&self.body, self.items_key); let mut content = Vec::with_capacity(2); if let Some(next_step) = next_step { content.push(ContentBlock::text(next_step)); @@ -318,20 +324,29 @@ impl ListBody { } } -/// Serializes a [`ListBody`] with its items key moved to the end. +/// Serialize `body` with `last` moved to the end, so an agent harness that cuts an +/// oversized result from the end loses the bulky items before the counts and +/// guidance. List and bulk write results both use this. +pub(crate) fn json_with_key_last(body: &serde_json::Map, last: &str) -> String { + serde_json::to_string(&KeyLast { body, last }) + .unwrap_or_else(|_| Value::Object(body.clone()).to_string()) +} + /// `SerializeMap` writes entries in the order they are given, which /// `serde_json::Map` would otherwise re-sort. -struct ItemsLast<'a>(&'a ListBody); +struct KeyLast<'a> { + body: &'a serde_json::Map, + last: &'a str, +} -impl Serialize for ItemsLast<'_> { +impl Serialize for KeyLast<'_> { fn serialize(&self, serializer: S) -> Result { - let ListBody { items_key, body } = self.0; - let mut out = serializer.serialize_map(Some(body.len()))?; - for (key, value) in body.iter().filter(|(key, _)| key != items_key) { + let mut out = serializer.serialize_map(Some(self.body.len()))?; + for (key, value) in self.body.iter().filter(|(key, _)| *key != self.last) { out.serialize_entry(key, value)?; } - if let Some(items) = body.get(*items_key) { - out.serialize_entry(items_key, items)?; + if let Some(value) = self.body.get(self.last) { + out.serialize_entry(self.last, value)?; } out.end() } diff --git a/rust/crates/sift_mcp/src/tool/common/test.rs b/rust/crates/sift_mcp/src/tool/common/test.rs index 8d1244c56..264e2dee7 100644 --- a/rust/crates/sift_mcp/src/tool/common/test.rs +++ b/rust/crates/sift_mcp/src/tool/common/test.rs @@ -28,6 +28,14 @@ fn exactly_one_returns_the_set_field_and_rejects_both_or_neither() { "{}", neither.message ); + for (blank, field) in [ + (one(Some(" "), None), "rule_id"), + (one(None, Some("")), "client_key"), + ] { + let error = blank.unwrap_err(); + assert_eq!(error.code, rmcp::model::ErrorCode::INVALID_PARAMS); + assert_eq!(error.message, format!("`{field}` must not be empty")); + } } #[test] diff --git a/rust/crates/sift_mcp/src/tool/reports/mod.rs b/rust/crates/sift_mcp/src/tool/reports/mod.rs index 153b9224b..c1482a24b 100644 --- a/rust/crates/sift_mcp/src/tool/reports/mod.rs +++ b/rust/crates/sift_mcp/src/tool/reports/mod.rs @@ -18,7 +18,7 @@ use crate::{ service::reports::{ReportSource, RuleIdentifier}, tool::common::{ MetadataEntry, - bulk::{self, BulkItems, Op}, + bulk::{BulkItems, Op}, read_result, require_non_empty, url_clause, }, }; @@ -307,17 +307,13 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_create()?; - let Parameters(BulkItems { - items, - selected_indices, - .. - }) = params; - // Evaluation can fail after the report exists, so every failure may be a partial write. - bulk::run( + // Evaluation can fail after the report exists. + self.bulk_write_partial( Op::Create, - bulk::parse_items::(items)?, - self.selection(selected_indices), - true, + params.0, + "report", + Ok, + |_| None, |item| self.create_report_item(Parameters(item)), ) .await @@ -458,7 +454,8 @@ impl SiftMcpServer { params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - self.bulk_write( + // The update commits before the report is read back, so a failed read is a partial write. + self.bulk_write_partial( Op::Update, params.0, "report", diff --git a/rust/crates/sift_mcp/src/tool/reports/test.rs b/rust/crates/sift_mcp/src/tool/reports/test.rs index 26f7b5382..d2dcb344e 100644 --- a/rust/crates/sift_mcp/src/tool/reports/test.rs +++ b/rust/crates/sift_mcp/src/tool/reports/test.rs @@ -65,6 +65,22 @@ mod bulk { handle.abort(); } + #[tokio::test] + async fn a_failed_read_after_a_committed_update_is_an_unknown_outcome() { + let mut mock = MockReportServiceImpl::new(); + mock.expect_update_report() + .times(1) + .returning(|_| Ok(Response::new(UpdateReportResponse {}))); + mock.expect_get_report() + .times(1) + .returning(|_| Err(Status::not_found("deleted after the update"))); + let (server, handle) = server_with_mock(mock).await; + let body = body(call(&server, json!({"items": [item("r1")]})).await.unwrap()); + assert_eq!(body["failures"][0]["error"]["code"], -32002); + assert_eq!(body["failures"][0]["outcome_unknown"], true); + handle.abort(); + } + #[tokio::test] async fn invalid_duplicate_and_missing_chat_mask_fail_before_rpc() { let (server, handle) = server_with_mock(MockReportServiceImpl::new()).await; diff --git a/rust/crates/sift_mcp/src/tool/rules/test.rs b/rust/crates/sift_mcp/src/tool/rules/test.rs index dc3e104dd..3c3af2c82 100644 --- a/rust/crates/sift_mcp/src/tool/rules/test.rs +++ b/rust/crates/sift_mcp/src/tool/rules/test.rs @@ -145,6 +145,14 @@ mod bulk { "item 1", ), (json!([{}]), "item 0"), + ( + json!([{"rule_id": "a"}, {"rule_id": " "}]), + "item 1: `rule_id` must not be empty", + ), + ( + json!([{"rule_id": "a"}, {"client_key": ""}]), + "item 1: `client_key` must not be empty", + ), ( json!([{"rule_id": "a", "typo": true}]), "unknown field `typo`", From d5de4cddaa22202d037ea0d02872ecbfc6e63c05 Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Fri, 13 Nov 2026 15:51:35 -0800 Subject: [PATCH 18/19] docs: drop stale artifact relay comments --- rust/crates/sift_mcp/CLAUDE.md | 3 +-- rust/crates/sift_mcp/src/tool/artifacts/mod.rs | 4 ---- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/rust/crates/sift_mcp/CLAUDE.md b/rust/crates/sift_mcp/CLAUDE.md index 128a8a061..113b49c9c 100644 --- a/rust/crates/sift_mcp/CLAUDE.md +++ b/rust/crates/sift_mcp/CLAUDE.md @@ -295,8 +295,7 @@ pub async fn list_webhooks(&self, params: Parameters) -> error::McpR item takes several RPCs, so a failure can follow a committed change (`create_artifact`, `update_report`). The call-level `next_step` covers the batch, so an item result carries no `next_step` of its own, - only a `warning` when that item needs the user's attention. Artifact results are the one - exception: the Sift chat host matches `Created artifact ` in them. Do not repeat the + only a `warning` when that item needs the user's attention. Do not repeat the `selected_indices` or `items` bounds in the description; the `BulkItems` schema documents them. - **Read tools keep their payload in `content`.** Clients such as Claude Code show the model `content`, not `structuredContent`. Replacing `content` with `next_step` alone hides the payload diff --git a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs index 964dba3e9..457b6cd6c 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/mod.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/mod.rs @@ -269,10 +269,6 @@ fn download_warning(artifact: &ArtifactView, uploaded: bool) -> Option<&'static /// One bulk write result: the artifact, the name the UI shows for it, its Sift link, a /// one-line `next_step`, and a `warning` when one applies. -/// -/// Unlike other bulk results, artifact results keep a per-item `next_step`: the Sift chat -/// host finds the artifacts to show by matching `Created artifact ` and -/// `artifact ` in the tool result, so this text is a host contract. fn artifact_result( server: &SiftMcpServer, artifact: &ArtifactView, From 5ca9210a3177cf0587e6038bf64c2f306f7ae44a Mon Sep 17 00:00:00 2001 From: Liam Neville Date: Fri, 13 Nov 2026 16:51:01 -0800 Subject: [PATCH 19/19] test: stop the artifact bulk test on a code that is not retried --- rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs index 76dd4f568..193de206a 100644 --- a/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs +++ b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs @@ -121,10 +121,11 @@ async fn resource_errors_continue_and_backend_errors_stop() { tonic::Code::NotFound, tonic::Code::PermissionDenied, tonic::Code::FailedPrecondition, - tonic::Code::Unavailable, + // Not retried by `with_retry`, so `.times` below counts each write once. + tonic::Code::ResourceExhausted, ] { let mut mock = MockArtifactServiceImpl::new(); - let stop = code == tonic::Code::Unavailable; + let stop = code == tonic::Code::ResourceExhausted; match op { "archive" => { mock.expect_archive_artifact()