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): list graph versions with their sub-graphs so the credentials schema matches the single-graph read (#15335)
Reinier van der Leerfdb7ee2d03fb
diff --git a/autogpt_platform/backend/backend/api/features/graphs/routes.py b/autogpt_platform/backend/backend/api/features/graphs/routes.pyindex 8886de2842..49e3181db4 100644--- a/autogpt_platform/backend/backend/api/features/graphs/routes.py+++ b/autogpt_platform/backend/backend/api/features/graphs/routes.py@@ -90,7 +90,10 @@ async def get_graph_all_versions(ctx: Annotated[RequestContext, Security(get_request_context)],) -> Sequence[graph_db.GraphModel]:graphs = await graph_db.get_graph_all_versions(- graph_id, user_id=user_id, organization_id=ctx.org_id+ graph_id,+ user_id=user_id,+ organization_id=ctx.org_id,+ include_subgraphs=True, # needed to construct full credentials input schema)if not graphs:raise HTTPException(status_code=404, detail=f"Workflow #{graph_id} not found.")diff --git a/autogpt_platform/backend/backend/api/features/graphs/routes_db_test.py b/autogpt_platform/backend/backend/api/features/graphs/routes_db_test.pynew file mode 100644index 0000000000..4ede2f116b--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/graphs/routes_db_test.py@@ -0,0 +1,145 @@+from uuid import uuid4++import pytest+from autogpt_libs.auth.models import RequestContext+from prisma.actions import AgentGraphActions+from pytest_mock import MockerFixture++from backend.api.features.graphs.routes import get_graph, get_graph_all_versions+from backend.blocks.agent import AgentExecutorBlock+from backend.blocks.code_executor import ExecuteCodeBlock+from backend.blocks.github.issues import GithubCommentBlock+from backend.data import graph as graph_db+from backend.data.user import get_or_create_user+from backend.util.test import SpinTestServer++[email protected](loop_scope="session")+async def test_listed_versions_carry_the_credentials_their_sub_graphs_need(+ server: SpinTestServer, mocker: MockerFixture+) -> None:+ user_id = await _new_user()+ code, github = await _leaf_graphs(user_id)+ parent_id = str(uuid4())+ # v1 runs one sub-graph and v2 both, so a version given the other's sub-graphs shows+ for version, subs in ((1, [code]), (2, [code, github])):+ await graph_db.create_graph(+ graph_db.Graph(+ id=parent_id,+ version=version,+ name="Parent",+ description="",+ nodes=[_agent_executor_node(user_id, sub) for sub in subs],+ ),+ user_id,+ )+ ctx = _request_context(user_id)++ find_many = mocker.spy(AgentGraphActions, "find_many")+ listed = {+ v.version: v for v in await get_graph_all_versions(parent_id, user_id, ctx)+ }+ sub_graph_fetches = [+ c for c in find_many.call_args_list if "OR" in c.kwargs["where"]+ ]++ assert len(sub_graph_fetches) == 1+ assert {v: {s.id for s in g.sub_graphs} for v, g in listed.items()} == {+ 1: {code.id},+ 2: {code.id, github.id},+ }+ assert listed[1].credentials_input_schema != listed[2].credentials_input_schema+ for version, graph in listed.items():+ single = await get_graph(parent_id, user_id, ctx, version=version)+ assert graph.credentials_input_schema == single.credentials_input_schema++[email protected](loop_scope="session")+async def test_a_listed_version_walks_both_versions_of_a_sub_graph_it_calls(+ server: SpinTestServer,+) -> None:+ user_id = await _new_user()+ code, github = await _leaf_graphs(user_id)+ # W v1 and v2 sit on one level and call different leaves, so walking one W loses one+ w_id = str(uuid4())+ w_versions = [+ await graph_db.create_graph(+ graph_db.Graph(+ id=w_id,+ version=version,+ name="W",+ description="",+ nodes=[_agent_executor_node(user_id, leaf)],+ ),+ user_id,+ )+ for version, leaf in ((1, code), (2, github))+ ]+ parent = await graph_db.create_graph(+ graph_db.Graph(+ name="Parent",+ description="",+ nodes=[_agent_executor_node(user_id, w) for w in w_versions],+ ),+ user_id,+ )+ ctx = _request_context(user_id)++ [listed] = await get_graph_all_versions(parent.id, user_id, ctx)+ single = await get_graph(parent.id, user_id, ctx, version=parent.version)++ assert {s.id for s in listed.sub_graphs} == {w_id, code.id, github.id}+ assert set(listed.credentials_input_schema["properties"]) == {+ *code.credentials_input_schema["properties"],+ *github.credentials_input_schema["properties"],+ }+ assert listed.credentials_input_schema == single.credentials_input_schema+++async def _new_user() -> str:+ user_id = str(uuid4())+ await get_or_create_user({"sub": user_id, "email": f"{user_id}@example.com"})+ return user_id+++async def _leaf_graphs(user_id: str) -> list[graph_db.GraphModel]:+ """One graph needing E2B credentials and one needing GitHub's."""+ return [+ await graph_db.create_graph(+ graph_db.Graph(+ name=block.name,+ description="",+ nodes=[graph_db.Node(block_id=block.id)],+ ),+ user_id,+ )+ for block in (ExecuteCodeBlock(), GithubCommentBlock())+ ]+++def _request_context(user_id: str) -> RequestContext:+ return RequestContext(+ user_id=user_id,+ org_id=str(uuid4()),+ team_id=None,+ is_org_owner=True,+ is_org_admin=True,+ is_org_billing_manager=False,+ is_team_admin=True,+ is_team_billing_manager=False,+ seat_status="ACTIVE",+ )+++def _agent_executor_node(user_id: str, sub: graph_db.GraphModel) -> graph_db.Node:+ return graph_db.Node(+ block_id=AgentExecutorBlock().id,+ input_default={+ "user_id": user_id,+ "graph_id": sub.id,+ "graph_version": sub.version,+ "inputs": {},+ "input_schema": {},+ "output_schema": {},+ },+ )diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.pyindex ad379952fb..4f8d85ee32 100644--- a/autogpt_platform/backend/backend/data/graph.py+++ b/autogpt_platform/backend/backend/data/graph.py@@ -2,7 +2,7 @@ import asyncioimport loggingimport uuidfrom collections import defaultdict-from collections.abc import Container, Mapping+from collections.abc import Container, Mapping, Sequencefrom datetime import datetime, timezonefrom typing import (TYPE_CHECKING,@@ -1713,52 +1713,80 @@ async def get_graph_as_admin(async def get_sub_graphs(graph: AgentGraph) -> list[AgentGraph]:+ """All sub-graphs of a given graph, recursively, flattened into a list."""+ [sub_graphs] = await get_sub_graphs_of_each([graph])+ return sub_graphs+++async def get_sub_graphs_of_each(+ graphs: Sequence[AgentGraph],+) -> list[list[AgentGraph]]:"""- Iteratively fetches all sub-graphs of a given graph, and flattens them into a list.- This call involves a DB fetch in batch, breadth-first, per-level of graph depth.- On each DB fetch we will only fetch the sub-graphs that are not already in the list.+ Each graph's sub-graphs, recursively, flattened, in the order of `graphs`.+ Fetched breadth-first with one DB query per level of depth for all `graphs`+ together, so a sub-graph they share (e.g. across versions) is fetched once."""- sub_graphs = {graph.id: graph}- search_graphs = [graph]agent_block_id = AgentExecutorBlock().id+ fetched: dict[tuple[str, int, str], AgentGraph | None] = {}+ found = [{graph.id: graph} for graph in graphs]+ frontiers = [[graph] for graph in graphs]++ while any(frontiers):+ refs = [+ [+ # A sub-graph counts only if the root graph's owner owns it+ (graph_id, graph_version, root.userId)+ for graph in frontier+ for graph_id, graph_version in _sub_graph_refs(graph, agent_block_id)+ ]+ for root, frontier in zip(graphs, frontiers)+ ]+ if missing := {ref for level in refs for ref in level} - fetched.keys():+ fetched.update(dict.fromkeys(missing))+ rows = await AgentGraph.prisma().find_many(+ where={+ "OR": [+ {"id": graph_id, "version": graph_version, "userId": user_id}+ for graph_id, graph_version, user_id in missing+ ]+ },+ include=AGENT_GRAPH_INCLUDE,+ )+ fetched.update({(row.id, row.version, row.userId): row for row in rows})- while search_graphs:- sub_graph_ids = [- (graph_id, graph_version)- for graph in search_graphs- for node in graph.Nodes or []- if (- node.AgentBlock- and node.AgentBlock.id == agent_block_id- and (graph_id := cast(str, dict(node.constantInput).get("graph_id")))- and (- graph_version := cast(- int, dict(node.constantInput).get("graph_version")- )- )+ for i, level in enumerate(refs):+ # Walk every version a level references, as get_sub_graphs always+ # has; the result still keeps one graph per id.+ frontiers[i] = list(+ {+ ref: graph+ for ref in level+ if (graph := fetched[ref]) and graph.id not in found[i]+ }.values())- ]- if not sub_graph_ids:- break+ found[i].update({graph.id: graph for graph in frontiers[i]})- graphs = await AgentGraph.prisma().find_many(- where={- "OR": [- {- "id": graph_id,- "version": graph_version,- "userId": graph.userId, # Ensure the sub-graph is owned by the same user- }- for graph_id, graph_version in sub_graph_ids- ]- },- include=AGENT_GRAPH_INCLUDE,- )+ return [+ [g for g in sub_graphs.values() if g.id != root.id]+ for root, sub_graphs in zip(graphs, found)+ ]- search_graphs = [graph for graph in graphs if graph.id not in sub_graphs]- sub_graphs.update({graph.id: graph for graph in search_graphs})- return [g for g in sub_graphs.values() if g.id != graph.id]+def _sub_graph_refs(graph: AgentGraph, agent_block_id: str) -> list[tuple[str, int]]:+ return [+ (graph_id, graph_version)+ for node in graph.Nodes or []+ if (+ node.AgentBlock+ and node.AgentBlock.id == agent_block_id+ and (graph_id := cast(str, dict(node.constantInput).get("graph_id")))+ and (+ graph_version := cast(+ int, dict(node.constantInput).get("graph_version")+ )+ )+ )+ ]async def get_connected_output_nodes(node_id: str) -> list[tuple[Link, Node]]:@@ -1804,6 +1832,7 @@ async def get_graph_all_versions(limit: int = MAX_GRAPH_VERSIONS_FETCH,team_id: str | None = None,organization_id: str | None = None,+ include_subgraphs: bool = False,) -> list[GraphModel]:where_clause: AgentGraphWhereInput = {"id": graph_id}if organization_id is not None:@@ -1832,7 +1861,15 @@ async def get_graph_all_versions(if not graph_versions:return []- versions = [GraphModel.from_db(graph) for graph in graph_versions]+ sub_graphs = (+ await get_sub_graphs_of_each(graph_versions)+ if include_subgraphs+ else [None] * len(graph_versions)+ )+ versions = [+ GraphModel.from_db(graph, sub_graphs=subs)+ for graph, subs in zip(graph_versions, sub_graphs)+ ]for version in versions:if version.user_id != user_id:# A teammate reading the history: only the owner sees the files