@@ -812,98 +812,6 @@ func TestContextPromptSourcePriority(t *testing.T) {
812812 }
813813 }
814814
815-func TestSelectPromptSourceHelper(t *testing.T) {
816- s, bus, cleanup := setupMysisTest(t)
817- defer cleanup()
818-
819- stored, err := s.CreateMysis("helper-test", "mock", "test-model", 0.7)
820- if err != nil {
821- t.Fatalf("CreateMysis() error: %v", err)
822- }
823-
824- mock := provider.NewMock("mock", "response")
825- mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus)
826-
827- tests := []struct {
828- name string
829- memories []*store.Memory
830- expectedSource store.MemorySource
831- expectedSender string
832- expectNil bool
833- }{
834- {
835- name: "nil_memories",
836- memories: nil,
837- expectNil: true,
838- },
839- {
840- name: "empty_memories",
841- memories: []*store.Memory{},
842- expectNil: true,
843- },
844- {
845- name: "only_system_messages",
846- memories: []*store.Memory{
847- {Role: store.MemoryRoleSystem, Source: store.MemorySourceSystem, Content: "System"},
848- {Role: store.MemoryRoleAssistant, Source: store.MemorySourceLLM, Content: "Response"},
849- },
850- expectNil: true,
851- },
852- {
853- name: "direct_message_priority",
854- memories: []*store.Memory{
855- {Role: store.MemoryRoleUser, Source: store.MemorySourceDirect, Content: "Direct", SenderID: "cmd"},
856- {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Broadcast", SenderID: ""},
857- },
858- expectedSource: store.MemorySourceDirect,
859- expectedSender: "cmd",
860- },
861- {
862- name: "commander_broadcast_when_no_direct",
863- memories: []*store.Memory{
864- {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Commander broadcast", SenderID: ""},
865- {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Swarm broadcast", SenderID: "mysis-1"},
866- },
867- expectedSource: store.MemorySourceBroadcast,
868- expectedSender: "",
869- },
870- {
871- name: "swarm_broadcast_when_no_commander",
872- memories: []*store.Memory{
873- {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Swarm broadcast 1", SenderID: "mysis-1"},
874- {Role: store.MemoryRoleUser, Source: store.MemorySourceBroadcast, Content: "Swarm broadcast 2", SenderID: "mysis-2"},
875- },
876- expectedSource: store.MemorySourceBroadcast,
877- expectedSender: "mysis-1",
878- },
879- }
880-
881- for _, tt := range tests {
882- t.Run(tt.name, func(t *testing.T) {
883- result := mysis.selectPromptSource(tt.memories)
884-
885- if tt.expectNil {
886- if result != nil {
887- t.Errorf("expected nil, got %+v", result)
888- }
889- return
890- }
891-
892- if result == nil {
893- t.Fatal("expected non-nil result, got nil")
894- }
895-
896- if result.Source != tt.expectedSource {
897- t.Errorf("expected source=%s, got %s", tt.expectedSource, result.Source)
898- }
899-
900- if result.SenderID != tt.expectedSender {
901- t.Errorf("expected sender_id=%s, got %s", tt.expectedSender, result.SenderID)
902- }
903- })
904- }
905-}
906-
907815 func TestExtractLatestToolLoopHelper(t *testing.T) {
908816 s, bus, cleanup := setupMysisTest(t)
909817 defer cleanup()
@@ -2125,3 +2033,91 @@ func TestGetContextMemories_CurrentTurnPreserved(t *testing.T) {
21252033 t.Error("Missing status result from current turn")
21262034 }
21272035 }
2036+
2037+func TestGetContextMemories_NoUserPrompt(t *testing.T) {
2038+ s, bus, cleanup := setupMysisTest(t)
2039+ defer cleanup()
2040+
2041+ stored, _ := s.CreateMysis("no-prompt-test", "mock", "test-model", 0.7)
2042+ mock := provider.NewMock("mock", "response")
2043+ mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus)
2044+
2045+ // Add only system and assistant messages (no user prompt)
2046+ err := s.AddMemory(stored.ID, store.MemoryRoleSystem, store.MemorySourceSystem, "System prompt", "", "")
2047+ if err != nil {
2048+ t.Fatalf("AddMemory error: %v", err)
2049+ }
2050+ err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM, "assistant msg", "", "")
2051+ if err != nil {
2052+ t.Fatalf("AddMemory error: %v", err)
2053+ }
2054+
2055+ memories, err := mysis.getContextMemories()
2056+ if err != nil {
2057+ t.Fatalf("getContextMemories error: %v", err)
2058+ }
2059+
2060+ // Should generate synthetic nudge when no user prompt exists
2061+ hasNudge := false
2062+ for _, mem := range memories {
2063+ if mem.Role == store.MemoryRoleUser && mem.Source == store.MemorySourceSystem {
2064+ hasNudge = true
2065+ break
2066+ }
2067+ }
2068+
2069+ if !hasNudge {
2070+ t.Error("Expected synthetic nudge when no user prompt exists")
2071+ }
2072+}
2073+
2074+func TestGetContextMemories_OnlyHistoricalTurns(t *testing.T) {
2075+ s, bus, cleanup := setupMysisTest(t)
2076+ defer cleanup()
2077+
2078+ stored, _ := s.CreateMysis("historical-test", "mock", "test-model", 0.7)
2079+ mock := provider.NewMock("mock", "response")
2080+ mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus)
2081+
2082+ // Add system prompt
2083+ err := s.AddMemory(stored.ID, store.MemoryRoleSystem, store.MemorySourceSystem, "System prompt", "", "")
2084+ if err != nil {
2085+ t.Fatalf("AddMemory error: %v", err)
2086+ }
2087+
2088+ // Add complete historical turn
2089+ err = s.AddMemory(stored.ID, store.MemoryRoleUser, store.MemorySourceDirect, "old msg", "", "")
2090+ if err != nil {
2091+ t.Fatalf("AddMemory error: %v", err)
2092+ }
2093+ err = s.AddMemory(stored.ID, store.MemoryRoleAssistant, store.MemorySourceLLM, "old response", "", "")
2094+ if err != nil {
2095+ t.Fatalf("AddMemory error: %v", err)
2096+ }
2097+
2098+ memories, err := mysis.getContextMemories()
2099+ if err != nil {
2100+ t.Fatalf("getContextMemories error: %v", err)
2101+ }
2102+
2103+ // Should include system + historical context (user+assistant from last turn)
2104+ if len(memories) < 2 {
2105+ t.Fatalf("Expected at least 2 memories (system + historical), got %d", len(memories))
2106+ }
2107+
2108+ if memories[0].Role != store.MemoryRoleSystem {
2109+ t.Error("First memory should be system")
2110+ }
2111+
2112+ // Last user message should be included as current turn
2113+ hasOldUser := false
2114+ for _, mem := range memories {
2115+ if mem.Content == "old msg" {
2116+ hasOldUser = true
2117+ }
2118+ }
2119+
2120+ if !hasOldUser {
2121+ t.Error("Expected historical user message to be included as current turn")
2122+ }
2123+}