claude-dev-env 1.40.0 → 1.42.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 (66) hide show
  1. package/CLAUDE.md +9 -1
  2. package/_shared/pr-loop/scripts/_claude_permissions_common.py +231 -3
  3. package/_shared/pr-loop/scripts/config/claude_permissions_constants.py +56 -2
  4. package/_shared/pr-loop/scripts/config/claude_settings_keys_constants.py +2 -0
  5. package/_shared/pr-loop/scripts/grant_project_claude_permissions.py +173 -6
  6. package/_shared/pr-loop/scripts/post_audit_thread.py +2 -2
  7. package/_shared/pr-loop/scripts/revoke_project_claude_permissions.py +135 -14
  8. package/_shared/pr-loop/scripts/tests/test_agent_config_carveout.py +385 -0
  9. package/_shared/pr-loop/scripts/tests/test_claude_permissions_constants.py +33 -0
  10. package/_shared/pr-loop/scripts/tests/test_grant_project_claude_permissions.py +1 -1
  11. package/_shared/pr-loop/scripts/tests/test_revoke_project_claude_permissions.py +4 -2
  12. package/hooks/_gh_pr_author_swap_utils.py +1211 -0
  13. package/hooks/blocking/gh_body_arg_blocker.py +9 -6
  14. package/hooks/blocking/gh_pr_author_enforcer.py +480 -0
  15. package/hooks/blocking/gh_pr_author_restore.py +100 -0
  16. package/hooks/blocking/pr_converge_bugteam_enforcer.py +170 -0
  17. package/hooks/blocking/pr_description_enforcer.py +1 -3
  18. package/hooks/blocking/test_gh_body_arg_blocker.py +25 -3
  19. package/hooks/blocking/test_gh_pr_author_enforcer.py +1166 -0
  20. package/hooks/blocking/test_gh_pr_author_restore.py +512 -0
  21. package/hooks/blocking/test_gh_pr_author_swap_utils.py +910 -0
  22. package/hooks/blocking/test_pr_converge_bugteam_enforcer.py +311 -0
  23. package/hooks/config/gh_pr_author_swap_constants.py +76 -0
  24. package/hooks/config/pr_converge_bugteam_enforcer_constants.py +55 -0
  25. package/hooks/config/pr_converge_bugteam_enforcer_state.py +67 -0
  26. package/hooks/config/pr_description_enforcer_constants.py +5 -0
  27. package/hooks/config/test_pr_description_enforcer_constants.py +82 -0
  28. package/hooks/hooks.json +40 -0
  29. package/hooks/lifecycle/pr_converge_bugteam_skill_tracker.py +204 -0
  30. package/hooks/lifecycle/test_pr_converge_bugteam_skill_tracker.py +283 -0
  31. package/hooks/session/gh_pr_author_session_cleanup.py +171 -0
  32. package/hooks/session/test_gh_pr_author_session_cleanup.py +575 -0
  33. package/hooks/test__gh_pr_author_swap_utils.py +333 -0
  34. package/package.json +1 -1
  35. package/skills/_shared/pr-loop/scripts/write_audit_outcomes.py +2 -2
  36. package/skills/_shared/pr-loop/scripts/write_fix_outcomes.py +2 -2
  37. package/skills/bugteam/reference/audit-contract.md +22 -0
  38. package/skills/bugteam/reference/github-pr-reviews.md +1 -1
  39. package/skills/bugteam/scripts/_claude_permissions_common.py +109 -0
  40. package/skills/bugteam/scripts/bugteam_fix_hookspath.py +8 -2
  41. package/skills/bugteam/scripts/config/claude_permissions_common_constants.py +51 -2
  42. package/skills/bugteam/scripts/grant_project_claude_permissions.py +115 -4
  43. package/skills/bugteam/scripts/revoke_project_claude_permissions.py +69 -17
  44. package/skills/bugteam/scripts/test__claude_permissions_common.py +48 -0
  45. package/skills/bugteam/scripts/test_agent_config_carveout.py +356 -0
  46. package/skills/bugteam/scripts/test_claude_permissions_common.py +18 -10
  47. package/skills/implement/SKILL.md +66 -0
  48. package/skills/implement/scripts/append_note.py +133 -0
  49. package/skills/implement/scripts/config/__init__.py +0 -0
  50. package/skills/implement/scripts/config/notes_constants.py +12 -0
  51. package/skills/implement/scripts/test_append_note.py +191 -0
  52. package/skills/pr-converge/SKILL.md +8 -2
  53. package/skills/pr-converge/config/constants.py +7 -1
  54. package/skills/pr-converge/reference/state-schema.md +36 -8
  55. package/skills/pr-converge/scripts/check_bugbot_ci.py +1 -1
  56. package/skills/pr-converge/scripts/check_convergence.py +167 -28
  57. package/skills/pr-converge/scripts/check_pending_reviews.py +1 -1
  58. package/skills/pr-converge/scripts/conftest.py +60 -0
  59. package/skills/pr-converge/scripts/fetch_copilot_reviews.py +1 -1
  60. package/skills/pr-converge/scripts/post_fix_reply.py +1 -1
  61. package/skills/pr-converge/scripts/test_check_bugbot_ci.py +1 -1
  62. package/skills/pr-converge/scripts/test_check_convergence.py +306 -0
  63. package/skills/pr-converge/scripts/test_evict_cached_config_modules.py +1 -1
  64. package/skills/refine/SKILL.md +257 -0
  65. package/skills/refine/templates/implementation-notes-template.html +56 -0
  66. package/skills/refine/templates/plan-template.md +60 -0
