diff --git a/README.md b/README.md index e3a927b..cabd10b 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,17 @@ When no token is passed, the client reads `PROOFLINE_TOKEN`, keeps it in memory, This repository is the published distribution of the Proofline TypeScript SDK. The public OpenAPI contract in `openapi.public.json` generates `src/operations.ts` and `src/schema.d.ts`. The transport in `src/transport.ts`, client helpers, and tests in `tests/` are hand-written. Open an [issue](https://github.com/prooflinedev/sdk-typescript/issues) for changes to the OpenAPI contract, generated code, or workflows. Pull requests are welcome for the hand-written parts. +## Review snapshots + +`getReviewSnapshot` reads a review once and reports the same status and gaps as `waitForReview`, without polling. Use it in a status page or an editor panel that refreshes on its own schedule. An active review reports `PENDING` or `RUNNING`. A settled review reports `COMPLETED`, `INCOMPLETE`, `FAILED` or `STOPPED`, with the assessment gaps that explain an incomplete result. The read uses the normal request timeout and retries; it never returns `TIMED_OUT`. + +```typescript +const snapshot = await client.getReviewSnapshot(organizationId, reviewId); +console.log(snapshot.status); +for (const gap of snapshot.gaps) + console.log(gap.assessmentId, gap.reasonCodes.join(", ")); +``` + ## Errors and refusals A refused request throws `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repositoryName`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. The token is redacted from every text field, and `String(error)`, `inspect(error)` and `JSON.stringify(error)` show only the status. diff --git a/src/index.ts b/src/index.ts index 4d4b633..4b5bebd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -77,6 +77,18 @@ export interface WaitOptions { timeoutMs?: number; } +/** A snapshot never times out on its own; a single read uses the request timeout. */ +export type SnapshotStatus = + | "PENDING" + | "RUNNING" + | Exclude; + +export interface ReviewSnapshot + extends Omit { + status: SnapshotStatus; + review: Review; +} + export class ProoflineClient extends Operations { readonly #transport: Transport; readonly mcp: McpClient; @@ -265,24 +277,46 @@ export class ProoflineClient extends Operations { validateReview(review, reviewId); const status = terminal(review); if (status !== null) - return outcome( - status, - url, - review, - status === "INCOMPLETE" - ? review.assessment_count === 0 && review.assessments.length === 0 - ? "the code assessment is unavailable or incomplete" - : review.status !== "INCOMPLETE" - ? "one or more assessment results are unavailable or unknown" - : null - : null, - ); + return outcome(status, url, review, settledReason(review, status)); await sleep( Math.min(interval, Math.max(0, deadline - performance.now())), ); interval = Math.min(interval * 2, 60_000); } } + + /** + * Reads a review once and reports the same status and gaps as `waitForReview`, + * without polling. An active review reports `PENDING` or `RUNNING`. + */ + async getReviewSnapshot( + orgId: string, + reviewId: string, + ): Promise { + const url = this.#transport.reviewUrl(orgId, reviewId); + const review = await this.#transport.request( + "GET", + "/api/v1/organizations/{org_id}/reviews/{review_id}", + { path: { org_id: orgId, review_id: reviewId } }, + ); + validateReview(review, reviewId); + const settled = terminal(review); + // An active review is still in progress; its unfinished assessments are + // normal, not gaps. Report no gaps until the review settles. + if (settled === null) { + return { + status: review.status as "PENDING" | "RUNNING", + reviewUrl: url, + review, + gaps: [], + reason: null, + }; + } + return { + ...outcome(settled, url, review, settledReason(review, settled)), + review, + }; + } } import { ProoflineError } from "./transport.js"; @@ -399,7 +433,16 @@ function checkGap(assessment: Assessment): boolean { ); } -function terminal(review: Review): ReviewStatus | null { +function settledReason(review: Review, status: ReviewStatus): string | null { + if (status !== "INCOMPLETE") return null; + if (review.assessment_count === 0 && review.assessments.length === 0) + return "the code assessment is unavailable or incomplete"; + return review.status !== "INCOMPLETE" + ? "one or more assessment results are unavailable or unknown" + : null; +} + +function terminal(review: Review): Exclude | null { if (review.status === "PENDING" || review.status === "RUNNING") return null; if (review.status === "SUPERSEDED") return "STOPPED"; if ( @@ -499,12 +542,12 @@ function assessmentReasons(assessment: Assessment): string[] { return reasons; } -function outcome( - status: ReviewStatus, +function outcome( + status: S, reviewUrl: string, review: Review | null, reason: string | null, -): ReviewOutcome { +): Omit & { status: S } { const gaps: AssessmentGaps[] = (review?.assessments ?? []) .filter( (assessment) => diff --git a/tests/http.test.mjs b/tests/http.test.mjs index 8a1e5e7..5d659a5 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -2176,6 +2176,70 @@ test("shared review outcome contract", async (t) => { } }); +test("a review snapshot reports the wait outcome from one read, without polling", async (t) => { + const contract = JSON.parse( + await readFile( + new URL("./fixtures/review-outcomes.json", import.meta.url), + "utf8", + ), + ); + let body; + let requests; + const fixture = await server(t, (_, response) => { + requests += 1; + json(response, 200, body); + }); + const client = new ProoflineClient({ baseUrl: fixture.baseUrl, token }); + for (const item of contract.cases) { + body = item.review; + requests = 0; + const snapshot = await client.getReviewSnapshot(orgId, body.id); + assert.equal(requests, 1, item.name); + assert.equal( + snapshot.reviewUrl, + `${fixture.baseUrl}/orgs/by-id/${orgId}/reviews/${body.id}`, + item.name, + ); + assert.deepEqual(snapshot.review, body, item.name); + assert.equal( + snapshot.status, + item.outcome === null ? body.status : item.outcome, + item.name, + ); + if (item.outcome === null) { + // An active review reports no gaps and no reason; progress is not a gap. + assert.deepEqual(snapshot.gaps, [], item.name); + assert.equal(snapshot.reason, null, item.name); + } else { + assert.deepEqual( + snapshot.gaps.map((gap) => ({ + assessment_id: gap.assessmentId || null, + reason_codes: gap.reasonCodes, + })), + item.gaps, + item.name, + ); + const expectedReason = + item.outcome === "INCOMPLETE" + ? body.assessment_count === 0 && body.assessments.length === 0 + ? "the code assessment is unavailable or incomplete" + : body.status !== "INCOMPLETE" + ? "one or more assessment results are unavailable or unknown" + : null + : null; + assert.equal(snapshot.reason, expectedReason, item.name); + } + } + + body = { ...review(), id: "another-review" }; + await assert.rejects( + client.getReviewSnapshot(orgId, reviewId), + (error) => + error instanceof ProoflineError && + error.type === "urn:proofline:sdk:invalid-response", + ); +}); + test("MCP helpers isolate per-call organizations and preserve refusals", async (t) => { const first = orgId; const second = "00000000-0000-7000-8000-000000000006";