File size: 24,030 Bytes
67d18ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
 * Async bridge: core state machine turning sync client stream expectation
 * into off-peak async reality. See `.omo/plans/async-off-peak-bridge.md` Β§3.
 *
 * State machine:
 *   WAIT   (queued)            β†’ poll every pollIntervalMs (signal-aware); emit keepalives
 *   READY  (ready/active)      β†’ forward LLM call upstream with X-Off-Peak-Ticket-ID
 *   EXPIRED (ticket revoked)   β†’ settleOnce(old); if attempt < maxRetries take new ticket β†’ WAIT; else terminal error
 *   DONE   (stream completed)  β†’ settleOnce(current); close stream
 *   ABORT  (client disconnect) β†’ settleOnce(current); release resources
 *
 * Anti-patterns enforced (see plan Β§9):
 *   #1 β€” pure `: keepalive\n\n` comments during wait; no custom data events
 *   #2 β€” every takeTicket matched by exactly one settleOnce (idempotent)
 *   #3 β€” retry resends original prompt (no NOe placeholder)
 *   #5 β€” settle is fire-and-forget, never blocks stream closure
 *
 * Output: homogeneous byte stream (keepalives + upstream Anthropic bytes + terminal error events).
 */
import type { OffPeakClient } from "./client.js";
import type { OffPeakCredentials, TakeTicketResult, TicketState } from "./types.js";
import { isTicketExpired, isTicketReady } from "./types.js";
import { keepaliveFrame } from "./keepalive.js";
import { buildIdentityHeaders } from "../proxy/identity.js";
import type { ProxyIdentity } from "../config/types.js";

const EXPIRED_MARKER = "off-peak-ticket-expired";

