How sdevanathan95/prompt-safe works
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: schema.md Trace schema · requirements.txt Python dependencies
Components 14
Agent Integration
Embeds middleware around provider agent loops.
- Existing agent loop · Produces candidate tool calls and receives enforcement decisions.
- Provider adapters · Planned OpenAI, Anthropic, and LangGraph lifecycle translation.
Security Middleware
Two tracks coordinate through the shared trace.
- Trace schema · Stable per-step contract between middleware tracks and consumers.
middleware/trace/schema.md - Per-step trace · Stores provenance, screening, MELON, and final decisions.
- Provenance screening · Tags regions and performs LM-judge-style screening.
- Policy verdict · Produces safe, block, or escalate.
- MELON re-execution · Compares original and masked-context tool calls.
- Call distance decision · Thresholds masked-versus-original call distance.
- Final action enforcement · Returns execute, block, or ask_user before external effects.
Evaluation & Presentation
Consumes traces and evaluates planned defenses.
- Benchmark evaluation · Planned AgentDojo and InjecAgent-style benchmark runs.
- Metrics reporting
- Trace visualizer · Planned live scenario and trace presentation.
Other components
- External tools
- Python dependencies
requirements.txt
Connections 18
- 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