@@ -0,0 +1,333 @@
1
+ """Canonical-location tests for the gh-pr-author swap utils module.
2
+
3
+ The TDD enforcer matches a production filename ``X.py`` to ``test_X.py``;
4
+ ``_gh_pr_author_swap_utils.py`` carries a leading underscore that the
5
+ enforcer treats as part of the name. This file's tests are the canonical
6
+ match. The broader behavioural suite lives alongside in
7
+ ``blocking/test_gh_pr_author_swap_utils.py``.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import importlib.util
13
+ import os
14
+ import pathlib
15
+ import sys
16
+ import tempfile
17
+
18
+ import pytest
19
+
20
+ from config.gh_pr_author_swap_constants import STATE_FILE_PERMISSION_MODE
21
+
22
+ _HOOKS_ROOT = pathlib.Path(__file__).resolve().parent
23
+ if str(_HOOKS_ROOT) not in sys.path:
24
+ sys.path.insert(0, str(_HOOKS_ROOT))
25
+
26
+ utils_module_spec = importlib.util.spec_from_file_location(
27
+ "_gh_pr_author_swap_utils",
28
+ _HOOKS_ROOT / "_gh_pr_author_swap_utils.py",
29
+ )
30
+ assert utils_module_spec is not None
31
+ assert utils_module_spec.loader is not None
32
+ utils_module = importlib.util.module_from_spec(utils_module_spec)
33
+ utils_module_spec.loader.exec_module(utils_module)
34
+
35
+
36
+ def test_state_file_path_rejects_path_traversal_session_id() -> None:
37
+ """A session_id containing path-traversal characters must not escape tempdir.
38
+
39
+ Regression guard: an unsanitised ``session_id`` containing ``../`` or
40
+ ``/`` would interpolate into the filename and let the resulting path
41
+ land outside ``tempfile.gettempdir()``. The sanitiser strips every
42
+ character outside ``[A-Za-z0-9_-]`` and falls back to the default
43
+ session id when the result is empty.
44
+ """
45
+ sanitised_path = utils_module._state_file_path("../../tmp/evil")
46
+ temporary_directory_path = pathlib.Path(tempfile.gettempdir()).resolve()
47
+ assert sanitised_path.parent.resolve() == temporary_directory_path
48
+
49
+
50
+ def test_state_file_path_rejects_backslash_in_session_id() -> None:
51
+ """Backslashes are also unsafe path separators on Windows."""
52
+ sanitised_path = utils_module._state_file_path("evil\\..\\..\\system32")
53
+ temporary_directory_path = pathlib.Path(tempfile.gettempdir()).resolve()
54
+ assert sanitised_path.parent.resolve() == temporary_directory_path
55
+
56
+
57
+ def test_state_file_path_rejects_nul_byte_in_session_id() -> None:
58
+ """A NUL byte inside the session id must not reach the filename."""
59
+ sanitised_path = utils_module._state_file_path("abc\x00../def")
60
+ temporary_directory_path = pathlib.Path(tempfile.gettempdir()).resolve()
61
+ assert sanitised_path.parent.resolve() == temporary_directory_path
62
+ assert "\x00" not in sanitised_path.name
63
+
64
+
65
+ def test_state_file_path_preserves_safe_session_id() -> None:
66
+ """A well-formed session id passes through unchanged."""
67
+ safe_session_id = "session-001_abc"
68
+ produced_path = utils_module._state_file_path(safe_session_id)
69
+ assert safe_session_id in produced_path.name
70
+
71
+
72
+ def test_backtick_substitution_blanks_inner_quoted_literals() -> None:
73
+ """A ``gh pr create`` literal inside a single-quoted argument of a backtick body must not trigger.
74
+
75
+ Mirrors ``$(printf '...')`` behaviour: when the backtick body's
76
+ quoted literal contains the token, the matcher must blank the
77
+ quoted region before searching.
78
+ """
79
+ stripped_command = utils_module._strip_quoted_regions("foo `printf ';gh pr create'`")
80
+ assert not utils_module._command_invokes_gh_pr_create_in_stripped(stripped_command)
81
+
82
+
83
+ def test_backtick_substitution_matches_unquoted_gh_pr_create_inside_body() -> None:
84
+ """A bare ``gh pr create`` inside a backtick body still matches.
85
+
86
+ Symmetric to ``$(gh pr create)`` — the substitution body is real
87
+ code, so the matcher must see it.
88
+ """
89
+ stripped_command = utils_module._strip_quoted_regions("echo `gh pr create --title T`")
90
+ assert utils_module._command_invokes_gh_pr_create_in_stripped(stripped_command)
91
+
92
+
93
+ def test_state_file_is_attacker_planted_returns_true_for_world_readable_mode(
94
+ tmp_path: pathlib.Path,
95
+ ) -> None:
96
+ """A state file with mode 0o644 is flagged as attacker-planted on POSIX.
97
+
98
+ The enforcer always atomically creates state files at 0o600. A file
99
+ at the predictable swap-state path with any other mode bits cannot
100
+ have come from the enforcer running as this user.
101
+ """
102
+ if not hasattr(os, "getuid"):
103
+ return
104
+ state_file = tmp_path / "gh_pr_author_swap_session-attacker.json"
105
+ state_file.write_text("{}", encoding="utf-8")
106
+ os.chmod(state_file, 0o644)
107
+
108
+ assert utils_module._state_file_is_attacker_planted(state_file) is True
109
+
110
+
111
+ def test_state_file_is_attacker_planted_returns_false_for_well_formed_file(
112
+ tmp_path: pathlib.Path,
113
+ ) -> None:
114
+ """A state file written exactly the way the enforcer writes is not flagged."""
115
+ state_file = tmp_path / "gh_pr_author_swap_session-good.json"
116
+ state_file.write_text("{}", encoding="utf-8")
117
+ if hasattr(os, "getuid"):
118
+ os.chmod(state_file, STATE_FILE_PERMISSION_MODE)
119
+
120
+ assert utils_module._state_file_is_attacker_planted(state_file) is False
121
+
122
+
123
+ def test_state_file_is_attacker_planted_returns_false_for_missing_file(
124
+ tmp_path: pathlib.Path,
125
+ ) -> None:
126
+ """A missing state file is treated as not-planted so callers can no-op cleanly."""
127
+ missing_state_file = tmp_path / "gh_pr_author_swap_session-missing.json"
128
+
129
+ assert utils_module._state_file_is_attacker_planted(missing_state_file) is False
130
+
131
+
132
+ def test_state_file_is_attacker_planted_returns_true_for_non_regular_file(
133
+ tmp_path: pathlib.Path,
134
+ ) -> None:
135
+ """A FIFO at the predictable swap-state path is flagged as attacker-planted.
136
+
137
+ The enforcer only writes regular files; any non-regular file type
138
+ (symlink, FIFO, device) at the predictable path indicates another
139
+ party pre-created it to redirect the restore or cleanup hook.
140
+ """
141
+ if not hasattr(os, "mkfifo"):
142
+ pytest.skip("mkfifo not available on this platform")
143
+ if not hasattr(os, "getuid"):
144
+ pytest.skip("POSIX ownership semantics not available on this platform")
145
+ fifo_state_file = tmp_path / "gh_pr_author_swap_session-fifo.json"
146
+ os.mkfifo(fifo_state_file, STATE_FILE_PERMISSION_MODE)
147
+
148
+ assert utils_module._state_file_is_attacker_planted(fifo_state_file) is True
149
+
150
+
151
+ def test_state_file_is_attacker_planted_returns_true_when_lstat_raises_os_error(
152
+ tmp_path: pathlib.Path,
153
+ monkeypatch: pytest.MonkeyPatch,
154
+ ) -> None:
155
+ """An ``OSError`` from ``lstat`` fails closed — the path is treated as planted.
156
+
157
+ A path that exists but is unreadable (permission denied, broken
158
+ filesystem mount, etc.) cannot be proven safe, so the helper
159
+ returns True to keep the restore or cleanup hook from trusting it.
160
+ """
161
+ if not hasattr(os, "getuid"):
162
+ pytest.skip("POSIX ownership semantics not available on this platform")
163
+ unreadable_state_file = tmp_path / "gh_pr_author_swap_session-unreadable.json"
164
+ unreadable_state_file.write_text("{}", encoding="utf-8")
165
+
166
+ def raise_permission_error(self: pathlib.Path) -> os.stat_result:
167
+ raise PermissionError("simulated lstat failure")
168
+
169
+ monkeypatch.setattr(pathlib.Path, "lstat", raise_permission_error)
170
+
171
+ assert utils_module._state_file_is_attacker_planted(unreadable_state_file) is True
172
+
173
+
174
+ def test_strip_bash_comments_preserves_substitution_body_when_hash_is_inside_dollar_paren() -> None:
175
+ """Regression: ``$(date +%H # 24h) && gh pr create`` must not let the regex eat past the substitution closer.
176
+
177
+ Before this fix ``_strip_bash_comments`` ran a flat regex sweep that
178
+ blanked from ``#`` to end-of-line regardless of substitution depth.
179
+ A ``#`` preceded by whitespace inside a ``$(...)`` body consumed
180
+ the closing ``)`` AND every byte after it on the same line,
181
+ erasing a real ``gh pr create`` invocation from the enforcer's
182
+ view and silently bypassing the swap.
183
+ """
184
+ raw_command = "$(date +%H # 24h) && gh pr create --title T"
185
+ preprocessed_command = utils_module._preprocess_command_for_matching(raw_command)
186
+ assert "gh pr create" in preprocessed_command
187
+ assert utils_module._command_invokes_gh_pr_create_in_stripped(preprocessed_command)
188
+
189
+
190
+ def test_strip_bash_comments_preserves_substitution_body_when_hash_is_inside_backtick() -> None:
191
+ """Regression: a ``#`` inside a backtick body must not erase a trailing ``gh pr create``.
192
+
193
+ Backtick substitution is symmetric with ``$(...)``; the
194
+ substitution-aware comment walker must treat both shapes the same.
195
+ """
196
+ raw_command = "foo `cmd # comment` bar && gh pr create"
197
+ preprocessed_command = utils_module._preprocess_command_for_matching(raw_command)
198
+ assert "gh pr create" in preprocessed_command
199
+ assert utils_module._command_invokes_gh_pr_create_in_stripped(preprocessed_command)
200
+
201
+
202
+ def test_strip_bash_comments_strips_top_level_trailing_comment() -> None:
203
+ """Existing behaviour: a top-level trailing ``#`` comment after ``gh pr create`` is blanked.
204
+
205
+ The comment-stripping pass must still erase a real top-level
206
+ comment so the enforcer treats only the command portion as code.
207
+ """
208
+ raw_command = "gh pr create # this is a comment"
209
+ preprocessed_command = utils_module._preprocess_command_for_matching(raw_command)
210
+ assert "this is a comment" not in preprocessed_command
211
+
212
+
213
+ def test_strip_bash_comments_strips_prior_line_comment_only() -> None:
214
+ """Existing behaviour: a comment on line 1 is blanked but a ``gh pr create`` on line 2 still matches.
215
+
216
+ The newline is preserved so the matcher can still tell the two
217
+ lines apart, and the second-line command stays intact.
218
+ """
219
+ raw_command = "echo a # b\ngh pr create"
220
+ preprocessed_command = utils_module._preprocess_command_for_matching(raw_command)
221
+ assert "gh pr create" in preprocessed_command
222
+ assert "# b" not in preprocessed_command
223
+ assert utils_module._command_invokes_gh_pr_create_in_stripped(preprocessed_command)
224
+
225
+
226
+ def test_lstat_indicates_attacker_planted_returns_false_for_well_formed_lstat(
227
+ tmp_path: pathlib.Path,
228
+ ) -> None:
229
+ """A 0o600 regular file owned by the current user is not flagged.
230
+
231
+ Mirrors ``test_state_file_is_attacker_planted_returns_false_for_well_formed_file``
232
+ but feeds the helper a pre-computed ``lstat`` result so the helper
233
+ does not perform its own syscall.
234
+ """
235
+ state_file = tmp_path / "gh_pr_author_swap_session-well_formed.json"
236
+ state_file.write_text("{}", encoding="utf-8")
237
+ if hasattr(os, "getuid"):
238
+ os.chmod(state_file, STATE_FILE_PERMISSION_MODE)
239
+
240
+ file_lstat_result = state_file.lstat()
241
+
242
+ assert utils_module._lstat_indicates_attacker_planted(file_lstat_result) is False
243
+
244
+
245
+ def test_lstat_indicates_attacker_planted_returns_true_for_world_readable_mode(
246
+ tmp_path: pathlib.Path,
247
+ ) -> None:
248
+ """A 0o644 regular file is flagged as attacker-planted on POSIX.
249
+
250
+ The enforcer always creates state files at 0o600, so a divergent
251
+ mode is treated as a plant.
252
+ """
253
+ if not hasattr(os, "getuid"):
254
+ pytest.skip("POSIX ownership semantics not available on this platform")
255
+ state_file = tmp_path / "gh_pr_author_swap_session-mode_wrong.json"
256
+ state_file.write_text("{}", encoding="utf-8")
257
+ os.chmod(state_file, 0o644)
258
+
259
+ file_lstat_result = state_file.lstat()
260
+
261
+ assert utils_module._lstat_indicates_attacker_planted(file_lstat_result) is True
262
+
263
+
264
+ def test_lstat_indicates_attacker_planted_returns_true_for_foreign_uid(
265
+ tmp_path: pathlib.Path,
266
+ ) -> None:
267
+ """A regular 0o600 file owned by a different uid is flagged on POSIX.
268
+
269
+ The helper sees only the ``stat_result`` it is given, so the test
270
+ builds a synthetic ``os.stat_result`` whose ``st_uid`` does not match
271
+ ``os.getuid()`` and feeds it directly to the helper.
272
+ """
273
+ if not hasattr(os, "getuid"):
274
+ pytest.skip("POSIX ownership semantics not available on this platform")
275
+ state_file = tmp_path / "gh_pr_author_swap_session-foreign_uid.json"
276
+ state_file.write_text("{}", encoding="utf-8")
277
+ os.chmod(state_file, STATE_FILE_PERMISSION_MODE)
278
+ real_lstat_result = state_file.lstat()
279
+ foreign_user_id = os.getuid() + 1
280
+ synthetic_stat_fields = (
281
+ real_lstat_result.st_mode,
282
+ real_lstat_result.st_ino,
283
+ real_lstat_result.st_dev,
284
+ real_lstat_result.st_nlink,
285
+ foreign_user_id,
286
+ real_lstat_result.st_gid,
287
+ real_lstat_result.st_size,
288
+ real_lstat_result.st_atime,
289
+ real_lstat_result.st_mtime,
290
+ real_lstat_result.st_ctime,
291
+ )
292
+ synthetic_stat_result = os.stat_result(synthetic_stat_fields)
293
+
294
+ assert utils_module._lstat_indicates_attacker_planted(synthetic_stat_result) is True
295
+
296
+
297
+ def test_lstat_indicates_attacker_planted_returns_true_for_non_regular_file(
298
+ tmp_path: pathlib.Path,
299
+ ) -> None:
300
+ """A FIFO at the predictable swap-state path is flagged.
301
+
302
+ Mirrors ``test_state_file_is_attacker_planted_returns_true_for_non_regular_file``
303
+ but feeds the helper the FIFO's own ``lstat`` result rather than the
304
+ path.
305
+ """
306
+ if not hasattr(os, "mkfifo"):
307
+ pytest.skip("mkfifo not available on this platform")
308
+ if not hasattr(os, "getuid"):
309
+ pytest.skip("POSIX ownership semantics not available on this platform")
310
+ fifo_state_file = tmp_path / "gh_pr_author_swap_session-fifo.json"
311
+ os.mkfifo(fifo_state_file, STATE_FILE_PERMISSION_MODE)
312
+
313
+ file_lstat_result = fifo_state_file.lstat()
314
+
315
+ assert utils_module._lstat_indicates_attacker_planted(file_lstat_result) is True
316
+
317
+
318
+ def test_lstat_indicates_attacker_planted_returns_false_on_windows(
319
+ tmp_path: pathlib.Path,
320
+ ) -> None:
321
+ """On Windows (no ``os.getuid``) the helper short-circuits to False.
322
+
323
+ Windows tempdir is already per-user, so the cross-user attack
324
+ surface this check guards against on POSIX does not exist there.
325
+ """
326
+ if hasattr(os, "getuid"):
327
+ pytest.skip("POSIX has os.getuid; this case asserts Windows-only behaviour")
328
+ state_file = tmp_path / "gh_pr_author_swap_session-windows.json"
329
+ state_file.write_text("{}", encoding="utf-8")
330
+
331
+ file_lstat_result = state_file.lstat()
332
+
333
+ assert utils_module._lstat_indicates_attacker_planted(file_lstat_result) is False
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-dev-env",
3
- "version": "1.40.0",
3
+ "version": "1.42.0",
4
4
  "description": "Claude Code development standards — rules, hooks, agents, commands, and skills",
5
5
  "type": "module",
6
6
  "bin": {
@@ -70,8 +70,8 @@ def _populate_findings(parent: Element, findings_data: list[dict[str, object]])
70
70
 
71
71
  Scalar finding fields become XML attributes on `<finding>`; the
72
72
  body fields named in `ALL_FINDING_BODY_ELEMENT_KEYS` (defined in
73
- `config/path_resolver_constants.py` and currently
74
- `("title", "excerpt", "description")`) become child elements.
73
+ `packages/claude-dev-env/skills/_shared/pr-loop/scripts/config/path_resolver_constants.py`
74
+ and currently `("title", "excerpt", "description")`) become child elements.
75
75
  Nested dicts or lists in scalar slots are flattened to string form
76
76
  so attribute serialization stays well-defined.
77
77
 
@@ -1,8 +1,8 @@
1
1
  """Validate status enum values and write <bugteam_fix> XML at the canonical path.
