@elvis1513/auto-coding-skill 4.2.6 → 4.2.8

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.
@@ -4,12 +4,15 @@ from __future__ import annotations
4
4
  import argparse
5
5
  import base64
6
6
  import contextlib
7
+ import hashlib
7
8
  import io
8
9
  import json
9
10
  import os
11
+ import signal
10
12
  import subprocess
11
13
  import sys
12
14
  import tempfile
15
+ import threading
13
16
  import time
14
17
  import tomllib
15
18
  import unittest
@@ -113,6 +116,9 @@ class AutoCodingProfileTests(unittest.TestCase):
113
116
  "diff_base": "HEAD~1",
114
117
  "diff_head": "HEAD",
115
118
  "diff_fingerprint": "a" * 64,
119
+ "diff_artifact_path": "/tmp/review.patch",
120
+ "diff_artifact_sha256": "b" * 64,
121
+ "diff_artifact_format": "git-binary-patch-v1",
116
122
  "owning_fixer": "fixer-1",
117
123
  }
118
124
  assignment.update(overrides)
@@ -138,6 +144,54 @@ class AutoCodingProfileTests(unittest.TestCase):
138
144
  target.write_text("change\n", encoding="utf-8")
139
145
  return repo, cfg
140
146
 
147
+ def test_direct_cleanup_restores_active_state_when_registry_removal_fails(self) -> None:
148
+ repo, _ = self.make_repo()
149
+ manifest = {
150
+ "task_id": "DIRECT-CLEANUP-RETRY",
151
+ "task_uuid": "direct-cleanup-uuid",
152
+ "worktree_path": str(repo),
153
+ }
154
+ active_path = ap._worktree_manifest_path(repo)
155
+ ap._write_json_object(active_path, manifest)
156
+
157
+ with mock.patch.object(
158
+ ap,
159
+ "_delete_task_manifest",
160
+ side_effect=APError("simulated registry failure"),
161
+ ):
162
+ with self.assertRaisesRegex(APError, "simulated registry failure"):
163
+ ap._clear_direct_task(repo, manifest)
164
+
165
+ restored = ap._read_json_object(active_path)
166
+ self.assertIsNotNone(restored)
167
+ self.assertEqual(manifest["task_uuid"], restored["task_uuid"])
168
+
169
+ def test_registry_failure_preserves_review_evidence_for_retry(self) -> None:
170
+ repo, _ = self.make_repo()
171
+ manifest = {
172
+ "task_id": "CLEANUP-EVIDENCE",
173
+ "task_uuid": "cleanup-evidence-uuid",
174
+ "worktree_path": str(repo),
175
+ }
176
+ registry_path = ap._task_registry_path(repo, manifest["task_id"])
177
+ ap._write_json_object(registry_path, manifest)
178
+ review_dir = ap._task_review_dir(repo, manifest["task_id"], create=True)
179
+ evidence_path = review_dir / "evidence.patch"
180
+ ap._write_private_bytes(evidence_path, b"immutable evidence\n")
181
+ original_unlink = Path.unlink
182
+
183
+ def fail_registry_unlink(path: Path, *args: object, **kwargs: object) -> None:
184
+ if path == registry_path:
185
+ raise PermissionError("simulated registry permission failure")
186
+ original_unlink(path, *args, **kwargs)
187
+
188
+ with mock.patch.object(Path, "unlink", new=fail_registry_unlink):
189
+ with self.assertRaisesRegex(APError, "Cannot remove task registry state"):
190
+ ap._delete_task_manifest(repo, manifest)
191
+
192
+ self.assertTrue(registry_path.exists())
193
+ self.assertEqual(b"immutable evidence\n", evidence_path.read_bytes())
194
+
141
195
  @staticmethod
142
196
  def write_config(repo: Path, cfg: dict) -> None:
143
197
  path = repo / "docs" / "ENGINEERING.md"
@@ -628,6 +682,9 @@ class AutoCodingProfileTests(unittest.TestCase):
628
682
  "diff_base": "HEAD~1",
629
683
  "diff_head": "HEAD",
630
684
  "diff_fingerprint": fingerprint,
