@@ -794,6 +794,214 @@ func TestContextPromptSourcePriority(t *testing.T) {
794794 }
795795 }
796796
797+func TestSelectPromptSourceHelper(t *testing.T) {
798+ s, bus, cleanup := setupMysisTest(t)
799+ defer cleanup()
800+
801+ stored, err := s.CreateMysis("helper-test", "mock", "test-model", 0.7)
802+ if err != nil {
803+ t.Fatalf("CreateMysis() error: %v", err)
804+ }
805+
806+ mock := provider.NewMock("mock", "response")
807+ mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus)
808+
809+ tests := []struct {
810+ name string
811+ memories []*store.Memory
812+ expectedSource store.MemorySource
813+ expectedSender string
814+ expectNil bool
815+ }{
816+ {
817+ name: "nil_memories",
818+ memories: nil,
819+ expectNil: true,
820+ },
821+ {
822+ name: "empty_memories",
823+ memories: []*store.Memory{},
824+ expectNil: true,
825+ },
826+ {
827+ name: "only_system_messages",
828+ memories: []*store.Memory{
829+ {Role: store.MemoryRoleSystem, Source: store.MemorySourceSystem, Content: "System"},
830+ {Role: store.MemoryRoleAssistant, Source: store.MemorySourceLLM, Content: "Response"},
831+ },
832+ expectNil: true,
833+ },
834+ {
835+ name: "direct_message_priority",
836+ memories: []*store.Memory{
837+ {Role: store.MemoryRoleUser, Source: store.MemorySourceDirect, Content: "Direct", SenderID: "cmd"},
838+ {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Broadcast", SenderID: ""},
839+ },
840+ expectedSource: store.MemorySourceDirect,
841+ expectedSender: "cmd",
842+ },
843+ {
844+ name: "commander_broadcast_when_no_direct",
845+ memories: []*store.Memory{
846+ {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Commander broadcast", SenderID: ""},
847+ {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Swarm broadcast", SenderID: "mysis-1"},
848+ },
849+ expectedSource: store.MemorySourceBroadcast,
850+ expectedSender: "",
851+ },
852+ {
853+ name: "swarm_broadcast_when_no_commander",
854+ memories: []*store.Memory{
855+ {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Swarm broadcast 1", SenderID: "mysis-1"},
856+ {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Swarm broadcast 2", SenderID: "mysis-2"},
857+ },
858+ expectedSource: store.MemorySourceBroadcast,
859+ expectedSender: "mysis-1",
860+ },
861+ }
862+
863+ for _, tt := range tests {
864+ t.Run(tt.name, func(t *testing.T) {
865+ result := mysis.selectPromptSource(tt.memories)
866+
867+ if tt.expectNil {
868+ if result != nil {
869+ t.Errorf("expected nil, got %+v", result)
870+ }
871+ return
872+ }
873+
874+ if result == nil {
875+ t.Fatal("expected non-nil result, got nil")
876+ }
877+
878+ if result.Source != tt.expectedSource {
879+ t.Errorf("expected source=%s, got %s", tt.expectedSource, result.Source)
880+ }
881+
882+ if result.SenderID != tt.expectedSender {
883+ t.Errorf("expected sender_id=%s, got %s", tt.expectedSender, result.SenderID)
884+ }
885+ })
886+ }
887+}
888+
889+func TestExtractLatestToolLoopHelper(t *testing.T) {
890+ s, bus, cleanup := setupMysisTest(t)
891+ defer cleanup()
892+
893+ stored, err := s.CreateMysis("helper-test", "mock", "test-model", 0.7)
894+ if err != nil {
895+ t.Fatalf("CreateMysis() error: %v", err)
896+ }
897+
898+ mock := provider.NewMock("mock", "response")
899+ mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus)
900+
901+ tests := []struct {
902+ name string
903+ memories []*store.Memory
904+ expectedCount int
905+ expectNil bool
906+ }{
907+ {
908+ name: "nil_memories",
909+ memories: nil,
910+ expectNil: true,
911+ },
912+ {
913+ name: "empty_memories",
914+ memories: []*store.Memory{},
915+ expectNil: true,
916+ },
917+ {
918+ name: "no_tool_calls",
919+ memories: []*store.Memory{
920+ {Role: store.MemoryRoleSystem, Content: "System"},
921+ {Role: store.MemoryRoleUser, Content: "User message"},
922+ {Role: store.MemoryRoleAssistant, Content: "Text response"},
923+ },
924+ expectNil: true,
925+ },
926+ {
927+ name: "single_tool_call_with_results",
928+ memories: []*store.Memory{
929+ {Role: store.MemoryRoleSystem, Content: "System"},
930+ {Role: store.MemoryRoleUser, Content: "User"},
931+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_1:get_status:{}"},
932+ {Role: store.MemoryRoleTool, Content: "call_1:status result"},
933+ },
934+ expectedCount: 2, // 1 tool call + 1 result
935+ },
936+ {
937+ name: "tool_call_with_no_results_yet",
938+ memories: []*store.Memory{
939+ {Role: store.MemoryRoleSystem, Content: "System"},
940+ {Role: store.MemoryRoleUser, Content: "User"},
941+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_1:get_status:{}"},
942+ },
943+ expectedCount: 1, // Just the tool call
944+ },
945+ {
946+ name: "multiple_loops_returns_most_recent",
947+ memories: []*store.Memory{
948+ {Role: store.MemoryRoleSystem, Content: "System"},
949+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_old:get_status:{}"},
950+ {Role: store.MemoryRoleTool, Content: "call_old:old result"},
951+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_new:get_poi:{}"},
952+ {Role: store.MemoryRoleTool, Content: "call_new:new result"},
953+ },
954+ expectedCount: 2, // Most recent loop only
955+ },
956+ {
957+ name: "tool_call_with_multiple_results",
958+ memories: []*store.Memory{
959+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_1:get_status:{}|call_2:get_system:{}"},
960+ {Role: store.MemoryRoleTool, Content: "call_1:result 1"},
961+ {Role: store.MemoryRoleTool, Content: "call_2:result 2"},
962+ },
963+ expectedCount: 3, // 1 tool call message + 2 results
964+ },
965+ }
966+
967+ for _, tt := range tests {
968+ t.Run(tt.name, func(t *testing.T) {
969+ result := mysis.extractLatestToolLoop(tt.memories)
970+
971+ if tt.expectNil {
972+ if result != nil {
973+ t.Errorf("expected nil, got %d memories", len(result))
974+ }
975+ return
976+ }
977+
978+ if len(result) != tt.expectedCount {
979+ t.Errorf("expected %d memories, got %d", tt.expectedCount, len(result))
980+ for i, m := range result {
981+ t.Logf(" result[%d]: role=%s content=%.50s", i, m.Role, m.Content)
982+ }
983+ }
984+
985+ // Verify first is tool call
986+ if len(result) > 0 {
987+ if result[0].Role != store.MemoryRoleAssistant {
988+ t.Errorf("first memory should be assistant, got %s", result[0].Role)
989+ }
990+ if !strings.HasPrefix(result[0].Content, "[TOOL_CALLS]") {
991+ t.Errorf("first memory should have tool calls prefix")
992+ }
993+ }
994+
995+ // Verify rest are tool results
996+ for i := 1; i < len(result); i++ {
997+ if result[i].Role != store.MemoryRoleTool {
998+ t.Errorf("memory[%d] should be tool result, got %s", i, result[i].Role)
999+ }
1000+ }
1001+ })
1002+ }
1003+}
1004+
7971005 func TestMysisContextCompactionNonSnapshot(t *testing.T) {
7981006 s, bus, cleanup := setupMysisTest(t)
7991007 defer cleanup()