# sdevanathan95/prompt-safe architecture

> Architecture diagram of the GitHub repository sdevanathan95/prompt-safe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sdevanathan95/prompt-safe
- Repository: https://github.com/sdevanathan95/prompt-safe

## Overview

#### Purpose

This repository defines a research-oriented security middleware layer for tool-calling AI agents. Its core defense is causal: determine whether a proposed tool action is attributable to the user’s request, rather than only detecting suspicious text in retrieved or tool-provided content.

Primary architectural documentation is in `README.md` and `docs/project-brief.md`.

#### Entry Points and Integration Surface

The project is intended to be embedded around an existing agent/tool-execution loop rather than run as a standalone web application. Provider and agent-framework integration points belong under:

- `adapters/`
- `middleware/screening/`
- `middleware/melon/`

Planned adapters cover OpenAI, Anthropic, and LangGraph. They should translate provider-specific messages, tool calls, and execution lifecycle events into the shared middleware trace contract.

#### Shared Trace Contract

The central integration boundary is the trace/state schema:

- `middleware/trace/schema.md`
- `middleware/trace/`

Both security tracks read and write this same per-step trace. It records provenance, screened content regions, policy decisions, MELON comparison results, and the resulting execution decision.

The trace is also the artifact consumed by observability and presentation layers, including the future demo visualizer in `demo/`. Schema stability is the key dependency between independently developed tracks.

#### Track A: Provenance Screening and Policy Gate

The screening pipeline is owned by:

- `middleware/screening/`
- `middleware/trace/`

It tags input/tool-output regions by provenance, performs an LM-judge-style screening pass, and produces a three-way policy verdict: `safe`, `block`, or `escalate`. This acts as an initial gate before an agent action is executed or subjected to deeper causal validation.

Its output is persisted into `source_provenance`, `screened_regions`, and `policy_verdict` on the shared trace.

#### Track B: MELON Causal Re-execution

The causal validation engine belongs in:

- `middleware/melon/`
- `eval/`

MELON performs masked re-execution: it compares tool calls generated from the original context against calls generated after masking potentially untrusted content. A distance/threshold decision determines whether the action remains causally supported by the user request.

Results populate the trace’s `melon_check`, including original and masked calls, distance, and a `safe`/`block` verdict.

#### Decision Flow

For each agent step, adapters capture the user request, untrusted tool/retrieval content, and candidate tool calls. Screening establishes provenance and a preliminary policy decision. Where required, MELON reruns the decision under masking and compares outputs. The middleware then writes one final action:

- `execute` for allowed calls,
- `block` for rejected behavior,
- `ask_user` for escalation.

This final action is the enforcement boundary between agent reasoning and external tool side effects.

#### Evaluation, Demo, and External Dependencies

Evaluation infrastructure is planned under `eval/`, using AgentDojo and InjecAgent-style benchmarks plus metrics reporting. A live scenario and trace visualizer are planned in `demo/`.

Python dependencies are declared in `requirements.txt`. No implemented runtime services or concrete provider SDKs are present yet; most functional directories currently contain placeholders. The architecture therefore centers on the trace schema and planned middleware boundaries rather than existing executable code.

