Skip to main content

AgentReplayGuard

面向 LangGraph + pytest 的副作用安全测试工具:用确定性的故障注入与 effect 账本,验证 重试、Checkpoint 恢复与 Interrupt 重放不会重复执行支付、退款、写入、通知等业务操作。

MVP 核心实现已完成(US1–US6),并已补齐异步工具、异步图执行、HTTP 传输层故障注入、 并行 fan-out 图支持、流式接口、requests 适配器、多框架工具接入、报告导出与 JUnit 报告; 当前覆盖 lost-ack 重复副作用、幂等键、Checkpoint 恢复、Interrupt 重放、业务不变量、 并行分支重复副作用、流式归属与确定性回归。内置退款、订单、邮件、库存四类模拟服务模板。

它解决什么问题

AI Agent 调用有副作用的工具时,失败语义与普通函数不同:

  1. 服务端已提交(如退款成功),但响应丢失,Agent 认为失败并重试 → 两笔退款。
  2. LangGraph 的 durability 保证图状态恢复,但不保证外部副作用 exactly-once。
  3. interrupt 恢复会从头重跑节点,放在 interrupt 之前的副作用可能再次执行。

传统 mock 验证「调用了几次」,无法表达「服务端提交了几次」。AgentReplayGuard 把业务 副作用的最终一致性变成可在 CI 中断言的属性。

现状与路线图

快速开始

安装

# 最小安装:核心包仅依赖标准库
pip install agentreplayguard

# 按需安装适配层(可组合)
pip install "agentreplayguard[langgraph]"   # LangGraph 图适配
pip install "agentreplayguard[httpx]"       # httpx 传输层故障注入
pip install "agentreplayguard[requests]"    # requests 客户端适配
pip install "agentreplayguard[agents]"      # OpenAI Agents SDK 工具

用 uv 等价写法:uv add agentreplayguard --extra langgraph。

从源码开发

uv sync --extra dev --extra langgraph --extra httpx --extra requests --extra agents
uv run pytest examples/refund -q

用户接入说明见 docs/quickstart.md,完整验收步骤见 quickstart.md,版本演进见 CHANGELOG.md。

pytest 最小示例

replayguard fixture 由插件自动注册(无需 import):声明场景后,测试结束会自动检查 副作用安全性,有违规即 FAILED——忘记写断言也不会静默通过。

from agentreplayguard import FaultKind, FaultPlan, FaultRule, NoDuplicateEffects

# examples/refund/test_refund_idempotent.py 的简化版
def test_refund_lost_ack(replayguard):
    scenario = replayguard(
        "refund-lost-ack",
        seed=7,
        faults=FaultPlan([
            FaultRule(FaultKind.TIMEOUT_AFTER_COMMIT, effect_type="refund", attempt=1),
        ]),
        invariants=[NoDuplicateEffects("refund")],
    )
    # 用 scenario.guard().effect(...) 包裹有副作用的工具,跑业务流程……
    # 无需手动 assert:出现重复退款时本测试自动 FAILED

如需细粒度断言(如具体笔数),仍可显式读报告——两者不冲突,也不会重复报错:

report = scenario.finish()
assert not report.ok
assert scenario.effects.committed_count("refund") == 2

核心概念

模块 职责
Effect Store 记录每次副作用尝试:类型、业务键、参数哈希、尝试序号、提交状态、run/thread/checkpoint、branch
Fault Injector 六类故障(提交前超时、提交后响应丢失、连接重置、500、部分成功、流式响应体丢失),固定 seed
Simulated Service 退款/订单/邮件/库存四类模拟服务:内存账本 + 幂等键开关(参考实现,供仿照接入)
Invariant Checker no_duplicate_effects、max_effect_count、concurrent_commit、自定义 predicate
pytest 集成 replayguard fixture 自动断言:测试结束后检查场景,有违规即 FAILED(忘记写断言也不会漏报)
LangGraph Adapter 驱动 RetryPolicy / resume / interrupt / stream / astream,支持同步/异步与并行 fan-out,记录节点分支归属
HTTP Transport Adapter 包裹 httpx 同步/异步 transport 与 requests adapter,业务代码零改动地记账并注入 lost-ack

许可

MIT License。详见 LICENSE。

参与贡献

见 CONTRIBUTING.md。

Metadata

Release files for agentreplayguard 0.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentreplayguard 0.14.0
File Size Uploaded Provenance Actions
agentreplayguard-0.14.0.tar.gz 31.4 kB Without provenance Details for agentreplayguard-0.14.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agentreplayguard 0.14.0
File Interpreter ABI Platform Actions
agentreplayguard-0.14.0-py3-none-any.whl Python 3 none any Details for agentreplayguard-0.14.0-py3-none-any.whl

Total release size: 70.0 kB

Release files / agentreplayguard-0.14.0.tar.gz

Download URL agentreplayguard-0.14.0.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5ad9a99c4b235c4c31f9569b673e82ae42d6f05bd973837c1c3a2ea569b71b6f
BLAKE2b-256 checksum
How to use checksums
074cf6ed017e0fb90632d361d418601290db3d7b3d5721e9c0afd4700423fddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / agentreplayguard-0.14.0-py3-none-any.whl

Download URL agentreplayguard-0.14.0-py3-none-any.whl
Size 38.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5653413adca62701a6c1f40eb1280fe47b26f62b78494e912a43d8c1632b3132
BLAKE2b-256 checksum
How to use checksums
6d10c8c0673947ae553b6963edab86a63c6d626d5da66c559f86f4d0c65ae5f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for agentreplayguard 0.14.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.14.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page