Storage Architecture: Encrypted On-Disk Body Archive (new in v2.0.45+)
Why this exists
Section titled “Why this exists”In earlier releases, two write paths (the transparent proxy and the agent web proxy) stored the full request and response payloads inline in the usage table’s metadata column. In real-world deployments this caused the SQLite database to grow to 12+ GB within days (about 2 MB per row, with some rows reaching 13 MB). The bloat made admin analytics queries stall and contributed to recurring container out-of-memory restarts.
v2.0.45 splits payload storage into two layers:
| Layer | Carries | Where | Why |
|---|---|---|---|
usage row |
Small metadata + a bodyArchive pointer (~1–2 KB total) |
SQLite / PostgreSQL | Fast index lookup; analytics aggregation |
| Request Body Archive (new) | Full request + response, AES-256-GCM encrypted, gzip compressed, one file per request | {File-Archiver-base}/{org}/bodies/YYYY/MM/DD/HH/{requestId}.json.gz.enc |
Bounded DB size, encrypted at rest, retention-driven |
The body archive lives under the same root the admin configured for the File Archiver Default Sink (eventPipelineFileArchiverBasePath, default ./data/events). Customer-mounted NFS / EBS / S3FS volumes for archived data are honored transparently.
File format
Section titled “File format”Each .json.gz.enc file is a single binary blob:
[ IV (12 bytes, random per file) ][ AES-256-GCM auth tag (16 bytes) ][ ciphertext = AES-256-GCM-encrypted gzip-compressed JSON payload ]The plaintext payload (post-decrypt, post-gunzip) is:
{ "request_id": "req_1779988853166_hdo8gga3e", "timestamp": "2026-05-29T15:42:11.123Z", "request": { /* full inbound request body */ }, "response": { /* full upstream response body */ }, "meta": { "source": "transparent-proxy", "provider": "openai", "endpoint": "/v1/chat/completions" }}Encryption key management
Section titled “Encryption key management”- Key source priority:
BODY_ARCHIVE_KEY_HEXenv var →CHAT_ENCRYPTION_KEYenv var → the conversation-history key (derived with HKDF-SHA256 fromENCRYPTION_KEY, else from the database key). - The source material is SHA-256-hashed to a 32-byte AES-256 key.
- There is no built-in key since 2.0.412. Earlier versions fell back to a key compiled into the product (the same one conversation history used); it is now a read-only legacy key, so archives and conversations written by older versions still open, and nothing is written with it again.
- The database key. When
ENCRYPTION_KEYis unset, a 256-bit key is created once and stored in the database (system_settings, keyencryption_key); replicas starting together insert-if-absent and adopt the one stored value, and an existing key is never overwritten. It also protects provider API keys and other database secrets. It persists wherever the database does, but it travels with every database backup, so a stolen backup carries its own key — a secret-managerENCRYPTION_KEYis the stronger, recommended option. Keep database backups either way. WithoutENCRYPTION_KEY, a database that cannot be read at startup stops the server rather than letting it use a throwaway key. - For production deployments: set
ENCRYPTION_KEYandCHAT_ENCRYPTION_KEY(and optionallyBODY_ARCHIVE_KEY_HEX,openssl rand -hex 64each) from your secret manager before first boot. Setting a key for the first time on an existing deployment is safe — older data keeps reading through the legacy or database key. Replacing one operator key with another invalidates data written under the old one. - Provider API keys still stored under the pre-2.0.412 public key (or under the database key, once
ENCRYPTION_KEYis set) are re-encrypted at startup (verified round trip; undecryptable rows are left untouched;AW_REENCRYPT_LEGACY_SECRETS=0disables it). Conversation and archive files are not rewritten; retention removes them in time.
Retention
Section titled “Retention”A background sweep runs once ~5 minutes after server boot and then every 24 hours. For each org, it reads eventPipelineFileArchiverRetentionDays (default 90) and deletes encrypted body files with mtime older than that cutoff. Empty directories are unwound on the way back up.
Same retention horizon as the File Archiver JSONL event stream — one source of truth (CompanySettings) governs both.
Backfill migration on upgrade
Section titled “Backfill migration on upgrade”When upgrading from a release < v2.0.45 to v2.0.63 (or later) with the same data volume, start.sh runs scripts/migrate-usage-bodies.mjs on every container boot. The migration:
- Reads each org’s
event_pipeline_file_archiver_base_pathfromcompany_settings. - Walks
usagerows whosemetadataexceeds 50 KB and containsfullRequest/fullResponse/fullMessages. - Writes each body to
{base}/{org}/bodies/.../<requestId>.json.gz.encusing the row’s original timestamp (so the directory tree and file mtime match request time, not migration time). - Strips the inline body fields from
metadata, replaces them with a smallbodyArchive: { path, base_path, recorded_at, encrypted: true }pointer. - Runs
VACUUMto reclaim the disk space.
The migration is idempotent (a re-run on a clean DB is a no-op) and safely interruptible (each row is independently committed). Sets PRAGMA busy_timeout=60000 so VACUUM waits up to a minute if anything else holds the SQLite lock. Production deployments observed DB shrink from 12 GB → a few hundred MB on first boot.
Operator verification
Section titled “Operator verification”After deploy:
# Average metadata size — should now be ~1–2 KB, not ~2 MBdocker exec <container> sqlite3 /app/data/agentwatch.db \ "SELECT ROUND(AVG(LENGTH(metadata))/1024.0, 2) AS avg_kb FROM usage;"
# Sample pointerdocker exec <container> sqlite3 /app/data/agentwatch.db \ "SELECT json_extract(metadata, '$.bodyArchive') FROM usage WHERE metadata LIKE '%bodyArchive%' LIMIT 1;"
# Body files actually writtendocker exec <container> find /app/data/events -name '*.json.gz.enc' | head -5