Metadata-Version: 2.4
Name: persona-integrity-protocol
Version: 0.1.0
Summary: PI² (Persona Integrity Protocol) - identity-bleed mitigation for multi-model AI pipelines.
Author-email: "Daniel Young (SpYdA573)" <dyoungandco@proton.me>
License: MIT
Project-URL: Homepage, https://github.com/dizzyd303/pi2-protocol
Project-URL: Original protocol, https://gist.github.com/dizzyd303/cf5b793163345a6ecb1dfa062a205324
Keywords: llm,ai-agents,multi-model,orchestration,prompt-engineering,agent-pipeline
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# pi2 — Persona Integrity Protocol (PI²)

Identity-bleed mitigation for multi-model AI orchestration pipelines.

## The problem

Multi-model AI pipelines suffer from **identity bleed** — when one model's
output format, reasoning style, or self-reference leaks into a downstream
model, corrupting the pipeline's operational voice over successive
handoffs.

Symptoms:

- Model A outputs a thinking preamble → Model B starts adding one too
- Model A uses markdown headers → Model B adopts them
- Model A says "As an AI assistant..." → Model B starts self-referencing
- Structured output (JSON plans, etc.) gets wrapped in code blocks,
  step-by-step reasoning, or bold headers
- The pipeline drifts from terse operator language to verbose
  assistant language, the longer the chain runs

Root cause: downstream models are effectively trained, in-context, on the
raw output of upstream models rather than on the intended format. The
upstream model's persona becomes part of the next model's input.

## The fix

PI² is a three-layer defense:

1. **Identity Reanchor** (`pi2.build_identity_reanchor`) — inject a fixed
   identity lock before every model call, so the model is told who it is
   before it sees anything else.
2. **Thinking-Bleed Stripper** (`pi2.strip_thinking_bleed`) — deterministically
   sanitize upstream output (reasoning preambles, thinking tags, markdown
   contamination) before it's passed downstream or parsed.
3. **Foreign Anchor Stress Test** (`pi2.validate_persona_integrity`) —
   check cleaned output against the pipeline's baseline persona; flag
   violations and signal a retry if it drifted.

```python
from pi2 import build_identity_reanchor, strip_thinking_bleed, validate_persona_integrity

prompt = build_identity_reanchor(
    module_name="SIC_ORCHESTRATOR",
    output_type="structured JSON task arrays",
    domain="cybersecurity operations",
    format="valid JSON",
) + "\n\nGoal: recon example.com\nReturn ONLY a JSON array..."

raw = call_your_model(prompt)          # your model call
cleaned = strip_thinking_bleed(raw)     # Layer 2
result = validate_persona_integrity(cleaned)   # Layer 3

if not result.valid:
    # result.action == "REANCHOR_AND_RETRY"
    ...  # re-run with a stronger reanchor, see pi2.PI2Pipeline for a
         # built-in retry loop
```

Or use the built-in pipeline wrapper, which does the reanchor → call →
strip → validate → retry loop for you:

```python
from pi2 import PI2Pipeline

stage = PI2Pipeline(
    call_model=your_model_call_fn,   # str -> str
    module_name="SIC_ORCHESTRATOR",
    output_type="structured JSON task arrays",
    domain="cybersecurity operations",
    format="valid JSON",
    max_retries=1,
)
result = stage.run("Goal: recon example.com")
print(result.cleaned)
```

## What this has actually been validated against

The original protocol was built and used in production on the SIC
Overlord v10 pipeline, across a Qwen3.5:9b / Dolphin-Mistral / MiniMax
M3:cloud model chain. In that use, identity/format drift dropped
noticeably after adopting the three-layer approach — that observation
comes from running it in production, not from a formally logged study,
so treat it as a real-world field result rather than a benchmarked
statistic. If you run PI² against your own stack and want to contribute
actual measured before/after numbers, PRs and issues are welcome.

## Limitations

- **Model-specific bleed patterns.** New models may introduce new
  thinking/reasoning formats not yet covered by the stripper's pattern
  list. Expect to extend `pi2/stripper.py`'s marker list over time.
- **False positives.** Aggressive stripping can remove legitimate bold
  text or formatting from genuine output. The patterns are
  line-anchored where possible to reduce this.
- **This does not call any model for you.** `PI2Pipeline` takes a
  `call_model` callable — bring your own backend (Ollama, an
  OpenAI-compatible API, Anthropic, local transformers, whatever you're
  already using).

## Origin

Original protocol authored by Daniel Young (SpYdA573), D. Young & Co.,
first documented as a
[gist](https://gist.github.com/dizzyd303/cf5b793163345a6ecb1dfa062a205324)
and grown out of an earlier, narrower "thinking-bleed" fix built for the
AIOP pipeline.

## License

MIT — see `LICENSE`.
