AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): add sink input validation to AgentValidator (#12514)
An Vy Lef871717f687d
diff --git a/autogpt_platform/backend/backend/blocks/discord/bot_blocks.py b/autogpt_platform/backend/backend/blocks/discord/bot_blocks.pyindex 4ec3d0eec2..451843615e 100644--- a/autogpt_platform/backend/backend/blocks/discord/bot_blocks.py+++ b/autogpt_platform/backend/backend/blocks/discord/bot_blocks.py@@ -73,7 +73,7 @@ class ReadDiscordMessagesBlock(Block):id="df06086a-d5ac-4abb-9996-2ad0acb2eff7",input_schema=ReadDiscordMessagesBlock.Input, # Assign input schemaoutput_schema=ReadDiscordMessagesBlock.Output, # Assign output schema- description="Reads messages from a Discord channel using a bot token.",+ description="Reads new messages from a Discord channel using a bot token and triggers when a new message is posted",categories={BlockCategory.SOCIAL},test_input={"continuous_read": False,diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_generator/fixer.py b/autogpt_platform/backend/backend/copilot/tools/agent_generator/fixer.pyindex 1c514ef53a..ce9c30dc3a 100644--- a/autogpt_platform/backend/backend/copilot/tools/agent_generator/fixer.py+++ b/autogpt_platform/backend/backend/copilot/tools/agent_generator/fixer.py@@ -4,6 +4,8 @@ import loggingimport refrom typing import Any+from backend.data.dynamic_fields import DICT_SPLIT+from .helpers import (AGENT_EXECUTOR_BLOCK_ID,MCP_TOOL_BLOCK_ID,@@ -1536,8 +1538,8 @@ class AgentFixer:for link in links:sink_name = link.get("sink_name", "")- if "_#_" in sink_name:- parent, child = sink_name.split("_#_", 1)+ if DICT_SPLIT in sink_name:+ parent, child = sink_name.split(DICT_SPLIT, 1)# Check if child is a numeric index (invalid for _#_ notation)if child.isdigit():diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_generator/helpers.py b/autogpt_platform/backend/backend/copilot/tools/agent_generator/helpers.pyindex 141426df07..b2099788e3 100644--- a/autogpt_platform/backend/backend/copilot/tools/agent_generator/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/agent_generator/helpers.py@@ -4,6 +4,8 @@ import reimport uuidfrom typing import Any+from backend.data.dynamic_fields import DICT_SPLIT+from .blocks import get_blocks_as_dicts__all__ = [@@ -51,8 +53,8 @@ def generate_uuid() -> str:def get_defined_property_type(schema: dict[str, Any], name: str) -> str | None:"""Get property type from a schema, handling nested `_#_` notation."""- if "_#_" in name:- parent, child = name.split("_#_", 1)+ if DICT_SPLIT in name:+ parent, child = name.split(DICT_SPLIT, 1)parent_schema = schema.get(parent, {})if "properties" in parent_schema and isinstance(parent_schema["properties"], dictdiff --git a/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator.py b/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator.pyindex b9ccfa8662..dd862092d7 100644--- a/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator.py+++ b/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator.py@@ -5,6 +5,8 @@ import loggingimport refrom typing import Any+from backend.data.dynamic_fields import DICT_SPLIT+from .helpers import (AGENT_EXECUTOR_BLOCK_ID,AGENT_INPUT_BLOCK_ID,@@ -256,95 +258,6 @@ class AgentValidator:return valid- def validate_nested_sink_links(- self,- agent: AgentDict,- blocks: list[dict[str, Any]],- node_lookup: dict[str, dict[str, Any]] | None = None,- ) -> bool:- """- Validate nested sink links (links with _#_ notation).- Returns True if all nested links are valid, False otherwise.- """- valid = True- block_input_schemas = {- block.get("id", ""): block.get("inputSchema", {}).get("properties", {})- for block in blocks- }- block_names = {- block.get("id", ""): block.get("name", "Unknown Block") for block in blocks- }- if node_lookup is None:- node_lookup = self._build_node_lookup(agent)-- for link in agent.get("links", []):- sink_name = link.get("sink_name", "")- sink_id = link.get("sink_id")-- if not sink_name or not sink_id:- continue-- if "_#_" in sink_name:- parent, child = sink_name.split("_#_", 1)-- sink_node = node_lookup.get(sink_id)- if not sink_node:- continue-- block_id = sink_node.get("block_id")- input_props = block_input_schemas.get(block_id, {})-- parent_schema = input_props.get(parent)- if not parent_schema:- block_name = block_names.get(block_id, "Unknown Block")- self.add_error(- f"Invalid nested sink link '{sink_name}' for "- f"node '{sink_id}' (block "- f"'{block_name}' - {block_id}): Parent property "- f"'{parent}' does not exist in the block's "- f"input schema."- )- valid = False- continue-- # Check if additionalProperties is allowed either directly- # or via anyOf- allows_additional_properties = parent_schema.get(- "additionalProperties", False- )-- # Check anyOf for additionalProperties- if not allows_additional_properties and "anyOf" in parent_schema:- any_of_schemas = parent_schema.get("anyOf", [])- if isinstance(any_of_schemas, list):- for schema_option in any_of_schemas:- if isinstance(schema_option, dict) and schema_option.get(- "additionalProperties"- ):- allows_additional_properties = True- break-- if not allows_additional_properties:- if not (- isinstance(parent_schema, dict)- and "properties" in parent_schema- and isinstance(parent_schema["properties"], dict)- and child in parent_schema["properties"]- ):- block_name = block_names.get(block_id, "Unknown Block")- self.add_error(- f"Invalid nested sink link '{sink_name}' "- f"for node '{link.get('sink_id', '')}' (block "- f"'{block_name}' - {block_id}): Child "- f"property '{child}' does not exist in "- f"parent '{parent}' schema. Available "- f"properties: "- f"{list(parent_schema.get('properties', {}).keys())}"- )- valid = False-- return valid-def validate_prompt_double_curly_braces_spaces(self, agent: AgentDict) -> bool:"""Validate that prompt parameters do not contain spaces in double curly@@ -471,8 +384,8 @@ class AgentValidator:output_props = block_output_schemas.get(block_id, {})# Handle nested source names (with _#_ notation)- if "_#_" in source_name:- parent, child = source_name.split("_#_", 1)+ if DICT_SPLIT in source_name:+ parent, child = source_name.split(DICT_SPLIT, 1)parent_schema = output_props.get(parent)if not parent_schema:@@ -553,6 +466,195 @@ class AgentValidator:return valid+ def validate_sink_input_existence(+ self,+ agent: AgentDict,+ blocks: list[dict[str, Any]],+ node_lookup: dict[str, dict[str, Any]] | None = None,+ ) -> bool:+ """+ Validate that all sink_names in links and input_default keys in nodes+ exist in the corresponding block's input schema.++ Checks that for each link the sink_name references a valid input+ property in the sink block's inputSchema, and that every key in a+ node's input_default is a recognised input property. Also handles+ nested inputs with _#_ notation and dynamic schemas for+ AgentExecutorBlock.++ Args:+ agent: The agent dictionary to validate+ blocks: List of available blocks with their schemas+ node_lookup: Optional pre-built node-id → node dict++ Returns:+ True if all sink input fields exist, False otherwise+ """+ valid = True++ block_input_schemas = {+ block.get("id", ""): block.get("inputSchema", {}).get("properties", {})+ for block in blocks+ }+ block_names = {+ block.get("id", ""): block.get("name", "Unknown Block") for block in blocks+ }+ if node_lookup is None:+ node_lookup = self._build_node_lookup(agent)++ def get_input_props(node: dict[str, Any]) -> dict[str, Any]:+ block_id = node.get("block_id", "")+ if block_id == AGENT_EXECUTOR_BLOCK_ID:+ input_default = node.get("input_default", {})+ dynamic_input_schema = input_default.get("input_schema", {})+ if not isinstance(dynamic_input_schema, dict):+ dynamic_input_schema = {}+ dynamic_props = dynamic_input_schema.get("properties", {})+ if not isinstance(dynamic_props, dict):+ dynamic_props = {}+ static_props = block_input_schemas.get(block_id, {})+ return {**static_props, **dynamic_props}+ return block_input_schemas.get(block_id, {})++ def check_nested_input(+ input_props: dict[str, Any],+ field_name: str,+ context: str,+ block_name: str,+ block_id: str,+ ) -> bool:+ parent, child = field_name.split(DICT_SPLIT, 1)+ parent_schema = input_props.get(parent)+ if not parent_schema:+ self.add_error(+ f"{context}: Parent property '{parent}' does not "+ f"exist in block '{block_name}' ({block_id}) input "+ f"schema."+ )+ return False++ allows_additional = parent_schema.get("additionalProperties", False)+ # Only anyOf is checked here because Pydantic's JSON schema+ # emits optional/union fields via anyOf. allOf and oneOf are+ # not currently used by any block's dict-typed inputs, so+ # false positives from them are not a concern in practice.+ if not allows_additional and "anyOf" in parent_schema:+ for schema_option in parent_schema.get("anyOf", []):+ if not isinstance(schema_option, dict):+ continue+ if schema_option.get("additionalProperties"):+ allows_additional = True+ break+ items_schema = schema_option.get("items")+ if isinstance(items_schema, dict) and items_schema.get(+ "additionalProperties"+ ):+ allows_additional = True+ break++ if not allows_additional:+ if not (+ isinstance(parent_schema, dict)+ and "properties" in parent_schema+ and isinstance(parent_schema["properties"], dict)+ and child in parent_schema["properties"]+ ):+ available = (+ list(parent_schema.get("properties", {}).keys())+ if isinstance(parent_schema, dict)+ else []+ )+ self.add_error(+ f"{context}: Child property '{child}' does not "+ f"exist in parent '{parent}' of block "+ f"'{block_name}' ({block_id}) input schema. "+ f"Available properties: {available}"+ )+ return False+ return True++ for link in agent.get("links", []):+ sink_id = link.get("sink_id")+ sink_name = link.get("sink_name", "")+ link_id = link.get("id", "Unknown")++ if not sink_name:+ # Missing sink_name is caught by validate_data_type_compatibility+ continue++ sink_node = node_lookup.get(sink_id)+ if not sink_node:+ # Already caught by validate_link_node_references+ continue++ block_id = sink_node.get("block_id", "")+ block_name = block_names.get(block_id, "Unknown Block")+ input_props = get_input_props(sink_node)++ context = (+ f"Invalid sink input field '{sink_name}' in link "+ f"'{link_id}' to node '{sink_id}'"+ )++ if DICT_SPLIT in sink_name:+ if not check_nested_input(+ input_props, sink_name, context, block_name, block_id+ ):+ valid = False+ else:+ if sink_name not in input_props:+ available_inputs = list(input_props.keys())+ self.add_error(+ f"{context} (block '{block_name}' - {block_id}): "+ f"Input property '{sink_name}' does not exist in "+ f"the block's input schema. "+ f"Available inputs: {available_inputs}"+ )+ valid = False++ for node in agent.get("nodes", []):+ node_id = node.get("id")+ block_id = node.get("block_id", "")+ block_name = block_names.get(block_id, "Unknown Block")+ input_default = node.get("input_default", {})++ if not isinstance(input_default, dict) or not input_default:+ continue++ if (+ block_id not in block_input_schemas+ and block_id != AGENT_EXECUTOR_BLOCK_ID+ ):+ continue++ input_props = get_input_props(node)++ for key in input_default:+ if key == "credentials":+ continue++ context = (+ f"Node '{node_id}' (block '{block_name}' - {block_id}) "+ f"has unknown input_default key '{key}'"+ )++ if DICT_SPLIT in key:+ if not check_nested_input(+ input_props, key, context, block_name, block_id+ ):+ valid = False+ else:+ if key not in input_props:+ available_inputs = list(input_props.keys())+ self.add_error(+ f"{context} which does not exist in the "+ f"block's input schema. "+ f"Available inputs: {available_inputs}"+ )+ valid = False++ return valid+def validate_io_blocks(self, agent: AgentDict) -> bool:"""Validate that the agent has at least one AgentInputBlock and one@@ -998,14 +1100,14 @@ class AgentValidator:"Data type compatibility",self.validate_data_type_compatibility(agent, blocks, node_lookup),),- (- "Nested sink links",- self.validate_nested_sink_links(agent, blocks, node_lookup),- ),("Source output existence",self.validate_source_output_existence(agent, blocks, node_lookup),),+ (+ "Sink input existence",+ self.validate_sink_input_existence(agent, blocks, node_lookup),+ ),("Prompt double curly braces spaces",self.validate_prompt_double_curly_braces_spaces(agent),diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator_test.py b/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator_test.pyindex 1ce54aa7f4..54a2ed673d 100644--- a/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/agent_generator/validator_test.py@@ -331,43 +331,6 @@ class TestValidatePromptDoubleCurlyBracesSpaces:assert any("spaces" in e for e in v.errors)-# ============================================================================-# validate_nested_sink_links-# ============================================================================---class TestValidateNestedSinkLinks:- def test_valid_nested_link_passes(self):- v = AgentValidator()- block = _make_block(- block_id="b1",- input_schema={- "properties": {- "config": {- "type": "object",- "properties": {"key": {"type": "string"}},- }- },- "required": [],- },- )- node = _make_node(node_id="n1", block_id="b1")- link = _make_link(sink_id="n1", sink_name="config_#_key", source_id="n2")- agent = _make_agent(nodes=[node], links=[link])-- assert v.validate_nested_sink_links(agent, [block]) is True-- def test_invalid_parent_fails(self):- v = AgentValidator()- block = _make_block(block_id="b1")- node = _make_node(node_id="n1", block_id="b1")- link = _make_link(sink_id="n1", sink_name="nonexistent_#_key", source_id="n2")- agent = _make_agent(nodes=[node], links=[link])-- assert v.validate_nested_sink_links(agent, [block]) is False- assert any("does not exist" in e for e in v.errors)--# ============================================================================# validate_agent_executor_block_schemas# ============================================================================@@ -595,11 +558,28 @@ class TestValidate:input_block = _make_block(block_id=AGENT_INPUT_BLOCK_ID,name="AgentInputBlock",+ input_schema={+ "properties": {+ "name": {"type": "string"},+ "title": {"type": "string"},+ "value": {},+ "description": {"type": "string"},+ },+ "required": ["name"],+ },output_schema={"properties": {"result": {}}},)output_block = _make_block(block_id=AGENT_OUTPUT_BLOCK_ID,name="AgentOutputBlock",+ input_schema={+ "properties": {+ "name": {"type": "string"},+ "title": {"type": "string"},+ "value": {},+ },+ "required": ["name"],+ },)input_node = _make_node(node_id="n-in",@@ -650,6 +630,201 @@ class TestValidate:assert "AgentOutputBlock" in error_message+class TestValidateSinkInputExistence:+ """Tests for validate_sink_input_existence."""++ def test_valid_sink_name_passes(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={"properties": {"url": {"type": "string"}}, "required": []},+ )+ node = _make_node(node_id="n1", block_id="b1")+ link = _make_link(+ source_id="src", source_name="out", sink_id="n1", sink_name="url"+ )+ agent = _make_agent(nodes=[node], links=[link])++ assert v.validate_sink_input_existence(agent, [block]) is True++ def test_invalid_sink_name_fails(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={"properties": {"url": {"type": "string"}}, "required": []},+ )+ node = _make_node(node_id="n1", block_id="b1")+ link = _make_link(+ source_id="src", source_name="out", sink_id="n1", sink_name="nonexistent"+ )+ agent = _make_agent(nodes=[node], links=[link])++ assert v.validate_sink_input_existence(agent, [block]) is False+ assert any("nonexistent" in e for e in v.errors)++ def test_valid_nested_link_passes(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={+ "properties": {+ "config": {+ "type": "object",+ "properties": {"key": {"type": "string"}},+ }+ },+ "required": [],+ },+ )+ node = _make_node(node_id="n1", block_id="b1")+ link = _make_link(+ source_id="src",+ source_name="out",+ sink_id="n1",+ sink_name="config_#_key",+ )+ agent = _make_agent(nodes=[node], links=[link])++ assert v.validate_sink_input_existence(agent, [block]) is True++ def test_invalid_nested_child_fails(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={+ "properties": {+ "config": {+ "type": "object",+ "properties": {"key": {"type": "string"}},+ }+ },+ "required": [],+ },+ )+ node = _make_node(node_id="n1", block_id="b1")+ link = _make_link(+ source_id="src",+ source_name="out",+ sink_id="n1",+ sink_name="config_#_missing",+ )+ agent = _make_agent(nodes=[node], links=[link])++ assert v.validate_sink_input_existence(agent, [block]) is False++ def test_unknown_input_default_key_fails(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={"properties": {"url": {"type": "string"}}, "required": []},+ )+ node = _make_node(+ node_id="n1", block_id="b1", input_default={"nonexistent_key": "value"}+ )+ agent = _make_agent(nodes=[node])++ assert v.validate_sink_input_existence(agent, [block]) is False+ assert any("nonexistent_key" in e for e in v.errors)++ def test_credentials_key_skipped(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={"properties": {"url": {"type": "string"}}, "required": []},+ )+ node = _make_node(+ node_id="n1",+ block_id="b1",+ input_default={+ "url": "http://example.com",+ "credentials": {"api_key": "x"},+ },+ )+ agent = _make_agent(nodes=[node])++ assert v.validate_sink_input_existence(agent, [block]) is True++ def test_agent_executor_dynamic_schema_passes(self):+ v = AgentValidator()+ block = _make_block(+ block_id=AGENT_EXECUTOR_BLOCK_ID,+ input_schema={+ "properties": {+ "graph_id": {"type": "string"},+ "input_schema": {"type": "object"},+ },+ "required": ["graph_id"],+ },+ )+ node = _make_node(+ node_id="n1",+ block_id=AGENT_EXECUTOR_BLOCK_ID,+ input_default={+ "graph_id": "abc",+ "input_schema": {+ "properties": {"query": {"type": "string"}},+ "required": [],+ },+ },+ )+ link = _make_link(+ source_id="src",+ source_name="out",+ sink_id="n1",+ sink_name="query",+ )+ agent = _make_agent(nodes=[node], links=[link])++ assert v.validate_sink_input_existence(agent, [block]) is True++ def test_input_default_nested_invalid_child_fails(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={+ "properties": {+ "config": {+ "type": "object",+ "properties": {"key": {"type": "string"}},+ }+ },+ "required": [],+ },+ )+ node = _make_node(+ node_id="n1",+ block_id="b1",+ input_default={"config_#_invalid_child": "value"},+ )+ agent = _make_agent(nodes=[node])++ assert v.validate_sink_input_existence(agent, [block]) is False+ assert any("invalid_child" in e for e in v.errors)++ def test_input_default_nested_valid_child_passes(self):+ v = AgentValidator()+ block = _make_block(+ block_id="b1",+ input_schema={+ "properties": {+ "config": {+ "type": "object",+ "properties": {"key": {"type": "string"}},+ }+ },+ "required": [],+ },+ )+ node = _make_node(+ node_id="n1",+ block_id="b1",+ input_default={"config_#_key": "value"},+ )+ agent = _make_agent(nodes=[node])++ assert v.validate_sink_input_existence(agent, [block]) is True++class TestValidateMCPToolBlocks:"""Tests for validate_mcp_tool_blocks."""diff --git a/autogpt_platform/backend/test/agent_generator/test_orchestrator.py b/autogpt_platform/backend/test/agent_generator/test_orchestrator.pyindex 0d4d39c331..557db8016b 100644--- a/autogpt_platform/backend/test/agent_generator/test_orchestrator.py+++ b/autogpt_platform/backend/test/agent_generator/test_orchestrator.py@@ -526,7 +526,12 @@ class TestValidateOrchestratorBlocks:"id": AGENT_INPUT_BLOCK_ID,"name": "AgentInputBlock","inputSchema": {- "properties": {"name": {"type": "string"}},+ "properties": {+ "name": {"type": "string"},+ "title": {"type": "string"},+ "value": {},+ "description": {"type": "string"},+ },"required": ["name"],},"outputSchema": {"properties": {"result": {}}},@@ -537,6 +542,7 @@ class TestValidateOrchestratorBlocks:"inputSchema": {"properties": {"name": {"type": "string"},+ "title": {"type": "string"},"value": {},},"required": ["name"],@@ -683,7 +689,12 @@ class TestOrchestratorE2EPipeline:"id": AGENT_INPUT_BLOCK_ID,"name": "AgentInputBlock","inputSchema": {- "properties": {"name": {"type": "string"}},+ "properties": {+ "name": {"type": "string"},+ "title": {"type": "string"},+ "value": {},+ "description": {"type": "string"},+ },"required": ["name"],},"outputSchema": {"properties": {"result": {}}},@@ -694,6 +705,7 @@ class TestOrchestratorE2EPipeline:"inputSchema": {"properties": {"name": {"type": "string"},+ "title": {"type": "string"},"value": {},},"required": ["name"],diff --git a/docs/integrations/README.md b/docs/integrations/README.mdindex 2d2d74f42c..49b9a86dde 100644--- a/docs/integrations/README.md+++ b/docs/integrations/README.md@@ -342,7 +342,7 @@ Below is a comprehensive list of all available blocks, categorized by their prim| [Post To X](block-integrations/ayrshare/post_to_x.md#post-to-x) | Post to X / Twitter using Ayrshare || [Post To YouTube](block-integrations/ayrshare/post_to_youtube.md#post-to-youtube) | Post to YouTube using Ayrshare || [Publish To Medium](block-integrations/misc.md#publish-to-medium) | Publishes a post to Medium |-| [Read Discord Messages](block-integrations/discord/bot_blocks.md#read-discord-messages) | Reads messages from a Discord channel using a bot token |+| [Read Discord Messages](block-integrations/discord/bot_blocks.md#read-discord-messages) | Reads new messages from a Discord channel using a bot token and triggers when a new message is posted || [Reddit Get My Posts](block-integrations/misc.md#reddit-get-my-posts) | Fetch posts created by the authenticated Reddit user (you) || [Reply To Discord Message](block-integrations/discord/bot_blocks.md#reply-to-discord-message) | Replies to a specific Discord message || [Reply To Reddit Comment](block-integrations/misc.md#reply-to-reddit-comment) | Reply to a specific Reddit comment |diff --git a/docs/integrations/block-integrations/discord.md b/docs/integrations/block-integrations/discord.mdindex fa08093463..65e31c806e 100644--- a/docs/integrations/block-integrations/discord.md+++ b/docs/integrations/block-integrations/discord.md@@ -1,7 +1,7 @@## Read Discord Messages### What it is-A block that reads messages from a Discord channel using a bot token.+A block that reads new messages from a Discord channel using a bot token and triggers when a new message is posted.### What it doesThis block connects to Discord using a bot token and retrieves messages from a specified channel. It can operate continuously or retrieve a single message.diff --git a/docs/integrations/block-integrations/discord/bot_blocks.md b/docs/integrations/block-integrations/discord/bot_blocks.mdindex 978cdd021e..50bdf9c021 100644--- a/docs/integrations/block-integrations/discord/bot_blocks.md+++ b/docs/integrations/block-integrations/discord/bot_blocks.md@@ -132,7 +132,7 @@ The user must be visible to your bot (share a server with your bot).## Read Discord Messages### What it is-Reads messages from a Discord channel using a bot token.+Reads new messages from a Discord channel using a bot token and triggers when a new message is posted### How it works<!-- MANUAL: how_it_works -->