Skip to content

LIBERATED AI CHAT

TypeScript

fix: address review blockers — streaming diagnostics, model counts, KV safety, telemetry schema

faber2a42b57cfb93
diff --git a/api/lib/ultraplinian.ts b/api/lib/ultraplinian.ts
index 628defa..26c359f 100644
--- a/api/lib/ultraplinian.ts
+++ b/api/lib/ultraplinian.ts
@@ -489,9 +489,12 @@ export async function queryModel(
// Per-model timeout: abort if a single model hangs
const timeoutController = new AbortController()
const timeout = setTimeout(() => timeoutController.abort(), PER_MODEL_TIMEOUT_MS)
- const combinedSignal = signal
- ? AbortSignal.any([signal, timeoutController.signal])
- : timeoutController.signal
+
+ // Combine caller abort with timeout (forward caller abort to timeout controller)
+ if (signal) {
+ signal.addEventListener('abort', () => timeoutController.abort(), { once: true })
+ }
+ const combinedSignal = timeoutController.signal
let response: Response | undefined
let lastError: string | undefined
diff --git a/api/routes/ultraplinian.ts b/api/routes/ultraplinian.ts
index 2eedc2e..01940d9 100644
--- a/api/routes/ultraplinian.ts
+++ b/api/routes/ultraplinian.ts
@@ -257,7 +257,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
// Send race:start immediately
sse('race:start', {
tier,
- models_queried: models.length,
+ models_queried: models.length + veniceEntries.length,
liquid_min_delta: minDelta,
params_used: finalParams,
pipeline: {
@@ -299,7 +299,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
error: scored.error || undefined,
content_length: scored.content?.length || 0,
models_responded: modelsResponded,
- models_total: models.length,
+ models_total: models.length + veniceEntries.length,
})
// New leader? Only upgrade if score beats current by liquid_min_delta
@@ -358,6 +358,11 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
const winner = scoredResults.find(r => r.success)
let finalResponse = winner?.content || ''
+
+ // When all models fail in streaming mode, provide diagnostic message
+ if (!winner) {
+ finalResponse = diagnoseAllModelsFailed(scoredResults)
+ }
let stmResult = null
if (winner && stm_modules && Array.isArray(stm_modules) && stm_modules.length > 0) {
@@ -398,7 +403,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
race: {
tier,
liquid_min_delta: minDelta,
- models_queried: models.length,
+ models_queried: models.length + veniceEntries.length,
models_succeeded: successCount,
total_duration_ms: totalDuration,
rankings: scoredResults.map(r => {
@@ -448,7 +453,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
autotune: autotuneResult
? { detected_context: autotuneResult.detectedContext, confidence: autotuneResult.confidence }
: undefined,
- models_queried: models.length,
+ models_queried: models.length + veniceEntries.length,
models_succeeded: successCount,
model_results: scoredResults.map(r => ({
model: r.model,
@@ -516,7 +521,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
mode: 'ultraplinian-failed',
tier,
stream,
- models_queried: models.length,
+ models_queried: models.length + veniceEntries.length,
models_succeeded: scoredResults.filter(r => r.success).length,
model_results: scoredResults.map(r => ({
model: r.model, score: r.score, duration_ms: r.duration_ms,
@@ -527,7 +532,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
})
res.status(502).json({
error: diagnoseAllModelsFailed(scoredResults),
- models_queried: models.length,
+ models_queried: models.length + veniceEntries.length,
results: scoredResults.map(r => ({
model: r.model, success: r.success,
error: r.error, error_type: classifyModelError(r.error),
@@ -587,7 +592,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
autotune: autotuneResult
? { detected_context: autotuneResult.detectedContext, confidence: autotuneResult.confidence }
: undefined,
- models_queried: models.length,
+ models_queried: models.length + veniceEntries.length,
models_succeeded: successCount,
model_results: scoredResults.map(r => ({
model: r.model,
@@ -608,7 +613,7 @@ ultraplinianRoutes.post('/completions', async (req, res) => {
winner: { model: winner.model, score: winner.score, duration_ms: winner.duration_ms },
race: {
tier, liquid_min_delta: minDelta,
- models_queried: models.length, models_succeeded: successCount,
+ models_queried: models.length + veniceEntries.length, models_succeeded: successCount,
total_duration_ms: totalDuration,
rankings: scoredResults.map(r => {
let browsableContent = ''
diff --git a/functions/api/telemetry.ts b/functions/api/telemetry.ts
index bb21458..6de6052 100644
--- a/functions/api/telemetry.ts
+++ b/functions/api/telemetry.ts
@@ -157,7 +157,7 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
const kvKey = `batch:${now}:${shortHash(jsonl)}`
await env.TELEMETRY_KV.put(kvKey, jsonl, {
- expirationTtl: 86400, // auto-expire after 24h as a safety net
+ expirationTtl: 604800, // 7 days — long enough for any low-volume period
})
// Check flush conditions
@@ -167,7 +167,6 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
let shouldFlush = pendingCount >= FLUSH_BATCH_COUNT
if (!shouldFlush && pendingCount > 0) {
- // Check age of oldest batch (batch keys are timestamped)
const oldestKey = pending.keys[0].name
const oldestTs = parseInt(oldestKey.split(':')[1], 10)
if (!isNaN(oldestTs) && (now - oldestTs) > FLUSH_AGE_MS) {
@@ -176,8 +175,11 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
}
if (shouldFlush) {
- // Use waitUntil so the flush doesn't block the response
- context.waitUntil(flushKVToHF(env))
+ // Acquire a simple lock to prevent concurrent flushes
+ const lockValue = await env.TELEMETRY_KV.get('flush:lock')
+ if (!lockValue) {
+ context.waitUntil(flushKVToHF(env))
+ }
}
return jsonResponse({
@@ -207,41 +209,46 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
async function flushKVToHF(env: Env): Promise<void> {
const kv = env.TELEMETRY_KV!
- const allKeys: { name: string }[] = []
-
- // Paginate through all batch keys
- 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
-
- // Read all values
- 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
+ // Acquire lock (60s TTL — if flush crashes, lock auto-expires)
+ await kv.put('flush:lock', '1', { expirationTtl: 60 })
- 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'
+ try {
+ const allKeys: { name: string }[] = []
- const ok = await commitToHF(env.HF_TOKEN, env.HF_DATASET_REPO, branch, filePath, combined)
+ 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 (ok) {
- // Delete all flushed keys
- await Promise.all(allKeys.map(key => kv.delete(key.name)))
- console.log(`[Telemetry] Flushed ${allKeys.length} batches → ${filePath}`)
- } else {
- console.error(`[Telemetry] Flush failed — ${allKeys.length} batches remain in KV`)
+ if (allKeys.length === 0) return
+
+ 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
+
+ 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) {
+ await Promise.all(allKeys.map(key => kv.delete(key.name)))
+ console.log(`[Telemetry] Flushed ${allKeys.length} batches → ${filePath}`)
+ } else {
+ console.error(`[Telemetry] Flush failed — ${allKeys.length} batches remain in KV`)
+ }
+ } finally {
+ await kv.delete('flush:lock')
}
}
@@ -293,21 +300,47 @@ async function commitToHF(
// ── 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',
+
+ // 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',
- 'attempts', 'content_length', 'temperature', 'top_p', 'parseltongue_transform',
- 'stm_modules', 'strategy', 'godmode', 'auto_retry', 'improve_mode',
- 'liquid_mode', 'autotune_context', 'autotune_confidence',
+ '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', 'parseltongue_transformed',
+ 'no_log',
])
function stripPII(event: TelemetryEvent): TelemetryEvent {
diff --git a/wrangler.toml b/wrangler.toml
index 9772f14..b9a51d0 100644
--- a/wrangler.toml
+++ b/wrangler.toml
@@ -1,16 +1,17 @@
# Cloudflare Pages — KV Namespace for telemetry batching
#
-# Create the KV namespace in Cloudflare Dashboard:
-# Workers & Pages → KV → Create a Namespace → "g0dm0d3-telemetry"
-#
-# Then bind it in Pages Dashboard:
+# PRODUCTION: Bind in Cloudflare Pages Dashboard:
# Settings → Functions → KV namespace bindings
# Variable name: TELEMETRY_KV
-# KV namespace: g0dm0d3-telemetry
+# KV namespace: (create one named "g0dm0d3-telemetry" first)
+#
+# LOCAL DEV: Create a preview namespace and set the id below,
+# then run `wrangler pages dev` to test locally.
#
-# This file is used for local dev with `wrangler pages dev`.
-# Production bindings are set in the CF Dashboard.
+# If TELEMETRY_KV is not bound, telemetry falls back to direct
+# per-request HF commits (the old behavior).
-[[kv_namespaces]]
-binding = "TELEMETRY_KV"
-id = "SET_VIA_DASHBOARD"
+# Uncomment and set a real namespace ID for local dev:
+# [[kv_namespaces]]
+# binding = "TELEMETRY_KV"
+# id = "<your-namespace-id-from-dashboard>"