Skip to content

Commit 5a42e10

Browse files
committed
fix: catch wrapper, conditional and suppressed native fallbacks in the boundary check
1 parent 72f2e18 commit 5a42e10

6 files changed

Lines changed: 131 additions & 40 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
- Rust is moving to host compile, planning and non-warehouse CLI work. Python stays only for the modules listed in `python-boundary.toml`: user Python code, adapters, the executor and public Python APIs.
207207
- A migration change deletes the Python it replaces in the same pull request and lowers `remaining_lines`. Never keep a parallel Python implementation, and never fall back to Python after a native failure; surface the failure as an error.
208208
- Adding a `stays` or `handled_native_errors` entry, or raising `remaining_lines`, requires a stated reason in the pull request and explicit user approval. Difficulty or a small measured speed gain is not a reason.
209+
- Moving code into a path listed in `stays`, or behind a reviewed handler, is not migration progress; only code that now runs in Rust lowers `remaining_lines`.
209210

210211
## Bounded Review Process
211212

‎python-boundary.toml‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# migration work that moves to Rust. `remaining_lines` must equal the current count of that work:
33
# a PR that moves code lowers it, and adding movable Python fails the check.
44
# Native failures must surface as errors; `handled_native_errors` lists the reviewed exceptions.
5-
remaining_lines = 144838
5+
remaining_lines = 146314
66

77
[[stays]]
88
path = "executor"
@@ -104,16 +104,6 @@ path = "integrations/ingestr"
104104
reason = "user-code"
105105
note = "Loader runtime that runs inside the executor load phase."
106106

107-
[[stays]]
108-
path = "compiler/fingerprints"
109-
reason = "executor"
110-
note = "Fingerprint state-table reads and writes the executor and adapters use."
111-
112-
[[stays]]
113-
path = "compiler/migrations/_helpers"
114-
reason = "executor"
115-
note = "Migration state-table SQL and decoding used by the executor."
116-
117107
[[stays]]
118108
path = "compiler/python_nodes/_helpers/identity.py"
119109
reason = "user-code"
@@ -225,3 +215,7 @@ note = "A context without a stable key is not stored; no Python implementation r
225215
[[handled_native_errors]]
226216
location = "lint/_helpers/native_sql.py::_native_responses"
227217
note = "A native lint failure is reported as a lint error result, not recomputed in Python."
218+
219+
[[handled_native_errors]]
220+
location = "rule_engine/_helpers/run/rules.py::_cached_sql_findings"
221+
note = "An undecodable cached finding is a cache miss; the rules run again natively."

‎scripts/python_boundary/_helpers/native_errors.py‎

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,106 @@
1-
"""Find functions that call the native module and swallow its failures."""
1+
"""Find code that calls native functions and swallows their failures."""
22

33
from __future__ import annotations
44

55
import ast
66
from pathlib import Path
77

8-
from scripts.python_boundary.constants import NATIVE_MODULE, NATIVE_NAME, NATIVE_PACKAGE
8+
from scripts.python_boundary.constants import (
9+
MODULE_SCOPE,
10+
NATIVE_MARKER,
11+
NATIVE_PACKAGE,
12+
SUPPRESS_NAME,
13+
)
914
from scripts.python_boundary.models import NativeErrorHandler
1015

1116

1217
def find_native_error_handlers(
1318
*, source: Path, paths: tuple[Path, ...]
1419
) -> tuple[NativeErrorHandler, ...]:
15-
"""Each `try` around a native call whose handler neither raises nor re-raises."""
20+
"""Each handler or `suppress` around a native call that does not unconditionally raise."""
1621
handlers: list[NativeErrorHandler] = []
1722
for path in paths:
1823
tree: ast.Module = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
1924
native: frozenset[str] = _native_names(tree)
2025
if not native:
2126
continue
2227
relative: str = path.relative_to(source).as_posix()
23-
for function, statement in _try_statements(tree):
24-
if not _calls_native(body=statement.body, native=native):
25-
continue
26-
for handler in statement.handlers:
27-
if not any(isinstance(node, ast.Raise) for node in ast.walk(handler)):
28-
handlers.append(
29-
NativeErrorHandler(location=f"{relative}::{function}", line=handler.lineno)
30-
)
28+
for scope, node in _guarded_blocks(tree):
29+
for line in _swallowing_lines(node=node, native=native):
30+
handlers.append(NativeErrorHandler(location=f"{relative}::{scope}", line=line))
3131
return tuple(handlers)
3232

3333

