Dedupe and idempotency
MCP tools get retried. A client might call post_review_comments after a timeout, or a user might run the review twice. Without idempotency, every retry stacks another review on the PR. The answer is a small signature in the review body that lets you skip posting if a matching review already exists.
import hashlib
def _review_signature(comments: list[ReviewComment], summary: str) -> str:
"""Stable fingerprint for a review payload."""
body = summary + "|" + "|".join(
f"{c.file}:{c.line}:{c.severity}:{c.suggestion}" for c in comments
)
return hashlib.sha256(body.encode()).hexdigest()[:16]
def post_review_idempotent(owner, repo, number, comments, summary):
sig = _review_signature(comments, summary)
marker = f"<!-- review-sig:{sig} -->"
gh = _client()
pr = gh.get_repo(f"{owner}/{repo}").get_pull(number)
for existing in pr.get_reviews():
if existing.body and marker in existing.body:
return {"status": "skipped", "review_id": existing.id}
body = f"{summary}\n\n{marker}" if summary else marker
return post_review(owner, repo, number, comments, body)The signature is a short SHA-256 hash of every comment plus the summary. A marker comment hides it in the review body. Before posting, the function scans existing reviews for the same marker. If it finds one, it returns skipped instead of posting again.
Ordering exercise: Order the idempotent write-back steps
Loading practice…
Most PRs have fewer than a dozen reviews. Fetching the list is one request and returns quickly. The alternative (posting duplicates on every retry) produces notification spam that annoys humans and drains trust in the bot. Cheap insurance for a very common failure mode.
Quiz: Quiz
Loading practice…
Checkpoint: Write-back checkpoint
Loading practice…