Skip to content

refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320

Closed

Description

@dan-petty

Problem

  • No test controls time. 84 modules read the wall clock and 59 read time.monotonic or time.perf_counter; 29 time.sleep calls sit in 20 modules, and 3 modules wait with asyncio.sleep (ai/pydantic_ai_bridge.py, ai/review/pool.py:58, server/routes/stream.py) [measured: grep at 5c45b1e]. A clock is injectable only in the roadmap store (clock=, roadmap/memory_store.py:176), the roadmap service (server/service.py:70, :232, :406) and the now= parameters of a few functions, and the only clock double is SteppedClock inside one test file [code: tests/test_roadmap_store_contract.py:44]. Tests patch time.sleep on about 35 lines in 15 files [measured: grep at 5c45b1e]; no clock library is a dev dependency [code: pyproject.toml:69-83].
  • The libraries take time from their own defaults, and devops-cli passes none in.
    • pydantic-ai's tenacity transports wait with tenacity's default sleep, time.sleep or asyncio.sleep [sourced: tenacity nap.py, tenacity/asyncio], because the RetryConfig that create_retry_config builds sets no sleep [code: ai/retries/__init__.py:40, :62-67], although RetryConfig takes one [sourced: pydantic-ai 2.35.0 retries.py, RetryConfig.sleep]. create_retry_transport (:108) and create_async_retry_transport (:140) use that config. Only a stdlib patch skips those waits today.
    • The GitHub rate limiter sleeps directly [code: github/rate_limiter.py:1004, :1821].
    • ratelimit's limits(calls=60, period=60) is built at import [code: github/rate_limiter.py:64] and wraps _burst_protected_subprocess (:71-73). Its clock defaults to the time.monotonic that ratelimit bound at its own import [sourced: ratelimit 2.2.1 decorators.py:11, :17], which neither time-machine nor a later patch of time.monotonic reaches, and @sleep_and_retry waits with time.sleep. fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintained ratelimit package is removed #1118 removes the ratelimit package and this decorator.
    • AsyncLimiter(max_rate=60, time_period=60) [code: github/rate_limiter.py:773] takes no sleep or clock: aiolimiter 1.3.0 waits on the running event loop's clock (loop.time(), loop.call_at) [sourced: aiolimiter 1.3.0 leakybucket.py:74, :111, :183]. Its only user, run_gh_async (:1976), has no caller in src or tests [measured: grep at 5c45b1e], and refactor(github): the github session owns transport, ledger, error classes and link pagination #983 deletes it.
  • freezegun does not fit. It is unmaintained, and its frozen monotonic clock hangs asyncio and tenacity. time-machine 3.5.1 fakes wall-clock reads and leaves time.monotonic real, so event loops keep running [sourced: time-machine 3.5.1 source] (Q30).

Acceptance criteria

Dependency

  • time-machine==3.5.1 is in the dev dependency group; freezegun is not added.

Seam

  • One place in src supplies the waiting functions the libraries take: a sync sleep and an async sleep, which are time.sleep and asyncio.sleep in production. It holds those functions only: no Clock type and no time arithmetic (Q30). Wall-clock reads stay plain stdlib calls.
  • The sync and async retry transports (ai/retries/__init__.py:108, :140) set RetryConfig's sleep to the seam's sync and async sleep, so tenacity waits through the seam.
  • If ratelimit is still a dependency when this item lands, the burst limit is built when the GitHub rate limiter is constructed, not at import, with ratelimit.limits(clock=...) taking a monotonic reading that the seam supplies for that use alone. If fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintained ratelimit package is removed #1118 has landed first, ratelimit and _BURST_LIMIT_DECORATOR are gone and there is nothing to inject.
  • AsyncLimiter and run_gh_async (github/rate_limiter.py:773, :1976) get no seam: no public behaviour reaches them (Q22), and refactor(github): the github session owns transport, ledger, error classes and link pagination #983 deletes them.
  • Direct time.sleep calls stay as they are; tests reach them through the stdlib fallback below. Code that waits with asyncio.sleep itself (the 3 modules above) calls the seam's async sleep once its package item routes it.

