Run the Goal Loop
The goal loop is the engine of loop engineering: attempt the next task, let a verifier judge it, retry failures, and stop when the goal is met — or stop loudly when the cap trips. Implement the whole outer loop as one pure, testable function.
The problem
Implement run_loop(queue, outcomes, max_iterations). Each iteration the agent attempts the task at the front of the queue, and outcomes[i] is the independent verifier’s verdict for iteration i (if the outcomes list is exhausted, the verdict is a failure). A pass moves the task to done; a fail leaves it at the front to retry. The loop ends when the queue is empty or the iteration cap is hit. Return {"done": […], "remaining": […], "iterations": n, "status": "complete" | "cap"}.
queue=["a","b"], outcomes=[True, False, True], max_iterations=10{'done': ['a', 'b'], 'remaining': [], 'iterations': 3, 'status': 'complete'}queue=["a","b"], outcomes=[False, False, False], max_iterations=3{'done': [], 'remaining': ['a', 'b'], 'iterations': 3, 'status': 'cap'}queue=[], outcomes=[True], max_iterations=5{'done': [], 'remaining': [], 'iterations': 0, 'status': 'complete'}- One verifier verdict is consumed per iteration: verdict for iteration i is
outcomes[i], or a failure once outcomes are exhausted. - A failed task stays at the front of the queue — state untouched, retried next iteration.
- Do not mutate the input queue; return a fresh
remaininglist. statusis"complete"only when the queue emptied; a cap-hit with leftovers is"cap"— even at 0 iterations.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement run_loop(queue, outcomes, max_iterations) — attempt the front task each iteration, let the verifier verdict decide pass/retry, and stop on goal-met or cap-hit with the full result dict.
- Copy the queue first — the caller’s list must survive the run.
- Loop while the queue is non-empty AND iterations < max_iterations.
- The verdict for this iteration is outcomes[iterations] if that index exists, else False — read it before incrementing.
- On a pass, pop the front task into done. On a fail, do nothing — the front task is automatically the retry target.
- Status at the end: "complete" if the queue is empty, otherwise "cap".
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.