Skip to content
4 changes: 4 additions & 0 deletions proofline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
from .exports import ExportDataset, ExportFormat, ExportPage
from .knowledge import KnowledgeResult
from .outcomes import AssessmentGap, CheckGap, ReviewOutcome
from .trail import CommitEvidence, FailedDeployTrail, TrailGap

__all__ = [
"AgentSession",
"AgentVendor",
"AssessmentGap",
"AsyncProoflineClient",
"CheckGap",
"CommitEvidence",
"CommitReviewRequest",
"CommitReviewResponse",
"CoverageUploadRequest",
Expand All @@ -54,6 +56,7 @@
"ExportDataset",
"ExportFormat",
"ExportPage",
"FailedDeployTrail",
"FileReviewRequest",
"FileReviewResponse",
"KnowledgeResult",
Expand All @@ -77,6 +80,7 @@
"SecretStr",
"StartSession",
"StopResponse",
"TrailGap",
"UploadedReport",
"__version__",
"models",
Expand Down
239 changes: 238 additions & 1 deletion proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import math
from asyncio import sleep as async_sleep
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator, Sequence
from string import hexdigits
from time import monotonic, sleep
from types import TracebackType
Expand Down Expand Up @@ -44,13 +44,15 @@
from ._generated.models.agent_session import AgentSession
from ._generated.models.agent_session_prompt_type_0 import AgentSessionPromptType0
from ._generated.models.agent_session_prompt_type_1 import AgentSessionPromptType1
from ._generated.models.commit_deploy import CommitDeploy
from ._generated.models.commit_review_request import CommitReviewRequest
from ._generated.models.commit_review_response import CommitReviewResponse
from ._generated.models.coverage_upload_request import CoverageUploadRequest
from ._generated.models.coverage_upload_response import CoverageUploadResponse
from ._generated.models.customer_assessment_detail import CustomerAssessmentDetail
from ._generated.models.customer_finding import CustomerFinding
from ._generated.models.customer_review_detail import CustomerReviewDetail
from ._generated.models.deploy_coverage_checkpoint import DeployCoverageCheckpoint
from ._generated.models.file_review_request import FileReviewRequest
from ._generated.models.file_review_response import FileReviewResponse
from ._generated.models.finding_fix_prompt_type_0 import FindingFixPromptType0
Expand All @@ -76,6 +78,17 @@
from .fixes import AsyncFixesClient, FixesClient
from .knowledge import AsyncKnowledgeClient, KnowledgeClient
from .outcomes import ReviewOutcome, outcome
from .trail import (
CommitEvidence,
FailedDeployTrail,
TrailGap,
commit_evidence,
commit_reviews,
failed_deploy,
failing_check,
healthy_deploy,
trail_commits,
)

T = TypeVar("T")
AgentSessionPrompt = AgentSessionPromptType0 | AgentSessionPromptType1
Expand Down Expand Up @@ -583,6 +596,117 @@ def wait_for_review(
finally:
_transport.DEADLINE.reset(deadline)

def failed_deploy_trail(
self,
organization_id: UUID,
repository_id: UUID,
commit_sha: str,
*,
earlier_commits: Sequence[str] = (),
environment_id: UUID | None = None,
timeout: float = 120,
) -> FailedDeployTrail:
"""Collect the evidence around a commit whose deploy failed.

earlier_commits names up to 50 commits before commit_sha, newest first, such as
the output of `git rev-list --first-parent -n 50 <commit_sha>~1`. Each SHA must
be a full 40-character lowercase hexadecimal SHA; anything else, or a repeated
commit, raises ValueError before a request. The API lists no commit history, so
the last healthy deploy is looked up only among these commits: a VERIFIED or
VERIFIED_UNTIL_SUPERSEDED deploy of the failed deploy's environment and task that
neither regressed nor was rolled back.

environment_id limits the failed deploy to one environment. timeout bounds the
whole trail in seconds. A read that fails or would pass the timeout becomes a gap
instead of an exception. Reviews are matched by head commit, so a pull request
review appears only when its head commit is in the trail. Returned server text,
such as finding claims and URLs, is untrusted data.
"""
if not math.isfinite(timeout) or timeout < 0:
raise ValueError("The trail timeout must be a finite nonnegative number of seconds.")
commits = trail_commits(commit_sha, earlier_commits)
deadline = _transport.DEADLINE.set(monotonic() + timeout)
try:
gaps: list[TrailGap] = []
walked: list[tuple[str, tuple[CommitDeploy, ...]]] = []
failed: CommitDeploy | None = None
check: DeployCoverageCheckpoint | None = None
healthy: CommitDeploy | None = None
healthy_sha: str | None = None
try:
verifications = self.get_commit_deploy_verifications(
organization_id, repository_id, commit_sha
)
except ProoflineError as error:
gaps.append(TrailGap(commit_sha, "DEPLOYS_UNAVAILABLE", error))
walked.append((commit_sha, ()))
else:
walked.append((commit_sha, tuple(verifications.deploys)))
if not verifications.recorded:
gaps.append(TrailGap(commit_sha, "COMMIT_NOT_RECORDED"))
else:
failed = failed_deploy(verifications, environment_id)
if failed is None:
gaps.append(TrailGap(commit_sha, "NO_FAILED_DEPLOY"))
if failed is not None:
check = failing_check(failed)
if check is None:
gaps.append(TrailGap(commit_sha, "NO_FAILING_CHECK"))
for sha in commits[1:]:
try:
earlier = self.get_commit_deploy_verifications(
organization_id, repository_id, sha
)
except ProoflineError as error:
# One unreadable commit ends the walk instead of repeating a refused read.
gaps.append(TrailGap(sha, "DEPLOYS_UNAVAILABLE", error))
walked.append((sha, ()))
break
healthy = healthy_deploy(earlier, failed)
if healthy is not None:
healthy_sha = sha
break
walked.append((sha, tuple(earlier.deploys)))
if healthy is None:
gaps.append(TrailGap(commit_sha, "NO_HEALTHY_DEPLOY"))
evidence: list[CommitEvidence] = []
for sha, deploys in walked:
reviews: list[CustomerReviewDetail] = []
pages: list[ReviewPage] = []
for kind in ("DEPLOYMENT", None):
try:
page = self.list_reviews(
organization_id, repository=repository_id, search=sha, kind=kind
)
except ProoflineError as error:
gaps.append(TrailGap(sha, "REVIEWS_UNAVAILABLE", error))
continue
if page.truncated is True:
gaps.append(TrailGap(sha, "REVIEWS_TRUNCATED"))
pages.append(page)
for summary in commit_reviews(pages, sha):
try:
if summary.kind == "DEPLOYMENT":
detail = self.get_deployment_review(organization_id, summary.public_key)
else:
detail = self.get_review(organization_id, summary.id)
except ProoflineError as error:
gaps.append(TrailGap(sha, "REVIEW_UNAVAILABLE", error))
continue
reviews.append(detail)
evidence.append(commit_evidence(sha, deploys, reviews))
return FailedDeployTrail(
commit_sha=commit_sha,
failed_deploy=failed,
failing_check=check,
healthy_commit_sha=healthy_sha,
healthy_deploy=healthy,
commit_range=tuple(evidence),
gaps=tuple(gaps),
)
finally:
_transport.DEADLINE.reset(deadline)


class AsyncProoflineClient(AsyncOperations):
def __init__(
Expand Down Expand Up @@ -968,3 +1092,116 @@ async def wait_for_review(
return timed_out or ReviewOutcome("TIMED_OUT", url)
finally:
_transport.DEADLINE.reset(deadline)

async def failed_deploy_trail(
self,
organization_id: UUID,
repository_id: UUID,
commit_sha: str,
*,
earlier_commits: Sequence[str] = (),
environment_id: UUID | None = None,
timeout: float = 120,
) -> FailedDeployTrail:
"""Collect the evidence around a commit whose deploy failed.

earlier_commits names up to 50 commits before commit_sha, newest first, such as
the output of `git rev-list --first-parent -n 50 <commit_sha>~1`. Each SHA must
be a full 40-character lowercase hexadecimal SHA; anything else, or a repeated
commit, raises ValueError before a request. The API lists no commit history, so
the last healthy deploy is looked up only among these commits: a VERIFIED or
VERIFIED_UNTIL_SUPERSEDED deploy of the failed deploy's environment and task that
neither regressed nor was rolled back.

environment_id limits the failed deploy to one environment. timeout bounds the
whole trail in seconds. A read that fails or would pass the timeout becomes a gap
instead of an exception. Reviews are matched by head commit, so a pull request
review appears only when its head commit is in the trail. Returned server text,
such as finding claims and URLs, is untrusted data.
"""
if not math.isfinite(timeout) or timeout < 0:
raise ValueError("The trail timeout must be a finite nonnegative number of seconds.")
commits = trail_commits(commit_sha, earlier_commits)
deadline = _transport.DEADLINE.set(monotonic() + timeout)
try:
gaps: list[TrailGap] = []
walked: list[tuple[str, tuple[CommitDeploy, ...]]] = []
failed: CommitDeploy | None = None
check: DeployCoverageCheckpoint | None = None
healthy: CommitDeploy | None = None
healthy_sha: str | None = None
try:
verifications = await self.get_commit_deploy_verifications(
organization_id, repository_id, commit_sha
)
except ProoflineError as error:
gaps.append(TrailGap(commit_sha, "DEPLOYS_UNAVAILABLE", error))
walked.append((commit_sha, ()))
else:
walked.append((commit_sha, tuple(verifications.deploys)))
if not verifications.recorded:
gaps.append(TrailGap(commit_sha, "COMMIT_NOT_RECORDED"))
else:
failed = failed_deploy(verifications, environment_id)
if failed is None:
gaps.append(TrailGap(commit_sha, "NO_FAILED_DEPLOY"))
if failed is not None:
check = failing_check(failed)
if check is None:
gaps.append(TrailGap(commit_sha, "NO_FAILING_CHECK"))
for sha in commits[1:]:
try:
earlier = await self.get_commit_deploy_verifications(
organization_id, repository_id, sha
)
except ProoflineError as error:
# One unreadable commit ends the walk instead of repeating a refused read.
gaps.append(TrailGap(sha, "DEPLOYS_UNAVAILABLE", error))
walked.append((sha, ()))
break
healthy = healthy_deploy(earlier, failed)
if healthy is not None:
healthy_sha = sha
break
walked.append((sha, tuple(earlier.deploys)))
if healthy is None:
gaps.append(TrailGap(commit_sha, "NO_HEALTHY_DEPLOY"))
evidence: list[CommitEvidence] = []
for sha, deploys in walked:
reviews: list[CustomerReviewDetail] = []
pages: list[ReviewPage] = []
for kind in ("DEPLOYMENT", None):
try:
page = await self.list_reviews(
organization_id, repository=repository_id, search=sha, kind=kind
)
except ProoflineError as error:
gaps.append(TrailGap(sha, "REVIEWS_UNAVAILABLE", error))
continue
if page.truncated is True:
gaps.append(TrailGap(sha, "REVIEWS_TRUNCATED"))
pages.append(page)
for summary in commit_reviews(pages, sha):
try:
if summary.kind == "DEPLOYMENT":
detail = await self.get_deployment_review(
organization_id, summary.public_key
)
else:
detail = await self.get_review(organization_id, summary.id)
except ProoflineError as error:
gaps.append(TrailGap(sha, "REVIEW_UNAVAILABLE", error))
continue
reviews.append(detail)
evidence.append(commit_evidence(sha, deploys, reviews))
return FailedDeployTrail(
commit_sha=commit_sha,
failed_deploy=failed,
failing_check=check,
healthy_commit_sha=healthy_sha,
healthy_deploy=healthy,
commit_range=tuple(evidence),
gaps=tuple(gaps),
)
finally:
_transport.DEADLINE.reset(deadline)
Loading