claude-dev-env 8.106.4 → 8.107.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/_shared/pr-loop/scripts/code_rules_gate_parts/tests/test_gate_running.py +1 -1
  2. package/bin/codex-trimmed-sol.test.mjs +1 -0
  3. package/bin/install-pele-package.mjs +161 -0
  4. package/bin/install-pele-package.test.mjs +269 -0
  5. package/bin/install-pstack-plugin.mjs +5 -3
  6. package/bin/install.agents-home.test.mjs +1 -0
  7. package/bin/install.codex-rules.test.mjs +2 -0
  8. package/bin/install.cursor-rules.test.mjs +1 -0
  9. package/bin/install.mjs +18 -0
  10. package/bin/install.pr-visual.test.mjs +1 -1
  11. package/bin/install.profile-settings.test.mjs +1 -0
  12. package/bin/install.prune.test.mjs +1 -0
  13. package/bin/install.question-presentation.test.mjs +4 -0
  14. package/bin/install.settings-defaults.test.mjs +1 -0
  15. package/bin/install.shared-settings.test.mjs +1 -0
  16. package/bin/install.test.mjs +8 -0
  17. package/bin/install.transaction.test.mjs +8 -0
  18. package/bin/install.uninstall-transaction.test.mjs +2 -0
  19. package/bin/install.update-keeps-hooks.test.mjs +1 -0
  20. package/docs/CODE_RULES.md +2 -2
  21. package/docs/code-rules/core-principles-and-config.md +2 -1
  22. package/hooks/blocking/code_rules_magic_values.py +134 -1
  23. package/hooks/blocking/test_code_rules_enforcer_stage.py +1 -1
  24. package/hooks/blocking/test_code_rules_enforcer_string_magic.py +1 -1
  25. package/hooks/blocking/test_code_rules_magic_values.py +162 -0
  26. package/hooks/blocking/test_code_rules_validation_phase.py +1 -1
  27. package/hooks/validators/magic_value_checks.py +77 -12
  28. package/hooks/validators/test_magic_value_checks.py +88 -4
  29. package/hooks/validators/test_run_all_validators_pretooluse.py +1 -1
  30. package/hooks/validators/test_todo_checks.py +13 -1
  31. package/hooks/validators/test_validator_base.py +19 -1
  32. package/hooks/validators/todo_checks.py +2 -12
  33. package/hooks/validators/validator_base.py +33 -2
  34. package/package.json +1 -1
  35. package/scripts/ci/scratch-home-install.test.mjs +1 -1
@@ -9,6 +9,7 @@ Note: Only checks for magic numbers. Magic string detection is not implemented.
9
9
  import ast
10
10
  import re
11
11
  import sys
12
+ from collections import Counter
12
13
  from pathlib import Path
13
14
  from typing import Dict, FrozenSet, List, Set, Tuple, Type
14
15
 
@@ -16,7 +17,7 @@ from .exempt_paths import (
16
17
  is_config_file,
17
18
  is_test_file,
18
19
  )
19
- from .validator_base import Violation
20
+ from .validator_base import Violation, run_file_validator_cli
20
21
 
21
22
 
22
23
  ALLOWED_NUMBERS: FrozenSet[int] = frozenset({-1, 0, 1})
@@ -35,6 +36,8 @@ def check_magic_values(tree: ast.AST, filename: str) -> List[Violation]:
35
36
  violations: List[Violation] = []
36
37
  negated_literal_ids: Set[int] = set()
37
38
  parent_by_child_id = _build_parent_map(tree)
39
+ all_outer_scope_names = _global_and_nonlocal_names(tree)
40
+ all_shared_statement_lines = _lines_holding_several_statements(tree)
38
41
 
39
42
  for node in ast.walk(tree):
40
43
  if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
@@ -55,6 +58,10 @@ def check_magic_values(tree: ast.AST, filename: str) -> List[Violation]:
55
58
  continue
56
59
  if _is_in_constant_definition(outermost_unary, parent_by_child_id):
57
60
  continue
