unrust

Public code review lab

Can a retry loop exceed its attempt budget?

Published

Published by Unrust. Scoring and editorial method

Quick answer

The loop checks the counter before incrementing it. Values 0, 1, 2 and 3 all satisfy attempts <= 3, so the function calls send four times. At a budget of zero it still calls once. Changing <= to < matches this contract. A different contract that defines retries after an initial attempt would need a different implementation.

Read the contract before the patch

maxAttempts is a non-negative integer and is the total number of permitted send calls, including the first call. Stop immediately after a successful call. A budget of zero permits no call.

Predict the result before reading the repair. Use a small counterexample to separate the code’s behavior from the behavior you expected.

Original teaching example
function attemptDelivery(maxAttempts, send) {
  let attempts = 0;
  while (attempts <= maxAttempts) {
    attempts++;
    if (send()) return attempts;
  }
  return attempts;
}

Compare the repair against the same inputs

The loop checks the counter before incrementing it. Values 0, 1, 2 and 3 all satisfy attempts <= 3, so the function calls send four times. At a budget of zero it still calls once. Changing <= to < matches this contract. A different contract that defines retries after an initial attempt would need a different implementation.

Repair under the stated contract
function attemptDelivery(maxAttempts, send) {
  let attempts = 0;
  while (attempts < maxAttempts) {
    attempts++;
    if (send()) return attempts;
  }
  return attempts;
}
Execution-checked examples, 21 October 2026
InputOriginalRepair
Budget 0, always false1 call0 calls
Budget 3, always false4 calls3 calls
Budget 3, first call succeeds1 call1 call

Extend the review

This synchronous teaching example does not implement backoff, cancellation, timeouts, idempotency or production network error handling.

Change one assumption, predict a new outcome, then run the example in your own environment. The public lab repository contains the snippets and their checks. These examples teach review reasoning; they do not measure coding ability.

Interactive reasoning check

Make your prediction

With maxAttempts = 3 and send always returning false, how many calls does the original loop make?
Read the reasoning without answering

The loop checks the counter before incrementing it. Values 0, 1, 2 and 3 all satisfy attempts <= 3, so the function calls send four times. At a budget of zero it still calls once. Changing <= to < matches this contract. A different contract that defines retries after an initial attempt would need a different implementation.

A small practice loop

  1. 01State the input contract in your own words.
  2. 02Predict each boundary result before running the code.
  3. 03Compare the original and repair with exactly the same input.
  4. 04Write one additional case and explain what it establishes.

Common questions

Is this a scored Unrust assessment?

No. This is a fully public, worked teaching example. Its answers are intentionally available and it produces no diagnostic score.

Can I reuse the example in a tutorial?

Yes. The public lab source is MIT licensed. Keep the license, state the input contract and link to the example and its verification date.

Limits of this guide

This synchronous teaching example does not implement backoff, cancellation, timeouts, idempotency or production network error handling.

Sources and scope

Original public teaching material authored for Unrust and execution-checked on 21 October 2026. It is separate from the diagnostic and Daily puzzle banks.

  • while statement

    MDN Web Docs

    The loop condition is evaluated before each iteration.

Check your own starting point

Unrust uses short, timed, unassisted drills to give developers a per-skill snapshot. One session cannot explain the cause of a result, but it can show you what is worth practicing next.

Take the free diagnostic