$PILOOP_HOME/piloop.db. Run one statement with piloop sql.
piloop schema prints the same tables with the guides that agents follow, and piloop schema <guide> prints one
guide.
The indexes are part of the contract: they show which filters are cheap. The data model
shows the same tables as a diagram. Each section is core’s tables, or a plugin’s that adds them
(tables and migrations).
core
CREATE TABLE campaigns (
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
path VARCHAR NOT NULL,
config JSON NOT NULL,
evaluator_id INTEGER,
status VARCHAR(16) DEFAULT 'active' NOT NULL,
stopped_reason VARCHAR,
paused VARCHAR(16),
paused_reason VARCHAR,
paused_by VARCHAR,
repo VARCHAR,
clone_url VARCHAR,
clone_secret VARCHAR,
type VARCHAR DEFAULT 'optimization' NOT NULL,
parent_id INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by VARCHAR,
PRIMARY KEY (id),
CONSTRAINT "campaigns.paused and campaigns.paused_reason go together" CHECK ((paused IS NULL) = (paused_reason IS NULL)),
CONSTRAINT "campaigns.a pause has its owner" CHECK ((paused IS NULL) = (paused_by IS NULL)),
CONSTRAINT "campaigns.a stopped or retired campaign has its stopped_reason" CHECK ((status = 'active') = (stopped_reason IS NULL)),
CONSTRAINT "campaigns.a retired campaign is not paused" CHECK (status != 'retired' OR paused IS NULL),
CONSTRAINT "campaigns.paused_reason must be a text value" CHECK (typeof(paused_reason) IN ('text', 'null')),
CONSTRAINT "campaigns.clone_url must be a text value" CHECK (typeof(clone_url) IN ('text', 'null')),
CONSTRAINT "campaigns.clone_secret must be a text value" CHECK (typeof(clone_secret) IN ('text', 'null')),
CONSTRAINT campaignstatus CHECK (status IN ('active', 'stopped', 'retired')),
CONSTRAINT pause CHECK (paused IN ('launches', 'all')),
FOREIGN KEY(parent_id) REFERENCES campaigns (id)
);
CREATE UNIQUE INDEX ix_campaigns_name ON campaigns (name) WHERE status != 'retired';
CREATE TABLE comment_kinds (
id VARCHAR(16) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE job_files (
id INTEGER NOT NULL,
place VARCHAR NOT NULL,
files VARCHAR NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE limits (
"key" VARCHAR NOT NULL,
value JSON NOT NULL,
reason VARCHAR NOT NULL,
actor VARCHAR NOT NULL,
at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY ("key"),
CONSTRAINT "limits.key must be a text value" CHECK (typeof(key) IN ('text')),
CONSTRAINT "limits.reason must be a text value" CHECK (typeof(reason) IN ('text')),
CONSTRAINT "limits.actor must be a text value" CHECK (typeof(actor) IN ('text'))
);
CREATE TABLE migrations (
name VARCHAR NOT NULL,
package VARCHAR NOT NULL,
fingerprint INTEGER NOT NULL,
PRIMARY KEY (name)
);
CREATE TABLE repo_transfers (
id INTEGER NOT NULL,
campaign_id INTEGER NOT NULL,
base VARCHAR,
head VARCHAR NOT NULL,
blob VARCHAR NOT NULL,
received_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);
CREATE INDEX ix_repo_transfers_campaign ON repo_transfers (campaign_id, id);
CREATE TABLE target_kinds (
id VARCHAR(16) NOT NULL,
owner VARCHAR NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE terms (
epoch INTEGER NOT NULL,
pid INTEGER NOT NULL,
listen VARCHAR,
started_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
stopping_at DATETIME,
PRIMARY KEY (epoch),
CONSTRAINT "terms.listen must be a text value" CHECK (typeof(listen) IN ('text', 'null'))
);
CREATE TABLE tokens (
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
access VARCHAR(16) NOT NULL,
sha256 VARCHAR NOT NULL,
agent_session_id INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE (name),
CONSTRAINT access CHECK (access IN ('read', 'act', 'admin', 'worker')),
UNIQUE (sha256),
UNIQUE (agent_session_id)
);
CREATE TABLE workspace_snapshots (
id INTEGER NOT NULL,
campaign_id INTEGER NOT NULL,
manifest VARCHAR NOT NULL,
at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);
CREATE INDEX ix_workspace_snapshots_campaign ON workspace_snapshots (campaign_id, id);
CREATE TABLE agent_sessions (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
kind VARCHAR NOT NULL,
campaign_id INTEGER,
subject_kind VARCHAR(16),
subject_id INTEGER,
harness VARCHAR,
model VARCHAR,
runtime VARCHAR,
handle JSON DEFAULT '{}' NOT NULL,
state VARCHAR(16) DEFAULT 'requested' NOT NULL,
reason VARCHAR,
log_dir VARCHAR,
started_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
deadline DATETIME,
ended_at DATETIME,
error VARCHAR,
session_id VARCHAR,
machine VARCHAR,
session_file VARCHAR,
usage JSON,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by VARCHAR,
CONSTRAINT "agent_sessions.an ended agent has its end" CHECK ((state IN ('exited', 'killed', 'never_worked')) = (ended_at IS NOT NULL)),
CONSTRAINT "agent_sessions.a never-worked agent says why" CHECK (state != 'never_worked' OR error IS NOT NULL),
CONSTRAINT agent_sessions_subject CHECK ((subject_kind IS NULL) = (subject_id IS NULL)),
CONSTRAINT "agent_sessions.kind must be a text value" CHECK (typeof(kind) IN ('text')),
CONSTRAINT "agent_sessions.reason must be a text value" CHECK (typeof(reason) IN ('text', 'null')),
CONSTRAINT "agent_sessions.harness must be a text value" CHECK (typeof(harness) IN ('text', 'null')),
CONSTRAINT "agent_sessions.model must be a text value" CHECK (typeof(model) IN ('text', 'null')),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE,
CONSTRAINT agent_sessions_kind FOREIGN KEY(subject_kind) REFERENCES target_kinds (id),
CONSTRAINT agentsessionstate CHECK (state IN ('requested', 'starting', 'running', 'ending', 'exited', 'killed', 'never_worked'))
);
CREATE INDEX ix_agent_sessions_campaign_kind_state ON agent_sessions (campaign_id, kind, state);
CREATE INDEX ix_agent_sessions_subject ON agent_sessions (subject_kind, subject_id);
CREATE TABLE comments (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
target_kind VARCHAR(16) NOT NULL,
target_id INTEGER NOT NULL,
in_reply_to INTEGER,
author VARCHAR NOT NULL,
kind VARCHAR(16) DEFAULT 'note' NOT NULL,
body VARCHAR NOT NULL,
priority INTEGER,
form JSON,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by VARCHAR,
CONSTRAINT escalation_needs_priority CHECK (kind != 'escalation' OR priority IS NOT NULL),
CONSTRAINT reply_is_on_a_comment CHECK (in_reply_to IS NULL OR target_kind = 'comment'),
CONSTRAINT comment_not_on_home CHECK (target_kind != 'home'),
CONSTRAINT "comments.author must be a text value" CHECK (typeof(author) IN ('text')),
CONSTRAINT "comments.body must be a text value" CHECK (typeof(body) IN ('text')),
CONSTRAINT targetkind FOREIGN KEY(target_kind) REFERENCES target_kinds (id),
FOREIGN KEY(in_reply_to) REFERENCES comments (id) ON DELETE SET NULL,
CONSTRAINT commentkind FOREIGN KEY(kind) REFERENCES comment_kinds (id)
);
CREATE INDEX ix_comments_inbox ON comments (priority, id) WHERE priority IS NOT NULL;
CREATE INDEX ix_comments_target ON comments (target_kind, target_id);
CREATE TABLE events (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
kind VARCHAR NOT NULL GENERATED ALWAYS AS (json_extract(payload, '$.kind')),
actor VARCHAR NOT NULL,
target_kind VARCHAR(16) NOT NULL,
target_id INTEGER NOT NULL,
payload JSON NOT NULL,
CONSTRAINT targetkind FOREIGN KEY(target_kind) REFERENCES target_kinds (id)
);
CREATE INDEX ix_events_kind ON events (kind, target_kind, target_id, id);
CREATE INDEX ix_events_target ON events (target_kind, target_id, id);
CREATE TABLE acks (
comment_id INTEGER NOT NULL,
party VARCHAR NOT NULL,
acked_by VARCHAR NOT NULL,
acked_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (comment_id, party),
CONSTRAINT "acks.party must be a text value" CHECK (typeof(party) IN ('text')),
CONSTRAINT "acks.acked_by must be a text value" CHECK (typeof(acked_by) IN ('text')),
FOREIGN KEY(comment_id) REFERENCES comments (id) ON DELETE CASCADE
);
CREATE TABLE leases (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
kind VARCHAR NOT NULL,
agent_session_id INTEGER,
job_id INTEGER,
runner VARCHAR,
state VARCHAR(16) DEFAULT 'queued' NOT NULL,
worker VARCHAR,
job_cid VARCHAR NOT NULL,
handle JSON DEFAULT '{}' NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
acquired_at DATETIME,
started_at DATETIME,
renewed_at DATETIME,
expires_at DATETIME,
revoked_at DATETIME,
ended_at DATETIME,
acquire_request VARCHAR,
report_request VARCHAR,
receipt JSON DEFAULT '{}' NOT NULL,
CONSTRAINT "leases.an ended lease has its end" CHECK ((state IN ('reported', 'expired', 'cancelled')) = (ended_at IS NOT NULL)),
CONSTRAINT "leases.a taken lease has its worker" CHECK ((worker IS NULL) = (acquired_at IS NULL)),
CONSTRAINT "leases.only a queued lease has no worker" CHECK (state = 'cancelled' OR (state = 'queued') = (worker IS NULL)),
CONSTRAINT "leases.a running lease has its start" CHECK (state != 'running' OR started_at IS NOT NULL),
CONSTRAINT "leases.a lease has the job of its kind" CHECK ((kind = 'agent') = (agent_session_id IS NOT NULL) AND (kind = 'agent') = (job_id IS NULL)),
CONSTRAINT "leases.worker must be a text value" CHECK (typeof(worker) IN ('text', 'null')),
CONSTRAINT "leases.report_request must be a text value" CHECK (typeof(report_request) IN ('text', 'null')),
FOREIGN KEY(agent_session_id) REFERENCES agent_sessions (id) ON DELETE CASCADE,
CONSTRAINT leasestate CHECK (state IN ('queued', 'acquired', 'running', 'reported', 'expired', 'cancelled'))
);
CREATE UNIQUE INDEX ix_leases_acquire_request ON leases (acquire_request);
CREATE INDEX ix_leases_agent_session ON leases (agent_session_id);
CREATE INDEX ix_leases_job ON leases (kind, job_id);
CREATE INDEX ix_leases_state ON leases (state);
CREATE VIRTUAL TABLE comments_fts USING fts5(body, content='comments', content_rowid='id', tokenize='porter unicode61 remove_diacritics 2');
archive/local
CREATE TABLE archives (
id INTEGER NOT NULL,
campaign_id INTEGER NOT NULL,
subject_kind VARCHAR(16),
subject_id INTEGER,
manifest VARCHAR,
files INTEGER DEFAULT 0 NOT NULL,
bytes INTEGER DEFAULT 0 NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
pruned_at DATETIME,
complete BOOLEAN DEFAULT 1 NOT NULL,
PRIMARY KEY (id),
CONSTRAINT archives_subject CHECK ((subject_kind IS NULL) = (subject_id IS NULL)),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE,
CONSTRAINT targetkind FOREIGN KEY(subject_kind) REFERENCES target_kinds (id)
);
CREATE INDEX ix_archives_campaign_subject ON archives (campaign_id, subject_kind, subject_id);
executor/workers
CREATE TABLE lease_workers (
id VARCHAR NOT NULL,
pool VARCHAR NOT NULL,
version VARCHAR NOT NULL,
runners JSON DEFAULT '[]' NOT NULL,
sample JSON DEFAULT '{}' NOT NULL,
seen_at DATETIME NOT NULL,
no_lease VARCHAR(16),
cursor INTEGER DEFAULT 0 NOT NULL,
drained_at DATETIME,
PRIMARY KEY (id),
CONSTRAINT "lease_workers.id must be a text value" CHECK (typeof(id) IN ('text')),
CONSTRAINT "lease_workers.pool must be a text value" CHECK (typeof(pool) IN ('text')),
CONSTRAINT "lease_workers.version must be a text value" CHECK (typeof(version) IN ('text')),
CONSTRAINT nolease CHECK (no_lease IN ('draining', 'memory', 'load', 'slots', 'nothing', 'unreachable'))
);
CREATE TABLE owners (
id VARCHAR NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE worker_instances (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
pool VARCHAR NOT NULL,
state VARCHAR(16) DEFAULT 'starting' NOT NULL,
handle JSON DEFAULT '{}' NOT NULL,
requested_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
registered_at DATETIME,
ended_at DATETIME,
"end" VARCHAR(16),
CONSTRAINT "worker_instances.an ended one says why" CHECK ((state = 'ended') = ("end" IS NOT NULL)),
CONSTRAINT "worker_instances.name must be a text value" CHECK (typeof(name) IN ('text')),
CONSTRAINT "worker_instances.pool must be a text value" CHECK (typeof(pool) IN ('text')),
UNIQUE (name),
CONSTRAINT workerinstancestate CHECK (state IN ('starting', 'running', 'draining', 'ended')),
CONSTRAINT instanceend CHECK ("end" IN ('drained', 'register_timeout', 'start_refused', 'lost', 'gone'))
);
CREATE INDEX ix_worker_instances_state ON worker_instances (state);
CREATE TABLE first_transfers (
campaign_id INTEGER NOT NULL,
state VARCHAR(16) NOT NULL,
started_at DATETIME NOT NULL,
revision VARCHAR,
blob VARCHAR,
error VARCHAR,
PRIMARY KEY (campaign_id),
CONSTRAINT "first_transfers.a built one names it" CHECK ((state = 'built') = (revision IS NOT NULL AND blob IS NOT NULL)),
CONSTRAINT "first_transfers.a failed one says why" CHECK ((state = 'failed') = (error IS NOT NULL)),
CONSTRAINT "first_transfers.revision must be a text value" CHECK (typeof(revision) IN ('text', 'null')),
CONSTRAINT "first_transfers.blob must be a text value" CHECK (typeof(blob) IN ('text', 'null')),
CONSTRAINT "first_transfers.error must be a text value" CHECK (typeof(error) IN ('text', 'null')),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE,
CONSTRAINT firsttransferstate CHECK (state IN ('requested', 'built', 'failed'))
);
optimization
CREATE TABLE evaluation_task_failures (
campaign_id INTEGER NOT NULL,
task_key VARCHAR NOT NULL,
failures INTEGER NOT NULL,
held BOOLEAN NOT NULL,
PRIMARY KEY (campaign_id, task_key),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE
);
CREATE TABLE evaluators (
id INTEGER NOT NULL,
campaign_id INTEGER NOT NULL,
"key" VARCHAR NOT NULL,
definition JSON NOT NULL,
runner VARCHAR DEFAULT 'process' NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE (campaign_id, "key"),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE
);
CREATE TABLE ideas (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
campaign_id INTEGER NOT NULL,
title VARCHAR NOT NULL,
hypothesis VARCHAR NOT NULL,
track VARCHAR NOT NULL,
"plan" VARCHAR,
status VARCHAR(16) DEFAULT 'open' NOT NULL,
priority INTEGER DEFAULT 2 NOT NULL,
created_by VARCHAR NOT NULL,
provenance VARCHAR,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by VARCHAR,
UNIQUE (campaign_id, title),
CONSTRAINT "ideas.title must be a text value" CHECK (typeof(title) IN ('text')),
CONSTRAINT "ideas.hypothesis must be a text value" CHECK (typeof(hypothesis) IN ('text')),
CONSTRAINT "ideas.created_by must be a text value" CHECK (typeof(created_by) IN ('text')),
CONSTRAINT "ideas.track must be a text value" CHECK (typeof(track) IN ('text')),
CONSTRAINT "ideas.plan must be a text value" CHECK (typeof(plan) IN ('text', 'null')),
CONSTRAINT "ideas.provenance must be a text value" CHECK (typeof(provenance) IN ('text', 'null')),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE,
CONSTRAINT ideastatus CHECK (status IN ('open', 'claimed', 'done', 'dropped'))
);
CREATE INDEX ix_ideas_campaign_status_priority ON ideas (campaign_id, status, priority);
CREATE TABLE runs (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
campaign_id INTEGER NOT NULL,
role VARCHAR DEFAULT 'implementer' NOT NULL,
base_candidate_id INTEGER NOT NULL,
harness VARCHAR NOT NULL,
model VARCHAR NOT NULL,
status VARCHAR(16) DEFAULT 'queued' NOT NULL,
scoring BOOLEAN DEFAULT 0 NOT NULL,
brief_cid VARCHAR,
started_at DATETIME,
finished_at DATETIME,
"end" VARCHAR(16),
error VARCHAR,
log_dir VARCHAR,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by VARCHAR,
CONSTRAINT "runs.scoring needs a finished status" CHECK (scoring = 0 OR status IN ('succeeded', 'failed', 'timed_out')),
CONSTRAINT "runs.end is set once a run is finished" CHECK (("end" IS NULL) = (status IN ('queued', 'running'))),
CONSTRAINT "runs.status follows from runs.end" CHECK ("end" IS NULL OR status = CASE "end" WHEN 'finished' THEN 'succeeded' WHEN 'timed_out' THEN 'timed_out' ELSE 'failed' END),
CONSTRAINT fk_runs_campaign_id_campaigns FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE,
CONSTRAINT runstatus CHECK (status IN ('queued', 'running', 'succeeded', 'failed', 'timed_out')),
CONSTRAINT runend CHECK ("end" IN ('finished', 'gave_up', 'failed', 'timed_out', 'cancelled', 'campaign_stopped', 'daemon_stopped', 'stale', 'role_removed', 'not_started', 'worker_lost', 'stalled'))
);
CREATE INDEX ix_runs_campaign_id ON runs (campaign_id);
CREATE INDEX ix_runs_status ON runs (status);
CREATE TABLE techniques (
id INTEGER NOT NULL,
campaign_id INTEGER NOT NULL,
title VARCHAR NOT NULL,
mechanism VARCHAR NOT NULL,
url VARCHAR NOT NULL,
created_by VARCHAR NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE (campaign_id, url),
CONSTRAINT "techniques.title must be a text value" CHECK (typeof(title) IN ('text')),
CONSTRAINT "techniques.mechanism must be a text value" CHECK (typeof(mechanism) IN ('text')),
CONSTRAINT "techniques.url must be a text value" CHECK (typeof(url) IN ('text')),
CONSTRAINT "techniques.created_by must be a text value" CHECK (typeof(created_by) IN ('text')),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE
);
CREATE TABLE candidates (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
campaign_id INTEGER NOT NULL,
run_id INTEGER,
idea_id INTEGER,
parent_id INTEGER,
tree_sha VARCHAR NOT NULL,
revision VARCHAR NOT NULL,
task_id VARCHAR,
summary VARCHAR,
origin VARCHAR(16) DEFAULT 'run' NOT NULL,
excluded_reason VARCHAR,
excluded_by VARCHAR,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by VARCHAR,
UNIQUE (campaign_id, tree_sha),
CONSTRAINT "candidates.summary must be a text value" CHECK (typeof(summary) IN ('text', 'null')),
CONSTRAINT "candidates.excluded_reason must be a text value" CHECK (typeof(excluded_reason) IN ('text', 'null')),
CONSTRAINT "candidates.excluded_by must be a text value" CHECK (typeof(excluded_by) IN ('text', 'null')),
CONSTRAINT "candidates.an exclusion has its author" CHECK ((excluded_reason IS NULL) = (excluded_by IS NULL)),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id) ON DELETE CASCADE,
FOREIGN KEY(run_id) REFERENCES runs (id) ON DELETE SET NULL,
FOREIGN KEY(idea_id) REFERENCES ideas (id) ON DELETE SET NULL,
FOREIGN KEY(parent_id) REFERENCES candidates (id),
CONSTRAINT origin CHECK (origin IN ('baseline', 'run', 'reference', 'recovered', 'skipped', 'merge'))
);
CREATE INDEX ix_candidates_parent ON candidates (parent_id);
CREATE INDEX ix_candidates_run ON candidates (run_id);
CREATE TABLE idea_links (
idea_id INTEGER NOT NULL,
target_id INTEGER NOT NULL,
kind VARCHAR(16) NOT NULL,
PRIMARY KEY (idea_id, target_id, kind),
CONSTRAINT idea_links_not_self CHECK (idea_id != target_id),
FOREIGN KEY(idea_id) REFERENCES ideas (id) ON DELETE CASCADE,
FOREIGN KEY(target_id) REFERENCES ideas (id) ON DELETE CASCADE,
CONSTRAINT linkkind CHECK (kind IN ('after'))
);
CREATE INDEX ix_idea_links_target ON idea_links (target_id, kind);
CREATE TABLE run_claims (
run_id INTEGER NOT NULL,
idea_id INTEGER NOT NULL,
attempt INTEGER NOT NULL,
claimed_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (run_id, idea_id),
UNIQUE (idea_id, attempt),
FOREIGN KEY(run_id) REFERENCES runs (id) ON DELETE CASCADE,
FOREIGN KEY(idea_id) REFERENCES ideas (id) ON DELETE CASCADE
);
CREATE TABLE cancels (
candidate_id INTEGER NOT NULL,
evaluator_id INTEGER NOT NULL,
cancelled_by VARCHAR NOT NULL,
cancelled_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (candidate_id, evaluator_id),
CONSTRAINT "cancels.cancelled_by must be a text value" CHECK (typeof(cancelled_by) IN ('text')),
FOREIGN KEY(candidate_id) REFERENCES candidates (id) ON DELETE CASCADE,
FOREIGN KEY(evaluator_id) REFERENCES evaluators (id)
);
CREATE TABLE evaluations (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
candidate_id INTEGER NOT NULL,
evaluator_id INTEGER NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
started_at DATETIME,
cancelled_by VARCHAR,
ask INTEGER,
UNIQUE (candidate_id, evaluator_id),
CONSTRAINT "evaluations.only a running evaluation is cancelled" CHECK (cancelled_by IS NULL OR started_at IS NOT NULL),
CONSTRAINT "evaluations.cancelled_by must be a text value" CHECK (typeof(cancelled_by) IN ('text', 'null')),
FOREIGN KEY(candidate_id) REFERENCES candidates (id) ON DELETE CASCADE,
FOREIGN KEY(evaluator_id) REFERENCES evaluators (id)
);
CREATE TABLE judgements (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
candidate_id INTEGER NOT NULL,
incumbent_id INTEGER NOT NULL,
evaluator_id INTEGER NOT NULL,
"key" VARCHAR NOT NULL,
answer VARCHAR(16) NOT NULL,
reason VARCHAR,
seconds DOUBLE NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
UNIQUE (candidate_id, "key"),
CONSTRAINT "judgements.key must be a text value" CHECK (typeof(key) IN ('text')),
CONSTRAINT "judgements.reason must be a text value" CHECK (typeof(reason) IN ('text', 'null')),
FOREIGN KEY(candidate_id) REFERENCES candidates (id) ON DELETE CASCADE,
FOREIGN KEY(incumbent_id) REFERENCES candidates (id) ON DELETE CASCADE,
FOREIGN KEY(evaluator_id) REFERENCES evaluators (id),
CONSTRAINT answer CHECK (answer IN ('keep', 'refuse', 'unanswered'))
);
CREATE TABLE measurements (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
candidate_id INTEGER NOT NULL,
evaluator_id INTEGER NOT NULL,
metrics JSON DEFAULT '{}' NOT NULL,
error VARCHAR,
seconds DOUBLE,
rung VARCHAR,
stopped VARCHAR,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
units VARCHAR,
CONSTRAINT "measurements.a stop is at a rung, with no error" CHECK (stopped IS NULL OR (rung IS NOT NULL AND error IS NULL)),
CONSTRAINT "measurements.rung must be a text value" CHECK (typeof(rung) IN ('text', 'null')),
CONSTRAINT "measurements.stopped must be a text value" CHECK (typeof(stopped) IN ('text', 'null')),
FOREIGN KEY(candidate_id) REFERENCES candidates (id) ON DELETE CASCADE,
FOREIGN KEY(evaluator_id) REFERENCES evaluators (id)
);
CREATE INDEX ix_measurements_candidate_evaluator ON measurements (candidate_id, evaluator_id);
CREATE TABLE submissions (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
run_id INTEGER NOT NULL,
candidate_id INTEGER NOT NULL,
idea_id INTEGER,
event_id INTEGER NOT NULL,
submitted_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY(run_id) REFERENCES runs (id) ON DELETE CASCADE,
FOREIGN KEY(candidate_id) REFERENCES candidates (id) ON DELETE CASCADE,
FOREIGN KEY(idea_id) REFERENCES ideas (id) ON DELETE SET NULL
);
CREATE INDEX ix_submissions_candidate ON submissions (candidate_id);
CREATE INDEX ix_submissions_idea ON submissions (idea_id, run_id);
CREATE INDEX ix_submissions_run ON submissions (run_id);
CREATE VIRTUAL TABLE ideas_fts USING fts5(title, hypothesis, plan, content='ideas', content_rowid='id', tokenize='porter unicode61 remove_diacritics 2');
secrets/local
CREATE TABLE secrets (
id INTEGER NOT NULL,
campaign_id INTEGER,
name VARCHAR NOT NULL,
value VARCHAR NOT NULL,
PRIMARY KEY (id),
UNIQUE (campaign_id, name),
FOREIGN KEY(campaign_id) REFERENCES campaigns (id)
);
CREATE UNIQUE INDEX ux_secrets_home_name ON secrets (name) WHERE campaign_id IS NULL;