@@ -1,400 +0,0 @@
1-package core
2-
3-import (
4- "strings"
5- "testing"
6-
7- "github.com/xonecas/zoea-nova/internal/constants"
8- "github.com/xonecas/zoea-nova/internal/store"
9-)
10-
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.
22-func TestAgent3Finding_OrphanedToolResults(t *testing.T) {
23- s, bus, cleanup := setupMysisTest(t)
24- defer cleanup()
25-
26- stored, err := s.CreateMysis("agent3-repro", "mock", "test-model", 0.7)
27- if err != nil {
28- t.Fatalf("CreateMysis() error: %v", err)
29- }
30-
31- // Recreate the exact sequence from the investigation logs
32-
33- // Step 1: Add system prompt
34- err = s.AddMemory(stored.ID, store.MemoryRoleSystem, store.MemorySourceSystem,
35- "You are an AI pilot in SpaceMolt", "", "")
36- if err != nil {
37- t.Fatalf("AddMemory system: %v", err)
38- }
39-
40- // Step 2: Add initial bootstrap sequence
41- // - zoea_claim_account
42- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
43- constants.ToolCallStoragePrefix+"call_claim:zoea_claim_account:{}", "", "")
44- if err != nil {
45- t.Fatalf("AddMemory claim: %v", err)
46- }
47-
48- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
49- "call_claim"+constants.ToolCallStorageFieldDelimiter+"Use the game's login tool", "", "")
50- if err != nil {
51- t.Fatalf("AddMemory claim result: %v", err)
52- }
53-
54- // - login
55- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
56- constants.ToolCallStoragePrefix+"call_login:login:{}", "", "")
57- if err != nil {
58- t.Fatalf("AddMemory login: %v", err)
59- }
60-
61- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
62- "call_login"+constants.ToolCallStorageFieldDelimiter+"Login successful", "", "")
63- if err != nil {
64- t.Fatalf("AddMemory login result: %v", err)
65- }
66-
67- // Step 3: Add the problematic assistant message with 4 PARALLEL tool calls
68- // THIS is the message that gets removed by context compression
69- assistantWith4Calls := constants.ToolCallStoragePrefix +
70- "call_-7908546686739502339:get_status:{}" + constants.ToolCallStorageFieldDelimiter +
71- "call_-7908546686739502338:get_system:{}" + constants.ToolCallStorageFieldDelimiter +
72- "call_-7908546686739502337:get_poi:{}" + constants.ToolCallStorageFieldDelimiter +
73- "call_-7908546686739502336:get_ship:{}"
74-
75- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
76- assistantWith4Calls, "", "")
77- if err != nil {
78- t.Fatalf("AddMemory 4 parallel calls: %v", err)
79- }
80-
81- // Step 4: Add tool results for all 4 calls
82- toolResults := []struct {
83- callID string
84- tool string
85- content string
86- }{
87- {"call_-7908546686739502339", "get_status", "Status result"},
88- {"call_-7908546686739502338", "get_system", "System result"},
89- {"call_-7908546686739502337", "get_poi", "POI result"},
90- {"call_-7908546686739502336", "get_ship", "Ship result"},
91- }
92-
93- for _, tr := range toolResults {
94- result := tr.callID + constants.ToolCallStorageFieldDelimiter + tr.content
95- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
96- result, "", "")
97- if err != nil {
98- t.Fatalf("AddMemory tool result %s: %v", tr.tool, err)
99- }
100- }
101-
102- // Step 5: Add more conversation to push the 4-call assistant message out of context
103- // We need enough messages to exceed MaxContextMessages (20) so that message #5 (the 4-call assistant) is dropped
104- for i := 0; i < 15; i++ {
105- // Add assistant with single tool call
106- callID := "call_extra_" + string(rune(i))
107- assistantMsg := constants.ToolCallStoragePrefix + callID + ":extra_tool:{}"
108- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
109- assistantMsg, "", "")
110- if err != nil {
111- t.Fatalf("AddMemory extra assistant %d: %v", i, err)
112- }
113-
114- // Add corresponding tool result
115- toolResult := callID + constants.ToolCallStorageFieldDelimiter + "extra result"
116- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
117- toolResult, "", "")
118- if err != nil {
119- t.Fatalf("AddMemory extra tool %d: %v", i, err)
120- }
121- }
122-
123- // Step 6: Get context memories (this simulates what happens before sending to LLM)
124- // Note: MaxContextMessages is a constant (20), defined in constants.MaxContextMessages
125- mysis := &Mysis{
126- id: stored.ID,
127- name: stored.Name,
128- store: s,
129- bus: bus,
130- }
131-
132- memories, _, err := mysis.getContextMemories()
133- if err != nil {
134- t.Fatalf("getContextMemories() error: %v", err)
135- }
136-
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{}
144- for _, mem := range memories {
145- 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- }
155- }
156- }
157-
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
171- 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
177- for _, msg := range messages {
178- if msg.Role == "tool" {
179- toolMessagesCount++
180- }
181- }
182-
183- t.Logf("Final message list has %d tool messages", toolMessagesCount)
184-}
185-
186-// TestLoopContextSlice_ToolCallResultPairing validates that tool results are
187-// present ONLY when their matching tool call is present in the context.
188-//
189-// This test is part of Task 2 of the Loop Context Composition plan:
190-// - Tool results must be paired with their tool calls
191-// - No orphaned tool results should exist
192-// - If a tool call is excluded from context, its results must also be excluded
193-//
194-// Expected behavior under new loop composition:
195-// - Only the most recent tool-call loop is included
196-// - Older tool loops are completely excluded (both calls and results)
197-//
198-// This test MUST FAIL initially because the current implementation uses
199-// MaxContextMessages sliding window which can split tool call/result pairs.
200-func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
201- s, bus, cleanup := setupMysisTest(t)
202- defer cleanup()
203-
204- stored, err := s.CreateMysis("loop-pairing-test", "mock", "test-model", 0.7)
205- if err != nil {
206- t.Fatalf("CreateMysis() error: %v", err)
207- }
208-
209- // Step 1: Add system prompt
210- err = s.AddMemory(stored.ID, store.MemoryRoleSystem, store.MemorySourceSystem,
211- "System prompt", "", "")
212- if err != nil {
213- t.Fatalf("AddMemory system: %v", err)
214- }
215-
216- // Step 2: Add commander message (current turn start)
217- err = s.AddMemory(stored.ID, store.MemoryRoleUser, store.MemorySourceDirect,
218- "Check your status and mine some resources", "", "")
219- if err != nil {
220- t.Fatalf("AddMemory commander: %v", err)
221- }
222-
223- // Step 3: Add first tool loop (part of current turn, will be included)
224- // Loop 1: get_status + get_system
225- oldLoop1Assistant := constants.ToolCallStoragePrefix +
226- "call_old_1:get_status:{}" + constants.ToolCallStorageRecordDelimiter +
227- "call_old_2:get_system:{}"
228-
229- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
230- oldLoop1Assistant, "", "")
231- if err != nil {
232- t.Fatalf("AddMemory old loop 1: %v", err)
233- }
234-
235- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
236- "call_old_1"+constants.ToolCallStorageFieldDelimiter+"Old status result", "", "")
237- if err != nil {
238- t.Fatalf("AddMemory old result 1: %v", err)
239- }
240-
241- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
242- "call_old_2"+constants.ToolCallStorageFieldDelimiter+"Old system result", "", "")
243- if err != nil {
244- t.Fatalf("AddMemory old result 2: %v", err)
245- }
246-
247- // Step 4: Add second tool loop (part of current turn, will be included)
248- // Loop 2: get_poi
249- oldLoop2Assistant := constants.ToolCallStoragePrefix + "call_old_3:get_poi:{}"
250-
251- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
252- oldLoop2Assistant, "", "")
253- if err != nil {
254- t.Fatalf("AddMemory old loop 2: %v", err)
255- }
256-
257- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
258- "call_old_3"+constants.ToolCallStorageFieldDelimiter+"Old POI result", "", "")
259- if err != nil {
260- t.Fatalf("AddMemory old result 3: %v", err)
261- }
262-
263- // Step 5: Add third tool loop (part of current turn, will be included)
264- // Loop 3: mine + get_cargo
265- recentLoopAssistant := constants.ToolCallStoragePrefix +
266- "call_recent_1:mine:{}" + constants.ToolCallStorageRecordDelimiter +
267- "call_recent_2:get_cargo:{}"
268-
269- err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM,
270- recentLoopAssistant, "", "")
271- if err != nil {
272- t.Fatalf("AddMemory recent loop: %v", err)
273- }
274-
275- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
276- "call_recent_1"+constants.ToolCallStorageFieldDelimiter+"Mining successful", "", "")
277- if err != nil {
278- t.Fatalf("AddMemory recent result 1: %v", err)
279- }
280-
281- err = s.AddMemory(stored.ID, store.MemoryRoleTool, store.MemorySourceTool,
282- "call_recent_2"+constants.ToolCallStorageFieldDelimiter+"Cargo: ore_iron x10", "", "")
283- if err != nil {
284- t.Fatalf("AddMemory recent result 2: %v", err)
285- }
286-
287- // Step 6: Get context memories using turn-aware composition
288- mysis := &Mysis{
289- id: stored.ID,
290- name: stored.Name,
291- store: s,
292- bus: bus,
293- }
294-
295- memories, _, err := mysis.getContextMemories()
296- if err != nil {
297- t.Fatalf("getContextMemories() error: %v", err)
298- }
299-
300- // Step 7: Validate turn-aware composition rules
301- t.Logf("Context has %d memories", len(memories))
302-
303- // Collect all tool call IDs present in assistant messages
304- validToolCalls := make(map[string]bool)
305- for _, mem := range memories {
306- if mem.Role == store.MemoryRoleAssistant &&
307- strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
308- calls := mysis.parseStoredToolCalls(mem.Content)
309- for _, call := range calls {
310- validToolCalls[call.ID] = true
311- t.Logf("Found tool call in context: %s (%s)", call.ID, call.Name)
312- }
313- }
314- }
315-
316- // Collect all tool result IDs present in tool messages
317- presentToolResults := make(map[string]bool)
318- for _, mem := range memories {
319- if mem.Role == store.MemoryRoleTool {
320- parts := strings.Split(mem.Content, constants.ToolCallStorageFieldDelimiter)
321- if len(parts) > 0 {
322- toolCallID := parts[0]
323- presentToolResults[toolCallID] = true
324- t.Logf("Found tool result in context: %s", toolCallID)
325- }
326- }
327- }
328-
329- // CRITICAL ASSERTION 1: No orphaned tool results
330- // Every tool result must have a matching tool call in the context
331- orphanedResults := []string{}
332- for resultID := range presentToolResults {
333- if !validToolCalls[resultID] {
334- orphanedResults = append(orphanedResults, resultID)
335- }
336- }
337-
338- if len(orphanedResults) > 0 {
339- t.Errorf("LOOP COMPOSITION VIOLATION: Found %d orphaned tool results (results without tool calls): %v",
340- len(orphanedResults), orphanedResults)
341- t.Errorf("Under loop composition, if a tool call is excluded, ALL its results must be excluded")
342- }
343-
344- // CRITICAL ASSERTION 2: All tool loops within current turn are present
345- // With turn-aware composition, all loops after the user prompt should be in context
346- // Old tool calls (call_old_*) are part of the current turn and SHOULD be in context
347- oldCallsPresent := []string{}
348- for callID := range validToolCalls {
349- if strings.Contains(callID, "call_old_") {
350- oldCallsPresent = append(oldCallsPresent, callID)
351- }
352- }
353-
354- // Expect 3 old calls (call_old_1, call_old_2, call_old_3) since they're in current turn
355- if len(oldCallsPresent) != 3 {
356- t.Errorf("TURN COMPOSITION: Expected 3 old tool calls in current turn, found %d: %v",
357- len(oldCallsPresent), oldCallsPresent)
358- }
359-
360- // CRITICAL ASSERTION 3: The most recent loop is complete
361- // Both call_recent_1 and call_recent_2 should be present
362- expectedRecentCalls := []string{"call_recent_1", "call_recent_2"}
363- missingRecentCalls := []string{}
364- for _, expectedCall := range expectedRecentCalls {
365- if !validToolCalls[expectedCall] {
366- missingRecentCalls = append(missingRecentCalls, expectedCall)
367- }
368- }
369-
370- if len(missingRecentCalls) > 0 {
371- t.Errorf("LOOP COMPOSITION VIOLATION: Missing %d tool calls from most recent loop: %v",
372- len(missingRecentCalls), missingRecentCalls)
373- t.Errorf("The most recent tool loop must be included completely")
374- }
375-
376- // CRITICAL ASSERTION 4: All tool calls have matching results
377- // This is the inverse check - no orphaned tool calls
378- missingResults := []string{}
379- for callID := range validToolCalls {
380- if !presentToolResults[callID] {
381- missingResults = append(missingResults, callID)
382- }
383- }
384-
385- if len(missingResults) > 0 {
386- t.Errorf("LOOP COMPOSITION VIOLATION: Found %d tool calls without results: %v",
387- len(missingResults), missingResults)
388- t.Errorf("Under loop composition, tool calls and their results must be paired")
389- }
390-
391- // SUCCESS: If we get here with no errors, loop composition is working correctly
392- if len(orphanedResults) == 0 && len(oldCallsPresent) == 0 &&
393- len(missingRecentCalls) == 0 && len(missingResults) == 0 {
394- t.Logf("Loop composition validated:")
395- t.Logf(" - No orphaned tool results")
396- t.Logf(" - Only most recent loop present")
397- t.Logf(" - Complete tool call/result pairing")
398- t.Logf(" - Found %d tool calls, %d tool results", len(validToolCalls), len(presentToolResults))
399- }
400-}