Skip to content

fix(ci): narrowed test workers fall back and slow runs #5606

Closed

Description

@dan-petty

Problem

The fix of #4472 (PR #5547, merged 2026-11-14 as 2126dc6, released in v0.136.0) does not work for the selection that #4472 reports, and it slows down narrowed test runs. An agent vet of the merged change found one blocking and two major defects. Each one was reproduced.

  1. The collection exits 1, so the fix falls back. _collection_cmd (src/devops_cli/commands/ci.py:1937) runs uv run pytest -n 0 --collect-only -q <targets>. syrupy makes this command exit 1 when a selected module has unused snapshots ("Re-run pytest with --snapshot-update"). _collected_test_count reads exit 1 as a failed collection, prints "Could not collect the selected tests ... exit code 1" and goes back to the file count. So the 7 modules of the fix(ci): a narrowed ci test runs many slow tests serially #4472 reproduction (422 tests) still run with -n 0. Example: tests/output/test_output.py collects 55 tests and exits 1. The shard rows (ci.py:697) already pass --snapshot-warn-unused. The unit test fakes exit 0, so it cannot find this.
  2. An explicit -n 4 ignores PYTEST_XDIST_AUTO_NUM_WORKERS. When more than 50 tests are collected, _narrowed_workers (ci.py:1981) returns the literal 4. A runner pod with 1 CPU (PYTEST_XDIST_AUTO_NUM_WORKERS=1) then runs 4 workers on 1 CPU. Measured on 1 pinned CPU with 52 tests: -n 0 took 33.6 s, the new default took 104 s. The implement job runs uv run devops ci test <file> many times in each attempt.
  3. Each narrowed run starts a second pytest session. The collection took 23 to 47 s of wall time at a high host load (most of it in tests/support/bootstrap.py register_support_plugins). A 52-test module took 86 to 103 s against 42 to 44 s with -n 0. design.md does not measure this cost.

Minor points from the same vet:

  • The collection uses DEFAULT_SUBPROCESS_TIMEOUT_SECONDS (1800 s), against the nested-timeout rule.
  • The warning at ci.py:1966 has no safe=True, so brackets in a target or a -k value become Rich markup or raise MarkupError.
  • src/devops_cli/ai/knowledge_base/devops_cli/tasks/ci_quality_gate.md:69 still states the old file-count rule (AGENTS.md rule 8).
  • No test proves that a --repeat run makes no collection, and the 12-module boundary test was deleted.

Expected behavior

  • A selection that collects more than 50 tests runs on the capped workers, also when a module has unused snapshots.
  • A pod with PYTEST_XDIST_AUTO_NUM_WORKERS=1 runs serially and starts no collection session.
  • The extra collection costs a measured, bounded time, with a short timeout of its own.

Fix

  • Add --snapshot-warn-unused to the collection argv, with a unit case that fails when the flag goes away, and record a real collection of the fix(ci): a narrowed ci test runs many slow tests serially #4472 selection (exit 0, 422 items, -n 4).
  • Use min(DEFAULT_CI_TEST_NARROWED_WORKERS, PYTEST_XDIST_AUTO_NUM_WORKERS) when the variable is set, run serially at 1, and skip the collection when the result cannot change the worker count.
  • Add DEFAULT_CI_TEST_COLLECTION_TIMEOUT_SECONDS with the measured time, give the warning safe=True, fix the knowledge-base page, and restore the boundary and --repeat tests.

Evidence

Source: #5547

Proposed design

Problem statement

The fix of #4472 does not work for the reported selection and slows narrowed test runs. The collection exits 1 when a selected module has unused snapshots, so the run falls back to the file count. An explicit -n 4 ignores PYTEST_XDIST_AUTO_NUM_WORKERS, and each narrowed run starts a second pytest session. The change corrects these defects and restores the missing tests.

Acceptance criteria

Requirement: Collection command tolerates unused snapshots

The narrowed test collection command SHALL include --snapshot-warn-unused.

Scenario: Unused snapshot in a selected module

  • WHEN a selected module has an unused snapshot and the collection command is built
  • THEN the command argv contains --snapshot-warn-unused and the test fails when the flag is absent.