685
+ "diff_artifact_path": "/tmp/review.patch",
686
+ "diff_artifact_sha256": "b" * 64,
687
+ "diff_artifact_format": "git-binary-patch-v1",
631
688
  "owning_fixer": "fixer-1",
632
689
  }
633
690
  ap._validate_orchestration_contract("assignment", assignment)
@@ -726,61 +783,352 @@ class AutoCodingProfileTests(unittest.TestCase):
726
783
  repo, _ = self.make_repo()
727
784
  assignment_path = repo / "assignment.json"
728
785
  result_path = repo / "result.json"
786
+ reviewer_config = repo / ".agents" / "agents" / "reviewer.toml"
787
+ reviewer_config.parent.mkdir(parents=True)
788
+ reviewer_config.write_text(
789
+ 'name = "reviewer"\n'
790
+ 'model = "vendor/reviewer-model"\n'
791
+ 'developer_instructions = "review only the immutable artifact"\n',
792
+ encoding="utf-8",
793
+ )
729
794
  with mock.patch.object(ap.shutil, "which", return_value="/usr/local/bin/codex"):
730
- command = ap._codex_reviewer_command(repo, assignment_path, result_path)
795
+ focused = ap._codex_reviewer_command(
796
+ repo,
797
+ assignment_path,
798
+ result_path,
799
+ review_depth="focused",
800
+ )
801
+ deep = ap._codex_reviewer_command(
802
+ repo,
803
+ assignment_path,
804
+ result_path,
805
+ review_depth="deep",
806
+ )
731
807
 
732
- self.assertEqual("/usr/local/bin/codex", command[0])
733
- self.assertIn("--ephemeral", command)
734
- self.assertIn("--ignore-user-config", command)
735
- self.assertIn("read-only", command)
736
- self.assertIn('model_reasoning_effort="xhigh"', command)
808
+ self.assertEqual("/usr/local/bin/codex", focused[0])
809
+ self.assertIn("--json", focused)
810
+ self.assertIn("--ephemeral", focused)
811
+ self.assertIn("--ignore-user-config", focused)
812
+ self.assertIn("read-only", focused)
813
+ self.assertIn('model_reasoning_effort="high"', focused)
814
+ self.assertIn('model_reasoning_effort="xhigh"', deep)
815
+ self.assertEqual("vendor/reviewer-model", focused[focused.index("--model") + 1])
816
+ prompt = focused[-1]
817
+ self.assertIn("docs/tools/autopipeline/ap.py review-artifact --file", prompt)
818
+ self.assertIn("diff_artifact_sha256", prompt)
819
+ self.assertIn("never substitute a live git diff", prompt)
737
820
  developer_override = next(
738
- item for item in command if item.startswith("developer_instructions=")
821
+ item for item in focused if item.startswith("developer_instructions=")
739
822
  )
740
823
  tomllib.loads(developer_override)
741
- self.assertEqual(str(result_path), command[command.index("-o") + 1])
824
+ self.assertEqual(str(result_path), focused[focused.index("-o") + 1])
742
825
 
743
826
  def test_reviewer_process_timeout_terminates_its_process_group(self) -> None:
744
827
  repo, _ = self.make_repo()
745
- process = mock.MagicMock()
746
- process.pid = 4321
747
- process.poll.return_value = None
748
- process.communicate.side_effect = [
749
- subprocess.TimeoutExpired(cmd=["fake-reviewer"], timeout=0.01),
750
- ("partial", "stopped"),
751
- ]
752
- with (
753
- mock.patch.object(ap.subprocess, "Popen", return_value=process) as popen,
754
- mock.patch.object(ap.os, "killpg") as killpg,
755
- ):
828
+ runner = repo / "slow-reviewer.py"
829
+ runner.write_text(
830
+ "import json, sys, time\n"
831
+ "print(json.dumps({'type': 'turn.started'}), flush=True)\n"
832
+ "print('Bearer secret-token password=hunter2', file=sys.stderr, flush=True)\n"
833
+ "time.sleep(30)\n",
834
+ encoding="utf-8",
835
+ )
836
+ started = time.monotonic()
837
+ with self.assertRaises(ap._ReviewerRuntimeTimeout) as raised:
838
+ ap._run_supervised_reviewer_process(
839
+ [sys.executable, str(runner)],
840
+ cwd=repo,
841
+ env={"PATH": os.environ.get("PATH", "")},
842
+ timeout_seconds=0.3,
843
+ startup_timeout_seconds=0.2,
844
+ )
845
+ self.assertLess(time.monotonic() - started, 3.0)
846
+ self.assertEqual(1, raised.exception.diagnostics["event_count"])
847
+ safe = ap._safe_reviewer_attempt(raised.exception.diagnostics, 1, "analysis-timeout")
848
+ self.assertNotIn("secret-token", json.dumps(safe))
849
+ self.assertNotIn("hunter2", json.dumps(safe))
850
+
851
+ @unittest.skipUnless(os.name == "posix", "POSIX process-group behavior")
852
+ def test_reviewer_timeout_kills_group_after_leader_exits(self) -> None:
853
+ repo, _ = self.make_repo()
854
+ child_pid_path = repo / "reviewer-grandchild.pid"
855
+ runner = repo / "leader-exits-reviewer.py"
856
+ runner.write_text(
857
+ "import json, subprocess, sys\n"
858
+ "from pathlib import Path\n"
859
+ "child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])\n"
860
+ f"Path({str(child_pid_path)!r}).write_text(str(child.pid))\n"
861
+ "print(json.dumps({'type': 'turn.started'}), flush=True)\n",
862
+ encoding="utf-8",
863
+ )
864
+ started = time.monotonic()
865
+ with self.assertRaises(ap._ReviewerRuntimeTimeout):
866
+ ap._run_supervised_reviewer_process(
867
+ [sys.executable, str(runner)],
868
+ cwd=repo,
869
+ env={"PATH": os.environ.get("PATH", "")},
870
+ timeout_seconds=0.3,
871
+ startup_timeout_seconds=0.2,
872
+ )
873
+ self.assertLess(time.monotonic() - started, 3.0)
874
+ child_pid = int(child_pid_path.read_text())
875
+ child_gone = False
876
+ for _ in range(20):
877
+ try:
878
+ os.kill(child_pid, 0)
879
+ except ProcessLookupError:
880
+ child_gone = True
881
+ break
882
+ time.sleep(0.05)
883
+ self.assertTrue(child_gone, "Reviewer grandchild survived process-group cleanup")
884
+
885
+ @unittest.skipUnless(os.name == "posix", "POSIX detached-process behavior")
886
+ def test_detached_pipe_holder_cannot_extend_reviewer_deadline(self) -> None:
887
+ repo, _ = self.make_repo()
888
+ child_pid_path = repo / "detached-reviewer-child.pid"
889
+ runner = repo / "detached-child-reviewer.py"
890
+ runner.write_text(
891
+ "import json, subprocess, sys\n"
892
+ "from pathlib import Path\n"
893
+ "child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'], start_new_session=True)\n"
894
+ f"Path({str(child_pid_path)!r}).write_text(str(child.pid))\n"
895
+ "print(json.dumps({'type': 'turn.started'}), flush=True)\n",
896
+ encoding="utf-8",
897
+ )
898
+ started = time.monotonic()
899
+ child_pid = 0
900
+ try:
756
901
  with self.assertRaises(ap._ReviewerRuntimeTimeout):
757
902
  ap._run_supervised_reviewer_process(
758
- ["fake-reviewer", "; touch must-not-run"],
903
+ [sys.executable, str(runner)],
759
904
  cwd=repo,
760
905
  env={"PATH": os.environ.get("PATH", "")},
761
- timeout_seconds=0.01,
906
+ timeout_seconds=0.3,
907
+ startup_timeout_seconds=0.2,
762
908
  )
