fix: preserve native Anthropic inference controls
This commit is contained in:
1 parent
8e92125fb6
commit
986c233d39
4 files changed
+137
-11
No files matched your search
@@ -5,7 +5,7 @@ streaming agent loop, tool registry, session storage, compaction, and
|
||||
token budgeting needed to run a multi-provider agent in production. BYO
|
||||
concrete tools, BYO provider clients, BYO memory/knowledge-graph plugins.
|
||||
|
||||
> **Status: v0.3.5.** Latest tagged release. The `runtime` API
|
||||
> **Status: v0.3.6.** Latest tagged release. The `runtime` API
|
||||
> surface may still shift in the v0.x line — pin your version.
|
||||
|
||||
## Why Harness
|
||||
@@ -376,12 +376,12 @@ a real failure.
|
||||
|
||||
## Status
|
||||
|
||||
`v0.3.5` is the latest tagged release. The v0.x line follows Go
|
||||
`v0.3.6` is the latest tagged release. The v0.x line follows Go
|
||||
module semver: minor bumps may break API, patch bumps are bug-fix
|
||||
only. Pin your dependency:
|
||||
|
||||
```bash
|
||||
go get github.com/sausheong/[email protected].5
|
||||
go get github.com/sausheong/[email protected].6
|
||||
```
|
||||
|
||||
Likely sources of v0.x churn before a v1.0.0:
|
||||
|
||||
+37
-6
@@ -108,6 +108,31 @@ const (
|
||||
ReasoningHigh ReasoningMode = "high"
|
||||
)
|
||||
|
||||
// NativeReasoningConfig preserves an inbound provider reasoning request when
|
||||
// Harness is used as a protocol proxy. A nil config means the client omitted
|
||||
// the provider's reasoning controls; this is deliberately different from an
|
||||
// explicit disabled config because some models think by default.
|
||||
//
|
||||
// Type is "adaptive", "enabled", or "disabled". BudgetTokens applies to
|
||||
// enabled thinking and Effort is one of "low", "medium", "high", or "max".
|
||||
// Providers that cannot represent the requested semantics must reject the
|
||||
// request instead of silently changing it.
|
||||
type NativeReasoningConfig struct {
|
||||
Type string
|
||||
BudgetTokens int64
|
||||
Effort string
|
||||
Display string
|
||||
// OutputFormat is the provider-native structured-output format object.
|
||||
// It is kept as JSON so a protocol proxy can preserve schemas without
|
||||
// forcing provider-specific types into the portable llm package.
|
||||
OutputFormat json.RawMessage
|
||||
}
|
||||
|
||||
// Enabled reports whether the client explicitly requested reasoning.
|
||||
func (c *NativeReasoningConfig) Enabled() bool {
|
||||
return c != nil && (c.Type == "adaptive" || c.Type == "enabled")
|
||||
}
|
||||
|
||||
// ParseReasoningMode parses a config string into a ReasoningMode.
|
||||
// Accepts "" or "off" for ReasoningOff; "low", "medium", "high" for
|
||||
// the named levels. Case-sensitive. Returns an error for unknown values.
|
||||
@@ -128,12 +153,15 @@ func ParseReasoningMode(s string) (ReasoningMode, error) {
|
||||
|
||||
// ChatRequest is the input to a streaming chat call.
|
||||
type ChatRequest struct {
|
||||
Model string
|
||||
Messages []Message
|
||||
Tools []ToolDef
|
||||
MaxTokens int
|
||||
Temperature float64
|
||||
SystemPrompt string
|
||||
Model string
|
||||
Messages []Message
|
||||
Tools []ToolDef
|
||||
MaxTokens int
|
||||
Temperature float64
|
||||
// TemperatureSet distinguishes an omitted sampling parameter from an
|
||||
// explicit zero, which is material when proxying an existing API request.
|
||||
TemperatureSet bool
|
||||
SystemPrompt string
|
||||
// SystemPromptParts, when non-empty, replaces SystemPrompt. Providers
|
||||
// that support caching emit one block per part, attaching cache markers
|
||||
// per Cache flag. Providers that don't support caching concatenate
|
||||
@@ -147,6 +175,9 @@ type ChatRequest struct {
|
||||
// SessionID is routing metadata only and is not sent to providers.
|
||||
SessionID string
|
||||
Reasoning ReasoningMode // zero value = ReasoningOff; safe default
|
||||
// NativeReasoning takes precedence over the portable Reasoning knob and
|
||||
// preserves exact inbound proxy semantics, including omitted vs disabled.
|
||||
NativeReasoning *NativeReasoningConfig
|
||||
}
|
||||
|
||||
// Usage tracks token usage.
|
||||
|
||||
@@ -98,10 +98,54 @@ func (p *AnthropicProvider) buildMessageParams(req llm.ChatRequest) anthropic.Me
|
||||
if sys := buildAnthropicSystem(req); len(sys) > 0 {
|
||||
params.System = sys
|
||||
}
|
||||
if req.Temperature > 0 && !anthropicAdaptiveThinkingOnly(model) {
|
||||
if req.TemperatureSet {
|
||||
// Protocol-proxy requests must preserve even an explicit zero. Let the
|
||||
// upstream API reject an incompatible client combination unchanged.
|
||||
params.Temperature = anthropic.Float(req.Temperature)
|
||||
} else if req.Temperature > 0 && !anthropicAdaptiveThinkingOnly(model) {
|
||||
// Portable Harness callers retain the provider's compatibility guard:
|
||||
// adaptive-only models reject sampling parameters while thinking.
|
||||
params.Temperature = anthropic.Float(req.Temperature)
|
||||
}
|
||||
if anthropicAdaptiveThinkingOnly(model) {
|
||||
if native := req.NativeReasoning; native != nil {
|
||||
// Proxy mode preserves exactly what the client requested. Do not infer a
|
||||
// model default, silently remove sampling controls, or enlarge max_tokens:
|
||||
// the upstream should accept or reject the same semantics as a direct call.
|
||||
switch native.Type {
|
||||
case "adaptive":
|
||||
params.Thinking = anthropic.ThinkingConfigParamUnion{
|
||||
OfAdaptive: &anthropic.ThinkingConfigAdaptiveParam{
|
||||
Display: anthropic.ThinkingConfigAdaptiveDisplay(native.Display),
|
||||
},
|
||||
}
|
||||
case "enabled":
|
||||
params.Thinking = anthropic.ThinkingConfigParamUnion{
|
||||
OfEnabled: &anthropic.ThinkingConfigEnabledParam{
|
||||
BudgetTokens: native.BudgetTokens,
|
||||
Display: anthropic.ThinkingConfigEnabledDisplay(native.Display),
|
||||
},
|
||||
}
|
||||
case "disabled":
|
||||
params.Thinking = anthropic.ThinkingConfigParamUnion{
|
||||
OfDisabled: &anthropic.ThinkingConfigDisabledParam{},
|
||||
}
|
||||
}
|
||||
if native.Effort != "" {
|
||||
params.OutputConfig = anthropic.OutputConfigParam{
|
||||
Effort: anthropic.OutputConfigEffort(native.Effort),
|
||||
}
|
||||
}
|
||||
if len(native.OutputFormat) > 0 {
|
||||
var format anthropic.JSONOutputFormatParam
|
||||
if err := json.Unmarshal(native.OutputFormat, &format); err == nil {
|
||||
params.OutputConfig.Format = format
|
||||
} else {
|
||||
// Decoders are expected to validate this before constructing the
|
||||
// request. Keep this defensive diagnostic for direct Harness callers.
|
||||
slog.Error("invalid native Anthropic output format", "err", err)
|
||||
}
|
||||
}
|
||||
} else if anthropicAdaptiveThinkingOnly(model) {
|
||||
// Fable 5 / Mythos 5 / Opus 4.7+ / Opus 5 / Sonnet 5 reject the
|
||||
// legacy enabled+budget_tokens config (400 "thinking.type.enabled
|
||||
// is not supported"). They take {type:"adaptive"} plus
|
||||
|
||||
@@ -668,6 +668,57 @@ func TestAdaptiveThinkingModels_ReasoningOffOmitsThinking(t *testing.T) {
|
||||
assert.NotContains(t, got, `"output_config"`)
|
||||
}
|
||||
|
||||
func TestNativeReasoningConfigPreservesInboundProxySemantics(t *testing.T) {
|
||||
p := &AnthropicProvider{}
|
||||
tests := []struct {
|
||||
name string
|
||||
config *llm.NativeReasoningConfig
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "adaptive effort max",
|
||||
config: &llm.NativeReasoningConfig{Type: "adaptive", Effort: "max"},
|
||||
want: []string{`"thinking":{"type":"adaptive"}`, `"output_config":{"effort":"max"}`},
|
||||
},
|
||||
{
|
||||
name: "enabled exact budget",
|
||||
config: &llm.NativeReasoningConfig{Type: "enabled", BudgetTokens: 3072},
|
||||
want: []string{`"thinking":{"budget_tokens":3072,"type":"enabled"}`},
|
||||
},
|
||||
{
|
||||
name: "explicit disabled",
|
||||
config: &llm.NativeReasoningConfig{Type: "disabled"},
|
||||
want: []string{`"thinking":{"type":"disabled"}`},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
params := p.buildMessageParams(llm.ChatRequest{
|
||||
Model: "claude-opus-4-8-global", MaxTokens: 2048,
|
||||
Messages: []llm.Message{{Role: "user", Content: "hello"}},
|
||||
NativeReasoning: test.config,
|
||||
})
|
||||
raw, err := json.Marshal(params)
|
||||
require.NoError(t, err)
|
||||
for _, want := range test.want {
|
||||
assert.Contains(t, string(raw), want)
|
||||
}
|
||||
assert.Contains(t, string(raw), `"max_tokens":2048`, "proxy mode must not enlarge max_tokens")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeProxyPreservesExplicitZeroTemperature(t *testing.T) {
|
||||
p := &AnthropicProvider{}
|
||||
params := p.buildMessageParams(llm.ChatRequest{
|
||||
Model: "claude-opus-4-8-global", MaxTokens: 32, Temperature: 0, TemperatureSet: true,
|
||||
Messages: []llm.Message{{Role: "user", Content: "hello"}},
|
||||
})
|
||||
raw, err := json.Marshal(params)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(raw), `"temperature":0`)
|
||||
}
|
||||
|
||||
func TestAdaptiveThinkingModels_TemperatureDropped(t *testing.T) {
|
||||
p := NewAnthropicProvider("test-key", "")
|
||||
for _, model := range []string{
|
||||
|
||||
Reference in new issue
Block a user