-- Generated by rastrillo migration generate; DO NOT EDIT. -- The schema every migration in this directory adds up to. CREATE TABLE accounts (id integer PRIMARY KEY AUTOINCREMENT, label text, revision integer); CREATE TABLE actions ( id INTEGER PRIMARY KEY, job_id INTEGER NOT NULL REFERENCES jobs(id), step_id INTEGER NOT NULL, attempt_id INTEGER NOT NULL, fence INTEGER NOT NULL, key TEXT NOT NULL, operation TEXT NOT NULL, class TEXT NOT NULL, payload_hash TEXT NOT NULL, payload BLOB NOT NULL, state TEXT NOT NULL, reservation_id INTEGER NOT NULL, grant_ref TEXT NOT NULL, grant_version INTEGER NOT NULL, grant_epoch INTEGER NOT NULL, admitted_at TEXT NOT NULL, admission_ends TEXT NOT NULL, receipt TEXT NOT NULL, last_error TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, admitted_incarnation INTEGER NOT NULL DEFAULT 0, UNIQUE(job_id, key) ); CREATE TABLE artifacts ( id INTEGER PRIMARY KEY, job_id INTEGER NOT NULL REFERENCES jobs(id), step_id INTEGER NOT NULL, attempt_id INTEGER NOT NULL, fence INTEGER NOT NULL, role TEXT NOT NULL, hash TEXT NOT NULL, media_type TEXT NOT NULL, size INTEGER NOT NULL, provenance TEXT NOT NULL, payload BLOB NOT NULL, created_at TEXT NOT NULL ); CREATE TABLE attempts ( id INTEGER PRIMARY KEY, job_id INTEGER NOT NULL REFERENCES jobs(id), step_id INTEGER NOT NULL REFERENCES steps(id), number INTEGER NOT NULL, runner TEXT NOT NULL, workspace TEXT NOT NULL, fence INTEGER NOT NULL, lease_deadline TEXT NOT NULL, state TEXT NOT NULL, containment TEXT NOT NULL, stop_requested TEXT NOT NULL, stop_acked TEXT NOT NULL, provider TEXT NOT NULL, model TEXT NOT NULL, effort TEXT NOT NULL, outcome TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, UNIQUE(job_id, workspace) ); CREATE TABLE audit_events (id integer PRIMARY KEY AUTOINCREMENT, actor text, kind text, record_id text, at datetime); CREATE TABLE budget_records (team_id text, revision integer, config text, PRIMARY KEY (team_id)); CREATE TABLE decision_owners (project_id integer, decision text, address text, PRIMARY KEY (project_id, decision)); CREATE TABLE events ( job_id INTEGER NOT NULL REFERENCES jobs(id), seq INTEGER NOT NULL, type TEXT NOT NULL, actor TEXT NOT NULL, payload BLOB NOT NULL, source_at TEXT NOT NULL, PRIMARY KEY(job_id, seq) ); CREATE TABLE jobs ( request_key TEXT NOT NULL DEFAULT "", request_hash TEXT NOT NULL DEFAULT "", grant_stale INTEGER NOT NULL DEFAULT 0, id INTEGER PRIMARY KEY, account TEXT NOT NULL, parent INTEGER NOT NULL, depth INTEGER NOT NULL, requester TEXT NOT NULL, target TEXT NOT NULL, evidence TEXT NOT NULL, policy_version INTEGER NOT NULL, policy TEXT NOT NULL, grant_ref TEXT NOT NULL, grant_version INTEGER NOT NULL, grant_epoch INTEGER NOT NULL, grant_observed_at TEXT NOT NULL, state TEXT NOT NULL, wait TEXT NOT NULL, revision INTEGER NOT NULL, outcome TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL , project_id INTEGER NOT NULL DEFAULT 0); CREATE TABLE members (address text, role text, revision integer, active numeric, session_epoch integer, PRIMARY KEY (address)); CREATE TABLE period_budgets ( content_hash TEXT NOT NULL DEFAULT "", account TEXT PRIMARY KEY, revision INTEGER NOT NULL, config TEXT NOT NULL ); CREATE TABLE project_requests (actor text, operation text, request_id text, input_hash text, result text, PRIMARY KEY (actor, operation, request_id)); CREATE TABLE projects ( grant_version INTEGER NOT NULL DEFAULT 1,id integer PRIMARY KEY AUTOINCREMENT, account_id integer, name text, repository text NOT NULL, app_name text NOT NULL, default_branch text, kind text, revision integer); CREATE TABLE reservations ( project_id INTEGER NOT NULL DEFAULT 0, period TEXT NOT NULL DEFAULT "", cost_unavailable INTEGER NOT NULL DEFAULT 0, s_cached INTEGER NOT NULL DEFAULT 0, id INTEGER PRIMARY KEY, account TEXT NOT NULL, job_id INTEGER NOT NULL REFERENCES jobs(id), step_id INTEGER NOT NULL, attempt_id INTEGER NOT NULL, action_id INTEGER NOT NULL, r_calls INTEGER NOT NULL, r_input INTEGER NOT NULL, r_output INTEGER NOT NULL, r_cost INTEGER NOT NULL, s_calls INTEGER NOT NULL, s_input INTEGER NOT NULL, s_output INTEGER NOT NULL, s_cost INTEGER NOT NULL, state TEXT NOT NULL, receipt_id TEXT NOT NULL, settled_by TEXT NOT NULL, created_at TEXT NOT NULL, settled_at TEXT NOT NULL ); CREATE TABLE service ( release_required INTEGER NOT NULL DEFAULT 0, recovery_revision INTEGER NOT NULL DEFAULT 0, reconciled_revision INTEGER NOT NULL DEFAULT -1, clock_high_water TEXT NOT NULL DEFAULT "", id INTEGER PRIMARY KEY CHECK (id = 1), schema_version INTEGER NOT NULL, incarnation INTEGER NOT NULL, revision INTEGER NOT NULL DEFAULT 0, restore_uncertain INTEGER NOT NULL DEFAULT 0, admission_paused INTEGER NOT NULL, quarantine INTEGER NOT NULL, quarantine_why TEXT NOT NULL ); CREATE TABLE specialists (key text, model_selection text, provider text, model_family text, model text, standing_instructions text, max_concurrent integer, revision integer, team_id text NOT NULL DEFAULT "",creator text NOT NULL DEFAULT "",name text NOT NULL DEFAULT "",icon text NOT NULL DEFAULT "",role text NOT NULL DEFAULT "",required numeric NOT NULL DEFAULT false,archived numeric NOT NULL DEFAULT false,allowed_models text NOT NULL DEFAULT "[]",tools text NOT NULL DEFAULT "[]",project_ids text NOT NULL DEFAULT "[]",template_id text NOT NULL DEFAULT "",template_version integer NOT NULL DEFAULT 0,template_base text NOT NULL DEFAULT "{}", PRIMARY KEY (key)); CREATE TABLE steps ( id INTEGER PRIMARY KEY, job_id INTEGER NOT NULL REFERENCES jobs(id), key TEXT NOT NULL, capability TEXT NOT NULL, version INTEGER NOT NULL, depends_on TEXT NOT NULL, state TEXT NOT NULL, fence INTEGER NOT NULL, holder INTEGER NOT NULL, attempts INTEGER NOT NULL, UNIQUE(job_id, key) ); CREATE TABLE teams (id text, name text, roster_epoch integer, PRIMARY KEY (id)); CREATE TABLE time_reservations ( elapsed_unknown INTEGER NOT NULL DEFAULT 0, attempt_id INTEGER PRIMARY KEY REFERENCES attempts(id), account TEXT NOT NULL, project_id INTEGER NOT NULL, job_id INTEGER NOT NULL REFERENCES jobs(id), period TEXT NOT NULL, reserved_seconds INTEGER NOT NULL, settled_seconds INTEGER NOT NULL, state TEXT NOT NULL, settled_at TEXT NOT NULL ); CREATE UNIQUE INDEX account_label ON accounts(lower(trim(label))); CREATE INDEX actions_state ON actions(state); CREATE INDEX artifacts_job ON artifacts(job_id, role); CREATE INDEX attempts_job ON attempts(job_id, containment); CREATE UNIQUE INDEX attempts_one_live ON attempts(step_id) WHERE containment = 'pending'; CREATE INDEX idx_decision_owners_address ON decision_owners(address); CREATE INDEX jobs_account ON jobs(account, state); CREATE UNIQUE INDEX project_binding ON projects(repository, app_name); CREATE INDEX reservations_account ON reservations(account, state); CREATE INDEX reservations_job ON reservations(job_id, state); CREATE INDEX time_reservations_account ON time_reservations(account, project_id, state, period); CREATE TABLE grant_fences (ref TEXT PRIMARY KEY, version INTEGER NOT NULL, epoch INTEGER NOT NULL); CREATE TABLE recovery_charges (key TEXT PRIMARY KEY, account TEXT NOT NULL, project_id INTEGER NOT NULL, reserved TEXT NOT NULL, actor TEXT NOT NULL, reason TEXT NOT NULL, incarnation INTEGER NOT NULL, created_at TEXT NOT NULL); CREATE TABLE recovery_events (revision INTEGER PRIMARY KEY, kind TEXT NOT NULL, actor TEXT NOT NULL, reason TEXT NOT NULL, snapshot_hash TEXT NOT NULL); CREATE INDEX reservations_period_account ON reservations(account,state,period); CREATE INDEX reservations_period_project ON reservations(account,project_id,state,period); CREATE INDEX time_period_account ON time_reservations(account,state,period); CREATE INDEX time_period_project ON time_reservations(account,project_id,state,period); CREATE INDEX recovery_charges_scope ON recovery_charges(account,project_id); CREATE TABLE runners ( id text PRIMARY KEY, person text, machine_key_hash text, label text, revision integer, revoked_at datetime, instance_id text, role text, removing_at datetime, tainted_at datetime ); CREATE INDEX runner_person ON runners(person); CREATE TABLE runner_enrolments ( id text PRIMARY KEY, instance_id text, audience text, machine_public_key text, machine_key_hash text, label text, device_secret_hash text, user_code_hash text, challenge text, expires_at datetime, next_poll_at datetime, person text, runner_id text, approved_at datetime, consumed_at datetime, revoked_at datetime ); CREATE UNIQUE INDEX runner_device_secret ON runner_enrolments(device_secret_hash); CREATE UNIQUE INDEX runner_user_code ON runner_enrolments(user_code_hash); CREATE TABLE runner_credentials ( id text PRIMARY KEY, runner_id text, instance_id text, audience text, person text, machine_key_hash text, token_hash text, expires_at datetime, rotated_at datetime, revoked_at datetime ); CREATE INDEX runner_credential_runner ON runner_credentials(runner_id); CREATE UNIQUE INDEX runner_token ON runner_credentials(token_hash); CREATE TABLE runner_rates ( key text PRIMARY KEY, window_start datetime, count integer ); CREATE TABLE connections ( label text, revoked_at datetime, id text PRIMARY KEY, runner_id text, person text, local_id text, revision integer, last_seen datetime, reports_json text, security_hash text ); CREATE UNIQUE INDEX connection_local ON connections(runner_id, local_id); CREATE INDEX connection_person ON connections(person); CREATE TABLE connection_choices ( person text, project_id integer, revision integer, connection_ids_json text, pinned_id text, mode text NOT NULL DEFAULT "best_fit", PRIMARY KEY(person, project_id) ); CREATE TABLE isolation_acknowledgements ( id text PRIMARY KEY, connection_id text, owner text, provider text, provider_version text, paseo_version text, isolation_config_hash text, acknowledged_at datetime, connection_revision integer, owner_revision integer, revoked_at datetime ); CREATE INDEX connection_ack ON isolation_acknowledgements(connection_id); CREATE TABLE connection_invalidations ( id integer PRIMARY KEY AUTOINCREMENT, connection_id text, revision integer, reason text, at datetime ); CREATE INDEX connection_invalidation ON connection_invalidations(connection_id); CREATE TABLE tasks (id text, requester text, description text, request_id text, project_id integer, revision integer, job_id integer, state text, wait_code text, base_commit text, review_ref text, check_definition_hash text, team_id text, merge_owner text, baseline_test_set_hash text, PRIMARY KEY (id)); CREATE UNIQUE INDEX task_request ON tasks(requester, request_id); CREATE TABLE task_outboxes (task_id text, state text, project_revision integer, budget_revision integer, engineering text, review text, prepared text, job_spec text, PRIMARY KEY (task_id)); CREATE UNIQUE INDEX jobs_request ON jobs(account, requester, request_key) WHERE request_key <> ''; CREATE TABLE admission_tickets ( action_id INTEGER PRIMARY KEY REFERENCES actions(id), job_id INTEGER NOT NULL REFERENCES jobs(id), request_id TEXT NOT NULL, attempt_id INTEGER NOT NULL REFERENCES attempts(id), fence INTEGER NOT NULL, runner TEXT NOT NULL, dispatch TEXT NOT NULL, expires_at TEXT NOT NULL, incarnation INTEGER NOT NULL, UNIQUE(job_id, request_id) ); CREATE TABLE runner_assignments (id text, task_id text, step_key text, workspace_key text, team_id text, runner_id text, person text, selection text, isolation text, kind text, state text, revision integer, job_id integer, attempt_id integer, fence integer, claim_request text, brief text, lease_deadline datetime, invalidation_id integer, PRIMARY KEY (id)); CREATE UNIQUE INDEX runner_assignment_step ON runner_assignments(task_id, step_key, revision); CREATE TABLE runner_cleanups (id text, assignment_id text, runner_id text, person text, machine_key_hash text, instance_id text, audience text, token_hash text, fence integer, attempt_id integer, expires_at datetime, PRIMARY KEY(id)); CREATE UNIQUE INDEX runner_cleanup_token ON runner_cleanups(token_hash); CREATE TABLE runner_observations (assignment_id text, request_id text, operation text, payload_hash text, payload text, at datetime, PRIMARY KEY(assignment_id, request_id)); CREATE TABLE runner_rotations (runner_id text, assignment_id text, request_id text, state text, credential_id text, PRIMARY KEY(runner_id)); CREATE TABLE artifact_blobs (team_id text, job_id integer, digest text, bytes blob, PRIMARY KEY(team_id, job_id, digest)); CREATE TABLE artifact_blob_uploads (attempt_id integer, digest text, size integer, PRIMARY KEY(attempt_id, digest)); CREATE TABLE runner_events (key text, assignment_id text, stream_key text, epoch text, sequence integer, kind text, content_hash text, body text, isolation text, PRIMARY KEY(key)); CREATE INDEX runner_event_assignment ON runner_events(assignment_id); CREATE INDEX runner_event_timeline ON runner_events(stream_key, epoch, sequence); CREATE TABLE runner_event_ids (runner_id text, event_id text, event_key text, PRIMARY KEY(runner_id, event_id)); CREATE TABLE runner_timelines (stream_key text, assignment_id text, epoch text, high_water integer, blocked numeric, isolation text, PRIMARY KEY(stream_key)); CREATE INDEX runner_timeline_assignment ON runner_timelines(assignment_id); CREATE TABLE runner_artifact_requests (assignment_id text, request_id text, payload_hash text, artifact_id integer, PRIMARY KEY(assignment_id, request_id)); CREATE TABLE questions (id text, assignment_id text, task_id text, server_id text, agent_id text, session_id text, permission_id text, kind text, attempt integer, fence integer, revision integer, raw_request text, input_hash text, state text, answer text, answered_by text, expires_at datetime, PRIMARY KEY(id)); CREATE INDEX question_assignment ON questions(assignment_id); CREATE TABLE supervised_trials (id text PRIMARY KEY,runner_id text,owner text,owner_revision integer,acknowledged_at datetime,versions text,revoked_at datetime); CREATE INDEX trial_runner ON supervised_trials(runner_id); ALTER TABLE runner_assignments ADD COLUMN protocol_version integer NOT NULL DEFAULT 1; CREATE TABLE conversations (id text PRIMARY KEY, team_id text, person text, revision integer, last_sequence integer); CREATE UNIQUE INDEX conversation_person ON conversations(team_id,person); CREATE TABLE conversation_threads (id text PRIMARY KEY, task_id text, team_id text, revision integer, last_sequence integer); CREATE UNIQUE INDEX conversation_task ON conversation_threads(task_id); CREATE TABLE conversation_messages (id text PRIMARY KEY, stream_id text, sequence integer, conversation_id text, thread_id text, author_kind text, author_id text, body text, revision integer, created_at datetime, source_refs text); CREATE UNIQUE INDEX conversation_sequence ON conversation_messages(stream_id,sequence); CREATE TABLE conversation_cards (id text PRIMARY KEY, stream_id text, kind text, resource_id text, sequence integer, revision integer); CREATE INDEX conversation_card_stream ON conversation_cards(stream_id); CREATE TABLE conversation_requests (principal text, operation text, resource text, request_id text, input_hash text, result text, PRIMARY KEY(principal,operation,resource,request_id)); CREATE TABLE conversation_routes (source_id text PRIMARY KEY, task_id text, reply_id text, notice_id text); CREATE TABLE conversation_outboxes (message_id text PRIMARY KEY, stream_id text, purpose text); CREATE TABLE conversation_cursors (id text PRIMARY KEY, principal text, stream_id text, after integer, through integer, revision integer, expires_at datetime); ALTER TABLE tasks ADD COLUMN kind text NOT NULL DEFAULT 'bug'; ALTER TABLE tasks ADD COLUMN plan_revision integer NOT NULL DEFAULT 0; ALTER TABLE tasks ADD COLUMN approval_revision integer NOT NULL DEFAULT 0; ALTER TABLE tasks ADD COLUMN replacement_id text NOT NULL DEFAULT ''; ALTER TABLE tasks ADD COLUMN replaces_id text NOT NULL DEFAULT ''; CREATE TABLE plan_revisions (task_id text, revision integer, plan text, actor_kind text, actor text, changed_keys text, created_at datetime, PRIMARY KEY(task_id,revision)); CREATE TABLE plan_requests (principal text, operation text, resource text, request_id text, input_hash text, result text, PRIMARY KEY(principal,operation,resource,request_id)); CREATE TABLE plan_approval_settings (project_id integer, person text, mode text, revision integer, PRIMARY KEY(project_id,person)); CREATE TABLE coordinator_bindings (id text PRIMARY KEY, person text, runner_id text, connection_id text, revision integer, generation integer, team_id text, person_key text, member_revision integer, connection_revision integer); CREATE UNIQUE INDEX coordinator_person ON coordinator_bindings(team_id,person_key); CREATE TABLE coordinator_turns (id text PRIMARY KEY, message_id text, request_id text, provider_session_id text, context_hash text, generation integer, revision integer, state text, team_id text, person text, binding_id text, conversation_id text, sequence integer, project_id integer, project_revision integer, member_revision integer, connection_revision integer, claim text, selection text, input_hash text, delivery_hash text, session_id text, job_id integer, attempt_id integer, fence integer); CREATE TABLE coordinator_questions (id text PRIMARY KEY, binding_id text, turn_id text, person text, request_id text, state text, payload text, generation integer, revision integer); CREATE TABLE IF NOT EXISTS coordinator_limits ( account TEXT NOT NULL, person TEXT NOT NULL, revision INTEGER NOT NULL, limits TEXT NOT NULL, content_hash TEXT NOT NULL, PRIMARY KEY(account,person) ); UPDATE service SET schema_version=8 WHERE id=1; ALTER TABLE audit_events ADD COLUMN delegating_person text; CREATE UNIQUE INDEX coordinator_active ON coordinator_turns(team_id,person) WHERE state='running'; CREATE TABLE personal_preferences (id text PRIMARY KEY, text text, status text, source_message_id text, revision integer, team_id text, person text, source_revision integer, deleted_at datetime); CREATE TABLE personal_settings (team_id text, person text, landing text, revision integer, PRIMARY KEY (team_id,person)); CREATE TABLE person_api_requests (principal text NOT NULL, operation text NOT NULL, resource text NOT NULL, request_id text NOT NULL, input_hash text NOT NULL, result text NOT NULL, created_at datetime NOT NULL, expires_at datetime NOT NULL, PRIMARY KEY (principal,operation,resource,request_id)); CREATE INDEX person_api_requests_expiry ON person_api_requests(expires_at); CREATE TABLE specialist_requests (team_id text, principal text, operation text, resource text, request_id text, input_hash text, result text, PRIMARY KEY (team_id,principal,operation,resource,request_id)); CREATE TABLE playbooks (id text, team_id text, creator text, name text, kind text, steps text, project_ids text, template_id text, template_version integer, template_base text, revision integer, archived numeric, PRIMARY KEY (id)); CREATE TABLE playbook_revisions (id text, revision integer, value text, PRIMARY KEY (id,revision)); CREATE TABLE playbook_defaults (team_id text, project_id integer, kind text, playbook_id text, revision integer, PRIMARY KEY (team_id,project_id,kind)); CREATE TABLE playbook_snapshots (task_id text, playbook_id text, plan text, PRIMARY KEY (task_id,playbook_id)); CREATE TABLE playbook_requests (team_id text, principal text, operation text, resource text, request_id text, input_hash text, result text, PRIMARY KEY (team_id,principal,operation,resource,request_id)); CREATE TABLE playbook_suggestions (id text, playbook_id text, PRIMARY KEY (id)); CREATE TABLE plan_selections (task_id text,step_key text,plan_revision integer,confirmed numeric,selection text,PRIMARY KEY (task_id,step_key)); CREATE TABLE invitations ( id text PRIMARY KEY, address text, role text, state text, inviter text, secret_hash text, revision integer, target_revision integer, inviter_revision integer, created_at datetime, expires_at datetime, accepted_at datetime ); CREATE INDEX invitation_address ON invitations(address); CREATE TABLE invitation_requests ( principal text, operation text, resource text, request_id text, input_hash text, result text, PRIMARY KEY(principal, operation, resource, request_id) ); CREATE TABLE onboarding_states ( person text PRIMARY KEY, revision integer, provider_choice text, runner_id text, account_setup_id text, completed text ); ALTER TABLE runner_enrolments ADD COLUMN created_at datetime; ALTER TABLE runner_enrolments ADD COLUMN worker_only numeric; CREATE TABLE account_setups ( id text PRIMARY KEY, person text, runner_id text, provider text, label text, state text, mode text, connection_id text, revision integer, runner_revision integer, member_revision integer, created_at datetime, expires_at datetime, challenge text ); CREATE INDEX account_setup_person ON account_setups(person); CREATE TABLE team_links ( id text PRIMARY KEY, person text, origin text, name text, last_signed_in_at datetime, revision integer ); CREATE UNIQUE INDEX team_link_person_origin ON team_links(person, origin); CREATE TABLE person_devices ( id text PRIMARY KEY, request_hash text, input_hash text, public_key text, label text, secret_hash text, code_hash text, challenge text, instance_id text, person text, session_epoch integer, revision integer, created_at datetime, expires_at datetime, approved_at datetime, exchanged_at datetime, revoked_at datetime, last_poll datetime, token_hash text, token_expires_at datetime, nonce_hash text, begin_cipher text, result_cipher text, result_expires_at datetime ); CREATE UNIQUE INDEX person_device_request ON person_devices(request_hash); CREATE INDEX person_device_token ON person_devices(token_hash); CREATE TABLE person_device_rates (key text PRIMARY KEY, started_at datetime, count integer); CREATE TABLE project_rules (project_id integer PRIMARY KEY, text text, revision integer); ALTER TABLE person_devices ADD COLUMN last_used_at datetime; CREATE TABLE notification_outbox (sequence integer PRIMARY KEY AUTOINCREMENT, id text NOT NULL, team_id text NOT NULL, recipient text NOT NULL, event text NOT NULL, resource_id text NOT NULL, resource_revision integer NOT NULL, kind text, resource_type text, deep_link text, state text, created_at datetime, acknowledged_at datetime); CREATE UNIQUE INDEX notification_id ON notification_outbox(id); CREATE UNIQUE INDEX notification_event ON notification_outbox(team_id,recipient,event,resource_id,resource_revision); CREATE TABLE notification_subscriptions (id text PRIMARY KEY, team_id text, person text, device_id text, revision integer, ack_sequence integer, active numeric, created_at datetime, expires_at datetime); CREATE INDEX notification_device ON notification_subscriptions(device_id); CREATE TABLE notification_deliveries (subscription_id text, sequence integer, delivered_at datetime, acknowledged_at datetime, PRIMARY KEY(subscription_id,sequence)); CREATE TABLE notification_history (team_id text, person text, pruned_through integer, PRIMARY KEY(team_id,person)); CREATE TRIGGER notification_clarification_insert AFTER INSERT ON coordinator_questions BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='clarification' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending'); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),b.team_id,NEW.person,'question',NEW.id,NEW.revision,'question','clarification','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM coordinator_bindings b WHERE b.id=NEW.binding_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_question_insert AFTER INSERT ON questions BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='question' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending'); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'question',NEW.id,NEW.revision,'question','question','/tasks/'||t.id||'/questions/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.id=NEW.task_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_task_insert AFTER INSERT ON tasks BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='task' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state IN ('cancelled','cancel_requested','merged','failed') OR (event='task_ready.plan' AND NOT (NEW.approval_revision<>NEW.plan_revision AND NEW.state NOT IN ('cancelled','cancel_requested','merged','failed','succeeded') AND (NEW.wait_code='plan.awaiting_approval' OR (NEW.state='proposed' AND EXISTS(SELECT 1 FROM plan_revisions p WHERE p.task_id=NEW.id AND p.revision=NEW.plan_revision AND p.actor_kind='coordinator') AND COALESCE(NULLIF((SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=NEW.requester),''),(SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=''),'immediate')='required'))))); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.requester,'task_ready.plan',NEW.id,NEW.revision,'task_ready','task','/tasks/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.approval_revision<>NEW.plan_revision AND NEW.state NOT IN ('cancelled','cancel_requested','merged','failed','succeeded') AND (NEW.wait_code='plan.awaiting_approval' OR (NEW.state='proposed' AND EXISTS(SELECT 1 FROM plan_revisions p WHERE p.task_id=NEW.id AND p.revision=NEW.plan_revision AND p.actor_kind='coordinator') AND COALESCE(NULLIF((SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=NEW.requester),''),(SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=''),'immediate')='required')) AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_card_insert AFTER INSERT ON conversation_cards BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='card' AND resource_id=NEW.id AND state<>'superseded' AND resource_revision<>NEW.revision; INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),c.team_id,c.person,'card',NEW.id,NEW.revision,'card','card','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM conversations c WHERE c.id=NEW.stream_id AND NEW.kind LIKE 'suggest_%' AND EXISTS(SELECT 1 FROM members m WHERE m.address=c.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_turn_insert AFTER INSERT ON coordinator_turns BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='coordinator' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state NOT IN ('uncertain','fenced','context_limit','context_unavailable')); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.person,'interrupted',NEW.id,NEW.revision,'interrupted','coordinator','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.state IN ('uncertain','fenced','context_limit','context_unavailable') AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_clarification_update AFTER UPDATE ON coordinator_questions BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='clarification' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending'); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),b.team_id,NEW.person,'question',NEW.id,NEW.revision,'question','clarification','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM coordinator_bindings b WHERE b.id=NEW.binding_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_question_update AFTER UPDATE ON questions BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='question' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending'); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'question',NEW.id,NEW.revision,'question','question','/tasks/'||t.id||'/questions/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.id=NEW.task_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_task_update AFTER UPDATE ON tasks BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='task' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state IN ('cancelled','cancel_requested','merged','failed') OR (event='task_ready.plan' AND NOT (NEW.approval_revision<>NEW.plan_revision AND NEW.state NOT IN ('cancelled','cancel_requested','merged','failed','succeeded') AND (NEW.wait_code='plan.awaiting_approval' OR (NEW.state='proposed' AND EXISTS(SELECT 1 FROM plan_revisions p WHERE p.task_id=NEW.id AND p.revision=NEW.plan_revision AND p.actor_kind='coordinator') AND COALESCE(NULLIF((SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=NEW.requester),''),(SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=''),'immediate')='required'))))); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.requester,'task_ready.plan',NEW.id,NEW.revision,'task_ready','task','/tasks/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.approval_revision<>NEW.plan_revision AND NEW.state NOT IN ('cancelled','cancel_requested','merged','failed','succeeded') AND (NEW.wait_code='plan.awaiting_approval' OR (NEW.state='proposed' AND EXISTS(SELECT 1 FROM plan_revisions p WHERE p.task_id=NEW.id AND p.revision=NEW.plan_revision AND p.actor_kind='coordinator') AND COALESCE(NULLIF((SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=NEW.requester),''),(SELECT mode FROM plan_approval_settings WHERE project_id=NEW.project_id AND person=''),'immediate')='required')) AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_card_update AFTER UPDATE ON conversation_cards BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='card' AND resource_id=NEW.id AND state<>'superseded' AND resource_revision<>NEW.revision; INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),c.team_id,c.person,'card',NEW.id,NEW.revision,'card','card','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM conversations c WHERE c.id=NEW.stream_id AND NEW.kind LIKE 'suggest_%' AND EXISTS(SELECT 1 FROM members m WHERE m.address=c.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_turn_update AFTER UPDATE ON coordinator_turns BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type='coordinator' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state NOT IN ('uncertain','fenced','context_limit','context_unavailable')); INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.person,'interrupted',NEW.id,NEW.revision,'interrupted','coordinator','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.state IN ('uncertain','fenced','context_limit','context_unavailable') AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_job_ready AFTER UPDATE ON jobs WHEN NEW.state='succeeded' AND OLD.state IS NOT NEW.state BEGIN INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,r.person,'task_ready.complete',t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t JOIN (SELECT id AS task_id,requester AS person FROM tasks UNION SELECT t.id,o.address FROM tasks t JOIN decision_owners o ON o.project_id=t.project_id) r ON r.task_id=t.id WHERE t.job_id=NEW.id AND t.team_id=NEW.account AND EXISTS(SELECT 1 FROM members m WHERE m.address=r.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_job_decision AFTER UPDATE ON jobs WHEN NEW.wait='approval' AND (OLD.wait IS NOT NEW.wait OR OLD.revision IS NOT NEW.revision) BEGIN INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,r.person,'task_ready.decision.'||NEW.revision,t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t JOIN (SELECT id AS task_id,requester AS person FROM tasks UNION SELECT t.id,o.address FROM tasks t JOIN decision_owners o ON o.project_id=t.project_id) r ON r.task_id=t.id WHERE t.job_id=NEW.id AND t.team_id=NEW.account AND EXISTS(SELECT 1 FROM members m WHERE m.address=r.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_job_interrupted AFTER UPDATE ON jobs WHEN NEW.wait IN ('uncertain_external_outcome','runner_unavailable','connection_unavailable') AND OLD.wait IS NOT NEW.wait BEGIN INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'interrupted.job.'||NEW.revision,t.id,t.revision,'interrupted','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.job_id=NEW.id AND t.team_id=NEW.account AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING; END; CREATE TRIGGER notification_device_revoke AFTER UPDATE ON person_devices WHEN NEW.revoked_at IS NOT NULL AND OLD.revoked_at IS NULL BEGIN UPDATE notification_subscriptions SET active=0,revision=revision+1 WHERE device_id=NEW.id AND active=1; END; CREATE TRIGGER notification_member_revoke AFTER UPDATE ON members WHEN NEW.active=0 OR NEW.session_epoch<>OLD.session_epoch BEGIN UPDATE notification_subscriptions SET active=0,revision=revision+1 WHERE person=NEW.address AND active=1; END; CREATE TRIGGER notification_binding_generation AFTER UPDATE ON coordinator_bindings WHEN NEW.generation<>OLD.generation BEGIN UPDATE notification_outbox SET state='superseded' WHERE resource_type IN ('clarification','coordinator') AND resource_id IN (SELECT id FROM coordinator_questions WHERE binding_id=NEW.id UNION SELECT id FROM coordinator_turns WHERE binding_id=NEW.id); END; CREATE INDEX notification_feed ON notification_outbox(team_id,recipient,sequence); CREATE TABLE runner_repositories (runner_id text, repository text, PRIMARY KEY (runner_id, repository)); CREATE TABLE project_proposals (id text, conversation_id text, binding_id text, person text, repository text, name text, why text, state text, generation integer, revision integer, project_id integer, PRIMARY KEY (id)); CREATE INDEX project_proposal_person ON project_proposals(person); ALTER TABLE coordinator_turns ADD COLUMN partial_text text; ALTER TABLE coordinator_turns ADD COLUMN partial_sequence integer; ALTER TABLE coordinator_turns ADD COLUMN partial_at integer; ALTER TABLE coordinator_turns ADD COLUMN reply_message_id text; ALTER TABLE coordinator_turns ADD COLUMN superseded_by text; CREATE TABLE chat_turns (id text PRIMARY KEY, person text, state text, reason text, attempt integer, dispatched numeric, reply_id text, revision integer, created_at datetime, updated_at datetime, dispatched_at datetime, timing text, kind text, yield numeric, connection text, lane text, note text); CREATE INDEX chat_turn_person ON chat_turns(person,state); CREATE TABLE chat_messages (seq integer PRIMARY KEY, id text, person text, idempotency_key text, body_hash text, role text, text text, turn_id text, revision integer, created_at datetime, switched text, lane text); CREATE UNIQUE INDEX chat_message_id ON chat_messages(id); CREATE INDEX chat_message_person ON chat_messages(person); CREATE UNIQUE INDEX chat_message_key ON chat_messages(person,lane,idempotency_key); CREATE INDEX chat_message_turn ON chat_messages(turn_id); CREATE TABLE chat_events (id integer PRIMARY KEY, person text, type text, body text, created_at datetime, lane text); CREATE INDEX chat_event_person ON chat_events(person); CREATE TABLE runner_commands (seq integer PRIMARY KEY, person text, kind text, body text, created_at datetime, acked_at datetime, runner_id text); CREATE INDEX runner_command_person ON runner_commands(person); CREATE TABLE runner_inbox (runner_id text, frame_id text, seen_at datetime, PRIMARY KEY (runner_id, frame_id)); CREATE TABLE runner_sessions (runner_id text PRIMARY KEY, generation integer, connected numeric, process text, revision integer, hello text, seen_at datetime, protocol integer); CREATE TABLE chat_copy_attempts (id text PRIMARY KEY, task_id text, step_id text, attempt integer, tokens integer, known numeric); CREATE TABLE chat_copy_feedback (id text PRIMARY KEY, project_id text, owner text, source_id text, key text, before text, after text, reason text, origin text, learned numeric, created_at datetime); CREATE TABLE chat_copy_reviews (id text PRIMARY KEY, project_id text, task_id text, step_id text, attempt integer, owner text, delegation integer, candidate text, input text, key text, text text, hash text, flag text, created_at datetime); CREATE INDEX idx_copy_reviews_task ON chat_copy_reviews(task_id); CREATE INDEX idx_copy_reviews_attempt ON chat_copy_reviews(step_id, attempt); CREATE TABLE chat_projects (id text PRIMARY KEY, repo text, state text, added_by text, revision integer, created_at datetime, updated_at datetime, title text, site_app text, copy_owner text, copy_revision integer); CREATE UNIQUE INDEX chat_project_site_app ON chat_projects(site_app); CREATE UNIQUE INDEX chat_project_repo ON chat_projects(repo); CREATE TABLE chat_cards (id text PRIMARY KEY, person text, kind text, state text, turn_id text, project_id text, question text, options text, choice text, answer_message text, revision integer, created_at datetime, updated_at datetime, undo text, task_id text, name text, action_id text, visible_at datetime, held numeric, lane text, source_card text, links text, result text, handoff_id text, detail text); CREATE INDEX chat_card_person ON chat_cards(person); CREATE INDEX chat_card_source ON chat_cards(source_card); CREATE INDEX chat_card_project ON chat_cards(project_id); CREATE INDEX chat_card_answer ON chat_cards(answer_message); CREATE TABLE chat_tool_calls (call_id text PRIMARY KEY, person text, turn_id text, name text, result text, created_at datetime, attempt integer, args_hash text, op text, lane text, state text, operation text, ledger_key text, args text); CREATE TABLE chat_settings (key text PRIMARY KEY, value text, revision integer, updated_by text, updated_at datetime, unset numeric); CREATE TABLE chat_coordinators (person text PRIMARY KEY, state text, turn_id text, revision integer, moved_in_at datetime, updated_at datetime); CREATE TABLE chat_notebooks (person text PRIMARY KEY, entries text, revision integer, updated_at datetime); CREATE INDEX chat_card_task ON chat_cards(task_id); CREATE INDEX chat_card_handoff ON chat_cards(handoff_id); CREATE TABLE chat_project_attachments (project_id text, kind text, ref text, name text, created_at datetime, PRIMARY KEY (project_id, kind, ref)); CREATE TABLE chat_tasks (id text PRIMARY KEY, project_id text, title text, brief text, stage text, branch text, base_sha text, creator text, turn_id text, cancel_requested numeric, revision integer, created_at datetime, updated_at datetime, watching text, moved_from text, branch_head text, merge_asked text, merged_sha text, merged_into text, merge_strategy text, merged_without_checks numeric, merged_at datetime, ready_sha text, merge_eligible text, done_at datetime, duplicate_of text, folded_into text, folded_stage text, folded_at datetime, engineering_runner text, merge_hold text, merge_verified numeric, revert_of text, revert_sha text, kind text, direction_asked text, built_as text, explored_from text, priority text, due_by datetime, priority_set_by text, priority_revision integer, publish_asked text, repair_round integer, copy_hold numeric, copy_recovery text, lane text); CREATE INDEX chat_task_project ON chat_tasks(project_id); CREATE INDEX chat_task_lane ON chat_tasks(lane); CREATE TABLE chat_steps (id text PRIMARY KEY, task_id text, idx integer, specialist text, person text, state text, reason text, attempt integer, base_sha text, candidate_sha text, pushed numeric, terminated numeric, summary text, log_tail text, started_at datetime, finished_at datetime, revision integer, created_at datetime, updated_at datetime, round integer, family text, account text, verdict text, review_label text, findings text, checks text, gate text, exit_code integer, output text, republish numeric, from_attempt integer, askable numeric, rebase numeric, from_base text, from_candidate text, merge_sha text, merge_into text, merge_strategy text, without_checks numeric, activity text, switched text, headline text, runner_id text, needs text, placement text, waiting_since datetime, refused_by text, withdrawn numeric, termination_kind text, publication text, publication_id text, apple numeric, checks_sandbox text, recheck_by text, change_facts text, decided_by text, merge_source text, merge_parents text, hosted_ci text, sandbox_runner text, unsandboxed_by text, unsandboxed_at datetime, unsandboxed_attempt integer, unsandboxed_runner text, has_preview numeric, preview text, kind text, note text, boundary text, question text, request_turn text, sources_card text, sources_hash text, research_grant text, research_grant_revision integer, hold text, housekeeping_attempt integer, held_at datetime, publish text, model text, copy_owner text, copy_revision integer, copy_result text, failure_signature text, ability_held numeric); CREATE INDEX chat_step_task ON chat_steps(task_id); CREATE TABLE chat_task_entries (id text PRIMARY KEY, task_id text, idempotency_key text, kind text, author text, text text, repo text, path text, line integer, created_at datetime, headline text, folded_from text); CREATE INDEX chat_entry_task ON chat_task_entries(task_id); CREATE UNIQUE INDEX chat_entry_key ON chat_task_entries(task_id, idempotency_key); CREATE TABLE chat_task_distinct (a text, b text, created_at datetime, PRIMARY KEY (a, b)); CREATE TABLE chat_questions (id text PRIMARY KEY, task_id text, asker text, idempotency_key text, step_id text, specialist text, chosen text, text text, state text, reason text, attempt integer, dispatched numeric, answer text, timing text, revision integer, created_at datetime, updated_at datetime); CREATE INDEX chat_question_task ON chat_questions(task_id); CREATE UNIQUE INDEX chat_question_key ON chat_questions(task_id, asker, idempotency_key); CREATE TABLE chat_connections (id text PRIMARY KEY, person text, runner_id text, local_id text, provider text, kind text, label text, state text, reason text, plan text, headroom text, prompt text, revision integer, created_at datetime, updated_at datetime, expires_at datetime, expires_estimated numeric, stated_email text, harness text, upstream text, billing text, model text, limit_confirmed_at datetime, payg_off_confirmed_at datetime, budget text, budget_applied integer, spend text, models text, models_revision integer, models_applied integer, models_state text, models_listed text); CREATE INDEX chat_connection_person ON chat_connections(person); CREATE TABLE chat_usage (id integer PRIMARY KEY, person text, connection text, provider text, model text, role text, task_id text, step_id text, turn_id text, attempt integer, outcome text, complete numeric, input_tokens integer, cached_input_tokens integer, cache_write_tokens integer, output_tokens integer, reasoning_output_tokens integer, recorded_at datetime, round integer, turns integer, overcount numeric, cost_micros integer, currency text, price_version text, cost_reported_micros integer, family text, tier text, tier_version text, model_mismatch numeric); CREATE INDEX chat_usage_person ON chat_usage(person, recorded_at); CREATE UNIQUE INDEX chat_usage_key ON chat_usage(turn_id, step_id, attempt, connection, model); CREATE INDEX chat_tool_call_turn ON chat_tool_calls(turn_id, op); CREATE INDEX chat_tool_call_state ON chat_tool_calls(state); CREATE TABLE chat_housekeeping (id text PRIMARY KEY, team text, person text, detector text, subject_kind text, subject_id text, subject_revision integer, finding_key text, class text, state text, reason text, claimed_at datetime, attempt text, reserved_turns integer, reserved_tokens integer, reserved_retries integer, charged_turns integer, charged_tokens integer, charged_retries integer, charged_complete numeric, releasing numeric, evidence text, result text, visible_at datetime, undo_until datetime, recurrences integer, at datetime, undone_at datetime, revision integer, reserved_rounds integer, charged_rounds integer); CREATE UNIQUE INDEX chat_housekeeping_key ON chat_housekeeping(team, person, detector, finding_key); CREATE INDEX chat_housekeeping_person ON chat_housekeeping(person, at); CREATE INDEX chat_housekeeping_subject ON chat_housekeeping(subject_id); CREATE TABLE chat_housekeeping_failures (id integer PRIMARY KEY, person text, detector text, kind text, action_id text, at datetime); CREATE INDEX chat_housekeeping_failure_person ON chat_housekeeping_failures(person, detector, at); CREATE TABLE chat_housekeeping_settings (person text PRIMARY KEY, paused numeric, disabled text, last_digest_at datetime, last_sweep_at datetime, revision integer, quiet_from integer, quiet_until integer); CREATE TABLE chat_attachments (id text PRIMARY KEY, person text, message_id text, position integer, kind text, name text, media_type text, size integer, sha256 text, width integer, height integer, has_preview numeric, revision integer, created_at datetime); CREATE INDEX chat_attachment_message ON chat_attachments(message_id); CREATE INDEX chat_attachment_person ON chat_attachments(person); CREATE TABLE chat_attachment_data (id text PRIMARY KEY, data blob, preview blob); CREATE INDEX runner_command_runner ON runner_commands(runner_id); CREATE INDEX chat_step_runner ON chat_steps(runner_id); CREATE TABLE runner_transfers (id text PRIMARY KEY, person text, from_runner text, to_runner text, repo text, task_id text, step_id text, attempt integer, base_sha text, candidate_sha text, direction text, state text, sha256 text, total integer, received integer, bytes integer, expires_at datetime, created_at datetime); CREATE INDEX runner_transfer_step ON runner_transfers(step_id); CREATE TABLE chat_merge_policies (project_id text PRIMARY KEY, mode text, mode_reason text, max_lines integer, max_files integer, main_ships text, ships_answered_by text, ships_asked numeric, extra_sensitive text, words text, revision integer, updated_by text, updated_at datetime); CREATE TABLE chat_merge_audit (id text PRIMARY KEY, project_id text, task_id text, kind text, at text, candidate_sha text, merge_sha text, class text, stance text, decided_by text, reasons text, inputs text, created_at datetime); CREATE INDEX chat_merge_audit_project ON chat_merge_audit(project_id); CREATE INDEX chat_merge_audit_task ON chat_merge_audit(task_id); CREATE TABLE chat_project_stars (person text, project_id text, starred numeric, set_by text, reason text, n integer, signal_at datetime, told_at datetime, created_at datetime, updated_at datetime, PRIMARY KEY (person, project_id)); CREATE TABLE chat_specialists (id text PRIMARY KEY, profile text, name text, brief text, model_preference text, state text, revision integer, proposed_by text, proposed_turn text, card_id text, approver text, approved_at datetime, decided_by text, decided_at datetime, revoked_by text, revoked_at datetime, created_at datetime, updated_at datetime); CREATE INDEX chat_specialist_profile ON chat_specialists(profile); CREATE INDEX chat_specialist_card ON chat_specialists(card_id); CREATE TABLE chat_capability_grants (id text PRIMARY KEY, specialist_id text, capability text, revision integer, granted_by text, granted_at datetime, revoked_by text, revoked_at datetime); CREATE INDEX chat_capability_grant_specialist ON chat_capability_grants(specialist_id); CREATE TABLE chat_specialist_audit (id text PRIMARY KEY, specialist_id text, action text, actor text, revision integer, detail text, at datetime); CREATE INDEX chat_specialist_audit_specialist ON chat_specialist_audit(specialist_id); CREATE INDEX chat_step_sources_card ON chat_steps(sources_card); CREATE TABLE chat_research_sources (step_id text, idx integer, source_id text, url text, origin text, approved numeric, created_at datetime, PRIMARY KEY (step_id, idx)); CREATE TABLE chat_research_addresses (runner_id text, source text, address text, seen_at datetime, PRIMARY KEY (runner_id, source, address)); CREATE TABLE chat_research_prefixes (runner_id text, prefix text, confirmed_by text, confirmed_at datetime, card_id text, PRIMARY KEY (runner_id, prefix)); CREATE TABLE chat_research_inventory (card_id text PRIMARY KEY, runner_id text, addresses text, created_at datetime, updated_at datetime); CREATE INDEX chat_research_inventory_runner ON chat_research_inventory(runner_id); CREATE TABLE instance_prefixes (prefix text PRIMARY KEY, confirmed_by text, confirmed_at datetime, card_id text); CREATE TABLE instance_addresses (address text PRIMARY KEY, seen_at datetime); CREATE TABLE chat_instance_inventory (card_id text PRIMARY KEY, prefixes text, addresses text, created_at datetime); CREATE TABLE chat_web_search_usage (backend text, month text, used integer NOT NULL DEFAULT 0, PRIMARY KEY (backend, month)); CREATE TABLE chat_lookups (id text PRIMARY KEY, person text NOT NULL, turn_id text NOT NULL, attempt integer NOT NULL, seq integer NOT NULL, call_id text NOT NULL, state text NOT NULL, queries text, reads text, answer text, sources text, code text, created_at datetime NOT NULL); CREATE INDEX chat_lookup_turn ON chat_lookups(turn_id, attempt, seq); CREATE INDEX chat_lookup_person ON chat_lookups(person, created_at); CREATE TABLE chat_research (step_id text, attempt integer, task_id text, revision integer, findings text, created_at datetime, PRIMARY KEY (step_id, attempt)); CREATE INDEX chat_research_task ON chat_research(task_id); CREATE TABLE chat_research_fetches (step_id text, attempt integer, idx integer, task_id text, source_id text, origin text, parent text, url text, hops text, address text, status integer, type text, bytes integer, sha256 text, millis integer, reason text, PRIMARY KEY (step_id, attempt, idx)); CREATE TABLE chat_task_files (id text PRIMARY KEY, task_id text, step_id text, attempt integer, runner_id text, image_id text, source_id text, role text, description text, media_type text, size integer, sha256 text, total integer, received integer, bytes integer, width integer, height integer, complete numeric, kept numeric, created_at datetime, updated_at datetime); CREATE INDEX chat_task_file_task ON chat_task_files(task_id); CREATE INDEX chat_task_file_step ON chat_task_files(step_id); CREATE TABLE chat_task_file_data (file_id text, idx integer, data blob, PRIMARY KEY (file_id, idx)); CREATE TABLE chat_releases (id text PRIMARY KEY, task_id text, step_id text, step_revision integer, person text, runner_id text, created_at datetime); CREATE INDEX chat_release_task ON chat_releases(task_id); CREATE UNIQUE INDEX chat_release_step ON chat_releases(step_id, step_revision); CREATE TABLE chat_advice (id text PRIMARY KEY, person text, key text, kind text, state text, subject text, facts text, text text, act text, dismiss text, card_id text, revision integer, created_at datetime, updated_at datetime, visible_at datetime, shown_at datetime, dismissed_at datetime); CREATE INDEX chat_advice_person ON chat_advice(person, key); CREATE TABLE chat_assistant_hosts (person text PRIMARY KEY, origin text, token text, revision integer, updated_at datetime, capabilities text); CREATE TABLE chat_assistant_abilities (person text, origin text, revision integer, abilities text, admin numeric, ask text, source text, fetch_seq integer, confirmed_at datetime, revoked_at datetime, authoritative_seen numeric, PRIMARY KEY (person, origin)); CREATE TABLE chat_handoffs (id text PRIMARY KEY, person text, assistant_turn text, message_id text, context text, ledger_key text, state text, digest text, created_at datetime, updated_at datetime, lane text, title text, nudges integer NOT NULL DEFAULT 0, ability_held_at datetime); CREATE UNIQUE INDEX chat_handoff_key ON chat_handoffs(ledger_key); CREATE INDEX chat_handoff_person ON chat_handoffs(person); CREATE INDEX chat_handoff_message ON chat_handoffs(message_id); CREATE TABLE chat_handoff_turns (handoff_id text, turn_id text PRIMARY KEY); CREATE INDEX chat_handoff_turn_handoff ON chat_handoff_turns(handoff_id); CREATE TABLE chat_handoff_tasks (handoff_id text, task_id text, created numeric, PRIMARY KEY (handoff_id, task_id)); CREATE INDEX chat_handoff_task ON chat_handoff_tasks(task_id); CREATE TABLE chat_handoff_actions (ledger_key text PRIMARY KEY, handoff_id text NOT NULL, person text NOT NULL, kind text NOT NULL, context text, message_id text, result text, created_at datetime); CREATE INDEX chat_handoff_action_handoff ON chat_handoff_actions(handoff_id); CREATE INDEX chat_handoff_action_message ON chat_handoff_actions(message_id); ALTER TABLE members ADD COLUMN identity_key text NOT NULL DEFAULT ""; UPDATE members SET identity_key=lower(hex(randomblob(16))); CREATE UNIQUE INDEX members_identity_key ON members(identity_key); CREATE TRIGGER equipo_member_identity AFTER INSERT ON members WHEN NEW.identity_key='' BEGIN UPDATE members SET identity_key=lower(hex(randomblob(16))) WHERE address=NEW.address; END; CREATE TABLE equipo_id_accounts (id INTEGER PRIMARY KEY AUTOINCREMENT, binding TEXT NOT NULL UNIQUE, member_key TEXT NOT NULL UNIQUE, linked_at INTEGER NOT NULL DEFAULT 0); CREATE TABLE equipo_id_reauth (id_hash TEXT PRIMARY KEY, account_id INTEGER NOT NULL, session_hash TEXT NOT NULL, started INTEGER NOT NULL, expires INTEGER NOT NULL, purpose TEXT NOT NULL, ready INTEGER NOT NULL DEFAULT 0); CREATE TABLE chat_card_threads (id text PRIMARY KEY, card_id text, person text, message_id text, turn_id text, step_id text, text text, created_at datetime); CREATE TABLE chat_card_visibility (person text, card_id text, aside numeric, updated_at datetime, PRIMARY KEY (person, card_id)); CREATE INDEX chat_card_thread_card ON chat_card_threads(card_id); CREATE INDEX chat_card_thread_message ON chat_card_threads(message_id); ALTER TABLE chat_cards ADD COLUMN placement text; ALTER TABLE chat_tasks ADD COLUMN move_project text; ALTER TABLE chat_tasks ADD COLUMN move_by text; ALTER TABLE chat_tasks ADD COLUMN move_restart numeric; ALTER TABLE chat_tasks ADD COLUMN move_count integer; ALTER TABLE chat_steps ADD COLUMN retired numeric; ALTER TABLE chat_messages ADD COLUMN origin text; ALTER TABLE chat_turns ADD COLUMN create_repo text; ALTER TABLE chat_notebooks ADD COLUMN corrections text; CREATE INDEX chat_task_move ON chat_tasks(creator, move_project) WHERE move_project IS NOT NULL AND move_project <> ''; UPDATE chat_notebooks SET entries = (SELECT json_group_array(json(value)) FROM json_each(chat_notebooks.entries) WHERE NOT (coalesce(json_extract(value, '$.source'), '') = 'own' AND substr(json_extract(value, '$.id'), 1, 2) = 'mv')) WHERE entries IS NOT NULL AND entries <> ''; CREATE TABLE oficina_connections (person text PRIMARY KEY, refresh text, revision integer, created_at datetime, updated_at datetime); ALTER TABLE chat_tasks ADD COLUMN move_abandoned numeric; ALTER TABLE chat_cards ADD COLUMN access text; CREATE TABLE chat_connect_turns (card_id text PRIMARY KEY, turn_id text, person text, asked_turn text, created_at datetime); CREATE UNIQUE INDEX chat_connect_turn ON chat_connect_turns(turn_id); ALTER TABLE chat_projects ADD COLUMN autonomy text; ALTER TABLE chat_projects ADD COLUMN autonomy_owner text; ALTER TABLE chat_projects ADD COLUMN autonomy_revision integer; ALTER TABLE chat_steps ADD COLUMN base_checks_sha text; -- Historical notebook entries have no composer reference. Preserve their text -- and IDs, but do not certify the old model-written attribution as person words. UPDATE chat_notebooks SET entries = ( SELECT json_group_array(json(CASE WHEN json_extract(value, '$.source') = 'person' THEN json_set(value, '$.source', 'own') ELSE value END)) FROM json_each(chat_notebooks.entries) ), revision = coalesce(revision, 0) + 1 WHERE CASE WHEN json_valid(entries) THEN json_type(entries) = 'array' ELSE 0 END AND NOT EXISTS (SELECT 1 FROM json_each(chat_notebooks.entries) WHERE type <> 'object') AND EXISTS (SELECT 1 FROM json_each(chat_notebooks.entries) WHERE json_extract(value, '$.source') = 'person'); ALTER TABLE chat_steps ADD COLUMN base_checks text; CREATE TABLE chat_turn_carried (turn_id text, message_id text, person text, PRIMARY KEY (turn_id, message_id)); CREATE TABLE push_registrations (credential text PRIMARY KEY, person text, token text, environment text, platform text, start_token text, created_at datetime, updated_at datetime, actions_version integer, binding_id text); CREATE INDEX push_registration_person ON push_registrations(person); CREATE TABLE push_activities (credential text, task_id text, person text, token text, created_at datetime, PRIMARY KEY (credential, task_id)); CREATE INDEX push_activity_person ON push_activities(person); CREATE TABLE push_outbox (id text PRIMARY KEY, person text, credential text, kind text, target text, task_id text, push_type text, priority integer, collapse_id text, dedupe text, payload text, state text, next_at datetime, attempts integer, lease_until datetime, status integer, reason text, apns_id text, sent_at datetime, created_at datetime, updated_at datetime); CREATE INDEX push_outbox_due ON push_outbox(state, next_at); CREATE INDEX push_outbox_dedupe ON push_outbox(dedupe); CREATE INDEX push_outbox_credential ON push_outbox(credential); CREATE INDEX push_outbox_person ON push_outbox(person); CREATE TABLE push_tasks (person text, task_id text, title text, tone text, stage text, needs_you text, specialist text, step_id text, activity text, started_at datetime, ended numeric, line_at datetime, updated_at datetime, PRIMARY KEY (person, task_id)); CREATE TABLE push_once (person text, key text, created_at datetime, PRIMARY KEY (person, key)); -- Keep CARLOS retry deadlines across drains, newly queued pushes and restarts. CREATE TABLE push_holds (id text PRIMARY KEY, until datetime); ALTER TABLE person_devices ADD COLUMN native_session_hash text; CREATE INDEX person_device_native_session ON person_devices(native_session_hash); ALTER TABLE chat_turns ADD COLUMN retry_of text; -- Owner metadata is distinct from repository-derived display text. ALTER TABLE chat_projects ADD COLUMN metadata_set numeric; ALTER TABLE chat_projects ADD COLUMN description text; ALTER TABLE chat_messages ADD COLUMN project_id text; CREATE TABLE chat_project_names ( project_id text, person text, name text, created_at datetime, PRIMARY KEY (project_id, person, name) ); -- Preserve existing owner provenance, then track each field independently. ALTER TABLE chat_projects ADD COLUMN title_set numeric; ALTER TABLE chat_projects ADD COLUMN description_set numeric; UPDATE chat_projects SET title_set = metadata_set, description_set = metadata_set; -- Forge authority belongs to the team; pending setup is a single team slot. CREATE TABLE forge_connections ( id text PRIMARY KEY, generation integer, state text, login text, account_type text, account_id integer, app_id integer, slug text, app_name text, installation_id integer, selection text, confirmed numeric, active_account integer, sealed_key text, owner text, session text, origin text, manifest_hash text, installation_hash text, started_at datetime, redirect_until datetime, pending_until datetime, uninstalled_at datetime, count integer, count_known numeric, claim text, claim_until datetime, updated_at datetime ); CREATE UNIQUE INDEX forge_active_account ON forge_connections(active_account); CREATE TABLE forge_pending (id integer PRIMARY KEY, connection_id text); CREATE TABLE chat_person_zones (person text PRIMARY KEY, zone text NOT NULL, source text, updated_at datetime); ALTER TABLE chat_cards ADD COLUMN carry_on numeric; ALTER TABLE chat_cards ADD COLUMN carry_on_connection text; ALTER TABLE chat_releases ADD COLUMN connection text; ALTER TABLE chat_steps ADD COLUMN coordinator_attempt integer; -- Bounded Engineering file-list facts, never merge authority. ALTER TABLE chat_steps ADD COLUMN change_files text; -- Device passes and wrapper-only grants never create a general sign-in session. CREATE TABLE preview_passes ( hash text PRIMARY KEY, id text, instance text, person text, credential text, task_id text, candidate text, step_id text, fingerprint text, epoch integer, attempt integer, expires_at datetime, grant_expires_at datetime, consumed_at datetime ); CREATE INDEX preview_pass_expiry ON preview_passes(expires_at); CREATE TABLE preview_grants ( hash text PRIMARY KEY, id text, instance text, person text, credential text, task_id text, candidate text, step_id text, fingerprint text, epoch integer, attempt integer, expires_at datetime ); CREATE INDEX preview_grant_expiry ON preview_grants(expires_at); -- Replace pending passes and active grants by indexed device/task identity. CREATE INDEX idx_preview_passes_device_task ON preview_passes(credential, task_id); CREATE INDEX idx_preview_grants_device_task ON preview_grants(credential, task_id); CREATE TABLE chat_notification_receipts ( person text, credential text, operation text, target text, key text, digest text, card_id text, message_id text, turn_id text, attempt integer, expires_at datetime, PRIMARY KEY (person, credential, operation, target, key) ); CREATE INDEX notification_receipt_expiry ON chat_notification_receipts(expires_at); CREATE TABLE chat_places (id text PRIMARY KEY, kind text, adapter text, console text, account_sqid text, account_name text, state text, revision integer, proposed_by text, proposed_at datetime, confirmed_by text, confirmed_at datetime, created_at datetime, updated_at datetime); CREATE UNIQUE INDEX chat_place_destination ON chat_places(kind, adapter, console, account_sqid); CREATE TABLE chat_project_places (id text PRIMARY KEY, project_id text, kind text, place_id text, app text, revision integer, app_created_at datetime, app_created_step text, deploy_name text, deploy_runner text, deploy_minted_by text, deploy_minted_at datetime, deploy_step text, created_at datetime, updated_at datetime); CREATE UNIQUE INDEX chat_project_place_kind ON chat_project_places(project_id, kind); ALTER TABLE chat_cards ADD COLUMN place text; -- Display patches are separate from task views and merge evidence. CREATE TABLE chat_change_diffs ( step_id text, attempt integer, idx integer, task_id text, runner_id text, base text, candidate text, collection text, total integer, path text, patch text, reason text, truncated numeric, accepted numeric, expires_at datetime, PRIMARY KEY (step_id, attempt, idx) ); CREATE INDEX chat_diff_task ON chat_change_diffs(task_id); CREATE INDEX chat_diff_expiry ON chat_change_diffs(expires_at); ALTER TABLE chat_steps ADD COLUMN change_diff_expires_at datetime; ALTER TABLE chat_steps ADD COLUMN change_diff_attempt integer; -- Mac Engineering (slice F, protocol 37). Additive only: an existing row -- reads as before (a checks transfer, no Xcode access, no outcome, no -- kept session, no frozen question pin). ALTER TABLE runner_transfers ADD COLUMN kind text; ALTER TABLE runner_transfers ADD COLUMN old_base_sha text; ALTER TABLE chat_steps ADD COLUMN xcode_access text; ALTER TABLE chat_steps ADD COLUMN outcome_phase text; ALTER TABLE chat_steps ADD COLUMN session_id text; ALTER TABLE chat_steps ADD COLUMN publication_runner text; ALTER TABLE chat_steps ADD COLUMN on_worker numeric; ALTER TABLE chat_questions ADD COLUMN eng_attempt integer; ALTER TABLE chat_questions ADD COLUMN eng_account text; ALTER TABLE chat_questions ADD COLUMN eng_session text; ALTER TABLE chat_questions ADD COLUMN runner_id text; ALTER TABLE chat_questions ADD COLUMN base_sha text; ALTER TABLE chat_questions ADD COLUMN candidate_sha text; ALTER TABLE chat_tasks ADD COLUMN mac_needs text; ALTER TABLE runners ADD COLUMN credential_held_at datetime; ALTER TABLE chat_usage ADD COLUMN runner_id text; -- Dynamic specialists slice 1 (protocol 38): Design as a data specialist. -- Team records, the same for every member. Nothing an existing row holds -- changes meaning: a specialist from before is the team's (project_id ""), -- a task has no maker and no Design runs, a step is bound to no data -- specialist, and a task file is a Research one. ALTER TABLE chat_specialists ADD COLUMN project_id text NOT NULL DEFAULT ""; -- One open proposal and one active row per profile and scope (the team, -- or one project); declined and revoked rows are history. CREATE UNIQUE INDEX chat_specialist_open ON chat_specialists(profile, project_id) WHERE state = 'proposed'; CREATE UNIQUE INDEX chat_specialist_active ON chat_specialists(profile, project_id) WHERE state = 'active'; -- The data specialist that makes the task's changes ("" is Engineering), -- and the Design attempts since the person last acted on the task. ALTER TABLE chat_tasks ADD COLUMN maker_specialist_id text NOT NULL DEFAULT ""; ALTER TABLE chat_tasks ADD COLUMN design_runs integer NOT NULL DEFAULT 0; -- The binding a step was dispatched under: the specialist, its revision, -- its scope and its granted capabilities (JSON); "" is none. ALTER TABLE chat_steps ADD COLUMN specialist_id text NOT NULL DEFAULT ""; ALTER TABLE chat_steps ADD COLUMN specialist_revision integer NOT NULL DEFAULT 0; ALTER TABLE chat_steps ADD COLUMN specialist_project text NOT NULL DEFAULT ""; ALTER TABLE chat_steps ADD COLUMN specialist_caps text NOT NULL DEFAULT ""; CREATE INDEX chat_step_specialist ON chat_steps(specialist_id); -- A task file is a Research image or a Design render (of one option, at -- one tree digest). ALTER TABLE chat_task_files ADD COLUMN source text NOT NULL DEFAULT "research"; ALTER TABLE chat_task_files ADD COLUMN option text NOT NULL DEFAULT ""; ALTER TABLE chat_task_files ADD COLUMN tree_digest text NOT NULL DEFAULT ""; -- Dynamic specialists slice 1: the coordinator's continuation after an -- owner's Add on a specialist card for one project. One turn per card -- (the card is the key), bound to that card's specialist and project, the -- proposing turn's messages it carries (words, JSON) and the one task it -- records (task_id, "" until then). A team record; no existing row -- changes. CREATE TABLE chat_specialist_turns (card_id text PRIMARY KEY, turn_id text, person text, specialist_id text, project_id text, asked_turn text, words text NOT NULL DEFAULT "", task_id text NOT NULL DEFAULT "", created_at datetime); CREATE UNIQUE INDEX chat_specialist_turn ON chat_specialist_turns(turn_id); -- Dynamic specialists slice 1 (protocol 38): the options a done Design -- attempt's final report named, kept with its captures once the report -- validated them (JSON: the attempt, and each option's name and capture -- ids in the report's order); "" is none. A team record, as its step; -- no existing row changes. ALTER TABLE chat_steps ADD COLUMN design_options text NOT NULL DEFAULT ""; -- Dynamic specialists slice 1 (protocol 38): a Design attempt's render -- records (spec section 4, Audit: each render's counts and timing on the -- step), as its reports carried them: one row per step attempt, the -- records as JSON (wire.RenderAudit), plain data. A team record, as its -- step; no existing row changes. CREATE TABLE chat_step_renders (step_id text, attempt integer, renders text NOT NULL DEFAULT "", updated_at datetime, PRIMARY KEY (step_id, attempt));