Requirement: The #4472 selection collects 422 tests

A recorded collection of the seven #4472 modules SHALL exit 0 and report 422 test items.

Scenario: Recorded #4472 collection

Requirement: Environment caps the narrowed workers

When PYTEST_XDIST_AUTO_NUM_WORKERS is above 1, the narrowed worker count SHALL be the minimum of DEFAULT_CI_TEST_NARROWED_WORKERS and that value.

Scenario: Two-worker pod with 52 tests

  • WHEN PYTEST_XDIST_AUTO_NUM_WORKERS=2 and the collection reports 52 tests
  • THEN the run uses -n 2.

Requirement: One worker runs serial

When PYTEST_XDIST_AUTO_NUM_WORKERS is 1, the run SHALL use -n 0 and SHALL make no collection call.

Scenario: One-worker pod with 52 tests

  • WHEN PYTEST_XDIST_AUTO_NUM_WORKERS=1 and the selection has target paths
  • THEN the command argv contains -n 0 and the fake process records no --collect-only call.

Requirement: Explicit worker count skips collection

The run SHALL skip the collection when an explicit -n value already sets the worker count.

Scenario: Explicit worker count

  • WHEN the user passes -n 2 and the selection has target paths
  • THEN the fake process records no --collect-only call and the run uses -n 2.

Requirement: Collection has a short timeout

The collection SHALL use DEFAULT_CI_TEST_COLLECTION_TIMEOUT_SECONDS and not DEFAULT_SUBPROCESS_TIMEOUT_SECONDS.

Scenario: Collection timeout value

  • WHEN a test starts the collection command
  • THEN the process fake receives DEFAULT_CI_TEST_COLLECTION_TIMEOUT_SECONDS as its timeout.

Requirement: Failed collection keeps the fallback

A collection that exits non-zero for a reason other than unused snapshots SHALL keep the file-count fallback and print the warning.

Scenario: Collection exits 3 over 13 modules

  • WHEN the fake collection exits 3 over 13 target files
  • THEN the run prints the fallback warning and uses DEFAULT_CI_TEST_NARROWED_WORKERS.

Requirement: Fallback warning keeps brackets literal

The collection fallback warning SHALL pass safe=True so a target or -k value with brackets prints literally.

Scenario: Bracketed keyword

  • WHEN a collection fails for a -k value that holds brackets
  • THEN the warning prints the brackets literally and raises no MarkupError.

Requirement: Knowledge base states the collected-test rule

The CI quality gate knowledge-base page SHALL state the collected-test worker rule and the serial cap.

Scenario: Knowledge-base page content

  • WHEN the page ci_quality_gate.md is read
  • THEN it says that a selection over 50 tests uses the capped workers and a selection at or below 50 runs serially.

Requirement: Repeat run makes no collection

A --repeat run SHALL make no collection call.

Scenario: Repeat run with a target

  • WHEN the user passes --repeat 2 and a target path
  • THEN the fake process records no --collect-only call.

Requirement: File-count fallback keeps the 12-module boundary

The file-count fallback SHALL use DEFAULT_CI_TEST_NARROWED_WORKERS above 12 target modules and -n 0 at 12 or fewer.

Scenario: Twelve target modules

  • WHEN a collection fails and the targets name 12 modules
  • THEN the run uses -n 0; a 13-module case uses DEFAULT_CI_TEST_NARROWED_WORKERS.

Requirement: Collection cost is measured and bounded

The change SHALL record the measured collection time and SHALL set DEFAULT_CI_TEST_COLLECTION_TIMEOUT_SECONDS above it.

Scenario: Measured collection time

Existing Tool Considered

No alternatives check has run. No open, maintained tool or library covers this internal worker-selection fix. The change extends existing code in src/devops_cli/commands/ci.py. Estimated cost to build, keep and fix it: about 12,000 output tokens and 3 hours.

