Skip to content

perf(ci): the structural-invariants hook runs the stdlib metric once per commit, with no uv sync #1667

Closed

Description

@dan-petty

Friction

  • The structural-invariants pre-commit hook is a sub-second AST walk, but its entry is uv run, so every run first syncs the venv: a bytecode pass over 17,910 files and an OSV malware query for all locked packages.
  • The hook is not require_serial, so pre-commit splits the staged files into up to 16 batches, and each batch pays its own sync. The batches queue on the venv lock.
  • When OSV is slow or down, this hook fails the commit even though it needs no packages: about 8-10 s per refused sync, about 50 s when the connection hangs.

Evidence

Checked at release/v0.2.37 30b8fdd.

  • Entry uv run python3 -m devops_cli.core.code_metrics with no require_serial (.pre-commit-config.yaml:54-60).
  • src/devops_cli/core/code_metrics.py imports only the standard library (:15-22), and python3 -P src/devops_cli/core/code_metrics.py gives the same output and exit code, including on a violating file.
  • On one file, 3 interleaved runs at load 15-22: uv run python3 -m ... 2.80 / 1.43 / 1.59 s and about 4 CPU-s; python3 -P <path> 0.21 / 0.10 / 0.05 s and 0.1 CPU-s.
  • With 20 staged files at load 43-59, the hook took 17.2 s and 25.3 CPU-s (5 syncs) against 7.4 s and 2.3 CPU-s with the stdlib entry. With 64 files at load around 100 (3 runs), 101 s against 25 s with require_serial: true alone and 30 s with one sync and --no-sync batches; the stdlib entry was not timed at 64 files.
  • A test pins the entry string (tests/test_code_metrics.py:286-299), and the hook comment (.pre-commit-config.yaml:47-53) says the package inits load config and typer.
  • /usr/bin/python3 is 3.14 in the devcontainer, which the sources need (they use PEP 758 except A, B:). No GitHub workflow runs pre-commit.

Remedy

  • Set the entry to python3 -P src/devops_cli/core/code_metrics.py and add require_serial: true; keep args: [--max-depth, "5"]. If hosts outside the devcontainer matter, use uv run --no-sync python -P src/devops_cli/core/code_metrics.py instead, which keeps the project interpreter without a sync.
  • Update the pinned test and the hook comment in the same change.
  • The devops ci test hook keeps its synced uv run, so each commit still runs one malware-checked sync. No check or cap changes.

Acceptance criteria

  • Tests first, offline: the pinned-entry test expects the new entry, require_serial: true and the same args.
  • A test runs the new entry on a file that breaks the depth cap and on one that does not, in a tmp_path copy, and checks the exit codes and output match the module run.
  • A commit staging 20 Python files runs the hook in one process (pre-commit --verbose shows one run).

Proposed design

Problem statement

The structural-invariants pre-commit hook uses uv run to execute a Python script that only requires the standard library. This causes unnecessary overhead as uv run synchronizes the virtual environment on every invocation, including performing bytecode passes over thousands of files and OSV malware queries for all locked packages. The hook is not marked as require_serial, so pre-commit splits staged files into batches, each paying for its own sync, which leads to long commit times and failures when OSV is slow or down.

Acceptance criteria

  • Criterion 1: The test that pins the entry string expects the new entry with require_serial: true and the same arguments.
    Verification: Run the test test_the_commit_hook_runs_this_module_at_the_default_cap() and verify it passes with updated entry string, require_serial: true, and the same args.
  • Criterion 2: A test verifies that the new entry correctly handles files that breach or do not breach the depth cap.
    Verification: Create a test that runs the new entry on a file that breaks the depth cap and one that does not, in a tmp_path copy, and checks the exit codes and output match the module run.
  • Criterion 3: A commit staging 20 Python files runs the hook in one process.
    Verification: Stage 20 Python files and run pre-commit with verbose mode; observe that only one run of the hook is executed, confirming require_serial: true works as intended.

Key questions

  • What is the current entry point for the structural-invariants hook?: The current entry point is uv run python3 -m devops_cli.core.code_metrics. [sources: .pre-commit-config.yaml:54-60]
  • Does the code_metrics.py module only use the standard library?: Yes, according to the documentation in src/devops_cli/core/code_metrics.py, it imports only the standard library and does not import devops_cli. [sources: src/devops_cli/core/code_metrics.py:15-22]
  • Should the fix use python3 -P or uv run --no-sync python -P?: If hosts outside the devcontainer matter, use uv run --no-sync python -P src/devops_cli/core/code_metrics.py to keep the project interpreter without a sync. (proposed)
  • What should be updated in addition to the hook entry?: The pinned test and the hook comment in .pre-commit-config.yaml should be updated in the same change. (proposed)

