From b896de97afdc26f7f7fbcb7f24da876c1c8ae765 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 16:12:47 +0800 Subject: [PATCH] fix(lint): traverse Python expression depth iteratively Use an explicit stack for control-flow depth so valid long binary and attribute expressions cannot exhaust Python recursion during advisory lint. Preserve existing control-flow node counting and continue later scripts. Signed-off-by: Afloat --- CHANGELOG.md | 4 ++ src/skillevaluator/validators/script_lint.py | 12 ++--- tests/validators/test_script_lint.py | 54 +++++++++++++++++++- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dccc9a45c..7aae7cb60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -453,6 +453,10 @@ All notable changes to SkillEvaluator are documented in this file. ### Fixed +- Script lint measures control-flow nesting without recursively walking expression + nodes, so valid long operator or attribute chains no longer abort advisory + checks for that script and subsequent files. + - API caller now percent-encodes scalar OpenAPI path values as individual segments and rejects `.` and `..` values with a JSON input error. - Direct Tier 1 validates the target, selected checks and LLM configuration before preparing scanners, so configuration errors do not trigger downloads or stdout noise. diff --git a/src/skillevaluator/validators/script_lint.py b/src/skillevaluator/validators/script_lint.py index 00e1ce680..3daa79cc3 100644 --- a/src/skillevaluator/validators/script_lint.py +++ b/src/skillevaluator/validators/script_lint.py @@ -47,16 +47,14 @@ def _calculate_nesting_depth(tree: ast.AST) -> int: """Calculate maximum nesting depth of control flow statements.""" max_depth = 0 - - def _walk(node: ast.AST, depth: int) -> None: - nonlocal max_depth + pending = [(tree, 0)] + while pending: + node, depth = pending.pop() if isinstance(node, _NESTING_NODES): depth += 1 max_depth = max(max_depth, depth) - for child in ast.iter_child_nodes(node): - _walk(child, depth) - - _walk(tree, 0) + # Expression depth can exceed Python's call stack even when control flow is shallow. + pending.extend((child, depth) for child in ast.iter_child_nodes(node)) return max_depth diff --git a/tests/validators/test_script_lint.py b/tests/validators/test_script_lint.py index 780c2b122..b4ca4c3d3 100644 --- a/tests/validators/test_script_lint.py +++ b/tests/validators/test_script_lint.py @@ -3,12 +3,13 @@ """Tests for ScriptLintValidator -- AST-based advisory lint checks.""" +import ast from pathlib import Path import pytest from skillevaluator.models.result import Severity, ValidationResult -from skillevaluator.validators.script_lint import ScriptLintValidator +from skillevaluator.validators.script_lint import ScriptLintValidator, _calculate_nesting_depth @pytest.fixture @@ -230,3 +231,54 @@ def test_validator_name(self): v = ScriptLintValidator() assert "Lint" in v.name assert "Advisory" in v.name + + +@pytest.mark.parametrize("directory", ["scripts", "tools"]) +@pytest.mark.parametrize( + "expression", + [" + ".join(["value"] * 1500), "value" + ".next" * 1500], + ids=["binary-expression", "attribute-chain"], +) +def test_deep_expression_does_not_abort_other_script_checks(tmp_path, directory, expression): + (tmp_path / "SKILL.md").write_text("# Example\n", encoding="utf-8") + scripts = tmp_path / directory + scripts.mkdir() + (scripts / "first.py").write_text( + "#!/usr/bin/env python3\nimport argparse\ndef calculate(value):\n if value:\n return " + + expression + + "\n return 42\n", + encoding="utf-8", + ) + (scripts / "second.py").write_text("print('example')\n", encoding="utf-8") + + result = ScriptLintValidator().validate(tmp_path) + + findings = {(Path(f.file_path).name, f.check_name) for f in result.findings} + assert ("first.py", "magic_numbers") in findings + assert ("second.py", "flat_script") in findings + assert ("second.py", "missing_shebang") in findings + assert not any(f.check_name == "deep_nesting" for f in result.findings) + assert result.passed + + +def test_deep_expression_keeps_actual_control_flow_depth(): + source = "def calculate(value):\n" + for depth in range(1, 8): + source += " " * depth + "if value:\n" + source += " " * 8 + "return " + " + ".join(["value"] * 1500) + "\n" + + assert _calculate_nesting_depth(ast.parse(source)) == 7 + + +@pytest.mark.parametrize( + ("source", "expected"), + [ + ("value = 42\n", 0), + ("if first:\n if second:\n pass\nif third:\n pass\n", 2), + ("try:\n with context():\n pass\nexcept Exception:\n while condition:\n pass\n", 2), + ("async def work():\n async for item in items:\n async with item:\n pass\n", 2), + ], + ids=["flat", "siblings", "try-branches", "async"], +) +def test_control_flow_depth_keeps_existing_branch_semantics(source, expected): + assert _calculate_nesting_depth(ast.parse(source)) == expected