isolate-tx 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- isolate_tx-0.1.0/.gitignore +17 -0
- isolate_tx-0.1.0/LICENSE +21 -0
- isolate_tx-0.1.0/PKG-INFO +342 -0
- isolate_tx-0.1.0/README.md +309 -0
- isolate_tx-0.1.0/examples/refund_race.py +124 -0
- isolate_tx-0.1.0/pyproject.toml +62 -0
- isolate_tx-0.1.0/src/isolate_tx/__init__.py +55 -0
- isolate_tx-0.1.0/src/isolate_tx/_async.py +276 -0
- isolate_tx-0.1.0/src/isolate_tx/_client.py +493 -0
- isolate_tx-0.1.0/src/isolate_tx/_errors.py +149 -0
- isolate_tx-0.1.0/src/isolate_tx/_http.py +81 -0
- isolate_tx-0.1.0/src/isolate_tx/_version.py +1 -0
- isolate_tx-0.1.0/src/isolate_tx/py.typed +0 -0
- isolate_tx-0.1.0/tests/fake_server.py +282 -0
- isolate_tx-0.1.0/tests/test_async.py +126 -0
- isolate_tx-0.1.0/tests/test_errors.py +166 -0
- isolate_tx-0.1.0/tests/test_run.py +245 -0
- isolate_tx-0.1.0/tests/test_transaction.py +236 -0
isolate_tx-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Isolate contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: isolate-tx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for Isolate, a transaction server that keeps concurrent AI agents from overwriting each other's work.
|
|
5
|
+
Project-URL: Homepage, https://github.com/tathagata1010/isolate-tx
|
|
6
|
+
Project-URL: Repository, https://github.com/tathagata1010/isolate-tx
|
|
7
|
+
Project-URL: Documentation, https://github.com/tathagata1010/isolate-tx/tree/main/clients/python#readme
|
|
8
|
+
Project-URL: HTTP API, https://github.com/tathagata1010/isolate-tx/blob/main/docs/http-api.md
|
|
9
|
+
Project-URL: Issues, https://github.com/tathagata1010/isolate-tx/issues
|
|
10
|
+
Author: Isolate contributors
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: ai-agents,concurrency,crewai,langgraph,llm,multi-agent,transactions
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Framework :: AsyncIO
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
27
|
+
Classifier: Topic :: Database
|
|
28
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
29
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: >=3.9
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# isolate-tx
|
|
35
|
+
|
|
36
|
+
The official Python client for [Isolate](https://github.com/tathagata1010/isolate-tx),
|
|
37
|
+
a transaction server for AI agents that share state.
|
|
38
|
+
|
|
39
|
+
When two agents read the same data, think for a few seconds, and then write, one of them can
|
|
40
|
+
silently overwrite the other. Isolate gives each agent step a transaction: reads come from a
|
|
41
|
+
consistent snapshot, writes stay private until commit, and a conflicting commit is rejected so the
|
|
42
|
+
step can be retried on fresh data. Irreversible actions (refunds, emails) are queued and sent only
|
|
43
|
+
after commit.
|
|
44
|
+
|
|
45
|
+
- Python 3.9+, zero runtime dependencies (standard library only)
|
|
46
|
+
- Sync and asyncio APIs, fully typed (`py.typed`)
|
|
47
|
+
- Automatic retries with backoff for conflicts
|
|
48
|
+
|
|
49
|
+
## 30-second quickstart
|
|
50
|
+
|
|
51
|
+
Start a server:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
docker run -p 127.0.0.1:7070:7070 ghcr.io/tathagata1010/isolate
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Install the client:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install isolate-tx
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Use it:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from isolate_tx import Isolate
|
|
67
|
+
|
|
68
|
+
iso = Isolate() # uses $ISOLATE_URL, default http://localhost:7070
|
|
69
|
+
|
|
70
|
+
with iso.transaction(agent="hello") as tx:
|
|
71
|
+
tx.write("greetings/en", "hello")
|
|
72
|
+
|
|
73
|
+
print(iso.get("greetings/en")) # hello
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The package is called `isolate-tx` on PyPI and imported as `isolate_tx`.
|
|
77
|
+
|
|
78
|
+
## The refund example
|
|
79
|
+
|
|
80
|
+
Two support agents each want to approve a $70 refund from the same $100 budget. Without
|
|
81
|
+
coordination, both read $100, both approve, and $140 goes out. With a `serializable` transaction the
|
|
82
|
+
second commit fails with a conflict, `run` retries it, the retry reads the new $30 balance, and the
|
|
83
|
+
agent declines.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from isolate_tx import Isolate
|
|
87
|
+
|
|
88
|
+
iso = Isolate()
|
|
89
|
+
BUDGET = "refunds/account-42" # balance in cents, stored as a string
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def approve_refund(tx, amount_cents: int) -> bool:
|
|
93
|
+
available = int(tx.read(BUDGET) or "0")
|
|
94
|
+
# ... call your LLM here; this can take seconds ...
|
|
95
|
+
if available < amount_cents:
|
|
96
|
+
return False
|
|
97
|
+
tx.write(BUDGET, str(available - amount_cents))
|
|
98
|
+
tx.after_commit("issue-refund", {"account": "42", "amount": amount_cents})
|
|
99
|
+
return True
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
approved = iso.run(
|
|
103
|
+
lambda tx: approve_refund(tx, 7000),
|
|
104
|
+
agent="refund-agent",
|
|
105
|
+
isolation="serializable",
|
|
106
|
+
)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`after_commit` queues a webhook that the server sends only after the transaction commits, so a
|
|
110
|
+
failed or retried attempt never sends money. The effect name (`issue-refund`) must be configured on
|
|
111
|
+
the server, for example `-e ISOLATE_EFFECTS="issue-refund=https://payments.internal/refund"`.
|
|
112
|
+
|
|
113
|
+
[`examples/refund_race.py`](examples/refund_race.py) runs this race with two threads, once with
|
|
114
|
+
`read_committed` and once with `serializable`:
|
|
115
|
+
|
|
116
|
+
```text
|
|
117
|
+
READ_COMMITTED $140.00 refunded from a $100.00 budget; recorded balance $30.00
|
|
118
|
+
refunds issued: 2; retries: 0; invariant: VIOLATED
|
|
119
|
+
SERIALIZABLE $70.00 refunded from a $100.00 budget; recorded balance $30.00
|
|
120
|
+
refunds issued: 1; retries: 1; invariant: PRESERVED
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## `run` vs. the context manager
|
|
124
|
+
|
|
125
|
+
**`iso.run(fn, ...)`** is what you want most of the time. It begins a transaction, calls `fn(tx)`,
|
|
126
|
+
commits, and returns what `fn` returned. If the commit (or any call) fails with a retryable abort,
|
|
127
|
+
it begins a new transaction with `retryOf` set to the failed attempt's id and calls `fn` again,
|
|
128
|
+
with exponential backoff and full jitter (10 ms base, 1 s cap, 8 attempts by default). Because `fn`
|
|
129
|
+
can run more than once, keep side effects in `tx.after_commit`.
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
result = iso.run(fn, agent="planner", isolation="serializable", max_attempts=8)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The decorator form injects `tx` as the first argument:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
@iso.transactional(agent="refund-agent", isolation="serializable")
|
|
139
|
+
def approve(tx, amount_cents):
|
|
140
|
+
...
|
|
141
|
+
|
|
142
|
+
approve(7000)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**`with iso.transaction(...) as tx:`** runs exactly one attempt. It commits when the block ends
|
|
146
|
+
normally (the `CommitResult` is in `tx.result`) and aborts if the block raises. A conflict raises
|
|
147
|
+
`TxAborted` from the end of the `with` block; retrying is up to you.
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from isolate_tx import TxAborted
|
|
151
|
+
|
|
152
|
+
try:
|
|
153
|
+
with iso.transaction(agent="refund-agent", isolation="serializable") as tx:
|
|
154
|
+
budget = tx.read_json("budgets/42", default={"cents": 0})
|
|
155
|
+
budget["cents"] -= 7000
|
|
156
|
+
tx.write_json("budgets/42", budget)
|
|
157
|
+
tx.record_tokens(1850) # counts as useful or wasted in /v1/stats
|
|
158
|
+
print("committed at", tx.result.commit_ts)
|
|
159
|
+
except TxAborted as e:
|
|
160
|
+
if e.retryable:
|
|
161
|
+
... # begin again with iso.transaction(..., retry_of=e.tx_id)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Transaction methods
|
|
165
|
+
|
|
166
|
+
| Method | Does |
|
|
167
|
+
|---|---|
|
|
168
|
+
| `tx.read(key)` | Value at the snapshot, or `None` |
|
|
169
|
+
| `tx.read_many(keys)` | `dict[key, str \| None]` in one round trip |
|
|
170
|
+
| `tx.read_json(key, default=None)` | `json.loads` of the value |
|
|
171
|
+
| `tx.write(key, value)` | Buffer a string value; `None` deletes |
|
|
172
|
+
| `tx.write_many(mapping)` | Several writes in one round trip |
|
|
173
|
+
| `tx.write_json(key, obj)` | `json.dumps` and write |
|
|
174
|
+
| `tx.delete(key)` | Same as `write(key, None)` |
|
|
175
|
+
| `tx.after_commit(name, payload)` | Queue a server-configured effect; non-string payloads are `json.dumps`-ed |
|
|
176
|
+
| `tx.record_tokens(n)` | Add tokens spent; returns the running total |
|
|
177
|
+
| `tx.commit()` / `tx.abort()` | Finish explicitly (both optional inside `with` or `run`) |
|
|
178
|
+
| `tx.info()` | Server-side status of the transaction |
|
|
179
|
+
|
|
180
|
+
Transaction options (all keyword, all optional except `agent`): `isolation` (`"read_committed"`,
|
|
181
|
+
`"snapshot"`, `"serializable"`), `policy` (`"optimistic"`, `"pessimistic"`, `"adaptive"`),
|
|
182
|
+
`expected_tokens`, `lock_timeout_ms`, `idle_timeout_ms`. Options you leave out are not sent, so the
|
|
183
|
+
server defaults apply (`snapshot`, `optimistic`).
|
|
184
|
+
|
|
185
|
+
Outside a transaction: `iso.get(key)`, `iso.stats()`, `iso.health()`.
|
|
186
|
+
|
|
187
|
+
### Configuration
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
Isolate(url=None, api_key=None, timeout=30.0)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`url` falls back to `ISOLATE_URL`, then `http://localhost:7070`. `api_key` falls back to
|
|
194
|
+
`ISOLATE_API_KEY` and is sent as `Authorization: Bearer <key>`. Ids and timestamps from the server
|
|
195
|
+
are strings (they are 64-bit values).
|
|
196
|
+
|
|
197
|
+
## Async
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
import asyncio
|
|
201
|
+
from isolate_tx import AsyncIsolate
|
|
202
|
+
|
|
203
|
+
iso = AsyncIsolate()
|
|
204
|
+
|
|
205
|
+
async def approve(tx, amount_cents):
|
|
206
|
+
available = int(await tx.read("refunds/account-42") or "0")
|
|
207
|
+
if available < amount_cents:
|
|
208
|
+
return False
|
|
209
|
+
await tx.write("refunds/account-42", str(available - amount_cents))
|
|
210
|
+
await tx.after_commit("issue-refund", {"amount": amount_cents})
|
|
211
|
+
return True
|
|
212
|
+
|
|
213
|
+
async def main():
|
|
214
|
+
ok = await iso.run(lambda tx: approve(tx, 7000), agent="refund-agent", isolation="serializable")
|
|
215
|
+
|
|
216
|
+
async with iso.transaction(agent="auditor") as tx:
|
|
217
|
+
print(await tx.read("refunds/account-42"))
|
|
218
|
+
|
|
219
|
+
asyncio.run(main())
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`AsyncIsolate` has the same methods as `Isolate`, including `@iso.transactional` for `async def`
|
|
223
|
+
functions. It runs the synchronous client in worker threads with `asyncio.to_thread`. That keeps
|
|
224
|
+
the package free of dependencies (the standard library has no async HTTP client) and never blocks
|
|
225
|
+
the event loop; a round trip to Isolate is small next to an LLM call.
|
|
226
|
+
|
|
227
|
+
## Errors
|
|
228
|
+
|
|
229
|
+
Every error is an `IsolateError` with `.status`, `.error`, `.message`, `.reason`, `.retryable`,
|
|
230
|
+
`.tx_id` and the raw `.body`.
|
|
231
|
+
|
|
232
|
+
| Exception | HTTP | `error` | What to do |
|
|
233
|
+
|---|---|---|---|
|
|
234
|
+
| `InvalidRequest` | 400, 413 | `invalid_request`, `too_large` | Fix the request |
|
|
235
|
+
| `Unauthorized` | 401 | `unauthorized` | Set `api_key` / `ISOLATE_API_KEY` |
|
|
236
|
+
| `TransactionNotFound` | 404 | `tx_not_found` | Begin a new transaction |
|
|
237
|
+
| `TxAborted` | 409 | `aborted` | If `.retryable`, retry with `retry_of=e.tx_id` (`run` does this) |
|
|
238
|
+
| `TransactionNotActive` | 409 | `not_active` | The transaction already finished; begin a new one |
|
|
239
|
+
| `UnknownEffect` | 422 | `unknown_effect` | Configure the effect on the server or fix the name |
|
|
240
|
+
| `TooManyTransactions` | 429 | `too_many_transactions` | Back off and retry (`run` does this) |
|
|
241
|
+
| `InDoubt` | 500 | `in_doubt` | Outcome unknown; check after the server restarts, do not retry blindly |
|
|
242
|
+
| `IsolateError` | 500 | `internal_error` | Report it |
|
|
243
|
+
| `Fenced` | 503 | `fenced` | Talk to the new coordinator |
|
|
244
|
+
| `IsolateConnectionError` | (none) | `connection_error` | Server unreachable or timed out (`status == 0`) |
|
|
245
|
+
|
|
246
|
+
`TxAborted.reason` is one of `WRITE_WRITE_CONFLICT`, `READ_WRITE_CONFLICT`, `WOUNDED`,
|
|
247
|
+
`LOCK_TIMEOUT` (retryable), or `USER_ABORT`, `IDLE_TIMEOUT`, `STEP_FAILED`, `INTERRUPTED`,
|
|
248
|
+
`INTERNAL_ERROR`, `FENCED` (not retryable).
|
|
249
|
+
|
|
250
|
+
## Using it with agent frameworks
|
|
251
|
+
|
|
252
|
+
These are illustrative patterns, not official integrations. The idea is the same everywhere: wrap
|
|
253
|
+
the step that reads and writes shared state in `iso.run`, so the read, the decision and the write
|
|
254
|
+
commit together or retry together.
|
|
255
|
+
|
|
256
|
+
### LangGraph (illustrative)
|
|
257
|
+
|
|
258
|
+
Wrap a node's body. The graph state carries inputs; shared business data lives in Isolate.
|
|
259
|
+
|
|
260
|
+
```python
|
|
261
|
+
from isolate_tx import Isolate
|
|
262
|
+
|
|
263
|
+
iso = Isolate()
|
|
264
|
+
|
|
265
|
+
def refund_node(state: dict) -> dict:
|
|
266
|
+
def step(tx):
|
|
267
|
+
available = int(tx.read(f"budgets/{state['account']}") or "0")
|
|
268
|
+
decision = llm_decide(state["ticket"], available) # your model call
|
|
269
|
+
if decision.approve and decision.amount <= available:
|
|
270
|
+
tx.write(f"budgets/{state['account']}", str(available - decision.amount))
|
|
271
|
+
tx.after_commit("issue-refund", {"account": state["account"], "amount": decision.amount})
|
|
272
|
+
return {"decision": decision.model_dump()}
|
|
273
|
+
|
|
274
|
+
return iso.run(step, agent="langgraph-refund-node", isolation="serializable")
|
|
275
|
+
|
|
276
|
+
# graph.add_node("refund", refund_node)
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### CrewAI (illustrative)
|
|
280
|
+
|
|
281
|
+
Wrap a tool function so every call is one transaction.
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
from crewai.tools import tool
|
|
285
|
+
from isolate_tx import Isolate
|
|
286
|
+
|
|
287
|
+
iso = Isolate()
|
|
288
|
+
|
|
289
|
+
@tool("Approve refund")
|
|
290
|
+
def approve_refund(account: str, amount_cents: int) -> str:
|
|
291
|
+
"""Approve a refund if the account's budget allows it."""
|
|
292
|
+
def step(tx):
|
|
293
|
+
available = int(tx.read(f"budgets/{account}") or "0")
|
|
294
|
+
if available < amount_cents:
|
|
295
|
+
return f"declined: only {available} cents left"
|
|
296
|
+
tx.write(f"budgets/{account}", str(available - amount_cents))
|
|
297
|
+
tx.after_commit("issue-refund", {"account": account, "amount": amount_cents})
|
|
298
|
+
return "approved"
|
|
299
|
+
|
|
300
|
+
return iso.run(step, agent="crewai-support", isolation="serializable")
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### OpenAI Agents SDK (illustrative)
|
|
304
|
+
|
|
305
|
+
Same pattern inside a `function_tool`; use `AsyncIsolate` if your tools are async.
|
|
306
|
+
|
|
307
|
+
```python
|
|
308
|
+
from agents import function_tool
|
|
309
|
+
from isolate_tx import AsyncIsolate
|
|
310
|
+
|
|
311
|
+
iso = AsyncIsolate()
|
|
312
|
+
|
|
313
|
+
@function_tool
|
|
314
|
+
async def approve_refund(account: str, amount_cents: int) -> str:
|
|
315
|
+
"""Approve a refund if the account's budget allows it."""
|
|
316
|
+
async def step(tx):
|
|
317
|
+
available = int(await tx.read(f"budgets/{account}") or "0")
|
|
318
|
+
if available < amount_cents:
|
|
319
|
+
return "declined"
|
|
320
|
+
await tx.write(f"budgets/{account}", str(available - amount_cents))
|
|
321
|
+
await tx.after_commit("issue-refund", {"account": account, "amount": amount_cents})
|
|
322
|
+
return "approved"
|
|
323
|
+
|
|
324
|
+
return await iso.run(step, agent="openai-agents-refunds", isolation="serializable")
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
If the LLM call happens inside the transaction, call `tx.record_tokens(n)` with the tokens it used,
|
|
328
|
+
so `/v1/stats` can show how many tokens conflicts cost you.
|
|
329
|
+
|
|
330
|
+
## Development
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
cd clients/python
|
|
334
|
+
python -m unittest discover -s tests
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
The tests run against an in-memory fake of the HTTP API (`tests/fake_server.py`), so no server is
|
|
338
|
+
needed. The full API is documented in [`docs/http-api.md`](../../docs/http-api.md).
|
|
339
|
+
|
|
340
|
+
## License
|
|
341
|
+
|
|
342
|
+
MIT
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
# isolate-tx
|
|
2
|
+
|
|
3
|
+
The official Python client for [Isolate](https://github.com/tathagata1010/isolate-tx),
|
|
4
|
+
a transaction server for AI agents that share state.
|
|
5
|
+
|
|
6
|
+
When two agents read the same data, think for a few seconds, and then write, one of them can
|
|
7
|
+
silently overwrite the other. Isolate gives each agent step a transaction: reads come from a
|
|
8
|
+
consistent snapshot, writes stay private until commit, and a conflicting commit is rejected so the
|
|
9
|
+
step can be retried on fresh data. Irreversible actions (refunds, emails) are queued and sent only
|
|
10
|
+
after commit.
|
|
11
|
+
|
|
12
|
+
- Python 3.9+, zero runtime dependencies (standard library only)
|
|
13
|
+
- Sync and asyncio APIs, fully typed (`py.typed`)
|
|
14
|
+
- Automatic retries with backoff for conflicts
|
|
15
|
+
|
|
16
|
+
## 30-second quickstart
|
|
17
|
+
|
|
18
|
+
Start a server:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
docker run -p 127.0.0.1:7070:7070 ghcr.io/tathagata1010/isolate
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Install the client:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install isolate-tx
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Use it:
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from isolate_tx import Isolate
|
|
34
|
+
|
|
35
|
+
iso = Isolate() # uses $ISOLATE_URL, default http://localhost:7070
|
|
36
|
+
|
|
37
|
+
with iso.transaction(agent="hello") as tx:
|
|
38
|
+
tx.write("greetings/en", "hello")
|
|
39
|
+
|
|
40
|
+
print(iso.get("greetings/en")) # hello
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The package is called `isolate-tx` on PyPI and imported as `isolate_tx`.
|
|
44
|
+
|
|
45
|
+
## The refund example
|
|
46
|
+
|
|
47
|
+
Two support agents each want to approve a $70 refund from the same $100 budget. Without
|
|
48
|
+
coordination, both read $100, both approve, and $140 goes out. With a `serializable` transaction the
|
|
49
|
+
second commit fails with a conflict, `run` retries it, the retry reads the new $30 balance, and the
|
|
50
|
+
agent declines.
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from isolate_tx import Isolate
|
|
54
|
+
|
|
55
|
+
iso = Isolate()
|
|
56
|
+
BUDGET = "refunds/account-42" # balance in cents, stored as a string
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def approve_refund(tx, amount_cents: int) -> bool:
|
|
60
|
+
available = int(tx.read(BUDGET) or "0")
|
|
61
|
+
# ... call your LLM here; this can take seconds ...
|
|
62
|
+
if available < amount_cents:
|
|
63
|
+
return False
|
|
64
|
+
tx.write(BUDGET, str(available - amount_cents))
|
|
65
|
+
tx.after_commit("issue-refund", {"account": "42", "amount": amount_cents})
|
|
66
|
+
return True
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
approved = iso.run(
|
|
70
|
+
lambda tx: approve_refund(tx, 7000),
|
|
71
|
+
agent="refund-agent",
|
|
72
|
+
isolation="serializable",
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`after_commit` queues a webhook that the server sends only after the transaction commits, so a
|
|
77
|
+
failed or retried attempt never sends money. The effect name (`issue-refund`) must be configured on
|
|
78
|
+
the server, for example `-e ISOLATE_EFFECTS="issue-refund=https://payments.internal/refund"`.
|
|
79
|
+
|
|
80
|
+
[`examples/refund_race.py`](examples/refund_race.py) runs this race with two threads, once with
|
|
81
|
+
`read_committed` and once with `serializable`:
|
|
82
|
+
|
|
83
|
+
```text
|
|
84
|
+
READ_COMMITTED $140.00 refunded from a $100.00 budget; recorded balance $30.00
|
|
85
|
+
refunds issued: 2; retries: 0; invariant: VIOLATED
|
|
86
|
+
SERIALIZABLE $70.00 refunded from a $100.00 budget; recorded balance $30.00
|
|
87
|
+
refunds issued: 1; retries: 1; invariant: PRESERVED
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## `run` vs. the context manager
|
|
91
|
+
|
|
92
|
+
**`iso.run(fn, ...)`** is what you want most of the time. It begins a transaction, calls `fn(tx)`,
|
|
93
|
+
commits, and returns what `fn` returned. If the commit (or any call) fails with a retryable abort,
|
|
94
|
+
it begins a new transaction with `retryOf` set to the failed attempt's id and calls `fn` again,
|
|
95
|
+
with exponential backoff and full jitter (10 ms base, 1 s cap, 8 attempts by default). Because `fn`
|
|
96
|
+
can run more than once, keep side effects in `tx.after_commit`.
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
result = iso.run(fn, agent="planner", isolation="serializable", max_attempts=8)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The decorator form injects `tx` as the first argument:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
@iso.transactional(agent="refund-agent", isolation="serializable")
|
|
106
|
+
def approve(tx, amount_cents):
|
|
107
|
+
...
|
|
108
|
+
|
|
109
|
+
approve(7000)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**`with iso.transaction(...) as tx:`** runs exactly one attempt. It commits when the block ends
|
|
113
|
+
normally (the `CommitResult` is in `tx.result`) and aborts if the block raises. A conflict raises
|
|
114
|
+
`TxAborted` from the end of the `with` block; retrying is up to you.
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from isolate_tx import TxAborted
|
|
118
|
+
|
|
119
|
+
try:
|
|
120
|
+
with iso.transaction(agent="refund-agent", isolation="serializable") as tx:
|
|
121
|
+
budget = tx.read_json("budgets/42", default={"cents": 0})
|
|
122
|
+
budget["cents"] -= 7000
|
|
123
|
+
tx.write_json("budgets/42", budget)
|
|
124
|
+
tx.record_tokens(1850) # counts as useful or wasted in /v1/stats
|
|
125
|
+
print("committed at", tx.result.commit_ts)
|
|
126
|
+
except TxAborted as e:
|
|
127
|
+
if e.retryable:
|
|
128
|
+
... # begin again with iso.transaction(..., retry_of=e.tx_id)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Transaction methods
|
|
132
|
+
|
|
133
|
+
| Method | Does |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `tx.read(key)` | Value at the snapshot, or `None` |
|
|
136
|
+
| `tx.read_many(keys)` | `dict[key, str \| None]` in one round trip |
|
|
137
|
+
| `tx.read_json(key, default=None)` | `json.loads` of the value |
|
|
138
|
+
| `tx.write(key, value)` | Buffer a string value; `None` deletes |
|
|
139
|
+
| `tx.write_many(mapping)` | Several writes in one round trip |
|
|
140
|
+
| `tx.write_json(key, obj)` | `json.dumps` and write |
|
|
141
|
+
| `tx.delete(key)` | Same as `write(key, None)` |
|
|
142
|
+
| `tx.after_commit(name, payload)` | Queue a server-configured effect; non-string payloads are `json.dumps`-ed |
|
|
143
|
+
| `tx.record_tokens(n)` | Add tokens spent; returns the running total |
|
|
144
|
+
| `tx.commit()` / `tx.abort()` | Finish explicitly (both optional inside `with` or `run`) |
|
|
145
|
+
| `tx.info()` | Server-side status of the transaction |
|
|
146
|
+
|
|
147
|
+
Transaction options (all keyword, all optional except `agent`): `isolation` (`"read_committed"`,
|
|
148
|
+
`"snapshot"`, `"serializable"`), `policy` (`"optimistic"`, `"pessimistic"`, `"adaptive"`),
|
|
149
|
+
`expected_tokens`, `lock_timeout_ms`, `idle_timeout_ms`. Options you leave out are not sent, so the
|
|
150
|
+
server defaults apply (`snapshot`, `optimistic`).
|
|
151
|
+
|
|
152
|
+
Outside a transaction: `iso.get(key)`, `iso.stats()`, `iso.health()`.
|
|
153
|
+
|
|
154
|
+
### Configuration
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
Isolate(url=None, api_key=None, timeout=30.0)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`url` falls back to `ISOLATE_URL`, then `http://localhost:7070`. `api_key` falls back to
|
|
161
|
+
`ISOLATE_API_KEY` and is sent as `Authorization: Bearer <key>`. Ids and timestamps from the server
|
|
162
|
+
are strings (they are 64-bit values).
|
|
163
|
+
|
|
164
|
+
## Async
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
import asyncio
|
|
168
|
+
from isolate_tx import AsyncIsolate
|
|
169
|
+
|
|
170
|
+
iso = AsyncIsolate()
|
|
171
|
+
|
|
172
|
+
async def approve(tx, amount_cents):
|
|
173
|
+
available = int(await tx.read("refunds/account-42") or "0")
|
|
174
|
+
if available < amount_cents:
|
|
175
|
+
return False
|
|
176
|
+
await tx.write("refunds/account-42", str(available - amount_cents))
|
|
177
|
+
await tx.after_commit("issue-refund", {"amount": amount_cents})
|
|
178
|
+
return True
|
|
179
|
+
|
|
180
|
+
async def main():
|
|
181
|
+
ok = await iso.run(lambda tx: approve(tx, 7000), agent="refund-agent", isolation="serializable")
|
|
182
|
+
|
|
183
|
+
async with iso.transaction(agent="auditor") as tx:
|
|
184
|
+
print(await tx.read("refunds/account-42"))
|
|
185
|
+
|
|
186
|
+
asyncio.run(main())
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`AsyncIsolate` has the same methods as `Isolate`, including `@iso.transactional` for `async def`
|
|
190
|
+
functions. It runs the synchronous client in worker threads with `asyncio.to_thread`. That keeps
|
|
191
|
+
the package free of dependencies (the standard library has no async HTTP client) and never blocks
|
|
192
|
+
the event loop; a round trip to Isolate is small next to an LLM call.
|
|
193
|
+
|
|
194
|
+
## Errors
|
|
195
|
+
|
|
196
|
+
Every error is an `IsolateError` with `.status`, `.error`, `.message`, `.reason`, `.retryable`,
|
|
197
|
+
`.tx_id` and the raw `.body`.
|
|
198
|
+
|
|
199
|
+
| Exception | HTTP | `error` | What to do |
|
|
200
|
+
|---|---|---|---|
|
|
201
|
+
| `InvalidRequest` | 400, 413 | `invalid_request`, `too_large` | Fix the request |
|
|
202
|
+
| `Unauthorized` | 401 | `unauthorized` | Set `api_key` / `ISOLATE_API_KEY` |
|
|
203
|
+
| `TransactionNotFound` | 404 | `tx_not_found` | Begin a new transaction |
|
|
204
|
+
| `TxAborted` | 409 | `aborted` | If `.retryable`, retry with `retry_of=e.tx_id` (`run` does this) |
|
|
205
|
+
| `TransactionNotActive` | 409 | `not_active` | The transaction already finished; begin a new one |
|
|
206
|
+
| `UnknownEffect` | 422 | `unknown_effect` | Configure the effect on the server or fix the name |
|
|
207
|
+
| `TooManyTransactions` | 429 | `too_many_transactions` | Back off and retry (`run` does this) |
|
|
208
|
+
| `InDoubt` | 500 | `in_doubt` | Outcome unknown; check after the server restarts, do not retry blindly |
|
|
209
|
+
| `IsolateError` | 500 | `internal_error` | Report it |
|
|
210
|
+
| `Fenced` | 503 | `fenced` | Talk to the new coordinator |
|
|
211
|
+
| `IsolateConnectionError` | (none) | `connection_error` | Server unreachable or timed out (`status == 0`) |
|
|
212
|
+
|
|
213
|
+
`TxAborted.reason` is one of `WRITE_WRITE_CONFLICT`, `READ_WRITE_CONFLICT`, `WOUNDED`,
|
|
214
|
+
`LOCK_TIMEOUT` (retryable), or `USER_ABORT`, `IDLE_TIMEOUT`, `STEP_FAILED`, `INTERRUPTED`,
|
|
215
|
+
`INTERNAL_ERROR`, `FENCED` (not retryable).
|
|
216
|
+
|
|
217
|
+
## Using it with agent frameworks
|
|
218
|
+
|
|
219
|
+
These are illustrative patterns, not official integrations. The idea is the same everywhere: wrap
|
|
220
|
+
the step that reads and writes shared state in `iso.run`, so the read, the decision and the write
|
|
221
|
+
commit together or retry together.
|
|
222
|
+
|
|
223
|
+
### LangGraph (illustrative)
|
|
224
|
+
|
|
225
|
+
Wrap a node's body. The graph state carries inputs; shared business data lives in Isolate.
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
from isolate_tx import Isolate
|
|
229
|
+
|
|
230
|
+
iso = Isolate()
|
|
231
|
+
|
|
232
|
+
def refund_node(state: dict) -> dict:
|
|
233
|
+
def step(tx):
|
|
234
|
+
available = int(tx.read(f"budgets/{state['account']}") or "0")
|
|
235
|
+
decision = llm_decide(state["ticket"], available) # your model call
|
|
236
|
+
if decision.approve and decision.amount <= available:
|
|
237
|
+
tx.write(f"budgets/{state['account']}", str(available - decision.amount))
|
|
238
|
+
tx.after_commit("issue-refund", {"account": state["account"], "amount": decision.amount})
|
|
239
|
+
return {"decision": decision.model_dump()}
|
|
240
|
+
|
|
241
|
+
return iso.run(step, agent="langgraph-refund-node", isolation="serializable")
|
|
242
|
+
|
|
243
|
+
# graph.add_node("refund", refund_node)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### CrewAI (illustrative)
|
|
247
|
+
|
|
248
|
+
Wrap a tool function so every call is one transaction.
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
from crewai.tools import tool
|
|
252
|
+
from isolate_tx import Isolate
|
|
253
|
+
|
|
254
|
+
iso = Isolate()
|
|
255
|
+
|
|
256
|
+
@tool("Approve refund")
|
|
257
|
+
def approve_refund(account: str, amount_cents: int) -> str:
|
|
258
|
+
"""Approve a refund if the account's budget allows it."""
|
|
259
|
+
def step(tx):
|
|
260
|
+
available = int(tx.read(f"budgets/{account}") or "0")
|
|
261
|
+
if available < amount_cents:
|
|
262
|
+
return f"declined: only {available} cents left"
|
|
263
|
+
tx.write(f"budgets/{account}", str(available - amount_cents))
|
|
264
|
+
tx.after_commit("issue-refund", {"account": account, "amount": amount_cents})
|
|
265
|
+
return "approved"
|
|
266
|
+
|
|
267
|
+
return iso.run(step, agent="crewai-support", isolation="serializable")
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### OpenAI Agents SDK (illustrative)
|
|
271
|
+
|
|
272
|
+
Same pattern inside a `function_tool`; use `AsyncIsolate` if your tools are async.
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
from agents import function_tool
|
|
276
|
+
from isolate_tx import AsyncIsolate
|
|
277
|
+
|
|
278
|
+
iso = AsyncIsolate()
|
|
279
|
+
|
|
280
|
+
@function_tool
|
|
281
|
+
async def approve_refund(account: str, amount_cents: int) -> str:
|
|
282
|
+
"""Approve a refund if the account's budget allows it."""
|
|
283
|
+
async def step(tx):
|
|
284
|
+
available = int(await tx.read(f"budgets/{account}") or "0")
|
|
285
|
+
if available < amount_cents:
|
|
286
|
+
return "declined"
|
|
287
|
+
await tx.write(f"budgets/{account}", str(available - amount_cents))
|
|
288
|
+
await tx.after_commit("issue-refund", {"account": account, "amount": amount_cents})
|
|
289
|
+
return "approved"
|
|
290
|
+
|
|
291
|
+
return await iso.run(step, agent="openai-agents-refunds", isolation="serializable")
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
If the LLM call happens inside the transaction, call `tx.record_tokens(n)` with the tokens it used,
|
|
295
|
+
so `/v1/stats` can show how many tokens conflicts cost you.
|
|
296
|
+
|
|
297
|
+
## Development
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
cd clients/python
|
|
301
|
+
python -m unittest discover -s tests
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
The tests run against an in-memory fake of the HTTP API (`tests/fake_server.py`), so no server is
|
|
305
|
+
needed. The full API is documented in [`docs/http-api.md`](../../docs/http-api.md).
|
|
306
|
+
|
|
307
|
+
## License
|
|
308
|
+
|
|
309
|
+
MIT
|