@elvis1513/auto-coding-skill 4.2.1 → 4.2.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.
@@ -2,6 +2,7 @@
2
2
  from __future__ import annotations
3
3
 
4
4
  import argparse
5
+ import base64
5
6
  import contextlib
6
7
  import io
7
8
  import json
@@ -9,6 +10,7 @@ import os
9
10
  import subprocess
10
11
  import sys
11
12
  import tempfile
13
+ import tomllib
12
14
  import unittest
13
15
  from pathlib import Path
14
16
  from unittest import mock
@@ -695,6 +697,118 @@ class AutoCodingProfileTests(unittest.TestCase):
695
697
  self.assertEqual(next_owner, result["next_owner"])
696
698
  ap._validate_orchestration_contract("result", result)
697
699
 
700
+ def test_reviewer_runtime_normalizes_presentation_fields_but_binds_identity(self) -> None:
701
+ assignment = self.reviewer_assignment()
702
+ result = ap._normalize_reviewer_runtime_result(
703
+ assignment,
704
+ {
705
+ "verdict": "approved",
706
+ "summary": "focused review complete",
707
+ "evidence": ["diff inspected"],
708
+ },
709
+ )
710
+ self.assertEqual("reviewer-1", result["node_id"])
711
+ self.assertEqual("a" * 64, result["diff_fingerprint"])
712
+ self.assertEqual(["fixer-1"], result["depends_on"])
713
+ self.assertEqual("focused review complete", result["summary"])
714
+
715
+ with self.assertRaisesRegex(APError, "does not match its assignment"):
716
+ ap._normalize_reviewer_runtime_result(
717
+ assignment,
718
+ {
719
+ "verdict": "approved",
720
+ "diff_fingerprint": "b" * 64,
721
+ },
722
+ )
723
+
724
+ def test_codex_reviewer_command_is_ephemeral_read_only_and_uses_managed_instructions(self) -> None:
725
+ repo, _ = self.make_repo()
726
+ assignment_path = repo / "assignment.json"
727
+ result_path = repo / "result.json"
728
+ with mock.patch.object(ap.shutil, "which", return_value="/usr/local/bin/codex"):
729
+ command = ap._codex_reviewer_command(repo, assignment_path, result_path)
730
+
731
+ self.assertEqual("/usr/local/bin/codex", command[0])
732
+ self.assertIn("--ephemeral", command)
733
+ self.assertIn("--ignore-user-config", command)
734
+ self.assertIn("read-only", command)
735
+ self.assertIn('model_reasoning_effort="xhigh"', command)
736
+ developer_override = next(
737
+ item for item in command if item.startswith("developer_instructions=")
738
+ )
739
+ tomllib.loads(developer_override)
740
+ self.assertEqual(str(result_path), command[command.index("-o") + 1])
741
+
742
+ def test_reviewer_process_timeout_terminates_its_process_group(self) -> None:
743
+ repo, _ = self.make_repo()
744
+ process = mock.MagicMock()
745
+ process.pid = 4321
746
+ process.poll.return_value = None
747
+ process.communicate.side_effect = [
748
+ subprocess.TimeoutExpired(cmd=["fake-reviewer"], timeout=0.01),
749
+ ("partial", "stopped"),
750
+ ]
751
+ with (
752
+ mock.patch.object(ap.subprocess, "Popen", return_value=process) as popen,
753
+ mock.patch.object(ap.os, "killpg") as killpg,
754
+ ):
755
+ with self.assertRaises(ap._ReviewerRuntimeTimeout):
756
+ ap._run_supervised_reviewer_process(
757
+ ["fake-reviewer", "; touch must-not-run"],
758
+ cwd=repo,
759
+ env={"PATH": os.environ.get("PATH", "")},
760
+ timeout_seconds=0.01,
761
+ )
762
+
763
+ self.assertEqual(["fake-reviewer", "; touch must-not-run"], popen.call_args.args[0])
764
+ self.assertTrue(popen.call_args.kwargs["start_new_session"])
765
+ self.assertEqual(mock.call(4321, ap.signal.SIGTERM), killpg.call_args)
766
+
767
+ def test_reviewer_assignment_timing_contract_and_fixed_budgets(self) -> None:
768
+ issued_at = "2026-07-16T00:00:00+00:00"
769
+ assignment = self.reviewer_assignment(
770
+ review_depth="focused",
771
+ timeout_seconds=90,
772
+ issued_at=issued_at,
773
+ deadline_at="2026-07-16T00:01:30+00:00",
774
+ scope_revision=1,
775
+ )
776
+ self.assertEqual(
777
+ assignment,
778
+ ap._validate_orchestration_contract("assignment", assignment),
779
+ )
780
+ self.assertEqual(("focused", 90), ap._normalized_task_review_policy(True))
781
+ self.assertEqual(
782
+ ("deep", 300),
783
+ ap._normalized_task_review_policy(True, "focused", "deep"),
784
+ )
785
+ self.assertEqual(("none", 0), ap._normalized_task_review_policy(False, "deep"))
786
+
787
+ invalid_contracts = [
788
+ self.reviewer_assignment(
789
+ review_depth="focused",
790
+ timeout_seconds=0,
791
+ issued_at=issued_at,
792
+ deadline_at="2026-07-16T00:01:30+00:00",
793
+ ),
794
+ self.reviewer_assignment(
795
+ review_depth="focused",
796
+ timeout_seconds=90,
797
+ issued_at="2026-07-16T00:00:00",
798
+ deadline_at="2026-07-16T00:01:30",
799
+ ),
800
+ self.reviewer_assignment(
801
+ review_depth="focused",
802
+ timeout_seconds=90,
803
+ issued_at=issued_at,
804
+ deadline_at="2026-07-16T00:01:00+00:00",
805
+ ),
806
+ ]
807
+ for payload in invalid_contracts:
808
+ with self.subTest(payload=payload):
809
+ with self.assertRaises(APError):
810
+ ap._validate_orchestration_contract("assignment", payload)
811
+
698
812
  def test_reviewer_result_template_rejects_wrong_role_self_review_and_empty_fingerprint(self) -> None:
