Files
magpie/internal/gateway/vertex.go
T
yetone 2260390a01 gateway: a Gemini thought signature goes back only to the provider that gave it
mimi001234 on Discord: Pi (openai-completions) on auto/gemini-4-argon, a
group of google-vertex and google-aistudio-paid. Vertex AI served turns
1 to 14 and signed its calls; on turn 15 it answered 429, the group moved
to AI Studio, and magpie replayed Vertex AI's signatures from
gemini-signatures/. AI Studio answered every try with 400 "Corrupted
thought signature." A signature is the issuing backend's, and only it
takes one.

- The provider that gave a signature is noted beside it,
  gemini-signatures/<key>.by (noteSigner, written through temp and
  rename), wherever a reply is read: decoderFor (every translated path:
  Chat, Anthropic and Responses clients, plugins, search, continuations)
  and sigTidy (a Chat relay, and a Gemini client's reply relayed as it
  came, now only read, in native mode).
- Every request is fitted to its provider: ownSignatures in
  forwardTranslated, chatCallSignatures and geminiBodySignatures in
  passthrough. Another provider's signature goes as Google's documented
  skip_thought_signature_validator on a call, and not at all on text. A
  signature of no known provider (ids from before this change, a note
  that couldn't be written) goes as before.
- A 400 "Invalid/Corrupted thought signature" (signatureRefused) is asked
  again once without signatures at any upstream Gemini's signatures
  reach (signaturesReach), relayed too, not only Vertex AI translated;
  it rests no account (restAfterMarked) and lets a group go on to its
  next member (shapeRefused).

The signer is the provider id, not the account: two accounts of one
provider share signatures as before.

Run on mote (mote-run), the targeted run and vets at this change on origin/main 0e55a7593:
- go test ./internal/gateway -run 'Signature|Signer|Vertex|Gemini|Corrupted': ok
- go test -timeout 45m ./internal/gateway: ok (876s), at this change on
  bf60ea596; the only commit since, 47d583939, touches internal/library
- gofmt -l internal/: clean; go vet ./...; GOOS=linux go vet -tags nogui
  ./...; GOOS=windows go vet ./...; go build ./...: ok

Red on the old code (origin/main 0e55a7593 with the new test file):
TestSignatureGoesOnlyToItsSigner (turn 3: 400 "AI Studio: ... Corrupted
thought signature."), TestCorruptedSignatureTriedWithout and
TestCorruptedSignatureNotTheAccount (rested "backoff" 60s).
Broken by hand on the new code:
- chatCallSignatures without the foreignSignature check:
  TestSignatureGoesOnlyToItsSigner red (AI Studio asked twice: refused,
  then the retry).
- ownSignatures in forwardTranslated commented out:
  TestSignatureGoesOnlyToItsSignerTranslated red the same way.
- the passthrough retry turned off: TestCorruptedSignatureTriedWithout
  red with the 400.

Not tried: real Vertex AI and AI Studio accounts; a Gemini client's
request relayed to Vertex AI end to end (TestGeminiBodyKeepsOnlyItsOwn
covers the body and the noting only); Code Assist relays, whose replies
are not noted, so their signatures count as of no known provider.
2026-11-05 11:42:53 +08:00

132 lines
5.0 KiB
Go

package gateway
import (
"encoding/json"
"slices"
"strings"
)
// ---- Google Vertex AI (provider side) ----------------------------------------
//
// Vertex AI serves Gemini at streamGenerateContent, with the model in the
// path (provider.VertexPath) and Gemini's own request in the body: the one
// Code Assist carries in its envelope. Where it parts from Code Assist is in
// what it turns away. A thinking level the model doesn't have, or a level
// at all on 2.5, is a 400 (vertexEffort, vertexThinking). A call's thought
// signature goes back as Vertex AI gave it, as on every Gemini path
// (gemini_signature.go): a call made with none on a step it signed is a 400.
//
// Its partner models (provider.VertexPartner: deepseek-ai/…, zai-org/…,
// openai/gpt-oss-…) are not Gemini: Google serves them on its
// OpenAI-compatible chat completions at the project (Base(Chat)), under
// the same token, and the gateway asks them there as it asks any chat
// completions vendor. Nothing in this file is done to them.
//
// Nor to Claude (provider.VertexClaude: claude-opus-5-5,
// claude-sonnet-4-5@20250929), which Google serves on Anthropic's own
// Messages API at the project (Base(Anthropic)), under the same token.
// The gateway asks it as it asks any Anthropic vendor, relayed or built,
// and forwardOnce, last, moves the request to where Anthropic's Vertex SDK
// sends it (provider.VertexClaudeRequest): the model and whether it
// streams in the path (:streamRawPredict, :rawPredict), a count at
// count-tokens:rawPredict, anthropic_version vertex-2023-10-16 in the
// body. Tool search's beta goes under the name Claude Code gives it for
// Vertex (bedrockBetas).
// buildVertex is a streamGenerateContent body for Vertex AI, which names the
// model in its path and has none in here.
func buildVertex(r *Request, model string) ([]byte, error) {
req, err := geminiRequest(r, model, "vertex")
if err != nil {
return nil, err
}
return json.Marshal(req)
}
// vertexThinking is the thinkingConfig a request asks Vertex AI for, which
// turns away what a model can't do. Gemini 3 is sent its level, fitted to
// the model's own by vertexEffort, with no thoughts given back when the
// client turned reasoning off: the level is then the model's lowest, as
// Gemini 3 has no way to stop thinking altogether. 2.5 takes no level
// ("thinking_level is not supported by this model"): it is sent a budget
// in its own range. Turned off, Flash and Flash-Lite are sent 0, without
// thoughts asked for ("include_thoughts is only enabled when thinking is
// enabled"), and Pro, which can't stop ("does not support setting
// thinking_budget to 0"), its least. A Gemini before 2.5 doesn't think.
func vertexThinking(r *Request, model string) map[string]any {
m := strings.ToLower(model)
if strings.HasPrefix(m, "gemini-1") || strings.HasPrefix(m, "gemini-2.0") {
return nil
}
if r.Effort == "" && !r.Thinking && !r.ThinkOff {
return nil
}
if off, least, most, ok := vertexBudgets(m); ok {
if r.ThinkOff {
return map[string]any{"thinkingBudget": off}
}
tc := map[string]any{"includeThoughts": true}
if r.Effort != "" {
tc["thinkingBudget"] = min(max(budgetOf(r.Effort), least), most)
}
return tc
}
tc := map[string]any{"includeThoughts": !r.ThinkOff}
if r.Effort != "" {
tc["thinkingLevel"] = r.Effort
}
return tc
}
// vertexBudgets are the thinking budgets a Gemini 2.5 takes, as Google's
// model reference gives them: the one that turns its thinking off, or its
// least where it can't be, and its range. Not 2.5: false.
func vertexBudgets(model string) (off, least, most int, ok bool) {
switch {
case strings.HasPrefix(model, "gemini-2.5-pro"):
return 128, 128, 32768, true
case strings.HasPrefix(model, "gemini-2.5-flash-lite"):
return 0, 512, 24576, true
case strings.HasPrefix(model, "gemini-2.5-flash"):
return 0, 1, 24576, true
}
return 0, 0, 0, false
}
// vertexEffort is req at the level it goes to Vertex AI at. Vertex AI turns
// away a level the model doesn't have (gemini-3.8-flash: 400 "Thinking
// level is unsupported: THINKING_LEVEL_MINIMAL"; every Gemini 3 for xhigh),
// so the effort is the model's nearest of its own, and reasoning turned off
// is its lowest. A model whose levels aren't known is asked for no less
// than low and no more than high, which every Gemini 3 takes there.
func vertexEffort(req *Request, levels []string) *Request {
e := req.Effort
if req.ThinkOff {
e = "minimal"
}
if e == "" {
return req
}
if len(levels) > 0 {
e = fitEffort(e, levels)
} else if at := slices.Index(effortRank, e); at < slices.Index(effortRank, "low") {
e = "low"
} else if at > slices.Index(effortRank, "high") {
e = "high"
}
if e == req.Effort {
return req
}
r := *req
r.Effort = e
return &r
}
// withoutSignatures is req with no Gemini signature on its calls and
// texts, each call then going waved through (skipSignature); false when
// it had none.
func withoutSignatures(req *Request) (*Request, bool) {
r := dropSignatures(req, func(string) bool { return true })
return r, r != req
}