claude-dev-env 8.100.1 → 8.101.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/hooks/advisory/edit_lint_context.py +292 -0
- package/hooks/advisory/test_edit_lint_context.py +269 -0
- package/hooks/features/post-write-validation.md +8 -4
- package/hooks/hooks.json +1 -1
- package/hooks/hooks_constants/edit_lint_context_constants.py +40 -0
- package/hooks/hooks_constants/post_tool_use_dispatcher_constants.py +4 -0
- package/hooks/validation/test_post_tool_use_dispatcher.py +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""PostToolUse hook that runs a repository's lint on each file the agent edits.
|
|
3
|
+
|
|
4
|
+
The agent edits ``tools/report.py`` in a repository that opts in. The hook runs
|
|
5
|
+
that repository's lint command on the file and hands the findings back to the
|
|
6
|
+
agent, so it fixes them in the same turn::
|
|
7
|
+
|
|
8
|
+
environment list: owner/repo
|
|
9
|
+
<repo>/.claude/edit-lint.json:
|
|
10
|
+
{"file_suffixes": [".py"],
|
|
11
|
+
"command": ["python", "lint.py", "--advisory", "{file}"]}
|
|
12
|
+
|
|
13
|
+
Edit tools/report.py -> additionalContext:
|
|
14
|
+
"The repository's lint found these problems in tools/report.py ..."
|
|
15
|
+
|
|
16
|
+
A repository takes part only when its origin ``owner/repo`` slug is in
|
|
17
|
+
``CLAUDE_EDIT_LINT_REPOSITORIES``, it commits ``.claude/edit-lint.json``, and
|
|
18
|
+
its root sits inside the session's project folder, the folder Claude Code's
|
|
19
|
+
workspace trust covers. A checkout elsewhere never runs its declared command.
|
|
20
|
+
The command runs from the repository root, and ``{file}`` in it becomes the
|
|
21
|
+
edited path with every symlink resolved. Its standard output is the findings; empty output adds nothing.
|
|
22
|
+
A command that exits non-zero, times out, or cannot start adds nothing and
|
|
23
|
+
writes one diagnostic line to stderr. The hook never blocks and never edits
|
|
24
|
+
the file.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import json
|
|
30
|
+
import os
|
|
31
|
+
import subprocess
|
|
32
|
+
import sys
|
|
33
|
+
from dataclasses import dataclass
|
|
34
|
+
from pathlib import Path
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
_hooks_directory = str(Path(__file__).resolve().parent.parent)
|
|
38
|
+
_blocking_directory = str(Path(_hooks_directory) / "blocking")
|
|
39
|
+
for each_bootstrap_directory in (_hooks_directory, _blocking_directory):
|
|
40
|
+
if each_bootstrap_directory not in sys.path:
|
|
41
|
+
sys.path.insert(0, each_bootstrap_directory)
|
|
42
|
+
from hooks_constants.edit_lint_context_constants import (
|
|
43
|
+
ALL_EDIT_TOOL_NAMES,
|
|
44
|
+
ALL_GIT_TOPLEVEL_COMMAND,
|
|
45
|
+
COMMAND_KEY,
|
|
46
|
+
CONFIG_RELATIVE_PATH,
|
|
47
|
+
FILE_PLACEHOLDER,
|
|
48
|
+
FILE_SUFFIXES_KEY,
|
|
49
|
+
FINDINGS_CONTEXT_TEMPLATE,
|
|
50
|
+
GIT_COMMAND_TIMEOUT_SECONDS,
|
|
51
|
+
LINT_COMMAND_TIMEOUT_SECONDS,
|
|
52
|
+
LINT_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
53
|
+
MAXIMUM_FINDINGS_CHARACTERS,
|
|
54
|
+
PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE,
|
|
55
|
+
REPOSITORIES_ENVIRONMENT_VARIABLE,
|
|
56
|
+
REPOSITORY_SEPARATOR,
|
|
57
|
+
TRUNCATION_NOTICE,
|
|
58
|
+
)
|
|
59
|
+
from hooks_constants.post_tool_use_context import write_post_tool_use_context_to_stdout
|
|
60
|
+
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
61
|
+
from hooks_constants.subprocess_window import hidden_window_creation_flags
|
|
62
|
+
from pii_prevention_blocker_parts.repository_exemption import repository_origin_slug
|
|
63
|
+
except ImportError as import_error:
|
|
64
|
+
raise ImportError(
|
|
65
|
+
"edit_lint_context: cannot import its sibling modules; "
|
|
66
|
+
"ensure the hooks directory is importable."
|
|
67
|
+
) from import_error
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True)
|
|
71
|
+
class EditLintConfig:
|
|
72
|
+
"""A repository's edit lint settings from ``.claude/edit-lint.json``.
|
|
73
|
+
|
|
74
|
+
Attributes:
|
|
75
|
+
all_file_suffixes: File suffixes the command lints, such as ``.py``.
|
|
76
|
+
all_command_parts: The command and its arguments, with ``{file}``
|
|
77
|
+
where the edited path goes.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
all_file_suffixes: tuple[str, ...]
|
|
81
|
+
all_command_parts: tuple[str, ...]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def opted_in_repository_slugs() -> frozenset[str]:
|
|
85
|
+
"""Return the lowercased ``owner/repo`` slugs the environment value lists.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
Each non-empty comma-separated entry of
|
|
89
|
+
``CLAUDE_EDIT_LINT_REPOSITORIES``, stripped and lowercased.
|
|
90
|
+
"""
|
|
91
|
+
listed = os.environ.get(REPOSITORIES_ENVIRONMENT_VARIABLE, "")
|
|
92
|
+
return frozenset(
|
|
93
|
+
each.strip().casefold() for each in listed.split(REPOSITORY_SEPARATOR) if each.strip()
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def repository_root_of(file_path: Path) -> Path | None:
|
|
98
|
+
"""Return the git work tree root that holds the file, or None.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
file_path: Absolute path of the edited file.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
The work tree root, or None when the file sits outside a git work tree
|
|
105
|
+
or git cannot run.
|
|
106
|
+
"""
|
|
107
|
+
try:
|
|
108
|
+
completed_process = subprocess.run(
|
|
109
|
+
list(ALL_GIT_TOPLEVEL_COMMAND),
|
|
110
|
+
check=False,
|
|
111
|
+
capture_output=True,
|
|
112
|
+
text=True,
|
|
113
|
+
timeout=GIT_COMMAND_TIMEOUT_SECONDS,
|
|
114
|
+
cwd=str(file_path.parent),
|
|
115
|
+
creationflags=hidden_window_creation_flags(),
|
|
116
|
+
)
|
|
117
|
+
except (subprocess.TimeoutExpired, OSError):
|
|
118
|
+
return None
|
|
119
|
+
if completed_process.returncode != 0:
|
|
120
|
+
return None
|
|
121
|
+
return Path(completed_process.stdout.strip())
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def is_inside_project_directory(repository_root: Path) -> bool:
|
|
125
|
+
"""Return whether the repository root sits inside the session's project folder.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
repository_root: The git work tree root.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
True when ``CLAUDE_PROJECT_DIR`` is set and the resolved root equals it
|
|
132
|
+
or sits below it. False when the variable is unset or the root lies
|
|
133
|
+
outside it.
|
|
134
|
+
"""
|
|
135
|
+
project_directory = os.environ.get(PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE, "")
|
|
136
|
+
if not project_directory:
|
|
137
|
+
return False
|
|
138
|
+
resolved_root = repository_root.resolve()
|
|
139
|
+
resolved_project = Path(project_directory).resolve()
|
|
140
|
+
return resolved_root == resolved_project or resolved_project in resolved_root.parents
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def load_edit_lint_config(repository_root: Path) -> EditLintConfig | None:
|
|
144
|
+
"""Read the repository's ``.claude/edit-lint.json``, or None when unusable.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
repository_root: The git work tree root.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
The parsed settings, or None when the file is missing, is not JSON, or
|
|
151
|
+
lacks a non-empty string list under ``file_suffixes`` or ``command``.
|
|
152
|
+
"""
|
|
153
|
+
try:
|
|
154
|
+
raw_config = json.loads(
|
|
155
|
+
(repository_root / CONFIG_RELATIVE_PATH).read_text(encoding="utf-8")
|
|
156
|
+
)
|
|
157
|
+
except (OSError, ValueError):
|
|
158
|
+
return None
|
|
159
|
+
if not isinstance(raw_config, dict):
|
|
160
|
+
return None
|
|
161
|
+
all_file_suffixes = _string_tuple(raw_config.get(FILE_SUFFIXES_KEY))
|
|
162
|
+
all_command_parts = _string_tuple(raw_config.get(COMMAND_KEY))
|
|
163
|
+
if not all_file_suffixes or not all_command_parts:
|
|
164
|
+
return None
|
|
165
|
+
return EditLintConfig(all_file_suffixes, all_command_parts)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _string_tuple(raw_value: object) -> tuple[str, ...]:
|
|
169
|
+
if not isinstance(raw_value, list):
|
|
170
|
+
return ()
|
|
171
|
+
if not all(isinstance(each, str) and each for each in raw_value):
|
|
172
|
+
return ()
|
|
173
|
+
return tuple(raw_value)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def lint_findings(config: EditLintConfig, repository_root: Path, file_path: Path) -> str:
|
|
177
|
+
"""Run the lint command on one file and return its findings text.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
config: The repository's edit lint settings.
|
|
181
|
+
repository_root: The directory the command runs from.
|
|
182
|
+
file_path: The edited file, put where ``{file}`` stands.
|
|
183
|
+
|
|
184
|
+
Returns:
|
|
185
|
+
The command's standard output, stripped. An empty string when the
|
|
186
|
+
command printed nothing, exited non-zero, timed out, or could not
|
|
187
|
+
start.
|
|
188
|
+
"""
|
|
189
|
+
completed_process = _run_lint_command(config, repository_root, file_path)
|
|
190
|
+
if completed_process is None:
|
|
191
|
+
return ""
|
|
192
|
+
if completed_process.returncode != 0:
|
|
193
|
+
last_error_line = (completed_process.stderr.strip().splitlines() or [""])[-1]
|
|
194
|
+
_write_failure_diagnostic(
|
|
195
|
+
file_path, f"exit {completed_process.returncode}: {last_error_line}"
|
|
196
|
+
)
|
|
197
|
+
return ""
|
|
198
|
+
return completed_process.stdout.strip()
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _run_lint_command(
|
|
202
|
+
config: EditLintConfig, repository_root: Path, file_path: Path
|
|
203
|
+
) -> subprocess.CompletedProcess[str] | None:
|
|
204
|
+
all_arguments = [
|
|
205
|
+
each_part.replace(FILE_PLACEHOLDER, str(file_path))
|
|
206
|
+
for each_part in config.all_command_parts
|
|
207
|
+
]
|
|
208
|
+
try:
|
|
209
|
+
return subprocess.run(
|
|
210
|
+
all_arguments,
|
|
211
|
+
check=False,
|
|
212
|
+
capture_output=True,
|
|
213
|
+
text=True,
|
|
214
|
+
encoding="utf-8",
|
|
215
|
+
errors="replace",
|
|
216
|
+
timeout=LINT_COMMAND_TIMEOUT_SECONDS,
|
|
217
|
+
cwd=str(repository_root),
|
|
218
|
+
creationflags=hidden_window_creation_flags(),
|
|
219
|
+
)
|
|
220
|
+
except subprocess.TimeoutExpired:
|
|
221
|
+
_write_failure_diagnostic(file_path, "timed out")
|
|
222
|
+
except OSError as start_error:
|
|
223
|
+
_write_failure_diagnostic(file_path, str(start_error))
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _write_failure_diagnostic(file_path: Path, failure_text: str) -> None:
|
|
228
|
+
sys.stderr.write(LINT_FAILURE_DIAGNOSTIC_TEMPLATE % (file_path, failure_text))
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def findings_context(file_label: str, findings_text: str) -> str:
|
|
232
|
+
"""Return the note the agent reads, with the findings cut to the size limit.
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
file_label: The edited file as the note names it.
|
|
236
|
+
findings_text: The lint command's findings.
|
|
237
|
+
|
|
238
|
+
Returns:
|
|
239
|
+
The note text.
|
|
240
|
+
"""
|
|
241
|
+
if len(findings_text) > MAXIMUM_FINDINGS_CHARACTERS:
|
|
242
|
+
findings_text = findings_text[:MAXIMUM_FINDINGS_CHARACTERS] + TRUNCATION_NOTICE.format(
|
|
243
|
+
limit=MAXIMUM_FINDINGS_CHARACTERS
|
|
244
|
+
)
|
|
245
|
+
return FINDINGS_CONTEXT_TEMPLATE.format(file=file_label, findings=findings_text)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _edited_file_path(all_hook_fields: dict[str, object]) -> Path | None:
|
|
249
|
+
if all_hook_fields.get("tool_name") not in ALL_EDIT_TOOL_NAMES:
|
|
250
|
+
return None
|
|
251
|
+
tool_input = all_hook_fields.get("tool_input")
|
|
252
|
+
if not isinstance(tool_input, dict):
|
|
253
|
+
return None
|
|
254
|
+
raw_path = tool_input.get("file_path")
|
|
255
|
+
if not isinstance(raw_path, str) or not raw_path:
|
|
256
|
+
return None
|
|
257
|
+
file_path = Path(raw_path)
|
|
258
|
+
if not file_path.is_absolute() or not file_path.is_file():
|
|
259
|
+
return None
|
|
260
|
+
return file_path.resolve()
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def main() -> None:
|
|
264
|
+
hook_input = read_hook_input_dictionary_from_stdin()
|
|
265
|
+
if hook_input is None:
|
|
266
|
+
return
|
|
267
|
+
all_opted_in_slugs = opted_in_repository_slugs()
|
|
268
|
+
if not all_opted_in_slugs:
|
|
269
|
+
return
|
|
270
|
+
file_path = _edited_file_path(hook_input)
|
|
271
|
+
if file_path is None:
|
|
272
|
+
return
|
|
273
|
+
repository_root = repository_root_of(file_path)
|
|
274
|
+
if repository_root is None or not is_inside_project_directory(repository_root):
|
|
275
|
+
return
|
|
276
|
+
if repository_origin_slug(repository_root) not in all_opted_in_slugs:
|
|
277
|
+
return
|
|
278
|
+
config = load_edit_lint_config(repository_root)
|
|
279
|
+
if config is None or file_path.suffix not in config.all_file_suffixes:
|
|
280
|
+
return
|
|
281
|
+
findings_text = lint_findings(config, repository_root, file_path)
|
|
282
|
+
if not findings_text:
|
|
283
|
+
return
|
|
284
|
+
try:
|
|
285
|
+
file_label = file_path.relative_to(repository_root.resolve()).as_posix()
|
|
286
|
+
except ValueError:
|
|
287
|
+
file_label = str(file_path)
|
|
288
|
+
write_post_tool_use_context_to_stdout(findings_context(file_label, findings_text))
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
if __name__ == "__main__":
|
|
292
|
+
main()
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""Behavior tests for the edit lint context hook.
|
|
2
|
+
|
|
3
|
+
Each test builds a disposable git repository whose origin points at a
|
|
4
|
+
github.com slug and whose ``.claude/edit-lint.json`` names a small lint script.
|
|
5
|
+
The script prints one finding for every line holding ``BAD``. The hook runs
|
|
6
|
+
through the PostToolUse dispatcher the way the live entry runs it.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
import pytest
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
advisory_directory = str(Path(__file__).resolve().parent)
|
|
21
|
+
if advisory_directory not in sys.path:
|
|
22
|
+
sys.path.insert(0, advisory_directory)
|
|
23
|
+
import edit_lint_context
|
|
24
|
+
except ImportError as import_error:
|
|
25
|
+
raise ImportError(
|
|
26
|
+
"test_edit_lint_context: cannot import its sibling modules; "
|
|
27
|
+
"ensure the advisory directory is importable."
|
|
28
|
+
) from import_error
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
|
|
32
|
+
DISPATCHER_PATH = HOOKS_DIRECTORY / "validation" / "post_tool_use_dispatcher.py"
|
|
33
|
+
HOOKS_JSON_PATH = HOOKS_DIRECTORY / "hooks.json"
|
|
34
|
+
ORIGIN_SLUG = "LintOwner/lint-repo"
|
|
35
|
+
ORIGIN_URL = f"https://github.com/{ORIGIN_SLUG}.git"
|
|
36
|
+
LINT_SCRIPT_SOURCE = (
|
|
37
|
+
"import sys\n"
|
|
38
|
+
"path = sys.argv[1]\n"
|
|
39
|
+
"for number, line in enumerate(open(path, encoding='utf-8'), 1):\n"
|
|
40
|
+
" if 'BAD' in line:\n"
|
|
41
|
+
" print(f'{path}:{number}: F821 undefined name')\n"
|
|
42
|
+
" if 'CRASH' in line:\n"
|
|
43
|
+
" sys.stderr.write('lint tool missing\\n')\n"
|
|
44
|
+
" sys.exit(3)\n"
|
|
45
|
+
)
|
|
46
|
+
DISPATCHER_TIMEOUT_SECONDS = 60
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _git_environment() -> dict[str, str]:
|
|
50
|
+
return {
|
|
51
|
+
each_name: each_value
|
|
52
|
+
for each_name, each_value in os.environ.items()
|
|
53
|
+
if not each_name.upper().startswith("GIT_")
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _run_git(repository_root: Path, *all_arguments: str) -> None:
|
|
58
|
+
subprocess.run(
|
|
59
|
+
["git", *all_arguments],
|
|
60
|
+
cwd=repository_root,
|
|
61
|
+
check=True,
|
|
62
|
+
capture_output=True,
|
|
63
|
+
env=_git_environment(),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@pytest.fixture
|
|
68
|
+
def lint_repository(tmp_path: Path) -> Path:
|
|
69
|
+
repository_root = tmp_path / "lint-repo"
|
|
70
|
+
repository_root.mkdir()
|
|
71
|
+
_run_git(repository_root, "init", "--quiet")
|
|
72
|
+
_run_git(repository_root, "remote", "add", "origin", ORIGIN_URL)
|
|
73
|
+
(repository_root / "lint.py").write_text(LINT_SCRIPT_SOURCE, encoding="utf-8")
|
|
74
|
+
config_path = repository_root / ".claude" / "edit-lint.json"
|
|
75
|
+
config_path.parent.mkdir()
|
|
76
|
+
config_path.write_text(
|
|
77
|
+
json.dumps({"file_suffixes": [".py"], "command": [sys.executable, "lint.py", "{file}"]}),
|
|
78
|
+
encoding="utf-8",
|
|
79
|
+
)
|
|
80
|
+
return repository_root
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _run_dispatcher(
|
|
84
|
+
tool_name: str,
|
|
85
|
+
file_path: Path,
|
|
86
|
+
listed_repositories: str | None,
|
|
87
|
+
project_directory: Path | None = None,
|
|
88
|
+
) -> str:
|
|
89
|
+
environment = _git_environment()
|
|
90
|
+
environment["CLAUDE_PROJECT_DIR"] = str(project_directory or file_path.parents[-2])
|
|
91
|
+
environment.pop("CLAUDE_EDIT_LINT_REPOSITORIES", None)
|
|
92
|
+
if listed_repositories is not None:
|
|
93
|
+
environment["CLAUDE_EDIT_LINT_REPOSITORIES"] = listed_repositories
|
|
94
|
+
completed_process = subprocess.run(
|
|
95
|
+
[sys.executable, str(DISPATCHER_PATH)],
|
|
96
|
+
input=json.dumps({"tool_name": tool_name, "tool_input": {"file_path": str(file_path)}}),
|
|
97
|
+
capture_output=True,
|
|
98
|
+
text=True,
|
|
99
|
+
env=environment,
|
|
100
|
+
timeout=DISPATCHER_TIMEOUT_SECONDS,
|
|
101
|
+
check=False,
|
|
102
|
+
)
|
|
103
|
+
assert completed_process.returncode == 0, completed_process.stderr
|
|
104
|
+
return completed_process.stdout.strip()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _additional_context(dispatcher_output: str) -> str:
|
|
108
|
+
return json.loads(dispatcher_output)["hookSpecificOutput"]["additionalContext"]
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def should_add_findings_after_an_edit_in_an_opted_in_repository(lint_repository: Path) -> None:
|
|
112
|
+
edited_file = lint_repository / "module.py"
|
|
113
|
+
edited_file.write_text("VALUE = 1\nRESULT = BAD\n", encoding="utf-8")
|
|
114
|
+
|
|
115
|
+
note = _additional_context(_run_dispatcher("Edit", edited_file, ORIGIN_SLUG.lower()))
|
|
116
|
+
|
|
117
|
+
assert "module.py:2: F821 undefined name" in note
|
|
118
|
+
assert note.startswith("The repository's lint found these problems in module.py")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def should_add_findings_after_a_write_and_a_multi_edit(lint_repository: Path) -> None:
|
|
122
|
+
edited_file = lint_repository / "module.py"
|
|
123
|
+
edited_file.write_text("RESULT = BAD\n", encoding="utf-8")
|
|
124
|
+
|
|
125
|
+
for each_tool_name in ("Write", "MultiEdit"):
|
|
126
|
+
note = _additional_context(_run_dispatcher(each_tool_name, edited_file, ORIGIN_SLUG))
|
|
127
|
+
assert "module.py:1: F821" in note
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def should_pass_the_lint_command_the_path_with_symlinks_resolved(
|
|
131
|
+
lint_repository: Path, tmp_path: Path
|
|
132
|
+
) -> None:
|
|
133
|
+
package_directory = lint_repository / "package"
|
|
134
|
+
package_directory.mkdir()
|
|
135
|
+
(package_directory / "module.py").write_text("RESULT = BAD\n", encoding="utf-8")
|
|
136
|
+
linked_directory = tmp_path / "linked-package"
|
|
137
|
+
linked_directory.symlink_to(package_directory, target_is_directory=True)
|
|
138
|
+
|
|
139
|
+
note = _additional_context(
|
|
140
|
+
_run_dispatcher("Edit", linked_directory / "module.py", ORIGIN_SLUG)
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
assert f"{(package_directory / 'module.py').resolve()}:1: F821" in note
|
|
144
|
+
assert "linked-package" not in note
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def should_add_nothing_when_the_environment_lists_no_repository(lint_repository: Path) -> None:
|
|
148
|
+
edited_file = lint_repository / "module.py"
|
|
149
|
+
edited_file.write_text("RESULT = BAD\n", encoding="utf-8")
|
|
150
|
+
|
|
151
|
+
assert _run_dispatcher("Edit", edited_file, None) == ""
|
|
152
|
+
assert _run_dispatcher("Edit", edited_file, "other/repo") == ""
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def should_add_nothing_for_a_repository_outside_the_project_folder(
|
|
156
|
+
lint_repository: Path, tmp_path: Path
|
|
157
|
+
) -> None:
|
|
158
|
+
edited_file = lint_repository / "module.py"
|
|
159
|
+
edited_file.write_text("RESULT = BAD\n", encoding="utf-8")
|
|
160
|
+
other_project = tmp_path / "other-project"
|
|
161
|
+
other_project.mkdir()
|
|
162
|
+
|
|
163
|
+
assert _run_dispatcher("Edit", edited_file, ORIGIN_SLUG, other_project) == ""
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def should_accept_a_root_inside_the_project_folder(
|
|
167
|
+
lint_repository: Path, monkeypatch: pytest.MonkeyPatch
|
|
168
|
+
) -> None:
|
|
169
|
+
monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(lint_repository.parent))
|
|
170
|
+
assert edit_lint_context.is_inside_project_directory(lint_repository)
|
|
171
|
+
|
|
172
|
+
monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(lint_repository / "package"))
|
|
173
|
+
assert not edit_lint_context.is_inside_project_directory(lint_repository)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def should_add_nothing_after_a_clean_edit(lint_repository: Path) -> None:
|
|
177
|
+
edited_file = lint_repository / "module.py"
|
|
178
|
+
edited_file.write_text("VALUE = 1\n", encoding="utf-8")
|
|
179
|
+
|
|
180
|
+
assert _run_dispatcher("Edit", edited_file, ORIGIN_SLUG) == ""
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def should_add_nothing_for_a_suffix_the_repository_does_not_lint(lint_repository: Path) -> None:
|
|
184
|
+
edited_file = lint_repository / "notes.md"
|
|
185
|
+
edited_file.write_text("BAD\n", encoding="utf-8")
|
|
186
|
+
|
|
187
|
+
assert _run_dispatcher("Edit", edited_file, ORIGIN_SLUG) == ""
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def should_add_nothing_when_the_lint_command_fails(lint_repository: Path) -> None:
|
|
191
|
+
edited_file = lint_repository / "module.py"
|
|
192
|
+
edited_file.write_text("RESULT = BAD\nCRASH\n", encoding="utf-8")
|
|
193
|
+
|
|
194
|
+
assert _run_dispatcher("Edit", edited_file, ORIGIN_SLUG) == ""
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def should_add_nothing_for_a_file_outside_a_git_repository(tmp_path: Path) -> None:
|
|
198
|
+
loose_file = tmp_path / "loose.py"
|
|
199
|
+
loose_file.write_text("RESULT = BAD\n", encoding="utf-8")
|
|
200
|
+
|
|
201
|
+
assert _run_dispatcher("Edit", loose_file, ORIGIN_SLUG) == ""
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def should_register_the_dispatcher_for_edit_write_and_multi_edit() -> None:
|
|
205
|
+
registrations = json.loads(HOOKS_JSON_PATH.read_text(encoding="utf-8"))
|
|
206
|
+
all_matchers = [
|
|
207
|
+
each_group["matcher"]
|
|
208
|
+
for each_group in registrations["hooks"]["PostToolUse"]
|
|
209
|
+
if any(
|
|
210
|
+
"post_tool_use_dispatcher.py" in each_hook["command"]
|
|
211
|
+
for each_hook in each_group["hooks"]
|
|
212
|
+
)
|
|
213
|
+
]
|
|
214
|
+
|
|
215
|
+
assert all_matchers == ["Write|Edit|MultiEdit"]
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def should_read_listed_slugs_lowercased(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
219
|
+
monkeypatch.setenv("CLAUDE_EDIT_LINT_REPOSITORIES", " Owner/One , ,owner/two")
|
|
220
|
+
|
|
221
|
+
assert edit_lint_context.opted_in_repository_slugs() == frozenset({"owner/one", "owner/two"})
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def should_find_the_work_tree_root_of_a_nested_file(lint_repository: Path) -> None:
|
|
225
|
+
nested_directory = lint_repository / "package"
|
|
226
|
+
nested_directory.mkdir()
|
|
227
|
+
nested_file = nested_directory / "inner.py"
|
|
228
|
+
nested_file.write_text("VALUE = 1\n", encoding="utf-8")
|
|
229
|
+
|
|
230
|
+
found_root = edit_lint_context.repository_root_of(nested_file)
|
|
231
|
+
|
|
232
|
+
assert found_root is not None
|
|
233
|
+
assert found_root.resolve() == lint_repository.resolve()
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def should_reject_a_config_without_a_command(lint_repository: Path) -> None:
|
|
237
|
+
config_path = lint_repository / ".claude" / "edit-lint.json"
|
|
238
|
+
config_path.write_text(json.dumps({"file_suffixes": [".py"], "command": []}), encoding="utf-8")
|
|
239
|
+
|
|
240
|
+
assert edit_lint_context.load_edit_lint_config(lint_repository) is None
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def should_load_the_suffixes_and_command(lint_repository: Path) -> None:
|
|
244
|
+
config = edit_lint_context.load_edit_lint_config(lint_repository)
|
|
245
|
+
|
|
246
|
+
assert config == edit_lint_context.EditLintConfig(
|
|
247
|
+
(".py",), (sys.executable, "lint.py", "{file}")
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def should_return_the_lint_output_for_one_file(lint_repository: Path) -> None:
|
|
252
|
+
edited_file = lint_repository / "module.py"
|
|
253
|
+
edited_file.write_text("RESULT = BAD\n", encoding="utf-8")
|
|
254
|
+
config = edit_lint_context.load_edit_lint_config(lint_repository)
|
|
255
|
+
assert config is not None
|
|
256
|
+
|
|
257
|
+
assert edit_lint_context.lint_findings(config, lint_repository, edited_file) == (
|
|
258
|
+
f"{edited_file}:1: F821 undefined name"
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def should_cut_long_findings_to_the_limit() -> None:
|
|
263
|
+
long_findings = "x" * 5000
|
|
264
|
+
|
|
265
|
+
note = edit_lint_context.findings_context("module.py", long_findings)
|
|
266
|
+
|
|
267
|
+
assert "x" * 4000 in note
|
|
268
|
+
assert "x" * 4001 not in note
|
|
269
|
+
assert note.endswith("[edit lint output cut at 4000 characters]")
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
# Post-write validation
|
|
2
2
|
|
|
3
|
-
This family checks a completed Write or
|
|
3
|
+
This family checks a completed Write, Edit or MultiEdit call, formats eligible new source files, and runs an opted-in repository's lint on the edited file. The agent receives a combined block decision if a hosted check blocks.
|
|
4
4
|
|
|
5
5
|
## Checks
|
|
6
6
|
|
|
7
7
|
- `validation/post_tool_use_dispatcher.py` runs the post-write roster in order and combines any block reasons.
|
|
8
8
|
- `workflow/auto_formatter.py` formats eligible untracked source files after a Write and never blocks the write.
|
|
9
|
+
- `advisory/edit_lint_context.py` runs the lint command a repository names in `.claude/edit-lint.json` on the edited file and adds its findings to the agent's context. It runs only for a repository whose origin `owner/repo` slug is listed in `CLAUDE_EDIT_LINT_REPOSITORIES`, comma-separated, and whose root sits inside `CLAUDE_PROJECT_DIR`, the folder the session's workspace trust covers. It never blocks and never edits the file.
|
|
9
10
|
|
|
10
11
|
## When it fires
|
|
11
12
|
|
|
12
|
-
- `validation/post_tool_use_dispatcher.py` runs on `PostToolUse`, matcher `Write|Edit`, timeout `180` seconds in `hooks.json`.
|
|
13
|
-
- `workflow/auto_formatter.py` runs inside that dispatcher on `PostToolUse`, matcher `Write|Edit`, timeout `180` seconds, as named by `ALL_POST_HOSTED_HOOK_ENTRIES`. Its eligibility check accepts only a Write of an untracked source file.
|
|
13
|
+
- `validation/post_tool_use_dispatcher.py` runs on `PostToolUse`, matcher `Write|Edit|MultiEdit`, timeout `180` seconds in `hooks.json`.
|
|
14
|
+
- `workflow/auto_formatter.py` runs inside that dispatcher on `PostToolUse`, matcher `Write|Edit|MultiEdit`, timeout `180` seconds, as named by `ALL_POST_HOSTED_HOOK_ENTRIES`. Its eligibility check accepts only a Write of an untracked source file.
|
|
15
|
+
- `advisory/edit_lint_context.py` runs inside that dispatcher after the formatter, so it lints the file as the formatter left it.
|
|
14
16
|
|
|
15
17
|
## Proving it
|
|
16
18
|
|
|
@@ -19,10 +21,12 @@ Preconditions:
|
|
|
19
21
|
- Run each command from the repository root with Python and pytest available. The formatter tests make disposable repositories.
|
|
20
22
|
|
|
21
23
|
- **Post-write dispatch.** Input is an Edit of plain text. Run `python -m pytest packages/claude-dev-env/hooks/validation/test_post_tool_use_dispatcher.py -q`. The adjacent test observes an allow result and the hosted formatter's selection.
|
|
24
|
+
- **Edit lint context.** Input is an Edit, Write or MultiEdit of a file in a disposable repository that lists a lint script. Run `python -m pytest packages/claude-dev-env/hooks/advisory/test_edit_lint_context.py -q`. The adjacent test observes the findings note for an opted-in repository and no output for a clean edit, an unlisted repository, an unlisted suffix, or a failed lint command.
|
|
22
25
|
- **Formatter eligibility.** Input is a Write of an untracked source file. Run `python -m pytest packages/claude-dev-env/hooks/workflow/test_auto_formatter.py -q`. The adjacent test observes formatting for eligible writes and no edit of a tracked file.
|
|
23
26
|
|
|
24
27
|
## Gotchas
|
|
25
28
|
|
|
26
|
-
- The current post-write roster contains
|
|
29
|
+
- The current post-write roster contains `workflow/auto_formatter.py` and `advisory/edit_lint_context.py`. The dispatcher's docstring mentions other hosted work from an older roster, so use the roster for membership.
|
|
27
30
|
- The formatter can change the file after the original Write. A later hosted script would read the formatted file because the dispatcher preserves roster order.
|
|
28
31
|
- Formatter eligibility protects the hook tree and checks whether the file is tracked.
|
|
32
|
+
- The edit lint command's standard output is the findings. A repository's command prints nothing on a clean file and sends progress lines to stderr. A non-zero exit adds no note and writes one diagnostic line to stderr, so the command runs the lint in a mode that exits 0 when it reports findings.
|
package/hooks/hooks.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Constants for the PostToolUse hook that lints each edited file."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"REPOSITORIES_ENVIRONMENT_VARIABLE",
|
|
7
|
+
"REPOSITORY_SEPARATOR",
|
|
8
|
+
"PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE",
|
|
9
|
+
"CONFIG_RELATIVE_PATH",
|
|
10
|
+
"FILE_SUFFIXES_KEY",
|
|
11
|
+
"COMMAND_KEY",
|
|
12
|
+
"FILE_PLACEHOLDER",
|
|
13
|
+
"ALL_EDIT_TOOL_NAMES",
|
|
14
|
+
"ALL_GIT_TOPLEVEL_COMMAND",
|
|
15
|
+
"GIT_COMMAND_TIMEOUT_SECONDS",
|
|
16
|
+
"LINT_COMMAND_TIMEOUT_SECONDS",
|
|
17
|
+
"MAXIMUM_FINDINGS_CHARACTERS",
|
|
18
|
+
"TRUNCATION_NOTICE",
|
|
19
|
+
"FINDINGS_CONTEXT_TEMPLATE",
|
|
20
|
+
"LINT_FAILURE_DIAGNOSTIC_TEMPLATE",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
REPOSITORIES_ENVIRONMENT_VARIABLE = "CLAUDE_EDIT_LINT_REPOSITORIES"
|
|
24
|
+
REPOSITORY_SEPARATOR = ","
|
|
25
|
+
PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE = "CLAUDE_PROJECT_DIR"
|
|
26
|
+
CONFIG_RELATIVE_PATH = ".claude/edit-lint.json"
|
|
27
|
+
FILE_SUFFIXES_KEY = "file_suffixes"
|
|
28
|
+
COMMAND_KEY = "command"
|
|
29
|
+
FILE_PLACEHOLDER = "{file}"
|
|
30
|
+
ALL_EDIT_TOOL_NAMES = frozenset({"Write", "Edit", "MultiEdit"})
|
|
31
|
+
ALL_GIT_TOPLEVEL_COMMAND = ("git", "rev-parse", "--show-toplevel")
|
|
32
|
+
GIT_COMMAND_TIMEOUT_SECONDS = 10
|
|
33
|
+
LINT_COMMAND_TIMEOUT_SECONDS = 60
|
|
34
|
+
MAXIMUM_FINDINGS_CHARACTERS = 4000
|
|
35
|
+
TRUNCATION_NOTICE = "\n[edit lint output cut at {limit} characters]"
|
|
36
|
+
FINDINGS_CONTEXT_TEMPLATE = (
|
|
37
|
+
"The repository's lint found these problems in {file} after this edit. "
|
|
38
|
+
"Fix them in this turn:\n{findings}"
|
|
39
|
+
)
|
|
40
|
+
LINT_FAILURE_DIAGNOSTIC_TEMPLATE = "[edit_lint_context] lint command for %s did not finish: %s\n"
|
|
@@ -366,4 +366,5 @@ def test_live_roster_contains_only_nonblocking_hooks() -> None:
|
|
|
366
366
|
assert all(not each_entry.is_blocking for each_entry in ALL_POST_HOSTED_HOOK_ENTRIES)
|
|
367
367
|
assert tuple(each_entry.script_relative_path for each_entry in ALL_POST_HOSTED_HOOK_ENTRIES) == (
|
|
368
368
|
"workflow/auto_formatter.py",
|
|
369
|
+
"advisory/edit_lint_context.py",
|
|
369
370
|
)
|