Skip to content

chore(lint): extract Python pre-commit gate table into tools/lint/precommit_gates.py - #1699

Merged
VLukiantsev merged 3 commits into
mainfrom
chore/extract-precommit-gates
Nov 18, 2026
Merged

VLukiantsev merged 3 commits into
mainfrom
chore/extract-precommit-gates

Conversation

@VLukiantsev

@VLukiantsev VLukiantsev commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Replace the six duplicated Python lint wrapper functions in .githooks/pre-commit with a declarative PYTHON_GATES table and CLI runner in tools/lint/precommit_gates.py, backed by hermetic unit tests in tools/lint/precommit_gates_test.py.

Motivation & Context

.githooks/pre-commit repeated identical XMC_BYPASS_VENV=1 python3 ... error-handling, mode-gating, and failure-reporting blocks across all six standalone Python lint checks. Moving those gates into a typed Python module eliminates copy-paste Bash boilerplate, enables standard unittest + Bazel test coverage, and lays the shared git/execution primitives for the remaining pre-commit migration slices.

Key Changes

  • tools/lint/precommit_gates.py: Add PythonGate, PYTHON_GATES, RunContext, exec_cmd (handling OSError fail-closed with returncode 127 and stderr logging), git_staged_paths (printing git stderr before exiting 1), staged_paths_match, _update_baseline (checking --update-baseline, git diff, and git add return codes), run_python_gate, and main() (--mode fix|check, setting XMC_BYPASS_VENV=1).
  • tools/lint/precommit_gates_test.py & tools/lint/BUILD.bazel: Add precommit_gates_lib and precommit_gates_test covering RunContext.fail, real-subprocess and missing-binary exec_cmd behavior, git diff error stderr propagation (--no-renames), fix-mode AM/D trigger skipping, baseline unchanged/failure/lowering paths, non-zero failure propagation, and main().
  • .githooks/pre-commit: Replace check_legacy_imports, check_examples_coverage, check_matrix_contract, check_agent_doc_paths, check_file_naming, and check_ui_selector_contract with check_python_gates invoking tools/lint/precommit_gates.py --mode "${MODE}".
  • tools/README.md: Document tools/lint/precommit_gates.py under section 7 (tools/lint/).

Assumptions

  • Invoking precommit_gates.py with XMC_BYPASS_VENV=1 and Python 3.11+ standard-library modules allows pre-commit hooks to execute before ./tools/common/venv is provisioned — pre-commit is documented to bypass the Python venv wrapper — failing to stick to stdlib imports would break developers running hooks without an initialized virtualenv.

Verification & Testing

  • Commands Executed: ruff format --check, ruff check, mypy --config-file pyproject.toml, bazel test --test_output=errors //tools/lint:precommit_gates_test, ./.githooks/pre-commit (fix mode on commit), python3 tools/analyze_diff_size.py --base-ref origin/main, and full ./.githooks/pre-commit --check + bazel test //cmd/ui:ui_unit_tests + node --test .github/scripts/*.test.js on top of the stack.
  • Coverage & Test Status: precommit_gates_test and all 6 affected //tools/lint:*_test targets passed on commit (214 prod lines / 234 test lines, threshold_exceeded: false); full stack verification passed on the top branch.

Related Issues / Bugs

N/A

TAG=agy
CONV=ba8411ef-c093-4f45-950a-b3bc1899268c

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request consolidates several individual Python-based pre-commit checks into a single declarative runner script, tools/lint/precommit_gates.py, and updates the pre-commit hook configuration, Bazel build files, and documentation accordingly. It also introduces unit tests for the new runner. The review feedback suggests defining a REPO_ROOT constant and passing it as the working directory (cwd) to all command executions in precommit_gates.py to ensure the script runs reliably when invoked from subdirectories.

Comment thread tools/lint/precommit_gates.py Resolved
Comment thread tools/lint/precommit_gates.py Resolved
Comment thread tools/lint/precommit_gates.py Resolved
@VLukiantsev
VLukiantsev added this pull request to stack #1702 November 16, 2026 11:16
@VLukiantsev
VLukiantsev force-pushed the chore/extract-precommit-gates branch from 87be222 to 5c5fb1a Compare November 16, 2026 11:44
@VLukiantsev

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces precommit_gates.py, a new declarative runner for Python-based pre-commit linting tasks, which consolidates and simplifies the existing logic in the .githooks/pre-commit script. The changes include updated Bazel build rules and comprehensive unit tests for the new runner. The reviewer provided actionable feedback to improve debuggability by ensuring that standard error output is displayed when git commands fail, along with a corresponding update to the unit tests to verify this behavior.

