File size: 6,502 Bytes
842005b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import crypto from 'node:crypto'

export type ConsensusAgent = 'ORACLE' | 'SENTINEL' | 'CIPHER' | 'AXIOM' | 'MNEMEX'
export type Vote = 'approve' | 'reject'

export interface BridgedProject {
  sacmId: string
  legacyId: string
  legacySource: string
  name: string
  payload: unknown
  importedAt: string
  status: 'imported' | 'rejected' | 'optimizing' | 'sovereign'
  integrityHmac?: string
}

export interface AgentVote {
  agent: ConsensusAgent
  vote: Vote
  rationale: string
  signature: string
  wormPosition: number
}

export interface AgentWorkSeal {
  agent: string
  timestamp: string
  signature: string
}

export interface ConsensusProof {
  sacmId: string
  consensusId: string
  votes: AgentVote[]
  quorum: boolean
  approvalCount: number
  requiredQuorum: number
  wormHash: string
  sealedAt: string
  architecture: 'WORM-Causal-Consensus-Mesh'
  masterSeal: AgentWorkSeal
}

const AGENTS: ConsensusAgent[] = ['ORACLE', 'SENTINEL', 'CIPHER', 'AXIOM', 'MNEMEX']
const QUORUM_THRESHOLD = 0.6
const SECRET_PATTERNS = [/password/i, /secret/i, /token/i, /api.?key/i, /private.?key/i, /bearer/i]
const VENDOR_LOCK_KEYS = ['salesforce_id', 'hubspot', 'segment_id', 'mixpanel', 'amplitude', 'intercom']

export class SACMOptimizer {
  constructor(private readonly secret = process.env.VAULT_MASTER_SECRET ?? 'dev-sacm-optimizer-key') {
    if (process.env.NODE_ENV === 'production' && !process.env.VAULT_MASTER_SECRET) {
      throw new Error('VAULT_MASTER_SECRET is required in production')
    }
  }

  runConsensus(project: BridgedProject): ConsensusProof {
    if (project.status === 'rejected') {
      throw new Error(`Project ${project.sacmId} was rejected at bridge`)
    }

    const sealedAt = new Date().toISOString()
    const consensusId = `cons_${crypto.randomUUID().replaceAll('-', '').slice(0, 16)}`
    const votes = AGENTS.map((agent, index) => this.evaluateAgent(agent, index, project, sealedAt))
    const approvalCount = votes.filter(v => v.vote === 'approve').length
    const requiredQuorum = Math.ceil(AGENTS.length * QUORUM_THRESHOLD)
    const wormHash = this.hmac(`${project.sacmId}:${consensusId}:${sealedAt}:${votes.map(v => v.signature).join(':')}`)
    const proofBase = {
      sacmId: project.sacmId,
      consensusId,
      votes,
      quorum: approvalCount >= requiredQuorum,
      approvalCount,
      requiredQuorum,
      wormHash,
      sealedAt,
      architecture: 'WORM-Causal-Consensus-Mesh' as const,
    }
    return {
      ...proofBase,
      masterSeal: {
        agent: 'MNEMEX',
        timestamp: sealedAt,
        signature: this.hmac(`MNEMEX:${sealedAt}:${wormHash}`),
      },
    }
  }

  verifyProof(proof: ConsensusProof): boolean {
    const expectedWormHash = this.hmac(`${proof.sacmId}:${proof.consensusId}:${proof.sealedAt}:${proof.votes.map(v => v.signature).join(':')}`)
    const expectedMaster = this.hmac(`MNEMEX:${proof.sealedAt}:${expectedWormHash}`)
    return proof.wormHash === expectedWormHash && proof.masterSeal.signature === expectedMaster
  }

  private evaluateAgent(agent: ConsensusAgent, wormPosition: number, project: BridgedProject, ts: string): AgentVote {
    const result = validators[agent](project)
    return {
      agent,
      vote: result.approved ? 'approve' : 'reject',
      rationale: result.rationale,
      signature: this.hmac(`${agent}:${project.sacmId}:${result.approved ? 'approve' : 'reject'}:${ts}:${wormPosition}:${result.rationale}`),
      wormPosition,
    }
  }

  private hmac(message: string): string {
    return crypto.createHmac('sha256', this.secret).update(message).digest('hex')
  }
}

interface ValidatorResult {
  approved: boolean
  rationale: string
}

const validators: Record<ConsensusAgent, (project: BridgedProject) => ValidatorResult> = {
  ORACLE(project) {
    const hasIdentity = Boolean(project.sacmId && project.legacyId && project.name && project.importedAt)
    return {
      approved: hasIdentity && project.status === 'imported',
      rationale: hasIdentity ? 'Project identity and bridge status are present' : 'Project identity is incomplete',
    }
  },
  SENTINEL(project) {
    const paths = findMatchingPaths(project.payload, SECRET_PATTERNS)
    return {
      approved: paths.length === 0,
      rationale: paths.length === 0 ? 'No credential-like fields detected' : `Credential-like fields detected: ${paths.join(', ')}`,
    }
  },
  CIPHER(project) {
    const payloadHash = sha256(stableStringify(project.payload))
    return {
      approved: payloadHash.length === 64 && Boolean(project.integrityHmac || project.sacmId),
      rationale: `Payload SHA-256 computed: ${payloadHash.slice(0, 16)}`,
    }
  },
  AXIOM(project) {
    const lockKeys = findMatchingPaths(project.payload, VENDOR_LOCK_KEYS.map(k => new RegExp(k, 'i')))
    return {
      approved: lockKeys.length <= 5,
      rationale: lockKeys.length === 0 ? 'No vendor lock-in keys detected' : `Vendor lock keys queued for sovereign strip: ${lockKeys.join(', ')}`,
    }
  },
  MNEMEX(project) {
    const importedAt = Date.parse(project.importedAt)
    return {
      approved: Number.isFinite(importedAt) && project.sacmId.startsWith('sacm_'),
      rationale: 'Causal memory anchor is parseable and SACM ID is namespaced',
    }
  },
}

function findMatchingPaths(value: unknown, patterns: RegExp[], path = '$'): string[] {
  if (value === null || value === undefined) return []
  if (Array.isArray(value)) return value.flatMap((item, index) => findMatchingPaths(item, patterns, `${path}[${index}]`))
  if (typeof value !== 'object') return []
  return Object.entries(value as Record<string, unknown>).flatMap(([key, child]) => {
    const nextPath = `${path}.${key}`
    const own = patterns.some(p => p.test(key)) ? [nextPath] : []
    return own.concat(findMatchingPaths(child, patterns, nextPath))
  })
}

export function stableStringify(value: unknown): string {
  if (value === null || typeof value !== 'object') return JSON.stringify(value)
  if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`
  const obj = value as Record<string, unknown>
  return `{${Object.keys(obj).sort().map(key => `${JSON.stringify(key)}:${stableStringify(obj[key])}`).join(',')}}`
}

function sha256(input: string): string {
  return crypto.createHash('sha256').update(input).digest('hex')
}