Database Copy-On-Write for AI agent workspace isolation
Project description
agent-cow
Database Copy-On-Write for AI agent workspace isolation
agent-cow intercepts your AI agent's database writes and isolates them in a copy-on-write layer. The agent thinks it's modifying real data, but nothing touches production until you approve. Zero changes to your existing queries.
Read the full article: Copy-on-Write in Agentic Systems Try the interactive demo: www.agent-cow.com
Without agent-cow: With agent-cow:
┌───────┐ ┌──────────┐ ┌───────┐ ┌──────┐ ┌──────────┐
│ Agent │──────>│ Database │ │ Agent │────>│ COW │────>│ Database │
└───────┘ └──────────┘ └───────┘ │ View │ └──────────┘
writes directly └──────┘
to production writes go to changes table
reads merge base + changes
user reviews, then commits or discards
Installation
pip install agent-cow
Requires Python 3.10+.
How It Works
- Renames your table from
userstousers_base - Creates a changes table
users_changesto store session-specific modifications - Creates a COW view named
usersthat merges base + changes - Your code doesn't change — queries still target
users(now a view)
When you set app.session_id and app.operation_id variables, all writes go to the changes table. Reads automatically merge base data with your session's changes. Other sessions (and production) see only the base data.
See the interactive demo for a worked example of an inventory management system where an agent makes both good and bad decisions.
Why Copy-on-Write for agents?
Alignment is an open problem in AI safety, and misalignment during agent execution may not always be obvious. At best, a misaligned agent is annoying (i.e. if the agent does something other than what the user wants it to do) and at worst, dangerous (i.e. leading to sensitive data loss, tool misuse, and other harms). Rather than tackling the alignment problem directly, this repo focuses on minimizing potential harm a misaligned agent can cause.
- Changes can be reviewed at the end of a session, rather than needing to repeatedly 'accept' each action as it is executed. This minimizes the direct human supervision required while improving the safeguards in place.
- Mistakes are less consequential, since the agent can't write directly to the main/production data. If some changes are good but others aren't, users can cherry-pick operations they wish to keep.
- Misalignment patterns become more visible. When reviewing changes at the end of a session, users can clearly identify where the agent deviated from intended behavior and adjust the system prompt or agent configuration accordingly to prevent similar issues in future sessions.
- Multiple agents or agent sessions can run simultaneously on isolated copies without interfering with each other.
Backends
| Backend | Docs | Status |
|---|---|---|
| PostgreSQL | agentcow/postgres | Available |
| pg-lite (TypeScript) | agent-cow-typescript | Available |
| Blob/File Storage | — | In progress |
Quick Example (PostgreSQL)
import uuid
from agentcow.postgres import deploy_cow_functions, enable_cow_schema, apply_cow_variables, commit_cow_session
# Wrap any async PostgreSQL driver — asyncpg, SQLAlchemy, psycopg, etc.
class MyExecutor:
def __init__(self, conn):
self._conn = conn
async def execute(self, sql: str) -> list[tuple]:
return [tuple(r) for r in await self._conn.fetch(sql)]
executor = MyExecutor(conn)
# One-time setup — enables COW on all tables in the schema
await deploy_cow_functions(executor)
await enable_cow_schema(executor)
# Agent session — all writes are isolated
session_id = uuid.uuid4()
await apply_cow_variables(executor, session_id, operation_id=uuid.uuid4())
await executor.execute("INSERT INTO users (name) VALUES ('Bessie')")
# Review, then commit or discard
await commit_cow_session(executor, "users", session_id)
See the PostgreSQL docs for the full guide: driver adapters, schema-wide setup, selective commit, web framework integration, and the complete API reference.
API Reference
Core Functions
deploy_cow_functions(executor)— Deploy COW SQL functions (one-time setup)enable_cow(executor, table_name)— Enable COW on a tableenable_cow_schema(executor)— Enable COW on all tables in a schemadisable_cow(executor, table_name)— Disable COW and restore original tabledisable_cow_schema(executor)— Disable COW on all tables in a schemacommit_cow_session(executor, table_name, session_id)— Commit all session changesdiscard_cow_session(executor, table_name, session_id)— Discard all session changesget_cow_status(executor)— Get COW status for a schema
Operation-Level Functions
apply_cow_variables(executor, session_id, operation_id)— Set COW session variablesget_session_operations(executor, session_id)— List all operations in a sessionget_operation_dependencies(executor, session_id)— Get operation dependency graphcommit_cow_operations(executor, table_name, session_id, operation_ids)— Commit specific operationsdiscard_cow_operations(executor, table_name, session_id, operation_ids)— Discard specific operations
Session Management
CowPostgresConfig— Dataclass for COW configurationbuild_cow_variable_statements(session_id, operation_id)— Build SET LOCAL SQL statements
For parsing COW configuration from HTTP request headers (e.g. in FastAPI/Django/Flask middleware), see agentcow/examples/header_parsing_example.py.
Development
git clone https://github.com/trail-ml/agent-cow-python.git
cd agent-cow-python
pip install -e ".[dev]"
pytest agentcow/postgres/tests/ -v
Contributing
We welcome contributions! For questions, bug reports, or feature requests, please open an issue.
License
MIT License.
Credits
Created and maintained by trail.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agent_cow-0.1.4.tar.gz.
File metadata
- Download URL: agent_cow-0.1.4.tar.gz
- Upload date:
- Size: 111.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9573f04d1550235ffc62ab9444c7cfac4bc806e3fb470de4bea4034134117a91
|
|
| MD5 |
ffe8a1da57bb9ec4fe299fdf63a5537f
|
|
| BLAKE2b-256 |
c1990a8412d690ed04552415dd473413423eca8a19671800d2ea98224ecaaca6
|
Provenance
The following attestation bundles were made for agent_cow-0.1.4.tar.gz:
Publisher:
publish.yml on trail-ml/agent-cow-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_cow-0.1.4.tar.gz -
Subject digest:
9573f04d1550235ffc62ab9444c7cfac4bc806e3fb470de4bea4034134117a91 - Sigstore transparency entry: 1277980732
- Sigstore integration time:
-
Permalink:
trail-ml/agent-cow-python@5cdff8f1a3c2dbcba665dc54e7991e038c655a89 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/trail-ml
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5cdff8f1a3c2dbcba665dc54e7991e038c655a89 -
Trigger Event:
push
-
Statement type:
File details
Details for the file agent_cow-0.1.4-py3-none-any.whl.
File metadata
- Download URL: agent_cow-0.1.4-py3-none-any.whl
- Upload date:
- Size: 53.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5b3cb068b92edacbe9fff520eb2131e36527d0667dc15ef469016f633d1523d
|
|
| MD5 |
92da38373108ce7097f50143b6e80034
|
|
| BLAKE2b-256 |
9bd3b53bd37d0911985a01c9e826023c14d2189d09e3a7de795a7cd8d61f9847
|
Provenance
The following attestation bundles were made for agent_cow-0.1.4-py3-none-any.whl:
Publisher:
publish.yml on trail-ml/agent-cow-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_cow-0.1.4-py3-none-any.whl -
Subject digest:
e5b3cb068b92edacbe9fff520eb2131e36527d0667dc15ef469016f633d1523d - Sigstore transparency entry: 1277980811
- Sigstore integration time:
-
Permalink:
trail-ml/agent-cow-python@5cdff8f1a3c2dbcba665dc54e7991e038c655a89 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/trail-ml
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5cdff8f1a3c2dbcba665dc54e7991e038c655a89 -
Trigger Event:
push
-
Statement type: