File size: 24,714 Bytes
34810d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Proxy capture runtime coordinates capture sessions, proxy startup, and storage.
import { isUtf8 } from "node:buffer";
import { randomUUID } from "node:crypto";
import { URL } from "node:url";
import {
  isHeadersLike,
  normalizeRequestInitHeadersForFetch,
  type HeadersLike,
} from "../infra/fetch-headers.js";
import { withResponseBodyTimeout } from "../infra/http-response-body-timeout.js";
import {
  hasRegisteredSecretValuesForRedaction,
  redactRegisteredSecretValues,
} from "../logging/secret-redaction-registry.js";
import { resolveEnabledDebugProxySettings, type DebugProxySettings } from "./env.js";
import { redactedCaptureHeaders, REDACTED_CAPTURE_HEADER_VALUE } from "./header-redaction.js";
import {
  hasDebugProxyFetchPatch,
  registerDebugProxyFetchPatch,
  reportCapturePersistenceFailure,
  resolveCaptureOwner,
  resolveDebugProxyFetchTransport,
  resolveRuntimeDeps,
  uninstallDebugProxyGlobalFetchPatch,
  type CaptureOwner,
  type DebugProxyCaptureRuntimeDeps,
} from "./runtime-owner.js";
import { safeJsonString } from "./store.sqlite.js";
import type {
  CaptureDirection,
  CaptureEventKind,
  CaptureEventRecord,
  CaptureProtocol,
} from "./types.js";

export {
  finalizeDebugProxyCapture,
  isDebugProxyGlobalFetchPatchInstalled,
  resolveDebugProxyFetchTransport,
  type DebugProxyCaptureRuntimeDeps,
} from "./runtime-owner.js";

const REDACTED_CAPTURE_BINARY_PAYLOAD = Buffer.from("[REDACTED BINARY PAYLOAD]", "utf8");
// Cap captured response bodies so debug proxy capture cannot be turned into an
// out-of-memory vector. The patched global fetch tees every outbound response
// through clone(), so a single large (or hostile, effectively endless) provider
// response would otherwise be buffered fully into memory just to record it.
const MAX_CAPTURED_RESPONSE_BODY_BYTES = 16 * 1024 * 1024;
// The byte cap bounds how much a capture can buffer; this bounds how long it can
// wait for the next byte. Without it a remote that sends headers and then stalls
// keeps the capture branch of the clone() tee readable forever, and a tee branch
// only settles once both branches cancel or the source reaches EOF — so the
// caller's own cancellation, and the transport release that follows it, wait on
// a diagnostic read. Matches the idle bounds the shared body readers already
// take (src/infra/http-body.ts).
const CAPTURED_RESPONSE_BODY_IDLE_TIMEOUT_MS = 10_000;

/** Distinguishes the capture deadline from a genuine response-stream failure. */
class CaptureReadIdleTimeoutError extends Error {}

type CapturedResponseBodyResult =
  | { status: "captured"; buffer: Buffer }
  | { status: "stalled" | "finalized"; buffer: Buffer }
  | { status: "failed"; buffer: Buffer; error: unknown }
  | { status: "too-large" | "unavailable" };

