Skip to content

Self-improving AI Employees for everyone

refactor(tool): inline task_manager body into plugin + retire ddgs test shim (Wave 11.I, O)

trohitgb726d9fe2df0
diff --git a/server/nodes/tool/task_manager.py b/server/nodes/tool/task_manager.py
index 0763263e..6f45e81c 100644
--- a/server/nodes/tool/task_manager.py
+++ b/server/nodes/tool/task_manager.py
@@ -1,4 +1,13 @@
-"""Task Manager Tool — Wave 11.C migration."""
+"""Task Manager Tool — Wave 11.C migration; body inlined Wave 11.I (M.O).
+
+Inspects the in-memory delegation registry that lives on
+``services.handlers.tools`` (``_delegated_tasks`` for in-flight asyncio
+tasks, ``_delegation_results`` for completed-but-still-cached results,
+plus ``get_delegated_task_status`` which adds a DB-fallback layer for
+older runs). The plugin doesn't own that state — the delegation
+lifecycle in ``tools.py`` does — so we read through to it rather than
+duplicating registry plumbing here.
+"""
from __future__ import annotations
@@ -6,8 +15,11 @@ from typing import Any, Dict, Literal, Optional
from pydantic import BaseModel, ConfigDict, Field
+from core.logging import get_logger
from services.plugin import NodeContext, Operation, TaskQueue, ToolNode
+logger = get_logger(__name__)
+
class TaskManagerParams(BaseModel):
action: Literal["create", "list", "complete", "delete", "update"] = "create"
@@ -49,7 +61,137 @@ class TaskManagerNode(ToolNode):
@Operation("manage")
async def manage(self, ctx: NodeContext, params: TaskManagerParams) -> Any:
- from services.handlers.tools import _execute_task_manager
return await _execute_task_manager(
params.model_dump(), {"node_id": ctx.node_id, "workspace_dir": ctx.workspace_dir or ""},
)
+
+
+async def _execute_task_manager(
+ tool_args: Dict[str, Any],
+ config: Dict[str, Any],
+) -> Dict[str, Any]:
+ """Execute task manager operations.
+
+ Dual-purpose: works as AI tool (LLM fills args) or workflow node
+ (uses params).
+
+ Operations:
+ - ``list_tasks``: list all active and completed delegated tasks
+ - ``get_task``: detailed status for one task
+ - ``mark_done``: drop a task from active tracking
+ """
+ # Read-through to the delegation registry owned by tools.py. Keeping
+ # the singletons there is intentional -- the entire delegation
+ # lifecycle (spawn / cleanup / refcount / DB persistence) lives in
+ # tools.py and is genuine cross-cutting framework state.
+ from services.handlers.tools import (
+ _delegated_tasks,
+ _delegation_results,
+ get_delegated_task_status,
+ )
+
+ params = config.get("parameters", {})
+ operation = tool_args.get("operation") or params.get("operation", "list_tasks")
+ task_id = tool_args.get("task_id") or params.get("task_id")
+ status_filter = tool_args.get("status_filter") or params.get("status_filter")
+ database = config.get("database")
+
+ logger.debug(
+ f"[TaskManager] Operation: {operation}, task_id: {task_id}, filter: {status_filter}"
+ )
+
+ if operation == "list_tasks":
+ tasks = []
+
+ # Active tasks from asyncio.Task tracking.
+ for tid, task in _delegated_tasks.items():
+ if task.done():
+ try:
+ if task.cancelled():
+ status = "cancelled"
+ elif task.exception():
+ status = "error"
+ else:
+ status = "completed"
+ except Exception:
+ status = "completed"
+ else:
+ status = "running"
+
+ tasks.append({"task_id": tid, "status": status, "active": True})
+
+ # Completed tasks from in-memory cache.
+ for tid, result in _delegation_results.items():
+ if tid not in [t["task_id"] for t in tasks]:
+ tasks.append({
+ "task_id": tid,
+ "status": result.get("status", "completed"),
+ "agent_name": result.get("agent_name"),
+ "result_summary": str(result.get("result", ""))[:200],
+ "active": False,
+ })
+
+ if status_filter:
+ tasks = [t for t in tasks if t.get("status") == status_filter]
+
+ return {
+ "success": True,
+ "operation": "list_tasks",
+ "tasks": tasks,
+ "count": len(tasks),
+ "running": sum(1 for t in tasks if t.get("status") == "running"),
+ "completed": sum(1 for t in tasks if t.get("status") == "completed"),
+ "errors": sum(1 for t in tasks if t.get("status") == "error"),
+ }
+
+ if operation == "get_task":
+ if not task_id:
+ return {"success": False, "error": "task_id is required for get_task operation"}
+
+ # 3-layer lookup (live tasks -> memory cache -> DB).
+ result = await get_delegated_task_status(task_ids=[task_id], database=database)
+ tasks = result.get("tasks", [])
+
+ if not tasks:
+ return {
+ "success": False,
+ "error": f"Task {task_id} not found",
+ "task_id": task_id,
+ }
+
+ task_info = tasks[0]
+ return {
+ "success": True,
+ "operation": "get_task",
+ "task_id": task_id,
+ "status": task_info.get("status"),
+ "agent_name": task_info.get("agent_name"),
+ "result": task_info.get("result"),
+ "error": task_info.get("error"),
+ }
+
+ if operation == "mark_done":
+ if not task_id:
+ return {"success": False, "error": "task_id is required for mark_done operation"}
+
+ removed = False
+ if task_id in _delegated_tasks:
+ del _delegated_tasks[task_id]
+ removed = True
+ if task_id in _delegation_results:
+ del _delegation_results[task_id]
+ removed = True
+
+ return {
+ "success": True,
+ "operation": "mark_done",
+ "task_id": task_id,
+ "removed": removed,
+ "message": (
+ f"Task {task_id} marked as done and removed from tracking"
+ if removed
+ else f"Task {task_id} was not in active tracking"
+ ),
+ }
+
+ return {"success": False, "error": f"Unknown operation: {operation}"}
diff --git a/server/services/handlers/tools.py b/server/services/handlers/tools.py
index aad0e7b5..c9d3b7b4 100644
--- a/server/services/handlers/tools.py
+++ b/server/services/handlers/tools.py
@@ -201,10 +201,12 @@ async def _dispatch_tool(tool_name: str, tool_args: Dict[str, Any],
from nodes.android._base import execute_android_service_tool
return await execute_android_service_tool(tool_args, config)
- # taskManager: dispatcher shares state with the agent-delegation
+ # taskManager: dispatcher reads through to the agent-delegation
# tracking dicts (_delegated_tasks / _delegation_results) defined
- # below in this module, so the operation matrix stays here.
+ # below in this module. The operation matrix moved to the plugin
+ # (Wave 11.I, milestone O).
if node_type == 'taskManager':
+ from nodes.tool.task_manager import _execute_task_manager
return await _execute_task_manager(tool_args, config)
# proxyConfig: 10-operation matrix lives on the plugin
@@ -242,34 +244,11 @@ async def _dispatch_tool(tool_name: str, tool_args: Dict[str, Any],
# _execute_duckduckgo_search: Wave 11.C moved logic to
-# nodes/search/duckduckgo_search.py DuckDuckGoSearchNode.search(), but
-# some contract tests patch sys.modules['ddgs'] and call the flat
-# function directly. The shim below preserves the old (args, config) ->
-# dict signature so those tests stay plumbing-only.
-async def _execute_duckduckgo_search(
- args: Dict[str, Any],
- config: Dict[str, Any] = None,
-) -> Dict[str, Any]:
- """Back-compat wrapper around the plugin search path."""
- config = config or {}
- query = str(args.get("query", "")).strip()
- if not query:
- return {"error": "No search query provided"}
- max_results = int(config.get("max_results", args.get("max_results", 5)))
- provider = config.get("provider", "duckduckgo")
- from ddgs import DDGS
- raw = list(DDGS().text(query, max_results=max_results))
- results = [
- {
- "title": item.get("title", ""),
- "snippet": item.get("body", ""),
- "url": item.get("href", ""),
- }
- for item in raw
- ]
- return {"query": query, "provider": provider, "results": results}
-
-
+# nodes/search/duckduckgo_search.py DuckDuckGoSearchNode.search(). Wave
+# 11.I milestone O moved the flat (args, config) -> dict shim used by
+# contract tests to tests/nodes/_compat.py -- production code never
+# called it.
+#
# Wave 11.D.9: _execute_whatsapp_{send,db} deleted. WhatsApp tool execution
# now routes through the plugin fast-path (nodes/whatsapp/*.py).
@@ -779,160 +758,12 @@ async def _execute_check_delegated_tasks(args: Dict[str, Any],
}
-async def _execute_task_manager(
- tool_args: Dict[str, Any],
- config: Dict[str, Any]
-) -> Dict[str, Any]:
- """Execute task manager operations.
-
- Dual-purpose: works as AI tool (LLM fills args) or workflow node (uses params).
-
- Operations:
- - list_tasks: List all active and completed delegated tasks
- - get_task: Get status details for a specific task
- - mark_done: Remove a task from active tracking
-
- Args:
- tool_args: Arguments from LLM tool call (operation, task_id, status_filter)
- config: Tool configuration with node parameters
-
- Returns:
- Dict with operation results
- """
- # Merge tool_args with node parameters (tool_args takes precedence)
- params = config.get('parameters', {})
- operation = tool_args.get('operation') or params.get('operation', 'list_tasks')
- task_id = tool_args.get('task_id') or params.get('task_id')
- status_filter = tool_args.get('status_filter') or params.get('status_filter')
- database = config.get('database')
-
- logger.debug(f"[TaskManager] Operation: {operation}, task_id: {task_id}, filter: {status_filter}")
-
- if operation == 'list_tasks':
- # Collect all tasks from _delegated_tasks and _delegation_results
- tasks = []
-
- # Active tasks from asyncio.Task tracking
- for tid, task in _delegated_tasks.items():
- if task.done():
- try:
- if task.cancelled():
- status = 'cancelled'
- elif task.exception():
- status = 'error'
- else:
- status = 'completed'
- except Exception:
- status = 'completed'
- else:
- status = 'running'
-
- tasks.append({
- 'task_id': tid,
- 'status': status,
- 'active': True
- })
-
- # Completed tasks from in-memory cache
- for tid, result in _delegation_results.items():
- if tid not in [t['task_id'] for t in tasks]:
- tasks.append({
- 'task_id': tid,
- 'status': result.get('status', 'completed'),
- 'agent_name': result.get('agent_name'),
- 'result_summary': str(result.get('result', ''))[:200],
- 'active': False
- })
-
- # Apply status filter
- if status_filter:
- tasks = [t for t in tasks if t.get('status') == status_filter]
-
- return {
- 'success': True,
- 'operation': 'list_tasks',
- 'tasks': tasks,
- 'count': len(tasks),
- 'running': sum(1 for t in tasks if t.get('status') == 'running'),
- 'completed': sum(1 for t in tasks if t.get('status') == 'completed'),
- 'errors': sum(1 for t in tasks if t.get('status') == 'error')
- }
-
- elif operation == 'get_task':
- if not task_id:
- return {'success': False, 'error': 'task_id is required for get_task operation'}
-
- # Use existing get_delegated_task_status for detailed lookup
- result = await get_delegated_task_status(task_ids=[task_id], database=database)
- tasks = result.get('tasks', [])
-
- if not tasks:
- return {
- 'success': False,
- 'error': f'Task {task_id} not found',
- 'task_id': task_id
- }
-
- task_info = tasks[0]
- return {
- 'success': True,
- 'operation': 'get_task',
- 'task_id': task_id,
- 'status': task_info.get('status'),
- 'agent_name': task_info.get('agent_name'),
- 'result': task_info.get('result'),
- 'error': task_info.get('error')
- }
-
- elif operation == 'mark_done':
- if not task_id:
- return {'success': False, 'error': 'task_id is required for mark_done operation'}
-
- # Remove from active tracking
- removed = False
- if task_id in _delegated_tasks:
- del _delegated_tasks[task_id]
- removed = True
- if task_id in _delegation_results:
- del _delegation_results[task_id]
- removed = True
-
- return {
- 'success': True,
- 'operation': 'mark_done',
- 'task_id': task_id,
- 'removed': removed,
- 'message': f'Task {task_id} marked as done and removed from tracking' if removed else f'Task {task_id} was not in active tracking'
- }
-
- return {'success': False, 'error': f'Unknown operation: {operation}'}
-
-
-async def handle_task_manager(
- node_id: str,
- node_type: str,
- parameters: Dict[str, Any],
- context: Dict[str, Any]
-) -> Dict[str, Any]:
- """Handle taskManager as workflow node.
-
- Wrapper for _execute_task_manager that conforms to the standard
- workflow node handler signature.
-
- Args:
- node_id: The node ID
- node_type: Should be 'taskManager'
- parameters: Node parameters (operation, task_id, status_filter)
- context: Execution context
-
- Returns:
- Task manager operation results
- """
- config = {
- 'parameters': parameters,
- 'database': context.get('database')
- }
- return await _execute_task_manager({}, config)
+# _execute_task_manager + handle_task_manager: Wave 11.I milestone O moved
+# the operation matrix to nodes/tool/task_manager.py. The plugin reads
+# through to the delegation registry (``_delegated_tasks`` /
+# ``_delegation_results`` / ``get_delegated_task_status``) which still
+# lives in this module -- delegation lifecycle is genuine cross-cutting
+# framework state. ``handle_task_manager`` had no callers and was deleted.
# =============================================================================
diff --git a/server/tests/nodes/_compat.py b/server/tests/nodes/_compat.py
index 15bfe77b..4638107d 100644
--- a/server/tests/nodes/_compat.py
+++ b/server/tests/nodes/_compat.py
@@ -91,33 +91,33 @@ async def _execute_current_time(
async def _execute_duckduckgo_search(
args: Dict[str, Any],
- node_params: Dict[str, Any] = None,
+ config: Dict[str, Any] = None,
) -> Dict[str, Any]:
- """Shim for deleted services.handlers.tools._execute_duckduckgo_search."""
- try:
- # Plugin layout under scaling branch: nodes/search/duckduckgo_search.py
- from nodes.search.duckduckgo_search import (
- DuckDuckGoSearchNode,
- DuckDuckGoSearchParams,
- )
- except ImportError:
- return {"error": "duckduckgo search plugin missing"}
+ """Flat (args, config) -> dict shim for the deleted
+ ``services.handlers.tools._execute_duckduckgo_search``.
- q = str(args.get("query", "")).strip()
- if not q:
+ Wave 11.I milestone O moved this here from production code -- it
+ only ever served contract tests that patch ``sys.modules['ddgs']``
+ and assert the flat output shape. Production callers go through
+ :class:`nodes.search.duckduckgo_search.DuckDuckGoSearchNode`.
+ """
+ config = config or {}
+ query = str(args.get("query", "")).strip()
+ if not query:
return {"error": "No search query provided"}
-
- params = DuckDuckGoSearchParams(
- query=q,
- max_results=int(args.get("max_results", args.get("maxResults", 5))),
- )
-
- node = DuckDuckGoSearchNode()
- try:
- out = await node.search(_DummyContext(), params)
- except Exception as exc:
- return {"error": str(exc)}
- return out.model_dump() if hasattr(out, "model_dump") else dict(out)
+ max_results = int(config.get("max_results", args.get("max_results", 5)))
+ provider = config.get("provider", "duckduckgo")
+ from ddgs import DDGS
+ raw = list(DDGS().text(query, max_results=max_results))
+ results = [
+ {
+ "title": item.get("title", ""),
+ "snippet": item.get("body", ""),
+ "url": item.get("href", ""),
+ }
+ for item in raw
+ ]
+ return {"query": query, "provider": provider, "results": results}
async def handle_write_todos(
diff --git a/server/tests/nodes/test_ai_tools.py b/server/tests/nodes/test_ai_tools.py
index 04ce4f17..aed616a6 100644
--- a/server/tests/nodes/test_ai_tools.py
+++ b/server/tests/nodes/test_ai_tools.py
@@ -180,8 +180,9 @@ class TestDuckDuckGoSearch:
fake_module = MagicMock()
fake_module.DDGS = fake_ddgs_class
+ from tests.nodes._compat import _execute_duckduckgo_search as _flat_search
with patch.dict("sys.modules", {"ddgs": fake_module}):
- result = await tools_mod._execute_duckduckgo_search(
+ result = await _flat_search(
{"query": "python"},
{"provider": "duckduckgo", "max_results": 2},
)
@@ -201,7 +202,7 @@ class TestDuckDuckGoSearch:
assert result == {"error": "No search query provided"}
async def test_missing_keys_in_ddgs_output_default_to_empty_string(self):
- from services.handlers import tools as tools_mod
+ from tests.nodes._compat import _execute_duckduckgo_search as _flat_search
fake_ddgs_instance = MagicMock()
fake_ddgs_instance.text.return_value = [{"title": "only-title"}]
@@ -209,15 +210,12 @@ class TestDuckDuckGoSearch:
fake_module.DDGS = MagicMock(return_value=fake_ddgs_instance)
with patch.dict("sys.modules", {"ddgs": fake_module}):
- result = await tools_mod._execute_duckduckgo_search(
- {"query": "q"},
- {"max_results": 5},
- )
+ result = await _flat_search({"query": "q"}, {"max_results": 5})
assert result["results"] == [{"title": "only-title", "snippet": "", "url": ""}]
async def test_max_results_defaults_to_5_when_missing(self):
- from services.handlers import tools as tools_mod
+ from tests.nodes._compat import _execute_duckduckgo_search as _flat_search
fake_ddgs_instance = MagicMock()
fake_ddgs_instance.text.return_value = []
@@ -225,7 +223,7 @@ class TestDuckDuckGoSearch:
fake_module.DDGS = MagicMock(return_value=fake_ddgs_instance)
with patch.dict("sys.modules", {"ddgs": fake_module}):
- await tools_mod._execute_duckduckgo_search({"query": "q"}, {})
+ await _flat_search({"query": "q"}, {})
fake_ddgs_instance.text.assert_called_once_with("q", max_results=5)
@@ -269,7 +267,8 @@ class TestTaskManager:
"result": "x" * 500, # will be truncated to 200 chars
}
- result = await tools_mod._execute_task_manager(
+ from nodes.tool.task_manager import _execute_task_manager
+ result = await _execute_task_manager(
{"operation": "list_tasks"}, {"parameters": {}}
)
@@ -294,7 +293,8 @@ class TestTaskManager:
tools_mod._delegated_tasks["r1"] = running
tools_mod._delegation_results["c1"] = {"status": "completed"}
- result = await tools_mod._execute_task_manager(
+ from nodes.tool.task_manager import _execute_task_manager
+ result = await _execute_task_manager(
{"operation": "list_tasks", "status_filter": "running"},
{"parameters": {}},
)
@@ -305,7 +305,8 @@ class TestTaskManager:
async def test_get_task_without_id_errors(self, _reset_registry):
tools_mod = _reset_registry
- result = await tools_mod._execute_task_manager(
+ from nodes.tool.task_manager import _execute_task_manager
+ result = await _execute_task_manager(
{"operation": "get_task"}, {"parameters": {}}
)
assert result["success"] is False
@@ -315,7 +316,8 @@ class TestTaskManager:
tools_mod = _reset_registry
tools_mod._delegation_results["gone"] = {"status": "completed"}
- result = await tools_mod._execute_task_manager(
+ from nodes.tool.task_manager import _execute_task_manager
+ result = await _execute_task_manager(
{"operation": "mark_done", "task_id": "gone"},
{"parameters": {}},
)
@@ -327,7 +329,8 @@ class TestTaskManager:
async def test_mark_done_untracked_id_returns_removed_false(self, _reset_registry):
tools_mod = _reset_registry
- result = await tools_mod._execute_task_manager(
+ from nodes.tool.task_manager import _execute_task_manager
+ result = await _execute_task_manager(
{"operation": "mark_done", "task_id": "never-seen"},
{"parameters": {}},
)
@@ -339,7 +342,8 @@ class TestTaskManager:
async def test_unknown_operation_returns_failure_envelope(self, _reset_registry):
tools_mod = _reset_registry
- result = await tools_mod._execute_task_manager(
+ from nodes.tool.task_manager import _execute_task_manager
+ result = await _execute_task_manager(
{"operation": "self_destruct"}, {"parameters": {}}
)
assert result["success"] is False