crprotocol 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. crp/__init__.py +126 -0
  2. crp/__main__.py +8 -0
  3. crp/_typing.py +27 -0
  4. crp/_version.py +5 -0
  5. crp/adapters.py +31 -0
  6. crp/advanced/__init__.py +40 -0
  7. crp/advanced/auto_ingest.py +400 -0
  8. crp/advanced/cqs.py +235 -0
  9. crp/advanced/cross_window.py +477 -0
  10. crp/advanced/curator.py +265 -0
  11. crp/advanced/feedback.py +146 -0
  12. crp/advanced/hierarchical.py +211 -0
  13. crp/advanced/meta_learning.py +401 -0
  14. crp/advanced/parallel.py +98 -0
  15. crp/advanced/review_cycle.py +329 -0
  16. crp/advanced/scale_mode.py +129 -0
  17. crp/advanced/source_grounding.py +207 -0
  18. crp/ckf/__init__.py +35 -0
  19. crp/ckf/community.py +377 -0
  20. crp/ckf/fabric.py +445 -0
  21. crp/ckf/gc.py +175 -0
  22. crp/ckf/graph_walk.py +87 -0
  23. crp/ckf/merge.py +133 -0
  24. crp/ckf/pattern_query.py +122 -0
  25. crp/ckf/pubsub.py +128 -0
  26. crp/ckf/semantic.py +207 -0
  27. crp/cli/__init__.py +7 -0
  28. crp/cli/main.py +329 -0
  29. crp/cli/sidecar.py +929 -0
  30. crp/cli/startup.py +272 -0
  31. crp/continuation/__init__.py +103 -0
  32. crp/continuation/completion.py +348 -0
  33. crp/continuation/degradation.py +157 -0
  34. crp/continuation/document_map.py +160 -0
  35. crp/continuation/flow.py +109 -0
  36. crp/continuation/gap.py +419 -0
  37. crp/continuation/manager.py +484 -0
  38. crp/continuation/quality_monitor.py +179 -0
  39. crp/continuation/stitch.py +419 -0
  40. crp/continuation/trigger.py +142 -0
  41. crp/continuation/voice.py +157 -0
  42. crp/core/__init__.py +69 -0
  43. crp/core/batch.py +77 -0
  44. crp/core/circuit_breaker.py +116 -0
  45. crp/core/config.py +377 -0
  46. crp/core/context_tools.py +540 -0
  47. crp/core/dispatch_router.py +3977 -0
  48. crp/core/errors.py +128 -0
  49. crp/core/extraction_facade.py +384 -0
  50. crp/core/facilitator.py +713 -0
  51. crp/core/idempotency.py +215 -0
  52. crp/core/orchestrator.py +1435 -0
  53. crp/core/relay_strategies.py +613 -0
  54. crp/core/security_manager.py +140 -0
  55. crp/core/session.py +134 -0
  56. crp/core/task_intent.py +36 -0
  57. crp/core/window.py +363 -0
  58. crp/envelope/__init__.py +30 -0
  59. crp/envelope/builder.py +288 -0
  60. crp/envelope/decomposer.py +236 -0
  61. crp/envelope/formatter.py +168 -0
  62. crp/envelope/packer.py +211 -0
  63. crp/envelope/reranker.py +209 -0
  64. crp/envelope/scoring.py +310 -0
  65. crp/extraction/__init__.py +45 -0
  66. crp/extraction/complexity.py +96 -0
  67. crp/extraction/contradiction.py +132 -0
  68. crp/extraction/pipeline.py +360 -0
  69. crp/extraction/quality_gate.py +237 -0
  70. crp/extraction/stage1_regex.py +173 -0
  71. crp/extraction/stage2_statistical.py +244 -0
  72. crp/extraction/stage3_gliner.py +210 -0
  73. crp/extraction/stage4_uie.py +183 -0
  74. crp/extraction/stage5_discourse.py +175 -0
  75. crp/extraction/stage6_llm.py +178 -0
  76. crp/extraction/structured_output.py +219 -0
  77. crp/extraction/types.py +299 -0
  78. crp/license_guard.py +722 -0
  79. crp/observability/__init__.py +30 -0
  80. crp/observability/audit.py +118 -0
  81. crp/observability/events.py +233 -0
  82. crp/observability/metrics.py +264 -0
  83. crp/observability/quality.py +135 -0
  84. crp/observability/structured_logging.py +81 -0
  85. crp/observability/telemetry.py +117 -0
  86. crp/provenance/__init__.py +314 -0
  87. crp/provenance/_embeddings.py +97 -0
  88. crp/provenance/_types.py +378 -0
  89. crp/provenance/attribution_scorer.py +252 -0
  90. crp/provenance/claim_detector.py +229 -0
  91. crp/provenance/contradiction_detector.py +243 -0
  92. crp/provenance/distortion_detector.py +397 -0
  93. crp/provenance/entailment_verifier.py +358 -0
  94. crp/provenance/fabrication_detector.py +203 -0
  95. crp/provenance/hallucination_scorer.py +320 -0
  96. crp/provenance/omission_analyzer.py +106 -0
  97. crp/provenance/provenance_chain.py +205 -0
  98. crp/provenance/report_generator.py +440 -0
  99. crp/providers/__init__.py +43 -0
  100. crp/providers/anthropic.py +270 -0
  101. crp/providers/base.py +135 -0
  102. crp/providers/custom.py +63 -0
  103. crp/providers/diagnostic.py +251 -0
  104. crp/providers/llamacpp.py +224 -0
  105. crp/providers/manager.py +139 -0
  106. crp/providers/ollama.py +243 -0
  107. crp/providers/openai.py +628 -0
  108. crp/providers/tokenizers.py +48 -0
  109. crp/py.typed +0 -0
  110. crp/resources/__init__.py +53 -0
  111. crp/resources/adaptive_allocator.py +525 -0
  112. crp/resources/cost_model.py +388 -0
  113. crp/resources/overhead_manager.py +217 -0
  114. crp/resources/resource_manager.py +262 -0
  115. crp/schemas/__init__.py +20 -0
  116. crp/schemas/cost-estimate.json +33 -0
  117. crp/schemas/crp-error.json +43 -0
  118. crp/schemas/envelope-preview.json +40 -0
  119. crp/schemas/persisted-state-header.json +27 -0
  120. crp/schemas/quality-report.json +94 -0
  121. crp/schemas/session-handle.json +33 -0
  122. crp/schemas/session-status.json +57 -0
  123. crp/schemas/stream-event.json +18 -0
  124. crp/schemas/task-intent.json +42 -0
  125. crp/security/__init__.py +93 -0
  126. crp/security/audit_trail.py +392 -0
  127. crp/security/binding.py +192 -0
  128. crp/security/compliance.py +813 -0
  129. crp/security/consent.py +593 -0
  130. crp/security/embedding_defense.py +161 -0
  131. crp/security/encryption.py +202 -0
  132. crp/security/injection.py +335 -0
  133. crp/security/integrity.py +267 -0
  134. crp/security/privacy.py +662 -0
  135. crp/security/quarantine.py +249 -0
  136. crp/security/rbac.py +221 -0
  137. crp/security/validation.py +164 -0
  138. crp/state/__init__.py +31 -0
  139. crp/state/cold_storage.py +258 -0
  140. crp/state/compaction.py +263 -0
  141. crp/state/critical_state.py +104 -0
  142. crp/state/event_log.py +313 -0
  143. crp/state/fact.py +189 -0
  144. crp/state/serialization.py +189 -0
  145. crp/state/session_cleanup.py +77 -0
  146. crp/state/snapshot.py +290 -0
  147. crp/state/warm_store.py +346 -0
  148. crprotocol-2.0.0.dist-info/METADATA +1295 -0
  149. crprotocol-2.0.0.dist-info/RECORD +153 -0
  150. crprotocol-2.0.0.dist-info/WHEEL +4 -0
  151. crprotocol-2.0.0.dist-info/entry_points.txt +2 -0
  152. crprotocol-2.0.0.dist-info/licenses/LICENSE.md +170 -0
  153. crprotocol-2.0.0.dist-info/licenses/NOTICE +18 -0
