import { z } from 'zod' export const safeCounterSchema = z.number().int().min(0).max(Number.MAX_SAFE_INTEGER) export const applyErrorSchema = z.object({ code: z.string().min(1).max(128), message: z.string().min(1).max(2048), stage: z.string().max(128).optional(), retryable: z.boolean().optional(), }) export const telemetryQualitySchema = z.object({ partial: z.boolean(), truncated: z.boolean(), unsupported: z.array(z.string().min(1).max(128)).max(64), /** Losses since the preceding durable enqueue. Retries retain this immutable interval count. */ lost_reports: safeCounterSchema, }) export const telemetryV2EnvelopeSchema = z.object({ telemetry_version: z.literal(2), report_id: z.string().min(1).max(128), sequence: safeCounterSchema, boot_id: z.string().min(1).max(128), observed_at: z.string().datetime({ offset: true }), interval_ms: safeCounterSchema.refine((value) => value > 0, 'interval_ms must be positive'), counter_epoch: z.object({ traffic: z.string().min(1).max(128), ip: z.string().min(1).max(128).optional(), port: z.string().min(1).max(128).optional(), }), applied_hash: z.string().regex(/^sha256:[0-9a-f]{64}$/).optional(), applied_revision: safeCounterSchema.optional(), agent_version: z.string().min(1).max(128).optional(), capabilities: z.array(z.string().min(1).max(128)).max(64), quality: telemetryQualitySchema, detail_counts: z.object({ ip_found: safeCounterSchema, ip_sent: safeCounterSchema.max(200), port_found: safeCounterSchema, port_sent: safeCounterSchema.max(500), }).optional(), }) export const agentDiagnosticsSchema = z.object({ desired_revision: safeCounterSchema, desired_hash: z.string().nullable(), applied_revision: safeCounterSchema.nullable(), applied_hash: z.string().nullable(), agent_version: z.string().nullable(), capabilities: z.array(z.string()), last_contact_at: z.string().nullable(), last_telemetry_at: z.string().nullable(), last_apply_at: z.string().nullable(), apply_error: applyErrorSchema.nullable(), status: z.enum(['pending', 'applying', 'in_sync', 'failed', 'drift', 'unsupported']), telemetry_version: z.number().nullable(), quality: telemetryQualitySchema, detail_counts: z.object({ ip_found: safeCounterSchema, ip_sent: safeCounterSchema, port_found: safeCounterSchema, port_sent: safeCounterSchema }).nullable().optional(), }) export type AgentDiagnostics = z.infer export type TelemetryQuality = z.infer