@unrdf/knowledge-engine 5.0.1 → 26.4.2

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.
Files changed (71) hide show
  1. package/package.json +23 -17
  2. package/src/ai-enhanced-search.mjs +371 -0
  3. package/src/anomaly-detector.mjs +226 -0
  4. package/src/artifact-generator.mjs +252 -0
  5. package/src/browser.mjs +1 -1
  6. package/src/chatman/disruption-arithmetic.mjs +140 -0
  7. package/src/chatman/market-dynamics.mjs +140 -0
  8. package/src/chatman/organizational-dynamics.mjs +140 -0
  9. package/src/chatman/strategic-dynamics.mjs +140 -0
  10. package/src/chatman-config-loader.mjs +282 -0
  11. package/src/chatman-engine.mjs +435 -0
  12. package/src/chatman-operator.mjs +343 -0
  13. package/src/dark-field-detector.mjs +332 -0
  14. package/src/formation-theorems.mjs +345 -0
  15. package/src/index.mjs +20 -2
  16. package/src/knowledge-hook-manager.mjs +1 -1
  17. package/src/lockchain-writer-browser.mjs +2 -2
  18. package/src/observability.mjs +40 -4
  19. package/src/query-optimizer.mjs +1 -1
  20. package/src/resolution-layer.mjs +1 -1
  21. package/src/transaction.mjs +11 -9
  22. package/README.md +0 -84
  23. package/src/browser-shims.mjs +0 -343
  24. package/src/canonicalize.mjs +0 -414
  25. package/src/condition-cache.mjs +0 -109
  26. package/src/condition-evaluator.mjs +0 -722
  27. package/src/dark-matter-core.mjs +0 -742
  28. package/src/define-hook.mjs +0 -213
  29. package/src/effect-sandbox-browser.mjs +0 -283
  30. package/src/effect-sandbox-worker.mjs +0 -170
  31. package/src/effect-sandbox.mjs +0 -517
  32. package/src/engines/index.mjs +0 -11
  33. package/src/engines/rdf-engine.mjs +0 -299
  34. package/src/file-resolver.mjs +0 -387
  35. package/src/hook-executor-batching.mjs +0 -277
  36. package/src/hook-executor.mjs +0 -870
  37. package/src/hook-management.mjs +0 -150
  38. package/src/ken-parliment.mjs +0 -119
  39. package/src/ken.mjs +0 -149
  40. package/src/knowledge-engine/builtin-rules.mjs +0 -190
  41. package/src/knowledge-engine/inference-engine.mjs +0 -418
  42. package/src/knowledge-engine/knowledge-engine.mjs +0 -317
  43. package/src/knowledge-engine/pattern-dsl.mjs +0 -142
  44. package/src/knowledge-engine/pattern-matcher.mjs +0 -215
  45. package/src/knowledge-engine/rules.mjs +0 -184
  46. package/src/knowledge-engine.mjs +0 -319
  47. package/src/knowledge-hook-engine.mjs +0 -360
  48. package/src/knowledge-substrate-core.mjs +0 -927
  49. package/src/lite.mjs +0 -222
  50. package/src/lockchain-writer.mjs +0 -602
  51. package/src/monitoring/andon-signals.mjs +0 -775
  52. package/src/parse.mjs +0 -290
  53. package/src/performance-optimizer.mjs +0 -678
  54. package/src/policy-pack.mjs +0 -572
  55. package/src/query-cache.mjs +0 -116
  56. package/src/query.mjs +0 -306
  57. package/src/reason.mjs +0 -350
  58. package/src/schemas.mjs +0 -1063
  59. package/src/security/error-sanitizer.mjs +0 -257
  60. package/src/security/path-validator.mjs +0 -194
  61. package/src/security/sandbox-restrictions.mjs +0 -331
  62. package/src/security-validator.mjs +0 -389
  63. package/src/store-cache.mjs +0 -137
  64. package/src/telemetry.mjs +0 -167
  65. package/src/utils/adaptive-monitor.mjs +0 -746
  66. package/src/utils/circuit-breaker.mjs +0 -513
  67. package/src/utils/edge-case-handler.mjs +0 -503
  68. package/src/utils/memory-manager.mjs +0 -498
  69. package/src/utils/ring-buffer.mjs +0 -282
  70. package/src/validate.mjs +0 -319
  71. package/src/validators/index.mjs +0 -338
@@ -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;