Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Failure windows and recovery invariants
- Recovery correctness is determined by the business facts before and after the failure. The test must cut off the window between "submission, receipt, and status advancement" to prove that replay will not break idempotency, permissions, and budget constraints.
How does the mechanism work?
Step-by-step explanation
- What is the difference between repeat requests and repeat effects?
Recovery often relies on resending requests, so distinguish network attempts from actual business effects.
- Why is fixed model response still valuable for testing?
Fault-injection experiments require repeatable control of variables, so explain the value and limits of test doubles.
- Does stopping at unknown when it cannot be verified count as a test failure?
When the expected receipt is missing, specify exactly what the test requires.
Debugging · Diagnose failures
Diagnose failures · Failure windows and recovery invariants
Objectives of this level: Run three probes and ledger diagnosis;write your binding-negative check.
Visual guide · Commit can precede knowledge
The local fault commits one merchant application before losing its response. A missing caller receipt does not establish zero effects. Select the figure to open it at full size.
Three probes locate three gates
After before.json and before recover-main, save replay_probe.py and run python3 replay_probe.py. Original key/payload returns the original return_id; changed key returns task_already_has_return; changed parameters return idempotency_conflict. Checker exit 0 confirms expected rejections, not successful denied actions. Effects stay 1, local status remains unknown; recover next.
import json
from pathlib import Path
from common import LabError,digest
from harness import client,merchant_process
from store import Store
work=Path('fault-work')
with merchant_process(work) as service:
s=Store(work/'agent.sqlite');t=s.get('main-return');c=client(service.url)
payload={k:t[k] for k in ['proposal','proposal_hash','operation_key','fence']}
payload.update(task_id=t['id'],approval_id=t['approval']['approval_id'])
original=c.request('POST','/v1/returns',payload)
errors={}
for name,p in [('changed_key',{**payload,'operation_key':payload['operation_key']+':retry'}),('changed_parameters',{**payload,'proposal':{**payload['proposal'],'reason':'changed reason'}})]:
if name=='changed_parameters':p['proposal_hash']=digest(p['proposal'])
try:c.request('POST','/v1/returns',p);raise AssertionError('must reject')
except LabError as e:errors[name]=e.code
n=sum(r['task_id']==t['id'] for r in client(service.url,'admin-token').request('GET','/v1/audit')['returns'])
result={'same_payload_return_id':original['return_id'],'errors':errors,'merchant_returns':n,'local_status':s.get(t['id'])['status']}
assert errors=={'changed_key':'task_already_has_return','changed_parameters':'idempotency_conflict'} and n==1
(work/'duplicate-probes.json').write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(result,sort_keys=True));s.close()
Read both ledgers
After recovery run the diagnostic:local completed/key/receipt, merchant receipt key/request_hash, main-return returns count 1. Direct SQLite reading is isolated diagnosis, not production authorization. Do not repair by modifying ledgers.
python3 - <<'PY'
import json,sqlite3
from pathlib import Path
out={}
for name,sql in [('agent','SELECT id,status,operation_key,receipt_json FROM tasks WHERE id=?'),('merchant','SELECT operation_key,task_id,request_hash FROM receipts WHERE task_id=?')]:
with sqlite3.connect('fault-work/'+name+'.sqlite') as db:out[name]=db.execute(sql,('main-return',)).fetchall()
with sqlite3.connect('fault-work/merchant.sqlite') as db:out['returns']=db.execute('SELECT task_id,count(*) FROM returns WHERE task_id=? GROUP BY task_id',('main-return',)).fetchall()
Path('fault-work/ledger.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out,sort_keys=True))
PY
Locate key uniqueness, payload conflict and per-task existing-return gates in the merchant transaction. One task means one application here; partial returns need a new identity contract. HTTP attempts, Agent events and effects have different denominators.
Implement missed defect→repair→rerun
Copy recovery as a labeled checker fixture, alter receipt.quantity or proposal_hash, and show a status-only false accept. Write your binding checker so the fixture exits 1 and real record exits 0. Do not edit original ledgers or call the fixture a production incident. Preserve red, your diff, green and separate safety/deadline conclusions.
Input contract: task.receipt in recover-main.json must bind task.id, task.operation_key, task.proposal_hash, task.order_id and task.proposal.quantity. merchant_audit.returns must contain exactly one matching task_id, with id=receipt.return_id and matching quantity/proposal_hash. Quantity must be an actual integer, not Boolean. Save this false-accepting starter as receipt_check_before.py and pass the report path. It still exits 0 after quantity is altered, which is the failed acceptance to repair. Write your corrected checker and preserve its diff.
import json,sys
from pathlib import Path
r=json.loads(Path(sys.argv[1]).read_text())
accepted=r['task']['status']=='completed'
print(json.dumps({'accepted':accepted,'checker':'status_only'}))
sys.exit(0 if accepted else 1)
code example
Evidence and interview explanation
Run three probes and ledger diagnosis;write your binding-negative check.
Use the inputs,code,exits and ledgers above;submit your own diff,not copied green logs,and name unverified capabilities.
Acceptance task for this level
Run three probes and ledger diagnosis;write your binding-negative check.
Reading this browser’s record…
Hide the answer and check your understanding
Three HTTP probes mean three effects?
Expand reference derivation
No:one replay/two denials/one effect;direct probes are not Agent events.
Further explanations and practice
When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.
All linked explanations and exercises (1 )
Sources and verification scope
The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.