@monoes/monomindcli 2.16.12 → 2.16.13
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/.claude/agents/core/coder.md +1 -1
- package/.claude/agents/engineering/engineering-devops-automator.md +1 -1
- package/.claude/agents/github/repo-architect.md +1 -1
- package/.claude/agents/testing/production-validator.md +1 -1
- package/.claude/agents/testing/testing-tool-evaluator.md +1 -1
- package/.claude/commands/github/pr-manager.md +1 -1
- package/.claude/helpers/pick-rank.cjs +34 -4
- package/.claude/skills/monolean-audit/SKILL.md +1 -1
- package/dist/src/commands/cleanup-data.d.ts +30 -0
- package/dist/src/commands/cleanup-data.d.ts.map +1 -0
- package/dist/src/commands/cleanup-data.js +138 -0
- package/dist/src/commands/cleanup-data.js.map +1 -0
- package/dist/src/commands/cleanup-scratch.d.ts +43 -0
- package/dist/src/commands/cleanup-scratch.d.ts.map +1 -0
- package/dist/src/commands/cleanup-scratch.js +125 -0
- package/dist/src/commands/cleanup-scratch.js.map +1 -0
- package/dist/src/commands/cleanup.d.ts +2 -39
- package/dist/src/commands/cleanup.d.ts.map +1 -1
- package/dist/src/commands/cleanup.js +5 -247
- package/dist/src/commands/cleanup.js.map +1 -1
- package/dist/src/commands/doctor-gitignore-checks.d.ts +10 -0
- package/dist/src/commands/doctor-gitignore-checks.d.ts.map +1 -0
- package/dist/src/commands/doctor-gitignore-checks.js +141 -0
- package/dist/src/commands/doctor-gitignore-checks.js.map +1 -0
- package/dist/src/commands/doctor-helpers-checks.d.ts +10 -0
- package/dist/src/commands/doctor-helpers-checks.d.ts.map +1 -0
- package/dist/src/commands/doctor-helpers-checks.js +292 -0
- package/dist/src/commands/doctor-helpers-checks.js.map +1 -0
- package/dist/src/commands/doctor-memory-checks.d.ts +47 -0
- package/dist/src/commands/doctor-memory-checks.d.ts.map +1 -0
- package/dist/src/commands/doctor-memory-checks.js +298 -0
- package/dist/src/commands/doctor-memory-checks.js.map +1 -0
- package/dist/src/commands/doctor-monograph-checks.d.ts +9 -0
- package/dist/src/commands/doctor-monograph-checks.d.ts.map +1 -0
- package/dist/src/commands/doctor-monograph-checks.js +331 -0
- package/dist/src/commands/doctor-monograph-checks.js.map +1 -0
- package/dist/src/commands/doctor-project-checks.d.ts +7 -66
- package/dist/src/commands/doctor-project-checks.d.ts.map +1 -1
- package/dist/src/commands/doctor-project-checks.js +10 -1331
- package/dist/src/commands/doctor-project-checks.js.map +1 -1
- package/dist/src/commands/doctor-routing-checks.d.ts +10 -0
- package/dist/src/commands/doctor-routing-checks.d.ts.map +1 -0
- package/dist/src/commands/doctor-routing-checks.js +194 -0
- package/dist/src/commands/doctor-routing-checks.js.map +1 -0
- package/dist/src/commands/doctor-worker-checks.d.ts +17 -0
- package/dist/src/commands/doctor-worker-checks.d.ts.map +1 -0
- package/dist/src/commands/doctor-worker-checks.js +122 -0
- package/dist/src/commands/doctor-worker-checks.js.map +1 -0
- package/dist/src/commands/hooks-routing-commands.js +1 -1
- package/dist/src/commands/hooks-routing-commands.js.map +1 -1
- package/dist/src/commands/init.d.ts.map +1 -1
- package/dist/src/commands/init.js +6 -15
- package/dist/src/commands/init.js.map +1 -1
- package/dist/src/index.js +2 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/init/never-commit.d.ts +3 -3
- package/dist/src/init/types.d.ts +1 -1
- package/dist/src/init/types.js +1 -1
- package/dist/src/knowledge/document-store.js +1 -1
- package/dist/src/knowledge/document-store.js.map +1 -1
- package/dist/src/mcp-tools/hooks-edit-command.d.ts +10 -0
- package/dist/src/mcp-tools/hooks-edit-command.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-edit-command.js +247 -0
- package/dist/src/mcp-tools/hooks-edit-command.js.map +1 -0
- package/dist/src/mcp-tools/hooks-learning.d.ts +9 -0
- package/dist/src/mcp-tools/hooks-learning.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-learning.js +408 -0
- package/dist/src/mcp-tools/hooks-learning.js.map +1 -0
- package/dist/src/mcp-tools/hooks-metrics-list.d.ts +8 -0
- package/dist/src/mcp-tools/hooks-metrics-list.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-metrics-list.js +205 -0
- package/dist/src/mcp-tools/hooks-metrics-list.js.map +1 -0
- package/dist/src/mcp-tools/hooks-route.d.ts +12 -0
- package/dist/src/mcp-tools/hooks-route.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-route.js +251 -0
- package/dist/src/mcp-tools/hooks-route.js.map +1 -0
- package/dist/src/mcp-tools/hooks-routing.d.ts +6 -27
- package/dist/src/mcp-tools/hooks-routing.d.ts.map +1 -1
- package/dist/src/mcp-tools/hooks-routing.js +6 -1727
- package/dist/src/mcp-tools/hooks-routing.js.map +1 -1
- package/dist/src/mcp-tools/hooks-session.d.ts +9 -0
- package/dist/src/mcp-tools/hooks-session.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-session.js +243 -0
- package/dist/src/mcp-tools/hooks-session.js.map +1 -0
- package/dist/src/mcp-tools/hooks-task.d.ts +17 -0
- package/dist/src/mcp-tools/hooks-task.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-task.js +427 -0
- package/dist/src/mcp-tools/hooks-task.js.map +1 -0
- package/dist/src/mcp-tools/hooks-tools.d.ts +2 -1
- package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/hooks-tools.js +2 -1
- package/dist/src/mcp-tools/hooks-tools.js.map +1 -1
- package/dist/src/mcp-tools/memory-tools.js +1 -1
- package/dist/src/mcp-tools/memory-tools.js.map +1 -1
- package/dist/src/memory/bm25-index.d.ts +2 -2
- package/dist/src/memory/bm25-index.js +2 -2
- package/dist/src/memory/embedding-operations.js +2 -2
- package/dist/src/memory/embedding-operations.js.map +1 -1
- package/dist/src/memory/entity-name-key.d.ts +1 -1
- package/dist/src/memory/entity-name-key.js +1 -1
- package/dist/src/memory/hnsw-operations.d.ts +1 -1
- package/dist/src/memory/hnsw-operations.js +1 -1
- package/dist/src/memory/memory-bridge-backend.d.ts +43 -0
- package/dist/src/memory/memory-bridge-backend.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-backend.js +363 -0
- package/dist/src/memory/memory-bridge-backend.js.map +1 -0
- package/dist/src/memory/memory-bridge-core.d.ts +42 -0
- package/dist/src/memory/memory-bridge-core.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-core.js +126 -0
- package/dist/src/memory/memory-bridge-core.js.map +1 -0
- package/dist/src/memory/memory-bridge-entries.d.ts +131 -0
- package/dist/src/memory/memory-bridge-entries.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-entries.js +227 -0
- package/dist/src/memory/memory-bridge-entries.js.map +1 -0
- package/dist/src/memory/memory-bridge-feedback.d.ts +60 -0
- package/dist/src/memory/memory-bridge-feedback.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-feedback.js +118 -0
- package/dist/src/memory/memory-bridge-feedback.js.map +1 -0
- package/dist/src/memory/memory-bridge-paths.d.ts +123 -0
- package/dist/src/memory/memory-bridge-paths.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-paths.js +335 -0
- package/dist/src/memory/memory-bridge-paths.js.map +1 -0
- package/dist/src/memory/memory-bridge-search.d.ts +48 -0
- package/dist/src/memory/memory-bridge-search.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-search.js +383 -0
- package/dist/src/memory/memory-bridge-search.js.map +1 -0
- package/dist/src/memory/memory-bridge-sessions.d.ts +38 -0
- package/dist/src/memory/memory-bridge-sessions.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-sessions.js +109 -0
- package/dist/src/memory/memory-bridge-sessions.js.map +1 -0
- package/dist/src/memory/memory-bridge-store.d.ts +58 -0
- package/dist/src/memory/memory-bridge-store.d.ts.map +1 -0
- package/dist/src/memory/memory-bridge-store.js +201 -0
- package/dist/src/memory/memory-bridge-store.js.map +1 -0
- package/dist/src/memory/memory-bridge.d.ts +10 -438
- package/dist/src/memory/memory-bridge.d.ts.map +1 -1
- package/dist/src/memory/memory-bridge.js +14 -1787
- package/dist/src/memory/memory-bridge.js.map +1 -1
- package/dist/src/memory/memory-kg-claims.d.ts +52 -0
- package/dist/src/memory/memory-kg-claims.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-claims.js +99 -0
- package/dist/src/memory/memory-kg-claims.js.map +1 -0
- package/dist/src/memory/memory-kg-index.d.ts +86 -0
- package/dist/src/memory/memory-kg-index.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-index.js +256 -0
- package/dist/src/memory/memory-kg-index.js.map +1 -0
- package/dist/src/memory/memory-kg-ingest.d.ts +44 -0
- package/dist/src/memory/memory-kg-ingest.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-ingest.js +361 -0
- package/dist/src/memory/memory-kg-ingest.js.map +1 -0
- package/dist/src/memory/memory-kg-model.d.ts +168 -0
- package/dist/src/memory/memory-kg-model.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-model.js +158 -0
- package/dist/src/memory/memory-kg-model.js.map +1 -0
- package/dist/src/memory/memory-kg-names.d.ts +64 -0
- package/dist/src/memory/memory-kg-names.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-names.js +187 -0
- package/dist/src/memory/memory-kg-names.js.map +1 -0
- package/dist/src/memory/memory-kg-promote.d.ts +79 -0
- package/dist/src/memory/memory-kg-promote.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-promote.js +248 -0
- package/dist/src/memory/memory-kg-promote.js.map +1 -0
- package/dist/src/memory/memory-kg-rebuild.d.ts +77 -0
- package/dist/src/memory/memory-kg-rebuild.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-rebuild.js +299 -0
- package/dist/src/memory/memory-kg-rebuild.js.map +1 -0
- package/dist/src/memory/memory-kg-rollback.d.ts +47 -0
- package/dist/src/memory/memory-kg-rollback.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-rollback.js +193 -0
- package/dist/src/memory/memory-kg-rollback.js.map +1 -0
- package/dist/src/memory/memory-kg-rules.d.ts +64 -0
- package/dist/src/memory/memory-kg-rules.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-rules.js +251 -0
- package/dist/src/memory/memory-kg-rules.js.map +1 -0
- package/dist/src/memory/memory-kg-scan.d.ts +118 -0
- package/dist/src/memory/memory-kg-scan.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-scan.js +209 -0
- package/dist/src/memory/memory-kg-scan.js.map +1 -0
- package/dist/src/memory/memory-kg-search.d.ts +80 -0
- package/dist/src/memory/memory-kg-search.d.ts.map +1 -0
- package/dist/src/memory/memory-kg-search.js +239 -0
- package/dist/src/memory/memory-kg-search.js.map +1 -0
- package/dist/src/memory/memory-kg.d.ts +19 -547
- package/dist/src/memory/memory-kg.d.ts.map +1 -1
- package/dist/src/memory/memory-kg.js +10 -2389
- package/dist/src/memory/memory-kg.js.map +1 -1
- package/dist/src/ui/org-hil.mjs +1 -1
- package/dist/src/ui/routes-org-agents.mjs +318 -0
- package/dist/src/ui/routes-org-approvals.mjs +133 -0
- package/dist/src/ui/routes-org-config.mjs +459 -0
- package/dist/src/ui/routes-org-control.mjs +300 -0
- package/dist/src/ui/routes-org-files.mjs +172 -0
- package/dist/src/ui/routes-org-helpers.mjs +84 -0
- package/dist/src/ui/routes-org-knowledge.mjs +262 -0
- package/dist/src/ui/routes-org-lifecycle.mjs +224 -0
- package/dist/src/ui/routes-org-live.mjs +278 -0
- package/dist/src/ui/routes-org-mastermind.mjs +346 -0
- package/dist/src/ui/routes-org-planning.mjs +398 -0
- package/dist/src/ui/routes-org-runs.mjs +430 -0
- package/dist/src/ui/routes-org-status.mjs +342 -0
- package/dist/src/ui/routes-org-workflows.mjs +212 -0
- package/dist/src/ui/routes-org.mjs +26 -3847
- package/dist/src/ui/server.mjs +1 -1
- package/dist/src/utils/native-error.d.ts +1 -1
- package/dist/src/utils/native-error.js +1 -1
- package/dist/src/utils/paths.d.ts +2 -1
- package/dist/src/utils/paths.d.ts.map +1 -1
- package/dist/src/utils/paths.js +2 -1
- package/dist/src/utils/paths.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/org-skills/adversarial-reviewer/SKILL.md +1 -1
- package/org-skills/board-deck-builder/SKILL.md +1 -1
- package/org-skills/code-reviewer/SKILL.md +1 -1
- package/org-skills/debugging-wizard/SKILL.md +1 -1
- package/org-skills/scientific-thinking-literature-review/SKILL.md +1 -1
- package/org-skills/scientific-thinking-scholar-evaluation/SKILL.md +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Bridge — the lazy per-path backend cache, the process-wide local
|
|
3
|
+
* models (embedder + cross-encoder reranker) and their kill switch, and
|
|
4
|
+
* bridge lifecycle. Owns all of that module-level state; other bridge
|
|
5
|
+
* modules only read it. Split out of memory-bridge.ts, which re-exports the
|
|
6
|
+
* public symbols.
|
|
7
|
+
*/
|
|
8
|
+
import * as fs from 'node:fs';
|
|
9
|
+
import * as os from 'node:os';
|
|
10
|
+
import * as path from 'node:path';
|
|
11
|
+
import { BRIDGE_EMBEDDING_MODEL, logBridgeError } from './memory-bridge-core.js';
|
|
12
|
+
import { getDbPath, getGlobalBrainDir, getProjectRoot, projectDataDir, } from './memory-bridge-paths.js';
|
|
13
|
+
import { ettinHeadLogit, loadEttinHead, resolveRerankerSource } from './reranker-head.js';
|
|
14
|
+
const backendSlots = new Map();
|
|
15
|
+
const MAX_BACKEND_SLOTS = 5;
|
|
16
|
+
export let _embedder = null;
|
|
17
|
+
let _embedderPromise = null;
|
|
18
|
+
const MAX_INIT_ATTEMPTS = 3;
|
|
19
|
+
// Process-local kill switch for both local ONNX models (embedder + reranker):
|
|
20
|
+
// the in-process equivalent of MONOMIND_NO_LOCAL_EMBEDDINGS=1 plus
|
|
21
|
+
// MONOMIND_RERANKER=0. Org runs flip this instead of setting those env vars,
|
|
22
|
+
// because process.env is inherited by every child — each role's CLI and every
|
|
23
|
+
// command a role runs through Bash — which silently degraded a role's own
|
|
24
|
+
// `monomind memory search` to keyword-only (#249).
|
|
25
|
+
let _localModelsDisabled = false;
|
|
26
|
+
/** Disable local model loads for THIS process only (see `org run`/`org serve`). */
|
|
27
|
+
export function disableLocalModels() {
|
|
28
|
+
_localModelsDisabled = true;
|
|
29
|
+
}
|
|
30
|
+
export function localEmbeddingsDisabled() {
|
|
31
|
+
return _localModelsDisabled || process.env.MONOMIND_NO_LOCAL_EMBEDDINGS === '1';
|
|
32
|
+
}
|
|
33
|
+
export function rerankerDisabled() {
|
|
34
|
+
return _localModelsDisabled || process.env.MONOMIND_RERANKER === '0';
|
|
35
|
+
}
|
|
36
|
+
// ===== Lazy cross-encoder reranker (ettin-32m) =====
|
|
37
|
+
//
|
|
38
|
+
// Same ORT constraints as the embedder (ADR-R001). Loaded only when the first
|
|
39
|
+
// search with >1 candidate completes — never on store, never on startup.
|
|
40
|
+
// Disabled with MONOMIND_RERANKER=0.
|
|
41
|
+
//
|
|
42
|
+
// The upstream HF ONNX file for ettin-reranker-32m-v1 only contains the base
|
|
43
|
+
// ModernBERT encoder (outputs last_hidden_state, no logits). The classifier
|
|
44
|
+
// head lives in separate sentence-transformers module safetensors files
|
|
45
|
+
// (2_Dense, 3_LayerNorm, 4_Dense). Two ways to score, in order of preference:
|
|
46
|
+
// - a self-exported ONNX with the head baked in (scripts/export-ettin-onnx.py,
|
|
47
|
+
// needs PyTorch) under ~/.monomind/models/ettin-reranker-32m-v1-onnx/
|
|
48
|
+
// - the upstream encoder plus the head applied in JS (reranker-head.ts), from
|
|
49
|
+
// head files fetched by `doc eval --provision-model` into
|
|
50
|
+
// ~/.monomind/models/ettin-reranker-32m-v1-head/
|
|
51
|
+
// With neither, the reranker does not load: the upstream encoder alone cannot
|
|
52
|
+
// score, and loading it anyway ran the model on every search for nothing.
|
|
53
|
+
export const BRIDGE_RERANKER_MODEL = 'cross-encoder/ettin-reranker-32m-v1';
|
|
54
|
+
/** Where reranker export / head files live. */
|
|
55
|
+
export function rerankerModelsDir() {
|
|
56
|
+
return path.join(os.homedir(), '.monomind', 'models');
|
|
57
|
+
}
|
|
58
|
+
/** How the reranker is scoring, or null when it is not loaded. */
|
|
59
|
+
let _rerankerKind = null;
|
|
60
|
+
export function rerankerKind() {
|
|
61
|
+
return _rerankerKind;
|
|
62
|
+
}
|
|
63
|
+
export let _reranker = null;
|
|
64
|
+
export let _rerankerPromise = null;
|
|
65
|
+
/** Pre-load the cross-encoder reranker model. Idempotent, no-op when
|
|
66
|
+
* MONOMIND_RERANKER=0. Exported so the eval harness can force-load before
|
|
67
|
+
* the network guard blocks model downloads. */
|
|
68
|
+
export async function loadReranker() {
|
|
69
|
+
if (_reranker)
|
|
70
|
+
return;
|
|
71
|
+
if (rerankerDisabled())
|
|
72
|
+
return;
|
|
73
|
+
if (!_rerankerPromise) {
|
|
74
|
+
_rerankerPromise = (async () => {
|
|
75
|
+
try {
|
|
76
|
+
const source = resolveRerankerSource(rerankerModelsDir());
|
|
77
|
+
if (!source)
|
|
78
|
+
return; // nothing that can score — stay unloaded (no per-search cost)
|
|
79
|
+
const hf = await import('@huggingface/transformers');
|
|
80
|
+
const opts = { local_files_only: true };
|
|
81
|
+
const sigmoid = (logit) => 1 / (1 + Math.exp(-logit));
|
|
82
|
+
if (source.kind === 'export') {
|
|
83
|
+
const tokenizer = await hf.AutoTokenizer.from_pretrained(source.dir, opts);
|
|
84
|
+
const model = await hf.AutoModelForSequenceClassification.from_pretrained(source.dir, opts);
|
|
85
|
+
_reranker = async (query, passage) => {
|
|
86
|
+
const inputs = await tokenizer(query, {
|
|
87
|
+
text_pair: passage,
|
|
88
|
+
padding: true,
|
|
89
|
+
truncation: true,
|
|
90
|
+
});
|
|
91
|
+
const output = await model(inputs);
|
|
92
|
+
// num_labels=1 → [1,1] regression score, apply sigmoid
|
|
93
|
+
return sigmoid(output.logits.data[0]);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const head = loadEttinHead(source.dir);
|
|
98
|
+
const tokenizer = await hf.AutoTokenizer.from_pretrained(BRIDGE_RERANKER_MODEL, opts);
|
|
99
|
+
const model = await hf.AutoModel.from_pretrained(BRIDGE_RERANKER_MODEL, {
|
|
100
|
+
...opts,
|
|
101
|
+
dtype: 'fp32',
|
|
102
|
+
});
|
|
103
|
+
_reranker = async (query, passage) => {
|
|
104
|
+
const inputs = await tokenizer(query, {
|
|
105
|
+
text_pair: passage,
|
|
106
|
+
padding: true,
|
|
107
|
+
truncation: true,
|
|
108
|
+
});
|
|
109
|
+
const output = await model(inputs);
|
|
110
|
+
return sigmoid(ettinHeadLogit(head, output.last_hidden_state.data));
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
_rerankerKind = source.kind;
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
_rerankerPromise = null; // allow retry
|
|
117
|
+
if (process.env.DEBUG || process.env.MONOMIND_DEBUG)
|
|
118
|
+
console.error('[memory-bridge] reranker failed to load:', e);
|
|
119
|
+
}
|
|
120
|
+
})();
|
|
121
|
+
}
|
|
122
|
+
await _rerankerPromise;
|
|
123
|
+
}
|
|
124
|
+
/** Rerank an array of results using the cross-encoder. Mutates nothing; returns
|
|
125
|
+
* a new sorted array with reranker scores in provenance. */
|
|
126
|
+
export async function rerankResults(query, results, limit) {
|
|
127
|
+
const reranker = _reranker;
|
|
128
|
+
if (!reranker || results.length <= 1)
|
|
129
|
+
return { reranked: results, applied: false };
|
|
130
|
+
try {
|
|
131
|
+
const scored = await Promise.all(results.map(async (r) => {
|
|
132
|
+
const rerankerScore = await reranker(query, r.content || '');
|
|
133
|
+
return {
|
|
134
|
+
...r,
|
|
135
|
+
score: rerankerScore,
|
|
136
|
+
provenance: `${r.provenance ?? ''}→rerank:${rerankerScore.toFixed(3)}`,
|
|
137
|
+
};
|
|
138
|
+
}));
|
|
139
|
+
scored.sort((a, b) => b.score - a.score);
|
|
140
|
+
return { reranked: scored.slice(0, limit), applied: true };
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
if (process.env.DEBUG || process.env.MONOMIND_DEBUG)
|
|
144
|
+
console.error('[memory-bridge] reranking failed — returning original order:', e);
|
|
145
|
+
return { reranked: results, applied: false };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** Flush after mutations: the sql.js fallback backend is in-memory WASM and
|
|
149
|
+
* only reaches disk via persist(); the CLI process is short-lived, so waiting
|
|
150
|
+
* for an auto-persist interval would lose writes. No-op on better-sqlite3. */
|
|
151
|
+
export async function flushBackend(backend) {
|
|
152
|
+
try {
|
|
153
|
+
await backend?.persist?.();
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
logBridgeError('flushBackend', e); /* best effort */
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Loads the local embedding model.
|
|
160
|
+
*
|
|
161
|
+
* This is the single point where `onnxruntime-node` enters the CLI process
|
|
162
|
+
* (via @huggingface/transformers). Once it has run, the process is subject to
|
|
163
|
+
* docs/adrs/ADR-R001-onnxruntime-process-teardown.md: calling process.exit()
|
|
164
|
+
* will abort with SIGABRT ("mutex lock failed") instead of exiting cleanly,
|
|
165
|
+
* and disposing the pipeline first does not help.
|
|
166
|
+
*
|
|
167
|
+
* Anything that reaches this — `doctor`, `memory store`, `memory search`, the
|
|
168
|
+
* MCP memory tools — inherits that constraint, which is why it bit commands
|
|
169
|
+
* that look nothing like ML work. Adding a new caller is fine; adding a new
|
|
170
|
+
* process-exit path is not. */
|
|
171
|
+
async function loadEmbedder() {
|
|
172
|
+
if (_embedder)
|
|
173
|
+
return;
|
|
174
|
+
// MONOMIND_NO_LOCAL_EMBEDDINGS: skip the native pipeline() load entirely.
|
|
175
|
+
// On some machines @huggingface/transformers' ONNX runtime crashes the
|
|
176
|
+
// whole process with a native `libc++abi terminate` (a mutex failure
|
|
177
|
+
// inside the tokenizer backend) rather than throwing a catchable JS
|
|
178
|
+
// error — the try/catch below only protects against normal failures
|
|
179
|
+
// (missing cache, bad revision, offline), not that. Everything that
|
|
180
|
+
// reads _embedder already tolerates it being unset (keyword-only
|
|
181
|
+
// fallback), so skipping the call is the only way to actually avoid the
|
|
182
|
+
// crash rather than just failing to catch it. Org runs disable it
|
|
183
|
+
// in-process (disableLocalModels()) since a crashed org is much worse than
|
|
184
|
+
// degraded search; anyone else can opt in with the env var.
|
|
185
|
+
if (localEmbeddingsDisabled())
|
|
186
|
+
return;
|
|
187
|
+
if (!_embedderPromise) {
|
|
188
|
+
_embedderPromise = (async () => {
|
|
189
|
+
try {
|
|
190
|
+
const hf = await import('@huggingface/transformers');
|
|
191
|
+
// revision must be a git ref — 'main' is the HF default; 'default' 404s and
|
|
192
|
+
// silently killed embeddings (every search degraded to keyword matching)
|
|
193
|
+
// dtype pinned explicitly: transformers.js logs a "dtype not specified"
|
|
194
|
+
// warning to the console on every load otherwise (leaks into CLI output).
|
|
195
|
+
const extractor = await hf.pipeline('feature-extraction', BRIDGE_EMBEDDING_MODEL, {
|
|
196
|
+
revision: 'main',
|
|
197
|
+
dtype: 'q8',
|
|
198
|
+
local_files_only: true,
|
|
199
|
+
});
|
|
200
|
+
_embedder = async (text) => {
|
|
201
|
+
const output = await extractor(text, { pooling: 'cls', normalize: true });
|
|
202
|
+
return new Float32Array(output.data);
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
catch (e) {
|
|
206
|
+
_embedderPromise = null; // allow retry (e.g. first call offline)
|
|
207
|
+
if (process.env.DEBUG || process.env.MONOMIND_DEBUG)
|
|
208
|
+
console.error('[memory-bridge] embedding model failed to load — store and search without vectors:', e);
|
|
209
|
+
}
|
|
210
|
+
})();
|
|
211
|
+
}
|
|
212
|
+
await _embedderPromise;
|
|
213
|
+
}
|
|
214
|
+
/** Fetches the local embedding model into the transformers cache so later
|
|
215
|
+
* loadEmbedder() calls (local_files_only) find it. Same model, revision and
|
|
216
|
+
* dtype as loadEmbedder() — only the network is allowed here, which is why it
|
|
217
|
+
* is reserved for explicit opt-in steps (`init --with-embeddings`). Throws
|
|
218
|
+
* when the model cannot be fetched (offline, disabled, package missing);
|
|
219
|
+
* callers degrade. Loads onnxruntime, so ADR-R001 applies to the caller. */
|
|
220
|
+
export async function downloadEmbeddingModel() {
|
|
221
|
+
if (localEmbeddingsDisabled())
|
|
222
|
+
throw new Error('local embeddings are disabled (MONOMIND_NO_LOCAL_EMBEDDINGS=1)');
|
|
223
|
+
const hf = await import('@huggingface/transformers');
|
|
224
|
+
await hf.pipeline('feature-extraction', BRIDGE_EMBEDDING_MODEL, {
|
|
225
|
+
revision: 'main',
|
|
226
|
+
dtype: 'q8',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
export async function getBackend(dbPath) {
|
|
230
|
+
const dir = getDbPath(dbPath);
|
|
231
|
+
let slot = backendSlots.get(dir);
|
|
232
|
+
if (!slot) {
|
|
233
|
+
if (backendSlots.size >= MAX_BACKEND_SLOTS) {
|
|
234
|
+
const oldest = backendSlots.keys().next().value;
|
|
235
|
+
const evicted = backendSlots.get(oldest);
|
|
236
|
+
// shutdownBridge() below uses .shutdown() — that's the real method these
|
|
237
|
+
// backends expose. .close() doesn't exist on either backend class, so this
|
|
238
|
+
// resolved to undefined via the optional chain every time and never actually
|
|
239
|
+
// released the connection: every 6th distinct database path opened in this
|
|
240
|
+
// process leaked the oldest slot's connection for the process lifetime.
|
|
241
|
+
try {
|
|
242
|
+
await evicted?.instance?.shutdown?.();
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
logBridgeError('getBackend.evictedShutdown', e); /* best effort */
|
|
246
|
+
}
|
|
247
|
+
backendSlots.delete(oldest);
|
|
248
|
+
}
|
|
249
|
+
slot = { promise: null, instance: null, available: null, attempts: 0 };
|
|
250
|
+
backendSlots.set(dir, slot);
|
|
251
|
+
}
|
|
252
|
+
if (slot.available === false)
|
|
253
|
+
return null;
|
|
254
|
+
if (slot.attempts >= MAX_INIT_ATTEMPTS) {
|
|
255
|
+
slot.available = false;
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
if (slot.instance)
|
|
259
|
+
return slot.instance;
|
|
260
|
+
if (!slot.promise) {
|
|
261
|
+
slot.promise = (async () => {
|
|
262
|
+
try {
|
|
263
|
+
const mod = await import('@monoes/memory');
|
|
264
|
+
await loadEmbedder();
|
|
265
|
+
// Local SQLite engine (LanceDB replaced 2026-07): better-sqlite3 when its
|
|
266
|
+
// native binding loads, sql.js (pure WASM) otherwise — both persist text
|
|
267
|
+
// AND embeddings, so vectors are always recomputable/derivable data.
|
|
268
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
269
|
+
// Origin marker: records which project this data dir belongs to, so
|
|
270
|
+
// `monomind cleanup --data` can verifiably prune dirs whose project
|
|
271
|
+
// no longer exists (the dir-name hash is one-way). Best-effort; never
|
|
272
|
+
// written for the global brain (it has no single origin project).
|
|
273
|
+
if (dir !== getGlobalBrainDir()) {
|
|
274
|
+
try {
|
|
275
|
+
// MUST be the same path projectDataDir() hashed into the slug. If
|
|
276
|
+
// this recorded the raw cwd, running any memory command from a
|
|
277
|
+
// package subdirectory would stamp that subdirectory into the
|
|
278
|
+
// project-root-keyed dir — and deleting the subdirectory later
|
|
279
|
+
// would make `cleanup --data` prune the WHOLE project's brain as
|
|
280
|
+
// orphaned.
|
|
281
|
+
const originFile = path.join(projectDataDir(), 'origin.json');
|
|
282
|
+
fs.writeFileSync(originFile, JSON.stringify({ path: getProjectRoot(), updatedAt: new Date().toISOString() }) +
|
|
283
|
+
'\n', 'utf-8');
|
|
284
|
+
}
|
|
285
|
+
catch (e) {
|
|
286
|
+
logBridgeError('getBackend.originWrite', e); /* non-fatal */
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const cfg = {
|
|
290
|
+
databasePath: path.join(dir, 'memory.db'),
|
|
291
|
+
walMode: true,
|
|
292
|
+
optimize: true,
|
|
293
|
+
defaultNamespace: 'default',
|
|
294
|
+
embeddingGenerator: _embedder ?? undefined,
|
|
295
|
+
// R3: when the MCP server and a CLI hook subprocess hit the same
|
|
296
|
+
// memory.db at the same time, SQLite returns SQLITE_BUSY and the
|
|
297
|
+
// bridge silently no-ops. busy_timeout tells SQLite to wait up to
|
|
298
|
+
// 5s for a lock before giving up, which covers the normal handoff
|
|
299
|
+
// window. The native @monoes/memory backend reads this key.
|
|
300
|
+
busyTimeoutMs: 5000,
|
|
301
|
+
};
|
|
302
|
+
const origLog = console.log;
|
|
303
|
+
console.log = (...args) => {
|
|
304
|
+
const msg = String(args[0] ?? '');
|
|
305
|
+
if (msg.includes('Transformers.js') || msg.includes('Loading model'))
|
|
306
|
+
return;
|
|
307
|
+
origLog.apply(console, args);
|
|
308
|
+
};
|
|
309
|
+
let backend;
|
|
310
|
+
try {
|
|
311
|
+
try {
|
|
312
|
+
backend = new mod.SQLiteBackend(cfg);
|
|
313
|
+
await backend.initialize();
|
|
314
|
+
}
|
|
315
|
+
catch (e) {
|
|
316
|
+
if (process.env.DEBUG || process.env.MONOMIND_DEBUG)
|
|
317
|
+
console.error('[memory-bridge] better-sqlite3 unavailable — using sql.js backend:', e);
|
|
318
|
+
backend = new mod.SqlJsBackend(cfg);
|
|
319
|
+
await backend.initialize();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
finally {
|
|
323
|
+
console.log = origLog;
|
|
324
|
+
}
|
|
325
|
+
slot.instance = backend;
|
|
326
|
+
slot.available = true;
|
|
327
|
+
return backend;
|
|
328
|
+
}
|
|
329
|
+
catch (e) {
|
|
330
|
+
slot.attempts++;
|
|
331
|
+
slot.promise = null;
|
|
332
|
+
if (slot.attempts >= MAX_INIT_ATTEMPTS)
|
|
333
|
+
slot.available = false;
|
|
334
|
+
logBridgeError('getBackend', e);
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
})();
|
|
338
|
+
}
|
|
339
|
+
return slot.promise;
|
|
340
|
+
}
|
|
341
|
+
// ===== Availability / lifecycle =====
|
|
342
|
+
export async function isBridgeAvailable(dbPath) {
|
|
343
|
+
const backend = await getBackend(dbPath);
|
|
344
|
+
return !!backend;
|
|
345
|
+
}
|
|
346
|
+
export async function shutdownBridge() {
|
|
347
|
+
for (const slot of backendSlots.values()) {
|
|
348
|
+
if (slot.instance) {
|
|
349
|
+
try {
|
|
350
|
+
await slot.instance.shutdown();
|
|
351
|
+
}
|
|
352
|
+
catch (e) {
|
|
353
|
+
logBridgeError('bridgeShutdown.slotShutdown', e); /* ignore */
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
backendSlots.clear();
|
|
358
|
+
_embedder = null;
|
|
359
|
+
_embedderPromise = null;
|
|
360
|
+
_reranker = null;
|
|
361
|
+
_rerankerPromise = null;
|
|
362
|
+
}
|
|
363
|
+
//# sourceMappingURL=memory-bridge-backend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-bridge-backend.js","sourceRoot":"","sources":["../../../src/memory/memory-bridge-backend.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACjF,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,cAAc,GACf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAiB1F,MAAM,YAAY,GAAG,IAAI,GAAG,EAAuB,CAAC;AACpD,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,CAAC,IAAI,SAAS,GAAqD,IAAI,CAAC;AAC9E,IAAI,gBAAgB,GAAyB,IAAI,CAAC;AAClD,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAE5B,8EAA8E;AAC9E,mEAAmE;AACnE,6EAA6E;AAC7E,8EAA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CAAC;IACjB,gBAAgB,GAAG,IAAI,CAAC;IACxB,SAAS,GAAG,IAAI,CAAC;IACjB,gBAAgB,GAAG,IAAI,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Bridge — shared helpers: error logging, embedding validation,
|
|
3
|
+
* limits, automem config and usage/feedback weights. Split out of
|
|
4
|
+
* memory-bridge.ts, which re-exports the public symbols.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* R1: surface swallowed errors when DEBUG/MONOMIND_DEBUG is on. The bridge
|
|
8
|
+
* has ~12 `} catch { return null; }` sites that collapse SQLITE_BUSY,
|
|
9
|
+
* EACCES, disk-full, and schema mismatches into "no matches" with zero
|
|
10
|
+
* diagnostic. Behavior contract (return null on failure) is unchanged;
|
|
11
|
+
* observability is added. Caller passes the bridge fn name + the thrown
|
|
12
|
+
* value so the log is greppable per call site.
|
|
13
|
+
*/
|
|
14
|
+
export declare function logBridgeError(label: string, err: unknown): void;
|
|
15
|
+
export declare function safeParseEmbedding(raw: string | null | undefined): number[] | null;
|
|
16
|
+
export declare const BRIDGE_EMBEDDING_MODEL = "Alibaba-NLP/gte-modernbert-base";
|
|
17
|
+
export declare const BRIDGE_EMBEDDING_DIMS = 768;
|
|
18
|
+
export declare const BRIDGE_MAX_KEY_LEN: number;
|
|
19
|
+
export declare const BRIDGE_MAX_VALUE_LEN: number;
|
|
20
|
+
export declare const MAX_TAGS = 32;
|
|
21
|
+
export declare const MAX_TAG_LEN = 64;
|
|
22
|
+
export declare function capResultContent(content: string): string;
|
|
23
|
+
export declare function getAutomemConfig(): {
|
|
24
|
+
dedupThreshold: number;
|
|
25
|
+
staleDays: number;
|
|
26
|
+
feedbackInfluence: number;
|
|
27
|
+
};
|
|
28
|
+
export declare const FEEDBACK_EWMA_ALPHA = 0.1;
|
|
29
|
+
export declare function entryWeights(metadata: unknown): {
|
|
30
|
+
feedback: number;
|
|
31
|
+
frequency: number;
|
|
32
|
+
};
|
|
33
|
+
/** Blend learned usefulness into a GENUINE embedding-similarity score.
|
|
34
|
+
* Cognee guard: never applied to keyword-fallback scores — those carry no
|
|
35
|
+
* real relevance signal, and blending there lets a high-feedback stale entry
|
|
36
|
+
* outrank relevant matches and self-reinforce (rich-get-richer). */
|
|
37
|
+
export declare function blendScore(cosineSim: number, weights: {
|
|
38
|
+
feedback: number;
|
|
39
|
+
frequency: number;
|
|
40
|
+
}, influence: number): number;
|
|
41
|
+
export declare function generateId(prefix: string): string;
|
|
42
|
+
//# sourceMappingURL=memory-bridge-core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-bridge-core.d.ts","sourceRoot":"","sources":["../../../src/memory/memory-bridge-core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAWH;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,IAAI,CAIhE;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,EAAE,GAAG,IAAI,CAiBlF;AAID,eAAO,MAAM,sBAAsB,oCAAoC,CAAC;AACxE,eAAO,MAAM,qBAAqB,MAAM,CAAC;AACzC,eAAO,MAAM,kBAAkB,QAAW,CAAC;AAC3C,eAAO,MAAM,oBAAoB,QAAY,CAAC;AAC9C,eAAO,MAAM,QAAQ,KAAK,CAAC;AAC3B,eAAO,MAAM,WAAW,KAAK,CAAC;AAK9B,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAIxD;AAED,wBAAgB,gBAAgB,IAAI;IAClC,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CA0BA;AAWD,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAIvC,wBAAgB,YAAY,CAAC,QAAQ,EAAE,OAAO,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAWvF;AAED;;;qEAGqE;AACrE,wBAAgB,UAAU,CACxB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,EAChD,SAAS,EAAE,MAAM,GAChB,MAAM,CAKR;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEjD"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Bridge — shared helpers: error logging, embedding validation,
|
|
3
|
+
* limits, automem config and usage/feedback weights. Split out of
|
|
4
|
+
* memory-bridge.ts, which re-exports the public symbols.
|
|
5
|
+
*/
|
|
6
|
+
import * as crypto from 'node:crypto';
|
|
7
|
+
import * as fs from 'node:fs';
|
|
8
|
+
import * as path from 'node:path';
|
|
9
|
+
// ===== Embedding validation =====
|
|
10
|
+
const MAX_EMBEDDING_DIMS = 8192;
|
|
11
|
+
const MAX_EMBEDDING_JSON_BYTES = MAX_EMBEDDING_DIMS * 32; // ~256KB ceiling
|
|
12
|
+
/**
|
|
13
|
+
* R1: surface swallowed errors when DEBUG/MONOMIND_DEBUG is on. The bridge
|
|
14
|
+
* has ~12 `} catch { return null; }` sites that collapse SQLITE_BUSY,
|
|
15
|
+
* EACCES, disk-full, and schema mismatches into "no matches" with zero
|
|
16
|
+
* diagnostic. Behavior contract (return null on failure) is unchanged;
|
|
17
|
+
* observability is added. Caller passes the bridge fn name + the thrown
|
|
18
|
+
* value so the log is greppable per call site.
|
|
19
|
+
*/
|
|
20
|
+
export function logBridgeError(label, err) {
|
|
21
|
+
if (!(process.env.DEBUG || process.env.MONOMIND_DEBUG))
|
|
22
|
+
return;
|
|
23
|
+
const msg = err instanceof Error ? `${err.name}: ${err.message}` : String(err);
|
|
24
|
+
console.error(`[bridge:${label}] ${msg}`);
|
|
25
|
+
}
|
|
26
|
+
export function safeParseEmbedding(raw) {
|
|
27
|
+
if (typeof raw !== 'string' || raw.length === 0)
|
|
28
|
+
return null;
|
|
29
|
+
if (raw.length > MAX_EMBEDDING_JSON_BYTES)
|
|
30
|
+
return null;
|
|
31
|
+
let parsed;
|
|
32
|
+
try {
|
|
33
|
+
parsed = JSON.parse(raw);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
logBridgeError('safeParseEmbedding', e);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (!Array.isArray(parsed))
|
|
40
|
+
return null;
|
|
41
|
+
if (parsed.length === 0 || parsed.length > MAX_EMBEDDING_DIMS)
|
|
42
|
+
return null;
|
|
43
|
+
for (let i = 0; i < parsed.length; i++) {
|
|
44
|
+
const v = parsed[i];
|
|
45
|
+
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return parsed;
|
|
49
|
+
}
|
|
50
|
+
// ===== Constants =====
|
|
51
|
+
export const BRIDGE_EMBEDDING_MODEL = 'Alibaba-NLP/gte-modernbert-base';
|
|
52
|
+
export const BRIDGE_EMBEDDING_DIMS = 768;
|
|
53
|
+
export const BRIDGE_MAX_KEY_LEN = 4 * 1024;
|
|
54
|
+
export const BRIDGE_MAX_VALUE_LEN = 16 * 1024;
|
|
55
|
+
export const MAX_TAGS = 32;
|
|
56
|
+
export const MAX_TAG_LEN = 64;
|
|
57
|
+
// Search results serve the head of the stored content only — full values bloat
|
|
58
|
+
// every MCP payload. Entries needing the full text can read the entry by key.
|
|
59
|
+
const BRIDGE_RESULT_CONTENT_CAP = 500;
|
|
60
|
+
export function capResultContent(content) {
|
|
61
|
+
return content.length > BRIDGE_RESULT_CONTENT_CAP
|
|
62
|
+
? `${content.slice(0, BRIDGE_RESULT_CONTENT_CAP)}…`
|
|
63
|
+
: content;
|
|
64
|
+
}
|
|
65
|
+
export function getAutomemConfig() {
|
|
66
|
+
const defaults = { dedupThreshold: 0.85, staleDays: 7, feedbackInfluence: 0.2 };
|
|
67
|
+
try {
|
|
68
|
+
const configPath = path.join(process.cwd(), '.monomind', 'automem-config.json');
|
|
69
|
+
if (!fs.existsSync(configPath))
|
|
70
|
+
return defaults;
|
|
71
|
+
const stat = fs.statSync(configPath);
|
|
72
|
+
if (stat.size > 64 * 1024)
|
|
73
|
+
return defaults;
|
|
74
|
+
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
75
|
+
return {
|
|
76
|
+
dedupThreshold: typeof config?.scaffold?.dedupThreshold === 'number'
|
|
77
|
+
? config.scaffold.dedupThreshold
|
|
78
|
+
: defaults.dedupThreshold,
|
|
79
|
+
staleDays: typeof config?.scaffold?.staleDays === 'number'
|
|
80
|
+
? config.scaffold.staleDays
|
|
81
|
+
: defaults.staleDays,
|
|
82
|
+
feedbackInfluence: typeof config?.scaffold?.feedbackInfluence === 'number'
|
|
83
|
+
? Math.max(0, Math.min(1, config.scaffold.feedbackInfluence))
|
|
84
|
+
: defaults.feedbackInfluence,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
logBridgeError('loadBridgeConfig', e);
|
|
89
|
+
return defaults;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// ===== Usage/feedback weights (cognee-style, stored in entry metadata) =====
|
|
93
|
+
//
|
|
94
|
+
// feedback_weight (0..1, default 0.5): EWMA of explicit/auto ratings applied to
|
|
95
|
+
// the entries actually used to produce an answer. frequency_weight (>=0): how
|
|
96
|
+
// often the entry was returned by a search. Both live in the entry's metadata
|
|
97
|
+
// JSON — deliberately NOT backend schema columns, so no @monoes/memory publish
|
|
98
|
+
// is needed and both backends work unchanged.
|
|
99
|
+
const DEFAULT_FEEDBACK_WEIGHT = 0.5;
|
|
100
|
+
export const FEEDBACK_EWMA_ALPHA = 0.1;
|
|
101
|
+
/** frequency_weight normalization ceiling: 10+ uses counts as fully reinforced. */
|
|
102
|
+
const FREQUENCY_NORM_CAP = 10;
|
|
103
|
+
export function entryWeights(metadata) {
|
|
104
|
+
const md = (metadata ?? {});
|
|
105
|
+
const fw = typeof md.feedback_weight === 'number' && Number.isFinite(md.feedback_weight)
|
|
106
|
+
? Math.max(0, Math.min(1, md.feedback_weight))
|
|
107
|
+
: DEFAULT_FEEDBACK_WEIGHT;
|
|
108
|
+
const freq = typeof md.frequency_weight === 'number' && Number.isFinite(md.frequency_weight)
|
|
109
|
+
? Math.max(0, md.frequency_weight)
|
|
110
|
+
: 0;
|
|
111
|
+
return { feedback: fw, frequency: freq };
|
|
112
|
+
}
|
|
113
|
+
/** Blend learned usefulness into a GENUINE embedding-similarity score.
|
|
114
|
+
* Cognee guard: never applied to keyword-fallback scores — those carry no
|
|
115
|
+
* real relevance signal, and blending there lets a high-feedback stale entry
|
|
116
|
+
* outrank relevant matches and self-reinforce (rich-get-richer). */
|
|
117
|
+
export function blendScore(cosineSim, weights, influence) {
|
|
118
|
+
if (influence <= 0)
|
|
119
|
+
return cosineSim;
|
|
120
|
+
const usefulness = 0.7 * weights.feedback + 0.3 * Math.min(1, weights.frequency / FREQUENCY_NORM_CAP);
|
|
121
|
+
return (1 - influence) * cosineSim + influence * usefulness;
|
|
122
|
+
}
|
|
123
|
+
export function generateId(prefix) {
|
|
124
|
+
return `${prefix}_${Date.now()}_${crypto.randomBytes(8).toString('hex')}`;
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=memory-bridge-core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-bridge-core.js","sourceRoot":"","sources":["../../../src/memory/memory-bridge-core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,mCAAmC;AAEnC,MAAM,kBAAkB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