699
813
  invalid_assignments = [
700
814
  self.reviewer_assignment(role="explorer"),
@@ -832,6 +946,32 @@ class AutoCodingProfileTests(unittest.TestCase):
832
946
  ap._final_gate_budget(cfg)
833
947
  self.assertIn("cannot exceed validation.max_total_seconds", str(context.exception))
834
948
 
949
+ def test_final_changed_scope_gate_runs_blocking_structure_check(self) -> None:
950
+ cfg = base_config()
951
+ cfg["structure"] = {
952
+ "enabled": True,
953
+ "enforcement": "blocking",
954
+ "max_file_lines_warn": 0,
955
+ "max_file_lines_block": 1,
956
+ "max_function_lines_warn": 0,
957
+ "layer_rules": {"enabled": False},
958
+ }
959
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
960
+ repo, _ = self.make_repo("src/too-large.py", cfg)
961
+ (repo / "src" / "too-large.py").write_text("one\ntwo\n", encoding="utf-8")
962
+
963
+ with self.assertRaisesRegex(APError, "structure-check failed"):
964
+ ap.cmd_light_gate(
965
+ argparse.Namespace(
966
+ repo=str(repo),
967
+ scope="changed",
968
+ profile="",
969
+ mode="dev",
970
+ base="",
971
+ explain=False,
972
+ )
973
+ )
974
+
835
975
  def test_route_timeout_uses_smallest_matching_budget(self) -> None:
836
976
  cfg = base_config()
837
977
  cfg["commands"] = {"one": "true", "two": "true"}
@@ -1451,6 +1591,231 @@ class AutoCodingProfileTests(unittest.TestCase):
1451
1591
  ap.cmd_doctor(argparse.Namespace(repo=str(repo)))
1452
1592
  ap.cmd_verify_api_docs(argparse.Namespace(repo=str(repo)))
1453
1593
 
1594
+ def test_root_and_nested_package_json_are_high_risk_and_require_review(self) -> None:
1595
+ repo, cfg = self.make_repo()
1596
+ for path in ["package.json", "frontend/admin/package.json"]:
1597
+ with self.subTest(path=path):
1598
+ plan = self.plan(
1599
+ repo,
1600
+ cfg,
1601
+ planned_paths=[path],
1602
+ requested_task_kind="change",
1603
+ )
1604
+ self.assertEqual("high-risk", plan["profile"])
1605
+ self.assertTrue(plan["review_required"])
1606
+ self.assertIn("release_or_tooling", plan["categories"])
1607
+
1608
+ def test_explicit_full_structure_scope_inspects_clean_tracked_files(self) -> None:
1609
+ cfg = base_config()
1610
+ cfg["structure"] = {
1611
+ "enabled": True,
1612
+ "enforcement": "advisory",
1613
+ "max_file_lines_warn": 0,
1614
+ "max_file_lines_block": 0,
1615
+ "max_function_lines_warn": 0,
1616
+ "layer_rules": {"enabled": False},
1617
+ }
1618
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
1619
+ repo, _ = self.make_repo("src/app.py", cfg)
1620
+ run(repo, "git", "add", "-A")
1621
+ run(repo, "git", "commit", "-qm", "add tracked structure fixture")
1622
+
1623
+ output = io.StringIO()
1624
+ with mock.patch.object(ap, "_record_evidence"), contextlib.redirect_stdout(output):
1625
+ ap.cmd_structure_check(
1626
+ argparse.Namespace(repo=str(repo), scope="full", base="", json=True)
1627
+ )
1628
+
1629
+ result = json.loads(output.getvalue())
1630
+ self.assertEqual("full", result["scope"])
1631
+ self.assertGreaterEqual(result["inspected_files"], 1)
1632
+
1633
+ def test_verify_jenkins_accepts_access_schema_and_uses_selected_identity(self) -> None:
1634
+ repo, cfg = self.make_repo()
1635
+ (repo / "Jenkinsfile").write_text("pipeline {}\n", encoding="utf-8")
1636
+
1637
+ with mock.patch.object(ap, "_record_evidence"):
1638
+ ap.cmd_verify_jenkins(argparse.Namespace(repo=str(repo), component="all"))
1639
+
1640
+ header = ap._jenkins_basic_auth_headers(cfg, component="backend")["Authorization"]
1641
+ decoded = base64.b64decode(header.removeprefix("Basic ")).decode("utf-8")
1642
+ self.assertEqual("back:back-pass", decoded)
1643
+
1644
+ def test_jenkins_component_is_ambiguous_with_two_access_endpoints(self) -> None:
1645
+ cfg = base_config()
1646
+ with self.assertRaisesRegex(APError, "Multiple access.jenkins endpoints"):
1647
+ ap._resolve_jenkins_component(cfg)
1648
+
1649
+ cfg["access"]["jenkins"]["backend"] = dict(cfg["access"]["jenkins"]["frontend"])
1650
+ self.assertEqual("frontend", ap._resolve_jenkins_component(cfg))
1651
+
1652
+ def test_verify_jenkins_build_uses_one_selected_access_component(self) -> None:
1653
+ repo, _ = self.make_repo()
1654
+ args = argparse.Namespace(
1655
+ repo=str(repo),
1656
+ component="backend",
1657
+ git_ref="HEAD",
1658
+ job_name=None,
1659
+ job_url=None,
1660
+ multibranch_root_job=None,
1661
+ branch_name=None,
1662
+ build_number=7,
1663
+ max_builds=20,
1664
+ timeout_sec=1,
1665
+ poll_sec=1,
1666
+ allow_no_deploy=True,
1667
+ )
1668
+ payload = {
1669
+ "number": 7,
1670
+ "result": "SUCCESS",
1671
+ "building": False,
1672
+ "description": "deployed",
1673
+ "url": "https://jenkins-back.test/7/",
1674
+ }
1675
+ with (
1676
+ mock.patch.object(ap, "_jenkins_api_get_json", return_value=payload) as api_get,
1677
+ mock.patch.object(ap, "_record_evidence"),
1678
+ ):
1679
+ ap.cmd_verify_jenkins_build(args)
1680
+
1681
+ self.assertEqual("backend", api_get.call_args.kwargs["component"])
1682
+ self.assertIn("https://jenkins-back.test/7/api/json", api_get.call_args.args[0])
1683
+
1684
+ def test_verify_jenkins_preserves_legacy_configuration(self) -> None:
1685
+ cfg = base_config()
1686
+ cfg["access"]["jenkins"] = {}
1687
+ cfg["jenkins"] = {
1688
+ "base_url": "https://legacy-jenkins.test",
1689
+ "job_url": "https://legacy-jenkins.test/job/backend",
1690
+ "trigger_branch": "dev",
1691
+ "image_repository": "registry.test/project/backend",
1692
+ "image_tag_strategy": "commit",
1693
+ "deploy_env": "production",
1694
+ "api_user": "legacy-user",
1695
+ "api_password": "legacy-pass",
1696
+ }
1697
+ cfg["target_env"] = {
1698
+ "health_base_url": "https://legacy-target.test",
1699
+ "health_path": "/healthz",
1700
+ }
1701
+ repo, _ = self.make_repo(config=cfg)
1702
+ (repo / "Jenkinsfile").write_text("pipeline {}\n", encoding="utf-8")
1703
+
1704
+ with mock.patch.object(ap, "_record_evidence"):
1705
+ ap.cmd_verify_jenkins(argparse.Namespace(repo=str(repo), component="all"))
1706
+
1707
+ self.assertEqual("", ap._resolve_jenkins_component(cfg))
1708
+ header = ap._jenkins_basic_auth_headers(cfg)["Authorization"]
1709
+ decoded = base64.b64decode(header.removeprefix("Basic ")).decode("utf-8")
1710
+ self.assertEqual("legacy-user:legacy-pass", decoded)
1711
+
1712
+ def test_jenkins_crumb_cache_is_scoped_by_identity(self) -> None:
1713
+ cfg = base_config()
1714
+ cfg["access"]["jenkins"]["frontend"].update(
1715
+ {"url": "https://jenkins.test/job/frontend", "username": "front-user"}
1716
+ )
1717
+ cfg["access"]["jenkins"]["backend"].update(
1718
+ {"url": "https://jenkins.test/job/backend", "username": "back-user"}
1719
+ )
1720
+
1721
+ def response(crumb: str) -> mock.MagicMock:
1722
+ value = mock.MagicMock()
1723
+ value.__enter__.return_value.read.return_value = json.dumps(
1724
+ {"crumbRequestField": "Jenkins-Crumb", "crumb": crumb}
1725
+ ).encode("utf-8")
1726
+ return value
1727
+
1728
+ ap._JENKINS_CRUMB_CACHE.clear()
1729
+ self.addCleanup(ap._JENKINS_CRUMB_CACHE.clear)
1730
+ with mock.patch.object(
1731
+ ap.urllib.request,
1732
+ "urlopen",
1733
+ side_effect=[response("crumb-front"), response("crumb-back")],
1734
+ ) as urlopen:
1735
+ frontend = ap._jenkins_crumb_headers(cfg, component="frontend")
1736
+ backend = ap._jenkins_crumb_headers(cfg, component="backend")
1737
+
1738
+ self.assertEqual({"Jenkins-Crumb": "crumb-front"}, frontend)
1739
+ self.assertEqual({"Jenkins-Crumb": "crumb-back"}, backend)
1740
+ self.assertEqual(2, urlopen.call_count)
1741
+ self.assertEqual(2, len(ap._JENKINS_CRUMB_CACHE))
1742
+
1743
+ def test_verify_target_uses_access_project_urls_without_legacy_wait(self) -> None:
1744
+ repo, _ = self.make_repo()
1745
+ args = argparse.Namespace(
1746
+ repo=str(repo),
1747
+ backend_path=["/healthz"],
1748
+ frontend_path=["/"],
1749
+ backend_basic_auth=False,
1750
+ frontend_basic_auth=False,
1751
+ )
1752
+ with (
1753
+ mock.patch.object(ap, "_http_get", return_value=(200, "ok")) as http_get,
1754
+ mock.patch.object(ap, "_wait_for_health_url") as wait_health,
1755
+ mock.patch.object(ap, "_record_evidence"),
1756
+ ):
1757
+ ap.cmd_verify_target(args)
1758
+
1759
+ self.assertEqual(
1760
+ ["https://project-back.test/healthz", "https://project-front.test/"],
1761
+ [call.args[0] for call in http_get.call_args_list],
1762
+ )
1763
+ wait_health.assert_not_called()
1764
+
1765
+ def test_verify_target_missing_access_url_fails_before_network(self) -> None:
1766
+ cfg = base_config()
1767
+ cfg["access"]["project"]["backend"]["url"] = ""
1768
+ repo, _ = self.make_repo(config=cfg)
1769
+ args = argparse.Namespace(
1770
+ repo=str(repo),
1771
+ backend_path=["/healthz"],
1772
+ frontend_path=None,
1773
+ backend_basic_auth=False,
1774
+ frontend_basic_auth=False,
1775
+ )
1776
+ with (
1777
+ mock.patch.object(ap, "_http_get") as http_get,
1778
+ mock.patch.object(ap, "http_get_status") as get_status,
1779
+ mock.patch.object(ap.time, "sleep") as sleep,
1780
+ ):
1781
+ with self.assertRaisesRegex(APError, "access.project.backend.url"):
1782
+ ap.cmd_verify_target(args)
1783
+
1784
+ http_get.assert_not_called()
1785
+ get_status.assert_not_called()
1786
+ sleep.assert_not_called()
1787
+
1788
+ def test_verify_target_preserves_legacy_endpoint_and_health_preflight(self) -> None:
1789
+ cfg = base_config()
1790
+ cfg["access"]["project"] = {}
1791
+ cfg["jenkins"] = {"deploy_timeout_sec": 7}
1792
+ cfg["target_env"] = {
1793
+ "backend_base_url": "https://legacy-target.test/api",
1794
+ "backend_username": "legacy-user",
1795
+ "backend_password": "legacy-pass",
1796
+ "health_base_url": "https://legacy-target.test",
1797
+ "health_path": "/healthz",
1798
+ }
1799
+ repo, _ = self.make_repo(config=cfg)
1800
+ args = argparse.Namespace(
1801
+ repo=str(repo),
1802
+ backend_path=["/ready"],
1803
+ frontend_path=None,
1804
+ backend_basic_auth=False,
1805
+ frontend_basic_auth=False,
1806
+ )
1807
+ with (
1808
+ mock.patch.object(ap, "_wait_for_health_url") as wait_health,
1809
+ mock.patch.object(ap, "_http_get", return_value=(200, "ok")) as http_get,
1810
+ mock.patch.object(ap, "_record_evidence"),
1811
+ ):
1812
+ ap.cmd_verify_target(args)
1813
+
1814
+ wait_health.assert_called_once_with(
1815
+ "target", "https://legacy-target.test/healthz", 7
1816
+ )
1817
+ self.assertEqual("https://legacy-target.test/api/ready", http_get.call_args.args[0])
1818
+
1454
1819
  def test_dev_closure_rejects_owner_acceptance_pass(self) -> None:
1455
1820
  with self.assertRaises(APError):
1456
1821
  ap._validate_closure_result("dev", "PASS")