Files
PaperClipAI/tests/runner-e2e/ingestion-usage.test.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

119 lines
7.8 KiB
TypeScript

import { createHash } from "node:crypto";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, expect, it } from "vitest";
import { openEvalCostLedger } from "./cost-ledger.js";
import { startEvalOtlpRelay } from "./eval-otlp-relay.js";
import { EVAL_FINALIZATION_LIMITS } from "./eval-finalization-relay.js";
import { openExperimentIngestion } from "./experiment-ingestion.js";
import { collectScheduledIngestion } from "./ingestion-usage.js";
const projectId = "11111111-1111-4111-8111-111111111111";
const roots: string[] = [];
afterEach(async () => { await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); });
async function fixture() {
const root = await mkdtemp(path.join(tmpdir(), "scheduled-ingestion-")); roots.push(root);
const input = { ledgerPath: path.join(root, "ledger.jsonl"), campaignId: "campaign", projectId, executionIds: ["fixture.local"] };
const ledger = await openEvalCostLedger(input.ledgerPath, { campaignId: input.campaignId, maxUsd: "1",
deadlineAt: new Date(Date.now() + 60_000).toISOString(), maxRequests: 1, maxConcurrent: 1, maxAttemptsPerExecution: 1 });
try { await ledger.admitAttempt(input.executionIds[0]!, "attempt"); } finally { await ledger.close(); }
const name = createHash("sha256").update("attempt").digest("hex"), files: string[] = [];
for (const kind of ["native", "finalization"] as const) {
const receiptPath = path.join(`${input.ledgerPath}.otlp`, `${name}${kind === "finalization" ? "-finalization" : ""}.jsonl`);
files.push(receiptPath);
const relay = await startEvalOtlpRelay({ ...input, attemptId: "attempt", apiKey: "private-fixture", receiptPath,
deadlineAt: Date.now() + 60_000, ...(kind === "finalization" ? { limits: EVAL_FINALIZATION_LIMITS } : {}),
transport: async () => Response.json({}) });
try {
expect((await fetch(relay.baseUrl + "/v1/traces", { method: "POST", body: "{}",
headers: { authorization: `Bearer ${relay.token}`, "content-type": "application/json" } })).status).toBe(200);
} finally { await relay.close(); }
}
const experiment = await openExperimentIngestion({ ...input, receiptPath: `${input.ledgerPath}.experiment-ingestion.jsonl`,
deadlineAt: Date.now() + 60_000, transport: async () => Response.json({}) });
const body = JSON.stringify({ project_id: projectId });
try { await experiment.transport("https://api.braintrust.dev/v1/experiment", { method: "POST", body }); }
finally { await experiment.close(); }
return { input, files, expectedBytes: 4 + Buffer.byteLength(body) };
}
it("reconciles real durable receipts for all three transport paths without inventing vendor billing", async () => {
const f = await fixture(), usage = await collectScheduledIngestion(f.input);
expect(usage).toMatchObject({ status: "complete", observedAdmittedPayloadBytes: f.expectedBytes,
totalAdmittedPayloadBytes: f.expectedBytes, billedCostUsd: null, billingStatus: "unreconciled", missingExecutions: [] });
expect(usage.receipts.map(row => row.kind)).toEqual(["native", "finalization", "experiment"]);
expect(usage.receipts[0]).toMatchObject({ admittedExpandedSourceBytes: 2, admittedRecords: 0 });
expect(usage.receipts.every(row => "receiptSha256" in row && /^[a-f0-9]{64}$/.test(row.receiptSha256!))).toBe(true);
expect(usage.ledgerHash).toMatch(/^[a-f0-9]{64}$/);
expect(JSON.stringify(usage)).not.toContain("private-fixture");
});
it.each(["missing", "unclosed", "unresolved", "foreign-project", "bad-total", "bad-expanded-total", "missing-record-count", "duplicate-outcome"])(
"retains other measured traffic and an unknown total for a %s receipt", async reason => {
const f = await fixture(), file = f.files[0]!;
if (reason === "missing") await rm(file);
else {
const rows = (await readFile(file, "utf8")).trim().split("\n").map(line => JSON.parse(line));
if (reason === "unclosed") rows.pop();
if (reason === "unresolved") rows[2].outcome = "uncertain";
if (reason === "foreign-project") rows[0].projectId = "22222222-2222-4222-8222-222222222222";
if (reason === "bad-total") rows.at(-1).admittedBytes = 0;
if (reason === "bad-expanded-total") rows.at(-1).admittedExpandedBytes = 0;
if (reason === "missing-record-count") delete rows[1].recordCount;
if (reason === "duplicate-outcome") rows.splice(3, 0, rows[2]);
await writeFile(file, rows.map(row => JSON.stringify(row)).join("\n") + "\n");
}
const usage = await collectScheduledIngestion(f.input);
expect(usage).toMatchObject({ status: "incomplete", totalAdmittedPayloadBytes: null, billedCostUsd: null });
expect(usage.observedAdmittedPayloadBytes).toBeGreaterThan(0);
expect(usage.receipts[0]!.status).not.toBe("complete");
expect(usage.receipts.slice(1).every(row => row.status === "complete")).toBe(true);
});
it("binds the expected receipt set to admitted attempts and retains unstarted selections", async () => {
const f = await fixture();
expect(await collectScheduledIngestion({ ...f.input, executionIds: [...f.input.executionIds, "missing.local"] }))
.toMatchObject({ status: "incomplete", missingExecutions: ["missing.local"], totalAdmittedPayloadBytes: null });
await expect(collectScheduledIngestion({ ...f.input, executionIds: [] })).rejects.toThrow("selection");
await expect(collectScheduledIngestion({ ...f.input, executionIds: ["foreign.local"] })).rejects.toThrow("Foreign ingestion attempt");
await expect(collectScheduledIngestion({ ...f.input, campaignId: "foreign" })).rejects.toThrow("campaign mismatch");
});
it("keeps historical native receipts with the larger policy limits readable", async () => {
const f = await fixture(), file = f.files[0]!;
const rows = (await readFile(file, "utf8")).trim().split("\n").map(line => JSON.parse(line));
rows[0].limits.maxExpandedBytes = 256 * 1024 * 1024;
rows[0].limits.maxRecords = 16_384;
await writeFile(file, rows.map(row => JSON.stringify(row)).join("\n") + "\n");
expect(await collectScheduledIngestion(f.input)).toMatchObject({ status: "complete",
totalAdmittedPayloadBytes: f.expectedBytes, billedCostUsd: null });
});
it("keeps version 1 transport receipts readable without inventing expanded data measurements", async () => {
const f = await fixture(), file = f.files[0]!;
const rows = (await readFile(file, "utf8")).trim().split("\n").map(line => JSON.parse(line));
rows[0].version = 1; delete rows[0].sizing;
delete rows[0].limits.maxExpandedBytes; delete rows[0].limits.maxRecords;
for (const row of rows) {
delete row.expandedBytes; delete row.recordCount;
delete row.admittedExpandedBytes; delete row.admittedRecords;
}
await writeFile(file, rows.map(row => JSON.stringify(row)).join("\n") + "\n");
const usage = await collectScheduledIngestion(f.input);
expect(usage).toMatchObject({ status: "complete", billedCostUsd: null });
expect(usage.receipts[0]).toMatchObject({ admittedPayloadBytes: 2, admittedExpandedSourceBytes: null, admittedRecords: null });
});
it.each(["outcome", "closed"])("retains the verified prefix and digest when the final %s line is truncated", async kind => {
const f = await fixture(), file = f.files[0]!;
const lines = (await readFile(file, "utf8")).trim().split("\n");
const prefix = lines.slice(0, kind === "outcome" ? 2 : 3);
const truncated = prefix.join("\n") + '\n{"type":"' + kind;
await writeFile(file, truncated);
const usage = await collectScheduledIngestion(f.input);
expect(usage).toMatchObject({ status: "incomplete", observedAdmittedPayloadBytes: f.expectedBytes, totalAdmittedPayloadBytes: null });
expect(usage.receipts[0]).toMatchObject({ status: "incomplete", admittedRequests: 1, admittedPayloadBytes: 2,
unresolvedRequests: kind === "outcome" ? 1 : 0, receiptSha256: createHash("sha256").update(truncated).digest("hex") });
});