Metadata-Version: 2.4
Name: coreason_sentinel
Version: 0.1.0
Summary: alerts you when things go wrong in production
License: # The Prosperity Public License 3.0.0
         
         Contributor: CoReason, Inc.
         
         Source Code: https://github.com/CoReason-AI/coreason_sentinel
         
         ## 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
Requires-Dist: anyio
Requires-Dist: fastapi (>=0.128.0,<0.129.0)
Requires-Dist: httpx (>=0.28.1,<0.29.0)
Requires-Dist: loguru (>=0.7.2,<0.8.0)
Requires-Dist: numpy (>=2.4.1,<3.0.0)
Requires-Dist: pydantic (>=2.12.5,<3.0.0)
Requires-Dist: redis (>=5.0.0,<6.0.0)
Requires-Dist: scipy (>=1.16.3,<2.0.0)
Requires-Dist: uvicorn (>=0.40.0,<0.41.0)
Project-URL: Documentation, https://github.com/CoReason-AI/coreason_sentinel
Project-URL: Homepage, https://github.com/CoReason-AI/coreason_sentinel
Project-URL: Repository, https://github.com/CoReason-AI/coreason_sentinel
Description-Content-Type: text/markdown

# coreason-sentinel

**The "Watchtower" & Circuit Breaker for Production AI Agents**

[![License: Prosperity 3.0](https://img.shields.io/badge/license-Prosperity%203.0-blue)](https://github.com/CoReason-AI/coreason-sentinel/blob/main/LICENSE)
[![CI](https://github.com/CoReason-AI/coreason-sentinel/actions/workflows/ci.yml/badge.svg)](https://github.com/CoReason-AI/coreason-sentinel/actions/workflows/ci.yml)
[![Code Style: 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-product_requirements.md-blue)](docs/product_requirements.md)

**coreason-sentinel** is an automated monitoring service for deployed LLM agents. It goes beyond simple "System Error" tracking to detect "Cognitive Errors" (Hallucinations) and "Business Errors" (Budget Spikes). Acting as a pharmacovigilance unit for AI, it monitors the agent's behavior in the real world, detects side effects like drift or toxicity, and triggers a **Circuit Breaker** to protect users and budgets if safety limits are breached.

## Features

*   **Deep Tracing & Observability:** visualize the entire cognitive chain (Retrieve -> Rerank -> Scout -> Generate) using OpenTelemetry and Arize Phoenix.
*   **Holistic Signal Detection:** Monitors three key signal planes:
    *   **Cognitive:** Hallucination rates, RAG relevance.
    *   **Business:** Token costs, Latency spikes.
    *   **User:** Sentiment analysis, refusal rates, and frustration signals.
*   **The Drift Engine:** Advanced statistical detection for:
    *   **Content Drift:** Changes in retrieved document vectors.
    *   **Style Drift:** "Lazy Agent" syndrome detection using Kullback-Leibler (KL) Divergence.
    *   **Relevance Drift:** Semantic distance between queries and responses.
*   **Automated Circuit Breaker:** A "Dead Man's Switch" that transitions to **OPEN** (blocking traffic) if quality or safety drops below configured thresholds. Includes **HALF-OPEN** self-healing to test recovery.
*   **The Spot Checker:** Automated QA that randomly samples live traffic (or focuses on negative sentiment) and loops it back for grading.

For a full breakdown of requirements and philosophy, see the [Product Requirements Document](docs/product_requirements.md).

## Installation

```bash
pip install coreason-sentinel
```

## Usage

Here is how to initialize the Circuit Breaker to protect your agent:

```python
import time
from redis import Redis
from coreason_sentinel.circuit_breaker import CircuitBreaker
from coreason_sentinel.models import SentinelConfig, CircuitBreakerTrigger

# 1. Configure the Sentinel
config = SentinelConfig(
    agent_id="agent-alpha",
    owner_email="ops@coreason.ai",
    phoenix_endpoint="http://localhost:6006",
    triggers=[
        # Trip if Faithfulness drops below 0.7 in the last hour
        CircuitBreakerTrigger(metric="faithfulness", threshold=0.7, window_seconds=3600, operator="<"),
        # Trip if Latency exceeds 10s in the last minute
        CircuitBreakerTrigger(metric="latency", threshold=10.0, window_seconds=60, operator=">"),
    ]
)

# 2. Initialize the Circuit Breaker
# Note: You must provide a valid Redis client and a NotificationService implementation.
# (Assuming a mock notification service for this example)
class MockNotificationService:
    def notify(self, message):
        print(f"NOTIFICATION: {message}")

breaker = CircuitBreaker(
    redis_client=Redis(),
    config=config,
    notification_service=MockNotificationService()
)

# 3. Protect your Agent
if not breaker.allow_request():
    # Return a maintenance message or failover
    print("Circuit Breaker OPEN: Traffic blocked due to safety violation.")
else:
    # Process your agent request...
    start_time = time.time()
    try:
        # ... agent logic ...
        print("Agent processing request...")
        pass
    finally:
        # Record metrics for the breaker to monitor
        # In a real app, you would calculate actual latency
        breaker.record_metric("latency", time.time() - start_time)
```

