forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
933 lines (788 loc) · 31.8 KB
/
main.py
File metadata and controls
933 lines (788 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# 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
# ============================================================
@dataclass
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
@dataclass
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
@dataclass
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
@dataclass
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
@dataclass
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
@dataclass
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())