Skip to content

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

fix(bench): correct proxy capture test to await promise

Steven Enamakel973737cea460
diff --git a/bench/harnesses/meter-proxy/capture.test.mjs b/bench/harnesses/meter-proxy/capture.test.mjs
index ad65406a19..fd096ed848 100644
--- a/bench/harnesses/meter-proxy/capture.test.mjs
+++ b/bench/harnesses/meter-proxy/capture.test.mjs
@@ -82,3 +82,71 @@ test("proxy writes a capture file with the system prompt as sent", async () => {
proxy.server.close();
fake.close();
});
+
+import zlib from "node:zlib";
+import { forwardHeaders } from "./headers.mjs";
+
+test("forwardHeaders keeps every header verbatim, swapping only host/length/credentials/hop-by-hop", () => {
+ const raw = [
+ "Host", "meter-proxy:8080", "Authorization", "Bearer dummy", "X-Api-Key", "dummy",
+ "X-Session-Affinity", "abc", "HTTP-Referer", "https://x", "anthropic-beta", "a,b",
+ "Connection", "keep-alive", "Content-Length", "5", "X-Dup", "1", "X-Dup", "2",
+ ];
+ const { list, dropped, replaced } = forwardHeaders(raw, { host: "openrouter.ai", apiKey: "REAL", bodyLength: 9 });
+ const pairs = [];
+ for (let i = 0; i < list.length; i += 2) pairs.push([list[i], list[i + 1]]);
+ const get = (n) => pairs.filter(([k]) => k.toLowerCase() === n).map(([, v]) => v);
+ assert.deepEqual(get("x-session-affinity"), ["abc"]);
+ assert.deepEqual(get("http-referer"), ["https://x"]);
+ assert.deepEqual(get("anthropic-beta"), ["a,b"]);
+ assert.deepEqual(get("x-dup"), ["1", "2"]); // duplicates and order survive
+ assert.ok(pairs.some(([k]) => k === "X-Session-Affinity")); // casing survives
+ assert.deepEqual(get("authorization"), ["Bearer REAL"]);
+ assert.deepEqual(get("x-api-key"), []);
+ assert.deepEqual(get("host"), ["openrouter.ai"]);
+ assert.deepEqual(get("content-length"), ["9"]);
+ assert.deepEqual(get("connection"), []);
+ assert.deepEqual(dropped, ["connection"]);
+ assert.ok(replaced.includes("authorization") && replaced.includes("x-api-key"));
+});
+
+test("proxy forwards unknown headers upstream and meters a gzip response while passing it through", async () => {
+ let seen = null;
+ const payload = zlib.gzipSync('{"choices":[{"message":{"content":"zip"}}],"usage":{"prompt_tokens":10,"completion_tokens":1,"prompt_tokens_details":{"cached_tokens":8}}}');
+ const fake = http.createServer((req, res) => {
+ seen = req.headers;
+ req.resume();
+ req.on("end", () => {
+ res.writeHead(200, { "content-type": "application/json", "content-encoding": "gzip" });
+ res.end(payload);
+ });
+ });
+ await new Promise((r) => fake.listen(0, "127.0.0.1", r));
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "hdr-"));
+ const logPath = path.join(dir, "meter.jsonl");
+ const proxy = createProxy({ upstream: `http://127.0.0.1:${fake.address().port}/api`, model: "m", apiKey: "REAL", logPath });
+ await new Promise((r) => proxy.server.listen(0, "127.0.0.1", r));
+ const port = proxy.server.address().port;
+ const body = JSON.stringify(chat());
+ const got = await new Promise((resolve) => {
+ const req = http.request(
+ { port, host: "127.0.0.1", method: "POST", path: "/v1/chat/completions", headers: {
+ "x-session-id": "sess-1", "user-agent": "bench-ua/1", "accept-encoding": "gzip", authorization: "Bearer dummy", "content-type": "application/json",
+ } },
+ (res) => { const c = []; res.on("data", (d) => c.push(d)); res.on("end", () => resolve({ enc: res.headers["content-encoding"], raw: Buffer.concat(c) })); },
+ );
+ req.end(body);
+ });
+ assert.equal(seen["x-session-id"], "sess-1");
+ assert.equal(seen["user-agent"], "bench-ua/1");
+ assert.equal(seen["accept-encoding"], "gzip");
+ assert.equal(seen.authorization, "Bearer REAL");
+ assert.equal(got.enc, "gzip");
+ assert.deepEqual(got.raw, payload); // client receives the provider's bytes untouched
+ await new Promise((r) => setTimeout(r, 100));
+ const rec = JSON.parse(fs.readFileSync(logPath, "utf8").trim().split("\n").pop());
+ assert.equal(rec.prompt_tokens, 10);
+ assert.equal(rec.cached_tokens, 8);
+ proxy.server.close();
+ fake.close();
+});
diff --git a/bench/harnesses/meter-proxy/proxy.mjs b/bench/harnesses/meter-proxy/proxy.mjs
index e74127f327..54d41150de 100644
--- a/bench/harnesses/meter-proxy/proxy.mjs
+++ b/bench/harnesses/meter-proxy/proxy.mjs
@@ -219,10 +219,6 @@ export function createProxy(opts) {
else meter(chunk);
res.write(chunk);
});
- upstreamRes.on("end", () => {
- decoder?.end();
- finish();
- });
const finish = () => {
const usage = parseResponse(
format,
@@ -252,6 +248,7 @@ export function createProxy(opts) {
at: new Date(startedAt).toISOString(),
status: upstreamRes.statusCode || 502,
usage,
+ response_headers: upstreamRes.headers,
response: capResponse(responseText),
});
} catch (error) {
@@ -260,6 +257,14 @@ export function createProxy(opts) {
}
res.end();
};
+ // With a decoder, metering completes when it has flushed, not when the socket ends.
+ if (decoder) {
+ decoder.on("end", finish);
+ decoder.on("error", finish);
+ upstreamRes.on("end", () => decoder.end());
+ } else {
+ upstreamRes.on("end", finish);
+ }
upstreamRes.on("error", () => res.end());
},
);