Skip to content

fix(tests): refuse real sleeps in tests #4669

Closed

Description

@dan-petty

Problem or Motivation

No rule refuses a real sleep. An audit hook recorded 177 s of real sleep in 629 tests in 96 files, in the 56% of the suite that the audit probed (measured). The ast-grep rule test-elapsed-upper-bound finds 6 of 9 elapsed-time forms and no sleep. src has 23 direct time.sleep calls outside src/devops_cli/core/clock.py.

Saving: It stops the 177 s of real sleep (measured) from coming back.

Found by the CI and test audit of 2026-11-11. The account runs at most 20 hosted jobs at one time (measured), so each job that does no work makes the queue of each pull request longer.

Proposed Solution & Architectural Design

  • ruff already selects TID251, and pyproject.toml:174 has a banned-api table. Add time.sleep with a per-file ignore for src/devops_cli/core/clock.py, and move the 23 call sites to devops_cli.core.clock.sleep.
  • In the session floor (tests/conftest.py), add sys.addaudithook for the standard-library time.sleep audit event (PEP 578). Fail a test that has more than 0.25 s of real sleep, unless it has a mark.

Risk: Tests that still sleep fail, for example the process stop waits in tests/commands/test_ci.py (13.0 s in 1,297 short sleeps). Mark them or route them through the clock.

Target Subsystem

SDLC, Git & PR Governance (pyproject.toml, tests/conftest.py, src/devops_cli/core/clock.py).

Existing Tool Considered

ruff TID251 (banned-api) covers src. No open tool fails a test on a real sleep through the audit event, so the floor part is built: about 30 lines and one floor test (estimated), about 20,000 tokens and 2 hours.

Acceptance Criteria

  • ruff fails on a new time.sleep in src.
  • A probe test that sleeps 1 s for real fails with a clear message.
  • The full suite passes with the guard.

Dependencies

Depends on #4466, #4459 and #4666.

Alternatives check of 2026-11-13: partly.
Tool: pytest-fail-slow (https://pypi.org/project/pytest-fail-slow), MIT License, last activity 2024-06-01, not maintained.

Proposed design

Problem statement

Ban real sleeps in the test suite. No rule refuses a real sleep today: an audit hook recorded 177 s of real sleep in 629 tests in 96 files, and src holds 20 direct time.sleep call sites outside src/devops_cli/core/clock.py (a scan of this base; the issue counts 23). The change bans time.sleep with the ruff TID251 banned-api table of pyproject.toml (pyproject.toml:192), with no per-file ignore, because the project's own gates refuse one; the one real call stays in src/devops_cli/core/clock.py under a reasoned # noqa: TID251 with the src noqa: TID251 ceiling at 1. The other src waits move to devops_cli.core.clock.sleep, the two gh-write tests of tests/commands/test_pr_update.py take the clock fake, and the tests that poll a real child process declare a new real_sleep mark. A floor guard in tests/conftest.py fails a test whose real sleep passes 0.25 s unless it declares that mark.

Acceptance criteria

Requirement: Ruff bans time.sleep in src

The ruff TID251 banned-api table in pyproject.toml SHALL name time.sleep, and no per-file-ignore entry of pyproject.toml SHALL waive TID251.

Scenario: The ban carries no per-file ignore

  • WHEN a test reads the banned-api table and the ruff per-file-ignore tables from pyproject.toml
  • THEN the banned-api table names time.sleep, and no per-file-ignore entry names TID251

Requirement: The clock seam holds the one recorded waiver

src/devops_cli/core/clock.py SHALL be the only module that calls time.sleep, and its call SHALL carry a # noqa: TID251 comment whose reason names the clock seam, with the src noqa: TID251 ceiling of DEFAULT_SUPPRESSION_CEILINGS set to 1.

Scenario: The one waiver is recorded

  • WHEN a test counts the src noqa: TID251 suppressions of the tree and reads their ceiling
  • THEN the count is 1 and the ceiling is 1

Requirement: The src tree waits through the clock

Every wait in src outside src/devops_cli/core/clock.py SHALL call devops_cli.core.clock.sleep and SHALL NOT call time.sleep.

Scenario: No direct time.sleep stays in src

  • WHEN a test scans the modules of src for a direct time.sleep call
  • THEN the only module that calls time.sleep is src/devops_cli/core/clock.py

Requirement: The floor refuses a real sleep

The test floor SHALL fail a test whose real sleep passes 0.25 s, unless the test declares the real_sleep mark.

Scenario: A one-second probe fails

  • WHEN a probe test calls time.sleep(1.0) and the floor runs it
  • THEN the run fails, and the message names the real sleep and the 0.25 s budget

Requirement: The floor allows a declared real sleep

The test floor SHALL pass a test that declares the real_sleep mark, and pyproject.toml SHALL declare that mark in its pytest markers table.

Scenario: A marked probe passes

  • WHEN a probe test declares the real_sleep mark and calls time.sleep(1.0)
  • THEN the floor passes the probe test, and the markers table of pyproject.toml names real_sleep

Requirement: The floor sees no sleep on the fake clock

The test floor SHALL record no real sleep for a test that requests the clock fixture.

Scenario: A probe on the fake clock passes

  • WHEN a probe test requests the clock fixture and calls time.sleep(1.0)
  • THEN the floor records no real sleep and passes the probe test

Requirement: The floor charges a sleep to one test

The floor SHALL attribute each real sleep to the test that caused it.

Scenario: Only the sleeping test fails

  • WHEN one test sleeps 0.3 s for real and the test after it sleeps not at all
  • THEN only the first test fails for real sleep

Requirement: The process-wait tests declare the mark

Every test that polls a real child process through tests/support/waits.py SHALL declare the real_sleep mark.

Scenario: The signal tests carry the mark

  • WHEN a test reads the marks of the tests of tests/commands/test_ci.py that wait through wait_while_running
  • THEN each of them declares real_sleep

Requirement: The pr update tests record no real sleep

Every test of tests/commands/test_pr_update.py SHALL record no real sleep above the floor's budget, so each test that scripts a gh write SHALL request the clock fixture or gh_edge.

Scenario: The write interval of the rate limiter waits on the fake clock

  • WHEN the floor runs the tests of tests/commands/test_pr_update.py that script the gh writes
  • THEN no test of that module fails for real sleep

Requirement: The suite passes with the floor

The full test suite SHALL pass with the floor guard in place.

Scenario: The test row is green

  • WHEN the test row of the gate runs the whole suite with the guard installed
  • THEN the row passes and no test fails for real sleep

Existing Tool Considered

The check of 2026-11-13 found pytest-fail-slow (https://pypi.org/project/pytest-fail-slow, MIT License): it fails a test that runs longer than a configured duration, and its fail_slow marker lets a known slow test through. The check found no PyPI release after 2024-06-01; the repository's last commit is 2026-10-11 (https://github.com/jwodder/pytest-fail-slow, fetched 2026-11-14). It lacks the 0.25 s sleep budget, a sleep-specific message, the static ban (ruff TID251 gives that part) and the audit-event hook. No open tool fails a test on the standard-library time.sleep audit event. So the floor part is built: about 30 lines and one floor test, about 20,000 tokens and 2 hours.

Key questions

  • Which open, maintained tool or library covers the need to refuse a real sleep in a test?: No tool covers the whole need. pytest-fail-slow fails a test on its whole duration and its fail_slow marker lets a known slow test through, so it gives no 0.25 s sleep budget, no sleep-specific message and no audit hook, and a short sleep inside a long test passes it. The check of 2026-11-13 found no PyPI release after 2024-06-01, and the repository's last commit is 2026-10-11. pytest-timeout is already in the project and fails only a hang at its 600 s bound. The floor part is therefore built: about 30 lines and one floor test, about 20,000 tokens and 2 hours. (best practice) [sources: https://pypi.org/project/pytest-fail-slow, https://github.com/jwodder/pytest-fail-slow, pyproject.toml:229, tests/support/config_write_guard.py:87]
    • Chosen: Build the audit-hook floor. No tool reads the time.sleep audit event, and the need is a sleep budget with a sleep-specific message. The floor already installs PEP 578 hooks (sys.addaudithook in tests/support/config_write_guard.py:87), and the 4666 probe measured the time.sleep audit event.
    • Rejected: Adopt pytest-fail-slow for the duration rule. It measures the whole test duration, so a short sleep inside a long test passes and a slow test that never sleeps fails. The check found no PyPI release after 2024-06-01.
    • Rejected: Reuse pytest-timeout. It fails a test that hangs at the 600 s hang bound (pyproject.toml:233), not one that sleeps 0.3 s.
  • Where does the static ban on time.sleep in src live, and how does the one seam keep its real call?: In the ruff TID251 banned-api table of pyproject.toml, which already holds the socket bans, with time.sleep added and no per-file ignore. The project's own gates refuse a per-file ignore that waives TID251: the lint row requires linter.per_file_ignores to resolve to {}, the construct gate reports such a waiver as an escape, the per-file-ignores ceiling is 0, and the owner's 2026-10-22 decision rules out per-file-ignores for findings. The one real call in src/devops_cli/core/clock.py therefore stays under a # noqa: TID251 comment that names the seam, and the src noqa: TID251 ceiling goes from 0 to 1 with the reason recorded in the change's proposal, as the ceiling guard allows; src/devops_cli/github/rate_limiter.py:240 holds the same shape of one reasoned # noqa: SIM115 in src with a ceiling of 1. (best practice) [sources: pyproject.toml:192, tests/gate/ruff_config.py:99, tests/gate/construct_gate.py:366, tests/invariants/test_architectural_invariants.py:143, tests/invariants/test_architectural_invariants.py:1405, openspec/changes/archive/2026-10-29-1876-ruff-nesting-cap/design.md:18, src/devops_cli/github/rate_limiter.py:240, tests/invariants/test_architectural_invariants.py:127]
    • Chosen: Ruff TID251 with no per-file ignore, and the seam's call under a reasoned # noqa: TID251 with the ceiling at 1. The lint row pins linter.per_file_ignores to {} and the construct gate reports a per-file ignore that waives TID251, so the ban must carry no per-file ignore; the ceiling guard allows a raised ceiling with a recorded reason, and the project already keeps one reasoned ruff noqa in src with a ceiling of 1.
    • Rejected: Ruff TID251 with a per-file ignore for src/devops_cli/core/clock.py. Refused by the project's gates: tests/gate/ruff_config.py:99 requires linter.per_file_ignores to be {}, tests/gate/construct_gate.py:374-377 reports a per-file ignore that waives TID251 as an escape, the per-file-ignores ceiling is 0, and the owner's 2026-10-22 decision rules it out.
    • Rejected: An ast-grep rule under .ast-grep/rules/. Rule-level ignores is outside the allowlist (tests/gate/construct_gate.py:291-292), and the rule cannot name the one allowed module; ruff resolves import time as t and from time import sleep, which the ast-grep pattern does not.
    • Rejected: A scan of the src tree in a devops ci row. Ruff TID251 already covers the need (AGENTS.md rule 3), and the scan is code the project builds and keeps.
  • How does the floor record a real sleep and charge it to one test?: A session-scoped sys.addaudithook records each time.sleep audit event, as the floor already installs PEP 578 hooks for file writes. A function-scoped autouse fixture reads the total before the test and after it and fails the test when the difference passes 0.25 s; it reads the mark through request.node.get_closest_marker, as the floor reads bwrap. The time.sleep audit event exists and the 4666 probe found one stack for every real sleep. (best practice) [sources: tests/conftest.py:641, tests/conftest.py:698, tests/support/config_write_guard.py:87, openspec/changes/archive/2026-11-12-4666-pace-mutations-through-the-clock-seam/proposal.md:29, tests/support/fakes/clock.py:90]
    • Chosen: Audit hook and a per-test window. The hook sees every time.sleep call, also one that a module bound at import, and the shape matches the network and process guards of the floor.
    • Rejected: Monkeypatch time.sleep during the test. A module that bound time.sleep at import keeps the original function, so the patch misses the call.
    • Rejected: Measure the duration of each test. A slow test that sleeps not at all would fail, and the failure would name no sleep.
  • Which mark lets a test sleep for real?: A new pytest mark, real_sleep, declared in the markers table of pyproject.toml and read by the floor through request.node.get_closest_marker. The test declares it and the floor passes the test whatever its real sleep. (best practice) [sources: pyproject.toml:241, tests/conftest.py:703]
    • Chosen: A new mark, real_sleep. The floor must tell an allowed wait from a defect, and it already reads marks by name.
    • Rejected: Reuse the live mark. live names a real local tool binary that a test may start (docs/TESTING.md:73), and a live test needs no real sleep.
  • Which module holds the one allowed time.sleep call in src?: src/devops_cli/core/clock.py: sleep there is the one place in src/devops_cli that supplies the real wait, the target that docs/TESTING.md:153 names. Each direct time.sleep call site in src moves to devops_cli.core.clock.sleep, and the seam keeps the one real call under the recorded # noqa: TID251. (best practice) [sources: src/devops_cli/core/clock.py:8, docs/TESTING.md:153, docs/adr/0007-tests-double-only-process-edges.md:17]
    • Chosen: Route each call site through devops_cli.core.clock.sleep. The module is the one seam for waiting, and the one recorded waiver names it.
    • Rejected: Keep the calls and add a waiver for each site. A waiver for each site hides a new direct call in those files, and the per-file-ignore route is refused outright.
  • Which tests fail the 0.25 s guard, and how does the change repair them?: Two tests of tests/commands/test_pr_update.py on origin/main 2bd87bc: test_pr_edit_fallback_patch_title_body_base_milestone (line 512) and test_pr_edit_fallback_milestone_failure_fails_with_message (line 598). They request only cli and process_fake, so the write interval of the rate limiter calls the real time.sleep; the reply measured 3.12 s and 3.09 s, and 0.43 s and 0.28 s with ClockFake. Each takes clock: ClockFake, or moves to gh_edge, which requests clock. The change to the file is small, and if refactor(pr): devops pr update acts only on a failed check #4674 deletes devops pr update first, the two tests are gone and the change leaves them alone. (best practice) [sources: tests/commands/test_pr_update.py:512, tests/commands/test_pr_update.py:598, tests/support/gh_process.py:35, tests/support/fakes/clock.py:80, openspec/changes/archive/2026-11-12-4666-pace-mutations-through-the-clock-seam/design.md:13, openspec/changes/archive/2026-11-12-4670-push-gate/proposal.md:13]
    • Chosen: Add clock: ClockFake to the two tests. The reply measured the real sleep and the fake-clock call times, and the clock fixture replaces both the seam and time.sleep.
    • Rejected: Move the two tests to gh_edge. gh_edge requests clock and works, but the two tests script gh writes of their own, so the reply gives this as the second fix.
    • Rejected: Declare real_sleep on the two tests. Their sleep is the rate limiter's write interval, a wait the fake clock can answer, so a mark would hide a real defect and keep 6 s of real sleep.
  • Which tests keep a real sleep, and why?: The tests that wait for a real child process. The signal tests of tests/commands/test_ci.py poll the child through wait_while_running, which waits POLL_INTERVAL_SECONDS (0.01 s) between reads; the reply recorded 13.0 s in 1,297 such sleeps. A fake clock cannot serve a wait on a real process, and docs/TESTING.md:160 keeps that wait unbounded, so those tests declare the real_sleep mark. (best practice) [sources: tests/support/waits.py:35, tests/support/waits.py:60, tests/commands/test_ci.py:2690, docs/TESTING.md:160, docs/adr/0007-tests-double-only-process-edges.md:30]
    • Chosen: Declare the real_sleep mark on the tests that wait for a real process. The wait on the real process is the point of the test, and the mark records that decision.
    • Rejected: Route the wait helper through devops_cli.core.clock.sleep. A wait on a real child process reads the wall clock, and a fake clock would return at once with no wait.
    • Rejected: Make the poll interval of wait_while_running longer. It changes how fast a passing test sees its event and gives no sleep budget; the guard still charges the poll sleeps.
  • Does this item still wait on fix(test): bootstrap-k8s test waits 45 s for the Docker poll #4466, fix(test): refine failure tests wait 60 s on real retries #4459 and fix(github): pace mutations through the clock seam #4666?: No. Each is delivered: their OpenSpec changes are archived (2026-11-11-4466-bootstrap-k8s-test-waits-45-s, 2026-11-11-4459-refine-failure-tests-wait-60-s and 2026-11-12-4666-pace-mutations-through-the-clock-seam), and the reply cites the merged PR fix(github): pace mutations through the clock seam #5063 of fix(github): pace mutations through the clock seam #4666, which already routes the rate limiter's waits through the seam. So dependencies is empty. [sources: openspec/changes/archive/2026-11-11-4466-bootstrap-k8s-test-waits-45-s/proposal.md, openspec/changes/archive/2026-11-11-4459-refine-failure-tests-wait-60-s/proposal.md, openspec/changes/archive/2026-11-12-4666-pace-mutations-through-the-clock-seam/proposal.md, openspec/changes/archive/2026-11-12-4666-pace-mutations-through-the-clock-seam/tasks.md]
  • How many direct time.sleep call sites does src hold outside the seam?: 20, not the 23 of the issue. A scan of the tree for time.sleep( lists 20 call sites under src outside src/devops_cli/core/clock.py, for example src/devops_cli/ai/chaos/injector.py:129, src/devops_cli/commands/pr.py:1824 and src/devops_cli/watchers/live_resource.py:45; the 21st hit in src is the seam itself, src/devops_cli/core/clock.py:9. [sources: src/devops_cli/ai/chaos/injector.py:129, src/devops_cli/ai/client/structured.py:251, src/devops_cli/commands/pr.py:1824, src/devops_cli/watchers/live_resource.py:45, src/devops_cli/core/clock.py:9]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/tests
    status/new
    value/medium
    effort/medium
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 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/tests. The (tests) scope of the title gives the scope.
    • Priority: P2-Medium. It saves 177 s of CI time and prevents regression, but does not block a release.
    • Value: Medium. It stops 177 s of wasted test time and prevents new real sleeps from entering the suite.
    • Effort: Medium. It needs a ruff config change, 23 call-site moves, and about 30 lines of audit hook code.
    • Related: fix(github): pace mutations through the clock seam #4666, which asks for a different change. Both eliminate real sleep in tests, but #4666 routes one call site through the clock seam while the candidate adds a general ruff rule and an audit hook guard.
  3. added
    priority/p2-medium
    on Nov 11, 2026
  4. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    Two tests still sleep for real after #5063

    PR #5063 (for #4666) sends the waits of the rate limiter through devops_cli.core.clock.sleep. A test that requests the clock fixture, or gh_edge, which requests clock, does not wait for real.

    Two tests in tests/commands/test_pr_update.py request only cli and process_fake. They script gh writes (gh pr edit and two gh api --method PATCH calls). Thus the write interval of the rate limiter calls time.sleep for real.

    Test (line on origin/main 2bd87bc) Call time now Call time with clock: ClockFake
    test_pr_edit_fallback_patch_title_body_base_milestone (512) 3.12 s 0.43 s
    test_pr_edit_fallback_milestone_failure_fails_with_message (598) 3.09 s 0.28 s

    Command: uv run pytest -p no:cacheprovider --durations=0 --no-cov -n0 <the two node IDs>, on a worktree at origin/main 2bd87bc.

    The 0.25 s guard of this issue will fail these two tests. The fix: add clock: ClockFake to the parameters of the two tests, or move them to gh_edge. I measured the first fix in the table above. If the deletion of devops pr update removes these two tests first, do not change them.

    This gap is the open part of the first acceptance criterion of #4666: "An audit hook records 0 real sleep in tests/commands/test_pr_*.py".

  5. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Alternatives check: partly

    Reason: The searches found pytest-fail-slow, an open, maintained pytest plugin that fails any test whose wall-clock duration exceeds a configured threshold, so it would fail the 1 s sleep probe and can allow named slow tests through markers. It measures whole-test duration rather than real sleep, so it does not give the 0.25 s sleep budget, a sleep-specific message, or the static ban on time.sleep (which ruff TID251 already provides), and no tool that fails a test on the standard-library sleep audit event was found.

    Tool: pytest-fail-slow (https://pypi.org/project/pytest-fail-slow)

    • License: MIT License
    • Last activity: 2024-06-01
    • Maintained: no

    Covers:

    • Fails a test that runs longer than a configured duration, so a test that really sleeps 1 s fails
    • Per-test, per-module and per-package markers let a known-slow test be allowed, like the issue's mark allowance

    Lacks:

    • Measures total test duration, not accumulated real sleep, so sleeps under the threshold or inside a longer test pass
    • No sleep-specific failure message that names a real sleep
    • No static ban on time.sleep in src (ruff TID251 already does that part)
    • No audit-event hook, and no help routing call sites through devops_cli.core.clock.sleep

    Tools considered:

    Searches run:

    • ruff TID251 banned-api time.sleep configuration, results: 5
    • pytest plugin detect real time.sleep calls in tests, results: 5
    • pytest-sleep plugin measure sleep duration tests, results: 5
    • pytest plugin fail test that calls time.sleep audit hook, results: 5
    • "pytest-sleep" PyPI, results: 5
    • pytest-fail-slow fail tests that take too long, results: 5
    • python plugin warns when tests call time.sleep real sleep detection, results: 5
    • pytest plugin forbid time.sleep no real sleep tests github, results: 5
    • sys.addaudithook time.sleep pytest plugin fail test, results: 5
    • pytest-fail-slow documentation fail_slow marker duration, results: 5

    Sources:

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

  6. added
    status/ready
    and removed
    status/new
    on Nov 14, 2026
  7. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

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

  8. added
    status/in-progress
    and removed
    status/ready
    on Nov 18, 2026
  9. added 5 commits that reference this issue on Nov 18, 2026
    881fac4
    0624dca
    5ffb700
    d5c6c1c
    32b85b6
  10. added
    status/in-review
    and removed
    status/in-progress
    on Nov 18, 2026
  11. removed
    status/in-review
    on Nov 18, 2026
  12. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor
  13. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-18 20:24 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/p2-mediumscope/testssource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions