Skip to content

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/executor): ask for a file instead of silently skipping an empty picker (#14924)

Nicholas Tindlebd06fa8e1c4f
diff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.py
index ce7768dddf..968fff5300 100644
--- a/autogpt_platform/backend/backend/executor/utils.py
+++ b/autogpt_platform/backend/backend/executor/utils.py
@@ -5,14 +5,14 @@ import threading
import time
from collections import defaultdict
from concurrent.futures import Future
-from typing import Literal, Mapping, Optional, cast
+from typing import Literal, Mapping, Optional, cast, get_args
from prisma.enums import ReviewStatus
from pydantic import BaseModel, JsonValue, ValidationError
from backend.api.features.experts import scheduling as experts_scheduling
from backend.blocks import get_block
-from backend.blocks._base import Block, BlockCostType, BlockType
+from backend.blocks._base import Block, BlockCostType, BlockSchema, BlockType
from backend.copilot.rate_limit import UserPaywalledError, is_user_paywalled
from backend.data import execution as execution_db
from backend.data import graph as graph_db
@@ -482,8 +482,7 @@ async def _validate_node_input_credentials(
# `nodes_to_skip` here rather than relying on the post-loop
# guard — that guard only fires when the NODE-level
# ``is_creds_optional`` is True. For auto-credential fields the
- # optionality is usually field-level (``field_name not in
- # required_fields`` because the schema default is None), so
+ # optionality can be field-level (``_is_optional_picker``), so
# deferring would let the node silently pass validation and then
# crash in ``_acquire_auto_credentials`` at runtime. See Cursor
# thread PRRT_kwDOJKSTjM58r_37. Defined once per node (not per
@@ -583,8 +582,12 @@ async def _validate_node_input_credentials(
if auto_credentials_fields:
for _kwarg_name, info in auto_credentials_fields.items():
field_name = info["field_name"]
- field_is_optional = (
- is_creds_optional or field_name not in required_fields
+ if _is_linked(graph, node, field_name):
+ # An upstream block supplies the file at run time, so the
+ # stored value (possibly a leftover None) isn't what runs.
+ continue
+ field_is_optional = is_creds_optional or _is_optional_picker(
+ block.input_schema, field_name
)
# Check input_default and nodes_input_masks for the field value
field_value = node.input_default.get(field_name)
@@ -681,9 +684,12 @@ async def _validate_node_input_credentials(
_mark_optional_skip()
continue
has_missing_credentials = True
- credential_errors[node.id][
- field_name
- ] = f"{CRED_ERR_UNKNOWN_PREFIX}{cred_id}"
+ credential_errors[node.id][field_name] = (
+ f"{CRED_ERR_NOT_AVAILABLE_PREFIX} the selected file "
+ "was picked with an account you don't have connected "
+ "(it was removed, or it belongs to someone else). "
+ "Please select the file again with your own account."
+ )
# If node has optional credentials and any are missing, skip the
# node so the executor doesn't try to execute it with None creds.
@@ -703,6 +709,27 @@ async def _validate_node_input_credentials(
return credential_errors, nodes_to_skip
+def _is_optional_picker(input_schema: type[BlockSchema], field_name: str) -> bool:
+ """Whether a block can run with nothing picked in this picker input.
+
+ Picker fields (e.g. `GoogleDriveFileField`) always default to None, so the
+ schema's required fields can't answer this; the annotation does. An
+ `Optional[GoogleDriveFile]` input may stay empty and its node is skipped.
+ A plain `GoogleDriveFile` input needs a file, so leaving it empty (as a
+ fork or copy does) is an error the user can act on, not a silent skip.
+ """
+ field = input_schema.model_fields.get(field_name)
+ return field is not None and type(None) in get_args(field.annotation)
+
+
+def _is_linked(graph: GraphModel, node: Node, field_name: str) -> bool:
+ """Whether a link supplies the whole input. A link into one attribute of
+ it (e.g. `spreadsheet_@_id`) can't bring the `_credentials_id`."""
+ return any(
+ link.sink_id == node.id and link.sink_name == field_name for link in graph.links
+ )
+
+
def make_node_credentials_input_map(
graph: GraphModel,
graph_credentials_input: Mapping[str, CredentialsMetaInput],
diff --git a/autogpt_platform/backend/backend/executor/utils_test.py b/autogpt_platform/backend/backend/executor/utils_test.py
index 06eccafe89..08b4af30ab 100644
--- a/autogpt_platform/backend/backend/executor/utils_test.py
+++ b/autogpt_platform/backend/backend/executor/utils_test.py
@@ -1,10 +1,12 @@
from datetime import datetime, timezone
-from typing import cast
+from typing import Optional, cast
import pytest
from prisma.enums import ReviewStatus
from pytest_mock import MockerFixture
+from backend.blocks.google._drive import GoogleDriveFile
+from backend.blocks.google.sheets import GoogleSheetsReadBlock
from backend.data.dynamic_fields import merge_execution_input, parse_execution_output
from backend.data.execution import (
ExecutionContext,
@@ -19,6 +21,7 @@ from backend.executor.utils import (
CRED_ERR_NOT_AVAILABLE_PREFIX,
CRED_ERR_REQUIRED,
CRED_ERR_UNKNOWN_PREFIX,
+ _is_optional_picker,
add_graph_execution,
is_credential_validation_error_message,
)
@@ -1418,12 +1421,11 @@ async def test_validate_node_input_credentials_auto_creds_missing(
)
assert mock_node.id in errors
- assert "spreadsheet" in errors[mock_node.id]
- # Error message uses the CRED_ERR_UNKNOWN_PREFIX marker so the copilot
- # credential-race fallback recognises it as a credentials gate failure.
- assert (
- errors[mock_node.id]["spreadsheet"].lower().startswith("unknown credentials #")
- )
+ message = errors[mock_node.id]["spreadsheet"]
+ # A CRED_ERR_* marker lets the copilot credential-race fallback recognise
+ # it as a credentials gate failure, and the text says how to fix it.
+ assert is_credential_validation_error_message(message)
+ assert "select the file again with your own account" in message
@pytest.mark.asyncio
@@ -1754,12 +1756,9 @@ async def test_validate_node_input_credentials_field_level_optional_none_value_s
mocker: MockerFixture,
):
"""Cursor Medium (thread PRRT_kwDOJKSTjM58r_37): a node with
- ``credentials_optional=False`` (the default) but whose auto-credential
- field is NOT in ``required_fields`` (typical — the ``spreadsheet``
- field on Google Sheets blocks defaults to None, so pydantic marks it
- non-required at the schema level). The per-field check correctly
- flags ``field_is_optional=True`` via ``field_name not in
- required_fields``, but the POST-LOOP guard used ``is_creds_optional``
+ ``credentials_optional=False`` (the default) whose auto-credential field
+ is optional on its own: annotated ``Optional[GoogleDriveFile]``, see
+ ``_is_optional_picker``. The POST-LOOP guard used ``is_creds_optional``
(the node-level flag) only — so the node silently passed validation
and crashed at runtime inside ``_acquire_auto_credentials`` with
``ValueError('No file selected')``.
@@ -1784,9 +1783,11 @@ async def test_validate_node_input_credentials_field_level_optional_none_value_s
"config": {"provider": "google", "type": "oauth2"},
}
}
- # Field-level optional: `spreadsheet` is NOT in required_fields because
- # its pydantic default is None.
+ # Field-level optional: the annotation allows None.
mock_block.input_schema.get_required_fields.return_value = []
+ mock_block.input_schema.model_fields = {
+ "spreadsheet": mocker.MagicMock(annotation=Optional[GoogleDriveFile])
+ }
mock_node.block = mock_block
mock_graph = mocker.MagicMock()
@@ -1847,6 +1848,104 @@ async def test_validate_node_input_credentials_auto_creds_required_none_value_er
assert is_credential_validation_error_message(errors[mock_node.id]["spreadsheet"])
+def _picker_node(mocker: MockerFixture, spreadsheet_value, *, annotation):
+ """A node whose block has one picker input, `spreadsheet`, declared with
+ `annotation` and, like every GoogleDriveFileField, a None default."""
+ mock_node = mocker.MagicMock()
+ mock_node.id = "node-with-picker"
+ mock_node.credentials_optional = False
+ mock_node.input_default = {"spreadsheet": spreadsheet_value}
+ mock_block = mocker.MagicMock()
+ mock_block.input_schema.get_credentials_fields.return_value = {}
+ mock_block.input_schema.get_auto_credentials_fields.return_value = {
+ "credentials": {
+ "field_name": "spreadsheet",
+ "config": {"provider": "google", "type": "oauth2"},
+ }
+ }
+ mock_block.input_schema.get_required_fields.return_value = []
+ mock_block.input_schema.model_fields = {
+ "spreadsheet": mocker.MagicMock(annotation=annotation)
+ }
+ mock_node.block = mock_block
+ return mock_node
+
+
+async def test_validate_node_input_credentials_empty_required_picker_errors(
+ mocker: MockerFixture,
+):
+ """[SECRT-1772] A picker the block needs (a plain `GoogleDriveFile`) still
+ defaults to None, so it is never in the schema's required fields. Left
+ empty, as after a fork or copy, its node used to be skipped silently and
+ the run finished without doing that work. Now the run is refused with a
+ message asking for a file."""
+ from backend.executor.utils import _validate_node_input_credentials
+
+ mock_node = _picker_node(mocker, None, annotation=GoogleDriveFile)
+ mock_graph = mocker.MagicMock(nodes=[mock_node], links=[])
+
+ errors, nodes_to_skip = await _validate_node_input_credentials(
+ graph=mock_graph, user_id="some-user", nodes_input_masks=None
+ )
+
+ message = errors[mock_node.id]["spreadsheet"]
+ assert is_credential_validation_error_message(message)
+ assert "select a file" in message
+ assert mock_node.id not in nodes_to_skip
+
+
+async def test_validate_node_input_credentials_linked_picker_is_not_checked(
+ mocker: MockerFixture,
+):
+ """A picker fed by a link gets its file from the upstream block at run
+ time, so a leftover None in its stored value must not fail the run."""
+ from backend.executor.utils import _validate_node_input_credentials
+
+ mock_node = _picker_node(mocker, None, annotation=GoogleDriveFile)
+ link = mocker.MagicMock(sink_id=mock_node.id, sink_name="spreadsheet")
+ mock_graph = mocker.MagicMock(nodes=[mock_node], links=[link])
+
+ errors, nodes_to_skip = await _validate_node_input_credentials(
+ graph=mock_graph, user_id="some-user", nodes_input_masks=None
+ )
+
+ assert mock_node.id not in errors
+ assert mock_node.id not in nodes_to_skip
+
+
+async def test_validate_node_input_credentials_link_into_one_attribute_needs_a_file(
+ mocker: MockerFixture,
+):
+ """A link into one attribute of the picked file (`spreadsheet_@_id`) sets
+ only that attribute at run time. It can't bring the `_credentials_id` the
+ file needs, so an empty picker is still refused."""
+ from backend.executor.utils import _validate_node_input_credentials
+
+ mock_node = _picker_node(mocker, None, annotation=GoogleDriveFile)
+ link = mocker.MagicMock(sink_id=mock_node.id, sink_name="spreadsheet_@_id")
+ mock_graph = mocker.MagicMock(nodes=[mock_node], links=[link])
+
+ errors, _ = await _validate_node_input_credentials(
+ graph=mock_graph, user_id="some-user", nodes_input_masks=None
+ )
+
+ assert "select a file" in errors[mock_node.id]["spreadsheet"]
+
+
+def test_is_optional_picker_follows_the_annotation(mocker: MockerFixture):
+ """The Sheets blocks' pickers are plain `GoogleDriveFile` inputs, so they
+ are required; only a picker annotated to allow None may stay empty."""
+ optional_schema = mocker.MagicMock(
+ model_fields={"file": mocker.MagicMock(annotation=Optional[GoogleDriveFile])}
+ )
+
+ assert not _is_optional_picker(GoogleSheetsReadBlock().input_schema, "spreadsheet")
+ assert _is_optional_picker(optional_schema, "file")
+
+
# ============================================================================
# Tests for CredentialsFieldInfo auto_credential tag (Fix 4: Path 4)
# ============================================================================