export interface BridgeOptions {
  client: OffPeakClient;
  credentials: OffPeakCredentials;
  origin: string;
  identity: ProxyIdentity;
  llmRequestBody: string;
  initialTicket: TakeTicketResult;
  taskId: string;
  pollIntervalMs: number;
  keepAliveIntervalMs: number;
  maxRetries: number;
  /** Total wait cap across ALL retries (not per-ticket). 0 = unlimited. */
  maxWaitMs: number;
  clientSignal?: AbortSignal;
  fetchImpl?: (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
  onTransition?: (info: BridgeTransition) => void;
}

export interface BridgeTransition {
  phase: "wait" | "ready" | "expired" | "done" | "abort" | "error";
  attempt: number;
  ticketId: string;
  state?: TicketState;
  message?: string;
}

export interface BridgeOutcome {
  attempts: number;
  finalTicketId: string;
  terminalPhase: BridgeTransition["phase"];
}

export function runAsyncBridge(opts: BridgeOptions): { stream: ReadableStream<Uint8Array>; outcome: Promise<BridgeOutcome> } {
  const fetchImpl = opts.fetchImpl ?? globalThis.fetch;
  const encoder = new TextEncoder();
  let outcomeResolve!: (o: BridgeOutcome) => void;
  const outcome = new Promise<BridgeOutcome>((r) => {
    outcomeResolve = r;
  });
  let outcomeResolved = false;
  function resolveOutcome(o: BridgeOutcome): void {
    if (outcomeResolved) return;
    outcomeResolved = true;
    outcomeResolve(o);
  }

  let aborted = false;
  const settledTickets = new Set<string>();
  function settleOnce(ticketId: string): void {
    if (settledTickets.has(ticketId)) return;
    settledTickets.add(ticketId);
    void opts.client.settle(ticketId).catch(() => {
      // best-effort; settle failure is non-fatal
    });
  }

  function log(info: BridgeTransition): void {
    try {
      opts.onTransition?.(info);
    } catch {
      // observability hook must never break the bridge
    }
  }

  function emitKeepalive(controller: ReadableStreamDefaultController<Uint8Array>): void {
    if (aborted) return;
    try {
      controller.enqueue(keepaliveFrame());
    } catch {
      // controller closed by consumer
    }
  }

  function emitTerminalError(controller: ReadableStreamDefaultController<Uint8Array>, message: string, type: string = "api_error"): void {
    if (aborted) return;
    const payload = JSON.stringify({ type: "error", error: { type, message } });
    try {
      controller.enqueue(encoder.encode(`event: error\ndata: ${payload}\n\n`));
    } catch {
      // already closed
    }
  }

  /**
   * Wait until ticket becomes ready/active OR expired/not_found OR abort OR total maxWaitMs exceeded.
   * `settled` and unknown states are treated as expired (server can't dispatch this ticket again).
   * Polls respect clientSignal for immediate cancellation.
   *
   * `maxWaitExceeded: true` marks a CLIENT-SIDE deadline overrun (distinct from
   * a server-side ticket expiry) so the caller terminates with a timeout error
   * instead of misreading it as "ticket expired" and burning retakes.
   */
  async function waitForReady(ticketId: string, deadlineAt: number): Promise<{ state: TicketState; maxWaitExceeded?: boolean }> {
    while (true) {
      if (aborted) return { state: "expired" as TicketState };
      const result = await pollBatchStatusWithRetry(ticketId);
      if (aborted) return { state: "expired" as TicketState };
      const ticket = result.tickets[0];
      if (!ticket) return { state: "not_found" as TicketState };
      if (isTicketReady(ticket.state)) return { state: ticket.state };
      // `settled`, `expired`, `not_found`, or any unknown state β†’ unusable, treat as expired
      if (ticket.state !== "queued") return { state: "expired" as TicketState };

      const delay = result.nextPollAfterMs ?? opts.pollIntervalMs;
      if (opts.maxWaitMs > 0 && Date.now() + delay > deadlineAt) {
        // The NEXT poll would overrun the total wait budget. Sleep out the
        // remainder and report a deadline overrun (NOT "expired" β€” an early
        // `expired` here used to send the caller into the retake loop against
        // the same deadline until maxRetries burned out).
        await sleep(Math.max(0, deadlineAt - Date.now()), opts.clientSignal);
        return { state: "expired" as TicketState, maxWaitExceeded: true };
      }
      await sleep(delay, opts.clientSignal);
    }
  }

  /**
   * Single batchStatus poll with local tolerance for transient control-plane
   * errors: up to POLL_RETRY_LIMIT consecutive failures (backoff
   * min(pollIntervalMs, 2s) between them) before the error escapes. A single
   * network blip must not kill an hours-long queue wait β€” the sync path
   * (proxy/handler.ts connect-retry) retries the same class of failure.
   * Abandon immediately (no retry) once the client has aborted.
   */
  async function pollBatchStatusWithRetry(ticketId: string): Promise<Awaited<ReturnType<OffPeakClient["batchStatus"]>>> {
    const POLL_RETRY_LIMIT = 3;
    const retryDelayMs = Math.min(opts.pollIntervalMs, 2000);
    let lastErr: unknown;
    for (let attempt = 1; attempt <= POLL_RETRY_LIMIT; attempt++) {
      if (aborted || opts.clientSignal?.aborted) throw lastErr ?? new Error("client aborted during poll");
      try {
        return await opts.client.batchStatus([ticketId], opts.clientSignal);
      } catch (err) {
        lastErr = err;
        // Client disconnected mid-poll β€” retrying would delay the abort path.
        if (aborted || opts.clientSignal?.aborted) throw err;
        if (attempt < POLL_RETRY_LIMIT) await sleep(retryDelayMs, opts.clientSignal);
      }
    }
    throw lastErr;
  }

  async function forwardLLM(ticketId: string): Promise<{ response: Response; expiredInBody: boolean }> {
    const url = `${opts.origin.replace(/\/+$/, "")}/api/v1/off-peak/anthropic/v1/messages`;
    const headers: Record<string, string> = {
      "content-type": "application/json",
      authorization: `Bearer ${opts.credentials.jwt}`,
      "x-coding-plan-api-key": opts.credentials.codingPlanApiKey,
      "x-off-peak-ticket-id": ticketId,
      ...buildIdentityHeaders(opts.identity),
    };
    if (opts.credentials.bigmodelOrganization) headers["bigmodel-organization"] = opts.credentials.bigmodelOrganization;
    if (opts.credentials.bigmodelProject) headers["bigmodel-project"] = opts.credentials.bigmodelProject;

    const resp = await fetchImpl(url, {
      method: "POST",
      headers,
      body: opts.llmRequestBody,
      signal: opts.clientSignal,
    });

    if (!resp.ok) {
      const bodyText = await resp.text().catch(() => "");
      if (bodyText.includes(EXPIRED_MARKER)) {
        return { response: new Response(bodyText, { status: resp.status, headers: resp.headers }), expiredInBody: true };
      }
      return { response: new Response(bodyText, { status: resp.status, headers: resp.headers }), expiredInBody: false };
    }
    return { response: resp, expiredInBody: false };
  }

  const stream = new ReadableStream<Uint8Array>({
    async start(controller) {
      // External signal β†’ local aborted flag. Both paths (consumer cancel + signal abort) converge here.
      if (opts.clientSignal) {
        if (opts.clientSignal.aborted) aborted = true;
        else opts.clientSignal.addEventListener("abort", () => { aborted = true; }, { once: true });
      }

      // Keepalive runs ONLY during WAIT/retry phases; stopped before LLM streaming to
      // avoid inserting `: keepalive\n\n` inside a partial SSE frame split across chunks.
      let keepAliveTimer: ReturnType<typeof setInterval> | undefined;
      function startKeepalive(): void {
        if (keepAliveTimer) return;
        keepAliveTimer = setInterval(() => emitKeepalive(controller), opts.keepAliveIntervalMs);
      }
      function stopKeepalive(): void {
        if (keepAliveTimer) {
          clearInterval(keepAliveTimer);
          keepAliveTimer = undefined;
        }
      }

      // Total wait deadline across all retries (B11: was per-ticket, leaked to (maxRetries+1)*maxWaitMs).
      const requestStartedAt = Date.now();
      const deadlineAt = opts.maxWaitMs > 0 ? requestStartedAt + opts.maxWaitMs : Number.MAX_SAFE_INTEGER;

      let currentTicket = opts.initialTicket;
      let attempt = 0;

      try {
        while (true) {
          if (aborted) {
            log({ phase: "abort", attempt, ticketId: currentTicket.ticketId });
            settleOnce(currentTicket.ticketId);
            return;
          }

          // WAIT phase
          if (!isTicketReady(currentTicket.state)) {
            log({ phase: "wait", attempt, ticketId: currentTicket.ticketId, state: currentTicket.state });
            startKeepalive();
            const wait = await waitForReady(currentTicket.ticketId, deadlineAt);
            if (aborted) {
              stopKeepalive();
              log({ phase: "abort", attempt, ticketId: currentTicket.ticketId });
              settleOnce(currentTicket.ticketId);
              return;
            }
            if (wait.maxWaitExceeded) {
              // CL-02: client-side maxWaitMs overrun β€” terminal timeout, NOT a
              // server expiry. Retaking would just re-wait against the same
              // (already blown) deadline until maxRetries exhausts.
              stopKeepalive();
              settleOnce(currentTicket.ticketId);
              emitTerminalError(controller, `async max wait timeout (${opts.maxWaitMs}ms exceeded)`, "timeout");
              resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
              return;
            }
            if (isTicketExpired(wait.state)) {
              stopKeepalive();
              settleOnce(currentTicket.ticketId);
              // Check if this was a client-side maxWaitMs timeout (not server expiration).
              // Distinguish the error so users don't chase server-side ticket issues.
              if (opts.maxWaitMs > 0 && Date.now() >= deadlineAt) {
                emitTerminalError(controller, `async max wait timeout (${opts.maxWaitMs}ms exceeded)`, "timeout");
                resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
                return;
              }
              log({ phase: "expired", attempt, ticketId: currentTicket.ticketId, state: wait.state, message: "expired in queue" });
              attempt++;
              if (attempt > opts.maxRetries) {
                emitTerminalError(controller, `async upstream exhausted retries (ticket expired in queue ${attempt}x)`, "api_error");
                resolveOutcome({ attempts: attempt, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
                return;
              }
              try {
                currentTicket = await opts.client.takeTicket(opts.taskId, opts.clientSignal);
              } catch (takeErr) {
                emitTerminalError(controller, `async retake failed: ${(takeErr as Error).message}`, "api_error");
                resolveOutcome({ attempts: attempt, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
                return;
              }
              continue;
            }
            stopKeepalive();
          }

          // READY phase β€” NO keepalive (would corrupt SSE frames split across chunks)
          log({ phase: "ready", attempt, ticketId: currentTicket.ticketId });
          let resp: Response;
          let expiredInBody: boolean;
          try {
            const forward = await forwardLLM(currentTicket.ticketId);
            resp = forward.response;
            expiredInBody = forward.expiredInBody;
          } catch (err) {
            if (aborted) {
              settleOnce(currentTicket.ticketId);
              return;
            }
            emitTerminalError(controller, `async upstream network error: ${(err as Error).message}`, "api_error");
            settleOnce(currentTicket.ticketId);
            resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
            return;
          }

          if (!resp.ok || expiredInBody) {
            const bodyText = await resp.text().catch(() => "");
            if (expiredInBody || bodyText.includes(EXPIRED_MARKER)) {
              log({ phase: "expired", attempt, ticketId: currentTicket.ticketId, message: "expired during LLM (pre-stream)" });
              settleOnce(currentTicket.ticketId);
              attempt++;
              if (attempt > opts.maxRetries) {
                emitTerminalError(controller, `async upstream exhausted retries (ticket expired during LLM ${attempt}x)`, "api_error");
                resolveOutcome({ attempts: attempt, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
                return;
              }
              try {
                currentTicket = await opts.client.takeTicket(opts.taskId, opts.clientSignal);
              } catch (takeErr) {
                emitTerminalError(controller, `async retake failed: ${(takeErr as Error).message}`, "api_error");
                resolveOutcome({ attempts: attempt, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
                return;
              }
              continue;
            }
            // Non-expired upstream error β†’ emit standard Anthropic error event.
            // Sanitized message: status only; body omitted to avoid leaking upstream internals.
            emitTerminalError(controller, `async upstream HTTP ${resp.status}`, "api_error");
            settleOnce(currentTicket.ticketId);
            resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
            return;
          }

          // Stream upstream body with complete-frame buffering.
          //
          // DESIGN (fixes R3-2/R3-3/R4-1/R4-3 + bounds memory R4-5):
          //   - One TextDecoder for the whole streaming phase (handles partial
          //     multibyte UTF-8 correctly across chunk boundaries).
          //   - Accumulate decoded text until a complete SSE frame (`\n\n` boundary).
          //   - Scan the COMPLETE frame for the expired marker before forwarding.
          //     This catches markers split across any number of network chunks.
          //   - Forward via `encoder.encode(frame)` β€” re-encoding Unicode codepoints
          //     through UTF-16β†’UTF-8 is lossless for valid input, and avoids the
          //     raw-vs-decoded byte mismatch that previously dropped pre-commit
          //     chunks when the boundary spanned chunks.
          //   - Cap `pending` at MAX_PENDING_FRAME_BYTES to prevent a malicious
          //     upstream from exhausting memory by streaming without `\n\n`.
          //
          // State machine:
          //   Pre-commit (no frame forwarded yet): marker found β†’ transparent retry.
          //   Post-commit (β‰₯1 frame forwarded): marker found β†’ terminal error
          //     (cannot retry β€” client already saw message_start; retrying would
          //     produce duplicate lifecycle events).
          let midStreamExpiredPreCommit = false;
          let midStreamExpiredPostCommit = false;
          let committed = false;
          if (resp.body) {
            const reader = resp.body.getReader();
            const streamDecoder = new TextDecoder();
            let pending = "";
            const MAX_PENDING_FRAME_BYTES = 1 * 1024 * 1024;
            try {
              while (true) {
                if (aborted) {
                  await reader.cancel().catch(() => {});
                  settleOnce(currentTicket.ticketId);
                  return;
                }
                const { done, value } = await reader.read();
                if (done) {
                  // SSE spec: discard pending data at EOF if it lacks a terminating
                  // blank line (incomplete event). Only flush if marker-free AND
                  // properly terminated.
                  if (pending.length > 0 && !aborted && !midStreamExpiredPreCommit && !midStreamExpiredPostCommit) {
                    if (pending.includes(EXPIRED_MARKER)) {
                      if (!committed) midStreamExpiredPreCommit = true;
                      else midStreamExpiredPostCommit = true;
                    } else if (pending.endsWith("\n\n")) {
                      // Properly terminated final frame β€” safe to forward.
                      try { controller.enqueue(encoder.encode(pending)); } catch {}
                    }
                    // else: unterminated event at EOF β€” discard per SSE spec.
                    pending = "";
                  }
                  break;
                }
                if (aborted) break;
                pending += streamDecoder.decode(value, { stream: true }).replace(/\r\n/g, "\n").replace(/\r/g, "\n");

                // Bounded buffer: if pending exceeds the cap without a frame boundary,
                // the upstream is malformed or malicious β€” terminate.
                if (pending.length > MAX_PENDING_FRAME_BYTES) {
                  await reader.cancel().catch(() => {});
                  emitTerminalError(controller, "async upstream SSE frame exceeded 1 MiB boundary", "api_error");
                  settleOnce(currentTicket.ticketId);
                  resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
                  return;
                }

                // Process all complete frames in `pending`.
                let frameBoundaryFound = false;
                while (true) {
                  const idx = pending.indexOf("\n\n");
                  if (idx === -1) break;
                  const frame = pending.slice(0, idx + 2);
                  pending = pending.slice(idx + 2);

                  if (frame.includes(EXPIRED_MARKER)) {
                    if (!committed) {
                      midStreamExpiredPreCommit = true;
                    } else {
                      midStreamExpiredPostCommit = true;
                    }
                    await reader.cancel().catch(() => {});
                    frameBoundaryFound = true;
                    break;
                  }
                  try {
                    controller.enqueue(encoder.encode(frame));
                    committed = true;
                  } catch {
                    await reader.cancel().catch(() => {});
                    frameBoundaryFound = true;
                    break;
                  }
                }
                if (midStreamExpiredPreCommit || midStreamExpiredPostCommit || frameBoundaryFound) break;
              }
              // Flush decoder (no stream flag) to release internal partial-byte state.
              streamDecoder.decode();
            } finally {
              reader.releaseLock?.();
            }
          }

          if (midStreamExpiredPreCommit && !aborted) {
            log({ phase: "expired", attempt, ticketId: currentTicket.ticketId, message: "expired during LLM (mid-stream, pre-commit)" });
            settleOnce(currentTicket.ticketId);
            attempt++;
            if (attempt > opts.maxRetries) {
              emitTerminalError(controller, `async upstream exhausted retries (ticket expired mid-stream ${attempt}x)`, "api_error");
              resolveOutcome({ attempts: attempt, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
              return;
            }
            try {
              currentTicket = await opts.client.takeTicket(opts.taskId, opts.clientSignal);
            } catch (takeErr) {
              emitTerminalError(controller, `async retake failed: ${(takeErr as Error).message}`, "api_error");
              resolveOutcome({ attempts: attempt, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
              return;
            }
            continue;
          }

          if (midStreamExpiredPostCommit && !aborted) {
            // Cannot retry transparently β€” client already saw message_start.
            // Emit terminal error and close.
            log({ phase: "error", attempt, ticketId: currentTicket.ticketId, message: "expired mid-stream after commit; cannot retry" });
            settleOnce(currentTicket.ticketId);
            emitTerminalError(controller, "async upstream ticket expired mid-stream after output started", "api_error");
            resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
            return;
          }

          if (aborted) {
            log({ phase: "abort", attempt, ticketId: currentTicket.ticketId });
            settleOnce(currentTicket.ticketId);
            resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "abort" });
            return;
          }

          log({ phase: "done", attempt, ticketId: currentTicket.ticketId });
          settleOnce(currentTicket.ticketId);
          resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "done" });
          return;
        }
      } catch (err) {
        if (!aborted) {
          emitTerminalError(controller, `async bridge internal error: ${(err as Error).message}`, "api_error");
        }
        settleOnce(currentTicket.ticketId);
        resolveOutcome({ attempts: attempt + 1, finalTicketId: currentTicket.ticketId, terminalPhase: "error" });
      } finally {
        stopKeepalive();
        try {
          controller.close();
        } catch {
          // already closed
        }
        // Abort paths return without calling resolveOutcome; resolve here as a fallback
        // so `await outcome` never hangs. Idempotent β€” explicit calls above take precedence.
        resolveOutcome({
          attempts: attempt + 1,
          finalTicketId: currentTicket.ticketId,
          terminalPhase: aborted ? "abort" : "error",
        });
      }
    },

    cancel() {
      aborted = true;
    },
  });

  return { stream, outcome };
}

function sleep(ms: number, signal?: AbortSignal): Promise<void> {
  return new Promise((resolve) => {
    if (signal?.aborted) {
      resolve();
      return;
    }
    const t = setTimeout(() => {
      signal?.removeEventListener("abort", onAbort);
      resolve();
    }, ms);
    const onAbort = (): void => {
      clearTimeout(t);
      resolve();
    };
    signal?.addEventListener("abort", onAbort, { once: true });
  });
}