Skip to content

Bug: 'Body already used' in transformAntigravityResponse catch block (v1.5.1) #444

@BrunoMarc

Description

@BrunoMarc

Summary

transformAntigravityResponse() in request.js consumes the response body via response.text() at the top of a try block. If anything throws later (e.g. detectErrorType, rewriteAntigravityPreviewAccessError, etc.), the catch block returns the consumed original response. When OpenCode's AI SDK (@ai-sdk/provider-utils@3.0.20) then calls response.text() in its failedResponseHandler, it gets TypeError: Body already used, which is wrapped as:

AI_APICallError: Failed to process error response
Caused by: Body already used

Root Cause

In plugin/request.js, the transformAntigravityResponse function:

// line ~1207
try {
    const text = await response.text();  // ← BODY CONSUMED
    // ... 110 lines of processing ...
}
catch (error) {
    return response;  // ← BUG: returns consumed body
}

Secondary Bug: THINKING_RECOVERY_NEEDED silently swallowed

detectErrorType() at line ~1224 can trigger throw new Error("THINKING_RECOVERY_NEEDED") at line ~1230. This throw is caught by the same catch block at the bottom of the function, so it never reaches the outer catch in plugin.js:1940 where the retry logic lives. Thinking recovery is effectively broken.

Fix

Two-line change:

// Clone BEFORE consuming
const responseFallback = response.clone();
try {
    const text = await response.text();
    // ... unchanged ...
}
catch (error) {
    // Re-throw recovery errors for outer handler
    if (error instanceof Error && error.message === "THINKING_RECOVERY_NEEDED") {
        throw error;
    }
    logAntigravityDebugResponse(debugContext, response, {
        error,
        note: "Failed to transform Antigravity response",
    });
    return responseFallback;  // ← Return unconsumed clone
}

Environment

  • OpenCode v1.1.65
  • opencode-antigravity-auth v1.5.1
  • @ai-sdk/provider-utils@3.0.20 (bundled in OpenCode binary)
  • Model: antigravity-claude-opus-4-6-thinking
  • Error confirmed in logs from new session (PID started fresh)

Reproduction

Intermittent — triggers when the Google API returns an error response (403/429/500) AND something throws during error body processing in transformAntigravityResponse. The Body already used error is deterministic once this code path is reached.

Stack Trace from Logs

AI_APICallError: Failed to process error response
    at <anonymous> (../../node_modules/.bun/@ai-sdk+provider-utils@3.0.20+d6123d32214422cb/node_modules/@ai-sdk/provider-utils/dist/index.mjs:790:19)
    at processTicksAndRejections (native:7:39)

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/compatCompatibility with OpenCode, DCPbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions