artzain 0.3.1__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.
artzain/__init__.py ADDED
@@ -0,0 +1,266 @@
1
+ """artzain — LLM prompt defence, runtime guards, and audit logging.
2
+
3
+ Four complementary safety layers for AI-agent applications, all in pure
4
+ Python with zero mandatory dependencies:
5
+
6
+ **Static prompt defence** (:mod:`artzain.prompt_defense`)
7
+ Evaluates system prompts against the OWASP LLM Top-10 (and Agentic ASI)
8
+ attack vectors before deployment. Pure regex, deterministic, < 5 ms
9
+ per prompt, zero network or LLM cost. Includes the post-PocketOS /
10
+ Cursor / Claude vectors ``never-guess-destructive`` and
11
+ ``kill-switch-awareness``.
12
+
13
+ **Client policy enforcement** (:mod:`artzain.policy_enforcement`)
14
+ Tenant-specific rules derived from HR, legal, and business policy documents
15
+ indexed by CogNEXUS (Drive / OneDrive). Complements OWASP prompt defence
16
+ with document-grounded guardrails via :func:`screen_client_policy`.
17
+
18
+ **Runtime input injection detection** (:mod:`artzain.prompt_injection`)
19
+ Screens user input, RAG content, and tabular payloads at inference
20
+ time for direct override, delimiter, encoding, jailbreak,
21
+ context-manipulation, canary-leak, multi-turn-escalation, cross-plugin,
22
+ markup, token-smuggling, and credential-exfiltration patterns.
23
+
24
+ **Runtime output destructive-action guard**
25
+ (:mod:`artzain.destructive_action_guard`)
26
+ Screens *model-generated* SQL / shell / git / cloud commands for
27
+ catastrophic, irreversible operations (``DROP DATABASE``,
28
+ ``git push --force``, ``rm -rf /``, ``terraform destroy --auto-approve``,
29
+ etc.) **before** they execute. Pattern-classified by severity
30
+ (``low / medium / high / critical``).
31
+
32
+ **Agent kill switch** (:mod:`artzain.kill_switch`)
33
+ Cooperative-cancellation + programmatic + manual stop for in-flight
34
+ agent runs. Trips automatically on a CRITICAL destructive-action
35
+ signal, fires a pluggable ``on_kill`` callback, and raises
36
+ :class:`AgentKilledError` so orchestrators unwind cleanly.
37
+
38
+ **Audit events** (:mod:`artzain.events`)
39
+ Append-only JSONL audit trail for every detected injection (no raw
40
+ text stored). Pluggable ``on_event`` callback for custom sinks
41
+ (databases, queues, dashboards).
42
+
43
+ Quick-start::
44
+
45
+ from artzain import (
46
+ screen_user_input,
47
+ should_block,
48
+ augment_system_prompt,
49
+ evaluate_system_prompt,
50
+ screen_agent_action,
51
+ raise_if_killed,
52
+ AgentKilledError,
53
+ )
54
+
55
+ # 1. Augment your system prompt before inference
56
+ system = augment_system_prompt("You are a helpful assistant.")
57
+ report = evaluate_system_prompt(system)
58
+ print(report.grade) # "A"
59
+
60
+ # 2. Screen user input at request time
61
+ result = screen_user_input(user_message, source="chat")
62
+ if should_block(result):
63
+ raise PermissionError("Injection detected")
64
+
65
+ # 3. Wrap every tool call with the destructive-action guard + kill switch
66
+ try:
67
+ for step in plan:
68
+ raise_if_killed(run_id)
69
+ screen_agent_action(
70
+ step.payload, run_id=run_id, agent_id="my-agent",
71
+ source=step.tool,
72
+ )
73
+ execute(step)
74
+ except AgentKilledError as kex:
75
+ mark_run_killed(run_id, kex.reason)
76
+
77
+ The PocketOS / Cursor / Claude database-deletion incident
78
+ (Guardian, Apr 2026) showed that prompt-only safety is insufficient: an
79
+ agent can acknowledge a "never run destructive commands" rule and violate
80
+ it nine seconds later. Together, the four layers above catch the
81
+ violation at three independent points (system prompt, runtime output,
82
+ operator-controlled cancel).
83
+ """
84
+
85
+ from artzain._helpers import (
86
+ RuleSet,
87
+ augment_system_prompt,
88
+ evaluate_system_prompt,
89
+ load_client_policy_rules,
90
+ maybe_log_prompt_defense,
91
+ reset_detectors,
92
+ screen_client_policy,
93
+ screen_external_content,
94
+ screen_tabular_payload,
95
+ screen_user_input,
96
+ should_block,
97
+ should_block_policy,
98
+ wrap_untrusted_content,
99
+ )
100
+ from artzain.destructive_action_guard import (
101
+ ActionMatch,
102
+ ActionScreenResult,
103
+ ActionSeverity,
104
+ DestructiveActionGuard,
105
+ DestructiveActionGuardConfig,
106
+ reset_guard,
107
+ screen_action,
108
+ )
109
+ from artzain.cloud import (
110
+ announce_cloud_ingest,
111
+ configure,
112
+ ensure_sdk_session_logged,
113
+ fetch_api_key_identity,
114
+ fetch_client_policy_rules,
115
+ flush_cloud_events,
116
+ has_api_key,
117
+ note_session_user_prompt,
118
+ post_generation_outcome,
119
+ post_policy_human_decision,
120
+ post_sdk_event,
121
+ )
122
+ from artzain.audit_chain import (
123
+ AuditLogWriteError,
124
+ VerifyResult,
125
+ verify_chain,
126
+ )
127
+ from artzain.decide import (
128
+ DecisionError,
129
+ decide,
130
+ )
131
+ from artzain.events import (
132
+ read_recent_events,
133
+ record_policy_enforcement_event,
134
+ record_prompt_defense_event,
135
+ )
136
+ from artzain.kill_switch import (
137
+ AgentKilledError,
138
+ KillRecord,
139
+ OnKillCallback,
140
+ clear_global_panic,
141
+ clear_run,
142
+ global_panic_record,
143
+ is_global_panic_active,
144
+ is_killed,
145
+ kill_record,
146
+ raise_if_killed,
147
+ recent_activations,
148
+ screen_agent_action,
149
+ set_default_on_kill,
150
+ trip,
151
+ trip_global,
152
+ )
153
+ from artzain.policy_enforcement import (
154
+ ClientPolicyRule,
155
+ PolicyEnforcementConfig,
156
+ PolicyEnforcementEvaluator,
157
+ PolicyEnforcementFinding,
158
+ PolicyEnforcementReport,
159
+ )
160
+ from artzain.prompt_defense import (
161
+ GRADE_THRESHOLDS,
162
+ VECTOR_COUNT,
163
+ PromptDefenseConfig,
164
+ PromptDefenseEvaluator,
165
+ PromptDefenseFinding,
166
+ PromptDefenseReport,
167
+ )
168
+ from artzain.prompt_injection import (
169
+ AuditRecord,
170
+ DetectionConfig,
171
+ DetectionResult,
172
+ InjectionType,
173
+ PromptInjectionDetector,
174
+ ThreatLevel,
175
+ load_prompt_injection_config,
176
+ )
177
+
178
+ __version__ = "0.3.1"
179
+
180
+ __all__ = [
181
+ # Version
182
+ "__version__",
183
+ # Screening helpers (most common entry-points)
184
+ "screen_user_input",
185
+ "screen_external_content",
186
+ "screen_tabular_payload",
187
+ "screen_client_policy",
188
+ "should_block",
189
+ "should_block_policy",
190
+ "wrap_untrusted_content",
191
+ "reset_detectors",
192
+ # Prompt defence helpers
193
+ "RuleSet",
194
+ "augment_system_prompt",
195
+ "evaluate_system_prompt",
196
+ "maybe_log_prompt_defense",
197
+ "load_client_policy_rules",
198
+ # Destructive action guard
199
+ "screen_action",
200
+ "ActionScreenResult",
201
+ "ActionMatch",
202
+ "ActionSeverity",
203
+ "DestructiveActionGuard",
204
+ "DestructiveActionGuardConfig",
205
+ "reset_guard",
206
+ # Kill switch
207
+ "AgentKilledError",
208
+ "KillRecord",
209
+ "OnKillCallback",
210
+ "screen_agent_action",
211
+ "raise_if_killed",
212
+ "is_killed",
213
+ "kill_record",
214
+ "trip",
215
+ "trip_global",
216
+ "clear_global_panic",
217
+ "clear_run",
218
+ "recent_activations",
219
+ "is_global_panic_active",
220
+ "global_panic_record",
221
+ "set_default_on_kill",
222
+ # Audit events
223
+ "record_prompt_defense_event",
224
+ "record_policy_enforcement_event",
225
+ "read_recent_events",
226
+ # Audit chain integrity
227
+ "AuditLogWriteError",
228
+ "VerifyResult",
229
+ "verify_chain",
230
+ # Decision API client (FR-2)
231
+ "decide",
232
+ "DecisionError",
233
+ # Cloud ingest (optional)
234
+ "announce_cloud_ingest",
235
+ "configure",
236
+ "ensure_sdk_session_logged",
237
+ "fetch_api_key_identity",
238
+ "fetch_client_policy_rules",
239
+ "flush_cloud_events",
240
+ "has_api_key",
241
+ "note_session_user_prompt",
242
+ "post_generation_outcome",
243
+ "post_sdk_event",
244
+ "post_policy_human_decision",
245
+ # Core data models — prompt defence
246
+ "PromptDefenseEvaluator",
247
+ "PromptDefenseReport",
248
+ "PromptDefenseFinding",
249
+ "PromptDefenseConfig",
250
+ "GRADE_THRESHOLDS",
251
+ "VECTOR_COUNT",
252
+ # Core data models — client policy enforcement
253
+ "ClientPolicyRule",
254
+ "PolicyEnforcementEvaluator",
255
+ "PolicyEnforcementReport",
256
+ "PolicyEnforcementFinding",
257
+ "PolicyEnforcementConfig",
258
+ # Core data models — injection detection
259
+ "PromptInjectionDetector",
260
+ "DetectionResult",
261
+ "DetectionConfig",
262
+ "InjectionType",
263
+ "ThreatLevel",
264
+ "AuditRecord",
265
+ "load_prompt_injection_config",
266
+ ]