@yugenlab/vaayu 0.1.10 → 0.1.12
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/chunks/{agentic-tool-loop-2FZK72JO.js → agentic-tool-loop-NQESOBLC.js} +2 -2
- package/chunks/akasha-5C5Q6NMP.js +12 -0
- package/chunks/{chunk-JGI4SDWS.js → chunk-26K6DS6N.js} +7 -5
- package/chunks/chunk-5E3ZS5SW.js +529 -0
- package/chunks/{chunk-VJHNE47S.js → chunk-ARZCIITZ.js} +104 -94
- package/chunks/{chunk-PRXQW76U.js → chunk-EG37M4QL.js} +17 -6
- package/chunks/{chunk-HIYHTWFW.js → chunk-FEDPZOZ5.js} +572 -441
- package/chunks/{chunk-O4KV7TFP.js → chunk-GWYC7R2L.js} +38 -20
- package/chunks/chunk-H46F2Y6R.js +134 -0
- package/chunks/{chunk-M7THR63C.js → chunk-HXHDP2PZ.js} +78 -65
- package/chunks/chunk-KVQH4LE7.js +396 -0
- package/chunks/{chunk-TND3MU4Z.js → chunk-LJCT7UYP.js} +86 -68
- package/chunks/{chunk-OT4G2L46.js → chunk-M2RLX5LU.js} +229 -163
- package/chunks/{chunk-IGKYKEKT.js → chunk-NAQKA54E.js} +8 -2
- package/chunks/{chunk-77725AR7.js → chunk-PZ4AQ22L.js} +151 -57
- package/chunks/{chunk-C76USAC5.js → chunk-QFGAB4XD.js} +13 -5
- package/chunks/{chunk-MJ74G5RB.js → chunk-R273KC7J.js} +276 -3
- package/chunks/{chunk-JAWZ7ANC.js → chunk-RVKTGKFD.js} +12 -8
- package/chunks/{chunk-AGK3A7R7.js → chunk-TSOQ2CT3.js} +1430 -1173
- package/chunks/{chunk-YJRXLRTE.js → chunk-VEZ2DI2M.js} +24 -12
- package/chunks/{chunk-N22M7D4P.js → chunk-XP3NIH5F.js} +91 -98
- package/chunks/{chunk-6556EKOB.js → chunk-Y6IZH6FT.js} +42 -26
- package/chunks/{chunk-AS3DJFY3.js → chunk-YRTGGYJU.js} +45 -41
- package/chunks/{consolidation-indexer-VKQ6DNU3.js → consolidation-indexer-KPXORCJ4.js} +9 -9
- package/chunks/database-BX3LVYXS.js +11 -0
- package/chunks/{day-consolidation-BH3QU2SZ.js → day-consolidation-CR3TJFAL.js} +5 -5
- package/chunks/{src-Y3TGMINC.js → dist-ESCM3CP5.js} +31 -21
- package/chunks/graphrag-73XA7LBX.js +14 -0
- package/chunks/hierarchical-temporal-search-GHKVKNZ6.js +8 -0
- package/chunks/hybrid-search-OD756RDV.js +20 -0
- package/chunks/{memory-store-A6WOWLWC.js → memory-store-4GCBR2DZ.js} +4 -4
- package/chunks/periodic-consolidation-IINCHP6L.js +11 -0
- package/chunks/postgres-YLCUNVPQ.js +8 -0
- package/chunks/recall-64RROTUC.js +21 -0
- package/chunks/search-JVCDNTAJ.js +19 -0
- package/chunks/{session-store-3BRPGC6P.js → session-store-3EDQZEDS.js} +12 -6
- package/chunks/{sqlite-DHUQGPR5.js → sqlite-4N7YH2KK.js} +3 -3
- package/chunks/{src-6GVZTUH6.js → src-OPSDZEFI.js} +2 -2
- package/chunks/{suncalc-NOHGYHDU.js → suncalc-RM7URNUR.js} +2 -2
- package/chunks/{tree-RSHKDTCR.js → tree-FIUVGJ5J.js} +2 -2
- package/chunks/{vasana-engine-MU25OQ23.js → vasana-engine-W4PYWT5H.js} +5 -5
- package/gateway.js +38859 -30650
- package/package.json +1 -1
- package/pair-cli.js +2 -2
- package/chunks/chunk-U62ABYKD.js +0 -123
- package/chunks/chunk-UZ6OIVEC.js +0 -198
- package/chunks/graphrag-D7OXWAWD.js +0 -14
- package/chunks/hierarchical-temporal-search-PVHVA3NZ.js +0 -8
- package/chunks/hybrid-search-G2NAJKJ7.js +0 -20
- package/chunks/periodic-consolidation-LMYMNS4Q.js +0 -11
- package/chunks/postgres-WLH3D5HG.js +0 -8
- package/chunks/recall-ZNL4DJ2L.js +0 -21
- package/chunks/search-35JMSGUT.js +0 -19
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {
|
|
2
|
+
__esm
|
|
3
|
+
} from "./chunk-NAQKA54E.js";
|
|
4
|
+
|
|
5
|
+
// ../chitragupta/packages/smriti/dist/db/schema.js
|
|
5
6
|
function initAllSchemas(dbm) {
|
|
6
7
|
initAgentSchema(dbm);
|
|
7
8
|
initGraphSchema(dbm);
|
|
@@ -10,7 +11,8 @@ function initAllSchemas(dbm) {
|
|
|
10
11
|
function initAgentSchema(dbm) {
|
|
11
12
|
const db = dbm.get("agent");
|
|
12
13
|
const currentVersion = getSchemaVersion(db, "agent");
|
|
13
|
-
if (currentVersion >= AGENT_SCHEMA_VERSION)
|
|
14
|
+
if (currentVersion >= AGENT_SCHEMA_VERSION)
|
|
15
|
+
return;
|
|
14
16
|
db.exec(`
|
|
15
17
|
-- \u2500\u2500\u2500 Sessions \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
16
18
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
@@ -243,7 +245,8 @@ function initAgentSchema(dbm) {
|
|
|
243
245
|
function initGraphSchema(dbm) {
|
|
244
246
|
const db = dbm.get("graph");
|
|
245
247
|
const currentVersion = getSchemaVersion(db, "graph");
|
|
246
|
-
if (currentVersion >= GRAPH_SCHEMA_VERSION)
|
|
248
|
+
if (currentVersion >= GRAPH_SCHEMA_VERSION)
|
|
249
|
+
return;
|
|
247
250
|
db.exec(`
|
|
248
251
|
-- \u2500\u2500\u2500 Nodes \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
249
252
|
CREATE TABLE IF NOT EXISTS nodes (
|
|
@@ -290,7 +293,8 @@ function initGraphSchema(dbm) {
|
|
|
290
293
|
function initVectorsSchema(dbm) {
|
|
291
294
|
const db = dbm.get("vectors");
|
|
292
295
|
const currentVersion = getSchemaVersion(db, "vectors");
|
|
293
|
-
if (currentVersion >= VECTORS_SCHEMA_VERSION)
|
|
296
|
+
if (currentVersion >= VECTORS_SCHEMA_VERSION)
|
|
297
|
+
return;
|
|
294
298
|
db.exec(`
|
|
295
299
|
-- \u2500\u2500\u2500 Embeddings \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
296
300
|
CREATE TABLE IF NOT EXISTS embeddings (
|
|
@@ -323,15 +327,23 @@ function getSchemaVersion(db, name) {
|
|
|
323
327
|
}
|
|
324
328
|
function setSchemaVersion(db, name, version) {
|
|
325
329
|
ensureVersionTable(db);
|
|
326
|
-
db.prepare(
|
|
327
|
-
"INSERT OR REPLACE INTO _schema_versions (name, version) VALUES (?, ?)"
|
|
328
|
-
).run(name, version);
|
|
330
|
+
db.prepare("INSERT OR REPLACE INTO _schema_versions (name, version) VALUES (?, ?)").run(name, version);
|
|
329
331
|
}
|
|
332
|
+
var AGENT_SCHEMA_VERSION, GRAPH_SCHEMA_VERSION, VECTORS_SCHEMA_VERSION;
|
|
333
|
+
var init_schema = __esm({
|
|
334
|
+
"../chitragupta/packages/smriti/dist/db/schema.js"() {
|
|
335
|
+
"use strict";
|
|
336
|
+
AGENT_SCHEMA_VERSION = 4;
|
|
337
|
+
GRAPH_SCHEMA_VERSION = 1;
|
|
338
|
+
VECTORS_SCHEMA_VERSION = 1;
|
|
339
|
+
}
|
|
340
|
+
});
|
|
330
341
|
|
|
331
342
|
export {
|
|
332
343
|
initAllSchemas,
|
|
333
344
|
initAgentSchema,
|
|
334
345
|
initGraphSchema,
|
|
335
|
-
initVectorsSchema
|
|
346
|
+
initVectorsSchema,
|
|
347
|
+
init_schema
|
|
336
348
|
};
|
|
337
|
-
//# sourceMappingURL=chunk-
|
|
349
|
+
//# sourceMappingURL=chunk-VEZ2DI2M.js.map
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
DatabaseManager
|
|
3
|
-
|
|
2
|
+
DatabaseManager,
|
|
3
|
+
init_database
|
|
4
|
+
} from "./chunk-H46F2Y6R.js";
|
|
4
5
|
|
|
5
|
-
// ../chitragupta/packages/smriti/
|
|
6
|
+
// ../chitragupta/packages/smriti/dist/vasana-engine.js
|
|
7
|
+
init_database();
|
|
8
|
+
|
|
9
|
+
// ../chitragupta/packages/smriti/dist/vasana-bocpd.js
|
|
6
10
|
var FNV_OFFSET = 2166136261;
|
|
7
11
|
var FNV_PRIME = 16777619;
|
|
8
12
|
function fnv1a(input) {
|
|
@@ -35,11 +39,14 @@ var HARD_CEILINGS = {
|
|
|
35
39
|
stabilityWindow: 100
|
|
36
40
|
};
|
|
37
41
|
function logsumexp(xs) {
|
|
38
|
-
if (xs.length === 0)
|
|
42
|
+
if (xs.length === 0)
|
|
43
|
+
return -Infinity;
|
|
39
44
|
const m = Math.max(...xs);
|
|
40
|
-
if (m === -Infinity)
|
|
45
|
+
if (m === -Infinity)
|
|
46
|
+
return -Infinity;
|
|
41
47
|
let s = 0;
|
|
42
|
-
for (let i = 0; i < xs.length; i++)
|
|
48
|
+
for (let i = 0; i < xs.length; i++)
|
|
49
|
+
s += Math.exp(xs[i] - m);
|
|
43
50
|
return m + Math.log(s);
|
|
44
51
|
}
|
|
45
52
|
function lgamma(z) {
|
|
@@ -54,10 +61,12 @@ function lgamma(z) {
|
|
|
54
61
|
9984369578019572e-21,
|
|
55
62
|
15056327351493116e-23
|
|
56
63
|
];
|
|
57
|
-
if (z < 0.5)
|
|
64
|
+
if (z < 0.5)
|
|
65
|
+
return Math.log(Math.PI / Math.sin(Math.PI * z)) - lgamma(1 - z);
|
|
58
66
|
z -= 1;
|
|
59
67
|
let x = c[0];
|
|
60
|
-
for (let i = 1; i < 9; i++)
|
|
68
|
+
for (let i = 1; i < 9; i++)
|
|
69
|
+
x += c[i] / (z + i);
|
|
61
70
|
const t = z + 7.5;
|
|
62
71
|
return 0.5 * Math.log(2 * Math.PI) + (z + 0.5) * Math.log(t) - t + Math.log(x);
|
|
63
72
|
}
|
|
@@ -98,7 +107,8 @@ function updateBOCPD(st, x, cfg) {
|
|
|
98
107
|
}
|
|
99
108
|
newLR[0] = logsumexp(cpTerms);
|
|
100
109
|
const logZ = logsumexp(newLR);
|
|
101
|
-
for (let i = 0; i <= n; i++)
|
|
110
|
+
for (let i = 0; i <= n; i++)
|
|
111
|
+
newLR[i] -= logZ;
|
|
102
112
|
const newS = new Array(n + 1);
|
|
103
113
|
newS[0] = {
|
|
104
114
|
mu: cfg.priorMu,
|
|
@@ -121,12 +131,14 @@ function updateBOCPD(st, x, cfg) {
|
|
|
121
131
|
const idx = newLR.map((lp, i) => ({ lp, i })).sort((a, b) => b.lp - a.lp);
|
|
122
132
|
const keep = new Set(idx.slice(0, cfg.maxRunLength).map((e) => e.i));
|
|
123
133
|
const pR = [], pS = [];
|
|
124
|
-
for (let i = 0; i <= n; i++)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
134
|
+
for (let i = 0; i <= n; i++)
|
|
135
|
+
if (keep.has(i)) {
|
|
136
|
+
pR.push(newLR[i]);
|
|
137
|
+
pS.push(newS[i]);
|
|
138
|
+
}
|
|
128
139
|
const norm = logsumexp(pR);
|
|
129
|
-
for (let i = 0; i < pR.length; i++)
|
|
140
|
+
for (let i = 0; i < pR.length; i++)
|
|
141
|
+
pR[i] -= norm;
|
|
130
142
|
st.logR = pR;
|
|
131
143
|
st.stats = pS;
|
|
132
144
|
} else {
|
|
@@ -135,7 +147,8 @@ function updateBOCPD(st, x, cfg) {
|
|
|
135
147
|
}
|
|
136
148
|
st.totalObs++;
|
|
137
149
|
const cpProb = st.logR.length > 0 ? Math.exp(st.logR[0]) : 0;
|
|
138
|
-
if (!st.recentCpProbs)
|
|
150
|
+
if (!st.recentCpProbs)
|
|
151
|
+
st.recentCpProbs = [];
|
|
139
152
|
st.recentCpProbs.push(cpProb);
|
|
140
153
|
if (st.recentCpProbs.length > cfg.anomalyRevertWindow) {
|
|
141
154
|
st.recentCpProbs.shift();
|
|
@@ -150,13 +163,11 @@ function serializeBOCPD(states, obs) {
|
|
|
150
163
|
function deserializeBOCPD(raw) {
|
|
151
164
|
return {
|
|
152
165
|
states: new Map(Object.entries(raw.features)),
|
|
153
|
-
obs: new Map(
|
|
154
|
-
Object.entries(raw.observations).map(([k, v]) => [k, Array.isArray(v) ? v : []])
|
|
155
|
-
)
|
|
166
|
+
obs: new Map(Object.entries(raw.observations).map(([k, v]) => [k, Array.isArray(v) ? v : []]))
|
|
156
167
|
};
|
|
157
168
|
}
|
|
158
169
|
|
|
159
|
-
// ../chitragupta/packages/smriti/
|
|
170
|
+
// ../chitragupta/packages/smriti/dist/vasana-engine.js
|
|
160
171
|
var VasanaEngine = class {
|
|
161
172
|
cfg;
|
|
162
173
|
states = /* @__PURE__ */ new Map();
|
|
@@ -175,10 +186,12 @@ var VasanaEngine = class {
|
|
|
175
186
|
/** Observe a samskara: extract features and update BOCPD state per dimension. */
|
|
176
187
|
observe(samskara) {
|
|
177
188
|
for (const [feat, val] of this.extractFeatures(samskara)) {
|
|
178
|
-
if (!this.states.has(feat))
|
|
189
|
+
if (!this.states.has(feat))
|
|
190
|
+
this.states.set(feat, initBOCPD(this.cfg));
|
|
179
191
|
const buf = this.obs.get(feat) ?? [];
|
|
180
192
|
buf.push(val);
|
|
181
|
-
if (buf.length > this.cfg.windowSize)
|
|
193
|
+
if (buf.length > this.cfg.windowSize)
|
|
194
|
+
buf.splice(0, buf.length - this.cfg.windowSize);
|
|
182
195
|
this.obs.set(feat, buf);
|
|
183
196
|
updateBOCPD(this.states.get(feat), val, this.cfg);
|
|
184
197
|
}
|
|
@@ -188,12 +201,10 @@ var VasanaEngine = class {
|
|
|
188
201
|
const now = Date.now();
|
|
189
202
|
const res = { created: [], reinforced: [], pending: [], changePoints: [], anomalies: [], timestamp: now };
|
|
190
203
|
const db = DatabaseManager.instance().get("agent");
|
|
191
|
-
const rows = db.prepare(
|
|
192
|
-
`SELECT id, session_id, pattern_type, pattern_content, observation_count,
|
|
204
|
+
const rows = db.prepare(`SELECT id, session_id, pattern_type, pattern_content, observation_count,
|
|
193
205
|
confidence, pramana_type, project, created_at, updated_at
|
|
194
206
|
FROM samskaras WHERE project = ? OR project IS NULL
|
|
195
|
-
ORDER BY updated_at DESC LIMIT ?`
|
|
196
|
-
).all(project, this.cfg.windowSize * 10);
|
|
207
|
+
ORDER BY updated_at DESC LIMIT ?`).all(project, this.cfg.windowSize * 10);
|
|
197
208
|
for (const [key, ids] of this.clusterSamskaras(rows)) {
|
|
198
209
|
const feat = `cluster:${key}`;
|
|
199
210
|
const st = this.states.get(feat);
|
|
@@ -265,7 +276,8 @@ var VasanaEngine = class {
|
|
|
265
276
|
/** Reinforce with diminishing returns: delta = 0.1 / (1 + ln(1 + count)). */
|
|
266
277
|
reinforce(vasanaId) {
|
|
267
278
|
const v = this.loadVasana(vasanaId);
|
|
268
|
-
if (!v)
|
|
279
|
+
if (!v)
|
|
280
|
+
return;
|
|
269
281
|
v.strength = Math.min(1, v.strength + 0.1 / (1 + Math.log(1 + v.reinforcementCount)));
|
|
270
282
|
v.reinforcementCount++;
|
|
271
283
|
v.lastActivated = v.updatedAt = Date.now();
|
|
@@ -274,19 +286,18 @@ var VasanaEngine = class {
|
|
|
274
286
|
/** Weaken by fixed decrement. */
|
|
275
287
|
weaken(vasanaId) {
|
|
276
288
|
const v = this.loadVasana(vasanaId);
|
|
277
|
-
if (!v)
|
|
289
|
+
if (!v)
|
|
290
|
+
return;
|
|
278
291
|
v.strength = Math.max(0, v.strength - 0.15);
|
|
279
292
|
v.updatedAt = Date.now();
|
|
280
293
|
this.saveVasana(v);
|
|
281
294
|
}
|
|
282
295
|
/** Get vasanas for a project (includes global), sorted by strength desc. */
|
|
283
296
|
getVasanas(project, topK = 20) {
|
|
284
|
-
const rows = DatabaseManager.instance().get("agent").prepare(
|
|
285
|
-
`SELECT id,name,description,valence,strength,stability,source_samskaras,
|
|
297
|
+
const rows = DatabaseManager.instance().get("agent").prepare(`SELECT id,name,description,valence,strength,stability,source_samskaras,
|
|
286
298
|
project,created_at,updated_at,last_activated,activation_count
|
|
287
299
|
FROM vasanas WHERE project=? OR project IS NULL OR project='__global__'
|
|
288
|
-
ORDER BY strength DESC LIMIT ?`
|
|
289
|
-
).all(project, topK);
|
|
300
|
+
ORDER BY strength DESC LIMIT ?`).all(project, topK);
|
|
290
301
|
return rows.map((r) => this.toVasana(r));
|
|
291
302
|
}
|
|
292
303
|
/** Promote project vasanas to global when found in >= promotionMinProjects. */
|
|
@@ -294,12 +305,10 @@ var VasanaEngine = class {
|
|
|
294
305
|
const now = Date.now();
|
|
295
306
|
const res = { promoted: [], projectSources: {}, timestamp: now };
|
|
296
307
|
const db = DatabaseManager.instance().get("agent");
|
|
297
|
-
const rows = db.prepare(
|
|
298
|
-
`SELECT id,name,description,valence,strength,stability,source_samskaras,
|
|
308
|
+
const rows = db.prepare(`SELECT id,name,description,valence,strength,stability,source_samskaras,
|
|
299
309
|
project,created_at,updated_at,last_activated,activation_count
|
|
300
310
|
FROM vasanas WHERE project IS NOT NULL AND project!='__global__' AND strength>=0.4
|
|
301
|
-
ORDER BY name`
|
|
302
|
-
).all();
|
|
311
|
+
ORDER BY name`).all();
|
|
303
312
|
const byName = /* @__PURE__ */ new Map();
|
|
304
313
|
for (const r of rows) {
|
|
305
314
|
const k = r.name.toLowerCase();
|
|
@@ -307,12 +316,16 @@ var VasanaEngine = class {
|
|
|
307
316
|
}
|
|
308
317
|
for (const [tendency, group] of byName) {
|
|
309
318
|
const projects = new Set(group.map((r) => r.project));
|
|
310
|
-
if (projects.size < this.cfg.promotionMinProjects)
|
|
311
|
-
|
|
319
|
+
if (projects.size < this.cfg.promotionMinProjects)
|
|
320
|
+
continue;
|
|
321
|
+
if (db.prepare(`SELECT 1 FROM vasanas WHERE name=? AND (project IS NULL OR project='__global__')`).get(tendency))
|
|
322
|
+
continue;
|
|
312
323
|
const allSrc = [];
|
|
313
|
-
for (const r of group)
|
|
324
|
+
for (const r of group)
|
|
325
|
+
allSrc.push(...jsonArr(r.source_samskaras));
|
|
314
326
|
const votes = { positive: 0, negative: 0, neutral: 0 };
|
|
315
|
-
for (const r of group)
|
|
327
|
+
for (const r of group)
|
|
328
|
+
votes[r.valence]++;
|
|
316
329
|
const valence = ["positive", "negative", "neutral"].reduce((a, b) => votes[a] >= votes[b] ? a : b);
|
|
317
330
|
const gv = {
|
|
318
331
|
id: fnv1a(tendency + ":__global__"),
|
|
@@ -344,32 +357,31 @@ var VasanaEngine = class {
|
|
|
344
357
|
let deleted = 0;
|
|
345
358
|
for (const r of rows) {
|
|
346
359
|
const elapsed = now - (r.last_activated ?? now);
|
|
347
|
-
if (elapsed <= 0)
|
|
360
|
+
if (elapsed <= 0)
|
|
361
|
+
continue;
|
|
348
362
|
const s = r.strength * Math.exp(-Math.LN2 * elapsed / hl);
|
|
349
363
|
if (s < 0.01) {
|
|
350
364
|
db.prepare(`DELETE FROM vasanas WHERE id=?`).run(r.id);
|
|
351
365
|
deleted++;
|
|
352
|
-
} else
|
|
366
|
+
} else
|
|
367
|
+
db.prepare(`UPDATE vasanas SET strength=?,updated_at=? WHERE id=?`).run(s, now, r.id);
|
|
353
368
|
}
|
|
354
369
|
return deleted;
|
|
355
370
|
}
|
|
356
371
|
/** Persist BOCPD state (run-length distributions + observations) to SQLite. */
|
|
357
372
|
persist() {
|
|
358
373
|
const json = JSON.stringify(serializeBOCPD(this.states, this.obs));
|
|
359
|
-
DatabaseManager.instance().get("agent").prepare(
|
|
360
|
-
`INSERT OR REPLACE INTO consolidation_rules
|
|
374
|
+
DatabaseManager.instance().get("agent").prepare(`INSERT OR REPLACE INTO consolidation_rules
|
|
361
375
|
(id, category, rule_text, confidence, source_sessions, created_at, updated_at, hit_count, project)
|
|
362
376
|
VALUES ((SELECT id FROM consolidation_rules WHERE category='bocpd_state' AND project='__vasana_engine__'),
|
|
363
|
-
'bocpd_state',?,1.0,NULL,?,?,1,'__vasana_engine__')`
|
|
364
|
-
).run(json, Date.now(), Date.now());
|
|
377
|
+
'bocpd_state',?,1.0,NULL,?,?,1,'__vasana_engine__')`).run(json, Date.now(), Date.now());
|
|
365
378
|
}
|
|
366
379
|
/** Restore BOCPD state from SQLite. No-op if nothing persisted. */
|
|
367
380
|
restore() {
|
|
368
|
-
const row = DatabaseManager.instance().get("agent").prepare(
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
if (!row) return;
|
|
381
|
+
const row = DatabaseManager.instance().get("agent").prepare(`SELECT rule_text FROM consolidation_rules
|
|
382
|
+
WHERE category='bocpd_state' AND project='__vasana_engine__' LIMIT 1`).get();
|
|
383
|
+
if (!row)
|
|
384
|
+
return;
|
|
373
385
|
try {
|
|
374
386
|
const raw = JSON.parse(row.rule_text);
|
|
375
387
|
const { states, obs } = deserializeBOCPD(raw);
|
|
@@ -392,9 +404,11 @@ var VasanaEngine = class {
|
|
|
392
404
|
* - Otherwise -> stable
|
|
393
405
|
*/
|
|
394
406
|
classifyDeviation(st) {
|
|
395
|
-
if (st.logR.length === 0)
|
|
407
|
+
if (st.logR.length === 0)
|
|
408
|
+
return "stable";
|
|
396
409
|
const cpProb = Math.exp(st.logR[0]);
|
|
397
|
-
if (cpProb <= this.cfg.changePointThreshold)
|
|
410
|
+
if (cpProb <= this.cfg.changePointThreshold)
|
|
411
|
+
return "stable";
|
|
398
412
|
const recent = st.recentCpProbs ?? [cpProb];
|
|
399
413
|
const exceedCount = recent.filter((p) => p > this.cfg.changePointThreshold).length;
|
|
400
414
|
const ratio = exceedCount / Math.max(1, recent.length);
|
|
@@ -404,7 +418,8 @@ var VasanaEngine = class {
|
|
|
404
418
|
return "anomaly";
|
|
405
419
|
}
|
|
406
420
|
stabilityScore(st) {
|
|
407
|
-
if (st.logR.length === 0)
|
|
421
|
+
if (st.logR.length === 0)
|
|
422
|
+
return 0;
|
|
408
423
|
return Math.max(0, Math.min(1, 1 - Math.exp(st.logR[0])));
|
|
409
424
|
}
|
|
410
425
|
// ── Feature Extraction & Validation ──────────────────────────────────
|
|
@@ -426,25 +441,31 @@ var VasanaEngine = class {
|
|
|
426
441
|
}
|
|
427
442
|
/** 70/30 holdout: fraction of test points within 1.5 sigma of train mean. */
|
|
428
443
|
holdoutValidation(obs) {
|
|
429
|
-
if (obs.length < 4)
|
|
444
|
+
if (obs.length < 4)
|
|
445
|
+
return 0;
|
|
430
446
|
const si = Math.floor(obs.length * this.cfg.holdoutTrainRatio);
|
|
431
447
|
const train = obs.slice(0, si), test = obs.slice(si);
|
|
432
|
-
if (!train.length || !test.length)
|
|
448
|
+
if (!train.length || !test.length)
|
|
449
|
+
return 0;
|
|
433
450
|
const mu = train.reduce((a, b) => a + b, 0) / train.length;
|
|
434
451
|
const std = Math.sqrt(train.reduce((a, v) => a + (v - mu) ** 2, 0) / train.length + 1e-10);
|
|
435
452
|
let ok = 0;
|
|
436
|
-
for (const v of test)
|
|
453
|
+
for (const v of test)
|
|
454
|
+
if (Math.abs(v - mu) <= 1.5 * std)
|
|
455
|
+
ok++;
|
|
437
456
|
return ok / test.length;
|
|
438
457
|
}
|
|
439
458
|
assignValence(rows) {
|
|
440
459
|
let pos = 0, neg = 0;
|
|
441
460
|
for (const r of rows) {
|
|
442
|
-
if (r.pattern_type === "correction")
|
|
461
|
+
if (r.pattern_type === "correction")
|
|
462
|
+
neg += r.confidence;
|
|
443
463
|
else if (r.pattern_type !== "tool-sequence" || r.confidence > 0.6)
|
|
444
464
|
pos += r.confidence * (r.pattern_type === "tool-sequence" ? 0.5 : 1);
|
|
445
465
|
}
|
|
446
466
|
const tot = pos + neg;
|
|
447
|
-
if (tot < 0.1)
|
|
467
|
+
if (tot < 0.1)
|
|
468
|
+
return "neutral";
|
|
448
469
|
return pos / tot > 0.6 ? "positive" : neg / tot > 0.6 ? "negative" : "neutral";
|
|
449
470
|
}
|
|
450
471
|
clusterSamskaras(rows) {
|
|
@@ -462,50 +483,21 @@ var VasanaEngine = class {
|
|
|
462
483
|
// reinforcementCount->activation_count, lastActivated->last_activated
|
|
463
484
|
saveVasana(v) {
|
|
464
485
|
const db = DatabaseManager.instance().get("agent");
|
|
465
|
-
const existing = db.prepare(
|
|
466
|
-
`SELECT id FROM vasanas WHERE name=? AND (project=? OR (project IS NULL AND ?='__global__'))`
|
|
467
|
-
).get(v.tendency, v.project, v.project);
|
|
486
|
+
const existing = db.prepare(`SELECT id FROM vasanas WHERE name=? AND (project=? OR (project IS NULL AND ?='__global__'))`).get(v.tendency, v.project, v.project);
|
|
468
487
|
if (existing) {
|
|
469
|
-
db.prepare(
|
|
470
|
-
|
|
471
|
-
source_samskaras=?,updated_at=?,last_activated=?,activation_count=? WHERE id=?`
|
|
472
|
-
).run(
|
|
473
|
-
v.description,
|
|
474
|
-
v.valence,
|
|
475
|
-
v.strength,
|
|
476
|
-
v.stability,
|
|
477
|
-
JSON.stringify(v.sourceSamskaras),
|
|
478
|
-
v.updatedAt,
|
|
479
|
-
v.lastActivated,
|
|
480
|
-
v.reinforcementCount,
|
|
481
|
-
existing.id
|
|
482
|
-
);
|
|
488
|
+
db.prepare(`UPDATE vasanas SET description=?,valence=?,strength=?,stability=?,
|
|
489
|
+
source_samskaras=?,updated_at=?,last_activated=?,activation_count=? WHERE id=?`).run(v.description, v.valence, v.strength, v.stability, JSON.stringify(v.sourceSamskaras), v.updatedAt, v.lastActivated, v.reinforcementCount, existing.id);
|
|
483
490
|
} else {
|
|
484
|
-
db.prepare(
|
|
485
|
-
|
|
486
|
-
project,created_at,updated_at,last_activated,activation_count) VALUES (?,?,?,?,?,?,?,?,?,?,?)`
|
|
487
|
-
).run(
|
|
488
|
-
v.tendency,
|
|
489
|
-
v.description,
|
|
490
|
-
v.valence,
|
|
491
|
-
v.strength,
|
|
492
|
-
v.stability,
|
|
493
|
-
JSON.stringify(v.sourceSamskaras),
|
|
494
|
-
v.project === "__global__" ? null : v.project,
|
|
495
|
-
v.createdAt,
|
|
496
|
-
v.updatedAt,
|
|
497
|
-
v.lastActivated,
|
|
498
|
-
v.reinforcementCount
|
|
499
|
-
);
|
|
491
|
+
db.prepare(`INSERT INTO vasanas (name,description,valence,strength,stability,source_samskaras,
|
|
492
|
+
project,created_at,updated_at,last_activated,activation_count) VALUES (?,?,?,?,?,?,?,?,?,?,?)`).run(v.tendency, v.description, v.valence, v.strength, v.stability, JSON.stringify(v.sourceSamskaras), v.project === "__global__" ? null : v.project, v.createdAt, v.updatedAt, v.lastActivated, v.reinforcementCount);
|
|
500
493
|
}
|
|
501
494
|
this.cache.set(v.id, v);
|
|
502
495
|
}
|
|
503
496
|
loadVasana(vid) {
|
|
504
|
-
if (this.cache.has(vid))
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
project,created_at,updated_at,last_activated,activation_count FROM vasanas`
|
|
508
|
-
).all();
|
|
497
|
+
if (this.cache.has(vid))
|
|
498
|
+
return this.cache.get(vid);
|
|
499
|
+
const rows = DatabaseManager.instance().get("agent").prepare(`SELECT id,name,description,valence,strength,stability,source_samskaras,
|
|
500
|
+
project,created_at,updated_at,last_activated,activation_count FROM vasanas`).all();
|
|
509
501
|
for (const r of rows) {
|
|
510
502
|
const v = this.toVasana(r);
|
|
511
503
|
this.cache.set(v.id, v);
|
|
@@ -532,7 +524,8 @@ var VasanaEngine = class {
|
|
|
532
524
|
}
|
|
533
525
|
};
|
|
534
526
|
function jsonArr(s) {
|
|
535
|
-
if (!s)
|
|
527
|
+
if (!s)
|
|
528
|
+
return [];
|
|
536
529
|
try {
|
|
537
530
|
const p = JSON.parse(s);
|
|
538
531
|
return Array.isArray(p) ? p : [];
|
|
@@ -554,4 +547,4 @@ export {
|
|
|
554
547
|
deserializeBOCPD,
|
|
555
548
|
VasanaEngine
|
|
556
549
|
};
|
|
557
|
-
//# sourceMappingURL=chunk-
|
|
550
|
+
//# sourceMappingURL=chunk-XP3NIH5F.js.map
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MemoryError,
|
|
3
|
-
getChitraguptaHome
|
|
4
|
-
|
|
3
|
+
getChitraguptaHome,
|
|
4
|
+
init_dist
|
|
5
|
+
} from "./chunk-KVQH4LE7.js";
|
|
5
6
|
|
|
6
|
-
// ../chitragupta/packages/smriti/
|
|
7
|
+
// ../chitragupta/packages/smriti/dist/memory-store.js
|
|
8
|
+
init_dist();
|
|
7
9
|
import fs from "fs";
|
|
8
10
|
import path from "path";
|
|
9
11
|
import crypto from "crypto";
|
|
@@ -44,30 +46,39 @@ function scopeKey(scope) {
|
|
|
44
46
|
return `session:${scope.sessionId}`;
|
|
45
47
|
}
|
|
46
48
|
}
|
|
49
|
+
function normalizeForDedupe(value) {
|
|
50
|
+
return value.toLowerCase().replace(/\*[^*]+\*/g, " ").replace(/[^\p{L}\p{N}\s\[\]:._/-]/gu, " ").replace(/\s+/g, " ").trim();
|
|
51
|
+
}
|
|
52
|
+
function hasDuplicateEntry(existing, entry) {
|
|
53
|
+
const needle = normalizeForDedupe(entry);
|
|
54
|
+
if (!needle || needle.length < 8)
|
|
55
|
+
return false;
|
|
56
|
+
const haystack = normalizeForDedupe(existing.slice(-2e5));
|
|
57
|
+
return haystack.includes(needle);
|
|
58
|
+
}
|
|
47
59
|
function getMemory(scope) {
|
|
48
60
|
if (scope.type === "session") {
|
|
49
|
-
throw new MemoryError(
|
|
50
|
-
"Session memory is stored within the session file. Use loadSession() to access it."
|
|
51
|
-
);
|
|
61
|
+
throw new MemoryError("Session memory is stored within the session file. Use loadSession() to access it.");
|
|
52
62
|
}
|
|
53
63
|
const filePath = resolveMemoryPath(scope);
|
|
54
|
-
if (!filePath)
|
|
64
|
+
if (!filePath)
|
|
65
|
+
return "";
|
|
55
66
|
try {
|
|
56
67
|
return fs.readFileSync(filePath, "utf-8");
|
|
57
68
|
} catch (err) {
|
|
58
69
|
const isNotFound = err.code === "ENOENT" || err instanceof Error && err.message.includes("ENOENT");
|
|
59
|
-
if (isNotFound)
|
|
70
|
+
if (isNotFound)
|
|
71
|
+
return "";
|
|
60
72
|
throw new MemoryError(`Failed to read memory at ${filePath}: ${err}`);
|
|
61
73
|
}
|
|
62
74
|
}
|
|
63
75
|
function updateMemory(scope, content) {
|
|
64
76
|
if (scope.type === "session") {
|
|
65
|
-
throw new MemoryError(
|
|
66
|
-
"Session memory is stored within the session file. Use saveSession() to update it."
|
|
67
|
-
);
|
|
77
|
+
throw new MemoryError("Session memory is stored within the session file. Use saveSession() to update it.");
|
|
68
78
|
}
|
|
69
79
|
const filePath = resolveMemoryPath(scope);
|
|
70
|
-
if (!filePath)
|
|
80
|
+
if (!filePath)
|
|
81
|
+
return Promise.resolve();
|
|
71
82
|
const key = scopeKey(scope);
|
|
72
83
|
const prev = memoryWriteQueues.get(key) ?? Promise.resolve();
|
|
73
84
|
const next = prev.then(() => {
|
|
@@ -85,14 +96,13 @@ function updateMemory(scope, content) {
|
|
|
85
96
|
memoryWriteQueues.set(key, next);
|
|
86
97
|
return next;
|
|
87
98
|
}
|
|
88
|
-
function appendMemory(scope, entry) {
|
|
99
|
+
function appendMemory(scope, entry, options) {
|
|
89
100
|
if (scope.type === "session") {
|
|
90
|
-
throw new MemoryError(
|
|
91
|
-
"Session memory is stored within the session file. Use addTurn() to append."
|
|
92
|
-
);
|
|
101
|
+
throw new MemoryError("Session memory is stored within the session file. Use addTurn() to append.");
|
|
93
102
|
}
|
|
94
103
|
const filePath = resolveMemoryPath(scope);
|
|
95
|
-
if (!filePath)
|
|
104
|
+
if (!filePath)
|
|
105
|
+
return Promise.resolve();
|
|
96
106
|
const key = scopeKey(scope);
|
|
97
107
|
const prev = memoryWriteQueues.get(key) ?? Promise.resolve();
|
|
98
108
|
const next = prev.then(() => {
|
|
@@ -111,9 +121,13 @@ ${entry}
|
|
|
111
121
|
existing = fs.readFileSync(filePath, "utf-8");
|
|
112
122
|
} catch (readErr) {
|
|
113
123
|
const isNotFound = readErr.code === "ENOENT" || readErr instanceof Error && readErr.message.includes("ENOENT");
|
|
114
|
-
if (!isNotFound)
|
|
124
|
+
if (!isNotFound)
|
|
125
|
+
throw readErr;
|
|
115
126
|
}
|
|
116
127
|
if (existing !== null) {
|
|
128
|
+
if (options?.dedupe && hasDuplicateEntry(existing, entry)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
117
131
|
const totalSize = Buffer.byteLength(existing, "utf-8") + Buffer.byteLength(formatted, "utf-8");
|
|
118
132
|
if (totalSize > MAX_MEMORY_SIZE) {
|
|
119
133
|
const result = truncateToFit(existing, formatted);
|
|
@@ -126,7 +140,8 @@ ${entry}
|
|
|
126
140
|
fs.writeFileSync(filePath, header + formatted, "utf-8");
|
|
127
141
|
}
|
|
128
142
|
} catch (err) {
|
|
129
|
-
if (err instanceof MemoryError)
|
|
143
|
+
if (err instanceof MemoryError)
|
|
144
|
+
throw err;
|
|
130
145
|
throw new MemoryError(`Failed to append memory at ${filePath}: ${err}`);
|
|
131
146
|
}
|
|
132
147
|
}).finally(() => {
|
|
@@ -146,7 +161,8 @@ function truncateToFit(existing, incoming) {
|
|
|
146
161
|
let size = Buffer.byteLength(header, "utf-8");
|
|
147
162
|
for (let i = segments.length - 1; i >= 1; i--) {
|
|
148
163
|
const segSize = Buffer.byteLength(ENTRY_SEPARATOR + segments[i], "utf-8");
|
|
149
|
-
if (size + segSize > budget)
|
|
164
|
+
if (size + segSize > budget)
|
|
165
|
+
break;
|
|
150
166
|
size += segSize;
|
|
151
167
|
kept.unshift(segments[i]);
|
|
152
168
|
}
|
|
@@ -155,18 +171,18 @@ function truncateToFit(existing, incoming) {
|
|
|
155
171
|
}
|
|
156
172
|
function deleteMemory(scope) {
|
|
157
173
|
if (scope.type === "session") {
|
|
158
|
-
throw new MemoryError(
|
|
159
|
-
"Session memory is stored within the session file. Use deleteSession() to remove."
|
|
160
|
-
);
|
|
174
|
+
throw new MemoryError("Session memory is stored within the session file. Use deleteSession() to remove.");
|
|
161
175
|
}
|
|
162
176
|
const filePath = resolveMemoryPath(scope);
|
|
163
|
-
if (!filePath)
|
|
177
|
+
if (!filePath)
|
|
178
|
+
return;
|
|
164
179
|
try {
|
|
165
180
|
try {
|
|
166
181
|
fs.unlinkSync(filePath);
|
|
167
182
|
} catch (unlinkErr) {
|
|
168
183
|
const isNotFound = unlinkErr.code === "ENOENT" || unlinkErr instanceof Error && unlinkErr.message.includes("ENOENT");
|
|
169
|
-
if (!isNotFound)
|
|
184
|
+
if (!isNotFound)
|
|
185
|
+
throw unlinkErr;
|
|
170
186
|
}
|
|
171
187
|
const dir = path.dirname(filePath);
|
|
172
188
|
try {
|
|
@@ -243,4 +259,4 @@ export {
|
|
|
243
259
|
deleteMemory,
|
|
244
260
|
listMemoryScopes
|
|
245
261
|
};
|
|
246
|
-
//# sourceMappingURL=chunk-
|
|
262
|
+
//# sourceMappingURL=chunk-Y6IZH6FT.js.map
|