Skip to content

Expose official SDK LLM retry attempts in review output #785

Description

@Gongyl01

Problem Statement

Part of #368

OCR's Anthropic, OpenAI Chat Completions, and OpenAI Responses clients all use the official SDK retry loops with WithMaxRetries(5). The SDKs retry eligible connection failures and HTTP responses internally, so OCR currently sees only the final success or failure. A main_task that receives 429, then 529, then succeeds is indistinguishable from a first-attempt success in the final review output.

This makes it difficult to tell whether retries happened, whether they recovered the request, what the provider returned before recovery, or why a request ultimately failed.

Proposed Solution

Keep the official SDK retry decisions unchanged and add request-level observability:

  1. Observe real attempts. Install SDK middleware on all three clients and record each real HTTP attempt: status code, provider request ID, Retry-After, x-should-retry, time to response headers, and the observed gap before the next attempt. The middleware must not consume response bodies or override SDK decisions.
  2. Correct post-header blind spots. At the client return boundary, correct attempts whose failure becomes visible only after HTTP 200, including unexpected EOF or malformed response decoding, SSE stream interruption or integrity failure, and unsuccessful OpenAI Responses object statuses.
  3. Publish one frozen report. Aggregate attempts by stable logical-request identity in a concurrency-safe collector, freeze one RetryReport at the ocr review run boundary, and publish the same snapshot to the terminal and to an optional top-level JSON field named retry_report with schema ocr.llm-retry-report/v1.

Attempt classification reads only HTTP status and Go error types, never error message text:

error_class Trigger
rate_limited HTTP 429
overloaded HTTP 529
authentication HTTP 401/403; the SDK's status-based default does not retry these, although an explicit x-should-retry: true response directive may override that default
timeout HTTP 408/504 or an error matching context.DeadlineExceeded, including parent-context and SDK per-attempt deadlines
network DNS/connect/reset errors without a response, or unexpected EOF
provider Other explicit HTTP/API errors, including 409
cancelled An error matching context.Canceled, including explicit parent-context cancellation
unknown Stable fallback when no more specific classification is supported

Per-request outcome is decided when the logical request finishes, rather than inferred from its last attempt: succeeded, recovered, failed, or cancelled. In particular, cancellation during SDK backoff is reported as cancelled even though the last observed attempt was an error.

Acceptance criteria

  • Anthropic, OpenAI Chat Completions, and OpenAI Responses expose every real SDK HTTP attempt through the shared observer.
  • Review requests carry stable, non-secret identity for plan, main_task, memory compression, re-location, and review filter calls; scan and llm test requests do not enter the report.
  • The report classifies the error classes listed above without inspecting raw error-message text.
  • Unexpected EOF, malformed response decoding, post-200 stream failures, and unsuccessful Responses object statuses are not misreported as successful attempts.
  • Cancellation during retry backoff produces no additional attempt and finalizes the logical request as cancelled.
  • Terminal and JSON output are generated from the same frozen snapshot; concurrent runs have deterministic request and attempt ordering.
  • A clean first-attempt-success run does not emit retry_report; a request with an error or an additional attempt does.
  • The report does not contain credentials, authorization headers, prompts, request or response bodies, full endpoint URLs, or raw provider error strings.
  • Tests cover 429 recovery, 529 exhaustion, authentication failure under the SDK's default status policy, network recovery, cancellation, EOF recovery, stream/response-status correction, concurrent collection, and output compatibility.

Documentation follow-up

To avoid documenting an intermediate contract that may change while the remaining #368 sub-issues settle retry policy and resume lineage, the CLI reference update is deferred until the #368 contracts are finalized. The final documentation should cover the terminal retry summary and the optional retry_report JSON contract together with any later provider-policy or lineage surfaces.

Out of scope

  • This issue does not change SDK retry counts, backoff, jitter, or Retry-After behavior.
  • It does not add a configuration-error class, provider-specific policy, provider concurrency budget, or a cross-subtask retry coordinator; those are tracked separately under Add provider-aware partial retry with preserved lineage #368.
  • It does not change --resume, checkpoints, ocr.run-manifest/v1, or provider/model transition behavior.
  • Only ocr review emits the report; scan and llm test keep their existing output contracts.

Status

Implementation is available on Gongyl01/open-code-review:feat/llm-retry-report as five layered commits: data model, middleware observation, client-boundary correction, request-identity wiring, and run-boundary publishing. A PR with final test and coverage results will follow.

Alternatives Considered

No response

Affected Area

Telemetry / Observability

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions