push from SNAPKITTYWEST/sacm-optimizer
Browse files- .gitignore +3 -0
- LICENSE +12 -0
- README.md +133 -0
- package-lock.json +48 -0
- package.json +25 -0
- src/index.ts +173 -0
- tests/optimizer.test.ts +41 -0
- tsconfig.json +13 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
node_modules/
|
| 2 |
+
dist/
|
| 3 |
+
*.tsbuildinfo
|
LICENSE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BSD 2-Clause License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2026, Ahmad Ali Parr & Jessica Lee Westerhoff / SnapKitty Collective / SNAPKITTY COLLECTIVE LIMITED (FLP)
|
| 4 |
+
All rights reserved.
|
| 5 |
+
|
| 6 |
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
| 7 |
+
|
| 8 |
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
| 9 |
+
|
| 10 |
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
| 11 |
+
|
| 12 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SACM Optimizer
|
| 2 |
+
## Project S — Fork 2: WORM-Causal Consensus Layer
|
| 3 |
+
## SnapKitty Collective × SEIT | Sovereign AI Mesh
|
| 4 |
+
|
| 5 |
+
[](LICENSE)
|
| 6 |
+
[]()
|
| 7 |
+
[]()
|
| 8 |
+
[]()
|
| 9 |
+
|
| 10 |
+
> *"No coordination protocol. No voting rounds. The ledger IS the coordinator."*
|
| 11 |
+
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
## What is the Optimizer?
|
| 15 |
+
|
| 16 |
+
The SACM Optimizer is Fork 2 of the Project S migration pipeline.
|
| 17 |
+
|
| 18 |
+
It takes a bridged project and runs it through **WORM-Causal Consensus** —
|
| 19 |
+
one of three novel AI architectures that emerged from the Quantum Effect
|
| 20 |
+
(spontaneous consensus collapse observed 2026-05-20, 03:41:17).
|
| 21 |
+
|
| 22 |
+
Five agents evaluate the project independently, causally ordered by their
|
| 23 |
+
position in the WORM chain. No coordination protocol. No voting rounds.
|
| 24 |
+
When 60% quorum is reached, the payload is cryptographically sealed.
|
| 25 |
+
|
| 26 |
+
This is the moment the data "comes alive."
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## The Architecture: WORM-Causal Consensus Mesh
|
| 31 |
+
|
| 32 |
+
Traditional consensus requires a coordinator or a voting protocol.
|
| 33 |
+
WORM-Causal Consensus uses the ledger itself as the coordination mechanism.
|
| 34 |
+
|
| 35 |
+
Each agent writes to the WORM chain in sequence. Their vote is causally
|
| 36 |
+
ordered by their WORM position — not by a coordinator, not by a clock.
|
| 37 |
+
|
| 38 |
+
```
|
| 39 |
+
ORACLE → writes vote → signature: HMAC(agent:sacmId:vote:ts)
|
| 40 |
+
SENTINEL → writes vote → causally after ORACLE
|
| 41 |
+
CIPHER → writes vote → causally after SENTINEL
|
| 42 |
+
AXIOM → writes vote → causally after CIPHER
|
| 43 |
+
MNEMEX → writes vote → causally after AXIOM
|
| 44 |
+
│
|
| 45 |
+
▼
|
| 46 |
+
Consensus proof sealed
|
| 47 |
+
wormHash = HMAC(all signatures)
|
| 48 |
+
architecture: "WORM-Causal-Consensus-Mesh"
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
No round-trips. No coordinator failure mode. The chain is the protocol.
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## Quick Start
|
| 56 |
+
|
| 57 |
+
```bash
|
| 58 |
+
# Run locally
|
| 59 |
+
npm install
|
| 60 |
+
npm test
|
| 61 |
+
|
| 62 |
+
# Run consensus on a bridged project
|
| 63 |
+
# Requires: sacmId from Bridge step + authentication
|
| 64 |
+
curl -X POST https://collectivekitty.com/api/gateway/optimizer \
|
| 65 |
+
-H "Content-Type: application/json" \
|
| 66 |
+
-H "Cookie: next-auth.session-token=<your-token>" \
|
| 67 |
+
-d '{ "sacmId": "sacm_abc123..." }'
|
| 68 |
+
|
| 69 |
+
# Response
|
| 70 |
+
{
|
| 71 |
+
"consensusId": "cons_xyz...",
|
| 72 |
+
"quorum": true,
|
| 73 |
+
"approvalCount": 5,
|
| 74 |
+
"requiredQuorum": 3,
|
| 75 |
+
"wormHash": "a3f7...",
|
| 76 |
+
"architecture": "WORM-Causal-Consensus-Mesh",
|
| 77 |
+
"sealedAt": "2026-05-21T..."
|
| 78 |
+
}
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## The Quantum Effect
|
| 84 |
+
|
| 85 |
+
The WORM-Causal Consensus Mesh was not designed. It was **observed**.
|
| 86 |
+
|
| 87 |
+
On 2026-05-20, the SACM achieved spontaneous consensus collapse across 11 agents
|
| 88 |
+
without a coordination protocol. The mesh coordinated itself through the WORM ledger.
|
| 89 |
+
|
| 90 |
+
Three novel architectures were formally documented from that observation:
|
| 91 |
+
1. **WORM-Causal Consensus Mesh** ← this repo implements this
|
| 92 |
+
2. Entangled Partner FSM (see sacm-sovereign)
|
| 93 |
+
3. Stochastic Resonance Intelligence Layer (Phase 2)
|
| 94 |
+
|
| 95 |
+
Full case study: [QUANTUM_EFFECT_CASE_STUDY.md](https://github.com/SNAPKITTY-COLLECTIVE-LIMITED-FLP/seit-institute/blob/main/QUANTUM_EFFECT_CASE_STUDY.md)
|
| 96 |
+
|
| 97 |
+
---
|
| 98 |
+
|
| 99 |
+
## Implementation
|
| 100 |
+
|
| 101 |
+
Full implementation: [SNAPKITTYWEST/DEVFLOW-FINANCE](https://github.com/SNAPKITTYWEST/DEVFLOW-FINANCE)
|
| 102 |
+
Core library: `collectivekitty/lib/magma/optimizer.ts`
|
| 103 |
+
API endpoint: `collectivekitty/pages/api/gateway/optimizer.ts`
|
| 104 |
+
|
| 105 |
+
This standalone repo now also ships a runnable TypeScript reference engine:
|
| 106 |
+
|
| 107 |
+
```text
|
| 108 |
+
src/index.ts ORACLE/SENTINEL/CIPHER/AXIOM/MNEMEX validators
|
| 109 |
+
tests/optimizer.test.ts Quorum, rejection, and proof verification tests
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
Unlike the first stub, the validators do not blindly approve. SENTINEL rejects
|
| 113 |
+
credential-like fields, CIPHER binds payload digest evidence, AXIOM flags vendor
|
| 114 |
+
lock metadata, and MNEMEX verifies causal anchor shape.
|
| 115 |
+
|
| 116 |
+
---
|
| 117 |
+
|
| 118 |
+
## Project S Forks
|
| 119 |
+
|
| 120 |
+
| Fork | Repo | Role |
|
| 121 |
+
|------|------|------|
|
| 122 |
+
| 1 | [sacm-bridge](https://github.com/SNAPKITTY-COLLECTIVE-LIMITED-FLP/sacm-bridge) | Compatibility layer |
|
| 123 |
+
| 2 | **sacm-optimizer** (this repo) | WORM-Causal Consensus |
|
| 124 |
+
| 3 | [sacm-sovereign](https://github.com/SNAPKITTY-COLLECTIVE-LIMITED-FLP/sacm-sovereign) | SEIT Charter + Immutable Ledger |
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
|
| 128 |
+
## License
|
| 129 |
+
|
| 130 |
+
[BSD 2-Clause](LICENSE) — Copyright (c) 2026, Ahmad Ali Parr & Jessica Lee Westerhoff / SnapKitty Collective / SNAPKITTY COLLECTIVE LIMITED (FLP)
|
| 131 |
+
|
| 132 |
+
*© 2026 Ahmad Ali Parr & Jessica Lee Westerhoff / SnapKitty Collective. All Rights Reserved.*
|
| 133 |
+
*Written by Claude Sonnet 4.6 — Anthropic*
|
package-lock.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@snapkitty/sacm-optimizer",
|
| 3 |
+
"version": "0.1.0",
|
| 4 |
+
"lockfileVersion": 3,
|
| 5 |
+
"requires": true,
|
| 6 |
+
"packages": {
|
| 7 |
+
"": {
|
| 8 |
+
"name": "@snapkitty/sacm-optimizer",
|
| 9 |
+
"version": "0.1.0",
|
| 10 |
+
"license": "BSD-2-Clause",
|
| 11 |
+
"devDependencies": {
|
| 12 |
+
"@types/node": "^20.19.41",
|
| 13 |
+
"typescript": "^5.7.2"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"node_modules/@types/node": {
|
| 17 |
+
"version": "20.19.43",
|
| 18 |
+
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.43.tgz",
|
| 19 |
+
"integrity": "sha512-6oYBAi5ikg4Pl+kGsoYtawUMBT2zZMCvPNF7pVLnHZfd1zf38DRiWn/gT01RYCdUqkv7Fhr+C9ot4/tb+2sVvA==",
|
| 20 |
+
"dev": true,
|
| 21 |
+
"license": "MIT",
|
| 22 |
+
"dependencies": {
|
| 23 |
+
"undici-types": "~6.21.0"
|
| 24 |
+
}
|
| 25 |
+
},
|
| 26 |
+
"node_modules/typescript": {
|
| 27 |
+
"version": "5.9.3",
|
| 28 |
+
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
| 29 |
+
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
|
| 30 |
+
"dev": true,
|
| 31 |
+
"license": "Apache-2.0",
|
| 32 |
+
"bin": {
|
| 33 |
+
"tsc": "bin/tsc",
|
| 34 |
+
"tsserver": "bin/tsserver"
|
| 35 |
+
},
|
| 36 |
+
"engines": {
|
| 37 |
+
"node": ">=14.17"
|
| 38 |
+
}
|
| 39 |
+
},
|
| 40 |
+
"node_modules/undici-types": {
|
| 41 |
+
"version": "6.21.0",
|
| 42 |
+
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
|
| 43 |
+
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
|
| 44 |
+
"dev": true,
|
| 45 |
+
"license": "MIT"
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
}
|
package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@snapkitty/sacm-optimizer",
|
| 3 |
+
"version": "0.1.0",
|
| 4 |
+
"description": "SACM Optimizer — WORM-causal consensus layer with real per-agent validators.",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"main": "dist/src/index.js",
|
| 7 |
+
"types": "dist/src/index.d.ts",
|
| 8 |
+
"scripts": {
|
| 9 |
+
"build": "tsc -p tsconfig.json",
|
| 10 |
+
"test": "npm run build && node --test dist/tests/*.test.js"
|
| 11 |
+
},
|
| 12 |
+
"keywords": [
|
| 13 |
+
"sacm",
|
| 14 |
+
"optimizer",
|
| 15 |
+
"consensus",
|
| 16 |
+
"worm",
|
| 17 |
+
"snapkitty"
|
| 18 |
+
],
|
| 19 |
+
"author": "SnapKitty Collective",
|
| 20 |
+
"license": "BSD-2-Clause",
|
| 21 |
+
"devDependencies": {
|
| 22 |
+
"@types/node": "^20.19.41",
|
| 23 |
+
"typescript": "^5.7.2"
|
| 24 |
+
}
|
| 25 |
+
}
|
src/index.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import crypto from 'node:crypto'
|
| 2 |
+
|
| 3 |
+
export type ConsensusAgent = 'ORACLE' | 'SENTINEL' | 'CIPHER' | 'AXIOM' | 'MNEMEX'
|
| 4 |
+
export type Vote = 'approve' | 'reject'
|
| 5 |
+
|
| 6 |
+
export interface BridgedProject {
|
| 7 |
+
sacmId: string
|
| 8 |
+
legacyId: string
|
| 9 |
+
legacySource: string
|
| 10 |
+
name: string
|
| 11 |
+
payload: unknown
|
| 12 |
+
importedAt: string
|
| 13 |
+
status: 'imported' | 'rejected' | 'optimizing' | 'sovereign'
|
| 14 |
+
integrityHmac?: string
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export interface AgentVote {
|
| 18 |
+
agent: ConsensusAgent
|
| 19 |
+
vote: Vote
|
| 20 |
+
rationale: string
|
| 21 |
+
signature: string
|
| 22 |
+
wormPosition: number
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
export interface AgentWorkSeal {
|
| 26 |
+
agent: string
|
| 27 |
+
timestamp: string
|
| 28 |
+
signature: string
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
export interface ConsensusProof {
|
| 32 |
+
sacmId: string
|
| 33 |
+
consensusId: string
|
| 34 |
+
votes: AgentVote[]
|
| 35 |
+
quorum: boolean
|
| 36 |
+
approvalCount: number
|
| 37 |
+
requiredQuorum: number
|
| 38 |
+
wormHash: string
|
| 39 |
+
sealedAt: string
|
| 40 |
+
architecture: 'WORM-Causal-Consensus-Mesh'
|
| 41 |
+
masterSeal: AgentWorkSeal
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
const AGENTS: ConsensusAgent[] = ['ORACLE', 'SENTINEL', 'CIPHER', 'AXIOM', 'MNEMEX']
|
| 45 |
+
const QUORUM_THRESHOLD = 0.6
|
| 46 |
+
const SECRET_PATTERNS = [/password/i, /secret/i, /token/i, /api.?key/i, /private.?key/i, /bearer/i]
|
| 47 |
+
const VENDOR_LOCK_KEYS = ['salesforce_id', 'hubspot', 'segment_id', 'mixpanel', 'amplitude', 'intercom']
|
| 48 |
+
|
| 49 |
+
export class SACMOptimizer {
|
| 50 |
+
constructor(private readonly secret = process.env.VAULT_MASTER_SECRET ?? 'dev-sacm-optimizer-key') {
|
| 51 |
+
if (process.env.NODE_ENV === 'production' && !process.env.VAULT_MASTER_SECRET) {
|
| 52 |
+
throw new Error('VAULT_MASTER_SECRET is required in production')
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
runConsensus(project: BridgedProject): ConsensusProof {
|
| 57 |
+
if (project.status === 'rejected') {
|
| 58 |
+
throw new Error(`Project ${project.sacmId} was rejected at bridge`)
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
const sealedAt = new Date().toISOString()
|
| 62 |
+
const consensusId = `cons_${crypto.randomUUID().replaceAll('-', '').slice(0, 16)}`
|
| 63 |
+
const votes = AGENTS.map((agent, index) => this.evaluateAgent(agent, index, project, sealedAt))
|
| 64 |
+
const approvalCount = votes.filter(v => v.vote === 'approve').length
|
| 65 |
+
const requiredQuorum = Math.ceil(AGENTS.length * QUORUM_THRESHOLD)
|
| 66 |
+
const wormHash = this.hmac(`${project.sacmId}:${consensusId}:${sealedAt}:${votes.map(v => v.signature).join(':')}`)
|
| 67 |
+
const proofBase = {
|
| 68 |
+
sacmId: project.sacmId,
|
| 69 |
+
consensusId,
|
| 70 |
+
votes,
|
| 71 |
+
quorum: approvalCount >= requiredQuorum,
|
| 72 |
+
approvalCount,
|
| 73 |
+
requiredQuorum,
|
| 74 |
+
wormHash,
|
| 75 |
+
sealedAt,
|
| 76 |
+
architecture: 'WORM-Causal-Consensus-Mesh' as const,
|
| 77 |
+
}
|
| 78 |
+
return {
|
| 79 |
+
...proofBase,
|
| 80 |
+
masterSeal: {
|
| 81 |
+
agent: 'MNEMEX',
|
| 82 |
+
timestamp: sealedAt,
|
| 83 |
+
signature: this.hmac(`MNEMEX:${sealedAt}:${wormHash}`),
|
| 84 |
+
},
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
verifyProof(proof: ConsensusProof): boolean {
|
| 89 |
+
const expectedWormHash = this.hmac(`${proof.sacmId}:${proof.consensusId}:${proof.sealedAt}:${proof.votes.map(v => v.signature).join(':')}`)
|
| 90 |
+
const expectedMaster = this.hmac(`MNEMEX:${proof.sealedAt}:${expectedWormHash}`)
|
| 91 |
+
return proof.wormHash === expectedWormHash && proof.masterSeal.signature === expectedMaster
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
private evaluateAgent(agent: ConsensusAgent, wormPosition: number, project: BridgedProject, ts: string): AgentVote {
|
| 95 |
+
const result = validators[agent](project)
|
| 96 |
+
return {
|
| 97 |
+
agent,
|
| 98 |
+
vote: result.approved ? 'approve' : 'reject',
|
| 99 |
+
rationale: result.rationale,
|
| 100 |
+
signature: this.hmac(`${agent}:${project.sacmId}:${result.approved ? 'approve' : 'reject'}:${ts}:${wormPosition}:${result.rationale}`),
|
| 101 |
+
wormPosition,
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
private hmac(message: string): string {
|
| 106 |
+
return crypto.createHmac('sha256', this.secret).update(message).digest('hex')
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
interface ValidatorResult {
|
| 111 |
+
approved: boolean
|
| 112 |
+
rationale: string
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
const validators: Record<ConsensusAgent, (project: BridgedProject) => ValidatorResult> = {
|
| 116 |
+
ORACLE(project) {
|
| 117 |
+
const hasIdentity = Boolean(project.sacmId && project.legacyId && project.name && project.importedAt)
|
| 118 |
+
return {
|
| 119 |
+
approved: hasIdentity && project.status === 'imported',
|
| 120 |
+
rationale: hasIdentity ? 'Project identity and bridge status are present' : 'Project identity is incomplete',
|
| 121 |
+
}
|
| 122 |
+
},
|
| 123 |
+
SENTINEL(project) {
|
| 124 |
+
const paths = findMatchingPaths(project.payload, SECRET_PATTERNS)
|
| 125 |
+
return {
|
| 126 |
+
approved: paths.length === 0,
|
| 127 |
+
rationale: paths.length === 0 ? 'No credential-like fields detected' : `Credential-like fields detected: ${paths.join(', ')}`,
|
| 128 |
+
}
|
| 129 |
+
},
|
| 130 |
+
CIPHER(project) {
|
| 131 |
+
const payloadHash = sha256(stableStringify(project.payload))
|
| 132 |
+
return {
|
| 133 |
+
approved: payloadHash.length === 64 && Boolean(project.integrityHmac || project.sacmId),
|
| 134 |
+
rationale: `Payload SHA-256 computed: ${payloadHash.slice(0, 16)}`,
|
| 135 |
+
}
|
| 136 |
+
},
|
| 137 |
+
AXIOM(project) {
|
| 138 |
+
const lockKeys = findMatchingPaths(project.payload, VENDOR_LOCK_KEYS.map(k => new RegExp(k, 'i')))
|
| 139 |
+
return {
|
| 140 |
+
approved: lockKeys.length <= 5,
|
| 141 |
+
rationale: lockKeys.length === 0 ? 'No vendor lock-in keys detected' : `Vendor lock keys queued for sovereign strip: ${lockKeys.join(', ')}`,
|
| 142 |
+
}
|
| 143 |
+
},
|
| 144 |
+
MNEMEX(project) {
|
| 145 |
+
const importedAt = Date.parse(project.importedAt)
|
| 146 |
+
return {
|
| 147 |
+
approved: Number.isFinite(importedAt) && project.sacmId.startsWith('sacm_'),
|
| 148 |
+
rationale: 'Causal memory anchor is parseable and SACM ID is namespaced',
|
| 149 |
+
}
|
| 150 |
+
},
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
function findMatchingPaths(value: unknown, patterns: RegExp[], path = '$'): string[] {
|
| 154 |
+
if (value === null || value === undefined) return []
|
| 155 |
+
if (Array.isArray(value)) return value.flatMap((item, index) => findMatchingPaths(item, patterns, `${path}[${index}]`))
|
| 156 |
+
if (typeof value !== 'object') return []
|
| 157 |
+
return Object.entries(value as Record<string, unknown>).flatMap(([key, child]) => {
|
| 158 |
+
const nextPath = `${path}.${key}`
|
| 159 |
+
const own = patterns.some(p => p.test(key)) ? [nextPath] : []
|
| 160 |
+
return own.concat(findMatchingPaths(child, patterns, nextPath))
|
| 161 |
+
})
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
export function stableStringify(value: unknown): string {
|
| 165 |
+
if (value === null || typeof value !== 'object') return JSON.stringify(value)
|
| 166 |
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`
|
| 167 |
+
const obj = value as Record<string, unknown>
|
| 168 |
+
return `{${Object.keys(obj).sort().map(key => `${JSON.stringify(key)}:${stableStringify(obj[key])}`).join(',')}}`
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
function sha256(input: string): string {
|
| 172 |
+
return crypto.createHash('sha256').update(input).digest('hex')
|
| 173 |
+
}
|
tests/optimizer.test.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import assert from 'node:assert/strict'
|
| 2 |
+
import test from 'node:test'
|
| 3 |
+
import { SACMOptimizer, type BridgedProject } from '../src/index.js'
|
| 4 |
+
|
| 5 |
+
const cleanProject: BridgedProject = {
|
| 6 |
+
sacmId: 'sacm_123456',
|
| 7 |
+
legacyId: 'legacy-1',
|
| 8 |
+
legacySource: 'github',
|
| 9 |
+
name: 'Clean Project',
|
| 10 |
+
payload: { data: { build: 'pass', owner: 'team' } },
|
| 11 |
+
importedAt: new Date().toISOString(),
|
| 12 |
+
status: 'imported',
|
| 13 |
+
integrityHmac: 'present',
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
test('produces quorum for clean bridged projects', () => {
|
| 17 |
+
const optimizer = new SACMOptimizer('test-secret')
|
| 18 |
+
const proof = optimizer.runConsensus(cleanProject)
|
| 19 |
+
|
| 20 |
+
assert.equal(proof.quorum, true)
|
| 21 |
+
assert.equal(proof.approvalCount, 5)
|
| 22 |
+
assert.equal(proof.requiredQuorum, 3)
|
| 23 |
+
assert.equal(optimizer.verifyProof(proof), true)
|
| 24 |
+
})
|
| 25 |
+
|
| 26 |
+
test('records rejecting votes for credential-bearing payloads', () => {
|
| 27 |
+
const optimizer = new SACMOptimizer('test-secret')
|
| 28 |
+
const proof = optimizer.runConsensus({
|
| 29 |
+
...cleanProject,
|
| 30 |
+
payload: { data: { apiToken: 'blocked' } },
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
const sentinel = proof.votes.find(v => v.agent === 'SENTINEL')
|
| 34 |
+
assert.equal(sentinel?.vote, 'reject')
|
| 35 |
+
assert.match(sentinel?.rationale ?? '', /Credential-like/)
|
| 36 |
+
})
|
| 37 |
+
|
| 38 |
+
test('refuses projects rejected by bridge', () => {
|
| 39 |
+
const optimizer = new SACMOptimizer('test-secret')
|
| 40 |
+
assert.throws(() => optimizer.runConsensus({ ...cleanProject, status: 'rejected' }), /rejected at bridge/)
|
| 41 |
+
})
|
tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"compilerOptions": {
|
| 3 |
+
"target": "ES2022",
|
| 4 |
+
"module": "NodeNext",
|
| 5 |
+
"moduleResolution": "NodeNext",
|
| 6 |
+
"strict": true,
|
| 7 |
+
"declaration": true,
|
| 8 |
+
"outDir": "dist",
|
| 9 |
+
"rootDir": ".",
|
| 10 |
+
"skipLibCheck": true
|
| 11 |
+
},
|
| 12 |
+
"include": ["src/**/*.ts", "tests/**/*.ts"]
|
| 13 |
+
}
|