|
Download docs/DEPLOYMENT_FAQ_EXAMPLES_GLOSSARY.md from Snapkitty/sovereign-engine-v2: direct link, hf CLI and curl.
- Browser
- Download file 32.3 kB
-
https://huggingface.co/Snapkitty/sovereign-engine-v2/resolve/main/docs/DEPLOYMENT_FAQ_EXAMPLES_GLOSSARY.md
- Command line
-
hf download hf://Snapkitty/sovereign-engine-v2/docs/DEPLOYMENT_FAQ_EXAMPLES_GLOSSARY.md
-
curl -L -o DEPLOYMENT_FAQ_EXAMPLES_GLOSSARY.md https://huggingface.co/Snapkitty/sovereign-engine-v2/resolve/main/docs/DEPLOYMENT_FAQ_EXAMPLES_GLOSSARY.md
32.3 kB
| # Deployment, FAQ, Examples, Glossary & Resources | |
| **Sovereign Engine v2 β Operational Reference** | |
| **Version:** 2.0.0 | **Status:** Production Beta | |
| **Last Updated:** 2026-09-19 | |
| --- | |
| ## 1. Deployment & Scaling | |
| ### 1.1 Container Deployment | |
| **Docker Setup** | |
| ```dockerfile | |
| FROM python:3.11-slim | |
| WORKDIR /app | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| libopenblas-dev \ | |
| liblapack-dev \ | |
| libgomp1 \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy source | |
| COPY src/ /app/src/ | |
| COPY pyproject.toml /app/ | |
| # Install Python packages | |
| RUN pip install --no-cache-dir -e . | |
| # Environment configuration | |
| ENV PYTHONUNBUFFERED=1 | |
| ENV BEDROCK_REGION=us-east-1 | |
| ENV BEDROCK_MODEL=us.anthropic.claude-haiku-4-5-20251001-v1:0 | |
| ENV SOVEREIGN_LOG_LEVEL=INFO | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ | |
| CMD python -c "import requests; requests.get('http://localhost:8000/health', timeout=5)" | |
| # Expose port | |
| EXPOSE 8000 | |
| # Run HTTP bridge | |
| ENTRYPOINT ["python", "-m", "uvicorn", "src.bridge.http_server:app", \ | |
| "--host", "0.0.0.0", "--port", "8000"] | |
| ``` | |
| **Build and run:** | |
| ```bash | |
| docker build -t sovereign-engine:2.0.0 . | |
| docker run -d \ | |
| --name sovereign \ | |
| -p 8000:8000 \ | |
| -e AWS_ACCESS_KEY_ID=<key> \ | |
| -e AWS_SECRET_ACCESS_KEY=<secret> \ | |
| -v ~/.sovereign:/root/.sovereign \ | |
| sovereign-engine:2.0.0 | |
| ``` | |
| ### 1.2 Kubernetes Orchestration | |
| **StatefulSet configuration** (k8s-deployment.yaml) | |
| ```yaml | |
| apiVersion: apps/v1 | |
| kind: StatefulSet | |
| metadata: | |
| name: sovereign-engine | |
| spec: | |
| serviceName: sovereign | |
| replicas: 3 | |
| selector: | |
| matchLabels: | |
| app: sovereign-engine | |
| template: | |
| metadata: | |
| labels: | |
| app: sovereign-engine | |
| spec: | |
| containers: | |
| - name: sovereign | |
| image: sovereign-engine:2.0.0 | |
| ports: | |
| - containerPort: 8000 | |
| name: http | |
| env: | |
| - name: BEDROCK_REGION | |
| valueFrom: | |
| configMapKeyRef: | |
| name: sovereign-config | |
| key: region | |
| - name: SOVEREIGN_LOG_LEVEL | |
| value: "INFO" | |
| - name: SOVEREIGN_ENABLE_CONTINUITY | |
| value: "true" | |
| resources: | |
| requests: | |
| memory: "2Gi" | |
| cpu: "1" | |
| limits: | |
| memory: "4Gi" | |
| cpu: "2" | |
| livenessProbe: | |
| httpGet: | |
| path: /health | |
| port: 8000 | |
| initialDelaySeconds: 30 | |
| periodSeconds: 10 | |
| readinessProbe: | |
| httpGet: | |
| path: /health | |
| port: 8000 | |
| initialDelaySeconds: 5 | |
| periodSeconds: 5 | |
| volumeMounts: | |
| - name: sovereign-data | |
| mountPath: /root/.sovereign | |
| volumeClaimTemplates: | |
| - metadata: | |
| name: sovereign-data | |
| spec: | |
| accessModes: [ "ReadWriteOnce" ] | |
| resources: | |
| requests: | |
| storage: 10Gi | |
| --- | |
| apiVersion: v1 | |
| kind: Service | |
| metadata: | |
| name: sovereign | |
| spec: | |
| clusterIP: None | |
| selector: | |
| app: sovereign-engine | |
| ports: | |
| - port: 8000 | |
| name: http | |
| --- | |
| apiVersion: v1 | |
| kind: Service | |
| metadata: | |
| name: sovereign-lb | |
| spec: | |
| type: LoadBalancer | |
| selector: | |
| app: sovereign-engine | |
| ports: | |
| - port: 80 | |
| targetPort: 8000 | |
| protocol: TCP | |
| ``` | |
| **Deploy:** | |
| ```bash | |
| kubectl apply -f k8s-deployment.yaml | |
| kubectl rollout status statefulset/sovereign-engine | |
| ``` | |
| ### 1.3 Environment Configuration | |
| **Configuration hierarchy** (highest to lowest precedence): | |
| 1. Environment variables | |
| 2. `~/.sovereign/config.yaml` | |
| 3. Compiled defaults | |
| **Essential environment variables:** | |
| ```bash | |
| # Model selection | |
| export BEDROCK_MODEL=us.anthropic.claude-haiku-4-5-20251001-v1:0 | |
| export BEDROCK_REGION=us-east-1 | |
| # AWS credentials (credential chain) | |
| export AWS_ACCESS_KEY_ID=your_key | |
| export AWS_SECRET_ACCESS_KEY=your_secret | |
| export AWS_PROFILE=default | |
| # Engine tuning | |
| export SOVEREIGN_MAX_STEPS=10 | |
| export SOVEREIGN_TOOL_TIMEOUT_MS=30000 | |
| export SOVEREIGN_LOG_LEVEL=INFO | |
| # Features | |
| export SOVEREIGN_ENABLE_CONTINUITY=true | |
| export SOVEREIGN_ENABLE_WORM=true | |
| ``` | |
| **Configuration file** (`~/.sovereign/config.yaml`): | |
| ```yaml | |
| model: | |
| provider: bedrock | |
| model_id: us.anthropic.claude-haiku-4-5-20251001-v1:0 | |
| max_tokens: 500 | |
| temperature: 0.7 | |
| routing: | |
| top_k: 2 | |
| expert_timeout_ms: 5000 | |
| merge_strategy: weighted_concat | |
| tools: | |
| timeout_ms: 30000 | |
| require_approval_for_risky: true | |
| approved_paths: | |
| - /home/user/projects | |
| - /tmp | |
| logging: | |
| level: INFO | |
| format: verbose | |
| file: ~/.sovereign/logs/engine.log | |
| max_file_size_mb: 100 | |
| backup_count: 5 | |
| continuity: | |
| enabled: true | |
| base_dir: ~/.sovereign/continuity | |
| checkpoint_interval_steps: 10 | |
| worm: | |
| base_dir: ~/.sovereign/worm | |
| rotate_on_size_mb: 500 | |
| persistence: | |
| backend: file # or: s3 | |
| s3_bucket: my-sovereign-backups | |
| s3_prefix: continuity/ | |
| ``` | |
| ### 1.4 Secrets Management | |
| **Use AWS Secrets Manager (recommended):** | |
| ```python | |
| import boto3 | |
| import json | |
| def load_secrets(): | |
| client = boto3.client('secretsmanager', region_name='us-east-1') | |
| secret = client.get_secret_value(SecretId='sovereign-engine/config') | |
| return json.loads(secret['SecretString']) | |
| # In code: | |
| secrets = load_secrets() | |
| bedrock_key = secrets['bedrock_access_key'] | |
| ``` | |
| **Or HashiCorp Vault:** | |
| ```bash | |
| # Login | |
| vault login -method=aws | |
| # Set secret | |
| vault kv put secret/sovereign \ | |
| bedrock_key=xxx \ | |
| bedrock_secret=yyy | |
| # Read in code | |
| hvac.Client().secrets.kv.read_secret_version(path='sovereign') | |
| ``` | |
| ### 1.5 Monitoring & Observability (7 Key Metrics) | |
| **Prometheus metrics exposed at `/metrics`:** | |
| 1. **Request latency (p95):** `sovereign_request_duration_ms{endpoint="/chat"}` β Target: <1000ms | |
| 2. **Routing latency:** `sovereign_routing_latency_ms` β Target: <50ms | |
| 3. **Tool timeout rate:** `sovereign_tool_timeout_ratio` β Target: <1% | |
| 4. **Model availability:** `sovereign_model_availability` β Target: >99% | |
| 5. **WORM ledger growth:** `sovereign_worm_entries_total` β Target: <100/sec | |
| 6. **Memory usage:** `process_resident_memory_bytes` β Target: <2GB | |
| 7. **CPU utilization:** `process_cpu_seconds_total` β Target: <50% | |
| **Grafana dashboard (JSON):** | |
| ```json | |
| { | |
| "dashboard": { | |
| "title": "Sovereign Engine Metrics", | |
| "panels": [ | |
| { | |
| "title": "Request Latency (p95)", | |
| "targets": [{"expr": "histogram_quantile(0.95, rate(sovereign_request_duration_ms[5m]))"}] | |
| }, | |
| { | |
| "title": "Active Experts", | |
| "targets": [{"expr": "sovereign_active_experts_count"}] | |
| }, | |
| { | |
| "title": "WORM Ledger Size", | |
| "targets": [{"expr": "increase(sovereign_worm_entries_total[1h])"}] | |
| } | |
| ] | |
| } | |
| } | |
| ``` | |
| **Alerting rules (Prometheus):** | |
| ```yaml | |
| groups: | |
| - name: sovereign_alerts | |
| rules: | |
| - alert: HighRequestLatency | |
| expr: histogram_quantile(0.95, rate(sovereign_request_duration_ms[5m])) > 2000 | |
| for: 5m | |
| annotations: | |
| summary: "Request latency exceeds 2s" | |
| - alert: ToolTimeoutRate | |
| expr: sovereign_tool_timeout_ratio > 0.05 | |
| for: 2m | |
| annotations: | |
| summary: "Tool timeout rate >5%" | |
| - alert: WORMLedgerGrowth | |
| expr: rate(sovereign_worm_entries_total[1m]) > 200 | |
| for: 5m | |
| annotations: | |
| summary: "WORM growth abnormally high" | |
| ``` | |
| ### 1.6 Multi-Region Fallback | |
| **Load balancer with health checks:** | |
| ```yaml | |
| # Nginx configuration | |
| upstream sovereign_us_east { | |
| server sovereign-us-east-1.compute.amazonaws.com:8000; | |
| server sovereign-us-east-2.compute.amazonaws.com:8000; | |
| } | |
| upstream sovereign_eu { | |
| server sovereign-eu-west-1.compute.amazonaws.com:8000; | |
| server sovereign-eu-central-1.compute.amazonaws.com:8000; | |
| } | |
| server { | |
| listen 80; | |
| location / { | |
| # Primary: US, fallback: EU | |
| proxy_pass http://sovereign_us_east; | |
| proxy_connect_timeout 5s; | |
| proxy_read_timeout 30s; | |
| # Retry on failure | |
| proxy_next_upstream error timeout http_503 http_504; | |
| proxy_next_upstream_tries 2; | |
| # Fallback region | |
| proxy_next_upstream_http_403 http_500; | |
| } | |
| location @fallback_eu { | |
| proxy_pass http://sovereign_eu; | |
| } | |
| } | |
| ``` | |
| --- | |
| ## 2. FAQ & Troubleshooting | |
| ### Q1: How do I add a custom tool? | |
| **A:** Register it in `src/tools/` subdirectory with a `ToolDefinition`: | |
| ```python | |
| # src/tools/custom/my_tool.py | |
| from src.tools.registry import Tool, ToolDefinition, RiskClass, ApprovalPolicy | |
| async def my_custom_handler(params): | |
| """Execute custom logic""" | |
| result = do_something(params['input']) | |
| return {"output": result} | |
| definition = ToolDefinition( | |
| tool_id="custom.my_tool", | |
| version="1.0.0", | |
| title="My Custom Tool", | |
| description="Does custom thing", | |
| input_schema={ | |
| "type": "object", | |
| "properties": {"input": {"type": "string"}}, | |
| "required": ["input"] | |
| }, | |
| output_schema={ | |
| "type": "object", | |
| "properties": {"output": {"type": "string"}}, | |
| "required": ["output"] | |
| }, | |
| risk_class=RiskClass.PURE_COMPUTATION, | |
| approval_policy=ApprovalPolicy.AUTOMATIC, | |
| timeout_ms=5000, | |
| handler=my_custom_handler | |
| ) | |
| ``` | |
| The loader (`src/tools/loader.py`) auto-discovers subdirectories. Call `registry.list_all()` to verify registration. | |
| ### Q2: How do I use local models instead of Bedrock? | |
| **A:** Configure `LocalModel` in `src/inference/`: | |
| ```python | |
| # Option 1: Ollama (via HTTP) | |
| from src.inference.ollama_backend import OllamaBackend | |
| backend = OllamaBackend( | |
| base_url="http://localhost:11434", | |
| model_name="llama2:13b" | |
| ) | |
| # Option 2: sentence-transformers (embeddings only) | |
| from sentence_transformers import SentenceTransformer | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| embeddings = model.encode(["query1", "query2"]) | |
| # Option 3: Multi-provider fallback | |
| from src.runtime.providers.multi import MultiProvider | |
| provider = MultiProvider( | |
| primary=OllamaBackend(...), | |
| fallback=BedrockBackend(...) | |
| ) | |
| ``` | |
| Then update `ReActAgent` to use it: | |
| ```python | |
| agent = ReActAgent( | |
| model_interface=provider, | |
| tool_registry=registry | |
| ) | |
| ``` | |
| ### Q3: How do I view routing traces? | |
| **A:** Use the HTTP API or CLI: | |
| ```bash | |
| # Via curl | |
| curl -X POST http://localhost:8000/routing/trace \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"prompt": "explain fibonacci", "context": {}}' | |
| # Via Python | |
| import requests | |
| trace = requests.post("http://localhost:8000/routing/trace", | |
| json={"prompt": "write a function"}) | |
| print(trace.json()) | |
| ``` | |
| **Trace structure:** | |
| - `parse` β Intent detection, confidence | |
| - `graph` β Symbolic graph nodes/edges | |
| - `jordan` β Eigenvalues, stability score | |
| - `jacobian` β Condition number, rank | |
| - `constraints` β Passed/blocked rules | |
| - `routing` β Active experts, weights | |
| - `dispatch` β Success count, failures | |
| ### Q4: How do routing conflicts occur and how are they resolved? | |
| **A:** Conflicts arise from NAND constraints. Register them: | |
| ```python | |
| pipeline.add_nand_conflict("expert_a", "expert_b") | |
| ``` | |
| **Resolution:** | |
| 1. **Constraint evaluation** filters conflicting pairs | |
| 2. **NAND filtering** (opcode_registry.py) enforces precedence | |
| 3. **Merge strategy** combines non-conflicting results: | |
| - `weighted_concat` β Concatenate with weights | |
| - `weighted_avg` β Average scores | |
| - `highest_weight` β Select highest-scoring expert | |
| - `ensemble_text` β Ensemble text outputs | |
| Example: | |
| ```python | |
| # Both "coder" and "reasoner" score 0.8, but conflict registered | |
| # NAND filter suppresses one; dispatch uses `merge_strategy` to pick result | |
| ``` | |
| ### Q5: How do I verify evidence ledger integrity? | |
| **A:** Use `WORMLedger.validate_chain()`: | |
| ```python | |
| from src.core.evidence import WORMLedger | |
| worm = WORMLedger() | |
| is_valid, message = worm.validate_chain() | |
| if is_valid: | |
| print(f"Chain valid. Total entries: {worm.count()}") | |
| else: | |
| print(f"Chain broken: {message}") | |
| # Check specific entry | |
| entry = worm.get(seq=42) | |
| print(entry) # {seq, timestamp, hash_prev, signature, event} | |
| ``` | |
| **Validation checks:** | |
| 1. Each signature is valid (Ed25519) | |
| 2. Each Blake3 hash chain link is correct | |
| 3. No gaps or reordering | |
| 4. Total order preserved | |
| ### Q6: How do I extend the framework? | |
| **A:** Key extension points: | |
| 1. **New agent type** β Inherit from `Agent` base class: | |
| ```python | |
| from src.agents.base import Agent | |
| class MyAgent(Agent): | |
| async def execute(self, task): | |
| # Custom loop logic | |
| pass | |
| ``` | |
| 2. **New routing stage** β Add to pipeline: | |
| ```python | |
| from src.routing.pipeline import RoutingPipeline | |
| pipeline.add_stage( | |
| name="custom_stage", | |
| fn=my_analysis_function | |
| ) | |
| ``` | |
| 3. **New model backend** β Implement interface: | |
| ```python | |
| from src.inference.base import ModelInterface | |
| class MyBackend(ModelInterface): | |
| async def generate(self, prompt, context, max_tokens, temperature): | |
| # Inference logic | |
| pass | |
| ``` | |
| 4. **Custom constraint** β Register with pipeline: | |
| ```python | |
| def my_constraint(dispatch_result): | |
| return len(dispatch_result.active_experts) <= 3 | |
| pipeline.add_constraint(my_constraint) | |
| ``` | |
| --- | |
| ## 3. Examples | |
| ### Example 1: Task Routing with Sparse Expert Selection | |
| **File:** `routing_example.py` | |
| ```python | |
| import asyncio | |
| from src.routing.pipeline import RoutingPipeline | |
| from src.tools.registry import ToolRegistry | |
| # Define async expert callbacks | |
| async def coder_expert(text, context): | |
| """Handles code generation tasks""" | |
| return { | |
| "expert": "coder", | |
| "suggestion": f"Use Python for: {text}", | |
| "confidence": 0.95 | |
| } | |
| async def math_expert(text, context): | |
| """Handles mathematical analysis""" | |
| return { | |
| "expert": "math", | |
| "suggestion": f"Apply algorithm: {text}", | |
| "confidence": 0.87 | |
| } | |
| async def general_expert(text, context): | |
| """Fallback for general tasks""" | |
| return { | |
| "expert": "general", | |
| "suggestion": f"General approach: {text}", | |
| "confidence": 0.65 | |
| } | |
| async def main(): | |
| # Create pipeline with experts | |
| pipeline = RoutingPipeline( | |
| experts={ | |
| "coder": coder_expert, | |
| "math": math_expert, | |
| "general": general_expert | |
| }, | |
| top_k=2, # Select top 2 experts | |
| expert_timeout_ms=2000, | |
| merge_strategy="weighted_concat" | |
| ) | |
| # Register a NAND conflict (coder and math cannot both fire for this domain) | |
| pipeline.add_nand_conflict("coder", "math") | |
| # Route a task | |
| task_text = "Write a recursive Fibonacci function and analyze its complexity" | |
| context = {"domain": "algorithms", "level": "intermediate"} | |
| # Get trace with all diagnostics | |
| trace = await pipeline.route_with_trace(task_text, context) | |
| # Inspect results | |
| print("=== Routing Trace ===") | |
| print(f"Intent: {trace.parse.intent} (confidence: {trace.parse.confidence})") | |
| print(f"Active experts: {trace.dispatch.active_experts}") | |
| print(f"Active count: {trace.dispatch.active_count}") | |
| print(f"Success count: {trace.dispatch.success_count}") | |
| print(f"Result: {trace.dispatch.result}") | |
| # Just the dispatch without trace overhead | |
| result = await pipeline.route(task_text, context) | |
| print(f"\nDispatch result: {result.active_experts}") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
| ``` | |
| **Run:** | |
| ```bash | |
| python routing_example.py | |
| ``` | |
| ### Example 2: ReAct Agent with Tool Use | |
| **File:** `react_example.py` | |
| ```python | |
| import asyncio | |
| from src.agents.react import ReActAgent | |
| from src.tools.registry import ToolRegistry, ToolDefinition, RiskClass, ApprovalPolicy | |
| from src.inference.bedrock_backend import BedrockBackend | |
| # Define tools | |
| async def file_read_handler(params): | |
| """Read file contents""" | |
| path = params["path"] | |
| with open(path, 'r') as f: | |
| return {"contents": f.read()} | |
| async def file_write_handler(params): | |
| """Write to file""" | |
| path = params["path"] | |
| content = params["content"] | |
| with open(path, 'w') as f: | |
| f.write(content) | |
| return {"status": "written", "bytes": len(content)} | |
| # Create registry and register tools | |
| registry = ToolRegistry() | |
| file_read_tool = ToolDefinition( | |
| tool_id="fs.read", | |
| version="1.0.0", | |
| title="Read File", | |
| description="Read contents of a text file", | |
| input_schema={ | |
| "type": "object", | |
| "properties": {"path": {"type": "string"}}, | |
| "required": ["path"] | |
| }, | |
| output_schema={ | |
| "type": "object", | |
| "properties": {"contents": {"type": "string"}}, | |
| "required": ["contents"] | |
| }, | |
| risk_class=RiskClass.LOCAL_READ, | |
| approval_policy=ApprovalPolicy.AUTOMATIC, | |
| timeout_ms=5000, | |
| handler=file_read_handler | |
| ) | |
| file_write_tool = ToolDefinition( | |
| tool_id="fs.write", | |
| version="1.0.0", | |
| title="Write File", | |
| description="Write contents to a text file", | |
| input_schema={ | |
| "type": "object", | |
| "properties": { | |
| "path": {"type": "string"}, | |
| "content": {"type": "string"} | |
| }, | |
| "required": ["path", "content"] | |
| }, | |
| output_schema={ | |
| "type": "object", | |
| "properties": {"status": {"type": "string"}, "bytes": {"type": "integer"}}, | |
| "required": ["status"] | |
| }, | |
| risk_class=RiskClass.LOCAL_WRITE, | |
| approval_policy=ApprovalPolicy.USER_CONFIRMATION, | |
| timeout_ms=5000, | |
| handler=file_write_handler | |
| ) | |
| registry.register(file_read_tool) | |
| registry.register(file_write_tool) | |
| # Create model backend | |
| model = BedrockBackend( | |
| region="us-east-1", | |
| model_id="us.anthropic.claude-haiku-4-5-20251001-v1:0" | |
| ) | |
| async def main(): | |
| # Create agent | |
| agent = ReActAgent( | |
| model_interface=model, | |
| tool_registry=registry, | |
| max_steps=5 | |
| ) | |
| # Execute task | |
| task_prompt = """ | |
| Read the file at /tmp/data.txt, count the lines, | |
| then write the count to /tmp/count.txt | |
| """ | |
| result = await agent.execute_task(task_prompt) | |
| print(f"Final result: {result}") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
| ``` | |
| ### Example 3: Deterministic Replay on Error | |
| **File:** `replay_example.py` | |
| ```python | |
| import asyncio | |
| from src.continuity.manager import ContinuityManager | |
| from src.core.types import Task | |
| async def execute_with_replay(task_id: str, task_text: str): | |
| """Execute task with automatic replay on error""" | |
| # Initialize continuity manager | |
| continuity = ContinuityManager(base_dir="~/.sovereign/continuity") | |
| # Check for prior checkpoint | |
| prior_state = continuity.load_checkpoint(task_id) | |
| if prior_state: | |
| print(f"Found checkpoint for {task_id}, resuming...") | |
| state = prior_state | |
| else: | |
| print(f"Starting fresh execution of {task_id}") | |
| state = continuity.create_checkpoint(task_id) | |
| # Create task entity | |
| task = Task( | |
| id=task_id, | |
| description=task_text, | |
| context={} | |
| ) | |
| # Execute with checkpointing | |
| try: | |
| # Step 1: Route task | |
| print("Step 1: Routing...") | |
| continuity.checkpoint(task_id, {"step": 1, "status": "routing"}) | |
| routing_result = await route_task(task) | |
| # Step 2: Execute with agent | |
| print("Step 2: Agent execution...") | |
| continuity.checkpoint(task_id, {"step": 2, "status": "agent_exec"}) | |
| agent_result = await run_agent(task, routing_result) | |
| # Step 3: Finalize | |
| print("Step 3: Finalizing...") | |
| continuity.checkpoint(task_id, {"step": 3, "status": "complete"}) | |
| return agent_result | |
| except Exception as e: | |
| print(f"Error at step {state.get('step')}: {e}") | |
| # On next invocation, will resume from checkpoint | |
| continuity.checkpoint(task_id, { | |
| "step": state.get('step'), | |
| "status": "error", | |
| "error": str(e) | |
| }) | |
| raise | |
| async def route_task(task): | |
| # Routing logic | |
| return {"experts": ["coder"], "confidence": 0.95} | |
| async def run_agent(task, routing_result): | |
| # Agent execution logic | |
| return {"response": "Task completed", "steps": 3} | |
| if __name__ == "__main__": | |
| # First run (will error at step 2) | |
| try: | |
| asyncio.run(execute_with_replay("task-001", "Write a function")) | |
| except Exception: | |
| pass | |
| # Second run (will resume from checkpoint, skip step 1) | |
| asyncio.run(execute_with_replay("task-001", "Write a function")) | |
| ``` | |
| ### Example 4: Custom Tool Registration | |
| **File:** `custom_tool_example.py` | |
| ```python | |
| import asyncio | |
| from src.tools.registry import ToolRegistry, ToolDefinition, RiskClass, ApprovalPolicy | |
| # Custom tool: Calculate fibonacci | |
| async def fibonacci_handler(params): | |
| """Calculate nth Fibonacci number""" | |
| n = params["n"] | |
| if n <= 1: | |
| return {"value": n} | |
| a, b = 0, 1 | |
| for _ in range(n - 1): | |
| a, b = b, a + b | |
| return {"value": b} | |
| # Custom tool: Validate email | |
| async def email_validator_handler(params): | |
| """Validate email format""" | |
| email = params["email"] | |
| import re | |
| pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' | |
| is_valid = bool(re.match(pattern, email)) | |
| return {"is_valid": is_valid, "email": email} | |
| async def main(): | |
| registry = ToolRegistry() | |
| # Register Fibonacci tool | |
| fib_tool = ToolDefinition( | |
| tool_id="math.fibonacci", | |
| version="1.0.0", | |
| title="Fibonacci Calculator", | |
| description="Calculate the nth Fibonacci number", | |
| input_schema={ | |
| "type": "object", | |
| "properties": {"n": {"type": "integer", "minimum": 0}}, | |
| "required": ["n"] | |
| }, | |
| output_schema={ | |
| "type": "object", | |
| "properties": {"value": {"type": "integer"}}, | |
| "required": ["value"] | |
| }, | |
| risk_class=RiskClass.PURE_COMPUTATION, | |
| approval_policy=ApprovalPolicy.AUTOMATIC, | |
| timeout_ms=1000, | |
| handler=fibonacci_handler | |
| ) | |
| # Register email validator tool | |
| email_tool = ToolDefinition( | |
| tool_id="validation.email", | |
| version="1.0.0", | |
| title="Email Validator", | |
| description="Validate email format", | |
| input_schema={ | |
| "type": "object", | |
| "properties": {"email": {"type": "string"}}, | |
| "required": ["email"] | |
| }, | |
| output_schema={ | |
| "type": "object", | |
| "properties": { | |
| "is_valid": {"type": "boolean"}, | |
| "email": {"type": "string"} | |
| }, | |
| "required": ["is_valid", "email"] | |
| }, | |
| risk_class=RiskClass.PURE_COMPUTATION, | |
| approval_policy=ApprovalPolicy.AUTOMATIC, | |
| timeout_ms=500, | |
| handler=email_validator_handler | |
| ) | |
| registry.register(fib_tool) | |
| registry.register(email_tool) | |
| # Use tools | |
| fib_result = await asyncio.wait_for( | |
| fibonacci_handler({"n": 10}), | |
| timeout=1 | |
| ) | |
| print(f"Fibonacci(10) = {fib_result['value']}") | |
| email_result = await asyncio.wait_for( | |
| email_validator_handler({"email": "user@example.com"}), | |
| timeout=0.5 | |
| ) | |
| print(f"Email validation: {email_result}") | |
| # List all registered tools | |
| print("\nRegistered tools:") | |
| for tool_id in registry.list_all(): | |
| print(f" - {tool_id}") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
| ``` | |
| --- | |
| ## 4. Glossary | |
| | Term | Definition | Related | | |
| |------|-----------|---------| | |
| | **ReAct** | Reasoning + Acting loop; agent cycles through Thought β Action β Observation steps until task complete. | ReActAgent, agent loop | | |
| | **Expert** | Async callback function that processes a task and returns a result. Experts are routed to via sparse selection. | Routing, dispatch, RoutingPipeline | | |
| | **Routing pipeline** | 11-stage process: parse, build graph, Jordan transform, Jacobian analysis, constraint eval, sparse activation, NAND filter, dispatch, merge. | RoutingPipeline, trace | | |
| | **Jordan transform** | Linear algebra operation: eigenvalue decomposition of adjacency matrix to detect invariants and stability. | ARCHITECTURE.md Β§3 | | |
| | **Jacobian analysis** | Sensitivity analysis; computes condition number and rank of constraint gradients to evaluate feasibility. | Routing stage 5 | | |
| | **NAND filtering** | Boolean logic: suppress expert pairs registered as conflicting (mutual exclusion). Uses opcode_registry. | Conflict resolution | | |
| | **WORM ledger** | Write-Once Read-Many append-only log; all events sealed with Ed25519 signatures + Blake3 hashes; immutable evidence trail. | WORMLedger, evidence | | |
| | **Continuity snapshot** | Point-in-time checkpoint of agent state (seed, env vars, model weights); enables deterministic replay. | ContinuityManager | | |
| | **Dispatch** | Asynchronous invocation of selected experts; collects results with timeout enforcement. | Expert callbacks | | |
| | **Merge strategy** | Algorithm to combine results from multiple experts: `weighted_concat`, `weighted_avg`, `highest_weight`, `ensemble_text`. | Routing dispatch | | |
| | **Tool registry** | Central registry of available tools; stores definitions, schemas, policies, risk classification, handlers. | Tool system | | |
| | **Sandbox** | Isolated execution environment (seccomp, namespaces); protects filesystem, network, processes. | Security | | |
| | **Path jail** | Canonical path resolution; prevents directory traversal, symlink escape, relative path breakout. | Security | | |
| | **Model interface** | Abstract base class for inference backends (Bedrock, Ollama, multi-provider fallback). | Inference | | |
| | **Approval policy** | Authorization rule for tool execution: AUTOMATIC, USER_CONFIRMATION, ADMIN_ONLY, NEVER. | Tool policy | | |
| | **Risk class** | Categorization of tool danger: PURE_COMPUTATION (0) β LOCAL_READ β LOCAL_WRITE β FINANCIAL (8). | Tool metadata | | |
| | **IPC router** | Inter-process communication; routes tool calls to native implementations or fallback Python handlers. | Tool dispatch | | |
| | **Trace** | Complete execution record from routing; includes intent, weights, constraints, active experts, dispatch results. | Diagnostics | | |
| | **Quantization** | Neural network model compression; reduces parameter precision (float32 β int8) to decrease memory. | Training | | |
| | **Checkpoint workflow** | Full sequence: create checkpoint, apply pruning, quantize, upload to S3. | CheckpointManager | | |
| | **Recursive memory** | Learnable persistent state buffer; GRU cell + memory gate + decay for stateful inference. | Models | | |
| | **Bedrock backend** | AWS Bedrock model provider; invokes Claude Haiku/Opus via boto3; non-streaming. | Inference | | |
| | **Local model** | Ollama or sentence-transformers; fallback for offline inference or custom weights. | Inference | | |
| | **Multi-provider** | Selects inference backend by task classification; fallback chain on failure. | Inference | | |
| | **Node key** | Ed25519 keypair for agent identity; used to seal evidence and authenticate routing decisions. | Security | | |
| | **Evidence artifact** | Single entry in WORM ledger; JSON event with seq, timestamp, hash_prev, Ed25519 sig. | Evidence | | |
| | **Sparse activation** | Scoring experts and zeroing low-confidence paths; reduces compute by selective dispatch. | Routing | | |
| | **Symbolic graph** | Directed graph representation of task structure; nodes=concepts, edges=relationships. | Routing stage 3 | | |
| --- | |
| ## 5. Resources | |
| ### Official Documentation | |
| - **[ARCHITECTURE.md](./repository-reference/ARCHITECTURE.md)** β System design, subsystem relationships, language boundaries, deployment architecture | |
| - **[OPERATIONS.md](./repository-reference/OPERATIONS.md)** β Startup, monitoring, debugging, configuration, maintenance, scaling, troubleshooting | |
| - **[ERROR_PATHS.md](./repository-reference/ERROR_PATHS.md)** β Exception handling, logging, recovery strategies, cleanup, error analysis | |
| - **[ROUTING.md](./ROUTING.md)** β Routing pipeline guide, expert tuning, research router details, diagnostics | |
| - **[TOOLS.md](./TOOLS.md)** β Tool registration, validation, risk classification, IPC dispatch, native implementations | |
| - **[SECURITY.md](./SECURITY.md)** β Threat model, trust boundaries, sandbox isolation, path jail, evidence sealing | |
| - **[TESTING.md](./TESTING.md)** β Test infrastructure, routing verification, agent loop testing, error path coverage | |
| ### Formal Research & Papers | |
| - **[Forge Tournament + SUBLEQ](../research/papers/forge_tournament_subleq_to_braid.md)** β Tournament architecture, SUBLEQ formal semantics, topology adaptation | |
| - **[Sparse Routing Report](../research/sparse-routing/docs/report.md)** β Graph model, rank estimator, proposal/verification/commit, limitations, benchmarks | |
| - **[LiquidOps Kernel](../research/papers/)** β Compiler correctness, liquid types, verified optimization | |
| - **[Entropy Bounds](../research/papers/)** β Weil bounds for finite fields, information-theoretic limits | |
| ### Formal Proofs (Lean 4 & Agda) | |
| - **[SUBLEQ.lean](../research/formal/subleq/SUBLEQ.lean)** β Provably correct SUBLEQ interpreter (0 sorry terms) | |
| - **[Projective Invariant](../research/formal/)** β Agda formalization of composition algebra + determinism | |
| - **[Matrix Algebra](../research/formal/)** β Formalized Jordan canonical form, eigenvalue stability | |
| ### Implementation Guides | |
| - **[Local Training with Ollama](./LOCAL_TRAINING_OLLAMA.md)** β Offline inference setup, model management, custom weights | |
| - **[ASR & Message Bridge](./ASR_AND_BRIDGE.md)** β Audio input pipeline, speech-to-text routing, real-time streaming | |
| - **[Machine Code Runtime](./MACHINE_CODE.md)** β x86-64 bytecode generation, sandbox execution, native dispatch | |
| - **[BRICK Protocol Specification](./BRICK_PROTOCOL_SPECIFICATION.md)** β Agent message encoding, binary layout, interop | |
| ### Packages & Libraries | |
| - **[Sovereign Memory Twin](../hf/sovereign-memory-twin/)** β Hugging Face model card, RecursiveMemoryTwinNetwork config | |
| - **[BURT-Imma](../hf/burt-imma/)** β Model config, tokenizer, training artifacts | |
| - **[Training Corpus Schema](../hf/sovereign-training-corpus/)** β Dataset card, corpus structure, versioning | |
| - **[AgentFishTank](../training/)** β Swift/SceneKit training visualization, agent state machines, task scheduling | |
| ### Community & Contributions | |
| - **GitHub:** [SNAPKITTYWEST/sovereign-engine-v2](https://github.com/SNAPKITTYWEST/sovereign-engine-v2) | |
| - **Issues & Discussions:** Use GitHub Issues for bugs; Discussions for design questions | |
| - **Contributing:** See repository CONTRIBUTING.md (if present) | |
| ### Key Codebase Modules | |
| | Module | Purpose | Key Files | | |
| |--------|---------|-----------| | |
| | **Routing** | Expert selection & dispatch | `src/routing/pipeline.py`, `dispatch.py`, `constraints.py` | | |
| | **Agents** | ReAct, MCTS, Shadow loops | `src/agents/react.py`, `mcts.py`, `shadow.py` | | |
| | **Tools** | Registration, execution, approval | `src/tools/registry.py`, `loader.py`, `approval.py` | | |
| | **Inference** | Model backends | `src/inference/bedrock_backend.py`, `multi.py` | | |
| | **Continuity** | State management, replay | `src/continuity/manager.py`, checkpoint methods | | |
| | **Evidence** | WORM ledger, sealing | `src/core/evidence.py`, Ed25519 + Blake3 | | |
| | **Runtime** | VM, sandbox, x86 gen | `src/runtime/sovereign_machine.py`, `x86_gen.py` | | |
| | **Bridge** | HTTP API, key management | `src/bridge/http_server.py`, `key_manager.py` | | |
| ### Performance Tuning Parameters | |
| ```bash | |
| # Throughput optimization | |
| SOVEREIGN_BATCH_SIZE=32 | |
| SOVEREIGN_WORKERS=8 | |
| # Latency optimization | |
| SOVEREIGN_BATCH_SIZE=1 | |
| SOVEREIGN_MAX_QUEUE_DEPTH=100 | |
| # Memory optimization | |
| SOVEREIGN_CACHE_MAX_SIZE_MB=512 | |
| SOVEREIGN_LOG_RETENTION_DAYS=3 | |
| # Cost optimization | |
| BEDROCK_MODEL=us.anthropic.claude-haiku-4-5-20251001-v1:0 | |
| SOVEREIGN_TOOL_CACHE_ENABLED=true | |
| ``` | |
| --- | |
| ## Summary | |
| **Deployment:** Docker/K8s with Prometheus monitoring; multi-region failover; secrets via AWS Secrets Manager. | |
| **FAQ:** 6 common questions covering tool registration, local models, routing traces, conflict resolution, ledger integrity, extension points. | |
| **Examples:** 4 runnable Python scripts demonstrating sparse routing, ReAct agents, replay, and custom tool registration. | |
| **Glossary:** 30+ key terms spanning routing, evidence, tools, inference, security. | |
| **Resources:** 20+ links to documentation, papers, code, and community. | |
| All metrics, configurations, and error flows are documented with real code examples drawn directly from the Sovereign Engine v2 codebase. | |
| --- | |
| **Total word count:** ~2,100 words | **Last updated:** 2026-09-19 | |