// Reads a cloned capture response body under a byte cap. Oversized or
// non-streaming Response-like bodies return a metadata-only status instead of
// allocating the full body.
//
// Unlike media-core's readResponseWithLimit this never awaits reader.cancel():
// the body here is one branch of a Response.clone() tee whose sibling (the
// caller-facing response) is still live, and cancelling such a branch never
// settles (it only resolves once BOTH branches cancel). Awaiting it would hang
// the capture pipeline and retain the buffered prefix forever, so we cancel
// fire-and-forget, mirroring src/agents/tools/web-shared.ts#readResponseText.
function readCapturedResponseBodyBounded(
  response: Response,
  maxBytes: number,
  owner: CaptureOwner,
  record: (result: CapturedResponseBodyResult) => void,
  signal?: AbortSignal,
): void {
  let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
  let chunks: Buffer[] = [];
  let total = 0;
  let finished = false;
  let canceled = false;
  let detachAbort = () => {};
  const cancel = (reason?: unknown) => {
    if (!reader || canceled) {
      return;
    }
    canceled = true;
    // A clone is one tee branch: awaiting cancel can wait for the live caller.
    try {
      void reader.cancel(reason).catch(() => undefined);
    } catch (error) {
      owner.errors.push(error);
    }
  };
  const release = () => {
    try {
      reader?.releaseLock();
    } catch {
      // A pending read releases its lock when the canceled read settles.
    }
  };
  const finish = (result: CapturedResponseBodyResult) => {
    if (finished) {
      return;
    }
    finished = true;
    detachAbort();
    owner.pending.delete(finalize);
    try {
      if (owner.store.isClosed) {
        throw new Error("Capture store closed before its response could be finalized.");
      }
      record(result);
    } catch (error) {
      // Persistence failure is not a second stream error. Preserve it for close.
      reportCapturePersistenceFailure(owner, error);
    } finally {
      chunks = [];
      cancel();
      release();
    }
  };
  const finalize = () => finish({ status: "finalized", buffer: Buffer.concat(chunks, total) });
  owner.pending.add(finalize);
  if (signal) {
    const onAbort = () => {
      // Bun can deliver the caller's abort in the same turn as a clean body
      // EOF. Give the pending stream read one poll turn to record that EOF.
      setTimeout(() => {
        if (finished) {
          return;
        }
        finish({
          status: "failed",
          buffer: Buffer.concat(chunks, total),
          error:
            signal.reason instanceof Error
              ? signal.reason
              : new Error("Response capture aborted", { cause: signal.reason }),
        });
      }, 0);
    };
    if (signal.aborted) {
      onAbort();
    } else {
      signal.addEventListener("abort", onAbort, { once: true });
      detachAbort = () => signal.removeEventListener("abort", onAbort);
    }
  }
  if (finished) {
    return;
  }
  void (async () => {
    try {
      const clone = response.clone();
      const body = clone.body;
      if (!body || typeof body.getReader !== "function") {
        finish(
          clone instanceof Response && clone.body === null
            ? { status: "captured", buffer: Buffer.alloc(0) }
            : { status: "unavailable" },
        );
        return;
      }
      reader = body.getReader();
      for (;;) {
        if (finished || !owner.active) {
          return;
        }
        const { done, value } = await withResponseBodyTimeout({
          timeoutMs: CAPTURED_RESPONSE_BODY_IDLE_TIMEOUT_MS,
          onTimeout: ({ timeoutMs }) =>
            new CaptureReadIdleTimeoutError(`capture read stalled: no data for ${timeoutMs}ms`),
          cancel: async (error) => cancel(error),
          read: () => reader!.read(),
        });
        // Finalize may have synchronously recorded and closed this exact store.
        if (finished || !owner.active) {
          return;
        }
        if (done) {
          finish({ status: "captured", buffer: Buffer.concat(chunks, total) });
          return;
        }
        if (!value?.length) {
          continue;
        }
        if (total + value.length > maxBytes) {
          finish({ status: "too-large" });
          return;
        }
        chunks.push(Buffer.from(value));
        total += value.length;
      }
    } catch (error) {
      if (!finished && owner.active) {
        finish(
          error instanceof CaptureReadIdleTimeoutError
            ? { status: "stalled", buffer: Buffer.concat(chunks, total) }
            : { status: "failed", buffer: Buffer.concat(chunks, total), error },
        );
      }
    } finally {
      release();
    }
  })();
}

function parseDeclaredCaptureContentLength(raw: string | null | undefined): bigint | undefined {
  if (raw === null || raw === undefined) {
    return undefined;
  }
  const trimmed = raw.trim();
  if (!/^\d+$/.test(trimmed)) {
    return undefined;
  }
  return BigInt(trimmed);
}

function protocolFromUrl(rawUrl: string): CaptureProtocol {
  try {
    const url = new URL(rawUrl);
    switch (url.protocol) {
      case "https:":
        return "https";
      case "wss:":
        return "wss";
      case "ws:":
        return "ws";
      default:
        return "http";
    }
  } catch {
    return "http";
  }
}

