-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_until_done.py
More file actions
30 lines (25 loc) · 1.21 KB
/
Copy pathloop_until_done.py
File metadata and controls
30 lines (25 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Pattern: loop-until-done (Anthropic best practice).
For tasks with an unknown amount of work, keep spawning agents until a stop
condition is met (dry rounds) — not a fixed iteration count. Bounded by budget
so it always terminates. Fights agentic-laziness on open-ended discovery.
flow run examples/patterns/loop_until_done.py --args '{"target":"./src","max_rounds":5}' --budget '{"max_usd":3}'
"""
FOUND = {"type": "object", "required": ["items"], "properties": {"items": {"type": "array", "items": {"type": "string"}}}}
def run(wf, args):
target = args["target"]
max_rounds = (args or {}).get("max_rounds", 5)
seen: set = set()
dry = 0
rnd = 0
while dry < 2 and rnd < max_rounds and wf.has_headroom(min_tokens=2000):
rnd += 1
wf.phase(f"round-{rnd}")
res = wf.agent(f"Find issues in {target} NOT already in this list: {sorted(seen)}. JSON {{items:[...]}}.",
label=f"scan-{rnd}", schema=FOUND, tier="quality", required=False)
fresh = [i for i in (res or {}).get("items", []) if i not in seen]
if not fresh:
dry += 1
continue
dry = 0
seen.update(fresh)
return {"rounds": rnd, "found": sorted(seen)}