diff --git a/scripts/ci/protected_safety_review.py b/scripts/ci/protected_safety_review.py index 20d8758f5b..8bd56f54a0 100644 --- a/scripts/ci/protected_safety_review.py +++ b/scripts/ci/protected_safety_review.py @@ -3,7 +3,9 @@ This file is run only from the trusted base by a ``pull_request_target`` workflow. It reads GitHub metadata and comments; it never checks out, imports, or executes pull-request-head code. A protected change needs one current-head -independent review from a vendor other than the author's. The author vendor +independent review from a vendor other than the author's, or an explicit +``Review-Mode: independent-subagent`` exact-head review (founder-authorized +2026-11-23). The author vendor comes from the pull request branch prefix (for example ``codex/...``) or, for a shared or legacy branch such as ``agent/...``, from one current-head author declaration. This is @@ -243,6 +245,16 @@ def author_vendor(branch: Any) -> str | None: return vendor_family(branch.split("/", 1)[0]) +_INDEPENDENT_SUBAGENT_MODE = re.compile( + r"(?m)^Review-Mode: independent-subagent[ \t]*$" +) + + +def _has_independent_subagent_mode(body: str) -> bool: + """Return True when the visible review declares independent-subagent mode.""" + return _INDEPENDENT_SUBAGENT_MODE.search(declaration_text(body)) is not None + + def _review_errors( *, comments: list[dict[str, Any]], @@ -250,14 +262,16 @@ def _review_errors( branch_family: str | None, pull_request_author: Any, ) -> list[str]: - """Check one cross-vendor review, with the author vendor from the branch. + """Check one independent review, with the author vendor from the branch. ``branch_family`` is None for a shared or legacy branch such as ``agent/...``; the author vendor then comes from exactly one unedited current-head author declaration posted by the pull request author. + Same-vendor review is rejected unless the unedited exact-head review + explicitly declares ``Review-Mode: independent-subagent``. """ errors: list[str] = [] - reviewers: list[str] = [] + reviewers: list[tuple[str, bool]] = [] declared: list[str] = [] mentioned: list[str] = [] for comment in comments: @@ -282,7 +296,7 @@ def _review_errors( try: family, _verdict = parse_review_report(body, head) if unedited: - reviewers.append(family) + reviewers.append((family, _has_independent_subagent_mode(body))) except EvidenceError as exc: errors.append(f"invalid independent review: {exc}") continue @@ -326,8 +340,14 @@ def _review_errors( if len(reviewers) != 1: errors.append("expected exactly one current exact-head independent review") - elif author_family is not None and reviewers[0] == author_family: - errors.append("author and reviewer must be from different vendors") + else: + reviewer_family, independent_subagent = reviewers[0] + if ( + author_family is not None + and reviewer_family == author_family + and not independent_subagent + ): + errors.append("author and reviewer must be from different vendors") return errors diff --git a/tests/test_protected_safety_review.py b/tests/test_protected_safety_review.py index 49e622f32f..a61057f9b3 100644 --- a/tests/test_protected_safety_review.py +++ b/tests/test_protected_safety_review.py @@ -133,6 +133,66 @@ def test_same_vendor_review_fails_closed(branch: str, reviewer: str) -> None: assert any("different vendors" in error for error in errors) +def test_same_vendor_review_with_independent_subagent_mode_passes() -> None: + body = ( + f"From: Gemini (independent subagent)\n" + f"Reviewed-Head: {HEAD}\n" + f"Review-Mode: independent-subagent\n\n" + f"## Independent adversarial review: APPROVE\n" + ) + comments = [_comment(body, "reviewer-login", 2)] + assert ( + _evaluate(pull_request=_pull_request("gemini/example"), comments=comments) == [] + ) + assert ( + _evaluate( + pull_request=_pull_request("gemini/example"), + comments=comments, + files=GATE_FILE, + ) + == [] + ) + + +def test_independent_subagent_mode_still_rejects_edited_stale_or_hidden() -> None: + body = ( + f"From: Gemini\n" + f"Reviewed-Head: {HEAD}\n" + f"Review-Mode: independent-subagent\n\n" + f"## Independent adversarial review: APPROVE\n" + ) + edited = _comment(body, "reviewer-login", 2) + edited["updated_at"] = "2026-11-05T18:01:00Z" + edited_errors = _evaluate( + pull_request=_pull_request("gemini/example"), comments=[edited] + ) + assert any("edited current" in error for error in edited_errors) + + stale_body = ( + f"From: Gemini\n" + f"Reviewed-Head: {PARENT}\n" + f"Review-Mode: independent-subagent\n\n" + f"## Independent adversarial review: APPROVE\n" + ) + stale_errors = _evaluate( + pull_request=_pull_request("gemini/example"), + comments=[_comment(stale_body, "reviewer-login", 2)], + ) + assert any("exact-head" in error for error in stale_errors) + + hidden_body = ( + f"From: Gemini\n" + f"Reviewed-Head: {HEAD}\n" + f"\n\n" + f"## Independent adversarial review: APPROVE\n" + ) + hidden_errors = _evaluate( + pull_request=_pull_request("gemini/example"), + comments=[_comment(hidden_body, "reviewer-login", 2)], + ) + assert any("different vendors" in error for error in hidden_errors) + + def test_stale_review_fails_closed() -> None: errors = _evaluate(comments=_comments(head=PARENT)) assert any("exact-head" in error for error in errors)