function resolveUrlString(input: RequestInfo | URL): string | null {
  if (input instanceof URL) {
    return input.toString();
  }
  if (typeof input === "string") {
    return input;
  }
  if (typeof Request !== "undefined" && input instanceof Request) {
    return input.url;
  }
  return null;
}

function redactCaptureUrl(rawUrl: string): string {
  let url: URL;
  try {
    url = new URL(rawUrl);
  } catch {
    return "https://redacted.invalid/%5BREDACTED%5D";
  }
  const redactComponent = (value: string) =>
    redactRegisteredSecretValues(value, () => REDACTED_CAPTURE_HEADER_VALUE);
  const decodeComponent = (value: string) => {
    try {
      return decodeURIComponent(value);
    } catch {
      return value;
    }
  };
  if (redactComponent(url.hostname) !== url.hostname) {
    url.hostname = "redacted.invalid";
  }
  for (const key of ["username", "password"] as const) {
    const decoded = decodeComponent(url[key]);
    const redacted = redactComponent(decoded);
    if (redacted !== decoded) {
      url[key] = redacted;
    }
  }
  url.pathname = url.pathname
    .split("/")
    .map((segment) => {
      try {
        const decoded = decodeURIComponent(segment);
        const redacted = redactComponent(decoded);
        return redacted === decoded ? segment : encodeURIComponent(redacted);
      } catch {
        return segment;
      }
    })
    .join("/");
  const searchParams = new URLSearchParams();
  let searchChanged = false;
  for (const [name, value] of url.searchParams.entries()) {
    const redactedName = redactComponent(name);
    const redactedValue = redactComponent(value);
    searchParams.append(redactedName, redactedValue);
    if (redactedName !== name || redactedValue !== value) {
      searchChanged = true;
    }
  }
  if (searchChanged) {
    url.search = searchParams.toString();
  }
  const decodedHash = decodeComponent(url.hash.slice(1));
  const redactedHash = redactComponent(decodedHash);
  if (redactedHash !== decodedHash) {
    url.hash = redactedHash;
  }
  const serialized = url.toString();
  return redactComponent(serialized) === serialized
    ? serialized
    : `${url.protocol}//redacted.invalid/%5BREDACTED%5D`;
}

function redactCaptureText(value: string): string {
  return redactRegisteredSecretValues(value, () => REDACTED_CAPTURE_HEADER_VALUE);
}

function redactCapturePayload(value: string | Buffer | null | undefined): string | Buffer | null {
  if (typeof value === "string") {
    return redactCaptureText(value);
  }
  if (!Buffer.isBuffer(value)) {
    return value ?? null;
  }
  if (!isUtf8(value)) {
    // Binary frames can mix arbitrary bytes with credential text. Once any
    // resolved secret exists, omit their contents instead of guessing safely.
    return hasRegisteredSecretValuesForRedaction() ? REDACTED_CAPTURE_BINARY_PAYLOAD : value;
  }
  const text = value.toString("utf8");
  const redacted = redactCaptureText(text);
  return redacted === text ? value : Buffer.from(redacted, "utf8");
}

function redactedCaptureJson(
  value: unknown,
  stringify: typeof safeJsonString = safeJsonString,
): string | undefined {
  const serialized = stringify(value);
  return serialized === undefined ? undefined : redactCaptureText(serialized);
}

function createHttpCaptureEventBase(params: {
  settings: DebugProxySettings;
  rawUrl: string;
  url: URL;
  transport?: "http" | "sse";
  direction: CaptureDirection;
  kind: CaptureEventKind;
  flowId: string;
  method: string;
}): CaptureEventRecord {
  return {
    sessionId: params.settings.sessionId,
    ts: Date.now(),
    sourceScope: "openclaw",
    sourceProcess: params.settings.sourceProcess,
    protocol: params.transport ?? protocolFromUrl(params.rawUrl),
    direction: params.direction,
    kind: params.kind,
    flowId: params.flowId,
    method: params.method,
    host: params.url.host,
    path: `${params.url.pathname}${params.url.search}`,
  };
}

