import type { TranslatorBudget } from "../../lib/translator-budget";
import type { AdapterEvent } from "../../types";
import { repairFreeformToolInput } from "../../responses/apply-patch-envelope";

const OPEN_TAG = "<tool_call>";
const CLOSE_TAG = "</tool_call>";
const FUNCTION_TAG = "<function=";
/**
 * Streaming bounds (ingestStreaming only; buffered reconciliation already knows its structured calls).
 * MAX_HELD_BYTES is a runaway guard on held text plus queued events, far below the translator budget,
 * so a large duplicated write inside the block is still reconciled. MAX_TRAILING_CHARS bounds the
 * latency case: a duplicated block is the tail of the content (#5548), so a closed block followed by
 * this much prose, with no block open after it, is treated as prose and released.
 */
const MAX_HELD_BYTES = 4 * 1024 * 1024;
const MAX_TRAILING_CHARS = 8 * 1024;

interface TextContext {
  fence: string | null;
  lineStart: boolean;
  inlineTicks?: number;
}

export interface SerializedToolCall {
  name: string;
  body: string;
  start: number;
  end: number;
}

/** A freeform tool's declared identity. Echoes are matched against it, never against the wire alias. */
export interface FreeformToolIdentity {
  name: string;
  namespace?: string;
}

/** The request's freeform tools keyed by the wire name the provider sees for each of them. */
export function freeformToolsByWireName<T extends FreeformToolIdentity & { freeform?: boolean }>(
  tools: readonly T[] | undefined,
  wireName: (tool: T) => string,
): Map<string, FreeformToolIdentity> {
  return new Map(tools?.filter(tool => tool.freeform).map(tool => [wireName(tool), { name: tool.name, namespace: tool.namespace }] as const) ?? []);
}

export interface StructuredToolCallReference {
  names: ReadonlySet<string>;
  argumentsText: string;
  freeformTool?: FreeformToolIdentity;
  recoveredParameterWrapper?: boolean;
}

const BLOCK_HEADER = /<tool_call>\s*<function=([^>\r\n]+)>/y;
/** A separate bare block starts a line (see `splitAtPossibleSerializedToolCall`); a header mid-line is body text. */
const NEXT_BLOCK_HEADER = /\n<tool_call>\s*<function=[^>\r\n]+>/g;
const FUNCTION_CLOSE = "</function>";
const PARAMETER_CLOSE = "</parameter>";

function trimmedEnd(text: string, from: number, to: number): number {
  while (to > from && /\s/.test(text[to - 1]!)) to--;
  return to;
}

function endsWithAt(text: string, from: number, to: number, suffix: string): boolean {
  return to - suffix.length >= from && text.startsWith(suffix, to - suffix.length);
}

/**
 * The block starting at `offset`, read by delimiter scan so an unterminated block costs linear time.
 * MiMo's echo may close a freeform body with a stray `</parameter>` and may omit `</function>`
 * (#5724), the grammar the Command Code reader accepts too. The first `</tool_call>` preceded by
 * `</function>` closes the block, so a body can still carry a literal `</tool_call>` or header;
 * with none before the next line-start block header, the first `</tool_call>` does. That header only
 * bounds an unclosed candidate: with no close at all before it, it is body text, and a closed
 * `</function></tool_call>` after it still ends the block.
 */
