xonecas.io
60255
Zn

Zoea-Nova / commit 92cf244

Revert "fix(core,mcp): drop tool memories from context"

This reverts commit 09607bf849e852edb1f4bf85f8e565623b5068cc.

xonecas2026-02-07 92cf244880de6bed67eeee8d514e3f557d81df46 parent 09607bf

9 files changed

internal/core/agent3_reproduction_test.go+48−9
@@ -8,7 +8,17 @@ import (
88 "github.com/xonecas/zoea-nova/internal/store"
99 )
1010
11-// TestAgent3Finding_OrphanedToolResults ensures tool messages are excluded from context.
11+// TestAgent3Finding_OrphanedToolResults reproduces the EXACT finding from Agent 3:
12+// The request sent to OpenCode Zen contained orphaned tool results at messages 5-6.
13+//
14+// Agent 3 found:
15+// - Message 5: Tool result for call_-7908546686739502338 (get_system) - ORPHANED
16+// - Message 6: Tool result for call_-7908546686739502336 (get_ship) - ORPHANED
17+//
18+// The assistant message with these 4 tool calls was removed during context compression,
19+// but 2 of the 4 tool results remained.
20+//
21+// This test MUST FAIL to prove the bug exists in the actual code path.
1222 func TestAgent3Finding_OrphanedToolResults(t *testing.T) {
1323 s, bus, cleanup := setupMysisTest(t)
1424 defer cleanup()
@@ -124,24 +134,53 @@ func TestAgent3Finding_OrphanedToolResults(t *testing.T) {
124134 t.Fatalf("getContextMemories() error: %v", err)
125135 }
126136
137+ // Step 7: Check for orphaned tool results (the bug)
138+ validToolCalls := mysis.collectValidToolCallIDs(memories)
139+
140+ t.Logf("Context has %d memories", len(memories))
141+ t.Logf("Valid tool calls in context: %d", len(validToolCalls))
142+
143+ orphanedResults := []string{}
127144 for _, mem := range memories {
128145 if mem.Role == store.MemoryRoleTool {
129- t.Fatalf("expected no tool memories in context, found: %s", mem.Content)
130- }
131- if mem.Role == store.MemoryRoleAssistant && strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
132- t.Fatalf("expected no assistant tool-call memories in context, found: %s", mem.Content)
146+ // Extract tool call ID
147+ parts := strings.Split(mem.Content, constants.ToolCallStorageFieldDelimiter)
148+ if len(parts) > 0 {
149+ toolCallID := parts[0]
150+ if !validToolCalls[toolCallID] {
151+ orphanedResults = append(orphanedResults, toolCallID)
152+ t.Logf("Found orphaned tool result: %s (content: %.50s...)", toolCallID, mem.Content)
153+ }
154+ }
133155 }
134156 }
135157
158+ // CRITICAL ASSERTION: This test MUST FAIL if the bug exists
159+ // If orphanedResults is NOT empty, the bug is present
160+ if len(orphanedResults) == 0 {
161+ t.Logf("✅ No orphaned tool results found - bug is FIXED")
162+ } else {
163+ t.Errorf("🚨 Found %d orphaned tool results (THIS IS THE BUG): %v",
164+ len(orphanedResults), orphanedResults)
165+ t.Errorf("Agent 3 found call_-7908546686739502338 and call_-7908546686739502336 as orphaned")
166+ t.Errorf("Our cleanup functions should have removed these, but they didn't")
167+ }
168+
169+ // Step 8: Also check that our cleanup functions are being called
170+ // Convert to messages to see if they would be sent to the LLM
136171 messages := mysis.memoriesToMessages(memories)
172+
173+ t.Logf("Converted to %d messages for LLM", len(messages))
174+
175+ // Check for orphaned tool messages in the final message list
176+ toolMessagesCount := 0
137177 for _, msg := range messages {
138178 if msg.Role == "tool" {
139- t.Fatalf("expected no tool messages for LLM, found: %+v", msg)
140- }
141- if msg.Role == "assistant" && len(msg.ToolCalls) > 0 {
142- t.Fatalf("expected no assistant tool calls for LLM, found: %+v", msg)
179+ toolMessagesCount++
143180 }
144181 }
182+
183+ t.Logf("Final message list has %d tool messages", toolMessagesCount)
145184 }
146185
147186 // TestMemoriesToMessages_WithOrphanedResults documents that memoriesToMessages
internal/core/mysis.go+1−17
@@ -1275,10 +1275,8 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
12751275 return nil, err
12761276 }
12771277
1278- filtered := a.filterContextMemories(recent)
1279-
12801278 // Apply compaction to remove redundant snapshot tool results
1281- compacted := a.compactSnapshots(filtered)
1279+ compacted := a.compactSnapshots(recent)
12821280
12831281 // Remove orphaned tool messages (results without corresponding tool calls)
12841282 // This ensures OpenAI Chat Completions API compliance
@@ -1307,20 +1305,6 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
13071305 return result, nil
13081306 }
13091307
1310-func (m *Mysis) filterContextMemories(memories []*store.Memory) []*store.Memory {
1311- result := make([]*store.Memory, 0, len(memories))
1312- for _, mem := range memories {
1313- if mem.Role == store.MemoryRoleTool {
1314- continue
1315- }
1316- if mem.Role == store.MemoryRoleAssistant && strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
1317- continue
1318- }
1319- result = append(result, mem)
1320- }
1321- return result
1322-}
1323-
13241308 // compactSnapshots removes redundant snapshot tool results, keeping only the most recent
13251309 // result for each snapshot tool. This prevents state-heavy tools from crowding out
13261310 // conversation history while ensuring the latest state is available.
internal/core/mysis_test.go+65−13
@@ -529,14 +529,49 @@ func TestMysisContextCompaction(t *testing.T) {
529529 t.Errorf("expected first memory to be system prompt, got %s", memories[0].Role)
530530 }
531531
532+ // Count get_ship tool results - should only have 1 (the latest)
533+ shipResults := 0
532534 for _, m := range memories {
533- if m.Role == store.MemoryRoleTool {
534- t.Fatalf("expected no tool memories in context, found: %s", m.Content)
535+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"ship_id"`) {
536+ shipResults++
535537 }
536- if m.Role == store.MemoryRoleAssistant && strings.HasPrefix(m.Content, constants.ToolCallStoragePrefix) {
537- t.Fatalf("expected no assistant tool-call memories in context, found: %s", m.Content)
538+ }
539+ if shipResults != 1 {
540+ t.Errorf("expected 1 get_ship result after compaction, got %d", shipResults)
541+ }
542+
543+ // Count get_system tool results - should only have 1 (the latest)
544+ systemResults := 0
545+ for _, m := range memories {
546+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"system_id"`) {
547+ systemResults++
538548 }
539549 }
550+ if systemResults != 1 {
551+ t.Errorf("expected 1 get_system result after compaction, got %d", systemResults)
552+ }
553+
554+ // Non-snapshot tool result should be kept
555+ mineResults := 0
556+ for _, m := range memories {
557+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"result":"mining"`) {
558+ mineResults++
559+ }
560+ }
561+ if mineResults != 1 {
562+ t.Errorf("expected 1 mine result (non-snapshot), got %d", mineResults)
563+ }
564+
565+ // Verify the latest get_ship result is kept (ship_4, not ship_0)
566+ foundLatestShip := false
567+ for _, m := range memories {
568+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"ship_id":"ship_4"`) {
569+ foundLatestShip = true
570+ }
571+ }
572+ if !foundLatestShip {
573+ t.Error("expected latest get_ship result (ship_4) to be kept")
574+ }
540575 }
541576
542577 func TestSystemPromptContainsSearchGuidance(t *testing.T) {
@@ -607,14 +642,28 @@ func TestZoeaListMysesCompaction(t *testing.T) {
607642 if err != nil {
608643 t.Fatalf("getContextMemories() error: %v", err)
609644 }
645+
646+ // Count zoea_list_myses tool results - should only have 1 (the latest)
647+ listResults := 0
610648 for _, m := range memories {
611- if m.Role == store.MemoryRoleTool {
612- t.Fatalf("expected no tool memories in context, found: %s", m.Content)
649+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"id":"mysis-`) {
650+ listResults++
613651 }
614- if m.Role == store.MemoryRoleAssistant && strings.HasPrefix(m.Content, constants.ToolCallStoragePrefix) {
615- t.Fatalf("expected no assistant tool-call memories in context, found: %s", m.Content)
652+ }
653+ if listResults != 1 {
654+ t.Errorf("expected 1 zoea_list_myses result after compaction, got %d", listResults)
655+ }
656+
657+ // Verify the latest result is kept (mysis-4, not mysis-0)
658+ foundLatest := false
659+ for _, m := range memories {
660+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"id":"mysis-4"`) {
661+ foundLatest = true
616662 }
617663 }
664+ if !foundLatest {
665+ t.Error("expected latest zoea_list_myses result (mysis-4) to be kept")
666+ }
618667 }
619668
620669 func TestMysisContextCompactionNonSnapshot(t *testing.T) {
@@ -637,14 +686,17 @@ func TestMysisContextCompactionNonSnapshot(t *testing.T) {
637686 if err != nil {
638687 t.Fatalf("getContextMemories() error: %v", err)
639688 }
689+
690+ shipResults := 0
640691 for _, m := range memories {
641- if m.Role == store.MemoryRoleTool {
642- t.Fatalf("expected no tool memories in context, found: %s", m.Content)
643- }
644- if m.Role == store.MemoryRoleAssistant && strings.HasPrefix(m.Content, constants.ToolCallStoragePrefix) {
645- t.Fatalf("expected no assistant tool-call memories in context, found: %s", m.Content)
692+ if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"ship_id"`) {
693+ shipResults++
646694 }
647695 }
696+
697+ if shipResults != 2 {
698+ t.Fatalf("expected 2 travel results to be kept, got %d", shipResults)
699+ }
648700 }
649701
650702 func TestComputeMemoryStats(t *testing.T) {
internal/core/orphaned_tool_results_test.go+36−6
@@ -8,7 +8,12 @@ import (
88 "github.com/xonecas/zoea-nova/internal/store"
99 )
1010
11-// TestOrphanedToolResults_ContextCompression ensures tool messages are excluded from context.
11+// TestOrphanedToolResults_ContextCompression tests the bug found in investigation:
12+// When context window compression removes an assistant message with parallel tool calls,
13+// but keeps some of the tool results, we get orphaned tool results that violate OpenAI API.
14+//
15+// This reproduces Agent 3's finding: Messages 5-6 contained orphaned tool results
16+// for tool calls that were removed during context compression.
1217 func TestOrphanedToolResults_ContextCompression(t *testing.T) {
1318 s, bus, cleanup := setupMysisTest(t)
1419 defer cleanup()
@@ -83,7 +88,8 @@ func TestOrphanedToolResults_ContextCompression(t *testing.T) {
8388 }
8489 }
8590
86- // Step 5: Get context memories with filtering (last 20 messages)
91+ // Step 5: Get context memories with compression (last 20 messages)
92+ // This should simulate what happens in real usage
8793 mysis := &Mysis{
8894 id: stored.ID,
8995 name: stored.Name,
@@ -96,14 +102,38 @@ func TestOrphanedToolResults_ContextCompression(t *testing.T) {
96102 t.Fatalf("getContextMemories() error: %v", err)
97103 }
98104
105+ // Step 6: Check if orphaned tool results exist
106+ validToolCalls := mysis.collectValidToolCallIDs(memories)
107+ orphanedResults := []string{}
108+
99109 for _, mem := range memories {
100110 if mem.Role == store.MemoryRoleTool {
101- t.Fatalf("expected no tool memories in context, found: %s", mem.Content)
102- }
103- if mem.Role == store.MemoryRoleAssistant && strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
104- t.Fatalf("expected no assistant tool-call memories in context, found: %s", mem.Content)
111+ // Extract tool call ID from result
112+ idx := len(mem.Content)
113+ for i, ch := range mem.Content {
114+ if ch == ':' {
115+ idx = i
116+ break
117+ }
118+ }
119+ if idx > 0 && idx < len(mem.Content) {
120+ toolCallID := mem.Content[:idx]
121+ if !validToolCalls[toolCallID] {
122+ orphanedResults = append(orphanedResults, toolCallID)
123+ }
124+ }
105125 }
106126 }
127+
128+ // ASSERTION: This test should FAIL initially, proving the bug exists
129+ // After fix, orphanedResults should be empty
130+ if len(orphanedResults) > 0 {
131+ t.Errorf("Found %d orphaned tool results (expected 0): %v",
132+ len(orphanedResults), orphanedResults)
133+ t.Logf("This proves the bug: orphaned tool results exist after context compression")
134+ t.Logf("Valid tool calls in context: %d", len(validToolCalls))
135+ t.Logf("Total memories in context: %d", len(memories))
136+ }
107137 }
108138
109139 // TestRemoveOrphanedToolMessages_WithOrphanedResults tests that removeOrphanedToolMessages
internal/mcp/client.go+1−1
@@ -82,7 +82,7 @@ func (c *Client) send(ctx context.Context, req *Request) (*Response, error) {
8282
8383 if httpResp.StatusCode != http.StatusOK {
8484 respBody, _ := io.ReadAll(httpResp.Body)
85- return nil, &HTTPError{Status: httpResp.StatusCode, Body: string(respBody)}
85+ return nil, fmt.Errorf("http error %d: %s", httpResp.StatusCode, string(respBody))
8686 }
8787
8888 // Capture session ID from response if present
internal/mcp/proxy.go+0−8
@@ -183,14 +183,6 @@ func (p *Proxy) callUpstreamWithRetry(ctx context.Context, name string, args int
183183 lastErr = err
184184 }
185185
186- var httpErr *HTTPError
187- if errors.As(lastErr, &httpErr) && httpErr.Status == 429 {
188- return &ToolResult{
189- Content: []ContentBlock{{Type: "text", Text: httpErr.Error()}},
190- IsError: true,
191- }, nil
192- }
193-
194186 return nil, fmt.Errorf("%w: %v", ErrToolRetryExhausted, lastErr)
195187 }
196188
internal/mcp/proxy_test.go+0−17
@@ -3,7 +3,6 @@ package mcp
33 import (
44 "context"
55 "encoding/json"
6- "strings"
76 "testing"
87 )
98
@@ -150,19 +149,3 @@ func TestProxyAuthInterceptionLogout(t *testing.T) {
150149 t.Fatalf("expected account released for pilot, got %+v", accounts.released)
151150 }
152151 }
153-
154-func TestProxyCallToolUpstreamRateLimitReturnsToolResult(t *testing.T) {
155- upstream := &mockUpstream{err: &HTTPError{Status: 429, Body: "Rate limited. Too many requests"}}
156- proxy := NewProxy(upstream)
157-
158- result, err := proxy.CallTool(context.Background(), CallerContext{}, "rate_limited_tool", json.RawMessage(`{}`))
159- if err != nil {
160- t.Fatalf("expected no error, got %v", err)
161- }
162- if result == nil || !result.IsError {
163- t.Fatalf("expected error ToolResult, got %+v", result)
164- }
165- if len(result.Content) == 0 || !strings.Contains(result.Content[0].Text, "http error 429") {
166- t.Fatalf("expected 429 text, got %+v", result)
167- }
168-}
internal/mcp/types.go+0−11
@@ -4,7 +4,6 @@ package mcp
44 import (
55 "context"
66 "encoding/json"
7- "fmt"
87 )
98
109 // Request represents an MCP request.
@@ -29,16 +28,6 @@ type Error struct {
2928 Message string `json:"message"`
3029 }
3130
32-// HTTPError represents a non-200 HTTP response from the MCP server.
33-type HTTPError struct {
34- Status int
35- Body string
36-}
37-
38-func (e *HTTPError) Error() string {
39- return fmt.Sprintf("http error %d: %s", e.Status, e.Body)
40-}
41-
4231 // Tool represents an MCP tool definition.
4332 type Tool struct {
4433 Name string `json:"name"`
internal/provider/opencode.go+0−36
@@ -240,14 +240,12 @@ func (p *OpenCodeProvider) createChatCompletion(ctx context.Context, req openai.
240240 if resp.StatusCode == 429 || resp.StatusCode == 500 || resp.StatusCode == 502 ||
241241 resp.StatusCode == 503 || resp.StatusCode == 504 {
242242 payload, _ := io.ReadAll(resp.Body)
243- rateHeaders := opencodeRateLimitHeaders(resp.Header)
244243 lastErr = fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
245244
246245 log.Warn().
247246 Str("provider", "opencode_zen").
248247 Int("status", resp.StatusCode).
249248 Int("attempt", attempt+1).
250- Interface("rate_limit_headers", rateHeaders).
251249 Str("body", string(payload)).
252250 Msg("OpenCode retryable error")
253251
@@ -363,40 +361,6 @@ func opencodeEndpointForModel(model string) string {
363361 }
364362 }
365363
366-func opencodeRateLimitHeaders(header http.Header) map[string]string {
367- if header == nil {
368- return nil
369- }
370-
371- keys := []string{
372- "Retry-After",
373- "X-RateLimit-Limit",
374- "X-RateLimit-Remaining",
375- "X-RateLimit-Reset",
376- "X-RateLimit-Reset-Requests",
377- "X-RateLimit-Reset-Tokens",
378- "X-RateLimit-Limit-Requests",
379- "X-RateLimit-Limit-Tokens",
380- "X-RateLimit-Remaining-Requests",
381- "X-RateLimit-Remaining-Tokens",
382- "X-Request-Id",
383- "OpenAI-Request-Id",
384- }
385-
386- result := make(map[string]string)
387- for _, key := range keys {
388- if value := header.Get(key); value != "" {
389- result[key] = value
390- }
391- }
392-
393- if len(result) == 0 {
394- return nil
395- }
396-
397- return result
398-}
399-
400364 // Close closes idle HTTP connections
401365 func (p *OpenCodeProvider) Close() error {
402366 if p.httpClient != nil {