Skip to content

fix(scripts): check_test_timing counts a sleep on a module loaded by name and scans the tests tree faster (#1110, #1177) - #1202

Merged
napetrov merged 4 commits into
intel:mainfrom
antonwolfy:test/timing-checker
Nov 26, 2026
Merged

napetrov merged 4 commits into
intel:mainfrom
antonwolfy:test/timing-checker

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

#1110: scripts/check_test_timing.py followed only import statements, so a sleep reached through __import__ or importlib was invisible to it. On main, scan('__import__("time").sleep(1)\n', "x.py") returns ({}, []), python scripts/check_test_timing.py prints test timing ratchet ok: 0 sleep/Timer call(s) in 0 file(s), and tests/test_container_agent.py:1106 still polls the egress proxy's log with __import__("time").sleep(0.05) and has no baseline entry. The checker now counts it, and that test waits with poll_until.

#1177: test_the_repo_tree_holds_its_own_baseline scans all 637 files of tests/ and took 5.5 s on an idle host. The cause is the time: no test writes into the real tests/ directory (every writer found uses tmp_path), so the scan is not racing a file that appears or goes away. Pinned to one CPU with two copies of the test running and --timeout 10, it fails on main (1 failed in 10.38 s, both copies). The scan now parses every file but walks and tokenizes only the ones that can hold a call, and the test takes 3.7 s; two copies on one CPU pass with --timeout 10 (7.7 s).

The timeout stays at 60 s. The issue lists no fix options, so the one taken is the cheaper scan, since a test of the whole tree has to scan it. A count does not suit this test: the 60 s is the suite's pytest-timeout (pyproject.toml), not a bound the test asserts, and the test's work is fixed (parse every file of tests/ and compare the result with the baseline), so there is no smaller amount of work to count instead; only the time per file can fall.

Closes #1110

Closes #1177

Changes

  • scripts/check_test_timing.py: a call on __import__("<module>") (the builtin, not rebound by an assignment, a parameter or a def) or on importlib.import_module("<module>") / import_module("<module>") (resolved through the file's imports) counts as module.attr when the module and attribute are targets. The argument must be one string literal; a computed name is not followed. # timing-ok: <reason> waives it. The module docstring lists the new forms. _resolve keeps its complexity cap by moving the loader check into _loaded.
  • tests/test_container_agent.py: test_proxy_logs_plain_http_failures_with_host_and_normalised_names waits with poll_until(lambda: len(proxy.log) >= 4, 2.5, interval=0.05). It was the only such call in tests/, so test_timing_baseline.json does not change; the file's entry in scripts/growth_baseline.json shrinks by its two lines.
  • tests/test_check_test_timing.py: a parametrized test for the loaded-by-name forms (counted, waived, non-literal, other module, shadowed __import__ and import_module).
  • scripts/check_test_timing.py (Flaky on CI: tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline #1177): scan parses every file, so a syntax error is still reported, and returns at once for an ASCII file with no sleep, Timer or timing-ok in it. A non-ASCII file is scanned in full, because Python folds identifiers to NFKC (time.sleep(1) is time.sleep(1)).
  • scripts/check_test_timing.py, tests/test_check_test_timing.py: the module docstring says a module bound by assignment (mod = __import__("time")), a keyword name (import_module(name="time")) and a load with a second argument (__import__("time", globals())) are not followed, and tests pin each, with from importlib import *.
  • tests/test_check_test_timing.py (Flaky on CI: tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline #1177): the shortcut gives the result of the full scan (count) for files with and without a marker, does not walk a file without one, and still raises SyntaxError for it.
  • No changelog fragment: only scripts/ and tests/ change.

Risk

The checker counts more: a __import__("time").sleep or importlib.import_module("time").sleep added to tests/ now fails the ratchet unless waived. None exists in the tree today (python scripts/check_test_timing.py exits 0 with no baseline change). The #1177 shortcut leaves the result of every file as it was; only the time differs. Open PRs #1197, #1185, #1045 and #1011 also edit scripts/growth_baseline.json, and #1011 edits tests/test_container_agent.py: each touches other lines than this PR's (the tests/test_container_agent.py entry here, line 1103 of that file), so they merge without a conflict.

Testing

On Linux, at acab195b (on main 316a3413):

  • make ci-local: 38 passed, 0 failed, 15 skipped (matrix legs of other jobs, micromamba, semgrep, fuzz, deploy). 28440 tests passed, 23 skipped; the diff has no covered lines to measure. The run is on the tree with main merged in, and tests/test_repo_batch_lifecycle.py::test_ctrl_c_starts_no_queued_cell is among the passing tests.
  • check_test_timing: __import__("time").sleep(...) is not counted, so test_container_agent.py still polls with a fixed sleep #1110 on main's code: tests/test_check_test_timing.py fails 9 tests (the 7 loaded-by-name counts and 2 of the shortcut cases), the first on __import__("time").sleep(1) returning ({}, []) instead of ({"time.sleep": 1}, []); the file has 172 tests, 163 pass. Here all pass.
  • Flaky on CI: tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline #1177 on main's code: two copies of test_the_repo_tree_holds_its_own_baseline on one CPU with --timeout 10 fail (1 failed in 10.38 s each); four copies fail on this branch too (10.7 s each, 3.7 s of CPU apiece), two pass.
  • Mutants: each of 10 breaks of check_test_timing.py (builtin check, keywords, import_module via from-import, attribute check, counted names, literal-argument check, the ASCII guard, the timing-ok marker, no shortcut, parse after the shortcut) fails a test of this PR or of the file. Two more: reading TARGETS[node.module] for from importlib import * raises KeyError and fails the star-import case; len(load.args) >= 1 counts __import__("time", globals()).sleep(1) and fails its case.
  • Not run: the other Python versions (3.11, 3.12, 3.14).

Open questions

None.

Revert

A plain revert restores the old checker (it ignores a sleep on a module loaded by name) and the fixed-sleep wait in test_container_agent.py. Nothing is stored.

Checklist

  • Tests added/updated and pytest passes
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

…oaded by name (intel#1110)

The checker followed only `import` statements, so `__import__("time").sleep(0.05)` and `importlib.import_module("time").sleep(1)` were invisible to it: `python scripts/check_test_timing.py` reported 0 calls while tests/test_container_agent.py polled a proxy log with `__import__("time").sleep(0.05)`.

A call on `__import__("<module>")` (the builtin, not rebound by an assignment, parameter or `def`) or on `importlib.import_module("<module>")` / `import_module("<module>")` (resolved through the file's imports) now counts as `module.attr` when the module and attribute are in the targets. The argument must be a single string literal; a computed name is not followed, as before. A `# timing-ok: <reason>` waives it like any other call.

test_proxy_logs_plain_http_failures_with_host_and_normalised_names waits for the proxy log with `poll_until` instead of a fixed-sleep loop. It was the only such call in tests/, so the baseline does not grow. Its growth baseline entry shrinks with the file.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…or waiver and does not walk it (intel#1177)

tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline scans all of tests/ (637 files) and took 5.5 s of CPU on an idle host. Pinned to one CPU, with two copies of it running and `--timeout 10`, it fails on main (`1 failed` after 10.38 s), and the 60 s limit is reached on a runner that shares its CPUs among enough workers. No test writes into the real tests directory (every writer found uses `tmp_path`), so the scan is not racing a file that appears or goes away: the cause is the time of the scan.

Most of that time is the scope walk and the tokenizer run over files that cannot hold a call. A call is spelt with `sleep` or `Timer` and a waiver with `timing-ok`, so an ASCII file with none of these words is now parsed, to keep reporting a syntax error, and not walked or tokenized. A file with a non-ASCII character is still scanned in full, since Python folds identifiers to NFKC (`time.sleep(1)` is `time.sleep(1)`). The whole-tree test takes 3.7 s, and two copies on one CPU pass with `--timeout 10`.

tests/test_check_test_timing.py pins that the shortcut gives the same result as the full scan and does not walk a file without a marker.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy
antonwolfy requested a review from a team as a code owner November 25, 2026 11:34
@antonwolfy antonwolfy self-assigned this Nov 25, 2026
@exai-bot

exai-bot commented Nov 25, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 4 selected item(s).

CI at acab195: 20 passing, 4 skipped.

Review details
  • Commit reviewed: acab195 against merge base 316a341
  • Coverage: 4 files in 2 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 124k tokens (54k from cache), 39s
  • Tool calls: 7 (6 file_read, 1 linked_items)
  • Test timing checks, baseline, and tests: 3 files
  • Container agent tests: 1 file

@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.07%. Comparing base (316a341) to head (acab195).
⚠️ Report is 10 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1202      +/-   ##
==========================================
- Coverage   96.07%   96.07%   -0.01%     
==========================================
  Files         496      495       -1     
  Lines       65276    65206      -70     
  Branches    11330    11314      -16     
==========================================
- Hits        62717    62644      -73     
+ Misses       1585     1584       -1     
- Partials      974      978       +4     

see 15 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vchamarthi vchamarthi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antonwolfy, could you answer my 3 questions below?
I'll approve once that's done.

My questions before I approve:

  • mod = __import__("time") followed by mod.sleep(1) is not counted, and neither is t = importlib.import_module("time") followed by t.sleep(1). Decide whether that should count.
  • importlib.import_module(name="time").sleep(1) is not counted, because a keyword argument is not read. State in the module docstring that only a positional literal is followed, or count the keyword form.
  • Issue #1177 says a bound on wall-clock time fails on a busy runner, and that a test which counts the work passes there. If the 60 s bound stays, say in the description why a count does not suit this test, since the description says the issue lists no fix options.

Also holding my approval:

  • A check of the change fails on this head.
Optional, for later (2)
  • dokimi/scripts/check_test_timing.py:201: If I change WATCHED[node.module] on line 201 to TARGETS[node.module], from importlib import * raises KeyError and every changed test still passes.
  • dokimi/scripts/check_test_timing.py:338: If I change == 1 on line 338 to >= 1, __import__("time", globals()).sleep(1) is counted and every changed test still passes.

What I checked on 22afbc5:

  • Not passing: The full test suite fails with main merged in, at tests/test_repo_batch_lifecycle.py::test_ctrl_c_starts_no_queued_cell, ci.yml:test / Run tests.
  • The 265 changed tests pass, and fail on main as expected.
  • It merges cleanly onto main.
  • When I broke the changed code on purpose, the tests caught all 12 breakages.

…he star import of importlib

Settles the review questions on intel#1202. A module bound by `mod = __import__("time")` or `t = importlib.import_module("time")`, a keyword `name=`, and a load with a second argument are not counted: the module docstring now says so, and a test pins each. A test also pins `from importlib import *`, whose `WATCHED[...]` lookup raised KeyError when it read `TARGETS[...]`.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
Signed-off-by: Anton Volkov <antonwolfy@gmail.com>

@napetrov napetrov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change class: LOCAL (head 22afbc5)

  • Contract: none
  • Responsibility: none
  • Dependency: none
  • Review depth: skim: local change, check diff + tests
  • Coverage gaps: none

The direct dynamic-import forms requested by #1110 now count. The scanner parses before taking the ASCII fast path, so it preserves syntax errors and handles Unicode identifiers. The previously reported Ctrl-C lifecycle test passed locally on a temporary merge with current main (1b24ab379). GitHub CI is green. I reviewed the timing checker from source. Its module-binding and positional-literal limits are consistent with the described scope.

@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) @antonwolfy, could you answer my 3 questions below?

@vchamarthi (#1202 (review)): The docstring and tests are in 0f812f4. The three questions, as the branch answers them:

  • mod = __import__("time") then mod.sleep(1), and t = importlib.import_module("time") then t.sleep(1), stay uncounted on purpose: the checker follows imports and a call written on the loader itself, not a name assigned from them (the same rule as nap = time.sleep). The module docstring of scripts/check_test_timing.py says so (lines 12-15) and a test pins the assignment form.
  • importlib.import_module(name="time").sleep(1) stays uncounted too. The docstring states that only one positional literal and no other argument is followed (import_module(name="time") and __import__("time", globals()) are not), and tests pin both.
  • On a count instead of a bound for Flaky on CI: tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline #1177: the description now has a paragraph saying the 60 s is the suite's pytest-timeout, not a bound the test asserts, and that the test's work is fixed (parse every file of tests/ and compare with the baseline), so there is no smaller amount of work to count; the fix is the cheaper scan.

The two optional points are already pinned: the description's mutants list reading TARGETS[node.module] (raises KeyError on from importlib import *, fails the star-import case) and len(load.args) >= 1 (counts __import__("time", globals()).sleep(1), fails its case).

The failing check: make ci-local on the tree with main merged in (acab195) passes, 0 failed, 28440 tests passed, and tests/test_repo_batch_lifecycle.py::test_ctrl_c_starts_no_queued_cell is among them.

@antonwolfy

Copy link
Copy Markdown
Contributor Author

(approved) ### Change class: LOCAL (head 22afbc5)

@napetrov (#1202 (review)): Approval that recaps its checks; it asks for no change, and the branch has no new commit this round.

@napetrov
napetrov merged commit 8f58626 into intel:main Nov 26, 2026
24 checks passed
@antonwolfy
antonwolfy deleted the test/timing-checker branch November 26, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants