Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /CIRISAI_CIRISAgent /tests /test_action_sequence_conscience.py
| """ | |
| Unit tests for ActionSequenceConscience. | |
| Tests the heuristic conscience that prevents repeated SPEAK actions | |
| without intervening actions within the same task. | |
| """ | |
| import tempfile | |
| import uuid | |
| from datetime import datetime, timezone | |
| from unittest.mock import Mock | |
| import pytest | |
| from ciris_engine.logic.conscience.action_sequence_conscience import ActionSequenceConscience, _repeated_speak_guidance | |
| from ciris_engine.logic.persistence.db import initialize_database | |
| from ciris_engine.logic.persistence.models.tasks import add_task | |
| from ciris_engine.logic.persistence.models.thoughts import add_thought, update_thought_status | |
| from ciris_engine.schemas.actions.parameters import PonderParams, SpeakParams, ToolParams | |
| from ciris_engine.schemas.conscience.context import ConscienceCheckContext | |
| from ciris_engine.schemas.conscience.core import ConscienceStatus | |
| from ciris_engine.schemas.dma.results import ActionSelectionDMAResult | |
| from ciris_engine.schemas.runtime.enums import HandlerActionType, TaskStatus, ThoughtStatus | |
| from ciris_engine.schemas.runtime.models import FinalAction, Task, TaskContext, Thought, ThoughtContext | |
| class TestActionSequenceConscience: | |
| """Test ActionSequenceConscience check.""" | |
| def temp_db(self): | |
| """Create temporary database with schema.""" | |
| with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f: | |
| db_path = f.name | |
| # Initialize database schema with migrations | |
| initialize_database(db_path) | |
| yield db_path | |
| # Cleanup | |
| import os | |
| try: | |
| os.unlink(db_path) | |
| except OSError: | |
| pass | |
| def mock_time_service(self): | |
| """Create mock time service.""" | |
| mock_time = Mock() | |
| mock_time.now.return_value = datetime.now(timezone.utc) | |
| mock_time.now_iso.return_value = datetime.now(timezone.utc).isoformat() | |
| return mock_time | |
| def conscience(self, mock_time_service): | |
| """Create ActionSequenceConscience instance.""" | |
| return ActionSequenceConscience(time_service=mock_time_service) | |
| def patch_db_path(self, temp_db): | |
| """Return temp_db. Persist's engine is already wired to temp_db by | |
| the `temp_db` fixture's call to `initialize_database(temp_db)`, so | |
| all reads/writes through the migrated persistence layer flow through | |
| it without monkeypatching.""" | |
| return temp_db | |
| def sample_task(self, temp_db, mock_time_service): | |
| """Create and save a sample task.""" | |
| task = Task( | |
| task_id=str(uuid.uuid4()), | |
| channel_id="test-channel-123", | |
| description="Test task", | |
| status=TaskStatus.ACTIVE, | |
| priority=5, | |
| created_at=mock_time_service.now_iso(), | |
| updated_at=mock_time_service.now_iso(), | |
| context=TaskContext( | |
| channel_id="test-channel-123", | |
| user_id="user-123", | |
| correlation_id=str(uuid.uuid4()), | |
| parent_task_id=None, | |
| ), | |
| ) | |
| add_task(task) | |
| return task | |
| def sample_thought(self, sample_task, mock_time_service): | |
| """Create a sample thought (not saved to DB yet - represents current thought).""" | |
| return Thought( | |
| thought_id=str(uuid.uuid4()), | |
| source_task_id=sample_task.task_id, | |
| content="Processing task", | |
| status=ThoughtStatus.PENDING, | |
| created_at=mock_time_service.now_iso(), | |
| updated_at=mock_time_service.now_iso(), | |
| thought_depth=0, | |
| context=ThoughtContext( | |
| task_id=sample_task.task_id, | |
| correlation_id=str(uuid.uuid4()), | |
| round_number=0, | |
| depth=0, | |
| ), | |
| ) | |
| def speak_action(self): | |
| """Create a sample SPEAK action.""" | |
| return ActionSelectionDMAResult( | |
| selected_action=HandlerActionType.SPEAK, | |
| action_parameters=SpeakParams(content="Hello there"), | |
| rationale="Ready to respond", | |
| raw_llm_response=None, | |
| ) | |
| def tool_action(self): | |
| """Create a sample TOOL action.""" | |
| return ActionSelectionDMAResult( | |
| selected_action=HandlerActionType.TOOL, | |
| action_parameters=ToolParams(name="search", parameters={"query": "test"}), | |
| rationale="Using a tool", | |
| raw_llm_response=None, | |
| ) | |
| def ponder_action(self): | |
| """Create a sample PONDER action.""" | |
| return ActionSelectionDMAResult( | |
| selected_action=HandlerActionType.PONDER, | |
| action_parameters=PonderParams(questions=["What should I do?"]), | |
| rationale="Need to think", | |
| raw_llm_response=None, | |
| ) | |
| _thought_counter = 0 | |
| def _create_completed_thought( | |
| self, | |
| task_id: str, | |
| action_type: str, | |
| mock_time_service, | |
| temp_db: str, | |
| ) -> Thought: | |
| """Helper to create and save a completed thought with a final action. | |
| Each thought gets a unique advancing timestamp so persist's DESC sort | |
| is deterministic by insertion order. Real time would do this; the | |
| frozen mock clock would otherwise collide and order-by-timestamp | |
| falls back to thought_id (a random UUID). | |
| """ | |
| TestActionSequenceConscience._thought_counter += 1 | |
| from datetime import timedelta | |
| base = mock_time_service.now.return_value | |
| created_iso = (base + timedelta(microseconds=TestActionSequenceConscience._thought_counter)).isoformat() | |
| thought = Thought( | |
| thought_id=str(uuid.uuid4()), | |
| source_task_id=task_id, | |
| content=f"Completed thought with {action_type}", | |
| status=ThoughtStatus.COMPLETED, | |
| created_at=created_iso, | |
| updated_at=created_iso, | |
| thought_depth=0, | |
| final_action=FinalAction( | |
| action_type=action_type, | |
| action_params={"content": "test"} if action_type == "speak" else {"questions": ["test"]}, | |
| reasoning=f"Decided to {action_type}", | |
| ), | |
| context=ThoughtContext( | |
| task_id=task_id, | |
| correlation_id=str(uuid.uuid4()), | |
| round_number=0, | |
| depth=0, | |
| ), | |
| ) | |
| add_thought(thought) | |
| return thought | |
| # ==================== CORE LOGIC TESTS ==================== | |
| async def test_allows_first_speak_no_prior_actions(self, conscience, speak_action, sample_thought, patch_db_path): | |
| """Test that first SPEAK in a task is allowed (no prior actions).""" | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| assert "First SPEAK" in result.reason | |
| async def test_blocks_speak_after_speak_no_intervening( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that SPEAK after prior SPEAK with no intervening action is blocked.""" | |
| # Create a completed SPEAK thought | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is False | |
| assert result.status == ConscienceStatus.FAILED | |
| assert result.action_sequence_triggered is True | |
| # #974 step 5: the guidance is routed (conscience.repeated_speak_guidance) | |
| assert _repeated_speak_guidance() in result.reason | |
| # Should NOT have a replacement_action (soft bounce to recursive ASPDMA) | |
| assert result.replacement_action is None | |
| async def test_allows_speak_after_speak_with_ponder_intervening( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that SPEAK is allowed after SPEAK → PONDER (PONDER intervenes).""" | |
| # Create SPEAK then PONDER | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "ponder", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| assert "intervening" in result.reason | |
| async def test_allows_speak_after_speak_with_tool_intervening( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """SPEAK allowed after SPEAK → TOOL: any non-SPEAK action counts as intervening. | |
| Updated for the simplified rule (block only on immediate-prior SPEAK). | |
| Stuck TOOL→SPEAK→TOOL→SPEAK loops should be caught by content-level | |
| consciences (entropy, optimization_veto, coherence) rather than this | |
| sequence-shape check, which would otherwise prevent legitimate | |
| tool-then-speak chains. | |
| """ | |
| # Create SPEAK then TOOL | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "tool", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| # TOOL is intervening — SPEAK allowed | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| async def test_allows_speak_after_multiple_ponders( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that SPEAK is allowed after SPEAK → PONDER → PONDER → PONDER.""" | |
| # Create SPEAK then multiple PONDERs | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "ponder", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "ponder", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "ponder", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| async def test_blocks_speak_after_speak_tool_speak( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that SPEAK is blocked after SPEAK → TOOL → SPEAK (last was SPEAK).""" | |
| # Create SPEAK → TOOL → SPEAK | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "tool", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is False | |
| assert result.status == ConscienceStatus.FAILED | |
| assert result.action_sequence_triggered is True | |
| # ==================== NON-SPEAK ACTION TESTS ==================== | |
| async def test_allows_tool_action_always( | |
| self, conscience, tool_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that TOOL actions are always allowed (conscience only checks SPEAK).""" | |
| # Even with prior SPEAK, TOOL should pass | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(tool_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| assert "not SPEAK" in result.reason | |
| async def test_allows_ponder_action_always( | |
| self, conscience, ponder_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that PONDER actions are always allowed.""" | |
| # Even with prior SPEAK, PONDER should pass | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(ponder_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| # ==================== EDGE CASE TESTS ==================== | |
| async def test_allows_speak_when_no_thought_context(self, conscience, speak_action, patch_db_path): | |
| """Test that SPEAK is allowed when no thought context is provided.""" | |
| context = ConscienceCheckContext(thought=None) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| async def test_allows_speak_when_only_non_speak_prior_actions( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that SPEAK is allowed when prior actions are only non-SPEAK.""" | |
| # Create TOOL → PONDER (no prior SPEAK) | |
| self._create_completed_thought(sample_task.task_id, "tool", mock_time_service, patch_db_path) | |
| self._create_completed_thought(sample_task.task_id, "ponder", mock_time_service, patch_db_path) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| assert result.passed is True | |
| assert result.status == ConscienceStatus.PASSED | |
| assert result.action_sequence_triggered is False | |
| assert "First SPEAK" in result.reason | |
| async def test_ignores_pending_thoughts( | |
| self, conscience, speak_action, sample_task, sample_thought, mock_time_service, patch_db_path | |
| ): | |
| """Test that pending/processing thoughts are ignored (only completed count).""" | |
| # Create a completed SPEAK | |
| self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| # Create a pending PONDER (should be ignored) | |
| pending_thought = Thought( | |
| thought_id=str(uuid.uuid4()), | |
| source_task_id=sample_task.task_id, | |
| content="Pending ponder", | |
| status=ThoughtStatus.PENDING, # Not completed | |
| created_at=mock_time_service.now_iso(), | |
| updated_at=mock_time_service.now_iso(), | |
| thought_depth=0, | |
| final_action=FinalAction( | |
| action_type="ponder", | |
| action_params={"questions": ["test"]}, | |
| reasoning="Pending", | |
| ), | |
| context=ThoughtContext( | |
| task_id=sample_task.task_id, | |
| correlation_id=str(uuid.uuid4()), | |
| round_number=0, | |
| depth=0, | |
| ), | |
| ) | |
| add_thought(pending_thought) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| result = await conscience.check(speak_action, context) | |
| # Should still be blocked because pending PONDER doesn't count as intervening | |
| assert result.passed is False | |
| assert result.status == ConscienceStatus.FAILED | |
| assert result.action_sequence_triggered is True | |
| async def test_excludes_current_thought_from_history( | |
| self, conscience, speak_action, sample_task, mock_time_service, patch_db_path | |
| ): | |
| """Test that the current thought is excluded from action history check.""" | |
| # Create a completed SPEAK | |
| completed_speak = self._create_completed_thought(sample_task.task_id, "speak", mock_time_service, patch_db_path) | |
| # Create current thought with same ID as completed (edge case) | |
| # This should still be blocked because we exclude current thought | |
| current_thought = Thought( | |
| thought_id=str(uuid.uuid4()), # Different ID | |
| source_task_id=sample_task.task_id, | |
| content="Current thought", | |
| status=ThoughtStatus.PENDING, | |
| created_at=mock_time_service.now_iso(), | |
| updated_at=mock_time_service.now_iso(), | |
| thought_depth=0, | |
| context=ThoughtContext( | |
| task_id=sample_task.task_id, | |
| correlation_id=str(uuid.uuid4()), | |
| round_number=0, | |
| depth=0, | |
| ), | |
| ) | |
| context = ConscienceCheckContext(thought=current_thought) | |
| result = await conscience.check(speak_action, context) | |
| # Should be blocked - prior completed SPEAK exists | |
| assert result.passed is False | |
| assert result.action_sequence_triggered is True | |
| # ==================== TRACING/CORRELATION TESTS ==================== | |
| async def test_creates_trace_correlation( | |
| self, conscience, speak_action, sample_thought, patch_db_path, monkeypatch | |
| ): | |
| """Test that conscience creates trace correlation for auditing.""" | |
| from ciris_engine.logic import persistence | |
| correlations_added = [] | |
| original_add_correlation = persistence.add_correlation | |
| def mock_add_correlation(correlation, time_service=None): | |
| correlations_added.append(correlation) | |
| return original_add_correlation(correlation, time_service) | |
| monkeypatch.setattr(persistence, "add_correlation", mock_add_correlation) | |
| context = ConscienceCheckContext(thought=sample_thought) | |
| await conscience.check(speak_action, context) | |
| assert len(correlations_added) == 1 | |
| correlation = correlations_added[0] | |
| assert correlation.handler_name == "ActionSequenceConscience" | |
| assert correlation.service_type == "conscience" | |
| assert "action_sequence" in correlation.tags.get("conscience_type", "") | |
Xet Storage Details
- Size:
- 19.2 kB
- Xet hash:
- 69c86332ec90cc62be6fd35920dd9850818504b89f6d3d86a73abae37b97327d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.