Files
PaperClipAI/tests/runner-e2e/eval-otlp-relay.ts
T
DottaandPaperclip 52a66af29d perf(evals): tighten measured campaign resource ceilings (#17381)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Product E2E checks real provider behavior under a campaign budget.
> - Its request and trace limits also determine the operating-cost
forecast.
> - The initial limits greatly exceed the measured marker and feedback
cases.
> - This pull request lowers those defaults while keeping the same
selected cases and full model context limits.
> - The benefit is a smaller bounded resource envelope with explicit
quota failures and readable historical evidence.

## Linked Issues or Issue Description

Refs: #17365
Refs: #17371

**What existing behavior does this improve?**

The resource ceilings for bounded Product E2E campaigns and their OTLP
relay.

**Current behavior**

Daily and weekly campaigns allow 60 and 120 model requests. The native
relay defaults to 256 MiB of expanded source and 16,384 records per
attempt. Both October 23 two-case qualification campaigns used ten model
requests. The largest native attempt used 6,531,801 expanded source
bytes and 2,455 records.

**Proposed behavior**

Use 30 daily and 60 weekly model requests. Default native exports to 16
MiB of expanded source and 4,096 records. Preserve the older explicit
relay maxima so prior receipts remain valid and deliberate controller
overrides still work.

**Reason and benefit**

The smaller limits leave headroom for the measured cases and reduce the
maximum source traffic in the operating forecast. They do not change the
daily or weekly dollar cap, model context ceiling, per-request
reservation, selected cases, or attempt policy.

**Breaking changes**

A new bounded attempt that exceeds the smaller default trace allowance
receives a quota error. It cannot silently omit required evidence.
Existing receipts remain readable. No paid recurrence is enabled by this
change.

## What Changed

- Set scheduled model request caps to 30 daily and 60 weekly.
- Add smaller native relay defaults while preserving the existing
explicit maximum and historical receipt validation.
- Exercise default count and expanded-source exhaustion through the HTTP
relay. Keep a regression for larger historical receipt policies.
- Document the measured basis, default capacities, and failure behavior.

## Verification

- `pnpm test:e2e:runner:unit`: 4,095 Vitest tests passed, two existing
skips; 137 Node contract tests passed.
- `pnpm test:e2e:runner:typecheck`: passed.
- `git diff --check`: passed.
- The relay test accepts two 3-MiB log payloads, rejects the third
before upstream dispatch, and retains its quota receipt.
- Read-only replay of the prior real hosted campaign preserved all five
ingestion receipts and 123,044 payload bytes exactly. Its accounting
journal remained byte-for-byte unchanged; zero provider calls.
- Full repository typecheck, tests, build and reviews are pending hosted
CI. No paid model call was made for this change. Live qualification of
the smaller defaults remains open.

## Risks

- More verbose future cases may exhaust the new defaults. Such a failure
remains visible and requires evidence before increasing a limit.
- Source-byte ceilings are not Braintrust invoice units. Operating-cost
qualification and reconciliation remain separate requirements.
- Manual controller overrides can still use the earlier larger maxima;
their cost audit must account for the limits actually recorded in the
receipt.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, code execution and tool use.
The host does not expose an exact model build identifier or context
window size for this session.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-24 07:38:12 -07:00

142 lines
10 KiB
TypeScript

import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
import { once } from "node:events";
import { mkdir, open } from "node:fs/promises";
import { createServer, type ServerResponse } from "node:http";
import path from "node:path";
import { gunzipSync } from "node:zlib";
import { evalOtlpRejected, evalOtlpResponse, isEvalProjectId, validateEvalOtlpParent, validateEvalOtlpPayload, type EvalOtlpSignal } from "./eval-otlp-payload.js";
export const EVAL_OTLP_LIMITS = { maxRequests: 2048, maxRequestBytes: 8 * 1024 * 1024,
maxTotalBytes: 64 * 1024 * 1024, maxConcurrent: 4,
maxExpandedBytes: 256 * 1024 * 1024, maxRecords: 16_384 } as const;
export const EVAL_OTLP_DEFAULT_LIMITS = { ...EVAL_OTLP_LIMITS,
maxExpandedBytes: 16 * 1024 * 1024, maxRecords: 4096 } as const;
export async function startEvalOtlpRelay(input: {
projectId: string; apiKey: string; campaignId: string; attemptId: string; receiptPath: string; deadlineAt: number;
limits?: { [K in keyof typeof EVAL_OTLP_LIMITS]: number }; transport?: typeof fetch;
}) {
const limits = input.limits ?? EVAL_OTLP_DEFAULT_LIMITS;
if (!isEvalProjectId(input.projectId) || !input.apiKey.trim() || /[\r\n]/.test(input.apiKey)
|| ![input.campaignId, input.attemptId].every(value => /^[A-Za-z0-9_.:-]{1,512}$/.test(value))
|| !path.isAbsolute(input.receiptPath) || !Number.isFinite(input.deadlineAt)
|| input.deadlineAt <= Date.now() || input.deadlineAt > Date.now() + 4 * 3_600_000
|| Object.keys(limits).sort().join(",") !== Object.keys(EVAL_OTLP_LIMITS).sort().join(",")
|| Object.entries(EVAL_OTLP_LIMITS).some(([key, maximum]) => !Number.isSafeInteger(limits[key as keyof typeof limits])
|| limits[key as keyof typeof limits] < 1 || limits[key as keyof typeof limits] > maximum)) throw new Error("Invalid eval OTLP relay policy");
await mkdir(path.dirname(input.receiptPath), { recursive: true, mode: 0o700 });
const file = await open(input.receiptPath, "ax", 0o600);
let writes = Promise.resolve(), poisoned = false, closed = false, quotaRecorded = false;
const record = (value: object) => {
writes = writes.then(async () => { await file.writeFile(JSON.stringify(value) + "\n"); await file.sync(); })
.catch(error => { poisoned = true; throw error; });
return writes;
};
try { await record({ type: "policy", version: 2, sizing: "otlp-v1.9-expanded-source-json", projectId: input.projectId, campaignId: input.campaignId,
attemptId: input.attemptId, limits, deadlineAt: new Date(input.deadlineAt).toISOString(), openedAt: new Date().toISOString() }); }
catch (error) { await file.close(); throw error; }
const token = randomBytes(32).toString("hex");
const pending = new Set<Promise<void>>(), controllers = new Set<AbortController>();
const usage = { admittedRequests: 0, admittedWireBytes: 0, admittedBytes: 0, admittedExpandedBytes: 0,
admittedRecords: 0, attemptedForwardBytes: 0, acceptedRequests: 0, incompleteRequests: 0 };
const deny = (response: ServerResponse, status: number, code: string) => {
if (response.destroyed) return;
response.writeHead(status, { "content-type": "application/json", "cache-control": "no-store" });
response.end(JSON.stringify({ error: { type: "eval_otlp_relay", message: code } }));
};
const server = createServer((request, response) => {
const operation = (async () => {
const auth = typeof request.headers.authorization === "string" ? request.headers.authorization.replace(/^Bearer /, "") : "";
if (Buffer.byteLength(auth) !== Buffer.byteLength(token) || !timingSafeEqual(Buffer.from(auth), Buffer.from(token))) return deny(response, 401, "unauthorized");
if (closed || poisoned || Date.now() >= input.deadlineAt) return deny(response, 503, "relay_unavailable");
const signal: EvalOtlpSignal | undefined = request.url === "/otel/v1/traces" ? "traces" : request.url === "/otel/v1/logs" ? "logs" : undefined;
if (request.method !== "POST" || !signal) return deny(response, 404, "unqualified_endpoint");
if (pending.size >= limits.maxConcurrent) return deny(response, 429, "concurrency_limit");
const contentType = request.headers["content-type"]?.split(";")[0].trim();
const encoding = request.headers["content-encoding"];
if (!["application/json", "application/x-protobuf"].includes(contentType ?? "") || encoding && encoding !== "gzip" && encoding !== "identity") return deny(response, 415, "unqualified_encoding");
let body: Buffer, parent: string, wireBytes = 0, expandedBytes = 0, recordCount = 0;
try {
parent = validateEvalOtlpParent(String(request.headers["x-bt-parent"] ?? `project_id:${input.projectId}`), input.projectId);
const chunks: Buffer[] = [];
for await (const chunk of request) {
wireBytes += chunk.length;
if (wireBytes > limits.maxRequestBytes) throw new Error("request_limit");
chunks.push(Buffer.from(chunk));
}
body = Buffer.concat(chunks);
if (encoding === "gzip") body = gunzipSync(body, { maxOutputLength: limits.maxRequestBytes });
const validated = validateEvalOtlpPayload(body, contentType!, signal, input.projectId);
({ body, expandedBytes, recordCount } = validated);
if (body.length > limits.maxRequestBytes) throw new Error("request_limit");
} catch { return deny(response, 400, "invalid_or_oversized_payload"); }
if (closed || poisoned || Date.now() >= input.deadlineAt) return deny(response, 503, "relay_unavailable");
if (usage.admittedRequests >= limits.maxRequests || usage.admittedBytes + body.length > limits.maxTotalBytes
|| usage.admittedExpandedBytes + expandedBytes > limits.maxExpandedBytes
|| usage.admittedRecords + recordCount > limits.maxRecords) {
if (!quotaRecorded) { quotaRecorded = true; await record({ type: "quota_exhausted", at: new Date().toISOString(), ...usage }); }
return deny(response, 429, "ingestion_allowance_exhausted");
}
const id = randomUUID();
usage.admittedRequests++; usage.admittedWireBytes += wireBytes; usage.admittedBytes += body.length;
usage.admittedExpandedBytes += expandedBytes; usage.admittedRecords += recordCount;
await record({ type: "admitted", id, signal, at: new Date().toISOString(), wireBytes, bodyBytes: body.length,
expandedBytes, recordCount,
payloadSha256: createHash("sha256").update(body).digest("hex") });
const abort = new AbortController(); controllers.add(abort);
const disconnected = () => { if (!response.writableEnded) abort.abort(); };
response.on("close", disconnected);
let status: number | undefined, outcome = "uncertain", rejected: number | undefined, dispatched = false;
try {
if (closed || poisoned || response.destroyed || Date.now() >= input.deadlineAt) throw new Error("relay_unavailable");
dispatched = true; usage.attemptedForwardBytes += body.length;
const upstream = await (input.transport ?? fetch)(`https://api.braintrust.dev/otel/v1/${signal}`, {
method: "POST", redirect: "error", signal: AbortSignal.any([abort.signal,
AbortSignal.timeout(Math.max(1, Math.min(10_000, input.deadlineAt - Date.now())))]),
headers: { authorization: `Bearer ${input.apiKey}`, "x-bt-parent": parent, "content-type": contentType!,
"accept-encoding": "identity", accept: "application/x-protobuf, application/json" }, body: Buffer.from(body),
});
status = upstream.status;
if (!upstream.ok) { await upstream.body?.cancel(); outcome = "rejected"; throw new Error("receiver_rejected"); }
const type = upstream.headers.get("content-type")?.split(";")[0].trim() ?? contentType!;
if (!["application/json", "application/x-protobuf"].includes(type)) { await upstream.body?.cancel(); throw new Error("invalid_response"); }
const reader = upstream.body?.getReader(), chunks: Uint8Array[] = []; let bytes = 0;
try {
while (reader) {
const next = await reader.read(); if (next.done) break;
bytes += next.value.length; if (bytes > 65_536) throw new Error("response_limit");
chunks.push(next.value);
}
} finally { await reader?.cancel().catch(() => {}); }
rejected = evalOtlpRejected(Buffer.concat(chunks), type, signal);
outcome = rejected ? "partial" : "accepted";
} catch { if (!dispatched) outcome = "not_dispatched"; }
finally { controllers.delete(abort); response.off("close", disconnected); }
if (outcome === "accepted") usage.acceptedRequests++; else usage.incompleteRequests++;
await record({ type: "outcome", id, at: new Date().toISOString(), outcome, dispatched,
...(status === undefined ? {} : { httpStatus: status }), ...(rejected === undefined ? {} : { rejectedRecords: rejected }) });
if (response.destroyed) return;
if (outcome !== "accepted" && outcome !== "partial") return deny(response, 502, "export_unconfirmed");
response.writeHead(200, { "content-type": contentType!, "cache-control": "no-store" });
response.end(evalOtlpResponse(rejected!, contentType!, signal));
})();
pending.add(operation);
void operation.then(() => pending.delete(operation), () => { poisoned = true; pending.delete(operation); deny(response, 503, "receipt_persistence_failed"); });
});
server.requestTimeout = 15_000; server.headersTimeout = 10_000; server.maxConnections = limits.maxConcurrent + 8;
try { server.listen(0, "127.0.0.1"); await once(server, "listening"); }
catch (error) { await file.close(); throw error; }
const address = server.address();
if (!address || typeof address === "string") { await file.close(); throw new Error("Eval OTLP relay did not bind loopback"); }
let closing: Promise<void> | undefined;
const close = () => closing ??= (async () => {
closed = true; clearTimeout(timer); controllers.forEach(controller => controller.abort()); server.closeAllConnections();
await new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()));
await Promise.allSettled([...pending]);
try { await record({ type: "closed", at: new Date().toISOString(), ...usage, poisoned }); }
finally { await file.close(); }
})();
const timer = setTimeout(() => { void close().catch(() => {}); }, Math.max(1, input.deadlineAt - Date.now())); timer.unref();
return { baseUrl: `http://127.0.0.1:${address.port}/otel`, token, receiptPath: input.receiptPath, close };
}