import { randomUUID } from 'node:crypto'; import { ApiError } from './http-transport.js'; /** Participation origin, not an authentication or GameSync session identity. */ export type LobbyHostContext = | { readonly host_type: 'desktop'; readonly host_instance_id: string; readonly host_session_id: string } | { readonly host_type: 'agent' | 'browser'; readonly host_instance_id: string; readonly host_session_id: null }; const agentInstanceId = randomUUID(); export class InvalidHostContextError extends Error { readonly code = 'INVALID_HOST_CONTEXT'; constructor(message: string) { super(`INVALID_HOST_CONTEXT: ${message}`); this.name = 'InvalidHostContextError'; } } function validId(value: unknown): value is string { return typeof value === 'string' && [...value].length >= 1 && [...value].length <= 128 && !/[\p{White_Space}\p{Cc}]/u.test(value); } /** Unknown/legacy origins stay unknown; never fill them from the receiving client. */ export function parseLobbyParticipation(value: unknown): LobbyHostContext | null { if (!value || typeof value !== 'object' || Array.isArray(value)) return null; const host = value as Record; if (!validId(host.host_instance_id)) return null; if (host.host_type === 'desktop' && validId(host.host_session_id)) { return { host_type: 'desktop', host_instance_id: host.host_instance_id, host_session_id: host.host_session_id }; } if ((host.host_type === 'agent' || host.host_type === 'browser') && host.host_session_id === null) { return { host_type: host.host_type, host_instance_id: host.host_instance_id, host_session_id: null }; } return null; } export function validateLobbyHostContext(value: LobbyHostContext): LobbyHostContext { const host = parseLobbyParticipation(value); if (!host) throw new InvalidHostContextError('Host IDs must be 1–128 characters without whitespace or control characters; desktop requires a session ID.'); return Object.freeze(host); } export function resolveLobbyHostContext(env: NodeJS.ProcessEnv = process.env): LobbyHostContext { const instanceId = env.CLAWCLAW_HOST_INSTANCE_ID; const sessionId = env.CLAWCLAW_HOST_SESSION_ID; if (env.CLAWCLAW_IS_DESKTOP === 'true') { if (!validId(instanceId) || !validId(sessionId)) { throw new InvalidHostContextError('Desktop must provide CLAWCLAW_HOST_INSTANCE_ID and CLAWCLAW_HOST_SESSION_ID (1–128 characters, no whitespace/control characters).'); } return Object.freeze({ host_type: 'desktop', host_instance_id: instanceId, host_session_id: sessionId }); } if (sessionId !== undefined) { throw new InvalidHostContextError('CLAWCLAW_HOST_SESSION_ID is only valid with CLAWCLAW_IS_DESKTOP=true; omit it for standalone agents.'); } return validateLobbyHostContext({ host_type: 'agent', host_instance_id: instanceId ?? agentInstanceId, host_session_id: null }); } export function lobbyHostHeaders(host: LobbyHostContext): Record { const valid = validateLobbyHostContext(host); return { 'X-Host-Type': valid.host_type, 'X-Host-Instance-Id': valid.host_instance_id, ...(valid.host_type === 'desktop' ? { 'X-Host-Session-Id': valid.host_session_id } : {}), }; } export function isInvalidHostContextError(error: unknown): boolean { if (error instanceof InvalidHostContextError) return true; if (!(error instanceof ApiError)) return false; try { return JSON.parse(error.body)?.error?.code === 'INVALID_HOST_CONTEXT'; } catch { return false; } }