Square’s payments-infrastructure team owns the job runner that moves money into merchant balances, and its queue delivers every job message at least once. A worker timeout, a consumer rebalance, or a broker failover can hand the same job_id to a worker twice, sometimes while the first copy is still being applied. In this 60-minute AI-enabled round you harden that runner in three phases. The rule never changes: a job may arrive any number of times, but it must move money exactly once.
Phase 1 (unaided): the starter JobHandler below passes a smoke test that delivers a job twice in a row. In staging, the payout log still shows payout-7731 crediting merchant-42 twice, about 40 ms apart, with both calls returning 'applied'. The second delivery arrived while a slow ledger write for the first was still running. Repair the handler so it honors the contract in its comment, with no AI assistant. The one-shot on_credit hook lets a test run code at the start of credit().
Phase 2 (steer the AI): the single-process handler grows into IdempotentConsumer. It runs as several replicas against a provided toy database, store.py, which offers transactions, a unique-key insert, an injected clock, and fault hooks. Pick the one prompt out of three whose contract is exact. Phase 3 (audit): the assistant’s draft passes the sequential demo. Find its three flaws, patch them in place, and prove the patch with a test.
Phase 2 Target Contract: (1) A duplicate routed to another replica, or to a worker restarted after a deploy, is recognized. (2) A redelivery up to REDELIVERY_HORIZON_S (24 h) after a job completes is recognized, while older dedup rows may be evicted to bound storage. (3) A worker that dies at any instant, including right after a commit, never causes a second credit on redelivery. (4) A duplicate that arrives while the first copy is in flight never credits. (5) A job whose credit fails before it commits stays retryable.
Phase 1 Starter (payout_handler.py):
01 # payout_handler.py: Phase 1 starter (single process, no replicas yet)
02
03
04 class Ledger:
05 # Toy effect target: cents credited per merchant account.
06 def __init__(self):
07 self.balances = {}
08 self.on_credit = None # one-shot test hook, called at the start of credit()
09
10 def credit(self, account, cents):
11 if self.on_credit is not None:
12 hook, self.on_credit = self.on_credit, None
13 hook()
14 self.balances[account] = self.balances.get(account, 0) + cents
15
16
17 class JobHandler:
18 # Contract: a job_id moves money at most once, and a job whose
19 # credit raises must stay retryable when the queue redelivers it.
20 def __init__(self, ledger):
21 self.ledger = ledger
22 self.seen_ids = set()
23
24 def handle(self, msg):
25 job_id = msg['job_id']
26 if job_id in self.seen_ids:
27 return 'duplicate'
28 self.ledger.credit(msg['account'], msg['cents'])
29 self.seen_ids.add(job_id)
30 return 'applied'
Figure 1: the round’s three phases and the delivery patterns the finished consumer must survive. Every pattern must credit merchant-42 exactly once, whichever replica or restarted worker receives the copy.
1Inspection Checklist (Where to Look)
(1) Claim Timing: For each line of handle(), ask what a second delivery of the same job_id would observe if it arrived at exactly that moment.
(2) Failure Path: Trace what state is left behind when credit() raises, and whether the queue’s redelivery can still succeed afterwards.
(3) Dedup Visibility: In Phase 3, list every process that could receive the job and check which copy of the dedup state each one actually reads.
(4) Retention Horizon: Compare how long a dedup record lives with how late the queue may redeliver.
(5) Durability Boundaries: Mark every commit in the draft and ask what survives if the process dies between two of them.
2Visual Review Map (Figure 2)

Figure 2: One job’s path through a consumer. Each uniform ? marks a question you should settle before patching; the answers are yours to work out.
3Interviewer Nudge & Line Focus
Walk each phase and answer the Socratic check before editing:
on_credit hook delivers the same message again at that moment, what does line 26 report? Once a second look-up can see the job, what must the handler do so that a credit which raises does not look finished forever?
REDELIVERY_HORIZON_S, and which commit does the dedup row ride in? A prompt that leaves any one of them open lets the assistant pick the convenient answer.
evict_expired() first, and a duplicate delivered mid-commit on the same replica. Which of them end with 1,000 cents instead of 500?
Leave a Reply