claude-dev-env 2.17.0 → 2.19.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.
- package/.agents/skills/AGENTS.md +4 -4
- package/.agents/skills/_shared/AGENTS.md +1 -0
- package/.agents/skills/_shared/pr-loop/AGENTS.md +1 -0
- package/.agents/skills/_shared/pr-loop/preflight-proposal.contract.test.mjs +89 -0
- package/.agents/skills/_shared/pr-loop/preflight-proposal.md +5 -0
- package/.agents/skills/orchestrator/SKILL.md +6 -4
- package/.agents/skills/orchestrator-refresh/SKILL.md +9 -5
- package/.agents/skills/pr-cleanup/SKILL.md +17 -85
- package/.agents/skills/pr-cleanup/reference/process-inventory.md +15 -0
- package/.agents/skills/pr-cleanup/reference/task-seeds.md +20 -0
- package/.agents/skills/{name-by-capability-audit → pr-name-by-capability}/SKILL.md +18 -12
- package/.agents/skills/{name-by-capability-audit → pr-name-by-capability}/reference/offense-examples.md +1 -1
- package/.agents/skills/{name-by-capability-audit → pr-name-by-capability}/reference/task-seeds.md +3 -2
- package/.agents/skills/pr-refinement/SKILL.md +31 -0
- package/.agents/skills/{shared-extraction-audit → pr-shared-extraction}/SKILL.md +24 -7
- package/.agents/skills/{small-cl → pr-small-cl}/SKILL.md +1 -1
- package/.agents/skills/prototype/scripts/test_probe_sandbox_safety.py +33 -5
- package/.agents/skills/skill-builder/SKILL.md +1 -0
- package/.agents/skills/team-advisor/SKILL.md +5 -5
- package/.agents/skills-archived/bugteam/SKILL.md +1 -1
- package/.agents/skills-archived/bugteam/test_skill_additions.py +1 -1
- package/.agents/skills-archived/comments/SKILL.md +1 -1
- package/.agents/skills-archived/descriptions/SKILL.md +1 -1
- package/.agents/skills-archived/reviews/SKILL.md +1 -1
- package/AGENTS.md +1 -1
- package/_shared/advisor/AGENTS.md +5 -5
- package/_shared/advisor/advisor-protocol.md +54 -31
- package/_shared/advisor/reference/advisor-block.md +5 -1
- package/_shared/advisor/reference/consult-format.md +1 -1
- package/_shared/advisor/reference/identity.md +28 -0
- package/_shared/advisor/reference/lifecycle.md +8 -1
- package/_shared/advisor/reference/sol-rung.md +12 -7
- package/_shared/advisor/reference/spawn-walk-log.md +6 -5
- package/_shared/advisor/reference/third-party-bind.md +5 -6
- package/_shared/advisor/reference/warm-up.md +9 -2
- package/_shared/advisor/scripts/codex_sol_advisor.py +66 -11
- package/_shared/advisor/scripts/config/advisor_scripts_constants/advisor_route_constants.py +9 -0
- package/_shared/advisor/scripts/config/advisor_scripts_constants/model_tier_run_validator_constants.py +16 -4
- package/_shared/advisor/scripts/config/advisor_scripts_constants/sol_advisor_constants.py +11 -10
- package/_shared/advisor/scripts/model_tier_run_validator.py +50 -23
- package/_shared/advisor/scripts/tests/test_codex_sol_advisor.py +108 -3
- package/_shared/advisor/scripts/tests/test_model_tier_run_validator.py +171 -82
- package/_shared/advisor/scripts/tests/test_tier_model_ids.py +43 -0
- package/_shared/advisor/scripts/tier_model_ids.py +75 -7
- package/_shared/pr-loop/AGENTS.md +2 -1
- package/_shared/pr-loop/preflight-proposal.md +56 -0
- package/_shared/pr-loop/scripts/code_rules_gate_parts/tests/test_gate_running.py +47 -4
- package/_shared/pr-loop/worker-spawn.md +4 -3
- package/bin/ever-shipped-skills.mjs +14 -0
- package/bin/install-constants.mjs +5 -0
- package/bin/install.agents-home.test.mjs +23 -0
- package/bin/install.codex-rules.test.mjs +61 -6
- package/bin/install.mjs +57 -9
- package/bin/install.prune.test.mjs +65 -3
- package/bin/install.test.mjs +202 -2
- package/docs/CODE_RULES.md +4 -0
- package/docs/references/team-advisor-skill.md +2 -2
- package/hooks/advisory/migration_safety_advisor.py +14 -14
- package/hooks/advisory/test_migration_safety_advisor.py +120 -0
- package/hooks/atomic_file_writer.py +81 -0
- package/hooks/blocking/AGENTS.md +2 -1
- package/hooks/blocking/code_rules_banned_identifiers.py +55 -4
- package/hooks/blocking/code_rules_enforcer_test_support.py +92 -0
- package/hooks/blocking/destructive_command_blocker.py +8 -7
- package/hooks/blocking/pre_tool_use_dispatcher.py +11 -7
- package/hooks/blocking/precommit_code_rules_gate.py +14 -139
- package/hooks/blocking/sensitive_file_protector.py +8 -6
- package/hooks/blocking/session_edit_stage_gate.py +31 -1
- package/hooks/blocking/test_claude_md_orphan_file_blocker.py +171 -1
- package/hooks/blocking/test_code_rules_enforcer_agent_home_tooling.py +37 -12
- package/hooks/blocking/test_code_rules_enforcer_banned_noun_word.py +54 -0
- package/hooks/blocking/test_code_rules_enforcer_duplicate_body_hook_routing.py +15 -53
- package/hooks/blocking/test_code_rules_enforcer_ephemeral.py +13 -62
- package/hooks/blocking/test_code_rules_enforcer_narrow_edit.py +308 -0
- package/hooks/blocking/test_code_rules_enforcer_precheck_forecast.py +14 -71
- package/hooks/blocking/test_code_rules_enforcer_scratchpad.py +14 -16
- package/hooks/blocking/test_code_rules_enforcer_split_entry_2.py +31 -46
- package/hooks/blocking/test_code_rules_enforcer_stage.py +192 -0
- package/hooks/blocking/test_code_rules_enforcer_zero_payload_alias_hook_routing.py +13 -59
- package/hooks/blocking/test_destructive_command_blocker_deny_mode.py +42 -25
- package/hooks/blocking/test_destructive_command_blocker_no_verify.py +58 -38
- package/hooks/blocking/test_docstring_rule_gate_count_blocker.py +113 -1
- package/hooks/blocking/test_env_var_table_code_drift_blocker.py +70 -3
- package/hooks/blocking/test_hook_subprocess_support.py +296 -0
- package/hooks/blocking/test_package_inventory_stale_blocker.py +1 -1
- package/hooks/blocking/test_pii_scanner.py +81 -0
- package/hooks/blocking/test_pre_tool_use_dispatcher.py +32 -3
- package/hooks/blocking/test_precommit_code_rules_gate.py +11 -16
- package/hooks/blocking/test_precommit_code_rules_gate_native_owner.py +168 -0
- package/hooks/blocking/test_pytest_testpaths_orphan_blocker.py +25 -18
- package/hooks/blocking/test_sensitive_file_protector.py +145 -5
- package/hooks/blocking/test_session_edit_stage_gate_staging.py +64 -0
- package/hooks/blocking/test_session_edit_stage_gate_support.py +204 -0
- package/hooks/blocking/test_session_edit_stage_gate_tracker.py +116 -0
- package/hooks/blocking/test_shared_stdin_adoption.py +31 -20
- package/hooks/blocking/test_test_preflight_check.py +80 -0
- package/hooks/git-hooks/pre_commit.py +6 -2
- package/hooks/git-hooks/test_pre_push.py +57 -0
- package/hooks/hooks_constants/AGENTS.md +3 -1
- package/hooks/hooks_constants/atomic_file_writer_constants.py +4 -0
- package/hooks/hooks_constants/banned_identifiers_constants.py +1 -0
- package/hooks/hooks_constants/destructive_command_environment_constants.py +18 -0
- package/hooks/hooks_constants/destructive_command_segment_constants.py +1 -11
- package/hooks/hooks_constants/hardcoded_user_path_constants.py +9 -3
- package/hooks/hooks_constants/pre_tool_use_dispatcher_constants.py +1 -1
- package/hooks/hooks_constants/sensitive_file_protector_constants.py +2 -4
- package/hooks/json_file_reader.py +22 -0
- package/hooks/pending_sidecars.py +33 -0
- package/hooks/test_atomic_file_writer.py +73 -0
- package/hooks/test_json_file_reader.py +24 -0
- package/hooks/test_pending_sidecars.py +25 -0
- package/package.json +1 -1
- /package/.agents/skills/{name-by-capability-audit → pr-name-by-capability}/reference/fetch-commands.md +0 -0
- /package/.agents/skills/{name-by-capability-audit → pr-name-by-capability}/reference/report-template.md +0 -0
- /package/.agents/skills/{name-by-capability-audit → pr-name-by-capability}/reference/rule-checklist.md +0 -0
- /package/.agents/skills/{shared-extraction-audit → pr-shared-extraction}/reference/examples.md +0 -0
- /package/.agents/skills/{shared-extraction-audit → pr-shared-extraction}/reference/offense-taxonomy.md +0 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Test narrow Edit classification and required rule selection."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import io
|
|
7
|
+
import inspect
|
|
8
|
+
import json
|
|
9
|
+
import sys
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import pytest
|
|
14
|
+
_BLOCKING_DIRECTORY = str(Path(__file__).resolve().parent)
|
|
15
|
+
_HOOKS_DIRECTORY = str(Path(__file__).resolve().parent.parent)
|
|
16
|
+
if _BLOCKING_DIRECTORY not in sys.path:
|
|
17
|
+
sys.path.insert(0, _BLOCKING_DIRECTORY)
|
|
18
|
+
if _HOOKS_DIRECTORY not in sys.path:
|
|
19
|
+
sys.path.insert(0, _HOOKS_DIRECTORY)
|
|
20
|
+
|
|
21
|
+
from code_rules_enforcer import ( # noqa: E402
|
|
22
|
+
main,
|
|
23
|
+
validate_content,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
PRODUCTION_FILE_PATH = "packages/app/services.py"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class NarrowEditFixture:
|
|
32
|
+
"""One accepted Edit family with a clean prior fragment and a violation."""
|
|
33
|
+
|
|
34
|
+
old_fragment: str
|
|
35
|
+
new_fragment: str
|
|
36
|
+
expected_marker: str
|
|
37
|
+
rule_name: str
|
|
38
|
+
|
|
39
|
+
ALL_SCOPE_AWARE_RULE_NAMES = frozenset(
|
|
40
|
+
{
|
|
41
|
+
"check_import_block_sorted",
|
|
42
|
+
"check_magic_values",
|
|
43
|
+
"check_duplicate_function_body_across_files",
|
|
44
|
+
"check_same_file_inline_duplicate_body",
|
|
45
|
+
"check_banned_identifiers",
|
|
46
|
+
"check_banned_noun_word_boundary",
|
|
47
|
+
"check_docstring_runon_sentence",
|
|
48
|
+
"check_docstring_prose_wall_without_illustration",
|
|
49
|
+
"check_boolean_naming",
|
|
50
|
+
"check_ignored_must_check_return",
|
|
51
|
+
"check_tests_use_isolated_filesystem_paths",
|
|
52
|
+
"check_return_annotations",
|
|
53
|
+
"check_function_length",
|
|
54
|
+
"check_public_function_missing_paired_test",
|
|
55
|
+
"check_join_separator_string_magic",
|
|
56
|
+
"check_string_literal_magic",
|
|
57
|
+
"check_js_boolean_naming",
|
|
58
|
+
"check_js_banned_identifiers",
|
|
59
|
+
"check_js_bare_flag_return_directive",
|
|
60
|
+
}
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
SCOPE_AWARE_RULE_NAMES = ALL_SCOPE_AWARE_RULE_NAMES
|
|
65
|
+
NARROW_EDIT_ACCEPTED_RULE_NAMES = frozenset(
|
|
66
|
+
{
|
|
67
|
+
"check_string_literal_magic",
|
|
68
|
+
"check_join_separator_string_magic",
|
|
69
|
+
"check_banned_noun_word_boundary",
|
|
70
|
+
"check_boolean_naming",
|
|
71
|
+
"check_ignored_must_check_return",
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
EXPECTED_RULE_TEXT_BY_NAME = {
|
|
77
|
+
"check_string_literal_magic": "string magic value",
|
|
78
|
+
"check_join_separator_string_magic": "string separator",
|
|
79
|
+
"check_banned_noun_word_boundary": "Identifier",
|
|
80
|
+
"check_boolean_naming": "Boolean",
|
|
81
|
+
"check_ignored_must_check_return": "return value",
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
ALL_REQUIRED_RULE_FIXTURES = (
|
|
86
|
+
NarrowEditFixture(
|
|
87
|
+
old_fragment=(
|
|
88
|
+
"import os\n\n"
|
|
89
|
+
"def fetch_secret() -> str:\n"
|
|
90
|
+
" return os.environ.get('missing', '')\n"
|
|
91
|
+
),
|
|
92
|
+
new_fragment=(
|
|
93
|
+
"import os\n\n"
|
|
94
|
+
"def fetch_secret() -> str:\n"
|
|
95
|
+
" return os.environ['STRIPE_SECRET']\n"
|
|
96
|
+
),
|
|
97
|
+
expected_marker="STRIPE_SECRET",
|
|
98
|
+
rule_name="check_string_literal_magic",
|
|
99
|
+
),
|
|
100
|
+
NarrowEditFixture(
|
|
101
|
+
old_fragment=(
|
|
102
|
+
"def render_paths(all_paths: list[str]) -> str:\n"
|
|
103
|
+
" return JOIN_DELIMITER.join(all_paths)\n"
|
|
104
|
+
),
|
|
105
|
+
new_fragment=(
|
|
106
|
+
"def render_paths(all_paths: list[str]) -> str:\n"
|
|
107
|
+
" return ', '.join(all_paths)\n"
|
|
108
|
+
),
|
|
109
|
+
expected_marker="join",
|
|
110
|
+
rule_name="check_join_separator_string_magic",
|
|
111
|
+
),
|
|
112
|
+
NarrowEditFixture(
|
|
113
|
+
old_fragment=(
|
|
114
|
+
"def read_count() -> int:\n"
|
|
115
|
+
" clean_count = 0\n"
|
|
116
|
+
" return clean_count\n"
|
|
117
|
+
),
|
|
118
|
+
new_fragment=(
|
|
119
|
+
"def read_count() -> int:\n"
|
|
120
|
+
" result_count = 0\n"
|
|
121
|
+
" return result_count\n"
|
|
122
|
+
),
|
|
123
|
+
expected_marker="result_count",
|
|
124
|
+
rule_name="check_banned_noun_word_boundary",
|
|
125
|
+
),
|
|
126
|
+
NarrowEditFixture(
|
|
127
|
+
old_fragment=(
|
|
128
|
+
"def check_ready() -> bool:\n"
|
|
129
|
+
" is_ready = True\n"
|
|
130
|
+
" return is_ready\n"
|
|
131
|
+
),
|
|
132
|
+
new_fragment=(
|
|
133
|
+
"def check_ready() -> bool:\n"
|
|
134
|
+
" ready = True\n"
|
|
135
|
+
" return ready\n"
|
|
136
|
+
),
|
|
137
|
+
expected_marker="ready",
|
|
138
|
+
rule_name="check_boolean_naming",
|
|
139
|
+
),
|
|
140
|
+
NarrowEditFixture(
|
|
141
|
+
old_fragment=(
|
|
142
|
+
"def submit_form() -> None:\n"
|
|
143
|
+
" if find_and_click('#submit'):\n"
|
|
144
|
+
" return\n"
|
|
145
|
+
),
|
|
146
|
+
new_fragment=(
|
|
147
|
+
"def submit_form() -> None:\n"
|
|
148
|
+
" find_and_click('#submit')\n"
|
|
149
|
+
),
|
|
150
|
+
expected_marker="find_and_click",
|
|
151
|
+
rule_name="check_ignored_must_check_return",
|
|
152
|
+
),
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _validate_narrow_edit(narrow_edit_fixture: NarrowEditFixture) -> list[str]:
|
|
157
|
+
prior_full_file = narrow_edit_fixture.old_fragment
|
|
158
|
+
post_edit_full_file = narrow_edit_fixture.new_fragment
|
|
159
|
+
return validate_content(
|
|
160
|
+
narrow_edit_fixture.new_fragment,
|
|
161
|
+
PRODUCTION_FILE_PATH,
|
|
162
|
+
old_content=narrow_edit_fixture.old_fragment,
|
|
163
|
+
full_file_content=post_edit_full_file,
|
|
164
|
+
prior_full_file_content=prior_full_file,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _scope_aware_rule_names_from_source() -> frozenset[str]:
|
|
169
|
+
source_tree = ast.parse(inspect.getsource(validate_content))
|
|
170
|
+
all_rule_names: set[str] = set()
|
|
171
|
+
for each_call in ast.walk(source_tree):
|
|
172
|
+
if not isinstance(each_call, ast.Call):
|
|
173
|
+
continue
|
|
174
|
+
if not isinstance(each_call.func, ast.Name):
|
|
175
|
+
continue
|
|
176
|
+
if each_call.func.id == "_fragment_or_deferred_check":
|
|
177
|
+
all_argument_names = {
|
|
178
|
+
each_name.id
|
|
179
|
+
for each_argument in each_call.args[1:]
|
|
180
|
+
for each_name in ast.walk(each_argument)
|
|
181
|
+
if isinstance(each_name, ast.Name)
|
|
182
|
+
}
|
|
183
|
+
if (
|
|
184
|
+
"defer_scope_to_caller" in all_argument_names
|
|
185
|
+
and each_call.args
|
|
186
|
+
and isinstance(each_call.args[0], ast.Name)
|
|
187
|
+
):
|
|
188
|
+
all_rule_names.add(each_call.args[0].id)
|
|
189
|
+
continue
|
|
190
|
+
if not each_call.func.id.startswith("check_"):
|
|
191
|
+
continue
|
|
192
|
+
all_argument_names = {
|
|
193
|
+
each_name.id
|
|
194
|
+
for each_argument in each_call.args
|
|
195
|
+
for each_name in ast.walk(each_argument)
|
|
196
|
+
if isinstance(each_name, ast.Name)
|
|
197
|
+
}
|
|
198
|
+
all_argument_names.update(
|
|
199
|
+
each_name.id
|
|
200
|
+
for each_keyword in each_call.keywords
|
|
201
|
+
for each_name in ast.walk(each_keyword.value)
|
|
202
|
+
if isinstance(each_name, ast.Name)
|
|
203
|
+
)
|
|
204
|
+
if (
|
|
205
|
+
"all_changed_lines" in all_argument_names
|
|
206
|
+
or "defer_scope_to_caller" in all_argument_names
|
|
207
|
+
):
|
|
208
|
+
all_rule_names.add(each_call.func.id)
|
|
209
|
+
return frozenset(all_rule_names)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
@pytest.mark.parametrize("narrow_edit_fixture", ALL_REQUIRED_RULE_FIXTURES)
|
|
213
|
+
def test_narrow_edit_selects_each_accepted_rule(
|
|
214
|
+
narrow_edit_fixture: NarrowEditFixture,
|
|
215
|
+
) -> None:
|
|
216
|
+
all_issues = _validate_narrow_edit(narrow_edit_fixture)
|
|
217
|
+
expected_rule_text = EXPECTED_RULE_TEXT_BY_NAME[narrow_edit_fixture.rule_name]
|
|
218
|
+
|
|
219
|
+
assert any(
|
|
220
|
+
narrow_edit_fixture.expected_marker in each_issue
|
|
221
|
+
and expected_rule_text in each_issue
|
|
222
|
+
for each_issue in all_issues
|
|
223
|
+
), (
|
|
224
|
+
"the changed fragment must select its checker and report its diagnostic marker "
|
|
225
|
+
f"{narrow_edit_fixture.expected_marker!r}, got {all_issues!r}"
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def test_scope_aware_inventory_matches_enforcer_dispatch() -> None:
|
|
230
|
+
assert _scope_aware_rule_names_from_source() == ALL_SCOPE_AWARE_RULE_NAMES
|
|
231
|
+
def test_narrow_edit_acceptance_set_is_source_backed_and_complete() -> None:
|
|
232
|
+
all_fixture_rule_names = {
|
|
233
|
+
each_fixture.rule_name for each_fixture in ALL_REQUIRED_RULE_FIXTURES
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
assert all_fixture_rule_names == NARROW_EDIT_ACCEPTED_RULE_NAMES
|
|
237
|
+
assert NARROW_EDIT_ACCEPTED_RULE_NAMES <= ALL_SCOPE_AWARE_RULE_NAMES
|
|
238
|
+
|
|
239
|
+
def test_narrow_edit_drops_an_untouched_banned_noun() -> None:
|
|
240
|
+
untouched_source = "OLD_RESULT_PATH = 0\n"
|
|
241
|
+
old_fragment = "PLACEHOLDER_NAME = 0\n"
|
|
242
|
+
new_fragment = "NEW_RESULT_PATH = 0\n"
|
|
243
|
+
issues = validate_content(
|
|
244
|
+
new_fragment,
|
|
245
|
+
PRODUCTION_FILE_PATH,
|
|
246
|
+
old_content=old_fragment,
|
|
247
|
+
full_file_content=untouched_source + new_fragment,
|
|
248
|
+
prior_full_file_content=untouched_source + old_fragment,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
assert any("NEW_RESULT_PATH" in each_issue for each_issue in issues)
|
|
252
|
+
assert not any("OLD_RESULT_PATH" in each_issue for each_issue in issues)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def test_narrow_edit_skips_python_rules_for_a_non_python_target() -> None:
|
|
256
|
+
source = "def process_data() -> None:\n print('payload')\n"
|
|
257
|
+
|
|
258
|
+
issues = validate_content(source, "packages/app/services.txt", old_content="")
|
|
259
|
+
|
|
260
|
+
assert issues == []
|
|
261
|
+
def _run_edit_stage(
|
|
262
|
+
file_path: Path,
|
|
263
|
+
old_fragment: str,
|
|
264
|
+
new_fragment: str,
|
|
265
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
266
|
+
capsys: pytest.CaptureFixture[str],
|
|
267
|
+
) -> str:
|
|
268
|
+
payload = json.dumps(
|
|
269
|
+
{
|
|
270
|
+
"tool_name": "Edit",
|
|
271
|
+
"tool_input": {
|
|
272
|
+
"file_path": str(file_path),
|
|
273
|
+
"old_string": old_fragment,
|
|
274
|
+
"new_string": new_fragment,
|
|
275
|
+
},
|
|
276
|
+
}
|
|
277
|
+
)
|
|
278
|
+
monkeypatch.setattr(sys, "stdin", io.StringIO(payload))
|
|
279
|
+
try:
|
|
280
|
+
main([])
|
|
281
|
+
except SystemExit:
|
|
282
|
+
pass
|
|
283
|
+
return capsys.readouterr().out
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def test_edit_entrypoint_reports_an_accepted_rule_for_the_changed_fragment(
|
|
287
|
+
tmp_path_factory: pytest.TempPathFactory,
|
|
288
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
289
|
+
capsys: pytest.CaptureFixture[str],
|
|
290
|
+
) -> None:
|
|
291
|
+
staging_directory = tmp_path_factory.mktemp("narrow_edit")
|
|
292
|
+
source_file = staging_directory / "service.py"
|
|
293
|
+
old_fragment = "def read_count() -> int:\n clean_count = 0\n return clean_count\n"
|
|
294
|
+
new_fragment = "def read_count() -> int:\n result_count = 0\n return result_count\n"
|
|
295
|
+
source_file.write_text(old_fragment, encoding="utf-8")
|
|
296
|
+
|
|
297
|
+
captured_stdout = _run_edit_stage(
|
|
298
|
+
source_file,
|
|
299
|
+
old_fragment,
|
|
300
|
+
new_fragment,
|
|
301
|
+
monkeypatch,
|
|
302
|
+
capsys,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
deny_payload = json.loads(captured_stdout)
|
|
306
|
+
deny_reason = deny_payload["hookSpecificOutput"]["permissionDecisionReason"]
|
|
307
|
+
assert deny_payload["hookSpecificOutput"]["permissionDecision"] == "deny"
|
|
308
|
+
assert "result_count" in deny_reason
|
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import io
|
|
6
5
|
import json
|
|
7
6
|
import subprocess
|
|
8
7
|
import sys
|
|
9
8
|
from pathlib import Path
|
|
10
|
-
from types import SimpleNamespace
|
|
11
9
|
|
|
12
10
|
_BLOCKING_DIRECTORY = str(Path(__file__).resolve().parent)
|
|
13
11
|
_HOOKS_DIRECTORY = str(Path(__file__).resolve().parent.parent)
|
|
@@ -20,11 +18,10 @@ from code_rules_enforcer import ( # noqa: E402
|
|
|
20
18
|
main,
|
|
21
19
|
validate_content,
|
|
22
20
|
)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
validate_content=validate_content,
|
|
21
|
+
from code_rules_enforcer_test_support import ( # noqa: E402
|
|
22
|
+
run_enforcer_cli,
|
|
23
|
+
run_precheck,
|
|
24
|
+
run_serialized_payload_entrypoint,
|
|
28
25
|
)
|
|
29
26
|
|
|
30
27
|
_ENFORCER_SCRIPT_PATH = Path(__file__).resolve().parent / "code_rules_enforcer.py"
|
|
@@ -57,13 +54,7 @@ def _run_enforcer_cli(
|
|
|
57
54
|
Returns:
|
|
58
55
|
The completed process carrying stdout, stderr, and the exit code.
|
|
59
56
|
"""
|
|
60
|
-
return
|
|
61
|
-
[sys.executable, str(_ENFORCER_SCRIPT_PATH), *all_cli_arguments],
|
|
62
|
-
input="",
|
|
63
|
-
capture_output=True,
|
|
64
|
-
text=True,
|
|
65
|
-
check=False,
|
|
66
|
-
)
|
|
57
|
+
return run_enforcer_cli(_ENFORCER_SCRIPT_PATH, all_cli_arguments, None)
|
|
67
58
|
|
|
68
59
|
|
|
69
60
|
def _run_precheck(
|
|
@@ -80,28 +71,15 @@ def _run_precheck(
|
|
|
80
71
|
Returns:
|
|
81
72
|
The completed process carrying the pre-check stdout, stderr, and exit code.
|
|
82
73
|
"""
|
|
83
|
-
return
|
|
74
|
+
return run_precheck(Path(candidate_path), target_path, None)
|
|
84
75
|
|
|
85
76
|
|
|
86
77
|
def _run_main_with_edit_payload(
|
|
87
78
|
file_path: str,
|
|
88
79
|
old_string: str,
|
|
89
80
|
new_string: str,
|
|
90
|
-
monkeypatch: object,
|
|
91
|
-
capsys: object,
|
|
92
81
|
) -> str:
|
|
93
|
-
"""Drive ``main()`` through its stdin entry point for an Edit
|
|
94
|
-
|
|
95
|
-
Args:
|
|
96
|
-
file_path: The on-disk path the Edit targets.
|
|
97
|
-
old_string: The Edit's ``old_string`` fragment.
|
|
98
|
-
new_string: The Edit's ``new_string`` fragment.
|
|
99
|
-
monkeypatch: The pytest fixture used to redirect ``sys.stdin``.
|
|
100
|
-
capsys: The pytest fixture used to capture the deny payload on stdout.
|
|
101
|
-
|
|
102
|
-
Returns:
|
|
103
|
-
The captured stdout, which holds the deny payload when violations fire.
|
|
104
|
-
"""
|
|
82
|
+
"""Drive ``main()`` through its stdin entry point for an Edit."""
|
|
105
83
|
edit_payload = json.dumps(
|
|
106
84
|
{
|
|
107
85
|
"tool_name": "Edit",
|
|
@@ -112,13 +90,8 @@ def _run_main_with_edit_payload(
|
|
|
112
90
|
},
|
|
113
91
|
}
|
|
114
92
|
)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
code_rules_enforcer.main([])
|
|
118
|
-
except SystemExit:
|
|
119
|
-
pass
|
|
120
|
-
captured = getattr(capsys, "readouterr")()
|
|
121
|
-
return captured.out
|
|
93
|
+
captured_stdout, _exit_code = run_serialized_payload_entrypoint(main, edit_payload)
|
|
94
|
+
return captured_stdout
|
|
122
95
|
|
|
123
96
|
|
|
124
97
|
def test_precheck_stream_parameter_carries_no_banned_noun() -> None:
|
|
@@ -130,9 +103,7 @@ def test_precheck_stream_parameter_carries_no_banned_noun() -> None:
|
|
|
130
103
|
banned-noun check fires. Scanning the enforcer source at a production path
|
|
131
104
|
proves the introduced stream parameter is free of that violation."""
|
|
132
105
|
enforcer_source = _ENFORCER_SCRIPT_PATH.read_text(encoding="utf-8")
|
|
133
|
-
issues = code_rules_enforcer.
|
|
134
|
-
enforcer_source, "/project/src/code_rules_enforcer.py"
|
|
135
|
-
)
|
|
106
|
+
issues = validate_content(enforcer_source, "/project/src/code_rules_enforcer.py")
|
|
136
107
|
banned_noun_issues = [
|
|
137
108
|
each_issue
|
|
138
109
|
for each_issue in issues
|
|
@@ -234,8 +205,6 @@ def test_precheck_missing_candidate_errors_on_stderr_without_traceback(
|
|
|
234
205
|
|
|
235
206
|
def test_edit_deny_reason_includes_forecast_and_precheck_hint(
|
|
236
207
|
tmp_path_factory: object,
|
|
237
|
-
monkeypatch: object,
|
|
238
|
-
capsys: object,
|
|
239
208
|
) -> None:
|
|
240
209
|
"""An Edit whose new_string introduces a violation on a file that already
|
|
241
210
|
contains a separate violation elsewhere blocks on the fragment violation and
|
|
@@ -244,9 +213,7 @@ def test_edit_deny_reason_includes_forecast_and_precheck_hint(
|
|
|
244
213
|
production_directory = getattr(tmp_path_factory, "mktemp")("production_pkg")
|
|
245
214
|
untouched_print_violation = _UNTOUCHED_PRINT_VIOLATION_SOURCE
|
|
246
215
|
clean_before = _CLEAN_FRAGMENT_BEFORE
|
|
247
|
-
introduces_banned_noun_after = (
|
|
248
|
-
"def short_helper() -> int:\n output = 0\n return output\n"
|
|
249
|
-
)
|
|
216
|
+
introduces_banned_noun_after = "def short_helper() -> int:\n output = 0\n return output\n"
|
|
250
217
|
on_disk_before = untouched_print_violation + "\n" + clean_before
|
|
251
218
|
source_file = production_directory / "production_module.py"
|
|
252
219
|
source_file.write_text(on_disk_before, encoding="utf-8")
|
|
@@ -254,8 +221,6 @@ def test_edit_deny_reason_includes_forecast_and_precheck_hint(
|
|
|
254
221
|
str(source_file),
|
|
255
222
|
clean_before,
|
|
256
223
|
introduces_banned_noun_after,
|
|
257
|
-
monkeypatch,
|
|
258
|
-
capsys,
|
|
259
224
|
)
|
|
260
225
|
deny_payload = json.loads(stdout)
|
|
261
226
|
reason = deny_payload["hookSpecificOutput"]["permissionDecisionReason"]
|
|
@@ -263,9 +228,7 @@ def test_edit_deny_reason_includes_forecast_and_precheck_hint(
|
|
|
263
228
|
assert "output" in blocking_section, (
|
|
264
229
|
f"fragment-introduced banned-noun must block, got reason: {reason!r}"
|
|
265
230
|
)
|
|
266
|
-
assert forecast_section != "",
|
|
267
|
-
f"forecast section must be present, got reason: {reason!r}"
|
|
268
|
-
)
|
|
231
|
+
assert forecast_section != "", f"forecast section must be present, got reason: {reason!r}"
|
|
269
232
|
assert "Library print()" in forecast_section, (
|
|
270
233
|
f"forecast must name the untouched print violation, got reason: {reason!r}"
|
|
271
234
|
)
|
|
@@ -274,8 +237,6 @@ def test_edit_deny_reason_includes_forecast_and_precheck_hint(
|
|
|
274
237
|
|
|
275
238
|
def test_edit_clean_fragment_on_dirty_file_produces_no_deny_payload(
|
|
276
239
|
tmp_path_factory: object,
|
|
277
|
-
monkeypatch: object,
|
|
278
|
-
capsys: object,
|
|
279
240
|
) -> None:
|
|
280
241
|
"""A clean Edit fragment on a file that is dirty elsewhere must not block —
|
|
281
242
|
the forecast never converts a clean-fragment edit into a deny."""
|
|
@@ -290,8 +251,6 @@ def test_edit_clean_fragment_on_dirty_file_produces_no_deny_payload(
|
|
|
290
251
|
str(source_file),
|
|
291
252
|
clean_before,
|
|
292
253
|
clean_after,
|
|
293
|
-
monkeypatch,
|
|
294
|
-
capsys,
|
|
295
254
|
)
|
|
296
255
|
assert stdout == "", (
|
|
297
256
|
f"a clean fragment on a dirty file must not produce a deny payload, got stdout: {stdout!r}"
|
|
@@ -300,8 +259,6 @@ def test_edit_clean_fragment_on_dirty_file_produces_no_deny_payload(
|
|
|
300
259
|
|
|
301
260
|
def test_every_deny_reason_carries_the_precheck_hint(
|
|
302
261
|
tmp_path_factory: object,
|
|
303
|
-
monkeypatch: object,
|
|
304
|
-
capsys: object,
|
|
305
262
|
) -> None:
|
|
306
263
|
"""A deny with no forecast still appends the pre-check hint to the reason."""
|
|
307
264
|
production_directory = getattr(tmp_path_factory, "mktemp")("production_pkg")
|
|
@@ -313,8 +270,6 @@ def test_every_deny_reason_carries_the_precheck_hint(
|
|
|
313
270
|
str(source_file),
|
|
314
271
|
clean_before,
|
|
315
272
|
introduces_violation_after,
|
|
316
|
-
monkeypatch,
|
|
317
|
-
capsys,
|
|
318
273
|
)
|
|
319
274
|
deny_payload = json.loads(stdout)
|
|
320
275
|
reason = deny_payload["hookSpecificOutput"]["permissionDecisionReason"]
|
|
@@ -333,8 +288,6 @@ def test_every_deny_reason_carries_the_precheck_hint(
|
|
|
333
288
|
|
|
334
289
|
def test_forecast_skipped_when_edit_prior_is_unreconstructable(
|
|
335
290
|
tmp_path_factory: object,
|
|
336
|
-
monkeypatch: object,
|
|
337
|
-
capsys: object,
|
|
338
291
|
) -> None:
|
|
339
292
|
"""An Edit whose old_string is absent from the file has no reliable prior to
|
|
340
293
|
diff against, so the full-file forecast must not run: a pre-existing inline
|
|
@@ -350,8 +303,6 @@ def test_forecast_skipped_when_edit_prior_is_unreconstructable(
|
|
|
350
303
|
str(source_file),
|
|
351
304
|
absent_old,
|
|
352
305
|
introduces_print_new,
|
|
353
|
-
monkeypatch,
|
|
354
|
-
capsys,
|
|
355
306
|
)
|
|
356
307
|
deny_payload = json.loads(stdout)
|
|
357
308
|
reason = deny_payload["hookSpecificOutput"]["permissionDecisionReason"]
|
|
@@ -363,8 +314,6 @@ def test_forecast_skipped_when_edit_prior_is_unreconstructable(
|
|
|
363
314
|
|
|
364
315
|
def test_forecast_omits_the_fragment_introduced_violation(
|
|
365
316
|
tmp_path_factory: object,
|
|
366
|
-
monkeypatch: object,
|
|
367
|
-
capsys: object,
|
|
368
317
|
) -> None:
|
|
369
318
|
"""An Edit that introduces the file's only print() blocks on it without the
|
|
370
319
|
forecast re-listing that same violation under its full-file line number."""
|
|
@@ -378,8 +327,6 @@ def test_forecast_omits_the_fragment_introduced_violation(
|
|
|
378
327
|
str(source_file),
|
|
379
328
|
_CLEAN_FRAGMENT_BEFORE,
|
|
380
329
|
introduces_print_after,
|
|
381
|
-
monkeypatch,
|
|
382
|
-
capsys,
|
|
383
330
|
)
|
|
384
331
|
deny_payload = json.loads(stdout)
|
|
385
332
|
reason = deny_payload["hookSpecificOutput"]["permissionDecisionReason"]
|
|
@@ -454,9 +401,7 @@ def test_precheck_strips_candidate_byte_order_mark_before_validation(
|
|
|
454
401
|
staging_directory = getattr(tmp_path_factory, "mktemp")("staging")
|
|
455
402
|
production_directory = getattr(tmp_path_factory, "mktemp")("production_pkg")
|
|
456
403
|
candidate_file = staging_directory / "candidate.py"
|
|
457
|
-
candidate_file.write_text(
|
|
458
|
-
"\ufeff" + _VIOLATING_PRODUCTION_SOURCE, encoding="utf-8"
|
|
459
|
-
)
|
|
404
|
+
candidate_file.write_text("\ufeff" + _VIOLATING_PRODUCTION_SOURCE, encoding="utf-8")
|
|
460
405
|
target_path = str(production_directory / "production_module.py")
|
|
461
406
|
completed = _run_precheck(str(candidate_file), target_path)
|
|
462
407
|
assert completed.returncode == 1, (
|
|
@@ -478,9 +423,7 @@ def test_precheck_strips_every_leading_byte_order_mark_before_validation(
|
|
|
478
423
|
staging_directory = getattr(tmp_path_factory, "mktemp")("staging")
|
|
479
424
|
production_directory = getattr(tmp_path_factory, "mktemp")("production_pkg")
|
|
480
425
|
candidate_file = staging_directory / "candidate.py"
|
|
481
|
-
candidate_file.write_text(
|
|
482
|
-
"\ufeff\ufeff" + _VIOLATING_PRODUCTION_SOURCE, encoding="utf-8"
|
|
483
|
-
)
|
|
426
|
+
candidate_file.write_text("\ufeff\ufeff" + _VIOLATING_PRODUCTION_SOURCE, encoding="utf-8")
|
|
484
427
|
target_path = str(production_directory / "production_module.py")
|
|
485
428
|
completed = _run_precheck(str(candidate_file), target_path)
|
|
486
429
|
assert completed.returncode == 1, (
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"""Tests for the session-scratchpad exemption wired into code_rules_enforcer.main."""
|
|
2
2
|
|
|
3
3
|
import importlib.util
|
|
4
|
-
import io
|
|
5
4
|
import json
|
|
6
5
|
import os
|
|
7
6
|
import sys
|
|
@@ -11,6 +10,13 @@ from types import ModuleType
|
|
|
11
10
|
|
|
12
11
|
import pytest
|
|
13
12
|
|
|
13
|
+
_HOOKS_DIRECTORY = str(Path(__file__).resolve().parent.parent)
|
|
14
|
+
if _HOOKS_DIRECTORY not in sys.path:
|
|
15
|
+
sys.path.insert(0, _HOOKS_DIRECTORY)
|
|
16
|
+
from blocking import _path_setup # noqa: F401
|
|
17
|
+
|
|
18
|
+
from code_rules_enforcer_test_support import run_serialized_payload_entrypoint
|
|
19
|
+
|
|
14
20
|
ENFORCER_PATH = Path(__file__).parent / "code_rules_enforcer.py"
|
|
15
21
|
FIXED_USER_ID = 6070
|
|
16
22
|
WORKING_DIRECTORY = "/home/user/project"
|
|
@@ -55,19 +61,11 @@ def _write_payload(target: Path) -> dict[str, object]:
|
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
|
|
58
|
-
def _run_main(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
monkeypatch.setattr(sys, "stdout", captured_stdout)
|
|
64
|
-
exit_code: int | None = None
|
|
65
|
-
try:
|
|
66
|
-
_ENFORCER.main([])
|
|
67
|
-
except SystemExit as raised_exit:
|
|
68
|
-
raw_code = raised_exit.code
|
|
69
|
-
exit_code = raw_code if isinstance(raw_code, int) else None
|
|
70
|
-
return exit_code, captured_stdout.getvalue()
|
|
64
|
+
def _run_main(payload_by_key: dict[str, object]) -> tuple[int | None, str]:
|
|
65
|
+
captured_stdout, exit_code = run_serialized_payload_entrypoint(
|
|
66
|
+
_ENFORCER.main, json.dumps(payload_by_key)
|
|
67
|
+
)
|
|
68
|
+
return exit_code, captured_stdout
|
|
71
69
|
|
|
72
70
|
|
|
73
71
|
def _decision_from(stdout_text: str) -> str | None:
|
|
@@ -84,7 +82,7 @@ def test_scratchpad_write_is_exempt_from_code_rules(
|
|
|
84
82
|
monkeypatch.setenv("CLAUDE_CODE_RULES_DISABLE_EPHEMERAL_EXEMPT", "1")
|
|
85
83
|
throwaway_script = scratchpad_directory / "one_off_probe.py"
|
|
86
84
|
|
|
87
|
-
exit_code, stdout_text = _run_main(
|
|
85
|
+
exit_code, stdout_text = _run_main(_write_payload(throwaway_script))
|
|
88
86
|
|
|
89
87
|
assert exit_code == 0
|
|
90
88
|
assert _decision_from(stdout_text) != "deny"
|
|
@@ -99,7 +97,7 @@ def test_identical_non_scratchpad_write_is_still_blocked(
|
|
|
99
97
|
outside_directory.mkdir(parents=True)
|
|
100
98
|
production_module = outside_directory / "one_off_probe.py"
|
|
101
99
|
|
|
102
|
-
exit_code, stdout_text = _run_main(
|
|
100
|
+
exit_code, stdout_text = _run_main(_write_payload(production_module))
|
|
103
101
|
|
|
104
102
|
assert exit_code == 0
|
|
105
103
|
assert _decision_from(stdout_text) == "deny"
|