2
2
 
3
3
  Status enum (canonical source: `ALL_VALID_FIX_STATUSES` in
4
- `config/path_resolver_constants.py`): fixed | could_not_address |
5
- hook_blocked | unverified_fixed.
4
+ `packages/claude-dev-env/skills/_shared/pr-loop/scripts/config/path_resolver_constants.py`):
5
+ fixed | could_not_address | hook_blocked | unverified_fixed.
6
6
 
7
7
  Each outcome's scalar fields become XML attributes on `<outcome>`; the
8
8
  body fields named in `ALL_FIX_OUTCOME_BODY_ELEMENT_KEYS` (currently
@@ -91,6 +91,17 @@ The audit must either produce new Shape A findings citing new file:line referenc
91
91
 
92
92
  For `/bugteam`, the single audit agent provides per-category coverage by walking all A–K rubrics in one invocation.
93
93
 
94
+ ## Merge rules
95
+
96
+ When the LEAD combines findings from multiple sources (primary auditor, Haiku secondary auditor, adversarial pass), it applies these rules:
97
+
98
+ - **De-dup key**: `(file, line, category)`. Two findings sharing the same `(file, line, category)` tuple are the same finding and collapse into one entry.
99
+ - **Severity conflict**: max wins (`P0 > P1 > P2`). When sources disagree on severity for the same de-dup key, the merged entry keeps the highest severity.
100
+ - **Unique-to-secondary findings**: added to the merged set with the secondary's severity and source annotation.
101
+ - **Unique-to-primary findings**: kept as-is.
102
+ - **Zero secondary findings**: primary set trusted; proceed.
103
+ - **Malformed or non-parseable secondary output**: lead trusts the primary set and logs the event in `loop-<L>-diagnostics.json` under `haiku_findings` as `[{"parse_error": "<message>"}]`.
104
+
94
105
  ## Post-fix self-audit
95
106
 
96
107
  Audit-and-fix skills (`/qbug`, `/bugteam`) MUST re-audit modified files between `py_compile` and `git add`. This catches fix-induced regressions in the same loop that introduced them rather than on loop N+1.
@@ -109,6 +120,17 @@ Sequence:
109
120
 
110
121
  `converged` exit condition: `primary_audit_clean AND post_fix_audit_clean` for the committing loop.
111
122
 
123
+ ## De-dup and merge
124
+
125
+ Findings from primary, adversarial, Haiku secondary, and post-fix passes are merged into a single deduped finding list before persistence.
126
+
127
+ - **De-dup key:** `(file, line, category)`. Two findings sharing the same `(file, line, category)` tuple collapse into a single deduped entry.
128
+ - **Severity conflict resolution:** `max wins`. When merged findings disagree on severity, the deduped entry carries the maximum severity (`P0 > P1 > P2`).
129
+ - **Excerpt and failure_mode:** the deduped entry inherits these fields from the highest-severity contributing finding. Ties keep the first observed contributor.
130
+ - **`evidence_files`:** the deduped entry carries the union of every contributor's `evidence_files`, deduplicated and sorted.
131
+
132
+ The merged list lands in `loop-<L>-diagnostics.json` under both `merged` (one entry per contributing finding) and `deduped` (one entry per unique `(file, line, category)` tuple).
133
+
112
134
  ## Persistence
113
135
 
114
136
  Every audit loop writes two JSON files under the skill's scoped temp directory (resolved via `tempfile.gettempdir()`):
@@ -22,7 +22,7 @@ python "${CLAUDE_SKILL_DIR}/../../_shared/pr-loop/scripts/post_audit_thread.py"
22
22
 
23
23
  Capture `<head_sha>` via `git rev-parse HEAD` in the subagent cwd immediately before this call so the review attaches to the commit the audit actually scoped.
24
24
 
25
- `--findings-json` points to a JSON file whose root is a list of objects shaped `{path, line, side, severity, description, fix_summary}`. Build it from the merged Shape A findings: finding `file` → `path`. Each finding's `failure_mode` carries the full audit-to-fix handoff text per [`agents/code-quality-agent.md`](../../../agents/code-quality-agent.md); split `failure_mode` at the literal `Fix:` heading so the failure narrative becomes `description` and the suffix beginning at `Fix:` (including the trailing `Validation:` clause) becomes `fix_summary`. When a finding's `failure_mode` omits the `Fix:` heading, write the full text to BOTH `description` and `fix_summary` so the script's body template (`INLINE_COMMENT_BODY_TEMPLATE` in [`scripts/config/post_audit_thread_constants.py`](../../../_shared/pr-loop/scripts/config/post_audit_thread_constants.py)) renders coherently. Set `side="RIGHT"` for every entry. On CLEAN the list is empty (`[]`); on DIRTY the list carries one entry per finding.
25
+ `--findings-json` points to a JSON file whose root is a list of objects shaped `{path, line, side, severity, description, fix_summary}`. Build it from the merged Shape A findings: finding `file` → `path`. Each finding's `failure_mode` carries the full audit-to-fix handoff text per [`agents/code-quality-agent.md`](../../../agents/code-quality-agent.md); split `failure_mode` at the literal `Fix:` heading so the failure narrative becomes `description` and the suffix beginning at `Fix:` (including the trailing `Validation:` clause) becomes `fix_summary`. When a finding's `failure_mode` omits the `Fix:` heading, write the full text to BOTH `description` and `fix_summary` so the script's body template (`INLINE_COMMENT_BODY_TEMPLATE` in [`packages/claude-dev-env/_shared/pr-loop/scripts/config/post_audit_thread_constants.py`](../../../_shared/pr-loop/scripts/config/post_audit_thread_constants.py)) renders coherently. Set `side="RIGHT"` for every entry. On CLEAN the list is empty (`[]`); on DIRTY the list carries one entry per finding.
26
26
 
27
27
  The script handles retries internally — 1s / 4s / 16s backoff across four attempts (one initial plus three retries). Exit codes:
28
28
 
@@ -12,6 +12,7 @@ import json
12
12
  import os
13
13
  import stat
14
14
  import sys
15
+ from collections.abc import Callable
15
16
  from pathlib import Path
16
17
  from typing import NoReturn
17
18
 
@@ -26,6 +27,7 @@ for each_cached_module_name in [
26
27
  )
27
28
 
28
29
  from config.claude_permissions_common_constants import (
30
+ ALL_TRUST_ENTRY_PROJECT_PATH_BOUNDARY_QUOTE_CHARACTERS,
29
31
  ATOMIC_WRITE_TEMPORARY_SUFFIX,
30
32
  DEFAULT_SETTINGS_FILE_MODE,
31
33
  TEXT_FILE_ENCODING,
@@ -105,6 +107,113 @@ def build_permission_rules(
105
107
  ]
106
108
 
107
109
 
110
+ def build_agent_config_deny_rule(
111
+ tool_name: str, project_path: str, agent_config_path_pattern: str
112
+ ) -> str:
113
+ """Construct a deny rule for a single agent-config path pattern.
114
+
115
+ Args:
116
+ tool_name: The permission tool name (e.g., "Edit", "Write", "Read").
117
+ project_path: The POSIX-style project root path.
118
+ agent_config_path_pattern: The agent-config path pattern under .claude/.
119
+
120
+ Returns:
121
+ The deny rule string Claude Code matches against tool invocations.
122
+ """
123
+ return f"{tool_name}({project_path}/.claude/{agent_config_path_pattern})"
124
+
125
+
126
+ def build_agent_config_deny_rules(
127
+ project_path: str,
128
+ all_permission_allow_tools: tuple[str, ...],
129
+ all_agent_config_path_patterns: tuple[str, ...],
130
+ ) -> list[str]:
131
+ """Construct deny rules covering every tool and pattern pair.
132
+
133
+ Args:
134
+ project_path: The POSIX-style project root path.
135
+ all_permission_allow_tools: Tool names to build deny rules for.
136
+ all_agent_config_path_patterns: Agent-config path patterns to deny under .claude/.
137
+
138
+ Returns:
139
+ List of deny rule strings, one per tool/pattern combination.
140
+ """
141
+ return [
142
+ build_agent_config_deny_rule(each_tool, project_path, each_pattern)
143
+ for each_tool in all_permission_allow_tools
144
+ for each_pattern in all_agent_config_path_patterns
145
+ ]
146
+
147
+
148
+ def _is_project_path_token_at_word_boundary(
149
+ body_after_prefix: str, token_position: int
150
+ ) -> bool:
151
+ if token_position == 0:
152
+ return True
153
+ preceding_character = body_after_prefix[token_position - 1]
154
+ if preceding_character.isspace():
155
+ return True
156
+ return preceding_character in ALL_TRUST_ENTRY_PROJECT_PATH_BOUNDARY_QUOTE_CHARACTERS
157
+
158
+
159
+ def is_trust_entry_for_project(
160
+ candidate_entry: object, project_path: str, prefix: str
161
+ ) -> bool:
162
+ """Detect whether an autoMode.environment entry is a trust entry for the project.
163
+
164
+ The predicate matches any string entry whose prefix matches the trust-entry
165
+ marker and that contains the project's .claude/** path token anchored on a
166
+ non-path boundary (the start of the body after the prefix, a whitespace
167
+ character, or a quote character). The boundary anchor prevents
168
+ cross-project false positives where the current project's path is a path
169
+ suffix of an unrelated entry's path. The exact wording after the prefix is
170
+ allowed to vary between template revisions.
171
+
172
+ Args:
173
+ candidate_entry: The autoMode.environment list value to inspect.
174
+ project_path: The POSIX-style project root path.
175
+ prefix: The literal prefix that marks a trust entry.
176
+
177
+ Returns:
178
+ True when the entry is a prior trust entry for this project.
179
+ """
180
+ if not isinstance(candidate_entry, str):
181
+ return False
182
+ if not candidate_entry.startswith(prefix):
183
+ return False
184
+ project_path_token = f"{project_path}/.claude/**"
185
+ body_after_prefix = candidate_entry[len(prefix):]
186
+ token_position = body_after_prefix.find(project_path_token)
187
+ while token_position != -1:
188
+ if _is_project_path_token_at_word_boundary(body_after_prefix, token_position):
189
+ return True
190
+ next_search_start = token_position + 1
191
+ token_position = body_after_prefix.find(project_path_token, next_search_start)
192
+ return False
193
+
194
+
195
+ def remove_matching_entries_from_list(
196
+ all_target_list: list[object],
197
+ match_predicate: Callable[[object], bool],
198
+ ) -> int:
199
+ """Remove every entry from a list that satisfies the predicate.
200
+
201
+ Args:
202
+ all_target_list: The list to filter in place.
203
+ match_predicate: Function returning True for entries to remove.
204
+
205
+ Returns:
206
+ Number of entries removed.
207
+ """
208
+ original_length = len(all_target_list)
209
+ all_target_list[:] = [
210
+ each_value
211
+ for each_value in all_target_list
212
+ if not match_predicate(each_value)
213
+ ]
214
+ return original_length - len(all_target_list)
215
+
216
+
108
217
  def load_settings(settings_path: Path) -> dict[str, object]:
109
218
  """Read and parse a JSON settings file from disk.
110
219
 
@@ -5,14 +5,20 @@ import subprocess
5
5
  import sys
6
6
  from pathlib import Path
7
7
 
8
+ parent_directory = str(Path(__file__).resolve().parent)
9
+ try:
10
+ sys.path.remove(parent_directory)
11
+ except ValueError:
12
+ pass
13
+ if parent_directory not in sys.path:
14
+ sys.path.insert(0, parent_directory)
15
+
8
16
  for each_cached_module_name in [
9
17
  each_module_key
10
18
  for each_module_key in list(sys.modules)
11
19
  if each_module_key == "config" or each_module_key.startswith("config.")
12
20
  ]:
13
21
  sys.modules.pop(each_cached_module_name, None)
14
- if str(Path(__file__).resolve().parent) not in sys.path:
15
- sys.path.insert(0, str(Path(__file__).resolve().parent))
16
22
 
17
23
  from config.bugteam_fix_hookspath_constants import (
18
24
  ALL_CANONICAL_HOOKS_DIRECTORY_COMPONENTS,
@@ -4,10 +4,58 @@ from __future__ import annotations
4
4
 
5
5
  TEXT_FILE_ENCODING: str = "utf-8"
6
6
  ALL_PERMISSION_ALLOW_TOOLS: tuple[str, ...] = ("Edit", "Write", "Read")
7
+ ALL_AGENT_CONFIG_DENY_TOOLS: tuple[str, ...] = ("Edit", "Write", "Read", "Glob")
8
+ ALL_AGENT_CONFIG_PATH_PATTERNS: tuple[str, ...] = (
9
+ "settings*.json",
10
+ "hooks/**",
11
+ "commands/**",
12
+ "agents/**",
13
+ "skills/**",
14
+ "mcp.json",
15
+ "CLAUDE.md",
16
+ )
17
+ AUTO_MODE_ENVIRONMENT_ENTRY_PREFIX: str = "Trusted local workspace:"
18
+
19
+
20
+ def _describe_agent_config_pattern_for_humans(agent_config_path_pattern: str) -> str:
21
+ glob_suffix_under_directory = "/**"
22
+ file_name_for_special_phrasing = "mcp.json"
23
+ if agent_config_path_pattern.endswith(glob_suffix_under_directory):
24
+ directory_name = agent_config_path_pattern[
25
+ : -len(glob_suffix_under_directory)
26
+ ]
27
+ return f"anything under {directory_name}/"
28
+ if agent_config_path_pattern == file_name_for_special_phrasing:
29
+ return f"the {file_name_for_special_phrasing} file"
30
+ return agent_config_path_pattern
31
+
32
+
33
+ def _build_agent_config_pattern_phrase(
34
+ all_agent_config_path_patterns: tuple[str, ...],
35
+ ) -> str:
36
+ all_described_patterns: list[str] = [
37
+ _describe_agent_config_pattern_for_humans(each_pattern)
38
+ for each_pattern in all_agent_config_path_patterns
39
+ ]
40
+ if len(all_described_patterns) <= 1:
41
+ return ", ".join(all_described_patterns)
42
+ leading_phrase_parts = ", ".join(all_described_patterns[:-1])
43
+ final_phrase_part = all_described_patterns[-1]
44
+ return f"{leading_phrase_parts}, and {final_phrase_part}"
45
+
46
+
47
+ _AGENT_CONFIG_PATTERN_PHRASE: str = _build_agent_config_pattern_phrase(
48
+ ALL_AGENT_CONFIG_PATH_PATTERNS
49
+ )
50
+
7
51
  AUTO_MODE_ENVIRONMENT_ENTRY_TEMPLATE: str = (
8
- "Trusted local workspace: {project_path}/.claude/** is the user's "
9
- "project Claude Code config tree; edits inside are routine"
52
+ f"Trusted local workspace: Files under {{project_path}}/.claude/** inherit "
53
+ f"the workspace's trust for Edit, Write, Read, and Glob operations EXCEPT "
54
+ f"for agent-config files: {_AGENT_CONFIG_PATTERN_PHRASE}. Edits to those "
55
+ f"agent-config files always require explicit per-edit user approval."
10
56
  )
57
+
58
+ ALL_TRUST_ENTRY_PROJECT_PATH_BOUNDARY_QUOTE_CHARACTERS: tuple[str, ...] = ('"', "'")
11
59
  ATOMIC_WRITE_TEMPORARY_SUFFIX: str = ".tmp"
12
60
  GIT_DIRECTORY_MARKER: str = ".git"
13
61
  CLAUDE_DIRECTORY_MARKER: str = ".claude"
@@ -15,6 +63,7 @@ CLAUDE_USER_SETTINGS_FILENAME: str = "settings.json"
15
63
  DEFAULT_SETTINGS_FILE_MODE: int = 0o600
16
64
  SETTINGS_PERMISSIONS_KEY: str = "permissions"
17
65
  SETTINGS_ALLOW_KEY: str = "allow"
66
+ SETTINGS_DENY_KEY: str = "deny"
18
67
  SETTINGS_ADDITIONAL_DIRECTORIES_KEY: str = "additionalDirectories"
19
68
  SETTINGS_AUTO_MODE_KEY: str = "autoMode"
20
69
  SETTINGS_ENVIRONMENT_KEY: str = "environment"