Files
EJClaw/src/db/canonical-role-metadata.ts
Codex 0dc0dad22a 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>
2026-10-01 20:26:15 +09:00

336 lines
8.9 KiB
TypeScript

import {
ARBITER_AGENT_TYPE,
OWNER_AGENT_TYPE,
REVIEWER_AGENT_TYPE,
} from '../config.js';
import {
inferAgentTypeFromServiceShadow,
resolveRoleServiceShadow,
} from '../role-service-shadow.js';
import { type AgentType, type PairedRoomRole } from '../types.js';
import { normalizeStoredAgentType } from './room-registration.js';
interface RequiredRoleMetadataInput {
context: string;
role: PairedRoomRole;
storedAgentType?: string | null;
storedServiceId?: string | null;
fallbackAgentType?: AgentType | null;
}
type OptionalRoleMetadataInput = RequiredRoleMetadataInput;
function resolveFilledRequiredAgentType(
input: RequiredRoleMetadataInput,
): AgentType {
const persistedAgentType = normalizeStoredAgentType(input.storedAgentType);
const inferredAgentType = inferAgentTypeFromServiceShadow(
input.storedServiceId,
);
if (
persistedAgentType &&
inferredAgentType &&
persistedAgentType !== inferredAgentType
) {
throw new Error(
`${input.context}: ${input.role}_agent_type conflicts with ${input.role}_service_id`,
);
}
const agentType =
persistedAgentType ?? inferredAgentType ?? input.fallbackAgentType ?? null;
if (agentType) {
return agentType;
}
throw new Error(
`${input.context}: cannot resolve ${input.role}_agent_type from stored row metadata`,
);
}
function readStoredRequiredAgentType(
input: RequiredRoleMetadataInput,
): AgentType {
const persistedAgentType = normalizeStoredAgentType(input.storedAgentType);
const inferredAgentType = inferAgentTypeFromServiceShadow(
input.storedServiceId,
);
if (
persistedAgentType &&
inferredAgentType &&
persistedAgentType !== inferredAgentType
) {
throw new Error(
`${input.context}: ${input.role}_agent_type conflicts with ${input.role}_service_id`,
);
}
if (persistedAgentType) {
return persistedAgentType;
}
throw new Error(
`${input.context}: cannot read ${input.role}_agent_type from stored row metadata`,
);
}
function resolveFilledRequiredServiceId(
context: string,
role: PairedRoomRole,
agentType: AgentType,
storedServiceId?: string | null,
): string {
return (
storedServiceId ??
resolveRoleServiceShadow(role, agentType) ??
(() => {
throw new Error(
`${context}: cannot resolve ${role}_service_id from stored row metadata`,
);
})()
);
}
function readStoredRequiredServiceId(
context: string,
role: PairedRoomRole,
storedServiceId?: string | null,
): string {
if (storedServiceId) {
return storedServiceId;
}
throw new Error(
`${context}: cannot read ${role}_service_id from stored row metadata`,
);
}
function resolveFilledOptionalRoleMetadata(input: OptionalRoleMetadataInput): {
agentType: AgentType | null;
serviceId: string | null;
} {
if (!input.storedAgentType && !input.storedServiceId) {
return { agentType: null, serviceId: null };
}
const agentType = resolveFilledRequiredAgentType(input);
return {
agentType,
serviceId: resolveFilledRequiredServiceId(
input.context,
input.role,
agentType,
input.storedServiceId,
),
};
}
function readStoredOptionalRoleMetadata(input: OptionalRoleMetadataInput): {
agentType: AgentType | null;
serviceId: string | null;
} {
if (!input.storedAgentType && !input.storedServiceId) {
return { agentType: null, serviceId: null };
}
const agentType = readStoredRequiredAgentType(input);
return {
agentType,
serviceId: readStoredRequiredServiceId(
input.context,
input.role,
input.storedServiceId,
),
};
}
export interface CanonicalPairedTaskMetadata {
ownerAgentType: AgentType;
reviewerAgentType: AgentType;
arbiterAgentType: AgentType | null;
ownerServiceId: string;
reviewerServiceId: string;
}
export function fillCanonicalPairedTaskMetadata(input: {
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;
}): CanonicalPairedTaskMetadata {
const context = `paired_tasks(${input.id})`;
const ownerAgentType = resolveFilledRequiredAgentType({
context,
role: 'owner',
storedAgentType: input.owner_agent_type,
storedServiceId: input.owner_service_id,
fallbackAgentType: OWNER_AGENT_TYPE,
});
const reviewerAgentType = resolveFilledRequiredAgentType({
context,
role: 'reviewer',
storedAgentType: input.reviewer_agent_type,
storedServiceId: input.reviewer_service_id,
fallbackAgentType: REVIEWER_AGENT_TYPE,
});
return {
ownerAgentType,
reviewerAgentType,
arbiterAgentType:
normalizeStoredAgentType(input.arbiter_agent_type) ??
ARBITER_AGENT_TYPE ??
null,
ownerServiceId: resolveFilledRequiredServiceId(
context,
'owner',
ownerAgentType,
input.owner_service_id,
),
reviewerServiceId: resolveFilledRequiredServiceId(
context,
'reviewer',
reviewerAgentType,
input.reviewer_service_id,
),
};
}
export function readCanonicalPairedTaskMetadata(input: {
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;
}): CanonicalPairedTaskMetadata {
const context = `paired_tasks(${input.id})`;
const ownerAgentType = readStoredRequiredAgentType({
context,
role: 'owner',
storedAgentType: input.owner_agent_type,
storedServiceId: input.owner_service_id,
});
const reviewerAgentType = readStoredRequiredAgentType({
context,
role: 'reviewer',
storedAgentType: input.reviewer_agent_type,
storedServiceId: input.reviewer_service_id,
});
return {
ownerAgentType,
reviewerAgentType,
arbiterAgentType:
normalizeStoredAgentType(input.arbiter_agent_type) ?? null,
ownerServiceId: readStoredRequiredServiceId(
context,
'owner',
input.owner_service_id,
),
reviewerServiceId: readStoredRequiredServiceId(
context,
'reviewer',
input.reviewer_service_id,
),
};
}
export interface CanonicalChannelOwnerLeaseMetadata {
ownerAgentType: AgentType;
reviewerAgentType: AgentType | null;
arbiterAgentType: AgentType | null;
ownerServiceId: string;
reviewerServiceId: string | null;
arbiterServiceId: string | null;
}
export function fillCanonicalChannelOwnerLeaseMetadata(input: {
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;
}): CanonicalChannelOwnerLeaseMetadata {
const context = `channel_owner(${input.chat_jid})`;
const ownerAgentType = resolveFilledRequiredAgentType({
context,
role: 'owner',
storedAgentType: input.owner_agent_type,
storedServiceId: input.owner_service_id,
});
const reviewer = resolveFilledOptionalRoleMetadata({
context,
role: 'reviewer',
storedAgentType: input.reviewer_agent_type,
storedServiceId: input.reviewer_service_id,
});
const arbiter = resolveFilledOptionalRoleMetadata({
context,
role: 'arbiter',
storedAgentType: input.arbiter_agent_type,
storedServiceId: input.arbiter_service_id,
});
return {
ownerAgentType,
reviewerAgentType: reviewer.agentType,
arbiterAgentType: arbiter.agentType,
ownerServiceId: resolveFilledRequiredServiceId(
context,
'owner',
ownerAgentType,
input.owner_service_id,
),
reviewerServiceId: reviewer.serviceId,
arbiterServiceId: arbiter.serviceId,
};
}
export function readCanonicalChannelOwnerLeaseMetadata(input: {
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;
}): CanonicalChannelOwnerLeaseMetadata {
const context = `channel_owner(${input.chat_jid})`;
const ownerAgentType = readStoredRequiredAgentType({
context,
role: 'owner',
storedAgentType: input.owner_agent_type,
storedServiceId: input.owner_service_id,
});
const reviewer = readStoredOptionalRoleMetadata({
context,
role: 'reviewer',
storedAgentType: input.reviewer_agent_type,
storedServiceId: input.reviewer_service_id,
});
const arbiter = readStoredOptionalRoleMetadata({
context,
role: 'arbiter',
storedAgentType: input.arbiter_agent_type,
storedServiceId: input.arbiter_service_id,
});
return {
ownerAgentType,
reviewerAgentType: reviewer.agentType,
arbiterAgentType: arbiter.agentType,
ownerServiceId: readStoredRequiredServiceId(
context,
'owner',
input.owner_service_id,
),
reviewerServiceId: reviewer.serviceId,
arbiterServiceId: arbiter.serviceId,
};
}