function blockAt(text: string, offset: number): SerializedToolCall | undefined {
  BLOCK_HEADER.lastIndex = offset;
  const header = BLOCK_HEADER.exec(text);
  if (!header) return undefined;
  const bodyStart = offset + header[0].length;
  NEXT_BLOCK_HEADER.lastIndex = bodyStart;
  const next = NEXT_BLOCK_HEADER.exec(text);
  const limit = next ? next.index + 1 : text.length;
  let unclosed: SerializedToolCall | undefined;
  for (let close = text.indexOf(CLOSE_TAG, bodyStart); close >= 0 && (close < limit || !unclosed);
    close = text.indexOf(CLOSE_TAG, close + CLOSE_TAG.length)) {
    let bodyEnd = trimmedEnd(text, bodyStart, close);
    const closed = endsWithAt(text, bodyStart, bodyEnd, FUNCTION_CLOSE);
    if (closed) bodyEnd = trimmedEnd(text, bodyStart, bodyEnd - FUNCTION_CLOSE.length);
    if (endsWithAt(text, bodyStart, bodyEnd, PARAMETER_CLOSE)) bodyEnd -= PARAMETER_CLOSE.length;
    const call = {
      name: header[1]!.trim(),
      body: text.slice(bodyStart, bodyEnd),
      start: offset,
      end: close + CLOSE_TAG.length,
    };
    if (closed) return call;
    if (close < limit) unclosed ??= call;
  }
  return unclosed;
}

/** Finds complete bare blocks outside literal Markdown; ambiguous outer blocks stop the scan. */
function callsIn(text: string, context: TextContext = { fence: null, lineStart: true }): SerializedToolCall[] {
  const calls: SerializedToolCall[] = [];
  let offset = 0;
  while (offset < text.length) {
    const split = splitAtPossibleSerializedToolCall(text.slice(offset), context, true);
    offset += split.emit.length;
    if (!split.hasOpenTag) break;
    const match = blockAt(text, offset);
    if (!match) break; // An incomplete/ambiguous outer block cannot authorize an inner call.
    calls.push(match);
    offset = match.end;
    context = { fence: null, lineStart: false };
  }
  return calls;
}

/**
 * The first block, and only when the text after it is exactly one repetition of that same block
 * (trailing whitespace allowed). The returned range covers the pair and any trailing whitespace.
 */
function repeatedCallIn(text: string, context?: TextContext): SerializedToolCall | undefined {
  const first = callsIn(text, context)[0];
  if (!first) return undefined;
  if (text.slice(first.end).trimEnd() !== text.slice(first.start, first.end).trimEnd()) return undefined;
  return { ...first, end: text.length };
}

/** Splits safe visible text from a possible control block while carrying Markdown context across chunks. */
export function splitAtPossibleSerializedToolCall(
  text: string,
  initialContext: TextContext = { fence: null, lineStart: true },
  final = false,
): {
  emit: string;
  defer: string;
  hasOpenTag: boolean;
  context: TextContext;
} {
  const context = { ...initialContext };
  const split = (at: number, hasOpenTag = false) => ({
    emit: text.slice(0, at), defer: text.slice(at), hasOpenTag, context,
  });
  for (let index = 0; index < text.length; index++) {
    if (context.lineStart) {
      const rest = text.slice(index);
      const fence = /^ {0,3}(`{3,}|~{3,})([^\n]*)/.exec(rest);
      if (!context.inlineTicks && fence && (!context.fence || (fence[1]![0] === context.fence[0]
          && fence[1]!.length >= context.fence.length && /^[ \t\r]*$/.test(fence[2]!)))) {
        if (!final && !rest.includes("\n")) return split(index);
        context.fence = context.fence ? null : fence[1]!;
        index += fence[0].length - 1;
        context.lineStart = false;
        continue;
      }
      if (!final && /^ {0,3}(`*|~*)$/.test(rest)) return split(index);
      // Only bare control markup qualifies. Prose, quotes, indented examples and
      // fenced code stay user-visible even when their body matches a real call.
      if (!context.fence && !context.inlineTicks) {
        if (rest.startsWith(OPEN_TAG)) {
          const header = rest.slice(OPEN_TAG.length).trimStart();
          if (/^<function=[^>\r\n]+>/.test(header)) return split(index, true);
          if (!final && (FUNCTION_TAG.startsWith(header)
              || (header.startsWith(FUNCTION_TAG) && !/[>\r\n]/.test(header.slice(FUNCTION_TAG.length))))) {
            return split(index);
          }
        } else if (!final && OPEN_TAG.startsWith(rest)) return split(index);
      }
    }
    if (!context.fence && text[index] === "`") {
      let end = index + 1;
      while (text[end] === "`") end++;
      if (!final && end === text.length) return split(index);
      const ticks = end - index;
      if (!context.inlineTicks) context.inlineTicks = ticks;
      else if (context.inlineTicks === ticks) context.inlineTicks = undefined;
      index = end - 1;
    }
    context.lineStart = text[index] === "\n";
  }
  return split(text.length);
}

