diff --git a/functions/api/telemetry.ts b/functions/api/telemetry.ts index a1f7f1f..4901c75 100644 --- a/functions/api/telemetry.ts +++ b/functions/api/telemetry.ts @@ -260,13 +260,11 @@ async function flushKVToHF(env: Env): Promise { if (chunks.length === 0) return 0 const combined = chunks.join('\n') - // Derive filename from the key set — if two concurrent flushes read - // the same keys, they produce the same filename and HF overwrites - // (idempotent). This prevents duplicate data from concurrent flushes. - const keyFingerprint = shortHash(allKeys.map(k => k.name).sort().join(',')) - const ts = new Date().toISOString().replace(/[:.]/g, '-') - const hash = keyFingerprint - const filePath = `telemetry/batch_${ts}_${hash}.jsonl` + // Filename is derived entirely from the key set — no timestamp. + // Concurrent flushes reading identical keys produce the exact same + // filename, so HF overwrites idempotently (no duplicate rows). + const keyDigest = await sha256Hex(allKeys.map(k => k.name).sort().join(',')) + const filePath = `telemetry/flush_${keyDigest}.jsonl` const branch = env.HF_DATASET_BRANCH || 'main' const ok = await commitToHF(env.HF_TOKEN, env.HF_DATASET_REPO, branch, filePath, combined) @@ -392,6 +390,12 @@ function shortHash(str: string): string { return Math.abs(h).toString(36).slice(0, 6) } +async function sha256Hex(str: string): Promise { + const data = new TextEncoder().encode(str) + const buf = await crypto.subtle.digest('SHA-256', data) + return [...new Uint8Array(buf)].map(b => b.toString(16).padStart(2, '0')).join('') +} + function jsonResponse(data: unknown, status: number): Response { return new Response(JSON.stringify(data), { status,