From 1e967334352eb4b75a59ad40520d0aa2a1eca939 Mon Sep 17 00:00:00 2001 From: yinwm Date: Thu, 19 Feb 2026 22:47:03 +0800 Subject: [PATCH] fix(agent): avoid consecutive system messages in compression Append emergency compression note to the original system prompt instead of creating a separate system message. Some APIs like Zhipu reject two consecutive system messages. --- pkg/agent/loop.go | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index 32e655710..f4627f907 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -779,31 +779,21 @@ func (al *AgentLoop) forceCompression(sessionKey string) { mid := len(conversation) / 2 // New history structure: - // 1. System Prompt - // 2. [Summary of dropped part] - synthesized - // 3. Second half of conversation - // 4. Last message - - // Simplified approach for emergency: Drop first half of conversation - // and rely on existing summary if present, or create a placeholder. + // 1. System Prompt (with compression note appended) + // 2. Second half of conversation + // 3. Last message droppedCount := mid keptConversation := conversation[mid:] newHistory := make([]providers.Message, 0) - newHistory = append(newHistory, history[0]) // System prompt - // Add a note about compression - compressionNote := fmt.Sprintf("[System: Emergency compression dropped %d oldest messages due to context limit]", droppedCount) - // If there was an existing summary, we might lose it if it was in the dropped part (which is just messages). - // The summary is stored separately in session.Summary, so it persists! - // We just need to ensure the user knows there's a gap. - - // We only modify the messages list here - newHistory = append(newHistory, providers.Message{ - Role: "system", - Content: compressionNote, - }) + // Append compression note to the original system prompt instead of adding a new system message + // This avoids having two consecutive system messages which some APIs (like Zhipu) reject + compressionNote := fmt.Sprintf("\n\n[System Note: Emergency compression dropped %d oldest messages due to context limit]", droppedCount) + enhancedSystemPrompt := history[0] + enhancedSystemPrompt.Content = enhancedSystemPrompt.Content + compressionNote + newHistory = append(newHistory, enhancedSystemPrompt) newHistory = append(newHistory, keptConversation...) newHistory = append(newHistory, history[len(history)-1]) // Last message