From 2f0b679e75743a732d75b62683abc31859c45a56 Mon Sep 17 00:00:00 2001 From: cointem Date: Fri, 20 Feb 2026 01:22:04 +0800 Subject: [PATCH] feat: allow temperature 0 and distinguish unset --- pkg/agent/instance.go | 6 +++--- pkg/agent/instance_test.go | 34 ++++++++++++++++++++++++++++++++-- pkg/config/config.go | 3 +-- pkg/config/config_test.go | 8 ++++---- pkg/migrate/config.go | 2 +- pkg/migrate/migrate_test.go | 7 +++++-- 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/pkg/agent/instance.go b/pkg/agent/instance.go index 55fced78b..37b253685 100644 --- a/pkg/agent/instance.go +++ b/pkg/agent/instance.go @@ -83,9 +83,9 @@ func NewAgentInstance( maxTokens = 8192 } - temperature := defaults.Temperature - if temperature == 0 { - temperature = 0.7 + temperature := 0.7 + if defaults.Temperature != nil { + temperature = *defaults.Temperature } // Resolve fallback candidates diff --git a/pkg/agent/instance_test.go b/pkg/agent/instance_test.go index baf824911..fcc8e9bea 100644 --- a/pkg/agent/instance_test.go +++ b/pkg/agent/instance_test.go @@ -20,12 +20,14 @@ func TestNewAgentInstance_UsesDefaultsTemperatureAndMaxTokens(t *testing.T) { Workspace: tmpDir, Model: "test-model", MaxTokens: 1234, - Temperature: 1.0, MaxToolIterations: 5, }, }, } + configuredTemp := 1.0 + cfg.Agents.Defaults.Temperature = &configuredTemp + provider := &mockProvider{} agent := NewAgentInstance(nil, &cfg.Agents.Defaults, cfg, provider) @@ -50,7 +52,35 @@ func TestNewAgentInstance_DefaultsTemperatureWhenZero(t *testing.T) { Workspace: tmpDir, Model: "test-model", MaxTokens: 1234, - Temperature: 0, + MaxToolIterations: 5, + }, + }, + } + + configuredTemp := 0.0 + cfg.Agents.Defaults.Temperature = &configuredTemp + + provider := &mockProvider{} + agent := NewAgentInstance(nil, &cfg.Agents.Defaults, cfg, provider) + + if agent.Temperature != 0.0 { + t.Fatalf("Temperature = %f, want %f", agent.Temperature, 0.0) + } +} + +func TestNewAgentInstance_DefaultsTemperatureWhenUnset(t *testing.T) { + tmpDir, err := os.MkdirTemp("", "agent-instance-test-*") + if err != nil { + t.Fatalf("Failed to create temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + + cfg := &config.Config{ + Agents: config.AgentsConfig{ + Defaults: config.AgentDefaults{ + Workspace: tmpDir, + Model: "test-model", + MaxTokens: 1234, MaxToolIterations: 5, }, }, diff --git a/pkg/config/config.go b/pkg/config/config.go index 682996bd6..3bdb6f030 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -147,7 +147,7 @@ type AgentDefaults struct { ImageModel string `json:"image_model,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_IMAGE_MODEL"` ImageModelFallbacks []string `json:"image_model_fallbacks,omitempty"` MaxTokens int `json:"max_tokens" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOKENS"` - Temperature float64 `json:"temperature" env:"PICOCLAW_AGENTS_DEFAULTS_TEMPERATURE"` + Temperature *float64 `json:"temperature,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_TEMPERATURE"` MaxToolIterations int `json:"max_tool_iterations" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"` } @@ -330,7 +330,6 @@ func DefaultConfig() *Config { Provider: "", Model: "glm-4.7", MaxTokens: 8192, - Temperature: 0.7, MaxToolIterations: 20, }, }, diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 47916d155..7e706d8ce 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -237,8 +237,8 @@ func TestDefaultConfig_MaxToolIterations(t *testing.T) { func TestDefaultConfig_Temperature(t *testing.T) { cfg := DefaultConfig() - if cfg.Agents.Defaults.Temperature == 0 { - t.Error("Temperature should not be zero") + if cfg.Agents.Defaults.Temperature != nil { + t.Error("Temperature should be nil when not provided") } } @@ -334,8 +334,8 @@ func TestConfig_Complete(t *testing.T) { if cfg.Agents.Defaults.Model == "" { t.Error("Model should not be empty") } - if cfg.Agents.Defaults.Temperature == 0 { - t.Error("Temperature should have default value") + if cfg.Agents.Defaults.Temperature != nil { + t.Error("Temperature should be nil when not provided") } if cfg.Agents.Defaults.MaxTokens == 0 { t.Error("MaxTokens should not be zero") diff --git a/pkg/migrate/config.go b/pkg/migrate/config.go index 57032e566..665719f2a 100644 --- a/pkg/migrate/config.go +++ b/pkg/migrate/config.go @@ -76,7 +76,7 @@ func ConvertConfig(data map[string]interface{}) (*config.Config, []string, error cfg.Agents.Defaults.MaxTokens = int(v) } if v, ok := getFloat(defaults, "temperature"); ok { - cfg.Agents.Defaults.Temperature = v + cfg.Agents.Defaults.Temperature = &v } if v, ok := getFloat(defaults, "max_tool_iterations"); ok { cfg.Agents.Defaults.MaxToolIterations = int(v) diff --git a/pkg/migrate/migrate_test.go b/pkg/migrate/migrate_test.go index e930d45f4..f6f8b7908 100644 --- a/pkg/migrate/migrate_test.go +++ b/pkg/migrate/migrate_test.go @@ -275,8 +275,11 @@ func TestConvertConfig(t *testing.T) { if cfg.Agents.Defaults.MaxTokens != 4096 { t.Errorf("MaxTokens = %d, want %d", cfg.Agents.Defaults.MaxTokens, 4096) } - if cfg.Agents.Defaults.Temperature != 0.5 { - t.Errorf("Temperature = %f, want %f", cfg.Agents.Defaults.Temperature, 0.5) + if cfg.Agents.Defaults.Temperature == nil { + t.Fatalf("Temperature is nil, want %f", 0.5) + } + if *cfg.Agents.Defaults.Temperature != 0.5 { + t.Errorf("Temperature = %f, want %f", *cfg.Agents.Defaults.Temperature, 0.5) } if cfg.Agents.Defaults.Workspace != "~/.picoclaw/workspace" { t.Errorf("Workspace = %q, want %q", cfg.Agents.Defaults.Workspace, "~/.picoclaw/workspace")