File size: 7,886 Bytes
24214fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
"""
Comprehensive test to verify AI agent prompt synchronization.
"""

import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'src'))

from config.prompt_management import PromptController

def test_agent_synchronization():
    """Test that all AI agents are properly synchronized with shared components."""
    print("Testing AI agent prompt synchronization...")
    
    controller = PromptController()
    
    # Test agents
    agents = ['spiritual_monitor', 'triage_question', 'triage_evaluator']
    
    print(f"\n1. Testing {len(agents)} agents for synchronization...")
    
    # Get configurations for all agents
    configs = {}
    for agent in agents:
        try:
            configs[agent] = controller.get_prompt(agent)
            print(f"   βœ“ {agent}: loaded successfully")
        except Exception as e:
            print(f"   βœ— {agent}: failed to load - {e}")
            return False
    
    # Test 2: Verify shared indicators consistency
    print("\n2. Testing shared indicators consistency...")
    
    indicator_sets = {}
    for agent, config in configs.items():
        indicator_sets[agent] = {ind.name: ind.to_dict() for ind in config.shared_indicators}
    
    # Compare all agents
    base_agent = agents[0]
    base_indicators = indicator_sets[base_agent]
    
    for other_agent in agents[1:]:
        other_indicators = indicator_sets[other_agent]
        
        # Check indicator names
        if set(base_indicators.keys()) != set(other_indicators.keys()):
            print(f"   βœ— Indicator names differ between {base_agent} and {other_agent}")
            return False
        
        # Check indicator definitions
        for ind_name in base_indicators:
            if base_indicators[ind_name] != other_indicators[ind_name]:
                print(f"   βœ— Indicator '{ind_name}' differs between {base_agent} and {other_agent}")
                return False
    
    print(f"   βœ“ All agents have identical {len(base_indicators)} indicators")
    
    # Test 3: Verify shared rules consistency
    print("\n3. Testing shared rules consistency...")
    
    rule_sets = {}
    for agent, config in configs.items():
        rule_sets[agent] = {rule.rule_id: rule.to_dict() for rule in config.shared_rules}
    
    base_rules = rule_sets[base_agent]
    
    for other_agent in agents[1:]:
        other_rules = rule_sets[other_agent]
        
        # Check rule IDs
        if set(base_rules.keys()) != set(other_rules.keys()):
            print(f"   βœ— Rule IDs differ between {base_agent} and {other_agent}")
            return False
        
        # Check rule definitions
        for rule_id in base_rules:
            if base_rules[rule_id] != other_rules[rule_id]:
                print(f"   βœ— Rule '{rule_id}' differs between {base_agent} and {other_agent}")
                return False
    
    print(f"   βœ“ All agents have identical {len(base_rules)} rules")
    
    # Test 4: Verify rule priority consistency
    print("\n4. Testing rule priority consistency...")
    
    for agent, config in configs.items():
        sorted_rules = sorted(config.shared_rules, key=lambda r: r.priority)
        rule_order = [rule.rule_id for rule in sorted_rules]
        
        if agent == base_agent:
            base_rule_order = rule_order
        else:
            if rule_order != base_rule_order:
                print(f"   βœ— Rule priority order differs for {agent}")
                return False
    
    print("   βœ“ All agents have identical rule priority order")
    
    # Test 5: Verify template consistency
    print("\n5. Testing template consistency...")
    
    template_sets = {}
    for agent, config in configs.items():
        template_sets[agent] = {tmpl.template_id: tmpl.to_dict() for tmpl in config.templates}
    
    base_templates = template_sets[base_agent]
    
    for other_agent in agents[1:]:
        other_templates = template_sets[other_agent]
        
        # Check template IDs
        if set(base_templates.keys()) != set(other_templates.keys()):
            print(f"   βœ— Template IDs differ between {base_agent} and {other_agent}")
            return False
        
        # Check template definitions
        for tmpl_id in base_templates:
            if base_templates[tmpl_id] != other_templates[tmpl_id]:
                print(f"   βœ— Template '{tmpl_id}' differs between {base_agent} and {other_agent}")
                return False
    
    print(f"   βœ“ All agents have identical {len(base_templates)} templates")
    
    # Test 6: Verify category definitions consistency
    print("\n6. Testing category definitions consistency...")
    
    categories = controller.category_definitions.get_all_categories()
    required_categories = ['GREEN', 'YELLOW', 'RED']
    
    for category in required_categories:
        if category not in categories:
            print(f"   βœ— Missing required category: {category}")
            return False
    
    print(f"   βœ“ All required categories present: {required_categories}")
    
    # Test 7: Verify validation consistency
    print("\n7. Testing validation consistency...")
    
    validation_result = controller.validate_consistency()
    
    if validation_result.errors:
        print("   ⚠ Validation errors found:")
        for error in validation_result.errors:
            print(f"     - {error}")
    else:
        print("   βœ“ No validation errors")
    
    if validation_result.warnings:
        print("   ⚠ Validation warnings:")
        for warning in validation_result.warnings:
            print(f"     - {warning}")
    else:
        print("   βœ“ No validation warnings")
    
    # Test 8: Test session isolation
    print("\n8. Testing session isolation...")
    
    session_id = "sync_test_session"
    test_override = "Test session override for synchronization testing"
    
    # Set override for one agent
    success = controller.set_session_override('spiritual_monitor', test_override, session_id)
    if not success:
        print("   βœ— Failed to set session override")
        return False
    
    # Verify other agents unaffected
    for agent in agents:
        config = controller.get_prompt(agent, session_id=session_id)
        
        if agent == 'spiritual_monitor':
            if config.session_override != test_override:
                print(f"   βœ— Session override not applied to {agent}")
                return False
        else:
            if config.session_override is not None:
                print(f"   βœ— Session override incorrectly applied to {agent}")
                return False
    
    # Clean up
    controller.clear_session_overrides(session_id)
    print("   βœ“ Session isolation working correctly")
    
    # Test 9: Performance metrics
    print("\n9. Testing performance metrics...")
    
    # Log some test metrics
    for agent in agents:
        controller.log_performance_metric(agent, 0.5, 0.85, False)
    
    # Verify metrics are recorded
    for agent in agents:
        metrics = controller.get_performance_metrics(agent)
        if metrics['total_executions'] == 0:
            print(f"   βœ— No metrics recorded for {agent}")
            return False
    
    print("   βœ“ Performance metrics working correctly")
    
    # Summary
    print("\n" + "="*60)
    print("SYNCHRONIZATION TEST SUMMARY")
    print("="*60)
    print(f"βœ“ Agents tested: {len(agents)}")
    print(f"βœ“ Shared indicators: {len(base_indicators)}")
    print(f"βœ“ Shared rules: {len(base_rules)}")
    print(f"βœ“ Shared templates: {len(base_templates)}")
    print(f"βœ“ Category definitions: {len(categories)}")
    print("βœ“ All agents are properly synchronized!")
    
    return True

if __name__ == "__main__":
    success = test_agent_synchronization()
    sys.exit(0 if success else 1)