/**
 * The line, fence and inline-code state after `text`. Serialized blocks are neutralised first so
 * the scan runs through the whole text instead of stopping at the first opening tag; a block ends
 * mid-line, which is exactly what the neutral spelling reports too.
 */
function contextAfter(text: string, context: TextContext): TextContext {
  if (text.length === 0) return context;
  return splitAtPossibleSerializedToolCall(text.replaceAll(OPEN_TAG, "<tool-call>"), context, true).context;
}

/**
 * A deferred header/fence can itself be long. While its unresolved portion only grows, a
 * chunk-local check suffices; the first resolving character goes back through the full splitter.
 * Each unbounded phase is scanned once on entry and once on exit, preserving its Markdown rules.
 */
function deferredContinuationBreak(text: string, context: TextContext): RegExp | undefined {
  if (text.startsWith(OPEN_TAG)) {
    if (/^<tool_call>\s*$/.test(text)) return /\S/;
    if (/^<tool_call>\s*<function=[^>\r\n]*$/.test(text)) return /[>\r\n]/;
  }
  if (!context.inlineTicks && context.lineStart) {
    const fence = /^ {0,3}(`{3,}|~{3,})([^\n]*)$/.exec(text);
    if (fence) {
      if (!context.fence) return /\n/;
      // Closing-fence ticks may grow only before its optional whitespace suffix.
      if (!fence[2]) return fence[1]![0] === "`" ? /[^`]/ : /[^~]/;
      return /[^ \t\r]/;
    }
  }
  if (/^`+$/.test(text)) return /[^`]/;
  return undefined;
}

/**
 * Holds possible duplicate text within the shared translator budget until the dispatch outcome is
 * known. While a block candidate is open, any other event (reasoning) is queued at its position in
 * the held text rather than overtaking it or forcing the block out early, and `drain` restores the
 * original order.
 */
export class SerializedToolCallContentBuffer {
  releasedAnswerText = false;
  private text = "";
  private bytes = 0;
  private hasOpenTag = false;
  private continuationBreak: RegExp | undefined;
  private delimiterSuffix = "";
  private sawCloser = false;
  private openAfterCloser = false;
  private trailingProseChars = 0;
  private context: TextContext = { fence: null, lineStart: true };
  private queued: { offset: number; event: AdapterEvent }[] = [];

  constructor(private readonly budget: TranslatorBudget) {}

  /** Remembers visible text released before a later structured call can be reconciled. */
  private observeText(events: AdapterEvent[]): AdapterEvent[] {
    if (events.some(event => event.type === "text_delta" && event.text.trim() !== "")) this.releasedAnswerText = true;
    return events;
  }

  /** Measures an append exactly even when the runtime prices isolated UTF-16 surrogates differently. */
  private appendedByteLength(delta: string): number {
    let bytes = Buffer.byteLength(delta);
    if (this.text.length === 0 || delta.length === 0) return bytes;
    const tail = this.text[this.text.length - 1]!;
    const head = delta[0]!;
    const tailCode = tail.charCodeAt(0);
    const headCode = head.charCodeAt(0);
    if (tailCode >= 0xd800 && tailCode <= 0xdbff && headCode >= 0xdc00 && headCode <= 0xdfff) {
      bytes += Buffer.byteLength(tail + head) - Buffer.byteLength(tail) - Buffer.byteLength(head);
    }
    return bytes;
  }

