898. Run an agent plan with retries and an attempt budget
Why it matters Agent orchestrators (LangGraph, Temporal-style workers, CI pipelines) all need retries, failure propagation and a global budget so one flaky tool cannot burn the whole run.
An agent has a plan of tasks with dependencies. Run it like an orchestrator would.
Write run_plan(tasks, fail_counts, max_retries, max_attempts).
- tasks: dict mapping a task name to the list of task names it depends on. The plan has no cycles, and every dependency is a key.
- fail_counts: dict mapping a task to how many times it fails before it succeeds (missing means 0).
- Repeatedly pick the alphabetically first task that has not been decided yet and whose dependencies all succeeded. Attempt it up to 1 + max_retries times, stopping at the first success. Log each attempt as "name:ok" or "name:fail".
- If a task uses all its attempts without success it is "failed". Every task that depends on a failed or skipped task (directly or through others) becomes "skipped".
- max_attempts is a budget for the whole run. Before each attempt, if max_attempts attempts have already been made, stop: every task not yet "ok", "failed" or "skipped" becomes "not_run".
Return a dict {"log": list of str, "status": dict mapping every task to "ok", "failed", "skipped" or "not_run"}.
def run_plan(tasks, fail_counts, max_retries, max_attempts)Example 1
Input: run_plan({'search': [], 'read': ['search'], 'write': ['read']}, {'read': 1}, 2, 10)
Output: {'log': ['search:ok', 'read:fail', 'read:ok', 'write:ok'], 'status': {'search': 'ok', 'read': 'ok', 'write': 'ok'}}
Example 2
Input: tasks = {'a': [], 'b': ['a'], 'c': ['b'], 'd': []}, fail_counts = {'a': 5}, max_retries = 1, max_attempts = 10
Output: {'log': ['a:fail', 'a:fail', 'd:ok'], 'status': {'a': 'failed', 'b': 'skipped', 'c': 'skipped', 'd': 'ok'}}
Constraints
- Python 3 standard library. Code runs in your browser.
- Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1
Keep a status dict and recompute the ready list after every task.
Hint 2
Write a small propagate step that marks dependents of failed or skipped tasks as skipped until nothing changes.
Hint 3
Check the budget before every single attempt, not once per task.
Common mistakes
- Marking a task failed after
max_retriesattempts instead of 1 +max_retries. - Only skipping direct dependents.
- Leaving the task that ran out of budget as failed instead of
not_run.
Understand the idea
Three rules interact. Retries hide transient failures. A permanent failure must stop everything downstream, so dependents become skipped instead of running on bad inputs. A global attempt budget caps cost: in the third test x needs 3 attempts and the budget is 4, so y gets one failed attempt and then the run stops with y and z not_run.
Shapes and data flow
Dicts in, dict out.