Metadata-Version: 2.4
Name: claude-verified-agent
Version: 0.2.0
Summary: End-to-end verified agent demo: inter-agent memory (agent-context-bridge) + cited-or-refused RAG (cited-rag) + CI safety gates (llm-eval-ci). Stdlib only, offline, no API key.
Author-email: shenn667 <lehdev@yandex.com>
License: MIT
Project-URL: Homepage, https://github.com/shenn667/claude-verified-agent
Keywords: agents,rag,citations,evals,offline,claude
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Provides-Extra: pillars
Requires-Dist: agent-context-bridge>=0.4.0; extra == "pillars"
Requires-Dist: cited-rag>=0.2.0; extra == "pillars"
Requires-Dist: llm-eval-ci>=0.2.0; extra == "pillars"
Dynamic: license-file

# claude-verified-agent

[![CI](https://github.com/shenn667/claude-verified-agent/actions/workflows/ci.yml/badge.svg)](https://github.com/shenn667/claude-verified-agent/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/claude-verified-agent.svg)](https://pypi.org/project/claude-verified-agent/)
[![Python 3.12+](https://img.shields.io/badge/python-3.12%2B-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
[![offline](https://img.shields.io/badge/offline-stdlib--only-lightgrey.svg)](https://github.com/shenn667/claude-verified-agent)

**An end-to-end verified agent: every answer is cited or refused, every
delivery passes safety gates, every run is CI-blocked on regression.**

No hallucination by design, no PII/injection leak by gate, no silent
regression by suite. Stdlib-only core, fully offline, no API key.

```bash
pip install -e .
make demo     # 1 command: cited answer + refusal + gate verdicts
make banking-demo  # banking & SecOps vertical: 4 asserted offline cases
make test     # 48 contract tests (pytest, offline)
make gates    # 4 llm-eval-ci suites, exit != 0 blocks CI
```

## Architecture

```text
 question
    |
    v
 [1] record-contexte (agent-context-bridge)
    |  acb record question + answer | acb search past context
    v
 [2] ask cite (cited-rag)
    |  BM25 + TF-IDF -> RRF -> verbatim sentences + [file:L1-L2]
    |  max_score < 0.35 or fidelity < 0.5 -> "Insufficient evidence"
    v
 [3] gate (llm-eval-ci)
    |  no_pii_leak + no_injection_obey over the produced answer
    |  red gate -> refusal (raw text never delivered)
    v
 cited answer  |  refusal (no-evidence / safety-gate)
```

Each stage prefers its pillar implementation and degrades to a compact
stdlib equivalent when the pillar is not installed (`backends` field of
every answer states which implementation served each stage, so the demo
output is auditable). Set `CVA_FORCE_FALLBACK=1` to exercise the portable
path — the 48 tests pass identically on both paths.

## The 3 pillars (used, not rewritten)

| Stage | Pillar repo | How it is used here |
|-------|--------------|---------------------|
| Inter-agent memory | [agent-context-bridge](https://github.com/shenn667/agent-context-bridge) | `MemoryStore` + `ContextRecord` + `search_records`: every question and every verdict is recorded (`acb record` semantics) and past context is searched before retrieval |
| Cited RAG | [cited-rag](https://github.com/shenn667/cited-rag) | `build_index_data` + `Retriever.search` + `generate_answer`: hybrid retrieval, extractive verbatim answers with exact `[file:L1-L2]` citations, `Insufficient evidence` refusal with zero citations |
| CI gates | [llm-eval-ci](https://github.com/shenn667/llm-eval-ci) | `check_no_pii_leak` + `check_no_injection_obey` gate every answer; `evals/*.json` suites run via `llm-eval run` with blocking exit codes |

## Eval results (reproduce with `make gates` + `make test`)

`llm-eval run` on the bundled suites (deterministic, offline):

| Suite | Cases | Result |
|-------|-------|--------|
| `evals/non_regression.json` | cited answers (contains + citation regex + PII-free), off-corpus refusal | 3/3 passed, exit 0 |
| `evals/schema.json` | JSON payload contract (cited + refused shapes, budgets) | 2/2 passed, exit 0 |
| `evals/tripwires.json` | PII request refused, injection-laced prompt refused | 2/2 passed, exit 0 |
| `evals/banking_compliance.json` | banking cited answer, off-corpus refusal, PII gate, injection gate, masked memory | 5/5 passed, exit 0 |

`pytest`: **48/48 passed** on the pillar path and **48/48 passed** with
`CVA_FORCE_FALLBACK=1` (portable stdlib path): exact verbatim citations,
refusal with zero citations, gate blocks PII/injection strings,
injection-laced questions never obeyed, answer determinism, JSON contract,
memory records, backend auditability. The count is 14 core contract tests
(`tests/test_verified_agent.py`) plus 34 banking-compliance tests
(`tests/test_banking_compliance.py`, 16 cases × 2 backends).

The red path (a leaked PII string or an obeyed injection **fails** the
gate, exit 1) is proven in `pytest`, not in the suites: the suites must
stay green or CI blocks.

## Banking & SecOps Compliance Use Case (B2B)

A regulated-banking vertical built on the same verified pipeline, shipped
under `examples/banking-compliance/`. The corpus is **100% synthetic** —
four bilingual (FR + EN) markdown files for the fictional *Banque Exemple
SA / Example Bank SA*. Every file opens with a `SYNTHETIC` header; there is
**zero real enterprise data**, no real PII, and no actual regulatory text
(the BCEAO-inspired audit policy is explicitly labelled synthetic).

Topics: PCI DSS controls (AES-256 at rest, TLS 1.2+ in transit, PAN
masking first-6/last-4, HSM key storage, 90-day key rotation), BCEAO-inspired
audit logs (10-year retention, WORM integrity, NTP sync, maker/checker),
a SecOps ransomware playbook (VLAN isolation, east-west blocking, forensic
image before rebuild, RSSI/CISO escalation within 1 hour, 72-hour regulator
notification), and IAM privileged access (mandatory bastion, phishing-resistant
MFA, quarterly review, JIT elevation, session recording) ending in an
assistant-secrecy section that serves as the injection tripwire target.

The offline demo (`demo_banking.py`, tempfile workspace, no API key) runs
four cases with hard asserts (exit 1 on any failure):

| Case | Question theme | Expected outcome |
|------|----------------|------------------|
| C1 | PCI DSS key-rotation frequency (in-corpus) | cited verbatim answer, gate PASS |
| C2 | off-corpus trading query | `no-evidence` refusal, zero citations |
| C3 | IBAN + PAN + email probe | gate blocks `no_pii_leak`; no raw value in answer or workspace; memory masked |
| C4 | injection-laced IAM question | `safety-gate` refusal via `no_injection_obey` |

```bash
make banking-demo   # 4 asserted cases, offline, exit 0 on success
make gates          # 4 blocking eval suites (incl. banking_compliance)
make test           # 48 contract tests, both backends
```

**PAN gate extension.** The pillar PII patterns are frozen and carry no
card-number rule, so the agent-side gate (`verified_agent.gates`) now
supplements **both** backends with Luhn-validated PAN detection
(`_luhn_ok` + `_pan_candidates`): 13-19 digit runs (separators
stripped) that pass the Luhn checksum are flagged as `pan` under
`no_pii_leak` and masked. Detection and masking share one overlap-safe
span collector (`_collect_spans`), so a greedy IBAN match can no longer
swallow an adjacent PAN and a phone sub-pattern cannot leave an IBAN
fragment in the masked output (no raw value or digit fragment survives).
`4111111111111111` is detected; `4111111111111112` (bad Luhn) is not.
The demo and the 34 banking tests
pass identically on the native-pillar path and with `CVA_FORCE_FALLBACK=1`.

## Quickstart (3 commands)

```bash
pip install -e .
make demo    # offline demo, no key
make gates   # 4 blocking eval suites
```

CI (`.github/workflows/ci.yml`): job `pytest` + job `gates` (one step per
suite, `llm-eval run` when installed, stdlib fallback runner otherwise).
Any red gate fails the build.

## Watchdog wrapper (autonomous, stdlib)

The repo includes an autonomous watchdog wrapper: `scripts/run_watchdog.py`
(stdlib-only, Popen + start_new_session + 2 reader threads + 1s loop +
900s timeout + 300s stall detection + process-group kill, bounded output).

```bash
python3 scripts/run_watchdog.py --target demo --json
python3 scripts/run_watchdog.py --target gates --json --timeout 900 --stall-timeout 300
python3 scripts/run_watchdog.py --target all --json
```

With `--json`, the wrapper emits an additive JSON summary with at least:
`job_id`/`run_id`, `status` (`DONE|FAILED|TIMEOUT|STALLED`), `exit_code`,
`started_at`, `finished_at`, `duration_s`, `reason`, `tail`.

Makefile: the `demo-wd` and `gates-wd` targets go through the watchdog;
`demo` and `gates` stay unchanged for compatibility.

## Verification (3 trials)

`scripts/verify_3_trials.py` runs three independent trials (byte-compile,
`pytest -q`, `make gates`) with the current interpreter (`sys.executable`).
The verdict is `PASS 3/3` only if every trial passes:

```bash
python3 scripts/verify_3_trials.py
```

## Action gate / status JSON

- `verified_agent.gates.run_suite_file` applies action-gate semantics: `0` allow, `2` deny (red gate / failed checks), `1` ask / fail-closed (usage, env, invalid, parse, unknown). On a red gate, no raw output is echoed; only the id, failed checks, and safe/masked info.
- `VerifiedAgent.ask()` adds additive top-level fields `scores`, `rejected`, `backtracks_count` without breaking the existing `backends` field (`memory`, `rag`, `gate`).
- `emit_summary(result, json_mode=False)` and `print_status(result, json_mode=False)` are exposed; `demo/demo.py --json` uses this path.

## Links

- Dataset (Hugging Face): https://huggingface.co/datasets/SHENNN667/cited-rag-golden-52
  (published under namespace `SHENNN667` — no write rights on `shenn667`;
  the lowercase `shenn667/...` URL returns 401, do not use it as the main link)
- Eval page: https://shenn667.github.io/eval/ (live, deployed from
  `shenn667/shenn667.github.io` main branch)

## License

MIT — see [LICENSE](LICENSE).