Comment thread tools/lint/precommit_gates.py Resolved
Comment thread tools/lint/precommit_gates_test.py Resolved
@VLukiantsev
VLukiantsev marked this pull request as ready for review November 16, 2026 19:00
@github-actions
github-actions Bot requested a review from AntonTyb November 16, 2026 19:00
@VLukiantsev

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the pre-commit git hook by consolidating several individual Python-based lint checks (such as legacy imports, examples coverage, matrix contract, agent doc paths, file naming, and UI selector contract) into a single declarative runner script, tools/lint/precommit_gates.py. The pre-commit hook now calls this consolidated script instead of running each check individually. Additionally, Bazel build targets and comprehensive unit tests have been added for the new runner, and the tools documentation has been updated. There are no review comments to address, and I have no additional feedback to provide.

…commit_gates.py

- Replace the six duplicated Python lint wrapper functions in `.githooks/pre-commit` with a declarative `PYTHON_GATES` table and CLI runner (`tools/lint/precommit_gates.py --mode fix|check`).
- Add hermetic unit tests in `tools/lint/precommit_gates_test.py` and document the module in `tools/README.md`.

TAG=agy
CONV=ba8411ef-c093-4f45-950a-b3bc1899268c
…EPO_ROOT

Comment links:
- #1699 (comment)
- #1699 (comment)
- #1699 (comment)

TAG=agy
CONV=ba8411ef-c093-4f45-950a-b3bc1899268c
…line failure paths

Comment links:
- #1699 (comment)
- #1699 (comment)

TAG=agy
CONV=ba8411ef-c093-4f45-950a-b3bc1899268c
@VLukiantsev
VLukiantsev force-pushed the chore/extract-precommit-gates branch from 46bb9f2 to 67061d0 Compare November 17, 2026 11:58

@AntonTyb AntonTyb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Multi-Specialist Review Summary (Round 1 · 67061d061d)

  • Convergence Verdict: APPROVE_WITH_NON_BLOCKING_POLISH (S_1 = 1)
  • Inline Review Threads Opened: 0 (Zero-Thread APPROVE policy: 1 non-blocking notes collapsed below so automated merge/Grain loops are not disrupted)
  • Scoped Human Review Lenses: CONTROL_PLANE_CI_OR_LINTER_EDIT, INLINE_LINT_SUPPRESSION_ADDED

PR #1699 cleanly extracts the six standalone Python pre-commit gates from .githooks/pre-commit into a declarative PYTHON_GATES table and stdlib-only runner in tools/lint/precommit_gates.py, with matching Bazel targets (tools/lint/BUILD.bazel), unit tests (tools/lint/precommit_gates_test.py), documentation (tools/README.md), and all CI checks passing. All five prior gemini-code-assist review threads (REPO_ROOT resolution, cwd=REPO_ROOT propagation, and git diff --cached stderr logging/test assertion) are verified fixed at HEAD 67061d061d. Only one non-blocking Medium test-coverage suggestion remains: adding a mode="check" unit test case when baseline_path is configured to guard the ctx.mode != "fix" check in _update_baseline. Because the diff modifies the protected .githooks/pre-commit control-plane file (CONTROL_PLANE_CI_OR_LINTER_EDIT) and adds one # noqa: E402 suppression (INLINE_LINT_SUPPRESSION_ADDED), scoped human sign-off is required before merge.

Non-blocking polish & follow-up notes (1 items — no action required to merge)

C1 · [Medium · Non-Blocking] Add check-mode boundary test for baseline_path gates to verify --update-baseline is skipped (tools/lint/precommit_gates_test.py:117)

test_baseline_error_paths_and_unchanged and test_runs_and_updates_baseline_when_changed_in_fix_mode only exercise baseline_path with mode="fix", while test_records_failure_on_nonzero_script_exit uses mode="check" with baseline_path=None. As a result, the ctx.mode != "fix" guard in _update_baseline (tools/lint/precommit_gates.py:146) that prevents check mode from invoking --update-baseline or git add is untested.

Consider adding a check-mode case to verify exec_cmd is called only once when baseline_path is set:

ctx_check = gates.RunContext(mode="check")
with (
    mock.patch.object(
        gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "")
    ) as chk_exec,
    mock.patch("sys.stdout", new_callable=io.StringIO),
):
    gates.run_python_gate(ctx_check, gate)
self.assertFalse(ctx_check.failed)
chk_exec.assert_called_once()

@VLukiantsev
VLukiantsev added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit a253932 Nov 18, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants