diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index 8fd7328d1..1150b5ab3 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -980,8 +980,15 @@ func (al *AgentLoop) forceCompression(agent *AgentInstance, sessionKey string) { return } - // Helper to find the mid-point of the conversation + // Find the mid-point of the conversation, avoiding splitting tool call/result pairs. + // A tool-call message (role=assistant with ToolCalls) must be followed by its + // tool-result message (role=tool). Splitting between them causes API errors. mid := len(conversation) / 2 + if mid < len(conversation) && mid > 0 { + if conversation[mid].Role == "tool" { + mid++ // move past the tool result to keep the pair together + } + } // New history structure: // 1. System Prompt (with compression note appended) diff --git a/pkg/config/migration.go b/pkg/config/migration.go index 5deb09270..aade11c1b 100644 --- a/pkg/config/migration.go +++ b/pkg/config/migration.go @@ -367,7 +367,9 @@ func ConvertProvidersToModelList(cfg *Config) []ModelConfig { // Check if this is the user's configured provider if slices.Contains(m.providerNames, userProvider) && userModel != "" { - // Use the user's configured model instead of default + // Use the user's configured model instead of default. + // Also set ModelName so GetModelConfig(userModel) can find this entry. + mc.ModelName = userModel mc.Model = buildModelWithProtocol(m.protocol, userModel) } else if userProvider == "" && userModel != "" && !legacyModelNameApplied { // Legacy config: no explicit provider field but model is specified diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index 5dab9b03e..604331185 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -289,10 +289,11 @@ func parseResponse(body []byte) (*LLMResponse, error) { // It mirrors protocoltypes.Message but omits SystemParts, which is an // internal field that would be unknown to third-party endpoints. type openaiMessage struct { - Role string `json:"role"` - Content string `json:"content"` - ToolCalls []ToolCall `json:"tool_calls,omitempty"` - ToolCallID string `json:"tool_call_id,omitempty"` + Role string `json:"role"` + Content string `json:"content"` + ReasoningContent string `json:"reasoning_content,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` } // stripSystemParts converts []Message to []openaiMessage, dropping the @@ -302,10 +303,11 @@ func stripSystemParts(messages []Message) []openaiMessage { out := make([]openaiMessage, len(messages)) for i, m := range messages { out[i] = openaiMessage{ - Role: m.Role, - Content: m.Content, - ToolCalls: m.ToolCalls, - ToolCallID: m.ToolCallID, + Role: m.Role, + Content: m.Content, + ReasoningContent: m.ReasoningContent, + ToolCalls: m.ToolCalls, + ToolCallID: m.ToolCallID, } } return out diff --git a/pkg/tools/shell.go b/pkg/tools/shell.go index b52433b6f..3c671aed2 100644 --- a/pkg/tools/shell.go +++ b/pkg/tools/shell.go @@ -28,7 +28,7 @@ var defaultDenyPatterns = []*regexp.Regexp{ regexp.MustCompile(`\brm\s+-[rf]{1,2}\b`), regexp.MustCompile(`\bdel\s+/[fq]\b`), regexp.MustCompile(`\brmdir\s+/s\b`), - regexp.MustCompile(`\b(format|mkfs|diskpart)\b\s`), // Match disk wiping commands (must be followed by space/args) + regexp.MustCompile(`(?:^|\s)(format|mkfs|diskpart)\s`), // Match disk wiping commands, avoid matching --format flags regexp.MustCompile(`\bdd\s+if=`), regexp.MustCompile(`>\s*/dev/sd[a-z]\b`), // Block writes to disk devices (but allow /dev/null) regexp.MustCompile(`\b(shutdown|reboot|poweroff)\b`), @@ -287,10 +287,11 @@ func (t *ExecTool) guardCommand(command, cwd string) string { return "" } - pathPattern := regexp.MustCompile(`[A-Za-z]:\\[^\\\"']+|/[^\s\"']+`) - matches := pathPattern.FindAllString(cmd, -1) + pathPattern := regexp.MustCompile(`(?:^|\s)([A-Za-z]:\\[^\\"']+|/[a-zA-Z][^\s"']*)`) + matches := pathPattern.FindAllStringSubmatch(cmd, -1) - for _, raw := range matches { + for _, match := range matches { + raw := match[1] p, err := filepath.Abs(raw) if err != nil { continue