From 0dc0dad22a5bbc2cef70066806c47399a31a0f4c Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 1 Oct 2026 20:26:15 +0900 Subject: [PATCH] refactor: squash DB migrations 1-22 into a single baseline schema base-schema.ts now holds the full schema at version 22, taken from the production database (column order, indexes, provenance triggers), so a fresh database is identical to the live one. Previously a fresh DB differed: it had an extra idx_messages_chat_timestamp index and a different column order in paired_tasks and paired_turn_outputs. The migration runner keeps schema_migrations: - empty DB: apply the baseline and record version 22 ('baseline') - existing DB: must already be at version >= 22 (the live DB records 1-22), otherwise startup refuses it without touching it - later migrations (23+) run once each inside a transaction Removed the 22 historical migrations, the legacy schema bundle, the paired-turn provenance rebuild/backfill helpers that migration 10 replayed on every start (all no-ops on the live DB), legacy session migration helpers, the unused service handoff metadata helpers, and the tests that only exercised legacy-schema upgrades. New schema tests check a fresh DB against a snapshot dumped from production and that reopening a DB at the baseline changes neither schema nor data. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 1 + quality/code-quality-budgets.json | 9 - setup/migrate-room-registrations.test.ts | 17 +- src/db-message-source.test.ts | 73 +- src/db.test.ts | 3895 +---------------- src/db/base-schema.ts | 1338 ++++-- src/db/bootstrap.test.ts | 182 - src/db/bootstrap.ts | 27 +- src/db/canonical-role-metadata.ts | 195 +- src/db/migrations/001_legacy-schema-bundle.ts | 67 - .../migrations/002_scheduled-task-columns.ts | 41 - ...003_room-registration-canonical-columns.ts | 72 - .../004_message-seq-and-work-item-indexes.ts | 36 - .../migrations/005_sessions-composite-key.ts | 17 - .../migrations/006_chat-channel-metadata.ts | 26 - .../007_runtime-service-metadata.ts | 509 --- .../008_paired-task-schema-cleanup.ts | 124 - ...paired-workspace-project-schema-cleanup.ts | 54 - .../010_paired-turn-provenance-upgrade.ts | 193 - src/db/migrations/011_owner-failure-count.ts | 23 - .../012_paired-verdict-and-step-telemetry.ts | 75 - src/db/migrations/013_message-source-kind.ts | 43 - .../migrations/014_work-item-attachments.ts | 17 - src/db/migrations/015_turn-progress-text.ts | 19 - src/db/migrations/016_room-skill-overrides.ts | 29 - .../017_scheduled-task-room-role.ts | 13 - .../018_paired-turn-output-attachments.ts | 20 - .../019_turn-progress-text-recovery.ts | 36 - .../020_arbiter-intervention-count.ts | 25 - .../migrations/021_reviewer-failure-count.ts | 23 - .../022_turn-progress-text-compat.ts | 32 - src/db/migrations/helpers.ts | 64 - src/db/migrations/index.ts | 148 +- src/db/migrations/types.ts | 12 - src/db/paired-turn-attempts.ts | 145 - src/db/paired-turn-provenance-schema.ts | 2061 --------- src/db/schema.snapshot.sql | 68 + src/db/schema.test.ts | 264 ++ src/db/schema.ts | 25 - src/db/sessions.ts | 100 - src/paired-follow-up-scheduler.test.ts | 144 +- 41 files changed, 1321 insertions(+), 8941 deletions(-) delete mode 100644 src/db/bootstrap.test.ts delete mode 100644 src/db/migrations/001_legacy-schema-bundle.ts delete mode 100644 src/db/migrations/002_scheduled-task-columns.ts delete mode 100644 src/db/migrations/003_room-registration-canonical-columns.ts delete mode 100644 src/db/migrations/004_message-seq-and-work-item-indexes.ts delete mode 100644 src/db/migrations/005_sessions-composite-key.ts delete mode 100644 src/db/migrations/006_chat-channel-metadata.ts delete mode 100644 src/db/migrations/007_runtime-service-metadata.ts delete mode 100644 src/db/migrations/008_paired-task-schema-cleanup.ts delete mode 100644 src/db/migrations/009_paired-workspace-project-schema-cleanup.ts delete mode 100644 src/db/migrations/010_paired-turn-provenance-upgrade.ts delete mode 100644 src/db/migrations/011_owner-failure-count.ts delete mode 100644 src/db/migrations/012_paired-verdict-and-step-telemetry.ts delete mode 100644 src/db/migrations/013_message-source-kind.ts delete mode 100644 src/db/migrations/014_work-item-attachments.ts delete mode 100644 src/db/migrations/015_turn-progress-text.ts delete mode 100644 src/db/migrations/016_room-skill-overrides.ts delete mode 100644 src/db/migrations/017_scheduled-task-room-role.ts delete mode 100644 src/db/migrations/018_paired-turn-output-attachments.ts delete mode 100644 src/db/migrations/019_turn-progress-text-recovery.ts delete mode 100644 src/db/migrations/020_arbiter-intervention-count.ts delete mode 100644 src/db/migrations/021_reviewer-failure-count.ts delete mode 100644 src/db/migrations/022_turn-progress-text-compat.ts delete mode 100644 src/db/migrations/helpers.ts delete mode 100644 src/db/migrations/types.ts delete mode 100644 src/db/paired-turn-provenance-schema.ts create mode 100644 src/db/schema.snapshot.sql create mode 100644 src/db/schema.test.ts delete mode 100644 src/db/schema.ts diff --git a/docs/architecture.md b/docs/architecture.md index a0d33c0..3da08e4 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -99,6 +99,7 @@ Discord ──► SQLite (WAL) ──► GroupQueue ──┬──► Owner (ho - reviewer / arbiter가 직접 로컬 빌드를 못 돌려도, host verification 경로로 `typecheck`, `test`, `build`를 수행할 수 있음 - startup precondition은 전용 오류로 올리고, setup이 생성하는 유닛은 `RestartPreventExitStatus=78`로 crash loop를 막음(현재 운영 호스트의 `ejclaw.service`는 수동 작성 유닛으로 `bun src/index.ts`를 실행하며 이 줄이 없고, 로그는 journald로 감) - deploy는 `migrate-room-registrations`를 선행한 뒤 service restart를 수행 +- DB 스키마: 과거 마이그레이션 1~22는 `src/db/base-schema.ts`(기준 버전 22) 하나로 합쳐짐. 새 DB는 이 스키마로 만들고, 기존 DB는 `schema_migrations` 최대 버전이 22 이상이어야 열림(그보다 오래된 DB는 시작 시 거부). 스키마 변경은 `src/db/migrations/index.ts`에 23번부터 마이그레이션을 추가하고 `src/db/schema.snapshot.sql`을 갱신 ## 주요 파일 diff --git a/quality/code-quality-budgets.json b/quality/code-quality-budgets.json index d70aab2..f6852d1 100644 --- a/quality/code-quality-budgets.json +++ b/quality/code-quality-budgets.json @@ -48,20 +48,11 @@ "maxFunctionLines": 277, "owner": "codex warmup test hotspot" }, - "src/db/base-schema.ts": { - "maxFunctionLines": 436, - "owner": "db schema hotspot" - }, "src/db/paired-state.ts": { "maxLines": 951, "maxFunctionLines": 217, "owner": "paired db state hotspot" }, - "src/db/paired-turn-provenance-schema.ts": { - "maxLines": 2062, - "maxFunctionLines": 343, - "owner": "paired turn provenance schema hotspot" - }, "src/db.test.ts": { "maxLines": 9380, "maxFunctionLines": 4990, diff --git a/setup/migrate-room-registrations.test.ts b/setup/migrate-room-registrations.test.ts index 45c1303..a2eab87 100644 --- a/setup/migrate-room-registrations.test.ts +++ b/setup/migrate-room-registrations.test.ts @@ -5,6 +5,8 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; import { Database } from 'bun:sqlite'; +import { applySchemaMigrations } from '../src/db/migrations/index.js'; + describe('migrate room registrations step', () => { const tempRoots: string[] = []; @@ -31,6 +33,7 @@ describe('migrate room registrations step', () => { const dbPath = path.join(storeDir, 'messages.db'); const legacyDb = new Database(dbPath); + applySchemaMigrations(legacyDb); legacyDb.exec(` CREATE TABLE registered_groups ( jid TEXT NOT NULL, @@ -218,20 +221,8 @@ describe('migrate room registrations step', () => { const dbPath = path.join(storeDir, 'messages.db'); const legacyDb = new Database(dbPath); + applySchemaMigrations(legacyDb); legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL, - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT NOT NULL - ); CREATE TABLE registered_groups ( jid TEXT NOT NULL, name TEXT NOT NULL, diff --git a/src/db-message-source.test.ts b/src/db-message-source.test.ts index 929e733..2ad5dbe 100644 --- a/src/db-message-source.test.ts +++ b/src/db-message-source.test.ts @@ -1,13 +1,7 @@ -import { Database } from 'bun:sqlite'; -import fs from 'fs'; -import os from 'os'; -import path from 'path'; - -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { beforeEach, describe, expect, it } from 'vitest'; import { _initTestDatabase, - _initTestDatabaseFromFile, getMessagesSinceSeq, getRecentChatMessages, storeChatMetadata, @@ -15,19 +9,10 @@ import { } from './db.js'; describe('message_source_kind persistence', () => { - let tempDir: string | null = null; - beforeEach(() => { _initTestDatabase(); }); - afterEach(() => { - if (tempDir) { - fs.rmSync(tempDir, { recursive: true, force: true }); - tempDir = null; - } - }); - it('persists explicit message source kind through store and read paths', () => { storeChatMetadata('room@g.us', '2026-04-24T00:00:00.000Z', 'Room'); storeMessage({ @@ -98,60 +83,4 @@ describe('message_source_kind persistence', () => { expect.objectContaining({ id: 'bot-1', message_source_kind: 'bot' }), ]); }); - - it('migrates legacy message tables and backfills source kind', () => { - tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-msg-source-')); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - legacyDb.exec(` - CREATE TABLE chats ( - jid TEXT PRIMARY KEY, - name TEXT, - last_message_time TEXT, - channel TEXT, - is_group INTEGER DEFAULT 0 - ); - CREATE TABLE messages ( - id TEXT, - chat_jid TEXT, - sender TEXT, - sender_name TEXT, - content TEXT, - timestamp TEXT, - seq INTEGER, - is_from_me INTEGER, - is_bot_message INTEGER DEFAULT 0, - PRIMARY KEY (id, chat_jid) - ); - CREATE TABLE schema_migrations ( - version INTEGER PRIMARY KEY, - name TEXT NOT NULL, - applied_at TEXT NOT NULL DEFAULT (datetime('now')) - ); - INSERT INTO chats (jid, name, last_message_time, channel, is_group) - VALUES ('room@g.us', 'Room', '2026-04-24T00:00:02.000Z', 'discord', 1); - INSERT INTO messages ( - id, chat_jid, sender, sender_name, content, timestamp, seq, is_from_me, is_bot_message - ) VALUES - ('human-legacy', 'room@g.us', 'user', 'User', 'hello', '2026-04-24T00:00:01.000Z', 1, 0, 0), - ('bot-legacy', 'room@g.us', 'bot', 'Bot', 'done', '2026-04-24T00:00:02.000Z', 2, 0, 1); - `); - const insertMigration = legacyDb.prepare( - 'INSERT INTO schema_migrations (version, name) VALUES (?, ?)', - ); - for (let version = 1; version <= 12; version += 1) { - insertMigration.run(version, `legacy-${version}`); - } - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect(getRecentChatMessages('room@g.us', 2)).toEqual([ - expect.objectContaining({ - id: 'human-legacy', - message_source_kind: 'human', - }), - expect.objectContaining({ id: 'bot-legacy', message_source_kind: 'bot' }), - ]); - }); }); diff --git a/src/db.test.ts b/src/db.test.ts index 87e8c64..5bf6f67 100644 --- a/src/db.test.ts +++ b/src/db.test.ts @@ -87,7 +87,6 @@ import { } from './db/room-registration.js'; import { buildPairedTurnIdentity } from './paired-turn-identity.js'; import { - ARBITER_AGENT_TYPE, CLAUDE_SERVICE_ID, CODEX_MAIN_SERVICE_ID, CODEX_REVIEW_SERVICE_ID, @@ -814,82 +813,6 @@ describe('session accessors', () => { deleteSession('group-a'); expect(getSession('group-a')).toBeUndefined(); }); - - it('migrates legacy sessions table rows into the composite primary key schema', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-session-schema-migration-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE sessions ( - group_folder TEXT PRIMARY KEY, - session_id TEXT NOT NULL - ); - `); - legacyDb - .prepare( - `INSERT INTO sessions (group_folder, session_id) - VALUES (?, ?)`, - ) - .run('group-legacy-schema', 'legacy-schema-session-123'); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect(getSession('group-legacy-schema', 'claude-code')).toBe( - 'legacy-schema-session-123', - ); - - const migratedDb = new Database(dbPath, { readonly: true }); - const sessionColumns = migratedDb - .prepare(`PRAGMA table_info(sessions)`) - .all() as Array<{ name: string }>; - expect(sessionColumns.some((col) => col.name === 'agent_type')).toBe(true); - migratedDb.close(); - }); - - it('backfills legacy service_sessions rows into sessions during init', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-service-sessions-backfill-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE sessions ( - group_folder TEXT PRIMARY KEY, - session_id TEXT NOT NULL - ); - CREATE TABLE service_sessions ( - group_folder TEXT NOT NULL, - service_id TEXT NOT NULL, - session_id TEXT NOT NULL, - PRIMARY KEY (group_folder, service_id) - ); - `); - legacyDb - .prepare( - `INSERT INTO service_sessions (group_folder, service_id, session_id) - VALUES (?, ?, ?)`, - ) - .run('group-legacy', CLAUDE_SERVICE_ID, 'legacy-service-session-123'); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect(getSession('group-legacy', 'claude-code')).toBe( - 'legacy-service-session-123', - ); - - const migratedDb = new Database(dbPath, { readonly: true }); - const hasServiceSessions = Boolean( - migratedDb - .prepare( - `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'service_sessions'`, - ) - .get(), - ); - expect(hasServiceSessions).toBe(false); - migratedDb.close(); - }); }); // --- storeChatMetadata --- @@ -1328,173 +1251,6 @@ describe('paired task state', () => { expect(outputs[0].created_at).toBe('2026-03-28T00:01:23.000Z'); }); - it('fails init when paired task agent and service metadata conflict', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-paired-task-shadow-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - - legacyDb - .prepare( - `INSERT INTO paired_tasks ( - id, - chat_jid, - group_folder, - owner_service_id, - reviewer_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - title, - source_ref, - plan_notes, - review_requested_at, - round_trip_count, - status, - arbiter_verdict, - arbiter_requested_at, - completion_reason, - created_at, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'paired-legacy-1', - 'dc:paired', - 'paired-room', - CODEX_REVIEW_SERVICE_ID, - CODEX_MAIN_SERVICE_ID, - 'codex', - 'claude-code', - 'codex', - null, - 'HEAD', - null, - null, - 0, - 'active', - null, - null, - null, - '2026-03-28T00:00:00.000Z', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /paired_tasks\(paired-legacy-1\): reviewer_agent_type conflicts with reviewer_service_id/, - ); - }); - - it('preserves raw legacy paired task service ids during init when failover created the task', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-paired-task-legacy-failover-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - - legacyDb - .prepare( - `INSERT INTO paired_tasks ( - id, - chat_jid, - group_folder, - owner_service_id, - reviewer_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - title, - source_ref, - plan_notes, - review_requested_at, - round_trip_count, - status, - arbiter_verdict, - arbiter_requested_at, - completion_reason, - created_at, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'paired-legacy-failover', - 'dc:paired-failover', - 'paired-failover-room', - CODEX_REVIEW_SERVICE_ID, - CLAUDE_SERVICE_ID, - null, - null, - null, - 'legacy failover task', - null, - null, - null, - 0, - 'active', - null, - null, - null, - '2026-03-28T00:00:00.000Z', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect(getPairedTaskById('paired-legacy-failover')).toMatchObject({ - owner_service_id: CODEX_REVIEW_SERVICE_ID, - reviewer_service_id: CLAUDE_SERVICE_ID, - owner_agent_type: 'codex', - reviewer_agent_type: 'claude-code', - }); - }); - it('backfills configured owner agent type when creating a paired task with a raw non-shadow owner service id', () => { createPairedTask({ id: 'paired-task-raw-owner-service', @@ -1523,197 +1279,6 @@ describe('paired task state', () => { }); }); - it('preserves raw legacy paired task service ids during init when registered group metadata is present', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-paired-task-legacy-groups-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE registered_groups ( - jid TEXT NOT NULL, - name TEXT NOT NULL, - folder TEXT NOT NULL, - trigger_pattern TEXT NOT NULL, - added_at TEXT NOT NULL, - agent_config TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - agent_type TEXT NOT NULL DEFAULT 'claude-code', - work_dir TEXT, - PRIMARY KEY (jid, agent_type), - UNIQUE (folder, agent_type) - ); - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - - const insertGroup = legacyDb.prepare( - `INSERT INTO registered_groups ( - jid, - name, - folder, - trigger_pattern, - added_at, - agent_config, - requires_trigger, - is_main, - agent_type, - work_dir - ) VALUES (?, ?, ?, ?, ?, NULL, 1, 0, ?, NULL)`, - ); - insertGroup.run( - 'dc:paired-failover-groups', - 'Legacy Failover Groups', - 'paired-failover-groups', - '@Claude', - '2024-01-01T00:00:00.000Z', - 'claude-code', - ); - insertGroup.run( - 'dc:paired-failover-groups', - 'Legacy Failover Groups', - 'paired-failover-groups', - '@Codex', - '2024-01-01T00:00:00.000Z', - 'codex', - ); - - legacyDb - .prepare( - `INSERT INTO paired_tasks ( - id, - chat_jid, - group_folder, - owner_service_id, - reviewer_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - title, - source_ref, - plan_notes, - review_requested_at, - round_trip_count, - status, - arbiter_verdict, - arbiter_requested_at, - completion_reason, - created_at, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'paired-legacy-groups', - 'dc:paired-failover-groups', - 'paired-failover-groups', - CODEX_REVIEW_SERVICE_ID, - CLAUDE_SERVICE_ID, - null, - null, - null, - 'legacy failover with groups', - null, - null, - null, - 0, - 'active', - null, - null, - null, - '2026-03-28T00:00:00.000Z', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - expect(migrateLegacyRoomRegistrationsInFile(dbPath)).toEqual({ - migratedRooms: 1, - migratedRoleOverrides: 2, - }); - _initTestDatabaseFromFile(dbPath); - - expect(getPairedTaskById('paired-legacy-groups')).toMatchObject({ - owner_service_id: CODEX_REVIEW_SERVICE_ID, - reviewer_service_id: CLAUDE_SERVICE_ID, - owner_agent_type: 'codex', - reviewer_agent_type: 'claude-code', - }); - }); - - it('preserves raw legacy channel owner lease service ids during init', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-channel-owner-legacy-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE channel_owner ( - chat_jid TEXT PRIMARY KEY, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT, - arbiter_service_id TEXT, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - activated_at TEXT, - reason TEXT - ); - `); - - legacyDb - .prepare( - `INSERT INTO channel_owner ( - chat_jid, - owner_service_id, - reviewer_service_id, - arbiter_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - activated_at, - reason - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:legacy-channel-owner', - CODEX_REVIEW_SERVICE_ID, - CLAUDE_SERVICE_ID, - null, - null, - null, - null, - '2026-03-28T00:00:00.000Z', - 'legacy-failover', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect(getChannelOwnerLease('dc:legacy-channel-owner')).toMatchObject({ - owner_service_id: CODEX_REVIEW_SERVICE_ID, - reviewer_service_id: CLAUDE_SERVICE_ID, - owner_agent_type: 'codex', - reviewer_agent_type: 'claude-code', - }); - }); - it('fails fast when a paired task row loses canonical agent metadata after init', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-paired-task-strict-read-'); const dbPath = path.join(tempDir, 'messages.db'); @@ -1765,66 +1330,6 @@ describe('paired task state', () => { } }); - it('preserves stored reviewer service ids during init even when reviewer agent metadata exists', () => { - const tempDir = fs.mkdtempSync( - '/tmp/ejclaw-channel-owner-stored-reviewer-', - ); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE channel_owner ( - chat_jid TEXT PRIMARY KEY, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT, - arbiter_service_id TEXT, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - activated_at TEXT, - reason TEXT - ); - `); - - legacyDb - .prepare( - `INSERT INTO channel_owner ( - chat_jid, - owner_service_id, - reviewer_service_id, - arbiter_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - activated_at, - reason - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:legacy-channel-owner-stored-reviewer', - CLAUDE_SERVICE_ID, - 'stale-reviewer-shadow', - null, - 'claude-code', - 'codex', - null, - '2026-03-28T00:00:00.000Z', - 'legacy-reviewer-stored-id', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect( - getChannelOwnerLease('dc:legacy-channel-owner-stored-reviewer'), - ).toMatchObject({ - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: 'stale-reviewer-shadow', - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - }); - }); - it('fails fast when a channel owner lease row loses canonical reviewer metadata after init', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-channel-owner-strict-read-'); const dbPath = path.join(tempDir, 'messages.db'); @@ -1862,434 +1367,6 @@ describe('paired task state', () => { _initTestDatabase(); } }); - - it('fails init when stored paired task metadata conflicts with stored service ids even if room settings differ', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-paired-task-ssot-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL, - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT NOT NULL - ); - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - - legacyDb - .prepare( - `INSERT INTO room_settings ( - chat_jid, - room_mode, - mode_source, - owner_agent_type, - updated_at - ) VALUES (?, ?, ?, ?, ?)`, - ) - .run( - 'dc:task-ssot', - 'tribunal', - 'explicit', - 'codex', - '2026-03-28T00:00:00.000Z', - ); - - legacyDb - .prepare( - `INSERT INTO paired_tasks ( - id, - chat_jid, - group_folder, - owner_service_id, - reviewer_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - title, - source_ref, - plan_notes, - review_requested_at, - round_trip_count, - status, - arbiter_verdict, - arbiter_requested_at, - completion_reason, - created_at, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'paired-task-ssot', - 'dc:task-ssot', - 'task-ssot-room', - CODEX_REVIEW_SERVICE_ID, - CLAUDE_SERVICE_ID, - 'claude-code', - 'codex', - 'codex', - null, - 'HEAD', - null, - null, - 0, - 'active', - null, - null, - null, - '2026-03-28T00:00:00.000Z', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /paired_tasks\(paired-task-ssot\): owner_agent_type conflicts with owner_service_id/, - ); - }); - - it('preserves explicit room trigger during init without rewriting task agent metadata from room settings', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-room-settings-ssot-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL, - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT NOT NULL - ); - CREATE TABLE registered_groups ( - jid TEXT NOT NULL, - name TEXT NOT NULL, - folder TEXT NOT NULL, - trigger_pattern TEXT NOT NULL, - added_at TEXT NOT NULL, - agent_config TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - agent_type TEXT NOT NULL DEFAULT 'claude-code', - work_dir TEXT, - PRIMARY KEY (jid, agent_type), - UNIQUE (folder, agent_type) - ); - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - - legacyDb - .prepare( - `INSERT INTO room_settings ( - chat_jid, - room_mode, - mode_source, - name, - folder, - trigger_pattern, - requires_trigger, - is_main, - owner_agent_type, - work_dir, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:explicit-owner', - 'tribunal', - 'explicit', - 'Explicit Owner Room', - 'explicit-owner-room', - '@Custom', - 1, - 0, - 'claude-code', - null, - '2026-03-28T00:00:00.000Z', - ); - - const insertGroup = legacyDb.prepare( - `INSERT INTO registered_groups ( - jid, - name, - folder, - trigger_pattern, - added_at, - agent_config, - requires_trigger, - is_main, - agent_type, - work_dir - ) VALUES (?, ?, ?, ?, ?, NULL, 1, 0, ?, NULL)`, - ); - insertGroup.run( - 'dc:explicit-owner', - 'Explicit Owner Room', - 'explicit-owner-room', - '@Claude', - '2024-01-01T00:00:00.000Z', - 'claude-code', - ); - insertGroup.run( - 'dc:explicit-owner', - 'Explicit Owner Room', - 'explicit-owner-room', - '@Codex', - '2024-01-01T00:00:00.000Z', - 'codex', - ); - - legacyDb - .prepare( - `INSERT INTO paired_tasks ( - id, - chat_jid, - group_folder, - owner_service_id, - reviewer_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type, - title, - source_ref, - plan_notes, - review_requested_at, - round_trip_count, - status, - arbiter_verdict, - arbiter_requested_at, - completion_reason, - created_at, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'paired-explicit-owner', - 'dc:explicit-owner', - 'explicit-owner-room', - CODEX_REVIEW_SERVICE_ID, - CODEX_MAIN_SERVICE_ID, - null, - null, - null, - null, - 'HEAD', - null, - null, - 0, - 'active', - null, - null, - null, - '2026-03-28T00:00:00.000Z', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - expect(migrateLegacyRoomRegistrationsInFile(dbPath)).toEqual({ - migratedRooms: 0, - migratedRoleOverrides: 2, - }); - _initTestDatabaseFromFile(dbPath); - - expect(getStoredRoomSettings('dc:explicit-owner')).toMatchObject({ - roomMode: 'tribunal', - modeSource: 'explicit', - ownerAgentType: 'claude-code', - trigger: '@Custom', - }); - expect(getPairedTaskById('paired-explicit-owner')).toMatchObject({ - owner_service_id: CODEX_REVIEW_SERVICE_ID, - reviewer_service_id: CODEX_MAIN_SERVICE_ID, - owner_agent_type: 'codex', - reviewer_agent_type: 'codex', - arbiter_agent_type: ARBITER_AGENT_TYPE ?? null, - }); - }); - - it('preserves explicit room trigger during init even when legacy explicit rows lack owner agent type', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-room-settings-trigger-ssot-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL, - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT NOT NULL - ); - CREATE TABLE registered_groups ( - jid TEXT NOT NULL, - name TEXT NOT NULL, - folder TEXT NOT NULL, - trigger_pattern TEXT NOT NULL, - added_at TEXT NOT NULL, - agent_config TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - agent_type TEXT NOT NULL DEFAULT 'claude-code', - work_dir TEXT, - PRIMARY KEY (jid, agent_type), - UNIQUE (folder, agent_type) - ); - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - - legacyDb - .prepare( - `INSERT INTO room_settings ( - chat_jid, - room_mode, - mode_source, - name, - folder, - trigger_pattern, - requires_trigger, - is_main, - owner_agent_type, - work_dir, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:explicit-trigger-only', - 'tribunal', - 'explicit', - 'Explicit Trigger Room', - 'explicit-trigger-room', - '@Custom', - 1, - 0, - null, - null, - '2026-03-28T00:00:00.000Z', - ); - - const insertGroup = legacyDb.prepare( - `INSERT INTO registered_groups ( - jid, - name, - folder, - trigger_pattern, - added_at, - agent_config, - requires_trigger, - is_main, - agent_type, - work_dir - ) VALUES (?, ?, ?, ?, ?, NULL, 1, 0, ?, NULL)`, - ); - insertGroup.run( - 'dc:explicit-trigger-only', - 'Explicit Trigger Room', - 'explicit-trigger-room', - '@Claude', - '2024-01-01T00:00:00.000Z', - 'claude-code', - ); - insertGroup.run( - 'dc:explicit-trigger-only', - 'Explicit Trigger Room', - 'explicit-trigger-room', - '@Codex', - '2024-01-01T00:00:00.000Z', - 'codex', - ); - legacyDb.close(); - - expect(migrateLegacyRoomRegistrationsInFile(dbPath)).toEqual({ - migratedRooms: 0, - migratedRoleOverrides: 2, - }); - _initTestDatabaseFromFile(dbPath); - - expect(getStoredRoomSettings('dc:explicit-trigger-only')).toMatchObject({ - roomMode: 'tribunal', - modeSource: 'explicit', - trigger: '@Custom', - }); - }); }); // --- LIMIT behavior --- @@ -2605,6 +1682,7 @@ describe('room assignment writes', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-legacy-room-settings-init-'); const dbPath = path.join(tempDir, 'messages.db'); const legacyDb = new Database(dbPath); + initializeDatabaseSchema(legacyDb); legacyDb.exec(` CREATE TABLE registered_groups ( @@ -2713,6 +1791,7 @@ describe('room assignment writes', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-legacy-room-conflict-init-'); const dbPath = path.join(tempDir, 'messages.db'); const legacyDb = new Database(dbPath); + initializeDatabaseSchema(legacyDb); legacyDb.exec(` CREATE TABLE registered_groups ( @@ -2775,32 +1854,9 @@ describe('room assignment writes', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-legacy-room-mixed-conflict-'); const dbPath = path.join(tempDir, 'messages.db'); const legacyDb = new Database(dbPath); + initializeDatabaseSchema(legacyDb); legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL DEFAULT 'single', - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER, - is_main INTEGER, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT - ); - - CREATE TABLE room_role_overrides ( - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - agent_type TEXT NOT NULL, - agent_config_json TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - PRIMARY KEY (chat_jid, role) - ); - CREATE TABLE registered_groups ( jid TEXT NOT NULL, name TEXT NOT NULL, @@ -2938,22 +1994,9 @@ describe('room assignment writes', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-room-ssot-'); const dbPath = path.join(tempDir, 'messages.db'); const legacyDb = new Database(dbPath); + initializeDatabaseSchema(legacyDb); legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL DEFAULT 'single', - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER, - is_main INTEGER, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT - ); - CREATE TABLE registered_groups ( jid TEXT NOT NULL, name TEXT NOT NULL, @@ -3076,22 +2119,9 @@ describe('room assignment writes', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-room-writeback-'); const dbPath = path.join(tempDir, 'messages.db'); const legacyDb = new Database(dbPath); + initializeDatabaseSchema(legacyDb); legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL DEFAULT 'single', - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER, - is_main INTEGER, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT - ); - CREATE TABLE registered_groups ( jid TEXT NOT NULL, name TEXT NOT NULL, @@ -3445,263 +2475,6 @@ describe('paired room registration', () => { }); describe('legacy service handoff rows', () => { - it('preserves stored owner handoff service ids during init when service id columns already exist', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-handoff-shadow-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL, - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - owner_agent_type TEXT, - work_dir TEXT, - updated_at TEXT NOT NULL - ); - CREATE TABLE service_handoffs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - source_role TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT - ); - `); - - legacyDb - .prepare( - `INSERT INTO room_settings ( - chat_jid, - room_mode, - mode_source, - name, - folder, - trigger_pattern, - requires_trigger, - is_main, - owner_agent_type, - work_dir, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:handoff-owner-shadow', - 'tribunal', - 'explicit', - 'Owner Handoff Shadow', - 'owner-handoff-shadow', - '@Owner', - 1, - 0, - 'codex', - null, - '2026-03-28T00:00:00.000Z', - ); - - legacyDb - .prepare( - `INSERT INTO service_handoffs ( - chat_jid, - group_folder, - source_service_id, - target_service_id, - source_role, - target_role, - target_agent_type, - prompt, - status, - reason, - intended_role, - created_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:handoff-owner-shadow', - 'owner-handoff-shadow', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'owner', - 'codex', - 'owner fallback', - 'pending', - 'claude-usage-exhausted', - 'owner', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - const rawDb = new Database(dbPath); - const storedHandoffs = rawDb - .prepare( - `SELECT chat_jid, source_service_id, target_service_id, source_role, target_role - FROM service_handoffs`, - ) - .all(); - rawDb.close(); - expect(storedHandoffs).toEqual([ - expect.objectContaining({ - chat_jid: 'dc:handoff-owner-shadow', - source_service_id: CLAUDE_SERVICE_ID, - target_service_id: CODEX_REVIEW_SERVICE_ID, - source_role: 'owner', - target_role: 'owner', - }), - ]); - }); - - it('fails startup when stored handoff agent metadata conflicts with service ids', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-handoff-metadata-conflict-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE service_handoffs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - source_role TEXT, - source_agent_type TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT - ); - `); - legacyDb - .prepare( - `INSERT INTO service_handoffs ( - chat_jid, - group_folder, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:handoff-conflict', - 'handoff-conflict', - CODEX_REVIEW_SERVICE_ID, - CLAUDE_SERVICE_ID, - 'reviewer', - 'claude-code', - 'reviewer', - 'codex', - 'conflicting handoff metadata', - 'pending', - 'reviewer', - '2026-04-10T00:00:00.000Z', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /source_agent_type conflicts with source_service_id/, - ); - }); - - it('fails startup when stored handoff role metadata conflicts with service shadows', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-handoff-role-shadow-conflict-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE service_handoffs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - source_role TEXT, - source_agent_type TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT - ); - `); - legacyDb - .prepare( - `INSERT INTO service_handoffs ( - chat_jid, - group_folder, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'dc:handoff-role-conflict', - 'handoff-role-conflict', - CLAUDE_SERVICE_ID, - CODEX_MAIN_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'conflicting handoff role shadow', - 'pending', - 'reviewer', - '2026-04-10T00:00:00.000Z', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /target_role conflicts with target_service_id/, - ); - }); - it('records queued and running logical turn state across reservation and lease claims', () => { const task: PairedTask = { id: 'task-paired-turn-state', @@ -3923,301 +2696,6 @@ describe('legacy service handoff rows', () => { } }); - it('drops legacy paired_turn active_run_id scratch state on re-init and keeps same-run continuation on attempt rows', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-current-run-write-drift-'); - const dbPath = path.join(tempDir, 'messages.db'); - - const turnIdentity = buildPairedTurnIdentity({ - taskId: 'task-current-run-drift', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - intentKind: 'reviewer-turn', - role: 'reviewer', - }); - - try { - _initTestDatabaseFromFile(dbPath); - - markPairedTurnRunning({ - turnIdentity, - executorServiceId: CODEX_REVIEW_SERVICE_ID, - executorAgentType: 'codex', - runId: 'run-current-run-drift-1', - }); - - const rawDb = new Database(dbPath); - rawDb.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - `); - rawDb.exec(`ALTER TABLE paired_turns ADD COLUMN active_run_id TEXT`); - rawDb - .prepare( - ` - UPDATE paired_turns - SET active_run_id = ? - WHERE turn_id = ? - `, - ) - .run('stale-run-id', turnIdentity.turnId); - rawDb.close(); - - _initTestDatabaseFromFile(dbPath); - - const rebuiltDb = new Database(dbPath); - const pairedTurnColumns = rebuiltDb - .prepare(`PRAGMA table_info(paired_turns)`) - .all() as Array<{ name: string }>; - rebuiltDb.close(); - - expect( - pairedTurnColumns.some((column) => column.name === 'active_run_id'), - ).toBe(false); - - markPairedTurnRunning({ - turnIdentity, - executorServiceId: CODEX_REVIEW_SERVICE_ID, - executorAgentType: 'codex', - runId: 'run-current-run-drift-1', - }); - - expect(getPairedTurnById(turnIdentity.turnId)).toMatchObject({ - state: 'running', - attempt_no: 1, - }); - expect(getPairedTurnAttempts(turnIdentity.turnId)).toMatchObject([ - { - attempt_no: 1, - state: 'running', - executor_service_id: CODEX_REVIEW_SERVICE_ID, - executor_agent_type: 'codex', - active_run_id: 'run-current-run-drift-1', - }, - ]); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('backfills running attempt active_run_id from lease provenance before legacy paired_turn scratch on re-init', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-current-run-lease-backfill-'); - const dbPath = path.join(tempDir, 'messages.db'); - - const turnIdentity = buildPairedTurnIdentity({ - taskId: 'task-current-run-lease-backfill', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - intentKind: 'reviewer-turn', - role: 'reviewer', - }); - - try { - _initTestDatabaseFromFile(dbPath); - - markPairedTurnRunning({ - turnIdentity, - executorServiceId: CODEX_REVIEW_SERVICE_ID, - executorAgentType: 'codex', - runId: 'run-correct', - }); - - const rawDb = new Database(dbPath); - rawDb.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - `); - rawDb.exec(`ALTER TABLE paired_turns ADD COLUMN active_run_id TEXT`); - rawDb - .prepare( - ` - UPDATE paired_turn_attempts - SET active_run_id = NULL - WHERE turn_id = ? - AND attempt_no = 1 - `, - ) - .run(turnIdentity.turnId); - rawDb - .prepare( - ` - UPDATE paired_turns - SET active_run_id = ? - WHERE turn_id = ? - `, - ) - .run('run-stale', turnIdentity.turnId); - rawDb - .prepare( - ` - INSERT OR REPLACE INTO paired_task_execution_leases ( - task_id, - chat_jid, - role, - turn_id, - turn_attempt_id, - turn_attempt_no, - intent_kind, - claimed_run_id, - claimed_service_id, - task_status, - task_updated_at, - claimed_at, - updated_at, - expires_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - turnIdentity.taskId, - 'dc:current-run-lease-backfill', - 'reviewer', - turnIdentity.turnId, - buildPairedTurnAttemptId(turnIdentity.turnId, 1), - 1, - turnIdentity.intentKind, - 'run-correct', - CODEX_REVIEW_SERVICE_ID, - 'review_ready', - turnIdentity.taskUpdatedAt, - '2026-04-10T00:00:05.000Z', - '2026-04-10T00:00:10.000Z', - '2026-04-10T01:00:00.000Z', - ); - rawDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect(getPairedTurnAttempts(turnIdentity.turnId)).toMatchObject([ - { - attempt_no: 1, - state: 'running', - executor_service_id: CODEX_REVIEW_SERVICE_ID, - executor_agent_type: 'codex', - active_run_id: 'run-correct', - }, - ]); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('keeps attempt 1 when same-run continuation follows lease-backed active_run_id after re-init', () => { - const tempDir = fs.mkdtempSync( - '/tmp/ejclaw-current-run-lease-continuation-', - ); - const dbPath = path.join(tempDir, 'messages.db'); - - const turnIdentity = buildPairedTurnIdentity({ - taskId: 'task-current-run-lease-continuation', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - intentKind: 'reviewer-turn', - role: 'reviewer', - }); - - try { - _initTestDatabaseFromFile(dbPath); - - markPairedTurnRunning({ - turnIdentity, - executorServiceId: CODEX_REVIEW_SERVICE_ID, - executorAgentType: 'codex', - runId: 'run-correct', - }); - - const rawDb = new Database(dbPath); - rawDb.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - `); - rawDb.exec(`ALTER TABLE paired_turns ADD COLUMN active_run_id TEXT`); - rawDb - .prepare( - ` - UPDATE paired_turn_attempts - SET active_run_id = NULL - WHERE turn_id = ? - AND attempt_no = 1 - `, - ) - .run(turnIdentity.turnId); - rawDb - .prepare( - ` - UPDATE paired_turns - SET active_run_id = ? - WHERE turn_id = ? - `, - ) - .run('run-stale', turnIdentity.turnId); - rawDb - .prepare( - ` - INSERT OR REPLACE INTO paired_task_execution_leases ( - task_id, - chat_jid, - role, - turn_id, - turn_attempt_id, - turn_attempt_no, - intent_kind, - claimed_run_id, - claimed_service_id, - task_status, - task_updated_at, - claimed_at, - updated_at, - expires_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - turnIdentity.taskId, - 'dc:current-run-lease-continuation', - 'reviewer', - turnIdentity.turnId, - buildPairedTurnAttemptId(turnIdentity.turnId, 1), - 1, - turnIdentity.intentKind, - 'run-correct', - CODEX_REVIEW_SERVICE_ID, - 'review_ready', - turnIdentity.taskUpdatedAt, - '2026-04-10T00:00:05.000Z', - '2026-04-10T00:00:10.000Z', - '2026-04-10T01:00:00.000Z', - ); - rawDb.close(); - - _initTestDatabaseFromFile(dbPath); - - markPairedTurnRunning({ - turnIdentity, - executorServiceId: CODEX_REVIEW_SERVICE_ID, - executorAgentType: 'codex', - runId: 'run-correct', - }); - - expect(getPairedTurnById(turnIdentity.turnId)).toMatchObject({ - state: 'running', - attempt_no: 1, - }); - expect(getPairedTurnAttempts(turnIdentity.turnId)).toMatchObject([ - { - attempt_no: 1, - state: 'running', - executor_service_id: CODEX_REVIEW_SERVICE_ID, - executor_agent_type: 'codex', - active_run_id: 'run-correct', - }, - ]); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - it('keeps attempt 1 when paired_turn state drifts away from the current attempt row', () => { const tempDir = fs.mkdtempSync('/tmp/ejclaw-current-state-write-drift-'); const dbPath = path.join(tempDir, 'messages.db'); @@ -4474,76 +2952,6 @@ describe('legacy service handoff rows', () => { } }); - it('fails init when a legacy paired_turn aggregate implies a non-contiguous attempt lineage', () => { - const tempDir = fs.mkdtempSync(path.join('/tmp', 'ejclaw-paired-attempt-')); - const dbPath = path.join(tempDir, 'paired-attempts.db'); - - try { - const legacyDb = new Database(dbPath); - legacyDb.exec(` - CREATE TABLE paired_turns ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - attempt_no INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT - ); - `); - legacyDb - .prepare( - ` - INSERT INTO paired_turns ( - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - attempt_no, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'legacy-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 'legacy-task', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - 2, - '2026-04-10T00:00:00.000Z', - '2026-04-10T01:00:00.000Z', - '2026-04-10T01:00:00.000Z', - 'legacy attempt failure', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /must preserve contiguous parent lineage/, - ); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - it('records parent_attempt_id when a retry creates a new attempt', () => { const turnIdentity = buildPairedTurnIdentity({ taskId: 'parent-attempt-task', @@ -4586,944 +2994,6 @@ describe('legacy service handoff rows', () => { ]); }); - it('backfills parent_attempt_id for legacy multi-attempt rows during init', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-parent-attempt-backfill-'), - ); - const dbPath = path.join(tempDir, 'parent-attempt-backfill.db'); - const turnId = - 'legacy-parent-attempt:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - legacyDb.exec(` - CREATE TABLE paired_turns ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - active_run_id TEXT, - attempt_no INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT - ); - CREATE TABLE paired_turn_attempts ( - turn_id TEXT NOT NULL, - attempt_no INTEGER NOT NULL, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT, - PRIMARY KEY (turn_id, attempt_no) - ); - `); - legacyDb - .prepare( - ` - INSERT INTO paired_turns ( - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - attempt_no, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - turnId, - 'legacy-parent-attempt', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - 2, - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:02:40.000Z', - '2026-04-10T00:02:40.000Z', - 'attempt 2 failed', - ); - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - insertAttempt.run( - turnId, - 1, - 'legacy-parent-attempt', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'delegated', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:50.000Z', - '2026-04-10T00:01:50.000Z', - 'attempt 1 delegated', - ); - insertAttempt.run( - turnId, - 2, - 'legacy-parent-attempt', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:02:00.000Z', - '2026-04-10T00:02:40.000Z', - '2026-04-10T00:02:40.000Z', - 'attempt 2 failed', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - const rawDb = new Database(dbPath, { readonly: true }); - expect( - rawDb - .prepare( - ` - SELECT attempt_no, attempt_id, parent_attempt_id - FROM paired_turn_attempts - WHERE turn_id = ? - ORDER BY attempt_no ASC - `, - ) - .all(turnId), - ).toEqual([ - { - attempt_no: 1, - attempt_id: buildPairedTurnAttemptId(turnId, 1), - parent_attempt_id: null, - }, - { - attempt_no: 2, - attempt_id: buildPairedTurnAttemptId(turnId, 2), - parent_attempt_id: buildPairedTurnAttemptParentId(turnId, 2), - }, - ]); - rawDb.close(); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('fails init when legacy attempt lineage skips the previous attempt', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-parent-attempt-gap-'), - ); - const dbPath = path.join(tempDir, 'parent-attempt-gap.db'); - const turnId = 'legacy-parent-gap:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - legacyDb.exec(` - CREATE TABLE paired_turns ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - active_run_id TEXT, - attempt_no INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT - ); - CREATE TABLE paired_turn_attempts ( - turn_id TEXT NOT NULL, - attempt_no INTEGER NOT NULL, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT, - PRIMARY KEY (turn_id, attempt_no) - ); - `); - legacyDb - .prepare( - ` - INSERT INTO paired_turns ( - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - attempt_no, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - turnId, - 'legacy-parent-gap', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - 3, - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:03:00.000Z', - '2026-04-10T00:03:00.000Z', - 'attempt 3 failed', - ); - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - insertAttempt.run( - turnId, - 1, - 'legacy-parent-gap', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'delegated', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:30.000Z', - '2026-04-10T00:01:30.000Z', - 'attempt 1 delegated', - ); - insertAttempt.run( - turnId, - 3, - 'legacy-parent-gap', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:03:00.000Z', - '2026-04-10T00:03:20.000Z', - '2026-04-10T00:03:20.000Z', - 'attempt 3 failed', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /invalid parent_attempt_id provenance/, - ); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('backfills turn attempt provenance onto legacy reservations, leases, and handoffs during init', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-turn-attempt-provenance-'), - ); - const dbPath = path.join(tempDir, 'turn-attempt-provenance.db'); - - try { - const legacyDb = new Database(dbPath); - legacyDb.exec(` - CREATE TABLE paired_turns ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - attempt_no INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT - ); - CREATE TABLE paired_turn_attempts ( - turn_id TEXT NOT NULL, - attempt_no INTEGER NOT NULL, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT, - PRIMARY KEY (turn_id, attempt_no) - ); - CREATE TABLE paired_turn_reservations ( - chat_jid TEXT NOT NULL, - task_id TEXT NOT NULL, - task_status TEXT NOT NULL, - round_trip_count INTEGER NOT NULL DEFAULT 0, - task_updated_at TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - scheduled_run_id TEXT, - consumed_run_id TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - consumed_at TEXT, - PRIMARY KEY (chat_jid, task_id, task_updated_at, intent_kind) - ); - CREATE TABLE paired_task_execution_leases ( - task_id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - turn_id TEXT NOT NULL, - intent_kind TEXT NOT NULL, - claimed_run_id TEXT NOT NULL, - claimed_service_id TEXT NOT NULL, - task_status TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - claimed_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - expires_at TEXT NOT NULL - ); - CREATE TABLE service_handoffs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - paired_task_id TEXT, - paired_task_updated_at TEXT, - turn_id TEXT, - turn_intent_kind TEXT, - turn_role TEXT, - source_role TEXT, - source_agent_type TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT - ); - `); - legacyDb - .prepare( - ` - INSERT INTO paired_turns ( - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - attempt_no, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 'legacy-provenance-task', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'running', - 'other-service', - 'codex', - 2, - '2026-04-10T00:00:00.000Z', - '2026-04-10T01:00:00.000Z', - null, - null, - ); - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - insertAttempt.run( - 'legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 1, - 'legacy-provenance-task', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'delegated', - 'other-service', - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:30:00.000Z', - '2026-04-10T00:30:00.000Z', - 'legacy attempt 1 delegated', - ); - insertAttempt.run( - 'legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 2, - 'legacy-provenance-task', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'running', - 'other-service', - 'codex', - '2026-04-10T00:40:00.000Z', - '2026-04-10T01:00:00.000Z', - null, - null, - ); - legacyDb - .prepare( - ` - INSERT INTO paired_turn_reservations ( - chat_jid, - task_id, - task_status, - round_trip_count, - task_updated_at, - turn_id, - turn_role, - intent_kind, - status, - scheduled_run_id, - consumed_run_id, - created_at, - updated_at, - consumed_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'legacy-provenance-task', - 'review_ready', - 1, - '2026-04-10T00:00:00.000Z', - 'legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 'reviewer', - 'reviewer-turn', - 'completed', - 'run-scheduled', - 'run-consumed', - '2026-04-10T00:00:00.000Z', - '2026-04-10T01:00:00.000Z', - '2026-04-10T01:00:00.000Z', - ); - legacyDb - .prepare( - ` - INSERT INTO paired_task_execution_leases ( - task_id, - chat_jid, - role, - turn_id, - intent_kind, - claimed_run_id, - claimed_service_id, - task_status, - task_updated_at, - claimed_at, - updated_at, - expires_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'legacy-provenance-task', - 'group@test', - 'reviewer', - 'legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 'reviewer-turn', - 'run-active', - 'other-service', - 'review_ready', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:00:00.000Z', - '2026-04-10T01:00:00.000Z', - '2099-04-10T01:10:00.000Z', - ); - legacyDb - .prepare( - ` - INSERT INTO service_handoffs ( - chat_jid, - group_folder, - source_service_id, - target_service_id, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_intent_kind, - turn_role, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'test-group', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'legacy-provenance-task', - '2026-04-10T00:00:00.000Z', - 'legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn', - 'reviewer-turn', - 'reviewer', - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'legacy provenance handoff', - 'pending', - 'reviewer', - '2026-04-10T00:50:00.000Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - const rawDb = new Database(dbPath, { readonly: true }); - expect( - rawDb - .prepare( - `SELECT turn_attempt_no - FROM paired_turn_reservations - WHERE turn_id = ?`, - ) - .get('legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn'), - ).toEqual({ turn_attempt_no: 2 }); - expect( - rawDb - .prepare( - `SELECT turn_attempt_no - FROM paired_task_execution_leases - WHERE turn_id = ?`, - ) - .get('legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn'), - ).toEqual({ turn_attempt_no: 2 }); - expect( - rawDb - .prepare( - `SELECT turn_attempt_no - FROM service_handoffs - WHERE turn_id = ?`, - ) - .get('legacy-provenance-task:2026-04-10T00:00:00.000Z:reviewer-turn'), - ).toEqual({ turn_attempt_no: 2 }); - rawDb.close(); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('preserves per-row attempt provenance when backfilling a multi-attempt legacy turn', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-turn-attempt-provenance-multi-'), - ); - const dbPath = path.join(tempDir, 'turn-attempt-provenance-multi.db'); - const turnId = - 'legacy-multi-provenance:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - initializeDatabaseSchema(legacyDb); - - insertPairedTurnIdentityRow(legacyDb, { - turnId, - taskId: 'legacy-multi-provenance', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:01:00.000Z', - updatedAt: '2026-04-10T00:02:40.000Z', - }); - - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - attempt_id, - parent_attempt_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 1), - null, - turnId, - 1, - 'legacy-multi-provenance', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'delegated', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:50.000Z', - '2026-04-10T00:01:50.000Z', - 'attempt 1 delegated', - ); - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 2), - buildPairedTurnAttemptParentId(turnId, 2), - turnId, - 2, - 'legacy-multi-provenance', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:02:00.000Z', - '2026-04-10T00:02:40.000Z', - '2026-04-10T00:02:40.000Z', - 'attempt 2 failed', - ); - - legacyDb - .prepare( - ` - INSERT INTO paired_turn_reservations ( - chat_jid, - task_id, - task_status, - round_trip_count, - task_updated_at, - turn_id, - turn_attempt_no, - turn_role, - intent_kind, - status, - scheduled_run_id, - consumed_run_id, - created_at, - updated_at, - consumed_at - ) - VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'legacy-multi-provenance', - 'review_ready', - 1, - '2026-04-10T00:00:00.000Z', - turnId, - 'reviewer', - 'reviewer-turn', - 'completed', - 'run-scheduled-1', - 'run-consumed-1', - '2026-04-10T00:00:30.000Z', - '2026-04-10T00:01:10.000Z', - '2026-04-10T00:01:10.000Z', - ); - - legacyDb - .prepare( - ` - INSERT INTO paired_task_execution_leases ( - task_id, - chat_jid, - role, - turn_id, - turn_attempt_no, - intent_kind, - claimed_run_id, - claimed_service_id, - task_status, - task_updated_at, - claimed_at, - updated_at, - expires_at - ) - VALUES (?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'legacy-multi-provenance', - 'group@test', - 'reviewer', - turnId, - 'reviewer-turn', - 'run-active-1', - CODEX_REVIEW_SERVICE_ID, - 'review_ready', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:01:15.000Z', - '2026-04-10T00:01:20.000Z', - '2099-04-10T00:11:20.000Z', - ); - - const insertHandoff = legacyDb.prepare( - ` - INSERT INTO service_handoffs ( - chat_jid, - group_folder, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_attempt_no, - turn_intent_kind, - turn_role, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at, - claimed_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - insertHandoff.run( - 'group@test', - 'test-group', - 'legacy-multi-provenance', - '2026-04-10T00:00:00.000Z', - turnId, - 'reviewer-turn', - 'reviewer', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'legacy handoff attempt 1', - 'failed', - 'reviewer', - '2026-04-10T00:01:30.000Z', - '2026-04-10T00:01:35.000Z', - '2026-04-10T00:01:50.000Z', - 'attempt 1 failed', - ); - insertHandoff.run( - 'group@test', - 'test-group', - 'legacy-multi-provenance', - '2026-04-10T00:00:00.000Z', - turnId, - 'reviewer-turn', - 'reviewer', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'legacy handoff attempt 2', - 'pending', - 'reviewer', - '2026-04-10T00:02:10.000Z', - null, - null, - null, - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - const rawDb = new Database(dbPath, { readonly: true }); - expect( - rawDb - .prepare( - ` - SELECT turn_attempt_no - FROM paired_turn_reservations - WHERE turn_id = ? - `, - ) - .get(turnId), - ).toEqual({ turn_attempt_no: 1 }); - expect( - rawDb - .prepare( - ` - SELECT turn_attempt_no - FROM paired_task_execution_leases - WHERE turn_id = ? - `, - ) - .get(turnId), - ).toEqual({ turn_attempt_no: 1 }); - expect( - rawDb - .prepare( - ` - SELECT id, turn_attempt_no - FROM service_handoffs - WHERE turn_id = ? - ORDER BY id ASC - `, - ) - .all(turnId), - ).toEqual([ - { id: 1, turn_attempt_no: 1 }, - { id: 2, turn_attempt_no: 2 }, - ]); - expect( - rawDb - .prepare( - ` - SELECT attempt_no, parent_attempt_id - FROM paired_turn_attempts - WHERE turn_id = ? - ORDER BY attempt_no ASC - `, - ) - .all(turnId), - ).toEqual([ - { attempt_no: 1, parent_attempt_id: null }, - { - attempt_no: 2, - parent_attempt_id: buildPairedTurnAttemptParentId(turnId, 2), - }, - ]); - rawDb.close(); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - it('rejects mismatched turn-attempt provenance writes across attempt-backed tables', () => { const database = new Database(':memory:'); const turnId = @@ -5775,961 +3245,6 @@ describe('legacy service handoff rows', () => { } }); - it('fails init when a legacy handoff keeps an invalid turn_attempt_no provenance reference', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-invalid-handoff-turn-attempt-'), - ); - const dbPath = path.join(tempDir, 'invalid-handoff-turn-attempt.db'); - const turnId = - 'legacy-invalid-handoff:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - initializeDatabaseSchema(legacyDb); - legacyDb.exec(` - DROP TRIGGER IF EXISTS service_handoffs_validate_attempt_insert; - DROP TRIGGER IF EXISTS service_handoffs_validate_attempt_update; - `); - - insertPairedTurnIdentityRow(legacyDb, { - turnId, - taskId: 'legacy-invalid-handoff', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:00:00.000Z', - updatedAt: '2026-04-10T00:01:00.000Z', - }); - legacyDb - .prepare( - ` - INSERT INTO paired_turn_attempts ( - attempt_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - buildPairedTurnAttemptId(turnId, 1), - turnId, - 1, - 'legacy-invalid-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:00.000Z', - 'attempt failed', - ); - legacyDb - .prepare( - ` - INSERT INTO service_handoffs ( - chat_jid, - group_folder, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_attempt_no, - turn_intent_kind, - turn_role, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'test-group', - 'legacy-invalid-handoff', - '2026-04-10T00:05:00.000Z', - turnId, - 1, - 'reviewer-turn', - 'reviewer', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'legacy invalid handoff', - 'failed', - 'reviewer', - '2026-04-10T00:00:30.000Z', - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /service_handoffs\(id=1\) has invalid paired_turn_attempt provenance/, - ); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('fails init when an attempt keeps an invalid parent_handoff_id provenance reference', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-invalid-parent-handoff-'), - ); - const dbPath = path.join(tempDir, 'invalid-parent-handoff.db'); - const turnId = - 'legacy-invalid-parent-handoff:2026-04-10T00:00:00.000Z:reviewer-turn'; - const otherTurnId = - 'legacy-other-parent-handoff:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - initializeDatabaseSchema(legacyDb); - legacyDb.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - `); - - const insertTurn = (args: { - turnId: string; - taskId: string; - taskUpdatedAt: string; - role: 'owner' | 'reviewer' | 'arbiter'; - intentKind: - | 'owner-turn' - | 'reviewer-turn' - | 'arbiter-turn' - | 'owner-follow-up' - | 'finalize-owner-turn'; - createdAt: string; - updatedAt: string; - }) => insertPairedTurnIdentityRow(legacyDb, args); - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - attempt_id, - parent_attempt_id, - parent_handoff_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - - insertTurn({ - turnId: otherTurnId, - taskId: 'legacy-other-parent-handoff', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:00:00.000Z', - updatedAt: '2026-04-10T00:00:30.000Z', - }); - insertAttempt.run( - buildPairedTurnAttemptId(otherTurnId, 1), - null, - null, - otherTurnId, - 1, - 'legacy-other-parent-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'delegated', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:00:30.000Z', - null, - null, - ); - legacyDb - .prepare( - ` - INSERT INTO service_handoffs ( - chat_jid, - group_folder, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_attempt_id, - turn_attempt_no, - turn_intent_kind, - turn_role, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'test-group', - 'legacy-other-parent-handoff', - '2026-04-10T00:00:00.000Z', - otherTurnId, - buildPairedTurnAttemptId(otherTurnId, 1), - 1, - 'reviewer-turn', - 'reviewer', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'wrong parent handoff', - 'failed', - 'reviewer', - '2026-04-10T00:00:20.000Z', - ); - const wrongHandoffId = ( - legacyDb.prepare('SELECT last_insert_rowid() AS id').get() as { - id: number; - } - ).id; - - insertTurn({ - turnId, - taskId: 'legacy-invalid-parent-handoff', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:00:00.000Z', - updatedAt: '2026-04-10T00:01:00.000Z', - }); - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 1), - null, - null, - turnId, - 1, - 'legacy-invalid-parent-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:00:40.000Z', - '2026-04-10T00:00:40.000Z', - 'attempt 1 failed', - ); - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 2), - buildPairedTurnAttemptParentId(turnId, 2), - wrongHandoffId, - turnId, - 2, - 'legacy-invalid-parent-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'running', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:00.000Z', - null, - null, - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /invalid parent_handoff_id provenance/, - ); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('fails init when an attempt keeps a completed parent_handoff_id provenance reference', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-completed-parent-handoff-'), - ); - const dbPath = path.join(tempDir, 'completed-parent-handoff.db'); - const turnId = - 'legacy-completed-parent-handoff:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - initializeDatabaseSchema(legacyDb); - legacyDb.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - `); - - const insertTurn = (args: { - turnId: string; - taskId: string; - taskUpdatedAt: string; - role: 'owner' | 'reviewer' | 'arbiter'; - intentKind: - | 'owner-turn' - | 'reviewer-turn' - | 'arbiter-turn' - | 'owner-follow-up' - | 'finalize-owner-turn'; - createdAt: string; - updatedAt: string; - }) => insertPairedTurnIdentityRow(legacyDb, args); - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - attempt_id, - parent_attempt_id, - parent_handoff_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - - insertTurn({ - turnId, - taskId: 'legacy-completed-parent-handoff', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:00:00.000Z', - updatedAt: '2026-04-10T00:01:00.000Z', - }); - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 1), - null, - null, - turnId, - 1, - 'legacy-completed-parent-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'completed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:00:40.000Z', - '2026-04-10T00:00:40.000Z', - null, - ); - legacyDb - .prepare( - ` - INSERT INTO service_handoffs ( - chat_jid, - group_folder, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_attempt_id, - turn_attempt_no, - turn_intent_kind, - turn_role, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at, - completed_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'test-group', - 'legacy-completed-parent-handoff', - '2026-04-10T00:00:00.000Z', - turnId, - buildPairedTurnAttemptId(turnId, 1), - 1, - 'reviewer-turn', - 'reviewer', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'completed handoff cannot seed retry lineage', - 'completed', - 'reviewer', - '2026-04-10T00:00:20.000Z', - '2026-04-10T00:00:30.000Z', - ); - const completedHandoffId = ( - legacyDb.prepare('SELECT last_insert_rowid() AS id').get() as { - id: number; - } - ).id; - - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 2), - buildPairedTurnAttemptParentId(turnId, 2), - completedHandoffId, - turnId, - 2, - 'legacy-completed-parent-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'running', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:00.000Z', - null, - null, - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /invalid parent_handoff_id provenance/, - ); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('fails init when an attempt keeps an invalid continuation_handoff_id provenance reference', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-invalid-continuation-handoff-'), - ); - const dbPath = path.join(tempDir, 'invalid-continuation-handoff.db'); - const turnId = - 'legacy-invalid-continuation-handoff:2026-04-10T00:00:00.000Z:reviewer-turn'; - const otherTurnId = - 'legacy-other-continuation-handoff:2026-04-10T00:00:00.000Z:reviewer-turn'; - - try { - const legacyDb = new Database(dbPath); - initializeDatabaseSchema(legacyDb); - legacyDb.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - `); - - const insertTurn = (args: { - turnId: string; - taskId: string; - taskUpdatedAt: string; - role: 'owner' | 'reviewer' | 'arbiter'; - intentKind: - | 'owner-turn' - | 'reviewer-turn' - | 'arbiter-turn' - | 'owner-follow-up' - | 'finalize-owner-turn'; - createdAt: string; - updatedAt: string; - }) => - insertPairedTurnIdentityRow(legacyDb, { - turnId: args.turnId, - taskId: args.taskId, - taskUpdatedAt: args.taskUpdatedAt, - role: args.role, - intentKind: args.intentKind, - createdAt: args.createdAt, - updatedAt: args.updatedAt, - }); - const insertAttempt = legacyDb.prepare( - ` - INSERT INTO paired_turn_attempts ( - attempt_id, - parent_attempt_id, - parent_handoff_id, - continuation_handoff_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ); - - insertTurn({ - turnId: otherTurnId, - taskId: 'legacy-other-continuation-handoff', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:00:00.000Z', - updatedAt: '2026-04-10T00:00:30.000Z', - }); - insertAttempt.run( - buildPairedTurnAttemptId(otherTurnId, 1), - null, - null, - null, - otherTurnId, - 1, - 'legacy-other-continuation-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'delegated', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:00:30.000Z', - null, - null, - ); - legacyDb - .prepare( - ` - INSERT INTO service_handoffs ( - chat_jid, - group_folder, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_attempt_id, - turn_attempt_no, - turn_intent_kind, - turn_role, - source_service_id, - target_service_id, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - intended_role, - created_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'test-group', - 'legacy-other-continuation-handoff', - '2026-04-10T00:00:00.000Z', - otherTurnId, - buildPairedTurnAttemptId(otherTurnId, 1), - 1, - 'reviewer-turn', - 'reviewer', - CLAUDE_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, - 'owner', - 'claude-code', - 'reviewer', - 'codex', - 'wrong continuation handoff', - 'claimed', - 'reviewer', - '2026-04-10T00:00:20.000Z', - ); - const wrongHandoffId = ( - legacyDb.prepare('SELECT last_insert_rowid() AS id').get() as { - id: number; - } - ).id; - - insertTurn({ - turnId, - taskId: 'legacy-invalid-continuation-handoff', - taskUpdatedAt: '2026-04-10T00:00:00.000Z', - role: 'reviewer', - intentKind: 'reviewer-turn', - createdAt: '2026-04-10T00:00:00.000Z', - updatedAt: '2026-04-10T00:01:00.000Z', - }); - insertAttempt.run( - buildPairedTurnAttemptId(turnId, 1), - null, - null, - wrongHandoffId, - turnId, - 1, - 'legacy-invalid-continuation-handoff', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'running', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:00.000Z', - null, - null, - ); - legacyDb.close(); - - expect(() => _initTestDatabaseFromFile(dbPath)).toThrow( - /invalid continuation_handoff_id provenance/, - ); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - - it('rebuilds legacy turn-attempt provenance tables with actual foreign keys on init', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-turn-attempt-fk-rebuild-'), - ); - const dbPath = path.join(tempDir, 'turn-attempt-fk-rebuild.db'); - - try { - const legacyDb = new Database(dbPath); - legacyDb.exec(` - CREATE TABLE paired_turns ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL DEFAULT 'queued', - executor_service_id TEXT, - executor_agent_type TEXT, - active_run_id TEXT, - attempt_no INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT - ); - CREATE TABLE paired_turn_attempts ( - turn_id TEXT NOT NULL, - attempt_no INTEGER NOT NULL, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT, - PRIMARY KEY (turn_id, attempt_no) - ); - CREATE TABLE paired_turn_reservations ( - chat_jid TEXT NOT NULL, - task_id TEXT NOT NULL, - task_status TEXT NOT NULL, - round_trip_count INTEGER NOT NULL DEFAULT 0, - task_updated_at TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_attempt_no INTEGER, - turn_role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - scheduled_run_id TEXT, - consumed_run_id TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - consumed_at TEXT, - PRIMARY KEY (chat_jid, task_id, task_updated_at, intent_kind) - ); - CREATE TABLE paired_task_execution_leases ( - task_id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_attempt_no INTEGER, - intent_kind TEXT NOT NULL, - claimed_run_id TEXT NOT NULL, - claimed_service_id TEXT NOT NULL, - task_status TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - claimed_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - expires_at TEXT NOT NULL - ); - CREATE TABLE service_handoffs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - paired_task_id TEXT, - paired_task_updated_at TEXT, - turn_id TEXT, - turn_attempt_no INTEGER, - turn_intent_kind TEXT, - turn_role TEXT, - source_role TEXT, - source_agent_type TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT - ); - `); - legacyDb - .prepare( - ` - INSERT INTO paired_turns ( - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - attempt_no, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'fk-rebuild-turn:2026-04-10T00:00:00.000Z:reviewer-turn', - 'fk-rebuild-task', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - 1, - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:00.000Z', - 'attempt failed', - ); - legacyDb - .prepare( - ` - INSERT INTO paired_turn_attempts ( - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'fk-rebuild-turn:2026-04-10T00:00:00.000Z:reviewer-turn', - 1, - 'fk-rebuild-task', - '2026-04-10T00:00:00.000Z', - 'reviewer', - 'reviewer-turn', - 'failed', - CODEX_REVIEW_SERVICE_ID, - 'codex', - '2026-04-10T00:00:00.000Z', - '2026-04-10T00:01:00.000Z', - '2026-04-10T00:01:00.000Z', - 'attempt failed', - ); - legacyDb - .prepare( - ` - INSERT INTO paired_turn_reservations ( - chat_jid, - task_id, - task_status, - round_trip_count, - task_updated_at, - turn_id, - turn_attempt_no, - turn_role, - intent_kind, - status, - scheduled_run_id, - consumed_run_id, - created_at, - updated_at, - consumed_at - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `, - ) - .run( - 'group@test', - 'fk-rebuild-task', - 'review_ready', - 1, - '2026-04-10T00:00:00.000Z', - 'fk-rebuild-turn:2026-04-10T00:00:00.000Z:reviewer-turn', - 1, - 'reviewer', - 'reviewer-turn', - 'completed', - 'run-1', - 'run-1', - '2026-04-10T00:00:10.000Z', - '2026-04-10T00:00:20.000Z', - '2026-04-10T00:00:20.000Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - const migratedDb = new Database(dbPath, { readonly: true }); - const attemptFks = migratedDb - .prepare(`PRAGMA foreign_key_list(paired_turn_attempts)`) - .all() as Array<{ table: string; from: string; to: string }>; - const reservationFks = migratedDb - .prepare(`PRAGMA foreign_key_list(paired_turn_reservations)`) - .all() as Array<{ table: string; from: string; to: string }>; - const leaseFks = migratedDb - .prepare(`PRAGMA foreign_key_list(paired_task_execution_leases)`) - .all() as Array<{ table: string; from: string; to: string }>; - const handoffFks = migratedDb - .prepare(`PRAGMA foreign_key_list(service_handoffs)`) - .all() as Array<{ table: string; from: string; to: string }>; - - expect( - attemptFks.some( - (row) => - row.table === 'paired_turns' && - row.from === 'turn_id' && - row.to === 'turn_id', - ), - ).toBe(true); - expect( - attemptFks.some( - (row) => - row.table === 'paired_turn_attempts' && - row.from === 'parent_attempt_id' && - row.to === 'attempt_id', - ), - ).toBe(true); - expect( - attemptFks.some( - (row) => - row.table === 'service_handoffs' && - row.from === 'parent_handoff_id' && - row.to === 'id', - ), - ).toBe(true); - expect( - attemptFks.some( - (row) => - row.table === 'service_handoffs' && - row.from === 'continuation_handoff_id' && - row.to === 'id', - ), - ).toBe(true); - expect( - reservationFks.some( - (row) => - row.table === 'paired_turn_attempts' && row.from === 'turn_id', - ), - ).toBe(true); - expect( - leaseFks.some( - (row) => - row.table === 'paired_turn_attempts' && row.from === 'turn_id', - ), - ).toBe(true); - expect( - handoffFks.some( - (row) => - row.table === 'paired_turn_attempts' && row.from === 'turn_id', - ), - ).toBe(true); - migratedDb.close(); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - } - }); - it('uses real foreign keys to reject orphan attempt provenance when triggers are absent', () => { const database = new Database(':memory:'); @@ -7609,340 +4124,6 @@ describe('message seq cursors', () => { getLatestMessageSeqAtOrBefore('2024-01-01T00:00:02.000Z', 'group@g.us'), ).toBe(2); }); - - it('preserves legacy timestamp order when backfilling seq during init', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-message-seq-order-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE messages ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - sender TEXT NOT NULL, - sender_name TEXT NOT NULL, - content TEXT NOT NULL, - timestamp TEXT NOT NULL, - is_from_me INTEGER NOT NULL DEFAULT 0, - is_bot_message INTEGER NOT NULL DEFAULT 0, - seq INTEGER - ); - `); - - legacyDb - .prepare( - `INSERT INTO messages ( - id, chat_jid, sender, sender_name, content, timestamp, is_from_me, is_bot_message, seq - ) VALUES (?, ?, ?, ?, ?, ?, 0, 0, NULL)`, - ) - .run( - 'legacy-late', - 'dc:legacy-seq', - 'user-1', - 'User One', - 'late insert', - '2024-01-01T00:00:02.000Z', - ); - legacyDb - .prepare( - `INSERT INTO messages ( - id, chat_jid, sender, sender_name, content, timestamp, is_from_me, is_bot_message, seq - ) VALUES (?, ?, ?, ?, ?, ?, 0, 0, NULL)`, - ) - .run( - 'legacy-early', - 'dc:legacy-seq', - 'user-2', - 'User Two', - 'early insert', - '2024-01-01T00:00:01.000Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect( - getMessagesSinceSeq('dc:legacy-seq', 0, 'Andy').map((message) => ({ - id: message.id, - seq: message.seq, - })), - ).toEqual([ - { id: 'legacy-early', seq: 1 }, - { id: 'legacy-late', seq: 2 }, - ]); - }); - - it('backfills missing legacy seq values after the existing maximum without duplicates', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-message-seq-partial-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE messages ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - sender TEXT NOT NULL, - sender_name TEXT NOT NULL, - content TEXT NOT NULL, - timestamp TEXT NOT NULL, - is_from_me INTEGER NOT NULL DEFAULT 0, - is_bot_message INTEGER NOT NULL DEFAULT 0, - seq INTEGER - ); - `); - - legacyDb - .prepare( - `INSERT INTO messages ( - id, chat_jid, sender, sender_name, content, timestamp, is_from_me, is_bot_message, seq - ) VALUES (?, ?, ?, ?, ?, ?, 0, 0, ?)`, - ) - .run( - 'legacy-existing-seq', - 'dc:legacy-partial-seq', - 'user-1', - 'User One', - 'existing seq', - '2024-01-01T00:00:01.000Z', - 1, - ); - legacyDb - .prepare( - `INSERT INTO messages ( - id, chat_jid, sender, sender_name, content, timestamp, is_from_me, is_bot_message, seq - ) VALUES (?, ?, ?, ?, ?, ?, 0, 0, NULL)`, - ) - .run( - 'legacy-missing-seq', - 'dc:legacy-partial-seq', - 'user-2', - 'User Two', - 'missing seq', - '2024-01-01T00:00:00.500Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect( - getMessagesSinceSeq('dc:legacy-partial-seq', 0, 'Andy').map( - (message) => ({ - id: message.id, - seq: message.seq, - }), - ), - ).toEqual([ - { id: 'legacy-existing-seq', seq: 1 }, - { id: 'legacy-missing-seq', seq: 2 }, - ]); - }); -}); - -describe('legacy schema writes after init', () => { - it('creates paired tasks after init on a legacy paired_tasks schema', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-legacy-paired-task-write-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - createPairedTask({ - id: 'paired-legacy-write', - chat_jid: 'dc:legacy-write', - group_folder: 'legacy-write-room', - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - arbiter_agent_type: ARBITER_AGENT_TYPE ?? null, - title: 'legacy write task', - source_ref: 'HEAD', - plan_notes: null, - review_requested_at: null, - round_trip_count: 0, - status: 'active', - arbiter_verdict: null, - arbiter_requested_at: null, - completion_reason: null, - created_at: '2026-03-28T00:00:00.000Z', - updated_at: '2026-03-28T00:00:00.000Z', - }); - - expect(getPairedTaskById('paired-legacy-write')).toMatchObject({ - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - }); - }); - - it('creates paired tasks after init on a canonical paired_tasks schema without service id columns', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-canonical-paired-task-write-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL, - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - createPairedTask({ - id: 'paired-canonical-write', - chat_jid: 'dc:canonical-write', - group_folder: 'canonical-write-room', - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - arbiter_agent_type: ARBITER_AGENT_TYPE ?? null, - title: 'canonical write task', - source_ref: 'HEAD', - plan_notes: null, - review_requested_at: null, - round_trip_count: 0, - status: 'active', - arbiter_verdict: null, - arbiter_requested_at: null, - completion_reason: null, - created_at: '2026-03-28T00:00:00.000Z', - updated_at: '2026-03-28T00:00:00.000Z', - }); - - expect(getPairedTaskById('paired-canonical-write')).toMatchObject({ - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - }); - }); - - it('creates channel owner leases after init on a legacy channel_owner schema', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-legacy-channel-owner-write-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE channel_owner ( - chat_jid TEXT PRIMARY KEY, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT, - arbiter_service_id TEXT, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - activated_at TEXT, - reason TEXT - ); - `); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - setChannelOwnerLease({ - chat_jid: 'dc:legacy-channel-owner-write', - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - activated_at: '2026-03-28T00:00:00.000Z', - reason: 'legacy-write', - }); - - expect(getChannelOwnerLease('dc:legacy-channel-owner-write')).toMatchObject( - { - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - }, - ); - }); - - it('creates channel owner leases after init on a canonical channel_owner schema without service id columns', () => { - const tempDir = fs.mkdtempSync( - '/tmp/ejclaw-canonical-channel-owner-write-', - ); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE channel_owner ( - chat_jid TEXT PRIMARY KEY, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - activated_at TEXT, - reason TEXT - ); - `); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - setChannelOwnerLease({ - chat_jid: 'dc:canonical-channel-owner-write', - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - activated_at: '2026-03-28T00:00:00.000Z', - reason: 'canonical-write', - }); - - expect( - getChannelOwnerLease('dc:canonical-channel-owner-write'), - ).toMatchObject({ - owner_service_id: CLAUDE_SERVICE_ID, - reviewer_service_id: CODEX_REVIEW_SERVICE_ID, - owner_agent_type: 'claude-code', - reviewer_agent_type: 'codex', - }); - }); }); describe('memories', () => { @@ -8308,70 +4489,4 @@ describe('work items', () => { _initTestDatabase(); } }); - - it('backfills work item service ids during init on a canonical work_items schema without service_id columns', () => { - const tempDir = fs.mkdtempSync('/tmp/ejclaw-work-items-canonical-'); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - legacyDb.exec(` - CREATE TABLE work_items ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - group_folder TEXT NOT NULL, - chat_jid TEXT NOT NULL, - agent_type TEXT NOT NULL, - delivery_role TEXT, - status TEXT NOT NULL DEFAULT 'produced', - start_seq INTEGER, - end_seq INTEGER, - result_payload TEXT NOT NULL, - delivery_attempts INTEGER NOT NULL DEFAULT 0, - delivery_message_id TEXT, - last_error TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - delivered_at TEXT - ); - `); - legacyDb - .prepare( - `INSERT INTO work_items ( - group_folder, - chat_jid, - agent_type, - delivery_role, - status, - start_seq, - end_seq, - result_payload, - delivery_attempts, - created_at, - updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .run( - 'discord_test', - 'dc:legacy-work-item', - 'codex', - 'reviewer', - 'produced', - 1, - 2, - 'legacy reviewer output', - 0, - '2026-03-28T00:00:00.000Z', - '2026-03-28T00:00:00.000Z', - ); - legacyDb.close(); - - _initTestDatabaseFromFile(dbPath); - - expect( - getOpenWorkItem('dc:legacy-work-item', 'codex', CODEX_REVIEW_SERVICE_ID), - ).toMatchObject({ - delivery_role: 'reviewer', - service_id: CODEX_REVIEW_SERVICE_ID, - result_payload: 'legacy reviewer output', - }); - }); }); diff --git a/src/db/base-schema.ts b/src/db/base-schema.ts index 2bbd45d..2a1bd11 100644 --- a/src/db/base-schema.ts +++ b/src/db/base-schema.ts @@ -1,460 +1,890 @@ import { Database } from 'bun:sqlite'; -const ROOM_SKILL_OVERRIDES_SCHEMA = ` - CREATE TABLE IF NOT EXISTS room_skill_overrides ( - chat_jid TEXT NOT NULL, - agent_type TEXT NOT NULL, - skill_scope TEXT NOT NULL, - skill_name TEXT NOT NULL, - enabled INTEGER NOT NULL, - created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (chat_jid, agent_type, skill_scope, skill_name), - FOREIGN KEY (chat_jid) REFERENCES room_settings(chat_jid) ON DELETE CASCADE, - CHECK (agent_type IN ('claude-code', 'codex')), - CHECK (enabled IN (0, 1)), - CHECK (length(skill_scope) > 0), - CHECK (length(skill_name) > 0) - ); - CREATE INDEX IF NOT EXISTS idx_room_skill_overrides_room - ON room_skill_overrides(chat_jid, agent_type); +/** + * Full schema at BASELINE_SCHEMA_VERSION (see migrations/index.ts). + * + * The historical migrations 1-22 were squashed into this baseline. It matches + * the production database schema after those migrations, column order + * included. Do not edit it to change the schema: add a numbered migration in + * migrations/index.ts instead, so existing databases and fresh ones converge. + */ +const BASE_SCHEMA_SQL = ` +CREATE TABLE chats ( + jid TEXT PRIMARY KEY, + name TEXT, + last_message_time TEXT, + channel TEXT, + is_group INTEGER DEFAULT 0 +); + +CREATE TABLE messages ( + id TEXT, + chat_jid TEXT, + sender TEXT, + sender_name TEXT, + content TEXT, + timestamp TEXT, + seq INTEGER, + is_from_me INTEGER, + is_bot_message INTEGER DEFAULT 0, + message_source_kind TEXT NOT NULL DEFAULT 'human', + PRIMARY KEY (id, chat_jid), + FOREIGN KEY (chat_jid) REFERENCES chats(jid) +); + +CREATE INDEX idx_timestamp ON messages(timestamp); + +CREATE UNIQUE INDEX idx_messages_seq ON messages(seq); + +CREATE INDEX idx_messages_chat_jid_seq ON messages(chat_jid, seq); + +CREATE TABLE message_sequence ( + id INTEGER PRIMARY KEY AUTOINCREMENT +); + +CREATE TABLE work_items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + group_folder TEXT NOT NULL, + chat_jid TEXT NOT NULL, + agent_type TEXT NOT NULL, + service_id TEXT, + delivery_role TEXT, + status TEXT NOT NULL DEFAULT 'produced', + start_seq INTEGER, + end_seq INTEGER, + result_payload TEXT NOT NULL, + attachment_payload TEXT, + delivery_attempts INTEGER NOT NULL DEFAULT 0, + delivery_message_id TEXT, + last_error TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + updated_at TEXT NOT NULL DEFAULT (datetime('now')), + delivered_at TEXT, + CHECK (status IN ('produced', 'delivery_retry', 'delivered')), + CHECK (delivery_role IN ('owner', 'reviewer', 'arbiter') OR delivery_role IS NULL) +); + +CREATE INDEX idx_work_items_status ON work_items(status, updated_at); + +CREATE INDEX idx_work_items_group_agent + ON work_items(chat_jid, agent_type, service_id, delivery_role, status); + +CREATE UNIQUE INDEX idx_work_items_open + ON work_items(chat_jid, agent_type, IFNULL(service_id, ''), IFNULL(delivery_role, '')) + WHERE status IN ('produced', 'delivery_retry'); + +CREATE TABLE scheduled_tasks ( + id TEXT PRIMARY KEY, + group_folder TEXT NOT NULL, + chat_jid TEXT NOT NULL, + agent_type TEXT, + ci_provider TEXT, + ci_metadata TEXT, + max_duration_ms INTEGER, + status_message_id TEXT, + status_started_at TEXT, + prompt TEXT NOT NULL, + schedule_type TEXT NOT NULL, + schedule_value TEXT NOT NULL, + next_run TEXT, + last_run TEXT, + last_result TEXT, + status TEXT DEFAULT 'active', + created_at TEXT NOT NULL, + context_mode TEXT DEFAULT 'isolated', + suspended_until TEXT, + room_role TEXT +); + +CREATE INDEX idx_next_run ON scheduled_tasks(next_run); + +CREATE INDEX idx_status ON scheduled_tasks(status); + +CREATE TABLE task_run_logs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + task_id TEXT NOT NULL, + run_at TEXT NOT NULL, + duration_ms INTEGER NOT NULL, + status TEXT NOT NULL, + result TEXT, + error TEXT, + FOREIGN KEY (task_id) REFERENCES scheduled_tasks(id) +); + +CREATE INDEX idx_task_run_logs ON task_run_logs(task_id, run_at); + +CREATE TABLE router_state ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL +); + +CREATE TABLE sessions ( + group_folder TEXT NOT NULL, + agent_type TEXT NOT NULL DEFAULT 'claude-code', + session_id TEXT NOT NULL, + PRIMARY KEY (group_folder, agent_type) +); + +CREATE TABLE paired_projects ( + chat_jid TEXT PRIMARY KEY, + group_folder TEXT NOT NULL, + canonical_work_dir TEXT NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL +); + +CREATE TABLE paired_tasks ( + id TEXT PRIMARY KEY, + chat_jid TEXT NOT NULL, + group_folder TEXT NOT NULL, + owner_service_id TEXT NOT NULL, + reviewer_service_id TEXT NOT NULL, + owner_agent_type TEXT, + reviewer_agent_type TEXT, + arbiter_agent_type TEXT, + title TEXT, + source_ref TEXT, + plan_notes TEXT, + review_requested_at TEXT, + round_trip_count INTEGER NOT NULL DEFAULT 0, + owner_failure_count INTEGER NOT NULL DEFAULT 0, + owner_step_done_streak INTEGER NOT NULL DEFAULT 0, + finalize_step_done_count INTEGER NOT NULL DEFAULT 0, + task_done_then_user_reopen_count INTEGER NOT NULL DEFAULT 0, + empty_step_done_streak INTEGER NOT NULL DEFAULT 0, + status TEXT NOT NULL DEFAULT 'active', + arbiter_verdict TEXT, + arbiter_requested_at TEXT, + completion_reason TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + reviewer_failure_count INTEGER NOT NULL DEFAULT 0, + arbiter_intervention_count INTEGER NOT NULL DEFAULT 0, + CHECK (status IN ('active', 'review_ready', 'in_review', 'merge_ready', 'completed', 'arbiter_requested', 'in_arbitration')), + CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL), + CHECK (reviewer_agent_type IN ('claude-code', 'codex') OR reviewer_agent_type IS NULL), + CHECK (arbiter_agent_type IN ('claude-code', 'codex') OR arbiter_agent_type IS NULL) +); + +CREATE INDEX idx_paired_tasks_chat_status + ON paired_tasks(chat_jid, status, updated_at); + +CREATE TABLE paired_workspaces ( + id TEXT PRIMARY KEY, + task_id TEXT NOT NULL, + role TEXT NOT NULL, + workspace_dir TEXT NOT NULL, + snapshot_source_dir TEXT, + snapshot_ref TEXT, + status TEXT NOT NULL DEFAULT 'ready', + snapshot_refreshed_at TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + CHECK (role IN ('owner', 'reviewer')), + CHECK (status IN ('ready', 'stale')) +); + +CREATE UNIQUE INDEX idx_paired_workspaces_task_role + ON paired_workspaces(task_id, role); + +CREATE TABLE paired_turn_outputs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + task_id TEXT NOT NULL, + turn_number INTEGER NOT NULL, + role TEXT NOT NULL, + output_text TEXT NOT NULL, + verdict TEXT, + created_at TEXT NOT NULL, + attachment_payload TEXT, + UNIQUE(task_id, turn_number, role) +); + +CREATE INDEX idx_paired_turn_outputs_task + ON paired_turn_outputs(task_id, turn_number); + +CREATE TABLE paired_turns ( + turn_id TEXT PRIMARY KEY, + task_id TEXT NOT NULL, + task_updated_at TEXT NOT NULL, + role TEXT NOT NULL, + intent_kind TEXT NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + progress_text TEXT, + progress_updated_at TEXT, + CHECK (role IN ('owner', 'reviewer', 'arbiter')), + CHECK ( + intent_kind IN ( + 'owner-turn', + 'reviewer-turn', + 'arbiter-turn', + 'owner-follow-up', + 'finalize-owner-turn' + ) + ) +); + +CREATE INDEX idx_paired_turns_task + ON paired_turns(task_id, task_updated_at, updated_at); + +CREATE TABLE paired_turn_attempts ( + attempt_id TEXT NOT NULL PRIMARY KEY, + parent_attempt_id TEXT, + parent_handoff_id INTEGER, + continuation_handoff_id INTEGER, + turn_id TEXT NOT NULL, + attempt_no INTEGER NOT NULL, + task_id TEXT NOT NULL, + task_updated_at TEXT NOT NULL, + role TEXT NOT NULL, + intent_kind TEXT NOT NULL, + state TEXT NOT NULL, + executor_service_id TEXT, + executor_agent_type TEXT, + active_run_id TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + completed_at TEXT, + last_error TEXT, + UNIQUE (turn_id, attempt_no), + FOREIGN KEY (parent_attempt_id) + REFERENCES paired_turn_attempts(attempt_id) + ON DELETE CASCADE, + FOREIGN KEY (parent_handoff_id) + REFERENCES service_handoffs(id) + ON DELETE SET NULL, + FOREIGN KEY (continuation_handoff_id) + REFERENCES service_handoffs(id) + ON DELETE SET NULL, + FOREIGN KEY (turn_id) REFERENCES paired_turns(turn_id) ON DELETE CASCADE, + CHECK (role IN ('owner', 'reviewer', 'arbiter')), + CHECK ( + intent_kind IN ( + 'owner-turn', + 'reviewer-turn', + 'arbiter-turn', + 'owner-follow-up', + 'finalize-owner-turn' + ) + ), + CHECK ( + state IN ( + 'running', + 'delegated', + 'completed', + 'failed', + 'cancelled' + ) + ), + CHECK (executor_agent_type IN ('claude-code', 'codex') OR executor_agent_type IS NULL) +); + +CREATE INDEX idx_paired_turn_attempts_turn + ON paired_turn_attempts(turn_id, attempt_no); + +CREATE INDEX idx_paired_turn_attempts_task + ON paired_turn_attempts(task_id, task_updated_at, attempt_no); + +CREATE INDEX idx_paired_turn_attempts_parent_handoff_id + ON paired_turn_attempts(parent_handoff_id); + +CREATE INDEX idx_paired_turn_attempts_continuation_handoff_id + ON paired_turn_attempts(continuation_handoff_id); + +CREATE TABLE channel_owner ( + chat_jid TEXT PRIMARY KEY, + owner_service_id TEXT NOT NULL, + reviewer_service_id TEXT, + arbiter_service_id TEXT, + owner_agent_type TEXT, + reviewer_agent_type TEXT, + arbiter_agent_type TEXT, + activated_at TEXT, + reason TEXT, + CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL), + CHECK (reviewer_agent_type IN ('claude-code', 'codex') OR reviewer_agent_type IS NULL), + CHECK (arbiter_agent_type IN ('claude-code', 'codex') OR arbiter_agent_type IS NULL) +); + +CREATE TABLE room_settings ( + chat_jid TEXT PRIMARY KEY, + room_mode TEXT NOT NULL, + mode_source TEXT NOT NULL DEFAULT 'explicit', + name TEXT, + folder TEXT, + trigger_pattern TEXT, + requires_trigger INTEGER DEFAULT 1, + is_main INTEGER DEFAULT 0, + owner_agent_type TEXT, + work_dir TEXT, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TEXT NOT NULL, + CHECK (room_mode IN ('single', 'tribunal')), + CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL) +); + +CREATE TABLE room_role_overrides ( + chat_jid TEXT NOT NULL, + role TEXT NOT NULL, + agent_type TEXT NOT NULL, + agent_config_json TEXT, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (chat_jid, role), + CHECK (role IN ('owner', 'reviewer', 'arbiter')), + CHECK (agent_type IN ('claude-code', 'codex')) +); + +CREATE TABLE memories ( + id INTEGER PRIMARY KEY, + scope_kind TEXT NOT NULL, + scope_key TEXT NOT NULL, + content TEXT NOT NULL, + keywords_json TEXT NOT NULL DEFAULT '[]', + memory_kind TEXT, + source_kind TEXT NOT NULL, + source_ref TEXT, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_used_at TEXT, + archived_at TEXT +); + +CREATE INDEX idx_memories_scope + ON memories(scope_kind, scope_key); + +CREATE INDEX idx_memories_active + ON memories(scope_kind, scope_key, archived_at, created_at); + +CREATE VIRTUAL TABLE memories_fts USING fts5( + content, + keywords, + content='', + tokenize='unicode61' +); + +CREATE TABLE paired_turn_reservations ( + chat_jid TEXT NOT NULL, + task_id TEXT NOT NULL, + task_status TEXT NOT NULL, + round_trip_count INTEGER NOT NULL DEFAULT 0, + task_updated_at TEXT NOT NULL, + turn_id TEXT NOT NULL, + turn_attempt_id TEXT, + turn_attempt_no INTEGER, + turn_role TEXT NOT NULL, + intent_kind TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'pending', + scheduled_run_id TEXT, + consumed_run_id TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + consumed_at TEXT, + PRIMARY KEY (chat_jid, task_id, task_updated_at, intent_kind), + FOREIGN KEY (turn_attempt_id) + REFERENCES paired_turn_attempts(attempt_id) + ON DELETE CASCADE, + FOREIGN KEY (turn_id, turn_attempt_no) + REFERENCES paired_turn_attempts(turn_id, attempt_no) + ON DELETE CASCADE, + CHECK (status IN ('pending', 'completed')), + CHECK (turn_role IN ('owner', 'reviewer', 'arbiter')), + CHECK ( + intent_kind IN ( + 'owner-turn', + 'reviewer-turn', + 'arbiter-turn', + 'owner-follow-up', + 'finalize-owner-turn' + ) + ) +); + +CREATE INDEX idx_paired_turn_reservations_task + ON paired_turn_reservations(task_id, task_updated_at, status); + +CREATE INDEX idx_paired_turn_reservations_turn_attempt + ON paired_turn_reservations(turn_id, turn_attempt_no); + +CREATE INDEX idx_paired_turn_reservations_turn_attempt_id + ON paired_turn_reservations(turn_attempt_id); + +CREATE TABLE paired_task_execution_leases ( + task_id TEXT PRIMARY KEY, + chat_jid TEXT NOT NULL, + role TEXT NOT NULL, + turn_id TEXT NOT NULL, + turn_attempt_id TEXT, + turn_attempt_no INTEGER, + intent_kind TEXT NOT NULL, + claimed_run_id TEXT NOT NULL, + claimed_service_id TEXT NOT NULL, + task_status TEXT NOT NULL, + task_updated_at TEXT NOT NULL, + claimed_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + expires_at TEXT NOT NULL, + FOREIGN KEY (turn_attempt_id) + REFERENCES paired_turn_attempts(attempt_id) + ON DELETE CASCADE, + FOREIGN KEY (turn_id, turn_attempt_no) + REFERENCES paired_turn_attempts(turn_id, attempt_no) + ON DELETE CASCADE, + CHECK (role IN ('owner', 'reviewer', 'arbiter')), + CHECK ( + intent_kind IN ( + 'owner-turn', + 'reviewer-turn', + 'arbiter-turn', + 'owner-follow-up', + 'finalize-owner-turn' + ) + ) +); + +CREATE INDEX idx_paired_task_execution_leases_expires_at + ON paired_task_execution_leases(expires_at); + +CREATE INDEX idx_paired_task_execution_leases_turn_attempt + ON paired_task_execution_leases(turn_id, turn_attempt_no); + +CREATE INDEX idx_paired_task_execution_leases_turn_attempt_id + ON paired_task_execution_leases(turn_attempt_id); + +CREATE TABLE service_handoffs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + chat_jid TEXT NOT NULL, + group_folder TEXT NOT NULL, + source_service_id TEXT NOT NULL, + target_service_id TEXT NOT NULL, + paired_task_id TEXT, + paired_task_updated_at TEXT, + turn_id TEXT, + turn_attempt_id TEXT, + turn_attempt_no INTEGER, + turn_intent_kind TEXT, + turn_role TEXT, + source_role TEXT, + source_agent_type TEXT, + target_role TEXT, + target_agent_type TEXT NOT NULL, + prompt TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'pending', + start_seq INTEGER, + end_seq INTEGER, + reason TEXT, + intended_role TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + claimed_at TEXT, + completed_at TEXT, + last_error TEXT, + FOREIGN KEY (turn_attempt_id) + REFERENCES paired_turn_attempts(attempt_id) + ON DELETE CASCADE, + FOREIGN KEY (turn_id, turn_attempt_no) + REFERENCES paired_turn_attempts(turn_id, attempt_no) + ON DELETE CASCADE, + CHECK (status IN ('pending', 'claimed', 'completed', 'failed')), + CHECK (intended_role IN ('owner', 'reviewer', 'arbiter') OR intended_role IS NULL), + CHECK ( + turn_intent_kind IN ( + 'owner-turn', + 'reviewer-turn', + 'arbiter-turn', + 'owner-follow-up', + 'finalize-owner-turn' + ) OR turn_intent_kind IS NULL + ), + CHECK (turn_role IN ('owner', 'reviewer', 'arbiter') OR turn_role IS NULL), + CHECK (source_role IN ('owner', 'reviewer', 'arbiter') OR source_role IS NULL), + CHECK (target_role IN ('owner', 'reviewer', 'arbiter') OR target_role IS NULL) +); + +CREATE INDEX idx_service_handoffs_target + ON service_handoffs(status, target_role, target_agent_type, created_at); + +CREATE INDEX idx_service_handoffs_turn_attempt + ON service_handoffs(turn_id, turn_attempt_no); + +CREATE INDEX idx_service_handoffs_turn_attempt_id + ON service_handoffs(turn_attempt_id); + +CREATE TABLE room_skill_overrides ( + chat_jid TEXT NOT NULL, + agent_type TEXT NOT NULL, + skill_scope TEXT NOT NULL, + skill_name TEXT NOT NULL, + enabled INTEGER NOT NULL, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (chat_jid, agent_type, skill_scope, skill_name), + FOREIGN KEY (chat_jid) REFERENCES room_settings(chat_jid) ON DELETE CASCADE, + CHECK (agent_type IN ('claude-code', 'codex')), + CHECK (enabled IN (0, 1)), + CHECK (length(skill_scope) > 0), + CHECK (length(skill_name) > 0) +); + +CREATE INDEX idx_room_skill_overrides_room + ON room_skill_overrides(chat_jid, agent_type); + +CREATE TRIGGER memories_ai AFTER INSERT ON memories BEGIN + INSERT INTO memories_fts(rowid, content, keywords) + VALUES (new.id, new.content, new.keywords_json); +END; + +CREATE TRIGGER memories_ad AFTER DELETE ON memories BEGIN + INSERT INTO memories_fts(memories_fts, rowid, content, keywords) + VALUES ('delete', old.id, old.content, old.keywords_json); +END; + +CREATE TRIGGER memories_au AFTER UPDATE ON memories BEGIN + INSERT INTO memories_fts(memories_fts, rowid, content, keywords) + VALUES ('delete', old.id, old.content, old.keywords_json); + INSERT INTO memories_fts(rowid, content, keywords) + VALUES (new.id, new.content, new.keywords_json); +END; + +CREATE TRIGGER paired_turn_attempts_validate_insert +BEFORE INSERT ON paired_turn_attempts +BEGIN + SELECT RAISE(ABORT, 'paired_turn_attempts attempt_id must match turn_id/attempt_no') + WHERE NEW.attempt_id IS NULL + OR NEW.attempt_id != NEW.turn_id || ':attempt:' || CAST(NEW.attempt_no AS TEXT); + SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_attempt_id') + WHERE NEW.attempt_no <= 1 + AND NEW.parent_attempt_id IS NOT NULL; + SELECT RAISE(ABORT, 'paired_turn_attempts must preserve contiguous parent lineage') + WHERE NEW.attempt_no > 1 + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts previous_attempt + WHERE previous_attempt.turn_id = NEW.turn_id + AND previous_attempt.attempt_no = NEW.attempt_no - 1 + ); + SELECT RAISE(ABORT, 'paired_turn_attempts must keep parent_attempt_id lineage') + WHERE NEW.attempt_no > 1 + AND ( + NEW.parent_attempt_id IS NULL + OR NEW.parent_attempt_id != CASE + WHEN NEW.attempt_no <= 1 THEN NULL + ELSE NEW.turn_id || ':attempt:' || CAST((NEW.attempt_no) - 1 AS TEXT) + END + ); + SELECT RAISE(ABORT, 'paired_turn_attempts parent_attempt_id must point to the previous attempt of the same turn') + WHERE NEW.parent_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts previous_attempt + WHERE COALESCE( + previous_attempt.attempt_id, + previous_attempt.turn_id || ':attempt:' || CAST(previous_attempt.attempt_no AS TEXT) + ) = NEW.parent_attempt_id + AND previous_attempt.turn_id = NEW.turn_id + AND previous_attempt.attempt_no = NEW.attempt_no - 1 + ); + SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_handoff_id') + WHERE NEW.attempt_no <= 1 + AND NEW.parent_handoff_id IS NOT NULL; + SELECT RAISE(ABORT, 'paired_turn_attempts parent_handoff_id must reference the previous attempt handoff of the same turn') + WHERE NEW.parent_handoff_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM service_handoffs + WHERE service_handoffs.id = NEW.parent_handoff_id + AND service_handoffs.turn_id = NEW.turn_id + AND service_handoffs.status = 'failed' + AND ( + service_handoffs.turn_attempt_id = NEW.parent_attempt_id + OR ( + service_handoffs.turn_attempt_id IS NULL + AND service_handoffs.turn_attempt_no = (NEW.attempt_no) - 1 + ) + ) + ); + SELECT RAISE(ABORT, 'paired_turn_attempts continuation_handoff_id must reference a handoff of the same attempt') + WHERE NEW.continuation_handoff_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM service_handoffs + WHERE service_handoffs.id = NEW.continuation_handoff_id + AND service_handoffs.turn_id = NEW.turn_id + AND ( + service_handoffs.turn_attempt_id = NEW.attempt_id + OR ( + service_handoffs.turn_attempt_id IS NULL + AND service_handoffs.turn_attempt_no = NEW.attempt_no + ) + ) + ); + SELECT RAISE(ABORT, 'paired_turn_attempts running attempts must declare active_run_id') + WHERE NEW.state = 'running' + AND ( + NEW.active_run_id IS NULL + OR TRIM(NEW.active_run_id) = '' + ); + SELECT RAISE(ABORT, 'paired_turn_attempts only running attempts may declare active_run_id') + WHERE NEW.state != 'running' + AND NEW.active_run_id IS NOT NULL; + SELECT RAISE(ABORT, 'paired_turn_attempts must reference a matching paired_turns row') + WHERE NOT EXISTS ( + SELECT 1 + FROM paired_turns + WHERE paired_turns.turn_id = NEW.turn_id + AND paired_turns.task_id = NEW.task_id + AND paired_turns.task_updated_at = NEW.task_updated_at + AND paired_turns.role = NEW.role + AND paired_turns.intent_kind = NEW.intent_kind + ); +END; + +CREATE TRIGGER paired_turn_attempts_validate_update +BEFORE UPDATE OF attempt_id, parent_attempt_id, parent_handoff_id, continuation_handoff_id, turn_id, attempt_no, task_id, task_updated_at, role, intent_kind, state, executor_service_id, executor_agent_type, active_run_id + ON paired_turn_attempts +BEGIN + SELECT RAISE(ABORT, 'paired_turn_attempts attempt_id must match turn_id/attempt_no') + WHERE NEW.attempt_id IS NULL + OR NEW.attempt_id != NEW.turn_id || ':attempt:' || CAST(NEW.attempt_no AS TEXT); + SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_attempt_id') + WHERE NEW.attempt_no <= 1 + AND NEW.parent_attempt_id IS NOT NULL; + SELECT RAISE(ABORT, 'paired_turn_attempts must preserve contiguous parent lineage') + WHERE NEW.attempt_no > 1 + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts previous_attempt + WHERE previous_attempt.turn_id = NEW.turn_id + AND previous_attempt.attempt_no = NEW.attempt_no - 1 + ); + SELECT RAISE(ABORT, 'paired_turn_attempts must keep parent_attempt_id lineage') + WHERE NEW.attempt_no > 1 + AND ( + NEW.parent_attempt_id IS NULL + OR NEW.parent_attempt_id != CASE + WHEN NEW.attempt_no <= 1 THEN NULL + ELSE NEW.turn_id || ':attempt:' || CAST((NEW.attempt_no) - 1 AS TEXT) + END + ); + SELECT RAISE(ABORT, 'paired_turn_attempts parent_attempt_id must point to the previous attempt of the same turn') + WHERE NEW.parent_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts previous_attempt + WHERE COALESCE( + previous_attempt.attempt_id, + previous_attempt.turn_id || ':attempt:' || CAST(previous_attempt.attempt_no AS TEXT) + ) = NEW.parent_attempt_id + AND previous_attempt.turn_id = NEW.turn_id + AND previous_attempt.attempt_no = NEW.attempt_no - 1 + ); + SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_handoff_id') + WHERE NEW.attempt_no <= 1 + AND NEW.parent_handoff_id IS NOT NULL; + SELECT RAISE(ABORT, 'paired_turn_attempts parent_handoff_id must reference the previous attempt handoff of the same turn') + WHERE NEW.parent_handoff_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM service_handoffs + WHERE service_handoffs.id = NEW.parent_handoff_id + AND service_handoffs.turn_id = NEW.turn_id + AND service_handoffs.status = 'failed' + AND ( + service_handoffs.turn_attempt_id = NEW.parent_attempt_id + OR ( + service_handoffs.turn_attempt_id IS NULL + AND service_handoffs.turn_attempt_no = (NEW.attempt_no) - 1 + ) + ) + ); + SELECT RAISE(ABORT, 'paired_turn_attempts continuation_handoff_id must reference a handoff of the same attempt') + WHERE NEW.continuation_handoff_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM service_handoffs + WHERE service_handoffs.id = NEW.continuation_handoff_id + AND service_handoffs.turn_id = NEW.turn_id + AND ( + service_handoffs.turn_attempt_id = NEW.attempt_id + OR ( + service_handoffs.turn_attempt_id IS NULL + AND service_handoffs.turn_attempt_no = NEW.attempt_no + ) + ) + ); + SELECT RAISE(ABORT, 'paired_turn_attempts running attempts must declare active_run_id') + WHERE NEW.state = 'running' + AND ( + NEW.active_run_id IS NULL + OR TRIM(NEW.active_run_id) = '' + ); + SELECT RAISE(ABORT, 'paired_turn_attempts only running attempts may declare active_run_id') + WHERE NEW.state != 'running' + AND NEW.active_run_id IS NOT NULL; + SELECT RAISE(ABORT, 'paired_turn_attempts must reference a matching paired_turns row') + WHERE NOT EXISTS ( + SELECT 1 + FROM paired_turns + WHERE paired_turns.turn_id = NEW.turn_id + AND paired_turns.task_id = NEW.task_id + AND paired_turns.task_updated_at = NEW.task_updated_at + AND paired_turns.role = NEW.role + AND paired_turns.intent_kind = NEW.intent_kind + ); +END; + +CREATE TRIGGER paired_turn_reservations_validate_attempt_insert +BEFORE INSERT ON paired_turn_reservations +BEGIN + SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt_no requires turn_id') + WHERE NEW.turn_attempt_no IS NOT NULL + AND NEW.turn_id IS NULL; + SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt provenance must keep attempt_id and attempt_no in sync') + WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); + SELECT RAISE(ABORT, 'paired_turn_reservations turn_attempt_no must reference a matching paired_turn_attempts row') + WHERE NEW.turn_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts + WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id + AND paired_turn_attempts.turn_id = NEW.turn_id + AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no + AND paired_turn_attempts.task_id = NEW.task_id + AND paired_turn_attempts.task_updated_at = NEW.task_updated_at + AND paired_turn_attempts.role = NEW.turn_role + AND paired_turn_attempts.intent_kind = NEW.intent_kind + ); +END; + +CREATE TRIGGER paired_turn_reservations_validate_attempt_update +BEFORE UPDATE OF turn_id, turn_attempt_id, turn_attempt_no, task_id, task_updated_at, turn_role, intent_kind + ON paired_turn_reservations +BEGIN + SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt_no requires turn_id') + WHERE NEW.turn_attempt_no IS NOT NULL + AND NEW.turn_id IS NULL; + SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt provenance must keep attempt_id and attempt_no in sync') + WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); + SELECT RAISE(ABORT, 'paired_turn_reservations turn_attempt_no must reference a matching paired_turn_attempts row') + WHERE NEW.turn_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts + WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id + AND paired_turn_attempts.turn_id = NEW.turn_id + AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no + AND paired_turn_attempts.task_id = NEW.task_id + AND paired_turn_attempts.task_updated_at = NEW.task_updated_at + AND paired_turn_attempts.role = NEW.turn_role + AND paired_turn_attempts.intent_kind = NEW.intent_kind + ); +END; + +CREATE TRIGGER paired_task_execution_leases_validate_attempt_insert +BEFORE INSERT ON paired_task_execution_leases +BEGIN + SELECT RAISE(ABORT, 'paired_task_execution_leases.turn_attempt_no requires turn_id') + WHERE NEW.turn_attempt_no IS NOT NULL + AND NEW.turn_id IS NULL; + SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt provenance must keep attempt_id and attempt_no in sync') + WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); + SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt_no must reference a matching paired_turn_attempts row') + WHERE NEW.turn_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts + WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id + AND paired_turn_attempts.turn_id = NEW.turn_id + AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no + AND paired_turn_attempts.task_id = NEW.task_id + AND paired_turn_attempts.task_updated_at = NEW.task_updated_at + AND paired_turn_attempts.role = NEW.role + AND paired_turn_attempts.intent_kind = NEW.intent_kind + ); +END; + +CREATE TRIGGER paired_task_execution_leases_validate_attempt_update +BEFORE UPDATE OF turn_id, turn_attempt_id, turn_attempt_no, task_id, task_updated_at, role, intent_kind + ON paired_task_execution_leases +BEGIN + SELECT RAISE(ABORT, 'paired_task_execution_leases.turn_attempt_no requires turn_id') + WHERE NEW.turn_attempt_no IS NOT NULL + AND NEW.turn_id IS NULL; + SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt provenance must keep attempt_id and attempt_no in sync') + WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); + SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt_no must reference a matching paired_turn_attempts row') + WHERE NEW.turn_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts + WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id + AND paired_turn_attempts.turn_id = NEW.turn_id + AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no + AND paired_turn_attempts.task_id = NEW.task_id + AND paired_turn_attempts.task_updated_at = NEW.task_updated_at + AND paired_turn_attempts.role = NEW.role + AND paired_turn_attempts.intent_kind = NEW.intent_kind + ); +END; + +CREATE TRIGGER service_handoffs_validate_attempt_insert +BEFORE INSERT ON service_handoffs +BEGIN + SELECT RAISE(ABORT, 'service_handoffs.turn_attempt_no requires paired turn identity') + WHERE NEW.turn_attempt_no IS NOT NULL + AND ( + NEW.turn_id IS NULL + OR NEW.paired_task_id IS NULL + OR NEW.paired_task_updated_at IS NULL + OR NEW.turn_role IS NULL + OR NEW.turn_intent_kind IS NULL + ); + SELECT RAISE(ABORT, 'service_handoffs turn_attempt provenance must keep attempt_id and attempt_no in sync') + WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); + SELECT RAISE(ABORT, 'service_handoffs turn_attempt_no must reference a matching paired_turn_attempts row') + WHERE NEW.turn_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts + WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id + AND paired_turn_attempts.turn_id = NEW.turn_id + AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no + AND paired_turn_attempts.task_id = NEW.paired_task_id + AND paired_turn_attempts.task_updated_at = NEW.paired_task_updated_at + AND paired_turn_attempts.role = NEW.turn_role + AND paired_turn_attempts.intent_kind = NEW.turn_intent_kind + ); +END; + +CREATE TRIGGER service_handoffs_validate_attempt_update +BEFORE UPDATE OF turn_id, turn_attempt_id, turn_attempt_no, paired_task_id, paired_task_updated_at, turn_role, turn_intent_kind + ON service_handoffs +BEGIN + SELECT RAISE(ABORT, 'service_handoffs.turn_attempt_no requires paired turn identity') + WHERE NEW.turn_attempt_no IS NOT NULL + AND ( + NEW.turn_id IS NULL + OR NEW.paired_task_id IS NULL + OR NEW.paired_task_updated_at IS NULL + OR NEW.turn_role IS NULL + OR NEW.turn_intent_kind IS NULL + ); + SELECT RAISE(ABORT, 'service_handoffs turn_attempt provenance must keep attempt_id and attempt_no in sync') + WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); + SELECT RAISE(ABORT, 'service_handoffs turn_attempt_no must reference a matching paired_turn_attempts row') + WHERE NEW.turn_attempt_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM paired_turn_attempts + WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id + AND paired_turn_attempts.turn_id = NEW.turn_id + AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no + AND paired_turn_attempts.task_id = NEW.paired_task_id + AND paired_turn_attempts.task_updated_at = NEW.paired_task_updated_at + AND paired_turn_attempts.role = NEW.turn_role + AND paired_turn_attempts.intent_kind = NEW.turn_intent_kind + ); +END; `; export function applyBaseSchema(database: Database): void { - database.exec(` - CREATE TABLE IF NOT EXISTS chats ( - jid TEXT PRIMARY KEY, - name TEXT, - last_message_time TEXT, - channel TEXT, - is_group INTEGER DEFAULT 0 - ); - CREATE TABLE IF NOT EXISTS messages ( - id TEXT, - chat_jid TEXT, - sender TEXT, - sender_name TEXT, - content TEXT, - timestamp TEXT, - seq INTEGER, - is_from_me INTEGER, - is_bot_message INTEGER DEFAULT 0, - message_source_kind TEXT NOT NULL DEFAULT 'human', - PRIMARY KEY (id, chat_jid), - FOREIGN KEY (chat_jid) REFERENCES chats(jid) - ); - CREATE INDEX IF NOT EXISTS idx_timestamp ON messages(timestamp); - CREATE INDEX IF NOT EXISTS idx_messages_chat_timestamp ON messages(chat_jid, timestamp DESC); - CREATE TABLE IF NOT EXISTS message_sequence ( - id INTEGER PRIMARY KEY AUTOINCREMENT - ); - - CREATE TABLE IF NOT EXISTS work_items ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - group_folder TEXT NOT NULL, - chat_jid TEXT NOT NULL, - agent_type TEXT NOT NULL, - service_id TEXT, - delivery_role TEXT, - status TEXT NOT NULL DEFAULT 'produced', - start_seq INTEGER, - end_seq INTEGER, - result_payload TEXT NOT NULL, - attachment_payload TEXT, - delivery_attempts INTEGER NOT NULL DEFAULT 0, - delivery_message_id TEXT, - last_error TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - updated_at TEXT NOT NULL DEFAULT (datetime('now')), - delivered_at TEXT, - CHECK (status IN ('produced', 'delivery_retry', 'delivered')), - CHECK (delivery_role IN ('owner', 'reviewer', 'arbiter') OR delivery_role IS NULL) - ); - CREATE INDEX IF NOT EXISTS idx_work_items_status ON work_items(status, updated_at); - CREATE INDEX IF NOT EXISTS idx_work_items_group_agent ON work_items(chat_jid, agent_type, delivery_role, status); - CREATE UNIQUE INDEX IF NOT EXISTS idx_work_items_open - ON work_items(chat_jid, agent_type, IFNULL(delivery_role, '')) - WHERE status IN ('produced', 'delivery_retry'); - - CREATE TABLE IF NOT EXISTS scheduled_tasks ( - id TEXT PRIMARY KEY, - group_folder TEXT NOT NULL, - chat_jid TEXT NOT NULL, - agent_type TEXT, - ci_provider TEXT, - ci_metadata TEXT, - max_duration_ms INTEGER, - status_message_id TEXT, - status_started_at TEXT, - prompt TEXT NOT NULL, - schedule_type TEXT NOT NULL, - schedule_value TEXT NOT NULL, - next_run TEXT, - last_run TEXT, - last_result TEXT, - status TEXT DEFAULT 'active', - created_at TEXT NOT NULL - ); - CREATE INDEX IF NOT EXISTS idx_next_run ON scheduled_tasks(next_run); - CREATE INDEX IF NOT EXISTS idx_status ON scheduled_tasks(status); - - CREATE TABLE IF NOT EXISTS task_run_logs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - task_id TEXT NOT NULL, - run_at TEXT NOT NULL, - duration_ms INTEGER NOT NULL, - status TEXT NOT NULL, - result TEXT, - error TEXT, - FOREIGN KEY (task_id) REFERENCES scheduled_tasks(id) - ); - CREATE INDEX IF NOT EXISTS idx_task_run_logs ON task_run_logs(task_id, run_at); - - CREATE TABLE IF NOT EXISTS router_state ( - key TEXT PRIMARY KEY, - value TEXT NOT NULL - ); - CREATE TABLE IF NOT EXISTS sessions ( - group_folder TEXT NOT NULL, - agent_type TEXT NOT NULL DEFAULT 'claude-code', - session_id TEXT NOT NULL, - PRIMARY KEY (group_folder, agent_type) - ); - CREATE TABLE IF NOT EXISTS paired_projects ( - chat_jid TEXT PRIMARY KEY, - group_folder TEXT NOT NULL, - canonical_work_dir TEXT NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - CREATE TABLE IF NOT EXISTS paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - owner_failure_count INTEGER NOT NULL DEFAULT 0, - reviewer_failure_count INTEGER NOT NULL DEFAULT 0, - owner_step_done_streak INTEGER NOT NULL DEFAULT 0, - finalize_step_done_count INTEGER NOT NULL DEFAULT 0, - task_done_then_user_reopen_count INTEGER NOT NULL DEFAULT 0, - empty_step_done_streak INTEGER NOT NULL DEFAULT 0, - arbiter_intervention_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL DEFAULT 'active', - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (status IN ('active', 'review_ready', 'in_review', 'merge_ready', 'completed', 'arbiter_requested', 'in_arbitration')), - CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL), - CHECK (reviewer_agent_type IN ('claude-code', 'codex') OR reviewer_agent_type IS NULL), - CHECK (arbiter_agent_type IN ('claude-code', 'codex') OR arbiter_agent_type IS NULL) - ); - CREATE INDEX IF NOT EXISTS idx_paired_tasks_chat_status - ON paired_tasks(chat_jid, status, updated_at); - CREATE TABLE IF NOT EXISTS paired_workspaces ( - id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - role TEXT NOT NULL, - workspace_dir TEXT NOT NULL, - snapshot_source_dir TEXT, - snapshot_ref TEXT, - status TEXT NOT NULL DEFAULT 'ready', - snapshot_refreshed_at TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (role IN ('owner', 'reviewer')), - CHECK (status IN ('ready', 'stale')) - ); - CREATE UNIQUE INDEX IF NOT EXISTS idx_paired_workspaces_task_role - ON paired_workspaces(task_id, role); - CREATE TABLE IF NOT EXISTS paired_turn_outputs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - task_id TEXT NOT NULL, - turn_number INTEGER NOT NULL, - role TEXT NOT NULL, - output_text TEXT NOT NULL, attachment_payload TEXT, - verdict TEXT, - created_at TEXT NOT NULL, - UNIQUE(task_id, turn_number, role) - ); - CREATE INDEX IF NOT EXISTS idx_paired_turn_outputs_task - ON paired_turn_outputs(task_id, turn_number); - CREATE TABLE IF NOT EXISTS paired_turn_reservations ( - chat_jid TEXT NOT NULL, - task_id TEXT NOT NULL, - task_status TEXT NOT NULL, - round_trip_count INTEGER NOT NULL DEFAULT 0, - task_updated_at TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_attempt_id TEXT, - turn_attempt_no INTEGER, - turn_role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - scheduled_run_id TEXT, - consumed_run_id TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - consumed_at TEXT, - PRIMARY KEY (chat_jid, task_id, task_updated_at, intent_kind), - FOREIGN KEY (turn_id, turn_attempt_no) - REFERENCES paired_turn_attempts(turn_id, attempt_no) - ON DELETE CASCADE, - CHECK (status IN ('pending', 'completed')), - CHECK (turn_role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - CREATE INDEX IF NOT EXISTS idx_paired_turn_reservations_task - ON paired_turn_reservations(task_id, task_updated_at, status); - CREATE TABLE IF NOT EXISTS paired_turns ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - CREATE INDEX IF NOT EXISTS idx_paired_turns_task - ON paired_turns(task_id, task_updated_at, updated_at); - CREATE TABLE IF NOT EXISTS paired_turn_attempts ( - attempt_id TEXT NOT NULL PRIMARY KEY, - parent_attempt_id TEXT, - parent_handoff_id INTEGER, - continuation_handoff_id INTEGER, - turn_id TEXT NOT NULL, - attempt_no INTEGER NOT NULL, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - active_run_id TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT, - UNIQUE (turn_id, attempt_no), - FOREIGN KEY (parent_attempt_id) - REFERENCES paired_turn_attempts(attempt_id) - ON DELETE CASCADE, - FOREIGN KEY (parent_handoff_id) - REFERENCES service_handoffs(id) - ON DELETE SET NULL, - FOREIGN KEY (continuation_handoff_id) - REFERENCES service_handoffs(id) - ON DELETE SET NULL, - FOREIGN KEY (turn_id) REFERENCES paired_turns(turn_id) ON DELETE CASCADE, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ), - CHECK ( - state IN ( - 'running', - 'delegated', - 'completed', - 'failed', - 'cancelled' - ) - ), - CHECK (executor_agent_type IN ('claude-code', 'codex') OR executor_agent_type IS NULL) - ); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_turn - ON paired_turn_attempts(turn_id, attempt_no); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_task - ON paired_turn_attempts(task_id, task_updated_at, attempt_no); - CREATE TABLE IF NOT EXISTS paired_task_execution_leases ( - task_id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_attempt_id TEXT, - turn_attempt_no INTEGER, - intent_kind TEXT NOT NULL, - claimed_run_id TEXT NOT NULL, - claimed_service_id TEXT NOT NULL, - task_status TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - claimed_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - expires_at TEXT NOT NULL, - FOREIGN KEY (turn_id, turn_attempt_no) - REFERENCES paired_turn_attempts(turn_id, attempt_no) - ON DELETE CASCADE, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - CREATE INDEX IF NOT EXISTS idx_paired_task_execution_leases_expires_at - ON paired_task_execution_leases(expires_at); - CREATE TABLE IF NOT EXISTS channel_owner ( - chat_jid TEXT PRIMARY KEY, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT, - arbiter_service_id TEXT, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - activated_at TEXT, - reason TEXT, - CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL), - CHECK (reviewer_agent_type IN ('claude-code', 'codex') OR reviewer_agent_type IS NULL), - CHECK (arbiter_agent_type IN ('claude-code', 'codex') OR arbiter_agent_type IS NULL) - ); - CREATE TABLE IF NOT EXISTS room_settings ( - chat_jid TEXT PRIMARY KEY, - room_mode TEXT NOT NULL, - mode_source TEXT NOT NULL DEFAULT 'explicit', - name TEXT, - folder TEXT, - trigger_pattern TEXT, - requires_trigger INTEGER DEFAULT 1, - is_main INTEGER DEFAULT 0, - owner_agent_type TEXT, - work_dir TEXT, - created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TEXT NOT NULL, - CHECK (room_mode IN ('single', 'tribunal')), - CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL) - ); - CREATE TABLE IF NOT EXISTS room_role_overrides ( - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - agent_type TEXT NOT NULL, - agent_config_json TEXT, - created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (chat_jid, role), - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK (agent_type IN ('claude-code', 'codex')) - ); - CREATE TABLE IF NOT EXISTS service_handoffs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - paired_task_id TEXT, - paired_task_updated_at TEXT, - turn_id TEXT, - turn_attempt_id TEXT, - turn_attempt_no INTEGER, - turn_intent_kind TEXT, - turn_role TEXT, - source_role TEXT, - source_agent_type TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT, - FOREIGN KEY (turn_id, turn_attempt_no) - REFERENCES paired_turn_attempts(turn_id, attempt_no) - ON DELETE CASCADE, - CHECK (status IN ('pending', 'claimed', 'completed', 'failed')), - CHECK (intended_role IN ('owner', 'reviewer', 'arbiter') OR intended_role IS NULL), - CHECK ( - turn_intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) OR turn_intent_kind IS NULL - ), - CHECK (turn_role IN ('owner', 'reviewer', 'arbiter') OR turn_role IS NULL), - CHECK (source_role IN ('owner', 'reviewer', 'arbiter') OR source_role IS NULL), - CHECK (target_role IN ('owner', 'reviewer', 'arbiter') OR target_role IS NULL) - ); - CREATE INDEX IF NOT EXISTS idx_service_handoffs_target - ON service_handoffs(status, target_role, target_agent_type, created_at); - CREATE TABLE IF NOT EXISTS memories ( - id INTEGER PRIMARY KEY, - scope_kind TEXT NOT NULL, - scope_key TEXT NOT NULL, - content TEXT NOT NULL, - keywords_json TEXT NOT NULL DEFAULT '[]', - memory_kind TEXT, - source_kind TEXT NOT NULL, - source_ref TEXT, - created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - last_used_at TEXT, - archived_at TEXT - ); - CREATE INDEX IF NOT EXISTS idx_memories_scope ON memories(scope_kind, scope_key); - CREATE INDEX IF NOT EXISTS idx_memories_active - ON memories(scope_kind, scope_key, archived_at, created_at); - CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5( - content, - keywords, - content='', - tokenize='unicode61' - ); - CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN - INSERT INTO memories_fts(rowid, content, keywords) - VALUES (new.id, new.content, new.keywords_json); - END; - CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN - INSERT INTO memories_fts(memories_fts, rowid, content, keywords) - VALUES ('delete', old.id, old.content, old.keywords_json); - END; - CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN - INSERT INTO memories_fts(memories_fts, rowid, content, keywords) - VALUES ('delete', old.id, old.content, old.keywords_json); - INSERT INTO memories_fts(rowid, content, keywords) - VALUES (new.id, new.content, new.keywords_json); - END; - `); - database.exec(ROOM_SKILL_OVERRIDES_SCHEMA); + database.exec(BASE_SCHEMA_SQL); } diff --git a/src/db/bootstrap.test.ts b/src/db/bootstrap.test.ts deleted file mode 100644 index 1fd849f..0000000 --- a/src/db/bootstrap.test.ts +++ /dev/null @@ -1,182 +0,0 @@ -import { Database } from 'bun:sqlite'; -import fs from 'fs'; -import path from 'path'; - -import { afterEach, describe, expect, it } from 'vitest'; - -import { applyBaseSchema } from './base-schema.js'; -import { initializeDatabaseSchema } from './bootstrap.js'; -import { applyVersionedSchemaMigrations } from './migrations/index.js'; -import { applyLegacySchemaMigrations } from './schema.js'; - -function tableColumns(database: Database, table: string): string[] { - return ( - database.prepare(`PRAGMA table_info(${table})`).all() as Array<{ - name: string; - }> - ).map((column) => column.name); -} - -function getAppliedSchemaMigrations( - database: Database, -): Array<{ version: number; name: string }> { - return database - .prepare( - `SELECT version, name - FROM schema_migrations - ORDER BY version ASC`, - ) - .all() as Array<{ version: number; name: string }>; -} - -function getExpectedSchemaMigrations(): Array<{ - version: number; - name: string; -}> { - return [ - { version: 1, name: 'legacy_schema_bundle' }, - { version: 2, name: 'scheduled_task_columns' }, - { version: 3, name: 'room_registration_canonical_columns' }, - { version: 4, name: 'message_seq_and_work_item_indexes' }, - { version: 5, name: 'sessions_composite_key' }, - { version: 6, name: 'chat_channel_metadata' }, - { version: 7, name: 'runtime_service_metadata' }, - { version: 8, name: 'paired_task_schema_cleanup' }, - { version: 9, name: 'paired_workspace_project_schema_cleanup' }, - { version: 10, name: 'paired_turn_provenance_upgrade' }, - { version: 11, name: 'owner_failure_count' }, - { version: 12, name: 'paired_verdict_and_step_telemetry' }, - { version: 13, name: 'message_source_kind' }, - { version: 14, name: 'work_item_attachments' }, - { version: 15, name: 'turn_progress_text' }, - { version: 16, name: 'room_skill_overrides' }, - { version: 17, name: 'scheduled_task_room_role' }, - { version: 18, name: 'paired_turn_output_attachments' }, - { version: 19, name: 'turn_progress_text_recovery' }, - { version: 20, name: 'arbiter_intervention_count' }, - { version: 21, name: 'reviewer_failure_count' }, - { version: 22, name: 'turn_progress_text_compat' }, - ]; -} - -describe('initializeDatabaseSchema', () => { - const tempDirs: string[] = []; - - afterEach(() => { - for (const tempDir of tempDirs.splice(0)) { - fs.rmSync(tempDir, { recursive: true, force: true }); - } - }); - - it('records the legacy schema bundle as version 1 on a fresh database', () => { - const database = new Database(':memory:'); - - try { - initializeDatabaseSchema(database); - - expect(getAppliedSchemaMigrations(database)).toEqual( - getExpectedSchemaMigrations(), - ); - } finally { - database.close(); - } - }); - - it('does not duplicate schema migration rows on repeated initialization', () => { - const database = new Database(':memory:'); - - try { - initializeDatabaseSchema(database); - initializeDatabaseSchema(database); - - expect(getAppliedSchemaMigrations(database)).toEqual( - getExpectedSchemaMigrations(), - ); - } finally { - database.close(); - } - }); - - it('backfills schema migration tracking for an existing pre-versioned database', () => { - const tempDir = fs.mkdtempSync( - path.join('/tmp', 'ejclaw-schema-migrations-'), - ); - tempDirs.push(tempDir); - const dbPath = path.join(tempDir, 'messages.db'); - const legacyDb = new Database(dbPath); - - try { - applyBaseSchema(legacyDb); - applyLegacySchemaMigrations(legacyDb, { - assistantName: 'Andy', - }); - } finally { - legacyDb.close(); - } - - const reopened = new Database(dbPath); - - try { - initializeDatabaseSchema(reopened); - - expect(getAppliedSchemaMigrations(reopened)).toEqual( - getExpectedSchemaMigrations(), - ); - } finally { - reopened.close(); - } - }); - - it('backfills turn_progress_text columns when version 15 was recorded under a different name', () => { - const database = new Database(':memory:'); - - try { - applyBaseSchema(database); - - // Reproduce a deployment that first shipped reviewer_failure_count as a - // local migration numbered 15, before turn_progress_text claimed that - // version upstream. The runner skips by version number, so migration 015 - // (turn_progress_text) would otherwise never run on this database. - database.exec(` - CREATE TABLE schema_migrations ( - version INTEGER PRIMARY KEY, - name TEXT NOT NULL, - applied_at TEXT NOT NULL DEFAULT (datetime('now')) - ) - `); - const collidedNames: Record = { - 15: 'reviewer_failure_count', - }; - for (let version = 1; version <= 15; version += 1) { - database - .prepare( - 'INSERT INTO schema_migrations (version, name) VALUES (?, ?)', - ) - .run(version, collidedNames[version] ?? `legacy_${version}`); - } - // Precondition: the collided database is missing the progress columns. - expect(tableColumns(database, 'paired_turns')).not.toContain( - 'progress_text', - ); - - applyVersionedSchemaMigrations(database, { assistantName: 'Andy' }); - - const columns = tableColumns(database, 'paired_turns'); - expect(columns).toContain('progress_text'); - expect(columns).toContain('progress_updated_at'); - - // The pre-existing version-15 row is left untouched; the compat migration - // (version 22 after merge-renumbering) is what reconciles the schema. - const version15 = database - .prepare('SELECT name FROM schema_migrations WHERE version = 15') - .get() as { name: string }; - expect(version15.name).toBe('reviewer_failure_count'); - const version22 = database - .prepare('SELECT name FROM schema_migrations WHERE version = 22') - .get() as { name: string } | null; - expect(version22?.name).toBe('turn_progress_text_compat'); - } finally { - database.close(); - } - }); -}); diff --git a/src/db/bootstrap.ts b/src/db/bootstrap.ts index 84511e1..24bb269 100644 --- a/src/db/bootstrap.ts +++ b/src/db/bootstrap.ts @@ -2,24 +2,8 @@ import { Database } from 'bun:sqlite'; import fs from 'fs'; import path from 'path'; -import { ASSISTANT_NAME, STORE_DIR } from '../config.js'; -import { applyBaseSchema } from './base-schema.js'; -import { applyVersionedSchemaMigrations } from './migrations/index.js'; - -function isFreshDatabase(database: Database): boolean { - const row = database - .prepare( - ` - SELECT COUNT(*) AS count - FROM sqlite_master - WHERE type = 'table' - AND name NOT LIKE 'sqlite_%' - AND name != 'schema_migrations' - `, - ) - .get() as { count: number }; - return row.count === 0; -} +import { STORE_DIR } from '../config.js'; +import { applySchemaMigrations } from './migrations/index.js'; function setForeignKeys(database: Database, enabled: boolean): void { database.exec(`PRAGMA foreign_keys = ${enabled ? 'ON' : 'OFF'}`); @@ -46,12 +30,7 @@ function assertNoForeignKeyViolations(database: Database): void { export function initializeDatabaseSchema(database: Database): void { setForeignKeys(database, false); - if (isFreshDatabase(database)) { - applyBaseSchema(database); - } - applyVersionedSchemaMigrations(database, { - assistantName: ASSISTANT_NAME, - }); + applySchemaMigrations(database); setForeignKeys(database, true); assertNoForeignKeyViolations(database); } diff --git a/src/db/canonical-role-metadata.ts b/src/db/canonical-role-metadata.ts index ab8f0cf..8f6d5e6 100644 --- a/src/db/canonical-role-metadata.ts +++ b/src/db/canonical-role-metadata.ts @@ -2,18 +2,12 @@ import { ARBITER_AGENT_TYPE, OWNER_AGENT_TYPE, REVIEWER_AGENT_TYPE, - SERVICE_SESSION_SCOPE, } from '../config.js'; import { inferAgentTypeFromServiceShadow, - inferRoleFromServiceShadow, resolveRoleServiceShadow, } from '../role-service-shadow.js'; -import { - normalizePairedRoomRoleOrNull, - type AgentType, - type PairedRoomRole, -} from '../types.js'; +import { type AgentType, type PairedRoomRole } from '../types.js'; import { normalizeStoredAgentType } from './room-registration.js'; interface RequiredRoleMetadataInput { @@ -339,190 +333,3 @@ export function readCanonicalChannelOwnerLeaseMetadata(input: { arbiterServiceId: arbiter.serviceId, }; } - -function assertStoredRoleMatchesServiceShadow(args: { - context: string; - storedRole: PairedRoomRole | null; - agentType: AgentType | null; - serviceId: string | null; - roleField: 'source_role' | 'target_role'; - serviceField: 'source_service_id' | 'target_service_id'; -}): void { - if (!args.storedRole || !args.agentType || !args.serviceId) { - return; - } - - const inferredRole = inferRoleFromServiceShadow( - args.agentType, - args.serviceId, - ); - if (inferredRole && inferredRole !== args.storedRole) { - throw new Error( - `${args.context}: ${args.roleField} conflicts with ${args.serviceField}`, - ); - } -} - -export interface CanonicalServiceHandoffMetadata { - sourceRole: PairedRoomRole | null; - targetRole: PairedRoomRole | null; - sourceAgentType: AgentType | null; - targetAgentType: AgentType; - sourceServiceId: string; - targetServiceId: string; -} - -export function fillCanonicalServiceHandoffMetadata(input: { - id: number | string; - chat_jid: string; - source_service_id?: string | null; - target_service_id?: string | null; - source_role?: string | null; - target_role?: string | null; - intended_role?: string | null; - source_agent_type?: string | null; - target_agent_type?: string | null; -}): CanonicalServiceHandoffMetadata { - const context = `service_handoffs(${input.id})`; - const sourceRole = - normalizePairedRoomRoleOrNull(input.source_role) ?? - normalizePairedRoomRoleOrNull(input.intended_role); - const targetRole = - normalizePairedRoomRoleOrNull(input.target_role) ?? - normalizePairedRoomRoleOrNull(input.intended_role); - const storedSourceAgentType = normalizeStoredAgentType( - input.source_agent_type, - ); - const inferredSourceAgentType = inferAgentTypeFromServiceShadow( - input.source_service_id, - ); - if ( - storedSourceAgentType && - inferredSourceAgentType && - storedSourceAgentType !== inferredSourceAgentType - ) { - throw new Error( - `${context}: source_agent_type conflicts with source_service_id`, - ); - } - const storedTargetAgentType = normalizeStoredAgentType( - input.target_agent_type, - ); - const inferredTargetAgentType = inferAgentTypeFromServiceShadow( - input.target_service_id, - ); - if ( - storedTargetAgentType && - inferredTargetAgentType && - storedTargetAgentType !== inferredTargetAgentType - ) { - throw new Error( - `${context}: target_agent_type conflicts with target_service_id`, - ); - } - const targetAgentType = storedTargetAgentType ?? inferredTargetAgentType; - - if (!targetAgentType) { - throw new Error( - `${context}: cannot resolve target_agent_type from stored row metadata`, - ); - } - - const targetServiceId = - input.target_service_id ?? - (targetRole != null - ? resolveRoleServiceShadow(targetRole, targetAgentType) - : null); - if (!targetServiceId) { - throw new Error( - `${context}: cannot resolve target_service_id from stored row metadata`, - ); - } - - const sourceAgentTypeForShadow = - storedSourceAgentType ?? inferredSourceAgentType ?? null; - const sourceServiceId = - input.source_service_id ?? - (sourceRole != null && sourceAgentTypeForShadow != null - ? resolveRoleServiceShadow(sourceRole, sourceAgentTypeForShadow) - : null) ?? - SERVICE_SESSION_SCOPE; - const sourceAgentType = - storedSourceAgentType ?? - inferredSourceAgentType ?? - inferAgentTypeFromServiceShadow(sourceServiceId) ?? - null; - - assertStoredRoleMatchesServiceShadow({ - context, - storedRole: sourceRole, - agentType: sourceAgentType, - serviceId: sourceServiceId, - roleField: 'source_role', - serviceField: 'source_service_id', - }); - assertStoredRoleMatchesServiceShadow({ - context, - storedRole: targetRole, - agentType: targetAgentType, - serviceId: targetServiceId, - roleField: 'target_role', - serviceField: 'target_service_id', - }); - - return { - sourceRole, - targetRole, - sourceAgentType, - targetAgentType, - sourceServiceId, - targetServiceId, - }; -} - -interface ExecutionLeaseMetadataRow { - rowid: number; - role: string; - owner_service_id?: string | null; - reviewer_service_id?: string | null; - arbiter_service_id?: string | null; - owner_agent_type?: string | null; - reviewer_agent_type?: string | null; - arbiter_agent_type?: string | null; -} - -export function inferExecutionLeaseServiceIdFromCanonicalMetadata( - row: ExecutionLeaseMetadataRow, -): string | null { - switch (row.role) { - case 'owner': { - const owner = resolveFilledOptionalRoleMetadata({ - context: `paired_task_execution_leases(${row.rowid})`, - role: 'owner', - storedAgentType: row.owner_agent_type, - storedServiceId: row.owner_service_id, - }); - return owner.serviceId; - } - case 'reviewer': { - const reviewer = resolveFilledOptionalRoleMetadata({ - context: `paired_task_execution_leases(${row.rowid})`, - role: 'reviewer', - storedAgentType: row.reviewer_agent_type, - storedServiceId: row.reviewer_service_id, - }); - return reviewer.serviceId; - } - case 'arbiter': { - const arbiter = resolveFilledOptionalRoleMetadata({ - context: `paired_task_execution_leases(${row.rowid})`, - role: 'arbiter', - storedAgentType: row.arbiter_agent_type, - storedServiceId: row.arbiter_service_id, - }); - return arbiter.serviceId; - } - default: - return null; - } -} diff --git a/src/db/migrations/001_legacy-schema-bundle.ts b/src/db/migrations/001_legacy-schema-bundle.ts deleted file mode 100644 index bc51eeb..0000000 --- a/src/db/migrations/001_legacy-schema-bundle.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { applyBaseSchema } from '../base-schema.js'; -import { applyLegacySchemaMigrations } from '../schema.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -const LEGACY_BASELINE_TABLES = [ - 'chats', - 'messages', - 'message_sequence', - 'work_items', - 'scheduled_tasks', - 'task_run_logs', - 'router_state', - 'sessions', - 'paired_projects', - 'paired_tasks', - 'paired_workspaces', - 'paired_turn_outputs', - 'paired_turn_reservations', - 'paired_turns', - 'paired_turn_attempts', - 'paired_task_execution_leases', - 'channel_owner', - 'room_settings', - 'room_role_overrides', - 'service_handoffs', - 'memories', -] as const; - -function tableExists( - database: Parameters[0], - tableName: string, -): boolean { - const row = database - .prepare( - ` - SELECT 1 - FROM sqlite_master - WHERE type = 'table' - AND name = ? - `, - ) - .get(tableName); - return row !== null && row !== undefined; -} - -function requiresLegacyBaseSchemaPreflight( - database: Parameters[0], -): boolean { - return LEGACY_BASELINE_TABLES.some( - (tableName) => !tableExists(database, tableName), - ); -} - -export const LEGACY_SCHEMA_BUNDLE_MIGRATION = { - version: 1, - name: 'legacy_schema_bundle', - alwaysRun: true, - apply(database, args) { - // Some pre-versioned test and upgrade fixtures contain only a subset of the - // current base tables. Re-establish the baseline before applying the legacy - // monolithic upgrade bundle. - if (requiresLegacyBaseSchemaPreflight(database)) { - applyBaseSchema(database); - } - applyLegacySchemaMigrations(database, args); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/002_scheduled-task-columns.ts b/src/db/migrations/002_scheduled-task-columns.ts deleted file mode 100644 index fe18641..0000000 --- a/src/db/migrations/002_scheduled-task-columns.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; -import { tryExecMigration } from './helpers.js'; - -export const SCHEDULED_TASK_COLUMNS_MIGRATION = { - version: 2, - name: 'scheduled_task_columns', - apply(database) { - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN context_mode TEXT DEFAULT 'isolated'`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN agent_type TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN ci_provider TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN ci_metadata TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN max_duration_ms INTEGER`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN status_message_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN status_started_at TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN suspended_until TEXT`, - ); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/003_room-registration-canonical-columns.ts b/src/db/migrations/003_room-registration-canonical-columns.ts deleted file mode 100644 index 200026b..0000000 --- a/src/db/migrations/003_room-registration-canonical-columns.ts +++ /dev/null @@ -1,72 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; -import { tableHasColumn, tryExecMigration } from './helpers.js'; - -export const ROOM_REGISTRATION_CANONICAL_COLUMNS_MIGRATION = { - version: 3, - name: 'room_registration_canonical_columns', - apply(database) { - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN mode_source TEXT NOT NULL DEFAULT 'explicit'`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN name TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN folder TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN trigger_pattern TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN requires_trigger INTEGER DEFAULT 1`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN is_main INTEGER DEFAULT 0`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN owner_agent_type TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN work_dir TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE room_settings ADD COLUMN created_at TEXT`, - ); - - if (tableHasColumn(database, 'room_settings', 'created_at')) { - database.exec(` - UPDATE room_settings - SET created_at = COALESCE(created_at, updated_at, CURRENT_TIMESTAMP) - `); - } - - database.exec(` - CREATE TABLE IF NOT EXISTS room_role_overrides ( - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - agent_type TEXT NOT NULL, - agent_config_json TEXT, - created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (chat_jid, role), - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK (agent_type IN ('claude-code', 'codex')) - ) - `); - - database.exec( - `UPDATE room_settings - SET mode_source = 'explicit' - WHERE COALESCE(mode_source, '') NOT IN ('explicit', 'inferred')`, - ); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/004_message-seq-and-work-item-indexes.ts b/src/db/migrations/004_message-seq-and-work-item-indexes.ts deleted file mode 100644 index c842f3a..0000000 --- a/src/db/migrations/004_message-seq-and-work-item-indexes.ts +++ /dev/null @@ -1,36 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; -import { backfillMessageSeq, tryExecMigration } from './helpers.js'; - -export const MESSAGE_SEQ_AND_WORK_ITEM_INDEXES_MIGRATION = { - version: 4, - name: 'message_seq_and_work_item_indexes', - apply(database, args) { - try { - database.exec( - `ALTER TABLE messages ADD COLUMN is_bot_message INTEGER DEFAULT 0`, - ); - database - .prepare(`UPDATE messages SET is_bot_message = 1 WHERE content LIKE ?`) - .run(`${args.assistantName}:%`); - } catch { - /* column already exists */ - } - - tryExecMigration(database, `ALTER TABLE messages ADD COLUMN seq INTEGER`); - backfillMessageSeq(database); - - database.exec(` - CREATE UNIQUE INDEX IF NOT EXISTS idx_messages_seq ON messages(seq); - CREATE INDEX IF NOT EXISTS idx_messages_chat_jid_seq ON messages(chat_jid, seq); - `); - database.exec(`DROP INDEX IF EXISTS idx_work_items_group_agent;`); - database.exec(`DROP INDEX IF EXISTS idx_work_items_open;`); - database.exec(` - CREATE INDEX IF NOT EXISTS idx_work_items_group_agent - ON work_items(chat_jid, agent_type, service_id, delivery_role, status); - CREATE UNIQUE INDEX IF NOT EXISTS idx_work_items_open - ON work_items(chat_jid, agent_type, IFNULL(service_id, ''), IFNULL(delivery_role, '')) - WHERE status IN ('produced', 'delivery_retry'); - `); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/005_sessions-composite-key.ts b/src/db/migrations/005_sessions-composite-key.ts deleted file mode 100644 index 97c6d25..0000000 --- a/src/db/migrations/005_sessions-composite-key.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { inferAgentTypeFromServiceShadow } from '../../role-service-shadow.js'; -import { - backfillLegacyServiceSessions, - dropLegacyServiceSessionsTable, - migrateSessionsTableToCompositePk, -} from '../sessions.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const SESSIONS_COMPOSITE_KEY_MIGRATION = { - version: 5, - name: 'sessions_composite_key', - apply(database) { - migrateSessionsTableToCompositePk(database, 'claude-code'); - backfillLegacyServiceSessions(database, inferAgentTypeFromServiceShadow); - dropLegacyServiceSessionsTable(database); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/006_chat-channel-metadata.ts b/src/db/migrations/006_chat-channel-metadata.ts deleted file mode 100644 index 5bfadf6..0000000 --- a/src/db/migrations/006_chat-channel-metadata.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; - -export const CHAT_CHANNEL_METADATA_MIGRATION = { - version: 6, - name: 'chat_channel_metadata', - apply(database) { - try { - database.exec(`ALTER TABLE chats ADD COLUMN channel TEXT`); - database.exec(`ALTER TABLE chats ADD COLUMN is_group INTEGER DEFAULT 0`); - database.exec( - `UPDATE chats SET channel = 'whatsapp', is_group = 1 WHERE jid LIKE '%@g.us'`, - ); - database.exec( - `UPDATE chats SET channel = 'whatsapp', is_group = 0 WHERE jid LIKE '%@s.whatsapp.net'`, - ); - database.exec( - `UPDATE chats SET channel = 'discord', is_group = 1 WHERE jid LIKE 'dc:%'`, - ); - database.exec( - `UPDATE chats SET channel = 'telegram', is_group = 1 WHERE jid LIKE 'tg:%'`, - ); - } catch { - /* columns already exist */ - } - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/007_runtime-service-metadata.ts b/src/db/migrations/007_runtime-service-metadata.ts deleted file mode 100644 index 70a076e..0000000 --- a/src/db/migrations/007_runtime-service-metadata.ts +++ /dev/null @@ -1,509 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { SERVICE_SESSION_SCOPE, normalizeServiceId } from '../../config.js'; -import { resolveRoleServiceShadow } from '../../role-service-shadow.js'; -import { - fillCanonicalChannelOwnerLeaseMetadata, - fillCanonicalPairedTaskMetadata, - fillCanonicalServiceHandoffMetadata, - inferExecutionLeaseServiceIdFromCanonicalMetadata, -} from '../canonical-role-metadata.js'; -import { normalizeStoredAgentType } from '../room-registration.js'; -import { - getTableColumns, - tableHasColumn, - tryExecMigration, -} from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -interface StoredPairedTaskServiceRow { - rowid: number; - id: string; - owner_service_id?: string | null; - reviewer_service_id?: string | null; - owner_agent_type?: string | null; - reviewer_agent_type?: string | null; - arbiter_agent_type?: string | null; -} - -interface StoredChannelOwnerLeaseServiceRow { - rowid: number; - chat_jid: string; - owner_service_id?: string | null; - reviewer_service_id?: string | null; - arbiter_service_id?: string | null; - owner_agent_type?: string | null; - reviewer_agent_type?: string | null; - arbiter_agent_type?: string | null; -} - -interface StoredServiceHandoffServiceRow { - id: number; - chat_jid: string; - group_folder: string; - source_service_id?: string | null; - target_service_id?: string | null; - source_role?: string | null; - target_role?: string | null; - source_agent_type?: string | null; - target_agent_type?: string | null; - intended_role?: string | null; -} - -interface StoredWorkItemServiceRow { - id: number; - agent_type?: string | null; - service_id?: string | null; - delivery_role?: string | null; -} - -interface LegacyExecutionLeaseServiceRow { - rowid: number; - role: string; - owner_service_id?: string | null; - reviewer_service_id?: string | null; - arbiter_service_id?: string | null; - owner_agent_type?: string | null; - reviewer_agent_type?: string | null; - arbiter_agent_type?: string | null; -} - -function normalizePairedRole( - role: string | null | undefined, -): 'owner' | 'reviewer' | 'arbiter' | null { - return role === 'owner' || role === 'reviewer' || role === 'arbiter' - ? role - : null; -} - -function backfillCanonicalPairedTaskServiceIds(database: Database): void { - if ( - !tableHasColumn(database, 'paired_tasks', 'owner_service_id') || - !tableHasColumn(database, 'paired_tasks', 'reviewer_service_id') - ) { - return; - } - - const rows = database - .prepare( - ` - SELECT - rowid, - id, - owner_service_id, - reviewer_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type - FROM paired_tasks - `, - ) - .all() as StoredPairedTaskServiceRow[]; - if (rows.length === 0) { - return; - } - - const update = database.prepare( - ` - UPDATE paired_tasks - SET owner_service_id = ?, - reviewer_service_id = ?, - owner_agent_type = ?, - reviewer_agent_type = ?, - arbiter_agent_type = ? - WHERE rowid = ? - `, - ); - const tx = database.transaction((taskRows: StoredPairedTaskServiceRow[]) => { - for (const row of taskRows) { - const { - ownerAgentType, - reviewerAgentType, - arbiterAgentType, - ownerServiceId, - reviewerServiceId, - } = fillCanonicalPairedTaskMetadata({ - id: row.id, - owner_service_id: row.owner_service_id, - reviewer_service_id: row.reviewer_service_id, - owner_agent_type: row.owner_agent_type, - reviewer_agent_type: row.reviewer_agent_type, - arbiter_agent_type: row.arbiter_agent_type, - }); - - if ( - row.owner_service_id === ownerServiceId && - row.reviewer_service_id === reviewerServiceId && - row.owner_agent_type === ownerAgentType && - row.reviewer_agent_type === reviewerAgentType && - row.arbiter_agent_type === arbiterAgentType - ) { - continue; - } - - update.run( - ownerServiceId, - reviewerServiceId, - ownerAgentType, - reviewerAgentType, - arbiterAgentType, - row.rowid, - ); - } - }); - - tx(rows); -} - -function backfillCanonicalChannelOwnerServiceIds(database: Database): void { - if (!tableHasColumn(database, 'channel_owner', 'owner_service_id')) { - return; - } - - const rows = database - .prepare( - ` - SELECT - rowid, - chat_jid, - owner_service_id, - reviewer_service_id, - arbiter_service_id, - owner_agent_type, - reviewer_agent_type, - arbiter_agent_type - FROM channel_owner - `, - ) - .all() as StoredChannelOwnerLeaseServiceRow[]; - if (rows.length === 0) { - return; - } - - const update = database.prepare( - ` - UPDATE channel_owner - SET owner_service_id = ?, - reviewer_service_id = ?, - arbiter_service_id = ?, - owner_agent_type = ?, - reviewer_agent_type = ?, - arbiter_agent_type = ? - WHERE rowid = ? - `, - ); - const tx = database.transaction( - (leaseRows: StoredChannelOwnerLeaseServiceRow[]) => { - for (const row of leaseRows) { - const { - ownerAgentType, - reviewerAgentType, - arbiterAgentType, - ownerServiceId, - reviewerServiceId, - arbiterServiceId, - } = fillCanonicalChannelOwnerLeaseMetadata({ - chat_jid: row.chat_jid, - owner_service_id: row.owner_service_id, - reviewer_service_id: row.reviewer_service_id, - arbiter_service_id: row.arbiter_service_id, - owner_agent_type: row.owner_agent_type, - reviewer_agent_type: row.reviewer_agent_type, - arbiter_agent_type: row.arbiter_agent_type, - }); - - if ( - row.owner_service_id === ownerServiceId && - row.reviewer_service_id === reviewerServiceId && - row.arbiter_service_id === arbiterServiceId && - row.owner_agent_type === ownerAgentType && - row.reviewer_agent_type === reviewerAgentType && - row.arbiter_agent_type === arbiterAgentType - ) { - continue; - } - - update.run( - ownerServiceId, - reviewerServiceId, - arbiterServiceId, - ownerAgentType, - reviewerAgentType, - arbiterAgentType, - row.rowid, - ); - } - }, - ); - - tx(rows); -} - -function backfillCanonicalServiceHandoffServiceIds(database: Database): void { - if (!tableHasColumn(database, 'service_handoffs', 'source_service_id')) { - return; - } - - const rows = database - .prepare( - ` - SELECT - id, - chat_jid, - group_folder, - source_service_id, - target_service_id, - source_role, - target_role, - source_agent_type, - target_agent_type, - intended_role - FROM service_handoffs - `, - ) - .all() as StoredServiceHandoffServiceRow[]; - if (rows.length === 0) { - return; - } - - const update = database.prepare( - ` - UPDATE service_handoffs - SET source_service_id = ?, - target_service_id = ?, - source_agent_type = ?, - target_agent_type = ?, - source_role = ?, - target_role = ? - WHERE id = ? - `, - ); - const tx = database.transaction( - (handoffRows: StoredServiceHandoffServiceRow[]) => { - for (const row of handoffRows) { - const { - sourceRole, - targetRole, - sourceAgentType, - targetAgentType, - sourceServiceId, - targetServiceId, - } = fillCanonicalServiceHandoffMetadata({ - id: row.id, - chat_jid: row.chat_jid, - source_service_id: row.source_service_id, - target_service_id: row.target_service_id, - source_role: row.source_role, - target_role: row.target_role, - intended_role: row.intended_role, - source_agent_type: row.source_agent_type, - target_agent_type: row.target_agent_type, - }); - - if ( - row.source_service_id === sourceServiceId && - row.target_service_id === targetServiceId && - row.source_agent_type === (sourceAgentType ?? null) && - row.target_agent_type === targetAgentType && - row.source_role === sourceRole && - row.target_role === targetRole - ) { - continue; - } - - update.run( - sourceServiceId, - targetServiceId, - sourceAgentType, - targetAgentType, - sourceRole, - targetRole, - row.id, - ); - } - }, - ); - - tx(rows); -} - -function backfillCanonicalWorkItemServiceIds(database: Database): void { - if (!tableHasColumn(database, 'work_items', 'service_id')) { - return; - } - - const rows = database - .prepare( - `SELECT id, agent_type, service_id, delivery_role - FROM work_items`, - ) - .all() as StoredWorkItemServiceRow[]; - if (rows.length === 0) { - return; - } - - const update = database.prepare( - `UPDATE work_items - SET service_id = ? - WHERE id = ?`, - ); - const tx = database.transaction( - (workItemRows: StoredWorkItemServiceRow[]) => { - for (const row of workItemRows) { - const agentType = - normalizeStoredAgentType(row.agent_type) ?? 'claude-code'; - const deliveryRole = normalizePairedRole(row.delivery_role) ?? 'owner'; - const serviceId = - (row.service_id ? normalizeServiceId(row.service_id) : null) ?? - resolveRoleServiceShadow(deliveryRole, agentType) ?? - SERVICE_SESSION_SCOPE; - - if (row.service_id === serviceId) { - continue; - } - - update.run(serviceId, row.id); - } - }, - ); - - tx(rows); -} - -function backfillLegacyExecutionLeaseServiceShadows(database: Database): void { - if ( - !tableHasColumn( - database, - 'paired_task_execution_leases', - 'claimed_service_id', - ) - ) { - return; - } - - const pairedTasksTable = database - .prepare( - ` - SELECT 1 - FROM sqlite_master - WHERE type = 'table' - AND name = 'paired_tasks' - `, - ) - .get(); - if (!pairedTasksTable) { - return; - } - - const pairedTaskColumns = getTableColumns(database, 'paired_tasks'); - const selectPairedTaskColumn = (columnName: string): string => - pairedTaskColumns.includes(columnName) - ? `paired_tasks.${columnName} AS ${columnName}` - : `NULL AS ${columnName}`; - - const rows = database - .prepare( - ` - SELECT - paired_task_execution_leases.rowid AS rowid, - paired_task_execution_leases.role AS role, - ${selectPairedTaskColumn('owner_service_id')}, - ${selectPairedTaskColumn('reviewer_service_id')}, - ${selectPairedTaskColumn('arbiter_service_id')}, - ${selectPairedTaskColumn('owner_agent_type')}, - ${selectPairedTaskColumn('reviewer_agent_type')}, - ${selectPairedTaskColumn('arbiter_agent_type')} - FROM paired_task_execution_leases - LEFT JOIN paired_tasks - ON paired_tasks.id = paired_task_execution_leases.task_id - WHERE paired_task_execution_leases.claimed_service_id IS NULL - `, - ) - .all() as LegacyExecutionLeaseServiceRow[]; - - if (rows.length === 0) { - return; - } - - const update = database.prepare( - ` - UPDATE paired_task_execution_leases - SET claimed_service_id = ? - WHERE rowid = ? - AND claimed_service_id IS NULL - `, - ); - const tx = database.transaction( - (leaseRows: LegacyExecutionLeaseServiceRow[]) => { - for (const row of leaseRows) { - const claimedServiceId = - inferExecutionLeaseServiceIdFromCanonicalMetadata(row); - if (!claimedServiceId) { - continue; - } - - update.run(claimedServiceId, row.rowid); - } - }, - ); - - tx(rows); -} - -export const RUNTIME_SERVICE_METADATA_MIGRATION = { - version: 7, - name: 'runtime_service_metadata', - apply(database) { - for (const statement of [ - `ALTER TABLE service_handoffs ADD COLUMN intended_role TEXT`, - `ALTER TABLE service_handoffs ADD COLUMN source_role TEXT`, - `ALTER TABLE service_handoffs ADD COLUMN target_role TEXT`, - `ALTER TABLE service_handoffs ADD COLUMN source_agent_type TEXT`, - `ALTER TABLE service_handoffs ADD COLUMN source_service_id TEXT`, - `ALTER TABLE service_handoffs ADD COLUMN target_service_id TEXT`, - `ALTER TABLE paired_tasks ADD COLUMN owner_service_id TEXT`, - `ALTER TABLE paired_tasks ADD COLUMN reviewer_service_id TEXT`, - `ALTER TABLE paired_tasks ADD COLUMN owner_agent_type TEXT`, - `ALTER TABLE paired_tasks ADD COLUMN reviewer_agent_type TEXT`, - `ALTER TABLE paired_tasks ADD COLUMN arbiter_agent_type TEXT`, - `ALTER TABLE paired_task_execution_leases ADD COLUMN expires_at TEXT`, - `ALTER TABLE paired_task_execution_leases ADD COLUMN claimed_service_id TEXT`, - `ALTER TABLE work_items ADD COLUMN delivery_role TEXT`, - `ALTER TABLE work_items ADD COLUMN service_id TEXT`, - ]) { - tryExecMigration(database, statement); - } - - for (const column of [ - 'owner_service_id', - 'reviewer_service_id', - 'arbiter_service_id', - 'owner_agent_type', - 'reviewer_agent_type', - 'arbiter_agent_type', - ]) { - tryExecMigration( - database, - `ALTER TABLE channel_owner ADD COLUMN ${column} TEXT`, - ); - } - - database.exec(` - UPDATE paired_task_execution_leases - SET expires_at = COALESCE( - expires_at, - strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '+10 minutes') - ) - `); - database.exec(` - CREATE INDEX IF NOT EXISTS idx_paired_task_execution_leases_expires_at - ON paired_task_execution_leases(expires_at) - `); - - backfillCanonicalPairedTaskServiceIds(database); - backfillCanonicalChannelOwnerServiceIds(database); - backfillCanonicalServiceHandoffServiceIds(database); - backfillCanonicalWorkItemServiceIds(database); - backfillLegacyExecutionLeaseServiceShadows(database); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/008_paired-task-schema-cleanup.ts b/src/db/migrations/008_paired-task-schema-cleanup.ts deleted file mode 100644 index 1fbec3d..0000000 --- a/src/db/migrations/008_paired-task-schema-cleanup.ts +++ /dev/null @@ -1,124 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; -import { getTableColumns, tryExecMigration } from './helpers.js'; - -export const PAIRED_TASK_SCHEMA_CLEANUP_MIGRATION = { - version: 8, - name: 'paired_task_schema_cleanup', - apply(database) { - const pairedTasksSqlRow = database - .prepare( - `SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'paired_tasks'`, - ) - .get() as { sql?: string } | undefined; - const pairedTasksSql = pairedTasksSqlRow?.sql || ''; - const pairedTasksNeedsRebuild = - pairedTasksSql && - (pairedTasksSql.includes('task_policy') || - !pairedTasksSql.includes('round_trip_count')); - if (pairedTasksNeedsRebuild) { - database.exec(`DROP TABLE IF EXISTS paired_tasks`); - database.exec(` - CREATE TABLE IF NOT EXISTS paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL DEFAULT 'active', - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (status IN ('active', 'review_ready', 'in_review', 'merge_ready', 'completed', 'arbiter_requested', 'in_arbitration')), - CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL), - CHECK (reviewer_agent_type IN ('claude-code', 'codex') OR reviewer_agent_type IS NULL), - CHECK (arbiter_agent_type IN ('claude-code', 'codex') OR arbiter_agent_type IS NULL) - ); - CREATE INDEX IF NOT EXISTS idx_paired_tasks_chat_status - ON paired_tasks(chat_jid, status, updated_at); - `); - } - - const ptSqlRow = database - .prepare( - `SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'paired_tasks'`, - ) - .get() as { sql?: string } | undefined; - const ptSql = ptSqlRow?.sql || ''; - if (ptSql && !ptSql.includes('arbiter_requested')) { - const pairedTaskCols = getTableColumns(database, 'paired_tasks'); - const selectPairedTaskColumn = (columnName: string): string => - pairedTaskCols.includes(columnName) - ? columnName - : `NULL AS ${columnName}`; - database.exec(` - CREATE TABLE paired_tasks_new ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT, - reviewer_service_id TEXT, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL DEFAULT 'active', - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (status IN ('active', 'review_ready', 'in_review', 'merge_ready', 'completed', 'arbiter_requested', 'in_arbitration')), - CHECK (owner_agent_type IN ('claude-code', 'codex') OR owner_agent_type IS NULL), - CHECK (reviewer_agent_type IN ('claude-code', 'codex') OR reviewer_agent_type IS NULL), - CHECK (arbiter_agent_type IN ('claude-code', 'codex') OR arbiter_agent_type IS NULL) - ); - INSERT INTO paired_tasks_new ( - id, chat_jid, group_folder, owner_service_id, reviewer_service_id, - owner_agent_type, reviewer_agent_type, - arbiter_agent_type, title, source_ref, plan_notes, review_requested_at, - round_trip_count, status, created_at, updated_at - ) - SELECT - id, chat_jid, group_folder, - ${selectPairedTaskColumn('owner_service_id')}, - ${selectPairedTaskColumn('reviewer_service_id')}, - owner_agent_type, reviewer_agent_type, - arbiter_agent_type, title, source_ref, plan_notes, review_requested_at, - round_trip_count, status, created_at, updated_at - FROM paired_tasks; - DROP TABLE paired_tasks; - ALTER TABLE paired_tasks_new RENAME TO paired_tasks; - CREATE INDEX IF NOT EXISTS idx_paired_tasks_chat_status - ON paired_tasks(chat_jid, status, updated_at); - `); - } - - tryExecMigration( - database, - `ALTER TABLE paired_tasks ADD COLUMN completion_reason TEXT`, - ); - - for (const table of [ - 'paired_executions', - 'paired_approvals', - 'paired_artifacts', - 'paired_events', - ]) { - database.exec(`DROP TABLE IF EXISTS ${table}`); - } - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/009_paired-workspace-project-schema-cleanup.ts b/src/db/migrations/009_paired-workspace-project-schema-cleanup.ts deleted file mode 100644 index 6e54270..0000000 --- a/src/db/migrations/009_paired-workspace-project-schema-cleanup.ts +++ /dev/null @@ -1,54 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; - -export const PAIRED_WORKSPACE_PROJECT_SCHEMA_CLEANUP_MIGRATION = { - version: 9, - name: 'paired_workspace_project_schema_cleanup', - apply(database) { - const pairedWsSqlRow = database - .prepare( - `SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'paired_workspaces'`, - ) - .get() as { sql?: string } | undefined; - const pairedWsSql = pairedWsSqlRow?.sql || ''; - if (pairedWsSql && pairedWsSql.includes('snapshot_source_fingerprint')) { - database.exec(`DROP TABLE IF EXISTS paired_workspaces`); - database.exec(` - CREATE TABLE IF NOT EXISTS paired_workspaces ( - id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - role TEXT NOT NULL, - workspace_dir TEXT NOT NULL, - snapshot_source_dir TEXT, - snapshot_ref TEXT, - status TEXT NOT NULL DEFAULT 'ready', - snapshot_refreshed_at TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (role IN ('owner', 'reviewer')), - CHECK (status IN ('ready', 'stale')) - ); - CREATE UNIQUE INDEX IF NOT EXISTS idx_paired_workspaces_task_role - ON paired_workspaces(task_id, role); - `); - } - - const pairedProjSqlRow = database - .prepare( - `SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'paired_projects'`, - ) - .get() as { sql?: string } | undefined; - const pairedProjSql = pairedProjSqlRow?.sql || ''; - if (pairedProjSql && pairedProjSql.includes('workspace_topology')) { - database.exec(`DROP TABLE IF EXISTS paired_projects`); - database.exec(` - CREATE TABLE IF NOT EXISTS paired_projects ( - chat_jid TEXT PRIMARY KEY, - group_folder TEXT NOT NULL, - canonical_work_dir TEXT NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - `); - } - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/010_paired-turn-provenance-upgrade.ts b/src/db/migrations/010_paired-turn-provenance-upgrade.ts deleted file mode 100644 index c3132bf..0000000 --- a/src/db/migrations/010_paired-turn-provenance-upgrade.ts +++ /dev/null @@ -1,193 +0,0 @@ -import { backfillPairedTurnAttemptsFromTurns } from '../paired-turn-attempts.js'; -import { - applyPairedTurnAttemptProvenanceConstraints, - assertPairedTurnAttemptProvenanceIntegrity, - backfillPairedTurnAttemptActiveRunIds, - backfillPairedTurnAttemptEntityIds, - backfillPairedTurnAttemptIds, - backfillPairedTurnAttemptParentIds, - backfillPairedTurnAttemptProvenance, - dropPairedTurnAttemptProvenanceConstraints, - rebuildPairedTurnAttemptForeignKeyTables, - rebuildPairedTurnsWithoutLegacyScratchColumns, -} from '../paired-turn-provenance-schema.js'; -import { tryExecMigration } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const PAIRED_TURN_PROVENANCE_UPGRADE_MIGRATION = { - version: 10, - name: 'paired_turn_provenance_upgrade', - alwaysRun: true, - apply(database) { - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN paired_task_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN paired_task_updated_at TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN turn_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN turn_attempt_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN turn_attempt_no INTEGER`, - ); - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN turn_intent_kind TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE service_handoffs ADD COLUMN turn_role TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_task_execution_leases ADD COLUMN turn_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_task_execution_leases ADD COLUMN turn_attempt_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_task_execution_leases ADD COLUMN turn_attempt_no INTEGER`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_attempts ADD COLUMN attempt_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_attempts ADD COLUMN parent_attempt_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_attempts ADD COLUMN parent_handoff_id INTEGER`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_attempts ADD COLUMN continuation_handoff_id INTEGER`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_attempts ADD COLUMN active_run_id TEXT`, - ); - database.exec(` - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_parent_handoff_id - ON paired_turn_attempts(parent_handoff_id) - `); - database.exec(` - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_continuation_handoff_id - ON paired_turn_attempts(continuation_handoff_id) - `); - tryExecMigration( - database, - `ALTER TABLE paired_turn_reservations ADD COLUMN turn_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_reservations ADD COLUMN turn_attempt_id TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_reservations ADD COLUMN turn_attempt_no INTEGER`, - ); - tryExecMigration( - database, - `ALTER TABLE paired_turn_reservations ADD COLUMN turn_role TEXT`, - ); - - database.exec(` - UPDATE paired_turn_reservations - SET turn_id = COALESCE( - turn_id, - task_id || ':' || task_updated_at || ':' || intent_kind - ) - `); - database.exec(` - UPDATE paired_turn_reservations - SET turn_role = COALESCE( - turn_role, - CASE - WHEN intent_kind = 'reviewer-turn' THEN 'reviewer' - WHEN intent_kind = 'arbiter-turn' THEN 'arbiter' - ELSE 'owner' - END - ) - `); - database.exec(` - UPDATE paired_task_execution_leases - SET turn_id = COALESCE( - turn_id, - task_id || ':' || task_updated_at || ':' || intent_kind - ) - `); - database.exec( - `UPDATE service_handoffs - SET target_role = COALESCE( - target_role, - intended_role, - CASE - WHEN reason LIKE 'reviewer-%' THEN 'reviewer' - WHEN reason LIKE 'arbiter-%' THEN 'arbiter' - WHEN reason IS NOT NULL THEN 'owner' - ELSE NULL - END - ) - WHERE target_role IS NULL`, - ); - database.exec( - `UPDATE service_handoffs - SET source_role = COALESCE(source_role, target_role, intended_role) - WHERE source_role IS NULL`, - ); - database.exec( - `UPDATE service_handoffs - SET turn_role = COALESCE(turn_role, target_role, intended_role) - WHERE turn_role IS NULL`, - ); - database.exec( - `UPDATE service_handoffs - SET turn_intent_kind = COALESCE( - turn_intent_kind, - CASE - WHEN turn_role = 'reviewer' THEN 'reviewer-turn' - WHEN turn_role = 'arbiter' THEN 'arbiter-turn' - ELSE turn_intent_kind - END - )`, - ); - database.exec( - `UPDATE service_handoffs - SET turn_id = COALESCE( - turn_id, - CASE - WHEN paired_task_id IS NOT NULL - AND paired_task_updated_at IS NOT NULL - AND turn_intent_kind IS NOT NULL - THEN paired_task_id || ':' || paired_task_updated_at || ':' || turn_intent_kind - ELSE turn_id - END - )`, - ); - - backfillPairedTurnAttemptsFromTurns(database); - backfillPairedTurnAttemptIds(database); - backfillPairedTurnAttemptParentIds(database); - backfillPairedTurnAttemptActiveRunIds(database); - backfillPairedTurnAttemptProvenance(database); - backfillPairedTurnAttemptEntityIds(database); - assertPairedTurnAttemptProvenanceIntegrity(database); - dropPairedTurnAttemptProvenanceConstraints(database); - rebuildPairedTurnsWithoutLegacyScratchColumns(database); - rebuildPairedTurnAttemptForeignKeyTables(database); - applyPairedTurnAttemptProvenanceConstraints(database); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/011_owner-failure-count.ts b/src/db/migrations/011_owner-failure-count.ts deleted file mode 100644 index f05efc4..0000000 --- a/src/db/migrations/011_owner-failure-count.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const OWNER_FAILURE_COUNT_MIGRATION: SchemaMigrationDefinition = { - version: 11, - name: 'owner_failure_count', - apply(database: Database) { - if (!tableHasColumn(database, 'paired_tasks', 'owner_failure_count')) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN owner_failure_count INTEGER NOT NULL DEFAULT 0 - `); - } - - database.exec(` - UPDATE paired_tasks - SET owner_failure_count = 0 - WHERE owner_failure_count IS NULL - `); - }, -}; diff --git a/src/db/migrations/012_paired-verdict-and-step-telemetry.ts b/src/db/migrations/012_paired-verdict-and-step-telemetry.ts deleted file mode 100644 index d4f9e6e..0000000 --- a/src/db/migrations/012_paired-verdict-and-step-telemetry.ts +++ /dev/null @@ -1,75 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const PAIRED_VERDICT_AND_STEP_TELEMETRY_MIGRATION: SchemaMigrationDefinition = - { - version: 12, - name: 'paired_verdict_and_step_telemetry', - apply(database: Database) { - if (!tableHasColumn(database, 'paired_turn_outputs', 'verdict')) { - database.exec(` - ALTER TABLE paired_turn_outputs - ADD COLUMN verdict TEXT - `); - } - - if (!tableHasColumn(database, 'paired_tasks', 'owner_step_done_streak')) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN owner_step_done_streak INTEGER NOT NULL DEFAULT 0 - `); - } - - if ( - !tableHasColumn(database, 'paired_tasks', 'finalize_step_done_count') - ) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN finalize_step_done_count INTEGER NOT NULL DEFAULT 0 - `); - } - - if ( - !tableHasColumn( - database, - 'paired_tasks', - 'task_done_then_user_reopen_count', - ) - ) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN task_done_then_user_reopen_count INTEGER NOT NULL DEFAULT 0 - `); - } - - if (!tableHasColumn(database, 'paired_tasks', 'empty_step_done_streak')) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN empty_step_done_streak INTEGER NOT NULL DEFAULT 0 - `); - } - - database.exec(` - UPDATE paired_tasks - SET owner_step_done_streak = 0 - WHERE owner_step_done_streak IS NULL - `); - database.exec(` - UPDATE paired_tasks - SET finalize_step_done_count = 0 - WHERE finalize_step_done_count IS NULL - `); - database.exec(` - UPDATE paired_tasks - SET task_done_then_user_reopen_count = 0 - WHERE task_done_then_user_reopen_count IS NULL - `); - database.exec(` - UPDATE paired_tasks - SET empty_step_done_streak = 0 - WHERE empty_step_done_streak IS NULL - `); - }, - }; diff --git a/src/db/migrations/013_message-source-kind.ts b/src/db/migrations/013_message-source-kind.ts deleted file mode 100644 index bdb358e..0000000 --- a/src/db/migrations/013_message-source-kind.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const MESSAGE_SOURCE_KIND_MIGRATION: SchemaMigrationDefinition = { - version: 13, - name: 'message_source_kind', - apply(database: Database) { - const addedColumn = !tableHasColumn( - database, - 'messages', - 'message_source_kind', - ); - if (addedColumn) { - database.exec(` - ALTER TABLE messages - ADD COLUMN message_source_kind TEXT NOT NULL DEFAULT 'human' - `); - } - - const invalidOnlyWhere = ` - WHERE message_source_kind IS NULL - OR message_source_kind = '' - OR message_source_kind NOT IN ( - 'human', - 'bot', - 'trusted_external_bot', - 'ipc_injected_human', - 'ipc_injected_bot' - ) - `; - - database.exec(` - UPDATE messages - SET message_source_kind = CASE - WHEN is_bot_message = 1 THEN 'bot' - ELSE 'human' - END - ${addedColumn ? '' : invalidOnlyWhere} - `); - }, -}; diff --git a/src/db/migrations/014_work-item-attachments.ts b/src/db/migrations/014_work-item-attachments.ts deleted file mode 100644 index d5a3522..0000000 --- a/src/db/migrations/014_work-item-attachments.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const WORK_ITEM_ATTACHMENTS_MIGRATION: SchemaMigrationDefinition = { - version: 14, - name: 'work_item_attachments', - apply(database: Database) { - if (!tableHasColumn(database, 'work_items', 'attachment_payload')) { - database.exec(` - ALTER TABLE work_items - ADD COLUMN attachment_payload TEXT - `); - } - }, -}; diff --git a/src/db/migrations/015_turn-progress-text.ts b/src/db/migrations/015_turn-progress-text.ts deleted file mode 100644 index b4b9111..0000000 --- a/src/db/migrations/015_turn-progress-text.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const TURN_PROGRESS_TEXT_MIGRATION: SchemaMigrationDefinition = { - version: 15, - name: 'turn_progress_text', - apply(database: Database) { - if (!tableHasColumn(database, 'paired_turns', 'progress_text')) { - database.exec(`ALTER TABLE paired_turns ADD COLUMN progress_text TEXT`); - } - if (!tableHasColumn(database, 'paired_turns', 'progress_updated_at')) { - database.exec( - `ALTER TABLE paired_turns ADD COLUMN progress_updated_at TEXT`, - ); - } - }, -}; diff --git a/src/db/migrations/016_room-skill-overrides.ts b/src/db/migrations/016_room-skill-overrides.ts deleted file mode 100644 index e94c81a..0000000 --- a/src/db/migrations/016_room-skill-overrides.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import type { SchemaMigrationDefinition } from './types.js'; - -export const ROOM_SKILL_OVERRIDES_MIGRATION: SchemaMigrationDefinition = { - version: 16, - name: 'room_skill_overrides', - apply(database: Database) { - database.exec(` - CREATE TABLE IF NOT EXISTS room_skill_overrides ( - chat_jid TEXT NOT NULL, - agent_type TEXT NOT NULL, - skill_scope TEXT NOT NULL, - skill_name TEXT NOT NULL, - enabled INTEGER NOT NULL, - created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (chat_jid, agent_type, skill_scope, skill_name), - FOREIGN KEY (chat_jid) REFERENCES room_settings(chat_jid) ON DELETE CASCADE, - CHECK (agent_type IN ('claude-code', 'codex')), - CHECK (enabled IN (0, 1)), - CHECK (length(skill_scope) > 0), - CHECK (length(skill_name) > 0) - ); - CREATE INDEX IF NOT EXISTS idx_room_skill_overrides_room - ON room_skill_overrides(chat_jid, agent_type); - `); - }, -}; diff --git a/src/db/migrations/017_scheduled-task-room-role.ts b/src/db/migrations/017_scheduled-task-room-role.ts deleted file mode 100644 index 2a97c47..0000000 --- a/src/db/migrations/017_scheduled-task-room-role.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type { SchemaMigrationDefinition } from './types.js'; -import { tryExecMigration } from './helpers.js'; - -export const SCHEDULED_TASK_ROOM_ROLE_MIGRATION = { - version: 17, - name: 'scheduled_task_room_role', - apply(database) { - tryExecMigration( - database, - `ALTER TABLE scheduled_tasks ADD COLUMN room_role TEXT`, - ); - }, -} satisfies SchemaMigrationDefinition; diff --git a/src/db/migrations/018_paired-turn-output-attachments.ts b/src/db/migrations/018_paired-turn-output-attachments.ts deleted file mode 100644 index a717673..0000000 --- a/src/db/migrations/018_paired-turn-output-attachments.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const PAIRED_TURN_OUTPUT_ATTACHMENTS_MIGRATION: SchemaMigrationDefinition = - { - version: 18, - name: 'paired_turn_output_attachments', - apply(database: Database) { - if ( - !tableHasColumn(database, 'paired_turn_outputs', 'attachment_payload') - ) { - database.exec(` - ALTER TABLE paired_turn_outputs - ADD COLUMN attachment_payload TEXT - `); - } - }, - }; diff --git a/src/db/migrations/019_turn-progress-text-recovery.ts b/src/db/migrations/019_turn-progress-text-recovery.ts deleted file mode 100644 index fac68b4..0000000 --- a/src/db/migrations/019_turn-progress-text-recovery.ts +++ /dev/null @@ -1,36 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -/** - * Recovery for a migration-version collision introduced by merging - * origin/main into the long-lived deployment branch. - * - * The deployment DB recorded schema version 15 as `reviewer_failure_count` - * (a downstream-only migration), while upstream defines version 15 as - * `turn_progress_text` (migration 015). Because the migration runner tracks - * applied migrations by version number alone, the upstream `turn_progress_text` - * migration is skipped on the live DB and `paired_turns.progress_text` / - * `progress_updated_at` are never created — even though merged runtime code - * (src/db/paired-turns.ts) reads and writes them. - * - * This migration re-applies those columns idempotently under a fresh version - * number so the live deployment gets them. It is a no-op on databases where - * migration 015 already ran (fresh installs), thanks to the column guards. - */ -export const TURN_PROGRESS_TEXT_RECOVERY_MIGRATION: SchemaMigrationDefinition = - { - version: 19, - name: 'turn_progress_text_recovery', - apply(database: Database) { - if (!tableHasColumn(database, 'paired_turns', 'progress_text')) { - database.exec(`ALTER TABLE paired_turns ADD COLUMN progress_text TEXT`); - } - if (!tableHasColumn(database, 'paired_turns', 'progress_updated_at')) { - database.exec( - `ALTER TABLE paired_turns ADD COLUMN progress_updated_at TEXT`, - ); - } - }, - }; diff --git a/src/db/migrations/020_arbiter-intervention-count.ts b/src/db/migrations/020_arbiter-intervention-count.ts deleted file mode 100644 index 7e3615a..0000000 --- a/src/db/migrations/020_arbiter-intervention-count.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const ARBITER_INTERVENTION_COUNT_MIGRATION: SchemaMigrationDefinition = { - version: 20, - name: 'arbiter_intervention_count', - apply(database: Database) { - if ( - !tableHasColumn(database, 'paired_tasks', 'arbiter_intervention_count') - ) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN arbiter_intervention_count INTEGER NOT NULL DEFAULT 0 - `); - } - - database.exec(` - UPDATE paired_tasks - SET arbiter_intervention_count = 0 - WHERE arbiter_intervention_count IS NULL - `); - }, -}; diff --git a/src/db/migrations/021_reviewer-failure-count.ts b/src/db/migrations/021_reviewer-failure-count.ts deleted file mode 100644 index 0ed36a6..0000000 --- a/src/db/migrations/021_reviewer-failure-count.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -export const REVIEWER_FAILURE_COUNT_MIGRATION: SchemaMigrationDefinition = { - version: 21, - name: 'reviewer_failure_count', - apply(database: Database) { - if (!tableHasColumn(database, 'paired_tasks', 'reviewer_failure_count')) { - database.exec(` - ALTER TABLE paired_tasks - ADD COLUMN reviewer_failure_count INTEGER NOT NULL DEFAULT 0 - `); - } - - database.exec(` - UPDATE paired_tasks - SET reviewer_failure_count = 0 - WHERE reviewer_failure_count IS NULL - `); - }, -}; diff --git a/src/db/migrations/022_turn-progress-text-compat.ts b/src/db/migrations/022_turn-progress-text-compat.ts deleted file mode 100644 index 616a034..0000000 --- a/src/db/migrations/022_turn-progress-text-compat.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -import { tableHasColumn } from './helpers.js'; -import type { SchemaMigrationDefinition } from './types.js'; - -/** - * Compatibility backfill for `turn_progress_text` (migration 015). - * - * Some deployments first shipped `reviewer_failure_count` as a local migration - * numbered 15, so their `schema_migrations` table records version 15 under a - * different name than the canonical `turn_progress_text` migration. The runner - * skips migrations purely by version number, so on those databases the real - * version-15 migration never runs and `paired_turns.progress_text` / - * `progress_updated_at` are missing — yet the runtime reads them. This migration - * re-adds the columns idempotently so collided databases converge with fresh - * ones. On a fresh database migration 015 already created the columns, so the - * `tableHasColumn` guards make this a no-op. - */ -export const TURN_PROGRESS_TEXT_COMPAT_MIGRATION: SchemaMigrationDefinition = { - version: 22, - name: 'turn_progress_text_compat', - apply(database: Database) { - if (!tableHasColumn(database, 'paired_turns', 'progress_text')) { - database.exec(`ALTER TABLE paired_turns ADD COLUMN progress_text TEXT`); - } - if (!tableHasColumn(database, 'paired_turns', 'progress_updated_at')) { - database.exec( - `ALTER TABLE paired_turns ADD COLUMN progress_updated_at TEXT`, - ); - } - }, -}; diff --git a/src/db/migrations/helpers.ts b/src/db/migrations/helpers.ts deleted file mode 100644 index 92d368b..0000000 --- a/src/db/migrations/helpers.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { Database } from 'bun:sqlite'; - -function getTableColumns(database: Database, tableName: string): string[] { - return ( - database.prepare(`PRAGMA table_info(${tableName})`).all() as Array<{ - name: string; - }> - ).map((column) => column.name); -} - -export function tableHasColumn( - database: Database, - tableName: string, - columnName: string, -): boolean { - return getTableColumns(database, tableName).includes(columnName); -} - -export function tryExecMigration(database: Database, sql: string): void { - try { - database.exec(sql); - } catch { - /* column already exists */ - } -} - -export function backfillMessageSeq(database: Database): void { - const rows = database - .prepare( - `SELECT rowid, seq - FROM messages - ORDER BY CASE WHEN seq IS NULL THEN 1 ELSE 0 END, seq, timestamp, rowid`, - ) - .all() as Array<{ rowid: number; seq: number | null }>; - if (rows.length === 0) { - return; - } - - let nextSeq = 1; - const assignSeq = database.prepare( - 'UPDATE messages SET seq = ? WHERE rowid = ? AND seq IS NULL', - ); - const tx = database.transaction(() => { - for (const row of rows) { - if (row.seq === null) { - assignSeq.run(nextSeq, row.rowid); - } - nextSeq = Math.max(nextSeq, (row.seq ?? nextSeq) + 1); - } - }); - tx(); - - const maxSeqRow = database - .prepare('SELECT MAX(seq) AS maxSeq FROM messages') - .get() as { maxSeq: number | null }; - const maxSeq = maxSeqRow.maxSeq ?? 0; - if (maxSeq > 0) { - database - .prepare('INSERT OR IGNORE INTO message_sequence (id) VALUES (?)') - .run(maxSeq); - } -} - -export { getTableColumns }; diff --git a/src/db/migrations/index.ts b/src/db/migrations/index.ts index d3b134e..e23bde1 100644 --- a/src/db/migrations/index.ts +++ b/src/db/migrations/index.ts @@ -1,59 +1,27 @@ import type { Database } from 'bun:sqlite'; -import { LEGACY_SCHEMA_BUNDLE_MIGRATION } from './001_legacy-schema-bundle.js'; -import { SCHEDULED_TASK_COLUMNS_MIGRATION } from './002_scheduled-task-columns.js'; -import { ROOM_REGISTRATION_CANONICAL_COLUMNS_MIGRATION } from './003_room-registration-canonical-columns.js'; -import { MESSAGE_SEQ_AND_WORK_ITEM_INDEXES_MIGRATION } from './004_message-seq-and-work-item-indexes.js'; -import { SESSIONS_COMPOSITE_KEY_MIGRATION } from './005_sessions-composite-key.js'; -import { CHAT_CHANNEL_METADATA_MIGRATION } from './006_chat-channel-metadata.js'; -import { RUNTIME_SERVICE_METADATA_MIGRATION } from './007_runtime-service-metadata.js'; -import { PAIRED_TASK_SCHEMA_CLEANUP_MIGRATION } from './008_paired-task-schema-cleanup.js'; -import { PAIRED_WORKSPACE_PROJECT_SCHEMA_CLEANUP_MIGRATION } from './009_paired-workspace-project-schema-cleanup.js'; -import { PAIRED_TURN_PROVENANCE_UPGRADE_MIGRATION } from './010_paired-turn-provenance-upgrade.js'; -import { OWNER_FAILURE_COUNT_MIGRATION } from './011_owner-failure-count.js'; -import { PAIRED_VERDICT_AND_STEP_TELEMETRY_MIGRATION } from './012_paired-verdict-and-step-telemetry.js'; -import { MESSAGE_SOURCE_KIND_MIGRATION } from './013_message-source-kind.js'; -import { WORK_ITEM_ATTACHMENTS_MIGRATION } from './014_work-item-attachments.js'; -import { TURN_PROGRESS_TEXT_MIGRATION } from './015_turn-progress-text.js'; -import { ROOM_SKILL_OVERRIDES_MIGRATION } from './016_room-skill-overrides.js'; -import { SCHEDULED_TASK_ROOM_ROLE_MIGRATION } from './017_scheduled-task-room-role.js'; -import { PAIRED_TURN_OUTPUT_ATTACHMENTS_MIGRATION } from './018_paired-turn-output-attachments.js'; -import { TURN_PROGRESS_TEXT_RECOVERY_MIGRATION } from './019_turn-progress-text-recovery.js'; -import { ARBITER_INTERVENTION_COUNT_MIGRATION } from './020_arbiter-intervention-count.js'; -import { REVIEWER_FAILURE_COUNT_MIGRATION } from './021_reviewer-failure-count.js'; -import { TURN_PROGRESS_TEXT_COMPAT_MIGRATION } from './022_turn-progress-text-compat.js'; -import type { - SchemaMigrationArgs, - SchemaMigrationDefinition, -} from './types.js'; +import { applyBaseSchema } from '../base-schema.js'; + +/** + * Schema version that base-schema.ts represents. Migrations 1-22 were squashed + * into it; databases that recorded them are already at this version. + */ +export const BASELINE_SCHEMA_VERSION = 22; + +export interface SchemaMigrationDefinition { + version: number; + name: string; + apply: (database: Database) => void; +} + +/** + * Migrations applied on top of the baseline, in order. Versions must be + * greater than BASELINE_SCHEMA_VERSION and must never be renumbered. + */ +const ORDERED_SCHEMA_MIGRATIONS: readonly SchemaMigrationDefinition[] = []; const SCHEMA_MIGRATIONS_TABLE = 'schema_migrations'; -const ORDERED_SCHEMA_MIGRATIONS: readonly SchemaMigrationDefinition[] = [ - LEGACY_SCHEMA_BUNDLE_MIGRATION, - SCHEDULED_TASK_COLUMNS_MIGRATION, - ROOM_REGISTRATION_CANONICAL_COLUMNS_MIGRATION, - MESSAGE_SEQ_AND_WORK_ITEM_INDEXES_MIGRATION, - SESSIONS_COMPOSITE_KEY_MIGRATION, - CHAT_CHANNEL_METADATA_MIGRATION, - RUNTIME_SERVICE_METADATA_MIGRATION, - PAIRED_TASK_SCHEMA_CLEANUP_MIGRATION, - PAIRED_WORKSPACE_PROJECT_SCHEMA_CLEANUP_MIGRATION, - PAIRED_TURN_PROVENANCE_UPGRADE_MIGRATION, - OWNER_FAILURE_COUNT_MIGRATION, - PAIRED_VERDICT_AND_STEP_TELEMETRY_MIGRATION, - MESSAGE_SOURCE_KIND_MIGRATION, - WORK_ITEM_ATTACHMENTS_MIGRATION, - TURN_PROGRESS_TEXT_MIGRATION, - ROOM_SKILL_OVERRIDES_MIGRATION, - SCHEDULED_TASK_ROOM_ROLE_MIGRATION, - PAIRED_TURN_OUTPUT_ATTACHMENTS_MIGRATION, - TURN_PROGRESS_TEXT_RECOVERY_MIGRATION, - ARBITER_INTERVENTION_COUNT_MIGRATION, - REVIEWER_FAILURE_COUNT_MIGRATION, - TURN_PROGRESS_TEXT_COMPAT_MIGRATION, -]; - function ensureSchemaMigrationsTable(database: Database): void { database.exec(` CREATE TABLE IF NOT EXISTS ${SCHEMA_MIGRATIONS_TABLE} ( @@ -64,46 +32,82 @@ function ensureSchemaMigrationsTable(database: Database): void { `); } -function getAppliedSchemaVersions(database: Database): Set { - const rows = database +function isFreshDatabase(database: Database): boolean { + const row = database .prepare( - `SELECT version - FROM ${SCHEMA_MIGRATIONS_TABLE} - ORDER BY version ASC`, + `SELECT COUNT(*) AS count + FROM sqlite_master + WHERE type = 'table' + AND name NOT LIKE 'sqlite_%' + AND name != '${SCHEMA_MIGRATIONS_TABLE}'`, ) + .get() as { count: number }; + return row.count === 0; +} + +function getAppliedSchemaVersions(database: Database): Set { + const hasTable = database + .prepare( + `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = '${SCHEMA_MIGRATIONS_TABLE}'`, + ) + .get(); + if (!hasTable) { + return new Set(); + } + const rows = database + .prepare(`SELECT version FROM ${SCHEMA_MIGRATIONS_TABLE}`) .all() as Array<{ version: number }>; return new Set(rows.map((row) => row.version)); } function recordAppliedSchemaMigration( database: Database, - migration: SchemaMigrationDefinition, + version: number, + name: string, ): void { database .prepare( - `INSERT INTO ${SCHEMA_MIGRATIONS_TABLE} (version, name) - VALUES (?, ?)`, + `INSERT INTO ${SCHEMA_MIGRATIONS_TABLE} (version, name) VALUES (?, ?)`, ) - .run(migration.version, migration.name); + .run(version, name); } -export function applyVersionedSchemaMigrations( +export function applySchemaMigrations( database: Database, - args: SchemaMigrationArgs, + migrations: readonly SchemaMigrationDefinition[] = ORDERED_SCHEMA_MIGRATIONS, ): void { - ensureSchemaMigrationsTable(database); - const appliedVersions = getAppliedSchemaVersions(database); + if (isFreshDatabase(database)) { + database.transaction(() => { + ensureSchemaMigrationsTable(database); + applyBaseSchema(database); + recordAppliedSchemaMigration( + database, + BASELINE_SCHEMA_VERSION, + 'baseline', + ); + })(); + } - for (const migration of ORDERED_SCHEMA_MIGRATIONS) { - const alreadyApplied = appliedVersions.has(migration.version); - if (alreadyApplied && !migration.alwaysRun) { + const appliedVersions = getAppliedSchemaVersions(database); + const currentVersion = Math.max(0, ...appliedVersions); + if (currentVersion < BASELINE_SCHEMA_VERSION) { + throw new Error( + `Database schema version ${currentVersion} predates the squashed baseline ${BASELINE_SCHEMA_VERSION}; upgrade it with an EJClaw release from before the migration squash first`, + ); + } + + for (const migration of migrations) { + if (appliedVersions.has(migration.version)) { continue; } - - // Fail fast: a partially-applied migration must not be recorded as complete. - migration.apply(database, args); - if (!alreadyApplied) { - recordAppliedSchemaMigration(database, migration); + if (migration.version <= BASELINE_SCHEMA_VERSION) { + throw new Error( + `Schema migration ${migration.version} (${migration.name}) must be numbered above the baseline ${BASELINE_SCHEMA_VERSION}`, + ); } + database.transaction(() => { + migration.apply(database); + recordAppliedSchemaMigration(database, migration.version, migration.name); + })(); } } diff --git a/src/db/migrations/types.ts b/src/db/migrations/types.ts deleted file mode 100644 index 912ad32..0000000 --- a/src/db/migrations/types.ts +++ /dev/null @@ -1,12 +0,0 @@ -import type { Database } from 'bun:sqlite'; - -export interface SchemaMigrationArgs { - assistantName: string; -} - -export interface SchemaMigrationDefinition { - version: number; - name: string; - alwaysRun?: boolean; - apply: (database: Database, args: SchemaMigrationArgs) => void; -} diff --git a/src/db/paired-turn-attempts.ts b/src/db/paired-turn-attempts.ts index d6e3d68..b799b99 100644 --- a/src/db/paired-turn-attempts.ts +++ b/src/db/paired-turn-attempts.ts @@ -111,17 +111,6 @@ function getPairedTurnAttemptParentIdFromDatabase( return parentAttemptId; } -function tableHasColumn( - database: Database, - tableName: string, - columnName: string, -): boolean { - const rows = database - .prepare(`PRAGMA table_info(${tableName})`) - .all() as Array<{ name: string }>; - return rows.some((row) => row.name === columnName); -} - export function syncPairedTurnAttemptInDatabase( database: Database, args: { @@ -239,140 +228,6 @@ export function syncPairedTurnAttemptInDatabase( ); } -export function backfillPairedTurnAttemptsFromTurns(database: Database): void { - if ( - !tableHasColumn(database, 'paired_turns', 'state') || - !tableHasColumn(database, 'paired_turns', 'attempt_no') - ) { - return; - } - - const rows = database - .prepare( - ` - SELECT * - FROM paired_turns - WHERE attempt_no >= 1 - AND state IN ('running', 'delegated', 'completed', 'failed', 'cancelled') - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts existing_attempt - WHERE existing_attempt.turn_id = paired_turns.turn_id - AND existing_attempt.attempt_no = paired_turns.attempt_no - ) - `, - ) - .all() as Array<{ - attempt_id: string | null; - parent_attempt_id: string | null; - turn_id: string; - task_id: string; - task_updated_at: string; - role: PairedRoomRole; - intent_kind: PairedTurnIdentity['intentKind']; - state: PairedTurnAttemptState; - executor_service_id: string | null; - executor_agent_type: AgentType | null; - active_run_id: string | null; - attempt_no: number; - created_at: string; - updated_at: string; - completed_at: string | null; - last_error: string | null; - }>; - - if (rows.length === 0) { - return; - } - - const insert = database.prepare( - ` - INSERT INTO paired_turn_attempts ( - attempt_id, - parent_attempt_id, - parent_handoff_id, - continuation_handoff_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - active_run_id, - created_at, - updated_at, - completed_at, - last_error - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ON CONFLICT(turn_id, attempt_no) DO NOTHING - `, - ); - - const tx = database.transaction( - ( - attemptRows: Array<{ - attempt_id?: string | null; - parent_attempt_id?: string | null; - continuation_handoff_id?: number | null; - turn_id: string; - task_id: string; - task_updated_at: string; - role: PairedRoomRole; - intent_kind: PairedTurnIdentity['intentKind']; - state: PairedTurnAttemptState; - executor_service_id: string | null; - executor_agent_type: AgentType | null; - active_run_id: string | null; - attempt_no: number; - created_at: string; - updated_at: string; - completed_at: string | null; - last_error: string | null; - }>, - ) => { - for (const row of attemptRows) { - const { executorServiceId, executorAgentType } = - resolveExecutorMetadata({ - executorServiceId: row.executor_service_id, - executorAgentType: row.executor_agent_type, - }); - insert.run( - row.attempt_id ?? - buildPairedTurnAttemptId(row.turn_id, row.attempt_no), - row.parent_attempt_id ?? - getPairedTurnAttemptParentIdFromDatabase( - database, - row.turn_id, - row.attempt_no, - ), - null, - row.continuation_handoff_id ?? null, - row.turn_id, - row.attempt_no, - row.task_id, - row.task_updated_at, - row.role, - row.intent_kind, - row.state, - executorServiceId, - executorAgentType, - row.state === 'running' ? (row.active_run_id ?? null) : null, - row.created_at, - row.updated_at, - row.completed_at, - row.last_error, - ); - } - }, - ); - - tx(rows); -} - export function getPairedTurnAttemptsForTurnFromDatabase( database: Database, turnId: string, diff --git a/src/db/paired-turn-provenance-schema.ts b/src/db/paired-turn-provenance-schema.ts deleted file mode 100644 index 700e185..0000000 --- a/src/db/paired-turn-provenance-schema.ts +++ /dev/null @@ -1,2061 +0,0 @@ -import { Database } from 'bun:sqlite'; - -import { buildPairedTurnAttemptId } from './paired-turn-attempts.js'; -import { tableHasColumn } from './migrations/helpers.js'; - -// Paired-turn provenance rebuild helpers extracted from the legacy schema -// bundle. These remain runtime helpers because v10 replays them during -// initialization for pre-versioned and partially-migrated databases. - -function getTableSql(database: Database, tableName: string): string | null { - const row = database - .prepare( - ` - SELECT sql - FROM sqlite_master - WHERE type = 'table' - AND name = ? - `, - ) - .get(tableName) as { sql?: string | null } | undefined; - return row?.sql ?? null; -} - -function buildPairedTurnAttemptIdSql( - turnIdExpr: string, - attemptNoExpr: string, -): string { - return `${turnIdExpr} || ':attempt:' || CAST(${attemptNoExpr} AS TEXT)`; -} - -function buildPairedTurnAttemptParentIdSql( - turnIdExpr: string, - attemptNoExpr: string, -): string { - return `CASE - WHEN ${attemptNoExpr} <= 1 THEN NULL - ELSE ${buildPairedTurnAttemptIdSql(turnIdExpr, `(${attemptNoExpr}) - 1`)} - END`; -} - -function buildPairedTurnAttemptParentHandoffMatchSql(args: { - parentHandoffIdExpr: string; - turnIdExpr: string; - parentAttemptIdExpr: string; - attemptNoExpr: string; -}): string { - return `EXISTS ( - SELECT 1 - FROM service_handoffs - WHERE service_handoffs.id = ${args.parentHandoffIdExpr} - AND service_handoffs.turn_id = ${args.turnIdExpr} - AND service_handoffs.status = 'failed' - AND ( - service_handoffs.turn_attempt_id = ${args.parentAttemptIdExpr} - OR ( - service_handoffs.turn_attempt_id IS NULL - AND service_handoffs.turn_attempt_no = (${args.attemptNoExpr}) - 1 - ) - ) - )`; -} - -function buildPairedTurnAttemptContinuationHandoffMatchSql(args: { - continuationHandoffIdExpr: string; - turnIdExpr: string; - attemptIdExpr: string; - attemptNoExpr: string; -}): string { - return `EXISTS ( - SELECT 1 - FROM service_handoffs - WHERE service_handoffs.id = ${args.continuationHandoffIdExpr} - AND service_handoffs.turn_id = ${args.turnIdExpr} - AND ( - service_handoffs.turn_attempt_id = ${args.attemptIdExpr} - OR ( - service_handoffs.turn_attempt_id IS NULL - AND service_handoffs.turn_attempt_no = ${args.attemptNoExpr} - ) - ) - )`; -} - -function tableHasForeignKey( - database: Database, - args: { - tableName: string; - referencedTable: string; - fromColumns: string[]; - toColumns: string[]; - onDelete?: string; - }, -): boolean { - const rows = database - .prepare(`PRAGMA foreign_key_list(${args.tableName})`) - .all() as Array<{ - id: number; - seq: number; - table: string; - from: string; - to: string; - on_delete: string; - }>; - if (rows.length === 0) { - return false; - } - - const byId = new Map(); - for (const row of rows) { - const group = byId.get(row.id) ?? []; - group.push(row); - byId.set(row.id, group); - } - - for (const group of byId.values()) { - const sorted = [...group].sort((left, right) => left.seq - right.seq); - if (sorted.length !== args.fromColumns.length) { - continue; - } - if (sorted[0]!.table !== args.referencedTable) { - continue; - } - if ( - args.onDelete && - sorted[0]!.on_delete.toUpperCase() !== args.onDelete.toUpperCase() - ) { - continue; - } - - const matches = sorted.every((row, index) => { - return ( - row.from === args.fromColumns[index] && row.to === args.toColumns[index] - ); - }); - if (matches) { - return true; - } - } - - return false; -} - -interface PairedTurnAttemptTimingRow { - turn_id: string; - attempt_no: number; - created_at: string; -} - -interface PairedTurnAttemptTiming { - attemptNo: number; - createdAtMs: number; -} - -function parseBackfillTimestampMs( - value: string | null | undefined, -): number | null { - if (!value) { - return null; - } - - const trimmed = value.trim(); - if (trimmed.length === 0) { - return null; - } - - let normalized = trimmed.includes('T') ? trimmed : trimmed.replace(' ', 'T'); - if (!/[zZ]|[+-]\d{2}:\d{2}$/.test(normalized)) { - normalized = `${normalized}Z`; - } - - const parsed = Date.parse(normalized); - return Number.isNaN(parsed) ? null : parsed; -} - -function getPairedTurnAttemptTimingsByTurnId( - database: Database, -): Map { - const rows = database - .prepare( - ` - SELECT turn_id, attempt_no, created_at - FROM paired_turn_attempts - WHERE attempt_no >= 1 - ORDER BY turn_id ASC, attempt_no ASC - `, - ) - .all() as PairedTurnAttemptTimingRow[]; - const timingsByTurnId = new Map(); - - for (const row of rows) { - const createdAtMs = parseBackfillTimestampMs(row.created_at); - if (createdAtMs === null) { - throw new Error( - `paired_turn_attempts(${row.turn_id}, attempt=${row.attempt_no}) has an invalid created_at timestamp`, - ); - } - const timings = timingsByTurnId.get(row.turn_id) ?? []; - timings.push({ - attemptNo: row.attempt_no, - createdAtMs, - }); - timingsByTurnId.set(row.turn_id, timings); - } - - return timingsByTurnId; -} - -function resolveAttemptNoForBackfill( - args: { - turnId: string; - eventTimestamp: string | null | undefined; - rowLabel: string; - }, - timingsByTurnId: Map, -): number | null { - const attemptTimings = timingsByTurnId.get(args.turnId) ?? []; - if (attemptTimings.length === 0) { - return null; - } - - if (attemptTimings.length === 1) { - return attemptTimings[0]!.attemptNo; - } - - const eventAtMs = parseBackfillTimestampMs(args.eventTimestamp); - if (eventAtMs === null) { - throw new Error( - `${args.rowLabel} has turn_id=${args.turnId} but no timestamp that can be mapped to a specific attempt`, - ); - } - - const exactMatches = attemptTimings.filter((timing, index) => { - const nextTiming = attemptTimings[index + 1]; - return ( - eventAtMs >= timing.createdAtMs && - (nextTiming === undefined || eventAtMs < nextTiming.createdAtMs) - ); - }); - if (exactMatches.length === 1) { - return exactMatches[0]!.attemptNo; - } - if (exactMatches.length > 1) { - throw new Error( - `${args.rowLabel} has an ambiguous exact timestamp match for turn_id=${args.turnId}`, - ); - } - - const eventAtSecond = Math.floor(eventAtMs / 1000); - const secondPrecisionMatches = attemptTimings.filter((timing, index) => { - const nextTiming = attemptTimings[index + 1]; - const timingSecond = Math.floor(timing.createdAtMs / 1000); - const nextTimingSecond = - nextTiming === undefined - ? null - : Math.floor(nextTiming.createdAtMs / 1000); - return ( - eventAtSecond >= timingSecond && - (nextTimingSecond === null || eventAtSecond < nextTimingSecond) - ); - }); - if (secondPrecisionMatches.length === 1) { - return secondPrecisionMatches[0]!.attemptNo; - } - if (secondPrecisionMatches.length > 1) { - throw new Error( - `${args.rowLabel} has an ambiguous second-precision timestamp match for turn_id=${args.turnId}`, - ); - } - - throw new Error( - `${args.rowLabel} could not be mapped to a specific attempt for turn_id=${args.turnId}`, - ); -} - -export function backfillPairedTurnAttemptIds(database: Database): void { - if (!tableHasColumn(database, 'paired_turn_attempts', 'attempt_id')) { - return; - } - - database.exec(` - UPDATE paired_turn_attempts - SET attempt_id = ${buildPairedTurnAttemptIdSql('turn_id', 'attempt_no')} - WHERE attempt_id IS NULL - OR TRIM(attempt_id) = '' - `); -} - -export function backfillPairedTurnAttemptParentIds(database: Database): void { - if (!tableHasColumn(database, 'paired_turn_attempts', 'parent_attempt_id')) { - return; - } - - database.exec(` - UPDATE paired_turn_attempts - SET parent_attempt_id = ${buildPairedTurnAttemptParentIdSql('turn_id', 'attempt_no')} - WHERE attempt_no > 1 - AND EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE previous_attempt.turn_id = paired_turn_attempts.turn_id - AND previous_attempt.attempt_no = paired_turn_attempts.attempt_no - 1 - ) - AND ( - parent_attempt_id IS NULL - OR TRIM(parent_attempt_id) = '' - OR parent_attempt_id != ${buildPairedTurnAttemptParentIdSql('turn_id', 'attempt_no')} - ) - `); - - database.exec(` - UPDATE paired_turn_attempts - SET parent_attempt_id = NULL - WHERE attempt_no <= 1 - AND parent_attempt_id IS NOT NULL - `); -} - -export function backfillPairedTurnAttemptActiveRunIds( - database: Database, -): void { - if (!tableHasColumn(database, 'paired_turn_attempts', 'active_run_id')) { - return; - } - - const pairedTurnsActiveRunIdExists = tableHasColumn( - database, - 'paired_turns', - 'active_run_id', - ); - const leaseTurnIdExists = tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_id', - ); - const leaseTurnAttemptNoExists = tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_no', - ); - const leaseActiveRunIdSql = leaseTurnIdExists - ? leaseTurnAttemptNoExists - ? `COALESCE( - ( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - AND paired_task_execution_leases.turn_attempt_no = paired_turn_attempts.attempt_no - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - ), - ( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - AND paired_task_execution_leases.turn_attempt_no IS NULL - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - ) - )` - : `( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - )` - : 'NULL'; - const legacyTurnActiveRunIdSql = pairedTurnsActiveRunIdExists - ? `( - SELECT paired_turns.active_run_id - FROM paired_turns - WHERE paired_turns.turn_id = paired_turn_attempts.turn_id - )` - : 'NULL'; - - database.exec(` - UPDATE paired_turn_attempts - SET active_run_id = COALESCE( - active_run_id, - CASE - WHEN state = 'running' - THEN COALESCE( - ${leaseActiveRunIdSql}, - ${legacyTurnActiveRunIdSql} - ) - ELSE NULL - END - ) - WHERE state = 'running' - AND (active_run_id IS NULL OR TRIM(active_run_id) = '') - `); - - database.exec(` - UPDATE paired_turn_attempts - SET active_run_id = NULL - WHERE state != 'running' - AND active_run_id IS NOT NULL - `); -} - -export function backfillPairedTurnAttemptEntityIds(database: Database): void { - if ( - tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_id') && - tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_no') - ) { - database.exec(` - UPDATE paired_turn_reservations - SET turn_attempt_id = ( - SELECT paired_turn_attempts.attempt_id - FROM paired_turn_attempts - WHERE paired_turn_attempts.turn_id = paired_turn_reservations.turn_id - AND paired_turn_attempts.attempt_no = paired_turn_reservations.turn_attempt_no - ) - WHERE turn_attempt_no IS NOT NULL - AND (turn_attempt_id IS NULL OR TRIM(turn_attempt_id) = '') - `); - } - - if ( - tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_id', - ) && - tableHasColumn(database, 'paired_task_execution_leases', 'turn_attempt_no') - ) { - database.exec(` - UPDATE paired_task_execution_leases - SET turn_attempt_id = ( - SELECT paired_turn_attempts.attempt_id - FROM paired_turn_attempts - WHERE paired_turn_attempts.turn_id = paired_task_execution_leases.turn_id - AND paired_turn_attempts.attempt_no = paired_task_execution_leases.turn_attempt_no - ) - WHERE turn_attempt_no IS NOT NULL - AND (turn_attempt_id IS NULL OR TRIM(turn_attempt_id) = '') - `); - } - - if ( - tableHasColumn(database, 'service_handoffs', 'turn_attempt_id') && - tableHasColumn(database, 'service_handoffs', 'turn_attempt_no') - ) { - database.exec(` - UPDATE service_handoffs - SET turn_attempt_id = ( - SELECT paired_turn_attempts.attempt_id - FROM paired_turn_attempts - WHERE paired_turn_attempts.turn_id = service_handoffs.turn_id - AND paired_turn_attempts.attempt_no = service_handoffs.turn_attempt_no - ) - WHERE turn_attempt_no IS NOT NULL - AND (turn_attempt_id IS NULL OR TRIM(turn_attempt_id) = '') - `); - } -} - -export function backfillPairedTurnAttemptProvenance(database: Database): void { - const timingsByTurnId = getPairedTurnAttemptTimingsByTurnId(database); - - if (tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_no')) { - const hasTurnAttemptIdColumn = tableHasColumn( - database, - 'paired_turn_reservations', - 'turn_attempt_id', - ); - const rows = database - .prepare( - ` - SELECT rowid, turn_id, status, created_at, updated_at, consumed_at - FROM paired_turn_reservations - WHERE turn_id IS NOT NULL - AND turn_attempt_no IS NULL - `, - ) - .all() as Array<{ - rowid: number; - turn_id: string; - status: string; - created_at: string | null; - updated_at: string | null; - consumed_at: string | null; - }>; - const update = hasTurnAttemptIdColumn - ? database.prepare( - 'UPDATE paired_turn_reservations SET turn_attempt_id = ?, turn_attempt_no = ? WHERE rowid = ?', - ) - : database.prepare( - 'UPDATE paired_turn_reservations SET turn_attempt_no = ? WHERE rowid = ?', - ); - - for (const row of rows) { - if (row.status !== 'completed') { - continue; - } - const attemptNo = resolveAttemptNoForBackfill( - { - turnId: row.turn_id, - eventTimestamp: row.consumed_at ?? row.updated_at ?? row.created_at, - rowLabel: `paired_turn_reservations(rowid=${row.rowid})`, - }, - timingsByTurnId, - ); - if (attemptNo !== null) { - const attemptId = buildPairedTurnAttemptId(row.turn_id, attemptNo); - if (hasTurnAttemptIdColumn) { - update.run(attemptId, attemptNo, row.rowid); - } else { - update.run(attemptNo, row.rowid); - } - } - } - } - - if ( - tableHasColumn(database, 'paired_task_execution_leases', 'turn_attempt_no') - ) { - const hasTurnAttemptIdColumn = tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_id', - ); - const rows = database - .prepare( - ` - SELECT rowid, turn_id, claimed_at, updated_at - FROM paired_task_execution_leases - WHERE turn_id IS NOT NULL - AND turn_attempt_no IS NULL - `, - ) - .all() as Array<{ - rowid: number; - turn_id: string; - claimed_at: string | null; - updated_at: string | null; - }>; - const update = hasTurnAttemptIdColumn - ? database.prepare( - 'UPDATE paired_task_execution_leases SET turn_attempt_id = ?, turn_attempt_no = ? WHERE rowid = ?', - ) - : database.prepare( - 'UPDATE paired_task_execution_leases SET turn_attempt_no = ? WHERE rowid = ?', - ); - - for (const row of rows) { - const attemptNo = resolveAttemptNoForBackfill( - { - turnId: row.turn_id, - eventTimestamp: row.updated_at ?? row.claimed_at, - rowLabel: `paired_task_execution_leases(rowid=${row.rowid})`, - }, - timingsByTurnId, - ); - if (attemptNo !== null) { - const attemptId = buildPairedTurnAttemptId(row.turn_id, attemptNo); - if (hasTurnAttemptIdColumn) { - update.run(attemptId, attemptNo, row.rowid); - } else { - update.run(attemptNo, row.rowid); - } - } - } - } - - if (tableHasColumn(database, 'service_handoffs', 'turn_attempt_no')) { - const hasTurnAttemptIdColumn = tableHasColumn( - database, - 'service_handoffs', - 'turn_attempt_id', - ); - const rows = database - .prepare( - ` - SELECT id, turn_id, created_at, claimed_at, completed_at - FROM service_handoffs - WHERE turn_id IS NOT NULL - AND turn_attempt_no IS NULL - `, - ) - .all() as Array<{ - id: number; - turn_id: string; - created_at: string | null; - claimed_at: string | null; - completed_at: string | null; - }>; - const update = hasTurnAttemptIdColumn - ? database.prepare( - 'UPDATE service_handoffs SET turn_attempt_id = ?, turn_attempt_no = ? WHERE id = ?', - ) - : database.prepare( - 'UPDATE service_handoffs SET turn_attempt_no = ? WHERE id = ?', - ); - - for (const row of rows) { - const attemptNo = resolveAttemptNoForBackfill( - { - turnId: row.turn_id, - eventTimestamp: row.completed_at ?? row.claimed_at ?? row.created_at, - rowLabel: `service_handoffs(id=${row.id})`, - }, - timingsByTurnId, - ); - if (attemptNo !== null) { - const attemptId = buildPairedTurnAttemptId(row.turn_id, attemptNo); - if (hasTurnAttemptIdColumn) { - update.run(attemptId, attemptNo, row.id); - } else { - update.run(attemptNo, row.id); - } - } - } - } -} - -export function assertPairedTurnAttemptProvenanceIntegrity( - database: Database, -): void { - if (tableHasColumn(database, 'paired_turn_attempts', 'attempt_id')) { - const invalidAttemptIdRow = database - .prepare( - ` - SELECT turn_id, attempt_no, attempt_id - FROM paired_turn_attempts - WHERE attempt_id IS NULL - OR attempt_id != ${buildPairedTurnAttemptIdSql('turn_id', 'attempt_no')} - LIMIT 1 - `, - ) - .get() as - | { - turn_id: string; - attempt_no: number; - attempt_id: string | null; - } - | undefined; - if (invalidAttemptIdRow) { - throw new Error( - `paired_turn_attempts(${invalidAttemptIdRow.turn_id}, attempt=${invalidAttemptIdRow.attempt_no}) has invalid attempt_id provenance`, - ); - } - } - - if (tableHasColumn(database, 'paired_turn_attempts', 'parent_attempt_id')) { - const invalidParentAttemptRow = database - .prepare( - ` - SELECT turn_id, attempt_no, parent_attempt_id - FROM paired_turn_attempts - WHERE ( - attempt_no <= 1 - AND parent_attempt_id IS NOT NULL - ) - OR ( - attempt_no > 1 - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE previous_attempt.turn_id = paired_turn_attempts.turn_id - AND previous_attempt.attempt_no = paired_turn_attempts.attempt_no - 1 - ) - ) - OR ( - attempt_no > 1 - AND ( - parent_attempt_id IS NULL - OR parent_attempt_id != ${buildPairedTurnAttemptParentIdSql('turn_id', 'attempt_no')} - OR NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE COALESCE( - previous_attempt.attempt_id, - ${buildPairedTurnAttemptIdSql( - 'previous_attempt.turn_id', - 'previous_attempt.attempt_no', - )} - ) = paired_turn_attempts.parent_attempt_id - AND previous_attempt.turn_id = paired_turn_attempts.turn_id - AND previous_attempt.attempt_no = paired_turn_attempts.attempt_no - 1 - ) - ) - ) - LIMIT 1 - `, - ) - .get() as - | { - turn_id: string; - attempt_no: number; - parent_attempt_id: string | null; - } - | undefined; - if (invalidParentAttemptRow) { - throw new Error( - `paired_turn_attempts(${invalidParentAttemptRow.turn_id}, attempt=${invalidParentAttemptRow.attempt_no}) has invalid parent_attempt_id provenance`, - ); - } - } - - if (tableHasColumn(database, 'paired_turn_attempts', 'parent_handoff_id')) { - const invalidParentHandoffRow = database - .prepare( - ` - SELECT turn_id, attempt_no, parent_handoff_id - FROM paired_turn_attempts - WHERE parent_handoff_id IS NOT NULL - AND ( - attempt_no <= 1 - OR NOT ${buildPairedTurnAttemptParentHandoffMatchSql({ - parentHandoffIdExpr: 'paired_turn_attempts.parent_handoff_id', - turnIdExpr: 'paired_turn_attempts.turn_id', - parentAttemptIdExpr: 'paired_turn_attempts.parent_attempt_id', - attemptNoExpr: 'paired_turn_attempts.attempt_no', - })} - ) - LIMIT 1 - `, - ) - .get() as - | { - turn_id: string; - attempt_no: number; - parent_handoff_id: number | null; - } - | undefined; - if (invalidParentHandoffRow) { - throw new Error( - `paired_turn_attempts(${invalidParentHandoffRow.turn_id}, attempt=${invalidParentHandoffRow.attempt_no}) has invalid parent_handoff_id provenance`, - ); - } - } - - if ( - tableHasColumn(database, 'paired_turn_attempts', 'continuation_handoff_id') - ) { - const invalidContinuationHandoffRow = database - .prepare( - ` - SELECT turn_id, attempt_no, continuation_handoff_id - FROM paired_turn_attempts - WHERE continuation_handoff_id IS NOT NULL - AND NOT ${buildPairedTurnAttemptContinuationHandoffMatchSql({ - continuationHandoffIdExpr: - 'paired_turn_attempts.continuation_handoff_id', - turnIdExpr: 'paired_turn_attempts.turn_id', - attemptIdExpr: - "COALESCE(paired_turn_attempts.attempt_id, paired_turn_attempts.turn_id || ':attempt:' || CAST(paired_turn_attempts.attempt_no AS TEXT))", - attemptNoExpr: 'paired_turn_attempts.attempt_no', - })} - LIMIT 1 - `, - ) - .get() as - | { - turn_id: string; - attempt_no: number; - continuation_handoff_id: number | null; - } - | undefined; - if (invalidContinuationHandoffRow) { - throw new Error( - `paired_turn_attempts(${invalidContinuationHandoffRow.turn_id}, attempt=${invalidContinuationHandoffRow.attempt_no}) has invalid continuation_handoff_id provenance`, - ); - } - } - - const invalidAttemptRow = database - .prepare( - ` - SELECT turn_id, attempt_no - FROM paired_turn_attempts - WHERE NOT EXISTS ( - SELECT 1 - FROM paired_turns - WHERE paired_turns.turn_id = paired_turn_attempts.turn_id - AND paired_turns.task_id = paired_turn_attempts.task_id - AND paired_turns.task_updated_at = paired_turn_attempts.task_updated_at - AND paired_turns.role = paired_turn_attempts.role - AND paired_turns.intent_kind = paired_turn_attempts.intent_kind - ) - LIMIT 1 - `, - ) - .get() as - | { - turn_id: string; - attempt_no: number; - } - | undefined; - if (invalidAttemptRow) { - throw new Error( - `paired_turn_attempts(${invalidAttemptRow.turn_id}, attempt=${invalidAttemptRow.attempt_no}) does not match its paired_turns identity`, - ); - } - - if (tableHasColumn(database, 'paired_turn_attempts', 'active_run_id')) { - const invalidAttemptActiveRunRow = database - .prepare( - ` - SELECT turn_id, attempt_no - FROM paired_turn_attempts - WHERE ( - state = 'running' - AND ( - active_run_id IS NULL - OR TRIM(active_run_id) = '' - ) - ) - OR ( - state != 'running' - AND active_run_id IS NOT NULL - ) - LIMIT 1 - `, - ) - .get() as - | { - turn_id: string; - attempt_no: number; - } - | undefined; - if (invalidAttemptActiveRunRow) { - throw new Error( - `paired_turn_attempts(${invalidAttemptActiveRunRow.turn_id}, attempt=${invalidAttemptActiveRunRow.attempt_no}) has invalid active_run_id provenance`, - ); - } - } - - if (tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_no')) { - const invalidReservation = database - .prepare( - ` - SELECT rowid - FROM paired_turn_reservations - WHERE turn_attempt_no IS NOT NULL - AND ( - turn_id IS NULL - OR (turn_attempt_id IS NULL AND ${tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_id') ? '1 = 1' : '0 = 1'}) - OR NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.turn_id = paired_turn_reservations.turn_id - AND paired_turn_attempts.attempt_no = paired_turn_reservations.turn_attempt_no - ${ - tableHasColumn( - database, - 'paired_turn_reservations', - 'turn_attempt_id', - ) - ? 'AND paired_turn_attempts.attempt_id = paired_turn_reservations.turn_attempt_id' - : '' - } - AND paired_turn_attempts.task_id = paired_turn_reservations.task_id - AND paired_turn_attempts.task_updated_at = paired_turn_reservations.task_updated_at - AND paired_turn_attempts.role = paired_turn_reservations.turn_role - AND paired_turn_attempts.intent_kind = paired_turn_reservations.intent_kind - ) - ) - LIMIT 1 - `, - ) - .get() as { rowid: number } | undefined; - if (invalidReservation) { - throw new Error( - `paired_turn_reservations(rowid=${invalidReservation.rowid}) has invalid paired_turn_attempt provenance`, - ); - } - } - - if ( - tableHasColumn(database, 'paired_task_execution_leases', 'turn_attempt_no') - ) { - const invalidLease = database - .prepare( - ` - SELECT rowid - FROM paired_task_execution_leases - WHERE turn_attempt_no IS NOT NULL - AND ( - turn_id IS NULL - OR (turn_attempt_id IS NULL AND ${tableHasColumn(database, 'paired_task_execution_leases', 'turn_attempt_id') ? '1 = 1' : '0 = 1'}) - OR NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.turn_id = paired_task_execution_leases.turn_id - AND paired_turn_attempts.attempt_no = paired_task_execution_leases.turn_attempt_no - ${ - tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_id', - ) - ? 'AND paired_turn_attempts.attempt_id = paired_task_execution_leases.turn_attempt_id' - : '' - } - AND paired_turn_attempts.task_id = paired_task_execution_leases.task_id - AND paired_turn_attempts.task_updated_at = paired_task_execution_leases.task_updated_at - AND paired_turn_attempts.role = paired_task_execution_leases.role - AND paired_turn_attempts.intent_kind = paired_task_execution_leases.intent_kind - ) - ) - LIMIT 1 - `, - ) - .get() as { rowid: number } | undefined; - if (invalidLease) { - throw new Error( - `paired_task_execution_leases(rowid=${invalidLease.rowid}) has invalid paired_turn_attempt provenance`, - ); - } - } - - if (tableHasColumn(database, 'service_handoffs', 'turn_attempt_no')) { - const invalidHandoff = database - .prepare( - ` - SELECT id - FROM service_handoffs - WHERE turn_attempt_no IS NOT NULL - AND ( - turn_id IS NULL - OR (turn_attempt_id IS NULL AND ${tableHasColumn(database, 'service_handoffs', 'turn_attempt_id') ? '1 = 1' : '0 = 1'}) - OR paired_task_id IS NULL - OR paired_task_updated_at IS NULL - OR turn_role IS NULL - OR turn_intent_kind IS NULL - OR NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.turn_id = service_handoffs.turn_id - AND paired_turn_attempts.attempt_no = service_handoffs.turn_attempt_no - ${ - tableHasColumn( - database, - 'service_handoffs', - 'turn_attempt_id', - ) - ? 'AND paired_turn_attempts.attempt_id = service_handoffs.turn_attempt_id' - : '' - } - AND paired_turn_attempts.task_id = service_handoffs.paired_task_id - AND paired_turn_attempts.task_updated_at = service_handoffs.paired_task_updated_at - AND paired_turn_attempts.role = service_handoffs.turn_role - AND paired_turn_attempts.intent_kind = service_handoffs.turn_intent_kind - ) - ) - LIMIT 1 - `, - ) - .get() as { id: number } | undefined; - if (invalidHandoff) { - throw new Error( - `service_handoffs(id=${invalidHandoff.id}) has invalid paired_turn_attempt provenance`, - ); - } - } -} - -function rebuildPairedTurnAttemptsWithForeignKeys(database: Database): void { - const tableSql = getTableSql(database, 'paired_turn_attempts') ?? ''; - if ( - tableHasColumn(database, 'paired_turn_attempts', 'attempt_id') && - tableHasColumn(database, 'paired_turn_attempts', 'parent_attempt_id') && - tableHasColumn(database, 'paired_turn_attempts', 'parent_handoff_id') && - tableHasColumn( - database, - 'paired_turn_attempts', - 'continuation_handoff_id', - ) && - tableSql.includes('attempt_id TEXT NOT NULL PRIMARY KEY') && - tableSql.includes('parent_attempt_id TEXT') && - tableSql.includes('parent_handoff_id INTEGER') && - tableSql.includes('continuation_handoff_id INTEGER') && - tableSql.includes('UNIQUE (turn_id, attempt_no)') && - tableHasForeignKey(database, { - tableName: 'paired_turn_attempts', - referencedTable: 'paired_turn_attempts', - fromColumns: ['parent_attempt_id'], - toColumns: ['attempt_id'], - onDelete: 'CASCADE', - }) && - tableHasForeignKey(database, { - tableName: 'paired_turn_attempts', - referencedTable: 'service_handoffs', - fromColumns: ['parent_handoff_id'], - toColumns: ['id'], - onDelete: 'SET NULL', - }) && - tableHasForeignKey(database, { - tableName: 'paired_turn_attempts', - referencedTable: 'service_handoffs', - fromColumns: ['continuation_handoff_id'], - toColumns: ['id'], - onDelete: 'SET NULL', - }) && - tableHasForeignKey(database, { - tableName: 'paired_turn_attempts', - referencedTable: 'paired_turns', - fromColumns: ['turn_id'], - toColumns: ['turn_id'], - onDelete: 'CASCADE', - }) - ) { - return; - } - - const parentAttemptIdSelectSql = tableHasColumn( - database, - 'paired_turn_attempts', - 'parent_attempt_id', - ) - ? `COALESCE( - parent_attempt_id, - CASE - WHEN attempt_no > 1 - AND EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE previous_attempt.turn_id = paired_turn_attempts.turn_id - AND previous_attempt.attempt_no = paired_turn_attempts.attempt_no - 1 - ) - THEN ${buildPairedTurnAttemptParentIdSql('turn_id', 'attempt_no')} - ELSE NULL - END - )` - : `CASE - WHEN attempt_no > 1 - AND EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE previous_attempt.turn_id = paired_turn_attempts.turn_id - AND previous_attempt.attempt_no = paired_turn_attempts.attempt_no - 1 - ) - THEN ${buildPairedTurnAttemptParentIdSql('turn_id', 'attempt_no')} - ELSE NULL - END`; - const parentHandoffIdSelectSql = tableHasColumn( - database, - 'paired_turn_attempts', - 'parent_handoff_id', - ) - ? 'parent_handoff_id' - : 'NULL'; - const continuationHandoffIdSelectSql = tableHasColumn( - database, - 'paired_turn_attempts', - 'continuation_handoff_id', - ) - ? 'continuation_handoff_id' - : 'NULL'; - const activeRunIdSelectSql = tableHasColumn( - database, - 'paired_turn_attempts', - 'active_run_id', - ) - ? `CASE - WHEN state = 'running' THEN active_run_id - ELSE NULL - END` - : `CASE - WHEN state = 'running' - THEN ${ - tableHasColumn(database, 'paired_turns', 'active_run_id') - ? `COALESCE( - ${ - tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_no', - ) - ? `COALESCE( - ( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - AND paired_task_execution_leases.turn_attempt_no = paired_turn_attempts.attempt_no - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - ), - ( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - AND paired_task_execution_leases.turn_attempt_no IS NULL - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - ) - )` - : `( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - )` - }, - ( - SELECT paired_turns.active_run_id - FROM paired_turns - WHERE paired_turns.turn_id = paired_turn_attempts.turn_id - ) - )` - : `( - ${ - tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_no', - ) - ? `COALESCE( - ( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - AND paired_task_execution_leases.turn_attempt_no = paired_turn_attempts.attempt_no - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - ), - ( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - AND paired_task_execution_leases.turn_attempt_no IS NULL - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - ) - )` - : `( - SELECT paired_task_execution_leases.claimed_run_id - FROM paired_task_execution_leases - WHERE paired_task_execution_leases.turn_id = paired_turn_attempts.turn_id - ORDER BY paired_task_execution_leases.updated_at DESC, - paired_task_execution_leases.claimed_at DESC - LIMIT 1 - )` - }` - } - ELSE NULL - END`; - - database.transaction(() => { - database.exec(` - CREATE TABLE paired_turn_attempts_new ( - attempt_id TEXT NOT NULL PRIMARY KEY, - parent_attempt_id TEXT, - parent_handoff_id INTEGER, - continuation_handoff_id INTEGER, - turn_id TEXT NOT NULL, - attempt_no INTEGER NOT NULL, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - state TEXT NOT NULL, - executor_service_id TEXT, - executor_agent_type TEXT, - active_run_id TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - completed_at TEXT, - last_error TEXT, - UNIQUE (turn_id, attempt_no), - FOREIGN KEY (parent_attempt_id) - REFERENCES paired_turn_attempts_new(attempt_id) - ON DELETE CASCADE, - FOREIGN KEY (parent_handoff_id) - REFERENCES service_handoffs(id) - ON DELETE SET NULL, - FOREIGN KEY (continuation_handoff_id) - REFERENCES service_handoffs(id) - ON DELETE SET NULL, - FOREIGN KEY (turn_id) REFERENCES paired_turns(turn_id) ON DELETE CASCADE, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ), - CHECK ( - state IN ( - 'running', - 'delegated', - 'completed', - 'failed', - 'cancelled' - ) - ), - CHECK (executor_agent_type IN ('claude-code', 'codex') OR executor_agent_type IS NULL) - ); - INSERT INTO paired_turn_attempts_new ( - attempt_id, - parent_attempt_id, - parent_handoff_id, - continuation_handoff_id, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - active_run_id, - created_at, - updated_at, - completed_at, - last_error - ) - SELECT - COALESCE(attempt_id, ${buildPairedTurnAttemptIdSql('turn_id', 'attempt_no')}), - ${parentAttemptIdSelectSql}, - ${parentHandoffIdSelectSql}, - ${continuationHandoffIdSelectSql}, - turn_id, - attempt_no, - task_id, - task_updated_at, - role, - intent_kind, - state, - executor_service_id, - executor_agent_type, - ${activeRunIdSelectSql}, - created_at, - updated_at, - completed_at, - last_error - FROM paired_turn_attempts - ORDER BY turn_id ASC, attempt_no ASC; - DROP TABLE paired_turn_attempts; - ALTER TABLE paired_turn_attempts_new RENAME TO paired_turn_attempts; - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_attempt_id - ON paired_turn_attempts(attempt_id); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_parent_attempt_id - ON paired_turn_attempts(parent_attempt_id); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_parent_handoff_id - ON paired_turn_attempts(parent_handoff_id); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_continuation_handoff_id - ON paired_turn_attempts(continuation_handoff_id); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_turn - ON paired_turn_attempts(turn_id, attempt_no); - CREATE INDEX IF NOT EXISTS idx_paired_turn_attempts_task - ON paired_turn_attempts(task_id, task_updated_at, attempt_no); - `); - })(); -} - -export function rebuildPairedTurnsWithoutLegacyScratchColumns( - database: Database, -): void { - if ( - !tableHasColumn(database, 'paired_turns', 'next_parent_handoff_id') && - !tableHasColumn(database, 'paired_turns', 'active_run_id') && - !tableHasColumn(database, 'paired_turns', 'state') && - !tableHasColumn(database, 'paired_turns', 'executor_service_id') && - !tableHasColumn(database, 'paired_turns', 'executor_agent_type') && - !tableHasColumn(database, 'paired_turns', 'attempt_no') && - !tableHasColumn(database, 'paired_turns', 'completed_at') && - !tableHasColumn(database, 'paired_turns', 'last_error') - ) { - return; - } - - database.transaction(() => { - database.exec(` - CREATE TABLE paired_turns_new ( - turn_id TEXT PRIMARY KEY, - task_id TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - INSERT INTO paired_turns_new ( - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - created_at, - updated_at - ) - SELECT - turn_id, - task_id, - task_updated_at, - role, - intent_kind, - created_at, - updated_at - FROM paired_turns - ORDER BY created_at ASC, turn_id ASC; - DROP TABLE paired_turns; - ALTER TABLE paired_turns_new RENAME TO paired_turns; - CREATE INDEX IF NOT EXISTS idx_paired_turns_task - ON paired_turns(task_id, task_updated_at, updated_at); - `); - })(); -} - -function rebuildPairedTurnReservationsWithForeignKeys( - database: Database, -): void { - if ( - !tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_no') || - (tableHasForeignKey(database, { - tableName: 'paired_turn_reservations', - referencedTable: 'paired_turn_attempts', - fromColumns: ['turn_id', 'turn_attempt_no'], - toColumns: ['turn_id', 'attempt_no'], - onDelete: 'CASCADE', - }) && - tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_id') && - tableHasForeignKey(database, { - tableName: 'paired_turn_reservations', - referencedTable: 'paired_turn_attempts', - fromColumns: ['turn_attempt_id'], - toColumns: ['attempt_id'], - onDelete: 'CASCADE', - })) - ) { - return; - } - - database.transaction(() => { - database.exec(` - CREATE TABLE paired_turn_reservations_new ( - chat_jid TEXT NOT NULL, - task_id TEXT NOT NULL, - task_status TEXT NOT NULL, - round_trip_count INTEGER NOT NULL DEFAULT 0, - task_updated_at TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_attempt_id TEXT, - turn_attempt_no INTEGER, - turn_role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - scheduled_run_id TEXT, - consumed_run_id TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - consumed_at TEXT, - PRIMARY KEY (chat_jid, task_id, task_updated_at, intent_kind), - FOREIGN KEY (turn_attempt_id) - REFERENCES paired_turn_attempts(attempt_id) - ON DELETE CASCADE, - FOREIGN KEY (turn_id, turn_attempt_no) - REFERENCES paired_turn_attempts(turn_id, attempt_no) - ON DELETE CASCADE, - CHECK (status IN ('pending', 'completed')), - CHECK (turn_role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - INSERT INTO paired_turn_reservations_new ( - chat_jid, - task_id, - task_status, - round_trip_count, - task_updated_at, - turn_id, - turn_attempt_id, - turn_attempt_no, - turn_role, - intent_kind, - status, - scheduled_run_id, - consumed_run_id, - created_at, - updated_at, - consumed_at - ) - SELECT - chat_jid, - task_id, - task_status, - round_trip_count, - task_updated_at, - turn_id, - COALESCE( - turn_attempt_id, - CASE - WHEN turn_attempt_no IS NOT NULL - THEN ${buildPairedTurnAttemptIdSql('turn_id', 'turn_attempt_no')} - ELSE NULL - END - ), - turn_attempt_no, - turn_role, - intent_kind, - status, - scheduled_run_id, - consumed_run_id, - created_at, - updated_at, - consumed_at - FROM paired_turn_reservations; - DROP TABLE paired_turn_reservations; - ALTER TABLE paired_turn_reservations_new RENAME TO paired_turn_reservations; - CREATE INDEX IF NOT EXISTS idx_paired_turn_reservations_task - ON paired_turn_reservations(task_id, task_updated_at, status); - `); - })(); -} - -function rebuildPairedTaskExecutionLeasesWithForeignKeys( - database: Database, -): void { - if ( - !tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_no', - ) || - (tableHasForeignKey(database, { - tableName: 'paired_task_execution_leases', - referencedTable: 'paired_turn_attempts', - fromColumns: ['turn_id', 'turn_attempt_no'], - toColumns: ['turn_id', 'attempt_no'], - onDelete: 'CASCADE', - }) && - tableHasColumn( - database, - 'paired_task_execution_leases', - 'turn_attempt_id', - ) && - tableHasForeignKey(database, { - tableName: 'paired_task_execution_leases', - referencedTable: 'paired_turn_attempts', - fromColumns: ['turn_attempt_id'], - toColumns: ['attempt_id'], - onDelete: 'CASCADE', - })) - ) { - return; - } - - database.transaction(() => { - database.exec(` - CREATE TABLE paired_task_execution_leases_new ( - task_id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - turn_id TEXT NOT NULL, - turn_attempt_id TEXT, - turn_attempt_no INTEGER, - intent_kind TEXT NOT NULL, - claimed_run_id TEXT NOT NULL, - claimed_service_id TEXT NOT NULL, - task_status TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - claimed_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - expires_at TEXT NOT NULL, - FOREIGN KEY (turn_attempt_id) - REFERENCES paired_turn_attempts(attempt_id) - ON DELETE CASCADE, - FOREIGN KEY (turn_id, turn_attempt_no) - REFERENCES paired_turn_attempts(turn_id, attempt_no) - ON DELETE CASCADE, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - INSERT INTO paired_task_execution_leases_new ( - task_id, - chat_jid, - role, - turn_id, - turn_attempt_id, - turn_attempt_no, - intent_kind, - claimed_run_id, - claimed_service_id, - task_status, - task_updated_at, - claimed_at, - updated_at, - expires_at - ) - SELECT - task_id, - chat_jid, - role, - turn_id, - COALESCE( - turn_attempt_id, - CASE - WHEN turn_attempt_no IS NOT NULL - THEN ${buildPairedTurnAttemptIdSql('turn_id', 'turn_attempt_no')} - ELSE NULL - END - ), - turn_attempt_no, - intent_kind, - claimed_run_id, - claimed_service_id, - task_status, - task_updated_at, - claimed_at, - updated_at, - expires_at - FROM paired_task_execution_leases; - DROP TABLE paired_task_execution_leases; - ALTER TABLE paired_task_execution_leases_new RENAME TO paired_task_execution_leases; - CREATE INDEX IF NOT EXISTS idx_paired_task_execution_leases_expires_at - ON paired_task_execution_leases(expires_at); - `); - })(); -} - -function rebuildServiceHandoffsWithForeignKeys(database: Database): void { - if ( - !tableHasColumn(database, 'service_handoffs', 'turn_attempt_no') || - (tableHasForeignKey(database, { - tableName: 'service_handoffs', - referencedTable: 'paired_turn_attempts', - fromColumns: ['turn_id', 'turn_attempt_no'], - toColumns: ['turn_id', 'attempt_no'], - onDelete: 'CASCADE', - }) && - tableHasColumn(database, 'service_handoffs', 'turn_attempt_id') && - tableHasForeignKey(database, { - tableName: 'service_handoffs', - referencedTable: 'paired_turn_attempts', - fromColumns: ['turn_attempt_id'], - toColumns: ['attempt_id'], - onDelete: 'CASCADE', - })) - ) { - return; - } - - database.transaction(() => { - database.exec(` - CREATE TABLE service_handoffs_new ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - source_service_id TEXT NOT NULL, - target_service_id TEXT NOT NULL, - paired_task_id TEXT, - paired_task_updated_at TEXT, - turn_id TEXT, - turn_attempt_id TEXT, - turn_attempt_no INTEGER, - turn_intent_kind TEXT, - turn_role TEXT, - source_role TEXT, - source_agent_type TEXT, - target_role TEXT, - target_agent_type TEXT NOT NULL, - prompt TEXT NOT NULL, - status TEXT NOT NULL DEFAULT 'pending', - start_seq INTEGER, - end_seq INTEGER, - reason TEXT, - intended_role TEXT, - created_at TEXT NOT NULL DEFAULT (datetime('now')), - claimed_at TEXT, - completed_at TEXT, - last_error TEXT, - FOREIGN KEY (turn_attempt_id) - REFERENCES paired_turn_attempts(attempt_id) - ON DELETE CASCADE, - FOREIGN KEY (turn_id, turn_attempt_no) - REFERENCES paired_turn_attempts(turn_id, attempt_no) - ON DELETE CASCADE, - CHECK (status IN ('pending', 'claimed', 'completed', 'failed')), - CHECK (intended_role IN ('owner', 'reviewer', 'arbiter') OR intended_role IS NULL), - CHECK ( - turn_intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) OR turn_intent_kind IS NULL - ), - CHECK (turn_role IN ('owner', 'reviewer', 'arbiter') OR turn_role IS NULL), - CHECK (source_role IN ('owner', 'reviewer', 'arbiter') OR source_role IS NULL), - CHECK (target_role IN ('owner', 'reviewer', 'arbiter') OR target_role IS NULL) - ); - INSERT INTO service_handoffs_new ( - id, - chat_jid, - group_folder, - source_service_id, - target_service_id, - paired_task_id, - paired_task_updated_at, - turn_id, - turn_attempt_id, - turn_attempt_no, - turn_intent_kind, - turn_role, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - start_seq, - end_seq, - reason, - intended_role, - created_at, - claimed_at, - completed_at, - last_error - ) - SELECT - id, - chat_jid, - group_folder, - source_service_id, - target_service_id, - paired_task_id, - paired_task_updated_at, - turn_id, - COALESCE( - turn_attempt_id, - CASE - WHEN turn_attempt_no IS NOT NULL - THEN ${buildPairedTurnAttemptIdSql('turn_id', 'turn_attempt_no')} - ELSE NULL - END - ), - turn_attempt_no, - turn_intent_kind, - turn_role, - source_role, - source_agent_type, - target_role, - target_agent_type, - prompt, - status, - start_seq, - end_seq, - reason, - intended_role, - created_at, - claimed_at, - completed_at, - last_error - FROM service_handoffs; - DROP TABLE service_handoffs; - ALTER TABLE service_handoffs_new RENAME TO service_handoffs; - CREATE INDEX IF NOT EXISTS idx_service_handoffs_target - ON service_handoffs(status, target_role, target_agent_type, created_at); - `); - })(); -} - -export function rebuildPairedTurnAttemptForeignKeyTables( - database: Database, -): void { - rebuildPairedTurnAttemptsWithForeignKeys(database); - rebuildPairedTurnReservationsWithForeignKeys(database); - rebuildPairedTaskExecutionLeasesWithForeignKeys(database); - rebuildServiceHandoffsWithForeignKeys(database); -} - -export function dropPairedTurnAttemptProvenanceConstraints( - database: Database, -): void { - database.exec(` - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_insert; - DROP TRIGGER IF EXISTS paired_turn_attempts_validate_update; - DROP TRIGGER IF EXISTS paired_turn_reservations_validate_attempt_insert; - DROP TRIGGER IF EXISTS paired_turn_reservations_validate_attempt_update; - DROP TRIGGER IF EXISTS paired_task_execution_leases_validate_attempt_insert; - DROP TRIGGER IF EXISTS paired_task_execution_leases_validate_attempt_update; - DROP TRIGGER IF EXISTS service_handoffs_validate_attempt_insert; - DROP TRIGGER IF EXISTS service_handoffs_validate_attempt_update; - `); -} - -export function applyPairedTurnAttemptProvenanceConstraints( - database: Database, -): void { - database.exec(` - CREATE TRIGGER IF NOT EXISTS paired_turn_attempts_validate_insert - BEFORE INSERT ON paired_turn_attempts - BEGIN - SELECT RAISE(ABORT, 'paired_turn_attempts attempt_id must match turn_id/attempt_no') - WHERE NEW.attempt_id IS NULL - OR NEW.attempt_id != ${buildPairedTurnAttemptIdSql('NEW.turn_id', 'NEW.attempt_no')}; - SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_attempt_id') - WHERE NEW.attempt_no <= 1 - AND NEW.parent_attempt_id IS NOT NULL; - SELECT RAISE(ABORT, 'paired_turn_attempts must preserve contiguous parent lineage') - WHERE NEW.attempt_no > 1 - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE previous_attempt.turn_id = NEW.turn_id - AND previous_attempt.attempt_no = NEW.attempt_no - 1 - ); - SELECT RAISE(ABORT, 'paired_turn_attempts must keep parent_attempt_id lineage') - WHERE NEW.attempt_no > 1 - AND ( - NEW.parent_attempt_id IS NULL - OR NEW.parent_attempt_id != ${buildPairedTurnAttemptParentIdSql('NEW.turn_id', 'NEW.attempt_no')} - ); - SELECT RAISE(ABORT, 'paired_turn_attempts parent_attempt_id must point to the previous attempt of the same turn') - WHERE NEW.parent_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE COALESCE( - previous_attempt.attempt_id, - ${buildPairedTurnAttemptIdSql( - 'previous_attempt.turn_id', - 'previous_attempt.attempt_no', - )} - ) = NEW.parent_attempt_id - AND previous_attempt.turn_id = NEW.turn_id - AND previous_attempt.attempt_no = NEW.attempt_no - 1 - ); - SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_handoff_id') - WHERE NEW.attempt_no <= 1 - AND NEW.parent_handoff_id IS NOT NULL; - SELECT RAISE(ABORT, 'paired_turn_attempts parent_handoff_id must reference the previous attempt handoff of the same turn') - WHERE NEW.parent_handoff_id IS NOT NULL - AND NOT ${buildPairedTurnAttemptParentHandoffMatchSql({ - parentHandoffIdExpr: 'NEW.parent_handoff_id', - turnIdExpr: 'NEW.turn_id', - parentAttemptIdExpr: 'NEW.parent_attempt_id', - attemptNoExpr: 'NEW.attempt_no', - })}; - SELECT RAISE(ABORT, 'paired_turn_attempts continuation_handoff_id must reference a handoff of the same attempt') - WHERE NEW.continuation_handoff_id IS NOT NULL - AND NOT ${buildPairedTurnAttemptContinuationHandoffMatchSql({ - continuationHandoffIdExpr: 'NEW.continuation_handoff_id', - turnIdExpr: 'NEW.turn_id', - attemptIdExpr: 'NEW.attempt_id', - attemptNoExpr: 'NEW.attempt_no', - })}; - SELECT RAISE(ABORT, 'paired_turn_attempts running attempts must declare active_run_id') - WHERE NEW.state = 'running' - AND ( - NEW.active_run_id IS NULL - OR TRIM(NEW.active_run_id) = '' - ); - SELECT RAISE(ABORT, 'paired_turn_attempts only running attempts may declare active_run_id') - WHERE NEW.state != 'running' - AND NEW.active_run_id IS NOT NULL; - SELECT RAISE(ABORT, 'paired_turn_attempts must reference a matching paired_turns row') - WHERE NOT EXISTS ( - SELECT 1 - FROM paired_turns - WHERE paired_turns.turn_id = NEW.turn_id - AND paired_turns.task_id = NEW.task_id - AND paired_turns.task_updated_at = NEW.task_updated_at - AND paired_turns.role = NEW.role - AND paired_turns.intent_kind = NEW.intent_kind - ); - END; - CREATE TRIGGER IF NOT EXISTS paired_turn_attempts_validate_update - BEFORE UPDATE OF attempt_id, parent_attempt_id, parent_handoff_id, continuation_handoff_id, turn_id, attempt_no, task_id, task_updated_at, role, intent_kind, state, executor_service_id, executor_agent_type, active_run_id - ON paired_turn_attempts - BEGIN - SELECT RAISE(ABORT, 'paired_turn_attempts attempt_id must match turn_id/attempt_no') - WHERE NEW.attempt_id IS NULL - OR NEW.attempt_id != ${buildPairedTurnAttemptIdSql('NEW.turn_id', 'NEW.attempt_no')}; - SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_attempt_id') - WHERE NEW.attempt_no <= 1 - AND NEW.parent_attempt_id IS NOT NULL; - SELECT RAISE(ABORT, 'paired_turn_attempts must preserve contiguous parent lineage') - WHERE NEW.attempt_no > 1 - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE previous_attempt.turn_id = NEW.turn_id - AND previous_attempt.attempt_no = NEW.attempt_no - 1 - ); - SELECT RAISE(ABORT, 'paired_turn_attempts must keep parent_attempt_id lineage') - WHERE NEW.attempt_no > 1 - AND ( - NEW.parent_attempt_id IS NULL - OR NEW.parent_attempt_id != ${buildPairedTurnAttemptParentIdSql('NEW.turn_id', 'NEW.attempt_no')} - ); - SELECT RAISE(ABORT, 'paired_turn_attempts parent_attempt_id must point to the previous attempt of the same turn') - WHERE NEW.parent_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts previous_attempt - WHERE COALESCE( - previous_attempt.attempt_id, - ${buildPairedTurnAttemptIdSql( - 'previous_attempt.turn_id', - 'previous_attempt.attempt_no', - )} - ) = NEW.parent_attempt_id - AND previous_attempt.turn_id = NEW.turn_id - AND previous_attempt.attempt_no = NEW.attempt_no - 1 - ); - SELECT RAISE(ABORT, 'paired_turn_attempts attempt 1 cannot declare parent_handoff_id') - WHERE NEW.attempt_no <= 1 - AND NEW.parent_handoff_id IS NOT NULL; - SELECT RAISE(ABORT, 'paired_turn_attempts parent_handoff_id must reference the previous attempt handoff of the same turn') - WHERE NEW.parent_handoff_id IS NOT NULL - AND NOT ${buildPairedTurnAttemptParentHandoffMatchSql({ - parentHandoffIdExpr: 'NEW.parent_handoff_id', - turnIdExpr: 'NEW.turn_id', - parentAttemptIdExpr: 'NEW.parent_attempt_id', - attemptNoExpr: 'NEW.attempt_no', - })}; - SELECT RAISE(ABORT, 'paired_turn_attempts continuation_handoff_id must reference a handoff of the same attempt') - WHERE NEW.continuation_handoff_id IS NOT NULL - AND NOT ${buildPairedTurnAttemptContinuationHandoffMatchSql({ - continuationHandoffIdExpr: 'NEW.continuation_handoff_id', - turnIdExpr: 'NEW.turn_id', - attemptIdExpr: 'NEW.attempt_id', - attemptNoExpr: 'NEW.attempt_no', - })}; - SELECT RAISE(ABORT, 'paired_turn_attempts running attempts must declare active_run_id') - WHERE NEW.state = 'running' - AND ( - NEW.active_run_id IS NULL - OR TRIM(NEW.active_run_id) = '' - ); - SELECT RAISE(ABORT, 'paired_turn_attempts only running attempts may declare active_run_id') - WHERE NEW.state != 'running' - AND NEW.active_run_id IS NOT NULL; - SELECT RAISE(ABORT, 'paired_turn_attempts must reference a matching paired_turns row') - WHERE NOT EXISTS ( - SELECT 1 - FROM paired_turns - WHERE paired_turns.turn_id = NEW.turn_id - AND paired_turns.task_id = NEW.task_id - AND paired_turns.task_updated_at = NEW.task_updated_at - AND paired_turns.role = NEW.role - AND paired_turns.intent_kind = NEW.intent_kind - ); - END; - `); - - if (tableHasColumn(database, 'paired_turn_reservations', 'turn_attempt_no')) { - database.exec(` - CREATE INDEX IF NOT EXISTS idx_paired_turn_reservations_turn_attempt - ON paired_turn_reservations(turn_id, turn_attempt_no); - CREATE INDEX IF NOT EXISTS idx_paired_turn_reservations_turn_attempt_id - ON paired_turn_reservations(turn_attempt_id); - CREATE TRIGGER IF NOT EXISTS paired_turn_reservations_validate_attempt_insert - BEFORE INSERT ON paired_turn_reservations - BEGIN - SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt_no requires turn_id') - WHERE NEW.turn_attempt_no IS NOT NULL - AND NEW.turn_id IS NULL; - SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt provenance must keep attempt_id and attempt_no in sync') - WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); - SELECT RAISE(ABORT, 'paired_turn_reservations turn_attempt_no must reference a matching paired_turn_attempts row') - WHERE NEW.turn_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id - AND paired_turn_attempts.turn_id = NEW.turn_id - AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no - AND paired_turn_attempts.task_id = NEW.task_id - AND paired_turn_attempts.task_updated_at = NEW.task_updated_at - AND paired_turn_attempts.role = NEW.turn_role - AND paired_turn_attempts.intent_kind = NEW.intent_kind - ); - END; - CREATE TRIGGER IF NOT EXISTS paired_turn_reservations_validate_attempt_update - BEFORE UPDATE OF turn_id, turn_attempt_id, turn_attempt_no, task_id, task_updated_at, turn_role, intent_kind - ON paired_turn_reservations - BEGIN - SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt_no requires turn_id') - WHERE NEW.turn_attempt_no IS NOT NULL - AND NEW.turn_id IS NULL; - SELECT RAISE(ABORT, 'paired_turn_reservations.turn_attempt provenance must keep attempt_id and attempt_no in sync') - WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); - SELECT RAISE(ABORT, 'paired_turn_reservations turn_attempt_no must reference a matching paired_turn_attempts row') - WHERE NEW.turn_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id - AND paired_turn_attempts.turn_id = NEW.turn_id - AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no - AND paired_turn_attempts.task_id = NEW.task_id - AND paired_turn_attempts.task_updated_at = NEW.task_updated_at - AND paired_turn_attempts.role = NEW.turn_role - AND paired_turn_attempts.intent_kind = NEW.intent_kind - ); - END; - `); - } - - if ( - tableHasColumn(database, 'paired_task_execution_leases', 'turn_attempt_no') - ) { - database.exec(` - CREATE INDEX IF NOT EXISTS idx_paired_task_execution_leases_turn_attempt - ON paired_task_execution_leases(turn_id, turn_attempt_no); - CREATE INDEX IF NOT EXISTS idx_paired_task_execution_leases_turn_attempt_id - ON paired_task_execution_leases(turn_attempt_id); - CREATE TRIGGER IF NOT EXISTS paired_task_execution_leases_validate_attempt_insert - BEFORE INSERT ON paired_task_execution_leases - BEGIN - SELECT RAISE(ABORT, 'paired_task_execution_leases.turn_attempt_no requires turn_id') - WHERE NEW.turn_attempt_no IS NOT NULL - AND NEW.turn_id IS NULL; - SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt provenance must keep attempt_id and attempt_no in sync') - WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); - SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt_no must reference a matching paired_turn_attempts row') - WHERE NEW.turn_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id - AND paired_turn_attempts.turn_id = NEW.turn_id - AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no - AND paired_turn_attempts.task_id = NEW.task_id - AND paired_turn_attempts.task_updated_at = NEW.task_updated_at - AND paired_turn_attempts.role = NEW.role - AND paired_turn_attempts.intent_kind = NEW.intent_kind - ); - END; - CREATE TRIGGER IF NOT EXISTS paired_task_execution_leases_validate_attempt_update - BEFORE UPDATE OF turn_id, turn_attempt_id, turn_attempt_no, task_id, task_updated_at, role, intent_kind - ON paired_task_execution_leases - BEGIN - SELECT RAISE(ABORT, 'paired_task_execution_leases.turn_attempt_no requires turn_id') - WHERE NEW.turn_attempt_no IS NOT NULL - AND NEW.turn_id IS NULL; - SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt provenance must keep attempt_id and attempt_no in sync') - WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); - SELECT RAISE(ABORT, 'paired_task_execution_leases turn_attempt_no must reference a matching paired_turn_attempts row') - WHERE NEW.turn_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id - AND paired_turn_attempts.turn_id = NEW.turn_id - AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no - AND paired_turn_attempts.task_id = NEW.task_id - AND paired_turn_attempts.task_updated_at = NEW.task_updated_at - AND paired_turn_attempts.role = NEW.role - AND paired_turn_attempts.intent_kind = NEW.intent_kind - ); - END; - `); - } - - if ( - tableHasColumn(database, 'service_handoffs', 'turn_attempt_no') && - tableHasColumn(database, 'service_handoffs', 'paired_task_id') && - tableHasColumn(database, 'service_handoffs', 'paired_task_updated_at') && - tableHasColumn(database, 'service_handoffs', 'turn_role') && - tableHasColumn(database, 'service_handoffs', 'turn_intent_kind') - ) { - database.exec(` - CREATE INDEX IF NOT EXISTS idx_service_handoffs_turn_attempt - ON service_handoffs(turn_id, turn_attempt_no); - CREATE INDEX IF NOT EXISTS idx_service_handoffs_turn_attempt_id - ON service_handoffs(turn_attempt_id); - CREATE TRIGGER IF NOT EXISTS service_handoffs_validate_attempt_insert - BEFORE INSERT ON service_handoffs - BEGIN - SELECT RAISE(ABORT, 'service_handoffs.turn_attempt_no requires paired turn identity') - WHERE NEW.turn_attempt_no IS NOT NULL - AND ( - NEW.turn_id IS NULL - OR NEW.paired_task_id IS NULL - OR NEW.paired_task_updated_at IS NULL - OR NEW.turn_role IS NULL - OR NEW.turn_intent_kind IS NULL - ); - SELECT RAISE(ABORT, 'service_handoffs turn_attempt provenance must keep attempt_id and attempt_no in sync') - WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); - SELECT RAISE(ABORT, 'service_handoffs turn_attempt_no must reference a matching paired_turn_attempts row') - WHERE NEW.turn_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id - AND paired_turn_attempts.turn_id = NEW.turn_id - AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no - AND paired_turn_attempts.task_id = NEW.paired_task_id - AND paired_turn_attempts.task_updated_at = NEW.paired_task_updated_at - AND paired_turn_attempts.role = NEW.turn_role - AND paired_turn_attempts.intent_kind = NEW.turn_intent_kind - ); - END; - CREATE TRIGGER IF NOT EXISTS service_handoffs_validate_attempt_update - BEFORE UPDATE OF turn_id, turn_attempt_id, turn_attempt_no, paired_task_id, paired_task_updated_at, turn_role, turn_intent_kind - ON service_handoffs - BEGIN - SELECT RAISE(ABORT, 'service_handoffs.turn_attempt_no requires paired turn identity') - WHERE NEW.turn_attempt_no IS NOT NULL - AND ( - NEW.turn_id IS NULL - OR NEW.paired_task_id IS NULL - OR NEW.paired_task_updated_at IS NULL - OR NEW.turn_role IS NULL - OR NEW.turn_intent_kind IS NULL - ); - SELECT RAISE(ABORT, 'service_handoffs turn_attempt provenance must keep attempt_id and attempt_no in sync') - WHERE (NEW.turn_attempt_id IS NULL) != (NEW.turn_attempt_no IS NULL); - SELECT RAISE(ABORT, 'service_handoffs turn_attempt_no must reference a matching paired_turn_attempts row') - WHERE NEW.turn_attempt_id IS NOT NULL - AND NOT EXISTS ( - SELECT 1 - FROM paired_turn_attempts - WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id - AND paired_turn_attempts.turn_id = NEW.turn_id - AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no - AND paired_turn_attempts.task_id = NEW.paired_task_id - AND paired_turn_attempts.task_updated_at = NEW.paired_task_updated_at - AND paired_turn_attempts.role = NEW.turn_role - AND paired_turn_attempts.intent_kind = NEW.turn_intent_kind - ); - END; - `); - } -} diff --git a/src/db/schema.snapshot.sql b/src/db/schema.snapshot.sql new file mode 100644 index 0000000..9701381 --- /dev/null +++ b/src/db/schema.snapshot.sql @@ -0,0 +1,68 @@ +CREATE INDEX idx_memories_active ON memories(scope_kind,scope_key,archived_at,created_at); +CREATE INDEX idx_memories_scope ON memories(scope_kind,scope_key); +CREATE INDEX idx_messages_chat_jid_seq ON messages(chat_jid,seq); +CREATE UNIQUE INDEX idx_messages_seq ON messages(seq); +CREATE INDEX idx_next_run ON scheduled_tasks(next_run); +CREATE INDEX idx_paired_task_execution_leases_expires_at ON paired_task_execution_leases(expires_at); +CREATE INDEX idx_paired_task_execution_leases_turn_attempt ON paired_task_execution_leases(turn_id,turn_attempt_no); +CREATE INDEX idx_paired_task_execution_leases_turn_attempt_id ON paired_task_execution_leases(turn_attempt_id); +CREATE INDEX idx_paired_tasks_chat_status ON paired_tasks(chat_jid,status,updated_at); +CREATE INDEX idx_paired_turn_attempts_continuation_handoff_id ON paired_turn_attempts(continuation_handoff_id); +CREATE INDEX idx_paired_turn_attempts_parent_handoff_id ON paired_turn_attempts(parent_handoff_id); +CREATE INDEX idx_paired_turn_attempts_task ON paired_turn_attempts(task_id,task_updated_at,attempt_no); +CREATE INDEX idx_paired_turn_attempts_turn ON paired_turn_attempts(turn_id,attempt_no); +CREATE INDEX idx_paired_turn_outputs_task ON paired_turn_outputs(task_id,turn_number); +CREATE INDEX idx_paired_turn_reservations_task ON paired_turn_reservations(task_id,task_updated_at,status); +CREATE INDEX idx_paired_turn_reservations_turn_attempt ON paired_turn_reservations(turn_id,turn_attempt_no); +CREATE INDEX idx_paired_turn_reservations_turn_attempt_id ON paired_turn_reservations(turn_attempt_id); +CREATE INDEX idx_paired_turns_task ON paired_turns(task_id,task_updated_at,updated_at); +CREATE UNIQUE INDEX idx_paired_workspaces_task_role ON paired_workspaces(task_id,role); +CREATE INDEX idx_room_skill_overrides_room ON room_skill_overrides(chat_jid,agent_type); +CREATE INDEX idx_service_handoffs_target ON service_handoffs(status,target_role,target_agent_type,created_at); +CREATE INDEX idx_service_handoffs_turn_attempt ON service_handoffs(turn_id,turn_attempt_no); +CREATE INDEX idx_service_handoffs_turn_attempt_id ON service_handoffs(turn_attempt_id); +CREATE INDEX idx_status ON scheduled_tasks(status); +CREATE INDEX idx_task_run_logs ON task_run_logs(task_id,run_at); +CREATE INDEX idx_timestamp ON messages(timestamp); +CREATE INDEX idx_work_items_group_agent ON work_items(chat_jid,agent_type,service_id,delivery_role,status); +CREATE UNIQUE INDEX idx_work_items_open ON work_items(chat_jid,agent_type,IFNULL(service_id,''),IFNULL(delivery_role,''))WHERE status IN('produced','delivery_retry'); +CREATE INDEX idx_work_items_status ON work_items(status,updated_at); +CREATE TABLE channel_owner(chat_jid TEXT PRIMARY KEY,owner_service_id TEXT NOT NULL,reviewer_service_id TEXT,arbiter_service_id TEXT,owner_agent_type TEXT,reviewer_agent_type TEXT,arbiter_agent_type TEXT,activated_at TEXT,reason TEXT,CHECK(owner_agent_type IN('claude-code','codex')OR owner_agent_type IS NULL),CHECK(reviewer_agent_type IN('claude-code','codex')OR reviewer_agent_type IS NULL),CHECK(arbiter_agent_type IN('claude-code','codex')OR arbiter_agent_type IS NULL)); +CREATE TABLE chats(jid TEXT PRIMARY KEY,name TEXT,last_message_time TEXT,channel TEXT,is_group INTEGER DEFAULT 0); +CREATE TABLE memories(id INTEGER PRIMARY KEY,scope_kind TEXT NOT NULL,scope_key TEXT NOT NULL,content TEXT NOT NULL,keywords_json TEXT NOT NULL DEFAULT '[]',memory_kind TEXT,source_kind TEXT NOT NULL,source_ref TEXT,created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,last_used_at TEXT,archived_at TEXT); +CREATE VIRTUAL TABLE memories_fts USING fts5(content,keywords,content='',tokenize='unicode61'); +CREATE TABLE 'memories_fts_config'(k PRIMARY KEY,v)WITHOUT ROWID; +CREATE TABLE 'memories_fts_data'(id INTEGER PRIMARY KEY,block BLOB); +CREATE TABLE 'memories_fts_docsize'(id INTEGER PRIMARY KEY,sz BLOB); +CREATE TABLE 'memories_fts_idx'(segid,term,pgno,PRIMARY KEY(segid,term))WITHOUT ROWID; +CREATE TABLE message_sequence(id INTEGER PRIMARY KEY AUTOINCREMENT); +CREATE TABLE messages(id TEXT,chat_jid TEXT,sender TEXT,sender_name TEXT,content TEXT,timestamp TEXT,seq INTEGER,is_from_me INTEGER,is_bot_message INTEGER DEFAULT 0,message_source_kind TEXT NOT NULL DEFAULT 'human',PRIMARY KEY(id,chat_jid),FOREIGN KEY(chat_jid)REFERENCES chats(jid)); +CREATE TABLE paired_projects(chat_jid TEXT PRIMARY KEY,group_folder TEXT NOT NULL,canonical_work_dir TEXT NOT NULL,created_at TEXT NOT NULL,updated_at TEXT NOT NULL); +CREATE TABLE paired_task_execution_leases(task_id TEXT PRIMARY KEY,chat_jid TEXT NOT NULL,role TEXT NOT NULL,turn_id TEXT NOT NULL,turn_attempt_id TEXT,turn_attempt_no INTEGER,intent_kind TEXT NOT NULL,claimed_run_id TEXT NOT NULL,claimed_service_id TEXT NOT NULL,task_status TEXT NOT NULL,task_updated_at TEXT NOT NULL,claimed_at TEXT NOT NULL,updated_at TEXT NOT NULL,expires_at TEXT NOT NULL,FOREIGN KEY(turn_attempt_id)REFERENCES paired_turn_attempts(attempt_id)ON DELETE CASCADE,FOREIGN KEY(turn_id,turn_attempt_no)REFERENCES paired_turn_attempts(turn_id,attempt_no)ON DELETE CASCADE,CHECK(role IN('owner','reviewer','arbiter')),CHECK(intent_kind IN('owner-turn','reviewer-turn','arbiter-turn','owner-follow-up','finalize-owner-turn'))); +CREATE TABLE paired_tasks(id TEXT PRIMARY KEY,chat_jid TEXT NOT NULL,group_folder TEXT NOT NULL,owner_service_id TEXT NOT NULL,reviewer_service_id TEXT NOT NULL,owner_agent_type TEXT,reviewer_agent_type TEXT,arbiter_agent_type TEXT,title TEXT,source_ref TEXT,plan_notes TEXT,review_requested_at TEXT,round_trip_count INTEGER NOT NULL DEFAULT 0,owner_failure_count INTEGER NOT NULL DEFAULT 0,owner_step_done_streak INTEGER NOT NULL DEFAULT 0,finalize_step_done_count INTEGER NOT NULL DEFAULT 0,task_done_then_user_reopen_count INTEGER NOT NULL DEFAULT 0,empty_step_done_streak INTEGER NOT NULL DEFAULT 0,status TEXT NOT NULL DEFAULT 'active',arbiter_verdict TEXT,arbiter_requested_at TEXT,completion_reason TEXT,created_at TEXT NOT NULL,updated_at TEXT NOT NULL,reviewer_failure_count INTEGER NOT NULL DEFAULT 0,arbiter_intervention_count INTEGER NOT NULL DEFAULT 0,CHECK(status IN('active','review_ready','in_review','merge_ready','completed','arbiter_requested','in_arbitration')),CHECK(owner_agent_type IN('claude-code','codex')OR owner_agent_type IS NULL),CHECK(reviewer_agent_type IN('claude-code','codex')OR reviewer_agent_type IS NULL),CHECK(arbiter_agent_type IN('claude-code','codex')OR arbiter_agent_type IS NULL)); +CREATE TABLE paired_turn_attempts(attempt_id TEXT NOT NULL PRIMARY KEY,parent_attempt_id TEXT,parent_handoff_id INTEGER,continuation_handoff_id INTEGER,turn_id TEXT NOT NULL,attempt_no INTEGER NOT NULL,task_id TEXT NOT NULL,task_updated_at TEXT NOT NULL,role TEXT NOT NULL,intent_kind TEXT NOT NULL,state TEXT NOT NULL,executor_service_id TEXT,executor_agent_type TEXT,active_run_id TEXT,created_at TEXT NOT NULL,updated_at TEXT NOT NULL,completed_at TEXT,last_error TEXT,UNIQUE(turn_id,attempt_no),FOREIGN KEY(parent_attempt_id)REFERENCES paired_turn_attempts(attempt_id)ON DELETE CASCADE,FOREIGN KEY(parent_handoff_id)REFERENCES service_handoffs(id)ON DELETE SET NULL,FOREIGN KEY(continuation_handoff_id)REFERENCES service_handoffs(id)ON DELETE SET NULL,FOREIGN KEY(turn_id)REFERENCES paired_turns(turn_id)ON DELETE CASCADE,CHECK(role IN('owner','reviewer','arbiter')),CHECK(intent_kind IN('owner-turn','reviewer-turn','arbiter-turn','owner-follow-up','finalize-owner-turn')),CHECK(state IN('running','delegated','completed','failed','cancelled')),CHECK(executor_agent_type IN('claude-code','codex')OR executor_agent_type IS NULL)); +CREATE TABLE paired_turn_outputs(id INTEGER PRIMARY KEY AUTOINCREMENT,task_id TEXT NOT NULL,turn_number INTEGER NOT NULL,role TEXT NOT NULL,output_text TEXT NOT NULL,verdict TEXT,created_at TEXT NOT NULL,attachment_payload TEXT,UNIQUE(task_id,turn_number,role)); +CREATE TABLE paired_turn_reservations(chat_jid TEXT NOT NULL,task_id TEXT NOT NULL,task_status TEXT NOT NULL,round_trip_count INTEGER NOT NULL DEFAULT 0,task_updated_at TEXT NOT NULL,turn_id TEXT NOT NULL,turn_attempt_id TEXT,turn_attempt_no INTEGER,turn_role TEXT NOT NULL,intent_kind TEXT NOT NULL,status TEXT NOT NULL DEFAULT 'pending',scheduled_run_id TEXT,consumed_run_id TEXT,created_at TEXT NOT NULL,updated_at TEXT NOT NULL,consumed_at TEXT,PRIMARY KEY(chat_jid,task_id,task_updated_at,intent_kind),FOREIGN KEY(turn_attempt_id)REFERENCES paired_turn_attempts(attempt_id)ON DELETE CASCADE,FOREIGN KEY(turn_id,turn_attempt_no)REFERENCES paired_turn_attempts(turn_id,attempt_no)ON DELETE CASCADE,CHECK(status IN('pending','completed')),CHECK(turn_role IN('owner','reviewer','arbiter')),CHECK(intent_kind IN('owner-turn','reviewer-turn','arbiter-turn','owner-follow-up','finalize-owner-turn'))); +CREATE TABLE paired_turns(turn_id TEXT PRIMARY KEY,task_id TEXT NOT NULL,task_updated_at TEXT NOT NULL,role TEXT NOT NULL,intent_kind TEXT NOT NULL,created_at TEXT NOT NULL,updated_at TEXT NOT NULL,progress_text TEXT,progress_updated_at TEXT,CHECK(role IN('owner','reviewer','arbiter')),CHECK(intent_kind IN('owner-turn','reviewer-turn','arbiter-turn','owner-follow-up','finalize-owner-turn'))); +CREATE TABLE paired_workspaces(id TEXT PRIMARY KEY,task_id TEXT NOT NULL,role TEXT NOT NULL,workspace_dir TEXT NOT NULL,snapshot_source_dir TEXT,snapshot_ref TEXT,status TEXT NOT NULL DEFAULT 'ready',snapshot_refreshed_at TEXT,created_at TEXT NOT NULL,updated_at TEXT NOT NULL,CHECK(role IN('owner','reviewer')),CHECK(status IN('ready','stale'))); +CREATE TABLE room_role_overrides(chat_jid TEXT NOT NULL,role TEXT NOT NULL,agent_type TEXT NOT NULL,agent_config_json TEXT,created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY(chat_jid,role),CHECK(role IN('owner','reviewer','arbiter')),CHECK(agent_type IN('claude-code','codex'))); +CREATE TABLE room_settings(chat_jid TEXT PRIMARY KEY,room_mode TEXT NOT NULL,mode_source TEXT NOT NULL DEFAULT 'explicit',name TEXT,folder TEXT,trigger_pattern TEXT,requires_trigger INTEGER DEFAULT 1,is_main INTEGER DEFAULT 0,owner_agent_type TEXT,work_dir TEXT,created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,updated_at TEXT NOT NULL,CHECK(room_mode IN('single','tribunal')),CHECK(owner_agent_type IN('claude-code','codex')OR owner_agent_type IS NULL)); +CREATE TABLE room_skill_overrides(chat_jid TEXT NOT NULL,agent_type TEXT NOT NULL,skill_scope TEXT NOT NULL,skill_name TEXT NOT NULL,enabled INTEGER NOT NULL,created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY(chat_jid,agent_type,skill_scope,skill_name),FOREIGN KEY(chat_jid)REFERENCES room_settings(chat_jid)ON DELETE CASCADE,CHECK(agent_type IN('claude-code','codex')),CHECK(enabled IN(0,1)),CHECK(length(skill_scope)> 0),CHECK(length(skill_name)> 0)); +CREATE TABLE router_state(key TEXT PRIMARY KEY,value TEXT NOT NULL); +CREATE TABLE scheduled_tasks(id TEXT PRIMARY KEY,group_folder TEXT NOT NULL,chat_jid TEXT NOT NULL,agent_type TEXT,ci_provider TEXT,ci_metadata TEXT,max_duration_ms INTEGER,status_message_id TEXT,status_started_at TEXT,prompt TEXT NOT NULL,schedule_type TEXT NOT NULL,schedule_value TEXT NOT NULL,next_run TEXT,last_run TEXT,last_result TEXT,status TEXT DEFAULT 'active',created_at TEXT NOT NULL,context_mode TEXT DEFAULT 'isolated',suspended_until TEXT,room_role TEXT); +CREATE TABLE schema_migrations(version INTEGER PRIMARY KEY,name TEXT NOT NULL,applied_at TEXT NOT NULL DEFAULT(datetime('now'))); +CREATE TABLE service_handoffs(id INTEGER PRIMARY KEY AUTOINCREMENT,chat_jid TEXT NOT NULL,group_folder TEXT NOT NULL,source_service_id TEXT NOT NULL,target_service_id TEXT NOT NULL,paired_task_id TEXT,paired_task_updated_at TEXT,turn_id TEXT,turn_attempt_id TEXT,turn_attempt_no INTEGER,turn_intent_kind TEXT,turn_role TEXT,source_role TEXT,source_agent_type TEXT,target_role TEXT,target_agent_type TEXT NOT NULL,prompt TEXT NOT NULL,status TEXT NOT NULL DEFAULT 'pending',start_seq INTEGER,end_seq INTEGER,reason TEXT,intended_role TEXT,created_at TEXT NOT NULL DEFAULT(datetime('now')),claimed_at TEXT,completed_at TEXT,last_error TEXT,FOREIGN KEY(turn_attempt_id)REFERENCES paired_turn_attempts(attempt_id)ON DELETE CASCADE,FOREIGN KEY(turn_id,turn_attempt_no)REFERENCES paired_turn_attempts(turn_id,attempt_no)ON DELETE CASCADE,CHECK(status IN('pending','claimed','completed','failed')),CHECK(intended_role IN('owner','reviewer','arbiter')OR intended_role IS NULL),CHECK(turn_intent_kind IN('owner-turn','reviewer-turn','arbiter-turn','owner-follow-up','finalize-owner-turn')OR turn_intent_kind IS NULL),CHECK(turn_role IN('owner','reviewer','arbiter')OR turn_role IS NULL),CHECK(source_role IN('owner','reviewer','arbiter')OR source_role IS NULL),CHECK(target_role IN('owner','reviewer','arbiter')OR target_role IS NULL)); +CREATE TABLE sessions(group_folder TEXT NOT NULL,agent_type TEXT NOT NULL DEFAULT 'claude-code',session_id TEXT NOT NULL,PRIMARY KEY(group_folder,agent_type)); +CREATE TABLE task_run_logs(id INTEGER PRIMARY KEY AUTOINCREMENT,task_id TEXT NOT NULL,run_at TEXT NOT NULL,duration_ms INTEGER NOT NULL,status TEXT NOT NULL,result TEXT,error TEXT,FOREIGN KEY(task_id)REFERENCES scheduled_tasks(id)); +CREATE TABLE work_items(id INTEGER PRIMARY KEY AUTOINCREMENT,group_folder TEXT NOT NULL,chat_jid TEXT NOT NULL,agent_type TEXT NOT NULL,service_id TEXT,delivery_role TEXT,status TEXT NOT NULL DEFAULT 'produced',start_seq INTEGER,end_seq INTEGER,result_payload TEXT NOT NULL,attachment_payload TEXT,delivery_attempts INTEGER NOT NULL DEFAULT 0,delivery_message_id TEXT,last_error TEXT,created_at TEXT NOT NULL DEFAULT(datetime('now')),updated_at TEXT NOT NULL DEFAULT(datetime('now')),delivered_at TEXT,CHECK(status IN('produced','delivery_retry','delivered')),CHECK(delivery_role IN('owner','reviewer','arbiter')OR delivery_role IS NULL)); +CREATE TRIGGER memories_ad AFTER DELETE ON memories BEGIN INSERT INTO memories_fts(memories_fts,rowid,content,keywords)VALUES('delete',old.id,old.content,old.keywords_json);END; +CREATE TRIGGER memories_ai AFTER INSERT ON memories BEGIN INSERT INTO memories_fts(rowid,content,keywords)VALUES(new.id,new.content,new.keywords_json);END; +CREATE TRIGGER memories_au AFTER UPDATE ON memories BEGIN INSERT INTO memories_fts(memories_fts,rowid,content,keywords)VALUES('delete',old.id,old.content,old.keywords_json);INSERT INTO memories_fts(rowid,content,keywords)VALUES(new.id,new.content,new.keywords_json);END; +CREATE TRIGGER paired_task_execution_leases_validate_attempt_insert BEFORE INSERT ON paired_task_execution_leases BEGIN SELECT RAISE(ABORT,'paired_task_execution_leases.turn_attempt_no requires turn_id')WHERE NEW.turn_attempt_no IS NOT NULL AND NEW.turn_id IS NULL;SELECT RAISE(ABORT,'paired_task_execution_leases turn_attempt provenance must keep attempt_id and attempt_no in sync')WHERE(NEW.turn_attempt_id IS NULL)!=(NEW.turn_attempt_no IS NULL);SELECT RAISE(ABORT,'paired_task_execution_leases turn_attempt_no must reference a matching paired_turn_attempts row')WHERE NEW.turn_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id AND paired_turn_attempts.turn_id = NEW.turn_id AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no AND paired_turn_attempts.task_id = NEW.task_id AND paired_turn_attempts.task_updated_at = NEW.task_updated_at AND paired_turn_attempts.role = NEW.role AND paired_turn_attempts.intent_kind = NEW.intent_kind);END; +CREATE TRIGGER paired_task_execution_leases_validate_attempt_update BEFORE UPDATE OF turn_id,turn_attempt_id,turn_attempt_no,task_id,task_updated_at,role,intent_kind ON paired_task_execution_leases BEGIN SELECT RAISE(ABORT,'paired_task_execution_leases.turn_attempt_no requires turn_id')WHERE NEW.turn_attempt_no IS NOT NULL AND NEW.turn_id IS NULL;SELECT RAISE(ABORT,'paired_task_execution_leases turn_attempt provenance must keep attempt_id and attempt_no in sync')WHERE(NEW.turn_attempt_id IS NULL)!=(NEW.turn_attempt_no IS NULL);SELECT RAISE(ABORT,'paired_task_execution_leases turn_attempt_no must reference a matching paired_turn_attempts row')WHERE NEW.turn_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id AND paired_turn_attempts.turn_id = NEW.turn_id AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no AND paired_turn_attempts.task_id = NEW.task_id AND paired_turn_attempts.task_updated_at = NEW.task_updated_at AND paired_turn_attempts.role = NEW.role AND paired_turn_attempts.intent_kind = NEW.intent_kind);END; +CREATE TRIGGER paired_turn_attempts_validate_insert BEFORE INSERT ON paired_turn_attempts BEGIN SELECT RAISE(ABORT,'paired_turn_attempts attempt_id must match turn_id/attempt_no')WHERE NEW.attempt_id IS NULL OR NEW.attempt_id != NEW.turn_id || ':attempt:' || CAST(NEW.attempt_no AS TEXT);SELECT RAISE(ABORT,'paired_turn_attempts attempt 1 cannot declare parent_attempt_id')WHERE NEW.attempt_no <= 1 AND NEW.parent_attempt_id IS NOT NULL;SELECT RAISE(ABORT,'paired_turn_attempts must preserve contiguous parent lineage')WHERE NEW.attempt_no > 1 AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts previous_attempt WHERE previous_attempt.turn_id = NEW.turn_id AND previous_attempt.attempt_no = NEW.attempt_no - 1);SELECT RAISE(ABORT,'paired_turn_attempts must keep parent_attempt_id lineage')WHERE NEW.attempt_no > 1 AND(NEW.parent_attempt_id IS NULL OR NEW.parent_attempt_id != CASE WHEN NEW.attempt_no <= 1 THEN NULL ELSE NEW.turn_id || ':attempt:' || CAST((NEW.attempt_no)- 1 AS TEXT)END);SELECT RAISE(ABORT,'paired_turn_attempts parent_attempt_id must point to the previous attempt of the same turn')WHERE NEW.parent_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts previous_attempt WHERE COALESCE(previous_attempt.attempt_id,previous_attempt.turn_id || ':attempt:' || CAST(previous_attempt.attempt_no AS TEXT))= NEW.parent_attempt_id AND previous_attempt.turn_id = NEW.turn_id AND previous_attempt.attempt_no = NEW.attempt_no - 1);SELECT RAISE(ABORT,'paired_turn_attempts attempt 1 cannot declare parent_handoff_id')WHERE NEW.attempt_no <= 1 AND NEW.parent_handoff_id IS NOT NULL;SELECT RAISE(ABORT,'paired_turn_attempts parent_handoff_id must reference the previous attempt handoff of the same turn')WHERE NEW.parent_handoff_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM service_handoffs WHERE service_handoffs.id = NEW.parent_handoff_id AND service_handoffs.turn_id = NEW.turn_id AND service_handoffs.status = 'failed' AND(service_handoffs.turn_attempt_id = NEW.parent_attempt_id OR(service_handoffs.turn_attempt_id IS NULL AND service_handoffs.turn_attempt_no =(NEW.attempt_no)- 1)));SELECT RAISE(ABORT,'paired_turn_attempts continuation_handoff_id must reference a handoff of the same attempt')WHERE NEW.continuation_handoff_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM service_handoffs WHERE service_handoffs.id = NEW.continuation_handoff_id AND service_handoffs.turn_id = NEW.turn_id AND(service_handoffs.turn_attempt_id = NEW.attempt_id OR(service_handoffs.turn_attempt_id IS NULL AND service_handoffs.turn_attempt_no = NEW.attempt_no)));SELECT RAISE(ABORT,'paired_turn_attempts running attempts must declare active_run_id')WHERE NEW.state = 'running' AND(NEW.active_run_id IS NULL OR TRIM(NEW.active_run_id)= '');SELECT RAISE(ABORT,'paired_turn_attempts only running attempts may declare active_run_id')WHERE NEW.state != 'running' AND NEW.active_run_id IS NOT NULL;SELECT RAISE(ABORT,'paired_turn_attempts must reference a matching paired_turns row')WHERE NOT EXISTS(SELECT 1 FROM paired_turns WHERE paired_turns.turn_id = NEW.turn_id AND paired_turns.task_id = NEW.task_id AND paired_turns.task_updated_at = NEW.task_updated_at AND paired_turns.role = NEW.role AND paired_turns.intent_kind = NEW.intent_kind);END; +CREATE TRIGGER paired_turn_attempts_validate_update BEFORE UPDATE OF attempt_id,parent_attempt_id,parent_handoff_id,continuation_handoff_id,turn_id,attempt_no,task_id,task_updated_at,role,intent_kind,state,executor_service_id,executor_agent_type,active_run_id ON paired_turn_attempts BEGIN SELECT RAISE(ABORT,'paired_turn_attempts attempt_id must match turn_id/attempt_no')WHERE NEW.attempt_id IS NULL OR NEW.attempt_id != NEW.turn_id || ':attempt:' || CAST(NEW.attempt_no AS TEXT);SELECT RAISE(ABORT,'paired_turn_attempts attempt 1 cannot declare parent_attempt_id')WHERE NEW.attempt_no <= 1 AND NEW.parent_attempt_id IS NOT NULL;SELECT RAISE(ABORT,'paired_turn_attempts must preserve contiguous parent lineage')WHERE NEW.attempt_no > 1 AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts previous_attempt WHERE previous_attempt.turn_id = NEW.turn_id AND previous_attempt.attempt_no = NEW.attempt_no - 1);SELECT RAISE(ABORT,'paired_turn_attempts must keep parent_attempt_id lineage')WHERE NEW.attempt_no > 1 AND(NEW.parent_attempt_id IS NULL OR NEW.parent_attempt_id != CASE WHEN NEW.attempt_no <= 1 THEN NULL ELSE NEW.turn_id || ':attempt:' || CAST((NEW.attempt_no)- 1 AS TEXT)END);SELECT RAISE(ABORT,'paired_turn_attempts parent_attempt_id must point to the previous attempt of the same turn')WHERE NEW.parent_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts previous_attempt WHERE COALESCE(previous_attempt.attempt_id,previous_attempt.turn_id || ':attempt:' || CAST(previous_attempt.attempt_no AS TEXT))= NEW.parent_attempt_id AND previous_attempt.turn_id = NEW.turn_id AND previous_attempt.attempt_no = NEW.attempt_no - 1);SELECT RAISE(ABORT,'paired_turn_attempts attempt 1 cannot declare parent_handoff_id')WHERE NEW.attempt_no <= 1 AND NEW.parent_handoff_id IS NOT NULL;SELECT RAISE(ABORT,'paired_turn_attempts parent_handoff_id must reference the previous attempt handoff of the same turn')WHERE NEW.parent_handoff_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM service_handoffs WHERE service_handoffs.id = NEW.parent_handoff_id AND service_handoffs.turn_id = NEW.turn_id AND service_handoffs.status = 'failed' AND(service_handoffs.turn_attempt_id = NEW.parent_attempt_id OR(service_handoffs.turn_attempt_id IS NULL AND service_handoffs.turn_attempt_no =(NEW.attempt_no)- 1)));SELECT RAISE(ABORT,'paired_turn_attempts continuation_handoff_id must reference a handoff of the same attempt')WHERE NEW.continuation_handoff_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM service_handoffs WHERE service_handoffs.id = NEW.continuation_handoff_id AND service_handoffs.turn_id = NEW.turn_id AND(service_handoffs.turn_attempt_id = NEW.attempt_id OR(service_handoffs.turn_attempt_id IS NULL AND service_handoffs.turn_attempt_no = NEW.attempt_no)));SELECT RAISE(ABORT,'paired_turn_attempts running attempts must declare active_run_id')WHERE NEW.state = 'running' AND(NEW.active_run_id IS NULL OR TRIM(NEW.active_run_id)= '');SELECT RAISE(ABORT,'paired_turn_attempts only running attempts may declare active_run_id')WHERE NEW.state != 'running' AND NEW.active_run_id IS NOT NULL;SELECT RAISE(ABORT,'paired_turn_attempts must reference a matching paired_turns row')WHERE NOT EXISTS(SELECT 1 FROM paired_turns WHERE paired_turns.turn_id = NEW.turn_id AND paired_turns.task_id = NEW.task_id AND paired_turns.task_updated_at = NEW.task_updated_at AND paired_turns.role = NEW.role AND paired_turns.intent_kind = NEW.intent_kind);END; +CREATE TRIGGER paired_turn_reservations_validate_attempt_insert BEFORE INSERT ON paired_turn_reservations BEGIN SELECT RAISE(ABORT,'paired_turn_reservations.turn_attempt_no requires turn_id')WHERE NEW.turn_attempt_no IS NOT NULL AND NEW.turn_id IS NULL;SELECT RAISE(ABORT,'paired_turn_reservations.turn_attempt provenance must keep attempt_id and attempt_no in sync')WHERE(NEW.turn_attempt_id IS NULL)!=(NEW.turn_attempt_no IS NULL);SELECT RAISE(ABORT,'paired_turn_reservations turn_attempt_no must reference a matching paired_turn_attempts row')WHERE NEW.turn_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id AND paired_turn_attempts.turn_id = NEW.turn_id AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no AND paired_turn_attempts.task_id = NEW.task_id AND paired_turn_attempts.task_updated_at = NEW.task_updated_at AND paired_turn_attempts.role = NEW.turn_role AND paired_turn_attempts.intent_kind = NEW.intent_kind);END; +CREATE TRIGGER paired_turn_reservations_validate_attempt_update BEFORE UPDATE OF turn_id,turn_attempt_id,turn_attempt_no,task_id,task_updated_at,turn_role,intent_kind ON paired_turn_reservations BEGIN SELECT RAISE(ABORT,'paired_turn_reservations.turn_attempt_no requires turn_id')WHERE NEW.turn_attempt_no IS NOT NULL AND NEW.turn_id IS NULL;SELECT RAISE(ABORT,'paired_turn_reservations.turn_attempt provenance must keep attempt_id and attempt_no in sync')WHERE(NEW.turn_attempt_id IS NULL)!=(NEW.turn_attempt_no IS NULL);SELECT RAISE(ABORT,'paired_turn_reservations turn_attempt_no must reference a matching paired_turn_attempts row')WHERE NEW.turn_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id AND paired_turn_attempts.turn_id = NEW.turn_id AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no AND paired_turn_attempts.task_id = NEW.task_id AND paired_turn_attempts.task_updated_at = NEW.task_updated_at AND paired_turn_attempts.role = NEW.turn_role AND paired_turn_attempts.intent_kind = NEW.intent_kind);END; +CREATE TRIGGER service_handoffs_validate_attempt_insert BEFORE INSERT ON service_handoffs BEGIN SELECT RAISE(ABORT,'service_handoffs.turn_attempt_no requires paired turn identity')WHERE NEW.turn_attempt_no IS NOT NULL AND(NEW.turn_id IS NULL OR NEW.paired_task_id IS NULL OR NEW.paired_task_updated_at IS NULL OR NEW.turn_role IS NULL OR NEW.turn_intent_kind IS NULL);SELECT RAISE(ABORT,'service_handoffs turn_attempt provenance must keep attempt_id and attempt_no in sync')WHERE(NEW.turn_attempt_id IS NULL)!=(NEW.turn_attempt_no IS NULL);SELECT RAISE(ABORT,'service_handoffs turn_attempt_no must reference a matching paired_turn_attempts row')WHERE NEW.turn_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id AND paired_turn_attempts.turn_id = NEW.turn_id AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no AND paired_turn_attempts.task_id = NEW.paired_task_id AND paired_turn_attempts.task_updated_at = NEW.paired_task_updated_at AND paired_turn_attempts.role = NEW.turn_role AND paired_turn_attempts.intent_kind = NEW.turn_intent_kind);END; +CREATE TRIGGER service_handoffs_validate_attempt_update BEFORE UPDATE OF turn_id,turn_attempt_id,turn_attempt_no,paired_task_id,paired_task_updated_at,turn_role,turn_intent_kind ON service_handoffs BEGIN SELECT RAISE(ABORT,'service_handoffs.turn_attempt_no requires paired turn identity')WHERE NEW.turn_attempt_no IS NOT NULL AND(NEW.turn_id IS NULL OR NEW.paired_task_id IS NULL OR NEW.paired_task_updated_at IS NULL OR NEW.turn_role IS NULL OR NEW.turn_intent_kind IS NULL);SELECT RAISE(ABORT,'service_handoffs turn_attempt provenance must keep attempt_id and attempt_no in sync')WHERE(NEW.turn_attempt_id IS NULL)!=(NEW.turn_attempt_no IS NULL);SELECT RAISE(ABORT,'service_handoffs turn_attempt_no must reference a matching paired_turn_attempts row')WHERE NEW.turn_attempt_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM paired_turn_attempts WHERE paired_turn_attempts.attempt_id = NEW.turn_attempt_id AND paired_turn_attempts.turn_id = NEW.turn_id AND paired_turn_attempts.attempt_no = NEW.turn_attempt_no AND paired_turn_attempts.task_id = NEW.paired_task_id AND paired_turn_attempts.task_updated_at = NEW.paired_task_updated_at AND paired_turn_attempts.role = NEW.turn_role AND paired_turn_attempts.intent_kind = NEW.turn_intent_kind);END; diff --git a/src/db/schema.test.ts b/src/db/schema.test.ts new file mode 100644 index 0000000..3e431e9 --- /dev/null +++ b/src/db/schema.test.ts @@ -0,0 +1,264 @@ +import { Database } from 'bun:sqlite'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; + +import { afterEach, describe, expect, it } from 'vitest'; + +import { initializeDatabaseSchema } from './bootstrap.js'; +import { + applySchemaMigrations, + BASELINE_SCHEMA_VERSION, +} from './migrations/index.js'; + +// Normalized schema a fresh database must end up with. It was dumped from the +// production database when migrations 1-22 were squashed into base-schema.ts; +// update it together with any migration added after the baseline. +const SCHEMA_SNAPSHOT_PATH = path.join( + import.meta.dirname, + 'schema.snapshot.sql', +); + +// Names of the squashed migrations as recorded in the production database. +const PRODUCTION_MIGRATION_ROWS = [ + 'legacy_schema_bundle', + 'scheduled_task_columns', + 'room_registration_canonical_columns', + 'message_seq_and_work_item_indexes', + 'sessions_composite_key', + 'chat_channel_metadata', + 'runtime_service_metadata', + 'paired_task_schema_cleanup', + 'paired_workspace_project_schema_cleanup', + 'paired_turn_provenance_upgrade', + 'owner_failure_count', + 'paired_verdict_and_step_telemetry', + 'message_source_kind', + 'work_item_attachments', + 'reviewer_failure_count', + 'room_skill_overrides', + 'scheduled_task_room_role', + 'paired_turn_output_attachments', + 'turn_progress_text_recovery', + 'arbiter_intervention_count', + 'reviewer_failure_count', + 'turn_progress_text_compat', +].map((name, index) => ({ version: index + 1, name })); + +function normalizeSql(sql: string): string { + return sql + .replace(/\s+/g, ' ') + .replace(/\s*([(),;])\s*/g, '$1') + .replace(/"/g, '') + .trim(); +} + +function dumpNormalizedSchema(database: Database): string { + const rows = database + .prepare( + `SELECT sql + FROM sqlite_master + WHERE sql IS NOT NULL + AND name NOT LIKE 'sqlite_%' + ORDER BY type, name`, + ) + .all() as Array<{ sql: string }>; + return rows.map((row) => `${normalizeSql(row.sql)};\n`).join(''); +} + +function dumpRawSchema(database: Database): unknown[] { + return database + .prepare( + 'SELECT type, name, tbl_name, sql FROM sqlite_master ORDER BY name', + ) + .all(); +} + +function dumpAllRows(database: Database): Record { + const tables = database + .prepare( + `SELECT name + FROM sqlite_master + WHERE type = 'table' + AND name NOT LIKE 'memories_fts%' + ORDER BY name`, + ) + .all() as Array<{ name: string }>; + return Object.fromEntries( + tables.map(({ name }) => [ + name, + database.prepare(`SELECT rowid AS _rowid, * FROM "${name}"`).all(), + ]), + ); +} + +function getSchemaMigrationRows( + database: Database, +): Array<{ version: number; name: string }> { + return database + .prepare('SELECT version, name FROM schema_migrations ORDER BY version') + .all() as Array<{ version: number; name: string }>; +} + +describe('database schema baseline', () => { + const tempDirs: string[] = []; + + function tempDbPath(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-schema-')); + tempDirs.push(dir); + return path.join(dir, 'messages.db'); + } + + afterEach(() => { + for (const dir of tempDirs.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }); + } + }); + + it('builds a fresh database that matches the production schema snapshot', () => { + const database = new Database(':memory:'); + initializeDatabaseSchema(database); + + expect(dumpNormalizedSchema(database)).toBe( + fs.readFileSync(SCHEMA_SNAPSHOT_PATH, 'utf8'), + ); + expect(getSchemaMigrationRows(database)).toEqual([ + { version: BASELINE_SCHEMA_VERSION, name: 'baseline' }, + ]); + }); + + it('opens a database already at the baseline without touching schema or data', () => { + const dbPath = tempDbPath(); + const seeded = new Database(dbPath); + applySchemaMigrations(seeded); + // Mirror the production schema_migrations history (versions 1-22). + seeded.exec('DELETE FROM schema_migrations'); + const insertMigration = seeded.prepare( + `INSERT INTO schema_migrations (version, name, applied_at) + VALUES (?, ?, '2026-04-26 13:08:10')`, + ); + for (const row of PRODUCTION_MIGRATION_ROWS) { + insertMigration.run(row.version, row.name); + } + seeded.exec(` + INSERT INTO chats (jid, name, last_message_time, channel, is_group) + VALUES ('dc:1', 'Room', '2026-04-24T00:00:00.000Z', 'discord', 1); + INSERT INTO messages (id, chat_jid, sender, sender_name, content, timestamp, seq, is_from_me) + VALUES ('m1', 'dc:1', 'u', 'User', 'hello', '2026-04-24T00:00:00.000Z', 1, 0); + INSERT INTO room_settings (chat_jid, room_mode, folder, updated_at) + VALUES ('dc:1', 'tribunal', 'room', '2026-04-24T00:00:00.000Z'); + INSERT INTO paired_tasks ( + id, chat_jid, group_folder, owner_service_id, reviewer_service_id, + created_at, updated_at + ) VALUES ('t1', 'dc:1', 'room', 'claude', 'codex-review', + '2026-04-24T00:00:00.000Z', '2026-04-24T00:00:00.000Z'); + `); + const schemaBefore = dumpRawSchema(seeded); + const rowsBefore = dumpAllRows(seeded); + seeded.close(); + + const reopened = new Database(dbPath); + initializeDatabaseSchema(reopened); + + expect(dumpRawSchema(reopened)).toEqual(schemaBefore); + expect(dumpAllRows(reopened)).toEqual(rowsBefore); + expect(getSchemaMigrationRows(reopened)).toEqual(PRODUCTION_MIGRATION_ROWS); + reopened.close(); + }); + + it('refuses a database that predates the baseline and leaves it unchanged', () => { + const dbPath = tempDbPath(); + const legacy = new Database(dbPath); + legacy.exec(` + CREATE TABLE schema_migrations ( + version INTEGER PRIMARY KEY, + name TEXT NOT NULL, + applied_at TEXT NOT NULL DEFAULT (datetime('now')) + ); + INSERT INTO schema_migrations (version, name) VALUES (12, 'old'); + CREATE TABLE chats (jid TEXT PRIMARY KEY, name TEXT); + `); + const schemaBefore = dumpRawSchema(legacy); + + expect(() => initializeDatabaseSchema(legacy)).toThrow( + /schema version 12 predates the squashed baseline 22/, + ); + expect(dumpRawSchema(legacy)).toEqual(schemaBefore); + legacy.close(); + + const unversioned = new Database(':memory:'); + unversioned.exec('CREATE TABLE chats (jid TEXT PRIMARY KEY, name TEXT)'); + const unversionedBefore = dumpRawSchema(unversioned); + expect(() => initializeDatabaseSchema(unversioned)).toThrow( + /schema version 0 predates the squashed baseline 22/, + ); + expect(dumpRawSchema(unversioned)).toEqual(unversionedBefore); + }); + + it('applies migrations added after the baseline exactly once', () => { + const database = new Database(':memory:'); + let applyCount = 0; + const migrations = [ + { + version: BASELINE_SCHEMA_VERSION + 1, + name: 'add_chat_note', + apply(db: Database) { + applyCount += 1; + db.exec('ALTER TABLE chats ADD COLUMN note TEXT'); + }, + }, + ]; + + applySchemaMigrations(database, migrations); + applySchemaMigrations(database, migrations); + + expect(applyCount).toBe(1); + expect(getSchemaMigrationRows(database)).toEqual([ + { version: BASELINE_SCHEMA_VERSION, name: 'baseline' }, + { version: BASELINE_SCHEMA_VERSION + 1, name: 'add_chat_note' }, + ]); + expect( + ( + database.prepare('PRAGMA table_info(chats)').all() as Array<{ + name: string; + }> + ).map((column) => column.name), + ).toContain('note'); + }); + + it('rolls back a failing migration and does not record it', () => { + const database = new Database(':memory:'); + applySchemaMigrations(database); + const failing = [ + { + version: BASELINE_SCHEMA_VERSION + 1, + name: 'broken', + apply(db: Database) { + db.exec('ALTER TABLE chats ADD COLUMN note TEXT'); + throw new Error('boom'); + }, + }, + ]; + + expect(() => applySchemaMigrations(database, failing)).toThrow('boom'); + expect(getSchemaMigrationRows(database)).toEqual([ + { version: BASELINE_SCHEMA_VERSION, name: 'baseline' }, + ]); + expect( + ( + database.prepare('PRAGMA table_info(chats)').all() as Array<{ + name: string; + }> + ).map((column) => column.name), + ).not.toContain('note'); + }); + + it('rejects a new migration numbered at or below the baseline', () => { + const database = new Database(':memory:'); + expect(() => + applySchemaMigrations(database, [ + { version: 5, name: 'too_low', apply() {} }, + ]), + ).toThrow(/must be numbered above the baseline/); + }); +}); diff --git a/src/db/schema.ts b/src/db/schema.ts deleted file mode 100644 index bf4d5fa..0000000 --- a/src/db/schema.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Database } from 'bun:sqlite'; - -import { tryExecMigration } from './migrations/helpers.js'; - -// Legacy monolithic migration entrypoint kept only for databases that predate -// ordered schema migration tracking. New schema changes belong in -// src/db/migrations/*. - -export function applyLegacySchemaMigrations( - database: Database, - _args: { - assistantName: string; - }, -): void { - // Some legacy columns must exist before later numbered migrations can run. - // `work_items.service_id` / `delivery_role` are referenced by v4 indexes. - tryExecMigration( - database, - `ALTER TABLE work_items ADD COLUMN delivery_role TEXT`, - ); - tryExecMigration( - database, - `ALTER TABLE work_items ADD COLUMN service_id TEXT`, - ); -} diff --git a/src/db/sessions.ts b/src/db/sessions.ts index 07bf82a..3bcca55 100644 --- a/src/db/sessions.ts +++ b/src/db/sessions.ts @@ -1,105 +1,5 @@ import { Database } from 'bun:sqlite'; -import { AgentType } from '../types.js'; - -export type ServiceShadowAgentTypeResolver = ( - serviceId: string, -) => AgentType | undefined; - -function hasLegacyServiceSessionsTable(database: Database): boolean { - return Boolean( - database - .prepare( - `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'service_sessions'`, - ) - .get(), - ); -} - -export function migrateSessionsTableToCompositePk( - database: Database, - defaultAgentType: AgentType, -): void { - const sessionCols = database - .prepare('PRAGMA table_info(sessions)') - .all() as Array<{ name: string }>; - if (sessionCols.some((col) => col.name === 'agent_type')) { - return; - } - - database.exec(` - CREATE TABLE sessions_new ( - group_folder TEXT NOT NULL, - agent_type TEXT NOT NULL DEFAULT '${defaultAgentType}', - session_id TEXT NOT NULL, - PRIMARY KEY (group_folder, agent_type) - ); - `); - database - .prepare( - `INSERT INTO sessions_new (group_folder, agent_type, session_id) - SELECT group_folder, ?, session_id FROM sessions`, - ) - .run(defaultAgentType); - database.exec(` - DROP TABLE sessions; - ALTER TABLE sessions_new RENAME TO sessions; - `); -} - -export function backfillLegacyServiceSessions( - database: Database, - resolveAgentTypeFromServiceId: ServiceShadowAgentTypeResolver, -): void { - if (!hasLegacyServiceSessionsTable(database)) { - return; - } - - const rows = database - .prepare( - `SELECT group_folder, service_id, session_id - FROM service_sessions`, - ) - .all() as Array<{ - group_folder: string; - service_id: string; - session_id: string; - }>; - - const upsert = database.prepare( - `INSERT OR IGNORE INTO sessions (group_folder, agent_type, session_id) - VALUES (?, ?, ?)`, - ); - - const tx = database.transaction( - ( - sessionRows: Array<{ - group_folder: string; - service_id: string; - session_id: string; - }>, - ) => { - for (const row of sessionRows) { - const agentType = resolveAgentTypeFromServiceId(row.service_id); - if (!agentType) { - continue; - } - upsert.run(row.group_folder, agentType, row.session_id); - } - }, - ); - - tx(rows); -} - -export function dropLegacyServiceSessionsTable(database: Database): void { - if (!hasLegacyServiceSessionsTable(database)) { - return; - } - - database.exec(`DROP TABLE service_sessions`); -} - export function getSessionFromDatabase( database: Database, groupFolder: string, diff --git a/src/paired-follow-up-scheduler.test.ts b/src/paired-follow-up-scheduler.test.ts index 1b02709..d7fea17 100644 --- a/src/paired-follow-up-scheduler.test.ts +++ b/src/paired-follow-up-scheduler.test.ts @@ -8,7 +8,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; import { CLAUDE_SERVICE_ID, CODEX_MAIN_SERVICE_ID, - CODEX_REVIEW_SERVICE_ID, SERVICE_ID, normalizeServiceId, } from './config.js'; @@ -27,6 +26,7 @@ import { resetPairedFollowUpScheduleState, schedulePairedFollowUpOnce, } from './paired-follow-up-scheduler.js'; +import { applySchemaMigrations } from './db/migrations/index.js'; const CURRENT_SERVICE_ID = normalizeServiceId(SERVICE_ID); const CURRENT_AGENT_TYPE = @@ -41,69 +41,11 @@ const OTHER_AGENT_TYPE = function createLegacyLeaseDatabase(args: { dbPath: string; taskId: string; - role?: 'owner' | 'reviewer' | 'arbiter'; - intentKind?: - | 'owner-turn' - | 'reviewer-turn' - | 'arbiter-turn' - | 'owner-follow-up' - | 'finalize-owner-turn'; - claimedRunId?: string; - taskStatus?: string; reviewerServiceId: string; reviewerAgentType: 'codex' | 'claude-code'; - ownerServiceId?: string; - ownerAgentType?: 'codex' | 'claude-code'; - arbiterServiceId?: string; - arbiterAgentType?: 'codex' | 'claude-code'; }): void { const legacyDb = new Database(args.dbPath); - legacyDb.exec(` - CREATE TABLE paired_tasks ( - id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - group_folder TEXT NOT NULL, - owner_service_id TEXT NOT NULL, - reviewer_service_id TEXT NOT NULL, - arbiter_service_id TEXT, - owner_agent_type TEXT, - reviewer_agent_type TEXT, - arbiter_agent_type TEXT, - title TEXT, - source_ref TEXT, - plan_notes TEXT, - review_requested_at TEXT, - round_trip_count INTEGER NOT NULL DEFAULT 0, - status TEXT NOT NULL DEFAULT 'active', - arbiter_verdict TEXT, - arbiter_requested_at TEXT, - completion_reason TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL - ); - CREATE TABLE paired_task_execution_leases ( - task_id TEXT PRIMARY KEY, - chat_jid TEXT NOT NULL, - role TEXT NOT NULL, - intent_kind TEXT NOT NULL, - claimed_run_id TEXT NOT NULL, - task_status TEXT NOT NULL, - task_updated_at TEXT NOT NULL, - claimed_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - expires_at TEXT NOT NULL, - CHECK (role IN ('owner', 'reviewer', 'arbiter')), - CHECK ( - intent_kind IN ( - 'owner-turn', - 'reviewer-turn', - 'arbiter-turn', - 'owner-follow-up', - 'finalize-owner-turn' - ) - ) - ); - `); + applySchemaMigrations(legacyDb); legacyDb .prepare( `INSERT INTO paired_tasks ( @@ -112,42 +54,26 @@ function createLegacyLeaseDatabase(args: { group_folder, owner_service_id, reviewer_service_id, - arbiter_service_id, owner_agent_type, reviewer_agent_type, - arbiter_agent_type, - title, source_ref, - plan_notes, - review_requested_at, round_trip_count, status, - arbiter_verdict, - arbiter_requested_at, - completion_reason, created_at, updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ) .run( args.taskId, 'group@test', 'test-group', - args.ownerServiceId ?? OTHER_SERVICE_ID, + OTHER_SERVICE_ID, args.reviewerServiceId, - args.arbiterServiceId ?? null, - args.ownerAgentType ?? OTHER_AGENT_TYPE, + OTHER_AGENT_TYPE, args.reviewerAgentType, - args.arbiterAgentType ?? null, - null, 'HEAD', - null, - null, 1, 'review_ready', - null, - null, - null, '2026-03-30T00:00:00.000Z', '2026-03-30T00:00:00.000Z', ); @@ -157,22 +83,26 @@ function createLegacyLeaseDatabase(args: { task_id, chat_jid, role, + turn_id, intent_kind, claimed_run_id, + claimed_service_id, task_status, task_updated_at, claimed_at, updated_at, expires_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ) .run( args.taskId, 'group@test', - args.role ?? 'reviewer', - args.intentKind ?? 'reviewer-turn', - args.claimedRunId ?? `legacy-run-${args.role ?? 'reviewer'}`, - args.taskStatus ?? 'review_ready', + 'reviewer', + `${args.taskId}:2026-03-30T00:00:00.000Z:reviewer-turn`, + 'reviewer-turn', + 'legacy-run-reviewer', + args.reviewerServiceId, + 'review_ready', '2026-03-30T00:00:00.000Z', '2026-03-30T00:00:00.000Z', '2026-03-30T00:00:00.000Z', @@ -833,50 +763,4 @@ describe('paired follow-up scheduler', () => { resetPairedFollowUpScheduleState(); } }); - - it('preserves the raw runtime service id for legacy owner leases instead of canonicalizing to the owner shadow', () => { - const tempDir = fs.mkdtempSync( - path.join(os.tmpdir(), 'ejclaw-paired-lease-owner-failover-'), - ); - const dbPath = path.join(tempDir, 'paired-state.db'); - - try { - createLegacyLeaseDatabase({ - dbPath, - taskId: 'task-owner-failover-legacy-lease', - role: 'owner', - intentKind: 'owner-turn', - taskStatus: 'active', - ownerServiceId: CODEX_REVIEW_SERVICE_ID, - ownerAgentType: 'codex', - reviewerServiceId: CLAUDE_SERVICE_ID, - reviewerAgentType: 'claude-code', - }); - resetPairedFollowUpScheduleState(); - - _initTestDatabaseFromFile(dbPath); - - const migratedDb = new Database(dbPath, { readonly: true }); - const leaseRow = migratedDb - .prepare( - ` - SELECT claimed_service_id - FROM paired_task_execution_leases - WHERE task_id = ? - `, - ) - .get('task-owner-failover-legacy-lease') as - | { claimed_service_id: string | null } - | undefined; - migratedDb.close(); - - expect(leaseRow).toEqual({ - claimed_service_id: CODEX_REVIEW_SERVICE_ID, - }); - } finally { - fs.rmSync(tempDir, { recursive: true, force: true }); - _initTestDatabase(); - resetPairedFollowUpScheduleState(); - } - }); });