Implement an in-memory queue whose jobs are reserved with expiring leases. A failed worker must not hold a job forever, and jobs that exceed their retry budget move to a dead-letter state.
Question
Implement an in-memory queue whose jobs are reserved with expiring leases. A failed worker must not hold a job forever, and jobs that exceed their retry budget move to a dead-letter state.
```python
class WorkQueue:
def __init__(self, max_attempts: int, lease_ms: int): ...
def enqueue(self, item_id) -> bool: ...
def reserve(self, now: int) -> dict | None: ...
def complete(self, item_id, token: str, now: int) -> bool: ...
def fail(self, item_id, token: str, now: int) -> bool: ...
def expire(self, now: int) -> None: ...
```
## Requirements and assumptions
- Jobs move among `ready`, `reserved`, `completed`, and `dead`.
- `reserve` increments attempts and returns `{item_id, token}` plus an internal deadline.
- On explicit failure or `lease_deadline <= now`, return the job to ready unless attempts reached `max_attempts`; then mark it dead.
- Completion or failure with a stale token returns `False` without changing state.
- Queue ready jobs deterministically and reject duplicate active IDs.
- Validate positive limits and use the explicit clock supplied by callers.
## Follow-ups
1. Add delayed exponential backoff and priorities.
2. Persist the queue across crashes with idempotent operations.
3. Use fencing tokens to protect an external side effect.
4. Support replay from the dead-letter queue.The public brief is identical for people and crawlers. Account access changes tools, not the indexed description.