  /** Reserves the replacement before releasing the old text, preserving it if the budget rejects growth. */
  private replace(next: string, hasOpenTag: boolean): void {
    const nextBytes = Buffer.byteLength(next);
    const reservation = this.budget.reserveTransient(nextBytes, { kind: "live_transient" });
    try {
      reservation.commitRetained();
      this.budget.releaseRetained(this.bytes, { kind: "live_transient" });
      this.text = next;
      this.bytes = nextBytes;
      this.hasOpenTag = hasOpenTag;
      this.continuationBreak = undefined;
      this.delimiterSuffix = "";
      this.sawCloser = false;
      this.openAfterCloser = false;
      this.trailingProseChars = 0;
      if (hasOpenTag) this.observeDelimiters(next);
    } catch (error) {
      reservation.release();
      throw error;
    }
  }

  /** Charges only the appended bytes, so holding an open block never needs twice its retained size. */
  private append(delta: string): void {
    const deltaBytes = this.appendedByteLength(delta);
    this.budget.reserveTransient(deltaBytes, { kind: "live_transient" }).commitRetained();
    if (this.hasOpenTag) this.observeDelimiters(delta);
    this.text += delta;
    this.bytes += deltaBytes;
  }

  /** Scan new text plus a fixed overlap, never the retained body (which may be a large rope). */
  private observeDelimiters(delta: string): void {
    const scan = this.delimiterSuffix + delta;
    const closer = scan.lastIndexOf(CLOSE_TAG);
    const afterCloser = closer + CLOSE_TAG.length;
    if (closer >= 0) {
      this.sawCloser = true;
      this.openAfterCloser = false;
      this.trailingProseChars = 0;
    }
    if (scan.lastIndexOf(OPEN_TAG) >= (closer >= 0 ? afterCloser : 0)) {
      this.openAfterCloser = true;
    }
    const tail = closer >= 0 ? scan.slice(afterCloser) : delta;
    if (this.sawCloser) this.trailingProseChars += tail.replace(/\s+/g, "").length;
    this.delimiterSuffix = scan.slice(-(Math.max(OPEN_TAG.length, CLOSE_TAG.length) - 1));
  }

  /** Returns immediately safe text and retains only the suffix that still needs reconciliation. */
  ingest(delta: string): string {
    if (this.hasOpenTag || (this.continuationBreak && !this.continuationBreak.test(delta))) {
      this.append(delta);
      return "";
    }
    const split = splitAtPossibleSerializedToolCall(this.text + delta, this.context);
    this.replace(split.defer, split.hasOpenTag);
    this.context = split.context;
    if (!split.hasOpenTag) this.continuationBreak = deferredContinuationBreak(split.defer, split.context);
    return split.emit;
  }

  /**
   * Streaming ingest with bounded retention. Past either bound the stream prefers delivering text
   * over suppressing a possible duplicate: everything held is released in order, nothing is
   * suppressed (the behaviour before #5548 for that block), and scanning resumes from the carried
   * context. The size bound is checked before the delta is retained.
   */
  ingestStreaming(delta: string): AdapterEvent[] {
    const deltaBytes = this.appendedByteLength(delta);
    if (this.hasOpenTag && this.bytes + deltaBytes > MAX_HELD_BYTES) {
      const released = this.drain([]);
      // A delta that alone passes the bound is delivered as text rather than retained.
      if (Buffer.byteLength(delta) > MAX_HELD_BYTES) {
        this.context = contextAfter(delta, this.context);
        return this.observeText([...released, ...textEvents(delta)]);
      }
      return this.observeText([...released, ...textEvents(this.ingest(delta))]);
    }
    const text = this.ingest(delta);
    // Checked after ingest too: one delta can open a block and already carry more than a bound.
    if (this.hasOpenTag && (this.bytes > MAX_HELD_BYTES || (this.sawCloser && !this.openAfterCloser && this.trailingProseChars > MAX_TRAILING_CHARS))) {
      return this.observeText([...textEvents(text), ...this.drain([])]);
    }
    return this.observeText(textEvents(text));
  }

