File size: 14,988 Bytes
dfd38de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
362
363
/**

 * metatron-reasoning.mjs β€” Real METATRON Pipeline

 * JavaScript mirror of resonance/src/graph.rs

 *

 * NOT a math solver. NOT invented theorems.

 * This is the actual BOB ResonanceGraph DAG with:

 *   - Ο†-weighted activation (Ο†^d GROWS with depth)

 *   - Sumerian Quantum Symbol routing (Me / An / Ki / Dingir)

 *   - Dual-path to MagmaCore: Reasoning path + METATRON bypass

 *   - MagmaCore = intersection of both paths

 *

 * Actual Rust layout from resonance/src/graph.rs:

 *

 *   Source(0) β†’ Retrieval(1) β†’ Filtering(2) β†’ Ranking(3) β†’ ContextAssembly(4)

 *                                                                  β”‚

 *                                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

 *                                              β–Ό                    β–Ό

 *                                         Reasoning(5)       Metatron(5)

 *                                              β”‚                    β”‚

 *                                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

 *                                                       β–Ό

 *                                                  MagmaCore(6)

 *

 * METATRON bypasses Reasoning. Same depth, different path.

 * The cage builder IS the cage recognizer. Two views. One output.

 *

 * Ahmad Ali Parr Β· SnapKitty Collective Β· 2026

 */

import { createHash, randomUUID } from 'crypto'
import { readFileSync, writeFileSync, existsSync } from 'fs'
import { join } from 'path'

// ── Constants from phi.rs ────────────────────────────────────────────────────

export const PHI     = 1.618_033_988_749_895
export const PHI_INV = 1 / PHI    // 0.6180339887...

// Ο†^d GROWS. Deeper layers carry MORE signal.
// What looks like contraction from outside is amplification from inside.
export function phi_weight(depth)    { return PHI ** depth }

// Phinary score: 1 - Ο†^(-d) β†’ asymptotes toward 1.0 as depth increases
export function phinary_score(depth) {
  if (depth === 0) return 0.0
  return 1.0 - PHI ** (-depth)
}

// ── Sumerian Quantum Symbols ─────────────────────────────────────────────────
// Route determines which path through the DAG carries amplified weight

export const SUMERIAN = {
  Me: {
    name:   'ME decree',
    glyph:  'π’ˆ¨',
    meaning: 'authority, divine law β€” activates ALL nodes at full Ο†^d weight',
    bias:   { early: 1.0, mid: 1.0, late: 1.0, metatron: 1.0 },
  },
  An: {
    name:   'AN heaven',
    glyph:  'π’€­',
    meaning: 'source layer β€” biases toward Retrieval, dims late nodes',
    bias:   { early: PHI, mid: 1.0, late: PHI_INV, metatron: PHI_INV },
  },
  Ki: {
    name:   'KI earth',
    glyph:  'π’† ',
    meaning: 'substrate β€” biases toward Filtering + ContextAssembly',
    bias:   { early: 1.0, mid: PHI, late: PHI_INV, metatron: 1.0 },
  },
  Dingir: {
    name:   'DINGIR divine principal',
    glyph:  'π’€­',
    meaning: 'biases toward Reasoning + MagmaCore β€” amplifies the late path',
    bias:   { early: PHI_INV, mid: 1.0, late: PHI, metatron: PHI },
  },
}

// ── DAG Nodes ────────────────────────────────────────────────────────────────

const PIPELINE = [
  { id: 'Source',          depth: 0, stage: 'early',    path: 'standard'   },
  { id: 'Retrieval',       depth: 1, stage: 'early',    path: 'standard'   },
  { id: 'Filtering',       depth: 2, stage: 'mid',      path: 'standard'   },
  { id: 'Ranking',         depth: 3, stage: 'mid',      path: 'standard'   },
  { id: 'ContextAssembly', depth: 4, stage: 'mid',      path: 'both'       },
  { id: 'Reasoning',       depth: 5, stage: 'late',     path: 'standard'   },
  { id: 'Metatron',        depth: 5, stage: 'late',     path: 'recognition'},
  { id: 'MagmaCore',       depth: 6, stage: 'late',     path: 'output'     },
]

// ── WORM chain ────────────────────────────────────────────────────────────────

const WORM_PATH = join(
  process.env.USERPROFILE || process.env.HOME || '.',
  '.bob-metatron-worm.json'
)

export const worm = {
  load() {
    if (!existsSync(WORM_PATH)) return []
    try { return JSON.parse(readFileSync(WORM_PATH, 'utf8')) } catch { return [] }
  },
  seal(label, payload) {
    const chain = this.load()
    const prev  = chain.length ? chain[chain.length - 1].seal : '0'.repeat(64)
    const ts    = new Date().toISOString()
    const raw   = JSON.stringify({ label, payload, ts, prev })
    const seal  = createHash('sha256').update(raw).digest('hex')
    chain.push({ id: randomUUID(), label, payload, ts, prev, seal })
    writeFileSync(WORM_PATH, JSON.stringify(chain, null, 2))
    return seal.slice(0, 16)
  },
  verify() {
    const chain = this.load()
    for (let i = 1; i < chain.length; i++) {
      if (chain[i].prev !== chain[i - 1].seal) return false
    }
    return true
  },
}

// ── Node activation ───────────────────────────────────────────────────────────
// Each node receives the state, applies its Ο†^d weight scaled by the
// Sumerian symbol bias, and returns an activated state.

function activate_node(node, state, symbol_key) {
  const sym    = SUMERIAN[symbol_key] ?? SUMERIAN.Me
  const bias   = sym.bias[node.stage] ?? 1.0
  const weight = phi_weight(node.depth) * bias

  return {
    node_id:  node.id,
    depth:    node.depth,
    path:     node.path,
    weight,
    phinary:  phinary_score(node.depth),
    symbol:   symbol_key,
    state:    {
      ...state,
      activation: (state.activation ?? 1.0) * weight,
      depth:      node.depth,
    },
    timestamp: new Date().toISOString(),
  }
}

// ── Reasoning path: ContextAssembly β†’ Reasoning β†’ MagmaCore ─────────────────
// Standard logical derivation. Forward read. Source β†’ conclusion.

function reasoning_path(context_state, symbol_key, claim) {
  const reasoning_node = PIPELINE.find(n => n.id === 'Reasoning')
  const act = activate_node(reasoning_node, context_state, symbol_key)

  return {
    path:       'reasoning',
    claim,
    activation: act.state.activation,
    weight:     act.weight,
    verdict:    derive_forward(claim, context_state),
    node:       act,
  }
}

// ── METATRON path: ContextAssembly β†’ Metatron β†’ MagmaCore ───────────────────
// Backward read. Reads the conclusion and asks: what must be true for this
// to hold? If those conditions are present in context β†’ certify.
// Same depth as Reasoning (5). Bypasses Reasoning entirely.

function metatron_path(context_state, symbol_key, claim) {
  const metatron_node = PIPELINE.find(n => n.id === 'Metatron')
  const act = activate_node(metatron_node, context_state, symbol_key)

  return {
    path:       'metatron',
    claim,
    activation: act.state.activation,
    weight:     act.weight,
    verdict:    read_backward(claim, context_state),
    node:       act,
  }
}

// ── Forward derivation (Reasoning node logic) ─────────────────────────────────

function derive_forward(claim, ctx) {
  const entropy = ctx.entropy ?? 0.5
  const trust   = ctx.trust   ?? 0.8

  return {
    direction:     'forward',
    entropy_gate:  entropy < 0.21 ? 'OPEN' : 'CLOSED',
    trust_score:   trust,
    consistent:    trust > 0.75 && entropy < 0.21,
    sorry_count:   ctx.sorry_count ?? 0,
    derivable:     (ctx.sorry_count ?? 0) === 0 && trust > 0.75,
  }
}

// ── Backward read (METATRON node logic) ──────────────────────────────────────
// METATRON asks: if this claim is TRUE, what constraints must hold?
// Then checks those constraints against context.
// This is the cage recognizer: it knows the cage because it built it.

function read_backward(claim, ctx) {
  const provenance     = ctx.provenance     ?? false
  const sorry_count    = ctx.sorry_count    ?? 0
  const contradiction  = ctx.contradiction  ?? false
  const boundary_check = ctx.boundary_check ?? true

  const constraints_required = [
    { name: 'no_sorry',        met: sorry_count === 0,   weight: phi_weight(5) },
    { name: 'has_provenance',  met: provenance,           weight: phi_weight(4) },
    { name: 'no_contradiction',met: !contradiction,       weight: phi_weight(3) },
    { name: 'boundary_holds',  met: boundary_check,       weight: phi_weight(2) },
  ]

  const total_weight = constraints_required.reduce((s, c) => s + c.weight, 0)
  const met_weight   = constraints_required
    .filter(c => c.met)
    .reduce((s, c) => s + c.weight, 0)

  const score = met_weight / total_weight

  return {
    direction:    'backward',
    constraints:  constraints_required,
    score:        +score.toFixed(4),
    certified:    score > 0.85,
    method:       'METATRON backward read β€” cage recognizer',
  }
}

// ── MagmaCore: intersection of both paths ────────────────────────────────────
// The sovereign output. Only certifies if BOTH paths agree.

function magma_core(reasoning, metatron, claim) {
  const both_agree = reasoning.verdict.consistent && metatron.verdict.certified
  const total_activation = reasoning.activation + metatron.activation

  return {
    node:       'MagmaCore',
    depth:      6,
    claim,
    certified:  both_agree,
    status:     both_agree ? 'SOVEREIGN_CERTIFIED' : 'INSUFFICIENT',
    reasoning_consistent:  reasoning.verdict.consistent,
    metatron_certified:    metatron.verdict.certified,
    total_activation:      +total_activation.toFixed(4),
    phinary_score:         +phinary_score(6).toFixed(6),
    phi_weight_6:          +phi_weight(6).toFixed(4),
    reason: both_agree
      ? 'Forward derivation consistent + METATRON backward read certified'
      : !reasoning.verdict.consistent
        ? 'Forward path blocked: entropy gate closed or trust below threshold'
        : 'METATRON backward read: constraints not satisfied',
  }
}

