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): pin resume identity and rebuild credential masks (#15324)

Nicholas Tindle41118b5f028c
diff --git a/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py b/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py
index dafef361e8..813b359736 100644
--- a/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py
@@ -10,13 +10,25 @@ from prisma.enums import ReviewStatus
from pytest_snapshot.plugin import Snapshot
from backend.api.rest_api import app
+from backend.blocks.github._auth import TEST_CREDENTIALS
+from backend.blocks.github.issues import GithubMakeIssueBlock
from backend.copilot.gate.held import HeldCall
from backend.data.execution import (
ExecutionContext,
ExecutionStatus,
+ GraphExecutionEntry,
NodeExecutionResult,
+ create_graph_execution,
+ update_graph_execution_stats,
)
-from backend.data.graph import GraphSettings
+from backend.data.graph import (
+ Graph,
+ GraphSettings,
+ Node,
+ create_graph,
+ make_graph_model,
+)
+from backend.util.exceptions import BlockExecutionError
from .model import PendingHumanReviewModel
@@ -1503,3 +1515,122 @@ async def test_an_approved_chat_card_sets_the_rule_it_asked_for(
set_scoped_rule.assert_awaited_once_with(
scope, test_user_id, expert, rule_key, "allow"
)
+
+
[email protected](loop_scope="session")
+async def test_a_rejected_irreversible_action_never_runs_when_the_run_resumes(
+ client: httpx.AsyncClient,
+ mocker: pytest_mock.MockerFixture,
+ test_user_id: str,
+) -> None:
+ """#15300 through the real route, review rows and gate.
+
+ The node pauses at the real gate, the user rejects it through
+ ``POST /api/review/action``, and the resume the route publishes is replayed
+ through ``Block.execute`` with that entry's context, as the executor would.
+ Only the queue, the outbound GitHub call and the user's safe-mode setting
+ are stubbed. Without the
+ identity pin the resumed context has no ``graph_exec_id``, the gate is
+ skipped and the issue is created anyway.
+ """
+ block = GithubMakeIssueBlock()
+ create_issue = mocker.patch.object(
+ GithubMakeIssueBlock,
+ "create_issue",
+ new=mocker.AsyncMock(
+ return_value=(1, "https://github.com/owner/repo/issues/1")
+ ),
+ )
+ queue = mocker.AsyncMock()
+ mocker.patch(
+ "backend.executor.utils.get_async_execution_queue",
+ new=mocker.AsyncMock(return_value=queue),
+ )
+
+ graph = make_graph_model(
+ Graph(
+ name="Rejected irreversible action",
+ description="",
+ nodes=[Node(block_id=block.id)],
+ links=[],
+ ),
+ test_user_id,
+ )
+ graph.reassign_ids(user_id=test_user_id, reassign_graph_id=True)
+ await create_graph(graph, user_id=test_user_id)
+ # The user's library setting that asks for irreversible actions to pause.
+ mocker.patch(
+ "backend.api.features.graph_executions.review.routes.get_graph_settings",
+ new=mocker.AsyncMock(
+ return_value=GraphSettings(sensitive_action_safe_mode=True)
+ ),
+ )
+
+ node_id = graph.nodes[0].id
+ input_data = dict(block.test_input)
+ graph_exec = await create_graph_execution(
+ graph_id=graph.id,
+ graph_version=graph.version,
+ starting_nodes_input=[(node_id, input_data)],
+ inputs={},
+ user_id=test_user_id,
+ )
+ node_exec_id = graph_exec.node_executions[0].node_exec_id
+
+ async def run_node(context: ExecutionContext) -> list:
+ return [
+ output
+ async for output in block.execute(
+ input_data,
+ execution_context=context.model_copy(
+ update={"node_id": node_id, "node_exec_id": node_exec_id}
+ ),
+ credentials=TEST_CREDENTIALS,
+ user_id=test_user_id,
+ node_id=node_id,
+ node_exec_id=node_exec_id,
+ graph_exec_id=graph_exec.id,
+ graph_id=graph.id,
+ graph_version=graph.version,
+ )
+ ]
+
+ # First pass: the gate parks the node for review.
+ assert (
+ await run_node(
+ ExecutionContext(
+ user_id=test_user_id,
+ graph_id=graph.id,
+ graph_exec_id=graph_exec.id,
+ graph_version=graph.version,
+ root_execution_id=graph_exec.id,
+ sensitive_action_safe_mode=True,
+ )
+ )
+ == []
+ )
+ create_issue.assert_not_awaited()
+ assert await update_graph_execution_stats(
+ graph_exec_id=graph_exec.id, status=ExecutionStatus.REVIEW
+ )
+
+ response = await client.post(
+ "/api/review/action",
+ json={
+ "reviews": [
+ {"node_exec_id": node_exec_id, "approved": False, "message": "No"}
+ ]
+ },
+ )
+ assert response.status_code == 200
+ assert response.json()["rejected_count"] == 1
+
+ queue.publish_message.assert_awaited_once()
+ resumed = GraphExecutionEntry.model_validate_json(
+ queue.publish_message.call_args.kwargs["message"]
+ ).execution_context
+ assert resumed.graph_exec_id == graph_exec.id
+
+ with pytest.raises(BlockExecutionError, match="rejected by reviewer"):
+ await run_node(resumed)
+ create_issue.assert_not_awaited()
diff --git a/autogpt_platform/backend/backend/api/features/graph_executions/review/workflow_card_db_test.py b/autogpt_platform/backend/backend/api/features/graph_executions/review/workflow_card_db_test.py
index 3cf8ca8ca1..c888d58f27 100644
--- a/autogpt_platform/backend/backend/api/features/graph_executions/review/workflow_card_db_test.py
+++ b/autogpt_platform/backend/backend/api/features/graph_executions/review/workflow_card_db_test.py
@@ -34,7 +34,7 @@ from backend.copilot.model import (
upsert_chat_session,
)
from backend.copilot.tools.run_capability import RunCapabilityTool
-from backend.data.execution import ExecutionContext
+from backend.data.execution import ExecutionContext, GraphExecutionEntry
from backend.data.human_review import (
cancel_pending_reviews_for_execution,
get_pending_reviews_for_user,
@@ -233,6 +233,51 @@ async def test_a_step_parks_on_its_card_and_the_card_resumes_the_run(
assert await run.step() == (False, _INPUT)
+async def test_a_run_its_card_resumed_asks_the_chat_again_at_its_next_step(
+ setup_test_user, test_user_id, flags, client
+):
+ """The resume the card publishes, replayed as the executor would: the run's
+ next irreversible step parks on a new card in the same chat (#15300)."""
+ chat, run = await _parked(test_user_id)
+ queue = AsyncMock()
+ with patch(
+ "backend.executor.utils.get_async_execution_queue",
+ AsyncMock(return_value=queue),
+ ):
+ await _answer(client, run.card_id, approved=True)
+
+ queue.publish_message.assert_awaited_once()
+ resumed = GraphExecutionEntry.model_validate_json(
+ queue.publish_message.call_args.kwargs["message"]
+ ).execution_context
+ assert (resumed.graph_exec_id, resumed.approval_chat_id) == (
+ run.graph_exec_id,
+ chat.session_id,
+ )
+
+ await run.next_step()
+ outputs = [
+ output
+ async for output in _BLOCK.execute(
+ _INPUT,
+ execution_context=resumed.model_copy(
+ update={"node_id": run.node_id, "node_exec_id": run.node_exec_id}
+ ),
+ user_id=test_user_id,
+ node_id=run.node_id,
+ node_exec_id=run.node_exec_id,
+ graph_exec_id=run.graph_exec_id,
+ graph_id=run.graph_id,
+ graph_version=1,
+ )
+ ]
+
+ assert outputs == []
+ card = await run.card()
+ assert card is not None and card.status == ReviewStatus.WAITING
+ assert card.session_id == chat.session_id
+
+
async def test_a_card_declined_in_chat_rejects_the_step_and_asks_from_now_on(
setup_test_user, test_user_id, flags, client, resumed
):
diff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.py
index 9c4f5a26bf..18c6152a77 100644
--- a/autogpt_platform/backend/backend/executor/utils.py
+++ b/autogpt_platform/backend/backend/executor/utils.py
@@ -1504,8 +1504,27 @@ async def _add_graph_execution(
graph_exec.status = ExecutionStatus.TERMINATED
return graph_exec
- # Use existing execution's compiled input masks
+ # The row stores the caller's raw masks and the run-time credential
+ # inputs separately; rebuild the credential masks the same way the
+ # create path compiles them, or nodes that get their credentials from
+ # the run dialog fail validation and are silently skipped (#15266).
compiled_nodes_input_masks = graph_exec.nodes_input_masks or {}
+ if graph_exec.credential_inputs:
+ resumed_graph = await gdb.get_graph(
+ graph_id=graph_exec.graph_id,
+ version=graph_exec.graph_version,
+ user_id=user_id,
+ include_subgraphs=True,
+ skip_access_check=True,
+ )
+ if resumed_graph:
+ # Same precedence as create: explicit masks override credentials.
+ compiled_nodes_input_masks = _merge_nodes_input_masks(
+ make_node_credentials_input_map(
+ resumed_graph, graph_exec.credential_inputs
+ ),
+ compiled_nodes_input_masks,
+ )
# For resumed executions, nodes_to_skip was already determined at creation time
# TODO: Consider storing nodes_to_skip in DB if we need to preserve it across resumes
nodes_to_skip: set[str] = set()
@@ -1696,6 +1715,20 @@ async def _add_graph_execution(
}
)
+ # Identity always comes from the persisted row. A caller-supplied context
+ # (review-resume builds one with only safety/user settings) would
+ # otherwise reach the executor with graph_exec_id=None, which skips the
+ # review gate for irreversible blocks and breaks file blocks (#15300).
+ execution_context = execution_context.model_copy(
+ update={
+ "user_id": graph_exec.user_id,
+ "graph_id": graph_exec.graph_id,
+ "graph_exec_id": graph_exec.id,
+ "graph_version": graph_exec.graph_version,
+ "root_execution_id": execution_context.root_execution_id or graph_exec.id,
+ }
+ )
+
stamp: dict[str, Any] = {
"approval_chat_id": (
None
diff --git a/autogpt_platform/backend/backend/executor/utils_test.py b/autogpt_platform/backend/backend/executor/utils_test.py
index 682dd9edaa..7605b24ce7 100644
--- a/autogpt_platform/backend/backend/executor/utils_test.py
+++ b/autogpt_platform/backend/backend/executor/utils_test.py
@@ -378,6 +378,9 @@ async def test_add_graph_execution_is_repeatable(mocker: MockerFixture):
# Mock the graph execution object
mock_graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec.is_dry_run = False
+ mock_graph_exec.user_id = "test-user-id"
+ mock_graph_exec.graph_id = "test-graph-id"
+ mock_graph_exec.credential_inputs = None
mock_graph_exec.organization_id = None
mock_graph_exec.expert_id = None
mock_graph_exec.team_id = None
@@ -483,6 +486,9 @@ async def test_add_graph_execution_is_repeatable(mocker: MockerFixture):
# Create a second mock execution for the sanity check
mock_graph_exec_2 = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec_2.is_dry_run = False
+ mock_graph_exec_2.user_id = "test-user-id"
+ mock_graph_exec_2.graph_id = "test-graph-id"
+ mock_graph_exec_2.credential_inputs = None
mock_graph_exec_2.organization_id = None
mock_graph_exec_2.expert_id = None
mock_graph_exec_2.team_id = None
@@ -545,6 +551,9 @@ async def test_add_graph_execution_via_rpc_returns_typed_user(
mock_graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec.is_dry_run = False
+ mock_graph_exec.user_id = "test-user-id"
+ mock_graph_exec.graph_id = "test-graph-id"
+ mock_graph_exec.credential_inputs = None
mock_graph_exec.organization_id = None
mock_graph_exec.expert_id = None
mock_graph_exec.team_id = None
@@ -638,6 +647,9 @@ async def test_add_graph_execution_born_tenanted_via_rpc_when_prisma_disconnecte
mock_graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec.is_dry_run = False
+ mock_graph_exec.user_id = "test-user-id"
+ mock_graph_exec.graph_id = "test-graph-id"
+ mock_graph_exec.credential_inputs = None
mock_graph_exec.organization_id = "org-rpc"
mock_graph_exec.expert_id = None
mock_graph_exec.team_id = "team-rpc"
@@ -866,6 +878,9 @@ async def test_add_graph_execution_with_nodes_to_skip(mocker: MockerFixture):
# Mock the graph execution object
mock_graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec.is_dry_run = False
+ mock_graph_exec.user_id = "test-user-id"
+ mock_graph_exec.graph_id = "test-graph-id"
+ mock_graph_exec.credential_inputs = None
mock_graph_exec.organization_id = None
mock_graph_exec.expert_id = None
mock_graph_exec.team_id = None
@@ -960,6 +975,9 @@ async def test_add_graph_execution_resume_backfills_org_from_row(mocker: MockerF
# Existing row carries org/team; the resume caller's context does not.
mock_graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec.is_dry_run = False
+ mock_graph_exec.user_id = "test-user-id"
+ mock_graph_exec.graph_id = "test-graph-id"
+ mock_graph_exec.credential_inputs = None
mock_graph_exec.id = "exec-resume-1"
mock_graph_exec.node_executions = []
mock_graph_exec.status = ExecutionStatus.QUEUED
@@ -2174,6 +2192,9 @@ def _mock_add_graph_execution_create_path(
mock_graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
mock_graph_exec.is_dry_run = False
+ mock_graph_exec.user_id = "test-user-id"
+ mock_graph_exec.graph_id = "test-graph-id"
+ mock_graph_exec.credential_inputs = None
mock_graph_exec.organization_id = org_id
mock_graph_exec.expert_id = None
mock_graph_exec.team_id = team_id
@@ -2267,6 +2288,9 @@ def _mock_add_graph_execution_requeue_path(
graph_exec = mocker.MagicMock(spec=GraphExecutionWithNodes)
graph_exec.is_dry_run = False
+ graph_exec.user_id = "test-user-id"
+ graph_exec.graph_id = "test-graph-id"
+ graph_exec.credential_inputs = None
graph_exec.id = "existing-execution"
graph_exec.node_executions = []
graph_exec.status = ExecutionStatus.QUEUED
@@ -3259,6 +3283,224 @@ async def test_subgraph_inherits_the_pause_through_its_context(
assert child_context.sensitive_action_safe_mode is True
+# ============================================================================
+# Resume path: identity (#15300) and run-time credential masks (#15266).
+# Uses a real GraphExecutionWithNodes and decodes the real published entry,
+# so a mock can't paper over a missing field.
+# ============================================================================
+
+
+def _resumable_graph_exec(**overrides):
+ from backend.data.execution import GraphExecutionWithNodes
+
+ fields: dict = dict(
+ id="exec-resume-real",
+ user_id="row-user",
+ graph_id="row-graph",
+ graph_version=3,
+ inputs={},
+ outputs={},
+ credential_inputs=None,
+ nodes_input_masks=None,
+ preset_id=None,
+ status=ExecutionStatus.REVIEW,
+ stats=None,
+ node_executions=[],
+ organization_id="org-row",
+ team_id="team-row",
+ )
+ fields.update(overrides)
+ return GraphExecutionWithNodes(**fields)
+
+
+async def _resume_and_capture_entry(mocker: MockerFixture, graph_exec, graph=None):
+ """Resume ``graph_exec`` with a review-resume style partial context and
+ return the GraphExecutionEntry that was published to the queue."""
+ from backend.data.execution import ExecutionContext, GraphExecutionEntry
+ from backend.executor.utils import add_graph_execution
+
+ mocker.patch("backend.executor.utils.prisma").is_connected.return_value = True
+ mocker.patch(
+ "backend.executor.utils.is_user_paywalled",
+ new=mocker.AsyncMock(return_value=False),
+ )
+ mock_edb = mocker.patch("backend.executor.utils.execution_db")
+ mock_edb.get_graph_execution = mocker.AsyncMock(return_value=graph_exec)
+ mock_edb.update_graph_execution_stats = mocker.AsyncMock(
+ return_value=graph_exec.model_copy(update={"status": ExecutionStatus.QUEUED})
+ )
+ mock_gdb = mocker.patch("backend.executor.utils.graph_db")
+ mock_gdb.get_graph = mocker.AsyncMock(return_value=graph)
+ queue = mocker.AsyncMock()
+ mocker.patch(
+ "backend.executor.utils.get_async_execution_queue",
+ new=mocker.AsyncMock(return_value=queue),
+ )
+ mocker.patch(
+ "backend.executor.utils.get_async_execution_event_bus"
+ ).return_value = mocker.MagicMock(publish=mocker.AsyncMock())
+
+ # What process_review_action passes: safety/user settings, no identity.
+ review_resume_ctx = ExecutionContext(
+ human_in_the_loop_safe_mode=True,
+ sensitive_action_safe_mode=True,
+ user_timezone="UTC",
+ workspace_id="ws-1",
+ )
+ await add_graph_execution(
+ graph_id=graph_exec.graph_id,
+ user_id=graph_exec.user_id,
+ graph_exec_id=graph_exec.id,
+ execution_context=review_resume_ctx,
+ )
+
+ queue.publish_message.assert_awaited_once()
+ return GraphExecutionEntry.model_validate_json(
+ queue.publish_message.call_args.kwargs["message"]
+ )
+
+
+async def test_resume_pins_execution_identity_from_the_row(mocker: MockerFixture):
+ """#15300: review-resume passed a context without graph_exec_id, so the
+ executor skipped the review gate for irreversible blocks on resume."""
+ entry = await _resume_and_capture_entry(mocker, _resumable_graph_exec())
+
+ ctx = entry.execution_context
+ assert ctx.graph_exec_id == "exec-resume-real"
+ assert ctx.user_id == "row-user"
+ assert ctx.graph_id == "row-graph"
+ assert ctx.graph_version == 3
+ assert ctx.root_execution_id == "exec-resume-real"
+ # Caller-supplied settings survive the pin.
+ assert ctx.sensitive_action_safe_mode is True
+ assert ctx.workspace_id == "ws-1"
+ assert ctx.organization_id == "org-row"
+
+
+def _github_creds():
+ from backend.data.model import CredentialsMetaInput
+
+ return CredentialsMetaInput(
+ id="cred-1", title="GitHub", provider="github", type="api_key"
+ )
+
+
+def _graph_with_credentials_input(mocker: MockerFixture):
+ graph = mocker.MagicMock()
+ graph.regular_credentials_inputs = {
+ "github_credentials": (None, {("github-node", "credentials")}, True)
+ }
+ return graph
+
+
+async def test_resume_rebuilds_credential_masks_from_row(mocker: MockerFixture):
+ """#15266: credentials picked in the run dialog are persisted as
+ credential_inputs, not as masks; resume must recompile them."""
+ graph_exec = _resumable_graph_exec(
+ credential_inputs={"github_credentials": _github_creds()},
+ nodes_input_masks={},
+ )
+
+ entry = await _resume_and_capture_entry(
+ mocker, graph_exec, graph=_graph_with_credentials_input(mocker)
+ )
+
+ assert entry.nodes_input_masks is not None
+ assert entry.nodes_input_masks["github-node"]["credentials"] == {
+ "id": "cred-1",
+ "title": "GitHub",
+ "provider": "github",
+ "type": "api_key",
+ }
+
+
+async def test_resume_raw_masks_override_rebuilt_credentials(mocker: MockerFixture):
+ """Same precedence as the create path: explicit masks win."""
+ graph_exec = _resumable_graph_exec(
+ credential_inputs={"github_credentials": _github_creds()},
+ nodes_input_masks={"github-node": {"credentials": {"id": "explicit"}}},
+ )
+
+ entry = await _resume_and_capture_entry(
+ mocker, graph_exec, graph=_graph_with_credentials_input(mocker)
+ )
+
+ assert entry.nodes_input_masks is not None
+ assert entry.nodes_input_masks["github-node"]["credentials"] == {"id": "explicit"}
+
+
+async def test_a_subgraph_run_gets_its_own_identity_and_keeps_its_tree(
+ mocker: MockerFixture,
+):
+ """AgentExecutorBlock creates the child with the parent's context.
+
+ The child must run under its own execution, graph and version, so its
+ files and LLM tracking stay its own, while keeping the parent's tree so
+ the executor still skips it once the root is terminated.
+ """
+ from backend.data.execution import ExecutionContext, GraphExecutionEntry
+ from backend.executor.utils import add_graph_execution
+
+ child = _resumable_graph_exec(
+ id="child-exec",
+ graph_id="child-graph",
+ graph_version=2,
+ status=ExecutionStatus.QUEUED,
+ )
+ mocker.patch(
+ "backend.executor.utils.validate_and_construct_node_execution_input",
+ new=mocker.AsyncMock(return_value=(mocker.MagicMock(version=2), [], {}, set())),
+ )
+ mocker.patch("backend.executor.utils.prisma").is_connected.return_value = True
+ mocker.patch(
+ "backend.executor.utils.is_user_paywalled",
+ new=mocker.AsyncMock(return_value=False),
+ )
+ mock_edb = mocker.patch("backend.executor.utils.execution_db")
+ mock_edb.create_graph_execution = mocker.AsyncMock(return_value=child)
+ mock_edb.update_graph_execution_stats = mocker.AsyncMock(return_value=child)
+ mocker.patch("backend.executor.utils.onboarding_db")
+ queue = mocker.AsyncMock()
+ mocker.patch(
+ "backend.executor.utils.get_async_execution_queue",
+ new=mocker.AsyncMock(return_value=queue),
+ )
+ mocker.patch(
+ "backend.executor.utils.get_async_execution_event_bus"
+ ).return_value = mocker.MagicMock(publish=mocker.AsyncMock())
+
+ await add_graph_execution(
+ graph_id="child-graph",
+ user_id="row-user",
+ execution_context=ExecutionContext(
+ graph_exec_id="parent-exec",
+ graph_id="parent-graph",
+ graph_version=7,
+ root_execution_id="root-exec",
+ parent_execution_id="parent-exec",
+ ),
+ organization_id="org-row",
+ team_id="team-row",
+ )
+
+ ctx = GraphExecutionEntry.model_validate_json(
+ queue.publish_message.call_args.kwargs["message"]
+ ).execution_context
+ assert (ctx.graph_exec_id, ctx.graph_id, ctx.graph_version) == (
+ "child-exec",
+ "child-graph",
+ 2,
+ )
+ assert (ctx.root_execution_id, ctx.parent_execution_id) == (
+ "root-exec",
+ "parent-exec",
+ )
+
+
# ============================================================================
# A run carries the chat that started it, whose approval gate answers its
# steps' review pauses (OPEN-3592).