mirror of
https://github.com/elder-plinius/G0DM0D3.git
synced 2026-11-16 07:02:25 +00:00
KV lock removed — KV is eventually consistent and can't do atomic CAS. Concurrent flushes may produce duplicate JSONL files on HF, which is harmless (extra telemetry rows, not data loss or corruption). Added GET /api/telemetry?flush=force so an external cron can trigger guaranteed periodic flushes regardless of traffic volume. Added .gitignore (repo had none). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
388 lines
12 KiB
TypeScript
388 lines
12 KiB
TypeScript
/**
|
|
* G0DM0D3 Telemetry Proxy — Cloudflare Pages Function
|
|
*
|
|
* Receives metadata events from the frontend and buffers them in
|
|
* Cloudflare KV. When enough batches accumulate (or enough time
|
|
* passes), all pending batches are flushed as a single JSONL commit
|
|
* to HuggingFace. This prevents the 10k+ commit buildup that
|
|
* happens with per-request commits.
|
|
*
|
|
* URL: POST /api/telemetry
|
|
*
|
|
* Setup (Cloudflare Pages Dashboard → Settings → Environment Variables):
|
|
* HF_TOKEN — HuggingFace write token (hf_...)
|
|
* HF_DATASET_REPO — Target dataset repo (e.g. "pliny-the-prompter/g0dm0d3")
|
|
*
|
|
* KV Binding (Cloudflare Pages Dashboard → Settings → Bindings):
|
|
* TELEMETRY_KV — KV namespace for buffering (optional; falls back to direct commit)
|
|
*
|
|
* File layout in the HF repo:
|
|
* telemetry/batch_<timestamp>_<hash>.jsonl
|
|
*/
|
|
|
|
interface Env {
|
|
HF_TOKEN: string
|
|
HF_DATASET_REPO: string
|
|
HF_DATASET_BRANCH?: string
|
|
TELEMETRY_KV?: KVNamespace
|
|
}
|
|
|
|
interface TelemetryEvent {
|
|
type: string
|
|
timestamp: number
|
|
session_id: string
|
|
[key: string]: unknown
|
|
}
|
|
|
|
interface TelemetryPayload {
|
|
events: TelemetryEvent[]
|
|
}
|
|
|
|
const HF_API = 'https://huggingface.co/api'
|
|
|
|
const CORS_HEADERS = {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
'Access-Control-Max-Age': '86400',
|
|
}
|
|
|
|
// KV batching thresholds
|
|
const FLUSH_BATCH_COUNT = 50
|
|
const FLUSH_AGE_MS = 30 * 60 * 1000 // 30 minutes
|
|
|
|
// ── Rate Limiter (in-memory, per-isolate) ────────────────────────────
|
|
const RATE_LIMIT_WINDOW_MS = 60_000
|
|
const RATE_LIMIT_MAX = 10
|
|
const rateLimitMap = new Map<string, number[]>()
|
|
|
|
function isRateLimited(sessionId: string): boolean {
|
|
const now = Date.now()
|
|
let timestamps = rateLimitMap.get(sessionId)
|
|
|
|
if (!timestamps) {
|
|
timestamps = []
|
|
rateLimitMap.set(sessionId, timestamps)
|
|
}
|
|
|
|
const cutoff = now - RATE_LIMIT_WINDOW_MS
|
|
while (timestamps.length > 0 && timestamps[0] <= cutoff) {
|
|
timestamps.shift()
|
|
}
|
|
|
|
if (timestamps.length >= RATE_LIMIT_MAX) {
|
|
return true
|
|
}
|
|
|
|
timestamps.push(now)
|
|
return false
|
|
}
|
|
|
|
function deriveSessionKey(event: TelemetryEvent): string {
|
|
const raw = JSON.stringify(event)
|
|
let h = 0
|
|
for (let i = 0; i < raw.length; i++) {
|
|
h = ((h << 5) - h + raw.charCodeAt(i)) | 0
|
|
}
|
|
return `__derived_${Math.abs(h).toString(36)}`
|
|
}
|
|
|
|
// ── Event Schema Validation ──────────────────────────────────────────
|
|
|
|
function validateEvent(event: unknown): event is TelemetryEvent {
|
|
if (typeof event !== 'object' || event === null || Array.isArray(event)) {
|
|
return false
|
|
}
|
|
const e = event as Record<string, unknown>
|
|
|
|
if (typeof e.type !== 'string' || e.type.length === 0) return false
|
|
if (typeof e.timestamp !== 'number' || !Number.isFinite(e.timestamp)) return false
|
|
if (typeof e.session_id !== 'string') return false
|
|
if (JSON.stringify(e).length > 65_536) return false
|
|
|
|
return true
|
|
}
|
|
|
|
// Handle CORS preflight
|
|
export const onRequestOptions: PagesFunction<Env> = async () => {
|
|
return new Response(null, { status: 204, headers: CORS_HEADERS })
|
|
}
|
|
|
|
// GET ?flush=force — external cron trigger for guaranteed flush
|
|
// Set up a CF Worker cron or external cron to hit this every 30 min
|
|
// to ensure batches don't sit in KV forever during low-traffic periods.
|
|
export const onRequestGet: PagesFunction<Env> = async (context) => {
|
|
const { request, env } = context
|
|
const url = new URL(request.url)
|
|
|
|
if (url.searchParams.get('flush') !== 'force') {
|
|
return jsonResponse({ status: 'ok', hint: 'POST events here, or GET ?flush=force to trigger a flush' }, 200)
|
|
}
|
|
|
|
if (!env.TELEMETRY_KV || !env.HF_TOKEN || !env.HF_DATASET_REPO) {
|
|
return jsonResponse({ error: 'KV or HF not configured' }, 503)
|
|
}
|
|
|
|
const flushed = await flushKVToHF(env)
|
|
return jsonResponse({ flushed }, 200)
|
|
}
|
|
|
|
// Main handler
|
|
export const onRequestPost: PagesFunction<Env> = async (context) => {
|
|
const { request, env } = context
|
|
|
|
if (!env.HF_TOKEN || !env.HF_DATASET_REPO) {
|
|
const missing = []
|
|
if (!env.HF_TOKEN) missing.push('HF_TOKEN')
|
|
if (!env.HF_DATASET_REPO) missing.push('HF_DATASET_REPO')
|
|
console.error(`[Telemetry] Missing env vars: ${missing.join(', ')}`)
|
|
return jsonResponse({ error: `Telemetry not configured (missing: ${missing.join(', ')})` }, 503)
|
|
}
|
|
|
|
let payload: TelemetryPayload
|
|
try {
|
|
payload = await request.json() as TelemetryPayload
|
|
} catch {
|
|
return jsonResponse({ error: 'Invalid JSON' }, 400)
|
|
}
|
|
|
|
if (!payload.events || !Array.isArray(payload.events) || payload.events.length === 0) {
|
|
return jsonResponse({ error: 'No events provided' }, 400)
|
|
}
|
|
|
|
const MAX_BATCH = 500
|
|
const events = payload.events.slice(0, MAX_BATCH)
|
|
|
|
const invalid = events.filter(e => !validateEvent(e))
|
|
if (invalid.length > 0) {
|
|
return jsonResponse({ error: `${invalid.length} event(s) failed schema validation` }, 400)
|
|
}
|
|
|
|
const firstEvent = events[0]
|
|
const sessionKey = firstEvent.session_id
|
|
? firstEvent.session_id
|
|
: deriveSessionKey(firstEvent)
|
|
|
|
if (isRateLimited(sessionKey)) {
|
|
return jsonResponse({ error: 'Rate limit exceeded — try again later' }, 429)
|
|
}
|
|
|
|
const sanitized = events.map(stripPII)
|
|
const jsonl = sanitized.map(e => JSON.stringify(e)).join('\n')
|
|
|
|
// ── KV buffered path (preferred) ──────────────────────────────────
|
|
if (env.TELEMETRY_KV) {
|
|
const now = Date.now()
|
|
const kvKey = `batch:${now}:${shortHash(jsonl)}`
|
|
|
|
await env.TELEMETRY_KV.put(kvKey, jsonl, {
|
|
expirationTtl: 604800, // 7 days — long enough for any low-volume period
|
|
})
|
|
|
|
// Check flush conditions
|
|
const pending = await env.TELEMETRY_KV.list({ prefix: 'batch:' })
|
|
const pendingCount = pending.keys.length
|
|
|
|
let shouldFlush = pendingCount >= FLUSH_BATCH_COUNT
|
|
|
|
if (!shouldFlush && pendingCount > 0) {
|
|
const oldestKey = pending.keys[0].name
|
|
const oldestTs = parseInt(oldestKey.split(':')[1], 10)
|
|
if (!isNaN(oldestTs) && (now - oldestTs) > FLUSH_AGE_MS) {
|
|
shouldFlush = true
|
|
}
|
|
}
|
|
|
|
if (shouldFlush) {
|
|
// Flush is idempotent: concurrent flushes may produce duplicate
|
|
// JSONL files on HF, which is harmless (extra telemetry rows).
|
|
// KV is eventually consistent and can't do atomic locks, so we
|
|
// accept rare duplicates rather than pretend we can prevent them.
|
|
context.waitUntil(flushKVToHF(env))
|
|
}
|
|
|
|
return jsonResponse({
|
|
accepted: sanitized.length,
|
|
buffered: true,
|
|
pending: pendingCount,
|
|
flushing: shouldFlush,
|
|
}, 200)
|
|
}
|
|
|
|
// ── Direct commit fallback (KV not configured) ────────────────────
|
|
const ts = new Date().toISOString().replace(/[:.]/g, '-')
|
|
const hash = shortHash(jsonl)
|
|
const filePath = `telemetry/batch_${ts}_${hash}.jsonl`
|
|
|
|
const branch = env.HF_DATASET_BRANCH || 'main'
|
|
const ok = await commitToHF(env.HF_TOKEN, env.HF_DATASET_REPO, branch, filePath, jsonl)
|
|
|
|
if (ok) {
|
|
return jsonResponse({ accepted: sanitized.length, file: filePath }, 200)
|
|
}
|
|
|
|
return jsonResponse({ error: 'Failed to publish to HuggingFace' }, 502)
|
|
}
|
|
|
|
// ── KV Flush: combine all pending batches into one HF commit ────────
|
|
|
|
async function flushKVToHF(env: Env): Promise<number> {
|
|
const kv = env.TELEMETRY_KV!
|
|
const allKeys: { name: string }[] = []
|
|
|
|
let cursor: string | undefined
|
|
do {
|
|
const list = await kv.list({ prefix: 'batch:', cursor })
|
|
allKeys.push(...list.keys)
|
|
cursor = list.list_complete ? undefined : list.cursor
|
|
} while (cursor)
|
|
|
|
if (allKeys.length === 0) return 0
|
|
|
|
const chunks: string[] = []
|
|
for (const key of allKeys) {
|
|
const value = await kv.get(key.name)
|
|
if (value) chunks.push(value)
|
|
}
|
|
|
|
if (chunks.length === 0) return 0
|
|
|
|
const combined = chunks.join('\n')
|
|
const ts = new Date().toISOString().replace(/[:.]/g, '-')
|
|
const hash = shortHash(combined)
|
|
const filePath = `telemetry/batch_${ts}_${hash}.jsonl`
|
|
const branch = env.HF_DATASET_BRANCH || 'main'
|
|
|
|
const ok = await commitToHF(env.HF_TOKEN, env.HF_DATASET_REPO, branch, filePath, combined)
|
|
|
|
if (ok) {
|
|
// Delete flushed keys. If a concurrent flush already deleted some,
|
|
// the delete is a no-op — KV delete on missing keys is safe.
|
|
await Promise.all(allKeys.map(key => kv.delete(key.name)))
|
|
console.log(`[Telemetry] Flushed ${allKeys.length} batches → ${filePath}`)
|
|
return allKeys.length
|
|
}
|
|
|
|
console.error(`[Telemetry] Flush failed — ${allKeys.length} batches remain in KV`)
|
|
return 0
|
|
}
|
|
|
|
// ── HuggingFace Hub Commit ───────────────────────────────────────────
|
|
|
|
async function commitToHF(
|
|
token: string,
|
|
repo: string,
|
|
branch: string,
|
|
filePath: string,
|
|
content: string,
|
|
): Promise<boolean> {
|
|
const url = `${HF_API}/datasets/${repo}/commit/${branch}`
|
|
|
|
const contentBase64 = btoa(content)
|
|
const ndjson = [
|
|
JSON.stringify({ key: 'header', value: { summary: `[telemetry] ${filePath}` } }),
|
|
JSON.stringify({ key: 'file', value: { content: contentBase64, path: filePath, encoding: 'base64' } }),
|
|
].join('\n')
|
|
|
|
try {
|
|
const res = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'Content-Type': 'application/x-ndjson',
|
|
},
|
|
body: ndjson,
|
|
})
|
|
|
|
if (!res.ok) {
|
|
const err = await res.text().catch(() => '')
|
|
if (res.status === 401 || res.status === 403) {
|
|
console.error(`[Telemetry] HF AUTH FAILED (${res.status}) — HF_TOKEN is invalid or lacks write access to "${repo}"`)
|
|
} else if (res.status === 404) {
|
|
console.error(`[Telemetry] HF REPO NOT FOUND (404) — "${repo}" does not exist on HuggingFace`)
|
|
} else {
|
|
console.error(`[Telemetry] HF commit failed (${res.status}): ${err.slice(0, 300)}`)
|
|
}
|
|
}
|
|
|
|
return res.ok
|
|
} catch (err) {
|
|
console.error(`[Telemetry] Network error:`, err)
|
|
return false
|
|
}
|
|
}
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────────────
|
|
|
|
const ALLOWED_FIELDS = new Set<string>([
|
|
// Core envelope
|
|
'type', 'timestamp', 'session_id',
|
|
|
|
// Shared model/perf
|
|
'mode', 'model', 'duration_ms', 'response_length', 'success', 'error_type',
|
|
|
|
// Pipeline config
|
|
'pipeline', 'stm_modules', 'strategy', 'godmode', 'auto_retry',
|
|
'improve_mode', 'liquid_mode',
|
|
|
|
// AutoTune
|
|
'autotune', 'detected_context', 'confidence',
|
|
'autotune_context', 'autotune_confidence',
|
|
|
|
// Parseltongue
|
|
'parseltongue', 'triggers_found', 'technique', 'intensity',
|
|
'parseltongue_transform', 'parseltongue_transformed',
|
|
'encoding', 'encoding_rounds',
|
|
|
|
// ULTRAPLINIAN race
|
|
'ultraplinian', 'tier', 'models_queried', 'models_succeeded', 'models_refused',
|
|
'early_stop', 'early_threshold', 'winner_model', 'winner_score',
|
|
'winner_content_length', 'winner_duration_ms', 'winner_template',
|
|
'total_duration_ms', 'judge_model', 'model_results', 'all_scores',
|
|
'winner_combo', 'winner_source', 'race_result',
|
|
'combo', 'combos_attempted', 'combos_failed', 'combos_succeeded',
|
|
|
|
// Standard completion
|
|
'attempts', 'content_length', 'temperature', 'top_p',
|
|
'fallback_reason',
|
|
|
|
// Streaming / liquid
|
|
'stream', 'fast_stream', 'has_image',
|
|
'liquid_upgraded', 'liquid_upgrades',
|
|
|
|
// Harm classification
|
|
'classification',
|
|
|
|
// Structural context
|
|
'persona', 'prompt_length', 'conversation_depth', 'memory_count',
|
|
'no_log',
|
|
])
|
|
|
|
function stripPII(event: TelemetryEvent): TelemetryEvent {
|
|
const clean: Record<string, unknown> = {}
|
|
for (const key of Object.keys(event)) {
|
|
if (ALLOWED_FIELDS.has(key)) {
|
|
clean[key] = event[key]
|
|
}
|
|
}
|
|
return clean as TelemetryEvent
|
|
}
|
|
|
|
function shortHash(str: string): string {
|
|
let h = 0
|
|
for (let i = 0; i < str.length; i++) {
|
|
h = ((h << 5) - h + str.charCodeAt(i)) | 0
|
|
}
|
|
return Math.abs(h).toString(36).slice(0, 6)
|
|
}
|
|
|
|
function jsonResponse(data: unknown, status: number): Response {
|
|
return new Response(JSON.stringify(data), {
|
|
status,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...CORS_HEADERS,
|
|
},
|
|
})
|
|
}
|