bugger.
All challenges/Pythonmediumai-agent-idempotency-v1
THE INVESTIGATION

The agent that performs an action twice

The tool commits a credit, loses its acknowledgement, and the agent’s retry commits the same action again.

Applied AIAgentsTool callsRetriesIdempotency

What’s going wrong

Original Applied AI lab. This is a deterministic simulated tool replay, not a live agent, payment system or historical upstream defect.

An assistant calls a credit tool. The tool commits the action, but its response times out. The orchestrator retries with the same operationId and the user receives two credits. Repair replay so a lost acknowledgement never turns one logical operation into two committed effects. Preserve independent operations and reject reuse of an operation ID with a different payload.

Read {"requests":[{"operationId":"op-1","recipient":"alice","amount":10,"delivery":"timeout_after"}, ...]}. The array contains 0 to 128 chronological wire attempts. operationId and recipient are nonempty ASCII strings up to 64 characters; amount is an integer from 0 to 10000. delivery is "ok", "timeout_before" or "timeout_after". All inputs satisfy these bounds. Each invocation starts with an empty committed store.

A timeout_before attempt never reaches the tool: return {"status":"retry"} with no validation, lookup or effect. Otherwise look up operationId in completed operations. A matching recipient and amount reuses its receipt without another effect. A different recipient or amount returns {"status":"conflict"} without changing anything, regardless of delivery. A new operation commits one effect and records its payload and receipt. The receipt is "r-1", "r-2", ... in effect commit order. For a new or matching operation, timeout_after loses the reply after tool processing and returns {"status":"retry"}; ok returns {"status":"ok","receipt":"r-1"}. A retry must retain the original operationId.

Print {"effects":[{"operationId":"op-1","recipient":"alice","amount":10,"receipt":"r-1"}],"results":[{"status":"retry"},{"status":"ok","receipt":"r-1"}]}. effects records every actual committed effect in order, including accidental duplicates; results has one entry per input attempt. Fix the tool’s commit bookkeeping rather than hiding duplicate effects from the output. Use only the Python standard library.

Expected behavior

One operation ID and payload produce one committed effect and a stable receipt across retries; distinct IDs remain independent and mismatched reuse is rejected.

Reproduction

Execution evidence

Simulated system · deterministic replay

A constructed tool replay of the first visible reproduction. Delivery failures are explicit events, not real network calls.

  1. 01Agent
    Attempt op-1: credit alice by 10; retain this operation ID for retries.
  2. 02Tool commit
    Effect r-1 commits. delivery=timeout_after drops the acknowledgement.
  3. 03Orchestrator
    Caller sees retry, then repeats op-1 with the identical payload.
  4. 04Starter ledger
    Effects r-1 and r-2 both credit alice. The retry should have recovered receipt r-1.
Explain your repair before submitting

Which observation contradicts the intended behavior? Where does the code break that contract? Explain why your change fixes the cause and which new regression case would detect it.

This evidence describes the starter. Use Run to observe your current code; Submit checks additional regressions. No live model calls are made.

01 A committed tool action survives a lost acknowledgement
{"requests":[{"operationId":"op-1","recipient":"alice","amount":10,"delivery":"timeout_after"},{"operationId":"op-1","recipient":"alice","amount":10,"delivery":"ok"}]}
{"effects":[{"operationId":"op-1","recipient":"alice","amount":10,"receipt":"r-1"}],"results":[{"status":"retry"},{"status":"ok","receipt":"r-1"}]}
02 Distinct intents with identical payloads are independent
{"requests":[{"operationId":"op-1","recipient":"alice","amount":10,"delivery":"ok"},{"operationId":"op-2","recipient":"alice","amount":10,"delivery":"ok"}]}
{"effects":[{"operationId":"op-1","recipient":"alice","amount":10,"receipt":"r-1"},{"operationId":"op-2","recipient":"alice","amount":10,"receipt":"r-2"}],"results":[{"status":"ok","receipt":"r-1"},{"status":"ok","receipt":"r-2"}]}
03 A timeout before delivery leaves no committed effect
{"requests":[{"operationId":"op-1","recipient":"alice","amount":10,"delivery":"timeout_before"},{"operationId":"op-1","recipient":"alice","amount":10,"delivery":"ok"}]}
{"effects":[{"operationId":"op-1","recipient":"alice","amount":10,"receipt":"r-1"}],"results":[{"status":"retry"},{"status":"ok","receipt":"r-1"}]}

Before you begin

  • Python dictionaries and chronological state transitions
  • A timeout can happen before or after a tool commits
  • An operation ID identifies intent independently of a wire attempt

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.