From b08ed0604bc8d6b22329009f28e0a3b836634334 Mon Sep 17 00:00:00 2001 From: Simo Lin <25425177+slin1237@users.noreply.github.com> Date: Thu, 8 Oct 2026 05:58:18 -0700 Subject: [PATCH 1/4] fix(symphony): the pointer walk is bounded, and reads the property's own definitions first A chain of distinct definitions sent the walk one level deeper per link, with nothing but cycle detection to stop it, and a request could chain enough of them to exhaust the stack. The walk follows at most sixty-four pointers in all; past that a pointer counts as unresolved and the property pins nothing. And a property's own `$defs` or `definitions` stand before the root's when a pointer is resolved, as they do in the document the engine gets, so a property that defines a name the root defines badly keeps its type. Signed-off-by: Simo Lin <25425177+slin1237@users.noreply.github.com> Co-authored-by: Chang Su <8605658+CatherineSue@users.noreply.github.com> Co-authored-by: Keyang Ru Co-authored-by: key4ng Co-authored-by: Chen Hongtao <56470055+chenht2022@users.noreply.github.com> Co-authored-by: LightSeek Foundation <243258330+lightseek-bot@users.noreply.github.com> --- crates/symphony/src/grammar/schema.rs | 85 ++++++++++++++++++++------- crates/symphony/src/grammar/xtml.rs | 78 ++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 21 deletions(-) diff --git a/crates/symphony/src/grammar/schema.rs b/crates/symphony/src/grammar/schema.rs index b8819aa94d..ac0a6e0d1a 100644 --- a/crates/symphony/src/grammar/schema.rs +++ b/crates/symphony/src/grammar/schema.rs @@ -53,11 +53,12 @@ pub(super) fn arguments( /// points at nothing (a grammar with such a pointer would not compile, and the whole call with /// it). A `$ref` lends its target's keywords, and the property's own win, as in JSON Schema /// 2020-12. -pub(super) fn shape( - schema: &Value, - definitions: Definitions<'_>, +pub(super) fn shape<'a>( + schema: &'a Value, + definitions: Definitions<'a>, text: impl FnOnce() -> Grammar, ) -> Option<(&'static str, Grammar)> { + let definitions = definitions.within(schema); if !definitions.pointers_resolve(schema) { return None; } @@ -92,35 +93,74 @@ pub(super) fn shape( } } -/// The definitions at the root of a tool's schema, `$defs` and `definitions`. Each property's -/// schema goes to the engine as a document of its own, and a `$ref` such as `#/$defs/Node` points -/// from the root of the document it stands in, so the definitions travel with the property's -/// schema. A pointer to the root itself, `#`, names the tool's schema in the tool and the property -/// in the property's document, so it resolves to nothing here, and the property takes any value. +/// The definitions a property's schema can point into: its own `$defs` and `definitions` first, +/// then the tool's root ones, which is the document the engine gets ([`Self::attached_to`]). Each +/// property's schema goes to the engine as a document of its own, and a `$ref` such as +/// `#/$defs/Node` points from the root of the document it stands in, so the definitions travel +/// with the property's schema. A pointer to the root itself, `#`, names the tool's schema in the +/// tool and the property in the property's document, so it resolves to nothing here, and the +/// property takes any value. #[derive(Clone, Copy)] pub(super) struct Definitions<'a> { + own: Blocks<'a>, + root: Blocks<'a>, +} + +/// A schema's two definition blocks. +#[derive(Clone, Copy)] +struct Blocks<'a> { defs: Option<&'a Value>, definitions: Option<&'a Value>, } +impl<'a> Blocks<'a> { + fn of(schema: &'a Value) -> Self { + Self { + defs: schema.get("$defs"), + definitions: schema.get("definitions"), + } + } + + fn named(self, block: &str) -> Option<&'a Value> { + match block { + "$defs" => self.defs, + "definitions" => self.definitions, + _ => None, + } + } +} + +/// How many pointers the check follows before it gives up on a schema: a tool whose definitions +/// chain deeper pins nothing, and the walk's depth stays bounded whatever a request sends. +const POINTERS_FOLLOWED_AT_MOST: usize = 64; + impl<'a> Definitions<'a> { pub(super) fn of(root: &'a Value) -> Self { Self { - defs: root.get("$defs"), - definitions: root.get("definitions"), + own: Blocks::of(root), + root: Blocks::of(root), + } + } + + /// The definitions as the property `schema` sees them: its own blocks first, then the root's. + fn within(self, schema: &'a Value) -> Self { + Self { + own: Blocks::of(schema), + root: self.root, } } - /// The schema a local pointer names: `#/$defs/Name` or `#/definitions/Name` an entry, with the - /// pointer's escapes (`~1` for `/`, `~0` for `~`) undone; `#` itself nothing, since it would - /// name the property's own document once the engine compiles it alone. + /// The schema a local pointer names: `#/$defs/Name` or `#/definitions/Name` an entry, the + /// property's own block before the root's, with the pointer's escapes (`~1` for `/`, `~0` for + /// `~`) undone; `#` itself nothing, since it would name the property's own document once the + /// engine compiles it alone. fn resolve(self, pointer: &str) -> Option<&'a Value> { let mut segments = pointer.strip_prefix("#/")?.split('/'); - let mut node = match segments.next()? { - "$defs" => self.defs?, - "definitions" => self.definitions?, - _ => return None, - }; + let block = segments.next()?; + let name = segments.next()?.replace("~1", "/").replace("~0", "~"); + let mut node = [self.own, self.root] + .into_iter() + .find_map(|blocks| blocks.named(block)?.get(&name))?; for segment in segments { node = node.get(segment.replace("~1", "/").replace("~0", "~"))?; } @@ -136,7 +176,8 @@ impl<'a> Definitions<'a> { /// Whether every local pointer in `schema` names something, and every pointer in what it /// names, as far as the pointers reach: the definitions travel with the property, so a pointer /// at nothing inside one of them reaches the engine too. A definition that names itself is - /// followed once. + /// followed once, and no more than [`POINTERS_FOLLOWED_AT_MOST`] pointers are followed in all, + /// so a chain of definitions cannot send the walk arbitrarily deep. fn pointers_resolve(self, schema: &Value) -> bool { self.pointers_resolve_from(schema, &mut Vec::new()) } @@ -150,6 +191,7 @@ impl<'a> Definitions<'a> { let named_resolves = local.is_none_or(|pointer| match self.resolve(pointer) { None => false, Some(_) if followed.iter().any(|seen| seen == pointer) => true, + Some(_) if followed.len() >= POINTERS_FOLLOWED_AT_MOST => false, Some(target) => { followed.push(pointer.to_string()); self.pointers_resolve_from(target, followed) @@ -169,13 +211,14 @@ impl<'a> Definitions<'a> { /// stays; the root's fill in the names it lacks. A schema with no local pointer goes as it is. fn attached_to(self, schema: &Value) -> Value { let mut attached = schema.clone(); - if self.defs.is_none() && self.definitions.is_none() || !has_local_reference(schema) { + let root = self.root; + if root.defs.is_none() && root.definitions.is_none() || !has_local_reference(schema) { return attached; } let Some(object) = attached.as_object_mut() else { return attached; }; - for (name, block) in [("$defs", self.defs), ("definitions", self.definitions)] { + for (name, block) in [("$defs", root.defs), ("definitions", root.definitions)] { let Some(Value::Object(entries)) = block else { continue; }; diff --git a/crates/symphony/src/grammar/xtml.rs b/crates/symphony/src/grammar/xtml.rs index 095db9a915..0776cd03b3 100644 --- a/crates/symphony/src/grammar/xtml.rs +++ b/crates/symphony/src/grammar/xtml.rs @@ -381,6 +381,84 @@ mod tests { } } + #[test] + fn a_propertys_own_definition_stands_before_the_roots() { + // The engine gets the property's schema with its own definitions kept and the root's + // filling in the rest, so the check reads the same: the property's `Inner` is whole and + // the root's `Inner`, which points at nothing, is not the one `Outer` reaches. + let call = first_call(&[tool( + "t", + value!({ + "type": "object", + "$defs": { + "Outer": {"type": "object", "properties": {"in": {"$ref": "#/$defs/Inner"}}}, + "Inner": {"$ref": "#/$defs/Gone"}, + }, + "properties": { + "own": { + "$ref": "#/$defs/Outer", + "$defs": {"Inner": {"type": "integer"}}, + }, + "root": {"$ref": "#/$defs/Outer"}, + }, + "required": ["own", "root"], + }), + )]); + let slots = call["elements"][3]["elements"] + .as_array() + .expect("two slots"); + assert_eq!( + slots[0]["elements"][0]["value"], + argument_open("own", "object") + ); + assert_eq!( + slots[0]["elements"][1]["json_schema"]["$defs"]["Inner"], + value!({"type": "integer"}), + "the property's own definition goes to the engine" + ); + assert_eq!( + slots[1]["elements"][0]["value"], "<|open|>argument key=\"root\" type=\"", + "through the root's Inner the pointer reaches nothing" + ); + } + + #[test] + fn a_chain_of_definitions_is_followed_so_far_and_no_further() { + // Every link is a different pointer, so cycle detection alone would follow a request's + // chain as deep as it goes; the walk stops after a fixed number and the property pins + // nothing, which keeps the stack bounded whatever a request sends. + let chain = |links: usize| { + let mut defs = serde_json::Map::new(); + for i in 0..links { + defs.insert( + format!("D{i}"), + value!({"$ref": format!("#/$defs/D{}", i + 1)}), + ); + } + defs.insert(format!("D{links}"), value!({"type": "integer"})); + tool( + "t", + value!({ + "type": "object", + "$defs": defs, + "properties": {"p": {"$ref": "#/$defs/D0"}}, + "required": ["p"], + }), + ) + }; + let short = first_call(&[chain(10)]); + assert_eq!( + short["elements"][3]["elements"][0]["elements"][0]["value"], + "<|open|>argument key=\"p\" type=\"", + "a chain lends no type, since a $ref lends its target's keywords one hop only" + ); + let long = first_call(&[chain(5_000)]); + assert_eq!( + long["elements"][3]["elements"][0]["elements"][0]["value"], + "<|open|>argument key=\"p\" type=\"" + ); + } + #[test] fn attribute_values_are_escaped_as_the_template_writes_them() { let parameters = value!({ From 2e1c7ff76ef96e0363d07157f180d69b82fc008f Mon Sep 17 00:00:00 2001 From: Simo Lin <25425177+slin1237@users.noreply.github.com> Date: Thu, 8 Oct 2026 06:27:20 -0700 Subject: [PATCH 2/4] fix(symphony): the pointer walk bounds depth, not breadth, and a non-object block resolves nothing The cap on pointers followed counted every distinct pointer, so a wide schema, a Pydantic model whose tree names more than sixty-four definitions with no chain at all, lost its type pins. The memo of pointers followed stays, so each is walked once and a wide schema costs one walk per definition, and a depth counter bounds the chain instead. A block the property has but that is no object (`"$defs": null`) now resolves nothing, as the document the engine gets has no entry at the pointer. The chain test types its property, so its outcome depends on the walk, and pins the boundary; a wide schema and the non-object block have tests of their own. Signed-off-by: Simo Lin <25425177+slin1237@users.noreply.github.com> Co-authored-by: Chang Su <8605658+CatherineSue@users.noreply.github.com> Co-authored-by: Keyang Ru Co-authored-by: key4ng Co-authored-by: Chen Hongtao <56470055+chenht2022@users.noreply.github.com> Co-authored-by: LightSeek Foundation <243258330+lightseek-bot@users.noreply.github.com> --- crates/symphony/src/grammar/schema.rs | 44 +++++++++----- crates/symphony/src/grammar/xtml.rs | 87 ++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 25 deletions(-) diff --git a/crates/symphony/src/grammar/schema.rs b/crates/symphony/src/grammar/schema.rs index ac0a6e0d1a..d3af63d269 100644 --- a/crates/symphony/src/grammar/schema.rs +++ b/crates/symphony/src/grammar/schema.rs @@ -130,9 +130,11 @@ impl<'a> Blocks<'a> { } } -/// How many pointers the check follows before it gives up on a schema: a tool whose definitions -/// chain deeper pins nothing, and the walk's depth stays bounded whatever a request sends. -const POINTERS_FOLLOWED_AT_MOST: usize = 64; +/// How deep a chain of pointers the check follows before it gives up on a schema: a tool whose +/// definitions chain deeper pins nothing, and the walk's depth stays bounded whatever a request +/// sends. Breadth is not counted: a schema may point at any number of definitions, each walked +/// once. +pub(super) const POINTER_DEPTH_AT_MOST: usize = 64; impl<'a> Definitions<'a> { pub(super) fn of(root: &'a Value) -> Self { @@ -158,9 +160,16 @@ impl<'a> Definitions<'a> { let mut segments = pointer.strip_prefix("#/")?.split('/'); let block = segments.next()?; let name = segments.next()?.replace("~1", "/").replace("~0", "~"); - let mut node = [self.own, self.root] - .into_iter() - .find_map(|blocks| blocks.named(block)?.get(&name))?; + // A block the property has but that is no object (`"$defs": null`) stays as it is in the + // document the engine gets, with no entry to find, so nothing resolves through it. + let mut node = match self.own.named(block) { + Some(Value::Object(entries)) => match entries.get(&name) { + Some(entry) => entry, + None => self.root.named(block)?.get(&name)?, + }, + Some(_) => return None, + None => self.root.named(block)?.get(&name)?, + }; for segment in segments { node = node.get(segment.replace("~1", "/").replace("~0", "~"))?; } @@ -175,14 +184,19 @@ impl<'a> Definitions<'a> { /// Whether every local pointer in `schema` names something, and every pointer in what it /// names, as far as the pointers reach: the definitions travel with the property, so a pointer - /// at nothing inside one of them reaches the engine too. A definition that names itself is - /// followed once, and no more than [`POINTERS_FOLLOWED_AT_MOST`] pointers are followed in all, - /// so a chain of definitions cannot send the walk arbitrarily deep. + /// at nothing inside one of them reaches the engine too. Each distinct pointer is walked once + /// (`followed` remembers them, so a definition that names itself, or a wide schema, costs one + /// walk per definition), and a chain is followed no deeper than [`POINTER_DEPTH_AT_MOST`]. fn pointers_resolve(self, schema: &Value) -> bool { - self.pointers_resolve_from(schema, &mut Vec::new()) + self.pointers_resolve_from(schema, &mut Vec::new(), 0) } - fn pointers_resolve_from(self, schema: &Value, followed: &mut Vec) -> bool { + fn pointers_resolve_from( + self, + schema: &Value, + followed: &mut Vec, + depth: usize, + ) -> bool { match schema { Value::Object(map) => map.iter().all(|(key, value)| { let local = value @@ -191,17 +205,17 @@ impl<'a> Definitions<'a> { let named_resolves = local.is_none_or(|pointer| match self.resolve(pointer) { None => false, Some(_) if followed.iter().any(|seen| seen == pointer) => true, - Some(_) if followed.len() >= POINTERS_FOLLOWED_AT_MOST => false, + Some(_) if depth >= POINTER_DEPTH_AT_MOST => false, Some(target) => { followed.push(pointer.to_string()); - self.pointers_resolve_from(target, followed) + self.pointers_resolve_from(target, followed, depth + 1) } }); - named_resolves && self.pointers_resolve_from(value, followed) + named_resolves && self.pointers_resolve_from(value, followed, depth) }), Value::Array(items) => items .iter() - .all(|item| self.pointers_resolve_from(item, followed)), + .all(|item| self.pointers_resolve_from(item, followed, depth)), _ => true, } } diff --git a/crates/symphony/src/grammar/xtml.rs b/crates/symphony/src/grammar/xtml.rs index 0776cd03b3..046cd303ca 100644 --- a/crates/symphony/src/grammar/xtml.rs +++ b/crates/symphony/src/grammar/xtml.rs @@ -134,7 +134,7 @@ mod tests { use serde_json::json as value; use super::*; - use crate::formats; + use crate::{formats, grammar::schema::POINTER_DEPTH_AT_MOST}; fn tool(name: &str, parameters: Value) -> Tool { Tool { @@ -425,8 +425,9 @@ mod tests { #[test] fn a_chain_of_definitions_is_followed_so_far_and_no_further() { // Every link is a different pointer, so cycle detection alone would follow a request's - // chain as deep as it goes; the walk stops after a fixed number and the property pins - // nothing, which keeps the stack bounded whatever a request sends. + // chain as deep as it goes; the walk stops at a fixed depth and the property pins nothing, + // which keeps the stack bounded whatever a request sends. The property carries a type of + // its own, so a chain within the depth pins it and one past the depth does not. let chain = |links: usize| { let mut defs = serde_json::Map::new(); for i in 0..links { @@ -441,24 +442,90 @@ mod tests { value!({ "type": "object", "$defs": defs, - "properties": {"p": {"$ref": "#/$defs/D0"}}, + "properties": {"p": {"type": "object", "$ref": "#/$defs/D0"}}, "required": ["p"], }), ) }; - let short = first_call(&[chain(10)]); + let opener = |links: usize| { + first_call(&[chain(links)])["elements"][3]["elements"][0]["elements"][0]["value"] + .clone() + }; + let depth = POINTER_DEPTH_AT_MOST; + assert_eq!(opener(10), argument_open("p", "object")); + assert_eq!( + opener(depth - 1), + argument_open("p", "object"), + "within the depth" + ); assert_eq!( - short["elements"][3]["elements"][0]["elements"][0]["value"], + opener(depth + 1), "<|open|>argument key=\"p\" type=\"", - "a chain lends no type, since a $ref lends its target's keywords one hop only" + "past the depth" + ); + assert_eq!(opener(5_000), "<|open|>argument key=\"p\" type=\""); + } + + #[test] + fn a_wide_schema_keeps_its_pins_however_many_definitions_it_names() { + // Breadth is not depth: a property whose fields each name a different leaf definition, + // more of them than the depth the walk allows, is walked once per definition and keeps + // its type. + let mut defs = serde_json::Map::new(); + let mut fields = serde_json::Map::new(); + for i in 0..70 { + defs.insert(format!("L{i}"), value!({"type": "integer"})); + fields.insert(format!("f{i}"), value!({"$ref": format!("#/$defs/L{i}")})); + } + let call = first_call(&[tool( + "t", + value!({ + "type": "object", + "$defs": defs, + "properties": {"wide": {"type": "object", "properties": fields}}, + "required": ["wide"], + }), + )]); + let slot = &call["elements"][3]["elements"][0]; + assert_eq!( + slot["elements"][0]["value"], + argument_open("wide", "object") ); - let long = first_call(&[chain(5_000)]); assert_eq!( - long["elements"][3]["elements"][0]["elements"][0]["value"], - "<|open|>argument key=\"p\" type=\"" + slot["elements"][1]["json_schema"]["$defs"]["L69"], + value!({"type": "integer"}) ); } + #[test] + fn a_propertys_own_definitions_block_that_is_no_object_resolves_nothing() { + // `"$defs": null` (an SDK writing an empty map as null) stays as it is in the document the + // engine gets, with no entry at the pointer, so the property pins nothing rather than a + // schema the engine cannot compile. + let call = first_call(&[tool( + "t", + value!({ + "type": "object", + "$defs": {"Count": {"type": "integer"}}, + "properties": { + "null_block": {"$ref": "#/$defs/Count", "$defs": null}, + "list_block": {"$ref": "#/$defs/Count", "$defs": []}, + }, + "required": ["null_block", "list_block"], + }), + )]); + let slots = call["elements"][3]["elements"] + .as_array() + .expect("two slots"); + for (slot, key) in [(&slots[0], "null_block"), (&slots[1], "list_block")] { + assert_eq!( + slot["elements"][0]["value"], + format!("<|open|>argument key=\"{key}\" type=\""), + "{key} pins nothing" + ); + } + } + #[test] fn attribute_values_are_escaped_as_the_template_writes_them() { let parameters = value!({ From c9ed9cd94e1bc35d5e2e59afbf58199579ad72c6 Mon Sep 17 00:00:00 2001 From: Simo Lin <25425177+slin1237@users.noreply.github.com> Date: Thu, 8 Oct 2026 06:44:45 -0700 Subject: [PATCH 3/4] fix(symphony): the pointer walk keeps its own list of schemas to look at, and follows a chain to its end The depth cap counted pointer hops, and between hops the walk still recursed through each definition's own nesting, so a chain of sixty-four definitions each nested a hundred levels deep put some seven thousand frames on the stack, more than the five-thousand-link chain the test called the unsafe case. The walk now keeps the schemas still to look at in a list of its own and follows a chain to its end: each distinct pointer is followed once, as before, and nothing a request sends reaches the call stack, whatever its chains and nesting. The depth constant goes. The chain test pins ten links, five thousand links, and sixty-four links each wrapped a hundred and twenty times in `properties` before their pointer, all followed to the integer at the end. Signed-off-by: Simo Lin <25425177+slin1237@users.noreply.github.com> Co-authored-by: Chang Su <8605658+CatherineSue@users.noreply.github.com> Co-authored-by: Keyang Ru Co-authored-by: key4ng Co-authored-by: Chen Hongtao <56470055+chenht2022@users.noreply.github.com> Co-authored-by: LightSeek Foundation <243258330+lightseek-bot@users.noreply.github.com> --- crates/symphony/src/grammar/schema.rs | 69 ++++++++++++--------------- crates/symphony/src/grammar/xtml.rs | 51 ++++++++++---------- 2 files changed, 58 insertions(+), 62 deletions(-) diff --git a/crates/symphony/src/grammar/schema.rs b/crates/symphony/src/grammar/schema.rs index d3af63d269..03dff15d0a 100644 --- a/crates/symphony/src/grammar/schema.rs +++ b/crates/symphony/src/grammar/schema.rs @@ -5,6 +5,8 @@ //! over a tool's properties the syntaxes share: one argument per property, in the schema's order, //! optional unless the schema requires it. +use std::collections::HashSet; + use serde_json::{json, Value}; use super::Grammar; @@ -130,12 +132,6 @@ impl<'a> Blocks<'a> { } } -/// How deep a chain of pointers the check follows before it gives up on a schema: a tool whose -/// definitions chain deeper pins nothing, and the walk's depth stays bounded whatever a request -/// sends. Breadth is not counted: a schema may point at any number of definitions, each walked -/// once. -pub(super) const POINTER_DEPTH_AT_MOST: usize = 64; - impl<'a> Definitions<'a> { pub(super) fn of(root: &'a Value) -> Self { Self { @@ -184,40 +180,37 @@ impl<'a> Definitions<'a> { /// Whether every local pointer in `schema` names something, and every pointer in what it /// names, as far as the pointers reach: the definitions travel with the property, so a pointer - /// at nothing inside one of them reaches the engine too. Each distinct pointer is walked once - /// (`followed` remembers them, so a definition that names itself, or a wide schema, costs one - /// walk per definition), and a chain is followed no deeper than [`POINTER_DEPTH_AT_MOST`]. - fn pointers_resolve(self, schema: &Value) -> bool { - self.pointers_resolve_from(schema, &mut Vec::new(), 0) - } - - fn pointers_resolve_from( - self, - schema: &Value, - followed: &mut Vec, - depth: usize, - ) -> bool { - match schema { - Value::Object(map) => map.iter().all(|(key, value)| { - let local = value - .as_str() - .filter(|pointer| is_reference(key) && pointer.starts_with('#')); - let named_resolves = local.is_none_or(|pointer| match self.resolve(pointer) { - None => false, - Some(_) if followed.iter().any(|seen| seen == pointer) => true, - Some(_) if depth >= POINTER_DEPTH_AT_MOST => false, - Some(target) => { - followed.push(pointer.to_string()); - self.pointers_resolve_from(target, followed, depth + 1) + /// at nothing inside one of them reaches the engine too. Each distinct pointer is followed + /// once (`followed` remembers them, so a definition that names itself, or a wide schema, costs + /// one look per definition), and the schemas still to look at wait in a list of the walk's + /// own, not on the call stack: a chain of definitions costs its length and no stack, however + /// long a request makes it and however deep each definition nests before its pointer. + fn pointers_resolve(self, schema: &'a Value) -> bool { + let mut followed = HashSet::new(); + let mut pending = vec![schema]; + while let Some(schema) = pending.pop() { + match schema { + Value::Object(map) => { + for (key, value) in map { + let local = value + .as_str() + .filter(|pointer| is_reference(key) && pointer.starts_with('#')); + if let Some(pointer) = local { + let Some(target) = self.resolve(pointer) else { + return false; + }; + if followed.insert(pointer) { + pending.push(target); + } + } + pending.push(value); } - }); - named_resolves && self.pointers_resolve_from(value, followed, depth) - }), - Value::Array(items) => items - .iter() - .all(|item| self.pointers_resolve_from(item, followed, depth)), - _ => true, + } + Value::Array(items) => pending.extend(items), + _ => {} + } } + true } /// The property's schema with the root's definitions attached, so a local pointer in it still diff --git a/crates/symphony/src/grammar/xtml.rs b/crates/symphony/src/grammar/xtml.rs index 046cd303ca..20c96e851c 100644 --- a/crates/symphony/src/grammar/xtml.rs +++ b/crates/symphony/src/grammar/xtml.rs @@ -134,7 +134,7 @@ mod tests { use serde_json::json as value; use super::*; - use crate::{formats, grammar::schema::POINTER_DEPTH_AT_MOST}; + use crate::formats; fn tool(name: &str, parameters: Value) -> Tool { Tool { @@ -423,18 +423,20 @@ mod tests { } #[test] - fn a_chain_of_definitions_is_followed_so_far_and_no_further() { - // Every link is a different pointer, so cycle detection alone would follow a request's - // chain as deep as it goes; the walk stops at a fixed depth and the property pins nothing, - // which keeps the stack bounded whatever a request sends. The property carries a type of - // its own, so a chain within the depth pins it and one past the depth does not. - let chain = |links: usize| { + fn a_chain_of_definitions_is_followed_to_its_end_however_long_and_deep() { + // Every link is a different pointer, so the memo of pointers followed does not shorten a + // chain, and the walk keeps the schemas still to look at in a list of its own: a request's + // chain costs its length and no stack, however many links it has and however deep each + // link nests before its pointer. The property carries a type of its own, so its pin shows + // the chain was followed to its end. + let chain = |links: usize, nesting: usize| { let mut defs = serde_json::Map::new(); for i in 0..links { - defs.insert( - format!("D{i}"), - value!({"$ref": format!("#/$defs/D{}", i + 1)}), - ); + let mut link = value!({"$ref": format!("#/$defs/D{}", i + 1)}); + for _ in 0..nesting { + link = value!({"properties": {"a": link}}); + } + defs.insert(format!("D{i}"), link); } defs.insert(format!("D{links}"), value!({"type": "integer"})); tool( @@ -447,30 +449,31 @@ mod tests { }), ) }; - let opener = |links: usize| { - first_call(&[chain(links)])["elements"][3]["elements"][0]["elements"][0]["value"] + let opener = |links: usize, nesting: usize| { + first_call(&[chain(links, nesting)])["elements"][3]["elements"][0]["elements"][0] + ["value"] .clone() }; - let depth = POINTER_DEPTH_AT_MOST; - assert_eq!(opener(10), argument_open("p", "object")); + assert_eq!(opener(10, 0), argument_open("p", "object")); assert_eq!( - opener(depth - 1), + opener(5_000, 0), argument_open("p", "object"), - "within the depth" + "five thousand links" ); + // Sixty-four links, each wrapped a hundred and twenty times in `properties` before its + // pointer, two objects a wrap: a walk with a frame per object would need some fifteen + // thousand of them. assert_eq!( - opener(depth + 1), - "<|open|>argument key=\"p\" type=\"", - "past the depth" + opener(64, 120), + argument_open("p", "object"), + "sixty-four links nested deep" ); - assert_eq!(opener(5_000), "<|open|>argument key=\"p\" type=\""); } #[test] fn a_wide_schema_keeps_its_pins_however_many_definitions_it_names() { - // Breadth is not depth: a property whose fields each name a different leaf definition, - // more of them than the depth the walk allows, is walked once per definition and keeps - // its type. + // A property whose fields each name a different leaf definition is looked at once per + // definition and keeps its type, with every definition attached. let mut defs = serde_json::Map::new(); let mut fields = serde_json::Map::new(); for i in 0..70 { From b475a59572ff5759ba31b814c0c4daa83f473834 Mon Sep 17 00:00:00 2001 From: Simo Lin <25425177+slin1237@users.noreply.github.com> Date: Thu, 8 Oct 2026 06:50:36 -0700 Subject: [PATCH 4/4] test(symphony): the chain test reaches for the end, with a dangling last link Every chain case pinned `object`, which a walk that stopped after the first hop, or dropped its pending schemas past some depth, would also have given, since every link resolves and the last definition is a whole `integer`. The same chains with their last definition missing now have to come out typeless: only a walk that reaches the end finds the pointer at nothing, so the pin shows the property the test's name promises. Signed-off-by: Simo Lin <25425177+slin1237@users.noreply.github.com> Co-authored-by: Chang Su <8605658+CatherineSue@users.noreply.github.com> Co-authored-by: Keyang Ru Co-authored-by: key4ng Co-authored-by: Chen Hongtao <56470055+chenht2022@users.noreply.github.com> Co-authored-by: LightSeek Foundation <243258330+lightseek-bot@users.noreply.github.com> --- crates/symphony/src/grammar/xtml.rs | 34 +++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/symphony/src/grammar/xtml.rs b/crates/symphony/src/grammar/xtml.rs index 20c96e851c..4cd3ea0bd9 100644 --- a/crates/symphony/src/grammar/xtml.rs +++ b/crates/symphony/src/grammar/xtml.rs @@ -427,9 +427,10 @@ mod tests { // Every link is a different pointer, so the memo of pointers followed does not shorten a // chain, and the walk keeps the schemas still to look at in a list of its own: a request's // chain costs its length and no stack, however many links it has and however deep each - // link nests before its pointer. The property carries a type of its own, so its pin shows - // the chain was followed to its end. - let chain = |links: usize, nesting: usize| { + // link nests before its pointer. The property carries a type of its own: a whole chain + // pins it, and a chain whose last link points at nothing does not, so the pin shows the + // walk reached the end rather than stopping partway. + let chain = |links: usize, nesting: usize, whole: bool| { let mut defs = serde_json::Map::new(); for i in 0..links { let mut link = value!({"$ref": format!("#/$defs/D{}", i + 1)}); @@ -438,7 +439,9 @@ mod tests { } defs.insert(format!("D{i}"), link); } - defs.insert(format!("D{links}"), value!({"type": "integer"})); + if whole { + defs.insert(format!("D{links}"), value!({"type": "integer"})); + } tool( "t", value!({ @@ -449,14 +452,15 @@ mod tests { }), ) }; - let opener = |links: usize, nesting: usize| { - first_call(&[chain(links, nesting)])["elements"][3]["elements"][0]["elements"][0] + let opener = |links: usize, nesting: usize, whole: bool| { + first_call(&[chain(links, nesting, whole)])["elements"][3]["elements"][0]["elements"][0] ["value"] .clone() }; - assert_eq!(opener(10, 0), argument_open("p", "object")); + let typeless = "<|open|>argument key=\"p\" type=\""; + assert_eq!(opener(10, 0, true), argument_open("p", "object")); assert_eq!( - opener(5_000, 0), + opener(5_000, 0, true), argument_open("p", "object"), "five thousand links" ); @@ -464,10 +468,22 @@ mod tests { // pointer, two objects a wrap: a walk with a frame per object would need some fifteen // thousand of them. assert_eq!( - opener(64, 120), + opener(64, 120, true), argument_open("p", "object"), "sixty-four links nested deep" ); + // The same chains with their last definition missing: only a walk that reaches the end + // finds the pointer at nothing. + assert_eq!( + opener(5_000, 0, false), + typeless, + "five thousand links to nothing" + ); + assert_eq!( + opener(64, 120, false), + typeless, + "sixty-four deep links to nothing" + ); } #[test]