Key questions

  • Should the collection argv include --snapshot-warn-unused?: Yes. The shard rows already pass it, and syrupy exits 1 on an unused snapshot without it. (best practice) [sources: src/devops_cli/commands/ci.py:697, src/devops_cli/commands/ci.py:1937, openspec/changes/archive/2026-11-13-4472-a-narrowed-ci-test-runs-many/design.md:35]
    • Chosen: Add --snapshot-warn-unused to the collection argv. The shard rows already pass it, and it stops syrupy from failing an unused snapshot.
    • Rejected: Read collection exit 1 as success. Exit 1 also covers real collection failures, so this would hide them.
    • Rejected: Disable syrupy snapshots in the collection. The collection must import the same test modules as the run.
  • How should PYTEST_XDIST_AUTO_NUM_WORKERS cap the narrowed workers?: Use min(DEFAULT_CI_TEST_NARROWED_WORKERS, PYTEST_XDIST_AUTO_NUM_WORKERS) when the variable is set. At 1, use -n 0 and skip the collection. (best practice) [sources: docs/CLUSTER.md:63, k8s/devops/cronjob-runner.yaml:86, k8s/devops/roadmap-service/admissionpolicy.yaml:222, src/devops_cli/commands/ci.py:1981]
    • Chosen: Cap with the environment value. The pod sets the variable to its CPU limit. Four workers on one CPU took 104 s against 33.6 s serial.
    • Rejected: Keep the literal 4. It overrides the CPU limit of the pod.
    • Rejected: Use the environment value with no default cap. It can start more than the 4 workers that the design measured.
  • When should the run skip the collection?: Skip when PYTEST_XDIST_AUTO_NUM_WORKERS is 1, when -n is explicit, or when the run has no targets. (best practice) [sources: src/devops_cli/commands/ci.py:2071, src/devops_cli/commands/ci.py:1981, docs/CLUSTER.md:63]
    • Chosen: Skip when the worker count cannot change. It removes the second pytest session on a one-CPU pod and for an explicit -n.
    • Rejected: Always collect. It adds 23 to 47 s to each narrowed run.
    • Rejected: Never collect. It keeps the file-count rule and misses a module with many tests.
  • What timeout should bound the collection?: Add DEFAULT_CI_TEST_COLLECTION_TIMEOUT_SECONDS above the measured collection time and below DEFAULT_SUBPROCESS_TIMEOUT_SECONDS. (best practice) [sources: src/devops_cli/commands/ci.py:1937, src/devops_cli/config/defaults.py:383]
    • Chosen: Dedicated short timeout. The collection took 23 to 47 s, and 1800 s is too long for it.
    • Rejected: Keep DEFAULT_SUBPROCESS_TIMEOUT_SECONDS. It lets a stuck collection hold the run for 1800 s.
    • Rejected: No timeout. It can stall a narrowed run.
  • Should the fallback warning pass safe=True?: Yes. A target or -k value with brackets must print literally. (best practice) [sources: src/devops_cli/commands/ci.py:1966, tests/output/test_output.py]
    • Chosen: Pass safe=True. It stops Rich markup and MarkupError on a valid bracket.
    • Rejected: Escape the text before the call. It duplicates the safe path and can miss a caller.
    • Rejected: Leave the warning as is. It can raise MarkupError on a valid -k value.
  • Should the knowledge-base page change in the same pull request?: Yes. AGENTS.md rule 8 says to fix a document that the change makes wrong in the same pull request. (best practice) [sources: AGENTS.md, src/devops_cli/ai/knowledge_base/devops_cli/tasks/ci_quality_gate.md:69]
    • Chosen: Update the page in the same pull request. The page still states the old file-count rule.
    • Rejected: File a separate docs issue. It leaves the old rule in the knowledge base.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. A merged fix in v0.136.0 regressed the original selection and made narrowed runs slower than before. The regression commit 2126dc6 does not set P0: the author has no write access, and no caller attached it as evidence. A person can set P0 with the label priority/p0-critical.
    • Related: fix(ci): a narrowed ci test runs many slow tests serially #4472, which asks for a different change. Both target narrowed CI test worker selection, but #4472 asked for multi-worker runs and the candidate fixes defects in that delivered fix.
  3. added
    priority/p1-high
    owner/agent
    on Nov 14, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 14, 2026
  5. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

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

  6. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 14, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 14, 2026
  8. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor
  9. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-14 04:20 UTC

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/p1-highscope/cisource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions