const SAFE_NUMERIC_METADATA = new Set(["payloadchars", "payloadestimatedtokens", "taskchars", "taskestimatedtokens", "contextestimatedtokens", "transmittedenvelopefacts", "usedenvelopefacts", "requestchars", "explicitpaths", "explicitsymbols", "explicitcommands", "dirtyfiles", "diffstatfiles", "scopeddirtyfiles", "scopeddiffstatfiles", "affecteddirectories", "currentrunfailurecount", "contextratio", "inputtokens", "cachedinputtokens", "outputtokens", "contexttokens", "totaltokens", "latencyms", "turnindex", "callindex"]); const SAFE_RECOVERY_IDS = new Set(["recoveredfromrunid", "continuedasrunid"]); const SAFE_HASHES = new Set(["confighash", "modelcataloghash"]); const SAFE_VERSIONS = new Set(["configversion", "policyversion", "pricecatalogversion", "piversion", "piagentsversion", "ultrapiversion"]); const SAFE_TIMESTAMPS = new Set(["createdat", "startedat", "stoppedat", "cooldownuntil"]); const SAFE_TOOL_NAMES = new Set(["ultra_dispatch", "read", "write", "edit", "bash", "grep", "find", "ls"]); function keyId(key: string): string { return key.replace(/[^a-z0-9]/gi, "").toLowerCase(); } export function isSafeTelemetryMetadata(key: string, value: unknown): boolean { const id = keyId(key); if (id === "requestexport") return typeof value === "string" && /^intent=(?:answer|investigate|review|change|fix);breadth=(?:tiny|local|cross-module|wide);coupling=(?:low|medium|high);uncertainty=(?:low|medium|high);risk=(?:low|medium|high);verifiability=(?:strong|partial|weak)$/.test(value); if (id === "requesthash") return typeof value === "string" && /^$/.test(value); if (id === "commandhash") return typeof value === "string" && /^$/.test(value); if (id === "steeringhash") return typeof value === "string" && /^$/.test(value); if (id === "fingerprint") return typeof value === "string" && /^$/.test(value); if (SAFE_NUMERIC_METADATA.has(id)) return typeof value === "number" && Number.isFinite(value); if (SAFE_RECOVERY_IDS.has(id)) return typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value); if (SAFE_HASHES.has(id)) return typeof value === "string" && /^[a-f0-9]{64}$/i.test(value); if (SAFE_TIMESTAMPS.has(id)) return typeof value === "string" && !Number.isNaN(Date.parse(value)) && new Date(value).toISOString() === value; if (id === "toolidentity") return typeof value === "string" && (SAFE_TOOL_NAMES.has(value) || /^$/.test(value)); if (id === "toolcallhash") return typeof value === "string" && /^$/.test(value); if (id === "contexthash") return typeof value === "string" && /^$/.test(value); if (id === "sessioncachestate") return value === "cold-proxy" || value === "warm-proxy" || value === "unknown"; return SAFE_VERSIONS.has(id) && typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9._+-]{0,63}$/.test(value); }