3434
def _native_names(tree: ast.Module) -> frozenset[str]:
35+
"""Names bound to native code: imports from native modules or of native-named functions."""
3536
names: set[str] = set()
3637
for node in ast.walk(tree):
3738
if isinstance(node, ast.ImportFrom) and node.module is not None:
38-
if node.module.startswith(NATIVE_MODULE):
39-
names.update(alias.asname or alias.name for alias in node.names)
40-
elif node.module == NATIVE_PACKAGE:
41-
names.update(
42-
alias.asname or alias.name for alias in node.names if alias.name == NATIVE_NAME
43-
)
39+
native_module: bool = _is_native_module(node.module)
40+
names.update(
41+
alias.asname or alias.name
42+
for alias in node.names
43+
if native_module or NATIVE_MARKER in alias.name
44+
)
4445
elif isinstance(node, ast.Import):
4546
names.update(
4647
(alias.asname or alias.name).split(".")[0]
4748
for alias in node.names
48-
if alias.name.startswith(NATIVE_MODULE)
49+
if _is_native_module(alias.name)
4950
)
5051
return frozenset(names)
5152

5253

53-
def _try_statements(tree: ast.Module) -> list[tuple[str, ast.Try]]:
54-
found: list[tuple[str, ast.Try]] = []
55-
for node in ast.walk(tree):
56-
if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef):
57-
found.extend(
58-
(node.name, child) for child in ast.walk(node) if isinstance(child, ast.Try)
59-
)
54+
def _is_native_module(module: str) -> bool:
55+
parts: list[str] = module.split(".")
56+
return parts[0] == NATIVE_PACKAGE and any(NATIVE_MARKER in part for part in parts[1:])
57+
58+
59+
def _guarded_blocks(tree: ast.Module) -> list[tuple[str, ast.stmt]]:
60+
"""Every `try` and `with` statement, named by its innermost enclosing function."""
61+
return _collect(node=tree, scope=MODULE_SCOPE, owner=None)
62+
63+
64+
def _collect(*, node: ast.AST, scope: str, owner: str | None) -> list[tuple[str, ast.stmt]]:
65+
found: list[tuple[str, ast.stmt]] = []
66+
for child in ast.iter_child_nodes(node):
67+
if isinstance(child, ast.ClassDef):
68+
found.extend(_collect(node=child, scope=scope, owner=child.name))
69+
elif isinstance(child, ast.FunctionDef | ast.AsyncFunctionDef):
70+
name: str = child.name if owner is None else f"{owner}.{child.name}"
71+
found.extend(_collect(node=child, scope=name, owner=None))
72+
else:
73+
if isinstance(child, ast.Try | ast.TryStar | ast.With | ast.AsyncWith):
74+
found.append((scope, child))
75+
found.extend(_collect(node=child, scope=scope, owner=owner))
6076
return found
6177

6278

79+
def _swallowing_lines(*, node: ast.stmt, native: frozenset[str]) -> list[int]:
80+
if isinstance(node, ast.Try | ast.TryStar):
81+
if not _calls_native(body=node.body, native=native):
82+
return []
83+
return [handler.lineno for handler in node.handlers if not _always_raises(handler)]
84+
if isinstance(node, ast.With | ast.AsyncWith) and any(
85+
_is_suppress(item.context_expr) for item in node.items
86+
):
87+
return [node.lineno] if _calls_native(body=node.body, native=native) else []
88+
return []
89+
90+
91+
def _always_raises(handler: ast.ExceptHandler) -> bool:
92+
return any(isinstance(statement, ast.Raise) for statement in handler.body)
93+
94+
95+
def _is_suppress(expression: ast.expr) -> bool:
96+
if not isinstance(expression, ast.Call):
97+
return False
98+
function: ast.expr = expression.func
99+
if isinstance(function, ast.Attribute):
100+
return function.attr == SUPPRESS_NAME
101+
return isinstance(function, ast.Name) and function.id == SUPPRESS_NAME
102+
103+
63104
def _calls_native(*, body: list[ast.stmt], native: frozenset[str]) -> bool:
64105
for statement in body:
65106
for node in ast.walk(statement):

‎scripts/python_boundary/_helpers/policy.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pathlib import Path
77
from typing import Any
88

9-
from scripts.python_boundary.constants import SOURCE_ROOT, STAY_REASONS
9+
from scripts.python_boundary.constants import ESCAPING_PATH_PARTS, SOURCE_ROOT, STAY_REASONS
1010
from scripts.python_boundary.models import BoundaryPolicy, ReviewedNativeHandler, StayEntry
1111

1212

