From 24334b8a733862ccb2fb896e0a5e2db4a5286cb0 Mon Sep 17 00:00:00 2001 From: rUv Date: Tue, 12 Aug 2025 02:43:26 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Checkpoint:=20Edit=20/workspaces?= =?UTF-8?q?/claude-code-flow/src/verification/types.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/checkpoints/1754966591.json | 1 + .claude/checkpoints/1754966593.json | 1 + src/verification/types.ts | 1384 +++++++++++++++++++++------ 3 files changed, 1069 insertions(+), 317 deletions(-) create mode 100644 .claude/checkpoints/1754966591.json create mode 100644 .claude/checkpoints/1754966593.json diff --git a/.claude/checkpoints/1754966591.json b/.claude/checkpoints/1754966591.json new file mode 100644 index 0000000000..0787b90013 --- /dev/null +++ b/.claude/checkpoints/1754966591.json @@ -0,0 +1 @@ +{"branch": "checkpoint/pre-edit-20250812-024311", "file": "/workspaces/claude-code-flow/jest.config.js", "timestamp": "2025-08-12T02:43:11Z", "type": "pre-edit", "original_branch": "alpha-89"} diff --git a/.claude/checkpoints/1754966593.json b/.claude/checkpoints/1754966593.json new file mode 100644 index 0000000000..31bec20982 --- /dev/null +++ b/.claude/checkpoints/1754966593.json @@ -0,0 +1 @@ +{"tag": "checkpoint-20250812-024313", "file": "/workspaces/claude-code-flow/jest.config.js", "timestamp": "2025-08-12T02:43:13Z", "type": "post-edit", "branch": "alpha-89", "diff_summary": " .claude-flow/metrics/performance.json | 2 +- .claude-flow/metrics/system-metrics.json | 208 ++++++++++++++++++++++++++++--- .claude-flow/metrics/task-metrics.json | 6 +- .claude/checkpoints/1754966588.json | 1 + .claude/checkpoints/1754966589.json | 1 + jest.config.js | 6 + 6 files changed, 206 insertions(+), 18 deletions(-) "} diff --git a/src/verification/types.ts b/src/verification/types.ts index dbf23123e8..e413f46d2b 100644 --- a/src/verification/types.ts +++ b/src/verification/types.ts @@ -1,349 +1,1099 @@ /** - * Type definitions for Security Enforcement System + * Verification Engine Types and Interfaces + * Comprehensive type definitions for the verification system */ -export interface SecurityConfig { - totalNodes: number; +import type { AgentId, TaskId, SwarmId, TaskResult, AgentState } from '../swarm/types.js'; + +// ===== CORE VERIFICATION TYPES ===== + +export type VerificationLevel = 'basic' | 'standard' | 'strict' | 'critical'; +export type VerificationStatus = 'pending' | 'running' | 'passed' | 'failed' | 'error' | 'cancelled'; +export type CheckpointType = 'pre_execution' | 'mid_execution' | 'post_execution' | 'rollback_point'; +export type ClaimType = 'task_completion' | 'quality_metric' | 'performance_benchmark' | 'system_state' | 'agent_capability'; + +// ===== TRUTH SCORING ===== + +export interface TruthScoreConfig { + /** Minimum threshold for truth score (0-1, default: 0.95) */ threshold: number; - rateLimits: { - perSecond: number; - perMinute: number; - perHour: number; - perDay: number; - }; - byzantineTolerance: { - suspicionThreshold: number; - consensusThreshold: number; - heartbeatInterval: number; - }; - cryptography: { - keySize: number; - algorithm: string; - hashAlgorithm: string; - }; + /** Weighted factors for scoring */ + weights: TruthScoringWeights; + /** Enable/disable specific validation checks */ + checks: TruthValidationChecks; + /** Confidence intervals for statistical validation */ + confidence: ConfidenceConfig; } -export interface ThreatLevel { - level: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'; +export interface TruthScoringWeights { + /** Weight for agent reliability history (0-1) */ + agentReliability: number; + /** Weight for cross-validation results (0-1) */ + crossValidation: number; + /** Weight for external verification (0-1) */ + externalVerification: number; + /** Weight for consistency with known facts (0-1) */ + factualConsistency: number; + /** Weight for logical coherence (0-1) */ + logicalCoherence: number; +} + +export interface TruthValidationChecks { + /** Validate against agent performance history */ + historicalValidation: boolean; + /** Cross-check with multiple agents */ + crossAgentValidation: boolean; + /** Verify against external sources */ + externalValidation: boolean; + /** Check for logical consistency */ + logicalValidation: boolean; + /** Validate statistical claims */ + statisticalValidation: boolean; +} + +export interface ConfidenceConfig { + /** Confidence level for statistical tests (e.g., 0.95 for 95%) */ + level: number; + /** Sample size requirements */ + minSampleSize: number; + /** Maximum acceptable error margin */ + maxErrorMargin: number; +} + +export interface TruthScore { + /** Overall truth score (0-1) */ score: number; - indicators: string[]; - mitigationActions: string[]; -} - -export interface SecurityEvent { - eventId: string; + /** Individual component scores */ + components: TruthScoreComponents; + /** Confidence intervals */ + confidence: ConfidenceInterval; + /** Evidence supporting the score */ + evidence: TruthEvidence[]; + /** Computed timestamp */ timestamp: Date; - type: 'AUTHENTICATION' | 'AUTHORIZATION' | 'BYZANTINE' | 'RATE_LIMIT' | 'CRYPTOGRAPHIC'; - severity: 'INFO' | 'WARNING' | 'ERROR' | 'CRITICAL'; - agentId: string; - details: any; - resolved: boolean; + /** Validation metadata */ + metadata: Record; } -export interface ComplianceReport { - reportId: string; - generatedAt: Date; - period: { from: Date; to: Date }; - totalTransactions: number; - securityEvents: SecurityEvent[]; - auditTrailIntegrity: boolean; - complianceScore: number; +export interface TruthScoreComponents { + agentReliability: number; + crossValidation: number; + externalVerification: number; + factualConsistency: number; + logicalCoherence: number; + overall: number; +} + +export interface ConfidenceInterval { + lower: number; + upper: number; + level: number; +} + +export interface TruthEvidence { + type: 'agent_history' | 'cross_validation' | 'external_source' | 'logical_proof' | 'statistical_test'; + source: string; + weight: number; + score: number; + details: Record; + timestamp: Date; +} + +// ===== VERIFICATION PIPELINE ===== + +export interface VerificationPipelineConfig { + /** Pipeline identification */ + id: string; + name: string; + description: string; + + /** Pipeline configuration */ + level: VerificationLevel; + checkpoints: VerificationCheckpoint[]; + truthScoreConfig: TruthScoreConfig; + + /** Execution settings */ + parallel: boolean; + timeout: number; + retryAttempts: number; + + /** Rollback settings */ + enableRollback: boolean; + snapshotInterval: number; + maxSnapshots: number; + + /** Reporting settings */ + reportLevel: 'minimal' | 'standard' | 'detailed' | 'comprehensive'; + notifications: NotificationConfig[]; +} + +export interface VerificationCheckpoint { + /** Checkpoint identification */ + id: string; + name: string; + type: CheckpointType; + + /** Execution configuration */ + mandatory: boolean; + order: number; + dependencies: string[]; + + /** Validation rules */ + validators: CheckpointValidator[]; + conditions: CheckpointCondition[]; + + /** Execution settings */ + timeout: number; + retryAttempts: number; + + /** Rollback configuration */ + createSnapshot: boolean; + rollbackOnFailure: boolean; +} + +export interface CheckpointValidator { + id: string; + type: 'truth_score' | 'agent_claim' | 'integration_test' | 'state_validation' | 'custom'; + config: Record; + weight: number; + required: boolean; +} + +export interface CheckpointCondition { + field: string; + operator: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'regex' | 'custom'; + value: unknown; + description: string; +} + +export interface NotificationConfig { + type: 'email' | 'webhook' | 'slack' | 'teams' | 'custom'; + endpoint: string; + conditions: string[]; + template: string; +} + +export interface VerificationResult { + /** Result identification */ + id: string; + pipelineId: string; + timestamp: Date; + + /** Overall result */ + status: VerificationStatus; + score: number; + passed: boolean; + + /** Detailed results */ + checkpointResults: CheckpointResult[]; + truthScore: TruthScore; + + /** Performance metrics */ + duration: number; + resourceUsage: ResourceUsage; + + /** Evidence and artifacts */ + evidence: VerificationEvidence[]; + artifacts: Record; + + /** Error information */ + errors: VerificationError[]; + warnings: VerificationWarning[]; + + /** Recommendations */ + recommendations: string[]; + nextSteps: string[]; +} + +export interface CheckpointResult { + checkpointId: string; + status: VerificationStatus; + score: number; + passed: boolean; + duration: number; + validatorResults: ValidatorResult[]; + evidence: VerificationEvidence[]; + errors: VerificationError[]; + warnings: VerificationWarning[]; +} + +export interface ValidatorResult { + validatorId: string; + status: VerificationStatus; + score: number; + passed: boolean; + details: Record; + evidence: VerificationEvidence[]; +} + +export interface ResourceUsage { + cpu: number; + memory: number; + disk: number; + network: number; + tokens?: number; + apiCalls?: number; +} + +export interface VerificationEvidence { + type: string; + source: string; + timestamp: Date; + data: Record; + reliability: number; + weight: number; +} + +export interface VerificationError { + code: string; + message: string; + severity: 'low' | 'medium' | 'high' | 'critical'; + context: Record; + recoverable: boolean; + timestamp: Date; +} + +export interface VerificationWarning { + code: string; + message: string; + severity: 'low' | 'medium' | 'high'; + context: Record; + recommendation: string; + timestamp: Date; +} + +// ===== AGENT CLAIM VALIDATION ===== + +export interface AgentClaim { + /** Claim identification */ + id: string; + agentId: AgentId; + taskId?: TaskId; + swarmId?: SwarmId; + + /** Claim details */ + type: ClaimType; + title: string; + description: string; + + /** Claim data */ + data: Record; + metrics: ClaimMetrics; + + /** Supporting evidence */ + evidence: ClaimEvidence[]; + references: string[]; + + /** Validation status */ + status: VerificationStatus; + confidence: number; + + /** Timestamps */ + submittedAt: Date; + validatedAt?: Date; + + /** Validation metadata */ + metadata: Record; +} + +export interface ClaimMetrics { + /** Performance metrics */ + executionTime?: number; + resourceUsage?: ResourceUsage; + throughput?: number; + errorRate?: number; + + /** Quality metrics */ + accuracy?: number; + precision?: number; + recall?: number; + f1Score?: number; + + /** Custom metrics */ + custom: Record; +} + +export interface ClaimEvidence { + type: 'log_entry' | 'test_result' | 'measurement' | 'artifact' | 'witness_testimony' | 'external_verification'; + source: string; + timestamp: Date; + data: Record; + reliability: number; + verifiable: boolean; +} + +export interface ClaimValidationConfig { + /** Validation rules */ + requiredEvidence: string[]; + minimumConfidence: number; + crossValidationRequired: boolean; + + /** Verification methods */ + methods: ClaimValidationMethod[]; + + /** Timeout and retry settings */ + timeout: number; + retryAttempts: number; + + /** Quality thresholds */ + qualityThresholds: Record; +} + +export interface ClaimValidationMethod { + name: string; + type: 'historical_comparison' | 'cross_agent_verification' | 'external_validation' | 'statistical_test' | 'custom'; + config: Record; + weight: number; + required: boolean; +} + +export interface ClaimValidationResult { + claimId: string; + status: VerificationStatus; + confidence: number; + score: number; + passed: boolean; + + methodResults: MethodValidationResult[]; + evidence: VerificationEvidence[]; + + duration: number; + timestamp: Date; + + errors: VerificationError[]; + warnings: VerificationWarning[]; recommendations: string[]; } -export interface AttackPattern { - patternId: string; - name: string; - description: string; - indicators: string[]; - severity: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'; - mitigation: string[]; - frequency: number; -} - -export interface AgentBehaviorProfile { - agentId: string; - profileCreated: Date; - lastUpdated: Date; - requestPatterns: { - averageRequestsPerMinute: number; - peakRequestTimes: Date[]; - requestTypes: Map; - }; - behaviorScore: number; - anomalies: string[]; - trustLevel: number; -} - -export interface ConsensusState { - roundId: string; - proposalId: string; - participants: string[]; - votes: Map; - consensusReached: boolean; - result: boolean | null; - byzantineNodes: string[]; - timestamp: Date; -} - -export interface ZKProofChallenge { - challengeId: string; - challenger: string; - challenged: string; - proofType: 'KNOWLEDGE' | 'RANGE' | 'MEMBERSHIP' | 'STATEMENT'; - challenge: string; - timestamp: Date; - expiresAt: Date; -} - -export interface ThresholdSignatureState { - signatureId: string; - message: any; - requiredSignatures: number; - currentSignatures: number; - signatories: string[]; - partialSignatures: Map; - completed: boolean; - finalSignature?: string; -} - -export interface RateLimitWindow { - windowStart: Date; - windowEnd: Date; - requestCount: number; - limit: number; - violations: number; -} - -export interface SecurityAlert { - alertId: string; - timestamp: Date; - type: 'BREACH_ATTEMPT' | 'BYZANTINE_BEHAVIOR' | 'RATE_LIMIT_EXCEEDED' | 'AUTH_FAILURE' | 'SIGNATURE_INVALID'; - agentId: string; - severity: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'; - description: string; - evidence: any; - automaticResponse: string[]; - requiresManualIntervention: boolean; -} - -export interface VerificationChain { - chainId: string; - startedBy: string; - truthClaim: any; - verificationSteps: Array<{ - stepId: string; - verifiedBy: string; - timestamp: Date; - result: boolean; - evidence: any[]; - signature: string; - }>; - finalResult: boolean; +export interface MethodValidationResult { + methodName: string; + status: VerificationStatus; + score: number; confidence: number; - chainIntegrity: boolean; + details: Record; + evidence: VerificationEvidence[]; } -export interface CryptographicEvidence { - evidenceId: string; - type: 'SIGNATURE' | 'HASH' | 'PROOF' | 'CERTIFICATE'; - data: string; - algorithm: string; - timestamp: Date; - verifiable: boolean; - relatedAgents: string[]; -} +// ===== INTEGRATION TESTING ===== -export interface SecureCommunicationChannel { - channelId: string; - participants: string[]; - encryptionKey: string; - established: Date; - lastActivity: Date; - messageCount: number; - integrityVerified: boolean; -} - -export interface FraudDetectionResult { - scanId: string; - timestamp: Date; - agentId: string; - suspiciousActivities: Array<{ - activity: string; - riskScore: number; - indicators: string[]; - }>; - overallRiskScore: number; - recommendedActions: string[]; - escalationRequired: boolean; -} - -export interface BackupSecurityState { - backupId: string; - timestamp: Date; - securityHash: string; - agentStates: Map; - auditTrail: any[]; - cryptographicKeys: Map; - configurationSnapshot: any; - integrityProof: string; -} - -export interface SecurityPolicy { - policyId: string; +export interface IntegrationTestConfig { + /** Test identification */ + id: string; name: string; description: string; - enforcementLevel: 'ADVISORY' | 'MANDATORY' | 'CRITICAL'; - rules: Array<{ - ruleId: string; - condition: string; - action: string; - parameters: any; - }>; - applicableAgents: string[]; - createdAt: Date; - lastModified: Date; - active: boolean; + + /** Test scope */ + agents: AgentId[]; + tasks: TaskId[]; + swarms: SwarmId[]; + + /** Test scenarios */ + scenarios: TestScenario[]; + + /** Execution settings */ + parallel: boolean; + timeout: number; + retryAttempts: number; + + /** Environment settings */ + environment: TestEnvironment; + + /** Validation criteria */ + validationCriteria: ValidationCriteria; } -// Utility types for enhanced type safety -export type SecurityOperation = - | 'REGISTER_AGENT' - | 'AUTHENTICATE_AGENT' - | 'VERIFY_TRUTH_CLAIM' - | 'SIGN_RESULT' - | 'AUDIT_TRAIL' - | 'RATE_LIMIT_CHECK' - | 'BYZANTINE_DETECTION' - | 'CONSENSUS_PARTICIPATION' - | 'EMERGENCY_SHUTDOWN' - | 'BACKUP_CREATION' - | 'RESTORE_STATE'; - -export type ThreatVector = - | 'SYBIL_ATTACK' - | 'BYZANTINE_ATTACK' - | 'ECLIPSE_ATTACK' - | 'DOS_ATTACK' - | 'REPLAY_ATTACK' - | 'MAN_IN_MIDDLE' - | 'SIGNATURE_FORGERY' - | 'COLLUSION_ATTACK' - | 'TIMING_ATTACK' - | 'SOCIAL_ENGINEERING'; - -export type CryptographicPrimitive = - | 'RSA_SIGNATURE' - | 'ECDSA_SIGNATURE' - | 'THRESHOLD_SIGNATURE' - | 'ZERO_KNOWLEDGE_PROOF' - | 'HASH_FUNCTION' - | 'SYMMETRIC_ENCRYPTION' - | 'ASYMMETRIC_ENCRYPTION' - | 'MERKLE_TREE' - | 'COMMITMENT_SCHEME' - | 'SECRET_SHARING'; - -export type AuditEventType = - | 'SYSTEM_START' - | 'SYSTEM_SHUTDOWN' - | 'AGENT_REGISTRATION' - | 'AGENT_DEREGISTRATION' - | 'VERIFICATION_REQUEST' - | 'VERIFICATION_RESULT' - | 'SIGNATURE_CREATION' - | 'SIGNATURE_VERIFICATION' - | 'CONSENSUS_ROUND' - | 'THREAT_DETECTED' - | 'POLICY_VIOLATION' - | 'EMERGENCY_ACTION'; - -// Generic interfaces for extensibility -export interface SecurityExtension { +export interface TestScenario { + id: string; name: string; + description: string; + + /** Test steps */ + steps: TestStep[]; + + /** Setup and teardown */ + setup?: TestStep[]; + teardown?: TestStep[]; + + /** Dependencies */ + dependencies: string[]; + + /** Expected outcomes */ + expectedResults: ExpectedResult[]; + + /** Resource requirements */ + resources: ResourceRequirements; +} + +export interface TestStep { + id: string; + name: string; + type: 'agent_action' | 'system_action' | 'validation' | 'wait' | 'custom'; + + /** Step configuration */ + config: Record; + params: Record; + + /** Execution settings */ + timeout: number; + retryAttempts: number; + continueOnError: boolean; + + /** Validation */ + validators: TestValidator[]; + assertions: TestAssertion[]; +} + +export interface TestValidator { + type: string; + config: Record; + weight: number; + required: boolean; +} + +export interface TestAssertion { + field: string; + operator: string; + expected: unknown; + actual?: unknown; + passed?: boolean; + message?: string; +} + +export interface ExpectedResult { + type: 'agent_state' | 'task_result' | 'system_metric' | 'custom'; + criteria: Record; + tolerance: number; + required: boolean; +} + +export interface ResourceRequirements { + cpu: number; + memory: number; + disk: number; + network: number; + agents: number; + duration: number; +} + +export interface TestEnvironment { + name: string; + variables: Record; + services: ServiceConfig[]; + constraints: EnvironmentConstraint[]; +} + +export interface ServiceConfig { + name: string; + type: string; + config: Record; + healthCheck: HealthCheck; +} + +export interface HealthCheck { + type: 'http' | 'tcp' | 'exec' | 'custom'; + config: Record; + interval: number; + timeout: number; + retries: number; +} + +export interface EnvironmentConstraint { + type: string; + value: unknown; + description: string; +} + +export interface ValidationCriteria { + /** Performance criteria */ + maxExecutionTime: number; + minThroughput: number; + maxErrorRate: number; + + /** Quality criteria */ + minAccuracy: number; + minReliability: number; + minConsistency: number; + + /** Resource criteria */ + maxResourceUsage: ResourceUsage; + + /** Custom criteria */ + custom: Record; +} + +export interface IntegrationTestResult { + /** Test identification */ + testId: string; + timestamp: Date; + + /** Overall result */ + status: VerificationStatus; + passed: boolean; + score: number; + + /** Scenario results */ + scenarioResults: ScenarioResult[]; + + /** Performance metrics */ + duration: number; + resourceUsage: ResourceUsage; + + /** Quality metrics */ + coverage: TestCoverage; + reliability: number; + + /** Evidence and artifacts */ + evidence: VerificationEvidence[]; + artifacts: Record; + logs: TestLog[]; + + /** Error information */ + errors: VerificationError[]; + warnings: VerificationWarning[]; + + /** Analysis and recommendations */ + analysis: TestAnalysis; + recommendations: string[]; +} + +export interface ScenarioResult { + scenarioId: string; + status: VerificationStatus; + passed: boolean; + score: number; + duration: number; + + stepResults: StepResult[]; + validationResults: ValidationResult[]; + + evidence: VerificationEvidence[]; + artifacts: Record; + + errors: VerificationError[]; + warnings: VerificationWarning[]; +} + +export interface StepResult { + stepId: string; + status: VerificationStatus; + passed: boolean; + duration: number; + + validatorResults: ValidatorResult[]; + assertionResults: AssertionResult[]; + + output: Record; + evidence: VerificationEvidence[]; +} + +export interface AssertionResult { + field: string; + operator: string; + expected: unknown; + actual: unknown; + passed: boolean; + message: string; + timestamp: Date; +} + +export interface ValidationResult { + type: string; + passed: boolean; + score: number; + details: Record; + evidence: VerificationEvidence[]; +} + +export interface TestCoverage { + agents: number; + tasks: number; + scenarios: number; + steps: number; + assertions: number; + percentage: number; +} + +export interface TestLog { + level: 'debug' | 'info' | 'warn' | 'error'; + message: string; + timestamp: Date; + source: string; + context: Record; +} + +export interface TestAnalysis { + bottlenecks: PerformanceBottleneck[]; + patterns: TestPattern[]; + insights: TestInsight[]; + trends: TestTrend[]; +} + +export interface PerformanceBottleneck { + type: string; + location: string; + impact: number; + description: string; + recommendations: string[]; +} + +export interface TestPattern { + type: string; + pattern: string; + frequency: number; + significance: number; + examples: string[]; +} + +export interface TestInsight { + category: string; + insight: string; + confidence: number; + evidence: string[]; + implications: string[]; +} + +export interface TestTrend { + metric: string; + direction: 'increasing' | 'decreasing' | 'stable' | 'volatile'; + magnitude: number; + significance: number; + period: string; +} + +// ===== STATE SNAPSHOT SYSTEM ===== + +export interface StateSnapshotConfig { + /** Snapshot identification */ + id: string; + name: string; + description: string; + + /** Snapshot scope */ + scope: SnapshotScope; + + /** Storage settings */ + storage: SnapshotStorageConfig; + + /** Retention settings */ + retention: SnapshotRetentionConfig; + + /** Compression settings */ + compression: SnapshotCompressionConfig; + + /** Encryption settings */ + encryption?: SnapshotEncryptionConfig; +} + +export interface SnapshotScope { + /** Include agent states */ + includeAgents: boolean; + agentFilter?: AgentFilter; + + /** Include task states */ + includeTasks: boolean; + taskFilter?: TaskFilter; + + /** Include swarm states */ + includeSwarms: boolean; + swarmFilter?: SwarmFilter; + + /** Include system state */ + includeSystem: boolean; + systemComponents: string[]; + + /** Include memory state */ + includeMemory: boolean; + memoryPartitions: string[]; + + /** Custom inclusions */ + custom: Record; +} + +export interface AgentFilter { + types: string[]; + statuses: string[]; + tags: string[]; + exclude: AgentId[]; +} + +export interface TaskFilter { + types: string[]; + statuses: string[]; + priorities: string[]; + exclude: TaskId[]; +} + +export interface SwarmFilter { + modes: string[]; + strategies: string[]; + statuses: string[]; + exclude: SwarmId[]; +} + +export interface SnapshotStorageConfig { + type: 'local' | 'remote' | 'distributed' | 'cloud'; + location: string; + redundancy: number; + checksum: boolean; + versioning: boolean; +} + +export interface SnapshotRetentionConfig { + maxSnapshots: number; + maxAge: number; + strategy: 'fifo' | 'lru' | 'importance' | 'custom'; + archiveOld: boolean; + archiveLocation?: string; +} + +export interface SnapshotCompressionConfig { + enabled: boolean; + algorithm: 'gzip' | 'brotli' | 'lz4' | 'zstd'; + level: number; + threshold: number; +} + +export interface SnapshotEncryptionConfig { + enabled: boolean; + algorithm: 'aes-256-gcm' | 'chacha20-poly1305' | 'custom'; + keyDerivation: 'pbkdf2' | 'scrypt' | 'argon2'; + saltLength: number; +} + +export interface StateSnapshot { + /** Snapshot identification */ + id: string; + name: string; + description: string; + + /** Snapshot metadata */ + timestamp: Date; version: string; - initialize(): Promise; - process(input: T): Promise; - cleanup(): Promise; + checksum: string; + + /** Snapshot scope */ + scope: SnapshotScope; + + /** Snapshot data */ + agentStates: Record; + taskStates: Record; + swarmStates: Record; + systemState: SystemState; + memoryState: MemoryState; + customState: Record; + + /** Snapshot statistics */ + statistics: SnapshotStatistics; + + /** Storage information */ + storage: SnapshotStorageInfo; + + /** Validation information */ + validation: SnapshotValidation; } -export interface SecurityMiddleware { +export interface SwarmState { + id: SwarmId; + status: string; + progress: Record; + metrics: Record; + configuration: Record; + timestamp: Date; +} + +export interface SystemState { + timestamp: Date; + version: string; + uptime: number; + resources: ResourceUsage; + services: ServiceState[]; + configuration: Record; + health: HealthState; +} + +export interface ServiceState { name: string; - priority: number; - beforeVerification?(request: any): Promise; - afterVerification?(result: any): Promise; - onError?(error: Error): Promise; + status: string; + health: string; + metrics: Record; + configuration: Record; + timestamp: Date; } -export interface SecurityMetricsCollector { - collect(): Promise>; - reset(): Promise; - export(format: 'json' | 'csv' | 'prometheus'): Promise; +export interface HealthState { + overall: string; + components: Record; + checks: HealthCheckResult[]; + timestamp: Date; } -// Constants for security configuration -export const SECURITY_CONSTANTS = { - DEFAULT_KEY_SIZE: 4096, - DEFAULT_HASH_ALGORITHM: 'sha256', - DEFAULT_SIGNATURE_ALGORITHM: 'rsa', - DEFAULT_ENCRYPTION_ALGORITHM: 'aes-256-gcm', - MINIMUM_CONSENSUS_THRESHOLD: 0.67, - MAXIMUM_BYZANTINE_TOLERANCE: 0.33, - DEFAULT_RATE_LIMITS: { - perSecond: 10, - perMinute: 100, - perHour: 1000, - perDay: 10000 - }, - REPUTATION_THRESHOLDS: { - MINIMUM_FOR_VERIFICATION: 50, - MINIMUM_FOR_CONSENSUS: 70, - MAXIMUM_REPUTATION: 100, - MINIMUM_REPUTATION: 0 - }, - AUDIT_RETENTION: { - DAYS: 365, - MAX_ENTRIES: 1000000 - } +export interface HealthCheckResult { + name: string; + status: string; + message: string; + duration: number; + timestamp: Date; +} + +export interface MemoryState { + timestamp: Date; + partitions: Record; + statistics: MemoryStatistics; + integrity: MemoryIntegrity; +} + +export interface MemoryPartitionState { + id: string; + type: string; + entries: number; + size: number; + lastModified: Date; + checksum: string; +} + +export interface MemoryStatistics { + totalEntries: number; + totalSize: number; + utilizationRate: number; + accessPatterns: Record; + performance: Record; +} + +export interface MemoryIntegrity { + checksumValid: boolean; + corruptedEntries: string[]; + missingReferences: string[]; + inconsistencies: string[]; +} + +export interface SnapshotStatistics { + totalSize: number; + compressedSize: number; + compressionRatio: number; + entryCount: Record; + captureTime: number; + validationTime: number; +} + +export interface SnapshotStorageInfo { + type: string; + location: string; + size: number; + checksum: string; + encrypted: boolean; + redundancy: number; + accessibility: string; +} + +export interface SnapshotValidation { + checksumValid: boolean; + structureValid: boolean; + dataIntegrity: boolean; + versionCompatible: boolean; + errors: string[]; + warnings: string[]; + timestamp: Date; +} + +export interface RollbackConfig { + /** Rollback identification */ + id: string; + snapshotId: string; + reason: string; + + /** Rollback scope */ + scope: RollbackScope; + + /** Rollback strategy */ + strategy: RollbackStrategy; + + /** Safety settings */ + safety: RollbackSafetyConfig; + + /** Notification settings */ + notifications: NotificationConfig[]; +} + +export interface RollbackScope { + includeAgents: boolean; + includeTasks: boolean; + includeSwarms: boolean; + includeSystem: boolean; + includeMemory: boolean; + + /** Partial rollback filters */ + agentFilter?: AgentFilter; + taskFilter?: TaskFilter; + swarmFilter?: SwarmFilter; + + /** Exclude from rollback */ + preserveData: string[]; +} + +export interface RollbackStrategy { + type: 'immediate' | 'gradual' | 'selective' | 'custom'; + stopRunningTasks: boolean; + backupCurrent: boolean; + validateBefore: boolean; + validateAfter: boolean; + rollbackTimeout: number; +} + +export interface RollbackSafetyConfig { + requireConfirmation: boolean; + maxRollbackTime: number; + emergencyStop: boolean; + backupRequired: boolean; + validationRequired: boolean; + approvalRequired: boolean; +} + +export interface RollbackResult { + /** Rollback identification */ + id: string; + snapshotId: string; + timestamp: Date; + + /** Result status */ + status: 'completed' | 'partial' | 'failed' | 'cancelled'; + success: boolean; + + /** Rollback details */ + scope: RollbackScope; + duration: number; + + /** Component results */ + componentResults: ComponentRollbackResult[]; + + /** Validation results */ + validation: RollbackValidation; + + /** Data preservation */ + preservedData: string[]; + lostData: string[]; + + /** Backup information */ + backupSnapshot?: string; + + /** Error information */ + errors: VerificationError[]; + warnings: VerificationWarning[]; + + /** Recovery information */ + recoveryActions: string[]; + nextSteps: string[]; +} + +export interface ComponentRollbackResult { + component: string; + status: 'completed' | 'failed' | 'skipped'; + duration: number; + itemsRolledBack: number; + itemsFailed: number; + errors: string[]; +} + +export interface RollbackValidation { + preValidation: ValidationResult; + postValidation: ValidationResult; + dataIntegrity: boolean; + systemHealth: boolean; + functionalityPreserved: boolean; +} + +// ===== UTILITY TYPES ===== + +export type VerificationCallback = (result: VerificationResult) => void | Promise; +export type CheckpointCallback = (result: CheckpointResult) => void | Promise; +export type ClaimValidationCallback = (result: ClaimValidationResult) => void | Promise; +export type TestCompletionCallback = (result: IntegrationTestResult) => void | Promise; +export type SnapshotCallback = (snapshot: StateSnapshot) => void | Promise; +export type RollbackCallback = (result: RollbackResult) => void | Promise; + +// ===== TYPE GUARDS ===== + +export function isTruthScore(obj: unknown): obj is TruthScore { + return ( + typeof obj === 'object' && + obj !== null && + typeof (obj as TruthScore).score === 'number' && + typeof (obj as TruthScore).components === 'object' && + (obj as TruthScore).timestamp instanceof Date + ); +} + +export function isVerificationResult(obj: unknown): obj is VerificationResult { + return ( + typeof obj === 'object' && + obj !== null && + typeof (obj as VerificationResult).id === 'string' && + typeof (obj as VerificationResult).status === 'string' && + typeof (obj as VerificationResult).passed === 'boolean' + ); +} + +export function isAgentClaim(obj: unknown): obj is AgentClaim { + return ( + typeof obj === 'object' && + obj !== null && + typeof (obj as AgentClaim).id === 'string' && + typeof (obj as AgentClaim).type === 'string' && + (obj as AgentClaim).submittedAt instanceof Date + ); +} + +export function isStateSnapshot(obj: unknown): obj is StateSnapshot { + return ( + typeof obj === 'object' && + obj !== null && + typeof (obj as StateSnapshot).id === 'string' && + (obj as StateSnapshot).timestamp instanceof Date && + typeof (obj as StateSnapshot).checksum === 'string' + ); +} + +// ===== CONSTANTS ===== + +export const VERIFICATION_CONSTANTS = { + // Default thresholds + DEFAULT_TRUTH_THRESHOLD: 0.95, + DEFAULT_CONFIDENCE_LEVEL: 0.95, + DEFAULT_MIN_SAMPLE_SIZE: 30, + DEFAULT_MAX_ERROR_MARGIN: 0.05, + + // Timeouts + DEFAULT_VERIFICATION_TIMEOUT: 5 * 60 * 1000, // 5 minutes + DEFAULT_CHECKPOINT_TIMEOUT: 2 * 60 * 1000, // 2 minutes + DEFAULT_CLAIM_VALIDATION_TIMEOUT: 1 * 60 * 1000, // 1 minute + DEFAULT_TEST_TIMEOUT: 10 * 60 * 1000, // 10 minutes + DEFAULT_ROLLBACK_TIMEOUT: 3 * 60 * 1000, // 3 minutes + + // Retry limits + DEFAULT_RETRY_ATTEMPTS: 3, + MAX_RETRY_ATTEMPTS: 10, + + // Snapshot limits + DEFAULT_MAX_SNAPSHOTS: 100, + DEFAULT_SNAPSHOT_RETENTION_DAYS: 30, + DEFAULT_COMPRESSION_THRESHOLD: 1024 * 1024, // 1MB + + // Quality thresholds + MIN_TRUTH_SCORE: 0.0, + MAX_TRUTH_SCORE: 1.0, + HIGH_CONFIDENCE_THRESHOLD: 0.9, + MEDIUM_CONFIDENCE_THRESHOLD: 0.7, + LOW_CONFIDENCE_THRESHOLD: 0.5, + + // Resource limits + MAX_VERIFICATION_MEMORY: 512 * 1024 * 1024, // 512MB + MAX_SNAPSHOT_SIZE: 1024 * 1024 * 1024, // 1GB + MAX_TEST_DURATION: 60 * 60 * 1000, // 1 hour } as const; -// Error types for better error handling -export class SecurityError extends Error { - constructor( - message: string, - public code: string, - public agentId?: string, - public details?: any - ) { - super(message); - this.name = 'SecurityError'; - } -} - -export class AuthenticationError extends SecurityError { - constructor(message: string, agentId?: string, details?: any) { - super(message, 'AUTH_ERROR', agentId, details); - this.name = 'AuthenticationError'; - } -} - -export class ByzantineError extends SecurityError { - constructor(message: string, agentId: string, details?: any) { - super(message, 'BYZANTINE_ERROR', agentId, details); - this.name = 'ByzantineError'; - } -} - -export class CryptographicError extends SecurityError { - constructor(message: string, details?: any) { - super(message, 'CRYPTO_ERROR', undefined, details); - this.name = 'CryptographicError'; - } -} - -export class RateLimitError extends SecurityError { - constructor(message: string, agentId: string, retryAfter?: number) { - super(message, 'RATE_LIMIT_ERROR', agentId, { retryAfter }); - this.name = 'RateLimitError'; - } -} \ No newline at end of file +export default { + VERIFICATION_CONSTANTS, + isTruthScore, + isVerificationResult, + isAgentClaim, + isStateSnapshot, +}; \ No newline at end of file