DEV Community

Taylor Wang
Taylor Wang

Posted on

A Timeout Is Not a Rollback: Testing Agent Tool Calls

A Timeout Is Not a Rollback: Testing Agent Tool Calls

A wrapper that retries a write when its client deadline fires may send the same logical operation twice. That single line of retry logic hides an assumption nobody wrote down: that a timeout means the effect did not happen. In practice the deadline you set, the transport hop, and the server's commit point are three different clocks, and they rarely agree.

The decision worth making explicit is what your wrapper should do with a timed-out write call. This draft lays out an illustrative test plan for that decision, covering what to observe, how to read the observations, and which configurations make the whole exercise meaningless.

Who this applies to, and what you need first

This plan suits anyone who owns the retry layer between a model and a tool that mutates state. That includes agent frameworks, background job runners, and internal workbenches that expose write actions such as creating tickets, posting messages, or provisioning resources.

Prerequisites worth confirming before you start:

  1. A tool wrapper you control, kept separate from the tool server so you can toggle retries without touching the side effect.
  2. A read path that returns the effect state independently of the write path, ideally strongly consistent.
  3. A way to inject server-side delay, or a proxy that delays the response without cancelling the request.
  4. A per-attempt log with timestamps, client outcome, and any request identifier you send.

If you only have the wrapper and no independent read path, you can still observe counts through a side-effect sink. You cannot, however, distinguish "no effect" from "effect not yet visible", which is the ambiguity this plan exists to remove.

Where the three clocks diverge

A timed-out call has three possible histories, and your wrapper currently treats them as one. Understanding the difference is what turns a vague concern into a testable claim.

  • Deadline before send. The request never left the client. Nothing happened, and a retry is safe.
  • Deadline during transit or processing. The server may complete the work and simply fail to deliver the response back to you.
  • Deadline after commit. The effect landed, the response was lost, and an unprotected retry can duplicate it.

The second and third cases are indistinguishable from the client side, which is exactly why "retry on timeout" is a policy choice rather than a bug fix. Note also that some servers abort work when the connection closes, while others keep going; you cannot assume either behaviour without checking the implementation you actually run.

Four policy shapes and what each one costs

Before writing a test, choose the policy you intend to verify. Mixing them mid-test produces observations you cannot interpret.

  1. Never retry writes. Simplest to reason about, but it converts transient timeouts into user-visible failures and pushes the retry decision to a human.
  2. Retry with an idempotency key. The wrapper generates one key per logical operation, and the server stores the result against it. Cost: server-side key storage, expiry policy, and a conflict response you must handle.
  3. Query status before retrying. The wrapper asks the tool whether the operation already committed, then decides. Cost: a second endpoint, plus a read path that may lag behind the write.
  4. Make the operation convergent. Redefine the call so repeating it produces the same state, for example a set-status call instead of an append-message call. Cost: not every operation can be reshaped this way.

One common implementation mistake in policy 2 is easy to isolate in a fixture: the key is generated inside the retry loop, so each attempt carries a fresh key and deduplication never triggers.

Test

The following is an illustrative plan, not a record of an executed experiment. Adapt identifiers, delays, and thresholds to your own stack, and treat every expected observation as a hypothesis to confirm or refute.

Fixtures

  • One write tool with a single, countable effect, plus a read endpoint that returns the effect by operation identifier.
  • One composite tool that performs two writes in sequence, so you can observe partial completion.
  • A delay control that can hold a response past the client deadline without aborting server-side work.
  • Two wrapper configurations: retry disabled, and retry enabled with a maximum of two attempts.

