Metadata-Version: 2.4
Name: coreason_aegis
Version: 0.4.0
Summary: bidirectional privacy filter that sits between the CoReason platform and the external world
License: # The Prosperity Public License 3.0.0
         
         Contributor: CoReason, Inc.
         
         Source Code: https://github.com/CoReason-AI/coreason_aegis
         
         ## Purpose
         
         This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
         
         ## Agreement
         
         In order to receive this license, you have to agree to its rules.  Those rules are both obligations under that agreement and conditions to your license.  Don't do anything with this software that triggers a rule you can't or won't follow.
         
         ## Notices
         
         Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
         
         ## Commercial Trial
         
         Limit your use of this software for commercial purposes to a thirty-day trial period.  If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
         
         ## Contributions Back
         
         Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
         
         ## Personal Uses
         
         Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
         
         ## Noncommercial Organizations
         
         Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
         
         ## Defense
         
         Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
         
         ## Copyright
         
         The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
         
         ## Patent
         
         The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
         
         ## Reliability
         
         The contributor can't revoke this license.
         
         ## Excuse
         
         You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
         
         ## No Liability
         
         ***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
License-File: LICENSE
License-File: NOTICE
Author: Gowtham A Rao
Author-email: gowtham.rao@coreason.ai
Requires-Python: >=3.11
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Requires-Dist: aiofiles (>=25.1.0,<26.0.0)
Requires-Dist: anyio (>=4.12.1,<5.0.0)
Requires-Dist: cachetools (>=6.2.6,<7.0.0)
Requires-Dist: coreason-identity (>=0.4.2,<0.5.0)
Requires-Dist: faker (>=40.1.2,<41.0.0)
Requires-Dist: fastapi (>=0.115.0,<0.116.0)
Requires-Dist: httpx (>=0.28.1,<0.29.0)
Requires-Dist: jaraco-context (>=6.1.0,<7.0.0)
Requires-Dist: loguru (>=0.7.2,<0.8.0)
Requires-Dist: presidio-analyzer (>=2.2.360,<3.0.0)
Requires-Dist: presidio-anonymizer (>=2.2.360,<3.0.0)
Requires-Dist: pydantic (>=2.12.5,<3.0.0)
Requires-Dist: pydantic-settings (>=2.12.0,<3.0.0)
Requires-Dist: types-aiofiles (>=25.1.0.20251011,<26.0.0.0)
Requires-Dist: uvicorn[standard] (>=0.27.0,<0.28.0)
Project-URL: Documentation, https://github.com/CoReason-AI/coreason_aegis
Project-URL: Homepage, https://github.com/CoReason-AI/coreason_aegis
Project-URL: Repository, https://github.com/CoReason-AI/coreason_aegis
Description-Content-Type: text/markdown

# coreason-aegis

A bidirectional privacy filter that sits between the CoReason platform and the external world.

[![License](https://img.shields.io/badge/license-Prosperity%203.0-blue)](https://github.com/CoReason-AI/coreason_aegis)
[![CI](https://github.com/CoReason-AI/coreason_aegis/actions/workflows/ci.yml/badge.svg)](https://github.com/CoReason-AI/coreason_aegis/actions/workflows/ci.yml)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Docs](https://img.shields.io/badge/docs-requirements-green)](docs/product_requirements.md)
[![Docs](https://img.shields.io/badge/docs-usage-blue)](docs/usage.md)

**coreason-aegis** implements a **"Tokenize & Detokenize"** strategy. Before any text leaves the secure perimeter (e.g., to OpenAI), Aegis scans it for sensitive entities (Names, MRNs, SSNs, Emails) and replaces them with consistent, context-aware tokens. When the LLM responds using these tokens, Aegis intercepts the message and re-injects the real data *only* for the authorized user's eyes.

## Features

*   **Async-Native with Sync Facade:** Designed for high-performance async environments with a convenient synchronous wrapper.
*   **Scan-Mask-Map-Reveal Loop:** Deterministic tokenization ensures LLM reasoning consistency.
*   **The "Vault" of Identity:** Ephemeral, encrypted mapping tables.
*   **Context Preservation:** Uses tokens like `[PATIENT_A]` instead of `[REDACTED]`.
*   **Custom Recognizers:** Supports custom entities like MRNs, Protocol IDs, and Lot Numbers.

For detailed documentation, see:
*   [Product Requirements](docs/product_requirements.md)
*   [Usage Guide](docs/usage.md)
*   [Requirements & Dependencies](docs/requirements.md)

## Installation

```bash
pip install coreason-aegis
```

Or using Poetry:

```bash
poetry add coreason-aegis
```

*Note: You may need to download the Spacy model manually if not handled by the package installer:*

```bash
python -m spacy download en_core_web_lg
```

## Usage

```python
from coreason_aegis.main import Aegis
from coreason_aegis.models import AegisPolicy, RedactionMode
from coreason_identity.models import UserContext
from coreason_identity.types import SecretStr

# Initialize Aegis
aegis = Aegis()

# Create User Context (Required)
context = UserContext(
    user_id=SecretStr("user_123"),
    roles=["analyst"]
)

# Define a policy
policy = AegisPolicy(
    allow_list=["Tylenol"],
    entity_types=["PERSON", "EMAIL", "PHONE_NUMBER"],
    mode=RedactionMode.REPLACE,
    confidence_score=0.7
)

# Sanitize user prompt
user_prompt = "Patient John Doe (DOB: 12/01/1980) has a rash."
session_id = "session_123"

sanitized_prompt, deid_map = aegis.sanitize(
    text=user_prompt,
    session_id=session_id,
    context=context,
    policy=policy
)

print(f"Sanitized: {sanitized_prompt}")
# Output: "Patient [PATIENT_A] (DOB: [DATE_B]) has a rash."

# ... Send to LLM ...
llm_response = "For [PATIENT_A], considering the rash..."

# Desanitize LLM response
final_response = aegis.desanitize(
    text=llm_response,
    session_id=session_id,
    context=context,
    authorized=True
)

print(f"Final: {final_response}")
# Output: "For John Doe, considering the rash..."
```

For more examples, see the [Usage Guide](docs/usage.md).