61
+ if _is_named_function_local_binding(
62
+ outermost_unary, parent_by_child_id, all_outer_scope_names, all_shared_statement_lines
63
+ ):
64
+ continue
58
65
  violations.append(
59
66
  Violation(
60
67
  filename,
@@ -74,6 +81,10 @@ def check_magic_values(tree: ast.AST, filename: str) -> List[Violation]:
74
81
  continue
75
82
  if _is_in_constant_definition(node, parent_by_child_id):
76
83
  continue
84
+ if _is_named_function_local_binding(
85
+ node, parent_by_child_id, all_outer_scope_names, all_shared_statement_lines
86
+ ):
87
+ continue
77
88
  violations.append(
78
89
  Violation(
79
90
  filename,
@@ -130,6 +141,70 @@ def _is_in_constant_definition(
130
141
  return False
131
142
 
132
143
 
144
+ def _global_and_nonlocal_names(tree: ast.AST) -> Set[str]:
145
+ """Return every name a ``global`` or ``nonlocal`` statement declares in the tree."""
146
+ return {
147
+ each_name
148
+ for each_node in ast.walk(tree)
149
+ if isinstance(each_node, (ast.Global, ast.Nonlocal))
150
+ for each_name in each_node.names
151
+ }
152
+
153
+
154
+ def _lines_holding_several_statements(tree: ast.AST) -> Set[int]:
155
+ """Return the line numbers where more than one statement starts."""
156
+ statement_count_by_line = Counter(
157
+ each_node.lineno for each_node in ast.walk(tree) if isinstance(each_node, ast.stmt)
158
+ )
159
+ return {each_line for each_line, each_count in statement_count_by_line.items() if each_count > 1}
160
+
161
+
162
+ def _is_named_function_local_binding(
163
+ node: ast.AST,
164
+ parent_by_child_id: Dict[int, ast.AST],
165
+ all_outer_scope_names: Set[str],
166
+ all_shared_statement_lines: Set[int],
167
+ ) -> bool:
168
+ """Return True when a function binds ``node`` alone to one plain name.
169
+
170
+ ``header_row_count = 30`` on one line inside a function passes, since
171
+ the name explains the number. A number inside an expression, a default
172
+ parameter, a keyword argument, a module-level or class-level binding, a
173
+ binding that shares its line with another statement, and a binding to a
174
+ name the tree declares ``global`` or ``nonlocal`` still fire.
175
+ """
176
+ bound_value: ast.AST = node
177
+ parent = parent_by_child_id.get(id(bound_value))
178
+ while isinstance(parent, ast.UnaryOp) and isinstance(parent.op, (ast.USub, ast.UAdd)):
179
+ bound_value = parent
180
+ parent = parent_by_child_id.get(id(bound_value))
181
+ if not isinstance(parent, (ast.Assign, ast.AnnAssign)) or parent.value is not bound_value:
182
+ return False
183
+ all_targets = parent.targets if isinstance(parent, ast.Assign) else [parent.target]
184
+ if len(all_targets) != 1 or not isinstance(all_targets[0], ast.Name):
185
+ return False
186
+ is_one_line = parent.lineno == parent.end_lineno
187
+ is_alone_on_line = parent.lineno not in all_shared_statement_lines
188
+ if not (is_one_line and is_alone_on_line) or all_targets[0].id in all_outer_scope_names:
189
+ return False
190
+ return _encloses_in_function(parent, parent_by_child_id)
191
+
192
+
193
+ def _encloses_in_function(
194
+ node: ast.AST,
195
+ parent_by_child_id: Dict[int, ast.AST],
196
+ ) -> bool:
197
+ """Return True when the nearest enclosing scope of ``node`` is a function."""
198
+ current_node: ast.AST = node
199
+ while id(current_node) in parent_by_child_id:
200
+ current_node = parent_by_child_id[id(current_node)]
201
+ if isinstance(current_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
202
+ return True
203
+ if isinstance(current_node, ast.ClassDef):
204
+ return False
205
+ return False
206
+
207
+
133
208
  def _is_upper_snake_constant_assignment(node: ast.AST) -> bool:
134
209
  if isinstance(node, ast.Assign):
135
210
  for target in node.targets:
@@ -164,17 +239,7 @@ def validate_file(file_path: Path) -> List[Violation]:
164
239
 
165
240
 
166
241
  def main() -> int:
167
- if len(sys.argv) < 2:
168
- return 1
169
-
170
- all_violations: List[Violation] = []
171
- for file_arg in sys.argv[1:]:
172
- all_violations.extend(validate_file(Path(file_arg)))
173
-
174
- for violation in all_violations:
175
- print(violation)
176
-
177
- return 1 if all_violations else 0
242
+ return run_file_validator_cli(validate_file)
178
243
 
179
244
 
180
245
  if __name__ == "__main__":
@@ -27,8 +27,8 @@ def process():
27
27
  '''
28
28
 
29
29
  BAD_MAGIC_NUMBER = '''
30
- def process():
31
- timeout = 5000
30
+ def process(attempts):
31
+ timeout = attempts * 5000
32
32
  return timeout
33
33
  '''
34
34
 
@@ -113,8 +113,8 @@ SETTINGS: dict[str, int] = {"timeout": 30}
113
113
  '''
114
114
 
115
115
  NON_CONSTANT_ASSIGNMENT_IN_FUNCTION = '''
116
- def configure():
117
- delay = 30
116
+ def configure(base):
117
+ delay = base + 30
118
118
  return delay
119
119
  '''
120
120
 
@@ -415,3 +415,87 @@ class TestValidateFilePathExemptions:
415
415
  violations = validate_file(production_path)
416
416
  assert len(violations) == 1
417
417
  assert "42" in violations[0].message
418
+
419
+
420
+ NAMED_FUNCTION_LOCAL_NUMBERS = """
421
+ def configure():
422
+ header_row_count: int = 30
423
+ lowest_offset = -40
424
+ highest_offset = +40
425
+ return header_row_count, lowest_offset, highest_offset
426
+ """
427
+
428
+ DEFAULT_PARAMETER_NUMBER = """
429
+ def configure(header_row_count=30):
430
+ return header_row_count
431
+ """
432
+
433
+ CLASS_LEVEL_NUMBER = """
434
+ class Settings:
435
+ header_row_count = 30
436
+ """
437
+
438
+
439
+ def test_should_pass_numbers_bound_alone_to_function_local_names() -> None:
440
+ assert check_magic_values(ast.parse(NAMED_FUNCTION_LOCAL_NUMBERS), "test.py") == []
441
+
442
+
443
+ def test_should_flag_a_default_parameter_number() -> None:
444
+ violations = check_magic_values(ast.parse(DEFAULT_PARAMETER_NUMBER), "test.py")
445
+ assert [violation.message for violation in violations] == ["Magic number 30 - use named constant"]
446
+
447
+
448
+ def test_should_flag_a_class_level_number() -> None:
449
+ violations = check_magic_values(ast.parse(CLASS_LEVEL_NUMBER), "test.py")
450
+ assert [violation.message for violation in violations] == ["Magic number 30 - use named constant"]
451
+
452
+
453
+ GLOBAL_NAME_NUMBER = """
454
+ def raise_limit():
455
+ global retry_limit
456
+ retry_limit = 40
457
+ """
458
+
459
+ MIXED_SIGN_NAMED_LOCAL = """
460
+ def configure():
461
+ lowest_offset = -+40
462
+ return lowest_offset
463
+ """
464
+
465
+
466
+ def test_should_flag_a_number_bound_to_a_global_name() -> None:
467
+ violations = check_magic_values(ast.parse(GLOBAL_NAME_NUMBER), "test.py")
468
+ assert [violation.message for violation in violations] == ["Magic number 40 - use named constant"]
469
+
470
+
471
+ def test_should_pass_a_mixed_sign_number_bound_to_a_local_name() -> None:
472
+ assert check_magic_values(ast.parse(MIXED_SIGN_NAMED_LOCAL), "test.py") == []
473
+
474
+
475
+ MULTI_LINE_NAMED_LOCAL = """
476
+ def configure():
477
+ header_row_count = (
478
+ 30
479
+ )
480
+ return header_row_count
481
+ """
482
+
483
+
484
+ def test_should_flag_a_number_bound_across_several_lines() -> None:
485
+ violations = check_magic_values(ast.parse(MULTI_LINE_NAMED_LOCAL), "test.py")
486
+ assert [violation.message for violation in violations] == ["Magic number 30 - use named constant"]
487
+
488
+
489
+ SHARED_LINE_NAMED_LOCALS = """
490
+ def configure():
491
+ low_offset = 30; high_offset = 40
492
+ return low_offset, high_offset
493
+ """
494
+
495
+
496
+ def test_should_flag_numbers_bound_on_a_line_shared_with_another_statement() -> None:
497
+ violations = check_magic_values(ast.parse(SHARED_LINE_NAMED_LOCALS), "test.py")
498
+ assert sorted(violation.message for violation in violations) == [
499
+ "Magic number 30 - use named constant",
500
+ "Magic number 40 - use named constant",
501
+ ]
@@ -458,7 +458,7 @@ class TestBaselineScopedGate:
458
458
  "tool_input": {
459
459
  "file_path": str(target_file),
460
460
  "old_string": " return 199\n",
461
- "new_string": " threshold = 42\n return 199 + threshold\n",
461
+ "new_string": " threshold = abs(42)\n return 199 + threshold\n",
462
462
  },
463
463
  }
464
464
  )
@@ -1,8 +1,10 @@
1
1
  """Tests for TODO/FIXME detection."""
2
2
 
3
+ from pathlib import Path
4
+
3
5
  import pytest
4
6
 
5
- from .todo_checks import check_untracked_todos
7
+ from .todo_checks import check_untracked_todos, main
6
8
  from .validator_base import Violation
7
9
 
8
10
 
@@ -38,3 +40,13 @@ class TestUntrackedTodos:
38
40
  code = "# FIXME: broken\ndef foo(): pass"
39
41
  violations = check_untracked_todos(code, "test.py")
40
42
  assert len(violations) == 1
43
+
44
+
45
+ def test_main_should_report_an_untracked_todo_in_a_named_file(
46
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
47
+ ) -> None:
48
+ source_path = tmp_path / "worker.py"
49
+ source_path.write_text("def run():\n pass # TODO tidy up\n", encoding="utf-8")
50
+ monkeypatch.setattr("sys.argv", ["todo_checks", str(source_path)])
51
+ assert main() == 1
52
+ assert capsys.readouterr().out.startswith(f"{source_path}:2:")
@@ -5,7 +5,7 @@ from pathlib import Path
5
5
 
6
6
  import pytest
7
7
 
8
- from .validator_base import Violation, source_text, syntax_tree
8
+ from .validator_base import Violation, run_file_validator_cli, source_text, syntax_tree
9
9
 
10
10
 
11
11
  class TestViolation:
@@ -73,3 +73,21 @@ def test_syntax_tree_raises_syntax_error_for_unparseable_source() -> None:
73
73
  """A caller's own SyntaxError branch still fires."""
74
74
  with pytest.raises(SyntaxError):
75
75
  syntax_tree("def broken(:\n")
76
+
77
+
78
+ def _flag_every_file(file_path: Path) -> list[Violation]:
79
+ return [Violation(file=str(file_path), line=3, message="flagged")]
80
+
81
+
82
+ def test_run_file_validator_cli_should_print_violations_and_fail(capsys: pytest.CaptureFixture[str]) -> None:
83
+ exit_code = run_file_validator_cli(_flag_every_file, ["validator", "a.py", "b.py"])
84
+ assert exit_code == 1
85
+ assert capsys.readouterr().out == "a.py:3: flagged\nb.py:3: flagged\n"
86
+
87
+
88
+ def test_run_file_validator_cli_should_pass_clean_files() -> None:
89
+ assert run_file_validator_cli(lambda file_path: [], ["validator", "a.py"]) == 0
90
+
91
+
92
+ def test_run_file_validator_cli_should_fail_with_no_file_named() -> None:
93
+ assert run_file_validator_cli(_flag_every_file, ["validator"]) == 1
@@ -8,7 +8,7 @@ import sys
8
8
  from pathlib import Path
9
9
  from typing import List
10
10
 
11
- from .validator_base import Violation, source_text
11
+ from .validator_base import Violation, run_file_validator_cli, source_text
12
12
 
13
13
 
14
14
  TODO_PATTERN = re.compile(r"#\s*(TODO|FIXME)\b(?!.*#\d+)", re.IGNORECASE)
@@ -42,17 +42,7 @@ def validate_file(file_path: Path) -> List[Violation]:
42
42
 
43
43
 
44
44
  def main() -> int:
45
- if len(sys.argv) < 2:
46
- return 1
47
-
48
- all_violations: List[Violation] = []
49
- for file_arg in sys.argv[1:]:
50
- all_violations.extend(validate_file(Path(file_arg)))
51
-
52
- for violation in all_violations:
53
- print(violation)
54
-
55
- return 1 if all_violations else 0
45
+ return run_file_validator_cli(validate_file)
56
46
 
57
47
 
58
48
  if __name__ == "__main__":
@@ -1,10 +1,13 @@
1
1
  """Base classes for validators.
2
2
 
3
- Provides shared dataclasses used across all validator modules, and the one
4
- read and the one parse each of those modules shares for a given file.
3
+ Provides shared dataclasses used across all validator modules, the one
4
+ read and the one parse each of those modules shares for a given file, and
5
+ the command-line runner a validator module's ``main`` can call.
5
6
  """
6
7
 
7
8
  import ast
9
+ import sys
10
+ from collections.abc import Callable, Sequence
8
11
  from dataclasses import dataclass
9
12
  from functools import lru_cache
10
13
  from pathlib import Path
@@ -89,3 +92,31 @@ def syntax_tree(source: str) -> ast.Module:
89
92
  The parsed module.
90
93
  """
91
94
  return ast.parse(source)
95
+
96
+
97
+ def run_file_validator_cli(
98
+ validate_file: Callable[[Path], list[Violation]],
99
+ all_arguments: Sequence[str] | None = None,
100
+ ) -> int:
101
+ """Validate each file named on the command line and print its violations.
102
+
103
+ ``python -m validators.todo_checks a.py b.py`` prints one line per
104
+ violation and exits 1 when any file has one.
105
+
106
+ Args:
107
+ validate_file: The module's per-file check.
108
+ all_arguments: The command-line arguments, program name first.
109
+ Defaults to ``sys.argv``.
110
+
111
+ Returns:
112
+ 1 with no file named or any violation found, else 0.
113
+ """
114
+ all_file_arguments = list(sys.argv if all_arguments is None else all_arguments)[1:]
115
+ if not all_file_arguments:
116
+ return 1
117
+ all_violations: list[Violation] = []
118
+ for each_file_argument in all_file_arguments:
119
+ all_violations.extend(validate_file(Path(each_file_argument)))
120
+ for each_violation in all_violations:
121
+ sys.stdout.write(f"{each_violation}\n")
122
+ return 1 if all_violations else 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-dev-env",
3
- "version": "8.106.4",
3
+ "version": "8.107.0",
4
4
  "description": "Claude Code development standards — rules, hooks, agents, commands, and skills",
5
5
  "type": "module",
6
6
  "bin": {
@@ -59,7 +59,7 @@ test('an install into a scratch home leaves the caller Codex home config alone',
59
59
  process.env.CODEX_HOME = callerCodexHome;
60
60
  const scratchHome = createScratchHome();
61
61
  try {
62
- const install = runInstaller(scratchHome, []);
62
+ const install = runInstaller(scratchHome, ['--no-pele']);
63
63
  assert.equal(install.status, 0, `${install.stdout}\n${install.stderr}`);
64
64
  assert.equal(readFileSync(callerConfigPath, 'utf8'), callerConfigBody);
65
65
  } finally {