xonecas.io
60255
Zn

Zoea-Nova / commit 7cf375c

Phase 2: Implement turn-aware context composition

Replace getContextMemories() with turn-aware composition that preserves
complete tool history within the current conversation turn.

Changes:
- Modified getContextMemories() to use turn boundaries instead of loop slicing
- Current turn (from last user prompt onward) included in full without compression
- Historical turns (before current turn) compressed via extractLatestToolLoop
- Preserves multi-step tool calling (e.g., login → get_status → get_notifications)
- Session IDs and other tool results from current turn now retained

Test Updates:
- Added TestGetContextMemories_CurrentTurnPreserved: verifies multi-loop preservation
- Updated TestContextPromptSourcePriority: reflects turn-based (not priority-based) selection
- Updated TestZoeaListMysesCompaction: expects 2 results (historical + current turn)
- Updated TestLoopContextSlice: expects all 3 loops in current turn (was 1)
- Updated TestLoopContextSlice_ToolCallResultPairing: expects all loops in current turn

Rationale:
- Multi-step tool sequences require complete context to function correctly
- SpaceMolt session management requires login results to be available for subsequent calls
- Turn boundaries provide natural compression point without breaking tool chains

All tests pass.

xonecas2026-02-07 7cf375c6cc29c5e599d2e2fa71d5f3c1b69880d5 parent 134f332

4 files changed

