feat(batch): async wait/reconcile primitives for the Batches API - #111
Merged
Conversation
Completes the BatchClient surface for real async usage: - WaitUntilDone polls until a terminal state (ended/completed/failed/ expired/canceled) with exponential backoff + jitter capped at MaxInterval, honoring Retry-After headers on 429/5xx, failing fast on other non-200s, and bounding total wall-clock via Timeout. - RequestResults fetches and parses the JSONL results document into BatchRequestResult rows that preserve the provider payload byte-exact for per-provider decoding. - PollRequestResult reconciles one custom_id: waits for batch completion, then tolerates eventual consistency where the results endpoint lags completion (bounded extra attempts), surfacing ErrResultNotVisible only after the row is genuinely absent. - backoffDelay helper with jitter and Retry-After override. Motivated by agent-fleet orchestration patterns (Orca/grok-cli): batch result rows are eventually consistent and naive pollers either miss rows or storm the API; these primitives make the reconcile loop correct by construction. Tests: 8 new httptest-based cases covering terminal transition, timeout, Retry-After honoring on 429, fail-fast on 401, JSONL parsing, lag tolerance, not-visible exhaustion, and Retry-After override in backoff.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Completes the
BatchClientsurface for real async usage, adopting the reconcile pattern proven in agent-fleet orchestration (Orca/grok-cli): batch result rows are eventually consistent — an individual request's row can lag the batch reaching a terminal state — so naive pollers either miss rows or storm the API.Additions (
client/batch_async.go)WaitUntilDone— polls until a terminal state with exponential backoff + jitter capped atMaxInterval, honoringRetry-Afteron 429/5xx, failing fast on other non-200s, bounded byTimeout(default 10m)RequestResults— fetches + parses the JSONL results document; eachBatchRequestResultpreserves the provider payload byte-exact for per-provider decodingPollRequestResult— reconciles onecustom_id: waits for batch completion, then tolerates eventual consistency via bounded extra attempts before surfacingErrResultNotVisiblebackoffDelayhelper with jitter and Retry-After overrideTesting
8 new httptest-based cases: terminal transition, timeout, Retry-After honored on 429 (timing-verified), fail-fast 401, JSONL parsing, lag tolerance, not-visible exhaustion, Retry-After override. Full client suite green.