Metadata-Version: 2.4
Name: irimi-sdk
Version: 0.2.0
Summary: The irimi SDK alone, for production apps: record workflow runs and send them to an irimi server.
License-Expression: Apache-2.0
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Typing :: Typed
Requires-Python: >=3.10
Project-URL: Homepage, https://github.com/irimi-io/Irimi
Project-URL: Issues, https://github.com/irimi-io/Irimi/issues
Project-URL: Source, https://github.com/irimi-io/Irimi
Description-Content-Type: text/markdown

# irimi-sdk

The irimi SDK alone. irimi does shadow runs for AI agents: record a workflow run in production,
replay it with one change (a model, a prompt, code) with every write faked, and see how the two
runs differ.

```sh
pip install irimi-sdk
```

This is the part that runs inside an app and records it: `import irimi`, no dependencies, Python
3.10 or newer. `pip install irimi` installs it together with the `irimi` command and the irimi
server, and needs Python 3.12 or newer.

## Replay a run

A replay runs a recorded run's workflow as the code is now and records the result as a new run.
Calls that match the recording are answered from it. Writes sent through the HTTP clients irimi
intercepts (httpx, httpx2, requests, urllib3 and aiohttp) and calls to functions marked
`@irimi.function(write=True)` are never performed; a read with no match is sent for real. So is an
LLM call whose request changed: its new answer is the point of a replay, and it costs money and
tokens like any other call. Once the replay makes a write that is new or has different arguments, or
skips a recorded write, every read answered from the recording after that is flagged
`after_divergence`: its answer shows the world as the recorded run left it, not as the replay's
writes would have. The workflow must live in a module that can be imported. Run the replay from the
app's directory. With `IRIMI_SERVER_URL` set it uses that server; without it, it reads the runs the
app recorded to `.irimi` in that directory:

```sh
irimi replay <run id>
irimi runs <new run id>
```

`irimi replay <run id> --diff` ends by printing how the replay differs from the recorded run,
call by call, as `irimi diff <run id> <new run id>` does.

The `irimi` command comes with `pip install irimi`; `pip install irimi-sdk` does not install it.

A replay stops when it finds a client irimi cannot intercept, such as a database driver
(psycopg, SQLAlchemy, redis and others) or an HTTP client irimi could not patch, and only when the
app loads it: one that is installed but never imported does not stop a replay. It checks once
the workflow's module has been imported, and again whenever the workflow imports such a client
later; writes a module makes while it is being imported happen before that first check. Probing
for an optional driver inside the workflow, as `importlib.util.find_spec("redis")` does, also
stops the replay. Label every write through a driver `@irimi.function(write=True)`, then allow it
by name with `irimi replay <run id> --dangerously-allow-unpatched psycopg` or
`irimi.init(dangerously_allow_unpatched=["psycopg"])`. Upgrade an HTTP client irimi could not
patch, or allow it the same way if the workflow makes no calls through it.

A replay also points every client that honours proxy settings (`HTTP_PROXY`, `HTTPS_PROXY` and
`ALL_PROXY`), such as `urllib.request` or `curl`, at a proxy of irimi's own. A plain HTTP call it
catches is answered like any other call in the replay: from the recording, faked, or sent for real
when it is a read. An HTTPS connection it catches is refused, since irimi cannot see what would be
sent through it, and is recorded as `CONNECT <host>`. Both are flagged `caught_by_proxy`. During a
replay your own `NO_PROXY` is ignored and your `HTTP_PROXY`, `HTTPS_PROXY` and `ALL_PROXY` are
replaced, so where calls out of your network need a company proxy, the replay's live calls fail.

Some writes still really happen: those through a client irimi does not know by name, HTTP sent by a
client that ignores proxy settings, such as `http.client` used directly, a call the proxy catches on
its way to a host while irimi has a live call of its own open to that same host, and writes a
process the workflow starts, such as a `multiprocessing` worker, makes other than through a labeled
function. In such a process functions marked `@irimi.function(write=True)` are still faked and
nothing is recorded, but irimi intercepts no HTTP client there: HTTP reaches the replay's proxy only
from a client that honours proxy settings, and a database driver there is not guarded. Code that
catches the refusal keeps the replay running, without the driver it tried to import.

Early development. Source and issues: https://github.com/irimi-io/Irimi
