Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix: reject nonpositive inspection fanout widths (#281)

Rudy Mizrahi Celeklie97e06335d9d
diff --git a/ifixai/inspections/fanout.py b/ifixai/inspections/fanout.py
index 6d48d27..0766002 100644
--- a/ifixai/inspections/fanout.py
+++ b/ifixai/inspections/fanout.py
@@ -36,10 +36,15 @@ async def bounded_gather(
) -> list[T]:
"""Run independent cells concurrently under a width cap; results in input order.
+ A non-positive width is rejected and never-started cell coroutines are closed.
+
`gather` schedules every coroutine as a task up front, so nothing is left un-awaited on
failure; the semaphore only staggers when each body runs. A CANCELLED sweep is the
exception, and `close_unstarted` handles it.
"""
+ if limit <= 0:
+ close_unstarted(calls)
+ raise ValueError("limit must be positive")
semaphore = asyncio.Semaphore(limit)
try:
outcomes = await asyncio.gather(
diff --git a/ifixai/tests/test_fanout_positive_width.py b/ifixai/tests/test_fanout_positive_width.py
new file mode 100644
index 0000000..a74ec4c
--- /dev/null
+++ b/ifixai/tests/test_fanout_positive_width.py
@@ -0,0 +1,45 @@
+"""Invalid fan-out widths fail before scheduling and release owned coroutines."""
+
+import asyncio
+import inspect
+
+import pytest
+
+from ifixai.inspections.fanout import bounded_gather
+
+
[email protected]("limit", [0, -1])
+async def test_nonpositive_width_rejects_and_closes_unstarted_cells(limit):
+ started = []
+
+ async def cell():
+ started.append(True)
+ return "result"
+
+ calls = [cell(), cell()]
+ try:
+ with pytest.raises(ValueError, match="limit must be positive"):
+ await asyncio.wait_for(bounded_gather(calls, limit), timeout=0.1)
+ assert started == []
+ assert all(inspect.getcoroutinestate(call) == inspect.CORO_CLOSED for call in calls)
+ finally:
+ for call in calls:
+ call.close()
+
+
+async def test_one_slot_retains_input_order_and_finishes_all_cells():
+ active = 0
+ peak = 0
+
+ async def cell(value):
+ nonlocal active, peak
+ active += 1
+ peak = max(peak, active)
+ await asyncio.sleep(0)
+ active -= 1
+ return value
+
+ assert await bounded_gather([cell(3), cell(1), cell(2)], 1) == [3, 1, 2]
+ assert peak == 1