Skip to content

feat(pregate): lanes follows Python imports and mock.patch targets - #3201

Closed
scottgigante-hubflow wants to merge 1 commit into
garrytan:mainfrom
scottgigante-hubflow:feat/pregate-python-import-graph
Closed

scottgigante-hubflow wants to merge 1 commit into
garrytan:mainfrom
scottgigante-hubflow:feat/pregate-python-import-graph

Conversation

@scottgigante-hubflow

Copy link
Copy Markdown
Contributor

Problem

gstack-pregate's lanes check maps each touched file to the tests that depend on it through lib/pregate/import-graph.ts. SOURCE_EXT, TEST_FILE_RE, detectRunner and parseSummary already handle Python and pytest, but referencesIn() only parsed imports for JS/TS, and the literal scan only resolved slash paths with JS extensions. In a Python repo, nearly every touched .py file therefore fails lanes with PREGATE_NO_LANE.

The case that prompted this is a private FastAPI service: about 2,200 .py files, about 700 of them pytest test files, absolute imports from the repo root (from managers.stop import StopManager), tests under tests/test_<layer>/test_<module>.py, and heavy use of mock.patch("pkg.mod.Class.method"). Every touched Python file came back NO_LANE.

Change

For .py sources, referencesIn now adds:

  • import edges from import a.b.c [as x], import a, b.c, from a.b import c, d (each of a.b, a.b.c and a.b.d is tried, because c may be a submodule), parenthesised multi-line import lists, and relative from . import x / from ..pkg.mod import y, which resolve against the importing file's package directory.
  • Resolution: a dotted module resolves to a/b/c.py or a/b/c/__init__.py when that file is in the repo. Stdlib and site-packages modules resolve to nothing.
  • literal edges from Python test files (anywhere scansLiterals is true): a dotted string such as 'pkg.mod.Class.method', the form a mock.patch target takes, resolves to the longest dotted prefix that is a repo module.
  • Strings and # comments are blanked in one left-to-right pass before imports are matched. Triple-quoted docstrings, ordinary strings and comments that mention import x therefore create no edge, and a # inside a string does not cut a line short. Backslash line continuations are joined.

The parser is a dependency-free line/regex scanner. It does not spawn Python.

Design decision: __init__.py edges

Importing a.b.c also adds an edge from a/__init__.py and a/b/__init__.py (when they exist), because Python executes those files on the way in. This is cheap: one files.has per path segment. It matches the graph's rule that uncertainty selects more tests: a touched package __init__.py selects every test that imports anything beneath it. Dotted patch literals get the same treatment.

Effect

I built the graph over the repo described above. Counting non-test .py files with no lane:

no-lane .py files (of 1,517 non-test) graph build
before 1,473 113 ms
after 780 676 ms

Almost all of the remaining 780 are things no import reaches:

  • Alembic migrations (618)
  • conftest.py, test helpers and package __init__.py files under tests/
  • one-off scripts
  • a vendored package rooted in a subdirectory

Only about 9 application modules are still unmapped. Those can use .gstack/pregate.json dependencies declarations.

Known gaps (left as follow-ups)

  • conftest.py: pytest loads it implicitly, so no edge reaches it. A follow-up could map each conftest.py to every test under its directory.
  • Source roots: only the repository root is a source root. A src/ layout, or a package rooted in a subdirectory, does not resolve. Declarations cover these today.
  • importlib.import_module("…"): dynamic imports only resolve when they appear in a test file, through the dotted-literal path.

Tests

The new case in test/pregate-selection.test.ts uses a small Python package with tests. It covers:

  • an absolute import
  • a from-import of a submodule
  • relative . and .. imports
  • a parenthesised multi-line list with an inline comment
  • stdlib imports (os, json, os.path) producing no edge
  • a mock.patch dotted literal
  • __init__.py selection
  • a module mentioned only in a docstring, a comment and a string, which gets no lane
bun test test/pregate-selection.test.ts -t "import graph"   # 2 pass
bun run typecheck                                            # clean

The other pregate files (pregate-guards, pregate-invalidation, pregate-regate, the bin tests in pregate-selection) fail locally on this Mac, but they fail on unmodified main in the same way. They are subprocess lanes runs plus 5s timeouts in pregate-regate (local bun 1.3.11, CI pins 1.4.2). Which regate tests time out changes from run to run. None of these tests involve a .py file. I'm relying on CI for the full suite.

VERSION and CHANGELOG are not bumped. I've left that to the maintainer's release slot.

🤖 Generated with Claude Code

The import graph only parsed JS/TS imports, so in a Python repo every
touched module failed `lanes` with PREGATE_NO_LANE even though
SOURCE_EXT, TEST_FILE_RE and the pytest runner/summary already
understood Python.

