import { carryProxyError } from "../../lib/client-error-body";
import type { TranslatorBudget } from "../../lib/translator-budget";
import { carryResponseToolOwnership } from "./response-tool-ownership";
import { carryPolicyCandidateRefusal } from "./policy-request-scope";
import { finalizeTranslatorBudgetResponse } from "../../lib/translator-budget";
import { carryPacedStream, pacedQueuingStrategy } from "../../lib/paced-stream";
import { carryReplayRefusal, isNonReplayableResponse, markResponseNonReplayable } from "../../lib/upstream-retry";
import { isVerifiedModelNotInPlanResponse, markVerifiedModelNotInPlanResponse } from "../../lib/errors";
import { carryCodexAccountCooldownResponse } from "../../codex/account-cooldown-response";
import { carryFormattedQuotaSource } from "../../bridge/errors";
import {
  isNativePassthroughSseResponse,
  markNativePassthroughSseResponse,
  isEagerRelaySseResponse,
  markEagerRelaySseResponse,
  isPreinspectedJsonResponse,
  markPreinspectedJsonResponse,
} from "../relay";

// runTurn adapters own an event queue and perform their combo preflight before
// bridging. A second byte-stream reader would reinterpret that transport's
// already-committed event boundary and can replay custom adapter work.
export const runTurnAdapterSseResponses = new WeakSet<Response>();


// Whole-body policy for non-streaming upstream JSON responses (see the application/json
// branch of the passthrough return path). 32 MiB matches the continuation snapshot read
// bound and is far above any legitimate non-streaming completion, including base64 image
// payloads. The stall deadlines only govern the body transfer — generation time before
// the response headers is untouched. Generation after early/chunked headers but before
// the first body byte previously used the 30-second inactivity deadline; this call site
// gives it the full body deadline instead.
export const MAX_UPSTREAM_JSON_BODY_BYTES = 32 * 1024 * 1024;

export const UPSTREAM_JSON_BODY_TOTAL_TIMEOUT_MS = 180_000;

export const UPSTREAM_JSON_BODY_INACTIVITY_TIMEOUT_MS = 30_000;

export const UPSTREAM_JSON_BODY_READ_OPTIONS = {
  maxBytes: MAX_UPSTREAM_JSON_BODY_BYTES,
  totalTimeoutMs: UPSTREAM_JSON_BODY_TOTAL_TIMEOUT_MS,
  inactivityTimeoutMs: UPSTREAM_JSON_BODY_INACTIVITY_TIMEOUT_MS,
  firstByteTimeoutMs: UPSTREAM_JSON_BODY_TOTAL_TIMEOUT_MS,
};




export function finalizeOwnedTranslatorBudget(response: Response, budget: TranslatorBudget, signal?: AbortSignal): Response {
  const finalizedResponse = carryProxyError(response, finalizeTranslatorBudgetResponse(response, budget, signal));
  if (isNativePassthroughSseResponse(response)) {
    markNativePassthroughSseResponse(finalizedResponse);
  }
  if (isEagerRelaySseResponse(response)) {
    markEagerRelaySseResponse(finalizedResponse);
  }
  if (isPreinspectedJsonResponse(response)) markPreinspectedJsonResponse(finalizedResponse);
  if (isNonReplayableResponse(response)) markResponseNonReplayable(finalizedResponse);
  if (isVerifiedModelNotInPlanResponse(response)) markVerifiedModelNotInPlanResponse(finalizedResponse);
  if (runTurnAdapterSseResponses.has(response)) runTurnAdapterSseResponses.add(finalizedResponse);
  carryCodexAccountCooldownResponse(response, finalizedResponse);
  carryPolicyCandidateRefusal(response, finalizedResponse);
  return carryResponseToolOwnership(response, carryFormattedQuotaSource(response, finalizedResponse));
}

/** Release a serving-account slot when the client body ends, errors or is cancelled. */
export function finalizeAccountLease(response: Response, release: () => void, onCancel?: () => void): Response {
  if (!response.body) { release(); return response; }
  const reader = response.body.getReader();
  let done = false;
  const finish = () => { if (!done) { done = true; release(); } };
  const body = carryPacedStream(response.body, new ReadableStream<Uint8Array>({
    async pull(controller) {
      try {
        const next = await reader.read();
        if (next.done) { finish(); controller.close(); }
        else controller.enqueue(next.value);
      } catch (error) { finish(); controller.error(error); }
    },
    async cancel(reason) { onCancel?.(); try { await reader.cancel(reason); } finally { finish(); } },
  }, pacedQueuingStrategy(response.body)));
  const wrapped = new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers });
  carryProxyError(response, wrapped);
  if (isNonReplayableResponse(response)) markResponseNonReplayable(wrapped);
  if (isVerifiedModelNotInPlanResponse(response)) markVerifiedModelNotInPlanResponse(wrapped);
  if (isNativePassthroughSseResponse(response)) markNativePassthroughSseResponse(wrapped);
  if (isEagerRelaySseResponse(response)) markEagerRelaySseResponse(wrapped);
  if (isPreinspectedJsonResponse(response)) markPreinspectedJsonResponse(wrapped);
  if (runTurnAdapterSseResponses.has(response)) runTurnAdapterSseResponses.add(wrapped);
  // Retained compaction delivers through this lease wrapper; keep a local cooldown refusal local (#7850).
  carryCodexAccountCooldownResponse(response, wrapped);
  carryPolicyCandidateRefusal(response, wrapped);
  return carryResponseToolOwnership(response, carryReplayRefusal(response, carryFormattedQuotaSource(response, wrapped)));
}




export function linkAbortSignal(upstream: AbortController, signal?: AbortSignal): () => void {
  if (!signal) return () => {};
  if (signal.aborted) {
    upstream.abort(signal.reason);
    return () => {};
  }
  const onAbort = () => upstream.abort(signal.reason);
  signal.addEventListener("abort", onAbort, { once: true });
  return () => signal.removeEventListener("abort", onAbort);
}
