Skip to content

Commit 0460052

Browse files
committed
fix(image-batch): give each image its own agent instead of sharing one
ImageAgentBatchProcessor submits the same Agent object once per image and never resets it, so image N is answered with images 1..N-1 still in context. Memory grows for the whole batch and is re-sent on every later call, and concurrent workers append to one short_memory at the same time. Hand each submission a private copy with empty short-term memory. Copying also closes the race, since no two workers touch the same object. Where the agent cannot be copied -- a held thread lock, an HTTP connection pool -- fall back to the original, matching AgentRearrange._clone_for_task. The fallback returns the agent untouched rather than resetting it: resetting an object another worker is mid-run on would be worse than the sharing it is meant to fix. Closes kyegomez#2046
1 parent cfc4366 commit 0460052

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

swarms/structs/image_batch_processor.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import os
23
import time
34
from concurrent.futures import ThreadPoolExecutor, as_completed
@@ -107,6 +108,14 @@ def _validate_image_path(
107108
)
108109
return path
109110

111+
def _isolate(self, agent: Agent) -> Agent:
112+
try:
113+
isolated = copy.deepcopy(agent)
114+
except Exception:
115+
return agent
116+
isolated.short_memory = isolated.short_memory_init()
117+
return isolated
118+
110119
def _process_single_image(
111120
self,
112121
image_path: Path,
@@ -217,7 +226,10 @@ def run(
217226
for path in validated_paths:
218227
for agent in self.agents:
219228
future = executor.submit(
220-
self._process_single_image, path, tasks, agent
229+
self._process_single_image,
230+
path,
231+
tasks,
232+
self._isolate(agent),
221233
)
222234
future_to_path[future] = (path, agent)
223235

0 commit comments

Comments
 (0)