Database & Migrations
Database Location
- Docker/PaaS:
~/.projectachilles/agents.db(better-sqlite3, WAL mode) - Vercel: Turso (
@libsql/client, async)
Schema
Tables are created via CREATE TABLE IF NOT EXISTS in database.ts with incremental migrations.
Tables
| Table | Purpose |
|---|---|
agents | Enrolled agents with status, OS, tags |
enrollment_tokens | Registration tokens with TTL and max uses |
tasks | Task queue with status lifecycle |
deleted_tasks | Archive of deleted tasks: the full row as JSON with deleted_at / deleted_by. Task lists never read it |
agent_versions | Registered agent binary versions per platform |
schedules | Recurring execution schedules |
heartbeat_history | Heartbeat samples per agent |
agent_events | Agent lifecycle events (went offline, key rotation, and so on) |
agent_key_digests | HMAC-SHA256 digest of an agent key that already passed bcrypt, so later requests skip bcrypt |
run_notices | Pre-execution notices (tasks.notice_id points here) |
notice_digests | Advance (day-before) notice digests |
campaigns | Saved test campaigns |
campaign_runs | Campaign runs with a JSON snapshot of the campaign (tasks.campaign_run_id points here; no foreign key to campaigns) |
api_keys | API keys (hashed) with their scope |
cli_auth_codes | CLI device authorization codes with short TTL |
cli_refresh_tokens | CLI refresh tokens |
cli_session_revocations | Revoked CLI sessions |
The serverless backend has its own schema and does not have the campaign, notice, API key or archive tables.
Table Recreation Migrations
SQLite has no ALTER COLUMN, so changing a CHECK constraint means recreating the table: create <table>_new with the new definition, copy every row, drop the old table, rename. Five tables have such a migration: agents, agent_versions, tasks, agent_events and api_keys.
All of them go through one function in database.ts, and a new one must too:
function migrateAgentVersionsOs(database: Database.Database): void {
recreateIfOutdated(database, 'agent_versions', ["'darwin'"], () => recreateAgentVersionsTable(database));
}
recreateIfOutdated(database, table, values, recreate) does four things:
- It decides from the table's definition. It reads the
CREATE TABLEtext fromsqlite_masterand looks for the values today's CHECK names. It never tries a test row. - It turns foreign-key checks off with
database.pragma('foreign_keys = OFF'), and back on in afinally. - It runs
recreatein oneBEGIN IMMEDIATEtransaction, and decides again once it holds the write lock. - On an up-to-date table it warns about a leftover
<table>_newand does not delete it.
The recreate function holds only the statements: DROP TABLE IF EXISTS <table>_new, CREATE TABLE <table>_new (...), INSERT INTO <table>_new (<cols>) SELECT <cols> FROM <table>, DROP TABLE <table>, ALTER TABLE <table>_new RENAME TO <table>, then the table's indexes.
Earlier releases did, and decided with a test insert. Each rule above exists because of a failure:
- A test insert fails for reasons that have nothing to do with the CHECK. The
tasksprobe pointed at an agent that does not exist, so the table was recreated on every start of every install. Any insert fails withSQLITE_BUSYwhile another process holds the write lock, which started a recreation of a table that was up to date. - Separate statements interleave. Two processes starting within a fraction of a second of each other lost the
taskstable (one dropped the table the other had just renamed into place), and a failure after theDROPlost every row. - Without the second look, a process that decided before another one finished recreates the table again.
- Without the lock taken first, another process's write between the decision and the first statement ends the recreation with
SQLITE_BUSY_SNAPSHOT.
Key Gotchas
- A recreation keeps only the columns it lists. A column added later by
ALTER TABLEmust be added to the recreate function too: to itsoptionalCols(recreateAgentsTable,recreateAgentVersionsTable,recreateTasksTable) or to itsCREATE TABLE(recreateAgentEventsTable,recreateApiKeysTable), and its index to the index statements. Otherwise the recreation fails on a database that has the column (table tasks_new has no column named ...).checkRecreations.test.tsandtasksRecreation.test.tsrebuild each table with today's columns and an old CHECK, and fail when a column is missing. - Use
database.pragma(), notdatabase.exec('PRAGMA ...'), and only outside a transaction: SQLite ignoresPRAGMA foreign_keysinside one. With the checks on, SQLite refuses to drop a table other tables reference. - A
<table>_newbeside an up-to-date table is never dropped. It can be the only copy of the rows after an interrupted upgrade of an earlier release, so start-up warns about it and leaves it. Beside an out-of-date table it is taken for the unfinished copy of a failed attempt and dropped when the recreation starts. - Do not name a CHECK value in a comment inside the
CREATE TABLEparentheses. SQLite stores such comments as part of the definition, and the text search would find the value there. agentsis backed up first, toagents.db.bakbeside the database, with the database file's permissions. The copy is made inside the lock, by the process that recreates, through a second read-only connection (VACUUM INTOcannot run inside a transaction). It needs free disk space the size of the database; a copy that cannot be made is logged and does not stop the start. Pass{ backUpFirst: true }for a table whose loss would cut off the fleet.
Adding a Column
Check PRAGMA table_info(<table>) and add the column with addColumn(database, 'ALTER TABLE ... ADD COLUMN ...'), never with a bare database.exec. addColumn accepts duplicate column name: when two processes start together on a database that lacks the column, both find it missing and the slower one's ALTER fails with that error. A test adds every column from a second connection first and fails on a bare ALTER.
A Failed Start-Up
getDatabase() keeps its connection only after initializeTables() has returned. If a migration throws, the connection is closed. Calls in the next 5 seconds get the same error without another attempt, and the first call after that opens the database and runs the set-up again. A process never serves requests on a database whose migrations stopped part-way.
In single-process mode and in the cluster primary, the first call happens at start (the scheduler's catch-up) and is not caught, so a failed set-up ends the process and its supervisor restarts it. Cluster workers open the database on their first request and are the ones that retry.
Storage Architecture
Beyond the SQLite database, the backend persists configuration, certificates, and binary artifacts to a file-system-based storage layer. On serverless (Vercel), an equivalent Blob storage layer is used instead.
File System Layout (Docker/PaaS)
~/.projectachilles/
├── agents.db # SQLite database (WAL mode)
├── agents.db.bak # Copy made before the agents table is recreated (old installs only)
├── analytics.json # ES connection config (encrypted fields)
├── tests.json # Platform/arch build settings
├── integrations.json # EDR integrations, alerting, pre-execution notices (encrypted fields)
├── console-identity.json # Console identity (code, name, colour)
├── certs/
│ ├── active-cert.txt # ID of active signing certificate
│ ├── cert-1709234567/
│ │ ├── cert.pfx # PKCS#12 certificate bundle
│ │ └── cert-meta.json # Label, subject, expiry, source, encrypted password
│ └── cert-1709345678/
│ ├── cert.pfx
│ └── cert-meta.json
├── binaries/<os>-<arch>/ # Registered agent binaries
├── builds/<test-uuid>/ # Test build output + build-meta.json
└── signing/ # Ed25519 key that signs agent update binaries
Vercel Blob Layout (Serverless)
On the serverless backend, the same logical structure is mirrored in Vercel Blob with hierarchical key prefixes:
settings/analytics.json
settings/tests.json
settings/agent-settings.json
certs/cert-<timestamp>/cert.pfx
certs/cert-<timestamp>/cert-meta.json
certs/active-cert.txt
binaries/<os>-<arch>/<filename>
builds/<uuid>/build-meta.json
Encrypted Settings Management
Sensitive configuration fields are encrypted at rest using AES-256-GCM. This applies to both the Docker backend (file system) and the serverless backend (Vercel Blob).
How It Works
- The
ENCRYPTION_SECRETenvironment variable provides the secret the key is derived from (HKDF-SHA256). It is required and must be at least 32 characters; encryption throws without it. - Sensitive fields (Elasticsearch
cloudId,apiKey,password; EDR client secrets and API tokens; alerting webhook URLs and SMTP password; certificate passwords) are encrypted before being written to JSON files. - Encrypted values are stored with an
enc:prefix so the settings service can distinguish encrypted from plaintext values. - On read, the service detects the
enc:prefix and decrypts transparently.
Configuration Priority (Analytics Settings)
The SettingsService resolves Elasticsearch connection config in this order:
- User-configured settings (via UI) -- stored in
analytics.jsonwith encrypted fields - Environment variables --
ELASTICSEARCH_CLOUD_ID,ELASTICSEARCH_NODE,ELASTICSEARCH_API_KEY, etc. - Default settings -- last resort
Settings encrypted with one ENCRYPTION_SECRET cannot be read with another. Generate it once (openssl rand -base64 32), keep it in your secret store and reuse it when the application moves to a different machine or container. Values written by old versions (keyed by a plain hash of the secret, or by a machine-derived key) are still decrypted for backward compatibility, but nothing is written with those keys any more.
Settings Files Reference
| File | Encrypted Fields | Purpose |
|---|---|---|
analytics.json | cloudId, apiKey, password | Elasticsearch connection |
integrations.json | Client secrets, API tokens, webhook URLs, SMTP password | EDR integration and alerting credentials, pre-execution notice settings |
tests.json | None | Build platform config |
certs/<id>/cert-meta.json | password | Signing certificate metadata |
console-identity.json | None | Console identity |
Serverless Storage Differences
| Aspect | Docker/PaaS (backend/) | Serverless (backend-serverless/) |
|---|---|---|
| Storage layer | fs (file system) | @vercel/blob (Vercel Blob API) |
| Read operation | fs.readFileSync() | blobReadText(key) -- returns null on error |
| Write operation | fs.writeFileSync() | blobWrite(key, data) -- returns public URL |
| Existence check | fs.existsSync() | blobExists(key) -- HEAD request |
| List operation | fs.readdirSync() | blobList(prefix) -- key/URL/size array |
| Delete operation | fs.unlinkSync() | blobDelete(key) -- silently ignores missing keys |
| Caching | None (reads from disk) | None (reads from Blob API) |
| Access model | Process-local file access | Public URLs with BLOB_READ_WRITE_TOKEN |
The serverless storage module follows a "fail-safe" philosophy: read operations return null on any error (network, missing file, permission), and delete operations silently succeed even for non-existent keys. This prevents cascading failures in calling services.