fix: support Claude Opus 5 / Sonnet 5 adaptive thinking; unique Gemini tool-call IDs

Anthropic: add claude-opus-5 and claude-sonnet-5 to the adaptive-thinking-only
model list. These models also think by default when the thinking param is
omitted (unlike Fable 5/Mythos/Opus 4.7/4.8), so ReasoningOff now sends an
explicit {type:"disabled"} on them instead of relying on omission.

Gemini: replace the empty-FunctionCall.ID fallback (function name) with a
synthetic toolu_-shaped random ID. The name-based fallback wasn't unique —
repeated calls to the same tool in one conversation collided on one ID,
which produced duplicate tool_use ids if that history was later replayed to
a different (Anthropic) model on a routing switch, rejected with a 400.
This commit is contained in:
sausheong committed 2026-07-28 13:40:22 +08:00
1 parent c7846eeab9
commit 445614757b
6 files changed
+144 -560

No files matched your search

+40 -1
View File
@@ -2,10 +2,15 @@ package gemini
import (
"context"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"log/slog"
"os"
"strings"
"time"
"google.golang.org/genai"
"github.com/sausheong/harness/llm"
@@ -213,9 +218,25 @@ func (p *GeminiProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
if err != nil {
argsJSON = []byte("{}")
}
// Gemini's FunctionCall.ID is optional and is
// frequently empty (single, non-parallel calls
// commonly omit it). Falling back to fc.Name is
// wrong on two counts: it isn't a valid
// provider-neutral tool-call ID shape, and it
// isn't unique — two calls to the same tool in
// one turn, or the same tool called again later
// in the conversation (routine for a coding
// agent re-reading files), collide on the same
// ID. When that history is later replayed to
// Anthropic on a routing switch, duplicate
// tool_use ids across the conversation are
// rejected with a 400. Mint a synthetic
// toolu_-shaped ID instead, matching Anthropic's
// own ID shape so a later replay to Anthropic
// looks like any other tool_use id.
id := fc.ID
if id == "" {
id = fc.Name
id = syntheticToolCallID()
}
events <- llm.ChatEvent{
Type: llm.EventToolCallStart,
@@ -251,6 +272,24 @@ func updateUsage(_ *llm.Usage, prompt, candidates int) *llm.Usage {
return &llm.Usage{InputTokens: prompt, OutputTokens: candidates}
}
// syntheticToolCallID mints a unique, Anthropic-shaped tool-call ID
// ("toolu_" + 16 hex chars) for Gemini function calls that arrive with
// no ID of their own. Uniqueness (not just shape) is the point: a
// name-based fallback collides whenever the same tool is called more
// than once in a conversation, which produces duplicate tool_use ids if
// that history is later replayed to Anthropic. Falls back to a
// time+PID-derived suffix if crypto/rand is unavailable — still unique
// within one process lifetime, which is all a single conversation needs.
func syntheticToolCallID() string {
b := make([]byte, 8)
if _, err := rand.Read(b); err == nil {
return "toolu_" + hex.EncodeToString(b)
}
mix := uint64(time.Now().UnixNano()) ^ uint64(os.Getpid())
binary.BigEndian.PutUint64(b, mix)
return "toolu_" + hex.EncodeToString(b)
}
// geminiUnsupportedFields are JSON Schema fields Gemini's "OpenAPI 3.0
// subset" rejects. This is the broadest strip set across the four
// providers — most of the cross-provider portability gap shows up here.