Repository navigation
Closed
Labels
priority/p2-mediumPriority P2: standard workscope/aiAI review pipeline, personas, prompts, LLM client, or RAGscope/cliCore CLI framework, Typer subcommands, options, and tablesscope/githubGitHub Projects v2, Views, Milestones, Labels, and PR automationtype/refactorType refactor: a new structure, same behavior
Milestone
Description
Problem
- No test controls time. 84 modules read the wall clock and 59 read
time.monotonicortime.perf_counter; 29time.sleepcalls sit in 20 modules, and 3 modules wait withasyncio.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 thenow=parameters of a few functions, and the only clock double isSteppedClockinside one test file [code: tests/test_roadmap_store_contract.py:44]. Tests patchtime.sleepon 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.sleeporasyncio.sleep[sourced: tenacity nap.py, tenacity/asyncio], because theRetryConfigthatcreate_retry_configbuilds sets nosleep[code:ai/retries/__init__.py:40, :62-67], althoughRetryConfigtakes one [sourced: pydantic-ai 2.35.0 retries.py,RetryConfig.sleep].create_retry_transport(:108) andcreate_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). Itsclockdefaults to thetime.monotonicthat ratelimit bound at its own import [sourced: ratelimit 2.2.1 decorators.py:11, :17], which neither time-machine nor a later patch oftime.monotonicreaches, and@sleep_and_retrywaits withtime.sleep. fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintainedratelimitpackage is removed #1118 removes theratelimitpackage 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 insrcortests[measured: grep at 5c45b1e], and refactor(github): the github session owns transport, ledger, error classes and link pagination #983 deletes it.
- pydantic-ai's tenacity transports wait with tenacity's default sleep,
- 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.monotonicreal, so event loops keep running [sourced: time-machine 3.5.1 source] (Q30).
Acceptance criteria
Dependency
-
time-machine==3.5.1is in thedevdependency group; freezegun is not added.
Seam
- One place in
srcsupplies the waiting functions the libraries take: a sync sleep and an async sleep, which aretime.sleepandasyncio.sleepin 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) setRetryConfig'ssleepto the seam's sync and async sleep, so tenacity waits through the seam. - If
ratelimitis still a dependency when this item lands, the burst limit is built when the GitHub rate limiter is constructed, not at import, withratelimit.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 unmaintainedratelimitpackage is removed #1118 has landed first,ratelimitand_BURST_LIMIT_DECORATORare gone and there is nothing to inject. -
AsyncLimiterandrun_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.sleepcalls stay as they are; tests reach them through the stdlib fallback below. Code that waits withasyncio.sleepitself (the 3 modules above) calls the seam's async sleep once its package item routes it.
Fake and fixture
-
tests/support/fakes/clock.pyprovides 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.sleepfor the test with the same recorded wait. It never replacesasyncio.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
ratelimitremains, aratelimit.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
- tests/test_pydantic_ai_retries.py, whose retry tests sleep for real today (
min_waitat :135, :160, :231), and the tests in tests/test_github_rate_limiter.py that patchtime.sleeportime.time(from :174) or sleep for real (:272 for 1.05 s, :1011) (paths before test(cli): every test file sits in the directory mirroring the package it tests or in tests/invariants/, and the root conftest holds only the hermetic floor #1313's move) use the clock fixture, and neither sleep for real nor patchtime. If fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintainedratelimitpackage is removed #1118 lands first, the stall tests it adds to tests/test_github_rate_limiter.py are included. - The other
time.sleeppatches and real waits are left to their package items. - Tests run offline; each new test runs under 1 s.
- If test(cli): ratchets hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one #1309 has landed, its ceilings drop by the sites this PR removes.
-
changelog.d/<n>.mdrecords the change;CHANGELOG.mdanddocs/ROADMAP.mdare not edited. -
uv run devops cipasses.
Key questions
- Which clock library? Decided 2026-10-07: time-machine for wall-clock reads, and waiting through the libraries' own injection points; no home-grown Clock; freezegun rejected (Q30).
- Q30 names
ratelimit.limits(clock=)and an injected async sleep forAsyncLimiter. Does each still get one? Decided 2026-10-07: waiting goes through the libraries' own injection points (Q30). (proposed) Only where the code survives:AsyncLimitertakes no sleep in aiolimiter 1.3.0, and its only user,run_gh_async, has no caller and leaves with refactor(github): the github session owns transport, ledger, error classes and link pagination #983;ratelimittakes the seam's clock until fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintainedratelimitpackage is removed #1118 removes it. - May a test patch
time.sleep? Decided 2026-10-07: only as the stdlib fallback for direct callers, through the clock fixture (Q24, Q30). - Why not replace
asyncio.sleeptoo? (proposed) Event loops, anyio and libraries call it to yield, so replacing it for a whole test changes their scheduling. Code that waits with it calls the seam's async sleep instead. - Do the existing
clock=parameters stay? Decided 2026-10-07: yes; a constructor or parameter that domain code already takes is the injection Q24 prefers. (proposed) test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334 keeps server/service.py'sclock=and routes the baretime.time()beside it [code: server/service.py:157] through it.SteppedClockmoves totests/support/fakes/clock.pyonly when a second package needs a stepped wall clock; time-machine covers the rest. - Fixture name? (proposed)
clock.
Strategic alignment
- Milestone: none; the roadmap tooling places it. Depends on: test(cli): tests find the repository root and fixtures from any depth, import no other test module and build test data with shared builders, and devops test maps a change to its mirror directory #1312. Blocks: test(github): github/ is tested through its public interface over the gh process and session fakes, with no internal patches or private imports #1321, test(k8s): k8s/ is tested through its public interface over the Kubernetes API, process, HTTP and keyring fakes, with no internal patches or private imports #1322, test(k8s): devops k8s commands are thin adapters tested over edge fakes, and the _K8sModule patch forwarding is gone #1323, test(cli): devops pr, gh and repos are thin adapters tested over edge fakes, and merge readiness is tested in its package #1324, test(ci): devops ci, test, devcontainer, workspace, install-tools, uv, branches and pipeline are tested over edge fakes, with no lazy patch targets or PYTEST_CURRENT_TEST checks #1325, test(cli): the infra commands are thin adapters tested over edge fakes, with no internal patches or private imports #1327, test(review): ai/review and review/ are tested through one public interface over the model, tool-executor and process fakes, with no mock detection in src #1328, test(ai): Wire rag once in ai/rag, test with edge fakes #1329, test(mcp): mcp, agent and tool tests run on edge fakes #1330, test(ai): the remaining ai modules are tested through their public interfaces on edge fakes, and the legacy provider layer leaves src #1331, test(security): scanner, secret and crypto tests run on the executor, process, http and keyring fakes, and scanners stop looking for mocks #1332, test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334, test(infra): infrastructure tests run on edge fakes #1335. Related: refactor(github): the github session owns transport, ledger, error classes and link pagination #983 (one retry policy for the GitHub session; deletes
run_gh_asyncand with it the onlyAsyncLimiter), fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintainedratelimitpackage is removed #1118 (removesratelimitand_BURST_LIMIT_DECORATOR), test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310 (default deny and state resets). - Parent: test(cli): tests double only process edges and mirror the source tree #1307 (phase D).
Metadata
Metadata
Assignees
Labels
priority/p2-mediumPriority P2: standard workscope/aiAI review pipeline, personas, prompts, LLM client, or RAGscope/cliCore CLI framework, Typer subcommands, options, and tablesscope/githubGitHub Projects v2, Views, Milestones, Labels, and PR automationtype/refactorType refactor: a new structure, same behavior
Type
Projects
- StatusShow more project fieldsDone
Activity
dan-petty-bot commentedon Oct 8, 2026
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.
It improves test reliability and developer productivity but does not fix a user-facing defect or block a release.It removes flaky time-dependent tests and makes retry/rate-limit paths verifiable, benefiting the project's maintainers rather than end users directly.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.dan-petty commentedon Oct 19, 2026
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.monotonicandtime.perf_counterstay 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 withtime.monotonic()around an AS_AVAILABLEacquire_ollama_slotthat 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 withtime.perf_counter()arounddrain_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:
Acceptance: neither test reads
time.monotonicortime.perf_counter, and each fails when its behaviour breaks (a blocking acquisition, or a drain that waits for held rounds).refactor(tests): add clock fake backed by time-machine and waiting se…
dan-petty commentedon Oct 21, 2026
What changed
#1772, merged into
release/v0.2.38as 838ad6e.14 file(s) changed.
How it was verified
Check runs at b48a467:
Acceptance Criteria of
docs/agent/tasks/task-1320-clock-fake-and-wait-seam.md:No Acceptance Criteria section.
feat(release): v0.2.38 (#1799)
feat(release): v0.2.38 (#1798)
test(security): the security and crypto tests run on the process, clo…