Files
PaperClipAI/tests/runner-e2e/braintrust-gateway-context.ts
T
DottaandPaperclip e48f1867b3 fix(evals): correlate canceled gateway requests with their ledger (#17354)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Product E2E evaluations use a budget ledger and a local model
gateway.
> - The gateway writes each request reservation before it calls a
provider.
> - A client can cancel before the gateway returns response headers.
> - Project-only logging context cannot then bind a late gateway log to
its reservation.
> - This change sends the reserved request identity in Braintrust log
metadata before dispatch.
> - Operators can use that identity to inspect late records without
guessing from time or session order.

## Linked Issues or Issue Description

Refs: #17009

**What happened?**

A hosted evaluation passed its product checks but retained two canceled
requests with no response headers. Later gateway logs had partial usage.
Their time and session matched, but they had no ledger request identity.

**Expected behavior**

A gateway log must carry the ledger request ID even when the client
receives no headers. A log match must not imply final billing.

**Steps to reproduce**

Run the gateway regression with a transport that receives a request and
then cancels before response headers. Before this change, both requests
have the same project-only parent header.

## What Changed

- Add a small Braintrust context encoder owned by the eval gateway
integration. It uses the existing SpanComponentsV4 project-log format
and an allowlist of request, campaign, execution, attempt and phase
metadata.
- Save the exact context in the private request receipt before dispatch.
Send it as the gateway parent header.
- Test both provider routes, repeated identical requests, cancellation
holds and direct-provider isolation.
- Document the lookup field and the limits of historical and partial
evidence.

## Verification

- The new cancellation regressions failed before the change: 2 failed,
37 passed.
- Gateway, operations and tracing checks: 82 passed.
- `pnpm test:e2e:runner:typecheck`: passed after rebuilding stale
generated plugin-SDK types from current master. The initial stale-type
failure remains recorded locally.
- Official Braintrust SDK 3.38.0 decoded the actual generated context.
Its `startSpan` implementation emitted the reserved identity in the
initial LLM log. The check captured logs in memory and forbade all
network calls.
- `git diff --check`: passed.
- Full repository CI passed on
`529446a9295f2dc6fa6499a2f976480d74626097`, including typecheck, tests,
build and both required gates. Greptile is 5/5 on that head; its sole
finding is fixed and resolved.
- The optional Paperclip review turn ended without a final response. Its
check remains queued and provides no review assessment. No passing
review is inferred from it.
- Live hosted acceptance of this header change remains unqualified; no
paid test was dispatched for this fix.

## Risks

- The encoder follows Braintrust's versioned span format. A future
vendor change can require an update. It adds no package dependency.
- A late log can have partial usage. Existing reservations remain held
until valid cost evidence resolves them.
- Historical project-only requests cannot gain the new identity
retroactively.
- Only synthetic eval identifiers enter the new metadata. Credentials,
prompts and responses are excluded. Direct provider routes keep their
current headers.

## Model Used

OpenAI Codex, GPT-6 family. The exact deployment ID and context-window
size are not exposed in this session. Used reasoning, repository tools
and code execution. No paid evaluation model calls were made for this
change.

## 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
- [x] All Paperclip CI gates are green
- [x] 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-23 12:50:51 -07:00

18 lines
1002 B
TypeScript

import { validEvalLedgerId, type EvalChargeIdentity } from "./cost-ledger.js";
export function braintrustGatewayRequestParent(projectId: string, requestId: string, campaignId: string, identity: EvalChargeIdentity): string {
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(projectId)
|| ![requestId, campaignId, identity.executionId, identity.attemptId].every(validEvalLedgerId)
|| !["setup", "agent", "judge", "feedback_review", "tool", "infrastructure"].includes(identity.phase)) {
throw new Error("Invalid eval gateway logging identity");
}
return Buffer.concat([Buffer.from([4, 2, 0]), Buffer.from(JSON.stringify({
object_id: projectId,
propagated_event: { metadata: {
paperclip_eval_request_id: requestId, paperclip_eval_campaign_id: campaignId,
paperclip_eval_execution_id: identity.executionId, paperclip_eval_attempt_id: identity.attemptId,
paperclip_eval_phase: identity.phase,
} },
}))]).toString("base64");
}