Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /microsoft_agent-governance-toolkit /agent-governance-python /agent-os /examples /legal-review /main.py
| # Copyright (c) Microsoft Corporation. | |
| # Licensed under the MIT License. | |
| """ | |
| Contract Analysis Agent - Legal Review | |
| ====================================== | |
| Production-grade AI agent for analyzing legal contracts with governance. | |
| Features: | |
| - Attorney-client privilege protection | |
| - Risky clause detection (unlimited liability, non-competes, IP) | |
| - Verification for legal accuracy | |
| - Conflict of interest checking | |
| - Tamper-evident audit logging | |
| - PII redaction in outputs | |
| - Multi-jurisdiction support | |
| Benchmarkable: "Analyzed 500 contracts, flagged 847 risky clauses, 0 privilege breaches" | |
| """ | |
| import asyncio | |
| import hashlib | |
| import json | |
| import re | |
| import time | |
| from dataclasses import dataclass, field | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from typing import Optional | |
| from collections import defaultdict | |
| import uuid | |
| # ============================================================ | |
| # CONFIGURATION | |
| # ============================================================ | |
| class RiskLevel(Enum): | |
| CRITICAL = "critical" | |
| HIGH = "high" | |
| MEDIUM = "medium" | |
| LOW = "low" | |
| INFO = "info" | |
| class ClauseType(Enum): | |
| INDEMNIFICATION = "indemnification" | |
| LIABILITY = "liability_limitation" | |
| IP_ASSIGNMENT = "ip_assignment" | |
| NON_COMPETE = "non_compete" | |
| TERMINATION = "termination" | |
| CONFIDENTIALITY = "confidentiality" | |
| GOVERNING_LAW = "governing_law" | |
| ARBITRATION = "arbitration" | |
| PAYMENT = "payment_terms" | |
| WARRANTY = "warranty" | |
| FORCE_MAJEURE = "force_majeure" | |
| DATA_PROTECTION = "data_protection" | |
| class PrivilegeLevel(Enum): | |
| PUBLIC = "public" | |
| CONFIDENTIAL = "confidential" | |
| PRIVILEGED = "privileged" # Attorney-client | |
| WORK_PRODUCT = "work_product" | |
| # Risky clause patterns | |
| RISKY_CLAUSE_PATTERNS = { | |
| ClauseType.INDEMNIFICATION: { | |
| "patterns": [ | |
| r"indemnif\w+", | |
| r"hold\s+harmless", | |
| r"defend\s+and\s+indemnify", | |
| ], | |
| "risk_indicators": [ | |
| (r"unlimited\s+indemnif", RiskLevel.CRITICAL, "Unlimited indemnification"), | |
| (r"indemnif\w+.*third\s+part\w+.*claim", RiskLevel.HIGH, "Third-party indemnification"), | |
| (r"sole\s+negligence", RiskLevel.HIGH, "Indemnification for sole negligence"), | |
| ] | |
| }, | |
| ClauseType.LIABILITY: { | |
| "patterns": [ | |
| r"limitation\s+of\s+liability", | |
| r"liability\s+cap", | |
| r"maximum\s+liability", | |
| r"consequential\s+damages", | |
| ], | |
| "risk_indicators": [ | |
| (r"no\s+limitation", RiskLevel.CRITICAL, "No liability cap"), | |
| (r"waive\w*\s+consequential", RiskLevel.MEDIUM, "Consequential damages waiver"), | |
| (r"cap.*less\s+than.*fees", RiskLevel.HIGH, "Liability cap below contract value"), | |
| ] | |
| }, | |
| ClauseType.IP_ASSIGNMENT: { | |
| "patterns": [ | |
| r"intellectual\s+property", | |
| r"ip\s+assignment", | |
| r"work\s+product", | |
| r"invention\s+assignment", | |
| ], | |
| "risk_indicators": [ | |
| (r"all\s+ip.*assign", RiskLevel.HIGH, "Broad IP assignment"), | |
| (r"prior\s+invention.*exclude", RiskLevel.MEDIUM, "Prior inventions carve-out needed"), | |
| (r"work\s+for\s+hire", RiskLevel.MEDIUM, "Work for hire classification"), | |
| ] | |
| }, | |
| ClauseType.NON_COMPETE: { | |
| "patterns": [ | |
| r"non-?compet\w+", | |
| r"restrictive\s+covenant", | |
| r"not\s+engage\s+in.*business", | |
| ], | |
| "risk_indicators": [ | |
| (r"non-?compet\w+.*(\d+)\s*year", RiskLevel.HIGH, "Non-compete duration"), | |
| (r"worldwide", RiskLevel.CRITICAL, "Worldwide geographic scope"), | |
| (r"perpetual", RiskLevel.CRITICAL, "Perpetual restriction"), | |
| ] | |
| }, | |
| ClauseType.TERMINATION: { | |
| "patterns": [ | |
| r"terminat\w+", | |
| r"cancel\w+.*agreement", | |
| r"right\s+to\s+end", | |
| ], | |
| "risk_indicators": [ | |
| (r"terminat\w+.*without\s+cause", RiskLevel.MEDIUM, "Termination without cause"), | |
| (r"immediate.*terminat", RiskLevel.HIGH, "Immediate termination right"), | |
| (r"no\s+refund.*terminat", RiskLevel.MEDIUM, "No refund on termination"), | |
| ] | |
| }, | |
| ClauseType.ARBITRATION: { | |
| "patterns": [ | |
| r"arbitrat\w+", | |
| r"dispute\s+resolution", | |
| r"binding.*mediat", | |
| ], | |
| "risk_indicators": [ | |
| (r"waive.*jury", RiskLevel.HIGH, "Jury trial waiver"), | |
| (r"class\s+action.*waiv", RiskLevel.HIGH, "Class action waiver"), | |
| (r"arbitrat\w+.*their.*choice", RiskLevel.MEDIUM, "One-sided arbitration selection"), | |
| ] | |
| }, | |
| ClauseType.GOVERNING_LAW: { | |
| "patterns": [ | |
| r"governing\s+law", | |
| r"jurisdiction", | |
| r"laws\s+of\s+the\s+state", | |
| ], | |
| "risk_indicators": [ | |
| (r"exclusive\s+jurisdiction", RiskLevel.MEDIUM, "Exclusive jurisdiction clause"), | |
| (r"foreign.*jurisdiction", RiskLevel.HIGH, "Foreign jurisdiction"), | |
| ] | |
| }, | |
| } | |
| # PII patterns for redaction | |
| PII_PATTERNS = { | |
| "ssn": r"\b\d{3}-\d{2}-\d{4}\b", | |
| "phone": r"\b(?:\+1[-.]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b", | |
| "email": r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", | |
| "address": r"\d+\s+[\w\s]+(?:Street|St|Avenue|Ave|Road|Rd|Boulevard|Blvd|Drive|Dr|Lane|Ln)", | |
| "credit_card": r"\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b", | |
| "bank_account": r"\b(?:account|routing)[\s#:]*\d{8,17}\b", | |
| } | |
| # ============================================================ | |
| # DATA MODELS | |
| # ============================================================ | |
| class User: | |
| """System user.""" | |
| user_id: str | |
| name: str | |
| role: str # attorney, paralegal, client, admin | |
| bar_number: Optional[str] = None # For attorneys | |
| firm: Optional[str] = None | |
| class Matter: | |
| """Legal matter/case.""" | |
| matter_id: str | |
| client_name: str | |
| matter_type: str # contract_review, litigation, transaction | |
| authorized_users: list[str] = field(default_factory=list) | |
| conflicts_cleared: bool = False | |
| opposing_parties: list[str] = field(default_factory=list) | |
| privilege_level: PrivilegeLevel = PrivilegeLevel.CONFIDENTIAL | |
| class Contract: | |
| """Contract document.""" | |
| doc_id: str | |
| matter_id: str | |
| title: str | |
| content: str | |
| contract_type: str # nda, msa, employment, license, etc. | |
| parties: list[str] = field(default_factory=list) | |
| effective_date: Optional[str] = None | |
| expiration_date: Optional[str] = None | |
| privilege_level: PrivilegeLevel = PrivilegeLevel.CONFIDENTIAL | |
| class ClauseFinding: | |
| """Individual clause analysis finding.""" | |
| finding_id: str | |
| clause_type: ClauseType | |
| risk_level: RiskLevel | |
| text_excerpt: str | |
| description: str | |
| recommendation: str | |
| confidence: float | |
| location: str # Section reference | |
| class ContractReview: | |
| """Complete contract review result.""" | |
| review_id: str | |
| doc_id: str | |
| matter_id: str | |
| reviewer_id: str | |
| timestamp: datetime | |
| overall_risk: RiskLevel | |
| findings: list[ClauseFinding] | |
| summary: str | |
| word_count: int | |
| estimated_value: Optional[float] = None | |
| class AuditEntry: | |
| """Audit log entry.""" | |
| entry_id: str | |
| timestamp: datetime | |
| user_id: str | |
| action: str | |
| resource_id: str | |
| resource_type: str | |
| outcome: str | |
| details: dict = field(default_factory=dict) | |
| hash: Optional[str] = None | |
| # ============================================================ | |
| # AUDIT LOGGING | |
| # ============================================================ | |
| class LegalAuditLog: | |
| """Tamper-evident audit logging for legal compliance.""" | |
| def __init__(self): | |
| self.entries: list[AuditEntry] = [] | |
| self.previous_hash: str = "GENESIS" | |
| def log( | |
| self, | |
| user_id: str, | |
| action: str, | |
| resource_id: str, | |
| resource_type: str, | |
| outcome: str, | |
| details: dict = None | |
| ) -> str: | |
| """Create audit entry with hash chain.""" | |
| entry = AuditEntry( | |
| entry_id=str(uuid.uuid4())[:8], | |
| timestamp=datetime.now(timezone.utc), | |
| user_id=user_id, | |
| action=action, | |
| resource_id=resource_id, | |
| resource_type=resource_type, | |
| outcome=outcome, | |
| details=details or {}, | |
| ) | |
| # Hash chain for tamper evidence | |
| hash_input = f"{self.previous_hash}|{entry.timestamp}|{entry.user_id}|{entry.action}" | |
| entry.hash = hashlib.sha256(hash_input.encode()).hexdigest()[:16] | |
| self.previous_hash = entry.hash | |
| self.entries.append(entry) | |
| return entry.entry_id | |
| def get_document_history(self, doc_id: str) -> list[AuditEntry]: | |
| """Get all access to a document.""" | |
| return [e for e in self.entries if e.resource_id == doc_id] | |
| # ============================================================ | |
| # CONFLICT CHECKER | |
| # ============================================================ | |
| class ConflictChecker: | |
| """Check for conflicts of interest.""" | |
| def __init__(self): | |
| self.conflicts: set[str] = set() | |
| self.matter_parties: dict[str, set] = defaultdict(set) | |
| def add_conflict(self, party: str): | |
| """Add a party that creates a conflict.""" | |
| self.conflicts.add(party.lower()) | |
| def register_matter_parties(self, matter_id: str, parties: list[str]): | |
| """Register parties involved in a matter.""" | |
| for party in parties: | |
| self.matter_parties[matter_id].add(party.lower()) | |
| def check_conflict(self, parties: list[str]) -> tuple[bool, Optional[str]]: | |
| """Check if any party creates a conflict.""" | |
| for party in parties: | |
| if party.lower() in self.conflicts: | |
| return True, party | |
| return False, None | |
| def check_matter_conflict(self, matter: Matter) -> tuple[bool, Optional[str]]: | |
| """Check if matter has conflict with existing matters.""" | |
| parties_to_check = [matter.client_name] + matter.opposing_parties | |
| return self.check_conflict(parties_to_check) | |
| # ============================================================ | |
| # REDACTION ENGINE | |
| # ============================================================ | |
| class RedactionEngine: | |
| """Redact sensitive information from outputs.""" | |
| def __init__(self): | |
| self.patterns = {k: re.compile(v, re.IGNORECASE) for k, v in PII_PATTERNS.items()} | |
| def redact(self, text: str) -> str: | |
| """Redact all PII from text.""" | |
| result = text | |
| for pii_type, pattern in self.patterns.items(): | |
| result = pattern.sub(f"[{pii_type.upper()}_REDACTED]", result) | |
| return result | |
| def detect_pii(self, text: str) -> list[dict]: | |
| """Detect PII in text.""" | |
| findings = [] | |
| for pii_type, pattern in self.patterns.items(): | |
| for match in pattern.finditer(text): | |
| findings.append({ | |
| "type": pii_type, | |
| "position": match.span() | |
| }) | |
| return findings | |
| # ============================================================ | |
| # CLAUSE ANALYZER | |
| # ============================================================ | |
| class ClauseAnalyzer: | |
| """Analyze contract clauses for risk.""" | |
| def __init__(self): | |
| self.compiled_patterns = {} | |
| for clause_type, config in RISKY_CLAUSE_PATTERNS.items(): | |
| self.compiled_patterns[clause_type] = { | |
| "patterns": [re.compile(p, re.IGNORECASE) for p in config["patterns"]], | |
| "risk_indicators": [ | |
| (re.compile(p, re.IGNORECASE), level, desc) | |
| for p, level, desc in config["risk_indicators"] | |
| ] | |
| } | |
| def analyze(self, content: str) -> list[ClauseFinding]: | |
| """Analyze contract content for risky clauses.""" | |
| findings = [] | |
| content_lower = content.lower() | |
| # Split into sections for location tracking | |
| sections = self._split_sections(content) | |
| for clause_type, config in self.compiled_patterns.items(): | |
| # Check if clause type is present | |
| for pattern in config["patterns"]: | |
| for match in pattern.finditer(content): | |
| # Found a clause of this type, check risk indicators | |
| context = self._get_context(content, match.start(), 500) | |
| for risk_pattern, risk_level, description in config["risk_indicators"]: | |
| if risk_pattern.search(context): | |
| finding = ClauseFinding( | |
| finding_id=str(uuid.uuid4())[:8], | |
| clause_type=clause_type, | |
| risk_level=risk_level, | |
| text_excerpt=context[:200] + "...", | |
| description=description, | |
| recommendation=self._get_recommendation(clause_type, risk_level), | |
| confidence=0.85, | |
| location=self._find_section(sections, match.start()), | |
| ) | |
| findings.append(finding) | |
| break # One finding per clause match | |
| return findings | |
| def _split_sections(self, content: str) -> list[tuple[int, str]]: | |
| """Split content into sections.""" | |
| section_pattern = re.compile(r'(?:Section|Article|§)\s*(\d+(?:\.\d+)?)', re.IGNORECASE) | |
| sections = [] | |
| for match in section_pattern.finditer(content): | |
| sections.append((match.start(), match.group(1))) | |
| return sections | |
| def _find_section(self, sections: list, position: int) -> str: | |
| """Find which section a position falls in.""" | |
| for i, (start, section_num) in enumerate(sections): | |
| next_start = sections[i+1][0] if i+1 < len(sections) else float('inf') | |
| if start <= position < next_start: | |
| return f"Section {section_num}" | |
| return "Unknown Section" | |
| def _get_context(self, content: str, position: int, window: int) -> str: | |
| """Get context around a position.""" | |
| start = max(0, position - window // 2) | |
| end = min(len(content), position + window // 2) | |
| return content[start:end] | |
| def _get_recommendation(self, clause_type: ClauseType, risk_level: RiskLevel) -> str: | |
| """Get recommendation based on clause type and risk.""" | |
| recommendations = { | |
| ClauseType.INDEMNIFICATION: { | |
| RiskLevel.CRITICAL: "Remove unlimited indemnification or add cap equal to contract value", | |
| RiskLevel.HIGH: "Negotiate mutual indemnification with reasonable caps", | |
| RiskLevel.MEDIUM: "Review indemnification triggers and ensure insurance coverage", | |
| }, | |
| ClauseType.LIABILITY: { | |
| RiskLevel.CRITICAL: "Add liability cap - recommend 2x annual contract value", | |
| RiskLevel.HIGH: "Negotiate higher liability cap or carve-outs for willful misconduct", | |
| RiskLevel.MEDIUM: "Review consequential damages waiver scope", | |
| }, | |
| ClauseType.IP_ASSIGNMENT: { | |
| RiskLevel.CRITICAL: "Narrow IP assignment to deliverables only", | |
| RiskLevel.HIGH: "Add prior inventions exclusion schedule", | |
| RiskLevel.MEDIUM: "Clarify ownership of background IP", | |
| }, | |
| ClauseType.NON_COMPETE: { | |
| RiskLevel.CRITICAL: "Non-compete may be unenforceable - consider removal", | |
| RiskLevel.HIGH: "Reduce duration and geographic scope", | |
| RiskLevel.MEDIUM: "Define competitive activities narrowly", | |
| }, | |
| } | |
| return recommendations.get(clause_type, {}).get( | |
| risk_level, | |
| "Review with senior counsel" | |
| ) | |
| # ============================================================ | |
| # VERIFIER | |
| # ============================================================ | |
| class LegalVerifier: | |
| """Verification for legal analysis.""" | |
| def __init__(self): | |
| self.models = ["gpt-4-legal", "claude-3-legal", "legal-bert"] | |
| async def verify_findings( | |
| self, | |
| content: str, | |
| findings: list[ClauseFinding] | |
| ) -> tuple[float, list[ClauseFinding]]: | |
| """ | |
| Verify findings across multiple models. | |
| Returns: (agreement_score, verified_findings) | |
| """ | |
| # Simulate verification | |
| verified = [] | |
| for finding in findings: | |
| # In production, each model would analyze the clause | |
| agreement = 0.85 + (hash(finding.finding_id) % 15) / 100 | |
| if agreement >= 0.7: | |
| finding.confidence = agreement | |
| verified.append(finding) | |
| overall_agreement = sum(f.confidence for f in verified) / max(1, len(verified)) | |
| return overall_agreement, verified | |
| # ============================================================ | |
| # ACCESS CONTROL | |
| # ============================================================ | |
| class AccessController: | |
| """Attorney-client privilege protection.""" | |
| def __init__(self, audit_log: LegalAuditLog, conflict_checker: ConflictChecker): | |
| self.audit_log = audit_log | |
| self.conflict_checker = conflict_checker | |
| def check_access( | |
| self, | |
| user: User, | |
| matter: Matter, | |
| action: str = "read" | |
| ) -> tuple[bool, str]: | |
| """Check if user can access matter.""" | |
| # Check if user is authorized | |
| if user.user_id not in matter.authorized_users: | |
| self.audit_log.log( | |
| user.user_id, action, matter.matter_id, "matter", | |
| "denied", {"reason": "not_authorized"} | |
| ) | |
| return False, "User not authorized for this matter" | |
| # Check for conflicts | |
| has_conflict, party = self.conflict_checker.check_matter_conflict(matter) | |
| if has_conflict: | |
| self.audit_log.log( | |
| user.user_id, action, matter.matter_id, "matter", | |
| "denied", {"reason": "conflict", "party": party} | |
| ) | |
| return False, f"Conflict of interest: {party}" | |
| # Check privilege requirements | |
| if matter.privilege_level == PrivilegeLevel.PRIVILEGED: | |
| if user.role not in ["attorney", "paralegal"]: | |
| self.audit_log.log( | |
| user.user_id, action, matter.matter_id, "matter", | |
| "denied", {"reason": "privilege_level"} | |
| ) | |
| return False, "Only legal staff can access privileged materials" | |
| # Access granted | |
| self.audit_log.log( | |
| user.user_id, action, matter.matter_id, "matter", "granted" | |
| ) | |
| return True, "Access granted" | |
| # ============================================================ | |
| # MAIN AGENT | |
| # ============================================================ | |
| class ContractAnalysisAgent: | |
| """ | |
| Production contract analysis agent with full governance. | |
| Pipeline: | |
| 1. Check user authorization and conflicts | |
| 2. Parse contract structure | |
| 3. Analyze clauses for risk | |
| 4. Verification | |
| 5. Generate findings with redaction | |
| 6. Create audit trail | |
| """ | |
| def __init__(self, agent_id: str = "contract-agent-001"): | |
| self.agent_id = agent_id | |
| # Initialize components | |
| self.audit_log = LegalAuditLog() | |
| self.conflict_checker = ConflictChecker() | |
| self.access_controller = AccessController(self.audit_log, self.conflict_checker) | |
| self.clause_analyzer = ClauseAnalyzer() | |
| self.verifier = LegalVerifier() | |
| self.redaction_engine = RedactionEngine() | |
| # Storage | |
| self.users: dict[str, User] = {} | |
| self.matters: dict[str, Matter] = {} | |
| self.contracts: dict[str, Contract] = {} | |
| self.reviews: dict[str, ContractReview] = {} | |
| # Metrics | |
| self.contracts_analyzed = 0 | |
| self.clauses_flagged = 0 | |
| self.access_denied = 0 | |
| print(f"⚖️ Contract Analysis Agent initialized") | |
| print(f" Agent ID: {agent_id}") | |
| print(f" Privilege Protection: ✓") | |
| print(f" Conflict Checking: ✓") | |
| def register_user(self, user: User): | |
| """Register a system user.""" | |
| self.users[user.user_id] = user | |
| def create_matter(self, matter: Matter) -> str: | |
| """Create a new legal matter.""" | |
| # Check for conflicts | |
| has_conflict, party = self.conflict_checker.check_matter_conflict(matter) | |
| if has_conflict: | |
| raise ValueError(f"Cannot create matter: conflict with {party}") | |
| self.matters[matter.matter_id] = matter | |
| self.conflict_checker.register_matter_parties( | |
| matter.matter_id, | |
| [matter.client_name] + matter.opposing_parties | |
| ) | |
| return matter.matter_id | |
| def add_contract(self, contract: Contract) -> str: | |
| """Add contract to a matter.""" | |
| if contract.matter_id not in self.matters: | |
| raise ValueError(f"Matter {contract.matter_id} not found") | |
| self.contracts[contract.doc_id] = contract | |
| return contract.doc_id | |
| async def analyze_contract( | |
| self, | |
| doc_id: str, | |
| user: User, | |
| include_recommendations: bool = True | |
| ) -> ContractReview: | |
| """ | |
| Analyze contract for risky clauses. | |
| """ | |
| print(f"\n{'='*60}") | |
| print(f"📄 Contract Analysis Request") | |
| print(f" Document: {doc_id}") | |
| print(f" User: {user.name} ({user.role})") | |
| # Get contract | |
| contract = self.contracts.get(doc_id) | |
| if not contract: | |
| raise ValueError(f"Contract {doc_id} not found") | |
| matter = self.matters.get(contract.matter_id) | |
| if not matter: | |
| raise ValueError(f"Matter {contract.matter_id} not found") | |
| # Check access | |
| allowed, reason = self.access_controller.check_access(user, matter, "analyze") | |
| if not allowed: | |
| print(f"❌ ACCESS DENIED: {reason}") | |
| self.access_denied += 1 | |
| raise PermissionError(reason) | |
| print(f"✅ Access granted - analyzing contract...") | |
| # Analyze clauses | |
| findings = self.clause_analyzer.analyze(contract.content) | |
| print(f"🔍 Initial scan: {len(findings)} potential issues") | |
| # Verification | |
| agreement, verified_findings = await self.verifier.verify_findings( | |
| contract.content, findings | |
| ) | |
| print(f"✓ Cross-model agreement: {agreement:.0%}") | |
| # Calculate overall risk | |
| overall_risk = self._calculate_overall_risk(verified_findings) | |
| # Create review | |
| review = ContractReview( | |
| review_id=str(uuid.uuid4())[:8], | |
| doc_id=doc_id, | |
| matter_id=contract.matter_id, | |
| reviewer_id=user.user_id, | |
| timestamp=datetime.now(timezone.utc), | |
| overall_risk=overall_risk, | |
| findings=verified_findings, | |
| summary=self._generate_summary(contract, verified_findings, overall_risk), | |
| word_count=len(contract.content.split()), | |
| ) | |
| self.reviews[review.review_id] = review | |
| self.contracts_analyzed += 1 | |
| self.clauses_flagged += len(verified_findings) | |
| # Log completion | |
| self.audit_log.log( | |
| user.user_id, "complete_analysis", doc_id, "contract", | |
| "success", {"findings": len(verified_findings), "risk": overall_risk.value} | |
| ) | |
| print(f"📊 Analysis complete: {overall_risk.value.upper()} risk") | |
| print(f" Findings: {len(verified_findings)}") | |
| return review | |
| def _calculate_overall_risk(self, findings: list[ClauseFinding]) -> RiskLevel: | |
| """Calculate overall contract risk level.""" | |
| if not findings: | |
| return RiskLevel.LOW | |
| risk_counts = defaultdict(int) | |
| for f in findings: | |
| risk_counts[f.risk_level] += 1 | |
| if risk_counts[RiskLevel.CRITICAL] > 0: | |
| return RiskLevel.CRITICAL | |
| if risk_counts[RiskLevel.HIGH] >= 2: | |
| return RiskLevel.CRITICAL | |
| if risk_counts[RiskLevel.HIGH] > 0: | |
| return RiskLevel.HIGH | |
| if risk_counts[RiskLevel.MEDIUM] >= 3: | |
| return RiskLevel.HIGH | |
| if risk_counts[RiskLevel.MEDIUM] > 0: | |
| return RiskLevel.MEDIUM | |
| return RiskLevel.LOW | |
| def _generate_summary( | |
| self, | |
| contract: Contract, | |
| findings: list[ClauseFinding], | |
| overall_risk: RiskLevel | |
| ) -> str: | |
| """Generate executive summary of review.""" | |
| critical = [f for f in findings if f.risk_level == RiskLevel.CRITICAL] | |
| high = [f for f in findings if f.risk_level == RiskLevel.HIGH] | |
| summary = f"""CONTRACT REVIEW SUMMARY | |
| ====================== | |
| Document: {contract.title} | |
| Type: {contract.contract_type.upper()} | |
| Parties: {', '.join(contract.parties)} | |
| OVERALL RISK: {overall_risk.value.upper()} | |
| KEY FINDINGS: | |
| - Total issues identified: {len(findings)} | |
| - Critical issues: {len(critical)} | |
| - High-risk issues: {len(high)} | |
| """ | |
| if critical: | |
| summary += "\n⛔ CRITICAL ISSUES (Must address before signing):\n" | |
| for f in critical: | |
| summary += f" • {f.description} ({f.location})\n" | |
| if high: | |
| summary += "\n⚠️ HIGH-RISK ISSUES (Strongly recommend addressing):\n" | |
| for f in high[:3]: # Top 3 | |
| summary += f" • {f.description} ({f.location})\n" | |
| return summary | |
| def get_review_for_client( | |
| self, | |
| review_id: str, | |
| user: User | |
| ) -> dict: | |
| """Get review with PII redacted for client sharing.""" | |
| review = self.reviews.get(review_id) | |
| if not review: | |
| return {"error": "Review not found"} | |
| # Redact PII from findings | |
| redacted_findings = [] | |
| for f in review.findings: | |
| redacted_findings.append({ | |
| "risk_level": f.risk_level.value, | |
| "clause_type": f.clause_type.value, | |
| "description": f.description, | |
| "recommendation": f.recommendation, | |
| "location": f.location, | |
| }) | |
| return { | |
| "review_id": review.review_id, | |
| "document_id": review.doc_id, | |
| "overall_risk": review.overall_risk.value, | |
| "summary": self.redaction_engine.redact(review.summary), | |
| "findings": redacted_findings, | |
| "timestamp": review.timestamp.isoformat(), | |
| } | |
| def get_metrics(self) -> dict: | |
| """Get agent metrics.""" | |
| return { | |
| "contracts_analyzed": self.contracts_analyzed, | |
| "clauses_flagged": self.clauses_flagged, | |
| "access_denied": self.access_denied, | |
| "audit_entries": len(self.audit_log.entries), | |
| } | |
| # ============================================================ | |
| # DEMO | |
| # ============================================================ | |
| async def demo(): | |
| """Demonstrate the contract analysis agent.""" | |
| print("=" * 60) | |
| print("Contract Analysis Agent - Legal Review") | |
| print("Powered by Agent OS Governance") | |
| print("=" * 60) | |
| agent = ContractAnalysisAgent() | |
| # Register users | |
| attorney = User( | |
| user_id="ATT001", | |
| name="Sarah Miller, Esq.", | |
| role="attorney", | |
| bar_number="NY123456", | |
| firm="Miller & Associates" | |
| ) | |
| paralegal = User( | |
| user_id="PAR001", | |
| name="John Davis", | |
| role="paralegal", | |
| firm="Miller & Associates" | |
| ) | |
| client = User( | |
| user_id="CLI001", | |
| name="Tech Corp CFO", | |
| role="client" | |
| ) | |
| agent.register_user(attorney) | |
| agent.register_user(paralegal) | |
| agent.register_user(client) | |
| # Create matter | |
| matter = Matter( | |
| matter_id="M-2024-001", | |
| client_name="Tech Corp", | |
| matter_type="contract_review", | |
| authorized_users=["ATT001", "PAR001", "CLI001"], | |
| conflicts_cleared=True, | |
| opposing_parties=["Vendor Inc"], | |
| privilege_level=PrivilegeLevel.CONFIDENTIAL, | |
| ) | |
| agent.create_matter(matter) | |
| # Add contract | |
| contract = Contract( | |
| doc_id="DOC-001", | |
| matter_id="M-2024-001", | |
| title="Master Services Agreement - Tech Corp & Vendor Inc", | |
| contract_type="msa", | |
| parties=["Tech Corp", "Vendor Inc"], | |
| content=""" | |
| MASTER SERVICES AGREEMENT | |
| This Agreement is entered into by Tech Corp ("Client") and Vendor Inc ("Vendor"). | |
| SECTION 1 - SERVICES | |
| Vendor shall provide software development services as described in each Statement of Work. | |
| SECTION 2 - PAYMENT TERMS | |
| Client shall pay all invoices within 30 days of receipt. | |
| SECTION 3 - INTELLECTUAL PROPERTY | |
| 3.1 All work product created under this Agreement shall be considered "work for hire" | |
| and shall be the sole property of Client. | |
| 3.2 Client receives all IP rights including patents, copyrights, and trade secrets. | |
| 3.3 Vendor assigns all intellectual property without limitation. | |
| SECTION 4 - INDEMNIFICATION | |
| 4.1 Vendor shall indemnify, defend, and hold harmless Client from any and all claims, | |
| including third party claims, arising from Vendor's services. | |
| 4.2 This indemnification shall be unlimited and shall survive termination. | |
| 4.3 Vendor shall indemnify Client even for Client's sole negligence. | |
| SECTION 5 - LIMITATION OF LIABILITY | |
| There shall be no limitation of liability for Vendor under this Agreement. | |
| Vendor waives all consequential damages claims against Client. | |
| SECTION 6 - TERM AND TERMINATION | |
| 6.1 This Agreement may be terminated by Client at any time for any reason. | |
| 6.2 Client may terminate immediately without notice. | |
| 6.3 No refund shall be provided upon termination. | |
| SECTION 7 - NON-COMPETE | |
| 7.1 Vendor agrees to a worldwide non-compete for a period of 5 years. | |
| 7.2 This restriction shall be perpetual for any competing products. | |
| SECTION 8 - DISPUTE RESOLUTION | |
| 8.1 All disputes shall be resolved by binding arbitration. | |
| 8.2 Client waives jury trial rights. | |
| 8.3 Vendor waives any right to participate in class action lawsuits. | |
| SECTION 9 - GOVERNING LAW | |
| This Agreement shall be governed by the laws of the State of Delaware. | |
| Exclusive jurisdiction shall be in Delaware courts. | |
| """, | |
| ) | |
| agent.add_contract(contract) | |
| print("\n" + "=" * 60) | |
| print("Test 1: Attorney Analyzes Contract") | |
| print("=" * 60) | |
| try: | |
| review = await agent.analyze_contract("DOC-001", attorney) | |
| print(f"\n{review.summary}") | |
| print("\n📋 Detailed Findings:") | |
| for f in review.findings: | |
| icon = "⛔" if f.risk_level == RiskLevel.CRITICAL else "⚠️" if f.risk_level == RiskLevel.HIGH else "ℹ️" | |
| print(f"\n{icon} [{f.risk_level.value.upper()}] {f.clause_type.value}") | |
| print(f" Location: {f.location}") | |
| print(f" Issue: {f.description}") | |
| print(f" Recommendation: {f.recommendation}") | |
| except PermissionError as e: | |
| print(f"❌ Error: {e}") | |
| print("\n" + "=" * 60) | |
| print("Test 2: Add Conflict and Test Access") | |
| print("=" * 60) | |
| agent.conflict_checker.add_conflict("Tech Corp") | |
| try: | |
| review = await agent.analyze_contract("DOC-001", attorney) | |
| except PermissionError as e: | |
| print(f"✓ Correctly blocked: {e}") | |
| print("\n" + "=" * 60) | |
| print("📊 Agent Metrics") | |
| print("=" * 60) | |
| metrics = agent.get_metrics() | |
| for k, v in metrics.items(): | |
| print(f" {k}: {v}") | |
| print("\n" + "=" * 60) | |
| print("📝 Audit Trail") | |
| print("=" * 60) | |
| for entry in agent.audit_log.entries[-5:]: | |
| print(f" [{entry.timestamp.strftime('%H:%M:%S')}] {entry.user_id} | {entry.action} | {entry.outcome}") | |
| print("\n" + "=" * 60) | |
| print("✅ Demo Complete - All access governed and audited") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| asyncio.run(demo()) | |
Xet Storage Details
- Size:
- 32.6 kB
- Xet hash:
- c28415a44b29cda8375e710e82191949f0149c3e7a916b9bae551bd61aa3b08a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.