export const PI_RUNTIME_MILESTONES = [ 'worker.queue_wait', 'worker.spawn', 'rpc.ready', 'session.open', 'resources.ready', 'prompt.accepted', 'agent.settled', ] as const; export type PiRuntimeMilestone = (typeof PI_RUNTIME_MILESTONES)[number]; export interface PiRuntimeTelemetryEvent { milestone: PiRuntimeMilestone; conversationRef: string; workerGeneration: number; spanId: string; runRef?: string; cold: boolean; durationMs: number; at: number; } export interface PiRuntimeMilestoneInput { milestone: PiRuntimeMilestone; conversationId: string; workerGeneration: number; runId?: string; cold: boolean; durationMs: number; at: number; } function shortRef(value: string): string { const normalized = value.replace(/[^A-Za-z0-9]/g, ''); return normalized.slice(-8).toLowerCase() || 'unknown'; } export function createPiRuntimeTelemetryEvent( input: PiRuntimeMilestoneInput, ): PiRuntimeTelemetryEvent { const conversationRef = shortRef(input.conversationId); const runRef = input.runId ? shortRef(input.runId) : undefined; return { milestone: input.milestone, conversationRef, workerGeneration: input.workerGeneration, spanId: runRef ? `${conversationRef}:${runRef}` : `${conversationRef}:worker-${input.workerGeneration}`, ...(runRef ? { runRef } : {}), cold: input.cold, durationMs: Math.max(0, Math.round(input.durationMs)), at: input.at, }; }