Skip to content

fix(ci): test shards take up to 11 minutes, over the budget #4250

Closed

Description

@dan-petty

Problem Description

The test shards of CI take more than the 5-minute budget of AGENTS.md rule 5 (the owner allows 1 minute of tolerance). The slowest shard changes from run to run, so the split by line count does not balance the time.

Reproduction Steps

  1. Read the job times of the Tests & Coverage (shard N of 4) jobs of the last five successful ci.yml runs on main.
  2. Run uv run devops ci locally and read the slowest tests that it prints.

Expected vs Actual Behavior

Expected: each shard averages 5 minutes or less.

Actual, shard times in minutes (the last five successful runs on main, 2026-11-07):

Run Shard 1 Shard 2 Shard 3 Shard 4
42390712729 5.9 6.1 4.6 9.8
42387511375 4.4 5.9 11.3 3.9
42386768643 6.6 5.8 6.8 2.3
42385949201 4.0 5.2 5.8 4.6
42385531727 2.1 3.2 2.0 3.1
Average 4.6 5.2 6.1 4.7

shard_test_files (src/devops_cli/core/test_selection.py) gives modules to shards by line count, so a module with few lines and slow tests can make one shard long.

Environment Diagnostics

GitHub-hosted ubuntu-26.04 runners, four shards (#3050). A local full gate on 2026-11-09 printed "Tests took 16m 17s, over the 5m budget" on a host that also ran other work, so the local times below show only which tests are slow.

Logs, Traces, or Terminal Output

Slowest tests of the local run:

  • 154 s tests/prometheus/test_k8s_monitoring_alerting.py::test_each_unit_tested_rule_fires_on_a_sample_and_stays_quiet_on_a_normal_series
  • 74 s tests/commands/test_ai_local_dry_run.py::test_local_ai_commands_run_cleanly
  • 48 s tests/roadmap/stage/test_roadmap_stage_plan.py::test_check_proposal_refuses_a_plan_number_differing_from_a_constant
  • 47 s tests/roadmap/stage/test_roadmap_stage_eval_facts_context.py::test_a_golden_case_of_each_missing_shape_reaches_the_model_and_none_ends_refused
  • 44 s tests/roadmap/implement/test_roadmap_implement_job.py::test_a_run_gives_each_item_the_target_or_reason_of_a_run_over_that_item_alone
  • 40 s tests/server/test_service_memory.py::test_the_heap_at_round_200_is_within_the_byte_budget_of_the_heap_at_round_10

Possible directions: make the slowest tests fast, or split the shards by measured time (pytest-split, already a dependency, balances groups from stored durations).

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Proposed design

Problem statement

The four test shards of CI take up to 11 minutes, and the slowest shard changes from run to run. The split by line count in shard_test_files cannot see that a short module can hold slow tests. The average total of the four shards is about 20.6 minutes, so a perfect balance gives about 5.2 minutes for each shard. That is inside the 5-minute budget plus the 1-minute tolerance of AGENTS.md rule 5. So the change balances the shards by measured time and does not need to cut the total time.

Acceptance criteria

Requirement: Shards balance by recorded duration

The function shard_test_files SHALL weigh each test module by its recorded duration and SHALL place the modules, heaviest first, in the shard with the lowest total weight.

Scenario: A short module with a long recorded duration

  • WHEN a module of 10 lines has a recorded duration of 300 seconds and three modules of 500 lines each have 10 seconds
  • THEN the short module is alone in its shard and the three other modules share the other shards

Requirement: Durations sum by module

The reader of the durations file SHALL add the duration of each test id of the pytest-split format to the module path that the id names.

Scenario: Two tests of one module

  • WHEN the file holds tests/a/test_x.py::test_one with 2.5 and tests/a/test_x.py::test_two with 1.5
  • THEN the weight of tests/a/test_x.py is 4.0

Requirement: A new module gets the mean weight

The function shard_test_files SHALL give a test module that has no recorded duration the mean of the recorded module durations.

Scenario: A module that the file lacks

  • WHEN the file holds two modules with 10 and 30 seconds and the tree holds a third module
  • THEN the third module weighs 20 seconds

Requirement: A deleted module is ignored

The function shard_test_files SHALL ignore each recorded duration whose module is not in the tests tree.

Scenario: A stale entry

  • WHEN the file names a module that the tree no longer holds
  • THEN no shard lists the module and the weights of the other modules do not change

Requirement: No durations file falls back to line count

The function shard_test_files SHALL weigh each module by its line count when the durations file is absent or cannot be read.

Scenario: A missing file

  • WHEN the repository holds no durations file
  • THEN the lists of the shards equal the lists of the line-count split of the base commit

Requirement: Each module lands in exactly one shard

The function shard_test_files SHALL return the same lists on every runner, and the lists of all shards SHALL together hold each test module exactly once.

Scenario: Four shards over one tree

  • WHEN the function runs for shards 1 to 4 on the same tree and file
  • THEN the union of the four lists equals the set of test modules and no module appears twice

Existing Tool Considered

pytest-split (jerry-git/pytest-split, MIT, open source) records durations (--store-durations) and splits by stored durations. It lacks a split by module: it splits at test level, so each shard must collect the whole suite, and the rule of the ci-test-shards spec that each shard collects only its own test files would fail. The change therefore uses pytest-split only to record the durations in its file format, and keeps the module-level split in shard_test_files with a duration weight in place of the line count. Cost of the own code: about 40 lines, about 15,000 tokens to build and test, one working session, and a small cost to refresh the file now and then.

Key questions

  • Is pytest-split already a dependency of the project?: The issue states that it is. The investigation must confirm this in the dependency files before the build. [sources: pyproject.toml]
  • Should the split of the shards be by test or by module?: Split by module. Each shard then collects only its own test files, as the ci-test-shards spec requires, and the one function shard_test_files stays the single place of the split. (best practice) [sources: src/devops_cli/core/test_selection.py, src/devops_cli/commands/ci.py, tests/commands/test_ci_shard.py]
    • Chosen: Module-level split with duration weights. It keeps the spec rule that a shard collects only its own files and changes one function.
    • Rejected: pytest-split at test level (--splits 4 --group N). Each shard collects the whole suite, which breaks the spec rule and adds collection time to each shard.
    • Rejected: Keep the line-count split and speed up the slowest tests only. It does not fix the imbalance, and each new slow test brings the problem back.
  • Where do the durations live?: In a committed file in the pytest-split format. A person or an agent refreshes it with uv run pytest --store-durations on an idle host. A committed file gives every runner the same input, so every shard computes the same lists and the split stays deterministic. (best practice) [sources: src/devops_cli/core/test_selection.py]
    • Chosen: Committed durations file, refreshed by hand. It is deterministic on every runner and needs no network or cache service.
    • Rejected: Durations from a CI cache or artifact of the last run. Shards could read different data, and a cold cache would change the split between runs.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-10: in-use.
The item already names this tool.
Tool: jerry-git/pytest-split (https://github.com/jerry-git/pytest-split), MIT, last activity 2026-10-01, maintained.

Activity

  1. added
    source/agent
    type/bug
    scope/ci
    status/new
    value/medium
    effort/medium
    on Nov 9, 2026
  2. dan-petty commented on Nov 9, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/ci. The (ci) scope of the title gives the scope.
    • Priority: P2-Medium. Shards exceed the 5-minute budget by up to 6 minutes, slowing every CI run, but the pipeline still completes.
    • Value: Medium. Faster and more predictable CI reduces developer wait time and flaky timeout risk on every push.
    • Effort: Medium. Requires either optimising six slow tests or switching the split logic from line count to stored durations via pytest-split.
    • Related: perf(ci): the Tests & Coverage check averages 7 minutes, over the 5 minute bound #3050, which asks for a different change. Both concern the Tests & Coverage check exceeding the 5-minute budget, but #3050 set up the 4-shard split while the candidate fixes the imbalance within that split.
  3. added
    priority/p2-medium
    on Nov 9, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 9, 2026
  5. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at cc3d22e (see proposed design in issue body).

  6. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: The searches found pytest-split, which the issue already names as a project dependency: it records test durations and balances groups by measured time, and it assigns an average weight to tests without a recorded duration. It only splits at test level (the whole suite is collected and tests are deselected), so it cannot give each shard only its own test files as the ci-test-shards rule requires, and it therefore covers the duration recording and balancing part of the need, not the module-level split.

    The item already names this tool.

    Tool: jerry-git/pytest-split (https://github.com/jerry-git/pytest-split)

    • License: MIT
    • Last activity: 2026-10-01
    • Maintained: yes

    Covers:

    • Records per-test durations with pytest --store-durations into a durations file that can be committed to the repo and refreshed by hand.
    • Splits a test suite into groups balanced by recorded execution time (--splits N --group M, with a least_duration algorithm that puts each test in the group with the lowest current total).
    • Weighs tests with no stored duration by the average recorded duration, and tolerates stale or renamed entries.

    Lacks:

    • No module/file-level split: it selects tests after collecting the whole suite, so every shard would collect all test files, which breaks the project rule that each shard collects only its own test files.
    • Collecting the full suite on each shard adds collection time to every shard instead of keeping the cheap per-module split in shard_test_files.
    • It gives no API to place modules in shards, so the module-level balancing the design asks for still has to be written in shard_test_files.

    Tools considered:

    Searches run:

    • balance pytest shards by test duration balancing tool, results: 5
    • pytest-split split test suite by stored durations, results: 5
    • jerry-git pytest-split GitHub, results: 5
    • "pytest-split" PyPI store durations, results: 5
    • pytest plugin run subset of tests per CI job using last run durations, results: 5
    • pytest xdist --dist loadfile balance test files across workers, results: 5
    • github pytest-test-groups split tests by timing, results: 5
    • pytest-split, results: 5

    Sources:

    Checked on 2026-11-10. A person decides what this means for the item. The check changes no label and closes no issue.

  7. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    New evidence on 2026-11-11: the Test Job Duration job of the CI Quality Gate on main at 5f75d256 (run 42977184776, job 130378873629) failed with Over the limit: the last 20 runs of Tests & Coverage took 540 s on average, and the limit is 300 s. On pull request #4543, Tests & Coverage (shard 2 of 4) started at 07:45 UTC and was still in progress at 08:17 UTC. The same shard took 8 minutes on #4526 at 06:33 UTC.

  8. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Another observation: on PR #4548 (run 42981611761, attempt 1), Tests & Coverage (shard 2 of 4) ran from 07:57 to 08:22 UTC with 5661 items and the job stopped at its time limit ("The operation was canceled") with no failed test. The other three shards of the same run took 5 to 7 minutes, and shard 2 of the main run 42977184776 took 7.5 minutes. The shard holds no test file that the PR changes. At that time 14 CI Quality Gate runs of other branches were queued or in progress, so runner load is a possible cause. The pytest progress lines show gaps of 1 to 6 minutes (for example 08:04:26 to 08:10:44).

  9. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Scope from the CI audit (2026-11-11)

    Problem (measured)

    shard_test_files (src/devops_cli/core/test_selection.py:338-357) splits the shards by line count. The fixed gap of the slowest shard is about 66 s on Tests & Coverage and 70 s on Service Image Tests (12 runs). A replay on the current tree gives max/mean 1.28 for 4 shards and 1.52 for 6 shards. The registry parse is 44-65% of each shard, so #4509 changes the balance. pytest-split 0.11.0 loads in each run and has no use.

    New scope

    Acceptance criteria

    Dependencies

    Depends on #4509 and #4673.

  10. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    #5377 merged the shard weights from recorded test times (tests/module_durations.json, devops ci durations), which is what this item asks for. Closed as done by #5377.

  11. removed
    status/ready
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/cisource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions