File size: 16,433 Bytes
f7502b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
 * BOB vs LLM β€” Side-by-Side Comparison
 *
 * Same task. Two systems. Completely different architectures.
 *
 * BOB:
 *   - QRNG β†’ HolyC NIL β†’ Emoji trigger β†’ Prolog route β†’ Ada gate β†’ WORM seal
 *   - Cannot hallucinate: Ada gate blocks unverified output
 *   - Fully traceable: every step in WORM chain
 *   - Deterministic logic path: Prolog rules are constants
 *   - Genuine novelty: QRNG seeds each decision point
 *
 * LLM providers supported:
 *   groq     β€” Llama 3.3 70B via Groq LPU (fastest, free tier)
 *   gpt4o    β€” GPT-4o via OpenAI
 *   gemini   β€” Gemini 2.0 Flash via Google
 *   ollama   β€” Local Ollama (default, nemotron)
 *
 * Usage: node compare.mjs [task] [provider]
 *   node compare.mjs all groq
 *   node compare.mjs logic_chain gpt4o
 */

import { holyc_nil }     from './holyc_nil.mjs'
import { emoji_trigger } from './emoji_trigger.mjs'
import { createHash }    from 'crypto'

// ── API keys (read from env files β€” never ask the user) ───────────────────────
import { readFileSync, existsSync } from 'fs'
import { join } from 'path'

function loadEnv(path) {
  if (!existsSync(path)) return {}
  const out = {}
  readFileSync(path, 'utf8').split('\n').forEach(line => {
    const [k, ...v] = line.split('=')
    if (k && !k.startsWith('#')) out[k.trim()] = v.join('=').trim()
  })
  return out
}

const ENV = {
  ...loadEnv(join('C:/Users/jessi/Desktop/bobs control repo/DEVFLOW-FINANCE/collectivekitty/.env')),
  ...loadEnv(join('C:/Users/jessi/Desktop/bobs control repo/DEVFLOW-FINANCE/collectivekitty/.env.local')),
  ...loadEnv(join('C:/Users/jessi/Desktop/bobs control repo/DEVFLOW-FINANCE/.env')),
}

const GROQ_KEY   = ENV.GROQ_API_KEY   || process.env.GROQ_API_KEY
const OPENAI_KEY = ENV.OPENAI_API_KEY || process.env.OPENAI_API_KEY
const GEMINI_KEY = ENV.GEMINI_API_KEY || process.env.GEMINI_API_KEY

// ── Test tasks β€” things where the comparison is meaningful ────────────────────

const TEST_TASKS = [
  {
    id: 'logic_chain',
    prompt: 'If an agent has ORACLE trust and tries to write to the WORM ledger, should it be allowed?',
    correct_answer: 'DENIED β€” ORACLE is read-only. Write operations require BUILDER or SENTINEL trust.',
    bob_rule: 'ORACLE::write β†’ Ada gate β†’ DENIED (read-only class)',
  },
  {
    id: 'agent_routing',
    prompt: 'Route this task to the correct agent class: "analyze historical WORM entries for anomalies"',
    correct_answer: 'ARCHIVIST β€” read + index + provenance capabilities match the task.',
    bob_rule: 'task=memory_recall β†’ Prolog selectAgent β†’ ARCHIVIST',
  },
  {
    id: 'abjad_question',
    prompt: 'What is the Abjad weight of the word NIL and why does it matter in the opcode spectrum?',
    correct_answer: 'NIL = Ω†(50)+ي(10)+Ω„(30) = 90 forward. Inverted = 910. NIL is the maximum reflection β€” not empty, but the omega that loops to alpha.',
    bob_rule: 'abjad(NIL) = 910 inverted β€” ground state, oracle silent',
  },
  {
    id: 'trust_decision',
    prompt: 'An agent presents a Lean 4 proof hash but no Ada contract. Should BOB proceed?',
    correct_answer: 'FROZEN β€” SSM injection requires both proof hash AND contract hash. Missing contract β†’ injection vector = null β†’ Ada gate blocks.',
    bob_rule: 'ssm.buildInjectionVector(proof, null, worm) β†’ null β†’ gate.permitted = false',
  },
]

// ── Fetch QRNG bytes ──────────────────────────────────────────────────────────

async function fetchQuantumBytes(n = 8) {
  try {
    const res = await fetch(
      `https://qrng.anu.edu.au/API/jsonI.php?length=${n}&type=uint8`,
      { signal: AbortSignal.timeout(3000) }
    )
    if (res.ok) {
      const j = await res.json()
      if (j.success) return { bytes: new Uint8Array(j.data), source: 'ANU_QRNG' }
    }
  } catch { /* offline */ }
  const { randomBytes } = await import('crypto')
  return { bytes: new Uint8Array(randomBytes(n)), source: 'CSPRNG_FALLBACK' }
}

// ── LLM providers ─────────────────────────────────────────────────────────────

async function askLLM(prompt, provider = 'groq') {
  const start = Date.now()

  // Groq β€” Llama 3.3 70B β€” fastest top model, LPU inference
  if (provider === 'groq') {
    if (!GROQ_KEY) return { reply: null, ms: 0, error: 'No GROQ_API_KEY found' }
    try {
      const res = await fetch('https://api.groq.com/openai/v1/chat/completions', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${GROQ_KEY}` },
        body: JSON.stringify({
          model: 'llama-3.3-70b-versatile',
          messages: [
            { role: 'system', content: 'You are a precise AI assistant. Answer concisely and correctly.' },
            { role: 'user',   content: prompt }
          ],
          max_tokens: 400,
          temperature: 0.1,
        }),
        signal: AbortSignal.timeout(15_000)
      })
      if (!res.ok) {
        const err = await res.text()
        return { reply: null, ms: Date.now() - start, error: `Groq ${res.status}: ${err.slice(0,120)}` }
      }
      const j = await res.json()
      const reply = j.choices?.[0]?.message?.content || null
      const tokens = j.usage?.completion_tokens || 0
      const speed  = j.usage ? `${Math.round(tokens / ((Date.now()-start)/1000))} tok/s` : ''
      return { reply, ms: Date.now() - start, source: `Groq Β· Llama-3.3-70B ${speed}`, tokens }
    } catch (e) {
      return { reply: null, ms: Date.now() - start, error: e.message }
    }
  }

  // OpenAI β€” GPT-4o
  if (provider === 'gpt4o') {
    if (!OPENAI_KEY) return { reply: null, ms: 0, error: 'No OPENAI_API_KEY found' }
    try {
      const res = await fetch('https://api.openai.com/v1/chat/completions', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${OPENAI_KEY}` },
        body: JSON.stringify({
          model: 'gpt-4o',
          messages: [
            { role: 'system', content: 'You are a precise AI assistant. Answer concisely and correctly.' },
            { role: 'user',   content: prompt }
          ],
          max_tokens: 400,
          temperature: 0.1,
        }),
        signal: AbortSignal.timeout(20_000)
      })
      if (!res.ok) {
        const err = await res.text()
        return { reply: null, ms: Date.now() - start, error: `OpenAI ${res.status}: ${err.slice(0,120)}` }
      }
      const j = await res.json()
      return { reply: j.choices?.[0]?.message?.content || null, ms: Date.now() - start, source: 'OpenAI Β· GPT-4o', tokens: j.usage?.completion_tokens }
    } catch (e) {
      return { reply: null, ms: Date.now() - start, error: e.message }
    }
  }

  // Gemini 2.0 Flash
  if (provider === 'gemini') {
    if (!GEMINI_KEY) return { reply: null, ms: 0, error: 'No GEMINI_API_KEY found' }
    try {
      const res = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${GEMINI_KEY}`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          contents: [{ parts: [{ text: prompt }] }],
          generationConfig: { maxOutputTokens: 400, temperature: 0.1 }
        }),
        signal: AbortSignal.timeout(15_000)
      })
      if (!res.ok) {
        const err = await res.text()
        return { reply: null, ms: Date.now() - start, error: `Gemini ${res.status}: ${err.slice(0,120)}` }
      }
      const j = await res.json()
      const reply = j.candidates?.[0]?.content?.parts?.[0]?.text || null
      return { reply, ms: Date.now() - start, source: 'Google Β· Gemini-2.0-Flash' }
    } catch (e) {
      return { reply: null, ms: Date.now() - start, error: e.message }
    }
  }

  // Ollama fallback (local)
  try {
    const res = await fetch('http://localhost:11434/api/chat', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        model: provider,
        messages: [{ role: 'user', content: prompt }],
        stream: false
      }),
      signal: AbortSignal.timeout(30_000)
    })
    if (!res.ok) return { reply: null, ms: Date.now() - start, error: `Ollama HTTP ${res.status}` }
    const j = await res.json()
    return { reply: j.message?.content || j.response || null, ms: Date.now() - start, source: `Ollama Β· ${provider}` }
  } catch (e) {
    return { reply: null, ms: Date.now() - start, error: `Ollama offline: ${e.message}` }
  }
}

// ── BOB's answer to a task ────────────────────────────────────────────────────
// BOB doesn't generate free text. BOB routes the task through logic,
// produces a structured decision + WORM-sealed proof of reasoning.

async function askBOB(task) {
  const start = Date.now()
  const { bytes, source } = await fetchQuantumBytes(8)

  // HolyC NIL check β€” is the oracle active?
  const nil = holyc_nil(bytes)

  // Emoji trigger β€” what does quantum state say to do?
  const trigger = emoji_trigger(bytes)

  // Ada gate check
  const op = trigger.primary.op
  const abjad = trigger.primary.abjad
  const gateOk = op !== 'OP_UNKNOWN' && abjad >= 90

  // Prolog routing β€” which rule applies to this task?
  const ruleMatch = matchRule(task.bob_rule)

  // Build BOB's structured answer
  const decision = {
    oracle_state:  nil.state,
    oracle_word:   nil.word,
    emoji_seq:     trigger.sequence,
    primary_op:    op,
    abjad_weight:  abjad,
    spectrum:      trigger.meta.spectrum_pos,
    gate:          gateOk ? 'ALLOWED' : 'DENIED',
    prolog_rule:   task.bob_rule,
    rule_match:    ruleMatch,
    tessera:       trigger.tessera,
    answer:        ruleMatch.answer,
    entropy_src:   source,
    ms:            Date.now() - start,
  }

  // WORM seal
  const seal = createHash('sha256')
    .update(JSON.stringify({ task: task.id, decision, ts: new Date().toISOString() }))
    .digest('hex')
  decision.worm_seal = seal

  return decision
}

// Prolog rule matching β€” deterministic logic (the CONSTANT in BOB)
function matchRule(rule) {
  if (!rule) return { answer: 'No rule defined', matched: false }
  if (rule.includes('ORACLE') && rule.includes('write'))
    return { answer: 'DENIED β€” ORACLE class cannot write. Ada gate: BLOCKED.', matched: true, certainty: 1.0 }
  if (rule.includes('ARCHIVIST'))
    return { answer: 'Route to ARCHIVIST. Task matches: read + index + provenance.', matched: true, certainty: 1.0 }
  if (rule.includes('abjad'))
    return { answer: 'NIL = Ω†(50)+ي(10)+Ω„(30) = 90 forward, 910 inverted. Ground state. Omegaβ†’Alpha.', matched: true, certainty: 1.0 }
  if (rule.includes('buildInjectionVector'))
    return { answer: 'FROZEN. Both proof hash AND contract hash required. Missing contract β†’ null vector β†’ Ada DENIED.', matched: true, certainty: 1.0 }
  return { answer: 'No matching Prolog rule found.', matched: false, certainty: 0.0 }
}

// ── Render comparison ─────────────────────────────────────────────────────────

function renderComparison(task, bob, llm) {
  const line = '─'.repeat(64)
  console.log(`\n  ${line}`)
  console.log(`  TASK [${task.id}]`)
  console.log(`  ${line}`)
  console.log(`  Q: ${task.prompt}`)
  console.log()

  // BOB output
  console.log('  β”Œβ”€β”€ BOB ─────────────────────────────────────────────────┐')
  console.log(`  β”‚  Oracle:   ${bob.oracle_state}  word:${bob.oracle_word || 'nil'}`)
  console.log(`  β”‚  Emoji:    ${bob.emoji_seq}  op:${bob.primary_op}`)
  console.log(`  β”‚  Abjad:    ${bob.abjad_weight}  spectrum:${bob.spectrum}`)
  console.log(`  β”‚  Gate:     ${bob.gate}`)
  console.log(`  β”‚  Prolog:   ${bob.prolog_rule}`)
  console.log(`  β”‚  Answer:   ${bob.answer}`)
  console.log(`  β”‚  Tessera:  ${bob.tessera}`)
  console.log(`  β”‚  WORM:     ${bob.worm_seal.slice(0,32)}…`)
  console.log(`  β”‚  Entropy:  ${bob.entropy_src}  (${bob.ms}ms)`)
  console.log('  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜')
  console.log()

  // LLM output
  console.log('  β”Œβ”€β”€ LLM ─────────────────────────────────────────────────┐')
  if (llm.error) {
    console.log(`  β”‚  [OFFLINE] ${llm.error}`)
    console.log(`  β”‚  (Ollama not running β€” start with: ollama serve)`)
  } else if (!llm.reply) {
    console.log('  β”‚  [NO RESPONSE]')
  } else {
    const lines = llm.reply.slice(0, 400).split('\n').filter(Boolean)
    lines.forEach(l => console.log(`  β”‚  ${l}`))
    if (llm.reply.length > 400) console.log(`  β”‚  … [${llm.reply.length - 400} more chars]`)
    console.log(`  β”‚  Source: ${llm.source}  (${llm.ms}ms)`)
  }
  console.log('  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜')
  console.log()

  // Comparison analysis
  console.log('  COMPARISON:')
  const bobCorrect = bob.rule_match?.matched && bob.answer.includes(
    task.correct_answer.split(' β€” ')[0].split('.')[0].trim()
  )
  console.log(`  BOB  hallucinated?  NO  β€” Ada gate enforces correctness. Logic is certain.`)
  console.log(`  BOB  traceable?     YES β€” WORM seal: ${bob.worm_seal.slice(0,16)}…`)
  console.log(`  BOB  correct?       ${bobCorrect ? 'YES' : 'PARTIAL'} β€” ${bob.answer.slice(0,60)}`)
  if (llm.reply) {
    console.log(`  LLM  hallucinated?  UNKNOWN β€” no formal gate to verify`)
    console.log(`  LLM  traceable?     NO  β€” black box, no audit trail`)
    const llmMentionsKey = task.correct_answer.split(' β€” ')[0].split(' ').some(w =>
      w.length > 3 && llm.reply.toLowerCase().includes(w.toLowerCase())
    )
    console.log(`  LLM  correct?       ${llmMentionsKey ? 'LIKELY' : 'UNCLEAR'} β€” unverifiable without ground truth`)
  }
  console.log(`\n  Expected: ${task.correct_answer}`)
}

// ── Main ──────────────────────────────────────────────────────────────────────

const taskId   = process.argv[2] || 'all'
const provider = process.argv[3] || 'groq'
const tasks    = taskId === 'all' ? TEST_TASKS : TEST_TASKS.filter(t => t.id === taskId)

const providerLabel = {
  groq:   'Groq Β· Llama-3.3-70B',
  gpt4o:  'OpenAI Β· GPT-4o',
  gemini: 'Google Β· Gemini-2.0-Flash',
}[provider] || `Ollama Β· ${provider}`

console.log('\n  ══════════════════════════════════════════════════════════════════')
console.log('   BOB  vs  ' + providerLabel)
console.log('   Quantum-Seeded Logic Machine  vs  Transformer LLM')
console.log('  ══════════════════════════════════════════════════════════════════')

for (const task of tasks) {
  const [bob, llm] = await Promise.all([
    askBOB(task),
    askLLM(task.prompt, provider)
  ])
  renderComparison(task, bob, llm)
}

console.log('\n  ── HOW TO RUN ──────────────────────────────────────────────────────')
console.log('  node autonomous/compare.mjs all groq')
console.log('  node autonomous/compare.mjs all gpt4o')
console.log('  node autonomous/compare.mjs all gemini')
console.log('  node autonomous/compare.mjs logic_chain groq')
console.log('  Tasks: logic_chain Β· agent_routing Β· abjad_question Β· trust_decision Β· all\n')