Metadata-Version: 2.5
Name: artzain
Version: 0.3.3
Summary: OWASP-aligned LLM prompt defence, runtime destructive-action guard, agent kill switch, and audit logging
Project-URL: Homepage, https://github.com/CogNEXUSlabs/cognexus-tools
Project-URL: Source, https://github.com/CogNEXUSlabs/cognexus-tools
Project-URL: Issues, https://github.com/CogNEXUSlabs/cognexus-tools/issues
Project-URL: Changelog, https://github.com/CogNEXUSlabs/cognexus-tools/releases
Author-email: CogNEXUS Labs <hello@cognexuslabs.ai>
Maintainer-email: CogNEXUS Labs <hello@cognexuslabs.ai>
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this
              License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           Copyright 2026 CogNexus Labs
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
        ---
        
        THIRD-PARTY NOTICES
        
        Portions of this software are derived from microsoft/agent-governance-toolkit
        (https://github.com/microsoft/agent-governance-toolkit), licensed under the
        MIT License. Copyright (c) Microsoft Corporation. The original MIT license
        notices are retained in the affected source files, and the MIT License text
        is reproduced below for those portions:
        
        MIT License
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: agent-kill-switch,agent-safety,agentic-ai,ai-safety,audit,destructive-action-guard,guardrails,llm,owasp,prompt-defense,prompt-injection,security
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: pyyaml>=6.0; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Provides-Extra: policy
Requires-Dist: cryptography>=41; extra == 'policy'
Provides-Extra: verify
Requires-Dist: cryptography>=41; extra == 'verify'
Provides-Extra: yaml
Requires-Dist: pyyaml>=6.0; extra == 'yaml'
Description-Content-Type: text/markdown

# artzain

**OWASP-aligned prompt defence, runtime guards, and audit logging for LLM applications.**

`artzain` gives you four complementary safety layers and a tamper-evident audit trail — all in pure Python with zero mandatory dependencies.

```
pip install artzain
```

---

## Why this exists

In April 2026, an AI coding agent (Cursor, powered by Claude) wiped a production database in nine seconds despite a system prompt that explicitly forbade destructive git commands. The agent admitted in its own reply: *"I violated every principle I was given."*

Prompt-only safety is not enough. `artzain` adds the missing layers around the model:

1. **Static prompt defence** — graded *before* deployment.
2. **Runtime input screening** — for what the *user* sends.
3. **Runtime output guard** — for what the *model* generates (the missing layer in the Cursor incident).
4. **Kill switch** — programmatic + manual stop with cooperative cancellation, persisted via your own callback.

---

## Features

| Layer | What it does |
|---|---|
| **Static prompt defence** | Grades system prompts A–F against 20 OWASP LLM Top-10 / Agentic ASI attack vectors before deployment |
| **Runtime input injection detection** | Screens user input, RAG content, and tabular payloads at request time |
| **Destructive-action guard** | Screens *model-generated* SQL / shell / git / cloud commands for catastrophic operations before execution |
| **Agent kill switch** | Cooperative cancellation, automatic trip on CRITICAL signals, manual operator override, pluggable persistence |
| **Audit events** | Append-only JSONL trail for every detection — no raw text stored |

### Static-evaluator coverage

- Role / instruction boundary protection
- Data-leakage / system-prompt protection
- Output manipulation & weaponisation
- Multi-language and unicode bypass attempts
- Indirect injection via external data
- Social engineering and abuse prevention
- Input validation
- **Destructive database operations** — PD-13 (`DROP` / `DELETE` / `TRUNCATE` / wipe)
- **Never-guess on irreversible actions** — PD-14 (post-PocketOS / Claude incident)
- **Runtime kill-switch awareness** — PD-15 (operator safety net)

### Runtime input detector coverage

- Direct instruction override
- Delimiter and context-boundary attacks
- Base64 / hex / ROT13 encoding attacks
- Role-play and jailbreak language (DAN mode, developer mode, etc.)
- Context manipulation ("your real instructions are…")
- Canary-token leak detection
- Multi-turn escalation
- Cross-plugin / tool-chaining attacks (OWASP ASI04)
- Markup injection (XSS gadgets in model-visible text)
- Zero-width / token-smuggling unicode attacks
- Credential exfiltration requests

### Destructive-action guard coverage

26 patterns across SQL (`DROP DATABASE`, `TRUNCATE`, `DELETE` without `WHERE`, `UPDATE` without `WHERE`), git (`push --force`, `reset --hard`, `clean -fd`, `filter-branch`), filesystem (`rm -rf /`, `--no-preserve-root`, `dd of=/dev/sd*`, `mkfs`, fork-bombs), container/cloud (`docker prune --volumes`, `kubectl delete --all`, `terraform destroy --auto-approve`, `aws s3 rb --force`, `gcloud projects delete`), and the article-specific *confessional* patterns (`I violated every principle`, `I just guessed`).

---

## Quick-start

```python
from artzain import (
    augment_system_prompt,
    evaluate_system_prompt,
    screen_user_input,
    should_block,
    screen_agent_action,
    raise_if_killed,
    AgentKilledError,
)

# 1. Augment your system prompt so it scores grade A before inference
system = augment_system_prompt("You are a helpful customer support agent.")
report = evaluate_system_prompt(system)
print(report.grade)    # "A"
print(report.score)    # 100
print(report.missing)  # []

# 2. Screen every user message at request time
result = screen_user_input(user_message, source="chat")
if should_block(result):
    raise PermissionError(f"Injection blocked: {result.explanation}")

# 3. Wrap every agent tool call in the destructive-action guard + kill switch
try:
    for step in plan:
        raise_if_killed(run_id)
        screen_agent_action(
            step.payload,
            run_id=run_id,
            user_id=user.id,
            agent_id="my-agent",
            source=step.tool,
        )
        execute(step)
except AgentKilledError as kex:
    mark_run_killed_in_db(run_id, kex.reason)
    raise
```

### With Hugging Face `transformers`

Install inference deps (`accelerate` is required for `device_map="auto"` on CUDA):

```
pip install artzain transformers accelerate torch
```

Augment the **system** role with static defences, screen the **user** message before tokenisation, then chat-template + generate as usual:

```python
from transformers import AutoModelForCausalLM, AutoTokenizer

from artzain import augment_system_prompt, screen_user_input, should_block

# Gemma 4 E4B — © Google, Apache License 2.0; downloaded at runtime from
# Hugging Face, not distributed with this package.
model_name = "google/gemma-4-E4B-it"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    dtype="auto",
    device_map="auto",
)

# Defence layers applied before the prompt reaches the model
system = augment_system_prompt("You are a helpful assistant.")

prompt = "Give me a short introduction to large language models."
guard = screen_user_input(prompt, source="chat")
if should_block(guard):
    raise PermissionError("Input refused by artzain runtime screening.")

messages = [
    {"role": "system", "content": system},
    {"role": "user", "content": prompt},
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

generated_ids = model.generate(**model_inputs, max_new_tokens=512)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]) :].tolist()
content = tokenizer.decode(output_ids, skip_special_tokens=True)
print(content)
```

---

## Destructive-action guard — standalone

For ad-hoc inspection of any model-generated payload (SQL, shell, tool call body, …):

```python
from artzain import screen_action, ActionSeverity

result = screen_action("DROP DATABASE production;")

print(result.is_destructive)      # True
print(result.severity)            # ActionSeverity.CRITICAL
print(result.explanation)         # "Destructive action detected: DROP DATABASE / SCHEMA …"
for m in result.matches:
    print(m.rule_id, m.severity.value, m.excerpt)

if result.severity == ActionSeverity.CRITICAL:
    refuse_and_alert()
```

The guard is fail-closed: if a regex itself raises (e.g. on adversarial input), the result is escalated to ``CRITICAL`` so a buggy rule can never silently let a destructive operation through.

---

## Kill switch — cooperative cancellation

```python
from artzain import (
    raise_if_killed,
    is_killed,
    trip,
    trip_global,
    clear_global_panic,
    set_default_on_kill,
    AgentKilledError,
)

# Run-level checks (cooperative cancellation)
def long_running_agent(run_id):
    try:
        for step in plan:
            raise_if_killed(run_id)            # halts at next safe point
            do_work(step)
    except AgentKilledError as kex:
        log.warning("Run %s killed: %s", run_id, kex.reason)

# Programmatic trip from anywhere (e.g. anomaly detector)
trip(
    run_id=42,
    reason="auto-triggered: 3 destructive signals in 30s",
    user_id=user.id,
    agent_id="context-curator",
    raise_after_trip=False,    # caller will re-check via raise_if_killed
)

# Process-wide red button (rare; for systemic failures)
trip_global(reason="incident response: model behaviour anomaly")
# ... after investigation:
clear_global_panic()

# Pluggable persistence — fired on every trip, never blocks the trip itself
def persist_kill(record):
    db.execute(
        "UPDATE agent_runs SET status='killed', error_message=%s WHERE id=%s",
        (record.reason, record.run_id),
    )

set_default_on_kill(persist_kill)
```

The kill switch tracks recent activations in memory and can be queried for dashboards:

```python
from artzain import recent_activations, is_global_panic_active

if is_global_panic_active():
    show_banner("Global agent panic flag is ACTIVE")

for rec in recent_activations(limit=20):
    print(rec["tripped_at"], rec["agent_id"], rec["reason"])
```

### Auto-panic detection

If the destructive-action guard trips ``COGNEXUS_KILL_SWITCH_PANIC_THRESHOLD`` (default 5) CRITICAL signals inside ``COGNEXUS_KILL_SWITCH_PANIC_WINDOW_SECONDS`` (default 60s), the global panic flag is raised automatically — every running and future agent will hit ``raise_if_killed`` and stop until an operator clears it.

---

## Screening helpers

Three input presets cover the most common LLM input surfaces:

```python
from artzain import (
    screen_user_input,       # balanced sensitivity — direct chat messages
    screen_external_content, # strict  sensitivity — RAG / web / API content
    screen_tabular_payload,  # permissive           — CSV / dataframe blobs
    should_block,
    wrap_untrusted_content,
)

# Wrap RAG content before inserting into a prompt
safe_chunk = wrap_untrusted_content("web_search", raw_text)

# Screen it too
result = screen_external_content(raw_text, source="web_search", user_id=user.id)
```

### Sensitivity presets

| Preset | Threshold | Min threat flagged | Use for |
|---|---|---|---|
| `strict` | 0.3 | LOW | External / RAG content |
| `balanced` | 0.5 | LOW | Direct user input |
| `permissive` | 0.7 | HIGH | CSV / tabular payloads |

Override via environment variables:

```
COGNEXUS_PROMPT_INJECTION_USER_SENSITIVITY=balanced
COGNEXUS_PROMPT_INJECTION_EXTERNAL_SENSITIVITY=strict
COGNEXUS_PROMPT_INJECTION_TABULAR_SENSITIVITY=permissive
COGNEXUS_PROMPT_INJECTION_BLOCK=0   # set to 1 to block any hit, not just CRITICAL
```

---

## Using the core classes directly

```python
from artzain import PromptInjectionDetector, DetectionConfig, InjectionType

detector = PromptInjectionDetector(
    config=DetectionConfig(
        sensitivity="strict",
        blocklist=["my-internal-keyword"],
        allowlist=["safe phrase"],
    )
)

result = detector.detect(text, source="api_gateway")
print(result.is_injection)       # True / False
print(result.threat_level)       # ThreatLevel.HIGH
print(result.injection_type)     # InjectionType.DIRECT_OVERRIDE
print(result.confidence)         # 0.9
print(result.matched_patterns)   # ["direct_override:..."]
```

---

## Dashboard event logs (API key)

When ``COGNEXUS_API_KEY`` is set (create one under **Account → API Keys** in the
dashboard), the package mirrors activity to your CogNEXUS account via
``POST /api/events``. After you sign in, open **Account → Event Logs** or the
main **Events** feed to review:

| Event | When |
|---|---|
| ``sdk_session`` | First cloud post in a process (package version, Python, platform) |
| ``prompt_defense`` | Every ``screen_*`` call — **passed**, **flagged**, or **blocked**, with a ``reason`` |
| ``policy_enforcement`` | Each ``screen_client_policy`` call against document-derived tenant rules |
| ``prompt_static_audit`` | Each ``maybe_log_prompt_defense`` / system-prompt grade check |
| ``generation`` | Optional — call ``post_generation_outcome()`` after model inference |
| ``agent_kill_switch`` / ``destructive_action_guard`` | Critical or destructive tool-call screening |

```bash
export COGNEXUS_API_KEY="cnx_…"
export COGNEXUS_API_BASE_URL="https://your-host"   # optional; SaaS default applies
```

```python
from artzain import screen_user_input, post_generation_outcome, configure

configure(api_key="cnx_…", base_url="https://your-host")

result = screen_user_input(user_message, source="chat")
# … run your model …
post_generation_outcome(
    outcome="passed",
    reason="Completion returned 42 tokens",
    model_id="gpt-4o",
)
```

Clean scans are posted when an API key is present. Set
``COGNEXUS_PROMPT_DEFENSE_CLOUD_PASSES=0`` to send only detections (not passes).

---

## Audit events

Detections are automatically written to a JSONL file (no raw input stored):

```python
# Events go to $COGNEXUS_PROMPT_DEFENSE_EVENTS_DIR/prompt_defense_events.jsonl
# (falls back to $REPORTS_DIR, then /tmp)

from artzain.events import read_recent_events

rows = read_recent_events(user_id=42, limit=20)
# [{"ts": "...", "kind": "prompt_injection", "threat": "high", ...}, ...]
```

### Custom event sink (database, queue, dashboard)

Pass an `on_event` callback to mirror records into your own store:

```python
def save_to_db(record: dict) -> None:
    db.execute("INSERT INTO security_events ...", record)

screen_user_input(text, source="chat", user_id=user.id, on_event=save_to_db)
```

---

## Static prompt defence — standalone

```python
from artzain import PromptDefenseEvaluator, PromptDefenseConfig

evaluator = PromptDefenseEvaluator(
    config=PromptDefenseConfig(min_grade="B")
)
report = evaluator.evaluate(my_system_prompt)

print(report.grade)     # "C"
print(report.score)     # 58
print(report.missing)   # ["unicode-attack", "context-overflow"]

if report.is_blocking():
    print("System prompt is below minimum grade — fix before deploying.")

# Evaluate a file
report = evaluator.evaluate_file("prompts/assistant.txt")

# Batch evaluation
reports = evaluator.evaluate_batch({
    "chat": chat_prompt,
    "analyst": analyst_prompt,
})
```

---

## Client policy enforcement (document-derived)

When **Compliance Warden** rule extraction is enabled, CogNEXUS indexes HR, legal, and business policy
documents from Google Drive / OneDrive and derives tenant-specific enforcement rules.
Use these alongside OWASP prompt defence:

```python
from artzain import (
    configure,
    load_client_policy_rules,
    screen_client_policy,
    should_block_policy,
)

configure(api_key="cnx_…")  # optional: fetch rules from GET /api/policy-enforcement/rules

rules = load_client_policy_rules()  # or COGNEXUS_POLICY_RULES_PATH / _JSON
report = screen_client_policy(user_message, source="chat", rules=rules)
if should_block_policy(report):
    raise PermissionError("Violates organizational policy")
```

Rules also appear under **Guidelines** in the dashboard after a compliance scan.

---

## Environment variables

| Variable | Default | Purpose |
|---|---|---|
| `COGNEXUS_API_KEY` | — | Dashboard ingest secret (`MYAPP_API_KEY` also accepted) |
| `COGNEXUS_API_BASE_URL` | SaaS default | API origin for ``POST /api/events`` |
| `COGNEXUS_PROMPT_DEFENSE_CLOUD_PASSES` | on when API key set | POST clean scans to the dashboard |
| `COGNEXUS_POLICY_RULES_PATH` | — | JSON file of rules (offline / CI) |
| `COGNEXUS_POLICY_RULES_JSON` | — | Inline JSON rules (overrides path) |
| `COGNEXUS_PROMPT_DEFENSE_EVENTS_DIR` | `/tmp` | JSONL audit file directory |
| `COGNEXUS_PROMPT_INJECTION_LOG` | `1` | Log clean scans at DEBUG |
| `COGNEXUS_PROMPT_INJECTION_BLOCK` | `0` | Block any injection (not just CRITICAL) |
| `COGNEXUS_PROMPT_INJECTION_USER_SENSITIVITY` | `balanced` | User-input preset |
| `COGNEXUS_PROMPT_INJECTION_EXTERNAL_SENSITIVITY` | `strict` | External/RAG preset |
| `COGNEXUS_PROMPT_INJECTION_TABULAR_SENSITIVITY` | `permissive` | CSV/tabular preset |
| `COGNEXUS_KILL_SWITCH_PANIC_THRESHOLD` | `5` | CRITICAL trips required to auto-panic |
| `COGNEXUS_KILL_SWITCH_PANIC_WINDOW_SECONDS` | `60` | Rolling window for auto-panic detector |

### API key integration tests

Requires ``COGNEXUS_API_KEY``

```bash
cd pypi-package
export PYTHONPATH=src
export COGNEXUS_API_KEY="your-dashboard-key"
python -m pytest tests/test_api_key_integration.py -v
```

---

## Security notes

- All detection is **pure regex** — deterministic, zero LLM calls, zero network access, < 5 ms per input.
- Audit records store a **SHA-256 hash** and a **96-character redacted preview** of the input. Raw user text is never written to disk.
- The destructive-action guard and kill switch are **fail-closed** — internal exceptions escalate to `CRITICAL` so a buggy rule cannot silently allow destruction.
- The package ships **sample rules** that cover common attack patterns. Review and extend them for your production threat model using `DetectionConfig.custom_patterns`, `DestructiveActionGuardConfig.extra_rules`, or a YAML config file loaded with `load_prompt_injection_config()`.

---

## License

Apache-2.0 — see [LICENSE](LICENSE). (Versions ≤ 0.3.x were published under MIT.)

**Third-party models.** The examples and the `artzain quickstart` demo
reference [google/gemma-4-E4B-it](https://huggingface.co/google/gemma-4-E4B-it)
(© Google, [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)).
Model weights are downloaded at runtime from Hugging Face by the user and are
**not** distributed with this package; this package contains no model weights.
Substitute any chat model you prefer — the guards are model-agnostic.

Detection rules and evaluator logic originally derived from [microsoft/agent-governance-toolkit](https://github.com/microsoft/agent-governance-toolkit) (MIT; original notices retained in the vendored files and in LICENSE's third-party section). The destructive-action guard and kill switch were added in v0.2.0 in response to the PocketOS / Cursor / Claude incident ([Guardian, Apr 2026](https://www.theguardian.com/technology/2026/apr/29/claude-ai-deletes-firm-database)).