// ── Full pipeline execution ────────────────────────────────────────────────────

export async function run_pipeline(claim, ctx = {}, symbol_key = 'Me') {
  const sym = SUMERIAN[symbol_key]
  if (!sym) throw new Error(`Unknown Sumerian symbol: ${symbol_key}. Use Me/An/Ki/Dingir.`)

  // Run all nodes through Source β†’ ContextAssembly
  const activations = []
  let state = { activation: 1.0, ...ctx }

  for (const node of PIPELINE.filter(n => n.depth <= 4)) {
    const act = activate_node(node, state, symbol_key)
    state = act.state
    activations.push(act)
  }

  const context_state = state

  // Fork at ContextAssembly β€” run both paths simultaneously
  const [r_path, m_path] = await Promise.all([
    Promise.resolve(reasoning_path(context_state, symbol_key, claim)),
    Promise.resolve(metatron_path(context_state, symbol_key, claim)),
  ])

  // MagmaCore: intersection
  const output = magma_core(r_path, m_path, claim)

  // WORM seal the certified output
  const seal = output.certified
    ? worm.seal('magmacore-certified', {
        claim,
        symbol: symbol_key,
        reasoning_consistent: r_path.verdict.consistent,
        metatron_certified:   m_path.verdict.certified,
        total_activation:     output.total_activation,
      })
    : null

  return {
    claim,
    symbol:     { key: symbol_key, ...sym },
    pipeline:   activations.map(a => ({
      node:    a.node_id,
      depth:   a.depth,
      weight:  +a.weight.toFixed(4),
      phinary: +a.phinary.toFixed(6),
    })),
    reasoning_path:  r_path,
    metatron_path:   m_path,
    magmacore:       output,
    worm_seal:       seal,
    worm_valid:      worm.verify(),
  }
}

// ── CLI demo ──────────────────────────────────────────────────────────────────

if (process.argv[1].endsWith('metatron-reasoning.mjs')) {
  const claims = [
    {
      claim:  'INTERCOL(D_i, D_j) = 0 β†’ βŠ₯ (null state, not rejection)',
      ctx:    { entropy: 0.18, trust: 0.97, provenance: true, sorry_count: 0, boundary_check: true },
      symbol: 'Me',
    },
    {
      claim:  'Riemann Hypothesis: all non-trivial zeros lie on Re(s)=0.5',
      ctx:    { entropy: 0.50, trust: 0.60, provenance: false, sorry_count: 1, boundary_check: false },
      symbol: 'Dingir',
    },
    {
      claim:  'Goldilocks: 1/Ο† is the unique contractive fixed point',
      ctx:    { entropy: 0.12, trust: 0.99, provenance: true, sorry_count: 0, boundary_check: true },
      symbol: 'Ki',
    },
  ]

  console.log('\n' + '═'.repeat(68))
  console.log('  METATRON PIPELINE β€” Real Architecture')
  console.log('  BOB ResonanceGraph Β· Ο†^d grows Β· Sumerian routing')
  console.log('═'.repeat(68))

  for (const { claim, ctx, symbol } of claims) {
    const result = await run_pipeline(claim, ctx, symbol)
    const mc     = result.magmacore

    console.log(`\n  Claim: "${claim.slice(0, 60)}..."`)
    console.log(`  Symbol: ${symbol} β€” ${SUMERIAN[symbol].meaning.slice(0, 45)}`)
    console.log(`  Pipeline activations (Ο†^d grows):`)
    for (const n of result.pipeline) {
      const bar = 'β–ˆ'.repeat(Math.min(20, Math.round(n.weight / 5)))
      console.log(`    ${n.node.padEnd(16)} depth=${n.depth}  Ο†^d=${n.weight.toFixed(2).padStart(6)}  ${bar}`)
    }
    console.log(`  Reasoning: consistent=${mc.reasoning_consistent}  Metatron: certified=${mc.metatron_certified}`)
    console.log(`  MagmaCore: ${mc.status}`)
    if (result.worm_seal) {
      console.log(`  WORM seal: ${result.worm_seal}`)
    }
  }

  console.log('\n  Ο†^0=1.000  Ο†^1=1.618  Ο†^2=2.618  Ο†^3=4.236  Ο†^4=6.854  Ο†^5=11.090  Ο†^6=17.944')
  console.log('  Deeper = more signal. METATRON at depth 5 = 11x source weight.')
  console.log('  The cage builder reads backward with 11x amplification.\n')
}