Durable and Asynchronous Agent Work › Reconciliation and compensation
Language AI HandbookAll chapters There's a residual failure mode that no in-process protocol fixes: the process crashed after the side effect happened, but before the idem row was written (the at-least-once gap). For a payment API that honors idempotency keys upstream, you solve it by passing the key to the API, and a subsequent read of "did charge ch_… for order ord_42 exist?" tells you the truth. For an email server without such a feature, the honest tools are:
- Reconciliation compares external state with the local log. If the outcome is unknown, check the upstream operation using its identifier. Retry only when the upstream contract or verified state permits it.
- Compensation records a separate corrective operation when a prior effect cannot simply be undone. Give the correction its own authorization and audit record.
The reconciler checks whether a local result event and a cache entry exist and whether the result event carries the expected key. It does not compare the stored output values, and it cannot verify the upstream effect. Reconcile the actual external state before resolving a missing receipt.
PythonIn [35]Expand
def reconcile(task_id: str):
events = read_events(task_id)
calls = {
e["payload"]["call_id"]: e["payload"]
for e in events
if e["kind"] == "tool_called"
}
results = {
e["payload"]["call_id"]: e["payload"]
for e in events
if e["kind"] == "tool_result"
}
issues = []
with DB as con:
for call_id, call in calls.items():
key = logical_key(
task_id, call["step"], call["tool"], call["input"]
)
cached = (
con.execute("SELECT 1 FROM idem WHERE key=?", (key,)).fetchone()
is not None
)
result = results.get(call_id)
if result is None or result["key"] != key or not cached:
issues.append(
{
"call_id": call_id,
"cached": cached,
"recorded": result is not None,
}
)
return {"task_id": task_id, "issues": issues, "checked": len(calls)}ConsoleOut [36]Expand
incomplete local trace: [{'call_id': 's0', 'cached': False, 'recorded': False}]
recovered report trace: []The first trace has a call with neither a cached result nor a recorded completion. The recovered report has all three results. Inspecting the local trace identifies the unresolved call. An external query is still needed to determine what happened outside the worker.
Reconciliation reports which recorded intent still lacks an outcome. Use that identifier to ask the upstream system before deciding whether a retry or corrective operation is appropriate.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!