@unrdf/knowledge-engine 5.0.1 → 26.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -7
- package/src/ai-enhanced-search.mjs +371 -0
- package/src/anomaly-detector.mjs +226 -0
- package/src/artifact-generator.mjs +251 -0
- package/src/browser.mjs +1 -1
- package/src/chatman/disruption-arithmetic.mjs +140 -0
- package/src/chatman/market-dynamics.mjs +140 -0
- package/src/chatman/organizational-dynamics.mjs +140 -0
- package/src/chatman/strategic-dynamics.mjs +140 -0
- package/src/chatman-config-loader.mjs +282 -0
- package/src/chatman-engine.mjs +431 -0
- package/src/chatman-operator.mjs +342 -0
- package/src/dark-field-detector.mjs +312 -0
- package/src/formation-theorems.mjs +345 -0
- package/src/index.mjs +20 -2
- package/src/knowledge-hook-manager.mjs +1 -1
- package/src/lockchain-writer-browser.mjs +2 -2
- package/src/observability.mjs +40 -4
- package/src/query-optimizer.mjs +1 -1
- package/src/resolution-layer.mjs +1 -1
- package/src/transaction.mjs +11 -9
- package/README.md +0 -84
- package/src/browser-shims.mjs +0 -343
- package/src/canonicalize.mjs +0 -414
- package/src/condition-cache.mjs +0 -109
- package/src/condition-evaluator.mjs +0 -722
- package/src/dark-matter-core.mjs +0 -742
- package/src/define-hook.mjs +0 -213
- package/src/effect-sandbox-browser.mjs +0 -283
- package/src/effect-sandbox-worker.mjs +0 -170
- package/src/effect-sandbox.mjs +0 -517
- package/src/engines/index.mjs +0 -11
- package/src/engines/rdf-engine.mjs +0 -299
- package/src/file-resolver.mjs +0 -387
- package/src/hook-executor-batching.mjs +0 -277
- package/src/hook-executor.mjs +0 -870
- package/src/hook-management.mjs +0 -150
- package/src/ken-parliment.mjs +0 -119
- package/src/ken.mjs +0 -149
- package/src/knowledge-engine/builtin-rules.mjs +0 -190
- package/src/knowledge-engine/inference-engine.mjs +0 -418
- package/src/knowledge-engine/knowledge-engine.mjs +0 -317
- package/src/knowledge-engine/pattern-dsl.mjs +0 -142
- package/src/knowledge-engine/pattern-matcher.mjs +0 -215
- package/src/knowledge-engine/rules.mjs +0 -184
- package/src/knowledge-engine.mjs +0 -319
- package/src/knowledge-hook-engine.mjs +0 -360
- package/src/knowledge-substrate-core.mjs +0 -927
- package/src/lite.mjs +0 -222
- package/src/lockchain-writer.mjs +0 -602
- package/src/monitoring/andon-signals.mjs +0 -775
- package/src/parse.mjs +0 -290
- package/src/performance-optimizer.mjs +0 -678
- package/src/policy-pack.mjs +0 -572
- package/src/query-cache.mjs +0 -116
- package/src/query.mjs +0 -306
- package/src/reason.mjs +0 -350
- package/src/schemas.mjs +0 -1063
- package/src/security/error-sanitizer.mjs +0 -257
- package/src/security/path-validator.mjs +0 -194
- package/src/security/sandbox-restrictions.mjs +0 -331
- package/src/security-validator.mjs +0 -389
- package/src/store-cache.mjs +0 -137
- package/src/telemetry.mjs +0 -167
- package/src/utils/adaptive-monitor.mjs +0 -746
- package/src/utils/circuit-breaker.mjs +0 -513
- package/src/utils/edge-case-handler.mjs +0 -503
- package/src/utils/memory-manager.mjs +0 -498
- package/src/utils/ring-buffer.mjs +0 -282
- package/src/validate.mjs +0 -319
- package/src/validators/index.mjs +0 -338
package/src/store-cache.mjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Store Cache - Eliminates N×M store conversions
|
|
3
|
-
*
|
|
4
|
-
* @description
|
|
5
|
-
* Caches Oxigraph store instances by version hash to eliminate redundant
|
|
6
|
-
* N3 Store → Oxigraph Store conversions on every SPARQL query.
|
|
7
|
-
*
|
|
8
|
-
* Cache Strategy:
|
|
9
|
-
* - Key: Store version hash (quad count + sampled quad hashes)
|
|
10
|
-
* - Value: Oxigraph store instance
|
|
11
|
-
* - Invalidation: On any delta application
|
|
12
|
-
* - LRU eviction: Maximum 10 entries
|
|
13
|
-
*
|
|
14
|
-
* Expected Impact: 50-70% latency reduction
|
|
15
|
-
*
|
|
16
|
-
* @module knowledge-engine/store-cache
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import crypto from 'node:crypto';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Simple fast hash function for quad sampling
|
|
23
|
-
* @param {string} text - Text to hash
|
|
24
|
-
* @returns {string} Hex digest
|
|
25
|
-
*/
|
|
26
|
-
function quickHash(text) {
|
|
27
|
-
return crypto.createHash('md5').update(text).digest('hex').slice(0, 8);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Store Cache - Oxigraph instance caching by store version
|
|
32
|
-
*
|
|
33
|
-
* @class StoreCache
|
|
34
|
-
*/
|
|
35
|
-
export class StoreCache {
|
|
36
|
-
/**
|
|
37
|
-
* Create a new store cache
|
|
38
|
-
* @param {object} options - Configuration options
|
|
39
|
-
* @param {number} options.maxSize - Maximum cache entries (default: 10)
|
|
40
|
-
*/
|
|
41
|
-
constructor(options = {}) {
|
|
42
|
-
this.cache = new Map(); // storeVersion → Oxigraph Store
|
|
43
|
-
this.currentVersion = null;
|
|
44
|
-
this.maxSize = options.maxSize || 10;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get store version hash (fast fingerprint)
|
|
49
|
-
*
|
|
50
|
-
* Computes a version hash based on:
|
|
51
|
-
* - Total quad count (main cache key differentiator)
|
|
52
|
-
* - Hash of first 100 quads + last 100 quads (quick validation)
|
|
53
|
-
*
|
|
54
|
-
* @param {Store} store - N3 Store instance
|
|
55
|
-
* @returns {string} Version hash (format: "count-hash")
|
|
56
|
-
*/
|
|
57
|
-
getVersion(store) {
|
|
58
|
-
// Get all quads (this is unavoidable)
|
|
59
|
-
const quads = store.getQuads();
|
|
60
|
-
const count = quads.length;
|
|
61
|
-
|
|
62
|
-
if (count === 0) {
|
|
63
|
-
return '0-empty';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Sample quads: first 100 + last 100 for quick validation
|
|
67
|
-
const sampleStart = Math.min(100, count);
|
|
68
|
-
const sampleEnd = Math.max(count - 100, sampleStart);
|
|
69
|
-
|
|
70
|
-
const sample = [...quads.slice(0, sampleStart), ...quads.slice(sampleEnd)];
|
|
71
|
-
|
|
72
|
-
// Hash the string representation of sampled quads
|
|
73
|
-
const sampleStr = sample
|
|
74
|
-
.map(q => `${q.subject.value}|${q.predicate.value}|${q.object.value}`)
|
|
75
|
-
.join('\n');
|
|
76
|
-
|
|
77
|
-
const sampleHash = quickHash(sampleStr);
|
|
78
|
-
|
|
79
|
-
return `${count}-${sampleHash}`;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Get cached Oxigraph store or create new one
|
|
84
|
-
*
|
|
85
|
-
* @param {Store} store - N3 Store instance
|
|
86
|
-
* @param {Function} createStore - Factory function to create Oxigraph store
|
|
87
|
-
* @returns {object} Oxigraph Store instance
|
|
88
|
-
*/
|
|
89
|
-
getOrCreate(store, createStore) {
|
|
90
|
-
const version = this.getVersion(store);
|
|
91
|
-
|
|
92
|
-
// Cache hit - return cached Oxigraph store
|
|
93
|
-
if (this.cache.has(version)) {
|
|
94
|
-
return this.cache.get(version);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Cache miss - convert ONCE
|
|
98
|
-
const quads = store.getQuads();
|
|
99
|
-
const oxStore = createStore(Array.from(quads));
|
|
100
|
-
|
|
101
|
-
// Store in cache
|
|
102
|
-
this.cache.set(version, oxStore);
|
|
103
|
-
this.currentVersion = version;
|
|
104
|
-
|
|
105
|
-
// LRU eviction - remove oldest entry if over capacity
|
|
106
|
-
if (this.cache.size > this.maxSize) {
|
|
107
|
-
const firstKey = this.cache.keys().next().value;
|
|
108
|
-
this.cache.delete(firstKey);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return oxStore;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Invalidate entire cache
|
|
116
|
-
* Call this when the store version changes (delta applied)
|
|
117
|
-
*/
|
|
118
|
-
clear() {
|
|
119
|
-
this.cache.clear();
|
|
120
|
-
this.currentVersion = null;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Get cache statistics
|
|
125
|
-
* @returns {object} Cache stats (size, maxSize, currentVersion)
|
|
126
|
-
*/
|
|
127
|
-
stats() {
|
|
128
|
-
return {
|
|
129
|
-
size: this.cache.size,
|
|
130
|
-
maxSize: this.maxSize,
|
|
131
|
-
currentVersion: this.currentVersion,
|
|
132
|
-
entries: Array.from(this.cache.keys()),
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export default StoreCache;
|
package/src/telemetry.mjs
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Batched OTEL Telemetry - Reduces span overhead
|
|
3
|
-
*
|
|
4
|
-
* @description
|
|
5
|
-
* Optimizes OpenTelemetry instrumentation to reduce span creation overhead:
|
|
6
|
-
* - Single parent span per transaction (not per hook)
|
|
7
|
-
* - Async attribute setting (non-blocking)
|
|
8
|
-
* - Conditional instrumentation (disable in production)
|
|
9
|
-
* - Batch flush for pending attributes
|
|
10
|
-
*
|
|
11
|
-
* Expected Impact: 10-15% latency reduction
|
|
12
|
-
*
|
|
13
|
-
* @module knowledge-engine/telemetry
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Batched OTEL Telemetry for Knowledge Hooks
|
|
18
|
-
*
|
|
19
|
-
* @class BatchedTelemetry
|
|
20
|
-
*/
|
|
21
|
-
export class BatchedTelemetry {
|
|
22
|
-
/**
|
|
23
|
-
* Create a new batched telemetry instance
|
|
24
|
-
* @param {object} tracer - OpenTelemetry tracer instance
|
|
25
|
-
* @param {object} options - Configuration options
|
|
26
|
-
* @param {boolean} options.enabled - Enable telemetry (default: true if not in production)
|
|
27
|
-
* @param {number} options.flushInterval - Batch flush interval in ms (default: 10ms)
|
|
28
|
-
*/
|
|
29
|
-
constructor(tracer, options = {}) {
|
|
30
|
-
this.tracer = tracer;
|
|
31
|
-
this.enabled = options.enabled ?? process.env.NODE_ENV !== 'production';
|
|
32
|
-
this.flushInterval = options.flushInterval ?? 10;
|
|
33
|
-
this.pendingAttributes = [];
|
|
34
|
-
this.flushTimeout = null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Start parent span for entire transaction (not per hook)
|
|
39
|
-
*
|
|
40
|
-
* @param {string} name - Span name
|
|
41
|
-
* @param {object} attributes - Initial attributes
|
|
42
|
-
* @returns {object|null} Span instance or null if disabled
|
|
43
|
-
*/
|
|
44
|
-
startTransactionSpan(name, attributes = {}) {
|
|
45
|
-
if (!this.enabled || !this.tracer) {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
return this.tracer.startSpan(name, {
|
|
51
|
-
attributes: {
|
|
52
|
-
'hook.transaction': true,
|
|
53
|
-
...attributes,
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
} catch {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Queue attribute update for batch flush (async, non-blocking)
|
|
63
|
-
*
|
|
64
|
-
* Attributes are queued and flushed in batch after flushInterval
|
|
65
|
-
* to avoid blocking the hot path with span attribute mutations.
|
|
66
|
-
*
|
|
67
|
-
* @param {object} span - Span instance
|
|
68
|
-
* @param {string} key - Attribute key
|
|
69
|
-
* @param {*} value - Attribute value
|
|
70
|
-
*/
|
|
71
|
-
setAttribute(span, key, value) {
|
|
72
|
-
if (!this.enabled || !span) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Queue attribute update
|
|
77
|
-
this.pendingAttributes.push({ span, key, value });
|
|
78
|
-
|
|
79
|
-
// Schedule batch flush if not already scheduled
|
|
80
|
-
if (!this.flushTimeout) {
|
|
81
|
-
this.flushTimeout = setTimeout(() => this.flush(), this.flushInterval);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Flush pending attributes in batch
|
|
87
|
-
*
|
|
88
|
-
* This reduces the number of span attribute mutations
|
|
89
|
-
* by batching them together rather than setting each individually.
|
|
90
|
-
*/
|
|
91
|
-
flush() {
|
|
92
|
-
try {
|
|
93
|
-
for (const { span, key, value } of this.pendingAttributes) {
|
|
94
|
-
try {
|
|
95
|
-
span.setAttribute(key, value);
|
|
96
|
-
} catch {
|
|
97
|
-
// Ignore individual attribute errors
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
} finally {
|
|
101
|
-
this.pendingAttributes = [];
|
|
102
|
-
this.flushTimeout = null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Record span event (batched)
|
|
108
|
-
*
|
|
109
|
-
* @param {object} span - Span instance
|
|
110
|
-
* @param {string} name - Event name
|
|
111
|
-
* @param {object} attributes - Event attributes
|
|
112
|
-
*/
|
|
113
|
-
recordEvent(span, name, attributes = {}) {
|
|
114
|
-
if (!this.enabled || !span) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
span.recordEvent(name, attributes);
|
|
120
|
-
} catch {
|
|
121
|
-
// Ignore recording errors
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* End span with status
|
|
127
|
-
*
|
|
128
|
-
* @param {object} span - Span instance
|
|
129
|
-
* @param {string} status - Status code ('ok', 'error', 'unset')
|
|
130
|
-
* @param {string} message - Status message (optional)
|
|
131
|
-
*/
|
|
132
|
-
endSpan(span, status = 'ok', message = '') {
|
|
133
|
-
if (!this.enabled || !span) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
// Flush any pending attributes first
|
|
139
|
-
if (this.pendingAttributes.length > 0) {
|
|
140
|
-
this.flush();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// End span with status
|
|
144
|
-
span.setStatus({ code: status, message });
|
|
145
|
-
span.end();
|
|
146
|
-
} catch {
|
|
147
|
-
// Ignore end errors
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Disable telemetry (for production or testing)
|
|
153
|
-
*/
|
|
154
|
-
disable() {
|
|
155
|
-
this.enabled = false;
|
|
156
|
-
this.flush();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Enable telemetry
|
|
161
|
-
*/
|
|
162
|
-
enable() {
|
|
163
|
-
this.enabled = true;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export default BatchedTelemetry;
|