-
Notifications
You must be signed in to change notification settings - Fork 0
Development
This guide covers development practices, contributing guidelines, and technical details for working on the Cognitive Engine codebase.
- Python 3.9 or higher
- Git
- pip
- Clone the repository
git clone <repository-url>
cd cognitive_engine- Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install development dependencies
pip install -r requirements.txt
pip install pytest pytest-asyncio black flake8 mypy- Configure environment
cp .env.example .env
# Edit .env with your development configuration- Run tests
python run.py testcognitive_engine/
├── core/ # Core engine orchestration
│ ├── engine.py # Main orchestration loop
│ ├── config.py # Configuration management
│ ├── memory.py # Three-layer memory system
│ ├── meta_cognition.py # Meta-cognitive oversight
│ ├── reasoning_trace.py # Reasoning process tracking
│ ├── inner_knowing.py # Self-awareness system
│ ├── self_doubt.py # Uncertainty handling
│ ├── ethical_alignment.py # Ethical reasoning
│ ├── emotional_simulation.py # Emotional modeling
│ ├── decision_control.py # Decision management
│ ├── temporal_identity.py # Identity continuity
│ ├── peaceful_resolution.py # Conflict resolution
│ └── obedience_understanding.py # Authority analysis
├── models/ # Data models
│ ├── thought.py # Thought object
│ └── state.py # Problem state
├── layers/ # Cognitive layers
│ ├── interpreter.py # Input interpretation
│ ├── generator.py # Thought generation
│ ├── deliberator.py # Thought deliberation
│ ├── committer.py # Output commitment
│ └── meta.py # Meta-cognition
├── utils/ # Utilities
│ ├── scoring.py # Thought scoring
│ ├── memory.py # Memory utilities
│ └── logger.py # Logging
├── llm/ # LLM integration
│ ├── client.py # LLM client
│ ├── prompts.py # Prompt templates
│ └── knowledge_base.py # Context management
├── agent/ # Autonomous agent
│ ├── agent.py # Main agent
│ ├── planner.py # Goal planning
│ ├── executor.py # Action execution
│ ├── observer.py # Result observation
│ └── goals.py # Goal definitions
├── tools/ # Tool system
│ ├── registry.py # Tool registry
│ ├── web_search.py # Web search tool
│ └── code_exec.py # Code execution tool
├── learning/ # Learning system
│ ├── extractor.py # Pattern extraction
│ ├── patterns.py # Pattern model
│ ├── synthesizer.py # Rule synthesis
│ └── updater.py # Knowledge update
├── prompt_evolution/ # Prompt evolution
│ ├── prompt_store.py # Prompt versioning
│ ├── proposer.py # Improvement suggestions
│ ├── tester.py # A/B testing
│ ├── evaluator.py # Performance evaluation
│ └── controller.py # Change control
├── dashboard/ # Cognitive telemetry
│ ├── server.py # WebSocket server
│ ├── stream.py # Event streaming
│ └── events.py # Event schemas
├── ui/ # Dashboard UI
│ ├── index.html # Main dashboard
│ ├── app.js # Frontend logic
│ └── styles.css # Styling
├── tests/ # Test suite
├── docs/ # Documentation
├── website/ # Website and wiki
│ └── wiki/ # Project wiki
├── run.py # Entry point
├── requirements.txt # Dependencies
└── .env.example # Environment template
- Use Black for code formatting (120 character line length)
- Use flake8 for linting
- Include type hints for all functions
- Write Google-style docstrings
- Follow PEP 8 naming conventions:
- PascalCase for classes
- snake_case for functions and variables
- UPPER_CASE for constants
from typing import Optional, List, Dict, Any
from datetime import datetime
class ExampleClass:
"""
A brief description of the class.
Longer description if needed.
Attributes:
attribute1: Description of attribute1
attribute2: Description of attribute2
"""
def __init__(self, attribute1: str, attribute2: Optional[int] = None):
"""
Initialize the ExampleClass.
Args:
attribute1: Description of attribute1
attribute2: Description of attribute2 (optional)
"""
self.attribute1 = attribute1
self.attribute2 = attribute2
def example_method(self, param: str) -> Dict[str, Any]:
"""
A brief description of the method.
Args:
param: Description of param
Returns:
A dictionary containing the result
Raises:
ValueError: If param is invalid
"""
if not param:
raise ValueError("param cannot be empty")
return {
"result": param,
"timestamp": datetime.now()
}# Format code with Black
black cognitive_engine/
# Check linting with flake8
flake8 cognitive_engine/
# Type checking with mypy
mypy cognitive_engine/# Run all tests
python run.py test
# Run specific test file
pytest tests/test_engine.py
# Run with coverage
pytest --cov=cognitive_engine tests/
# Run specific test
pytest tests/test_engine.py::test_processCreate test files in the tests/ directory following the naming convention test_<module>.py.
import pytest
from core.engine import CognitiveEngine
class TestCognitiveEngine:
"""Test suite for CognitiveEngine."""
def setup_method(self):
"""Set up test fixtures."""
self.engine = CognitiveEngine()
def test_initialization(self):
"""Test engine initialization."""
assert self.engine is not None
assert self.engine.thought_graph is not None
def test_process_basic_query(self):
"""Test processing a basic query."""
result = self.engine.process("What is AI?")
assert result is not None
assert result.final_output is not None
assert result.iteration_count > 0Maintain test coverage above 80%. Check coverage with:
pytest --cov=cognitive_engine --cov-report=html- All public classes and methods must have docstrings
- Use Google-style docstring format
- Include type hints in function signatures
- Document complex algorithms with inline comments
- Update the wiki in
website/wiki/when adding features - Keep API documentation in sync with code changes
- Update architecture documentation for structural changes
- Add examples for new features
- Update README.md for user-facing changes
- Update installation instructions for dependency changes
- Update configuration guide for new configuration options
git checkout -b feature/your-feature-name
# or
git checkout -b fix/your-bug-fix- Follow code style guidelines
- Write tests for new functionality
- Update documentation
- Ensure all tests pass
Follow conventional commits format:
<type>(<scope>): <subject>
<body>
<footer>
Types:
-
feat: New feature -
fix: Bug fix -
docs: Documentation changes -
style: Code style changes -
refactor: Code refactoring -
test: Test changes -
chore: Maintenance tasks
Example:
feat(generator): add thought refinement capability
Add ability to refine thoughts based on identified weaknesses.
This improves the quality of generated thoughts over iterations.
Closes #123
python run.py test
black cognitive_engine/
flake8 cognitive_engine/git push origin feature/your-feature-nameCreate a pull request with:
- Clear description of changes
- Link to related issues
- Test results
- Documentation updates
- Each module should have a single, well-defined responsibility
- Minimize dependencies between modules
- Use dependency injection where appropriate
- Define clear interfaces between components
- Write code that is easy to test
- Avoid global state
- Use dependency injection for external dependencies
- Mock external services (LLM APIs, databases)
- Profile before optimizing
- Consider caching for expensive operations
- Use async I/O for network operations
- Monitor memory usage
- Never commit API keys or secrets
- Validate all user inputs
- Use parameterized queries for database access
- Sanitize outputs from LLMs
- Write clear, self-documenting code
- Use descriptive names
- Keep functions focused and small
- Document complex logic
- Create new file in
layers/directory - Inherit from base layer pattern
- Implement required methods
- Add layer to engine initialization
- Write tests
- Update documentation
Example:
# layers/new_layer.py
from typing import List
from models.thought import Thought
class NewLayer:
"""Description of new layer."""
def process(self, thoughts: List[Thought]) -> List[Thought]:
"""Process thoughts."""
# Implementation
return thoughts- Create new file in
tools/directory - Implement tool interface
- Register tool in registry
- Write tests
- Update documentation
Example:
# tools/new_tool.py
from typing import Dict, Any
class NewTool:
"""Description of new tool."""
def execute(self, params: Dict[str, Any]) -> Dict[str, Any]:
"""Execute tool."""
# Implementation
return {"result": "..."}- Extend memory interface in
core/memory.py - Implement storage and retrieval methods
- Add to ThreeLayerMemory
- Write tests
- Update documentation
- Edit
llm/prompts.py - Test with various inputs
- Monitor performance impact
- Update prompt evolution if enabled
- Document changes
import logging
logging.basicConfig(level=logging.DEBUG)Or set environment variable:
LOG_LEVEL=DEBUGStart the dashboard to visualize cognitive processes:
python run.py dashboardAccess at http://localhost:8000
from core.engine import CognitiveEngine
engine = CognitiveEngine()
result = engine.process("Your query")
# Inspect thought graph
for thought_id, thought in engine.thought_graph.thoughts.items():
print(f"ID: {thought_id}")
print(f"Premise: {thought.premise}")
print(f"Confidence: {thought.confidence}")
print(f"Score: {thought.score}")
print(f"Weaknesses: {thought.weaknesses}")
print(f"History: {thought.history}")
print("---")from core.engine import CognitiveEngine
engine = CognitiveEngine()
result = engine.process("Your query")
# Inspect memory
memory = engine.get_memory()
episodic = memory.get_episodic_memory()
patterns = memory.get_pattern_memory()
rules = memory.get_rule_memory()
print(f"Episodic entries: {len(episodic)}")
print(f"Patterns: {len(patterns)}")
print(f"Rules: {len(rules)}")import cProfile
import pstats
from core.engine import CognitiveEngine
engine = CognitiveEngine()
profiler = cProfile.Profile()
profiler.enable()
result = engine.process("Your query")
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative')
stats.print_stats(20)- LLM API calls: Cache responses where appropriate
- Memory operations: Use indexing for faster queries
- Thought graph traversal: Implement memoization
- Dashboard streaming: Use batching for events
- Use async/await for I/O operations
- Implement caching for expensive computations
- Use connection pooling for database operations
- Batch operations where possible
- Consider using compiled extensions for performance-critical code
- Update configuration for production
- Set appropriate log levels
- Disable debug features
- Secure API keys
- Set up monitoring
- Configure backup for database
- Test in staging environment
- Document deployment process
Example Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV LOG_LEVEL=INFO
ENV ENABLE_DASHBOARD=false
CMD ["python", "run.py"]Use different .env files for different environments:
-
.env.development- Development settings -
.env.staging- Staging settings -
.env.production- Production settings
Load with:
export $(cat .env.production | xargs)Problem: ModuleNotFoundError
Solutions:
- Ensure virtual environment is activated
- Install dependencies:
pip install -r requirements.txt - Check PYTHONPATH includes project root
Problem: Tests failing intermittently
Solutions:
- Check for external dependencies (API keys, network)
- Use mocking for external services
- Add retry logic for flaky tests
- Check for race conditions in async code
Problem: Memory usage growing over time
Solutions:
- Profile memory usage
- Check for circular references
- Ensure proper cleanup in objects
- Limit memory size in configuration
Problem: Hitting API rate limits
Solutions:
- Implement exponential backoff
- Cache responses
- Use multiple API keys
- Reduce request frequency
- Python Documentation: https://docs.python.org/
- Pydantic Documentation: https://docs.pydantic.dev/
- FastAPI Documentation: https://fastapi.tiangolo.com/
- OpenAI API Documentation: https://platform.openai.com/docs
- Anthropic API Documentation: https://docs.anthropic.com/
For development questions:
- Email: autobotsolution@gmail.com
- Address: Flushing MI
- Check existing issues and documentation first