diff --git a/docs/execution/architecture.md b/docs/execution/architecture.md index ae81499b6d..7f2cbcae94 100644 --- a/docs/execution/architecture.md +++ b/docs/execution/architecture.md @@ -521,6 +521,13 @@ secondary-rate responses persist shared back-pressure. A capacity-one fenced lease enforces at least one second between review mutation starts across sessions. +When the authenticated reviewer is the pull request's author, requested +`APPROVE` and `REQUEST_CHANGES` events are published as `COMMENT`. The publisher +chooses from preflight identities before any mutation, adds a fixed disclosure, +and records both events in the receipt. This comment provides neither a GitHub +approval nor a changes-requested review and does not itself satisfy a required +approval. The skills' verdict and gate are independent of the effective event. + Final success requires a server-authored receipt with the operation identity, review/comment IDs, response and reconciliation classes, and an exhaustive posted/already-present/omitted disposition for every original finding. Dry-run diff --git a/src/autoskillit/core/types/constants/_type_constants.py b/src/autoskillit/core/types/constants/_type_constants.py index 0da0630a71..aca15b697b 100644 --- a/src/autoskillit/core/types/constants/_type_constants.py +++ b/src/autoskillit/core/types/constants/_type_constants.py @@ -446,6 +446,7 @@ class SkillFamilyDef(NamedTuple): "drift-discipline", "graphql-batch-aliases", "mutating-call-delay", + "response-body-guard", } ), ), @@ -461,6 +462,7 @@ class SkillFamilyDef(NamedTuple): required_patterns=frozenset( { "unpostable-prefilter", + "response-body-guard", } ), ), diff --git a/src/autoskillit/execution/github_ops/_github_http.py b/src/autoskillit/execution/github_ops/_github_http.py index 5451b77012..d8d53c06b6 100644 --- a/src/autoskillit/execution/github_ops/_github_http.py +++ b/src/autoskillit/execution/github_ops/_github_http.py @@ -2,6 +2,7 @@ from __future__ import annotations +import json from collections.abc import Mapping from email.utils import parsedate_to_datetime from typing import Any @@ -16,6 +17,18 @@ def github_error_message(data: Any) -> str | None: return message if isinstance(message, str) and message else None +def github_error_detail(data: Any) -> str | None: + """Return GitHub's message plus bounded string or object ``errors[]`` details.""" + message = github_error_message(data) + errors = data.get("errors") if isinstance(data, Mapping) else None + if not isinstance(errors, list) or not errors: + return message[:500] if message else None + items = "; ".join( + item if isinstance(item, str) else json.dumps(item, sort_keys=True) for item in errors + ) + return (f"{message}: {items}" if message else items)[:500] + + def is_secondary_rate_limit( *, status_code: int, diff --git a/src/autoskillit/execution/github_review/_poster_retry.py b/src/autoskillit/execution/github_review/_poster_retry.py index 7c76e0b56d..5e0765c8fc 100644 --- a/src/autoskillit/execution/github_review/_poster_retry.py +++ b/src/autoskillit/execution/github_review/_poster_retry.py @@ -112,8 +112,19 @@ async def resume_pending_retry( retry_findings = tuple( finding for finding in findings if finding.canonical_index in retry_indexes ) + error = None if tuple(finding.canonical_index for finding in retry_findings) != (attempt.canonical_indexes): error = "persisted review retry references unknown canonical findings" + elif ( + _poster_support.author_effective_event( + request.event, + authenticated_login=authenticated_login, + pr_author_login=pr_author_login, + ) + != attempt.effective_event + ): + error = "persisted review retry event no longer matches current reviewer identity" + if error is not None: poster.ledger.complete_attempt( operation_key=operation_key, attempt_number=attempt.attempt_number, @@ -142,7 +153,6 @@ async def resume_pending_retry( attempt_number=attempt.attempt_number, omitted=attempt.omitted_dispositions, authenticated_login=authenticated_login, - pr_author_login=pr_author_login, resume_pending=True, ) diff --git a/src/autoskillit/execution/github_review/_poster_support.py b/src/autoskillit/execution/github_review/_poster_support.py index 7c5d829944..6c1a858d09 100644 --- a/src/autoskillit/execution/github_review/_poster_support.py +++ b/src/autoskillit/execution/github_review/_poster_support.py @@ -41,6 +41,41 @@ class RemoteFindingScan: error: str | None = None +_AUTHOR_EVENT_DISCLOSURES = { + "APPROVE": ( + "Requested review event APPROVE was published as COMMENT because GitHub does not allow " + "a pull request's author to approve it; this is not a GitHub approval." + ), + "REQUEST_CHANGES": ( + "Requested review event REQUEST_CHANGES was published as COMMENT because GitHub does " + "not allow a pull request's author to request changes on it; this review does not " + "block merging." + ), +} + + +def author_effective_event( + requested_event: str, *, authenticated_login: str, pr_author_login: str +) -> str: + """Choose the event for GitHub's known own-author restriction.""" + if ( + requested_event in _AUTHOR_EVENT_DISCLOSURES + and authenticated_login.casefold() == pr_author_login.casefold() + ): + return "COMMENT" + return requested_event + + +def _disclose_author_event(body: str, *, requested_event: str, event: str) -> str: + if event == requested_event: + return body + if event != "COMMENT" or requested_event not in _AUTHOR_EVENT_DISCLOSURES: + raise ValueError( + f"unsupported review event transformation: {requested_event!r} -> {event!r}" + ) + return append_marker(body, _AUTHOR_EVENT_DISCLOSURES[requested_event]) + + def payload( *, request: GitHubReviewRequest, @@ -81,6 +116,7 @@ def payload( ) if entries: body += "\n\n## Outside Diff Range\n\n" + "\n\n".join(entries) + body = _disclose_author_event(body, requested_event=request.event, event=event) if len(body) > 60000: raise ValueError("review body including omitted findings exceeds 60000 characters") return { @@ -161,20 +197,6 @@ def structured_invalid_comment_index( return index if 0 <= index < comment_count else None -def is_structured_self_review(response: GatewayResult) -> bool: - if response.status_code != 422: - return False - data = response.data if isinstance(response.data, Mapping) else {} - errors = data.get("errors") - return isinstance(errors, list) and any( - isinstance(item, Mapping) - and item.get("field") == "event" - and item.get("code") == "invalid" - and "own pull request" in str(item.get("message", "")).casefold() - for item in errors - ) - - def structured_error_message(response: GatewayResult) -> str | None: if not isinstance(response.data, Mapping): return None diff --git a/src/autoskillit/execution/github_review/poster.py b/src/autoskillit/execution/github_review/poster.py index 9d0c006f2d..8d033d4bba 100644 --- a/src/autoskillit/execution/github_review/poster.py +++ b/src/autoskillit/execution/github_review/poster.py @@ -23,7 +23,7 @@ ReviewResponseClass, ) from autoskillit.execution.github_ops._github_http import ( - github_error_message, + github_error_detail, retry_after_seconds, ) @@ -56,8 +56,6 @@ class ReviewAttemptOutcome(StrEnum): SERVER_ERROR = auto() SECONDARY_RATE_LIMIT = auto() ANCHOR_422 = auto() - SELF_REVIEW_422 = auto() - COMBINED_422 = auto() GENERIC_422 = auto() SECOND_422 = auto() CLIENT_ERROR = auto() @@ -68,9 +66,6 @@ def classify_attempt_outcome( *, attempt_number: int, comment_count: int, - effective_event: str, - authenticated_login: str, - pr_author_login: str, ) -> tuple[ReviewAttemptOutcome, int | None]: if response.succeeded: return ReviewAttemptOutcome.SUCCESS, None @@ -84,22 +79,8 @@ def classify_attempt_outcome( if attempt_number != 1: return ReviewAttemptOutcome.SECOND_422, None invalid_index = _poster_support.structured_invalid_comment_index(response, comment_count) - self_review = ( - effective_event == "REQUEST_CHANGES" - and authenticated_login == pr_author_login - and _poster_support.is_structured_self_review(response) - ) if invalid_index is not None: - return ( - ReviewAttemptOutcome.COMBINED_422 - if self_review - else ReviewAttemptOutcome.ANCHOR_422 - ), invalid_index - if self_review: - # A second error whose anchor is not identifiable must not cause a full-set retry. - errors = response.data.get("errors", []) if isinstance(response.data, Mapping) else [] - if len(errors) == 1: - return ReviewAttemptOutcome.SELF_REVIEW_422, None + return ReviewAttemptOutcome.ANCHOR_422, invalid_index return ReviewAttemptOutcome.GENERIC_422, None return ReviewAttemptOutcome.CLIENT_ERROR, None @@ -202,6 +183,18 @@ async def _post(self, request: GitHubReviewRequest) -> GitHubReviewPostResult: preflight, operation_key=operation_key, planned_comment_count=len(findings) ) scope_material, authenticated_login, pr_author_login = preflight + effective_event = _poster_support.author_effective_event( + request.event, + authenticated_login=authenticated_login, + pr_author_login=pr_author_login, + ) + prepared_material = _poster_retry.attempt_material( + request=request, + operation_key=operation_key, + findings=findings, + omitted=omitted, + effective_event=effective_event, + ) persisted_findings = tuple( ( finding.canonical_index, @@ -249,11 +242,11 @@ async def _post(self, request: GitHubReviewRequest) -> GitHubReviewPostResult: operation_key=operation_key, scope_id=scope_id, findings=findings, - effective_event=request.event, + effective_event=effective_event, attempt_number=1, omitted=omitted, authenticated_login=authenticated_login, - pr_author_login=pr_author_login, + prepared_material=prepared_material, ) return replace(result, planned_comment_count=len(findings)) @@ -274,15 +267,18 @@ async def _attempt( attempt_number: int, omitted: tuple[GitHubReviewFindingDisposition, ...], authenticated_login: str, - pr_author_login: str, resume_pending: bool = False, + prepared_material: tuple[dict[str, Any], bytes, str] | None = None, ) -> GitHubReviewPostResult: - payload, payload_json, attempt_digest = _poster_retry.attempt_material( - request=request, - operation_key=operation_key, - findings=findings, - omitted=omitted, - effective_event=effective_event, + payload, payload_json, attempt_digest = ( + prepared_material + or _poster_retry.attempt_material( + request=request, + operation_key=operation_key, + findings=findings, + omitted=omitted, + effective_event=effective_event, + ) ) attempt = self.ledger.begin_attempt( operation_key=operation_key, @@ -334,12 +330,8 @@ async def _attempt( response, attempt_number=attempt_number, comment_count=len(findings), - effective_event=effective_event, - authenticated_login=authenticated_login, - pr_author_login=pr_author_login, ) retry = False - next_event = effective_event match outcome: case ( ReviewAttemptOutcome.RETRY_STALE @@ -383,7 +375,7 @@ async def _attempt( state=ReviewOperationState.THROTTLED, response_class=response.response_class, status_code=response.status_code, - error=github_error_message(response.data) or response.error, + error=github_error_detail(response.data) or response.error, ) _poster_retry.release_slot( self, scope_id, slot, operation_key, keep_in_flight=False @@ -392,7 +384,7 @@ async def _attempt( request, ReviewOperationState.THROTTLED, response.response_class, - github_error_message(response.data) or "GitHub secondary rate limit", + github_error_detail(response.data) or "GitHub secondary rate limit", operation_key=operation_key, executed_mutations=attempt_number, ) @@ -400,18 +392,9 @@ async def _attempt( attempt_state = ReviewOperationState.COMMITTED_PENDING_VERIFICATION case ReviewAttemptOutcome.TRANSPORT_ERROR | ReviewAttemptOutcome.SERVER_ERROR: attempt_state = ReviewOperationState.AMBIGUOUS - case ( - ReviewAttemptOutcome.ANCHOR_422 - | ReviewAttemptOutcome.SELF_REVIEW_422 - | ReviewAttemptOutcome.COMBINED_422 - ): + case ReviewAttemptOutcome.ANCHOR_422: attempt_state = ReviewOperationState.AMBIGUOUS retry = True - if outcome in { - ReviewAttemptOutcome.SELF_REVIEW_422, - ReviewAttemptOutcome.COMBINED_422, - }: - next_event = "COMMENT" case ( ReviewAttemptOutcome.GENERIC_422 | ReviewAttemptOutcome.SECOND_422 @@ -422,7 +405,7 @@ async def _attempt( assert_never(outcome) assert response is not None and slot is not None response_class = response.response_class - error = github_error_message(response.data) or response.error + error = github_error_detail(response.data) or response.error reconciliation = await self._reconcile_payload( request=request, operation_key=operation_key, @@ -501,70 +484,67 @@ async def _attempt( reconciliation=reconciliation.result, executed_mutations=attempt_number, ) - next_findings = findings - next_omitted = list(omitted) - if invalid_index is not None: - scan = await _poster_reconcile.scan_remote_findings( - self, - request=request, - authenticated_login=authenticated_login, + assert invalid_index is not None + scan = await _poster_reconcile.scan_remote_findings( + self, + request=request, + authenticated_login=authenticated_login, + ) + if not scan.certain: + self.ledger.complete_attempt( + operation_key=operation_key, + attempt_number=attempt_number, + state=ReviewOperationState.AMBIGUOUS, + response_class=response_class, + status_code=response.status_code, + error=scan.error, ) - if not scan.certain: - self.ledger.complete_attempt( - operation_key=operation_key, - attempt_number=attempt_number, - state=ReviewOperationState.AMBIGUOUS, - response_class=response_class, - status_code=response.status_code, - error=scan.error, - ) - _poster_retry.release_slot( - self, scope_id, slot, operation_key, keep_in_flight=True - ) - return _poster_support.nonfinal_result( - request, - ReviewOperationState.AMBIGUOUS, - response_class, - scan.error or "remote finding deduplication was uncertain", - operation_key=operation_key, - executed_mutations=attempt_number, - ) - rejected = findings[invalid_index] - reduced: list[AdmittedFinding] = [] - for finding in findings: - if finding.canonical_index == rejected.canonical_index: - next_omitted.append( - GitHubReviewFindingDisposition( - original_index=finding.original_index, - canonical_index=finding.canonical_index, - kind=ReviewFindingDispositionKind.OMITTED_INVALID, - reason=_poster_support.structured_error_message(response) - or "invalid diff anchor", - ) - ) - elif (remote_id := scan.ids_by_digest.get(finding.digest)) is not None: - next_omitted.append( - GitHubReviewFindingDisposition( - original_index=finding.original_index, - canonical_index=finding.canonical_index, - kind=ReviewFindingDispositionKind.ALREADY_PRESENT, - remote_comment_id=remote_id, - ) - ) - else: - reduced.append(finding) - assert len(reduced) < len(findings) - next_findings = tuple(reduced) - if not next_findings: - next_omitted = list(omitted) + [ + _poster_retry.release_slot(self, scope_id, slot, operation_key, keep_in_flight=True) + return _poster_support.nonfinal_result( + request, + ReviewOperationState.AMBIGUOUS, + response_class, + scan.error or "remote finding deduplication was uncertain", + operation_key=operation_key, + executed_mutations=attempt_number, + ) + rejected = findings[invalid_index] + next_omitted = list(omitted) + reduced: list[AdmittedFinding] = [] + for finding in findings: + if finding.canonical_index == rejected.canonical_index: + next_omitted.append( GitHubReviewFindingDisposition( original_index=finding.original_index, canonical_index=finding.canonical_index, kind=ReviewFindingDispositionKind.OMITTED_INVALID, - reason="validation correction exhausted inline findings", + reason=_poster_support.structured_error_message(response) + or "invalid diff anchor", ) - for finding in findings - ] + ) + elif (remote_id := scan.ids_by_digest.get(finding.digest)) is not None: + next_omitted.append( + GitHubReviewFindingDisposition( + original_index=finding.original_index, + canonical_index=finding.canonical_index, + kind=ReviewFindingDispositionKind.ALREADY_PRESENT, + remote_comment_id=remote_id, + ) + ) + else: + reduced.append(finding) + assert len(reduced) < len(findings) + next_findings = tuple(reduced) + if not next_findings: + next_omitted = list(omitted) + [ + GitHubReviewFindingDisposition( + original_index=finding.original_index, + canonical_index=finding.canonical_index, + kind=ReviewFindingDispositionKind.OMITTED_INVALID, + reason="validation correction exhausted inline findings", + ) + for finding in findings + ] next_omitted_tuple = tuple(next_omitted) _poster_retry.schedule_retry( self, @@ -576,7 +556,7 @@ async def _attempt( error=error, retry_attempt_number=2, retry_findings=next_findings, - retry_effective_event=next_event, + retry_effective_event=effective_event, retry_omitted=next_omitted_tuple, ) _poster_retry.release_slot(self, scope_id, slot, operation_key, keep_in_flight=False) @@ -585,11 +565,10 @@ async def _attempt( operation_key=operation_key, scope_id=scope_id, findings=next_findings, - effective_event=next_event, + effective_event=effective_event, attempt_number=2, omitted=next_omitted_tuple, authenticated_login=authenticated_login, - pr_author_login=pr_author_login, resume_pending=True, ) diff --git a/src/autoskillit/skills_extended/audit-claims/SKILL.md b/src/autoskillit/skills_extended/audit-claims/SKILL.md index d00dc94dbf..f8308f2491 100644 --- a/src/autoskillit/skills_extended/audit-claims/SKILL.md +++ b/src/autoskillit/skills_extended/audit-claims/SKILL.md @@ -65,6 +65,9 @@ comments and emits a verdict for recipe routing. ## Critical Constraints +Never infer publication success from the GitHub HTTP response `comments` array +or its length. + **NEVER:** - Fabricate, invent, or embellish information not supported by the available evidence or code. diff --git a/src/autoskillit/skills_extended/resolve-claims-review/SKILL.md b/src/autoskillit/skills_extended/resolve-claims-review/SKILL.md index 9d231b413f..1e1b2be061 100644 --- a/src/autoskillit/skills_extended/resolve-claims-review/SKILL.md +++ b/src/autoskillit/skills_extended/resolve-claims-review/SKILL.md @@ -61,6 +61,9 @@ Called by the research recipe when `audit_claims` routes `changes_requested` via ## Critical Constraints +Never infer publication success from the GitHub HTTP response `comments` array +or its length. + **NEVER:** - Fabricate, invent, or embellish information not supported by the available evidence or code. diff --git a/src/autoskillit/skills_extended/resolve-research-review/SKILL.md b/src/autoskillit/skills_extended/resolve-research-review/SKILL.md index 05ac15f493..ad8f09e190 100644 --- a/src/autoskillit/skills_extended/resolve-research-review/SKILL.md +++ b/src/autoskillit/skills_extended/resolve-research-review/SKILL.md @@ -60,6 +60,9 @@ Bounded by `retries: 2` — on exhaustion routes to `research_complete`. ## Critical Constraints +Never infer publication success from the GitHub HTTP response `comments` array +or its length. + **NEVER:** - Fabricate, invent, or embellish information not supported by the available evidence or code. diff --git a/src/autoskillit/skills_extended/resolve-review/SKILL.md b/src/autoskillit/skills_extended/resolve-review/SKILL.md index 275516c1fe..c3ca1d7e2b 100644 --- a/src/autoskillit/skills_extended/resolve-review/SKILL.md +++ b/src/autoskillit/skills_extended/resolve-review/SKILL.md @@ -76,6 +76,9 @@ branch already checked out. ## Critical Constraints +Never infer publication success from the GitHub HTTP response `comments` array +or its length. + **NEVER:** - Fabricate, invent, or embellish information not supported by the available evidence or code. diff --git a/src/autoskillit/skills_extended/review-pr/SKILL.md b/src/autoskillit/skills_extended/review-pr/SKILL.md index fa4dbdb69a..81ac473018 100644 --- a/src/autoskillit/skills_extended/review-pr/SKILL.md +++ b/src/autoskillit/skills_extended/review-pr/SKILL.md @@ -73,6 +73,9 @@ by the recipe pipeline after `open_pr_step` opens the PR. ## Critical Constraints +Never infer publication success from the GitHub HTTP response `comments` array +or its length. + **NEVER:** - Fabricate, invent, or embellish information not supported by the available evidence or code. diff --git a/src/autoskillit/skills_extended/review-research-pr/SKILL.md b/src/autoskillit/skills_extended/review-research-pr/SKILL.md index c10d54e5bc..3029adddb8 100644 --- a/src/autoskillit/skills_extended/review-research-pr/SKILL.md +++ b/src/autoskillit/skills_extended/review-research-pr/SKILL.md @@ -62,6 +62,9 @@ a summary verdict. Called by the recipe pipeline after `open_research_pr` opens ## Critical Constraints +Never infer publication success from the GitHub HTTP response `comments` array +or its length. + **NEVER:** - Fabricate, invent, or embellish information not supported by the available evidence or code. diff --git a/tests/arch/test_skill_family_parity.py b/tests/arch/test_skill_family_parity.py index b60a597de2..dc178b0875 100644 --- a/tests/arch/test_skill_family_parity.py +++ b/tests/arch/test_skill_family_parity.py @@ -5,6 +5,8 @@ from __future__ import annotations +from typing import ClassVar + import pytest from autoskillit.core.paths import pkg_root @@ -41,11 +43,23 @@ def test_thread_resolvers_require_drift_discipline() -> None: assert "drift-discipline" in family.required_patterns -class TestGraphqlBatchAliases: +@pytest.mark.parametrize("family_name", ["thread-resolvers", "review-posters"]) +def test_review_families_require_response_body_guard(family_name: str) -> None: + family = next(family for family in GITHUB_API_SKILL_FAMILIES if family.name == family_name) + assert "response-body-guard" in family.required_patterns + + +class _PatternGuard: + PATTERN: ClassVar[str] + @pytest.fixture(autouse=True) def _filter(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - if "graphql-batch-aliases" not in family_skill[2].required_patterns: - pytest.skip("graphql-batch-aliases not required for this family") + if self.PATTERN not in family_skill[2].required_patterns: + pytest.skip(f"{self.PATTERN} not required for this family") + + +class TestGraphqlBatchAliases(_PatternGuard): + PATTERN = "graphql-batch-aliases" def test_uses_batch_aliases(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: text = _read_skill_md(family_skill[1]) @@ -55,11 +69,8 @@ def test_uses_batch_aliases(self, family_skill: tuple[str, str, SkillFamilyDef]) ) -class TestMutatingCallDelay: - @pytest.fixture(autouse=True) - def _filter(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - if "mutating-call-delay" not in family_skill[2].required_patterns: - pytest.skip("mutating-call-delay not required for this family") +class TestMutatingCallDelay(_PatternGuard): + PATTERN = "mutating-call-delay" def test_has_sleep_delay(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: text = _read_skill_md(family_skill[1]) @@ -68,11 +79,8 @@ def test_has_sleep_delay(self, family_skill: tuple[str, str, SkillFamilyDef]) -> ) -class TestUnpostablePrefilter: - @pytest.fixture(autouse=True) - def _filter(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - if "unpostable-prefilter" not in family_skill[2].required_patterns: - pytest.skip("unpostable-prefilter not required for this family") +class TestUnpostablePrefilter(_PatternGuard): + PATTERN = "unpostable-prefilter" def test_has_unpostable_bucket(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: text = _read_skill_md(family_skill[1]) @@ -81,52 +89,19 @@ def test_has_unpostable_bucket(self, family_skill: tuple[str, str, SkillFamilyDe ) -class TestResponseBodyGuard: - @pytest.fixture(autouse=True) - def _filter(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - if "response-body-guard" not in family_skill[2].required_patterns: - pytest.skip("response-body-guard not required for this family") +class TestResponseBodyGuard(_PatternGuard): + PATTERN = "response-body-guard" def test_has_response_body_guard(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - text = _read_skill_md(family_skill[1]) - text_lower = text.lower() - assert "http 200" in text_lower, f"{family_skill[1]} must document HTTP 200 success signal" - idx = text_lower.find("http 200") - after_http200 = text_lower[idx:] + text = " ".join(_read_skill_md(family_skill[1]).split()) assert ( - "do not inspect" in after_http200 - or "regardless of response body" in after_http200 - or "do not check" in after_http200 - ), f"{family_skill[1]} must prohibit response body inspection near HTTP 200 reference" + "Never infer publication success from the GitHub HTTP response `comments` array " + "or its length." + ) in text, f"{family_skill[1]} must prohibit inferring success from response comments" -class TestOwnPrGuard: - @pytest.fixture(autouse=True) - def _filter(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - if "own-pr-guard" not in family_skill[2].required_patterns: - pytest.skip("own-pr-guard not required for this family") - - def test_has_own_pr_guard(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - text = _read_skill_md(family_skill[1]) - text_lower = text.lower() - has_self_ref = ( - "own pr" in text_lower or "self-review" in text_lower or "authored" in text_lower - ) - has_comment_fallback = ( - "`comment`" in text_lower - or '"comment"' in text_lower - or "comment fallback" in text_lower - ) - assert has_self_ref and has_comment_fallback, ( - f"{family_skill[1]} must include own-PR guard with COMMENT fallback" - ) - - -class TestDriftDiscipline: - @pytest.fixture(autouse=True) - def _filter(self, family_skill: tuple[str, str, SkillFamilyDef]) -> None: - if "drift-discipline" not in family_skill[2].required_patterns: - pytest.skip("drift-discipline not required for this family") +class TestDriftDiscipline(_PatternGuard): + PATTERN = "drift-discipline" def test_groups_descends_and_rereads_live_source( self, family_skill: tuple[str, str, SkillFamilyDef] @@ -141,3 +116,14 @@ def test_groups_descends_and_rereads_live_source( assert "live" in text_lower and ("re-read" in text_lower or "reread" in text_lower), ( f"{family_skill[1]} must re-read live source before each edit" ) + + +def test_pattern_guards_match_required_patterns() -> None: + """Every local guard covers a pattern required by a skill family.""" + guard_classes = [ + cls for name, cls in globals().items() if name.startswith("Test") and isinstance(cls, type) + ] + assert all(issubclass(cls, _PatternGuard) for cls in guard_classes) + assert {cls.PATTERN for cls in guard_classes} == set().union( + *(family.required_patterns for family in GITHUB_API_SKILL_FAMILIES) + ) diff --git a/tests/execution/github_review/fakes.py b/tests/execution/github_review/fakes.py index 78d97e1537..1549ab78ee 100644 --- a/tests/execution/github_review/fakes.py +++ b/tests/execution/github_review/fakes.py @@ -21,6 +21,11 @@ from autoskillit.execution.github_review.gateway import ( CredentialScopeMaterial, GatewayResult, + _classify_response, +) +from tests.fixtures.github_rest import ( + reviews_create_422_author_approve, + reviews_create_422_author_request_changes, ) _REMOTE_STATES = { @@ -122,6 +127,17 @@ async def create_review( self.create_calls.append(payload) self.call_trace.append("create_review") self.create_timestamps.append(self.clock()) + if self.authenticated_login.casefold() == self.pr_author_login.casefold() and payload[ + "event" + ] in {"APPROVE", "REQUEST_CHANGES"}: + body = ( + reviews_create_422_author_approve() + if payload["event"] == "APPROVE" + else reviews_create_422_author_request_changes() + ) + return GatewayResult( + 422, data=body, headers={}, response_class=_classify_response(422, body, {}) + ) outcome = self.outcomes.pop(0) review_id = self._commit(payload) if outcome.commit else None if outcome.fail_reads_after: @@ -133,7 +149,11 @@ async def create_review( status_code=outcome.status_code, data=data, headers=outcome.headers or {}, - response_class=_classify(outcome.status_code), + response_class=( + ReviewResponseClass.TRANSPORT_ERROR + if outcome.status_code is None + else _classify_response(outcome.status_code, data, outcome.headers or {}) + ), error=outcome.error, ) @@ -220,20 +240,6 @@ def _success(data: Any) -> GatewayResult: ) -def _classify(status_code: int | None) -> ReviewResponseClass: - if status_code is None: - return ReviewResponseClass.TRANSPORT_ERROR - if 200 <= status_code < 300: - return ReviewResponseClass.SUCCESS - if status_code >= 500: - return ReviewResponseClass.SERVER_ERROR - if status_code in {403, 429}: - return ReviewResponseClass.SECONDARY_RATE_LIMIT - if status_code == 422: - return ReviewResponseClass.VALIDATION_ERROR - return ReviewResponseClass.CLIENT_ERROR - - def _request(tmp_path: Path, **overrides: object) -> GitHubReviewRequest: values: dict[str, object] = { "repository": "octo/example", diff --git a/tests/execution/github_review/test_gateway.py b/tests/execution/github_review/test_gateway.py new file mode 100644 index 0000000000..8746cc39dd --- /dev/null +++ b/tests/execution/github_review/test_gateway.py @@ -0,0 +1,49 @@ +"""HTTP response classification at the real GitHub review gateway boundary.""" + +import httpx +import pytest + +from autoskillit.core import ReviewResponseClass +from autoskillit.execution.github_review.gateway import DefaultGitHubReviewGateway + +pytestmark = [pytest.mark.layer("execution"), pytest.mark.small] + + +@pytest.mark.anyio +@pytest.mark.parametrize( + "status,message,expected", + [ + (201, "Created", ReviewResponseClass.SUCCESS), + (422, "Validation Failed", ReviewResponseClass.VALIDATION_ERROR), + (502, "Bad Gateway", ReviewResponseClass.SERVER_ERROR), + (401, "Bad credentials", ReviewResponseClass.CLIENT_ERROR), + (403, "Resource not accessible by integration", ReviewResponseClass.CLIENT_ERROR), + ( + 403, + "You have exceeded a secondary rate limit", + ReviewResponseClass.SECONDARY_RATE_LIMIT, + ), + ( + 403, + "You have triggered an abuse detection mechanism", + ReviewResponseClass.SECONDARY_RATE_LIMIT, + ), + (429, "Too Many Requests", ReviewResponseClass.SECONDARY_RATE_LIMIT), + ], +) +async def test_gateway_classifies_http_responses( + status: int, message: str, expected: ReviewResponseClass +) -> None: + transport = httpx.MockTransport( + lambda request: httpx.Response(status, json={"message": message}) + ) + gateway = DefaultGitHubReviewGateway( + token_factory=lambda: "test-token", + api_log=None, + client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), + ) + + result = await gateway.create_review("owner/repo", 1, {"event": "COMMENT", "body": "Review"}) + + assert result.status_code == status + assert result.response_class is expected diff --git a/tests/execution/github_review/test_resilience.py b/tests/execution/github_review/test_resilience.py index 5a14291665..b429b78acf 100644 --- a/tests/execution/github_review/test_resilience.py +++ b/tests/execution/github_review/test_resilience.py @@ -2,6 +2,8 @@ from __future__ import annotations +import hashlib +from collections.abc import Callable from pathlib import Path import pytest @@ -19,6 +21,11 @@ from autoskillit.execution.github_review import _poster_support from autoskillit.execution.github_review.canonical import admit_findings, canonical_findings from autoskillit.execution.github_review.gateway import GatewayResult +from tests.fixtures.github_rest import ( + reviews_create_422_author_approve, + reviews_create_422_author_request_changes, + reviews_create_422_line_unresolved, +) from .fakes import ( CreateOutcome, @@ -83,7 +90,6 @@ async def test_pre_gateway_guard_returns_are_classified_outcomes( attempt_number=2, omitted=(), authenticated_login="bot", - pr_author_login="author", resume_pending=outcome is not ReviewAttemptOutcome.SLOT_BLOCKED, ) assert result.state is ReviewOperationState.AMBIGUOUS @@ -149,66 +155,6 @@ def test_structured_invalid_comment_index_rejects_malformed_suffix(field: str) - ) -def _self_review_error() -> dict[str, object]: - return { - "message": "Validation Failed", - "errors": [ - { - "resource": "PullRequestReview", - "field": "event", - "code": "invalid", - "message": "Can not request changes on your own pull request", - } - ], - } - - -@pytest.mark.anyio -async def test_combined_self_review_and_anchor_422_does_not_retry_unreduced( - tmp_path: Path, -) -> None: - combined = _validation_error(0) - combined["errors"] += _self_review_error()["errors"] - clock = ManualClock() - gateway = StatefulReviewGateway( - clock=clock, - authenticated_login="author", - pr_author_login="author", - outcomes=[CreateOutcome(422, data=combined), CreateOutcome(200, commit=True)], - ) - result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post( - _request( - tmp_path, - event="REQUEST_CHANGES", - comments=( - GitHubReviewComment(path="src/a.py", line=10, body="Reject"), - GitHubReviewComment(path="src/b.py", line=20, body="Keep"), - ), - ) - ) - assert result.state is ReviewOperationState.SUCCEEDED - assert [len(call["comments"]) for call in gateway.create_calls] == [2, 1] - assert gateway.create_calls[1]["event"] == "COMMENT" - assert "Reject" in gateway.create_calls[1]["body"] - assert gateway.call_trace[:2] == ["create_review", "list_reviews"] - - -@pytest.mark.anyio -async def test_self_review_retry_reconciles_before_mutating(tmp_path: Path) -> None: - clock = ManualClock() - gateway = StatefulReviewGateway( - clock=clock, - authenticated_login="author", - pr_author_login="author", - outcomes=[CreateOutcome(422, data=_self_review_error()), CreateOutcome(200, commit=True)], - ) - result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post( - _request(tmp_path, event="REQUEST_CHANGES") - ) - assert result.state is ReviewOperationState.SUCCEEDED - assert gateway.call_trace == ["create_review", "list_reviews", "create_review", "list_reviews"] - - @pytest.mark.anyio @pytest.mark.parametrize("second,fail_reads", [(False, True), (True, True), (True, False)]) async def test_generic_and_second_422_reconcile_before_terminal( @@ -217,7 +163,7 @@ async def test_generic_and_second_422_reconcile_before_terminal( fail_reads: bool, ) -> None: clock = ManualClock() - outcomes = [CreateOutcome(422, data=_self_review_error())] if second else [] + outcomes = [CreateOutcome(422, data=_validation_error(0))] if second else [] outcomes.append( CreateOutcome( 422, @@ -225,12 +171,8 @@ async def test_generic_and_second_422_reconcile_before_terminal( fail_reads_after=fail_reads, ) ) - gateway = StatefulReviewGateway( - clock=clock, authenticated_login="author", pr_author_login="author", outcomes=outcomes - ) - result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post( - _request(tmp_path, event="REQUEST_CHANGES") - ) + gateway = StatefulReviewGateway(clock=clock, outcomes=outcomes) + result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post(_request(tmp_path)) assert result.state is ( ReviewOperationState.AMBIGUOUS if fail_reads else ReviewOperationState.TERMINAL ) @@ -243,14 +185,12 @@ async def test_second_422_after_corrected_retry_terminates_without_third_attempt tmp_path: Path, ) -> None: database_path = tmp_path / "ledger.sqlite3" - request = _request(tmp_path, event="REQUEST_CHANGES") + request = _request(tmp_path) clock = ManualClock() gateway = StatefulReviewGateway( clock=clock, - authenticated_login="author", - pr_author_login="author", outcomes=[ - CreateOutcome(422, data=_self_review_error()), + CreateOutcome(422, data=_validation_error(0)), CreateOutcome(422, data=_validation_error(0)), ], ) @@ -262,6 +202,7 @@ async def test_second_422_after_corrected_retry_terminates_without_third_attempt assert gateway.call_trace == [ "create_review", "list_reviews", + "list_reviews", "create_review", "list_reviews", ] @@ -536,6 +477,57 @@ def crash_after_retry_is_persisted(*, operation_key: str, attempt_number: int) - assert ledger.load_attempts(operation_key)[-1].state == (ReviewOperationState.SUCCEEDED.value) +@pytest.mark.anyio +@pytest.mark.parametrize( + "reviewer,resumed_reviewer", + [("reviewer", "pull-author"), ("pull-author", "reviewer")], +) +async def test_resumed_retry_rejects_changed_author_event( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + reviewer: str, + resumed_reviewer: str, +) -> None: + request = _request( + tmp_path, + event="APPROVE", + comments=( + GitHubReviewComment(path="src/a.py", line=10, body="Reject A"), + GitHubReviewComment(path="src/b.py", line=20, body="Keep B"), + ), + ) + database_path = tmp_path / "ledger.sqlite3" + clock = ManualClock() + gateway = StatefulReviewGateway( + clock=clock, + authenticated_login=reviewer, + pr_author_login="pull-author", + outcomes=[CreateOutcome(422, data=_validation_error(0))], + ) + poster = _poster(database_path, gateway, clock) + + def crash_after_retry_is_persisted(*, operation_key: str, attempt_number: int) -> bool: + raise RuntimeError("crash after retry scheduling") + + monkeypatch.setattr(poster.ledger, "claim_retry_attempt", crash_after_retry_is_persisted) + interrupted = await poster.post(request) + assert interrupted.state is ReviewOperationState.AMBIGUOUS + operation_key = compute_review_operation_key(request) + assert GitHubReviewLedger(database_path).load_attempts(operation_key)[-1].state == ( + ReviewOperationState.RETRY_PENDING.value + ) + + monkeypatch.setattr(gateway, "authenticated_login", resumed_reviewer) + recovered = await _poster(database_path, gateway, clock).post(request) + + assert recovered.state is ReviewOperationState.TERMINAL + assert "current reviewer identity" in recovered.error + assert len(gateway.create_calls) == 1 + attempts = GitHubReviewLedger(database_path).load_attempts(operation_key) + assert len(attempts) == 2 + assert attempts[-1].state == ReviewOperationState.TERMINAL.value + + @pytest.mark.anyio async def test_release_failure_after_reduced_retry_preserves_retry_intent( tmp_path: Path, @@ -727,75 +719,266 @@ async def test_unstructured_or_second_422_is_terminal_without_further_fallback( @pytest.mark.anyio -async def test_self_review_422_transforms_request_changes_to_comment_once( +async def test_secondary_rate_limit_persists_backpressure_without_retry_or_receipt( tmp_path: Path, ) -> None: - request = _request(tmp_path, event="REQUEST_CHANGES") clock = ManualClock() gateway = StatefulReviewGateway( clock=clock, - authenticated_login="pull-author", - pr_author_login="pull-author", outcomes=[ - CreateOutcome(422, data=_self_review_error()), - CreateOutcome(200, commit=True), + CreateOutcome( + 403, + data={"message": "You have exceeded a secondary rate limit."}, + headers={"retry-after": "7"}, + ) ], ) + database_path = tmp_path / "ledger.sqlite3" - result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post(request) + result = await _poster(database_path, gateway, clock).post(_request(tmp_path)) + + assert result.state is ReviewOperationState.THROTTLED + assert result.executed_mutation_count == 1 + assert result.receipt is None + assert GitHubReviewLedger(database_path).load_receipt(result.operation_key) is None + assert len(gateway.create_calls) == 1 + + +_AUTHOR_DISCLOSURES = { + "APPROVE": ( + "Requested review event APPROVE was published as COMMENT because GitHub does not allow " + "a pull request's author to approve it; this is not a GitHub approval." + ), + "REQUEST_CHANGES": ( + "Requested review event REQUEST_CHANGES was published as COMMENT because GitHub does " + "not allow a pull request's author to request changes on it; this review does not " + "block merging." + ), +} + + +@pytest.mark.parametrize( + "event,reviewer,author,expected", + [ + ("APPROVE", "pull-author", "pull-author", "COMMENT"), + ("REQUEST_CHANGES", "pull-author", "pull-author", "COMMENT"), + ("COMMENT", "pull-author", "pull-author", "COMMENT"), + ("APPROVE", "reviewer", "pull-author", "APPROVE"), + ("REQUEST_CHANGES", "reviewer", "pull-author", "REQUEST_CHANGES"), + ("APPROVE", "Pull-Author", "pull-author", "COMMENT"), + ], +) +def test_author_effective_event_decision_table( + event: str, reviewer: str, author: str, expected: str +) -> None: + assert ( + _poster_support.author_effective_event( + event, authenticated_login=reviewer, pr_author_login=author + ) + == expected + ) + + +@pytest.mark.parametrize( + "requested,event,body,raises", + [ + ("APPROVE", "COMMENT", "Automated review", False), + ("APPROVE", "COMMENT", "", False), + ("REQUEST_CHANGES", "COMMENT", "Automated review", False), + ("COMMENT", "COMMENT", "Automated review", False), + ("COMMENT", "APPROVE", "Automated review", True), + ("APPROVE", "REQUEST_CHANGES", "Automated review", True), + ], +) +def test_payload_discloses_only_author_comment_transformation( + tmp_path: Path, requested: str, event: str, body: str, raises: bool +) -> None: + request = _request(tmp_path, event=requested, body=body) + arguments = dict(request=request, operation_key="key", findings=(), omitted=(), event=event) + if raises: + with pytest.raises( + ValueError, match="unsupported review event transformation" + ) as exc_info: + _poster_support.payload(**arguments) + assert f"{requested!r} -> {event!r}" in str(exc_info.value) + return + pieces = [body] if body else [] + if event != requested: + pieces.append(_AUTHOR_DISCLOSURES[requested]) + pieces.append("") + assert _poster_support.payload(**arguments)["body"] == "\n\n".join(pieces) + + +@pytest.mark.anyio +@pytest.mark.parametrize("event", ["APPROVE", "REQUEST_CHANGES", "COMMENT"]) +async def test_author_review_publishes_one_comment(tmp_path: Path, event: str) -> None: + request = _request( + tmp_path, + event=event, + comments=( + GitHubReviewComment(path="src/a.py", line=10, body="First finding"), + GitHubReviewComment(path="src/b.py", line=20, body="Second finding"), + ), + ) + clock = ManualClock() + gateway = StatefulReviewGateway( + clock=clock, authenticated_login="pull-author", pr_author_login="pull-author" + ) + db = tmp_path / "ledger.sqlite3" + result = await _poster(db, gateway, clock).post(request) - assert [payload["event"] for payload in gateway.create_calls] == [ - "REQUEST_CHANGES", - "COMMENT", - ] - assert gateway.create_timestamps[1] - gateway.create_timestamps[0] >= 1.0 assert result.state is ReviewOperationState.SUCCEEDED + assert result.executed_mutation_count == 1 + assert [call["event"] for call in gateway.create_calls] == ["COMMENT"] + assert len(gateway.create_calls[0]["comments"]) == len(request.comments) + pieces = [request.body] + if event != "COMMENT": + pieces.append(_AUTHOR_DISCLOSURES[event]) + pieces.append(f"") + body = "\n\n".join(pieces) + assert gateway.create_calls[0]["body"] == body assert result.receipt is not None - assert result.receipt.requested_event == "REQUEST_CHANGES" + assert result.receipt.requested_event == event assert result.receipt.effective_event == "COMMENT" + assert result.receipt.effective_body_digest == hashlib.sha256(body.encode()).hexdigest() + assert [review["state"] for review in gateway.reviews] == ["COMMENTED"] + assert [ + attempt.effective_event + for attempt in GitHubReviewLedger(db).load_attempts(result.operation_key) + ] == ["COMMENT"] @pytest.mark.anyio -async def test_self_review_text_without_identity_proof_does_not_transform( - tmp_path: Path, +@pytest.mark.parametrize( + "event,state", [("APPROVE", "APPROVED"), ("REQUEST_CHANGES", "CHANGES_REQUESTED")] +) +async def test_third_party_review_keeps_requested_event( + tmp_path: Path, event: str, state: str ) -> None: clock = ManualClock() - gateway = StatefulReviewGateway( - clock=clock, - authenticated_login="review-bot", - pr_author_login="pull-author", - outcomes=[CreateOutcome(422, data=_self_review_error())], - ) + gateway = StatefulReviewGateway(clock=clock) + request = _request(tmp_path, event=event) + result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post(request) - result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post( - _request(tmp_path, event="REQUEST_CHANGES") + assert result.state is ReviewOperationState.SUCCEEDED + assert [call["event"] for call in gateway.create_calls] == [event] + assert [review["state"] for review in gateway.reviews] == [state] + assert gateway.create_calls[0]["body"] == ( + f"{request.body}\n\n" ) + assert result.receipt is not None + assert result.receipt.requested_event == result.receipt.effective_event == event + + +@pytest.mark.anyio +@pytest.mark.parametrize( + "capture,event,error", + [ + ( + reviews_create_422_author_approve, + "APPROVE", + "Unprocessable Entity: Review Can not approve your own pull request", + ), + ( + reviews_create_422_author_request_changes, + "REQUEST_CHANGES", + "Unprocessable Entity: Review Can not request changes on your own pull request", + ), + ( + reviews_create_422_line_unresolved, + "COMMENT", + "Unprocessable Entity: Line could not be resolved", + ), + ], +) +async def test_captured_review_422_is_terminal_with_github_detail( + tmp_path: Path, capture: Callable[[], dict[str, object]], event: str, error: str +) -> None: + clock = ManualClock() + gateway = StatefulReviewGateway(clock=clock, outcomes=[CreateOutcome(422, data=capture())]) + db = tmp_path / "ledger.sqlite3" + result = await _poster(db, gateway, clock).post(_request(tmp_path, event=event)) assert result.state is ReviewOperationState.TERMINAL - assert len(gateway.create_calls) == 1 + assert result.executed_mutation_count == 1 + assert [call["event"] for call in gateway.create_calls] == [event] + assert gateway.call_trace == ["create_review", "list_reviews"] + assert result.error == error + ledger = GitHubReviewLedger(db) + assert ledger.load_attempts(result.operation_key)[0].error == error + assert result.receipt is None + assert ledger.load_receipt(result.operation_key) is None @pytest.mark.anyio -async def test_secondary_rate_limit_persists_backpressure_without_retry_or_receipt( - tmp_path: Path, +async def test_persisted_terminal_author_approve_is_not_cleared(tmp_path: Path) -> None: + clock = ManualClock() + db = tmp_path / "ledger.sqlite3" + request = _request(tmp_path, event="APPROVE") + gateway = StatefulReviewGateway( + clock=clock, outcomes=[CreateOutcome(422, data=reviews_create_422_author_approve())] + ) + first = await _poster(db, gateway, clock).post(request) + gateway2 = StatefulReviewGateway( + clock=clock, authenticated_login="pull-author", pr_author_login="pull-author" + ) + result = await _poster(db, gateway2, clock).post(request) + + assert first.state is ReviewOperationState.TERMINAL + assert result.state is ReviewOperationState.AMBIGUOUS + assert gateway2.create_calls == [] + assert len(GitHubReviewLedger(db).load_attempts(first.operation_key)) == 1 + + +@pytest.mark.anyio +@pytest.mark.parametrize( + "event,own_author,length", [("APPROVE", True, 59990), ("COMMENT", False, 60001)] +) +async def test_effective_body_overflow_rejected_before_ledger_state( + tmp_path: Path, event: str, own_author: bool, length: int ) -> None: clock = ManualClock() gateway = StatefulReviewGateway( clock=clock, - outcomes=[ - CreateOutcome( - 403, - data={"message": "You have exceeded a secondary rate limit."}, - headers={"retry-after": "7"}, - ) - ], + authenticated_login="pull-author" if own_author else "reviewer", + pr_author_login="pull-author", ) - database_path = tmp_path / "ledger.sqlite3" + db = tmp_path / "ledger.sqlite3" + request = _request(tmp_path, event=event, body="x" * length) + result = await _poster(db, gateway, clock).post(request) - result = await _poster(database_path, gateway, clock).post(_request(tmp_path)) + assert result.state is ReviewOperationState.TERMINAL + assert "60000" in result.error + assert gateway.create_calls == [] + assert GitHubReviewLedger(db).load_operation(compute_review_operation_key(request)) is None - assert result.state is ReviewOperationState.THROTTLED - assert result.executed_mutation_count == 1 - assert result.receipt is None - assert GitHubReviewLedger(database_path).load_receipt(result.operation_key) is None - assert len(gateway.create_calls) == 1 + +@pytest.mark.anyio +async def test_author_event_anchor_retry_keeps_disclosed_comment(tmp_path: Path) -> None: + clock = ManualClock() + gateway = StatefulReviewGateway( + clock=clock, + authenticated_login="pull-author", + pr_author_login="pull-author", + outcomes=[CreateOutcome(422, data=_validation_error(0)), CreateOutcome(200, commit=True)], + ) + request = _request( + tmp_path, + event="REQUEST_CHANGES", + comments=( + GitHubReviewComment(path="src/a.py", line=10, body="Reject"), + GitHubReviewComment(path="src/b.py", line=20, body="Keep"), + ), + ) + result = await _poster(tmp_path / "ledger.sqlite3", gateway, clock).post(request) + + assert result.state is ReviewOperationState.SUCCEEDED + assert [call["event"] for call in gateway.create_calls] == ["COMMENT", "COMMENT"] + assert [len(call["comments"]) for call in gateway.create_calls] == [2, 1] + assert all( + _AUTHOR_DISCLOSURES["REQUEST_CHANGES"] in call["body"] for call in gateway.create_calls + ) + assert result.receipt is not None + assert result.receipt.requested_event == "REQUEST_CHANGES" + assert result.receipt.effective_event == "COMMENT" + assert gateway.create_timestamps[1] - gateway.create_timestamps[0] >= 1.0 diff --git a/tests/execution/test_github_http.py b/tests/execution/test_github_http.py new file mode 100644 index 0000000000..b4bd058ccc --- /dev/null +++ b/tests/execution/test_github_http.py @@ -0,0 +1,41 @@ +"""GitHub validation diagnostics retain bounded response details.""" + +import json + +import pytest + +from autoskillit.execution.github_ops import _github_http +from tests.fixtures.github_rest import reviews_create_422_author_approve + +pytestmark = [pytest.mark.layer("execution"), pytest.mark.small] + + +def test_error_detail_renders_string_errors() -> None: + assert _github_http.github_error_detail(reviews_create_422_author_approve()) == ( + "Unprocessable Entity: Review Can not approve your own pull request" + ) + + +def test_error_detail_renders_object_errors() -> None: + error = {"resource": "Issue", "field": "title", "code": "missing_field"} + assert _github_http.github_error_detail( + {"message": "Validation Failed", "errors": [error]} + ) == "Validation Failed: " + json.dumps(error, sort_keys=True) + + +def test_error_detail_is_bounded() -> None: + detail = _github_http.github_error_detail( + {"message": "Validation Failed", "errors": ["x" * 1000] * 50} + ) + prefix = "Validation Failed: " + assert detail == prefix + "x" * (500 - len(prefix)) + + +def test_error_detail_returns_message_without_errors() -> None: + assert _github_http.github_error_detail({"message": "Unprocessable Entity"}) == ( + "Unprocessable Entity" + ) + + +def test_error_detail_bounds_message_without_errors() -> None: + assert _github_http.github_error_detail({"message": "x" * 1000}) == "x" * 500 diff --git a/tests/fixtures/github_rest.py b/tests/fixtures/github_rest.py new file mode 100644 index 0000000000..e8a3f71756 --- /dev/null +++ b/tests/fixtures/github_rest.py @@ -0,0 +1,49 @@ +"""GitHub REST response bodies captured from actual API calls.""" + + +def reviews_create_422_author_approve() -> dict[str, object]: + """POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews, status 422. + + Captured 2026-06-27 UTC, PR #4128, reviewer == author, APPROVE. + ``gh api … --method POST`` output; body is the GitHub response JSON. + """ + return { + "message": "Unprocessable Entity", + "errors": ["Review Can not approve your own pull request"], + "documentation_url": ( + "https://docs.github.com/rest/pulls/reviews#create-a-review-for-a-pull-request" + ), + "status": "422", + } + + +def reviews_create_422_author_request_changes() -> dict[str, object]: + """POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews, status 422. + + Captured 2026-07-17 UTC, PR #4277, reviewer == author, REQUEST_CHANGES. + ``gh api … --method POST`` output; body is the GitHub response JSON. + """ + return { + "message": "Unprocessable Entity", + "errors": ["Review Can not request changes on your own pull request"], + "documentation_url": ( + "https://docs.github.com/rest/pulls/reviews#create-a-review-for-a-pull-request" + ), + "status": "422", + } + + +def reviews_create_422_line_unresolved() -> dict[str, object]: + """POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews, status 422. + + Captured 2026-07-31 UTC, PR #4427, inline comment outside the resolvable diff. + ``gh api … --method POST`` output; body is the GitHub response JSON. + """ + return { + "message": "Unprocessable Entity", + "errors": ["Line could not be resolved"], + "documentation_url": ( + "https://docs.github.com/rest/pulls/reviews#create-a-review-for-a-pull-request" + ), + "status": "422", + } diff --git a/tests/infra/test_pretty_output_recipe.py b/tests/infra/test_pretty_output_recipe.py index fdd058949a..c1850867c4 100644 --- a/tests/infra/test_pretty_output_recipe.py +++ b/tests/infra/test_pretty_output_recipe.py @@ -1375,9 +1375,9 @@ def test_pretty_output_recipe_grid_preserves_semantics_and_budgets(tmp_path, mon f"ingredients_only=both; actual={measured_modes!r}; limit=all registry modes" ) assert maxima == { - "get_recipe_section": (187_157, "remediation", "all_truthy"), - "load_recipe": (187_157, "remediation", "all_truthy"), - "open_kitchen": (187_211, "remediation", "all_truthy"), + "get_recipe_section": (187_151, "remediation", "all_truthy"), + "load_recipe": (187_151, "remediation", "all_truthy"), + "open_kitchen": (187_205, "remediation", "all_truthy"), }, ( "property=canonical baseline maxima; recipe=all; mode=all; tool=all; " f"ingredients_only=both; actual={maxima!r}; limit=measured current baseline" diff --git a/tests/skills/test_review_pr_verdict_guards.py b/tests/skills/test_review_pr_verdict_guards.py index 99be17c8a0..afd9c5fa43 100644 --- a/tests/skills/test_review_pr_verdict_guards.py +++ b/tests/skills/test_review_pr_verdict_guards.py @@ -290,3 +290,15 @@ def test_contract_yamls_include_approved_with_comments() -> None: f"{contract_path.name}: pattern_examples for review-pr must include " "'approved_with_comments'" ) + + +def test_review_gate_is_keyed_on_verdict_not_github_review_state() -> None: + text = _skill_text() + start = text.index("emit the review gate tag") + gate = text[start : text.index("Exit 0", start)] + assert "`verdict = approved`" in gate + assert "%%REVIEW_GATE::CLEAR%%" in gate + assert all( + term not in gate + for term in ("effective_event", "requested_event", "APPROVED", "COMMENTED", "receipt") + )