  /** Exposes held text as evidence for narrowly repairing duplicated argument prefixes. */
  current(): string {
    return this.text;
  }

  /**
   * Passes a non-text event through, in order. With an open block candidate held, the event is
   * queued behind the held text and a heartbeat stands in for it; with only a partial prefix held (no complete opening tag yet),
   * that prefix cannot be a whole duplicate and is released ahead of the event.
   */
  hold(event: AdapterEvent): AdapterEvent[] {
    if (!this.hasOpenTag) return [...this.drain([]), event];
    const eventBytes = Buffer.byteLength(JSON.stringify(event));
    // Queued events count toward the same runaway bound as held text.
    if (this.bytes + eventBytes > MAX_HELD_BYTES) return [...this.drain([]), event];
    this.budget.reserveTransient(eventBytes, { kind: "live_transient" }).commitRetained();
    this.bytes += eventBytes;
    this.queued.push({ offset: this.text.length, event });
    // The consumer still sees activity, so a stall watchdog never mistakes a held turn for a dead one.
    return [{ type: "heartbeat" }];
  }

  /**
   * Drains held text and queued events in their original order, suppressing only blocks that
   * duplicate a dispatched call; pass an empty list on failure to preserve everything.
   */
  drain(structuredCalls: readonly StructuredToolCallReference[]): AdapterEvent[] {
    const removed = duplicatedSerializedToolCallRanges(this.text, structuredCalls, this.context);
    const kept = (from: number, to: number): string => {
      let piece = "";
      let cursor = from;
      for (const range of removed) {
        if (range.end <= cursor || range.start >= to) continue;
        piece += this.text.slice(cursor, Math.max(cursor, range.start));
        cursor = Math.min(to, range.end);
      }
      return piece + this.text.slice(cursor, to);
    };
    const out: AdapterEvent[] = [];
    let cursor = 0;
    for (const boundary of [...this.queued, { offset: this.text.length, event: undefined }]) {
      const text = kept(cursor, boundary.offset);
      if (text.length > 0) out.push({ type: "text_delta", text });
      if (boundary.event) out.push(boundary.event);
      cursor = boundary.offset;
    }
    // Later text continues after what was drained, so its line and fence state carry forward.
    this.context = contextAfter(this.text, this.context);
    this.queued = [];
    this.replace("", false);
    return this.observeText(out);
  }

  /** Text-only drain for callers that never queued an event. */
  flush(structuredCalls: readonly StructuredToolCallReference[]): string {
    return this.drain(structuredCalls)
      .map(event => (event.type === "text_delta" ? event.text : ""))
      .join("");
  }

  /** Releases retained bytes when the stream ends or its consumer cancels iteration. */
  dispose(): void {
    this.budget.releaseRetained(this.bytes, { kind: "live_transient" });
    this.text = "";
    this.bytes = 0;
    this.hasOpenTag = false;
    this.continuationBreak = undefined;
    this.queued = [];
  }
}

/** Reads a wrapped input or raw arguments from a declared freeform tool; neither path rewrites them. */
function inputFromArguments(
  argumentsText: string,
  freeformTool?: FreeformToolIdentity,
): string | undefined {
  try {
    const parsed = JSON.parse(argumentsText) as unknown;
    const input = parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
      ? (parsed as Record<string, unknown>).input
      : undefined;
    if (typeof input === "string") return input;
  } catch {
    // Chat gateways can send custom-tool input as raw text.
  }
  if (!freeformTool) return undefined;
  // Match raw input only when the Responses bridge dispatches these same bytes. It can unwrap
  // alternate fields or repair freeform input using the declared tool's name and namespace.
  return repairFreeformToolInput(argumentsText, freeformTool.name, freeformTool.namespace) === argumentsText
    ? argumentsText : undefined;
}

