@@ -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+}