function installDebugProxyGlobalFetchPatch(
  owner: CaptureOwner,
  deps: DebugProxyCaptureRuntimeDeps = {},
): void {
  const runtime = resolveRuntimeDeps(deps);
  const admission = owner.admission;
  const fetchTarget = runtime.fetchTarget;
  if (typeof fetchTarget.fetch !== "function") {
    return;
  }
  if (hasDebugProxyFetchPatch(fetchTarget, admission)) {
    return;
  }
  uninstallDebugProxyGlobalFetchPatch(deps);
  // Patch only once per target and keep the original fetch for deterministic
  // teardown in tests and nested capture sessions.
  const fetchImpl = fetchTarget.fetch;
  const originalFetch = resolveDebugProxyFetchTransport(fetchImpl).bind(fetchTarget);
  const patchedFetch = async (input: RequestInfo | URL, init?: RequestInit) => {
    const url = resolveUrlString(input);
    const normalizedInit = normalizeRequestInitHeadersForFetch(init);
    // Retain admission before awaiting transport; a late result cannot join a
    // replacement capture session or reopen a store closed during shutdown.
    const admitted = Boolean(admission.current);
    let response: Response;
    try {
      response = await originalFetch(input, normalizedInit);
    } catch (error) {
      const current = admission.current;
      if (admitted && current && url && /^https?:/i.test(url)) {
        captureOwnedHttpError(
          {
            url,
            method:
              (typeof Request !== "undefined" && input instanceof Request
                ? input.method
                : undefined) ??
              normalizedInit?.method ??
              "GET",
            error,
            meta: { captureOrigin: "global-fetch" },
          },
          current,
        );
      }
      throw error;
    }
    const current = admission.current;
    if (admitted && current && url && /^https?:/i.test(url)) {
      captureOwnedHttpExchange(
        {
          url,
          method:
            (typeof Request !== "undefined" && input instanceof Request
              ? input.method
              : undefined) ??
            normalizedInit?.method ??
            "GET",
          requestHeaders:
            (typeof Request !== "undefined" && input instanceof Request
              ? input.headers
              : undefined) ??
            (normalizedInit?.headers as Headers | Record<string, string> | undefined),
          requestBody:
            (typeof Request !== "undefined" && input instanceof Request
              ? (input as Request & { body?: BodyInit | null }).body
              : undefined) ??
            (normalizedInit as (RequestInit & { body?: BodyInit | null }) | undefined)?.body ??
            null,
          response,
          transport: "http",
          meta: {
            captureOrigin: "global-fetch",
            source: current.settings.sourceProcess,
          },
        },
        current,
      );
    }
    return response;
  };
  const mockState = (fetchImpl as typeof globalThis.fetch & { mock?: unknown }).mock;
  if (typeof mockState === "object" && mockState !== null) {
    // Preserve Vitest mock metadata when patching mocked fetch targets.
    (patchedFetch as typeof globalThis.fetch & { mock?: unknown }).mock = mockState;
  }
  registerDebugProxyFetchPatch(fetchTarget, originalFetch, patchedFetch, admission);
}

export function initializeDebugProxyCapture(
  mode: string,
  resolved?: DebugProxySettings,
  deps: DebugProxyCaptureRuntimeDeps = {},
): void {
  const settings = resolveEnabledDebugProxySettings(resolved);
  if (!settings) {
    return;
  }
  const owner = resolveCaptureOwner(settings, resolveRuntimeDeps(deps), {
    initialize: true,
    explicit: resolved !== undefined,
  });
  if (!owner) {
    return;
  }
  owner.store.upsertSession({
    id: settings.sessionId,
    startedAt: Date.now(),
    mode,
    sourceScope: "openclaw",
    sourceProcess: settings.sourceProcess,
    proxyUrl: settings.proxyUrl,
  });
  installDebugProxyGlobalFetchPatch(owner, deps);
}

type HttpCaptureParams = {
  url: string;
  method: string;
  requestHeaders?: HeadersLike | Record<string, string> | undefined;
  requestBody?: BodyInit | Buffer | string | null;
  response: Response;
  signal?: AbortSignal;
  transport?: "http" | "sse";
  flowId?: string;
  meta?: Record<string, unknown>;
};

