Grok 4.1 Fast· xai
Frontier Reasoning Traces
Build a CI flake quarantine: auto-detect, isolate, and deflake.
- User
Build a CI flake quarantine: auto-detect, isolate, and deflake.
- Assistant
Build a CI flake quarantine: auto-detect, isolate, and deflake.
name: CI with Flake Quarantine
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
detect-and-analyze:
name: 🔍 Detect & Analyze Flakes (5x runs)
runs-on: ubuntu-latest
permissions:
checks: write # Optional: for annotations
outputs:
stable_k: ${{ steps.flakes.outputs.stable_k }}
quarantine_k: ${{ steps.flakes.outputs.quarantine_k }}
num_flakes: ${{ steps.flakes.outputs.num_flakes }}
steps:
- name: 📥 Checkout
uses: actions/checkout@v4
- name: 🐍 Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11' # Adjust to your Python version
- name: 📦 Install dependencies
run: |
pip install --upgrade pip
pip install pytest pytest-rerunfailures # Core testing deps
# Add: pip install -r requirements-dev.txt # Uncomment if you have one
- name: 🧪 Run tests 5x for flake detection
run: |
for i in {1..5}; do
echo "=== Test run #$i ==="
pytest -v --tb=no > test_run_$i.log 2>&1 || true
done
- name: 🛠️ Create flake analyzer script
run: |
cat > analyze_flakes.py << 'PYEOF'
import sys
import json
from collections import defaultdict
def parse_log(log_file):
passes = set()
fails = set()
try:
with open(log_file, 'r') as f:
for line in f:
parts = line.split(maxsplit=2)
if len(parts) >= 2:
nodeid = parts[0]
status = parts[1]
if status == 'PASSED':
passes.add(nodeid)
elif status == 'FAILED':
fails.add(nodeid)
except Exception:
pass
return passes, fails
all_passes = defaultdict(int)
all_fails = defaultdict(int)
log_files = sys.argv[1:] if len(sys.argv) > 1 else []
for log_file in log_files:
passes, fails = parse_log(log_file)
for t in passes:
all_passes[t] += 1
for t in fails:
all_fails[t] += 1
flaky = []
all_tests = set(all_passes.keys()) | set(all_fails.keys())
for test in all_tests:
p = all_passes[test]
f = all_fails[test]
total = p + f
if total >= 3 and p > 0 and f > 0:
flaky.append(test)
print(json.dumps(flaky))
PYEOF
- name: 📊 Analyze for flakes
id: flakes
run: |
FLAKY_JSON=$(python3 analyze_flakes.py test_run_*.log 2>/dev/null || echo '[]')
FLAKY_EXPR=$(echo "$FLAKY_JSON" | jq -r 'if length == 0 then empty else join(" or ") end')
if [ -n "$FLAKY_EXPR" ]; then
STABLE_K="not ($FLAKY_EXPR)"
QUAR_K="$FLAKY_EXPR"
NUM_FLAKES=$(echo "$FLAKY_JSON" | jq '. | length')
else
STABLE_K=""
QUAR_K=""
NUM_FLAKES=0
fi
echo "stable_k<<$STABLE_K" >> $GITHUB_OUTPUT
echo "quarantine_k<<$QUAR_K" >> $GITHUB_OUTPUT
echo "num_flakes=$NUM_FLAKES" >> $GITHUB_OUTPUT
echo "::notice::Detected $NUM_FLAKES flaky tests"
echo "$FLAKY_JSON" | jq -r 'select(. != null) | .[]' | while read test; do
echo "::warning::Flaky test: $test"
done
# Optional: Save flakes for artifact
echo "$FLAKY_JSON" > flakes.json
- name: 📤 Upload flake report
uses: actions/upload-artifact@v4
with:
name: flake-report
path: flakes.json
test-stable:
name: ✅ Stable Tests (Fast lane)
runs-on: ubuntu-latest
needs: detect-and-analyze
steps:
- name: 📥 Checkout
uses: actions/checkout@v4
- name: 🐍 Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: 📦 Install dependencies
run: |
pip install pytest pytest-rerunfailures
# pip install -r requirements-dev.txt
- name: 🧪 Run stable tests only
run: |
echo "Stable k-expression: '${{ needs.detect-and-analyze.outputs.stable_k }}'"
pytest -v --tb=no -k "${{ needs.detect-and-analyze.outputs.stable_k }}"
test-quarantine:
name: 🔄 Quarantine Tests (High retries)
runs-on: ubuntu-latest
needs: detect-and-analyze
if: needs.detect-and-analyze.outputs.num_flakes > '0'
steps:
- name: 📥 Checkout
uses: actions/checkout@v4
- name: 🐍 Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: 📦 Install dependencies
run: |
pip install pytest pytest-rerunfailures
# pip install -r requirements-dev.txt
- name: 🧪 Run quarantined tests with 10x retries (60s delay)
run: |
echo "Quarantine k-expression: '${{ needs.detect-and-analyze.outputs.quarantine_k }}'"
pytest --reruns=10 --reruns-delay=60 -v --tb=no -k "${{ needs.detect-and-analyze.outputs.quarantine_k }}"
Auto-detect 📡:
pytest suite 5 times sequentially, capturing verbose logs (-v --tb=no for clean parsing).tests/test_foo.py::TestBar::test_baz).Isolate 🛡️:
test-stable): Runs only non-flaky tests once using pytest -k "not (flaky1 or flaky2)". Fast feedback on reliable code.test-quarantine): Skips if no flakes. Otherwise, runs only flaky tests in isolation.Deflake ⚡:
--reruns=10 --reruns-delay=60 (retries up to 10x, 1min between for network/DB flakes).flake-report.json for review.parse_log() for Jest (npm test -- --reporter=spec), Go (go test -v), etc. (grep PASS/FAIL lines).pytest calls: e.g., pytest myflags....total >= 3, add f / total > 0.1 in script.flakes.json in repo, merge if quarantined tests pass 3 builds in a row (add GitHub API step).strategy.matrix.iteration: [1,2,3,4,5], artifact logs, merge in analyze job.if f > 1 and p > 1.Drop this .github/workflows/ci-flake-quarantine.yml in your repo. Tests pass? Merge! Flakes quarantined & deflaked automatically. No more random reds. Case closed. 🚀