Fake and fixture

  • tests/support/fakes/clock.py provides a fixture that starts time-machine travel without ticking and supplies the seam's waits. Each sync or async wait shifts time-machine's wall clock (and the monotonic reading, while the seam supplies one) by the requested amount, returns at once and is recorded, so tests compare the recorded waits as one value ([1.0, 2.0]).
  • As the stdlib fallback for direct callers (Q24), the fixture replaces time.sleep for the test with the same recorded wait. It never replaces asyncio.sleep. The fixture is registered once from the root conftest.

Contract suite

  • One suite in the seam's mirror directory runs against the fake and the real seam, the real side with waits of 10-50 ms. Cases: after a wait of d, sync and async, the wall clock has advanced by at least d; a tenacity retry with fixed waits waits those amounts in order, sync and async; and, while ratelimit remains, a ratelimit.limits(calls=2, period=p) built with the seam's clock makes the third call wait until the period ends. Each case runs under 1 s.

Migration

Key questions

Strategic alignment

Activity

  1. added
    type/refactor
    scope/ai
    scope/cli
    scope/github
    priority/p2-medium
    on Oct 7, 2026
  2. marked this as blocking 13 issues on Oct 7, 2026
  3. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It improves test reliability and developer productivity but does not fix a user-facing defect or block a release.
    • Value: Medium. It removes flaky time-dependent tests and makes retry/rate-limit paths verifiable, benefiting the project's maintainers rather than end users directly.
    • Effort: High. It requires adding a dev dependency, creating a sleep seam, wiring it into retry transports and rate limiters, building a time-machine fixture, and updating roughly 35 test lines across 15 files.
  4. dan-petty commented on Oct 19, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-19): two elapsed-time asserts on monotonic clocks, found while fixing #1664

    #1664's work on the thread waits found two tests that assert on elapsed time read from a clock time-machine does not fake. time-machine fakes wall-clock reads; time.monotonic and time.perf_counter stay real. So this item's plan as written leaves both tests timing real seconds. Both passed in #1664's loops under load, but each passes or fails on how busy the host is, which docs/TESTING.md (#1308) rules out: no test asserts on elapsed wall-clock time.

    At release/v0.2.37 83939e6:

    • tests/test_ai_request_priority.py:126, test_as_available_non_blocking_when_full: assert elapsed < 0.2, timed with time.monotonic() around an AS_AVAILABLE acquire_ollama_slot that must refuse at once when the cluster is full.
    • tests/test_server_service.py:1141, test_a_drain_during_a_pause_stops_the_workers_without_running_the_held_rounds: drained < 1.0, timed with time.perf_counter() around drain_and_stop(), to show the drain does not wait out its 120 s timeout for rounds a pause holds.

    Proposed addition to the remedy

    Replace each elapsed-time assert with a structural check of the same behaviour, as #1691 did for the startup import budget:

    • The AS_AVAILABLE refusal: assert that the slot acquisition never blocks, by recording the blocking flag or timeout the slot's wait receives. Do not time it.
    • The drain: assert that the drain finished without reaching its timeout path, through the drain's own outcome or log line. Alternatively, inject the drain timeout through the service's existing seam, and assert that the workers stopped and the held rounds never ran.

    Acceptance: neither test reads time.monotonic or time.perf_counter, and each fails when its behaviour breaks (a blocking acquisition, or a drain that waits for held rounds).

  5. added this to the v0.2.38 milestone on Oct 20, 2026
  6. dan-petty commented on Oct 21, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1772, merged into release/v0.2.38 as 838ad6e.

    Closes #1320

    Fail-to-pass: not applicable: refactor adds time-machine dev dependency and clock fake/seam imported by the changed tests

    14 file(s) changed.

    How it was verified

    Check runs at b48a467:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • devops-cli: pass
    • zizmor: pass
    • bandit: pass
    • CodeQL: pass
    • Analyze (actions): pass
    • Fail-to-Pass Check: pass
    • Analyze (python): pass
    • Fail-to-Pass Self-Check: pass
    • Rule Pack Tests: pass
    • Changed-Test Independence: pass
    • Tests & Coverage: pass
    • Static Analysis: pass
    • Dependency Freshness: skipping
    • Service Image: skipping
    • DevContainer Image: skipping

    Acceptance Criteria of docs/agent/tasks/task-1320-clock-fake-and-wait-seam.md:

    No Acceptance Criteria section.

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

    priority/p2-mediumscope/aiscope/cliscope/githubtype/refactor

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions