From 97c1fb5e89b0a1286c0deb2a3fb42994c95f6b9b Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 10:43:56 -0700 Subject: [PATCH 01/13] fix: reconcile review receipts against reviewed-head comment evidence --- .../github_review/_poster_post_attempt.py | 21 +- .../github_review/_poster_reconcile.py | 61 ++--- .../github_review/_poster_support.py | 35 ++- .../execution/github_review/gateway.py | 18 +- tests/execution/github_review/fakes.py | 12 +- tests/execution/github_review/test_gateway.py | 155 ++++++++++++ tests/execution/github_review/test_poster.py | 171 +++++++++++++ .../github_review/test_resilience.py | 86 ++++++- tests/fixtures/github_rest.py | 61 +++++ .../test_review_receipt_production_seam.py | 234 +++++++++++++++++- 10 files changed, 798 insertions(+), 56 deletions(-) diff --git a/src/autoskillit/execution/github_review/_poster_post_attempt.py b/src/autoskillit/execution/github_review/_poster_post_attempt.py index f87f2c1664..e5156fe540 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="review comment marker association could not be verified", + ) 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,8 @@ 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.split("{digest}", 1)[0] 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..305fbe0e53 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, "review comment marker association could not be verified" + ) + 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..b1545c951c 100644 --- a/src/autoskillit/execution/github_review/_poster_support.py +++ b/src/autoskillit/execution/github_review/_poster_support.py @@ -134,16 +134,41 @@ 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]] = [] + prefix = FINDING_MARKER.split("{digest}", 1)[0] + 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 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/tests/execution/github_review/fakes.py b/tests/execution/github_review/fakes.py index 1549ab78ee..0b8f7ff5a3 100644 --- a/tests/execution/github_review/fakes.py +++ b/tests/execution/github_review/fakes.py @@ -171,11 +171,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 +186,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 +225,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..b6f627de98 100644 --- a/tests/execution/github_review/test_gateway.py +++ b/tests/execution/github_review/test_gateway.py @@ -5,6 +5,10 @@ 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] @@ -47,3 +51,154 @@ async def test_gateway_classifies_http_responses( 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() + review_id = target["pull_request_review_id"] + assert isinstance(review_id, int) + foreign_review = { + **pulls_comments_outdated_right(), + "pull_request_review_id": 88, + } + legacy_shape = { + "id": target["id"], + "pull_request_review_id": target["pull_request_review_id"], + "path": target["path"], + "position": 37, + "original_position": 37, + } + trace: list[tuple[str, str]] = [] + + def handler(request: httpx.Request) -> httpx.Response: + trace.append((request.method, str(request.url))) + if request.url.path.endswith(f"/reviews/{review_id}/comments"): + return httpx.Response(200, json=[legacy_shape]) + 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}") + + transport = httpx.MockTransport(handler) + gateway = DefaultGitHubReviewGateway( + token_factory=lambda: "test-token", + api_log=None, + client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), + ) + + 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}") + + transport = httpx.MockTransport(handler) + gateway = DefaultGitHubReviewGateway( + token_factory=lambda: "test-token", + api_log=None, + client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), + ) + + 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}") + + transport = httpx.MockTransport(handler) + gateway = DefaultGitHubReviewGateway( + token_factory=lambda: "test-token", + api_log=None, + client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), + ) + + 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..ca0666ea82 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,170 @@ 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)], + ) + gateway._next_review_id = int(remote["pull_request_review_id"]) + gateway._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", False), + (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() + 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) + # 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 + + recovered = await poster.post(request) + + 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] + remote["pull_request_review_id"] = 700 + # 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": "" FINDING_MARKER = "" +FINDING_MARKER_PREFIX = FINDING_MARKER.split("{digest}", 1)[0] @dataclass(frozen=True, slots=True) @@ -159,14 +160,13 @@ def comments_for_review_ids( comments: list[Any], review_ids: set[int] ) -> list[Mapping[str, Any]] | None: selected: list[Mapping[str, Any]] = [] - prefix = FINDING_MARKER.split("{digest}", 1)[0] 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 prefix in str(comment.get("body", "")): + elif review_id is None and FINDING_MARKER_PREFIX in str(comment.get("body", "")): return None return selected From 7caf754523f1fc85ba1f24337ac4242a55642ffe Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:33:26 -0700 Subject: [PATCH 04/13] fix(review): share unscopable marker diagnostic --- src/autoskillit/execution/github_review/_poster_post_attempt.py | 2 +- src/autoskillit/execution/github_review/_poster_reconcile.py | 2 +- src/autoskillit/execution/github_review/_poster_support.py | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/autoskillit/execution/github_review/_poster_post_attempt.py b/src/autoskillit/execution/github_review/_poster_post_attempt.py index c477c62233..459bafb710 100644 --- a/src/autoskillit/execution/github_review/_poster_post_attempt.py +++ b/src/autoskillit/execution/github_review/_poster_post_attempt.py @@ -108,7 +108,7 @@ async def _reconcile_remote_comment_set( if selected is None: return _poster_support.Reconciliation( ReviewReconciliationResult.UNCERTAIN, - error="review comment marker association could not be verified", + error=_poster_support.UNSCOPABLE_MARKER_ERROR, ) expected_comments = payload.get("comments") if not isinstance(expected_comments, list) or len(expected_comments) != len(findings): diff --git a/src/autoskillit/execution/github_review/_poster_reconcile.py b/src/autoskillit/execution/github_review/_poster_reconcile.py index 305fbe0e53..9972266271 100644 --- a/src/autoskillit/execution/github_review/_poster_reconcile.py +++ b/src/autoskillit/execution/github_review/_poster_reconcile.py @@ -231,7 +231,7 @@ async def scan_remote_findings( selected = _poster_support.comments_for_review_ids(comments.data, review_ids) if selected is None: return _poster_support.RemoteFindingScan( - False, found, "review comment marker association could not be verified" + False, found, _poster_support.UNSCOPABLE_MARKER_ERROR ) for comment in selected: digest = _poster_support.finding_marker_digest(str(comment.get("body", ""))) diff --git a/src/autoskillit/execution/github_review/_poster_support.py b/src/autoskillit/execution/github_review/_poster_support.py index b8f433fcb6..73560dc2d9 100644 --- a/src/autoskillit/execution/github_review/_poster_support.py +++ b/src/autoskillit/execution/github_review/_poster_support.py @@ -25,6 +25,7 @@ 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) From 27279f92c4f35036392735d0ddcb8d31fc5d7313 Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:34:46 -0700 Subject: [PATCH 05/13] test(review): remove redundant review association assignment --- tests/execution/github_review/test_poster.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/execution/github_review/test_poster.py b/tests/execution/github_review/test_poster.py index ca0666ea82..d445b94d84 100644 --- a/tests/execution/github_review/test_poster.py +++ b/tests/execution/github_review/test_poster.py @@ -173,7 +173,6 @@ async def test_exact_readback_scopes_review_before_marker_accounting( request = _request(tmp_path) first = await poster.post(request) remote = gateway.comments_by_review[700][0] - remote["pull_request_review_id"] = 700 # Synthetic associations isolate scoping from otherwise identical anchors. gateway.comments_by_review[700].extend( [ From af20bc84c45271ebe07d1f0766f839b6c9d1447d Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:35:49 -0700 Subject: [PATCH 06/13] test(review): require remote readback during pending recovery --- tests/execution/github_review/test_poster.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/execution/github_review/test_poster.py b/tests/execution/github_review/test_poster.py index d445b94d84..e1c169d59c 100644 --- a/tests/execution/github_review/test_poster.py +++ b/tests/execution/github_review/test_poster.py @@ -150,9 +150,11 @@ async def test_incomplete_historical_anchor_stays_pending( 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 From 45702129603a4a7a531a4447988053132b47581a Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:36:29 -0700 Subject: [PATCH 07/13] test(review): make boolean anchor cases detect numeric equality --- tests/execution/github_review/test_poster.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/execution/github_review/test_poster.py b/tests/execution/github_review/test_poster.py index e1c169d59c..b9589ba6e6 100644 --- a/tests/execution/github_review/test_poster.py +++ b/tests/execution/github_review/test_poster.py @@ -103,7 +103,7 @@ async def test_recovery_uses_observed_original_anchor( (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", False), + (pulls_comments_relocated_left, "original_start_line", True), (pulls_comments_relocated_left, "start_side", None), (pulls_comments_relocated_left, "start_side", "RIGHT"), ], @@ -115,6 +115,9 @@ async def test_incomplete_historical_anchor_stays_pending( value: object, ) -> None: remote = capture() + if value is True: + remote["original_line"] = 1 + remote["original_start_line"] = 1 request = _request( tmp_path, head_sha=remote["original_commit_id"], From 1177f851c50f95d36c49a57ad59729aba798a4d5 Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:37:13 -0700 Subject: [PATCH 08/13] test(review): seed captured IDs through fake initialization --- tests/execution/github_review/fakes.py | 6 ++++-- tests/execution/github_review/test_poster.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/execution/github_review/fakes.py b/tests/execution/github_review/fakes.py index 0b8f7ff5a3..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 diff --git a/tests/execution/github_review/test_poster.py b/tests/execution/github_review/test_poster.py index b9589ba6e6..60f72015a4 100644 --- a/tests/execution/github_review/test_poster.py +++ b/tests/execution/github_review/test_poster.py @@ -63,9 +63,9 @@ async def test_recovery_uses_observed_original_anchor( 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"]), ) - gateway._next_review_id = int(remote["pull_request_review_id"]) - gateway._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 From 21c15a80d27c11bab960cb1142ea3492a0260c1b Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:38:35 -0700 Subject: [PATCH 09/13] test(review): exercise shared production poster composition --- src/autoskillit/server/_factory.py | 26 ++++++++++++++++--- .../test_review_receipt_production_seam.py | 17 +++++------- 2 files changed, 29 insertions(+), 14 deletions(-) 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/server/test_review_receipt_production_seam.py b/tests/server/test_review_receipt_production_seam.py index 40eebdc8a0..72fdb83a92 100644 --- a/tests/server/test_review_receipt_production_seam.py +++ b/tests/server/test_review_receipt_production_seam.py @@ -16,9 +16,9 @@ from autoskillit.execution import ( DefaultGitHubReviewPoster, GitHubReviewLedger, - GitHubReviewMutationCoordinator, ) from autoskillit.execution.github_review.gateway import DefaultGitHubReviewGateway +from autoskillit.server._factory import _make_github_review_poster from autoskillit.server.tools.tools_pr_ops import post_pr_review, verify_review_receipt from autoskillit.smoke_utils import github_review_handoff_error from tests.execution.github_review.fakes import ( @@ -230,17 +230,12 @@ def fresh_poster() -> DefaultGitHubReviewPoster: api_log=None, client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), ) - ledger = GitHubReviewLedger(database_path) - return DefaultGitHubReviewPoster( - ledger=ledger, - coordinator=GitHubReviewMutationCoordinator( - ledger=ledger, - clock=clock, - sleeper=AdvancingSleeper(clock), - minimum_interval_seconds=1.0, - lease_ttl_seconds=60.0, - ), + return _make_github_review_poster( + ledger=GitHubReviewLedger(database_path), gateway=gateway, + review_comment_cap=tool_ctx_kitchen_open.config.github.review_comment_cap, + clock=clock, + sleeper=AdvancingSleeper(clock), wall_clock=clock, ) From a5e035e758bb36e17465b76db8a66e8586c8b96e Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:39:40 -0700 Subject: [PATCH 10/13] test(review): remove redundant unexpected request tracking --- tests/server/test_review_receipt_production_seam.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/server/test_review_receipt_production_seam.py b/tests/server/test_review_receipt_production_seam.py index 72fdb83a92..8df67818f5 100644 --- a/tests/server/test_review_receipt_production_seam.py +++ b/tests/server/test_review_receipt_production_seam.py @@ -157,7 +157,6 @@ async def test_real_gateway_recovers_pending_relocated_left_multiline_review( ] calls: list[tuple[str, str]] = [] - unexpected_calls: list[tuple[str, str]] = [] posted_payload: dict[str, Any] | None = None review_reads = 0 comment_reads = 0 @@ -217,7 +216,6 @@ def handle(request: httpx.Request) -> httpx.Response: remote_comment.pop("original_line") remote_comment.pop("original_start_line") return httpx.Response(200, json=[remote_comment]) - unexpected_calls.append((request.method, str(request.url))) raise AssertionError(f"unexpected GitHub request: {request.method} {request.url}") transport = httpx.MockTransport(handle) @@ -261,7 +259,6 @@ def fresh_poster() -> DefaultGitHubReviewPoster: ] expected_calls.extend([("GET", comments_url)] * initial_comment_reads) assert calls == expected_calls - assert unexpected_calls == [] assert pending["state"] == "COMMITTED_PENDING_VERIFICATION" assert pending["operation_key"] assert pending["receipt"] is None @@ -284,7 +281,6 @@ def fresh_poster() -> DefaultGitHubReviewPoster: ] ) assert calls == expected_calls - assert unexpected_calls == [] assert recovered["state"] == "SUCCEEDED" assert recovered["operation_key"] == pending["operation_key"] assert recovered["head_sha"] == old_head From be5e15d9567f32ba66b6fef8a6abf7e4add9121e Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:40:40 -0700 Subject: [PATCH 11/13] test(review): share HTTP gateway setup --- tests/execution/github_review/test_gateway.py | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/tests/execution/github_review/test_gateway.py b/tests/execution/github_review/test_gateway.py index b6f627de98..da8b487067 100644 --- a/tests/execution/github_review/test_gateway.py +++ b/tests/execution/github_review/test_gateway.py @@ -1,5 +1,7 @@ """HTTP response classification at the real GitHub review gateway boundary.""" +from collections.abc import Callable + import httpx import pytest @@ -13,6 +15,15 @@ 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", @@ -38,14 +49,7 @@ 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"}) @@ -94,12 +98,7 @@ def handler(request: httpx.Request) -> httpx.Response: ) raise AssertionError(f"unexpected request: {request.method} {request.url}") - transport = httpx.MockTransport(handler) - gateway = DefaultGitHubReviewGateway( - token_factory=lambda: "test-token", - api_log=None, - client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), - ) + gateway = _gateway(handler) result = await gateway.list_pull_review_comments("owner/repo", 1) @@ -142,12 +141,7 @@ def handler(request: httpx.Request) -> httpx.Response: ) raise AssertionError(f"unexpected request: {request.method} {request.url}") - transport = httpx.MockTransport(handler) - gateway = DefaultGitHubReviewGateway( - token_factory=lambda: "test-token", - api_log=None, - client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), - ) + gateway = _gateway(handler) result = await gateway.list_pull_review_comments("owner/repo", 1) @@ -185,12 +179,7 @@ def handler(request: httpx.Request) -> httpx.Response: ) raise AssertionError(f"unexpected request: {request.method} {request.url}") - transport = httpx.MockTransport(handler) - gateway = DefaultGitHubReviewGateway( - token_factory=lambda: "test-token", - api_log=None, - client_factory=lambda **kwargs: httpx.AsyncClient(transport=transport, **kwargs), - ) + gateway = _gateway(handler) result = await gateway.list_pull_review_comments("owner/repo", 1) From d3979d5dec2934dc38becd352b67c79c01fe7341 Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:41:21 -0700 Subject: [PATCH 12/13] test(review): remove unreachable legacy comment endpoint branch --- tests/execution/github_review/test_gateway.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tests/execution/github_review/test_gateway.py b/tests/execution/github_review/test_gateway.py index da8b487067..83516696da 100644 --- a/tests/execution/github_review/test_gateway.py +++ b/tests/execution/github_review/test_gateway.py @@ -60,25 +60,14 @@ async def test_gateway_classifies_http_responses( @pytest.mark.anyio async def test_review_comment_collection_uses_pr_wide_endpoint_and_paginates() -> None: target = pulls_comments_relocated_right() - review_id = target["pull_request_review_id"] - assert isinstance(review_id, int) foreign_review = { **pulls_comments_outdated_right(), "pull_request_review_id": 88, } - legacy_shape = { - "id": target["id"], - "pull_request_review_id": target["pull_request_review_id"], - "path": target["path"], - "position": 37, - "original_position": 37, - } trace: list[tuple[str, str]] = [] def handler(request: httpx.Request) -> httpx.Response: trace.append((request.method, str(request.url))) - if request.url.path.endswith(f"/reviews/{review_id}/comments"): - return httpx.Response(200, json=[legacy_shape]) if ( request.url.path.endswith("/pulls/1/comments") and request.url.params.get("page") == "2" From cfcfc8d9700ec2a71b39c57c442b4742dd1afc4a Mon Sep 17 00:00:00 2001 From: Trecek Date: Mon, 26 Oct 2026 13:52:28 -0700 Subject: [PATCH 13/13] fix(review): preserve single-line shape in boolean anchor fixture --- tests/execution/github_review/test_poster.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/execution/github_review/test_poster.py b/tests/execution/github_review/test_poster.py index 60f72015a4..c85803acbf 100644 --- a/tests/execution/github_review/test_poster.py +++ b/tests/execution/github_review/test_poster.py @@ -117,7 +117,8 @@ async def test_incomplete_historical_anchor_stays_pending( remote = capture() if value is True: remote["original_line"] = 1 - remote["original_start_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"], @@ -140,6 +141,7 @@ async def test_incomplete_historical_anchor_stays_pending( ) 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"],