Open questions

  • What performance difference was observed between uv run and direct Python invocation? (missing verified source)
  • How does the performance degrade with more staged files? (missing verified source)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/infra
    on Oct 18, 2026
  2. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    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.

    • Type: type/infra. The candidate improves CI infrastructure by optimizing a pre-commit hook.
    • Priority: P2-Medium. Improves developer experience and CI performance but is not critical.
    • Value: Medium. Reduces commit time and network usage for developers, improving workflow efficiency.
    • Effort: Low. Involves changing the hook entry and adding a flag, with minimal code changes.
  3. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-18): folded here by intake under the agent filing quota

    The cycle's agent filing allowance (#1153) was used, so devops roadmap intake filed nothing and named this item as the nearest. The candidate below is related work that can land with this item or be filed on its own in a later cycle. Candidate title: "fix(ci): devops ci test reads the test tree once per run and shows which staged sources no test covers".

    Friction

    • The pre-commit hook devops ci test <staged paths> re-reads all 478 test modules (about 8 MB) once for every staged source file, about 0.4 CPU-s each.
    • When at least one staged file maps to a test, every staged source that maps to nothing is dropped from the run. The warning about it is hidden on a passing commit, because pre-commit shows hook output only on failure. The hook comment and the --fallback help say the opposite: that unmapped sources fall back to the full suite.
    • About 31% of Python commits since 2026-09-01 staged an unmapped module next to a mapped one, mostly src/devops_cli/lang/en/help.py.

    Evidence

    Checked at release/v0.2.37 30b8fdd.

    • _map_src_tree_path calls select_tests_for_sources([path]) per path, and each call reads every test file again (src/devops_cli/core/test_selection.py:177-180).
    • Unmapped sources are dropped when targets exist (src/devops_cli/commands/ci.py:1373-1374); pre-commit hides the output of a passing hook (pre_commit/commands/run.py:217); the hook comment (.pre-commit-config.yaml:62-64) and the help text (src/devops_cli/lang/en/help.py:628-631) promise a fallback.
    • ci.py maps paths with its own code (_resolve_test_targets, :1334-1380), not the sound policy the coverage index module already has: select_from_index and needs_full_run (src/devops_cli/core/coverage_index.py:191, :324) are not called from ci.py.
    • With no index (any fresh worktree), text matching on hub modules such as config/constants.py, config/defaults.py and lang/en/messages.py selects about 90 of 478 test modules. Hook-like runs had a median of 66 s and a p90 of 186 s.
    • Related: feat(cli): devops ci test selects tests from a coverage index built on demand by devops ci coverage --build-index #406 built the index on demand; fix(ci): coverage index treats module-level and import-time changes as unindexed #884 made module-level changes unindexed. Neither covers the points above.

    Remedy

    • Read the test tree once per invocation and map every staged source against it.
    • Make the hook honest at no speed cost: reword the hook comment, the --fallback help and MESSAGES.ci.no_covering_tests to say the narrowed run is advisory for unmapped sources and the pre-push devops ci is the gate; set verbose: true on the devops-ci-test-changed hook so the unmapped list is shown.
    • Follow-up, measured before it lands: route selection through select_from_index and needs_full_run, and delete the path that loses. Building the index as a by-product of the pre-push test row must not lengthen the push; measure its cost first.
    • Do not make every unmapped source trigger the full suite with text selection alone: about 31% of Python commits would then run the full suite at commit time.

    Acceptance criteria

    • Tests first, offline: a test counts test-file reads in one devops ci test call with three staged sources and expects one pass over the tree.
    • A test for a commit staging one mapped and one unmapped source shows the unmapped source named in the output.
    • The hook comment, help text and message say the same thing; devops docs check passes.
  4. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-18): folded here by intake under the agent filing quota

    The cycle's agent filing allowance (#1153) was used, so devops roadmap intake filed nothing and named this item as the nearest. The candidate below is related work that can land with this item or be filed on its own in a later cycle. Candidate title: "fix(ci): devops ci's uv children keep the malware check and the project uv cache, and leave the one sync to the hook".

    Friction

    • devops ci strips every UV_* variable from the uv commands it starts. Its child syncs therefore run no malware check and use ~/.cache/uv instead of the project cache.
    • Through the hooks this is hidden, because the hook's own uv run devops ci already ran one checked sync. A bare devops ci (the .venv/bin/devops on PATH) or a run in a new worktree installs packages through the children with no malware check.
    • Each push also starts 11 child syncs that queue on the venv lock; the 3 serial pre-fix syncs sit on the critical path.

    Evidence

    Checked at release/v0.2.37 30b8fdd.

    • The allowlist in src/devops_cli/core/process.py:45-102 has no UV_ entry, and ci.py passes no extra_allowed_env (src/devops_cli/commands/ci.py:217, :350, :1875).
    • Only UV_MALWARE_CHECK turns the check on. The --preview-features malware-check,check-command that ci.py prepends (:189-190, :215-216, :345-346) only silences the preview warning and repeats pyproject.toml:231-232.
    • install_tools.py:200 already passes a _UV_ENV set through for its own uv calls.
    • Passing the check through alone would add about 1.2 s of OSV per child sync, so it must ship with --no-sync on the children.
    • A checked sync inside devops ci would repeat the hook's OSV query: uv run true took a median 1.48 s against 0.57 s with UV_MALWARE_CHECK unset, 3 interleaved runs at load 13-23. That is about 0.9 s per push, more than the child syncs cost.
    • Measured cost of the child syncs on the critical path: about 1.4 s per cache-miss push at load 85-100, likely 0.5 s or less at normal load.

    Remedy

    • Move _UV_ENV into src/devops_cli/config/constants.py (for example CONST_UV_PASSTHROUGH_ENV = frozenset({"UV_MALWARE_CHECK", "UV_CACHE_DIR", "UV_LINK_MODE"})) and pass it as extra_allowed_env from every ci.py uv call site. The deny patterns still apply. UV_COMPILE_BYTECODE stays out.
    • Add --no-sync right after the run and check subcommands at the three injection points (:189, :215, :345; _capture at :1872 goes through :189), so no child syncs.
    • The hook's own uv run devops ci stays the one malware-checked sync. A bare devops ci then installs nothing through its children, which closes the unchecked-install gap.
    • Add no uv sync inside devops ci: under the hook it would repeat the OSV query and cancel the saving. A guard for bare runs is the owner's call, at one OSV query per push.
    • Drop the redundant --preview-features injection.
    • No check, threshold or lock check changes; uv lock --check stays.

    Acceptance criteria

    • Tests first, offline: a test of the built child environment shows UV_MALWARE_CHECK and UV_CACHE_DIR kept and every variable matched by the deny patterns (src/devops_cli/core/process.py:104-112) still dropped.
    • A test of the check specs shows every child uv run and uv check carries --no-sync.
    • A dry run lists no child sync.
  5. dan-petty commented on Oct 19, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-19): devops test run --changed needs a base other than main

    Found in #1312 PR 2's review.

    The follow-up, which fits this item's commit-time selection work:

    • devops test run --base/-b, defaulting to the merge base with the branch's upstream (@{upstream}), else the active release branch, else CONST_GIT_MAIN_BRANCH;
    • the ref resolved with git.operations.resolve_merge_base and validated with _is_safe_revision;
    • the existing mutate_base_missing and mutate_base_refused errors when the ref is absent or refused, instead of ignoring git diff's exit status.

    Test it with a temporary repository whose topic branch sits on a release branch: --changed lists only the topic's own changes.

  6. dan-petty commented on Oct 19, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-19): the covering-test selector matches imports in text, including string literals

    #1312 PR 2's selection review found that select_tests_for_sources decides "imports it" with a regex over each test file's text.

    • It matches docstrings and string literals. For example, tests/test_test_runner.py (16 IDs) is selected for any devops_cli/ai/review/* or devops_cli/ai/__init__.py change, because of the literals at its lines 22-24 and 84-86.
    • It finds direct imports only, so a test reaching a module through a helper is missed.

    This item already reads the test tree once per run. The same pass can parse each test module with ast and build an import graph:

    • one parse per run;
    • real imports only;
    • transitive through test helpers;
    • no string-literal false positives.

    Measured on the last 40 commits: selection takes a median of about 1 s, and 10-22 s for release-size diffs, almost all of it the per-source regex over all 480 test files.

  7. added this to the v0.2.38 milestone on Oct 20, 2026
  8. dan-petty commented on Oct 20, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1760, merged into release/v0.2.38 as dbdb009.

    Resolves #1667

    5 file(s) changed.

    How it was verified

    Check runs at 415d613:

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

    Acceptance Criteria of docs/agent/tasks/task-1667-structural-invariants-hook-one-pass.md:

    No Acceptance Criteria section.

  9. dan-petty commented on Oct 20, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1760, merged into release/v0.2.38 as dbdb009.

    Resolves #1667

    5 file(s) changed.

    How it was verified

    Check runs at 415d613:

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

    Acceptance Criteria of docs/agent/tasks/task-1667-structural-invariants-hook-one-pass.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

    source/agenttype/infra

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions