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

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