mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-10-14 05:18:54 +00:00
fix(routes): preserve response bytes and close remaining test gaps
Replace the alias-echo rewrite on the Anthropic passthrough branch with a byte-preserving field replacement. The prior version decoded the response into a Go map and re-marshaled it. This silently corrupted a JSON integer beyond float64's exact range. It also rewrote 1.0 to 1, HTML-escaped angle brackets and ampersands, and re-sorted every key. A tool_use block's "input" is arbitrary model-generated JSON. A long numeric id inside tool arguments could be silently corrupted on a route documented as passthrough. rewriteModelField now walks the response with a JSON decoder and copies every field but "model" as raw bytes. Add a test with a 20-digit integer, a 1.0 float, and a literal "<" character inside tool_use input. It asserts the response is byte-identical to the source apart from the model field. Add a negative test for the anthropic-beta header allowlist. It asserts Authorization and Cookie never reach the upstream. It also asserts the upstream sees exactly one x-api-key value: the gateway's own key, never the client's. A single Get() call could miss a second, leaked value, since Set() and Add() interact in a way a naive check would not catch. The test checks the Values() length explicitly. Add a test case for the native passthrough route's prompt-cache billing. It fails if cache_creation_input_tokens or cache_read_input_tokens drop out of the billed prompt count. This closes a production billing change that previously carried no coverage at all. Decouple response delivery from usage extraction on the passthrough branch. A response body read failure, or a malformed usage field, no longer both discard the response and misreport the failure as an upstream connection error. A read failure still returns an error, now correctly classified as a translation failure. A malformed usage field no longer blocks the response at all: the client still gets it, and the existing zero-usage estimate takes over for accounting. Return the adapter's own usage value on a chatCompletion error in the translated path, instead of discarding it. This has no effect while streaming stays rejected up front. It prevents the same critical bug from resurfacing once streaming support lands. Normalize tool_result content to a plain string before applying the error prefix. The prior version only prefixed a Go string. Anthropic's array content form, which Claude Code sends, lost the is_error signal entirely. Absent content now becomes an empty string instead of the JSON null OpenAI rejects. Treat "stream": null the same as an absent key. The prior fix for a non-boolean stream value also rejected null. An SDK that serializes an unset optional field as null must still get an ordinary non-streaming response. Drop a yaegi-check assertion that could not fail. The fixture configures no cache, so the header it checked was never set regardless of what the code under test did. A mutation pass confirmed this: it deleted the line the assertion was meant to guard, and make yaegi-check still reported OK. Document that this route now intercepts POST /v1/messages unconditionally, even under passthroughUnknown, where it previously fell through to the next handler. Add the note to both the new README section and Known limitations. Correct the passthrough table's claim about response bytes to describe the actual guarantee.
This commit is contained in:
@@ -764,9 +764,17 @@ func exerciseMessagesRoute(handler http.Handler) error {
|
||||
if err := assertAnthropicErrorShape("upstream 429", upErrRec.Body.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
if upErrRec.Header().Get("X-Llmgw-Cache") != "" {
|
||||
return fmt.Errorf("POST /v1/messages (upstream 429) carries a stale X-Llmgw-Cache header: %q", upErrRec.Header().Get("X-Llmgw-Cache"))
|
||||
}
|
||||
// A stale X-Llmgw-Cache header on an error response was deliberately
|
||||
// NOT asserted here (F6 fix, 2026-08-23 review): this harness's own
|
||||
// fixture configures no cache/Redis, so g.cache is nil and the
|
||||
// header is never set on any response regardless of what this route
|
||||
// does — the assertion could not fail no matter what code ran,
|
||||
// confirmed by a mutation pass that deleted the header-clear line it
|
||||
// was meant to guard (handleAdapterErrorEnvelope, routes_unified.go)
|
||||
// with make yaegi-check still reporting OK. That behavior is already
|
||||
// covered where it can actually fail — a real cache fixture, go
|
||||
// test: TestHandleChat_CacheableRequest_UpstreamDown_502HasNoCacheHeader
|
||||
// (routes_unified_test.go).
|
||||
|
||||
// 7. A tool_use round trip through the TRANSLATED path, to drive
|
||||
// openAIToolCallFromAnthropic/openAIToolMessageFromAnthropic and
|
||||
|
||||
Reference in New Issue
Block a user