Steps

  1. Baseline. Call the write tool once with no injected delay, then read back the effect. Expected observation: exactly one effect, and a client success. If this fails, stop; the harness is not trustworthy yet.
  2. Delay past the deadline. Hold the response beyond the client timeout, then read back the effect state. Expected observation: a client-side timeout, and an effect count that may change while the server is still running. Wait for a known terminal outcome in the fixture before recording the final count.
  3. Record which side of commit you landed on. This observation describes one execution. The safety of retries depends on the provider’s contract and whether the caller obeys it. Repeat the step with the delay set just below and just above the commit boundary.
  4. Retry without a key. With retries enabled and no idempotency key, force a timeout and let the wrapper retry. In a fixture configured to commit every delivery independently, expect an effect for each committed attempt. A received request can still be rejected or aborted before commit. Count effects, not log lines.
  5. Retry with a stable key. Move key generation outside the retry loop, then repeat step 4. In the fixture where at least one attempt commits and the receiver honors the same key throughout the replay, expect one effect and the documented replay response.
  6. Retry with status query. Disable keys, enable a status lookup before retry, and repeat. Try two cases. First, let the initial request commit and become visible before lookup. Then have lookup return an accurate empty result while the initial request is still running. A second create in that window can duplicate even with a strongly consistent lookup. An eventual read can extend the uncertainty.
  7. Composite tool partial failure. Confirm the first write and have the receiver drop the response to the second. Inspect both effects at the receiver. The second write may have committed. Replay the composite with stable, distinct keys per sub-operation while the receiver’s deduplication window remains active. Each committed sub-operation should keep its original effect ID. Repeat in a separate fixture without deduplication to expose any duplicate writes.

Verification

Check outcomes against effects, not against wrapper logs. For a single create which the receiver confirms, expect one effect ID. For an unconfirmed operation, use the receiver’s terminal state and test fixture to distinguish zero effects, one effect and an operation still in progress. The sketch below is illustrative pseudocode and has not been executed.

for op in confirmed_creates_with_receiver_dedup:
    ids = read_effects_after_completion(op.id)
    assert len(ids) == 1
    assert receiver_saw_same_key_on_every_attempt(op)

# duplicate-key behaviour
r1 = write(op, key="k-1")
r2 = write(op, key="k-1")
assert final_distinct_effect_count(op) == 1
assert replay_response_matches_receiver_contract(r1, r2)
Enter fullscreen mode Exit fullscreen mode

If the second call produces a second effect, your deduplication is absent or scoped wrongly. If the effect is unchanged but a retry returns an error, compare the response with the provider’s documented replay semantics before treating it as a mapping fault.

Reading the observations

Observation Likely interpretation Next check
Timeout, one effect, client success on retry Either attempt may have committed; the count alone cannot identify it Receiver timestamps, operation key, and effect ID
Timeout, initially no visible effect, retry succeeds The initial read may have been early or stale Observe the receiver’s terminal state and match the effect to its operation ID
Effect count equals attempt count No server-side deduplication Inspect key generation site
One effect, duplicate attempt returns an error Replay might return a documented conflict or an unrelated failure Receiver contract and recorded operation IDs
Read-back reports no effect, later shows one Read may be stale or first request may have committed later Receiver commit order and read visibility

Failure modes that make the test lie

Several configurations produce clean-looking results for the wrong reason, and each one is worth ruling out explicitly.

  • The fixture tests only a known pre-send stop. Add a case that loses the response after commit and one where the first request remains in flight during lookup.
  • The proxy buffers the request, so the client times out while the server still receives a complete call and processes it normally.
  • The idempotency key lives in wrapper memory, which loses deduplication across restarts and across horizontally scaled replicas.
  • The status query reads a replica, returning "not found" for an operation that committed moments earlier, which invites a duplicate.
  • Retryable and non-retryable errors are conflated, so a 429 rate limit and a write timeout follow the same path even though only one of them is ambiguous.
  • Sub-operation keys change on a composite retry. A receiver sees a new key and can create another effect. Keep the identity of each intended write stable across attempts.

When this plan does not apply

Not every tool call justifies this work, and applying it indiscriminately wastes effort. Reads can still be charged, consume quota or return a newer view. Operations that converge naturally, such as setting a field to a fixed value, tolerate repeats and need no key. For a third-party tool that documents at-most-once semantics, exercise its observable contract with disposable data. Finite tests cannot establish that guarantee for every failure and timing. HTTP Semantics, section 9.2.2

Finally, if the tool server's behaviour on disconnect is undocumented and unchanged by configuration, your test measures one implementation detail rather than a contract. Treat that as a finding about missing guarantees, not as a passing test.

What to record before merging the change

The durable output of this exercise is not a green check but a short written policy you can point to during review. Record which tool classes retry, where the key is generated, how long the server retains keys, and what the wrapper does with a duplicate response. Add the observation that decided it, including the delay values you used and the effect counts you saw.

Anyone reviewing the change should be able to answer one question without reading the code: after a timeout, does this system know whether the effect happened? If the answer depends on a read path that lags, say so plainly and set the retry policy accordingly.

This article was created with AI assistance. The fixture is a proposed verification plan.

Top comments (0)