Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(bench): correct meter-proxy benchmark imports

Steven Enamakel1dc4c8d8e8cd
diff --git a/bench/harnesses/meter-proxy/headers.mjs b/bench/harnesses/meter-proxy/headers.mjs
new file mode 100644
index 0000000000..1c199d0098
--- /dev/null
+++ b/bench/harnesses/meter-proxy/headers.mjs
@@ -0,0 +1,45 @@
+// headers.mjs — what the proxy forwards upstream.
+//
+// Provider prompt caches key on more than the body: session-affinity headers
+// (x-session-id, ...), user-agent, anthropic-beta / anthropic-version and
+// OpenRouter's HTTP-Referer / X-Title can all steer routing, and routing decides
+// which backend's cache is hit. So every header the harness sent is forwarded
+// exactly as received (name casing, order, duplicates) except the few that must
+// change for the hop to work:
+// host -> the upstream host
+// content-length -> the (possibly rewritten) body length
+// hop-by-hop -> per RFC 9110 they describe one connection, not the request
+// credentials -> swapped for the real key (harnesses only carry a dummy)
+
+const HOP_BY_HOP = new Set([
+ "connection", "keep-alive", "proxy-connection", "transfer-encoding", "te",
+ "trailer", "upgrade", "proxy-authenticate", "proxy-authorization",
+]);
+const CREDENTIALS = new Set(["authorization", "x-api-key"]);
+
+/**
+ * @param {string[]} rawHeaders req.rawHeaders: [name, value, name, value, ...]
+ * @returns {{list: string[], dropped: string[], replaced: string[]}}
+ * `list` is the flat [name, value, ...] array http.request accepts.
+ */
+export function forwardHeaders(rawHeaders, { host, apiKey, bodyLength }) {
+ const list = [];
+ const dropped = [];
+ const replaced = [];
+ for (let i = 0; i < rawHeaders.length; i += 2) {
+ const name = rawHeaders[i];
+ const lower = name.toLowerCase();
+ if (lower === "host" || lower === "content-length") {
+ replaced.push(lower);
+ } else if (HOP_BY_HOP.has(lower)) {
+ dropped.push(lower);
+ } else if (apiKey && CREDENTIALS.has(lower)) {
+ replaced.push(lower);
+ } else {
+ list.push(name, rawHeaders[i + 1]);
+ }
+ }
+ list.push("Host", host, "Content-Length", String(bodyLength));
+ if (apiKey) list.push("Authorization", `Bearer ${apiKey}`);
+ return { list, dropped, replaced };
+}
diff --git a/bench/harnesses/meter-proxy/proxy.mjs b/bench/harnesses/meter-proxy/proxy.mjs
index 5d613e377b..e74127f327 100644
--- a/bench/harnesses/meter-proxy/proxy.mjs
+++ b/bench/harnesses/meter-proxy/proxy.mjs
@@ -24,6 +24,8 @@ import http from "node:http";
import https from "node:https";
import path from "node:path";
import { pathToFileURL } from "node:url";
+import { forwardHeaders } from "./headers.mjs";
+import zlib from "node:zlib";
import { buildCapture, capResponse, writeCapture } from "./capture.mjs";
import { computeCost, fetchPricing } from "./pricing.mjs";
import {
@@ -46,6 +48,16 @@ try {
// tokenizer not installed: sizes fall back to a chars/4 estimate, labelled as such
}
+/** Decoder for a response's content-encoding, or null when it is not compressed. */
+function decoderFor(encoding) {
+ switch ((encoding ?? "").toLowerCase()) {
+ case "gzip": return zlib.createGunzip();
+ case "deflate": return zlib.createInflate();
+ case "br": return zlib.createBrotliDecompress();
+ default: return null;
+ }
+}
+
export function createProxy(opts) {
const upstream = new URL(opts.upstream ?? "https://openrouter.ai/api");
const logPath = path.resolve(opts.logPath ?? "/results/meter.jsonl");
@@ -163,13 +175,16 @@ export function createProxy(opts) {
}
}
- const headers = { ...req.headers, host: upstream.host };
- // Harnesses carry a dummy credential; the real key lives only here.
- delete headers.authorization;
- delete headers["x-api-key"];
- if (apiKey) headers.authorization = `Bearer ${apiKey}`;
- headers["content-length"] = String(outBody.length);
- delete headers["accept-encoding"]; // keep bodies parseable
+ // Every header goes upstream as received (see headers.mjs); only the
+ // credential, host and content-length change.
+ const fwd = forwardHeaders(req.rawHeaders, {
+ host: upstream.host,
+ apiKey,
+ bodyLength: outBody.length,
+ });
+ const headers = fwd.list;
+ if (built) built.capture.headers_replaced = fwd.replaced;
+ if (built) built.capture.headers_dropped = fwd.dropped;
const upstreamReq = transport.request(
{
@@ -189,13 +204,26 @@ export function createProxy(opts) {
let firstByteAt = null;
const tokens = createFirstTokenTracker(format, startedAt);
const pieces = [];
- upstreamRes.on("data", (chunk) => {
- if (firstByteAt === null) firstByteAt = Date.now();
+ // The client gets the bytes untouched (accept-encoding is forwarded, so
+ // the body may be compressed); a decoded copy feeds the metering.
+ const decoder = decoderFor(upstreamRes.headers["content-encoding"]);
+ const meter = (chunk) => {
tokens.observe(chunk);
pieces.push(chunk);
+ };
+ decoder?.on("data", meter);
+ decoder?.on("error", () => {});
+ upstreamRes.on("data", (chunk) => {
+ if (firstByteAt === null) firstByteAt = Date.now();
+ if (decoder) decoder.write(chunk);
+ else meter(chunk);
res.write(chunk);
});
upstreamRes.on("end", () => {
+ decoder?.end();
+ finish();
+ });
+ const finish = () => {
const usage = parseResponse(
format,
Buffer.concat(pieces).toString("utf8"),
@@ -231,7 +259,7 @@ export function createProxy(opts) {
}
}
res.end();
- });
+ };
upstreamRes.on("error", () => res.end());
},
);