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