import type { DomainRuntimeHandoffCandidate, DomainRuntimeHandoffSource, } from './domain-access-orchestrator.js'; import type { DomainNodeType } from './domain-access.js'; const IDENTIFIER_PATTERN = /^[A-Z0-9][A-Z0-9._:-]{1,159}$/; export interface DomainRuntimeHandoffRequest { domainId: string; nodeId: string; nodeType: DomainNodeType; } export interface DomainRuntimeHandoffTransport { request(input: Readonly): Promise; } function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value); } function hasExactKeys(value: Record, expected: readonly string[]): boolean { const actual = Object.keys(value).sort(); const keys = [...expected].sort(); return actual.length === keys.length && actual.every((key, index) => key === keys[index]); } function parseResponse(input: unknown): Readonly | null { if (!isRecord(input) || !hasExactKeys(input, ['handoff', 'schema', 'signer_lookup']) || input.schema !== 'gh-aios.domain-runtime-handoff-response/v1' || !isRecord(input.handoff) || !isRecord(input.signer_lookup) || !hasExactKeys(input.signer_lookup, ['repository_id', 'signer_id']) || typeof input.signer_lookup.repository_id !== 'string' || !IDENTIFIER_PATTERN.test(input.signer_lookup.repository_id) || typeof input.signer_lookup.signer_id !== 'string' || !IDENTIFIER_PATTERN.test(input.signer_lookup.signer_id)) { return null; } return Object.freeze({ handoff: input.handoff, signerLookup: Object.freeze({ repositoryId: input.signer_lookup.repository_id, signerId: input.signer_lookup.signer_id, }), }); } export class StrictDomainRuntimeHandoffSource implements DomainRuntimeHandoffSource { constructor(private readonly transport: DomainRuntimeHandoffTransport) {} async read(domainId: string, nodeId: string, nodeType: DomainNodeType): Promise | null> { if (!IDENTIFIER_PATTERN.test(domainId) || !IDENTIFIER_PATTERN.test(nodeId)) return null; if (nodeType !== 'local-terminal' && nodeType !== 'cloud-resident') return null; try { return parseResponse(await this.transport.request(Object.freeze({ domainId, nodeId, nodeType }))); } catch { return null; } } }