internal/core/agent3_reproduction_test.go+11−9
@@ -303,7 +303,7 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
303303 t.Fatalf("AddMemory commander: %v", err)
304304 }
305305
306- // Step 3: Add OLD tool loop (should be excluded entirely)
306+ // Step 3: Add first tool loop (part of current turn, will be included)
307307 // Loop 1: get_status + get_system
308308 oldLoop1Assistant := constants.ToolCallStoragePrefix +
309309 "call_old_1:get_status:{}" + constants.ToolCallStorageRecordDelimiter +
@@ -327,7 +327,7 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
327327 t.Fatalf("AddMemory old result 2: %v", err)
328328 }
329329
330- // Step 4: Add another OLD tool loop (should also be excluded)
330+ // Step 4: Add second tool loop (part of current turn, will be included)
331331 // Loop 2: get_poi
332332 oldLoop2Assistant := constants.ToolCallStoragePrefix + "call_old_3:get_poi:{}"
333333
@@ -343,7 +343,7 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
343343 t.Fatalf("AddMemory old result 3: %v", err)
344344 }
345345
346- // Step 5: Add the MOST RECENT tool loop (should be included entirely)
346+ // Step 5: Add third tool loop (part of current turn, will be included)
347347 // Loop 3: mine + get_cargo
348348 recentLoopAssistant := constants.ToolCallStoragePrefix +
349349 "call_recent_1:mine:{}" + constants.ToolCallStorageRecordDelimiter +
@@ -367,7 +367,7 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
367367 t.Fatalf("AddMemory recent result 2: %v", err)
368368 }
369369
370- // Step 6: Get context memories using new loop composition
370+ // Step 6: Get context memories using turn-aware composition
371371 mysis := &Mysis{
372372 id: stored.ID,
373373 name: stored.Name,
@@ -380,7 +380,7 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
380380 t.Fatalf("getContextMemories() error: %v", err)
381381 }
382382
383- // Step 7: Validate loop composition rules
383+ // Step 7: Validate turn-aware composition rules
384384 t.Logf("Context has %d memories", len(memories))
385385
386386 // Collect all tool call IDs present in assistant messages
@@ -424,8 +424,9 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
424424 t.Errorf("Under loop composition, if a tool call is excluded, ALL its results must be excluded")
425425 }
426426
427- // CRITICAL ASSERTION 2: Only the most recent tool loop is present
428- // Old tool calls (call_old_*) should NOT be in context
427+ // CRITICAL ASSERTION 2: All tool loops within current turn are present
428+ // With turn-aware composition, all loops after the user prompt should be in context
429+ // Old tool calls (call_old_*) are part of the current turn and SHOULD be in context
429430 oldCallsPresent := []string{}
430431 for callID := range validToolCalls {
431432 if strings.Contains(callID, "call_old_") {
@@ -433,8 +434,9 @@ func TestLoopContextSlice_ToolCallResultPairing(t *testing.T) {
433434 }
434435 }
435436
436- if len(oldCallsPresent) > 0 {
437- t.Errorf("LOOP COMPOSITION VIOLATION: Found %d old tool calls in context (should only have most recent loop): %v",
437+ // Expect 3 old calls (call_old_1, call_old_2, call_old_3) since they're in current turn
438+ if len(oldCallsPresent) != 3 {
439+ t.Errorf("TURN COMPOSITION: Expected 3 old tool calls in current turn, found %d: %v",
438440 len(oldCallsPresent), oldCallsPresent)
439441 }
440442
internal/core/mysis.go+38−40
@@ -1454,44 +1454,38 @@ func (m *Mysis) findLastUserPromptIndex(memories []*store.Memory) int {
14541454 return -1
14551455 }
14561456
1457-// getContextMemories returns memories for LLM context using loop-based composition.
1458-// Composes context as: [system prompt] + [chosen prompt source] + [most recent tool loop].
1459-// This ensures stable, bounded context and eliminates orphaned tool sequencing.
1457+// getContextMemories returns memories for LLM context with turn-aware composition.
1458+// Composes context as: [system prompt] + [historical context] + [current turn].
14601459 //
14611460 // Returns:
14621461 // - []*store.Memory: Composed context slice ready for provider conversion
14631462 // - error: Database error from GetRecentMemories or GetSystemMemory
14641463 //
1465-// Context Composition:
1464+// Context Composition Strategy:
14661465 // 1. System prompt (if available) - provides mysis identity and mission
1467-// 2. Prompt source (by priority) - the user message that triggers this LLM turn
1468-// - Commander direct message (highest priority)
1469-// - Commander broadcast
1470-// - Swarm broadcast
1471-// - Synthetic nudge (if no prompt found)
1472-// 3. Latest tool loop (if any) - most recent tool call + all its results
1466+// 2. Historical context - memories before the current turn (compressed via extractLatestToolLoop)
1467+// 3. Current turn - ALL memories from the last user prompt onward (uncompressed)
14731468 //
1474-// Behavior:
1475-// - Fetches up to constants.MaxContextMessages (20) recent memories
1476-// - Creates synthetic nudge if no valid prompt source exists
1477-// - Nudge memory is NOT stored in database (temporary, in-memory only)
1478-// - Nudge counter increment happens in caller (handleLLMResponse)
1469+// Turn Boundary:
1470+// - "Current turn" starts at the most recent user-initiated prompt (direct message, broadcast, or nudge)
1471+// - Everything from that prompt forward is included in full to preserve multi-step tool reasoning
1472+// - Historical turns (before current turn boundary) are compressed to save context
14791473 //
14801474 // Rationale:
1475+// - Multi-step tool calling (login → get_status → get_notifications) requires complete tool history
1476+// - Historical turns can be compressed without breaking current reasoning
1477+// - Preserves session_id and other tool results from within the current turn
1478+// - Prevents orphaned tool calls by keeping complete tool loops together
14811479 //
1482-// Loop-based composition solves the orphaned tool result problem:
1483-// - Sliding window (MaxContextMessages) can split tool call/result pairs
1484-// - By extracting only the latest complete loop, we guarantee proper pairing
1485-// - Bounded context (3 components max) keeps token usage predictable
1486-// - Stable structure prevents OpenAI API errors from malformed sequences
1487-//
1488-// Example context:
1480+// Example context for multi-step turn:
14891481 //
14901482 // [
14911483 // {role: system, content: "You are Mysis Alpha..."},
14921484 // {role: user, source: direct, content: "Check ship status"},
1493-// {role: assistant, content: "[TOOL_CALLS]call_1:get_ship:{}"},
1494-// {role: tool, content: "Ship health: 100%"}
1485+// {role: assistant, content: "[TOOL_CALLS]call_1:login:{}"},
1486+// {role: tool, content: "call_1:session_id: abc123"},
1487+// {role: assistant, content: "[TOOL_CALLS]call_2:get_status:{\"session_id\":\"abc123\"}"},
1488+// {role: tool, content: "call_2:Ship health: 100%"}
14951489 // ]
14961490 func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
14971491 // Get all recent memories
@@ -1500,8 +1494,10 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
15001494 return nil, err
15011495 }
15021496
1503- // Build context: [system] + [prompt source] + [latest tool loop]
1504- result := make([]*store.Memory, 0, 10) // Pre-allocate for typical size
1497+ // Find current turn boundary
1498+ turnBoundaryIdx := m.findLastUserPromptIndex(allMemories)
1499+
1500+ result := make([]*store.Memory, 0, 20) // Pre-allocate for typical size
15051501
15061502 // Step 1: Add system prompt (if available)
15071503 system, err := m.store.GetSystemMemory(m.id)
@@ -1509,14 +1505,24 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
15091505 result = append(result, system)
15101506 }
15111507
1512- // Step 2: Select and add prompt source by priority
1513- // Priority: commander direct → last commander broadcast → last swarm broadcast → nudge
1514- promptSource := m.selectPromptSource(allMemories)
1508+ // Step 2: Add historical context (before current turn)
1509+ if turnBoundaryIdx > 0 {
1510+ historicalMemories := allMemories[:turnBoundaryIdx]
1511+ // Apply compression to historical turns - extract only latest tool loop
1512+ historicalToolLoop := m.extractLatestToolLoop(historicalMemories)
1513+ if len(historicalToolLoop) > 0 {
1514+ result = append(result, historicalToolLoop...)
1515+ }
1516+ }
15151517
1516- if promptSource == nil {
1517- // No prompt source found - generate synthetic nudge
1518+ // Step 3: Add current turn (from last user prompt onward)
1519+ if turnBoundaryIdx >= 0 {
1520+ // Include ALL messages from current turn without compression
1521+ currentTurnMemories := allMemories[turnBoundaryIdx:]
1522+ result = append(result, currentTurnMemories...)
1523+ } else {
1524+ // No user prompt found - generate synthetic nudge
15181525 // Note: Nudge counter increment happens in caller (handleLLMResponse)
1519- // We create a temporary memory that is NOT stored in database
15201526 nudgeContent := "Continue your mission. Check notifications and coordinate with the swarm."
15211527 nudgeMemory := &store.Memory{
15221528 Role: store.MemoryRoleUser,
@@ -1526,14 +1532,6 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
15261532 CreatedAt: time.Now(),
15271533 }
15281534 result = append(result, nudgeMemory)
1529- } else {
1530- result = append(result, promptSource)
1531- }
1532-
1533- // Step 3: Extract and add the most recent tool loop (if any)
1534- toolLoop := m.extractLatestToolLoop(allMemories)
1535- if len(toolLoop) > 0 {
1536- result = append(result, toolLoop...)
15371535 }
15381536
15391537 return result, nil
internal/core/mysis_test.go+115−21
@@ -646,26 +646,32 @@ func TestZoeaListMysesCompaction(t *testing.T) {
646646 t.Fatalf("getContextMemories() error: %v", err)
647647 }
648648
649- // Count zoea_list_myses tool results - should only have 1 (the latest)
649+ // With turn-aware composition:
650+ // - Historical context: latest tool loop from turns 0-3 (mysis-3)
651+ // - Current turn: all of turn 4 (user + assistant + tool for mysis-4)
652+ // So we expect 2 tool results: mysis-3 and mysis-4
650653 listResults := 0
654+ hasMysis3 := false
655+ hasMysis4 := false
651656 for _, m := range memories {
652657 if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"id":"mysis-`) {
653658 listResults++
659+ if strings.Contains(m.Content, `"id":"mysis-3"`) {
660+ hasMysis3 = true
661+ }
662+ if strings.Contains(m.Content, `"id":"mysis-4"`) {
663+ hasMysis4 = true
664+ }
654665 }
655666 }
656- if listResults != 1 {
657- t.Errorf("expected 1 zoea_list_myses result after compaction, got %d", listResults)
667+ if listResults != 2 {
668+ t.Errorf("expected 2 zoea_list_myses results (historical latest + current turn), got %d", listResults)
658669 }
659-
660- // Verify the latest result is kept (mysis-4, not mysis-0)
661- foundLatest := false
662- for _, m := range memories {
663- if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"id":"mysis-4"`) {
664- foundLatest = true
665- }
670+ if !hasMysis3 {
671+ t.Error("expected mysis-3 result from historical context compression")
666672 }
667- if !foundLatest {
668- t.Error("expected latest zoea_list_myses result (mysis-4) to be kept")
673+ if !hasMysis4 {
674+ t.Error("expected mysis-4 result from current turn")
669675 }
670676 }
671677
@@ -693,19 +699,19 @@ func TestContextPromptSourcePriority(t *testing.T) {
693699 expectedSender string
694700 }{
695701 {
696- name: "commander_direct_present",
702+ name: "most_recent_message_defines_turn",
697703 setupMemories: func() {
698704 // Add system prompt
699705 s.AddMemory(stored.ID, store.MemoryRoleSystem, store.MemorySourceSystem, "System prompt", "", "")
700- // Add commander direct message
706+ // Add commander direct message (older)
701707 s.AddMemory(stored.ID, store.MemoryRoleUser, store.MemorySourceDirect, "Commander direct message", "", commanderID)
702- // Add commander broadcast (should be ignored when direct exists)
708+ // Add commander broadcast (middle)
703709 s.AddMemory(stored.ID, store.MemoryRoleUser, store.MemorySourceBroadcast, "Commander broadcast", "", commanderID)
704- // Add swarm broadcast (should be ignored when direct exists)
710+ // Add swarm broadcast (most recent - this defines the current turn)
705711 s.AddMemory(stored.ID, store.MemoryRoleUser, store.MemorySourceBroadcast, "Swarm broadcast", "", swarmMysisID)
706712 },
707- expectedSource: store.MemorySourceDirect,
708- expectedSender: commanderID,
713+ expectedSource: store.MemorySourceBroadcast,
714+ expectedSender: swarmMysisID,
709715 },
710716 {
711717 name: "commander_broadcast_when_no_direct",
@@ -763,9 +769,8 @@ func TestContextPromptSourcePriority(t *testing.T) {
763769 }
764770
765771 // Find the prompt source in the returned memories
766- // Note: This test currently passes because getContextMemories happens to
767- // return memories in the correct order (most recent first), which matches
768- // the priority order. The implementation will be made explicit in Task 3.
772+ // With turn-aware composition, the current turn starts at the most recent user prompt.
773+ // The first user message in the result is the turn boundary.
769774 var foundPromptSource *store.Memory
770775 for _, m := range memories {
771776 if m.Role == store.MemoryRoleUser {
@@ -2031,3 +2036,92 @@ func TestFindLastUserPromptIndex(t *testing.T) {
20312036 })
20322037 }
20332038 }
2039+
2040+func TestGetContextMemories_CurrentTurnPreserved(t *testing.T) {
2041+ mysis, cleanup := setupTestMysis(t)
2042+ defer cleanup()
2043+
2044+ // Add historical turn (should be compressed)
2045+ err := mysis.store.AddMemory(mysis.id, store.MemoryRoleUser, store.MemorySourceDirect, "old user msg", "", "")
2046+ if err != nil {
2047+ t.Fatalf("AddMemory error: %v", err)
2048+ }
2049+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_old:get_status:{}", "", "")
2050+ if err != nil {
2051+ t.Fatalf("AddMemory error: %v", err)
2052+ }
2053+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_old:old status", "", "")
2054+ if err != nil {
2055+ t.Fatalf("AddMemory error: %v", err)
2056+ }
2057+
2058+ // Add current turn with multiple tool loops
2059+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleUser, store.MemorySourceDirect, "current user msg", "", "")
2060+ if err != nil {
2061+ t.Fatalf("AddMemory error: %v", err)
2062+ }
2063+ // First tool loop in current turn
2064+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_1:login:{}", "", "")
2065+ if err != nil {
2066+ t.Fatalf("AddMemory error: %v", err)
2067+ }
2068+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_1:session_id: abc123", "", "")
2069+ if err != nil {
2070+ t.Fatalf("AddMemory error: %v", err)
2071+ }
2072+ // Second tool loop in current turn
2073+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_2:get_status:{}", "", "")
2074+ if err != nil {
2075+ t.Fatalf("AddMemory error: %v", err)
2076+ }
2077+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_2:status data", "", "")
2078+ if err != nil {
2079+ t.Fatalf("AddMemory error: %v", err)
2080+ }
2081+
2082+ memories, err := mysis.getContextMemories()
2083+ if err != nil {
2084+ t.Fatalf("getContextMemories error: %v", err)
2085+ }
2086+
2087+ // Verify structure: current turn should have both tool loops
2088+ hasCurrentUser := false
2089+ hasLoginCall := false
2090+ hasLoginResult := false
2091+ hasStatusCall := false
2092+ hasStatusResult := false
2093+
2094+ for _, mem := range memories {
2095+ if mem.Role == store.MemoryRoleUser && mem.Content == "current user msg" {
2096+ hasCurrentUser = true
2097+ }
2098+ if mem.Role == store.MemoryRoleAssistant && strings.Contains(mem.Content, "call_1:login") {
2099+ hasLoginCall = true
2100+ }
2101+ if mem.Role == store.MemoryRoleTool && strings.Contains(mem.Content, "call_1") && strings.Contains(mem.Content, "session_id") {
2102+ hasLoginResult = true
2103+ }
2104+ if mem.Role == store.MemoryRoleAssistant && strings.Contains(mem.Content, "call_2:get_status") {
2105+ hasStatusCall = true
2106+ }
2107+ if mem.Role == store.MemoryRoleTool && strings.Contains(mem.Content, "call_2") {
2108+ hasStatusResult = true
2109+ }
2110+ }
2111+
2112+ if !hasCurrentUser {
2113+ t.Error("Missing current user message")
2114+ }
2115+ if !hasLoginCall {
2116+ t.Error("Missing login tool call from current turn")
2117+ }
2118+ if !hasLoginResult {
2119+ t.Error("Missing login result from current turn - THIS IS THE KEY FIX")
2120+ }
2121+ if !hasStatusCall {
2122+ t.Error("Missing status tool call from current turn")
2123+ }
2124+ if !hasStatusResult {
2125+ t.Error("Missing status result from current turn")
2126+ }
2127+}
internal/core/orphaned_tool_results_test.go+23−18
@@ -489,12 +489,13 @@ func TestLoopContextSlice(t *testing.T) {
489489 }
490490
491491 // Step 7: Assertions
492- // Expected: system prompt + commander direct + ONLY third loop (1 assistant + 2 tool results)
493- // Total: 5 memories
494- expectedCount := 5
492+ // With turn-aware composition:
493+ // Expected: system prompt + current turn (user prompt + all 3 tool loops)
494+ // Total: 11 memories (1 system + 1 user + 3 loops * 3 messages each)
495+ expectedCount := 11
495496 if len(memories) != expectedCount {
496497 t.Errorf("Expected %d memories in context, got %d", expectedCount, len(memories))
497- t.Logf("This test SHOULD fail initially - getContextMemories doesn't implement loop slicing yet")
498+ t.Logf("Turn-aware composition includes ALL tool loops within the current turn")
498499 for i, m := range memories {
499500 contentPreview := m.Content
500501 if len(contentPreview) > 50 {
@@ -515,7 +516,7 @@ func TestLoopContextSlice(t *testing.T) {
515516 memories[1].Role, memories[1].Source)
516517 }
517518
518- // ASSERTION 3: Only third loop tool calls should be present
519+ // ASSERTION 3: ALL tool loops within current turn should be present
519520 foundLoop1Calls := false
520521 foundLoop2Calls := false
521522 foundLoop3Calls := false
@@ -534,17 +535,17 @@ func TestLoopContextSlice(t *testing.T) {
534535 }
535536 }
536537
537- if foundLoop1Calls {
538- t.Errorf("First tool loop calls should be excluded from context (too old)")
538+ if !foundLoop1Calls {
539+ t.Errorf("First tool loop calls should be included in current turn")
539540 }
540- if foundLoop2Calls {
541- t.Errorf("Second tool loop calls should be excluded from context (too old)")
541+ if !foundLoop2Calls {
542+ t.Errorf("Second tool loop calls should be included in current turn")
542543 }
543544 if !foundLoop3Calls {
544- t.Errorf("Third tool loop calls should be included in context (most recent)")
545+ t.Errorf("Third tool loop calls should be included in current turn")
545546 }
546547
547- // ASSERTION 4: Only third loop tool results should be present
548+ // ASSERTION 4: ALL tool results within current turn should be present
548549 foundLoop1Results := false
549550 foundLoop2Results := false
550551 foundLoop3Results := false
@@ -563,14 +564,14 @@ func TestLoopContextSlice(t *testing.T) {
563564 }
564565 }
565566
566- if foundLoop1Results {
567- t.Errorf("First tool loop results should be excluded from context")
567+ if !foundLoop1Results {
568+ t.Errorf("First tool loop results should be included in current turn")
568569 }
569- if foundLoop2Results {
570- t.Errorf("Second tool loop results should be excluded from context")
570+ if !foundLoop2Results {
571+ t.Errorf("Second tool loop results should be included in current turn")
571572 }
572573 if !foundLoop3Results {
573- t.Errorf("Third tool loop results should be included in context")
574+ t.Errorf("Third tool loop results should be included in current turn")
574575 }
575576
576577 // ASSERTION 5: Verify no orphaned tool results (all results have matching calls)
@@ -594,8 +595,12 @@ func TestLoopContextSlice(t *testing.T) {
594595 }
595596 }
596597
597- // ASSERTION 6: Verify all expected results from loop 3 are present
598+ // ASSERTION 6: Verify all expected results from ALL loops are present
598599 expectedResults := map[string]bool{
600+ "call_loop1_1": false,
601+ "call_loop1_2": false,
602+ "call_loop2_1": false,
603+ "call_loop2_2": false,
599604 "call_loop3_1": false,
600605 "call_loop3_2": false,
601606 }
@@ -618,5 +623,5 @@ func TestLoopContextSlice(t *testing.T) {
618623
619624 t.Logf("Total memories in full history: 11 (system + prompt + 3 loops)")
620625 t.Logf("Total memories in context: %d", len(memories))
621- t.Logf("Expected context: 5 (system + prompt + last loop)")
626+ t.Logf("Expected context: 11 (system + current turn with all 3 loops)")
622627 }