From fc78eb5f6b2cf239f0cf87f47bf3bc790790951e Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 19 Oct 2026 07:38:40 +0800 Subject: [PATCH] gateway: ClinePass's upstream is read from provider_metadata.gateway.routing.finalProvider (#2847, smart-lty) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ClinePass (api.cline.bot) names the provider that answered only in a Chat choice's provider_metadata.gateway.routing.finalProvider: under delta in a stream chunk, and under message in a single body. upstreamOf read only a top-level provider, message.provider and response.provider, so the Routing page's 上游 column, the request log and the usage ledger showed nothing for ClinePass. upstreamOf now also reads finalProvider at those two places, and nowhere else. A choice's content belongs to the model. providerAttempts inside routing lists every provider that was tried, not the one that answered. The cheap pre-check now passes a chunk that carries only "finalProvider". Tests: - TestUpstreamOfClinePass checks: - the real chunk gives deepseek; - the same chunk as one body (message) gives deepseek; - content, tool_call arguments, providerAttempts alone and an object-valued finalProvider give ""; - a top-level provider still wins. - TestClinePassUpstreamRecorded serves the real stream through the Handler on /v1/chat/completions and on /v1/messages (translated). It checks that the route, the try, Recent() and the usage ledger all say deepseek. The fixture testdata/clinepass_stream.sse is a real ClinePass stream: api.cline.bot, deepseek/deepseek-v4.1-flash, captured 2026-10-02 in the MIT-licensed wkeking/clinepass-channel-monitor (internal/channellog/testdata/plain_success.log). It is not the reporter's own bytes; #2847 has none. What was run at 1a3b92688 + this change, on macOS (Go 1.26.8): - gofmt -l: clean. - go vet: darwin, linux (-tags nogui) and windows. - Builds: darwin, linux (nogui) and windows. - go test -run 'TestUpstream|TestClinePass|TestServed' ./internal/gateway: ok. - Broken by hand: with the old upstream.go put back, both new tests go red ("real ClinePass chunk: upstreamOf = \"\"; want deepseek", plus the route, request-log and usage assertions on both paths). - go test -timeout 45m ./... at load average 420-460 (several sessions' suites at once). agent, davsync, gateway, library, plugin and provider ran past the 45m timeout. gui was red on load reds that are already filed: - TestSetAnswersWhileDevinAndProvidersHang: #1375. - TestTrayCellClickReleasedPanel: #2645. - TestClaudeAccountsSpentFallBackToCodex: #2859, also #2858. - TestHostOnBunLackingLibsSaysWhich and TestGatewayHookMixedPiPackage: #2865. - The package timeouts: #2869. Every other package was ok. Not tried: a live ClinePass account in the real app. --- .../gateway/testdata/clinepass_stream.sse | 3 + internal/gateway/upstream.go | 28 +++++- internal/gateway/upstream_test.go | 96 +++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 internal/gateway/testdata/clinepass_stream.sse diff --git a/internal/gateway/testdata/clinepass_stream.sse b/internal/gateway/testdata/clinepass_stream.sse new file mode 100644 index 000000000..9fc780758 --- /dev/null +++ b/internal/gateway/testdata/clinepass_stream.sse @@ -0,0 +1,3 @@ +data: {"id":"gen_01M3YFPP549JB6EP8D4RCXD6EH","object":"chat.completion.chunk","created":1790950726,"model":"deepseek/deepseek-v4.1-flash","choices":[{"index":0,"delta":{"role":"assistant"},"logprobs":null,"finish_reason":null}],"system_fingerprint":"fp_jgawxng80g"} +data: {"choices":[{"delta":{"provider_metadata":{"deepseek":{"choiceIndex":0,"messageRole":"assistant","promptCacheHitTokens":0,"promptCacheMissTokens":37,"responseObject":"chat.completion.chunk","systemFingerprint":"aeb56401ca74e127821c4f9126dcb669"},"gateway":{"cost":"0.00001515","gatewayCost":"0.00001515","generationId":"gen_01M3YFPP549JB6EP8D4RCXD6EH","inferenceCost":"0.00001515","inputInferenceCost":"0.00000555","marketCost":"0.00001515","outputInferenceCost":"0.0000096","routing":{"affinity":{"outcome":"confirmed","pinnedProvider":"deepseek"},"canonicalSlug":"deepseek/deepseek-v4.1-flash","clientSessionId":"sess-ed01ce273d78b615f19287b8325e4aee","clientSessionIdSource":"explicit","fallbacksAvailable":[],"finalProvider":"deepseek","modelAttemptCount":1,"modelAttempts":[{"canonicalSlug":"deepseek/deepseek-v4.1-flash","providerAttemptCount":1,"providerAttempts":[{"credentialType":"system","endTime":1790950726481,"provider":"deepseek","providerRequestId":"4ae0eeb4ebc6bcc03390f31ec70cc26a","providerResponseId":"00b1015b-004f-4132-9223-649fed188560","startTime":1790950725813,"statusCode":200,"success":true}],"success":true}],"originalModelId":"deepseek/deepseek-v4.1-flash","planningReasoning":"Provider set restricted to: deepseek. System credentials planned for: deepseek. Total execution order: deepseek(system)","resolvedProvider":"deepseek","totalProviderAttemptCount":1},"surchargeCost":"0"}}},"finish_reason":"length","index":0,"logprobs":null}],"created":1790950726,"generationId":"gen_01M3YFPP549JB6EP8D4RCXD6EH","id":"gen_01M3YFPP549JB6EP8D4RCXD6EH","model":"deepseek/deepseek-v4.1-flash","object":"chat.completion.chunk","system_fingerprint":"fp_jgawxng80g","usage":{"cache_creation_input_tokens":0,"completion_tokens":16,"completion_tokens_details":{"image_tokens":0,"reasoning_tokens":16},"cost":0.00001515,"cost_details":null,"gateway_cost":0.00001515,"is_byok":false,"market_cost":0.00001515,"prompt_tokens":37,"prompt_tokens_details":{"audio_tokens":0,"cached_tokens":0,"video_tokens":0},"total_tokens":53}} +data: [DONE] diff --git a/internal/gateway/upstream.go b/internal/gateway/upstream.go index 4b21415ca..90b5c25cc 100644 --- a/internal/gateway/upstream.go +++ b/internal/gateway/upstream.go @@ -13,11 +13,25 @@ import ( // OpenRouter names it in a top-level "provider" — a Chat body and every // chunk of its stream, an Anthropic Messages body — and in a Messages // stream's message_start, as message.provider (seen 2026-10-05: DeepInfra, -// StreamLake). Its Responses replies name none. +// StreamLake). Its Responses replies name none. ClinePass (api.cline.bot) +// names it in a Chat choice's provider_metadata, as +// gateway.routing.finalProvider: under delta in a stream's chunk, under +// message in one body (#2847, smart-lty). Only those places count: a +// choice's content is the model's, and providerAttempts inside routing +// lists every provider tried, not the one that answered. func upstreamOf(b []byte) string { - if !bytes.Contains(b, []byte(`"provider"`)) { + if !bytes.Contains(b, []byte(`"provider"`)) && !bytes.Contains(b, []byte(`"finalProvider"`)) { return "" } + type routed struct { + ProviderMetadata struct { + Gateway struct { + Routing struct { + FinalProvider json.RawMessage `json:"finalProvider"` + } `json:"routing"` + } `json:"gateway"` + } `json:"provider_metadata"` + } var v struct { Provider json.RawMessage `json:"provider"` Message struct { @@ -26,11 +40,19 @@ func upstreamOf(b []byte) string { Response struct { Provider json.RawMessage `json:"provider"` } `json:"response"` + Choices []struct { + Delta routed `json:"delta"` + Message routed `json:"message"` + } `json:"choices"` } if json.Unmarshal(b, &v) != nil { return "" } - for _, raw := range []json.RawMessage{v.Provider, v.Message.Provider, v.Response.Provider} { + raws := []json.RawMessage{v.Provider, v.Message.Provider, v.Response.Provider} + for _, c := range v.Choices { + raws = append(raws, c.Delta.ProviderMetadata.Gateway.Routing.FinalProvider, c.Message.ProviderMetadata.Gateway.Routing.FinalProvider) + } + for _, raw := range raws { var s string // a request's provider options echoed back are an object if json.Unmarshal(raw, &s) == nil { if s = strings.TrimSpace(s); s != "" && len(s) <= 64 { diff --git a/internal/gateway/upstream_test.go b/internal/gateway/upstream_test.go index 79c7a9491..181277d24 100644 --- a/internal/gateway/upstream_test.go +++ b/internal/gateway/upstream_test.go @@ -1,9 +1,12 @@ package gateway import ( + "bufio" + "bytes" "io" "net/http" "net/http/httptest" + "os" "strings" "testing" "time" @@ -137,3 +140,96 @@ func TestUpstreamOf(t *testing.T) { } } } + +// clinePassStream is a real ClinePass stream (api.cline.bot, DeepSeek V4.1 +// Flash, 2026-10-02), as captured in wkeking/clinepass-channel-monitor's +// testdata/plain_success.log (MIT): its last chunk names the provider that +// answered only in choices[].delta.provider_metadata.gateway.routing. +func clinePassStream(t *testing.T) []string { + t.Helper() + b, err := os.ReadFile("testdata/clinepass_stream.sse") + if err != nil { + t.Fatal(err) + } + var lines []string + sc := bufio.NewScanner(bytes.NewReader(b)) + sc.Buffer(nil, 1<<20) + for sc.Scan() { + if l := sc.Text(); l != "" { + lines = append(lines, l) + } + } + return lines +} + +// ClinePass says the provider behind it as finalProvider in a choice's +// provider_metadata (#2847, smart-lty): under delta in a stream, under +// message in one body. Only there: the model's own text naming it, or the +// list of providers tried, says none. +func TestUpstreamOfClinePass(t *testing.T) { + var last string + for _, l := range clinePassStream(t) { + if d := strings.TrimPrefix(l, "data: "); strings.Contains(d, "finalProvider") { + last = d + } + } + if got := upstreamOf([]byte(last)); got != "deepseek" { + t.Errorf("real ClinePass chunk: upstreamOf = %q; want deepseek", got) + } + whole := strings.Replace(last, `"delta":{"provider_metadata"`, `"message":{"role":"assistant","content":"hi","provider_metadata"`, 1) + if got := upstreamOf([]byte(whole)); got != "deepseek" { + t.Errorf("one body: upstreamOf = %q; want deepseek", got) + } + for in, want := range map[string]string{ + `{"choices":[{"delta":{"provider_metadata":{"gateway":{"routing":{"finalProvider":"novita"}}}}}]}`: "novita", + `{"choices":[{"delta":{"content":"provider_metadata gateway routing \"finalProvider\":\"deepseek\""}}]}`: "", + `{"choices":[{"delta":{"provider_metadata":{"gateway":{"routing":{"modelAttempts":[{"providerAttempts":[{"provider":"deepseek"}]}]}}}}}]}`: "", + `{"choices":[{"delta":{"provider_metadata":{"gateway":{"routing":{"finalProvider":{"id":"x"}}}}}}]}`: "", + `{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"{\"finalProvider\":\"x\"}"}}]}}]}`: "", + `{"provider":"Novita","choices":[{"delta":{"provider_metadata":{"gateway":{"routing":{"finalProvider":"deepseek"}}}}}]}`: "Novita", + } { + if got := upstreamOf([]byte(in)); got != want { + t.Errorf("upstreamOf(%s) = %q; want %q", in, got, want) + } + } +} + +// The real ClinePass stream's provider reaches the route, its try, the +// request log and the usage ledger, relayed to a Chat client or translated +// to a Messages one (#2847). +func TestClinePassUpstreamRecorded(t *testing.T) { + stream := clinePassStream(t) + for _, path := range []string{"/v1/chat/completions", "/v1/messages"} { + t.Run(path, func(t *testing.T) { + fresh(t) + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + for _, l := range stream { + io.WriteString(w, l+"\n\n") + } + })) + t.Cleanup(up.Close) + p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Chat: up.URL + "/v1", Models: []string{"sol"}} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + body := `{"model":"fake/sol","max_tokens":100,"stream":true,"messages":[{"role":"user","content":"hi"}]}` + s := New() + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body))) + if rec.Code != 200 { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + r := lastRoute(s) + if r.Upstream != "deepseek" || len(r.Tries) != 1 || r.Tries[0].Upstream != "deepseek" { + t.Errorf("route upstream %q, tries %+v", r.Upstream, r.Tries) + } + if calls := s.Recent(); len(calls) == 0 || calls[0].Usage.Upstream != "deepseek" { + t.Errorf("request log: %+v", calls) + } + if u := usage.Load(time.Time{}); len(u) != 1 || u[0].Upstream != "deepseek" { + t.Errorf("usage %+v", u) + } + }) + } +}