Search RU
Tasks / CI/CD / Flaky job quarantine
Statement hard

Flaky job quarantine

Implement `update_quarantine(history, job, result, policy)` — updating the
quarantine of a flaky job.

`history` is the sequence of the job's previous results (`True` — passed,
`False` — failed), oldest first. `result` is the current run. The function does
not mutate `history`; it returns a `QuarantineDecision`.

```python
policy = QuarantinePolicy(
window=4,
failure_threshold=0.5,
recovery_runs=3,
blocking_jobs={"deploy"},
)
decision = update_quarantine([True, False, True], "e2e", False, policy)
# decision.quarantined is True
# decision.transition == "quarantined"
# decision.history == (True, False, True, False)
```

`QuarantinePolicy` fields:

- `window` — window size, an integer ≥ 1;
- `failure_threshold` — failure ratio, a number `0 < x <= 1`;
- `recovery_runs` — how many consecutive passes are needed to leave;
- `blocking_jobs` — a set of non-empty strings; such jobs are never
quarantined.

`QuarantineDecision` fields: `job`, `quarantined`, `transition`, `history`.

Rules:

- `job` is a non-empty string; `result` is strictly a `bool`;
- `history` is a list or tuple of `bool`; an empty history is allowed;
- a job enters quarantine when the failure ratio in the last `window` runs is
`>= failure_threshold`; the window starts counting only after `window` runs
since the start or since the last exit from quarantine;
- while quarantined, the job stays until `recovery_runs` consecutive runs pass;
after leaving, the window starts fresh;
- `transition`: `"quarantined"` if the job entered quarantine on this run;
`"recovered"` if it left; otherwise `"none"`;
- `decision.history` is a new tuple with `result` appended;
- invalid inputs and an invalid policy raise `ValueError`.

Hidden tests: a stable job, a flaky job, recovery, a blocking job, an empty
history, re-quarantine after recovery, and input immutability.

Python
ci-cdflaky-testsquarantinestate-machinepolicy
solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

Why should a job not be quarantined after its first failure? How is a "failure window" different from a "streak of passes" during recovery?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts