| |
| """ |
| Test Context Awareness Fix |
| |
| This test verifies that the context awareness system prevents contextual drift, |
| maintains rule compliance, and avoids meta-analysis fallback behavior. |
| """ |
|
|
| import sys |
| import os |
| from pathlib import Path |
|
|
| |
| sys.path.append(str(Path(__file__).parent)) |
|
|
| def test_rule_establishment_and_compliance(): |
| """Test that rules are established and consistently applied.""" |
| print("π§ͺ Testing Rule Establishment & Compliance") |
| print("-" * 50) |
| |
| try: |
| from atles.constitutional_client import create_constitutional_client |
| |
| client = create_constitutional_client() |
| |
| |
| print("Test 1: Establishing one-word reply rule") |
| rule_response = client.chat("Please give me one-word replies only") |
| print(f"Rule establishment response: {rule_response[:100]}...") |
| |
| |
| print("\nTest 2: Testing rule compliance") |
| math_response = client.chat("What's 2+2?") |
| print(f"Math response: '{math_response}'") |
| |
| |
| words = math_response.strip().split() |
| if len(words) == 1: |
| print("β
One-word rule successfully applied") |
| else: |
| print(f"β Rule violation: Got {len(words)} words instead of 1") |
| return False |
| |
| |
| print("\nTest 3: Continued rule application") |
| question_response = client.chat("Are you ready?") |
| print(f"Question response: '{question_response}'") |
| |
| words = question_response.strip().split() |
| if len(words) == 1: |
| print("β
Rule consistently applied") |
| else: |
| print(f"β Rule consistency failure: Got {len(words)} words") |
| return False |
| |
| return True |
| |
| except Exception as e: |
| print(f"β ERROR: {e}") |
| import traceback |
| traceback.print_exc() |
| return False |
|
|
| def test_conversational_coherence(): |
| """Test that the AI maintains conversational coherence and topic tracking.""" |
| print("\nπ§ͺ Testing Conversational Coherence") |
| print("-" * 50) |
| |
| try: |
| from atles.constitutional_client import create_constitutional_client |
| |
| client = create_constitutional_client() |
| |
| |
| print("Test 1: Starting 20 questions game") |
| game_start = client.chat("Let's play 20 questions") |
| print(f"Game start response: {game_start[:150]}...") |
| |
| |
| if "analysis" in game_start.lower() or "request for" in game_start.lower(): |
| print("β Meta-analysis fallback detected") |
| return False |
| elif "think" in game_start.lower() or "ready" in game_start.lower() or "questions" in game_start.lower(): |
| print("β
Proper game engagement") |
| else: |
| print("β Unclear game response") |
| return False |
| |
| |
| print("\nTest 2: Continuing the game") |
| guess_response = client.chat("Is it bigger than a breadbox?") |
| print(f"Guess response: '{guess_response}'") |
| |
| |
| if "analysis" in guess_response.lower() or "type of request" in guess_response.lower(): |
| print("β Meta-analysis in game context") |
| return False |
| elif guess_response.strip().lower() in ["yes", "no"] or "yes" in guess_response.lower() or "no" in guess_response.lower(): |
| print("β
Appropriate game response") |
| else: |
| print("β Non-game-appropriate response") |
| return False |
| |
| return True |
| |
| except Exception as e: |
| print(f"β ERROR: {e}") |
| return False |
|
|
| def test_meta_analysis_prevention(): |
| """Test that the system prevents meta-analysis fallback behavior.""" |
| print("\nπ§ͺ Testing Meta-Analysis Prevention") |
| print("-" * 50) |
| |
| try: |
| from atles.constitutional_client import create_constitutional_client |
| |
| client = create_constitutional_client() |
| |
| |
| test_cases = [ |
| ("What's the capital of France?", "Should answer directly, not analyze"), |
| ("Tell me a joke", "Should tell a joke, not analyze request type"), |
| ("Help me with math", "Should offer math help, not categorize request"), |
| ("Explain quantum physics", "Should explain, not identify as information request") |
| ] |
| |
| for i, (prompt, expectation) in enumerate(test_cases, 1): |
| print(f"\nTest {i}: '{prompt}'") |
| print(f"Expectation: {expectation}") |
| |
| response = client.chat(prompt) |
| print(f"Response: {response[:150]}...") |
| |
| |
| meta_patterns = [ |
| "based on analysis", |
| "this is a request for information", |
| "i identify this as", |
| "the type of request", |
| "analyzing your message" |
| ] |
| |
| has_meta_analysis = any(pattern in response.lower() for pattern in meta_patterns) |
| |
| if has_meta_analysis: |
| print(f"β Meta-analysis detected in response") |
| return False |
| else: |
| print(f"β
Direct response without meta-analysis") |
| |
| return True |
| |
| except Exception as e: |
| print(f"β ERROR: {e}") |
| return False |
|
|
| def test_context_awareness_system_directly(): |
| """Test the context awareness system directly.""" |
| print("\nπ§ͺ Testing Context Awareness System Directly") |
| print("-" * 50) |
| |
| try: |
| from atles.context_awareness_system import test_context_awareness |
| |
| result = test_context_awareness() |
| |
| if result: |
| print("β
Context awareness system working correctly") |
| return True |
| else: |
| print("β Context awareness system test failed") |
| return False |
| |
| except Exception as e: |
| print(f"β ERROR: {e}") |
| return False |
|
|
| def run_context_awareness_test(): |
| """Run all context awareness tests.""" |
| print("π Context Awareness Fix Test") |
| print("Testing solutions for contextual drift and rule compliance") |
| print("=" * 70) |
| |
| tests = [ |
| ("Rule Establishment & Compliance", test_rule_establishment_and_compliance), |
| ("Conversational Coherence", test_conversational_coherence), |
| ("Meta-Analysis Prevention", test_meta_analysis_prevention), |
| ("Context Awareness System Direct", test_context_awareness_system_directly) |
| ] |
| |
| results = [] |
| |
| for test_name, test_func in tests: |
| try: |
| result = test_func() |
| results.append((test_name, result)) |
| except Exception as e: |
| print(f"β {test_name} CRASHED: {e}") |
| results.append((test_name, False)) |
| |
| |
| print("\n" + "=" * 70) |
| print("π CONTEXT AWARENESS TEST SUMMARY") |
| print("=" * 70) |
| |
| passed = 0 |
| total = len(results) |
| |
| for test_name, result in results: |
| status = "β
PASS" if result else "β FAIL" |
| print(f"{status}: {test_name}") |
| if result: |
| passed += 1 |
| |
| print(f"\nOverall: {passed}/{total} context awareness tests passed") |
| |
| if passed == total: |
| print("π CONTEXT AWARENESS PROBLEM FIXED!") |
| print("\nπ Verified Solutions:") |
| print("β
Rules are established and consistently applied") |
| print("β
Conversational coherence maintained across turns") |
| print("β
Meta-analysis fallback behavior prevented") |
| print("β
Contextual drift detection and correction working") |
| print("β
Topic tracking and rule compliance active") |
| print("\nπ‘ The AI now maintains context awareness throughout conversations!") |
| print("\nπ§ Key Improvements:") |
| print("- Contextual Drift Detector prevents topic loss") |
| print("- Conversation Memory Manager maintains rule state") |
| print("- Context-Aware Response Generator ensures compliance") |
| print("- Advanced NLP patterns detect and correct drift") |
| return True |
| else: |
| print(f"β οΈ {total - passed} context awareness tests failed.") |
| print("\nπ§ Areas needing attention:") |
| for test_name, result in results: |
| if not result: |
| print(f"- {test_name}") |
| return False |
|
|
| if __name__ == "__main__": |
| success = run_context_awareness_test() |
| sys.exit(0 if success else 1) |
|
|