909
+ child_pid = int(child_pid_path.read_text())
910
+ finally:
911
+ if not child_pid and child_pid_path.exists():
912
+ child_pid = int(child_pid_path.read_text())
913
+ if child_pid:
914
+ try:
915
+ os.kill(child_pid, signal.SIGKILL)
916
+ except ProcessLookupError:
917
+ pass
918
+ self.assertLess(time.monotonic() - started, 3.0)
919
+ self.assertFalse(
920
+ any(thread.name.startswith("reviewer-stream-") for thread in threading.enumerate())
921
+ )
922
+
923
+ def test_reviewer_process_without_first_event_is_runtime_unavailable(self) -> None:
924
+ repo, _ = self.make_repo()
925
+ runner = repo / "silent-reviewer.py"
926
+ runner.write_text("import time\ntime.sleep(30)\n", encoding="utf-8")
927
+ with self.assertRaises(ap._ReviewerRuntimeUnavailable) as raised:
928
+ ap._run_supervised_reviewer_process(
929
+ [sys.executable, str(runner)],
930
+ cwd=repo,
931
+ env={"PATH": os.environ.get("PATH", "")},
932
+ timeout_seconds=1,
933
+ startup_timeout_seconds=0.1,
934
+ )
935
+ self.assertEqual(0, raised.exception.diagnostics["event_count"])
936
+
937
+ def test_reviewer_process_clean_exit_without_semantic_event_is_unavailable(self) -> None:
938
+ repo, _ = self.make_repo()
939
+ runner = repo / "empty-reviewer.py"
940
+ runner.write_text("pass\n", encoding="utf-8")
941
+ with self.assertRaises(ap._ReviewerRuntimeUnavailable):
942
+ ap._run_supervised_reviewer_process(
943
+ [sys.executable, str(runner)],
944
+ cwd=repo,
945
+ env={"PATH": os.environ.get("PATH", "")},
946
+ timeout_seconds=1,
947
+ startup_timeout_seconds=0.2,
948
+ )
949
+
950
+ def test_reviewer_event_writer_failure_stops_process_and_reader_threads(self) -> None:
951
+ repo, _ = self.make_repo()
952
+ runner = repo / "writer-failure-reviewer.py"
953
+ runner.write_text(
954
+ "import json, time\n"
955
+ "print(json.dumps({'type': 'turn.started'}), flush=True)\n"
956
+ "time.sleep(30)\n",
957
+ encoding="utf-8",
958
+ )
763
959
 
