diff --git a/rust/crates/sift_cli/CHANGELOG.md b/rust/crates/sift_cli/CHANGELOG.md index 44b8cd35b..8b230fcc0 100644 --- a/rust/crates/sift_cli/CHANGELOG.md +++ b/rust/crates/sift_cli/CHANGELOG.md @@ -7,6 +7,33 @@ 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. +- `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`. 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 + 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_cli/assets/skills/sift/SKILL.md b/rust/crates/sift_cli/assets/skills/sift/SKILL.md index d2049e945..fa0990644 100644 --- a/rust/crates/sift_cli/assets/skills/sift/SKILL.md +++ b/rust/crates/sift_cli/assets/skills/sift/SKILL.md @@ -120,9 +120,13 @@ tool. available in 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. `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`, @@ -131,7 +135,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 diff --git a/rust/crates/sift_mcp/CLAUDE.md b/rust/crates/sift_mcp/CLAUDE.md index 19fe99920..113b49c9c 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. 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. 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/error/mod.rs b/rust/crates/sift_mcp/src/error/mod.rs index fd784e564..db988a0fe 100644 --- a/rust/crates/sift_mcp/src/error/mod.rs +++ b/rust/crates/sift_mcp/src/error/mod.rs @@ -118,15 +118,19 @@ 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()); + 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/service/annotations/mod.rs b/rust/crates/sift_mcp/src/service/annotations/mod.rs index 7732b26ec..cc7d76e63 100644 --- a/rust/crates/sift_mcp/src/service/annotations/mod.rs +++ b/rust/crates/sift_mcp/src/service/annotations/mod.rs @@ -1,19 +1,18 @@ use crate::policy::{RetryPolicy, once, with_retry}; use crate::service::common; -use anyhow::{Context, Error, Result, anyhow}; +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; @@ -21,18 +20,19 @@ mod test; const MAX_CONCURRENT_ANNOTATION_UPDATES: usize = 50; #[derive(Debug)] -pub struct AnnotationUpdateFailure { +pub struct AnnotationUpdateFailure { pub annotation_id: String, - pub error: Error, + pub error: E, } #[derive(Debug)] -pub struct UpdateAnnotationsResult { +pub struct UpdateAnnotationsResult { pub annotations: Vec, - pub failures: 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, - pub batch_archive_error: Option, - pub archive_skipped: bool, } async fn fan_out_bounded(items: Vec, concurrency: usize, mut op: F) -> Vec @@ -52,21 +52,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,30 +208,30 @@ 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")) } + /// 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, @@ -259,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() @@ -269,136 +255,90 @@ 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 updated_ids = 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); + updated_ids.push(annotation_id); + } + Err(error) => { + let (error, stops) = classify(error); + stop |= stops; + 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, + updated_ids, failures, not_attempted, - batch_archive_error, - archive_skipped, }) } @@ -489,7 +429,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 +437,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..d6605e61b 100644 --- a/rust/crates/sift_mcp/src/service/annotations/test.rs +++ b/rust/crates/sift_mcp/src/service/annotations/test.rs @@ -7,15 +7,31 @@ 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}; 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<()>) { @@ -326,55 +342,195 @@ 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 batch archive annotations") - ); + 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), + classify, + ) + .await + .unwrap(); + assert_eq!(outcome.annotations.len(), 2); + assert_eq!(outcome.failures.len(), 1); + assert_eq!(outcome.failures[0].annotation_id, "ann2"); + } +} + +#[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), + classify, + ) + .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::resource_exhausted("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), + classify, + ) + .await + .unwrap(); + assert_eq!(outcome.failures.len(), 50); + assert_eq!(outcome.not_attempted, ids[50..]); } #[tokio::test] @@ -405,6 +561,7 @@ async fn update_annotations_collects_failures_and_continues() { None, None, None, + classify, ) .await .expect("bulk update failed"); @@ -444,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/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/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/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/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/annotations/mod.rs b/rust/crates/sift_mcp/src/tool/annotations/mod.rs index 2e3c566d3..f64dd0b99 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, }; @@ -11,11 +11,14 @@ 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, - tool::common::{MetadataEntry, list_body, url_clause, with_urls}, + tool::common::{MetadataEntry, list_body, with_urls}, }; #[cfg(test)] @@ -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, @@ -113,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, @@ -123,8 +118,12 @@ pub struct CreateAnnotationParams { } #[derive(Debug, Deserialize, JsonSchema)] +#[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, start_time_unix_nanos: Option, @@ -234,21 +233,20 @@ 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. `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. @@ -263,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 @@ -420,79 +415,63 @@ 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( 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_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`. 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. - `name`: optional new name. - `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. - `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 +490,7 @@ impl SiftMcpServer { let Parameters(UpdateAnnotationParams { annotation_ids, + selected_indices, name, description, start_time_unix_nanos, @@ -523,6 +503,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 +524,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 +551,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( @@ -578,91 +574,59 @@ 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 updated_count = outcome.annotations.len(); + // A compact result per id keeps 100 updates well inside a chat host's output cap. + let successes = outcome + .updated_ids + .iter() + .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_id": annotation.annotation_id, + "name": annotation.name, + "state": state, + "is_archived": annotation.is_archived, + "url": url, + })), + ) + }) + .collect(); 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| { - 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." - ) - } 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) + .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, + 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 54b2128b3..1148ae7d7 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; @@ -110,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"); @@ -168,6 +183,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 +197,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"]["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::resource_exhausted("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,12 +540,24 @@ 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"]["name"], "renamed"); assert_eq!( - annotations[0]["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] @@ -408,9 +581,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_id"], "ann1"); + assert_eq!(successes[1]["result"]["annotation_id"], "ann2"); } #[tokio::test] @@ -439,16 +612,16 @@ 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["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!( - body["failures"][0]["message"], - "Some requested entity was not found: no such annotation" + body["failures"][0]["error"]["code"], + ErrorCode::RESOURCE_NOT_FOUND.0 ); assert_eq!(body["not_attempted"], serde_json::json!([])); } @@ -475,37 +648,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 +681,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"]["is_archived"], true); } #[tokio::test] @@ -540,21 +703,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 +741,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 +768,28 @@ 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_id"], "ann1"); + assert_eq!(body["successes"][0]["result"]["is_archived"], 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 +800,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"]) - ); - assert_eq!( - body["batch_archive_error"]["message"], - "Some requested entity was not found: one target was not found" + body["failures"][0]["error"]["data"]["reason"], + "PermissionDenied" ); - 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 +846,29 @@ 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["successes"][0]["result"]["annotation_id"], "ann1"); + assert_eq!(body["failures"][0]["index"], 1); 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 ); - 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 +881,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_id"], "ann1"); + assert_eq!(successes[1]["result"]["annotation_id"], "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::resource_exhausted("backend unavailable"))); let (server, _h) = server_with_mock(mock).await; @@ -741,32 +910,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 +974,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 +1034,47 @@ 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 ); } + +#[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); +} 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..193de206a --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/artifacts/bulk_test.rs @@ -0,0 +1,224 @@ +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_eq!( + body["successes"][0]["result"]["next_step"], + "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::FailedPrecondition, + // 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::ResourceExhausted; + match op { + "archive" => { + mock.expect_archive_artifact() + .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() + .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() + .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; + 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..457b6cd6c 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,12 +12,15 @@ use sift_rs::artifacts::v1::{ ArtifactLinkRelation, ArtifactStorageClass, ArtifactVersion, }; -use crate::tool::common::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::{BulkItems, Op}, + list_body, require_non_empty, to_values, + }, }; #[cfg(test)] @@ -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,34 +228,63 @@ fn parse_links(links: Option>) -> Result &'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." +fn validate_update_artifact(item: &UpdateArtifactItem) -> Result<(), ErrorData> { + 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() + && 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(()) +} + +/// 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. 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. @@ -571,26 +603,30 @@ 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`, 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: - - `artifact_id`: required stable artifact id. + - `items`: each item takes `artifact_id`, the stable artifact id. 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,63 +638,39 @@ impl SiftMcpServer { )] pub async fn archive_artifact( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(ArtifactArchiveParams { 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}`.{} Tell the user it is hidden from default artifact \ - listings and that `unarchive_artifact` restores it.", - 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( 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`, 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: - - `artifact_id`: required stable artifact id. + - `items`: each item takes `artifact_id`, the stable artifact id. 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,38 +681,50 @@ impl SiftMcpServer { )] pub async fn unarchive_artifact( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - let Parameters(ArtifactArchiveParams { 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, - )); - } + self.set_artifacts_archived(params, Op::Unarchive).await + } - 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_artifacts_archived( + &self, + params: Parameters>, + op: Op, + ) -> error::McpResult { + self.bulk_write( + op, + 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 + } - let next_step = format!( - "Unarchived artifact `{artifact_id}`.{} Tell the user it is restored to default artifact \ - listings.", - url_clause("artifact", None, artifact_url.as_deref()), - ); - let mut result = CallToolResult::structured(serde_json::json!({ + 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)?; + + 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( @@ -717,39 +741,37 @@ 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 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 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`. @@ -773,7 +795,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 @@ -790,20 +812,17 @@ impl SiftMcpServer { )] pub async fn create_artifact( &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_with_partial_writes( - items, - self.selection(selected_indices), + // An upload can fail after the artifact exists. + self.bulk_write_partial( + Op::Create, + params.0, + "artifact", + Ok, + |_| None, |item| self.create_artifact_item(Parameters(item)), - true, ) .await } @@ -862,37 +881,40 @@ 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( 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: - - `{ \"artifact\": Artifact, \"artifact_url\": string|null, \"next_step\": string }`. Same + - `{ 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, \"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`: 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. @@ -917,8 +939,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,26 +959,34 @@ impl SiftMcpServer { )] pub async fn update_artifact( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + // 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(); + validate_update_artifact(&item)?; + Ok(item) + }, + |item| Some(item.artifact_id.clone()), + |item| self.update_artifact_item(item), + ) + .await + } - let Parameters(UpdateArtifactParams { + 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(); @@ -971,12 +1006,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 @@ -993,19 +1022,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 {}.{}{} 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), - 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 e3f53847e..05db4324b 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::{ @@ -18,18 +21,24 @@ use sift_test_util::{ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; +use serde_json::{Value, json}; + use super::{ - ArtifactArchiveParams, ArtifactVersionListParams, CreateArtifactItem, DownloadArtifactParams, - UpdateArtifactParams, display_name, parse_created_via, parse_storage_class, + 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, + }, }, }, }; @@ -424,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(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(), @@ -452,98 +457,28 @@ async fn unarchive_artifact_blocked_without_allow_destructive() { false, ) .await; - let err = server - .unarchive_artifact(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(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(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(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(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") @@ -553,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(Parameters(ArtifactArchiveParams { - artifact_id: " art-1\n".into(), - })) - .await - .expect("archive"); - let unarchived = server - .unarchive_artifact(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}"); } } @@ -594,12 +520,7 @@ async fn update_artifact_blocked_without_allow_destructive() { false, ) .await; - let err = server - .update_artifact(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); @@ -639,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(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 { @@ -691,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(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", ), ] { @@ -762,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(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(Parameters(params)) - .await - .expect_err("rejected"); + let err = update_one(&server, item).await.expect_err("rejected"); assert_eq!(err.code, ErrorCode::INVALID_PARAMS); } } @@ -914,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] @@ -994,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] @@ -1123,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/bulk_test.rs b/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs new file mode 100644 index 000000000..8816f86a6 --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/assets/bulk_test.rs @@ -0,0 +1,65 @@ +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 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..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, }; @@ -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::{BulkItems, Op}, + list_body, require_non_empty, with_urls, + }, }; #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateAssetParams { +pub struct UpdateAssetItem { asset_id: String, tags: Option>, metadata: Option>, @@ -24,6 +28,17 @@ pub struct UpdateAssetParams { #[cfg(test)] mod test; +fn validate_update_asset(item: &UpdateAssetItem) -> Result<(), ErrorData> { + 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", + None, + )); + } + Ok(()) +} + #[tool_router(router = assets_router, vis = "pub(crate)")] impl SiftMcpServer { #[tool( @@ -102,14 +117,18 @@ 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: - - `{ \"asset\": Asset, \"asset_url\": string|null, \"next_step\": string }`. The returned `Asset` is + - `{ 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 }`. 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`: 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 @@ -130,8 +149,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,27 +170,31 @@ 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()?; + self.bulk_write( + Op::Update, + params.0, + "asset", + |item: UpdateAssetItem| { + validate_update_asset(&item)?; + Ok(item) + }, + |item| Some(item.asset_id.clone()), + |item| self.update_asset_item(item), + ) + .await + } - let Parameters(UpdateAssetParams { + async fn update_asset_item(&self, item: UpdateAssetItem) -> error::McpResult { + let 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, - )); - } + } = item; let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); @@ -177,21 +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 `{}` ({}).{} 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.", - 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 b96fe88c0..3c17f11a1 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}; @@ -7,10 +10,7 @@ use tonic::{Response, Status, transport::Server}; use crate::{ server::SiftMcpServer, service::common::DEFAULT_LIMIT, - tool::{ - assets::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<()>) { @@ -242,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(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 ce365c159..ec9df3cf3 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,14 +10,17 @@ use sift_rs::{ calculated_channels::v2::CalculatedChannelAbstractChannelReference, metadata::v1::MetadataValue, }; -use crate::tool::common::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)] @@ -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, } @@ -225,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 @@ -264,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\"`. @@ -290,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 @@ -327,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 } @@ -405,33 +403,28 @@ 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( 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\": [...], - \"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. + - `{ 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\": [...], + \"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: - - `calculated_channel_id`: required. The calculated channel to update. + - `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. - `units`: optional. New units. @@ -449,6 +442,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 +456,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,83 +470,25 @@ 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 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 {}.{} Surface the new definition to the user \ - and confirm it matches their intent.", - written.calculated_channel.calculated_channel_id, - written.calculated_channel.version, - inapplicable_clause(&written), - ); - - Ok(write_result(written, next_step, None)) + self.bulk_write( + Op::Update, + params.0, + "calculated channel", + calculated_channel_update, + |(id, _)| Some(id.clone()), + |(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 } #[tool( @@ -558,19 +498,24 @@ impl SiftMcpServer { Reversible with `unarchive_calculated_channel`. Output: - - `{ \"archived\": boolean, \"calculated_channel\": CalculatedChannel, \"next_step\": string }`. - `archived` reflects the returned channel 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, \"calculated_channel\": CalculatedChannel }`. If the server returns the + channel still unarchived, that item is a failure with `outcome_unknown: true`. Parameters: - - `calculated_channel_id`: required. The calculated channel to archive. + - `items`: each item takes `calculated_channel_id`, the id to archive. 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,42 +530,11 @@ impl SiftMcpServer { )] pub async fn archive_calculated_channel( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(CalculatedChannelArchiveParams { - calculated_channel_id, - }) = params; - - require_id(&calculated_channel_id)?; - - 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 `{}`. Tell the user it is archived and no longer offered \ - for plotting or querying, and that `unarchive_calculated_channel` restores it.", - 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( @@ -629,19 +543,24 @@ 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 }`. - `unarchived` reflects the returned channel 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, \"calculated_channel\": CalculatedChannel }`. If the server returns the + channel still archived, that item is a failure with `outcome_unknown: true`. Parameters: - - `calculated_channel_id`: required. The calculated channel to restore. + - `items`: each item takes `calculated_channel_id`, the id to restore. 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,53 +573,120 @@ impl SiftMcpServer { )] pub async fn unarchive_calculated_channel( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(CalculatedChannelArchiveParams { - calculated_channel_id, - }) = params; - - require_id(&calculated_channel_id)?; - - let written = self - .calculated_channel_service - .unarchive_calculated_channel(calculated_channel_id) + self.set_calculated_channels_archived(params, Op::Unarchive) .await - .map_err(from_anyhow)?; + } - 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.", - written.calculated_channel.calculated_channel_id, - ) + async fn set_calculated_channels_archived( + &self, + params: Parameters>, + op: Op, + ) -> error::McpResult { + self.bulk_write( + op, + params.0, + "calculated channel", + |item: CalculatedChannelArchiveItem| { + 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 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)?; - Ok(write_result( - written, - next_step, - Some(("unarchived", unarchived)), - )) + let channel = written.calculated_channel; + 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(serde_json::json!({ + op.state_key(): true, + "calculated_channel": channel, + }))) } } -/// Reject an empty `calculated_channel_id` before any RPC. -fn require_id(calculated_channel_id: &str) -> Result<(), ErrorData> { - if calculated_channel_id.is_empty() { +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_non_empty("calculated_channel_id", &calculated_channel_id)?; + if expression.is_some() != expression_channel_references_json.is_some() { return Err(ErrorData::invalid_params( - "`calculated_channel_id` must not be empty", + "`expression` and `expression_channel_references_json` must be provided together", None, )); } - Ok(()) + 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)) } /// Deserialize the JSON-string channel reference array, mapping any parse error @@ -745,41 +731,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 034155a4f..1818b5864 100644 --- a/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs +++ b/rust/crates/sift_mcp/src/tool/calculated_channels/test.rs @@ -12,13 +12,13 @@ use tokio::task::JoinHandle; use tonic::{Response, Status, transport::Server}; use super::{ - CalculatedChannelArchiveParams, CalculatedChannelVersionListParams, - CreateCalculatedChannelItem, UpdateCalculatedChannelParams, + CalculatedChannelVersionListParams, CreateCalculatedChannelItem, UpdateCalculatedChannelItem, }; 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, }, }; @@ -76,8 +76,192 @@ 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> { + bulk_params(serde_json::json!([{ "calculated_channel_id": id }]), None) +} + +fn update_params() -> UpdateCalculatedChannelItem { + UpdateCalculatedChannelItem { calculated_channel_id: "cc1".into(), name: None, description: None, @@ -340,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] @@ -429,7 +618,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 +631,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 +649,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 +669,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 +685,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 +701,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,14 +736,14 @@ 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()); + assert!(body.get("next_step").is_none(), "{body}"); } #[tokio::test] @@ -563,9 +752,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 +766,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,16 +798,14 @@ 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()); + assert!(body.get("warning").is_none(), "{body}"); } #[tokio::test] @@ -649,15 +832,13 @@ 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()); + assert!(body.get("warning").is_none(), "{body}"); } #[tokio::test] @@ -678,15 +859,17 @@ 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"); - + assert_eq!(resp.is_error, Some(true)); let body = structured(resp); - assert_eq!(body["archived"], false); - assert!(body["next_step"].as_str().unwrap().contains("unarchived")); + 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] @@ -707,15 +890,17 @@ 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"); - + assert_eq!(resp.is_error, Some(true)); let body = structured(resp); - assert_eq!(body["unarchived"], false); - assert!(body["next_step"].as_str().unwrap().contains("archived")); + 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] @@ -723,9 +908,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"); 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..c00926d30 --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/common/bulk.rs @@ -0,0 +1,626 @@ +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, server::SiftMcpServer, service::batch}; + +pub const MAX_ITEMS: usize = 100; + +#[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; + /// omitted means every item. 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, + }) + } +} + +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()) +} + +pub fn at_item(index: usize, error: ErrorData) -> ErrorData { + ErrorData::new( + error.code, + format!("`items` item {index}: {}", error.message), + error.data, + ) +} + +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> { + 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() +} + +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(()) +} + +#[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, +} + +#[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", + } + } + + /// 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)?; + 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 + } +} + +fn reason(error: &ErrorData) -> Option<&str> { + error.data.as_ref()?.get("reason")?.as_str() +} + +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" | "FailedPrecondition") => op == Op::Create, + _ => true, + } +} + +#[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 { + 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() + } +} + +pub fn select(count: usize, selection: Selection) -> Result { + 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..count).collect(), + }; + let mut selected = vec![false; count]; + for &index in &selected_indices { + if index >= count || selected[index] { + return Err(ErrorData::invalid_params( + "`selected_indices` must contain unique, in-range input indices", + None, + )); + } + selected[index] = true; + } + 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 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, + ) +} + +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": index, "result": result.structured_content})) + .collect::>(); + let failures = failures + .into_iter() + .map(|(index, error)| { + let outcome_unknown = partial_writes + || (error.code != ErrorCode::INVALID_PARAMS + && error.code != ErrorCode::RESOURCE_NOT_FOUND + && !matches!( + reason(&error), + Some( + "AlreadyExists" + | "PermissionDenied" + | "Unauthenticated" + | "FailedPrecondition" + ) + )); + json!({"index": 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. 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, \ + 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!(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)); + body.insert("not_attempted".into(), json!(not_attempted)); + body.insert("next_step".into(), json!(next_step)); + // 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) +} + +#[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:?}"); + } + } + } + + #[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 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 [ + (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 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] + 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..b1f6364ea 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, 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, @@ -358,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 998e387ff..a1093559a 100644 --- a/rust/crates/sift_mcp/src/tool/common/mod.rs +++ b/rust/crates/sift_mcp/src/tool/common/mod.rs @@ -6,6 +6,7 @@ use std::{ }; use anyhow::Context; +pub(crate) mod bulk; pub(crate) mod bulk_create; use rmcp::ErrorData; @@ -47,6 +48,45 @@ 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 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) => { + 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, + )), + (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 @@ -265,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)); @@ -285,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() } @@ -539,6 +587,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..264e2dee7 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,46 @@ 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 + ); + 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] +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 84bbe9911..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,19 +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_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)] @@ -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,54 @@ 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; + 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)), + (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( @@ -142,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 @@ -164,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` @@ -196,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 } @@ -229,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), @@ -272,42 +316,29 @@ 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( 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 }`. 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`: 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. - `tag_names`: optional REPLACEMENT list of tag names. Passing this overwrites the template's full @@ -326,12 +357,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,99 +383,38 @@ impl SiftMcpServer { )] pub async fn update_report_template( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + self.bulk_write( + Op::Update, + params.0, + "report template", + template_update, + |(id, _)| Some(id.clone()), + |(id, changes)| self.update_report_template_item(id, changes), + ) + .await + } - 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, - )); - } - + 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) .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 `{}` ({}).{}{} Surface the new state to the user and confirm nothing \ - was unintentionally dropped — `tags`, `rules`, and `metadata` use REPLACE semantics.", - 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 4956696d6..d0e4092f5 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,93 @@ 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 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, @@ -182,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"); } @@ -257,20 +334,15 @@ 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"); - 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"); } @@ -284,7 +356,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 +368,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 +384,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 +414,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"); diff --git a/rust/crates/sift_mcp/src/tool/reports/mod.rs b/rust/crates/sift_mcp/src/tool/reports/mod.rs index fc1d03a19..c1482a24b 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, }; @@ -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::{BulkItems, Op}, + read_result, require_non_empty, url_clause, + }, }; #[cfg(test)] @@ -87,7 +91,7 @@ pub struct CreateReportItem { } #[derive(Debug, Deserialize, JsonSchema)] -pub struct UpdateReportParams { +pub struct UpdateReportItem { report_id: String, metadata: Vec, } @@ -250,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. @@ -270,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, @@ -304,20 +304,17 @@ impl SiftMcpServer { )] pub async fn create_report( &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_with_partial_writes( - items, - self.selection(selected_indices), + // Evaluation can fail after the report exists. + self.bulk_write_partial( + Op::Create, + params.0, + "report", + Ok, + |_| None, |item| self.create_report_item(Parameters(item)), - true, ) .await } @@ -341,15 +338,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), @@ -403,44 +393,31 @@ 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( 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 }`. 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`: 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. Pass `[]` to clear all metadata. A `name` that does not yet exist in the organization's @@ -451,12 +428,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 +449,37 @@ 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 { self.require_destructive()?; + // 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", + |item: UpdateReportItem| { + require_non_empty("report_id", &item.report_id)?; + Ok(( + item.report_id, + item.metadata + .into_iter() + .map(MetadataValue::from) + .collect::>(), + )) + }, + |(id, _)| Some(id.clone()), + |(id, metadata)| self.update_report_item(id, metadata), + ) + .await + } - 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 - .into_iter() - .map(MetadataValue::from) - .collect::>(); - + async fn update_report_item( + &self, + report_id: String, + metadata: Vec, + ) -> error::McpResult { let report = self .report_service .update_report(report_id, metadata) @@ -494,20 +487,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 `{}` ({}).{} Surface the new state to the user and confirm \ - nothing was unintentionally dropped — metadata is a REPLACE operation.", - 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/reports/test.rs b/rust/crates/sift_mcp/src/tool/reports/test.rs index 457582ba0..d2dcb344e 100644 --- a/rust/crates/sift_mcp/src/tool/reports/test.rs +++ b/rust/crates/sift_mcp/src/tool/reports/test.rs @@ -18,7 +18,91 @@ 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 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; + 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 +637,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 +655,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"); diff --git a/rust/crates/sift_mcp/src/tool/rules/mod.rs b/rust/crates/sift_mcp/src/tool/rules/mod.rs index 170fcb0c7..25d2f2ec5 100644 --- a/rust/crates/sift_mcp/src/tool/rules/mod.rs +++ b/rust/crates/sift_mcp/src/tool/rules/mod.rs @@ -1,19 +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_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)] @@ -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, } @@ -214,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). @@ -232,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. @@ -263,101 +262,87 @@ 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( 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 fields of existing Sift rules, preserving omitted fields. This is a WRITE. 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: `{ 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 }`. `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`: one object per rule, each with: + - `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. 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,95 +351,67 @@ 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()?; + self.bulk_write( + Op::Update, + params.0, + "rule", + rule_update, + |(rule_id, _)| Some(rule_id.clone()), + |(rule_id, changes)| self.update_rule_item(rule_id, changes), + ) + .await + } - let Parameters(UpdateRuleParams { - rule_id, - name, - description, - version_notes, - is_live_evaluation_enabled, - asset_ids, - tag_ids, - conditions_json, - }) = params; - - 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, - )); - } - - let rule_name = changes.name.clone(); + async fn update_rule_item(&self, rule_id: String, changes: RuleUpdate) -> error::McpResult { 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}`.{} 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}\"`).", - 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( 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: `{ 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: - - `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`: 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. 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 +420,44 @@ 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: `{ 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: - - `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`: 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. 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 +466,108 @@ 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, - }) = params; - - let (rule_id, client_key) = rule_identifier(rule_id, client_key)?; - - let rule_url = rule_url_for(self, &rule_id); + async fn set_rules_archived( + &self, + params: Parameters>, + op: Op, + ) -> error::McpResult { + self.bulk_write( + op, + params.0, + "rule", + |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}") + } else { + format!("rule_id:{rule_id}") + }) + }, + |(rule_id, client_key)| self.set_rule_archived(rule_id, client_key, op), + ) + .await + } - self.rule_service - .unarchive_rule(rule_id, client_key) + 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 write = if op == Op::Archive { + 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)?; + + Ok(CallToolResult::structured(serde_json::json!({ + "rule_id": rule.rule_id, + "name": rule.name, + op.state_key(): 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; + require_non_empty("rule_id", &rule_id)?; + 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 @@ -587,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 c07f453a8..3c3af2c82 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() { @@ -44,3 +15,207 @@ 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 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"}, {"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`", + ), + ] { + 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 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(); + } +} 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..f86226d66 --- /dev/null +++ b/rust/crates/sift_mcp/src/tool/runs/bulk_test.rs @@ -0,0 +1,64 @@ +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 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..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, }; @@ -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::{BulkItems, Op}, + list_body, require_non_empty, 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,25 @@ pub struct UpdateRunParams { metadata: Option>, } +fn validate_update_run(item: &UpdateRunItem) -> Result<(), ErrorData> { + require_non_empty("run_id", &item.run_id)?; + 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 +142,19 @@ 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: - - `{ \"run\": Run, \"run_url\": string|null, \"next_step\": string }`. The returned `Run` is the + - `{ 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 }`. 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`: 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. @@ -144,11 +171,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 +189,27 @@ 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()?; + self.bulk_write( + Op::Update, + params.0, + "run", + |item: UpdateRunItem| { + validate_update_run(&item)?; + Ok(item) + }, + |item| Some(item.run_id.clone()), + |item| self.update_run_item(item), + ) + .await + } - let Parameters(UpdateRunParams { + async fn update_run_item(&self, item: UpdateRunItem) -> error::McpResult { + let UpdateRunItem { run_id, name, description, @@ -170,29 +219,7 @@ impl SiftMcpServer { client_key, tags, 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, - )); - } + } = item; let metadata = metadata.map(|m| m.into_iter().map(MetadataValue::from).collect::>()); @@ -213,20 +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 `{}` ({}).{} Surface the new state to the user and confirm the change matches their \ - intent. Remember: tags and metadata are REPLACE operations.", - 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 cc33564d8..c80a5a2db 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,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::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; @@ -212,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(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(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(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 daefbb7f0..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,12 +11,15 @@ use sift_rs::{ metadata::v1::MetadataValue, }; -use crate::tool::common::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)] @@ -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, } @@ -224,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 @@ -261,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 @@ -285,14 +291,11 @@ 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 - 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 @@ -321,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 } @@ -352,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(); @@ -379,36 +374,30 @@ 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( 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 }`. 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`: 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 ignores any other field in the same call, so this tool rejects `name` combined with another @@ -424,6 +413,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 +426,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,81 +444,34 @@ impl SiftMcpServer { )] pub async fn update_user_defined_function( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; + self.bulk_write( + Op::Update, + params.0, + "user defined function", + udf_update, + |(id, _)| Some(id.clone()), + |(id, changes)| self.update_user_defined_function_item(id, changes), + ) + .await + } - 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, - )); - } - + 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) .await .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.", - 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( @@ -534,18 +481,22 @@ 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 }`. Parameters: - - `user_defined_function_id`: required. The function to archive. + - `items`: each item takes `user_defined_function_id`, the id to archive. 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,29 +513,10 @@ impl SiftMcpServer { )] pub async fn archive_user_defined_function( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(ArchiveUserDefinedFunctionParams { - user_defined_function_id, - }) = params; - - 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.", - 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( @@ -593,18 +525,22 @@ 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 }`. Parameters: - - `user_defined_function_id`: required. The function to unarchive. + - `items`: each item takes `user_defined_function_id`, the id to restore. 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,73 +553,86 @@ impl SiftMcpServer { )] pub async fn unarchive_user_defined_function( &self, - params: Parameters, + params: Parameters>, ) -> error::McpResult { self.require_destructive()?; - - let Parameters(ArchiveUserDefinedFunctionParams { - user_defined_function_id, - }) = params; - - 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.", - 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) + self.set_udfs_archived(params, Op::Unarchive).await } } 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( + /// `is_archived` field through the update mask. + async fn set_udfs_archived( &self, - user_defined_function_id: String, - is_archived: bool, - ) -> Result { - if user_defined_function_id.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) + params: Parameters>, + op: Op, + ) -> error::McpResult { + self.bulk_write( + op, + params.0, + "user defined function", + |item: ArchiveUserDefinedFunctionItem| { + require_non_empty("user_defined_function_id", &item.user_defined_function_id)?; + Ok(item.user_defined_function_id) + }, + |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 } } -/// 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", +fn udf_update(item: UpdateUserDefinedFunctionItem) -> Result<(String, UdfUpdate), ErrorData> { + let UpdateUserDefinedFunctionItem { + user_defined_function_id, + name, + description, + expression, + function_inputs_json, + metadata, + } = item; + require_non_empty("user_defined_function_id", &user_defined_function_id)?; + 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, - )), - (None, None) => Err(ErrorData::invalid_params( - "one of `user_defined_function_id` or `name` must be set", + )); + } + 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)) } fn validate_function_name(name: &str) -> Result<(), ErrorData> { 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..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 @@ -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) { @@ -434,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) @@ -472,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] @@ -600,13 +773,13 @@ 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"], "v4" ); - assert!(body["next_step"].as_str().expect("next_step").contains("4")); + assert!(body.get("next_step").is_none(), "{body}"); } #[tokio::test] @@ -679,7 +852,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 +886,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); }