Repository navigation
UN-3702 [FEAT] Hide deprecated runner tools from new workflows and block adding them #2298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Deepak-Kesavan
wants to merge
2
commits into
main
Choose a base branch
from
UN-3702-hide-runner-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
backend/tool_instance_v2/tests/test_deprecated_registry_tools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| """Registry tools (Classifier, Text Extractor) run on the deprecated Docker | ||
| runner: new workflows must not be able to pick them, while workflows that | ||
| already use one keep seeing it. | ||
|
|
||
| Unit tests: collaborators are patched per-test, so no database is touched. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| import uuid | ||
| from typing import Any | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from rest_framework.test import APIRequestFactory, force_authenticate | ||
|
|
||
| from tool_instance_v2 import serializers as _ser_mod | ||
| from tool_instance_v2 import tool_processor as _tp_mod | ||
| from tool_instance_v2 import views as _views_mod | ||
|
|
||
| ToolProcessor = _tp_mod.ToolProcessor | ||
| ToolInstanceSerializer = _ser_mod.ToolInstanceSerializer | ||
|
|
||
| PROMPT_STUDIO_TOOL_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" | ||
| WORKFLOW_ID = "11111111-2222-3333-4444-555555555555" | ||
| REPO_TOOL_REGISTRY_CONFIG = ( | ||
| Path(__file__).resolve().parents[3] / "unstract/tool-registry/tool_registry_config" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def repo_tool_registry(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Resolve tools from the repo's `public_tools.json`, not a deployed volume.""" | ||
| monkeypatch.setenv("TOOL_REGISTRY_CONFIG_PATH", str(REPO_TOOL_REGISTRY_CONFIG)) | ||
|
|
||
|
|
||
| def _tool(function_name: str) -> dict[str, Any]: | ||
| return {"name": function_name, "function_name": function_name} | ||
|
|
||
|
|
||
| def _function_names(tools: list[dict[str, Any]]) -> list[str]: | ||
| return [tool["function_name"] for tool in tools] | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("repo_tool_registry") | ||
| class TestIsRegistryTool: | ||
| """Reads the real `public_tools.json`, so a renamed tool id fails here.""" | ||
|
|
||
| @pytest.mark.parametrize("tool_uid", ["classify", "text_extractor"]) | ||
| def test_public_registry_tools_are_registry_tools(self, tool_uid: str) -> None: | ||
| assert ToolProcessor.is_registry_tool(tool_uid) | ||
|
|
||
| def test_prompt_studio_tool_is_not_a_registry_tool(self) -> None: | ||
| assert not ToolProcessor.is_registry_tool(PROMPT_STUDIO_TOOL_ID) | ||
|
|
||
|
|
||
| class TestGetToolList: | ||
| def _get_tool_list(self, tool_ids_in_workflow: set[str]) -> list[str]: | ||
| registry = MagicMock() | ||
| registry.fetch_tools_descriptions.return_value = [ | ||
| _tool("classify"), | ||
| _tool("text_extractor"), | ||
| ] | ||
| with ( | ||
| patch.object(_tp_mod, "ToolRegistry", MagicMock(return_value=registry)), | ||
| patch.object(_tp_mod, "IS_AGENTIC_REGISTRY_AVAILABLE", False), | ||
| patch.object( | ||
| _tp_mod.PromptStudioRegistryHelper, | ||
| "fetch_json_for_registry", | ||
| MagicMock(return_value=[_tool(PROMPT_STUDIO_TOOL_ID)]), | ||
| ), | ||
| patch.object( | ||
| ToolProcessor, | ||
| "_get_tool_ids_in_workflow", | ||
| MagicMock(return_value=tool_ids_in_workflow), | ||
| ), | ||
| ): | ||
| return _function_names( | ||
| ToolProcessor.get_tool_list(MagicMock(name="user"), WORKFLOW_ID) | ||
| ) | ||
|
|
||
| def test_registry_tools_are_hidden_from_a_workflow_without_them(self) -> None: | ||
| assert self._get_tool_list(set()) == [PROMPT_STUDIO_TOOL_ID] | ||
|
|
||
| def test_registry_tool_already_in_the_workflow_stays_listed(self) -> None: | ||
| assert self._get_tool_list({"classify"}) == [ | ||
| "classify", | ||
| PROMPT_STUDIO_TOOL_ID, | ||
| ] | ||
|
|
||
| def test_prompt_studio_tool_in_the_workflow_does_not_unhide_registry_tools( | ||
| self, | ||
| ) -> None: | ||
| assert self._get_tool_list({PROMPT_STUDIO_TOOL_ID}) == [PROMPT_STUDIO_TOOL_ID] | ||
|
|
||
| def test_no_workflow_means_no_tool_ids_and_no_query(self) -> None: | ||
| with patch.object(_tp_mod.Workflow, "objects") as workflow_objects: | ||
| assert ToolProcessor._get_tool_ids_in_workflow(MagicMock(), None) == set() | ||
| workflow_objects.for_user.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("repo_tool_registry") | ||
| class TestToolIdValidation: | ||
| """`is_valid` is what the viewset's create and update both run.""" | ||
|
|
||
| def _errors(self, data: dict[str, Any], instance: Any = None) -> dict[str, Any]: | ||
| serializer = ToolInstanceSerializer( | ||
| instance, data=data, partial=instance is not None | ||
| ) | ||
| serializer.is_valid() | ||
| return serializer.errors | ||
|
|
||
| @pytest.mark.parametrize("tool_id", ["classify", "text_extractor"]) | ||
| def test_create_with_a_registry_tool_is_rejected(self, tool_id: str) -> None: | ||
| errors = self._errors({"workflow_id": WORKFLOW_ID, "tool_id": tool_id}) | ||
| assert "deprecated" in str(errors["tool_id"]) | ||
|
|
||
| def test_create_with_a_prompt_studio_tool_is_accepted(self) -> None: | ||
| errors = self._errors( | ||
| {"workflow_id": WORKFLOW_ID, "tool_id": PROMPT_STUDIO_TOOL_ID} | ||
| ) | ||
| assert "tool_id" not in errors | ||
|
|
||
| def test_switching_an_instance_to_a_registry_tool_is_rejected(self) -> None: | ||
| instance = MagicMock(name="instance", tool_id=PROMPT_STUDIO_TOOL_ID) | ||
| errors = self._errors({"tool_id": "classify"}, instance=instance) | ||
| assert "deprecated" in str(errors["tool_id"]) | ||
|
|
||
| def test_an_instance_keeps_the_registry_tool_it_already_has(self) -> None: | ||
| instance = MagicMock(name="instance", tool_id="classify") | ||
| errors = self._errors({"tool_id": "classify"}, instance=instance) | ||
| assert "tool_id" not in errors | ||
|
|
||
|
|
||
| class TestGetToolListView: | ||
| def _get(self, query: str) -> Any: | ||
| request = APIRequestFactory().get(f"/tool/{query}") | ||
| force_authenticate(request, user=MagicMock(name="user")) | ||
| with patch.object( | ||
| _views_mod.ToolProcessor, "get_tool_list", MagicMock(return_value=[]) | ||
| ) as get_tool_list: | ||
| response = _views_mod.get_tool_list(request) | ||
| return response, get_tool_list | ||
|
|
||
| def test_workflow_id_is_passed_as_a_uuid(self) -> None: | ||
| response, get_tool_list = self._get(f"?workflow_id={WORKFLOW_ID}") | ||
| assert response.status_code == 200 | ||
| assert get_tool_list.call_args.args[1] == uuid.UUID(WORKFLOW_ID) | ||
|
|
||
| def test_missing_workflow_id_lists_without_a_workflow(self) -> None: | ||
| response, get_tool_list = self._get("") | ||
| assert response.status_code == 200 | ||
| assert get_tool_list.call_args.args[1] is None | ||
|
|
||
| def test_malformed_workflow_id_is_a_bad_request(self) -> None: | ||
| response, get_tool_list = self._get("?workflow_id=not-a-uuid") | ||
| assert response.status_code == 400 | ||
| get_tool_list.assert_not_called() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!