764
- self.assertEqual(["fake-reviewer", "; touch must-not-run"], popen.call_args.args[0])
765
- self.assertTrue(popen.call_args.kwargs["start_new_session"])
766
- self.assertEqual(mock.call(4321, ap.signal.SIGTERM), killpg.call_args)
960
+ class FailingWriter:
961
+ def write(self, _: str) -> None:
962
+ raise OSError("event log unavailable")
963
+
964
+ def flush(self) -> None:
965
+ pass
966
+
967
+ started = time.monotonic()
968
+ with mock.patch.object(
969
+ ap,
970
+ "_terminate_reviewer_process",
971
+ wraps=ap._terminate_reviewer_process,
972
+ ) as terminate:
973
+ with self.assertRaises(ap._ReviewerRuntimeInternalError):
974
+ ap._run_supervised_reviewer_process(
975
+ [sys.executable, str(runner)],
976
+ cwd=repo,
977
+ env={"PATH": os.environ.get("PATH", "")},
978
+ timeout_seconds=3,
979
+ startup_timeout_seconds=1,
980
+ event_writer=FailingWriter(),
981
+ )
982
+ self.assertLess(time.monotonic() - started, 3.0)
983
+ terminate.assert_called_once()
984
+ self.assertFalse(
985
+ any(thread.name.startswith("reviewer-stream-") for thread in threading.enumerate())
986
+ )
987
+
988
+ def test_reviewer_output_flood_is_bounded_and_cleans_reader_threads(self) -> None:
989
+ repo, _ = self.make_repo()
990
+ runner = repo / "flood-reviewer.py"
991
+ runner.write_text(
992
+ "import json\n"
993
+ "for index in range(10000):\n"
994
+ " print(json.dumps({'type': 'item.completed', 'item': {'type': 'note'}, 'i': index}), flush=True)\n",
995
+ encoding="utf-8",
996
+ )
997
+ started = time.monotonic()
998
+ with mock.patch.object(ap, "_REVIEW_OUTPUT_MAX_BYTES", 2048):
999
+ with self.assertRaises(ap._ReviewerRuntimeOutputLimit):
1000
+ ap._run_supervised_reviewer_process(
1001
+ [sys.executable, str(runner)],
1002
+ cwd=repo,
1003
+ env={"PATH": os.environ.get("PATH", "")},
1004
+ timeout_seconds=3,
1005
+ startup_timeout_seconds=1,
1006
+ )
1007
+ self.assertLess(time.monotonic() - started, 3.0)
1008
+ self.assertFalse(
1009
+ any(thread.name.startswith("reviewer-stream-") for thread in threading.enumerate())
1010
+ )
1011
+
1012
+ def test_reviewer_single_unterminated_line_is_bounded(self) -> None:
1013
+ repo, _ = self.make_repo()
1014
+ runner = repo / "long-line-reviewer.py"
1015
+ runner.write_text(
1016
+ "import sys, time\n"
1017
+ "sys.stdout.write('x' * 10000)\n"
1018
+ "sys.stdout.flush()\n"
1019
+ "time.sleep(30)\n",
1020
+ encoding="utf-8",
1021
+ )
1022
+ with mock.patch.object(ap, "_REVIEW_OUTPUT_MAX_BYTES", 2048):
1023
+ with self.assertRaises(ap._ReviewerRuntimeOutputLimit):
1024
+ ap._run_supervised_reviewer_process(
1025
+ [sys.executable, str(runner)],
1026
+ cwd=repo,
1027
+ env={"PATH": os.environ.get("PATH", "")},
1028
+ timeout_seconds=3,
1029
+ startup_timeout_seconds=1,
1030
+ )
1031
+ self.assertFalse(
1032
+ any(thread.name.startswith("reviewer-stream-") for thread in threading.enumerate())
1033
+ )
1034
+
1035
+ def test_reviewer_event_metadata_does_not_persist_model_or_patch_content(self) -> None:
1036
+ record, semantic = ap._reviewer_event_metadata(
1037
+ "stdout",
1038
+ json.dumps(
1039
+ {
1040
+ "type": "item.completed",
1041
+ "item": {
1042
+ "type": "command_execution",
1043
+ "text": "PATCH_SECRET_TOKEN",
1044
+ "output": "Bearer private-token",
1045
+ },
1046
+ }
1047
+ ),
1048
+ "2026-07-17T00:00:00+00:00",
1049
+ )
1050
+ serialized = json.dumps(record)
1051
+ self.assertTrue(semantic)
1052
+ self.assertEqual("item.completed", record["event_type"])
1053
+ self.assertEqual("command_execution", record["item_type"])
1054
+ self.assertNotIn("PATCH_SECRET_TOKEN", serialized)
1055
+ self.assertNotIn("private-token", serialized)
1056
+
1057
+ def test_substantive_agent_message_is_not_erased_by_later_approved_message(self) -> None:
1058
+ assignment = self.reviewer_assignment(
1059
+ review_depth="focused",
1060
+ timeout_seconds=150,
1061
+ issued_at="2026-07-17T00:00:00+00:00",
1062
+ deadline_at="2026-07-17T00:02:30+00:00",
1063
+ )
1064
+ stdout = "\n".join(
1065
+ json.dumps(
1066
+ {
1067
+ "type": "item.completed",
1068
+ "item": {"type": "agent_message", "text": json.dumps({"verdict": verdict})},
1069
+ }
1070
+ )
1071
+ for verdict in ("blocked", "approved")
1072
+ )
1073
+ result = ap._substantive_reviewer_event_result(stdout, assignment)
1074
+ self.assertIsNotNone(result)
1075
+ self.assertEqual("blocked", result["verdict"])
1076
+
1077
+ def test_reviewer_spawn_failure_is_runtime_unavailable_with_safe_diagnostics(self) -> None:
1078
+ repo, _ = self.make_repo()
1079
+ with mock.patch.object(
1080
+ ap.subprocess,
1081
+ "Popen",
1082
+ side_effect=OSError("permission denied token=private-value"),
1083
+ ):
1084
+ with self.assertRaises(ap._ReviewerRuntimeUnavailable) as raised:
1085
+ ap._run_supervised_reviewer_process(
1086
+ ["missing-reviewer"],
1087
+ cwd=repo,
1088
+ env={"PATH": os.environ.get("PATH", "")},
1089
+ timeout_seconds=1,
1090
+ )
1091
+ safe = ap._safe_reviewer_attempt(raised.exception.diagnostics, 1, "runtime-unavailable")
1092
+ self.assertEqual("spawn-failed", safe["phase"])
1093
+ self.assertNotIn("private-value", json.dumps(safe))
1094
+
1095
+ def test_reviewer_attempt_receipt_never_persists_raw_stderr(self) -> None:
1096
+ diagnostics = ap._reviewer_attempt_diagnostics(
1097
+ started_at="2026-07-17T00:00:00+00:00",
1098
+ finished_at="2026-07-17T00:00:01+00:00",
1099
+ stdout="",
1100
+ stderr="PATCH_SECRET_TOKEN\n-----BEGIN OPENSSH PRIVATE KEY-----\nprivate-material",
1101
+ first_event_at="",
1102
+ last_event_at="",
1103
+ last_event_type="",
1104
+ event_count=0,
1105
+ phase="spawn-failed",
1106
+ exit_code=None,
1107
+ )
1108
+ serialized = json.dumps(
1109
+ ap._safe_reviewer_attempt(diagnostics, 1, "runtime-unavailable")
1110
+ )
1111
+ self.assertNotIn("stderr_tail", serialized)
1112
+ self.assertNotIn("PATCH_SECRET_TOKEN", serialized)
1113
+ self.assertNotIn("PRIVATE KEY", serialized)
1114
+ self.assertIn(hashlib.sha256(diagnostics["stderr"].encode()).hexdigest(), serialized)
767
1115
 