/** One wrapping newline after the function header is template layout, not input (vLLM `_trim_wrapping_newlines`). */
function freeformBody(value: string): string {
  return value.replace(/^\r?\n/, "").trimEnd();
}

/**
 * Whether a structured call's freeform input already equals the body of `repeated`. Such a call
 * explains the repeated pair on its own, which is what competes with a doubled call in the same
 * batch: both readings account for the two blocks, and the response never says which one it meant.
 */
function agreesWithRepeatedBlock(
  structured: StructuredToolCallReference,
  repeated: SerializedToolCall,
): boolean {
  const input = structured.names.has(repeated.name) ? inputFromArguments(structured.argumentsText, structured.freeformTool) : undefined;
  return input !== undefined && freeformBody(input) === freeformBody(repeated.body);
}

/** A second call can explain a repeated pair even when its arguments cannot safely be rewritten. */
function hasDoubledInput(structured: StructuredToolCallReference, repeated: SerializedToolCall): boolean {
  if (!structured.names.has(repeated.name)) return false;
  const input = inputFromArguments(structured.argumentsText, structured.freeformTool);
  if (input === undefined) return false;
  const body = freeformBody(repeated.body);
  const normalized = freeformBody(input);
  return normalized === body + body || normalized === body + "\n" + body;
}

/** The `[start, end)` ranges of blocks whose function identity and freeform input match a dispatched call. */
function duplicatedSerializedToolCallRanges(
  text: string,
  structuredCalls: readonly StructuredToolCallReference[],
  context?: TextContext,
): { start: number; end: number }[] {
  if (structuredCalls.length === 0) return [];
  const repeated = repeatedCallIn(text, context);
  if (repeated) {
    // A doubled call beside an agreeing call leaves the pair ambiguous. If reduction was
    // refused, keep the markup too: otherwise the visible text and executable call disagree.
    const matching = structuredCalls.filter(structured => agreesWithRepeatedBlock(structured, repeated));
    const doubled = structuredCalls.some(structured => structured !== matching[0] && hasDoubledInput(structured, repeated));
    return matching.length === 1 && !doubled ? [{ start: repeated.start, end: repeated.end }] : [];
  }
  return callsIn(text, context).filter(call => {
    const body = freeformBody(call.body);
    return structuredCalls.some(structured => {
      const input = structured.names.has(call.name) ? inputFromArguments(structured.argumentsText, structured.freeformTool) : undefined;
      if (input === undefined) return false;
      const normalized = freeformBody(input);
      return normalized === body || (structured.recoveredParameterWrapper === true
        && body.startsWith("<parameter=") && normalized === freeformBody(body.slice("<parameter=".length)));
    });
  });
}

/** Removes eligible blocks only when both the function identity and freeform input match a dispatched call. */
export function stripDuplicatedSerializedToolCalls(
  text: string,
  structuredCalls: readonly StructuredToolCallReference[],
  context?: TextContext,
): string {
  let result = "";
  let cursor = 0;
  for (const range of duplicatedSerializedToolCallRanges(text, structuredCalls, context)) {
    result += text.slice(cursor, range.start);
    cursor = range.end;
  }
  return result + text.slice(cursor);
}

/**
 * The reduced arguments when the freeform body of the repeated block is written twice in the
 * single string "input" field, or undefined for any other shape. Only the batch reconciler may
 * apply it: the reduction rewrites executable arguments, so it needs a uniqueness proof.
 */
function doubledInputReduction(
  argumentsText: string,
  functionNames: ReadonlySet<string>,
  repeated: SerializedToolCall,
): string | undefined {
  if (!functionNames.has(repeated.name)) return undefined;
  const body = freeformBody(repeated.body);
  try {
    const parsed = JSON.parse(argumentsText) as unknown;
    if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
        && Object.keys(parsed).length === 1
        && ((parsed as Record<string, unknown>).input === body + body
          || (parsed as Record<string, unknown>).input === body + "\n" + body)) {
      return JSON.stringify({ input: body });
    }
  } catch {
    // A malformed concatenation is handled by the prefix repair instead.
  }
  return undefined;
}

