claude-dev-env 8.119.0 → 8.119.2

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 (41) hide show
  1. package/bin/git_hooks_installer.mjs +26 -2
  2. package/bin/git_hooks_installer.test.mjs +36 -0
  3. package/bin/install-transaction.mjs +3 -1
  4. package/bin/install.mjs +12 -8
  5. package/bin/install.test.mjs +89 -0
  6. package/bin/install.transaction.test.mjs +34 -0
  7. package/bin/merge_profile_settings.mjs +3 -2
  8. package/bin/merge_profile_settings.test.mjs +25 -1
  9. package/bin/profile-stop-gate.mjs +2 -1
  10. package/bin/profile-stop-gate.test.mjs +26 -1
  11. package/bin/write-file-atomically.mjs +148 -0
  12. package/bin/write-file-atomically.test.mjs +263 -0
  13. package/hooks/blocking/followup_pr_dedupe.py +71 -13
  14. package/hooks/blocking/merge_review_gate.py +4 -0
  15. package/hooks/blocking/merge_review_gate_parts/create_folders.py +6 -2
  16. package/hooks/blocking/merge_review_gate_parts/open_request.py +40 -15
  17. package/hooks/blocking/resolve_thread_reply_gate.py +116 -1
  18. package/hooks/blocking/test_cloud_graphql_gate.py +4 -0
  19. package/hooks/blocking/test_followup_pr_dedupe.py +99 -0
  20. package/hooks/blocking/test_gh_global_account_switch_gate.py +4 -0
  21. package/hooks/blocking/test_headless_claude_broker_gate.py +4 -0
  22. package/hooks/blocking/test_merge_review_gate.py +133 -0
  23. package/hooks/blocking/test_python_windows_invocation_gate.py +4 -0
  24. package/hooks/blocking/test_resolve_thread_reply_gate.py +430 -0
  25. package/hooks/blocking/tests/test_create_folders.py +7 -0
  26. package/hooks/blocking/tests/test_open_request.py +21 -0
  27. package/hooks/features/conduct-gates.md +3 -3
  28. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +5 -0
  29. package/hooks/hooks_constants/merge_review_gate_constants.py +5 -0
  30. package/hooks/hooks_constants/pytest_invocation.py +24 -9
  31. package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +21 -1
  32. package/hooks/hooks_constants/test_pytest_invocation.py +25 -0
  33. package/hooks/hooks_constants/test_shell_command_mutation.py +11 -0
  34. package/hooks/hooks_constants/transcript_skill_scan_constants.py +22 -0
  35. package/hooks/session/test_npx_pele_guidance.py +4 -0
  36. package/hooks/test_transcript_skill_scan.py +72 -0
  37. package/hooks/transcript_skill_scan.py +138 -4
  38. package/hooks/validators/config/security_checks_constants.py +88 -0
  39. package/hooks/validators/security_checks.py +186 -1
  40. package/hooks/validators/test_security_checks.py +149 -0
  41. package/package.json +1 -1
@@ -49,6 +49,8 @@ from hooks_constants.shell_command_pipeline import (
49
49
  segments_with_following_operator,
50
50
  )
