claude-dev-env 8.120.0 → 8.122.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/bin/ever-registered-hooks.mjs +2 -0
- package/docs/rule-guides/question-presentation.md +4 -0
- package/hooks/advisory/auto_mode_denial_quick_fix.py +275 -27
- package/hooks/advisory/auto_mode_rule_flow_eval/auto_mode_rule_flow_cases.json +3475 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/auto_mode_rule_flow_runner.py +849 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/stub_allow_hook.py +20 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/stub_hold_hook.py +54 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/test_auto_mode_rule_flow_runner.py +143 -0
- package/hooks/advisory/test_auto_mode_denial_quick_fix.py +533 -9
- package/hooks/auto_mode_question_transcript.py +215 -0
- package/hooks/auto_mode_refusal_ledger.py +481 -0
- package/hooks/auto_mode_refusal_ledger_files.py +336 -0
- package/hooks/auto_mode_rule_steps.py +177 -0
- package/hooks/blocking/auto_mode_refusal_stop_gate.py +384 -0
- package/hooks/blocking/sent_file_question_detector.py +340 -0
- package/hooks/blocking/sent_file_question_gate.py +307 -0
- package/hooks/blocking/sent_file_scan.py +209 -0
- package/hooks/blocking/test_auto_mode_refusal_stop_gate.py +484 -0
- package/hooks/blocking/test_sent_file_question_detector.py +68 -0
- package/hooks/blocking/test_sent_file_question_gate.py +688 -0
- package/hooks/blocking/test_sent_file_scan.py +71 -0
- package/hooks/blocking/test_user_step_wording.py +46 -0
- package/hooks/blocking/user_step_wording.py +113 -15
- package/hooks/features/README.md +1 -1
- package/hooks/features/conduct-gates.md +3 -0
- package/hooks/features/session-context.md +5 -2
- package/hooks/hooks.json +15 -0
- package/hooks/hooks_constants/auto_mode_denial_quick_fix_constants.py +51 -3
- package/hooks/hooks_constants/auto_mode_refusal_ledger_constants.py +85 -0
- package/hooks/hooks_constants/auto_mode_refusal_stop_gate_constants.py +80 -0
- package/hooks/hooks_constants/auto_mode_rule_flow_runner_constants.py +225 -0
- package/hooks/hooks_constants/auto_mode_rule_steps_constants.py +82 -0
- package/hooks/hooks_constants/sent_file_question_gate_constants.py +261 -0
- package/hooks/hooks_constants/user_step_wording_constants.py +4 -0
- package/hooks/test_auto_mode_question_transcript.py +113 -0
- package/hooks/test_auto_mode_refusal_ledger.py +398 -0
- package/hooks/test_auto_mode_refusal_ledger_files.py +155 -0
- package/hooks/test_auto_mode_rule_steps.py +210 -0
- package/package.json +1 -1
- package/scripts/policy_lint/adapter_configuration.py +10 -0
- package/scripts/policy_lint/config/constants.py +2 -0
- package/scripts/tests/test_adapter_configuration.py +16 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
import time
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
import sent_file_question_detector
|
|
11
|
+
import sent_file_question_gate
|
|
12
|
+
|
|
13
|
+
GATE_SCRIPT_PATH = Path(sent_file_question_gate.__file__)
|
|
14
|
+
|
|
15
|
+
ASKING_FILES = [
|
|
16
|
+
(
|
|
17
|
+
"questions_heading.md",
|
|
18
|
+
"# Status\n\nThe docs build is green.\n\n## Questions for you\n\n1. The release date\n",
|
|
19
|
+
"Questions for you",
|
|
20
|
+
),
|
|
21
|
+
("open_questions.md", "## Open questions\n\n- The rate limit value\n", "Open questions"),
|
|
22
|
+
("decisions.md", "**Decisions needed**\n\n- The release date\n", "Decisions needed"),
|
|
23
|
+
(
|
|
24
|
+
"should_i.md",
|
|
25
|
+
"The branch is pushed. Should I archive the old branches?\n",
|
|
26
|
+
"Should I archive the old branches?",
|
|
27
|
+
),
|
|
28
|
+
(
|
|
29
|
+
"do_you_want.md",
|
|
30
|
+
"- Do you want the cache cleared before the rerun?\n",
|
|
31
|
+
"Do you want the cache cleared before the rerun?",
|
|
32
|
+
),
|
|
33
|
+
(
|
|
34
|
+
"choice.md",
|
|
35
|
+
"The change is live. Keep it merged, or revert it?\n",
|
|
36
|
+
"Keep it merged, or revert it?",
|
|
37
|
+
),
|
|
38
|
+
(
|
|
39
|
+
"short_choice.md",
|
|
40
|
+
"Both fixes pass. Tag now or wait for the nightly build?\n",
|
|
41
|
+
"Tag now or wait for the nightly build?",
|
|
42
|
+
),
|
|
43
|
+
(
|
|
44
|
+
"want_me.txt",
|
|
45
|
+
"Checks passed.\nWant me to open the pull request?\n",
|
|
46
|
+
"Want me to open the pull request?",
|
|
47
|
+
),
|
|
48
|
+
("proceed.md", "Three files changed. Proceed?\n", "Proceed?"),
|
|
49
|
+
(
|
|
50
|
+
"wrapped.md",
|
|
51
|
+
"The deploy finished and the logs are clean. Would you like\nme to remove the old build folder?\n",
|
|
52
|
+
"Would you like me to remove the old build folder?",
|
|
53
|
+
),
|
|
54
|
+
(
|
|
55
|
+
"table.md",
|
|
56
|
+
"| Item | Question |\n|---|---|\n| Cache | Should we clear it? |\n",
|
|
57
|
+
"Should we clear it?",
|
|
58
|
+
),
|
|
59
|
+
(
|
|
60
|
+
"prefer.html",
|
|
61
|
+
"<html><body><h1>Plan</h1><p>Two layouts fit. Which option do you prefer?</p></body></html>",
|
|
62
|
+
"Which option do you prefer?",
|
|
63
|
+
),
|
|
64
|
+
("open.html", "<h2>Open questions</h2><ul><li>Rate limit value</li></ul>", "Open questions"),
|
|
65
|
+
(
|
|
66
|
+
"decide.htm",
|
|
67
|
+
"<p>The key expires Friday. <b>Should I</b> rotate it now?</p>",
|
|
68
|
+
"Should I rotate it now?",
|
|
69
|
+
),
|
|
70
|
+
("yes_or_no.md", "Checks passed. Should I merge it? Yes or no.\n", "Should I merge it?"),
|
|
71
|
+
(
|
|
72
|
+
"full_width.md",
|
|
73
|
+
"Do you want the cache cleared? Reply yes to clear it.\n",
|
|
74
|
+
"Do you want the cache cleared?",
|
|
75
|
+
),
|
|
76
|
+
(
|
|
77
|
+
"can_i.md",
|
|
78
|
+
"The logs fill the disk. Can I delete the old ones?\n",
|
|
79
|
+
"Can I delete the old ones?",
|
|
80
|
+
),
|
|
81
|
+
(
|
|
82
|
+
"choice_reply.md",
|
|
83
|
+
"Is it a bug or a flake? Let me know before noon.\n",
|
|
84
|
+
"Is it a bug or a flake?",
|
|
85
|
+
),
|
|
86
|
+
("no_rush.md", "The plan is ready. Do you approve? No rush.\n", "Do you approve?"),
|
|
87
|
+
("wish_then_context.md", "Do you want the cache cleared? It takes a minute.\n", "Do you want"),
|
|
88
|
+
("thoughts.md", "The draft is up.\n\nThoughts?\n", "Thoughts?"),
|
|
89
|
+
("are_we_good.md", "Tests pass. Are we good to ship?\n", "Are we good to ship?"),
|
|
90
|
+
(
|
|
91
|
+
"checkbox.md",
|
|
92
|
+
"- [ ] Should I archive the old branches?\n",
|
|
93
|
+
"Should I archive the old branches?",
|
|
94
|
+
),
|
|
95
|
+
(
|
|
96
|
+
"decisions_from_you.md",
|
|
97
|
+
"## Decisions needed from you\n\n- Release date\n",
|
|
98
|
+
"Decisions needed",
|
|
99
|
+
),
|
|
100
|
+
("need_your_input.md", "### Need your input\n\n- Release date\n", "Need your input"),
|
|
101
|
+
(
|
|
102
|
+
"which_date.md",
|
|
103
|
+
"The draft has three dates. What release date works best?\n",
|
|
104
|
+
"What release date works best?",
|
|
105
|
+
),
|
|
106
|
+
("lone_check.md", "The run is done.\n\n- Did you run the tests?\n", "Did you run the tests?"),
|
|
107
|
+
(
|
|
108
|
+
"box_confirm.md",
|
|
109
|
+
"- [ ] Can you confirm the release date?\n",
|
|
110
|
+
"Can you confirm the release date?",
|
|
111
|
+
),
|
|
112
|
+
("box_prefer.md", "- [ ] Do you have a preferred date?\n", "Do you have a preferred date?"),
|
|
113
|
+
(
|
|
114
|
+
"box_decision.md",
|
|
115
|
+
"- [ ] Have you got a decision on the launch?\n",
|
|
116
|
+
"Have you got a decision on the launch?",
|
|
117
|
+
),
|
|
118
|
+
(
|
|
119
|
+
"let_me_know.md",
|
|
120
|
+
"Two layouts fit. Let me know which you prefer.\n",
|
|
121
|
+
"Let me know which you prefer.",
|
|
122
|
+
),
|
|
123
|
+
(
|
|
124
|
+
"please_advise.md",
|
|
125
|
+
"Both patches apply. Please advise which one to apply.\n",
|
|
126
|
+
"Please advise which one to apply.",
|
|
127
|
+
),
|
|
128
|
+
("heading_should_i.md", "## Should I delete the old logs?\n", "Should I delete the old logs?"),
|
|
129
|
+
(
|
|
130
|
+
"heading_prefer.md",
|
|
131
|
+
"## Which option do you prefer?\n\n- Option A\n- Option B\n",
|
|
132
|
+
"Which option do you prefer?",
|
|
133
|
+
),
|
|
134
|
+
("box_ship.md", "- [ ] Are we good to ship?\n", "Are we good to ship?"),
|
|
135
|
+
("box_delete.md", "- [ ] Is it fine to delete the logs?\n", "Is it fine to delete the logs?"),
|
|
136
|
+
(
|
|
137
|
+
"box_sign_off.md",
|
|
138
|
+
"- [ ] Have you signed off on the release?\n",
|
|
139
|
+
"Have you signed off on the release?",
|
|
140
|
+
),
|
|
141
|
+
(
|
|
142
|
+
"long_should_first.md",
|
|
143
|
+
"Should I merge it, given "
|
|
144
|
+
+ "the runner logs from the nightly build, " * 60
|
|
145
|
+
+ "and the cache?\n",
|
|
146
|
+
"Should I merge it, given",
|
|
147
|
+
),
|
|
148
|
+
(
|
|
149
|
+
"long_want_last.md",
|
|
150
|
+
"Given "
|
|
151
|
+
+ "the runner logs from the nightly build, " * 60
|
|
152
|
+
+ "do you want me to merge it?\n",
|
|
153
|
+
"Given the runner logs",
|
|
154
|
+
),
|
|
155
|
+
("reply_yes_no.md", "The patch is ready. Reply with yes or no.\n", "Reply with yes or no."),
|
|
156
|
+
("answer_yes_no.md", "The patch is ready. Answer yes or no.\n", "Answer yes or no."),
|
|
157
|
+
("choose_a_b.md", "Two layouts fit. Choose A or B.\n", "Choose A or B."),
|
|
158
|
+
(
|
|
159
|
+
"pick_one.md",
|
|
160
|
+
"Two layouts fit. Pick one of the two layouts.\n",
|
|
161
|
+
"Pick one of the two layouts.",
|
|
162
|
+
),
|
|
163
|
+
("send_pick.md", "Two layouts fit. Send me your pick.\n", "Send me your pick."),
|
|
164
|
+
(
|
|
165
|
+
"heading_what_next.md",
|
|
166
|
+
"## What should I do next?\n\nThe build passed and the logs are clean.\n",
|
|
167
|
+
"What should I do next?",
|
|
168
|
+
),
|
|
169
|
+
(
|
|
170
|
+
"heading_pin.md",
|
|
171
|
+
"## Should I pin the version?\n\nPin it in the lock file.\n",
|
|
172
|
+
"Should I pin",
|
|
173
|
+
),
|
|
174
|
+
(
|
|
175
|
+
"bold_can_you.md",
|
|
176
|
+
"**Can you run it offline?**\n\nIt runs offline.\n",
|
|
177
|
+
"Can you run it offline?",
|
|
178
|
+
),
|
|
179
|
+
(
|
|
180
|
+
"heading_how_should_we.md",
|
|
181
|
+
"## How should we handle the rollback?\n\nThe build passed.\n",
|
|
182
|
+
"How should we handle the rollback?",
|
|
183
|
+
),
|
|
184
|
+
(
|
|
185
|
+
"heading_where_should_i.md",
|
|
186
|
+
"## Where should I put the config?\n\nThe build passed.\n",
|
|
187
|
+
"Where should I put the config?",
|
|
188
|
+
),
|
|
189
|
+
(
|
|
190
|
+
"heading_preferred.md",
|
|
191
|
+
"## Is option A preferred?\n\nThe build passed.\n",
|
|
192
|
+
"Is option A preferred?",
|
|
193
|
+
),
|
|
194
|
+
(
|
|
195
|
+
"mid_length_reader.md",
|
|
196
|
+
"Given the runner logs, " * 20
|
|
197
|
+
+ "do you want me to merge it, "
|
|
198
|
+
+ "given the runner logs, " * 25
|
|
199
|
+
+ "and the cache?\n",
|
|
200
|
+
"Given the runner logs",
|
|
201
|
+
),
|
|
202
|
+
(
|
|
203
|
+
"late_yes_no.md",
|
|
204
|
+
"When the run ends, reply with yes or no.\n",
|
|
205
|
+
"When the run ends, reply with yes or no.",
|
|
206
|
+
),
|
|
207
|
+
(
|
|
208
|
+
"please_yes_no.md",
|
|
209
|
+
"The patch is ready. Please answer yes or no.\n",
|
|
210
|
+
"Please answer yes or no.",
|
|
211
|
+
),
|
|
212
|
+
(
|
|
213
|
+
"box_confirm_plan.md",
|
|
214
|
+
"- [ ] Did you read it and confirm the plan?\n",
|
|
215
|
+
"Did you read it and confirm the plan?",
|
|
216
|
+
),
|
|
217
|
+
(
|
|
218
|
+
"list_confirm_output.md",
|
|
219
|
+
"- Did you run the tests and confirm the output?\n- Did you push the branch?\n",
|
|
220
|
+
"Did you run the tests and confirm the output?",
|
|
221
|
+
),
|
|
222
|
+
(
|
|
223
|
+
"box_choice.md",
|
|
224
|
+
"- [ ] Did you check the choice I made?\n",
|
|
225
|
+
"Did you check the choice I made?",
|
|
226
|
+
),
|
|
227
|
+
(
|
|
228
|
+
"html_box_ask.html",
|
|
229
|
+
'<ul><li><input type="checkbox"> Should I archive the old logs?</li></ul>',
|
|
230
|
+
"Should I archive the old logs?",
|
|
231
|
+
),
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
CLEAN_FILES = [
|
|
235
|
+
("faq.md", "## How do I install it?\n\nRun the installer.\n"),
|
|
236
|
+
("faq_you.md", "Do you need admin rights? Only for the system install.\n"),
|
|
237
|
+
("checklist.md", "## Checklist\n\n- Did you run the tests?\n- Did you update the changelog?\n"),
|
|
238
|
+
("task_box_check.md", "- [ ] Did you run the tests?\n"),
|
|
239
|
+
("which_version.md", "Which Python version works best? Python 3.12.\n"),
|
|
240
|
+
("box_work_check.md", "- [ ] Have you saved the file?\n- [ ] Is the build done?\n"),
|
|
241
|
+
("why_heading.md", "## Why does this exist?\n\nIt keeps questions in cards.\n"),
|
|
242
|
+
("how_heading.md", "## How does it work?\n"),
|
|
243
|
+
("troubleshooting.md", "## Troubleshooting\n\nDid you set `HOME`? Set it before the run.\n"),
|
|
244
|
+
("q_and_a_split.md", "Q: Can you run it offline?\n\nA: Yes.\n"),
|
|
245
|
+
("faq_heading_can_i.md", "## Can I run it offline?\n\nIt runs offline after the first sync.\n"),
|
|
246
|
+
("choose_instruction.md", "Choose IDs that contain no personal or secret text.\n"),
|
|
247
|
+
("select_one_or_more.md", "Select one or more archives.\n"),
|
|
248
|
+
("choose_named_options.md", "Choose Color or Image.\n"),
|
|
249
|
+
("select_file_or_folder.md", "Select the file or folder.\n"),
|
|
250
|
+
("pick_from_list.md", "Pick one of the themes in the list and press Save.\n"),
|
|
251
|
+
("send_message.md", "Send me a message to start it.\n"),
|
|
252
|
+
("please_survey.md", "Please answer the survey.\n"),
|
|
253
|
+
("describes_yes_no.md", "The prompt asks the user to answer yes or no.\n"),
|
|
254
|
+
("html_box_check.html", '<ul><li><input type="checkbox"> Did you run the tests?</li></ul>'),
|
|
255
|
+
(
|
|
256
|
+
"html_box_checks.html",
|
|
257
|
+
'<ul><li><input type="checkbox" /> Did you run the tests?</li>'
|
|
258
|
+
"<li><input type='checkbox'> Did you push the branch?</li></ul>",
|
|
259
|
+
),
|
|
260
|
+
("pick_instruction.md", "Pick one of two widths:\n\n- 640\n- 1280\n"),
|
|
261
|
+
("reply_instruction.md", "Reply to the sender, by name.\n"),
|
|
262
|
+
("subject_heading.md", "## What does the gate read?\n\nIt reads the sent file.\n"),
|
|
263
|
+
("fenced.md", "Run this:\n\n```\nread -p 'Should I continue? ' answer\n```\n"),
|
|
264
|
+
("quoted.md", "You wrote:\n\n> Should we ship this today?\n\nIt shipped at noon.\n"),
|
|
265
|
+
("code_span.md", "The prompt reads `Do you want to continue?` on start.\n"),
|
|
266
|
+
("answered.md", "Does the gate hold a second stop? No. It passes a held stop.\n"),
|
|
267
|
+
("answered_yes.md", "Can you rerun it? Yes, the rerun button sits on the run page.\n"),
|
|
268
|
+
("q_and_a.md", "Q: Can you run it offline?\nA: Yes.\n"),
|
|
269
|
+
("answered_cell.md", "| Question | Answer |\n|---|---|\n| Does it retry? | Twice |\n"),
|
|
270
|
+
("statement.md", "# Report\n\nThe build passed. Two tests were added.\n"),
|
|
271
|
+
("rhetorical.md", "Why did the build fail? The cache key changed.\n"),
|
|
272
|
+
("url.md", "The page is at https://example.com/search?q=1 now.\n"),
|
|
273
|
+
("quoted_example.md", 'Ask the user one thing, such as "Do you want approach A or B?"\n'),
|
|
274
|
+
(
|
|
275
|
+
"curly_quoted.md",
|
|
276
|
+
"Preferences: “Do you want the cache kept?” go through the question tool.\n",
|
|
277
|
+
),
|
|
278
|
+
(
|
|
279
|
+
"quoted_pair.md",
|
|
280
|
+
'Then ask: "Does this map cover what you need? Any rules to add?" and wait.\n',
|
|
281
|
+
),
|
|
282
|
+
(
|
|
283
|
+
"long_list_or.md",
|
|
284
|
+
"What files, folders, or areas of the code does this change touch in the build?\n",
|
|
285
|
+
),
|
|
286
|
+
("bare_questions.md", "## Questions\n\nThe team answered each one below.\n"),
|
|
287
|
+
("decisions_taken.md", "## Decisions\n\n- The cache stays on.\n"),
|
|
288
|
+
(
|
|
289
|
+
"pre.html",
|
|
290
|
+
"<p>The script prints a prompt.</p><pre>Do you want this?</pre><script>var x = 'Should I?';</script>",
|
|
291
|
+
),
|
|
292
|
+
("heading_question.html", "<h3>Should you rotate the key?</h3><p>Rotate it every 90 days.</p>"),
|
|
293
|
+
("quote.html", "<blockquote>Should we ship this?</blockquote><p>It shipped.</p>"),
|
|
294
|
+
("image.png", "Should I archive the old branches?\n"),
|
|
295
|
+
("faq_item.md", "## FAQ\n\n- Can I use it offline? After the first sync.\n"),
|
|
296
|
+
(
|
|
297
|
+
"details.html",
|
|
298
|
+
"<details><summary>Can I use it offline?</summary><p>After the first sync.</p></details>",
|
|
299
|
+
),
|
|
300
|
+
(
|
|
301
|
+
"indented_code.md",
|
|
302
|
+
"Run the prompt:\n\n read -p 'Do you want to continue? ' answer\n\nIt waits for input.\n",
|
|
303
|
+
),
|
|
304
|
+
(
|
|
305
|
+
"self_answered.md",
|
|
306
|
+
"Is it a bug or a flake? Logs show the same stack on each run, so a flake.\n",
|
|
307
|
+
),
|
|
308
|
+
]
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
@pytest.fixture(autouse=True)
|
|
312
|
+
def isolated_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
313
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
314
|
+
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def run_gate(
|
|
318
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
319
|
+
capsys: pytest.CaptureFixture[str],
|
|
320
|
+
tool_name: str,
|
|
321
|
+
tool_input: object,
|
|
322
|
+
working_directory: Path,
|
|
323
|
+
) -> tuple[int, str]:
|
|
324
|
+
hook_input = {
|
|
325
|
+
"hook_event_name": "PreToolUse",
|
|
326
|
+
"tool_name": tool_name,
|
|
327
|
+
"tool_input": tool_input,
|
|
328
|
+
"cwd": str(working_directory),
|
|
329
|
+
}
|
|
330
|
+
monkeypatch.setattr(
|
|
331
|
+
"sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
|
|
332
|
+
)
|
|
333
|
+
exit_code = sent_file_question_gate.main()
|
|
334
|
+
return exit_code, capsys.readouterr().err
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def written_file(folder: Path, file_name: str, file_text: str) -> Path:
|
|
338
|
+
file_path = folder / file_name
|
|
339
|
+
file_path.write_text(file_text, encoding="utf-8")
|
|
340
|
+
return file_path
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
@pytest.mark.parametrize(("file_name", "file_text", "quoted_question"), ASKING_FILES)
|
|
344
|
+
def test_should_deny_a_sent_file_that_asks_the_user(
|
|
345
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
346
|
+
capsys: pytest.CaptureFixture[str],
|
|
347
|
+
tmp_path: Path,
|
|
348
|
+
file_name: str,
|
|
349
|
+
file_text: str,
|
|
350
|
+
quoted_question: str,
|
|
351
|
+
) -> None:
|
|
352
|
+
file_path = written_file(tmp_path, file_name, file_text)
|
|
353
|
+
exit_code, reason = run_gate(
|
|
354
|
+
monkeypatch, capsys, "SendUserFile", {"files": [str(file_path)]}, tmp_path
|
|
355
|
+
)
|
|
356
|
+
assert exit_code == 2
|
|
357
|
+
assert quoted_question in reason
|
|
358
|
+
assert file_name in reason
|
|
359
|
+
assert "AskUserQuestion" in reason
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
@pytest.mark.parametrize(("file_name", "file_text"), CLEAN_FILES)
|
|
363
|
+
def test_should_allow_a_sent_file_that_asks_nothing(
|
|
364
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
365
|
+
capsys: pytest.CaptureFixture[str],
|
|
366
|
+
tmp_path: Path,
|
|
367
|
+
file_name: str,
|
|
368
|
+
file_text: str,
|
|
369
|
+
) -> None:
|
|
370
|
+
file_path = written_file(tmp_path, file_name, file_text)
|
|
371
|
+
exit_code, reason = run_gate(
|
|
372
|
+
monkeypatch, capsys, "SendUserFile", {"files": [str(file_path)]}, tmp_path
|
|
373
|
+
)
|
|
374
|
+
assert (exit_code, reason) == (0, "")
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
@pytest.mark.parametrize(("file_name", "file_text", "quoted_question"), ASKING_FILES)
|
|
378
|
+
def test_should_deny_a_page_publish_that_asks_the_user(
|
|
379
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
380
|
+
capsys: pytest.CaptureFixture[str],
|
|
381
|
+
tmp_path: Path,
|
|
382
|
+
file_name: str,
|
|
383
|
+
file_text: str,
|
|
384
|
+
quoted_question: str,
|
|
385
|
+
) -> None:
|
|
386
|
+
file_path = written_file(tmp_path, file_name, file_text)
|
|
387
|
+
exit_code, reason = run_gate(
|
|
388
|
+
monkeypatch, capsys, "Artifact", {"file_path": str(file_path)}, tmp_path
|
|
389
|
+
)
|
|
390
|
+
assert exit_code == 2
|
|
391
|
+
assert quoted_question in reason
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def test_should_allow_a_page_that_saves_the_answer_through_a_capability(
|
|
395
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
396
|
+
) -> None:
|
|
397
|
+
file_path = written_file(
|
|
398
|
+
tmp_path,
|
|
399
|
+
"cards.html",
|
|
400
|
+
"<h2>Which layout do you prefer?</h2><p>Which option do you prefer?</p>",
|
|
401
|
+
)
|
|
402
|
+
tool_input = {"action": "publish", "file_path": str(file_path), "capabilities": {"db": {}}}
|
|
403
|
+
assert run_gate(monkeypatch, capsys, "Artifact", tool_input, tmp_path) == (0, "")
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
@pytest.mark.parametrize(
|
|
407
|
+
"tool_input",
|
|
408
|
+
[
|
|
409
|
+
{"action": "read", "url": "https://claude.ai/artifact/x", "file_path": "ask.md"},
|
|
410
|
+
{
|
|
411
|
+
"action": "publish",
|
|
412
|
+
"url": "https://claude.ai/artifact/x",
|
|
413
|
+
"asset": True,
|
|
414
|
+
"file_path": "ask.md",
|
|
415
|
+
},
|
|
416
|
+
{"action": "publish", "type_url": "https://claude.ai/artifact/t", "file_path": "ask.md"},
|
|
417
|
+
{"action": "list"},
|
|
418
|
+
],
|
|
419
|
+
)
|
|
420
|
+
def test_should_allow_an_artifact_call_that_publishes_no_page(
|
|
421
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
422
|
+
capsys: pytest.CaptureFixture[str],
|
|
423
|
+
tmp_path: Path,
|
|
424
|
+
tool_input: dict[str, object],
|
|
425
|
+
) -> None:
|
|
426
|
+
written_file(tmp_path, "ask.md", "Should I archive the old branches?\n")
|
|
427
|
+
assert run_gate(monkeypatch, capsys, "Artifact", tool_input, tmp_path) == (0, "")
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def test_should_read_a_relative_path_from_the_working_directory(
|
|
431
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
432
|
+
) -> None:
|
|
433
|
+
written_file(tmp_path, "status.md", "Should I archive the old branches?\n")
|
|
434
|
+
exit_code, reason = run_gate(
|
|
435
|
+
monkeypatch, capsys, "SendUserFile", {"files": ["status.md"]}, tmp_path
|
|
436
|
+
)
|
|
437
|
+
assert exit_code == 2
|
|
438
|
+
assert "status.md" in reason
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def test_should_name_the_file_that_asks_among_several(
|
|
442
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
443
|
+
) -> None:
|
|
444
|
+
clean_path = written_file(tmp_path, "clean.md", "The build passed.\n")
|
|
445
|
+
asking_path = written_file(tmp_path, "asking.md", "Do you want the logs too?\n")
|
|
446
|
+
tool_input = {"files": [str(clean_path), str(asking_path)]}
|
|
447
|
+
exit_code, reason = run_gate(monkeypatch, capsys, "SendUserFile", tool_input, tmp_path)
|
|
448
|
+
assert exit_code == 2
|
|
449
|
+
assert "asking.md" in reason
|
|
450
|
+
assert "clean.md" not in reason
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
@pytest.mark.parametrize(
|
|
454
|
+
("tool_name", "tool_input"),
|
|
455
|
+
[
|
|
456
|
+
("SendUserFile", {"files": ["missing.md"]}),
|
|
457
|
+
("SendUserFile", {"files": "not-a-list"}),
|
|
458
|
+
("SendUserFile", "not-a-dict"),
|
|
459
|
+
("Write", {"file_path": "ask.md", "content": "Should I archive the old branches?"}),
|
|
460
|
+
],
|
|
461
|
+
)
|
|
462
|
+
def test_should_allow_a_payload_it_cannot_read(
|
|
463
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
464
|
+
capsys: pytest.CaptureFixture[str],
|
|
465
|
+
tmp_path: Path,
|
|
466
|
+
tool_name: str,
|
|
467
|
+
tool_input: object,
|
|
468
|
+
) -> None:
|
|
469
|
+
assert run_gate(monkeypatch, capsys, tool_name, tool_input, tmp_path) == (0, "")
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def test_should_deny_through_the_script_entry_point(tmp_path: Path) -> None:
|
|
473
|
+
file_path = written_file(tmp_path, "status.md", "Should I archive the old branches?\n")
|
|
474
|
+
hook_input = {
|
|
475
|
+
"hook_event_name": "PreToolUse",
|
|
476
|
+
"tool_name": "SendUserFile",
|
|
477
|
+
"tool_input": {"files": [str(file_path)]},
|
|
478
|
+
"cwd": str(tmp_path),
|
|
479
|
+
}
|
|
480
|
+
completed = subprocess.run(
|
|
481
|
+
[sys.executable, str(GATE_SCRIPT_PATH)],
|
|
482
|
+
input=json.dumps(hook_input),
|
|
483
|
+
capture_output=True,
|
|
484
|
+
text=True,
|
|
485
|
+
env={"HOME": str(tmp_path), "PATH": ""},
|
|
486
|
+
check=False,
|
|
487
|
+
)
|
|
488
|
+
assert completed.returncode == 2
|
|
489
|
+
assert "Should I archive the old branches?" in completed.stderr
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def test_question_for_user_should_return_none_for_plain_statements() -> None:
|
|
493
|
+
assert sent_file_question_gate.question_for_user("The build passed.\n", is_html=False) is None
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
@pytest.mark.parametrize(
|
|
497
|
+
"file_text",
|
|
498
|
+
[
|
|
499
|
+
"# T" + " " * 3000 + "x",
|
|
500
|
+
"**a**" + " " * 50000,
|
|
501
|
+
"**a**" + " " * 50000 + "x",
|
|
502
|
+
"## Open" + " " * 50000 + "questions #" + " " * 50000 + "x",
|
|
503
|
+
],
|
|
504
|
+
)
|
|
505
|
+
def test_question_for_user_should_read_long_space_runs_in_linear_time(file_text: str) -> None:
|
|
506
|
+
started_at = time.perf_counter()
|
|
507
|
+
sent_file_question_gate.question_for_user(file_text, is_html=False)
|
|
508
|
+
assert time.perf_counter() - started_at < 1
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
@pytest.mark.parametrize("capability_name", ["fetch", "sample", "mcp", "tts"])
|
|
512
|
+
def test_should_deny_a_page_whose_capability_saves_no_answer(
|
|
513
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
514
|
+
capsys: pytest.CaptureFixture[str],
|
|
515
|
+
tmp_path: Path,
|
|
516
|
+
capability_name: str,
|
|
517
|
+
) -> None:
|
|
518
|
+
file_path = written_file(tmp_path, "page.html", "<p>Which option do you prefer?</p>")
|
|
519
|
+
tool_input = {
|
|
520
|
+
"action": "publish",
|
|
521
|
+
"file_path": str(file_path),
|
|
522
|
+
"capabilities": {capability_name: {}},
|
|
523
|
+
}
|
|
524
|
+
exit_code, reason = run_gate(monkeypatch, capsys, "Artifact", tool_input, tmp_path)
|
|
525
|
+
assert exit_code == 2
|
|
526
|
+
assert "Which option do you prefer?" in reason
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
@pytest.mark.parametrize("capability_name", ["db", "artifact", "self"])
|
|
530
|
+
def test_should_allow_each_capability_that_saves_the_answer(
|
|
531
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
532
|
+
capsys: pytest.CaptureFixture[str],
|
|
533
|
+
tmp_path: Path,
|
|
534
|
+
capability_name: str,
|
|
535
|
+
) -> None:
|
|
536
|
+
file_path = written_file(tmp_path, "page.html", "<p>Which option do you prefer?</p>")
|
|
537
|
+
tool_input = {
|
|
538
|
+
"action": "publish",
|
|
539
|
+
"file_path": str(file_path),
|
|
540
|
+
"capabilities": {capability_name: {}, "user": {}},
|
|
541
|
+
}
|
|
542
|
+
assert run_gate(monkeypatch, capsys, "Artifact", tool_input, tmp_path) == (0, "")
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
@pytest.mark.parametrize(
|
|
546
|
+
("question_at_start", "quoted_question"),
|
|
547
|
+
[(True, "Should I archive the old branches?"), (False, "Do you want the logs too?")],
|
|
548
|
+
)
|
|
549
|
+
def test_should_read_the_start_and_end_of_a_file_over_the_size_limit(
|
|
550
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
551
|
+
capsys: pytest.CaptureFixture[str],
|
|
552
|
+
tmp_path: Path,
|
|
553
|
+
question_at_start: bool,
|
|
554
|
+
quoted_question: str,
|
|
555
|
+
) -> None:
|
|
556
|
+
filler_text = "The build passed on every runner. é\n" * 20000
|
|
557
|
+
file_text = (
|
|
558
|
+
quoted_question + "\n\n" + filler_text
|
|
559
|
+
if question_at_start
|
|
560
|
+
else filler_text + "\n" + quoted_question + "\n"
|
|
561
|
+
)
|
|
562
|
+
file_path = written_file(tmp_path, "large.md", file_text)
|
|
563
|
+
assert file_path.stat().st_size > 262144
|
|
564
|
+
exit_code, reason = run_gate(
|
|
565
|
+
monkeypatch, capsys, "SendUserFile", {"files": [str(file_path)]}, tmp_path
|
|
566
|
+
)
|
|
567
|
+
assert exit_code == 2
|
|
568
|
+
assert quoted_question in reason
|
|
569
|
+
assert "read only the first and last 128 KB" in reason
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
@pytest.mark.parametrize(
|
|
573
|
+
"file_bytes",
|
|
574
|
+
[
|
|
575
|
+
b"\x00\x01Should I archive it?\xff\xfe",
|
|
576
|
+
b"Should I archive the old branches?\n\xff\xfe\xfa",
|
|
577
|
+
],
|
|
578
|
+
)
|
|
579
|
+
def test_should_allow_a_file_that_is_not_utf8_text(
|
|
580
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
581
|
+
capsys: pytest.CaptureFixture[str],
|
|
582
|
+
tmp_path: Path,
|
|
583
|
+
file_bytes: bytes,
|
|
584
|
+
) -> None:
|
|
585
|
+
file_path = tmp_path / "binary.md"
|
|
586
|
+
file_path.write_bytes(file_bytes)
|
|
587
|
+
assert run_gate(monkeypatch, capsys, "SendUserFile", {"files": [str(file_path)]}, tmp_path) == (
|
|
588
|
+
0,
|
|
589
|
+
"",
|
|
590
|
+
)
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def test_should_quote_at_most_a_short_line_of_a_long_question(
|
|
594
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
595
|
+
) -> None:
|
|
596
|
+
long_question = "Should I archive " + "the old branch, " * 60 + "now?"
|
|
597
|
+
file_path = written_file(tmp_path, "long.md", long_question + "\n")
|
|
598
|
+
exit_code, reason = run_gate(
|
|
599
|
+
monkeypatch, capsys, "SendUserFile", {"files": [str(file_path)]}, tmp_path
|
|
600
|
+
)
|
|
601
|
+
assert exit_code == 2
|
|
602
|
+
quoted_text = reason.split('"')[1]
|
|
603
|
+
assert len(quoted_text) <= 160
|
|
604
|
+
assert quoted_text.startswith("Should I archive the old branch,")
|
|
605
|
+
assert quoted_text.endswith("...")
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
def test_should_say_when_it_read_only_the_start_and_end_of_a_clean_file(
|
|
609
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
610
|
+
) -> None:
|
|
611
|
+
file_path = written_file(tmp_path, "large.md", "The build passed on every runner.\n" * 20000)
|
|
612
|
+
hook_input = {
|
|
613
|
+
"hook_event_name": "PreToolUse",
|
|
614
|
+
"tool_name": "SendUserFile",
|
|
615
|
+
"tool_input": {"files": [str(file_path)]},
|
|
616
|
+
"cwd": str(tmp_path),
|
|
617
|
+
}
|
|
618
|
+
monkeypatch.setattr(
|
|
619
|
+
"sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
|
|
620
|
+
)
|
|
621
|
+
assert sent_file_question_gate.main() == 0
|
|
622
|
+
captured = capsys.readouterr()
|
|
623
|
+
assert captured.err == ""
|
|
624
|
+
system_message = json.loads(captured.out)["systemMessage"]
|
|
625
|
+
assert "large.md" in system_message
|
|
626
|
+
assert "middle of the file was not checked" in system_message
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
def slow_question_for_user(file_text: str, is_html: bool) -> str | None:
|
|
630
|
+
time.sleep(2)
|
|
631
|
+
return None
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
def test_findings_within_budget_should_give_up_when_the_read_runs_long(
|
|
635
|
+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
636
|
+
) -> None:
|
|
637
|
+
written_file(tmp_path, "status.md", "Should I archive the old branches?\n")
|
|
638
|
+
findings = sent_file_question_gate.findings_within_budget(
|
|
639
|
+
["status.md"], str(tmp_path), 5, is_plain_page_read=False
|
|
640
|
+
)
|
|
641
|
+
assert findings == [("status.md", "Should I archive the old branches?", False)]
|
|
642
|
+
monkeypatch.setattr(sent_file_question_gate, "question_for_user", slow_question_for_user)
|
|
643
|
+
assert (
|
|
644
|
+
sent_file_question_gate.findings_within_budget(
|
|
645
|
+
["status.md"], str(tmp_path), 0.2, is_plain_page_read=False
|
|
646
|
+
)
|
|
647
|
+
is None
|
|
648
|
+
)
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
def test_should_say_so_when_the_read_runs_past_its_budget(
|
|
652
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
653
|
+
) -> None:
|
|
654
|
+
written_file(tmp_path, "page.html", "<p>Which option do you prefer?</p>")
|
|
655
|
+
monkeypatch.setattr(sent_file_question_gate, "question_for_user", slow_question_for_user)
|
|
656
|
+
monkeypatch.setattr(sent_file_question_gate, "PARSED_READ_BUDGET_SECONDS", 0.1)
|
|
657
|
+
monkeypatch.setattr(sent_file_question_gate, "PLAIN_READ_BUDGET_SECONDS", 0.1)
|
|
658
|
+
hook_input = {
|
|
659
|
+
"hook_event_name": "PreToolUse",
|
|
660
|
+
"tool_name": "SendUserFile",
|
|
661
|
+
"tool_input": {"files": ["page.html"]},
|
|
662
|
+
"cwd": str(tmp_path),
|
|
663
|
+
}
|
|
664
|
+
monkeypatch.setattr(
|
|
665
|
+
"sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
|
|
666
|
+
)
|
|
667
|
+
assert sent_file_question_gate.main() == 0
|
|
668
|
+
system_message = json.loads(capsys.readouterr().out)["systemMessage"]
|
|
669
|
+
assert "could not finish reading page.html within 0.2 s" in system_message
|
|
670
|
+
assert "AskUserQuestion" in system_message
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
def slow_html_text_blocks(page_markup: str, heading_mark: str, task_box_mark: str) -> list[str]:
|
|
674
|
+
time.sleep(2)
|
|
675
|
+
return []
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
def test_should_read_a_page_without_the_parser_when_the_parser_runs_long(
|
|
679
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
680
|
+
) -> None:
|
|
681
|
+
written_file(tmp_path, "page.html", "<h2>Plan</h2><p>Checks passed. Should I merge it?</p>")
|
|
682
|
+
monkeypatch.setattr(sent_file_question_detector, "html_text_blocks", slow_html_text_blocks)
|
|
683
|
+
monkeypatch.setattr(sent_file_question_gate, "PARSED_READ_BUDGET_SECONDS", 0.2)
|
|
684
|
+
exit_code, reason = run_gate(
|
|
685
|
+
monkeypatch, capsys, "SendUserFile", {"files": ["page.html"]}, tmp_path
|
|
686
|
+
)
|
|
687
|
+
assert exit_code == 2
|
|
688
|
+
assert "Should I merge it?" in reason
|