/** * FEAT-458 S3 (ADR-013 Decision 4): the tee sink's only inputs are BODY * events — a raw `Buffer` chunk, or an end-of-stream marker for one * direction. There is no field here a future edit could widen to carry * headers without changing this type's shape at the call site below; the * exclusion is structural, not a filtered allow-list. `dir` says which leg * of the exchange the bytes belong to, nothing about the request/response * objects themselves is ever exposed. */ export type ProxyTeeEvent = { chunk: Buffer; } | { end: true; }; export type ProxyTeeSink = (dir: 'req' | 'res', event: ProxyTeeEvent) => void; /** * FEAT-458 S4 (ADR-013 Decision 2, "proxy down ≠ agent down"): a silently * hanging upstream — no connection error, just nothing ever happens — is the * one failure mode the S2 forward path left completely unhandled. Every * other upstream failure (refused, DNS, TLS, 4xx/5xx) surfaces an 'error' or * a real response on its own; a hang produces neither, so it needs an * explicit timer. 30s is generous enough for a real provider's * time-to-first-byte under normal load while still bounding how long one * stuck request can occupy a socket. */ export declare const DEFAULT_UPSTREAM_TIMEOUT_MS = 30000; export interface ProxyServeOpts { port: number; host: string; upstream: string; /** * Optional factory, called once per incoming request (never per server * start) so each exchange's tee state (buffered chunks, end-of-stream * flags) is isolated from every other concurrent request — a single * shared sink would corrupt interleaved captures under concurrent load. * Any cross-request state (e.g. a relevance-gate dedup window) belongs in * the closure that produces this factory, one level up, in the capture * module itself. Undefined (the default) means capture is off: zero * listeners are attached and the forward path is identical to S2. */ createTee?: () => ProxyTeeSink; /** * FEAT-458 S4 — per-request idle timeout applied to the upstream leg only * (never the client leg). `undefined` uses `DEFAULT_UPSTREAM_TIMEOUT_MS`; * `0` or a negative value disables the timeout entirely (matches the * escape-hatch shape other `timeoutMs` config surfaces in this repo use, * e.g. `crossEncoder.timeoutMs`). */ upstreamTimeoutMs?: number; } export interface ProxyHandle { readonly port: number; readonly host: string; close(): Promise; } /** * Start the core proxy: bind `host:port`, forward every request to * `upstream` byte-transparently. Callers (the CLI layer) are responsible for * refusing a non-loopback `host` before calling this — this function does * not itself validate the host, so it stays usable in tests that * intentionally probe non-default bind targets without going through * process.exit semantics. `opts.createTee` (FEAT-458 S3) is fully optional * and additive: omitting it reproduces S2's exact capture-free behavior. */ export declare function startProxyServer(opts: ProxyServeOpts): Promise;