Improve hyper-p2p-reputation-system with production snapshot export/import system (v0.2.0)
- Added exportSnapshot() and importSnapshot() methods for state backup, migration, analytics, and portable reputation data in Bare/Pear apps - Implemented versioned snapshots (1.1-snapshot), full metrics/history/options preservation, validation, and snapshotImported event - Added comprehensive test for snapshot roundtrip in test/test.js - Updated module README.md with v0.2.0 status and improvement details - Updated main workspace README.md with improved module entry and detailed This Run (1:32 PM EDT) summary including research, Bare scan (100% compliant), and autonomous development notes - Performed mandatory recursive Node.js builtin scan across all modules — no issues found, all using bare-* equivalents - All work inside /root/user-data/342128351638585344/projects/modules/, high-quality production-grade improvement, full docs/tests maintained Continuous development: meaningful improvement to existing module; ready for next novel primitive or further enhancements.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
const HyperP2PSemanticVectorIndex = require('../index.js')
|
||||
const b4a = require('b4a')
|
||||
|
||||
async function main () {
|
||||
console.log('=== hyper-p2p-semantic-vector-index Demo ===')
|
||||
|
||||
const index = new HyperP2PSemanticVectorIndex({
|
||||
dimension: 32, // small for demo
|
||||
enableSigning: true,
|
||||
enableQuantization: true,
|
||||
pruneIntervalMs: 5000,
|
||||
defaultTtlMs: 1000 * 60 * 5 // 5 min for demo
|
||||
})
|
||||
|
||||
console.log('Local ID:', index.localId)
|
||||
|
||||
// Generate some random normalized vectors
|
||||
function randomVec (dim) {
|
||||
const v = Array.from({ length: dim }, () => Math.random() * 2 - 1)
|
||||
const norm = Math.sqrt(v.reduce((s, x) => s + x * x, 0)) || 1
|
||||
return v.map(x => x / norm)
|
||||
}
|
||||
|
||||
// Insert several vectors
|
||||
const ids = []
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const vec = randomVec(32)
|
||||
const id = await index.insert(vec, {
|
||||
tags: i % 2 === 0 ? ['demo', 'even'] : ['demo', 'odd'],
|
||||
owner: `agent-${i % 3}`,
|
||||
description: `Embedding #${i}`,
|
||||
ttlMs: 1000 * 60 * 2
|
||||
})
|
||||
ids.push(id)
|
||||
console.log(`Inserted #${i}: ${id.substring(0, 8)}...`)
|
||||
}
|
||||
|
||||
console.log('\nMetrics after inserts:', index.getMetrics())
|
||||
|
||||
// Semantic search
|
||||
const query = randomVec(32)
|
||||
const results = await index.search(query, 3, {
|
||||
minSimilarity: 0.1,
|
||||
tags: ['demo']
|
||||
})
|
||||
console.log('\nSearch results:')
|
||||
results.forEach((r, i) => {
|
||||
console.log(` ${i + 1}. sim=${r.similarity.toFixed(4)} owner=${r.metadata.owner} tags=${r.metadata.tags.join(',')}`)
|
||||
})
|
||||
|
||||
// Tag query
|
||||
const evenResults = await index.findByTags(['even'], { mode: 'union' })
|
||||
console.log(`\nFound ${evenResults.length} vectors with 'even' tag`)
|
||||
|
||||
// P2P topic
|
||||
const topic = index.createP2PTopic('demo-swarm')
|
||||
console.log('\nP2P Topic (hex):', b4a.toString(topic, 'hex').substring(0, 16) + '...')
|
||||
|
||||
// Simulate gossip receive (self for demo)
|
||||
const gossipPayload = {
|
||||
type: 'vector-insert',
|
||||
id: 'gossip-demo-123',
|
||||
entry: {
|
||||
vector: randomVec(32),
|
||||
metadata: { tags: ['gossip'], owner: 'remote-peer' },
|
||||
timestamp: Date.now(),
|
||||
signature: null,
|
||||
issuer: null
|
||||
}
|
||||
}
|
||||
const accepted = await index.receiveGossip(gossipPayload)
|
||||
console.log('Gossip accepted:', accepted)
|
||||
|
||||
// Prune demo (force some expiry)
|
||||
await index.pruneExpired()
|
||||
console.log('After prune, total vectors:', index.getMetrics().totalVectors)
|
||||
|
||||
// Close
|
||||
await index.close()
|
||||
console.log('\nDemo completed successfully. All Bare-compatible.')
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
Reference in New Issue
Block a user