xonecas.io
60255
Zn

Zoea-Nova / commit 09607bf

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

xonecas2026-02-07 09607bf849e852edb1f4bf85f8e565623b5068cc parent 5c8f43f

9 files changed

internal/core/agent3_reproduction_test.go+9−48
@@ -8,17 +8,7 @@ import (
88 "github.com/xonecas/zoea-nova/internal/store"
99 )
1010
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.
11+// TestAgent3Finding_OrphanedToolResults ensures tool messages are excluded from context.
2212 func TestAgent3Finding_OrphanedToolResults(t *testing.T) {
2313 s, bus, cleanup := setupMysisTest(t)
2414 defer cleanup()
@@ -134,53 +124,24 @@ func TestAgent3Finding_OrphanedToolResults(t *testing.T) {
134124 t.Fatalf("getContextMemories() error: %v", err)
135125 }
136126
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{}
144127 for _, mem := range memories {
145128 if mem.Role == store.MemoryRoleTool {
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- }
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)
155133 }
156134 }
157135
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
171136 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
177137 for _, msg := range messages {
178138 if msg.Role == "tool" {
179- toolMessagesCount++
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)
180143 }
181144 }
182-
183- t.Logf("Final message list has %d tool messages", toolMessagesCount)
184145 }
185146
186147 // TestMemoriesToMessages_WithOrphanedResults documents that memoriesToMessages
internal/core/mysis.go+17−1
@@ -1275,8 +1275,10 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
12751275 return nil, err
12761276 }
12771277
1278+ filtered := a.filterContextMemories(recent)
1279+
12781280 // Apply compaction to remove redundant snapshot tool results
1279- compacted := a.compactSnapshots(recent)
1281+ compacted := a.compactSnapshots(filtered)
12801282
12811283 // Remove orphaned tool messages (results without corresponding tool calls)
12821284 // This ensures OpenAI Chat Completions API compliance
@@ -1305,6 +1307,20 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
13051307 return result, nil
13061308 }
13071309
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+
13081324 // compactSnapshots removes redundant snapshot tool results, keeping only the most recent
13091325 // result for each snapshot tool. This prevents state-heavy tools from crowding out
13101326 // conversation history while ensuring the latest state is available.
internal/core/mysis_test.go+13−65
@@ -529,49 +529,14 @@ 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
534532 for _, m := range memories {
535- if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"ship_id"`) {
536- shipResults++
533+ if m.Role == store.MemoryRoleTool {
534+ t.Fatalf("expected no tool memories in context, found: %s", m.Content)
537535 }
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++
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)
548538 }
549539 }
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- }
575540 }
576541
577542 func TestSystemPromptContainsSearchGuidance(t *testing.T) {
@@ -642,28 +607,14 @@ func TestZoeaListMysesCompaction(t *testing.T) {
642607 if err != nil {
643608 t.Fatalf("getContextMemories() error: %v", err)
644609 }
645-
646- // Count zoea_list_myses tool results - should only have 1 (the latest)
647- listResults := 0
648610 for _, m := range memories {
649- if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"id":"mysis-`) {
650- listResults++
611+ if m.Role == store.MemoryRoleTool {
612+ t.Fatalf("expected no tool memories in context, found: %s", m.Content)
651613 }
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
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)
662616 }
663617 }
664- if !foundLatest {
665- t.Error("expected latest zoea_list_myses result (mysis-4) to be kept")
666- }
667618 }
668619
669620 func TestMysisContextCompactionNonSnapshot(t *testing.T) {
@@ -686,16 +637,13 @@ func TestMysisContextCompactionNonSnapshot(t *testing.T) {
686637 if err != nil {
687638 t.Fatalf("getContextMemories() error: %v", err)
688639 }
689-
690- shipResults := 0
691640 for _, m := range memories {
692- if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"ship_id"`) {
693- shipResults++
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)
694646 }
695- }
696-
697- if shipResults != 2 {
698- t.Fatalf("expected 2 travel results to be kept, got %d", shipResults)
699647 }
700648 }
701649
internal/core/orphaned_tool_results_test.go+6−36
@@ -8,12 +8,7 @@ import (
88 "github.com/xonecas/zoea-nova/internal/store"
99 )
1010
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.
11+// TestOrphanedToolResults_ContextCompression ensures tool messages are excluded from context.
1712 func TestOrphanedToolResults_ContextCompression(t *testing.T) {
1813 s, bus, cleanup := setupMysisTest(t)
1914 defer cleanup()
@@ -88,8 +83,7 @@ func TestOrphanedToolResults_ContextCompression(t *testing.T) {
8883 }
8984 }
9085
91- // Step 5: Get context memories with compression (last 20 messages)
92- // This should simulate what happens in real usage
86+ // Step 5: Get context memories with filtering (last 20 messages)
9387 mysis := &Mysis{
9488 id: stored.ID,
9589 name: stored.Name,
@@ -102,37 +96,13 @@ func TestOrphanedToolResults_ContextCompression(t *testing.T) {
10296 t.Fatalf("getContextMemories() error: %v", err)
10397 }
10498
105- // Step 6: Check if orphaned tool results exist
106- validToolCalls := mysis.collectValidToolCallIDs(memories)
107- orphanedResults := []string{}
108-
10999 for _, mem := range memories {
110100 if mem.Role == store.MemoryRoleTool {
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- }
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)
125105 }
126- }
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))
136106 }
137107 }
138108
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, fmt.Errorf("http error %d: %s", httpResp.StatusCode, string(respBody))
85+ return nil, &HTTPError{Status: httpResp.StatusCode, Body: string(respBody)}
8686 }
8787
8888 // Capture session ID from response if present
internal/mcp/proxy.go+8−0
@@ -183,6 +183,14 @@ 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+
186194 return nil, fmt.Errorf("%w: %v", ErrToolRetryExhausted, lastErr)
187195 }
188196
internal/mcp/proxy_test.go+17−0
@@ -3,6 +3,7 @@ package mcp
33 import (
44 "context"
55 "encoding/json"
6+ "strings"
67 "testing"
78 )
89
@@ -149,3 +150,19 @@ func TestProxyAuthInterceptionLogout(t *testing.T) {
149150 t.Fatalf("expected account released for pilot, got %+v", accounts.released)
150151 }
151152 }
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+11−0
@@ -4,6 +4,7 @@ package mcp
44 import (
55 "context"
66 "encoding/json"
7+ "fmt"
78 )
89
910 // Request represents an MCP request.
@@ -28,6 +29,16 @@ type Error struct {
2829 Message string `json:"message"`
2930 }
3031
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+
3142 // Tool represents an MCP tool definition.
3243 type Tool struct {
3344 Name string `json:"name"`
internal/provider/opencode.go+36−0
@@ -240,12 +240,14 @@ 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)
243244 lastErr = fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
244245
245246 log.Warn().
246247 Str("provider", "opencode_zen").
247248 Int("status", resp.StatusCode).
248249 Int("attempt", attempt+1).
250+ Interface("rate_limit_headers", rateHeaders).
249251 Str("body", string(payload)).
250252 Msg("OpenCode retryable error")
251253
@@ -361,6 +363,40 @@ func opencodeEndpointForModel(model string) string {
361363 }
362364 }
363365
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+
364400 // Close closes idle HTTP connections
365401 func (p *OpenCodeProvider) Close() error {
366402 if p.httpClient != nil {