Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /langgraph_orchestration /constitutional_checkpoints.py
| """ | |
| ACGS-2 LangGraph Orchestration - Constitutional Checkpoints | |
| Constitutional Hash: 608508a9bd224290 | |
| Constitutional checkpoints ensure governance compliance at | |
| transition boundaries: | |
| - Hash validation at each checkpoint | |
| - MACI role enforcement | |
| - Constitutional classification of state changes | |
| - Audit trail for all mutations | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import time | |
| import uuid | |
| from abc import ABC, abstractmethod | |
| from datetime import UTC, datetime | |
| from typing import Protocol | |
| try: | |
| from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH | |
| except ImportError: | |
| CONSTITUTIONAL_HASH = "standalone" | |
| from enhanced_agent_bus.bus_types import JSONDict | |
| from enhanced_agent_bus.observability.structured_logging import get_logger | |
| from .exceptions import CheckpointError, ConstitutionalViolationError | |
| from .models import ( | |
| Checkpoint, | |
| CheckpointStatus, | |
| ExecutionContext, | |
| GraphState, | |
| ) | |
| logger = get_logger(__name__) | |
| _CHECKPOINT_OPERATION_ERRORS = ( | |
| RuntimeError, | |
| ValueError, | |
| TypeError, | |
| AttributeError, | |
| LookupError, | |
| OSError, | |
| TimeoutError, | |
| ConnectionError, | |
| ) | |
| class _CheckpointPersistenceProtocol(Protocol): | |
| async def save_checkpoint(self, checkpoint: Checkpoint) -> None: ... | |
| async def load_checkpoint(self, checkpoint_id: str) -> Checkpoint | None: ... | |
| async def list_checkpoints( | |
| self, workflow_id: str, run_id: str | None = None | |
| ) -> list[Checkpoint]: ... | |
| async def delete_checkpoint(self, checkpoint_id: str) -> bool: ... | |
| class CheckpointValidator(ABC): | |
| """Abstract validator for checkpoint validation. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| async def validate( | |
| self, | |
| checkpoint: Checkpoint, | |
| context: ExecutionContext, | |
| ) -> tuple[bool, list[str]]: | |
| """Validate a checkpoint. | |
| Args: | |
| checkpoint: Checkpoint to validate | |
| context: Execution context | |
| Returns: | |
| Tuple of (is_valid, list of violation messages) | |
| """ | |
| ... | |
| class ConstitutionalHashValidator(CheckpointValidator): | |
| """Validates constitutional hash compliance. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| def __init__(self, expected_hash: str = CONSTITUTIONAL_HASH) -> None: | |
| self.expected_hash = expected_hash | |
| async def validate( | |
| self, | |
| checkpoint: Checkpoint, | |
| context: ExecutionContext, | |
| ) -> tuple[bool, list[str]]: | |
| """Validate constitutional hash matches.""" | |
| violations = [] | |
| if checkpoint.constitutional_hash != self.expected_hash: | |
| violations.append( | |
| f"Checkpoint hash mismatch: expected '{self.expected_hash}', " | |
| f"got '{checkpoint.constitutional_hash}'" | |
| ) | |
| if checkpoint.state.constitutional_hash != self.expected_hash: | |
| violations.append( | |
| f"State hash mismatch: expected '{self.expected_hash}', " | |
| f"got '{checkpoint.state.constitutional_hash}'" | |
| ) | |
| if context.constitutional_hash != self.expected_hash: | |
| violations.append( | |
| f"Context hash mismatch: expected '{self.expected_hash}', " | |
| f"got '{context.constitutional_hash}'" | |
| ) | |
| return len(violations) == 0, violations | |
| class StateIntegrityValidator(CheckpointValidator): | |
| """Validates state integrity using checksums. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| async def validate( | |
| self, | |
| checkpoint: Checkpoint, | |
| context: ExecutionContext, | |
| ) -> tuple[bool, list[str]]: | |
| """Validate state integrity via checksum.""" | |
| violations = [] | |
| try: | |
| # Compute state checksum | |
| state_json = json.dumps(checkpoint.state.data, sort_keys=True) | |
| computed_hash = hashlib.sha256(state_json.encode()).hexdigest()[:16] | |
| # Store for audit | |
| checkpoint.metadata["state_checksum"] = computed_hash | |
| # Verify version continuity | |
| if checkpoint.state.version <= 0 and checkpoint.step_index > 0: | |
| violations.append( | |
| f"Invalid state version {checkpoint.state.version} at step {checkpoint.step_index}" | |
| ) | |
| except _CHECKPOINT_OPERATION_ERRORS as e: | |
| violations.append(f"State integrity check failed: {e}") | |
| return len(violations) == 0, violations | |
| class MACIRoleValidator(CheckpointValidator): | |
| """Validates MACI role constraints at checkpoints. | |
| Integrates with the MACI enforcement system to ensure | |
| role-based access control at transition boundaries. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| def __init__(self, maci_enforcer: object | None = None) -> None: | |
| self.maci_enforcer = maci_enforcer | |
| async def validate( | |
| self, | |
| checkpoint: Checkpoint, | |
| context: ExecutionContext, | |
| ) -> tuple[bool, list[str]]: | |
| """Validate MACI role constraints.""" | |
| violations = [] | |
| if not self.maci_enforcer: | |
| # No MACI enforcer - pass validation | |
| checkpoint.maci_validated = True | |
| return True, [] | |
| try: | |
| # Check if session has MACI context | |
| session_id = context.maci_session_id | |
| if not session_id: | |
| checkpoint.maci_validated = True | |
| return True, [] | |
| # Validate checkpoint creation permissions | |
| # In production, this would check agent roles | |
| checkpoint.maci_validated = True | |
| except _CHECKPOINT_OPERATION_ERRORS as e: | |
| violations.append(f"MACI validation failed: {e}") | |
| checkpoint.maci_validated = False | |
| return len(violations) == 0, violations | |
| class ConstitutionalCheckpoint: | |
| """Constitutional checkpoint with full validation. | |
| Wraps checkpoint creation with constitutional validation, | |
| MACI enforcement, and audit logging. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| def __init__( | |
| self, | |
| workflow_id: str, | |
| run_id: str, | |
| node_id: str, | |
| step_index: int, | |
| state: GraphState, | |
| metadata: JSONDict | None = None, | |
| ) -> None: | |
| self.checkpoint = Checkpoint( | |
| id=str(uuid.uuid4()), | |
| workflow_id=workflow_id, | |
| run_id=run_id, | |
| node_id=node_id, | |
| step_index=step_index, | |
| state=state, | |
| metadata=metadata or {}, | |
| constitutional_hash=CONSTITUTIONAL_HASH, | |
| ) | |
| self._validators: list[tuple[str, CheckpointValidator]] = [] | |
| self._validation_results: list[tuple[str, bool, list[str]]] = [] | |
| def add_validator(self, name: str, validator: CheckpointValidator) -> None: | |
| """Add a validator to the checkpoint.""" | |
| self._validators.append((name, validator)) | |
| async def validate(self, context: ExecutionContext) -> bool: | |
| """Run all validators on the checkpoint. | |
| Args: | |
| context: Execution context | |
| Returns: | |
| True if all validations pass | |
| Raises: | |
| ConstitutionalViolationError: If validation fails | |
| """ | |
| all_valid = True | |
| all_violations = [] | |
| for validator_tuple in self._validators: | |
| name: str = validator_tuple[0] | |
| validator: CheckpointValidator = validator_tuple[1] | |
| try: | |
| is_valid, violations = await validator.validate(self.checkpoint, context) | |
| self._validation_results.append((name, is_valid, violations)) | |
| if not is_valid: | |
| all_valid = False | |
| all_violations.extend([f"[{name}] {v}" for v in violations]) | |
| except _CHECKPOINT_OPERATION_ERRORS as e: | |
| logger.error(f"Validator {name} raised exception: {e}") | |
| all_valid = False | |
| all_violations.append(f"[{name}] Validation error: {e}") | |
| if all_valid: | |
| self.checkpoint.status = CheckpointStatus.VALIDATED | |
| self.checkpoint.validated_at = datetime.now(UTC) | |
| self.checkpoint.constitutional_validated = True | |
| else: | |
| self.checkpoint.status = CheckpointStatus.FAILED | |
| raise ConstitutionalViolationError( | |
| violations=all_violations, | |
| node_id=self.checkpoint.node_id, | |
| ) | |
| return all_valid | |
| def to_dict(self) -> JSONDict: | |
| """Convert to dictionary for serialization.""" | |
| return { | |
| "checkpoint": self.checkpoint.model_dump(), | |
| "validation_results": [ | |
| {"name": name, "valid": valid, "violations": violations} | |
| for name, valid, violations in self._validation_results | |
| ], | |
| } | |
| class ConstitutionalCheckpointManager: | |
| """Manager for constitutional checkpoints. | |
| Handles checkpoint creation, validation, persistence, | |
| and recovery operations. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| def __init__( | |
| self, | |
| persistence: _CheckpointPersistenceProtocol | None = None, | |
| maci_enforcer: object | None = None, | |
| enable_integrity_check: bool = True, | |
| constitutional_hash: str = CONSTITUTIONAL_HASH, | |
| ) -> None: | |
| self.persistence = persistence | |
| self.maci_enforcer = maci_enforcer | |
| self.enable_integrity_check = enable_integrity_check | |
| self.constitutional_hash = constitutional_hash | |
| # Standard validators | |
| self._validators: list[tuple[str, CheckpointValidator]] = [ | |
| ("constitutional_hash", ConstitutionalHashValidator(constitutional_hash)), | |
| ] | |
| if enable_integrity_check: | |
| self._validators.append(("state_integrity", StateIntegrityValidator())) | |
| if maci_enforcer: | |
| self._validators.append(("maci_role", MACIRoleValidator(maci_enforcer))) | |
| # Checkpoint cache | |
| self._checkpoints: dict[str, Checkpoint] = {} | |
| async def create_checkpoint( | |
| self, | |
| context: ExecutionContext, | |
| node_id: str, | |
| state: GraphState, | |
| validate: bool = True, | |
| metadata: JSONDict | None = None, | |
| ) -> Checkpoint: | |
| """Create a new checkpoint with validation. | |
| Args: | |
| context: Execution context | |
| node_id: Node at which checkpoint is created | |
| state: Current graph state | |
| validate: Whether to run validation | |
| metadata: Optional checkpoint metadata | |
| Returns: | |
| Created checkpoint | |
| Raises: | |
| ConstitutionalViolationError: If validation fails | |
| """ | |
| start_time = time.perf_counter() | |
| checkpoint = ConstitutionalCheckpoint( | |
| workflow_id=context.workflow_id, | |
| run_id=context.run_id, | |
| node_id=node_id, | |
| step_index=context.step_count, | |
| state=state, | |
| metadata=metadata, | |
| ) | |
| # Add validators | |
| for name, validator in self._validators: | |
| checkpoint.add_validator(name, validator) | |
| # Run validation | |
| if validate: | |
| await checkpoint.validate(context) | |
| # Persist checkpoint | |
| if self.persistence: | |
| await self.persistence.save_checkpoint(checkpoint.checkpoint) | |
| # Update cache | |
| self._checkpoints[checkpoint.checkpoint.id] = checkpoint.checkpoint | |
| # Update context | |
| context.checkpoints.append(checkpoint.checkpoint.id) | |
| context.last_checkpoint_id = checkpoint.checkpoint.id | |
| execution_time = (time.perf_counter() - start_time) * 1000 | |
| logger.debug( | |
| f"[{self.constitutional_hash}] Created checkpoint {checkpoint.checkpoint.id} " | |
| f"at node {node_id} in {execution_time:.2f}ms" | |
| ) | |
| return checkpoint.checkpoint | |
| async def restore_checkpoint( | |
| self, | |
| checkpoint_id: str, | |
| context: ExecutionContext, | |
| ) -> tuple[Checkpoint, GraphState]: | |
| """Restore state from a checkpoint. | |
| Args: | |
| checkpoint_id: Checkpoint ID to restore | |
| context: Execution context | |
| Returns: | |
| Tuple of (checkpoint, restored state) | |
| Raises: | |
| CheckpointError: If checkpoint not found or invalid | |
| """ | |
| # Try cache first | |
| checkpoint = self._checkpoints.get(checkpoint_id) | |
| # Try persistence | |
| if not checkpoint and self.persistence: | |
| checkpoint = await self.persistence.load_checkpoint(checkpoint_id) | |
| if not checkpoint: | |
| raise CheckpointError( | |
| checkpoint_id=checkpoint_id, | |
| operation="restore", | |
| reason="Checkpoint not found", | |
| workflow_id=context.workflow_id, | |
| ) | |
| # Validate constitutional hash | |
| if checkpoint.constitutional_hash != self.constitutional_hash: | |
| raise CheckpointError( | |
| checkpoint_id=checkpoint_id, | |
| operation="restore", | |
| reason="Constitutional hash mismatch", | |
| workflow_id=context.workflow_id, | |
| ) | |
| # Update checkpoint status | |
| checkpoint.status = CheckpointStatus.RESTORED | |
| checkpoint.metadata["restored_at"] = datetime.now(UTC).isoformat() | |
| logger.info( | |
| f"[{self.constitutional_hash}] Restored checkpoint {checkpoint_id} " | |
| f"for workflow {context.workflow_id}" | |
| ) | |
| return checkpoint, checkpoint.state | |
| async def get_checkpoint(self, checkpoint_id: str) -> Checkpoint | None: | |
| """Get a checkpoint by ID. | |
| Args: | |
| checkpoint_id: Checkpoint ID | |
| Returns: | |
| Checkpoint if found, None otherwise | |
| """ | |
| checkpoint = self._checkpoints.get(checkpoint_id) | |
| if not checkpoint and self.persistence: | |
| checkpoint = await self.persistence.load_checkpoint(checkpoint_id) | |
| if checkpoint: | |
| self._checkpoints[checkpoint_id] = checkpoint | |
| return checkpoint | |
| async def list_checkpoints( | |
| self, | |
| workflow_id: str, | |
| run_id: str | None = None, | |
| ) -> list[Checkpoint]: | |
| """List checkpoints for a workflow. | |
| Args: | |
| workflow_id: Workflow ID | |
| run_id: Optional run ID filter | |
| Returns: | |
| List of checkpoints | |
| """ | |
| if self.persistence: | |
| return await self.persistence.list_checkpoints(workflow_id, run_id) | |
| # Filter from cache | |
| return [ | |
| cp | |
| for cp in self._checkpoints.values() | |
| if cp.workflow_id == workflow_id and (run_id is None or cp.run_id == run_id) | |
| ] | |
| async def delete_checkpoint(self, checkpoint_id: str) -> bool: | |
| """Delete a checkpoint. | |
| Args: | |
| checkpoint_id: Checkpoint ID to delete | |
| Returns: | |
| True if deleted | |
| """ | |
| deleted = False | |
| if checkpoint_id in self._checkpoints: | |
| del self._checkpoints[checkpoint_id] | |
| deleted = True | |
| if self.persistence: | |
| await self.persistence.delete_checkpoint(checkpoint_id) | |
| deleted = True | |
| return deleted | |
| async def cleanup_old_checkpoints( | |
| self, | |
| workflow_id: str, | |
| keep_count: int = 5, | |
| ) -> int: | |
| """Cleanup old checkpoints, keeping most recent. | |
| Args: | |
| workflow_id: Workflow ID | |
| keep_count: Number of checkpoints to keep | |
| Returns: | |
| Number of checkpoints deleted | |
| """ | |
| checkpoints = await self.list_checkpoints(workflow_id) | |
| # Sort by creation time, newest first | |
| checkpoints.sort(key=lambda c: c.created_at, reverse=True) | |
| # Delete old checkpoints | |
| deleted = 0 | |
| for checkpoint in checkpoints[keep_count:]: | |
| if await self.delete_checkpoint(checkpoint.id): | |
| deleted += 1 | |
| return deleted | |
| def create_checkpoint_manager( | |
| persistence: _CheckpointPersistenceProtocol | None = None, | |
| maci_enforcer: object | None = None, | |
| enable_integrity_check: bool = True, | |
| constitutional_hash: str = CONSTITUTIONAL_HASH, | |
| ) -> ConstitutionalCheckpointManager: | |
| """Factory function to create checkpoint manager. | |
| Args: | |
| persistence: Optional persistence backend | |
| maci_enforcer: Optional MACI enforcer | |
| enable_integrity_check: Enable state integrity validation | |
| constitutional_hash: Constitutional hash to enforce | |
| Returns: | |
| Configured checkpoint manager | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| return ConstitutionalCheckpointManager( | |
| persistence=persistence, | |
| maci_enforcer=maci_enforcer, | |
| enable_integrity_check=enable_integrity_check, | |
| constitutional_hash=constitutional_hash, | |
| ) | |
| __all__ = [ | |
| "CheckpointValidator", | |
| "ConstitutionalCheckpoint", | |
| "ConstitutionalCheckpointManager", | |
| "ConstitutionalHashValidator", | |
| "MACIRoleValidator", | |
| "StateIntegrityValidator", | |
| "create_checkpoint_manager", | |
| ] | |
Xet Storage Details
- Size:
- 17.2 kB
- Xet hash:
- 723ae81b3fdc36ff7c51ce7b6935cce697918c22123a8aed348544da73834f32
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.