Files
PaperClipAI/tests
DottaandPaperclip eadc560712 fix(evals): retain usage during bounded gateway shutdown (#17371)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Product E2E tests use a local gateway to record model usage and
reserve costs.
> - Test cleanup can disconnect a background request before its provider
sends final usage.
> - Immediate abort then leaves the request cost unknown and its full
reservation held.
> - This pull request lets an admitted response finish within a short
drain period.
> - The benefit is better accounting without a new request or a retry.

## Linked Issues or Issue Description

Refs #17365 and #17354.

**What happened?**

Two hosted cases passed, but cleanup canceled background requests before
the gateway received their usage. Both requests kept their full holds. A
local HTTP regression reproduces this loss with OpenAI and Anthropic
responses.

**Expected behavior**

Retain final usage when an already-dispatched response finishes within a
bounded cleanup period. Keep the client outcome canceled. Preserve the
hold when final usage is still unknown.

**Steps to reproduce**

1. Send a request through the eval gateway.
2. Disconnect the client before the provider returns final usage.
3. Close the gateway and let the provider response arrive.
4. Before this fix, the gateway aborts immediately and loses the final
receipt.

**Paperclip version or commit**

Reproduced against master `b6e690044a821faab52d5466db36c4330c754a2d`.

## What Changed

- Drain an already-dispatched response for at most five seconds after a
disconnect. Keep the original provider timeout and campaign deadline.
- Stop forwarding response data to a disconnected client. Stop admission
as soon as shutdown begins.
- Retain `clientDisconnected` on the private receipt and keep the
outcome canceled, even when final usage arrives.
- Make repeated shutdown calls share completion.
- Add HTTP and SSE regression tests. Document the drain and its
accounting limits.

## Verification

- Red: five regression cases failed against the previous gateway
implementation. Four further cases reproduced late cancellation during
review.
- `pnpm test:e2e:runner:unit`: 4,072 Vitest tests and 137 Node contract
tests passed. Two existing Vitest tests were skipped.
- `pnpm exec tsc --noEmit -p tests/runner-e2e/tsconfig.json`: passed.
- `git diff --check`: passed.
- Regressions cover both providers, an interrupted SSE stream, grace
expiry, a shorter provider deadline, no dispatch during interrupted
identity resolution, no new admission, repeated shutdown, client-only
disconnects, and disconnects during each final receipt/ledger write.
- Full repository typecheck, tests, and build passed in [CI run
40026846417](https://github.com/paperclipai/paperclip/actions/runs/40026846417)
on `40ef13bd4d5df6b2ca0a9666fb485adf2a565c28`. Greptile and Paperclip
reviews are both 5/5. No paid model test was run for this change. Live
qualification remains open.

## Risks

- Cleanup can take up to five more seconds. The admitted request can
continue generating during that period, inside its existing reservation
and deadlines.
- An abort does not prove that the provider stopped billing. Missing
usage keeps the unknown hold.
- This change does not establish invoice settlement or rewrite
historical receipts. It changes only the eval gateway.

## 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
- [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 14:36:41 -07:00
..