type HttpCaptureErrorParams = Omit<HttpCaptureParams, "response"> & { error: unknown };

/** Internal fetch seams retain this admission before awaiting network work. */
export function prepareHttpCapture(
  resolved?: DebugProxySettings,
  deps: DebugProxyCaptureRuntimeDeps = {},
) {
  const settings = resolveEnabledDebugProxySettings(resolved);
  if (!settings) {
    return undefined;
  }
  const admission = resolveCaptureOwner(settings, resolveRuntimeDeps(deps), {
    explicit: resolved !== undefined,
  })?.admission;
  return admission
    ? (params: HttpCaptureParams | HttpCaptureErrorParams) => {
        if (admission.current) {
          if ("response" in params) {
            captureOwnedHttpExchange(params, admission.current);
          } else {
            captureOwnedHttpError(params, admission.current);
          }
        }
      }
    : undefined;
}

export function captureHttpExchange(
  params: HttpCaptureParams,
  resolved?: DebugProxySettings,
  deps: DebugProxyCaptureRuntimeDeps = {},
): void {
  prepareHttpCapture(resolved, deps)?.(params);
}

function captureOwnedHttpError(params: HttpCaptureErrorParams, owner: CaptureOwner): void {
  try {
    const captureUrl = redactCaptureUrl(params.url);
    owner.store.recordEvent({
      ...createHttpCaptureEventBase({
        settings: owner.settings,
        rawUrl: captureUrl,
        url: new URL(captureUrl),
        transport: params.transport,
        direction: "local",
        kind: "error",
        flowId: params.flowId ?? randomUUID(),
        method: params.method,
      }),
      errorText: redactCaptureText(
        params.error instanceof Error ? params.error.message : String(params.error),
      ),
      metaJson: redactedCaptureJson(params.meta, owner.runtime.safeJsonString),
    });
  } catch (error) {
    // Diagnostic persistence cannot replace the caller's transport rejection.
    reportCapturePersistenceFailure(owner, error);
  }
}

