aws-samples / aws-samples/sample-agent-greenhouse

Replace monkey-patching with dependency injection in ParallelDispatcher

Open
#3 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
21
Forks
5
PR merge metrics
No merged PRs in 30d

Description

## Problem

`src/platform_agent/foundation/parallel_dispatch.py` has a module-level `_execute_task` function that is a no-op echo stub:

```python
async def _execute_task(task: dict[str, Any]) -> Any:
# Default implementation: echo the prompt.
prompt = task.get("prompt", "")
return f"completed: {prompt}"
```

The comment says "Override or monkey-patch this function to integrate with a real sub-agent backend." Monkey-patching a module-level function is fragile, hard to test, and breaks in concurrent scenarios where different dispatchers need different executors.

## Proposed Solution

Use constructor-based dependency injection:

```python
class ParallelDispatcher:
def __init__(self, executor: Callable[[dict[str, Any]], Awaitable[Any]] | None = None):
self._executor = executor or _default_echo_executor

async def _run_task(self, task, semaphore):
# ... use self._executor(task) instead of _execute_task(task)
```

This makes the dispatcher testable with mock executors and allows different dispatcher instances to use different backends simultaneously.

## Acceptance Criteria

- [ ] `ParallelDispatcher.__init__` accepts an optional `executor` callable
- [ ] Default behavior unchanged (echo stub) when no executor provided
- [ ] Module-level `_execute_task` removed or deprecated
- [ ] Existing tests pass with the new interface
- [ ] Add test showing two dispatchers with different executors running concurrently

Contributor guide

Open the contributing guide

Research direction

Read src/platform_agent/foundation/parallel_dispatch.py, especially ParallelDispatcher and its _run_task path, then inspect the existing tests. Verify the constructor accepts an optional async executor, preserves the default echo behavior, and supports two dispatcher instances with different executors running concurrently; run the existing test suite and add the concurrent-dispatcher coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, testing-qa
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.