Metadata-Version: 2.5
Name: stepfork
Version: 0.3.0
Summary: Open-source behavioral regression testing for AI agents.
Project-URL: Homepage, https://github.com/utsab345/stepfork
Project-URL: Repository, https://github.com/utsab345/stepfork
Project-URL: Issues, https://github.com/utsab345/stepfork/issues
Project-URL: PyPI, https://pypi.org/project/stepfork/
Author: Utsab Dahal
License: Apache-2.0
License-File: LICENSE
Keywords: agents,ai,debugging,llm,pytest,regression-testing,replay,testing
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Testing
Requires-Python: <3.14,>=3.11
Requires-Dist: pydantic>=2.0
Requires-Dist: rich>=13.0
Requires-Dist: typer>=0.12
Provides-Extra: langgraph
Requires-Dist: langchain-core>=1.0; extra == 'langgraph'
Requires-Dist: langgraph>=1.0; extra == 'langgraph'
Provides-Extra: openai
Requires-Dist: openai>=1.0; extra == 'openai'
Description-Content-Type: text/markdown

# Stepfork

**Open-source behavioral regression testing for AI agents.**

Record failed AI agent executions locally, replay instrumented dependencies, compare behavior, and export reproducible pytest regression tests.

[PyPI](https://pypi.org/project/stepfork/) ·
[Documentation](https://utsab345.github.io/stepfork/) ·
[CI](https://github.com/utsab345/stepfork/actions/workflows/ci.yml) ·
[Issues](https://github.com/utsab345/stepfork/issues)

## The problem

Unit tests often miss regressions in multi-step agent behavior. A recorded run
can show what the agent called and returned, but replaying against live tools or
models makes a regression test slow, costly, and nondeterministic.

Stepfork captures instrumented dependency calls in a local trace and replays
their recorded responses. You can compare runs and export a pytest test with an
explicit expectation for the corrected behavior.

## How it works

Wrap the tool and LLM boundaries you want captured. Frozen replay strictly
matches those boundaries and substitutes recorded responses, so the wrapped
bodies are not run again. Calls outside instrumented boundaries are not
intercepted.

1. **Record** a failed or completed run into a local `.sftrace` bundle.
2. **Inspect** the events, integrity metadata, and failure details.
3. **Replay** the entrypoint with instrumented dependencies frozen.
4. **Diff** the recorded behavior against a corrected run.
5. **Export** a pytest regression test with an explicit expectation.
6. **Test** the buggy entrypoint and the fixed entrypoint with pytest.

![Stepfork workflow](assets/stepfork-workflow.png)

## Features

### In v0.3.0

- Record completed and failed runs as local `.sftrace` bundles.
- Inspect and validate traces; check bundle integrity.
- Replay instrumented tool and LLM calls with recorded responses.
- Compare traces and inspect behavior-level differences.
- Report coverage of observed instrumented calls. Calls outside instrumented
  boundaries are not measured.
- Save named local baselines and gate trace changes in CI
  ([guide](docs/baselines.md), [runnable example](docs/baseline-example.md)).
- Export pytest regression tests with output and trajectory expectations.
- Minimize failing traces and fork traces at a selected step.
- Use optional OpenAI Python SDK and LangGraph adapters for supported
  non-streaming boundaries.
- Capture optional retrieval evidence and per-node context through LangGraph
  `traced_node`, `capture_retrieval`, and `record_assembled_context`; see
  [RAG evidence](docs/rag.md).
- Best-effort redaction of common credential patterns when recording.

Streaming model calls are not supported by the adapters.

Stepfork is experimental. Redaction is not a guarantee against sensitive-data
leaks, integrity hashes are not signatures, and replay is not a sandbox. See
[security notes](docs/security.md).

## Installation

Requires Python 3.11, 3.12, or 3.13.

Install the v0.3.0 release from PyPI:

```bash
pip install "stepfork==0.3.0"
```

For a contributor checkout or the latest development changes, install from
source:

```bash
pip install "git+https://github.com/utsab345/stepfork.git@main"
```

See the [development instructions](docs/development.md) for contributor setup.

Install an adapter only if needed:

```bash
pip install "stepfork[openai]"
pip install "stepfork[langgraph]"
```

## Quick start

Save this as `quickstart.py`. The example records a shipped order that the
buggy agent fails to notify about.

```python
from stepfork import record, trace_tool


@trace_tool
def order_status(order_id: str) -> dict[str, str]:
    return {"order_id": order_id, "status": "shipped"}


def run_agent() -> dict[str, bool]:
    order = order_status("ORD-1001")
    return {"notify": order["status"] == "delivered"}  # Bug: should notify on shipment


if __name__ == "__main__":
    with record("order-agent", output="failure.sftrace") as session:
        result = run_agent()
        session.set_output(result)
    print(result)
```

Run the CLI commands from the directory containing `quickstart.py`. The CLI
resolves `--entrypoint` from the current working directory; use `PYTHONPATH` if
the module lives elsewhere.

```bash
python -m pip install pytest
python quickstart.py
stepfork inspect failure.sftrace
stepfork replay failure.sftrace --entrypoint quickstart:run_agent --mode frozen
printf '{"notify": true}\n' > expected.json
stepfork export failure.sftrace \
  --entrypoint quickstart:run_agent \
  --expect-output expected.json \
  --output test_order_regression.py
```

Change the condition in `run_agent` to check for `"shipped"`, then run
`python -m pytest -q test_order_regression.py`. See the
[five-minute quickstart](docs/getting-started.md) for inspect, validate, diff,
and red-green pytest commands.

See the [terminal demo](scripts/terminal-demo/stepfork-demo.gif).

## Integrations

The core API is framework-independent. Optional adapters currently support:

- **OpenAI Python SDK:** sync and async, non-streaming
  `chat.completions.create`.
- **LangGraph / LangChain:** sync and async non-streaming chat-model generation
  and wrapped function tools. v0.3.0 also adds optional node and retrieval
  evidence. Streaming is not supported.

Offline GitHub Actions examples cover frozen replay and named baseline
comparisons without provider credentials. Stepfork does not currently provide
a reusable GitHub Action or an MCP adapter. See the
[integration details](docs/integrations.md).

## Contributing

Contributions are welcome. Read the [contribution guide](CONTRIBUTING.md) and
[development instructions](docs/development.md), then open an issue or pull
request. Please do not include credentials or unreviewed trace data in issues,
tests, or commits.

## License

Apache-2.0. See [LICENSE](LICENSE).
