Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(ai-tools): reject unsupported root schema composition (#9515)
gbirmandc32998db0b5
diff --git a/crates/agent_code_mode/AGENTS.md b/crates/agent_code_mode/AGENTS.mdindex f784c6c875..8b9c9fb676 100644--- a/crates/agent_code_mode/AGENTS.md+++ b/crates/agent_code_mode/AGENTS.md@@ -78,6 +78,8 @@ longer delivery state.- `cargo test -p agent_code_mode --lib` from the repository root, with live localdatabase configuration and `SQLX_OFFLINE` unset.+- The review toolset test builds every review tool through shared provider schema+ validation.- Keep generated discovery topics within the classifier's 32-question limit.- Regenerate SDK documentation through `packages/code-mode-sdk`; never edit`src/domain/documentation.json` by hand.diff --git a/crates/agent_code_mode/src/inbound/reviews/test.rs b/crates/agent_code_mode/src/inbound/reviews/test.rsindex 79f7a36f09..932d3b726e 100644--- a/crates/agent_code_mode/src/inbound/reviews/test.rs+++ b/crates/agent_code_mode/src/inbound/reviews/test.rs@@ -2,6 +2,11 @@ use super::PresentReview;use ai_toolset::schema::generate_validated_input_schema;use serde_json::json;+#[test]+fn review_toolset_passes_schema_validation() {+ let _ = super::toolset();+}+/// Anthropic rejects every request whose tools include a root `oneOf`,/// `allOf` or `anyOf`, and PresentReview ships to every harness.#[test]diff --git a/crates/ai_toolset/AGENTS.md b/crates/ai_toolset/AGENTS.mdindex ca9b574164..a93369cb8e 100644--- a/crates/ai_toolset/AGENTS.md+++ b/crates/ai_toolset/AGENTS.md@@ -52,6 +52,9 @@ deliberately stable and widely depended on.- Every tool must declare MCP annotations; a tool without them fails tocompile/register (see `src/annotations.rs`).+- Keep tool input roots free of `oneOf`, `anyOf`, and `allOf`; put tagged variants+ under a named property so provider validation accepts them. Schema generation+ rejects root composition when toolsets are built, including in their tests.## Relateddiff --git a/crates/ai_toolset/src/schema/error.rs b/crates/ai_toolset/src/schema/error.rsindex 0503996016..db80305a5b 100644--- a/crates/ai_toolset/src/schema/error.rs+++ b/crates/ai_toolset/src/schema/error.rs@@ -41,4 +41,9 @@ pub enum ValidationError {/// The root of a tool input schema must be an object.#[error("tool input schema root must have type \"object\"")]RootNotObject,+ /// Providers reject composition keywords at the tool input root.+ #[error(+ "tool input schema root must not contain oneOf, anyOf, or allOf; put alternatives inside a named property instead of flattening them"+ )]+ RootComposition,}diff --git a/crates/ai_toolset/src/schema/generate.rs b/crates/ai_toolset/src/schema/generate.rsindex f88fd51f1a..c890e18842 100644--- a/crates/ai_toolset/src/schema/generate.rs+++ b/crates/ai_toolset/src/schema/generate.rs@@ -33,23 +33,14 @@ pub struct ValidatedSchema {////// Transforms (in order):/// 1. subschemas inlined — providers get one self-contained schema-/// 2. [`OneOfToAnyOf`] — `oneOf` is unsupported by both providers; `anyOf`-/// is supported by both+/// 2. [`OneOfToAnyOf`] — `oneOf` is unsupported by both providers; nested+/// `anyOf` is supported by both/// 3. [`StripUnsupported`] — numeric/string/array constraints and/// non-whitelisted `format`s are removed and recorded in `description`-/// 4. [`NullifyOptional`] — properties not in `required` get a null union;-/// strict mode forbids optionality, so "may be omitted" becomes "may be-/// null"-/// 5. [`AddRequired`] — every property listed in `required`, as OpenAI-/// requires (must run after [`NullifyOptional`])-/// 6. [`AdditionalPropertiesFalse`] — `additionalProperties: false` on-/// every object (both providers require it)///-/// Validation then rejects what cannot be transformed: missing-/// title/description, a non-object root, recursive types (surviving-/// `$ref`s), map types (`additionalProperties` schemas), non-primitive-/// enums, and residual `oneOf`. Never panics — failures are returned as-/// [`ValidationError`]s.+/// Validation then rejects missing title/description, a non-object root,+/// root-level `oneOf`/`anyOf`/`allOf`, and residual `oneOf`. Never panics —+/// failures are returned as [`ValidationError`]s.pub fn generate_validated_input_schema<T: schemars::JsonSchema>()-> Result<ValidatedSchema, ValidationError> {let schema = SchemaSettings::draft2020_12()diff --git a/crates/ai_toolset/src/schema/generate/test.rs b/crates/ai_toolset/src/schema/generate/test.rsindex afc35a049a..8d41ab6653 100644--- a/crates/ai_toolset/src/schema/generate/test.rs+++ b/crates/ai_toolset/src/schema/generate/test.rs@@ -132,3 +132,46 @@ fn test_unsupported_constraints_stripped_into_description() {// `uuid` is on both providers' format whitelist and survives.assert_eq!(property(&json, "id")["format"], "uuid");}++#[derive(JsonSchema)]+#[serde(tag = "kind", content = "drafts", rename_all = "snake_case")]+#[expect(dead_code, reason = "Schema generation fixture")]+enum Drafts {+ Email(Vec<String>),+ Calendar(Vec<String>),+}++#[derive(JsonSchema)]+#[schemars(title = "FlattenedInput", description = "A flattened tagged enum")]+#[expect(dead_code, reason = "Schema generation fixture")]+struct FlattenedInput {+ title: String,+ #[serde(flatten)]+ drafts: Drafts,+}++#[test]+fn rejects_a_flattened_enum_at_the_tool_input_root() {+ assert!(generate_validated_input_schema::<FlattenedInput>().is_err());+}++#[derive(JsonSchema)]+#[schemars(title = "NestedInput", description = "A nested tagged enum")]+#[expect(dead_code, reason = "Schema generation fixture")]+struct NestedInput {+ review: FlattenedInput,+}++#[test]+fn accepts_a_tagged_enum_under_a_named_property() {+ let validated = generate_validated_input_schema::<NestedInput>().unwrap();+ let json = schema_json(&validated.schema);+ assert!(json.get("anyOf").is_none());+ assert_eq!(+ json["properties"]["review"]["anyOf"]+ .as_array()+ .unwrap()+ .len(),+ 2+ );+}diff --git a/crates/ai_toolset/src/schema/validate.rs b/crates/ai_toolset/src/schema/validate.rsindex 7227e0dbf3..c6eb4f33bc 100644--- a/crates/ai_toolset/src/schema/validate.rs+++ b/crates/ai_toolset/src/schema/validate.rs@@ -8,6 +8,9 @@ mod extract;mod one_of;mod recursive;+#[cfg(test)]+mod test;+use super::error::ValidationError;use extract::Extract;use schemars::Schema;@@ -30,9 +33,9 @@ pub trait Validate {/// both OpenAI and Anthropic.////// Returns the tool's name and description extracted from the schema-/// metadata. Checks what the transforms cannot fix: surviving `$ref`s-/// (recursive types), non-`false` `additionalProperties` (map types),-/// non-primitive enum members, residual `oneOf`, and a non-object root.+/// metadata. Requires an object root without `oneOf`, `anyOf`, or `allOf`,+/// and rejects residual `oneOf` throughout the schema. Nested `anyOf` is+/// supported, so tagged variants belong under a named property.////// See:/// * <https://platform.claude.com/docs/en/agents-and-tools/tool-use/strict-tool-use>@@ -47,6 +50,13 @@ pub fn validate_tool_schema(schema: &Schema) -> Result<(String, String), Validatreturn Err(ValidationError::RootNotObject);}+ if ["oneOf", "anyOf", "allOf"]+ .into_iter()+ .any(|keyword| schema.get(keyword).is_some())+ {+ return Err(ValidationError::RootComposition);+ }+RecursiveValidate(ValidateNoOneOf).validate(schema)?;Ok((name, description))diff --git a/crates/ai_toolset/src/schema/validate/test.rs b/crates/ai_toolset/src/schema/validate/test.rsnew file mode 100644index 0000000000..73b1bec7ee--- /dev/null+++ b/crates/ai_toolset/src/schema/validate/test.rs@@ -0,0 +1,30 @@+use super::*;+use schemars::json_schema;+use serde_json::json;++#[test]+fn rejects_composition_at_the_tool_input_root() {+ for keyword in ["oneOf", "anyOf", "allOf"] {+ let mut schema = json_schema!({+ "title": "ComposedInput",+ "description": "A composed tool input",+ "type": "object",+ "properties": {"title": {"type": "string"}},+ });+ schema.insert(+ keyword.into(),+ json!([+ {"type": "object", "properties": {"kind": {"const": "email"}}},+ {"type": "object", "properties": {"kind": {"const": "calendar"}}},+ ]),+ );++ assert!(+ matches!(+ validate_tool_schema(&schema),+ Err(ValidationError::RootComposition)+ ),+ "tool input with root {keyword} must be rejected"+ );+ }+}