@@ -0,0 +1,813 @@
1
+ # Copyright © 2025 Constantinos Vidiniotis. All rights reserved.
2
+ # Licensed under Elastic License 2.0 — see LICENSE.md for details.
3
+ """EU AI Act + ISO 42001 compliance framework (§7.15).
4
+
5
+ Implements:
6
+ - AI system risk classification (EU AI Act Art. 6)
7
+ - Transparency declarations (EU AI Act Art. 13)
8
+ - Technical documentation generation (EU AI Act Art. 11)
9
+ - Compliance status reporting (EU AI Act Art. 9, ISO 42001 9.1)
10
+ - AI impact assessment (ISO 42001 A.6.2.4)
11
+ - Quality management system integration (EU AI Act Art. 17)
12
+
13
+ EU AI Act: Art. 6 (classification), Art. 9-17 (high-risk requirements)
14
+ ISO 42001: 4-10 (full AIMS lifecycle), A.6.2 (AI-specific controls)
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import logging
20
+ import time
21
+ from dataclasses import dataclass, field
22
+ from enum import Enum
23
+ from typing import Any
24
+
25
+ logger = logging.getLogger("crp.security.compliance")
26
+
27
+
28
+ # ---------------------------------------------------------------------------
29
+ # AI risk classification (EU AI Act Art. 6)
30
+ # ---------------------------------------------------------------------------
31
+
32
+
33
+ class AIRiskLevel(str, Enum):
34
+ """EU AI Act risk classification levels (Art. 6) (§7.15.1)."""
35
+
36
+ MINIMAL = "minimal" # Unregulated (spam filters, video games)
37
+ LIMITED = "limited" # Transparency obligations (chatbots, deepfakes)
38
+ HIGH = "high" # Full compliance required (see Annex III)
39
+ UNACCEPTABLE = "unacceptable" # Prohibited (social scoring, etc.)
40
+
41
+
42
+ class AISystemCategory(str, Enum):
43
+ """Categories of AI system use cases relevant to risk classification."""
44
+
45
+ GENERAL_PURPOSE = "general_purpose" # GPAI model provider/integrator
46
+ CONTEXT_MANAGEMENT = "context_management" # CRP core function
47
+ CONTENT_GENERATION = "content_generation" # Text generation via LLM
48
+ DECISION_SUPPORT = "decision_support" # AI-assisted decisions
49
+ AUTOMATED_DECISION = "automated_decision" # Automated decision-making
50
+ BIOMETRIC = "biometric" # Biometric processing
51
+ CRITICAL_INFRASTRUCTURE = "critical_infrastructure" # Safety-critical
52
+ EMPLOYMENT = "employment" # HR/recruitment
53
+ EDUCATION = "education" # Education assessment
54
+ LAW_ENFORCEMENT = "law_enforcement" # Law enforcement
55
+ HEALTHCARE = "healthcare" # Health/medical
56
+ FINANCIAL = "financial" # Credit scoring, insurance
57
+
58
+
59
+ @dataclass
60
+ class RiskAssessment:
61
+ """AI system risk assessment result (§7.15.1).
62
+
63
+ EU AI Act Art. 9: Providers must establish a risk management system
64
+ for the entire lifecycle of the high-risk AI system.
65
+ """
66
+
67
+ assessment_id: str
68
+ timestamp: float = field(default_factory=time.time)
69
+ risk_level: AIRiskLevel = AIRiskLevel.MINIMAL
70
+ system_category: AISystemCategory = AISystemCategory.CONTEXT_MANAGEMENT
71
+ intended_purpose: str = ""
72
+ # Risk factors
73
+ processes_personal_data: bool = False
74
+ makes_automated_decisions: bool = False
75
+ affects_fundamental_rights: bool = False
76
+ safety_critical: bool = False
77
+ profiles_individuals: bool = False
78
+ # Mitigation measures
79
+ mitigations: list[str] = field(default_factory=list)
80
+ residual_risks: list[str] = field(default_factory=list)
81
+ # Assessment outcome
82
+ assessment_notes: str = ""
83
+ assessor: str = "" # Who performed the assessment
84
+ review_date: float = 0.0 # When to review again
85
+
86
+ def to_dict(self) -> dict[str, Any]:
87
+ return {
88
+ "assessment_id": self.assessment_id,
89
+ "timestamp": self.timestamp,
90
+ "risk_level": self.risk_level.value,
91
+ "system_category": self.system_category.value,
92
+ "intended_purpose": self.intended_purpose,
93
+ "risk_factors": {
94
+ "processes_personal_data": self.processes_personal_data,
95
+ "makes_automated_decisions": self.makes_automated_decisions,
96
+ "affects_fundamental_rights": self.affects_fundamental_rights,
97
+ "safety_critical": self.safety_critical,
98
+ "profiles_individuals": self.profiles_individuals,
99
+ },
100
+ "mitigations": self.mitigations,
101
+ "residual_risks": self.residual_risks,
102
+ "assessment_notes": self.assessment_notes,
103
+ "assessor": self.assessor,
104
+ "review_date": self.review_date,
105
+ }
106
+
107
+
108
+ class RiskClassifier:
109
+ """Classify AI system risk level per EU AI Act (§7.15.1).
110
+
111
+ Helps users determine their obligation level based on how they
112
+ deploy CRP within their AI system.
113
+
114
+ CRP itself is a context management tool — typically MINIMAL or LIMITED
115
+ risk. However, if CRP is integrated into a high-risk AI system
116
+ (e.g., employment screening, credit scoring), the overall system
117
+ inherits the higher classification.
118
+
119
+ Usage::
120
+
121
+ classifier = RiskClassifier()
122
+ assessment = classifier.assess(
123
+ category=AISystemCategory.CONTEXT_MANAGEMENT,
124
+ intended_purpose="Managing context for a customer support chatbot",
125
+ processes_personal_data=True,
126
+ )
127
+ print(f"Risk level: {assessment.risk_level.value}")
128
+ """
129
+
130
+ # Categories that are always HIGH risk per EU AI Act Annex III
131
+ _HIGH_RISK_CATEGORIES: frozenset[AISystemCategory] = frozenset(
132
+ {
133
+ AISystemCategory.BIOMETRIC,
134
+ AISystemCategory.CRITICAL_INFRASTRUCTURE,
135
+ AISystemCategory.EMPLOYMENT,
136
+ AISystemCategory.EDUCATION,
137
+ AISystemCategory.LAW_ENFORCEMENT,
138
+ AISystemCategory.HEALTHCARE,
139
+ AISystemCategory.FINANCIAL,
140
+ }
141
+ )
142
+
143
+ # Categories that are always UNACCEPTABLE
144
+ _PROHIBITED_INDICATORS: list[str] = [
145
+ "social_scoring",
146
+ "subliminal_manipulation",
147
+ "vulnerability_exploitation",
148
+ "emotion_recognition_workplace",
149
+ "untargeted_facial_scraping",
150
+ "predictive_policing_profiling",
151
+ ]
152
+
153
+ def assess(
154
+ self,
155
+ category: AISystemCategory = AISystemCategory.CONTEXT_MANAGEMENT,
156
+ intended_purpose: str = "",
157
+ processes_personal_data: bool = False,
158
+ makes_automated_decisions: bool = False,
159
+ affects_fundamental_rights: bool = False,
160
+ safety_critical: bool = False,
161
+ profiles_individuals: bool = False,
162
+ ) -> RiskAssessment:
163
+ """Perform risk assessment based on EU AI Act criteria."""
164
+ import uuid
165
+
166
+ # Determine risk level
167
+ risk_level = self._classify(
168
+ category=category,
169
+ processes_personal_data=processes_personal_data,
170
+ makes_automated_decisions=makes_automated_decisions,
171
+ affects_fundamental_rights=affects_fundamental_rights,
172
+ safety_critical=safety_critical,
173
+ profiles_individuals=profiles_individuals,
174
+ )
175
+
176
+ # Determine mitigations (CRP provides these natively)
177
+ mitigations = self._get_native_mitigations(risk_level)
178
+
179
+ # Identify residual risks
180
+ residual_risks = self._get_residual_risks(
181
+ risk_level, processes_personal_data, makes_automated_decisions
182
+ )
183
+
184
+ assessment = RiskAssessment(
185
+ assessment_id=f"risk-{uuid.uuid4().hex[:12]}",
186
+ risk_level=risk_level,
187
+ system_category=category,
188
+ intended_purpose=intended_purpose,
189
+ processes_personal_data=processes_personal_data,
190
+ makes_automated_decisions=makes_automated_decisions,
191
+ affects_fundamental_rights=affects_fundamental_rights,
192
+ safety_critical=safety_critical,
193
+ profiles_individuals=profiles_individuals,
194
+ mitigations=mitigations,
195
+ residual_risks=residual_risks,
196
+ )
197
+
198
+ logger.info(
199
+ "Risk assessment: %s → %s (category=%s)",
200
+ assessment.assessment_id,
201
+ risk_level.value,
202
+ category.value,
203
+ )
204
+ return assessment
205
+
206
+ def _classify(
207
+ self,
208
+ category: AISystemCategory,
209
+ processes_personal_data: bool,
210
+ makes_automated_decisions: bool,
211
+ affects_fundamental_rights: bool,
212
+ safety_critical: bool,
213
+ profiles_individuals: bool,
214
+ ) -> AIRiskLevel:
215
+ """Apply EU AI Act classification rules."""
216
+ # Annex III high-risk categories
217
+ if category in self._HIGH_RISK_CATEGORIES:
218
+ return AIRiskLevel.HIGH
219
+
220
+ # Profiling individuals always at least HIGH (Art. 6.2)
221
+ if profiles_individuals:
222
+ return AIRiskLevel.HIGH
223
+
224
+ # Safety-critical → HIGH
225
+ if safety_critical:
226
+ return AIRiskLevel.HIGH
227
+
228
+ # Automated decisions affecting fundamental rights → HIGH
229
+ if makes_automated_decisions and affects_fundamental_rights:
230
+ return AIRiskLevel.HIGH
231
+
232
+ # AI systems that interact with humans → LIMITED (transparency)
233
+ if category in (
234
+ AISystemCategory.CONTENT_GENERATION,
235
+ AISystemCategory.DECISION_SUPPORT,
236
+ AISystemCategory.GENERAL_PURPOSE,
237
+ ):
238
+ return AIRiskLevel.LIMITED
239
+
240
+ # Context management with personal data → LIMITED
241
+ if processes_personal_data:
242
+ return AIRiskLevel.LIMITED
243
+
244
+ # Default: MINIMAL
245
+ return AIRiskLevel.MINIMAL
246
+
247
+ def _get_native_mitigations(self, risk_level: AIRiskLevel) -> list[str]:
248
+ """List CRP's native risk mitigations."""
249
+ mitigations = [
250
+ "Session-scoped cryptographic isolation (§7.1)",
251
+ "AES-256-GCM encryption at rest (§7.3)",
252
+ "Input validation — always on, cannot disable (§7.4)",
253
+ "Prompt injection detection — advisory, never blocks (§7.5)",
254
+ "Anti-poisoning quarantine with confidence penalty (§7.8)",
255
+ "RBAC with three-tier access control (§7.10)",
256
+ "Embedding defense — SQ8 + XOR salting (§7.11)",
257
+ "PII detection and data classification (§7.12)",
258
+ "Consent management with purpose limitation (§7.13)",
259
+ "Tamper-evident HMAC-signed audit trail (§7.14)",
260
+ "Fact integrity chain — BLAKE3/SHA-256 (§7.2, §7.7)",
261
+ "Data retention with automatic expiry (§7.12.3)",
262
+ "Right to erasure support — GDPR Art. 17 (§7.12.4)",
263
+ ]
264
+
265
+ if risk_level in (AIRiskLevel.HIGH, AIRiskLevel.LIMITED):
266
+ mitigations.extend(
267
+ [
268
+ "Human oversight controls — configurable levels (§7.13.4)",
269
+ "Processing records — GDPR Art. 30 compliant (§7.13.3)",
270
+ "Data lineage tracking (§7.12.5)",
271
+ "Compliance audit trail export for regulatory review (§7.14)",
272
+ ]
273
+ )
274
+
275
+ return mitigations
276
+
277
+ def _get_residual_risks(
278
+ self,
279
+ risk_level: AIRiskLevel,
280
+ processes_personal_data: bool,
281
+ makes_automated_decisions: bool,
282
+ ) -> list[str]:
283
+ """Identify residual risks that CRP cannot fully mitigate."""
284
+ risks: list[str] = []
285
+
286
+ if risk_level == AIRiskLevel.HIGH:
287
+ risks.append(
288
+ "CRP provides context management — the deployer is responsible "
289
+ "for the overall high-risk AI system conformity assessment"
290
+ )
291
+ risks.append(
292
+ "LLM output quality and bias are the provider's responsibility "
293
+ "(CRP relays output without modification — Axiom 9)"
294
+ )
295
+
296
+ if processes_personal_data:
297
+ risks.append(
298
+ "PII detection is pattern-based and may miss novel PII formats; "
299
+ "deployers should implement additional domain-specific checks"
300
+ )
301
+
302
+ if makes_automated_decisions:
303
+ risks.append(
304
+ "CRP does not make decisions — it manages context for LLMs; "
305
+ "decision-making logic is the deployer's responsibility"
306
+ )
307
+
308
+ risks.append(
309
+ "XOR cipher fallback when cryptography package is not installed "
310
+ "provides only obfuscation — install cryptography for production"
311
+ )
312
+
313
+ return risks
314
+
315
+
316
+ # ---------------------------------------------------------------------------
317
+ # Transparency declaration (EU AI Act Art. 13)
318
+ # ---------------------------------------------------------------------------
319
+
320
+
321
+ @dataclass
322
+ class TransparencyDeclaration:
323
+ """Transparency declaration for AI system users (§7.15.2).
324
+
325
+ EU AI Act Art. 13: Providers must ensure that high-risk AI systems
326
+ are designed and developed in such a way that their operation is
327
+ sufficiently transparent to enable deployers to interpret the
328
+ system's output and use it appropriately.
329
+ """
330
+
331
+ system_name: str = "Context Relay Protocol (CRP)"
332
+ system_version: str = ""
333
+ provider: str = "AutoCyber AI Pty Ltd"
334
+ provider_contact: str = "security@autocyberai.com"
335
+ intended_purpose: str = (
336
+ "CRP manages context windows for Large Language Model (LLM) "
337
+ "applications. It extracts, stores, and retrieves knowledge "
338
+ "across multi-window conversations to maximize LLM output quality."
339
+ )
340
+ ai_involvement: str = (
341
+ "CRP uses AI/ML for: (1) fact extraction from text, "
342
+ "(2) semantic similarity scoring for context selection, "
343
+ "(3) prompt injection detection. CRP does NOT generate text — "
344
+ "it relays context to an LLM chosen and controlled by the deployer."
345
+ )
346
+ data_processed: list[str] = field(
347
+ default_factory=lambda: [
348
+ "Text provided by the user for context management",
349
+ "Facts extracted from text via NLP pipeline",
350
+ "Knowledge graph relationships between facts",
351
+ "Context envelopes assembled for LLM calls",
352
+ "Quality scores for LLM output assessment",
353
+ ]
354
+ )
355
+ data_not_processed: list[str] = field(
356
+ default_factory=lambda: [
357
+ "LLM API keys (never touch CRP servers)",
358
+ "LLM request/response traffic (stays in user's process)",
359
+ "System prompts (remain in user's application)",
360
+ "User's application source code",
361
+ ]
362
+ )
363
+ limitations: list[str] = field(
364
+ default_factory=lambda: [
365
+ "CRP does not generate text — quality depends on the LLM",
366
+ "PII detection is pattern-based, not guaranteed comprehensive",
367
+ "Injection detection is advisory, not guaranteed to catch all attacks",
368
+ "Context selection is based on relevance scoring, not perfect recall",
369
+ ]
370
+ )
371
+ human_oversight: str = (
372
+ "CRP supports configurable human oversight levels: NONE, INFORMED, "
373
+ "APPROVAL, and CONTROL. Deployers can require human approval "
374
+ "before dispatch, ingest, export, or deletion operations."
375
+ )
376
+ risk_level: AIRiskLevel = AIRiskLevel.MINIMAL
377
+ last_updated: float = field(default_factory=time.time)
378
+
379
+ def to_dict(self) -> dict[str, Any]:
380
+ return {
381
+ "system_name": self.system_name,
382
+ "system_version": self.system_version,
383
+ "provider": self.provider,
384
+ "provider_contact": self.provider_contact,
385
+ "intended_purpose": self.intended_purpose,
386
+ "ai_involvement": self.ai_involvement,
387
+ "data_processed": self.data_processed,
388
+ "data_not_processed": self.data_not_processed,
389
+ "limitations": self.limitations,
390
+ "human_oversight": self.human_oversight,
391
+ "risk_level": self.risk_level.value,
392
+ "last_updated": self.last_updated,
393
+ }
394
+
395
+
396
+ # ---------------------------------------------------------------------------
397
+ # Compliance status report
398
+ # ---------------------------------------------------------------------------
399
+
400
+
401
+ @dataclass
402
+ class ComplianceControl:
403
+ """Single compliance control status."""
404
+
405
+ control_id: str
406
+ framework: str # "eu_ai_act" | "iso_42001"
407
+ article: str # e.g. "Art. 9" or "A.6.2.4"
408
+ description: str
409
+ status: str # "implemented" | "partial" | "planned" | "not_applicable"
410
+ implementation: str # How CRP implements this control
411
+ evidence: str = "" # Where to find evidence (file, module, test)
412
+
413
+
414
+ class ComplianceReporter:
415
+ """Generate compliance status reports (§7.15.3).
416
+
417
+ Maps CRP's native security controls to EU AI Act articles and
418
+ ISO 42001 clauses, reporting implementation status for each.
419
+
420
+ Usage::
421
+
422
+ reporter = ComplianceReporter()
423
+ report = reporter.generate_report(session_stats={...})
424
+ print(report["summary"]["compliance_score"])
425
+ """
426
+
427
+ def __init__(self) -> None:
428
+ self._controls = self._build_control_map()
429
+
430
+ def _build_control_map(self) -> list[ComplianceControl]:
431
+ """Build the full control map — CRP features → regulations."""
432
+ return [
433
+ # ── EU AI Act ──────────────────────────────────────
434
+ ComplianceControl(
435
+ control_id="EUAI-01",
436
+ framework="eu_ai_act",
437
+ article="Art. 9",
438
+ description="Risk management system",
439
+ status="implemented",
440
+ implementation=(
441
+ "RiskClassifier provides automated risk assessment. "
442
+ "Session-scoped security with 8-layer defense-in-depth."
443
+ ),
444
+ evidence="crp/security/compliance.py::RiskClassifier",
445
+ ),
446
+ ComplianceControl(
447
+ control_id="EUAI-02",
448
+ framework="eu_ai_act",
449
+ article="Art. 10",
450
+ description="Data governance",
451
+ status="implemented",
452
+ implementation=(
453
+ "DataClassification (5 levels), PII detection, "
454
+ "DataLineageTracker, RetentionManager with auto-expiry, "
455
+ "IngestQuarantine for anti-poisoning."
456
+ ),
457
+ evidence="crp/security/privacy.py, crp/security/quarantine.py",
458
+ ),
459
+ ComplianceControl(
460
+ control_id="EUAI-03",
461
+ framework="eu_ai_act",
462
+ article="Art. 11",
463
+ description="Technical documentation",
464
+ status="implemented",
465
+ implementation=(
466
+ "ComplianceReporter generates structured technical "
467
+ "documentation. TransparencyDeclaration provides "
468
+ "system-level documentation."
469
+ ),
470
+ evidence="crp/security/compliance.py::ComplianceReporter",
471
+ ),
472
+ ComplianceControl(
473
+ control_id="EUAI-04",
474
+ framework="eu_ai_act",
475
+ article="Art. 12",
476
+ description="Record-keeping (automatic logging)",
477
+ status="implemented",
478
+ implementation=(
479
+ "ComplianceAuditTrail with HMAC-signed tamper-evident "
480
+ "entries. ProcessingRecordKeeper for GDPR Art. 30. "
481
+ "EventEmitter with 30+ event types. TelemetryWriter "
482
+ "for per-window JSONL logs."
483
+ ),
484
+ evidence="crp/security/audit_trail.py, crp/observability/",
485
+ ),
486
+ ComplianceControl(
487
+ control_id="EUAI-05",
488
+ framework="eu_ai_act",
489
+ article="Art. 13",
490
+ description="Transparency",
491
+ status="implemented",
492
+ implementation=(
493
+ "TransparencyDeclaration documents system purpose, "
494
+ "AI involvement, data processed/not processed, "
495
+ "limitations. ConsentManager tracks processing purposes."
496
+ ),
497
+ evidence="crp/security/compliance.py, crp/security/consent.py",
498
+ ),
499
+ ComplianceControl(
500
+ control_id="EUAI-06",
501
+ framework="eu_ai_act",
502
+ article="Art. 14",
503
+ description="Human oversight",
504
+ status="implemented",
505
+ implementation=(
506
+ "HumanOversightController with 4 levels (NONE, INFORMED, "
507
+ "APPROVAL, CONTROL). Configurable approval requirements "
508
+ "per operation. Halt-on-detection for injection/PII."
509
+ ),
510
+ evidence="crp/security/consent.py::HumanOversightController",
511
+ ),
512
+ ComplianceControl(
513
+ control_id="EUAI-07",
514
+ framework="eu_ai_act",
515
+ article="Art. 15",
516
+ description="Accuracy, robustness, cybersecurity",
517
+ status="implemented",
518
+ implementation=(
519
+ "AES-256-GCM encryption, HMAC-SHA256 binding, "
520
+ "BLAKE3 integrity chains, 8-layer defense stack, "
521
+ "RBAC, rate limiting, input validation, injection "
522
+ "detection, anti-poisoning quarantine."
523
+ ),
524
+ evidence="crp/security/ (all 8 modules)",
525
+ ),
526
+ ComplianceControl(
527
+ control_id="EUAI-08",
528
+ framework="eu_ai_act",
529
+ article="Art. 17",
530
+ description="Quality management system",
531
+ status="implemented",
532
+ implementation=(
533
+ "QualityReport per dispatch with tier grading (S/A/B/C/D). "
534
+ "Overhead tracking, resource management, envelope "
535
+ "saturation metrics. ComplianceReporter for QMS evidence."
536
+ ),
537
+ evidence="crp/core/session.py::QualityReport, crp/observability/quality.py",
538
+ ),
539
+ # ── ISO 42001 ─────────────────────────────────────
540
+ ComplianceControl(
541
+ control_id="ISO-01",
542
+ framework="iso_42001",
543
+ article="A.6.2.3",
544
+ description="Human oversight of AI systems",
545
+ status="implemented",
546
+ implementation=(
547
+ "HumanOversightController with configurable levels. "
548
+ "Approval workflows, halt mechanisms, autonomous limits."
549
+ ),
550
+ evidence="crp/security/consent.py::HumanOversightController",
551
+ ),
552
+ ComplianceControl(
553
+ control_id="ISO-02",
554
+ framework="iso_42001",
555
+ article="A.6.2.4",
556
+ description="AI impact assessment",
557
+ status="implemented",
558
+ implementation=(
559
+ "RiskClassifier performs AI risk assessment per EU AI Act "
560
+ "classification. Identifies mitigations and residual risks."
561
+ ),
562
+ evidence="crp/security/compliance.py::RiskClassifier",
563
+ ),
564
+ ComplianceControl(
565
+ control_id="ISO-03",
566
+ framework="iso_42001",
567
+ article="A.6.2.5",
568
+ description="Data for AI systems (collection & use)",
569
+ status="implemented",
570
+ implementation=(
571
+ "ConsentManager with processing purposes. ProcessingRecordKeeper "
572
+ "tracks all data processing activities with legal basis."
573
+ ),
574
+ evidence="crp/security/consent.py",
575
+ ),
576
+ ComplianceControl(
577
+ control_id="ISO-04",
578
+ framework="iso_42001",
579
+ article="A.6.2.6",
580
+ description="Data management",
581
+ status="implemented",
582
+ implementation=(
583
+ "DataClassification (5 levels), DataLineageTracker, "
584
+ "RetentionManager with auto-expiry, PII detection, "
585
+ "WarmStateStore with fact lifecycle management."
586
+ ),
587
+ evidence="crp/security/privacy.py",
588
+ ),
589
+ ComplianceControl(
590
+ control_id="ISO-05",
591
+ framework="iso_42001",
592
+ article="A.6.2.7",
593
+ description="Data subject rights",
594
+ status="implemented",
595
+ implementation=(
596
+ "ErasureManager for right to erasure (GDPR Art. 17). "
597
+ "export_state() for data portability. ConsentManager "
598
+ "for consent withdrawal."
599
+ ),
600
+ evidence="crp/security/privacy.py::ErasureManager",
601
+ ),
602
+ ComplianceControl(
603
+ control_id="ISO-06",
604
+ framework="iso_42001",
605
+ article="A.6.2.8",
606
+ description="Records management",
607
+ status="implemented",
608
+ implementation=(
609
+ "ComplianceAuditTrail with tamper-evident HMAC-signed entries. "
610
+ "ProcessingRecordKeeper for GDPR Art. 30. "
611
+ "EventEmitter + AuditLog for operational records."
612
+ ),
613
+ evidence="crp/security/audit_trail.py, crp/observability/audit.py",
614
+ ),
615
+ ComplianceControl(
616
+ control_id="ISO-07",
617
+ framework="iso_42001",
618
+ article="9.1",
619
+ description="Performance monitoring & measurement",
620
+ status="implemented",
621
+ implementation=(
622
+ "QualityReport with tier grading. TelemetryWriter for "
623
+ "per-window metrics. ResourceManager for memory tracking. "
624
+ "OverheadBudgetManager for performance caps."
625
+ ),
626
+ evidence="crp/observability/telemetry.py, crp/resources/",
627
+ ),
628
+ ComplianceControl(
629
+ control_id="ISO-08",
630
+ framework="iso_42001",
631
+ article="10.1",
632
+ description="Continual improvement",
633
+ status="implemented",
634
+ implementation=(
635
+ "Fact confidence decay, supersession, and archival. "
636
+ "Adaptive resource allocation. Meta-learning scaffolds. "
637
+ "Quality tier tracking across sessions."
638
+ ),
639
+ evidence="crp/state/warm_store.py, crp/advanced/meta_learning.py",
640
+ ),
641
+ ]
642
+
643
+ def generate_report(
644
+ self,
645
+ session_stats: dict[str, Any] | None = None,
646
+ risk_assessment: RiskAssessment | None = None,
647
+ ) -> dict[str, Any]:
648
+ """Generate a comprehensive compliance status report."""
649
+ eu_controls = [c for c in self._controls if c.framework == "eu_ai_act"]
650
+ iso_controls = [c for c in self._controls if c.framework == "iso_42001"]
651
+
652
+ eu_implemented = sum(1 for c in eu_controls if c.status == "implemented")
653
+ iso_implemented = sum(1 for c in iso_controls if c.status == "implemented")
654
+
655
+ report = {
656
+ "report_type": "compliance_status",
657
+ "generated_at": time.time(),
658
+ "frameworks": {
659
+ "eu_ai_act": {
660
+ "total_controls": len(eu_controls),
661
+ "implemented": eu_implemented,
662
+ "compliance_pct": round(
663
+ eu_implemented / len(eu_controls) * 100, 1
664
+ )
665
+ if eu_controls
666
+ else 0,
667
+ "controls": [
668
+ {
669
+ "control_id": c.control_id,
670
+ "article": c.article,
671
+ "description": c.description,
672
+ "status": c.status,
673
+ "implementation": c.implementation,
674
+ "evidence": c.evidence,
675
+ }
676
+ for c in eu_controls
677
+ ],
678
+ },
679
+ "iso_42001": {
680
+ "total_controls": len(iso_controls),
681
+ "implemented": iso_implemented,
682
+ "compliance_pct": round(
683
+ iso_implemented / len(iso_controls) * 100, 1
684
+ )
685
+ if iso_controls
686
+ else 0,
687
+ "controls": [
688
+ {
689
+ "control_id": c.control_id,
690
+ "article": c.article,
691
+ "description": c.description,
692
+ "status": c.status,
693
+ "implementation": c.implementation,
694
+ "evidence": c.evidence,
695
+ }
696
+ for c in iso_controls
697
+ ],
698
+ },
699
+ },
700
+ "summary": {
701
+ "total_controls": len(self._controls),
702
+ "implemented": eu_implemented + iso_implemented,
703
+ "compliance_score": round(
704
+ (eu_implemented + iso_implemented)
705
+ / len(self._controls)
706
+ * 100,
707
+ 1,
708
+ )
709
+ if self._controls
710
+ else 0,
711
+ },
712
+ }
713
+
714
+ if risk_assessment:
715
+ report["risk_assessment"] = risk_assessment.to_dict()
716
+
717
+ if session_stats:
718
+ report["session_stats"] = session_stats
719
+
720
+ return report
721
+
722
+ def generate_technical_documentation(
723
+ self,
724
+ transparency: TransparencyDeclaration | None = None,
725
+ risk_assessment: RiskAssessment | None = None,
726
+ ) -> dict[str, Any]:
727
+ """Generate EU AI Act Art. 11 technical documentation.
728
+
729
+ Returns structured documentation suitable for submission to
730
+ national competent authorities.
731
+ """
732
+ from crp._version import __version__
733
+
734
+ doc = {
735
+ "document_type": "technical_documentation",
736
+ "document_version": "1.0",
737
+ "generated_at": time.time(),
738
+ "system": {
739
+ "name": "Context Relay Protocol (CRP)",
740
+ "version": __version__,
741
+ "provider": "AutoCyber AI Pty Ltd",
742
+ "provider_jurisdiction": "NSW, Australia",
743
+ "license": "Elastic License 2.0",
744
+ },
745
+ "intended_purpose": (
746
+ transparency.intended_purpose
747
+ if transparency
748
+ else "AI context management for LLM applications"
749
+ ),
750
+ "risk_classification": (
751
+ risk_assessment.to_dict()
752
+ if risk_assessment
753
+ else {"risk_level": "minimal", "category": "context_management"}
754
+ ),
755
+ "architecture": {
756
+ "type": "Context management middleware",
757
+ "components": [
758
+ "Extraction pipeline (6-stage graduated NLP)",
759
+ "Warm state store (in-memory fact storage)",
760
+ "Contextual Knowledge Fabric (4-mode retrieval)",
761
+ "Envelope builder (6-phase context assembly)",
762
+ "Security layer (12 modules, 8-layer defense)",
763
+ "Observability layer (audit, events, telemetry, metrics)",
764
+ ],
765
+ "dependencies": {
766
+ "core": "Zero external dependencies",
767
+ "optional": "cryptography, blake3, keyring, sentence-transformers",
768
+ },
769
+ },
770
+ "data_governance": {
771
+ "data_classification_levels": 5,
772
+ "pii_detection": "Pattern-based with configurable rules",
773
+ "data_retention": "Configurable per classification level",
774
+ "data_minimization": "Session-scoped, auto-purge on expiry",
775
+ "right_to_erasure": "GDPR Article 17 compliant",
776
+ "consent_management": "Purpose-based with 8 processing purposes",
777
+ },
778
+ "security_measures": {
779
+ "encryption": "AES-256-GCM (NIST SP 800-38D)",
780
+ "key_derivation": "HMAC-SHA256 + HKDF-SHA256 (RFC 5869)",
781
+ "session_binding": "Cryptographic per-session isolation",
782
+ "integrity": "BLAKE3/SHA-256 hash chains with HMAC signing",
783
+ "access_control": "RBAC (OBSERVER/OPERATOR/ADMIN)",
784
+ "input_validation": "Always-on, cannot be disabled",
785
+ "injection_detection": "21 patterns + ML classifiers (advisory)",
786
+ "anti_poisoning": "1-window quarantine with 0.7× confidence penalty",
787
+ "embedding_protection": "SQ8 quantization + XOR salting",
788
+ "audit_trail": "Tamper-evident HMAC-signed compliance logging",
789
+ },
790
+ "human_oversight": {
791
+ "levels": ["NONE", "INFORMED", "APPROVAL", "CONTROL"],
792
+ "configurable_per_operation": True,
793
+ "halt_mechanisms": ["injection_detected", "pii_detected"],
794
+ "autonomous_limits": "Configurable max dispatches",
795
+ },
796
+ "transparency": (
797
+ transparency.to_dict()
798
+ if transparency
799
+ else {"note": "Generate with TransparencyDeclaration"}
800
+ ),
801
+ "compliance_controls": [
802
+ {
803
+ "control_id": c.control_id,
804
+ "framework": c.framework,
805
+ "article": c.article,
806
+ "description": c.description,
807
+ "status": c.status,
808
+ }
809
+ for c in self._controls
810
+ ],
811
+ }
812
+
813
+ return doc