/** Removes a malformed argument prefix only when a bare block and the JSON suffix prove identical input. */
export function repairArgumentsDuplicatedBesideSerializedCall(
  argumentsText: string,
  functionNames: ReadonlySet<string>,
  serializedText: string,
): string {
  try {
    JSON.parse(argumentsText);
    return argumentsText;
  } catch {
    // Continue only for the exact duplication shape emitted by some Chat gateways.
  }

  const bodies = callsIn(serializedText)
    .filter(call => functionNames.has(call.name))
    .map(call => call.body.trimEnd());
  if (bodies.length === 0) return argumentsText;

  for (const body of bodies) {
    if (!argumentsText.startsWith(body)) continue;
    let start = body.length;
    while (start < argumentsText.length && /\s/.test(argumentsText[start]!)) start += 1;
    const candidate = argumentsText.slice(start);
    let parsed: unknown;
    try {
      parsed = JSON.parse(candidate);
    } catch {
      continue;
    }
    if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) continue;
    const input = (parsed as Record<string, unknown>).input;
    if (typeof input !== "string") continue;
    if (body !== input.trimEnd()) continue;
    return candidate;
  }
  return argumentsText;
}

/** One structured call as the reconciler sees it, before its arguments meet the visible text. */
export interface StructuredToolCallInput {
  wireName: string;
  restoredName: string;
  argumentsText: string;
  freeformTool?: FreeformToolIdentity;
}

/**
 * Repairs the arguments of every structured call in one response against the visible text that
 * response carried, and returns them in input order. The per-call prefix repair stands alone,
 * because the markup it proves is matched against that call's own repaired input. The
 * doubled-input reduction is applied only when exactly one call in the batch explains the repeated
 * pair, including doubled shapes that cannot themselves be rewritten. It rewrites executable
 * arguments, so a second explanatory call leaves the pair ambiguous.
 */
export function reconcileStructuredToolCalls(
  calls: readonly StructuredToolCallInput[],
  serializedText: string,
  recoverEmptyMiMoFreeformInput = false,
): StructuredToolCallReference[] {
  const references: StructuredToolCallReference[] = calls.map(call => {
    const names = new Set([call.wireName, call.restoredName]);
    return { names, freeformTool: call.freeformTool, argumentsText: repairArgumentsDuplicatedBesideSerializedCall(call.argumentsText, names, serializedText) };
  });
  if (recoverEmptyMiMoFreeformInput && calls.length === 1 && calls[0]!.freeformTool
      && emptyObjectArguments(references[0]!.argumentsText)) {
    // A standalone MiMo block and one empty call to the exact wire tool provide the only
    // unambiguous recovery shape. Prose before/after it or another call keeps text inert.
    const blocks = callsIn(serializedText);
    if (blocks.length === 1 && serializedText.slice(0, blocks[0]!.start).trim() === ""
        && serializedText.slice(blocks[0]!.end).trim() === "") {
      const block = blocks[0]!;
      const wrappedBody = freeformBody(block.body);
      // MiMo can put the freeform input after a malformed `<parameter=` opener.
      const hasParameterOpener = wrappedBody.startsWith("<parameter=");
      const body = hasParameterOpener
        ? freeformBody(wrappedBody.slice("<parameter=".length)) : wrappedBody;
      if (block.name === calls[0]!.wireName && body.trim() !== ""
          && !/(?:^|\n)<tool_call>\s*<function=[^>\r\n]+>/.test(block.body)
          && !(hasParameterOpener && /^<parameter=[A-Za-z_$][\w$.-]*>/.test(wrappedBody))
          && !body.includes("<parameter=") && !body.includes("</parameter>")) {
        references[0] = { ...references[0]!, argumentsText: JSON.stringify({ input: body }),
          recoveredParameterWrapper: hasParameterOpener };
      }
    }
  }
  return reduceUnambiguousDoubledInput(references, serializedText);
}

