Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(ci): adopt optimal_chunks for balanced backend test sharding (#64461)

Julian Bez37aef3162bb3
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index c442c5a73798..9f327a9b93a8 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -2328,6 +2328,14 @@ jobs:
shell: 'signal-fanout bash --noprofile --norc -eo pipefail {0}'
run: | # async_migrations covered in ci-async-migrations.yml
set +e
+ # Pick the split algorithm at runtime: optimal_chunks only when the
+ # installed pytest-split offers it, else duration_based_chunks. A workflow
+ # edit runs merged with master, so an unrebased PR on older pytest-split
+ # must not hit an unknown --splitting-algorithm. See AGENTS.md (CI).
+ SPLITTING_ALGORITHM=duration_based_chunks
+ if python -c "from pytest_split.algorithms import Algorithms; raise SystemExit(0 if 'optimal_chunks' in Algorithms.names() else 1)" 2>/dev/null; then
+ SPLITTING_ALGORITHM=optimal_chunks
+ fi
if [[ "$MODE" == "selected" ]]; then
# Selected mode: run only the test files chosen by the snob shadow selector.
# No --splits/--group (no sharding), no --store-durations (don't pollute timing
@@ -2361,7 +2369,7 @@ jobs:
--splits ${{ matrix.concurrency }} --group ${{ matrix.group }} \
--durations=1000 --durations-min=1.0 --store-durations \
--pytest-durations=100 \
- --splitting-algorithm=duration_based_chunks \
+ --splitting-algorithm="$SPLITTING_ALGORITHM" \
--reruns 2 --reruns-delay 1 \
-r fEsxX \
--junitxml=junit-core.xml \
@@ -2416,6 +2424,14 @@ jobs:
TEMPORAL_FILES: ${{ needs.select-tests.outputs.temporal_files }}
run: |
set +e
+ # Pick the split algorithm at runtime: optimal_chunks only when the
+ # installed pytest-split offers it, else duration_based_chunks. A workflow
+ # edit runs merged with master, so an unrebased PR on older pytest-split
+ # must not hit an unknown --splitting-algorithm. See AGENTS.md (CI).
+ SPLITTING_ALGORITHM=duration_based_chunks
+ if python -c "from pytest_split.algorithms import Algorithms; raise SystemExit(0 if 'optimal_chunks' in Algorithms.names() else 1)" 2>/dev/null; then
+ SPLITTING_ALGORITHM=optimal_chunks
+ fi
if [[ "$MODE" == "selected" ]]; then
# Selected mode: no sharding, no --store-durations (don't pollute
# timing data with a partial run), no --reruns (fail loud; the ready
@@ -2436,7 +2452,7 @@ jobs:
--splits ${{ matrix.concurrency }} --group ${{ matrix.group }} \
--durations=100 --durations-min=1.0 --store-durations \
--pytest-durations=100 \
- --splitting-algorithm=duration_based_chunks \
+ --splitting-algorithm="$SPLITTING_ALGORITHM" \
--reruns 2 --reruns-delay 1 \
-r fEsxX \
--junitxml=junit-temporal.xml \
diff --git a/.github/workflows/ci-dagster.yml b/.github/workflows/ci-dagster.yml
index c2dc882538c9..0fa903ddd87d 100644
--- a/.github/workflows/ci-dagster.yml
+++ b/.github/workflows/ci-dagster.yml
@@ -395,11 +395,19 @@ jobs:
- name: Run Dagster tests
run: |
+ # Pick the split algorithm at runtime: optimal_chunks only when the
+ # installed pytest-split offers it, else duration_based_chunks. A workflow
+ # edit runs merged with master, so an unrebased PR on older pytest-split
+ # must not hit an unknown --splitting-algorithm. See AGENTS.md (CI).
+ SPLITTING_ALGORITHM=duration_based_chunks
+ if python -c "from pytest_split.algorithms import Algorithms; raise SystemExit(0 if 'optimal_chunks' in Algorithms.names() else 1)" 2>/dev/null; then
+ SPLITTING_ALGORITHM=optimal_chunks
+ fi
pytest posthog/dags products/**/dags \
--reuse-db \
--splits ${{ matrix.concurrency }} --group ${{ matrix.group }} \
--durations=100 --durations-min=1.0 --store-durations \
- --splitting-algorithm=duration_based_chunks \
+ --splitting-algorithm="$SPLITTING_ALGORITHM" \
--reruns 2 --reruns-delay 1 \
-r fEsxX \
--junitxml=junit-dagster-${{ matrix.group }}.xml
diff --git a/pyproject.toml b/pyproject.toml
index fa22527a748e..104d3c979825 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -246,7 +246,7 @@ dev = [
"pytest-env~=1.2.0",
"pytest-icdiff~=0.9",
"pytest-mock~=3.15.1",
- "pytest-split~=0.10.0",
+ "pytest-split~=0.11.0", # EXPERIMENT: git-pinned below to the optimal_chunks fork
"pytest-timeout~=2.4.0",
"pytest-xdist~=3.8.0",
"python-dateutil~=2.9.0",
@@ -298,6 +298,11 @@ members = ["tools/hogli"]
[tool.uv.sources]
hogli = { workspace = true }
infi-clickhouse-orm = { git = "https://github.com/PostHog/infi.clickhouse_orm", rev = "4e3996becf66bf5c26580aff12a80425d9d56fe5" }
+# pytest-split: pinned to PostHog's fork (immutable tag) for the order-preserving
+# optimal_chunks split. Fork: PostHog/pytest-split (of jerry-git/pytest-split).
+# To pull upstream fixes: rebase the fork's feat branch onto upstream, cut the next
+# v0.11.0-posthog.N tag, and bump this pin.
+pytest-split = { git = "https://github.com/PostHog/pytest-split", tag = "v0.11.0-posthog.1" }
[tool.isort]
diff --git a/uv.lock b/uv.lock
index a53884fece04..e1745405b9cd 100644
--- a/uv.lock
+++ b/uv.lock
@@ -11,7 +11,7 @@ resolution-markers = [
]
[options]
-exclude-newer = "0001-01-01T00:00:00Z" # This has no effect and is included for backwards compatibility when using relative exclude-newer values.
+exclude-newer = "2026-06-11T08:15:12.733489Z"
exclude-newer-span = "P7D"
[options.exclude-newer-package]
@@ -6015,7 +6015,7 @@ dev = [
{ name = "pytest-icdiff", specifier = "~=0.9" },
{ name = "pytest-mock", specifier = "~=3.15.1" },
{ name = "pytest-rerunfailures", specifier = "~=16.1" },
- { name = "pytest-split", specifier = "~=0.10.0" },
+ { name = "pytest-split", git = "https://github.com/PostHog/pytest-split?tag=v0.11.0-posthog.1" },
{ name = "pytest-timeout", specifier = "~=2.4.0" },
{ name = "pytest-xdist", specifier = "~=3.8.0" },
{ name = "python-dateutil", specifier = "~=2.9.0" },
@@ -6818,15 +6818,11 @@ wheels = [
[[package]]
name = "pytest-split"
-version = "0.10.0"
-source = { registry = "https://pypi.org/simple" }
+version = "0.11.0"
+source = { git = "https://github.com/PostHog/pytest-split?tag=v0.11.0-posthog.1#83365ed14512bb854d51c917389d3359be360c5c" }
dependencies = [
{ name = "pytest" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/46/d7/e30ba44adf83f15aee3f636daea54efadf735769edc0f0a7d98163f61038/pytest_split-0.10.0.tar.gz", hash = "sha256:adf80ba9fef7be89500d571e705b4f963dfa05038edf35e4925817e6b34ea66f", size = 13903, upload-time = "2024-10-16T15:45:19.783Z" }
-wheels = [
- { url = "https://files.pythonhosted.org/packages/d6/a7/cad88e9c1109a5c2a320d608daa32e5ee008ccbc766310f54b1cd6b3d69c/pytest_split-0.10.0-py3-none-any.whl", hash = "sha256:466096b086a7147bcd423c6e6c2e57fc62af1c5ea2e256b4ed50fc030fc3dddc", size = 11961, upload-time = "2024-10-16T15:45:18.289Z" },
-]
[[package]]
name = "pytest-timeout"