Product case study · November 26, 2026
Ask Claude Code to fix a queue that crashes on a tie
Repair a Python priority queue with Claude Code. Reproduce equal-priority crashes, specify FIFO ties, and verify the fix with five unchanged regression tests.

A tie needs an explicit rule. Give jobs an arrival counter so equal priorities preserve queue order without comparing their payloads.
Your job queue handles urgent work perfectly until two jobs are equally urgent. Then Python tries to decide which job dictionary is smaller, and the queue crashes. The missing requirement is surprisingly ordinary: who goes first when priority cannot decide?
For this exercise, choose arrival order. Ask Claude Code to repair a tiny queue, keeping that rule visible in tests. A fix that merely stops the exception can still send the wrong job first.
You need Bash, Python 3, and installed, signed-in Claude Code. The Python baseline and reference repair below were tested locally; the agent prompt is an authored exercise, not a recorded Claude session. Official sources were checked November 26, 2026.
Make the tie fail
Create a disposable folder and the deliberately broken implementation:
queue_lab=$(mktemp -d /tmp/claude-priority-ties.XXXXXX)
cd "$queue_lab"
cat > jobs.py <<'PY'
import heapq
class JobQueue:
def __init__(self):
self._heap = []
def add(self, priority, job):
heapq.heappush(self._heap, (priority, job))
def pop(self):
return heapq.heappop(self._heap)[1]
PY
python3 - <<'PY'
from jobs import JobQueue
q = JobQueue()
q.add(2, {"name": "render"})
q.add(2, {"name": "upload"})
PY
The second add() raises TypeError. Python’s heapq priority-queue notes describe this equal-priority comparison problem. The priority alone no longer separates the entries, so the payload becomes part of the comparison.
Do not catch this exception and keep using that queue. Start a fresh instance for the tests: a failed operation is not evidence that the earlier state was restored.
Decide what a correct queue means
Save this contract as SPEC.md:
JobQueue is an in-memory, single-threaded practice queue.
Callers supply integer priorities and dictionary payloads.
Lower priority numbers leave the queue first.
Equal-priority jobs leave in insertion order, including after a pop.
Return the original payload object without changing it.
The same object may be queued twice; retain both entries.
Popping an empty queue raises IndexError.
Do not add persistence, retries, cancellation, input validation,
external services, or dependencies.
I would choose FIFO ties for a small work queue because it makes waiting predictable. Sorting by a job’s name might also be deterministic, but it would quietly replace arrival order with an unrelated business rule.
Save these checks as test_jobs.py. They test observable behavior without requiring a particular internal representation.
import unittest
from jobs import JobQueue
class JobQueueTests(unittest.TestCase):
def test_lower_number_first(self):
q = JobQueue()
later = {"name": "later"}
urgent = {"name": "urgent"}
q.add(5, later)
q.add(1, urgent)
self.assertIs(q.pop(), urgent)
self.assertIs(q.pop(), later)
def test_equal_priorities_keep_arrival_order(self):
q = JobQueue()
first = {"name": "zebra"}
second = {"name": "apple"}
q.add(2, first)
q.add(2, second)
self.assertIs(q.pop(), first)
self.assertIs(q.pop(), second)
self.assertEqual(first, {"name": "zebra"})
self.assertEqual(second, {"name": "apple"})
def test_tie_after_pop_keeps_waiting_job_first(self):
q = JobQueue()
jobs = [{"name": name} for name in ("a", "b", "c")]
q.add(2, jobs[0])
q.add(2, jobs[1])
self.assertIs(q.pop(), jobs[0])
q.add(2, jobs[2])
self.assertIs(q.pop(), jobs[1])
self.assertIs(q.pop(), jobs[2])
def test_duplicate_payload_is_not_dropped(self):
q = JobQueue()
job = {"name": "repeat"}
q.add(2, job)
q.add(2, job)
self.assertIs(q.pop(), job)
self.assertIs(q.pop(), job)
with self.assertRaises(IndexError):
q.pop()
def test_empty_queue(self):
with self.assertRaises(IndexError):
JobQueue().pop()
Run python3 -m unittest -v test_jobs. In the local baseline, three methods passed and the two distinct-payload tie tests errored. Notice that adding the same dictionary twice does not reproduce the crash. A duplicate-only test would miss the bug.
The deliberately reversed names, zebra then apple, also expose a tempting wrong fix: using the name to break ties.
Give Claude Code the repair boundary
Start claude in the practice folder and paste:
Read SPEC.md, jobs.py and test_jobs.py.
Run python3 -m unittest -v test_jobs and report the baseline.
Explain why distinct dictionary payloads fail at equal priority.
Change only jobs.py to satisfy the contract and unchanged tests.
Keep heapq. Do not compare payloads or use their names to break ties.
Do not edit the tests, install packages, delegate, commit, or access
other folders. Run the same tests after the repair and report the
actual result. Explain why arrival order still holds after a pop.
This follows Anthropic’s debugging workflow: supply a reproduction, make a focused repair, and verify it. The tests are the useful part of the handoff. They let you reject an attractive explanation when the behavior is wrong.
If the agent changes the tests, restore their original contents and rerun them. If it adds retries or a database, point it back to the in-memory contract. If it reports success without command output, run the command yourself.
Compare the repair
A reference implementation that passes all five tests is:
import heapq
import itertools
class JobQueue:
def __init__(self):
self._heap = []
self._arrival = itertools.count()
def add(self, priority, job):
entry = (priority, next(self._arrival), job)
heapq.heappush(self._heap, entry)
def pop(self):
return heapq.heappop(self._heap)[2]
The middle field gives each insertion a distinct position. Python’s itertools.count documentation describes the increasing sequence used here. Keep that counter for the queue’s lifetime; deriving it from the current queue length can reuse a value after a pop.
The payload stays untouched. The queue returns the dictionary you supplied, so changes you make to that dictionary remain visible; this is not a snapshotting API.
Run python3 -m unittest -v test_jobs again and inspect jobs.py. The unittest reference explains the runner and its verbose output. Confirm that the test file still matches the version above.
These checks establish the small queue’s ordering behavior. They do not establish thread safety, recovery after process exit, or delivery of real work. Before connecting an agent to a production worker, specify and test those boundaries separately. A reliable tie-breaker is a useful component, not a complete scheduler.
Sources
Checked November 26, 2026:
- Python heapq — priority ties and queue implementation guidance.
- Python itertools.count — an incrementing counter for insertion order.
- Python unittest — local test runner and assertions.
- Claude Code common workflows — reproduction-led debugging and verification.
Put this to work
Why a priority tuple needs a tie-breaker before its payload.
Try
Ask Claude Code to repair only the queue implementation in a disposable folder.
Prove it worked
Run the same five tests before and after the change, then inspect the diff.
Keep in view
- Reproduce a queue that accepts one job but crashes on a second equal-priority job.
- Specify first-in, first-out ordering within each priority.
- Keep payloads intact and verify the repair with five fixed tests.