/** An empty JSON object means the structured freeform call supplied no input. */
function emptyObjectArguments(text: string): boolean {
  try {
    const parsed: unknown = JSON.parse(text);
    return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
      && Object.keys(parsed).length === 0;
  } catch {
    return false;
  }
}

/**
 * Applies the doubled-input reduction across the batch. The doubled shape is valid JSON, so the
 * prefix repair returns it untouched, and the reduction only ever rewrites a call the repair left
 * alone. A call whose input already equals the repeated body is a competing explanation, not a
 * bystander: both readings account for the pair and the response never picks one, so a batch with
 * two qualifying calls keeps every argument exactly as sent. The reduction then rewrites nothing,
 * and the range matcher keeps the markup when a doubled call competes with an agreeing call.
 * An already-agreeing call leaves nothing to reduce.
 */
function reduceUnambiguousDoubledInput(
  references: readonly StructuredToolCallReference[],
  serializedText: string,
): StructuredToolCallReference[] {
  const repeated = repeatedCallIn(serializedText);
  if (!repeated) return [...references];
  const candidates = references.map(reference => ({
    reduction: doubledInputReduction(reference.argumentsText, reference.names, repeated),
    explains: agreesWithRepeatedBlock(reference, repeated),
    doubled: hasDoubledInput(reference, repeated),
  }));
  if (candidates.filter(candidate => candidate.explains || candidate.doubled).length !== 1) {
    return [...references];
  }
  return references.map((reference, index) => {
    const reduction = candidates[index]!.reduction;
    return reduction === undefined ? reference : { ...reference, argumentsText: reduction };
  });
}

/**
 * One structured call as the reconciler sees it: both the wire name and its restored client name
 * identify it, and its arguments are repaired against the visible text the same response carried.
 * A single call is its own batch, so the doubled-input reduction still applies here; a caller
 * holding several calls of one response must pass them together to
 * `reconcileStructuredToolCalls` so the reduction sees all of them.
 */
export function reconcileStructuredToolCall(
  wireName: string,
  restoredName: string,
  argumentsText: string,
  serializedText: string,
): StructuredToolCallReference {
  return reconcileStructuredToolCalls([{ wireName, restoredName, argumentsText }], serializedText)[0]!;
}

/**
 * Buffered-response counterpart of the streaming path, applied in place to the content events in
 * `events[start, end)`. It replays them through the same buffer the stream uses, so both paths
 * share one rule set: text carries its line and fence context across events (the inline-think
 * splitter may cut one answer into several), any other event keeps its place relative to held
 * text, and only text still held at the end is matched against the structured calls.
 */
export function reconcileSerializedToolCallEvents(
  events: AdapterEvent[],
  start: number,
  end: number,
  structuredCalls: readonly StructuredToolCallReference[],
  budget: TranslatorBudget,
): void {
  if (structuredCalls.length === 0) return;
  const buffer = new SerializedToolCallContentBuffer(budget);
  const reconciled: AdapterEvent[] = [];
  try {
    for (const event of events.slice(start, end)) {
      if (event.type !== "text_delta") { reconciled.push(...buffer.hold(event).filter(held => held.type !== "heartbeat")); continue; }
      const text = buffer.ingest(event.text);
      if (text.length > 0) reconciled.push({ type: "text_delta", text });
    }
    reconciled.push(...buffer.drain(structuredCalls));
  } finally {
    buffer.dispose();
  }
  events.splice(start, end - start, ...reconciled);
}

function textEvents(text: string): AdapterEvent[] {
  return text.length > 0 ? [{ type: "text_delta", text }] : [];
}