51
51
  from hooks_constants.shell_command_segments import (
52
+ ALL_LAUNCHER_WRAPPER_COMMANDS,
53
+ LAUNCHER_DURATION_PATTERN,
52
54
  LEADING_ASSIGNMENT_PATTERN,
53
55
  effective_leading_program,
54
56
  token_basename,
@@ -186,20 +188,33 @@ def _wrapper_basename(token: str) -> str:
186
188
  def _wrapper_program_index(all_segment_tokens: list[str]) -> int | None:
187
189
  """Return the index of the program a wrapper step reads.
188
190
 
189
- A wrapper with its own option grammar is read where it stands, so the
190
- ``-u HOME`` behind ``env`` stays an option. Any other leading program is
191
- found past assignments and plain launchers such as ``time`` and ``nohup``.
191
+ ::
192
+
193
+ nohup env -u HOME gh pr the index of env
194
+ nice -n 5 stdbuf -o0 gh pr the index of gh
195
+
196
+ Assignments and plain launchers such as ``time``, ``nohup`` and
197
+ ``nice -n 5`` are stepped past with their options. A wrapper with its
198
+ own option grammar is read where it stands, so the ``-u HOME`` behind
199
+ ``env`` stays an option.
192
200
  """
201
+ has_seen_launcher = False
193
202
  for each_index, each_token in enumerate(all_segment_tokens):
194
203
  if LEADING_ASSIGNMENT_PATTERN.match(each_token) is not None:
195
204
  continue
196
- if _wrapper_basename(each_token) in ALL_WRAPPER_OPTION_GRAMMARS_BY_NAME:
205
+ program_basename = _wrapper_basename(each_token)
206
+ if program_basename in ALL_WRAPPER_OPTION_GRAMMARS_BY_NAME:
197
207
  return each_index
198
- break
199
- leading_program = effective_leading_program(all_segment_tokens)
200
- if leading_program is None:
201
- return None
202
- return all_segment_tokens.index(leading_program)
208
+ if program_basename in ALL_LAUNCHER_WRAPPER_COMMANDS:
209
+ has_seen_launcher = True
210
+ continue
211
+ is_launcher_option = each_token.startswith(SHORT_OPTION_PREFIX) or (
212
+ LAUNCHER_DURATION_PATTERN.match(each_token) is not None
213
+ )
214
+ if has_seen_launcher and is_launcher_option:
215
+ continue
216
+ return each_index
217
+ return None
203
218
 
204
219
 
205
220
  def _all_tokens_after_one_wrapper(all_segment_tokens: list[str]) -> list[str] | None:
@@ -11,6 +11,24 @@ SHELL_COMMAND_KEY = "command"
11
11
  SHELL_REPLY_COMMAND_PATTERN = re.compile(
12
12
  r"\bgh\s+api\b.*(?:/comments/\d+/replies\b|addPullRequestReviewThreadReply)", re.DOTALL
13
13
  )
14
+ REPLY_SCRIPT_FILE_NAME = "review_reply_post.py"
15
+ REPLY_SCRIPT_REQUEST_SUBCOMMAND = "request"
16
+ REPLY_SCRIPT_LAUNCHER_NAME_PATTERN = re.compile(r"(?:python(?:3(?:\.\d+)?)?|py)(?:\.exe)?")
17
+ REPLY_SCRIPT_COMMENT_ID_OPTION = "--comment-id"
18
+ REPLY_SCRIPT_THREAD_ID_OPTION = "--thread-id"
19
+ ALL_REPLY_SCRIPT_VALUE_OPTIONS = (
20
+ "--pull",
21
+ REPLY_SCRIPT_COMMENT_ID_OPTION,
22
+ REPLY_SCRIPT_THREAD_ID_OPTION,
23
+ "--body-file",
24
+ "--wait-seconds",
25
+ )
26
+ ALL_REPLY_SCRIPT_FLAG_OPTIONS = ("--supersede",)
27
+ SHELL_OPTION_PREFIX = "-"
28
+ SHELL_UNREADABLE_CHARACTER_PATTERN = re.compile(r"[;&|<>()`$#{}*?\[\]~!\\\n\r]")
29
+ SHELL_OPTION_VALUE_SEPARATOR = "="
30
+ SHELL_PATH_SEPARATOR = "/"
31
+ ABSOLUTE_LAUNCHER_FOLDER_PATTERN = re.compile(r"(?:[A-Za-z]:)?/")
14
32
  SHELL_RESOLVE_COMMAND_PATTERN = re.compile(r"\bgh\s+api\s+graphql\b.*\bresolveReviewThread\b", re.DOTALL)
15
33
  SHELL_RESOLVE_COMMENT_ID_PATTERN = re.compile(r"/pulls/\d+/ccr/comments/(\d+)/resolve\b")
16
34
  ALL_THREAD_ID_INPUT_KEYS = ("threadID", "threadId")
@@ -23,7 +41,9 @@ TEXT_FIELD = "text"
23
41
  MISSING_REPLY_REASON = (
24
42
  "This review thread has no reply on it that posted since the last user prompt and the last"
25
43
  " resolve of the thread. Post the reply on this thread with add_reply_to_pull_request_comment,"
26
- " or with gh api graphql addPullRequestReviewThreadReply and the thread's PRRT_ id, confirm its"
44
+ " with gh api graphql addPullRequestReviewThreadReply and the thread's PRRT_ id, or with a"
45
+ " review_reply_post.py request run as one plain command (python3, the script, request, then only"
46
+ " full option names, each once) that exits 0, confirm its"
27
47
  " result came back without an error, then resolve the thread by its PRRT_ id or by the comment id the reply answered. A comment id"
28
48
  " ties to a thread only through a pull_request_read get_review_comments result in this session."
29
49
  )
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
  import pytest
6
6
 
7
7
  from hooks_constants.pytest_invocation import (
8
+ all_tokens_after_wrappers,
8
9
  segment_reports_a_pytest_exit_code,
9
10
  segment_runs_pytest,
10
11
  )
@@ -134,3 +135,27 @@ def test_should_not_false_classify_option_value_named_pytest() -> None:
134
135
  def test_should_not_false_classify_script_argument_named_pytest() -> None:
135
136
  all_tokens = _first_segment_tokens("python myscript.py -m pytest")
136
137
  assert segment_runs_pytest(all_tokens) is False
138
+
139
+
140
+ @pytest.mark.parametrize(
141
+ ("all_segment_tokens", "all_expected_tokens"),
142
+ [
143
+ (["nohup", "env", "-u", "GH_REPO", "gh", "pr"], ["gh", "pr"]),
144
+ (["GH_REPO=o/r", "nice", "env", "-u", "GH_REPO", "gh", "pr"], ["gh", "pr"]),
145
+ (
146
+ ["nice", "-n", "5", "env", "-u", "GH_REPO", "stdbuf", "-o0", "gh", "pr"],
147
+ ["stdbuf", "-o0", "gh", "pr"],
148
+ ),
149
+ (["stdbuf", "-o0", "env", "-u", "FOO", "gh", "pr"], ["gh", "pr"]),
150
+ (["time", "nohup", "env", "--unset", "FOO", "nice", "gh", "pr"], ["nice", "gh", "pr"]),
151
+ (["env", "nice", "env", "-u", "FOO", "gh", "pr"], ["gh", "pr"]),
152
+ (["nohup", "timeout", "-s", "KILL", "60", "gh", "pr"], ["gh", "pr"]),
153
+ (["setsid", "sudo", "-u", "ci", "gh", "pr"], ["gh", "pr"]),
154
+ (["nohup", "gh", "pr"], ["nohup", "gh", "pr"]),
155
+ (["gh", "pr"], ["gh", "pr"]),
156
+ ],
157
+ )
158
+ def test_all_tokens_after_wrappers_should_step_over_an_option_reading_wrapper_behind_launchers(
159
+ all_segment_tokens: list[str], all_expected_tokens: list[str]
160
+ ) -> None:
161
+ assert all_tokens_after_wrappers(all_segment_tokens) == all_expected_tokens
@@ -22,6 +22,17 @@ def test_should_read_a_write_as_mutating(command: str) -> None:
22
22
  assert is_mutating_shell_command(command) is True
23
23
 
24
24
 
25
+ @pytest.mark.parametrize(
26
+ "command",
27
+ [
28
+ "nohup env -u FOO git push origin main",
29
+ "time -p env -u HOME bash -c 'touch x'",
30
+ ],
31
+ )
32
+ def test_should_read_a_write_behind_a_launcher_and_env_unset_as_mutating(command: str) -> None:
33
+ assert is_mutating_shell_command(command) is True
34
+
35
+
25
36
  @pytest.mark.parametrize(
26
37
  "command",
27
38
  [
@@ -10,6 +10,28 @@ META_ENTRY_FIELD = "isMeta"
10
10
  TOOL_RESULT_BLOCK_TYPE = "tool_result"
11
11
  IS_ERROR_FIELD = "is_error"
12
12
  TOOL_USE_ID_FIELD = "tool_use_id"
13
+ ORIGIN_FIELD = "origin"
14
+ ORIGIN_KIND_FIELD = "kind"
15
+ TASK_NOTIFICATION_ORIGIN_KIND = "task-notification"
16
+ RUN_IN_BACKGROUND_INPUT_KEY = "run_in_background"
17
+ MOVED_TO_BACKGROUND_RESULT_PATTERN = (
18
+ r"(?m)^Command (?:running in background with ID: |was manually backgrounded by user with ID: "
19
+ r"|did not complete within [^\n]*\bmoved to the background\b)"
20
+ )
21
+ ALL_BACKGROUND_SHELL_TOOL_NAMES = frozenset({"Bash", "PowerShell"})
22
+ ATTACHMENT_ENTRY_TYPE = "attachment"
23
+ ATTACHMENT_FIELD = "attachment"
24
+ QUEUED_COMMAND_ATTACHMENT_TYPE = "queued_command"
25
+ QUEUED_COMMAND_PROMPT_FIELD = "prompt"
26
+ RESULT_TEXT_FIELD = "text"
27
+ RESULT_TEXT_SEPARATOR = "\n"
28
+ TASK_NOTIFICATION_OPENING_TAG = "<task-notification>"
29
+ TASK_NOTIFICATION_CLOSING_TAG = "</task-notification>"
30
+ TASK_NOTIFICATION_SUMMARY_OPENING_TAG = "<summary>"
31
+ TASK_NOTIFICATION_TOOL_USE_ID_PATTERN = r"<tool-use-id>\s*([^<\s]+)\s*</tool-use-id>"
32
+ TASK_NOTIFICATION_COMPLETED_STATUS_PATTERN = r"<status>\s*completed\s*</status>"
33
+ TASK_NOTIFICATION_ANY_STATUS_PATTERN = r"<status>"
34
+ TASK_NOTIFICATION_EXIT_ZERO_ENDING_PATTERN = r" completed \(exit code 0\)\s*</summary>\s*</task-notification>\s*\Z"
13
35
  NEWLINE_BYTE = b"\n"
14
36
  CARRIAGE_RETURN_BYTE = b"\r"
15
37
  ALL_LINE_BREAK_BYTES = (NEWLINE_BYTE, CARRIAGE_RETURN_BYTE)
@@ -102,6 +102,10 @@ def test_npx_pele_commands_match(command_text: str) -> None:
102
102
  assert npx_pele_guidance.is_npx_pele_command({"command": command_text})
103
103
 
104
104
 
105
+ def test_should_match_npx_pele_behind_a_launcher_and_env_unset() -> None:
106
+ assert npx_pele_guidance.is_npx_pele_command({"command": "nohup env -u FOO npx pele a.mmd"})
107
+
108
+
105
109
  @pytest.mark.parametrize(
106
110
  "command_text",
107
111
  [
@@ -232,6 +232,78 @@ def should_list_each_tool_call_since_the_last_prompt_with_its_outcome() -> None:
232
232
  ]
233
233
 
234
234
 
235
+ def should_count_a_background_call_only_after_its_notice_reports_exit_code_zero() -> None:
236
+ background_input: dict[str, object] = {"command": "sleep 1", "run_in_background": True}
237
+
238
+ def _call(tool_use_id: str) -> str:
239
+ tool_use = {"type": "tool_use", "id": tool_use_id, "name": "Bash", "input": background_input}
240
+ return json.dumps({"type": "assistant", "message": {"content": [tool_use]}})
241
+
242
+ def _started(tool_use_id: str) -> str:
243
+ tool_result = {"type": "tool_result", "tool_use_id": tool_use_id, "is_error": False, "content": "started"}
244
+ return json.dumps({"type": "user", "message": {"content": [tool_result]}})
245
+
246
+ def _notice(tool_use_id: str, status: str, exit_code: int) -> str:
247
+ text = (
248
+ f"<task-notification><tool-use-id>{tool_use_id}</tool-use-id><status>{status}</status>"
249
+ f"<summary>Background command {status} (exit code {exit_code})</summary></task-notification>"
250
+ )
251
+ return json.dumps({"type": "user", "origin": {"kind": "task-notification"}, "message": {"content": text}})
252
+
253
+ lines = [
254
+ json.dumps({"type": "user", "message": {"content": "First."}}),
255
+ _call("done"),
256
+ _started("done"),
257
+ _call("failed"),
258
+ _started("failed"),
259
+ _call("running"),
260
+ _started("running"),
261
+ _notice("done", "completed", 0),
262
+ _notice("failed", "failed", 3),
263
+ ]
264
+ assert tool_calls_since_last_prompt(lines) == [
265
+ TranscriptToolCall("Bash", background_input, True),
266
+ TranscriptToolCall("Bash", background_input, False),
267
+ TranscriptToolCall("Bash", background_input, False),
268
+ ]
269
+
270
+
271
+ def should_read_queued_command_attachments_and_calls_moved_to_the_background() -> None:
272
+ def _call(tool_use_id: str, tool_input: dict[str, object]) -> str:
273
+ tool_use = {"type": "tool_use", "id": tool_use_id, "name": "Bash", "input": tool_input}
274
+ return json.dumps({"type": "assistant", "message": {"content": [tool_use]}})
275
+
276
+ def _result(tool_use_id: str, text: str) -> str:
277
+ tool_result = {"type": "tool_result", "tool_use_id": tool_use_id, "is_error": False, "content": text}
278
+ return json.dumps({"type": "user", "message": {"content": [tool_result]}})
279
+
280
+ def _queued(text: str, origin_kind: str) -> str:
281
+ attachment = {"type": "queued_command", "prompt": text, "origin": {"kind": origin_kind}}
282
+ return json.dumps({"type": "attachment", "attachment": attachment})
283
+
284
+ def _notice(tool_use_id: str) -> str:
285
+ return (
286
+ f"<task-notification><tool-use-id>{tool_use_id}</tool-use-id><status>completed</status>"
287
+ '<summary>Background command "x" completed (exit code 0)</summary></task-notification>'
288
+ )
289
+
290
+ all_turn_lines = [
291
+ json.dumps({"type": "user", "message": {"content": "First."}}),
292
+ _call("flag-text", {"run_in_background": "true"}),
293
+ _result("flag-text", "Command running in background with ID: b1"),
294
+ _call("moved", {}),
295
+ _result("moved", "Command did not complete within its 120s timeout and was moved to the background."),
296
+ _call("waiting", {"run_in_background": 1}),
297
+ _result("waiting", "Command running in background with ID: b3"),
298
+ _call("plain", {}),
299
+ _result("plain", "done"),
300
+ _queued(_notice("flag-text"), "task-notification"),
301
+ _queued(_notice("moved"), "task-notification"),
302
+ ]
303
+ assert [each_call.succeeded for each_call in tool_calls_since_last_prompt(all_turn_lines)] == [True, True, False, True]
304
+ assert tool_calls_since_last_prompt([*all_turn_lines, _queued("Stop.", "human")]) == []
305
+
306
+
235
307
  NEWEST_FIRST_SKILL_NAMES = ("pr-lifecycle",)
236
308
  NEWEST_FIRST_MARKERS = ("<command-name>/pr-lifecycle</command-name>",)
237
309
  ALL_GENERATED_LINE_KINDS = (
@@ -17,14 +17,33 @@ from typing import BinaryIO, NamedTuple
17
17
  from hooks_constants.transcript_skill_scan_constants import (
18
18
  ALL_LINE_BREAK_BYTES,
19
19
  ASSISTANT_ENTRY_TYPE,
20
+ ALL_BACKGROUND_SHELL_TOOL_NAMES,
21
+ ATTACHMENT_ENTRY_TYPE,
22
+ ATTACHMENT_FIELD,
20
23
  CARRIAGE_RETURN_BYTE,
21
24
  COMMAND_ARGUMENTS_PATTERN,
22
25
  COMPACT_BOUNDARY_SUBTYPE,
23
26
  FORWARD_COPY_BYTE_CAP,
24
27
  IS_ERROR_FIELD,
25
28
  META_ENTRY_FIELD,
29
+ MOVED_TO_BACKGROUND_RESULT_PATTERN,
26
30
  NEWLINE_BYTE,
31
+ ORIGIN_FIELD,
32
+ ORIGIN_KIND_FIELD,
33
+ QUEUED_COMMAND_ATTACHMENT_TYPE,
34
+ QUEUED_COMMAND_PROMPT_FIELD,
35
+ RESULT_TEXT_FIELD,
36
+ RESULT_TEXT_SEPARATOR,
37
+ RUN_IN_BACKGROUND_INPUT_KEY,
27
38
  SKILL_TOOL_NAME,
39
+ TASK_NOTIFICATION_ORIGIN_KIND,
40
+ TASK_NOTIFICATION_ANY_STATUS_PATTERN,
41
+ TASK_NOTIFICATION_CLOSING_TAG,
42
+ TASK_NOTIFICATION_COMPLETED_STATUS_PATTERN,
43
+ TASK_NOTIFICATION_EXIT_ZERO_ENDING_PATTERN,
44
+ TASK_NOTIFICATION_OPENING_TAG,
45
+ TASK_NOTIFICATION_SUMMARY_OPENING_TAG,
46
+ TASK_NOTIFICATION_TOOL_USE_ID_PATTERN,
28
47
  TOOL_RESULT_BLOCK_TYPE,
29
48
  TRANSCRIPT_BLOCK_BYTES,
30
49
  TRANSCRIPT_LINE_BYTE_CAP,
@@ -523,6 +542,83 @@ def _succeeded_result_ids(all_entry_fields: dict[str, object]) -> set[object]:
523
542
  }
524
543
 
525
544
 
545
+ def _queued_command(all_entry_fields: dict[str, object]) -> dict[str, object] | None:
546
+ attachment = all_entry_fields.get(ATTACHMENT_FIELD)
547
+ if all_entry_fields.get("type") != ATTACHMENT_ENTRY_TYPE or not isinstance(attachment, dict):
548
+ return None
549
+ return attachment if attachment.get("type") == QUEUED_COMMAND_ATTACHMENT_TYPE else None
550
+
551
+
552
+ def _is_task_notification_origin(all_fields: dict[str, object]) -> bool:
553
+ origin = all_fields.get(ORIGIN_FIELD)
554
+ return isinstance(origin, dict) and origin.get(ORIGIN_KIND_FIELD) == TASK_NOTIFICATION_ORIGIN_KIND
555
+
556
+
557
+ def _task_notification_text(all_entry_fields: dict[str, object]) -> str | None:
558
+ queued_command = _queued_command(all_entry_fields)
559
+ if queued_command is not None:
560
+ if not _is_task_notification_origin(queued_command):
561
+ return None
562
+ queued_text = queued_command.get(QUEUED_COMMAND_PROMPT_FIELD)
563
+ return queued_text if isinstance(queued_text, str) else ""
564
+ if not _is_task_notification_origin(all_entry_fields):
565
+ return None
566
+ return _user_command_text(all_entry_fields) or ""
567
+
568
+
569
+ def _result_block_text(result_content: object) -> str:
570
+ if isinstance(result_content, str):
571
+ return result_content
572
+ all_parts = result_content if isinstance(result_content, list) else []
573
+ return RESULT_TEXT_SEPARATOR.join(
574
+ each_part[RESULT_TEXT_FIELD]
575
+ for each_part in all_parts
576
+ if isinstance(each_part, dict) and isinstance(each_part.get(RESULT_TEXT_FIELD), str)
577
+ )
578
+
579
+
580
+ def _moved_to_background_result_ids(all_entry_fields: dict[str, object]) -> set[object]:
581
+ return {
582
+ each_block.get(TOOL_USE_ID_FIELD)
583
+ for each_block in _content_blocks(all_entry_fields, USER_ENTRY_TYPE)
584
+ if each_block.get("type") == TOOL_RESULT_BLOCK_TYPE
585
+ and re.search(MOVED_TO_BACKGROUND_RESULT_PATTERN, _result_block_text(each_block.get("content"))) is not None
586
+ }
587
+
588
+
589
+ def _background_ids_finished_without_error(notification_text: str) -> set[object]:
590
+ """Return the tool use id of a notice that reports exit code 0, as a set of one, else an empty set.
591
+
592
+ ::
593
+
594
+ one notice, status completed, summary ends "(exit code 0)" -> {its id}
595
+ one notice, status failed, summary ends "(exit code 3)" -> {}
596
+ two notices in one text, either outcome -> {}
597
+ a failed notice whose summary quotes "(exit code 0)" -> {}
598
+ a failed notice whose summary quotes a whole passing notice -> {}
599
+
600
+ The harness writes the id and the status before the summary, and the exit
601
+ code as the last words of the summary. The summary also quotes the
602
+ command's description, which the agent wrote, so only the text before the
603
+ summary and the very end of the notice are read.
604
+ """
605
+ if (
606
+ notification_text.count(TASK_NOTIFICATION_OPENING_TAG) != 1
607
+ or notification_text.count(TASK_NOTIFICATION_CLOSING_TAG) != 1
608
+ ):
609
+ return set()
610
+ harness_header = notification_text.partition(TASK_NOTIFICATION_SUMMARY_OPENING_TAG)[0]
611
+ all_tool_use_ids = re.findall(TASK_NOTIFICATION_TOOL_USE_ID_PATTERN, harness_header)
612
+ if (
613
+ len(all_tool_use_ids) != 1
614
+ or len(re.findall(TASK_NOTIFICATION_ANY_STATUS_PATTERN, harness_header)) != 1
615
+ or re.search(TASK_NOTIFICATION_COMPLETED_STATUS_PATTERN, harness_header) is None
616
+ or re.search(TASK_NOTIFICATION_EXIT_ZERO_ENDING_PATTERN, notification_text) is None
617
+ ):
618
+ return set()
619
+ return set(all_tool_use_ids)
620
+
621
+
526
622
  def tool_calls_since_last_prompt(all_transcript_lines: Iterable[str]) -> list[TranscriptToolCall]:
527
623
  """Return the tool calls after the last user prompt, in order, each marked by whether it succeeded.
528
624
 
@@ -532,23 +628,61 @@ def tool_calls_since_last_prompt(all_transcript_lines: Iterable[str]) -> list[Tr
532
628
  prompt, reply call, its result with is_error -> [reply failed]
533
629
  prompt, resolve call with no result yet -> [resolve failed]
534
630
  reply call and result, then a new prompt -> []
631
+ reply call and result, then a queued prompt -> []
632
+ background call, started -> [call failed]
633
+ background call, notice of exit code 0 -> [call succeeded]
634
+ background call, notice of exit code 3 -> [call failed]
635
+ shell call whose result is the harness's own line -> [call failed] until its notice
636
+ any other call whose result holds those words -> [call succeeded]
535
637
 
536
638
  A hook that denies a call leaves an errored result, so a denied call does
537
- not count as a success. A tool result leaves the turn open.
639
+ not count as a success. A tool result leaves the turn open. A background
640
+ task notice is no user prompt, so it leaves the turn open too, whether it
641
+ is a user entry or a queued command attachment. Any other queued command
642
+ attachment, such as a prompt the user sent mid-turn, ends the turn as a
643
+ user prompt does. A call runs in the background when its
644
+ ``run_in_background`` input is truthy, or when it is a ``Bash`` or
645
+ ``PowerShell`` call whose result holds a line the harness writes for a
646
+ command in the background, starting ``Command running in background with
647
+ ID:``, ``Command was manually backgrounded by user with ID:`` or ``Command
648
+ did not complete within``. The same words in the result of any other tool,
649
+ or elsewhere in a line, change nothing. Such a call succeeds only
650
+ when the notice that names its tool use id reports a completed status
651
+ with exit code 0.
538
652
 
539
653
  Args:
540
654
  all_transcript_lines: JSON transcript entries, one per line.
541
655
  """
542
656
  all_calls: list[tuple[object, str, dict[str, object]]] = []
543
657
  all_succeeded_ids: set[object] = set()
658
+ all_finished_background_ids: set[object] = set()
659
+ all_moved_to_background_ids: set[object] = set()
544
660
  all_entries = (each_entry for each_entry in map(_parsed_entry, all_transcript_lines) if each_entry is not None)
545
661
  for each_entry_fields in all_entries:
546
- if _is_user_prompt(each_entry_fields):
547
- all_calls, all_succeeded_ids = [], set()
662
+ notification_text = _task_notification_text(each_entry_fields)
663
+ if notification_text is not None:
664
+ all_finished_background_ids.update(_background_ids_finished_without_error(notification_text))
665
+ continue
666
+ if _is_user_prompt(each_entry_fields) or _queued_command(each_entry_fields) is not None:
667
+ all_calls, all_succeeded_ids, all_finished_background_ids = [], set(), set()
668
+ all_moved_to_background_ids = set()
548
669
  continue
549
670
  all_calls.extend(_tool_use_calls(each_entry_fields))
550
671
  all_succeeded_ids.update(_succeeded_result_ids(each_entry_fields))
672
+ all_moved_to_background_ids.update(_moved_to_background_result_ids(each_entry_fields))
551
673
  return [
552
- TranscriptToolCall(each_name, each_input, each_id is not None and each_id in all_succeeded_ids)
674
+ TranscriptToolCall(
675
+ each_name,
676
+ each_input,
677
+ each_id is not None
678
+ and each_id in all_succeeded_ids
679
+ and (
680
+ each_id in all_finished_background_ids
681
+ or not (
682
+ each_input.get(RUN_IN_BACKGROUND_INPUT_KEY)
683
+ or (each_name in ALL_BACKGROUND_SHELL_TOOL_NAMES and each_id in all_moved_to_background_ids)
684
+ )
685
+ ),
686
+ )
553
687
  for each_id, each_name, each_input in all_calls
554
688
  ]
@@ -0,0 +1,88 @@
1
+ """Constants for the hardcoded-secret value judgment in security_checks."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ MINIMUM_MESSAGE_WORD_COUNT: int = 4
8
+
9
+ MAXIMUM_ORDINARY_WORD_LENGTH: int = 14
10
+
11
+ MAXIMUM_CAPITALS_WORD_LENGTH: int = 5
12
+
13
+ MAXIMUM_CAPITALS_WORD_COUNT: int = 1
14
+
15
+ MINIMUM_GROUP_WORD_LENGTH: int = 4
16
+
17
+ MAXIMUM_SHORT_WORD_LENGTH: int = 2
18
+
19
+ MINIMUM_GROUP_RUN_LENGTH: int = 3
20
+
21
+ MINIMUM_VARIABLE_SEGMENT_COUNT: int = 2
22
+
23
+ MINIMUM_FLAG_SEGMENT_COUNT: int = 1
24
+
25
+ WORD_SEPARATOR: str = " "
26
+
27
+ WORD_EDGE_PUNCTUATION: str = ".,;:!?()"
28
+
29
+ LABEL_CLOSING_MARK: str = ":"
30
+
31
+ NAME_SEGMENT_SEPARATOR: str = "_"
32
+
33
+ FLAG_SEGMENT_SEPARATOR: str = "-"
34
+
35
+ FLAG_LEAD: str = "--"
36
+
37
+ FORMAT_PLACEHOLDER_PATTERN: re.Pattern[str] = re.compile(
38
+ r"\{(?:[a-z]+(?:_[a-z]+)*)?(?:![rsa])?(?::[<>^]?[0-9,.]*[a-z%]?)?\}"
39
+ )
40
+
41
+ UNPRONOUNCEABLE_RUN_PATTERN: re.Pattern[str] = re.compile(
42
+ r"[b-df-hj-np-tv-xz]{5,}", re.IGNORECASE
43
+ )
44
+
45
+ ALL_PASSWORD_NAME_WORDS: frozenset[str] = frozenset(
46
+ {"password", "passwd", "pwd", "passphrase", "passcode", "phrase", "answer"}
47
+ )
48
+
49
+ ALL_PASSWORD_NAME_SEGMENTS: frozenset[str] = frozenset({"pass"})
50
+
51
+ ALL_AUTH_SCHEME_LEAD_WORDS: frozenset[str] = frozenset(
52
+ {"bearer", "basic", "digest", "negotiate", "token", "key", "apikey", "bot"}
53
+ )
54
+
55
+ ALL_AUTH_SCHEME_WORDS: frozenset[str] = frozenset(
56
+ {"bearer", "basic", "digest", "negotiate"}
57
+ )
58
+
59
+ ALL_SENTENCE_FUNCTION_WORDS: frozenset[str] = frozenset(
60
+ {
61
+ "a", "an", "the", "is", "are", "was", "were", "be", "been", "no", "not",
62
+ "to", "of", "in", "on", "for", "with", "from", "by", "at", "and", "or",
63
+ "this", "that", "it", "before", "after", "has", "have", "can", "cannot",
64
+ "will", "when", "if", "as", "do", "so", "up",
65
+ }
66
+ )
67
+
68
+ ALL_POINTER_VALUE_WORDS: tuple[str, ...] = (
69
+ "token",
70
+ "key",
71
+ "secret",
72
+ "auth",
73
+ "password",
74
+ "credential",
75
+ )
76
+
77
+ ALL_VARIABLE_NAME_SUFFIXES: tuple[str, ...] = (
78
+ "_env",
79
+ "_env_var",
80
+ "_env_name",
81
+ "_variable_name",
82
+ )
83
+
84
+ ALL_FLAG_NAME_SUFFIXES: tuple[str, ...] = ("_flag", "_option")
85
+
86
+ VARIABLE_NAME_SEGMENT_PATTERN: re.Pattern[str] = re.compile(r"[A-Z]{1,12}")
87
+
88
+ FLAG_SEGMENT_PATTERN: re.Pattern[str] = re.compile(r"[a-z]{1,12}")