diff --git a/src/autoskillit/execution/github_review/_poster_post_attempt.py b/src/autoskillit/execution/github_review/_poster_post_attempt.py index f87f2c1664..459bafb710 100644 --- a/src/autoskillit/execution/github_review/_poster_post_attempt.py +++ b/src/autoskillit/execution/github_review/_poster_post_attempt.py @@ -95,16 +95,21 @@ async def _reconcile_remote_comment_set( payload: Mapping[str, Any], findings: tuple[AdmittedFinding, ...], ) -> _poster_support.Reconciliation: - comments = await gateway.list_review_comments( + comments = await gateway.list_pull_review_comments( request.repository, request.pr_number, - review_id, ) if not comments.succeeded or not isinstance(comments.data, list): return _poster_support.Reconciliation( ReviewReconciliationResult.UNCERTAIN, error=comments.error or "could not read review comments", ) + selected = _poster_support.comments_for_review_ids(comments.data, {review_id}) + if selected is None: + return _poster_support.Reconciliation( + ReviewReconciliationResult.UNCERTAIN, + error=_poster_support.UNSCOPABLE_MARKER_ERROR, + ) expected_comments = payload.get("comments") if not isinstance(expected_comments, list) or len(expected_comments) != len(findings): return _poster_support.Reconciliation( @@ -121,9 +126,10 @@ async def _reconcile_remote_comment_set( ) candidates = [ item - for item in comments.data - if isinstance(item, Mapping) - and _poster_support.remote_comment_matches(item, expected, finding.digest) + for item in selected + if _poster_support.remote_comment_matches( + item, expected, finding.digest, reviewed_head_sha=request.head_sha + ) ] if len(candidates) != 1: return _poster_support.Reconciliation( @@ -139,9 +145,7 @@ async def _reconcile_remote_comment_set( used_remote_ids.add(comment_id) matched_ids.append((finding.canonical_index, comment_id)) marker_count = sum( - isinstance(item, Mapping) - and _poster_support.FINDING_MARKER.split("{digest}", 1)[0] in str(item.get("body", "")) - for item in comments.data + _poster_support.FINDING_MARKER_PREFIX in str(item.get("body", "")) for item in selected ) if marker_count != len(findings): return _poster_support.Reconciliation( diff --git a/src/autoskillit/execution/github_review/_poster_reconcile.py b/src/autoskillit/execution/github_review/_poster_reconcile.py index 47438af6eb..9972266271 100644 --- a/src/autoskillit/execution/github_review/_poster_reconcile.py +++ b/src/autoskillit/execution/github_review/_poster_reconcile.py @@ -11,7 +11,6 @@ from __future__ import annotations import json -from collections.abc import Mapping from typing import TYPE_CHECKING from autoskillit.core import ( @@ -210,38 +209,40 @@ async def scan_remote_findings( reviews.error or "could not list reviews for remote deduplication", ) found: dict[str, int] = {} - for review in reviews.data: - if ( - not isinstance(review, Mapping) - or _poster_support.nested_string(review, "user", "login") != authenticated_login - ): - continue - review_id = _poster_support.positive_int(review.get("id")) - if review_id is None: - return _poster_support.RemoteFindingScan(False, found, "review id was invalid") - comments = await poster.gateway.list_review_comments( - request.repository, - request.pr_number, - review_id, + authored = [ + review + for review in reviews.data + if _poster_support.nested_string(review, "user", "login") == authenticated_login + ] + if any(_poster_support.positive_int(review.get("id")) is None for review in authored): + return _poster_support.RemoteFindingScan(False, found, "review id was invalid") + review_ids = {review["id"] for review in authored} + if not review_ids: + return _poster_support.RemoteFindingScan(True, found) + comments = await poster.gateway.list_pull_review_comments( + request.repository, request.pr_number + ) + if not comments.succeeded or not isinstance(comments.data, list): + return _poster_support.RemoteFindingScan( + False, + found, + comments.error or "could not list comments for remote deduplication", ) - if not comments.succeeded or not isinstance(comments.data, list): + selected = _poster_support.comments_for_review_ids(comments.data, review_ids) + if selected is None: + return _poster_support.RemoteFindingScan( + False, found, _poster_support.UNSCOPABLE_MARKER_ERROR + ) + for comment in selected: + digest = _poster_support.finding_marker_digest(str(comment.get("body", ""))) + comment_id = _poster_support.positive_int(comment.get("id")) + if digest is None or comment_id is None: + continue + previous = found.setdefault(digest, comment_id) + if previous != comment_id: return _poster_support.RemoteFindingScan( False, found, - comments.error or "could not list comments for remote deduplication", + "duplicate remote finding markers made deduplication ambiguous", ) - for comment in comments.data: - if not isinstance(comment, Mapping): - continue - digest = _poster_support.finding_marker_digest(str(comment.get("body", ""))) - comment_id = _poster_support.positive_int(comment.get("id")) - if digest is None or comment_id is None: - continue - previous = found.setdefault(digest, comment_id) - if previous != comment_id: - return _poster_support.RemoteFindingScan( - False, - found, - "duplicate remote finding markers made deduplication ambiguous", - ) return _poster_support.RemoteFindingScan(True, found) diff --git a/src/autoskillit/execution/github_review/_poster_support.py b/src/autoskillit/execution/github_review/_poster_support.py index 6c1a858d09..73560dc2d9 100644 --- a/src/autoskillit/execution/github_review/_poster_support.py +++ b/src/autoskillit/execution/github_review/_poster_support.py @@ -24,6 +24,8 @@ OPERATION_MARKER = "" FINDING_MARKER = "" +FINDING_MARKER_PREFIX = FINDING_MARKER.split("{digest}", 1)[0] +UNSCOPABLE_MARKER_ERROR = "review comment marker association could not be verified" @dataclass(frozen=True, slots=True) @@ -134,16 +136,40 @@ def remote_comment_matches( remote: Mapping[str, Any], expected: Mapping[str, Any], finding_digest: str, + *, + reviewed_head_sha: str, ) -> bool: if remote.get("body") != expected.get("body"): return False if finding_marker_digest(str(remote.get("body", ""))) != finding_digest: return False - for key in ("path", "line", "side", "start_line", "start_side"): - expected_value = expected.get(key) - if expected_value is not None and remote.get(key) != expected_value: - return False - return True + if ( + remote.get("path") != expected["path"] + or remote.get("original_commit_id") != reviewed_head_sha + or positive_int(remote.get("original_line")) != expected["line"] + or "original_start_line" not in remote + or remote.get("side") != expected["side"] + or remote.get("start_side") != expected.get("start_side") + ): + return False + if expected.get("start_line") is None: + return remote["original_start_line"] is None + return positive_int(remote["original_start_line"]) == expected["start_line"] + + +def comments_for_review_ids( + comments: list[Any], review_ids: set[int] +) -> list[Mapping[str, Any]] | None: + selected: list[Mapping[str, Any]] = [] + for comment in comments: + if not isinstance(comment, Mapping): + continue + review_id = positive_int(comment.get("pull_request_review_id")) + if review_id in review_ids: + selected.append(comment) + elif review_id is None and FINDING_MARKER_PREFIX in str(comment.get("body", "")): + return None + return selected def finding_marker_digest(body: str) -> str | None: diff --git a/src/autoskillit/execution/github_review/gateway.py b/src/autoskillit/execution/github_review/gateway.py index 7f73f66f63..6ebe3cab22 100644 --- a/src/autoskillit/execution/github_review/gateway.py +++ b/src/autoskillit/execution/github_review/gateway.py @@ -81,15 +81,12 @@ async def create_review( async def list_reviews(self, repository: str, pr_number: int) -> GatewayResult: return await self._paginate(f"/repos/{repository}/pulls/{pr_number}/reviews?per_page=100") - async def list_review_comments( + async def list_pull_review_comments( self, repository: str, pr_number: int, - review_id: int, ) -> GatewayResult: - return await self._paginate( - f"/repos/{repository}/pulls/{pr_number}/reviews/{review_id}/comments?per_page=100" - ) + return await self._paginate(f"/repos/{repository}/pulls/{pr_number}/comments?per_page=100") async def _paginate(self, path: str) -> GatewayResult: items: list[Any] = [] @@ -109,7 +106,16 @@ async def _paginate(self, path: str) -> GatewayResult: error="GitHub pagination response was not a list", ) items.extend(response.data) - next_path = self._next_page_path(response.headers.get("link")) + try: + next_path = self._next_page_path(response.headers.get("link")) + except ValueError as exc: + return GatewayResult( + status_code=response.status_code, + data=None, + headers=response.headers, + response_class=ReviewResponseClass.CLIENT_ERROR, + error=str(exc), + ) return GatewayResult( status_code=200, data=items, diff --git a/src/autoskillit/server/_factory.py b/src/autoskillit/server/_factory.py index 097a9ce30c..b75a9aa457 100644 --- a/src/autoskillit/server/_factory.py +++ b/src/autoskillit/server/_factory.py @@ -9,9 +9,11 @@ from __future__ import annotations +import asyncio import os import subprocess -from collections.abc import Callable +import time +from collections.abc import Awaitable, Callable from pathlib import Path from typing import Any @@ -121,6 +123,25 @@ logger = get_logger(__name__) +def _make_github_review_poster( + *, + ledger: GitHubReviewLedger, + gateway: DefaultGitHubReviewGateway, + review_comment_cap: int = 50, + clock: Callable[[], float] = time.monotonic, + sleeper: Callable[[float], Awaitable[None]] = asyncio.sleep, + wall_clock: Callable[[], float] = time.time, +) -> DefaultGitHubReviewPoster: + """Compose review publication with one ledger for receipts and mutation pacing.""" + return DefaultGitHubReviewPoster( + ledger=ledger, + coordinator=GitHubReviewMutationCoordinator(ledger=ledger, clock=clock, sleeper=sleeper), + gateway=gateway, + review_comment_cap=review_comment_cap, + wall_clock=wall_clock, + ) + + def make_recipe_execution( *, snapshot: RecipeExecutionSnapshot, @@ -437,9 +458,8 @@ def make_context( token_factory=token_factory, api_log=github_api_log, ) - github_review_poster = DefaultGitHubReviewPoster( + github_review_poster = _make_github_review_poster( ledger=github_review_ledger, - coordinator=GitHubReviewMutationCoordinator(ledger=github_review_ledger), gateway=github_review_gateway, review_comment_cap=config.github.review_comment_cap, ) diff --git a/tests/execution/github_review/fakes.py b/tests/execution/github_review/fakes.py index 1549ab78ee..cc61a792ae 100644 --- a/tests/execution/github_review/fakes.py +++ b/tests/execution/github_review/fakes.py @@ -76,6 +76,8 @@ def __init__( head_sha: str = "a" * 40, authenticated_login: str = "autoskillit-bot", pr_author_login: str = "pull-author", + next_review_id: int = 700, + next_comment_id: int = 900, ) -> None: self.clock = clock self.outcomes = list(outcomes or [CreateOutcome(200, commit=True)]) @@ -93,8 +95,8 @@ def __init__( self.review_read_calls = 0 self.comment_read_calls = 0 self.fail_reads = False - self._next_review_id = 700 - self._next_comment_id = 900 + self._next_review_id = next_review_id + self._next_comment_id = next_comment_id async def scope_material(self) -> CredentialScopeMaterial: self.scope_calls += 1 @@ -171,11 +173,10 @@ async def list_reviews(self, repository: str, pr_number: int) -> GatewayResult: ) return _success(list(self.reviews)) - async def list_review_comments( + async def list_pull_review_comments( self, repository: str, pr_number: int, - review_id: int, ) -> GatewayResult: del repository, pr_number self.comment_read_calls += 1 @@ -187,7 +188,9 @@ async def list_review_comments( response_class=ReviewResponseClass.TRANSPORT_ERROR, error="injected comment read failure", ) - return _success(list(self.comments_by_review.get(review_id, []))) + return _success( + [comment for comments in self.comments_by_review.values() for comment in comments] + ) async def create_review_comment( self, @@ -224,6 +227,11 @@ def _commit(self, payload: dict[str, Any]) -> int: { **comment, "id": self._next_comment_id, + "pull_request_review_id": review_id, + "commit_id": payload["commit_id"], + "original_commit_id": payload["commit_id"], + "original_line": comment["line"], + "original_start_line": comment.get("start_line"), } ) self._next_comment_id += 1 diff --git a/tests/execution/github_review/test_gateway.py b/tests/execution/github_review/test_gateway.py index 8746cc39dd..83516696da 100644 --- a/tests/execution/github_review/test_gateway.py +++ b/tests/execution/github_review/test_gateway.py @@ -1,14 +1,29 @@ """HTTP response classification at the real GitHub review gateway boundary.""" +from collections.abc import Callable + import httpx import pytest from autoskillit.core import ReviewResponseClass from autoskillit.execution.github_review.gateway import DefaultGitHubReviewGateway +from tests.fixtures.github_rest import ( + pulls_comments_outdated_right, + pulls_comments_relocated_right, +) pytestmark = [pytest.mark.layer("execution"), pytest.mark.small] +def _gateway(handler: Callable[[httpx.Request], httpx.Response]) -> DefaultGitHubReviewGateway: + transport = httpx.MockTransport(handler) + return DefaultGitHubReviewGateway( + token_factory=lambda: "test-token", + api_log=None, + client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), + ) + + @pytest.mark.anyio @pytest.mark.parametrize( "status,message,expected", @@ -34,16 +49,134 @@ 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), - ) + gateway = _gateway(lambda request: httpx.Response(status, json={"message": message})) result = await gateway.create_review("owner/repo", 1, {"event": "COMMENT", "body": "Review"}) assert result.status_code == status assert result.response_class is expected + + +@pytest.mark.anyio +async def test_review_comment_collection_uses_pr_wide_endpoint_and_paginates() -> None: + target = pulls_comments_relocated_right() + foreign_review = { + **pulls_comments_outdated_right(), + "pull_request_review_id": 88, + } + trace: list[tuple[str, str]] = [] + + def handler(request: httpx.Request) -> httpx.Response: + trace.append((request.method, str(request.url))) + if ( + request.url.path.endswith("/pulls/1/comments") + and request.url.params.get("page") == "2" + ): + return httpx.Response(200, json=[target], headers={"x-page": "2"}) + if request.url.path.endswith("/pulls/1/comments"): + return httpx.Response( + 200, + json=[foreign_review], + headers={ + "link": ( + "; rel="next"' + ), + "x-page": "1", + }, + ) + raise AssertionError(f"unexpected request: {request.method} {request.url}") + + gateway = _gateway(handler) + + result = await gateway.list_pull_review_comments("owner/repo", 1) + + assert result.succeeded + assert result.data == [foreign_review, target] + assert trace == [ + ("GET", "https://api.github.com/repos/owner/repo/pulls/1/comments?per_page=100"), + ( + "GET", + "https://api.github.com/repos/owner/repo/pulls/1/comments?per_page=100&page=2", + ), + ] + + +@pytest.mark.anyio +async def test_review_comment_collection_returns_later_page_http_failure() -> None: + trace: list[tuple[str, str]] = [] + + def handler(request: httpx.Request) -> httpx.Response: + trace.append((request.method, str(request.url))) + if ( + request.url.path.endswith("/pulls/1/comments") + and request.url.params.get("page") == "2" + ): + return httpx.Response( + 502, + json={"message": "Bad Gateway"}, + headers={"x-page": "2"}, + ) + if request.url.path.endswith("/pulls/1/comments"): + return httpx.Response( + 200, + json=[pulls_comments_relocated_right()], + headers={ + "link": ( + "; rel="next"' + ) + }, + ) + raise AssertionError(f"unexpected request: {request.method} {request.url}") + + gateway = _gateway(handler) + + result = await gateway.list_pull_review_comments("owner/repo", 1) + + assert result.status_code == 502 + assert result.response_class is ReviewResponseClass.SERVER_ERROR + assert result.data == {"message": "Bad Gateway"} + assert result.headers["x-page"] == "2" + assert trace == [ + ("GET", "https://api.github.com/repos/owner/repo/pulls/1/comments?per_page=100"), + ( + "GET", + "https://api.github.com/repos/owner/repo/pulls/1/comments?per_page=100&page=2", + ), + ] + + +@pytest.mark.anyio +@pytest.mark.parametrize( + "link", + [ + 'https://api.github.com/repos/owner/repo/pulls/1/comments?page=2; rel="next"', + '; rel="next"', + ], +) +async def test_review_comment_collection_rejects_unsafe_next_link(link: str) -> None: + trace: list[tuple[str, str]] = [] + + def handler(request: httpx.Request) -> httpx.Response: + trace.append((request.method, str(request.url))) + if request.url.path.endswith("/pulls/1/comments"): + return httpx.Response( + 200, + json=[pulls_comments_relocated_right()], + headers={"link": link, "x-page": "1"}, + ) + raise AssertionError(f"unexpected request: {request.method} {request.url}") + + gateway = _gateway(handler) + + result = await gateway.list_pull_review_comments("owner/repo", 1) + + assert result.status_code == 200 + assert result.response_class is ReviewResponseClass.CLIENT_ERROR + assert result.data is None + assert result.headers["x-page"] == "1" + assert result.error + assert trace == [ + ("GET", "https://api.github.com/repos/owner/repo/pulls/1/comments?per_page=100") + ] diff --git a/tests/execution/github_review/test_poster.py b/tests/execution/github_review/test_poster.py index 509e50e08e..c85803acbf 100644 --- a/tests/execution/github_review/test_poster.py +++ b/tests/execution/github_review/test_poster.py @@ -5,7 +5,9 @@ import asyncio import hashlib import json +from collections.abc import Callable from pathlib import Path +from typing import Any from unittest.mock import AsyncMock import pytest @@ -16,6 +18,11 @@ ReviewOperationState, ) from autoskillit.execution import GitHubReviewLedger +from tests.fixtures.github_rest import ( + pulls_comments_outdated_right, + pulls_comments_relocated_left, + pulls_comments_relocated_right, +) from .fakes import ( CreateOutcome, @@ -28,6 +35,176 @@ pytestmark = [pytest.mark.layer("execution"), pytest.mark.small] +@pytest.mark.anyio +@pytest.mark.parametrize( + "capture", + [pulls_comments_relocated_right, pulls_comments_outdated_right, pulls_comments_relocated_left], +) +async def test_recovery_uses_observed_original_anchor( + tmp_path: Path, capture: Callable[[], dict[str, Any]] +) -> None: + remote = capture() + request = _request( + tmp_path, + head_sha=remote["original_commit_id"], + comments=( + GitHubReviewComment( + path=remote["path"], + line=remote["original_line"], + side=remote["side"], + start_line=remote["original_start_line"], + start_side=remote["start_side"], + body="Finding", + ), + ), + ) + clock = ManualClock() + gateway = StatefulReviewGateway( + clock=clock, + head_sha=request.head_sha, + outcomes=[CreateOutcome(200, commit=True, fail_reads_after=True)], + next_review_id=int(remote["pull_request_review_id"]), + next_comment_id=int(remote["id"]), + ) + poster = _poster(tmp_path / "ledger.sqlite3", gateway, clock) + first = await poster.post(request) + assert first.state is ReviewOperationState.COMMITTED_PENDING_VERIFICATION + assert first.receipt is None + # Only the finding body is synthetic; captured coordinates remain intact. + remote["body"] = gateway.create_calls[0]["comments"][0]["body"] + gateway.comments_by_review[int(remote["pull_request_review_id"])] = [remote] + gateway.head_sha = str(remote["commit_id"]) + gateway.fail_reads = False + pull_calls = gateway.pull_calls + + recovered = await poster.post(request) + + assert recovered.state is ReviewOperationState.SUCCEEDED + assert recovered.operation_key == first.operation_key + assert recovered.comment_ids == (remote["id"],) + assert recovered.receipt is not None + assert recovered.receipt.head_sha == request.head_sha + assert recovered.executed_mutation_count == 0 + assert len(gateway.create_calls) == 1 + assert gateway.pull_calls == pull_calls + + +@pytest.mark.anyio +@pytest.mark.parametrize( + "capture,field,value", + [ + (pulls_comments_relocated_right, "original_commit_id", "b" * 40), + (pulls_comments_relocated_right, "original_line", None), + (pulls_comments_relocated_right, "original_line", True), + (pulls_comments_relocated_right, "original_start_line", None), + (pulls_comments_relocated_right, "original_start_line", 123), + (pulls_comments_relocated_right, "side", None), + (pulls_comments_relocated_right, "side", "LEFT"), + (pulls_comments_relocated_right, "start_side", "RIGHT"), + (pulls_comments_relocated_left, "original_start_line", None), + (pulls_comments_relocated_left, "original_start_line", 12), + (pulls_comments_relocated_left, "original_start_line", True), + (pulls_comments_relocated_left, "start_side", None), + (pulls_comments_relocated_left, "start_side", "RIGHT"), + ], +) +async def test_incomplete_historical_anchor_stays_pending( + tmp_path: Path, + capture: Callable[[], dict[str, Any]], + field: str, + value: object, +) -> None: + remote = capture() + if value is True: + remote["original_line"] = 1 + if remote["original_start_line"] is not None: + remote["original_start_line"] = 1 + request = _request( + tmp_path, + head_sha=remote["original_commit_id"], + comments=( + GitHubReviewComment( + path=remote["path"], + line=remote["original_line"], + side=remote["side"], + start_line=remote["original_start_line"], + start_side=remote["start_side"], + body="Finding", + ), + ), + ) + clock = ManualClock() + gateway = StatefulReviewGateway( + clock=clock, + head_sha=request.head_sha, + outcomes=[CreateOutcome(200, commit=True, fail_reads_after=True)], + ) + poster = _poster(tmp_path / "ledger.sqlite3", gateway, clock) + first = await poster.post(request) + assert first.state is ReviewOperationState.COMMITTED_PENDING_VERIFICATION + # Synthetic negatives make current coordinates coincide with the submission. + remote.update( + body=gateway.create_calls[0]["comments"][0]["body"], + pull_request_review_id=700, + line=remote["original_line"], + start_line=remote["original_start_line"], + ) + if value is None: + del remote[field] + else: + remote[field] = value + gateway.comments_by_review[700] = [remote] + gateway.fail_reads = False + comment_reads = gateway.comment_read_calls + + recovered = await poster.post(request) + + assert gateway.comment_read_calls > comment_reads + assert recovered.state is ReviewOperationState.COMMITTED_PENDING_VERIFICATION + assert recovered.operation_key == first.operation_key + assert recovered.receipt is None + assert recovered.executed_mutation_count == 0 + assert len(gateway.create_calls) == 1 + + +@pytest.mark.anyio +@pytest.mark.parametrize("unscopable_marker", [False, True]) +async def test_exact_readback_scopes_review_before_marker_accounting( + tmp_path: Path, unscopable_marker: bool +) -> None: + clock = ManualClock() + gateway = StatefulReviewGateway( + clock=clock, outcomes=[CreateOutcome(200, commit=True, fail_reads_after=True)] + ) + poster = _poster(tmp_path / "ledger.sqlite3", gateway, clock) + request = _request(tmp_path) + first = await poster.post(request) + remote = gateway.comments_by_review[700][0] + # Synthetic associations isolate scoping from otherwise identical anchors. + gateway.comments_by_review[700].extend( + [ + {**remote, "id": 901, "pull_request_review_id": 701}, + {"id": 902, "pull_request_review_id": None, "body": "Unrelated"}, + ] + ) + if unscopable_marker: + gateway.comments_by_review[700].append( + {"id": 903, "pull_request_review_id": None, "body": remote["body"]} + ) + gateway.fail_reads = False + + recovered = await poster.post(request) + + assert recovered.operation_key == first.operation_key + assert len(gateway.create_calls) == 1 + if unscopable_marker: + assert recovered.state is ReviewOperationState.COMMITTED_PENDING_VERIFICATION + assert recovered.receipt is None + else: + assert recovered.state is ReviewOperationState.SUCCEEDED + assert recovered.comment_ids == (900,) + + @pytest.mark.anyio async def test_unexpected_exception_returns_ambiguous_result( monkeypatch: pytest.MonkeyPatch, diff --git a/tests/execution/github_review/test_resilience.py b/tests/execution/github_review/test_resilience.py index b429b78acf..2cdeedaa3f 100644 --- a/tests/execution/github_review/test_resilience.py +++ b/tests/execution/github_review/test_resilience.py @@ -18,7 +18,7 @@ ReviewResponseClass, ) from autoskillit.execution import GitHubReviewLedger, compute_review_operation_key -from autoskillit.execution.github_review import _poster_support +from autoskillit.execution.github_review import _poster_reconcile, _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 ( @@ -637,6 +637,90 @@ async def test_crash_after_reduced_batch_reconciles_persisted_subset_without_rep ) +@pytest.mark.anyio +async def test_remote_finding_scan_collects_multiple_authored_reviews_once( + tmp_path: Path, +) -> None: + clock = ManualClock() + gateway = StatefulReviewGateway(clock=clock) + # PR #4427 associations are observed; marker bodies and the foreign author are synthetic. + gateway.reviews.extend( + [ + {"id": 4824396371, "user": {"login": "Trecek"}}, + {"id": 4824397786, "user": {"login": "Trecek"}}, + {"id": 4824432714, "user": {"login": "other-reviewer"}}, + ] + ) + first_digest = "1" * 64 + second_digest = "2" * 64 + foreign_digest = "3" * 64 + gateway.comments_by_review.update( + { + 4824396371: [ + { + "id": 3687217371, + "pull_request_review_id": 4824396371, + "body": _poster_support.FINDING_MARKER.format(digest=first_digest), + } + ], + 4824397786: [ + { + "id": 3687218450, + "pull_request_review_id": 4824397786, + "body": _poster_support.FINDING_MARKER.format(digest=second_digest), + } + ], + 4824432714: [ + { + "id": 3687243187, + "pull_request_review_id": 4824432714, + "body": _poster_support.FINDING_MARKER.format(digest=foreign_digest), + } + ], + } + ) + poster = _poster(tmp_path / "ledger.sqlite3", gateway, clock) + + scan = await _poster_reconcile.scan_remote_findings( + poster, + request=_request(tmp_path), + authenticated_login="Trecek", + ) + + assert scan.certain + assert scan.ids_by_digest == {first_digest: 3687217371, second_digest: 3687218450} + assert gateway.review_read_calls == 1 + assert gateway.comment_read_calls == 1 + + +@pytest.mark.anyio +async def test_remote_finding_scan_rejects_unscopable_marker_prefix( + tmp_path: Path, +) -> None: + clock = ManualClock() + gateway = StatefulReviewGateway(clock=clock) + gateway.reviews.append({"id": 700, "user": {"login": "autoskillit-bot"}}) + gateway.comments_by_review[700] = [ + { + "id": 900, + "body": "