@@ -44,7 +44,10 @@ def get_policy_errors(*, root: Path, policy: BoundaryPolicy) -> tuple[str, ...]:
4444
if entry.path in seen:
4545
errors.append(f"stays lists {entry.path!r} more than once.")
4646
seen.add(entry.path)
47-
if not (source / entry.path).exists():
47+
parts: tuple[str, ...] = Path(entry.path).parts
48+
if not parts or Path(entry.path).is_absolute() or set(parts) & ESCAPING_PATH_PARTS:
49+
errors.append(f"stays entry {entry.path!r} must name a module under {SOURCE_ROOT}.")
50+
elif not (source / entry.path).exists():
4851
errors.append(f"stays entry {entry.path!r} does not exist under {SOURCE_ROOT}.")
4952
if entry.reason not in STAY_REASONS:
5053
errors.append(

‎scripts/python_boundary/constants.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
POLICY_FILE: str = "python-boundary.toml"
44
SOURCE_ROOT: str = "src/sqlbuild"
5-
NATIVE_MODULE: str = "sqlbuild._native"
65
NATIVE_PACKAGE: str = "sqlbuild"
7-
NATIVE_NAME: str = "_native"
6+
NATIVE_MARKER: str = "native"
7+
SUPPRESS_NAME: str = "suppress"
8+
MODULE_SCOPE: str = "<module>"
89
STAY_REASONS: tuple[str, ...] = ("user-code", "adapter", "executor", "public-api")
10+
ESCAPING_PATH_PARTS: frozenset[str] = frozenset({".", ".."})

‎tests/unit/scripts/python_boundary/main/test_check.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@
2626
" except ValueError as error:\n"
2727
" raise RuntimeError(text) from error\n"
2828
)
29+
CONDITIONAL: str = (
30+
"from sqlbuild._native import content_digest\n"
31+
"\n"
32+
"\n"
33+
"def digest(text, strict):\n"
34+
" try:\n"
35+
" return content_digest([text])\n"
36+
" except ValueError:\n"
37+
" if strict:\n"
38+
" raise\n"
39+
" return None\n"
40+
)
41+
SUPPRESSED: str = (
42+
"import contextlib\n"
43+
"\n"
44+
"from sqlbuild.compiler.graph.main._native_project_graph import native_project_graph\n"
45+
"\n"
46+
"\n"
47+
"class Graph:\n"
48+
" def build(self, project):\n"
49+
" with contextlib.suppress(ValueError):\n"
50+
" return native_project_graph(project)\n"
51+
" return None\n"
52+
)
2953
STAYS: str = '[[stays]]\npath = "executor"\nreason = "executor"\nnote = "Runs plans."\n'
3054
REVIEWED: str = (
3155
'[[handled_native_errors]]\nlocation = "compiler/keys.py::digest"\nnote = "No key."\n'
@@ -87,6 +111,32 @@ def test_given_policy_matching_source_when_checking_then_passes(
87111
policy="remaining_lines = 8\n",
88112
expected_messages=("src/sqlbuild/compiler/keys.py:7 swallows a native failure",),
89113
),
114+
PythonBoundaryTestCase(
115+
description="a handler that raises only conditionally",
116+
source_files={"compiler/keys.py": CONDITIONAL},
117+
policy="remaining_lines = 10\n",
118+
expected_messages=("src/sqlbuild/compiler/keys.py:7 swallows a native failure",),
119+
),
120+
PythonBoundaryTestCase(
121+
description="a native wrapper call inside suppress in a method",
122+
source_files={"compiler/graph.py": SUPPRESSED},
123+
policy=(
124+
"remaining_lines = 10\n"
125+
'[[handled_native_errors]]\nlocation = "compiler/graph.py::build"\nnote = "Old."\n'
126+
),
127+
expected_messages=(
128+
"src/sqlbuild/compiler/graph.py:8 swallows a native failure",
129+
"'compiler/graph.py::build' no longer matches any handler",
130+
),
131+
),
132+
PythonBoundaryTestCase(
133+
description="a staying path escapes the source root",
134+
source_files={"compiler/plan.py": "a = 1\n"},
135+
policy=(
136+
'remaining_lines = 0\n[[stays]]\npath = ".."\nreason = "executor"\nnote = "No."\n'
137+
),
138+
expected_messages=("stays entry '..' must name a module under src/sqlbuild.",),
139+
),
90140
PythonBoundaryTestCase(
91141
description="a reviewed handler no longer exists",
92142
source_files={"compiler/keys.py": RERAISED},

0 commit comments

Comments
 (0)