768
1116
  def test_reviewer_assignment_timing_contract_and_fixed_budgets(self) -> None:
769
1117
  issued_at = "2026-07-16T00:00:00+00:00"
770
1118
  assignment = self.reviewer_assignment(
771
1119
  review_depth="focused",
772
- timeout_seconds=90,
1120
+ timeout_seconds=150,
773
1121
  issued_at=issued_at,
774
- deadline_at="2026-07-16T00:01:30+00:00",
1122
+ deadline_at="2026-07-16T00:02:30+00:00",
775
1123
  scope_revision=1,
776
1124
  )
777
1125
  self.assertEqual(
778
1126
  assignment,
779
1127
  ap._validate_orchestration_contract("assignment", assignment),
780
1128
  )
781
- self.assertEqual(("focused", 90), ap._normalized_task_review_policy(True))
1129
+ self.assertEqual(("focused", 150), ap._normalized_task_review_policy(True))
782
1130
  self.assertEqual(
783
- ("deep", 300),
1131
+ ("deep", 360),
784
1132
  ap._normalized_task_review_policy(True, "focused", "deep"),
785
1133
  )
786
1134
  self.assertEqual(("none", 0), ap._normalized_task_review_policy(False, "deep"))
@@ -790,17 +1138,17 @@ class AutoCodingProfileTests(unittest.TestCase):
790
1138
  review_depth="focused",
791
1139
  timeout_seconds=0,
792
1140
  issued_at=issued_at,
793
- deadline_at="2026-07-16T00:01:30+00:00",
1141
+ deadline_at="2026-07-16T00:02:30+00:00",
794
1142
  ),
795
1143
  self.reviewer_assignment(
796
1144
  review_depth="focused",
797
- timeout_seconds=90,
1145
+ timeout_seconds=150,
798
1146
  issued_at="2026-07-16T00:00:00",
799
- deadline_at="2026-07-16T00:01:30",
1147
+ deadline_at="2026-07-16T00:02:30",
800
1148
  ),
801
1149
  self.reviewer_assignment(
802
1150
  review_depth="focused",
803
- timeout_seconds=90,
1151
+ timeout_seconds=150,
804
1152
  issued_at=issued_at,
805
1153
  deadline_at="2026-07-16T00:01:00+00:00",
806
1154
  ),
@@ -1042,8 +1390,8 @@ class AutoCodingProfileTests(unittest.TestCase):
1042
1390
  deep = self.plan(repo, cfg, planned_paths=["backend/auth/service.py"])
1043
1391
 
1044
1392
  for plan, depth, timeout in [
1045
- (focused, "focused", 90),
1046
- (deep, "deep", 300),
1393
+ (focused, "focused", 150),
1394
+ (deep, "deep", 360),
1047
1395
  ]:
1048
1396
  with self.subTest(depth=depth):
1049
1397
  self.assertTrue(plan["review_required"])