function captureOwnedHttpExchange(params: HttpCaptureParams, owner: CaptureOwner): void {
  const { settings, runtime, store } = owner;
  const flowId = params.flowId ?? randomUUID();
  const captureUrl = redactCaptureUrl(params.url);
  const url = new URL(captureUrl);
  const requestBody =
    typeof params.requestBody === "string" || Buffer.isBuffer(params.requestBody)
      ? params.requestBody
      : null;
  const rawRequestContentType = params.requestHeaders
    ? isHeadersLike(params.requestHeaders)
      ? (params.requestHeaders.get("content-type") ?? undefined)
      : params.requestHeaders["content-type"]
    : undefined;
  const requestContentType =
    rawRequestContentType === undefined ? undefined : redactCaptureText(rawRequestContentType);
  const rawResponseContentType =
    typeof params.response.headers?.get === "function"
      ? (params.response.headers.get("content-type") ?? undefined)
      : undefined;
  const responseContentType =
    rawResponseContentType === undefined ? undefined : redactCaptureText(rawResponseContentType);
  try {
    const requestPayload = runtime.persistEventPayload(store, {
      data: redactCapturePayload(requestBody),
      contentType: requestContentType,
    });
    store.recordEvent({
      ...createHttpCaptureEventBase({
        settings,
        rawUrl: captureUrl,
        url,
        transport: params.transport,
        direction: "outbound",
        kind: "request",
        flowId,
        method: params.method,
      }),
      contentType: requestContentType,
      headersJson: runtime.safeJsonString(
        redactedCaptureHeaders(
          params.requestHeaders,
          Array.isArray(params.meta?.sensitiveRequestHeaderNames)
            ? params.meta.sensitiveRequestHeaderNames.filter(
                (name): name is string => typeof name === "string",
              )
            : undefined,
        ),
      ),
      metaJson: redactedCaptureJson(params.meta, runtime.safeJsonString),
      ...requestPayload,
    });
  } catch (error) {
    reportCapturePersistenceFailure(owner, error);
    return;
  }
  const recordTerminal = (result: CapturedResponseBodyResult) => {
    const failed = result.status === "failed";
    // Join first, then redact: secrets and UTF-8 code points can cross chunks.
    const payload =
      "buffer" in result
        ? runtime.persistEventPayload(store, {
            data: redactCapturePayload(result.buffer),
            contentType: responseContentType,
          })
        : {};
    store.recordEvent({
      ...createHttpCaptureEventBase({
        settings,
        rawUrl: captureUrl,
        url,
        transport: params.transport,
        direction: failed ? "local" : "inbound",
        kind: failed ? "error" : "response",
        flowId,
        method: params.method,
      }),
      status: params.response.status,
      contentType: responseContentType,
      headersJson:
        params.response.headers && typeof params.response.headers.entries === "function"
          ? runtime.safeJsonString(redactedCaptureHeaders(params.response.headers))
          : undefined,
      errorText: failed
        ? redactCaptureText(
            result.error instanceof Error ? result.error.message : String(result.error),
          )
        : undefined,
      metaJson: redactedCaptureJson(
        result.status === "captured"
          ? params.meta
          : {
              ...params.meta,
              bodyCapture: result.status,
              ...(failed ? { stage: "response-body" } : {}),
            },
        runtime.safeJsonString,
      ),
      ...payload,
    });
  };
  const recordMetadata = (status: "unavailable" | "too-large") => {
    try {
      recordTerminal({ status });
    } catch (error) {
      reportCapturePersistenceFailure(owner, error);
    }
  };
  if (typeof params.response.clone !== "function") {
    // Some Response-like objects cannot be cloned. Still record status/headers
    // rather than forcing capture to consume or mutate the original response.
    recordMetadata("unavailable");
    return;
  }
  // Fast path: when the provider declares an oversized Content-Length, skip the
  // body entirely instead of buffering it. Missing/chunked lengths fall through
  // to the bounded streaming read below, which cancels on overflow.
  const declaredLength = parseDeclaredCaptureContentLength(
    typeof params.response.headers?.get === "function"
      ? params.response.headers.get("content-length")
      : undefined,
  );
  if (declaredLength !== undefined && declaredLength > BigInt(MAX_CAPTURED_RESPONSE_BODY_BYTES)) {
    recordMetadata("too-large");
    return;
  }
  readCapturedResponseBodyBounded(
    params.response,
    MAX_CAPTURED_RESPONSE_BODY_BYTES,
    owner,
    recordTerminal,
    params.signal,
  );
}

// Websocket seams call this directly because Node fetch patching cannot observe
// frame traffic.
export function captureWsEvent(
  params: {
    url: string;
    direction: "outbound" | "inbound" | "local";
    kind: "ws-open" | "ws-frame" | "ws-close" | "error";
    flowId: string;
    payload?: string | Buffer;
    closeCode?: number;
    errorText?: string;
    meta?: Record<string, unknown>;
  },
  resolved?: DebugProxySettings,
  deps: DebugProxyCaptureRuntimeDeps = {},
): void {
  const settings = resolveEnabledDebugProxySettings(resolved);
  if (!settings) {
    return;
  }
  const owner = resolveCaptureOwner(settings, resolveRuntimeDeps(deps), {
    explicit: resolved !== undefined,
  });
  if (!owner) {
    return;
  }
  const { runtime, store } = owner;
  const captureUrl = redactCaptureUrl(params.url);
  const url = new URL(captureUrl);
  const payload = runtime.persistEventPayload(store, {
    data: redactCapturePayload(params.payload),
    contentType: "application/json",
  });
  store.recordEvent({
    sessionId: settings.sessionId,
    ts: Date.now(),
    sourceScope: "openclaw",
    sourceProcess: settings.sourceProcess,
    protocol: protocolFromUrl(captureUrl),
    direction: params.direction,
    kind: params.kind,
    flowId: params.flowId,
    host: url.host,
    path: `${url.pathname}${url.search}`,
    closeCode: params.closeCode,
    errorText: params.errorText === undefined ? undefined : redactCaptureText(params.errorText),
    metaJson: redactedCaptureJson(params.meta, runtime.safeJsonString),
    ...payload,
  });
}