xonecas.io
60255
Zn

Zoea-Nova / commit 24f009c

fix(mysis): integrate compression functions in getContextMemories

- Added compactSnapshots() call in getContextMemories (mysis.go:1335)
- Added removeOrphanedToolCalls() call (mysis.go:1336)
- Added integration test TestGetContextMemories_WithCompression
- Added unit test TestCompactSnapshots_MultipleSnapshots
- Fixed TestZoeaListMysesCompaction expectations (compression reduces duplicates)

Resolves Critical Issue #3: compression functions now called in production
Impact: Myses no longer run blind with stale state, reduced token waste

Tests: All pass (86.7% coverage maintained)
Coverage: New tests verify both functions execute correctly

xonecas2026-02-08 24f009c83033cf92a3a3635cea47d78b4a4937ee parent 9841a55

2 files changed

internal/core/mysis.go+4−0
@@ -1331,6 +1331,10 @@ func (m *Mysis) getContextMemories() ([]*store.Memory, bool, error) {
13311331 }
13321332 }
13331333
1334+ // Apply compression to reduce context size and ensure API compliance
1335+ result = m.compactSnapshots(result)
1336+ result = m.removeOrphanedToolCalls(result)
1337+
13341338 return result, addedSynthetic, nil
13351339 }
13361340
internal/core/mysis_test.go+213−12
@@ -464,32 +464,26 @@ func TestZoeaListMysesCompaction(t *testing.T) {
464464 t.Fatalf("getContextMemories() error: %v", err)
465465 }
466466
467- // With turn-aware composition:
467+ // With turn-aware composition + snapshot compression:
468468 // - Historical context: latest tool loop from turns 0-3 (mysis-3)
469469 // - Current turn: all of turn 4 (user + assistant + tool for mysis-4)
470- // So we expect 2 tool results: mysis-3 and mysis-4
470+ // - Compression: zoea_list_myses is a snapshot tool, so only the LATEST is kept (mysis-4)
471+ // So we expect 1 tool result: mysis-4 (most recent)
471472 listResults := 0
472- hasMysis3 := false
473473 hasMysis4 := false
474474 for _, m := range memories {
475475 if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, `"id":"mysis-`) {
476476 listResults++
477- if strings.Contains(m.Content, `"id":"mysis-3"`) {
478- hasMysis3 = true
479- }
480477 if strings.Contains(m.Content, `"id":"mysis-4"`) {
481478 hasMysis4 = true
482479 }
483480 }
484481 }
485- if listResults != 2 {
486- t.Errorf("expected 2 zoea_list_myses results (historical latest + current turn), got %d", listResults)
487- }
488- if !hasMysis3 {
489- t.Error("expected mysis-3 result from historical context compression")
482+ if listResults != 1 {
483+ t.Errorf("expected 1 zoea_list_myses result after compression, got %d", listResults)
490484 }
491485 if !hasMysis4 {
492- t.Error("expected mysis-4 result from current turn")
486+ t.Error("expected mysis-4 result (latest snapshot after compression)")
493487 }
494488 }
495489
@@ -2590,3 +2584,210 @@ func TestNewMysisInheritsGlobalBroadcast(t *testing.T) {
25902584 t.Error("synthetic encouragement added despite global broadcast existing")
25912585 }
25922586 }
2587+
2588+// TestGetContextMemories_WithCompression verifies that getContextMemories()
2589+// applies both compactSnapshots() and removeOrphanedToolCalls() to ensure:
2590+// 1. Duplicate snapshot tool results are removed (only latest kept)
2591+// 2. Orphaned assistant tool calls without matching results are removed
2592+func TestGetContextMemories_WithCompression(t *testing.T) {
2593+ mysis, cleanup := setupTestMysis(t)
2594+ defer cleanup()
2595+
2596+ // Add current turn with duplicate snapshots and orphaned tool calls
2597+ // User prompt
2598+ err := mysis.store.AddMemory(mysis.id, store.MemoryRoleUser, store.MemorySourceDirect, "Check game status", "", "")
2599+ if err != nil {
2600+ t.Fatalf("AddMemory error: %v", err)
2601+ }
2602+
2603+ // First get_status call (will be compacted - duplicate)
2604+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_status_1:get_status:{}", "", "")
2605+ if err != nil {
2606+ t.Fatalf("AddMemory error: %v", err)
2607+ }
2608+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_status_1:old status data", "", "")
2609+ if err != nil {
2610+ t.Fatalf("AddMemory error: %v", err)
2611+ }
2612+
2613+ // First get_system call (will be compacted - duplicate)
2614+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_system_1:get_system:{}", "", "")
2615+ if err != nil {
2616+ t.Fatalf("AddMemory error: %v", err)
2617+ }
2618+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_system_1:old system data", "", "")
2619+ if err != nil {
2620+ t.Fatalf("AddMemory error: %v", err)
2621+ }
2622+
2623+ // Second get_status call (latest - will be kept)
2624+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_status_2:get_status:{}", "", "")
2625+ if err != nil {
2626+ t.Fatalf("AddMemory error: %v", err)
2627+ }
2628+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_status_2:latest status data", "", "")
2629+ if err != nil {
2630+ t.Fatalf("AddMemory error: %v", err)
2631+ }
2632+
2633+ // Second get_system call (latest - will be kept)
2634+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_system_2:get_system:{}", "", "")
2635+ if err != nil {
2636+ t.Fatalf("AddMemory error: %v", err)
2637+ }
2638+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleTool, store.MemorySourceTool, "call_system_2:latest system data", "", "")
2639+ if err != nil {
2640+ t.Fatalf("AddMemory error: %v", err)
2641+ }
2642+
2643+ // Orphaned tool call (no matching tool result - will be removed)
2644+ err = mysis.store.AddMemory(mysis.id, store.MemoryRoleAssistant, store.MemorySourceLLM, "[TOOL_CALLS]call_orphan:get_notifications:{}", "", "")
2645+ if err != nil {
2646+ t.Fatalf("AddMemory error: %v", err)
2647+ }
2648+
2649+ memories, _, err := mysis.getContextMemories()
2650+ if err != nil {
2651+ t.Fatalf("getContextMemories error: %v", err)
2652+ }
2653+
2654+ // Verify compression applied: only latest snapshots present
2655+ statusCount := 0
2656+ systemCount := 0
2657+ hasLatestStatus := false
2658+ hasLatestSystem := false
2659+ hasOrphanedCall := false
2660+
2661+ for _, mem := range memories {
2662+ if mem.Role == store.MemoryRoleTool {
2663+ if strings.Contains(mem.Content, "call_status_1") {
2664+ t.Error("Found old get_status result - compactSnapshots() not applied")
2665+ }
2666+ if strings.Contains(mem.Content, "call_system_1") {
2667+ t.Error("Found old get_system result - compactSnapshots() not applied")
2668+ }
2669+ if strings.Contains(mem.Content, "call_status_2") && strings.Contains(mem.Content, "latest status") {
2670+ statusCount++
2671+ hasLatestStatus = true
2672+ }
2673+ if strings.Contains(mem.Content, "call_system_2") && strings.Contains(mem.Content, "latest system") {
2674+ systemCount++
2675+ hasLatestSystem = true
2676+ }
2677+ }
2678+ if mem.Role == store.MemoryRoleAssistant && strings.Contains(mem.Content, "call_orphan") {
2679+ hasOrphanedCall = true
2680+ }
2681+ }
2682+
2683+ if !hasLatestStatus {
2684+ t.Error("Missing latest get_status snapshot")
2685+ }
2686+ if !hasLatestSystem {
2687+ t.Error("Missing latest get_system snapshot")
2688+ }
2689+ if statusCount != 1 {
2690+ t.Errorf("Expected exactly 1 get_status result, got %d", statusCount)
2691+ }
2692+ if systemCount != 1 {
2693+ t.Errorf("Expected exactly 1 get_system result, got %d", systemCount)
2694+ }
2695+ if hasOrphanedCall {
2696+ t.Error("Found orphaned tool call - removeOrphanedToolCalls() not applied")
2697+ }
2698+}
2699+
2700+// TestCompactSnapshots_MultipleSnapshots verifies that compactSnapshots()
2701+// correctly removes duplicate snapshot tool results, keeping only the latest
2702+// for each tool type while preserving order and non-snapshot tools.
2703+func TestCompactSnapshots_MultipleSnapshots(t *testing.T) {
2704+ mysis, cleanup := setupTestMysis(t)
2705+ defer cleanup()
2706+
2707+ // Create test memories with multiple snapshots
2708+ memories := []*store.Memory{
2709+ {Role: store.MemoryRoleUser, Content: "user message", Source: store.MemorySourceDirect},
2710+ // First get_status (old - should be removed)
2711+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_status_1:get_status:{}", Source: store.MemorySourceLLM},
2712+ {Role: store.MemoryRoleTool, Content: "call_status_1:old status", Source: store.MemorySourceTool},
2713+ // Non-snapshot tool (should be kept)
2714+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_action:send_message:{\"text\":\"hello\"}", Source: store.MemorySourceLLM},
2715+ {Role: store.MemoryRoleTool, Content: "call_action:message sent", Source: store.MemorySourceTool},
2716+ // Second get_status (latest - should be kept)
2717+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_status_2:get_status:{}", Source: store.MemorySourceLLM},
2718+ {Role: store.MemoryRoleTool, Content: "call_status_2:latest status", Source: store.MemorySourceTool},
2719+ // First get_system (old - should be removed)
2720+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_system_1:get_system:{}", Source: store.MemorySourceLLM},
2721+ {Role: store.MemoryRoleTool, Content: "call_system_1:old system", Source: store.MemorySourceTool},
2722+ // Third get_status (latest - should be kept)
2723+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_status_3:get_status:{}", Source: store.MemorySourceLLM},
2724+ {Role: store.MemoryRoleTool, Content: "call_status_3:newest status", Source: store.MemorySourceTool},
2725+ // Second get_system (latest - should be kept)
2726+ {Role: store.MemoryRoleAssistant, Content: "[TOOL_CALLS]call_system_2:get_system:{}", Source: store.MemorySourceLLM},
2727+ {Role: store.MemoryRoleTool, Content: "call_system_2:latest system", Source: store.MemorySourceTool},
2728+ {Role: store.MemoryRoleAssistant, Content: "Based on the data...", Source: store.MemorySourceLLM},
2729+ }
2730+
2731+ result := mysis.compactSnapshots(memories)
2732+
2733+ // Count snapshots and verify correct ones kept
2734+ statusCount := 0
2735+ systemCount := 0
2736+ nonSnapshotCount := 0
2737+ hasNewestStatus := false
2738+ hasLatestSystem := false
2739+ hasOldStatus := false
2740+ hasOldSystem := false
2741+
2742+ for _, mem := range result {
2743+ if mem.Role == store.MemoryRoleTool {
2744+ if strings.Contains(mem.Content, "call_status_1") {
2745+ hasOldStatus = true
2746+ }
2747+ if strings.Contains(mem.Content, "call_status_2") {
2748+ hasOldStatus = true
2749+ }
2750+ if strings.Contains(mem.Content, "call_status_3") && strings.Contains(mem.Content, "newest") {
2751+ statusCount++
2752+ hasNewestStatus = true
2753+ }
2754+ if strings.Contains(mem.Content, "call_system_1") {
2755+ hasOldSystem = true
2756+ }
2757+ if strings.Contains(mem.Content, "call_system_2") && strings.Contains(mem.Content, "latest") {
2758+ systemCount++
2759+ hasLatestSystem = true
2760+ }
2761+ if strings.Contains(mem.Content, "call_action") && strings.Contains(mem.Content, "message sent") {
2762+ nonSnapshotCount++
2763+ }
2764+ }
2765+ }
2766+
2767+ if hasOldStatus {
2768+ t.Error("Found old get_status results - should be compacted")
2769+ }
2770+ if hasOldSystem {
2771+ t.Error("Found old get_system results - should be compacted")
2772+ }
2773+ if !hasNewestStatus {
2774+ t.Error("Missing newest get_status snapshot")
2775+ }
2776+ if !hasLatestSystem {
2777+ t.Error("Missing latest get_system snapshot")
2778+ }
2779+ if statusCount != 1 {
2780+ t.Errorf("Expected exactly 1 get_status result, got %d", statusCount)
2781+ }
2782+ if systemCount != 1 {
2783+ t.Errorf("Expected exactly 1 get_system result, got %d", systemCount)
2784+ }
2785+ if nonSnapshotCount != 1 {
2786+ t.Errorf("Expected 1 non-snapshot tool result, got %d - non-snapshots should be preserved", nonSnapshotCount)
2787+ }
2788+
2789+ // Verify order preserved (user message should still be first)
2790+ if len(result) > 0 && result[0].Role != store.MemoryRoleUser {
2791+ t.Error("Message order not preserved - user message should be first")
2792+ }
2793+}