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 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-01 20:26:15 +09:00
parent 8c68577a24
commit 0dc0dad22a
41 changed files with 1321 additions and 8941 deletions

View File

@@ -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`을 갱신
## 주요 파일

View File

@@ -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,

View File

@@ -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,

View File

@@ -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' }),
]);
});
});

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -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<number, string> = {
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();
}
});
});

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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<SchemaMigrationDefinition['apply']>[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<SchemaMigrationDefinition['apply']>[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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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
`);
},
};

View File

@@ -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
`);
},
};

View File

@@ -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}
`);
},
};

View File

@@ -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
`);
}
},
};

View File

@@ -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`,
);
}
},
};

View File

@@ -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);
`);
},
};

View File

@@ -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;

View File

@@ -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
`);
}
},
};

View File

@@ -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`,
);
}
},
};

View File

@@ -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
`);
},
};

View File

@@ -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
`);
},
};

View File

@@ -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`,
);
}
},
};

View File

@@ -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 };

View File

@@ -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<number> {
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<number> {
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);
})();
}
}

View File

@@ -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;
}

View File

@@ -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,

File diff suppressed because it is too large Load Diff

View File

@@ -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;

264
src/db/schema.test.ts Normal file
View File

@@ -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<string, unknown[]> {
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/);
});
});

View File

@@ -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`,
);
}

View File

@@ -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,

View File

@@ -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();
}
});
});