Main entry points: [`middleware/trace/schema.md`](https://github.com/sdevanathan95/prompt-safe/blob/main/middleware/trace/schema.md) (Trace schema), [`requirements.txt`](https://github.com/sdevanathan95/prompt-safe/blob/main/requirements.txt) (Python dependencies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_integration["Agent Integration"]
  node_agent_loop{{"Existing agent loop<br/>host runtime"}}
  node_adapters["Provider adapters<br/>integration adapters"]
end

subgraph group_security["Security Middleware"]
  node_trace_schema["Trace schema<br/>shared contract<br/>[schema.md]"]
  node_trace_state[("Per-step trace<br/>trace state")]
  node_screening["Provenance screening<br/>security pipeline"]
  node_policy_gate{{"Policy verdict<br/>decision gate"}}
  node_melon["MELON re-execution<br/>causal validator"]
  node_comparison{{"Call distance decision<br/>causal decision"}}
  node_enforcement{{"Final action enforcement<br/>side-effect boundary"}}
end

subgraph group_validation["Evaluation &amp; Presentation"]
  node_evaluation["Benchmark evaluation<br/>evaluation suite"]
  node_metrics["Metrics reporting<br/>evaluation output"]
  node_demo["Trace visualizer<br/>demo"]
end

node_external_tools(("External tools<br/>tool side effects"))
node_dependencies["Python dependencies<br/>dependency manifest<br/>[requirements.txt]"]

node_agent_loop -->|"messages, tool calls, events"| node_adapters
node_adapters -->|"captures step inputs"| node_trace_state
node_trace_schema -->|"defines"| node_trace_state
node_trace_state -->|"untrusted content and context"| node_screening
node_screening -->|"provenance and screened regions"| node_policy_gate
node_screening -->|"writes provenance and screening"| node_trace_state
node_policy_gate -->|"requires causal validation"| node_melon
node_policy_gate -->|"safe/block/escalate"| node_enforcement
node_trace_state -->|"candidate call and masked regions"| node_melon
node_melon -->|"original and masked calls"| node_comparison
node_comparison -->|"writes melon_check"| node_trace_state
node_comparison -->|"safe or block"| node_enforcement
node_enforcement -->|"execute only"| node_external_tools
node_enforcement -->|"execute, block, or ask_user"| node_agent_loop
node_trace_state -->|"evaluation artifact"| node_evaluation
node_evaluation -->|"reports results"| node_metrics
node_trace_state -->|"visualizes trace"| node_demo
node_dependencies -.->|"declares runtime packages"| node_adapters

click node_trace_schema "https://github.com/sdevanathan95/prompt-safe/blob/main/middleware/trace/schema.md"
click node_dependencies "https://github.com/sdevanathan95/prompt-safe/blob/main/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_agent_loop,node_adapters toneBlue
class node_trace_schema,node_trace_state,node_screening,node_policy_gate,node_melon,node_comparison,node_enforcement toneAmber
class node_evaluation,node_metrics,node_demo toneMint
class node_external_tools,node_dependencies toneNeutral
```

## Components

### Agent Integration

Embeds middleware around provider agent loops.

- **Existing agent loop**: host runtime · Produces candidate tool calls and receives enforcement decisions.
- **Provider adapters**: integration adapters · Planned OpenAI, Anthropic, and LangGraph lifecycle translation.

### Security Middleware

Two tracks coordinate through the shared trace.

- [Trace schema](https://github.com/sdevanathan95/prompt-safe/blob/main/middleware/trace/schema.md): shared contract · Stable per-step contract between middleware tracks and consumers. · `middleware/trace/schema.md`
- **Per-step trace**: trace state · Stores provenance, screening, MELON, and final decisions.
- **Provenance screening**: security pipeline · Tags regions and performs LM-judge-style screening.
- **Policy verdict**: decision gate · Produces safe, block, or escalate.
- **MELON re-execution**: causal validator · Compares original and masked-context tool calls.
- **Call distance decision**: causal decision · Thresholds masked-versus-original call distance.
- **Final action enforcement**: side-effect boundary · Returns execute, block, or ask_user before external effects.

### Evaluation & Presentation

Consumes traces and evaluates planned defenses.

- **Benchmark evaluation**: evaluation suite · Planned AgentDojo and InjecAgent-style benchmark runs.
- **Metrics reporting**: evaluation output
- **Trace visualizer**: demo · Planned live scenario and trace presentation.

### Other components

- **External tools**: tool side effects
- [Python dependencies](https://github.com/sdevanathan95/prompt-safe/blob/main/requirements.txt): dependency manifest · `requirements.txt`

## Connections

- Existing agent loop → Provider adapters: messages, tool calls, events
- Provider adapters → Per-step trace: captures step inputs
- Trace schema → Per-step trace: defines
- Per-step trace → Provenance screening: untrusted content and context
- Provenance screening → Policy verdict: provenance and screened regions
- Provenance screening → Per-step trace: writes provenance and screening
- Policy verdict → MELON re-execution: requires causal validation
- Policy verdict → Final action enforcement: safe/block/escalate
- Per-step trace → MELON re-execution: candidate call and masked regions
- MELON re-execution → Call distance decision: original and masked calls
- Call distance decision → Per-step trace: writes melon_check
- Call distance decision → Final action enforcement: safe or block
- Final action enforcement → External tools: execute only
- Final action enforcement → Existing agent loop: execute, block, or ask_user
- Per-step trace → Benchmark evaluation: evaluation artifact
- Benchmark evaluation → Metrics reporting: reports results
- Per-step trace → Trace visualizer: visualizes trace
- Python dependencies → Provider adapters: declares runtime packages