Python sources now contribute `import a.b`, `from a.b import c`
(c may be a submodule), parenthesised multi-line lists and relative
`from ..pkg import x` edges, resolved to `a/b.py` or `a/b/__init__.py`
from the repository root. Package `__init__.py` files on the path get
edges too, since Python executes them on import. Modules outside the
repo resolve to nothing. In test files, dotted string literals such as
`'pkg.mod.Class.method'` (mock.patch targets) resolve to the longest
dotted prefix that is a repo module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trunk-io

trunk-io Bot commented Oct 15, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Triage (fix wave 5, GSTA-37): this is a feature (Python import edges for lanes), not a bug fix, and lib/pregate/import-graph.ts is being changed by the open wave PR #3205/#3209. It isn't in this wave. It can be reviewed on its own once #3209 lands.

garrytan added a commit that referenced this pull request Oct 17, 2026
Review of #3201 on the integrated tree found two quadratic paths on
untrusted Python source and three ways a test could be left unselected:

- The from-import module spec let two repetitions share a dot, so a
  failed match on a long run of dots backtracked quadratically. The spec
  is now dots followed by an optional word-led name.
- Dotted literals and imports resolved every prefix with repeated
  split/join. Names are capped at 32 segments, deeper than any module path.
- A literal that also named a repo file skipped its mock.patch reading;
  both readings now add edges.
- A package and a same-named module both map (Python loads the package).
- Imports after `;` or a compound statement's `:` are recognised.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
capy-ai Bot added a commit that referenced this pull request Oct 18, 2026
…dependencies regen diagnostic (#3252)

* feat(pregate): lanes follows Python imports and mock.patch targets

The import graph only parsed JS/TS imports, so in a Python repo every
touched module failed `lanes` with PREGATE_NO_LANE even though
SOURCE_EXT, TEST_FILE_RE and the pytest runner/summary already
understood Python.

Python sources now contribute `import a.b`, `from a.b import c`
(c may be a submodule), parenthesised multi-line lists and relative
`from ..pkg import x` edges, resolved to `a/b.py` or `a/b/__init__.py`
from the repository root. Package `__init__.py` files on the path get
edges too, since Python executes them on import. Modules outside the
repo resolve to nothing. In test files, dotted string literals such as
`'pkg.mod.Class.method'` (mock.patch targets) resolve to the longest
dotted prefix that is a repo module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(regen): report missing dependencies as REGEN_DEPS_MISSING, not REGEN_STALE

When package.json declares dependencies and node_modules is missing, the
scratch worktree runs the generator without them. A generator that degrades
and still exits 0 then looked stale, and the fix line sent people to rerun
the generator. gstack-regen check and the pregate regen check now report
"dependencies missing; run <install>" with its own result code and
troubleshooting anchor; the install command follows the lockfile. The
fixture reproduces the shape GSTA-31 saw in gbrain.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): keep Python import scanning linear and conservative

Review of #3201 on the integrated tree found two quadratic paths on
untrusted Python source and three ways a test could be left unselected:

- The from-import module spec let two repetitions share a dot, so a
  failed match on a long run of dots backtracked quadratically. The spec
  is now dots followed by an optional word-led name.
- Dotted literals and imports resolved every prefix with repeated
  split/join. Names are capped at 32 segments, deeper than any module path.
- A literal that also named a repo file skipped its mock.patch reading;
  both readings now add edges.
- A package and a same-named module both map (Python loads the package).
- Imports after `;` or a compound statement's `:` are recognised.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): linear Python string blanking, non-empty from-spec, repo-bounded depth

Second review round on the Python import scanner:

- An empty from-spec let the whitespace on both sides of it compete, so
  `from` followed by a long run of spaces backtracked quadratically. The
  spec now starts with a dot or a word character.
- Strings and comments are blanked by a single linear scanner that honours
  backslash escapes, so an escaped triple quote no longer ends a docstring
  early and swallows the imports after it.
- The fixed 32-segment cap dropped real deep modules. Dotted names are now
  bounded by the repo's deepest path, which no module can exceed.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): unreleased entry for Python import lanes and REGEN_DEPS_MISSING

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* v1.91.126.0 stamp

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* release slot 1.91.126.0: back to unstamped (CHANGELOG entry unreleased)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(changelog): resolve the main-merge conflict (Unreleased entry above 1.91.127.0)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* v1.91.131.0 stamp

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: Scott Gigante <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai

capy-ai Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Thank you, @scottgigante-hubflow. Your Python import support for pregate landed in #3252 (v1.91.131.0, 8f103ba), with your commit cherry-picked under your authorship. Review found and fixed some backtracking and selection edge cases on top of it, and each fix has a regression test. Closing as merged via #3252.

@capy-ai capy-ai Bot closed this Oct 18, 2026
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.

1 participant